Merge master.kernel.org:/home/rmk/linux-2.6-serial
[pandora-kernel.git] / arch / sparc64 / kernel / of_device.c
1 #include <linux/config.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
8
9 #include <asm/errno.h>
10 #include <asm/of_device.h>
11
12 /**
13  * of_match_device - Tell if an of_device structure has a matching
14  * of_match structure
15  * @ids: array of of device match structures to search in
16  * @dev: the of device structure to match against
17  *
18  * Used by a driver to check whether an of_device present in the
19  * system is in its list of supported devices.
20  */
21 const struct of_device_id *of_match_device(const struct of_device_id *matches,
22                                         const struct of_device *dev)
23 {
24         if (!dev->node)
25                 return NULL;
26         while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
27                 int match = 1;
28                 if (matches->name[0])
29                         match &= dev->node->name
30                                 && !strcmp(matches->name, dev->node->name);
31                 if (matches->type[0])
32                         match &= dev->node->type
33                                 && !strcmp(matches->type, dev->node->type);
34                 if (matches->compatible[0])
35                         match &= of_device_is_compatible(dev->node,
36                                                          matches->compatible);
37                 if (match)
38                         return matches;
39                 matches++;
40         }
41         return NULL;
42 }
43
44 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
45 {
46         struct of_device * of_dev = to_of_device(dev);
47         struct of_platform_driver * of_drv = to_of_platform_driver(drv);
48         const struct of_device_id * matches = of_drv->match_table;
49
50         if (!matches)
51                 return 0;
52
53         return of_match_device(matches, of_dev) != NULL;
54 }
55
56 struct of_device *of_dev_get(struct of_device *dev)
57 {
58         struct device *tmp;
59
60         if (!dev)
61                 return NULL;
62         tmp = get_device(&dev->dev);
63         if (tmp)
64                 return to_of_device(tmp);
65         else
66                 return NULL;
67 }
68
69 void of_dev_put(struct of_device *dev)
70 {
71         if (dev)
72                 put_device(&dev->dev);
73 }
74
75
76 static int of_device_probe(struct device *dev)
77 {
78         int error = -ENODEV;
79         struct of_platform_driver *drv;
80         struct of_device *of_dev;
81         const struct of_device_id *match;
82
83         drv = to_of_platform_driver(dev->driver);
84         of_dev = to_of_device(dev);
85
86         if (!drv->probe)
87                 return error;
88
89         of_dev_get(of_dev);
90
91         match = of_match_device(drv->match_table, of_dev);
92         if (match)
93                 error = drv->probe(of_dev, match);
94         if (error)
95                 of_dev_put(of_dev);
96
97         return error;
98 }
99
100 static int of_device_remove(struct device *dev)
101 {
102         struct of_device * of_dev = to_of_device(dev);
103         struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
104
105         if (dev->driver && drv->remove)
106                 drv->remove(of_dev);
107         return 0;
108 }
109
110 static int of_device_suspend(struct device *dev, pm_message_t state)
111 {
112         struct of_device * of_dev = to_of_device(dev);
113         struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
114         int error = 0;
115
116         if (dev->driver && drv->suspend)
117                 error = drv->suspend(of_dev, state);
118         return error;
119 }
120
121 static int of_device_resume(struct device * dev)
122 {
123         struct of_device * of_dev = to_of_device(dev);
124         struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
125         int error = 0;
126
127         if (dev->driver && drv->resume)
128                 error = drv->resume(of_dev);
129         return error;
130 }
131
132 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
133 {
134         unsigned long ret = res->start + offset;
135
136         if (!request_region(ret, size, name))
137                 ret = 0;
138
139         return (void __iomem *) ret;
140 }
141 EXPORT_SYMBOL(of_ioremap);
142
143 void of_iounmap(void __iomem *base, unsigned long size)
144 {
145         release_region((unsigned long) base, size);
146 }
147 EXPORT_SYMBOL(of_iounmap);
148
149 static int node_match(struct device *dev, void *data)
150 {
151         struct of_device *op = to_of_device(dev);
152         struct device_node *dp = data;
153
154         return (op->node == dp);
155 }
156
157 struct of_device *of_find_device_by_node(struct device_node *dp)
158 {
159         struct device *dev = bus_find_device(&of_bus_type, NULL,
160                                              dp, node_match);
161
162         if (dev)
163                 return to_of_device(dev);
164
165         return NULL;
166 }
167 EXPORT_SYMBOL(of_find_device_by_node);
168
169 #ifdef CONFIG_PCI
170 struct bus_type isa_bus_type = {
171        .name    = "isa",
172        .match   = of_platform_bus_match,
173        .probe   = of_device_probe,
174        .remove  = of_device_remove,
175        .suspend = of_device_suspend,
176        .resume  = of_device_resume,
177 };
178 EXPORT_SYMBOL(isa_bus_type);
179
180 struct bus_type ebus_bus_type = {
181        .name    = "ebus",
182        .match   = of_platform_bus_match,
183        .probe   = of_device_probe,
184        .remove  = of_device_remove,
185        .suspend = of_device_suspend,
186        .resume  = of_device_resume,
187 };
188 EXPORT_SYMBOL(ebus_bus_type);
189 #endif
190
191 #ifdef CONFIG_SBUS
192 struct bus_type sbus_bus_type = {
193        .name    = "sbus",
194        .match   = of_platform_bus_match,
195        .probe   = of_device_probe,
196        .remove  = of_device_remove,
197        .suspend = of_device_suspend,
198        .resume  = of_device_resume,
199 };
200 EXPORT_SYMBOL(sbus_bus_type);
201 #endif
202
203 struct bus_type of_bus_type = {
204        .name    = "of",
205        .match   = of_platform_bus_match,
206        .probe   = of_device_probe,
207        .remove  = of_device_remove,
208        .suspend = of_device_suspend,
209        .resume  = of_device_resume,
210 };
211 EXPORT_SYMBOL(of_bus_type);
212
213 static inline u64 of_read_addr(const u32 *cell, int size)
214 {
215         u64 r = 0;
216         while (size--)
217                 r = (r << 32) | *(cell++);
218         return r;
219 }
220
221 static void __init get_cells(struct device_node *dp,
222                              int *addrc, int *sizec)
223 {
224         if (addrc)
225                 *addrc = of_n_addr_cells(dp);
226         if (sizec)
227                 *sizec = of_n_size_cells(dp);
228 }
229
230 /* Max address size we deal with */
231 #define OF_MAX_ADDR_CELLS       4
232
233 struct of_bus {
234         const char      *name;
235         const char      *addr_prop_name;
236         int             (*match)(struct device_node *parent);
237         void            (*count_cells)(struct device_node *child,
238                                        int *addrc, int *sizec);
239         int             (*map)(u32 *addr, const u32 *range,
240                                int na, int ns, int pna);
241         unsigned int    (*get_flags)(u32 *addr);
242 };
243
244 /*
245  * Default translator (generic bus)
246  */
247
248 static void of_bus_default_count_cells(struct device_node *dev,
249                                        int *addrc, int *sizec)
250 {
251         get_cells(dev, addrc, sizec);
252 }
253
254 /* Make sure the least significant 64-bits are in-range.  Even
255  * for 3 or 4 cell values it is a good enough approximation.
256  */
257 static int of_out_of_range(const u32 *addr, const u32 *base,
258                            const u32 *size, int na, int ns)
259 {
260         u64 a = of_read_addr(addr, na);
261         u64 b = of_read_addr(base, na);
262
263         if (a < b)
264                 return 1;
265
266         b += of_read_addr(size, ns);
267         if (a >= b)
268                 return 1;
269
270         return 0;
271 }
272
273 static int of_bus_default_map(u32 *addr, const u32 *range,
274                               int na, int ns, int pna)
275 {
276         u32 result[OF_MAX_ADDR_CELLS];
277         int i;
278
279         if (ns > 2) {
280                 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
281                 return -EINVAL;
282         }
283
284         if (of_out_of_range(addr, range, range + na + pna, na, ns))
285                 return -EINVAL;
286
287         /* Start with the parent range base.  */
288         memcpy(result, range + na, pna * 4);
289
290         /* Add in the child address offset.  */
291         for (i = 0; i < na; i++)
292                 result[pna - 1 - i] +=
293                         (addr[na - 1 - i] -
294                          range[na - 1 - i]);
295
296         memcpy(addr, result, pna * 4);
297
298         return 0;
299 }
300
301 static unsigned int of_bus_default_get_flags(u32 *addr)
302 {
303         return IORESOURCE_MEM;
304 }
305
306 /*
307  * PCI bus specific translator
308  */
309
310 static int of_bus_pci_match(struct device_node *np)
311 {
312         if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
313                 /* Do not do PCI specific frobbing if the
314                  * PCI bridge lacks a ranges property.  We
315                  * want to pass it through up to the next
316                  * parent as-is, not with the PCI translate
317                  * method which chops off the top address cell.
318                  */
319                 if (!of_find_property(np, "ranges", NULL))
320                         return 0;
321
322                 return 1;
323         }
324
325         return 0;
326 }
327
328 static void of_bus_pci_count_cells(struct device_node *np,
329                                    int *addrc, int *sizec)
330 {
331         if (addrc)
332                 *addrc = 3;
333         if (sizec)
334                 *sizec = 2;
335 }
336
337 static int of_bus_pci_map(u32 *addr, const u32 *range,
338                           int na, int ns, int pna)
339 {
340         u32 result[OF_MAX_ADDR_CELLS];
341         int i;
342
343         /* Check address type match */
344         if ((addr[0] ^ range[0]) & 0x03000000)
345                 return -EINVAL;
346
347         if (of_out_of_range(addr + 1, range + 1, range + na + pna,
348                             na - 1, ns))
349                 return -EINVAL;
350
351         /* Start with the parent range base.  */
352         memcpy(result, range + na, pna * 4);
353
354         /* Add in the child address offset, skipping high cell.  */
355         for (i = 0; i < na - 1; i++)
356                 result[pna - 1 - i] +=
357                         (addr[na - 1 - i] -
358                          range[na - 1 - i]);
359
360         memcpy(addr, result, pna * 4);
361
362         return 0;
363 }
364
365 static unsigned int of_bus_pci_get_flags(u32 *addr)
366 {
367         unsigned int flags = 0;
368         u32 w = addr[0];
369
370         switch((w >> 24) & 0x03) {
371         case 0x01:
372                 flags |= IORESOURCE_IO;
373         case 0x02: /* 32 bits */
374         case 0x03: /* 64 bits */
375                 flags |= IORESOURCE_MEM;
376         }
377         if (w & 0x40000000)
378                 flags |= IORESOURCE_PREFETCH;
379         return flags;
380 }
381
382 /*
383  * SBUS bus specific translator
384  */
385
386 static int of_bus_sbus_match(struct device_node *np)
387 {
388         return !strcmp(np->name, "sbus") ||
389                 !strcmp(np->name, "sbi");
390 }
391
392 static void of_bus_sbus_count_cells(struct device_node *child,
393                                    int *addrc, int *sizec)
394 {
395         if (addrc)
396                 *addrc = 2;
397         if (sizec)
398                 *sizec = 1;
399 }
400
401 static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
402 {
403         return of_bus_default_map(addr, range, na, ns, pna);
404 }
405
406 static unsigned int of_bus_sbus_get_flags(u32 *addr)
407 {
408         return IORESOURCE_MEM;
409 }
410
411
412 /*
413  * Array of bus specific translators
414  */
415
416 static struct of_bus of_busses[] = {
417         /* PCI */
418         {
419                 .name = "pci",
420                 .addr_prop_name = "assigned-addresses",
421                 .match = of_bus_pci_match,
422                 .count_cells = of_bus_pci_count_cells,
423                 .map = of_bus_pci_map,
424                 .get_flags = of_bus_pci_get_flags,
425         },
426         /* SBUS */
427         {
428                 .name = "sbus",
429                 .addr_prop_name = "reg",
430                 .match = of_bus_sbus_match,
431                 .count_cells = of_bus_sbus_count_cells,
432                 .map = of_bus_sbus_map,
433                 .get_flags = of_bus_sbus_get_flags,
434         },
435         /* Default */
436         {
437                 .name = "default",
438                 .addr_prop_name = "reg",
439                 .match = NULL,
440                 .count_cells = of_bus_default_count_cells,
441                 .map = of_bus_default_map,
442                 .get_flags = of_bus_default_get_flags,
443         },
444 };
445
446 static struct of_bus *of_match_bus(struct device_node *np)
447 {
448         int i;
449
450         for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
451                 if (!of_busses[i].match || of_busses[i].match(np))
452                         return &of_busses[i];
453         BUG();
454         return NULL;
455 }
456
457 static int __init build_one_resource(struct device_node *parent,
458                                      struct of_bus *bus,
459                                      struct of_bus *pbus,
460                                      u32 *addr,
461                                      int na, int ns, int pna)
462 {
463         u32 *ranges;
464         unsigned int rlen;
465         int rone;
466
467         ranges = of_get_property(parent, "ranges", &rlen);
468         if (ranges == NULL || rlen == 0) {
469                 u32 result[OF_MAX_ADDR_CELLS];
470                 int i;
471
472                 memset(result, 0, pna * 4);
473                 for (i = 0; i < na; i++)
474                         result[pna - 1 - i] =
475                                 addr[na - 1 - i];
476
477                 memcpy(addr, result, pna * 4);
478                 return 0;
479         }
480
481         /* Now walk through the ranges */
482         rlen /= 4;
483         rone = na + pna + ns;
484         for (; rlen >= rone; rlen -= rone, ranges += rone) {
485                 if (!bus->map(addr, ranges, na, ns, pna))
486                         return 0;
487         }
488
489         return 1;
490 }
491
492 static int __init use_1to1_mapping(struct device_node *pp)
493 {
494         char *model;
495
496         /* If this is on the PMU bus, don't try to translate it even
497          * if a ranges property exists.
498          */
499         if (!strcmp(pp->name, "pmu"))
500                 return 1;
501
502         /* If we have a ranges property in the parent, use it.  */
503         if (of_find_property(pp, "ranges", NULL) != NULL)
504                 return 0;
505
506         /* If the parent is the dma node of an ISA bus, pass
507          * the translation up to the root.
508          */
509         if (!strcmp(pp->name, "dma"))
510                 return 0;
511
512         /* Similarly for Simba PCI bridges.  */
513         model = of_get_property(pp, "model", NULL);
514         if (model && !strcmp(model, "SUNW,simba"))
515                 return 0;
516
517         return 1;
518 }
519
520 static int of_resource_verbose;
521
522 static void __init build_device_resources(struct of_device *op,
523                                           struct device *parent)
524 {
525         struct of_device *p_op;
526         struct of_bus *bus;
527         int na, ns;
528         int index, num_reg;
529         void *preg;
530
531         if (!parent)
532                 return;
533
534         p_op = to_of_device(parent);
535         bus = of_match_bus(p_op->node);
536         bus->count_cells(op->node, &na, &ns);
537
538         preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
539         if (!preg || num_reg == 0)
540                 return;
541
542         /* Convert to num-cells.  */
543         num_reg /= 4;
544
545         /* Conver to num-entries.  */
546         num_reg /= na + ns;
547
548         for (index = 0; index < num_reg; index++) {
549                 struct resource *r = &op->resource[index];
550                 u32 addr[OF_MAX_ADDR_CELLS];
551                 u32 *reg = (preg + (index * ((na + ns) * 4)));
552                 struct device_node *dp = op->node;
553                 struct device_node *pp = p_op->node;
554                 struct of_bus *pbus;
555                 u64 size, result = OF_BAD_ADDR;
556                 unsigned long flags;
557                 int dna, dns;
558                 int pna, pns;
559
560                 size = of_read_addr(reg + na, ns);
561                 flags = bus->get_flags(reg);
562
563                 memcpy(addr, reg, na * 4);
564
565                 if (use_1to1_mapping(pp)) {
566                         result = of_read_addr(addr, na);
567                         goto build_res;
568                 }
569
570                 dna = na;
571                 dns = ns;
572
573                 while (1) {
574                         dp = pp;
575                         pp = dp->parent;
576                         if (!pp) {
577                                 result = of_read_addr(addr, dna);
578                                 break;
579                         }
580
581                         pbus = of_match_bus(pp);
582                         pbus->count_cells(dp, &pna, &pns);
583
584                         if (build_one_resource(dp, bus, pbus, addr,
585                                                dna, dns, pna))
586                                 break;
587
588                         dna = pna;
589                         dns = pns;
590                         bus = pbus;
591                 }
592
593         build_res:
594                 memset(r, 0, sizeof(*r));
595
596                 if (of_resource_verbose)
597                         printk("%s reg[%d] -> %lx\n",
598                                op->node->full_name, index,
599                                result);
600
601                 if (result != OF_BAD_ADDR) {
602                         if (tlb_type == hypervisor)
603                                 result &= 0x0fffffffffffffffUL;
604
605                         r->start = result;
606                         r->end = result + size - 1;
607                         r->flags = flags;
608                 } else {
609                         r->start = ~0UL;
610                         r->end = ~0UL;
611                 }
612                 r->name = op->node->name;
613         }
614 }
615
616 static struct device_node * __init
617 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
618                     u32 *imap, int imlen, u32 *imask,
619                     unsigned int *irq_p)
620 {
621         struct device_node *cp;
622         unsigned int irq = *irq_p;
623         struct of_bus *bus;
624         phandle handle;
625         u32 *reg;
626         int na, num_reg, i;
627
628         bus = of_match_bus(pp);
629         bus->count_cells(dp, &na, NULL);
630
631         reg = of_get_property(dp, "reg", &num_reg);
632         if (!reg || !num_reg)
633                 return NULL;
634
635         imlen /= ((na + 3) * 4);
636         handle = 0;
637         for (i = 0; i < imlen; i++) {
638                 int j;
639
640                 for (j = 0; j < na; j++) {
641                         if ((reg[j] & imask[j]) != imap[j])
642                                 goto next;
643                 }
644                 if (imap[na] == irq) {
645                         handle = imap[na + 1];
646                         irq = imap[na + 2];
647                         break;
648                 }
649
650         next:
651                 imap += (na + 3);
652         }
653         if (i == imlen)
654                 return NULL;
655
656         *irq_p = irq;
657         cp = of_find_node_by_phandle(handle);
658
659         return cp;
660 }
661
662 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
663                                            struct device_node *pp,
664                                            unsigned int irq)
665 {
666         struct linux_prom_pci_registers *regs;
667         unsigned int devfn, slot, ret;
668
669         if (irq < 1 || irq > 4)
670                 return irq;
671
672         regs = of_get_property(dp, "reg", NULL);
673         if (!regs)
674                 return irq;
675
676         devfn = (regs->phys_hi >> 8) & 0xff;
677         slot = (devfn >> 3) & 0x1f;
678
679         ret = ((irq - 1 + (slot & 3)) & 3) + 1;
680
681         return ret;
682 }
683
684 static int of_irq_verbose;
685
686 static unsigned int __init build_one_device_irq(struct of_device *op,
687                                                 struct device *parent,
688                                                 unsigned int irq)
689 {
690         struct device_node *dp = op->node;
691         struct device_node *pp, *ip;
692         unsigned int orig_irq = irq;
693
694         if (irq == 0xffffffff)
695                 return irq;
696
697         if (dp->irq_trans) {
698                 irq = dp->irq_trans->irq_build(dp, irq,
699                                                dp->irq_trans->data);
700
701                 if (of_irq_verbose)
702                         printk("%s: direct translate %x --> %x\n",
703                                dp->full_name, orig_irq, irq);
704
705                 return irq;
706         }
707
708         /* Something more complicated.  Walk up to the root, applying
709          * interrupt-map or bus specific translations, until we hit
710          * an IRQ translator.
711          *
712          * If we hit a bus type or situation we cannot handle, we
713          * stop and assume that the original IRQ number was in a
714          * format which has special meaning to it's immediate parent.
715          */
716         pp = dp->parent;
717         ip = NULL;
718         while (pp) {
719                 void *imap, *imsk;
720                 int imlen;
721
722                 imap = of_get_property(pp, "interrupt-map", &imlen);
723                 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
724                 if (imap && imsk) {
725                         struct device_node *iret;
726                         int this_orig_irq = irq;
727
728                         iret = apply_interrupt_map(dp, pp,
729                                                    imap, imlen, imsk,
730                                                    &irq);
731
732                         if (of_irq_verbose)
733                                 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
734                                        op->node->full_name,
735                                        pp->full_name, this_orig_irq,
736                                        (iret ? iret->full_name : "NULL"), irq);
737
738                         if (!iret)
739                                 break;
740
741                         if (iret->irq_trans) {
742                                 ip = iret;
743                                 break;
744                         }
745                 } else {
746                         if (!strcmp(pp->type, "pci") ||
747                             !strcmp(pp->type, "pciex")) {
748                                 unsigned int this_orig_irq = irq;
749
750                                 irq = pci_irq_swizzle(dp, pp, irq);
751                                 if (of_irq_verbose)
752                                         printk("%s: PCI swizzle [%s] "
753                                                "%x --> %x\n",
754                                                op->node->full_name,
755                                                pp->full_name, this_orig_irq,
756                                                irq);
757
758                         }
759
760                         if (pp->irq_trans) {
761                                 ip = pp;
762                                 break;
763                         }
764                 }
765                 dp = pp;
766                 pp = pp->parent;
767         }
768         if (!ip)
769                 return orig_irq;
770
771         irq = ip->irq_trans->irq_build(op->node, irq,
772                                        ip->irq_trans->data);
773         if (of_irq_verbose)
774                 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
775                        op->node->full_name, ip->full_name, orig_irq, irq);
776
777         return irq;
778 }
779
780 static struct of_device * __init scan_one_device(struct device_node *dp,
781                                                  struct device *parent)
782 {
783         struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
784         unsigned int *irq;
785         int len, i;
786
787         if (!op)
788                 return NULL;
789
790         op->node = dp;
791
792         op->clock_freq = of_getintprop_default(dp, "clock-frequency",
793                                                (25*1000*1000));
794         op->portid = of_getintprop_default(dp, "upa-portid", -1);
795         if (op->portid == -1)
796                 op->portid = of_getintprop_default(dp, "portid", -1);
797
798         irq = of_get_property(dp, "interrupts", &len);
799         if (irq) {
800                 memcpy(op->irqs, irq, len);
801                 op->num_irqs = len / 4;
802         } else {
803                 op->num_irqs = 0;
804         }
805
806         build_device_resources(op, parent);
807         for (i = 0; i < op->num_irqs; i++)
808                 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
809
810         op->dev.parent = parent;
811         op->dev.bus = &of_bus_type;
812         if (!parent)
813                 strcpy(op->dev.bus_id, "root");
814         else
815                 strcpy(op->dev.bus_id, dp->path_component_name);
816
817         if (of_device_register(op)) {
818                 printk("%s: Could not register of device.\n",
819                        dp->full_name);
820                 kfree(op);
821                 op = NULL;
822         }
823
824         return op;
825 }
826
827 static void __init scan_tree(struct device_node *dp, struct device *parent)
828 {
829         while (dp) {
830                 struct of_device *op = scan_one_device(dp, parent);
831
832                 if (op)
833                         scan_tree(dp->child, &op->dev);
834
835                 dp = dp->sibling;
836         }
837 }
838
839 static void __init scan_of_devices(void)
840 {
841         struct device_node *root = of_find_node_by_path("/");
842         struct of_device *parent;
843
844         parent = scan_one_device(root, NULL);
845         if (!parent)
846                 return;
847
848         scan_tree(root->child, &parent->dev);
849 }
850
851 static int __init of_bus_driver_init(void)
852 {
853         int err;
854
855         err = bus_register(&of_bus_type);
856 #ifdef CONFIG_PCI
857         if (!err)
858                 err = bus_register(&isa_bus_type);
859         if (!err)
860                 err = bus_register(&ebus_bus_type);
861 #endif
862 #ifdef CONFIG_SBUS
863         if (!err)
864                 err = bus_register(&sbus_bus_type);
865 #endif
866
867         if (!err)
868                 scan_of_devices();
869
870         return err;
871 }
872
873 postcore_initcall(of_bus_driver_init);
874
875 static int __init of_debug(char *str)
876 {
877         int val = 0;
878
879         get_option(&str, &val);
880         if (val & 1)
881                 of_resource_verbose = 1;
882         if (val & 2)
883                 of_irq_verbose = 1;
884         return 1;
885 }
886
887 __setup("of_debug=", of_debug);
888
889 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
890 {
891         /* initialize common driver fields */
892         drv->driver.name = drv->name;
893         drv->driver.bus = bus;
894
895         /* register with core */
896         return driver_register(&drv->driver);
897 }
898
899 void of_unregister_driver(struct of_platform_driver *drv)
900 {
901         driver_unregister(&drv->driver);
902 }
903
904
905 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
906 {
907         struct of_device *ofdev;
908
909         ofdev = to_of_device(dev);
910         return sprintf(buf, "%s", ofdev->node->full_name);
911 }
912
913 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
914
915 /**
916  * of_release_dev - free an of device structure when all users of it are finished.
917  * @dev: device that's been disconnected
918  *
919  * Will be called only by the device core when all users of this of device are
920  * done.
921  */
922 void of_release_dev(struct device *dev)
923 {
924         struct of_device *ofdev;
925
926         ofdev = to_of_device(dev);
927
928         kfree(ofdev);
929 }
930
931 int of_device_register(struct of_device *ofdev)
932 {
933         int rc;
934
935         BUG_ON(ofdev->node == NULL);
936
937         rc = device_register(&ofdev->dev);
938         if (rc)
939                 return rc;
940
941         rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
942         if (rc)
943                 device_unregister(&ofdev->dev);
944
945         return rc;
946 }
947
948 void of_device_unregister(struct of_device *ofdev)
949 {
950         device_remove_file(&ofdev->dev, &dev_attr_devspec);
951         device_unregister(&ofdev->dev);
952 }
953
954 struct of_device* of_platform_device_create(struct device_node *np,
955                                             const char *bus_id,
956                                             struct device *parent,
957                                             struct bus_type *bus)
958 {
959         struct of_device *dev;
960
961         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
962         if (!dev)
963                 return NULL;
964         memset(dev, 0, sizeof(*dev));
965
966         dev->dev.parent = parent;
967         dev->dev.bus = bus;
968         dev->dev.release = of_release_dev;
969
970         strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
971
972         if (of_device_register(dev) != 0) {
973                 kfree(dev);
974                 return NULL;
975         }
976
977         return dev;
978 }
979
980 EXPORT_SYMBOL(of_match_device);
981 EXPORT_SYMBOL(of_register_driver);
982 EXPORT_SYMBOL(of_unregister_driver);
983 EXPORT_SYMBOL(of_device_register);
984 EXPORT_SYMBOL(of_device_unregister);
985 EXPORT_SYMBOL(of_dev_get);
986 EXPORT_SYMBOL(of_dev_put);
987 EXPORT_SYMBOL(of_platform_device_create);
988 EXPORT_SYMBOL(of_release_dev);