[SERIAL] Make uart_line_info() correctly tell MMIO from I/O port
[linux-2.6/openmoko-kernel/knife-kernel.git] / arch / sparc / kernel / of_device.c
blob97bf87e8cdde56853b86dda4677569be491dff9f
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>
9 #include <asm/errno.h>
10 #include <asm/of_device.h>
12 /**
13 * of_match_device - Tell if an of_device structure has a matching
14 * of_match structure
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)
24 if (!dev->node)
25 return NULL;
26 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
27 int match = 1;
28 if (matches->name[0])
29 match &= dev->node->name
30 && !strcmp(matches->name, dev->node->name);
31 if (matches->type[0])
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,
36 matches->compatible);
37 if (match)
38 return matches;
39 matches++;
41 return NULL;
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;
50 if (!matches)
51 return 0;
53 return of_match_device(matches, of_dev) != NULL;
56 struct of_device *of_dev_get(struct of_device *dev)
58 struct device *tmp;
60 if (!dev)
61 return NULL;
62 tmp = get_device(&dev->dev);
63 if (tmp)
64 return to_of_device(tmp);
65 else
66 return NULL;
69 void of_dev_put(struct of_device *dev)
71 if (dev)
72 put_device(&dev->dev);
76 static int of_device_probe(struct device *dev)
78 int error = -ENODEV;
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);
86 if (!drv->probe)
87 return error;
89 of_dev_get(of_dev);
91 match = of_match_device(drv->match_table, of_dev);
92 if (match)
93 error = drv->probe(of_dev, match);
94 if (error)
95 of_dev_put(of_dev);
97 return error;
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)
106 drv->remove(of_dev);
107 return 0;
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);
114 int error = 0;
116 if (dev->driver && drv->suspend)
117 error = drv->suspend(of_dev, state);
118 return error;
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);
125 int error = 0;
127 if (dev->driver && drv->resume)
128 error = drv->resume(of_dev);
129 return error;
132 static int node_match(struct device *dev, void *data)
134 struct of_device *op = to_of_device(dev);
135 struct device_node *dp = data;
137 return (op->node == dp);
140 struct of_device *of_find_device_by_node(struct device_node *dp)
142 struct device *dev = bus_find_device(&of_bus_type, NULL,
143 dp, node_match);
145 if (dev)
146 return to_of_device(dev);
148 return NULL;
150 EXPORT_SYMBOL(of_find_device_by_node);
152 #ifdef CONFIG_PCI
153 struct bus_type ebus_bus_type = {
154 .name = "ebus",
155 .match = of_platform_bus_match,
156 .probe = of_device_probe,
157 .remove = of_device_remove,
158 .suspend = of_device_suspend,
159 .resume = of_device_resume,
161 EXPORT_SYMBOL(ebus_bus_type);
162 #endif
164 #ifdef CONFIG_SBUS
165 struct bus_type sbus_bus_type = {
166 .name = "sbus",
167 .match = of_platform_bus_match,
168 .probe = of_device_probe,
169 .remove = of_device_remove,
170 .suspend = of_device_suspend,
171 .resume = of_device_resume,
173 EXPORT_SYMBOL(sbus_bus_type);
174 #endif
176 struct bus_type of_bus_type = {
177 .name = "of",
178 .match = of_platform_bus_match,
179 .probe = of_device_probe,
180 .remove = of_device_remove,
181 .suspend = of_device_suspend,
182 .resume = of_device_resume,
184 EXPORT_SYMBOL(of_bus_type);
186 static inline u64 of_read_addr(const u32 *cell, int size)
188 u64 r = 0;
189 while (size--)
190 r = (r << 32) | *(cell++);
191 return r;
194 static void __init get_cells(struct device_node *dp,
195 int *addrc, int *sizec)
197 if (addrc)
198 *addrc = of_n_addr_cells(dp);
199 if (sizec)
200 *sizec = of_n_size_cells(dp);
203 /* Max address size we deal with */
204 #define OF_MAX_ADDR_CELLS 4
206 struct of_bus {
207 const char *name;
208 const char *addr_prop_name;
209 int (*match)(struct device_node *parent);
210 void (*count_cells)(struct device_node *child,
211 int *addrc, int *sizec);
212 int (*map)(u32 *addr, const u32 *range,
213 int na, int ns, int pna);
214 unsigned int (*get_flags)(u32 *addr);
218 * Default translator (generic bus)
221 static void of_bus_default_count_cells(struct device_node *dev,
222 int *addrc, int *sizec)
224 get_cells(dev, addrc, sizec);
227 /* Make sure the least significant 64-bits are in-range. Even
228 * for 3 or 4 cell values it is a good enough approximation.
230 static int of_out_of_range(const u32 *addr, const u32 *base,
231 const u32 *size, int na, int ns)
233 u64 a = of_read_addr(addr, na);
234 u64 b = of_read_addr(base, na);
236 if (a < b)
237 return 1;
239 b += of_read_addr(size, ns);
240 if (a >= b)
241 return 1;
243 return 0;
246 static int of_bus_default_map(u32 *addr, const u32 *range,
247 int na, int ns, int pna)
249 u32 result[OF_MAX_ADDR_CELLS];
250 int i;
252 if (ns > 2) {
253 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
254 return -EINVAL;
257 if (of_out_of_range(addr, range, range + na + pna, na, ns))
258 return -EINVAL;
260 /* Start with the parent range base. */
261 memcpy(result, range + na, pna * 4);
263 /* Add in the child address offset. */
264 for (i = 0; i < na; i++)
265 result[pna - 1 - i] +=
266 (addr[na - 1 - i] -
267 range[na - 1 - i]);
269 memcpy(addr, result, pna * 4);
271 return 0;
274 static unsigned int of_bus_default_get_flags(u32 *addr)
276 return IORESOURCE_MEM;
280 * PCI bus specific translator
283 static int of_bus_pci_match(struct device_node *np)
285 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
286 /* Do not do PCI specific frobbing if the
287 * PCI bridge lacks a ranges property. We
288 * want to pass it through up to the next
289 * parent as-is, not with the PCI translate
290 * method which chops off the top address cell.
292 if (!of_find_property(np, "ranges", NULL))
293 return 0;
295 return 1;
298 return 0;
301 static void of_bus_pci_count_cells(struct device_node *np,
302 int *addrc, int *sizec)
304 if (addrc)
305 *addrc = 3;
306 if (sizec)
307 *sizec = 2;
310 static int of_bus_pci_map(u32 *addr, const u32 *range,
311 int na, int ns, int pna)
313 u32 result[OF_MAX_ADDR_CELLS];
314 int i;
316 /* Check address type match */
317 if ((addr[0] ^ range[0]) & 0x03000000)
318 return -EINVAL;
320 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
321 na - 1, ns))
322 return -EINVAL;
324 /* Start with the parent range base. */
325 memcpy(result, range + na, pna * 4);
327 /* Add in the child address offset, skipping high cell. */
328 for (i = 0; i < na - 1; i++)
329 result[pna - 1 - i] +=
330 (addr[na - 1 - i] -
331 range[na - 1 - i]);
333 memcpy(addr, result, pna * 4);
335 return 0;
338 static unsigned int of_bus_pci_get_flags(u32 *addr)
340 unsigned int flags = 0;
341 u32 w = addr[0];
343 switch((w >> 24) & 0x03) {
344 case 0x01:
345 flags |= IORESOURCE_IO;
346 case 0x02: /* 32 bits */
347 case 0x03: /* 64 bits */
348 flags |= IORESOURCE_MEM;
350 if (w & 0x40000000)
351 flags |= IORESOURCE_PREFETCH;
352 return flags;
356 * SBUS bus specific translator
359 static int of_bus_sbus_match(struct device_node *np)
361 return !strcmp(np->name, "sbus") ||
362 !strcmp(np->name, "sbi");
365 static void of_bus_sbus_count_cells(struct device_node *child,
366 int *addrc, int *sizec)
368 if (addrc)
369 *addrc = 2;
370 if (sizec)
371 *sizec = 1;
374 static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
376 return of_bus_default_map(addr, range, na, ns, pna);
379 static unsigned int of_bus_sbus_get_flags(u32 *addr)
381 return IORESOURCE_MEM;
386 * Array of bus specific translators
389 static struct of_bus of_busses[] = {
390 /* PCI */
392 .name = "pci",
393 .addr_prop_name = "assigned-addresses",
394 .match = of_bus_pci_match,
395 .count_cells = of_bus_pci_count_cells,
396 .map = of_bus_pci_map,
397 .get_flags = of_bus_pci_get_flags,
399 /* SBUS */
401 .name = "sbus",
402 .addr_prop_name = "reg",
403 .match = of_bus_sbus_match,
404 .count_cells = of_bus_sbus_count_cells,
405 .map = of_bus_sbus_map,
406 .get_flags = of_bus_sbus_get_flags,
408 /* Default */
410 .name = "default",
411 .addr_prop_name = "reg",
412 .match = NULL,
413 .count_cells = of_bus_default_count_cells,
414 .map = of_bus_default_map,
415 .get_flags = of_bus_default_get_flags,
419 static struct of_bus *of_match_bus(struct device_node *np)
421 int i;
423 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
424 if (!of_busses[i].match || of_busses[i].match(np))
425 return &of_busses[i];
426 BUG();
427 return NULL;
430 static int __init build_one_resource(struct device_node *parent,
431 struct of_bus *bus,
432 struct of_bus *pbus,
433 u32 *addr,
434 int na, int ns, int pna)
436 u32 *ranges;
437 unsigned int rlen;
438 int rone;
440 ranges = of_get_property(parent, "ranges", &rlen);
441 if (ranges == NULL || rlen == 0) {
442 u32 result[OF_MAX_ADDR_CELLS];
443 int i;
445 memset(result, 0, pna * 4);
446 for (i = 0; i < na; i++)
447 result[pna - 1 - i] =
448 addr[na - 1 - i];
450 memcpy(addr, result, pna * 4);
451 return 0;
454 /* Now walk through the ranges */
455 rlen /= 4;
456 rone = na + pna + ns;
457 for (; rlen >= rone; rlen -= rone, ranges += rone) {
458 if (!bus->map(addr, ranges, na, ns, pna))
459 return 0;
462 return 1;
465 static int of_resource_verbose;
467 static void __init build_device_resources(struct of_device *op,
468 struct device *parent)
470 struct of_device *p_op;
471 struct of_bus *bus;
472 int na, ns;
473 int index, num_reg;
474 void *preg;
476 if (!parent)
477 return;
479 p_op = to_of_device(parent);
480 bus = of_match_bus(p_op->node);
481 bus->count_cells(op->node, &na, &ns);
483 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
484 if (!preg || num_reg == 0)
485 return;
487 /* Convert to num-cells. */
488 num_reg /= 4;
490 /* Conver to num-entries. */
491 num_reg /= na + ns;
493 for (index = 0; index < num_reg; index++) {
494 struct resource *r = &op->resource[index];
495 u32 addr[OF_MAX_ADDR_CELLS];
496 u32 *reg = (preg + (index * ((na + ns) * 4)));
497 struct device_node *dp = op->node;
498 struct device_node *pp = p_op->node;
499 struct of_bus *pbus;
500 u64 size, result = OF_BAD_ADDR;
501 unsigned long flags;
502 int dna, dns;
503 int pna, pns;
505 size = of_read_addr(reg + na, ns);
506 flags = bus->get_flags(reg);
508 memcpy(addr, reg, na * 4);
510 /* If the immediate parent has no ranges property to apply,
511 * just use a 1<->1 mapping.
513 if (of_find_property(pp, "ranges", NULL) == NULL) {
514 result = of_read_addr(addr, na);
515 goto build_res;
518 dna = na;
519 dns = ns;
521 while (1) {
522 dp = pp;
523 pp = dp->parent;
524 if (!pp) {
525 result = of_read_addr(addr, dna);
526 break;
529 pbus = of_match_bus(pp);
530 pbus->count_cells(dp, &pna, &pns);
532 if (build_one_resource(dp, bus, pbus, addr,
533 dna, dns, pna))
534 break;
536 dna = pna;
537 dns = pns;
538 bus = pbus;
541 build_res:
542 memset(r, 0, sizeof(*r));
544 if (of_resource_verbose)
545 printk("%s reg[%d] -> %llx\n",
546 op->node->full_name, index,
547 result);
549 if (result != OF_BAD_ADDR) {
550 r->start = result & 0xffffffff;
551 r->end = result + size - 1;
552 r->flags = flags | ((result >> 32ULL) & 0xffUL);
553 } else {
554 r->start = ~0UL;
555 r->end = ~0UL;
557 r->name = op->node->name;
561 static struct of_device * __init scan_one_device(struct device_node *dp,
562 struct device *parent)
564 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
565 struct linux_prom_irqs *intr;
566 int len, i;
568 if (!op)
569 return NULL;
571 op->node = dp;
573 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
574 (25*1000*1000));
575 op->portid = of_getintprop_default(dp, "upa-portid", -1);
576 if (op->portid == -1)
577 op->portid = of_getintprop_default(dp, "portid", -1);
579 intr = of_get_property(dp, "intr", &len);
580 if (intr) {
581 op->num_irqs = len / sizeof(struct linux_prom_irqs);
582 for (i = 0; i < op->num_irqs; i++)
583 op->irqs[i] = intr[i].pri;
584 } else {
585 unsigned int *irq = of_get_property(dp, "interrupts", &len);
587 if (irq) {
588 op->num_irqs = len / sizeof(unsigned int);
589 for (i = 0; i < op->num_irqs; i++)
590 op->irqs[i] = irq[i];
591 } else {
592 op->num_irqs = 0;
595 if (sparc_cpu_model == sun4d) {
596 static int pil_to_sbus[] = {
597 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
599 struct device_node *io_unit, *sbi = dp->parent;
600 struct linux_prom_registers *regs;
601 int board, slot;
603 while (sbi) {
604 if (!strcmp(sbi->name, "sbi"))
605 break;
607 sbi = sbi->parent;
609 if (!sbi)
610 goto build_resources;
612 regs = of_get_property(dp, "reg", NULL);
613 if (!regs)
614 goto build_resources;
616 slot = regs->which_io;
618 /* If SBI's parent is not io-unit or the io-unit lacks
619 * a "board#" property, something is very wrong.
621 if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
622 printk("%s: Error, parent is not io-unit.\n",
623 sbi->full_name);
624 goto build_resources;
626 io_unit = sbi->parent;
627 board = of_getintprop_default(io_unit, "board#", -1);
628 if (board == -1) {
629 printk("%s: Error, lacks board# property.\n",
630 io_unit->full_name);
631 goto build_resources;
634 for (i = 0; i < op->num_irqs; i++) {
635 int this_irq = op->irqs[i];
636 int sbusl = pil_to_sbus[this_irq];
638 if (sbusl)
639 this_irq = (((board + 1) << 5) +
640 (sbusl << 2) +
641 slot);
643 op->irqs[i] = this_irq;
647 build_resources:
648 build_device_resources(op, parent);
650 op->dev.parent = parent;
651 op->dev.bus = &of_bus_type;
652 if (!parent)
653 strcpy(op->dev.bus_id, "root");
654 else
655 strcpy(op->dev.bus_id, dp->path_component_name);
657 if (of_device_register(op)) {
658 printk("%s: Could not register of device.\n",
659 dp->full_name);
660 kfree(op);
661 op = NULL;
664 return op;
667 static void __init scan_tree(struct device_node *dp, struct device *parent)
669 while (dp) {
670 struct of_device *op = scan_one_device(dp, parent);
672 if (op)
673 scan_tree(dp->child, &op->dev);
675 dp = dp->sibling;
679 static void __init scan_of_devices(void)
681 struct device_node *root = of_find_node_by_path("/");
682 struct of_device *parent;
684 parent = scan_one_device(root, NULL);
685 if (!parent)
686 return;
688 scan_tree(root->child, &parent->dev);
691 static int __init of_bus_driver_init(void)
693 int err;
695 err = bus_register(&of_bus_type);
696 #ifdef CONFIG_PCI
697 if (!err)
698 err = bus_register(&ebus_bus_type);
699 #endif
700 #ifdef CONFIG_SBUS
701 if (!err)
702 err = bus_register(&sbus_bus_type);
703 #endif
705 if (!err)
706 scan_of_devices();
708 return err;
711 postcore_initcall(of_bus_driver_init);
713 static int __init of_debug(char *str)
715 int val = 0;
717 get_option(&str, &val);
718 if (val & 1)
719 of_resource_verbose = 1;
720 return 1;
723 __setup("of_debug=", of_debug);
725 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
727 /* initialize common driver fields */
728 drv->driver.name = drv->name;
729 drv->driver.bus = bus;
731 /* register with core */
732 return driver_register(&drv->driver);
735 void of_unregister_driver(struct of_platform_driver *drv)
737 driver_unregister(&drv->driver);
741 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
743 struct of_device *ofdev;
745 ofdev = to_of_device(dev);
746 return sprintf(buf, "%s", ofdev->node->full_name);
749 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
752 * of_release_dev - free an of device structure when all users of it are finished.
753 * @dev: device that's been disconnected
755 * Will be called only by the device core when all users of this of device are
756 * done.
758 void of_release_dev(struct device *dev)
760 struct of_device *ofdev;
762 ofdev = to_of_device(dev);
764 kfree(ofdev);
767 int of_device_register(struct of_device *ofdev)
769 int rc;
771 BUG_ON(ofdev->node == NULL);
773 rc = device_register(&ofdev->dev);
774 if (rc)
775 return rc;
777 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
778 if (rc)
779 device_unregister(&ofdev->dev);
781 return rc;
784 void of_device_unregister(struct of_device *ofdev)
786 device_remove_file(&ofdev->dev, &dev_attr_devspec);
787 device_unregister(&ofdev->dev);
790 struct of_device* of_platform_device_create(struct device_node *np,
791 const char *bus_id,
792 struct device *parent,
793 struct bus_type *bus)
795 struct of_device *dev;
797 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
798 if (!dev)
799 return NULL;
800 memset(dev, 0, sizeof(*dev));
802 dev->dev.parent = parent;
803 dev->dev.bus = bus;
804 dev->dev.release = of_release_dev;
806 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
808 if (of_device_register(dev) != 0) {
809 kfree(dev);
810 return NULL;
813 return dev;
816 EXPORT_SYMBOL(of_match_device);
817 EXPORT_SYMBOL(of_register_driver);
818 EXPORT_SYMBOL(of_unregister_driver);
819 EXPORT_SYMBOL(of_device_register);
820 EXPORT_SYMBOL(of_device_unregister);
821 EXPORT_SYMBOL(of_dev_get);
822 EXPORT_SYMBOL(of_dev_put);
823 EXPORT_SYMBOL(of_platform_device_create);
824 EXPORT_SYMBOL(of_release_dev);