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;
135 if (!request_region(ret, size, name))
138 return (void __iomem *) ret;
140 EXPORT_SYMBOL(of_ioremap);
142 void of_iounmap(void __iomem *base, unsigned long size)
144 release_region((unsigned long) base, size);
146 EXPORT_SYMBOL(of_iounmap);
148 static int node_match(struct device *dev, void *data)
150 struct of_device *op = to_of_device(dev);
151 struct device_node *dp = data;
153 return (op->node == dp);
156 struct of_device *of_find_device_by_node(struct device_node *dp)
158 struct device *dev = bus_find_device(&of_bus_type, NULL,
162 return to_of_device(dev);
166 EXPORT_SYMBOL(of_find_device_by_node);
169 struct bus_type isa_bus_type = {
171 .match = of_platform_bus_match,
172 .probe = of_device_probe,
173 .remove = of_device_remove,
174 .suspend = of_device_suspend,
175 .resume = of_device_resume,
177 EXPORT_SYMBOL(isa_bus_type);
179 struct bus_type ebus_bus_type = {
181 .match = of_platform_bus_match,
182 .probe = of_device_probe,
183 .remove = of_device_remove,
184 .suspend = of_device_suspend,
185 .resume = of_device_resume,
187 EXPORT_SYMBOL(ebus_bus_type);
191 struct bus_type sbus_bus_type = {
193 .match = of_platform_bus_match,
194 .probe = of_device_probe,
195 .remove = of_device_remove,
196 .suspend = of_device_suspend,
197 .resume = of_device_resume,
199 EXPORT_SYMBOL(sbus_bus_type);
202 struct bus_type of_bus_type = {
204 .match = of_platform_bus_match,
205 .probe = of_device_probe,
206 .remove = of_device_remove,
207 .suspend = of_device_suspend,
208 .resume = of_device_resume,
210 EXPORT_SYMBOL(of_bus_type);
212 static inline u64 of_read_addr(const u32 *cell, int size)
216 r = (r << 32) | *(cell++);
220 static void __init get_cells(struct device_node *dp,
221 int *addrc, int *sizec)
224 *addrc = of_n_addr_cells(dp);
226 *sizec = of_n_size_cells(dp);
229 /* Max address size we deal with */
230 #define OF_MAX_ADDR_CELLS 4
234 const char *addr_prop_name;
235 int (*match)(struct device_node *parent);
236 void (*count_cells)(struct device_node *child,
237 int *addrc, int *sizec);
238 int (*map)(u32 *addr, const u32 *range,
239 int na, int ns, int pna);
240 unsigned int (*get_flags)(u32 *addr);
244 * Default translator (generic bus)
247 static void of_bus_default_count_cells(struct device_node *dev,
248 int *addrc, int *sizec)
250 get_cells(dev, addrc, sizec);
253 /* Make sure the least significant 64-bits are in-range. Even
254 * for 3 or 4 cell values it is a good enough approximation.
256 static int of_out_of_range(const u32 *addr, const u32 *base,
257 const u32 *size, int na, int ns)
259 u64 a = of_read_addr(addr, na);
260 u64 b = of_read_addr(base, na);
265 b += of_read_addr(size, ns);
272 static int of_bus_default_map(u32 *addr, const u32 *range,
273 int na, int ns, int pna)
275 u32 result[OF_MAX_ADDR_CELLS];
279 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
283 if (of_out_of_range(addr, range, range + na + pna, na, ns))
286 /* Start with the parent range base. */
287 memcpy(result, range + na, pna * 4);
289 /* Add in the child address offset. */
290 for (i = 0; i < na; i++)
291 result[pna - 1 - i] +=
295 memcpy(addr, result, pna * 4);
300 static unsigned int of_bus_default_get_flags(u32 *addr)
302 return IORESOURCE_MEM;
306 * PCI bus specific translator
309 static int of_bus_pci_match(struct device_node *np)
311 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
312 /* Do not do PCI specific frobbing if the
313 * PCI bridge lacks a ranges property. We
314 * want to pass it through up to the next
315 * parent as-is, not with the PCI translate
316 * method which chops off the top address cell.
318 if (!of_find_property(np, "ranges", NULL))
327 static void of_bus_pci_count_cells(struct device_node *np,
328 int *addrc, int *sizec)
336 static int of_bus_pci_map(u32 *addr, const u32 *range,
337 int na, int ns, int pna)
339 u32 result[OF_MAX_ADDR_CELLS];
342 /* Check address type match */
343 if ((addr[0] ^ range[0]) & 0x03000000)
346 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
350 /* Start with the parent range base. */
351 memcpy(result, range + na, pna * 4);
353 /* Add in the child address offset, skipping high cell. */
354 for (i = 0; i < na - 1; i++)
355 result[pna - 1 - i] +=
359 memcpy(addr, result, pna * 4);
364 static unsigned int of_bus_pci_get_flags(u32 *addr)
366 unsigned int flags = 0;
369 switch((w >> 24) & 0x03) {
371 flags |= IORESOURCE_IO;
372 case 0x02: /* 32 bits */
373 case 0x03: /* 64 bits */
374 flags |= IORESOURCE_MEM;
377 flags |= IORESOURCE_PREFETCH;
382 * SBUS bus specific translator
385 static int of_bus_sbus_match(struct device_node *np)
387 return !strcmp(np->name, "sbus") ||
388 !strcmp(np->name, "sbi");
391 static void of_bus_sbus_count_cells(struct device_node *child,
392 int *addrc, int *sizec)
400 static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
402 return of_bus_default_map(addr, range, na, ns, pna);
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 .get_flags = of_bus_pci_get_flags,
428 .addr_prop_name = "reg",
429 .match = of_bus_sbus_match,
430 .count_cells = of_bus_sbus_count_cells,
431 .map = of_bus_sbus_map,
432 .get_flags = of_bus_sbus_get_flags,
437 .addr_prop_name = "reg",
439 .count_cells = of_bus_default_count_cells,
440 .map = of_bus_default_map,
441 .get_flags = of_bus_default_get_flags,
445 static struct of_bus *of_match_bus(struct device_node *np)
449 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
450 if (!of_busses[i].match || of_busses[i].match(np))
451 return &of_busses[i];
456 static int __init build_one_resource(struct device_node *parent,
460 int na, int ns, int pna)
466 ranges = of_get_property(parent, "ranges", &rlen);
467 if (ranges == NULL || rlen == 0) {
468 u32 result[OF_MAX_ADDR_CELLS];
471 memset(result, 0, pna * 4);
472 for (i = 0; i < na; i++)
473 result[pna - 1 - i] =
476 memcpy(addr, result, pna * 4);
480 /* Now walk through the ranges */
482 rone = na + pna + ns;
483 for (; rlen >= rone; rlen -= rone, ranges += rone) {
484 if (!bus->map(addr, ranges, na, ns, pna))
491 static int __init use_1to1_mapping(struct device_node *pp)
495 /* If this is on the PMU bus, don't try to translate it even
496 * if a ranges property exists.
498 if (!strcmp(pp->name, "pmu"))
501 /* If we have a ranges property in the parent, use it. */
502 if (of_find_property(pp, "ranges", NULL) != NULL)
505 /* If the parent is the dma node of an ISA bus, pass
506 * the translation up to the root.
508 if (!strcmp(pp->name, "dma"))
511 /* Similarly for Simba PCI bridges. */
512 model = of_get_property(pp, "model", NULL);
513 if (model && !strcmp(model, "SUNW,simba"))
519 static int of_resource_verbose;
521 static void __init build_device_resources(struct of_device *op,
522 struct device *parent)
524 struct of_device *p_op;
533 p_op = to_of_device(parent);
534 bus = of_match_bus(p_op->node);
535 bus->count_cells(op->node, &na, &ns);
537 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
538 if (!preg || num_reg == 0)
541 /* Convert to num-cells. */
544 /* Convert to num-entries. */
547 /* Prevent overruning the op->resources[] array. */
548 if (num_reg > PROMREG_MAX) {
549 printk(KERN_WARNING "%s: Too many regs (%d), "
551 op->node->full_name, num_reg, PROMREG_MAX);
552 num_reg = PROMREG_MAX;
555 for (index = 0; index < num_reg; index++) {
556 struct resource *r = &op->resource[index];
557 u32 addr[OF_MAX_ADDR_CELLS];
558 u32 *reg = (preg + (index * ((na + ns) * 4)));
559 struct device_node *dp = op->node;
560 struct device_node *pp = p_op->node;
562 u64 size, result = OF_BAD_ADDR;
567 size = of_read_addr(reg + na, ns);
568 flags = bus->get_flags(reg);
570 memcpy(addr, reg, na * 4);
572 if (use_1to1_mapping(pp)) {
573 result = of_read_addr(addr, na);
584 result = of_read_addr(addr, dna);
588 pbus = of_match_bus(pp);
589 pbus->count_cells(dp, &pna, &pns);
591 if (build_one_resource(dp, bus, pbus, addr,
601 memset(r, 0, sizeof(*r));
603 if (of_resource_verbose)
604 printk("%s reg[%d] -> %lx\n",
605 op->node->full_name, index,
608 if (result != OF_BAD_ADDR) {
609 if (tlb_type == hypervisor)
610 result &= 0x0fffffffffffffffUL;
613 r->end = result + size - 1;
619 r->name = op->node->name;
623 static struct device_node * __init
624 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
625 u32 *imap, int imlen, u32 *imask,
628 struct device_node *cp;
629 unsigned int irq = *irq_p;
635 bus = of_match_bus(pp);
636 bus->count_cells(dp, &na, NULL);
638 reg = of_get_property(dp, "reg", &num_reg);
639 if (!reg || !num_reg)
642 imlen /= ((na + 3) * 4);
644 for (i = 0; i < imlen; i++) {
647 for (j = 0; j < na; j++) {
648 if ((reg[j] & imask[j]) != imap[j])
651 if (imap[na] == irq) {
652 handle = imap[na + 1];
661 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
662 * properties that do not include the on-board device
663 * interrupts. Instead, the device's 'interrupts' property
664 * is already a fully specified INO value.
666 * Handle this by deciding that, if we didn't get a
667 * match in the parent's 'interrupt-map', and the
668 * parent is an IRQ translater, then use the parent as
669 * our IRQ controller.
678 cp = of_find_node_by_phandle(handle);
683 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
684 struct device_node *pp,
687 struct linux_prom_pci_registers *regs;
688 unsigned int devfn, slot, ret;
690 if (irq < 1 || irq > 4)
693 regs = of_get_property(dp, "reg", NULL);
697 devfn = (regs->phys_hi >> 8) & 0xff;
698 slot = (devfn >> 3) & 0x1f;
700 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
705 static int of_irq_verbose;
707 static unsigned int __init build_one_device_irq(struct of_device *op,
708 struct device *parent,
711 struct device_node *dp = op->node;
712 struct device_node *pp, *ip;
713 unsigned int orig_irq = irq;
715 if (irq == 0xffffffff)
719 irq = dp->irq_trans->irq_build(dp, irq,
720 dp->irq_trans->data);
723 printk("%s: direct translate %x --> %x\n",
724 dp->full_name, orig_irq, irq);
729 /* Something more complicated. Walk up to the root, applying
730 * interrupt-map or bus specific translations, until we hit
733 * If we hit a bus type or situation we cannot handle, we
734 * stop and assume that the original IRQ number was in a
735 * format which has special meaning to it's immediate parent.
743 imap = of_get_property(pp, "interrupt-map", &imlen);
744 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
746 struct device_node *iret;
747 int this_orig_irq = irq;
749 iret = apply_interrupt_map(dp, pp,
754 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
756 pp->full_name, this_orig_irq,
757 (iret ? iret->full_name : "NULL"), irq);
762 if (iret->irq_trans) {
767 if (!strcmp(pp->type, "pci") ||
768 !strcmp(pp->type, "pciex")) {
769 unsigned int this_orig_irq = irq;
771 irq = pci_irq_swizzle(dp, pp, irq);
773 printk("%s: PCI swizzle [%s] "
776 pp->full_name, this_orig_irq,
792 irq = ip->irq_trans->irq_build(op->node, irq,
793 ip->irq_trans->data);
795 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
796 op->node->full_name, ip->full_name, orig_irq, irq);
801 static struct of_device * __init scan_one_device(struct device_node *dp,
802 struct device *parent)
804 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
813 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
815 op->portid = of_getintprop_default(dp, "upa-portid", -1);
816 if (op->portid == -1)
817 op->portid = of_getintprop_default(dp, "portid", -1);
819 irq = of_get_property(dp, "interrupts", &len);
821 memcpy(op->irqs, irq, len);
822 op->num_irqs = len / 4;
827 /* Prevent overruning the op->irqs[] array. */
828 if (op->num_irqs > PROMINTR_MAX) {
829 printk(KERN_WARNING "%s: Too many irqs (%d), "
831 dp->full_name, op->num_irqs, PROMINTR_MAX);
832 op->num_irqs = PROMINTR_MAX;
835 build_device_resources(op, parent);
836 for (i = 0; i < op->num_irqs; i++)
837 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
839 op->dev.parent = parent;
840 op->dev.bus = &of_bus_type;
842 strcpy(op->dev.bus_id, "root");
844 sprintf(op->dev.bus_id, "%s@%08x", dp->name, dp->node);
846 if (of_device_register(op)) {
847 printk("%s: Could not register of device.\n",
856 static void __init scan_tree(struct device_node *dp, struct device *parent)
859 struct of_device *op = scan_one_device(dp, parent);
862 scan_tree(dp->child, &op->dev);
868 static void __init scan_of_devices(void)
870 struct device_node *root = of_find_node_by_path("/");
871 struct of_device *parent;
873 parent = scan_one_device(root, NULL);
877 scan_tree(root->child, &parent->dev);
880 static int __init of_bus_driver_init(void)
884 err = bus_register(&of_bus_type);
887 err = bus_register(&isa_bus_type);
889 err = bus_register(&ebus_bus_type);
893 err = bus_register(&sbus_bus_type);
902 postcore_initcall(of_bus_driver_init);
904 static int __init of_debug(char *str)
908 get_option(&str, &val);
910 of_resource_verbose = 1;
916 __setup("of_debug=", of_debug);
918 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
920 /* initialize common driver fields */
921 drv->driver.name = drv->name;
922 drv->driver.bus = bus;
924 /* register with core */
925 return driver_register(&drv->driver);
928 void of_unregister_driver(struct of_platform_driver *drv)
930 driver_unregister(&drv->driver);
934 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
936 struct of_device *ofdev;
938 ofdev = to_of_device(dev);
939 return sprintf(buf, "%s", ofdev->node->full_name);
942 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
945 * of_release_dev - free an of device structure when all users of it are finished.
946 * @dev: device that's been disconnected
948 * Will be called only by the device core when all users of this of device are
951 void of_release_dev(struct device *dev)
953 struct of_device *ofdev;
955 ofdev = to_of_device(dev);
960 int of_device_register(struct of_device *ofdev)
964 BUG_ON(ofdev->node == NULL);
966 rc = device_register(&ofdev->dev);
970 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
972 device_unregister(&ofdev->dev);
977 void of_device_unregister(struct of_device *ofdev)
979 device_remove_file(&ofdev->dev, &dev_attr_devspec);
980 device_unregister(&ofdev->dev);
983 struct of_device* of_platform_device_create(struct device_node *np,
985 struct device *parent,
986 struct bus_type *bus)
988 struct of_device *dev;
990 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
993 memset(dev, 0, sizeof(*dev));
995 dev->dev.parent = parent;
997 dev->dev.release = of_release_dev;
999 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
1001 if (of_device_register(dev) != 0) {
1009 EXPORT_SYMBOL(of_match_device);
1010 EXPORT_SYMBOL(of_register_driver);
1011 EXPORT_SYMBOL(of_unregister_driver);
1012 EXPORT_SYMBOL(of_device_register);
1013 EXPORT_SYMBOL(of_device_unregister);
1014 EXPORT_SYMBOL(of_dev_get);
1015 EXPORT_SYMBOL(of_dev_put);
1016 EXPORT_SYMBOL(of_platform_device_create);
1017 EXPORT_SYMBOL(of_release_dev);