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