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