blob: 95d5eaa0caa2e42cb18618557550b76ededae17e [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +110028
29#include <asm/io.h>
30#include <asm/pci-bridge.h>
31#include <asm/machdep.h>
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +110032#include <asm/dcr.h>
33#include <asm/dcr-regs.h>
Ilya Yanokcc2e1132008-09-01 17:53:22 +100034#include <mm/mmu_decl.h>
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +110035
36#include "ppc4xx_pci.h"
37
38static int dma_offset_set;
39
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +110040#define U64_TO_U32_LOW(val) ((u32)((val) & 0x00000000ffffffffULL))
41#define U64_TO_U32_HIGH(val) ((u32)((val) >> 32))
42
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -070043#define RES_TO_U32_LOW(val) \
44 ((sizeof(resource_size_t) > sizeof(u32)) ? U64_TO_U32_LOW(val) : (val))
45#define RES_TO_U32_HIGH(val) \
46 ((sizeof(resource_size_t) > sizeof(u32)) ? U64_TO_U32_HIGH(val) : (0))
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +110047
Stefan Roeseaccf5ef2007-12-21 15:39:38 +110048static inline int ppc440spe_revA(void)
49{
50 /* Catch both 440SPe variants, with and without RAID6 support */
51 if ((mfspr(SPRN_PVR) & 0xffefffff) == 0x53421890)
52 return 1;
53 else
54 return 0;
55}
56
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +110057static void fixup_ppc4xx_pci_bridge(struct pci_dev *dev)
58{
59 struct pci_controller *hose;
60 int i;
61
62 if (dev->devfn != 0 || dev->bus->self != NULL)
63 return;
64
65 hose = pci_bus_to_host(dev->bus);
66 if (hose == NULL)
67 return;
68
69 if (!of_device_is_compatible(hose->dn, "ibm,plb-pciex") &&
70 !of_device_is_compatible(hose->dn, "ibm,plb-pcix") &&
71 !of_device_is_compatible(hose->dn, "ibm,plb-pci"))
72 return;
73
Josh Boyer5ce4b592008-06-17 19:01:38 -040074 if (of_device_is_compatible(hose->dn, "ibm,plb440epx-pci") ||
75 of_device_is_compatible(hose->dn, "ibm,plb440grx-pci")) {
76 hose->indirect_type |= PPC_INDIRECT_TYPE_BROKEN_MRM;
77 }
78
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +110079 /* Hide the PCI host BARs from the kernel as their content doesn't
80 * fit well in the resource management
81 */
82 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
83 dev->resource[i].start = dev->resource[i].end = 0;
84 dev->resource[i].flags = 0;
85 }
86
87 printk(KERN_INFO "PCI: Hiding 4xx host bridge resources %s\n",
88 pci_name(dev));
89}
90DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, fixup_ppc4xx_pci_bridge);
91
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +110092static int __init ppc4xx_parse_dma_ranges(struct pci_controller *hose,
93 void __iomem *reg,
94 struct resource *res)
95{
96 u64 size;
97 const u32 *ranges;
98 int rlen;
99 int pna = of_n_addr_cells(hose->dn);
100 int np = pna + 5;
101
102 /* Default */
103 res->start = 0;
Ilya Yanokcc2e1132008-09-01 17:53:22 +1000104 size = 0x80000000;
105 res->end = size - 1;
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100106 res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
107
108 /* Get dma-ranges property */
109 ranges = of_get_property(hose->dn, "dma-ranges", &rlen);
110 if (ranges == NULL)
111 goto out;
112
113 /* Walk it */
114 while ((rlen -= np * 4) >= 0) {
115 u32 pci_space = ranges[0];
116 u64 pci_addr = of_read_number(ranges + 1, 2);
117 u64 cpu_addr = of_translate_dma_address(hose->dn, ranges + 3);
118 size = of_read_number(ranges + pna + 3, 2);
119 ranges += np;
120 if (cpu_addr == OF_BAD_ADDR || size == 0)
121 continue;
122
123 /* We only care about memory */
124 if ((pci_space & 0x03000000) != 0x02000000)
125 continue;
126
127 /* We currently only support memory at 0, and pci_addr
128 * within 32 bits space
129 */
130 if (cpu_addr != 0 || pci_addr > 0xffffffff) {
131 printk(KERN_WARNING "%s: Ignored unsupported dma range"
132 " 0x%016llx...0x%016llx -> 0x%016llx\n",
133 hose->dn->full_name,
134 pci_addr, pci_addr + size - 1, cpu_addr);
135 continue;
136 }
137
138 /* Check if not prefetchable */
139 if (!(pci_space & 0x40000000))
140 res->flags &= ~IORESOURCE_PREFETCH;
141
142
143 /* Use that */
144 res->start = pci_addr;
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100145 /* Beware of 32 bits resources */
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -0700146 if (sizeof(resource_size_t) == sizeof(u32) &&
147 (pci_addr + size) > 0x100000000ull)
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100148 res->end = 0xffffffff;
149 else
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100150 res->end = res->start + size - 1;
151 break;
152 }
153
154 /* We only support one global DMA offset */
155 if (dma_offset_set && pci_dram_offset != res->start) {
156 printk(KERN_ERR "%s: dma-ranges(s) mismatch\n",
157 hose->dn->full_name);
158 return -ENXIO;
159 }
160
161 /* Check that we can fit all of memory as we don't support
162 * DMA bounce buffers
163 */
164 if (size < total_memory) {
165 printk(KERN_ERR "%s: dma-ranges too small "
Ilya Yanokcc2e1132008-09-01 17:53:22 +1000166 "(size=%llx total_memory=%llx)\n",
167 hose->dn->full_name, size, (u64)total_memory);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100168 return -ENXIO;
169 }
170
171 /* Check we are a power of 2 size and that base is a multiple of size*/
Ilya Yanokcc2e1132008-09-01 17:53:22 +1000172 if ((size & (size - 1)) != 0 ||
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100173 (res->start & (size - 1)) != 0) {
174 printk(KERN_ERR "%s: dma-ranges unaligned\n",
175 hose->dn->full_name);
176 return -ENXIO;
177 }
178
179 /* Check that we are fully contained within 32 bits space */
180 if (res->end > 0xffffffff) {
181 printk(KERN_ERR "%s: dma-ranges outside of 32 bits space\n",
182 hose->dn->full_name);
183 return -ENXIO;
184 }
185 out:
186 dma_offset_set = 1;
187 pci_dram_offset = res->start;
188
189 printk(KERN_INFO "4xx PCI DMA offset set to 0x%08lx\n",
190 pci_dram_offset);
191 return 0;
192}
193
194/*
195 * 4xx PCI 2.x part
196 */
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100197
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000198static int __init ppc4xx_setup_one_pci_PMM(struct pci_controller *hose,
199 void __iomem *reg,
200 u64 plb_addr,
201 u64 pci_addr,
202 u64 size,
203 unsigned int flags,
204 int index)
205{
206 u32 ma, pcila, pciha;
207
Benjamin Herrenschmidt1ac00cc2009-02-01 14:24:18 +0000208 /* Hack warning ! The "old" PCI 2.x cell only let us configure the low
209 * 32-bit of incoming PLB addresses. The top 4 bits of the 36-bit
210 * address are actually hard wired to a value that appears to depend
211 * on the specific SoC. For example, it's 0 on 440EP and 1 on 440EPx.
212 *
213 * The trick here is we just crop those top bits and ignore them when
214 * programming the chip. That means the device-tree has to be right
215 * for the specific part used (we don't print a warning if it's wrong
216 * but on the other hand, you'll crash quickly enough), but at least
217 * this code should work whatever the hard coded value is
218 */
219 plb_addr &= 0xffffffffull;
220
221 /* Note: Due to the above hack, the test below doesn't actually test
222 * if you address is above 4G, but it tests that address and
223 * (address + size) are both contained in the same 4G
224 */
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000225 if ((plb_addr + size) > 0xffffffffull || !is_power_of_2(size) ||
226 size < 0x1000 || (plb_addr & (size - 1)) != 0) {
227 printk(KERN_WARNING "%s: Resource out of range\n",
228 hose->dn->full_name);
229 return -1;
230 }
231 ma = (0xffffffffu << ilog2(size)) | 1;
232 if (flags & IORESOURCE_PREFETCH)
233 ma |= 2;
234
235 pciha = RES_TO_U32_HIGH(pci_addr);
236 pcila = RES_TO_U32_LOW(pci_addr);
237
238 writel(plb_addr, reg + PCIL0_PMM0LA + (0x10 * index));
239 writel(pcila, reg + PCIL0_PMM0PCILA + (0x10 * index));
240 writel(pciha, reg + PCIL0_PMM0PCIHA + (0x10 * index));
241 writel(ma, reg + PCIL0_PMM0MA + (0x10 * index));
242
243 return 0;
244}
245
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100246static void __init ppc4xx_configure_pci_PMMs(struct pci_controller *hose,
247 void __iomem *reg)
248{
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000249 int i, j, found_isa_hole = 0;
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100250
251 /* Setup outbound memory windows */
252 for (i = j = 0; i < 3; i++) {
253 struct resource *res = &hose->mem_resources[i];
254
255 /* we only care about memory windows */
256 if (!(res->flags & IORESOURCE_MEM))
257 continue;
258 if (j > 2) {
259 printk(KERN_WARNING "%s: Too many ranges\n",
260 hose->dn->full_name);
261 break;
262 }
263
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000264 /* Configure the resource */
265 if (ppc4xx_setup_one_pci_PMM(hose, reg,
266 res->start,
267 res->start - hose->pci_mem_offset,
Joe Perches28f65c112011-06-09 09:13:32 -0700268 resource_size(res),
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000269 res->flags,
270 j) == 0) {
271 j++;
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100272
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000273 /* If the resource PCI address is 0 then we have our
274 * ISA memory hole
275 */
276 if (res->start == hose->pci_mem_offset)
277 found_isa_hole = 1;
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100278 }
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100279 }
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000280
281 /* Handle ISA memory hole if not already covered */
282 if (j <= 2 && !found_isa_hole && hose->isa_mem_size)
283 if (ppc4xx_setup_one_pci_PMM(hose, reg, hose->isa_mem_phys, 0,
284 hose->isa_mem_size, 0, j) == 0)
285 printk(KERN_INFO "%s: Legacy ISA memory support enabled\n",
286 hose->dn->full_name);
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100287}
288
289static void __init ppc4xx_configure_pci_PTMs(struct pci_controller *hose,
290 void __iomem *reg,
291 const struct resource *res)
292{
Joe Perches28f65c112011-06-09 09:13:32 -0700293 resource_size_t size = resource_size(res);
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100294 u32 sa;
295
296 /* Calculate window size */
297 sa = (0xffffffffu << ilog2(size)) | 1;
298 sa |= 0x1;
299
300 /* RAM is always at 0 local for now */
301 writel(0, reg + PCIL0_PTM1LA);
302 writel(sa, reg + PCIL0_PTM1MS);
303
304 /* Map on PCI side */
305 early_write_config_dword(hose, hose->first_busno, 0,
306 PCI_BASE_ADDRESS_1, res->start);
307 early_write_config_dword(hose, hose->first_busno, 0,
308 PCI_BASE_ADDRESS_2, 0x00000000);
309 early_write_config_word(hose, hose->first_busno, 0,
310 PCI_COMMAND, 0x0006);
311}
312
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100313static void __init ppc4xx_probe_pci_bridge(struct device_node *np)
314{
315 /* NYI */
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100316 struct resource rsrc_cfg;
317 struct resource rsrc_reg;
318 struct resource dma_window;
319 struct pci_controller *hose = NULL;
320 void __iomem *reg = NULL;
321 const int *bus_range;
322 int primary = 0;
323
Matthias Fuchs5a013fc2008-09-10 05:55:46 +0000324 /* Check if device is enabled */
325 if (!of_device_is_available(np)) {
326 printk(KERN_INFO "%s: Port disabled via device-tree\n",
327 np->full_name);
328 return;
329 }
330
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100331 /* Fetch config space registers address */
332 if (of_address_to_resource(np, 0, &rsrc_cfg)) {
Matthias Fuchs5a013fc2008-09-10 05:55:46 +0000333 printk(KERN_ERR "%s: Can't get PCI config register base !",
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100334 np->full_name);
335 return;
336 }
337 /* Fetch host bridge internal registers address */
338 if (of_address_to_resource(np, 3, &rsrc_reg)) {
339 printk(KERN_ERR "%s: Can't get PCI internal register base !",
340 np->full_name);
341 return;
342 }
343
344 /* Check if primary bridge */
345 if (of_get_property(np, "primary", NULL))
346 primary = 1;
347
348 /* Get bus range if any */
349 bus_range = of_get_property(np, "bus-range", NULL);
350
351 /* Map registers */
Joe Perches28f65c112011-06-09 09:13:32 -0700352 reg = ioremap(rsrc_reg.start, resource_size(&rsrc_reg));
Benjamin Herrenschmidtc839e0e2007-12-21 15:39:23 +1100353 if (reg == NULL) {
354 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
355 goto fail;
356 }
357
358 /* Allocate the host controller data structure */
359 hose = pcibios_alloc_controller(np);
360 if (!hose)
361 goto fail;
362
363 hose->first_busno = bus_range ? bus_range[0] : 0x0;
364 hose->last_busno = bus_range ? bus_range[1] : 0xff;
365
366 /* Setup config space */
367 setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
368
369 /* Disable all windows */
370 writel(0, reg + PCIL0_PMM0MA);
371 writel(0, reg + PCIL0_PMM1MA);
372 writel(0, reg + PCIL0_PMM2MA);
373 writel(0, reg + PCIL0_PTM1MS);
374 writel(0, reg + PCIL0_PTM2MS);
375
376 /* Parse outbound mapping resources */
377 pci_process_bridge_OF_ranges(hose, np, primary);
378
379 /* Parse inbound mapping resources */
380 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
381 goto fail;
382
383 /* Configure outbound ranges POMs */
384 ppc4xx_configure_pci_PMMs(hose, reg);
385
386 /* Configure inbound ranges PIMs */
387 ppc4xx_configure_pci_PTMs(hose, reg, &dma_window);
388
389 /* We don't need the registers anymore */
390 iounmap(reg);
391 return;
392
393 fail:
394 if (hose)
395 pcibios_free_controller(hose);
396 if (reg)
397 iounmap(reg);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100398}
399
400/*
401 * 4xx PCI-X part
402 */
403
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000404static int __init ppc4xx_setup_one_pcix_POM(struct pci_controller *hose,
405 void __iomem *reg,
406 u64 plb_addr,
407 u64 pci_addr,
408 u64 size,
409 unsigned int flags,
410 int index)
411{
412 u32 lah, lal, pciah, pcial, sa;
413
414 if (!is_power_of_2(size) || size < 0x1000 ||
415 (plb_addr & (size - 1)) != 0) {
416 printk(KERN_WARNING "%s: Resource out of range\n",
417 hose->dn->full_name);
418 return -1;
419 }
420
421 /* Calculate register values */
422 lah = RES_TO_U32_HIGH(plb_addr);
423 lal = RES_TO_U32_LOW(plb_addr);
424 pciah = RES_TO_U32_HIGH(pci_addr);
425 pcial = RES_TO_U32_LOW(pci_addr);
426 sa = (0xffffffffu << ilog2(size)) | 0x1;
427
428 /* Program register values */
429 if (index == 0) {
430 writel(lah, reg + PCIX0_POM0LAH);
431 writel(lal, reg + PCIX0_POM0LAL);
432 writel(pciah, reg + PCIX0_POM0PCIAH);
433 writel(pcial, reg + PCIX0_POM0PCIAL);
434 writel(sa, reg + PCIX0_POM0SA);
435 } else {
436 writel(lah, reg + PCIX0_POM1LAH);
437 writel(lal, reg + PCIX0_POM1LAL);
438 writel(pciah, reg + PCIX0_POM1PCIAH);
439 writel(pcial, reg + PCIX0_POM1PCIAL);
440 writel(sa, reg + PCIX0_POM1SA);
441 }
442
443 return 0;
444}
445
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100446static void __init ppc4xx_configure_pcix_POMs(struct pci_controller *hose,
447 void __iomem *reg)
448{
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000449 int i, j, found_isa_hole = 0;
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100450
451 /* Setup outbound memory windows */
452 for (i = j = 0; i < 3; i++) {
453 struct resource *res = &hose->mem_resources[i];
454
455 /* we only care about memory windows */
456 if (!(res->flags & IORESOURCE_MEM))
457 continue;
458 if (j > 1) {
459 printk(KERN_WARNING "%s: Too many ranges\n",
460 hose->dn->full_name);
461 break;
462 }
463
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000464 /* Configure the resource */
465 if (ppc4xx_setup_one_pcix_POM(hose, reg,
466 res->start,
467 res->start - hose->pci_mem_offset,
Joe Perches28f65c112011-06-09 09:13:32 -0700468 resource_size(res),
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000469 res->flags,
470 j) == 0) {
471 j++;
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100472
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000473 /* If the resource PCI address is 0 then we have our
474 * ISA memory hole
475 */
476 if (res->start == hose->pci_mem_offset)
477 found_isa_hole = 1;
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100478 }
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100479 }
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +0000480
481 /* Handle ISA memory hole if not already covered */
482 if (j <= 1 && !found_isa_hole && hose->isa_mem_size)
483 if (ppc4xx_setup_one_pcix_POM(hose, reg, hose->isa_mem_phys, 0,
484 hose->isa_mem_size, 0, j) == 0)
485 printk(KERN_INFO "%s: Legacy ISA memory support enabled\n",
486 hose->dn->full_name);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100487}
488
489static void __init ppc4xx_configure_pcix_PIMs(struct pci_controller *hose,
490 void __iomem *reg,
491 const struct resource *res,
492 int big_pim,
493 int enable_msi_hole)
494{
Joe Perches28f65c112011-06-09 09:13:32 -0700495 resource_size_t size = resource_size(res);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100496 u32 sa;
497
498 /* RAM is always at 0 */
499 writel(0x00000000, reg + PCIX0_PIM0LAH);
500 writel(0x00000000, reg + PCIX0_PIM0LAL);
501
502 /* Calculate window size */
503 sa = (0xffffffffu << ilog2(size)) | 1;
504 sa |= 0x1;
505 if (res->flags & IORESOURCE_PREFETCH)
506 sa |= 0x2;
507 if (enable_msi_hole)
508 sa |= 0x4;
509 writel(sa, reg + PCIX0_PIM0SA);
510 if (big_pim)
511 writel(0xffffffff, reg + PCIX0_PIM0SAH);
512
513 /* Map on PCI side */
514 writel(0x00000000, reg + PCIX0_BAR0H);
515 writel(res->start, reg + PCIX0_BAR0L);
516 writew(0x0006, reg + PCIX0_COMMAND);
517}
518
519static void __init ppc4xx_probe_pcix_bridge(struct device_node *np)
520{
521 struct resource rsrc_cfg;
522 struct resource rsrc_reg;
523 struct resource dma_window;
524 struct pci_controller *hose = NULL;
525 void __iomem *reg = NULL;
526 const int *bus_range;
527 int big_pim = 0, msi = 0, primary = 0;
528
529 /* Fetch config space registers address */
530 if (of_address_to_resource(np, 0, &rsrc_cfg)) {
531 printk(KERN_ERR "%s:Can't get PCI-X config register base !",
532 np->full_name);
533 return;
534 }
535 /* Fetch host bridge internal registers address */
536 if (of_address_to_resource(np, 3, &rsrc_reg)) {
537 printk(KERN_ERR "%s: Can't get PCI-X internal register base !",
538 np->full_name);
539 return;
540 }
541
542 /* Check if it supports large PIMs (440GX) */
543 if (of_get_property(np, "large-inbound-windows", NULL))
544 big_pim = 1;
545
546 /* Check if we should enable MSIs inbound hole */
547 if (of_get_property(np, "enable-msi-hole", NULL))
548 msi = 1;
549
550 /* Check if primary bridge */
551 if (of_get_property(np, "primary", NULL))
552 primary = 1;
553
554 /* Get bus range if any */
555 bus_range = of_get_property(np, "bus-range", NULL);
556
557 /* Map registers */
Joe Perches28f65c112011-06-09 09:13:32 -0700558 reg = ioremap(rsrc_reg.start, resource_size(&rsrc_reg));
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100559 if (reg == NULL) {
560 printk(KERN_ERR "%s: Can't map registers !", np->full_name);
561 goto fail;
562 }
563
564 /* Allocate the host controller data structure */
565 hose = pcibios_alloc_controller(np);
566 if (!hose)
567 goto fail;
568
569 hose->first_busno = bus_range ? bus_range[0] : 0x0;
570 hose->last_busno = bus_range ? bus_range[1] : 0xff;
571
572 /* Setup config space */
Stef van Osd234b3c2010-01-20 03:59:39 +0000573 setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4,
574 PPC_INDIRECT_TYPE_SET_CFG_TYPE);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100575
576 /* Disable all windows */
577 writel(0, reg + PCIX0_POM0SA);
578 writel(0, reg + PCIX0_POM1SA);
579 writel(0, reg + PCIX0_POM2SA);
580 writel(0, reg + PCIX0_PIM0SA);
581 writel(0, reg + PCIX0_PIM1SA);
582 writel(0, reg + PCIX0_PIM2SA);
583 if (big_pim) {
584 writel(0, reg + PCIX0_PIM0SAH);
585 writel(0, reg + PCIX0_PIM2SAH);
586 }
587
588 /* Parse outbound mapping resources */
589 pci_process_bridge_OF_ranges(hose, np, primary);
590
591 /* Parse inbound mapping resources */
592 if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
593 goto fail;
594
595 /* Configure outbound ranges POMs */
596 ppc4xx_configure_pcix_POMs(hose, reg);
597
598 /* Configure inbound ranges PIMs */
599 ppc4xx_configure_pcix_PIMs(hose, reg, &dma_window, big_pim, msi);
600
601 /* We don't need the registers anymore */
602 iounmap(reg);
603 return;
604
605 fail:
606 if (hose)
607 pcibios_free_controller(hose);
608 if (reg)
609 iounmap(reg);
610}
611
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100612#ifdef CONFIG_PPC4xx_PCI_EXPRESS
613
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100614/*
615 * 4xx PCI-Express part
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100616 *
617 * We support 3 parts currently based on the compatible property:
618 *
Stefan Roeseaccf5ef2007-12-21 15:39:38 +1100619 * ibm,plb-pciex-440spe
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100620 * ibm,plb-pciex-405ex
Stefan Roese66b7e502008-02-24 08:08:27 +1100621 * ibm,plb-pciex-460ex
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100622 *
623 * Anything else will be rejected for now as they are all subtly
624 * different unfortunately.
625 *
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +1100626 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100627
Stefan Roese78994e22007-12-31 16:41:15 +1100628#define MAX_PCIE_BUS_MAPPED 0x40
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100629
630struct ppc4xx_pciex_port
631{
632 struct pci_controller *hose;
633 struct device_node *node;
634 unsigned int index;
635 int endpoint;
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100636 int link;
637 int has_ibpre;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100638 unsigned int sdr_base;
639 dcr_host_t dcrs;
640 struct resource cfg_space;
641 struct resource utl_regs;
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100642 void __iomem *utl_base;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100643};
644
645static struct ppc4xx_pciex_port *ppc4xx_pciex_ports;
646static unsigned int ppc4xx_pciex_port_count;
647
648struct ppc4xx_pciex_hwops
649{
Tony Breeds81158462011-11-30 21:39:18 +0000650 bool want_sdr;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100651 int (*core_init)(struct device_node *np);
652 int (*port_init_hw)(struct ppc4xx_pciex_port *port);
653 int (*setup_utl)(struct ppc4xx_pciex_port *port);
Tony Breeds112d1fe2011-06-30 20:44:24 +0000654 void (*check_link)(struct ppc4xx_pciex_port *port);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100655};
656
657static struct ppc4xx_pciex_hwops *ppc4xx_pciex_hwops;
658
Tony Breeds112d1fe2011-06-30 20:44:24 +0000659static int __init ppc4xx_pciex_wait_on_sdr(struct ppc4xx_pciex_port *port,
660 unsigned int sdr_offset,
661 unsigned int mask,
662 unsigned int value,
663 int timeout_ms)
664{
665 u32 val;
666
667 while(timeout_ms--) {
668 val = mfdcri(SDR0, port->sdr_base + sdr_offset);
669 if ((val & mask) == value) {
670 pr_debug("PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
671 port->index, sdr_offset, timeout_ms, val);
672 return 0;
673 }
674 msleep(1);
675 }
676 return -1;
677}
678
679static int __init ppc4xx_pciex_port_reset_sdr(struct ppc4xx_pciex_port *port)
680{
Tony Breeds112d1fe2011-06-30 20:44:24 +0000681 /* Wait for reset to complete */
682 if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS, 1 << 20, 0, 10)) {
683 printk(KERN_WARNING "PCIE%d: PGRST failed\n",
684 port->index);
685 return -1;
686 }
687 return 0;
688}
689
Benjamin Herrenschmidt883a8052011-08-05 15:59:40 +1000690
Tony Breeds112d1fe2011-06-30 20:44:24 +0000691static void __init ppc4xx_pciex_check_link_sdr(struct ppc4xx_pciex_port *port)
692{
Josh Boyera8e616b2011-07-12 16:37:50 -0400693 printk(KERN_INFO "PCIE%d: Checking link...\n", port->index);
694
Tony Breeds112d1fe2011-06-30 20:44:24 +0000695 /* Check for card presence detect if supported, if not, just wait for
696 * link unconditionally.
697 *
698 * note that we don't fail if there is no link, we just filter out
699 * config space accesses. That way, it will be easier to implement
700 * hotplug later on.
701 */
702 if (!port->has_ibpre ||
703 !ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
704 1 << 28, 1 << 28, 100)) {
705 printk(KERN_INFO
706 "PCIE%d: Device detected, waiting for link...\n",
707 port->index);
708 if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
709 0x1000, 0x1000, 2000))
710 printk(KERN_WARNING
711 "PCIE%d: Link up failed\n", port->index);
712 else {
713 printk(KERN_INFO
714 "PCIE%d: link is up !\n", port->index);
715 port->link = 1;
716 }
717 } else
718 printk(KERN_INFO "PCIE%d: No device detected.\n", port->index);
719}
720
Benjamin Herrenschmidt883a8052011-08-05 15:59:40 +1000721#ifdef CONFIG_44x
722
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100723/* Check various reset bits of the 440SPe PCIe core */
724static int __init ppc440spe_pciex_check_reset(struct device_node *np)
725{
726 u32 valPE0, valPE1, valPE2;
727 int err = 0;
728
729 /* SDR0_PEGPLLLCT1 reset */
730 if (!(mfdcri(SDR0, PESDR0_PLLLCT1) & 0x01000000)) {
731 /*
732 * the PCIe core was probably already initialised
733 * by firmware - let's re-reset RCSSET regs
734 *
735 * -- Shouldn't we also re-reset the whole thing ? -- BenH
736 */
737 pr_debug("PCIE: SDR0_PLLLCT1 already reset.\n");
738 mtdcri(SDR0, PESDR0_440SPE_RCSSET, 0x01010000);
739 mtdcri(SDR0, PESDR1_440SPE_RCSSET, 0x01010000);
740 mtdcri(SDR0, PESDR2_440SPE_RCSSET, 0x01010000);
741 }
742
743 valPE0 = mfdcri(SDR0, PESDR0_440SPE_RCSSET);
744 valPE1 = mfdcri(SDR0, PESDR1_440SPE_RCSSET);
745 valPE2 = mfdcri(SDR0, PESDR2_440SPE_RCSSET);
746
747 /* SDR0_PExRCSSET rstgu */
748 if (!(valPE0 & 0x01000000) ||
749 !(valPE1 & 0x01000000) ||
750 !(valPE2 & 0x01000000)) {
751 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstgu error\n");
752 err = -1;
753 }
754
755 /* SDR0_PExRCSSET rstdl */
756 if (!(valPE0 & 0x00010000) ||
757 !(valPE1 & 0x00010000) ||
758 !(valPE2 & 0x00010000)) {
759 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstdl error\n");
760 err = -1;
761 }
762
763 /* SDR0_PExRCSSET rstpyn */
764 if ((valPE0 & 0x00001000) ||
765 (valPE1 & 0x00001000) ||
766 (valPE2 & 0x00001000)) {
767 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstpyn error\n");
768 err = -1;
769 }
770
771 /* SDR0_PExRCSSET hldplb */
772 if ((valPE0 & 0x10000000) ||
773 (valPE1 & 0x10000000) ||
774 (valPE2 & 0x10000000)) {
775 printk(KERN_INFO "PCIE: SDR0_PExRCSSET hldplb error\n");
776 err = -1;
777 }
778
779 /* SDR0_PExRCSSET rdy */
780 if ((valPE0 & 0x00100000) ||
781 (valPE1 & 0x00100000) ||
782 (valPE2 & 0x00100000)) {
783 printk(KERN_INFO "PCIE: SDR0_PExRCSSET rdy error\n");
784 err = -1;
785 }
786
787 /* SDR0_PExRCSSET shutdown */
788 if ((valPE0 & 0x00000100) ||
789 (valPE1 & 0x00000100) ||
790 (valPE2 & 0x00000100)) {
791 printk(KERN_INFO "PCIE: SDR0_PExRCSSET shutdown error\n");
792 err = -1;
793 }
794
795 return err;
796}
797
798/* Global PCIe core initializations for 440SPe core */
799static int __init ppc440spe_pciex_core_init(struct device_node *np)
800{
801 int time_out = 20;
802
803 /* Set PLL clock receiver to LVPECL */
Valentine Barshak6e42b212008-03-07 01:34:52 +1100804 dcri_clrset(SDR0, PESDR0_PLLLCT1, 0, 1 << 28);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100805
806 /* Shouldn't we do all the calibration stuff etc... here ? */
807 if (ppc440spe_pciex_check_reset(np))
808 return -ENXIO;
809
810 if (!(mfdcri(SDR0, PESDR0_PLLLCT2) & 0x10000)) {
811 printk(KERN_INFO "PCIE: PESDR_PLLCT2 resistance calibration "
812 "failed (0x%08x)\n",
813 mfdcri(SDR0, PESDR0_PLLLCT2));
814 return -1;
815 }
816
817 /* De-assert reset of PCIe PLL, wait for lock */
Valentine Barshak6e42b212008-03-07 01:34:52 +1100818 dcri_clrset(SDR0, PESDR0_PLLLCT1, 1 << 24, 0);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100819 udelay(3);
820
821 while (time_out) {
822 if (!(mfdcri(SDR0, PESDR0_PLLLCT3) & 0x10000000)) {
823 time_out--;
824 udelay(1);
825 } else
826 break;
827 }
828 if (!time_out) {
829 printk(KERN_INFO "PCIE: VCO output not locked\n");
830 return -1;
831 }
832
833 pr_debug("PCIE initialization OK\n");
834
835 return 3;
836}
837
Tony Breeds9c57a322011-08-10 20:16:54 +0000838static int __init ppc440spe_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100839{
840 u32 val = 1 << 24;
841
842 if (port->endpoint)
843 val = PTYPE_LEGACY_ENDPOINT << 20;
844 else
845 val = PTYPE_ROOT_PORT << 20;
846
847 if (port->index == 0)
848 val |= LNKW_X8 << 12;
849 else
850 val |= LNKW_X4 << 12;
851
852 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
853 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x20222222);
Stefan Roeseaccf5ef2007-12-21 15:39:38 +1100854 if (ppc440spe_revA())
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100855 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x11000000);
856 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL0SET1, 0x35000000);
857 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL1SET1, 0x35000000);
858 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL2SET1, 0x35000000);
859 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL3SET1, 0x35000000);
860 if (port->index == 0) {
861 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL4SET1,
862 0x35000000);
863 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL5SET1,
864 0x35000000);
865 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL6SET1,
866 0x35000000);
867 mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL7SET1,
868 0x35000000);
869 }
Valentine Barshak6e42b212008-03-07 01:34:52 +1100870 dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET,
871 (1 << 24) | (1 << 16), 1 << 12);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100872
Tony Breeds112d1fe2011-06-30 20:44:24 +0000873 return ppc4xx_pciex_port_reset_sdr(port);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100874}
875
Tony Breeds9c57a322011-08-10 20:16:54 +0000876static int __init ppc440speA_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100877{
878 return ppc440spe_pciex_init_port_hw(port);
879}
880
Tony Breeds9c57a322011-08-10 20:16:54 +0000881static int __init ppc440speB_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100882{
883 int rc = ppc440spe_pciex_init_port_hw(port);
884
885 port->has_ibpre = 1;
886
887 return rc;
888}
889
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100890static int ppc440speA_pciex_init_utl(struct ppc4xx_pciex_port *port)
891{
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100892 /* XXX Check what that value means... I hate magic */
893 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x68782800);
894
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100895 /*
896 * Set buffer allocations and then assert VRB and TXE.
897 */
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100898 out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
899 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
900 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x10000000);
901 out_be32(port->utl_base + PEUTL_PBBSZ, 0x53000000);
902 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x08000000);
903 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x10000000);
904 out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
905 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100906
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100907 return 0;
908}
909
910static int ppc440speB_pciex_init_utl(struct ppc4xx_pciex_port *port)
911{
912 /* Report CRS to the operating system */
913 out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100914
915 return 0;
916}
917
918static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops __initdata =
919{
Tony Breeds81158462011-11-30 21:39:18 +0000920 .want_sdr = true,
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100921 .core_init = ppc440spe_pciex_core_init,
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100922 .port_init_hw = ppc440speA_pciex_init_port_hw,
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100923 .setup_utl = ppc440speA_pciex_init_utl,
Tony Breeds112d1fe2011-06-30 20:44:24 +0000924 .check_link = ppc4xx_pciex_check_link_sdr,
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100925};
926
927static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata =
928{
Tony Breeds81158462011-11-30 21:39:18 +0000929 .want_sdr = true,
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100930 .core_init = ppc440spe_pciex_core_init,
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +1100931 .port_init_hw = ppc440speB_pciex_init_port_hw,
932 .setup_utl = ppc440speB_pciex_init_utl,
Tony Breeds112d1fe2011-06-30 20:44:24 +0000933 .check_link = ppc4xx_pciex_check_link_sdr,
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +1100934};
935
Stefan Roese66b7e502008-02-24 08:08:27 +1100936static int __init ppc460ex_pciex_core_init(struct device_node *np)
937{
938 /* Nothing to do, return 2 ports */
939 return 2;
940}
941
Tony Breeds9c57a322011-08-10 20:16:54 +0000942static int __init ppc460ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
Stefan Roese66b7e502008-02-24 08:08:27 +1100943{
944 u32 val;
945 u32 utlset1;
946
Stefan Roese5f919252008-04-02 00:45:00 +1100947 if (port->endpoint)
Stefan Roese66b7e502008-02-24 08:08:27 +1100948 val = PTYPE_LEGACY_ENDPOINT << 20;
Stefan Roese5f919252008-04-02 00:45:00 +1100949 else
Stefan Roese66b7e502008-02-24 08:08:27 +1100950 val = PTYPE_ROOT_PORT << 20;
Stefan Roese66b7e502008-02-24 08:08:27 +1100951
952 if (port->index == 0) {
953 val |= LNKW_X1 << 12;
Stefan Roese5f919252008-04-02 00:45:00 +1100954 utlset1 = 0x20000000;
Stefan Roese66b7e502008-02-24 08:08:27 +1100955 } else {
956 val |= LNKW_X4 << 12;
Stefan Roese5f919252008-04-02 00:45:00 +1100957 utlset1 = 0x20101101;
Stefan Roese66b7e502008-02-24 08:08:27 +1100958 }
959
960 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
961 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, utlset1);
962 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01210000);
963
964 switch (port->index) {
965 case 0:
966 mtdcri(SDR0, PESDR0_460EX_L0CDRCTL, 0x00003230);
Tirumala R Marrie30c9872008-08-21 18:53:34 +0000967 mtdcri(SDR0, PESDR0_460EX_L0DRV, 0x00000130);
Stefan Roese66b7e502008-02-24 08:08:27 +1100968 mtdcri(SDR0, PESDR0_460EX_L0CLK, 0x00000006);
969
970 mtdcri(SDR0, PESDR0_460EX_PHY_CTL_RST,0x10000000);
971 break;
972
973 case 1:
974 mtdcri(SDR0, PESDR1_460EX_L0CDRCTL, 0x00003230);
975 mtdcri(SDR0, PESDR1_460EX_L1CDRCTL, 0x00003230);
976 mtdcri(SDR0, PESDR1_460EX_L2CDRCTL, 0x00003230);
977 mtdcri(SDR0, PESDR1_460EX_L3CDRCTL, 0x00003230);
Tirumala R Marrie30c9872008-08-21 18:53:34 +0000978 mtdcri(SDR0, PESDR1_460EX_L0DRV, 0x00000130);
979 mtdcri(SDR0, PESDR1_460EX_L1DRV, 0x00000130);
980 mtdcri(SDR0, PESDR1_460EX_L2DRV, 0x00000130);
981 mtdcri(SDR0, PESDR1_460EX_L3DRV, 0x00000130);
Stefan Roese66b7e502008-02-24 08:08:27 +1100982 mtdcri(SDR0, PESDR1_460EX_L0CLK, 0x00000006);
983 mtdcri(SDR0, PESDR1_460EX_L1CLK, 0x00000006);
984 mtdcri(SDR0, PESDR1_460EX_L2CLK, 0x00000006);
985 mtdcri(SDR0, PESDR1_460EX_L3CLK, 0x00000006);
986
987 mtdcri(SDR0, PESDR1_460EX_PHY_CTL_RST,0x10000000);
988 break;
989 }
990
991 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
992 mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) |
993 (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTPYN));
994
995 /* Poll for PHY reset */
996 /* XXX FIXME add timeout */
997 switch (port->index) {
998 case 0:
999 while (!(mfdcri(SDR0, PESDR0_460EX_RSTSTA) & 0x1))
1000 udelay(10);
1001 break;
1002 case 1:
1003 while (!(mfdcri(SDR0, PESDR1_460EX_RSTSTA) & 0x1))
1004 udelay(10);
1005 break;
1006 }
1007
1008 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
1009 (mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) &
1010 ~(PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL)) |
1011 PESDRx_RCSSET_RSTPYN);
1012
1013 port->has_ibpre = 1;
1014
Tony Breeds112d1fe2011-06-30 20:44:24 +00001015 return ppc4xx_pciex_port_reset_sdr(port);
Stefan Roese66b7e502008-02-24 08:08:27 +11001016}
1017
1018static int ppc460ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
1019{
1020 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
1021
1022 /*
1023 * Set buffer allocations and then assert VRB and TXE.
1024 */
1025 out_be32(port->utl_base + PEUTL_PBCTL, 0x0800000c);
1026 out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
1027 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
1028 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
1029 out_be32(port->utl_base + PEUTL_PBBSZ, 0x00000000);
1030 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
1031 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
1032 out_be32(port->utl_base + PEUTL_RCIRQEN,0x00f00000);
1033 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
1034
1035 return 0;
1036}
1037
1038static struct ppc4xx_pciex_hwops ppc460ex_pcie_hwops __initdata =
1039{
Tony Breeds81158462011-11-30 21:39:18 +00001040 .want_sdr = true,
Stefan Roese66b7e502008-02-24 08:08:27 +11001041 .core_init = ppc460ex_pciex_core_init,
1042 .port_init_hw = ppc460ex_pciex_init_port_hw,
1043 .setup_utl = ppc460ex_pciex_init_utl,
Tony Breeds112d1fe2011-06-30 20:44:24 +00001044 .check_link = ppc4xx_pciex_check_link_sdr,
Stefan Roese66b7e502008-02-24 08:08:27 +11001045};
1046
Tirumala Marrie2efc092009-12-21 22:49:41 +00001047static int __init ppc460sx_pciex_core_init(struct device_node *np)
1048{
1049 /* HSS drive amplitude */
1050 mtdcri(SDR0, PESDR0_460SX_HSSL0DAMP, 0xB9843211);
1051 mtdcri(SDR0, PESDR0_460SX_HSSL1DAMP, 0xB9843211);
1052 mtdcri(SDR0, PESDR0_460SX_HSSL2DAMP, 0xB9843211);
1053 mtdcri(SDR0, PESDR0_460SX_HSSL3DAMP, 0xB9843211);
1054 mtdcri(SDR0, PESDR0_460SX_HSSL4DAMP, 0xB9843211);
1055 mtdcri(SDR0, PESDR0_460SX_HSSL5DAMP, 0xB9843211);
1056 mtdcri(SDR0, PESDR0_460SX_HSSL6DAMP, 0xB9843211);
1057 mtdcri(SDR0, PESDR0_460SX_HSSL7DAMP, 0xB9843211);
1058
1059 mtdcri(SDR0, PESDR1_460SX_HSSL0DAMP, 0xB9843211);
1060 mtdcri(SDR0, PESDR1_460SX_HSSL1DAMP, 0xB9843211);
1061 mtdcri(SDR0, PESDR1_460SX_HSSL2DAMP, 0xB9843211);
1062 mtdcri(SDR0, PESDR1_460SX_HSSL3DAMP, 0xB9843211);
1063
1064 mtdcri(SDR0, PESDR2_460SX_HSSL0DAMP, 0xB9843211);
1065 mtdcri(SDR0, PESDR2_460SX_HSSL1DAMP, 0xB9843211);
1066 mtdcri(SDR0, PESDR2_460SX_HSSL2DAMP, 0xB9843211);
1067 mtdcri(SDR0, PESDR2_460SX_HSSL3DAMP, 0xB9843211);
1068
1069 /* HSS TX pre-emphasis */
1070 mtdcri(SDR0, PESDR0_460SX_HSSL0COEFA, 0xDCB98987);
1071 mtdcri(SDR0, PESDR0_460SX_HSSL1COEFA, 0xDCB98987);
1072 mtdcri(SDR0, PESDR0_460SX_HSSL2COEFA, 0xDCB98987);
1073 mtdcri(SDR0, PESDR0_460SX_HSSL3COEFA, 0xDCB98987);
1074 mtdcri(SDR0, PESDR0_460SX_HSSL4COEFA, 0xDCB98987);
1075 mtdcri(SDR0, PESDR0_460SX_HSSL5COEFA, 0xDCB98987);
1076 mtdcri(SDR0, PESDR0_460SX_HSSL6COEFA, 0xDCB98987);
1077 mtdcri(SDR0, PESDR0_460SX_HSSL7COEFA, 0xDCB98987);
1078
1079 mtdcri(SDR0, PESDR1_460SX_HSSL0COEFA, 0xDCB98987);
1080 mtdcri(SDR0, PESDR1_460SX_HSSL1COEFA, 0xDCB98987);
1081 mtdcri(SDR0, PESDR1_460SX_HSSL2COEFA, 0xDCB98987);
1082 mtdcri(SDR0, PESDR1_460SX_HSSL3COEFA, 0xDCB98987);
1083
1084 mtdcri(SDR0, PESDR2_460SX_HSSL0COEFA, 0xDCB98987);
1085 mtdcri(SDR0, PESDR2_460SX_HSSL1COEFA, 0xDCB98987);
1086 mtdcri(SDR0, PESDR2_460SX_HSSL2COEFA, 0xDCB98987);
1087 mtdcri(SDR0, PESDR2_460SX_HSSL3COEFA, 0xDCB98987);
1088
1089 /* HSS TX calibration control */
1090 mtdcri(SDR0, PESDR0_460SX_HSSL1CALDRV, 0x22222222);
1091 mtdcri(SDR0, PESDR1_460SX_HSSL1CALDRV, 0x22220000);
1092 mtdcri(SDR0, PESDR2_460SX_HSSL1CALDRV, 0x22220000);
1093
1094 /* HSS TX slew control */
1095 mtdcri(SDR0, PESDR0_460SX_HSSSLEW, 0xFFFFFFFF);
1096 mtdcri(SDR0, PESDR1_460SX_HSSSLEW, 0xFFFF0000);
1097 mtdcri(SDR0, PESDR2_460SX_HSSSLEW, 0xFFFF0000);
1098
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001099 /* Set HSS PRBS enabled */
1100 mtdcri(SDR0, PESDR0_460SX_HSSCTLSET, 0x00001130);
1101 mtdcri(SDR0, PESDR2_460SX_HSSCTLSET, 0x00001130);
1102
Tirumala Marrie2efc092009-12-21 22:49:41 +00001103 udelay(100);
1104
1105 /* De-assert PLLRESET */
1106 dcri_clrset(SDR0, PESDR0_PLLLCT2, 0x00000100, 0);
1107
1108 /* Reset DL, UTL, GPL before configuration */
1109 mtdcri(SDR0, PESDR0_460SX_RCSSET,
1110 PESDRx_RCSSET_RSTDL | PESDRx_RCSSET_RSTGU);
1111 mtdcri(SDR0, PESDR1_460SX_RCSSET,
1112 PESDRx_RCSSET_RSTDL | PESDRx_RCSSET_RSTGU);
1113 mtdcri(SDR0, PESDR2_460SX_RCSSET,
1114 PESDRx_RCSSET_RSTDL | PESDRx_RCSSET_RSTGU);
1115
1116 udelay(100);
1117
1118 /*
1119 * If bifurcation is not enabled, u-boot would have disabled the
1120 * third PCIe port
1121 */
1122 if (((mfdcri(SDR0, PESDR1_460SX_HSSCTLSET) & 0x00000001) ==
1123 0x00000001)) {
1124 printk(KERN_INFO "PCI: PCIE bifurcation setup successfully.\n");
1125 printk(KERN_INFO "PCI: Total 3 PCIE ports are present\n");
1126 return 3;
1127 }
1128
1129 printk(KERN_INFO "PCI: Total 2 PCIE ports are present\n");
1130 return 2;
1131}
1132
Tony Breeds9c57a322011-08-10 20:16:54 +00001133static int __init ppc460sx_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
Tirumala Marrie2efc092009-12-21 22:49:41 +00001134{
1135
1136 if (port->endpoint)
1137 dcri_clrset(SDR0, port->sdr_base + PESDRn_UTLSET2,
1138 0x01000000, 0);
1139 else
1140 dcri_clrset(SDR0, port->sdr_base + PESDRn_UTLSET2,
1141 0, 0x01000000);
1142
Tirumala Marrie2efc092009-12-21 22:49:41 +00001143 dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET,
1144 (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL),
1145 PESDRx_RCSSET_RSTPYN);
1146
1147 port->has_ibpre = 1;
1148
Tony Breeds112d1fe2011-06-30 20:44:24 +00001149 return ppc4xx_pciex_port_reset_sdr(port);
Tirumala Marrie2efc092009-12-21 22:49:41 +00001150}
1151
1152static int ppc460sx_pciex_init_utl(struct ppc4xx_pciex_port *port)
1153{
1154 /* Max 128 Bytes */
1155 out_be32 (port->utl_base + PEUTL_PBBSZ, 0x00000000);
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001156 /* Assert VRB and TXE - per datasheet turn off addr validation */
1157 out_be32(port->utl_base + PEUTL_PCTL, 0x80800000);
Tirumala Marrie2efc092009-12-21 22:49:41 +00001158 return 0;
1159}
1160
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001161static void __init ppc460sx_pciex_check_link(struct ppc4xx_pciex_port *port)
1162{
1163 void __iomem *mbase;
1164 int attempt = 50;
1165
1166 port->link = 0;
1167
1168 mbase = ioremap(port->cfg_space.start + 0x10000000, 0x1000);
1169 if (mbase == NULL) {
1170 printk(KERN_ERR "%s: Can't map internal config space !",
1171 port->node->full_name);
1172 goto done;
1173 }
1174
1175 while (attempt && (0 == (in_le32(mbase + PECFG_460SX_DLLSTA)
1176 & PECFG_460SX_DLLSTA_LINKUP))) {
1177 attempt--;
1178 mdelay(10);
1179 }
1180 if (attempt)
1181 port->link = 1;
1182done:
1183 iounmap(mbase);
1184
1185}
1186
Tirumala Marrie2efc092009-12-21 22:49:41 +00001187static struct ppc4xx_pciex_hwops ppc460sx_pcie_hwops __initdata = {
Tony Breeds81158462011-11-30 21:39:18 +00001188 .want_sdr = true,
Tirumala Marrie2efc092009-12-21 22:49:41 +00001189 .core_init = ppc460sx_pciex_core_init,
1190 .port_init_hw = ppc460sx_pciex_init_port_hw,
1191 .setup_utl = ppc460sx_pciex_init_utl,
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001192 .check_link = ppc460sx_pciex_check_link,
Tirumala Marrie2efc092009-12-21 22:49:41 +00001193};
1194
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001195#endif /* CONFIG_44x */
1196
1197#ifdef CONFIG_40x
1198
1199static int __init ppc405ex_pciex_core_init(struct device_node *np)
1200{
1201 /* Nothing to do, return 2 ports */
1202 return 2;
1203}
1204
1205static void ppc405ex_pcie_phy_reset(struct ppc4xx_pciex_port *port)
1206{
1207 /* Assert the PE0_PHY reset */
1208 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01010000);
1209 msleep(1);
1210
1211 /* deassert the PE0_hotreset */
1212 if (port->endpoint)
1213 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01111000);
1214 else
1215 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01101000);
1216
1217 /* poll for phy !reset */
1218 /* XXX FIXME add timeout */
1219 while (!(mfdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSTA) & 0x00001000))
1220 ;
1221
1222 /* deassert the PE0_gpl_utl_reset */
1223 mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x00101000);
1224}
1225
Tony Breeds9c57a322011-08-10 20:16:54 +00001226static int __init ppc405ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001227{
1228 u32 val;
1229
1230 if (port->endpoint)
1231 val = PTYPE_LEGACY_ENDPOINT;
1232 else
1233 val = PTYPE_ROOT_PORT;
1234
1235 mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET,
1236 1 << 24 | val << 20 | LNKW_X1 << 12);
1237
1238 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x00000000);
1239 mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01010000);
1240 mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET1, 0x720F0000);
1241 mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET2, 0x70600003);
1242
1243 /*
1244 * Only reset the PHY when no link is currently established.
1245 * This is for the Atheros PCIe board which has problems to establish
1246 * the link (again) after this PHY reset. All other currently tested
1247 * PCIe boards don't show this problem.
1248 * This has to be re-tested and fixed in a later release!
1249 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001250 val = mfdcri(SDR0, port->sdr_base + PESDRn_LOOP);
1251 if (!(val & 0x00001000))
1252 ppc405ex_pcie_phy_reset(port);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001253
1254 dcr_write(port->dcrs, DCRO_PEGPL_CFG, 0x10000000); /* guarded on */
1255
Stefan Roese55aaf6e2007-12-07 20:34:34 +11001256 port->has_ibpre = 1;
1257
Tony Breeds112d1fe2011-06-30 20:44:24 +00001258 return ppc4xx_pciex_port_reset_sdr(port);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001259}
1260
1261static int ppc405ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
1262{
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001263 dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
1264
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001265 /*
1266 * Set buffer allocations and then assert VRB and TXE.
1267 */
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001268 out_be32(port->utl_base + PEUTL_OUTTR, 0x02000000);
1269 out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
1270 out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
1271 out_be32(port->utl_base + PEUTL_PBBSZ, 0x21000000);
1272 out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
1273 out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
1274 out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
1275 out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001276
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001277 out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001278
1279 return 0;
1280}
1281
1282static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata =
1283{
Tony Breeds81158462011-11-30 21:39:18 +00001284 .want_sdr = true,
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001285 .core_init = ppc405ex_pciex_core_init,
1286 .port_init_hw = ppc405ex_pciex_init_port_hw,
1287 .setup_utl = ppc405ex_pciex_init_utl,
Tony Breeds112d1fe2011-06-30 20:44:24 +00001288 .check_link = ppc4xx_pciex_check_link_sdr,
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001289};
1290
1291#endif /* CONFIG_40x */
1292
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001293/* Check that the core has been initied and if not, do it */
1294static int __init ppc4xx_pciex_check_core_init(struct device_node *np)
1295{
1296 static int core_init;
1297 int count = -ENODEV;
1298
1299 if (core_init++)
1300 return 0;
1301
1302#ifdef CONFIG_44x
Stefan Roeseaccf5ef2007-12-21 15:39:38 +11001303 if (of_device_is_compatible(np, "ibm,plb-pciex-440spe")) {
1304 if (ppc440spe_revA())
1305 ppc4xx_pciex_hwops = &ppc440speA_pcie_hwops;
1306 else
1307 ppc4xx_pciex_hwops = &ppc440speB_pcie_hwops;
1308 }
Stefan Roese66b7e502008-02-24 08:08:27 +11001309 if (of_device_is_compatible(np, "ibm,plb-pciex-460ex"))
1310 ppc4xx_pciex_hwops = &ppc460ex_pcie_hwops;
Tirumala Marrie2efc092009-12-21 22:49:41 +00001311 if (of_device_is_compatible(np, "ibm,plb-pciex-460sx"))
1312 ppc4xx_pciex_hwops = &ppc460sx_pcie_hwops;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001313#endif /* CONFIG_44x */
1314#ifdef CONFIG_40x
1315 if (of_device_is_compatible(np, "ibm,plb-pciex-405ex"))
1316 ppc4xx_pciex_hwops = &ppc405ex_pcie_hwops;
1317#endif
1318 if (ppc4xx_pciex_hwops == NULL) {
1319 printk(KERN_WARNING "PCIE: unknown host type %s\n",
1320 np->full_name);
1321 return -ENODEV;
1322 }
1323
1324 count = ppc4xx_pciex_hwops->core_init(np);
1325 if (count > 0) {
1326 ppc4xx_pciex_ports =
1327 kzalloc(count * sizeof(struct ppc4xx_pciex_port),
1328 GFP_KERNEL);
1329 if (ppc4xx_pciex_ports) {
1330 ppc4xx_pciex_port_count = count;
1331 return 0;
1332 }
1333 printk(KERN_WARNING "PCIE: failed to allocate ports array\n");
1334 return -ENOMEM;
1335 }
1336 return -ENODEV;
1337}
1338
1339static void __init ppc4xx_pciex_port_init_mapping(struct ppc4xx_pciex_port *port)
1340{
1341 /* We map PCI Express configuration based on the reg property */
1342 dcr_write(port->dcrs, DCRO_PEGPL_CFGBAH,
1343 RES_TO_U32_HIGH(port->cfg_space.start));
1344 dcr_write(port->dcrs, DCRO_PEGPL_CFGBAL,
1345 RES_TO_U32_LOW(port->cfg_space.start));
1346
1347 /* XXX FIXME: Use size from reg property. For now, map 512M */
1348 dcr_write(port->dcrs, DCRO_PEGPL_CFGMSK, 0xe0000001);
1349
1350 /* We map UTL registers based on the reg property */
1351 dcr_write(port->dcrs, DCRO_PEGPL_REGBAH,
1352 RES_TO_U32_HIGH(port->utl_regs.start));
1353 dcr_write(port->dcrs, DCRO_PEGPL_REGBAL,
1354 RES_TO_U32_LOW(port->utl_regs.start));
1355
1356 /* XXX FIXME: Use size from reg property */
1357 dcr_write(port->dcrs, DCRO_PEGPL_REGMSK, 0x00007001);
1358
1359 /* Disable all other outbound windows */
1360 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, 0);
1361 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, 0);
1362 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0);
1363 dcr_write(port->dcrs, DCRO_PEGPL_MSGMSK, 0);
1364}
1365
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001366static int __init ppc4xx_pciex_port_init(struct ppc4xx_pciex_port *port)
1367{
1368 int rc = 0;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001369
1370 /* Init HW */
1371 if (ppc4xx_pciex_hwops->port_init_hw)
1372 rc = ppc4xx_pciex_hwops->port_init_hw(port);
1373 if (rc != 0)
1374 return rc;
1375
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001376 /*
1377 * Initialize mapping: disable all regions and configure
1378 * CFG and REG regions based on resources in the device tree
1379 */
1380 ppc4xx_pciex_port_init_mapping(port);
1381
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001382 if (ppc4xx_pciex_hwops->check_link)
1383 ppc4xx_pciex_hwops->check_link(port);
1384
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001385 /*
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001386 * Map UTL
1387 */
1388 port->utl_base = ioremap(port->utl_regs.start, 0x100);
1389 BUG_ON(port->utl_base == NULL);
1390
1391 /*
1392 * Setup UTL registers --BenH.
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001393 */
1394 if (ppc4xx_pciex_hwops->setup_utl)
1395 ppc4xx_pciex_hwops->setup_utl(port);
1396
1397 /*
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001398 * Check for VC0 active or PLL Locked and assert RDY.
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001399 */
Tony Breeds112d1fe2011-06-30 20:44:24 +00001400 if (port->sdr_base) {
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001401 if (of_device_is_compatible(port->node,
1402 "ibm,plb-pciex-460sx")){
1403 if (port->link && ppc4xx_pciex_wait_on_sdr(port,
1404 PESDRn_RCSSTS,
1405 1 << 12, 1 << 12, 5000)) {
1406 printk(KERN_INFO "PCIE%d: PLL not locked\n",
1407 port->index);
1408 port->link = 0;
1409 }
1410 } else if (port->link &&
1411 ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS,
1412 1 << 16, 1 << 16, 5000)) {
1413 printk(KERN_INFO "PCIE%d: VC0 not active\n",
1414 port->index);
Tony Breeds112d1fe2011-06-30 20:44:24 +00001415 port->link = 0;
1416 }
1417
1418 dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET, 0, 1 << 20);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001419 }
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001420
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001421 msleep(100);
1422
1423 return 0;
1424}
1425
1426static int ppc4xx_pciex_validate_bdf(struct ppc4xx_pciex_port *port,
1427 struct pci_bus *bus,
1428 unsigned int devfn)
1429{
1430 static int message;
1431
1432 /* Endpoint can not generate upstream(remote) config cycles */
1433 if (port->endpoint && bus->number != port->hose->first_busno)
1434 return PCIBIOS_DEVICE_NOT_FOUND;
1435
1436 /* Check we are within the mapped range */
1437 if (bus->number > port->hose->last_busno) {
1438 if (!message) {
1439 printk(KERN_WARNING "Warning! Probing bus %u"
1440 " out of range !\n", bus->number);
1441 message++;
1442 }
1443 return PCIBIOS_DEVICE_NOT_FOUND;
1444 }
1445
1446 /* The root complex has only one device / function */
1447 if (bus->number == port->hose->first_busno && devfn != 0)
1448 return PCIBIOS_DEVICE_NOT_FOUND;
1449
1450 /* The other side of the RC has only one device as well */
1451 if (bus->number == (port->hose->first_busno + 1) &&
1452 PCI_SLOT(devfn) != 0)
1453 return PCIBIOS_DEVICE_NOT_FOUND;
1454
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001455 /* Check if we have a link */
1456 if ((bus->number != port->hose->first_busno) && !port->link)
1457 return PCIBIOS_DEVICE_NOT_FOUND;
1458
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001459 return 0;
1460}
1461
1462static void __iomem *ppc4xx_pciex_get_config_base(struct ppc4xx_pciex_port *port,
1463 struct pci_bus *bus,
1464 unsigned int devfn)
1465{
1466 int relbus;
1467
1468 /* Remove the casts when we finally remove the stupid volatile
1469 * in struct pci_controller
1470 */
1471 if (bus->number == port->hose->first_busno)
1472 return (void __iomem *)port->hose->cfg_addr;
1473
1474 relbus = bus->number - (port->hose->first_busno + 1);
1475 return (void __iomem *)port->hose->cfg_data +
1476 ((relbus << 20) | (devfn << 12));
1477}
1478
1479static int ppc4xx_pciex_read_config(struct pci_bus *bus, unsigned int devfn,
1480 int offset, int len, u32 *val)
1481{
Kumar Galaf159eda2009-04-30 03:10:10 +00001482 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001483 struct ppc4xx_pciex_port *port =
1484 &ppc4xx_pciex_ports[hose->indirect_type];
1485 void __iomem *addr;
1486 u32 gpl_cfg;
1487
1488 BUG_ON(hose != port->hose);
1489
1490 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1491 return PCIBIOS_DEVICE_NOT_FOUND;
1492
1493 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1494
1495 /*
1496 * Reading from configuration space of non-existing device can
1497 * generate transaction errors. For the read duration we suppress
1498 * assertion of machine check exceptions to avoid those.
1499 */
1500 gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1501 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1502
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001503 /* Make sure no CRS is recorded */
1504 out_be32(port->utl_base + PEUTL_RCSTA, 0x00040000);
1505
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001506 switch (len) {
1507 case 1:
1508 *val = in_8((u8 *)(addr + offset));
1509 break;
1510 case 2:
1511 *val = in_le16((u16 *)(addr + offset));
1512 break;
1513 default:
1514 *val = in_le32((u32 *)(addr + offset));
1515 break;
1516 }
1517
1518 pr_debug("pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
1519 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1520 bus->number, hose->first_busno, hose->last_busno,
1521 devfn, offset, len, addr + offset, *val);
1522
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11001523 /* Check for CRS (440SPe rev B does that for us but heh ..) */
1524 if (in_be32(port->utl_base + PEUTL_RCSTA) & 0x00040000) {
1525 pr_debug("Got CRS !\n");
1526 if (len != 4 || offset != 0)
1527 return PCIBIOS_DEVICE_NOT_FOUND;
1528 *val = 0xffff0001;
1529 }
1530
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001531 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1532
1533 return PCIBIOS_SUCCESSFUL;
1534}
1535
1536static int ppc4xx_pciex_write_config(struct pci_bus *bus, unsigned int devfn,
1537 int offset, int len, u32 val)
1538{
Kumar Galaf159eda2009-04-30 03:10:10 +00001539 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001540 struct ppc4xx_pciex_port *port =
1541 &ppc4xx_pciex_ports[hose->indirect_type];
1542 void __iomem *addr;
1543 u32 gpl_cfg;
1544
1545 if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
1546 return PCIBIOS_DEVICE_NOT_FOUND;
1547
1548 addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
1549
1550 /*
1551 * Reading from configuration space of non-existing device can
1552 * generate transaction errors. For the read duration we suppress
1553 * assertion of machine check exceptions to avoid those.
1554 */
1555 gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
1556 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
1557
1558 pr_debug("pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
1559 " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
1560 bus->number, hose->first_busno, hose->last_busno,
1561 devfn, offset, len, addr + offset, val);
1562
1563 switch (len) {
1564 case 1:
1565 out_8((u8 *)(addr + offset), val);
1566 break;
1567 case 2:
1568 out_le16((u16 *)(addr + offset), val);
1569 break;
1570 default:
1571 out_le32((u32 *)(addr + offset), val);
1572 break;
1573 }
1574
1575 dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
1576
1577 return PCIBIOS_SUCCESSFUL;
1578}
1579
1580static struct pci_ops ppc4xx_pciex_pci_ops =
1581{
1582 .read = ppc4xx_pciex_read_config,
1583 .write = ppc4xx_pciex_write_config,
1584};
1585
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +00001586static int __init ppc4xx_setup_one_pciex_POM(struct ppc4xx_pciex_port *port,
1587 struct pci_controller *hose,
1588 void __iomem *mbase,
1589 u64 plb_addr,
1590 u64 pci_addr,
1591 u64 size,
1592 unsigned int flags,
1593 int index)
1594{
1595 u32 lah, lal, pciah, pcial, sa;
1596
1597 if (!is_power_of_2(size) ||
1598 (index < 2 && size < 0x100000) ||
1599 (index == 2 && size < 0x100) ||
1600 (plb_addr & (size - 1)) != 0) {
1601 printk(KERN_WARNING "%s: Resource out of range\n",
1602 hose->dn->full_name);
1603 return -1;
1604 }
1605
1606 /* Calculate register values */
1607 lah = RES_TO_U32_HIGH(plb_addr);
1608 lal = RES_TO_U32_LOW(plb_addr);
1609 pciah = RES_TO_U32_HIGH(pci_addr);
1610 pcial = RES_TO_U32_LOW(pci_addr);
1611 sa = (0xffffffffu << ilog2(size)) | 0x1;
1612
1613 /* Program register values */
1614 switch (index) {
1615 case 0:
1616 out_le32(mbase + PECFG_POM0LAH, pciah);
1617 out_le32(mbase + PECFG_POM0LAL, pcial);
1618 dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAH, lah);
1619 dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAL, lal);
1620 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKH, 0x7fffffff);
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001621 /*Enabled and single region */
1622 if (of_device_is_compatible(port->node, "ibm,plb-pciex-460sx"))
1623 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL,
1624 sa | DCRO_PEGPL_460SX_OMR1MSKL_UOT
1625 | DCRO_PEGPL_OMRxMSKL_VAL);
1626 else
1627 dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL,
1628 sa | DCRO_PEGPL_OMR1MSKL_UOT
1629 | DCRO_PEGPL_OMRxMSKL_VAL);
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +00001630 break;
1631 case 1:
1632 out_le32(mbase + PECFG_POM1LAH, pciah);
1633 out_le32(mbase + PECFG_POM1LAL, pcial);
1634 dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAH, lah);
1635 dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAL, lal);
1636 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKH, 0x7fffffff);
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001637 dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL,
1638 sa | DCRO_PEGPL_OMRxMSKL_VAL);
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +00001639 break;
1640 case 2:
1641 out_le32(mbase + PECFG_POM2LAH, pciah);
1642 out_le32(mbase + PECFG_POM2LAL, pcial);
1643 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAH, lah);
1644 dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAL, lal);
1645 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKH, 0x7fffffff);
1646 /* Note that 3 here means enabled | IO space !!! */
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001647 dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL,
1648 sa | DCRO_PEGPL_OMR3MSKL_IO
1649 | DCRO_PEGPL_OMRxMSKL_VAL);
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +00001650 break;
1651 }
1652
1653 return 0;
1654}
1655
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001656static void __init ppc4xx_configure_pciex_POMs(struct ppc4xx_pciex_port *port,
1657 struct pci_controller *hose,
1658 void __iomem *mbase)
1659{
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +00001660 int i, j, found_isa_hole = 0;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001661
1662 /* Setup outbound memory windows */
1663 for (i = j = 0; i < 3; i++) {
1664 struct resource *res = &hose->mem_resources[i];
1665
1666 /* we only care about memory windows */
1667 if (!(res->flags & IORESOURCE_MEM))
1668 continue;
1669 if (j > 1) {
1670 printk(KERN_WARNING "%s: Too many ranges\n",
1671 port->node->full_name);
1672 break;
1673 }
1674
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +00001675 /* Configure the resource */
1676 if (ppc4xx_setup_one_pciex_POM(port, hose, mbase,
1677 res->start,
1678 res->start - hose->pci_mem_offset,
Joe Perches28f65c112011-06-09 09:13:32 -07001679 resource_size(res),
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +00001680 res->flags,
1681 j) == 0) {
1682 j++;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001683
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +00001684 /* If the resource PCI address is 0 then we have our
1685 * ISA memory hole
1686 */
1687 if (res->start == hose->pci_mem_offset)
1688 found_isa_hole = 1;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001689 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001690 }
1691
Benjamin Herrenschmidt84d727a2008-10-09 16:58:19 +00001692 /* Handle ISA memory hole if not already covered */
1693 if (j <= 1 && !found_isa_hole && hose->isa_mem_size)
1694 if (ppc4xx_setup_one_pciex_POM(port, hose, mbase,
1695 hose->isa_mem_phys, 0,
1696 hose->isa_mem_size, 0, j) == 0)
1697 printk(KERN_INFO "%s: Legacy ISA memory support enabled\n",
1698 hose->dn->full_name);
1699
1700 /* Configure IO, always 64K starting at 0. We hard wire it to 64K !
1701 * Note also that it -has- to be region index 2 on this HW
1702 */
1703 if (hose->io_resource.flags & IORESOURCE_IO)
1704 ppc4xx_setup_one_pciex_POM(port, hose, mbase,
1705 hose->io_base_phys, 0,
1706 0x10000, IORESOURCE_IO, 2);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001707}
1708
1709static void __init ppc4xx_configure_pciex_PIMs(struct ppc4xx_pciex_port *port,
1710 struct pci_controller *hose,
1711 void __iomem *mbase,
1712 struct resource *res)
1713{
Joe Perches28f65c112011-06-09 09:13:32 -07001714 resource_size_t size = resource_size(res);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001715 u64 sa;
1716
Stefan Roese80daac32008-04-22 00:54:30 +10001717 if (port->endpoint) {
1718 resource_size_t ep_addr = 0;
1719 resource_size_t ep_size = 32 << 20;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001720
Stefan Roese80daac32008-04-22 00:54:30 +10001721 /* Currently we map a fixed 64MByte window to PLB address
1722 * 0 (SDRAM). This should probably be configurable via a dts
1723 * property.
1724 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001725
Stefan Roese80daac32008-04-22 00:54:30 +10001726 /* Calculate window size */
Joe Perchesd258e642009-06-28 06:26:10 +00001727 sa = (0xffffffffffffffffull << ilog2(ep_size));
Stefan Roese80daac32008-04-22 00:54:30 +10001728
1729 /* Setup BAR0 */
1730 out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
1731 out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa) |
1732 PCI_BASE_ADDRESS_MEM_TYPE_64);
1733
1734 /* Disable BAR1 & BAR2 */
1735 out_le32(mbase + PECFG_BAR1MPA, 0);
1736 out_le32(mbase + PECFG_BAR2HMPA, 0);
1737 out_le32(mbase + PECFG_BAR2LMPA, 0);
1738
1739 out_le32(mbase + PECFG_PIM01SAH, RES_TO_U32_HIGH(sa));
1740 out_le32(mbase + PECFG_PIM01SAL, RES_TO_U32_LOW(sa));
1741
1742 out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(ep_addr));
1743 out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(ep_addr));
1744 } else {
1745 /* Calculate window size */
Joe Perchesd258e642009-06-28 06:26:10 +00001746 sa = (0xffffffffffffffffull << ilog2(size));
Stefan Roese80daac32008-04-22 00:54:30 +10001747 if (res->flags & IORESOURCE_PREFETCH)
Tony Breeds9fb55292011-11-30 21:39:17 +00001748 sa |= PCI_BASE_ADDRESS_MEM_PREFETCH;
Stefan Roese80daac32008-04-22 00:54:30 +10001749
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001750 if (of_device_is_compatible(port->node, "ibm,plb-pciex-460sx"))
1751 sa |= PCI_BASE_ADDRESS_MEM_TYPE_64;
1752
Stefan Roese80daac32008-04-22 00:54:30 +10001753 out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
1754 out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa));
1755
1756 /* The setup of the split looks weird to me ... let's see
1757 * if it works
1758 */
1759 out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
1760 out_le32(mbase + PECFG_PIM0LAH, 0x00000000);
1761 out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
1762 out_le32(mbase + PECFG_PIM1LAH, 0x00000000);
1763 out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
1764 out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
1765
1766 out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(res->start));
1767 out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(res->start));
1768 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001769
1770 /* Enable inbound mapping */
1771 out_le32(mbase + PECFG_PIMEN, 0x1);
1772
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001773 /* Enable I/O, Mem, and Busmaster cycles */
1774 out_le16(mbase + PCI_COMMAND,
1775 in_le16(mbase + PCI_COMMAND) |
1776 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1777}
1778
1779static void __init ppc4xx_pciex_port_setup_hose(struct ppc4xx_pciex_port *port)
1780{
1781 struct resource dma_window;
1782 struct pci_controller *hose = NULL;
1783 const int *bus_range;
1784 int primary = 0, busses;
1785 void __iomem *mbase = NULL, *cfg_data = NULL;
Stefan Roese80daac32008-04-22 00:54:30 +10001786 const u32 *pval;
1787 u32 val;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001788
1789 /* Check if primary bridge */
1790 if (of_get_property(port->node, "primary", NULL))
1791 primary = 1;
1792
1793 /* Get bus range if any */
1794 bus_range = of_get_property(port->node, "bus-range", NULL);
1795
1796 /* Allocate the host controller data structure */
1797 hose = pcibios_alloc_controller(port->node);
1798 if (!hose)
1799 goto fail;
1800
1801 /* We stick the port number in "indirect_type" so the config space
1802 * ops can retrieve the port data structure easily
1803 */
1804 hose->indirect_type = port->index;
1805
1806 /* Get bus range */
1807 hose->first_busno = bus_range ? bus_range[0] : 0x0;
1808 hose->last_busno = bus_range ? bus_range[1] : 0xff;
1809
1810 /* Because of how big mapping the config space is (1M per bus), we
1811 * limit how many busses we support. In the long run, we could replace
1812 * that with something akin to kmap_atomic instead. We set aside 1 bus
1813 * for the host itself too.
1814 */
1815 busses = hose->last_busno - hose->first_busno; /* This is off by 1 */
1816 if (busses > MAX_PCIE_BUS_MAPPED) {
1817 busses = MAX_PCIE_BUS_MAPPED;
1818 hose->last_busno = hose->first_busno + busses;
1819 }
1820
Stefan Roese80daac32008-04-22 00:54:30 +10001821 if (!port->endpoint) {
1822 /* Only map the external config space in cfg_data for
1823 * PCIe root-complexes. External space is 1M per bus
1824 */
1825 cfg_data = ioremap(port->cfg_space.start +
1826 (hose->first_busno + 1) * 0x100000,
1827 busses * 0x100000);
1828 if (cfg_data == NULL) {
1829 printk(KERN_ERR "%s: Can't map external config space !",
1830 port->node->full_name);
1831 goto fail;
1832 }
1833 hose->cfg_data = cfg_data;
1834 }
1835
1836 /* Always map the host config space in cfg_addr.
1837 * Internal space is 4K
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001838 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001839 mbase = ioremap(port->cfg_space.start + 0x10000000, 0x1000);
Stefan Roese80daac32008-04-22 00:54:30 +10001840 if (mbase == NULL) {
1841 printk(KERN_ERR "%s: Can't map internal config space !",
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001842 port->node->full_name);
1843 goto fail;
1844 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001845 hose->cfg_addr = mbase;
1846
1847 pr_debug("PCIE %s, bus %d..%d\n", port->node->full_name,
1848 hose->first_busno, hose->last_busno);
1849 pr_debug(" config space mapped at: root @0x%p, other @0x%p\n",
1850 hose->cfg_addr, hose->cfg_data);
1851
1852 /* Setup config space */
1853 hose->ops = &ppc4xx_pciex_pci_ops;
1854 port->hose = hose;
1855 mbase = (void __iomem *)hose->cfg_addr;
1856
Stefan Roese80daac32008-04-22 00:54:30 +10001857 if (!port->endpoint) {
1858 /*
1859 * Set bus numbers on our root port
1860 */
1861 out_8(mbase + PCI_PRIMARY_BUS, hose->first_busno);
1862 out_8(mbase + PCI_SECONDARY_BUS, hose->first_busno + 1);
1863 out_8(mbase + PCI_SUBORDINATE_BUS, hose->last_busno);
1864 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001865
1866 /*
1867 * OMRs are already reset, also disable PIMs
1868 */
1869 out_le32(mbase + PECFG_PIMEN, 0);
1870
1871 /* Parse outbound mapping resources */
1872 pci_process_bridge_OF_ranges(hose, port->node, primary);
1873
1874 /* Parse inbound mapping resources */
1875 if (ppc4xx_parse_dma_ranges(hose, mbase, &dma_window) != 0)
1876 goto fail;
1877
1878 /* Configure outbound ranges POMs */
1879 ppc4xx_configure_pciex_POMs(port, hose, mbase);
1880
1881 /* Configure inbound ranges PIMs */
1882 ppc4xx_configure_pciex_PIMs(port, hose, mbase, &dma_window);
1883
1884 /* The root complex doesn't show up if we don't set some vendor
Stefan Roese80daac32008-04-22 00:54:30 +10001885 * and device IDs into it. The defaults below are the same bogus
1886 * one that the initial code in arch/ppc had. This can be
1887 * overwritten by setting the "vendor-id/device-id" properties
1888 * in the pciex node.
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001889 */
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001890
Stefan Roese80daac32008-04-22 00:54:30 +10001891 /* Get the (optional) vendor-/device-id from the device-tree */
1892 pval = of_get_property(port->node, "vendor-id", NULL);
1893 if (pval) {
1894 val = *pval;
1895 } else {
1896 if (!port->endpoint)
1897 val = 0xaaa0 + port->index;
1898 else
1899 val = 0xeee0 + port->index;
1900 }
1901 out_le16(mbase + 0x200, val);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001902
Stefan Roese80daac32008-04-22 00:54:30 +10001903 pval = of_get_property(port->node, "device-id", NULL);
1904 if (pval) {
1905 val = *pval;
1906 } else {
1907 if (!port->endpoint)
1908 val = 0xbed0 + port->index;
1909 else
1910 val = 0xfed0 + port->index;
1911 }
1912 out_le16(mbase + 0x202, val);
1913
Ayman El-Khashabe7fa1d12011-07-20 03:02:29 +00001914 /* Enable Bus master, memory, and io space */
1915 if (of_device_is_compatible(port->node, "ibm,plb-pciex-460sx"))
1916 out_le16(mbase + 0x204, 0x7);
1917
Stefan Roese80daac32008-04-22 00:54:30 +10001918 if (!port->endpoint) {
1919 /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
1920 out_le32(mbase + 0x208, 0x06040001);
1921
1922 printk(KERN_INFO "PCIE%d: successfully set as root-complex\n",
1923 port->index);
1924 } else {
1925 /* Set Class Code to Processor/PPC */
1926 out_le32(mbase + 0x208, 0x0b200001);
1927
1928 printk(KERN_INFO "PCIE%d: successfully set as endpoint\n",
1929 port->index);
1930 }
1931
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001932 return;
1933 fail:
1934 if (hose)
1935 pcibios_free_controller(hose);
1936 if (cfg_data)
1937 iounmap(cfg_data);
1938 if (mbase)
1939 iounmap(mbase);
1940}
1941
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11001942static void __init ppc4xx_probe_pciex_bridge(struct device_node *np)
1943{
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001944 struct ppc4xx_pciex_port *port;
1945 const u32 *pval;
1946 int portno;
1947 unsigned int dcrs;
Stefan Roese80daac32008-04-22 00:54:30 +10001948 const char *val;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001949
1950 /* First, proceed to core initialization as we assume there's
1951 * only one PCIe core in the system
1952 */
1953 if (ppc4xx_pciex_check_core_init(np))
1954 return;
1955
1956 /* Get the port number from the device-tree */
1957 pval = of_get_property(np, "port", NULL);
1958 if (pval == NULL) {
1959 printk(KERN_ERR "PCIE: Can't find port number for %s\n",
1960 np->full_name);
1961 return;
1962 }
1963 portno = *pval;
1964 if (portno >= ppc4xx_pciex_port_count) {
1965 printk(KERN_ERR "PCIE: port number out of range for %s\n",
1966 np->full_name);
1967 return;
1968 }
1969 port = &ppc4xx_pciex_ports[portno];
1970 port->index = portno;
Stefan Roese995ada82008-06-06 00:22:29 +10001971
1972 /*
1973 * Check if device is enabled
1974 */
1975 if (!of_device_is_available(np)) {
1976 printk(KERN_INFO "PCIE%d: Port disabled via device-tree\n", port->index);
1977 return;
1978 }
1979
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001980 port->node = of_node_get(np);
Tony Breeds81158462011-11-30 21:39:18 +00001981 if (ppc4xx_pciex_hwops->want_sdr) {
1982 pval = of_get_property(np, "sdr-base", NULL);
1983 if (pval == NULL) {
1984 printk(KERN_ERR "PCIE: missing sdr-base for %s\n",
1985 np->full_name);
1986 return;
1987 }
1988 port->sdr_base = *pval;
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001989 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11001990
Stefan Roese80daac32008-04-22 00:54:30 +10001991 /* Check if device_type property is set to "pci" or "pci-endpoint".
1992 * Resulting from this setup this PCIe port will be configured
1993 * as root-complex or as endpoint.
1994 */
1995 val = of_get_property(port->node, "device_type", NULL);
1996 if (!strcmp(val, "pci-endpoint")) {
1997 port->endpoint = 1;
1998 } else if (!strcmp(val, "pci")) {
1999 port->endpoint = 0;
2000 } else {
2001 printk(KERN_ERR "PCIE: missing or incorrect device_type for %s\n",
2002 np->full_name);
2003 return;
2004 }
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11002005
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11002006 /* Fetch config space registers address */
2007 if (of_address_to_resource(np, 0, &port->cfg_space)) {
2008 printk(KERN_ERR "%s: Can't get PCI-E config space !",
2009 np->full_name);
2010 return;
2011 }
2012 /* Fetch host bridge internal registers address */
2013 if (of_address_to_resource(np, 1, &port->utl_regs)) {
2014 printk(KERN_ERR "%s: Can't get UTL register base !",
2015 np->full_name);
2016 return;
2017 }
2018
2019 /* Map DCRs */
2020 dcrs = dcr_resource_start(np, 0);
2021 if (dcrs == 0) {
2022 printk(KERN_ERR "%s: Can't get DCR register base !",
2023 np->full_name);
2024 return;
2025 }
2026 port->dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
2027
2028 /* Initialize the port specific registers */
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11002029 if (ppc4xx_pciex_port_init(port)) {
2030 printk(KERN_WARNING "PCIE%d: Port init failed\n", port->index);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11002031 return;
Benjamin Herrenschmidt035ee422007-12-21 15:39:36 +11002032 }
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11002033
2034 /* Setup the linux hose data structure */
2035 ppc4xx_pciex_port_setup_hose(port);
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11002036}
2037
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11002038#endif /* CONFIG_PPC4xx_PCI_EXPRESS */
2039
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11002040static int __init ppc4xx_pci_find_bridges(void)
2041{
2042 struct device_node *np;
2043
Rob Herring0e47ff12011-07-12 09:25:51 -05002044 pci_add_flags(PCI_ENABLE_PROC_DOMAINS | PCI_COMPAT_DOMAIN_0);
Benjamin Herrenschmidt41b6a082009-02-01 16:59:13 +00002045
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11002046#ifdef CONFIG_PPC4xx_PCI_EXPRESS
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11002047 for_each_compatible_node(np, NULL, "ibm,plb-pciex")
2048 ppc4xx_probe_pciex_bridge(np);
Benjamin Herrenschmidta2d2e1e2007-12-21 15:39:24 +11002049#endif
Benjamin Herrenschmidt5738ec62007-12-21 15:39:22 +11002050 for_each_compatible_node(np, NULL, "ibm,plb-pcix")
2051 ppc4xx_probe_pcix_bridge(np);
2052 for_each_compatible_node(np, NULL, "ibm,plb-pci")
2053 ppc4xx_probe_pci_bridge(np);
2054
2055 return 0;
2056}
2057arch_initcall(ppc4xx_pci_find_bridges);
2058