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 extern struct device_node *allnodes; /* temporary while merging */
30 extern rwlock_t devtree_lock; /* temporary while merging */
32 struct device_node *of_find_node_by_phandle(phandle handle)
34 struct device_node *np;
36 for (np = allnodes; np != 0; np = np->allnext)
37 if (np->node == handle)
42 EXPORT_SYMBOL(of_find_node_by_phandle);
44 int of_getintprop_default(struct device_node *np, const char *name, int def)
46 struct property *prop;
49 prop = of_find_property(np, name, &len);
50 if (!prop || len != 4)
53 return *(int *) prop->value;
55 EXPORT_SYMBOL(of_getintprop_default);
57 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
59 struct property **prevp;
63 new_val = kmalloc(len, GFP_KERNEL);
67 memcpy(new_val, val, len);
71 write_lock(&devtree_lock);
72 prevp = &dp->properties;
74 struct property *prop = *prevp;
76 if (!strcasecmp(prop->name, name)) {
77 void *old_val = prop->value;
80 ret = prom_setprop(dp->node, (char *) name, val, len);
83 prop->value = new_val;
86 if (OF_IS_DYNAMIC(prop))
89 OF_MARK_DYNAMIC(prop);
95 prevp = &(*prevp)->next;
97 write_unlock(&devtree_lock);
99 /* XXX Upate procfs if necessary... */
103 EXPORT_SYMBOL(of_set_property);
105 int of_find_in_proplist(const char *list, const char *match, int len)
110 if (!strcmp(list, match))
112 l = strlen(list) + 1;
118 EXPORT_SYMBOL(of_find_in_proplist);
120 static unsigned int prom_early_allocated;
122 static void * __init prom_early_alloc(unsigned long size)
126 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
128 memset(ret, 0, size);
130 prom_early_allocated += size;
135 static int is_root_node(const struct device_node *dp)
140 return (dp->parent == NULL);
143 /* The following routines deal with the black magic of fully naming a
146 * Certain well known named nodes are just the simple name string.
148 * Actual devices have an address specifier appended to the base name
149 * string, like this "foo@addr". The "addr" can be in any number of
150 * formats, and the platform plus the type of the node determine the
151 * format and how it is constructed.
153 * For children of the ROOT node, the naming convention is fixed and
154 * determined by whether this is a sun4u or sun4v system.
156 * For children of other nodes, it is bus type specific. So
157 * we walk up the tree until we discover a "device_type" property
158 * we recognize and we go from there.
160 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
162 struct linux_prom_registers *regs;
163 struct property *rprop;
165 rprop = of_find_property(dp, "reg", NULL);
170 sprintf(tmp_buf, "%s@%x,%x",
172 regs->which_io, regs->phys_addr);
175 /* "name@slot,offset" */
176 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
178 struct linux_prom_registers *regs;
179 struct property *prop;
181 prop = of_find_property(dp, "reg", NULL);
186 sprintf(tmp_buf, "%s@%x,%x",
192 /* "name@devnum[,func]" */
193 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
195 struct linux_prom_pci_registers *regs;
196 struct property *prop;
199 prop = of_find_property(dp, "reg", NULL);
204 devfn = (regs->phys_hi >> 8) & 0xff;
206 sprintf(tmp_buf, "%s@%x,%x",
211 sprintf(tmp_buf, "%s@%x",
217 /* "name@addrhi,addrlo" */
218 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
220 struct linux_prom_registers *regs;
221 struct property *prop;
223 prop = of_find_property(dp, "reg", NULL);
229 sprintf(tmp_buf, "%s@%x,%x",
231 regs->which_io, regs->phys_addr);
234 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
236 struct device_node *parent = dp->parent;
238 if (parent != NULL) {
239 if (!strcmp(parent->type, "pci") ||
240 !strcmp(parent->type, "pciex"))
241 return pci_path_component(dp, tmp_buf);
242 if (!strcmp(parent->type, "sbus"))
243 return sbus_path_component(dp, tmp_buf);
244 if (!strcmp(parent->type, "ebus"))
245 return ebus_path_component(dp, tmp_buf);
247 /* "isa" is handled with platform naming */
250 /* Use platform naming convention. */
251 return sparc32_path_component(dp, tmp_buf);
254 static char * __init build_path_component(struct device_node *dp)
256 char tmp_buf[64], *n;
259 __build_path_component(dp, tmp_buf);
260 if (tmp_buf[0] == '\0')
261 strcpy(tmp_buf, dp->name);
263 n = prom_early_alloc(strlen(tmp_buf) + 1);
269 static char * __init build_full_name(struct device_node *dp)
271 int len, ourlen, plen;
274 plen = strlen(dp->parent->full_name);
275 ourlen = strlen(dp->path_component_name);
276 len = ourlen + plen + 2;
278 n = prom_early_alloc(len);
279 strcpy(n, dp->parent->full_name);
280 if (!is_root_node(dp->parent)) {
281 strcpy(n + plen, "/");
284 strcpy(n + plen, dp->path_component_name);
289 static unsigned int unique_id;
291 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
293 static struct property *tmp = NULL;
300 memset(p, 0, sizeof(*p) + 32);
303 p = prom_early_alloc(sizeof(struct property) + 32);
304 p->unique_id = unique_id++;
307 p->name = (char *) (p + 1);
309 strcpy(p->name, special_name);
310 p->length = special_len;
311 p->value = prom_early_alloc(special_len);
312 memcpy(p->value, special_val, special_len);
315 name = prom_firstprop(node, NULL);
317 name = prom_nextprop(node, prev, NULL);
319 if (strlen(name) == 0) {
323 strcpy(p->name, name);
324 p->length = prom_getproplen(node, p->name);
325 if (p->length <= 0) {
328 p->value = prom_early_alloc(p->length + 1);
329 len = prom_getproperty(node, p->name, p->value,
333 ((unsigned char *)p->value)[p->length] = '\0';
339 static struct property * __init build_prop_list(phandle node)
341 struct property *head, *tail;
343 head = tail = build_one_prop(node, NULL,
344 ".node", &node, sizeof(node));
346 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
349 tail->next = build_one_prop(node, tail->name,
357 static char * __init get_one_property(phandle node, char *name)
359 char *buf = "<NULL>";
362 len = prom_getproplen(node, name);
364 buf = prom_early_alloc(len);
365 len = prom_getproperty(node, name, buf, len);
371 static struct device_node * __init create_node(phandle node)
373 struct device_node *dp;
378 dp = prom_early_alloc(sizeof(*dp));
379 dp->unique_id = unique_id++;
381 kref_init(&dp->kref);
383 dp->name = get_one_property(node, "name");
384 dp->type = get_one_property(node, "device_type");
387 /* Build interrupts later... */
389 dp->properties = build_prop_list(node);
394 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
396 struct device_node *dp;
398 dp = create_node(node);
401 *nextp = &dp->allnext;
404 dp->path_component_name = build_path_component(dp);
405 dp->full_name = build_full_name(dp);
407 dp->child = build_tree(dp, prom_getchild(node), nextp);
409 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
415 struct device_node *of_console_device;
416 EXPORT_SYMBOL(of_console_device);
418 char *of_console_path;
419 EXPORT_SYMBOL(of_console_path);
421 char *of_console_options;
422 EXPORT_SYMBOL(of_console_options);
424 extern void restore_current(void);
426 static void __init of_console_init(void)
428 char *msg = "OF stdout device is: %s\n";
429 struct device_node *dp;
435 of_console_path = prom_early_alloc(256);
441 switch (*romvec->pv_stdout) {
455 prom_printf("Invalid PROM_V0 stdout value %u\n",
461 for_each_node_by_type(dp, type) {
466 prom_printf("Cannot find PROM_V0 console node.\n");
469 of_console_device = dp;
471 strcpy(of_console_path, dp->full_name);
472 if (!strcmp(type, "serial")) {
473 strcat(of_console_path,
474 (skip ? ":b" : ":a"));
481 fd = *romvec->pv_v2bootargs.fd_stdout;
483 spin_lock_irqsave(&prom_lock, flags);
484 node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
486 spin_unlock_irqrestore(&prom_lock, flags);
489 prom_printf("Cannot resolve stdout node from "
490 "instance %08x.\n", fd);
493 dp = of_find_node_by_phandle(node);
494 type = of_get_property(dp, "device_type", NULL);
497 prom_printf("Console stdout lacks "
498 "device_type property.\n");
502 if (strcmp(type, "display") && strcmp(type, "serial")) {
503 prom_printf("Console device_type is neither display "
508 of_console_device = dp;
510 if (prom_vers == PROM_V2) {
511 strcpy(of_console_path, dp->full_name);
512 switch (*romvec->pv_stdout) {
514 strcat(of_console_path, ":a");
517 strcat(of_console_path, ":b");
523 dp = of_find_node_by_path("/");
524 path = of_get_property(dp, "stdout-path", NULL);
526 prom_printf("No stdout-path in root node.\n");
529 strcpy(of_console_path, path);
534 of_console_options = strrchr(of_console_path, ':');
535 if (of_console_options) {
536 of_console_options++;
537 if (*of_console_options == '\0')
538 of_console_options = NULL;
541 prom_printf(msg, of_console_path);
542 printk(msg, of_console_path);
545 void __init prom_build_devicetree(void)
547 struct device_node **nextp;
549 allnodes = create_node(prom_root_node);
550 allnodes->path_component_name = "";
551 allnodes->full_name = "/";
553 nextp = &allnodes->allnext;
554 allnodes->child = build_tree(allnodes,
555 prom_getchild(allnodes->node),
559 printk("PROM: Built device tree with %u bytes of memory.\n",
560 prom_early_allocated);