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 static unsigned int prom_early_allocated;
32 void * __init prom_early_alloc(unsigned long size)
36 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
40 prom_early_allocated += size;
45 static int is_root_node(const struct device_node *dp)
50 return (dp->parent == NULL);
53 /* The following routines deal with the black magic of fully naming a
56 * Certain well known named nodes are just the simple name string.
58 * Actual devices have an address specifier appended to the base name
59 * string, like this "foo@addr". The "addr" can be in any number of
60 * formats, and the platform plus the type of the node determine the
61 * format and how it is constructed.
63 * For children of the ROOT node, the naming convention is fixed and
64 * determined by whether this is a sun4u or sun4v system.
66 * For children of other nodes, it is bus type specific. So
67 * we walk up the tree until we discover a "device_type" property
68 * we recognize and we go from there.
70 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
72 struct linux_prom_registers *regs;
73 struct property *rprop;
75 rprop = of_find_property(dp, "reg", NULL);
80 sprintf(tmp_buf, "%s@%x,%x",
82 regs->which_io, regs->phys_addr);
85 /* "name@slot,offset" */
86 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
88 struct linux_prom_registers *regs;
89 struct property *prop;
91 prop = of_find_property(dp, "reg", NULL);
96 sprintf(tmp_buf, "%s@%x,%x",
102 /* "name@devnum[,func]" */
103 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
105 struct linux_prom_pci_registers *regs;
106 struct property *prop;
109 prop = of_find_property(dp, "reg", NULL);
114 devfn = (regs->phys_hi >> 8) & 0xff;
116 sprintf(tmp_buf, "%s@%x,%x",
121 sprintf(tmp_buf, "%s@%x",
127 /* "name@addrhi,addrlo" */
128 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
130 struct linux_prom_registers *regs;
131 struct property *prop;
133 prop = of_find_property(dp, "reg", NULL);
139 sprintf(tmp_buf, "%s@%x,%x",
141 regs->which_io, regs->phys_addr);
144 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
146 struct device_node *parent = dp->parent;
148 if (parent != NULL) {
149 if (!strcmp(parent->type, "pci") ||
150 !strcmp(parent->type, "pciex"))
151 return pci_path_component(dp, tmp_buf);
152 if (!strcmp(parent->type, "sbus"))
153 return sbus_path_component(dp, tmp_buf);
154 if (!strcmp(parent->type, "ebus"))
155 return ebus_path_component(dp, tmp_buf);
157 /* "isa" is handled with platform naming */
160 /* Use platform naming convention. */
161 return sparc32_path_component(dp, tmp_buf);
164 static char * __init build_path_component(struct device_node *dp)
166 char tmp_buf[64], *n;
169 __build_path_component(dp, tmp_buf);
170 if (tmp_buf[0] == '\0')
171 strcpy(tmp_buf, dp->name);
173 n = prom_early_alloc(strlen(tmp_buf) + 1);
179 static char * __init build_full_name(struct device_node *dp)
181 int len, ourlen, plen;
184 plen = strlen(dp->parent->full_name);
185 ourlen = strlen(dp->path_component_name);
186 len = ourlen + plen + 2;
188 n = prom_early_alloc(len);
189 strcpy(n, dp->parent->full_name);
190 if (!is_root_node(dp->parent)) {
191 strcpy(n + plen, "/");
194 strcpy(n + plen, dp->path_component_name);
199 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
201 static struct property *tmp = NULL;
208 memset(p, 0, sizeof(*p) + 32);
211 p = prom_early_alloc(sizeof(struct property) + 32);
212 p->unique_id = prom_unique_id++;
215 p->name = (char *) (p + 1);
217 strcpy(p->name, special_name);
218 p->length = special_len;
219 p->value = prom_early_alloc(special_len);
220 memcpy(p->value, special_val, special_len);
223 name = prom_firstprop(node, NULL);
225 name = prom_nextprop(node, prev, NULL);
227 if (strlen(name) == 0) {
231 strcpy(p->name, name);
232 p->length = prom_getproplen(node, p->name);
233 if (p->length <= 0) {
236 p->value = prom_early_alloc(p->length + 1);
237 len = prom_getproperty(node, p->name, p->value,
241 ((unsigned char *)p->value)[p->length] = '\0';
247 static struct property * __init build_prop_list(phandle node)
249 struct property *head, *tail;
251 head = tail = build_one_prop(node, NULL,
252 ".node", &node, sizeof(node));
254 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
257 tail->next = build_one_prop(node, tail->name,
265 static char * __init get_one_property(phandle node, char *name)
267 char *buf = "<NULL>";
270 len = prom_getproplen(node, name);
272 buf = prom_early_alloc(len);
273 len = prom_getproperty(node, name, buf, len);
279 static struct device_node * __init create_node(phandle node)
281 struct device_node *dp;
286 dp = prom_early_alloc(sizeof(*dp));
287 dp->unique_id = prom_unique_id++;
289 kref_init(&dp->kref);
291 dp->name = get_one_property(node, "name");
292 dp->type = get_one_property(node, "device_type");
295 /* Build interrupts later... */
297 dp->properties = build_prop_list(node);
302 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
304 struct device_node *dp;
306 dp = create_node(node);
309 *nextp = &dp->allnext;
312 dp->path_component_name = build_path_component(dp);
313 dp->full_name = build_full_name(dp);
315 dp->child = build_tree(dp, prom_getchild(node), nextp);
317 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
323 struct device_node *of_console_device;
324 EXPORT_SYMBOL(of_console_device);
326 char *of_console_path;
327 EXPORT_SYMBOL(of_console_path);
329 char *of_console_options;
330 EXPORT_SYMBOL(of_console_options);
332 extern void restore_current(void);
334 static void __init of_console_init(void)
336 char *msg = "OF stdout device is: %s\n";
337 struct device_node *dp;
343 of_console_path = prom_early_alloc(256);
348 switch (*romvec->pv_stdout) {
362 prom_printf("Invalid PROM_V0 stdout value %u\n",
368 for_each_node_by_type(dp, type) {
373 prom_printf("Cannot find PROM_V0 console node.\n");
376 of_console_device = dp;
378 strcpy(of_console_path, dp->full_name);
379 if (!strcmp(type, "serial")) {
380 strcat(of_console_path,
381 (skip ? ":b" : ":a"));
388 fd = *romvec->pv_v2bootargs.fd_stdout;
390 spin_lock_irqsave(&prom_lock, flags);
391 node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
393 spin_unlock_irqrestore(&prom_lock, flags);
396 prom_printf("Cannot resolve stdout node from "
397 "instance %08x.\n", fd);
400 dp = of_find_node_by_phandle(node);
401 type = of_get_property(dp, "device_type", NULL);
404 prom_printf("Console stdout lacks "
405 "device_type property.\n");
409 if (strcmp(type, "display") && strcmp(type, "serial")) {
410 prom_printf("Console device_type is neither display "
415 of_console_device = dp;
417 if (prom_vers == PROM_V2) {
418 strcpy(of_console_path, dp->full_name);
419 switch (*romvec->pv_stdout) {
421 strcat(of_console_path, ":a");
424 strcat(of_console_path, ":b");
430 dp = of_find_node_by_path("/");
431 path = of_get_property(dp, "stdout-path", NULL);
433 prom_printf("No stdout-path in root node.\n");
436 strcpy(of_console_path, path);
441 of_console_options = strrchr(of_console_path, ':');
442 if (of_console_options) {
443 of_console_options++;
444 if (*of_console_options == '\0')
445 of_console_options = NULL;
448 prom_printf(msg, of_console_path);
449 printk(msg, of_console_path);
452 void __init prom_build_devicetree(void)
454 struct device_node **nextp;
456 allnodes = create_node(prom_root_node);
457 allnodes->path_component_name = "";
458 allnodes->full_name = "/";
460 nextp = &allnodes->allnext;
461 allnodes->child = build_tree(allnodes,
462 prom_getchild(allnodes->node),
466 printk("PROM: Built device tree with %u bytes of memory.\n",
467 prom_early_allocated);