sparc: Kill SBUS layer IRQ hooks.
[linux-2.6] / arch / sparc64 / kernel / of_device.c
1 #include <linux/string.h>
2 #include <linux/kernel.h>
3 #include <linux/of.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
8 #include <linux/errno.h>
9 #include <linux/irq.h>
10 #include <linux/of_device.h>
11 #include <linux/of_platform.h>
12
13 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
14 {
15         unsigned long ret = res->start + offset;
16         struct resource *r;
17
18         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)
23                 ret = 0;
24
25         return (void __iomem *) ret;
26 }
27 EXPORT_SYMBOL(of_ioremap);
28
29 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
30 {
31         if (res->flags & IORESOURCE_MEM)
32                 release_mem_region((unsigned long) base, size);
33         else
34                 release_region((unsigned long) base, size);
35 }
36 EXPORT_SYMBOL(of_iounmap);
37
38 static 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
46 struct of_device *of_find_device_by_node(struct device_node *dp)
47 {
48         struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
49                                              dp, node_match);
50
51         if (dev)
52                 return to_of_device(dev);
53
54         return NULL;
55 }
56 EXPORT_SYMBOL(of_find_device_by_node);
57
58 unsigned int 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 0;
64
65         return op->irqs[index];
66 }
67 EXPORT_SYMBOL(irq_of_parse_and_map);
68
69 #ifdef CONFIG_PCI
70 struct bus_type ebus_bus_type;
71 EXPORT_SYMBOL(ebus_bus_type);
72 #endif
73
74 #ifdef CONFIG_SBUS
75 struct bus_type sbus_bus_type;
76 EXPORT_SYMBOL(sbus_bus_type);
77 #endif
78
79 struct bus_type of_platform_bus_type;
80 EXPORT_SYMBOL(of_platform_bus_type);
81
82 static inline u64 of_read_addr(const u32 *cell, int size)
83 {
84         u64 r = 0;
85         while (size--)
86                 r = (r << 32) | *(cell++);
87         return r;
88 }
89
90 static 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
102 struct 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);
108         int             (*map)(u32 *addr, const u32 *range,
109                                int na, int ns, int pna);
110         unsigned int    (*get_flags)(const u32 *addr);
111 };
112
113 /*
114  * Default translator (generic bus)
115  */
116
117 static void of_bus_default_count_cells(struct device_node *dev,
118                                        int *addrc, int *sizec)
119 {
120         get_cells(dev, addrc, sizec);
121 }
122
123 /* 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  */
126 static int of_out_of_range(const u32 *addr, const u32 *base,
127                            const u32 *size, int na, int ns)
128 {
129         u64 a = of_read_addr(addr, na);
130         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
142 static 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);
166
167         return 0;
168 }
169
170 static unsigned int of_bus_default_get_flags(const u32 *addr)
171 {
172         return IORESOURCE_MEM;
173 }
174
175 /*
176  * PCI bus specific translator
177  */
178
179 static int of_bus_pci_match(struct device_node *np)
180 {
181         if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
182                 const char *model = of_get_property(np, "model", NULL);
183
184                 if (model && !strcmp(model, "SUNW,simba"))
185                         return 0;
186
187                 /* 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;
200 }
201
202 static int of_bus_simba_match(struct device_node *np)
203 {
204         const char *model = of_get_property(np, "model", NULL);
205
206         if (model && !strcmp(model, "SUNW,simba"))
207                 return 1;
208
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
217         return 0;
218 }
219
220 static int of_bus_simba_map(u32 *addr, const u32 *range,
221                             int na, int ns, int pna)
222 {
223         return 0;
224 }
225
226 static 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
235 static int of_bus_pci_map(u32 *addr, const u32 *range,
236                           int na, int ns, int pna)
237 {
238         u32 result[OF_MAX_ADDR_CELLS];
239         int i;
240
241         /* Check address type match */
242         if ((addr[0] ^ range[0]) & 0x03000000)
243                 return -EINVAL;
244
245         if (of_out_of_range(addr + 1, range + 1, range + na + pna,
246                             na - 1, ns))
247                 return -EINVAL;
248
249         /* Start with the parent range base.  */
250         memcpy(result, range + na, pna * 4);
251
252         /* 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;
261 }
262
263 static unsigned int of_bus_pci_get_flags(const u32 *addr)
264 {
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 /*
281  * SBUS bus specific translator
282  */
283
284 static int of_bus_sbus_match(struct device_node *np)
285 {
286         return !strcmp(np->name, "sbus") ||
287                 !strcmp(np->name, "sbi");
288 }
289
290 static 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
299 /*
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  */
308 static int of_bus_fhc_match(struct device_node *np)
309 {
310         return !strcmp(np->name, "fhc") ||
311                 !strcmp(np->name, "central");
312 }
313
314 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
315
316 /*
317  * Array of bus specific translators
318  */
319
320 static 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,
328                 .get_flags = of_bus_pci_get_flags,
329         },
330         /* 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         },
339         /* SBUS */
340         {
341                 .name = "sbus",
342                 .addr_prop_name = "reg",
343                 .match = of_bus_sbus_match,
344                 .count_cells = of_bus_sbus_count_cells,
345                 .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,
356         },
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,
364                 .get_flags = of_bus_default_get_flags,
365         },
366 };
367
368 static 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
379 static 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 {
385         const u32 *ranges;
386         unsigned int rlen;
387         int rone;
388
389         ranges = of_get_property(parent, "ranges", &rlen);
390         if (ranges == NULL || rlen == 0) {
391                 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;
401         }
402
403         /* Now walk through the ranges */
404         rlen /= 4;
405         rone = na + pna + ns;
406         for (; rlen >= rone; rlen -= rone, ranges += rone) {
407                 if (!bus->map(addr, ranges, na, ns, pna))
408                         return 0;
409         }
410
411         /* 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
418         return 1;
419 }
420
421 static int __init use_1to1_mapping(struct device_node *pp)
422 {
423         /* If we have a ranges property in the parent, use it.  */
424         if (of_find_property(pp, "ranges", NULL) != NULL)
425                 return 0;
426
427         /* 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
433         /* 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"))
438                 return 0;
439
440         return 1;
441 }
442
443 static int of_resource_verbose;
444
445 static 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;
452         const void *preg;
453
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
468         /* Convert to num-entries.  */
469         num_reg /= na + ns;
470
471         /* Prevent overrunning the op->resources[] array.  */
472         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
479         for (index = 0; index < num_reg; index++) {
480                 struct resource *r = &op->resource[index];
481                 u32 addr[OF_MAX_ADDR_CELLS];
482                 const u32 *reg = (preg + (index * ((na + ns) * 4)));
483                 struct device_node *dp = op->node;
484                 struct device_node *pp = p_op->node;
485                 struct of_bus *pbus, *dbus;
486                 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
496                 if (use_1to1_mapping(pp)) {
497                         result = of_read_addr(addr, na);
498                         goto build_res;
499                 }
500
501                 dna = na;
502                 dns = ns;
503                 dbus = bus;
504
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
516                         if (build_one_resource(dp, dbus, pbus, addr,
517                                                dna, dns, pna))
518                                 break;
519
520                         dna = pna;
521                         dns = pns;
522                         dbus = pbus;
523                 }
524
525         build_res:
526                 memset(r, 0, sizeof(*r));
527
528                 if (of_resource_verbose)
529                         printk("%s reg[%d] -> %lx\n",
530                                op->node->full_name, index,
531                                result);
532
533                 if (result != OF_BAD_ADDR) {
534                         if (tlb_type == hypervisor)
535                                 result &= 0x0fffffffffffffffUL;
536
537                         r->start = result;
538                         r->end = result + size - 1;
539                         r->flags = flags;
540                 }
541                 r->name = op->node->name;
542         }
543 }
544
545 static struct device_node * __init
546 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
547                     const u32 *imap, int imlen, const u32 *imask,
548                     unsigned int *irq_p)
549 {
550         struct device_node *cp;
551         unsigned int irq = *irq_p;
552         struct of_bus *bus;
553         phandle handle;
554         const u32 *reg;
555         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         }
582         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
596                 return NULL;
597         }
598
599         *irq_p = irq;
600         cp = of_find_node_by_phandle(handle);
601
602         return cp;
603 }
604
605 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
606                                            struct device_node *pp,
607                                            unsigned int irq)
608 {
609         const struct linux_prom_pci_registers *regs;
610         unsigned int bus, devfn, slot, ret;
611
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
619         bus = (regs->phys_hi >> 16) & 0xff;
620         devfn = (regs->phys_hi >> 8) & 0xff;
621         slot = (devfn >> 3) & 0x1f;
622
623         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
634                  */
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         }
653
654         return ret;
655 }
656
657 static int of_irq_verbose;
658
659 static 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;
666         int nid;
667
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);
674
675                 if (of_irq_verbose)
676                         printk("%s: direct translate %x --> %x\n",
677                                dp->full_name, orig_irq, irq);
678
679                 goto out;
680         }
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) {
693                 const void *imap, *imsk;
694                 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);
705
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
712                         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);
725                                 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
732                         }
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);
747         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);
750
751 out:
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
759         return irq;
760 }
761
762 static 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);
766         const unsigned int *irq;
767         struct dev_archdata *sd;
768         int len, i;
769
770         if (!op)
771                 return NULL;
772
773         sd = &op->dev.archdata;
774         sd->prom_node = dp;
775         sd->op = op;
776
777         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);
786         if (irq) {
787                 memcpy(op->irqs, irq, len);
788                 op->num_irqs = len / 4;
789         } else {
790                 op->num_irqs = 0;
791         }
792
793         /* Prevent overrunning the op->irqs[] array.  */
794         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
801         build_device_resources(op, parent);
802         for (i = 0; i < op->num_irqs; i++)
803                 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
804
805         op->dev.parent = parent;
806         op->dev.bus = &of_platform_bus_type;
807         if (!parent)
808                 dev_set_name(&op->dev, "root");
809         else
810                 dev_set_name(&op->dev, "%08x", dp->node);
811
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
822 static 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
834 static 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
846 static int __init of_bus_driver_init(void)
847 {
848         int err;
849
850         err = of_bus_type_init(&of_platform_bus_type, "of");
851 #ifdef CONFIG_PCI
852         if (!err)
853                 err = of_bus_type_init(&ebus_bus_type, "ebus");
854 #endif
855 #ifdef CONFIG_SBUS
856         if (!err)
857                 err = of_bus_type_init(&sbus_bus_type, "sbus");
858 #endif
859
860         if (!err)
861                 scan_of_devices();
862
863         return err;
864 }
865
866 postcore_initcall(of_bus_driver_init);
867
868 static 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);