allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / sparc / kernel / prom.c
blobeed140b3c7397caf4abe0bd204df9bbb45dd25df
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 /* use when traversing tree through the allnext, child, sibling,
31 * or parent members of struct device_node.
33 static DEFINE_RWLOCK(devtree_lock);
35 int of_device_is_compatible(const struct device_node *device,
36 const char *compat)
38 const char* cp;
39 int cplen, l;
41 cp = of_get_property(device, "compatible", &cplen);
42 if (cp == NULL)
43 return 0;
44 while (cplen > 0) {
45 if (strncmp(cp, compat, strlen(compat)) == 0)
46 return 1;
47 l = strlen(cp) + 1;
48 cp += l;
49 cplen -= l;
52 return 0;
54 EXPORT_SYMBOL(of_device_is_compatible);
56 struct device_node *of_get_parent(const struct device_node *node)
58 struct device_node *np;
60 if (!node)
61 return NULL;
63 np = node->parent;
65 return np;
67 EXPORT_SYMBOL(of_get_parent);
69 struct device_node *of_get_next_child(const struct device_node *node,
70 struct device_node *prev)
72 struct device_node *next;
74 next = prev ? prev->sibling : node->child;
75 for (; next != 0; next = next->sibling) {
76 break;
79 return next;
81 EXPORT_SYMBOL(of_get_next_child);
83 struct device_node *of_find_node_by_path(const char *path)
85 struct device_node *np = allnodes;
87 for (; np != 0; np = np->allnext) {
88 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
89 break;
92 return np;
94 EXPORT_SYMBOL(of_find_node_by_path);
96 struct device_node *of_find_node_by_phandle(phandle handle)
98 struct device_node *np;
100 for (np = allnodes; np != 0; np = np->allnext)
101 if (np->node == handle)
102 break;
104 return np;
106 EXPORT_SYMBOL(of_find_node_by_phandle);
108 struct device_node *of_find_node_by_name(struct device_node *from,
109 const char *name)
111 struct device_node *np;
113 np = from ? from->allnext : allnodes;
114 for (; np != NULL; np = np->allnext)
115 if (np->name != NULL && strcmp(np->name, name) == 0)
116 break;
118 return np;
120 EXPORT_SYMBOL(of_find_node_by_name);
122 struct device_node *of_find_node_by_type(struct device_node *from,
123 const char *type)
125 struct device_node *np;
127 np = from ? from->allnext : allnodes;
128 for (; np != 0; np = np->allnext)
129 if (np->type != 0 && strcmp(np->type, type) == 0)
130 break;
132 return np;
134 EXPORT_SYMBOL(of_find_node_by_type);
136 struct device_node *of_find_compatible_node(struct device_node *from,
137 const char *type, const char *compatible)
139 struct device_node *np;
141 np = from ? from->allnext : allnodes;
142 for (; np != 0; np = np->allnext) {
143 if (type != NULL
144 && !(np->type != 0 && strcmp(np->type, type) == 0))
145 continue;
146 if (of_device_is_compatible(np, compatible))
147 break;
150 return np;
152 EXPORT_SYMBOL(of_find_compatible_node);
154 struct property *of_find_property(const struct device_node *np,
155 const char *name,
156 int *lenp)
158 struct property *pp;
160 for (pp = np->properties; pp != 0; pp = pp->next) {
161 if (strcasecmp(pp->name, name) == 0) {
162 if (lenp != 0)
163 *lenp = pp->length;
164 break;
167 return pp;
169 EXPORT_SYMBOL(of_find_property);
172 * Find a property with a given name for a given node
173 * and return the value.
175 const void *of_get_property(const struct device_node *np, const char *name,
176 int *lenp)
178 struct property *pp = of_find_property(np,name,lenp);
179 return pp ? pp->value : NULL;
181 EXPORT_SYMBOL(of_get_property);
183 int of_getintprop_default(struct device_node *np, const char *name, int def)
185 struct property *prop;
186 int len;
188 prop = of_find_property(np, name, &len);
189 if (!prop || len != 4)
190 return def;
192 return *(int *) prop->value;
194 EXPORT_SYMBOL(of_getintprop_default);
196 int of_n_addr_cells(struct device_node *np)
198 const int* ip;
199 do {
200 if (np->parent)
201 np = np->parent;
202 ip = of_get_property(np, "#address-cells", NULL);
203 if (ip != NULL)
204 return *ip;
205 } while (np->parent);
206 /* No #address-cells property for the root node, default to 2 */
207 return 2;
209 EXPORT_SYMBOL(of_n_addr_cells);
211 int of_n_size_cells(struct device_node *np)
213 const int* ip;
214 do {
215 if (np->parent)
216 np = np->parent;
217 ip = of_get_property(np, "#size-cells", NULL);
218 if (ip != NULL)
219 return *ip;
220 } while (np->parent);
221 /* No #size-cells property for the root node, default to 1 */
222 return 1;
224 EXPORT_SYMBOL(of_n_size_cells);
226 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
228 struct property **prevp;
229 void *new_val;
230 int err;
232 new_val = kmalloc(len, GFP_KERNEL);
233 if (!new_val)
234 return -ENOMEM;
236 memcpy(new_val, val, len);
238 err = -ENODEV;
240 write_lock(&devtree_lock);
241 prevp = &dp->properties;
242 while (*prevp) {
243 struct property *prop = *prevp;
245 if (!strcasecmp(prop->name, name)) {
246 void *old_val = prop->value;
247 int ret;
249 ret = prom_setprop(dp->node, (char *) name, val, len);
250 err = -EINVAL;
251 if (ret >= 0) {
252 prop->value = new_val;
253 prop->length = len;
255 if (OF_IS_DYNAMIC(prop))
256 kfree(old_val);
258 OF_MARK_DYNAMIC(prop);
260 err = 0;
262 break;
264 prevp = &(*prevp)->next;
266 write_unlock(&devtree_lock);
268 /* XXX Upate procfs if necessary... */
270 return err;
272 EXPORT_SYMBOL(of_set_property);
274 static unsigned int prom_early_allocated;
276 static void * __init prom_early_alloc(unsigned long size)
278 void *ret;
280 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
281 if (ret != NULL)
282 memset(ret, 0, size);
284 prom_early_allocated += size;
286 return ret;
289 static int is_root_node(const struct device_node *dp)
291 if (!dp)
292 return 0;
294 return (dp->parent == NULL);
297 /* The following routines deal with the black magic of fully naming a
298 * node.
300 * Certain well known named nodes are just the simple name string.
302 * Actual devices have an address specifier appended to the base name
303 * string, like this "foo@addr". The "addr" can be in any number of
304 * formats, and the platform plus the type of the node determine the
305 * format and how it is constructed.
307 * For children of the ROOT node, the naming convention is fixed and
308 * determined by whether this is a sun4u or sun4v system.
310 * For children of other nodes, it is bus type specific. So
311 * we walk up the tree until we discover a "device_type" property
312 * we recognize and we go from there.
314 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
316 struct linux_prom_registers *regs;
317 struct property *rprop;
319 rprop = of_find_property(dp, "reg", NULL);
320 if (!rprop)
321 return;
323 regs = rprop->value;
324 sprintf(tmp_buf, "%s@%x,%x",
325 dp->name,
326 regs->which_io, regs->phys_addr);
329 /* "name@slot,offset" */
330 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
332 struct linux_prom_registers *regs;
333 struct property *prop;
335 prop = of_find_property(dp, "reg", NULL);
336 if (!prop)
337 return;
339 regs = prop->value;
340 sprintf(tmp_buf, "%s@%x,%x",
341 dp->name,
342 regs->which_io,
343 regs->phys_addr);
346 /* "name@devnum[,func]" */
347 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
349 struct linux_prom_pci_registers *regs;
350 struct property *prop;
351 unsigned int devfn;
353 prop = of_find_property(dp, "reg", NULL);
354 if (!prop)
355 return;
357 regs = prop->value;
358 devfn = (regs->phys_hi >> 8) & 0xff;
359 if (devfn & 0x07) {
360 sprintf(tmp_buf, "%s@%x,%x",
361 dp->name,
362 devfn >> 3,
363 devfn & 0x07);
364 } else {
365 sprintf(tmp_buf, "%s@%x",
366 dp->name,
367 devfn >> 3);
371 /* "name@addrhi,addrlo" */
372 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
374 struct linux_prom_registers *regs;
375 struct property *prop;
377 prop = of_find_property(dp, "reg", NULL);
378 if (!prop)
379 return;
381 regs = prop->value;
383 sprintf(tmp_buf, "%s@%x,%x",
384 dp->name,
385 regs->which_io, regs->phys_addr);
388 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
390 struct device_node *parent = dp->parent;
392 if (parent != NULL) {
393 if (!strcmp(parent->type, "pci") ||
394 !strcmp(parent->type, "pciex"))
395 return pci_path_component(dp, tmp_buf);
396 if (!strcmp(parent->type, "sbus"))
397 return sbus_path_component(dp, tmp_buf);
398 if (!strcmp(parent->type, "ebus"))
399 return ebus_path_component(dp, tmp_buf);
401 /* "isa" is handled with platform naming */
404 /* Use platform naming convention. */
405 return sparc32_path_component(dp, tmp_buf);
408 static char * __init build_path_component(struct device_node *dp)
410 char tmp_buf[64], *n;
412 tmp_buf[0] = '\0';
413 __build_path_component(dp, tmp_buf);
414 if (tmp_buf[0] == '\0')
415 strcpy(tmp_buf, dp->name);
417 n = prom_early_alloc(strlen(tmp_buf) + 1);
418 strcpy(n, tmp_buf);
420 return n;
423 static char * __init build_full_name(struct device_node *dp)
425 int len, ourlen, plen;
426 char *n;
428 plen = strlen(dp->parent->full_name);
429 ourlen = strlen(dp->path_component_name);
430 len = ourlen + plen + 2;
432 n = prom_early_alloc(len);
433 strcpy(n, dp->parent->full_name);
434 if (!is_root_node(dp->parent)) {
435 strcpy(n + plen, "/");
436 plen++;
438 strcpy(n + plen, dp->path_component_name);
440 return n;
443 static unsigned int unique_id;
445 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
447 static struct property *tmp = NULL;
448 struct property *p;
449 int len;
450 const char *name;
452 if (tmp) {
453 p = tmp;
454 memset(p, 0, sizeof(*p) + 32);
455 tmp = NULL;
456 } else {
457 p = prom_early_alloc(sizeof(struct property) + 32);
458 p->unique_id = unique_id++;
461 p->name = (char *) (p + 1);
462 if (special_name) {
463 strcpy(p->name, special_name);
464 p->length = special_len;
465 p->value = prom_early_alloc(special_len);
466 memcpy(p->value, special_val, special_len);
467 } else {
468 if (prev == NULL) {
469 name = prom_firstprop(node, NULL);
470 } else {
471 name = prom_nextprop(node, prev, NULL);
473 if (strlen(name) == 0) {
474 tmp = p;
475 return NULL;
477 strcpy(p->name, name);
478 p->length = prom_getproplen(node, p->name);
479 if (p->length <= 0) {
480 p->length = 0;
481 } else {
482 p->value = prom_early_alloc(p->length + 1);
483 len = prom_getproperty(node, p->name, p->value,
484 p->length);
485 if (len <= 0)
486 p->length = 0;
487 ((unsigned char *)p->value)[p->length] = '\0';
490 return p;
493 static struct property * __init build_prop_list(phandle node)
495 struct property *head, *tail;
497 head = tail = build_one_prop(node, NULL,
498 ".node", &node, sizeof(node));
500 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
501 tail = tail->next;
502 while(tail) {
503 tail->next = build_one_prop(node, tail->name,
504 NULL, NULL, 0);
505 tail = tail->next;
508 return head;
511 static char * __init get_one_property(phandle node, char *name)
513 char *buf = "<NULL>";
514 int len;
516 len = prom_getproplen(node, name);
517 if (len > 0) {
518 buf = prom_early_alloc(len);
519 len = prom_getproperty(node, name, buf, len);
522 return buf;
525 static struct device_node * __init create_node(phandle node)
527 struct device_node *dp;
529 if (!node)
530 return NULL;
532 dp = prom_early_alloc(sizeof(*dp));
533 dp->unique_id = unique_id++;
535 kref_init(&dp->kref);
537 dp->name = get_one_property(node, "name");
538 dp->type = get_one_property(node, "device_type");
539 dp->node = node;
541 /* Build interrupts later... */
543 dp->properties = build_prop_list(node);
545 return dp;
548 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
550 struct device_node *dp;
552 dp = create_node(node);
553 if (dp) {
554 *(*nextp) = dp;
555 *nextp = &dp->allnext;
557 dp->parent = parent;
558 dp->path_component_name = build_path_component(dp);
559 dp->full_name = build_full_name(dp);
561 dp->child = build_tree(dp, prom_getchild(node), nextp);
563 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
566 return dp;
569 void __init prom_build_devicetree(void)
571 struct device_node **nextp;
573 allnodes = create_node(prom_root_node);
574 allnodes->path_component_name = "";
575 allnodes->full_name = "/";
577 nextp = &allnodes->allnext;
578 allnodes->child = build_tree(allnodes,
579 prom_getchild(allnodes->node),
580 &nextp);
581 printk("PROM: Built device tree with %u bytes of memory.\n",
582 prom_early_allocated);