blob: f8b50cbf4bf711658ef3ad742270da424cbf4294 [file] [log] [blame]
David S. Millera2bd4fd2006-06-23 01:44:10 -07001#include <linux/string.h>
2#include <linux/kernel.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +10003#include <linux/of.h>
David S. Millera2bd4fd2006-06-23 01:44:10 -07004#include <linux/init.h>
5#include <linux/module.h>
6#include <linux/mod_devicetable.h>
7#include <linux/slab.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +10008#include <linux/errno.h>
David S. Millerc1b1a5f2008-03-19 04:52:48 -07009#include <linux/irq.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100010#include <linux/of_device.h>
11#include <linux/of_platform.h>
David S. Millera2bd4fd2006-06-23 01:44:10 -070012
David S. Miller3ca9fab2006-06-29 14:35:33 -070013void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
14{
15 unsigned long ret = res->start + offset;
David S. Miller6bda5732006-10-18 23:00:35 -070016 struct resource *r;
David S. Miller3ca9fab2006-06-29 14:35:33 -070017
David S. Miller6bda5732006-10-18 23:00:35 -070018 if (res->flags & IORESOURCE_MEM)
19 r = request_mem_region(ret, size, name);
20 else
21 r = request_region(ret, size, name);
22 if (!r)
David S. Miller3ca9fab2006-06-29 14:35:33 -070023 ret = 0;
24
25 return (void __iomem *) ret;
26}
27EXPORT_SYMBOL(of_ioremap);
28
David S. Millere3a411a2006-12-28 21:01:32 -080029void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
David S. Miller3ca9fab2006-06-29 14:35:33 -070030{
David S. Millere3a411a2006-12-28 21:01:32 -080031 if (res->flags & IORESOURCE_MEM)
32 release_mem_region((unsigned long) base, size);
33 else
34 release_region((unsigned long) base, size);
David S. Miller3ca9fab2006-06-29 14:35:33 -070035}
36EXPORT_SYMBOL(of_iounmap);
37
David S. Miller2b1e5972006-06-29 15:07:37 -070038static int node_match(struct device *dev, void *data)
39{
40 struct of_device *op = to_of_device(dev);
41 struct device_node *dp = data;
42
43 return (op->node == dp);
44}
45
46struct of_device *of_find_device_by_node(struct device_node *dp)
47{
Stephen Rothwell37b77542007-04-30 17:43:56 +100048 struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
David S. Miller2b1e5972006-06-29 15:07:37 -070049 dp, node_match);
50
51 if (dev)
52 return to_of_device(dev);
53
54 return NULL;
55}
56EXPORT_SYMBOL(of_find_device_by_node);
57
David S. Millera2bd4fd2006-06-23 01:44:10 -070058#ifdef CONFIG_PCI
Stephen Rothwell3f23de12007-05-03 02:38:57 +100059struct bus_type ebus_bus_type;
David S. Miller69853912006-06-25 01:21:38 -070060EXPORT_SYMBOL(ebus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -070061#endif
62
63#ifdef CONFIG_SBUS
Stephen Rothwell3f23de12007-05-03 02:38:57 +100064struct bus_type sbus_bus_type;
David S. Miller69853912006-06-25 01:21:38 -070065EXPORT_SYMBOL(sbus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -070066#endif
67
Stephen Rothwell3f23de12007-05-03 02:38:57 +100068struct bus_type of_platform_bus_type;
Stephen Rothwell37b77542007-04-30 17:43:56 +100069EXPORT_SYMBOL(of_platform_bus_type);
David S. Millercf44bbc2006-06-29 14:34:50 -070070
David S. Millera83f9822006-07-12 23:19:31 -070071static inline u64 of_read_addr(const u32 *cell, int size)
David S. Millercf44bbc2006-06-29 14:34:50 -070072{
73 u64 r = 0;
74 while (size--)
75 r = (r << 32) | *(cell++);
76 return r;
77}
78
79static void __init get_cells(struct device_node *dp,
80 int *addrc, int *sizec)
81{
82 if (addrc)
83 *addrc = of_n_addr_cells(dp);
84 if (sizec)
85 *sizec = of_n_size_cells(dp);
86}
87
88/* Max address size we deal with */
89#define OF_MAX_ADDR_CELLS 4
90
91struct of_bus {
92 const char *name;
93 const char *addr_prop_name;
94 int (*match)(struct device_node *parent);
95 void (*count_cells)(struct device_node *child,
96 int *addrc, int *sizec);
David S. Millera83f9822006-07-12 23:19:31 -070097 int (*map)(u32 *addr, const u32 *range,
98 int na, int ns, int pna);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -070099 unsigned int (*get_flags)(const u32 *addr);
David S. Millercf44bbc2006-06-29 14:34:50 -0700100};
101
102/*
103 * Default translator (generic bus)
104 */
105
106static void of_bus_default_count_cells(struct device_node *dev,
107 int *addrc, int *sizec)
108{
109 get_cells(dev, addrc, sizec);
110}
111
David S. Millera83f9822006-07-12 23:19:31 -0700112/* Make sure the least significant 64-bits are in-range. Even
113 * for 3 or 4 cell values it is a good enough approximation.
114 */
115static int of_out_of_range(const u32 *addr, const u32 *base,
116 const u32 *size, int na, int ns)
David S. Millercf44bbc2006-06-29 14:34:50 -0700117{
118 u64 a = of_read_addr(addr, na);
David S. Millera83f9822006-07-12 23:19:31 -0700119 u64 b = of_read_addr(base, na);
120
121 if (a < b)
122 return 1;
123
124 b += of_read_addr(size, ns);
125 if (a >= b)
126 return 1;
127
128 return 0;
129}
130
131static int of_bus_default_map(u32 *addr, const u32 *range,
132 int na, int ns, int pna)
133{
134 u32 result[OF_MAX_ADDR_CELLS];
135 int i;
136
137 if (ns > 2) {
138 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
139 return -EINVAL;
140 }
141
142 if (of_out_of_range(addr, range, range + na + pna, na, ns))
143 return -EINVAL;
144
145 /* Start with the parent range base. */
146 memcpy(result, range + na, pna * 4);
147
148 /* Add in the child address offset. */
149 for (i = 0; i < na; i++)
150 result[pna - 1 - i] +=
151 (addr[na - 1 - i] -
152 range[na - 1 - i]);
153
154 memcpy(addr, result, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700155
156 return 0;
157}
158
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700159static unsigned int of_bus_default_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700160{
161 return IORESOURCE_MEM;
162}
163
David S. Millercf44bbc2006-06-29 14:34:50 -0700164/*
165 * PCI bus specific translator
166 */
167
168static int of_bus_pci_match(struct device_node *np)
169{
David S. Millera83f9822006-07-12 23:19:31 -0700170 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
David S. Millera165b422007-03-29 01:50:16 -0700171 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800172
173 if (model && !strcmp(model, "SUNW,simba"))
174 return 0;
175
David S. Millera83f9822006-07-12 23:19:31 -0700176 /* Do not do PCI specific frobbing if the
177 * PCI bridge lacks a ranges property. We
178 * want to pass it through up to the next
179 * parent as-is, not with the PCI translate
180 * method which chops off the top address cell.
181 */
182 if (!of_find_property(np, "ranges", NULL))
183 return 0;
184
185 return 1;
186 }
187
188 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700189}
190
David S. Miller01f94c42007-03-04 12:53:19 -0800191static int of_bus_simba_match(struct device_node *np)
192{
David S. Millera165b422007-03-29 01:50:16 -0700193 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800194
195 if (model && !strcmp(model, "SUNW,simba"))
196 return 1;
David S. Miller8c2786c2007-06-07 21:59:44 -0700197
198 /* Treat PCI busses lacking ranges property just like
199 * simba.
200 */
201 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
202 if (!of_find_property(np, "ranges", NULL))
203 return 1;
204 }
205
David S. Miller01f94c42007-03-04 12:53:19 -0800206 return 0;
207}
208
209static int of_bus_simba_map(u32 *addr, const u32 *range,
210 int na, int ns, int pna)
211{
212 return 0;
213}
214
David S. Millercf44bbc2006-06-29 14:34:50 -0700215static void of_bus_pci_count_cells(struct device_node *np,
216 int *addrc, int *sizec)
217{
218 if (addrc)
219 *addrc = 3;
220 if (sizec)
221 *sizec = 2;
222}
223
David S. Millera83f9822006-07-12 23:19:31 -0700224static int of_bus_pci_map(u32 *addr, const u32 *range,
225 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700226{
David S. Millera83f9822006-07-12 23:19:31 -0700227 u32 result[OF_MAX_ADDR_CELLS];
228 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700229
230 /* Check address type match */
231 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -0700232 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700233
David S. Millera83f9822006-07-12 23:19:31 -0700234 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
235 na - 1, ns))
236 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700237
David S. Millera83f9822006-07-12 23:19:31 -0700238 /* Start with the parent range base. */
239 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700240
David S. Millera83f9822006-07-12 23:19:31 -0700241 /* Add in the child address offset, skipping high cell. */
242 for (i = 0; i < na - 1; i++)
243 result[pna - 1 - i] +=
244 (addr[na - 1 - i] -
245 range[na - 1 - i]);
246
247 memcpy(addr, result, pna * 4);
248
249 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700250}
251
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700252static unsigned int of_bus_pci_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700253{
254 unsigned int flags = 0;
255 u32 w = addr[0];
256
257 switch((w >> 24) & 0x03) {
258 case 0x01:
259 flags |= IORESOURCE_IO;
260 case 0x02: /* 32 bits */
261 case 0x03: /* 64 bits */
262 flags |= IORESOURCE_MEM;
263 }
264 if (w & 0x40000000)
265 flags |= IORESOURCE_PREFETCH;
266 return flags;
267}
268
269/*
David S. Millercf44bbc2006-06-29 14:34:50 -0700270 * SBUS bus specific translator
271 */
272
273static int of_bus_sbus_match(struct device_node *np)
274{
275 return !strcmp(np->name, "sbus") ||
276 !strcmp(np->name, "sbi");
277}
278
279static void of_bus_sbus_count_cells(struct device_node *child,
280 int *addrc, int *sizec)
281{
282 if (addrc)
283 *addrc = 2;
284 if (sizec)
285 *sizec = 1;
286}
287
David S. Miller4130a4b2006-10-25 22:31:06 -0700288/*
289 * FHC/Central bus specific translator.
290 *
291 * This is just needed to hard-code the address and size cell
292 * counts. 'fhc' and 'central' nodes lack the #address-cells and
293 * #size-cells properties, and if you walk to the root on such
294 * Enterprise boxes all you'll get is a #size-cells of 2 which is
295 * not what we want to use.
296 */
297static int of_bus_fhc_match(struct device_node *np)
David S. Millercf44bbc2006-06-29 14:34:50 -0700298{
David S. Miller4130a4b2006-10-25 22:31:06 -0700299 return !strcmp(np->name, "fhc") ||
300 !strcmp(np->name, "central");
David S. Millercf44bbc2006-06-29 14:34:50 -0700301}
302
David S. Miller4130a4b2006-10-25 22:31:06 -0700303#define of_bus_fhc_count_cells of_bus_sbus_count_cells
David S. Millercf44bbc2006-06-29 14:34:50 -0700304
305/*
306 * Array of bus specific translators
307 */
308
309static struct of_bus of_busses[] = {
310 /* PCI */
311 {
312 .name = "pci",
313 .addr_prop_name = "assigned-addresses",
314 .match = of_bus_pci_match,
315 .count_cells = of_bus_pci_count_cells,
316 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700317 .get_flags = of_bus_pci_get_flags,
318 },
David S. Miller01f94c42007-03-04 12:53:19 -0800319 /* SIMBA */
320 {
321 .name = "simba",
322 .addr_prop_name = "assigned-addresses",
323 .match = of_bus_simba_match,
324 .count_cells = of_bus_pci_count_cells,
325 .map = of_bus_simba_map,
326 .get_flags = of_bus_pci_get_flags,
327 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700328 /* SBUS */
329 {
330 .name = "sbus",
331 .addr_prop_name = "reg",
332 .match = of_bus_sbus_match,
333 .count_cells = of_bus_sbus_count_cells,
David S. Miller4130a4b2006-10-25 22:31:06 -0700334 .map = of_bus_default_map,
335 .get_flags = of_bus_default_get_flags,
336 },
337 /* FHC */
338 {
339 .name = "fhc",
340 .addr_prop_name = "reg",
341 .match = of_bus_fhc_match,
342 .count_cells = of_bus_fhc_count_cells,
343 .map = of_bus_default_map,
344 .get_flags = of_bus_default_get_flags,
David S. Millercf44bbc2006-06-29 14:34:50 -0700345 },
346 /* Default */
347 {
348 .name = "default",
349 .addr_prop_name = "reg",
350 .match = NULL,
351 .count_cells = of_bus_default_count_cells,
352 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700353 .get_flags = of_bus_default_get_flags,
354 },
355};
356
357static struct of_bus *of_match_bus(struct device_node *np)
358{
359 int i;
360
361 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
362 if (!of_busses[i].match || of_busses[i].match(np))
363 return &of_busses[i];
364 BUG();
365 return NULL;
366}
367
368static int __init build_one_resource(struct device_node *parent,
369 struct of_bus *bus,
370 struct of_bus *pbus,
371 u32 *addr,
372 int na, int ns, int pna)
373{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700374 const u32 *ranges;
David S. Millercf44bbc2006-06-29 14:34:50 -0700375 unsigned int rlen;
376 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700377
378 ranges = of_get_property(parent, "ranges", &rlen);
379 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700380 u32 result[OF_MAX_ADDR_CELLS];
381 int i;
382
383 memset(result, 0, pna * 4);
384 for (i = 0; i < na; i++)
385 result[pna - 1 - i] =
386 addr[na - 1 - i];
387
388 memcpy(addr, result, pna * 4);
389 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700390 }
391
392 /* Now walk through the ranges */
393 rlen /= 4;
394 rone = na + pna + ns;
395 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700396 if (!bus->map(addr, ranges, na, ns, pna))
397 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700398 }
David S. Millera83f9822006-07-12 23:19:31 -0700399
David S. Miller49d23cf2007-05-13 22:01:18 -0700400 /* When we miss an I/O space match on PCI, just pass it up
401 * to the next PCI bridge and/or controller.
402 */
403 if (!strcmp(bus->name, "pci") &&
404 (addr[0] & 0x03000000) == 0x01000000)
405 return 0;
406
David S. Millera83f9822006-07-12 23:19:31 -0700407 return 1;
408}
409
410static int __init use_1to1_mapping(struct device_node *pp)
411{
David S. Millera83f9822006-07-12 23:19:31 -0700412 /* If we have a ranges property in the parent, use it. */
413 if (of_find_property(pp, "ranges", NULL) != NULL)
414 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700415
David S. Millera83f9822006-07-12 23:19:31 -0700416 /* If the parent is the dma node of an ISA bus, pass
417 * the translation up to the root.
418 */
419 if (!strcmp(pp->name, "dma"))
420 return 0;
421
David S. Miller8c2786c2007-06-07 21:59:44 -0700422 /* Similarly for all PCI bridges, if we get this far
423 * it lacks a ranges property, and this will include
424 * cases like Simba.
425 */
426 if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex"))
David S. Millera83f9822006-07-12 23:19:31 -0700427 return 0;
428
429 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700430}
431
David S. Millera83f9822006-07-12 23:19:31 -0700432static int of_resource_verbose;
433
David S. Millercf44bbc2006-06-29 14:34:50 -0700434static void __init build_device_resources(struct of_device *op,
435 struct device *parent)
436{
437 struct of_device *p_op;
438 struct of_bus *bus;
439 int na, ns;
440 int index, num_reg;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700441 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700442
443 if (!parent)
444 return;
445
446 p_op = to_of_device(parent);
447 bus = of_match_bus(p_op->node);
448 bus->count_cells(op->node, &na, &ns);
449
450 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
451 if (!preg || num_reg == 0)
452 return;
453
454 /* Convert to num-cells. */
455 num_reg /= 4;
456
David S. Miller46ba6d72006-07-16 22:10:44 -0700457 /* Convert to num-entries. */
David S. Millercf44bbc2006-06-29 14:34:50 -0700458 num_reg /= na + ns;
459
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700460 /* Prevent overrunning the op->resources[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700461 if (num_reg > PROMREG_MAX) {
462 printk(KERN_WARNING "%s: Too many regs (%d), "
463 "limiting to %d.\n",
464 op->node->full_name, num_reg, PROMREG_MAX);
465 num_reg = PROMREG_MAX;
466 }
467
David S. Millercf44bbc2006-06-29 14:34:50 -0700468 for (index = 0; index < num_reg; index++) {
469 struct resource *r = &op->resource[index];
470 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700471 const u32 *reg = (preg + (index * ((na + ns) * 4)));
David S. Millercf44bbc2006-06-29 14:34:50 -0700472 struct device_node *dp = op->node;
473 struct device_node *pp = p_op->node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800474 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700475 u64 size, result = OF_BAD_ADDR;
476 unsigned long flags;
477 int dna, dns;
478 int pna, pns;
479
480 size = of_read_addr(reg + na, ns);
481 flags = bus->get_flags(reg);
482
483 memcpy(addr, reg, na * 4);
484
David S. Millera83f9822006-07-12 23:19:31 -0700485 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700486 result = of_read_addr(addr, na);
487 goto build_res;
488 }
489
490 dna = na;
491 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800492 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700493
494 while (1) {
495 dp = pp;
496 pp = dp->parent;
497 if (!pp) {
498 result = of_read_addr(addr, dna);
499 break;
500 }
501
502 pbus = of_match_bus(pp);
503 pbus->count_cells(dp, &pna, &pns);
504
David S. Millerb85cdd42007-02-28 23:20:12 -0800505 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700506 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700507 break;
508
509 dna = pna;
510 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800511 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700512 }
513
514 build_res:
515 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700516
517 if (of_resource_verbose)
518 printk("%s reg[%d] -> %lx\n",
519 op->node->full_name, index,
520 result);
521
David S. Millercf44bbc2006-06-29 14:34:50 -0700522 if (result != OF_BAD_ADDR) {
David S. Miller1815aed2006-06-29 19:58:28 -0700523 if (tlb_type == hypervisor)
524 result &= 0x0fffffffffffffffUL;
525
David S. Millercf44bbc2006-06-29 14:34:50 -0700526 r->start = result;
527 r->end = result + size - 1;
528 r->flags = flags;
David S. Millercf44bbc2006-06-29 14:34:50 -0700529 }
530 r->name = op->node->name;
531 }
532}
533
David S. Miller2b1e5972006-06-29 15:07:37 -0700534static struct device_node * __init
535apply_interrupt_map(struct device_node *dp, struct device_node *pp,
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700536 const u32 *imap, int imlen, const u32 *imask,
David S. Miller2b1e5972006-06-29 15:07:37 -0700537 unsigned int *irq_p)
538{
539 struct device_node *cp;
540 unsigned int irq = *irq_p;
541 struct of_bus *bus;
542 phandle handle;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700543 const u32 *reg;
David S. Miller2b1e5972006-06-29 15:07:37 -0700544 int na, num_reg, i;
545
546 bus = of_match_bus(pp);
547 bus->count_cells(dp, &na, NULL);
548
549 reg = of_get_property(dp, "reg", &num_reg);
550 if (!reg || !num_reg)
551 return NULL;
552
553 imlen /= ((na + 3) * 4);
554 handle = 0;
555 for (i = 0; i < imlen; i++) {
556 int j;
557
558 for (j = 0; j < na; j++) {
559 if ((reg[j] & imask[j]) != imap[j])
560 goto next;
561 }
562 if (imap[na] == irq) {
563 handle = imap[na + 1];
564 irq = imap[na + 2];
565 break;
566 }
567
568 next:
569 imap += (na + 3);
570 }
David S. Miller46ba6d72006-07-16 22:10:44 -0700571 if (i == imlen) {
572 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
573 * properties that do not include the on-board device
574 * interrupts. Instead, the device's 'interrupts' property
575 * is already a fully specified INO value.
576 *
577 * Handle this by deciding that, if we didn't get a
578 * match in the parent's 'interrupt-map', and the
579 * parent is an IRQ translater, then use the parent as
580 * our IRQ controller.
581 */
582 if (pp->irq_trans)
583 return pp;
584
David S. Miller2b1e5972006-06-29 15:07:37 -0700585 return NULL;
David S. Miller46ba6d72006-07-16 22:10:44 -0700586 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700587
588 *irq_p = irq;
589 cp = of_find_node_by_phandle(handle);
590
591 return cp;
592}
593
594static unsigned int __init pci_irq_swizzle(struct device_node *dp,
595 struct device_node *pp,
596 unsigned int irq)
597{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700598 const struct linux_prom_pci_registers *regs;
David S. Millerbb4c18c2007-02-26 14:55:06 -0800599 unsigned int bus, devfn, slot, ret;
David S. Miller2b1e5972006-06-29 15:07:37 -0700600
601 if (irq < 1 || irq > 4)
602 return irq;
603
604 regs = of_get_property(dp, "reg", NULL);
605 if (!regs)
606 return irq;
607
David S. Millerbb4c18c2007-02-26 14:55:06 -0800608 bus = (regs->phys_hi >> 16) & 0xff;
David S. Miller2b1e5972006-06-29 15:07:37 -0700609 devfn = (regs->phys_hi >> 8) & 0xff;
610 slot = (devfn >> 3) & 0x1f;
611
David S. Millerbb4c18c2007-02-26 14:55:06 -0800612 if (pp->irq_trans) {
613 /* Derived from Table 8-3, U2P User's Manual. This branch
614 * is handling a PCI controller that lacks a proper set of
615 * interrupt-map and interrupt-map-mask properties. The
616 * Ultra-E450 is one example.
617 *
618 * The bit layout is BSSLL, where:
619 * B: 0 on bus A, 1 on bus B
620 * D: 2-bit slot number, derived from PCI device number as
621 * (dev - 1) for bus A, or (dev - 2) for bus B
622 * L: 2-bit line number
David S. Millerbb4c18c2007-02-26 14:55:06 -0800623 */
624 if (bus & 0x80) {
625 /* PBM-A */
626 bus = 0x00;
627 slot = (slot - 1) << 2;
628 } else {
629 /* PBM-B */
630 bus = 0x10;
631 slot = (slot - 2) << 2;
632 }
633 irq -= 1;
634
635 ret = (bus | slot | irq);
636 } else {
637 /* Going through a PCI-PCI bridge that lacks a set of
638 * interrupt-map and interrupt-map-mask properties.
639 */
640 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
641 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700642
643 return ret;
644}
645
David S. Millera83f9822006-07-12 23:19:31 -0700646static int of_irq_verbose;
647
David S. Miller2b1e5972006-06-29 15:07:37 -0700648static unsigned int __init build_one_device_irq(struct of_device *op,
649 struct device *parent,
650 unsigned int irq)
651{
652 struct device_node *dp = op->node;
653 struct device_node *pp, *ip;
654 unsigned int orig_irq = irq;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700655 int nid;
David S. Miller2b1e5972006-06-29 15:07:37 -0700656
657 if (irq == 0xffffffff)
658 return irq;
659
660 if (dp->irq_trans) {
661 irq = dp->irq_trans->irq_build(dp, irq,
662 dp->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700663
664 if (of_irq_verbose)
665 printk("%s: direct translate %x --> %x\n",
666 dp->full_name, orig_irq, irq);
667
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700668 goto out;
David S. Miller2b1e5972006-06-29 15:07:37 -0700669 }
670
671 /* Something more complicated. Walk up to the root, applying
672 * interrupt-map or bus specific translations, until we hit
673 * an IRQ translator.
674 *
675 * If we hit a bus type or situation we cannot handle, we
676 * stop and assume that the original IRQ number was in a
677 * format which has special meaning to it's immediate parent.
678 */
679 pp = dp->parent;
680 ip = NULL;
681 while (pp) {
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700682 const void *imap, *imsk;
David S. Miller2b1e5972006-06-29 15:07:37 -0700683 int imlen;
684
685 imap = of_get_property(pp, "interrupt-map", &imlen);
686 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
687 if (imap && imsk) {
688 struct device_node *iret;
689 int this_orig_irq = irq;
690
691 iret = apply_interrupt_map(dp, pp,
692 imap, imlen, imsk,
693 &irq);
David S. Millera83f9822006-07-12 23:19:31 -0700694
695 if (of_irq_verbose)
696 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
697 op->node->full_name,
698 pp->full_name, this_orig_irq,
699 (iret ? iret->full_name : "NULL"), irq);
700
David S. Miller2b1e5972006-06-29 15:07:37 -0700701 if (!iret)
702 break;
703
704 if (iret->irq_trans) {
705 ip = iret;
706 break;
707 }
708 } else {
709 if (!strcmp(pp->type, "pci") ||
710 !strcmp(pp->type, "pciex")) {
711 unsigned int this_orig_irq = irq;
712
713 irq = pci_irq_swizzle(dp, pp, irq);
David S. Millera83f9822006-07-12 23:19:31 -0700714 if (of_irq_verbose)
715 printk("%s: PCI swizzle [%s] "
716 "%x --> %x\n",
717 op->node->full_name,
718 pp->full_name, this_orig_irq,
719 irq);
720
David S. Miller2b1e5972006-06-29 15:07:37 -0700721 }
722
723 if (pp->irq_trans) {
724 ip = pp;
725 break;
726 }
727 }
728 dp = pp;
729 pp = pp->parent;
730 }
731 if (!ip)
732 return orig_irq;
733
734 irq = ip->irq_trans->irq_build(op->node, irq,
735 ip->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700736 if (of_irq_verbose)
737 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
738 op->node->full_name, ip->full_name, orig_irq, irq);
David S. Miller2b1e5972006-06-29 15:07:37 -0700739
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700740out:
741 nid = of_node_to_nid(dp);
742 if (nid != -1) {
743 cpumask_t numa_mask = node_to_cpumask(nid);
744
745 irq_set_affinity(irq, numa_mask);
746 }
747
David S. Miller2b1e5972006-06-29 15:07:37 -0700748 return irq;
749}
750
David S. Millercf44bbc2006-06-29 14:34:50 -0700751static struct of_device * __init scan_one_device(struct device_node *dp,
752 struct device *parent)
753{
754 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700755 const unsigned int *irq;
David S. Miller3d6e4702007-07-18 22:03:25 -0700756 struct dev_archdata *sd;
David S. Miller2b1e5972006-06-29 15:07:37 -0700757 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700758
759 if (!op)
760 return NULL;
761
David S. Miller3d6e4702007-07-18 22:03:25 -0700762 sd = &op->dev.archdata;
763 sd->prom_node = dp;
764 sd->op = op;
765
David S. Millercf44bbc2006-06-29 14:34:50 -0700766 op->node = dp;
767
768 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
769 (25*1000*1000));
770 op->portid = of_getintprop_default(dp, "upa-portid", -1);
771 if (op->portid == -1)
772 op->portid = of_getintprop_default(dp, "portid", -1);
773
774 irq = of_get_property(dp, "interrupts", &len);
David S. Miller2b1e5972006-06-29 15:07:37 -0700775 if (irq) {
776 memcpy(op->irqs, irq, len);
777 op->num_irqs = len / 4;
778 } else {
779 op->num_irqs = 0;
780 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700781
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700782 /* Prevent overrunning the op->irqs[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700783 if (op->num_irqs > PROMINTR_MAX) {
784 printk(KERN_WARNING "%s: Too many irqs (%d), "
785 "limiting to %d.\n",
786 dp->full_name, op->num_irqs, PROMINTR_MAX);
787 op->num_irqs = PROMINTR_MAX;
788 }
789
David S. Millercf44bbc2006-06-29 14:34:50 -0700790 build_device_resources(op, parent);
David S. Miller2b1e5972006-06-29 15:07:37 -0700791 for (i = 0; i < op->num_irqs; i++)
792 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
David S. Millercf44bbc2006-06-29 14:34:50 -0700793
794 op->dev.parent = parent;
Stephen Rothwell37b77542007-04-30 17:43:56 +1000795 op->dev.bus = &of_platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700796 if (!parent)
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200797 dev_set_name(&op->dev, "root");
David S. Millercf44bbc2006-06-29 14:34:50 -0700798 else
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200799 dev_set_name(&op->dev, "%08x", dp->node);
David S. Millercf44bbc2006-06-29 14:34:50 -0700800
801 if (of_device_register(op)) {
802 printk("%s: Could not register of device.\n",
803 dp->full_name);
804 kfree(op);
805 op = NULL;
806 }
807
808 return op;
809}
810
811static void __init scan_tree(struct device_node *dp, struct device *parent)
812{
813 while (dp) {
814 struct of_device *op = scan_one_device(dp, parent);
815
816 if (op)
817 scan_tree(dp->child, &op->dev);
818
819 dp = dp->sibling;
820 }
821}
822
823static void __init scan_of_devices(void)
824{
825 struct device_node *root = of_find_node_by_path("/");
826 struct of_device *parent;
827
828 parent = scan_one_device(root, NULL);
829 if (!parent)
830 return;
831
832 scan_tree(root->child, &parent->dev);
833}
834
David S. Millera2bd4fd2006-06-23 01:44:10 -0700835static int __init of_bus_driver_init(void)
836{
David S. Millercf44bbc2006-06-29 14:34:50 -0700837 int err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700838
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000839 err = of_bus_type_init(&of_platform_bus_type, "of");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700840#ifdef CONFIG_PCI
841 if (!err)
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000842 err = of_bus_type_init(&ebus_bus_type, "ebus");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700843#endif
844#ifdef CONFIG_SBUS
845 if (!err)
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000846 err = of_bus_type_init(&sbus_bus_type, "sbus");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700847#endif
David S. Millercf44bbc2006-06-29 14:34:50 -0700848
849 if (!err)
850 scan_of_devices();
851
852 return err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700853}
854
855postcore_initcall(of_bus_driver_init);
856
David S. Millera83f9822006-07-12 23:19:31 -0700857static int __init of_debug(char *str)
858{
859 int val = 0;
860
861 get_option(&str, &val);
862 if (val & 1)
863 of_resource_verbose = 1;
864 if (val & 2)
865 of_irq_verbose = 1;
866 return 1;
867}
868
869__setup("of_debug=", of_debug);