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 sparc and sparc64 by David S. Miller davem@davemloft.net
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 #include <linux/module.h>
22 #include <linux/spinlock.h>
23 #include <linux/slab.h>
24 #include <linux/proc_fs.h>
26 struct device_node
*allnodes
;
27 struct device_node
*of_chosen
;
29 /* use when traversing tree through the allnext, child, sibling,
30 * or parent members of struct device_node.
32 DEFINE_RWLOCK(devtree_lock
);
34 int of_n_addr_cells(struct device_node
*np
)
41 ip
= of_get_property(np
, "#address-cells", NULL
);
43 return be32_to_cpup(ip
);
45 /* No #address-cells property for the root node */
46 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
48 EXPORT_SYMBOL(of_n_addr_cells
);
50 int of_n_size_cells(struct device_node
*np
)
57 ip
= of_get_property(np
, "#size-cells", NULL
);
59 return be32_to_cpup(ip
);
61 /* No #size-cells property for the root node */
62 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
64 EXPORT_SYMBOL(of_n_size_cells
);
66 #if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */
68 * of_node_get - Increment refcount of a node
69 * @node: Node to inc refcount, NULL is supported to
70 * simplify writing of callers
74 struct device_node
*of_node_get(struct device_node
*node
)
77 kref_get(&node
->kref
);
80 EXPORT_SYMBOL(of_node_get
);
82 static inline struct device_node
*kref_to_device_node(struct kref
*kref
)
84 return container_of(kref
, struct device_node
, kref
);
88 * of_node_release - release a dynamically allocated node
89 * @kref: kref element of the node to be released
91 * In of_node_put() this function is passed to kref_put()
94 static void of_node_release(struct kref
*kref
)
96 struct device_node
*node
= kref_to_device_node(kref
);
97 struct property
*prop
= node
->properties
;
99 /* We should never be releasing nodes that haven't been detached. */
100 if (!of_node_check_flag(node
, OF_DETACHED
)) {
101 pr_err("ERROR: Bad of_node_put() on %s\n", node
->full_name
);
103 kref_init(&node
->kref
);
107 if (!of_node_check_flag(node
, OF_DYNAMIC
))
111 struct property
*next
= prop
->next
;
118 prop
= node
->deadprops
;
119 node
->deadprops
= NULL
;
122 kfree(node
->full_name
);
128 * of_node_put - Decrement refcount of a node
129 * @node: Node to dec refcount, NULL is supported to
130 * simplify writing of callers
133 void of_node_put(struct device_node
*node
)
136 kref_put(&node
->kref
, of_node_release
);
138 EXPORT_SYMBOL(of_node_put
);
139 #endif /* !CONFIG_SPARC */
141 struct property
*of_find_property(const struct device_node
*np
,
150 read_lock(&devtree_lock
);
151 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
) {
152 if (of_prop_cmp(pp
->name
, name
) == 0) {
158 read_unlock(&devtree_lock
);
162 EXPORT_SYMBOL(of_find_property
);
165 * of_find_all_nodes - Get next node in global list
166 * @prev: Previous node or NULL to start iteration
167 * of_node_put() will be called on it
169 * Returns a node pointer with refcount incremented, use
170 * of_node_put() on it when done.
172 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
174 struct device_node
*np
;
176 read_lock(&devtree_lock
);
177 np
= prev
? prev
->allnext
: allnodes
;
178 for (; np
!= NULL
; np
= np
->allnext
)
182 read_unlock(&devtree_lock
);
185 EXPORT_SYMBOL(of_find_all_nodes
);
188 * Find a property with a given name for a given node
189 * and return the value.
191 const void *of_get_property(const struct device_node
*np
, const char *name
,
194 struct property
*pp
= of_find_property(np
, name
, lenp
);
196 return pp
? pp
->value
: NULL
;
198 EXPORT_SYMBOL(of_get_property
);
200 /** Checks if the given "compat" string matches one of the strings in
201 * the device's "compatible" property
203 int of_device_is_compatible(const struct device_node
*device
,
209 cp
= of_get_property(device
, "compatible", &cplen
);
213 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
222 EXPORT_SYMBOL(of_device_is_compatible
);
225 * of_machine_is_compatible - Test root of device tree for a given compatible value
226 * @compat: compatible string to look for in root node's compatible property.
228 * Returns true if the root node has the given value in its
229 * compatible property.
231 int of_machine_is_compatible(const char *compat
)
233 struct device_node
*root
;
236 root
= of_find_node_by_path("/");
238 rc
= of_device_is_compatible(root
, compat
);
243 EXPORT_SYMBOL(of_machine_is_compatible
);
246 * of_device_is_available - check if a device is available for use
248 * @device: Node to check for availability
250 * Returns 1 if the status property is absent or set to "okay" or "ok",
253 int of_device_is_available(const struct device_node
*device
)
258 status
= of_get_property(device
, "status", &statlen
);
263 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
269 EXPORT_SYMBOL(of_device_is_available
);
272 * of_get_parent - Get a node's parent if any
273 * @node: Node to get parent
275 * Returns a node pointer with refcount incremented, use
276 * of_node_put() on it when done.
278 struct device_node
*of_get_parent(const struct device_node
*node
)
280 struct device_node
*np
;
285 read_lock(&devtree_lock
);
286 np
= of_node_get(node
->parent
);
287 read_unlock(&devtree_lock
);
290 EXPORT_SYMBOL(of_get_parent
);
293 * of_get_next_parent - Iterate to a node's parent
294 * @node: Node to get parent of
296 * This is like of_get_parent() except that it drops the
297 * refcount on the passed node, making it suitable for iterating
298 * through a node's parents.
300 * Returns a node pointer with refcount incremented, use
301 * of_node_put() on it when done.
303 struct device_node
*of_get_next_parent(struct device_node
*node
)
305 struct device_node
*parent
;
310 read_lock(&devtree_lock
);
311 parent
= of_node_get(node
->parent
);
313 read_unlock(&devtree_lock
);
318 * of_get_next_child - Iterate a node childs
320 * @prev: previous child of the parent node, or NULL to get first
322 * Returns a node pointer with refcount incremented, use
323 * of_node_put() on it when done.
325 struct device_node
*of_get_next_child(const struct device_node
*node
,
326 struct device_node
*prev
)
328 struct device_node
*next
;
330 read_lock(&devtree_lock
);
331 next
= prev
? prev
->sibling
: node
->child
;
332 for (; next
; next
= next
->sibling
)
333 if (of_node_get(next
))
336 read_unlock(&devtree_lock
);
339 EXPORT_SYMBOL(of_get_next_child
);
342 * of_find_node_by_path - Find a node matching a full OF path
343 * @path: The full path to match
345 * Returns a node pointer with refcount incremented, use
346 * of_node_put() on it when done.
348 struct device_node
*of_find_node_by_path(const char *path
)
350 struct device_node
*np
= allnodes
;
352 read_lock(&devtree_lock
);
353 for (; np
; np
= np
->allnext
) {
354 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
358 read_unlock(&devtree_lock
);
361 EXPORT_SYMBOL(of_find_node_by_path
);
364 * of_find_node_by_name - Find a node by its "name" property
365 * @from: The node to start searching from or NULL, the node
366 * you pass will not be searched, only the next one
367 * will; typically, you pass what the previous call
368 * returned. of_node_put() will be called on it
369 * @name: The name string to match against
371 * Returns a node pointer with refcount incremented, use
372 * of_node_put() on it when done.
374 struct device_node
*of_find_node_by_name(struct device_node
*from
,
377 struct device_node
*np
;
379 read_lock(&devtree_lock
);
380 np
= from
? from
->allnext
: allnodes
;
381 for (; np
; np
= np
->allnext
)
382 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
386 read_unlock(&devtree_lock
);
389 EXPORT_SYMBOL(of_find_node_by_name
);
392 * of_find_node_by_type - Find a node by its "device_type" property
393 * @from: The node to start searching from, or NULL to start searching
394 * the entire device tree. The node you pass will not be
395 * searched, only the next one will; typically, you pass
396 * what the previous call returned. of_node_put() will be
397 * called on from for you.
398 * @type: The type string to match against
400 * Returns a node pointer with refcount incremented, use
401 * of_node_put() on it when done.
403 struct device_node
*of_find_node_by_type(struct device_node
*from
,
406 struct device_node
*np
;
408 read_lock(&devtree_lock
);
409 np
= from
? from
->allnext
: allnodes
;
410 for (; np
; np
= np
->allnext
)
411 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
415 read_unlock(&devtree_lock
);
418 EXPORT_SYMBOL(of_find_node_by_type
);
421 * of_find_compatible_node - Find a node based on type and one of the
422 * tokens in its "compatible" property
423 * @from: The node to start searching from or NULL, the node
424 * you pass will not be searched, only the next one
425 * will; typically, you pass what the previous call
426 * returned. of_node_put() will be called on it
427 * @type: The type string to match "device_type" or NULL to ignore
428 * @compatible: The string to match to one of the tokens in the device
431 * Returns a node pointer with refcount incremented, use
432 * of_node_put() on it when done.
434 struct device_node
*of_find_compatible_node(struct device_node
*from
,
435 const char *type
, const char *compatible
)
437 struct device_node
*np
;
439 read_lock(&devtree_lock
);
440 np
= from
? from
->allnext
: allnodes
;
441 for (; np
; np
= np
->allnext
) {
443 && !(np
->type
&& (of_node_cmp(np
->type
, type
) == 0)))
445 if (of_device_is_compatible(np
, compatible
) && of_node_get(np
))
449 read_unlock(&devtree_lock
);
452 EXPORT_SYMBOL(of_find_compatible_node
);
455 * of_find_node_with_property - Find a node which has a property with
457 * @from: The node to start searching from or NULL, the node
458 * you pass will not be searched, only the next one
459 * will; typically, you pass what the previous call
460 * returned. of_node_put() will be called on it
461 * @prop_name: The name of the property to look for.
463 * Returns a node pointer with refcount incremented, use
464 * of_node_put() on it when done.
466 struct device_node
*of_find_node_with_property(struct device_node
*from
,
467 const char *prop_name
)
469 struct device_node
*np
;
472 read_lock(&devtree_lock
);
473 np
= from
? from
->allnext
: allnodes
;
474 for (; np
; np
= np
->allnext
) {
475 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
) {
476 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
484 read_unlock(&devtree_lock
);
487 EXPORT_SYMBOL(of_find_node_with_property
);
490 * of_match_node - Tell if an device_node has a matching of_match structure
491 * @matches: array of of device match structures to search in
492 * @node: the of device structure to match against
494 * Low level utility function used by device matching.
496 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
497 const struct device_node
*node
)
502 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
504 if (matches
->name
[0])
506 && !strcmp(matches
->name
, node
->name
);
507 if (matches
->type
[0])
509 && !strcmp(matches
->type
, node
->type
);
510 if (matches
->compatible
[0])
511 match
&= of_device_is_compatible(node
,
512 matches
->compatible
);
519 EXPORT_SYMBOL(of_match_node
);
522 * of_find_matching_node - Find a node based on an of_device_id match
524 * @from: The node to start searching from or NULL, the node
525 * you pass will not be searched, only the next one
526 * will; typically, you pass what the previous call
527 * returned. of_node_put() will be called on it
528 * @matches: array of of device match structures to search in
530 * Returns a node pointer with refcount incremented, use
531 * of_node_put() on it when done.
533 struct device_node
*of_find_matching_node(struct device_node
*from
,
534 const struct of_device_id
*matches
)
536 struct device_node
*np
;
538 read_lock(&devtree_lock
);
539 np
= from
? from
->allnext
: allnodes
;
540 for (; np
; np
= np
->allnext
) {
541 if (of_match_node(matches
, np
) && of_node_get(np
))
545 read_unlock(&devtree_lock
);
548 EXPORT_SYMBOL(of_find_matching_node
);
551 * of_modalias_node - Lookup appropriate modalias for a device node
552 * @node: pointer to a device tree node
553 * @modalias: Pointer to buffer that modalias value will be copied into
554 * @len: Length of modalias value
556 * Based on the value of the compatible property, this routine will attempt
557 * to choose an appropriate modalias value for a particular device tree node.
558 * It does this by stripping the manufacturer prefix (as delimited by a ',')
559 * from the first entry in the compatible list property.
561 * This routine returns 0 on success, <0 on failure.
563 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
565 const char *compatible
, *p
;
568 compatible
= of_get_property(node
, "compatible", &cplen
);
569 if (!compatible
|| strlen(compatible
) > cplen
)
571 p
= strchr(compatible
, ',');
572 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
575 EXPORT_SYMBOL_GPL(of_modalias_node
);
578 * of_find_node_by_phandle - Find a node given a phandle
579 * @handle: phandle of the node to find
581 * Returns a node pointer with refcount incremented, use
582 * of_node_put() on it when done.
584 struct device_node
*of_find_node_by_phandle(phandle handle
)
586 struct device_node
*np
;
588 read_lock(&devtree_lock
);
589 for (np
= allnodes
; np
; np
= np
->allnext
)
590 if (np
->phandle
== handle
)
593 read_unlock(&devtree_lock
);
596 EXPORT_SYMBOL(of_find_node_by_phandle
);
599 * of_property_read_u32_array - Find and read an array of 32 bit integers
602 * @np: device node from which the property value is to be read.
603 * @propname: name of the property to be searched.
604 * @out_value: pointer to return value, modified only if return value is 0.
606 * Search for a property in a device node and read 32-bit value(s) from
607 * it. Returns 0 on success, -EINVAL if the property does not exist,
608 * -ENODATA if property does not have a value, and -EOVERFLOW if the
609 * property data isn't large enough.
611 * The out_value is modified only if a valid u32 value can be decoded.
613 int of_property_read_u32_array(const struct device_node
*np
, char *propname
,
614 u32
*out_values
, size_t sz
)
616 struct property
*prop
= of_find_property(np
, propname
, NULL
);
623 if ((sz
* sizeof(*out_values
)) > prop
->length
)
628 *out_values
++ = be32_to_cpup(val
++);
631 EXPORT_SYMBOL_GPL(of_property_read_u32_array
);
634 * of_property_read_string - Find and read a string from a property
635 * @np: device node from which the property value is to be read.
636 * @propname: name of the property to be searched.
637 * @out_string: pointer to null terminated return string, modified only if
640 * Search for a property in a device tree node and retrieve a null
641 * terminated string value (pointer to data, not a copy). Returns 0 on
642 * success, -EINVAL if the property does not exist, -ENODATA if property
643 * does not have a value, and -EILSEQ if the string is not null-terminated
644 * within the length of the property data.
646 * The out_string pointer is modified only if a valid string can be decoded.
648 int of_property_read_string(struct device_node
*np
, char *propname
,
649 const char **out_string
)
651 struct property
*prop
= of_find_property(np
, propname
, NULL
);
656 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
658 *out_string
= prop
->value
;
661 EXPORT_SYMBOL_GPL(of_property_read_string
);
664 * of_parse_phandle - Resolve a phandle property to a device_node pointer
665 * @np: Pointer to device node holding phandle property
666 * @phandle_name: Name of property holding a phandle value
667 * @index: For properties holding a table of phandles, this is the index into
670 * Returns the device_node pointer with refcount incremented. Use
671 * of_node_put() on it when done.
674 of_parse_phandle(struct device_node
*np
, const char *phandle_name
, int index
)
676 const __be32
*phandle
;
679 phandle
= of_get_property(np
, phandle_name
, &size
);
680 if ((!phandle
) || (size
< sizeof(*phandle
) * (index
+ 1)))
683 return of_find_node_by_phandle(be32_to_cpup(phandle
+ index
));
685 EXPORT_SYMBOL(of_parse_phandle
);
688 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
689 * @np: pointer to a device tree node containing a list
690 * @list_name: property name that contains a list
691 * @cells_name: property name that specifies phandles' arguments count
692 * @index: index of a phandle to parse out
693 * @out_node: optional pointer to device_node struct pointer (will be filled)
694 * @out_args: optional pointer to arguments pointer (will be filled)
696 * This function is useful to parse lists of phandles and their arguments.
697 * Returns 0 on success and fills out_node and out_args, on error returns
698 * appropriate errno value.
711 * list = <&phandle1 1 2 &phandle2 3>;
714 * To get a device_node of the `node2' node you may call this:
715 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
717 int of_parse_phandles_with_args(struct device_node
*np
, const char *list_name
,
718 const char *cells_name
, int index
,
719 struct device_node
**out_node
,
720 const void **out_args
)
724 const __be32
*list_end
;
727 struct device_node
*node
= NULL
;
728 const void *args
= NULL
;
730 list
= of_get_property(np
, list_name
, &size
);
735 list_end
= list
+ size
/ sizeof(*list
);
737 while (list
< list_end
) {
741 phandle
= be32_to_cpup(list
++);
744 /* one cell hole in the list = <>; */
748 node
= of_find_node_by_phandle(phandle
);
750 pr_debug("%s: could not find phandle\n",
755 cells
= of_get_property(node
, cells_name
, &size
);
756 if (!cells
|| size
!= sizeof(*cells
)) {
757 pr_debug("%s: could not get %s for %s\n",
758 np
->full_name
, cells_name
, node
->full_name
);
762 list
+= be32_to_cpup(cells
);
763 if (list
> list_end
) {
764 pr_debug("%s: insufficient arguments length\n",
769 if (cur_index
== index
)
780 * args w/o node indicates that the loop above has stopped at
781 * the 'hole' cell. Report this differently.
799 pr_debug("%s failed with status %d\n", __func__
, ret
);
802 EXPORT_SYMBOL(of_parse_phandles_with_args
);
805 * prom_add_property - Add a property to a node
807 int prom_add_property(struct device_node
*np
, struct property
*prop
)
809 struct property
**next
;
813 write_lock_irqsave(&devtree_lock
, flags
);
814 next
= &np
->properties
;
816 if (strcmp(prop
->name
, (*next
)->name
) == 0) {
817 /* duplicate ! don't insert it */
818 write_unlock_irqrestore(&devtree_lock
, flags
);
821 next
= &(*next
)->next
;
824 write_unlock_irqrestore(&devtree_lock
, flags
);
826 #ifdef CONFIG_PROC_DEVICETREE
827 /* try to add to proc as well if it was initialized */
829 proc_device_tree_add_prop(np
->pde
, prop
);
830 #endif /* CONFIG_PROC_DEVICETREE */
836 * prom_remove_property - Remove a property from a node.
838 * Note that we don't actually remove it, since we have given out
839 * who-knows-how-many pointers to the data using get-property.
840 * Instead we just move the property to the "dead properties"
841 * list, so it won't be found any more.
843 int prom_remove_property(struct device_node
*np
, struct property
*prop
)
845 struct property
**next
;
849 write_lock_irqsave(&devtree_lock
, flags
);
850 next
= &np
->properties
;
855 prop
->next
= np
->deadprops
;
856 np
->deadprops
= prop
;
860 next
= &(*next
)->next
;
862 write_unlock_irqrestore(&devtree_lock
, flags
);
867 #ifdef CONFIG_PROC_DEVICETREE
868 /* try to remove the proc node as well */
870 proc_device_tree_remove_prop(np
->pde
, prop
);
871 #endif /* CONFIG_PROC_DEVICETREE */
877 * prom_update_property - Update a property in a node.
879 * Note that we don't actually remove it, since we have given out
880 * who-knows-how-many pointers to the data using get-property.
881 * Instead we just move the property to the "dead properties" list,
882 * and add the new property to the property list
884 int prom_update_property(struct device_node
*np
,
885 struct property
*newprop
,
886 struct property
*oldprop
)
888 struct property
**next
;
892 write_lock_irqsave(&devtree_lock
, flags
);
893 next
= &np
->properties
;
895 if (*next
== oldprop
) {
897 newprop
->next
= oldprop
->next
;
899 oldprop
->next
= np
->deadprops
;
900 np
->deadprops
= oldprop
;
904 next
= &(*next
)->next
;
906 write_unlock_irqrestore(&devtree_lock
, flags
);
911 #ifdef CONFIG_PROC_DEVICETREE
912 /* try to add to proc as well if it was initialized */
914 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
915 #endif /* CONFIG_PROC_DEVICETREE */
920 #if defined(CONFIG_OF_DYNAMIC)
922 * Support for dynamic device trees.
924 * On some platforms, the device tree can be manipulated at runtime.
925 * The routines in this section support adding, removing and changing
930 * of_attach_node - Plug a device node into the tree and global list.
932 void of_attach_node(struct device_node
*np
)
936 write_lock_irqsave(&devtree_lock
, flags
);
937 np
->sibling
= np
->parent
->child
;
938 np
->allnext
= allnodes
;
939 np
->parent
->child
= np
;
941 write_unlock_irqrestore(&devtree_lock
, flags
);
945 * of_detach_node - "Unplug" a node from the device tree.
947 * The caller must hold a reference to the node. The memory associated with
948 * the node is not freed until its refcount goes to zero.
950 void of_detach_node(struct device_node
*np
)
952 struct device_node
*parent
;
955 write_lock_irqsave(&devtree_lock
, flags
);
962 allnodes
= np
->allnext
;
964 struct device_node
*prev
;
965 for (prev
= allnodes
;
967 prev
= prev
->allnext
)
969 prev
->allnext
= np
->allnext
;
972 if (parent
->child
== np
)
973 parent
->child
= np
->sibling
;
975 struct device_node
*prevsib
;
976 for (prevsib
= np
->parent
->child
;
977 prevsib
->sibling
!= np
;
978 prevsib
= prevsib
->sibling
)
980 prevsib
->sibling
= np
->sibling
;
983 of_node_set_flag(np
, OF_DETACHED
);
986 write_unlock_irqrestore(&devtree_lock
, flags
);
988 #endif /* defined(CONFIG_OF_DYNAMIC) */