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/ctype.h>
21 #include <linux/cpu.h>
22 #include <linux/module.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/proc_fs.h>
29 #include "of_private.h"
31 LIST_HEAD(aliases_lookup
);
33 struct device_node
*of_allnodes
;
34 EXPORT_SYMBOL(of_allnodes
);
35 struct device_node
*of_chosen
;
36 struct device_node
*of_aliases
;
37 static struct device_node
*of_stdout
;
39 static struct kset
*of_kset
;
42 * Used to protect the of_aliases; but also overloaded to hold off addition of
45 DEFINE_MUTEX(of_aliases_mutex
);
47 /* use when traversing tree through the allnext, child, sibling,
48 * or parent members of struct device_node.
50 DEFINE_RAW_SPINLOCK(devtree_lock
);
52 int of_n_addr_cells(struct device_node
*np
)
59 ip
= of_get_property(np
, "#address-cells", NULL
);
61 return be32_to_cpup(ip
);
63 /* No #address-cells property for the root node */
64 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
66 EXPORT_SYMBOL(of_n_addr_cells
);
68 int of_n_size_cells(struct device_node
*np
)
75 ip
= of_get_property(np
, "#size-cells", NULL
);
77 return be32_to_cpup(ip
);
79 /* No #size-cells property for the root node */
80 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
82 EXPORT_SYMBOL(of_n_size_cells
);
85 int __weak
of_node_to_nid(struct device_node
*np
)
87 return numa_node_id();
91 #if defined(CONFIG_OF_DYNAMIC)
93 * of_node_get - Increment refcount of a node
94 * @node: Node to inc refcount, NULL is supported to
95 * simplify writing of callers
99 struct device_node
*of_node_get(struct device_node
*node
)
102 kobject_get(&node
->kobj
);
105 EXPORT_SYMBOL(of_node_get
);
107 static inline struct device_node
*kobj_to_device_node(struct kobject
*kobj
)
109 return container_of(kobj
, struct device_node
, kobj
);
113 * of_node_release - release a dynamically allocated node
114 * @kref: kref element of the node to be released
116 * In of_node_put() this function is passed to kref_put()
119 static void of_node_release(struct kobject
*kobj
)
121 struct device_node
*node
= kobj_to_device_node(kobj
);
122 struct property
*prop
= node
->properties
;
124 /* We should never be releasing nodes that haven't been detached. */
125 if (!of_node_check_flag(node
, OF_DETACHED
)) {
126 pr_err("ERROR: Bad of_node_put() on %s\n", node
->full_name
);
131 if (!of_node_check_flag(node
, OF_DYNAMIC
))
135 struct property
*next
= prop
->next
;
142 prop
= node
->deadprops
;
143 node
->deadprops
= NULL
;
146 kfree(node
->full_name
);
152 * of_node_put - Decrement refcount of a node
153 * @node: Node to dec refcount, NULL is supported to
154 * simplify writing of callers
157 void of_node_put(struct device_node
*node
)
160 kobject_put(&node
->kobj
);
162 EXPORT_SYMBOL(of_node_put
);
164 static void of_node_release(struct kobject
*kobj
)
166 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
168 #endif /* CONFIG_OF_DYNAMIC */
170 struct kobj_type of_node_ktype
= {
171 .release
= of_node_release
,
174 static ssize_t
of_node_property_read(struct file
*filp
, struct kobject
*kobj
,
175 struct bin_attribute
*bin_attr
, char *buf
,
176 loff_t offset
, size_t count
)
178 struct property
*pp
= container_of(bin_attr
, struct property
, attr
);
179 return memory_read_from_buffer(buf
, count
, &offset
, pp
->value
, pp
->length
);
182 static const char *safe_name(struct kobject
*kobj
, const char *orig_name
)
184 const char *name
= orig_name
;
185 struct kernfs_node
*kn
;
188 /* don't be a hero. After 16 tries give up */
189 while (i
< 16 && (kn
= sysfs_get_dirent(kobj
->sd
, name
))) {
191 if (name
!= orig_name
)
193 name
= kasprintf(GFP_KERNEL
, "%s#%i", orig_name
, ++i
);
196 if (name
!= orig_name
)
197 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
198 kobject_name(kobj
), name
);
202 static int __of_add_property_sysfs(struct device_node
*np
, struct property
*pp
)
206 /* Important: Don't leak passwords */
207 bool secure
= strncmp(pp
->name
, "security-", 9) == 0;
209 sysfs_bin_attr_init(&pp
->attr
);
210 pp
->attr
.attr
.name
= safe_name(&np
->kobj
, pp
->name
);
211 pp
->attr
.attr
.mode
= secure
? S_IRUSR
: S_IRUGO
;
212 pp
->attr
.size
= secure
? 0 : pp
->length
;
213 pp
->attr
.read
= of_node_property_read
;
215 rc
= sysfs_create_bin_file(&np
->kobj
, &pp
->attr
);
216 WARN(rc
, "error adding attribute %s to node %s\n", pp
->name
, np
->full_name
);
220 static int __of_node_add(struct device_node
*np
)
226 np
->kobj
.kset
= of_kset
;
228 /* Nodes without parents are new top level trees */
229 rc
= kobject_add(&np
->kobj
, NULL
, safe_name(&of_kset
->kobj
, "base"));
231 name
= safe_name(&np
->parent
->kobj
, kbasename(np
->full_name
));
232 if (!name
|| !name
[0])
235 rc
= kobject_add(&np
->kobj
, &np
->parent
->kobj
, "%s", name
);
240 for_each_property_of_node(np
, pp
)
241 __of_add_property_sysfs(np
, pp
);
246 int of_node_add(struct device_node
*np
)
250 BUG_ON(!of_node_is_initialized(np
));
253 * Grab the mutex here so that in a race condition between of_init() and
254 * of_node_add(), node addition will still be consistent.
256 mutex_lock(&of_aliases_mutex
);
258 rc
= __of_node_add(np
);
260 /* This scenario may be perfectly valid, but report it anyway */
261 pr_info("of_node_add(%s) before of_init()\n", np
->full_name
);
262 mutex_unlock(&of_aliases_mutex
);
266 #if defined(CONFIG_OF_DYNAMIC)
267 static void of_node_remove(struct device_node
*np
)
271 BUG_ON(!of_node_is_initialized(np
));
273 /* only remove properties if on sysfs */
274 if (of_node_is_attached(np
)) {
275 for_each_property_of_node(np
, pp
)
276 sysfs_remove_bin_file(&np
->kobj
, &pp
->attr
);
277 kobject_del(&np
->kobj
);
280 /* finally remove the kobj_init ref */
285 static int __init
of_init(void)
287 struct device_node
*np
;
289 /* Create the kset, and register existing nodes */
290 mutex_lock(&of_aliases_mutex
);
291 of_kset
= kset_create_and_add("devicetree", NULL
, firmware_kobj
);
293 mutex_unlock(&of_aliases_mutex
);
296 for_each_of_allnodes(np
)
298 mutex_unlock(&of_aliases_mutex
);
300 /* Symlink in /proc as required by userspace ABI */
302 proc_symlink("device-tree", NULL
, "/sys/firmware/devicetree/base");
306 core_initcall(of_init
);
308 static struct property
*__of_find_property(const struct device_node
*np
,
309 const char *name
, int *lenp
)
316 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
317 if (of_prop_cmp(pp
->name
, name
) == 0) {
327 struct property
*of_find_property(const struct device_node
*np
,
334 raw_spin_lock_irqsave(&devtree_lock
, flags
);
335 pp
= __of_find_property(np
, name
, lenp
);
336 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
340 EXPORT_SYMBOL(of_find_property
);
343 * of_find_all_nodes - Get next node in global list
344 * @prev: Previous node or NULL to start iteration
345 * of_node_put() will be called on it
347 * Returns a node pointer with refcount incremented, use
348 * of_node_put() on it when done.
350 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
352 struct device_node
*np
;
355 raw_spin_lock_irqsave(&devtree_lock
, flags
);
356 np
= prev
? prev
->allnext
: of_allnodes
;
357 for (; np
!= NULL
; np
= np
->allnext
)
361 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
364 EXPORT_SYMBOL(of_find_all_nodes
);
367 * Find a property with a given name for a given node
368 * and return the value.
370 static const void *__of_get_property(const struct device_node
*np
,
371 const char *name
, int *lenp
)
373 struct property
*pp
= __of_find_property(np
, name
, lenp
);
375 return pp
? pp
->value
: NULL
;
379 * Find a property with a given name for a given node
380 * and return the value.
382 const void *of_get_property(const struct device_node
*np
, const char *name
,
385 struct property
*pp
= of_find_property(np
, name
, lenp
);
387 return pp
? pp
->value
: NULL
;
389 EXPORT_SYMBOL(of_get_property
);
392 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
394 * @cpu: logical cpu index of a core/thread
395 * @phys_id: physical identifier of a core/thread
397 * CPU logical to physical index mapping is architecture specific.
398 * However this __weak function provides a default match of physical
399 * id to logical cpu index. phys_id provided here is usually values read
400 * from the device tree which must match the hardware internal registers.
402 * Returns true if the physical identifier and the logical cpu index
403 * correspond to the same core/thread, false otherwise.
405 bool __weak
arch_match_cpu_phys_id(int cpu
, u64 phys_id
)
407 return (u32
)phys_id
== cpu
;
411 * Checks if the given "prop_name" property holds the physical id of the
412 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
413 * NULL, local thread number within the core is returned in it.
415 static bool __of_find_n_match_cpu_property(struct device_node
*cpun
,
416 const char *prop_name
, int cpu
, unsigned int *thread
)
419 int ac
, prop_len
, tid
;
422 ac
= of_n_addr_cells(cpun
);
423 cell
= of_get_property(cpun
, prop_name
, &prop_len
);
426 prop_len
/= sizeof(*cell
) * ac
;
427 for (tid
= 0; tid
< prop_len
; tid
++) {
428 hwid
= of_read_number(cell
, ac
);
429 if (arch_match_cpu_phys_id(cpu
, hwid
)) {
440 * arch_find_n_match_cpu_physical_id - See if the given device node is
441 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
442 * else false. If 'thread' is non-NULL, the local thread number within the
443 * core is returned in it.
445 bool __weak
arch_find_n_match_cpu_physical_id(struct device_node
*cpun
,
446 int cpu
, unsigned int *thread
)
448 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
449 * for thread ids on PowerPC. If it doesn't exist fallback to
450 * standard "reg" property.
452 if (IS_ENABLED(CONFIG_PPC
) &&
453 __of_find_n_match_cpu_property(cpun
,
454 "ibm,ppc-interrupt-server#s",
458 if (__of_find_n_match_cpu_property(cpun
, "reg", cpu
, thread
))
465 * of_get_cpu_node - Get device node associated with the given logical CPU
467 * @cpu: CPU number(logical index) for which device node is required
468 * @thread: if not NULL, local thread number within the physical core is
471 * The main purpose of this function is to retrieve the device node for the
472 * given logical CPU index. It should be used to initialize the of_node in
473 * cpu device. Once of_node in cpu device is populated, all the further
474 * references can use that instead.
476 * CPU logical to physical index mapping is architecture specific and is built
477 * before booting secondary cores. This function uses arch_match_cpu_phys_id
478 * which can be overridden by architecture specific implementation.
480 * Returns a node pointer for the logical cpu if found, else NULL.
482 struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
)
484 struct device_node
*cpun
;
486 for_each_node_by_type(cpun
, "cpu") {
487 if (arch_find_n_match_cpu_physical_id(cpun
, cpu
, thread
))
492 EXPORT_SYMBOL(of_get_cpu_node
);
495 * __of_device_is_compatible() - Check if the node matches given constraints
496 * @device: pointer to node
497 * @compat: required compatible string, NULL or "" for any match
498 * @type: required device_type value, NULL or "" for any match
499 * @name: required node name, NULL or "" for any match
501 * Checks if the given @compat, @type and @name strings match the
502 * properties of the given @device. A constraints can be skipped by
503 * passing NULL or an empty string as the constraint.
505 * Returns 0 for no match, and a positive integer on match. The return
506 * value is a relative score with larger values indicating better
507 * matches. The score is weighted for the most specific compatible value
508 * to get the highest score. Matching type is next, followed by matching
509 * name. Practically speaking, this results in the following priority
512 * 1. specific compatible && type && name
513 * 2. specific compatible && type
514 * 3. specific compatible && name
515 * 4. specific compatible
516 * 5. general compatible && type && name
517 * 6. general compatible && type
518 * 7. general compatible && name
519 * 8. general compatible
524 static int __of_device_is_compatible(const struct device_node
*device
,
525 const char *compat
, const char *type
, const char *name
)
527 struct property
*prop
;
529 int index
= 0, score
= 0;
531 /* Compatible match has highest priority */
532 if (compat
&& compat
[0]) {
533 prop
= __of_find_property(device
, "compatible", NULL
);
534 for (cp
= of_prop_next_string(prop
, NULL
); cp
;
535 cp
= of_prop_next_string(prop
, cp
), index
++) {
536 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0) {
537 score
= INT_MAX
/2 - (index
<< 2);
545 /* Matching type is better than matching name */
546 if (type
&& type
[0]) {
547 if (!device
->type
|| of_node_cmp(type
, device
->type
))
552 /* Matching name is a bit better than not */
553 if (name
&& name
[0]) {
554 if (!device
->name
|| of_node_cmp(name
, device
->name
))
562 /** Checks if the given "compat" string matches one of the strings in
563 * the device's "compatible" property
565 int of_device_is_compatible(const struct device_node
*device
,
571 raw_spin_lock_irqsave(&devtree_lock
, flags
);
572 res
= __of_device_is_compatible(device
, compat
, NULL
, NULL
);
573 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
576 EXPORT_SYMBOL(of_device_is_compatible
);
579 * of_machine_is_compatible - Test root of device tree for a given compatible value
580 * @compat: compatible string to look for in root node's compatible property.
582 * Returns true if the root node has the given value in its
583 * compatible property.
585 int of_machine_is_compatible(const char *compat
)
587 struct device_node
*root
;
590 root
= of_find_node_by_path("/");
592 rc
= of_device_is_compatible(root
, compat
);
597 EXPORT_SYMBOL(of_machine_is_compatible
);
600 * __of_device_is_available - check if a device is available for use
602 * @device: Node to check for availability, with locks already held
604 * Returns 1 if the status property is absent or set to "okay" or "ok",
607 static int __of_device_is_available(const struct device_node
*device
)
615 status
= __of_get_property(device
, "status", &statlen
);
620 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
628 * of_device_is_available - check if a device is available for use
630 * @device: Node to check for availability
632 * Returns 1 if the status property is absent or set to "okay" or "ok",
635 int of_device_is_available(const struct device_node
*device
)
640 raw_spin_lock_irqsave(&devtree_lock
, flags
);
641 res
= __of_device_is_available(device
);
642 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
646 EXPORT_SYMBOL(of_device_is_available
);
649 * of_get_parent - Get a node's parent if any
650 * @node: Node to get parent
652 * Returns a node pointer with refcount incremented, use
653 * of_node_put() on it when done.
655 struct device_node
*of_get_parent(const struct device_node
*node
)
657 struct device_node
*np
;
663 raw_spin_lock_irqsave(&devtree_lock
, flags
);
664 np
= of_node_get(node
->parent
);
665 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
668 EXPORT_SYMBOL(of_get_parent
);
671 * of_get_next_parent - Iterate to a node's parent
672 * @node: Node to get parent of
674 * This is like of_get_parent() except that it drops the
675 * refcount on the passed node, making it suitable for iterating
676 * through a node's parents.
678 * Returns a node pointer with refcount incremented, use
679 * of_node_put() on it when done.
681 struct device_node
*of_get_next_parent(struct device_node
*node
)
683 struct device_node
*parent
;
689 raw_spin_lock_irqsave(&devtree_lock
, flags
);
690 parent
= of_node_get(node
->parent
);
692 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
695 EXPORT_SYMBOL(of_get_next_parent
);
698 * of_get_next_child - Iterate a node childs
700 * @prev: previous child of the parent node, or NULL to get first
702 * Returns a node pointer with refcount incremented, use
703 * of_node_put() on it when done.
705 struct device_node
*of_get_next_child(const struct device_node
*node
,
706 struct device_node
*prev
)
708 struct device_node
*next
;
711 raw_spin_lock_irqsave(&devtree_lock
, flags
);
712 next
= prev
? prev
->sibling
: node
->child
;
713 for (; next
; next
= next
->sibling
)
714 if (of_node_get(next
))
717 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
720 EXPORT_SYMBOL(of_get_next_child
);
723 * of_get_next_available_child - Find the next available child node
725 * @prev: previous child of the parent node, or NULL to get first
727 * This function is like of_get_next_child(), except that it
728 * automatically skips any disabled nodes (i.e. status = "disabled").
730 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
731 struct device_node
*prev
)
733 struct device_node
*next
;
736 raw_spin_lock_irqsave(&devtree_lock
, flags
);
737 next
= prev
? prev
->sibling
: node
->child
;
738 for (; next
; next
= next
->sibling
) {
739 if (!__of_device_is_available(next
))
741 if (of_node_get(next
))
745 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
748 EXPORT_SYMBOL(of_get_next_available_child
);
751 * of_get_child_by_name - Find the child node by name for a given parent
753 * @name: child name to look for.
755 * This function looks for child node for given matching name
757 * Returns a node pointer if found, with refcount incremented, use
758 * of_node_put() on it when done.
759 * Returns NULL if node is not found.
761 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
764 struct device_node
*child
;
766 for_each_child_of_node(node
, child
)
767 if (child
->name
&& (of_node_cmp(child
->name
, name
) == 0))
771 EXPORT_SYMBOL(of_get_child_by_name
);
774 * of_find_node_by_path - Find a node matching a full OF path
775 * @path: The full path to match
777 * Returns a node pointer with refcount incremented, use
778 * of_node_put() on it when done.
780 struct device_node
*of_find_node_by_path(const char *path
)
782 struct device_node
*np
= of_allnodes
;
785 raw_spin_lock_irqsave(&devtree_lock
, flags
);
786 for (; np
; np
= np
->allnext
) {
787 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
791 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
794 EXPORT_SYMBOL(of_find_node_by_path
);
797 * of_find_node_by_name - Find a node by its "name" property
798 * @from: The node to start searching from or NULL, the node
799 * you pass will not be searched, only the next one
800 * will; typically, you pass what the previous call
801 * returned. of_node_put() will be called on it
802 * @name: The name string to match against
804 * Returns a node pointer with refcount incremented, use
805 * of_node_put() on it when done.
807 struct device_node
*of_find_node_by_name(struct device_node
*from
,
810 struct device_node
*np
;
813 raw_spin_lock_irqsave(&devtree_lock
, flags
);
814 np
= from
? from
->allnext
: of_allnodes
;
815 for (; np
; np
= np
->allnext
)
816 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
820 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
823 EXPORT_SYMBOL(of_find_node_by_name
);
826 * of_find_node_by_type - Find a node by its "device_type" property
827 * @from: The node to start searching from, or NULL to start searching
828 * the entire device tree. The node you pass will not be
829 * searched, only the next one will; typically, you pass
830 * what the previous call returned. of_node_put() will be
831 * called on from for you.
832 * @type: The type string to match against
834 * Returns a node pointer with refcount incremented, use
835 * of_node_put() on it when done.
837 struct device_node
*of_find_node_by_type(struct device_node
*from
,
840 struct device_node
*np
;
843 raw_spin_lock_irqsave(&devtree_lock
, flags
);
844 np
= from
? from
->allnext
: of_allnodes
;
845 for (; np
; np
= np
->allnext
)
846 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
850 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
853 EXPORT_SYMBOL(of_find_node_by_type
);
856 * of_find_compatible_node - Find a node based on type and one of the
857 * tokens in its "compatible" property
858 * @from: The node to start searching from or NULL, the node
859 * you pass will not be searched, only the next one
860 * will; typically, you pass what the previous call
861 * returned. of_node_put() will be called on it
862 * @type: The type string to match "device_type" or NULL to ignore
863 * @compatible: The string to match to one of the tokens in the device
866 * Returns a node pointer with refcount incremented, use
867 * of_node_put() on it when done.
869 struct device_node
*of_find_compatible_node(struct device_node
*from
,
870 const char *type
, const char *compatible
)
872 struct device_node
*np
;
875 raw_spin_lock_irqsave(&devtree_lock
, flags
);
876 np
= from
? from
->allnext
: of_allnodes
;
877 for (; np
; np
= np
->allnext
) {
878 if (__of_device_is_compatible(np
, compatible
, type
, NULL
) &&
883 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
886 EXPORT_SYMBOL(of_find_compatible_node
);
889 * of_find_node_with_property - Find a node which has a property with
891 * @from: The node to start searching from or NULL, the node
892 * you pass will not be searched, only the next one
893 * will; typically, you pass what the previous call
894 * returned. of_node_put() will be called on it
895 * @prop_name: The name of the property to look for.
897 * Returns a node pointer with refcount incremented, use
898 * of_node_put() on it when done.
900 struct device_node
*of_find_node_with_property(struct device_node
*from
,
901 const char *prop_name
)
903 struct device_node
*np
;
907 raw_spin_lock_irqsave(&devtree_lock
, flags
);
908 np
= from
? from
->allnext
: of_allnodes
;
909 for (; np
; np
= np
->allnext
) {
910 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
911 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
919 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
922 EXPORT_SYMBOL(of_find_node_with_property
);
925 const struct of_device_id
*__of_match_node(const struct of_device_id
*matches
,
926 const struct device_node
*node
)
928 const struct of_device_id
*best_match
= NULL
;
929 int score
, best_score
= 0;
934 for (; matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]; matches
++) {
935 score
= __of_device_is_compatible(node
, matches
->compatible
,
936 matches
->type
, matches
->name
);
937 if (score
> best_score
) {
938 best_match
= matches
;
947 * of_match_node - Tell if an device_node has a matching of_match structure
948 * @matches: array of of device match structures to search in
949 * @node: the of device structure to match against
951 * Low level utility function used by device matching.
953 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
954 const struct device_node
*node
)
956 const struct of_device_id
*match
;
959 raw_spin_lock_irqsave(&devtree_lock
, flags
);
960 match
= __of_match_node(matches
, node
);
961 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
964 EXPORT_SYMBOL(of_match_node
);
967 * of_find_matching_node_and_match - Find a node based on an of_device_id
969 * @from: The node to start searching from or NULL, the node
970 * you pass will not be searched, only the next one
971 * will; typically, you pass what the previous call
972 * returned. of_node_put() will be called on it
973 * @matches: array of of device match structures to search in
974 * @match Updated to point at the matches entry which matched
976 * Returns a node pointer with refcount incremented, use
977 * of_node_put() on it when done.
979 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
980 const struct of_device_id
*matches
,
981 const struct of_device_id
**match
)
983 struct device_node
*np
;
984 const struct of_device_id
*m
;
990 raw_spin_lock_irqsave(&devtree_lock
, flags
);
991 np
= from
? from
->allnext
: of_allnodes
;
992 for (; np
; np
= np
->allnext
) {
993 m
= __of_match_node(matches
, np
);
994 if (m
&& of_node_get(np
)) {
1001 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1004 EXPORT_SYMBOL(of_find_matching_node_and_match
);
1007 * of_modalias_node - Lookup appropriate modalias for a device node
1008 * @node: pointer to a device tree node
1009 * @modalias: Pointer to buffer that modalias value will be copied into
1010 * @len: Length of modalias value
1012 * Based on the value of the compatible property, this routine will attempt
1013 * to choose an appropriate modalias value for a particular device tree node.
1014 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1015 * from the first entry in the compatible list property.
1017 * This routine returns 0 on success, <0 on failure.
1019 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
1021 const char *compatible
, *p
;
1024 compatible
= of_get_property(node
, "compatible", &cplen
);
1025 if (!compatible
|| strlen(compatible
) > cplen
)
1027 p
= strchr(compatible
, ',');
1028 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
1031 EXPORT_SYMBOL_GPL(of_modalias_node
);
1034 * of_find_node_by_phandle - Find a node given a phandle
1035 * @handle: phandle of the node to find
1037 * Returns a node pointer with refcount incremented, use
1038 * of_node_put() on it when done.
1040 struct device_node
*of_find_node_by_phandle(phandle handle
)
1042 struct device_node
*np
;
1043 unsigned long flags
;
1045 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1046 for (np
= of_allnodes
; np
; np
= np
->allnext
)
1047 if (np
->phandle
== handle
)
1050 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1053 EXPORT_SYMBOL(of_find_node_by_phandle
);
1056 * of_find_property_value_of_size
1058 * @np: device node from which the property value is to be read.
1059 * @propname: name of the property to be searched.
1060 * @len: requested length of property value
1062 * Search for a property in a device node and valid the requested size.
1063 * Returns the property value on success, -EINVAL if the property does not
1064 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1065 * property data isn't large enough.
1068 static void *of_find_property_value_of_size(const struct device_node
*np
,
1069 const char *propname
, u32 len
)
1071 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1074 return ERR_PTR(-EINVAL
);
1076 return ERR_PTR(-ENODATA
);
1077 if (len
> prop
->length
)
1078 return ERR_PTR(-EOVERFLOW
);
1084 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1086 * @np: device node from which the property value is to be read.
1087 * @propname: name of the property to be searched.
1088 * @index: index of the u32 in the list of values
1089 * @out_value: pointer to return value, modified only if no error.
1091 * Search for a property in a device node and read nth 32-bit value from
1092 * it. Returns 0 on success, -EINVAL if the property does not exist,
1093 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1094 * property data isn't large enough.
1096 * The out_value is modified only if a valid u32 value can be decoded.
1098 int of_property_read_u32_index(const struct device_node
*np
,
1099 const char *propname
,
1100 u32 index
, u32
*out_value
)
1102 const u32
*val
= of_find_property_value_of_size(np
, propname
,
1103 ((index
+ 1) * sizeof(*out_value
)));
1106 return PTR_ERR(val
);
1108 *out_value
= be32_to_cpup(((__be32
*)val
) + index
);
1111 EXPORT_SYMBOL_GPL(of_property_read_u32_index
);
1114 * of_property_read_u8_array - Find and read an array of u8 from a property.
1116 * @np: device node from which the property value is to be read.
1117 * @propname: name of the property to be searched.
1118 * @out_values: pointer to return value, modified only if return value is 0.
1119 * @sz: number of array elements to read
1121 * Search for a property in a device node and read 8-bit value(s) from
1122 * it. Returns 0 on success, -EINVAL if the property does not exist,
1123 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1124 * property data isn't large enough.
1126 * dts entry of array should be like:
1127 * property = /bits/ 8 <0x50 0x60 0x70>;
1129 * The out_values is modified only if a valid u8 value can be decoded.
1131 int of_property_read_u8_array(const struct device_node
*np
,
1132 const char *propname
, u8
*out_values
, size_t sz
)
1134 const u8
*val
= of_find_property_value_of_size(np
, propname
,
1135 (sz
* sizeof(*out_values
)));
1138 return PTR_ERR(val
);
1141 *out_values
++ = *val
++;
1144 EXPORT_SYMBOL_GPL(of_property_read_u8_array
);
1147 * of_property_read_u16_array - Find and read an array of u16 from a property.
1149 * @np: device node from which the property value is to be read.
1150 * @propname: name of the property to be searched.
1151 * @out_values: pointer to return value, modified only if return value is 0.
1152 * @sz: number of array elements to read
1154 * Search for a property in a device node and read 16-bit value(s) from
1155 * it. Returns 0 on success, -EINVAL if the property does not exist,
1156 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1157 * property data isn't large enough.
1159 * dts entry of array should be like:
1160 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1162 * The out_values is modified only if a valid u16 value can be decoded.
1164 int of_property_read_u16_array(const struct device_node
*np
,
1165 const char *propname
, u16
*out_values
, size_t sz
)
1167 const __be16
*val
= of_find_property_value_of_size(np
, propname
,
1168 (sz
* sizeof(*out_values
)));
1171 return PTR_ERR(val
);
1174 *out_values
++ = be16_to_cpup(val
++);
1177 EXPORT_SYMBOL_GPL(of_property_read_u16_array
);
1180 * of_property_read_u32_array - Find and read an array of 32 bit integers
1183 * @np: device node from which the property value is to be read.
1184 * @propname: name of the property to be searched.
1185 * @out_values: pointer to return value, modified only if return value is 0.
1186 * @sz: number of array elements to read
1188 * Search for a property in a device node and read 32-bit value(s) from
1189 * it. Returns 0 on success, -EINVAL if the property does not exist,
1190 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1191 * property data isn't large enough.
1193 * The out_values is modified only if a valid u32 value can be decoded.
1195 int of_property_read_u32_array(const struct device_node
*np
,
1196 const char *propname
, u32
*out_values
,
1199 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1200 (sz
* sizeof(*out_values
)));
1203 return PTR_ERR(val
);
1206 *out_values
++ = be32_to_cpup(val
++);
1209 EXPORT_SYMBOL_GPL(of_property_read_u32_array
);
1212 * of_property_read_u64 - Find and read a 64 bit integer from a property
1213 * @np: device node from which the property value is to be read.
1214 * @propname: name of the property to be searched.
1215 * @out_value: pointer to return value, modified only if return value is 0.
1217 * Search for a property in a device node and read a 64-bit value from
1218 * it. Returns 0 on success, -EINVAL if the property does not exist,
1219 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1220 * property data isn't large enough.
1222 * The out_value is modified only if a valid u64 value can be decoded.
1224 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
1227 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1228 sizeof(*out_value
));
1231 return PTR_ERR(val
);
1233 *out_value
= of_read_number(val
, 2);
1236 EXPORT_SYMBOL_GPL(of_property_read_u64
);
1239 * of_property_read_string - Find and read a string from a property
1240 * @np: device node from which the property value is to be read.
1241 * @propname: name of the property to be searched.
1242 * @out_string: pointer to null terminated return string, modified only if
1243 * return value is 0.
1245 * Search for a property in a device tree node and retrieve a null
1246 * terminated string value (pointer to data, not a copy). Returns 0 on
1247 * success, -EINVAL if the property does not exist, -ENODATA if property
1248 * does not have a value, and -EILSEQ if the string is not null-terminated
1249 * within the length of the property data.
1251 * The out_string pointer is modified only if a valid string can be decoded.
1253 int of_property_read_string(struct device_node
*np
, const char *propname
,
1254 const char **out_string
)
1256 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1261 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
1263 *out_string
= prop
->value
;
1266 EXPORT_SYMBOL_GPL(of_property_read_string
);
1269 * of_property_read_string_index - Find and read a string from a multiple
1271 * @np: device node from which the property value is to be read.
1272 * @propname: name of the property to be searched.
1273 * @index: index of the string in the list of strings
1274 * @out_string: pointer to null terminated return string, modified only if
1275 * return value is 0.
1277 * Search for a property in a device tree node and retrieve a null
1278 * terminated string value (pointer to data, not a copy) in the list of strings
1279 * contained in that property.
1280 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1281 * property does not have a value, and -EILSEQ if the string is not
1282 * null-terminated within the length of the property data.
1284 * The out_string pointer is modified only if a valid string can be decoded.
1286 int of_property_read_string_index(struct device_node
*np
, const char *propname
,
1287 int index
, const char **output
)
1289 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1291 size_t l
= 0, total
= 0;
1298 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
1303 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
) {
1312 EXPORT_SYMBOL_GPL(of_property_read_string_index
);
1315 * of_property_match_string() - Find string in a list and return index
1316 * @np: pointer to node containing string list property
1317 * @propname: string list property name
1318 * @string: pointer to string to search for in string list
1320 * This function searches a string list property and returns the index
1321 * of a specific string value.
1323 int of_property_match_string(struct device_node
*np
, const char *propname
,
1326 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1329 const char *p
, *end
;
1337 end
= p
+ prop
->length
;
1339 for (i
= 0; p
< end
; i
++, p
+= l
) {
1343 pr_debug("comparing %s with %s\n", string
, p
);
1344 if (strcmp(string
, p
) == 0)
1345 return i
; /* Found it; return index */
1349 EXPORT_SYMBOL_GPL(of_property_match_string
);
1352 * of_property_count_strings - Find and return the number of strings from a
1353 * multiple strings property.
1354 * @np: device node from which the property value is to be read.
1355 * @propname: name of the property to be searched.
1357 * Search for a property in a device tree node and retrieve the number of null
1358 * terminated string contain in it. Returns the number of strings on
1359 * success, -EINVAL if the property does not exist, -ENODATA if property
1360 * does not have a value, and -EILSEQ if the string is not null-terminated
1361 * within the length of the property data.
1363 int of_property_count_strings(struct device_node
*np
, const char *propname
)
1365 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1367 size_t l
= 0, total
= 0;
1374 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
1379 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
, i
++)
1384 EXPORT_SYMBOL_GPL(of_property_count_strings
);
1386 void of_print_phandle_args(const char *msg
, const struct of_phandle_args
*args
)
1389 printk("%s %s", msg
, of_node_full_name(args
->np
));
1390 for (i
= 0; i
< args
->args_count
; i
++)
1391 printk(i
? ",%08x" : ":%08x", args
->args
[i
]);
1395 static int __of_parse_phandle_with_args(const struct device_node
*np
,
1396 const char *list_name
,
1397 const char *cells_name
,
1398 int cell_count
, int index
,
1399 struct of_phandle_args
*out_args
)
1401 const __be32
*list
, *list_end
;
1402 int rc
= 0, size
, cur_index
= 0;
1404 struct device_node
*node
= NULL
;
1407 /* Retrieve the phandle list property */
1408 list
= of_get_property(np
, list_name
, &size
);
1411 list_end
= list
+ size
/ sizeof(*list
);
1413 /* Loop over the phandles until all the requested entry is found */
1414 while (list
< list_end
) {
1419 * If phandle is 0, then it is an empty entry with no
1420 * arguments. Skip forward to the next entry.
1422 phandle
= be32_to_cpup(list
++);
1425 * Find the provider node and parse the #*-cells
1426 * property to determine the argument length.
1428 * This is not needed if the cell count is hard-coded
1429 * (i.e. cells_name not set, but cell_count is set),
1430 * except when we're going to return the found node
1433 if (cells_name
|| cur_index
== index
) {
1434 node
= of_find_node_by_phandle(phandle
);
1436 pr_err("%s: could not find phandle\n",
1443 if (of_property_read_u32(node
, cells_name
,
1445 pr_err("%s: could not get %s for %s\n",
1446 np
->full_name
, cells_name
,
1455 * Make sure that the arguments actually fit in the
1456 * remaining property data length
1458 if (list
+ count
> list_end
) {
1459 pr_err("%s: arguments longer than property\n",
1466 * All of the error cases above bail out of the loop, so at
1467 * this point, the parsing is successful. If the requested
1468 * index matches, then fill the out_args structure and return,
1469 * or return -ENOENT for an empty entry.
1472 if (cur_index
== index
) {
1478 if (WARN_ON(count
> MAX_PHANDLE_ARGS
))
1479 count
= MAX_PHANDLE_ARGS
;
1480 out_args
->np
= node
;
1481 out_args
->args_count
= count
;
1482 for (i
= 0; i
< count
; i
++)
1483 out_args
->args
[i
] = be32_to_cpup(list
++);
1488 /* Found it! return success */
1499 * Unlock node before returning result; will be one of:
1500 * -ENOENT : index is for empty phandle
1501 * -EINVAL : parsing error on data
1502 * [1..n] : Number of phandle (count mode; when index = -1)
1504 rc
= index
< 0 ? cur_index
: -ENOENT
;
1512 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1513 * @np: Pointer to device node holding phandle property
1514 * @phandle_name: Name of property holding a phandle value
1515 * @index: For properties holding a table of phandles, this is the index into
1518 * Returns the device_node pointer with refcount incremented. Use
1519 * of_node_put() on it when done.
1521 struct device_node
*of_parse_phandle(const struct device_node
*np
,
1522 const char *phandle_name
, int index
)
1524 struct of_phandle_args args
;
1529 if (__of_parse_phandle_with_args(np
, phandle_name
, NULL
, 0,
1535 EXPORT_SYMBOL(of_parse_phandle
);
1538 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1539 * @np: pointer to a device tree node containing a list
1540 * @list_name: property name that contains a list
1541 * @cells_name: property name that specifies phandles' arguments count
1542 * @index: index of a phandle to parse out
1543 * @out_args: optional pointer to output arguments structure (will be filled)
1545 * This function is useful to parse lists of phandles and their arguments.
1546 * Returns 0 on success and fills out_args, on error returns appropriate
1549 * Caller is responsible to call of_node_put() on the returned out_args->node
1555 * #list-cells = <2>;
1559 * #list-cells = <1>;
1563 * list = <&phandle1 1 2 &phandle2 3>;
1566 * To get a device_node of the `node2' node you may call this:
1567 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1569 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1570 const char *cells_name
, int index
,
1571 struct of_phandle_args
*out_args
)
1575 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, 0,
1578 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1581 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1582 * @np: pointer to a device tree node containing a list
1583 * @list_name: property name that contains a list
1584 * @cell_count: number of argument cells following the phandle
1585 * @index: index of a phandle to parse out
1586 * @out_args: optional pointer to output arguments structure (will be filled)
1588 * This function is useful to parse lists of phandles and their arguments.
1589 * Returns 0 on success and fills out_args, on error returns appropriate
1592 * Caller is responsible to call of_node_put() on the returned out_args->node
1604 * list = <&phandle1 0 2 &phandle2 2 3>;
1607 * To get a device_node of the `node2' node you may call this:
1608 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1610 int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
1611 const char *list_name
, int cell_count
,
1612 int index
, struct of_phandle_args
*out_args
)
1616 return __of_parse_phandle_with_args(np
, list_name
, NULL
, cell_count
,
1619 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args
);
1622 * of_count_phandle_with_args() - Find the number of phandles references in a property
1623 * @np: pointer to a device tree node containing a list
1624 * @list_name: property name that contains a list
1625 * @cells_name: property name that specifies phandles' arguments count
1627 * Returns the number of phandle + argument tuples within a property. It
1628 * is a typical pattern to encode a list of phandle and variable
1629 * arguments into a single property. The number of arguments is encoded
1630 * by a property in the phandle-target node. For example, a gpios
1631 * property would contain a list of GPIO specifies consisting of a
1632 * phandle and 1 or more arguments. The number of arguments are
1633 * determined by the #gpio-cells property in the node pointed to by the
1636 int of_count_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1637 const char *cells_name
)
1639 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, 0, -1,
1642 EXPORT_SYMBOL(of_count_phandle_with_args
);
1644 #if defined(CONFIG_OF_DYNAMIC)
1645 static int of_property_notify(int action
, struct device_node
*np
,
1646 struct property
*prop
)
1648 struct of_prop_reconfig pr
;
1650 /* only call notifiers if the node is attached */
1651 if (!of_node_is_attached(np
))
1656 return of_reconfig_notify(action
, &pr
);
1659 static int of_property_notify(int action
, struct device_node
*np
,
1660 struct property
*prop
)
1667 * __of_add_property - Add a property to a node without lock operations
1669 static int __of_add_property(struct device_node
*np
, struct property
*prop
)
1671 struct property
**next
;
1674 next
= &np
->properties
;
1676 if (strcmp(prop
->name
, (*next
)->name
) == 0)
1677 /* duplicate ! don't insert it */
1680 next
= &(*next
)->next
;
1688 * of_add_property - Add a property to a node
1690 int of_add_property(struct device_node
*np
, struct property
*prop
)
1692 unsigned long flags
;
1695 rc
= of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
);
1699 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1700 rc
= __of_add_property(np
, prop
);
1701 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1705 if (of_node_is_attached(np
))
1706 __of_add_property_sysfs(np
, prop
);
1712 * of_remove_property - Remove a property from a node.
1714 * Note that we don't actually remove it, since we have given out
1715 * who-knows-how-many pointers to the data using get-property.
1716 * Instead we just move the property to the "dead properties"
1717 * list, so it won't be found any more.
1719 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1721 struct property
**next
;
1722 unsigned long flags
;
1726 rc
= of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
);
1730 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1731 next
= &np
->properties
;
1733 if (*next
== prop
) {
1734 /* found the node */
1736 prop
->next
= np
->deadprops
;
1737 np
->deadprops
= prop
;
1741 next
= &(*next
)->next
;
1743 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1748 /* at early boot, bail hear and defer setup to of_init() */
1752 sysfs_remove_bin_file(&np
->kobj
, &prop
->attr
);
1758 * of_update_property - Update a property in a node, if the property does
1759 * not exist, add it.
1761 * Note that we don't actually remove it, since we have given out
1762 * who-knows-how-many pointers to the data using get-property.
1763 * Instead we just move the property to the "dead properties" list,
1764 * and add the new property to the property list
1766 int of_update_property(struct device_node
*np
, struct property
*newprop
)
1768 struct property
**next
, *oldprop
;
1769 unsigned long flags
;
1772 rc
= of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
);
1779 oldprop
= of_find_property(np
, newprop
->name
, NULL
);
1781 return of_add_property(np
, newprop
);
1783 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1784 next
= &np
->properties
;
1786 if (*next
== oldprop
) {
1787 /* found the node */
1788 newprop
->next
= oldprop
->next
;
1790 oldprop
->next
= np
->deadprops
;
1791 np
->deadprops
= oldprop
;
1795 next
= &(*next
)->next
;
1797 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1801 /* Update the sysfs attribute */
1803 sysfs_remove_bin_file(&np
->kobj
, &oldprop
->attr
);
1804 __of_add_property_sysfs(np
, newprop
);
1812 #if defined(CONFIG_OF_DYNAMIC)
1814 * Support for dynamic device trees.
1816 * On some platforms, the device tree can be manipulated at runtime.
1817 * The routines in this section support adding, removing and changing
1818 * device tree nodes.
1821 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain
);
1823 int of_reconfig_notifier_register(struct notifier_block
*nb
)
1825 return blocking_notifier_chain_register(&of_reconfig_chain
, nb
);
1827 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register
);
1829 int of_reconfig_notifier_unregister(struct notifier_block
*nb
)
1831 return blocking_notifier_chain_unregister(&of_reconfig_chain
, nb
);
1833 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister
);
1835 int of_reconfig_notify(unsigned long action
, void *p
)
1839 rc
= blocking_notifier_call_chain(&of_reconfig_chain
, action
, p
);
1840 return notifier_to_errno(rc
);
1844 * of_attach_node - Plug a device node into the tree and global list.
1846 int of_attach_node(struct device_node
*np
)
1848 unsigned long flags
;
1851 rc
= of_reconfig_notify(OF_RECONFIG_ATTACH_NODE
, np
);
1855 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1856 np
->sibling
= np
->parent
->child
;
1857 np
->allnext
= of_allnodes
;
1858 np
->parent
->child
= np
;
1860 of_node_clear_flag(np
, OF_DETACHED
);
1861 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1868 * of_detach_node - "Unplug" a node from the device tree.
1870 * The caller must hold a reference to the node. The memory associated with
1871 * the node is not freed until its refcount goes to zero.
1873 int of_detach_node(struct device_node
*np
)
1875 struct device_node
*parent
;
1876 unsigned long flags
;
1879 rc
= of_reconfig_notify(OF_RECONFIG_DETACH_NODE
, np
);
1883 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1885 if (of_node_check_flag(np
, OF_DETACHED
)) {
1886 /* someone already detached it */
1887 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1891 parent
= np
->parent
;
1893 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1897 if (of_allnodes
== np
)
1898 of_allnodes
= np
->allnext
;
1900 struct device_node
*prev
;
1901 for (prev
= of_allnodes
;
1902 prev
->allnext
!= np
;
1903 prev
= prev
->allnext
)
1905 prev
->allnext
= np
->allnext
;
1908 if (parent
->child
== np
)
1909 parent
->child
= np
->sibling
;
1911 struct device_node
*prevsib
;
1912 for (prevsib
= np
->parent
->child
;
1913 prevsib
->sibling
!= np
;
1914 prevsib
= prevsib
->sibling
)
1916 prevsib
->sibling
= np
->sibling
;
1919 of_node_set_flag(np
, OF_DETACHED
);
1920 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1925 #endif /* defined(CONFIG_OF_DYNAMIC) */
1927 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
1928 int id
, const char *stem
, int stem_len
)
1932 strncpy(ap
->stem
, stem
, stem_len
);
1933 ap
->stem
[stem_len
] = 0;
1934 list_add_tail(&ap
->link
, &aliases_lookup
);
1935 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1936 ap
->alias
, ap
->stem
, ap
->id
, of_node_full_name(np
));
1940 * of_alias_scan - Scan all properties of 'aliases' node
1942 * The function scans all the properties of 'aliases' node and populate
1943 * the the global lookup table with the properties. It returns the
1944 * number of alias_prop found, or error code in error case.
1946 * @dt_alloc: An allocator that provides a virtual address to memory
1947 * for the resulting tree
1949 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
1951 struct property
*pp
;
1953 of_chosen
= of_find_node_by_path("/chosen");
1954 if (of_chosen
== NULL
)
1955 of_chosen
= of_find_node_by_path("/chosen@0");
1960 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
1962 of_stdout
= of_find_node_by_path(name
);
1965 of_aliases
= of_find_node_by_path("/aliases");
1969 for_each_property_of_node(of_aliases
, pp
) {
1970 const char *start
= pp
->name
;
1971 const char *end
= start
+ strlen(start
);
1972 struct device_node
*np
;
1973 struct alias_prop
*ap
;
1976 /* Skip those we do not want to proceed */
1977 if (!strcmp(pp
->name
, "name") ||
1978 !strcmp(pp
->name
, "phandle") ||
1979 !strcmp(pp
->name
, "linux,phandle"))
1982 np
= of_find_node_by_path(pp
->value
);
1986 /* walk the alias backwards to extract the id and work out
1987 * the 'stem' string */
1988 while (isdigit(*(end
-1)) && end
> start
)
1992 if (kstrtoint(end
, 10, &id
) < 0)
1995 /* Allocate an alias_prop with enough space for the stem */
1996 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, 4);
1999 memset(ap
, 0, sizeof(*ap
) + len
+ 1);
2001 of_alias_add(ap
, np
, id
, start
, len
);
2006 * of_alias_get_id - Get alias id for the given device_node
2007 * @np: Pointer to the given device_node
2008 * @stem: Alias stem of the given device_node
2010 * The function travels the lookup table to get alias id for the given
2011 * device_node and alias stem. It returns the alias id if find it.
2013 int of_alias_get_id(struct device_node
*np
, const char *stem
)
2015 struct alias_prop
*app
;
2018 mutex_lock(&of_aliases_mutex
);
2019 list_for_each_entry(app
, &aliases_lookup
, link
) {
2020 if (strcmp(app
->stem
, stem
) != 0)
2023 if (np
== app
->np
) {
2028 mutex_unlock(&of_aliases_mutex
);
2032 EXPORT_SYMBOL_GPL(of_alias_get_id
);
2034 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
2037 const void *curv
= cur
;
2047 curv
+= sizeof(*cur
);
2048 if (curv
>= prop
->value
+ prop
->length
)
2052 *pu
= be32_to_cpup(curv
);
2055 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
2057 const char *of_prop_next_string(struct property
*prop
, const char *cur
)
2059 const void *curv
= cur
;
2067 curv
+= strlen(cur
) + 1;
2068 if (curv
>= prop
->value
+ prop
->length
)
2073 EXPORT_SYMBOL_GPL(of_prop_next_string
);
2076 * of_device_is_stdout_path - check if a device node matches the
2077 * linux,stdout-path property
2079 * Check if this device node matches the linux,stdout-path property
2080 * in the chosen node. return true if yes, false otherwise.
2082 int of_device_is_stdout_path(struct device_node
*dn
)
2087 return of_stdout
== dn
;
2089 EXPORT_SYMBOL_GPL(of_device_is_stdout_path
);
2092 * of_find_next_cache_node - Find a node's subsidiary cache
2093 * @np: node of type "cpu" or "cache"
2095 * Returns a node pointer with refcount incremented, use
2096 * of_node_put() on it when done. Caller should hold a reference
2099 struct device_node
*of_find_next_cache_node(const struct device_node
*np
)
2101 struct device_node
*child
;
2102 const phandle
*handle
;
2104 handle
= of_get_property(np
, "l2-cache", NULL
);
2106 handle
= of_get_property(np
, "next-level-cache", NULL
);
2109 return of_find_node_by_phandle(be32_to_cpup(handle
));
2111 /* OF on pmac has nodes instead of properties named "l2-cache"
2112 * beneath CPU nodes.
2114 if (!strcmp(np
->type
, "cpu"))
2115 for_each_child_of_node(np
, child
)
2116 if (!strcmp(child
->type
, "cache"))