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