d64bb276f27b778a900c479e9ee6eed0ff3a73bc
[openwrt.git] / target / linux / olpc / files / arch / i386 / kernel / prom.c
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
3 *
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
6 *
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
9 *
10 * Adapted for sparc64 by David S. Miller davem@davemloft.net
11 *
12 * Adapter for i386/OLPC by Andres Salomon <dilinger@debian.org>
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 */
19
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/mm.h>
24 #include <linux/bootmem.h>
25 #include <linux/module.h>
26 #include <asm/prom.h>
27 #include <asm/ofw.h>
28
29 /*
30 * XXX: This is very much a stub; right now we're keeping 2 device trees
31 * in memory (one for promfs, and one here). That will not remain
32 * for long!
33 */
34
35 static struct device_node *allnodes;
36
37 /* use when traversing tree through the allnext, child, sibling,
38 * or parent members of struct device_node.
39 */
40 static DEFINE_RWLOCK(devtree_lock);
41
42 int of_device_is_compatible(const struct device_node *device,
43 const char *compat)
44 {
45 const char* cp;
46 int cplen, l;
47
48 cp = of_get_property(device, "compatible", &cplen);
49 if (cp == NULL)
50 return 0;
51 while (cplen > 0) {
52 if (strncmp(cp, compat, strlen(compat)) == 0)
53 return 1;
54 l = strlen(cp) + 1;
55 cp += l;
56 cplen -= l;
57 }
58
59 return 0;
60 }
61 EXPORT_SYMBOL(of_device_is_compatible);
62
63 struct device_node *of_get_parent(const struct device_node *node)
64 {
65 struct device_node *np;
66
67 if (!node)
68 return NULL;
69
70 np = node->parent;
71
72 return np;
73 }
74 EXPORT_SYMBOL(of_get_parent);
75
76 struct device_node *of_get_next_child(const struct device_node *node,
77 struct device_node *prev)
78 {
79 struct device_node *next;
80
81 next = prev ? prev->sibling : node->child;
82 for (; next != 0; next = next->sibling) {
83 break;
84 }
85
86 return next;
87 }
88 EXPORT_SYMBOL(of_get_next_child);
89
90 struct device_node *of_find_node_by_path(const char *path)
91 {
92 struct device_node *np = allnodes;
93
94 for (; np != 0; np = np->allnext) {
95 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
96 break;
97 }
98
99 return np;
100 }
101 EXPORT_SYMBOL(of_find_node_by_path);
102
103 struct device_node *of_find_node_by_phandle(phandle handle)
104 {
105 struct device_node *np;
106
107 for (np = allnodes; np != 0; np = np->allnext)
108 if (np->node == handle)
109 break;
110
111 return np;
112 }
113 EXPORT_SYMBOL(of_find_node_by_phandle);
114
115 struct device_node *of_find_node_by_name(struct device_node *from,
116 const char *name)
117 {
118 struct device_node *np;
119
120 np = from ? from->allnext : allnodes;
121 for (; np != NULL; np = np->allnext)
122 if (np->name != NULL && strcmp(np->name, name) == 0)
123 break;
124
125 return np;
126 }
127 EXPORT_SYMBOL(of_find_node_by_name);
128
129 struct device_node *of_find_node_by_type(struct device_node *from,
130 const char *type)
131 {
132 struct device_node *np;
133
134 np = from ? from->allnext : allnodes;
135 for (; np != 0; np = np->allnext)
136 if (np->type != 0 && strcmp(np->type, type) == 0)
137 break;
138
139 return np;
140 }
141 EXPORT_SYMBOL(of_find_node_by_type);
142
143 struct device_node *of_find_compatible_node(struct device_node *from,
144 const char *type, const char *compatible)
145 {
146 struct device_node *np;
147
148 np = from ? from->allnext : allnodes;
149 for (; np != 0; np = np->allnext) {
150 if (type != NULL
151 && !(np->type != 0 && strcmp(np->type, type) == 0))
152 continue;
153 if (of_device_is_compatible(np, compatible))
154 break;
155 }
156
157 return np;
158 }
159 EXPORT_SYMBOL(of_find_compatible_node);
160
161 struct property *of_find_property(const struct device_node *np,
162 const char *name,
163 int *lenp)
164 {
165 struct property *pp;
166
167 for (pp = np->properties; pp != 0; pp = pp->next) {
168 if (strcasecmp(pp->name, name) == 0) {
169 if (lenp != 0)
170 *lenp = pp->length;
171 break;
172 }
173 }
174 return pp;
175 }
176 EXPORT_SYMBOL(of_find_property);
177
178 /*
179 * Find a property with a given name for a given node
180 * and return the value.
181 */
182 const void *of_get_property(const struct device_node *np, const char *name,
183 int *lenp)
184 {
185 struct property *pp = of_find_property(np,name,lenp);
186 return pp ? pp->value : NULL;
187 }
188 EXPORT_SYMBOL(of_get_property);
189
190 int of_getintprop_default(struct device_node *np, const char *name, int def)
191 {
192 struct property *prop;
193 int len;
194
195 prop = of_find_property(np, name, &len);
196 if (!prop || len != 4)
197 return def;
198
199 return *(int *) prop->value;
200 }
201 EXPORT_SYMBOL(of_getintprop_default);
202
203 int of_n_addr_cells(struct device_node *np)
204 {
205 const int* ip;
206 do {
207 if (np->parent)
208 np = np->parent;
209 ip = of_get_property(np, "#address-cells", NULL);
210 if (ip != NULL)
211 return *ip;
212 } while (np->parent);
213 /* No #address-cells property for the root node, default to 2 */
214 return 2;
215 }
216 EXPORT_SYMBOL(of_n_addr_cells);
217
218 int of_n_size_cells(struct device_node *np)
219 {
220 const int* ip;
221 do {
222 if (np->parent)
223 np = np->parent;
224 ip = of_get_property(np, "#size-cells", NULL);
225 if (ip != NULL)
226 return *ip;
227 } while (np->parent);
228 /* No #size-cells property for the root node, default to 1 */
229 return 1;
230 }
231 EXPORT_SYMBOL(of_n_size_cells);
232
233 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
234 {
235 return -EIO;
236 }
237 EXPORT_SYMBOL(of_set_property);
238
239 static unsigned int prom_early_allocated;
240
241 static void * __init prom_early_alloc(unsigned long size)
242 {
243 void *ret;
244
245 ret = kmalloc(size, GFP_KERNEL);
246 if (ret != NULL)
247 memset(ret, 0, size);
248 else
249 printk(KERN_ERR "ACK! couldn't allocate prom memory!\n");
250
251 prom_early_allocated += size;
252
253 return ret;
254 }
255
256 static int is_root_node(const struct device_node *dp)
257 {
258 if (!dp)
259 return 0;
260
261 return (dp->parent == NULL);
262 }
263
264 static char * __init build_path_component(struct device_node *dp)
265 {
266 int pathlen;
267 char *n, *i;
268
269 if (ofw("package-to-path", 3, 1, dp->node, NULL, 0, &pathlen)) {
270 printk(KERN_ERR "PROM: unable to get path name from OFW!\n");
271 return "ERROR";
272 }
273 n = prom_early_alloc(pathlen + 1);
274 if (ofw("package-to-path", 3, 1, dp->node, n, pathlen+1, &pathlen))
275 printk(KERN_ERR "PROM: unable to get path name from OFW\n");
276
277 if ((i = strrchr(n, '/')))
278 n = ++i; /* we only want the file name */
279 return n;
280 }
281
282 static char * __init build_full_name(struct device_node *dp)
283 {
284 int len, ourlen, plen;
285 char *n;
286
287 plen = strlen(dp->parent->full_name);
288 ourlen = strlen(dp->path_component_name);
289 len = ourlen + plen + 2;
290
291 n = prom_early_alloc(len);
292 strcpy(n, dp->parent->full_name);
293 if (!is_root_node(dp->parent)) {
294 strcpy(n + plen, "/");
295 plen++;
296 }
297 strcpy(n + plen, dp->path_component_name);
298
299 return n;
300 }
301
302 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
303 {
304 static struct property *tmp = NULL;
305 struct property *p;
306
307 if (tmp) {
308 p = tmp;
309 memset(p, 0, sizeof(*p) + 32);
310 tmp = NULL;
311 } else {
312 p = prom_early_alloc(sizeof(struct property) + 32);
313 }
314
315 p->name = (char *) (p + 1);
316 if (special_name) {
317 strcpy(p->name, special_name);
318 p->length = special_len;
319 p->value = prom_early_alloc(special_len);
320 memcpy(p->value, special_val, special_len);
321 } else {
322 int fl;
323 if (prev == NULL) {
324 if (ofw("nextprop", 3, 1, node, "", p->name, &fl)) {
325 printk(KERN_ERR "PROM: %s: nextprop failed!\n", __func__);
326 return NULL;
327 }
328 } else {
329 if (ofw("nextprop", 3, 1, node, prev, p->name, &fl)) {
330 printk(KERN_ERR "PROM: %s: nextprop failed!\n", __func__);
331 return NULL;
332 }
333 }
334 if (strlen(p->name) == 0 || fl != 1) {
335 tmp = p;
336 return NULL;
337 }
338 if (ofw("getproplen", 2, 1, node, p->name, &p->length)) {
339 printk(KERN_ERR "PROM: %s: getproplen failed!\n", __func__);
340 return NULL;
341 }
342 if (p->length <= 0) {
343 p->length = 0;
344 } else {
345 p->value = prom_early_alloc(p->length + 1);
346 if (ofw("getprop", 4, 1, node, p->name, p->value, p->length, &p->length)) {
347 printk(KERN_ERR "PROM: %s: getprop failed!\n", __func__);
348 return NULL;
349 }
350 ((unsigned char *)p->value)[p->length] = '\0';
351 }
352 }
353 return p;
354 }
355
356 static struct property * __init build_prop_list(phandle node)
357 {
358 struct property *head, *tail;
359
360 head = tail = build_one_prop(node, NULL,
361 ".node", &node, sizeof(node));
362
363 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
364 tail = tail->next;
365 while(tail) {
366 tail->next = build_one_prop(node, tail->name,
367 NULL, NULL, 0);
368 tail = tail->next;
369 }
370
371 return head;
372 }
373
374 static char * __init get_one_property(phandle node, const char *name)
375 {
376 char *buf = "<NULL>";
377 int len;
378
379 if (ofw("getproplen", 2, 1, node, name, &len)) {
380 printk(KERN_ERR "PROM: %s: getproplen failed!\n", __func__);
381 return NULL;
382 }
383 if (len > 0) {
384 buf = prom_early_alloc(len);
385 if (ofw("getprop", 4, 1, node, name, buf, len, &len)) {
386 printk(KERN_ERR "PROM: %s: getprop failed!\n", __func__);
387 return NULL;
388 }
389 }
390
391 return buf;
392 }
393
394 static struct device_node * __init create_node(phandle node, struct device_node *parent)
395 {
396 struct device_node *dp;
397
398 if (!node)
399 return NULL;
400
401 dp = prom_early_alloc(sizeof(*dp));
402 dp->parent = parent;
403
404 kref_init(&dp->kref);
405
406 dp->name = get_one_property(node, "name");
407 dp->type = get_one_property(node, "device_type");
408 dp->node = node;
409
410 dp->properties = build_prop_list(node);
411
412 return dp;
413 }
414
415 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
416 {
417 struct device_node *ret = NULL, *prev_sibling = NULL;
418 struct device_node *dp;
419 u32 child;
420
421 while (1) {
422 dp = create_node(node, parent);
423 if (!dp)
424 break;
425
426 if (prev_sibling)
427 prev_sibling->sibling = dp;
428
429 if (!ret)
430 ret = dp;
431 prev_sibling = dp;
432
433 *(*nextp) = dp;
434 *nextp = &dp->allnext;
435
436 dp->path_component_name = build_path_component(dp);
437 dp->full_name = build_full_name(dp);
438
439 if (ofw("child", 1, 1, node, &child)) {
440 printk(KERN_ERR "PROM: %s: fetching child failed!\n", __func__);
441 return NULL;
442 }
443 dp->child = build_tree(dp, child, nextp);
444
445 if (ofw("peer", 1, 1, node, &node)) {
446 printk(KERN_ERR "PROM: %s: fetching peer failed!\n", __func__);
447 return NULL;
448 }
449 }
450
451 return ret;
452 }
453
454 static phandle root_node;
455
456 void __init prom_build_devicetree(void)
457 {
458 struct device_node **nextp;
459 u32 child;
460
461 if (ofw("peer", 1, 1, 0, &root_node)) {
462 printk(KERN_ERR "PROM: unable to get root node from OFW!\n");
463 return;
464 }
465
466 allnodes = create_node(root_node, NULL);
467 allnodes->path_component_name = "";
468 allnodes->full_name = "/";
469
470 nextp = &allnodes->allnext;
471 if (ofw("child", 1, 1, allnodes->node, &child)) {
472 printk(KERN_ERR "PROM: unable to get child node from OFW!\n");
473 return;
474 }
475 allnodes->child = build_tree(allnodes, child, &nextp);
476 printk("PROM: Built device tree with %u bytes of memory.\n",
477 prom_early_allocated);
478 }
This page took 0.059583 seconds and 3 git commands to generate.