blob: 3ae1c666ff9276c3a26ed90ce7c406f4717ab2d1 [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_cpc710_pci64(struct pci_dev* dev);
37#ifdef CONFIG_PPC_OF
38static u8* pci_to_OF_bus_map;
39#endif
40
41/* By default, we don't re-assign bus numbers. We do this only on
42 * some pmacs
43 */
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +110044static int pci_assign_all_buses;
Paul Mackerrase05b3b42006-01-15 22:05:47 +110045
Paul Mackerrase05b3b42006-01-15 22:05:47 +110046static int pci_bus_count;
47
Benjamin Herrenschmidt7b6b5742008-10-13 17:51:46 +000048/* This will remain NULL for now, until isa-bridge.c is made common
49 * to both 32-bit and 64-bit.
50 */
51struct pci_dev *isa_bridge_pcidev;
52EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
53
Paul Mackerrase05b3b42006-01-15 22:05:47 +110054static void
John Rigby4a015c32008-10-07 13:00:20 -060055fixup_hide_host_resource_fsl(struct pci_dev *dev)
Kumar Gala2052d6d2007-07-25 00:44:11 -050056{
57 int i, class = dev->class >> 8;
58
John Rigby4a015c32008-10-07 13:00:20 -060059 if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
60 class == PCI_CLASS_BRIDGE_OTHER) &&
Kumar Gala2052d6d2007-07-25 00:44:11 -050061 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
62 (dev->bus->parent == NULL)) {
63 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
64 dev->resource[i].start = 0;
65 dev->resource[i].end = 0;
66 dev->resource[i].flags = 0;
67 }
68 }
69}
70DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
71DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
72
73static void
Paul Mackerrase05b3b42006-01-15 22:05:47 +110074fixup_cpc710_pci64(struct pci_dev* dev)
75{
76 /* Hide the PCI64 BARs from the kernel as their content doesn't
77 * fit well in the resource management
78 */
79 dev->resource[0].start = dev->resource[0].end = 0;
80 dev->resource[0].flags = 0;
81 dev->resource[1].start = dev->resource[1].end = 0;
82 dev->resource[1].flags = 0;
83}
84DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
85
Paul Mackerrase05b3b42006-01-15 22:05:47 +110086#ifdef CONFIG_PPC_OF
87/*
88 * Functions below are used on OpenFirmware machines.
89 */
90static void
91make_one_node_map(struct device_node* node, u8 pci_bus)
92{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100093 const int *bus_range;
Paul Mackerrase05b3b42006-01-15 22:05:47 +110094 int len;
95
96 if (pci_bus >= pci_bus_count)
97 return;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100098 bus_range = of_get_property(node, "bus-range", &len);
Paul Mackerrase05b3b42006-01-15 22:05:47 +110099 if (bus_range == NULL || len < 2 * sizeof(int)) {
100 printk(KERN_WARNING "Can't get bus-range for %s, "
101 "assuming it starts at 0\n", node->full_name);
102 pci_to_OF_bus_map[pci_bus] = 0;
103 } else
104 pci_to_OF_bus_map[pci_bus] = bus_range[0];
105
Stephen Rothwell66524b22007-12-21 15:52:43 +1100106 for_each_child_of_node(node, node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100107 struct pci_dev* dev;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000108 const unsigned int *class_code, *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100109
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000110 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100111 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
112 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
113 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000114 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100115 if (!reg)
116 continue;
Alan Coxab462762007-04-23 14:47:59 +0100117 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
118 if (!dev || !dev->subordinate) {
119 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100120 continue;
Alan Coxab462762007-04-23 14:47:59 +0100121 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100122 make_one_node_map(node, dev->subordinate->number);
Alan Coxab462762007-04-23 14:47:59 +0100123 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100124 }
125}
126
127void
128pcibios_make_OF_bus_map(void)
129{
130 int i;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500131 struct pci_controller *hose, *tmp;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000132 struct property *map_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000133 struct device_node *dn;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100134
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800135 pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100136 if (!pci_to_OF_bus_map) {
137 printk(KERN_ERR "Can't allocate OF bus map !\n");
138 return;
139 }
140
141 /* We fill the bus map with invalid values, that helps
142 * debugging.
143 */
144 for (i=0; i<pci_bus_count; i++)
145 pci_to_OF_bus_map[i] = 0xff;
146
147 /* For each hose, we begin searching bridges */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500148 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100149 struct device_node* node = hose->dn;
150
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100151 if (!node)
152 continue;
153 make_one_node_map(node, hose->first_busno);
154 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000155 dn = of_find_node_by_path("/");
156 map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000157 if (map_prop) {
158 BUG_ON(pci_bus_count > map_prop->length);
159 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
160 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000161 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100162#ifdef DEBUG
163 printk("PCI->OF bus map:\n");
164 for (i=0; i<pci_bus_count; i++) {
165 if (pci_to_OF_bus_map[i] == 0xff)
166 continue;
167 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
168 }
169#endif
170}
171
172typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
173
174static struct device_node*
Stephen Rothwell66524b22007-12-21 15:52:43 +1100175scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100176{
Stephen Rothwell66524b22007-12-21 15:52:43 +1100177 struct device_node *node;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100178 struct device_node* sub_node;
179
Stephen Rothwell66524b22007-12-21 15:52:43 +1100180 for_each_child_of_node(parent, node) {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000181 const unsigned int *class_code;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100182
Stephen Rothwell66524b22007-12-21 15:52:43 +1100183 if (filter(node, data)) {
184 of_node_put(node);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100185 return node;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100186 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100187
188 /* For PCI<->PCI bridges or CardBus bridges, we go down
189 * Note: some OFs create a parent node "multifunc-device" as
190 * a fake root for all functions of a multi-function device,
191 * we go down them as well.
192 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000193 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100194 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
195 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
196 strcmp(node->name, "multifunc-device"))
197 continue;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100198 sub_node = scan_OF_pci_childs(node, filter, data);
199 if (sub_node) {
200 of_node_put(node);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100201 return sub_node;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100202 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100203 }
204 return NULL;
205}
206
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100207static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
208 unsigned int devfn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100209{
Tom Arbucklef8178692009-02-11 10:41:48 +0000210 struct device_node *np, *cnp;
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100211 const u32 *reg;
212 unsigned int psize;
213
Stephen Rothwell85e99b92008-01-03 15:13:37 +1100214 for_each_child_of_node(parent, np) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000215 reg = of_get_property(np, "reg", &psize);
Tom Arbucklef8178692009-02-11 10:41:48 +0000216 if (reg && psize >= 4 && ((reg[0] >> 8) & 0xff) == devfn)
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100217 return np;
Tom Arbucklef8178692009-02-11 10:41:48 +0000218
219 /* Note: some OFs create a parent node "multifunc-device" as
220 * a fake root for all functions of a multi-function device,
221 * we go down them as well. */
222 if (!strcmp(np->name, "multifunc-device")) {
223 cnp = scan_OF_for_pci_dev(np, devfn);
224 if (cnp)
225 return cnp;
226 }
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100227 }
228 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100229}
230
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100231
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100232static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
233{
234 struct device_node *parent, *np;
235
236 /* Are we a root bus ? */
237 if (bus->self == NULL || bus->parent == NULL) {
Kumar Gala0b1d40c2007-06-27 10:27:33 -0500238 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100239 if (hose == NULL)
240 return NULL;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100241 return of_node_get(hose->dn);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100242 }
243
244 /* not a root bus, we need to get our parent */
245 parent = scan_OF_for_pci_bus(bus->parent);
246 if (parent == NULL)
247 return NULL;
248
249 /* now iterate for children for a match */
250 np = scan_OF_for_pci_dev(parent, bus->self->devfn);
251 of_node_put(parent);
252
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100253 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100254}
255
256/*
257 * Scans the OF tree for a device node matching a PCI device
258 */
259struct device_node *
260pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
261{
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100262 struct device_node *parent, *np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100263
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000264 pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100265 parent = scan_OF_for_pci_bus(bus);
266 if (parent == NULL)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100267 return NULL;
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000268 pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100269 np = scan_OF_for_pci_dev(parent, devfn);
270 of_node_put(parent);
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000271 pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100272
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100273 /* XXX most callers don't release the returned node
274 * mostly because ppc64 doesn't increase the refcount,
275 * we need to fix that.
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100276 */
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100277 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100278}
279EXPORT_SYMBOL(pci_busdev_to_OF_node);
280
281struct device_node*
282pci_device_to_OF_node(struct pci_dev *dev)
283{
284 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
285}
286EXPORT_SYMBOL(pci_device_to_OF_node);
287
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100288static int
289find_OF_pci_device_filter(struct device_node* node, void* data)
290{
291 return ((void *)node == data);
292}
293
294/*
295 * Returns the PCI device matching a given OF node
296 */
297int
298pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
299{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000300 const unsigned int *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100301 struct pci_controller* hose;
302 struct pci_dev* dev = NULL;
303
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100304 /* Make sure it's really a PCI device */
305 hose = pci_find_hose_for_OF_device(node);
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100306 if (!hose || !hose->dn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100307 return -ENODEV;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100308 if (!scan_OF_pci_childs(hose->dn,
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100309 find_OF_pci_device_filter, (void *)node))
310 return -ENODEV;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000311 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100312 if (!reg)
313 return -ENODEV;
314 *bus = (reg[0] >> 16) & 0xff;
315 *devfn = ((reg[0] >> 8) & 0xff);
316
317 /* Ok, here we need some tweak. If we have already renumbered
318 * all busses, we can't rely on the OF bus number any more.
319 * the pci_to_OF_bus_map is not enough as several PCI busses
320 * may match the same OF bus number.
321 */
322 if (!pci_to_OF_bus_map)
323 return 0;
324
325 for_each_pci_dev(dev)
326 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
327 dev->devfn == *devfn) {
328 *bus = dev->bus->number;
329 pci_dev_put(dev);
330 return 0;
331 }
332
333 return -ENODEV;
334}
335EXPORT_SYMBOL(pci_device_from_OF_node);
336
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100337/* We create the "pci-OF-bus-map" property now so it appears in the
338 * /proc device tree
339 */
340void __init
341pci_create_OF_bus_map(void)
342{
343 struct property* of_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000344 struct device_node *dn;
345
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100346 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000347 if (!of_prop)
348 return;
349 dn = of_find_node_by_path("/");
350 if (dn) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100351 memset(of_prop, -1, sizeof(struct property) + 256);
352 of_prop->name = "pci-OF-bus-map";
353 of_prop->length = 256;
Stephen Rothwell1a381472007-04-03 10:58:52 +1000354 of_prop->value = &of_prop[1];
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000355 prom_add_property(dn, of_prop);
356 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100357 }
358}
359
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100360#else /* CONFIG_PPC_OF */
361void pcibios_make_OF_bus_map(void)
362{
363}
364#endif /* CONFIG_PPC_OF */
365
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +0000366static void __devinit pcibios_scan_phb(struct pci_controller *hose)
367{
368 struct pci_bus *bus;
369 struct device_node *node = hose->dn;
370 unsigned long io_offset;
371 struct resource *res = &hose->io_resource;
372
373 pr_debug("PCI: Scanning PHB %s\n",
374 node ? node->full_name : "<NO NAME>");
375
376 /* Create an empty bus for the toplevel */
377 bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
378 if (bus == NULL) {
379 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
380 hose->global_number);
381 return;
382 }
383 bus->secondary = hose->first_busno;
384 hose->bus = bus;
385
386 /* Fixup IO space offset */
387 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
388 res->start = (res->start + io_offset) & 0xffffffffu;
389 res->end = (res->end + io_offset) & 0xffffffffu;
390
391 /* Wire up PHB bus resources */
392 pcibios_setup_phb_resources(hose);
393
394 /* Scan children */
395 hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
396}
397
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100398static int __init pcibios_init(void)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100399{
Kumar Galaa4c9e322007-06-27 13:09:43 -0500400 struct pci_controller *hose, *tmp;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500401 int next_busno = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100402
403 printk(KERN_INFO "PCI: Probing PCI hardware\n");
404
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +1100405 if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
406 pci_assign_all_buses = 1;
407
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100408 /* Scan all of the recorded PCI controllers. */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500409 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100410 if (pci_assign_all_buses)
411 hose->first_busno = next_busno;
412 hose->last_busno = 0xff;
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +0000413 pcibios_scan_phb(hose);
414 pci_bus_add_devices(hose->bus);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100415 if (pci_assign_all_buses || next_busno <= hose->last_busno)
416 next_busno = hose->last_busno + pcibios_assign_bus_offset;
417 }
418 pci_bus_count = next_busno;
419
420 /* OpenFirmware based machines need a map of OF bus
421 * numbers vs. kernel bus numbers since we may have to
422 * remap them.
423 */
Anton Vorontsov6b82b3e2008-12-09 09:47:29 +0000424 if (pci_assign_all_buses)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100425 pcibios_make_OF_bus_map();
426
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100427 /* Call common code to handle resource allocation */
428 pcibios_resource_survey();
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100429
430 /* Call machine dependent post-init code */
431 if (ppc_md.pcibios_after_init)
432 ppc_md.pcibios_after_init();
433
434 return 0;
435}
436
437subsys_initcall(pcibios_init);
438
Kumar Gala0b1d40c2007-06-27 10:27:33 -0500439static struct pci_controller*
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100440pci_bus_to_hose(int bus)
441{
Kumar Galaa4c9e322007-06-27 13:09:43 -0500442 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100443
Kumar Galaa4c9e322007-06-27 13:09:43 -0500444 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100445 if (bus >= hose->first_busno && bus <= hose->last_busno)
446 return hose;
447 return NULL;
448}
449
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100450/* Provide information on locations of various I/O regions in physical
451 * memory. Do this on a per-card basis so that we choose the right
452 * root bridge.
453 * Note that the returned IO or memory base is a physical address
454 */
455
456long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
457{
458 struct pci_controller* hose;
459 long result = -EOPNOTSUPP;
460
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100461 hose = pci_bus_to_hose(bus);
462 if (!hose)
463 return -ENODEV;
464
465 switch (which) {
466 case IOBASE_BRIDGE_NUMBER:
467 return (long)hose->first_busno;
468 case IOBASE_MEMORY:
469 return (long)hose->pci_mem_offset;
470 case IOBASE_IO:
471 return (long)hose->io_base_phys;
472 case IOBASE_ISA_IO:
473 return (long)isa_io_base;
474 case IOBASE_ISA_MEM:
475 return (long)isa_mem_base;
476 }
477
478 return result;
479}
480
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100481/*
482 * Null PCI config access functions, for the case when we can't
483 * find a hose.
484 */
485#define NULL_PCI_OP(rw, size, type) \
486static int \
487null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
488{ \
489 return PCIBIOS_DEVICE_NOT_FOUND; \
490}
491
492static int
493null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
494 int len, u32 *val)
495{
496 return PCIBIOS_DEVICE_NOT_FOUND;
497}
498
499static int
500null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
501 int len, u32 val)
502{
503 return PCIBIOS_DEVICE_NOT_FOUND;
504}
505
506static struct pci_ops null_pci_ops =
507{
Nathan Lynch6127d1c2007-08-10 05:18:42 +1000508 .read = null_read_config,
509 .write = null_write_config,
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100510};
511
512/*
513 * These functions are used early on before PCI scanning is done
514 * and all of the pci_dev and pci_bus structures have been created.
515 */
516static struct pci_bus *
517fake_pci_bus(struct pci_controller *hose, int busnr)
518{
519 static struct pci_bus bus;
520
521 if (hose == 0) {
522 hose = pci_bus_to_hose(busnr);
523 if (hose == 0)
524 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
525 }
526 bus.number = busnr;
527 bus.sysdata = hose;
528 bus.ops = hose? hose->ops: &null_pci_ops;
529 return &bus;
530}
531
532#define EARLY_PCI_OP(rw, size, type) \
533int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
534 int devfn, int offset, type value) \
535{ \
536 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
537 devfn, offset, value); \
538}
539
540EARLY_PCI_OP(read, byte, u8 *)
541EARLY_PCI_OP(read, word, u16 *)
542EARLY_PCI_OP(read, dword, u32 *)
543EARLY_PCI_OP(write, byte, u8)
544EARLY_PCI_OP(write, word, u16)
545EARLY_PCI_OP(write, dword, u32)
Kumar Gala38805e52007-07-10 23:37:45 -0500546
547extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
548int early_find_capability(struct pci_controller *hose, int bus, int devfn,
549 int cap)
550{
551 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
552}