2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc64 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>
22 #include <linux/bootmem.h>
23 #include <linux/module.h>
26 #include <asm/oplib.h>
28 static struct device_node *allnodes;
30 struct device_node *of_get_parent(const struct device_node *node)
32 struct device_node *np;
42 struct device_node *of_get_next_child(const struct device_node *node,
43 struct device_node *prev)
45 struct device_node *next;
47 next = prev ? prev->sibling : node->child;
48 for (; next != 0; next = next->sibling) {
55 struct device_node *of_find_node_by_path(const char *path)
57 struct device_node *np = allnodes;
59 for (; np != 0; np = np->allnext) {
60 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
66 EXPORT_SYMBOL(of_find_node_by_path);
68 struct device_node *of_find_node_by_phandle(phandle handle)
70 struct device_node *np;
72 for (np = allnodes; np != 0; np = np->allnext)
73 if (np->node == handle)
79 struct device_node *of_find_node_by_name(struct device_node *from,
82 struct device_node *np;
84 np = from ? from->allnext : allnodes;
85 for (; np != NULL; np = np->allnext)
86 if (np->name != NULL && strcmp(np->name, name) == 0)
92 struct device_node *of_find_node_by_type(struct device_node *from,
95 struct device_node *np;
97 np = from ? from->allnext : allnodes;
98 for (; np != 0; np = np->allnext)
99 if (np->type != 0 && strcmp(np->type, type) == 0)
105 struct property *of_find_property(struct device_node *np, const char *name,
110 for (pp = np->properties; pp != 0; pp = pp->next) {
111 if (strcmp(pp->name, name) == 0) {
119 EXPORT_SYMBOL(of_find_property);
122 * Find a property with a given name for a given node
123 * and return the value.
125 void *of_get_property(struct device_node *np, const char *name, int *lenp)
127 struct property *pp = of_find_property(np,name,lenp);
128 return pp ? pp->value : NULL;
130 EXPORT_SYMBOL(of_get_property);
132 int of_getintprop_default(struct device_node *np, const char *name, int def)
134 struct property *prop;
137 prop = of_find_property(np, name, &len);
138 if (!prop || len != 4)
141 return *(int *) prop->value;
143 EXPORT_SYMBOL(of_getintprop_default);
145 static unsigned int prom_early_allocated;
147 static void * __init prom_early_alloc(unsigned long size)
151 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
153 memset(ret, 0, size);
155 prom_early_allocated += size;
160 static int is_root_node(const struct device_node *dp)
165 return (dp->parent == NULL);
168 /* The following routines deal with the black magic of fully naming a
171 * Certain well known named nodes are just the simple name string.
173 * Actual devices have an address specifier appended to the base name
174 * string, like this "foo@addr". The "addr" can be in any number of
175 * formats, and the platform plus the type of the node determine the
176 * format and how it is constructed.
178 * For children of the ROOT node, the naming convention is fixed and
179 * determined by whether this is a sun4u or sun4v system.
181 * For children of other nodes, it is bus type specific. So
182 * we walk up the tree until we discover a "device_type" property
183 * we recognize and we go from there.
185 * As an example, the boot device on my workstation has a full path:
187 * /pci@1e,600000/ide@d/disk@0,0:c
189 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
191 struct linux_prom64_registers *regs;
192 struct property *rprop;
193 u32 high_bits, low_bits, type;
195 rprop = of_find_property(dp, "reg", NULL);
200 if (!is_root_node(dp->parent)) {
201 sprintf(tmp_buf, "%s@%x,%x",
203 (unsigned int) (regs->phys_addr >> 32UL),
204 (unsigned int) (regs->phys_addr & 0xffffffffUL));
208 type = regs->phys_addr >> 60UL;
209 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
210 low_bits = (regs->phys_addr & 0xffffffffUL);
212 if (type == 0 || type == 8) {
213 const char *prefix = (type == 0) ? "m" : "i";
216 sprintf(tmp_buf, "%s@%s%x,%x",
218 high_bits, low_bits);
220 sprintf(tmp_buf, "%s@%s%x",
224 } else if (type == 12) {
225 sprintf(tmp_buf, "%s@%x",
226 dp->name, high_bits);
230 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
232 struct linux_prom64_registers *regs;
233 struct property *prop;
235 prop = of_find_property(dp, "reg", NULL);
240 if (!is_root_node(dp->parent)) {
241 sprintf(tmp_buf, "%s@%x,%x",
243 (unsigned int) (regs->phys_addr >> 32UL),
244 (unsigned int) (regs->phys_addr & 0xffffffffUL));
248 prop = of_find_property(dp, "upa-portid", NULL);
250 prop = of_find_property(dp, "portid", NULL);
252 unsigned long mask = 0xffffffffUL;
254 if (tlb_type >= cheetah)
257 sprintf(tmp_buf, "%s@%x,%x",
260 (unsigned int) (regs->phys_addr & mask));
264 /* "name@slot,offset" */
265 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
267 struct linux_prom_registers *regs;
268 struct property *prop;
270 prop = of_find_property(dp, "reg", NULL);
275 sprintf(tmp_buf, "%s@%x,%x",
281 /* "name@devnum[,func]" */
282 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
284 struct linux_prom_pci_registers *regs;
285 struct property *prop;
288 prop = of_find_property(dp, "reg", NULL);
293 devfn = (regs->phys_hi >> 8) & 0xff;
295 sprintf(tmp_buf, "%s@%x,%x",
300 sprintf(tmp_buf, "%s@%x",
306 /* "name@UPA_PORTID,offset" */
307 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
309 struct linux_prom64_registers *regs;
310 struct property *prop;
312 prop = of_find_property(dp, "reg", NULL);
318 prop = of_find_property(dp, "upa-portid", NULL);
322 sprintf(tmp_buf, "%s@%x,%x",
324 *(u32 *) prop->value,
325 (unsigned int) (regs->phys_addr & 0xffffffffUL));
329 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
331 struct property *prop;
334 prop = of_find_property(dp, "reg", NULL);
340 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
343 /* "name@addrhi,addrlo" */
344 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
346 struct linux_prom64_registers *regs;
347 struct property *prop;
349 prop = of_find_property(dp, "reg", NULL);
355 sprintf(tmp_buf, "%s@%x,%x",
357 (unsigned int) (regs->phys_addr >> 32UL),
358 (unsigned int) (regs->phys_addr & 0xffffffffUL));
361 /* "name@bus,addr" */
362 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
364 struct property *prop;
367 prop = of_find_property(dp, "reg", NULL);
373 /* This actually isn't right... should look at the #address-cells
374 * property of the i2c bus node etc. etc.
376 sprintf(tmp_buf, "%s@%x,%x",
377 dp->name, regs[0], regs[1]);
380 /* "name@reg0[,reg1]" */
381 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
383 struct property *prop;
386 prop = of_find_property(dp, "reg", NULL);
392 if (prop->length == sizeof(u32) || regs[1] == 1) {
393 sprintf(tmp_buf, "%s@%x",
396 sprintf(tmp_buf, "%s@%x,%x",
397 dp->name, regs[0], regs[1]);
401 /* "name@reg0reg1[,reg2reg3]" */
402 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
404 struct property *prop;
407 prop = of_find_property(dp, "reg", NULL);
413 if (regs[2] || regs[3]) {
414 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
415 dp->name, regs[0], regs[1], regs[2], regs[3]);
417 sprintf(tmp_buf, "%s@%08x%08x",
418 dp->name, regs[0], regs[1]);
422 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
424 struct device_node *parent = dp->parent;
426 if (parent != NULL) {
427 if (!strcmp(parent->type, "pci") ||
428 !strcmp(parent->type, "pciex"))
429 return pci_path_component(dp, tmp_buf);
430 if (!strcmp(parent->type, "sbus"))
431 return sbus_path_component(dp, tmp_buf);
432 if (!strcmp(parent->type, "upa"))
433 return upa_path_component(dp, tmp_buf);
434 if (!strcmp(parent->type, "ebus"))
435 return ebus_path_component(dp, tmp_buf);
436 if (!strcmp(parent->name, "usb") ||
437 !strcmp(parent->name, "hub"))
438 return usb_path_component(dp, tmp_buf);
439 if (!strcmp(parent->type, "i2c"))
440 return i2c_path_component(dp, tmp_buf);
441 if (!strcmp(parent->type, "firewire"))
442 return ieee1394_path_component(dp, tmp_buf);
443 if (!strcmp(parent->type, "virtual-devices"))
444 return vdev_path_component(dp, tmp_buf);
446 /* "isa" is handled with platform naming */
449 /* Use platform naming convention. */
450 if (tlb_type == hypervisor)
451 return sun4v_path_component(dp, tmp_buf);
453 return sun4u_path_component(dp, tmp_buf);
456 static char * __init build_path_component(struct device_node *dp)
458 char tmp_buf[64], *n;
461 __build_path_component(dp, tmp_buf);
462 if (tmp_buf[0] == '\0')
463 strcpy(tmp_buf, dp->name);
465 n = prom_early_alloc(strlen(tmp_buf) + 1);
471 static char * __init build_full_name(struct device_node *dp)
473 int len, ourlen, plen;
476 plen = strlen(dp->parent->full_name);
477 ourlen = strlen(dp->path_component_name);
478 len = ourlen + plen + 2;
480 n = prom_early_alloc(len);
481 strcpy(n, dp->parent->full_name);
482 if (!is_root_node(dp->parent)) {
483 strcpy(n + plen, "/");
486 strcpy(n + plen, dp->path_component_name);
491 static struct property * __init build_one_prop(phandle node, char *prev)
493 static struct property *tmp = NULL;
498 memset(p, 0, sizeof(*p) + 32);
501 p = prom_early_alloc(sizeof(struct property) + 32);
503 p->name = (char *) (p + 1);
505 prom_firstprop(node, p->name);
507 prom_nextprop(node, prev, p->name);
509 if (strlen(p->name) == 0) {
513 p->length = prom_getproplen(node, p->name);
514 if (p->length <= 0) {
517 p->value = prom_early_alloc(p->length);
518 prom_getproperty(node, p->name, p->value, p->length);
523 static struct property * __init build_prop_list(phandle node)
525 struct property *head, *tail;
527 head = tail = build_one_prop(node, NULL);
529 tail->next = build_one_prop(node, tail->name);
536 static char * __init get_one_property(phandle node, const char *name)
538 char *buf = "<NULL>";
541 len = prom_getproplen(node, name);
543 buf = prom_early_alloc(len);
544 prom_getproperty(node, name, buf, len);
550 static struct device_node * __init create_node(phandle node)
552 struct device_node *dp;
557 dp = prom_early_alloc(sizeof(*dp));
559 kref_init(&dp->kref);
561 dp->name = get_one_property(node, "name");
562 dp->type = get_one_property(node, "device_type");
565 /* Build interrupts later... */
567 dp->properties = build_prop_list(node);
572 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
574 struct device_node *dp;
576 dp = create_node(node);
579 *nextp = &dp->allnext;
582 dp->path_component_name = build_path_component(dp);
583 dp->full_name = build_full_name(dp);
585 dp->child = build_tree(dp, prom_getchild(node), nextp);
587 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
593 void __init prom_build_devicetree(void)
595 struct device_node **nextp;
597 allnodes = create_node(prom_root_node);
598 allnodes->path_component_name = "";
599 allnodes->full_name = "/";
601 nextp = &allnodes->allnext;
602 allnodes->child = build_tree(allnodes,
603 prom_getchild(allnodes->node),
605 printk("PROM: Built device tree with %u bytes of memory.\n",
606 prom_early_allocated);