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);
58 unsigned int irq_of_parse_and_map(struct device_node *node, int index)
60 struct of_device *op = of_find_device_by_node(node);
62 if (!op || index >= op->num_irqs)
65 return op->irqs[index];
67 EXPORT_SYMBOL(irq_of_parse_and_map);
69 /* Take the archdata values for IOMMU, STC, and HOSTDATA found in
70 * BUS and propagate to all child of_device objects.
72 void of_propagate_archdata(struct of_device *bus)
74 struct dev_archdata *bus_sd = &bus->dev.archdata;
75 struct device_node *bus_dp = bus->node;
76 struct device_node *dp;
78 for (dp = bus_dp->child; dp; dp = dp->sibling) {
79 struct of_device *op = of_find_device_by_node(dp);
81 op->dev.archdata.iommu = bus_sd->iommu;
82 op->dev.archdata.stc = bus_sd->stc;
83 op->dev.archdata.host_controller = bus_sd->host_controller;
84 op->dev.archdata.numa_node = bus_sd->numa_node;
87 of_propagate_archdata(op);
91 struct bus_type of_platform_bus_type;
92 EXPORT_SYMBOL(of_platform_bus_type);
94 static inline u64 of_read_addr(const u32 *cell, int size)
98 r = (r << 32) | *(cell++);
102 static void __init get_cells(struct device_node *dp,
103 int *addrc, int *sizec)
106 *addrc = of_n_addr_cells(dp);
108 *sizec = of_n_size_cells(dp);
111 /* Max address size we deal with */
112 #define OF_MAX_ADDR_CELLS 4
116 const char *addr_prop_name;
117 int (*match)(struct device_node *parent);
118 void (*count_cells)(struct device_node *child,
119 int *addrc, int *sizec);
120 int (*map)(u32 *addr, const u32 *range,
121 int na, int ns, int pna);
122 unsigned long (*get_flags)(const u32 *addr, unsigned long);
126 * Default translator (generic bus)
129 static void of_bus_default_count_cells(struct device_node *dev,
130 int *addrc, int *sizec)
132 get_cells(dev, addrc, sizec);
135 /* Make sure the least significant 64-bits are in-range. Even
136 * for 3 or 4 cell values it is a good enough approximation.
138 static int of_out_of_range(const u32 *addr, const u32 *base,
139 const u32 *size, int na, int ns)
141 u64 a = of_read_addr(addr, na);
142 u64 b = of_read_addr(base, na);
147 b += of_read_addr(size, ns);
154 static int of_bus_default_map(u32 *addr, const u32 *range,
155 int na, int ns, int pna)
157 u32 result[OF_MAX_ADDR_CELLS];
161 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
165 if (of_out_of_range(addr, range, range + na + pna, na, ns))
168 /* Start with the parent range base. */
169 memcpy(result, range + na, pna * 4);
171 /* Add in the child address offset. */
172 for (i = 0; i < na; i++)
173 result[pna - 1 - i] +=
177 memcpy(addr, result, pna * 4);
182 static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
186 return IORESOURCE_MEM;
190 * PCI bus specific translator
193 static int of_bus_pci_match(struct device_node *np)
195 if (!strcmp(np->name, "pci")) {
196 const char *model = of_get_property(np, "model", NULL);
198 if (model && !strcmp(model, "SUNW,simba"))
201 /* Do not do PCI specific frobbing if the
202 * PCI bridge lacks a ranges property. We
203 * want to pass it through up to the next
204 * parent as-is, not with the PCI translate
205 * method which chops off the top address cell.
207 if (!of_find_property(np, "ranges", NULL))
216 static int of_bus_simba_match(struct device_node *np)
218 const char *model = of_get_property(np, "model", NULL);
220 if (model && !strcmp(model, "SUNW,simba"))
223 /* Treat PCI busses lacking ranges property just like
226 if (!strcmp(np->name, "pci")) {
227 if (!of_find_property(np, "ranges", NULL))
234 static int of_bus_simba_map(u32 *addr, const u32 *range,
235 int na, int ns, int pna)
240 static void of_bus_pci_count_cells(struct device_node *np,
241 int *addrc, int *sizec)
249 static int of_bus_pci_map(u32 *addr, const u32 *range,
250 int na, int ns, int pna)
252 u32 result[OF_MAX_ADDR_CELLS];
255 /* Check address type match */
256 if ((addr[0] ^ range[0]) & 0x03000000)
259 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
263 /* Start with the parent range base. */
264 memcpy(result, range + na, pna * 4);
266 /* Add in the child address offset, skipping high cell. */
267 for (i = 0; i < na - 1; i++)
268 result[pna - 1 - i] +=
272 memcpy(addr, result, pna * 4);
277 static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
281 /* For PCI, we override whatever child busses may have used. */
283 switch((w >> 24) & 0x03) {
285 flags |= IORESOURCE_IO;
288 case 0x02: /* 32 bits */
289 case 0x03: /* 64 bits */
290 flags |= IORESOURCE_MEM;
294 flags |= IORESOURCE_PREFETCH;
299 * SBUS bus specific translator
302 static int of_bus_sbus_match(struct device_node *np)
304 return !strcmp(np->name, "sbus") ||
305 !strcmp(np->name, "sbi");
308 static void of_bus_sbus_count_cells(struct device_node *child,
309 int *addrc, int *sizec)
318 * FHC/Central bus specific translator.
320 * This is just needed to hard-code the address and size cell
321 * counts. 'fhc' and 'central' nodes lack the #address-cells and
322 * #size-cells properties, and if you walk to the root on such
323 * Enterprise boxes all you'll get is a #size-cells of 2 which is
324 * not what we want to use.
326 static int of_bus_fhc_match(struct device_node *np)
328 return !strcmp(np->name, "fhc") ||
329 !strcmp(np->name, "central");
332 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
335 * Array of bus specific translators
338 static struct of_bus of_busses[] = {
342 .addr_prop_name = "assigned-addresses",
343 .match = of_bus_pci_match,
344 .count_cells = of_bus_pci_count_cells,
345 .map = of_bus_pci_map,
346 .get_flags = of_bus_pci_get_flags,
351 .addr_prop_name = "assigned-addresses",
352 .match = of_bus_simba_match,
353 .count_cells = of_bus_pci_count_cells,
354 .map = of_bus_simba_map,
355 .get_flags = of_bus_pci_get_flags,
360 .addr_prop_name = "reg",
361 .match = of_bus_sbus_match,
362 .count_cells = of_bus_sbus_count_cells,
363 .map = of_bus_default_map,
364 .get_flags = of_bus_default_get_flags,
369 .addr_prop_name = "reg",
370 .match = of_bus_fhc_match,
371 .count_cells = of_bus_fhc_count_cells,
372 .map = of_bus_default_map,
373 .get_flags = of_bus_default_get_flags,
378 .addr_prop_name = "reg",
380 .count_cells = of_bus_default_count_cells,
381 .map = of_bus_default_map,
382 .get_flags = of_bus_default_get_flags,
386 static struct of_bus *of_match_bus(struct device_node *np)
390 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
391 if (!of_busses[i].match || of_busses[i].match(np))
392 return &of_busses[i];
397 static int __init build_one_resource(struct device_node *parent,
401 int na, int ns, int pna)
406 ranges = of_get_property(parent, "ranges", &rlen);
407 if (ranges == NULL || rlen == 0) {
408 u32 result[OF_MAX_ADDR_CELLS];
411 memset(result, 0, pna * 4);
412 for (i = 0; i < na; i++)
413 result[pna - 1 - i] =
416 memcpy(addr, result, pna * 4);
420 /* Now walk through the ranges */
422 rone = na + pna + ns;
423 for (; rlen >= rone; rlen -= rone, ranges += rone) {
424 if (!bus->map(addr, ranges, na, ns, pna))
428 /* When we miss an I/O space match on PCI, just pass it up
429 * to the next PCI bridge and/or controller.
431 if (!strcmp(bus->name, "pci") &&
432 (addr[0] & 0x03000000) == 0x01000000)
438 static int __init use_1to1_mapping(struct device_node *pp)
440 /* If we have a ranges property in the parent, use it. */
441 if (of_find_property(pp, "ranges", NULL) != NULL)
444 /* If the parent is the dma node of an ISA bus, pass
445 * the translation up to the root.
447 * Some SBUS devices use intermediate nodes to express
448 * hierarchy within the device itself. These aren't
449 * real bus nodes, and don't have a 'ranges' property.
450 * But, we should still pass the translation work up
451 * to the SBUS itself.
453 if (!strcmp(pp->name, "dma") ||
454 !strcmp(pp->name, "espdma") ||
455 !strcmp(pp->name, "ledma") ||
456 !strcmp(pp->name, "lebuffer"))
459 /* Similarly for all PCI bridges, if we get this far
460 * it lacks a ranges property, and this will include
463 if (!strcmp(pp->name, "pci"))
469 static int of_resource_verbose;
471 static void __init build_device_resources(struct of_device *op,
472 struct device *parent)
474 struct of_device *p_op;
483 p_op = to_of_device(parent);
484 bus = of_match_bus(p_op->node);
485 bus->count_cells(op->node, &na, &ns);
487 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
488 if (!preg || num_reg == 0)
491 /* Convert to num-cells. */
494 /* Convert to num-entries. */
497 /* Prevent overrunning the op->resources[] array. */
498 if (num_reg > PROMREG_MAX) {
499 printk(KERN_WARNING "%s: Too many regs (%d), "
501 op->node->full_name, num_reg, PROMREG_MAX);
502 num_reg = PROMREG_MAX;
505 for (index = 0; index < num_reg; index++) {
506 struct resource *r = &op->resource[index];
507 u32 addr[OF_MAX_ADDR_CELLS];
508 const u32 *reg = (preg + (index * ((na + ns) * 4)));
509 struct device_node *dp = op->node;
510 struct device_node *pp = p_op->node;
511 struct of_bus *pbus, *dbus;
512 u64 size, result = OF_BAD_ADDR;
517 size = of_read_addr(reg + na, ns);
518 memcpy(addr, reg, na * 4);
520 flags = bus->get_flags(addr, 0);
522 if (use_1to1_mapping(pp)) {
523 result = of_read_addr(addr, na);
535 result = of_read_addr(addr, dna);
539 pbus = of_match_bus(pp);
540 pbus->count_cells(dp, &pna, &pns);
542 if (build_one_resource(dp, dbus, pbus, addr,
546 flags = pbus->get_flags(addr, flags);
554 memset(r, 0, sizeof(*r));
556 if (of_resource_verbose)
557 printk("%s reg[%d] -> %lx\n",
558 op->node->full_name, index,
561 if (result != OF_BAD_ADDR) {
562 if (tlb_type == hypervisor)
563 result &= 0x0fffffffffffffffUL;
566 r->end = result + size - 1;
569 r->name = op->node->name;
573 static struct device_node * __init
574 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
575 const u32 *imap, int imlen, const u32 *imask,
578 struct device_node *cp;
579 unsigned int irq = *irq_p;
585 bus = of_match_bus(pp);
586 bus->count_cells(dp, &na, NULL);
588 reg = of_get_property(dp, "reg", &num_reg);
589 if (!reg || !num_reg)
592 imlen /= ((na + 3) * 4);
594 for (i = 0; i < imlen; i++) {
597 for (j = 0; j < na; j++) {
598 if ((reg[j] & imask[j]) != imap[j])
601 if (imap[na] == irq) {
602 handle = imap[na + 1];
611 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
612 * properties that do not include the on-board device
613 * interrupts. Instead, the device's 'interrupts' property
614 * is already a fully specified INO value.
616 * Handle this by deciding that, if we didn't get a
617 * match in the parent's 'interrupt-map', and the
618 * parent is an IRQ translater, then use the parent as
619 * our IRQ controller.
628 cp = of_find_node_by_phandle(handle);
633 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
634 struct device_node *pp,
637 const struct linux_prom_pci_registers *regs;
638 unsigned int bus, devfn, slot, ret;
640 if (irq < 1 || irq > 4)
643 regs = of_get_property(dp, "reg", NULL);
647 bus = (regs->phys_hi >> 16) & 0xff;
648 devfn = (regs->phys_hi >> 8) & 0xff;
649 slot = (devfn >> 3) & 0x1f;
652 /* Derived from Table 8-3, U2P User's Manual. This branch
653 * is handling a PCI controller that lacks a proper set of
654 * interrupt-map and interrupt-map-mask properties. The
655 * Ultra-E450 is one example.
657 * The bit layout is BSSLL, where:
658 * B: 0 on bus A, 1 on bus B
659 * D: 2-bit slot number, derived from PCI device number as
660 * (dev - 1) for bus A, or (dev - 2) for bus B
661 * L: 2-bit line number
666 slot = (slot - 1) << 2;
670 slot = (slot - 2) << 2;
674 ret = (bus | slot | irq);
676 /* Going through a PCI-PCI bridge that lacks a set of
677 * interrupt-map and interrupt-map-mask properties.
679 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
685 static int of_irq_verbose;
687 static unsigned int __init build_one_device_irq(struct of_device *op,
688 struct device *parent,
691 struct device_node *dp = op->node;
692 struct device_node *pp, *ip;
693 unsigned int orig_irq = irq;
696 if (irq == 0xffffffff)
700 irq = dp->irq_trans->irq_build(dp, irq,
701 dp->irq_trans->data);
704 printk("%s: direct translate %x --> %x\n",
705 dp->full_name, orig_irq, irq);
710 /* Something more complicated. Walk up to the root, applying
711 * interrupt-map or bus specific translations, until we hit
714 * If we hit a bus type or situation we cannot handle, we
715 * stop and assume that the original IRQ number was in a
716 * format which has special meaning to it's immediate parent.
721 const void *imap, *imsk;
724 imap = of_get_property(pp, "interrupt-map", &imlen);
725 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
727 struct device_node *iret;
728 int this_orig_irq = irq;
730 iret = apply_interrupt_map(dp, pp,
735 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
737 pp->full_name, this_orig_irq,
738 (iret ? iret->full_name : "NULL"), irq);
743 if (iret->irq_trans) {
748 if (!strcmp(pp->name, "pci")) {
749 unsigned int this_orig_irq = irq;
751 irq = pci_irq_swizzle(dp, pp, irq);
753 printk("%s: PCI swizzle [%s] "
756 pp->full_name, this_orig_irq,
772 irq = ip->irq_trans->irq_build(op->node, irq,
773 ip->irq_trans->data);
775 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
776 op->node->full_name, ip->full_name, orig_irq, irq);
779 nid = of_node_to_nid(dp);
781 cpumask_t numa_mask = node_to_cpumask(nid);
783 irq_set_affinity(irq, numa_mask);
789 static struct of_device * __init scan_one_device(struct device_node *dp,
790 struct device *parent)
792 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
793 const unsigned int *irq;
794 struct dev_archdata *sd;
800 sd = &op->dev.archdata;
806 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
808 op->portid = of_getintprop_default(dp, "upa-portid", -1);
809 if (op->portid == -1)
810 op->portid = of_getintprop_default(dp, "portid", -1);
812 irq = of_get_property(dp, "interrupts", &len);
814 memcpy(op->irqs, irq, len);
815 op->num_irqs = len / 4;
820 /* Prevent overrunning the op->irqs[] array. */
821 if (op->num_irqs > PROMINTR_MAX) {
822 printk(KERN_WARNING "%s: Too many irqs (%d), "
824 dp->full_name, op->num_irqs, PROMINTR_MAX);
825 op->num_irqs = PROMINTR_MAX;
828 build_device_resources(op, parent);
829 for (i = 0; i < op->num_irqs; i++)
830 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
832 op->dev.parent = parent;
833 op->dev.bus = &of_platform_bus_type;
835 dev_set_name(&op->dev, "root");
837 dev_set_name(&op->dev, "%08x", dp->node);
839 if (of_device_register(op)) {
840 printk("%s: Could not register of device.\n",
849 static void __init scan_tree(struct device_node *dp, struct device *parent)
852 struct of_device *op = scan_one_device(dp, parent);
855 scan_tree(dp->child, &op->dev);
861 static void __init scan_of_devices(void)
863 struct device_node *root = of_find_node_by_path("/");
864 struct of_device *parent;
866 parent = scan_one_device(root, NULL);
870 scan_tree(root->child, &parent->dev);
873 static int __init of_bus_driver_init(void)
877 err = of_bus_type_init(&of_platform_bus_type, "of");
884 postcore_initcall(of_bus_driver_init);
886 static int __init of_debug(char *str)
890 get_option(&str, &val);
892 of_resource_verbose = 1;
898 __setup("of_debug=", of_debug);