diff options
Diffstat (limited to 'arch/sparc/kernel/prom_common.c')
-rw-r--r-- | arch/sparc/kernel/prom_common.c | 326 |
1 files changed, 326 insertions, 0 deletions
diff --git a/arch/sparc/kernel/prom_common.c b/arch/sparc/kernel/prom_common.c new file mode 100644 index 000000000000..4e9af593db49 --- /dev/null +++ b/arch/sparc/kernel/prom_common.c | |||
@@ -0,0 +1,326 @@ | |||
1 | /* prom_common.c: OF device tree support common code. | ||
2 | * | ||
3 | * Paul Mackerras August 1996. | ||
4 | * Copyright (C) 1996-2005 Paul Mackerras. | ||
5 | * | ||
6 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. | ||
7 | * {engebret|bergner}@us.ibm.com | ||
8 | * | ||
9 | * Adapted for sparc by David S. Miller davem@davemloft.net | ||
10 | * | ||
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. | ||
15 | */ | ||
16 | |||
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> | ||
22 | #include <linux/of.h> | ||
23 | #include <asm/prom.h> | ||
24 | #include <asm/oplib.h> | ||
25 | |||
26 | #include "prom.h" | ||
27 | |||
28 | struct device_node *of_console_device; | ||
29 | EXPORT_SYMBOL(of_console_device); | ||
30 | |||
31 | char *of_console_path; | ||
32 | EXPORT_SYMBOL(of_console_path); | ||
33 | |||
34 | char *of_console_options; | ||
35 | EXPORT_SYMBOL(of_console_options); | ||
36 | |||
37 | struct device_node *of_find_node_by_phandle(phandle handle) | ||
38 | { | ||
39 | struct device_node *np; | ||
40 | |||
41 | for (np = allnodes; np; np = np->allnext) | ||
42 | if (np->node == handle) | ||
43 | break; | ||
44 | |||
45 | return np; | ||
46 | } | ||
47 | EXPORT_SYMBOL(of_find_node_by_phandle); | ||
48 | |||
49 | int of_getintprop_default(struct device_node *np, const char *name, int def) | ||
50 | { | ||
51 | struct property *prop; | ||
52 | int len; | ||
53 | |||
54 | prop = of_find_property(np, name, &len); | ||
55 | if (!prop || len != 4) | ||
56 | return def; | ||
57 | |||
58 | return *(int *) prop->value; | ||
59 | } | ||
60 | EXPORT_SYMBOL(of_getintprop_default); | ||
61 | |||
62 | DEFINE_MUTEX(of_set_property_mutex); | ||
63 | EXPORT_SYMBOL(of_set_property_mutex); | ||
64 | |||
65 | int of_set_property(struct device_node *dp, const char *name, void *val, int len) | ||
66 | { | ||
67 | struct property **prevp; | ||
68 | void *new_val; | ||
69 | int err; | ||
70 | |||
71 | new_val = kmalloc(len, GFP_KERNEL); | ||
72 | if (!new_val) | ||
73 | return -ENOMEM; | ||
74 | |||
75 | memcpy(new_val, val, len); | ||
76 | |||
77 | err = -ENODEV; | ||
78 | |||
79 | write_lock(&devtree_lock); | ||
80 | prevp = &dp->properties; | ||
81 | while (*prevp) { | ||
82 | struct property *prop = *prevp; | ||
83 | |||
84 | if (!strcasecmp(prop->name, name)) { | ||
85 | void *old_val = prop->value; | ||
86 | int ret; | ||
87 | |||
88 | mutex_lock(&of_set_property_mutex); | ||
89 | ret = prom_setprop(dp->node, name, val, len); | ||
90 | mutex_unlock(&of_set_property_mutex); | ||
91 | |||
92 | err = -EINVAL; | ||
93 | if (ret >= 0) { | ||
94 | prop->value = new_val; | ||
95 | prop->length = len; | ||
96 | |||
97 | if (OF_IS_DYNAMIC(prop)) | ||
98 | kfree(old_val); | ||
99 | |||
100 | OF_MARK_DYNAMIC(prop); | ||
101 | |||
102 | err = 0; | ||
103 | } | ||
104 | break; | ||
105 | } | ||
106 | prevp = &(*prevp)->next; | ||
107 | } | ||
108 | write_unlock(&devtree_lock); | ||
109 | |||
110 | /* XXX Upate procfs if necessary... */ | ||
111 | |||
112 | return err; | ||
113 | } | ||
114 | EXPORT_SYMBOL(of_set_property); | ||
115 | |||
116 | int of_find_in_proplist(const char *list, const char *match, int len) | ||
117 | { | ||
118 | while (len > 0) { | ||
119 | int l; | ||
120 | |||
121 | if (!strcmp(list, match)) | ||
122 | return 1; | ||
123 | l = strlen(list) + 1; | ||
124 | list += l; | ||
125 | len -= l; | ||
126 | } | ||
127 | return 0; | ||
128 | } | ||
129 | EXPORT_SYMBOL(of_find_in_proplist); | ||
130 | |||
131 | unsigned int prom_unique_id; | ||
132 | |||
133 | static struct property * __init build_one_prop(phandle node, char *prev, | ||
134 | char *special_name, | ||
135 | void *special_val, | ||
136 | int special_len) | ||
137 | { | ||
138 | static struct property *tmp = NULL; | ||
139 | struct property *p; | ||
140 | const char *name; | ||
141 | |||
142 | if (tmp) { | ||
143 | p = tmp; | ||
144 | memset(p, 0, sizeof(*p) + 32); | ||
145 | tmp = NULL; | ||
146 | } else { | ||
147 | p = prom_early_alloc(sizeof(struct property) + 32); | ||
148 | p->unique_id = prom_unique_id++; | ||
149 | } | ||
150 | |||
151 | p->name = (char *) (p + 1); | ||
152 | if (special_name) { | ||
153 | strcpy(p->name, special_name); | ||
154 | p->length = special_len; | ||
155 | p->value = prom_early_alloc(special_len); | ||
156 | memcpy(p->value, special_val, special_len); | ||
157 | } else { | ||
158 | #ifdef CONFIG_SPARC32 | ||
159 | if (prev == NULL) { | ||
160 | name = prom_firstprop(node, NULL); | ||
161 | } else { | ||
162 | name = prom_nextprop(node, prev, NULL); | ||
163 | } | ||
164 | #else | ||
165 | if (prev == NULL) { | ||
166 | prom_firstprop(node, p->name); | ||
167 | } else { | ||
168 | prom_nextprop(node, prev, p->name); | ||
169 | } | ||
170 | name = p->name; | ||
171 | #endif | ||
172 | if (strlen(name) == 0) { | ||
173 | tmp = p; | ||
174 | return NULL; | ||
175 | } | ||
176 | #ifdef CONFIG_SPARC32 | ||
177 | strcpy(p->name, name); | ||
178 | #endif | ||
179 | p->length = prom_getproplen(node, p->name); | ||
180 | if (p->length <= 0) { | ||
181 | p->length = 0; | ||
182 | } else { | ||
183 | int len; | ||
184 | |||
185 | p->value = prom_early_alloc(p->length + 1); | ||
186 | len = prom_getproperty(node, p->name, p->value, | ||
187 | p->length); | ||
188 | if (len <= 0) | ||
189 | p->length = 0; | ||
190 | ((unsigned char *)p->value)[p->length] = '\0'; | ||
191 | } | ||
192 | } | ||
193 | return p; | ||
194 | } | ||
195 | |||
196 | static struct property * __init build_prop_list(phandle node) | ||
197 | { | ||
198 | struct property *head, *tail; | ||
199 | |||
200 | head = tail = build_one_prop(node, NULL, | ||
201 | ".node", &node, sizeof(node)); | ||
202 | |||
203 | tail->next = build_one_prop(node, NULL, NULL, NULL, 0); | ||
204 | tail = tail->next; | ||
205 | while(tail) { | ||
206 | tail->next = build_one_prop(node, tail->name, | ||
207 | NULL, NULL, 0); | ||
208 | tail = tail->next; | ||
209 | } | ||
210 | |||
211 | return head; | ||
212 | } | ||
213 | |||
214 | static char * __init get_one_property(phandle node, const char *name) | ||
215 | { | ||
216 | char *buf = "<NULL>"; | ||
217 | int len; | ||
218 | |||
219 | len = prom_getproplen(node, name); | ||
220 | if (len > 0) { | ||
221 | buf = prom_early_alloc(len); | ||
222 | len = prom_getproperty(node, name, buf, len); | ||
223 | } | ||
224 | |||
225 | return buf; | ||
226 | } | ||
227 | |||
228 | static struct device_node * __init prom_create_node(phandle node, | ||
229 | struct device_node *parent) | ||
230 | { | ||
231 | struct device_node *dp; | ||
232 | |||
233 | if (!node) | ||
234 | return NULL; | ||
235 | |||
236 | dp = prom_early_alloc(sizeof(*dp)); | ||
237 | dp->unique_id = prom_unique_id++; | ||
238 | dp->parent = parent; | ||
239 | |||
240 | kref_init(&dp->kref); | ||
241 | |||
242 | dp->name = get_one_property(node, "name"); | ||
243 | dp->type = get_one_property(node, "device_type"); | ||
244 | dp->node = node; | ||
245 | |||
246 | dp->properties = build_prop_list(node); | ||
247 | |||
248 | irq_trans_init(dp); | ||
249 | |||
250 | return dp; | ||
251 | } | ||
252 | |||
253 | static char * __init build_full_name(struct device_node *dp) | ||
254 | { | ||
255 | int len, ourlen, plen; | ||
256 | char *n; | ||
257 | |||
258 | plen = strlen(dp->parent->full_name); | ||
259 | ourlen = strlen(dp->path_component_name); | ||
260 | len = ourlen + plen + 2; | ||
261 | |||
262 | n = prom_early_alloc(len); | ||
263 | strcpy(n, dp->parent->full_name); | ||
264 | if (!is_root_node(dp->parent)) { | ||
265 | strcpy(n + plen, "/"); | ||
266 | plen++; | ||
267 | } | ||
268 | strcpy(n + plen, dp->path_component_name); | ||
269 | |||
270 | return n; | ||
271 | } | ||
272 | |||
273 | static struct device_node * __init prom_build_tree(struct device_node *parent, | ||
274 | phandle node, | ||
275 | struct device_node ***nextp) | ||
276 | { | ||
277 | struct device_node *ret = NULL, *prev_sibling = NULL; | ||
278 | struct device_node *dp; | ||
279 | |||
280 | while (1) { | ||
281 | dp = prom_create_node(node, parent); | ||
282 | if (!dp) | ||
283 | break; | ||
284 | |||
285 | if (prev_sibling) | ||
286 | prev_sibling->sibling = dp; | ||
287 | |||
288 | if (!ret) | ||
289 | ret = dp; | ||
290 | prev_sibling = dp; | ||
291 | |||
292 | *(*nextp) = dp; | ||
293 | *nextp = &dp->allnext; | ||
294 | |||
295 | dp->path_component_name = build_path_component(dp); | ||
296 | dp->full_name = build_full_name(dp); | ||
297 | |||
298 | dp->child = prom_build_tree(dp, prom_getchild(node), nextp); | ||
299 | |||
300 | node = prom_getsibling(node); | ||
301 | } | ||
302 | |||
303 | return ret; | ||
304 | } | ||
305 | |||
306 | unsigned int prom_early_allocated __initdata; | ||
307 | |||
308 | void __init prom_build_devicetree(void) | ||
309 | { | ||
310 | struct device_node **nextp; | ||
311 | |||
312 | allnodes = prom_create_node(prom_root_node, NULL); | ||
313 | allnodes->path_component_name = ""; | ||
314 | allnodes->full_name = "/"; | ||
315 | |||
316 | nextp = &allnodes->allnext; | ||
317 | allnodes->child = prom_build_tree(allnodes, | ||
318 | prom_getchild(allnodes->node), | ||
319 | &nextp); | ||
320 | of_console_init(); | ||
321 | |||
322 | printk("PROM: Built device tree with %u bytes of memory.\n", | ||
323 | prom_early_allocated); | ||
324 | |||
325 | of_fill_in_cpu_data(); | ||
326 | } | ||