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>
9 #include <asm/of_device.h>
12 * of_match_device - Tell if an of_device structure has a matching
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)
25 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
28 match &= dev->node->name
29 && !strcmp(matches->name, dev->node->name);
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,
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;
52 return of_match_device(matches, of_dev) != NULL;
55 struct of_device *of_dev_get(struct of_device *dev)
61 tmp = get_device(&dev->dev);
63 return to_of_device(tmp);
68 void of_dev_put(struct of_device *dev)
71 put_device(&dev->dev);
75 static int of_device_probe(struct device *dev)
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);
90 match = of_match_device(drv->match_table, of_dev);
92 error = drv->probe(of_dev, match);
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)
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);
115 if (dev->driver && drv->suspend)
116 error = drv->suspend(of_dev, state);
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);
126 if (dev->driver && drv->resume)
127 error = drv->resume(of_dev);
131 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
133 unsigned long ret = res->start + offset;
136 if (res->flags & IORESOURCE_MEM)
137 r = request_mem_region(ret, size, name);
139 r = request_region(ret, size, name);
143 return (void __iomem *) ret;
145 EXPORT_SYMBOL(of_ioremap);
147 void of_iounmap(void __iomem *base, unsigned long size)
149 release_region((unsigned long) base, size);
151 EXPORT_SYMBOL(of_iounmap);
153 static int node_match(struct device *dev, void *data)
155 struct of_device *op = to_of_device(dev);
156 struct device_node *dp = data;
158 return (op->node == dp);
161 struct of_device *of_find_device_by_node(struct device_node *dp)
163 struct device *dev = bus_find_device(&of_bus_type, NULL,
167 return to_of_device(dev);
171 EXPORT_SYMBOL(of_find_device_by_node);
174 struct bus_type isa_bus_type = {
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,
182 EXPORT_SYMBOL(isa_bus_type);
184 struct bus_type ebus_bus_type = {
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,
192 EXPORT_SYMBOL(ebus_bus_type);
196 struct bus_type sbus_bus_type = {
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,
204 EXPORT_SYMBOL(sbus_bus_type);
207 struct bus_type of_bus_type = {
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,
215 EXPORT_SYMBOL(of_bus_type);
217 static inline u64 of_read_addr(const u32 *cell, int size)
221 r = (r << 32) | *(cell++);
225 static void __init get_cells(struct device_node *dp,
226 int *addrc, int *sizec)
229 *addrc = of_n_addr_cells(dp);
231 *sizec = of_n_size_cells(dp);
234 /* Max address size we deal with */
235 #define OF_MAX_ADDR_CELLS 4
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);
249 * Default translator (generic bus)
252 static void of_bus_default_count_cells(struct device_node *dev,
253 int *addrc, int *sizec)
255 get_cells(dev, addrc, sizec);
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.
261 static int of_out_of_range(const u32 *addr, const u32 *base,
262 const u32 *size, int na, int ns)
264 u64 a = of_read_addr(addr, na);
265 u64 b = of_read_addr(base, na);
270 b += of_read_addr(size, ns);
277 static int of_bus_default_map(u32 *addr, const u32 *range,
278 int na, int ns, int pna)
280 u32 result[OF_MAX_ADDR_CELLS];
284 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
288 if (of_out_of_range(addr, range, range + na + pna, na, ns))
291 /* Start with the parent range base. */
292 memcpy(result, range + na, pna * 4);
294 /* Add in the child address offset. */
295 for (i = 0; i < na; i++)
296 result[pna - 1 - i] +=
300 memcpy(addr, result, pna * 4);
305 static unsigned int of_bus_default_get_flags(u32 *addr)
307 return IORESOURCE_MEM;
311 * PCI bus specific translator
314 static int of_bus_pci_match(struct device_node *np)
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.
323 if (!of_find_property(np, "ranges", NULL))
332 static void of_bus_pci_count_cells(struct device_node *np,
333 int *addrc, int *sizec)
341 static int of_bus_pci_map(u32 *addr, const u32 *range,
342 int na, int ns, int pna)
344 u32 result[OF_MAX_ADDR_CELLS];
347 /* Check address type match */
348 if ((addr[0] ^ range[0]) & 0x03000000)
351 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
355 /* Start with the parent range base. */
356 memcpy(result, range + na, pna * 4);
358 /* Add in the child address offset, skipping high cell. */
359 for (i = 0; i < na - 1; i++)
360 result[pna - 1 - i] +=
364 memcpy(addr, result, pna * 4);
369 static unsigned int of_bus_pci_get_flags(u32 *addr)
371 unsigned int flags = 0;
374 switch((w >> 24) & 0x03) {
376 flags |= IORESOURCE_IO;
377 case 0x02: /* 32 bits */
378 case 0x03: /* 64 bits */
379 flags |= IORESOURCE_MEM;
382 flags |= IORESOURCE_PREFETCH;
387 * SBUS bus specific translator
390 static int of_bus_sbus_match(struct device_node *np)
392 return !strcmp(np->name, "sbus") ||
393 !strcmp(np->name, "sbi");
396 static void of_bus_sbus_count_cells(struct device_node *child,
397 int *addrc, int *sizec)
405 static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
407 return of_bus_default_map(addr, range, na, ns, pna);
410 static unsigned int of_bus_sbus_get_flags(u32 *addr)
412 return IORESOURCE_MEM;
417 * Array of bus specific translators
420 static struct of_bus of_busses[] = {
424 .addr_prop_name = "assigned-addresses",
425 .match = of_bus_pci_match,
426 .count_cells = of_bus_pci_count_cells,
427 .map = of_bus_pci_map,
428 .get_flags = of_bus_pci_get_flags,
433 .addr_prop_name = "reg",
434 .match = of_bus_sbus_match,
435 .count_cells = of_bus_sbus_count_cells,
436 .map = of_bus_sbus_map,
437 .get_flags = of_bus_sbus_get_flags,
442 .addr_prop_name = "reg",
444 .count_cells = of_bus_default_count_cells,
445 .map = of_bus_default_map,
446 .get_flags = of_bus_default_get_flags,
450 static struct of_bus *of_match_bus(struct device_node *np)
454 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
455 if (!of_busses[i].match || of_busses[i].match(np))
456 return &of_busses[i];
461 static int __init build_one_resource(struct device_node *parent,
465 int na, int ns, int pna)
471 ranges = of_get_property(parent, "ranges", &rlen);
472 if (ranges == NULL || rlen == 0) {
473 u32 result[OF_MAX_ADDR_CELLS];
476 memset(result, 0, pna * 4);
477 for (i = 0; i < na; i++)
478 result[pna - 1 - i] =
481 memcpy(addr, result, pna * 4);
485 /* Now walk through the ranges */
487 rone = na + pna + ns;
488 for (; rlen >= rone; rlen -= rone, ranges += rone) {
489 if (!bus->map(addr, ranges, na, ns, pna))
496 static int __init use_1to1_mapping(struct device_node *pp)
500 /* If this is on the PMU bus, don't try to translate it even
501 * if a ranges property exists.
503 if (!strcmp(pp->name, "pmu"))
506 /* If we have a ranges property in the parent, use it. */
507 if (of_find_property(pp, "ranges", NULL) != NULL)
510 /* If the parent is the dma node of an ISA bus, pass
511 * the translation up to the root.
513 if (!strcmp(pp->name, "dma"))
516 /* Similarly for Simba PCI bridges. */
517 model = of_get_property(pp, "model", NULL);
518 if (model && !strcmp(model, "SUNW,simba"))
524 static int of_resource_verbose;
526 static void __init build_device_resources(struct of_device *op,
527 struct device *parent)
529 struct of_device *p_op;
538 p_op = to_of_device(parent);
539 bus = of_match_bus(p_op->node);
540 bus->count_cells(op->node, &na, &ns);
542 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
543 if (!preg || num_reg == 0)
546 /* Convert to num-cells. */
549 /* Convert to num-entries. */
552 /* Prevent overruning the op->resources[] array. */
553 if (num_reg > PROMREG_MAX) {
554 printk(KERN_WARNING "%s: Too many regs (%d), "
556 op->node->full_name, num_reg, PROMREG_MAX);
557 num_reg = PROMREG_MAX;
560 for (index = 0; index < num_reg; index++) {
561 struct resource *r = &op->resource[index];
562 u32 addr[OF_MAX_ADDR_CELLS];
563 u32 *reg = (preg + (index * ((na + ns) * 4)));
564 struct device_node *dp = op->node;
565 struct device_node *pp = p_op->node;
567 u64 size, result = OF_BAD_ADDR;
572 size = of_read_addr(reg + na, ns);
573 flags = bus->get_flags(reg);
575 memcpy(addr, reg, na * 4);
577 if (use_1to1_mapping(pp)) {
578 result = of_read_addr(addr, na);
589 result = of_read_addr(addr, dna);
593 pbus = of_match_bus(pp);
594 pbus->count_cells(dp, &pna, &pns);
596 if (build_one_resource(dp, bus, pbus, addr,
606 memset(r, 0, sizeof(*r));
608 if (of_resource_verbose)
609 printk("%s reg[%d] -> %lx\n",
610 op->node->full_name, index,
613 if (result != OF_BAD_ADDR) {
614 if (tlb_type == hypervisor)
615 result &= 0x0fffffffffffffffUL;
618 r->end = result + size - 1;
624 r->name = op->node->name;
628 static struct device_node * __init
629 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
630 u32 *imap, int imlen, u32 *imask,
633 struct device_node *cp;
634 unsigned int irq = *irq_p;
640 bus = of_match_bus(pp);
641 bus->count_cells(dp, &na, NULL);
643 reg = of_get_property(dp, "reg", &num_reg);
644 if (!reg || !num_reg)
647 imlen /= ((na + 3) * 4);
649 for (i = 0; i < imlen; i++) {
652 for (j = 0; j < na; j++) {
653 if ((reg[j] & imask[j]) != imap[j])
656 if (imap[na] == irq) {
657 handle = imap[na + 1];
666 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
667 * properties that do not include the on-board device
668 * interrupts. Instead, the device's 'interrupts' property
669 * is already a fully specified INO value.
671 * Handle this by deciding that, if we didn't get a
672 * match in the parent's 'interrupt-map', and the
673 * parent is an IRQ translater, then use the parent as
674 * our IRQ controller.
683 cp = of_find_node_by_phandle(handle);
688 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
689 struct device_node *pp,
692 struct linux_prom_pci_registers *regs;
693 unsigned int devfn, slot, ret;
695 if (irq < 1 || irq > 4)
698 regs = of_get_property(dp, "reg", NULL);
702 devfn = (regs->phys_hi >> 8) & 0xff;
703 slot = (devfn >> 3) & 0x1f;
705 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
710 static int of_irq_verbose;
712 static unsigned int __init build_one_device_irq(struct of_device *op,
713 struct device *parent,
716 struct device_node *dp = op->node;
717 struct device_node *pp, *ip;
718 unsigned int orig_irq = irq;
720 if (irq == 0xffffffff)
724 irq = dp->irq_trans->irq_build(dp, irq,
725 dp->irq_trans->data);
728 printk("%s: direct translate %x --> %x\n",
729 dp->full_name, orig_irq, irq);
734 /* Something more complicated. Walk up to the root, applying
735 * interrupt-map or bus specific translations, until we hit
738 * If we hit a bus type or situation we cannot handle, we
739 * stop and assume that the original IRQ number was in a
740 * format which has special meaning to it's immediate parent.
748 imap = of_get_property(pp, "interrupt-map", &imlen);
749 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
751 struct device_node *iret;
752 int this_orig_irq = irq;
754 iret = apply_interrupt_map(dp, pp,
759 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
761 pp->full_name, this_orig_irq,
762 (iret ? iret->full_name : "NULL"), irq);
767 if (iret->irq_trans) {
772 if (!strcmp(pp->type, "pci") ||
773 !strcmp(pp->type, "pciex")) {
774 unsigned int this_orig_irq = irq;
776 irq = pci_irq_swizzle(dp, pp, irq);
778 printk("%s: PCI swizzle [%s] "
781 pp->full_name, this_orig_irq,
797 irq = ip->irq_trans->irq_build(op->node, irq,
798 ip->irq_trans->data);
800 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
801 op->node->full_name, ip->full_name, orig_irq, irq);
806 static struct of_device * __init scan_one_device(struct device_node *dp,
807 struct device *parent)
809 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
818 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
820 op->portid = of_getintprop_default(dp, "upa-portid", -1);
821 if (op->portid == -1)
822 op->portid = of_getintprop_default(dp, "portid", -1);
824 irq = of_get_property(dp, "interrupts", &len);
826 memcpy(op->irqs, irq, len);
827 op->num_irqs = len / 4;
832 /* Prevent overruning the op->irqs[] array. */
833 if (op->num_irqs > PROMINTR_MAX) {
834 printk(KERN_WARNING "%s: Too many irqs (%d), "
836 dp->full_name, op->num_irqs, PROMINTR_MAX);
837 op->num_irqs = PROMINTR_MAX;
840 build_device_resources(op, parent);
841 for (i = 0; i < op->num_irqs; i++)
842 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
844 op->dev.parent = parent;
845 op->dev.bus = &of_bus_type;
847 strcpy(op->dev.bus_id, "root");
849 sprintf(op->dev.bus_id, "%s@%08x", dp->name, dp->node);
851 if (of_device_register(op)) {
852 printk("%s: Could not register of device.\n",
861 static void __init scan_tree(struct device_node *dp, struct device *parent)
864 struct of_device *op = scan_one_device(dp, parent);
867 scan_tree(dp->child, &op->dev);
873 static void __init scan_of_devices(void)
875 struct device_node *root = of_find_node_by_path("/");
876 struct of_device *parent;
878 parent = scan_one_device(root, NULL);
882 scan_tree(root->child, &parent->dev);
885 static int __init of_bus_driver_init(void)
889 err = bus_register(&of_bus_type);
892 err = bus_register(&isa_bus_type);
894 err = bus_register(&ebus_bus_type);
898 err = bus_register(&sbus_bus_type);
907 postcore_initcall(of_bus_driver_init);
909 static int __init of_debug(char *str)
913 get_option(&str, &val);
915 of_resource_verbose = 1;
921 __setup("of_debug=", of_debug);
923 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
925 /* initialize common driver fields */
926 drv->driver.name = drv->name;
927 drv->driver.bus = bus;
929 /* register with core */
930 return driver_register(&drv->driver);
933 void of_unregister_driver(struct of_platform_driver *drv)
935 driver_unregister(&drv->driver);
939 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
941 struct of_device *ofdev;
943 ofdev = to_of_device(dev);
944 return sprintf(buf, "%s", ofdev->node->full_name);
947 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
950 * of_release_dev - free an of device structure when all users of it are finished.
951 * @dev: device that's been disconnected
953 * Will be called only by the device core when all users of this of device are
956 void of_release_dev(struct device *dev)
958 struct of_device *ofdev;
960 ofdev = to_of_device(dev);
965 int of_device_register(struct of_device *ofdev)
969 BUG_ON(ofdev->node == NULL);
971 rc = device_register(&ofdev->dev);
975 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
977 device_unregister(&ofdev->dev);
982 void of_device_unregister(struct of_device *ofdev)
984 device_remove_file(&ofdev->dev, &dev_attr_devspec);
985 device_unregister(&ofdev->dev);
988 struct of_device* of_platform_device_create(struct device_node *np,
990 struct device *parent,
991 struct bus_type *bus)
993 struct of_device *dev;
995 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
998 memset(dev, 0, sizeof(*dev));
1000 dev->dev.parent = parent;
1002 dev->dev.release = of_release_dev;
1004 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
1006 if (of_device_register(dev) != 0) {
1014 EXPORT_SYMBOL(of_match_device);
1015 EXPORT_SYMBOL(of_register_driver);
1016 EXPORT_SYMBOL(of_unregister_driver);
1017 EXPORT_SYMBOL(of_device_register);
1018 EXPORT_SYMBOL(of_device_unregister);
1019 EXPORT_SYMBOL(of_dev_get);
1020 EXPORT_SYMBOL(of_dev_put);
1021 EXPORT_SYMBOL(of_platform_device_create);
1022 EXPORT_SYMBOL(of_release_dev);