Consolidate of_find_property
[linux-2.6/mini2440.git] / arch / sparc / kernel / prom.c
blob0f5aab4326b3e7bf1d6f995e1a90d7d53079bc27
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
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
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>
21 #include <linux/mm.h>
22 #include <linux/bootmem.h>
23 #include <linux/module.h>
25 #include <asm/prom.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;
36 if (!node)
37 return NULL;
39 np = node->parent;
41 return 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) {
52 break;
55 return next;
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)
65 break;
68 return np;
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)
78 break;
80 return np;
82 EXPORT_SYMBOL(of_find_node_by_phandle);
84 struct device_node *of_find_node_by_name(struct device_node *from,
85 const char *name)
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)
92 break;
94 return np;
96 EXPORT_SYMBOL(of_find_node_by_name);
98 struct device_node *of_find_node_by_type(struct device_node *from,
99 const char *type)
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)
106 break;
108 return np;
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) {
119 if (type != NULL
120 && !(np->type != 0 && strcmp(np->type, type) == 0))
121 continue;
122 if (of_device_is_compatible(np, compatible))
123 break;
126 return np;
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;
133 int len;
135 prop = of_find_property(np, name, &len);
136 if (!prop || len != 4)
137 return def;
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;
146 void *new_val;
147 int err;
149 new_val = kmalloc(len, GFP_KERNEL);
150 if (!new_val)
151 return -ENOMEM;
153 memcpy(new_val, val, len);
155 err = -ENODEV;
157 write_lock(&devtree_lock);
158 prevp = &dp->properties;
159 while (*prevp) {
160 struct property *prop = *prevp;
162 if (!strcasecmp(prop->name, name)) {
163 void *old_val = prop->value;
164 int ret;
166 ret = prom_setprop(dp->node, (char *) name, val, len);
167 err = -EINVAL;
168 if (ret >= 0) {
169 prop->value = new_val;
170 prop->length = len;
172 if (OF_IS_DYNAMIC(prop))
173 kfree(old_val);
175 OF_MARK_DYNAMIC(prop);
177 err = 0;
179 break;
181 prevp = &(*prevp)->next;
183 write_unlock(&devtree_lock);
185 /* XXX Upate procfs if necessary... */
187 return err;
189 EXPORT_SYMBOL(of_set_property);
191 static unsigned int prom_early_allocated;
193 static void * __init prom_early_alloc(unsigned long size)
195 void *ret;
197 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
198 if (ret != NULL)
199 memset(ret, 0, size);
201 prom_early_allocated += size;
203 return ret;
206 static int is_root_node(const struct device_node *dp)
208 if (!dp)
209 return 0;
211 return (dp->parent == NULL);
214 /* The following routines deal with the black magic of fully naming a
215 * node.
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);
237 if (!rprop)
238 return;
240 regs = rprop->value;
241 sprintf(tmp_buf, "%s@%x,%x",
242 dp->name,
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);
253 if (!prop)
254 return;
256 regs = prop->value;
257 sprintf(tmp_buf, "%s@%x,%x",
258 dp->name,
259 regs->which_io,
260 regs->phys_addr);
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;
268 unsigned int devfn;
270 prop = of_find_property(dp, "reg", NULL);
271 if (!prop)
272 return;
274 regs = prop->value;
275 devfn = (regs->phys_hi >> 8) & 0xff;
276 if (devfn & 0x07) {
277 sprintf(tmp_buf, "%s@%x,%x",
278 dp->name,
279 devfn >> 3,
280 devfn & 0x07);
281 } else {
282 sprintf(tmp_buf, "%s@%x",
283 dp->name,
284 devfn >> 3);
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);
295 if (!prop)
296 return;
298 regs = prop->value;
300 sprintf(tmp_buf, "%s@%x,%x",
301 dp->name,
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;
329 tmp_buf[0] = '\0';
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);
335 strcpy(n, tmp_buf);
337 return n;
340 static char * __init build_full_name(struct device_node *dp)
342 int len, ourlen, plen;
343 char *n;
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, "/");
353 plen++;
355 strcpy(n + plen, dp->path_component_name);
357 return n;
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;
365 struct property *p;
366 int len;
367 const char *name;
369 if (tmp) {
370 p = tmp;
371 memset(p, 0, sizeof(*p) + 32);
372 tmp = NULL;
373 } else {
374 p = prom_early_alloc(sizeof(struct property) + 32);
375 p->unique_id = unique_id++;
378 p->name = (char *) (p + 1);
379 if (special_name) {
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);
384 } else {
385 if (prev == NULL) {
386 name = prom_firstprop(node, NULL);
387 } else {
388 name = prom_nextprop(node, prev, NULL);
390 if (strlen(name) == 0) {
391 tmp = p;
392 return NULL;
394 strcpy(p->name, name);
395 p->length = prom_getproplen(node, p->name);
396 if (p->length <= 0) {
397 p->length = 0;
398 } else {
399 p->value = prom_early_alloc(p->length + 1);
400 len = prom_getproperty(node, p->name, p->value,
401 p->length);
402 if (len <= 0)
403 p->length = 0;
404 ((unsigned char *)p->value)[p->length] = '\0';
407 return p;
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);
418 tail = tail->next;
419 while(tail) {
420 tail->next = build_one_prop(node, tail->name,
421 NULL, NULL, 0);
422 tail = tail->next;
425 return head;
428 static char * __init get_one_property(phandle node, char *name)
430 char *buf = "<NULL>";
431 int len;
433 len = prom_getproplen(node, name);
434 if (len > 0) {
435 buf = prom_early_alloc(len);
436 len = prom_getproperty(node, name, buf, len);
439 return buf;
442 static struct device_node * __init create_node(phandle node)
444 struct device_node *dp;
446 if (!node)
447 return NULL;
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");
456 dp->node = node;
458 /* Build interrupts later... */
460 dp->properties = build_prop_list(node);
462 return dp;
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);
470 if (dp) {
471 *(*nextp) = dp;
472 *nextp = &dp->allnext;
474 dp->parent = parent;
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);
483 return dp;
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),
497 &nextp);
498 printk("PROM: Built device tree with %u bytes of memory.\n",
499 prom_early_allocated);