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(struct resource *res, void __iomem *base, unsigned long size)
149 if (res->flags & IORESOURCE_MEM)
150 release_mem_region((unsigned long) base, size);
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,
170 return to_of_device(dev);
174 EXPORT_SYMBOL(of_find_device_by_node);
177 struct bus_type isa_bus_type = {
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 = {
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);
199 struct bus_type sbus_bus_type = {
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);
210 struct bus_type of_bus_type = {
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)
224 r = (r << 32) | *(cell++);
228 static void __init get_cells(struct device_node *dp,
229 int *addrc, int *sizec)
232 *addrc = of_n_addr_cells(dp);
234 *sizec = of_n_size_cells(dp);
237 /* Max address size we deal with */
238 #define OF_MAX_ADDR_CELLS 4
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)(const 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);
273 b += of_read_addr(size, ns);
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];
287 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
291 if (of_out_of_range(addr, range, range + na + pna, na, ns))
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] +=
303 memcpy(addr, result, pna * 4);
308 static unsigned int of_bus_default_get_flags(const 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 char *model = of_get_property(np, "model", NULL);
322 if (model && !strcmp(model, "SUNW,simba"))
325 /* Do not do PCI specific frobbing if the
326 * PCI bridge lacks a ranges property. We
327 * want to pass it through up to the next
328 * parent as-is, not with the PCI translate
329 * method which chops off the top address cell.
331 if (!of_find_property(np, "ranges", NULL))
340 static int of_bus_simba_match(struct device_node *np)
342 char *model = of_get_property(np, "model", NULL);
344 if (model && !strcmp(model, "SUNW,simba"))
349 static int of_bus_simba_map(u32 *addr, const u32 *range,
350 int na, int ns, int pna)
355 static void of_bus_pci_count_cells(struct device_node *np,
356 int *addrc, int *sizec)
364 static int of_bus_pci_map(u32 *addr, const u32 *range,
365 int na, int ns, int pna)
367 u32 result[OF_MAX_ADDR_CELLS];
370 /* Check address type match */
371 if ((addr[0] ^ range[0]) & 0x03000000)
374 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
378 /* Start with the parent range base. */
379 memcpy(result, range + na, pna * 4);
381 /* Add in the child address offset, skipping high cell. */
382 for (i = 0; i < na - 1; i++)
383 result[pna - 1 - i] +=
387 memcpy(addr, result, pna * 4);
392 static unsigned int of_bus_pci_get_flags(const u32 *addr)
394 unsigned int flags = 0;
397 switch((w >> 24) & 0x03) {
399 flags |= IORESOURCE_IO;
400 case 0x02: /* 32 bits */
401 case 0x03: /* 64 bits */
402 flags |= IORESOURCE_MEM;
405 flags |= IORESOURCE_PREFETCH;
410 * SBUS bus specific translator
413 static int of_bus_sbus_match(struct device_node *np)
415 return !strcmp(np->name, "sbus") ||
416 !strcmp(np->name, "sbi");
419 static void of_bus_sbus_count_cells(struct device_node *child,
420 int *addrc, int *sizec)
429 * FHC/Central bus specific translator.
431 * This is just needed to hard-code the address and size cell
432 * counts. 'fhc' and 'central' nodes lack the #address-cells and
433 * #size-cells properties, and if you walk to the root on such
434 * Enterprise boxes all you'll get is a #size-cells of 2 which is
435 * not what we want to use.
437 static int of_bus_fhc_match(struct device_node *np)
439 return !strcmp(np->name, "fhc") ||
440 !strcmp(np->name, "central");
443 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
446 * Array of bus specific translators
449 static struct of_bus of_busses[] = {
453 .addr_prop_name = "assigned-addresses",
454 .match = of_bus_pci_match,
455 .count_cells = of_bus_pci_count_cells,
456 .map = of_bus_pci_map,
457 .get_flags = of_bus_pci_get_flags,
462 .addr_prop_name = "assigned-addresses",
463 .match = of_bus_simba_match,
464 .count_cells = of_bus_pci_count_cells,
465 .map = of_bus_simba_map,
466 .get_flags = of_bus_pci_get_flags,
471 .addr_prop_name = "reg",
472 .match = of_bus_sbus_match,
473 .count_cells = of_bus_sbus_count_cells,
474 .map = of_bus_default_map,
475 .get_flags = of_bus_default_get_flags,
480 .addr_prop_name = "reg",
481 .match = of_bus_fhc_match,
482 .count_cells = of_bus_fhc_count_cells,
483 .map = of_bus_default_map,
484 .get_flags = of_bus_default_get_flags,
489 .addr_prop_name = "reg",
491 .count_cells = of_bus_default_count_cells,
492 .map = of_bus_default_map,
493 .get_flags = of_bus_default_get_flags,
497 static struct of_bus *of_match_bus(struct device_node *np)
501 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
502 if (!of_busses[i].match || of_busses[i].match(np))
503 return &of_busses[i];
508 static int __init build_one_resource(struct device_node *parent,
512 int na, int ns, int pna)
518 ranges = of_get_property(parent, "ranges", &rlen);
519 if (ranges == NULL || rlen == 0) {
520 u32 result[OF_MAX_ADDR_CELLS];
523 memset(result, 0, pna * 4);
524 for (i = 0; i < na; i++)
525 result[pna - 1 - i] =
528 memcpy(addr, result, pna * 4);
532 /* Now walk through the ranges */
534 rone = na + pna + ns;
535 for (; rlen >= rone; rlen -= rone, ranges += rone) {
536 if (!bus->map(addr, ranges, na, ns, pna))
543 static int __init use_1to1_mapping(struct device_node *pp)
547 /* If this is on the PMU bus, don't try to translate it even
548 * if a ranges property exists.
550 if (!strcmp(pp->name, "pmu"))
553 /* If we have a ranges property in the parent, use it. */
554 if (of_find_property(pp, "ranges", NULL) != NULL)
557 /* If the parent is the dma node of an ISA bus, pass
558 * the translation up to the root.
560 if (!strcmp(pp->name, "dma"))
563 /* Similarly for Simba PCI bridges. */
564 model = of_get_property(pp, "model", NULL);
565 if (model && !strcmp(model, "SUNW,simba"))
571 static int of_resource_verbose;
573 static void __init build_device_resources(struct of_device *op,
574 struct device *parent)
576 struct of_device *p_op;
585 p_op = to_of_device(parent);
586 bus = of_match_bus(p_op->node);
587 bus->count_cells(op->node, &na, &ns);
589 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
590 if (!preg || num_reg == 0)
593 /* Convert to num-cells. */
596 /* Convert to num-entries. */
599 /* Prevent overruning the op->resources[] array. */
600 if (num_reg > PROMREG_MAX) {
601 printk(KERN_WARNING "%s: Too many regs (%d), "
603 op->node->full_name, num_reg, PROMREG_MAX);
604 num_reg = PROMREG_MAX;
607 for (index = 0; index < num_reg; index++) {
608 struct resource *r = &op->resource[index];
609 u32 addr[OF_MAX_ADDR_CELLS];
610 const u32 *reg = (preg + (index * ((na + ns) * 4)));
611 struct device_node *dp = op->node;
612 struct device_node *pp = p_op->node;
613 struct of_bus *pbus, *dbus;
614 u64 size, result = OF_BAD_ADDR;
619 size = of_read_addr(reg + na, ns);
620 flags = bus->get_flags(reg);
622 memcpy(addr, reg, na * 4);
624 if (use_1to1_mapping(pp)) {
625 result = of_read_addr(addr, na);
637 result = of_read_addr(addr, dna);
641 pbus = of_match_bus(pp);
642 pbus->count_cells(dp, &pna, &pns);
644 if (build_one_resource(dp, dbus, pbus, addr,
654 memset(r, 0, sizeof(*r));
656 if (of_resource_verbose)
657 printk("%s reg[%d] -> %lx\n",
658 op->node->full_name, index,
661 if (result != OF_BAD_ADDR) {
662 if (tlb_type == hypervisor)
663 result &= 0x0fffffffffffffffUL;
666 r->end = result + size - 1;
669 r->name = op->node->name;
673 static struct device_node * __init
674 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
675 const u32 *imap, int imlen, const u32 *imask,
678 struct device_node *cp;
679 unsigned int irq = *irq_p;
685 bus = of_match_bus(pp);
686 bus->count_cells(dp, &na, NULL);
688 reg = of_get_property(dp, "reg", &num_reg);
689 if (!reg || !num_reg)
692 imlen /= ((na + 3) * 4);
694 for (i = 0; i < imlen; i++) {
697 for (j = 0; j < na; j++) {
698 if ((reg[j] & imask[j]) != imap[j])
701 if (imap[na] == irq) {
702 handle = imap[na + 1];
711 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
712 * properties that do not include the on-board device
713 * interrupts. Instead, the device's 'interrupts' property
714 * is already a fully specified INO value.
716 * Handle this by deciding that, if we didn't get a
717 * match in the parent's 'interrupt-map', and the
718 * parent is an IRQ translater, then use the parent as
719 * our IRQ controller.
728 cp = of_find_node_by_phandle(handle);
733 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
734 struct device_node *pp,
737 const struct linux_prom_pci_registers *regs;
738 unsigned int bus, devfn, slot, ret;
740 if (irq < 1 || irq > 4)
743 regs = of_get_property(dp, "reg", NULL);
747 bus = (regs->phys_hi >> 16) & 0xff;
748 devfn = (regs->phys_hi >> 8) & 0xff;
749 slot = (devfn >> 3) & 0x1f;
752 /* Derived from Table 8-3, U2P User's Manual. This branch
753 * is handling a PCI controller that lacks a proper set of
754 * interrupt-map and interrupt-map-mask properties. The
755 * Ultra-E450 is one example.
757 * The bit layout is BSSLL, where:
758 * B: 0 on bus A, 1 on bus B
759 * D: 2-bit slot number, derived from PCI device number as
760 * (dev - 1) for bus A, or (dev - 2) for bus B
761 * L: 2-bit line number
763 * Actually, more "portable" way to calculate the funky
764 * slot number is to subtract pbm->pci_first_slot from the
765 * device number, and that's exactly what the pre-OF
766 * sparc64 code did, but we're building this stuff generically
767 * using the OBP tree, not in the PCI controller layer.
772 slot = (slot - 1) << 2;
776 slot = (slot - 2) << 2;
780 ret = (bus | slot | irq);
782 /* Going through a PCI-PCI bridge that lacks a set of
783 * interrupt-map and interrupt-map-mask properties.
785 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
791 static int of_irq_verbose;
793 static unsigned int __init build_one_device_irq(struct of_device *op,
794 struct device *parent,
797 struct device_node *dp = op->node;
798 struct device_node *pp, *ip;
799 unsigned int orig_irq = irq;
801 if (irq == 0xffffffff)
805 irq = dp->irq_trans->irq_build(dp, irq,
806 dp->irq_trans->data);
809 printk("%s: direct translate %x --> %x\n",
810 dp->full_name, orig_irq, irq);
815 /* Something more complicated. Walk up to the root, applying
816 * interrupt-map or bus specific translations, until we hit
819 * If we hit a bus type or situation we cannot handle, we
820 * stop and assume that the original IRQ number was in a
821 * format which has special meaning to it's immediate parent.
826 const void *imap, *imsk;
829 imap = of_get_property(pp, "interrupt-map", &imlen);
830 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
832 struct device_node *iret;
833 int this_orig_irq = irq;
835 iret = apply_interrupt_map(dp, pp,
840 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
842 pp->full_name, this_orig_irq,
843 (iret ? iret->full_name : "NULL"), irq);
848 if (iret->irq_trans) {
853 if (!strcmp(pp->type, "pci") ||
854 !strcmp(pp->type, "pciex")) {
855 unsigned int this_orig_irq = irq;
857 irq = pci_irq_swizzle(dp, pp, irq);
859 printk("%s: PCI swizzle [%s] "
862 pp->full_name, this_orig_irq,
878 irq = ip->irq_trans->irq_build(op->node, irq,
879 ip->irq_trans->data);
881 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
882 op->node->full_name, ip->full_name, orig_irq, irq);
887 static struct of_device * __init scan_one_device(struct device_node *dp,
888 struct device *parent)
890 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
891 const unsigned int *irq;
899 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
901 op->portid = of_getintprop_default(dp, "upa-portid", -1);
902 if (op->portid == -1)
903 op->portid = of_getintprop_default(dp, "portid", -1);
905 irq = of_get_property(dp, "interrupts", &len);
907 memcpy(op->irqs, irq, len);
908 op->num_irqs = len / 4;
913 /* Prevent overruning the op->irqs[] array. */
914 if (op->num_irqs > PROMINTR_MAX) {
915 printk(KERN_WARNING "%s: Too many irqs (%d), "
917 dp->full_name, op->num_irqs, PROMINTR_MAX);
918 op->num_irqs = PROMINTR_MAX;
921 build_device_resources(op, parent);
922 for (i = 0; i < op->num_irqs; i++)
923 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
925 op->dev.parent = parent;
926 op->dev.bus = &of_bus_type;
928 strcpy(op->dev.bus_id, "root");
930 sprintf(op->dev.bus_id, "%08x", dp->node);
932 if (of_device_register(op)) {
933 printk("%s: Could not register of device.\n",
942 static void __init scan_tree(struct device_node *dp, struct device *parent)
945 struct of_device *op = scan_one_device(dp, parent);
948 scan_tree(dp->child, &op->dev);
954 static void __init scan_of_devices(void)
956 struct device_node *root = of_find_node_by_path("/");
957 struct of_device *parent;
959 parent = scan_one_device(root, NULL);
963 scan_tree(root->child, &parent->dev);
966 static int __init of_bus_driver_init(void)
970 err = bus_register(&of_bus_type);
973 err = bus_register(&isa_bus_type);
975 err = bus_register(&ebus_bus_type);
979 err = bus_register(&sbus_bus_type);
988 postcore_initcall(of_bus_driver_init);
990 static int __init of_debug(char *str)
994 get_option(&str, &val);
996 of_resource_verbose = 1;
1002 __setup("of_debug=", of_debug);
1004 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
1006 /* initialize common driver fields */
1007 drv->driver.name = drv->name;
1008 drv->driver.bus = bus;
1010 /* register with core */
1011 return driver_register(&drv->driver);
1014 void of_unregister_driver(struct of_platform_driver *drv)
1016 driver_unregister(&drv->driver);
1020 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
1022 struct of_device *ofdev;
1024 ofdev = to_of_device(dev);
1025 return sprintf(buf, "%s", ofdev->node->full_name);
1028 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
1031 * of_release_dev - free an of device structure when all users of it are finished.
1032 * @dev: device that's been disconnected
1034 * Will be called only by the device core when all users of this of device are
1037 void of_release_dev(struct device *dev)
1039 struct of_device *ofdev;
1041 ofdev = to_of_device(dev);
1046 int of_device_register(struct of_device *ofdev)
1050 BUG_ON(ofdev->node == NULL);
1052 rc = device_register(&ofdev->dev);
1056 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
1058 device_unregister(&ofdev->dev);
1063 void of_device_unregister(struct of_device *ofdev)
1065 device_remove_file(&ofdev->dev, &dev_attr_devspec);
1066 device_unregister(&ofdev->dev);
1069 struct of_device* of_platform_device_create(struct device_node *np,
1071 struct device *parent,
1072 struct bus_type *bus)
1074 struct of_device *dev;
1076 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1080 dev->dev.parent = parent;
1082 dev->dev.release = of_release_dev;
1084 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
1086 if (of_device_register(dev) != 0) {
1094 EXPORT_SYMBOL(of_match_device);
1095 EXPORT_SYMBOL(of_register_driver);
1096 EXPORT_SYMBOL(of_unregister_driver);
1097 EXPORT_SYMBOL(of_device_register);
1098 EXPORT_SYMBOL(of_device_unregister);
1099 EXPORT_SYMBOL(of_dev_get);
1100 EXPORT_SYMBOL(of_dev_put);
1101 EXPORT_SYMBOL(of_platform_device_create);
1102 EXPORT_SYMBOL(of_release_dev);