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 int of_device_is_compatible(struct device_node *device, const char *compat)
35 cp = (char *) of_get_property(device, "compatible", &cplen);
39 if (strncmp(cp, compat, strlen(compat)) == 0)
48 EXPORT_SYMBOL(of_device_is_compatible);
50 struct device_node *of_get_parent(const struct device_node *node)
52 struct device_node *np;
61 EXPORT_SYMBOL(of_get_parent);
63 struct device_node *of_get_next_child(const struct device_node *node,
64 struct device_node *prev)
66 struct device_node *next;
68 next = prev ? prev->sibling : node->child;
69 for (; next != 0; next = next->sibling) {
75 EXPORT_SYMBOL(of_get_next_child);
77 struct device_node *of_find_node_by_path(const char *path)
79 struct device_node *np = allnodes;
81 for (; np != 0; np = np->allnext) {
82 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
88 EXPORT_SYMBOL(of_find_node_by_path);
90 struct device_node *of_find_node_by_phandle(phandle handle)
92 struct device_node *np;
94 for (np = allnodes; np != 0; np = np->allnext)
95 if (np->node == handle)
100 EXPORT_SYMBOL(of_find_node_by_phandle);
102 struct device_node *of_find_node_by_name(struct device_node *from,
105 struct device_node *np;
107 np = from ? from->allnext : allnodes;
108 for (; np != NULL; np = np->allnext)
109 if (np->name != NULL && strcmp(np->name, name) == 0)
114 EXPORT_SYMBOL(of_find_node_by_name);
116 struct device_node *of_find_node_by_type(struct device_node *from,
119 struct device_node *np;
121 np = from ? from->allnext : allnodes;
122 for (; np != 0; np = np->allnext)
123 if (np->type != 0 && strcmp(np->type, type) == 0)
128 EXPORT_SYMBOL(of_find_node_by_type);
130 struct device_node *of_find_compatible_node(struct device_node *from,
131 const char *type, const char *compatible)
133 struct device_node *np;
135 np = from ? from->allnext : allnodes;
136 for (; np != 0; np = np->allnext) {
138 && !(np->type != 0 && strcmp(np->type, type) == 0))
140 if (of_device_is_compatible(np, compatible))
146 EXPORT_SYMBOL(of_find_compatible_node);
148 struct property *of_find_property(struct device_node *np, const char *name,
153 for (pp = np->properties; pp != 0; pp = pp->next) {
154 if (strcmp(pp->name, name) == 0) {
162 EXPORT_SYMBOL(of_find_property);
165 * Find a property with a given name for a given node
166 * and return the value.
168 void *of_get_property(struct device_node *np, const char *name, int *lenp)
170 struct property *pp = of_find_property(np,name,lenp);
171 return pp ? pp->value : NULL;
173 EXPORT_SYMBOL(of_get_property);
175 int of_getintprop_default(struct device_node *np, const char *name, int def)
177 struct property *prop;
180 prop = of_find_property(np, name, &len);
181 if (!prop || len != 4)
184 return *(int *) prop->value;
186 EXPORT_SYMBOL(of_getintprop_default);
188 static unsigned int prom_early_allocated;
190 static void * __init prom_early_alloc(unsigned long size)
194 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
196 memset(ret, 0, size);
198 prom_early_allocated += size;
203 static int is_root_node(const struct device_node *dp)
208 return (dp->parent == NULL);
211 /* The following routines deal with the black magic of fully naming a
214 * Certain well known named nodes are just the simple name string.
216 * Actual devices have an address specifier appended to the base name
217 * string, like this "foo@addr". The "addr" can be in any number of
218 * formats, and the platform plus the type of the node determine the
219 * format and how it is constructed.
221 * For children of the ROOT node, the naming convention is fixed and
222 * determined by whether this is a sun4u or sun4v system.
224 * For children of other nodes, it is bus type specific. So
225 * we walk up the tree until we discover a "device_type" property
226 * we recognize and we go from there.
228 * As an example, the boot device on my workstation has a full path:
230 * /pci@1e,600000/ide@d/disk@0,0:c
232 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
234 struct linux_prom64_registers *regs;
235 struct property *rprop;
236 u32 high_bits, low_bits, type;
238 rprop = of_find_property(dp, "reg", NULL);
243 if (!is_root_node(dp->parent)) {
244 sprintf(tmp_buf, "%s@%x,%x",
246 (unsigned int) (regs->phys_addr >> 32UL),
247 (unsigned int) (regs->phys_addr & 0xffffffffUL));
251 type = regs->phys_addr >> 60UL;
252 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
253 low_bits = (regs->phys_addr & 0xffffffffUL);
255 if (type == 0 || type == 8) {
256 const char *prefix = (type == 0) ? "m" : "i";
259 sprintf(tmp_buf, "%s@%s%x,%x",
261 high_bits, low_bits);
263 sprintf(tmp_buf, "%s@%s%x",
267 } else if (type == 12) {
268 sprintf(tmp_buf, "%s@%x",
269 dp->name, high_bits);
273 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
275 struct linux_prom64_registers *regs;
276 struct property *prop;
278 prop = of_find_property(dp, "reg", NULL);
283 if (!is_root_node(dp->parent)) {
284 sprintf(tmp_buf, "%s@%x,%x",
286 (unsigned int) (regs->phys_addr >> 32UL),
287 (unsigned int) (regs->phys_addr & 0xffffffffUL));
291 prop = of_find_property(dp, "upa-portid", NULL);
293 prop = of_find_property(dp, "portid", NULL);
295 unsigned long mask = 0xffffffffUL;
297 if (tlb_type >= cheetah)
300 sprintf(tmp_buf, "%s@%x,%x",
303 (unsigned int) (regs->phys_addr & mask));
307 /* "name@slot,offset" */
308 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
310 struct linux_prom_registers *regs;
311 struct property *prop;
313 prop = of_find_property(dp, "reg", NULL);
318 sprintf(tmp_buf, "%s@%x,%x",
324 /* "name@devnum[,func]" */
325 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
327 struct linux_prom_pci_registers *regs;
328 struct property *prop;
331 prop = of_find_property(dp, "reg", NULL);
336 devfn = (regs->phys_hi >> 8) & 0xff;
338 sprintf(tmp_buf, "%s@%x,%x",
343 sprintf(tmp_buf, "%s@%x",
349 /* "name@UPA_PORTID,offset" */
350 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
352 struct linux_prom64_registers *regs;
353 struct property *prop;
355 prop = of_find_property(dp, "reg", NULL);
361 prop = of_find_property(dp, "upa-portid", NULL);
365 sprintf(tmp_buf, "%s@%x,%x",
367 *(u32 *) prop->value,
368 (unsigned int) (regs->phys_addr & 0xffffffffUL));
372 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
374 struct property *prop;
377 prop = of_find_property(dp, "reg", NULL);
383 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
386 /* "name@addrhi,addrlo" */
387 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
389 struct linux_prom64_registers *regs;
390 struct property *prop;
392 prop = of_find_property(dp, "reg", NULL);
398 sprintf(tmp_buf, "%s@%x,%x",
400 (unsigned int) (regs->phys_addr >> 32UL),
401 (unsigned int) (regs->phys_addr & 0xffffffffUL));
404 /* "name@bus,addr" */
405 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
407 struct property *prop;
410 prop = of_find_property(dp, "reg", NULL);
416 /* This actually isn't right... should look at the #address-cells
417 * property of the i2c bus node etc. etc.
419 sprintf(tmp_buf, "%s@%x,%x",
420 dp->name, regs[0], regs[1]);
423 /* "name@reg0[,reg1]" */
424 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
426 struct property *prop;
429 prop = of_find_property(dp, "reg", NULL);
435 if (prop->length == sizeof(u32) || regs[1] == 1) {
436 sprintf(tmp_buf, "%s@%x",
439 sprintf(tmp_buf, "%s@%x,%x",
440 dp->name, regs[0], regs[1]);
444 /* "name@reg0reg1[,reg2reg3]" */
445 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
447 struct property *prop;
450 prop = of_find_property(dp, "reg", NULL);
456 if (regs[2] || regs[3]) {
457 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
458 dp->name, regs[0], regs[1], regs[2], regs[3]);
460 sprintf(tmp_buf, "%s@%08x%08x",
461 dp->name, regs[0], regs[1]);
465 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
467 struct device_node *parent = dp->parent;
469 if (parent != NULL) {
470 if (!strcmp(parent->type, "pci") ||
471 !strcmp(parent->type, "pciex"))
472 return pci_path_component(dp, tmp_buf);
473 if (!strcmp(parent->type, "sbus"))
474 return sbus_path_component(dp, tmp_buf);
475 if (!strcmp(parent->type, "upa"))
476 return upa_path_component(dp, tmp_buf);
477 if (!strcmp(parent->type, "ebus"))
478 return ebus_path_component(dp, tmp_buf);
479 if (!strcmp(parent->name, "usb") ||
480 !strcmp(parent->name, "hub"))
481 return usb_path_component(dp, tmp_buf);
482 if (!strcmp(parent->type, "i2c"))
483 return i2c_path_component(dp, tmp_buf);
484 if (!strcmp(parent->type, "firewire"))
485 return ieee1394_path_component(dp, tmp_buf);
486 if (!strcmp(parent->type, "virtual-devices"))
487 return vdev_path_component(dp, tmp_buf);
489 /* "isa" is handled with platform naming */
492 /* Use platform naming convention. */
493 if (tlb_type == hypervisor)
494 return sun4v_path_component(dp, tmp_buf);
496 return sun4u_path_component(dp, tmp_buf);
499 static char * __init build_path_component(struct device_node *dp)
501 char tmp_buf[64], *n;
504 __build_path_component(dp, tmp_buf);
505 if (tmp_buf[0] == '\0')
506 strcpy(tmp_buf, dp->name);
508 n = prom_early_alloc(strlen(tmp_buf) + 1);
514 static char * __init build_full_name(struct device_node *dp)
516 int len, ourlen, plen;
519 plen = strlen(dp->parent->full_name);
520 ourlen = strlen(dp->path_component_name);
521 len = ourlen + plen + 2;
523 n = prom_early_alloc(len);
524 strcpy(n, dp->parent->full_name);
525 if (!is_root_node(dp->parent)) {
526 strcpy(n + plen, "/");
529 strcpy(n + plen, dp->path_component_name);
534 static struct property * __init build_one_prop(phandle node, char *prev)
536 static struct property *tmp = NULL;
541 memset(p, 0, sizeof(*p) + 32);
544 p = prom_early_alloc(sizeof(struct property) + 32);
546 p->name = (char *) (p + 1);
548 prom_firstprop(node, p->name);
550 prom_nextprop(node, prev, p->name);
552 if (strlen(p->name) == 0) {
556 p->length = prom_getproplen(node, p->name);
557 if (p->length <= 0) {
560 p->value = prom_early_alloc(p->length);
561 prom_getproperty(node, p->name, p->value, p->length);
566 static struct property * __init build_prop_list(phandle node)
568 struct property *head, *tail;
570 head = tail = build_one_prop(node, NULL);
572 tail->next = build_one_prop(node, tail->name);
579 static char * __init get_one_property(phandle node, const char *name)
581 char *buf = "<NULL>";
584 len = prom_getproplen(node, name);
586 buf = prom_early_alloc(len);
587 prom_getproperty(node, name, buf, len);
593 static struct device_node * __init create_node(phandle node)
595 struct device_node *dp;
600 dp = prom_early_alloc(sizeof(*dp));
602 kref_init(&dp->kref);
604 dp->name = get_one_property(node, "name");
605 dp->type = get_one_property(node, "device_type");
608 /* Build interrupts later... */
610 dp->properties = build_prop_list(node);
615 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
617 struct device_node *dp;
619 dp = create_node(node);
622 *nextp = &dp->allnext;
625 dp->path_component_name = build_path_component(dp);
626 dp->full_name = build_full_name(dp);
628 dp->child = build_tree(dp, prom_getchild(node), nextp);
630 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
636 void __init prom_build_devicetree(void)
638 struct device_node **nextp;
640 allnodes = create_node(prom_root_node);
641 allnodes->path_component_name = "";
642 allnodes->full_name = "/";
644 nextp = &allnodes->allnext;
645 allnodes->child = build_tree(allnodes,
646 prom_getchild(allnodes->node),
648 printk("PROM: Built device tree with %u bytes of memory.\n",
649 prom_early_allocated);