blob: a848c6360bd117476aec3cd238f252bfe232426e [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>
23#include <asm/byteorder.h>
Paul Mackerrase05b3b42006-01-15 22:05:47 +110024#include <asm/uaccess.h>
25#include <asm/machdep.h>
26
27#undef DEBUG
28
29#ifdef DEBUG
30#define DBG(x...) printk(x)
31#else
32#define DBG(x...)
33#endif
34
35unsigned long isa_io_base = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +110036unsigned long pci_dram_offset = 0;
37int pcibios_assign_bus_offset = 1;
38
39void pcibios_make_OF_bus_map(void);
40
Paul Mackerrase05b3b42006-01-15 22:05:47 +110041static void fixup_broken_pcnet32(struct pci_dev* dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +110042static void fixup_cpc710_pci64(struct pci_dev* dev);
43#ifdef CONFIG_PPC_OF
44static u8* pci_to_OF_bus_map;
45#endif
46
47/* By default, we don't re-assign bus numbers. We do this only on
48 * some pmacs
49 */
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +110050static int pci_assign_all_buses;
Paul Mackerrase05b3b42006-01-15 22:05:47 +110051
Kumar Galaa4c9e322007-06-27 13:09:43 -050052LIST_HEAD(hose_list);
Paul Mackerrase05b3b42006-01-15 22:05:47 +110053
54static int pci_bus_count;
55
56static void
John Rigby4a015c32008-10-07 13:00:20 -060057fixup_hide_host_resource_fsl(struct pci_dev *dev)
Kumar Gala2052d6d2007-07-25 00:44:11 -050058{
59 int i, class = dev->class >> 8;
60
John Rigby4a015c32008-10-07 13:00:20 -060061 if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
62 class == PCI_CLASS_BRIDGE_OTHER) &&
Kumar Gala2052d6d2007-07-25 00:44:11 -050063 (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;
69 }
70 }
71}
72DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
73DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
74
75static void
Paul Mackerrase05b3b42006-01-15 22:05:47 +110076fixup_broken_pcnet32(struct pci_dev* dev)
77{
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);
81 }
82}
83DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
84
85static void
86fixup_cpc710_pci64(struct pci_dev* dev)
87{
88 /* Hide the PCI64 BARs from the kernel as their content doesn't
89 * fit well in the resource management
90 */
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;
95}
96DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
97
Paul Mackerrase05b3b42006-01-15 22:05:47 +110098#ifdef CONFIG_PPC_OF
99/*
100 * Functions below are used on OpenFirmware machines.
101 */
102static void
103make_one_node_map(struct device_node* node, u8 pci_bus)
104{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000105 const int *bus_range;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100106 int len;
107
108 if (pci_bus >= pci_bus_count)
109 return;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000110 bus_range = of_get_property(node, "bus-range", &len);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100111 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;
115 } else
116 pci_to_OF_bus_map[pci_bus] = bus_range[0];
117
Stephen Rothwell66524b22007-12-21 15:52:43 +1100118 for_each_child_of_node(node, node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100119 struct pci_dev* dev;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000120 const unsigned int *class_code, *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100121
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000122 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100123 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
124 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
125 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000126 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100127 if (!reg)
128 continue;
Alan Coxab462762007-04-23 14:47:59 +0100129 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
130 if (!dev || !dev->subordinate) {
131 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100132 continue;
Alan Coxab462762007-04-23 14:47:59 +0100133 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100134 make_one_node_map(node, dev->subordinate->number);
Alan Coxab462762007-04-23 14:47:59 +0100135 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100136 }
137}
138
139void
140pcibios_make_OF_bus_map(void)
141{
142 int i;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500143 struct pci_controller *hose, *tmp;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000144 struct property *map_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000145 struct device_node *dn;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100146
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800147 pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100148 if (!pci_to_OF_bus_map) {
149 printk(KERN_ERR "Can't allocate OF bus map !\n");
150 return;
151 }
152
153 /* We fill the bus map with invalid values, that helps
154 * debugging.
155 */
156 for (i=0; i<pci_bus_count; i++)
157 pci_to_OF_bus_map[i] = 0xff;
158
159 /* For each hose, we begin searching bridges */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500160 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100161 struct device_node* node = hose->dn;
162
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100163 if (!node)
164 continue;
165 make_one_node_map(node, hose->first_busno);
166 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000167 dn = of_find_node_by_path("/");
168 map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000169 if (map_prop) {
170 BUG_ON(pci_bus_count > map_prop->length);
171 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
172 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000173 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100174#ifdef DEBUG
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)
178 continue;
179 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
180 }
181#endif
182}
183
184typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
185
186static struct device_node*
Stephen Rothwell66524b22007-12-21 15:52:43 +1100187scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100188{
Stephen Rothwell66524b22007-12-21 15:52:43 +1100189 struct device_node *node;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100190 struct device_node* sub_node;
191
Stephen Rothwell66524b22007-12-21 15:52:43 +1100192 for_each_child_of_node(parent, node) {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000193 const unsigned int *class_code;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100194
Stephen Rothwell66524b22007-12-21 15:52:43 +1100195 if (filter(node, data)) {
196 of_node_put(node);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100197 return node;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100198 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100199
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.
204 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000205 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100206 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
207 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
208 strcmp(node->name, "multifunc-device"))
209 continue;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100210 sub_node = scan_OF_pci_childs(node, filter, data);
211 if (sub_node) {
212 of_node_put(node);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100213 return sub_node;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100214 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100215 }
216 return NULL;
217}
218
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100219static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
220 unsigned int devfn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100221{
Stephen Rothwell85e99b92008-01-03 15:13:37 +1100222 struct device_node *np;
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100223 const u32 *reg;
224 unsigned int psize;
225
Stephen Rothwell85e99b92008-01-03 15:13:37 +1100226 for_each_child_of_node(parent, np) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000227 reg = of_get_property(np, "reg", &psize);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100228 if (reg == NULL || psize < 4)
229 continue;
230 if (((reg[0] >> 8) & 0xff) == devfn)
231 return np;
232 }
233 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100234}
235
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100236
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100237static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
238{
239 struct device_node *parent, *np;
240
241 /* Are we a root bus ? */
242 if (bus->self == NULL || bus->parent == NULL) {
Kumar Gala0b1d40c2007-06-27 10:27:33 -0500243 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100244 if (hose == NULL)
245 return NULL;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100246 return of_node_get(hose->dn);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100247 }
248
249 /* not a root bus, we need to get our parent */
250 parent = scan_OF_for_pci_bus(bus->parent);
251 if (parent == NULL)
252 return NULL;
253
254 /* now iterate for children for a match */
255 np = scan_OF_for_pci_dev(parent, bus->self->devfn);
256 of_node_put(parent);
257
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100258 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100259}
260
261/*
262 * Scans the OF tree for a device node matching a PCI device
263 */
264struct device_node *
265pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
266{
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100267 struct device_node *parent, *np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100268
269 if (!have_of)
270 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100271
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100272 DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
273 parent = scan_OF_for_pci_bus(bus);
274 if (parent == NULL)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100275 return NULL;
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100276 DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
277 np = scan_OF_for_pci_dev(parent, devfn);
278 of_node_put(parent);
279 DBG(" result is %s\n", np ? np->full_name : "<NULL>");
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100280
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100281 /* XXX most callers don't release the returned node
282 * mostly because ppc64 doesn't increase the refcount,
283 * we need to fix that.
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100284 */
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100285 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100286}
287EXPORT_SYMBOL(pci_busdev_to_OF_node);
288
289struct device_node*
290pci_device_to_OF_node(struct pci_dev *dev)
291{
292 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
293}
294EXPORT_SYMBOL(pci_device_to_OF_node);
295
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100296static int
297find_OF_pci_device_filter(struct device_node* node, void* data)
298{
299 return ((void *)node == data);
300}
301
302/*
303 * Returns the PCI device matching a given OF node
304 */
305int
306pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
307{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000308 const unsigned int *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100309 struct pci_controller* hose;
310 struct pci_dev* dev = NULL;
311
312 if (!have_of)
313 return -ENODEV;
314 /* Make sure it's really a PCI device */
315 hose = pci_find_hose_for_OF_device(node);
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100316 if (!hose || !hose->dn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100317 return -ENODEV;
Stephen Rothwell66524b22007-12-21 15:52:43 +1100318 if (!scan_OF_pci_childs(hose->dn,
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100319 find_OF_pci_device_filter, (void *)node))
320 return -ENODEV;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000321 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100322 if (!reg)
323 return -ENODEV;
324 *bus = (reg[0] >> 16) & 0xff;
325 *devfn = ((reg[0] >> 8) & 0xff);
326
327 /* Ok, here we need some tweak. If we have already renumbered
328 * all busses, we can't rely on the OF bus number any more.
329 * the pci_to_OF_bus_map is not enough as several PCI busses
330 * may match the same OF bus number.
331 */
332 if (!pci_to_OF_bus_map)
333 return 0;
334
335 for_each_pci_dev(dev)
336 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
337 dev->devfn == *devfn) {
338 *bus = dev->bus->number;
339 pci_dev_put(dev);
340 return 0;
341 }
342
343 return -ENODEV;
344}
345EXPORT_SYMBOL(pci_device_from_OF_node);
346
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100347/* We create the "pci-OF-bus-map" property now so it appears in the
348 * /proc device tree
349 */
350void __init
351pci_create_OF_bus_map(void)
352{
353 struct property* of_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000354 struct device_node *dn;
355
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100356 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000357 if (!of_prop)
358 return;
359 dn = of_find_node_by_path("/");
360 if (dn) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100361 memset(of_prop, -1, sizeof(struct property) + 256);
362 of_prop->name = "pci-OF-bus-map";
363 of_prop->length = 256;
Stephen Rothwell1a381472007-04-03 10:58:52 +1000364 of_prop->value = &of_prop[1];
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000365 prom_add_property(dn, of_prop);
366 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100367 }
368}
369
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100370#else /* CONFIG_PPC_OF */
371void pcibios_make_OF_bus_map(void)
372{
373}
374#endif /* CONFIG_PPC_OF */
375
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100376static int __init pcibios_init(void)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100377{
Kumar Galaa4c9e322007-06-27 13:09:43 -0500378 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100379 struct pci_bus *bus;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500380 int next_busno = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100381
382 printk(KERN_INFO "PCI: Probing PCI hardware\n");
383
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +1100384 if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
385 pci_assign_all_buses = 1;
386
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100387 /* Scan all of the recorded PCI controllers. */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500388 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100389 if (pci_assign_all_buses)
390 hose->first_busno = next_busno;
391 hose->last_busno = 0xff;
Benjamin Herrenschmidt803d4572006-11-11 17:25:07 +1100392 bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
393 hose->ops, hose);
Benjamin Herrenschmidtb1b166b2007-12-21 14:53:27 +1100394 if (bus) {
Benjamin Herrenschmidt803d4572006-11-11 17:25:07 +1100395 pci_bus_add_devices(bus);
Benjamin Herrenschmidtb1b166b2007-12-21 14:53:27 +1100396 hose->last_busno = bus->subordinate;
397 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100398 if (pci_assign_all_buses || next_busno <= hose->last_busno)
399 next_busno = hose->last_busno + pcibios_assign_bus_offset;
400 }
401 pci_bus_count = next_busno;
402
403 /* OpenFirmware based machines need a map of OF bus
404 * numbers vs. kernel bus numbers since we may have to
405 * remap them.
406 */
407 if (pci_assign_all_buses && have_of)
408 pcibios_make_OF_bus_map();
409
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +1100410 /* Call common code to handle resource allocation */
411 pcibios_resource_survey();
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100412
413 /* Call machine dependent post-init code */
414 if (ppc_md.pcibios_after_init)
415 ppc_md.pcibios_after_init();
416
417 return 0;
418}
419
420subsys_initcall(pcibios_init);
421
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100422void __devinit pcibios_do_bus_setup(struct pci_bus *bus)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100423{
424 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
425 unsigned long io_offset;
426 struct resource *res;
427 int i;
Becky Bruce4fc665b2008-09-12 10:34:46 +0000428 struct pci_dev *dev;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100429
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100430 /* Hookup PHB resources */
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100431 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
432 if (bus->parent == NULL) {
433 /* This is a host bridge - fill in its resources */
434 hose->bus = bus;
435
436 bus->resource[0] = res = &hose->io_resource;
437 if (!res->flags) {
438 if (io_offset)
439 printk(KERN_ERR "I/O resource not set for host"
Kumar Gala5516b542007-06-27 01:17:57 -0500440 " bridge %d\n", hose->global_number);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100441 res->start = 0;
442 res->end = IO_SPACE_LIMIT;
443 res->flags = IORESOURCE_IO;
444 }
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100445 res->start = (res->start + io_offset) & 0xffffffffu;
446 res->end = (res->end + io_offset) & 0xffffffffu;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100447
448 for (i = 0; i < 3; ++i) {
449 res = &hose->mem_resources[i];
450 if (!res->flags) {
451 if (i > 0)
452 continue;
453 printk(KERN_ERR "Memory resource not set for "
Kumar Gala5516b542007-06-27 01:17:57 -0500454 "host bridge %d\n", hose->global_number);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100455 res->start = hose->pci_mem_offset;
456 res->end = ~0U;
457 res->flags = IORESOURCE_MEM;
458 }
459 bus->resource[i+1] = res;
460 }
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +1100461 }
Becky Bruce4fc665b2008-09-12 10:34:46 +0000462
463 if (ppc_md.pci_dma_bus_setup)
464 ppc_md.pci_dma_bus_setup(bus);
465
466 list_for_each_entry(dev, &bus->devices, bus_list)
467 pcibios_setup_new_device(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100468}
469
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100470/* the next one is stolen from the alpha port... */
471void __init
472pcibios_update_irq(struct pci_dev *dev, int irq)
473{
474 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
475 /* XXX FIXME - update OF device tree node interrupt property */
476}
477
Kumar Gala0b1d40c2007-06-27 10:27:33 -0500478static struct pci_controller*
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100479pci_bus_to_hose(int bus)
480{
Kumar Galaa4c9e322007-06-27 13:09:43 -0500481 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100482
Kumar Galaa4c9e322007-06-27 13:09:43 -0500483 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100484 if (bus >= hose->first_busno && bus <= hose->last_busno)
485 return hose;
486 return NULL;
487}
488
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100489/* Provide information on locations of various I/O regions in physical
490 * memory. Do this on a per-card basis so that we choose the right
491 * root bridge.
492 * Note that the returned IO or memory base is a physical address
493 */
494
495long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
496{
497 struct pci_controller* hose;
498 long result = -EOPNOTSUPP;
499
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100500 hose = pci_bus_to_hose(bus);
501 if (!hose)
502 return -ENODEV;
503
504 switch (which) {
505 case IOBASE_BRIDGE_NUMBER:
506 return (long)hose->first_busno;
507 case IOBASE_MEMORY:
508 return (long)hose->pci_mem_offset;
509 case IOBASE_IO:
510 return (long)hose->io_base_phys;
511 case IOBASE_ISA_IO:
512 return (long)isa_io_base;
513 case IOBASE_ISA_MEM:
514 return (long)isa_mem_base;
515 }
516
517 return result;
518}
519
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100520unsigned long pci_address_to_pio(phys_addr_t address)
521{
Kumar Galaa4c9e322007-06-27 13:09:43 -0500522 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100523
Kumar Galaa4c9e322007-06-27 13:09:43 -0500524 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100525 unsigned int size = hose->io_resource.end -
526 hose->io_resource.start + 1;
527 if (address >= hose->io_base_phys &&
528 address < (hose->io_base_phys + size)) {
529 unsigned long base =
530 (unsigned long)hose->io_base_virt - _IO_BASE;
531 return base + (address - hose->io_base_phys);
532 }
533 }
534 return (unsigned int)-1;
535}
536EXPORT_SYMBOL(pci_address_to_pio);
537
538/*
539 * Null PCI config access functions, for the case when we can't
540 * find a hose.
541 */
542#define NULL_PCI_OP(rw, size, type) \
543static int \
544null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
545{ \
546 return PCIBIOS_DEVICE_NOT_FOUND; \
547}
548
549static int
550null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
551 int len, u32 *val)
552{
553 return PCIBIOS_DEVICE_NOT_FOUND;
554}
555
556static int
557null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
558 int len, u32 val)
559{
560 return PCIBIOS_DEVICE_NOT_FOUND;
561}
562
563static struct pci_ops null_pci_ops =
564{
Nathan Lynch6127d1c2007-08-10 05:18:42 +1000565 .read = null_read_config,
566 .write = null_write_config,
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100567};
568
569/*
570 * These functions are used early on before PCI scanning is done
571 * and all of the pci_dev and pci_bus structures have been created.
572 */
573static struct pci_bus *
574fake_pci_bus(struct pci_controller *hose, int busnr)
575{
576 static struct pci_bus bus;
577
578 if (hose == 0) {
579 hose = pci_bus_to_hose(busnr);
580 if (hose == 0)
581 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
582 }
583 bus.number = busnr;
584 bus.sysdata = hose;
585 bus.ops = hose? hose->ops: &null_pci_ops;
586 return &bus;
587}
588
589#define EARLY_PCI_OP(rw, size, type) \
590int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
591 int devfn, int offset, type value) \
592{ \
593 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
594 devfn, offset, value); \
595}
596
597EARLY_PCI_OP(read, byte, u8 *)
598EARLY_PCI_OP(read, word, u16 *)
599EARLY_PCI_OP(read, dword, u32 *)
600EARLY_PCI_OP(write, byte, u8)
601EARLY_PCI_OP(write, word, u16)
602EARLY_PCI_OP(write, dword, u32)
Kumar Gala38805e52007-07-10 23:37:45 -0500603
604extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
605int early_find_capability(struct pci_controller *hose, int bus, int devfn,
606 int cap)
607{
608 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
609}