blob: c8f14c1dc5214da6b99a781f0bc58d963e144fae [file] [log] [blame]
David S. Millerfd531432006-06-23 15:55:17 -07001#include <linux/string.h>
2#include <linux/kernel.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +10003#include <linux/of.h>
David S. Millerfd531432006-06-23 15:55:17 -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>
9#include <linux/of_device.h>
10#include <linux/of_platform.h>
David S. Millerfd531432006-06-23 15:55:17 -070011
David S. Miller8f96cd12006-06-29 15:08:02 -070012static int node_match(struct device *dev, void *data)
13{
14 struct of_device *op = to_of_device(dev);
15 struct device_node *dp = data;
16
17 return (op->node == dp);
18}
19
20struct of_device *of_find_device_by_node(struct device_node *dp)
21{
Stephen Rothwell37b77542007-04-30 17:43:56 +100022 struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
David S. Miller8f96cd12006-06-29 15:08:02 -070023 dp, node_match);
24
25 if (dev)
26 return to_of_device(dev);
27
28 return NULL;
29}
30EXPORT_SYMBOL(of_find_device_by_node);
31
David S. Miller51e0f0042008-08-25 16:44:58 -070032unsigned int irq_of_parse_and_map(struct device_node *node, int index)
David S. Miller44266212008-08-20 16:34:39 -070033{
34 struct of_device *op = of_find_device_by_node(node);
35
36 if (!op || index >= op->num_irqs)
David S. Miller51e0f0042008-08-25 16:44:58 -070037 return 0;
David S. Miller44266212008-08-20 16:34:39 -070038
39 return op->irqs[index];
40}
41EXPORT_SYMBOL(irq_of_parse_and_map);
42
David S. Miller50596252008-08-27 04:22:37 -070043/* Take the archdata values for IOMMU, STC, and HOSTDATA found in
44 * BUS and propagate to all child of_device objects.
45 */
46void of_propagate_archdata(struct of_device *bus)
47{
48 struct dev_archdata *bus_sd = &bus->dev.archdata;
49 struct device_node *bus_dp = bus->node;
50 struct device_node *dp;
51
52 for (dp = bus_dp->child; dp; dp = dp->sibling) {
53 struct of_device *op = of_find_device_by_node(dp);
54
55 op->dev.archdata.iommu = bus_sd->iommu;
56 op->dev.archdata.stc = bus_sd->stc;
57 op->dev.archdata.host_controller = bus_sd->host_controller;
58 op->dev.archdata.numa_node = bus_sd->numa_node;
59
60 if (dp->child)
61 of_propagate_archdata(op);
62 }
63}
64
Stephen Rothwell3f23de12007-05-03 02:38:57 +100065struct bus_type of_platform_bus_type;
Stephen Rothwell37b77542007-04-30 17:43:56 +100066EXPORT_SYMBOL(of_platform_bus_type);
David S. Millercf44bbc2006-06-29 14:34:50 -070067
David S. Millera83f9822006-07-12 23:19:31 -070068static inline u64 of_read_addr(const u32 *cell, int size)
David S. Millercf44bbc2006-06-29 14:34:50 -070069{
70 u64 r = 0;
71 while (size--)
72 r = (r << 32) | *(cell++);
73 return r;
74}
75
76static void __init get_cells(struct device_node *dp,
77 int *addrc, int *sizec)
78{
79 if (addrc)
80 *addrc = of_n_addr_cells(dp);
81 if (sizec)
82 *sizec = of_n_size_cells(dp);
83}
84
85/* Max address size we deal with */
86#define OF_MAX_ADDR_CELLS 4
87
88struct of_bus {
89 const char *name;
90 const char *addr_prop_name;
91 int (*match)(struct device_node *parent);
92 void (*count_cells)(struct device_node *child,
93 int *addrc, int *sizec);
David S. Millera83f9822006-07-12 23:19:31 -070094 int (*map)(u32 *addr, const u32 *range,
95 int na, int ns, int pna);
David S. Millere3c71a32008-08-28 21:02:58 -070096 unsigned long (*get_flags)(const u32 *addr, unsigned long);
David S. Millercf44bbc2006-06-29 14:34:50 -070097};
98
99/*
100 * Default translator (generic bus)
101 */
102
103static void of_bus_default_count_cells(struct device_node *dev,
104 int *addrc, int *sizec)
105{
106 get_cells(dev, addrc, sizec);
107}
108
David S. Millera83f9822006-07-12 23:19:31 -0700109/* Make sure the least significant 64-bits are in-range. Even
110 * for 3 or 4 cell values it is a good enough approximation.
111 */
112static int of_out_of_range(const u32 *addr, const u32 *base,
113 const u32 *size, int na, int ns)
David S. Millercf44bbc2006-06-29 14:34:50 -0700114{
115 u64 a = of_read_addr(addr, na);
David S. Millera83f9822006-07-12 23:19:31 -0700116 u64 b = of_read_addr(base, na);
117
118 if (a < b)
119 return 1;
120
121 b += of_read_addr(size, ns);
122 if (a >= b)
123 return 1;
124
125 return 0;
126}
127
128static int of_bus_default_map(u32 *addr, const u32 *range,
129 int na, int ns, int pna)
130{
131 u32 result[OF_MAX_ADDR_CELLS];
132 int i;
133
134 if (ns > 2) {
135 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
136 return -EINVAL;
137 }
138
139 if (of_out_of_range(addr, range, range + na + pna, na, ns))
140 return -EINVAL;
141
142 /* Start with the parent range base. */
143 memcpy(result, range + na, pna * 4);
144
145 /* Add in the child address offset. */
146 for (i = 0; i < na; i++)
147 result[pna - 1 - i] +=
148 (addr[na - 1 - i] -
149 range[na - 1 - i]);
150
151 memcpy(addr, result, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700152
153 return 0;
154}
155
David S. Millere3c71a32008-08-28 21:02:58 -0700156static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
David S. Millercf44bbc2006-06-29 14:34:50 -0700157{
David S. Millere3c71a32008-08-28 21:02:58 -0700158 if (flags)
159 return flags;
David S. Millercf44bbc2006-06-29 14:34:50 -0700160 return IORESOURCE_MEM;
161}
162
David S. Millercf44bbc2006-06-29 14:34:50 -0700163/*
164 * PCI bus specific translator
165 */
166
167static int of_bus_pci_match(struct device_node *np)
168{
David S. Millera83f9822006-07-12 23:19:31 -0700169 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
170 /* Do not do PCI specific frobbing if the
171 * PCI bridge lacks a ranges property. We
172 * want to pass it through up to the next
173 * parent as-is, not with the PCI translate
174 * method which chops off the top address cell.
175 */
176 if (!of_find_property(np, "ranges", NULL))
177 return 0;
178
179 return 1;
180 }
181
182 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700183}
184
185static void of_bus_pci_count_cells(struct device_node *np,
186 int *addrc, int *sizec)
187{
188 if (addrc)
189 *addrc = 3;
190 if (sizec)
191 *sizec = 2;
192}
193
David S. Millera83f9822006-07-12 23:19:31 -0700194static int of_bus_pci_map(u32 *addr, const u32 *range,
195 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700196{
David S. Millera83f9822006-07-12 23:19:31 -0700197 u32 result[OF_MAX_ADDR_CELLS];
198 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700199
200 /* Check address type match */
201 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -0700202 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700203
David S. Millera83f9822006-07-12 23:19:31 -0700204 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
205 na - 1, ns))
206 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700207
David S. Millera83f9822006-07-12 23:19:31 -0700208 /* Start with the parent range base. */
209 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700210
David S. Millera83f9822006-07-12 23:19:31 -0700211 /* Add in the child address offset, skipping high cell. */
212 for (i = 0; i < na - 1; i++)
213 result[pna - 1 - i] +=
214 (addr[na - 1 - i] -
215 range[na - 1 - i]);
216
217 memcpy(addr, result, pna * 4);
218
219 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700220}
221
David S. Millere3c71a32008-08-28 21:02:58 -0700222static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
David S. Millercf44bbc2006-06-29 14:34:50 -0700223{
David S. Millercf44bbc2006-06-29 14:34:50 -0700224 u32 w = addr[0];
225
David S. Millere3c71a32008-08-28 21:02:58 -0700226 /* For PCI, we override whatever child busses may have used. */
227 flags = 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700228 switch((w >> 24) & 0x03) {
229 case 0x01:
230 flags |= IORESOURCE_IO;
David S. Millere3c71a32008-08-28 21:02:58 -0700231 break;
232
David S. Millercf44bbc2006-06-29 14:34:50 -0700233 case 0x02: /* 32 bits */
234 case 0x03: /* 64 bits */
235 flags |= IORESOURCE_MEM;
David S. Millere3c71a32008-08-28 21:02:58 -0700236 break;
David S. Millercf44bbc2006-06-29 14:34:50 -0700237 }
238 if (w & 0x40000000)
239 flags |= IORESOURCE_PREFETCH;
240 return flags;
241}
242
243/*
244 * SBUS bus specific translator
245 */
246
247static int of_bus_sbus_match(struct device_node *np)
248{
David S. Miller956d0392009-04-17 04:14:15 -0700249 struct device_node *dp = np;
250
251 while (dp) {
252 if (!strcmp(dp->name, "sbus") ||
253 !strcmp(dp->name, "sbi"))
254 return 1;
255
256 /* Have a look at use_1to1_mapping(). We're trying
257 * to match SBUS if that's the top-level bus and we
258 * don't have some intervening real bus that provides
259 * ranges based translations.
260 */
261 if (of_find_property(dp, "ranges", NULL) != NULL)
262 break;
263
264 dp = dp->parent;
265 }
266
267 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700268}
269
270static void of_bus_sbus_count_cells(struct device_node *child,
271 int *addrc, int *sizec)
272{
273 if (addrc)
274 *addrc = 2;
275 if (sizec)
276 *sizec = 1;
277}
278
David S. Millera83f9822006-07-12 23:19:31 -0700279static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700280{
281 return of_bus_default_map(addr, range, na, ns, pna);
282}
283
David S. Millerbdba4d62008-09-10 23:38:51 -0700284static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
David S. Millercf44bbc2006-06-29 14:34:50 -0700285{
286 return IORESOURCE_MEM;
287}
288
289
290/*
291 * Array of bus specific translators
292 */
293
294static struct of_bus of_busses[] = {
295 /* PCI */
296 {
297 .name = "pci",
298 .addr_prop_name = "assigned-addresses",
299 .match = of_bus_pci_match,
300 .count_cells = of_bus_pci_count_cells,
301 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700302 .get_flags = of_bus_pci_get_flags,
303 },
304 /* SBUS */
305 {
306 .name = "sbus",
307 .addr_prop_name = "reg",
308 .match = of_bus_sbus_match,
309 .count_cells = of_bus_sbus_count_cells,
310 .map = of_bus_sbus_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700311 .get_flags = of_bus_sbus_get_flags,
312 },
313 /* Default */
314 {
315 .name = "default",
316 .addr_prop_name = "reg",
317 .match = NULL,
318 .count_cells = of_bus_default_count_cells,
319 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700320 .get_flags = of_bus_default_get_flags,
321 },
322};
323
324static struct of_bus *of_match_bus(struct device_node *np)
325{
326 int i;
327
328 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
329 if (!of_busses[i].match || of_busses[i].match(np))
330 return &of_busses[i];
331 BUG();
332 return NULL;
333}
334
335static int __init build_one_resource(struct device_node *parent,
336 struct of_bus *bus,
337 struct of_bus *pbus,
338 u32 *addr,
339 int na, int ns, int pna)
340{
Stephen Rothwell8271f042007-03-29 00:47:23 -0700341 const u32 *ranges;
David S. Millercf44bbc2006-06-29 14:34:50 -0700342 unsigned int rlen;
343 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700344
345 ranges = of_get_property(parent, "ranges", &rlen);
346 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700347 u32 result[OF_MAX_ADDR_CELLS];
348 int i;
349
350 memset(result, 0, pna * 4);
351 for (i = 0; i < na; i++)
352 result[pna - 1 - i] =
353 addr[na - 1 - i];
354
355 memcpy(addr, result, pna * 4);
356 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700357 }
358
359 /* Now walk through the ranges */
360 rlen /= 4;
361 rone = na + pna + ns;
362 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700363 if (!bus->map(addr, ranges, na, ns, pna))
364 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700365 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700366
David S. Millera83f9822006-07-12 23:19:31 -0700367 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700368}
369
David S. Miller52802672008-09-03 02:04:41 -0700370static int __init use_1to1_mapping(struct device_node *pp)
371{
372 /* If we have a ranges property in the parent, use it. */
373 if (of_find_property(pp, "ranges", NULL) != NULL)
374 return 0;
375
376 /* Some SBUS devices use intermediate nodes to express
377 * hierarchy within the device itself. These aren't
378 * real bus nodes, and don't have a 'ranges' property.
379 * But, we should still pass the translation work up
380 * to the SBUS itself.
381 */
382 if (!strcmp(pp->name, "dma") ||
383 !strcmp(pp->name, "espdma") ||
384 !strcmp(pp->name, "ledma") ||
385 !strcmp(pp->name, "lebuffer"))
386 return 0;
387
388 return 1;
389}
390
David S. Millera83f9822006-07-12 23:19:31 -0700391static int of_resource_verbose;
392
David S. Millercf44bbc2006-06-29 14:34:50 -0700393static void __init build_device_resources(struct of_device *op,
394 struct device *parent)
395{
396 struct of_device *p_op;
397 struct of_bus *bus;
398 int na, ns;
399 int index, num_reg;
Stephen Rothwell8271f042007-03-29 00:47:23 -0700400 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700401
402 if (!parent)
403 return;
404
405 p_op = to_of_device(parent);
406 bus = of_match_bus(p_op->node);
407 bus->count_cells(op->node, &na, &ns);
408
409 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
410 if (!preg || num_reg == 0)
411 return;
412
413 /* Convert to num-cells. */
414 num_reg /= 4;
415
416 /* Conver to num-entries. */
417 num_reg /= na + ns;
418
419 for (index = 0; index < num_reg; index++) {
420 struct resource *r = &op->resource[index];
421 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell8271f042007-03-29 00:47:23 -0700422 const u32 *reg = (preg + (index * ((na + ns) * 4)));
David S. Millercf44bbc2006-06-29 14:34:50 -0700423 struct device_node *dp = op->node;
424 struct device_node *pp = p_op->node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800425 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700426 u64 size, result = OF_BAD_ADDR;
427 unsigned long flags;
428 int dna, dns;
429 int pna, pns;
430
431 size = of_read_addr(reg + na, ns);
David S. Millercf44bbc2006-06-29 14:34:50 -0700432
433 memcpy(addr, reg, na * 4);
434
David S. Millere3c71a32008-08-28 21:02:58 -0700435 flags = bus->get_flags(reg, 0);
436
David S. Miller52802672008-09-03 02:04:41 -0700437 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700438 result = of_read_addr(addr, na);
439 goto build_res;
440 }
441
442 dna = na;
443 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800444 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700445
446 while (1) {
447 dp = pp;
448 pp = dp->parent;
449 if (!pp) {
450 result = of_read_addr(addr, dna);
451 break;
452 }
453
454 pbus = of_match_bus(pp);
455 pbus->count_cells(dp, &pna, &pns);
456
David S. Millerb85cdd42007-02-28 23:20:12 -0800457 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700458 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700459 break;
460
David S. Millere3c71a32008-08-28 21:02:58 -0700461 flags = pbus->get_flags(addr, flags);
462
David S. Millercf44bbc2006-06-29 14:34:50 -0700463 dna = pna;
464 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800465 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700466 }
467
468 build_res:
469 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700470
471 if (of_resource_verbose)
472 printk("%s reg[%d] -> %llx\n",
473 op->node->full_name, index,
474 result);
475
David S. Millercf44bbc2006-06-29 14:34:50 -0700476 if (result != OF_BAD_ADDR) {
David S. Miller95714e12006-06-29 14:35:14 -0700477 r->start = result & 0xffffffff;
David S. Millercf44bbc2006-06-29 14:34:50 -0700478 r->end = result + size - 1;
David S. Miller95714e12006-06-29 14:35:14 -0700479 r->flags = flags | ((result >> 32ULL) & 0xffUL);
David S. Millercf44bbc2006-06-29 14:34:50 -0700480 }
481 r->name = op->node->name;
482 }
483}
484
485static struct of_device * __init scan_one_device(struct device_node *dp,
486 struct device *parent)
487{
488 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell8271f042007-03-29 00:47:23 -0700489 const struct linux_prom_irqs *intr;
David S. Miller3d6e4702007-07-18 22:03:25 -0700490 struct dev_archdata *sd;
David S. Miller8f96cd12006-06-29 15:08:02 -0700491 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700492
493 if (!op)
494 return NULL;
495
David S. Miller3d6e4702007-07-18 22:03:25 -0700496 sd = &op->dev.archdata;
497 sd->prom_node = dp;
498 sd->op = op;
499
David S. Millercf44bbc2006-06-29 14:34:50 -0700500 op->node = dp;
501
502 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
503 (25*1000*1000));
504 op->portid = of_getintprop_default(dp, "upa-portid", -1);
505 if (op->portid == -1)
506 op->portid = of_getintprop_default(dp, "portid", -1);
507
David S. Miller8f96cd12006-06-29 15:08:02 -0700508 intr = of_get_property(dp, "intr", &len);
509 if (intr) {
510 op->num_irqs = len / sizeof(struct linux_prom_irqs);
511 for (i = 0; i < op->num_irqs; i++)
512 op->irqs[i] = intr[i].pri;
513 } else {
Stephen Rothwell8271f042007-03-29 00:47:23 -0700514 const unsigned int *irq =
515 of_get_property(dp, "interrupts", &len);
David S. Miller8f96cd12006-06-29 15:08:02 -0700516
517 if (irq) {
518 op->num_irqs = len / sizeof(unsigned int);
519 for (i = 0; i < op->num_irqs; i++)
520 op->irqs[i] = irq[i];
521 } else {
522 op->num_irqs = 0;
523 }
524 }
525 if (sparc_cpu_model == sun4d) {
526 static int pil_to_sbus[] = {
527 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
528 };
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700529 struct device_node *io_unit, *sbi = dp->parent;
Stephen Rothwell8271f042007-03-29 00:47:23 -0700530 const struct linux_prom_registers *regs;
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700531 int board, slot;
532
533 while (sbi) {
534 if (!strcmp(sbi->name, "sbi"))
535 break;
536
537 sbi = sbi->parent;
538 }
539 if (!sbi)
540 goto build_resources;
David S. Miller8f96cd12006-06-29 15:08:02 -0700541
542 regs = of_get_property(dp, "reg", NULL);
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700543 if (!regs)
544 goto build_resources;
545
David S. Miller8f96cd12006-06-29 15:08:02 -0700546 slot = regs->which_io;
547
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700548 /* If SBI's parent is not io-unit or the io-unit lacks
549 * a "board#" property, something is very wrong.
550 */
551 if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
552 printk("%s: Error, parent is not io-unit.\n",
553 sbi->full_name);
554 goto build_resources;
555 }
556 io_unit = sbi->parent;
557 board = of_getintprop_default(io_unit, "board#", -1);
558 if (board == -1) {
559 printk("%s: Error, lacks board# property.\n",
560 io_unit->full_name);
561 goto build_resources;
562 }
563
David S. Miller8f96cd12006-06-29 15:08:02 -0700564 for (i = 0; i < op->num_irqs; i++) {
565 int this_irq = op->irqs[i];
566 int sbusl = pil_to_sbus[this_irq];
567
568 if (sbusl)
569 this_irq = (((board + 1) << 5) +
570 (sbusl << 2) +
571 slot);
572
573 op->irqs[i] = this_irq;
574 }
575 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700576
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700577build_resources:
David S. Millercf44bbc2006-06-29 14:34:50 -0700578 build_device_resources(op, parent);
579
580 op->dev.parent = parent;
Stephen Rothwell37b77542007-04-30 17:43:56 +1000581 op->dev.bus = &of_platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700582 if (!parent)
Kay Sievers5acdc1f2008-11-12 23:48:40 -0800583 dev_set_name(&op->dev, "root");
David S. Millercf44bbc2006-06-29 14:34:50 -0700584 else
Kay Sievers5acdc1f2008-11-12 23:48:40 -0800585 dev_set_name(&op->dev, "%08x", dp->node);
David S. Millercf44bbc2006-06-29 14:34:50 -0700586
587 if (of_device_register(op)) {
588 printk("%s: Could not register of device.\n",
589 dp->full_name);
590 kfree(op);
591 op = NULL;
592 }
593
594 return op;
595}
596
597static void __init scan_tree(struct device_node *dp, struct device *parent)
598{
599 while (dp) {
600 struct of_device *op = scan_one_device(dp, parent);
601
602 if (op)
603 scan_tree(dp->child, &op->dev);
604
605 dp = dp->sibling;
606 }
607}
608
609static void __init scan_of_devices(void)
610{
611 struct device_node *root = of_find_node_by_path("/");
612 struct of_device *parent;
613
614 parent = scan_one_device(root, NULL);
615 if (!parent)
616 return;
617
618 scan_tree(root->child, &parent->dev);
619}
620
David S. Millerfd531432006-06-23 15:55:17 -0700621static int __init of_bus_driver_init(void)
622{
David S. Millercf44bbc2006-06-29 14:34:50 -0700623 int err;
David S. Millerfd531432006-06-23 15:55:17 -0700624
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000625 err = of_bus_type_init(&of_platform_bus_type, "of");
David S. Millercf44bbc2006-06-29 14:34:50 -0700626 if (!err)
627 scan_of_devices();
628
629 return err;
David S. Millerfd531432006-06-23 15:55:17 -0700630}
631
632postcore_initcall(of_bus_driver_init);
633
David S. Millera83f9822006-07-12 23:19:31 -0700634static int __init of_debug(char *str)
635{
636 int val = 0;
637
638 get_option(&str, &val);
639 if (val & 1)
640 of_resource_verbose = 1;
641 return 1;
642}
643
644__setup("of_debug=", of_debug);