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