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);
149 static int node_match(struct device *dev, void *data)
151 struct of_device *op = to_of_device(dev);
152 struct device_node *dp = data;
154 return (op->node == dp);
157 struct of_device *of_find_device_by_node(struct device_node *dp)
159 struct device *dev = bus_find_device(&of_bus_type, NULL,
163 return to_of_device(dev);
167 EXPORT_SYMBOL(of_find_device_by_node);
170 struct bus_type isa_bus_type = {
172 .match = of_platform_bus_match,
173 .probe = of_device_probe,
174 .remove = of_device_remove,
175 .suspend = of_device_suspend,
176 .resume = of_device_resume,
178 EXPORT_SYMBOL(isa_bus_type);
180 struct bus_type ebus_bus_type = {
182 .match = of_platform_bus_match,
183 .probe = of_device_probe,
184 .remove = of_device_remove,
185 .suspend = of_device_suspend,
186 .resume = of_device_resume,
188 EXPORT_SYMBOL(ebus_bus_type);
192 struct bus_type sbus_bus_type = {
194 .match = of_platform_bus_match,
195 .probe = of_device_probe,
196 .remove = of_device_remove,
197 .suspend = of_device_suspend,
198 .resume = of_device_resume,
200 EXPORT_SYMBOL(sbus_bus_type);
203 struct bus_type of_bus_type = {
205 .match = of_platform_bus_match,
206 .probe = of_device_probe,
207 .remove = of_device_remove,
208 .suspend = of_device_suspend,
209 .resume = of_device_resume,
211 EXPORT_SYMBOL(of_bus_type);
213 static inline u64 of_read_addr(u32 *cell, int size)
217 r = (r << 32) | *(cell++);
221 static void __init get_cells(struct device_node *dp,
222 int *addrc, int *sizec)
225 *addrc = of_n_addr_cells(dp);
227 *sizec = of_n_size_cells(dp);
230 /* Max address size we deal with */
231 #define OF_MAX_ADDR_CELLS 4
235 const char *addr_prop_name;
236 int (*match)(struct device_node *parent);
237 void (*count_cells)(struct device_node *child,
238 int *addrc, int *sizec);
239 u64 (*map)(u32 *addr, u32 *range, int na, int ns, int pna);
240 int (*translate)(u32 *addr, u64 offset, int na);
241 unsigned int (*get_flags)(u32 *addr);
245 * Default translator (generic bus)
248 static void of_bus_default_count_cells(struct device_node *dev,
249 int *addrc, int *sizec)
251 get_cells(dev, addrc, sizec);
254 static u64 of_bus_default_map(u32 *addr, u32 *range, int na, int ns, int pna)
258 cp = of_read_addr(range, na);
259 s = of_read_addr(range + na + pna, ns);
260 da = of_read_addr(addr, na);
262 if (da < cp || da >= (cp + s))
267 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
269 u64 a = of_read_addr(addr, na);
270 memset(addr, 0, na * 4);
273 addr[na - 2] = a >> 32;
274 addr[na - 1] = a & 0xffffffffu;
279 static unsigned int of_bus_default_get_flags(u32 *addr)
281 return IORESOURCE_MEM;
285 * PCI bus specific translator
288 static int of_bus_pci_match(struct device_node *np)
290 return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex");
293 static void of_bus_pci_count_cells(struct device_node *np,
294 int *addrc, int *sizec)
302 static u64 of_bus_pci_map(u32 *addr, u32 *range, int na, int ns, int pna)
306 /* Check address type match */
307 if ((addr[0] ^ range[0]) & 0x03000000)
310 /* Read address values, skipping high cell */
311 cp = of_read_addr(range + 1, na - 1);
312 s = of_read_addr(range + na + pna, ns);
313 da = of_read_addr(addr + 1, na - 1);
315 if (da < cp || da >= (cp + s))
320 static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
322 return of_bus_default_translate(addr + 1, offset, na - 1);
325 static unsigned int of_bus_pci_get_flags(u32 *addr)
327 unsigned int flags = 0;
330 switch((w >> 24) & 0x03) {
332 flags |= IORESOURCE_IO;
333 case 0x02: /* 32 bits */
334 case 0x03: /* 64 bits */
335 flags |= IORESOURCE_MEM;
338 flags |= IORESOURCE_PREFETCH;
343 * ISA bus specific translator
346 static int of_bus_isa_match(struct device_node *np)
348 return !strcmp(np->name, "isa");
351 static void of_bus_isa_count_cells(struct device_node *child,
352 int *addrc, int *sizec)
360 static u64 of_bus_isa_map(u32 *addr, u32 *range, int na, int ns, int pna)
364 /* Check address type match */
365 if ((addr[0] ^ range[0]) & 0x00000001)
368 /* Read address values, skipping high cell */
369 cp = of_read_addr(range + 1, na - 1);
370 s = of_read_addr(range + na + pna, ns);
371 da = of_read_addr(addr + 1, na - 1);
373 if (da < cp || da >= (cp + s))
378 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
380 return of_bus_default_translate(addr + 1, offset, na - 1);
383 static unsigned int of_bus_isa_get_flags(u32 *addr)
385 unsigned int flags = 0;
389 flags |= IORESOURCE_IO;
391 flags |= IORESOURCE_MEM;
396 * SBUS bus specific translator
399 static int of_bus_sbus_match(struct device_node *np)
401 return !strcmp(np->name, "sbus") ||
402 !strcmp(np->name, "sbi");
405 static void of_bus_sbus_count_cells(struct device_node *child,
406 int *addrc, int *sizec)
414 static u64 of_bus_sbus_map(u32 *addr, u32 *range, int na, int ns, int pna)
416 return of_bus_default_map(addr, range, na, ns, pna);
419 static int of_bus_sbus_translate(u32 *addr, u64 offset, int na)
421 return of_bus_default_translate(addr, offset, na);
424 static unsigned int of_bus_sbus_get_flags(u32 *addr)
426 return IORESOURCE_MEM;
431 * Array of bus specific translators
434 static struct of_bus of_busses[] = {
438 .addr_prop_name = "assigned-addresses",
439 .match = of_bus_pci_match,
440 .count_cells = of_bus_pci_count_cells,
441 .map = of_bus_pci_map,
442 .translate = of_bus_pci_translate,
443 .get_flags = of_bus_pci_get_flags,
448 .addr_prop_name = "reg",
449 .match = of_bus_isa_match,
450 .count_cells = of_bus_isa_count_cells,
451 .map = of_bus_isa_map,
452 .translate = of_bus_isa_translate,
453 .get_flags = of_bus_isa_get_flags,
458 .addr_prop_name = "reg",
459 .match = of_bus_sbus_match,
460 .count_cells = of_bus_sbus_count_cells,
461 .map = of_bus_sbus_map,
462 .translate = of_bus_sbus_translate,
463 .get_flags = of_bus_sbus_get_flags,
468 .addr_prop_name = "reg",
470 .count_cells = of_bus_default_count_cells,
471 .map = of_bus_default_map,
472 .translate = of_bus_default_translate,
473 .get_flags = of_bus_default_get_flags,
477 static struct of_bus *of_match_bus(struct device_node *np)
481 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
482 if (!of_busses[i].match || of_busses[i].match(np))
483 return &of_busses[i];
488 static int __init build_one_resource(struct device_node *parent,
492 int na, int ns, int pna)
497 u64 offset = OF_BAD_ADDR;
499 ranges = of_get_property(parent, "ranges", &rlen);
500 if (ranges == NULL || rlen == 0) {
501 offset = of_read_addr(addr, na);
502 memset(addr, 0, pna * 4);
506 /* Now walk through the ranges */
508 rone = na + pna + ns;
509 for (; rlen >= rone; rlen -= rone, ranges += rone) {
510 offset = bus->map(addr, ranges, na, ns, pna);
511 if (offset != OF_BAD_ADDR)
514 if (offset == OF_BAD_ADDR)
517 memcpy(addr, ranges + na, 4 * pna);
520 /* Translate it into parent bus space */
521 return pbus->translate(addr, offset, pna);
524 static void __init build_device_resources(struct of_device *op,
525 struct device *parent)
527 struct of_device *p_op;
536 p_op = to_of_device(parent);
537 bus = of_match_bus(p_op->node);
538 bus->count_cells(op->node, &na, &ns);
540 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
541 if (!preg || num_reg == 0)
544 /* Convert to num-cells. */
547 /* Conver to num-entries. */
550 for (index = 0; index < num_reg; index++) {
551 struct resource *r = &op->resource[index];
552 u32 addr[OF_MAX_ADDR_CELLS];
553 u32 *reg = (preg + (index * ((na + ns) * 4)));
554 struct device_node *dp = op->node;
555 struct device_node *pp = p_op->node;
557 u64 size, result = OF_BAD_ADDR;
562 size = of_read_addr(reg + na, ns);
563 flags = bus->get_flags(reg);
565 memcpy(addr, reg, na * 4);
567 /* If the immediate parent has no ranges property to apply,
568 * just use a 1<->1 mapping. Unless it is the 'dma' child
569 * of an isa bus, which must be passed up towards the root.
571 * Also, don't try to translate PMU bus device registers.
573 if ((of_find_property(pp, "ranges", NULL) == NULL &&
574 strcmp(pp->name, "dma") != 0) ||
575 !strcmp(pp->name, "pmu")) {
576 result = of_read_addr(addr, na);
587 result = of_read_addr(addr, dna);
591 pbus = of_match_bus(pp);
592 pbus->count_cells(dp, &pna, &pns);
594 if (build_one_resource(dp, bus, pbus, addr, dna, dns, pna))
603 memset(r, 0, sizeof(*r));
604 if (result != OF_BAD_ADDR) {
606 r->end = result + size - 1;
612 r->name = op->node->name;
616 static struct device_node * __init
617 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
618 u32 *imap, int imlen, u32 *imask,
621 struct device_node *cp;
622 unsigned int irq = *irq_p;
628 bus = of_match_bus(pp);
629 bus->count_cells(dp, &na, NULL);
631 reg = of_get_property(dp, "reg", &num_reg);
632 if (!reg || !num_reg)
635 imlen /= ((na + 3) * 4);
637 for (i = 0; i < imlen; i++) {
640 for (j = 0; j < na; j++) {
641 if ((reg[j] & imask[j]) != imap[j])
644 if (imap[na] == irq) {
645 handle = imap[na + 1];
657 cp = of_find_node_by_phandle(handle);
662 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
663 struct device_node *pp,
666 struct linux_prom_pci_registers *regs;
667 unsigned int devfn, slot, ret;
669 if (irq < 1 || irq > 4)
672 regs = of_get_property(dp, "reg", NULL);
676 devfn = (regs->phys_hi >> 8) & 0xff;
677 slot = (devfn >> 3) & 0x1f;
679 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
684 static unsigned int __init build_one_device_irq(struct of_device *op,
685 struct device *parent,
688 struct device_node *dp = op->node;
689 struct device_node *pp, *ip;
690 unsigned int orig_irq = irq;
692 if (irq == 0xffffffff)
696 irq = dp->irq_trans->irq_build(dp, irq,
697 dp->irq_trans->data);
699 printk("%s: direct translate %x --> %x\n",
700 dp->full_name, orig_irq, irq);
705 /* Something more complicated. Walk up to the root, applying
706 * interrupt-map or bus specific translations, until we hit
709 * If we hit a bus type or situation we cannot handle, we
710 * stop and assume that the original IRQ number was in a
711 * format which has special meaning to it's immediate parent.
719 imap = of_get_property(pp, "interrupt-map", &imlen);
720 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
722 struct device_node *iret;
723 int this_orig_irq = irq;
725 iret = apply_interrupt_map(dp, pp,
729 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
731 pp->full_name, this_orig_irq,
732 (iret ? iret->full_name : "NULL"), irq);
737 if (iret->irq_trans) {
742 if (!strcmp(pp->type, "pci") ||
743 !strcmp(pp->type, "pciex")) {
744 unsigned int this_orig_irq = irq;
746 irq = pci_irq_swizzle(dp, pp, irq);
748 printk("%s: PCI swizzle [%s] %x --> %x\n",
750 pp->full_name, this_orig_irq, irq);
765 irq = ip->irq_trans->irq_build(op->node, irq,
766 ip->irq_trans->data);
768 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
769 op->node->full_name, ip->full_name, orig_irq, irq);
775 static struct of_device * __init scan_one_device(struct device_node *dp,
776 struct device *parent)
778 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
787 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
789 op->portid = of_getintprop_default(dp, "upa-portid", -1);
790 if (op->portid == -1)
791 op->portid = of_getintprop_default(dp, "portid", -1);
793 irq = of_get_property(dp, "interrupts", &len);
795 memcpy(op->irqs, irq, len);
796 op->num_irqs = len / 4;
801 build_device_resources(op, parent);
802 for (i = 0; i < op->num_irqs; i++)
803 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
805 op->dev.parent = parent;
806 op->dev.bus = &of_bus_type;
808 strcpy(op->dev.bus_id, "root");
810 strcpy(op->dev.bus_id, dp->path_component_name);
812 if (of_device_register(op)) {
813 printk("%s: Could not register of device.\n",
822 static void __init scan_tree(struct device_node *dp, struct device *parent)
825 struct of_device *op = scan_one_device(dp, parent);
828 scan_tree(dp->child, &op->dev);
834 static void __init scan_of_devices(void)
836 struct device_node *root = of_find_node_by_path("/");
837 struct of_device *parent;
839 parent = scan_one_device(root, NULL);
843 scan_tree(root->child, &parent->dev);
846 static int __init of_bus_driver_init(void)
850 err = bus_register(&of_bus_type);
853 err = bus_register(&isa_bus_type);
855 err = bus_register(&ebus_bus_type);
859 err = bus_register(&sbus_bus_type);
868 postcore_initcall(of_bus_driver_init);
870 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
872 /* initialize common driver fields */
873 drv->driver.name = drv->name;
874 drv->driver.bus = bus;
876 /* register with core */
877 return driver_register(&drv->driver);
880 void of_unregister_driver(struct of_platform_driver *drv)
882 driver_unregister(&drv->driver);
886 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
888 struct of_device *ofdev;
890 ofdev = to_of_device(dev);
891 return sprintf(buf, "%s", ofdev->node->full_name);
894 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
897 * of_release_dev - free an of device structure when all users of it are finished.
898 * @dev: device that's been disconnected
900 * Will be called only by the device core when all users of this of device are
903 void of_release_dev(struct device *dev)
905 struct of_device *ofdev;
907 ofdev = to_of_device(dev);
912 int of_device_register(struct of_device *ofdev)
916 BUG_ON(ofdev->node == NULL);
918 rc = device_register(&ofdev->dev);
922 device_create_file(&ofdev->dev, &dev_attr_devspec);
927 void of_device_unregister(struct of_device *ofdev)
929 device_remove_file(&ofdev->dev, &dev_attr_devspec);
930 device_unregister(&ofdev->dev);
933 struct of_device* of_platform_device_create(struct device_node *np,
935 struct device *parent,
936 struct bus_type *bus)
938 struct of_device *dev;
940 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
943 memset(dev, 0, sizeof(*dev));
945 dev->dev.parent = parent;
947 dev->dev.release = of_release_dev;
949 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
951 if (of_device_register(dev) != 0) {
959 EXPORT_SYMBOL(of_match_device);
960 EXPORT_SYMBOL(of_register_driver);
961 EXPORT_SYMBOL(of_unregister_driver);
962 EXPORT_SYMBOL(of_device_register);
963 EXPORT_SYMBOL(of_device_unregister);
964 EXPORT_SYMBOL(of_dev_get);
965 EXPORT_SYMBOL(of_dev_put);
966 EXPORT_SYMBOL(of_platform_device_create);
967 EXPORT_SYMBOL(of_release_dev);