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>
30 struct device_node *of_find_node_by_phandle(phandle handle)
32 struct device_node *np;
34 for (np = allnodes; np != 0; np = np->allnext)
35 if (np->node == handle)
40 EXPORT_SYMBOL(of_find_node_by_phandle);
42 int of_getintprop_default(struct device_node *np, const char *name, int def)
44 struct property *prop;
47 prop = of_find_property(np, name, &len);
48 if (!prop || len != 4)
51 return *(int *) prop->value;
53 EXPORT_SYMBOL(of_getintprop_default);
55 DEFINE_MUTEX(of_set_property_mutex);
56 EXPORT_SYMBOL(of_set_property_mutex);
58 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
60 struct property **prevp;
64 new_val = kmalloc(len, GFP_KERNEL);
68 memcpy(new_val, val, len);
72 write_lock(&devtree_lock);
73 prevp = &dp->properties;
75 struct property *prop = *prevp;
77 if (!strcasecmp(prop->name, name)) {
78 void *old_val = prop->value;
81 mutex_lock(&of_set_property_mutex);
82 ret = prom_setprop(dp->node, (char *) name, val, len);
83 mutex_unlock(&of_set_property_mutex);
87 prop->value = new_val;
90 if (OF_IS_DYNAMIC(prop))
93 OF_MARK_DYNAMIC(prop);
99 prevp = &(*prevp)->next;
101 write_unlock(&devtree_lock);
103 /* XXX Upate procfs if necessary... */
107 EXPORT_SYMBOL(of_set_property);
109 int of_find_in_proplist(const char *list, const char *match, int len)
114 if (!strcmp(list, match))
116 l = strlen(list) + 1;
122 EXPORT_SYMBOL(of_find_in_proplist);
124 static unsigned int prom_early_allocated;
126 static void * __init prom_early_alloc(unsigned long size)
130 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
132 memset(ret, 0, size);
134 prom_early_allocated += size;
139 static int is_root_node(const struct device_node *dp)
144 return (dp->parent == NULL);
147 /* The following routines deal with the black magic of fully naming a
150 * Certain well known named nodes are just the simple name string.
152 * Actual devices have an address specifier appended to the base name
153 * string, like this "foo@addr". The "addr" can be in any number of
154 * formats, and the platform plus the type of the node determine the
155 * format and how it is constructed.
157 * For children of the ROOT node, the naming convention is fixed and
158 * determined by whether this is a sun4u or sun4v system.
160 * For children of other nodes, it is bus type specific. So
161 * we walk up the tree until we discover a "device_type" property
162 * we recognize and we go from there.
164 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
166 struct linux_prom_registers *regs;
167 struct property *rprop;
169 rprop = of_find_property(dp, "reg", NULL);
174 sprintf(tmp_buf, "%s@%x,%x",
176 regs->which_io, regs->phys_addr);
179 /* "name@slot,offset" */
180 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
182 struct linux_prom_registers *regs;
183 struct property *prop;
185 prop = of_find_property(dp, "reg", NULL);
190 sprintf(tmp_buf, "%s@%x,%x",
196 /* "name@devnum[,func]" */
197 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
199 struct linux_prom_pci_registers *regs;
200 struct property *prop;
203 prop = of_find_property(dp, "reg", NULL);
208 devfn = (regs->phys_hi >> 8) & 0xff;
210 sprintf(tmp_buf, "%s@%x,%x",
215 sprintf(tmp_buf, "%s@%x",
221 /* "name@addrhi,addrlo" */
222 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
224 struct linux_prom_registers *regs;
225 struct property *prop;
227 prop = of_find_property(dp, "reg", NULL);
233 sprintf(tmp_buf, "%s@%x,%x",
235 regs->which_io, regs->phys_addr);
238 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
240 struct device_node *parent = dp->parent;
242 if (parent != NULL) {
243 if (!strcmp(parent->type, "pci") ||
244 !strcmp(parent->type, "pciex"))
245 return pci_path_component(dp, tmp_buf);
246 if (!strcmp(parent->type, "sbus"))
247 return sbus_path_component(dp, tmp_buf);
248 if (!strcmp(parent->type, "ebus"))
249 return ebus_path_component(dp, tmp_buf);
251 /* "isa" is handled with platform naming */
254 /* Use platform naming convention. */
255 return sparc32_path_component(dp, tmp_buf);
258 static char * __init build_path_component(struct device_node *dp)
260 char tmp_buf[64], *n;
263 __build_path_component(dp, tmp_buf);
264 if (tmp_buf[0] == '\0')
265 strcpy(tmp_buf, dp->name);
267 n = prom_early_alloc(strlen(tmp_buf) + 1);
273 static char * __init build_full_name(struct device_node *dp)
275 int len, ourlen, plen;
278 plen = strlen(dp->parent->full_name);
279 ourlen = strlen(dp->path_component_name);
280 len = ourlen + plen + 2;
282 n = prom_early_alloc(len);
283 strcpy(n, dp->parent->full_name);
284 if (!is_root_node(dp->parent)) {
285 strcpy(n + plen, "/");
288 strcpy(n + plen, dp->path_component_name);
293 static unsigned int unique_id;
295 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
297 static struct property *tmp = NULL;
304 memset(p, 0, sizeof(*p) + 32);
307 p = prom_early_alloc(sizeof(struct property) + 32);
308 p->unique_id = unique_id++;
311 p->name = (char *) (p + 1);
313 strcpy(p->name, special_name);
314 p->length = special_len;
315 p->value = prom_early_alloc(special_len);
316 memcpy(p->value, special_val, special_len);
319 name = prom_firstprop(node, NULL);
321 name = prom_nextprop(node, prev, NULL);
323 if (strlen(name) == 0) {
327 strcpy(p->name, name);
328 p->length = prom_getproplen(node, p->name);
329 if (p->length <= 0) {
332 p->value = prom_early_alloc(p->length + 1);
333 len = prom_getproperty(node, p->name, p->value,
337 ((unsigned char *)p->value)[p->length] = '\0';
343 static struct property * __init build_prop_list(phandle node)
345 struct property *head, *tail;
347 head = tail = build_one_prop(node, NULL,
348 ".node", &node, sizeof(node));
350 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
353 tail->next = build_one_prop(node, tail->name,
361 static char * __init get_one_property(phandle node, char *name)
363 char *buf = "<NULL>";
366 len = prom_getproplen(node, name);
368 buf = prom_early_alloc(len);
369 len = prom_getproperty(node, name, buf, len);
375 static struct device_node * __init create_node(phandle node)
377 struct device_node *dp;
382 dp = prom_early_alloc(sizeof(*dp));
383 dp->unique_id = unique_id++;
385 kref_init(&dp->kref);
387 dp->name = get_one_property(node, "name");
388 dp->type = get_one_property(node, "device_type");
391 /* Build interrupts later... */
393 dp->properties = build_prop_list(node);
398 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
400 struct device_node *dp;
402 dp = create_node(node);
405 *nextp = &dp->allnext;
408 dp->path_component_name = build_path_component(dp);
409 dp->full_name = build_full_name(dp);
411 dp->child = build_tree(dp, prom_getchild(node), nextp);
413 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
419 struct device_node *of_console_device;
420 EXPORT_SYMBOL(of_console_device);
422 char *of_console_path;
423 EXPORT_SYMBOL(of_console_path);
425 char *of_console_options;
426 EXPORT_SYMBOL(of_console_options);
428 extern void restore_current(void);
430 static void __init of_console_init(void)
432 char *msg = "OF stdout device is: %s\n";
433 struct device_node *dp;
439 of_console_path = prom_early_alloc(256);
444 switch (*romvec->pv_stdout) {
458 prom_printf("Invalid PROM_V0 stdout value %u\n",
464 for_each_node_by_type(dp, type) {
469 prom_printf("Cannot find PROM_V0 console node.\n");
472 of_console_device = dp;
474 strcpy(of_console_path, dp->full_name);
475 if (!strcmp(type, "serial")) {
476 strcat(of_console_path,
477 (skip ? ":b" : ":a"));
484 fd = *romvec->pv_v2bootargs.fd_stdout;
486 spin_lock_irqsave(&prom_lock, flags);
487 node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
489 spin_unlock_irqrestore(&prom_lock, flags);
492 prom_printf("Cannot resolve stdout node from "
493 "instance %08x.\n", fd);
496 dp = of_find_node_by_phandle(node);
497 type = of_get_property(dp, "device_type", NULL);
500 prom_printf("Console stdout lacks "
501 "device_type property.\n");
505 if (strcmp(type, "display") && strcmp(type, "serial")) {
506 prom_printf("Console device_type is neither display "
511 of_console_device = dp;
513 if (prom_vers == PROM_V2) {
514 strcpy(of_console_path, dp->full_name);
515 switch (*romvec->pv_stdout) {
517 strcat(of_console_path, ":a");
520 strcat(of_console_path, ":b");
526 dp = of_find_node_by_path("/");
527 path = of_get_property(dp, "stdout-path", NULL);
529 prom_printf("No stdout-path in root node.\n");
532 strcpy(of_console_path, path);
537 of_console_options = strrchr(of_console_path, ':');
538 if (of_console_options) {
539 of_console_options++;
540 if (*of_console_options == '\0')
541 of_console_options = NULL;
544 prom_printf(msg, of_console_path);
545 printk(msg, of_console_path);
548 void __init prom_build_devicetree(void)
550 struct device_node **nextp;
552 allnodes = create_node(prom_root_node);
553 allnodes->path_component_name = "";
554 allnodes->full_name = "/";
556 nextp = &allnodes->allnext;
557 allnodes->child = build_tree(allnodes,
558 prom_getchild(allnodes->node),
562 printk("PROM: Built device tree with %u bytes of memory.\n",
563 prom_early_allocated);