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