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 static unsigned int prom_early_allocated
;
107 static void * __init
prom_early_alloc(unsigned long size
)
111 ret
= __alloc_bootmem(size
, SMP_CACHE_BYTES
, 0UL);
113 memset(ret
, 0, size
);
115 prom_early_allocated
+= size
;
120 static int is_root_node(const struct device_node
*dp
)
125 return (dp
->parent
== NULL
);
128 /* The following routines deal with the black magic of fully naming a
131 * Certain well known named nodes are just the simple name string.
133 * Actual devices have an address specifier appended to the base name
134 * string, like this "foo@addr". The "addr" can be in any number of
135 * formats, and the platform plus the type of the node determine the
136 * format and how it is constructed.
138 * For children of the ROOT node, the naming convention is fixed and
139 * determined by whether this is a sun4u or sun4v system.
141 * For children of other nodes, it is bus type specific. So
142 * we walk up the tree until we discover a "device_type" property
143 * we recognize and we go from there.
145 static void __init
sparc32_path_component(struct device_node
*dp
, char *tmp_buf
)
147 struct linux_prom_registers
*regs
;
148 struct property
*rprop
;
150 rprop
= of_find_property(dp
, "reg", NULL
);
155 sprintf(tmp_buf
, "%s@%x,%x",
157 regs
->which_io
, regs
->phys_addr
);
160 /* "name@slot,offset" */
161 static void __init
sbus_path_component(struct device_node
*dp
, char *tmp_buf
)
163 struct linux_prom_registers
*regs
;
164 struct property
*prop
;
166 prop
= of_find_property(dp
, "reg", NULL
);
171 sprintf(tmp_buf
, "%s@%x,%x",
177 /* "name@devnum[,func]" */
178 static void __init
pci_path_component(struct device_node
*dp
, char *tmp_buf
)
180 struct linux_prom_pci_registers
*regs
;
181 struct property
*prop
;
184 prop
= of_find_property(dp
, "reg", NULL
);
189 devfn
= (regs
->phys_hi
>> 8) & 0xff;
191 sprintf(tmp_buf
, "%s@%x,%x",
196 sprintf(tmp_buf
, "%s@%x",
202 /* "name@addrhi,addrlo" */
203 static void __init
ebus_path_component(struct device_node
*dp
, char *tmp_buf
)
205 struct linux_prom_registers
*regs
;
206 struct property
*prop
;
208 prop
= of_find_property(dp
, "reg", NULL
);
214 sprintf(tmp_buf
, "%s@%x,%x",
216 regs
->which_io
, regs
->phys_addr
);
219 static void __init
__build_path_component(struct device_node
*dp
, char *tmp_buf
)
221 struct device_node
*parent
= dp
->parent
;
223 if (parent
!= NULL
) {
224 if (!strcmp(parent
->type
, "pci") ||
225 !strcmp(parent
->type
, "pciex"))
226 return pci_path_component(dp
, tmp_buf
);
227 if (!strcmp(parent
->type
, "sbus"))
228 return sbus_path_component(dp
, tmp_buf
);
229 if (!strcmp(parent
->type
, "ebus"))
230 return ebus_path_component(dp
, tmp_buf
);
232 /* "isa" is handled with platform naming */
235 /* Use platform naming convention. */
236 return sparc32_path_component(dp
, tmp_buf
);
239 static char * __init
build_path_component(struct device_node
*dp
)
241 char tmp_buf
[64], *n
;
244 __build_path_component(dp
, tmp_buf
);
245 if (tmp_buf
[0] == '\0')
246 strcpy(tmp_buf
, dp
->name
);
248 n
= prom_early_alloc(strlen(tmp_buf
) + 1);
254 static char * __init
build_full_name(struct device_node
*dp
)
256 int len
, ourlen
, plen
;
259 plen
= strlen(dp
->parent
->full_name
);
260 ourlen
= strlen(dp
->path_component_name
);
261 len
= ourlen
+ plen
+ 2;
263 n
= prom_early_alloc(len
);
264 strcpy(n
, dp
->parent
->full_name
);
265 if (!is_root_node(dp
->parent
)) {
266 strcpy(n
+ plen
, "/");
269 strcpy(n
+ plen
, dp
->path_component_name
);
274 static unsigned int unique_id
;
276 static struct property
* __init
build_one_prop(phandle node
, char *prev
, char *special_name
, void *special_val
, int special_len
)
278 static struct property
*tmp
= NULL
;
285 memset(p
, 0, sizeof(*p
) + 32);
288 p
= prom_early_alloc(sizeof(struct property
) + 32);
289 p
->unique_id
= unique_id
++;
292 p
->name
= (char *) (p
+ 1);
294 strcpy(p
->name
, special_name
);
295 p
->length
= special_len
;
296 p
->value
= prom_early_alloc(special_len
);
297 memcpy(p
->value
, special_val
, special_len
);
300 name
= prom_firstprop(node
, NULL
);
302 name
= prom_nextprop(node
, prev
, NULL
);
304 if (strlen(name
) == 0) {
308 strcpy(p
->name
, name
);
309 p
->length
= prom_getproplen(node
, p
->name
);
310 if (p
->length
<= 0) {
313 p
->value
= prom_early_alloc(p
->length
+ 1);
314 len
= prom_getproperty(node
, p
->name
, p
->value
,
318 ((unsigned char *)p
->value
)[p
->length
] = '\0';
324 static struct property
* __init
build_prop_list(phandle node
)
326 struct property
*head
, *tail
;
328 head
= tail
= build_one_prop(node
, NULL
,
329 ".node", &node
, sizeof(node
));
331 tail
->next
= build_one_prop(node
, NULL
, NULL
, NULL
, 0);
334 tail
->next
= build_one_prop(node
, tail
->name
,
342 static char * __init
get_one_property(phandle node
, char *name
)
344 char *buf
= "<NULL>";
347 len
= prom_getproplen(node
, name
);
349 buf
= prom_early_alloc(len
);
350 len
= prom_getproperty(node
, name
, buf
, len
);
356 static struct device_node
* __init
create_node(phandle node
)
358 struct device_node
*dp
;
363 dp
= prom_early_alloc(sizeof(*dp
));
364 dp
->unique_id
= unique_id
++;
366 kref_init(&dp
->kref
);
368 dp
->name
= get_one_property(node
, "name");
369 dp
->type
= get_one_property(node
, "device_type");
372 /* Build interrupts later... */
374 dp
->properties
= build_prop_list(node
);
379 static struct device_node
* __init
build_tree(struct device_node
*parent
, phandle node
, struct device_node
***nextp
)
381 struct device_node
*dp
;
383 dp
= create_node(node
);
386 *nextp
= &dp
->allnext
;
389 dp
->path_component_name
= build_path_component(dp
);
390 dp
->full_name
= build_full_name(dp
);
392 dp
->child
= build_tree(dp
, prom_getchild(node
), nextp
);
394 dp
->sibling
= build_tree(parent
, prom_getsibling(node
), nextp
);
400 struct device_node
*of_console_device
;
401 EXPORT_SYMBOL(of_console_device
);
403 char *of_console_path
;
404 EXPORT_SYMBOL(of_console_path
);
406 char *of_console_options
;
407 EXPORT_SYMBOL(of_console_options
);
409 extern void restore_current(void);
411 static void __init
of_console_init(void)
413 char *msg
= "OF stdout device is: %s\n";
414 struct device_node
*dp
;
420 of_console_path
= prom_early_alloc(256);
426 switch (*romvec
->pv_stdout
) {
440 prom_printf("Invalid PROM_V0 stdout value %u\n",
445 for_each_node_by_type(dp
, type
) {
450 prom_printf("Cannot find PROM_V0 console node.\n");
453 of_console_device
= dp
;
455 strcpy(of_console_path
, dp
->full_name
);
456 if (!strcmp(type
, "serial")) {
457 strcat(of_console_path
,
458 (skip
? ":b" : ":a"));
465 fd
= *romvec
->pv_v2bootargs
.fd_stdout
;
467 spin_lock_irqsave(&prom_lock
, flags
);
468 node
= (*romvec
->pv_v2devops
.v2_inst2pkg
)(fd
);
470 spin_unlock_irqrestore(&prom_lock
, flags
);
473 prom_printf("Cannot resolve stdout node from "
474 "instance %08x.\n", fd
);
477 dp
= of_find_node_by_phandle(node
);
478 type
= of_get_property(dp
, "device_type", NULL
);
481 prom_printf("Console stdout lacks "
482 "device_type property.\n");
486 if (strcmp(type
, "display") && strcmp(type
, "serial")) {
487 prom_printf("Console device_type is neither display "
492 of_console_device
= dp
;
494 if (prom_vers
== PROM_V2
) {
495 strcpy(of_console_path
, dp
->full_name
);
496 switch (*romvec
->pv_stdout
) {
498 strcat(of_console_path
, ":a");
501 strcat(of_console_path
, ":b");
507 dp
= of_find_node_by_path("/");
508 path
= of_get_property(dp
, "stdout-path", NULL
);
510 prom_printf("No stdout-path in root node.\n");
513 strcpy(of_console_path
, path
);
518 of_console_options
= strrchr(of_console_path
, ':');
519 if (of_console_options
) {
520 of_console_options
++;
521 if (*of_console_options
== '\0')
522 of_console_options
= NULL
;
525 prom_printf(msg
, of_console_path
);
526 printk(msg
, of_console_path
);
529 void __init
prom_build_devicetree(void)
531 struct device_node
**nextp
;
533 allnodes
= create_node(prom_root_node
);
534 allnodes
->path_component_name
= "";
535 allnodes
->full_name
= "/";
537 nextp
= &allnodes
->allnext
;
538 allnodes
->child
= build_tree(allnodes
,
539 prom_getchild(allnodes
->node
),
543 printk("PROM: Built device tree with %u bytes of memory.\n",
544 prom_early_allocated
);
This page took 0.07318 seconds and 5 git commands to generate.