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 long (*get_flags)(const u32 *addr, unsigned long);
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 long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
163 return IORESOURCE_MEM;
167 * PCI bus specific translator
170 static int of_bus_pci_match(struct device_node *np)
172 if (!strcmp(np->name, "pci")) {
173 const char *model = of_get_property(np, "model", NULL);
175 if (model && !strcmp(model, "SUNW,simba"))
178 /* Do not do PCI specific frobbing if the
179 * PCI bridge lacks a ranges property. We
180 * want to pass it through up to the next
181 * parent as-is, not with the PCI translate
182 * method which chops off the top address cell.
184 if (!of_find_property(np, "ranges", NULL))
193 static int of_bus_simba_match(struct device_node *np)
195 const char *model = of_get_property(np, "model", NULL);
197 if (model && !strcmp(model, "SUNW,simba"))
200 /* Treat PCI busses lacking ranges property just like
203 if (!strcmp(np->name, "pci")) {
204 if (!of_find_property(np, "ranges", NULL))
211 static int of_bus_simba_map(u32 *addr, const u32 *range,
212 int na, int ns, int pna)
217 static void of_bus_pci_count_cells(struct device_node *np,
218 int *addrc, int *sizec)
226 static int of_bus_pci_map(u32 *addr, const u32 *range,
227 int na, int ns, int pna)
229 u32 result[OF_MAX_ADDR_CELLS];
232 /* Check address type match */
233 if ((addr[0] ^ range[0]) & 0x03000000)
236 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
240 /* Start with the parent range base. */
241 memcpy(result, range + na, pna * 4);
243 /* Add in the child address offset, skipping high cell. */
244 for (i = 0; i < na - 1; i++)
245 result[pna - 1 - i] +=
249 memcpy(addr, result, pna * 4);
254 static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
258 /* For PCI, we override whatever child busses may have used. */
260 switch((w >> 24) & 0x03) {
262 flags |= IORESOURCE_IO;
265 case 0x02: /* 32 bits */
266 case 0x03: /* 64 bits */
267 flags |= IORESOURCE_MEM;
271 flags |= IORESOURCE_PREFETCH;
276 * SBUS bus specific translator
279 static int of_bus_sbus_match(struct device_node *np)
281 return !strcmp(np->name, "sbus") ||
282 !strcmp(np->name, "sbi");
285 static void of_bus_sbus_count_cells(struct device_node *child,
286 int *addrc, int *sizec)
295 * FHC/Central bus specific translator.
297 * This is just needed to hard-code the address and size cell
298 * counts. 'fhc' and 'central' nodes lack the #address-cells and
299 * #size-cells properties, and if you walk to the root on such
300 * Enterprise boxes all you'll get is a #size-cells of 2 which is
301 * not what we want to use.
303 static int of_bus_fhc_match(struct device_node *np)
305 return !strcmp(np->name, "fhc") ||
306 !strcmp(np->name, "central");
309 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
312 * Array of bus specific translators
315 static struct of_bus of_busses[] = {
319 .addr_prop_name = "assigned-addresses",
320 .match = of_bus_pci_match,
321 .count_cells = of_bus_pci_count_cells,
322 .map = of_bus_pci_map,
323 .get_flags = of_bus_pci_get_flags,
328 .addr_prop_name = "assigned-addresses",
329 .match = of_bus_simba_match,
330 .count_cells = of_bus_pci_count_cells,
331 .map = of_bus_simba_map,
332 .get_flags = of_bus_pci_get_flags,
337 .addr_prop_name = "reg",
338 .match = of_bus_sbus_match,
339 .count_cells = of_bus_sbus_count_cells,
340 .map = of_bus_default_map,
341 .get_flags = of_bus_default_get_flags,
346 .addr_prop_name = "reg",
347 .match = of_bus_fhc_match,
348 .count_cells = of_bus_fhc_count_cells,
349 .map = of_bus_default_map,
350 .get_flags = of_bus_default_get_flags,
355 .addr_prop_name = "reg",
357 .count_cells = of_bus_default_count_cells,
358 .map = of_bus_default_map,
359 .get_flags = of_bus_default_get_flags,
363 static struct of_bus *of_match_bus(struct device_node *np)
367 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
368 if (!of_busses[i].match || of_busses[i].match(np))
369 return &of_busses[i];
374 static int __init build_one_resource(struct device_node *parent,
378 int na, int ns, int pna)
384 ranges = of_get_property(parent, "ranges", &rlen);
385 if (ranges == NULL || rlen == 0) {
386 u32 result[OF_MAX_ADDR_CELLS];
389 memset(result, 0, pna * 4);
390 for (i = 0; i < na; i++)
391 result[pna - 1 - i] =
394 memcpy(addr, result, pna * 4);
398 /* Now walk through the ranges */
400 rone = na + pna + ns;
401 for (; rlen >= rone; rlen -= rone, ranges += rone) {
402 if (!bus->map(addr, ranges, na, ns, pna))
406 /* When we miss an I/O space match on PCI, just pass it up
407 * to the next PCI bridge and/or controller.
409 if (!strcmp(bus->name, "pci") &&
410 (addr[0] & 0x03000000) == 0x01000000)
416 static int __init use_1to1_mapping(struct device_node *pp)
418 /* If we have a ranges property in the parent, use it. */
419 if (of_find_property(pp, "ranges", NULL) != NULL)
422 /* If the parent is the dma node of an ISA bus, pass
423 * the translation up to the root.
425 if (!strcmp(pp->name, "dma"))
428 /* Similarly for all PCI bridges, if we get this far
429 * it lacks a ranges property, and this will include
432 if (!strcmp(pp->name, "pci"))
438 static int of_resource_verbose;
440 static void __init build_device_resources(struct of_device *op,
441 struct device *parent)
443 struct of_device *p_op;
452 p_op = to_of_device(parent);
453 bus = of_match_bus(p_op->node);
454 bus->count_cells(op->node, &na, &ns);
456 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
457 if (!preg || num_reg == 0)
460 /* Convert to num-cells. */
463 /* Convert to num-entries. */
466 /* Prevent overrunning the op->resources[] array. */
467 if (num_reg > PROMREG_MAX) {
468 printk(KERN_WARNING "%s: Too many regs (%d), "
470 op->node->full_name, num_reg, PROMREG_MAX);
471 num_reg = PROMREG_MAX;
474 for (index = 0; index < num_reg; index++) {
475 struct resource *r = &op->resource[index];
476 u32 addr[OF_MAX_ADDR_CELLS];
477 const u32 *reg = (preg + (index * ((na + ns) * 4)));
478 struct device_node *dp = op->node;
479 struct device_node *pp = p_op->node;
480 struct of_bus *pbus, *dbus;
481 u64 size, result = OF_BAD_ADDR;
486 size = of_read_addr(reg + na, ns);
487 memcpy(addr, reg, na * 4);
489 flags = bus->get_flags(addr, 0);
491 if (use_1to1_mapping(pp)) {
492 result = of_read_addr(addr, na);
504 result = of_read_addr(addr, dna);
508 pbus = of_match_bus(pp);
509 pbus->count_cells(dp, &pna, &pns);
511 if (build_one_resource(dp, dbus, pbus, addr,
515 flags = pbus->get_flags(addr, flags);
523 memset(r, 0, sizeof(*r));
525 if (of_resource_verbose)
526 printk("%s reg[%d] -> %lx\n",
527 op->node->full_name, index,
530 if (result != OF_BAD_ADDR) {
531 if (tlb_type == hypervisor)
532 result &= 0x0fffffffffffffffUL;
535 r->end = result + size - 1;
538 r->name = op->node->name;
542 static struct device_node * __init
543 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
544 const u32 *imap, int imlen, const u32 *imask,
547 struct device_node *cp;
548 unsigned int irq = *irq_p;
554 bus = of_match_bus(pp);
555 bus->count_cells(dp, &na, NULL);
557 reg = of_get_property(dp, "reg", &num_reg);
558 if (!reg || !num_reg)
561 imlen /= ((na + 3) * 4);
563 for (i = 0; i < imlen; i++) {
566 for (j = 0; j < na; j++) {
567 if ((reg[j] & imask[j]) != imap[j])
570 if (imap[na] == irq) {
571 handle = imap[na + 1];
580 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
581 * properties that do not include the on-board device
582 * interrupts. Instead, the device's 'interrupts' property
583 * is already a fully specified INO value.
585 * Handle this by deciding that, if we didn't get a
586 * match in the parent's 'interrupt-map', and the
587 * parent is an IRQ translater, then use the parent as
588 * our IRQ controller.
597 cp = of_find_node_by_phandle(handle);
602 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
603 struct device_node *pp,
606 const struct linux_prom_pci_registers *regs;
607 unsigned int bus, devfn, slot, ret;
609 if (irq < 1 || irq > 4)
612 regs = of_get_property(dp, "reg", NULL);
616 bus = (regs->phys_hi >> 16) & 0xff;
617 devfn = (regs->phys_hi >> 8) & 0xff;
618 slot = (devfn >> 3) & 0x1f;
621 /* Derived from Table 8-3, U2P User's Manual. This branch
622 * is handling a PCI controller that lacks a proper set of
623 * interrupt-map and interrupt-map-mask properties. The
624 * Ultra-E450 is one example.
626 * The bit layout is BSSLL, where:
627 * B: 0 on bus A, 1 on bus B
628 * D: 2-bit slot number, derived from PCI device number as
629 * (dev - 1) for bus A, or (dev - 2) for bus B
630 * L: 2-bit line number
635 slot = (slot - 1) << 2;
639 slot = (slot - 2) << 2;
643 ret = (bus | slot | irq);
645 /* Going through a PCI-PCI bridge that lacks a set of
646 * interrupt-map and interrupt-map-mask properties.
648 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
654 static int of_irq_verbose;
656 static unsigned int __init build_one_device_irq(struct of_device *op,
657 struct device *parent,
660 struct device_node *dp = op->node;
661 struct device_node *pp, *ip;
662 unsigned int orig_irq = irq;
665 if (irq == 0xffffffff)
669 irq = dp->irq_trans->irq_build(dp, irq,
670 dp->irq_trans->data);
673 printk("%s: direct translate %x --> %x\n",
674 dp->full_name, orig_irq, irq);
679 /* Something more complicated. Walk up to the root, applying
680 * interrupt-map or bus specific translations, until we hit
683 * If we hit a bus type or situation we cannot handle, we
684 * stop and assume that the original IRQ number was in a
685 * format which has special meaning to it's immediate parent.
690 const void *imap, *imsk;
693 imap = of_get_property(pp, "interrupt-map", &imlen);
694 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
696 struct device_node *iret;
697 int this_orig_irq = irq;
699 iret = apply_interrupt_map(dp, pp,
704 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
706 pp->full_name, this_orig_irq,
707 (iret ? iret->full_name : "NULL"), irq);
712 if (iret->irq_trans) {
717 if (!strcmp(pp->name, "pci")) {
718 unsigned int this_orig_irq = irq;
720 irq = pci_irq_swizzle(dp, pp, irq);
722 printk("%s: PCI swizzle [%s] "
725 pp->full_name, this_orig_irq,
741 irq = ip->irq_trans->irq_build(op->node, irq,
742 ip->irq_trans->data);
744 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
745 op->node->full_name, ip->full_name, orig_irq, irq);
748 nid = of_node_to_nid(dp);
750 cpumask_t numa_mask = node_to_cpumask(nid);
752 irq_set_affinity(irq, numa_mask);
758 static struct of_device * __init scan_one_device(struct device_node *dp,
759 struct device *parent)
761 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
762 const unsigned int *irq;
763 struct dev_archdata *sd;
769 sd = &op->dev.archdata;
775 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
777 op->portid = of_getintprop_default(dp, "upa-portid", -1);
778 if (op->portid == -1)
779 op->portid = of_getintprop_default(dp, "portid", -1);
781 irq = of_get_property(dp, "interrupts", &len);
783 memcpy(op->irqs, irq, len);
784 op->num_irqs = len / 4;
789 /* Prevent overrunning the op->irqs[] array. */
790 if (op->num_irqs > PROMINTR_MAX) {
791 printk(KERN_WARNING "%s: Too many irqs (%d), "
793 dp->full_name, op->num_irqs, PROMINTR_MAX);
794 op->num_irqs = PROMINTR_MAX;
797 build_device_resources(op, parent);
798 for (i = 0; i < op->num_irqs; i++)
799 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
801 op->dev.parent = parent;
802 op->dev.bus = &of_platform_bus_type;
804 dev_set_name(&op->dev, "root");
806 dev_set_name(&op->dev, "%08x", dp->node);
808 if (of_device_register(op)) {
809 printk("%s: Could not register of device.\n",
818 static void __init scan_tree(struct device_node *dp, struct device *parent)
821 struct of_device *op = scan_one_device(dp, parent);
824 scan_tree(dp->child, &op->dev);
830 static void __init scan_of_devices(void)
832 struct device_node *root = of_find_node_by_path("/");
833 struct of_device *parent;
835 parent = scan_one_device(root, NULL);
839 scan_tree(root->child, &parent->dev);
842 static int __init of_bus_driver_init(void)
846 err = of_bus_type_init(&of_platform_bus_type, "of");
849 err = of_bus_type_init(&ebus_bus_type, "ebus");
853 err = of_bus_type_init(&sbus_bus_type, "sbus");
862 postcore_initcall(of_bus_driver_init);
864 static int __init of_debug(char *str)
868 get_option(&str, &val);
870 of_resource_verbose = 1;
876 __setup("of_debug=", of_debug);