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/proc_fs.h>
25 struct device_node
*allnodes
;
26 struct device_node
*of_chosen
;
28 /* use when traversing tree through the allnext, child, sibling,
29 * or parent members of struct device_node.
31 DEFINE_RWLOCK(devtree_lock
);
33 int of_n_addr_cells(struct device_node
*np
)
40 ip
= of_get_property(np
, "#address-cells", NULL
);
42 return be32_to_cpup(ip
);
44 /* No #address-cells property for the root node */
45 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
47 EXPORT_SYMBOL(of_n_addr_cells
);
49 int of_n_size_cells(struct device_node
*np
)
56 ip
= of_get_property(np
, "#size-cells", NULL
);
58 return be32_to_cpup(ip
);
60 /* No #size-cells property for the root node */
61 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
63 EXPORT_SYMBOL(of_n_size_cells
);
65 #if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */
67 * of_node_get - Increment refcount of a node
68 * @node: Node to inc refcount, NULL is supported to
69 * simplify writing of callers
73 struct device_node
*of_node_get(struct device_node
*node
)
76 kref_get(&node
->kref
);
79 EXPORT_SYMBOL(of_node_get
);
81 static inline struct device_node
*kref_to_device_node(struct kref
*kref
)
83 return container_of(kref
, struct device_node
, kref
);
87 * of_node_release - release a dynamically allocated node
88 * @kref: kref element of the node to be released
90 * In of_node_put() this function is passed to kref_put()
93 static void of_node_release(struct kref
*kref
)
95 struct device_node
*node
= kref_to_device_node(kref
);
96 struct property
*prop
= node
->properties
;
98 /* We should never be releasing nodes that haven't been detached. */
99 if (!of_node_check_flag(node
, OF_DETACHED
)) {
100 pr_err("ERROR: Bad of_node_put() on %s\n", node
->full_name
);
102 kref_init(&node
->kref
);
106 if (!of_node_check_flag(node
, OF_DYNAMIC
))
110 struct property
*next
= prop
->next
;
117 prop
= node
->deadprops
;
118 node
->deadprops
= NULL
;
121 kfree(node
->full_name
);
127 * of_node_put - Decrement refcount of a node
128 * @node: Node to dec refcount, NULL is supported to
129 * simplify writing of callers
132 void of_node_put(struct device_node
*node
)
135 kref_put(&node
->kref
, of_node_release
);
137 EXPORT_SYMBOL(of_node_put
);
138 #endif /* !CONFIG_SPARC */
140 struct property
*of_find_property(const struct device_node
*np
,
149 read_lock(&devtree_lock
);
150 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
) {
151 if (of_prop_cmp(pp
->name
, name
) == 0) {
157 read_unlock(&devtree_lock
);
161 EXPORT_SYMBOL(of_find_property
);
164 * of_find_all_nodes - Get next node in global list
165 * @prev: Previous node or NULL to start iteration
166 * of_node_put() will be called on it
168 * Returns a node pointer with refcount incremented, use
169 * of_node_put() on it when done.
171 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
173 struct device_node
*np
;
175 read_lock(&devtree_lock
);
176 np
= prev
? prev
->allnext
: allnodes
;
177 for (; np
!= NULL
; np
= np
->allnext
)
181 read_unlock(&devtree_lock
);
184 EXPORT_SYMBOL(of_find_all_nodes
);
187 * Find a property with a given name for a given node
188 * and return the value.
190 const void *of_get_property(const struct device_node
*np
, const char *name
,
193 struct property
*pp
= of_find_property(np
, name
, lenp
);
195 return pp
? pp
->value
: NULL
;
197 EXPORT_SYMBOL(of_get_property
);
199 /** Checks if the given "compat" string matches one of the strings in
200 * the device's "compatible" property
202 int of_device_is_compatible(const struct device_node
*device
,
208 cp
= of_get_property(device
, "compatible", &cplen
);
212 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
221 EXPORT_SYMBOL(of_device_is_compatible
);
224 * of_machine_is_compatible - Test root of device tree for a given compatible value
225 * @compat: compatible string to look for in root node's compatible property.
227 * Returns true if the root node has the given value in its
228 * compatible property.
230 int of_machine_is_compatible(const char *compat
)
232 struct device_node
*root
;
235 root
= of_find_node_by_path("/");
237 rc
= of_device_is_compatible(root
, compat
);
242 EXPORT_SYMBOL(of_machine_is_compatible
);
245 * of_device_is_available - check if a device is available for use
247 * @device: Node to check for availability
249 * Returns 1 if the status property is absent or set to "okay" or "ok",
252 int of_device_is_available(const struct device_node
*device
)
257 status
= of_get_property(device
, "status", &statlen
);
262 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
268 EXPORT_SYMBOL(of_device_is_available
);
271 * of_get_parent - Get a node's parent if any
272 * @node: Node to get parent
274 * Returns a node pointer with refcount incremented, use
275 * of_node_put() on it when done.
277 struct device_node
*of_get_parent(const struct device_node
*node
)
279 struct device_node
*np
;
284 read_lock(&devtree_lock
);
285 np
= of_node_get(node
->parent
);
286 read_unlock(&devtree_lock
);
289 EXPORT_SYMBOL(of_get_parent
);
292 * of_get_next_parent - Iterate to a node's parent
293 * @node: Node to get parent of
295 * This is like of_get_parent() except that it drops the
296 * refcount on the passed node, making it suitable for iterating
297 * through a node's parents.
299 * Returns a node pointer with refcount incremented, use
300 * of_node_put() on it when done.
302 struct device_node
*of_get_next_parent(struct device_node
*node
)
304 struct device_node
*parent
;
309 read_lock(&devtree_lock
);
310 parent
= of_node_get(node
->parent
);
312 read_unlock(&devtree_lock
);
317 * of_get_next_child - Iterate a node childs
319 * @prev: previous child of the parent node, or NULL to get first
321 * Returns a node pointer with refcount incremented, use
322 * of_node_put() on it when done.
324 struct device_node
*of_get_next_child(const struct device_node
*node
,
325 struct device_node
*prev
)
327 struct device_node
*next
;
329 read_lock(&devtree_lock
);
330 next
= prev
? prev
->sibling
: node
->child
;
331 for (; next
; next
= next
->sibling
)
332 if (of_node_get(next
))
335 read_unlock(&devtree_lock
);
338 EXPORT_SYMBOL(of_get_next_child
);
341 * of_find_node_by_path - Find a node matching a full OF path
342 * @path: The full path to match
344 * Returns a node pointer with refcount incremented, use
345 * of_node_put() on it when done.
347 struct device_node
*of_find_node_by_path(const char *path
)
349 struct device_node
*np
= allnodes
;
351 read_lock(&devtree_lock
);
352 for (; np
; np
= np
->allnext
) {
353 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
357 read_unlock(&devtree_lock
);
360 EXPORT_SYMBOL(of_find_node_by_path
);
363 * of_find_node_by_name - Find a node by its "name" property
364 * @from: The node to start searching from or NULL, the node
365 * you pass will not be searched, only the next one
366 * will; typically, you pass what the previous call
367 * returned. of_node_put() will be called on it
368 * @name: The name string to match against
370 * Returns a node pointer with refcount incremented, use
371 * of_node_put() on it when done.
373 struct device_node
*of_find_node_by_name(struct device_node
*from
,
376 struct device_node
*np
;
378 read_lock(&devtree_lock
);
379 np
= from
? from
->allnext
: allnodes
;
380 for (; np
; np
= np
->allnext
)
381 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
385 read_unlock(&devtree_lock
);
388 EXPORT_SYMBOL(of_find_node_by_name
);
391 * of_find_node_by_type - Find a node by its "device_type" property
392 * @from: The node to start searching from, or NULL to start searching
393 * the entire device tree. The node you pass will not be
394 * searched, only the next one will; typically, you pass
395 * what the previous call returned. of_node_put() will be
396 * called on from for you.
397 * @type: The type string to match against
399 * Returns a node pointer with refcount incremented, use
400 * of_node_put() on it when done.
402 struct device_node
*of_find_node_by_type(struct device_node
*from
,
405 struct device_node
*np
;
407 read_lock(&devtree_lock
);
408 np
= from
? from
->allnext
: allnodes
;
409 for (; np
; np
= np
->allnext
)
410 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
414 read_unlock(&devtree_lock
);
417 EXPORT_SYMBOL(of_find_node_by_type
);
420 * of_find_compatible_node - Find a node based on type and one of the
421 * tokens in its "compatible" property
422 * @from: The node to start searching from or NULL, the node
423 * you pass will not be searched, only the next one
424 * will; typically, you pass what the previous call
425 * returned. of_node_put() will be called on it
426 * @type: The type string to match "device_type" or NULL to ignore
427 * @compatible: The string to match to one of the tokens in the device
430 * Returns a node pointer with refcount incremented, use
431 * of_node_put() on it when done.
433 struct device_node
*of_find_compatible_node(struct device_node
*from
,
434 const char *type
, const char *compatible
)
436 struct device_node
*np
;
438 read_lock(&devtree_lock
);
439 np
= from
? from
->allnext
: allnodes
;
440 for (; np
; np
= np
->allnext
) {
442 && !(np
->type
&& (of_node_cmp(np
->type
, type
) == 0)))
444 if (of_device_is_compatible(np
, compatible
) && of_node_get(np
))
448 read_unlock(&devtree_lock
);
451 EXPORT_SYMBOL(of_find_compatible_node
);
454 * of_find_node_with_property - Find a node which has a property with
456 * @from: The node to start searching from or NULL, the node
457 * you pass will not be searched, only the next one
458 * will; typically, you pass what the previous call
459 * returned. of_node_put() will be called on it
460 * @prop_name: The name of the property to look for.
462 * Returns a node pointer with refcount incremented, use
463 * of_node_put() on it when done.
465 struct device_node
*of_find_node_with_property(struct device_node
*from
,
466 const char *prop_name
)
468 struct device_node
*np
;
471 read_lock(&devtree_lock
);
472 np
= from
? from
->allnext
: allnodes
;
473 for (; np
; np
= np
->allnext
) {
474 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
) {
475 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
483 read_unlock(&devtree_lock
);
486 EXPORT_SYMBOL(of_find_node_with_property
);
489 * of_match_node - Tell if an device_node has a matching of_match structure
490 * @matches: array of of device match structures to search in
491 * @node: the of device structure to match against
493 * Low level utility function used by device matching.
495 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
496 const struct device_node
*node
)
498 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
500 if (matches
->name
[0])
502 && !strcmp(matches
->name
, node
->name
);
503 if (matches
->type
[0])
505 && !strcmp(matches
->type
, node
->type
);
506 if (matches
->compatible
[0])
507 match
&= of_device_is_compatible(node
,
508 matches
->compatible
);
515 EXPORT_SYMBOL(of_match_node
);
518 * of_find_matching_node - Find a node based on an of_device_id match
520 * @from: The node to start searching from or NULL, the node
521 * you pass will not be searched, only the next one
522 * will; typically, you pass what the previous call
523 * returned. of_node_put() will be called on it
524 * @matches: array of of device match structures to search in
526 * Returns a node pointer with refcount incremented, use
527 * of_node_put() on it when done.
529 struct device_node
*of_find_matching_node(struct device_node
*from
,
530 const struct of_device_id
*matches
)
532 struct device_node
*np
;
534 read_lock(&devtree_lock
);
535 np
= from
? from
->allnext
: allnodes
;
536 for (; np
; np
= np
->allnext
) {
537 if (of_match_node(matches
, np
) && of_node_get(np
))
541 read_unlock(&devtree_lock
);
544 EXPORT_SYMBOL(of_find_matching_node
);
547 * of_modalias_table: Table of explicit compatible ==> modalias mappings
549 * This table allows particulare compatible property values to be mapped
550 * to modalias strings. This is useful for busses which do not directly
551 * understand the OF device tree but are populated based on data contained
552 * within the device tree. SPI and I2C are the two current users of this
555 * In most cases, devices do not need to be listed in this table because
556 * the modalias value can be derived directly from the compatible table.
557 * However, if for any reason a value cannot be derived, then this table
558 * provides a method to override the implicit derivation.
560 * At the moment, a single table is used for all bus types because it is
561 * assumed that the data size is small and that the compatible values
562 * should already be distinct enough to differentiate between SPI, I2C
565 struct of_modalias_table
{
569 static struct of_modalias_table of_modalias_table
[] = {
570 { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
571 { "mmc-spi-slot", "mmc_spi" },
575 * of_modalias_node - Lookup appropriate modalias for a device node
576 * @node: pointer to a device tree node
577 * @modalias: Pointer to buffer that modalias value will be copied into
578 * @len: Length of modalias value
580 * Based on the value of the compatible property, this routine will determine
581 * an appropriate modalias value for a particular device tree node. Two
582 * separate methods are attempted to derive a modalias value.
584 * First method is to lookup the compatible value in of_modalias_table.
585 * Second is to strip off the manufacturer prefix from the first
586 * compatible entry and use the remainder as modalias
588 * This routine returns 0 on success
590 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
593 const char *compatible
;
596 /* 1. search for exception list entry */
597 for (i
= 0; i
< ARRAY_SIZE(of_modalias_table
); i
++) {
598 compatible
= of_modalias_table
[i
].of_device
;
599 if (!of_device_is_compatible(node
, compatible
))
601 strlcpy(modalias
, of_modalias_table
[i
].modalias
, len
);
605 compatible
= of_get_property(node
, "compatible", &cplen
);
609 /* 2. take first compatible entry and strip manufacturer */
610 p
= strchr(compatible
, ',');
614 strlcpy(modalias
, p
, len
);
617 EXPORT_SYMBOL_GPL(of_modalias_node
);
620 * of_find_node_by_phandle - Find a node given a phandle
621 * @handle: phandle of the node to find
623 * Returns a node pointer with refcount incremented, use
624 * of_node_put() on it when done.
626 struct device_node
*of_find_node_by_phandle(phandle handle
)
628 struct device_node
*np
;
630 read_lock(&devtree_lock
);
631 for (np
= allnodes
; np
; np
= np
->allnext
)
632 if (np
->phandle
== handle
)
635 read_unlock(&devtree_lock
);
638 EXPORT_SYMBOL(of_find_node_by_phandle
);
641 * of_parse_phandle - Resolve a phandle property to a device_node pointer
642 * @np: Pointer to device node holding phandle property
643 * @phandle_name: Name of property holding a phandle value
644 * @index: For properties holding a table of phandles, this is the index into
647 * Returns the device_node pointer with refcount incremented. Use
648 * of_node_put() on it when done.
651 of_parse_phandle(struct device_node
*np
, const char *phandle_name
, int index
)
653 const phandle
*phandle
;
656 phandle
= of_get_property(np
, phandle_name
, &size
);
657 if ((!phandle
) || (size
< sizeof(*phandle
) * (index
+ 1)))
660 return of_find_node_by_phandle(phandle
[index
]);
662 EXPORT_SYMBOL(of_parse_phandle
);
665 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
666 * @np: pointer to a device tree node containing a list
667 * @list_name: property name that contains a list
668 * @cells_name: property name that specifies phandles' arguments count
669 * @index: index of a phandle to parse out
670 * @out_node: optional pointer to device_node struct pointer (will be filled)
671 * @out_args: optional pointer to arguments pointer (will be filled)
673 * This function is useful to parse lists of phandles and their arguments.
674 * Returns 0 on success and fills out_node and out_args, on error returns
675 * appropriate errno value.
688 * list = <&phandle1 1 2 &phandle2 3>;
691 * To get a device_node of the `node2' node you may call this:
692 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
694 int of_parse_phandles_with_args(struct device_node
*np
, const char *list_name
,
695 const char *cells_name
, int index
,
696 struct device_node
**out_node
,
697 const void **out_args
)
701 const __be32
*list_end
;
704 struct device_node
*node
= NULL
;
705 const void *args
= NULL
;
707 list
= of_get_property(np
, list_name
, &size
);
712 list_end
= list
+ size
/ sizeof(*list
);
714 while (list
< list_end
) {
716 const phandle
*phandle
;
721 /* one cell hole in the list = <>; */
725 node
= of_find_node_by_phandle(*phandle
);
727 pr_debug("%s: could not find phandle\n",
732 cells
= of_get_property(node
, cells_name
, &size
);
733 if (!cells
|| size
!= sizeof(*cells
)) {
734 pr_debug("%s: could not get %s for %s\n",
735 np
->full_name
, cells_name
, node
->full_name
);
739 list
+= be32_to_cpup(cells
);
740 if (list
> list_end
) {
741 pr_debug("%s: insufficient arguments length\n",
746 if (cur_index
== index
)
757 * args w/o node indicates that the loop above has stopped at
758 * the 'hole' cell. Report this differently.
776 pr_debug("%s failed with status %d\n", __func__
, ret
);
779 EXPORT_SYMBOL(of_parse_phandles_with_args
);
782 * prom_add_property - Add a property to a node
784 int prom_add_property(struct device_node
*np
, struct property
*prop
)
786 struct property
**next
;
790 write_lock_irqsave(&devtree_lock
, flags
);
791 next
= &np
->properties
;
793 if (strcmp(prop
->name
, (*next
)->name
) == 0) {
794 /* duplicate ! don't insert it */
795 write_unlock_irqrestore(&devtree_lock
, flags
);
798 next
= &(*next
)->next
;
801 write_unlock_irqrestore(&devtree_lock
, flags
);
803 #ifdef CONFIG_PROC_DEVICETREE
804 /* try to add to proc as well if it was initialized */
806 proc_device_tree_add_prop(np
->pde
, prop
);
807 #endif /* CONFIG_PROC_DEVICETREE */
813 * prom_remove_property - Remove a property from a node.
815 * Note that we don't actually remove it, since we have given out
816 * who-knows-how-many pointers to the data using get-property.
817 * Instead we just move the property to the "dead properties"
818 * list, so it won't be found any more.
820 int prom_remove_property(struct device_node
*np
, struct property
*prop
)
822 struct property
**next
;
826 write_lock_irqsave(&devtree_lock
, flags
);
827 next
= &np
->properties
;
832 prop
->next
= np
->deadprops
;
833 np
->deadprops
= prop
;
837 next
= &(*next
)->next
;
839 write_unlock_irqrestore(&devtree_lock
, flags
);
844 #ifdef CONFIG_PROC_DEVICETREE
845 /* try to remove the proc node as well */
847 proc_device_tree_remove_prop(np
->pde
, prop
);
848 #endif /* CONFIG_PROC_DEVICETREE */
854 * prom_update_property - Update a property in a node.
856 * Note that we don't actually remove it, since we have given out
857 * who-knows-how-many pointers to the data using get-property.
858 * Instead we just move the property to the "dead properties" list,
859 * and add the new property to the property list
861 int prom_update_property(struct device_node
*np
,
862 struct property
*newprop
,
863 struct property
*oldprop
)
865 struct property
**next
;
869 write_lock_irqsave(&devtree_lock
, flags
);
870 next
= &np
->properties
;
872 if (*next
== oldprop
) {
874 newprop
->next
= oldprop
->next
;
876 oldprop
->next
= np
->deadprops
;
877 np
->deadprops
= oldprop
;
881 next
= &(*next
)->next
;
883 write_unlock_irqrestore(&devtree_lock
, flags
);
888 #ifdef CONFIG_PROC_DEVICETREE
889 /* try to add to proc as well if it was initialized */
891 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
892 #endif /* CONFIG_PROC_DEVICETREE */
897 #if defined(CONFIG_OF_DYNAMIC)
899 * Support for dynamic device trees.
901 * On some platforms, the device tree can be manipulated at runtime.
902 * The routines in this section support adding, removing and changing
907 * of_attach_node - Plug a device node into the tree and global list.
909 void of_attach_node(struct device_node
*np
)
913 write_lock_irqsave(&devtree_lock
, flags
);
914 np
->sibling
= np
->parent
->child
;
915 np
->allnext
= allnodes
;
916 np
->parent
->child
= np
;
918 write_unlock_irqrestore(&devtree_lock
, flags
);
922 * of_detach_node - "Unplug" a node from the device tree.
924 * The caller must hold a reference to the node. The memory associated with
925 * the node is not freed until its refcount goes to zero.
927 void of_detach_node(struct device_node
*np
)
929 struct device_node
*parent
;
932 write_lock_irqsave(&devtree_lock
, flags
);
939 allnodes
= np
->allnext
;
941 struct device_node
*prev
;
942 for (prev
= allnodes
;
944 prev
= prev
->allnext
)
946 prev
->allnext
= np
->allnext
;
949 if (parent
->child
== np
)
950 parent
->child
= np
->sibling
;
952 struct device_node
*prevsib
;
953 for (prevsib
= np
->parent
->child
;
954 prevsib
->sibling
!= np
;
955 prevsib
= prevsib
->sibling
)
957 prevsib
->sibling
= np
->sibling
;
960 of_node_set_flag(np
, OF_DETACHED
);
963 write_unlock_irqrestore(&devtree_lock
, flags
);
965 #endif /* defined(CONFIG_OF_DYNAMIC) */