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>
25 #include <asm/oplib.h>
27 static struct device_node *allnodes;
29 struct device_node *of_get_parent(const struct device_node *node)
31 struct device_node *np;
41 struct device_node *of_get_next_child(const struct device_node *node,
42 struct device_node *prev)
44 struct device_node *next;
46 next = prev ? prev->sibling : node->child;
47 for (; next != 0; next = next->sibling) {
54 struct device_node *of_find_node_by_path(const char *path)
56 struct device_node *np = allnodes;
58 for (; np != 0; np = np->allnext) {
59 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
66 struct device_node *of_find_node_by_name(struct device_node *from,
69 struct device_node *np;
71 np = from ? from->allnext : allnodes;
72 for (; np != NULL; np = np->allnext)
73 if (np->name != NULL && strcmp(np->name, name) == 0)
79 struct device_node *of_find_node_by_type(struct device_node *from,
82 struct device_node *np;
84 np = from ? from->allnext : allnodes;
85 for (; np != 0; np = np->allnext)
86 if (np->type != 0 && strcmp(np->type, type) == 0)
92 struct property *of_find_property(struct device_node *np, const char *name,
97 for (pp = np->properties; pp != 0; pp = pp->next) {
98 if (strcmp(pp->name, name) == 0) {
107 static unsigned int prom_early_allocated;
109 static void * __init prom_early_alloc(unsigned long size)
113 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
115 memset(ret, 0, size);
117 prom_early_allocated += size;
122 static int is_root_node(const struct device_node *dp)
127 return (dp->parent == NULL);
130 /* The following routines deal with the black magic of fully naming a
133 * Certain well known named nodes are just the simple name string.
135 * Actual devices have an address specifier appended to the base name
136 * string, like this "foo@addr". The "addr" can be in any number of
137 * formats, and the platform plus the type of the node determine the
138 * format and how it is constructed.
140 * For children of the ROOT node, the naming convention is fixed and
141 * determined by whether this is a sun4u or sun4v system.
143 * For children of other nodes, it is bus type specific. So
144 * we walk up the tree until we discover a "device_type" property
145 * we recognize and we go from there.
147 * As an example, the boot device on my workstation has a full path:
149 * /pci@1e,600000/ide@d/disk@0,0:c
151 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
153 struct linux_prom64_registers *regs;
154 struct property *rprop;
155 u32 high_bits, low_bits, type;
157 rprop = of_find_property(dp, "reg", NULL);
162 if (!is_root_node(dp->parent)) {
163 sprintf(tmp_buf, "%s@%x,%x",
165 (unsigned int) (regs->phys_addr >> 32UL),
166 (unsigned int) (regs->phys_addr & 0xffffffffUL));
170 type = regs->phys_addr >> 60UL;
171 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
172 low_bits = (regs->phys_addr & 0xffffffffUL);
174 if (type == 0 || type == 8) {
175 const char *prefix = (type == 0) ? "m" : "i";
178 sprintf(tmp_buf, "%s@%s%x,%x",
180 high_bits, low_bits);
182 sprintf(tmp_buf, "%s@%s%x",
186 } else if (type == 12) {
187 sprintf(tmp_buf, "%s@%x",
188 dp->name, high_bits);
192 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
194 struct linux_prom64_registers *regs;
195 struct property *prop;
197 prop = of_find_property(dp, "reg", NULL);
202 if (!is_root_node(dp->parent)) {
203 sprintf(tmp_buf, "%s@%x,%x",
205 (unsigned int) (regs->phys_addr >> 32UL),
206 (unsigned int) (regs->phys_addr & 0xffffffffUL));
210 prop = of_find_property(dp, "upa-portid", NULL);
212 prop = of_find_property(dp, "portid", NULL);
214 unsigned long mask = 0xffffffffUL;
216 if (tlb_type >= cheetah)
219 sprintf(tmp_buf, "%s@%x,%x",
222 (unsigned int) (regs->phys_addr & mask));
226 /* "name@slot,offset" */
227 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
229 struct linux_prom_registers *regs;
230 struct property *prop;
232 prop = of_find_property(dp, "reg", NULL);
237 sprintf(tmp_buf, "%s@%x,%x",
243 /* "name@devnum[,func]" */
244 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
246 struct linux_prom_pci_registers *regs;
247 struct property *prop;
250 prop = of_find_property(dp, "reg", NULL);
255 devfn = (regs->phys_hi >> 8) & 0xff;
257 sprintf(tmp_buf, "%s@%x,%x",
262 sprintf(tmp_buf, "%s@%x",
268 /* "name@UPA_PORTID,offset" */
269 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
271 struct linux_prom64_registers *regs;
272 struct property *prop;
274 prop = of_find_property(dp, "reg", NULL);
280 prop = of_find_property(dp, "upa-portid", NULL);
284 sprintf(tmp_buf, "%s@%x,%x",
286 *(u32 *) prop->value,
287 (unsigned int) (regs->phys_addr & 0xffffffffUL));
291 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
293 struct property *prop;
296 prop = of_find_property(dp, "reg", NULL);
302 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
305 /* "name@addrhi,addrlo" */
306 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
308 struct linux_prom64_registers *regs;
309 struct property *prop;
311 prop = of_find_property(dp, "reg", NULL);
317 sprintf(tmp_buf, "%s@%x,%x",
319 (unsigned int) (regs->phys_addr >> 32UL),
320 (unsigned int) (regs->phys_addr & 0xffffffffUL));
323 /* "name@bus,addr" */
324 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
326 struct property *prop;
329 prop = of_find_property(dp, "reg", NULL);
335 /* This actually isn't right... should look at the #address-cells
336 * property of the i2c bus node etc. etc.
338 sprintf(tmp_buf, "%s@%x,%x",
339 dp->name, regs[0], regs[1]);
342 /* "name@reg0[,reg1]" */
343 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
345 struct property *prop;
348 prop = of_find_property(dp, "reg", NULL);
354 if (prop->length == sizeof(u32) || regs[1] == 1) {
355 sprintf(tmp_buf, "%s@%x",
358 sprintf(tmp_buf, "%s@%x,%x",
359 dp->name, regs[0], regs[1]);
363 /* "name@reg0reg1[,reg2reg3]" */
364 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
366 struct property *prop;
369 prop = of_find_property(dp, "reg", NULL);
375 if (regs[2] || regs[3]) {
376 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
377 dp->name, regs[0], regs[1], regs[2], regs[3]);
379 sprintf(tmp_buf, "%s@%08x%08x",
380 dp->name, regs[0], regs[1]);
384 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
386 struct device_node *parent = dp->parent;
388 if (parent != NULL) {
389 if (!strcmp(parent->type, "pci") ||
390 !strcmp(parent->type, "pciex"))
391 return pci_path_component(dp, tmp_buf);
392 if (!strcmp(parent->type, "sbus"))
393 return sbus_path_component(dp, tmp_buf);
394 if (!strcmp(parent->type, "upa"))
395 return upa_path_component(dp, tmp_buf);
396 if (!strcmp(parent->type, "ebus"))
397 return ebus_path_component(dp, tmp_buf);
398 if (!strcmp(parent->name, "usb") ||
399 !strcmp(parent->name, "hub"))
400 return usb_path_component(dp, tmp_buf);
401 if (!strcmp(parent->type, "i2c"))
402 return i2c_path_component(dp, tmp_buf);
403 if (!strcmp(parent->type, "firewire"))
404 return ieee1394_path_component(dp, tmp_buf);
405 if (!strcmp(parent->type, "virtual-devices"))
406 return vdev_path_component(dp, tmp_buf);
408 /* "isa" is handled with platform naming */
411 /* Use platform naming convention. */
412 if (tlb_type == hypervisor)
413 return sun4v_path_component(dp, tmp_buf);
415 return sun4u_path_component(dp, tmp_buf);
418 static char * __init build_path_component(struct device_node *dp)
420 char tmp_buf[64], *n;
423 __build_path_component(dp, tmp_buf);
424 if (tmp_buf[0] == '\0')
425 strcpy(tmp_buf, dp->name);
427 n = prom_early_alloc(strlen(tmp_buf) + 1);
433 static char * __init build_full_name(struct device_node *dp)
435 int len, ourlen, plen;
438 plen = strlen(dp->parent->full_name);
439 ourlen = strlen(dp->path_component_name);
440 len = ourlen + plen + 2;
442 n = prom_early_alloc(len);
443 strcpy(n, dp->parent->full_name);
444 if (!is_root_node(dp->parent)) {
445 strcpy(n + plen, "/");
448 strcpy(n + plen, dp->path_component_name);
453 static struct property * __init build_one_prop(phandle node, char *prev)
455 static struct property *tmp = NULL;
460 memset(p, 0, sizeof(*p) + 32);
463 p = prom_early_alloc(sizeof(struct property) + 32);
465 p->name = (char *) (p + 1);
467 prom_firstprop(node, p->name);
469 prom_nextprop(node, prev, p->name);
471 if (strlen(p->name) == 0) {
475 p->length = prom_getproplen(node, p->name);
476 if (p->length <= 0) {
479 p->value = prom_early_alloc(p->length);
480 prom_getproperty(node, p->name, p->value, p->length);
485 static struct property * __init build_prop_list(phandle node)
487 struct property *head, *tail;
489 head = tail = build_one_prop(node, NULL);
491 tail->next = build_one_prop(node, tail->name);
498 static char * __init get_one_property(phandle node, const char *name)
500 char *buf = "<NULL>";
503 len = prom_getproplen(node, name);
505 buf = prom_early_alloc(len);
506 prom_getproperty(node, name, buf, len);
512 static struct device_node * __init create_node(phandle node)
514 struct device_node *dp;
519 dp = prom_early_alloc(sizeof(*dp));
521 kref_init(&dp->kref);
523 dp->name = get_one_property(node, "name");
524 dp->type = get_one_property(node, "device_type");
527 /* Build interrupts later... */
529 dp->properties = build_prop_list(node);
534 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
536 struct device_node *dp;
538 dp = create_node(node);
541 *nextp = &dp->allnext;
544 dp->path_component_name = build_path_component(dp);
545 dp->full_name = build_full_name(dp);
547 dp->child = build_tree(dp, prom_getchild(node), nextp);
549 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
555 void __init prom_build_devicetree(void)
557 struct device_node **nextp;
559 allnodes = create_node(prom_root_node);
560 allnodes->path_component_name = "";
561 allnodes->full_name = "/";
563 nextp = &allnodes->allnext;
564 allnodes->child = build_tree(allnodes,
565 prom_getchild(allnodes->node),
567 printk("PROM: Built device tree with %u bytes of memory.\n",
568 prom_early_allocated);