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>
27 #include <linux/slab.h>
30 #include <asm/pci-bridge.h>
31 #include <asm/machdep.h>
33 #include <asm/dcr-regs.h>
34 #include <mm/mmu_decl.h>
36 #include "ppc4xx_pci.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)
50 /* Catch both 440SPe variants, with and without RAID6 support */
51 if ((mfspr(SPRN_PVR
) & 0xffefffff) == 0x53421890)
57 static void fixup_ppc4xx_pci_bridge(struct pci_dev
*dev
)
59 struct pci_controller
*hose
;
62 if (dev
->devfn
!= 0 || dev
->bus
->self
!= NULL
)
65 hose
= pci_bus_to_host(dev
->bus
);
69 if (!of_device_is_compatible(hose
->dn
, "ibm,plb-pciex") &&
70 !of_device_is_compatible(hose
->dn
, "ibm,plb-pcix") &&
71 !of_device_is_compatible(hose
->dn
, "ibm,plb-pci"))
74 if (of_device_is_compatible(hose
->dn
, "ibm,plb440epx-pci") ||
75 of_device_is_compatible(hose
->dn
, "ibm,plb440grx-pci")) {
76 hose
->indirect_type
|= PPC_INDIRECT_TYPE_BROKEN_MRM
;
79 /* Hide the PCI host BARs from the kernel as their content doesn't
80 * fit well in the resource management
82 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
83 dev
->resource
[i
].start
= dev
->resource
[i
].end
= 0;
84 dev
->resource
[i
].flags
= 0;
87 printk(KERN_INFO
"PCI: Hiding 4xx host bridge resources %s\n",
90 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID
, PCI_ANY_ID
, fixup_ppc4xx_pci_bridge
);
92 static int __init
ppc4xx_parse_dma_ranges(struct pci_controller
*hose
,
99 int pna
= of_n_addr_cells(hose
->dn
);
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=%llx)\n",
167 hose
->dn
->full_name
, size
, (u64
)total_memory
);
171 /* Check we are a power of 2 size and that base is a multiple of size*/
172 if ((size
& (size
- 1)) != 0 ||
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 int __init
ppc4xx_setup_one_pci_PMM(struct pci_controller
*hose
,
206 u32 ma
, pcila
, pciha
;
208 /* Hack warning ! The "old" PCI 2.x cell only let us configure the low
209 * 32-bit of incoming PLB addresses. The top 4 bits of the 36-bit
210 * address are actually hard wired to a value that appears to depend
211 * on the specific SoC. For example, it's 0 on 440EP and 1 on 440EPx.
213 * The trick here is we just crop those top bits and ignore them when
214 * programming the chip. That means the device-tree has to be right
215 * for the specific part used (we don't print a warning if it's wrong
216 * but on the other hand, you'll crash quickly enough), but at least
217 * this code should work whatever the hard coded value is
219 plb_addr
&= 0xffffffffull
;
221 /* Note: Due to the above hack, the test below doesn't actually test
222 * if you address is above 4G, but it tests that address and
223 * (address + size) are both contained in the same 4G
225 if ((plb_addr
+ size
) > 0xffffffffull
|| !is_power_of_2(size
) ||
226 size
< 0x1000 || (plb_addr
& (size
- 1)) != 0) {
227 printk(KERN_WARNING
"%s: Resource out of range\n",
228 hose
->dn
->full_name
);
231 ma
= (0xffffffffu
<< ilog2(size
)) | 1;
232 if (flags
& IORESOURCE_PREFETCH
)
235 pciha
= RES_TO_U32_HIGH(pci_addr
);
236 pcila
= RES_TO_U32_LOW(pci_addr
);
238 writel(plb_addr
, reg
+ PCIL0_PMM0LA
+ (0x10 * index
));
239 writel(pcila
, reg
+ PCIL0_PMM0PCILA
+ (0x10 * index
));
240 writel(pciha
, reg
+ PCIL0_PMM0PCIHA
+ (0x10 * index
));
241 writel(ma
, reg
+ PCIL0_PMM0MA
+ (0x10 * index
));
246 static void __init
ppc4xx_configure_pci_PMMs(struct pci_controller
*hose
,
249 int i
, j
, found_isa_hole
= 0;
251 /* Setup outbound memory windows */
252 for (i
= j
= 0; i
< 3; i
++) {
253 struct resource
*res
= &hose
->mem_resources
[i
];
255 /* we only care about memory windows */
256 if (!(res
->flags
& IORESOURCE_MEM
))
259 printk(KERN_WARNING
"%s: Too many ranges\n",
260 hose
->dn
->full_name
);
264 /* Configure the resource */
265 if (ppc4xx_setup_one_pci_PMM(hose
, reg
,
267 res
->start
- hose
->pci_mem_offset
,
268 res
->end
+ 1 - res
->start
,
273 /* If the resource PCI address is 0 then we have our
276 if (res
->start
== hose
->pci_mem_offset
)
281 /* Handle ISA memory hole if not already covered */
282 if (j
<= 2 && !found_isa_hole
&& hose
->isa_mem_size
)
283 if (ppc4xx_setup_one_pci_PMM(hose
, reg
, hose
->isa_mem_phys
, 0,
284 hose
->isa_mem_size
, 0, j
) == 0)
285 printk(KERN_INFO
"%s: Legacy ISA memory support enabled\n",
286 hose
->dn
->full_name
);
289 static void __init
ppc4xx_configure_pci_PTMs(struct pci_controller
*hose
,
291 const struct resource
*res
)
293 resource_size_t size
= res
->end
- res
->start
+ 1;
296 /* Calculate window size */
297 sa
= (0xffffffffu
<< ilog2(size
)) | 1;
300 /* RAM is always at 0 local for now */
301 writel(0, reg
+ PCIL0_PTM1LA
);
302 writel(sa
, reg
+ PCIL0_PTM1MS
);
304 /* Map on PCI side */
305 early_write_config_dword(hose
, hose
->first_busno
, 0,
306 PCI_BASE_ADDRESS_1
, res
->start
);
307 early_write_config_dword(hose
, hose
->first_busno
, 0,
308 PCI_BASE_ADDRESS_2
, 0x00000000);
309 early_write_config_word(hose
, hose
->first_busno
, 0,
310 PCI_COMMAND
, 0x0006);
313 static void __init
ppc4xx_probe_pci_bridge(struct device_node
*np
)
316 struct resource rsrc_cfg
;
317 struct resource rsrc_reg
;
318 struct resource dma_window
;
319 struct pci_controller
*hose
= NULL
;
320 void __iomem
*reg
= NULL
;
321 const int *bus_range
;
324 /* Check if device is enabled */
325 if (!of_device_is_available(np
)) {
326 printk(KERN_INFO
"%s: Port disabled via device-tree\n",
331 /* Fetch config space registers address */
332 if (of_address_to_resource(np
, 0, &rsrc_cfg
)) {
333 printk(KERN_ERR
"%s: Can't get PCI config register base !",
337 /* Fetch host bridge internal registers address */
338 if (of_address_to_resource(np
, 3, &rsrc_reg
)) {
339 printk(KERN_ERR
"%s: Can't get PCI internal register base !",
344 /* Check if primary bridge */
345 if (of_get_property(np
, "primary", NULL
))
348 /* Get bus range if any */
349 bus_range
= of_get_property(np
, "bus-range", NULL
);
352 reg
= ioremap(rsrc_reg
.start
, rsrc_reg
.end
+ 1 - rsrc_reg
.start
);
354 printk(KERN_ERR
"%s: Can't map registers !", np
->full_name
);
358 /* Allocate the host controller data structure */
359 hose
= pcibios_alloc_controller(np
);
363 hose
->first_busno
= bus_range
? bus_range
[0] : 0x0;
364 hose
->last_busno
= bus_range
? bus_range
[1] : 0xff;
366 /* Setup config space */
367 setup_indirect_pci(hose
, rsrc_cfg
.start
, rsrc_cfg
.start
+ 0x4, 0);
369 /* Disable all windows */
370 writel(0, reg
+ PCIL0_PMM0MA
);
371 writel(0, reg
+ PCIL0_PMM1MA
);
372 writel(0, reg
+ PCIL0_PMM2MA
);
373 writel(0, reg
+ PCIL0_PTM1MS
);
374 writel(0, reg
+ PCIL0_PTM2MS
);
376 /* Parse outbound mapping resources */
377 pci_process_bridge_OF_ranges(hose
, np
, primary
);
379 /* Parse inbound mapping resources */
380 if (ppc4xx_parse_dma_ranges(hose
, reg
, &dma_window
) != 0)
383 /* Configure outbound ranges POMs */
384 ppc4xx_configure_pci_PMMs(hose
, reg
);
386 /* Configure inbound ranges PIMs */
387 ppc4xx_configure_pci_PTMs(hose
, reg
, &dma_window
);
389 /* We don't need the registers anymore */
395 pcibios_free_controller(hose
);
404 static int __init
ppc4xx_setup_one_pcix_POM(struct pci_controller
*hose
,
412 u32 lah
, lal
, pciah
, pcial
, sa
;
414 if (!is_power_of_2(size
) || size
< 0x1000 ||
415 (plb_addr
& (size
- 1)) != 0) {
416 printk(KERN_WARNING
"%s: Resource out of range\n",
417 hose
->dn
->full_name
);
421 /* Calculate register values */
422 lah
= RES_TO_U32_HIGH(plb_addr
);
423 lal
= RES_TO_U32_LOW(plb_addr
);
424 pciah
= RES_TO_U32_HIGH(pci_addr
);
425 pcial
= RES_TO_U32_LOW(pci_addr
);
426 sa
= (0xffffffffu
<< ilog2(size
)) | 0x1;
428 /* Program register values */
430 writel(lah
, reg
+ PCIX0_POM0LAH
);
431 writel(lal
, reg
+ PCIX0_POM0LAL
);
432 writel(pciah
, reg
+ PCIX0_POM0PCIAH
);
433 writel(pcial
, reg
+ PCIX0_POM0PCIAL
);
434 writel(sa
, reg
+ PCIX0_POM0SA
);
436 writel(lah
, reg
+ PCIX0_POM1LAH
);
437 writel(lal
, reg
+ PCIX0_POM1LAL
);
438 writel(pciah
, reg
+ PCIX0_POM1PCIAH
);
439 writel(pcial
, reg
+ PCIX0_POM1PCIAL
);
440 writel(sa
, reg
+ PCIX0_POM1SA
);
446 static void __init
ppc4xx_configure_pcix_POMs(struct pci_controller
*hose
,
449 int i
, j
, found_isa_hole
= 0;
451 /* Setup outbound memory windows */
452 for (i
= j
= 0; i
< 3; i
++) {
453 struct resource
*res
= &hose
->mem_resources
[i
];
455 /* we only care about memory windows */
456 if (!(res
->flags
& IORESOURCE_MEM
))
459 printk(KERN_WARNING
"%s: Too many ranges\n",
460 hose
->dn
->full_name
);
464 /* Configure the resource */
465 if (ppc4xx_setup_one_pcix_POM(hose
, reg
,
467 res
->start
- hose
->pci_mem_offset
,
468 res
->end
+ 1 - res
->start
,
473 /* If the resource PCI address is 0 then we have our
476 if (res
->start
== hose
->pci_mem_offset
)
481 /* Handle ISA memory hole if not already covered */
482 if (j
<= 1 && !found_isa_hole
&& hose
->isa_mem_size
)
483 if (ppc4xx_setup_one_pcix_POM(hose
, reg
, hose
->isa_mem_phys
, 0,
484 hose
->isa_mem_size
, 0, j
) == 0)
485 printk(KERN_INFO
"%s: Legacy ISA memory support enabled\n",
486 hose
->dn
->full_name
);
489 static void __init
ppc4xx_configure_pcix_PIMs(struct pci_controller
*hose
,
491 const struct resource
*res
,
495 resource_size_t size
= res
->end
- res
->start
+ 1;
498 /* RAM is always at 0 */
499 writel(0x00000000, reg
+ PCIX0_PIM0LAH
);
500 writel(0x00000000, reg
+ PCIX0_PIM0LAL
);
502 /* Calculate window size */
503 sa
= (0xffffffffu
<< ilog2(size
)) | 1;
505 if (res
->flags
& IORESOURCE_PREFETCH
)
509 writel(sa
, reg
+ PCIX0_PIM0SA
);
511 writel(0xffffffff, reg
+ PCIX0_PIM0SAH
);
513 /* Map on PCI side */
514 writel(0x00000000, reg
+ PCIX0_BAR0H
);
515 writel(res
->start
, reg
+ PCIX0_BAR0L
);
516 writew(0x0006, reg
+ PCIX0_COMMAND
);
519 static void __init
ppc4xx_probe_pcix_bridge(struct device_node
*np
)
521 struct resource rsrc_cfg
;
522 struct resource rsrc_reg
;
523 struct resource dma_window
;
524 struct pci_controller
*hose
= NULL
;
525 void __iomem
*reg
= NULL
;
526 const int *bus_range
;
527 int big_pim
= 0, msi
= 0, primary
= 0;
529 /* Fetch config space registers address */
530 if (of_address_to_resource(np
, 0, &rsrc_cfg
)) {
531 printk(KERN_ERR
"%s:Can't get PCI-X config register base !",
535 /* Fetch host bridge internal registers address */
536 if (of_address_to_resource(np
, 3, &rsrc_reg
)) {
537 printk(KERN_ERR
"%s: Can't get PCI-X internal register base !",
542 /* Check if it supports large PIMs (440GX) */
543 if (of_get_property(np
, "large-inbound-windows", NULL
))
546 /* Check if we should enable MSIs inbound hole */
547 if (of_get_property(np
, "enable-msi-hole", NULL
))
550 /* Check if primary bridge */
551 if (of_get_property(np
, "primary", NULL
))
554 /* Get bus range if any */
555 bus_range
= of_get_property(np
, "bus-range", NULL
);
558 reg
= ioremap(rsrc_reg
.start
, rsrc_reg
.end
+ 1 - rsrc_reg
.start
);
560 printk(KERN_ERR
"%s: Can't map registers !", np
->full_name
);
564 /* Allocate the host controller data structure */
565 hose
= pcibios_alloc_controller(np
);
569 hose
->first_busno
= bus_range
? bus_range
[0] : 0x0;
570 hose
->last_busno
= bus_range
? bus_range
[1] : 0xff;
572 /* Setup config space */
573 setup_indirect_pci(hose
, rsrc_cfg
.start
, rsrc_cfg
.start
+ 0x4,
574 PPC_INDIRECT_TYPE_SET_CFG_TYPE
);
576 /* Disable all windows */
577 writel(0, reg
+ PCIX0_POM0SA
);
578 writel(0, reg
+ PCIX0_POM1SA
);
579 writel(0, reg
+ PCIX0_POM2SA
);
580 writel(0, reg
+ PCIX0_PIM0SA
);
581 writel(0, reg
+ PCIX0_PIM1SA
);
582 writel(0, reg
+ PCIX0_PIM2SA
);
584 writel(0, reg
+ PCIX0_PIM0SAH
);
585 writel(0, reg
+ PCIX0_PIM2SAH
);
588 /* Parse outbound mapping resources */
589 pci_process_bridge_OF_ranges(hose
, np
, primary
);
591 /* Parse inbound mapping resources */
592 if (ppc4xx_parse_dma_ranges(hose
, reg
, &dma_window
) != 0)
595 /* Configure outbound ranges POMs */
596 ppc4xx_configure_pcix_POMs(hose
, reg
);
598 /* Configure inbound ranges PIMs */
599 ppc4xx_configure_pcix_PIMs(hose
, reg
, &dma_window
, big_pim
, msi
);
601 /* We don't need the registers anymore */
607 pcibios_free_controller(hose
);
612 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
615 * 4xx PCI-Express part
617 * We support 3 parts currently based on the compatible property:
619 * ibm,plb-pciex-440spe
620 * ibm,plb-pciex-405ex
621 * ibm,plb-pciex-460ex
623 * Anything else will be rejected for now as they are all subtly
624 * different unfortunately.
628 #define MAX_PCIE_BUS_MAPPED 0x40
630 struct ppc4xx_pciex_port
632 struct pci_controller
*hose
;
633 struct device_node
*node
;
638 unsigned int sdr_base
;
640 struct resource cfg_space
;
641 struct resource utl_regs
;
642 void __iomem
*utl_base
;
645 static struct ppc4xx_pciex_port
*ppc4xx_pciex_ports
;
646 static unsigned int ppc4xx_pciex_port_count
;
648 struct ppc4xx_pciex_hwops
650 int (*core_init
)(struct device_node
*np
);
651 int (*port_init_hw
)(struct ppc4xx_pciex_port
*port
);
652 int (*setup_utl
)(struct ppc4xx_pciex_port
*port
);
655 static struct ppc4xx_pciex_hwops
*ppc4xx_pciex_hwops
;
659 /* Check various reset bits of the 440SPe PCIe core */
660 static int __init
ppc440spe_pciex_check_reset(struct device_node
*np
)
662 u32 valPE0
, valPE1
, valPE2
;
665 /* SDR0_PEGPLLLCT1 reset */
666 if (!(mfdcri(SDR0
, PESDR0_PLLLCT1
) & 0x01000000)) {
668 * the PCIe core was probably already initialised
669 * by firmware - let's re-reset RCSSET regs
671 * -- Shouldn't we also re-reset the whole thing ? -- BenH
673 pr_debug("PCIE: SDR0_PLLLCT1 already reset.\n");
674 mtdcri(SDR0
, PESDR0_440SPE_RCSSET
, 0x01010000);
675 mtdcri(SDR0
, PESDR1_440SPE_RCSSET
, 0x01010000);
676 mtdcri(SDR0
, PESDR2_440SPE_RCSSET
, 0x01010000);
679 valPE0
= mfdcri(SDR0
, PESDR0_440SPE_RCSSET
);
680 valPE1
= mfdcri(SDR0
, PESDR1_440SPE_RCSSET
);
681 valPE2
= mfdcri(SDR0
, PESDR2_440SPE_RCSSET
);
683 /* SDR0_PExRCSSET rstgu */
684 if (!(valPE0
& 0x01000000) ||
685 !(valPE1
& 0x01000000) ||
686 !(valPE2
& 0x01000000)) {
687 printk(KERN_INFO
"PCIE: SDR0_PExRCSSET rstgu error\n");
691 /* SDR0_PExRCSSET rstdl */
692 if (!(valPE0
& 0x00010000) ||
693 !(valPE1
& 0x00010000) ||
694 !(valPE2
& 0x00010000)) {
695 printk(KERN_INFO
"PCIE: SDR0_PExRCSSET rstdl error\n");
699 /* SDR0_PExRCSSET rstpyn */
700 if ((valPE0
& 0x00001000) ||
701 (valPE1
& 0x00001000) ||
702 (valPE2
& 0x00001000)) {
703 printk(KERN_INFO
"PCIE: SDR0_PExRCSSET rstpyn error\n");
707 /* SDR0_PExRCSSET hldplb */
708 if ((valPE0
& 0x10000000) ||
709 (valPE1
& 0x10000000) ||
710 (valPE2
& 0x10000000)) {
711 printk(KERN_INFO
"PCIE: SDR0_PExRCSSET hldplb error\n");
715 /* SDR0_PExRCSSET rdy */
716 if ((valPE0
& 0x00100000) ||
717 (valPE1
& 0x00100000) ||
718 (valPE2
& 0x00100000)) {
719 printk(KERN_INFO
"PCIE: SDR0_PExRCSSET rdy error\n");
723 /* SDR0_PExRCSSET shutdown */
724 if ((valPE0
& 0x00000100) ||
725 (valPE1
& 0x00000100) ||
726 (valPE2
& 0x00000100)) {
727 printk(KERN_INFO
"PCIE: SDR0_PExRCSSET shutdown error\n");
734 /* Global PCIe core initializations for 440SPe core */
735 static int __init
ppc440spe_pciex_core_init(struct device_node
*np
)
739 /* Set PLL clock receiver to LVPECL */
740 dcri_clrset(SDR0
, PESDR0_PLLLCT1
, 0, 1 << 28);
742 /* Shouldn't we do all the calibration stuff etc... here ? */
743 if (ppc440spe_pciex_check_reset(np
))
746 if (!(mfdcri(SDR0
, PESDR0_PLLLCT2
) & 0x10000)) {
747 printk(KERN_INFO
"PCIE: PESDR_PLLCT2 resistance calibration "
749 mfdcri(SDR0
, PESDR0_PLLLCT2
));
753 /* De-assert reset of PCIe PLL, wait for lock */
754 dcri_clrset(SDR0
, PESDR0_PLLLCT1
, 1 << 24, 0);
758 if (!(mfdcri(SDR0
, PESDR0_PLLLCT3
) & 0x10000000)) {
765 printk(KERN_INFO
"PCIE: VCO output not locked\n");
769 pr_debug("PCIE initialization OK\n");
774 static int ppc440spe_pciex_init_port_hw(struct ppc4xx_pciex_port
*port
)
779 val
= PTYPE_LEGACY_ENDPOINT
<< 20;
781 val
= PTYPE_ROOT_PORT
<< 20;
783 if (port
->index
== 0)
784 val
|= LNKW_X8
<< 12;
786 val
|= LNKW_X4
<< 12;
788 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_DLPSET
, val
);
789 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_UTLSET1
, 0x20222222);
790 if (ppc440spe_revA())
791 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_UTLSET2
, 0x11000000);
792 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_440SPE_HSSL0SET1
, 0x35000000);
793 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_440SPE_HSSL1SET1
, 0x35000000);
794 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_440SPE_HSSL2SET1
, 0x35000000);
795 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_440SPE_HSSL3SET1
, 0x35000000);
796 if (port
->index
== 0) {
797 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_440SPE_HSSL4SET1
,
799 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_440SPE_HSSL5SET1
,
801 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_440SPE_HSSL6SET1
,
803 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_440SPE_HSSL7SET1
,
806 dcri_clrset(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
,
807 (1 << 24) | (1 << 16), 1 << 12);
812 static int ppc440speA_pciex_init_port_hw(struct ppc4xx_pciex_port
*port
)
814 return ppc440spe_pciex_init_port_hw(port
);
817 static int ppc440speB_pciex_init_port_hw(struct ppc4xx_pciex_port
*port
)
819 int rc
= ppc440spe_pciex_init_port_hw(port
);
826 static int ppc440speA_pciex_init_utl(struct ppc4xx_pciex_port
*port
)
828 /* XXX Check what that value means... I hate magic */
829 dcr_write(port
->dcrs
, DCRO_PEGPL_SPECIAL
, 0x68782800);
832 * Set buffer allocations and then assert VRB and TXE.
834 out_be32(port
->utl_base
+ PEUTL_OUTTR
, 0x08000000);
835 out_be32(port
->utl_base
+ PEUTL_INTR
, 0x02000000);
836 out_be32(port
->utl_base
+ PEUTL_OPDBSZ
, 0x10000000);
837 out_be32(port
->utl_base
+ PEUTL_PBBSZ
, 0x53000000);
838 out_be32(port
->utl_base
+ PEUTL_IPHBSZ
, 0x08000000);
839 out_be32(port
->utl_base
+ PEUTL_IPDBSZ
, 0x10000000);
840 out_be32(port
->utl_base
+ PEUTL_RCIRQEN
, 0x00f00000);
841 out_be32(port
->utl_base
+ PEUTL_PCTL
, 0x80800066);
846 static int ppc440speB_pciex_init_utl(struct ppc4xx_pciex_port
*port
)
848 /* Report CRS to the operating system */
849 out_be32(port
->utl_base
+ PEUTL_PBCTL
, 0x08000000);
854 static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops __initdata
=
856 .core_init
= ppc440spe_pciex_core_init
,
857 .port_init_hw
= ppc440speA_pciex_init_port_hw
,
858 .setup_utl
= ppc440speA_pciex_init_utl
,
861 static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata
=
863 .core_init
= ppc440spe_pciex_core_init
,
864 .port_init_hw
= ppc440speB_pciex_init_port_hw
,
865 .setup_utl
= ppc440speB_pciex_init_utl
,
868 static int __init
ppc460ex_pciex_core_init(struct device_node
*np
)
870 /* Nothing to do, return 2 ports */
874 static int ppc460ex_pciex_init_port_hw(struct ppc4xx_pciex_port
*port
)
880 val
= PTYPE_LEGACY_ENDPOINT
<< 20;
882 val
= PTYPE_ROOT_PORT
<< 20;
884 if (port
->index
== 0) {
885 val
|= LNKW_X1
<< 12;
886 utlset1
= 0x20000000;
888 val
|= LNKW_X4
<< 12;
889 utlset1
= 0x20101101;
892 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_DLPSET
, val
);
893 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_UTLSET1
, utlset1
);
894 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_UTLSET2
, 0x01210000);
896 switch (port
->index
) {
898 mtdcri(SDR0
, PESDR0_460EX_L0CDRCTL
, 0x00003230);
899 mtdcri(SDR0
, PESDR0_460EX_L0DRV
, 0x00000130);
900 mtdcri(SDR0
, PESDR0_460EX_L0CLK
, 0x00000006);
902 mtdcri(SDR0
, PESDR0_460EX_PHY_CTL_RST
,0x10000000);
906 mtdcri(SDR0
, PESDR1_460EX_L0CDRCTL
, 0x00003230);
907 mtdcri(SDR0
, PESDR1_460EX_L1CDRCTL
, 0x00003230);
908 mtdcri(SDR0
, PESDR1_460EX_L2CDRCTL
, 0x00003230);
909 mtdcri(SDR0
, PESDR1_460EX_L3CDRCTL
, 0x00003230);
910 mtdcri(SDR0
, PESDR1_460EX_L0DRV
, 0x00000130);
911 mtdcri(SDR0
, PESDR1_460EX_L1DRV
, 0x00000130);
912 mtdcri(SDR0
, PESDR1_460EX_L2DRV
, 0x00000130);
913 mtdcri(SDR0
, PESDR1_460EX_L3DRV
, 0x00000130);
914 mtdcri(SDR0
, PESDR1_460EX_L0CLK
, 0x00000006);
915 mtdcri(SDR0
, PESDR1_460EX_L1CLK
, 0x00000006);
916 mtdcri(SDR0
, PESDR1_460EX_L2CLK
, 0x00000006);
917 mtdcri(SDR0
, PESDR1_460EX_L3CLK
, 0x00000006);
919 mtdcri(SDR0
, PESDR1_460EX_PHY_CTL_RST
,0x10000000);
923 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
,
924 mfdcri(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
) |
925 (PESDRx_RCSSET_RSTGU
| PESDRx_RCSSET_RSTPYN
));
927 /* Poll for PHY reset */
928 /* XXX FIXME add timeout */
929 switch (port
->index
) {
931 while (!(mfdcri(SDR0
, PESDR0_460EX_RSTSTA
) & 0x1))
935 while (!(mfdcri(SDR0
, PESDR1_460EX_RSTSTA
) & 0x1))
940 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
,
941 (mfdcri(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
) &
942 ~(PESDRx_RCSSET_RSTGU
| PESDRx_RCSSET_RSTDL
)) |
943 PESDRx_RCSSET_RSTPYN
);
950 static int ppc460ex_pciex_init_utl(struct ppc4xx_pciex_port
*port
)
952 dcr_write(port
->dcrs
, DCRO_PEGPL_SPECIAL
, 0x0);
955 * Set buffer allocations and then assert VRB and TXE.
957 out_be32(port
->utl_base
+ PEUTL_PBCTL
, 0x0800000c);
958 out_be32(port
->utl_base
+ PEUTL_OUTTR
, 0x08000000);
959 out_be32(port
->utl_base
+ PEUTL_INTR
, 0x02000000);
960 out_be32(port
->utl_base
+ PEUTL_OPDBSZ
, 0x04000000);
961 out_be32(port
->utl_base
+ PEUTL_PBBSZ
, 0x00000000);
962 out_be32(port
->utl_base
+ PEUTL_IPHBSZ
, 0x02000000);
963 out_be32(port
->utl_base
+ PEUTL_IPDBSZ
, 0x04000000);
964 out_be32(port
->utl_base
+ PEUTL_RCIRQEN
,0x00f00000);
965 out_be32(port
->utl_base
+ PEUTL_PCTL
, 0x80800066);
970 static struct ppc4xx_pciex_hwops ppc460ex_pcie_hwops __initdata
=
972 .core_init
= ppc460ex_pciex_core_init
,
973 .port_init_hw
= ppc460ex_pciex_init_port_hw
,
974 .setup_utl
= ppc460ex_pciex_init_utl
,
977 static int __init
ppc460sx_pciex_core_init(struct device_node
*np
)
979 /* HSS drive amplitude */
980 mtdcri(SDR0
, PESDR0_460SX_HSSL0DAMP
, 0xB9843211);
981 mtdcri(SDR0
, PESDR0_460SX_HSSL1DAMP
, 0xB9843211);
982 mtdcri(SDR0
, PESDR0_460SX_HSSL2DAMP
, 0xB9843211);
983 mtdcri(SDR0
, PESDR0_460SX_HSSL3DAMP
, 0xB9843211);
984 mtdcri(SDR0
, PESDR0_460SX_HSSL4DAMP
, 0xB9843211);
985 mtdcri(SDR0
, PESDR0_460SX_HSSL5DAMP
, 0xB9843211);
986 mtdcri(SDR0
, PESDR0_460SX_HSSL6DAMP
, 0xB9843211);
987 mtdcri(SDR0
, PESDR0_460SX_HSSL7DAMP
, 0xB9843211);
989 mtdcri(SDR0
, PESDR1_460SX_HSSL0DAMP
, 0xB9843211);
990 mtdcri(SDR0
, PESDR1_460SX_HSSL1DAMP
, 0xB9843211);
991 mtdcri(SDR0
, PESDR1_460SX_HSSL2DAMP
, 0xB9843211);
992 mtdcri(SDR0
, PESDR1_460SX_HSSL3DAMP
, 0xB9843211);
994 mtdcri(SDR0
, PESDR2_460SX_HSSL0DAMP
, 0xB9843211);
995 mtdcri(SDR0
, PESDR2_460SX_HSSL1DAMP
, 0xB9843211);
996 mtdcri(SDR0
, PESDR2_460SX_HSSL2DAMP
, 0xB9843211);
997 mtdcri(SDR0
, PESDR2_460SX_HSSL3DAMP
, 0xB9843211);
999 /* HSS TX pre-emphasis */
1000 mtdcri(SDR0
, PESDR0_460SX_HSSL0COEFA
, 0xDCB98987);
1001 mtdcri(SDR0
, PESDR0_460SX_HSSL1COEFA
, 0xDCB98987);
1002 mtdcri(SDR0
, PESDR0_460SX_HSSL2COEFA
, 0xDCB98987);
1003 mtdcri(SDR0
, PESDR0_460SX_HSSL3COEFA
, 0xDCB98987);
1004 mtdcri(SDR0
, PESDR0_460SX_HSSL4COEFA
, 0xDCB98987);
1005 mtdcri(SDR0
, PESDR0_460SX_HSSL5COEFA
, 0xDCB98987);
1006 mtdcri(SDR0
, PESDR0_460SX_HSSL6COEFA
, 0xDCB98987);
1007 mtdcri(SDR0
, PESDR0_460SX_HSSL7COEFA
, 0xDCB98987);
1009 mtdcri(SDR0
, PESDR1_460SX_HSSL0COEFA
, 0xDCB98987);
1010 mtdcri(SDR0
, PESDR1_460SX_HSSL1COEFA
, 0xDCB98987);
1011 mtdcri(SDR0
, PESDR1_460SX_HSSL2COEFA
, 0xDCB98987);
1012 mtdcri(SDR0
, PESDR1_460SX_HSSL3COEFA
, 0xDCB98987);
1014 mtdcri(SDR0
, PESDR2_460SX_HSSL0COEFA
, 0xDCB98987);
1015 mtdcri(SDR0
, PESDR2_460SX_HSSL1COEFA
, 0xDCB98987);
1016 mtdcri(SDR0
, PESDR2_460SX_HSSL2COEFA
, 0xDCB98987);
1017 mtdcri(SDR0
, PESDR2_460SX_HSSL3COEFA
, 0xDCB98987);
1019 /* HSS TX calibration control */
1020 mtdcri(SDR0
, PESDR0_460SX_HSSL1CALDRV
, 0x22222222);
1021 mtdcri(SDR0
, PESDR1_460SX_HSSL1CALDRV
, 0x22220000);
1022 mtdcri(SDR0
, PESDR2_460SX_HSSL1CALDRV
, 0x22220000);
1024 /* HSS TX slew control */
1025 mtdcri(SDR0
, PESDR0_460SX_HSSSLEW
, 0xFFFFFFFF);
1026 mtdcri(SDR0
, PESDR1_460SX_HSSSLEW
, 0xFFFF0000);
1027 mtdcri(SDR0
, PESDR2_460SX_HSSSLEW
, 0xFFFF0000);
1031 /* De-assert PLLRESET */
1032 dcri_clrset(SDR0
, PESDR0_PLLLCT2
, 0x00000100, 0);
1034 /* Reset DL, UTL, GPL before configuration */
1035 mtdcri(SDR0
, PESDR0_460SX_RCSSET
,
1036 PESDRx_RCSSET_RSTDL
| PESDRx_RCSSET_RSTGU
);
1037 mtdcri(SDR0
, PESDR1_460SX_RCSSET
,
1038 PESDRx_RCSSET_RSTDL
| PESDRx_RCSSET_RSTGU
);
1039 mtdcri(SDR0
, PESDR2_460SX_RCSSET
,
1040 PESDRx_RCSSET_RSTDL
| PESDRx_RCSSET_RSTGU
);
1045 * If bifurcation is not enabled, u-boot would have disabled the
1048 if (((mfdcri(SDR0
, PESDR1_460SX_HSSCTLSET
) & 0x00000001) ==
1050 printk(KERN_INFO
"PCI: PCIE bifurcation setup successfully.\n");
1051 printk(KERN_INFO
"PCI: Total 3 PCIE ports are present\n");
1055 printk(KERN_INFO
"PCI: Total 2 PCIE ports are present\n");
1059 static int ppc460sx_pciex_init_port_hw(struct ppc4xx_pciex_port
*port
)
1063 dcri_clrset(SDR0
, port
->sdr_base
+ PESDRn_UTLSET2
,
1066 dcri_clrset(SDR0
, port
->sdr_base
+ PESDRn_UTLSET2
,
1070 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_460SX_RCEI
, 0x08000000);
1072 dcri_clrset(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
,
1073 (PESDRx_RCSSET_RSTGU
| PESDRx_RCSSET_RSTDL
),
1074 PESDRx_RCSSET_RSTPYN
);
1076 port
->has_ibpre
= 1;
1081 static int ppc460sx_pciex_init_utl(struct ppc4xx_pciex_port
*port
)
1084 out_be32 (port
->utl_base
+ PEUTL_PBBSZ
, 0x00000000);
1088 static struct ppc4xx_pciex_hwops ppc460sx_pcie_hwops __initdata
= {
1089 .core_init
= ppc460sx_pciex_core_init
,
1090 .port_init_hw
= ppc460sx_pciex_init_port_hw
,
1091 .setup_utl
= ppc460sx_pciex_init_utl
,
1094 #endif /* CONFIG_44x */
1098 static int __init
ppc405ex_pciex_core_init(struct device_node
*np
)
1100 /* Nothing to do, return 2 ports */
1104 static void ppc405ex_pcie_phy_reset(struct ppc4xx_pciex_port
*port
)
1106 /* Assert the PE0_PHY reset */
1107 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
, 0x01010000);
1110 /* deassert the PE0_hotreset */
1112 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
, 0x01111000);
1114 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
, 0x01101000);
1116 /* poll for phy !reset */
1117 /* XXX FIXME add timeout */
1118 while (!(mfdcri(SDR0
, port
->sdr_base
+ PESDRn_405EX_PHYSTA
) & 0x00001000))
1121 /* deassert the PE0_gpl_utl_reset */
1122 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
, 0x00101000);
1125 static int ppc405ex_pciex_init_port_hw(struct ppc4xx_pciex_port
*port
)
1130 val
= PTYPE_LEGACY_ENDPOINT
;
1132 val
= PTYPE_ROOT_PORT
;
1134 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_DLPSET
,
1135 1 << 24 | val
<< 20 | LNKW_X1
<< 12);
1137 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_UTLSET1
, 0x00000000);
1138 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_UTLSET2
, 0x01010000);
1139 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_405EX_PHYSET1
, 0x720F0000);
1140 mtdcri(SDR0
, port
->sdr_base
+ PESDRn_405EX_PHYSET2
, 0x70600003);
1143 * Only reset the PHY when no link is currently established.
1144 * This is for the Atheros PCIe board which has problems to establish
1145 * the link (again) after this PHY reset. All other currently tested
1146 * PCIe boards don't show this problem.
1147 * This has to be re-tested and fixed in a later release!
1149 val
= mfdcri(SDR0
, port
->sdr_base
+ PESDRn_LOOP
);
1150 if (!(val
& 0x00001000))
1151 ppc405ex_pcie_phy_reset(port
);
1153 dcr_write(port
->dcrs
, DCRO_PEGPL_CFG
, 0x10000000); /* guarded on */
1155 port
->has_ibpre
= 1;
1160 static int ppc405ex_pciex_init_utl(struct ppc4xx_pciex_port
*port
)
1162 dcr_write(port
->dcrs
, DCRO_PEGPL_SPECIAL
, 0x0);
1165 * Set buffer allocations and then assert VRB and TXE.
1167 out_be32(port
->utl_base
+ PEUTL_OUTTR
, 0x02000000);
1168 out_be32(port
->utl_base
+ PEUTL_INTR
, 0x02000000);
1169 out_be32(port
->utl_base
+ PEUTL_OPDBSZ
, 0x04000000);
1170 out_be32(port
->utl_base
+ PEUTL_PBBSZ
, 0x21000000);
1171 out_be32(port
->utl_base
+ PEUTL_IPHBSZ
, 0x02000000);
1172 out_be32(port
->utl_base
+ PEUTL_IPDBSZ
, 0x04000000);
1173 out_be32(port
->utl_base
+ PEUTL_RCIRQEN
, 0x00f00000);
1174 out_be32(port
->utl_base
+ PEUTL_PCTL
, 0x80800066);
1176 out_be32(port
->utl_base
+ PEUTL_PBCTL
, 0x08000000);
1181 static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata
=
1183 .core_init
= ppc405ex_pciex_core_init
,
1184 .port_init_hw
= ppc405ex_pciex_init_port_hw
,
1185 .setup_utl
= ppc405ex_pciex_init_utl
,
1188 #endif /* CONFIG_40x */
1191 /* Check that the core has been initied and if not, do it */
1192 static int __init
ppc4xx_pciex_check_core_init(struct device_node
*np
)
1194 static int core_init
;
1195 int count
= -ENODEV
;
1201 if (of_device_is_compatible(np
, "ibm,plb-pciex-440spe")) {
1202 if (ppc440spe_revA())
1203 ppc4xx_pciex_hwops
= &ppc440speA_pcie_hwops
;
1205 ppc4xx_pciex_hwops
= &ppc440speB_pcie_hwops
;
1207 if (of_device_is_compatible(np
, "ibm,plb-pciex-460ex"))
1208 ppc4xx_pciex_hwops
= &ppc460ex_pcie_hwops
;
1209 if (of_device_is_compatible(np
, "ibm,plb-pciex-460sx"))
1210 ppc4xx_pciex_hwops
= &ppc460sx_pcie_hwops
;
1211 #endif /* CONFIG_44x */
1213 if (of_device_is_compatible(np
, "ibm,plb-pciex-405ex"))
1214 ppc4xx_pciex_hwops
= &ppc405ex_pcie_hwops
;
1216 if (ppc4xx_pciex_hwops
== NULL
) {
1217 printk(KERN_WARNING
"PCIE: unknown host type %s\n",
1222 count
= ppc4xx_pciex_hwops
->core_init(np
);
1224 ppc4xx_pciex_ports
=
1225 kzalloc(count
* sizeof(struct ppc4xx_pciex_port
),
1227 if (ppc4xx_pciex_ports
) {
1228 ppc4xx_pciex_port_count
= count
;
1231 printk(KERN_WARNING
"PCIE: failed to allocate ports array\n");
1237 static void __init
ppc4xx_pciex_port_init_mapping(struct ppc4xx_pciex_port
*port
)
1239 /* We map PCI Express configuration based on the reg property */
1240 dcr_write(port
->dcrs
, DCRO_PEGPL_CFGBAH
,
1241 RES_TO_U32_HIGH(port
->cfg_space
.start
));
1242 dcr_write(port
->dcrs
, DCRO_PEGPL_CFGBAL
,
1243 RES_TO_U32_LOW(port
->cfg_space
.start
));
1245 /* XXX FIXME: Use size from reg property. For now, map 512M */
1246 dcr_write(port
->dcrs
, DCRO_PEGPL_CFGMSK
, 0xe0000001);
1248 /* We map UTL registers based on the reg property */
1249 dcr_write(port
->dcrs
, DCRO_PEGPL_REGBAH
,
1250 RES_TO_U32_HIGH(port
->utl_regs
.start
));
1251 dcr_write(port
->dcrs
, DCRO_PEGPL_REGBAL
,
1252 RES_TO_U32_LOW(port
->utl_regs
.start
));
1254 /* XXX FIXME: Use size from reg property */
1255 dcr_write(port
->dcrs
, DCRO_PEGPL_REGMSK
, 0x00007001);
1257 /* Disable all other outbound windows */
1258 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR1MSKL
, 0);
1259 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR2MSKL
, 0);
1260 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR3MSKL
, 0);
1261 dcr_write(port
->dcrs
, DCRO_PEGPL_MSGMSK
, 0);
1264 static int __init
ppc4xx_pciex_wait_on_sdr(struct ppc4xx_pciex_port
*port
,
1265 unsigned int sdr_offset
,
1272 while(timeout_ms
--) {
1273 val
= mfdcri(SDR0
, port
->sdr_base
+ sdr_offset
);
1274 if ((val
& mask
) == value
) {
1275 pr_debug("PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
1276 port
->index
, sdr_offset
, timeout_ms
, val
);
1284 static int __init
ppc4xx_pciex_port_init(struct ppc4xx_pciex_port
*port
)
1289 if (ppc4xx_pciex_hwops
->port_init_hw
)
1290 rc
= ppc4xx_pciex_hwops
->port_init_hw(port
);
1294 printk(KERN_INFO
"PCIE%d: Checking link...\n",
1297 /* Wait for reset to complete */
1298 if (ppc4xx_pciex_wait_on_sdr(port
, PESDRn_RCSSTS
, 1 << 20, 0, 10)) {
1299 printk(KERN_WARNING
"PCIE%d: PGRST failed\n",
1304 /* Check for card presence detect if supported, if not, just wait for
1305 * link unconditionally.
1307 * note that we don't fail if there is no link, we just filter out
1308 * config space accesses. That way, it will be easier to implement
1311 if (!port
->has_ibpre
||
1312 !ppc4xx_pciex_wait_on_sdr(port
, PESDRn_LOOP
,
1313 1 << 28, 1 << 28, 100)) {
1315 "PCIE%d: Device detected, waiting for link...\n",
1317 if (ppc4xx_pciex_wait_on_sdr(port
, PESDRn_LOOP
,
1318 0x1000, 0x1000, 2000))
1320 "PCIE%d: Link up failed\n", port
->index
);
1323 "PCIE%d: link is up !\n", port
->index
);
1327 printk(KERN_INFO
"PCIE%d: No device detected.\n", port
->index
);
1330 * Initialize mapping: disable all regions and configure
1331 * CFG and REG regions based on resources in the device tree
1333 ppc4xx_pciex_port_init_mapping(port
);
1338 port
->utl_base
= ioremap(port
->utl_regs
.start
, 0x100);
1339 BUG_ON(port
->utl_base
== NULL
);
1342 * Setup UTL registers --BenH.
1344 if (ppc4xx_pciex_hwops
->setup_utl
)
1345 ppc4xx_pciex_hwops
->setup_utl(port
);
1348 * Check for VC0 active and assert RDY.
1351 ppc4xx_pciex_wait_on_sdr(port
, PESDRn_RCSSTS
,
1352 1 << 16, 1 << 16, 5000)) {
1353 printk(KERN_INFO
"PCIE%d: VC0 not active\n", port
->index
);
1357 dcri_clrset(SDR0
, port
->sdr_base
+ PESDRn_RCSSET
, 0, 1 << 20);
1363 static int ppc4xx_pciex_validate_bdf(struct ppc4xx_pciex_port
*port
,
1364 struct pci_bus
*bus
,
1369 /* Endpoint can not generate upstream(remote) config cycles */
1370 if (port
->endpoint
&& bus
->number
!= port
->hose
->first_busno
)
1371 return PCIBIOS_DEVICE_NOT_FOUND
;
1373 /* Check we are within the mapped range */
1374 if (bus
->number
> port
->hose
->last_busno
) {
1376 printk(KERN_WARNING
"Warning! Probing bus %u"
1377 " out of range !\n", bus
->number
);
1380 return PCIBIOS_DEVICE_NOT_FOUND
;
1383 /* The root complex has only one device / function */
1384 if (bus
->number
== port
->hose
->first_busno
&& devfn
!= 0)
1385 return PCIBIOS_DEVICE_NOT_FOUND
;
1387 /* The other side of the RC has only one device as well */
1388 if (bus
->number
== (port
->hose
->first_busno
+ 1) &&
1389 PCI_SLOT(devfn
) != 0)
1390 return PCIBIOS_DEVICE_NOT_FOUND
;
1392 /* Check if we have a link */
1393 if ((bus
->number
!= port
->hose
->first_busno
) && !port
->link
)
1394 return PCIBIOS_DEVICE_NOT_FOUND
;
1399 static void __iomem
*ppc4xx_pciex_get_config_base(struct ppc4xx_pciex_port
*port
,
1400 struct pci_bus
*bus
,
1405 /* Remove the casts when we finally remove the stupid volatile
1406 * in struct pci_controller
1408 if (bus
->number
== port
->hose
->first_busno
)
1409 return (void __iomem
*)port
->hose
->cfg_addr
;
1411 relbus
= bus
->number
- (port
->hose
->first_busno
+ 1);
1412 return (void __iomem
*)port
->hose
->cfg_data
+
1413 ((relbus
<< 20) | (devfn
<< 12));
1416 static int ppc4xx_pciex_read_config(struct pci_bus
*bus
, unsigned int devfn
,
1417 int offset
, int len
, u32
*val
)
1419 struct pci_controller
*hose
= pci_bus_to_host(bus
);
1420 struct ppc4xx_pciex_port
*port
=
1421 &ppc4xx_pciex_ports
[hose
->indirect_type
];
1425 BUG_ON(hose
!= port
->hose
);
1427 if (ppc4xx_pciex_validate_bdf(port
, bus
, devfn
) != 0)
1428 return PCIBIOS_DEVICE_NOT_FOUND
;
1430 addr
= ppc4xx_pciex_get_config_base(port
, bus
, devfn
);
1433 * Reading from configuration space of non-existing device can
1434 * generate transaction errors. For the read duration we suppress
1435 * assertion of machine check exceptions to avoid those.
1437 gpl_cfg
= dcr_read(port
->dcrs
, DCRO_PEGPL_CFG
);
1438 dcr_write(port
->dcrs
, DCRO_PEGPL_CFG
, gpl_cfg
| GPL_DMER_MASK_DISA
);
1440 /* Make sure no CRS is recorded */
1441 out_be32(port
->utl_base
+ PEUTL_RCSTA
, 0x00040000);
1445 *val
= in_8((u8
*)(addr
+ offset
));
1448 *val
= in_le16((u16
*)(addr
+ offset
));
1451 *val
= in_le32((u32
*)(addr
+ offset
));
1455 pr_debug("pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
1456 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1457 bus
->number
, hose
->first_busno
, hose
->last_busno
,
1458 devfn
, offset
, len
, addr
+ offset
, *val
);
1460 /* Check for CRS (440SPe rev B does that for us but heh ..) */
1461 if (in_be32(port
->utl_base
+ PEUTL_RCSTA
) & 0x00040000) {
1462 pr_debug("Got CRS !\n");
1463 if (len
!= 4 || offset
!= 0)
1464 return PCIBIOS_DEVICE_NOT_FOUND
;
1468 dcr_write(port
->dcrs
, DCRO_PEGPL_CFG
, gpl_cfg
);
1470 return PCIBIOS_SUCCESSFUL
;
1473 static int ppc4xx_pciex_write_config(struct pci_bus
*bus
, unsigned int devfn
,
1474 int offset
, int len
, u32 val
)
1476 struct pci_controller
*hose
= pci_bus_to_host(bus
);
1477 struct ppc4xx_pciex_port
*port
=
1478 &ppc4xx_pciex_ports
[hose
->indirect_type
];
1482 if (ppc4xx_pciex_validate_bdf(port
, bus
, devfn
) != 0)
1483 return PCIBIOS_DEVICE_NOT_FOUND
;
1485 addr
= ppc4xx_pciex_get_config_base(port
, bus
, devfn
);
1488 * Reading from configuration space of non-existing device can
1489 * generate transaction errors. For the read duration we suppress
1490 * assertion of machine check exceptions to avoid those.
1492 gpl_cfg
= dcr_read(port
->dcrs
, DCRO_PEGPL_CFG
);
1493 dcr_write(port
->dcrs
, DCRO_PEGPL_CFG
, gpl_cfg
| GPL_DMER_MASK_DISA
);
1495 pr_debug("pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
1496 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1497 bus
->number
, hose
->first_busno
, hose
->last_busno
,
1498 devfn
, offset
, len
, addr
+ offset
, val
);
1502 out_8((u8
*)(addr
+ offset
), val
);
1505 out_le16((u16
*)(addr
+ offset
), val
);
1508 out_le32((u32
*)(addr
+ offset
), val
);
1512 dcr_write(port
->dcrs
, DCRO_PEGPL_CFG
, gpl_cfg
);
1514 return PCIBIOS_SUCCESSFUL
;
1517 static struct pci_ops ppc4xx_pciex_pci_ops
=
1519 .read
= ppc4xx_pciex_read_config
,
1520 .write
= ppc4xx_pciex_write_config
,
1523 static int __init
ppc4xx_setup_one_pciex_POM(struct ppc4xx_pciex_port
*port
,
1524 struct pci_controller
*hose
,
1525 void __iomem
*mbase
,
1532 u32 lah
, lal
, pciah
, pcial
, sa
;
1534 if (!is_power_of_2(size
) ||
1535 (index
< 2 && size
< 0x100000) ||
1536 (index
== 2 && size
< 0x100) ||
1537 (plb_addr
& (size
- 1)) != 0) {
1538 printk(KERN_WARNING
"%s: Resource out of range\n",
1539 hose
->dn
->full_name
);
1543 /* Calculate register values */
1544 lah
= RES_TO_U32_HIGH(plb_addr
);
1545 lal
= RES_TO_U32_LOW(plb_addr
);
1546 pciah
= RES_TO_U32_HIGH(pci_addr
);
1547 pcial
= RES_TO_U32_LOW(pci_addr
);
1548 sa
= (0xffffffffu
<< ilog2(size
)) | 0x1;
1550 /* Program register values */
1553 out_le32(mbase
+ PECFG_POM0LAH
, pciah
);
1554 out_le32(mbase
+ PECFG_POM0LAL
, pcial
);
1555 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR1BAH
, lah
);
1556 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR1BAL
, lal
);
1557 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR1MSKH
, 0x7fffffff);
1558 /* Note that 3 here means enabled | single region */
1559 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR1MSKL
, sa
| 3);
1562 out_le32(mbase
+ PECFG_POM1LAH
, pciah
);
1563 out_le32(mbase
+ PECFG_POM1LAL
, pcial
);
1564 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR2BAH
, lah
);
1565 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR2BAL
, lal
);
1566 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR2MSKH
, 0x7fffffff);
1567 /* Note that 3 here means enabled | single region */
1568 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR2MSKL
, sa
| 3);
1571 out_le32(mbase
+ PECFG_POM2LAH
, pciah
);
1572 out_le32(mbase
+ PECFG_POM2LAL
, pcial
);
1573 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR3BAH
, lah
);
1574 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR3BAL
, lal
);
1575 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR3MSKH
, 0x7fffffff);
1576 /* Note that 3 here means enabled | IO space !!! */
1577 dcr_write(port
->dcrs
, DCRO_PEGPL_OMR3MSKL
, sa
| 3);
1584 static void __init
ppc4xx_configure_pciex_POMs(struct ppc4xx_pciex_port
*port
,
1585 struct pci_controller
*hose
,
1586 void __iomem
*mbase
)
1588 int i
, j
, found_isa_hole
= 0;
1590 /* Setup outbound memory windows */
1591 for (i
= j
= 0; i
< 3; i
++) {
1592 struct resource
*res
= &hose
->mem_resources
[i
];
1594 /* we only care about memory windows */
1595 if (!(res
->flags
& IORESOURCE_MEM
))
1598 printk(KERN_WARNING
"%s: Too many ranges\n",
1599 port
->node
->full_name
);
1603 /* Configure the resource */
1604 if (ppc4xx_setup_one_pciex_POM(port
, hose
, mbase
,
1606 res
->start
- hose
->pci_mem_offset
,
1607 res
->end
+ 1 - res
->start
,
1612 /* If the resource PCI address is 0 then we have our
1615 if (res
->start
== hose
->pci_mem_offset
)
1620 /* Handle ISA memory hole if not already covered */
1621 if (j
<= 1 && !found_isa_hole
&& hose
->isa_mem_size
)
1622 if (ppc4xx_setup_one_pciex_POM(port
, hose
, mbase
,
1623 hose
->isa_mem_phys
, 0,
1624 hose
->isa_mem_size
, 0, j
) == 0)
1625 printk(KERN_INFO
"%s: Legacy ISA memory support enabled\n",
1626 hose
->dn
->full_name
);
1628 /* Configure IO, always 64K starting at 0. We hard wire it to 64K !
1629 * Note also that it -has- to be region index 2 on this HW
1631 if (hose
->io_resource
.flags
& IORESOURCE_IO
)
1632 ppc4xx_setup_one_pciex_POM(port
, hose
, mbase
,
1633 hose
->io_base_phys
, 0,
1634 0x10000, IORESOURCE_IO
, 2);
1637 static void __init
ppc4xx_configure_pciex_PIMs(struct ppc4xx_pciex_port
*port
,
1638 struct pci_controller
*hose
,
1639 void __iomem
*mbase
,
1640 struct resource
*res
)
1642 resource_size_t size
= res
->end
- res
->start
+ 1;
1645 if (port
->endpoint
) {
1646 resource_size_t ep_addr
= 0;
1647 resource_size_t ep_size
= 32 << 20;
1649 /* Currently we map a fixed 64MByte window to PLB address
1650 * 0 (SDRAM). This should probably be configurable via a dts
1654 /* Calculate window size */
1655 sa
= (0xffffffffffffffffull
<< ilog2(ep_size
));
1658 out_le32(mbase
+ PECFG_BAR0HMPA
, RES_TO_U32_HIGH(sa
));
1659 out_le32(mbase
+ PECFG_BAR0LMPA
, RES_TO_U32_LOW(sa
) |
1660 PCI_BASE_ADDRESS_MEM_TYPE_64
);
1662 /* Disable BAR1 & BAR2 */
1663 out_le32(mbase
+ PECFG_BAR1MPA
, 0);
1664 out_le32(mbase
+ PECFG_BAR2HMPA
, 0);
1665 out_le32(mbase
+ PECFG_BAR2LMPA
, 0);
1667 out_le32(mbase
+ PECFG_PIM01SAH
, RES_TO_U32_HIGH(sa
));
1668 out_le32(mbase
+ PECFG_PIM01SAL
, RES_TO_U32_LOW(sa
));
1670 out_le32(mbase
+ PCI_BASE_ADDRESS_0
, RES_TO_U32_LOW(ep_addr
));
1671 out_le32(mbase
+ PCI_BASE_ADDRESS_1
, RES_TO_U32_HIGH(ep_addr
));
1673 /* Calculate window size */
1674 sa
= (0xffffffffffffffffull
<< ilog2(size
));
1675 if (res
->flags
& IORESOURCE_PREFETCH
)
1678 out_le32(mbase
+ PECFG_BAR0HMPA
, RES_TO_U32_HIGH(sa
));
1679 out_le32(mbase
+ PECFG_BAR0LMPA
, RES_TO_U32_LOW(sa
));
1681 /* The setup of the split looks weird to me ... let's see
1684 out_le32(mbase
+ PECFG_PIM0LAL
, 0x00000000);
1685 out_le32(mbase
+ PECFG_PIM0LAH
, 0x00000000);
1686 out_le32(mbase
+ PECFG_PIM1LAL
, 0x00000000);
1687 out_le32(mbase
+ PECFG_PIM1LAH
, 0x00000000);
1688 out_le32(mbase
+ PECFG_PIM01SAH
, 0xffff0000);
1689 out_le32(mbase
+ PECFG_PIM01SAL
, 0x00000000);
1691 out_le32(mbase
+ PCI_BASE_ADDRESS_0
, RES_TO_U32_LOW(res
->start
));
1692 out_le32(mbase
+ PCI_BASE_ADDRESS_1
, RES_TO_U32_HIGH(res
->start
));
1695 /* Enable inbound mapping */
1696 out_le32(mbase
+ PECFG_PIMEN
, 0x1);
1698 /* Enable I/O, Mem, and Busmaster cycles */
1699 out_le16(mbase
+ PCI_COMMAND
,
1700 in_le16(mbase
+ PCI_COMMAND
) |
1701 PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
1704 static void __init
ppc4xx_pciex_port_setup_hose(struct ppc4xx_pciex_port
*port
)
1706 struct resource dma_window
;
1707 struct pci_controller
*hose
= NULL
;
1708 const int *bus_range
;
1709 int primary
= 0, busses
;
1710 void __iomem
*mbase
= NULL
, *cfg_data
= NULL
;
1714 /* Check if primary bridge */
1715 if (of_get_property(port
->node
, "primary", NULL
))
1718 /* Get bus range if any */
1719 bus_range
= of_get_property(port
->node
, "bus-range", NULL
);
1721 /* Allocate the host controller data structure */
1722 hose
= pcibios_alloc_controller(port
->node
);
1726 /* We stick the port number in "indirect_type" so the config space
1727 * ops can retrieve the port data structure easily
1729 hose
->indirect_type
= port
->index
;
1732 hose
->first_busno
= bus_range
? bus_range
[0] : 0x0;
1733 hose
->last_busno
= bus_range
? bus_range
[1] : 0xff;
1735 /* Because of how big mapping the config space is (1M per bus), we
1736 * limit how many busses we support. In the long run, we could replace
1737 * that with something akin to kmap_atomic instead. We set aside 1 bus
1738 * for the host itself too.
1740 busses
= hose
->last_busno
- hose
->first_busno
; /* This is off by 1 */
1741 if (busses
> MAX_PCIE_BUS_MAPPED
) {
1742 busses
= MAX_PCIE_BUS_MAPPED
;
1743 hose
->last_busno
= hose
->first_busno
+ busses
;
1746 if (!port
->endpoint
) {
1747 /* Only map the external config space in cfg_data for
1748 * PCIe root-complexes. External space is 1M per bus
1750 cfg_data
= ioremap(port
->cfg_space
.start
+
1751 (hose
->first_busno
+ 1) * 0x100000,
1753 if (cfg_data
== NULL
) {
1754 printk(KERN_ERR
"%s: Can't map external config space !",
1755 port
->node
->full_name
);
1758 hose
->cfg_data
= cfg_data
;
1761 /* Always map the host config space in cfg_addr.
1762 * Internal space is 4K
1764 mbase
= ioremap(port
->cfg_space
.start
+ 0x10000000, 0x1000);
1765 if (mbase
== NULL
) {
1766 printk(KERN_ERR
"%s: Can't map internal config space !",
1767 port
->node
->full_name
);
1770 hose
->cfg_addr
= mbase
;
1772 pr_debug("PCIE %s, bus %d..%d\n", port
->node
->full_name
,
1773 hose
->first_busno
, hose
->last_busno
);
1774 pr_debug(" config space mapped at: root @0x%p, other @0x%p\n",
1775 hose
->cfg_addr
, hose
->cfg_data
);
1777 /* Setup config space */
1778 hose
->ops
= &ppc4xx_pciex_pci_ops
;
1780 mbase
= (void __iomem
*)hose
->cfg_addr
;
1782 if (!port
->endpoint
) {
1784 * Set bus numbers on our root port
1786 out_8(mbase
+ PCI_PRIMARY_BUS
, hose
->first_busno
);
1787 out_8(mbase
+ PCI_SECONDARY_BUS
, hose
->first_busno
+ 1);
1788 out_8(mbase
+ PCI_SUBORDINATE_BUS
, hose
->last_busno
);
1792 * OMRs are already reset, also disable PIMs
1794 out_le32(mbase
+ PECFG_PIMEN
, 0);
1796 /* Parse outbound mapping resources */
1797 pci_process_bridge_OF_ranges(hose
, port
->node
, primary
);
1799 /* Parse inbound mapping resources */
1800 if (ppc4xx_parse_dma_ranges(hose
, mbase
, &dma_window
) != 0)
1803 /* Configure outbound ranges POMs */
1804 ppc4xx_configure_pciex_POMs(port
, hose
, mbase
);
1806 /* Configure inbound ranges PIMs */
1807 ppc4xx_configure_pciex_PIMs(port
, hose
, mbase
, &dma_window
);
1809 /* The root complex doesn't show up if we don't set some vendor
1810 * and device IDs into it. The defaults below are the same bogus
1811 * one that the initial code in arch/ppc had. This can be
1812 * overwritten by setting the "vendor-id/device-id" properties
1813 * in the pciex node.
1816 /* Get the (optional) vendor-/device-id from the device-tree */
1817 pval
= of_get_property(port
->node
, "vendor-id", NULL
);
1821 if (!port
->endpoint
)
1822 val
= 0xaaa0 + port
->index
;
1824 val
= 0xeee0 + port
->index
;
1826 out_le16(mbase
+ 0x200, val
);
1828 pval
= of_get_property(port
->node
, "device-id", NULL
);
1832 if (!port
->endpoint
)
1833 val
= 0xbed0 + port
->index
;
1835 val
= 0xfed0 + port
->index
;
1837 out_le16(mbase
+ 0x202, val
);
1839 if (!port
->endpoint
) {
1840 /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
1841 out_le32(mbase
+ 0x208, 0x06040001);
1843 printk(KERN_INFO
"PCIE%d: successfully set as root-complex\n",
1846 /* Set Class Code to Processor/PPC */
1847 out_le32(mbase
+ 0x208, 0x0b200001);
1849 printk(KERN_INFO
"PCIE%d: successfully set as endpoint\n",
1856 pcibios_free_controller(hose
);
1863 static void __init
ppc4xx_probe_pciex_bridge(struct device_node
*np
)
1865 struct ppc4xx_pciex_port
*port
;
1871 /* First, proceed to core initialization as we assume there's
1872 * only one PCIe core in the system
1874 if (ppc4xx_pciex_check_core_init(np
))
1877 /* Get the port number from the device-tree */
1878 pval
= of_get_property(np
, "port", NULL
);
1880 printk(KERN_ERR
"PCIE: Can't find port number for %s\n",
1885 if (portno
>= ppc4xx_pciex_port_count
) {
1886 printk(KERN_ERR
"PCIE: port number out of range for %s\n",
1890 port
= &ppc4xx_pciex_ports
[portno
];
1891 port
->index
= portno
;
1894 * Check if device is enabled
1896 if (!of_device_is_available(np
)) {
1897 printk(KERN_INFO
"PCIE%d: Port disabled via device-tree\n", port
->index
);
1901 port
->node
= of_node_get(np
);
1902 pval
= of_get_property(np
, "sdr-base", NULL
);
1904 printk(KERN_ERR
"PCIE: missing sdr-base for %s\n",
1908 port
->sdr_base
= *pval
;
1910 /* Check if device_type property is set to "pci" or "pci-endpoint".
1911 * Resulting from this setup this PCIe port will be configured
1912 * as root-complex or as endpoint.
1914 val
= of_get_property(port
->node
, "device_type", NULL
);
1915 if (!strcmp(val
, "pci-endpoint")) {
1917 } else if (!strcmp(val
, "pci")) {
1920 printk(KERN_ERR
"PCIE: missing or incorrect device_type for %s\n",
1925 /* Fetch config space registers address */
1926 if (of_address_to_resource(np
, 0, &port
->cfg_space
)) {
1927 printk(KERN_ERR
"%s: Can't get PCI-E config space !",
1931 /* Fetch host bridge internal registers address */
1932 if (of_address_to_resource(np
, 1, &port
->utl_regs
)) {
1933 printk(KERN_ERR
"%s: Can't get UTL register base !",
1939 dcrs
= dcr_resource_start(np
, 0);
1941 printk(KERN_ERR
"%s: Can't get DCR register base !",
1945 port
->dcrs
= dcr_map(np
, dcrs
, dcr_resource_len(np
, 0));
1947 /* Initialize the port specific registers */
1948 if (ppc4xx_pciex_port_init(port
)) {
1949 printk(KERN_WARNING
"PCIE%d: Port init failed\n", port
->index
);
1953 /* Setup the linux hose data structure */
1954 ppc4xx_pciex_port_setup_hose(port
);
1957 #endif /* CONFIG_PPC4xx_PCI_EXPRESS */
1959 static int __init
ppc4xx_pci_find_bridges(void)
1961 struct device_node
*np
;
1963 ppc_pci_flags
|= PPC_PCI_ENABLE_PROC_DOMAINS
| PPC_PCI_COMPAT_DOMAIN_0
;
1965 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
1966 for_each_compatible_node(np
, NULL
, "ibm,plb-pciex")
1967 ppc4xx_probe_pciex_bridge(np
);
1969 for_each_compatible_node(np
, NULL
, "ibm,plb-pcix")
1970 ppc4xx_probe_pcix_bridge(np
);
1971 for_each_compatible_node(np
, NULL
, "ibm,plb-pci")
1972 ppc4xx_probe_pci_bridge(np
);
1976 arch_initcall(ppc4xx_pci_find_bridges
);