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>
8 #include <linux/errno.h>
10 #include <linux/of_device.h>
11 #include <linux/of_platform.h>
13 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
15 unsigned long ret = res->start + offset;
18 if (res->flags & IORESOURCE_MEM)
19 r = request_mem_region(ret, size, name);
21 r = request_region(ret, size, name);
25 return (void __iomem *) ret;
27 EXPORT_SYMBOL(of_ioremap);
29 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
31 if (res->flags & IORESOURCE_MEM)
32 release_mem_region((unsigned long) base, size);
34 release_region((unsigned long) base, size);
36 EXPORT_SYMBOL(of_iounmap);
38 static int node_match(struct device *dev, void *data)
40 struct of_device *op = to_of_device(dev);
41 struct device_node *dp = data;
43 return (op->node == dp);
46 struct of_device *of_find_device_by_node(struct device_node *dp)
48 struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
52 return to_of_device(dev);
56 EXPORT_SYMBOL(of_find_device_by_node);
59 struct bus_type ebus_bus_type;
60 EXPORT_SYMBOL(ebus_bus_type);
64 struct bus_type sbus_bus_type;
65 EXPORT_SYMBOL(sbus_bus_type);
68 struct bus_type of_platform_bus_type;
69 EXPORT_SYMBOL(of_platform_bus_type);
71 static inline u64 of_read_addr(const u32 *cell, int size)
75 r = (r << 32) | *(cell++);
79 static void __init get_cells(struct device_node *dp,
80 int *addrc, int *sizec)
83 *addrc = of_n_addr_cells(dp);
85 *sizec = of_n_size_cells(dp);
88 /* Max address size we deal with */
89 #define OF_MAX_ADDR_CELLS 4
93 const char *addr_prop_name;
94 int (*match)(struct device_node *parent);
95 void (*count_cells)(struct device_node *child,
96 int *addrc, int *sizec);
97 int (*map)(u32 *addr, const u32 *range,
98 int na, int ns, int pna);
99 unsigned int (*get_flags)(const u32 *addr);
103 * Default translator (generic bus)
106 static void of_bus_default_count_cells(struct device_node *dev,
107 int *addrc, int *sizec)
109 get_cells(dev, addrc, sizec);
112 /* Make sure the least significant 64-bits are in-range. Even
113 * for 3 or 4 cell values it is a good enough approximation.
115 static int of_out_of_range(const u32 *addr, const u32 *base,
116 const u32 *size, int na, int ns)
118 u64 a = of_read_addr(addr, na);
119 u64 b = of_read_addr(base, na);
124 b += of_read_addr(size, ns);
131 static int of_bus_default_map(u32 *addr, const u32 *range,
132 int na, int ns, int pna)
134 u32 result[OF_MAX_ADDR_CELLS];
138 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
142 if (of_out_of_range(addr, range, range + na + pna, na, ns))
145 /* Start with the parent range base. */
146 memcpy(result, range + na, pna * 4);
148 /* Add in the child address offset. */
149 for (i = 0; i < na; i++)
150 result[pna - 1 - i] +=
154 memcpy(addr, result, pna * 4);
159 static unsigned int of_bus_default_get_flags(const u32 *addr)
161 return IORESOURCE_MEM;
165 * PCI bus specific translator
168 static int of_bus_pci_match(struct device_node *np)
170 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
171 const char *model = of_get_property(np, "model", NULL);
173 if (model && !strcmp(model, "SUNW,simba"))
176 /* Do not do PCI specific frobbing if the
177 * PCI bridge lacks a ranges property. We
178 * want to pass it through up to the next
179 * parent as-is, not with the PCI translate
180 * method which chops off the top address cell.
182 if (!of_find_property(np, "ranges", NULL))
191 static int of_bus_simba_match(struct device_node *np)
193 const char *model = of_get_property(np, "model", NULL);
195 if (model && !strcmp(model, "SUNW,simba"))
198 /* Treat PCI busses lacking ranges property just like
201 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
202 if (!of_find_property(np, "ranges", NULL))
209 static int of_bus_simba_map(u32 *addr, const u32 *range,
210 int na, int ns, int pna)
215 static void of_bus_pci_count_cells(struct device_node *np,
216 int *addrc, int *sizec)
224 static int of_bus_pci_map(u32 *addr, const u32 *range,
225 int na, int ns, int pna)
227 u32 result[OF_MAX_ADDR_CELLS];
230 /* Check address type match */
231 if ((addr[0] ^ range[0]) & 0x03000000)
234 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
238 /* Start with the parent range base. */
239 memcpy(result, range + na, pna * 4);
241 /* Add in the child address offset, skipping high cell. */
242 for (i = 0; i < na - 1; i++)
243 result[pna - 1 - i] +=
247 memcpy(addr, result, pna * 4);
252 static unsigned int of_bus_pci_get_flags(const u32 *addr)
254 unsigned int flags = 0;
257 switch((w >> 24) & 0x03) {
259 flags |= IORESOURCE_IO;
260 case 0x02: /* 32 bits */
261 case 0x03: /* 64 bits */
262 flags |= IORESOURCE_MEM;
265 flags |= IORESOURCE_PREFETCH;
270 * SBUS bus specific translator
273 static int of_bus_sbus_match(struct device_node *np)
275 return !strcmp(np->name, "sbus") ||
276 !strcmp(np->name, "sbi");
279 static void of_bus_sbus_count_cells(struct device_node *child,
280 int *addrc, int *sizec)
289 * FHC/Central bus specific translator.
291 * This is just needed to hard-code the address and size cell
292 * counts. 'fhc' and 'central' nodes lack the #address-cells and
293 * #size-cells properties, and if you walk to the root on such
294 * Enterprise boxes all you'll get is a #size-cells of 2 which is
295 * not what we want to use.
297 static int of_bus_fhc_match(struct device_node *np)
299 return !strcmp(np->name, "fhc") ||
300 !strcmp(np->name, "central");
303 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
306 * Array of bus specific translators
309 static struct of_bus of_busses[] = {
313 .addr_prop_name = "assigned-addresses",
314 .match = of_bus_pci_match,
315 .count_cells = of_bus_pci_count_cells,
316 .map = of_bus_pci_map,
317 .get_flags = of_bus_pci_get_flags,
322 .addr_prop_name = "assigned-addresses",
323 .match = of_bus_simba_match,
324 .count_cells = of_bus_pci_count_cells,
325 .map = of_bus_simba_map,
326 .get_flags = of_bus_pci_get_flags,
331 .addr_prop_name = "reg",
332 .match = of_bus_sbus_match,
333 .count_cells = of_bus_sbus_count_cells,
334 .map = of_bus_default_map,
335 .get_flags = of_bus_default_get_flags,
340 .addr_prop_name = "reg",
341 .match = of_bus_fhc_match,
342 .count_cells = of_bus_fhc_count_cells,
343 .map = of_bus_default_map,
344 .get_flags = of_bus_default_get_flags,
349 .addr_prop_name = "reg",
351 .count_cells = of_bus_default_count_cells,
352 .map = of_bus_default_map,
353 .get_flags = of_bus_default_get_flags,
357 static struct of_bus *of_match_bus(struct device_node *np)
361 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
362 if (!of_busses[i].match || of_busses[i].match(np))
363 return &of_busses[i];
368 static int __init build_one_resource(struct device_node *parent,
372 int na, int ns, int pna)
378 ranges = of_get_property(parent, "ranges", &rlen);
379 if (ranges == NULL || rlen == 0) {
380 u32 result[OF_MAX_ADDR_CELLS];
383 memset(result, 0, pna * 4);
384 for (i = 0; i < na; i++)
385 result[pna - 1 - i] =
388 memcpy(addr, result, pna * 4);
392 /* Now walk through the ranges */
394 rone = na + pna + ns;
395 for (; rlen >= rone; rlen -= rone, ranges += rone) {
396 if (!bus->map(addr, ranges, na, ns, pna))
400 /* When we miss an I/O space match on PCI, just pass it up
401 * to the next PCI bridge and/or controller.
403 if (!strcmp(bus->name, "pci") &&
404 (addr[0] & 0x03000000) == 0x01000000)
410 static int __init use_1to1_mapping(struct device_node *pp)
412 /* If we have a ranges property in the parent, use it. */
413 if (of_find_property(pp, "ranges", NULL) != NULL)
416 /* If the parent is the dma node of an ISA bus, pass
417 * the translation up to the root.
419 if (!strcmp(pp->name, "dma"))
422 /* Similarly for all PCI bridges, if we get this far
423 * it lacks a ranges property, and this will include
426 if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex"))
432 static int of_resource_verbose;
434 static void __init build_device_resources(struct of_device *op,
435 struct device *parent)
437 struct of_device *p_op;
446 p_op = to_of_device(parent);
447 bus = of_match_bus(p_op->node);
448 bus->count_cells(op->node, &na, &ns);
450 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
451 if (!preg || num_reg == 0)
454 /* Convert to num-cells. */
457 /* Convert to num-entries. */
460 /* Prevent overrunning the op->resources[] array. */
461 if (num_reg > PROMREG_MAX) {
462 printk(KERN_WARNING "%s: Too many regs (%d), "
464 op->node->full_name, num_reg, PROMREG_MAX);
465 num_reg = PROMREG_MAX;
468 for (index = 0; index < num_reg; index++) {
469 struct resource *r = &op->resource[index];
470 u32 addr[OF_MAX_ADDR_CELLS];
471 const u32 *reg = (preg + (index * ((na + ns) * 4)));
472 struct device_node *dp = op->node;
473 struct device_node *pp = p_op->node;
474 struct of_bus *pbus, *dbus;
475 u64 size, result = OF_BAD_ADDR;
480 size = of_read_addr(reg + na, ns);
481 flags = bus->get_flags(reg);
483 memcpy(addr, reg, na * 4);
485 if (use_1to1_mapping(pp)) {
486 result = of_read_addr(addr, na);
498 result = of_read_addr(addr, dna);
502 pbus = of_match_bus(pp);
503 pbus->count_cells(dp, &pna, &pns);
505 if (build_one_resource(dp, dbus, pbus, addr,
515 memset(r, 0, sizeof(*r));
517 if (of_resource_verbose)
518 printk("%s reg[%d] -> %lx\n",
519 op->node->full_name, index,
522 if (result != OF_BAD_ADDR) {
523 if (tlb_type == hypervisor)
524 result &= 0x0fffffffffffffffUL;
527 r->end = result + size - 1;
530 r->name = op->node->name;
534 static struct device_node * __init
535 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
536 const u32 *imap, int imlen, const u32 *imask,
539 struct device_node *cp;
540 unsigned int irq = *irq_p;
546 bus = of_match_bus(pp);
547 bus->count_cells(dp, &na, NULL);
549 reg = of_get_property(dp, "reg", &num_reg);
550 if (!reg || !num_reg)
553 imlen /= ((na + 3) * 4);
555 for (i = 0; i < imlen; i++) {
558 for (j = 0; j < na; j++) {
559 if ((reg[j] & imask[j]) != imap[j])
562 if (imap[na] == irq) {
563 handle = imap[na + 1];
572 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
573 * properties that do not include the on-board device
574 * interrupts. Instead, the device's 'interrupts' property
575 * is already a fully specified INO value.
577 * Handle this by deciding that, if we didn't get a
578 * match in the parent's 'interrupt-map', and the
579 * parent is an IRQ translater, then use the parent as
580 * our IRQ controller.
589 cp = of_find_node_by_phandle(handle);
594 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
595 struct device_node *pp,
598 const struct linux_prom_pci_registers *regs;
599 unsigned int bus, devfn, slot, ret;
601 if (irq < 1 || irq > 4)
604 regs = of_get_property(dp, "reg", NULL);
608 bus = (regs->phys_hi >> 16) & 0xff;
609 devfn = (regs->phys_hi >> 8) & 0xff;
610 slot = (devfn >> 3) & 0x1f;
613 /* Derived from Table 8-3, U2P User's Manual. This branch
614 * is handling a PCI controller that lacks a proper set of
615 * interrupt-map and interrupt-map-mask properties. The
616 * Ultra-E450 is one example.
618 * The bit layout is BSSLL, where:
619 * B: 0 on bus A, 1 on bus B
620 * D: 2-bit slot number, derived from PCI device number as
621 * (dev - 1) for bus A, or (dev - 2) for bus B
622 * L: 2-bit line number
627 slot = (slot - 1) << 2;
631 slot = (slot - 2) << 2;
635 ret = (bus | slot | irq);
637 /* Going through a PCI-PCI bridge that lacks a set of
638 * interrupt-map and interrupt-map-mask properties.
640 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
646 static int of_irq_verbose;
648 static unsigned int __init build_one_device_irq(struct of_device *op,
649 struct device *parent,
652 struct device_node *dp = op->node;
653 struct device_node *pp, *ip;
654 unsigned int orig_irq = irq;
657 if (irq == 0xffffffff)
661 irq = dp->irq_trans->irq_build(dp, irq,
662 dp->irq_trans->data);
665 printk("%s: direct translate %x --> %x\n",
666 dp->full_name, orig_irq, irq);
671 /* Something more complicated. Walk up to the root, applying
672 * interrupt-map or bus specific translations, until we hit
675 * If we hit a bus type or situation we cannot handle, we
676 * stop and assume that the original IRQ number was in a
677 * format which has special meaning to it's immediate parent.
682 const void *imap, *imsk;
685 imap = of_get_property(pp, "interrupt-map", &imlen);
686 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
688 struct device_node *iret;
689 int this_orig_irq = irq;
691 iret = apply_interrupt_map(dp, pp,
696 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
698 pp->full_name, this_orig_irq,
699 (iret ? iret->full_name : "NULL"), irq);
704 if (iret->irq_trans) {
709 if (!strcmp(pp->type, "pci") ||
710 !strcmp(pp->type, "pciex")) {
711 unsigned int this_orig_irq = irq;
713 irq = pci_irq_swizzle(dp, pp, irq);
715 printk("%s: PCI swizzle [%s] "
718 pp->full_name, this_orig_irq,
734 irq = ip->irq_trans->irq_build(op->node, irq,
735 ip->irq_trans->data);
737 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
738 op->node->full_name, ip->full_name, orig_irq, irq);
741 nid = of_node_to_nid(dp);
743 cpumask_t numa_mask = node_to_cpumask(nid);
745 irq_set_affinity(irq, numa_mask);
751 static struct of_device * __init scan_one_device(struct device_node *dp,
752 struct device *parent)
754 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
755 const unsigned int *irq;
756 struct dev_archdata *sd;
762 sd = &op->dev.archdata;
768 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
770 op->portid = of_getintprop_default(dp, "upa-portid", -1);
771 if (op->portid == -1)
772 op->portid = of_getintprop_default(dp, "portid", -1);
774 irq = of_get_property(dp, "interrupts", &len);
776 memcpy(op->irqs, irq, len);
777 op->num_irqs = len / 4;
782 /* Prevent overrunning the op->irqs[] array. */
783 if (op->num_irqs > PROMINTR_MAX) {
784 printk(KERN_WARNING "%s: Too many irqs (%d), "
786 dp->full_name, op->num_irqs, PROMINTR_MAX);
787 op->num_irqs = PROMINTR_MAX;
790 build_device_resources(op, parent);
791 for (i = 0; i < op->num_irqs; i++)
792 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
794 op->dev.parent = parent;
795 op->dev.bus = &of_platform_bus_type;
797 dev_set_name(&op->dev, "root");
799 dev_set_name(&op->dev, "%08x", dp->node);
801 if (of_device_register(op)) {
802 printk("%s: Could not register of device.\n",
811 static void __init scan_tree(struct device_node *dp, struct device *parent)
814 struct of_device *op = scan_one_device(dp, parent);
817 scan_tree(dp->child, &op->dev);
823 static void __init scan_of_devices(void)
825 struct device_node *root = of_find_node_by_path("/");
826 struct of_device *parent;
828 parent = scan_one_device(root, NULL);
832 scan_tree(root->child, &parent->dev);
835 static int __init of_bus_driver_init(void)
839 err = of_bus_type_init(&of_platform_bus_type, "of");
842 err = of_bus_type_init(&ebus_bus_type, "ebus");
846 err = of_bus_type_init(&sbus_bus_type, "sbus");
855 postcore_initcall(of_bus_driver_init);
857 static int __init of_debug(char *str)
861 get_option(&str, &val);
863 of_resource_verbose = 1;
869 __setup("of_debug=", of_debug);