1 /* prom_common.c: OF device tree support common code.
3 * Paul Mackerras August 1996.
4 * Copyright (C) 1996-2005 Paul Mackerras.
6 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
7 * {engebret|bergner}@us.ibm.com
9 * Adapted for sparc by David S. Miller davem@davemloft.net
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
24 #include <asm/oplib.h>
29 void (*prom_build_more
)(struct device_node
*dp
, struct device_node
***nextp
);
31 struct device_node
*of_console_device
;
32 EXPORT_SYMBOL(of_console_device
);
34 char *of_console_path
;
35 EXPORT_SYMBOL(of_console_path
);
37 char *of_console_options
;
38 EXPORT_SYMBOL(of_console_options
);
40 int of_getintprop_default(struct device_node
*np
, const char *name
, int def
)
42 struct property
*prop
;
45 prop
= of_find_property(np
, name
, &len
);
46 if (!prop
|| len
!= 4)
49 return *(int *) prop
->value
;
51 EXPORT_SYMBOL(of_getintprop_default
);
53 DEFINE_MUTEX(of_set_property_mutex
);
54 EXPORT_SYMBOL(of_set_property_mutex
);
56 int of_set_property(struct device_node
*dp
, const char *name
, void *val
, int len
)
58 struct property
**prevp
;
62 new_val
= kmalloc(len
, GFP_KERNEL
);
66 memcpy(new_val
, val
, len
);
70 mutex_lock(&of_set_property_mutex
);
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
->phandle
, name
, val
, len
);
84 prop
->value
= new_val
;
87 if (OF_IS_DYNAMIC(prop
))
90 OF_MARK_DYNAMIC(prop
);
96 prevp
= &(*prevp
)->next
;
98 write_unlock(&devtree_lock
);
99 mutex_unlock(&of_set_property_mutex
);
101 /* XXX Upate procfs if necessary... */
105 EXPORT_SYMBOL(of_set_property
);
107 int of_find_in_proplist(const char *list
, const char *match
, int len
)
112 if (!strcmp(list
, match
))
114 l
= strlen(list
) + 1;
120 EXPORT_SYMBOL(of_find_in_proplist
);
122 unsigned int prom_unique_id
;
124 static struct property
* __init
build_one_prop(phandle node
, char *prev
,
129 static struct property
*tmp
= NULL
;
135 memset(p
, 0, sizeof(*p
) + 32);
138 p
= prom_early_alloc(sizeof(struct property
) + 32);
139 p
->unique_id
= prom_unique_id
++;
142 p
->name
= (char *) (p
+ 1);
144 strcpy(p
->name
, special_name
);
145 p
->length
= special_len
;
146 p
->value
= prom_early_alloc(special_len
);
147 memcpy(p
->value
, special_val
, special_len
);
150 name
= prom_firstprop(node
, p
->name
);
152 name
= prom_nextprop(node
, prev
, p
->name
);
155 if (!name
|| strlen(name
) == 0) {
159 #ifdef CONFIG_SPARC32
160 strcpy(p
->name
, name
);
162 p
->length
= prom_getproplen(node
, p
->name
);
163 if (p
->length
<= 0) {
168 p
->value
= prom_early_alloc(p
->length
+ 1);
169 len
= prom_getproperty(node
, p
->name
, p
->value
,
173 ((unsigned char *)p
->value
)[p
->length
] = '\0';
179 static struct property
* __init
build_prop_list(phandle node
)
181 struct property
*head
, *tail
;
183 head
= tail
= build_one_prop(node
, NULL
,
184 ".node", &node
, sizeof(node
));
186 tail
->next
= build_one_prop(node
, NULL
, NULL
, NULL
, 0);
189 tail
->next
= build_one_prop(node
, tail
->name
,
197 static char * __init
get_one_property(phandle node
, const char *name
)
199 char *buf
= "<NULL>";
202 len
= prom_getproplen(node
, name
);
204 buf
= prom_early_alloc(len
);
205 len
= prom_getproperty(node
, name
, buf
, len
);
211 static struct device_node
* __init
prom_create_node(phandle node
,
212 struct device_node
*parent
)
214 struct device_node
*dp
;
219 dp
= prom_early_alloc(sizeof(*dp
));
220 dp
->unique_id
= prom_unique_id
++;
223 kref_init(&dp
->kref
);
225 dp
->name
= get_one_property(node
, "name");
226 dp
->type
= get_one_property(node
, "device_type");
229 dp
->properties
= build_prop_list(node
);
236 char * __init
build_full_name(struct device_node
*dp
)
238 int len
, ourlen
, plen
;
241 plen
= strlen(dp
->parent
->full_name
);
242 ourlen
= strlen(dp
->path_component_name
);
243 len
= ourlen
+ plen
+ 2;
245 n
= prom_early_alloc(len
);
246 strcpy(n
, dp
->parent
->full_name
);
247 if (!is_root_node(dp
->parent
)) {
248 strcpy(n
+ plen
, "/");
251 strcpy(n
+ plen
, dp
->path_component_name
);
256 static struct device_node
* __init
prom_build_tree(struct device_node
*parent
,
258 struct device_node
***nextp
)
260 struct device_node
*ret
= NULL
, *prev_sibling
= NULL
;
261 struct device_node
*dp
;
264 dp
= prom_create_node(node
, parent
);
269 prev_sibling
->sibling
= dp
;
276 *nextp
= &dp
->allnext
;
278 dp
->path_component_name
= build_path_component(dp
);
279 dp
->full_name
= build_full_name(dp
);
281 dp
->child
= prom_build_tree(dp
, prom_getchild(node
), nextp
);
284 prom_build_more(dp
, nextp
);
286 node
= prom_getsibling(node
);
292 unsigned int prom_early_allocated __initdata
;
294 void __init
prom_build_devicetree(void)
296 struct device_node
**nextp
;
298 allnodes
= prom_create_node(prom_root_node
, NULL
);
299 allnodes
->path_component_name
= "";
300 allnodes
->full_name
= "/";
302 nextp
= &allnodes
->allnext
;
303 allnodes
->child
= prom_build_tree(allnodes
,
304 prom_getchild(allnodes
->phandle
),
308 printk("PROM: Built device tree with %u bytes of memory.\n",
309 prom_early_allocated
);