21 #include <linux/kernel.h>
22 #include <linux/pci.h>
27 #include <linux/slab.h>
30 #include <asm/pci-bridge.h>
31 #include <asm/machdep.h>
38 static int dma_offset_set;
40 #define U64_TO_U32_LOW(val) ((u32)((val) & 0x00000000ffffffffULL))
41 #define U64_TO_U32_HIGH(val) ((u32)((val) >> 32))
43 #define RES_TO_U32_LOW(val) \
44 ((sizeof(resource_size_t) > sizeof(u32)) ? U64_TO_U32_LOW(val) : (val))
45 #define RES_TO_U32_HIGH(val) \
46 ((sizeof(resource_size_t) > sizeof(u32)) ? U64_TO_U32_HIGH(val) : (0))
48 static inline int ppc440spe_revA(
void)
57 static void fixup_ppc4xx_pci_bridge(
struct pci_dev *
dev)
65 hose = pci_bus_to_host(dev->
bus);
76 hose->indirect_type |= PPC_INDIRECT_TYPE_BROKEN_MRM;
114 while ((rlen -= np * 4) >= 0) {
115 u32 pci_space = ranges[0];
118 size = of_read_number(ranges + pna + 3, 2);
120 if (cpu_addr == OF_BAD_ADDR || size == 0)
124 if ((pci_space & 0x03000000) != 0x02000000)
130 if (cpu_addr != 0 || pci_addr > 0xffffffff) {
132 " 0x%016llx...0x%016llx -> 0x%016llx\n",
134 pci_addr, pci_addr + size - 1, cpu_addr);
139 if (!(pci_space & 0x40000000))
147 (pci_addr + size) > 0x100000000ull)
148 res->
end = 0xffffffff;
157 hose->dn->full_name);
166 "(size=%llx total_memory=%llx)\n",
172 if ((size & (size - 1)) != 0 ||
173 (res->
start & (size - 1)) != 0) {
175 hose->dn->full_name);
180 if (res->
end > 0xffffffff) {
182 hose->dn->full_name);
188 hose->dma_window_base_cur = res->
start;
189 hose->dma_window_size = resource_size(res);
194 (
unsigned long long)hose->dma_window_base_cur);
196 (
unsigned long long)hose->dma_window_size);
212 u32 ma, pcila, pciha;
225 plb_addr &= 0xffffffffull;
231 if ((plb_addr + size) > 0xffffffffull || !
is_power_of_2(size) ||
232 size < 0x1000 || (plb_addr & (size - 1)) != 0) {
234 hose->dn->full_name);
237 ma = (0xffffffff
u <<
ilog2(size)) | 1;
255 int i,
j, found_isa_hole = 0;
258 for (i = j = 0; i < 3; i++) {
266 hose->dn->full_name);
271 if (ppc4xx_setup_one_pci_PMM(hose, reg,
273 res->
start - hose->pci_mem_offset,
282 if (res->
start == hose->pci_mem_offset)
288 if (j <= 2 && !found_isa_hole && hose->isa_mem_size)
289 if (ppc4xx_setup_one_pci_PMM(hose, reg, hose->isa_mem_phys, 0,
290 hose->isa_mem_size, 0, j) == 0)
292 hose->dn->full_name);
303 sa = (0xffffffff
u <<
ilog2(size)) | 1;
311 early_write_config_dword(hose, hose->
first_busno, 0,
313 early_write_config_dword(hose, hose->
first_busno, 0,
315 early_write_config_word(hose, hose->
first_busno, 0,
327 const int *bus_range;
358 reg =
ioremap(rsrc_reg.start, resource_size(&rsrc_reg));
369 hose->
first_busno = bus_range ? bus_range[0] : 0x0;
370 hose->
last_busno = bus_range ? bus_range[1] : 0xff;
386 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
390 ppc4xx_configure_pci_PMMs(hose, reg);
393 ppc4xx_configure_pci_PTMs(hose, reg, &dma_window);
418 u32 lah, lal, pciah, pcial,
sa;
421 (plb_addr & (size - 1)) != 0) {
423 hose->dn->full_name);
432 sa = (0xffffffff
u <<
ilog2(size)) | 0x1;
455 int i,
j, found_isa_hole = 0;
458 for (i = j = 0; i < 3; i++) {
466 hose->dn->full_name);
471 if (ppc4xx_setup_one_pcix_POM(hose, reg,
473 res->
start - hose->pci_mem_offset,
482 if (res->
start == hose->pci_mem_offset)
488 if (j <= 1 && !found_isa_hole && hose->isa_mem_size)
489 if (ppc4xx_setup_one_pcix_POM(hose, reg, hose->isa_mem_phys, 0,
490 hose->isa_mem_size, 0, j) == 0)
492 hose->dn->full_name);
509 sa = (0xffffffff
u <<
ilog2(size)) | 1;
511 if (res->
flags & IORESOURCE_PREFETCH)
532 const int *bus_range;
533 int big_pim = 0, msi = 0, primary = 0;
564 reg =
ioremap(rsrc_reg.start, resource_size(&rsrc_reg));
575 hose->
first_busno = bus_range ? bus_range[0] : 0x0;
576 hose->
last_busno = bus_range ? bus_range[1] : 0xff;
580 PPC_INDIRECT_TYPE_SET_CFG_TYPE);
598 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
602 ppc4xx_configure_pcix_POMs(hose, reg);
605 ppc4xx_configure_pcix_PIMs(hose, reg, &dma_window, big_pim, msi);
618 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
634 #define MAX_PCIE_BUS_MAPPED 0x40
636 struct ppc4xx_pciex_port
644 unsigned int sdr_base;
651 static struct ppc4xx_pciex_port *ppc4xx_pciex_ports;
652 static unsigned int ppc4xx_pciex_port_count;
654 struct ppc4xx_pciex_hwops
658 int (*port_init_hw)(
struct ppc4xx_pciex_port *
port);
659 int (*setup_utl)(
struct ppc4xx_pciex_port *
port);
660 void (*check_link)(
struct ppc4xx_pciex_port *
port);
663 static struct ppc4xx_pciex_hwops *ppc4xx_pciex_hwops;
665 static int __init ppc4xx_pciex_wait_on_sdr(
struct ppc4xx_pciex_port *
port,
666 unsigned int sdr_offset,
673 while(timeout_ms--) {
674 val = mfdcri(SDR0, port->sdr_base + sdr_offset);
675 if ((val & mask) == value) {
676 pr_debug(
"PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
677 port->index, sdr_offset, timeout_ms, val);
685 static int __init ppc4xx_pciex_port_reset_sdr(
struct ppc4xx_pciex_port *port)
688 if (ppc4xx_pciex_wait_on_sdr(port,
PESDRn_RCSSTS, 1 << 20, 0, 10)) {
697 static void __init ppc4xx_pciex_check_link_sdr(
struct ppc4xx_pciex_port *port)
708 if (!port->has_ibpre ||
710 1 << 28, 1 << 28, 100)) {
712 "PCIE%d: Device detected, waiting for link...\n",
715 0x1000, 0x1000, 2000))
717 "PCIE%d: Link up failed\n", port->index);
720 "PCIE%d: link is up !\n", port->index);
732 u32 valPE0, valPE1, valPE2;
743 pr_debug(
"PCIE: SDR0_PLLLCT1 already reset.\n");
754 if (!(valPE0 & 0x01000000) ||
755 !(valPE1 & 0x01000000) ||
756 !(valPE2 & 0x01000000)) {
762 if (!(valPE0 & 0x00010000) ||
763 !(valPE1 & 0x00010000) ||
764 !(valPE2 & 0x00010000)) {
770 if ((valPE0 & 0x00001000) ||
771 (valPE1 & 0x00001000) ||
772 (valPE2 & 0x00001000)) {
778 if ((valPE0 & 0x10000000) ||
779 (valPE1 & 0x10000000) ||
780 (valPE2 & 0x10000000)) {
786 if ((valPE0 & 0x00100000) ||
787 (valPE1 & 0x00100000) ||
788 (valPE2 & 0x00100000)) {
794 if ((valPE0 & 0x00000100) ||
795 (valPE1 & 0x00000100) ||
796 (valPE2 & 0x00000100)) {
813 if (ppc440spe_pciex_check_reset(np))
839 pr_debug(
"PCIE initialization OK\n");
844 static int __init ppc440spe_pciex_init_port_hw(
struct ppc4xx_pciex_port *port)
853 if (port->index == 0)
860 if (ppc440spe_revA())
866 if (port->index == 0) {
877 (1 << 24) | (1 << 16), 1 << 12);
879 return ppc4xx_pciex_port_reset_sdr(port);
882 static int __init ppc440speA_pciex_init_port_hw(
struct ppc4xx_pciex_port *port)
884 return ppc440spe_pciex_init_port_hw(port);
887 static int __init ppc440speB_pciex_init_port_hw(
struct ppc4xx_pciex_port *port)
889 int rc = ppc440spe_pciex_init_port_hw(port);
896 static int ppc440speA_pciex_init_utl(
struct ppc4xx_pciex_port *port)
916 static int ppc440speB_pciex_init_utl(
struct ppc4xx_pciex_port *port)
924 static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops
__initdata =
927 .core_init = ppc440spe_pciex_core_init,
928 .port_init_hw = ppc440speA_pciex_init_port_hw,
929 .setup_utl = ppc440speA_pciex_init_utl,
930 .check_link = ppc4xx_pciex_check_link_sdr,
933 static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata =
936 .core_init = ppc440spe_pciex_core_init,
937 .port_init_hw = ppc440speB_pciex_init_port_hw,
938 .setup_utl = ppc440speB_pciex_init_utl,
939 .check_link = ppc4xx_pciex_check_link_sdr,
948 static int __init ppc460ex_pciex_init_port_hw(
struct ppc4xx_pciex_port *port)
958 if (port->index == 0) {
960 utlset1 = 0x20000000;
963 utlset1 = 0x20101101;
970 switch (port->index) {
1003 switch (port->index) {
1019 port->has_ibpre = 1;
1021 return ppc4xx_pciex_port_reset_sdr(port);
1024 static int ppc460ex_pciex_init_utl(
struct ppc4xx_pciex_port *port)
1044 static struct ppc4xx_pciex_hwops ppc460ex_pcie_hwops __initdata =
1047 .core_init = ppc460ex_pciex_core_init,
1048 .port_init_hw = ppc460ex_pciex_init_port_hw,
1049 .setup_utl = ppc460ex_pciex_init_utl,
1050 .check_link = ppc4xx_pciex_check_link_sdr,
1059 static int apm821xx_pciex_init_port_hw(
struct ppc4xx_pciex_port *port)
1099 if (ppc4xx_pciex_wait_on_sdr(port, val, 0x1, 1, 100)) {
1108 port->has_ibpre = 1;
1113 static struct ppc4xx_pciex_hwops apm821xx_pcie_hwops __initdata = {
1115 .core_init = apm821xx_pciex_core_init,
1116 .port_init_hw = apm821xx_pciex_init_port_hw,
1117 .setup_utl = ppc460ex_pciex_init_utl,
1118 .check_link = ppc4xx_pciex_check_link_sdr,
1207 static int __init ppc460sx_pciex_init_port_hw(
struct ppc4xx_pciex_port *port)
1221 port->has_ibpre = 1;
1223 return ppc4xx_pciex_port_reset_sdr(port);
1226 static int ppc460sx_pciex_init_utl(
struct ppc4xx_pciex_port *port)
1235 static void __init ppc460sx_pciex_check_link(
struct ppc4xx_pciex_port *port)
1242 mbase =
ioremap(port->cfg_space.start + 0x10000000, 0x1000);
1243 if (mbase ==
NULL) {
1245 port->node->full_name);
1261 static struct ppc4xx_pciex_hwops ppc460sx_pcie_hwops __initdata = {
1263 .core_init = ppc460sx_pciex_core_init,
1264 .port_init_hw = ppc460sx_pciex_init_port_hw,
1265 .setup_utl = ppc460sx_pciex_init_utl,
1266 .check_link = ppc460sx_pciex_check_link,
1279 static void ppc405ex_pcie_phy_reset(
struct ppc4xx_pciex_port *port)
1300 static int __init ppc405ex_pciex_init_port_hw(
struct ppc4xx_pciex_port *port)
1310 1 << 24 | val << 20 |
LNKW_X1 << 12);
1325 if (!(val & 0x00001000))
1326 ppc405ex_pcie_phy_reset(port);
1330 port->has_ibpre = 1;
1332 return ppc4xx_pciex_port_reset_sdr(port);
1335 static int ppc405ex_pciex_init_utl(
struct ppc4xx_pciex_port *port)
1356 static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata =
1359 .core_init = ppc405ex_pciex_core_init,
1360 .port_init_hw = ppc405ex_pciex_init_port_hw,
1361 .setup_utl = ppc405ex_pciex_init_utl,
1362 .check_link = ppc4xx_pciex_check_link_sdr,
1367 #ifdef CONFIG_476FPE
1373 static void __init ppc_476fpe_pciex_check_link(
struct ppc4xx_pciex_port *port)
1375 u32 timeout_ms = 20;
1377 void __iomem *mbase =
ioremap(port->cfg_space.start + 0x10000000,
1382 if (mbase ==
NULL) {
1388 while (timeout_ms--) {
1391 if ((val & mask) == mask)
1406 static struct ppc4xx_pciex_hwops ppc_476fpe_pcie_hwops __initdata =
1408 .core_init = ppc_476fpe_pciex_core_init,
1409 .check_link = ppc_476fpe_pciex_check_link,
1416 static int core_init;
1424 if (ppc440spe_revA())
1425 ppc4xx_pciex_hwops = &ppc440speA_pcie_hwops;
1427 ppc4xx_pciex_hwops = &ppc440speB_pcie_hwops;
1430 ppc4xx_pciex_hwops = &ppc460ex_pcie_hwops;
1432 ppc4xx_pciex_hwops = &ppc460sx_pcie_hwops;
1434 ppc4xx_pciex_hwops = &apm821xx_pcie_hwops;
1438 ppc4xx_pciex_hwops = &ppc405ex_pcie_hwops;
1440 #ifdef CONFIG_476FPE
1442 ppc4xx_pciex_hwops = &ppc_476fpe_pcie_hwops;
1444 if (ppc4xx_pciex_hwops ==
NULL) {
1450 count = ppc4xx_pciex_hwops->core_init(np);
1452 ppc4xx_pciex_ports =
1453 kzalloc(count *
sizeof(
struct ppc4xx_pciex_port),
1455 if (ppc4xx_pciex_ports) {
1456 ppc4xx_pciex_port_count =
count;
1465 static void __init ppc4xx_pciex_port_init_mapping(
struct ppc4xx_pciex_port *port)
1492 static int __init ppc4xx_pciex_port_init(
struct ppc4xx_pciex_port *port)
1497 if (ppc4xx_pciex_hwops->port_init_hw)
1498 rc = ppc4xx_pciex_hwops->port_init_hw(port);
1506 ppc4xx_pciex_port_init_mapping(port);
1508 if (ppc4xx_pciex_hwops->check_link)
1509 ppc4xx_pciex_hwops->check_link(port);
1514 port->utl_base =
ioremap(port->utl_regs.start, 0x100);
1520 if (ppc4xx_pciex_hwops->setup_utl)
1521 ppc4xx_pciex_hwops->setup_utl(port);
1526 if (port->sdr_base) {
1528 "ibm,plb-pciex-460sx")){
1529 if (port->link && ppc4xx_pciex_wait_on_sdr(port,
1531 1 << 12, 1 << 12, 5000)) {
1536 }
else if (port->link &&
1538 1 << 16, 1 << 16, 5000)) {
1544 dcri_clrset(SDR0, port->sdr_base +
PESDRn_RCSSET, 0, 1 << 20);
1552 static int ppc4xx_pciex_validate_bdf(
struct ppc4xx_pciex_port *port,
1559 if (port->endpoint && bus->
number != port->hose->first_busno)
1563 if (bus->
number > port->hose->last_busno) {
1566 " out of range !\n", bus->
number);
1573 if (bus->
number == port->hose->first_busno && devfn != 0)
1577 if (bus->
number == (port->hose->first_busno + 1) &&
1582 if ((bus->
number != port->hose->first_busno) && !port->link)
1588 static void __iomem *ppc4xx_pciex_get_config_base(
struct ppc4xx_pciex_port *port,
1597 if (bus->
number == port->hose->first_busno)
1598 return (
void __iomem *)port->hose->cfg_addr;
1600 relbus = bus->
number - (port->hose->first_busno + 1);
1601 return (
void __iomem *)port->hose->cfg_data +
1602 ((relbus << 20) | (devfn << 12));
1605 static int ppc4xx_pciex_read_config(
struct pci_bus *bus,
unsigned int devfn,
1609 struct ppc4xx_pciex_port *port =
1610 &ppc4xx_pciex_ports[hose->indirect_type];
1614 BUG_ON(hose != port->hose);
1616 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1619 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1634 *val =
in_8((
u8 *)(addr + offset));
1644 pr_debug(
"pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
1645 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1647 devfn, offset, len, addr + offset, *val);
1652 if (len != 4 || offset != 0)
1662 static int ppc4xx_pciex_write_config(
struct pci_bus *bus,
unsigned int devfn,
1663 int offset,
int len,
u32 val)
1666 struct ppc4xx_pciex_port *port =
1667 &ppc4xx_pciex_ports[hose->indirect_type];
1671 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1674 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1684 pr_debug(
"pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
1685 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1687 devfn, offset, len, addr + offset, val);
1691 out_8((
u8 *)(addr + offset), val);
1706 static struct pci_ops ppc4xx_pciex_pci_ops =
1708 .
read = ppc4xx_pciex_read_config,
1709 .write = ppc4xx_pciex_write_config,
1712 static int __init ppc4xx_setup_one_pciex_POM(
struct ppc4xx_pciex_port *port,
1721 u32 lah, lal, pciah, pcial,
sa;
1724 (index < 2 && size < 0x100000) ||
1725 (index == 2 && size < 0x100) ||
1726 (plb_addr & (size - 1)) != 0) {
1728 hose->dn->full_name);
1737 sa = (0xffffffff
u <<
ilog2(size)) | 0x1;
1786 static void __init ppc4xx_configure_pciex_POMs(
struct ppc4xx_pciex_port *port,
1790 int i,
j, found_isa_hole = 0;
1793 for (i = j = 0; i < 3; i++) {
1801 port->node->full_name);
1806 if (ppc4xx_setup_one_pciex_POM(port, hose, mbase,
1808 res->
start - hose->pci_mem_offset,
1817 if (res->
start == hose->pci_mem_offset)
1823 if (j <= 1 && !found_isa_hole && hose->isa_mem_size)
1824 if (ppc4xx_setup_one_pciex_POM(port, hose, mbase,
1825 hose->isa_mem_phys, 0,
1826 hose->isa_mem_size, 0, j) == 0)
1828 hose->dn->full_name);
1834 ppc4xx_setup_one_pciex_POM(port, hose, mbase,
1835 hose->io_base_phys, 0,
1839 static void __init ppc4xx_configure_pciex_PIMs(
struct ppc4xx_pciex_port *port,
1847 if (port->endpoint) {
1857 sa = (0xffffffffffffffffull <<
ilog2(ep_size));
1876 sa = (0xffffffffffffffffull <<
ilog2(size));
1877 if (res->
flags & IORESOURCE_PREFETCH)
1910 static void __init ppc4xx_pciex_port_setup_hose(
struct ppc4xx_pciex_port *port)
1914 const int *bus_range;
1915 int primary = 0, busses;
1935 hose->indirect_type = port->index;
1938 hose->
first_busno = bus_range ? bus_range[0] : 0x0;
1939 hose->
last_busno = bus_range ? bus_range[1] : 0xff;
1947 if (busses > MAX_PCIE_BUS_MAPPED) {
1948 busses = MAX_PCIE_BUS_MAPPED;
1952 if (!port->endpoint) {
1956 cfg_data =
ioremap(port->cfg_space.start +
1959 if (cfg_data ==
NULL) {
1961 port->node->full_name);
1964 hose->cfg_data = cfg_data;
1970 mbase =
ioremap(port->cfg_space.start + 0x10000000, 0x1000);
1971 if (mbase ==
NULL) {
1973 port->node->full_name);
1976 hose->cfg_addr = mbase;
1978 pr_debug(
"PCIE %s, bus %d..%d\n", port->node->full_name,
1980 pr_debug(
" config space mapped at: root @0x%p, other @0x%p\n",
1981 hose->cfg_addr, hose->cfg_data);
1984 hose->
ops = &ppc4xx_pciex_pci_ops;
1986 mbase = (
void __iomem *)hose->cfg_addr;
1988 if (!port->endpoint) {
2006 if (ppc4xx_parse_dma_ranges(hose, mbase, &dma_window) != 0)
2010 ppc4xx_configure_pciex_POMs(port, hose, mbase);
2013 ppc4xx_configure_pciex_PIMs(port, hose, mbase, &dma_window);
2027 if (!port->endpoint)
2028 val = 0xaaa0 + port->index;
2030 val = 0xeee0 + port->index;
2038 if (!port->endpoint)
2039 val = 0xbed0 + port->index;
2041 val = 0xfed0 + port->index;
2049 if (!port->endpoint) {
2051 out_le32(mbase + 0x208, 0x06040001);
2057 out_le32(mbase + 0x208, 0x0b200001);
2075 struct ppc4xx_pciex_port *
port;
2084 if (ppc4xx_pciex_check_core_init(np))
2095 if (portno >= ppc4xx_pciex_port_count) {
2100 port = &ppc4xx_pciex_ports[
portno];
2107 printk(
KERN_INFO "PCIE%d: Port disabled via device-tree\n", port->index);
2111 port->node = of_node_get(np);
2112 if (ppc4xx_pciex_hwops->want_sdr) {
2119 port->sdr_base = *pval;
2127 if (!
strcmp(val,
"pci-endpoint")) {
2129 }
else if (!
strcmp(val,
"pci")) {
2132 printk(
KERN_ERR "PCIE: missing or incorrect device_type for %s\n",
2160 if (ppc4xx_pciex_port_init(port)) {
2166 ppc4xx_pciex_port_setup_hose(port);
2171 static int __init ppc4xx_pci_find_bridges(
void)
2175 pci_add_flags(PCI_ENABLE_PROC_DOMAINS | PCI_COMPAT_DOMAIN_0);
2177 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
2178 for_each_compatible_node(np,
NULL,
"ibm,plb-pciex")
2179 ppc4xx_probe_pciex_bridge(np);
2181 for_each_compatible_node(np,
NULL,
"ibm,plb-pcix")
2182 ppc4xx_probe_pcix_bridge(np);
2183 for_each_compatible_node(np,
NULL, "ibm,plb-pci")
2184 ppc4xx_probe_pci_bridge(np);