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>
28 unsigned long isa_io_base;
29 unsigned long pci_dram_offset;
30 int pcibios_assign_bus_offset = 1;
32 static u8 *pci_to_OF_bus_map;
34 /* By default, we don't re-assign bus numbers. We do this only on
37 static int pci_assign_all_buses;
39 static int pci_bus_count;
42 * Functions below are used on OpenFirmware machines.
45 make_one_node_map(struct device_node *node, u8 pci_bus)
50 if (pci_bus >= pci_bus_count)
52 bus_range = of_get_property(node, "bus-range", &len);
53 if (bus_range == NULL || len < 2 * sizeof(int)) {
54 printk(KERN_WARNING "Can't get bus-range for %s, "
55 "assuming it starts at 0\n", node->full_name);
56 pci_to_OF_bus_map[pci_bus] = 0;
58 pci_to_OF_bus_map[pci_bus] = bus_range[0];
60 for_each_child_of_node(node, node) {
62 const unsigned int *class_code, *reg;
64 class_code = of_get_property(node, "class-code", NULL);
66 ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
67 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
69 reg = of_get_property(node, "reg", NULL);
72 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
73 if (!dev || !dev->subordinate) {
77 make_one_node_map(node, dev->subordinate->number);
83 pcibios_make_OF_bus_map(void)
86 struct pci_controller *hose, *tmp;
87 struct property *map_prop;
88 struct device_node *dn;
90 pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
91 if (!pci_to_OF_bus_map) {
92 printk(KERN_ERR "Can't allocate OF bus map !\n");
96 /* We fill the bus map with invalid values, that helps
99 for (i = 0; i < pci_bus_count; i++)
100 pci_to_OF_bus_map[i] = 0xff;
102 /* For each hose, we begin searching bridges */
103 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
104 struct device_node *node = hose->dn;
108 make_one_node_map(node, hose->first_busno);
110 dn = of_find_node_by_path("/");
111 map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
113 BUG_ON(pci_bus_count > map_prop->length);
114 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
118 printk(KERN_INFO "PCI->OF bus map:\n");
119 for (i = 0; i < pci_bus_count; i++) {
120 if (pci_to_OF_bus_map[i] == 0xff)
122 printk(KERN_INFO "%d -> %d\n", i, pci_to_OF_bus_map[i]);
127 typedef int (*pci_OF_scan_iterator)(struct device_node *node, void *data);
129 static struct device_node *scan_OF_pci_childs(struct device_node *parent,
130 pci_OF_scan_iterator filter, void *data)
132 struct device_node *node;
133 struct device_node *sub_node;
135 for_each_child_of_node(parent, node) {
136 const unsigned int *class_code;
138 if (filter(node, data)) {
143 /* For PCI<->PCI bridges or CardBus bridges, we go down
144 * Note: some OFs create a parent node "multifunc-device" as
145 * a fake root for all functions of a multi-function device,
146 * we go down them as well.
148 class_code = of_get_property(node, "class-code", NULL);
150 ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
151 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
152 strcmp(node->name, "multifunc-device"))
154 sub_node = scan_OF_pci_childs(node, filter, data);
163 static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
166 struct device_node *np, *cnp;
170 for_each_child_of_node(parent, np) {
171 reg = of_get_property(np, "reg", &psize);
172 if (reg && psize >= 4 && ((reg[0] >> 8) & 0xff) == devfn)
175 /* Note: some OFs create a parent node "multifunc-device" as
176 * a fake root for all functions of a multi-function device,
177 * we go down them as well. */
178 if (!strcmp(np->name, "multifunc-device")) {
179 cnp = scan_OF_for_pci_dev(np, devfn);
188 static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
190 struct device_node *parent, *np;
192 /* Are we a root bus ? */
193 if (bus->self == NULL || bus->parent == NULL) {
194 struct pci_controller *hose = pci_bus_to_host(bus);
197 return of_node_get(hose->dn);
200 /* not a root bus, we need to get our parent */
201 parent = scan_OF_for_pci_bus(bus->parent);
205 /* now iterate for children for a match */
206 np = scan_OF_for_pci_dev(parent, bus->self->devfn);
213 * Scans the OF tree for a device node matching a PCI device
216 pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
218 struct device_node *parent, *np;
220 pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
221 parent = scan_OF_for_pci_bus(bus);
224 pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
225 np = scan_OF_for_pci_dev(parent, devfn);
227 pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
229 /* XXX most callers don't release the returned node
230 * mostly because ppc64 doesn't increase the refcount,
231 * we need to fix that.
235 EXPORT_SYMBOL(pci_busdev_to_OF_node);
238 pci_device_to_OF_node(struct pci_dev *dev)
240 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
242 EXPORT_SYMBOL(pci_device_to_OF_node);
245 find_OF_pci_device_filter(struct device_node *node, void *data)
247 return ((void *)node == data);
251 * Returns the PCI device matching a given OF node
254 pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn)
256 const unsigned int *reg;
257 struct pci_controller *hose;
258 struct pci_dev *dev = NULL;
260 /* Make sure it's really a PCI device */
261 hose = pci_find_hose_for_OF_device(node);
262 if (!hose || !hose->dn)
264 if (!scan_OF_pci_childs(hose->dn,
265 find_OF_pci_device_filter, (void *)node))
267 reg = of_get_property(node, "reg", NULL);
270 *bus = (reg[0] >> 16) & 0xff;
271 *devfn = ((reg[0] >> 8) & 0xff);
273 /* Ok, here we need some tweak. If we have already renumbered
274 * all busses, we can't rely on the OF bus number any more.
275 * the pci_to_OF_bus_map is not enough as several PCI busses
276 * may match the same OF bus number.
278 if (!pci_to_OF_bus_map)
281 for_each_pci_dev(dev)
282 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
283 dev->devfn == *devfn) {
284 *bus = dev->bus->number;
291 EXPORT_SYMBOL(pci_device_from_OF_node);
293 /* We create the "pci-OF-bus-map" property now so it appears in the
297 pci_create_OF_bus_map(void)
299 struct property *of_prop;
300 struct device_node *dn;
302 of_prop = (struct property *) alloc_bootmem(sizeof(struct property) + \
306 dn = of_find_node_by_path("/");
308 memset(of_prop, -1, sizeof(struct property) + 256);
309 of_prop->name = "pci-OF-bus-map";
310 of_prop->length = 256;
311 of_prop->value = &of_prop[1];
312 prom_add_property(dn, of_prop);
317 static void __devinit pcibios_scan_phb(struct pci_controller *hose)
320 struct device_node *node = hose->dn;
321 unsigned long io_offset;
322 struct resource *res = &hose->io_resource;
324 pr_debug("PCI: Scanning PHB %s\n",
325 node ? node->full_name : "<NO NAME>");
327 /* Create an empty bus for the toplevel */
328 bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
330 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
331 hose->global_number);
334 bus->secondary = hose->first_busno;
337 /* Fixup IO space offset */
338 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
339 res->start = (res->start + io_offset) & 0xffffffffu;
340 res->end = (res->end + io_offset) & 0xffffffffu;
342 /* Wire up PHB bus resources */
343 pcibios_setup_phb_resources(hose);
346 hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
349 static int __init pcibios_init(void)
351 struct pci_controller *hose, *tmp;
354 printk(KERN_INFO "PCI: Probing PCI hardware\n");
356 if (pci_flags & PCI_REASSIGN_ALL_BUS) {
357 printk(KERN_INFO "setting pci_asign_all_busses\n");
358 pci_assign_all_buses = 1;
361 /* Scan all of the recorded PCI controllers. */
362 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
363 if (pci_assign_all_buses)
364 hose->first_busno = next_busno;
365 hose->last_busno = 0xff;
366 pcibios_scan_phb(hose);
367 printk(KERN_INFO "calling pci_bus_add_devices()\n");
368 pci_bus_add_devices(hose->bus);
369 if (pci_assign_all_buses || next_busno <= hose->last_busno)
370 next_busno = hose->last_busno + \
371 pcibios_assign_bus_offset;
373 pci_bus_count = next_busno;
375 /* OpenFirmware based machines need a map of OF bus
376 * numbers vs. kernel bus numbers since we may have to
379 if (pci_assign_all_buses)
380 pcibios_make_OF_bus_map();
382 /* Call common code to handle resource allocation */
383 pcibios_resource_survey();
388 subsys_initcall(pcibios_init);
390 static struct pci_controller*
391 pci_bus_to_hose(int bus)
393 struct pci_controller *hose, *tmp;
395 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
396 if (bus >= hose->first_busno && bus <= hose->last_busno)
401 /* Provide information on locations of various I/O regions in physical
402 * memory. Do this on a per-card basis so that we choose the right
404 * Note that the returned IO or memory base is a physical address
407 long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
409 struct pci_controller *hose;
410 long result = -EOPNOTSUPP;
412 hose = pci_bus_to_hose(bus);
417 case IOBASE_BRIDGE_NUMBER:
418 return (long)hose->first_busno;
420 return (long)hose->pci_mem_offset;
422 return (long)hose->io_base_phys;
424 return (long)isa_io_base;
426 return (long)isa_mem_base;