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
)
249 kobject_init(&np
->kobj
, &of_node_ktype
);
250 mutex_lock(&of_aliases_mutex
);
252 rc
= __of_node_add(np
);
253 mutex_unlock(&of_aliases_mutex
);
257 #if defined(CONFIG_OF_DYNAMIC)
258 static void of_node_remove(struct device_node
*np
)
262 for_each_property_of_node(np
, pp
)
263 sysfs_remove_bin_file(&np
->kobj
, &pp
->attr
);
265 kobject_del(&np
->kobj
);
269 static int __init
of_init(void)
271 struct device_node
*np
;
273 /* Create the kset, and register existing nodes */
274 mutex_lock(&of_aliases_mutex
);
275 of_kset
= kset_create_and_add("devicetree", NULL
, firmware_kobj
);
277 mutex_unlock(&of_aliases_mutex
);
280 for_each_of_allnodes(np
)
282 mutex_unlock(&of_aliases_mutex
);
284 #if !defined(CONFIG_PROC_DEVICETREE)
285 /* Symlink to the new tree when PROC_DEVICETREE is disabled */
287 proc_symlink("device-tree", NULL
, "/sys/firmware/devicetree/base");
288 #endif /* CONFIG_PROC_DEVICETREE */
292 core_initcall(of_init
);
294 static struct property
*__of_find_property(const struct device_node
*np
,
295 const char *name
, int *lenp
)
302 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
303 if (of_prop_cmp(pp
->name
, name
) == 0) {
313 struct property
*of_find_property(const struct device_node
*np
,
320 raw_spin_lock_irqsave(&devtree_lock
, flags
);
321 pp
= __of_find_property(np
, name
, lenp
);
322 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
326 EXPORT_SYMBOL(of_find_property
);
329 * of_find_all_nodes - Get next node in global list
330 * @prev: Previous node or NULL to start iteration
331 * of_node_put() will be called on it
333 * Returns a node pointer with refcount incremented, use
334 * of_node_put() on it when done.
336 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
338 struct device_node
*np
;
341 raw_spin_lock_irqsave(&devtree_lock
, flags
);
342 np
= prev
? prev
->allnext
: of_allnodes
;
343 for (; np
!= NULL
; np
= np
->allnext
)
347 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
350 EXPORT_SYMBOL(of_find_all_nodes
);
353 * Find a property with a given name for a given node
354 * and return the value.
356 static const void *__of_get_property(const struct device_node
*np
,
357 const char *name
, int *lenp
)
359 struct property
*pp
= __of_find_property(np
, name
, lenp
);
361 return pp
? pp
->value
: NULL
;
365 * Find a property with a given name for a given node
366 * and return the value.
368 const void *of_get_property(const struct device_node
*np
, const char *name
,
371 struct property
*pp
= of_find_property(np
, name
, lenp
);
373 return pp
? pp
->value
: NULL
;
375 EXPORT_SYMBOL(of_get_property
);
378 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
380 * @cpu: logical cpu index of a core/thread
381 * @phys_id: physical identifier of a core/thread
383 * CPU logical to physical index mapping is architecture specific.
384 * However this __weak function provides a default match of physical
385 * id to logical cpu index. phys_id provided here is usually values read
386 * from the device tree which must match the hardware internal registers.
388 * Returns true if the physical identifier and the logical cpu index
389 * correspond to the same core/thread, false otherwise.
391 bool __weak
arch_match_cpu_phys_id(int cpu
, u64 phys_id
)
393 return (u32
)phys_id
== cpu
;
397 * Checks if the given "prop_name" property holds the physical id of the
398 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
399 * NULL, local thread number within the core is returned in it.
401 static bool __of_find_n_match_cpu_property(struct device_node
*cpun
,
402 const char *prop_name
, int cpu
, unsigned int *thread
)
405 int ac
, prop_len
, tid
;
408 ac
= of_n_addr_cells(cpun
);
409 cell
= of_get_property(cpun
, prop_name
, &prop_len
);
412 prop_len
/= sizeof(*cell
) * ac
;
413 for (tid
= 0; tid
< prop_len
; tid
++) {
414 hwid
= of_read_number(cell
, ac
);
415 if (arch_match_cpu_phys_id(cpu
, hwid
)) {
426 * arch_find_n_match_cpu_physical_id - See if the given device node is
427 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
428 * else false. If 'thread' is non-NULL, the local thread number within the
429 * core is returned in it.
431 bool __weak
arch_find_n_match_cpu_physical_id(struct device_node
*cpun
,
432 int cpu
, unsigned int *thread
)
434 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
435 * for thread ids on PowerPC. If it doesn't exist fallback to
436 * standard "reg" property.
438 if (IS_ENABLED(CONFIG_PPC
) &&
439 __of_find_n_match_cpu_property(cpun
,
440 "ibm,ppc-interrupt-server#s",
444 if (__of_find_n_match_cpu_property(cpun
, "reg", cpu
, thread
))
451 * of_get_cpu_node - Get device node associated with the given logical CPU
453 * @cpu: CPU number(logical index) for which device node is required
454 * @thread: if not NULL, local thread number within the physical core is
457 * The main purpose of this function is to retrieve the device node for the
458 * given logical CPU index. It should be used to initialize the of_node in
459 * cpu device. Once of_node in cpu device is populated, all the further
460 * references can use that instead.
462 * CPU logical to physical index mapping is architecture specific and is built
463 * before booting secondary cores. This function uses arch_match_cpu_phys_id
464 * which can be overridden by architecture specific implementation.
466 * Returns a node pointer for the logical cpu if found, else NULL.
468 struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
)
470 struct device_node
*cpun
;
472 for_each_node_by_type(cpun
, "cpu") {
473 if (arch_find_n_match_cpu_physical_id(cpun
, cpu
, thread
))
478 EXPORT_SYMBOL(of_get_cpu_node
);
481 * __of_device_is_compatible() - Check if the node matches given constraints
482 * @device: pointer to node
483 * @compat: required compatible string, NULL or "" for any match
484 * @type: required device_type value, NULL or "" for any match
485 * @name: required node name, NULL or "" for any match
487 * Checks if the given @compat, @type and @name strings match the
488 * properties of the given @device. A constraints can be skipped by
489 * passing NULL or an empty string as the constraint.
491 * Returns 0 for no match, and a positive integer on match. The return
492 * value is a relative score with larger values indicating better
493 * matches. The score is weighted for the most specific compatible value
494 * to get the highest score. Matching type is next, followed by matching
495 * name. Practically speaking, this results in the following priority
498 * 1. specific compatible && type && name
499 * 2. specific compatible && type
500 * 3. specific compatible && name
501 * 4. specific compatible
502 * 5. general compatible && type && name
503 * 6. general compatible && type
504 * 7. general compatible && name
505 * 8. general compatible
510 static int __of_device_is_compatible(const struct device_node
*device
,
511 const char *compat
, const char *type
, const char *name
)
513 struct property
*prop
;
515 int index
= 0, score
= 0;
517 /* Compatible match has highest priority */
518 if (compat
&& compat
[0]) {
519 prop
= __of_find_property(device
, "compatible", NULL
);
520 for (cp
= of_prop_next_string(prop
, NULL
); cp
;
521 cp
= of_prop_next_string(prop
, cp
), index
++) {
522 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0) {
523 score
= INT_MAX
/2 - (index
<< 2);
531 /* Matching type is better than matching name */
532 if (type
&& type
[0]) {
533 if (!device
->type
|| of_node_cmp(type
, device
->type
))
538 /* Matching name is a bit better than not */
539 if (name
&& name
[0]) {
540 if (!device
->name
|| of_node_cmp(name
, device
->name
))
548 /** Checks if the given "compat" string matches one of the strings in
549 * the device's "compatible" property
551 int of_device_is_compatible(const struct device_node
*device
,
557 raw_spin_lock_irqsave(&devtree_lock
, flags
);
558 res
= __of_device_is_compatible(device
, compat
, NULL
, NULL
);
559 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
562 EXPORT_SYMBOL(of_device_is_compatible
);
565 * of_machine_is_compatible - Test root of device tree for a given compatible value
566 * @compat: compatible string to look for in root node's compatible property.
568 * Returns true if the root node has the given value in its
569 * compatible property.
571 int of_machine_is_compatible(const char *compat
)
573 struct device_node
*root
;
576 root
= of_find_node_by_path("/");
578 rc
= of_device_is_compatible(root
, compat
);
583 EXPORT_SYMBOL(of_machine_is_compatible
);
586 * __of_device_is_available - check if a device is available for use
588 * @device: Node to check for availability, with locks already held
590 * Returns 1 if the status property is absent or set to "okay" or "ok",
593 static int __of_device_is_available(const struct device_node
*device
)
601 status
= __of_get_property(device
, "status", &statlen
);
606 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
614 * of_device_is_available - check if a device is available for use
616 * @device: Node to check for availability
618 * Returns 1 if the status property is absent or set to "okay" or "ok",
621 int of_device_is_available(const struct device_node
*device
)
626 raw_spin_lock_irqsave(&devtree_lock
, flags
);
627 res
= __of_device_is_available(device
);
628 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
632 EXPORT_SYMBOL(of_device_is_available
);
635 * of_get_parent - Get a node's parent if any
636 * @node: Node to get parent
638 * Returns a node pointer with refcount incremented, use
639 * of_node_put() on it when done.
641 struct device_node
*of_get_parent(const struct device_node
*node
)
643 struct device_node
*np
;
649 raw_spin_lock_irqsave(&devtree_lock
, flags
);
650 np
= of_node_get(node
->parent
);
651 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
654 EXPORT_SYMBOL(of_get_parent
);
657 * of_get_next_parent - Iterate to a node's parent
658 * @node: Node to get parent of
660 * This is like of_get_parent() except that it drops the
661 * refcount on the passed node, making it suitable for iterating
662 * through a node's parents.
664 * Returns a node pointer with refcount incremented, use
665 * of_node_put() on it when done.
667 struct device_node
*of_get_next_parent(struct device_node
*node
)
669 struct device_node
*parent
;
675 raw_spin_lock_irqsave(&devtree_lock
, flags
);
676 parent
= of_node_get(node
->parent
);
678 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
681 EXPORT_SYMBOL(of_get_next_parent
);
684 * of_get_next_child - Iterate a node childs
686 * @prev: previous child of the parent node, or NULL to get first
688 * Returns a node pointer with refcount incremented, use
689 * of_node_put() on it when done.
691 struct device_node
*of_get_next_child(const struct device_node
*node
,
692 struct device_node
*prev
)
694 struct device_node
*next
;
697 raw_spin_lock_irqsave(&devtree_lock
, flags
);
698 next
= prev
? prev
->sibling
: node
->child
;
699 for (; next
; next
= next
->sibling
)
700 if (of_node_get(next
))
703 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
706 EXPORT_SYMBOL(of_get_next_child
);
709 * of_get_next_available_child - Find the next available child node
711 * @prev: previous child of the parent node, or NULL to get first
713 * This function is like of_get_next_child(), except that it
714 * automatically skips any disabled nodes (i.e. status = "disabled").
716 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
717 struct device_node
*prev
)
719 struct device_node
*next
;
722 raw_spin_lock_irqsave(&devtree_lock
, flags
);
723 next
= prev
? prev
->sibling
: node
->child
;
724 for (; next
; next
= next
->sibling
) {
725 if (!__of_device_is_available(next
))
727 if (of_node_get(next
))
731 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
734 EXPORT_SYMBOL(of_get_next_available_child
);
737 * of_get_child_by_name - Find the child node by name for a given parent
739 * @name: child name to look for.
741 * This function looks for child node for given matching name
743 * Returns a node pointer if found, with refcount incremented, use
744 * of_node_put() on it when done.
745 * Returns NULL if node is not found.
747 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
750 struct device_node
*child
;
752 for_each_child_of_node(node
, child
)
753 if (child
->name
&& (of_node_cmp(child
->name
, name
) == 0))
757 EXPORT_SYMBOL(of_get_child_by_name
);
760 * of_find_node_by_path - Find a node matching a full OF path
761 * @path: The full path to match
763 * Returns a node pointer with refcount incremented, use
764 * of_node_put() on it when done.
766 struct device_node
*of_find_node_by_path(const char *path
)
768 struct device_node
*np
= of_allnodes
;
771 raw_spin_lock_irqsave(&devtree_lock
, flags
);
772 for (; np
; np
= np
->allnext
) {
773 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
777 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
780 EXPORT_SYMBOL(of_find_node_by_path
);
783 * of_find_node_by_name - Find a node by its "name" property
784 * @from: The node to start searching from or NULL, the node
785 * you pass will not be searched, only the next one
786 * will; typically, you pass what the previous call
787 * returned. of_node_put() will be called on it
788 * @name: The name string to match against
790 * Returns a node pointer with refcount incremented, use
791 * of_node_put() on it when done.
793 struct device_node
*of_find_node_by_name(struct device_node
*from
,
796 struct device_node
*np
;
799 raw_spin_lock_irqsave(&devtree_lock
, flags
);
800 np
= from
? from
->allnext
: of_allnodes
;
801 for (; np
; np
= np
->allnext
)
802 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
806 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
809 EXPORT_SYMBOL(of_find_node_by_name
);
812 * of_find_node_by_type - Find a node by its "device_type" property
813 * @from: The node to start searching from, or NULL to start searching
814 * the entire device tree. The node you pass will not be
815 * searched, only the next one will; typically, you pass
816 * what the previous call returned. of_node_put() will be
817 * called on from for you.
818 * @type: The type string to match against
820 * Returns a node pointer with refcount incremented, use
821 * of_node_put() on it when done.
823 struct device_node
*of_find_node_by_type(struct device_node
*from
,
826 struct device_node
*np
;
829 raw_spin_lock_irqsave(&devtree_lock
, flags
);
830 np
= from
? from
->allnext
: of_allnodes
;
831 for (; np
; np
= np
->allnext
)
832 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
836 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
839 EXPORT_SYMBOL(of_find_node_by_type
);
842 * of_find_compatible_node - Find a node based on type and one of the
843 * tokens in its "compatible" property
844 * @from: The node to start searching from or NULL, the node
845 * you pass will not be searched, only the next one
846 * will; typically, you pass what the previous call
847 * returned. of_node_put() will be called on it
848 * @type: The type string to match "device_type" or NULL to ignore
849 * @compatible: The string to match to one of the tokens in the device
852 * Returns a node pointer with refcount incremented, use
853 * of_node_put() on it when done.
855 struct device_node
*of_find_compatible_node(struct device_node
*from
,
856 const char *type
, const char *compatible
)
858 struct device_node
*np
;
861 raw_spin_lock_irqsave(&devtree_lock
, flags
);
862 np
= from
? from
->allnext
: of_allnodes
;
863 for (; np
; np
= np
->allnext
) {
864 if (__of_device_is_compatible(np
, compatible
, type
, NULL
) &&
869 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
872 EXPORT_SYMBOL(of_find_compatible_node
);
875 * of_find_node_with_property - Find a node which has a property with
877 * @from: The node to start searching from or NULL, the node
878 * you pass will not be searched, only the next one
879 * will; typically, you pass what the previous call
880 * returned. of_node_put() will be called on it
881 * @prop_name: The name of the property to look for.
883 * Returns a node pointer with refcount incremented, use
884 * of_node_put() on it when done.
886 struct device_node
*of_find_node_with_property(struct device_node
*from
,
887 const char *prop_name
)
889 struct device_node
*np
;
893 raw_spin_lock_irqsave(&devtree_lock
, flags
);
894 np
= from
? from
->allnext
: of_allnodes
;
895 for (; np
; np
= np
->allnext
) {
896 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
897 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
905 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
908 EXPORT_SYMBOL(of_find_node_with_property
);
911 const struct of_device_id
*__of_match_node(const struct of_device_id
*matches
,
912 const struct device_node
*node
)
914 const struct of_device_id
*best_match
= NULL
;
915 int score
, best_score
= 0;
920 for (; matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]; matches
++) {
921 score
= __of_device_is_compatible(node
, matches
->compatible
,
922 matches
->type
, matches
->name
);
923 if (score
> best_score
) {
924 best_match
= matches
;
933 * of_match_node - Tell if an device_node has a matching of_match structure
934 * @matches: array of of device match structures to search in
935 * @node: the of device structure to match against
937 * Low level utility function used by device matching.
939 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
940 const struct device_node
*node
)
942 const struct of_device_id
*match
;
945 raw_spin_lock_irqsave(&devtree_lock
, flags
);
946 match
= __of_match_node(matches
, node
);
947 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
950 EXPORT_SYMBOL(of_match_node
);
953 * of_find_matching_node_and_match - Find a node based on an of_device_id
955 * @from: The node to start searching from or NULL, the node
956 * you pass will not be searched, only the next one
957 * will; typically, you pass what the previous call
958 * returned. of_node_put() will be called on it
959 * @matches: array of of device match structures to search in
960 * @match Updated to point at the matches entry which matched
962 * Returns a node pointer with refcount incremented, use
963 * of_node_put() on it when done.
965 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
966 const struct of_device_id
*matches
,
967 const struct of_device_id
**match
)
969 struct device_node
*np
;
970 const struct of_device_id
*m
;
976 raw_spin_lock_irqsave(&devtree_lock
, flags
);
977 np
= from
? from
->allnext
: of_allnodes
;
978 for (; np
; np
= np
->allnext
) {
979 m
= __of_match_node(matches
, np
);
980 if (m
&& of_node_get(np
)) {
987 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
990 EXPORT_SYMBOL(of_find_matching_node_and_match
);
993 * of_modalias_node - Lookup appropriate modalias for a device node
994 * @node: pointer to a device tree node
995 * @modalias: Pointer to buffer that modalias value will be copied into
996 * @len: Length of modalias value
998 * Based on the value of the compatible property, this routine will attempt
999 * to choose an appropriate modalias value for a particular device tree node.
1000 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1001 * from the first entry in the compatible list property.
1003 * This routine returns 0 on success, <0 on failure.
1005 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
1007 const char *compatible
, *p
;
1010 compatible
= of_get_property(node
, "compatible", &cplen
);
1011 if (!compatible
|| strlen(compatible
) > cplen
)
1013 p
= strchr(compatible
, ',');
1014 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
1017 EXPORT_SYMBOL_GPL(of_modalias_node
);
1020 * of_find_node_by_phandle - Find a node given a phandle
1021 * @handle: phandle of the node to find
1023 * Returns a node pointer with refcount incremented, use
1024 * of_node_put() on it when done.
1026 struct device_node
*of_find_node_by_phandle(phandle handle
)
1028 struct device_node
*np
;
1029 unsigned long flags
;
1031 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1032 for (np
= of_allnodes
; np
; np
= np
->allnext
)
1033 if (np
->phandle
== handle
)
1036 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1039 EXPORT_SYMBOL(of_find_node_by_phandle
);
1042 * of_find_property_value_of_size
1044 * @np: device node from which the property value is to be read.
1045 * @propname: name of the property to be searched.
1046 * @len: requested length of property value
1048 * Search for a property in a device node and valid the requested size.
1049 * Returns the property value on success, -EINVAL if the property does not
1050 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1051 * property data isn't large enough.
1054 static void *of_find_property_value_of_size(const struct device_node
*np
,
1055 const char *propname
, u32 len
)
1057 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1060 return ERR_PTR(-EINVAL
);
1062 return ERR_PTR(-ENODATA
);
1063 if (len
> prop
->length
)
1064 return ERR_PTR(-EOVERFLOW
);
1070 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1072 * @np: device node from which the property value is to be read.
1073 * @propname: name of the property to be searched.
1074 * @index: index of the u32 in the list of values
1075 * @out_value: pointer to return value, modified only if no error.
1077 * Search for a property in a device node and read nth 32-bit value from
1078 * it. Returns 0 on success, -EINVAL if the property does not exist,
1079 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1080 * property data isn't large enough.
1082 * The out_value is modified only if a valid u32 value can be decoded.
1084 int of_property_read_u32_index(const struct device_node
*np
,
1085 const char *propname
,
1086 u32 index
, u32
*out_value
)
1088 const u32
*val
= of_find_property_value_of_size(np
, propname
,
1089 ((index
+ 1) * sizeof(*out_value
)));
1092 return PTR_ERR(val
);
1094 *out_value
= be32_to_cpup(((__be32
*)val
) + index
);
1097 EXPORT_SYMBOL_GPL(of_property_read_u32_index
);
1100 * of_property_read_u8_array - Find and read an array of u8 from a property.
1102 * @np: device node from which the property value is to be read.
1103 * @propname: name of the property to be searched.
1104 * @out_values: pointer to return value, modified only if return value is 0.
1105 * @sz: number of array elements to read
1107 * Search for a property in a device node and read 8-bit value(s) from
1108 * it. Returns 0 on success, -EINVAL if the property does not exist,
1109 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1110 * property data isn't large enough.
1112 * dts entry of array should be like:
1113 * property = /bits/ 8 <0x50 0x60 0x70>;
1115 * The out_values is modified only if a valid u8 value can be decoded.
1117 int of_property_read_u8_array(const struct device_node
*np
,
1118 const char *propname
, u8
*out_values
, size_t sz
)
1120 const u8
*val
= of_find_property_value_of_size(np
, propname
,
1121 (sz
* sizeof(*out_values
)));
1124 return PTR_ERR(val
);
1127 *out_values
++ = *val
++;
1130 EXPORT_SYMBOL_GPL(of_property_read_u8_array
);
1133 * of_property_read_u16_array - Find and read an array of u16 from a property.
1135 * @np: device node from which the property value is to be read.
1136 * @propname: name of the property to be searched.
1137 * @out_values: pointer to return value, modified only if return value is 0.
1138 * @sz: number of array elements to read
1140 * Search for a property in a device node and read 16-bit value(s) from
1141 * it. Returns 0 on success, -EINVAL if the property does not exist,
1142 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1143 * property data isn't large enough.
1145 * dts entry of array should be like:
1146 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1148 * The out_values is modified only if a valid u16 value can be decoded.
1150 int of_property_read_u16_array(const struct device_node
*np
,
1151 const char *propname
, u16
*out_values
, size_t sz
)
1153 const __be16
*val
= of_find_property_value_of_size(np
, propname
,
1154 (sz
* sizeof(*out_values
)));
1157 return PTR_ERR(val
);
1160 *out_values
++ = be16_to_cpup(val
++);
1163 EXPORT_SYMBOL_GPL(of_property_read_u16_array
);
1166 * of_property_read_u32_array - Find and read an array of 32 bit integers
1169 * @np: device node from which the property value is to be read.
1170 * @propname: name of the property to be searched.
1171 * @out_values: pointer to return value, modified only if return value is 0.
1172 * @sz: number of array elements to read
1174 * Search for a property in a device node and read 32-bit value(s) from
1175 * it. Returns 0 on success, -EINVAL if the property does not exist,
1176 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1177 * property data isn't large enough.
1179 * The out_values is modified only if a valid u32 value can be decoded.
1181 int of_property_read_u32_array(const struct device_node
*np
,
1182 const char *propname
, u32
*out_values
,
1185 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1186 (sz
* sizeof(*out_values
)));
1189 return PTR_ERR(val
);
1192 *out_values
++ = be32_to_cpup(val
++);
1195 EXPORT_SYMBOL_GPL(of_property_read_u32_array
);
1198 * of_property_read_u64 - Find and read a 64 bit integer from a property
1199 * @np: device node from which the property value is to be read.
1200 * @propname: name of the property to be searched.
1201 * @out_value: pointer to return value, modified only if return value is 0.
1203 * Search for a property in a device node and read a 64-bit value from
1204 * it. Returns 0 on success, -EINVAL if the property does not exist,
1205 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1206 * property data isn't large enough.
1208 * The out_value is modified only if a valid u64 value can be decoded.
1210 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
1213 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1214 sizeof(*out_value
));
1217 return PTR_ERR(val
);
1219 *out_value
= of_read_number(val
, 2);
1222 EXPORT_SYMBOL_GPL(of_property_read_u64
);
1225 * of_property_read_string - Find and read a string from a property
1226 * @np: device node from which the property value is to be read.
1227 * @propname: name of the property to be searched.
1228 * @out_string: pointer to null terminated return string, modified only if
1229 * return value is 0.
1231 * Search for a property in a device tree node and retrieve a null
1232 * terminated string value (pointer to data, not a copy). Returns 0 on
1233 * success, -EINVAL if the property does not exist, -ENODATA if property
1234 * does not have a value, and -EILSEQ if the string is not null-terminated
1235 * within the length of the property data.
1237 * The out_string pointer is modified only if a valid string can be decoded.
1239 int of_property_read_string(struct device_node
*np
, const char *propname
,
1240 const char **out_string
)
1242 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1247 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
1249 *out_string
= prop
->value
;
1252 EXPORT_SYMBOL_GPL(of_property_read_string
);
1255 * of_property_read_string_index - Find and read a string from a multiple
1257 * @np: device node from which the property value is to be read.
1258 * @propname: name of the property to be searched.
1259 * @index: index of the string in the list of strings
1260 * @out_string: pointer to null terminated return string, modified only if
1261 * return value is 0.
1263 * Search for a property in a device tree node and retrieve a null
1264 * terminated string value (pointer to data, not a copy) in the list of strings
1265 * contained in that property.
1266 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1267 * property does not have a value, and -EILSEQ if the string is not
1268 * null-terminated within the length of the property data.
1270 * The out_string pointer is modified only if a valid string can be decoded.
1272 int of_property_read_string_index(struct device_node
*np
, const char *propname
,
1273 int index
, const char **output
)
1275 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1277 size_t l
= 0, total
= 0;
1284 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
1289 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
) {
1298 EXPORT_SYMBOL_GPL(of_property_read_string_index
);
1301 * of_property_match_string() - Find string in a list and return index
1302 * @np: pointer to node containing string list property
1303 * @propname: string list property name
1304 * @string: pointer to string to search for in string list
1306 * This function searches a string list property and returns the index
1307 * of a specific string value.
1309 int of_property_match_string(struct device_node
*np
, const char *propname
,
1312 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1315 const char *p
, *end
;
1323 end
= p
+ prop
->length
;
1325 for (i
= 0; p
< end
; i
++, p
+= l
) {
1329 pr_debug("comparing %s with %s\n", string
, p
);
1330 if (strcmp(string
, p
) == 0)
1331 return i
; /* Found it; return index */
1335 EXPORT_SYMBOL_GPL(of_property_match_string
);
1338 * of_property_count_strings - Find and return the number of strings from a
1339 * multiple strings property.
1340 * @np: device node from which the property value is to be read.
1341 * @propname: name of the property to be searched.
1343 * Search for a property in a device tree node and retrieve the number of null
1344 * terminated string contain in it. Returns the number of strings on
1345 * success, -EINVAL if the property does not exist, -ENODATA if property
1346 * does not have a value, and -EILSEQ if the string is not null-terminated
1347 * within the length of the property data.
1349 int of_property_count_strings(struct device_node
*np
, const char *propname
)
1351 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1353 size_t l
= 0, total
= 0;
1360 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
1365 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
, i
++)
1370 EXPORT_SYMBOL_GPL(of_property_count_strings
);
1372 void of_print_phandle_args(const char *msg
, const struct of_phandle_args
*args
)
1375 printk("%s %s", msg
, of_node_full_name(args
->np
));
1376 for (i
= 0; i
< args
->args_count
; i
++)
1377 printk(i
? ",%08x" : ":%08x", args
->args
[i
]);
1381 static int __of_parse_phandle_with_args(const struct device_node
*np
,
1382 const char *list_name
,
1383 const char *cells_name
,
1384 int cell_count
, int index
,
1385 struct of_phandle_args
*out_args
)
1387 const __be32
*list
, *list_end
;
1388 int rc
= 0, size
, cur_index
= 0;
1390 struct device_node
*node
= NULL
;
1393 /* Retrieve the phandle list property */
1394 list
= of_get_property(np
, list_name
, &size
);
1397 list_end
= list
+ size
/ sizeof(*list
);
1399 /* Loop over the phandles until all the requested entry is found */
1400 while (list
< list_end
) {
1405 * If phandle is 0, then it is an empty entry with no
1406 * arguments. Skip forward to the next entry.
1408 phandle
= be32_to_cpup(list
++);
1411 * Find the provider node and parse the #*-cells
1412 * property to determine the argument length.
1414 * This is not needed if the cell count is hard-coded
1415 * (i.e. cells_name not set, but cell_count is set),
1416 * except when we're going to return the found node
1419 if (cells_name
|| cur_index
== index
) {
1420 node
= of_find_node_by_phandle(phandle
);
1422 pr_err("%s: could not find phandle\n",
1429 if (of_property_read_u32(node
, cells_name
,
1431 pr_err("%s: could not get %s for %s\n",
1432 np
->full_name
, cells_name
,
1441 * Make sure that the arguments actually fit in the
1442 * remaining property data length
1444 if (list
+ count
> list_end
) {
1445 pr_err("%s: arguments longer than property\n",
1452 * All of the error cases above bail out of the loop, so at
1453 * this point, the parsing is successful. If the requested
1454 * index matches, then fill the out_args structure and return,
1455 * or return -ENOENT for an empty entry.
1458 if (cur_index
== index
) {
1464 if (WARN_ON(count
> MAX_PHANDLE_ARGS
))
1465 count
= MAX_PHANDLE_ARGS
;
1466 out_args
->np
= node
;
1467 out_args
->args_count
= count
;
1468 for (i
= 0; i
< count
; i
++)
1469 out_args
->args
[i
] = be32_to_cpup(list
++);
1474 /* Found it! return success */
1485 * Unlock node before returning result; will be one of:
1486 * -ENOENT : index is for empty phandle
1487 * -EINVAL : parsing error on data
1488 * [1..n] : Number of phandle (count mode; when index = -1)
1490 rc
= index
< 0 ? cur_index
: -ENOENT
;
1498 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1499 * @np: Pointer to device node holding phandle property
1500 * @phandle_name: Name of property holding a phandle value
1501 * @index: For properties holding a table of phandles, this is the index into
1504 * Returns the device_node pointer with refcount incremented. Use
1505 * of_node_put() on it when done.
1507 struct device_node
*of_parse_phandle(const struct device_node
*np
,
1508 const char *phandle_name
, int index
)
1510 struct of_phandle_args args
;
1515 if (__of_parse_phandle_with_args(np
, phandle_name
, NULL
, 0,
1521 EXPORT_SYMBOL(of_parse_phandle
);
1524 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1525 * @np: pointer to a device tree node containing a list
1526 * @list_name: property name that contains a list
1527 * @cells_name: property name that specifies phandles' arguments count
1528 * @index: index of a phandle to parse out
1529 * @out_args: optional pointer to output arguments structure (will be filled)
1531 * This function is useful to parse lists of phandles and their arguments.
1532 * Returns 0 on success and fills out_args, on error returns appropriate
1535 * Caller is responsible to call of_node_put() on the returned out_args->node
1541 * #list-cells = <2>;
1545 * #list-cells = <1>;
1549 * list = <&phandle1 1 2 &phandle2 3>;
1552 * To get a device_node of the `node2' node you may call this:
1553 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1555 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1556 const char *cells_name
, int index
,
1557 struct of_phandle_args
*out_args
)
1561 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, 0,
1564 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1567 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1568 * @np: pointer to a device tree node containing a list
1569 * @list_name: property name that contains a list
1570 * @cell_count: number of argument cells following the phandle
1571 * @index: index of a phandle to parse out
1572 * @out_args: optional pointer to output arguments structure (will be filled)
1574 * This function is useful to parse lists of phandles and their arguments.
1575 * Returns 0 on success and fills out_args, on error returns appropriate
1578 * Caller is responsible to call of_node_put() on the returned out_args->node
1590 * list = <&phandle1 0 2 &phandle2 2 3>;
1593 * To get a device_node of the `node2' node you may call this:
1594 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1596 int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
1597 const char *list_name
, int cell_count
,
1598 int index
, struct of_phandle_args
*out_args
)
1602 return __of_parse_phandle_with_args(np
, list_name
, NULL
, cell_count
,
1605 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args
);
1608 * of_count_phandle_with_args() - Find the number of phandles references in a property
1609 * @np: pointer to a device tree node containing a list
1610 * @list_name: property name that contains a list
1611 * @cells_name: property name that specifies phandles' arguments count
1613 * Returns the number of phandle + argument tuples within a property. It
1614 * is a typical pattern to encode a list of phandle and variable
1615 * arguments into a single property. The number of arguments is encoded
1616 * by a property in the phandle-target node. For example, a gpios
1617 * property would contain a list of GPIO specifies consisting of a
1618 * phandle and 1 or more arguments. The number of arguments are
1619 * determined by the #gpio-cells property in the node pointed to by the
1622 int of_count_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1623 const char *cells_name
)
1625 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, 0, -1,
1628 EXPORT_SYMBOL(of_count_phandle_with_args
);
1630 #if defined(CONFIG_OF_DYNAMIC)
1631 static int of_property_notify(int action
, struct device_node
*np
,
1632 struct property
*prop
)
1634 struct of_prop_reconfig pr
;
1638 return of_reconfig_notify(action
, &pr
);
1641 static int of_property_notify(int action
, struct device_node
*np
,
1642 struct property
*prop
)
1649 * __of_add_property - Add a property to a node without lock operations
1651 static int __of_add_property(struct device_node
*np
, struct property
*prop
)
1653 struct property
**next
;
1656 next
= &np
->properties
;
1658 if (strcmp(prop
->name
, (*next
)->name
) == 0)
1659 /* duplicate ! don't insert it */
1662 next
= &(*next
)->next
;
1670 * of_add_property - Add a property to a node
1672 int of_add_property(struct device_node
*np
, struct property
*prop
)
1674 unsigned long flags
;
1677 rc
= of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
);
1681 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1682 rc
= __of_add_property(np
, prop
);
1683 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1687 /* at early boot, bail hear and defer setup to of_init() */
1691 __of_add_property_sysfs(np
, prop
);
1693 #ifdef CONFIG_PROC_DEVICETREE
1694 /* try to add to proc as well if it was initialized */
1696 proc_device_tree_add_prop(np
->pde
, prop
);
1697 #endif /* CONFIG_PROC_DEVICETREE */
1703 * of_remove_property - Remove a property from a node.
1705 * Note that we don't actually remove it, since we have given out
1706 * who-knows-how-many pointers to the data using get-property.
1707 * Instead we just move the property to the "dead properties"
1708 * list, so it won't be found any more.
1710 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1712 struct property
**next
;
1713 unsigned long flags
;
1717 rc
= of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
);
1721 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1722 next
= &np
->properties
;
1724 if (*next
== prop
) {
1725 /* found the node */
1727 prop
->next
= np
->deadprops
;
1728 np
->deadprops
= prop
;
1732 next
= &(*next
)->next
;
1734 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1739 /* at early boot, bail hear and defer setup to of_init() */
1743 sysfs_remove_bin_file(&np
->kobj
, &prop
->attr
);
1745 #ifdef CONFIG_PROC_DEVICETREE
1746 /* try to remove the proc node as well */
1748 proc_device_tree_remove_prop(np
->pde
, prop
);
1749 #endif /* CONFIG_PROC_DEVICETREE */
1755 * of_update_property - Update a property in a node, if the property does
1756 * not exist, add it.
1758 * Note that we don't actually remove it, since we have given out
1759 * who-knows-how-many pointers to the data using get-property.
1760 * Instead we just move the property to the "dead properties" list,
1761 * and add the new property to the property list
1763 int of_update_property(struct device_node
*np
, struct property
*newprop
)
1765 struct property
**next
, *oldprop
;
1766 unsigned long flags
;
1769 rc
= of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
);
1776 oldprop
= of_find_property(np
, newprop
->name
, NULL
);
1778 return of_add_property(np
, newprop
);
1780 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1781 next
= &np
->properties
;
1783 if (*next
== oldprop
) {
1784 /* found the node */
1785 newprop
->next
= oldprop
->next
;
1787 oldprop
->next
= np
->deadprops
;
1788 np
->deadprops
= oldprop
;
1792 next
= &(*next
)->next
;
1794 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1798 /* Update the sysfs attribute */
1800 sysfs_remove_bin_file(&np
->kobj
, &oldprop
->attr
);
1801 __of_add_property_sysfs(np
, newprop
);
1806 #ifdef CONFIG_PROC_DEVICETREE
1807 /* try to add to proc as well if it was initialized */
1809 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
1810 #endif /* CONFIG_PROC_DEVICETREE */
1815 #if defined(CONFIG_OF_DYNAMIC)
1817 * Support for dynamic device trees.
1819 * On some platforms, the device tree can be manipulated at runtime.
1820 * The routines in this section support adding, removing and changing
1821 * device tree nodes.
1824 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain
);
1826 int of_reconfig_notifier_register(struct notifier_block
*nb
)
1828 return blocking_notifier_chain_register(&of_reconfig_chain
, nb
);
1830 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register
);
1832 int of_reconfig_notifier_unregister(struct notifier_block
*nb
)
1834 return blocking_notifier_chain_unregister(&of_reconfig_chain
, nb
);
1836 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister
);
1838 int of_reconfig_notify(unsigned long action
, void *p
)
1842 rc
= blocking_notifier_call_chain(&of_reconfig_chain
, action
, p
);
1843 return notifier_to_errno(rc
);
1846 #ifdef CONFIG_PROC_DEVICETREE
1847 static void of_add_proc_dt_entry(struct device_node
*dn
)
1849 struct proc_dir_entry
*ent
;
1851 ent
= proc_mkdir(strrchr(dn
->full_name
, '/') + 1, dn
->parent
->pde
);
1853 proc_device_tree_add_node(dn
, ent
);
1856 static void of_add_proc_dt_entry(struct device_node
*dn
)
1863 * of_attach_node - Plug a device node into the tree and global list.
1865 int of_attach_node(struct device_node
*np
)
1867 unsigned long flags
;
1870 rc
= of_reconfig_notify(OF_RECONFIG_ATTACH_NODE
, np
);
1874 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1875 np
->sibling
= np
->parent
->child
;
1876 np
->allnext
= of_allnodes
;
1877 np
->parent
->child
= np
;
1879 of_node_clear_flag(np
, OF_DETACHED
);
1880 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1883 of_add_proc_dt_entry(np
);
1887 #ifdef CONFIG_PROC_DEVICETREE
1888 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1890 proc_remove(dn
->pde
);
1893 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1900 * of_detach_node - "Unplug" a node from the device tree.
1902 * The caller must hold a reference to the node. The memory associated with
1903 * the node is not freed until its refcount goes to zero.
1905 int of_detach_node(struct device_node
*np
)
1907 struct device_node
*parent
;
1908 unsigned long flags
;
1911 rc
= of_reconfig_notify(OF_RECONFIG_DETACH_NODE
, np
);
1915 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1917 if (of_node_check_flag(np
, OF_DETACHED
)) {
1918 /* someone already detached it */
1919 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1923 parent
= np
->parent
;
1925 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1929 if (of_allnodes
== np
)
1930 of_allnodes
= np
->allnext
;
1932 struct device_node
*prev
;
1933 for (prev
= of_allnodes
;
1934 prev
->allnext
!= np
;
1935 prev
= prev
->allnext
)
1937 prev
->allnext
= np
->allnext
;
1940 if (parent
->child
== np
)
1941 parent
->child
= np
->sibling
;
1943 struct device_node
*prevsib
;
1944 for (prevsib
= np
->parent
->child
;
1945 prevsib
->sibling
!= np
;
1946 prevsib
= prevsib
->sibling
)
1948 prevsib
->sibling
= np
->sibling
;
1951 of_node_set_flag(np
, OF_DETACHED
);
1952 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1954 of_remove_proc_dt_entry(np
);
1958 #endif /* defined(CONFIG_OF_DYNAMIC) */
1960 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
1961 int id
, const char *stem
, int stem_len
)
1965 strncpy(ap
->stem
, stem
, stem_len
);
1966 ap
->stem
[stem_len
] = 0;
1967 list_add_tail(&ap
->link
, &aliases_lookup
);
1968 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1969 ap
->alias
, ap
->stem
, ap
->id
, of_node_full_name(np
));
1973 * of_alias_scan - Scan all properties of 'aliases' node
1975 * The function scans all the properties of 'aliases' node and populate
1976 * the the global lookup table with the properties. It returns the
1977 * number of alias_prop found, or error code in error case.
1979 * @dt_alloc: An allocator that provides a virtual address to memory
1980 * for the resulting tree
1982 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
1984 struct property
*pp
;
1986 of_chosen
= of_find_node_by_path("/chosen");
1987 if (of_chosen
== NULL
)
1988 of_chosen
= of_find_node_by_path("/chosen@0");
1993 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
1995 of_stdout
= of_find_node_by_path(name
);
1998 of_aliases
= of_find_node_by_path("/aliases");
2002 for_each_property_of_node(of_aliases
, pp
) {
2003 const char *start
= pp
->name
;
2004 const char *end
= start
+ strlen(start
);
2005 struct device_node
*np
;
2006 struct alias_prop
*ap
;
2009 /* Skip those we do not want to proceed */
2010 if (!strcmp(pp
->name
, "name") ||
2011 !strcmp(pp
->name
, "phandle") ||
2012 !strcmp(pp
->name
, "linux,phandle"))
2015 np
= of_find_node_by_path(pp
->value
);
2019 /* walk the alias backwards to extract the id and work out
2020 * the 'stem' string */
2021 while (isdigit(*(end
-1)) && end
> start
)
2025 if (kstrtoint(end
, 10, &id
) < 0)
2028 /* Allocate an alias_prop with enough space for the stem */
2029 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, 4);
2032 memset(ap
, 0, sizeof(*ap
) + len
+ 1);
2034 of_alias_add(ap
, np
, id
, start
, len
);
2039 * of_alias_get_id - Get alias id for the given device_node
2040 * @np: Pointer to the given device_node
2041 * @stem: Alias stem of the given device_node
2043 * The function travels the lookup table to get alias id for the given
2044 * device_node and alias stem. It returns the alias id if find it.
2046 int of_alias_get_id(struct device_node
*np
, const char *stem
)
2048 struct alias_prop
*app
;
2051 mutex_lock(&of_aliases_mutex
);
2052 list_for_each_entry(app
, &aliases_lookup
, link
) {
2053 if (strcmp(app
->stem
, stem
) != 0)
2056 if (np
== app
->np
) {
2061 mutex_unlock(&of_aliases_mutex
);
2065 EXPORT_SYMBOL_GPL(of_alias_get_id
);
2067 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
2070 const void *curv
= cur
;
2080 curv
+= sizeof(*cur
);
2081 if (curv
>= prop
->value
+ prop
->length
)
2085 *pu
= be32_to_cpup(curv
);
2088 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
2090 const char *of_prop_next_string(struct property
*prop
, const char *cur
)
2092 const void *curv
= cur
;
2100 curv
+= strlen(cur
) + 1;
2101 if (curv
>= prop
->value
+ prop
->length
)
2106 EXPORT_SYMBOL_GPL(of_prop_next_string
);
2109 * of_device_is_stdout_path - check if a device node matches the
2110 * linux,stdout-path property
2112 * Check if this device node matches the linux,stdout-path property
2113 * in the chosen node. return true if yes, false otherwise.
2115 int of_device_is_stdout_path(struct device_node
*dn
)
2120 return of_stdout
== dn
;
2122 EXPORT_SYMBOL_GPL(of_device_is_stdout_path
);
2125 * of_find_next_cache_node - Find a node's subsidiary cache
2126 * @np: node of type "cpu" or "cache"
2128 * Returns a node pointer with refcount incremented, use
2129 * of_node_put() on it when done. Caller should hold a reference
2132 struct device_node
*of_find_next_cache_node(const struct device_node
*np
)
2134 struct device_node
*child
;
2135 const phandle
*handle
;
2137 handle
= of_get_property(np
, "l2-cache", NULL
);
2139 handle
= of_get_property(np
, "next-level-cache", NULL
);
2142 return of_find_node_by_phandle(be32_to_cpup(handle
));
2144 /* OF on pmac has nodes instead of properties named "l2-cache"
2145 * beneath CPU nodes.
2147 if (!strcmp(np
->type
, "cpu"))
2148 for_each_child_of_node(np
, child
)
2149 if (!strcmp(child
->type
, "cache"))