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 static struct device_node
*allnodes
;
30 extern rwlock_t devtree_lock
; /* temporary while merging */
32 struct device_node
*of_get_parent(const struct device_node
*node
)
34 struct device_node
*np
;
43 EXPORT_SYMBOL(of_get_parent
);
45 struct device_node
*of_get_next_child(const struct device_node
*node
,
46 struct device_node
*prev
)
48 struct device_node
*next
;
50 next
= prev
? prev
->sibling
: node
->child
;
51 for (; next
!= 0; next
= next
->sibling
) {
57 EXPORT_SYMBOL(of_get_next_child
);
59 struct device_node
*of_find_node_by_path(const char *path
)
61 struct device_node
*np
= allnodes
;
63 for (; np
!= 0; np
= np
->allnext
) {
64 if (np
->full_name
!= 0 && strcmp(np
->full_name
, path
) == 0)
70 EXPORT_SYMBOL(of_find_node_by_path
);
72 struct device_node
*of_find_node_by_phandle(phandle handle
)
74 struct device_node
*np
;
76 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
77 if (np
->node
== handle
)
82 EXPORT_SYMBOL(of_find_node_by_phandle
);
84 struct device_node
*of_find_node_by_name(struct device_node
*from
,
87 struct device_node
*np
;
89 np
= from
? from
->allnext
: allnodes
;
90 for (; np
!= NULL
; np
= np
->allnext
)
91 if (np
->name
!= NULL
&& strcmp(np
->name
, name
) == 0)
96 EXPORT_SYMBOL(of_find_node_by_name
);
98 struct device_node
*of_find_node_by_type(struct device_node
*from
,
101 struct device_node
*np
;
103 np
= from
? from
->allnext
: allnodes
;
104 for (; np
!= 0; np
= np
->allnext
)
105 if (np
->type
!= 0 && strcmp(np
->type
, type
) == 0)
110 EXPORT_SYMBOL(of_find_node_by_type
);
112 struct device_node
*of_find_compatible_node(struct device_node
*from
,
113 const char *type
, const char *compatible
)
115 struct device_node
*np
;
117 np
= from
? from
->allnext
: allnodes
;
118 for (; np
!= 0; np
= np
->allnext
) {
120 && !(np
->type
!= 0 && strcmp(np
->type
, type
) == 0))
122 if (of_device_is_compatible(np
, compatible
))
128 EXPORT_SYMBOL(of_find_compatible_node
);
130 int of_getintprop_default(struct device_node
*np
, const char *name
, int def
)
132 struct property
*prop
;
135 prop
= of_find_property(np
, name
, &len
);
136 if (!prop
|| len
!= 4)
139 return *(int *) prop
->value
;
141 EXPORT_SYMBOL(of_getintprop_default
);
143 int of_set_property(struct device_node
*dp
, const char *name
, void *val
, int len
)
145 struct property
**prevp
;
149 new_val
= kmalloc(len
, GFP_KERNEL
);
153 memcpy(new_val
, val
, len
);
157 write_lock(&devtree_lock
);
158 prevp
= &dp
->properties
;
160 struct property
*prop
= *prevp
;
162 if (!strcasecmp(prop
->name
, name
)) {
163 void *old_val
= prop
->value
;
166 ret
= prom_setprop(dp
->node
, (char *) name
, val
, len
);
169 prop
->value
= new_val
;
172 if (OF_IS_DYNAMIC(prop
))
175 OF_MARK_DYNAMIC(prop
);
181 prevp
= &(*prevp
)->next
;
183 write_unlock(&devtree_lock
);
185 /* XXX Upate procfs if necessary... */
189 EXPORT_SYMBOL(of_set_property
);
191 static unsigned int prom_early_allocated
;
193 static void * __init
prom_early_alloc(unsigned long size
)
197 ret
= __alloc_bootmem(size
, SMP_CACHE_BYTES
, 0UL);
199 memset(ret
, 0, size
);
201 prom_early_allocated
+= size
;
206 static int is_root_node(const struct device_node
*dp
)
211 return (dp
->parent
== NULL
);
214 /* The following routines deal with the black magic of fully naming a
217 * Certain well known named nodes are just the simple name string.
219 * Actual devices have an address specifier appended to the base name
220 * string, like this "foo@addr". The "addr" can be in any number of
221 * formats, and the platform plus the type of the node determine the
222 * format and how it is constructed.
224 * For children of the ROOT node, the naming convention is fixed and
225 * determined by whether this is a sun4u or sun4v system.
227 * For children of other nodes, it is bus type specific. So
228 * we walk up the tree until we discover a "device_type" property
229 * we recognize and we go from there.
231 static void __init
sparc32_path_component(struct device_node
*dp
, char *tmp_buf
)
233 struct linux_prom_registers
*regs
;
234 struct property
*rprop
;
236 rprop
= of_find_property(dp
, "reg", NULL
);
241 sprintf(tmp_buf
, "%s@%x,%x",
243 regs
->which_io
, regs
->phys_addr
);
246 /* "name@slot,offset" */
247 static void __init
sbus_path_component(struct device_node
*dp
, char *tmp_buf
)
249 struct linux_prom_registers
*regs
;
250 struct property
*prop
;
252 prop
= of_find_property(dp
, "reg", NULL
);
257 sprintf(tmp_buf
, "%s@%x,%x",
263 /* "name@devnum[,func]" */
264 static void __init
pci_path_component(struct device_node
*dp
, char *tmp_buf
)
266 struct linux_prom_pci_registers
*regs
;
267 struct property
*prop
;
270 prop
= of_find_property(dp
, "reg", NULL
);
275 devfn
= (regs
->phys_hi
>> 8) & 0xff;
277 sprintf(tmp_buf
, "%s@%x,%x",
282 sprintf(tmp_buf
, "%s@%x",
288 /* "name@addrhi,addrlo" */
289 static void __init
ebus_path_component(struct device_node
*dp
, char *tmp_buf
)
291 struct linux_prom_registers
*regs
;
292 struct property
*prop
;
294 prop
= of_find_property(dp
, "reg", NULL
);
300 sprintf(tmp_buf
, "%s@%x,%x",
302 regs
->which_io
, regs
->phys_addr
);
305 static void __init
__build_path_component(struct device_node
*dp
, char *tmp_buf
)
307 struct device_node
*parent
= dp
->parent
;
309 if (parent
!= NULL
) {
310 if (!strcmp(parent
->type
, "pci") ||
311 !strcmp(parent
->type
, "pciex"))
312 return pci_path_component(dp
, tmp_buf
);
313 if (!strcmp(parent
->type
, "sbus"))
314 return sbus_path_component(dp
, tmp_buf
);
315 if (!strcmp(parent
->type
, "ebus"))
316 return ebus_path_component(dp
, tmp_buf
);
318 /* "isa" is handled with platform naming */
321 /* Use platform naming convention. */
322 return sparc32_path_component(dp
, tmp_buf
);
325 static char * __init
build_path_component(struct device_node
*dp
)
327 char tmp_buf
[64], *n
;
330 __build_path_component(dp
, tmp_buf
);
331 if (tmp_buf
[0] == '\0')
332 strcpy(tmp_buf
, dp
->name
);
334 n
= prom_early_alloc(strlen(tmp_buf
) + 1);
340 static char * __init
build_full_name(struct device_node
*dp
)
342 int len
, ourlen
, plen
;
345 plen
= strlen(dp
->parent
->full_name
);
346 ourlen
= strlen(dp
->path_component_name
);
347 len
= ourlen
+ plen
+ 2;
349 n
= prom_early_alloc(len
);
350 strcpy(n
, dp
->parent
->full_name
);
351 if (!is_root_node(dp
->parent
)) {
352 strcpy(n
+ plen
, "/");
355 strcpy(n
+ plen
, dp
->path_component_name
);
360 static unsigned int unique_id
;
362 static struct property
* __init
build_one_prop(phandle node
, char *prev
, char *special_name
, void *special_val
, int special_len
)
364 static struct property
*tmp
= NULL
;
371 memset(p
, 0, sizeof(*p
) + 32);
374 p
= prom_early_alloc(sizeof(struct property
) + 32);
375 p
->unique_id
= unique_id
++;
378 p
->name
= (char *) (p
+ 1);
380 strcpy(p
->name
, special_name
);
381 p
->length
= special_len
;
382 p
->value
= prom_early_alloc(special_len
);
383 memcpy(p
->value
, special_val
, special_len
);
386 name
= prom_firstprop(node
, NULL
);
388 name
= prom_nextprop(node
, prev
, NULL
);
390 if (strlen(name
) == 0) {
394 strcpy(p
->name
, name
);
395 p
->length
= prom_getproplen(node
, p
->name
);
396 if (p
->length
<= 0) {
399 p
->value
= prom_early_alloc(p
->length
+ 1);
400 len
= prom_getproperty(node
, p
->name
, p
->value
,
404 ((unsigned char *)p
->value
)[p
->length
] = '\0';
410 static struct property
* __init
build_prop_list(phandle node
)
412 struct property
*head
, *tail
;
414 head
= tail
= build_one_prop(node
, NULL
,
415 ".node", &node
, sizeof(node
));
417 tail
->next
= build_one_prop(node
, NULL
, NULL
, NULL
, 0);
420 tail
->next
= build_one_prop(node
, tail
->name
,
428 static char * __init
get_one_property(phandle node
, char *name
)
430 char *buf
= "<NULL>";
433 len
= prom_getproplen(node
, name
);
435 buf
= prom_early_alloc(len
);
436 len
= prom_getproperty(node
, name
, buf
, len
);
442 static struct device_node
* __init
create_node(phandle node
)
444 struct device_node
*dp
;
449 dp
= prom_early_alloc(sizeof(*dp
));
450 dp
->unique_id
= unique_id
++;
452 kref_init(&dp
->kref
);
454 dp
->name
= get_one_property(node
, "name");
455 dp
->type
= get_one_property(node
, "device_type");
458 /* Build interrupts later... */
460 dp
->properties
= build_prop_list(node
);
465 static struct device_node
* __init
build_tree(struct device_node
*parent
, phandle node
, struct device_node
***nextp
)
467 struct device_node
*dp
;
469 dp
= create_node(node
);
472 *nextp
= &dp
->allnext
;
475 dp
->path_component_name
= build_path_component(dp
);
476 dp
->full_name
= build_full_name(dp
);
478 dp
->child
= build_tree(dp
, prom_getchild(node
), nextp
);
480 dp
->sibling
= build_tree(parent
, prom_getsibling(node
), nextp
);
486 void __init
prom_build_devicetree(void)
488 struct device_node
**nextp
;
490 allnodes
= create_node(prom_root_node
);
491 allnodes
->path_component_name
= "";
492 allnodes
->full_name
= "/";
494 nextp
= &allnodes
->allnext
;
495 allnodes
->child
= build_tree(allnodes
,
496 prom_getchild(allnodes
->node
),
498 printk("PROM: Built device tree with %u bytes of memory.\n",
499 prom_early_allocated
);