Merge tag 'iwlwifi-next-for-kalle-2014-12-30' of https://git.kernel.org/pub/scm/linux...
[cascardo/linux.git] / arch / powerpc / platforms / powermac / pci.c
1 /*
2  * Support for PCI bridges found on Power Macintoshes.
3  *
4  * Copyright (C) 2003-2005 Benjamin Herrenschmuidt (benh@kernel.crashing.org)
5  * Copyright (C) 1997 Paul Mackerras (paulus@samba.org)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/delay.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/irq.h>
19 #include <linux/of_pci.h>
20
21 #include <asm/sections.h>
22 #include <asm/io.h>
23 #include <asm/prom.h>
24 #include <asm/pci-bridge.h>
25 #include <asm/machdep.h>
26 #include <asm/pmac_feature.h>
27 #include <asm/grackle.h>
28 #include <asm/ppc-pci.h>
29
30 #undef DEBUG
31
32 #ifdef DEBUG
33 #define DBG(x...) printk(x)
34 #else
35 #define DBG(x...)
36 #endif
37
38 /* XXX Could be per-controller, but I don't think we risk anything by
39  * assuming we won't have both UniNorth and Bandit */
40 static int has_uninorth;
41 #ifdef CONFIG_PPC64
42 static struct pci_controller *u3_agp;
43 #else
44 static int has_second_ohare;
45 #endif /* CONFIG_PPC64 */
46
47 extern int pcibios_assign_bus_offset;
48
49 struct device_node *k2_skiplist[2];
50
51 /*
52  * Magic constants for enabling cache coherency in the bandit/PSX bridge.
53  */
54 #define BANDIT_DEVID_2  8
55 #define BANDIT_REVID    3
56
57 #define BANDIT_DEVNUM   11
58 #define BANDIT_MAGIC    0x50
59 #define BANDIT_COHERENT 0x40
60
61 static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
62 {
63         for (; node != 0;node = node->sibling) {
64                 const int * bus_range;
65                 const unsigned int *class_code;
66                 int len;
67
68                 /* For PCI<->PCI bridges or CardBus bridges, we go down */
69                 class_code = of_get_property(node, "class-code", NULL);
70                 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
71                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
72                         continue;
73                 bus_range = of_get_property(node, "bus-range", &len);
74                 if (bus_range != NULL && len > 2 * sizeof(int)) {
75                         if (bus_range[1] > higher)
76                                 higher = bus_range[1];
77                 }
78                 higher = fixup_one_level_bus_range(node->child, higher);
79         }
80         return higher;
81 }
82
83 /* This routine fixes the "bus-range" property of all bridges in the
84  * system since they tend to have their "last" member wrong on macs
85  *
86  * Note that the bus numbers manipulated here are OF bus numbers, they
87  * are not Linux bus numbers.
88  */
89 static void __init fixup_bus_range(struct device_node *bridge)
90 {
91         int *bus_range, len;
92         struct property *prop;
93
94         /* Lookup the "bus-range" property for the hose */
95         prop = of_find_property(bridge, "bus-range", &len);
96         if (prop == NULL || prop->length < 2 * sizeof(int))
97                 return;
98
99         bus_range = prop->value;
100         bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
101 }
102
103 /*
104  * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
105  *
106  * The "Bandit" version is present in all early PCI PowerMacs,
107  * and up to the first ones using Grackle. Some machines may
108  * have 2 bandit controllers (2 PCI busses).
109  *
110  * "Chaos" is used in some "Bandit"-type machines as a bridge
111  * for the separate display bus. It is accessed the same
112  * way as bandit, but cannot be probed for devices. It therefore
113  * has its own config access functions.
114  *
115  * The "UniNorth" version is present in all Core99 machines
116  * (iBook, G4, new IMacs, and all the recent Apple machines).
117  * It contains 3 controllers in one ASIC.
118  *
119  * The U3 is the bridge used on G5 machines. It contains an
120  * AGP bus which is dealt with the old UniNorth access routines
121  * and a HyperTransport bus which uses its own set of access
122  * functions.
123  */
124
125 #define MACRISC_CFA0(devfn, off)        \
126         ((1 << (unsigned int)PCI_SLOT(dev_fn)) \
127         | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
128         | (((unsigned int)(off)) & 0xFCUL))
129
130 #define MACRISC_CFA1(bus, devfn, off)   \
131         ((((unsigned int)(bus)) << 16) \
132         |(((unsigned int)(devfn)) << 8) \
133         |(((unsigned int)(off)) & 0xFCUL) \
134         |1UL)
135
136 static volatile void __iomem *macrisc_cfg_access(struct pci_controller* hose,
137                                                u8 bus, u8 dev_fn, u8 offset)
138 {
139         unsigned int caddr;
140
141         if (bus == hose->first_busno) {
142                 if (dev_fn < (11 << 3))
143                         return NULL;
144                 caddr = MACRISC_CFA0(dev_fn, offset);
145         } else
146                 caddr = MACRISC_CFA1(bus, dev_fn, offset);
147
148         /* Uninorth will return garbage if we don't read back the value ! */
149         do {
150                 out_le32(hose->cfg_addr, caddr);
151         } while (in_le32(hose->cfg_addr) != caddr);
152
153         offset &= has_uninorth ? 0x07 : 0x03;
154         return hose->cfg_data + offset;
155 }
156
157 static int macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
158                                       int offset, int len, u32 *val)
159 {
160         struct pci_controller *hose;
161         volatile void __iomem *addr;
162
163         hose = pci_bus_to_host(bus);
164         if (hose == NULL)
165                 return PCIBIOS_DEVICE_NOT_FOUND;
166         if (offset >= 0x100)
167                 return  PCIBIOS_BAD_REGISTER_NUMBER;
168         addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
169         if (!addr)
170                 return PCIBIOS_DEVICE_NOT_FOUND;
171         /*
172          * Note: the caller has already checked that offset is
173          * suitably aligned and that len is 1, 2 or 4.
174          */
175         switch (len) {
176         case 1:
177                 *val = in_8(addr);
178                 break;
179         case 2:
180                 *val = in_le16(addr);
181                 break;
182         default:
183                 *val = in_le32(addr);
184                 break;
185         }
186         return PCIBIOS_SUCCESSFUL;
187 }
188
189 static int macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
190                                        int offset, int len, u32 val)
191 {
192         struct pci_controller *hose;
193         volatile void __iomem *addr;
194
195         hose = pci_bus_to_host(bus);
196         if (hose == NULL)
197                 return PCIBIOS_DEVICE_NOT_FOUND;
198         if (offset >= 0x100)
199                 return  PCIBIOS_BAD_REGISTER_NUMBER;
200         addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
201         if (!addr)
202                 return PCIBIOS_DEVICE_NOT_FOUND;
203         /*
204          * Note: the caller has already checked that offset is
205          * suitably aligned and that len is 1, 2 or 4.
206          */
207         switch (len) {
208         case 1:
209                 out_8(addr, val);
210                 break;
211         case 2:
212                 out_le16(addr, val);
213                 break;
214         default:
215                 out_le32(addr, val);
216                 break;
217         }
218         return PCIBIOS_SUCCESSFUL;
219 }
220
221 static struct pci_ops macrisc_pci_ops =
222 {
223         .read = macrisc_read_config,
224         .write = macrisc_write_config,
225 };
226
227 #ifdef CONFIG_PPC32
228 /*
229  * Verify that a specific (bus, dev_fn) exists on chaos
230  */
231 static int chaos_validate_dev(struct pci_bus *bus, int devfn, int offset)
232 {
233         struct device_node *np;
234         const u32 *vendor, *device;
235
236         if (offset >= 0x100)
237                 return  PCIBIOS_BAD_REGISTER_NUMBER;
238         np = of_pci_find_child_device(bus->dev.of_node, devfn);
239         if (np == NULL)
240                 return PCIBIOS_DEVICE_NOT_FOUND;
241
242         vendor = of_get_property(np, "vendor-id", NULL);
243         device = of_get_property(np, "device-id", NULL);
244         if (vendor == NULL || device == NULL)
245                 return PCIBIOS_DEVICE_NOT_FOUND;
246
247         if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
248             && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
249                 return PCIBIOS_BAD_REGISTER_NUMBER;
250
251         return PCIBIOS_SUCCESSFUL;
252 }
253
254 static int
255 chaos_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
256                   int len, u32 *val)
257 {
258         int result = chaos_validate_dev(bus, devfn, offset);
259         if (result == PCIBIOS_BAD_REGISTER_NUMBER)
260                 *val = ~0U;
261         if (result != PCIBIOS_SUCCESSFUL)
262                 return result;
263         return macrisc_read_config(bus, devfn, offset, len, val);
264 }
265
266 static int
267 chaos_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
268                    int len, u32 val)
269 {
270         int result = chaos_validate_dev(bus, devfn, offset);
271         if (result != PCIBIOS_SUCCESSFUL)
272                 return result;
273         return macrisc_write_config(bus, devfn, offset, len, val);
274 }
275
276 static struct pci_ops chaos_pci_ops =
277 {
278         .read = chaos_read_config,
279         .write = chaos_write_config,
280 };
281
282 static void __init setup_chaos(struct pci_controller *hose,
283                                struct resource *addr)
284 {
285         /* assume a `chaos' bridge */
286         hose->ops = &chaos_pci_ops;
287         hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
288         hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
289 }
290 #endif /* CONFIG_PPC32 */
291
292 #ifdef CONFIG_PPC64
293 /*
294  * These versions of U3 HyperTransport config space access ops do not
295  * implement self-view of the HT host yet
296  */
297
298 /*
299  * This function deals with some "special cases" devices.
300  *
301  *  0 -> No special case
302  *  1 -> Skip the device but act as if the access was successful
303  *       (return 0xff's on reads, eventually, cache config space
304  *       accesses in a later version)
305  * -1 -> Hide the device (unsuccessful access)
306  */
307 static int u3_ht_skip_device(struct pci_controller *hose,
308                              struct pci_bus *bus, unsigned int devfn)
309 {
310         struct device_node *busdn, *dn;
311         int i;
312
313         /* We only allow config cycles to devices that are in OF device-tree
314          * as we are apparently having some weird things going on with some
315          * revs of K2 on recent G5s, except for the host bridge itself, which
316          * is missing from the tree but we know we can probe.
317          */
318         if (bus->self)
319                 busdn = pci_device_to_OF_node(bus->self);
320         else if (devfn == 0)
321                 return 0;
322         else
323                 busdn = hose->dn;
324         for (dn = busdn->child; dn; dn = dn->sibling)
325                 if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
326                         break;
327         if (dn == NULL)
328                 return -1;
329
330         /*
331          * When a device in K2 is powered down, we die on config
332          * cycle accesses. Fix that here.
333          */
334         for (i=0; i<2; i++)
335                 if (k2_skiplist[i] == dn)
336                         return 1;
337
338         return 0;
339 }
340
341 #define U3_HT_CFA0(devfn, off)          \
342                 ((((unsigned int)devfn) << 8) | offset)
343 #define U3_HT_CFA1(bus, devfn, off)     \
344                 (U3_HT_CFA0(devfn, off) \
345                 + (((unsigned int)bus) << 16) \
346                 + 0x01000000UL)
347
348 static void __iomem *u3_ht_cfg_access(struct pci_controller *hose, u8 bus,
349                                       u8 devfn, u8 offset, int *swap)
350 {
351         *swap = 1;
352         if (bus == hose->first_busno) {
353                 if (devfn != 0)
354                         return hose->cfg_data + U3_HT_CFA0(devfn, offset);
355                 *swap = 0;
356                 return ((void __iomem *)hose->cfg_addr) + (offset << 2);
357         } else
358                 return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);
359 }
360
361 static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
362                                     int offset, int len, u32 *val)
363 {
364         struct pci_controller *hose;
365         void __iomem *addr;
366         int swap;
367
368         hose = pci_bus_to_host(bus);
369         if (hose == NULL)
370                 return PCIBIOS_DEVICE_NOT_FOUND;
371         if (offset >= 0x100)
372                 return  PCIBIOS_BAD_REGISTER_NUMBER;
373         addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
374         if (!addr)
375                 return PCIBIOS_DEVICE_NOT_FOUND;
376
377         switch (u3_ht_skip_device(hose, bus, devfn)) {
378         case 0:
379                 break;
380         case 1:
381                 switch (len) {
382                 case 1:
383                         *val = 0xff; break;
384                 case 2:
385                         *val = 0xffff; break;
386                 default:
387                         *val = 0xfffffffful; break;
388                 }
389                 return PCIBIOS_SUCCESSFUL;
390         default:
391                 return PCIBIOS_DEVICE_NOT_FOUND;
392         }
393
394         /*
395          * Note: the caller has already checked that offset is
396          * suitably aligned and that len is 1, 2 or 4.
397          */
398         switch (len) {
399         case 1:
400                 *val = in_8(addr);
401                 break;
402         case 2:
403                 *val = swap ? in_le16(addr) : in_be16(addr);
404                 break;
405         default:
406                 *val = swap ? in_le32(addr) : in_be32(addr);
407                 break;
408         }
409         return PCIBIOS_SUCCESSFUL;
410 }
411
412 static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
413                                      int offset, int len, u32 val)
414 {
415         struct pci_controller *hose;
416         void __iomem *addr;
417         int swap;
418
419         hose = pci_bus_to_host(bus);
420         if (hose == NULL)
421                 return PCIBIOS_DEVICE_NOT_FOUND;
422         if (offset >= 0x100)
423                 return  PCIBIOS_BAD_REGISTER_NUMBER;
424         addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
425         if (!addr)
426                 return PCIBIOS_DEVICE_NOT_FOUND;
427
428         switch (u3_ht_skip_device(hose, bus, devfn)) {
429         case 0:
430                 break;
431         case 1:
432                 return PCIBIOS_SUCCESSFUL;
433         default:
434                 return PCIBIOS_DEVICE_NOT_FOUND;
435         }
436
437         /*
438          * Note: the caller has already checked that offset is
439          * suitably aligned and that len is 1, 2 or 4.
440          */
441         switch (len) {
442         case 1:
443                 out_8(addr, val);
444                 break;
445         case 2:
446                 swap ? out_le16(addr, val) : out_be16(addr, val);
447                 break;
448         default:
449                 swap ? out_le32(addr, val) : out_be32(addr, val);
450                 break;
451         }
452         return PCIBIOS_SUCCESSFUL;
453 }
454
455 static struct pci_ops u3_ht_pci_ops =
456 {
457         .read = u3_ht_read_config,
458         .write = u3_ht_write_config,
459 };
460
461 #define U4_PCIE_CFA0(devfn, off)        \
462         ((1 << ((unsigned int)PCI_SLOT(dev_fn)))        \
463          | (((unsigned int)PCI_FUNC(dev_fn)) << 8)      \
464          | ((((unsigned int)(off)) >> 8) << 28) \
465          | (((unsigned int)(off)) & 0xfcU))
466
467 #define U4_PCIE_CFA1(bus, devfn, off)   \
468         ((((unsigned int)(bus)) << 16) \
469          |(((unsigned int)(devfn)) << 8)        \
470          | ((((unsigned int)(off)) >> 8) << 28) \
471          |(((unsigned int)(off)) & 0xfcU)       \
472          |1UL)
473
474 static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose,
475                                         u8 bus, u8 dev_fn, int offset)
476 {
477         unsigned int caddr;
478
479         if (bus == hose->first_busno) {
480                 caddr = U4_PCIE_CFA0(dev_fn, offset);
481         } else
482                 caddr = U4_PCIE_CFA1(bus, dev_fn, offset);
483
484         /* Uninorth will return garbage if we don't read back the value ! */
485         do {
486                 out_le32(hose->cfg_addr, caddr);
487         } while (in_le32(hose->cfg_addr) != caddr);
488
489         offset &= 0x03;
490         return hose->cfg_data + offset;
491 }
492
493 static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
494                                int offset, int len, u32 *val)
495 {
496         struct pci_controller *hose;
497         volatile void __iomem *addr;
498
499         hose = pci_bus_to_host(bus);
500         if (hose == NULL)
501                 return PCIBIOS_DEVICE_NOT_FOUND;
502         if (offset >= 0x1000)
503                 return  PCIBIOS_BAD_REGISTER_NUMBER;
504         addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
505         if (!addr)
506                 return PCIBIOS_DEVICE_NOT_FOUND;
507         /*
508          * Note: the caller has already checked that offset is
509          * suitably aligned and that len is 1, 2 or 4.
510          */
511         switch (len) {
512         case 1:
513                 *val = in_8(addr);
514                 break;
515         case 2:
516                 *val = in_le16(addr);
517                 break;
518         default:
519                 *val = in_le32(addr);
520                 break;
521         }
522         return PCIBIOS_SUCCESSFUL;
523 }
524
525 static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
526                                 int offset, int len, u32 val)
527 {
528         struct pci_controller *hose;
529         volatile void __iomem *addr;
530
531         hose = pci_bus_to_host(bus);
532         if (hose == NULL)
533                 return PCIBIOS_DEVICE_NOT_FOUND;
534         if (offset >= 0x1000)
535                 return  PCIBIOS_BAD_REGISTER_NUMBER;
536         addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
537         if (!addr)
538                 return PCIBIOS_DEVICE_NOT_FOUND;
539         /*
540          * Note: the caller has already checked that offset is
541          * suitably aligned and that len is 1, 2 or 4.
542          */
543         switch (len) {
544         case 1:
545                 out_8(addr, val);
546                 break;
547         case 2:
548                 out_le16(addr, val);
549                 break;
550         default:
551                 out_le32(addr, val);
552                 break;
553         }
554         return PCIBIOS_SUCCESSFUL;
555 }
556
557 static struct pci_ops u4_pcie_pci_ops =
558 {
559         .read = u4_pcie_read_config,
560         .write = u4_pcie_write_config,
561 };
562
563 static void pmac_pci_fixup_u4_of_node(struct pci_dev *dev)
564 {
565         /* Apple's device-tree "hides" the root complex virtual P2P bridge
566          * on U4. However, Linux sees it, causing the PCI <-> OF matching
567          * code to fail to properly match devices below it. This works around
568          * it by setting the node of the bridge to point to the PHB node,
569          * which is not entirely correct but fixes the matching code and
570          * doesn't break anything else. It's also the simplest possible fix.
571          */
572         if (dev->dev.of_node == NULL)
573                 dev->dev.of_node = pcibios_get_phb_of_node(dev->bus);
574 }
575 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, 0x5b, pmac_pci_fixup_u4_of_node);
576
577 #endif /* CONFIG_PPC64 */
578
579 #ifdef CONFIG_PPC32
580 /*
581  * For a bandit bridge, turn on cache coherency if necessary.
582  * N.B. we could clean this up using the hose ops directly.
583  */
584 static void __init init_bandit(struct pci_controller *bp)
585 {
586         unsigned int vendev, magic;
587         int rev;
588
589         /* read the word at offset 0 in config space for device 11 */
590         out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
591         udelay(2);
592         vendev = in_le32(bp->cfg_data);
593         if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
594                         PCI_VENDOR_ID_APPLE) {
595                 /* read the revision id */
596                 out_le32(bp->cfg_addr,
597                          (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
598                 udelay(2);
599                 rev = in_8(bp->cfg_data);
600                 if (rev != BANDIT_REVID)
601                         printk(KERN_WARNING
602                                "Unknown revision %d for bandit\n", rev);
603         } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
604                 printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
605                 return;
606         }
607
608         /* read the word at offset 0x50 */
609         out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
610         udelay(2);
611         magic = in_le32(bp->cfg_data);
612         if ((magic & BANDIT_COHERENT) != 0)
613                 return;
614         magic |= BANDIT_COHERENT;
615         udelay(2);
616         out_le32(bp->cfg_data, magic);
617         printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
618 }
619
620 /*
621  * Tweak the PCI-PCI bridge chip on the blue & white G3s.
622  */
623 static void __init init_p2pbridge(void)
624 {
625         struct device_node *p2pbridge;
626         struct pci_controller* hose;
627         u8 bus, devfn;
628         u16 val;
629
630         /* XXX it would be better here to identify the specific
631            PCI-PCI bridge chip we have. */
632         p2pbridge = of_find_node_by_name(NULL, "pci-bridge");
633         if (p2pbridge == NULL
634             || p2pbridge->parent == NULL
635             || strcmp(p2pbridge->parent->name, "pci") != 0)
636                 goto done;
637         if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
638                 DBG("Can't find PCI infos for PCI<->PCI bridge\n");
639                 goto done;
640         }
641         /* Warning: At this point, we have not yet renumbered all busses.
642          * So we must use OF walking to find out hose
643          */
644         hose = pci_find_hose_for_OF_device(p2pbridge);
645         if (!hose) {
646                 DBG("Can't find hose for PCI<->PCI bridge\n");
647                 goto done;
648         }
649         if (early_read_config_word(hose, bus, devfn,
650                                    PCI_BRIDGE_CONTROL, &val) < 0) {
651                 printk(KERN_ERR "init_p2pbridge: couldn't read bridge"
652                        " control\n");
653                 goto done;
654         }
655         val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
656         early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
657 done:
658         of_node_put(p2pbridge);
659 }
660
661 static void __init init_second_ohare(void)
662 {
663         struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");
664         unsigned char bus, devfn;
665         unsigned short cmd;
666
667         if (np == NULL)
668                 return;
669
670         /* This must run before we initialize the PICs since the second
671          * ohare hosts a PIC that will be accessed there.
672          */
673         if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
674                 struct pci_controller* hose =
675                         pci_find_hose_for_OF_device(np);
676                 if (!hose) {
677                         printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
678                         of_node_put(np);
679                         return;
680                 }
681                 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
682                 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
683                 cmd &= ~PCI_COMMAND_IO;
684                 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
685         }
686         has_second_ohare = 1;
687         of_node_put(np);
688 }
689
690 /*
691  * Some Apple desktop machines have a NEC PD720100A USB2 controller
692  * on the motherboard. Open Firmware, on these, will disable the
693  * EHCI part of it so it behaves like a pair of OHCI's. This fixup
694  * code re-enables it ;)
695  */
696 static void __init fixup_nec_usb2(void)
697 {
698         struct device_node *nec;
699
700         for_each_node_by_name(nec, "usb") {
701                 struct pci_controller *hose;
702                 u32 data;
703                 const u32 *prop;
704                 u8 bus, devfn;
705
706                 prop = of_get_property(nec, "vendor-id", NULL);
707                 if (prop == NULL)
708                         continue;
709                 if (0x1033 != *prop)
710                         continue;
711                 prop = of_get_property(nec, "device-id", NULL);
712                 if (prop == NULL)
713                         continue;
714                 if (0x0035 != *prop)
715                         continue;
716                 prop = of_get_property(nec, "reg", NULL);
717                 if (prop == NULL)
718                         continue;
719                 devfn = (prop[0] >> 8) & 0xff;
720                 bus = (prop[0] >> 16) & 0xff;
721                 if (PCI_FUNC(devfn) != 0)
722                         continue;
723                 hose = pci_find_hose_for_OF_device(nec);
724                 if (!hose)
725                         continue;
726                 early_read_config_dword(hose, bus, devfn, 0xe4, &data);
727                 if (data & 1UL) {
728                         printk("Found NEC PD720100A USB2 chip with disabled"
729                                " EHCI, fixing up...\n");
730                         data &= ~1UL;
731                         early_write_config_dword(hose, bus, devfn, 0xe4, data);
732                 }
733         }
734 }
735
736 static void __init setup_bandit(struct pci_controller *hose,
737                                 struct resource *addr)
738 {
739         hose->ops = &macrisc_pci_ops;
740         hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
741         hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
742         init_bandit(hose);
743 }
744
745 static int __init setup_uninorth(struct pci_controller *hose,
746                                  struct resource *addr)
747 {
748         pci_add_flags(PCI_REASSIGN_ALL_BUS);
749         has_uninorth = 1;
750         hose->ops = &macrisc_pci_ops;
751         hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
752         hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
753         /* We "know" that the bridge at f2000000 has the PCI slots. */
754         return addr->start == 0xf2000000;
755 }
756 #endif /* CONFIG_PPC32 */
757
758 #ifdef CONFIG_PPC64
759 static void __init setup_u3_agp(struct pci_controller* hose)
760 {
761         /* On G5, we move AGP up to high bus number so we don't need
762          * to reassign bus numbers for HT. If we ever have P2P bridges
763          * on AGP, we'll have to move pci_assign_all_busses to the
764          * pci_controller structure so we enable it for AGP and not for
765          * HT childs.
766          * We hard code the address because of the different size of
767          * the reg address cell, we shall fix that by killing struct
768          * reg_property and using some accessor functions instead
769          */
770         hose->first_busno = 0xf0;
771         hose->last_busno = 0xff;
772         has_uninorth = 1;
773         hose->ops = &macrisc_pci_ops;
774         hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
775         hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
776         u3_agp = hose;
777 }
778
779 static void __init setup_u4_pcie(struct pci_controller* hose)
780 {
781         /* We currently only implement the "non-atomic" config space, to
782          * be optimised later.
783          */
784         hose->ops = &u4_pcie_pci_ops;
785         hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
786         hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
787
788         /* The bus contains a bridge from root -> device, we need to
789          * make it visible on bus 0 so that we pick the right type
790          * of config cycles. If we didn't, we would have to force all
791          * config cycles to be type 1. So we override the "bus-range"
792          * property here
793          */
794         hose->first_busno = 0x00;
795         hose->last_busno = 0xff;
796 }
797
798 static void __init parse_region_decode(struct pci_controller *hose,
799                                        u32 decode)
800 {
801         unsigned long base, end, next = -1;
802         int i, cur = -1;
803
804         /* Iterate through all bits. We ignore the last bit as this region is
805          * reserved for the ROM among other niceties
806          */
807         for (i = 0; i < 31; i++) {
808                 if ((decode & (0x80000000 >> i)) == 0)
809                         continue;
810                 if (i < 16) {
811                         base = 0xf0000000 | (((u32)i) << 24);
812                         end = base + 0x00ffffff;
813                 } else {
814                         base = ((u32)i-16) << 28;
815                         end = base + 0x0fffffff;
816                 }
817                 if (base != next) {
818                         if (++cur >= 3) {
819                                 printk(KERN_WARNING "PCI: Too many ranges !\n");
820                                 break;
821                         }
822                         hose->mem_resources[cur].flags = IORESOURCE_MEM;
823                         hose->mem_resources[cur].name = hose->dn->full_name;
824                         hose->mem_resources[cur].start = base;
825                         hose->mem_resources[cur].end = end;
826                         hose->mem_offset[cur] = 0;
827                         DBG("  %d: 0x%08lx-0x%08lx\n", cur, base, end);
828                 } else {
829                         DBG("   :           -0x%08lx\n", end);
830                         hose->mem_resources[cur].end = end;
831                 }
832                 next = end + 1;
833         }
834 }
835
836 static void __init setup_u3_ht(struct pci_controller* hose)
837 {
838         struct device_node *np = hose->dn;
839         struct resource cfg_res, self_res;
840         u32 decode;
841
842         hose->ops = &u3_ht_pci_ops;
843
844         /* Get base addresses from OF tree
845          */
846         if (of_address_to_resource(np, 0, &cfg_res) ||
847             of_address_to_resource(np, 1, &self_res)) {
848                 printk(KERN_ERR "PCI: Failed to get U3/U4 HT resources !\n");
849                 return;
850         }
851
852         /* Map external cfg space access into cfg_data and self registers
853          * into cfg_addr
854          */
855         hose->cfg_data = ioremap(cfg_res.start, 0x02000000);
856         hose->cfg_addr = ioremap(self_res.start, resource_size(&self_res));
857
858         /*
859          * /ht node doesn't expose a "ranges" property, we read the register
860          * that controls the decoding logic and use that for memory regions.
861          * The IO region is hard coded since it is fixed in HW as well.
862          */
863         hose->io_base_phys = 0xf4000000;
864         hose->pci_io_size = 0x00400000;
865         hose->io_resource.name = np->full_name;
866         hose->io_resource.start = 0;
867         hose->io_resource.end = 0x003fffff;
868         hose->io_resource.flags = IORESOURCE_IO;
869         hose->first_busno = 0;
870         hose->last_busno = 0xef;
871
872         /* Note: fix offset when cfg_addr becomes a void * */
873         decode = in_be32(hose->cfg_addr + 0x80);
874
875         DBG("PCI: Apple HT bridge decode register: 0x%08x\n", decode);
876
877         /* NOTE: The decode register setup is a bit weird... region
878          * 0xf8000000 for example is marked as enabled in there while it's
879          & actually the memory controller registers.
880          * That means that we are incorrectly attributing it to HT.
881          *
882          * In a similar vein, region 0xf4000000 is actually the HT IO space but
883          * also marked as enabled in here and 0xf9000000 is used by some other
884          * internal bits of the northbridge.
885          *
886          * Unfortunately, we can't just mask out those bit as we would end
887          * up with more regions than we can cope (linux can only cope with
888          * 3 memory regions for a PHB at this stage).
889          *
890          * So for now, we just do a little hack. We happen to -know- that
891          * Apple firmware doesn't assign things below 0xfa000000 for that
892          * bridge anyway so we mask out all bits we don't want.
893          */
894         decode &= 0x003fffff;
895
896         /* Now parse the resulting bits and build resources */
897         parse_region_decode(hose, decode);
898 }
899 #endif /* CONFIG_PPC64 */
900
901 /*
902  * We assume that if we have a G3 powermac, we have one bridge called
903  * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
904  * if we have one or more bandit or chaos bridges, we don't have a MPC106.
905  */
906 static int __init pmac_add_bridge(struct device_node *dev)
907 {
908         int len;
909         struct pci_controller *hose;
910         struct resource rsrc;
911         char *disp_name;
912         const int *bus_range;
913         int primary = 1, has_address = 0;
914
915         DBG("Adding PCI host bridge %s\n", dev->full_name);
916
917         /* Fetch host bridge registers address */
918         has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
919
920         /* Get bus range if any */
921         bus_range = of_get_property(dev, "bus-range", &len);
922         if (bus_range == NULL || len < 2 * sizeof(int)) {
923                 printk(KERN_WARNING "Can't get bus-range for %s, assume"
924                        " bus 0\n", dev->full_name);
925         }
926
927         hose = pcibios_alloc_controller(dev);
928         if (!hose)
929                 return -ENOMEM;
930         hose->first_busno = bus_range ? bus_range[0] : 0;
931         hose->last_busno = bus_range ? bus_range[1] : 0xff;
932
933         disp_name = NULL;
934
935         /* 64 bits only bridges */
936 #ifdef CONFIG_PPC64
937         if (of_device_is_compatible(dev, "u3-agp")) {
938                 setup_u3_agp(hose);
939                 disp_name = "U3-AGP";
940                 primary = 0;
941         } else if (of_device_is_compatible(dev, "u3-ht")) {
942                 setup_u3_ht(hose);
943                 disp_name = "U3-HT";
944                 primary = 1;
945         } else if (of_device_is_compatible(dev, "u4-pcie")) {
946                 setup_u4_pcie(hose);
947                 disp_name = "U4-PCIE";
948                 primary = 0;
949         }
950         printk(KERN_INFO "Found %s PCI host bridge.  Firmware bus number:"
951                " %d->%d\n", disp_name, hose->first_busno, hose->last_busno);
952 #endif /* CONFIG_PPC64 */
953
954         /* 32 bits only bridges */
955 #ifdef CONFIG_PPC32
956         if (of_device_is_compatible(dev, "uni-north")) {
957                 primary = setup_uninorth(hose, &rsrc);
958                 disp_name = "UniNorth";
959         } else if (strcmp(dev->name, "pci") == 0) {
960                 /* XXX assume this is a mpc106 (grackle) */
961                 setup_grackle(hose);
962                 disp_name = "Grackle (MPC106)";
963         } else if (strcmp(dev->name, "bandit") == 0) {
964                 setup_bandit(hose, &rsrc);
965                 disp_name = "Bandit";
966         } else if (strcmp(dev->name, "chaos") == 0) {
967                 setup_chaos(hose, &rsrc);
968                 disp_name = "Chaos";
969                 primary = 0;
970         }
971         printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "
972                "Firmware bus number: %d->%d\n",
973                 disp_name, (unsigned long long)rsrc.start, hose->first_busno,
974                 hose->last_busno);
975 #endif /* CONFIG_PPC32 */
976
977         DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
978                 hose, hose->cfg_addr, hose->cfg_data);
979
980         /* Interpret the "ranges" property */
981         /* This also maps the I/O region and sets isa_io/mem_base */
982         pci_process_bridge_OF_ranges(hose, dev, primary);
983
984         /* Fixup "bus-range" OF property */
985         fixup_bus_range(dev);
986
987         return 0;
988 }
989
990 void pmac_pci_irq_fixup(struct pci_dev *dev)
991 {
992 #ifdef CONFIG_PPC32
993         /* Fixup interrupt for the modem/ethernet combo controller.
994          * on machines with a second ohare chip.
995          * The number in the device tree (27) is bogus (correct for
996          * the ethernet-only board but not the combo ethernet/modem
997          * board). The real interrupt is 28 on the second controller
998          * -> 28+32 = 60.
999          */
1000         if (has_second_ohare &&
1001             dev->vendor == PCI_VENDOR_ID_DEC &&
1002             dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {
1003                 dev->irq = irq_create_mapping(NULL, 60);
1004                 irq_set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);
1005         }
1006 #endif /* CONFIG_PPC32 */
1007 }
1008
1009 void __init pmac_pci_init(void)
1010 {
1011         struct device_node *np, *root;
1012         struct device_node *ht = NULL;
1013
1014         pci_set_flags(PCI_CAN_SKIP_ISA_ALIGN);
1015
1016         root = of_find_node_by_path("/");
1017         if (root == NULL) {
1018                 printk(KERN_CRIT "pmac_pci_init: can't find root "
1019                        "of device tree\n");
1020                 return;
1021         }
1022         for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
1023                 if (np->name == NULL)
1024                         continue;
1025                 if (strcmp(np->name, "bandit") == 0
1026                     || strcmp(np->name, "chaos") == 0
1027                     || strcmp(np->name, "pci") == 0) {
1028                         if (pmac_add_bridge(np) == 0)
1029                                 of_node_get(np);
1030                 }
1031                 if (strcmp(np->name, "ht") == 0) {
1032                         of_node_get(np);
1033                         ht = np;
1034                 }
1035         }
1036         of_node_put(root);
1037
1038 #ifdef CONFIG_PPC64
1039         /* Probe HT last as it relies on the agp resources to be already
1040          * setup
1041          */
1042         if (ht && pmac_add_bridge(ht) != 0)
1043                 of_node_put(ht);
1044
1045         /* Setup the linkage between OF nodes and PHBs */
1046         pci_devs_phb_init();
1047
1048         /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
1049          * assume there is no P2P bridge on the AGP bus, which should be a
1050          * safe assumptions for now. We should do something better in the
1051          * future though
1052          */
1053         if (u3_agp) {
1054                 struct device_node *np = u3_agp->dn;
1055                 PCI_DN(np)->busno = 0xf0;
1056                 for (np = np->child; np; np = np->sibling)
1057                         PCI_DN(np)->busno = 0xf0;
1058         }
1059         /* pmac_check_ht_link(); */
1060
1061 #else /* CONFIG_PPC64 */
1062         init_p2pbridge();
1063         init_second_ohare();
1064         fixup_nec_usb2();
1065
1066         /* We are still having some issues with the Xserve G4, enabling
1067          * some offset between bus number and domains for now when we
1068          * assign all busses should help for now
1069          */
1070         if (pci_has_flag(PCI_REASSIGN_ALL_BUS))
1071                 pcibios_assign_bus_offset = 0x10;
1072 #endif
1073 }
1074
1075 #ifdef CONFIG_PPC32
1076 int pmac_pci_enable_device_hook(struct pci_dev *dev)
1077 {
1078         struct device_node* node;
1079         int updatecfg = 0;
1080         int uninorth_child;
1081
1082         node = pci_device_to_OF_node(dev);
1083
1084         /* We don't want to enable USB controllers absent from the OF tree
1085          * (iBook second controller)
1086          */
1087         if (dev->vendor == PCI_VENDOR_ID_APPLE
1088             && dev->class == PCI_CLASS_SERIAL_USB_OHCI
1089             && !node) {
1090                 printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
1091                        pci_name(dev));
1092                 return -EINVAL;
1093         }
1094
1095         if (!node)
1096                 return 0;
1097
1098         uninorth_child = node->parent &&
1099                 of_device_is_compatible(node->parent, "uni-north");
1100
1101         /* Firewire & GMAC were disabled after PCI probe, the driver is
1102          * claiming them, we must re-enable them now.
1103          */
1104         if (uninorth_child && !strcmp(node->name, "firewire") &&
1105             (of_device_is_compatible(node, "pci106b,18") ||
1106              of_device_is_compatible(node, "pci106b,30") ||
1107              of_device_is_compatible(node, "pci11c1,5811"))) {
1108                 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
1109                 pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
1110                 updatecfg = 1;
1111         }
1112         if (uninorth_child && !strcmp(node->name, "ethernet") &&
1113             of_device_is_compatible(node, "gmac")) {
1114                 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
1115                 updatecfg = 1;
1116         }
1117
1118         /*
1119          * Fixup various header fields on 32 bits. We don't do that on
1120          * 64 bits as some of these have strange values behind the HT
1121          * bridge and we must not, for example, enable MWI or set the
1122          * cache line size on them.
1123          */
1124         if (updatecfg) {
1125                 u16 cmd;
1126
1127                 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1128                 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1129                         | PCI_COMMAND_INVALIDATE;
1130                 pci_write_config_word(dev, PCI_COMMAND, cmd);
1131                 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
1132
1133                 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1134                                       L1_CACHE_BYTES >> 2);
1135         }
1136
1137         return 0;
1138 }
1139
1140 void pmac_pci_fixup_ohci(struct pci_dev *dev)
1141 {
1142         struct device_node *node = pci_device_to_OF_node(dev);
1143
1144         /* We don't want to assign resources to USB controllers
1145          * absent from the OF tree (iBook second controller)
1146          */
1147         if (dev->class == PCI_CLASS_SERIAL_USB_OHCI && !node)
1148                 dev->resource[0].flags = 0;
1149 }
1150 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_ANY_ID, pmac_pci_fixup_ohci);
1151
1152 /* We power down some devices after they have been probed. They'll
1153  * be powered back on later on
1154  */
1155 void __init pmac_pcibios_after_init(void)
1156 {
1157         struct device_node* nd;
1158
1159         for_each_node_by_name(nd, "firewire") {
1160                 if (nd->parent && (of_device_is_compatible(nd, "pci106b,18") ||
1161                                    of_device_is_compatible(nd, "pci106b,30") ||
1162                                    of_device_is_compatible(nd, "pci11c1,5811"))
1163                     && of_device_is_compatible(nd->parent, "uni-north")) {
1164                         pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
1165                         pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
1166                 }
1167         }
1168         for_each_node_by_name(nd, "ethernet") {
1169                 if (nd->parent && of_device_is_compatible(nd, "gmac")
1170                     && of_device_is_compatible(nd->parent, "uni-north"))
1171                         pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
1172         }
1173 }
1174
1175 void pmac_pci_fixup_cardbus(struct pci_dev* dev)
1176 {
1177         if (!machine_is(powermac))
1178                 return;
1179         /*
1180          * Fix the interrupt routing on the various cardbus bridges
1181          * used on powerbooks
1182          */
1183         if (dev->vendor != PCI_VENDOR_ID_TI)
1184                 return;
1185         if (dev->device == PCI_DEVICE_ID_TI_1130 ||
1186             dev->device == PCI_DEVICE_ID_TI_1131) {
1187                 u8 val;
1188                 /* Enable PCI interrupt */
1189                 if (pci_read_config_byte(dev, 0x91, &val) == 0)
1190                         pci_write_config_byte(dev, 0x91, val | 0x30);
1191                 /* Disable ISA interrupt mode */
1192                 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1193                         pci_write_config_byte(dev, 0x92, val & ~0x06);
1194         }
1195         if (dev->device == PCI_DEVICE_ID_TI_1210 ||
1196             dev->device == PCI_DEVICE_ID_TI_1211 ||
1197             dev->device == PCI_DEVICE_ID_TI_1410 ||
1198             dev->device == PCI_DEVICE_ID_TI_1510) {
1199                 u8 val;
1200                 /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
1201                    signal out the MFUNC0 pin */
1202                 if (pci_read_config_byte(dev, 0x8c, &val) == 0)
1203                         pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
1204                 /* Disable ISA interrupt mode */
1205                 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1206                         pci_write_config_byte(dev, 0x92, val & ~0x06);
1207         }
1208 }
1209
1210 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
1211
1212 void pmac_pci_fixup_pciata(struct pci_dev* dev)
1213 {
1214        u8 progif = 0;
1215
1216        /*
1217         * On PowerMacs, we try to switch any PCI ATA controller to
1218         * fully native mode
1219         */
1220         if (!machine_is(powermac))
1221                 return;
1222
1223         /* Some controllers don't have the class IDE */
1224         if (dev->vendor == PCI_VENDOR_ID_PROMISE)
1225                 switch(dev->device) {
1226                 case PCI_DEVICE_ID_PROMISE_20246:
1227                 case PCI_DEVICE_ID_PROMISE_20262:
1228                 case PCI_DEVICE_ID_PROMISE_20263:
1229                 case PCI_DEVICE_ID_PROMISE_20265:
1230                 case PCI_DEVICE_ID_PROMISE_20267:
1231                 case PCI_DEVICE_ID_PROMISE_20268:
1232                 case PCI_DEVICE_ID_PROMISE_20269:
1233                 case PCI_DEVICE_ID_PROMISE_20270:
1234                 case PCI_DEVICE_ID_PROMISE_20271:
1235                 case PCI_DEVICE_ID_PROMISE_20275:
1236                 case PCI_DEVICE_ID_PROMISE_20276:
1237                 case PCI_DEVICE_ID_PROMISE_20277:
1238                         goto good;
1239                 }
1240         /* Others, check PCI class */
1241         if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1242                 return;
1243  good:
1244         pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1245         if ((progif & 5) != 5) {
1246                 printk(KERN_INFO "PCI: %s Forcing PCI IDE into native mode\n",
1247                        pci_name(dev));
1248                 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
1249                 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
1250                     (progif & 5) != 5)
1251                         printk(KERN_ERR "Rewrite of PROGIF failed !\n");
1252                 else {
1253                         /* Clear IO BARs, they will be reassigned */
1254                         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);
1255                         pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);
1256                         pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, 0);
1257                         pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, 0);
1258                 }
1259         }
1260 }
1261 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
1262 #endif /* CONFIG_PPC32 */
1263
1264 /*
1265  * Disable second function on K2-SATA, it's broken
1266  * and disable IO BARs on first one
1267  */
1268 static void fixup_k2_sata(struct pci_dev* dev)
1269 {
1270         int i;
1271         u16 cmd;
1272
1273         if (PCI_FUNC(dev->devfn) > 0) {
1274                 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1275                 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1276                 pci_write_config_word(dev, PCI_COMMAND, cmd);
1277                 for (i = 0; i < 6; i++) {
1278                         dev->resource[i].start = dev->resource[i].end = 0;
1279                         dev->resource[i].flags = 0;
1280                         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1281                                                0);
1282                 }
1283         } else {
1284                 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1285                 cmd &= ~PCI_COMMAND_IO;
1286                 pci_write_config_word(dev, PCI_COMMAND, cmd);
1287                 for (i = 0; i < 5; i++) {
1288                         dev->resource[i].start = dev->resource[i].end = 0;
1289                         dev->resource[i].flags = 0;
1290                         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1291                                                0);
1292                 }
1293         }
1294 }
1295 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);
1296
1297 /*
1298  * On U4 (aka CPC945) the PCIe root complex "P2P" bridge resource ranges aren't
1299  * configured by the firmware. The bridge itself seems to ignore them but it
1300  * causes problems with Linux which then re-assigns devices below the bridge,
1301  * thus changing addresses of those devices from what was in the device-tree,
1302  * which sucks when those are video cards using offb
1303  *
1304  * We could just mark it transparent but I prefer fixing up the resources to
1305  * properly show what's going on here, as I have some doubts about having them
1306  * badly configured potentially being an issue for DMA.
1307  *
1308  * We leave PIO alone, it seems to be fine
1309  *
1310  * Oh and there's another funny bug. The OF properties advertize the region
1311  * 0xf1000000..0xf1ffffff as being forwarded as memory space. But that's
1312  * actually not true, this region is the memory mapped config space. So we
1313  * also need to filter it out or we'll map things in the wrong place.
1314  */
1315 static void fixup_u4_pcie(struct pci_dev* dev)
1316 {
1317         struct pci_controller *host = pci_bus_to_host(dev->bus);
1318         struct resource *region = NULL;
1319         u32 reg;
1320         int i;
1321
1322         /* Only do that on PowerMac */
1323         if (!machine_is(powermac))
1324                 return;
1325
1326         /* Find the largest MMIO region */
1327         for (i = 0; i < 3; i++) {
1328                 struct resource *r = &host->mem_resources[i];
1329                 if (!(r->flags & IORESOURCE_MEM))
1330                         continue;
1331                 /* Skip the 0xf0xxxxxx..f2xxxxxx regions, we know they
1332                  * are reserved by HW for other things
1333                  */
1334                 if (r->start >= 0xf0000000 && r->start < 0xf3000000)
1335                         continue;
1336                 if (!region || resource_size(r) > resource_size(region))
1337                         region = r;
1338         }
1339         /* Nothing found, bail */
1340         if (region == 0)
1341                 return;
1342
1343         /* Print things out */
1344         printk(KERN_INFO "PCI: Fixup U4 PCIe bridge range: %pR\n", region);
1345
1346         /* Fixup bridge config space. We know it's a Mac, resource aren't
1347          * offset so let's just blast them as-is. We also know that they
1348          * fit in 32 bits
1349          */
1350         reg = ((region->start >> 16) & 0xfff0) | (region->end & 0xfff00000);
1351         pci_write_config_dword(dev, PCI_MEMORY_BASE, reg);
1352         pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0);
1353         pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
1354         pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0);
1355 }
1356 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_U4_PCIE, fixup_u4_pcie);