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 /* 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
)
40 cp
= (char *) of_get_property(device
, "compatible", &cplen
);
44 if (strncmp(cp
, compat
, strlen(compat
)) == 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
;
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
) {
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)
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
)
105 EXPORT_SYMBOL(of_find_node_by_phandle
);
107 struct device_node
*of_find_node_by_name(struct device_node
*from
,
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)
119 EXPORT_SYMBOL(of_find_node_by_name
);
121 struct device_node
*of_find_node_by_type(struct device_node
*from
,
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)
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
) {
143 && !(np
->type
!= 0 && strcmp(np
->type
, type
) == 0))
145 if (of_device_is_compatible(np
, compatible
))
151 EXPORT_SYMBOL(of_find_compatible_node
);
153 struct property
*of_find_property(struct device_node
*np
, const char *name
,
158 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
) {
159 if (strcmp(pp
->name
, name
) == 0) {
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
;
185 prop
= of_find_property(np
, name
, &len
);
186 if (!prop
|| len
!= 4)
189 return *(int *) prop
->value
;
191 EXPORT_SYMBOL(of_getintprop_default
);
193 int of_set_property(struct device_node
*dp
, const char *name
, void *val
, int len
)
195 struct property
**prevp
;
199 new_val
= kmalloc(len
, GFP_KERNEL
);
203 memcpy(new_val
, val
, len
);
207 write_lock(&devtree_lock
);
208 prevp
= &dp
->properties
;
210 struct property
*prop
= *prevp
;
212 if (!strcmp(prop
->name
, name
)) {
213 void *old_val
= prop
->value
;
216 ret
= prom_setprop(dp
->node
, name
, val
, len
);
219 prop
->value
= new_val
;
222 if (OF_IS_DYNAMIC(prop
))
225 OF_MARK_DYNAMIC(prop
);
231 prevp
= &(*prevp
)->next
;
233 write_unlock(&devtree_lock
);
235 /* XXX Upate procfs if necessary... */
239 EXPORT_SYMBOL(of_set_property
);
241 static unsigned int prom_early_allocated
;
243 static void * __init
prom_early_alloc(unsigned long size
)
247 ret
= __alloc_bootmem(size
, SMP_CACHE_BYTES
, 0UL);
249 memset(ret
, 0, size
);
251 prom_early_allocated
+= size
;
256 static int is_root_node(const struct device_node
*dp
)
261 return (dp
->parent
== NULL
);
264 /* The following routines deal with the black magic of fully naming a
267 * Certain well known named nodes are just the simple name string.
269 * Actual devices have an address specifier appended to the base name
270 * string, like this "foo@addr". The "addr" can be in any number of
271 * formats, and the platform plus the type of the node determine the
272 * format and how it is constructed.
274 * For children of the ROOT node, the naming convention is fixed and
275 * determined by whether this is a sun4u or sun4v system.
277 * For children of other nodes, it is bus type specific. So
278 * we walk up the tree until we discover a "device_type" property
279 * we recognize and we go from there.
281 * As an example, the boot device on my workstation has a full path:
283 * /pci@1e,600000/ide@d/disk@0,0:c
285 static void __init
sun4v_path_component(struct device_node
*dp
, char *tmp_buf
)
287 struct linux_prom64_registers
*regs
;
288 struct property
*rprop
;
289 u32 high_bits
, low_bits
, type
;
291 rprop
= of_find_property(dp
, "reg", NULL
);
296 if (!is_root_node(dp
->parent
)) {
297 sprintf(tmp_buf
, "%s@%x,%x",
299 (unsigned int) (regs
->phys_addr
>> 32UL),
300 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
304 type
= regs
->phys_addr
>> 60UL;
305 high_bits
= (regs
->phys_addr
>> 32UL) & 0x0fffffffUL
;
306 low_bits
= (regs
->phys_addr
& 0xffffffffUL
);
308 if (type
== 0 || type
== 8) {
309 const char *prefix
= (type
== 0) ? "m" : "i";
312 sprintf(tmp_buf
, "%s@%s%x,%x",
314 high_bits
, low_bits
);
316 sprintf(tmp_buf
, "%s@%s%x",
320 } else if (type
== 12) {
321 sprintf(tmp_buf
, "%s@%x",
322 dp
->name
, high_bits
);
326 static void __init
sun4u_path_component(struct device_node
*dp
, char *tmp_buf
)
328 struct linux_prom64_registers
*regs
;
329 struct property
*prop
;
331 prop
= of_find_property(dp
, "reg", NULL
);
336 if (!is_root_node(dp
->parent
)) {
337 sprintf(tmp_buf
, "%s@%x,%x",
339 (unsigned int) (regs
->phys_addr
>> 32UL),
340 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
344 prop
= of_find_property(dp
, "upa-portid", NULL
);
346 prop
= of_find_property(dp
, "portid", NULL
);
348 unsigned long mask
= 0xffffffffUL
;
350 if (tlb_type
>= cheetah
)
353 sprintf(tmp_buf
, "%s@%x,%x",
356 (unsigned int) (regs
->phys_addr
& mask
));
360 /* "name@slot,offset" */
361 static void __init
sbus_path_component(struct device_node
*dp
, char *tmp_buf
)
363 struct linux_prom_registers
*regs
;
364 struct property
*prop
;
366 prop
= of_find_property(dp
, "reg", NULL
);
371 sprintf(tmp_buf
, "%s@%x,%x",
377 /* "name@devnum[,func]" */
378 static void __init
pci_path_component(struct device_node
*dp
, char *tmp_buf
)
380 struct linux_prom_pci_registers
*regs
;
381 struct property
*prop
;
384 prop
= of_find_property(dp
, "reg", NULL
);
389 devfn
= (regs
->phys_hi
>> 8) & 0xff;
391 sprintf(tmp_buf
, "%s@%x,%x",
396 sprintf(tmp_buf
, "%s@%x",
402 /* "name@UPA_PORTID,offset" */
403 static void __init
upa_path_component(struct device_node
*dp
, char *tmp_buf
)
405 struct linux_prom64_registers
*regs
;
406 struct property
*prop
;
408 prop
= of_find_property(dp
, "reg", NULL
);
414 prop
= of_find_property(dp
, "upa-portid", NULL
);
418 sprintf(tmp_buf
, "%s@%x,%x",
420 *(u32
*) prop
->value
,
421 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
425 static void __init
vdev_path_component(struct device_node
*dp
, char *tmp_buf
)
427 struct property
*prop
;
430 prop
= of_find_property(dp
, "reg", NULL
);
436 sprintf(tmp_buf
, "%s@%x", dp
->name
, *regs
);
439 /* "name@addrhi,addrlo" */
440 static void __init
ebus_path_component(struct device_node
*dp
, char *tmp_buf
)
442 struct linux_prom64_registers
*regs
;
443 struct property
*prop
;
445 prop
= of_find_property(dp
, "reg", NULL
);
451 sprintf(tmp_buf
, "%s@%x,%x",
453 (unsigned int) (regs
->phys_addr
>> 32UL),
454 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
457 /* "name@bus,addr" */
458 static void __init
i2c_path_component(struct device_node
*dp
, char *tmp_buf
)
460 struct property
*prop
;
463 prop
= of_find_property(dp
, "reg", NULL
);
469 /* This actually isn't right... should look at the #address-cells
470 * property of the i2c bus node etc. etc.
472 sprintf(tmp_buf
, "%s@%x,%x",
473 dp
->name
, regs
[0], regs
[1]);
476 /* "name@reg0[,reg1]" */
477 static void __init
usb_path_component(struct device_node
*dp
, char *tmp_buf
)
479 struct property
*prop
;
482 prop
= of_find_property(dp
, "reg", NULL
);
488 if (prop
->length
== sizeof(u32
) || regs
[1] == 1) {
489 sprintf(tmp_buf
, "%s@%x",
492 sprintf(tmp_buf
, "%s@%x,%x",
493 dp
->name
, regs
[0], regs
[1]);
497 /* "name@reg0reg1[,reg2reg3]" */
498 static void __init
ieee1394_path_component(struct device_node
*dp
, char *tmp_buf
)
500 struct property
*prop
;
503 prop
= of_find_property(dp
, "reg", NULL
);
509 if (regs
[2] || regs
[3]) {
510 sprintf(tmp_buf
, "%s@%08x%08x,%04x%08x",
511 dp
->name
, regs
[0], regs
[1], regs
[2], regs
[3]);
513 sprintf(tmp_buf
, "%s@%08x%08x",
514 dp
->name
, regs
[0], regs
[1]);
518 static void __init
__build_path_component(struct device_node
*dp
, char *tmp_buf
)
520 struct device_node
*parent
= dp
->parent
;
522 if (parent
!= NULL
) {
523 if (!strcmp(parent
->type
, "pci") ||
524 !strcmp(parent
->type
, "pciex"))
525 return pci_path_component(dp
, tmp_buf
);
526 if (!strcmp(parent
->type
, "sbus"))
527 return sbus_path_component(dp
, tmp_buf
);
528 if (!strcmp(parent
->type
, "upa"))
529 return upa_path_component(dp
, tmp_buf
);
530 if (!strcmp(parent
->type
, "ebus"))
531 return ebus_path_component(dp
, tmp_buf
);
532 if (!strcmp(parent
->name
, "usb") ||
533 !strcmp(parent
->name
, "hub"))
534 return usb_path_component(dp
, tmp_buf
);
535 if (!strcmp(parent
->type
, "i2c"))
536 return i2c_path_component(dp
, tmp_buf
);
537 if (!strcmp(parent
->type
, "firewire"))
538 return ieee1394_path_component(dp
, tmp_buf
);
539 if (!strcmp(parent
->type
, "virtual-devices"))
540 return vdev_path_component(dp
, tmp_buf
);
542 /* "isa" is handled with platform naming */
545 /* Use platform naming convention. */
546 if (tlb_type
== hypervisor
)
547 return sun4v_path_component(dp
, tmp_buf
);
549 return sun4u_path_component(dp
, tmp_buf
);
552 static char * __init
build_path_component(struct device_node
*dp
)
554 char tmp_buf
[64], *n
;
557 __build_path_component(dp
, tmp_buf
);
558 if (tmp_buf
[0] == '\0')
559 strcpy(tmp_buf
, dp
->name
);
561 n
= prom_early_alloc(strlen(tmp_buf
) + 1);
567 static char * __init
build_full_name(struct device_node
*dp
)
569 int len
, ourlen
, plen
;
572 plen
= strlen(dp
->parent
->full_name
);
573 ourlen
= strlen(dp
->path_component_name
);
574 len
= ourlen
+ plen
+ 2;
576 n
= prom_early_alloc(len
);
577 strcpy(n
, dp
->parent
->full_name
);
578 if (!is_root_node(dp
->parent
)) {
579 strcpy(n
+ plen
, "/");
582 strcpy(n
+ plen
, dp
->path_component_name
);
587 static unsigned int unique_id
;
589 static struct property
* __init
build_one_prop(phandle node
, char *prev
, char *special_name
, void *special_val
, int special_len
)
591 static struct property
*tmp
= NULL
;
596 memset(p
, 0, sizeof(*p
) + 32);
599 p
= prom_early_alloc(sizeof(struct property
) + 32);
600 p
->unique_id
= unique_id
++;
603 p
->name
= (char *) (p
+ 1);
605 strcpy(p
->name
, special_name
);
606 p
->length
= special_len
;
607 p
->value
= prom_early_alloc(special_len
);
608 memcpy(p
->value
, special_val
, special_len
);
611 prom_firstprop(node
, p
->name
);
613 prom_nextprop(node
, prev
, p
->name
);
615 if (strlen(p
->name
) == 0) {
619 p
->length
= prom_getproplen(node
, p
->name
);
620 if (p
->length
<= 0) {
623 p
->value
= prom_early_alloc(p
->length
+ 1);
624 prom_getproperty(node
, p
->name
, p
->value
, p
->length
);
625 ((unsigned char *)p
->value
)[p
->length
] = '\0';
631 static struct property
* __init
build_prop_list(phandle node
)
633 struct property
*head
, *tail
;
635 head
= tail
= build_one_prop(node
, NULL
,
636 ".node", &node
, sizeof(node
));
638 tail
->next
= build_one_prop(node
, NULL
, NULL
, NULL
, 0);
641 tail
->next
= build_one_prop(node
, tail
->name
,
649 static char * __init
get_one_property(phandle node
, const char *name
)
651 char *buf
= "<NULL>";
654 len
= prom_getproplen(node
, name
);
656 buf
= prom_early_alloc(len
);
657 prom_getproperty(node
, name
, buf
, len
);
663 static struct device_node
* __init
create_node(phandle node
)
665 struct device_node
*dp
;
670 dp
= prom_early_alloc(sizeof(*dp
));
671 dp
->unique_id
= unique_id
++;
673 kref_init(&dp
->kref
);
675 dp
->name
= get_one_property(node
, "name");
676 dp
->type
= get_one_property(node
, "device_type");
679 /* Build interrupts later... */
681 dp
->properties
= build_prop_list(node
);
686 static struct device_node
* __init
build_tree(struct device_node
*parent
, phandle node
, struct device_node
***nextp
)
688 struct device_node
*dp
;
690 dp
= create_node(node
);
693 *nextp
= &dp
->allnext
;
696 dp
->path_component_name
= build_path_component(dp
);
697 dp
->full_name
= build_full_name(dp
);
699 dp
->child
= build_tree(dp
, prom_getchild(node
), nextp
);
701 dp
->sibling
= build_tree(parent
, prom_getsibling(node
), nextp
);
707 void __init
prom_build_devicetree(void)
709 struct device_node
**nextp
;
711 allnodes
= create_node(prom_root_node
);
712 allnodes
->path_component_name
= "";
713 allnodes
->full_name
= "/";
715 nextp
= &allnodes
->allnext
;
716 allnodes
->child
= build_tree(allnodes
,
717 prom_getchild(allnodes
->node
),
719 printk("PROM: Built device tree with %u bytes of memory.\n",
720 prom_early_allocated
);
This page took 0.088265 seconds and 5 git commands to generate.