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>
9 #include <linux/of_device.h>
10 #include <linux/of_platform.h>
12 static int node_match(struct device *dev, void *data)
14 struct of_device *op = to_of_device(dev);
15 struct device_node *dp = data;
17 return (op->node == dp);
20 struct of_device *of_find_device_by_node(struct device_node *dp)
22 struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
26 return to_of_device(dev);
30 EXPORT_SYMBOL(of_find_device_by_node);
33 struct bus_type ebus_bus_type;
34 EXPORT_SYMBOL(ebus_bus_type);
38 struct bus_type sbus_bus_type;
39 EXPORT_SYMBOL(sbus_bus_type);
42 struct bus_type of_platform_bus_type;
43 EXPORT_SYMBOL(of_platform_bus_type);
45 static inline u64 of_read_addr(const u32 *cell, int size)
49 r = (r << 32) | *(cell++);
53 static void __init get_cells(struct device_node *dp,
54 int *addrc, int *sizec)
57 *addrc = of_n_addr_cells(dp);
59 *sizec = of_n_size_cells(dp);
62 /* Max address size we deal with */
63 #define OF_MAX_ADDR_CELLS 4
67 const char *addr_prop_name;
68 int (*match)(struct device_node *parent);
69 void (*count_cells)(struct device_node *child,
70 int *addrc, int *sizec);
71 int (*map)(u32 *addr, const u32 *range,
72 int na, int ns, int pna);
73 unsigned long (*get_flags)(const u32 *addr, unsigned long);
77 * Default translator (generic bus)
80 static void of_bus_default_count_cells(struct device_node *dev,
81 int *addrc, int *sizec)
83 get_cells(dev, addrc, sizec);
86 /* Make sure the least significant 64-bits are in-range. Even
87 * for 3 or 4 cell values it is a good enough approximation.
89 static int of_out_of_range(const u32 *addr, const u32 *base,
90 const u32 *size, int na, int ns)
92 u64 a = of_read_addr(addr, na);
93 u64 b = of_read_addr(base, na);
98 b += of_read_addr(size, ns);
105 static int of_bus_default_map(u32 *addr, const u32 *range,
106 int na, int ns, int pna)
108 u32 result[OF_MAX_ADDR_CELLS];
112 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
116 if (of_out_of_range(addr, range, range + na + pna, na, ns))
119 /* Start with the parent range base. */
120 memcpy(result, range + na, pna * 4);
122 /* Add in the child address offset. */
123 for (i = 0; i < na; i++)
124 result[pna - 1 - i] +=
128 memcpy(addr, result, pna * 4);
133 static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
137 return IORESOURCE_MEM;
141 * PCI bus specific translator
144 static int of_bus_pci_match(struct device_node *np)
146 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
147 /* Do not do PCI specific frobbing if the
148 * PCI bridge lacks a ranges property. We
149 * want to pass it through up to the next
150 * parent as-is, not with the PCI translate
151 * method which chops off the top address cell.
153 if (!of_find_property(np, "ranges", NULL))
162 static void of_bus_pci_count_cells(struct device_node *np,
163 int *addrc, int *sizec)
171 static int of_bus_pci_map(u32 *addr, const u32 *range,
172 int na, int ns, int pna)
174 u32 result[OF_MAX_ADDR_CELLS];
177 /* Check address type match */
178 if ((addr[0] ^ range[0]) & 0x03000000)
181 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
185 /* Start with the parent range base. */
186 memcpy(result, range + na, pna * 4);
188 /* Add in the child address offset, skipping high cell. */
189 for (i = 0; i < na - 1; i++)
190 result[pna - 1 - i] +=
194 memcpy(addr, result, pna * 4);
199 static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
203 /* For PCI, we override whatever child busses may have used. */
205 switch((w >> 24) & 0x03) {
207 flags |= IORESOURCE_IO;
210 case 0x02: /* 32 bits */
211 case 0x03: /* 64 bits */
212 flags |= IORESOURCE_MEM;
216 flags |= IORESOURCE_PREFETCH;
221 * SBUS bus specific translator
224 static int of_bus_sbus_match(struct device_node *np)
226 return !strcmp(np->name, "sbus") ||
227 !strcmp(np->name, "sbi");
230 static void of_bus_sbus_count_cells(struct device_node *child,
231 int *addrc, int *sizec)
239 static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
241 return of_bus_default_map(addr, range, na, ns, pna);
244 static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
246 return IORESOURCE_MEM;
251 * Array of bus specific translators
254 static struct of_bus of_busses[] = {
258 .addr_prop_name = "assigned-addresses",
259 .match = of_bus_pci_match,
260 .count_cells = of_bus_pci_count_cells,
261 .map = of_bus_pci_map,
262 .get_flags = of_bus_pci_get_flags,
267 .addr_prop_name = "reg",
268 .match = of_bus_sbus_match,
269 .count_cells = of_bus_sbus_count_cells,
270 .map = of_bus_sbus_map,
271 .get_flags = of_bus_sbus_get_flags,
276 .addr_prop_name = "reg",
278 .count_cells = of_bus_default_count_cells,
279 .map = of_bus_default_map,
280 .get_flags = of_bus_default_get_flags,
284 static struct of_bus *of_match_bus(struct device_node *np)
288 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
289 if (!of_busses[i].match || of_busses[i].match(np))
290 return &of_busses[i];
295 static int __init build_one_resource(struct device_node *parent,
299 int na, int ns, int pna)
305 ranges = of_get_property(parent, "ranges", &rlen);
306 if (ranges == NULL || rlen == 0) {
307 u32 result[OF_MAX_ADDR_CELLS];
310 memset(result, 0, pna * 4);
311 for (i = 0; i < na; i++)
312 result[pna - 1 - i] =
315 memcpy(addr, result, pna * 4);
319 /* Now walk through the ranges */
321 rone = na + pna + ns;
322 for (; rlen >= rone; rlen -= rone, ranges += rone) {
323 if (!bus->map(addr, ranges, na, ns, pna))
330 static int of_resource_verbose;
332 static void __init build_device_resources(struct of_device *op,
333 struct device *parent)
335 struct of_device *p_op;
344 p_op = to_of_device(parent);
345 bus = of_match_bus(p_op->node);
346 bus->count_cells(op->node, &na, &ns);
348 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
349 if (!preg || num_reg == 0)
352 /* Convert to num-cells. */
355 /* Conver to num-entries. */
358 for (index = 0; index < num_reg; index++) {
359 struct resource *r = &op->resource[index];
360 u32 addr[OF_MAX_ADDR_CELLS];
361 const u32 *reg = (preg + (index * ((na + ns) * 4)));
362 struct device_node *dp = op->node;
363 struct device_node *pp = p_op->node;
364 struct of_bus *pbus, *dbus;
365 u64 size, result = OF_BAD_ADDR;
370 size = of_read_addr(reg + na, ns);
372 memcpy(addr, reg, na * 4);
374 flags = bus->get_flags(reg, 0);
376 /* If the immediate parent has no ranges property to apply,
377 * just use a 1<->1 mapping.
379 if (of_find_property(pp, "ranges", NULL) == NULL) {
380 result = of_read_addr(addr, na);
392 result = of_read_addr(addr, dna);
396 pbus = of_match_bus(pp);
397 pbus->count_cells(dp, &pna, &pns);
399 if (build_one_resource(dp, dbus, pbus, addr,
403 flags = pbus->get_flags(addr, flags);
411 memset(r, 0, sizeof(*r));
413 if (of_resource_verbose)
414 printk("%s reg[%d] -> %llx\n",
415 op->node->full_name, index,
418 if (result != OF_BAD_ADDR) {
419 r->start = result & 0xffffffff;
420 r->end = result + size - 1;
421 r->flags = flags | ((result >> 32ULL) & 0xffUL);
423 r->name = op->node->name;
427 static struct of_device * __init scan_one_device(struct device_node *dp,
428 struct device *parent)
430 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
431 const struct linux_prom_irqs *intr;
432 struct dev_archdata *sd;
438 sd = &op->dev.archdata;
444 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
446 op->portid = of_getintprop_default(dp, "upa-portid", -1);
447 if (op->portid == -1)
448 op->portid = of_getintprop_default(dp, "portid", -1);
450 intr = of_get_property(dp, "intr", &len);
452 op->num_irqs = len / sizeof(struct linux_prom_irqs);
453 for (i = 0; i < op->num_irqs; i++)
454 op->irqs[i] = intr[i].pri;
456 const unsigned int *irq =
457 of_get_property(dp, "interrupts", &len);
460 op->num_irqs = len / sizeof(unsigned int);
461 for (i = 0; i < op->num_irqs; i++)
462 op->irqs[i] = irq[i];
467 if (sparc_cpu_model == sun4d) {
468 static int pil_to_sbus[] = {
469 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
471 struct device_node *io_unit, *sbi = dp->parent;
472 const struct linux_prom_registers *regs;
476 if (!strcmp(sbi->name, "sbi"))
482 goto build_resources;
484 regs = of_get_property(dp, "reg", NULL);
486 goto build_resources;
488 slot = regs->which_io;
490 /* If SBI's parent is not io-unit or the io-unit lacks
491 * a "board#" property, something is very wrong.
493 if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
494 printk("%s: Error, parent is not io-unit.\n",
496 goto build_resources;
498 io_unit = sbi->parent;
499 board = of_getintprop_default(io_unit, "board#", -1);
501 printk("%s: Error, lacks board# property.\n",
503 goto build_resources;
506 for (i = 0; i < op->num_irqs; i++) {
507 int this_irq = op->irqs[i];
508 int sbusl = pil_to_sbus[this_irq];
511 this_irq = (((board + 1) << 5) +
515 op->irqs[i] = this_irq;
520 build_device_resources(op, parent);
522 op->dev.parent = parent;
523 op->dev.bus = &of_platform_bus_type;
525 strcpy(op->dev.bus_id, "root");
527 sprintf(op->dev.bus_id, "%08x", dp->node);
529 if (of_device_register(op)) {
530 printk("%s: Could not register of device.\n",
539 static void __init scan_tree(struct device_node *dp, struct device *parent)
542 struct of_device *op = scan_one_device(dp, parent);
545 scan_tree(dp->child, &op->dev);
551 static void __init scan_of_devices(void)
553 struct device_node *root = of_find_node_by_path("/");
554 struct of_device *parent;
556 parent = scan_one_device(root, NULL);
560 scan_tree(root->child, &parent->dev);
563 static int __init of_bus_driver_init(void)
567 err = of_bus_type_init(&of_platform_bus_type, "of");
570 err = of_bus_type_init(&ebus_bus_type, "ebus");
574 err = of_bus_type_init(&sbus_bus_type, "sbus");
583 postcore_initcall(of_bus_driver_init);
585 static int __init of_debug(char *str)
589 get_option(&str, &val);
591 of_resource_verbose = 1;
595 __setup("of_debug=", of_debug);