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_find_node_by_path(const char *path
)
34 struct device_node
*np
= allnodes
;
36 for (; np
!= 0; np
= np
->allnext
) {
37 if (np
->full_name
!= 0 && strcmp(np
->full_name
, path
) == 0)
43 EXPORT_SYMBOL(of_find_node_by_path
);
45 struct device_node
*of_find_node_by_phandle(phandle handle
)
47 struct device_node
*np
;
49 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
50 if (np
->node
== handle
)
55 EXPORT_SYMBOL(of_find_node_by_phandle
);
57 struct device_node
*of_find_node_by_name(struct device_node
*from
,
60 struct device_node
*np
;
62 np
= from
? from
->allnext
: allnodes
;
63 for (; np
!= NULL
; np
= np
->allnext
)
64 if (np
->name
!= NULL
&& strcmp(np
->name
, name
) == 0)
69 EXPORT_SYMBOL(of_find_node_by_name
);
71 struct device_node
*of_find_node_by_type(struct device_node
*from
,
74 struct device_node
*np
;
76 np
= from
? from
->allnext
: allnodes
;
77 for (; np
!= 0; np
= np
->allnext
)
78 if (np
->type
!= 0 && strcmp(np
->type
, type
) == 0)
83 EXPORT_SYMBOL(of_find_node_by_type
);
85 struct device_node
*of_find_compatible_node(struct device_node
*from
,
86 const char *type
, const char *compatible
)
88 struct device_node
*np
;
90 np
= from
? from
->allnext
: allnodes
;
91 for (; np
!= 0; np
= np
->allnext
) {
93 && !(np
->type
!= 0 && strcmp(np
->type
, type
) == 0))
95 if (of_device_is_compatible(np
, compatible
))
101 EXPORT_SYMBOL(of_find_compatible_node
);
103 int of_getintprop_default(struct device_node
*np
, const char *name
, int def
)
105 struct property
*prop
;
108 prop
= of_find_property(np
, name
, &len
);
109 if (!prop
|| len
!= 4)
112 return *(int *) prop
->value
;
114 EXPORT_SYMBOL(of_getintprop_default
);
116 int of_set_property(struct device_node
*dp
, const char *name
, void *val
, int len
)
118 struct property
**prevp
;
122 new_val
= kmalloc(len
, GFP_KERNEL
);
126 memcpy(new_val
, val
, len
);
130 write_lock(&devtree_lock
);
131 prevp
= &dp
->properties
;
133 struct property
*prop
= *prevp
;
135 if (!strcasecmp(prop
->name
, name
)) {
136 void *old_val
= prop
->value
;
139 ret
= prom_setprop(dp
->node
, (char *) name
, val
, len
);
142 prop
->value
= new_val
;
145 if (OF_IS_DYNAMIC(prop
))
148 OF_MARK_DYNAMIC(prop
);
154 prevp
= &(*prevp
)->next
;
156 write_unlock(&devtree_lock
);
158 /* XXX Upate procfs if necessary... */
162 EXPORT_SYMBOL(of_set_property
);
164 static unsigned int prom_early_allocated
;
166 static void * __init
prom_early_alloc(unsigned long size
)
170 ret
= __alloc_bootmem(size
, SMP_CACHE_BYTES
, 0UL);
172 memset(ret
, 0, size
);
174 prom_early_allocated
+= size
;
179 static int is_root_node(const struct device_node
*dp
)
184 return (dp
->parent
== NULL
);
187 /* The following routines deal with the black magic of fully naming a
190 * Certain well known named nodes are just the simple name string.
192 * Actual devices have an address specifier appended to the base name
193 * string, like this "foo@addr". The "addr" can be in any number of
194 * formats, and the platform plus the type of the node determine the
195 * format and how it is constructed.
197 * For children of the ROOT node, the naming convention is fixed and
198 * determined by whether this is a sun4u or sun4v system.
200 * For children of other nodes, it is bus type specific. So
201 * we walk up the tree until we discover a "device_type" property
202 * we recognize and we go from there.
204 static void __init
sparc32_path_component(struct device_node
*dp
, char *tmp_buf
)
206 struct linux_prom_registers
*regs
;
207 struct property
*rprop
;
209 rprop
= of_find_property(dp
, "reg", NULL
);
214 sprintf(tmp_buf
, "%s@%x,%x",
216 regs
->which_io
, regs
->phys_addr
);
219 /* "name@slot,offset" */
220 static void __init
sbus_path_component(struct device_node
*dp
, char *tmp_buf
)
222 struct linux_prom_registers
*regs
;
223 struct property
*prop
;
225 prop
= of_find_property(dp
, "reg", NULL
);
230 sprintf(tmp_buf
, "%s@%x,%x",
236 /* "name@devnum[,func]" */
237 static void __init
pci_path_component(struct device_node
*dp
, char *tmp_buf
)
239 struct linux_prom_pci_registers
*regs
;
240 struct property
*prop
;
243 prop
= of_find_property(dp
, "reg", NULL
);
248 devfn
= (regs
->phys_hi
>> 8) & 0xff;
250 sprintf(tmp_buf
, "%s@%x,%x",
255 sprintf(tmp_buf
, "%s@%x",
261 /* "name@addrhi,addrlo" */
262 static void __init
ebus_path_component(struct device_node
*dp
, char *tmp_buf
)
264 struct linux_prom_registers
*regs
;
265 struct property
*prop
;
267 prop
= of_find_property(dp
, "reg", NULL
);
273 sprintf(tmp_buf
, "%s@%x,%x",
275 regs
->which_io
, regs
->phys_addr
);
278 static void __init
__build_path_component(struct device_node
*dp
, char *tmp_buf
)
280 struct device_node
*parent
= dp
->parent
;
282 if (parent
!= NULL
) {
283 if (!strcmp(parent
->type
, "pci") ||
284 !strcmp(parent
->type
, "pciex"))
285 return pci_path_component(dp
, tmp_buf
);
286 if (!strcmp(parent
->type
, "sbus"))
287 return sbus_path_component(dp
, tmp_buf
);
288 if (!strcmp(parent
->type
, "ebus"))
289 return ebus_path_component(dp
, tmp_buf
);
291 /* "isa" is handled with platform naming */
294 /* Use platform naming convention. */
295 return sparc32_path_component(dp
, tmp_buf
);
298 static char * __init
build_path_component(struct device_node
*dp
)
300 char tmp_buf
[64], *n
;
303 __build_path_component(dp
, tmp_buf
);
304 if (tmp_buf
[0] == '\0')
305 strcpy(tmp_buf
, dp
->name
);
307 n
= prom_early_alloc(strlen(tmp_buf
) + 1);
313 static char * __init
build_full_name(struct device_node
*dp
)
315 int len
, ourlen
, plen
;
318 plen
= strlen(dp
->parent
->full_name
);
319 ourlen
= strlen(dp
->path_component_name
);
320 len
= ourlen
+ plen
+ 2;
322 n
= prom_early_alloc(len
);
323 strcpy(n
, dp
->parent
->full_name
);
324 if (!is_root_node(dp
->parent
)) {
325 strcpy(n
+ plen
, "/");
328 strcpy(n
+ plen
, dp
->path_component_name
);
333 static unsigned int unique_id
;
335 static struct property
* __init
build_one_prop(phandle node
, char *prev
, char *special_name
, void *special_val
, int special_len
)
337 static struct property
*tmp
= NULL
;
344 memset(p
, 0, sizeof(*p
) + 32);
347 p
= prom_early_alloc(sizeof(struct property
) + 32);
348 p
->unique_id
= unique_id
++;
351 p
->name
= (char *) (p
+ 1);
353 strcpy(p
->name
, special_name
);
354 p
->length
= special_len
;
355 p
->value
= prom_early_alloc(special_len
);
356 memcpy(p
->value
, special_val
, special_len
);
359 name
= prom_firstprop(node
, NULL
);
361 name
= prom_nextprop(node
, prev
, NULL
);
363 if (strlen(name
) == 0) {
367 strcpy(p
->name
, name
);
368 p
->length
= prom_getproplen(node
, p
->name
);
369 if (p
->length
<= 0) {
372 p
->value
= prom_early_alloc(p
->length
+ 1);
373 len
= prom_getproperty(node
, p
->name
, p
->value
,
377 ((unsigned char *)p
->value
)[p
->length
] = '\0';
383 static struct property
* __init
build_prop_list(phandle node
)
385 struct property
*head
, *tail
;
387 head
= tail
= build_one_prop(node
, NULL
,
388 ".node", &node
, sizeof(node
));
390 tail
->next
= build_one_prop(node
, NULL
, NULL
, NULL
, 0);
393 tail
->next
= build_one_prop(node
, tail
->name
,
401 static char * __init
get_one_property(phandle node
, char *name
)
403 char *buf
= "<NULL>";
406 len
= prom_getproplen(node
, name
);
408 buf
= prom_early_alloc(len
);
409 len
= prom_getproperty(node
, name
, buf
, len
);
415 static struct device_node
* __init
create_node(phandle node
)
417 struct device_node
*dp
;
422 dp
= prom_early_alloc(sizeof(*dp
));
423 dp
->unique_id
= unique_id
++;
425 kref_init(&dp
->kref
);
427 dp
->name
= get_one_property(node
, "name");
428 dp
->type
= get_one_property(node
, "device_type");
431 /* Build interrupts later... */
433 dp
->properties
= build_prop_list(node
);
438 static struct device_node
* __init
build_tree(struct device_node
*parent
, phandle node
, struct device_node
***nextp
)
440 struct device_node
*dp
;
442 dp
= create_node(node
);
445 *nextp
= &dp
->allnext
;
448 dp
->path_component_name
= build_path_component(dp
);
449 dp
->full_name
= build_full_name(dp
);
451 dp
->child
= build_tree(dp
, prom_getchild(node
), nextp
);
453 dp
->sibling
= build_tree(parent
, prom_getsibling(node
), nextp
);
459 void __init
prom_build_devicetree(void)
461 struct device_node
**nextp
;
463 allnodes
= create_node(prom_root_node
);
464 allnodes
->path_component_name
= "";
465 allnodes
->full_name
= "/";
467 nextp
= &allnodes
->allnext
;
468 allnodes
->child
= build_tree(allnodes
,
469 prom_getchild(allnodes
->node
),
471 printk("PROM: Built device tree with %u bytes of memory.\n",
472 prom_early_allocated
);