2 * Support for PCI bridges found on Power Macintoshes.
4 * Copyright (C) 2003-2005 Benjamin Herrenschmuidt (benh@kernel.crashing.org)
5 * Copyright (C) 1997 Paul Mackerras (paulus@samba.org)
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.
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>
21 #include <asm/sections.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>
33 #define DBG(x...) printk(x)
38 static int add_bridge(struct device_node *dev);
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;
44 static struct pci_controller *u3_agp;
45 static struct pci_controller *u4_pcie;
46 static struct pci_controller *u3_ht;
48 static int has_second_ohare;
49 #endif /* CONFIG_PPC64 */
51 extern u8 pci_cache_line_size;
52 extern int pcibios_assign_bus_offset;
54 struct device_node *k2_skiplist[2];
57 * Magic constants for enabling cache coherency in the bandit/PSX bridge.
59 #define BANDIT_DEVID_2 8
60 #define BANDIT_REVID 3
62 #define BANDIT_DEVNUM 11
63 #define BANDIT_MAGIC 0x50
64 #define BANDIT_COHERENT 0x40
66 static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
68 for (; node != 0;node = node->sibling) {
70 unsigned int *class_code;
73 /* For PCI<->PCI bridges or CardBus bridges, we go down */
74 class_code = (unsigned int *) 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))
78 bus_range = (int *) 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];
83 higher = fixup_one_level_bus_range(node->child, higher);
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
91 * Note that the bus numbers manipulated here are OF bus numbers, they
92 * are not Linux bus numbers.
94 static void __init fixup_bus_range(struct device_node *bridge)
99 /* Lookup the "bus-range" property for the hose */
100 bus_range = (int *) get_property(bridge, "bus-range", &len);
101 if (bus_range == NULL || len < 2 * sizeof(int))
103 bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
107 * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
109 * The "Bandit" version is present in all early PCI PowerMacs,
110 * and up to the first ones using Grackle. Some machines may
111 * have 2 bandit controllers (2 PCI busses).
113 * "Chaos" is used in some "Bandit"-type machines as a bridge
114 * for the separate display bus. It is accessed the same
115 * way as bandit, but cannot be probed for devices. It therefore
116 * has its own config access functions.
118 * The "UniNorth" version is present in all Core99 machines
119 * (iBook, G4, new IMacs, and all the recent Apple machines).
120 * It contains 3 controllers in one ASIC.
122 * The U3 is the bridge used on G5 machines. It contains an
123 * AGP bus which is dealt with the old UniNorth access routines
124 * and a HyperTransport bus which uses its own set of access
128 #define MACRISC_CFA0(devfn, off) \
129 ((1 << (unsigned int)PCI_SLOT(dev_fn)) \
130 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
131 | (((unsigned int)(off)) & 0xFCUL))
133 #define MACRISC_CFA1(bus, devfn, off) \
134 ((((unsigned int)(bus)) << 16) \
135 |(((unsigned int)(devfn)) << 8) \
136 |(((unsigned int)(off)) & 0xFCUL) \
139 static volatile void __iomem *macrisc_cfg_access(struct pci_controller* hose,
140 u8 bus, u8 dev_fn, u8 offset)
144 if (bus == hose->first_busno) {
145 if (dev_fn < (11 << 3))
147 caddr = MACRISC_CFA0(dev_fn, offset);
149 caddr = MACRISC_CFA1(bus, dev_fn, offset);
151 /* Uninorth will return garbage if we don't read back the value ! */
153 out_le32(hose->cfg_addr, caddr);
154 } while (in_le32(hose->cfg_addr) != caddr);
156 offset &= has_uninorth ? 0x07 : 0x03;
157 return hose->cfg_data + offset;
160 static int macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
161 int offset, int len, u32 *val)
163 struct pci_controller *hose;
164 volatile void __iomem *addr;
166 hose = pci_bus_to_host(bus);
168 return PCIBIOS_DEVICE_NOT_FOUND;
170 return PCIBIOS_BAD_REGISTER_NUMBER;
171 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
173 return PCIBIOS_DEVICE_NOT_FOUND;
175 * Note: the caller has already checked that offset is
176 * suitably aligned and that len is 1, 2 or 4.
183 *val = in_le16(addr);
186 *val = in_le32(addr);
189 return PCIBIOS_SUCCESSFUL;
192 static int macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
193 int offset, int len, u32 val)
195 struct pci_controller *hose;
196 volatile void __iomem *addr;
198 hose = pci_bus_to_host(bus);
200 return PCIBIOS_DEVICE_NOT_FOUND;
202 return PCIBIOS_BAD_REGISTER_NUMBER;
203 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
205 return PCIBIOS_DEVICE_NOT_FOUND;
207 * Note: the caller has already checked that offset is
208 * suitably aligned and that len is 1, 2 or 4.
217 (void) in_le16(addr);
221 (void) in_le32(addr);
224 return PCIBIOS_SUCCESSFUL;
227 static struct pci_ops macrisc_pci_ops =
235 * Verify that a specific (bus, dev_fn) exists on chaos
237 static int chaos_validate_dev(struct pci_bus *bus, int devfn, int offset)
239 struct device_node *np;
240 u32 *vendor, *device;
243 return PCIBIOS_BAD_REGISTER_NUMBER;
244 np = pci_busdev_to_OF_node(bus, devfn);
246 return PCIBIOS_DEVICE_NOT_FOUND;
248 vendor = (u32 *)get_property(np, "vendor-id", NULL);
249 device = (u32 *)get_property(np, "device-id", NULL);
250 if (vendor == NULL || device == NULL)
251 return PCIBIOS_DEVICE_NOT_FOUND;
253 if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
254 && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
255 return PCIBIOS_BAD_REGISTER_NUMBER;
257 return PCIBIOS_SUCCESSFUL;
261 chaos_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
264 int result = chaos_validate_dev(bus, devfn, offset);
265 if (result == PCIBIOS_BAD_REGISTER_NUMBER)
267 if (result != PCIBIOS_SUCCESSFUL)
269 return macrisc_read_config(bus, devfn, offset, len, val);
273 chaos_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
276 int result = chaos_validate_dev(bus, devfn, offset);
277 if (result != PCIBIOS_SUCCESSFUL)
279 return macrisc_write_config(bus, devfn, offset, len, val);
282 static struct pci_ops chaos_pci_ops =
288 static void __init setup_chaos(struct pci_controller *hose,
289 struct resource *addr)
291 /* assume a `chaos' bridge */
292 hose->ops = &chaos_pci_ops;
293 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
294 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
296 #endif /* CONFIG_PPC32 */
300 * These versions of U3 HyperTransport config space access ops do not
301 * implement self-view of the HT host yet
305 * This function deals with some "special cases" devices.
307 * 0 -> No special case
308 * 1 -> Skip the device but act as if the access was successfull
309 * (return 0xff's on reads, eventually, cache config space
310 * accesses in a later version)
311 * -1 -> Hide the device (unsuccessful acess)
313 static int u3_ht_skip_device(struct pci_controller *hose,
314 struct pci_bus *bus, unsigned int devfn)
316 struct device_node *busdn, *dn;
319 /* We only allow config cycles to devices that are in OF device-tree
320 * as we are apparently having some weird things going on with some
321 * revs of K2 on recent G5s
324 busdn = pci_device_to_OF_node(bus->self);
326 busdn = hose->arch_data;
327 for (dn = busdn->child; dn; dn = dn->sibling)
328 if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
334 * When a device in K2 is powered down, we die on config
335 * cycle accesses. Fix that here.
338 if (k2_skiplist[i] == dn)
344 #define U3_HT_CFA0(devfn, off) \
345 ((((unsigned int)devfn) << 8) | offset)
346 #define U3_HT_CFA1(bus, devfn, off) \
347 (U3_HT_CFA0(devfn, off) \
348 + (((unsigned int)bus) << 16) \
351 static volatile void __iomem *u3_ht_cfg_access(struct pci_controller* hose,
352 u8 bus, u8 devfn, u8 offset)
354 if (bus == hose->first_busno) {
355 /* For now, we don't self probe U3 HT bridge */
356 if (PCI_SLOT(devfn) == 0)
358 return hose->cfg_data + U3_HT_CFA0(devfn, offset);
360 return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);
363 static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
364 int offset, int len, u32 *val)
366 struct pci_controller *hose;
367 volatile void __iomem *addr;
369 hose = pci_bus_to_host(bus);
371 return PCIBIOS_DEVICE_NOT_FOUND;
373 return PCIBIOS_BAD_REGISTER_NUMBER;
374 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
376 return PCIBIOS_DEVICE_NOT_FOUND;
378 switch (u3_ht_skip_device(hose, bus, devfn)) {
386 *val = 0xffff; break;
388 *val = 0xfffffffful; break;
390 return PCIBIOS_SUCCESSFUL;
392 return PCIBIOS_DEVICE_NOT_FOUND;
396 * Note: the caller has already checked that offset is
397 * suitably aligned and that len is 1, 2 or 4.
404 *val = in_le16(addr);
407 *val = in_le32(addr);
410 return PCIBIOS_SUCCESSFUL;
413 static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
414 int offset, int len, u32 val)
416 struct pci_controller *hose;
417 volatile void __iomem *addr;
419 hose = pci_bus_to_host(bus);
421 return PCIBIOS_DEVICE_NOT_FOUND;
423 return PCIBIOS_BAD_REGISTER_NUMBER;
424 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
426 return PCIBIOS_DEVICE_NOT_FOUND;
428 switch (u3_ht_skip_device(hose, bus, devfn)) {
432 return PCIBIOS_SUCCESSFUL;
434 return PCIBIOS_DEVICE_NOT_FOUND;
438 * Note: the caller has already checked that offset is
439 * suitably aligned and that len is 1, 2 or 4.
448 (void) in_le16(addr);
451 out_le32((u32 __iomem *)addr, val);
452 (void) in_le32(addr);
455 return PCIBIOS_SUCCESSFUL;
458 static struct pci_ops u3_ht_pci_ops =
464 #define U4_PCIE_CFA0(devfn, off) \
465 ((1 << ((unsigned int)PCI_SLOT(dev_fn))) \
466 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
467 | ((((unsigned int)(off)) >> 8) << 28) \
468 | (((unsigned int)(off)) & 0xfcU))
470 #define U4_PCIE_CFA1(bus, devfn, off) \
471 ((((unsigned int)(bus)) << 16) \
472 |(((unsigned int)(devfn)) << 8) \
473 | ((((unsigned int)(off)) >> 8) << 28) \
474 |(((unsigned int)(off)) & 0xfcU) \
477 static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose,
478 u8 bus, u8 dev_fn, int offset)
482 if (bus == hose->first_busno) {
483 caddr = U4_PCIE_CFA0(dev_fn, offset);
485 caddr = U4_PCIE_CFA1(bus, dev_fn, offset);
487 /* Uninorth will return garbage if we don't read back the value ! */
489 out_le32(hose->cfg_addr, caddr);
490 } while (in_le32(hose->cfg_addr) != caddr);
493 return hose->cfg_data + offset;
496 static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
497 int offset, int len, u32 *val)
499 struct pci_controller *hose;
500 volatile void __iomem *addr;
502 hose = pci_bus_to_host(bus);
504 return PCIBIOS_DEVICE_NOT_FOUND;
505 if (offset >= 0x1000)
506 return PCIBIOS_BAD_REGISTER_NUMBER;
507 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
509 return PCIBIOS_DEVICE_NOT_FOUND;
511 * Note: the caller has already checked that offset is
512 * suitably aligned and that len is 1, 2 or 4.
519 *val = in_le16(addr);
522 *val = in_le32(addr);
525 return PCIBIOS_SUCCESSFUL;
528 static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
529 int offset, int len, u32 val)
531 struct pci_controller *hose;
532 volatile void __iomem *addr;
534 hose = pci_bus_to_host(bus);
536 return PCIBIOS_DEVICE_NOT_FOUND;
537 if (offset >= 0x1000)
538 return PCIBIOS_BAD_REGISTER_NUMBER;
539 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
541 return PCIBIOS_DEVICE_NOT_FOUND;
543 * Note: the caller has already checked that offset is
544 * suitably aligned and that len is 1, 2 or 4.
553 (void) in_le16(addr);
557 (void) in_le32(addr);
560 return PCIBIOS_SUCCESSFUL;
563 static struct pci_ops u4_pcie_pci_ops =
569 #endif /* CONFIG_PPC64 */
573 * For a bandit bridge, turn on cache coherency if necessary.
574 * N.B. we could clean this up using the hose ops directly.
576 static void __init init_bandit(struct pci_controller *bp)
578 unsigned int vendev, magic;
581 /* read the word at offset 0 in config space for device 11 */
582 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
584 vendev = in_le32(bp->cfg_data);
585 if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
586 PCI_VENDOR_ID_APPLE) {
587 /* read the revision id */
588 out_le32(bp->cfg_addr,
589 (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
591 rev = in_8(bp->cfg_data);
592 if (rev != BANDIT_REVID)
594 "Unknown revision %d for bandit\n", rev);
595 } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
596 printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
600 /* read the word at offset 0x50 */
601 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
603 magic = in_le32(bp->cfg_data);
604 if ((magic & BANDIT_COHERENT) != 0)
606 magic |= BANDIT_COHERENT;
608 out_le32(bp->cfg_data, magic);
609 printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
613 * Tweak the PCI-PCI bridge chip on the blue & white G3s.
615 static void __init init_p2pbridge(void)
617 struct device_node *p2pbridge;
618 struct pci_controller* hose;
622 /* XXX it would be better here to identify the specific
623 PCI-PCI bridge chip we have. */
624 if ((p2pbridge = find_devices("pci-bridge")) == 0
625 || p2pbridge->parent == NULL
626 || strcmp(p2pbridge->parent->name, "pci") != 0)
628 if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
629 DBG("Can't find PCI infos for PCI<->PCI bridge\n");
632 /* Warning: At this point, we have not yet renumbered all busses.
633 * So we must use OF walking to find out hose
635 hose = pci_find_hose_for_OF_device(p2pbridge);
637 DBG("Can't find hose for PCI<->PCI bridge\n");
640 if (early_read_config_word(hose, bus, devfn,
641 PCI_BRIDGE_CONTROL, &val) < 0) {
642 printk(KERN_ERR "init_p2pbridge: couldn't read bridge"
646 val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
647 early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
650 static void __init init_second_ohare(void)
652 struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");
653 unsigned char bus, devfn;
659 /* This must run before we initialize the PICs since the second
660 * ohare hosts a PIC that will be accessed there.
662 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
663 struct pci_controller* hose =
664 pci_find_hose_for_OF_device(np);
666 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
669 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
670 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
671 cmd &= ~PCI_COMMAND_IO;
672 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
674 has_second_ohare = 1;
678 * Some Apple desktop machines have a NEC PD720100A USB2 controller
679 * on the motherboard. Open Firmware, on these, will disable the
680 * EHCI part of it so it behaves like a pair of OHCI's. This fixup
681 * code re-enables it ;)
683 static void __init fixup_nec_usb2(void)
685 struct device_node *nec;
687 for (nec = NULL; (nec = of_find_node_by_name(nec, "usb")) != NULL;) {
688 struct pci_controller *hose;
692 prop = (u32 *)get_property(nec, "vendor-id", NULL);
697 prop = (u32 *)get_property(nec, "device-id", NULL);
702 prop = (u32 *)get_property(nec, "reg", NULL);
705 devfn = (prop[0] >> 8) & 0xff;
706 bus = (prop[0] >> 16) & 0xff;
707 if (PCI_FUNC(devfn) != 0)
709 hose = pci_find_hose_for_OF_device(nec);
712 early_read_config_dword(hose, bus, devfn, 0xe4, &data);
714 printk("Found NEC PD720100A USB2 chip with disabled"
715 " EHCI, fixing up...\n");
717 early_write_config_dword(hose, bus, devfn, 0xe4, data);
722 static void __init setup_bandit(struct pci_controller *hose,
723 struct resource *addr)
725 hose->ops = ¯isc_pci_ops;
726 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
727 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
731 static int __init setup_uninorth(struct pci_controller *hose,
732 struct resource *addr)
734 pci_assign_all_buses = 1;
736 hose->ops = ¯isc_pci_ops;
737 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
738 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
739 /* We "know" that the bridge at f2000000 has the PCI slots. */
740 return addr->start == 0xf2000000;
742 #endif /* CONFIG_PPC32 */
745 static void __init setup_u3_agp(struct pci_controller* hose)
747 /* On G5, we move AGP up to high bus number so we don't need
748 * to reassign bus numbers for HT. If we ever have P2P bridges
749 * on AGP, we'll have to move pci_assign_all_busses to the
750 * pci_controller structure so we enable it for AGP and not for
752 * We hard code the address because of the different size of
753 * the reg address cell, we shall fix that by killing struct
754 * reg_property and using some accessor functions instead
756 hose->first_busno = 0xf0;
757 hose->last_busno = 0xff;
759 hose->ops = ¯isc_pci_ops;
760 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
761 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
765 static void __init setup_u4_pcie(struct pci_controller* hose)
767 /* We currently only implement the "non-atomic" config space, to
768 * be optimised later.
770 hose->ops = &u4_pcie_pci_ops;
771 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
772 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
774 /* The bus contains a bridge from root -> device, we need to
775 * make it visible on bus 0 so that we pick the right type
776 * of config cycles. If we didn't, we would have to force all
777 * config cycles to be type 1. So we override the "bus-range"
780 hose->first_busno = 0x00;
781 hose->last_busno = 0xff;
785 static void __init setup_u3_ht(struct pci_controller* hose)
787 struct device_node *np = (struct device_node *)hose->arch_data;
788 struct pci_controller *other = NULL;
792 hose->ops = &u3_ht_pci_ops;
794 /* We hard code the address because of the different size of
795 * the reg address cell, we shall fix that by killing struct
796 * reg_property and using some accessor functions instead
798 hose->cfg_data = ioremap(0xf2000000, 0x02000000);
801 * /ht node doesn't expose a "ranges" property, so we "remove"
802 * regions that have been allocated to AGP. So far, this version of
803 * the code doesn't assign any of the 0xfxxxxxxx "fine" memory regions
804 * to /ht. We need to fix that sooner or later by either parsing all
805 * child "ranges" properties or figuring out the U3 address space
806 * decoding logic and then read its configuration register (if any).
808 hose->io_base_phys = 0xf4000000;
809 hose->pci_io_size = 0x00400000;
810 hose->io_resource.name = np->full_name;
811 hose->io_resource.start = 0;
812 hose->io_resource.end = 0x003fffff;
813 hose->io_resource.flags = IORESOURCE_IO;
814 hose->pci_mem_offset = 0;
815 hose->first_busno = 0;
816 hose->last_busno = 0xef;
817 hose->mem_resources[0].name = np->full_name;
818 hose->mem_resources[0].start = 0x80000000;
819 hose->mem_resources[0].end = 0xefffffff;
820 hose->mem_resources[0].flags = IORESOURCE_MEM;
826 else if (u4_pcie != NULL)
830 DBG("U3/4 has no AGP/PCIE, using full resource range\n");
834 /* Fixup bus range vs. PCIE */
836 hose->last_busno = u4_pcie->first_busno - 1;
838 /* We "remove" the AGP resources from the resources allocated to HT,
839 * that is we create "holes". However, that code does assumptions
840 * that so far happen to be true (cross fingers...), typically that
841 * resources in the AGP node are properly ordered
844 for (i=0; i<3; i++) {
845 struct resource *res = &other->mem_resources[i];
846 if (res->flags != IORESOURCE_MEM)
848 /* We don't care about "fine" resources */
849 if (res->start >= 0xf0000000)
851 /* Check if it's just a matter of "shrinking" us in one
854 if (hose->mem_resources[cur].start == res->start) {
855 DBG("U3/HT: shrink start of %d, %08lx -> %08lx\n",
856 cur, hose->mem_resources[cur].start,
858 hose->mem_resources[cur].start = res->end + 1;
861 if (hose->mem_resources[cur].end == res->end) {
862 DBG("U3/HT: shrink end of %d, %08lx -> %08lx\n",
863 cur, hose->mem_resources[cur].end,
865 hose->mem_resources[cur].end = res->start - 1;
868 /* No, it's not the case, we need a hole */
870 /* not enough resources for a hole, we drop part
873 printk(KERN_WARNING "Running out of resources"
874 " for /ht host !\n");
875 hose->mem_resources[cur].end = res->start - 1;
879 DBG("U3/HT: hole, %d end at %08lx, %d start at %08lx\n",
880 cur-1, res->start - 1, cur, res->end + 1);
881 hose->mem_resources[cur].name = np->full_name;
882 hose->mem_resources[cur].flags = IORESOURCE_MEM;
883 hose->mem_resources[cur].start = res->end + 1;
884 hose->mem_resources[cur].end = hose->mem_resources[cur-1].end;
885 hose->mem_resources[cur-1].end = res->start - 1;
888 #endif /* CONFIG_PPC64 */
891 * We assume that if we have a G3 powermac, we have one bridge called
892 * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
893 * if we have one or more bandit or chaos bridges, we don't have a MPC106.
895 static int __init add_bridge(struct device_node *dev)
898 struct pci_controller *hose;
899 struct resource rsrc;
902 int primary = 1, has_address = 0;
904 DBG("Adding PCI host bridge %s\n", dev->full_name);
906 /* Fetch host bridge registers address */
907 has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
909 /* Get bus range if any */
910 bus_range = (int *) get_property(dev, "bus-range", &len);
911 if (bus_range == NULL || len < 2 * sizeof(int)) {
912 printk(KERN_WARNING "Can't get bus-range for %s, assume"
913 " bus 0\n", dev->full_name);
916 /* XXX Different prototypes, to be merged */
918 hose = pcibios_alloc_controller(dev);
920 hose = pcibios_alloc_controller();
924 hose->arch_data = dev;
925 hose->first_busno = bus_range ? bus_range[0] : 0;
926 hose->last_busno = bus_range ? bus_range[1] : 0xff;
930 /* 64 bits only bridges */
932 if (device_is_compatible(dev, "u3-agp")) {
934 disp_name = "U3-AGP";
936 } else if (device_is_compatible(dev, "u3-ht")) {
940 } else if (device_is_compatible(dev, "u4-pcie")) {
942 disp_name = "U4-PCIE";
945 printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"
946 " %d->%d\n", disp_name, hose->first_busno, hose->last_busno);
947 #endif /* CONFIG_PPC64 */
949 /* 32 bits only bridges */
951 if (device_is_compatible(dev, "uni-north")) {
952 primary = setup_uninorth(hose, &rsrc);
953 disp_name = "UniNorth";
954 } else if (strcmp(dev->name, "pci") == 0) {
955 /* XXX assume this is a mpc106 (grackle) */
957 disp_name = "Grackle (MPC106)";
958 } else if (strcmp(dev->name, "bandit") == 0) {
959 setup_bandit(hose, &rsrc);
960 disp_name = "Bandit";
961 } else if (strcmp(dev->name, "chaos") == 0) {
962 setup_chaos(hose, &rsrc);
966 printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "
967 "Firmware bus number: %d->%d\n",
968 disp_name, (unsigned long long)rsrc.start, hose->first_busno,
970 #endif /* CONFIG_PPC32 */
972 DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
973 hose, hose->cfg_addr, hose->cfg_data);
975 /* Interpret the "ranges" property */
976 /* This also maps the I/O region and sets isa_io/mem_base */
977 pci_process_bridge_OF_ranges(hose, dev, primary);
979 /* Fixup "bus-range" OF property */
980 fixup_bus_range(dev);
985 void __init pmac_pcibios_fixup(void)
987 struct pci_dev* dev = NULL;
989 for_each_pci_dev(dev) {
990 /* Read interrupt from the device-tree */
991 pci_read_irq_line(dev);
994 /* Fixup interrupt for the modem/ethernet combo controller.
995 * on machines with a second ohare chip.
996 * The number in the device tree (27) is bogus (correct for
997 * the ethernet-only board but not the combo ethernet/modem
998 * board). The real interrupt is 28 on the second controller
1001 if (has_second_ohare &&
1002 dev->vendor == PCI_VENDOR_ID_DEC &&
1003 dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {
1004 dev->irq = irq_create_mapping(NULL, 60);
1005 set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);
1007 #endif /* CONFIG_PPC32 */
1012 static void __init pmac_fixup_phb_resources(void)
1014 struct pci_controller *hose, *tmp;
1016 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1017 printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
1018 hose->global_number,
1019 hose->io_resource.start, hose->io_resource.end);
1024 void __init pmac_pci_init(void)
1026 struct device_node *np, *root;
1027 struct device_node *ht = NULL;
1029 root = of_find_node_by_path("/");
1031 printk(KERN_CRIT "pmac_pci_init: can't find root "
1032 "of device tree\n");
1035 for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
1036 if (np->name == NULL)
1038 if (strcmp(np->name, "bandit") == 0
1039 || strcmp(np->name, "chaos") == 0
1040 || strcmp(np->name, "pci") == 0) {
1041 if (add_bridge(np) == 0)
1044 if (strcmp(np->name, "ht") == 0) {
1052 /* Probe HT last as it relies on the agp resources to be already
1055 if (ht && add_bridge(ht) != 0)
1059 * We need to call pci_setup_phb_io for the HT bridge first
1060 * so it gets the I/O port numbers starting at 0, and we
1061 * need to call it for the AGP bridge after that so it gets
1062 * small positive I/O port numbers.
1065 pci_setup_phb_io(u3_ht, 1);
1067 pci_setup_phb_io(u3_agp, 0);
1069 pci_setup_phb_io(u4_pcie, 0);
1072 * On ppc64, fixup the IO resources on our host bridges as
1073 * the common code does it only for children of the host bridges
1075 pmac_fixup_phb_resources();
1077 /* Setup the linkage between OF nodes and PHBs */
1078 pci_devs_phb_init();
1080 /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
1081 * assume there is no P2P bridge on the AGP bus, which should be a
1082 * safe assumptions for now. We should do something better in the
1086 struct device_node *np = u3_agp->arch_data;
1087 PCI_DN(np)->busno = 0xf0;
1088 for (np = np->child; np; np = np->sibling)
1089 PCI_DN(np)->busno = 0xf0;
1091 /* pmac_check_ht_link(); */
1093 /* Tell pci.c to not use the common resource allocation mechanism */
1096 #else /* CONFIG_PPC64 */
1098 init_second_ohare();
1101 /* We are still having some issues with the Xserve G4, enabling
1102 * some offset between bus number and domains for now when we
1103 * assign all busses should help for now
1105 if (pci_assign_all_buses)
1106 pcibios_assign_bus_offset = 0x10;
1111 pmac_pci_enable_device_hook(struct pci_dev *dev, int initial)
1113 struct device_node* node;
1117 node = pci_device_to_OF_node(dev);
1119 /* We don't want to enable USB controllers absent from the OF tree
1120 * (iBook second controller)
1122 if (dev->vendor == PCI_VENDOR_ID_APPLE
1123 && dev->class == PCI_CLASS_SERIAL_USB_OHCI
1125 printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
1133 uninorth_child = node->parent &&
1134 device_is_compatible(node->parent, "uni-north");
1136 /* Firewire & GMAC were disabled after PCI probe, the driver is
1137 * claiming them, we must re-enable them now.
1139 if (uninorth_child && !strcmp(node->name, "firewire") &&
1140 (device_is_compatible(node, "pci106b,18") ||
1141 device_is_compatible(node, "pci106b,30") ||
1142 device_is_compatible(node, "pci11c1,5811"))) {
1143 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
1144 pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
1147 if (uninorth_child && !strcmp(node->name, "ethernet") &&
1148 device_is_compatible(node, "gmac")) {
1149 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
1157 * Make sure PCI is correctly configured
1159 * We use old pci_bios versions of the function since, by
1160 * default, gmac is not powered up, and so will be absent
1161 * from the kernel initial PCI lookup.
1163 * Should be replaced by 2.4 new PCI mechanisms and really
1164 * register the device.
1166 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1167 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1168 | PCI_COMMAND_INVALIDATE;
1169 pci_write_config_word(dev, PCI_COMMAND, cmd);
1170 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
1171 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1172 L1_CACHE_BYTES >> 2);
1178 /* We power down some devices after they have been probed. They'll
1179 * be powered back on later on
1181 void __init pmac_pcibios_after_init(void)
1183 struct device_node* nd;
1185 #ifdef CONFIG_BLK_DEV_IDE
1186 struct pci_dev *dev = NULL;
1188 /* OF fails to initialize IDE controllers on macs
1189 * (and maybe other machines)
1191 * Ideally, this should be moved to the IDE layer, but we need
1192 * to check specifically with Andre Hedrick how to do it cleanly
1193 * since the common IDE code seem to care about the fact that the
1194 * BIOS may have disabled a controller.
1198 for_each_pci_dev(dev) {
1199 if ((dev->class >> 16) == PCI_BASE_CLASS_STORAGE)
1200 pci_enable_device(dev);
1202 #endif /* CONFIG_BLK_DEV_IDE */
1204 nd = find_devices("firewire");
1206 if (nd->parent && (device_is_compatible(nd, "pci106b,18") ||
1207 device_is_compatible(nd, "pci106b,30") ||
1208 device_is_compatible(nd, "pci11c1,5811"))
1209 && device_is_compatible(nd->parent, "uni-north")) {
1210 pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
1211 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
1215 nd = find_devices("ethernet");
1217 if (nd->parent && device_is_compatible(nd, "gmac")
1218 && device_is_compatible(nd->parent, "uni-north"))
1219 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
1225 void pmac_pci_fixup_cardbus(struct pci_dev* dev)
1227 if (!machine_is(powermac))
1230 * Fix the interrupt routing on the various cardbus bridges
1231 * used on powerbooks
1233 if (dev->vendor != PCI_VENDOR_ID_TI)
1235 if (dev->device == PCI_DEVICE_ID_TI_1130 ||
1236 dev->device == PCI_DEVICE_ID_TI_1131) {
1238 /* Enable PCI interrupt */
1239 if (pci_read_config_byte(dev, 0x91, &val) == 0)
1240 pci_write_config_byte(dev, 0x91, val | 0x30);
1241 /* Disable ISA interrupt mode */
1242 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1243 pci_write_config_byte(dev, 0x92, val & ~0x06);
1245 if (dev->device == PCI_DEVICE_ID_TI_1210 ||
1246 dev->device == PCI_DEVICE_ID_TI_1211 ||
1247 dev->device == PCI_DEVICE_ID_TI_1410 ||
1248 dev->device == PCI_DEVICE_ID_TI_1510) {
1250 /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
1251 signal out the MFUNC0 pin */
1252 if (pci_read_config_byte(dev, 0x8c, &val) == 0)
1253 pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
1254 /* Disable ISA interrupt mode */
1255 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1256 pci_write_config_byte(dev, 0x92, val & ~0x06);
1260 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
1262 void pmac_pci_fixup_pciata(struct pci_dev* dev)
1267 * On PowerMacs, we try to switch any PCI ATA controller to
1270 if (!machine_is(powermac))
1273 /* Some controllers don't have the class IDE */
1274 if (dev->vendor == PCI_VENDOR_ID_PROMISE)
1275 switch(dev->device) {
1276 case PCI_DEVICE_ID_PROMISE_20246:
1277 case PCI_DEVICE_ID_PROMISE_20262:
1278 case PCI_DEVICE_ID_PROMISE_20263:
1279 case PCI_DEVICE_ID_PROMISE_20265:
1280 case PCI_DEVICE_ID_PROMISE_20267:
1281 case PCI_DEVICE_ID_PROMISE_20268:
1282 case PCI_DEVICE_ID_PROMISE_20269:
1283 case PCI_DEVICE_ID_PROMISE_20270:
1284 case PCI_DEVICE_ID_PROMISE_20271:
1285 case PCI_DEVICE_ID_PROMISE_20275:
1286 case PCI_DEVICE_ID_PROMISE_20276:
1287 case PCI_DEVICE_ID_PROMISE_20277:
1290 /* Others, check PCI class */
1291 if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1294 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1295 if ((progif & 5) != 5) {
1296 printk(KERN_INFO "Forcing PCI IDE into native mode: %s\n",
1298 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
1299 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
1301 printk(KERN_ERR "Rewrite of PROGIF failed !\n");
1304 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
1308 * Disable second function on K2-SATA, it's broken
1309 * and disable IO BARs on first one
1311 static void fixup_k2_sata(struct pci_dev* dev)
1316 if (PCI_FUNC(dev->devfn) > 0) {
1317 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1318 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1319 pci_write_config_word(dev, PCI_COMMAND, cmd);
1320 for (i = 0; i < 6; i++) {
1321 dev->resource[i].start = dev->resource[i].end = 0;
1322 dev->resource[i].flags = 0;
1323 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1327 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1328 cmd &= ~PCI_COMMAND_IO;
1329 pci_write_config_word(dev, PCI_COMMAND, cmd);
1330 for (i = 0; i < 5; i++) {
1331 dev->resource[i].start = dev->resource[i].end = 0;
1332 dev->resource[i].flags = 0;
1333 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1338 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);