2 * PCI / PCI-X / PCI-Express support for 4xx parts
4 * Copyright 2007 Ben. Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
6 * Most PCI Express code is coming from Stefan Roese implementation for
7 * arch/ppc in the Denx tree, slightly reworked by me.
9 * Copyright 2007 DENX Software Engineering, Stefan Roese <sr@denx.de>
11 * Some of that comes itself from a previous implementation for 440SPE only
14 * Copyright (c) 2005 Cisco Systems. All rights reserved.
15 * Roland Dreier <rolandd@cisco.com>
21 #include <linux/kernel.h>
22 #include <linux/pci.h>
23 #include <linux/init.h>
25 #include <linux/bootmem.h>
26 #include <linux/delay.h>
29 #include <asm/pci-bridge.h>
30 #include <asm/machdep.h>
32 #include <asm/dcr-regs.h>
33 #include <mm/mmu_decl.h>
35 #include "ppc4xx_pci.h"
37 static int dma_offset_set;
39 #define U64_TO_U32_LOW(val) ((u32)((val) & 0x00000000ffffffffULL))
40 #define U64_TO_U32_HIGH(val) ((u32)((val) >> 32))
42 #ifdef CONFIG_RESOURCES_64BIT
43 #define RES_TO_U32_LOW(val) U64_TO_U32_LOW(val)
44 #define RES_TO_U32_HIGH(val) U64_TO_U32_HIGH(val)
46 #define RES_TO_U32_LOW(val) (val)
47 #define RES_TO_U32_HIGH(val) (0)
50 static inline int ppc440spe_revA(void)
52 /* Catch both 440SPe variants, with and without RAID6 support */
53 if ((mfspr(SPRN_PVR) & 0xffefffff) == 0x53421890)
59 static void fixup_ppc4xx_pci_bridge(struct pci_dev *dev)
61 struct pci_controller *hose;
64 if (dev->devfn != 0 || dev->bus->self != NULL)
67 hose = pci_bus_to_host(dev->bus);
71 if (!of_device_is_compatible(hose->dn, "ibm,plb-pciex") &&
72 !of_device_is_compatible(hose->dn, "ibm,plb-pcix") &&
73 !of_device_is_compatible(hose->dn, "ibm,plb-pci"))
76 if (of_device_is_compatible(hose->dn, "ibm,plb440epx-pci") ||
77 of_device_is_compatible(hose->dn, "ibm,plb440grx-pci")) {
78 hose->indirect_type |= PPC_INDIRECT_TYPE_BROKEN_MRM;
81 /* Hide the PCI host BARs from the kernel as their content doesn't
82 * fit well in the resource management
84 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
85 dev->resource[i].start = dev->resource[i].end = 0;
86 dev->resource[i].flags = 0;
89 printk(KERN_INFO "PCI: Hiding 4xx host bridge resources %s\n",
92 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, fixup_ppc4xx_pci_bridge);
94 static int __init ppc4xx_parse_dma_ranges(struct pci_controller *hose,
101 int pna = of_n_addr_cells(hose->dn);
108 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
110 /* Get dma-ranges property */
111 ranges = of_get_property(hose->dn, "dma-ranges", &rlen);
116 while ((rlen -= np * 4) >= 0) {
117 u32 pci_space = ranges[0];
118 u64 pci_addr = of_read_number(ranges + 1, 2);
119 u64 cpu_addr = of_translate_dma_address(hose->dn, ranges + 3);
120 size = of_read_number(ranges + pna + 3, 2);
122 if (cpu_addr == OF_BAD_ADDR || size == 0)
125 /* We only care about memory */
126 if ((pci_space & 0x03000000) != 0x02000000)
129 /* We currently only support memory at 0, and pci_addr
130 * within 32 bits space
132 if (cpu_addr != 0 || pci_addr > 0xffffffff) {
133 printk(KERN_WARNING "%s: Ignored unsupported dma range"
134 " 0x%016llx...0x%016llx -> 0x%016llx\n",
136 pci_addr, pci_addr + size - 1, cpu_addr);
140 /* Check if not prefetchable */
141 if (!(pci_space & 0x40000000))
142 res->flags &= ~IORESOURCE_PREFETCH;
146 res->start = pci_addr;
147 #ifndef CONFIG_RESOURCES_64BIT
148 /* Beware of 32 bits resources */
149 if ((pci_addr + size) > 0x100000000ull)
150 res->end = 0xffffffff;
153 res->end = res->start + size - 1;
157 /* We only support one global DMA offset */
158 if (dma_offset_set && pci_dram_offset != res->start) {
159 printk(KERN_ERR "%s: dma-ranges(s) mismatch\n",
160 hose->dn->full_name);
164 /* Check that we can fit all of memory as we don't support
167 if (size < total_memory) {
168 printk(KERN_ERR "%s: dma-ranges too small "
169 "(size=%llx total_memory=%llx)\n",
170 hose->dn->full_name, size, (u64)total_memory);
174 /* Check we are a power of 2 size and that base is a multiple of size*/
175 if ((size & (size - 1)) != 0 ||
176 (res->start & (size - 1)) != 0) {
177 printk(KERN_ERR "%s: dma-ranges unaligned\n",
178 hose->dn->full_name);
182 /* Check that we are fully contained within 32 bits space */
183 if (res->end > 0xffffffff) {
184 printk(KERN_ERR "%s: dma-ranges outside of 32 bits space\n",
185 hose->dn->full_name);
190 pci_dram_offset = res->start;
192 printk(KERN_INFO "4xx PCI DMA offset set to 0x%08lx\n",
201 static void __init ppc4xx_configure_pci_PMMs(struct pci_controller *hose,
204 u32 la, ma, pcila, pciha;
207 /* Setup outbound memory windows */
208 for (i = j = 0; i < 3; i++) {
209 struct resource *res = &hose->mem_resources[i];
211 /* we only care about memory windows */
212 if (!(res->flags & IORESOURCE_MEM))
215 printk(KERN_WARNING "%s: Too many ranges\n",
216 hose->dn->full_name);
220 /* Calculate register values */
222 pciha = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
223 pcila = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
225 ma = res->end + 1 - res->start;
226 if (!is_power_of_2(ma) || ma < 0x1000 || ma > 0xffffffffu) {
227 printk(KERN_WARNING "%s: Resource out of range\n",
228 hose->dn->full_name);
231 ma = (0xffffffffu << ilog2(ma)) | 0x1;
232 if (res->flags & IORESOURCE_PREFETCH)
235 /* Program register values */
236 writel(la, reg + PCIL0_PMM0LA + (0x10 * j));
237 writel(pcila, reg + PCIL0_PMM0PCILA + (0x10 * j));
238 writel(pciha, reg + PCIL0_PMM0PCIHA + (0x10 * j));
239 writel(ma, reg + PCIL0_PMM0MA + (0x10 * j));
244 static void __init ppc4xx_configure_pci_PTMs(struct pci_controller *hose,
246 const struct resource *res)
248 resource_size_t size = res->end - res->start + 1;
251 /* Calculate window size */
252 sa = (0xffffffffu << ilog2(size)) | 1;
255 /* RAM is always at 0 local for now */
256 writel(0, reg + PCIL0_PTM1LA);
257 writel(sa, reg + PCIL0_PTM1MS);
259 /* Map on PCI side */
260 early_write_config_dword(hose, hose->first_busno, 0,
261 PCI_BASE_ADDRESS_1, res->start);
262 early_write_config_dword(hose, hose->first_busno, 0,
263 PCI_BASE_ADDRESS_2, 0x00000000);
264 early_write_config_word(hose, hose->first_busno, 0,
265 PCI_COMMAND, 0x0006);
268 static void __init ppc4xx_probe_pci_bridge(struct device_node *np)
271 struct resource rsrc_cfg;
272 struct resource rsrc_reg;
273 struct resource dma_window;
274 struct pci_controller *hose = NULL;
275 void __iomem *reg = NULL;
276 const int *bus_range;
279 /* Fetch config space registers address */
280 if (of_address_to_resource(np, 0, &rsrc_cfg)) {
281 printk(KERN_ERR "%s:Can't get PCI config register base !",
285 /* Fetch host bridge internal registers address */
286 if (of_address_to_resource(np, 3, &rsrc_reg)) {
287 printk(KERN_ERR "%s: Can't get PCI internal register base !",
292 /* Check if primary bridge */
293 if (of_get_property(np, "primary", NULL))
296 /* Get bus range if any */
297 bus_range = of_get_property(np, "bus-range", NULL);
300 reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
302 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
306 /* Allocate the host controller data structure */
307 hose = pcibios_alloc_controller(np);
311 hose->first_busno = bus_range ? bus_range[0] : 0x0;
312 hose->last_busno = bus_range ? bus_range[1] : 0xff;
314 /* Setup config space */
315 setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
317 /* Disable all windows */
318 writel(0, reg + PCIL0_PMM0MA);
319 writel(0, reg + PCIL0_PMM1MA);
320 writel(0, reg + PCIL0_PMM2MA);
321 writel(0, reg + PCIL0_PTM1MS);
322 writel(0, reg + PCIL0_PTM2MS);
324 /* Parse outbound mapping resources */
325 pci_process_bridge_OF_ranges(hose, np, primary);
327 /* Parse inbound mapping resources */
328 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
331 /* Configure outbound ranges POMs */
332 ppc4xx_configure_pci_PMMs(hose, reg);
334 /* Configure inbound ranges PIMs */
335 ppc4xx_configure_pci_PTMs(hose, reg, &dma_window);
337 /* We don't need the registers anymore */
343 pcibios_free_controller(hose);
352 static void __init ppc4xx_configure_pcix_POMs(struct pci_controller *hose,
355 u32 lah, lal, pciah, pcial, sa;
358 /* Setup outbound memory windows */
359 for (i = j = 0; i < 3; i++) {
360 struct resource *res = &hose->mem_resources[i];
362 /* we only care about memory windows */
363 if (!(res->flags & IORESOURCE_MEM))
366 printk(KERN_WARNING "%s: Too many ranges\n",
367 hose->dn->full_name);
371 /* Calculate register values */
372 lah = RES_TO_U32_HIGH(res->start);
373 lal = RES_TO_U32_LOW(res->start);
374 pciah = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
375 pcial = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
376 sa = res->end + 1 - res->start;
377 if (!is_power_of_2(sa) || sa < 0x100000 ||
379 printk(KERN_WARNING "%s: Resource out of range\n",
380 hose->dn->full_name);
383 sa = (0xffffffffu << ilog2(sa)) | 0x1;
385 /* Program register values */
387 writel(lah, reg + PCIX0_POM0LAH);
388 writel(lal, reg + PCIX0_POM0LAL);
389 writel(pciah, reg + PCIX0_POM0PCIAH);
390 writel(pcial, reg + PCIX0_POM0PCIAL);
391 writel(sa, reg + PCIX0_POM0SA);
393 writel(lah, reg + PCIX0_POM1LAH);
394 writel(lal, reg + PCIX0_POM1LAL);
395 writel(pciah, reg + PCIX0_POM1PCIAH);
396 writel(pcial, reg + PCIX0_POM1PCIAL);
397 writel(sa, reg + PCIX0_POM1SA);
403 static void __init ppc4xx_configure_pcix_PIMs(struct pci_controller *hose,
405 const struct resource *res,
409 resource_size_t size = res->end - res->start + 1;
412 /* RAM is always at 0 */
413 writel(0x00000000, reg + PCIX0_PIM0LAH);
414 writel(0x00000000, reg + PCIX0_PIM0LAL);
416 /* Calculate window size */
417 sa = (0xffffffffu << ilog2(size)) | 1;
419 if (res->flags & IORESOURCE_PREFETCH)
423 writel(sa, reg + PCIX0_PIM0SA);
425 writel(0xffffffff, reg + PCIX0_PIM0SAH);
427 /* Map on PCI side */
428 writel(0x00000000, reg + PCIX0_BAR0H);
429 writel(res->start, reg + PCIX0_BAR0L);
430 writew(0x0006, reg + PCIX0_COMMAND);
433 static void __init ppc4xx_probe_pcix_bridge(struct device_node *np)
435 struct resource rsrc_cfg;
436 struct resource rsrc_reg;
437 struct resource dma_window;
438 struct pci_controller *hose = NULL;
439 void __iomem *reg = NULL;
440 const int *bus_range;
441 int big_pim = 0, msi = 0, primary = 0;
443 /* Fetch config space registers address */
444 if (of_address_to_resource(np, 0, &rsrc_cfg)) {
445 printk(KERN_ERR "%s:Can't get PCI-X config register base !",
449 /* Fetch host bridge internal registers address */
450 if (of_address_to_resource(np, 3, &rsrc_reg)) {
451 printk(KERN_ERR "%s: Can't get PCI-X internal register base !",
456 /* Check if it supports large PIMs (440GX) */
457 if (of_get_property(np, "large-inbound-windows", NULL))
460 /* Check if we should enable MSIs inbound hole */
461 if (of_get_property(np, "enable-msi-hole", NULL))
464 /* Check if primary bridge */
465 if (of_get_property(np, "primary", NULL))
468 /* Get bus range if any */
469 bus_range = of_get_property(np, "bus-range", NULL);
472 reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
474 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
478 /* Allocate the host controller data structure */
479 hose = pcibios_alloc_controller(np);
483 hose->first_busno = bus_range ? bus_range[0] : 0x0;
484 hose->last_busno = bus_range ? bus_range[1] : 0xff;
486 /* Setup config space */
487 setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
489 /* Disable all windows */
490 writel(0, reg + PCIX0_POM0SA);
491 writel(0, reg + PCIX0_POM1SA);
492 writel(0, reg + PCIX0_POM2SA);
493 writel(0, reg + PCIX0_PIM0SA);
494 writel(0, reg + PCIX0_PIM1SA);
495 writel(0, reg + PCIX0_PIM2SA);
497 writel(0, reg + PCIX0_PIM0SAH);
498 writel(0, reg + PCIX0_PIM2SAH);
501 /* Parse outbound mapping resources */
502 pci_process_bridge_OF_ranges(hose, np, primary);
504 /* Parse inbound mapping resources */
505 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
508 /* Configure outbound ranges POMs */
509 ppc4xx_configure_pcix_POMs(hose, reg);
511 /* Configure inbound ranges PIMs */
512 ppc4xx_configure_pcix_PIMs(hose, reg, &dma_window, big_pim, msi);
514 /* We don't need the registers anymore */
520 pcibios_free_controller(hose);
525 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
528 * 4xx PCI-Express part
530 * We support 3 parts currently based on the compatible property:
532 * ibm,plb-pciex-440spe
533 * ibm,plb-pciex-405ex
534 * ibm,plb-pciex-460ex
536 * Anything else will be rejected for now as they are all subtly
537 * different unfortunately.
541 #define MAX_PCIE_BUS_MAPPED 0x40
543 struct ppc4xx_pciex_port
545 struct pci_controller *hose;
546 struct device_node *node;
551 unsigned int sdr_base;
553 struct resource cfg_space;
554 struct resource utl_regs;
555 void __iomem *utl_base;
558 static struct ppc4xx_pciex_port *ppc4xx_pciex_ports;
559 static unsigned int ppc4xx_pciex_port_count;
561 struct ppc4xx_pciex_hwops
563 int (*core_init)(struct device_node *np);
564 int (*port_init_hw)(struct ppc4xx_pciex_port *port);
565 int (*setup_utl)(struct ppc4xx_pciex_port *port);
568 static struct ppc4xx_pciex_hwops *ppc4xx_pciex_hwops;
572 /* Check various reset bits of the 440SPe PCIe core */
573 static int __init ppc440spe_pciex_check_reset(struct device_node *np)
575 u32 valPE0, valPE1, valPE2;
578 /* SDR0_PEGPLLLCT1 reset */
579 if (!(mfdcri(SDR0, PESDR0_PLLLCT1) & 0x01000000)) {
581 * the PCIe core was probably already initialised
582 * by firmware - let's re-reset RCSSET regs
584 * -- Shouldn't we also re-reset the whole thing ? -- BenH
586 pr_debug("PCIE: SDR0_PLLLCT1 already reset.\n");
587 mtdcri(SDR0, PESDR0_440SPE_RCSSET, 0x01010000);
588 mtdcri(SDR0, PESDR1_440SPE_RCSSET, 0x01010000);
589 mtdcri(SDR0, PESDR2_440SPE_RCSSET, 0x01010000);
592 valPE0 = mfdcri(SDR0, PESDR0_440SPE_RCSSET);
593 valPE1 = mfdcri(SDR0, PESDR1_440SPE_RCSSET);
594 valPE2 = mfdcri(SDR0, PESDR2_440SPE_RCSSET);
596 /* SDR0_PExRCSSET rstgu */
597 if (!(valPE0 & 0x01000000) ||
598 !(valPE1 & 0x01000000) ||
599 !(valPE2 & 0x01000000)) {
600 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstgu error\n");
604 /* SDR0_PExRCSSET rstdl */
605 if (!(valPE0 & 0x00010000) ||
606 !(valPE1 & 0x00010000) ||
607 !(valPE2 & 0x00010000)) {
608 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstdl error\n");
612 /* SDR0_PExRCSSET rstpyn */
613 if ((valPE0 & 0x00001000) ||
614 (valPE1 & 0x00001000) ||
615 (valPE2 & 0x00001000)) {
616 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstpyn error\n");
620 /* SDR0_PExRCSSET hldplb */
621 if ((valPE0 & 0x10000000) ||
622 (valPE1 & 0x10000000) ||
623 (valPE2 & 0x10000000)) {
624 printk(KERN_INFO "PCIE: SDR0_PExRCSSET hldplb error\n");
628 /* SDR0_PExRCSSET rdy */
629 if ((valPE0 & 0x00100000) ||
630 (valPE1 & 0x00100000) ||
631 (valPE2 & 0x00100000)) {
632 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rdy error\n");
636 /* SDR0_PExRCSSET shutdown */
637 if ((valPE0 & 0x00000100) ||
638 (valPE1 & 0x00000100) ||
639 (valPE2 & 0x00000100)) {
640 printk(KERN_INFO "PCIE: SDR0_PExRCSSET shutdown error\n");
647 /* Global PCIe core initializations for 440SPe core */
648 static int __init ppc440spe_pciex_core_init(struct device_node *np)
652 /* Set PLL clock receiver to LVPECL */
653 dcri_clrset(SDR0, PESDR0_PLLLCT1, 0, 1 << 28);
655 /* Shouldn't we do all the calibration stuff etc... here ? */
656 if (ppc440spe_pciex_check_reset(np))
659 if (!(mfdcri(SDR0, PESDR0_PLLLCT2) & 0x10000)) {
660 printk(KERN_INFO "PCIE: PESDR_PLLCT2 resistance calibration "
662 mfdcri(SDR0, PESDR0_PLLLCT2));
666 /* De-assert reset of PCIe PLL, wait for lock */
667 dcri_clrset(SDR0, PESDR0_PLLLCT1, 1 << 24, 0);
671 if (!(mfdcri(SDR0, PESDR0_PLLLCT3) & 0x10000000)) {
678 printk(KERN_INFO "PCIE: VCO output not locked\n");
682 pr_debug("PCIE initialization OK\n");
687 static int ppc440spe_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
692 val = PTYPE_LEGACY_ENDPOINT << 20;
694 val = PTYPE_ROOT_PORT << 20;
696 if (port->index == 0)
697 val |= LNKW_X8 << 12;
699 val |= LNKW_X4 << 12;
701 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
702 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x20222222);
703 if (ppc440spe_revA())
704 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x11000000);
705 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL0SET1, 0x35000000);
706 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL1SET1, 0x35000000);
707 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL2SET1, 0x35000000);
708 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL3SET1, 0x35000000);
709 if (port->index == 0) {
710 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL4SET1,
712 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL5SET1,
714 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL6SET1,
716 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL7SET1,
719 dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET,
720 (1 << 24) | (1 << 16), 1 << 12);
725 static int ppc440speA_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
727 return ppc440spe_pciex_init_port_hw(port);
730 static int ppc440speB_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
732 int rc = ppc440spe_pciex_init_port_hw(port);
739 static int ppc440speA_pciex_init_utl(struct ppc4xx_pciex_port *port)
741 /* XXX Check what that value means... I hate magic */
742 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x68782800);
745 * Set buffer allocations and then assert VRB and TXE.
747 out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
748 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
749 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x10000000);
750 out_be32(port->utl_base + PEUTL_PBBSZ, 0x53000000);
751 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x08000000);
752 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x10000000);
753 out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
754 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
759 static int ppc440speB_pciex_init_utl(struct ppc4xx_pciex_port *port)
761 /* Report CRS to the operating system */
762 out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
767 static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops __initdata =
769 .core_init = ppc440spe_pciex_core_init,
770 .port_init_hw = ppc440speA_pciex_init_port_hw,
771 .setup_utl = ppc440speA_pciex_init_utl,
774 static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata =
776 .core_init = ppc440spe_pciex_core_init,
777 .port_init_hw = ppc440speB_pciex_init_port_hw,
778 .setup_utl = ppc440speB_pciex_init_utl,
781 static int __init ppc460ex_pciex_core_init(struct device_node *np)
783 /* Nothing to do, return 2 ports */
787 static int ppc460ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
793 val = PTYPE_LEGACY_ENDPOINT << 20;
795 val = PTYPE_ROOT_PORT << 20;
797 if (port->index == 0) {
798 val |= LNKW_X1 << 12;
799 utlset1 = 0x20000000;
801 val |= LNKW_X4 << 12;
802 utlset1 = 0x20101101;
805 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
806 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, utlset1);
807 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01210000);
809 switch (port->index) {
811 mtdcri(SDR0, PESDR0_460EX_L0CDRCTL, 0x00003230);
812 mtdcri(SDR0, PESDR0_460EX_L0DRV, 0x00000130);
813 mtdcri(SDR0, PESDR0_460EX_L0CLK, 0x00000006);
815 mtdcri(SDR0, PESDR0_460EX_PHY_CTL_RST,0x10000000);
819 mtdcri(SDR0, PESDR1_460EX_L0CDRCTL, 0x00003230);
820 mtdcri(SDR0, PESDR1_460EX_L1CDRCTL, 0x00003230);
821 mtdcri(SDR0, PESDR1_460EX_L2CDRCTL, 0x00003230);
822 mtdcri(SDR0, PESDR1_460EX_L3CDRCTL, 0x00003230);
823 mtdcri(SDR0, PESDR1_460EX_L0DRV, 0x00000130);
824 mtdcri(SDR0, PESDR1_460EX_L1DRV, 0x00000130);
825 mtdcri(SDR0, PESDR1_460EX_L2DRV, 0x00000130);
826 mtdcri(SDR0, PESDR1_460EX_L3DRV, 0x00000130);
827 mtdcri(SDR0, PESDR1_460EX_L0CLK, 0x00000006);
828 mtdcri(SDR0, PESDR1_460EX_L1CLK, 0x00000006);
829 mtdcri(SDR0, PESDR1_460EX_L2CLK, 0x00000006);
830 mtdcri(SDR0, PESDR1_460EX_L3CLK, 0x00000006);
832 mtdcri(SDR0, PESDR1_460EX_PHY_CTL_RST,0x10000000);
836 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
837 mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) |
838 (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTPYN));
840 /* Poll for PHY reset */
841 /* XXX FIXME add timeout */
842 switch (port->index) {
844 while (!(mfdcri(SDR0, PESDR0_460EX_RSTSTA) & 0x1))
848 while (!(mfdcri(SDR0, PESDR1_460EX_RSTSTA) & 0x1))
853 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
854 (mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) &
855 ~(PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL)) |
856 PESDRx_RCSSET_RSTPYN);
863 static int ppc460ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
865 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
868 * Set buffer allocations and then assert VRB and TXE.
870 out_be32(port->utl_base + PEUTL_PBCTL, 0x0800000c);
871 out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
872 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
873 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
874 out_be32(port->utl_base + PEUTL_PBBSZ, 0x00000000);
875 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
876 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
877 out_be32(port->utl_base + PEUTL_RCIRQEN,0x00f00000);
878 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
883 static struct ppc4xx_pciex_hwops ppc460ex_pcie_hwops __initdata =
885 .core_init = ppc460ex_pciex_core_init,
886 .port_init_hw = ppc460ex_pciex_init_port_hw,
887 .setup_utl = ppc460ex_pciex_init_utl,
890 #endif /* CONFIG_44x */
894 static int __init ppc405ex_pciex_core_init(struct device_node *np)
896 /* Nothing to do, return 2 ports */
900 static void ppc405ex_pcie_phy_reset(struct ppc4xx_pciex_port *port)
902 /* Assert the PE0_PHY reset */
903 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01010000);
906 /* deassert the PE0_hotreset */
908 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01111000);
910 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01101000);
912 /* poll for phy !reset */
913 /* XXX FIXME add timeout */
914 while (!(mfdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSTA) & 0x00001000))
917 /* deassert the PE0_gpl_utl_reset */
918 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x00101000);
921 static int ppc405ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
926 val = PTYPE_LEGACY_ENDPOINT;
928 val = PTYPE_ROOT_PORT;
930 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET,
931 1 << 24 | val << 20 | LNKW_X1 << 12);
933 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x00000000);
934 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01010000);
935 mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET1, 0x720F0000);
936 mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET2, 0x70600003);
939 * Only reset the PHY when no link is currently established.
940 * This is for the Atheros PCIe board which has problems to establish
941 * the link (again) after this PHY reset. All other currently tested
942 * PCIe boards don't show this problem.
943 * This has to be re-tested and fixed in a later release!
945 val = mfdcri(SDR0, port->sdr_base + PESDRn_LOOP);
946 if (!(val & 0x00001000))
947 ppc405ex_pcie_phy_reset(port);
949 dcr_write(port->dcrs, DCRO_PEGPL_CFG, 0x10000000); /* guarded on */
956 static int ppc405ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
958 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
961 * Set buffer allocations and then assert VRB and TXE.
963 out_be32(port->utl_base + PEUTL_OUTTR, 0x02000000);
964 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
965 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
966 out_be32(port->utl_base + PEUTL_PBBSZ, 0x21000000);
967 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
968 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
969 out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
970 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
972 out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
977 static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata =
979 .core_init = ppc405ex_pciex_core_init,
980 .port_init_hw = ppc405ex_pciex_init_port_hw,
981 .setup_utl = ppc405ex_pciex_init_utl,
984 #endif /* CONFIG_40x */
987 /* Check that the core has been initied and if not, do it */
988 static int __init ppc4xx_pciex_check_core_init(struct device_node *np)
990 static int core_init;
997 if (of_device_is_compatible(np, "ibm,plb-pciex-440spe")) {
998 if (ppc440spe_revA())
999 ppc4xx_pciex_hwops = &ppc440speA_pcie_hwops;
1001 ppc4xx_pciex_hwops = &ppc440speB_pcie_hwops;
1003 if (of_device_is_compatible(np, "ibm,plb-pciex-460ex"))
1004 ppc4xx_pciex_hwops = &ppc460ex_pcie_hwops;
1005 #endif /* CONFIG_44x */
1007 if (of_device_is_compatible(np, "ibm,plb-pciex-405ex"))
1008 ppc4xx_pciex_hwops = &ppc405ex_pcie_hwops;
1010 if (ppc4xx_pciex_hwops == NULL) {
1011 printk(KERN_WARNING "PCIE: unknown host type %s\n",
1016 count = ppc4xx_pciex_hwops->core_init(np);
1018 ppc4xx_pciex_ports =
1019 kzalloc(count * sizeof(struct ppc4xx_pciex_port),
1021 if (ppc4xx_pciex_ports) {
1022 ppc4xx_pciex_port_count = count;
1025 printk(KERN_WARNING "PCIE: failed to allocate ports array\n");
1031 static void __init ppc4xx_pciex_port_init_mapping(struct ppc4xx_pciex_port *port)
1033 /* We map PCI Express configuration based on the reg property */
1034 dcr_write(port->dcrs, DCRO_PEGPL_CFGBAH,
1035 RES_TO_U32_HIGH(port->cfg_space.start));
1036 dcr_write(port->dcrs, DCRO_PEGPL_CFGBAL,
1037 RES_TO_U32_LOW(port->cfg_space.start));
1039 /* XXX FIXME: Use size from reg property. For now, map 512M */
1040 dcr_write(port->dcrs, DCRO_PEGPL_CFGMSK, 0xe0000001);
1042 /* We map UTL registers based on the reg property */
1043 dcr_write(port->dcrs, DCRO_PEGPL_REGBAH,
1044 RES_TO_U32_HIGH(port->utl_regs.start));
1045 dcr_write(port->dcrs, DCRO_PEGPL_REGBAL,
1046 RES_TO_U32_LOW(port->utl_regs.start));
1048 /* XXX FIXME: Use size from reg property */
1049 dcr_write(port->dcrs, DCRO_PEGPL_REGMSK, 0x00007001);
1051 /* Disable all other outbound windows */
1052 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, 0);
1053 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, 0);
1054 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0);
1055 dcr_write(port->dcrs, DCRO_PEGPL_MSGMSK, 0);
1058 static int __init ppc4xx_pciex_wait_on_sdr(struct ppc4xx_pciex_port *port,
1059 unsigned int sdr_offset,
1066 while(timeout_ms--) {
1067 val = mfdcri(SDR0, port->sdr_base + sdr_offset);
1068 if ((val & mask) == value) {
1069 pr_debug("PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
1070 port->index, sdr_offset, timeout_ms, val);
1078 static int __init ppc4xx_pciex_port_init(struct ppc4xx_pciex_port *port)
1083 if (ppc4xx_pciex_hwops->port_init_hw)
1084 rc = ppc4xx_pciex_hwops->port_init_hw(port);
1088 printk(KERN_INFO "PCIE%d: Checking link...\n",
1091 /* Wait for reset to complete */
1092 if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS, 1 << 20, 0, 10)) {
1093 printk(KERN_WARNING "PCIE%d: PGRST failed\n",
1098 /* Check for card presence detect if supported, if not, just wait for
1099 * link unconditionally.
1101 * note that we don't fail if there is no link, we just filter out
1102 * config space accesses. That way, it will be easier to implement
1105 if (!port->has_ibpre ||
1106 !ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
1107 1 << 28, 1 << 28, 100)) {
1109 "PCIE%d: Device detected, waiting for link...\n",
1111 if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
1112 0x1000, 0x1000, 2000))
1114 "PCIE%d: Link up failed\n", port->index);
1117 "PCIE%d: link is up !\n", port->index);
1121 printk(KERN_INFO "PCIE%d: No device detected.\n", port->index);
1124 * Initialize mapping: disable all regions and configure
1125 * CFG and REG regions based on resources in the device tree
1127 ppc4xx_pciex_port_init_mapping(port);
1132 port->utl_base = ioremap(port->utl_regs.start, 0x100);
1133 BUG_ON(port->utl_base == NULL);
1136 * Setup UTL registers --BenH.
1138 if (ppc4xx_pciex_hwops->setup_utl)
1139 ppc4xx_pciex_hwops->setup_utl(port);
1142 * Check for VC0 active and assert RDY.
1145 ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS,
1146 1 << 16, 1 << 16, 5000)) {
1147 printk(KERN_INFO "PCIE%d: VC0 not active\n", port->index);
1151 dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET, 0, 1 << 20);
1157 static int ppc4xx_pciex_validate_bdf(struct ppc4xx_pciex_port *port,
1158 struct pci_bus *bus,
1163 /* Endpoint can not generate upstream(remote) config cycles */
1164 if (port->endpoint && bus->number != port->hose->first_busno)
1165 return PCIBIOS_DEVICE_NOT_FOUND;
1167 /* Check we are within the mapped range */
1168 if (bus->number > port->hose->last_busno) {
1170 printk(KERN_WARNING "Warning! Probing bus %u"
1171 " out of range !\n", bus->number);
1174 return PCIBIOS_DEVICE_NOT_FOUND;
1177 /* The root complex has only one device / function */
1178 if (bus->number == port->hose->first_busno && devfn != 0)
1179 return PCIBIOS_DEVICE_NOT_FOUND;
1181 /* The other side of the RC has only one device as well */
1182 if (bus->number == (port->hose->first_busno + 1) &&
1183 PCI_SLOT(devfn) != 0)
1184 return PCIBIOS_DEVICE_NOT_FOUND;
1186 /* Check if we have a link */
1187 if ((bus->number != port->hose->first_busno) && !port->link)
1188 return PCIBIOS_DEVICE_NOT_FOUND;
1193 static void __iomem *ppc4xx_pciex_get_config_base(struct ppc4xx_pciex_port *port,
1194 struct pci_bus *bus,
1199 /* Remove the casts when we finally remove the stupid volatile
1200 * in struct pci_controller
1202 if (bus->number == port->hose->first_busno)
1203 return (void __iomem *)port->hose->cfg_addr;
1205 relbus = bus->number - (port->hose->first_busno + 1);
1206 return (void __iomem *)port->hose->cfg_data +
1207 ((relbus << 20) | (devfn << 12));
1210 static int ppc4xx_pciex_read_config(struct pci_bus *bus, unsigned int devfn,
1211 int offset, int len, u32 *val)
1213 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1214 struct ppc4xx_pciex_port *port =
1215 &ppc4xx_pciex_ports[hose->indirect_type];
1219 BUG_ON(hose != port->hose);
1221 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1222 return PCIBIOS_DEVICE_NOT_FOUND;
1224 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1227 * Reading from configuration space of non-existing device can
1228 * generate transaction errors. For the read duration we suppress
1229 * assertion of machine check exceptions to avoid those.
1231 gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1232 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1234 /* Make sure no CRS is recorded */
1235 out_be32(port->utl_base + PEUTL_RCSTA, 0x00040000);
1239 *val = in_8((u8 *)(addr + offset));
1242 *val = in_le16((u16 *)(addr + offset));
1245 *val = in_le32((u32 *)(addr + offset));
1249 pr_debug("pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
1250 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1251 bus->number, hose->first_busno, hose->last_busno,
1252 devfn, offset, len, addr + offset, *val);
1254 /* Check for CRS (440SPe rev B does that for us but heh ..) */
1255 if (in_be32(port->utl_base + PEUTL_RCSTA) & 0x00040000) {
1256 pr_debug("Got CRS !\n");
1257 if (len != 4 || offset != 0)
1258 return PCIBIOS_DEVICE_NOT_FOUND;
1262 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1264 return PCIBIOS_SUCCESSFUL;
1267 static int ppc4xx_pciex_write_config(struct pci_bus *bus, unsigned int devfn,
1268 int offset, int len, u32 val)
1270 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1271 struct ppc4xx_pciex_port *port =
1272 &ppc4xx_pciex_ports[hose->indirect_type];
1276 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1277 return PCIBIOS_DEVICE_NOT_FOUND;
1279 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1282 * Reading from configuration space of non-existing device can
1283 * generate transaction errors. For the read duration we suppress
1284 * assertion of machine check exceptions to avoid those.
1286 gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1287 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1289 pr_debug("pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
1290 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1291 bus->number, hose->first_busno, hose->last_busno,
1292 devfn, offset, len, addr + offset, val);
1296 out_8((u8 *)(addr + offset), val);
1299 out_le16((u16 *)(addr + offset), val);
1302 out_le32((u32 *)(addr + offset), val);
1306 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1308 return PCIBIOS_SUCCESSFUL;
1311 static struct pci_ops ppc4xx_pciex_pci_ops =
1313 .read = ppc4xx_pciex_read_config,
1314 .write = ppc4xx_pciex_write_config,
1317 static void __init ppc4xx_configure_pciex_POMs(struct ppc4xx_pciex_port *port,
1318 struct pci_controller *hose,
1319 void __iomem *mbase)
1321 u32 lah, lal, pciah, pcial, sa;
1324 /* Setup outbound memory windows */
1325 for (i = j = 0; i < 3; i++) {
1326 struct resource *res = &hose->mem_resources[i];
1328 /* we only care about memory windows */
1329 if (!(res->flags & IORESOURCE_MEM))
1332 printk(KERN_WARNING "%s: Too many ranges\n",
1333 port->node->full_name);
1337 /* Calculate register values */
1338 lah = RES_TO_U32_HIGH(res->start);
1339 lal = RES_TO_U32_LOW(res->start);
1340 pciah = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
1341 pcial = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
1342 sa = res->end + 1 - res->start;
1343 if (!is_power_of_2(sa) || sa < 0x100000 ||
1345 printk(KERN_WARNING "%s: Resource out of range\n",
1346 port->node->full_name);
1349 sa = (0xffffffffu << ilog2(sa)) | 0x1;
1351 /* Program register values */
1354 out_le32(mbase + PECFG_POM0LAH, pciah);
1355 out_le32(mbase + PECFG_POM0LAL, pcial);
1356 dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAH, lah);
1357 dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAL, lal);
1358 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKH, 0x7fffffff);
1359 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, sa | 3);
1362 out_le32(mbase + PECFG_POM1LAH, pciah);
1363 out_le32(mbase + PECFG_POM1LAL, pcial);
1364 dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAH, lah);
1365 dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAL, lal);
1366 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKH, 0x7fffffff);
1367 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, sa | 3);
1373 /* Configure IO, always 64K starting at 0 */
1374 if (hose->io_resource.flags & IORESOURCE_IO) {
1375 lah = RES_TO_U32_HIGH(hose->io_base_phys);
1376 lal = RES_TO_U32_LOW(hose->io_base_phys);
1377 out_le32(mbase + PECFG_POM2LAH, 0);
1378 out_le32(mbase + PECFG_POM2LAL, 0);
1379 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAH, lah);
1380 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAL, lal);
1381 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKH, 0x7fffffff);
1382 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0xffff0000 | 3);
1386 static void __init ppc4xx_configure_pciex_PIMs(struct ppc4xx_pciex_port *port,
1387 struct pci_controller *hose,
1388 void __iomem *mbase,
1389 struct resource *res)
1391 resource_size_t size = res->end - res->start + 1;
1394 if (port->endpoint) {
1395 resource_size_t ep_addr = 0;
1396 resource_size_t ep_size = 32 << 20;
1398 /* Currently we map a fixed 64MByte window to PLB address
1399 * 0 (SDRAM). This should probably be configurable via a dts
1403 /* Calculate window size */
1404 sa = (0xffffffffffffffffull << ilog2(ep_size));;
1407 out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
1408 out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa) |
1409 PCI_BASE_ADDRESS_MEM_TYPE_64);
1411 /* Disable BAR1 & BAR2 */
1412 out_le32(mbase + PECFG_BAR1MPA, 0);
1413 out_le32(mbase + PECFG_BAR2HMPA, 0);
1414 out_le32(mbase + PECFG_BAR2LMPA, 0);
1416 out_le32(mbase + PECFG_PIM01SAH, RES_TO_U32_HIGH(sa));
1417 out_le32(mbase + PECFG_PIM01SAL, RES_TO_U32_LOW(sa));
1419 out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(ep_addr));
1420 out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(ep_addr));
1422 /* Calculate window size */
1423 sa = (0xffffffffffffffffull << ilog2(size));;
1424 if (res->flags & IORESOURCE_PREFETCH)
1427 out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
1428 out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa));
1430 /* The setup of the split looks weird to me ... let's see
1433 out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
1434 out_le32(mbase + PECFG_PIM0LAH, 0x00000000);
1435 out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
1436 out_le32(mbase + PECFG_PIM1LAH, 0x00000000);
1437 out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
1438 out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
1440 out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(res->start));
1441 out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(res->start));
1444 /* Enable inbound mapping */
1445 out_le32(mbase + PECFG_PIMEN, 0x1);
1447 /* Enable I/O, Mem, and Busmaster cycles */
1448 out_le16(mbase + PCI_COMMAND,
1449 in_le16(mbase + PCI_COMMAND) |
1450 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1453 static void __init ppc4xx_pciex_port_setup_hose(struct ppc4xx_pciex_port *port)
1455 struct resource dma_window;
1456 struct pci_controller *hose = NULL;
1457 const int *bus_range;
1458 int primary = 0, busses;
1459 void __iomem *mbase = NULL, *cfg_data = NULL;
1463 /* Check if primary bridge */
1464 if (of_get_property(port->node, "primary", NULL))
1467 /* Get bus range if any */
1468 bus_range = of_get_property(port->node, "bus-range", NULL);
1470 /* Allocate the host controller data structure */
1471 hose = pcibios_alloc_controller(port->node);
1475 /* We stick the port number in "indirect_type" so the config space
1476 * ops can retrieve the port data structure easily
1478 hose->indirect_type = port->index;
1481 hose->first_busno = bus_range ? bus_range[0] : 0x0;
1482 hose->last_busno = bus_range ? bus_range[1] : 0xff;
1484 /* Because of how big mapping the config space is (1M per bus), we
1485 * limit how many busses we support. In the long run, we could replace
1486 * that with something akin to kmap_atomic instead. We set aside 1 bus
1487 * for the host itself too.
1489 busses = hose->last_busno - hose->first_busno; /* This is off by 1 */
1490 if (busses > MAX_PCIE_BUS_MAPPED) {
1491 busses = MAX_PCIE_BUS_MAPPED;
1492 hose->last_busno = hose->first_busno + busses;
1495 if (!port->endpoint) {
1496 /* Only map the external config space in cfg_data for
1497 * PCIe root-complexes. External space is 1M per bus
1499 cfg_data = ioremap(port->cfg_space.start +
1500 (hose->first_busno + 1) * 0x100000,
1502 if (cfg_data == NULL) {
1503 printk(KERN_ERR "%s: Can't map external config space !",
1504 port->node->full_name);
1507 hose->cfg_data = cfg_data;
1510 /* Always map the host config space in cfg_addr.
1511 * Internal space is 4K
1513 mbase = ioremap(port->cfg_space.start + 0x10000000, 0x1000);
1514 if (mbase == NULL) {
1515 printk(KERN_ERR "%s: Can't map internal config space !",
1516 port->node->full_name);
1519 hose->cfg_addr = mbase;
1521 pr_debug("PCIE %s, bus %d..%d\n", port->node->full_name,
1522 hose->first_busno, hose->last_busno);
1523 pr_debug(" config space mapped at: root @0x%p, other @0x%p\n",
1524 hose->cfg_addr, hose->cfg_data);
1526 /* Setup config space */
1527 hose->ops = &ppc4xx_pciex_pci_ops;
1529 mbase = (void __iomem *)hose->cfg_addr;
1531 if (!port->endpoint) {
1533 * Set bus numbers on our root port
1535 out_8(mbase + PCI_PRIMARY_BUS, hose->first_busno);
1536 out_8(mbase + PCI_SECONDARY_BUS, hose->first_busno + 1);
1537 out_8(mbase + PCI_SUBORDINATE_BUS, hose->last_busno);
1541 * OMRs are already reset, also disable PIMs
1543 out_le32(mbase + PECFG_PIMEN, 0);
1545 /* Parse outbound mapping resources */
1546 pci_process_bridge_OF_ranges(hose, port->node, primary);
1548 /* Parse inbound mapping resources */
1549 if (ppc4xx_parse_dma_ranges(hose, mbase, &dma_window) != 0)
1552 /* Configure outbound ranges POMs */
1553 ppc4xx_configure_pciex_POMs(port, hose, mbase);
1555 /* Configure inbound ranges PIMs */
1556 ppc4xx_configure_pciex_PIMs(port, hose, mbase, &dma_window);
1558 /* The root complex doesn't show up if we don't set some vendor
1559 * and device IDs into it. The defaults below are the same bogus
1560 * one that the initial code in arch/ppc had. This can be
1561 * overwritten by setting the "vendor-id/device-id" properties
1562 * in the pciex node.
1565 /* Get the (optional) vendor-/device-id from the device-tree */
1566 pval = of_get_property(port->node, "vendor-id", NULL);
1570 if (!port->endpoint)
1571 val = 0xaaa0 + port->index;
1573 val = 0xeee0 + port->index;
1575 out_le16(mbase + 0x200, val);
1577 pval = of_get_property(port->node, "device-id", NULL);
1581 if (!port->endpoint)
1582 val = 0xbed0 + port->index;
1584 val = 0xfed0 + port->index;
1586 out_le16(mbase + 0x202, val);
1588 if (!port->endpoint) {
1589 /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
1590 out_le32(mbase + 0x208, 0x06040001);
1592 printk(KERN_INFO "PCIE%d: successfully set as root-complex\n",
1595 /* Set Class Code to Processor/PPC */
1596 out_le32(mbase + 0x208, 0x0b200001);
1598 printk(KERN_INFO "PCIE%d: successfully set as endpoint\n",
1605 pcibios_free_controller(hose);
1612 static void __init ppc4xx_probe_pciex_bridge(struct device_node *np)
1614 struct ppc4xx_pciex_port *port;
1620 /* First, proceed to core initialization as we assume there's
1621 * only one PCIe core in the system
1623 if (ppc4xx_pciex_check_core_init(np))
1626 /* Get the port number from the device-tree */
1627 pval = of_get_property(np, "port", NULL);
1629 printk(KERN_ERR "PCIE: Can't find port number for %s\n",
1634 if (portno >= ppc4xx_pciex_port_count) {
1635 printk(KERN_ERR "PCIE: port number out of range for %s\n",
1639 port = &ppc4xx_pciex_ports[portno];
1640 port->index = portno;
1643 * Check if device is enabled
1645 if (!of_device_is_available(np)) {
1646 printk(KERN_INFO "PCIE%d: Port disabled via device-tree\n", port->index);
1650 port->node = of_node_get(np);
1651 pval = of_get_property(np, "sdr-base", NULL);
1653 printk(KERN_ERR "PCIE: missing sdr-base for %s\n",
1657 port->sdr_base = *pval;
1659 /* Check if device_type property is set to "pci" or "pci-endpoint".
1660 * Resulting from this setup this PCIe port will be configured
1661 * as root-complex or as endpoint.
1663 val = of_get_property(port->node, "device_type", NULL);
1664 if (!strcmp(val, "pci-endpoint")) {
1666 } else if (!strcmp(val, "pci")) {
1669 printk(KERN_ERR "PCIE: missing or incorrect device_type for %s\n",
1674 /* Fetch config space registers address */
1675 if (of_address_to_resource(np, 0, &port->cfg_space)) {
1676 printk(KERN_ERR "%s: Can't get PCI-E config space !",
1680 /* Fetch host bridge internal registers address */
1681 if (of_address_to_resource(np, 1, &port->utl_regs)) {
1682 printk(KERN_ERR "%s: Can't get UTL register base !",
1688 dcrs = dcr_resource_start(np, 0);
1690 printk(KERN_ERR "%s: Can't get DCR register base !",
1694 port->dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
1696 /* Initialize the port specific registers */
1697 if (ppc4xx_pciex_port_init(port)) {
1698 printk(KERN_WARNING "PCIE%d: Port init failed\n", port->index);
1702 /* Setup the linux hose data structure */
1703 ppc4xx_pciex_port_setup_hose(port);
1706 #endif /* CONFIG_PPC4xx_PCI_EXPRESS */
1708 static int __init ppc4xx_pci_find_bridges(void)
1710 struct device_node *np;
1712 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
1713 for_each_compatible_node(np, NULL, "ibm,plb-pciex")
1714 ppc4xx_probe_pciex_bridge(np);
1716 for_each_compatible_node(np, NULL, "ibm,plb-pcix")
1717 ppc4xx_probe_pcix_bridge(np);
1718 for_each_compatible_node(np, NULL, "ibm,plb-pci")
1719 ppc4xx_probe_pci_bridge(np);
1723 arch_initcall(ppc4xx_pci_find_bridges);