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 /* XXX Could be per-controller, but I don't think we risk anything by
39 * assuming we won't have both UniNorth and Bandit */
40 static int has_uninorth;
42 static struct pci_controller *u3_agp;
44 static int has_second_ohare;
45 #endif /* CONFIG_PPC64 */
47 extern int pcibios_assign_bus_offset;
49 struct device_node *k2_skiplist[2];
52 * Magic constants for enabling cache coherency in the bandit/PSX bridge.
54 #define BANDIT_DEVID_2 8
55 #define BANDIT_REVID 3
57 #define BANDIT_DEVNUM 11
58 #define BANDIT_MAGIC 0x50
59 #define BANDIT_COHERENT 0x40
61 static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
63 for (; node != 0;node = node->sibling) {
64 const int * bus_range;
65 const unsigned int *class_code;
68 /* For PCI<->PCI bridges or CardBus bridges, we go down */
69 class_code = of_get_property(node, "class-code", NULL);
70 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
71 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
73 bus_range = of_get_property(node, "bus-range", &len);
74 if (bus_range != NULL && len > 2 * sizeof(int)) {
75 if (bus_range[1] > higher)
76 higher = bus_range[1];
78 higher = fixup_one_level_bus_range(node->child, higher);
83 /* This routine fixes the "bus-range" property of all bridges in the
84 * system since they tend to have their "last" member wrong on macs
86 * Note that the bus numbers manipulated here are OF bus numbers, they
87 * are not Linux bus numbers.
89 static void __init fixup_bus_range(struct device_node *bridge)
92 struct property *prop;
94 /* Lookup the "bus-range" property for the hose */
95 prop = of_find_property(bridge, "bus-range", &len);
96 if (prop == NULL || prop->length < 2 * sizeof(int))
99 bus_range = prop->value;
100 bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
104 * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
106 * The "Bandit" version is present in all early PCI PowerMacs,
107 * and up to the first ones using Grackle. Some machines may
108 * have 2 bandit controllers (2 PCI busses).
110 * "Chaos" is used in some "Bandit"-type machines as a bridge
111 * for the separate display bus. It is accessed the same
112 * way as bandit, but cannot be probed for devices. It therefore
113 * has its own config access functions.
115 * The "UniNorth" version is present in all Core99 machines
116 * (iBook, G4, new IMacs, and all the recent Apple machines).
117 * It contains 3 controllers in one ASIC.
119 * The U3 is the bridge used on G5 machines. It contains an
120 * AGP bus which is dealt with the old UniNorth access routines
121 * and a HyperTransport bus which uses its own set of access
125 #define MACRISC_CFA0(devfn, off) \
126 ((1 << (unsigned int)PCI_SLOT(dev_fn)) \
127 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
128 | (((unsigned int)(off)) & 0xFCUL))
130 #define MACRISC_CFA1(bus, devfn, off) \
131 ((((unsigned int)(bus)) << 16) \
132 |(((unsigned int)(devfn)) << 8) \
133 |(((unsigned int)(off)) & 0xFCUL) \
136 static volatile void __iomem *macrisc_cfg_access(struct pci_controller* hose,
137 u8 bus, u8 dev_fn, u8 offset)
141 if (bus == hose->first_busno) {
142 if (dev_fn < (11 << 3))
144 caddr = MACRISC_CFA0(dev_fn, offset);
146 caddr = MACRISC_CFA1(bus, dev_fn, offset);
148 /* Uninorth will return garbage if we don't read back the value ! */
150 out_le32(hose->cfg_addr, caddr);
151 } while (in_le32(hose->cfg_addr) != caddr);
153 offset &= has_uninorth ? 0x07 : 0x03;
154 return hose->cfg_data + offset;
157 static int macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
158 int offset, int len, u32 *val)
160 struct pci_controller *hose;
161 volatile void __iomem *addr;
163 hose = pci_bus_to_host(bus);
165 return PCIBIOS_DEVICE_NOT_FOUND;
167 return PCIBIOS_BAD_REGISTER_NUMBER;
168 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
170 return PCIBIOS_DEVICE_NOT_FOUND;
172 * Note: the caller has already checked that offset is
173 * suitably aligned and that len is 1, 2 or 4.
180 *val = in_le16(addr);
183 *val = in_le32(addr);
186 return PCIBIOS_SUCCESSFUL;
189 static int macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
190 int offset, int len, u32 val)
192 struct pci_controller *hose;
193 volatile void __iomem *addr;
195 hose = pci_bus_to_host(bus);
197 return PCIBIOS_DEVICE_NOT_FOUND;
199 return PCIBIOS_BAD_REGISTER_NUMBER;
200 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
202 return PCIBIOS_DEVICE_NOT_FOUND;
204 * Note: the caller has already checked that offset is
205 * suitably aligned and that len is 1, 2 or 4.
218 return PCIBIOS_SUCCESSFUL;
221 static struct pci_ops macrisc_pci_ops =
223 .read = macrisc_read_config,
224 .write = macrisc_write_config,
229 * Verify that a specific (bus, dev_fn) exists on chaos
231 static int chaos_validate_dev(struct pci_bus *bus, int devfn, int offset)
233 struct device_node *np;
234 const u32 *vendor, *device;
237 return PCIBIOS_BAD_REGISTER_NUMBER;
238 np = pci_busdev_to_OF_node(bus, devfn);
240 return PCIBIOS_DEVICE_NOT_FOUND;
242 vendor = of_get_property(np, "vendor-id", NULL);
243 device = of_get_property(np, "device-id", NULL);
244 if (vendor == NULL || device == NULL)
245 return PCIBIOS_DEVICE_NOT_FOUND;
247 if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
248 && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
249 return PCIBIOS_BAD_REGISTER_NUMBER;
251 return PCIBIOS_SUCCESSFUL;
255 chaos_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
258 int result = chaos_validate_dev(bus, devfn, offset);
259 if (result == PCIBIOS_BAD_REGISTER_NUMBER)
261 if (result != PCIBIOS_SUCCESSFUL)
263 return macrisc_read_config(bus, devfn, offset, len, val);
267 chaos_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
270 int result = chaos_validate_dev(bus, devfn, offset);
271 if (result != PCIBIOS_SUCCESSFUL)
273 return macrisc_write_config(bus, devfn, offset, len, val);
276 static struct pci_ops chaos_pci_ops =
278 .read = chaos_read_config,
279 .write = chaos_write_config,
282 static void __init setup_chaos(struct pci_controller *hose,
283 struct resource *addr)
285 /* assume a `chaos' bridge */
286 hose->ops = &chaos_pci_ops;
287 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
288 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
290 #endif /* CONFIG_PPC32 */
294 * These versions of U3 HyperTransport config space access ops do not
295 * implement self-view of the HT host yet
299 * This function deals with some "special cases" devices.
301 * 0 -> No special case
302 * 1 -> Skip the device but act as if the access was successfull
303 * (return 0xff's on reads, eventually, cache config space
304 * accesses in a later version)
305 * -1 -> Hide the device (unsuccessful acess)
307 static int u3_ht_skip_device(struct pci_controller *hose,
308 struct pci_bus *bus, unsigned int devfn)
310 struct device_node *busdn, *dn;
313 /* We only allow config cycles to devices that are in OF device-tree
314 * as we are apparently having some weird things going on with some
315 * revs of K2 on recent G5s, except for the host bridge itself, which
316 * is missing from the tree but we know we can probe.
319 busdn = pci_device_to_OF_node(bus->self);
324 for (dn = busdn->child; dn; dn = dn->sibling)
325 if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
331 * When a device in K2 is powered down, we die on config
332 * cycle accesses. Fix that here.
335 if (k2_skiplist[i] == dn)
341 #define U3_HT_CFA0(devfn, off) \
342 ((((unsigned int)devfn) << 8) | offset)
343 #define U3_HT_CFA1(bus, devfn, off) \
344 (U3_HT_CFA0(devfn, off) \
345 + (((unsigned int)bus) << 16) \
348 static void __iomem *u3_ht_cfg_access(struct pci_controller *hose, u8 bus,
349 u8 devfn, u8 offset, int *swap)
352 if (bus == hose->first_busno) {
354 return hose->cfg_data + U3_HT_CFA0(devfn, offset);
356 return ((void __iomem *)hose->cfg_addr) + (offset << 2);
358 return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);
361 static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
362 int offset, int len, u32 *val)
364 struct pci_controller *hose;
368 hose = pci_bus_to_host(bus);
370 return PCIBIOS_DEVICE_NOT_FOUND;
372 return PCIBIOS_BAD_REGISTER_NUMBER;
373 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
375 return PCIBIOS_DEVICE_NOT_FOUND;
377 switch (u3_ht_skip_device(hose, bus, devfn)) {
385 *val = 0xffff; break;
387 *val = 0xfffffffful; break;
389 return PCIBIOS_SUCCESSFUL;
391 return PCIBIOS_DEVICE_NOT_FOUND;
395 * Note: the caller has already checked that offset is
396 * suitably aligned and that len is 1, 2 or 4.
403 *val = swap ? in_le16(addr) : in_be16(addr);
406 *val = swap ? in_le32(addr) : in_be32(addr);
409 return PCIBIOS_SUCCESSFUL;
412 static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
413 int offset, int len, u32 val)
415 struct pci_controller *hose;
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, &swap);
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.
446 swap ? out_le16(addr, val) : out_be16(addr, val);
449 swap ? out_le32(addr, val) : out_be32(addr, val);
452 return PCIBIOS_SUCCESSFUL;
455 static struct pci_ops u3_ht_pci_ops =
457 .read = u3_ht_read_config,
458 .write = u3_ht_write_config,
461 #define U4_PCIE_CFA0(devfn, off) \
462 ((1 << ((unsigned int)PCI_SLOT(dev_fn))) \
463 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
464 | ((((unsigned int)(off)) >> 8) << 28) \
465 | (((unsigned int)(off)) & 0xfcU))
467 #define U4_PCIE_CFA1(bus, devfn, off) \
468 ((((unsigned int)(bus)) << 16) \
469 |(((unsigned int)(devfn)) << 8) \
470 | ((((unsigned int)(off)) >> 8) << 28) \
471 |(((unsigned int)(off)) & 0xfcU) \
474 static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose,
475 u8 bus, u8 dev_fn, int offset)
479 if (bus == hose->first_busno) {
480 caddr = U4_PCIE_CFA0(dev_fn, offset);
482 caddr = U4_PCIE_CFA1(bus, dev_fn, offset);
484 /* Uninorth will return garbage if we don't read back the value ! */
486 out_le32(hose->cfg_addr, caddr);
487 } while (in_le32(hose->cfg_addr) != caddr);
490 return hose->cfg_data + offset;
493 static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
494 int offset, int len, u32 *val)
496 struct pci_controller *hose;
497 volatile void __iomem *addr;
499 hose = pci_bus_to_host(bus);
501 return PCIBIOS_DEVICE_NOT_FOUND;
502 if (offset >= 0x1000)
503 return PCIBIOS_BAD_REGISTER_NUMBER;
504 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
506 return PCIBIOS_DEVICE_NOT_FOUND;
508 * Note: the caller has already checked that offset is
509 * suitably aligned and that len is 1, 2 or 4.
516 *val = in_le16(addr);
519 *val = in_le32(addr);
522 return PCIBIOS_SUCCESSFUL;
525 static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
526 int offset, int len, u32 val)
528 struct pci_controller *hose;
529 volatile void __iomem *addr;
531 hose = pci_bus_to_host(bus);
533 return PCIBIOS_DEVICE_NOT_FOUND;
534 if (offset >= 0x1000)
535 return PCIBIOS_BAD_REGISTER_NUMBER;
536 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
538 return PCIBIOS_DEVICE_NOT_FOUND;
540 * Note: the caller has already checked that offset is
541 * suitably aligned and that len is 1, 2 or 4.
554 return PCIBIOS_SUCCESSFUL;
557 static struct pci_ops u4_pcie_pci_ops =
559 .read = u4_pcie_read_config,
560 .write = u4_pcie_write_config,
563 #endif /* CONFIG_PPC64 */
567 * For a bandit bridge, turn on cache coherency if necessary.
568 * N.B. we could clean this up using the hose ops directly.
570 static void __init init_bandit(struct pci_controller *bp)
572 unsigned int vendev, magic;
575 /* read the word at offset 0 in config space for device 11 */
576 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
578 vendev = in_le32(bp->cfg_data);
579 if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
580 PCI_VENDOR_ID_APPLE) {
581 /* read the revision id */
582 out_le32(bp->cfg_addr,
583 (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
585 rev = in_8(bp->cfg_data);
586 if (rev != BANDIT_REVID)
588 "Unknown revision %d for bandit\n", rev);
589 } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
590 printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
594 /* read the word at offset 0x50 */
595 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
597 magic = in_le32(bp->cfg_data);
598 if ((magic & BANDIT_COHERENT) != 0)
600 magic |= BANDIT_COHERENT;
602 out_le32(bp->cfg_data, magic);
603 printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
607 * Tweak the PCI-PCI bridge chip on the blue & white G3s.
609 static void __init init_p2pbridge(void)
611 struct device_node *p2pbridge;
612 struct pci_controller* hose;
616 /* XXX it would be better here to identify the specific
617 PCI-PCI bridge chip we have. */
618 p2pbridge = of_find_node_by_name(NULL, "pci-bridge");
619 if (p2pbridge == NULL
620 || p2pbridge->parent == NULL
621 || strcmp(p2pbridge->parent->name, "pci") != 0)
623 if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
624 DBG("Can't find PCI infos for PCI<->PCI bridge\n");
627 /* Warning: At this point, we have not yet renumbered all busses.
628 * So we must use OF walking to find out hose
630 hose = pci_find_hose_for_OF_device(p2pbridge);
632 DBG("Can't find hose for PCI<->PCI bridge\n");
635 if (early_read_config_word(hose, bus, devfn,
636 PCI_BRIDGE_CONTROL, &val) < 0) {
637 printk(KERN_ERR "init_p2pbridge: couldn't read bridge"
641 val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
642 early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
644 of_node_put(p2pbridge);
647 static void __init init_second_ohare(void)
649 struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");
650 unsigned char bus, devfn;
656 /* This must run before we initialize the PICs since the second
657 * ohare hosts a PIC that will be accessed there.
659 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
660 struct pci_controller* hose =
661 pci_find_hose_for_OF_device(np);
663 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
666 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
667 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
668 cmd &= ~PCI_COMMAND_IO;
669 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
671 has_second_ohare = 1;
675 * Some Apple desktop machines have a NEC PD720100A USB2 controller
676 * on the motherboard. Open Firmware, on these, will disable the
677 * EHCI part of it so it behaves like a pair of OHCI's. This fixup
678 * code re-enables it ;)
680 static void __init fixup_nec_usb2(void)
682 struct device_node *nec;
684 for (nec = NULL; (nec = of_find_node_by_name(nec, "usb")) != NULL;) {
685 struct pci_controller *hose;
690 prop = of_get_property(nec, "vendor-id", NULL);
695 prop = of_get_property(nec, "device-id", NULL);
700 prop = of_get_property(nec, "reg", NULL);
703 devfn = (prop[0] >> 8) & 0xff;
704 bus = (prop[0] >> 16) & 0xff;
705 if (PCI_FUNC(devfn) != 0)
707 hose = pci_find_hose_for_OF_device(nec);
710 early_read_config_dword(hose, bus, devfn, 0xe4, &data);
712 printk("Found NEC PD720100A USB2 chip with disabled"
713 " EHCI, fixing up...\n");
715 early_write_config_dword(hose, bus, devfn, 0xe4, data);
720 static void __init setup_bandit(struct pci_controller *hose,
721 struct resource *addr)
723 hose->ops = ¯isc_pci_ops;
724 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
725 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
729 static int __init setup_uninorth(struct pci_controller *hose,
730 struct resource *addr)
732 ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
734 hose->ops = ¯isc_pci_ops;
735 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
736 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
737 /* We "know" that the bridge at f2000000 has the PCI slots. */
738 return addr->start == 0xf2000000;
740 #endif /* CONFIG_PPC32 */
743 static void __init setup_u3_agp(struct pci_controller* hose)
745 /* On G5, we move AGP up to high bus number so we don't need
746 * to reassign bus numbers for HT. If we ever have P2P bridges
747 * on AGP, we'll have to move pci_assign_all_busses to the
748 * pci_controller structure so we enable it for AGP and not for
750 * We hard code the address because of the different size of
751 * the reg address cell, we shall fix that by killing struct
752 * reg_property and using some accessor functions instead
754 hose->first_busno = 0xf0;
755 hose->last_busno = 0xff;
757 hose->ops = ¯isc_pci_ops;
758 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
759 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
763 static void __init setup_u4_pcie(struct pci_controller* hose)
765 /* We currently only implement the "non-atomic" config space, to
766 * be optimised later.
768 hose->ops = &u4_pcie_pci_ops;
769 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
770 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
772 /* The bus contains a bridge from root -> device, we need to
773 * make it visible on bus 0 so that we pick the right type
774 * of config cycles. If we didn't, we would have to force all
775 * config cycles to be type 1. So we override the "bus-range"
778 hose->first_busno = 0x00;
779 hose->last_busno = 0xff;
782 static void __init parse_region_decode(struct pci_controller *hose,
785 unsigned long base, end, next = -1;
788 /* Iterate through all bits. We ignore the last bit as this region is
789 * reserved for the ROM among other niceties
791 for (i = 0; i < 31; i++) {
792 if ((decode & (0x80000000 >> i)) == 0)
795 base = 0xf0000000 | (((u32)i) << 24);
796 end = base + 0x00ffffff;
798 base = ((u32)i-16) << 28;
799 end = base + 0x0fffffff;
803 printk(KERN_WARNING "PCI: Too many ranges !\n");
806 hose->mem_resources[cur].flags = IORESOURCE_MEM;
807 hose->mem_resources[cur].name = hose->dn->full_name;
808 hose->mem_resources[cur].start = base;
809 hose->mem_resources[cur].end = end;
810 DBG(" %d: 0x%08lx-0x%08lx\n", cur, base, end);
812 DBG(" : -0x%08lx\n", end);
813 hose->mem_resources[cur].end = end;
819 static void __init setup_u3_ht(struct pci_controller* hose)
821 struct device_node *np = hose->dn;
822 struct resource cfg_res, self_res;
825 hose->ops = &u3_ht_pci_ops;
827 /* Get base addresses from OF tree
829 if (of_address_to_resource(np, 0, &cfg_res) ||
830 of_address_to_resource(np, 1, &self_res)) {
831 printk(KERN_ERR "PCI: Failed to get U3/U4 HT resources !\n");
835 /* Map external cfg space access into cfg_data and self registers
838 hose->cfg_data = ioremap(cfg_res.start, 0x02000000);
839 hose->cfg_addr = ioremap(self_res.start,
840 self_res.end - self_res.start + 1);
843 * /ht node doesn't expose a "ranges" property, we read the register
844 * that controls the decoding logic and use that for memory regions.
845 * The IO region is hard coded since it is fixed in HW as well.
847 hose->io_base_phys = 0xf4000000;
848 hose->pci_io_size = 0x00400000;
849 hose->io_resource.name = np->full_name;
850 hose->io_resource.start = 0;
851 hose->io_resource.end = 0x003fffff;
852 hose->io_resource.flags = IORESOURCE_IO;
853 hose->pci_mem_offset = 0;
854 hose->first_busno = 0;
855 hose->last_busno = 0xef;
857 /* Note: fix offset when cfg_addr becomes a void * */
858 decode = in_be32(hose->cfg_addr + 0x80);
860 DBG("PCI: Apple HT bridge decode register: 0x%08x\n", decode);
862 /* NOTE: The decode register setup is a bit weird... region
863 * 0xf8000000 for example is marked as enabled in there while it's
864 & actually the memory controller registers.
865 * That means that we are incorrectly attributing it to HT.
867 * In a similar vein, region 0xf4000000 is actually the HT IO space but
868 * also marked as enabled in here and 0xf9000000 is used by some other
869 * internal bits of the northbridge.
871 * Unfortunately, we can't just mask out those bit as we would end
872 * up with more regions than we can cope (linux can only cope with
873 * 3 memory regions for a PHB at this stage).
875 * So for now, we just do a little hack. We happen to -know- that
876 * Apple firmware doesn't assign things below 0xfa000000 for that
877 * bridge anyway so we mask out all bits we don't want.
879 decode &= 0x003fffff;
881 /* Now parse the resulting bits and build resources */
882 parse_region_decode(hose, decode);
884 #endif /* CONFIG_PPC64 */
887 * We assume that if we have a G3 powermac, we have one bridge called
888 * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
889 * if we have one or more bandit or chaos bridges, we don't have a MPC106.
891 static int __init pmac_add_bridge(struct device_node *dev)
894 struct pci_controller *hose;
895 struct resource rsrc;
897 const int *bus_range;
898 int primary = 1, has_address = 0;
900 DBG("Adding PCI host bridge %s\n", dev->full_name);
902 /* Fetch host bridge registers address */
903 has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
905 /* Get bus range if any */
906 bus_range = of_get_property(dev, "bus-range", &len);
907 if (bus_range == NULL || len < 2 * sizeof(int)) {
908 printk(KERN_WARNING "Can't get bus-range for %s, assume"
909 " bus 0\n", dev->full_name);
912 hose = pcibios_alloc_controller(dev);
915 hose->first_busno = bus_range ? bus_range[0] : 0;
916 hose->last_busno = bus_range ? bus_range[1] : 0xff;
920 /* 64 bits only bridges */
922 if (of_device_is_compatible(dev, "u3-agp")) {
924 disp_name = "U3-AGP";
926 } else if (of_device_is_compatible(dev, "u3-ht")) {
930 } else if (of_device_is_compatible(dev, "u4-pcie")) {
932 disp_name = "U4-PCIE";
935 printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"
936 " %d->%d\n", disp_name, hose->first_busno, hose->last_busno);
937 #endif /* CONFIG_PPC64 */
939 /* 32 bits only bridges */
941 if (of_device_is_compatible(dev, "uni-north")) {
942 primary = setup_uninorth(hose, &rsrc);
943 disp_name = "UniNorth";
944 } else if (strcmp(dev->name, "pci") == 0) {
945 /* XXX assume this is a mpc106 (grackle) */
947 disp_name = "Grackle (MPC106)";
948 } else if (strcmp(dev->name, "bandit") == 0) {
949 setup_bandit(hose, &rsrc);
950 disp_name = "Bandit";
951 } else if (strcmp(dev->name, "chaos") == 0) {
952 setup_chaos(hose, &rsrc);
956 printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "
957 "Firmware bus number: %d->%d\n",
958 disp_name, (unsigned long long)rsrc.start, hose->first_busno,
960 #endif /* CONFIG_PPC32 */
962 DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
963 hose, hose->cfg_addr, hose->cfg_data);
965 /* Interpret the "ranges" property */
966 /* This also maps the I/O region and sets isa_io/mem_base */
967 pci_process_bridge_OF_ranges(hose, dev, primary);
969 /* Fixup "bus-range" OF property */
970 fixup_bus_range(dev);
975 void __devinit pmac_pci_irq_fixup(struct pci_dev *dev)
978 /* Fixup interrupt for the modem/ethernet combo controller.
979 * on machines with a second ohare chip.
980 * The number in the device tree (27) is bogus (correct for
981 * the ethernet-only board but not the combo ethernet/modem
982 * board). The real interrupt is 28 on the second controller
985 if (has_second_ohare &&
986 dev->vendor == PCI_VENDOR_ID_DEC &&
987 dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {
988 dev->irq = irq_create_mapping(NULL, 60);
989 set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);
991 #endif /* CONFIG_PPC32 */
994 void __init pmac_pci_init(void)
996 struct device_node *np, *root;
997 struct device_node *ht = NULL;
999 ppc_pci_set_flags(PPC_PCI_CAN_SKIP_ISA_ALIGN);
1001 root = of_find_node_by_path("/");
1003 printk(KERN_CRIT "pmac_pci_init: can't find root "
1004 "of device tree\n");
1007 for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
1008 if (np->name == NULL)
1010 if (strcmp(np->name, "bandit") == 0
1011 || strcmp(np->name, "chaos") == 0
1012 || strcmp(np->name, "pci") == 0) {
1013 if (pmac_add_bridge(np) == 0)
1016 if (strcmp(np->name, "ht") == 0) {
1024 /* Probe HT last as it relies on the agp resources to be already
1027 if (ht && pmac_add_bridge(ht) != 0)
1030 /* Setup the linkage between OF nodes and PHBs */
1031 pci_devs_phb_init();
1033 /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
1034 * assume there is no P2P bridge on the AGP bus, which should be a
1035 * safe assumptions for now. We should do something better in the
1039 struct device_node *np = u3_agp->dn;
1040 PCI_DN(np)->busno = 0xf0;
1041 for (np = np->child; np; np = np->sibling)
1042 PCI_DN(np)->busno = 0xf0;
1044 /* pmac_check_ht_link(); */
1046 /* We can allocate missing resources if any */
1049 #else /* CONFIG_PPC64 */
1051 init_second_ohare();
1054 /* We are still having some issues with the Xserve G4, enabling
1055 * some offset between bus number and domains for now when we
1056 * assign all busses should help for now
1058 if (ppc_pci_has_flag(PPC_PCI_REASSIGN_ALL_BUS))
1059 pcibios_assign_bus_offset = 0x10;
1064 int pmac_pci_enable_device_hook(struct pci_dev *dev)
1066 struct device_node* node;
1070 node = pci_device_to_OF_node(dev);
1072 /* We don't want to enable USB controllers absent from the OF tree
1073 * (iBook second controller)
1075 if (dev->vendor == PCI_VENDOR_ID_APPLE
1076 && dev->class == PCI_CLASS_SERIAL_USB_OHCI
1078 printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
1086 uninorth_child = node->parent &&
1087 of_device_is_compatible(node->parent, "uni-north");
1089 /* Firewire & GMAC were disabled after PCI probe, the driver is
1090 * claiming them, we must re-enable them now.
1092 if (uninorth_child && !strcmp(node->name, "firewire") &&
1093 (of_device_is_compatible(node, "pci106b,18") ||
1094 of_device_is_compatible(node, "pci106b,30") ||
1095 of_device_is_compatible(node, "pci11c1,5811"))) {
1096 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
1097 pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
1100 if (uninorth_child && !strcmp(node->name, "ethernet") &&
1101 of_device_is_compatible(node, "gmac")) {
1102 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
1107 * Fixup various header fields on 32 bits. We don't do that on
1108 * 64 bits as some of these have strange values behind the HT
1109 * bridge and we must not, for example, enable MWI or set the
1110 * cache line size on them.
1115 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1116 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1117 | PCI_COMMAND_INVALIDATE;
1118 pci_write_config_word(dev, PCI_COMMAND, cmd);
1119 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
1121 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1122 L1_CACHE_BYTES >> 2);
1128 void __devinit pmac_pci_fixup_ohci(struct pci_dev *dev)
1130 struct device_node *node = pci_device_to_OF_node(dev);
1132 /* We don't want to assign resources to USB controllers
1133 * absent from the OF tree (iBook second controller)
1135 if (dev->class == PCI_CLASS_SERIAL_USB_OHCI && !node)
1136 dev->resource[0].flags = 0;
1138 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_ANY_ID, pmac_pci_fixup_ohci);
1140 /* We power down some devices after they have been probed. They'll
1141 * be powered back on later on
1143 void __init pmac_pcibios_after_init(void)
1145 struct device_node* nd;
1147 for_each_node_by_name(nd, "firewire") {
1148 if (nd->parent && (of_device_is_compatible(nd, "pci106b,18") ||
1149 of_device_is_compatible(nd, "pci106b,30") ||
1150 of_device_is_compatible(nd, "pci11c1,5811"))
1151 && of_device_is_compatible(nd->parent, "uni-north")) {
1152 pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
1153 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
1157 for_each_node_by_name(nd, "ethernet") {
1158 if (nd->parent && of_device_is_compatible(nd, "gmac")
1159 && of_device_is_compatible(nd->parent, "uni-north"))
1160 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
1165 void pmac_pci_fixup_cardbus(struct pci_dev* dev)
1167 if (!machine_is(powermac))
1170 * Fix the interrupt routing on the various cardbus bridges
1171 * used on powerbooks
1173 if (dev->vendor != PCI_VENDOR_ID_TI)
1175 if (dev->device == PCI_DEVICE_ID_TI_1130 ||
1176 dev->device == PCI_DEVICE_ID_TI_1131) {
1178 /* Enable PCI interrupt */
1179 if (pci_read_config_byte(dev, 0x91, &val) == 0)
1180 pci_write_config_byte(dev, 0x91, val | 0x30);
1181 /* Disable ISA interrupt mode */
1182 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1183 pci_write_config_byte(dev, 0x92, val & ~0x06);
1185 if (dev->device == PCI_DEVICE_ID_TI_1210 ||
1186 dev->device == PCI_DEVICE_ID_TI_1211 ||
1187 dev->device == PCI_DEVICE_ID_TI_1410 ||
1188 dev->device == PCI_DEVICE_ID_TI_1510) {
1190 /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
1191 signal out the MFUNC0 pin */
1192 if (pci_read_config_byte(dev, 0x8c, &val) == 0)
1193 pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
1194 /* Disable ISA interrupt mode */
1195 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1196 pci_write_config_byte(dev, 0x92, val & ~0x06);
1200 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
1202 void pmac_pci_fixup_pciata(struct pci_dev* dev)
1207 * On PowerMacs, we try to switch any PCI ATA controller to
1210 if (!machine_is(powermac))
1213 /* Some controllers don't have the class IDE */
1214 if (dev->vendor == PCI_VENDOR_ID_PROMISE)
1215 switch(dev->device) {
1216 case PCI_DEVICE_ID_PROMISE_20246:
1217 case PCI_DEVICE_ID_PROMISE_20262:
1218 case PCI_DEVICE_ID_PROMISE_20263:
1219 case PCI_DEVICE_ID_PROMISE_20265:
1220 case PCI_DEVICE_ID_PROMISE_20267:
1221 case PCI_DEVICE_ID_PROMISE_20268:
1222 case PCI_DEVICE_ID_PROMISE_20269:
1223 case PCI_DEVICE_ID_PROMISE_20270:
1224 case PCI_DEVICE_ID_PROMISE_20271:
1225 case PCI_DEVICE_ID_PROMISE_20275:
1226 case PCI_DEVICE_ID_PROMISE_20276:
1227 case PCI_DEVICE_ID_PROMISE_20277:
1230 /* Others, check PCI class */
1231 if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1234 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1235 if ((progif & 5) != 5) {
1236 printk(KERN_INFO "PCI: %s Forcing PCI IDE into native mode\n",
1238 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
1239 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
1241 printk(KERN_ERR "Rewrite of PROGIF failed !\n");
1243 /* Clear IO BARs, they will be reassigned */
1244 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);
1245 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);
1246 pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, 0);
1247 pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, 0);
1251 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
1252 #endif /* CONFIG_PPC32 */
1255 * Disable second function on K2-SATA, it's broken
1256 * and disable IO BARs on first one
1258 static void fixup_k2_sata(struct pci_dev* dev)
1263 if (PCI_FUNC(dev->devfn) > 0) {
1264 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1265 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1266 pci_write_config_word(dev, PCI_COMMAND, cmd);
1267 for (i = 0; i < 6; i++) {
1268 dev->resource[i].start = dev->resource[i].end = 0;
1269 dev->resource[i].flags = 0;
1270 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1274 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1275 cmd &= ~PCI_COMMAND_IO;
1276 pci_write_config_word(dev, PCI_COMMAND, cmd);
1277 for (i = 0; i < 5; i++) {
1278 dev->resource[i].start = dev->resource[i].end = 0;
1279 dev->resource[i].flags = 0;
1280 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1285 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);