[POWERPC] 4xx: PCI-E Link setup improvements
[cascardo/linux.git] / arch / powerpc / sysdev / ppc4xx_pci.c
1 /*
2  * PCI / PCI-X / PCI-Express support for 4xx parts
3  *
4  * Copyright 2007 Ben. Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
5  *
6  * Most PCI Express code is coming from Stefan Roese implementation for
7  * arch/ppc in the Denx tree, slightly reworked by me.
8  *
9  * Copyright 2007 DENX Software Engineering, Stefan Roese <sr@denx.de>
10  *
11  * Some of that comes itself from a previous implementation for 440SPE only
12  * by Roland Dreier:
13  *
14  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
15  * Roland Dreier <rolandd@cisco.com>
16  *
17  */
18
19 #undef DEBUG
20
21 #include <linux/kernel.h>
22 #include <linux/pci.h>
23 #include <linux/init.h>
24 #include <linux/of.h>
25 #include <linux/bootmem.h>
26 #include <linux/delay.h>
27
28 #include <asm/io.h>
29 #include <asm/pci-bridge.h>
30 #include <asm/machdep.h>
31 #include <asm/dcr.h>
32 #include <asm/dcr-regs.h>
33
34 #include "ppc4xx_pci.h"
35
36 static int dma_offset_set;
37
38 /* Move that to a useable header */
39 extern unsigned long total_memory;
40
41 #define U64_TO_U32_LOW(val)     ((u32)((val) & 0x00000000ffffffffULL))
42 #define U64_TO_U32_HIGH(val)    ((u32)((val) >> 32))
43
44 #ifdef CONFIG_RESOURCES_64BIT
45 #define RES_TO_U32_LOW(val)     U64_TO_U32_LOW(val)
46 #define RES_TO_U32_HIGH(val)    U64_TO_U32_HIGH(val)
47 #else
48 #define RES_TO_U32_LOW(val)     (val)
49 #define RES_TO_U32_HIGH(val)    (0)
50 #endif
51
52 static void fixup_ppc4xx_pci_bridge(struct pci_dev *dev)
53 {
54         struct pci_controller *hose;
55         int i;
56
57         if (dev->devfn != 0 || dev->bus->self != NULL)
58                 return;
59
60         hose = pci_bus_to_host(dev->bus);
61         if (hose == NULL)
62                 return;
63
64         if (!of_device_is_compatible(hose->dn, "ibm,plb-pciex") &&
65             !of_device_is_compatible(hose->dn, "ibm,plb-pcix") &&
66             !of_device_is_compatible(hose->dn, "ibm,plb-pci"))
67                 return;
68
69         /* Hide the PCI host BARs from the kernel as their content doesn't
70          * fit well in the resource management
71          */
72         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
73                 dev->resource[i].start = dev->resource[i].end = 0;
74                 dev->resource[i].flags = 0;
75         }
76
77         printk(KERN_INFO "PCI: Hiding 4xx host bridge resources %s\n",
78                pci_name(dev));
79 }
80 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, fixup_ppc4xx_pci_bridge);
81
82 static int __init ppc4xx_parse_dma_ranges(struct pci_controller *hose,
83                                           void __iomem *reg,
84                                           struct resource *res)
85 {
86         u64 size;
87         const u32 *ranges;
88         int rlen;
89         int pna = of_n_addr_cells(hose->dn);
90         int np = pna + 5;
91
92         /* Default */
93         res->start = 0;
94         res->end = size = 0x80000000;
95         res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
96
97         /* Get dma-ranges property */
98         ranges = of_get_property(hose->dn, "dma-ranges", &rlen);
99         if (ranges == NULL)
100                 goto out;
101
102         /* Walk it */
103         while ((rlen -= np * 4) >= 0) {
104                 u32 pci_space = ranges[0];
105                 u64 pci_addr = of_read_number(ranges + 1, 2);
106                 u64 cpu_addr = of_translate_dma_address(hose->dn, ranges + 3);
107                 size = of_read_number(ranges + pna + 3, 2);
108                 ranges += np;
109                 if (cpu_addr == OF_BAD_ADDR || size == 0)
110                         continue;
111
112                 /* We only care about memory */
113                 if ((pci_space & 0x03000000) != 0x02000000)
114                         continue;
115
116                 /* We currently only support memory at 0, and pci_addr
117                  * within 32 bits space
118                  */
119                 if (cpu_addr != 0 || pci_addr > 0xffffffff) {
120                         printk(KERN_WARNING "%s: Ignored unsupported dma range"
121                                " 0x%016llx...0x%016llx -> 0x%016llx\n",
122                                hose->dn->full_name,
123                                pci_addr, pci_addr + size - 1, cpu_addr);
124                         continue;
125                 }
126
127                 /* Check if not prefetchable */
128                 if (!(pci_space & 0x40000000))
129                         res->flags &= ~IORESOURCE_PREFETCH;
130
131
132                 /* Use that */
133                 res->start = pci_addr;
134 #ifndef CONFIG_RESOURCES_64BIT
135                 /* Beware of 32 bits resources */
136                 if ((pci_addr + size) > 0x100000000ull)
137                         res->end = 0xffffffff;
138                 else
139 #endif
140                         res->end = res->start + size - 1;
141                 break;
142         }
143
144         /* We only support one global DMA offset */
145         if (dma_offset_set && pci_dram_offset != res->start) {
146                 printk(KERN_ERR "%s: dma-ranges(s) mismatch\n",
147                        hose->dn->full_name);
148                 return -ENXIO;
149         }
150
151         /* Check that we can fit all of memory as we don't support
152          * DMA bounce buffers
153          */
154         if (size < total_memory) {
155                 printk(KERN_ERR "%s: dma-ranges too small "
156                        "(size=%llx total_memory=%lx)\n",
157                        hose->dn->full_name, size, total_memory);
158                 return -ENXIO;
159         }
160
161         /* Check we are a power of 2 size and that base is a multiple of size*/
162         if (!is_power_of_2(size) ||
163             (res->start & (size - 1)) != 0) {
164                 printk(KERN_ERR "%s: dma-ranges unaligned\n",
165                        hose->dn->full_name);
166                 return -ENXIO;
167         }
168
169         /* Check that we are fully contained within 32 bits space */
170         if (res->end > 0xffffffff) {
171                 printk(KERN_ERR "%s: dma-ranges outside of 32 bits space\n",
172                        hose->dn->full_name);
173                 return -ENXIO;
174         }
175  out:
176         dma_offset_set = 1;
177         pci_dram_offset = res->start;
178
179         printk(KERN_INFO "4xx PCI DMA offset set to 0x%08lx\n",
180                pci_dram_offset);
181         return 0;
182 }
183
184 /*
185  * 4xx PCI 2.x part
186  */
187
188 static void __init ppc4xx_configure_pci_PMMs(struct pci_controller *hose,
189                                              void __iomem *reg)
190 {
191         u32 la, ma, pcila, pciha;
192         int i, j;
193
194         /* Setup outbound memory windows */
195         for (i = j = 0; i < 3; i++) {
196                 struct resource *res = &hose->mem_resources[i];
197
198                 /* we only care about memory windows */
199                 if (!(res->flags & IORESOURCE_MEM))
200                         continue;
201                 if (j > 2) {
202                         printk(KERN_WARNING "%s: Too many ranges\n",
203                                hose->dn->full_name);
204                         break;
205                 }
206
207                 /* Calculate register values */
208                 la = res->start;
209                 pciha = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
210                 pcila = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
211
212                 ma = res->end + 1 - res->start;
213                 if (!is_power_of_2(ma) || ma < 0x1000 || ma > 0xffffffffu) {
214                         printk(KERN_WARNING "%s: Resource out of range\n",
215                                hose->dn->full_name);
216                         continue;
217                 }
218                 ma = (0xffffffffu << ilog2(ma)) | 0x1;
219                 if (res->flags & IORESOURCE_PREFETCH)
220                         ma |= 0x2;
221
222                 /* Program register values */
223                 writel(la, reg + PCIL0_PMM0LA + (0x10 * j));
224                 writel(pcila, reg + PCIL0_PMM0PCILA + (0x10 * j));
225                 writel(pciha, reg + PCIL0_PMM0PCIHA + (0x10 * j));
226                 writel(ma, reg + PCIL0_PMM0MA + (0x10 * j));
227                 j++;
228         }
229 }
230
231 static void __init ppc4xx_configure_pci_PTMs(struct pci_controller *hose,
232                                              void __iomem *reg,
233                                              const struct resource *res)
234 {
235         resource_size_t size = res->end - res->start + 1;
236         u32 sa;
237
238         /* Calculate window size */
239         sa = (0xffffffffu << ilog2(size)) | 1;
240         sa |= 0x1;
241
242         /* RAM is always at 0 local for now */
243         writel(0, reg + PCIL0_PTM1LA);
244         writel(sa, reg + PCIL0_PTM1MS);
245
246         /* Map on PCI side */
247         early_write_config_dword(hose, hose->first_busno, 0,
248                                  PCI_BASE_ADDRESS_1, res->start);
249         early_write_config_dword(hose, hose->first_busno, 0,
250                                  PCI_BASE_ADDRESS_2, 0x00000000);
251         early_write_config_word(hose, hose->first_busno, 0,
252                                 PCI_COMMAND, 0x0006);
253 }
254
255 static void __init ppc4xx_probe_pci_bridge(struct device_node *np)
256 {
257         /* NYI */
258         struct resource rsrc_cfg;
259         struct resource rsrc_reg;
260         struct resource dma_window;
261         struct pci_controller *hose = NULL;
262         void __iomem *reg = NULL;
263         const int *bus_range;
264         int primary = 0;
265
266         /* Fetch config space registers address */
267         if (of_address_to_resource(np, 0, &rsrc_cfg)) {
268                 printk(KERN_ERR "%s:Can't get PCI config register base !",
269                        np->full_name);
270                 return;
271         }
272         /* Fetch host bridge internal registers address */
273         if (of_address_to_resource(np, 3, &rsrc_reg)) {
274                 printk(KERN_ERR "%s: Can't get PCI internal register base !",
275                        np->full_name);
276                 return;
277         }
278
279         /* Check if primary bridge */
280         if (of_get_property(np, "primary", NULL))
281                 primary = 1;
282
283         /* Get bus range if any */
284         bus_range = of_get_property(np, "bus-range", NULL);
285
286         /* Map registers */
287         reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
288         if (reg == NULL) {
289                 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
290                 goto fail;
291         }
292
293         /* Allocate the host controller data structure */
294         hose = pcibios_alloc_controller(np);
295         if (!hose)
296                 goto fail;
297
298         hose->first_busno = bus_range ? bus_range[0] : 0x0;
299         hose->last_busno = bus_range ? bus_range[1] : 0xff;
300
301         /* Setup config space */
302         setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
303
304         /* Disable all windows */
305         writel(0, reg + PCIL0_PMM0MA);
306         writel(0, reg + PCIL0_PMM1MA);
307         writel(0, reg + PCIL0_PMM2MA);
308         writel(0, reg + PCIL0_PTM1MS);
309         writel(0, reg + PCIL0_PTM2MS);
310
311         /* Parse outbound mapping resources */
312         pci_process_bridge_OF_ranges(hose, np, primary);
313
314         /* Parse inbound mapping resources */
315         if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
316                 goto fail;
317
318         /* Configure outbound ranges POMs */
319         ppc4xx_configure_pci_PMMs(hose, reg);
320
321         /* Configure inbound ranges PIMs */
322         ppc4xx_configure_pci_PTMs(hose, reg, &dma_window);
323
324         /* We don't need the registers anymore */
325         iounmap(reg);
326         return;
327
328  fail:
329         if (hose)
330                 pcibios_free_controller(hose);
331         if (reg)
332                 iounmap(reg);
333 }
334
335 /*
336  * 4xx PCI-X part
337  */
338
339 static void __init ppc4xx_configure_pcix_POMs(struct pci_controller *hose,
340                                               void __iomem *reg)
341 {
342         u32 lah, lal, pciah, pcial, sa;
343         int i, j;
344
345         /* Setup outbound memory windows */
346         for (i = j = 0; i < 3; i++) {
347                 struct resource *res = &hose->mem_resources[i];
348
349                 /* we only care about memory windows */
350                 if (!(res->flags & IORESOURCE_MEM))
351                         continue;
352                 if (j > 1) {
353                         printk(KERN_WARNING "%s: Too many ranges\n",
354                                hose->dn->full_name);
355                         break;
356                 }
357
358                 /* Calculate register values */
359                 lah = RES_TO_U32_HIGH(res->start);
360                 lal = RES_TO_U32_LOW(res->start);
361                 pciah = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
362                 pcial = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
363                 sa = res->end + 1 - res->start;
364                 if (!is_power_of_2(sa) || sa < 0x100000 ||
365                     sa > 0xffffffffu) {
366                         printk(KERN_WARNING "%s: Resource out of range\n",
367                                hose->dn->full_name);
368                         continue;
369                 }
370                 sa = (0xffffffffu << ilog2(sa)) | 0x1;
371
372                 /* Program register values */
373                 if (j == 0) {
374                         writel(lah, reg + PCIX0_POM0LAH);
375                         writel(lal, reg + PCIX0_POM0LAL);
376                         writel(pciah, reg + PCIX0_POM0PCIAH);
377                         writel(pcial, reg + PCIX0_POM0PCIAL);
378                         writel(sa, reg + PCIX0_POM0SA);
379                 } else {
380                         writel(lah, reg + PCIX0_POM1LAH);
381                         writel(lal, reg + PCIX0_POM1LAL);
382                         writel(pciah, reg + PCIX0_POM1PCIAH);
383                         writel(pcial, reg + PCIX0_POM1PCIAL);
384                         writel(sa, reg + PCIX0_POM1SA);
385                 }
386                 j++;
387         }
388 }
389
390 static void __init ppc4xx_configure_pcix_PIMs(struct pci_controller *hose,
391                                               void __iomem *reg,
392                                               const struct resource *res,
393                                               int big_pim,
394                                               int enable_msi_hole)
395 {
396         resource_size_t size = res->end - res->start + 1;
397         u32 sa;
398
399         /* RAM is always at 0 */
400         writel(0x00000000, reg + PCIX0_PIM0LAH);
401         writel(0x00000000, reg + PCIX0_PIM0LAL);
402
403         /* Calculate window size */
404         sa = (0xffffffffu << ilog2(size)) | 1;
405         sa |= 0x1;
406         if (res->flags & IORESOURCE_PREFETCH)
407                 sa |= 0x2;
408         if (enable_msi_hole)
409                 sa |= 0x4;
410         writel(sa, reg + PCIX0_PIM0SA);
411         if (big_pim)
412                 writel(0xffffffff, reg + PCIX0_PIM0SAH);
413
414         /* Map on PCI side */
415         writel(0x00000000, reg + PCIX0_BAR0H);
416         writel(res->start, reg + PCIX0_BAR0L);
417         writew(0x0006, reg + PCIX0_COMMAND);
418 }
419
420 static void __init ppc4xx_probe_pcix_bridge(struct device_node *np)
421 {
422         struct resource rsrc_cfg;
423         struct resource rsrc_reg;
424         struct resource dma_window;
425         struct pci_controller *hose = NULL;
426         void __iomem *reg = NULL;
427         const int *bus_range;
428         int big_pim = 0, msi = 0, primary = 0;
429
430         /* Fetch config space registers address */
431         if (of_address_to_resource(np, 0, &rsrc_cfg)) {
432                 printk(KERN_ERR "%s:Can't get PCI-X config register base !",
433                        np->full_name);
434                 return;
435         }
436         /* Fetch host bridge internal registers address */
437         if (of_address_to_resource(np, 3, &rsrc_reg)) {
438                 printk(KERN_ERR "%s: Can't get PCI-X internal register base !",
439                        np->full_name);
440                 return;
441         }
442
443         /* Check if it supports large PIMs (440GX) */
444         if (of_get_property(np, "large-inbound-windows", NULL))
445                 big_pim = 1;
446
447         /* Check if we should enable MSIs inbound hole */
448         if (of_get_property(np, "enable-msi-hole", NULL))
449                 msi = 1;
450
451         /* Check if primary bridge */
452         if (of_get_property(np, "primary", NULL))
453                 primary = 1;
454
455         /* Get bus range if any */
456         bus_range = of_get_property(np, "bus-range", NULL);
457
458         /* Map registers */
459         reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
460         if (reg == NULL) {
461                 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
462                 goto fail;
463         }
464
465         /* Allocate the host controller data structure */
466         hose = pcibios_alloc_controller(np);
467         if (!hose)
468                 goto fail;
469
470         hose->first_busno = bus_range ? bus_range[0] : 0x0;
471         hose->last_busno = bus_range ? bus_range[1] : 0xff;
472
473         /* Setup config space */
474         setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
475
476         /* Disable all windows */
477         writel(0, reg + PCIX0_POM0SA);
478         writel(0, reg + PCIX0_POM1SA);
479         writel(0, reg + PCIX0_POM2SA);
480         writel(0, reg + PCIX0_PIM0SA);
481         writel(0, reg + PCIX0_PIM1SA);
482         writel(0, reg + PCIX0_PIM2SA);
483         if (big_pim) {
484                 writel(0, reg + PCIX0_PIM0SAH);
485                 writel(0, reg + PCIX0_PIM2SAH);
486         }
487
488         /* Parse outbound mapping resources */
489         pci_process_bridge_OF_ranges(hose, np, primary);
490
491         /* Parse inbound mapping resources */
492         if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
493                 goto fail;
494
495         /* Configure outbound ranges POMs */
496         ppc4xx_configure_pcix_POMs(hose, reg);
497
498         /* Configure inbound ranges PIMs */
499         ppc4xx_configure_pcix_PIMs(hose, reg, &dma_window, big_pim, msi);
500
501         /* We don't need the registers anymore */
502         iounmap(reg);
503         return;
504
505  fail:
506         if (hose)
507                 pcibios_free_controller(hose);
508         if (reg)
509                 iounmap(reg);
510 }
511
512 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
513
514 /*
515  * 4xx PCI-Express part
516  *
517  * We support 3 parts currently based on the compatible property:
518  *
519  * ibm,plb-pciex-440speA
520  * ibm,plb-pciex-440speB
521  * ibm,plb-pciex-405ex
522  *
523  * Anything else will be rejected for now as they are all subtly
524  * different unfortunately.
525  *
526  */
527
528 #define MAX_PCIE_BUS_MAPPED     0x10
529
530 struct ppc4xx_pciex_port
531 {
532         struct pci_controller   *hose;
533         struct device_node      *node;
534         unsigned int            index;
535         int                     endpoint;
536         int                     link;
537         int                     has_ibpre;
538         unsigned int            sdr_base;
539         dcr_host_t              dcrs;
540         struct resource         cfg_space;
541         struct resource         utl_regs;
542         void __iomem            *utl_base;
543 };
544
545 static struct ppc4xx_pciex_port *ppc4xx_pciex_ports;
546 static unsigned int ppc4xx_pciex_port_count;
547
548 struct ppc4xx_pciex_hwops
549 {
550         int (*core_init)(struct device_node *np);
551         int (*port_init_hw)(struct ppc4xx_pciex_port *port);
552         int (*setup_utl)(struct ppc4xx_pciex_port *port);
553 };
554
555 static struct ppc4xx_pciex_hwops *ppc4xx_pciex_hwops;
556
557 #ifdef CONFIG_44x
558
559 /* Check various reset bits of the 440SPe PCIe core */
560 static int __init ppc440spe_pciex_check_reset(struct device_node *np)
561 {
562         u32 valPE0, valPE1, valPE2;
563         int err = 0;
564
565         /* SDR0_PEGPLLLCT1 reset */
566         if (!(mfdcri(SDR0, PESDR0_PLLLCT1) & 0x01000000)) {
567                 /*
568                  * the PCIe core was probably already initialised
569                  * by firmware - let's re-reset RCSSET regs
570                  *
571                  * -- Shouldn't we also re-reset the whole thing ? -- BenH
572                  */
573                 pr_debug("PCIE: SDR0_PLLLCT1 already reset.\n");
574                 mtdcri(SDR0, PESDR0_440SPE_RCSSET, 0x01010000);
575                 mtdcri(SDR0, PESDR1_440SPE_RCSSET, 0x01010000);
576                 mtdcri(SDR0, PESDR2_440SPE_RCSSET, 0x01010000);
577         }
578
579         valPE0 = mfdcri(SDR0, PESDR0_440SPE_RCSSET);
580         valPE1 = mfdcri(SDR0, PESDR1_440SPE_RCSSET);
581         valPE2 = mfdcri(SDR0, PESDR2_440SPE_RCSSET);
582
583         /* SDR0_PExRCSSET rstgu */
584         if (!(valPE0 & 0x01000000) ||
585             !(valPE1 & 0x01000000) ||
586             !(valPE2 & 0x01000000)) {
587                 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstgu error\n");
588                 err = -1;
589         }
590
591         /* SDR0_PExRCSSET rstdl */
592         if (!(valPE0 & 0x00010000) ||
593             !(valPE1 & 0x00010000) ||
594             !(valPE2 & 0x00010000)) {
595                 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstdl error\n");
596                 err = -1;
597         }
598
599         /* SDR0_PExRCSSET rstpyn */
600         if ((valPE0 & 0x00001000) ||
601             (valPE1 & 0x00001000) ||
602             (valPE2 & 0x00001000)) {
603                 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstpyn error\n");
604                 err = -1;
605         }
606
607         /* SDR0_PExRCSSET hldplb */
608         if ((valPE0 & 0x10000000) ||
609             (valPE1 & 0x10000000) ||
610             (valPE2 & 0x10000000)) {
611                 printk(KERN_INFO "PCIE: SDR0_PExRCSSET hldplb error\n");
612                 err = -1;
613         }
614
615         /* SDR0_PExRCSSET rdy */
616         if ((valPE0 & 0x00100000) ||
617             (valPE1 & 0x00100000) ||
618             (valPE2 & 0x00100000)) {
619                 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rdy error\n");
620                 err = -1;
621         }
622
623         /* SDR0_PExRCSSET shutdown */
624         if ((valPE0 & 0x00000100) ||
625             (valPE1 & 0x00000100) ||
626             (valPE2 & 0x00000100)) {
627                 printk(KERN_INFO "PCIE: SDR0_PExRCSSET shutdown error\n");
628                 err = -1;
629         }
630
631         return err;
632 }
633
634 /* Global PCIe core initializations for 440SPe core */
635 static int __init ppc440spe_pciex_core_init(struct device_node *np)
636 {
637         int time_out = 20;
638
639         /* Set PLL clock receiver to LVPECL */
640         mtdcri(SDR0, PESDR0_PLLLCT1, mfdcri(SDR0, PESDR0_PLLLCT1) | 1 << 28);
641
642         /* Shouldn't we do all the calibration stuff etc... here ? */
643         if (ppc440spe_pciex_check_reset(np))
644                 return -ENXIO;
645
646         if (!(mfdcri(SDR0, PESDR0_PLLLCT2) & 0x10000)) {
647                 printk(KERN_INFO "PCIE: PESDR_PLLCT2 resistance calibration "
648                        "failed (0x%08x)\n",
649                        mfdcri(SDR0, PESDR0_PLLLCT2));
650                 return -1;
651         }
652
653         /* De-assert reset of PCIe PLL, wait for lock */
654         mtdcri(SDR0, PESDR0_PLLLCT1,
655                mfdcri(SDR0, PESDR0_PLLLCT1) & ~(1 << 24));
656         udelay(3);
657
658         while (time_out) {
659                 if (!(mfdcri(SDR0, PESDR0_PLLLCT3) & 0x10000000)) {
660                         time_out--;
661                         udelay(1);
662                 } else
663                         break;
664         }
665         if (!time_out) {
666                 printk(KERN_INFO "PCIE: VCO output not locked\n");
667                 return -1;
668         }
669
670         pr_debug("PCIE initialization OK\n");
671
672         return 3;
673 }
674
675 static int ppc440spe_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
676 {
677         u32 val = 1 << 24;
678
679         if (port->endpoint)
680                 val = PTYPE_LEGACY_ENDPOINT << 20;
681         else
682                 val = PTYPE_ROOT_PORT << 20;
683
684         if (port->index == 0)
685                 val |= LNKW_X8 << 12;
686         else
687                 val |= LNKW_X4 << 12;
688
689         mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
690         mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x20222222);
691         if (of_device_is_compatible(port->node, "ibm,plb-pciex-440speA"))
692                 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x11000000);
693         mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL0SET1, 0x35000000);
694         mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL1SET1, 0x35000000);
695         mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL2SET1, 0x35000000);
696         mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL3SET1, 0x35000000);
697         if (port->index == 0) {
698                 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL4SET1,
699                        0x35000000);
700                 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL5SET1,
701                        0x35000000);
702                 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL6SET1,
703                        0x35000000);
704                 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL7SET1,
705                        0x35000000);
706         }
707         val = mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET);
708         mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
709                (val & ~(1 << 24 | 1 << 16)) | 1 << 12);
710
711         return 0;
712 }
713
714 static int ppc440speA_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
715 {
716         return ppc440spe_pciex_init_port_hw(port);
717 }
718
719 static int ppc440speB_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
720 {
721         int rc = ppc440spe_pciex_init_port_hw(port);
722
723         port->has_ibpre = 1;
724
725         return rc;
726 }
727
728 static int ppc440speA_pciex_init_utl(struct ppc4xx_pciex_port *port)
729 {
730         /* XXX Check what that value means... I hate magic */
731         dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x68782800);
732
733         /*
734          * Set buffer allocations and then assert VRB and TXE.
735          */
736         out_be32(port->utl_base + PEUTL_OUTTR,   0x08000000);
737         out_be32(port->utl_base + PEUTL_INTR,    0x02000000);
738         out_be32(port->utl_base + PEUTL_OPDBSZ,  0x10000000);
739         out_be32(port->utl_base + PEUTL_PBBSZ,   0x53000000);
740         out_be32(port->utl_base + PEUTL_IPHBSZ,  0x08000000);
741         out_be32(port->utl_base + PEUTL_IPDBSZ,  0x10000000);
742         out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
743         out_be32(port->utl_base + PEUTL_PCTL,    0x80800066);
744
745         return 0;
746 }
747
748 static int ppc440speB_pciex_init_utl(struct ppc4xx_pciex_port *port)
749 {
750         /* Report CRS to the operating system */
751         out_be32(port->utl_base + PEUTL_PBCTL,    0x08000000);
752
753         return 0;
754 }
755
756 static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops __initdata =
757 {
758         .core_init      = ppc440spe_pciex_core_init,
759         .port_init_hw   = ppc440speA_pciex_init_port_hw,
760         .setup_utl      = ppc440speA_pciex_init_utl,
761 };
762
763 static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata =
764 {
765         .core_init      = ppc440spe_pciex_core_init,
766         .port_init_hw   = ppc440speB_pciex_init_port_hw,
767         .setup_utl      = ppc440speB_pciex_init_utl,
768 };
769
770
771 #endif /* CONFIG_44x */
772
773 #ifdef CONFIG_40x
774
775 static int __init ppc405ex_pciex_core_init(struct device_node *np)
776 {
777         /* Nothing to do, return 2 ports */
778         return 2;
779 }
780
781 static void ppc405ex_pcie_phy_reset(struct ppc4xx_pciex_port *port)
782 {
783         /* Assert the PE0_PHY reset */
784         mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01010000);
785         msleep(1);
786
787         /* deassert the PE0_hotreset */
788         if (port->endpoint)
789                 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01111000);
790         else
791                 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01101000);
792
793         /* poll for phy !reset */
794         /* XXX FIXME add timeout */
795         while (!(mfdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSTA) & 0x00001000))
796                 ;
797
798         /* deassert the PE0_gpl_utl_reset */
799         mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x00101000);
800 }
801
802 static int ppc405ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
803 {
804         u32 val;
805
806         if (port->endpoint)
807                 val = PTYPE_LEGACY_ENDPOINT;
808         else
809                 val = PTYPE_ROOT_PORT;
810
811         mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET,
812                1 << 24 | val << 20 | LNKW_X1 << 12);
813
814         mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x00000000);
815         mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01010000);
816         mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET1, 0x720F0000);
817         mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET2, 0x70600003);
818
819         /*
820          * Only reset the PHY when no link is currently established.
821          * This is for the Atheros PCIe board which has problems to establish
822          * the link (again) after this PHY reset. All other currently tested
823          * PCIe boards don't show this problem.
824          * This has to be re-tested and fixed in a later release!
825          */
826 #if 0 /* XXX FIXME: Not resetting the PHY will leave all resources
827        * configured as done previously by U-Boot. Then Linux will currently
828        * not reassign them. So the PHY reset is now done always. This will
829        * lead to problems with the Atheros PCIe board again.
830        */
831         val = mfdcri(SDR0, port->sdr_base + PESDRn_LOOP);
832         if (!(val & 0x00001000))
833                 ppc405ex_pcie_phy_reset(port);
834 #else
835         ppc405ex_pcie_phy_reset(port);
836 #endif
837
838         dcr_write(port->dcrs, DCRO_PEGPL_CFG, 0x10000000);  /* guarded on */
839
840         return 0;
841 }
842
843 static int ppc405ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
844 {
845         dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
846
847         /*
848          * Set buffer allocations and then assert VRB and TXE.
849          */
850         out_be32(port->utl_base + PEUTL_OUTTR,   0x02000000);
851         out_be32(port->utl_base + PEUTL_INTR,    0x02000000);
852         out_be32(port->utl_base + PEUTL_OPDBSZ,  0x04000000);
853         out_be32(port->utl_base + PEUTL_PBBSZ,   0x21000000);
854         out_be32(port->utl_base + PEUTL_IPHBSZ,  0x02000000);
855         out_be32(port->utl_base + PEUTL_IPDBSZ,  0x04000000);
856         out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
857         out_be32(port->utl_base + PEUTL_PCTL,    0x80800066);
858
859         out_be32(port->utl_base + PEUTL_PBCTL,   0x08000000);
860
861         return 0;
862 }
863
864 static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata =
865 {
866         .core_init      = ppc405ex_pciex_core_init,
867         .port_init_hw   = ppc405ex_pciex_init_port_hw,
868         .setup_utl      = ppc405ex_pciex_init_utl,
869 };
870
871 #endif /* CONFIG_40x */
872
873
874 /* Check that the core has been initied and if not, do it */
875 static int __init ppc4xx_pciex_check_core_init(struct device_node *np)
876 {
877         static int core_init;
878         int count = -ENODEV;
879
880         if (core_init++)
881                 return 0;
882
883 #ifdef CONFIG_44x
884         if (of_device_is_compatible(np, "ibm,plb-pciex-440speA"))
885                 ppc4xx_pciex_hwops = &ppc440speA_pcie_hwops;
886         else if (of_device_is_compatible(np, "ibm,plb-pciex-440speB"))
887                 ppc4xx_pciex_hwops = &ppc440speB_pcie_hwops;
888 #endif /* CONFIG_44x    */
889 #ifdef CONFIG_40x
890         if (of_device_is_compatible(np, "ibm,plb-pciex-405ex"))
891                 ppc4xx_pciex_hwops = &ppc405ex_pcie_hwops;
892 #endif
893         if (ppc4xx_pciex_hwops == NULL) {
894                 printk(KERN_WARNING "PCIE: unknown host type %s\n",
895                        np->full_name);
896                 return -ENODEV;
897         }
898
899         count = ppc4xx_pciex_hwops->core_init(np);
900         if (count > 0) {
901                 ppc4xx_pciex_ports =
902                        kzalloc(count * sizeof(struct ppc4xx_pciex_port),
903                                GFP_KERNEL);
904                 if (ppc4xx_pciex_ports) {
905                         ppc4xx_pciex_port_count = count;
906                         return 0;
907                 }
908                 printk(KERN_WARNING "PCIE: failed to allocate ports array\n");
909                 return -ENOMEM;
910         }
911         return -ENODEV;
912 }
913
914 static void __init ppc4xx_pciex_port_init_mapping(struct ppc4xx_pciex_port *port)
915 {
916         /* We map PCI Express configuration based on the reg property */
917         dcr_write(port->dcrs, DCRO_PEGPL_CFGBAH,
918                   RES_TO_U32_HIGH(port->cfg_space.start));
919         dcr_write(port->dcrs, DCRO_PEGPL_CFGBAL,
920                   RES_TO_U32_LOW(port->cfg_space.start));
921
922         /* XXX FIXME: Use size from reg property. For now, map 512M */
923         dcr_write(port->dcrs, DCRO_PEGPL_CFGMSK, 0xe0000001);
924
925         /* We map UTL registers based on the reg property */
926         dcr_write(port->dcrs, DCRO_PEGPL_REGBAH,
927                   RES_TO_U32_HIGH(port->utl_regs.start));
928         dcr_write(port->dcrs, DCRO_PEGPL_REGBAL,
929                   RES_TO_U32_LOW(port->utl_regs.start));
930
931         /* XXX FIXME: Use size from reg property */
932         dcr_write(port->dcrs, DCRO_PEGPL_REGMSK, 0x00007001);
933
934         /* Disable all other outbound windows */
935         dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, 0);
936         dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, 0);
937         dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0);
938         dcr_write(port->dcrs, DCRO_PEGPL_MSGMSK, 0);
939 }
940
941 static int __init ppc4xx_pciex_wait_on_sdr(struct ppc4xx_pciex_port *port,
942                                            unsigned int sdr_offset,
943                                            unsigned int mask,
944                                            unsigned int value,
945                                            int timeout_ms)
946 {
947         u32 val;
948
949         while(timeout_ms--) {
950                 val = mfdcri(SDR0, port->sdr_base + sdr_offset);
951                 if ((val & mask) == value) {
952                         pr_debug("PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
953                                  port->index, sdr_offset, timeout_ms, val);
954                         return 0;
955                 }
956                 msleep(1);
957         }
958         return -1;
959 }
960
961 static int __init ppc4xx_pciex_port_init(struct ppc4xx_pciex_port *port)
962 {
963         int rc = 0;
964
965         /* Init HW */
966         if (ppc4xx_pciex_hwops->port_init_hw)
967                 rc = ppc4xx_pciex_hwops->port_init_hw(port);
968         if (rc != 0)
969                 return rc;
970
971         printk(KERN_INFO "PCIE%d: Checking link...\n",
972                port->index);
973
974         /* Wait for reset to complete */
975         if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS, 1 << 20, 0, 10)) {
976                 printk(KERN_WARNING "PCIE%d: PGRST failed\n",
977                        port->index);
978                 return -1;
979         }
980
981         /* Check for card presence detect if supported, if not, just wait for
982          * link unconditionally.
983          *
984          * note that we don't fail if there is no link, we just filter out
985          * config space accesses. That way, it will be easier to implement
986          * hotplug later on.
987          */
988         if (!port->has_ibpre ||
989             !ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
990                                       1 << 28, 1 << 28, 100)) {
991                 printk(KERN_INFO
992                        "PCIE%d: Device detected, waiting for link...\n",
993                        port->index);
994                 if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
995                                              0x1000, 0x1000, 2000))
996                         printk(KERN_WARNING
997                                "PCIE%d: Link up failed\n", port->index);
998                 else {
999                         printk(KERN_INFO
1000                                "PCIE%d: link is up !\n", port->index);
1001                         port->link = 1;
1002                 }
1003         } else
1004                 printk(KERN_INFO "PCIE%d: No device detected.\n", port->index);
1005
1006         /*
1007          * Initialize mapping: disable all regions and configure
1008          * CFG and REG regions based on resources in the device tree
1009          */
1010         ppc4xx_pciex_port_init_mapping(port);
1011
1012         /*
1013          * Map UTL
1014          */
1015         port->utl_base = ioremap(port->utl_regs.start, 0x100);
1016         BUG_ON(port->utl_base == NULL);
1017
1018         /*
1019          * Setup UTL registers --BenH.
1020          */
1021         if (ppc4xx_pciex_hwops->setup_utl)
1022                 ppc4xx_pciex_hwops->setup_utl(port);
1023
1024         /*
1025          * Check for VC0 active and assert RDY.
1026          */
1027         if (port->link &&
1028             ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS,
1029                                      1 << 16, 1 << 16, 5000)) {
1030                 printk(KERN_INFO "PCIE%d: VC0 not active\n", port->index);
1031                 port->link = 0;
1032         }
1033
1034         mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
1035                mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) | 1 << 20);
1036         msleep(100);
1037
1038         return 0;
1039 }
1040
1041 static int ppc4xx_pciex_validate_bdf(struct ppc4xx_pciex_port *port,
1042                                      struct pci_bus *bus,
1043                                      unsigned int devfn)
1044 {
1045         static int message;
1046
1047         /* Endpoint can not generate upstream(remote) config cycles */
1048         if (port->endpoint && bus->number != port->hose->first_busno)
1049                 return PCIBIOS_DEVICE_NOT_FOUND;
1050
1051         /* Check we are within the mapped range */
1052         if (bus->number > port->hose->last_busno) {
1053                 if (!message) {
1054                         printk(KERN_WARNING "Warning! Probing bus %u"
1055                                " out of range !\n", bus->number);
1056                         message++;
1057                 }
1058                 return PCIBIOS_DEVICE_NOT_FOUND;
1059         }
1060
1061         /* The root complex has only one device / function */
1062         if (bus->number == port->hose->first_busno && devfn != 0)
1063                 return PCIBIOS_DEVICE_NOT_FOUND;
1064
1065         /* The other side of the RC has only one device as well */
1066         if (bus->number == (port->hose->first_busno + 1) &&
1067             PCI_SLOT(devfn) != 0)
1068                 return PCIBIOS_DEVICE_NOT_FOUND;
1069
1070         /* Check if we have a link */
1071         if ((bus->number != port->hose->first_busno) && !port->link)
1072                 return PCIBIOS_DEVICE_NOT_FOUND;
1073
1074         return 0;
1075 }
1076
1077 static void __iomem *ppc4xx_pciex_get_config_base(struct ppc4xx_pciex_port *port,
1078                                                   struct pci_bus *bus,
1079                                                   unsigned int devfn)
1080 {
1081         int relbus;
1082
1083         /* Remove the casts when we finally remove the stupid volatile
1084          * in struct pci_controller
1085          */
1086         if (bus->number == port->hose->first_busno)
1087                 return (void __iomem *)port->hose->cfg_addr;
1088
1089         relbus = bus->number - (port->hose->first_busno + 1);
1090         return (void __iomem *)port->hose->cfg_data +
1091                 ((relbus  << 20) | (devfn << 12));
1092 }
1093
1094 static int ppc4xx_pciex_read_config(struct pci_bus *bus, unsigned int devfn,
1095                                     int offset, int len, u32 *val)
1096 {
1097         struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1098         struct ppc4xx_pciex_port *port =
1099                 &ppc4xx_pciex_ports[hose->indirect_type];
1100         void __iomem *addr;
1101         u32 gpl_cfg;
1102
1103         BUG_ON(hose != port->hose);
1104
1105         if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1106                 return PCIBIOS_DEVICE_NOT_FOUND;
1107
1108         addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1109
1110         /*
1111          * Reading from configuration space of non-existing device can
1112          * generate transaction errors. For the read duration we suppress
1113          * assertion of machine check exceptions to avoid those.
1114          */
1115         gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1116         dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1117
1118         /* Make sure no CRS is recorded */
1119         out_be32(port->utl_base + PEUTL_RCSTA, 0x00040000);
1120
1121         switch (len) {
1122         case 1:
1123                 *val = in_8((u8 *)(addr + offset));
1124                 break;
1125         case 2:
1126                 *val = in_le16((u16 *)(addr + offset));
1127                 break;
1128         default:
1129                 *val = in_le32((u32 *)(addr + offset));
1130                 break;
1131         }
1132
1133         pr_debug("pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
1134                  " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1135                  bus->number, hose->first_busno, hose->last_busno,
1136                  devfn, offset, len, addr + offset, *val);
1137
1138         /* Check for CRS (440SPe rev B does that for us but heh ..) */
1139         if (in_be32(port->utl_base + PEUTL_RCSTA) & 0x00040000) {
1140                 pr_debug("Got CRS !\n");
1141                 if (len != 4 || offset != 0)
1142                         return PCIBIOS_DEVICE_NOT_FOUND;
1143                 *val = 0xffff0001;
1144         }
1145
1146         dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1147
1148         return PCIBIOS_SUCCESSFUL;
1149 }
1150
1151 static int ppc4xx_pciex_write_config(struct pci_bus *bus, unsigned int devfn,
1152                                      int offset, int len, u32 val)
1153 {
1154         struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1155         struct ppc4xx_pciex_port *port =
1156                 &ppc4xx_pciex_ports[hose->indirect_type];
1157         void __iomem *addr;
1158         u32 gpl_cfg;
1159
1160         if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1161                 return PCIBIOS_DEVICE_NOT_FOUND;
1162
1163         addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1164
1165         /*
1166          * Reading from configuration space of non-existing device can
1167          * generate transaction errors. For the read duration we suppress
1168          * assertion of machine check exceptions to avoid those.
1169          */
1170         gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1171         dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1172
1173         pr_debug("pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
1174                  " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1175                  bus->number, hose->first_busno, hose->last_busno,
1176                  devfn, offset, len, addr + offset, val);
1177
1178         switch (len) {
1179         case 1:
1180                 out_8((u8 *)(addr + offset), val);
1181                 break;
1182         case 2:
1183                 out_le16((u16 *)(addr + offset), val);
1184                 break;
1185         default:
1186                 out_le32((u32 *)(addr + offset), val);
1187                 break;
1188         }
1189
1190         dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1191
1192         return PCIBIOS_SUCCESSFUL;
1193 }
1194
1195 static struct pci_ops ppc4xx_pciex_pci_ops =
1196 {
1197         .read  = ppc4xx_pciex_read_config,
1198         .write = ppc4xx_pciex_write_config,
1199 };
1200
1201 static void __init ppc4xx_configure_pciex_POMs(struct ppc4xx_pciex_port *port,
1202                                                struct pci_controller *hose,
1203                                                void __iomem *mbase)
1204 {
1205         u32 lah, lal, pciah, pcial, sa;
1206         int i, j;
1207
1208         /* Setup outbound memory windows */
1209         for (i = j = 0; i < 3; i++) {
1210                 struct resource *res = &hose->mem_resources[i];
1211
1212                 /* we only care about memory windows */
1213                 if (!(res->flags & IORESOURCE_MEM))
1214                         continue;
1215                 if (j > 1) {
1216                         printk(KERN_WARNING "%s: Too many ranges\n",
1217                                port->node->full_name);
1218                         break;
1219                 }
1220
1221                 /* Calculate register values */
1222                 lah = RES_TO_U32_HIGH(res->start);
1223                 lal = RES_TO_U32_LOW(res->start);
1224                 pciah = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
1225                 pcial = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
1226                 sa = res->end + 1 - res->start;
1227                 if (!is_power_of_2(sa) || sa < 0x100000 ||
1228                     sa > 0xffffffffu) {
1229                         printk(KERN_WARNING "%s: Resource out of range\n",
1230                                port->node->full_name);
1231                         continue;
1232                 }
1233                 sa = (0xffffffffu << ilog2(sa)) | 0x1;
1234
1235                 /* Program register values */
1236                 switch (j) {
1237                 case 0:
1238                         out_le32(mbase + PECFG_POM0LAH, pciah);
1239                         out_le32(mbase + PECFG_POM0LAL, pcial);
1240                         dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAH, lah);
1241                         dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAL, lal);
1242                         dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKH, 0x7fffffff);
1243                         dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, sa | 3);
1244                         break;
1245                 case 1:
1246                         out_le32(mbase + PECFG_POM1LAH, pciah);
1247                         out_le32(mbase + PECFG_POM1LAL, pcial);
1248                         dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAH, lah);
1249                         dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAL, lal);
1250                         dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKH, 0x7fffffff);
1251                         dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, sa | 3);
1252                         break;
1253                 }
1254                 j++;
1255         }
1256
1257         /* Configure IO, always 64K starting at 0 */
1258         if (hose->io_resource.flags & IORESOURCE_IO) {
1259                 lah = RES_TO_U32_HIGH(hose->io_base_phys);
1260                 lal = RES_TO_U32_LOW(hose->io_base_phys);
1261                 out_le32(mbase + PECFG_POM2LAH, 0);
1262                 out_le32(mbase + PECFG_POM2LAL, 0);
1263                 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAH, lah);
1264                 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAL, lal);
1265                 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKH, 0x7fffffff);
1266                 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0xffff0000 | 3);
1267         }
1268 }
1269
1270 static void __init ppc4xx_configure_pciex_PIMs(struct ppc4xx_pciex_port *port,
1271                                                struct pci_controller *hose,
1272                                                void __iomem *mbase,
1273                                                struct resource *res)
1274 {
1275         resource_size_t size = res->end - res->start + 1;
1276         u64 sa;
1277
1278         /* Calculate window size */
1279         sa = (0xffffffffffffffffull << ilog2(size));;
1280         if (res->flags & IORESOURCE_PREFETCH)
1281                 sa |= 0x8;
1282
1283         out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
1284         out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa));
1285
1286         /* The setup of the split looks weird to me ... let's see if it works */
1287         out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
1288         out_le32(mbase + PECFG_PIM0LAH, 0x00000000);
1289         out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
1290         out_le32(mbase + PECFG_PIM1LAH, 0x00000000);
1291         out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
1292         out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
1293
1294         /* Enable inbound mapping */
1295         out_le32(mbase + PECFG_PIMEN, 0x1);
1296
1297         out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(res->start));
1298         out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(res->start));
1299
1300         /* Enable I/O, Mem, and Busmaster cycles */
1301         out_le16(mbase + PCI_COMMAND,
1302                  in_le16(mbase + PCI_COMMAND) |
1303                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1304 }
1305
1306 static void __init ppc4xx_pciex_port_setup_hose(struct ppc4xx_pciex_port *port)
1307 {
1308         struct resource dma_window;
1309         struct pci_controller *hose = NULL;
1310         const int *bus_range;
1311         int primary = 0, busses;
1312         void __iomem *mbase = NULL, *cfg_data = NULL;
1313
1314         /* XXX FIXME: Handle endpoint mode properly */
1315         if (port->endpoint) {
1316                 printk(KERN_WARNING "PCIE%d: Port in endpoint mode !\n",
1317                        port->index);
1318                 return;
1319         }
1320
1321         /* Check if primary bridge */
1322         if (of_get_property(port->node, "primary", NULL))
1323                 primary = 1;
1324
1325         /* Get bus range if any */
1326         bus_range = of_get_property(port->node, "bus-range", NULL);
1327
1328         /* Allocate the host controller data structure */
1329         hose = pcibios_alloc_controller(port->node);
1330         if (!hose)
1331                 goto fail;
1332
1333         /* We stick the port number in "indirect_type" so the config space
1334          * ops can retrieve the port data structure easily
1335          */
1336         hose->indirect_type = port->index;
1337
1338         /* Get bus range */
1339         hose->first_busno = bus_range ? bus_range[0] : 0x0;
1340         hose->last_busno = bus_range ? bus_range[1] : 0xff;
1341
1342         /* Because of how big mapping the config space is (1M per bus), we
1343          * limit how many busses we support. In the long run, we could replace
1344          * that with something akin to kmap_atomic instead. We set aside 1 bus
1345          * for the host itself too.
1346          */
1347         busses = hose->last_busno - hose->first_busno; /* This is off by 1 */
1348         if (busses > MAX_PCIE_BUS_MAPPED) {
1349                 busses = MAX_PCIE_BUS_MAPPED;
1350                 hose->last_busno = hose->first_busno + busses;
1351         }
1352
1353         /* We map the external config space in cfg_data and the host config
1354          * space in cfg_addr. External space is 1M per bus, internal space
1355          * is 4K
1356          */
1357         cfg_data = ioremap(port->cfg_space.start +
1358                                  (hose->first_busno + 1) * 0x100000,
1359                                  busses * 0x100000);
1360         mbase = ioremap(port->cfg_space.start + 0x10000000, 0x1000);
1361         if (cfg_data == NULL || mbase == NULL) {
1362                 printk(KERN_ERR "%s: Can't map config space !",
1363                        port->node->full_name);
1364                 goto fail;
1365         }
1366
1367         hose->cfg_data = cfg_data;
1368         hose->cfg_addr = mbase;
1369
1370         pr_debug("PCIE %s, bus %d..%d\n", port->node->full_name,
1371                  hose->first_busno, hose->last_busno);
1372         pr_debug("     config space mapped at: root @0x%p, other @0x%p\n",
1373                  hose->cfg_addr, hose->cfg_data);
1374
1375         /* Setup config space */
1376         hose->ops = &ppc4xx_pciex_pci_ops;
1377         port->hose = hose;
1378         mbase = (void __iomem *)hose->cfg_addr;
1379
1380         /*
1381          * Set bus numbers on our root port
1382          */
1383         out_8(mbase + PCI_PRIMARY_BUS, hose->first_busno);
1384         out_8(mbase + PCI_SECONDARY_BUS, hose->first_busno + 1);
1385         out_8(mbase + PCI_SUBORDINATE_BUS, hose->last_busno);
1386
1387         /*
1388          * OMRs are already reset, also disable PIMs
1389          */
1390         out_le32(mbase + PECFG_PIMEN, 0);
1391
1392         /* Parse outbound mapping resources */
1393         pci_process_bridge_OF_ranges(hose, port->node, primary);
1394
1395         /* Parse inbound mapping resources */
1396         if (ppc4xx_parse_dma_ranges(hose, mbase, &dma_window) != 0)
1397                 goto fail;
1398
1399         /* Configure outbound ranges POMs */
1400         ppc4xx_configure_pciex_POMs(port, hose, mbase);
1401
1402         /* Configure inbound ranges PIMs */
1403         ppc4xx_configure_pciex_PIMs(port, hose, mbase, &dma_window);
1404
1405         /* The root complex doesn't show up if we don't set some vendor
1406          * and device IDs into it. Those are the same bogus one that the
1407          * initial code in arch/ppc add. We might want to change that.
1408          */
1409         out_le16(mbase + 0x200, 0xaaa0 + port->index);
1410         out_le16(mbase + 0x202, 0xbed0 + port->index);
1411
1412         /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
1413         out_le32(mbase + 0x208, 0x06040001);
1414
1415         printk(KERN_INFO "PCIE%d: successfully set as root-complex\n",
1416                port->index);
1417         return;
1418  fail:
1419         if (hose)
1420                 pcibios_free_controller(hose);
1421         if (cfg_data)
1422                 iounmap(cfg_data);
1423         if (mbase)
1424                 iounmap(mbase);
1425 }
1426
1427 static void __init ppc4xx_probe_pciex_bridge(struct device_node *np)
1428 {
1429         struct ppc4xx_pciex_port *port;
1430         const u32 *pval;
1431         int portno;
1432         unsigned int dcrs;
1433
1434         /* First, proceed to core initialization as we assume there's
1435          * only one PCIe core in the system
1436          */
1437         if (ppc4xx_pciex_check_core_init(np))
1438                 return;
1439
1440         /* Get the port number from the device-tree */
1441         pval = of_get_property(np, "port", NULL);
1442         if (pval == NULL) {
1443                 printk(KERN_ERR "PCIE: Can't find port number for %s\n",
1444                        np->full_name);
1445                 return;
1446         }
1447         portno = *pval;
1448         if (portno >= ppc4xx_pciex_port_count) {
1449                 printk(KERN_ERR "PCIE: port number out of range for %s\n",
1450                        np->full_name);
1451                 return;
1452         }
1453         port = &ppc4xx_pciex_ports[portno];
1454         port->index = portno;
1455         port->node = of_node_get(np);
1456         pval = of_get_property(np, "sdr-base", NULL);
1457         if (pval == NULL) {
1458                 printk(KERN_ERR "PCIE: missing sdr-base for %s\n",
1459                        np->full_name);
1460                 return;
1461         }
1462         port->sdr_base = *pval;
1463
1464         /* XXX Currently, we only support root complex mode */
1465         port->endpoint = 0;
1466
1467         /* Fetch config space registers address */
1468         if (of_address_to_resource(np, 0, &port->cfg_space)) {
1469                 printk(KERN_ERR "%s: Can't get PCI-E config space !",
1470                        np->full_name);
1471                 return;
1472         }
1473         /* Fetch host bridge internal registers address */
1474         if (of_address_to_resource(np, 1, &port->utl_regs)) {
1475                 printk(KERN_ERR "%s: Can't get UTL register base !",
1476                        np->full_name);
1477                 return;
1478         }
1479
1480         /* Map DCRs */
1481         dcrs = dcr_resource_start(np, 0);
1482         if (dcrs == 0) {
1483                 printk(KERN_ERR "%s: Can't get DCR register base !",
1484                        np->full_name);
1485                 return;
1486         }
1487         port->dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
1488
1489         /* Initialize the port specific registers */
1490         if (ppc4xx_pciex_port_init(port)) {
1491                 printk(KERN_WARNING "PCIE%d: Port init failed\n", port->index);
1492                 return;
1493         }
1494
1495         /* Setup the linux hose data structure */
1496         ppc4xx_pciex_port_setup_hose(port);
1497 }
1498
1499 #endif /* CONFIG_PPC4xx_PCI_EXPRESS */
1500
1501 static int __init ppc4xx_pci_find_bridges(void)
1502 {
1503         struct device_node *np;
1504
1505 #ifdef CONFIG_PPC4xx_PCI_EXPRESS
1506         for_each_compatible_node(np, NULL, "ibm,plb-pciex")
1507                 ppc4xx_probe_pciex_bridge(np);
1508 #endif
1509         for_each_compatible_node(np, NULL, "ibm,plb-pcix")
1510                 ppc4xx_probe_pcix_bridge(np);
1511         for_each_compatible_node(np, NULL, "ibm,plb-pci")
1512                 ppc4xx_probe_pci_bridge(np);
1513
1514         return 0;
1515 }
1516 arch_initcall(ppc4xx_pci_find_bridges);
1517