1 #include <linux/string.h>
2 #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>
12 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
14 struct of_device * of_dev = to_of_device(dev);
15 struct of_platform_driver * of_drv = to_of_platform_driver(drv);
16 const struct of_device_id * matches = of_drv->match_table;
21 return of_match_device(matches, of_dev) != NULL;
24 static int of_device_probe(struct device *dev)
27 struct of_platform_driver *drv;
28 struct of_device *of_dev;
29 const struct of_device_id *match;
31 drv = to_of_platform_driver(dev->driver);
32 of_dev = to_of_device(dev);
39 match = of_match_device(drv->match_table, of_dev);
41 error = drv->probe(of_dev, match);
48 static int of_device_remove(struct device *dev)
50 struct of_device * of_dev = to_of_device(dev);
51 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
53 if (dev->driver && drv->remove)
58 static int of_device_suspend(struct device *dev, pm_message_t state)
60 struct of_device * of_dev = to_of_device(dev);
61 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
64 if (dev->driver && drv->suspend)
65 error = drv->suspend(of_dev, state);
69 static int of_device_resume(struct device * dev)
71 struct of_device * of_dev = to_of_device(dev);
72 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
75 if (dev->driver && drv->resume)
76 error = drv->resume(of_dev);
80 static int node_match(struct device *dev, void *data)
82 struct of_device *op = to_of_device(dev);
83 struct device_node *dp = data;
85 return (op->node == dp);
88 struct of_device *of_find_device_by_node(struct device_node *dp)
90 struct device *dev = bus_find_device(&of_bus_type, NULL,
94 return to_of_device(dev);
98 EXPORT_SYMBOL(of_find_device_by_node);
101 struct bus_type ebus_bus_type = {
103 .match = of_platform_bus_match,
104 .probe = of_device_probe,
105 .remove = of_device_remove,
106 .suspend = of_device_suspend,
107 .resume = of_device_resume,
109 EXPORT_SYMBOL(ebus_bus_type);
113 struct bus_type sbus_bus_type = {
115 .match = of_platform_bus_match,
116 .probe = of_device_probe,
117 .remove = of_device_remove,
118 .suspend = of_device_suspend,
119 .resume = of_device_resume,
121 EXPORT_SYMBOL(sbus_bus_type);
124 struct bus_type of_bus_type = {
126 .match = of_platform_bus_match,
127 .probe = of_device_probe,
128 .remove = of_device_remove,
129 .suspend = of_device_suspend,
130 .resume = of_device_resume,
132 EXPORT_SYMBOL(of_bus_type);
134 static inline u64 of_read_addr(const u32 *cell, int size)
138 r = (r << 32) | *(cell++);
142 static void __init get_cells(struct device_node *dp,
143 int *addrc, int *sizec)
146 *addrc = of_n_addr_cells(dp);
148 *sizec = of_n_size_cells(dp);
151 /* Max address size we deal with */
152 #define OF_MAX_ADDR_CELLS 4
156 const char *addr_prop_name;
157 int (*match)(struct device_node *parent);
158 void (*count_cells)(struct device_node *child,
159 int *addrc, int *sizec);
160 int (*map)(u32 *addr, const u32 *range,
161 int na, int ns, int pna);
162 unsigned int (*get_flags)(const u32 *addr);
166 * Default translator (generic bus)
169 static void of_bus_default_count_cells(struct device_node *dev,
170 int *addrc, int *sizec)
172 get_cells(dev, addrc, sizec);
175 /* Make sure the least significant 64-bits are in-range. Even
176 * for 3 or 4 cell values it is a good enough approximation.
178 static int of_out_of_range(const u32 *addr, const u32 *base,
179 const u32 *size, int na, int ns)
181 u64 a = of_read_addr(addr, na);
182 u64 b = of_read_addr(base, na);
187 b += of_read_addr(size, ns);
194 static int of_bus_default_map(u32 *addr, const u32 *range,
195 int na, int ns, int pna)
197 u32 result[OF_MAX_ADDR_CELLS];
201 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
205 if (of_out_of_range(addr, range, range + na + pna, na, ns))
208 /* Start with the parent range base. */
209 memcpy(result, range + na, pna * 4);
211 /* Add in the child address offset. */
212 for (i = 0; i < na; i++)
213 result[pna - 1 - i] +=
217 memcpy(addr, result, pna * 4);
222 static unsigned int of_bus_default_get_flags(const u32 *addr)
224 return IORESOURCE_MEM;
228 * PCI bus specific translator
231 static int of_bus_pci_match(struct device_node *np)
233 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
234 /* Do not do PCI specific frobbing if the
235 * PCI bridge lacks a ranges property. We
236 * want to pass it through up to the next
237 * parent as-is, not with the PCI translate
238 * method which chops off the top address cell.
240 if (!of_find_property(np, "ranges", NULL))
249 static void of_bus_pci_count_cells(struct device_node *np,
250 int *addrc, int *sizec)
258 static int of_bus_pci_map(u32 *addr, const u32 *range,
259 int na, int ns, int pna)
261 u32 result[OF_MAX_ADDR_CELLS];
264 /* Check address type match */
265 if ((addr[0] ^ range[0]) & 0x03000000)
268 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
272 /* Start with the parent range base. */
273 memcpy(result, range + na, pna * 4);
275 /* Add in the child address offset, skipping high cell. */
276 for (i = 0; i < na - 1; i++)
277 result[pna - 1 - i] +=
281 memcpy(addr, result, pna * 4);
286 static unsigned int of_bus_pci_get_flags(const u32 *addr)
288 unsigned int flags = 0;
291 switch((w >> 24) & 0x03) {
293 flags |= IORESOURCE_IO;
294 case 0x02: /* 32 bits */
295 case 0x03: /* 64 bits */
296 flags |= IORESOURCE_MEM;
299 flags |= IORESOURCE_PREFETCH;
304 * SBUS bus specific translator
307 static int of_bus_sbus_match(struct device_node *np)
309 return !strcmp(np->name, "sbus") ||
310 !strcmp(np->name, "sbi");
313 static void of_bus_sbus_count_cells(struct device_node *child,
314 int *addrc, int *sizec)
322 static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
324 return of_bus_default_map(addr, range, na, ns, pna);
327 static unsigned int of_bus_sbus_get_flags(const u32 *addr)
329 return IORESOURCE_MEM;
334 * Array of bus specific translators
337 static struct of_bus of_busses[] = {
341 .addr_prop_name = "assigned-addresses",
342 .match = of_bus_pci_match,
343 .count_cells = of_bus_pci_count_cells,
344 .map = of_bus_pci_map,
345 .get_flags = of_bus_pci_get_flags,
350 .addr_prop_name = "reg",
351 .match = of_bus_sbus_match,
352 .count_cells = of_bus_sbus_count_cells,
353 .map = of_bus_sbus_map,
354 .get_flags = of_bus_sbus_get_flags,
359 .addr_prop_name = "reg",
361 .count_cells = of_bus_default_count_cells,
362 .map = of_bus_default_map,
363 .get_flags = of_bus_default_get_flags,
367 static struct of_bus *of_match_bus(struct device_node *np)
371 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
372 if (!of_busses[i].match || of_busses[i].match(np))
373 return &of_busses[i];
378 static int __init build_one_resource(struct device_node *parent,
382 int na, int ns, int pna)
388 ranges = of_get_property(parent, "ranges", &rlen);
389 if (ranges == NULL || rlen == 0) {
390 u32 result[OF_MAX_ADDR_CELLS];
393 memset(result, 0, pna * 4);
394 for (i = 0; i < na; i++)
395 result[pna - 1 - i] =
398 memcpy(addr, result, pna * 4);
402 /* Now walk through the ranges */
404 rone = na + pna + ns;
405 for (; rlen >= rone; rlen -= rone, ranges += rone) {
406 if (!bus->map(addr, ranges, na, ns, pna))
413 static int of_resource_verbose;
415 static void __init build_device_resources(struct of_device *op,
416 struct device *parent)
418 struct of_device *p_op;
427 p_op = to_of_device(parent);
428 bus = of_match_bus(p_op->node);
429 bus->count_cells(op->node, &na, &ns);
431 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
432 if (!preg || num_reg == 0)
435 /* Convert to num-cells. */
438 /* Conver to num-entries. */
441 for (index = 0; index < num_reg; index++) {
442 struct resource *r = &op->resource[index];
443 u32 addr[OF_MAX_ADDR_CELLS];
444 const u32 *reg = (preg + (index * ((na + ns) * 4)));
445 struct device_node *dp = op->node;
446 struct device_node *pp = p_op->node;
447 struct of_bus *pbus, *dbus;
448 u64 size, result = OF_BAD_ADDR;
453 size = of_read_addr(reg + na, ns);
454 flags = bus->get_flags(reg);
456 memcpy(addr, reg, na * 4);
458 /* If the immediate parent has no ranges property to apply,
459 * just use a 1<->1 mapping.
461 if (of_find_property(pp, "ranges", NULL) == NULL) {
462 result = of_read_addr(addr, na);
474 result = of_read_addr(addr, dna);
478 pbus = of_match_bus(pp);
479 pbus->count_cells(dp, &pna, &pns);
481 if (build_one_resource(dp, dbus, pbus, addr,
491 memset(r, 0, sizeof(*r));
493 if (of_resource_verbose)
494 printk("%s reg[%d] -> %llx\n",
495 op->node->full_name, index,
498 if (result != OF_BAD_ADDR) {
499 r->start = result & 0xffffffff;
500 r->end = result + size - 1;
501 r->flags = flags | ((result >> 32ULL) & 0xffUL);
503 r->name = op->node->name;
507 static struct of_device * __init scan_one_device(struct device_node *dp,
508 struct device *parent)
510 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
511 const struct linux_prom_irqs *intr;
519 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
521 op->portid = of_getintprop_default(dp, "upa-portid", -1);
522 if (op->portid == -1)
523 op->portid = of_getintprop_default(dp, "portid", -1);
525 intr = of_get_property(dp, "intr", &len);
527 op->num_irqs = len / sizeof(struct linux_prom_irqs);
528 for (i = 0; i < op->num_irqs; i++)
529 op->irqs[i] = intr[i].pri;
531 const unsigned int *irq =
532 of_get_property(dp, "interrupts", &len);
535 op->num_irqs = len / sizeof(unsigned int);
536 for (i = 0; i < op->num_irqs; i++)
537 op->irqs[i] = irq[i];
542 if (sparc_cpu_model == sun4d) {
543 static int pil_to_sbus[] = {
544 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
546 struct device_node *io_unit, *sbi = dp->parent;
547 const struct linux_prom_registers *regs;
551 if (!strcmp(sbi->name, "sbi"))
557 goto build_resources;
559 regs = of_get_property(dp, "reg", NULL);
561 goto build_resources;
563 slot = regs->which_io;
565 /* If SBI's parent is not io-unit or the io-unit lacks
566 * a "board#" property, something is very wrong.
568 if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
569 printk("%s: Error, parent is not io-unit.\n",
571 goto build_resources;
573 io_unit = sbi->parent;
574 board = of_getintprop_default(io_unit, "board#", -1);
576 printk("%s: Error, lacks board# property.\n",
578 goto build_resources;
581 for (i = 0; i < op->num_irqs; i++) {
582 int this_irq = op->irqs[i];
583 int sbusl = pil_to_sbus[this_irq];
586 this_irq = (((board + 1) << 5) +
590 op->irqs[i] = this_irq;
595 build_device_resources(op, parent);
597 op->dev.parent = parent;
598 op->dev.bus = &of_bus_type;
600 strcpy(op->dev.bus_id, "root");
602 sprintf(op->dev.bus_id, "%08x", dp->node);
604 if (of_device_register(op)) {
605 printk("%s: Could not register of device.\n",
614 static void __init scan_tree(struct device_node *dp, struct device *parent)
617 struct of_device *op = scan_one_device(dp, parent);
620 scan_tree(dp->child, &op->dev);
626 static void __init scan_of_devices(void)
628 struct device_node *root = of_find_node_by_path("/");
629 struct of_device *parent;
631 parent = scan_one_device(root, NULL);
635 scan_tree(root->child, &parent->dev);
638 static int __init of_bus_driver_init(void)
642 err = bus_register(&of_bus_type);
645 err = bus_register(&ebus_bus_type);
649 err = bus_register(&sbus_bus_type);
658 postcore_initcall(of_bus_driver_init);
660 static int __init of_debug(char *str)
664 get_option(&str, &val);
666 of_resource_verbose = 1;
670 __setup("of_debug=", of_debug);
672 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
674 /* initialize common driver fields */
675 drv->driver.name = drv->name;
676 drv->driver.bus = bus;
678 /* register with core */
679 return driver_register(&drv->driver);
682 void of_unregister_driver(struct of_platform_driver *drv)
684 driver_unregister(&drv->driver);
687 struct of_device* of_platform_device_create(struct device_node *np,
689 struct device *parent,
690 struct bus_type *bus)
692 struct of_device *dev;
694 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
698 dev->dev.parent = parent;
700 dev->dev.release = of_release_dev;
702 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
704 if (of_device_register(dev) != 0) {
712 EXPORT_SYMBOL(of_register_driver);
713 EXPORT_SYMBOL(of_unregister_driver);
714 EXPORT_SYMBOL(of_platform_device_create);