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 DEFINE_MUTEX(of_set_property_mutex
);
58 EXPORT_SYMBOL(of_set_property_mutex
);
60 int of_set_property(struct device_node
*dp
, const char *name
, void *val
, int len
)
62 struct property
**prevp
;
66 new_val
= kmalloc(len
, GFP_KERNEL
);
70 memcpy(new_val
, val
, len
);
74 write_lock(&devtree_lock
);
75 prevp
= &dp
->properties
;
77 struct property
*prop
= *prevp
;
79 if (!strcasecmp(prop
->name
, name
)) {
80 void *old_val
= prop
->value
;
83 mutex_lock(&of_set_property_mutex
);
84 ret
= prom_setprop(dp
->node
, (char *) name
, val
, len
);
85 mutex_unlock(&of_set_property_mutex
);
89 prop
->value
= new_val
;
92 if (OF_IS_DYNAMIC(prop
))
95 OF_MARK_DYNAMIC(prop
);
101 prevp
= &(*prevp
)->next
;
103 write_unlock(&devtree_lock
);
105 /* XXX Upate procfs if necessary... */
109 EXPORT_SYMBOL(of_set_property
);
111 int of_find_in_proplist(const char *list
, const char *match
, int len
)
116 if (!strcmp(list
, match
))
118 l
= strlen(list
) + 1;
124 EXPORT_SYMBOL(of_find_in_proplist
);
126 static unsigned int prom_early_allocated
;
128 static void * __init
prom_early_alloc(unsigned long size
)
132 ret
= __alloc_bootmem(size
, SMP_CACHE_BYTES
, 0UL);
134 memset(ret
, 0, size
);
136 prom_early_allocated
+= size
;
141 static int is_root_node(const struct device_node
*dp
)
146 return (dp
->parent
== NULL
);
149 /* The following routines deal with the black magic of fully naming a
152 * Certain well known named nodes are just the simple name string.
154 * Actual devices have an address specifier appended to the base name
155 * string, like this "foo@addr". The "addr" can be in any number of
156 * formats, and the platform plus the type of the node determine the
157 * format and how it is constructed.
159 * For children of the ROOT node, the naming convention is fixed and
160 * determined by whether this is a sun4u or sun4v system.
162 * For children of other nodes, it is bus type specific. So
163 * we walk up the tree until we discover a "device_type" property
164 * we recognize and we go from there.
166 static void __init
sparc32_path_component(struct device_node
*dp
, char *tmp_buf
)
168 struct linux_prom_registers
*regs
;
169 struct property
*rprop
;
171 rprop
= of_find_property(dp
, "reg", NULL
);
176 sprintf(tmp_buf
, "%s@%x,%x",
178 regs
->which_io
, regs
->phys_addr
);
181 /* "name@slot,offset" */
182 static void __init
sbus_path_component(struct device_node
*dp
, char *tmp_buf
)
184 struct linux_prom_registers
*regs
;
185 struct property
*prop
;
187 prop
= of_find_property(dp
, "reg", NULL
);
192 sprintf(tmp_buf
, "%s@%x,%x",
198 /* "name@devnum[,func]" */
199 static void __init
pci_path_component(struct device_node
*dp
, char *tmp_buf
)
201 struct linux_prom_pci_registers
*regs
;
202 struct property
*prop
;
205 prop
= of_find_property(dp
, "reg", NULL
);
210 devfn
= (regs
->phys_hi
>> 8) & 0xff;
212 sprintf(tmp_buf
, "%s@%x,%x",
217 sprintf(tmp_buf
, "%s@%x",
223 /* "name@addrhi,addrlo" */
224 static void __init
ebus_path_component(struct device_node
*dp
, char *tmp_buf
)
226 struct linux_prom_registers
*regs
;
227 struct property
*prop
;
229 prop
= of_find_property(dp
, "reg", NULL
);
235 sprintf(tmp_buf
, "%s@%x,%x",
237 regs
->which_io
, regs
->phys_addr
);
240 static void __init
__build_path_component(struct device_node
*dp
, char *tmp_buf
)
242 struct device_node
*parent
= dp
->parent
;
244 if (parent
!= NULL
) {
245 if (!strcmp(parent
->type
, "pci") ||
246 !strcmp(parent
->type
, "pciex"))
247 return pci_path_component(dp
, tmp_buf
);
248 if (!strcmp(parent
->type
, "sbus"))
249 return sbus_path_component(dp
, tmp_buf
);
250 if (!strcmp(parent
->type
, "ebus"))
251 return ebus_path_component(dp
, tmp_buf
);
253 /* "isa" is handled with platform naming */
256 /* Use platform naming convention. */
257 return sparc32_path_component(dp
, tmp_buf
);
260 static char * __init
build_path_component(struct device_node
*dp
)
262 char tmp_buf
[64], *n
;
265 __build_path_component(dp
, tmp_buf
);
266 if (tmp_buf
[0] == '\0')
267 strcpy(tmp_buf
, dp
->name
);
269 n
= prom_early_alloc(strlen(tmp_buf
) + 1);
275 static char * __init
build_full_name(struct device_node
*dp
)
277 int len
, ourlen
, plen
;
280 plen
= strlen(dp
->parent
->full_name
);
281 ourlen
= strlen(dp
->path_component_name
);
282 len
= ourlen
+ plen
+ 2;
284 n
= prom_early_alloc(len
);
285 strcpy(n
, dp
->parent
->full_name
);
286 if (!is_root_node(dp
->parent
)) {
287 strcpy(n
+ plen
, "/");
290 strcpy(n
+ plen
, dp
->path_component_name
);
295 static unsigned int unique_id
;
297 static struct property
* __init
build_one_prop(phandle node
, char *prev
, char *special_name
, void *special_val
, int special_len
)
299 static struct property
*tmp
= NULL
;
306 memset(p
, 0, sizeof(*p
) + 32);
309 p
= prom_early_alloc(sizeof(struct property
) + 32);
310 p
->unique_id
= unique_id
++;
313 p
->name
= (char *) (p
+ 1);
315 strcpy(p
->name
, special_name
);
316 p
->length
= special_len
;
317 p
->value
= prom_early_alloc(special_len
);
318 memcpy(p
->value
, special_val
, special_len
);
321 name
= prom_firstprop(node
, NULL
);
323 name
= prom_nextprop(node
, prev
, NULL
);
325 if (strlen(name
) == 0) {
329 strcpy(p
->name
, name
);
330 p
->length
= prom_getproplen(node
, p
->name
);
331 if (p
->length
<= 0) {
334 p
->value
= prom_early_alloc(p
->length
+ 1);
335 len
= prom_getproperty(node
, p
->name
, p
->value
,
339 ((unsigned char *)p
->value
)[p
->length
] = '\0';
345 static struct property
* __init
build_prop_list(phandle node
)
347 struct property
*head
, *tail
;
349 head
= tail
= build_one_prop(node
, NULL
,
350 ".node", &node
, sizeof(node
));
352 tail
->next
= build_one_prop(node
, NULL
, NULL
, NULL
, 0);
355 tail
->next
= build_one_prop(node
, tail
->name
,
363 static char * __init
get_one_property(phandle node
, char *name
)
365 char *buf
= "<NULL>";
368 len
= prom_getproplen(node
, name
);
370 buf
= prom_early_alloc(len
);
371 len
= prom_getproperty(node
, name
, buf
, len
);
377 static struct device_node
* __init
create_node(phandle node
)
379 struct device_node
*dp
;
384 dp
= prom_early_alloc(sizeof(*dp
));
385 dp
->unique_id
= unique_id
++;
387 kref_init(&dp
->kref
);
389 dp
->name
= get_one_property(node
, "name");
390 dp
->type
= get_one_property(node
, "device_type");
393 /* Build interrupts later... */
395 dp
->properties
= build_prop_list(node
);
400 static struct device_node
* __init
build_tree(struct device_node
*parent
, phandle node
, struct device_node
***nextp
)
402 struct device_node
*dp
;
404 dp
= create_node(node
);
407 *nextp
= &dp
->allnext
;
410 dp
->path_component_name
= build_path_component(dp
);
411 dp
->full_name
= build_full_name(dp
);
413 dp
->child
= build_tree(dp
, prom_getchild(node
), nextp
);
415 dp
->sibling
= build_tree(parent
, prom_getsibling(node
), nextp
);
421 struct device_node
*of_console_device
;
422 EXPORT_SYMBOL(of_console_device
);
424 char *of_console_path
;
425 EXPORT_SYMBOL(of_console_path
);
427 char *of_console_options
;
428 EXPORT_SYMBOL(of_console_options
);
430 extern void restore_current(void);
432 static void __init
of_console_init(void)
434 char *msg
= "OF stdout device is: %s\n";
435 struct device_node
*dp
;
441 of_console_path
= prom_early_alloc(256);
446 switch (*romvec
->pv_stdout
) {
460 prom_printf("Invalid PROM_V0 stdout value %u\n",
466 for_each_node_by_type(dp
, type
) {
471 prom_printf("Cannot find PROM_V0 console node.\n");
474 of_console_device
= dp
;
476 strcpy(of_console_path
, dp
->full_name
);
477 if (!strcmp(type
, "serial")) {
478 strcat(of_console_path
,
479 (skip
? ":b" : ":a"));
486 fd
= *romvec
->pv_v2bootargs
.fd_stdout
;
488 spin_lock_irqsave(&prom_lock
, flags
);
489 node
= (*romvec
->pv_v2devops
.v2_inst2pkg
)(fd
);
491 spin_unlock_irqrestore(&prom_lock
, flags
);
494 prom_printf("Cannot resolve stdout node from "
495 "instance %08x.\n", fd
);
498 dp
= of_find_node_by_phandle(node
);
499 type
= of_get_property(dp
, "device_type", NULL
);
502 prom_printf("Console stdout lacks "
503 "device_type property.\n");
507 if (strcmp(type
, "display") && strcmp(type
, "serial")) {
508 prom_printf("Console device_type is neither display "
513 of_console_device
= dp
;
515 if (prom_vers
== PROM_V2
) {
516 strcpy(of_console_path
, dp
->full_name
);
517 switch (*romvec
->pv_stdout
) {
519 strcat(of_console_path
, ":a");
522 strcat(of_console_path
, ":b");
528 dp
= of_find_node_by_path("/");
529 path
= of_get_property(dp
, "stdout-path", NULL
);
531 prom_printf("No stdout-path in root node.\n");
534 strcpy(of_console_path
, path
);
539 of_console_options
= strrchr(of_console_path
, ':');
540 if (of_console_options
) {
541 of_console_options
++;
542 if (*of_console_options
== '\0')
543 of_console_options
= NULL
;
546 prom_printf(msg
, of_console_path
);
547 printk(msg
, of_console_path
);
550 void __init
prom_build_devicetree(void)
552 struct device_node
**nextp
;
554 allnodes
= create_node(prom_root_node
);
555 allnodes
->path_component_name
= "";
556 allnodes
->full_name
= "/";
558 nextp
= &allnodes
->allnext
;
559 allnodes
->child
= build_tree(allnodes
,
560 prom_getchild(allnodes
->node
),
564 printk("PROM: Built device tree with %u bytes of memory.\n",
565 prom_early_allocated
);
This page took 0.042401 seconds and 6 git commands to generate.