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);
133 struct bus_type isa_bus_type = {
135 .match = of_platform_bus_match,
136 .probe = of_device_probe,
137 .remove = of_device_remove,
138 .suspend = of_device_suspend,
139 .resume = of_device_resume,
141 EXPORT_SYMBOL(isa_bus_type);
143 struct bus_type ebus_bus_type = {
145 .match = of_platform_bus_match,
146 .probe = of_device_probe,
147 .remove = of_device_remove,
148 .suspend = of_device_suspend,
149 .resume = of_device_resume,
151 EXPORT_SYMBOL(ebus_bus_type);
155 struct bus_type sbus_bus_type = {
157 .match = of_platform_bus_match,
158 .probe = of_device_probe,
159 .remove = of_device_remove,
160 .suspend = of_device_suspend,
161 .resume = of_device_resume,
163 EXPORT_SYMBOL(sbus_bus_type);
166 struct bus_type of_bus_type = {
168 .match = of_platform_bus_match,
169 .probe = of_device_probe,
170 .remove = of_device_remove,
171 .suspend = of_device_suspend,
172 .resume = of_device_resume,
174 EXPORT_SYMBOL(of_bus_type);
176 static inline u64 of_read_addr(u32 *cell, int size)
180 r = (r << 32) | *(cell++);
184 static void __init get_cells(struct device_node *dp,
185 int *addrc, int *sizec)
188 *addrc = of_n_addr_cells(dp);
190 *sizec = of_n_size_cells(dp);
193 /* Max address size we deal with */
194 #define OF_MAX_ADDR_CELLS 4
198 const char *addr_prop_name;
199 int (*match)(struct device_node *parent);
200 void (*count_cells)(struct device_node *child,
201 int *addrc, int *sizec);
202 u64 (*map)(u32 *addr, u32 *range, int na, int ns, int pna);
203 int (*translate)(u32 *addr, u64 offset, int na);
204 unsigned int (*get_flags)(u32 *addr);
208 * Default translator (generic bus)
211 static void of_bus_default_count_cells(struct device_node *dev,
212 int *addrc, int *sizec)
214 get_cells(dev, addrc, sizec);
217 static u64 of_bus_default_map(u32 *addr, u32 *range, int na, int ns, int pna)
221 cp = of_read_addr(range, na);
222 s = of_read_addr(range + na + pna, ns);
223 da = of_read_addr(addr, na);
225 if (da < cp || da >= (cp + s))
230 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
232 u64 a = of_read_addr(addr, na);
233 memset(addr, 0, na * 4);
236 addr[na - 2] = a >> 32;
237 addr[na - 1] = a & 0xffffffffu;
242 static unsigned int of_bus_default_get_flags(u32 *addr)
244 return IORESOURCE_MEM;
249 * PCI bus specific translator
252 static int of_bus_pci_match(struct device_node *np)
254 return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex");
257 static void of_bus_pci_count_cells(struct device_node *np,
258 int *addrc, int *sizec)
266 static u64 of_bus_pci_map(u32 *addr, u32 *range, int na, int ns, int pna)
270 /* Check address type match */
271 if ((addr[0] ^ range[0]) & 0x03000000)
274 /* Read address values, skipping high cell */
275 cp = of_read_addr(range + 1, na - 1);
276 s = of_read_addr(range + na + pna, ns);
277 da = of_read_addr(addr + 1, na - 1);
279 if (da < cp || da >= (cp + s))
284 static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
286 return of_bus_default_translate(addr + 1, offset, na - 1);
289 static unsigned int of_bus_pci_get_flags(u32 *addr)
291 unsigned int flags = 0;
294 switch((w >> 24) & 0x03) {
296 flags |= IORESOURCE_IO;
297 case 0x02: /* 32 bits */
298 case 0x03: /* 64 bits */
299 flags |= IORESOURCE_MEM;
302 flags |= IORESOURCE_PREFETCH;
307 * ISA bus specific translator
310 static int of_bus_isa_match(struct device_node *np)
312 return !strcmp(np->name, "isa");
315 static void of_bus_isa_count_cells(struct device_node *child,
316 int *addrc, int *sizec)
324 static u64 of_bus_isa_map(u32 *addr, u32 *range, int na, int ns, int pna)
328 /* Check address type match */
329 if ((addr[0] ^ range[0]) & 0x00000001)
332 /* Read address values, skipping high cell */
333 cp = of_read_addr(range + 1, na - 1);
334 s = of_read_addr(range + na + pna, ns);
335 da = of_read_addr(addr + 1, na - 1);
337 if (da < cp || da >= (cp + s))
342 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
344 return of_bus_default_translate(addr + 1, offset, na - 1);
347 static unsigned int of_bus_isa_get_flags(u32 *addr)
349 unsigned int flags = 0;
353 flags |= IORESOURCE_IO;
355 flags |= IORESOURCE_MEM;
360 * SBUS bus specific translator
363 static int of_bus_sbus_match(struct device_node *np)
365 return !strcmp(np->name, "sbus") ||
366 !strcmp(np->name, "sbi");
369 static void of_bus_sbus_count_cells(struct device_node *child,
370 int *addrc, int *sizec)
378 static u64 of_bus_sbus_map(u32 *addr, u32 *range, int na, int ns, int pna)
380 return of_bus_default_map(addr, range, na, ns, pna);
383 static int of_bus_sbus_translate(u32 *addr, u64 offset, int na)
385 return of_bus_default_translate(addr, offset, na);
388 static unsigned int of_bus_sbus_get_flags(u32 *addr)
390 return IORESOURCE_MEM;
395 * Array of bus specific translators
398 static struct of_bus of_busses[] = {
402 .addr_prop_name = "assigned-addresses",
403 .match = of_bus_pci_match,
404 .count_cells = of_bus_pci_count_cells,
405 .map = of_bus_pci_map,
406 .translate = of_bus_pci_translate,
407 .get_flags = of_bus_pci_get_flags,
412 .addr_prop_name = "reg",
413 .match = of_bus_isa_match,
414 .count_cells = of_bus_isa_count_cells,
415 .map = of_bus_isa_map,
416 .translate = of_bus_isa_translate,
417 .get_flags = of_bus_isa_get_flags,
422 .addr_prop_name = "reg",
423 .match = of_bus_sbus_match,
424 .count_cells = of_bus_sbus_count_cells,
425 .map = of_bus_sbus_map,
426 .translate = of_bus_sbus_translate,
427 .get_flags = of_bus_sbus_get_flags,
432 .addr_prop_name = "reg",
434 .count_cells = of_bus_default_count_cells,
435 .map = of_bus_default_map,
436 .translate = of_bus_default_translate,
437 .get_flags = of_bus_default_get_flags,
441 static struct of_bus *of_match_bus(struct device_node *np)
445 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
446 if (!of_busses[i].match || of_busses[i].match(np))
447 return &of_busses[i];
452 static int __init build_one_resource(struct device_node *parent,
456 int na, int ns, int pna)
461 u64 offset = OF_BAD_ADDR;
463 ranges = of_get_property(parent, "ranges", &rlen);
464 if (ranges == NULL || rlen == 0) {
465 offset = of_read_addr(addr, na);
466 memset(addr, 0, pna * 4);
470 /* Now walk through the ranges */
472 rone = na + pna + ns;
473 for (; rlen >= rone; rlen -= rone, ranges += rone) {
474 offset = bus->map(addr, ranges, na, ns, pna);
475 if (offset != OF_BAD_ADDR)
478 if (offset == OF_BAD_ADDR)
481 memcpy(addr, ranges + na, 4 * pna);
484 /* Translate it into parent bus space */
485 return pbus->translate(addr, offset, pna);
488 static void __init build_device_resources(struct of_device *op,
489 struct device *parent)
491 struct of_device *p_op;
500 p_op = to_of_device(parent);
501 bus = of_match_bus(p_op->node);
502 bus->count_cells(op->node, &na, &ns);
504 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
505 if (!preg || num_reg == 0)
508 /* Convert to num-cells. */
511 /* Conver to num-entries. */
514 for (index = 0; index < num_reg; index++) {
515 struct resource *r = &op->resource[index];
516 u32 addr[OF_MAX_ADDR_CELLS];
517 u32 *reg = (preg + (index * ((na + ns) * 4)));
518 struct device_node *dp = op->node;
519 struct device_node *pp = p_op->node;
521 u64 size, result = OF_BAD_ADDR;
526 size = of_read_addr(reg + na, ns);
527 flags = bus->get_flags(reg);
529 memcpy(addr, reg, na * 4);
531 /* If the immediate parent has no ranges property to apply,
532 * just use a 1<->1 mapping. Unless it is the 'dma' child
533 * of an isa bus, which must be passed up towards the root.
535 * Also, don't try to translate PMU bus device registers.
537 if ((of_find_property(pp, "ranges", NULL) == NULL &&
538 strcmp(pp->name, "dma") != 0) ||
539 !strcmp(pp->name, "pmu")) {
540 result = of_read_addr(addr, na);
551 result = of_read_addr(addr, dna);
555 pbus = of_match_bus(pp);
556 pbus->count_cells(dp, &pna, &pns);
558 if (build_one_resource(dp, bus, pbus, addr, dna, dns, pna))
567 memset(r, 0, sizeof(*r));
568 if (result != OF_BAD_ADDR) {
570 r->end = result + size - 1;
576 r->name = op->node->name;
580 static struct of_device * __init scan_one_device(struct device_node *dp,
581 struct device *parent)
583 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
592 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
594 op->portid = of_getintprop_default(dp, "upa-portid", -1);
595 if (op->portid == -1)
596 op->portid = of_getintprop_default(dp, "portid", -1);
598 irq = of_get_property(dp, "interrupts", &len);
602 op->irq = 0xffffffff;
604 build_device_resources(op, parent);
606 op->dev.parent = parent;
607 op->dev.bus = &of_bus_type;
609 strcpy(op->dev.bus_id, "root");
611 strcpy(op->dev.bus_id, dp->path_component_name);
613 if (of_device_register(op)) {
614 printk("%s: Could not register of device.\n",
623 static void __init scan_tree(struct device_node *dp, struct device *parent)
626 struct of_device *op = scan_one_device(dp, parent);
629 scan_tree(dp->child, &op->dev);
635 static void __init scan_of_devices(void)
637 struct device_node *root = of_find_node_by_path("/");
638 struct of_device *parent;
640 parent = scan_one_device(root, NULL);
644 scan_tree(root->child, &parent->dev);
647 static int __init of_bus_driver_init(void)
651 err = bus_register(&of_bus_type);
654 err = bus_register(&isa_bus_type);
656 err = bus_register(&ebus_bus_type);
660 err = bus_register(&sbus_bus_type);
669 postcore_initcall(of_bus_driver_init);
671 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
673 /* initialize common driver fields */
674 drv->driver.name = drv->name;
675 drv->driver.bus = bus;
677 /* register with core */
678 return driver_register(&drv->driver);
681 void of_unregister_driver(struct of_platform_driver *drv)
683 driver_unregister(&drv->driver);
687 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
689 struct of_device *ofdev;
691 ofdev = to_of_device(dev);
692 return sprintf(buf, "%s", ofdev->node->full_name);
695 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
698 * of_release_dev - free an of device structure when all users of it are finished.
699 * @dev: device that's been disconnected
701 * Will be called only by the device core when all users of this of device are
704 void of_release_dev(struct device *dev)
706 struct of_device *ofdev;
708 ofdev = to_of_device(dev);
713 int of_device_register(struct of_device *ofdev)
717 BUG_ON(ofdev->node == NULL);
719 rc = device_register(&ofdev->dev);
723 device_create_file(&ofdev->dev, &dev_attr_devspec);
728 void of_device_unregister(struct of_device *ofdev)
730 device_remove_file(&ofdev->dev, &dev_attr_devspec);
731 device_unregister(&ofdev->dev);
734 struct of_device* of_platform_device_create(struct device_node *np,
736 struct device *parent,
737 struct bus_type *bus)
739 struct of_device *dev;
741 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
744 memset(dev, 0, sizeof(*dev));
746 dev->dev.parent = parent;
748 dev->dev.release = of_release_dev;
750 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
752 if (of_device_register(dev) != 0) {
760 EXPORT_SYMBOL(of_match_device);
761 EXPORT_SYMBOL(of_register_driver);
762 EXPORT_SYMBOL(of_unregister_driver);
763 EXPORT_SYMBOL(of_device_register);
764 EXPORT_SYMBOL(of_device_unregister);
765 EXPORT_SYMBOL(of_dev_get);
766 EXPORT_SYMBOL(of_dev_put);
767 EXPORT_SYMBOL(of_platform_device_create);
768 EXPORT_SYMBOL(of_release_dev);