blob: 9e58e8cba1c38655fdd7138526d9dc29e60ca5c1 [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 isa_bus_type;
David S. Miller69853912006-06-25 01:21:38 -070060EXPORT_SYMBOL(isa_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -070061
Stephen Rothwell3f23de12007-05-03 02:38:57 +100062struct bus_type ebus_bus_type;
David S. Miller69853912006-06-25 01:21:38 -070063EXPORT_SYMBOL(ebus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -070064#endif
65
66#ifdef CONFIG_SBUS
Stephen Rothwell3f23de12007-05-03 02:38:57 +100067struct bus_type sbus_bus_type;
David S. Miller69853912006-06-25 01:21:38 -070068EXPORT_SYMBOL(sbus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -070069#endif
70
Stephen Rothwell3f23de12007-05-03 02:38:57 +100071struct bus_type of_platform_bus_type;
Stephen Rothwell37b77542007-04-30 17:43:56 +100072EXPORT_SYMBOL(of_platform_bus_type);
David S. Millercf44bbc2006-06-29 14:34:50 -070073
David S. Millera83f9822006-07-12 23:19:31 -070074static inline u64 of_read_addr(const u32 *cell, int size)
David S. Millercf44bbc2006-06-29 14:34:50 -070075{
76 u64 r = 0;
77 while (size--)
78 r = (r << 32) | *(cell++);
79 return r;
80}
81
82static void __init get_cells(struct device_node *dp,
83 int *addrc, int *sizec)
84{
85 if (addrc)
86 *addrc = of_n_addr_cells(dp);
87 if (sizec)
88 *sizec = of_n_size_cells(dp);
89}
90
91/* Max address size we deal with */
92#define OF_MAX_ADDR_CELLS 4
93
94struct of_bus {
95 const char *name;
96 const char *addr_prop_name;
97 int (*match)(struct device_node *parent);
98 void (*count_cells)(struct device_node *child,
99 int *addrc, int *sizec);
David S. Millera83f9822006-07-12 23:19:31 -0700100 int (*map)(u32 *addr, const u32 *range,
101 int na, int ns, int pna);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700102 unsigned int (*get_flags)(const u32 *addr);
David S. Millercf44bbc2006-06-29 14:34:50 -0700103};
104
105/*
106 * Default translator (generic bus)
107 */
108
109static void of_bus_default_count_cells(struct device_node *dev,
110 int *addrc, int *sizec)
111{
112 get_cells(dev, addrc, sizec);
113}
114
David S. Millera83f9822006-07-12 23:19:31 -0700115/* Make sure the least significant 64-bits are in-range. Even
116 * for 3 or 4 cell values it is a good enough approximation.
117 */
118static int of_out_of_range(const u32 *addr, const u32 *base,
119 const u32 *size, int na, int ns)
David S. Millercf44bbc2006-06-29 14:34:50 -0700120{
121 u64 a = of_read_addr(addr, na);
David S. Millera83f9822006-07-12 23:19:31 -0700122 u64 b = of_read_addr(base, na);
123
124 if (a < b)
125 return 1;
126
127 b += of_read_addr(size, ns);
128 if (a >= b)
129 return 1;
130
131 return 0;
132}
133
134static int of_bus_default_map(u32 *addr, const u32 *range,
135 int na, int ns, int pna)
136{
137 u32 result[OF_MAX_ADDR_CELLS];
138 int i;
139
140 if (ns > 2) {
141 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
142 return -EINVAL;
143 }
144
145 if (of_out_of_range(addr, range, range + na + pna, na, ns))
146 return -EINVAL;
147
148 /* Start with the parent range base. */
149 memcpy(result, range + na, pna * 4);
150
151 /* Add in the child address offset. */
152 for (i = 0; i < na; i++)
153 result[pna - 1 - i] +=
154 (addr[na - 1 - i] -
155 range[na - 1 - i]);
156
157 memcpy(addr, result, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700158
159 return 0;
160}
161
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700162static unsigned int of_bus_default_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700163{
164 return IORESOURCE_MEM;
165}
166
David S. Millercf44bbc2006-06-29 14:34:50 -0700167/*
168 * PCI bus specific translator
169 */
170
171static int of_bus_pci_match(struct device_node *np)
172{
David S. Millera83f9822006-07-12 23:19:31 -0700173 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
David S. Millera165b422007-03-29 01:50:16 -0700174 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800175
176 if (model && !strcmp(model, "SUNW,simba"))
177 return 0;
178
David S. Millera83f9822006-07-12 23:19:31 -0700179 /* Do not do PCI specific frobbing if the
180 * PCI bridge lacks a ranges property. We
181 * want to pass it through up to the next
182 * parent as-is, not with the PCI translate
183 * method which chops off the top address cell.
184 */
185 if (!of_find_property(np, "ranges", NULL))
186 return 0;
187
188 return 1;
189 }
190
191 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700192}
193
David S. Miller01f94c42007-03-04 12:53:19 -0800194static int of_bus_simba_match(struct device_node *np)
195{
David S. Millera165b422007-03-29 01:50:16 -0700196 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800197
198 if (model && !strcmp(model, "SUNW,simba"))
199 return 1;
David S. Miller8c2786c2007-06-07 21:59:44 -0700200
201 /* Treat PCI busses lacking ranges property just like
202 * simba.
203 */
204 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
205 if (!of_find_property(np, "ranges", NULL))
206 return 1;
207 }
208
David S. Miller01f94c42007-03-04 12:53:19 -0800209 return 0;
210}
211
212static int of_bus_simba_map(u32 *addr, const u32 *range,
213 int na, int ns, int pna)
214{
215 return 0;
216}
217
David S. Millercf44bbc2006-06-29 14:34:50 -0700218static void of_bus_pci_count_cells(struct device_node *np,
219 int *addrc, int *sizec)
220{
221 if (addrc)
222 *addrc = 3;
223 if (sizec)
224 *sizec = 2;
225}
226
David S. Millera83f9822006-07-12 23:19:31 -0700227static int of_bus_pci_map(u32 *addr, const u32 *range,
228 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700229{
David S. Millera83f9822006-07-12 23:19:31 -0700230 u32 result[OF_MAX_ADDR_CELLS];
231 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700232
233 /* Check address type match */
234 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -0700235 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700236
David S. Millera83f9822006-07-12 23:19:31 -0700237 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
238 na - 1, ns))
239 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700240
David S. Millera83f9822006-07-12 23:19:31 -0700241 /* Start with the parent range base. */
242 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700243
David S. Millera83f9822006-07-12 23:19:31 -0700244 /* Add in the child address offset, skipping high cell. */
245 for (i = 0; i < na - 1; i++)
246 result[pna - 1 - i] +=
247 (addr[na - 1 - i] -
248 range[na - 1 - i]);
249
250 memcpy(addr, result, pna * 4);
251
252 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700253}
254
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700255static unsigned int of_bus_pci_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700256{
257 unsigned int flags = 0;
258 u32 w = addr[0];
259
260 switch((w >> 24) & 0x03) {
261 case 0x01:
262 flags |= IORESOURCE_IO;
263 case 0x02: /* 32 bits */
264 case 0x03: /* 64 bits */
265 flags |= IORESOURCE_MEM;
266 }
267 if (w & 0x40000000)
268 flags |= IORESOURCE_PREFETCH;
269 return flags;
270}
271
272/*
David S. Millercf44bbc2006-06-29 14:34:50 -0700273 * SBUS bus specific translator
274 */
275
276static int of_bus_sbus_match(struct device_node *np)
277{
278 return !strcmp(np->name, "sbus") ||
279 !strcmp(np->name, "sbi");
280}
281
282static void of_bus_sbus_count_cells(struct device_node *child,
283 int *addrc, int *sizec)
284{
285 if (addrc)
286 *addrc = 2;
287 if (sizec)
288 *sizec = 1;
289}
290
David S. Miller4130a4b2006-10-25 22:31:06 -0700291/*
292 * FHC/Central bus specific translator.
293 *
294 * This is just needed to hard-code the address and size cell
295 * counts. 'fhc' and 'central' nodes lack the #address-cells and
296 * #size-cells properties, and if you walk to the root on such
297 * Enterprise boxes all you'll get is a #size-cells of 2 which is
298 * not what we want to use.
299 */
300static int of_bus_fhc_match(struct device_node *np)
David S. Millercf44bbc2006-06-29 14:34:50 -0700301{
David S. Miller4130a4b2006-10-25 22:31:06 -0700302 return !strcmp(np->name, "fhc") ||
303 !strcmp(np->name, "central");
David S. Millercf44bbc2006-06-29 14:34:50 -0700304}
305
David S. Miller4130a4b2006-10-25 22:31:06 -0700306#define of_bus_fhc_count_cells of_bus_sbus_count_cells
David S. Millercf44bbc2006-06-29 14:34:50 -0700307
308/*
309 * Array of bus specific translators
310 */
311
312static struct of_bus of_busses[] = {
313 /* PCI */
314 {
315 .name = "pci",
316 .addr_prop_name = "assigned-addresses",
317 .match = of_bus_pci_match,
318 .count_cells = of_bus_pci_count_cells,
319 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700320 .get_flags = of_bus_pci_get_flags,
321 },
David S. Miller01f94c42007-03-04 12:53:19 -0800322 /* SIMBA */
323 {
324 .name = "simba",
325 .addr_prop_name = "assigned-addresses",
326 .match = of_bus_simba_match,
327 .count_cells = of_bus_pci_count_cells,
328 .map = of_bus_simba_map,
329 .get_flags = of_bus_pci_get_flags,
330 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700331 /* SBUS */
332 {
333 .name = "sbus",
334 .addr_prop_name = "reg",
335 .match = of_bus_sbus_match,
336 .count_cells = of_bus_sbus_count_cells,
David S. Miller4130a4b2006-10-25 22:31:06 -0700337 .map = of_bus_default_map,
338 .get_flags = of_bus_default_get_flags,
339 },
340 /* FHC */
341 {
342 .name = "fhc",
343 .addr_prop_name = "reg",
344 .match = of_bus_fhc_match,
345 .count_cells = of_bus_fhc_count_cells,
346 .map = of_bus_default_map,
347 .get_flags = of_bus_default_get_flags,
David S. Millercf44bbc2006-06-29 14:34:50 -0700348 },
349 /* Default */
350 {
351 .name = "default",
352 .addr_prop_name = "reg",
353 .match = NULL,
354 .count_cells = of_bus_default_count_cells,
355 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700356 .get_flags = of_bus_default_get_flags,
357 },
358};
359
360static struct of_bus *of_match_bus(struct device_node *np)
361{
362 int i;
363
364 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
365 if (!of_busses[i].match || of_busses[i].match(np))
366 return &of_busses[i];
367 BUG();
368 return NULL;
369}
370
371static int __init build_one_resource(struct device_node *parent,
372 struct of_bus *bus,
373 struct of_bus *pbus,
374 u32 *addr,
375 int na, int ns, int pna)
376{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700377 const u32 *ranges;
David S. Millercf44bbc2006-06-29 14:34:50 -0700378 unsigned int rlen;
379 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700380
381 ranges = of_get_property(parent, "ranges", &rlen);
382 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700383 u32 result[OF_MAX_ADDR_CELLS];
384 int i;
385
386 memset(result, 0, pna * 4);
387 for (i = 0; i < na; i++)
388 result[pna - 1 - i] =
389 addr[na - 1 - i];
390
391 memcpy(addr, result, pna * 4);
392 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700393 }
394
395 /* Now walk through the ranges */
396 rlen /= 4;
397 rone = na + pna + ns;
398 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700399 if (!bus->map(addr, ranges, na, ns, pna))
400 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700401 }
David S. Millera83f9822006-07-12 23:19:31 -0700402
David S. Miller49d23cf2007-05-13 22:01:18 -0700403 /* When we miss an I/O space match on PCI, just pass it up
404 * to the next PCI bridge and/or controller.
405 */
406 if (!strcmp(bus->name, "pci") &&
407 (addr[0] & 0x03000000) == 0x01000000)
408 return 0;
409
David S. Millera83f9822006-07-12 23:19:31 -0700410 return 1;
411}
412
413static int __init use_1to1_mapping(struct device_node *pp)
414{
David S. Millera83f9822006-07-12 23:19:31 -0700415 /* If this is on the PMU bus, don't try to translate it even
416 * if a ranges property exists.
417 */
418 if (!strcmp(pp->name, "pmu"))
David S. Millercf44bbc2006-06-29 14:34:50 -0700419 return 1;
420
David S. Millera83f9822006-07-12 23:19:31 -0700421 /* If we have a ranges property in the parent, use it. */
422 if (of_find_property(pp, "ranges", NULL) != NULL)
423 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700424
David S. Millera83f9822006-07-12 23:19:31 -0700425 /* If the parent is the dma node of an ISA bus, pass
426 * the translation up to the root.
427 */
428 if (!strcmp(pp->name, "dma"))
429 return 0;
430
David S. Miller8c2786c2007-06-07 21:59:44 -0700431 /* Similarly for all PCI bridges, if we get this far
432 * it lacks a ranges property, and this will include
433 * cases like Simba.
434 */
435 if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex"))
David S. Millera83f9822006-07-12 23:19:31 -0700436 return 0;
437
438 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700439}
440
David S. Millera83f9822006-07-12 23:19:31 -0700441static int of_resource_verbose;
442
David S. Millercf44bbc2006-06-29 14:34:50 -0700443static void __init build_device_resources(struct of_device *op,
444 struct device *parent)
445{
446 struct of_device *p_op;
447 struct of_bus *bus;
448 int na, ns;
449 int index, num_reg;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700450 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700451
452 if (!parent)
453 return;
454
455 p_op = to_of_device(parent);
456 bus = of_match_bus(p_op->node);
457 bus->count_cells(op->node, &na, &ns);
458
459 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
460 if (!preg || num_reg == 0)
461 return;
462
463 /* Convert to num-cells. */
464 num_reg /= 4;
465
David S. Miller46ba6d72006-07-16 22:10:44 -0700466 /* Convert to num-entries. */
David S. Millercf44bbc2006-06-29 14:34:50 -0700467 num_reg /= na + ns;
468
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700469 /* Prevent overrunning the op->resources[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700470 if (num_reg > PROMREG_MAX) {
471 printk(KERN_WARNING "%s: Too many regs (%d), "
472 "limiting to %d.\n",
473 op->node->full_name, num_reg, PROMREG_MAX);
474 num_reg = PROMREG_MAX;
475 }
476
David S. Millercf44bbc2006-06-29 14:34:50 -0700477 for (index = 0; index < num_reg; index++) {
478 struct resource *r = &op->resource[index];
479 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700480 const u32 *reg = (preg + (index * ((na + ns) * 4)));
David S. Millercf44bbc2006-06-29 14:34:50 -0700481 struct device_node *dp = op->node;
482 struct device_node *pp = p_op->node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800483 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700484 u64 size, result = OF_BAD_ADDR;
485 unsigned long flags;
486 int dna, dns;
487 int pna, pns;
488
489 size = of_read_addr(reg + na, ns);
490 flags = bus->get_flags(reg);
491
492 memcpy(addr, reg, na * 4);
493
David S. Millera83f9822006-07-12 23:19:31 -0700494 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700495 result = of_read_addr(addr, na);
496 goto build_res;
497 }
498
499 dna = na;
500 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800501 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700502
503 while (1) {
504 dp = pp;
505 pp = dp->parent;
506 if (!pp) {
507 result = of_read_addr(addr, dna);
508 break;
509 }
510
511 pbus = of_match_bus(pp);
512 pbus->count_cells(dp, &pna, &pns);
513
David S. Millerb85cdd42007-02-28 23:20:12 -0800514 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700515 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700516 break;
517
518 dna = pna;
519 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800520 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700521 }
522
523 build_res:
524 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700525
526 if (of_resource_verbose)
527 printk("%s reg[%d] -> %lx\n",
528 op->node->full_name, index,
529 result);
530
David S. Millercf44bbc2006-06-29 14:34:50 -0700531 if (result != OF_BAD_ADDR) {
David S. Miller1815aed2006-06-29 19:58:28 -0700532 if (tlb_type == hypervisor)
533 result &= 0x0fffffffffffffffUL;
534
David S. Millercf44bbc2006-06-29 14:34:50 -0700535 r->start = result;
536 r->end = result + size - 1;
537 r->flags = flags;
David S. Millercf44bbc2006-06-29 14:34:50 -0700538 }
539 r->name = op->node->name;
540 }
541}
542
David S. Miller2b1e5972006-06-29 15:07:37 -0700543static struct device_node * __init
544apply_interrupt_map(struct device_node *dp, struct device_node *pp,
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700545 const u32 *imap, int imlen, const u32 *imask,
David S. Miller2b1e5972006-06-29 15:07:37 -0700546 unsigned int *irq_p)
547{
548 struct device_node *cp;
549 unsigned int irq = *irq_p;
550 struct of_bus *bus;
551 phandle handle;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700552 const u32 *reg;
David S. Miller2b1e5972006-06-29 15:07:37 -0700553 int na, num_reg, i;
554
555 bus = of_match_bus(pp);
556 bus->count_cells(dp, &na, NULL);
557
558 reg = of_get_property(dp, "reg", &num_reg);
559 if (!reg || !num_reg)
560 return NULL;
561
562 imlen /= ((na + 3) * 4);
563 handle = 0;
564 for (i = 0; i < imlen; i++) {
565 int j;
566
567 for (j = 0; j < na; j++) {
568 if ((reg[j] & imask[j]) != imap[j])
569 goto next;
570 }
571 if (imap[na] == irq) {
572 handle = imap[na + 1];
573 irq = imap[na + 2];
574 break;
575 }
576
577 next:
578 imap += (na + 3);
579 }
David S. Miller46ba6d72006-07-16 22:10:44 -0700580 if (i == imlen) {
581 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
582 * properties that do not include the on-board device
583 * interrupts. Instead, the device's 'interrupts' property
584 * is already a fully specified INO value.
585 *
586 * Handle this by deciding that, if we didn't get a
587 * match in the parent's 'interrupt-map', and the
588 * parent is an IRQ translater, then use the parent as
589 * our IRQ controller.
590 */
591 if (pp->irq_trans)
592 return pp;
593
David S. Miller2b1e5972006-06-29 15:07:37 -0700594 return NULL;
David S. Miller46ba6d72006-07-16 22:10:44 -0700595 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700596
597 *irq_p = irq;
598 cp = of_find_node_by_phandle(handle);
599
600 return cp;
601}
602
603static unsigned int __init pci_irq_swizzle(struct device_node *dp,
604 struct device_node *pp,
605 unsigned int irq)
606{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700607 const struct linux_prom_pci_registers *regs;
David S. Millerbb4c18c2007-02-26 14:55:06 -0800608 unsigned int bus, devfn, slot, ret;
David S. Miller2b1e5972006-06-29 15:07:37 -0700609
610 if (irq < 1 || irq > 4)
611 return irq;
612
613 regs = of_get_property(dp, "reg", NULL);
614 if (!regs)
615 return irq;
616
David S. Millerbb4c18c2007-02-26 14:55:06 -0800617 bus = (regs->phys_hi >> 16) & 0xff;
David S. Miller2b1e5972006-06-29 15:07:37 -0700618 devfn = (regs->phys_hi >> 8) & 0xff;
619 slot = (devfn >> 3) & 0x1f;
620
David S. Millerbb4c18c2007-02-26 14:55:06 -0800621 if (pp->irq_trans) {
622 /* Derived from Table 8-3, U2P User's Manual. This branch
623 * is handling a PCI controller that lacks a proper set of
624 * interrupt-map and interrupt-map-mask properties. The
625 * Ultra-E450 is one example.
626 *
627 * The bit layout is BSSLL, where:
628 * B: 0 on bus A, 1 on bus B
629 * D: 2-bit slot number, derived from PCI device number as
630 * (dev - 1) for bus A, or (dev - 2) for bus B
631 * L: 2-bit line number
David S. Millerbb4c18c2007-02-26 14:55:06 -0800632 */
633 if (bus & 0x80) {
634 /* PBM-A */
635 bus = 0x00;
636 slot = (slot - 1) << 2;
637 } else {
638 /* PBM-B */
639 bus = 0x10;
640 slot = (slot - 2) << 2;
641 }
642 irq -= 1;
643
644 ret = (bus | slot | irq);
645 } else {
646 /* Going through a PCI-PCI bridge that lacks a set of
647 * interrupt-map and interrupt-map-mask properties.
648 */
649 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
650 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700651
652 return ret;
653}
654
David S. Millera83f9822006-07-12 23:19:31 -0700655static int of_irq_verbose;
656
David S. Miller2b1e5972006-06-29 15:07:37 -0700657static unsigned int __init build_one_device_irq(struct of_device *op,
658 struct device *parent,
659 unsigned int irq)
660{
661 struct device_node *dp = op->node;
662 struct device_node *pp, *ip;
663 unsigned int orig_irq = irq;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700664 int nid;
David S. Miller2b1e5972006-06-29 15:07:37 -0700665
666 if (irq == 0xffffffff)
667 return irq;
668
669 if (dp->irq_trans) {
670 irq = dp->irq_trans->irq_build(dp, irq,
671 dp->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700672
673 if (of_irq_verbose)
674 printk("%s: direct translate %x --> %x\n",
675 dp->full_name, orig_irq, irq);
676
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700677 goto out;
David S. Miller2b1e5972006-06-29 15:07:37 -0700678 }
679
680 /* Something more complicated. Walk up to the root, applying
681 * interrupt-map or bus specific translations, until we hit
682 * an IRQ translator.
683 *
684 * If we hit a bus type or situation we cannot handle, we
685 * stop and assume that the original IRQ number was in a
686 * format which has special meaning to it's immediate parent.
687 */
688 pp = dp->parent;
689 ip = NULL;
690 while (pp) {
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700691 const void *imap, *imsk;
David S. Miller2b1e5972006-06-29 15:07:37 -0700692 int imlen;
693
694 imap = of_get_property(pp, "interrupt-map", &imlen);
695 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
696 if (imap && imsk) {
697 struct device_node *iret;
698 int this_orig_irq = irq;
699
700 iret = apply_interrupt_map(dp, pp,
701 imap, imlen, imsk,
702 &irq);
David S. Millera83f9822006-07-12 23:19:31 -0700703
704 if (of_irq_verbose)
705 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
706 op->node->full_name,
707 pp->full_name, this_orig_irq,
708 (iret ? iret->full_name : "NULL"), irq);
709
David S. Miller2b1e5972006-06-29 15:07:37 -0700710 if (!iret)
711 break;
712
713 if (iret->irq_trans) {
714 ip = iret;
715 break;
716 }
717 } else {
718 if (!strcmp(pp->type, "pci") ||
719 !strcmp(pp->type, "pciex")) {
720 unsigned int this_orig_irq = irq;
721
722 irq = pci_irq_swizzle(dp, pp, irq);
David S. Millera83f9822006-07-12 23:19:31 -0700723 if (of_irq_verbose)
724 printk("%s: PCI swizzle [%s] "
725 "%x --> %x\n",
726 op->node->full_name,
727 pp->full_name, this_orig_irq,
728 irq);
729
David S. Miller2b1e5972006-06-29 15:07:37 -0700730 }
731
732 if (pp->irq_trans) {
733 ip = pp;
734 break;
735 }
736 }
737 dp = pp;
738 pp = pp->parent;
739 }
740 if (!ip)
741 return orig_irq;
742
743 irq = ip->irq_trans->irq_build(op->node, irq,
744 ip->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700745 if (of_irq_verbose)
746 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
747 op->node->full_name, ip->full_name, orig_irq, irq);
David S. Miller2b1e5972006-06-29 15:07:37 -0700748
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700749out:
750 nid = of_node_to_nid(dp);
751 if (nid != -1) {
752 cpumask_t numa_mask = node_to_cpumask(nid);
753
754 irq_set_affinity(irq, numa_mask);
755 }
756
David S. Miller2b1e5972006-06-29 15:07:37 -0700757 return irq;
758}
759
David S. Millercf44bbc2006-06-29 14:34:50 -0700760static struct of_device * __init scan_one_device(struct device_node *dp,
761 struct device *parent)
762{
763 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700764 const unsigned int *irq;
David S. Miller3d6e4702007-07-18 22:03:25 -0700765 struct dev_archdata *sd;
David S. Miller2b1e5972006-06-29 15:07:37 -0700766 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700767
768 if (!op)
769 return NULL;
770
David S. Miller3d6e4702007-07-18 22:03:25 -0700771 sd = &op->dev.archdata;
772 sd->prom_node = dp;
773 sd->op = op;
774
David S. Millercf44bbc2006-06-29 14:34:50 -0700775 op->node = dp;
776
777 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
778 (25*1000*1000));
779 op->portid = of_getintprop_default(dp, "upa-portid", -1);
780 if (op->portid == -1)
781 op->portid = of_getintprop_default(dp, "portid", -1);
782
783 irq = of_get_property(dp, "interrupts", &len);
David S. Miller2b1e5972006-06-29 15:07:37 -0700784 if (irq) {
785 memcpy(op->irqs, irq, len);
786 op->num_irqs = len / 4;
787 } else {
788 op->num_irqs = 0;
789 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700790
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700791 /* Prevent overrunning the op->irqs[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700792 if (op->num_irqs > PROMINTR_MAX) {
793 printk(KERN_WARNING "%s: Too many irqs (%d), "
794 "limiting to %d.\n",
795 dp->full_name, op->num_irqs, PROMINTR_MAX);
796 op->num_irqs = PROMINTR_MAX;
797 }
798
David S. Millercf44bbc2006-06-29 14:34:50 -0700799 build_device_resources(op, parent);
David S. Miller2b1e5972006-06-29 15:07:37 -0700800 for (i = 0; i < op->num_irqs; i++)
801 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
David S. Millercf44bbc2006-06-29 14:34:50 -0700802
803 op->dev.parent = parent;
Stephen Rothwell37b77542007-04-30 17:43:56 +1000804 op->dev.bus = &of_platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700805 if (!parent)
806 strcpy(op->dev.bus_id, "root");
807 else
David S. Millerf5ef9d12006-10-27 01:03:31 -0700808 sprintf(op->dev.bus_id, "%08x", dp->node);
David S. Millercf44bbc2006-06-29 14:34:50 -0700809
810 if (of_device_register(op)) {
811 printk("%s: Could not register of device.\n",
812 dp->full_name);
813 kfree(op);
814 op = NULL;
815 }
816
817 return op;
818}
819
820static void __init scan_tree(struct device_node *dp, struct device *parent)
821{
822 while (dp) {
823 struct of_device *op = scan_one_device(dp, parent);
824
825 if (op)
826 scan_tree(dp->child, &op->dev);
827
828 dp = dp->sibling;
829 }
830}
831
832static void __init scan_of_devices(void)
833{
834 struct device_node *root = of_find_node_by_path("/");
835 struct of_device *parent;
836
837 parent = scan_one_device(root, NULL);
838 if (!parent)
839 return;
840
841 scan_tree(root->child, &parent->dev);
842}
843
David S. Millera2bd4fd2006-06-23 01:44:10 -0700844static int __init of_bus_driver_init(void)
845{
David S. Millercf44bbc2006-06-29 14:34:50 -0700846 int err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700847
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000848 err = of_bus_type_init(&of_platform_bus_type, "of");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700849#ifdef CONFIG_PCI
850 if (!err)
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000851 err = of_bus_type_init(&isa_bus_type, "isa");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700852 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);