[SPARC64]: Fix of_iounmap() region release.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sparc64 / kernel / of_device.c
blobb0f3e0082a0da113fc31eedb484c44ae5c10a9b7
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>
8 #include <asm/errno.h>
9 #include <asm/of_device.h>
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
17 * Used by a driver to check whether an of_device present in the
18 * system is in its list of supported devices.
20 const struct of_device_id *of_match_device(const struct of_device_id *matches,
21 const struct of_device *dev)
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++;
40 return NULL;
43 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
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;
49 if (!matches)
50 return 0;
52 return of_match_device(matches, of_dev) != NULL;
55 struct of_device *of_dev_get(struct of_device *dev)
57 struct device *tmp;
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;
68 void of_dev_put(struct of_device *dev)
70 if (dev)
71 put_device(&dev->dev);
75 static int of_device_probe(struct device *dev)
77 int error = -ENODEV;
78 struct of_platform_driver *drv;
79 struct of_device *of_dev;
80 const struct of_device_id *match;
82 drv = to_of_platform_driver(dev->driver);
83 of_dev = to_of_device(dev);
85 if (!drv->probe)
86 return error;
88 of_dev_get(of_dev);
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);
96 return error;
99 static int of_device_remove(struct device *dev)
101 struct of_device * of_dev = to_of_device(dev);
102 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
104 if (dev->driver && drv->remove)
105 drv->remove(of_dev);
106 return 0;
109 static int of_device_suspend(struct device *dev, pm_message_t state)
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;
115 if (dev->driver && drv->suspend)
116 error = drv->suspend(of_dev, state);
117 return error;
120 static int of_device_resume(struct device * dev)
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;
126 if (dev->driver && drv->resume)
127 error = drv->resume(of_dev);
128 return error;
131 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
133 unsigned long ret = res->start + offset;
134 struct resource *r;
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;
143 return (void __iomem *) ret;
145 EXPORT_SYMBOL(of_ioremap);
147 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
149 if (res->flags & IORESOURCE_MEM)
150 release_mem_region((unsigned long) base, size);
151 else
152 release_region((unsigned long) base, size);
154 EXPORT_SYMBOL(of_iounmap);
156 static int node_match(struct device *dev, void *data)
158 struct of_device *op = to_of_device(dev);
159 struct device_node *dp = data;
161 return (op->node == dp);
164 struct of_device *of_find_device_by_node(struct device_node *dp)
166 struct device *dev = bus_find_device(&of_bus_type, NULL,
167 dp, node_match);
169 if (dev)
170 return to_of_device(dev);
172 return NULL;
174 EXPORT_SYMBOL(of_find_device_by_node);
176 #ifdef CONFIG_PCI
177 struct bus_type isa_bus_type = {
178 .name = "isa",
179 .match = of_platform_bus_match,
180 .probe = of_device_probe,
181 .remove = of_device_remove,
182 .suspend = of_device_suspend,
183 .resume = of_device_resume,
185 EXPORT_SYMBOL(isa_bus_type);
187 struct bus_type ebus_bus_type = {
188 .name = "ebus",
189 .match = of_platform_bus_match,
190 .probe = of_device_probe,
191 .remove = of_device_remove,
192 .suspend = of_device_suspend,
193 .resume = of_device_resume,
195 EXPORT_SYMBOL(ebus_bus_type);
196 #endif
198 #ifdef CONFIG_SBUS
199 struct bus_type sbus_bus_type = {
200 .name = "sbus",
201 .match = of_platform_bus_match,
202 .probe = of_device_probe,
203 .remove = of_device_remove,
204 .suspend = of_device_suspend,
205 .resume = of_device_resume,
207 EXPORT_SYMBOL(sbus_bus_type);
208 #endif
210 struct bus_type of_bus_type = {
211 .name = "of",
212 .match = of_platform_bus_match,
213 .probe = of_device_probe,
214 .remove = of_device_remove,
215 .suspend = of_device_suspend,
216 .resume = of_device_resume,
218 EXPORT_SYMBOL(of_bus_type);
220 static inline u64 of_read_addr(const u32 *cell, int size)
222 u64 r = 0;
223 while (size--)
224 r = (r << 32) | *(cell++);
225 return r;
228 static void __init get_cells(struct device_node *dp,
229 int *addrc, int *sizec)
231 if (addrc)
232 *addrc = of_n_addr_cells(dp);
233 if (sizec)
234 *sizec = of_n_size_cells(dp);
237 /* Max address size we deal with */
238 #define OF_MAX_ADDR_CELLS 4
240 struct of_bus {
241 const char *name;
242 const char *addr_prop_name;
243 int (*match)(struct device_node *parent);
244 void (*count_cells)(struct device_node *child,
245 int *addrc, int *sizec);
246 int (*map)(u32 *addr, const u32 *range,
247 int na, int ns, int pna);
248 unsigned int (*get_flags)(u32 *addr);
252 * Default translator (generic bus)
255 static void of_bus_default_count_cells(struct device_node *dev,
256 int *addrc, int *sizec)
258 get_cells(dev, addrc, sizec);
261 /* Make sure the least significant 64-bits are in-range. Even
262 * for 3 or 4 cell values it is a good enough approximation.
264 static int of_out_of_range(const u32 *addr, const u32 *base,
265 const u32 *size, int na, int ns)
267 u64 a = of_read_addr(addr, na);
268 u64 b = of_read_addr(base, na);
270 if (a < b)
271 return 1;
273 b += of_read_addr(size, ns);
274 if (a >= b)
275 return 1;
277 return 0;
280 static int of_bus_default_map(u32 *addr, const u32 *range,
281 int na, int ns, int pna)
283 u32 result[OF_MAX_ADDR_CELLS];
284 int i;
286 if (ns > 2) {
287 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
288 return -EINVAL;
291 if (of_out_of_range(addr, range, range + na + pna, na, ns))
292 return -EINVAL;
294 /* Start with the parent range base. */
295 memcpy(result, range + na, pna * 4);
297 /* Add in the child address offset. */
298 for (i = 0; i < na; i++)
299 result[pna - 1 - i] +=
300 (addr[na - 1 - i] -
301 range[na - 1 - i]);
303 memcpy(addr, result, pna * 4);
305 return 0;
308 static unsigned int of_bus_default_get_flags(u32 *addr)
310 return IORESOURCE_MEM;
314 * PCI bus specific translator
317 static int of_bus_pci_match(struct device_node *np)
319 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
320 /* Do not do PCI specific frobbing if the
321 * PCI bridge lacks a ranges property. We
322 * want to pass it through up to the next
323 * parent as-is, not with the PCI translate
324 * method which chops off the top address cell.
326 if (!of_find_property(np, "ranges", NULL))
327 return 0;
329 return 1;
332 return 0;
335 static void of_bus_pci_count_cells(struct device_node *np,
336 int *addrc, int *sizec)
338 if (addrc)
339 *addrc = 3;
340 if (sizec)
341 *sizec = 2;
344 static int of_bus_pci_map(u32 *addr, const u32 *range,
345 int na, int ns, int pna)
347 u32 result[OF_MAX_ADDR_CELLS];
348 int i;
350 /* Check address type match */
351 if ((addr[0] ^ range[0]) & 0x03000000)
352 return -EINVAL;
354 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
355 na - 1, ns))
356 return -EINVAL;
358 /* Start with the parent range base. */
359 memcpy(result, range + na, pna * 4);
361 /* Add in the child address offset, skipping high cell. */
362 for (i = 0; i < na - 1; i++)
363 result[pna - 1 - i] +=
364 (addr[na - 1 - i] -
365 range[na - 1 - i]);
367 memcpy(addr, result, pna * 4);
369 return 0;
372 static unsigned int of_bus_pci_get_flags(u32 *addr)
374 unsigned int flags = 0;
375 u32 w = addr[0];
377 switch((w >> 24) & 0x03) {
378 case 0x01:
379 flags |= IORESOURCE_IO;
380 case 0x02: /* 32 bits */
381 case 0x03: /* 64 bits */
382 flags |= IORESOURCE_MEM;
384 if (w & 0x40000000)
385 flags |= IORESOURCE_PREFETCH;
386 return flags;
390 * SBUS bus specific translator
393 static int of_bus_sbus_match(struct device_node *np)
395 return !strcmp(np->name, "sbus") ||
396 !strcmp(np->name, "sbi");
399 static void of_bus_sbus_count_cells(struct device_node *child,
400 int *addrc, int *sizec)
402 if (addrc)
403 *addrc = 2;
404 if (sizec)
405 *sizec = 1;
409 * FHC/Central bus specific translator.
411 * This is just needed to hard-code the address and size cell
412 * counts. 'fhc' and 'central' nodes lack the #address-cells and
413 * #size-cells properties, and if you walk to the root on such
414 * Enterprise boxes all you'll get is a #size-cells of 2 which is
415 * not what we want to use.
417 static int of_bus_fhc_match(struct device_node *np)
419 return !strcmp(np->name, "fhc") ||
420 !strcmp(np->name, "central");
423 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
426 * Array of bus specific translators
429 static struct of_bus of_busses[] = {
430 /* PCI */
432 .name = "pci",
433 .addr_prop_name = "assigned-addresses",
434 .match = of_bus_pci_match,
435 .count_cells = of_bus_pci_count_cells,
436 .map = of_bus_pci_map,
437 .get_flags = of_bus_pci_get_flags,
439 /* SBUS */
441 .name = "sbus",
442 .addr_prop_name = "reg",
443 .match = of_bus_sbus_match,
444 .count_cells = of_bus_sbus_count_cells,
445 .map = of_bus_default_map,
446 .get_flags = of_bus_default_get_flags,
448 /* FHC */
450 .name = "fhc",
451 .addr_prop_name = "reg",
452 .match = of_bus_fhc_match,
453 .count_cells = of_bus_fhc_count_cells,
454 .map = of_bus_default_map,
455 .get_flags = of_bus_default_get_flags,
457 /* Default */
459 .name = "default",
460 .addr_prop_name = "reg",
461 .match = NULL,
462 .count_cells = of_bus_default_count_cells,
463 .map = of_bus_default_map,
464 .get_flags = of_bus_default_get_flags,
468 static struct of_bus *of_match_bus(struct device_node *np)
470 int i;
472 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
473 if (!of_busses[i].match || of_busses[i].match(np))
474 return &of_busses[i];
475 BUG();
476 return NULL;
479 static int __init build_one_resource(struct device_node *parent,
480 struct of_bus *bus,
481 struct of_bus *pbus,
482 u32 *addr,
483 int na, int ns, int pna)
485 u32 *ranges;
486 unsigned int rlen;
487 int rone;
489 ranges = of_get_property(parent, "ranges", &rlen);
490 if (ranges == NULL || rlen == 0) {
491 u32 result[OF_MAX_ADDR_CELLS];
492 int i;
494 memset(result, 0, pna * 4);
495 for (i = 0; i < na; i++)
496 result[pna - 1 - i] =
497 addr[na - 1 - i];
499 memcpy(addr, result, pna * 4);
500 return 0;
503 /* Now walk through the ranges */
504 rlen /= 4;
505 rone = na + pna + ns;
506 for (; rlen >= rone; rlen -= rone, ranges += rone) {
507 if (!bus->map(addr, ranges, na, ns, pna))
508 return 0;
511 return 1;
514 static int __init use_1to1_mapping(struct device_node *pp)
516 char *model;
518 /* If this is on the PMU bus, don't try to translate it even
519 * if a ranges property exists.
521 if (!strcmp(pp->name, "pmu"))
522 return 1;
524 /* If we have a ranges property in the parent, use it. */
525 if (of_find_property(pp, "ranges", NULL) != NULL)
526 return 0;
528 /* If the parent is the dma node of an ISA bus, pass
529 * the translation up to the root.
531 if (!strcmp(pp->name, "dma"))
532 return 0;
534 /* Similarly for Simba PCI bridges. */
535 model = of_get_property(pp, "model", NULL);
536 if (model && !strcmp(model, "SUNW,simba"))
537 return 0;
539 return 1;
542 static int of_resource_verbose;
544 static void __init build_device_resources(struct of_device *op,
545 struct device *parent)
547 struct of_device *p_op;
548 struct of_bus *bus;
549 int na, ns;
550 int index, num_reg;
551 void *preg;
553 if (!parent)
554 return;
556 p_op = to_of_device(parent);
557 bus = of_match_bus(p_op->node);
558 bus->count_cells(op->node, &na, &ns);
560 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
561 if (!preg || num_reg == 0)
562 return;
564 /* Convert to num-cells. */
565 num_reg /= 4;
567 /* Convert to num-entries. */
568 num_reg /= na + ns;
570 /* Prevent overruning the op->resources[] array. */
571 if (num_reg > PROMREG_MAX) {
572 printk(KERN_WARNING "%s: Too many regs (%d), "
573 "limiting to %d.\n",
574 op->node->full_name, num_reg, PROMREG_MAX);
575 num_reg = PROMREG_MAX;
578 for (index = 0; index < num_reg; index++) {
579 struct resource *r = &op->resource[index];
580 u32 addr[OF_MAX_ADDR_CELLS];
581 u32 *reg = (preg + (index * ((na + ns) * 4)));
582 struct device_node *dp = op->node;
583 struct device_node *pp = p_op->node;
584 struct of_bus *pbus;
585 u64 size, result = OF_BAD_ADDR;
586 unsigned long flags;
587 int dna, dns;
588 int pna, pns;
590 size = of_read_addr(reg + na, ns);
591 flags = bus->get_flags(reg);
593 memcpy(addr, reg, na * 4);
595 if (use_1to1_mapping(pp)) {
596 result = of_read_addr(addr, na);
597 goto build_res;
600 dna = na;
601 dns = ns;
603 while (1) {
604 dp = pp;
605 pp = dp->parent;
606 if (!pp) {
607 result = of_read_addr(addr, dna);
608 break;
611 pbus = of_match_bus(pp);
612 pbus->count_cells(dp, &pna, &pns);
614 if (build_one_resource(dp, bus, pbus, addr,
615 dna, dns, pna))
616 break;
618 dna = pna;
619 dns = pns;
620 bus = pbus;
623 build_res:
624 memset(r, 0, sizeof(*r));
626 if (of_resource_verbose)
627 printk("%s reg[%d] -> %lx\n",
628 op->node->full_name, index,
629 result);
631 if (result != OF_BAD_ADDR) {
632 if (tlb_type == hypervisor)
633 result &= 0x0fffffffffffffffUL;
635 r->start = result;
636 r->end = result + size - 1;
637 r->flags = flags;
638 } else {
639 r->start = ~0UL;
640 r->end = ~0UL;
642 r->name = op->node->name;
646 static struct device_node * __init
647 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
648 u32 *imap, int imlen, u32 *imask,
649 unsigned int *irq_p)
651 struct device_node *cp;
652 unsigned int irq = *irq_p;
653 struct of_bus *bus;
654 phandle handle;
655 u32 *reg;
656 int na, num_reg, i;
658 bus = of_match_bus(pp);
659 bus->count_cells(dp, &na, NULL);
661 reg = of_get_property(dp, "reg", &num_reg);
662 if (!reg || !num_reg)
663 return NULL;
665 imlen /= ((na + 3) * 4);
666 handle = 0;
667 for (i = 0; i < imlen; i++) {
668 int j;
670 for (j = 0; j < na; j++) {
671 if ((reg[j] & imask[j]) != imap[j])
672 goto next;
674 if (imap[na] == irq) {
675 handle = imap[na + 1];
676 irq = imap[na + 2];
677 break;
680 next:
681 imap += (na + 3);
683 if (i == imlen) {
684 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
685 * properties that do not include the on-board device
686 * interrupts. Instead, the device's 'interrupts' property
687 * is already a fully specified INO value.
689 * Handle this by deciding that, if we didn't get a
690 * match in the parent's 'interrupt-map', and the
691 * parent is an IRQ translater, then use the parent as
692 * our IRQ controller.
694 if (pp->irq_trans)
695 return pp;
697 return NULL;
700 *irq_p = irq;
701 cp = of_find_node_by_phandle(handle);
703 return cp;
706 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
707 struct device_node *pp,
708 unsigned int irq)
710 struct linux_prom_pci_registers *regs;
711 unsigned int devfn, slot, ret;
713 if (irq < 1 || irq > 4)
714 return irq;
716 regs = of_get_property(dp, "reg", NULL);
717 if (!regs)
718 return irq;
720 devfn = (regs->phys_hi >> 8) & 0xff;
721 slot = (devfn >> 3) & 0x1f;
723 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
725 return ret;
728 static int of_irq_verbose;
730 static unsigned int __init build_one_device_irq(struct of_device *op,
731 struct device *parent,
732 unsigned int irq)
734 struct device_node *dp = op->node;
735 struct device_node *pp, *ip;
736 unsigned int orig_irq = irq;
738 if (irq == 0xffffffff)
739 return irq;
741 if (dp->irq_trans) {
742 irq = dp->irq_trans->irq_build(dp, irq,
743 dp->irq_trans->data);
745 if (of_irq_verbose)
746 printk("%s: direct translate %x --> %x\n",
747 dp->full_name, orig_irq, irq);
749 return irq;
752 /* Something more complicated. Walk up to the root, applying
753 * interrupt-map or bus specific translations, until we hit
754 * an IRQ translator.
756 * If we hit a bus type or situation we cannot handle, we
757 * stop and assume that the original IRQ number was in a
758 * format which has special meaning to it's immediate parent.
760 pp = dp->parent;
761 ip = NULL;
762 while (pp) {
763 void *imap, *imsk;
764 int imlen;
766 imap = of_get_property(pp, "interrupt-map", &imlen);
767 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
768 if (imap && imsk) {
769 struct device_node *iret;
770 int this_orig_irq = irq;
772 iret = apply_interrupt_map(dp, pp,
773 imap, imlen, imsk,
774 &irq);
776 if (of_irq_verbose)
777 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
778 op->node->full_name,
779 pp->full_name, this_orig_irq,
780 (iret ? iret->full_name : "NULL"), irq);
782 if (!iret)
783 break;
785 if (iret->irq_trans) {
786 ip = iret;
787 break;
789 } else {
790 if (!strcmp(pp->type, "pci") ||
791 !strcmp(pp->type, "pciex")) {
792 unsigned int this_orig_irq = irq;
794 irq = pci_irq_swizzle(dp, pp, irq);
795 if (of_irq_verbose)
796 printk("%s: PCI swizzle [%s] "
797 "%x --> %x\n",
798 op->node->full_name,
799 pp->full_name, this_orig_irq,
800 irq);
804 if (pp->irq_trans) {
805 ip = pp;
806 break;
809 dp = pp;
810 pp = pp->parent;
812 if (!ip)
813 return orig_irq;
815 irq = ip->irq_trans->irq_build(op->node, irq,
816 ip->irq_trans->data);
817 if (of_irq_verbose)
818 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
819 op->node->full_name, ip->full_name, orig_irq, irq);
821 return irq;
824 static struct of_device * __init scan_one_device(struct device_node *dp,
825 struct device *parent)
827 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
828 unsigned int *irq;
829 int len, i;
831 if (!op)
832 return NULL;
834 op->node = dp;
836 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
837 (25*1000*1000));
838 op->portid = of_getintprop_default(dp, "upa-portid", -1);
839 if (op->portid == -1)
840 op->portid = of_getintprop_default(dp, "portid", -1);
842 irq = of_get_property(dp, "interrupts", &len);
843 if (irq) {
844 memcpy(op->irqs, irq, len);
845 op->num_irqs = len / 4;
846 } else {
847 op->num_irqs = 0;
850 /* Prevent overruning the op->irqs[] array. */
851 if (op->num_irqs > PROMINTR_MAX) {
852 printk(KERN_WARNING "%s: Too many irqs (%d), "
853 "limiting to %d.\n",
854 dp->full_name, op->num_irqs, PROMINTR_MAX);
855 op->num_irqs = PROMINTR_MAX;
858 build_device_resources(op, parent);
859 for (i = 0; i < op->num_irqs; i++)
860 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
862 op->dev.parent = parent;
863 op->dev.bus = &of_bus_type;
864 if (!parent)
865 strcpy(op->dev.bus_id, "root");
866 else
867 sprintf(op->dev.bus_id, "%08x", dp->node);
869 if (of_device_register(op)) {
870 printk("%s: Could not register of device.\n",
871 dp->full_name);
872 kfree(op);
873 op = NULL;
876 return op;
879 static void __init scan_tree(struct device_node *dp, struct device *parent)
881 while (dp) {
882 struct of_device *op = scan_one_device(dp, parent);
884 if (op)
885 scan_tree(dp->child, &op->dev);
887 dp = dp->sibling;
891 static void __init scan_of_devices(void)
893 struct device_node *root = of_find_node_by_path("/");
894 struct of_device *parent;
896 parent = scan_one_device(root, NULL);
897 if (!parent)
898 return;
900 scan_tree(root->child, &parent->dev);
903 static int __init of_bus_driver_init(void)
905 int err;
907 err = bus_register(&of_bus_type);
908 #ifdef CONFIG_PCI
909 if (!err)
910 err = bus_register(&isa_bus_type);
911 if (!err)
912 err = bus_register(&ebus_bus_type);
913 #endif
914 #ifdef CONFIG_SBUS
915 if (!err)
916 err = bus_register(&sbus_bus_type);
917 #endif
919 if (!err)
920 scan_of_devices();
922 return err;
925 postcore_initcall(of_bus_driver_init);
927 static int __init of_debug(char *str)
929 int val = 0;
931 get_option(&str, &val);
932 if (val & 1)
933 of_resource_verbose = 1;
934 if (val & 2)
935 of_irq_verbose = 1;
936 return 1;
939 __setup("of_debug=", of_debug);
941 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
943 /* initialize common driver fields */
944 drv->driver.name = drv->name;
945 drv->driver.bus = bus;
947 /* register with core */
948 return driver_register(&drv->driver);
951 void of_unregister_driver(struct of_platform_driver *drv)
953 driver_unregister(&drv->driver);
957 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
959 struct of_device *ofdev;
961 ofdev = to_of_device(dev);
962 return sprintf(buf, "%s", ofdev->node->full_name);
965 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
968 * of_release_dev - free an of device structure when all users of it are finished.
969 * @dev: device that's been disconnected
971 * Will be called only by the device core when all users of this of device are
972 * done.
974 void of_release_dev(struct device *dev)
976 struct of_device *ofdev;
978 ofdev = to_of_device(dev);
980 kfree(ofdev);
983 int of_device_register(struct of_device *ofdev)
985 int rc;
987 BUG_ON(ofdev->node == NULL);
989 rc = device_register(&ofdev->dev);
990 if (rc)
991 return rc;
993 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
994 if (rc)
995 device_unregister(&ofdev->dev);
997 return rc;
1000 void of_device_unregister(struct of_device *ofdev)
1002 device_remove_file(&ofdev->dev, &dev_attr_devspec);
1003 device_unregister(&ofdev->dev);
1006 struct of_device* of_platform_device_create(struct device_node *np,
1007 const char *bus_id,
1008 struct device *parent,
1009 struct bus_type *bus)
1011 struct of_device *dev;
1013 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1014 if (!dev)
1015 return NULL;
1017 dev->dev.parent = parent;
1018 dev->dev.bus = bus;
1019 dev->dev.release = of_release_dev;
1021 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
1023 if (of_device_register(dev) != 0) {
1024 kfree(dev);
1025 return NULL;
1028 return dev;
1031 EXPORT_SYMBOL(of_match_device);
1032 EXPORT_SYMBOL(of_register_driver);
1033 EXPORT_SYMBOL(of_unregister_driver);
1034 EXPORT_SYMBOL(of_device_register);
1035 EXPORT_SYMBOL(of_device_unregister);
1036 EXPORT_SYMBOL(of_dev_get);
1037 EXPORT_SYMBOL(of_dev_put);
1038 EXPORT_SYMBOL(of_platform_device_create);
1039 EXPORT_SYMBOL(of_release_dev);