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 sparc32 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 /* use when traversing tree through the allnext, child, sibling,
31 * or parent members of struct device_node.
33 static DEFINE_RWLOCK(devtree_lock);
35 struct device_node *of_get_parent(const struct device_node *node)
37 struct device_node *np;
46 EXPORT_SYMBOL(of_get_parent);
48 struct device_node *of_get_next_child(const struct device_node *node,
49 struct device_node *prev)
51 struct device_node *next;
53 next = prev ? prev->sibling : node->child;
54 for (; next != 0; next = next->sibling) {
60 EXPORT_SYMBOL(of_get_next_child);
62 struct device_node *of_find_node_by_path(const char *path)
64 struct device_node *np = allnodes;
66 for (; np != 0; np = np->allnext) {
67 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
73 EXPORT_SYMBOL(of_find_node_by_path);
75 struct device_node *of_find_node_by_phandle(phandle handle)
77 struct device_node *np;
79 for (np = allnodes; np != 0; np = np->allnext)
80 if (np->node == handle)
85 EXPORT_SYMBOL(of_find_node_by_phandle);
87 struct device_node *of_find_node_by_name(struct device_node *from,
90 struct device_node *np;
92 np = from ? from->allnext : allnodes;
93 for (; np != NULL; np = np->allnext)
94 if (np->name != NULL && strcmp(np->name, name) == 0)
99 EXPORT_SYMBOL(of_find_node_by_name);
101 struct device_node *of_find_node_by_type(struct device_node *from,
104 struct device_node *np;
106 np = from ? from->allnext : allnodes;
107 for (; np != 0; np = np->allnext)
108 if (np->type != 0 && strcmp(np->type, type) == 0)
113 EXPORT_SYMBOL(of_find_node_by_type);
115 struct device_node *of_find_compatible_node(struct device_node *from,
116 const char *type, const char *compatible)
118 struct device_node *np;
120 np = from ? from->allnext : allnodes;
121 for (; np != 0; np = np->allnext) {
123 && !(np->type != 0 && strcmp(np->type, type) == 0))
125 if (of_device_is_compatible(np, compatible))
131 EXPORT_SYMBOL(of_find_compatible_node);
133 struct property *of_find_property(const struct device_node *np,
139 for (pp = np->properties; pp != 0; pp = pp->next) {
140 if (strcasecmp(pp->name, name) == 0) {
148 EXPORT_SYMBOL(of_find_property);
150 int of_getintprop_default(struct device_node *np, const char *name, int def)
152 struct property *prop;
155 prop = of_find_property(np, name, &len);
156 if (!prop || len != 4)
159 return *(int *) prop->value;
161 EXPORT_SYMBOL(of_getintprop_default);
163 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
165 struct property **prevp;
169 new_val = kmalloc(len, GFP_KERNEL);
173 memcpy(new_val, val, len);
177 write_lock(&devtree_lock);
178 prevp = &dp->properties;
180 struct property *prop = *prevp;
182 if (!strcasecmp(prop->name, name)) {
183 void *old_val = prop->value;
186 ret = prom_setprop(dp->node, (char *) name, val, len);
189 prop->value = new_val;
192 if (OF_IS_DYNAMIC(prop))
195 OF_MARK_DYNAMIC(prop);
201 prevp = &(*prevp)->next;
203 write_unlock(&devtree_lock);
205 /* XXX Upate procfs if necessary... */
209 EXPORT_SYMBOL(of_set_property);
211 static unsigned int prom_early_allocated;
213 static void * __init prom_early_alloc(unsigned long size)
217 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
219 memset(ret, 0, size);
221 prom_early_allocated += size;
226 static int is_root_node(const struct device_node *dp)
231 return (dp->parent == NULL);
234 /* The following routines deal with the black magic of fully naming a
237 * Certain well known named nodes are just the simple name string.
239 * Actual devices have an address specifier appended to the base name
240 * string, like this "foo@addr". The "addr" can be in any number of
241 * formats, and the platform plus the type of the node determine the
242 * format and how it is constructed.
244 * For children of the ROOT node, the naming convention is fixed and
245 * determined by whether this is a sun4u or sun4v system.
247 * For children of other nodes, it is bus type specific. So
248 * we walk up the tree until we discover a "device_type" property
249 * we recognize and we go from there.
251 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
253 struct linux_prom_registers *regs;
254 struct property *rprop;
256 rprop = of_find_property(dp, "reg", NULL);
261 sprintf(tmp_buf, "%s@%x,%x",
263 regs->which_io, regs->phys_addr);
266 /* "name@slot,offset" */
267 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
269 struct linux_prom_registers *regs;
270 struct property *prop;
272 prop = of_find_property(dp, "reg", NULL);
277 sprintf(tmp_buf, "%s@%x,%x",
283 /* "name@devnum[,func]" */
284 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
286 struct linux_prom_pci_registers *regs;
287 struct property *prop;
290 prop = of_find_property(dp, "reg", NULL);
295 devfn = (regs->phys_hi >> 8) & 0xff;
297 sprintf(tmp_buf, "%s@%x,%x",
302 sprintf(tmp_buf, "%s@%x",
308 /* "name@addrhi,addrlo" */
309 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
311 struct linux_prom_registers *regs;
312 struct property *prop;
314 prop = of_find_property(dp, "reg", NULL);
320 sprintf(tmp_buf, "%s@%x,%x",
322 regs->which_io, regs->phys_addr);
325 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
327 struct device_node *parent = dp->parent;
329 if (parent != NULL) {
330 if (!strcmp(parent->type, "pci") ||
331 !strcmp(parent->type, "pciex"))
332 return pci_path_component(dp, tmp_buf);
333 if (!strcmp(parent->type, "sbus"))
334 return sbus_path_component(dp, tmp_buf);
335 if (!strcmp(parent->type, "ebus"))
336 return ebus_path_component(dp, tmp_buf);
338 /* "isa" is handled with platform naming */
341 /* Use platform naming convention. */
342 return sparc32_path_component(dp, tmp_buf);
345 static char * __init build_path_component(struct device_node *dp)
347 char tmp_buf[64], *n;
350 __build_path_component(dp, tmp_buf);
351 if (tmp_buf[0] == '\0')
352 strcpy(tmp_buf, dp->name);
354 n = prom_early_alloc(strlen(tmp_buf) + 1);
360 static char * __init build_full_name(struct device_node *dp)
362 int len, ourlen, plen;
365 plen = strlen(dp->parent->full_name);
366 ourlen = strlen(dp->path_component_name);
367 len = ourlen + plen + 2;
369 n = prom_early_alloc(len);
370 strcpy(n, dp->parent->full_name);
371 if (!is_root_node(dp->parent)) {
372 strcpy(n + plen, "/");
375 strcpy(n + plen, dp->path_component_name);
380 static unsigned int unique_id;
382 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
384 static struct property *tmp = NULL;
391 memset(p, 0, sizeof(*p) + 32);
394 p = prom_early_alloc(sizeof(struct property) + 32);
395 p->unique_id = unique_id++;
398 p->name = (char *) (p + 1);
400 strcpy(p->name, special_name);
401 p->length = special_len;
402 p->value = prom_early_alloc(special_len);
403 memcpy(p->value, special_val, special_len);
406 name = prom_firstprop(node, NULL);
408 name = prom_nextprop(node, prev, NULL);
410 if (strlen(name) == 0) {
414 strcpy(p->name, name);
415 p->length = prom_getproplen(node, p->name);
416 if (p->length <= 0) {
419 p->value = prom_early_alloc(p->length + 1);
420 len = prom_getproperty(node, p->name, p->value,
424 ((unsigned char *)p->value)[p->length] = '\0';
430 static struct property * __init build_prop_list(phandle node)
432 struct property *head, *tail;
434 head = tail = build_one_prop(node, NULL,
435 ".node", &node, sizeof(node));
437 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
440 tail->next = build_one_prop(node, tail->name,
448 static char * __init get_one_property(phandle node, char *name)
450 char *buf = "<NULL>";
453 len = prom_getproplen(node, name);
455 buf = prom_early_alloc(len);
456 len = prom_getproperty(node, name, buf, len);
462 static struct device_node * __init create_node(phandle node)
464 struct device_node *dp;
469 dp = prom_early_alloc(sizeof(*dp));
470 dp->unique_id = unique_id++;
472 kref_init(&dp->kref);
474 dp->name = get_one_property(node, "name");
475 dp->type = get_one_property(node, "device_type");
478 /* Build interrupts later... */
480 dp->properties = build_prop_list(node);
485 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
487 struct device_node *dp;
489 dp = create_node(node);
492 *nextp = &dp->allnext;
495 dp->path_component_name = build_path_component(dp);
496 dp->full_name = build_full_name(dp);
498 dp->child = build_tree(dp, prom_getchild(node), nextp);
500 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
506 void __init prom_build_devicetree(void)
508 struct device_node **nextp;
510 allnodes = create_node(prom_root_node);
511 allnodes->path_component_name = "";
512 allnodes->full_name = "/";
514 nextp = &allnodes->allnext;
515 allnodes->child = build_tree(allnodes,
516 prom_getchild(allnodes->node),
518 printk("PROM: Built device tree with %u bytes of memory.\n",
519 prom_early_allocated);