blob: 8a0d82343a257b6783b6bd718ee745d3d95815cf [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. Miller44266212008-08-20 16:34:39 -070058int irq_of_parse_and_map(struct device_node *node, int index)
59{
60 struct of_device *op = of_find_device_by_node(node);
61
62 if (!op || index >= op->num_irqs)
63 return 0xffffffff;
64
65 return op->irqs[index];
66}
67EXPORT_SYMBOL(irq_of_parse_and_map);
68
David S. Millera2bd4fd2006-06-23 01:44:10 -070069#ifdef CONFIG_PCI
Stephen Rothwell3f23de12007-05-03 02:38:57 +100070struct bus_type ebus_bus_type;
David S. Miller69853912006-06-25 01:21:38 -070071EXPORT_SYMBOL(ebus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -070072#endif
73
74#ifdef CONFIG_SBUS
Stephen Rothwell3f23de12007-05-03 02:38:57 +100075struct bus_type sbus_bus_type;
David S. Miller69853912006-06-25 01:21:38 -070076EXPORT_SYMBOL(sbus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -070077#endif
78
Stephen Rothwell3f23de12007-05-03 02:38:57 +100079struct bus_type of_platform_bus_type;
Stephen Rothwell37b77542007-04-30 17:43:56 +100080EXPORT_SYMBOL(of_platform_bus_type);
David S. Millercf44bbc2006-06-29 14:34:50 -070081
David S. Millera83f9822006-07-12 23:19:31 -070082static inline u64 of_read_addr(const u32 *cell, int size)
David S. Millercf44bbc2006-06-29 14:34:50 -070083{
84 u64 r = 0;
85 while (size--)
86 r = (r << 32) | *(cell++);
87 return r;
88}
89
90static void __init get_cells(struct device_node *dp,
91 int *addrc, int *sizec)
92{
93 if (addrc)
94 *addrc = of_n_addr_cells(dp);
95 if (sizec)
96 *sizec = of_n_size_cells(dp);
97}
98
99/* Max address size we deal with */
100#define OF_MAX_ADDR_CELLS 4
101
102struct of_bus {
103 const char *name;
104 const char *addr_prop_name;
105 int (*match)(struct device_node *parent);
106 void (*count_cells)(struct device_node *child,
107 int *addrc, int *sizec);
David S. Millera83f9822006-07-12 23:19:31 -0700108 int (*map)(u32 *addr, const u32 *range,
109 int na, int ns, int pna);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700110 unsigned int (*get_flags)(const u32 *addr);
David S. Millercf44bbc2006-06-29 14:34:50 -0700111};
112
113/*
114 * Default translator (generic bus)
115 */
116
117static void of_bus_default_count_cells(struct device_node *dev,
118 int *addrc, int *sizec)
119{
120 get_cells(dev, addrc, sizec);
121}
122
David S. Millera83f9822006-07-12 23:19:31 -0700123/* Make sure the least significant 64-bits are in-range. Even
124 * for 3 or 4 cell values it is a good enough approximation.
125 */
126static int of_out_of_range(const u32 *addr, const u32 *base,
127 const u32 *size, int na, int ns)
David S. Millercf44bbc2006-06-29 14:34:50 -0700128{
129 u64 a = of_read_addr(addr, na);
David S. Millera83f9822006-07-12 23:19:31 -0700130 u64 b = of_read_addr(base, na);
131
132 if (a < b)
133 return 1;
134
135 b += of_read_addr(size, ns);
136 if (a >= b)
137 return 1;
138
139 return 0;
140}
141
142static int of_bus_default_map(u32 *addr, const u32 *range,
143 int na, int ns, int pna)
144{
145 u32 result[OF_MAX_ADDR_CELLS];
146 int i;
147
148 if (ns > 2) {
149 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
150 return -EINVAL;
151 }
152
153 if (of_out_of_range(addr, range, range + na + pna, na, ns))
154 return -EINVAL;
155
156 /* Start with the parent range base. */
157 memcpy(result, range + na, pna * 4);
158
159 /* Add in the child address offset. */
160 for (i = 0; i < na; i++)
161 result[pna - 1 - i] +=
162 (addr[na - 1 - i] -
163 range[na - 1 - i]);
164
165 memcpy(addr, result, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700166
167 return 0;
168}
169
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700170static unsigned int of_bus_default_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700171{
172 return IORESOURCE_MEM;
173}
174
David S. Millercf44bbc2006-06-29 14:34:50 -0700175/*
176 * PCI bus specific translator
177 */
178
179static int of_bus_pci_match(struct device_node *np)
180{
David S. Millera83f9822006-07-12 23:19:31 -0700181 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
David S. Millera165b422007-03-29 01:50:16 -0700182 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800183
184 if (model && !strcmp(model, "SUNW,simba"))
185 return 0;
186
David S. Millera83f9822006-07-12 23:19:31 -0700187 /* Do not do PCI specific frobbing if the
188 * PCI bridge lacks a ranges property. We
189 * want to pass it through up to the next
190 * parent as-is, not with the PCI translate
191 * method which chops off the top address cell.
192 */
193 if (!of_find_property(np, "ranges", NULL))
194 return 0;
195
196 return 1;
197 }
198
199 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700200}
201
David S. Miller01f94c42007-03-04 12:53:19 -0800202static int of_bus_simba_match(struct device_node *np)
203{
David S. Millera165b422007-03-29 01:50:16 -0700204 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800205
206 if (model && !strcmp(model, "SUNW,simba"))
207 return 1;
David S. Miller8c2786c2007-06-07 21:59:44 -0700208
209 /* Treat PCI busses lacking ranges property just like
210 * simba.
211 */
212 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
213 if (!of_find_property(np, "ranges", NULL))
214 return 1;
215 }
216
David S. Miller01f94c42007-03-04 12:53:19 -0800217 return 0;
218}
219
220static int of_bus_simba_map(u32 *addr, const u32 *range,
221 int na, int ns, int pna)
222{
223 return 0;
224}
225
David S. Millercf44bbc2006-06-29 14:34:50 -0700226static void of_bus_pci_count_cells(struct device_node *np,
227 int *addrc, int *sizec)
228{
229 if (addrc)
230 *addrc = 3;
231 if (sizec)
232 *sizec = 2;
233}
234
David S. Millera83f9822006-07-12 23:19:31 -0700235static int of_bus_pci_map(u32 *addr, const u32 *range,
236 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700237{
David S. Millera83f9822006-07-12 23:19:31 -0700238 u32 result[OF_MAX_ADDR_CELLS];
239 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700240
241 /* Check address type match */
242 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -0700243 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700244
David S. Millera83f9822006-07-12 23:19:31 -0700245 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
246 na - 1, ns))
247 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700248
David S. Millera83f9822006-07-12 23:19:31 -0700249 /* Start with the parent range base. */
250 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700251
David S. Millera83f9822006-07-12 23:19:31 -0700252 /* Add in the child address offset, skipping high cell. */
253 for (i = 0; i < na - 1; i++)
254 result[pna - 1 - i] +=
255 (addr[na - 1 - i] -
256 range[na - 1 - i]);
257
258 memcpy(addr, result, pna * 4);
259
260 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700261}
262
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700263static unsigned int of_bus_pci_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700264{
265 unsigned int flags = 0;
266 u32 w = addr[0];
267
268 switch((w >> 24) & 0x03) {
269 case 0x01:
270 flags |= IORESOURCE_IO;
271 case 0x02: /* 32 bits */
272 case 0x03: /* 64 bits */
273 flags |= IORESOURCE_MEM;
274 }
275 if (w & 0x40000000)
276 flags |= IORESOURCE_PREFETCH;
277 return flags;
278}
279
280/*
David S. Millercf44bbc2006-06-29 14:34:50 -0700281 * SBUS bus specific translator
282 */
283
284static int of_bus_sbus_match(struct device_node *np)
285{
286 return !strcmp(np->name, "sbus") ||
287 !strcmp(np->name, "sbi");
288}
289
290static void of_bus_sbus_count_cells(struct device_node *child,
291 int *addrc, int *sizec)
292{
293 if (addrc)
294 *addrc = 2;
295 if (sizec)
296 *sizec = 1;
297}
298
David S. Miller4130a4b2006-10-25 22:31:06 -0700299/*
300 * FHC/Central bus specific translator.
301 *
302 * This is just needed to hard-code the address and size cell
303 * counts. 'fhc' and 'central' nodes lack the #address-cells and
304 * #size-cells properties, and if you walk to the root on such
305 * Enterprise boxes all you'll get is a #size-cells of 2 which is
306 * not what we want to use.
307 */
308static int of_bus_fhc_match(struct device_node *np)
David S. Millercf44bbc2006-06-29 14:34:50 -0700309{
David S. Miller4130a4b2006-10-25 22:31:06 -0700310 return !strcmp(np->name, "fhc") ||
311 !strcmp(np->name, "central");
David S. Millercf44bbc2006-06-29 14:34:50 -0700312}
313
David S. Miller4130a4b2006-10-25 22:31:06 -0700314#define of_bus_fhc_count_cells of_bus_sbus_count_cells
David S. Millercf44bbc2006-06-29 14:34:50 -0700315
316/*
317 * Array of bus specific translators
318 */
319
320static struct of_bus of_busses[] = {
321 /* PCI */
322 {
323 .name = "pci",
324 .addr_prop_name = "assigned-addresses",
325 .match = of_bus_pci_match,
326 .count_cells = of_bus_pci_count_cells,
327 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700328 .get_flags = of_bus_pci_get_flags,
329 },
David S. Miller01f94c42007-03-04 12:53:19 -0800330 /* SIMBA */
331 {
332 .name = "simba",
333 .addr_prop_name = "assigned-addresses",
334 .match = of_bus_simba_match,
335 .count_cells = of_bus_pci_count_cells,
336 .map = of_bus_simba_map,
337 .get_flags = of_bus_pci_get_flags,
338 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700339 /* SBUS */
340 {
341 .name = "sbus",
342 .addr_prop_name = "reg",
343 .match = of_bus_sbus_match,
344 .count_cells = of_bus_sbus_count_cells,
David S. Miller4130a4b2006-10-25 22:31:06 -0700345 .map = of_bus_default_map,
346 .get_flags = of_bus_default_get_flags,
347 },
348 /* FHC */
349 {
350 .name = "fhc",
351 .addr_prop_name = "reg",
352 .match = of_bus_fhc_match,
353 .count_cells = of_bus_fhc_count_cells,
354 .map = of_bus_default_map,
355 .get_flags = of_bus_default_get_flags,
David S. Millercf44bbc2006-06-29 14:34:50 -0700356 },
357 /* Default */
358 {
359 .name = "default",
360 .addr_prop_name = "reg",
361 .match = NULL,
362 .count_cells = of_bus_default_count_cells,
363 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700364 .get_flags = of_bus_default_get_flags,
365 },
366};
367
368static struct of_bus *of_match_bus(struct device_node *np)
369{
370 int i;
371
372 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
373 if (!of_busses[i].match || of_busses[i].match(np))
374 return &of_busses[i];
375 BUG();
376 return NULL;
377}
378
379static int __init build_one_resource(struct device_node *parent,
380 struct of_bus *bus,
381 struct of_bus *pbus,
382 u32 *addr,
383 int na, int ns, int pna)
384{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700385 const u32 *ranges;
David S. Millercf44bbc2006-06-29 14:34:50 -0700386 unsigned int rlen;
387 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700388
389 ranges = of_get_property(parent, "ranges", &rlen);
390 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700391 u32 result[OF_MAX_ADDR_CELLS];
392 int i;
393
394 memset(result, 0, pna * 4);
395 for (i = 0; i < na; i++)
396 result[pna - 1 - i] =
397 addr[na - 1 - i];
398
399 memcpy(addr, result, pna * 4);
400 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700401 }
402
403 /* Now walk through the ranges */
404 rlen /= 4;
405 rone = na + pna + ns;
406 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700407 if (!bus->map(addr, ranges, na, ns, pna))
408 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700409 }
David S. Millera83f9822006-07-12 23:19:31 -0700410
David S. Miller49d23cf2007-05-13 22:01:18 -0700411 /* When we miss an I/O space match on PCI, just pass it up
412 * to the next PCI bridge and/or controller.
413 */
414 if (!strcmp(bus->name, "pci") &&
415 (addr[0] & 0x03000000) == 0x01000000)
416 return 0;
417
David S. Millera83f9822006-07-12 23:19:31 -0700418 return 1;
419}
420
421static int __init use_1to1_mapping(struct device_node *pp)
422{
David S. Millera83f9822006-07-12 23:19:31 -0700423 /* If we have a ranges property in the parent, use it. */
424 if (of_find_property(pp, "ranges", NULL) != NULL)
425 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700426
David S. Millera83f9822006-07-12 23:19:31 -0700427 /* If the parent is the dma node of an ISA bus, pass
428 * the translation up to the root.
429 */
430 if (!strcmp(pp->name, "dma"))
431 return 0;
432
David S. Miller8c2786c2007-06-07 21:59:44 -0700433 /* Similarly for all PCI bridges, if we get this far
434 * it lacks a ranges property, and this will include
435 * cases like Simba.
436 */
437 if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex"))
David S. Millera83f9822006-07-12 23:19:31 -0700438 return 0;
439
440 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700441}
442
David S. Millera83f9822006-07-12 23:19:31 -0700443static int of_resource_verbose;
444
David S. Millercf44bbc2006-06-29 14:34:50 -0700445static void __init build_device_resources(struct of_device *op,
446 struct device *parent)
447{
448 struct of_device *p_op;
449 struct of_bus *bus;
450 int na, ns;
451 int index, num_reg;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700452 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700453
454 if (!parent)
455 return;
456
457 p_op = to_of_device(parent);
458 bus = of_match_bus(p_op->node);
459 bus->count_cells(op->node, &na, &ns);
460
461 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
462 if (!preg || num_reg == 0)
463 return;
464
465 /* Convert to num-cells. */
466 num_reg /= 4;
467
David S. Miller46ba6d72006-07-16 22:10:44 -0700468 /* Convert to num-entries. */
David S. Millercf44bbc2006-06-29 14:34:50 -0700469 num_reg /= na + ns;
470
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700471 /* Prevent overrunning the op->resources[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700472 if (num_reg > PROMREG_MAX) {
473 printk(KERN_WARNING "%s: Too many regs (%d), "
474 "limiting to %d.\n",
475 op->node->full_name, num_reg, PROMREG_MAX);
476 num_reg = PROMREG_MAX;
477 }
478
David S. Millercf44bbc2006-06-29 14:34:50 -0700479 for (index = 0; index < num_reg; index++) {
480 struct resource *r = &op->resource[index];
481 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700482 const u32 *reg = (preg + (index * ((na + ns) * 4)));
David S. Millercf44bbc2006-06-29 14:34:50 -0700483 struct device_node *dp = op->node;
484 struct device_node *pp = p_op->node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800485 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700486 u64 size, result = OF_BAD_ADDR;
487 unsigned long flags;
488 int dna, dns;
489 int pna, pns;
490
491 size = of_read_addr(reg + na, ns);
492 flags = bus->get_flags(reg);
493
494 memcpy(addr, reg, na * 4);
495
David S. Millera83f9822006-07-12 23:19:31 -0700496 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700497 result = of_read_addr(addr, na);
498 goto build_res;
499 }
500
501 dna = na;
502 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800503 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700504
505 while (1) {
506 dp = pp;
507 pp = dp->parent;
508 if (!pp) {
509 result = of_read_addr(addr, dna);
510 break;
511 }
512
513 pbus = of_match_bus(pp);
514 pbus->count_cells(dp, &pna, &pns);
515
David S. Millerb85cdd42007-02-28 23:20:12 -0800516 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700517 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700518 break;
519
520 dna = pna;
521 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800522 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700523 }
524
525 build_res:
526 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700527
528 if (of_resource_verbose)
529 printk("%s reg[%d] -> %lx\n",
530 op->node->full_name, index,
531 result);
532
David S. Millercf44bbc2006-06-29 14:34:50 -0700533 if (result != OF_BAD_ADDR) {
David S. Miller1815aed2006-06-29 19:58:28 -0700534 if (tlb_type == hypervisor)
535 result &= 0x0fffffffffffffffUL;
536
David S. Millercf44bbc2006-06-29 14:34:50 -0700537 r->start = result;
538 r->end = result + size - 1;
539 r->flags = flags;
David S. Millercf44bbc2006-06-29 14:34:50 -0700540 }
541 r->name = op->node->name;
542 }
543}
544
David S. Miller2b1e5972006-06-29 15:07:37 -0700545static struct device_node * __init
546apply_interrupt_map(struct device_node *dp, struct device_node *pp,
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700547 const u32 *imap, int imlen, const u32 *imask,
David S. Miller2b1e5972006-06-29 15:07:37 -0700548 unsigned int *irq_p)
549{
550 struct device_node *cp;
551 unsigned int irq = *irq_p;
552 struct of_bus *bus;
553 phandle handle;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700554 const u32 *reg;
David S. Miller2b1e5972006-06-29 15:07:37 -0700555 int na, num_reg, i;
556
557 bus = of_match_bus(pp);
558 bus->count_cells(dp, &na, NULL);
559
560 reg = of_get_property(dp, "reg", &num_reg);
561 if (!reg || !num_reg)
562 return NULL;
563
564 imlen /= ((na + 3) * 4);
565 handle = 0;
566 for (i = 0; i < imlen; i++) {
567 int j;
568
569 for (j = 0; j < na; j++) {
570 if ((reg[j] & imask[j]) != imap[j])
571 goto next;
572 }
573 if (imap[na] == irq) {
574 handle = imap[na + 1];
575 irq = imap[na + 2];
576 break;
577 }
578
579 next:
580 imap += (na + 3);
581 }
David S. Miller46ba6d72006-07-16 22:10:44 -0700582 if (i == imlen) {
583 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
584 * properties that do not include the on-board device
585 * interrupts. Instead, the device's 'interrupts' property
586 * is already a fully specified INO value.
587 *
588 * Handle this by deciding that, if we didn't get a
589 * match in the parent's 'interrupt-map', and the
590 * parent is an IRQ translater, then use the parent as
591 * our IRQ controller.
592 */
593 if (pp->irq_trans)
594 return pp;
595
David S. Miller2b1e5972006-06-29 15:07:37 -0700596 return NULL;
David S. Miller46ba6d72006-07-16 22:10:44 -0700597 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700598
599 *irq_p = irq;
600 cp = of_find_node_by_phandle(handle);
601
602 return cp;
603}
604
605static unsigned int __init pci_irq_swizzle(struct device_node *dp,
606 struct device_node *pp,
607 unsigned int irq)
608{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700609 const struct linux_prom_pci_registers *regs;
David S. Millerbb4c18c2007-02-26 14:55:06 -0800610 unsigned int bus, devfn, slot, ret;
David S. Miller2b1e5972006-06-29 15:07:37 -0700611
612 if (irq < 1 || irq > 4)
613 return irq;
614
615 regs = of_get_property(dp, "reg", NULL);
616 if (!regs)
617 return irq;
618
David S. Millerbb4c18c2007-02-26 14:55:06 -0800619 bus = (regs->phys_hi >> 16) & 0xff;
David S. Miller2b1e5972006-06-29 15:07:37 -0700620 devfn = (regs->phys_hi >> 8) & 0xff;
621 slot = (devfn >> 3) & 0x1f;
622
David S. Millerbb4c18c2007-02-26 14:55:06 -0800623 if (pp->irq_trans) {
624 /* Derived from Table 8-3, U2P User's Manual. This branch
625 * is handling a PCI controller that lacks a proper set of
626 * interrupt-map and interrupt-map-mask properties. The
627 * Ultra-E450 is one example.
628 *
629 * The bit layout is BSSLL, where:
630 * B: 0 on bus A, 1 on bus B
631 * D: 2-bit slot number, derived from PCI device number as
632 * (dev - 1) for bus A, or (dev - 2) for bus B
633 * L: 2-bit line number
David S. Millerbb4c18c2007-02-26 14:55:06 -0800634 */
635 if (bus & 0x80) {
636 /* PBM-A */
637 bus = 0x00;
638 slot = (slot - 1) << 2;
639 } else {
640 /* PBM-B */
641 bus = 0x10;
642 slot = (slot - 2) << 2;
643 }
644 irq -= 1;
645
646 ret = (bus | slot | irq);
647 } else {
648 /* Going through a PCI-PCI bridge that lacks a set of
649 * interrupt-map and interrupt-map-mask properties.
650 */
651 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
652 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700653
654 return ret;
655}
656
David S. Millera83f9822006-07-12 23:19:31 -0700657static int of_irq_verbose;
658
David S. Miller2b1e5972006-06-29 15:07:37 -0700659static unsigned int __init build_one_device_irq(struct of_device *op,
660 struct device *parent,
661 unsigned int irq)
662{
663 struct device_node *dp = op->node;
664 struct device_node *pp, *ip;
665 unsigned int orig_irq = irq;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700666 int nid;
David S. Miller2b1e5972006-06-29 15:07:37 -0700667
668 if (irq == 0xffffffff)
669 return irq;
670
671 if (dp->irq_trans) {
672 irq = dp->irq_trans->irq_build(dp, irq,
673 dp->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700674
675 if (of_irq_verbose)
676 printk("%s: direct translate %x --> %x\n",
677 dp->full_name, orig_irq, irq);
678
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700679 goto out;
David S. Miller2b1e5972006-06-29 15:07:37 -0700680 }
681
682 /* Something more complicated. Walk up to the root, applying
683 * interrupt-map or bus specific translations, until we hit
684 * an IRQ translator.
685 *
686 * If we hit a bus type or situation we cannot handle, we
687 * stop and assume that the original IRQ number was in a
688 * format which has special meaning to it's immediate parent.
689 */
690 pp = dp->parent;
691 ip = NULL;
692 while (pp) {
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700693 const void *imap, *imsk;
David S. Miller2b1e5972006-06-29 15:07:37 -0700694 int imlen;
695
696 imap = of_get_property(pp, "interrupt-map", &imlen);
697 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
698 if (imap && imsk) {
699 struct device_node *iret;
700 int this_orig_irq = irq;
701
702 iret = apply_interrupt_map(dp, pp,
703 imap, imlen, imsk,
704 &irq);
David S. Millera83f9822006-07-12 23:19:31 -0700705
706 if (of_irq_verbose)
707 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
708 op->node->full_name,
709 pp->full_name, this_orig_irq,
710 (iret ? iret->full_name : "NULL"), irq);
711
David S. Miller2b1e5972006-06-29 15:07:37 -0700712 if (!iret)
713 break;
714
715 if (iret->irq_trans) {
716 ip = iret;
717 break;
718 }
719 } else {
720 if (!strcmp(pp->type, "pci") ||
721 !strcmp(pp->type, "pciex")) {
722 unsigned int this_orig_irq = irq;
723
724 irq = pci_irq_swizzle(dp, pp, irq);
David S. Millera83f9822006-07-12 23:19:31 -0700725 if (of_irq_verbose)
726 printk("%s: PCI swizzle [%s] "
727 "%x --> %x\n",
728 op->node->full_name,
729 pp->full_name, this_orig_irq,
730 irq);
731
David S. Miller2b1e5972006-06-29 15:07:37 -0700732 }
733
734 if (pp->irq_trans) {
735 ip = pp;
736 break;
737 }
738 }
739 dp = pp;
740 pp = pp->parent;
741 }
742 if (!ip)
743 return orig_irq;
744
745 irq = ip->irq_trans->irq_build(op->node, irq,
746 ip->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700747 if (of_irq_verbose)
748 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
749 op->node->full_name, ip->full_name, orig_irq, irq);
David S. Miller2b1e5972006-06-29 15:07:37 -0700750
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700751out:
752 nid = of_node_to_nid(dp);
753 if (nid != -1) {
754 cpumask_t numa_mask = node_to_cpumask(nid);
755
756 irq_set_affinity(irq, numa_mask);
757 }
758
David S. Miller2b1e5972006-06-29 15:07:37 -0700759 return irq;
760}
761
David S. Millercf44bbc2006-06-29 14:34:50 -0700762static struct of_device * __init scan_one_device(struct device_node *dp,
763 struct device *parent)
764{
765 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700766 const unsigned int *irq;
David S. Miller3d6e4702007-07-18 22:03:25 -0700767 struct dev_archdata *sd;
David S. Miller2b1e5972006-06-29 15:07:37 -0700768 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700769
770 if (!op)
771 return NULL;
772
David S. Miller3d6e4702007-07-18 22:03:25 -0700773 sd = &op->dev.archdata;
774 sd->prom_node = dp;
775 sd->op = op;
776
David S. Millercf44bbc2006-06-29 14:34:50 -0700777 op->node = dp;
778
779 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
780 (25*1000*1000));
781 op->portid = of_getintprop_default(dp, "upa-portid", -1);
782 if (op->portid == -1)
783 op->portid = of_getintprop_default(dp, "portid", -1);
784
785 irq = of_get_property(dp, "interrupts", &len);
David S. Miller2b1e5972006-06-29 15:07:37 -0700786 if (irq) {
787 memcpy(op->irqs, irq, len);
788 op->num_irqs = len / 4;
789 } else {
790 op->num_irqs = 0;
791 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700792
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700793 /* Prevent overrunning the op->irqs[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700794 if (op->num_irqs > PROMINTR_MAX) {
795 printk(KERN_WARNING "%s: Too many irqs (%d), "
796 "limiting to %d.\n",
797 dp->full_name, op->num_irqs, PROMINTR_MAX);
798 op->num_irqs = PROMINTR_MAX;
799 }
800
David S. Millercf44bbc2006-06-29 14:34:50 -0700801 build_device_resources(op, parent);
David S. Miller2b1e5972006-06-29 15:07:37 -0700802 for (i = 0; i < op->num_irqs; i++)
803 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
David S. Millercf44bbc2006-06-29 14:34:50 -0700804
805 op->dev.parent = parent;
Stephen Rothwell37b77542007-04-30 17:43:56 +1000806 op->dev.bus = &of_platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700807 if (!parent)
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200808 dev_set_name(&op->dev, "root");
David S. Millercf44bbc2006-06-29 14:34:50 -0700809 else
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200810 dev_set_name(&op->dev, "%08x", dp->node);
David S. Millercf44bbc2006-06-29 14:34:50 -0700811
812 if (of_device_register(op)) {
813 printk("%s: Could not register of device.\n",
814 dp->full_name);
815 kfree(op);
816 op = NULL;
817 }
818
819 return op;
820}
821
822static void __init scan_tree(struct device_node *dp, struct device *parent)
823{
824 while (dp) {
825 struct of_device *op = scan_one_device(dp, parent);
826
827 if (op)
828 scan_tree(dp->child, &op->dev);
829
830 dp = dp->sibling;
831 }
832}
833
834static void __init scan_of_devices(void)
835{
836 struct device_node *root = of_find_node_by_path("/");
837 struct of_device *parent;
838
839 parent = scan_one_device(root, NULL);
840 if (!parent)
841 return;
842
843 scan_tree(root->child, &parent->dev);
844}
845
David S. Millera2bd4fd2006-06-23 01:44:10 -0700846static int __init of_bus_driver_init(void)
847{
David S. Millercf44bbc2006-06-29 14:34:50 -0700848 int err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700849
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000850 err = of_bus_type_init(&of_platform_bus_type, "of");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700851#ifdef CONFIG_PCI
852 if (!err)
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000853 err = of_bus_type_init(&ebus_bus_type, "ebus");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700854#endif
855#ifdef CONFIG_SBUS
856 if (!err)
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000857 err = of_bus_type_init(&sbus_bus_type, "sbus");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700858#endif
David S. Millercf44bbc2006-06-29 14:34:50 -0700859
860 if (!err)
861 scan_of_devices();
862
863 return err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700864}
865
866postcore_initcall(of_bus_driver_init);
867
David S. Millera83f9822006-07-12 23:19:31 -0700868static int __init of_debug(char *str)
869{
870 int val = 0;
871
872 get_option(&str, &val);
873 if (val & 1)
874 of_resource_verbose = 1;
875 if (val & 2)
876 of_irq_verbose = 1;
877 return 1;
878}
879
880__setup("of_debug=", of_debug);