blob: 9f6f73d584d62c20ce5650cecd4dbe7e3917d865 [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
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +110019#undef DEBUG
20
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +110021#include <linux/kernel.h>
22#include <linux/pci.h>
23#include <linux/init.h>
24#include <linux/of.h>
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +110025#include <linux/bootmem.h>
26#include <linux/delay.h>
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +110027
28#include <asm/io.h>
29#include <asm/pci-bridge.h>
30#include <asm/machdep.h>
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +110031#include <asm/dcr.h>
32#include <asm/dcr-regs.h>
Ilya Yanokcc2e1132008-09-01 17:53:22 +100033#include <mm/mmu_decl.h>
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +110034
35#include "ppc4xx_pci.h"
36
37static int dma_offset_set;
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
Stefan Roeseaccf5ef2007-12-21 15:39:38 +110050static 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
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +110059static 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
Josh Boyer5ce4b592008-06-17 19:01:38 -040076 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
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +110081 /* 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}
92DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, fixup_ppc4xx_pci_bridge);
93
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +110094static 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;
Ilya Yanokcc2e1132008-09-01 17:53:22 +1000106 size = 0x80000000;
107 res->end = size - 1;
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100108 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 "
Ilya Yanokcc2e1132008-09-01 17:53:22 +1000169 "(size=%llx total_memory=%llx)\n",
170 hose->dn->full_name, size, (u64)total_memory);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100171 return -ENXIO;
172 }
173
174 /* Check we are a power of 2 size and that base is a multiple of size*/
Ilya Yanokcc2e1132008-09-01 17:53:22 +1000175 if ((size & (size - 1)) != 0 ||
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100176 (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 */
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100200
201static 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;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100222 pciha = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
223 pcila = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100224
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
244static 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
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100268static void __init ppc4xx_probe_pci_bridge(struct device_node *np)
269{
270 /* NYI */
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100271 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
Matthias Fuchs5a013fc2008-09-10 05:55:46 +0000279 /* Check if device is enabled */
280 if (!of_device_is_available(np)) {
281 printk(KERN_INFO "%s: Port disabled via device-tree\n",
282 np->full_name);
283 return;
284 }
285
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100286 /* Fetch config space registers address */
287 if (of_address_to_resource(np, 0, &rsrc_cfg)) {
Matthias Fuchs5a013fc2008-09-10 05:55:46 +0000288 printk(KERN_ERR "%s: Can't get PCI config register base !",
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100289 np->full_name);
290 return;
291 }
292 /* Fetch host bridge internal registers address */
293 if (of_address_to_resource(np, 3, &rsrc_reg)) {
294 printk(KERN_ERR "%s: Can't get PCI internal register base !",
295 np->full_name);
296 return;
297 }
298
299 /* Check if primary bridge */
300 if (of_get_property(np, "primary", NULL))
301 primary = 1;
302
303 /* Get bus range if any */
304 bus_range = of_get_property(np, "bus-range", NULL);
305
306 /* Map registers */
307 reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
308 if (reg == NULL) {
309 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
310 goto fail;
311 }
312
313 /* Allocate the host controller data structure */
314 hose = pcibios_alloc_controller(np);
315 if (!hose)
316 goto fail;
317
318 hose->first_busno = bus_range ? bus_range[0] : 0x0;
319 hose->last_busno = bus_range ? bus_range[1] : 0xff;
320
321 /* Setup config space */
322 setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
323
324 /* Disable all windows */
325 writel(0, reg + PCIL0_PMM0MA);
326 writel(0, reg + PCIL0_PMM1MA);
327 writel(0, reg + PCIL0_PMM2MA);
328 writel(0, reg + PCIL0_PTM1MS);
329 writel(0, reg + PCIL0_PTM2MS);
330
331 /* Parse outbound mapping resources */
332 pci_process_bridge_OF_ranges(hose, np, primary);
333
334 /* Parse inbound mapping resources */
335 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
336 goto fail;
337
338 /* Configure outbound ranges POMs */
339 ppc4xx_configure_pci_PMMs(hose, reg);
340
341 /* Configure inbound ranges PIMs */
342 ppc4xx_configure_pci_PTMs(hose, reg, &dma_window);
343
344 /* We don't need the registers anymore */
345 iounmap(reg);
346 return;
347
348 fail:
349 if (hose)
350 pcibios_free_controller(hose);
351 if (reg)
352 iounmap(reg);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100353}
354
355/*
356 * 4xx PCI-X part
357 */
358
359static void __init ppc4xx_configure_pcix_POMs(struct pci_controller *hose,
360 void __iomem *reg)
361{
362 u32 lah, lal, pciah, pcial, sa;
363 int i, j;
364
365 /* Setup outbound memory windows */
366 for (i = j = 0; i < 3; i++) {
367 struct resource *res = &hose->mem_resources[i];
368
369 /* we only care about memory windows */
370 if (!(res->flags & IORESOURCE_MEM))
371 continue;
372 if (j > 1) {
373 printk(KERN_WARNING "%s: Too many ranges\n",
374 hose->dn->full_name);
375 break;
376 }
377
378 /* Calculate register values */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100379 lah = RES_TO_U32_HIGH(res->start);
380 lal = RES_TO_U32_LOW(res->start);
381 pciah = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
382 pcial = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100383 sa = res->end + 1 - res->start;
384 if (!is_power_of_2(sa) || sa < 0x100000 ||
385 sa > 0xffffffffu) {
386 printk(KERN_WARNING "%s: Resource out of range\n",
387 hose->dn->full_name);
388 continue;
389 }
390 sa = (0xffffffffu << ilog2(sa)) | 0x1;
391
392 /* Program register values */
393 if (j == 0) {
394 writel(lah, reg + PCIX0_POM0LAH);
395 writel(lal, reg + PCIX0_POM0LAL);
396 writel(pciah, reg + PCIX0_POM0PCIAH);
397 writel(pcial, reg + PCIX0_POM0PCIAL);
398 writel(sa, reg + PCIX0_POM0SA);
399 } else {
400 writel(lah, reg + PCIX0_POM1LAH);
401 writel(lal, reg + PCIX0_POM1LAL);
402 writel(pciah, reg + PCIX0_POM1PCIAH);
403 writel(pcial, reg + PCIX0_POM1PCIAL);
404 writel(sa, reg + PCIX0_POM1SA);
405 }
406 j++;
407 }
408}
409
410static void __init ppc4xx_configure_pcix_PIMs(struct pci_controller *hose,
411 void __iomem *reg,
412 const struct resource *res,
413 int big_pim,
414 int enable_msi_hole)
415{
416 resource_size_t size = res->end - res->start + 1;
417 u32 sa;
418
419 /* RAM is always at 0 */
420 writel(0x00000000, reg + PCIX0_PIM0LAH);
421 writel(0x00000000, reg + PCIX0_PIM0LAL);
422
423 /* Calculate window size */
424 sa = (0xffffffffu << ilog2(size)) | 1;
425 sa |= 0x1;
426 if (res->flags & IORESOURCE_PREFETCH)
427 sa |= 0x2;
428 if (enable_msi_hole)
429 sa |= 0x4;
430 writel(sa, reg + PCIX0_PIM0SA);
431 if (big_pim)
432 writel(0xffffffff, reg + PCIX0_PIM0SAH);
433
434 /* Map on PCI side */
435 writel(0x00000000, reg + PCIX0_BAR0H);
436 writel(res->start, reg + PCIX0_BAR0L);
437 writew(0x0006, reg + PCIX0_COMMAND);
438}
439
440static void __init ppc4xx_probe_pcix_bridge(struct device_node *np)
441{
442 struct resource rsrc_cfg;
443 struct resource rsrc_reg;
444 struct resource dma_window;
445 struct pci_controller *hose = NULL;
446 void __iomem *reg = NULL;
447 const int *bus_range;
448 int big_pim = 0, msi = 0, primary = 0;
449
450 /* Fetch config space registers address */
451 if (of_address_to_resource(np, 0, &rsrc_cfg)) {
452 printk(KERN_ERR "%s:Can't get PCI-X config register base !",
453 np->full_name);
454 return;
455 }
456 /* Fetch host bridge internal registers address */
457 if (of_address_to_resource(np, 3, &rsrc_reg)) {
458 printk(KERN_ERR "%s: Can't get PCI-X internal register base !",
459 np->full_name);
460 return;
461 }
462
463 /* Check if it supports large PIMs (440GX) */
464 if (of_get_property(np, "large-inbound-windows", NULL))
465 big_pim = 1;
466
467 /* Check if we should enable MSIs inbound hole */
468 if (of_get_property(np, "enable-msi-hole", NULL))
469 msi = 1;
470
471 /* Check if primary bridge */
472 if (of_get_property(np, "primary", NULL))
473 primary = 1;
474
475 /* Get bus range if any */
476 bus_range = of_get_property(np, "bus-range", NULL);
477
478 /* Map registers */
479 reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
480 if (reg == NULL) {
481 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
482 goto fail;
483 }
484
485 /* Allocate the host controller data structure */
486 hose = pcibios_alloc_controller(np);
487 if (!hose)
488 goto fail;
489
490 hose->first_busno = bus_range ? bus_range[0] : 0x0;
491 hose->last_busno = bus_range ? bus_range[1] : 0xff;
492
493 /* Setup config space */
494 setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
495
496 /* Disable all windows */
497 writel(0, reg + PCIX0_POM0SA);
498 writel(0, reg + PCIX0_POM1SA);
499 writel(0, reg + PCIX0_POM2SA);
500 writel(0, reg + PCIX0_PIM0SA);
501 writel(0, reg + PCIX0_PIM1SA);
502 writel(0, reg + PCIX0_PIM2SA);
503 if (big_pim) {
504 writel(0, reg + PCIX0_PIM0SAH);
505 writel(0, reg + PCIX0_PIM2SAH);
506 }
507
508 /* Parse outbound mapping resources */
509 pci_process_bridge_OF_ranges(hose, np, primary);
510
511 /* Parse inbound mapping resources */
512 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
513 goto fail;
514
515 /* Configure outbound ranges POMs */
516 ppc4xx_configure_pcix_POMs(hose, reg);
517
518 /* Configure inbound ranges PIMs */
519 ppc4xx_configure_pcix_PIMs(hose, reg, &dma_window, big_pim, msi);
520
521 /* We don't need the registers anymore */
522 iounmap(reg);
523 return;
524
525 fail:
526 if (hose)
527 pcibios_free_controller(hose);
528 if (reg)
529 iounmap(reg);
530}
531
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100532#ifdef CONFIG_PPC4xx_PCI_EXPRESS
533
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100534/*
535 * 4xx PCI-Express part
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100536 *
537 * We support 3 parts currently based on the compatible property:
538 *
Stefan Roeseaccf5ef2007-12-21 15:39:38 +1100539 * ibm,plb-pciex-440spe
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100540 * ibm,plb-pciex-405ex
Stefan Roese66b7e502008-02-24 08:08:27 +1100541 * ibm,plb-pciex-460ex
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100542 *
543 * Anything else will be rejected for now as they are all subtly
544 * different unfortunately.
545 *
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100546 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100547
Stefan Roese78994e22007-12-31 16:41:15 +1100548#define MAX_PCIE_BUS_MAPPED 0x40
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100549
550struct ppc4xx_pciex_port
551{
552 struct pci_controller *hose;
553 struct device_node *node;
554 unsigned int index;
555 int endpoint;
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100556 int link;
557 int has_ibpre;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100558 unsigned int sdr_base;
559 dcr_host_t dcrs;
560 struct resource cfg_space;
561 struct resource utl_regs;
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100562 void __iomem *utl_base;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100563};
564
565static struct ppc4xx_pciex_port *ppc4xx_pciex_ports;
566static unsigned int ppc4xx_pciex_port_count;
567
568struct ppc4xx_pciex_hwops
569{
570 int (*core_init)(struct device_node *np);
571 int (*port_init_hw)(struct ppc4xx_pciex_port *port);
572 int (*setup_utl)(struct ppc4xx_pciex_port *port);
573};
574
575static struct ppc4xx_pciex_hwops *ppc4xx_pciex_hwops;
576
577#ifdef CONFIG_44x
578
579/* Check various reset bits of the 440SPe PCIe core */
580static int __init ppc440spe_pciex_check_reset(struct device_node *np)
581{
582 u32 valPE0, valPE1, valPE2;
583 int err = 0;
584
585 /* SDR0_PEGPLLLCT1 reset */
586 if (!(mfdcri(SDR0, PESDR0_PLLLCT1) & 0x01000000)) {
587 /*
588 * the PCIe core was probably already initialised
589 * by firmware - let's re-reset RCSSET regs
590 *
591 * -- Shouldn't we also re-reset the whole thing ? -- BenH
592 */
593 pr_debug("PCIE: SDR0_PLLLCT1 already reset.\n");
594 mtdcri(SDR0, PESDR0_440SPE_RCSSET, 0x01010000);
595 mtdcri(SDR0, PESDR1_440SPE_RCSSET, 0x01010000);
596 mtdcri(SDR0, PESDR2_440SPE_RCSSET, 0x01010000);
597 }
598
599 valPE0 = mfdcri(SDR0, PESDR0_440SPE_RCSSET);
600 valPE1 = mfdcri(SDR0, PESDR1_440SPE_RCSSET);
601 valPE2 = mfdcri(SDR0, PESDR2_440SPE_RCSSET);
602
603 /* SDR0_PExRCSSET rstgu */
604 if (!(valPE0 & 0x01000000) ||
605 !(valPE1 & 0x01000000) ||
606 !(valPE2 & 0x01000000)) {
607 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstgu error\n");
608 err = -1;
609 }
610
611 /* SDR0_PExRCSSET rstdl */
612 if (!(valPE0 & 0x00010000) ||
613 !(valPE1 & 0x00010000) ||
614 !(valPE2 & 0x00010000)) {
615 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstdl error\n");
616 err = -1;
617 }
618
619 /* SDR0_PExRCSSET rstpyn */
620 if ((valPE0 & 0x00001000) ||
621 (valPE1 & 0x00001000) ||
622 (valPE2 & 0x00001000)) {
623 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstpyn error\n");
624 err = -1;
625 }
626
627 /* SDR0_PExRCSSET hldplb */
628 if ((valPE0 & 0x10000000) ||
629 (valPE1 & 0x10000000) ||
630 (valPE2 & 0x10000000)) {
631 printk(KERN_INFO "PCIE: SDR0_PExRCSSET hldplb error\n");
632 err = -1;
633 }
634
635 /* SDR0_PExRCSSET rdy */
636 if ((valPE0 & 0x00100000) ||
637 (valPE1 & 0x00100000) ||
638 (valPE2 & 0x00100000)) {
639 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rdy error\n");
640 err = -1;
641 }
642
643 /* SDR0_PExRCSSET shutdown */
644 if ((valPE0 & 0x00000100) ||
645 (valPE1 & 0x00000100) ||
646 (valPE2 & 0x00000100)) {
647 printk(KERN_INFO "PCIE: SDR0_PExRCSSET shutdown error\n");
648 err = -1;
649 }
650
651 return err;
652}
653
654/* Global PCIe core initializations for 440SPe core */
655static int __init ppc440spe_pciex_core_init(struct device_node *np)
656{
657 int time_out = 20;
658
659 /* Set PLL clock receiver to LVPECL */
Valentine Barshak6e42b212008-03-07 01:34:52 +1100660 dcri_clrset(SDR0, PESDR0_PLLLCT1, 0, 1 << 28);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100661
662 /* Shouldn't we do all the calibration stuff etc... here ? */
663 if (ppc440spe_pciex_check_reset(np))
664 return -ENXIO;
665
666 if (!(mfdcri(SDR0, PESDR0_PLLLCT2) & 0x10000)) {
667 printk(KERN_INFO "PCIE: PESDR_PLLCT2 resistance calibration "
668 "failed (0x%08x)\n",
669 mfdcri(SDR0, PESDR0_PLLLCT2));
670 return -1;
671 }
672
673 /* De-assert reset of PCIe PLL, wait for lock */
Valentine Barshak6e42b212008-03-07 01:34:52 +1100674 dcri_clrset(SDR0, PESDR0_PLLLCT1, 1 << 24, 0);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100675 udelay(3);
676
677 while (time_out) {
678 if (!(mfdcri(SDR0, PESDR0_PLLLCT3) & 0x10000000)) {
679 time_out--;
680 udelay(1);
681 } else
682 break;
683 }
684 if (!time_out) {
685 printk(KERN_INFO "PCIE: VCO output not locked\n");
686 return -1;
687 }
688
689 pr_debug("PCIE initialization OK\n");
690
691 return 3;
692}
693
694static int ppc440spe_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
695{
696 u32 val = 1 << 24;
697
698 if (port->endpoint)
699 val = PTYPE_LEGACY_ENDPOINT << 20;
700 else
701 val = PTYPE_ROOT_PORT << 20;
702
703 if (port->index == 0)
704 val |= LNKW_X8 << 12;
705 else
706 val |= LNKW_X4 << 12;
707
708 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
709 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x20222222);
Stefan Roeseaccf5ef2007-12-21 15:39:38 +1100710 if (ppc440spe_revA())
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100711 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x11000000);
712 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL0SET1, 0x35000000);
713 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL1SET1, 0x35000000);
714 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL2SET1, 0x35000000);
715 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL3SET1, 0x35000000);
716 if (port->index == 0) {
717 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL4SET1,
718 0x35000000);
719 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL5SET1,
720 0x35000000);
721 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL6SET1,
722 0x35000000);
723 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL7SET1,
724 0x35000000);
725 }
Valentine Barshak6e42b212008-03-07 01:34:52 +1100726 dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET,
727 (1 << 24) | (1 << 16), 1 << 12);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100728
729 return 0;
730}
731
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100732static int ppc440speA_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
733{
734 return ppc440spe_pciex_init_port_hw(port);
735}
736
737static int ppc440speB_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
738{
739 int rc = ppc440spe_pciex_init_port_hw(port);
740
741 port->has_ibpre = 1;
742
743 return rc;
744}
745
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100746static int ppc440speA_pciex_init_utl(struct ppc4xx_pciex_port *port)
747{
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100748 /* XXX Check what that value means... I hate magic */
749 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x68782800);
750
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100751 /*
752 * Set buffer allocations and then assert VRB and TXE.
753 */
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100754 out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
755 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
756 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x10000000);
757 out_be32(port->utl_base + PEUTL_PBBSZ, 0x53000000);
758 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x08000000);
759 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x10000000);
760 out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
761 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100762
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100763 return 0;
764}
765
766static int ppc440speB_pciex_init_utl(struct ppc4xx_pciex_port *port)
767{
768 /* Report CRS to the operating system */
769 out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100770
771 return 0;
772}
773
774static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops __initdata =
775{
776 .core_init = ppc440spe_pciex_core_init,
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100777 .port_init_hw = ppc440speA_pciex_init_port_hw,
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100778 .setup_utl = ppc440speA_pciex_init_utl,
779};
780
781static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata =
782{
783 .core_init = ppc440spe_pciex_core_init,
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100784 .port_init_hw = ppc440speB_pciex_init_port_hw,
785 .setup_utl = ppc440speB_pciex_init_utl,
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100786};
787
Stefan Roese66b7e502008-02-24 08:08:27 +1100788static int __init ppc460ex_pciex_core_init(struct device_node *np)
789{
790 /* Nothing to do, return 2 ports */
791 return 2;
792}
793
794static int ppc460ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
795{
796 u32 val;
797 u32 utlset1;
798
Stefan Roese5f919252008-04-02 00:45:00 +1100799 if (port->endpoint)
Stefan Roese66b7e502008-02-24 08:08:27 +1100800 val = PTYPE_LEGACY_ENDPOINT << 20;
Stefan Roese5f919252008-04-02 00:45:00 +1100801 else
Stefan Roese66b7e502008-02-24 08:08:27 +1100802 val = PTYPE_ROOT_PORT << 20;
Stefan Roese66b7e502008-02-24 08:08:27 +1100803
804 if (port->index == 0) {
805 val |= LNKW_X1 << 12;
Stefan Roese5f919252008-04-02 00:45:00 +1100806 utlset1 = 0x20000000;
Stefan Roese66b7e502008-02-24 08:08:27 +1100807 } else {
808 val |= LNKW_X4 << 12;
Stefan Roese5f919252008-04-02 00:45:00 +1100809 utlset1 = 0x20101101;
Stefan Roese66b7e502008-02-24 08:08:27 +1100810 }
811
812 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
813 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, utlset1);
814 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01210000);
815
816 switch (port->index) {
817 case 0:
818 mtdcri(SDR0, PESDR0_460EX_L0CDRCTL, 0x00003230);
Tirumala R Marrie30c9872008-08-21 18:53:34 +0000819 mtdcri(SDR0, PESDR0_460EX_L0DRV, 0x00000130);
Stefan Roese66b7e502008-02-24 08:08:27 +1100820 mtdcri(SDR0, PESDR0_460EX_L0CLK, 0x00000006);
821
822 mtdcri(SDR0, PESDR0_460EX_PHY_CTL_RST,0x10000000);
823 break;
824
825 case 1:
826 mtdcri(SDR0, PESDR1_460EX_L0CDRCTL, 0x00003230);
827 mtdcri(SDR0, PESDR1_460EX_L1CDRCTL, 0x00003230);
828 mtdcri(SDR0, PESDR1_460EX_L2CDRCTL, 0x00003230);
829 mtdcri(SDR0, PESDR1_460EX_L3CDRCTL, 0x00003230);
Tirumala R Marrie30c9872008-08-21 18:53:34 +0000830 mtdcri(SDR0, PESDR1_460EX_L0DRV, 0x00000130);
831 mtdcri(SDR0, PESDR1_460EX_L1DRV, 0x00000130);
832 mtdcri(SDR0, PESDR1_460EX_L2DRV, 0x00000130);
833 mtdcri(SDR0, PESDR1_460EX_L3DRV, 0x00000130);
Stefan Roese66b7e502008-02-24 08:08:27 +1100834 mtdcri(SDR0, PESDR1_460EX_L0CLK, 0x00000006);
835 mtdcri(SDR0, PESDR1_460EX_L1CLK, 0x00000006);
836 mtdcri(SDR0, PESDR1_460EX_L2CLK, 0x00000006);
837 mtdcri(SDR0, PESDR1_460EX_L3CLK, 0x00000006);
838
839 mtdcri(SDR0, PESDR1_460EX_PHY_CTL_RST,0x10000000);
840 break;
841 }
842
843 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
844 mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) |
845 (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTPYN));
846
847 /* Poll for PHY reset */
848 /* XXX FIXME add timeout */
849 switch (port->index) {
850 case 0:
851 while (!(mfdcri(SDR0, PESDR0_460EX_RSTSTA) & 0x1))
852 udelay(10);
853 break;
854 case 1:
855 while (!(mfdcri(SDR0, PESDR1_460EX_RSTSTA) & 0x1))
856 udelay(10);
857 break;
858 }
859
860 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
861 (mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) &
862 ~(PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL)) |
863 PESDRx_RCSSET_RSTPYN);
864
865 port->has_ibpre = 1;
866
867 return 0;
868}
869
870static int ppc460ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
871{
872 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
873
874 /*
875 * Set buffer allocations and then assert VRB and TXE.
876 */
877 out_be32(port->utl_base + PEUTL_PBCTL, 0x0800000c);
878 out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
879 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
880 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
881 out_be32(port->utl_base + PEUTL_PBBSZ, 0x00000000);
882 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
883 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
884 out_be32(port->utl_base + PEUTL_RCIRQEN,0x00f00000);
885 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
886
887 return 0;
888}
889
890static struct ppc4xx_pciex_hwops ppc460ex_pcie_hwops __initdata =
891{
892 .core_init = ppc460ex_pciex_core_init,
893 .port_init_hw = ppc460ex_pciex_init_port_hw,
894 .setup_utl = ppc460ex_pciex_init_utl,
895};
896
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100897#endif /* CONFIG_44x */
898
899#ifdef CONFIG_40x
900
901static int __init ppc405ex_pciex_core_init(struct device_node *np)
902{
903 /* Nothing to do, return 2 ports */
904 return 2;
905}
906
907static void ppc405ex_pcie_phy_reset(struct ppc4xx_pciex_port *port)
908{
909 /* Assert the PE0_PHY reset */
910 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01010000);
911 msleep(1);
912
913 /* deassert the PE0_hotreset */
914 if (port->endpoint)
915 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01111000);
916 else
917 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01101000);
918
919 /* poll for phy !reset */
920 /* XXX FIXME add timeout */
921 while (!(mfdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSTA) & 0x00001000))
922 ;
923
924 /* deassert the PE0_gpl_utl_reset */
925 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x00101000);
926}
927
928static int ppc405ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
929{
930 u32 val;
931
932 if (port->endpoint)
933 val = PTYPE_LEGACY_ENDPOINT;
934 else
935 val = PTYPE_ROOT_PORT;
936
937 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET,
938 1 << 24 | val << 20 | LNKW_X1 << 12);
939
940 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x00000000);
941 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01010000);
942 mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET1, 0x720F0000);
943 mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET2, 0x70600003);
944
945 /*
946 * Only reset the PHY when no link is currently established.
947 * This is for the Atheros PCIe board which has problems to establish
948 * the link (again) after this PHY reset. All other currently tested
949 * PCIe boards don't show this problem.
950 * This has to be re-tested and fixed in a later release!
951 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100952 val = mfdcri(SDR0, port->sdr_base + PESDRn_LOOP);
953 if (!(val & 0x00001000))
954 ppc405ex_pcie_phy_reset(port);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100955
956 dcr_write(port->dcrs, DCRO_PEGPL_CFG, 0x10000000); /* guarded on */
957
Stefan Roese55aaf6e2007-12-07 20:34:34 +1100958 port->has_ibpre = 1;
959
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100960 return 0;
961}
962
963static int ppc405ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
964{
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100965 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
966
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100967 /*
968 * Set buffer allocations and then assert VRB and TXE.
969 */
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100970 out_be32(port->utl_base + PEUTL_OUTTR, 0x02000000);
971 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
972 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
973 out_be32(port->utl_base + PEUTL_PBBSZ, 0x21000000);
974 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
975 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
976 out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
977 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100978
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100979 out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100980
981 return 0;
982}
983
984static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata =
985{
986 .core_init = ppc405ex_pciex_core_init,
987 .port_init_hw = ppc405ex_pciex_init_port_hw,
988 .setup_utl = ppc405ex_pciex_init_utl,
989};
990
991#endif /* CONFIG_40x */
992
993
994/* Check that the core has been initied and if not, do it */
995static int __init ppc4xx_pciex_check_core_init(struct device_node *np)
996{
997 static int core_init;
998 int count = -ENODEV;
999
1000 if (core_init++)
1001 return 0;
1002
1003#ifdef CONFIG_44x
Stefan Roeseaccf5ef2007-12-21 15:39:38 +11001004 if (of_device_is_compatible(np, "ibm,plb-pciex-440spe")) {
1005 if (ppc440spe_revA())
1006 ppc4xx_pciex_hwops = &ppc440speA_pcie_hwops;
1007 else
1008 ppc4xx_pciex_hwops = &ppc440speB_pcie_hwops;
1009 }
Stefan Roese66b7e502008-02-24 08:08:27 +11001010 if (of_device_is_compatible(np, "ibm,plb-pciex-460ex"))
1011 ppc4xx_pciex_hwops = &ppc460ex_pcie_hwops;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001012#endif /* CONFIG_44x */
1013#ifdef CONFIG_40x
1014 if (of_device_is_compatible(np, "ibm,plb-pciex-405ex"))
1015 ppc4xx_pciex_hwops = &ppc405ex_pcie_hwops;
1016#endif
1017 if (ppc4xx_pciex_hwops == NULL) {
1018 printk(KERN_WARNING "PCIE: unknown host type %s\n",
1019 np->full_name);
1020 return -ENODEV;
1021 }
1022
1023 count = ppc4xx_pciex_hwops->core_init(np);
1024 if (count > 0) {
1025 ppc4xx_pciex_ports =
1026 kzalloc(count * sizeof(struct ppc4xx_pciex_port),
1027 GFP_KERNEL);
1028 if (ppc4xx_pciex_ports) {
1029 ppc4xx_pciex_port_count = count;
1030 return 0;
1031 }
1032 printk(KERN_WARNING "PCIE: failed to allocate ports array\n");
1033 return -ENOMEM;
1034 }
1035 return -ENODEV;
1036}
1037
1038static void __init ppc4xx_pciex_port_init_mapping(struct ppc4xx_pciex_port *port)
1039{
1040 /* We map PCI Express configuration based on the reg property */
1041 dcr_write(port->dcrs, DCRO_PEGPL_CFGBAH,
1042 RES_TO_U32_HIGH(port->cfg_space.start));
1043 dcr_write(port->dcrs, DCRO_PEGPL_CFGBAL,
1044 RES_TO_U32_LOW(port->cfg_space.start));
1045
1046 /* XXX FIXME: Use size from reg property. For now, map 512M */
1047 dcr_write(port->dcrs, DCRO_PEGPL_CFGMSK, 0xe0000001);
1048
1049 /* We map UTL registers based on the reg property */
1050 dcr_write(port->dcrs, DCRO_PEGPL_REGBAH,
1051 RES_TO_U32_HIGH(port->utl_regs.start));
1052 dcr_write(port->dcrs, DCRO_PEGPL_REGBAL,
1053 RES_TO_U32_LOW(port->utl_regs.start));
1054
1055 /* XXX FIXME: Use size from reg property */
1056 dcr_write(port->dcrs, DCRO_PEGPL_REGMSK, 0x00007001);
1057
1058 /* Disable all other outbound windows */
1059 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, 0);
1060 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, 0);
1061 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0);
1062 dcr_write(port->dcrs, DCRO_PEGPL_MSGMSK, 0);
1063}
1064
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001065static int __init ppc4xx_pciex_wait_on_sdr(struct ppc4xx_pciex_port *port,
1066 unsigned int sdr_offset,
1067 unsigned int mask,
1068 unsigned int value,
1069 int timeout_ms)
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001070{
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001071 u32 val;
1072
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001073 while(timeout_ms--) {
1074 val = mfdcri(SDR0, port->sdr_base + sdr_offset);
1075 if ((val & mask) == value) {
1076 pr_debug("PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
1077 port->index, sdr_offset, timeout_ms, val);
1078 return 0;
1079 }
1080 msleep(1);
1081 }
1082 return -1;
1083}
1084
1085static int __init ppc4xx_pciex_port_init(struct ppc4xx_pciex_port *port)
1086{
1087 int rc = 0;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001088
1089 /* Init HW */
1090 if (ppc4xx_pciex_hwops->port_init_hw)
1091 rc = ppc4xx_pciex_hwops->port_init_hw(port);
1092 if (rc != 0)
1093 return rc;
1094
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001095 printk(KERN_INFO "PCIE%d: Checking link...\n",
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001096 port->index);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001097
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001098 /* Wait for reset to complete */
1099 if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS, 1 << 20, 0, 10)) {
1100 printk(KERN_WARNING "PCIE%d: PGRST failed\n",
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001101 port->index);
1102 return -1;
1103 }
1104
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001105 /* Check for card presence detect if supported, if not, just wait for
1106 * link unconditionally.
1107 *
1108 * note that we don't fail if there is no link, we just filter out
1109 * config space accesses. That way, it will be easier to implement
1110 * hotplug later on.
1111 */
1112 if (!port->has_ibpre ||
1113 !ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
1114 1 << 28, 1 << 28, 100)) {
1115 printk(KERN_INFO
1116 "PCIE%d: Device detected, waiting for link...\n",
1117 port->index);
1118 if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
1119 0x1000, 0x1000, 2000))
1120 printk(KERN_WARNING
1121 "PCIE%d: Link up failed\n", port->index);
1122 else {
1123 printk(KERN_INFO
1124 "PCIE%d: link is up !\n", port->index);
1125 port->link = 1;
1126 }
1127 } else
1128 printk(KERN_INFO "PCIE%d: No device detected.\n", port->index);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001129
1130 /*
1131 * Initialize mapping: disable all regions and configure
1132 * CFG and REG regions based on resources in the device tree
1133 */
1134 ppc4xx_pciex_port_init_mapping(port);
1135
1136 /*
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001137 * Map UTL
1138 */
1139 port->utl_base = ioremap(port->utl_regs.start, 0x100);
1140 BUG_ON(port->utl_base == NULL);
1141
1142 /*
1143 * Setup UTL registers --BenH.
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001144 */
1145 if (ppc4xx_pciex_hwops->setup_utl)
1146 ppc4xx_pciex_hwops->setup_utl(port);
1147
1148 /*
1149 * Check for VC0 active and assert RDY.
1150 */
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001151 if (port->link &&
1152 ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS,
1153 1 << 16, 1 << 16, 5000)) {
1154 printk(KERN_INFO "PCIE%d: VC0 not active\n", port->index);
1155 port->link = 0;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001156 }
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001157
Valentine Barshak6e42b212008-03-07 01:34:52 +11001158 dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET, 0, 1 << 20);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001159 msleep(100);
1160
1161 return 0;
1162}
1163
1164static int ppc4xx_pciex_validate_bdf(struct ppc4xx_pciex_port *port,
1165 struct pci_bus *bus,
1166 unsigned int devfn)
1167{
1168 static int message;
1169
1170 /* Endpoint can not generate upstream(remote) config cycles */
1171 if (port->endpoint && bus->number != port->hose->first_busno)
1172 return PCIBIOS_DEVICE_NOT_FOUND;
1173
1174 /* Check we are within the mapped range */
1175 if (bus->number > port->hose->last_busno) {
1176 if (!message) {
1177 printk(KERN_WARNING "Warning! Probing bus %u"
1178 " out of range !\n", bus->number);
1179 message++;
1180 }
1181 return PCIBIOS_DEVICE_NOT_FOUND;
1182 }
1183
1184 /* The root complex has only one device / function */
1185 if (bus->number == port->hose->first_busno && devfn != 0)
1186 return PCIBIOS_DEVICE_NOT_FOUND;
1187
1188 /* The other side of the RC has only one device as well */
1189 if (bus->number == (port->hose->first_busno + 1) &&
1190 PCI_SLOT(devfn) != 0)
1191 return PCIBIOS_DEVICE_NOT_FOUND;
1192
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001193 /* Check if we have a link */
1194 if ((bus->number != port->hose->first_busno) && !port->link)
1195 return PCIBIOS_DEVICE_NOT_FOUND;
1196
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001197 return 0;
1198}
1199
1200static void __iomem *ppc4xx_pciex_get_config_base(struct ppc4xx_pciex_port *port,
1201 struct pci_bus *bus,
1202 unsigned int devfn)
1203{
1204 int relbus;
1205
1206 /* Remove the casts when we finally remove the stupid volatile
1207 * in struct pci_controller
1208 */
1209 if (bus->number == port->hose->first_busno)
1210 return (void __iomem *)port->hose->cfg_addr;
1211
1212 relbus = bus->number - (port->hose->first_busno + 1);
1213 return (void __iomem *)port->hose->cfg_data +
1214 ((relbus << 20) | (devfn << 12));
1215}
1216
1217static int ppc4xx_pciex_read_config(struct pci_bus *bus, unsigned int devfn,
1218 int offset, int len, u32 *val)
1219{
1220 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1221 struct ppc4xx_pciex_port *port =
1222 &ppc4xx_pciex_ports[hose->indirect_type];
1223 void __iomem *addr;
1224 u32 gpl_cfg;
1225
1226 BUG_ON(hose != port->hose);
1227
1228 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1229 return PCIBIOS_DEVICE_NOT_FOUND;
1230
1231 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1232
1233 /*
1234 * Reading from configuration space of non-existing device can
1235 * generate transaction errors. For the read duration we suppress
1236 * assertion of machine check exceptions to avoid those.
1237 */
1238 gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1239 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1240
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001241 /* Make sure no CRS is recorded */
1242 out_be32(port->utl_base + PEUTL_RCSTA, 0x00040000);
1243
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001244 switch (len) {
1245 case 1:
1246 *val = in_8((u8 *)(addr + offset));
1247 break;
1248 case 2:
1249 *val = in_le16((u16 *)(addr + offset));
1250 break;
1251 default:
1252 *val = in_le32((u32 *)(addr + offset));
1253 break;
1254 }
1255
1256 pr_debug("pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
1257 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1258 bus->number, hose->first_busno, hose->last_busno,
1259 devfn, offset, len, addr + offset, *val);
1260
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001261 /* Check for CRS (440SPe rev B does that for us but heh ..) */
1262 if (in_be32(port->utl_base + PEUTL_RCSTA) & 0x00040000) {
1263 pr_debug("Got CRS !\n");
1264 if (len != 4 || offset != 0)
1265 return PCIBIOS_DEVICE_NOT_FOUND;
1266 *val = 0xffff0001;
1267 }
1268
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001269 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1270
1271 return PCIBIOS_SUCCESSFUL;
1272}
1273
1274static int ppc4xx_pciex_write_config(struct pci_bus *bus, unsigned int devfn,
1275 int offset, int len, u32 val)
1276{
1277 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1278 struct ppc4xx_pciex_port *port =
1279 &ppc4xx_pciex_ports[hose->indirect_type];
1280 void __iomem *addr;
1281 u32 gpl_cfg;
1282
1283 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1284 return PCIBIOS_DEVICE_NOT_FOUND;
1285
1286 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1287
1288 /*
1289 * Reading from configuration space of non-existing device can
1290 * generate transaction errors. For the read duration we suppress
1291 * assertion of machine check exceptions to avoid those.
1292 */
1293 gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1294 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1295
1296 pr_debug("pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
1297 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1298 bus->number, hose->first_busno, hose->last_busno,
1299 devfn, offset, len, addr + offset, val);
1300
1301 switch (len) {
1302 case 1:
1303 out_8((u8 *)(addr + offset), val);
1304 break;
1305 case 2:
1306 out_le16((u16 *)(addr + offset), val);
1307 break;
1308 default:
1309 out_le32((u32 *)(addr + offset), val);
1310 break;
1311 }
1312
1313 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1314
1315 return PCIBIOS_SUCCESSFUL;
1316}
1317
1318static struct pci_ops ppc4xx_pciex_pci_ops =
1319{
1320 .read = ppc4xx_pciex_read_config,
1321 .write = ppc4xx_pciex_write_config,
1322};
1323
1324static void __init ppc4xx_configure_pciex_POMs(struct ppc4xx_pciex_port *port,
1325 struct pci_controller *hose,
1326 void __iomem *mbase)
1327{
1328 u32 lah, lal, pciah, pcial, sa;
1329 int i, j;
1330
1331 /* Setup outbound memory windows */
1332 for (i = j = 0; i < 3; i++) {
1333 struct resource *res = &hose->mem_resources[i];
1334
1335 /* we only care about memory windows */
1336 if (!(res->flags & IORESOURCE_MEM))
1337 continue;
1338 if (j > 1) {
1339 printk(KERN_WARNING "%s: Too many ranges\n",
1340 port->node->full_name);
1341 break;
1342 }
1343
1344 /* Calculate register values */
1345 lah = RES_TO_U32_HIGH(res->start);
1346 lal = RES_TO_U32_LOW(res->start);
1347 pciah = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
1348 pcial = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
1349 sa = res->end + 1 - res->start;
1350 if (!is_power_of_2(sa) || sa < 0x100000 ||
1351 sa > 0xffffffffu) {
1352 printk(KERN_WARNING "%s: Resource out of range\n",
1353 port->node->full_name);
1354 continue;
1355 }
1356 sa = (0xffffffffu << ilog2(sa)) | 0x1;
1357
1358 /* Program register values */
1359 switch (j) {
1360 case 0:
1361 out_le32(mbase + PECFG_POM0LAH, pciah);
1362 out_le32(mbase + PECFG_POM0LAL, pcial);
1363 dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAH, lah);
1364 dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAL, lal);
1365 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKH, 0x7fffffff);
1366 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, sa | 3);
1367 break;
1368 case 1:
1369 out_le32(mbase + PECFG_POM1LAH, pciah);
1370 out_le32(mbase + PECFG_POM1LAL, pcial);
1371 dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAH, lah);
1372 dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAL, lal);
1373 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKH, 0x7fffffff);
1374 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, sa | 3);
1375 break;
1376 }
1377 j++;
1378 }
1379
1380 /* Configure IO, always 64K starting at 0 */
1381 if (hose->io_resource.flags & IORESOURCE_IO) {
1382 lah = RES_TO_U32_HIGH(hose->io_base_phys);
1383 lal = RES_TO_U32_LOW(hose->io_base_phys);
1384 out_le32(mbase + PECFG_POM2LAH, 0);
1385 out_le32(mbase + PECFG_POM2LAL, 0);
1386 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAH, lah);
1387 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAL, lal);
1388 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKH, 0x7fffffff);
1389 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0xffff0000 | 3);
1390 }
1391}
1392
1393static void __init ppc4xx_configure_pciex_PIMs(struct ppc4xx_pciex_port *port,
1394 struct pci_controller *hose,
1395 void __iomem *mbase,
1396 struct resource *res)
1397{
1398 resource_size_t size = res->end - res->start + 1;
1399 u64 sa;
1400
Stefan Roese80daac32008-04-22 00:54:30 +10001401 if (port->endpoint) {
1402 resource_size_t ep_addr = 0;
1403 resource_size_t ep_size = 32 << 20;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001404
Stefan Roese80daac32008-04-22 00:54:30 +10001405 /* Currently we map a fixed 64MByte window to PLB address
1406 * 0 (SDRAM). This should probably be configurable via a dts
1407 * property.
1408 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001409
Stefan Roese80daac32008-04-22 00:54:30 +10001410 /* Calculate window size */
1411 sa = (0xffffffffffffffffull << ilog2(ep_size));;
1412
1413 /* Setup BAR0 */
1414 out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
1415 out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa) |
1416 PCI_BASE_ADDRESS_MEM_TYPE_64);
1417
1418 /* Disable BAR1 & BAR2 */
1419 out_le32(mbase + PECFG_BAR1MPA, 0);
1420 out_le32(mbase + PECFG_BAR2HMPA, 0);
1421 out_le32(mbase + PECFG_BAR2LMPA, 0);
1422
1423 out_le32(mbase + PECFG_PIM01SAH, RES_TO_U32_HIGH(sa));
1424 out_le32(mbase + PECFG_PIM01SAL, RES_TO_U32_LOW(sa));
1425
1426 out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(ep_addr));
1427 out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(ep_addr));
1428 } else {
1429 /* Calculate window size */
1430 sa = (0xffffffffffffffffull << ilog2(size));;
1431 if (res->flags & IORESOURCE_PREFETCH)
1432 sa |= 0x8;
1433
1434 out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
1435 out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa));
1436
1437 /* The setup of the split looks weird to me ... let's see
1438 * if it works
1439 */
1440 out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
1441 out_le32(mbase + PECFG_PIM0LAH, 0x00000000);
1442 out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
1443 out_le32(mbase + PECFG_PIM1LAH, 0x00000000);
1444 out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
1445 out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
1446
1447 out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(res->start));
1448 out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(res->start));
1449 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001450
1451 /* Enable inbound mapping */
1452 out_le32(mbase + PECFG_PIMEN, 0x1);
1453
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001454 /* Enable I/O, Mem, and Busmaster cycles */
1455 out_le16(mbase + PCI_COMMAND,
1456 in_le16(mbase + PCI_COMMAND) |
1457 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1458}
1459
1460static void __init ppc4xx_pciex_port_setup_hose(struct ppc4xx_pciex_port *port)
1461{
1462 struct resource dma_window;
1463 struct pci_controller *hose = NULL;
1464 const int *bus_range;
1465 int primary = 0, busses;
1466 void __iomem *mbase = NULL, *cfg_data = NULL;
Stefan Roese80daac32008-04-22 00:54:30 +10001467 const u32 *pval;
1468 u32 val;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001469
1470 /* Check if primary bridge */
1471 if (of_get_property(port->node, "primary", NULL))
1472 primary = 1;
1473
1474 /* Get bus range if any */
1475 bus_range = of_get_property(port->node, "bus-range", NULL);
1476
1477 /* Allocate the host controller data structure */
1478 hose = pcibios_alloc_controller(port->node);
1479 if (!hose)
1480 goto fail;
1481
1482 /* We stick the port number in "indirect_type" so the config space
1483 * ops can retrieve the port data structure easily
1484 */
1485 hose->indirect_type = port->index;
1486
1487 /* Get bus range */
1488 hose->first_busno = bus_range ? bus_range[0] : 0x0;
1489 hose->last_busno = bus_range ? bus_range[1] : 0xff;
1490
1491 /* Because of how big mapping the config space is (1M per bus), we
1492 * limit how many busses we support. In the long run, we could replace
1493 * that with something akin to kmap_atomic instead. We set aside 1 bus
1494 * for the host itself too.
1495 */
1496 busses = hose->last_busno - hose->first_busno; /* This is off by 1 */
1497 if (busses > MAX_PCIE_BUS_MAPPED) {
1498 busses = MAX_PCIE_BUS_MAPPED;
1499 hose->last_busno = hose->first_busno + busses;
1500 }
1501
Stefan Roese80daac32008-04-22 00:54:30 +10001502 if (!port->endpoint) {
1503 /* Only map the external config space in cfg_data for
1504 * PCIe root-complexes. External space is 1M per bus
1505 */
1506 cfg_data = ioremap(port->cfg_space.start +
1507 (hose->first_busno + 1) * 0x100000,
1508 busses * 0x100000);
1509 if (cfg_data == NULL) {
1510 printk(KERN_ERR "%s: Can't map external config space !",
1511 port->node->full_name);
1512 goto fail;
1513 }
1514 hose->cfg_data = cfg_data;
1515 }
1516
1517 /* Always map the host config space in cfg_addr.
1518 * Internal space is 4K
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001519 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001520 mbase = ioremap(port->cfg_space.start + 0x10000000, 0x1000);
Stefan Roese80daac32008-04-22 00:54:30 +10001521 if (mbase == NULL) {
1522 printk(KERN_ERR "%s: Can't map internal config space !",
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001523 port->node->full_name);
1524 goto fail;
1525 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001526 hose->cfg_addr = mbase;
1527
1528 pr_debug("PCIE %s, bus %d..%d\n", port->node->full_name,
1529 hose->first_busno, hose->last_busno);
1530 pr_debug(" config space mapped at: root @0x%p, other @0x%p\n",
1531 hose->cfg_addr, hose->cfg_data);
1532
1533 /* Setup config space */
1534 hose->ops = &ppc4xx_pciex_pci_ops;
1535 port->hose = hose;
1536 mbase = (void __iomem *)hose->cfg_addr;
1537
Stefan Roese80daac32008-04-22 00:54:30 +10001538 if (!port->endpoint) {
1539 /*
1540 * Set bus numbers on our root port
1541 */
1542 out_8(mbase + PCI_PRIMARY_BUS, hose->first_busno);
1543 out_8(mbase + PCI_SECONDARY_BUS, hose->first_busno + 1);
1544 out_8(mbase + PCI_SUBORDINATE_BUS, hose->last_busno);
1545 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001546
1547 /*
1548 * OMRs are already reset, also disable PIMs
1549 */
1550 out_le32(mbase + PECFG_PIMEN, 0);
1551
1552 /* Parse outbound mapping resources */
1553 pci_process_bridge_OF_ranges(hose, port->node, primary);
1554
1555 /* Parse inbound mapping resources */
1556 if (ppc4xx_parse_dma_ranges(hose, mbase, &dma_window) != 0)
1557 goto fail;
1558
1559 /* Configure outbound ranges POMs */
1560 ppc4xx_configure_pciex_POMs(port, hose, mbase);
1561
1562 /* Configure inbound ranges PIMs */
1563 ppc4xx_configure_pciex_PIMs(port, hose, mbase, &dma_window);
1564
1565 /* The root complex doesn't show up if we don't set some vendor
Stefan Roese80daac32008-04-22 00:54:30 +10001566 * and device IDs into it. The defaults below are the same bogus
1567 * one that the initial code in arch/ppc had. This can be
1568 * overwritten by setting the "vendor-id/device-id" properties
1569 * in the pciex node.
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001570 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001571
Stefan Roese80daac32008-04-22 00:54:30 +10001572 /* Get the (optional) vendor-/device-id from the device-tree */
1573 pval = of_get_property(port->node, "vendor-id", NULL);
1574 if (pval) {
1575 val = *pval;
1576 } else {
1577 if (!port->endpoint)
1578 val = 0xaaa0 + port->index;
1579 else
1580 val = 0xeee0 + port->index;
1581 }
1582 out_le16(mbase + 0x200, val);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001583
Stefan Roese80daac32008-04-22 00:54:30 +10001584 pval = of_get_property(port->node, "device-id", NULL);
1585 if (pval) {
1586 val = *pval;
1587 } else {
1588 if (!port->endpoint)
1589 val = 0xbed0 + port->index;
1590 else
1591 val = 0xfed0 + port->index;
1592 }
1593 out_le16(mbase + 0x202, val);
1594
1595 if (!port->endpoint) {
1596 /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
1597 out_le32(mbase + 0x208, 0x06040001);
1598
1599 printk(KERN_INFO "PCIE%d: successfully set as root-complex\n",
1600 port->index);
1601 } else {
1602 /* Set Class Code to Processor/PPC */
1603 out_le32(mbase + 0x208, 0x0b200001);
1604
1605 printk(KERN_INFO "PCIE%d: successfully set as endpoint\n",
1606 port->index);
1607 }
1608
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001609 return;
1610 fail:
1611 if (hose)
1612 pcibios_free_controller(hose);
1613 if (cfg_data)
1614 iounmap(cfg_data);
1615 if (mbase)
1616 iounmap(mbase);
1617}
1618
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11001619static void __init ppc4xx_probe_pciex_bridge(struct device_node *np)
1620{
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001621 struct ppc4xx_pciex_port *port;
1622 const u32 *pval;
1623 int portno;
1624 unsigned int dcrs;
Stefan Roese80daac32008-04-22 00:54:30 +10001625 const char *val;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001626
1627 /* First, proceed to core initialization as we assume there's
1628 * only one PCIe core in the system
1629 */
1630 if (ppc4xx_pciex_check_core_init(np))
1631 return;
1632
1633 /* Get the port number from the device-tree */
1634 pval = of_get_property(np, "port", NULL);
1635 if (pval == NULL) {
1636 printk(KERN_ERR "PCIE: Can't find port number for %s\n",
1637 np->full_name);
1638 return;
1639 }
1640 portno = *pval;
1641 if (portno >= ppc4xx_pciex_port_count) {
1642 printk(KERN_ERR "PCIE: port number out of range for %s\n",
1643 np->full_name);
1644 return;
1645 }
1646 port = &ppc4xx_pciex_ports[portno];
1647 port->index = portno;
Stefan Roese995ada82008-06-06 00:22:29 +10001648
1649 /*
1650 * Check if device is enabled
1651 */
1652 if (!of_device_is_available(np)) {
1653 printk(KERN_INFO "PCIE%d: Port disabled via device-tree\n", port->index);
1654 return;
1655 }
1656
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001657 port->node = of_node_get(np);
1658 pval = of_get_property(np, "sdr-base", NULL);
1659 if (pval == NULL) {
1660 printk(KERN_ERR "PCIE: missing sdr-base for %s\n",
1661 np->full_name);
1662 return;
1663 }
1664 port->sdr_base = *pval;
1665
Stefan Roese80daac32008-04-22 00:54:30 +10001666 /* Check if device_type property is set to "pci" or "pci-endpoint".
1667 * Resulting from this setup this PCIe port will be configured
1668 * as root-complex or as endpoint.
1669 */
1670 val = of_get_property(port->node, "device_type", NULL);
1671 if (!strcmp(val, "pci-endpoint")) {
1672 port->endpoint = 1;
1673 } else if (!strcmp(val, "pci")) {
1674 port->endpoint = 0;
1675 } else {
1676 printk(KERN_ERR "PCIE: missing or incorrect device_type for %s\n",
1677 np->full_name);
1678 return;
1679 }
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001680
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001681 /* Fetch config space registers address */
1682 if (of_address_to_resource(np, 0, &port->cfg_space)) {
1683 printk(KERN_ERR "%s: Can't get PCI-E config space !",
1684 np->full_name);
1685 return;
1686 }
1687 /* Fetch host bridge internal registers address */
1688 if (of_address_to_resource(np, 1, &port->utl_regs)) {
1689 printk(KERN_ERR "%s: Can't get UTL register base !",
1690 np->full_name);
1691 return;
1692 }
1693
1694 /* Map DCRs */
1695 dcrs = dcr_resource_start(np, 0);
1696 if (dcrs == 0) {
1697 printk(KERN_ERR "%s: Can't get DCR register base !",
1698 np->full_name);
1699 return;
1700 }
1701 port->dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
1702
1703 /* Initialize the port specific registers */
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001704 if (ppc4xx_pciex_port_init(port)) {
1705 printk(KERN_WARNING "PCIE%d: Port init failed\n", port->index);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001706 return;
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001707 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001708
1709 /* Setup the linux hose data structure */
1710 ppc4xx_pciex_port_setup_hose(port);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11001711}
1712
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001713#endif /* CONFIG_PPC4xx_PCI_EXPRESS */
1714
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11001715static int __init ppc4xx_pci_find_bridges(void)
1716{
1717 struct device_node *np;
1718
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001719#ifdef CONFIG_PPC4xx_PCI_EXPRESS
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11001720 for_each_compatible_node(np, NULL, "ibm,plb-pciex")
1721 ppc4xx_probe_pciex_bridge(np);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001722#endif
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11001723 for_each_compatible_node(np, NULL, "ibm,plb-pcix")
1724 ppc4xx_probe_pcix_bridge(np);
1725 for_each_compatible_node(np, NULL, "ibm,plb-pci")
1726 ppc4xx_probe_pci_bridge(np);
1727
1728 return 0;
1729}
1730arch_initcall(ppc4xx_pci_find_bridges);
1731