20 #include <linux/kernel.h>
21 #include <linux/pci.h>
23 #include <linux/string.h>
28 #include <linux/slab.h>
32 #include <asm/pci-bridge.h>
33 #include <asm/machdep.h>
37 static int fsl_pcie_bus_fixup, is_mpc83xx_pci;
53 fsl_pcie_bus_fixup = 1;
61 early_read_config_dword(hose, 0, 0, PCIE_LTSSM, &val);
62 if (val < PCIE_LTSSM_L0)
67 #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
69 #define MAX_PHYS_ADDR_BITS 40
70 static u64 pci64_dma_offset = 1ull << MAX_PHYS_ADDR_BITS;
72 static int fsl_pci_dma_set_mask(
struct device *dev,
u64 dma_mask)
85 set_dma_offset(dev, pci64_dma_offset);
92 static int __init setup_one_atmu(
struct ccsr_pci
__iomem *pci,
102 pr_debug(
"PCI MEM resource start 0x%016llx, size 0x%016llx.\n",
108 for (i = 0; size > 0; i++) {
109 unsigned int bits =
min(__ilog2(size),
110 __ffs(pci_addr | phys_addr));
115 out_be32(&pci->pow[index + i].potar, pci_addr >> 12);
116 out_be32(&pci->pow[index + i].potear, (
u64)pci_addr >> 44);
117 out_be32(&pci->pow[index + i].powbar, phys_addr >> 12);
118 out_be32(&pci->pow[index + i].powar, flags | (bits - 1));
133 int i,
j,
n, mem_log, win_idx = 3, start_idx = 1, end_idx = 4;
134 u64 mem, sz, paddr_hi = 0;
136 u32 pcicsrbar = 0, pcicsrbar_sz;
137 u32 piwar = PIWAR_EN | PIWAR_PF | PIWAR_TGI_LOCAL |
138 PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
139 char *
name = hose->dn->full_name;
143 pr_debug(
"PCI memory map start 0x%016llx, size 0x%016llx\n",
148 dev_err(hose->parent,
"Unable to map ATMU registers\n");
153 if (
in_be32(&pci->block_rev1) >= PCIE_IP_REV_2_2) {
161 for(i = 1; i < 5; i++)
163 for (i = start_idx; i < end_idx; i++)
167 for(i = 0, j = 1; i < 3; i++) {
175 hose->pci_mem_offset);
177 if (n < 0 || j >= 5) {
178 pr_err(
"Ran out of outbound PCI ATMUs for resource %d!\n", i);
187 pr_err(
"Ran out of outbound PCI ATMUs for IO resource\n");
189 pr_debug(
"PCI IO resource start 0x%016llx, size 0x%016llx, "
190 "phy base 0x%016llx.\n",
191 (
u64)hose->io_resource.start,
192 (
u64)resource_size(&hose->io_resource),
193 (
u64)hose->io_base_phys);
194 out_be32(&pci->pow[j].potar, (hose->io_resource.start >> 12));
196 out_be32(&pci->pow[j].powbar, (hose->io_base_phys >> 12));
198 out_be32(&pci->pow[j].powar, 0x80088000
199 | (__ilog2(hose->io_resource.end
200 - hose->io_resource.start + 1) - 1));
205 paddr_hi -= hose->pci_mem_offset;
206 paddr_lo -= hose->pci_mem_offset;
208 if (paddr_hi == paddr_lo) {
209 pr_err(
"%s: No outbound window space\n", name);
214 pr_err(
"%s: No space for inbound window\n", name);
221 pcicsrbar_sz = ~pcicsrbar_sz + 1;
223 if (paddr_hi < (0x100000000ull - pcicsrbar_sz) ||
224 (paddr_lo > 0x100000000ull))
225 pcicsrbar = 0x100000000ull - pcicsrbar_sz;
227 pcicsrbar = (paddr_lo - pcicsrbar_sz) & -pcicsrbar_sz;
230 paddr_lo =
min(paddr_lo, (
u64)pcicsrbar);
232 pr_info(
"%s: PCICSRBAR @ 0x%x\n", name, pcicsrbar);
250 if (reg && (len ==
sizeof(
u64))) {
253 if ((address >= mem) && (address < (mem +
PAGE_SIZE))) {
254 pr_info(
"%s: extending DDR ATMU to cover MSIIR", name);
258 pr_warn(
"%s: msi-address-64 address of %llx is "
259 "unsupported\n", name, address);
263 sz =
min(mem, paddr_lo);
264 mem_log = __ilog2_u64(sz);
269 if ((1ull << mem_log) != mem) {
270 if ((1ull << mem_log) > mem)
271 pr_info(
"%s: Setting PCI inbound window "
272 "greater than memory size\n", name);
276 piwar |= ((mem_log - 1) & PIWAR_SZ_MASK);
279 out_be32(&pci->piw[win_idx].pitar, 0x00000000);
280 out_be32(&pci->piw[win_idx].piwbar, 0x00000000);
281 out_be32(&pci->piw[win_idx].piwar, piwar);
284 hose->dma_window_base_cur = 0x00000000;
293 mem_log = __ilog2_u64(mem);
296 if ((1ull << mem_log) != mem)
299 piwar = (piwar & ~PIWAR_SZ_MASK) | (mem_log - 1);
302 out_be32(&pci->piw[win_idx].pitar, 0x00000000);
303 out_be32(&pci->piw[win_idx].piwbear,
304 pci64_dma_offset >> 44);
306 pci64_dma_offset >> 12);
307 out_be32(&pci->piw[win_idx].piwar, piwar);
313 ppc_md.dma_set_mask = fsl_pci_dma_set_mask;
315 pr_info(
"%s: Setup 64-bit PCI DMA window\n", name);
321 out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
322 out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
323 out_be32(&pci->piw[win_idx].piwar, (piwar | (mem_log - 1)));
326 paddr += 1ull << mem_log;
327 sz -= 1ull << mem_log;
330 mem_log = __ilog2_u64(sz);
331 piwar |= (mem_log - 1);
333 out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
334 out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
335 out_be32(&pci->piw[win_idx].piwar, piwar);
338 paddr += 1ull << mem_log;
341 hose->dma_window_base_cur = 0x00000000;
345 if (hose->dma_window_size < mem) {
346 #ifndef CONFIG_SWIOTLB
347 pr_err(
"%s: ERROR: Memory size exceeds PCI ATMU ability to "
348 "map - enable CONFIG_SWIOTLB to avoid dma errors.\n",
352 if (paddr_hi < 0xffffffffull)
353 pr_warning(
"%s: WARNING: Outbound window cfg leaves "
354 "gaps in memory map. Adjusting the memory map "
355 "could reduce unnecessary bounce buffering.\n",
358 pr_info(
"%s: DMA window size is 0x%llx\n", name,
359 (
u64)hose->dma_window_size);
371 early_read_config_word(hose, 0, 0,
PCI_COMMAND, &cmd);
374 early_write_config_word(hose, 0, 0,
PCI_COMMAND, cmd);
381 early_write_config_word(hose, 0, 0, pci_x_cmd, cmd);
387 void fsl_pcibios_fixup_bus(
struct pci_bus *
bus)
390 int i, is_pcie = 0, no_link;
400 if (fsl_pcie_bus_fixup)
402 no_link = !!(hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK);
404 if (bus->
parent == hose->bus && (is_pcie || no_link)) {
412 par = &hose->io_resource;
418 res->
end = par ? par->
end : 0;
429 const int *bus_range;
447 if (bus_range ==
NULL || len < 2 *
sizeof(
int))
451 pci_add_flags(PCI_REASSIGN_ALL_BUS);
456 hose->
first_busno = bus_range ? bus_range[0] : 0x0;
457 hose->
last_busno = bus_range ? bus_range[1] : 0xff;
460 PPC_INDIRECT_TYPE_BIG_ENDIAN);
471 if ((progif & 1) == 1)
479 hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
480 PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
481 if (fsl_pcie_check_link(hose))
482 hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
486 "Firmware bus number: %d->%d\n",
490 pr_debug(
" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
491 hose, hose->cfg_addr, hose->cfg_data);
498 setup_pci_atmu(hose, &rsrc);
504 if (((
unsigned long)hose->cfg_data &
PAGE_MASK) !=
505 ((
unsigned long)hose->cfg_addr & PAGE_MASK))
515 #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
516 struct mpc83xx_pcie_priv {
522 struct pex_inbound_window {
533 #define PEX_OUTWIN0_BAR 0xCA4
534 #define PEX_OUTWIN0_TAL 0xCA8
535 #define PEX_OUTWIN0_TAH 0xCAC
536 #define PEX_RC_INWIN_BASE 0xE60
537 #define PEX_RCIWARn_EN 0x1
539 static int mpc83xx_pcie_exclude_device(
struct pci_bus *bus,
unsigned int devfn)
543 if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
556 if (
ppc_md.pci_exclude_device) {
557 if (
ppc_md.pci_exclude_device(hose, bus->
number, devfn))
565 unsigned int devfn,
int offset)
568 struct mpc83xx_pcie_priv *pcie = hose->dn->data;
569 u32 dev_base = bus->
number << 24 | devfn << 16;
572 ret = mpc83xx_pcie_exclude_device(bus, devfn);
580 return pcie->cfg_type0 +
offset;
582 if (pcie->dev_base == dev_base)
585 out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
587 pcie->dev_base = dev_base;
589 return pcie->cfg_type1 +
offset;
592 static int mpc83xx_pcie_read_config(
struct pci_bus *bus,
unsigned int devfn,
593 int offset,
int len,
u32 *val)
597 cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
603 *val =
in_8(cfg_addr);
616 static int mpc83xx_pcie_write_config(
struct pci_bus *bus,
unsigned int devfn,
617 int offset,
int len,
u32 val)
622 cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
632 out_8(cfg_addr, val);
645 static struct pci_ops mpc83xx_pcie_ops = {
646 .
read = mpc83xx_pcie_read_config,
647 .write = mpc83xx_pcie_write_config,
653 struct mpc83xx_pcie_priv *pcie;
661 pcie->cfg_type0 =
ioremap(reg->
start, resource_size(reg));
662 if (!pcie->cfg_type0)
665 cfg_bar =
in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
672 pcie->cfg_type1 =
ioremap(cfg_bar, 0x1000);
673 if (!pcie->cfg_type1)
677 hose->dn->data = pcie;
678 hose->
ops = &mpc83xx_pcie_ops;
680 out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
681 out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
683 if (fsl_pcie_check_link(hose))
684 hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
702 const int *bus_range;
720 memset(&rsrc_cfg, 0,
sizeof(rsrc_cfg));
724 "No pci config register base in dev tree, "
731 if ((rsrc_reg.start & 0xfffff) == 0x8500)
732 rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
733 else if ((rsrc_reg.start & 0xfffff) == 0x8600)
734 rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
739 if ((rsrc_reg.start & 0xfffff) == 0x8500)
746 if (bus_range ==
NULL || len < 2 *
sizeof(
int)) {
751 pci_add_flags(PCI_REASSIGN_ALL_BUS);
757 hose->
last_busno = bus_range ? bus_range[1] : 0xff;
760 ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
765 rsrc_cfg.start + 4, 0);
769 "Firmware bus number: %d->%d\n",
770 (
unsigned long long)rsrc_reg.start, hose->
first_busno,
773 pr_debug(
" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
774 hose, hose->cfg_addr, hose->cfg_data);
789 #ifdef CONFIG_PPC_83xx
790 if (is_mpc83xx_pci) {
791 struct mpc83xx_pcie_priv *pcie = hose->dn->data;
792 struct pex_inbound_window *
in;
796 in = pcie->cfg_type0 + PEX_RC_INWIN_BASE;
797 for (i = 0; i < 4; i++) {
799 if (!
in_le32(&in[i].
ar) & PEX_RCIWARn_EN)
811 #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
812 if (!is_mpc83xx_pci) {
815 pci_bus_read_config_dword(hose->bus,
824 #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
827 { .compatible =
"fsl,mpc8548-pcie", },
828 { .compatible =
"fsl,mpc8610-pci", },
829 { .compatible =
"fsl,mpc8641-pcie", },
830 { .compatible =
"fsl,p1022-pcie", },
831 { .compatible =
"fsl,p1010-pcie", },
832 { .compatible =
"fsl,p1023-pcie", },
833 { .compatible =
"fsl,p4080-pcie", },
834 { .compatible =
"fsl,qoriq-pcie-v2.4", },
835 { .compatible =
"fsl,qoriq-pcie-v2.3", },
836 { .compatible =
"fsl,qoriq-pcie-v2.2", },
842 void fsl_pci_assign_primary(
void)
854 np = fsl_pci_primary;
865 for_each_matching_node(np, pci_ids) {
867 fsl_pci_primary = np;
878 #ifdef CONFIG_SWIOTLB
882 node = pdev->
dev.of_node;
883 ret = fsl_add_bridge(node, fsl_pci_primary == node);
885 #ifdef CONFIG_SWIOTLB
895 hose->dma_window_size)
900 mpc85xx_pci_err_probe(pdev);
908 .of_match_table = pci_ids,
910 .probe = fsl_pci_probe,
913 static int __init fsl_pci_init(
void)