Pull altix-fpga-reset into release branch
[sfrench/cifs-2.6.git] / arch / ppc64 / kernel / pmac_pci.c
1 /*
2  * Support for PCI bridges found on Power Macintoshes.
3  * At present the "bandit" and "chaos" bridges are supported.
4  * Fortunately you access configuration space in the same
5  * way with either bridge.
6  *
7  * Copyright (C) 2003 Benjamin Herrenschmuidt (benh@kernel.crashing.org)
8  * Copyright (C) 1997 Paul Mackerras (paulus@samba.org)
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version
13  * 2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/pci.h>
18 #include <linux/delay.h>
19 #include <linux/string.h>
20 #include <linux/init.h>
21 #include <linux/bootmem.h>
22
23 #include <asm/sections.h>
24 #include <asm/io.h>
25 #include <asm/prom.h>
26 #include <asm/pci-bridge.h>
27 #include <asm/machdep.h>
28 #include <asm/pmac_feature.h>
29 #include <asm/iommu.h>
30
31 #include "pci.h"
32 #include "pmac.h"
33
34 #define DEBUG
35
36 #ifdef DEBUG
37 #define DBG(x...) printk(x)
38 #else
39 #define DBG(x...)
40 #endif
41
42 /* XXX Could be per-controller, but I don't think we risk anything by
43  * assuming we won't have both UniNorth and Bandit */
44 static int has_uninorth;
45 static struct pci_controller *u3_agp;
46 struct device_node *k2_skiplist[2];
47
48 static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
49 {
50         for (; node != 0;node = node->sibling) {
51                 int * bus_range;
52                 unsigned int *class_code;
53                 int len;
54
55                 /* For PCI<->PCI bridges or CardBus bridges, we go down */
56                 class_code = (unsigned int *) get_property(node, "class-code", NULL);
57                 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
58                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
59                         continue;
60                 bus_range = (int *) get_property(node, "bus-range", &len);
61                 if (bus_range != NULL && len > 2 * sizeof(int)) {
62                         if (bus_range[1] > higher)
63                                 higher = bus_range[1];
64                 }
65                 higher = fixup_one_level_bus_range(node->child, higher);
66         }
67         return higher;
68 }
69
70 /* This routine fixes the "bus-range" property of all bridges in the
71  * system since they tend to have their "last" member wrong on macs
72  *
73  * Note that the bus numbers manipulated here are OF bus numbers, they
74  * are not Linux bus numbers.
75  */
76 static void __init fixup_bus_range(struct device_node *bridge)
77 {
78         int * bus_range;
79         int len;
80
81         /* Lookup the "bus-range" property for the hose */
82         bus_range = (int *) get_property(bridge, "bus-range", &len);
83         if (bus_range == NULL || len < 2 * sizeof(int)) {
84                 printk(KERN_WARNING "Can't get bus-range for %s\n",
85                                bridge->full_name);
86                 return;
87         }
88         bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
89 }
90
91 /*
92  * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
93  *
94  * The "Bandit" version is present in all early PCI PowerMacs,
95  * and up to the first ones using Grackle. Some machines may
96  * have 2 bandit controllers (2 PCI busses).
97  *
98  * "Chaos" is used in some "Bandit"-type machines as a bridge
99  * for the separate display bus. It is accessed the same
100  * way as bandit, but cannot be probed for devices. It therefore
101  * has its own config access functions.
102  *
103  * The "UniNorth" version is present in all Core99 machines
104  * (iBook, G4, new IMacs, and all the recent Apple machines).
105  * It contains 3 controllers in one ASIC.
106  *
107  * The U3 is the bridge used on G5 machines. It contains on
108  * AGP bus which is dealt with the old UniNorth access routines
109  * and an HyperTransport bus which uses its own set of access
110  * functions.
111  */
112
113 #define MACRISC_CFA0(devfn, off)        \
114         ((1 << (unsigned long)PCI_SLOT(dev_fn)) \
115         | (((unsigned long)PCI_FUNC(dev_fn)) << 8) \
116         | (((unsigned long)(off)) & 0xFCUL))
117
118 #define MACRISC_CFA1(bus, devfn, off)   \
119         ((((unsigned long)(bus)) << 16) \
120         |(((unsigned long)(devfn)) << 8) \
121         |(((unsigned long)(off)) & 0xFCUL) \
122         |1UL)
123
124 static unsigned long __pmac macrisc_cfg_access(struct pci_controller* hose,
125                                                u8 bus, u8 dev_fn, u8 offset)
126 {
127         unsigned int caddr;
128
129         if (bus == hose->first_busno) {
130                 if (dev_fn < (11 << 3))
131                         return 0;
132                 caddr = MACRISC_CFA0(dev_fn, offset);
133         } else
134                 caddr = MACRISC_CFA1(bus, dev_fn, offset);
135
136         /* Uninorth will return garbage if we don't read back the value ! */
137         do {
138                 out_le32(hose->cfg_addr, caddr);
139         } while (in_le32(hose->cfg_addr) != caddr);
140
141         offset &= has_uninorth ? 0x07 : 0x03;
142         return ((unsigned long)hose->cfg_data) + offset;
143 }
144
145 static int __pmac macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
146                                       int offset, int len, u32 *val)
147 {
148         struct pci_controller *hose;
149         unsigned long addr;
150
151         hose = pci_bus_to_host(bus);
152         if (hose == NULL)
153                 return PCIBIOS_DEVICE_NOT_FOUND;
154
155         addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
156         if (!addr)
157                 return PCIBIOS_DEVICE_NOT_FOUND;
158         /*
159          * Note: the caller has already checked that offset is
160          * suitably aligned and that len is 1, 2 or 4.
161          */
162         switch (len) {
163         case 1:
164                 *val = in_8((u8 *)addr);
165                 break;
166         case 2:
167                 *val = in_le16((u16 *)addr);
168                 break;
169         default:
170                 *val = in_le32((u32 *)addr);
171                 break;
172         }
173         return PCIBIOS_SUCCESSFUL;
174 }
175
176 static int __pmac macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
177                                        int offset, int len, u32 val)
178 {
179         struct pci_controller *hose;
180         unsigned long addr;
181
182         hose = pci_bus_to_host(bus);
183         if (hose == NULL)
184                 return PCIBIOS_DEVICE_NOT_FOUND;
185
186         addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
187         if (!addr)
188                 return PCIBIOS_DEVICE_NOT_FOUND;
189         /*
190          * Note: the caller has already checked that offset is
191          * suitably aligned and that len is 1, 2 or 4.
192          */
193         switch (len) {
194         case 1:
195                 out_8((u8 *)addr, val);
196                 (void) in_8((u8 *)addr);
197                 break;
198         case 2:
199                 out_le16((u16 *)addr, val);
200                 (void) in_le16((u16 *)addr);
201                 break;
202         default:
203                 out_le32((u32 *)addr, val);
204                 (void) in_le32((u32 *)addr);
205                 break;
206         }
207         return PCIBIOS_SUCCESSFUL;
208 }
209
210 static struct pci_ops macrisc_pci_ops =
211 {
212         macrisc_read_config,
213         macrisc_write_config
214 };
215
216 /*
217  * These versions of U3 HyperTransport config space access ops do not
218  * implement self-view of the HT host yet
219  */
220
221 /*
222  * This function deals with some "special cases" devices.
223  *
224  *  0 -> No special case
225  *  1 -> Skip the device but act as if the access was successfull
226  *       (return 0xff's on reads, eventually, cache config space
227  *       accesses in a later version)
228  * -1 -> Hide the device (unsuccessful acess)
229  */
230 static int u3_ht_skip_device(struct pci_controller *hose,
231                              struct pci_bus *bus, unsigned int devfn)
232 {
233         struct device_node *busdn, *dn;
234         int i;
235
236         /* We only allow config cycles to devices that are in OF device-tree
237          * as we are apparently having some weird things going on with some
238          * revs of K2 on recent G5s
239          */
240         if (bus->self)
241                 busdn = pci_device_to_OF_node(bus->self);
242         else
243                 busdn = hose->arch_data;
244         for (dn = busdn->child; dn; dn = dn->sibling)
245                 if (dn->data && PCI_DN(dn)->devfn == devfn)
246                         break;
247         if (dn == NULL)
248                 return -1;
249
250         /*
251          * When a device in K2 is powered down, we die on config
252          * cycle accesses. Fix that here.
253          */
254         for (i=0; i<2; i++)
255                 if (k2_skiplist[i] == dn)
256                         return 1;
257
258         return 0;
259 }
260
261 #define U3_HT_CFA0(devfn, off)          \
262                 ((((unsigned long)devfn) << 8) | offset)
263 #define U3_HT_CFA1(bus, devfn, off)     \
264                 (U3_HT_CFA0(devfn, off) \
265                 + (((unsigned long)bus) << 16) \
266                 + 0x01000000UL)
267
268 static unsigned long __pmac u3_ht_cfg_access(struct pci_controller* hose,
269                                              u8 bus, u8 devfn, u8 offset)
270 {
271         if (bus == hose->first_busno) {
272                 /* For now, we don't self probe U3 HT bridge */
273                 if (PCI_SLOT(devfn) == 0)
274                         return 0;
275                 return ((unsigned long)hose->cfg_data) + U3_HT_CFA0(devfn, offset);
276         } else
277                 return ((unsigned long)hose->cfg_data) + U3_HT_CFA1(bus, devfn, offset);
278 }
279
280 static int __pmac u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
281                                     int offset, int len, u32 *val)
282 {
283         struct pci_controller *hose;
284         unsigned long addr;
285
286
287         hose = pci_bus_to_host(bus);      
288         if (hose == NULL)
289                 return PCIBIOS_DEVICE_NOT_FOUND;
290
291         addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
292         if (!addr)
293                 return PCIBIOS_DEVICE_NOT_FOUND;
294
295         switch (u3_ht_skip_device(hose, bus, devfn)) {
296         case 0:
297                 break;
298         case 1:
299                 switch (len) {
300                 case 1:
301                         *val = 0xff; break;
302                 case 2:
303                         *val = 0xffff; break;
304                 default:
305                         *val = 0xfffffffful; break;
306                 }
307                 return PCIBIOS_SUCCESSFUL;
308         default:
309                 return PCIBIOS_DEVICE_NOT_FOUND;
310         }
311
312         /*
313          * Note: the caller has already checked that offset is
314          * suitably aligned and that len is 1, 2 or 4.
315          */
316         switch (len) {
317         case 1:
318                 *val = in_8((u8 *)addr);
319                 break;
320         case 2:
321                 *val = in_le16((u16 *)addr);
322                 break;
323         default:
324                 *val = in_le32((u32 *)addr);
325                 break;
326         }
327         return PCIBIOS_SUCCESSFUL;
328 }
329
330 static int __pmac u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
331                                      int offset, int len, u32 val)
332 {
333         struct pci_controller *hose;
334         unsigned long addr;
335
336         hose = pci_bus_to_host(bus);
337         if (hose == NULL)
338                 return PCIBIOS_DEVICE_NOT_FOUND;
339
340         addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
341         if (!addr)
342                 return PCIBIOS_DEVICE_NOT_FOUND;
343
344         switch (u3_ht_skip_device(hose, bus, devfn)) {
345         case 0:
346                 break;
347         case 1:
348                 return PCIBIOS_SUCCESSFUL;
349         default:
350                 return PCIBIOS_DEVICE_NOT_FOUND;
351         }
352
353         /*
354          * Note: the caller has already checked that offset is
355          * suitably aligned and that len is 1, 2 or 4.
356          */
357         switch (len) {
358         case 1:
359                 out_8((u8 *)addr, val);
360                 (void) in_8((u8 *)addr);
361                 break;
362         case 2:
363                 out_le16((u16 *)addr, val);
364                 (void) in_le16((u16 *)addr);
365                 break;
366         default:
367                 out_le32((u32 *)addr, val);
368                 (void) in_le32((u32 *)addr);
369                 break;
370         }
371         return PCIBIOS_SUCCESSFUL;
372 }
373
374 static struct pci_ops u3_ht_pci_ops =
375 {
376         u3_ht_read_config,
377         u3_ht_write_config
378 };
379
380 static void __init setup_u3_agp(struct pci_controller* hose)
381 {
382         /* On G5, we move AGP up to high bus number so we don't need
383          * to reassign bus numbers for HT. If we ever have P2P bridges
384          * on AGP, we'll have to move pci_assign_all_busses to the
385          * pci_controller structure so we enable it for AGP and not for
386          * HT childs.
387          * We hard code the address because of the different size of
388          * the reg address cell, we shall fix that by killing struct
389          * reg_property and using some accessor functions instead
390          */
391         hose->first_busno = 0xf0;
392         hose->last_busno = 0xff;
393         has_uninorth = 1;
394         hose->ops = &macrisc_pci_ops;
395         hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
396         hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
397
398         u3_agp = hose;
399 }
400
401 static void __init setup_u3_ht(struct pci_controller* hose)
402 {
403         struct device_node *np = (struct device_node *)hose->arch_data;
404         int i, cur;
405
406         hose->ops = &u3_ht_pci_ops;
407
408         /* We hard code the address because of the different size of
409          * the reg address cell, we shall fix that by killing struct
410          * reg_property and using some accessor functions instead
411          */
412         hose->cfg_data = (volatile unsigned char *)ioremap(0xf2000000, 0x02000000);
413
414         /*
415          * /ht node doesn't expose a "ranges" property, so we "remove" regions that
416          * have been allocated to AGP. So far, this version of the code doesn't assign
417          * any of the 0xfxxxxxxx "fine" memory regions to /ht.
418          * We need to fix that sooner or later by either parsing all child "ranges"
419          * properties or figuring out the U3 address space decoding logic and
420          * then read it's configuration register (if any).
421          */
422         hose->io_base_phys = 0xf4000000;
423         hose->io_base_virt = ioremap(hose->io_base_phys, 0x00400000);
424         isa_io_base = pci_io_base = (unsigned long) hose->io_base_virt;
425         hose->io_resource.name = np->full_name;
426         hose->io_resource.start = 0;
427         hose->io_resource.end = 0x003fffff;
428         hose->io_resource.flags = IORESOURCE_IO;
429         hose->pci_mem_offset = 0;
430         hose->first_busno = 0;
431         hose->last_busno = 0xef;
432         hose->mem_resources[0].name = np->full_name;
433         hose->mem_resources[0].start = 0x80000000;
434         hose->mem_resources[0].end = 0xefffffff;
435         hose->mem_resources[0].flags = IORESOURCE_MEM;
436
437         if (u3_agp == NULL) {
438                 DBG("U3 has no AGP, using full resource range\n");
439                 return;
440         }
441
442         /* We "remove" the AGP resources from the resources allocated to HT, that
443          * is we create "holes". However, that code does assumptions that so far
444          * happen to be true (cross fingers...), typically that resources in the
445          * AGP node are properly ordered
446          */
447         cur = 0;
448         for (i=0; i<3; i++) {
449                 struct resource *res = &u3_agp->mem_resources[i];
450                 if (res->flags != IORESOURCE_MEM)
451                         continue;
452                 /* We don't care about "fine" resources */
453                 if (res->start >= 0xf0000000)
454                         continue;
455                 /* Check if it's just a matter of "shrinking" us in one direction */
456                 if (hose->mem_resources[cur].start == res->start) {
457                         DBG("U3/HT: shrink start of %d, %08lx -> %08lx\n",
458                             cur, hose->mem_resources[cur].start, res->end + 1);
459                         hose->mem_resources[cur].start = res->end + 1;
460                         continue;
461                 }
462                 if (hose->mem_resources[cur].end == res->end) {
463                         DBG("U3/HT: shrink end of %d, %08lx -> %08lx\n",
464                             cur, hose->mem_resources[cur].end, res->start - 1);
465                         hose->mem_resources[cur].end = res->start - 1;
466                         continue;
467                 }
468                 /* No, it's not the case, we need a hole */
469                 if (cur == 2) {
470                         /* not enough resources for a hole, we drop part of the range */
471                         printk(KERN_WARNING "Running out of resources for /ht host !\n");
472                         hose->mem_resources[cur].end = res->start - 1;
473                         continue;
474                 }               
475                 cur++;
476                 DBG("U3/HT: hole, %d end at %08lx, %d start at %08lx\n",
477                     cur-1, res->start - 1, cur, res->end + 1);
478                 hose->mem_resources[cur].name = np->full_name;
479                 hose->mem_resources[cur].flags = IORESOURCE_MEM;
480                 hose->mem_resources[cur].start = res->end + 1;
481                 hose->mem_resources[cur].end = hose->mem_resources[cur-1].end;
482                 hose->mem_resources[cur-1].end = res->start - 1;
483         }
484 }
485
486 static void __init pmac_process_bridge_OF_ranges(struct pci_controller *hose,
487                            struct device_node *dev, int primary)
488 {
489         static unsigned int static_lc_ranges[2024];
490         unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
491         unsigned int size;
492         int rlen = 0, orig_rlen;
493         int memno = 0;
494         struct resource *res;
495         int np, na = prom_n_addr_cells(dev);
496
497         np = na + 5;
498
499         /* First we try to merge ranges to fix a problem with some pmacs
500          * that can have more than 3 ranges, fortunately using contiguous
501          * addresses -- BenH
502          */
503         dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
504         if (!dt_ranges)
505                 return;
506         /*      lc_ranges = alloc_bootmem(rlen);*/
507         lc_ranges = static_lc_ranges;
508         if (!lc_ranges)
509                 return; /* what can we do here ? */
510         memcpy(lc_ranges, dt_ranges, rlen);
511         orig_rlen = rlen;
512
513         /* Let's work on a copy of the "ranges" property instead of damaging
514          * the device-tree image in memory
515          */
516         ranges = lc_ranges;
517         prev = NULL;
518         while ((rlen -= np * sizeof(unsigned int)) >= 0) {
519                 if (prev) {
520                         if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
521                                 (prev[2] + prev[na+4]) == ranges[2] &&
522                                 (prev[na+2] + prev[na+4]) == ranges[na+2]) {
523                                 prev[na+4] += ranges[na+4];
524                                 ranges[0] = 0;
525                                 ranges += np;
526                                 continue;
527                         }
528                 }
529                 prev = ranges;
530                 ranges += np;
531         }
532
533         /*
534          * The ranges property is laid out as an array of elements,
535          * each of which comprises:
536          *   cells 0 - 2:       a PCI address
537          *   cells 3 or 3+4:    a CPU physical address
538          *                      (size depending on dev->n_addr_cells)
539          *   cells 4+5 or 5+6:  the size of the range
540          */
541         ranges = lc_ranges;
542         rlen = orig_rlen;
543         while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
544                 res = NULL;
545                 size = ranges[na+4];
546                 switch (ranges[0] >> 24) {
547                 case 1:         /* I/O space */
548                         if (ranges[2] != 0)
549                                 break;
550                         hose->io_base_phys = ranges[na+2];
551                         /* limit I/O space to 16MB */
552                         if (size > 0x01000000)
553                                 size = 0x01000000;
554                         hose->io_base_virt = ioremap(ranges[na+2], size);
555                         if (primary)
556                                 isa_io_base = (unsigned long) hose->io_base_virt;
557                         res = &hose->io_resource;
558                         res->flags = IORESOURCE_IO;
559                         res->start = ranges[2];
560                         break;
561                 case 2:         /* memory space */
562                         memno = 0;
563                         if (ranges[1] == 0 && ranges[2] == 0
564                             && ranges[na+4] <= (16 << 20)) {
565                                 /* 1st 16MB, i.e. ISA memory area */
566 #if 0
567                                 if (primary)
568                                         isa_mem_base = ranges[na+2];
569 #endif
570                                 memno = 1;
571                         }
572                         while (memno < 3 && hose->mem_resources[memno].flags)
573                                 ++memno;
574                         if (memno == 0)
575                                 hose->pci_mem_offset = ranges[na+2] - ranges[2];
576                         if (memno < 3) {
577                                 res = &hose->mem_resources[memno];
578                                 res->flags = IORESOURCE_MEM;
579                                 res->start = ranges[na+2];
580                         }
581                         break;
582                 }
583                 if (res != NULL) {
584                         res->name = dev->full_name;
585                         res->end = res->start + size - 1;
586                         res->parent = NULL;
587                         res->sibling = NULL;
588                         res->child = NULL;
589                 }
590                 ranges += np;
591         }
592 }
593
594 /*
595  * We assume that if we have a G3 powermac, we have one bridge called
596  * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
597  * if we have one or more bandit or chaos bridges, we don't have a MPC106.
598  */
599 static int __init add_bridge(struct device_node *dev)
600 {
601         int len;
602         struct pci_controller *hose;
603         char* disp_name;
604         int *bus_range;
605         int primary = 1;
606         struct property *of_prop;
607
608         DBG("Adding PCI host bridge %s\n", dev->full_name);
609
610         bus_range = (int *) get_property(dev, "bus-range", &len);
611         if (bus_range == NULL || len < 2 * sizeof(int)) {
612                 printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n",
613                         dev->full_name);
614         }
615
616         hose = alloc_bootmem(sizeof(struct pci_controller));
617         if (hose == NULL)
618                 return -ENOMEM;
619         pci_setup_pci_controller(hose);
620
621         hose->arch_data = dev;
622         hose->first_busno = bus_range ? bus_range[0] : 0;
623         hose->last_busno = bus_range ? bus_range[1] : 0xff;
624
625         of_prop = alloc_bootmem(sizeof(struct property) +
626                                 sizeof(hose->global_number));
627         if (of_prop) {
628                 memset(of_prop, 0, sizeof(struct property));
629                 of_prop->name = "linux,pci-domain";
630                 of_prop->length = sizeof(hose->global_number);
631                 of_prop->value = (unsigned char *)&of_prop[1];
632                 memcpy(of_prop->value, &hose->global_number, sizeof(hose->global_number));
633                 prom_add_property(dev, of_prop);
634         }
635
636         disp_name = NULL;
637         if (device_is_compatible(dev, "u3-agp")) {
638                 setup_u3_agp(hose);
639                 disp_name = "U3-AGP";
640                 primary = 0;
641         } else if (device_is_compatible(dev, "u3-ht")) {
642                 setup_u3_ht(hose);
643                 disp_name = "U3-HT";
644                 primary = 1;
645         }
646         printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n",
647                 disp_name, hose->first_busno, hose->last_busno);
648
649         /* Interpret the "ranges" property */
650         /* This also maps the I/O region and sets isa_io/mem_base */
651         pmac_process_bridge_OF_ranges(hose, dev, primary);
652
653         /* Fixup "bus-range" OF property */
654         fixup_bus_range(dev);
655
656         return 0;
657 }
658
659 /*
660  * We use our own read_irq_line here because PCI_INTERRUPT_PIN is
661  * crap on some of Apple ASICs. We unconditionally use the Open Firmware
662  * interrupt number as this is always right.
663  */
664 static int pmac_pci_read_irq_line(struct pci_dev *pci_dev)
665 {
666         struct device_node *node;
667
668         node = pci_device_to_OF_node(pci_dev);
669         if (node == NULL)
670                 return -1;
671         if (node->n_intrs == 0)
672                 return -1;
673         pci_dev->irq = node->intrs[0].line;
674         pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
675
676         return 0;
677 }
678
679 void __init pmac_pcibios_fixup(void)
680 {
681         struct pci_dev *dev = NULL;
682
683         for_each_pci_dev(dev)
684                 pmac_pci_read_irq_line(dev);
685 }
686
687 static void __init pmac_fixup_phb_resources(void)
688 {
689         struct pci_controller *hose, *tmp;
690         
691         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
692                 unsigned long offset = (unsigned long)hose->io_base_virt - pci_io_base;
693                 hose->io_resource.start += offset;
694                 hose->io_resource.end += offset;
695                 printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
696                        hose->global_number,
697                        hose->io_resource.start, hose->io_resource.end);
698         }
699 }
700
701 void __init pmac_pci_init(void)
702 {
703         struct device_node *np, *root;
704         struct device_node *ht = NULL;
705
706         /* Probe root PCI hosts, that is on U3 the AGP host and the
707          * HyperTransport host. That one is actually "kept" around
708          * and actually added last as it's resource management relies
709          * on the AGP resources to have been setup first
710          */
711         root = of_find_node_by_path("/");
712         if (root == NULL) {
713                 printk(KERN_CRIT "pmac_find_bridges: can't find root of device tree\n");
714                 return;
715         }
716         for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
717                 if (np->name == NULL)
718                         continue;
719                 if (strcmp(np->name, "pci") == 0) {
720                         if (add_bridge(np) == 0)
721                                 of_node_get(np);
722                 }
723                 if (strcmp(np->name, "ht") == 0) {
724                         of_node_get(np);
725                         ht = np;
726                 }
727         }
728         of_node_put(root);
729
730         /* Now setup the HyperTransport host if we found any
731          */
732         if (ht && add_bridge(ht) != 0)
733                 of_node_put(ht);
734
735         /* Fixup the IO resources on our host bridges as the common code
736          * does it only for childs of the host bridges
737          */
738         pmac_fixup_phb_resources();
739
740         /* Setup the linkage between OF nodes and PHBs */ 
741         pci_devs_phb_init();
742
743         /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
744          * assume there is no P2P bridge on the AGP bus, which should be a
745          * safe assumptions hopefully.
746          */
747         if (u3_agp) {
748                 struct device_node *np = u3_agp->arch_data;
749                 PCI_DN(np)->busno = 0xf0;
750                 for (np = np->child; np; np = np->sibling)
751                         PCI_DN(np)->busno = 0xf0;
752         }
753
754         pmac_check_ht_link();
755
756         /* Tell pci.c to not use the common resource allocation mecanism */
757         pci_probe_only = 1;
758         
759         /* Allow all IO */
760         io_page_mask = -1;
761 }
762
763 /*
764  * Disable second function on K2-SATA, it's broken
765  * and disable IO BARs on first one
766  */
767 static void fixup_k2_sata(struct pci_dev* dev)
768 {
769         int i;
770         u16 cmd;
771
772         if (PCI_FUNC(dev->devfn) > 0) {
773                 pci_read_config_word(dev, PCI_COMMAND, &cmd);
774                 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
775                 pci_write_config_word(dev, PCI_COMMAND, cmd);
776                 for (i = 0; i < 6; i++) {
777                         dev->resource[i].start = dev->resource[i].end = 0;
778                         dev->resource[i].flags = 0;
779                         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
780                 }
781         } else {
782                 pci_read_config_word(dev, PCI_COMMAND, &cmd);
783                 cmd &= ~PCI_COMMAND_IO;
784                 pci_write_config_word(dev, PCI_COMMAND, cmd);
785                 for (i = 0; i < 5; i++) {
786                         dev->resource[i].start = dev->resource[i].end = 0;
787                         dev->resource[i].flags = 0;
788                         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
789                 }
790         }
791 }
792 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);
793