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 extern rwlock_t devtree_lock; /* temporary while merging */
32 struct device_node *of_get_next_child(const struct device_node *node,
33 struct device_node *prev)
35 struct device_node *next;
37 next = prev ? prev->sibling : node->child;
38 for (; next != 0; next = next->sibling) {
44 EXPORT_SYMBOL(of_get_next_child);
46 struct device_node *of_find_node_by_path(const char *path)
48 struct device_node *np = allnodes;
50 for (; np != 0; np = np->allnext) {
51 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
57 EXPORT_SYMBOL(of_find_node_by_path);
59 struct device_node *of_find_node_by_phandle(phandle handle)
61 struct device_node *np;
63 for (np = allnodes; np != 0; np = np->allnext)
64 if (np->node == handle)
69 EXPORT_SYMBOL(of_find_node_by_phandle);
71 struct device_node *of_find_node_by_name(struct device_node *from,
74 struct device_node *np;
76 np = from ? from->allnext : allnodes;
77 for (; np != NULL; np = np->allnext)
78 if (np->name != NULL && strcmp(np->name, name) == 0)
83 EXPORT_SYMBOL(of_find_node_by_name);
85 struct device_node *of_find_node_by_type(struct device_node *from,
88 struct device_node *np;
90 np = from ? from->allnext : allnodes;
91 for (; np != 0; np = np->allnext)
92 if (np->type != 0 && strcmp(np->type, type) == 0)
97 EXPORT_SYMBOL(of_find_node_by_type);
99 struct device_node *of_find_compatible_node(struct device_node *from,
100 const char *type, const char *compatible)
102 struct device_node *np;
104 np = from ? from->allnext : allnodes;
105 for (; np != 0; np = np->allnext) {
107 && !(np->type != 0 && strcmp(np->type, type) == 0))
109 if (of_device_is_compatible(np, compatible))
115 EXPORT_SYMBOL(of_find_compatible_node);
117 int of_getintprop_default(struct device_node *np, const char *name, int def)
119 struct property *prop;
122 prop = of_find_property(np, name, &len);
123 if (!prop || len != 4)
126 return *(int *) prop->value;
128 EXPORT_SYMBOL(of_getintprop_default);
130 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
132 struct property **prevp;
136 new_val = kmalloc(len, GFP_KERNEL);
140 memcpy(new_val, val, len);
144 write_lock(&devtree_lock);
145 prevp = &dp->properties;
147 struct property *prop = *prevp;
149 if (!strcasecmp(prop->name, name)) {
150 void *old_val = prop->value;
153 ret = prom_setprop(dp->node, (char *) name, val, len);
156 prop->value = new_val;
159 if (OF_IS_DYNAMIC(prop))
162 OF_MARK_DYNAMIC(prop);
168 prevp = &(*prevp)->next;
170 write_unlock(&devtree_lock);
172 /* XXX Upate procfs if necessary... */
176 EXPORT_SYMBOL(of_set_property);
178 static unsigned int prom_early_allocated;
180 static void * __init prom_early_alloc(unsigned long size)
184 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
186 memset(ret, 0, size);
188 prom_early_allocated += size;
193 static int is_root_node(const struct device_node *dp)
198 return (dp->parent == NULL);
201 /* The following routines deal with the black magic of fully naming a
204 * Certain well known named nodes are just the simple name string.
206 * Actual devices have an address specifier appended to the base name
207 * string, like this "foo@addr". The "addr" can be in any number of
208 * formats, and the platform plus the type of the node determine the
209 * format and how it is constructed.
211 * For children of the ROOT node, the naming convention is fixed and
212 * determined by whether this is a sun4u or sun4v system.
214 * For children of other nodes, it is bus type specific. So
215 * we walk up the tree until we discover a "device_type" property
216 * we recognize and we go from there.
218 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
220 struct linux_prom_registers *regs;
221 struct property *rprop;
223 rprop = of_find_property(dp, "reg", NULL);
228 sprintf(tmp_buf, "%s@%x,%x",
230 regs->which_io, regs->phys_addr);
233 /* "name@slot,offset" */
234 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
236 struct linux_prom_registers *regs;
237 struct property *prop;
239 prop = of_find_property(dp, "reg", NULL);
244 sprintf(tmp_buf, "%s@%x,%x",
250 /* "name@devnum[,func]" */
251 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
253 struct linux_prom_pci_registers *regs;
254 struct property *prop;
257 prop = of_find_property(dp, "reg", NULL);
262 devfn = (regs->phys_hi >> 8) & 0xff;
264 sprintf(tmp_buf, "%s@%x,%x",
269 sprintf(tmp_buf, "%s@%x",
275 /* "name@addrhi,addrlo" */
276 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
278 struct linux_prom_registers *regs;
279 struct property *prop;
281 prop = of_find_property(dp, "reg", NULL);
287 sprintf(tmp_buf, "%s@%x,%x",
289 regs->which_io, regs->phys_addr);
292 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
294 struct device_node *parent = dp->parent;
296 if (parent != NULL) {
297 if (!strcmp(parent->type, "pci") ||
298 !strcmp(parent->type, "pciex"))
299 return pci_path_component(dp, tmp_buf);
300 if (!strcmp(parent->type, "sbus"))
301 return sbus_path_component(dp, tmp_buf);
302 if (!strcmp(parent->type, "ebus"))
303 return ebus_path_component(dp, tmp_buf);
305 /* "isa" is handled with platform naming */
308 /* Use platform naming convention. */
309 return sparc32_path_component(dp, tmp_buf);
312 static char * __init build_path_component(struct device_node *dp)
314 char tmp_buf[64], *n;
317 __build_path_component(dp, tmp_buf);
318 if (tmp_buf[0] == '\0')
319 strcpy(tmp_buf, dp->name);
321 n = prom_early_alloc(strlen(tmp_buf) + 1);
327 static char * __init build_full_name(struct device_node *dp)
329 int len, ourlen, plen;
332 plen = strlen(dp->parent->full_name);
333 ourlen = strlen(dp->path_component_name);
334 len = ourlen + plen + 2;
336 n = prom_early_alloc(len);
337 strcpy(n, dp->parent->full_name);
338 if (!is_root_node(dp->parent)) {
339 strcpy(n + plen, "/");
342 strcpy(n + plen, dp->path_component_name);
347 static unsigned int unique_id;
349 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
351 static struct property *tmp = NULL;
358 memset(p, 0, sizeof(*p) + 32);
361 p = prom_early_alloc(sizeof(struct property) + 32);
362 p->unique_id = unique_id++;
365 p->name = (char *) (p + 1);
367 strcpy(p->name, special_name);
368 p->length = special_len;
369 p->value = prom_early_alloc(special_len);
370 memcpy(p->value, special_val, special_len);
373 name = prom_firstprop(node, NULL);
375 name = prom_nextprop(node, prev, NULL);
377 if (strlen(name) == 0) {
381 strcpy(p->name, name);
382 p->length = prom_getproplen(node, p->name);
383 if (p->length <= 0) {
386 p->value = prom_early_alloc(p->length + 1);
387 len = prom_getproperty(node, p->name, p->value,
391 ((unsigned char *)p->value)[p->length] = '\0';
397 static struct property * __init build_prop_list(phandle node)
399 struct property *head, *tail;
401 head = tail = build_one_prop(node, NULL,
402 ".node", &node, sizeof(node));
404 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
407 tail->next = build_one_prop(node, tail->name,
415 static char * __init get_one_property(phandle node, char *name)
417 char *buf = "<NULL>";
420 len = prom_getproplen(node, name);
422 buf = prom_early_alloc(len);
423 len = prom_getproperty(node, name, buf, len);
429 static struct device_node * __init create_node(phandle node)
431 struct device_node *dp;
436 dp = prom_early_alloc(sizeof(*dp));
437 dp->unique_id = unique_id++;
439 kref_init(&dp->kref);
441 dp->name = get_one_property(node, "name");
442 dp->type = get_one_property(node, "device_type");
445 /* Build interrupts later... */
447 dp->properties = build_prop_list(node);
452 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
454 struct device_node *dp;
456 dp = create_node(node);
459 *nextp = &dp->allnext;
462 dp->path_component_name = build_path_component(dp);
463 dp->full_name = build_full_name(dp);
465 dp->child = build_tree(dp, prom_getchild(node), nextp);
467 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
473 void __init prom_build_devicetree(void)
475 struct device_node **nextp;
477 allnodes = create_node(prom_root_node);
478 allnodes->path_component_name = "";
479 allnodes->full_name = "/";
481 nextp = &allnodes->allnext;
482 allnodes->child = build_tree(allnodes,
483 prom_getchild(allnodes->node),
485 printk("PROM: Built device tree with %u bytes of memory.\n",
486 prom_early_allocated);