2 * Common pmac/prep/chrp pci routines. -- Cort
5 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/string.h>
9 #include <linux/init.h>
10 #include <linux/capability.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/bootmem.h>
14 #include <linux/irq.h>
15 #include <linux/list.h>
18 #include <asm/processor.h>
21 #include <asm/sections.h>
22 #include <asm/pci-bridge.h>
23 #include <asm/byteorder.h>
24 #include <asm/uaccess.h>
25 #include <asm/machdep.h>
29 unsigned long isa_io_base = 0;
30 unsigned long pci_dram_offset = 0;
31 int pcibios_assign_bus_offset = 1;
33 void pcibios_make_OF_bus_map(void);
35 static void fixup_broken_pcnet32(struct pci_dev* dev);
36 static void fixup_cpc710_pci64(struct pci_dev* dev);
38 static u8* pci_to_OF_bus_map;
41 /* By default, we don't re-assign bus numbers. We do this only on
44 static int pci_assign_all_buses;
48 static int pci_bus_count;
50 /* This will remain NULL for now, until isa-bridge.c is made common
51 * to both 32-bit and 64-bit.
53 struct pci_dev *isa_bridge_pcidev;
54 EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
57 fixup_hide_host_resource_fsl(struct pci_dev *dev)
59 int i, class = dev->class >> 8;
61 if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
62 class == PCI_CLASS_BRIDGE_OTHER) &&
63 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
64 (dev->bus->parent == NULL)) {
65 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
66 dev->resource[i].start = 0;
67 dev->resource[i].end = 0;
68 dev->resource[i].flags = 0;
72 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
73 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
76 fixup_broken_pcnet32(struct pci_dev* dev)
78 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
79 dev->vendor = PCI_VENDOR_ID_AMD;
80 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
83 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
86 fixup_cpc710_pci64(struct pci_dev* dev)
88 /* Hide the PCI64 BARs from the kernel as their content doesn't
89 * fit well in the resource management
91 dev->resource[0].start = dev->resource[0].end = 0;
92 dev->resource[0].flags = 0;
93 dev->resource[1].start = dev->resource[1].end = 0;
94 dev->resource[1].flags = 0;
96 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
100 * Functions below are used on OpenFirmware machines.
103 make_one_node_map(struct device_node* node, u8 pci_bus)
105 const int *bus_range;
108 if (pci_bus >= pci_bus_count)
110 bus_range = of_get_property(node, "bus-range", &len);
111 if (bus_range == NULL || len < 2 * sizeof(int)) {
112 printk(KERN_WARNING "Can't get bus-range for %s, "
113 "assuming it starts at 0\n", node->full_name);
114 pci_to_OF_bus_map[pci_bus] = 0;
116 pci_to_OF_bus_map[pci_bus] = bus_range[0];
118 for_each_child_of_node(node, node) {
120 const unsigned int *class_code, *reg;
122 class_code = of_get_property(node, "class-code", NULL);
123 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
124 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
126 reg = of_get_property(node, "reg", NULL);
129 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
130 if (!dev || !dev->subordinate) {
134 make_one_node_map(node, dev->subordinate->number);
140 pcibios_make_OF_bus_map(void)
143 struct pci_controller *hose, *tmp;
144 struct property *map_prop;
145 struct device_node *dn;
147 pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
148 if (!pci_to_OF_bus_map) {
149 printk(KERN_ERR "Can't allocate OF bus map !\n");
153 /* We fill the bus map with invalid values, that helps
156 for (i=0; i<pci_bus_count; i++)
157 pci_to_OF_bus_map[i] = 0xff;
159 /* For each hose, we begin searching bridges */
160 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
161 struct device_node* node = hose->dn;
165 make_one_node_map(node, hose->first_busno);
167 dn = of_find_node_by_path("/");
168 map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
170 BUG_ON(pci_bus_count > map_prop->length);
171 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
175 printk("PCI->OF bus map:\n");
176 for (i=0; i<pci_bus_count; i++) {
177 if (pci_to_OF_bus_map[i] == 0xff)
179 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
184 typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
186 static struct device_node*
187 scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data)
189 struct device_node *node;
190 struct device_node* sub_node;
192 for_each_child_of_node(parent, node) {
193 const unsigned int *class_code;
195 if (filter(node, data)) {
200 /* For PCI<->PCI bridges or CardBus bridges, we go down
201 * Note: some OFs create a parent node "multifunc-device" as
202 * a fake root for all functions of a multi-function device,
203 * we go down them as well.
205 class_code = of_get_property(node, "class-code", NULL);
206 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
207 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
208 strcmp(node->name, "multifunc-device"))
210 sub_node = scan_OF_pci_childs(node, filter, data);
219 static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
222 struct device_node *np;
226 for_each_child_of_node(parent, np) {
227 reg = of_get_property(np, "reg", &psize);
228 if (reg == NULL || psize < 4)
230 if (((reg[0] >> 8) & 0xff) == devfn)
237 static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
239 struct device_node *parent, *np;
241 /* Are we a root bus ? */
242 if (bus->self == NULL || bus->parent == NULL) {
243 struct pci_controller *hose = pci_bus_to_host(bus);
246 return of_node_get(hose->dn);
249 /* not a root bus, we need to get our parent */
250 parent = scan_OF_for_pci_bus(bus->parent);
254 /* now iterate for children for a match */
255 np = scan_OF_for_pci_dev(parent, bus->self->devfn);
262 * Scans the OF tree for a device node matching a PCI device
265 pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
267 struct device_node *parent, *np;
269 pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
270 parent = scan_OF_for_pci_bus(bus);
273 pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
274 np = scan_OF_for_pci_dev(parent, devfn);
276 pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
278 /* XXX most callers don't release the returned node
279 * mostly because ppc64 doesn't increase the refcount,
280 * we need to fix that.
284 EXPORT_SYMBOL(pci_busdev_to_OF_node);
287 pci_device_to_OF_node(struct pci_dev *dev)
289 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
291 EXPORT_SYMBOL(pci_device_to_OF_node);
294 find_OF_pci_device_filter(struct device_node* node, void* data)
296 return ((void *)node == data);
300 * Returns the PCI device matching a given OF node
303 pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
305 const unsigned int *reg;
306 struct pci_controller* hose;
307 struct pci_dev* dev = NULL;
309 /* Make sure it's really a PCI device */
310 hose = pci_find_hose_for_OF_device(node);
311 if (!hose || !hose->dn)
313 if (!scan_OF_pci_childs(hose->dn,
314 find_OF_pci_device_filter, (void *)node))
316 reg = of_get_property(node, "reg", NULL);
319 *bus = (reg[0] >> 16) & 0xff;
320 *devfn = ((reg[0] >> 8) & 0xff);
322 /* Ok, here we need some tweak. If we have already renumbered
323 * all busses, we can't rely on the OF bus number any more.
324 * the pci_to_OF_bus_map is not enough as several PCI busses
325 * may match the same OF bus number.
327 if (!pci_to_OF_bus_map)
330 for_each_pci_dev(dev)
331 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
332 dev->devfn == *devfn) {
333 *bus = dev->bus->number;
340 EXPORT_SYMBOL(pci_device_from_OF_node);
342 /* We create the "pci-OF-bus-map" property now so it appears in the
346 pci_create_OF_bus_map(void)
348 struct property* of_prop;
349 struct device_node *dn;
351 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
354 dn = of_find_node_by_path("/");
356 memset(of_prop, -1, sizeof(struct property) + 256);
357 of_prop->name = "pci-OF-bus-map";
358 of_prop->length = 256;
359 of_prop->value = &of_prop[1];
360 prom_add_property(dn, of_prop);
365 #else /* CONFIG_PPC_OF */
366 void pcibios_make_OF_bus_map(void)
369 #endif /* CONFIG_PPC_OF */
371 static void __devinit pcibios_scan_phb(struct pci_controller *hose)
374 struct device_node *node = hose->dn;
375 unsigned long io_offset;
376 struct resource *res = &hose->io_resource;
378 pr_debug("PCI: Scanning PHB %s\n",
379 node ? node->full_name : "<NO NAME>");
381 /* Create an empty bus for the toplevel */
382 bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
384 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
385 hose->global_number);
388 bus->secondary = hose->first_busno;
391 /* Fixup IO space offset */
392 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
393 res->start = (res->start + io_offset) & 0xffffffffu;
394 res->end = (res->end + io_offset) & 0xffffffffu;
396 /* Wire up PHB bus resources */
397 pcibios_setup_phb_resources(hose);
400 hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
403 static int __init pcibios_init(void)
405 struct pci_controller *hose, *tmp;
408 printk(KERN_INFO "PCI: Probing PCI hardware\n");
410 if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
411 pci_assign_all_buses = 1;
413 /* Scan all of the recorded PCI controllers. */
414 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
415 if (pci_assign_all_buses)
416 hose->first_busno = next_busno;
417 hose->last_busno = 0xff;
418 pcibios_scan_phb(hose);
419 pci_bus_add_devices(hose->bus);
420 if (pci_assign_all_buses || next_busno <= hose->last_busno)
421 next_busno = hose->last_busno + pcibios_assign_bus_offset;
423 pci_bus_count = next_busno;
425 /* OpenFirmware based machines need a map of OF bus
426 * numbers vs. kernel bus numbers since we may have to
429 if (pci_assign_all_buses)
430 pcibios_make_OF_bus_map();
432 /* Call common code to handle resource allocation */
433 pcibios_resource_survey();
435 /* Call machine dependent post-init code */
436 if (ppc_md.pcibios_after_init)
437 ppc_md.pcibios_after_init();
442 subsys_initcall(pcibios_init);
444 /* the next one is stolen from the alpha port... */
446 pcibios_update_irq(struct pci_dev *dev, int irq)
448 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
449 /* XXX FIXME - update OF device tree node interrupt property */
452 static struct pci_controller*
453 pci_bus_to_hose(int bus)
455 struct pci_controller *hose, *tmp;
457 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
458 if (bus >= hose->first_busno && bus <= hose->last_busno)
463 /* Provide information on locations of various I/O regions in physical
464 * memory. Do this on a per-card basis so that we choose the right
466 * Note that the returned IO or memory base is a physical address
469 long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
471 struct pci_controller* hose;
472 long result = -EOPNOTSUPP;
474 hose = pci_bus_to_hose(bus);
479 case IOBASE_BRIDGE_NUMBER:
480 return (long)hose->first_busno;
482 return (long)hose->pci_mem_offset;
484 return (long)hose->io_base_phys;
486 return (long)isa_io_base;
488 return (long)isa_mem_base;
494 unsigned long pci_address_to_pio(phys_addr_t address)
496 struct pci_controller *hose, *tmp;
498 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
499 unsigned int size = hose->io_resource.end -
500 hose->io_resource.start + 1;
501 if (address >= hose->io_base_phys &&
502 address < (hose->io_base_phys + size)) {
504 (unsigned long)hose->io_base_virt - _IO_BASE;
505 return base + (address - hose->io_base_phys);
508 return (unsigned int)-1;
510 EXPORT_SYMBOL(pci_address_to_pio);
513 * Null PCI config access functions, for the case when we can't
516 #define NULL_PCI_OP(rw, size, type) \
518 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
520 return PCIBIOS_DEVICE_NOT_FOUND; \
524 null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
527 return PCIBIOS_DEVICE_NOT_FOUND;
531 null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
534 return PCIBIOS_DEVICE_NOT_FOUND;
537 static struct pci_ops null_pci_ops =
539 .read = null_read_config,
540 .write = null_write_config,
544 * These functions are used early on before PCI scanning is done
545 * and all of the pci_dev and pci_bus structures have been created.
547 static struct pci_bus *
548 fake_pci_bus(struct pci_controller *hose, int busnr)
550 static struct pci_bus bus;
553 hose = pci_bus_to_hose(busnr);
555 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
559 bus.ops = hose? hose->ops: &null_pci_ops;
563 #define EARLY_PCI_OP(rw, size, type) \
564 int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
565 int devfn, int offset, type value) \
567 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
568 devfn, offset, value); \
571 EARLY_PCI_OP(read, byte, u8 *)
572 EARLY_PCI_OP(read, word, u16 *)
573 EARLY_PCI_OP(read, dword, u32 *)
574 EARLY_PCI_OP(write, byte, u8)
575 EARLY_PCI_OP(write, word, u16)
576 EARLY_PCI_OP(write, dword, u32)
578 extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
579 int early_find_capability(struct pci_controller *hose, int bus, int devfn,
582 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);