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 int of_device_is_compatible(struct device_node *device, const char *compat)
40 cp = (char *) of_get_property(device, "compatible", &cplen);
44 if (strncmp(cp, compat, strlen(compat)) == 0)
53 EXPORT_SYMBOL(of_device_is_compatible);
55 struct device_node *of_get_parent(const struct device_node *node)
57 struct device_node *np;
66 EXPORT_SYMBOL(of_get_parent);
68 struct device_node *of_get_next_child(const struct device_node *node,
69 struct device_node *prev)
71 struct device_node *next;
73 next = prev ? prev->sibling : node->child;
74 for (; next != 0; next = next->sibling) {
80 EXPORT_SYMBOL(of_get_next_child);
82 struct device_node *of_find_node_by_path(const char *path)
84 struct device_node *np = allnodes;
86 for (; np != 0; np = np->allnext) {
87 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
93 EXPORT_SYMBOL(of_find_node_by_path);
95 struct device_node *of_find_node_by_phandle(phandle handle)
97 struct device_node *np;
99 for (np = allnodes; np != 0; np = np->allnext)
100 if (np->node == handle)
105 EXPORT_SYMBOL(of_find_node_by_phandle);
107 struct device_node *of_find_node_by_name(struct device_node *from,
110 struct device_node *np;
112 np = from ? from->allnext : allnodes;
113 for (; np != NULL; np = np->allnext)
114 if (np->name != NULL && strcmp(np->name, name) == 0)
119 EXPORT_SYMBOL(of_find_node_by_name);
121 struct device_node *of_find_node_by_type(struct device_node *from,
124 struct device_node *np;
126 np = from ? from->allnext : allnodes;
127 for (; np != 0; np = np->allnext)
128 if (np->type != 0 && strcmp(np->type, type) == 0)
133 EXPORT_SYMBOL(of_find_node_by_type);
135 struct device_node *of_find_compatible_node(struct device_node *from,
136 const char *type, const char *compatible)
138 struct device_node *np;
140 np = from ? from->allnext : allnodes;
141 for (; np != 0; np = np->allnext) {
143 && !(np->type != 0 && strcmp(np->type, type) == 0))
145 if (of_device_is_compatible(np, compatible))
151 EXPORT_SYMBOL(of_find_compatible_node);
153 struct property *of_find_property(struct device_node *np, const char *name,
158 for (pp = np->properties; pp != 0; pp = pp->next) {
159 if (strcmp(pp->name, name) == 0) {
167 EXPORT_SYMBOL(of_find_property);
170 * Find a property with a given name for a given node
171 * and return the value.
173 void *of_get_property(struct device_node *np, const char *name, int *lenp)
175 struct property *pp = of_find_property(np,name,lenp);
176 return pp ? pp->value : NULL;
178 EXPORT_SYMBOL(of_get_property);
180 int of_getintprop_default(struct device_node *np, const char *name, int def)
182 struct property *prop;
185 prop = of_find_property(np, name, &len);
186 if (!prop || len != 4)
189 return *(int *) prop->value;
191 EXPORT_SYMBOL(of_getintprop_default);
193 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
195 struct property **prevp;
199 new_val = kmalloc(len, GFP_KERNEL);
203 memcpy(new_val, val, len);
207 write_lock(&devtree_lock);
208 prevp = &dp->properties;
210 struct property *prop = *prevp;
212 if (!strcmp(prop->name, name)) {
213 void *old_val = prop->value;
216 ret = prom_setprop(dp->node, name, val, len);
219 prop->value = new_val;
222 if (OF_IS_DYNAMIC(prop))
225 OF_MARK_DYNAMIC(prop);
231 prevp = &(*prevp)->next;
233 write_unlock(&devtree_lock);
235 /* XXX Upate procfs if necessary... */
239 EXPORT_SYMBOL(of_set_property);
241 static unsigned int prom_early_allocated;
243 static void * __init prom_early_alloc(unsigned long size)
247 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
249 memset(ret, 0, size);
251 prom_early_allocated += size;
256 static int is_root_node(const struct device_node *dp)
261 return (dp->parent == NULL);
264 /* The following routines deal with the black magic of fully naming a
267 * Certain well known named nodes are just the simple name string.
269 * Actual devices have an address specifier appended to the base name
270 * string, like this "foo@addr". The "addr" can be in any number of
271 * formats, and the platform plus the type of the node determine the
272 * format and how it is constructed.
274 * For children of the ROOT node, the naming convention is fixed and
275 * determined by whether this is a sun4u or sun4v system.
277 * For children of other nodes, it is bus type specific. So
278 * we walk up the tree until we discover a "device_type" property
279 * we recognize and we go from there.
281 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
283 struct linux_prom_registers *regs;
284 struct property *rprop;
286 rprop = of_find_property(dp, "reg", NULL);
291 sprintf(tmp_buf, "%s@%x,%x",
293 regs->which_io, regs->phys_addr);
296 /* "name@slot,offset" */
297 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
299 struct linux_prom_registers *regs;
300 struct property *prop;
302 prop = of_find_property(dp, "reg", NULL);
307 sprintf(tmp_buf, "%s@%x,%x",
313 /* "name@devnum[,func]" */
314 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
316 struct linux_prom_pci_registers *regs;
317 struct property *prop;
320 prop = of_find_property(dp, "reg", NULL);
325 devfn = (regs->phys_hi >> 8) & 0xff;
327 sprintf(tmp_buf, "%s@%x,%x",
332 sprintf(tmp_buf, "%s@%x",
338 /* "name@addrhi,addrlo" */
339 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
341 struct linux_prom_registers *regs;
342 struct property *prop;
344 prop = of_find_property(dp, "reg", NULL);
350 sprintf(tmp_buf, "%s@%x,%x",
352 regs->which_io, regs->phys_addr);
355 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
357 struct device_node *parent = dp->parent;
359 if (parent != NULL) {
360 if (!strcmp(parent->type, "pci") ||
361 !strcmp(parent->type, "pciex"))
362 return pci_path_component(dp, tmp_buf);
363 if (!strcmp(parent->type, "sbus"))
364 return sbus_path_component(dp, tmp_buf);
365 if (!strcmp(parent->type, "ebus"))
366 return ebus_path_component(dp, tmp_buf);
368 /* "isa" is handled with platform naming */
371 /* Use platform naming convention. */
372 return sparc32_path_component(dp, tmp_buf);
375 static char * __init build_path_component(struct device_node *dp)
377 char tmp_buf[64], *n;
380 __build_path_component(dp, tmp_buf);
381 if (tmp_buf[0] == '\0')
382 strcpy(tmp_buf, dp->name);
384 n = prom_early_alloc(strlen(tmp_buf) + 1);
390 static char * __init build_full_name(struct device_node *dp)
392 int len, ourlen, plen;
395 plen = strlen(dp->parent->full_name);
396 ourlen = strlen(dp->path_component_name);
397 len = ourlen + plen + 2;
399 n = prom_early_alloc(len);
400 strcpy(n, dp->parent->full_name);
401 if (!is_root_node(dp->parent)) {
402 strcpy(n + plen, "/");
405 strcpy(n + plen, dp->path_component_name);
410 static unsigned int unique_id;
412 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
414 static struct property *tmp = NULL;
420 memset(p, 0, sizeof(*p) + 32);
423 p = prom_early_alloc(sizeof(struct property) + 32);
424 p->unique_id = unique_id++;
427 p->name = (char *) (p + 1);
429 p->length = special_len;
430 p->value = prom_early_alloc(special_len);
431 memcpy(p->value, special_val, special_len);
434 prom_firstprop(node, p->name);
436 prom_nextprop(node, prev, p->name);
438 if (strlen(p->name) == 0) {
442 p->length = prom_getproplen(node, p->name);
443 if (p->length <= 0) {
446 p->value = prom_early_alloc(p->length + 1);
447 prom_getproperty(node, p->name, p->value, p->length);
448 ((unsigned char *)p->value)[p->length] = '\0';
454 static struct property * __init build_prop_list(phandle node)
456 struct property *head, *tail;
458 head = tail = build_one_prop(node, NULL,
459 ".node", &node, sizeof(node));
461 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
464 tail->next = build_one_prop(node, tail->name,
472 static char * __init get_one_property(phandle node, char *name)
474 char *buf = "<NULL>";
477 len = prom_getproplen(node, name);
479 buf = prom_early_alloc(len);
480 len = prom_getproperty(node, name, buf, len);
486 static struct device_node * __init create_node(phandle node)
488 struct device_node *dp;
493 dp = prom_early_alloc(sizeof(*dp));
494 dp->unique_id = unique_id++;
496 kref_init(&dp->kref);
498 dp->name = get_one_property(node, "name");
499 dp->type = get_one_property(node, "device_type");
502 /* Build interrupts later... */
504 dp->properties = build_prop_list(node);
509 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
511 struct device_node *dp;
513 dp = create_node(node);
516 *nextp = &dp->allnext;
519 dp->path_component_name = build_path_component(dp);
520 dp->full_name = build_full_name(dp);
522 dp->child = build_tree(dp, prom_getchild(node), nextp);
524 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
530 void __init prom_build_devicetree(void)
532 struct device_node **nextp;
534 allnodes = create_node(prom_root_node);
535 allnodes->path_component_name = "";
536 allnodes->full_name = "/";
538 nextp = &allnodes->allnext;
539 allnodes->child = build_tree(allnodes,
540 prom_getchild(allnodes->node),
542 printk("PROM: Built device tree with %u bytes of memory.\n",
543 prom_early_allocated);