blob: d473634e39e3cf63e10de5d3f8aef5075eafd9a8 [file] [log] [blame]
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001/*
2 * Common pmac/prep/chrp pci routines. -- Cort
3 */
4
Paul Mackerrase05b3b42006-01-15 22:05:47 +11005#include <linux/kernel.h>
6#include <linux/pci.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>
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -070014#include <linux/irq.h>
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +110015#include <linux/list.h>
Stephen Rothwell66524b22007-12-21 15:52:43 +110016#include <linux/of.h>
Paul Mackerrase05b3b42006-01-15 22:05:47 +110017
18#include <asm/processor.h>
19#include <asm/io.h>
20#include <asm/prom.h>
21#include <asm/sections.h>
22#include <asm/pci-bridge.h>
Milton Millerc3bd5172009-01-08 02:19:46 +000023#include <asm/ppc-pci.h>
Paul Mackerrase05b3b42006-01-15 22:05:47 +110024#include <asm/byteorder.h>
Paul Mackerrase05b3b42006-01-15 22:05:47 +110025#include <asm/uaccess.h>
26#include <asm/machdep.h>
27
28#undef DEBUG
29
Paul Mackerrase05b3b42006-01-15 22:05:47 +110030unsigned long isa_io_base = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +110031unsigned long pci_dram_offset = 0;
32int pcibios_assign_bus_offset = 1;
33
34void pcibios_make_OF_bus_map(void);
35
Paul Mackerrase05b3b42006-01-15 22:05:47 +110036static void fixup_broken_pcnet32(struct pci_dev* dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +110037static void fixup_cpc710_pci64(struct pci_dev* dev);
38#ifdef CONFIG_PPC_OF
39static u8* pci_to_OF_bus_map;
40#endif
41
42/* By default, we don't re-assign bus numbers. We do this only on
43 * some pmacs
44 */
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +110045static int pci_assign_all_buses;
Paul Mackerrase05b3b42006-01-15 22:05:47 +110046
Paul Mackerrase05b3b42006-01-15 22:05:47 +110047static int pci_bus_count;
48
Benjamin Herrenschmidt7b6b5742008-10-13 17:51:46 +000049/* This will remain NULL for now, until isa-bridge.c is made common
50 * to both 32-bit and 64-bit.
51 */
52struct pci_dev *isa_bridge_pcidev;
53EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
54
Paul Mackerrase05b3b42006-01-15 22:05:47 +110055static void
John Rigby4a015c32008-10-07 13:00:20 -060056fixup_hide_host_resource_fsl(struct pci_dev *dev)
Kumar Gala2052d6d2007-07-25 00:44:11 -050057{
58 int i, class = dev->class >> 8;
59
John Rigby4a015c32008-10-07 13:00:20 -060060 if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
61 class == PCI_CLASS_BRIDGE_OTHER) &&
Kumar Gala2052d6d2007-07-25 00:44:11 -050062 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
63 (dev->bus->parent == NULL)) {
64 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
65 dev->resource[i].start = 0;
66 dev->resource[i].end = 0;
67 dev->resource[i].flags = 0;
68 }
69 }
70}
71DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
72DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
73
74static void
Paul Mackerrase05b3b42006-01-15 22:05:47 +110075fixup_broken_pcnet32(struct pci_dev* dev)
76{
77 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
78 dev->vendor = PCI_VENDOR_ID_AMD;
79 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
80 }
81}
82DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
83
84static void
85fixup_cpc710_pci64(struct pci_dev* dev)
86{
87 /* Hide the PCI64 BARs from the kernel as their content doesn't
88 * fit well in the resource management
89 */
90 dev->resource[0].start = dev->resource[0].end = 0;
91 dev->resource[0].flags = 0;
92 dev->resource[1].start = dev->resource[1].end = 0;
93 dev->resource[1].flags = 0;
94}
95DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
96
Paul Mackerrase05b3b42006-01-15 22:05:47 +110097#ifdef CONFIG_PPC_OF
98/*
99 * Functions below are used on OpenFirmware machines.
100 */
101static void
102make_one_node_map(struct device_node* node, u8 pci_bus)
103{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000104 const int *bus_range;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100105 int len;
106
107 if (pci_bus >= pci_bus_count)
108 return;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000109 bus_range = of_get_property(node, "bus-range", &len);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100110 if (bus_range == NULL || len < 2 * sizeof(int)) {
111 printk(KERN_WARNING "Can't get bus-range for %s, "
112 "assuming it starts at 0\n", node->full_name);
113 pci_to_OF_bus_map[pci_bus] = 0;
114 } else
115 pci_to_OF_bus_map[pci_bus] = bus_range[0];
116
Stephen Rothwell66524b22007-12-21 15:52:43 +1100117 for_each_child_of_node(node, node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100118 struct pci_dev* dev;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000119 const unsigned int *class_code, *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100120
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000121 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100122 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
123 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
124 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000125 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100126 if (!reg)
127 continue;
Alan Coxab462762007-04-23 14:47:59 +0100128 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
129 if (!dev || !dev->subordinate) {
130 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100131 continue;
Alan Coxab462762007-04-23 14:47:59 +0100132 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100133 make_one_node_map(node, dev->subordinate->number);
Alan Coxab462762007-04-23 14:47:59 +0100134 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100135 }
136}
137
138void
139pcibios_make_OF_bus_map(void)
140{
141 int i;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500142 struct pci_controller *hose, *tmp;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000143 struct property *map_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000144 struct device_node *dn;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100145
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800146 pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100147 if (!pci_to_OF_bus_map) {
148 printk(KERN_ERR "Can't allocate OF bus map !\n");
149 return;
150 }
151
152 /* We fill the bus map with invalid values, that helps
153 * debugging.
154 */
155 for (i=0; i<pci_bus_count; i++)
156 pci_to_OF_bus_map[i] = 0xff;
157
158 /* For each hose, we begin searching bridges */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500159 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100160 struct device_node* node = hose->dn;
161
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100162 if (!node)
163 continue;
164 make_one_node_map(node, hose->first_busno);
165 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000166 dn = of_find_node_by_path("/");
167 map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000168 if (map_prop) {
169 BUG_ON(pci_bus_count > map_prop->length);
170 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
171 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000172 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100173#ifdef DEBUG
174 printk("PCI->OF bus map:\n");
175 for (i=0; i<pci_bus_count; i++) {
176 if (pci_to_OF_bus_map[i] == 0xff)
177 continue;
178 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
179 }
180#endif
181}
182
183typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
184
185static struct device_node*
Stephen Rothwell66524b22007-12-21 15:52:43 +1100186scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100187{
Stephen Rothwell66524b22007-12-21 15:52:43 +1100188 struct device_node *node;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100189 struct device_node* sub_node;
190
Stephen Rothwell66524b22007-12-21 15:52:43 +1100191 for_each_child_of_node(parent, node) {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000192 const unsigned int *class_code;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100193
Stephen Rothwell66524b22007-12-21 15:52:43 +1100194 if (filter(node, data)) {
195 of_node_put(node);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100196 return node;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100197 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100198
199 /* For PCI<->PCI bridges or CardBus bridges, we go down
200 * Note: some OFs create a parent node "multifunc-device" as
201 * a fake root for all functions of a multi-function device,
202 * we go down them as well.
203 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000204 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100205 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
206 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
207 strcmp(node->name, "multifunc-device"))
208 continue;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100209 sub_node = scan_OF_pci_childs(node, filter, data);
210 if (sub_node) {
211 of_node_put(node);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100212 return sub_node;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100213 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100214 }
215 return NULL;
216}
217
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100218static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
219 unsigned int devfn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100220{
Tom Arbucklef8178692009-02-11 10:41:48 +0000221 struct device_node *np, *cnp;
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100222 const u32 *reg;
223 unsigned int psize;
224
Stephen Rothwell85e99b92008-01-03 15:13:37 +1100225 for_each_child_of_node(parent, np) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000226 reg = of_get_property(np, "reg", &psize);
Tom Arbucklef8178692009-02-11 10:41:48 +0000227 if (reg && psize >= 4 && ((reg[0] >> 8) & 0xff) == devfn)
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100228 return np;
Tom Arbucklef8178692009-02-11 10:41:48 +0000229
230 /* Note: some OFs create a parent node "multifunc-device" as
231 * a fake root for all functions of a multi-function device,
232 * we go down them as well. */
233 if (!strcmp(np->name, "multifunc-device")) {
234 cnp = scan_OF_for_pci_dev(np, devfn);
235 if (cnp)
236 return cnp;
237 }
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100238 }
239 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100240}
241
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100242
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100243static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
244{
245 struct device_node *parent, *np;
246
247 /* Are we a root bus ? */
248 if (bus->self == NULL || bus->parent == NULL) {
Kumar Gala0b1d40c2007-06-27 10:27:33 -0500249 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100250 if (hose == NULL)
251 return NULL;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100252 return of_node_get(hose->dn);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100253 }
254
255 /* not a root bus, we need to get our parent */
256 parent = scan_OF_for_pci_bus(bus->parent);
257 if (parent == NULL)
258 return NULL;
259
260 /* now iterate for children for a match */
261 np = scan_OF_for_pci_dev(parent, bus->self->devfn);
262 of_node_put(parent);
263
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100264 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100265}
266
267/*
268 * Scans the OF tree for a device node matching a PCI device
269 */
270struct device_node *
271pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
272{
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100273 struct device_node *parent, *np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100274
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000275 pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100276 parent = scan_OF_for_pci_bus(bus);
277 if (parent == NULL)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100278 return NULL;
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000279 pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100280 np = scan_OF_for_pci_dev(parent, devfn);
281 of_node_put(parent);
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000282 pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100283
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100284 /* XXX most callers don't release the returned node
285 * mostly because ppc64 doesn't increase the refcount,
286 * we need to fix that.
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100287 */
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100288 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100289}
290EXPORT_SYMBOL(pci_busdev_to_OF_node);
291
292struct device_node*
293pci_device_to_OF_node(struct pci_dev *dev)
294{
295 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
296}
297EXPORT_SYMBOL(pci_device_to_OF_node);
298
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100299static int
300find_OF_pci_device_filter(struct device_node* node, void* data)
301{
302 return ((void *)node == data);
303}
304
305/*
306 * Returns the PCI device matching a given OF node
307 */
308int
309pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
310{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000311 const unsigned int *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100312 struct pci_controller* hose;
313 struct pci_dev* dev = NULL;
314
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100315 /* Make sure it's really a PCI device */
316 hose = pci_find_hose_for_OF_device(node);
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100317 if (!hose || !hose->dn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100318 return -ENODEV;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100319 if (!scan_OF_pci_childs(hose->dn,
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100320 find_OF_pci_device_filter, (void *)node))
321 return -ENODEV;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000322 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100323 if (!reg)
324 return -ENODEV;
325 *bus = (reg[0] >> 16) & 0xff;
326 *devfn = ((reg[0] >> 8) & 0xff);
327
328 /* Ok, here we need some tweak. If we have already renumbered
329 * all busses, we can't rely on the OF bus number any more.
330 * the pci_to_OF_bus_map is not enough as several PCI busses
331 * may match the same OF bus number.
332 */
333 if (!pci_to_OF_bus_map)
334 return 0;
335
336 for_each_pci_dev(dev)
337 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
338 dev->devfn == *devfn) {
339 *bus = dev->bus->number;
340 pci_dev_put(dev);
341 return 0;
342 }
343
344 return -ENODEV;
345}
346EXPORT_SYMBOL(pci_device_from_OF_node);
347
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100348/* We create the "pci-OF-bus-map" property now so it appears in the
349 * /proc device tree
350 */
351void __init
352pci_create_OF_bus_map(void)
353{
354 struct property* of_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000355 struct device_node *dn;
356
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100357 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000358 if (!of_prop)
359 return;
360 dn = of_find_node_by_path("/");
361 if (dn) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100362 memset(of_prop, -1, sizeof(struct property) + 256);
363 of_prop->name = "pci-OF-bus-map";
364 of_prop->length = 256;
Stephen Rothwell1a381472007-04-03 10:58:52 +1000365 of_prop->value = &of_prop[1];
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000366 prom_add_property(dn, of_prop);
367 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100368 }
369}
370
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100371#else /* CONFIG_PPC_OF */
372void pcibios_make_OF_bus_map(void)
373{
374}
375#endif /* CONFIG_PPC_OF */
376
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +0000377static void __devinit pcibios_scan_phb(struct pci_controller *hose)
378{
379 struct pci_bus *bus;
380 struct device_node *node = hose->dn;
381 unsigned long io_offset;
382 struct resource *res = &hose->io_resource;
383
384 pr_debug("PCI: Scanning PHB %s\n",
385 node ? node->full_name : "<NO NAME>");
386
387 /* Create an empty bus for the toplevel */
388 bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
389 if (bus == NULL) {
390 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
391 hose->global_number);
392 return;
393 }
394 bus->secondary = hose->first_busno;
395 hose->bus = bus;
396
397 /* Fixup IO space offset */
398 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
399 res->start = (res->start + io_offset) & 0xffffffffu;
400 res->end = (res->end + io_offset) & 0xffffffffu;
401
402 /* Wire up PHB bus resources */
403 pcibios_setup_phb_resources(hose);
404
405 /* Scan children */
406 hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
407}
408
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100409static int __init pcibios_init(void)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100410{
Kumar Galaa4c9e322007-06-27 13:09:43 -0500411 struct pci_controller *hose, *tmp;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500412 int next_busno = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100413
414 printk(KERN_INFO "PCI: Probing PCI hardware\n");
415
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +1100416 if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
417 pci_assign_all_buses = 1;
418
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100419 /* Scan all of the recorded PCI controllers. */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500420 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100421 if (pci_assign_all_buses)
422 hose->first_busno = next_busno;
423 hose->last_busno = 0xff;
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +0000424 pcibios_scan_phb(hose);
425 pci_bus_add_devices(hose->bus);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100426 if (pci_assign_all_buses || next_busno <= hose->last_busno)
427 next_busno = hose->last_busno + pcibios_assign_bus_offset;
428 }
429 pci_bus_count = next_busno;
430
431 /* OpenFirmware based machines need a map of OF bus
432 * numbers vs. kernel bus numbers since we may have to
433 * remap them.
434 */
Anton Vorontsov6b82b3e2008-12-09 09:47:29 +0000435 if (pci_assign_all_buses)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100436 pcibios_make_OF_bus_map();
437
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100438 /* Call common code to handle resource allocation */
439 pcibios_resource_survey();
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100440
441 /* Call machine dependent post-init code */
442 if (ppc_md.pcibios_after_init)
443 ppc_md.pcibios_after_init();
444
445 return 0;
446}
447
448subsys_initcall(pcibios_init);
449
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100450/* the next one is stolen from the alpha port... */
451void __init
452pcibios_update_irq(struct pci_dev *dev, int irq)
453{
454 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
455 /* XXX FIXME - update OF device tree node interrupt property */
456}
457
Kumar Gala0b1d40c2007-06-27 10:27:33 -0500458static struct pci_controller*
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100459pci_bus_to_hose(int bus)
460{
Kumar Galaa4c9e322007-06-27 13:09:43 -0500461 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100462
Kumar Galaa4c9e322007-06-27 13:09:43 -0500463 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100464 if (bus >= hose->first_busno && bus <= hose->last_busno)
465 return hose;
466 return NULL;
467}
468
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100469/* Provide information on locations of various I/O regions in physical
470 * memory. Do this on a per-card basis so that we choose the right
471 * root bridge.
472 * Note that the returned IO or memory base is a physical address
473 */
474
475long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
476{
477 struct pci_controller* hose;
478 long result = -EOPNOTSUPP;
479
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100480 hose = pci_bus_to_hose(bus);
481 if (!hose)
482 return -ENODEV;
483
484 switch (which) {
485 case IOBASE_BRIDGE_NUMBER:
486 return (long)hose->first_busno;
487 case IOBASE_MEMORY:
488 return (long)hose->pci_mem_offset;
489 case IOBASE_IO:
490 return (long)hose->io_base_phys;
491 case IOBASE_ISA_IO:
492 return (long)isa_io_base;
493 case IOBASE_ISA_MEM:
494 return (long)isa_mem_base;
495 }
496
497 return result;
498}
499
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100500/*
501 * Null PCI config access functions, for the case when we can't
502 * find a hose.
503 */
504#define NULL_PCI_OP(rw, size, type) \
505static int \
506null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
507{ \
508 return PCIBIOS_DEVICE_NOT_FOUND; \
509}
510
511static int
512null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
513 int len, u32 *val)
514{
515 return PCIBIOS_DEVICE_NOT_FOUND;
516}
517
518static int
519null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
520 int len, u32 val)
521{
522 return PCIBIOS_DEVICE_NOT_FOUND;
523}
524
525static struct pci_ops null_pci_ops =
526{
Nathan Lynch6127d1c2007-08-10 05:18:42 +1000527 .read = null_read_config,
528 .write = null_write_config,
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100529};
530
531/*
532 * These functions are used early on before PCI scanning is done
533 * and all of the pci_dev and pci_bus structures have been created.
534 */
535static struct pci_bus *
536fake_pci_bus(struct pci_controller *hose, int busnr)
537{
538 static struct pci_bus bus;
539
540 if (hose == 0) {
541 hose = pci_bus_to_hose(busnr);
542 if (hose == 0)
543 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
544 }
545 bus.number = busnr;
546 bus.sysdata = hose;
547 bus.ops = hose? hose->ops: &null_pci_ops;
548 return &bus;
549}
550
551#define EARLY_PCI_OP(rw, size, type) \
552int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
553 int devfn, int offset, type value) \
554{ \
555 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
556 devfn, offset, value); \
557}
558
559EARLY_PCI_OP(read, byte, u8 *)
560EARLY_PCI_OP(read, word, u16 *)
561EARLY_PCI_OP(read, dword, u32 *)
562EARLY_PCI_OP(write, byte, u8)
563EARLY_PCI_OP(write, word, u16)
564EARLY_PCI_OP(write, dword, u32)
Kumar Gala38805e52007-07-10 23:37:45 -0500565
566extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
567int early_find_capability(struct pci_controller *hose, int bus, int devfn,
568 int cap)
569{
570 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
571}