1 #include <linux/config.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
10 #include <asm/of_device.h>
13 * of_match_device - Tell if an of_device structure has a matching
15 * @ids: array of of device match structures to search in
16 * @dev: the of device structure to match against
18 * Used by a driver to check whether an of_device present in the
19 * system is in its list of supported devices.
21 const struct of_device_id *of_match_device(const struct of_device_id *matches,
22 const struct of_device *dev)
26 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
29 match &= dev->node->name
30 && !strcmp(matches->name, dev->node->name);
32 match &= dev->node->type
33 && !strcmp(matches->type, dev->node->type);
34 if (matches->compatible[0])
35 match &= of_device_is_compatible(dev->node,
44 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
46 struct of_device * of_dev = to_of_device(dev);
47 struct of_platform_driver * of_drv = to_of_platform_driver(drv);
48 const struct of_device_id * matches = of_drv->match_table;
53 return of_match_device(matches, of_dev) != NULL;
56 struct of_device *of_dev_get(struct of_device *dev)
62 tmp = get_device(&dev->dev);
64 return to_of_device(tmp);
69 void of_dev_put(struct of_device *dev)
72 put_device(&dev->dev);
76 static int of_device_probe(struct device *dev)
79 struct of_platform_driver *drv;
80 struct of_device *of_dev;
81 const struct of_device_id *match;
83 drv = to_of_platform_driver(dev->driver);
84 of_dev = to_of_device(dev);
91 match = of_match_device(drv->match_table, of_dev);
93 error = drv->probe(of_dev, match);
100 static int of_device_remove(struct device *dev)
102 struct of_device * of_dev = to_of_device(dev);
103 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
105 if (dev->driver && drv->remove)
110 static int of_device_suspend(struct device *dev, pm_message_t state)
112 struct of_device * of_dev = to_of_device(dev);
113 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
116 if (dev->driver && drv->suspend)
117 error = drv->suspend(of_dev, state);
121 static int of_device_resume(struct device * dev)
123 struct of_device * of_dev = to_of_device(dev);
124 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
127 if (dev->driver && drv->resume)
128 error = drv->resume(of_dev);
132 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
134 unsigned long ret = res->start + offset;
136 if (!request_region(ret, size, name))
139 return (void __iomem *) ret;
141 EXPORT_SYMBOL(of_ioremap);
143 void of_iounmap(void __iomem *base, unsigned long size)
145 release_region((unsigned long) base, size);
147 EXPORT_SYMBOL(of_iounmap);
150 struct bus_type isa_bus_type = {
152 .match = of_platform_bus_match,
153 .probe = of_device_probe,
154 .remove = of_device_remove,
155 .suspend = of_device_suspend,
156 .resume = of_device_resume,
158 EXPORT_SYMBOL(isa_bus_type);
160 struct bus_type ebus_bus_type = {
162 .match = of_platform_bus_match,
163 .probe = of_device_probe,
164 .remove = of_device_remove,
165 .suspend = of_device_suspend,
166 .resume = of_device_resume,
168 EXPORT_SYMBOL(ebus_bus_type);
172 struct bus_type sbus_bus_type = {
174 .match = of_platform_bus_match,
175 .probe = of_device_probe,
176 .remove = of_device_remove,
177 .suspend = of_device_suspend,
178 .resume = of_device_resume,
180 EXPORT_SYMBOL(sbus_bus_type);
183 struct bus_type of_bus_type = {
185 .match = of_platform_bus_match,
186 .probe = of_device_probe,
187 .remove = of_device_remove,
188 .suspend = of_device_suspend,
189 .resume = of_device_resume,
191 EXPORT_SYMBOL(of_bus_type);
193 static inline u64 of_read_addr(u32 *cell, int size)
197 r = (r << 32) | *(cell++);
201 static void __init get_cells(struct device_node *dp,
202 int *addrc, int *sizec)
205 *addrc = of_n_addr_cells(dp);
207 *sizec = of_n_size_cells(dp);
210 /* Max address size we deal with */
211 #define OF_MAX_ADDR_CELLS 4
215 const char *addr_prop_name;
216 int (*match)(struct device_node *parent);
217 void (*count_cells)(struct device_node *child,
218 int *addrc, int *sizec);
219 u64 (*map)(u32 *addr, u32 *range, int na, int ns, int pna);
220 int (*translate)(u32 *addr, u64 offset, int na);
221 unsigned int (*get_flags)(u32 *addr);
225 * Default translator (generic bus)
228 static void of_bus_default_count_cells(struct device_node *dev,
229 int *addrc, int *sizec)
231 get_cells(dev, addrc, sizec);
234 static u64 of_bus_default_map(u32 *addr, u32 *range, int na, int ns, int pna)
238 cp = of_read_addr(range, na);
239 s = of_read_addr(range + na + pna, ns);
240 da = of_read_addr(addr, na);
242 if (da < cp || da >= (cp + s))
247 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
249 u64 a = of_read_addr(addr, na);
250 memset(addr, 0, na * 4);
253 addr[na - 2] = a >> 32;
254 addr[na - 1] = a & 0xffffffffu;
259 static unsigned int of_bus_default_get_flags(u32 *addr)
261 return IORESOURCE_MEM;
266 * PCI bus specific translator
269 static int of_bus_pci_match(struct device_node *np)
271 return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex");
274 static void of_bus_pci_count_cells(struct device_node *np,
275 int *addrc, int *sizec)
283 static u64 of_bus_pci_map(u32 *addr, u32 *range, int na, int ns, int pna)
287 /* Check address type match */
288 if ((addr[0] ^ range[0]) & 0x03000000)
291 /* Read address values, skipping high cell */
292 cp = of_read_addr(range + 1, na - 1);
293 s = of_read_addr(range + na + pna, ns);
294 da = of_read_addr(addr + 1, na - 1);
296 if (da < cp || da >= (cp + s))
301 static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
303 return of_bus_default_translate(addr + 1, offset, na - 1);
306 static unsigned int of_bus_pci_get_flags(u32 *addr)
308 unsigned int flags = 0;
311 switch((w >> 24) & 0x03) {
313 flags |= IORESOURCE_IO;
314 case 0x02: /* 32 bits */
315 case 0x03: /* 64 bits */
316 flags |= IORESOURCE_MEM;
319 flags |= IORESOURCE_PREFETCH;
324 * ISA bus specific translator
327 static int of_bus_isa_match(struct device_node *np)
329 return !strcmp(np->name, "isa");
332 static void of_bus_isa_count_cells(struct device_node *child,
333 int *addrc, int *sizec)
341 static u64 of_bus_isa_map(u32 *addr, u32 *range, int na, int ns, int pna)
345 /* Check address type match */
346 if ((addr[0] ^ range[0]) & 0x00000001)
349 /* Read address values, skipping high cell */
350 cp = of_read_addr(range + 1, na - 1);
351 s = of_read_addr(range + na + pna, ns);
352 da = of_read_addr(addr + 1, na - 1);
354 if (da < cp || da >= (cp + s))
359 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
361 return of_bus_default_translate(addr + 1, offset, na - 1);
364 static unsigned int of_bus_isa_get_flags(u32 *addr)
366 unsigned int flags = 0;
370 flags |= IORESOURCE_IO;
372 flags |= IORESOURCE_MEM;
377 * SBUS bus specific translator
380 static int of_bus_sbus_match(struct device_node *np)
382 return !strcmp(np->name, "sbus") ||
383 !strcmp(np->name, "sbi");
386 static void of_bus_sbus_count_cells(struct device_node *child,
387 int *addrc, int *sizec)
395 static u64 of_bus_sbus_map(u32 *addr, u32 *range, int na, int ns, int pna)
397 return of_bus_default_map(addr, range, na, ns, pna);
400 static int of_bus_sbus_translate(u32 *addr, u64 offset, int na)
402 return of_bus_default_translate(addr, offset, na);
405 static unsigned int of_bus_sbus_get_flags(u32 *addr)
407 return IORESOURCE_MEM;
412 * Array of bus specific translators
415 static struct of_bus of_busses[] = {
419 .addr_prop_name = "assigned-addresses",
420 .match = of_bus_pci_match,
421 .count_cells = of_bus_pci_count_cells,
422 .map = of_bus_pci_map,
423 .translate = of_bus_pci_translate,
424 .get_flags = of_bus_pci_get_flags,
429 .addr_prop_name = "reg",
430 .match = of_bus_isa_match,
431 .count_cells = of_bus_isa_count_cells,
432 .map = of_bus_isa_map,
433 .translate = of_bus_isa_translate,
434 .get_flags = of_bus_isa_get_flags,
439 .addr_prop_name = "reg",
440 .match = of_bus_sbus_match,
441 .count_cells = of_bus_sbus_count_cells,
442 .map = of_bus_sbus_map,
443 .translate = of_bus_sbus_translate,
444 .get_flags = of_bus_sbus_get_flags,
449 .addr_prop_name = "reg",
451 .count_cells = of_bus_default_count_cells,
452 .map = of_bus_default_map,
453 .translate = of_bus_default_translate,
454 .get_flags = of_bus_default_get_flags,
458 static struct of_bus *of_match_bus(struct device_node *np)
462 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
463 if (!of_busses[i].match || of_busses[i].match(np))
464 return &of_busses[i];
469 static int __init build_one_resource(struct device_node *parent,
473 int na, int ns, int pna)
478 u64 offset = OF_BAD_ADDR;
480 ranges = of_get_property(parent, "ranges", &rlen);
481 if (ranges == NULL || rlen == 0) {
482 offset = of_read_addr(addr, na);
483 memset(addr, 0, pna * 4);
487 /* Now walk through the ranges */
489 rone = na + pna + ns;
490 for (; rlen >= rone; rlen -= rone, ranges += rone) {
491 offset = bus->map(addr, ranges, na, ns, pna);
492 if (offset != OF_BAD_ADDR)
495 if (offset == OF_BAD_ADDR)
498 memcpy(addr, ranges + na, 4 * pna);
501 /* Translate it into parent bus space */
502 return pbus->translate(addr, offset, pna);
505 static void __init build_device_resources(struct of_device *op,
506 struct device *parent)
508 struct of_device *p_op;
517 p_op = to_of_device(parent);
518 bus = of_match_bus(p_op->node);
519 bus->count_cells(op->node, &na, &ns);
521 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
522 if (!preg || num_reg == 0)
525 /* Convert to num-cells. */
528 /* Conver to num-entries. */
531 for (index = 0; index < num_reg; index++) {
532 struct resource *r = &op->resource[index];
533 u32 addr[OF_MAX_ADDR_CELLS];
534 u32 *reg = (preg + (index * ((na + ns) * 4)));
535 struct device_node *dp = op->node;
536 struct device_node *pp = p_op->node;
538 u64 size, result = OF_BAD_ADDR;
543 size = of_read_addr(reg + na, ns);
544 flags = bus->get_flags(reg);
546 memcpy(addr, reg, na * 4);
548 /* If the immediate parent has no ranges property to apply,
549 * just use a 1<->1 mapping. Unless it is the 'dma' child
550 * of an isa bus, which must be passed up towards the root.
552 * Also, don't try to translate PMU bus device registers.
554 if ((of_find_property(pp, "ranges", NULL) == NULL &&
555 strcmp(pp->name, "dma") != 0) ||
556 !strcmp(pp->name, "pmu")) {
557 result = of_read_addr(addr, na);
568 result = of_read_addr(addr, dna);
572 pbus = of_match_bus(pp);
573 pbus->count_cells(dp, &pna, &pns);
575 if (build_one_resource(dp, bus, pbus, addr, dna, dns, pna))
584 memset(r, 0, sizeof(*r));
585 if (result != OF_BAD_ADDR) {
587 r->end = result + size - 1;
593 r->name = op->node->name;
597 static struct of_device * __init scan_one_device(struct device_node *dp,
598 struct device *parent)
600 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
609 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
611 op->portid = of_getintprop_default(dp, "upa-portid", -1);
612 if (op->portid == -1)
613 op->portid = of_getintprop_default(dp, "portid", -1);
615 irq = of_get_property(dp, "interrupts", &len);
619 op->irq = 0xffffffff;
621 build_device_resources(op, parent);
623 op->dev.parent = parent;
624 op->dev.bus = &of_bus_type;
626 strcpy(op->dev.bus_id, "root");
628 strcpy(op->dev.bus_id, dp->path_component_name);
630 if (of_device_register(op)) {
631 printk("%s: Could not register of device.\n",
640 static void __init scan_tree(struct device_node *dp, struct device *parent)
643 struct of_device *op = scan_one_device(dp, parent);
646 scan_tree(dp->child, &op->dev);
652 static void __init scan_of_devices(void)
654 struct device_node *root = of_find_node_by_path("/");
655 struct of_device *parent;
657 parent = scan_one_device(root, NULL);
661 scan_tree(root->child, &parent->dev);
664 static int __init of_bus_driver_init(void)
668 err = bus_register(&of_bus_type);
671 err = bus_register(&isa_bus_type);
673 err = bus_register(&ebus_bus_type);
677 err = bus_register(&sbus_bus_type);
686 postcore_initcall(of_bus_driver_init);
688 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
690 /* initialize common driver fields */
691 drv->driver.name = drv->name;
692 drv->driver.bus = bus;
694 /* register with core */
695 return driver_register(&drv->driver);
698 void of_unregister_driver(struct of_platform_driver *drv)
700 driver_unregister(&drv->driver);
704 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
706 struct of_device *ofdev;
708 ofdev = to_of_device(dev);
709 return sprintf(buf, "%s", ofdev->node->full_name);
712 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
715 * of_release_dev - free an of device structure when all users of it are finished.
716 * @dev: device that's been disconnected
718 * Will be called only by the device core when all users of this of device are
721 void of_release_dev(struct device *dev)
723 struct of_device *ofdev;
725 ofdev = to_of_device(dev);
730 int of_device_register(struct of_device *ofdev)
734 BUG_ON(ofdev->node == NULL);
736 rc = device_register(&ofdev->dev);
740 device_create_file(&ofdev->dev, &dev_attr_devspec);
745 void of_device_unregister(struct of_device *ofdev)
747 device_remove_file(&ofdev->dev, &dev_attr_devspec);
748 device_unregister(&ofdev->dev);
751 struct of_device* of_platform_device_create(struct device_node *np,
753 struct device *parent,
754 struct bus_type *bus)
756 struct of_device *dev;
758 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
761 memset(dev, 0, sizeof(*dev));
763 dev->dev.parent = parent;
765 dev->dev.release = of_release_dev;
767 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
769 if (of_device_register(dev) != 0) {
777 EXPORT_SYMBOL(of_match_device);
778 EXPORT_SYMBOL(of_register_driver);
779 EXPORT_SYMBOL(of_unregister_driver);
780 EXPORT_SYMBOL(of_device_register);
781 EXPORT_SYMBOL(of_device_unregister);
782 EXPORT_SYMBOL(of_dev_get);
783 EXPORT_SYMBOL(of_dev_put);
784 EXPORT_SYMBOL(of_platform_device_create);
785 EXPORT_SYMBOL(of_release_dev);