2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc64 by David S. Miller davem@davemloft.net
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
22 #include <linux/bootmem.h>
23 #include <linux/module.h>
26 #include <asm/oplib.h>
28 static struct device_node *allnodes;
30 struct device_node *of_get_parent(const struct device_node *node)
32 struct device_node *np;
42 struct device_node *of_get_next_child(const struct device_node *node,
43 struct device_node *prev)
45 struct device_node *next;
47 next = prev ? prev->sibling : node->child;
48 for (; next != 0; next = next->sibling) {
55 struct device_node *of_find_node_by_path(const char *path)
57 struct device_node *np = allnodes;
59 for (; np != 0; np = np->allnext) {
60 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
67 struct device_node *of_find_node_by_phandle(phandle handle)
69 struct device_node *np;
71 for (np = allnodes; np != 0; np = np->allnext)
72 if (np->node == handle)
78 struct device_node *of_find_node_by_name(struct device_node *from,
81 struct device_node *np;
83 np = from ? from->allnext : allnodes;
84 for (; np != NULL; np = np->allnext)
85 if (np->name != NULL && strcmp(np->name, name) == 0)
91 struct device_node *of_find_node_by_type(struct device_node *from,
94 struct device_node *np;
96 np = from ? from->allnext : allnodes;
97 for (; np != 0; np = np->allnext)
98 if (np->type != 0 && strcmp(np->type, type) == 0)
104 struct property *of_find_property(struct device_node *np, const char *name,
109 for (pp = np->properties; pp != 0; pp = pp->next) {
110 if (strcmp(pp->name, name) == 0) {
118 EXPORT_SYMBOL(of_find_property);
121 * Find a property with a given name for a given node
122 * and return the value.
124 void *of_get_property(struct device_node *np, const char *name, int *lenp)
126 struct property *pp = of_find_property(np,name,lenp);
127 return pp ? pp->value : NULL;
129 EXPORT_SYMBOL(of_get_property);
131 int of_getintprop_default(struct device_node *np, const char *name, int def)
133 struct property *prop;
136 prop = of_find_property(np, name, &len);
137 if (!prop || len != 4)
140 return *(int *) prop->value;
142 EXPORT_SYMBOL(of_getintprop_default);
144 static unsigned int prom_early_allocated;
146 static void * __init prom_early_alloc(unsigned long size)
150 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
152 memset(ret, 0, size);
154 prom_early_allocated += size;
159 static int is_root_node(const struct device_node *dp)
164 return (dp->parent == NULL);
167 /* The following routines deal with the black magic of fully naming a
170 * Certain well known named nodes are just the simple name string.
172 * Actual devices have an address specifier appended to the base name
173 * string, like this "foo@addr". The "addr" can be in any number of
174 * formats, and the platform plus the type of the node determine the
175 * format and how it is constructed.
177 * For children of the ROOT node, the naming convention is fixed and
178 * determined by whether this is a sun4u or sun4v system.
180 * For children of other nodes, it is bus type specific. So
181 * we walk up the tree until we discover a "device_type" property
182 * we recognize and we go from there.
184 * As an example, the boot device on my workstation has a full path:
186 * /pci@1e,600000/ide@d/disk@0,0:c
188 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
190 struct linux_prom64_registers *regs;
191 struct property *rprop;
192 u32 high_bits, low_bits, type;
194 rprop = of_find_property(dp, "reg", NULL);
199 if (!is_root_node(dp->parent)) {
200 sprintf(tmp_buf, "%s@%x,%x",
202 (unsigned int) (regs->phys_addr >> 32UL),
203 (unsigned int) (regs->phys_addr & 0xffffffffUL));
207 type = regs->phys_addr >> 60UL;
208 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
209 low_bits = (regs->phys_addr & 0xffffffffUL);
211 if (type == 0 || type == 8) {
212 const char *prefix = (type == 0) ? "m" : "i";
215 sprintf(tmp_buf, "%s@%s%x,%x",
217 high_bits, low_bits);
219 sprintf(tmp_buf, "%s@%s%x",
223 } else if (type == 12) {
224 sprintf(tmp_buf, "%s@%x",
225 dp->name, high_bits);
229 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
231 struct linux_prom64_registers *regs;
232 struct property *prop;
234 prop = of_find_property(dp, "reg", NULL);
239 if (!is_root_node(dp->parent)) {
240 sprintf(tmp_buf, "%s@%x,%x",
242 (unsigned int) (regs->phys_addr >> 32UL),
243 (unsigned int) (regs->phys_addr & 0xffffffffUL));
247 prop = of_find_property(dp, "upa-portid", NULL);
249 prop = of_find_property(dp, "portid", NULL);
251 unsigned long mask = 0xffffffffUL;
253 if (tlb_type >= cheetah)
256 sprintf(tmp_buf, "%s@%x,%x",
259 (unsigned int) (regs->phys_addr & mask));
263 /* "name@slot,offset" */
264 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
266 struct linux_prom_registers *regs;
267 struct property *prop;
269 prop = of_find_property(dp, "reg", NULL);
274 sprintf(tmp_buf, "%s@%x,%x",
280 /* "name@devnum[,func]" */
281 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
283 struct linux_prom_pci_registers *regs;
284 struct property *prop;
287 prop = of_find_property(dp, "reg", NULL);
292 devfn = (regs->phys_hi >> 8) & 0xff;
294 sprintf(tmp_buf, "%s@%x,%x",
299 sprintf(tmp_buf, "%s@%x",
305 /* "name@UPA_PORTID,offset" */
306 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
308 struct linux_prom64_registers *regs;
309 struct property *prop;
311 prop = of_find_property(dp, "reg", NULL);
317 prop = of_find_property(dp, "upa-portid", NULL);
321 sprintf(tmp_buf, "%s@%x,%x",
323 *(u32 *) prop->value,
324 (unsigned int) (regs->phys_addr & 0xffffffffUL));
328 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
330 struct property *prop;
333 prop = of_find_property(dp, "reg", NULL);
339 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
342 /* "name@addrhi,addrlo" */
343 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
345 struct linux_prom64_registers *regs;
346 struct property *prop;
348 prop = of_find_property(dp, "reg", NULL);
354 sprintf(tmp_buf, "%s@%x,%x",
356 (unsigned int) (regs->phys_addr >> 32UL),
357 (unsigned int) (regs->phys_addr & 0xffffffffUL));
360 /* "name@bus,addr" */
361 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
363 struct property *prop;
366 prop = of_find_property(dp, "reg", NULL);
372 /* This actually isn't right... should look at the #address-cells
373 * property of the i2c bus node etc. etc.
375 sprintf(tmp_buf, "%s@%x,%x",
376 dp->name, regs[0], regs[1]);
379 /* "name@reg0[,reg1]" */
380 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
382 struct property *prop;
385 prop = of_find_property(dp, "reg", NULL);
391 if (prop->length == sizeof(u32) || regs[1] == 1) {
392 sprintf(tmp_buf, "%s@%x",
395 sprintf(tmp_buf, "%s@%x,%x",
396 dp->name, regs[0], regs[1]);
400 /* "name@reg0reg1[,reg2reg3]" */
401 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
403 struct property *prop;
406 prop = of_find_property(dp, "reg", NULL);
412 if (regs[2] || regs[3]) {
413 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
414 dp->name, regs[0], regs[1], regs[2], regs[3]);
416 sprintf(tmp_buf, "%s@%08x%08x",
417 dp->name, regs[0], regs[1]);
421 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
423 struct device_node *parent = dp->parent;
425 if (parent != NULL) {
426 if (!strcmp(parent->type, "pci") ||
427 !strcmp(parent->type, "pciex"))
428 return pci_path_component(dp, tmp_buf);
429 if (!strcmp(parent->type, "sbus"))
430 return sbus_path_component(dp, tmp_buf);
431 if (!strcmp(parent->type, "upa"))
432 return upa_path_component(dp, tmp_buf);
433 if (!strcmp(parent->type, "ebus"))
434 return ebus_path_component(dp, tmp_buf);
435 if (!strcmp(parent->name, "usb") ||
436 !strcmp(parent->name, "hub"))
437 return usb_path_component(dp, tmp_buf);
438 if (!strcmp(parent->type, "i2c"))
439 return i2c_path_component(dp, tmp_buf);
440 if (!strcmp(parent->type, "firewire"))
441 return ieee1394_path_component(dp, tmp_buf);
442 if (!strcmp(parent->type, "virtual-devices"))
443 return vdev_path_component(dp, tmp_buf);
445 /* "isa" is handled with platform naming */
448 /* Use platform naming convention. */
449 if (tlb_type == hypervisor)
450 return sun4v_path_component(dp, tmp_buf);
452 return sun4u_path_component(dp, tmp_buf);
455 static char * __init build_path_component(struct device_node *dp)
457 char tmp_buf[64], *n;
460 __build_path_component(dp, tmp_buf);
461 if (tmp_buf[0] == '\0')
462 strcpy(tmp_buf, dp->name);
464 n = prom_early_alloc(strlen(tmp_buf) + 1);
470 static char * __init build_full_name(struct device_node *dp)
472 int len, ourlen, plen;
475 plen = strlen(dp->parent->full_name);
476 ourlen = strlen(dp->path_component_name);
477 len = ourlen + plen + 2;
479 n = prom_early_alloc(len);
480 strcpy(n, dp->parent->full_name);
481 if (!is_root_node(dp->parent)) {
482 strcpy(n + plen, "/");
485 strcpy(n + plen, dp->path_component_name);
490 static struct property * __init build_one_prop(phandle node, char *prev)
492 static struct property *tmp = NULL;
497 memset(p, 0, sizeof(*p) + 32);
500 p = prom_early_alloc(sizeof(struct property) + 32);
502 p->name = (char *) (p + 1);
504 prom_firstprop(node, p->name);
506 prom_nextprop(node, prev, p->name);
508 if (strlen(p->name) == 0) {
512 p->length = prom_getproplen(node, p->name);
513 if (p->length <= 0) {
516 p->value = prom_early_alloc(p->length);
517 prom_getproperty(node, p->name, p->value, p->length);
522 static struct property * __init build_prop_list(phandle node)
524 struct property *head, *tail;
526 head = tail = build_one_prop(node, NULL);
528 tail->next = build_one_prop(node, tail->name);
535 static char * __init get_one_property(phandle node, const char *name)
537 char *buf = "<NULL>";
540 len = prom_getproplen(node, name);
542 buf = prom_early_alloc(len);
543 prom_getproperty(node, name, buf, len);
549 static struct device_node * __init create_node(phandle node)
551 struct device_node *dp;
556 dp = prom_early_alloc(sizeof(*dp));
558 kref_init(&dp->kref);
560 dp->name = get_one_property(node, "name");
561 dp->type = get_one_property(node, "device_type");
564 /* Build interrupts later... */
566 dp->properties = build_prop_list(node);
571 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
573 struct device_node *dp;
575 dp = create_node(node);
578 *nextp = &dp->allnext;
581 dp->path_component_name = build_path_component(dp);
582 dp->full_name = build_full_name(dp);
584 dp->child = build_tree(dp, prom_getchild(node), nextp);
586 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
592 void __init prom_build_devicetree(void)
594 struct device_node **nextp;
596 allnodes = create_node(prom_root_node);
597 allnodes->path_component_name = "";
598 allnodes->full_name = "/";
600 nextp = &allnodes->allnext;
601 allnodes->child = build_tree(allnodes,
602 prom_getchild(allnodes->node),
604 printk("PROM: Built device tree with %u bytes of memory.\n",
605 prom_early_allocated);