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