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