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>
34 #include "ppc4xx_pci.h"
36 static int dma_offset_set;
38 /* Move that to a useable header */
39 extern unsigned long total_memory;
41 #define U64_TO_U32_LOW(val) ((u32)((val) & 0x00000000ffffffffULL))
42 #define U64_TO_U32_HIGH(val) ((u32)((val) >> 32))
44 #define RES_TO_U32_LOW(val) \
45 ((sizeof(resource_size_t) > sizeof(u32)) ? U64_TO_U32_LOW(val) : (val))
46 #define RES_TO_U32_HIGH(val) \
47 ((sizeof(resource_size_t) > sizeof(u32)) ? U64_TO_U32_HIGH(val) : (0))
49 static inline int ppc440spe_revA(void)
51 /* Catch both 440SPe variants, with and without RAID6 support */
52 if ((mfspr(SPRN_PVR) & 0xffefffff) == 0x53421890)
58 static void fixup_ppc4xx_pci_bridge(struct pci_dev *dev)
60 struct pci_controller *hose;
63 if (dev->devfn != 0 || dev->bus->self != NULL)
66 hose = pci_bus_to_host(dev->bus);
70 if (!of_device_is_compatible(hose->dn, "ibm,plb-pciex") &&
71 !of_device_is_compatible(hose->dn, "ibm,plb-pcix") &&
72 !of_device_is_compatible(hose->dn, "ibm,plb-pci"))
75 if (of_device_is_compatible(hose->dn, "ibm,plb440epx-pci") ||
76 of_device_is_compatible(hose->dn, "ibm,plb440grx-pci")) {
77 hose->indirect_type |= PPC_INDIRECT_TYPE_BROKEN_MRM;
80 /* Hide the PCI host BARs from the kernel as their content doesn't
81 * fit well in the resource management
83 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
84 dev->resource[i].start = dev->resource[i].end = 0;
85 dev->resource[i].flags = 0;
88 printk(KERN_INFO "PCI: Hiding 4xx host bridge resources %s\n",
91 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, fixup_ppc4xx_pci_bridge);
93 static int __init ppc4xx_parse_dma_ranges(struct pci_controller *hose,
100 int pna = of_n_addr_cells(hose->dn);
105 res->end = size = 0x80000000;
106 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
108 /* Get dma-ranges property */
109 ranges = of_get_property(hose->dn, "dma-ranges", &rlen);
114 while ((rlen -= np * 4) >= 0) {
115 u32 pci_space = ranges[0];
116 u64 pci_addr = of_read_number(ranges + 1, 2);
117 u64 cpu_addr = of_translate_dma_address(hose->dn, ranges + 3);
118 size = of_read_number(ranges + pna + 3, 2);
120 if (cpu_addr == OF_BAD_ADDR || size == 0)
123 /* We only care about memory */
124 if ((pci_space & 0x03000000) != 0x02000000)
127 /* We currently only support memory at 0, and pci_addr
128 * within 32 bits space
130 if (cpu_addr != 0 || pci_addr > 0xffffffff) {
131 printk(KERN_WARNING "%s: Ignored unsupported dma range"
132 " 0x%016llx...0x%016llx -> 0x%016llx\n",
134 pci_addr, pci_addr + size - 1, cpu_addr);
138 /* Check if not prefetchable */
139 if (!(pci_space & 0x40000000))
140 res->flags &= ~IORESOURCE_PREFETCH;
144 res->start = pci_addr;
145 /* Beware of 32 bits resources */
146 if (sizeof(resource_size_t) == sizeof(u32) &&
147 (pci_addr + size) > 0x100000000ull)
148 res->end = 0xffffffff;
150 res->end = res->start + size - 1;
154 /* We only support one global DMA offset */
155 if (dma_offset_set && pci_dram_offset != res->start) {
156 printk(KERN_ERR "%s: dma-ranges(s) mismatch\n",
157 hose->dn->full_name);
161 /* Check that we can fit all of memory as we don't support
164 if (size < total_memory) {
165 printk(KERN_ERR "%s: dma-ranges too small "
166 "(size=%llx total_memory=%lx)\n",
167 hose->dn->full_name, size, total_memory);
171 /* Check we are a power of 2 size and that base is a multiple of size*/
172 if (!is_power_of_2(size) ||
173 (res->start & (size - 1)) != 0) {
174 printk(KERN_ERR "%s: dma-ranges unaligned\n",
175 hose->dn->full_name);
179 /* Check that we are fully contained within 32 bits space */
180 if (res->end > 0xffffffff) {
181 printk(KERN_ERR "%s: dma-ranges outside of 32 bits space\n",
182 hose->dn->full_name);
187 pci_dram_offset = res->start;
189 printk(KERN_INFO "4xx PCI DMA offset set to 0x%08lx\n",
198 static void __init ppc4xx_configure_pci_PMMs(struct pci_controller *hose,
201 u32 la, ma, pcila, pciha;
204 /* Setup outbound memory windows */
205 for (i = j = 0; i < 3; i++) {
206 struct resource *res = &hose->mem_resources[i];
208 /* we only care about memory windows */
209 if (!(res->flags & IORESOURCE_MEM))
212 printk(KERN_WARNING "%s: Too many ranges\n",
213 hose->dn->full_name);
217 /* Calculate register values */
219 pciha = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
220 pcila = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
222 ma = res->end + 1 - res->start;
223 if (!is_power_of_2(ma) || ma < 0x1000 || ma > 0xffffffffu) {
224 printk(KERN_WARNING "%s: Resource out of range\n",
225 hose->dn->full_name);
228 ma = (0xffffffffu << ilog2(ma)) | 0x1;
229 if (res->flags & IORESOURCE_PREFETCH)
232 /* Program register values */
233 writel(la, reg + PCIL0_PMM0LA + (0x10 * j));
234 writel(pcila, reg + PCIL0_PMM0PCILA + (0x10 * j));
235 writel(pciha, reg + PCIL0_PMM0PCIHA + (0x10 * j));
236 writel(ma, reg + PCIL0_PMM0MA + (0x10 * j));
241 static void __init ppc4xx_configure_pci_PTMs(struct pci_controller *hose,
243 const struct resource *res)
245 resource_size_t size = res->end - res->start + 1;
248 /* Calculate window size */
249 sa = (0xffffffffu << ilog2(size)) | 1;
252 /* RAM is always at 0 local for now */
253 writel(0, reg + PCIL0_PTM1LA);
254 writel(sa, reg + PCIL0_PTM1MS);
256 /* Map on PCI side */
257 early_write_config_dword(hose, hose->first_busno, 0,
258 PCI_BASE_ADDRESS_1, res->start);
259 early_write_config_dword(hose, hose->first_busno, 0,
260 PCI_BASE_ADDRESS_2, 0x00000000);
261 early_write_config_word(hose, hose->first_busno, 0,
262 PCI_COMMAND, 0x0006);
265 static void __init ppc4xx_probe_pci_bridge(struct device_node *np)
268 struct resource rsrc_cfg;
269 struct resource rsrc_reg;
270 struct resource dma_window;
271 struct pci_controller *hose = NULL;
272 void __iomem *reg = NULL;
273 const int *bus_range;
276 /* Fetch config space registers address */
277 if (of_address_to_resource(np, 0, &rsrc_cfg)) {
278 printk(KERN_ERR "%s:Can't get PCI config register base !",
282 /* Fetch host bridge internal registers address */
283 if (of_address_to_resource(np, 3, &rsrc_reg)) {
284 printk(KERN_ERR "%s: Can't get PCI internal register base !",
289 /* Check if primary bridge */
290 if (of_get_property(np, "primary", NULL))
293 /* Get bus range if any */
294 bus_range = of_get_property(np, "bus-range", NULL);
297 reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
299 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
303 /* Allocate the host controller data structure */
304 hose = pcibios_alloc_controller(np);
308 hose->first_busno = bus_range ? bus_range[0] : 0x0;
309 hose->last_busno = bus_range ? bus_range[1] : 0xff;
311 /* Setup config space */
312 setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
314 /* Disable all windows */
315 writel(0, reg + PCIL0_PMM0MA);
316 writel(0, reg + PCIL0_PMM1MA);
317 writel(0, reg + PCIL0_PMM2MA);
318 writel(0, reg + PCIL0_PTM1MS);
319 writel(0, reg + PCIL0_PTM2MS);
321 /* Parse outbound mapping resources */
322 pci_process_bridge_OF_ranges(hose, np, primary);
324 /* Parse inbound mapping resources */
325 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
328 /* Configure outbound ranges POMs */
329 ppc4xx_configure_pci_PMMs(hose, reg);
331 /* Configure inbound ranges PIMs */
332 ppc4xx_configure_pci_PTMs(hose, reg, &dma_window);
334 /* We don't need the registers anymore */
340 pcibios_free_controller(hose);
349 static void __init ppc4xx_configure_pcix_POMs(struct pci_controller *hose,
352 u32 lah, lal, pciah, pcial, sa;
355 /* Setup outbound memory windows */
356 for (i = j = 0; i < 3; i++) {
357 struct resource *res = &hose->mem_resources[i];
359 /* we only care about memory windows */
360 if (!(res->flags & IORESOURCE_MEM))
363 printk(KERN_WARNING "%s: Too many ranges\n",
364 hose->dn->full_name);
368 /* Calculate register values */
369 lah = RES_TO_U32_HIGH(res->start);
370 lal = RES_TO_U32_LOW(res->start);
371 pciah = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
372 pcial = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
373 sa = res->end + 1 - res->start;
374 if (!is_power_of_2(sa) || sa < 0x100000 ||
376 printk(KERN_WARNING "%s: Resource out of range\n",
377 hose->dn->full_name);
380 sa = (0xffffffffu << ilog2(sa)) | 0x1;
382 /* Program register values */
384 writel(lah, reg + PCIX0_POM0LAH);
385 writel(lal, reg + PCIX0_POM0LAL);
386 writel(pciah, reg + PCIX0_POM0PCIAH);
387 writel(pcial, reg + PCIX0_POM0PCIAL);
388 writel(sa, reg + PCIX0_POM0SA);
390 writel(lah, reg + PCIX0_POM1LAH);
391 writel(lal, reg + PCIX0_POM1LAL);
392 writel(pciah, reg + PCIX0_POM1PCIAH);
393 writel(pcial, reg + PCIX0_POM1PCIAL);
394 writel(sa, reg + PCIX0_POM1SA);
400 static void __init ppc4xx_configure_pcix_PIMs(struct pci_controller *hose,
402 const struct resource *res,
406 resource_size_t size = res->end - res->start + 1;
409 /* RAM is always at 0 */
410 writel(0x00000000, reg + PCIX0_PIM0LAH);
411 writel(0x00000000, reg + PCIX0_PIM0LAL);
413 /* Calculate window size */
414 sa = (0xffffffffu << ilog2(size)) | 1;
416 if (res->flags & IORESOURCE_PREFETCH)
420 writel(sa, reg + PCIX0_PIM0SA);
422 writel(0xffffffff, reg + PCIX0_PIM0SAH);
424 /* Map on PCI side */
425 writel(0x00000000, reg + PCIX0_BAR0H);
426 writel(res->start, reg + PCIX0_BAR0L);
427 writew(0x0006, reg + PCIX0_COMMAND);
430 static void __init ppc4xx_probe_pcix_bridge(struct device_node *np)
432 struct resource rsrc_cfg;
433 struct resource rsrc_reg;
434 struct resource dma_window;
435 struct pci_controller *hose = NULL;
436 void __iomem *reg = NULL;
437 const int *bus_range;
438 int big_pim = 0, msi = 0, primary = 0;
440 /* Fetch config space registers address */
441 if (of_address_to_resource(np, 0, &rsrc_cfg)) {
442 printk(KERN_ERR "%s:Can't get PCI-X config register base !",
446 /* Fetch host bridge internal registers address */
447 if (of_address_to_resource(np, 3, &rsrc_reg)) {
448 printk(KERN_ERR "%s: Can't get PCI-X internal register base !",
453 /* Check if it supports large PIMs (440GX) */
454 if (of_get_property(np, "large-inbound-windows", NULL))
457 /* Check if we should enable MSIs inbound hole */
458 if (of_get_property(np, "enable-msi-hole", NULL))
461 /* Check if primary bridge */
462 if (of_get_property(np, "primary", NULL))
465 /* Get bus range if any */
466 bus_range = of_get_property(np, "bus-range", NULL);
469 reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
471 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
475 /* Allocate the host controller data structure */
476 hose = pcibios_alloc_controller(np);
480 hose->first_busno = bus_range ? bus_range[0] : 0x0;
481 hose->last_busno = bus_range ? bus_range[1] : 0xff;
483 /* Setup config space */
484 setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
486 /* Disable all windows */
487 writel(0, reg + PCIX0_POM0SA);
488 writel(0, reg + PCIX0_POM1SA);
489 writel(0, reg + PCIX0_POM2SA);
490 writel(0, reg + PCIX0_PIM0SA);
491 writel(0, reg + PCIX0_PIM1SA);
492 writel(0, reg + PCIX0_PIM2SA);
494 writel(0, reg + PCIX0_PIM0SAH);
495 writel(0, reg + PCIX0_PIM2SAH);
498 /* Parse outbound mapping resources */
499 pci_process_bridge_OF_ranges(hose, np, primary);
501 /* Parse inbound mapping resources */
502 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
505 /* Configure outbound ranges POMs */
506 ppc4xx_configure_pcix_POMs(hose, reg);
508 /* Configure inbound ranges PIMs */
509 ppc4xx_configure_pcix_PIMs(hose, reg, &dma_window, big_pim, msi);
511 /* We don't need the registers anymore */
517 pcibios_free_controller(hose);
522 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
525 * 4xx PCI-Express part
527 * We support 3 parts currently based on the compatible property:
529 * ibm,plb-pciex-440spe
530 * ibm,plb-pciex-405ex
531 * ibm,plb-pciex-460ex
533 * Anything else will be rejected for now as they are all subtly
534 * different unfortunately.
538 #define MAX_PCIE_BUS_MAPPED 0x40
540 struct ppc4xx_pciex_port
542 struct pci_controller *hose;
543 struct device_node *node;
548 unsigned int sdr_base;
550 struct resource cfg_space;
551 struct resource utl_regs;
552 void __iomem *utl_base;
555 static struct ppc4xx_pciex_port *ppc4xx_pciex_ports;
556 static unsigned int ppc4xx_pciex_port_count;
558 struct ppc4xx_pciex_hwops
560 int (*core_init)(struct device_node *np);
561 int (*port_init_hw)(struct ppc4xx_pciex_port *port);
562 int (*setup_utl)(struct ppc4xx_pciex_port *port);
565 static struct ppc4xx_pciex_hwops *ppc4xx_pciex_hwops;
569 /* Check various reset bits of the 440SPe PCIe core */
570 static int __init ppc440spe_pciex_check_reset(struct device_node *np)
572 u32 valPE0, valPE1, valPE2;
575 /* SDR0_PEGPLLLCT1 reset */
576 if (!(mfdcri(SDR0, PESDR0_PLLLCT1) & 0x01000000)) {
578 * the PCIe core was probably already initialised
579 * by firmware - let's re-reset RCSSET regs
581 * -- Shouldn't we also re-reset the whole thing ? -- BenH
583 pr_debug("PCIE: SDR0_PLLLCT1 already reset.\n");
584 mtdcri(SDR0, PESDR0_440SPE_RCSSET, 0x01010000);
585 mtdcri(SDR0, PESDR1_440SPE_RCSSET, 0x01010000);
586 mtdcri(SDR0, PESDR2_440SPE_RCSSET, 0x01010000);
589 valPE0 = mfdcri(SDR0, PESDR0_440SPE_RCSSET);
590 valPE1 = mfdcri(SDR0, PESDR1_440SPE_RCSSET);
591 valPE2 = mfdcri(SDR0, PESDR2_440SPE_RCSSET);
593 /* SDR0_PExRCSSET rstgu */
594 if (!(valPE0 & 0x01000000) ||
595 !(valPE1 & 0x01000000) ||
596 !(valPE2 & 0x01000000)) {
597 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstgu error\n");
601 /* SDR0_PExRCSSET rstdl */
602 if (!(valPE0 & 0x00010000) ||
603 !(valPE1 & 0x00010000) ||
604 !(valPE2 & 0x00010000)) {
605 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstdl error\n");
609 /* SDR0_PExRCSSET rstpyn */
610 if ((valPE0 & 0x00001000) ||
611 (valPE1 & 0x00001000) ||
612 (valPE2 & 0x00001000)) {
613 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstpyn error\n");
617 /* SDR0_PExRCSSET hldplb */
618 if ((valPE0 & 0x10000000) ||
619 (valPE1 & 0x10000000) ||
620 (valPE2 & 0x10000000)) {
621 printk(KERN_INFO "PCIE: SDR0_PExRCSSET hldplb error\n");
625 /* SDR0_PExRCSSET rdy */
626 if ((valPE0 & 0x00100000) ||
627 (valPE1 & 0x00100000) ||
628 (valPE2 & 0x00100000)) {
629 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rdy error\n");
633 /* SDR0_PExRCSSET shutdown */
634 if ((valPE0 & 0x00000100) ||
635 (valPE1 & 0x00000100) ||
636 (valPE2 & 0x00000100)) {
637 printk(KERN_INFO "PCIE: SDR0_PExRCSSET shutdown error\n");
644 /* Global PCIe core initializations for 440SPe core */
645 static int __init ppc440spe_pciex_core_init(struct device_node *np)
649 /* Set PLL clock receiver to LVPECL */
650 dcri_clrset(SDR0, PESDR0_PLLLCT1, 0, 1 << 28);
652 /* Shouldn't we do all the calibration stuff etc... here ? */
653 if (ppc440spe_pciex_check_reset(np))
656 if (!(mfdcri(SDR0, PESDR0_PLLLCT2) & 0x10000)) {
657 printk(KERN_INFO "PCIE: PESDR_PLLCT2 resistance calibration "
659 mfdcri(SDR0, PESDR0_PLLLCT2));
663 /* De-assert reset of PCIe PLL, wait for lock */
664 dcri_clrset(SDR0, PESDR0_PLLLCT1, 1 << 24, 0);
668 if (!(mfdcri(SDR0, PESDR0_PLLLCT3) & 0x10000000)) {
675 printk(KERN_INFO "PCIE: VCO output not locked\n");
679 pr_debug("PCIE initialization OK\n");
684 static int ppc440spe_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
689 val = PTYPE_LEGACY_ENDPOINT << 20;
691 val = PTYPE_ROOT_PORT << 20;
693 if (port->index == 0)
694 val |= LNKW_X8 << 12;
696 val |= LNKW_X4 << 12;
698 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
699 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x20222222);
700 if (ppc440spe_revA())
701 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x11000000);
702 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL0SET1, 0x35000000);
703 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL1SET1, 0x35000000);
704 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL2SET1, 0x35000000);
705 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL3SET1, 0x35000000);
706 if (port->index == 0) {
707 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL4SET1,
709 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL5SET1,
711 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL6SET1,
713 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL7SET1,
716 dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET,
717 (1 << 24) | (1 << 16), 1 << 12);
722 static int ppc440speA_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
724 return ppc440spe_pciex_init_port_hw(port);
727 static int ppc440speB_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
729 int rc = ppc440spe_pciex_init_port_hw(port);
736 static int ppc440speA_pciex_init_utl(struct ppc4xx_pciex_port *port)
738 /* XXX Check what that value means... I hate magic */
739 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x68782800);
742 * Set buffer allocations and then assert VRB and TXE.
744 out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
745 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
746 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x10000000);
747 out_be32(port->utl_base + PEUTL_PBBSZ, 0x53000000);
748 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x08000000);
749 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x10000000);
750 out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
751 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
756 static int ppc440speB_pciex_init_utl(struct ppc4xx_pciex_port *port)
758 /* Report CRS to the operating system */
759 out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
764 static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops __initdata =
766 .core_init = ppc440spe_pciex_core_init,
767 .port_init_hw = ppc440speA_pciex_init_port_hw,
768 .setup_utl = ppc440speA_pciex_init_utl,
771 static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata =
773 .core_init = ppc440spe_pciex_core_init,
774 .port_init_hw = ppc440speB_pciex_init_port_hw,
775 .setup_utl = ppc440speB_pciex_init_utl,
778 static int __init ppc460ex_pciex_core_init(struct device_node *np)
780 /* Nothing to do, return 2 ports */
784 static int ppc460ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
790 val = PTYPE_LEGACY_ENDPOINT << 20;
792 val = PTYPE_ROOT_PORT << 20;
794 if (port->index == 0) {
795 val |= LNKW_X1 << 12;
796 utlset1 = 0x20000000;
798 val |= LNKW_X4 << 12;
799 utlset1 = 0x20101101;
802 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
803 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, utlset1);
804 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01210000);
806 switch (port->index) {
808 mtdcri(SDR0, PESDR0_460EX_L0CDRCTL, 0x00003230);
809 mtdcri(SDR0, PESDR0_460EX_L0DRV, 0x00000136);
810 mtdcri(SDR0, PESDR0_460EX_L0CLK, 0x00000006);
812 mtdcri(SDR0, PESDR0_460EX_PHY_CTL_RST,0x10000000);
816 mtdcri(SDR0, PESDR1_460EX_L0CDRCTL, 0x00003230);
817 mtdcri(SDR0, PESDR1_460EX_L1CDRCTL, 0x00003230);
818 mtdcri(SDR0, PESDR1_460EX_L2CDRCTL, 0x00003230);
819 mtdcri(SDR0, PESDR1_460EX_L3CDRCTL, 0x00003230);
820 mtdcri(SDR0, PESDR1_460EX_L0DRV, 0x00000136);
821 mtdcri(SDR0, PESDR1_460EX_L1DRV, 0x00000136);
822 mtdcri(SDR0, PESDR1_460EX_L2DRV, 0x00000136);
823 mtdcri(SDR0, PESDR1_460EX_L3DRV, 0x00000136);
824 mtdcri(SDR0, PESDR1_460EX_L0CLK, 0x00000006);
825 mtdcri(SDR0, PESDR1_460EX_L1CLK, 0x00000006);
826 mtdcri(SDR0, PESDR1_460EX_L2CLK, 0x00000006);
827 mtdcri(SDR0, PESDR1_460EX_L3CLK, 0x00000006);
829 mtdcri(SDR0, PESDR1_460EX_PHY_CTL_RST,0x10000000);
833 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
834 mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) |
835 (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTPYN));
837 /* Poll for PHY reset */
838 /* XXX FIXME add timeout */
839 switch (port->index) {
841 while (!(mfdcri(SDR0, PESDR0_460EX_RSTSTA) & 0x1))
845 while (!(mfdcri(SDR0, PESDR1_460EX_RSTSTA) & 0x1))
850 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
851 (mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) &
852 ~(PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL)) |
853 PESDRx_RCSSET_RSTPYN);
860 static int ppc460ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
862 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
865 * Set buffer allocations and then assert VRB and TXE.
867 out_be32(port->utl_base + PEUTL_PBCTL, 0x0800000c);
868 out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
869 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
870 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
871 out_be32(port->utl_base + PEUTL_PBBSZ, 0x00000000);
872 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
873 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
874 out_be32(port->utl_base + PEUTL_RCIRQEN,0x00f00000);
875 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
880 static struct ppc4xx_pciex_hwops ppc460ex_pcie_hwops __initdata =
882 .core_init = ppc460ex_pciex_core_init,
883 .port_init_hw = ppc460ex_pciex_init_port_hw,
884 .setup_utl = ppc460ex_pciex_init_utl,
887 #endif /* CONFIG_44x */
891 static int __init ppc405ex_pciex_core_init(struct device_node *np)
893 /* Nothing to do, return 2 ports */
897 static void ppc405ex_pcie_phy_reset(struct ppc4xx_pciex_port *port)
899 /* Assert the PE0_PHY reset */
900 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01010000);
903 /* deassert the PE0_hotreset */
905 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01111000);
907 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01101000);
909 /* poll for phy !reset */
910 /* XXX FIXME add timeout */
911 while (!(mfdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSTA) & 0x00001000))
914 /* deassert the PE0_gpl_utl_reset */
915 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x00101000);
918 static int ppc405ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
923 val = PTYPE_LEGACY_ENDPOINT;
925 val = PTYPE_ROOT_PORT;
927 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET,
928 1 << 24 | val << 20 | LNKW_X1 << 12);
930 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x00000000);
931 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01010000);
932 mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET1, 0x720F0000);
933 mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET2, 0x70600003);
936 * Only reset the PHY when no link is currently established.
937 * This is for the Atheros PCIe board which has problems to establish
938 * the link (again) after this PHY reset. All other currently tested
939 * PCIe boards don't show this problem.
940 * This has to be re-tested and fixed in a later release!
942 val = mfdcri(SDR0, port->sdr_base + PESDRn_LOOP);
943 if (!(val & 0x00001000))
944 ppc405ex_pcie_phy_reset(port);
946 dcr_write(port->dcrs, DCRO_PEGPL_CFG, 0x10000000); /* guarded on */
953 static int ppc405ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
955 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
958 * Set buffer allocations and then assert VRB and TXE.
960 out_be32(port->utl_base + PEUTL_OUTTR, 0x02000000);
961 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
962 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
963 out_be32(port->utl_base + PEUTL_PBBSZ, 0x21000000);
964 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
965 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
966 out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
967 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
969 out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
974 static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata =
976 .core_init = ppc405ex_pciex_core_init,
977 .port_init_hw = ppc405ex_pciex_init_port_hw,
978 .setup_utl = ppc405ex_pciex_init_utl,
981 #endif /* CONFIG_40x */
984 /* Check that the core has been initied and if not, do it */
985 static int __init ppc4xx_pciex_check_core_init(struct device_node *np)
987 static int core_init;
994 if (of_device_is_compatible(np, "ibm,plb-pciex-440spe")) {
995 if (ppc440spe_revA())
996 ppc4xx_pciex_hwops = &ppc440speA_pcie_hwops;
998 ppc4xx_pciex_hwops = &ppc440speB_pcie_hwops;
1000 if (of_device_is_compatible(np, "ibm,plb-pciex-460ex"))
1001 ppc4xx_pciex_hwops = &ppc460ex_pcie_hwops;
1002 #endif /* CONFIG_44x */
1004 if (of_device_is_compatible(np, "ibm,plb-pciex-405ex"))
1005 ppc4xx_pciex_hwops = &ppc405ex_pcie_hwops;
1007 if (ppc4xx_pciex_hwops == NULL) {
1008 printk(KERN_WARNING "PCIE: unknown host type %s\n",
1013 count = ppc4xx_pciex_hwops->core_init(np);
1015 ppc4xx_pciex_ports =
1016 kzalloc(count * sizeof(struct ppc4xx_pciex_port),
1018 if (ppc4xx_pciex_ports) {
1019 ppc4xx_pciex_port_count = count;
1022 printk(KERN_WARNING "PCIE: failed to allocate ports array\n");
1028 static void __init ppc4xx_pciex_port_init_mapping(struct ppc4xx_pciex_port *port)
1030 /* We map PCI Express configuration based on the reg property */
1031 dcr_write(port->dcrs, DCRO_PEGPL_CFGBAH,
1032 RES_TO_U32_HIGH(port->cfg_space.start));
1033 dcr_write(port->dcrs, DCRO_PEGPL_CFGBAL,
1034 RES_TO_U32_LOW(port->cfg_space.start));
1036 /* XXX FIXME: Use size from reg property. For now, map 512M */
1037 dcr_write(port->dcrs, DCRO_PEGPL_CFGMSK, 0xe0000001);
1039 /* We map UTL registers based on the reg property */
1040 dcr_write(port->dcrs, DCRO_PEGPL_REGBAH,
1041 RES_TO_U32_HIGH(port->utl_regs.start));
1042 dcr_write(port->dcrs, DCRO_PEGPL_REGBAL,
1043 RES_TO_U32_LOW(port->utl_regs.start));
1045 /* XXX FIXME: Use size from reg property */
1046 dcr_write(port->dcrs, DCRO_PEGPL_REGMSK, 0x00007001);
1048 /* Disable all other outbound windows */
1049 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, 0);
1050 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, 0);
1051 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0);
1052 dcr_write(port->dcrs, DCRO_PEGPL_MSGMSK, 0);
1055 static int __init ppc4xx_pciex_wait_on_sdr(struct ppc4xx_pciex_port *port,
1056 unsigned int sdr_offset,
1063 while(timeout_ms--) {
1064 val = mfdcri(SDR0, port->sdr_base + sdr_offset);
1065 if ((val & mask) == value) {
1066 pr_debug("PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
1067 port->index, sdr_offset, timeout_ms, val);
1075 static int __init ppc4xx_pciex_port_init(struct ppc4xx_pciex_port *port)
1080 if (ppc4xx_pciex_hwops->port_init_hw)
1081 rc = ppc4xx_pciex_hwops->port_init_hw(port);
1085 printk(KERN_INFO "PCIE%d: Checking link...\n",
1088 /* Wait for reset to complete */
1089 if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS, 1 << 20, 0, 10)) {
1090 printk(KERN_WARNING "PCIE%d: PGRST failed\n",
1095 /* Check for card presence detect if supported, if not, just wait for
1096 * link unconditionally.
1098 * note that we don't fail if there is no link, we just filter out
1099 * config space accesses. That way, it will be easier to implement
1102 if (!port->has_ibpre ||
1103 !ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
1104 1 << 28, 1 << 28, 100)) {
1106 "PCIE%d: Device detected, waiting for link...\n",
1108 if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
1109 0x1000, 0x1000, 2000))
1111 "PCIE%d: Link up failed\n", port->index);
1114 "PCIE%d: link is up !\n", port->index);
1118 printk(KERN_INFO "PCIE%d: No device detected.\n", port->index);
1121 * Initialize mapping: disable all regions and configure
1122 * CFG and REG regions based on resources in the device tree
1124 ppc4xx_pciex_port_init_mapping(port);
1129 port->utl_base = ioremap(port->utl_regs.start, 0x100);
1130 BUG_ON(port->utl_base == NULL);
1133 * Setup UTL registers --BenH.
1135 if (ppc4xx_pciex_hwops->setup_utl)
1136 ppc4xx_pciex_hwops->setup_utl(port);
1139 * Check for VC0 active and assert RDY.
1142 ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS,
1143 1 << 16, 1 << 16, 5000)) {
1144 printk(KERN_INFO "PCIE%d: VC0 not active\n", port->index);
1148 dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET, 0, 1 << 20);
1154 static int ppc4xx_pciex_validate_bdf(struct ppc4xx_pciex_port *port,
1155 struct pci_bus *bus,
1160 /* Endpoint can not generate upstream(remote) config cycles */
1161 if (port->endpoint && bus->number != port->hose->first_busno)
1162 return PCIBIOS_DEVICE_NOT_FOUND;
1164 /* Check we are within the mapped range */
1165 if (bus->number > port->hose->last_busno) {
1167 printk(KERN_WARNING "Warning! Probing bus %u"
1168 " out of range !\n", bus->number);
1171 return PCIBIOS_DEVICE_NOT_FOUND;
1174 /* The root complex has only one device / function */
1175 if (bus->number == port->hose->first_busno && devfn != 0)
1176 return PCIBIOS_DEVICE_NOT_FOUND;
1178 /* The other side of the RC has only one device as well */
1179 if (bus->number == (port->hose->first_busno + 1) &&
1180 PCI_SLOT(devfn) != 0)
1181 return PCIBIOS_DEVICE_NOT_FOUND;
1183 /* Check if we have a link */
1184 if ((bus->number != port->hose->first_busno) && !port->link)
1185 return PCIBIOS_DEVICE_NOT_FOUND;
1190 static void __iomem *ppc4xx_pciex_get_config_base(struct ppc4xx_pciex_port *port,
1191 struct pci_bus *bus,
1196 /* Remove the casts when we finally remove the stupid volatile
1197 * in struct pci_controller
1199 if (bus->number == port->hose->first_busno)
1200 return (void __iomem *)port->hose->cfg_addr;
1202 relbus = bus->number - (port->hose->first_busno + 1);
1203 return (void __iomem *)port->hose->cfg_data +
1204 ((relbus << 20) | (devfn << 12));
1207 static int ppc4xx_pciex_read_config(struct pci_bus *bus, unsigned int devfn,
1208 int offset, int len, u32 *val)
1210 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1211 struct ppc4xx_pciex_port *port =
1212 &ppc4xx_pciex_ports[hose->indirect_type];
1216 BUG_ON(hose != port->hose);
1218 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1219 return PCIBIOS_DEVICE_NOT_FOUND;
1221 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1224 * Reading from configuration space of non-existing device can
1225 * generate transaction errors. For the read duration we suppress
1226 * assertion of machine check exceptions to avoid those.
1228 gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1229 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1231 /* Make sure no CRS is recorded */
1232 out_be32(port->utl_base + PEUTL_RCSTA, 0x00040000);
1236 *val = in_8((u8 *)(addr + offset));
1239 *val = in_le16((u16 *)(addr + offset));
1242 *val = in_le32((u32 *)(addr + offset));
1246 pr_debug("pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
1247 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1248 bus->number, hose->first_busno, hose->last_busno,
1249 devfn, offset, len, addr + offset, *val);
1251 /* Check for CRS (440SPe rev B does that for us but heh ..) */
1252 if (in_be32(port->utl_base + PEUTL_RCSTA) & 0x00040000) {
1253 pr_debug("Got CRS !\n");
1254 if (len != 4 || offset != 0)
1255 return PCIBIOS_DEVICE_NOT_FOUND;
1259 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1261 return PCIBIOS_SUCCESSFUL;
1264 static int ppc4xx_pciex_write_config(struct pci_bus *bus, unsigned int devfn,
1265 int offset, int len, u32 val)
1267 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1268 struct ppc4xx_pciex_port *port =
1269 &ppc4xx_pciex_ports[hose->indirect_type];
1273 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1274 return PCIBIOS_DEVICE_NOT_FOUND;
1276 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1279 * Reading from configuration space of non-existing device can
1280 * generate transaction errors. For the read duration we suppress
1281 * assertion of machine check exceptions to avoid those.
1283 gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1284 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1286 pr_debug("pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
1287 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1288 bus->number, hose->first_busno, hose->last_busno,
1289 devfn, offset, len, addr + offset, val);
1293 out_8((u8 *)(addr + offset), val);
1296 out_le16((u16 *)(addr + offset), val);
1299 out_le32((u32 *)(addr + offset), val);
1303 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1305 return PCIBIOS_SUCCESSFUL;
1308 static struct pci_ops ppc4xx_pciex_pci_ops =
1310 .read = ppc4xx_pciex_read_config,
1311 .write = ppc4xx_pciex_write_config,
1314 static void __init ppc4xx_configure_pciex_POMs(struct ppc4xx_pciex_port *port,
1315 struct pci_controller *hose,
1316 void __iomem *mbase)
1318 u32 lah, lal, pciah, pcial, sa;
1321 /* Setup outbound memory windows */
1322 for (i = j = 0; i < 3; i++) {
1323 struct resource *res = &hose->mem_resources[i];
1325 /* we only care about memory windows */
1326 if (!(res->flags & IORESOURCE_MEM))
1329 printk(KERN_WARNING "%s: Too many ranges\n",
1330 port->node->full_name);
1334 /* Calculate register values */
1335 lah = RES_TO_U32_HIGH(res->start);
1336 lal = RES_TO_U32_LOW(res->start);
1337 pciah = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
1338 pcial = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
1339 sa = res->end + 1 - res->start;
1340 if (!is_power_of_2(sa) || sa < 0x100000 ||
1342 printk(KERN_WARNING "%s: Resource out of range\n",
1343 port->node->full_name);
1346 sa = (0xffffffffu << ilog2(sa)) | 0x1;
1348 /* Program register values */
1351 out_le32(mbase + PECFG_POM0LAH, pciah);
1352 out_le32(mbase + PECFG_POM0LAL, pcial);
1353 dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAH, lah);
1354 dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAL, lal);
1355 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKH, 0x7fffffff);
1356 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, sa | 3);
1359 out_le32(mbase + PECFG_POM1LAH, pciah);
1360 out_le32(mbase + PECFG_POM1LAL, pcial);
1361 dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAH, lah);
1362 dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAL, lal);
1363 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKH, 0x7fffffff);
1364 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, sa | 3);
1370 /* Configure IO, always 64K starting at 0 */
1371 if (hose->io_resource.flags & IORESOURCE_IO) {
1372 lah = RES_TO_U32_HIGH(hose->io_base_phys);
1373 lal = RES_TO_U32_LOW(hose->io_base_phys);
1374 out_le32(mbase + PECFG_POM2LAH, 0);
1375 out_le32(mbase + PECFG_POM2LAL, 0);
1376 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAH, lah);
1377 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAL, lal);
1378 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKH, 0x7fffffff);
1379 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0xffff0000 | 3);
1383 static void __init ppc4xx_configure_pciex_PIMs(struct ppc4xx_pciex_port *port,
1384 struct pci_controller *hose,
1385 void __iomem *mbase,
1386 struct resource *res)
1388 resource_size_t size = res->end - res->start + 1;
1391 if (port->endpoint) {
1392 resource_size_t ep_addr = 0;
1393 resource_size_t ep_size = 32 << 20;
1395 /* Currently we map a fixed 64MByte window to PLB address
1396 * 0 (SDRAM). This should probably be configurable via a dts
1400 /* Calculate window size */
1401 sa = (0xffffffffffffffffull << ilog2(ep_size));;
1404 out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
1405 out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa) |
1406 PCI_BASE_ADDRESS_MEM_TYPE_64);
1408 /* Disable BAR1 & BAR2 */
1409 out_le32(mbase + PECFG_BAR1MPA, 0);
1410 out_le32(mbase + PECFG_BAR2HMPA, 0);
1411 out_le32(mbase + PECFG_BAR2LMPA, 0);
1413 out_le32(mbase + PECFG_PIM01SAH, RES_TO_U32_HIGH(sa));
1414 out_le32(mbase + PECFG_PIM01SAL, RES_TO_U32_LOW(sa));
1416 out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(ep_addr));
1417 out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(ep_addr));
1419 /* Calculate window size */
1420 sa = (0xffffffffffffffffull << ilog2(size));;
1421 if (res->flags & IORESOURCE_PREFETCH)
1424 out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
1425 out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa));
1427 /* The setup of the split looks weird to me ... let's see
1430 out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
1431 out_le32(mbase + PECFG_PIM0LAH, 0x00000000);
1432 out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
1433 out_le32(mbase + PECFG_PIM1LAH, 0x00000000);
1434 out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
1435 out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
1437 out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(res->start));
1438 out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(res->start));
1441 /* Enable inbound mapping */
1442 out_le32(mbase + PECFG_PIMEN, 0x1);
1444 /* Enable I/O, Mem, and Busmaster cycles */
1445 out_le16(mbase + PCI_COMMAND,
1446 in_le16(mbase + PCI_COMMAND) |
1447 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1450 static void __init ppc4xx_pciex_port_setup_hose(struct ppc4xx_pciex_port *port)
1452 struct resource dma_window;
1453 struct pci_controller *hose = NULL;
1454 const int *bus_range;
1455 int primary = 0, busses;
1456 void __iomem *mbase = NULL, *cfg_data = NULL;
1460 /* Check if primary bridge */
1461 if (of_get_property(port->node, "primary", NULL))
1464 /* Get bus range if any */
1465 bus_range = of_get_property(port->node, "bus-range", NULL);
1467 /* Allocate the host controller data structure */
1468 hose = pcibios_alloc_controller(port->node);
1472 /* We stick the port number in "indirect_type" so the config space
1473 * ops can retrieve the port data structure easily
1475 hose->indirect_type = port->index;
1478 hose->first_busno = bus_range ? bus_range[0] : 0x0;
1479 hose->last_busno = bus_range ? bus_range[1] : 0xff;
1481 /* Because of how big mapping the config space is (1M per bus), we
1482 * limit how many busses we support. In the long run, we could replace
1483 * that with something akin to kmap_atomic instead. We set aside 1 bus
1484 * for the host itself too.
1486 busses = hose->last_busno - hose->first_busno; /* This is off by 1 */
1487 if (busses > MAX_PCIE_BUS_MAPPED) {
1488 busses = MAX_PCIE_BUS_MAPPED;
1489 hose->last_busno = hose->first_busno + busses;
1492 if (!port->endpoint) {
1493 /* Only map the external config space in cfg_data for
1494 * PCIe root-complexes. External space is 1M per bus
1496 cfg_data = ioremap(port->cfg_space.start +
1497 (hose->first_busno + 1) * 0x100000,
1499 if (cfg_data == NULL) {
1500 printk(KERN_ERR "%s: Can't map external config space !",
1501 port->node->full_name);
1504 hose->cfg_data = cfg_data;
1507 /* Always map the host config space in cfg_addr.
1508 * Internal space is 4K
1510 mbase = ioremap(port->cfg_space.start + 0x10000000, 0x1000);
1511 if (mbase == NULL) {
1512 printk(KERN_ERR "%s: Can't map internal config space !",
1513 port->node->full_name);
1516 hose->cfg_addr = mbase;
1518 pr_debug("PCIE %s, bus %d..%d\n", port->node->full_name,
1519 hose->first_busno, hose->last_busno);
1520 pr_debug(" config space mapped at: root @0x%p, other @0x%p\n",
1521 hose->cfg_addr, hose->cfg_data);
1523 /* Setup config space */
1524 hose->ops = &ppc4xx_pciex_pci_ops;
1526 mbase = (void __iomem *)hose->cfg_addr;
1528 if (!port->endpoint) {
1530 * Set bus numbers on our root port
1532 out_8(mbase + PCI_PRIMARY_BUS, hose->first_busno);
1533 out_8(mbase + PCI_SECONDARY_BUS, hose->first_busno + 1);
1534 out_8(mbase + PCI_SUBORDINATE_BUS, hose->last_busno);
1538 * OMRs are already reset, also disable PIMs
1540 out_le32(mbase + PECFG_PIMEN, 0);
1542 /* Parse outbound mapping resources */
1543 pci_process_bridge_OF_ranges(hose, port->node, primary);
1545 /* Parse inbound mapping resources */
1546 if (ppc4xx_parse_dma_ranges(hose, mbase, &dma_window) != 0)
1549 /* Configure outbound ranges POMs */
1550 ppc4xx_configure_pciex_POMs(port, hose, mbase);
1552 /* Configure inbound ranges PIMs */
1553 ppc4xx_configure_pciex_PIMs(port, hose, mbase, &dma_window);
1555 /* The root complex doesn't show up if we don't set some vendor
1556 * and device IDs into it. The defaults below are the same bogus
1557 * one that the initial code in arch/ppc had. This can be
1558 * overwritten by setting the "vendor-id/device-id" properties
1559 * in the pciex node.
1562 /* Get the (optional) vendor-/device-id from the device-tree */
1563 pval = of_get_property(port->node, "vendor-id", NULL);
1567 if (!port->endpoint)
1568 val = 0xaaa0 + port->index;
1570 val = 0xeee0 + port->index;
1572 out_le16(mbase + 0x200, val);
1574 pval = of_get_property(port->node, "device-id", NULL);
1578 if (!port->endpoint)
1579 val = 0xbed0 + port->index;
1581 val = 0xfed0 + port->index;
1583 out_le16(mbase + 0x202, val);
1585 if (!port->endpoint) {
1586 /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
1587 out_le32(mbase + 0x208, 0x06040001);
1589 printk(KERN_INFO "PCIE%d: successfully set as root-complex\n",
1592 /* Set Class Code to Processor/PPC */
1593 out_le32(mbase + 0x208, 0x0b200001);
1595 printk(KERN_INFO "PCIE%d: successfully set as endpoint\n",
1602 pcibios_free_controller(hose);
1609 static void __init ppc4xx_probe_pciex_bridge(struct device_node *np)
1611 struct ppc4xx_pciex_port *port;
1617 /* First, proceed to core initialization as we assume there's
1618 * only one PCIe core in the system
1620 if (ppc4xx_pciex_check_core_init(np))
1623 /* Get the port number from the device-tree */
1624 pval = of_get_property(np, "port", NULL);
1626 printk(KERN_ERR "PCIE: Can't find port number for %s\n",
1631 if (portno >= ppc4xx_pciex_port_count) {
1632 printk(KERN_ERR "PCIE: port number out of range for %s\n",
1636 port = &ppc4xx_pciex_ports[portno];
1637 port->index = portno;
1640 * Check if device is enabled
1642 if (!of_device_is_available(np)) {
1643 printk(KERN_INFO "PCIE%d: Port disabled via device-tree\n", port->index);
1647 port->node = of_node_get(np);
1648 pval = of_get_property(np, "sdr-base", NULL);
1650 printk(KERN_ERR "PCIE: missing sdr-base for %s\n",
1654 port->sdr_base = *pval;
1656 /* Check if device_type property is set to "pci" or "pci-endpoint".
1657 * Resulting from this setup this PCIe port will be configured
1658 * as root-complex or as endpoint.
1660 val = of_get_property(port->node, "device_type", NULL);
1661 if (!strcmp(val, "pci-endpoint")) {
1663 } else if (!strcmp(val, "pci")) {
1666 printk(KERN_ERR "PCIE: missing or incorrect device_type for %s\n",
1671 /* Fetch config space registers address */
1672 if (of_address_to_resource(np, 0, &port->cfg_space)) {
1673 printk(KERN_ERR "%s: Can't get PCI-E config space !",
1677 /* Fetch host bridge internal registers address */
1678 if (of_address_to_resource(np, 1, &port->utl_regs)) {
1679 printk(KERN_ERR "%s: Can't get UTL register base !",
1685 dcrs = dcr_resource_start(np, 0);
1687 printk(KERN_ERR "%s: Can't get DCR register base !",
1691 port->dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
1693 /* Initialize the port specific registers */
1694 if (ppc4xx_pciex_port_init(port)) {
1695 printk(KERN_WARNING "PCIE%d: Port init failed\n", port->index);
1699 /* Setup the linux hose data structure */
1700 ppc4xx_pciex_port_setup_hose(port);
1703 #endif /* CONFIG_PPC4xx_PCI_EXPRESS */
1705 static int __init ppc4xx_pci_find_bridges(void)
1707 struct device_node *np;
1709 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
1710 for_each_compatible_node(np, NULL, "ibm,plb-pciex")
1711 ppc4xx_probe_pciex_bridge(np);
1713 for_each_compatible_node(np, NULL, "ibm,plb-pcix")
1714 ppc4xx_probe_pcix_bridge(np);
1715 for_each_compatible_node(np, NULL, "ibm,plb-pci")
1716 ppc4xx_probe_pci_bridge(np);
1720 arch_initcall(ppc4xx_pci_find_bridges);