sync hh.org
[hh.org.git] / arch / sparc / kernel / prom.c
blob2cc302b6bec00798564873fe7d1b063b5170e17b
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(struct device_node *device, const char *compat)
37 const char* cp;
38 int cplen, l;
40 cp = (char *) of_get_property(device, "compatible", &cplen);
41 if (cp == NULL)
42 return 0;
43 while (cplen > 0) {
44 if (strncmp(cp, compat, strlen(compat)) == 0)
45 return 1;
46 l = strlen(cp) + 1;
47 cp += l;
48 cplen -= l;
51 return 0;
53 EXPORT_SYMBOL(of_device_is_compatible);
55 struct device_node *of_get_parent(const struct device_node *node)
57 struct device_node *np;
59 if (!node)
60 return NULL;
62 np = node->parent;
64 return np;
66 EXPORT_SYMBOL(of_get_parent);
68 struct device_node *of_get_next_child(const struct device_node *node,
69 struct device_node *prev)
71 struct device_node *next;
73 next = prev ? prev->sibling : node->child;
74 for (; next != 0; next = next->sibling) {
75 break;
78 return next;
80 EXPORT_SYMBOL(of_get_next_child);
82 struct device_node *of_find_node_by_path(const char *path)
84 struct device_node *np = allnodes;
86 for (; np != 0; np = np->allnext) {
87 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
88 break;
91 return np;
93 EXPORT_SYMBOL(of_find_node_by_path);
95 struct device_node *of_find_node_by_phandle(phandle handle)
97 struct device_node *np;
99 for (np = allnodes; np != 0; np = np->allnext)
100 if (np->node == handle)
101 break;
103 return np;
105 EXPORT_SYMBOL(of_find_node_by_phandle);
107 struct device_node *of_find_node_by_name(struct device_node *from,
108 const char *name)
110 struct device_node *np;
112 np = from ? from->allnext : allnodes;
113 for (; np != NULL; np = np->allnext)
114 if (np->name != NULL && strcmp(np->name, name) == 0)
115 break;
117 return np;
119 EXPORT_SYMBOL(of_find_node_by_name);
121 struct device_node *of_find_node_by_type(struct device_node *from,
122 const char *type)
124 struct device_node *np;
126 np = from ? from->allnext : allnodes;
127 for (; np != 0; np = np->allnext)
128 if (np->type != 0 && strcmp(np->type, type) == 0)
129 break;
131 return np;
133 EXPORT_SYMBOL(of_find_node_by_type);
135 struct device_node *of_find_compatible_node(struct device_node *from,
136 const char *type, const char *compatible)
138 struct device_node *np;
140 np = from ? from->allnext : allnodes;
141 for (; np != 0; np = np->allnext) {
142 if (type != NULL
143 && !(np->type != 0 && strcmp(np->type, type) == 0))
144 continue;
145 if (of_device_is_compatible(np, compatible))
146 break;
149 return np;
151 EXPORT_SYMBOL(of_find_compatible_node);
153 struct property *of_find_property(struct device_node *np, const char *name,
154 int *lenp)
156 struct property *pp;
158 for (pp = np->properties; pp != 0; pp = pp->next) {
159 if (strcmp(pp->name, name) == 0) {
160 if (lenp != 0)
161 *lenp = pp->length;
162 break;
165 return pp;
167 EXPORT_SYMBOL(of_find_property);
170 * Find a property with a given name for a given node
171 * and return the value.
173 void *of_get_property(struct device_node *np, const char *name, int *lenp)
175 struct property *pp = of_find_property(np,name,lenp);
176 return pp ? pp->value : NULL;
178 EXPORT_SYMBOL(of_get_property);
180 int of_getintprop_default(struct device_node *np, const char *name, int def)
182 struct property *prop;
183 int len;
185 prop = of_find_property(np, name, &len);
186 if (!prop || len != 4)
187 return def;
189 return *(int *) prop->value;
191 EXPORT_SYMBOL(of_getintprop_default);
193 int of_n_addr_cells(struct device_node *np)
195 int* ip;
196 do {
197 if (np->parent)
198 np = np->parent;
199 ip = of_get_property(np, "#address-cells", NULL);
200 if (ip != NULL)
201 return *ip;
202 } while (np->parent);
203 /* No #address-cells property for the root node, default to 2 */
204 return 2;
206 EXPORT_SYMBOL(of_n_addr_cells);
208 int of_n_size_cells(struct device_node *np)
210 int* ip;
211 do {
212 if (np->parent)
213 np = np->parent;
214 ip = of_get_property(np, "#size-cells", NULL);
215 if (ip != NULL)
216 return *ip;
217 } while (np->parent);
218 /* No #size-cells property for the root node, default to 1 */
219 return 1;
221 EXPORT_SYMBOL(of_n_size_cells);
223 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
225 struct property **prevp;
226 void *new_val;
227 int err;
229 new_val = kmalloc(len, GFP_KERNEL);
230 if (!new_val)
231 return -ENOMEM;
233 memcpy(new_val, val, len);
235 err = -ENODEV;
237 write_lock(&devtree_lock);
238 prevp = &dp->properties;
239 while (*prevp) {
240 struct property *prop = *prevp;
242 if (!strcmp(prop->name, name)) {
243 void *old_val = prop->value;
244 int ret;
246 ret = prom_setprop(dp->node, (char *) name, val, len);
247 err = -EINVAL;
248 if (ret >= 0) {
249 prop->value = new_val;
250 prop->length = len;
252 if (OF_IS_DYNAMIC(prop))
253 kfree(old_val);
255 OF_MARK_DYNAMIC(prop);
257 err = 0;
259 break;
261 prevp = &(*prevp)->next;
263 write_unlock(&devtree_lock);
265 /* XXX Upate procfs if necessary... */
267 return err;
269 EXPORT_SYMBOL(of_set_property);
271 static unsigned int prom_early_allocated;
273 static void * __init prom_early_alloc(unsigned long size)
275 void *ret;
277 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
278 if (ret != NULL)
279 memset(ret, 0, size);
281 prom_early_allocated += size;
283 return ret;
286 static int is_root_node(const struct device_node *dp)
288 if (!dp)
289 return 0;
291 return (dp->parent == NULL);
294 /* The following routines deal with the black magic of fully naming a
295 * node.
297 * Certain well known named nodes are just the simple name string.
299 * Actual devices have an address specifier appended to the base name
300 * string, like this "foo@addr". The "addr" can be in any number of
301 * formats, and the platform plus the type of the node determine the
302 * format and how it is constructed.
304 * For children of the ROOT node, the naming convention is fixed and
305 * determined by whether this is a sun4u or sun4v system.
307 * For children of other nodes, it is bus type specific. So
308 * we walk up the tree until we discover a "device_type" property
309 * we recognize and we go from there.
311 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
313 struct linux_prom_registers *regs;
314 struct property *rprop;
316 rprop = of_find_property(dp, "reg", NULL);
317 if (!rprop)
318 return;
320 regs = rprop->value;
321 sprintf(tmp_buf, "%s@%x,%x",
322 dp->name,
323 regs->which_io, regs->phys_addr);
326 /* "name@slot,offset" */
327 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
329 struct linux_prom_registers *regs;
330 struct property *prop;
332 prop = of_find_property(dp, "reg", NULL);
333 if (!prop)
334 return;
336 regs = prop->value;
337 sprintf(tmp_buf, "%s@%x,%x",
338 dp->name,
339 regs->which_io,
340 regs->phys_addr);
343 /* "name@devnum[,func]" */
344 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
346 struct linux_prom_pci_registers *regs;
347 struct property *prop;
348 unsigned int devfn;
350 prop = of_find_property(dp, "reg", NULL);
351 if (!prop)
352 return;
354 regs = prop->value;
355 devfn = (regs->phys_hi >> 8) & 0xff;
356 if (devfn & 0x07) {
357 sprintf(tmp_buf, "%s@%x,%x",
358 dp->name,
359 devfn >> 3,
360 devfn & 0x07);
361 } else {
362 sprintf(tmp_buf, "%s@%x",
363 dp->name,
364 devfn >> 3);
368 /* "name@addrhi,addrlo" */
369 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
371 struct linux_prom_registers *regs;
372 struct property *prop;
374 prop = of_find_property(dp, "reg", NULL);
375 if (!prop)
376 return;
378 regs = prop->value;
380 sprintf(tmp_buf, "%s@%x,%x",
381 dp->name,
382 regs->which_io, regs->phys_addr);
385 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
387 struct device_node *parent = dp->parent;
389 if (parent != NULL) {
390 if (!strcmp(parent->type, "pci") ||
391 !strcmp(parent->type, "pciex"))
392 return pci_path_component(dp, tmp_buf);
393 if (!strcmp(parent->type, "sbus"))
394 return sbus_path_component(dp, tmp_buf);
395 if (!strcmp(parent->type, "ebus"))
396 return ebus_path_component(dp, tmp_buf);
398 /* "isa" is handled with platform naming */
401 /* Use platform naming convention. */
402 return sparc32_path_component(dp, tmp_buf);
405 static char * __init build_path_component(struct device_node *dp)
407 char tmp_buf[64], *n;
409 tmp_buf[0] = '\0';
410 __build_path_component(dp, tmp_buf);
411 if (tmp_buf[0] == '\0')
412 strcpy(tmp_buf, dp->name);
414 n = prom_early_alloc(strlen(tmp_buf) + 1);
415 strcpy(n, tmp_buf);
417 return n;
420 static char * __init build_full_name(struct device_node *dp)
422 int len, ourlen, plen;
423 char *n;
425 plen = strlen(dp->parent->full_name);
426 ourlen = strlen(dp->path_component_name);
427 len = ourlen + plen + 2;
429 n = prom_early_alloc(len);
430 strcpy(n, dp->parent->full_name);
431 if (!is_root_node(dp->parent)) {
432 strcpy(n + plen, "/");
433 plen++;
435 strcpy(n + plen, dp->path_component_name);
437 return n;
440 static unsigned int unique_id;
442 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
444 static struct property *tmp = NULL;
445 struct property *p;
446 int len;
447 const char *name;
449 if (tmp) {
450 p = tmp;
451 memset(p, 0, sizeof(*p) + 32);
452 tmp = NULL;
453 } else {
454 p = prom_early_alloc(sizeof(struct property) + 32);
455 p->unique_id = unique_id++;
458 p->name = (char *) (p + 1);
459 if (special_name) {
460 strcpy(p->name, special_name);
461 p->length = special_len;
462 p->value = prom_early_alloc(special_len);
463 memcpy(p->value, special_val, special_len);
464 } else {
465 if (prev == NULL) {
466 name = prom_firstprop(node, NULL);
467 } else {
468 name = prom_nextprop(node, prev, NULL);
470 if (strlen(name) == 0) {
471 tmp = p;
472 return NULL;
474 strcpy(p->name, name);
475 p->length = prom_getproplen(node, p->name);
476 if (p->length <= 0) {
477 p->length = 0;
478 } else {
479 p->value = prom_early_alloc(p->length + 1);
480 len = prom_getproperty(node, p->name, p->value,
481 p->length);
482 if (len <= 0)
483 p->length = 0;
484 ((unsigned char *)p->value)[p->length] = '\0';
487 return p;
490 static struct property * __init build_prop_list(phandle node)
492 struct property *head, *tail;
494 head = tail = build_one_prop(node, NULL,
495 ".node", &node, sizeof(node));
497 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
498 tail = tail->next;
499 while(tail) {
500 tail->next = build_one_prop(node, tail->name,
501 NULL, NULL, 0);
502 tail = tail->next;
505 return head;
508 static char * __init get_one_property(phandle node, char *name)
510 char *buf = "<NULL>";
511 int len;
513 len = prom_getproplen(node, name);
514 if (len > 0) {
515 buf = prom_early_alloc(len);
516 len = prom_getproperty(node, name, buf, len);
519 return buf;
522 static struct device_node * __init create_node(phandle node)
524 struct device_node *dp;
526 if (!node)
527 return NULL;
529 dp = prom_early_alloc(sizeof(*dp));
530 dp->unique_id = unique_id++;
532 kref_init(&dp->kref);
534 dp->name = get_one_property(node, "name");
535 dp->type = get_one_property(node, "device_type");
536 dp->node = node;
538 /* Build interrupts later... */
540 dp->properties = build_prop_list(node);
542 return dp;
545 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
547 struct device_node *dp;
549 dp = create_node(node);
550 if (dp) {
551 *(*nextp) = dp;
552 *nextp = &dp->allnext;
554 dp->parent = parent;
555 dp->path_component_name = build_path_component(dp);
556 dp->full_name = build_full_name(dp);
558 dp->child = build_tree(dp, prom_getchild(node), nextp);
560 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
563 return dp;
566 void __init prom_build_devicetree(void)
568 struct device_node **nextp;
570 allnodes = create_node(prom_root_node);
571 allnodes->path_component_name = "";
572 allnodes->full_name = "/";
574 nextp = &allnodes->allnext;
575 allnodes->child = build_tree(allnodes,
576 prom_getchild(allnodes->node),
577 &nextp);
578 printk("PROM: Built device tree with %u bytes of memory.\n",
579 prom_early_allocated);