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/console.h>
21 #include <linux/ctype.h>
22 #include <linux/cpu.h>
23 #include <linux/module.h>
25 #include <linux/of_graph.h>
26 #include <linux/spinlock.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <linux/proc_fs.h>
31 #include "of_private.h"
33 LIST_HEAD(aliases_lookup
);
35 struct device_node
*of_root
;
36 EXPORT_SYMBOL(of_root
);
37 struct device_node
*of_chosen
;
38 struct device_node
*of_aliases
;
39 struct device_node
*of_stdout
;
40 static const char *of_stdout_options
;
45 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
46 * This mutex must be held whenever modifications are being made to the
47 * device tree. The of_{attach,detach}_node() and
48 * of_{add,remove,update}_property() helpers make sure this happens.
50 DEFINE_MUTEX(of_mutex
);
52 /* use when traversing tree through the child, sibling,
53 * or parent members of struct device_node.
55 DEFINE_RAW_SPINLOCK(devtree_lock
);
57 int of_n_addr_cells(struct device_node
*np
)
64 ip
= of_get_property(np
, "#address-cells", NULL
);
66 return be32_to_cpup(ip
);
68 /* No #address-cells property for the root node */
69 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
71 EXPORT_SYMBOL(of_n_addr_cells
);
73 int of_n_size_cells(struct device_node
*np
)
80 ip
= of_get_property(np
, "#size-cells", NULL
);
82 return be32_to_cpup(ip
);
84 /* No #size-cells property for the root node */
85 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
87 EXPORT_SYMBOL(of_n_size_cells
);
90 int __weak
of_node_to_nid(struct device_node
*np
)
96 #ifndef CONFIG_OF_DYNAMIC
97 static void of_node_release(struct kobject
*kobj
)
99 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
101 #endif /* CONFIG_OF_DYNAMIC */
103 struct kobj_type of_node_ktype
= {
104 .release
= of_node_release
,
107 static ssize_t
of_node_property_read(struct file
*filp
, struct kobject
*kobj
,
108 struct bin_attribute
*bin_attr
, char *buf
,
109 loff_t offset
, size_t count
)
111 struct property
*pp
= container_of(bin_attr
, struct property
, attr
);
112 return memory_read_from_buffer(buf
, count
, &offset
, pp
->value
, pp
->length
);
115 /* always return newly allocated name, caller must free after use */
116 static const char *safe_name(struct kobject
*kobj
, const char *orig_name
)
118 const char *name
= orig_name
;
119 struct kernfs_node
*kn
;
122 /* don't be a hero. After 16 tries give up */
123 while (i
< 16 && (kn
= sysfs_get_dirent(kobj
->sd
, name
))) {
125 if (name
!= orig_name
)
127 name
= kasprintf(GFP_KERNEL
, "%s#%i", orig_name
, ++i
);
130 if (name
== orig_name
) {
131 name
= kstrdup(orig_name
, GFP_KERNEL
);
133 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
134 kobject_name(kobj
), name
);
139 int __of_add_property_sysfs(struct device_node
*np
, struct property
*pp
)
143 /* Important: Don't leak passwords */
144 bool secure
= strncmp(pp
->name
, "security-", 9) == 0;
146 if (!IS_ENABLED(CONFIG_SYSFS
))
149 if (!of_kset
|| !of_node_is_attached(np
))
152 sysfs_bin_attr_init(&pp
->attr
);
153 pp
->attr
.attr
.name
= safe_name(&np
->kobj
, pp
->name
);
154 pp
->attr
.attr
.mode
= secure
? S_IRUSR
: S_IRUGO
;
155 pp
->attr
.size
= secure
? 0 : pp
->length
;
156 pp
->attr
.read
= of_node_property_read
;
158 rc
= sysfs_create_bin_file(&np
->kobj
, &pp
->attr
);
159 WARN(rc
, "error adding attribute %s to node %s\n", pp
->name
, np
->full_name
);
163 int __of_attach_node_sysfs(struct device_node
*np
)
166 struct kobject
*parent
;
170 if (!IS_ENABLED(CONFIG_SYSFS
))
176 np
->kobj
.kset
= of_kset
;
178 /* Nodes without parents are new top level trees */
179 name
= safe_name(&of_kset
->kobj
, "base");
182 name
= safe_name(&np
->parent
->kobj
, kbasename(np
->full_name
));
183 parent
= &np
->parent
->kobj
;
187 rc
= kobject_add(&np
->kobj
, parent
, "%s", name
);
192 for_each_property_of_node(np
, pp
)
193 __of_add_property_sysfs(np
, pp
);
198 void __init
of_core_init(void)
200 struct device_node
*np
;
202 /* Create the kset, and register existing nodes */
203 mutex_lock(&of_mutex
);
204 of_kset
= kset_create_and_add("devicetree", NULL
, firmware_kobj
);
206 mutex_unlock(&of_mutex
);
207 pr_err("devicetree: failed to register existing nodes\n");
210 for_each_of_allnodes(np
)
211 __of_attach_node_sysfs(np
);
212 mutex_unlock(&of_mutex
);
214 /* Symlink in /proc as required by userspace ABI */
216 proc_symlink("device-tree", NULL
, "/sys/firmware/devicetree/base");
219 static struct property
*__of_find_property(const struct device_node
*np
,
220 const char *name
, int *lenp
)
227 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
228 if (of_prop_cmp(pp
->name
, name
) == 0) {
238 struct property
*of_find_property(const struct device_node
*np
,
245 raw_spin_lock_irqsave(&devtree_lock
, flags
);
246 pp
= __of_find_property(np
, name
, lenp
);
247 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
251 EXPORT_SYMBOL(of_find_property
);
253 struct device_node
*__of_find_all_nodes(struct device_node
*prev
)
255 struct device_node
*np
;
258 } else if (prev
->child
) {
261 /* Walk back up looking for a sibling, or the end of the structure */
263 while (np
->parent
&& !np
->sibling
)
265 np
= np
->sibling
; /* Might be null at the end of the tree */
271 * of_find_all_nodes - Get next node in global list
272 * @prev: Previous node or NULL to start iteration
273 * of_node_put() will be called on it
275 * Returns a node pointer with refcount incremented, use
276 * of_node_put() on it when done.
278 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
280 struct device_node
*np
;
283 raw_spin_lock_irqsave(&devtree_lock
, flags
);
284 np
= __of_find_all_nodes(prev
);
287 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
290 EXPORT_SYMBOL(of_find_all_nodes
);
293 * Find a property with a given name for a given node
294 * and return the value.
296 const void *__of_get_property(const struct device_node
*np
,
297 const char *name
, int *lenp
)
299 struct property
*pp
= __of_find_property(np
, name
, lenp
);
301 return pp
? pp
->value
: NULL
;
305 * Find a property with a given name for a given node
306 * and return the value.
308 const void *of_get_property(const struct device_node
*np
, const char *name
,
311 struct property
*pp
= of_find_property(np
, name
, lenp
);
313 return pp
? pp
->value
: NULL
;
315 EXPORT_SYMBOL(of_get_property
);
318 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
320 * @cpu: logical cpu index of a core/thread
321 * @phys_id: physical identifier of a core/thread
323 * CPU logical to physical index mapping is architecture specific.
324 * However this __weak function provides a default match of physical
325 * id to logical cpu index. phys_id provided here is usually values read
326 * from the device tree which must match the hardware internal registers.
328 * Returns true if the physical identifier and the logical cpu index
329 * correspond to the same core/thread, false otherwise.
331 bool __weak
arch_match_cpu_phys_id(int cpu
, u64 phys_id
)
333 return (u32
)phys_id
== cpu
;
337 * Checks if the given "prop_name" property holds the physical id of the
338 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
339 * NULL, local thread number within the core is returned in it.
341 static bool __of_find_n_match_cpu_property(struct device_node
*cpun
,
342 const char *prop_name
, int cpu
, unsigned int *thread
)
345 int ac
, prop_len
, tid
;
348 ac
= of_n_addr_cells(cpun
);
349 cell
= of_get_property(cpun
, prop_name
, &prop_len
);
352 prop_len
/= sizeof(*cell
) * ac
;
353 for (tid
= 0; tid
< prop_len
; tid
++) {
354 hwid
= of_read_number(cell
, ac
);
355 if (arch_match_cpu_phys_id(cpu
, hwid
)) {
366 * arch_find_n_match_cpu_physical_id - See if the given device node is
367 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
368 * else false. If 'thread' is non-NULL, the local thread number within the
369 * core is returned in it.
371 bool __weak
arch_find_n_match_cpu_physical_id(struct device_node
*cpun
,
372 int cpu
, unsigned int *thread
)
374 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
375 * for thread ids on PowerPC. If it doesn't exist fallback to
376 * standard "reg" property.
378 if (IS_ENABLED(CONFIG_PPC
) &&
379 __of_find_n_match_cpu_property(cpun
,
380 "ibm,ppc-interrupt-server#s",
384 return __of_find_n_match_cpu_property(cpun
, "reg", cpu
, thread
);
388 * of_get_cpu_node - Get device node associated with the given logical CPU
390 * @cpu: CPU number(logical index) for which device node is required
391 * @thread: if not NULL, local thread number within the physical core is
394 * The main purpose of this function is to retrieve the device node for the
395 * given logical CPU index. It should be used to initialize the of_node in
396 * cpu device. Once of_node in cpu device is populated, all the further
397 * references can use that instead.
399 * CPU logical to physical index mapping is architecture specific and is built
400 * before booting secondary cores. This function uses arch_match_cpu_phys_id
401 * which can be overridden by architecture specific implementation.
403 * Returns a node pointer for the logical cpu if found, else NULL.
405 struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
)
407 struct device_node
*cpun
;
409 for_each_node_by_type(cpun
, "cpu") {
410 if (arch_find_n_match_cpu_physical_id(cpun
, cpu
, thread
))
415 EXPORT_SYMBOL(of_get_cpu_node
);
418 * __of_device_is_compatible() - Check if the node matches given constraints
419 * @device: pointer to node
420 * @compat: required compatible string, NULL or "" for any match
421 * @type: required device_type value, NULL or "" for any match
422 * @name: required node name, NULL or "" for any match
424 * Checks if the given @compat, @type and @name strings match the
425 * properties of the given @device. A constraints can be skipped by
426 * passing NULL or an empty string as the constraint.
428 * Returns 0 for no match, and a positive integer on match. The return
429 * value is a relative score with larger values indicating better
430 * matches. The score is weighted for the most specific compatible value
431 * to get the highest score. Matching type is next, followed by matching
432 * name. Practically speaking, this results in the following priority
435 * 1. specific compatible && type && name
436 * 2. specific compatible && type
437 * 3. specific compatible && name
438 * 4. specific compatible
439 * 5. general compatible && type && name
440 * 6. general compatible && type
441 * 7. general compatible && name
442 * 8. general compatible
447 static int __of_device_is_compatible(const struct device_node
*device
,
448 const char *compat
, const char *type
, const char *name
)
450 struct property
*prop
;
452 int index
= 0, score
= 0;
454 /* Compatible match has highest priority */
455 if (compat
&& compat
[0]) {
456 prop
= __of_find_property(device
, "compatible", NULL
);
457 for (cp
= of_prop_next_string(prop
, NULL
); cp
;
458 cp
= of_prop_next_string(prop
, cp
), index
++) {
459 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0) {
460 score
= INT_MAX
/2 - (index
<< 2);
468 /* Matching type is better than matching name */
469 if (type
&& type
[0]) {
470 if (!device
->type
|| of_node_cmp(type
, device
->type
))
475 /* Matching name is a bit better than not */
476 if (name
&& name
[0]) {
477 if (!device
->name
|| of_node_cmp(name
, device
->name
))
485 /** Checks if the given "compat" string matches one of the strings in
486 * the device's "compatible" property
488 int of_device_is_compatible(const struct device_node
*device
,
494 raw_spin_lock_irqsave(&devtree_lock
, flags
);
495 res
= __of_device_is_compatible(device
, compat
, NULL
, NULL
);
496 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
499 EXPORT_SYMBOL(of_device_is_compatible
);
502 * of_machine_is_compatible - Test root of device tree for a given compatible value
503 * @compat: compatible string to look for in root node's compatible property.
505 * Returns a positive integer if the root node has the given value in its
506 * compatible property.
508 int of_machine_is_compatible(const char *compat
)
510 struct device_node
*root
;
513 root
= of_find_node_by_path("/");
515 rc
= of_device_is_compatible(root
, compat
);
520 EXPORT_SYMBOL(of_machine_is_compatible
);
523 * __of_device_is_available - check if a device is available for use
525 * @device: Node to check for availability, with locks already held
527 * Returns true if the status property is absent or set to "okay" or "ok",
530 static bool __of_device_is_available(const struct device_node
*device
)
538 status
= __of_get_property(device
, "status", &statlen
);
543 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
551 * of_device_is_available - check if a device is available for use
553 * @device: Node to check for availability
555 * Returns true if the status property is absent or set to "okay" or "ok",
558 bool of_device_is_available(const struct device_node
*device
)
563 raw_spin_lock_irqsave(&devtree_lock
, flags
);
564 res
= __of_device_is_available(device
);
565 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
569 EXPORT_SYMBOL(of_device_is_available
);
572 * of_device_is_big_endian - check if a device has BE registers
574 * @device: Node to check for endianness
576 * Returns true if the device has a "big-endian" property, or if the kernel
577 * was compiled for BE *and* the device has a "native-endian" property.
578 * Returns false otherwise.
580 * Callers would nominally use ioread32be/iowrite32be if
581 * of_device_is_big_endian() == true, or readl/writel otherwise.
583 bool of_device_is_big_endian(const struct device_node
*device
)
585 if (of_property_read_bool(device
, "big-endian"))
587 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) &&
588 of_property_read_bool(device
, "native-endian"))
592 EXPORT_SYMBOL(of_device_is_big_endian
);
595 * of_get_parent - Get a node's parent if any
596 * @node: Node to get parent
598 * Returns a node pointer with refcount incremented, use
599 * of_node_put() on it when done.
601 struct device_node
*of_get_parent(const struct device_node
*node
)
603 struct device_node
*np
;
609 raw_spin_lock_irqsave(&devtree_lock
, flags
);
610 np
= of_node_get(node
->parent
);
611 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
614 EXPORT_SYMBOL(of_get_parent
);
617 * of_get_next_parent - Iterate to a node's parent
618 * @node: Node to get parent of
620 * This is like of_get_parent() except that it drops the
621 * refcount on the passed node, making it suitable for iterating
622 * through a node's parents.
624 * Returns a node pointer with refcount incremented, use
625 * of_node_put() on it when done.
627 struct device_node
*of_get_next_parent(struct device_node
*node
)
629 struct device_node
*parent
;
635 raw_spin_lock_irqsave(&devtree_lock
, flags
);
636 parent
= of_node_get(node
->parent
);
638 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
641 EXPORT_SYMBOL(of_get_next_parent
);
643 static struct device_node
*__of_get_next_child(const struct device_node
*node
,
644 struct device_node
*prev
)
646 struct device_node
*next
;
651 next
= prev
? prev
->sibling
: node
->child
;
652 for (; next
; next
= next
->sibling
)
653 if (of_node_get(next
))
658 #define __for_each_child_of_node(parent, child) \
659 for (child = __of_get_next_child(parent, NULL); child != NULL; \
660 child = __of_get_next_child(parent, child))
663 * of_get_next_child - Iterate a node childs
665 * @prev: previous child of the parent node, or NULL to get first
667 * Returns a node pointer with refcount incremented, use of_node_put() on
668 * it when done. Returns NULL when prev is the last child. Decrements the
671 struct device_node
*of_get_next_child(const struct device_node
*node
,
672 struct device_node
*prev
)
674 struct device_node
*next
;
677 raw_spin_lock_irqsave(&devtree_lock
, flags
);
678 next
= __of_get_next_child(node
, prev
);
679 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
682 EXPORT_SYMBOL(of_get_next_child
);
685 * of_get_next_available_child - Find the next available child node
687 * @prev: previous child of the parent node, or NULL to get first
689 * This function is like of_get_next_child(), except that it
690 * automatically skips any disabled nodes (i.e. status = "disabled").
692 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
693 struct device_node
*prev
)
695 struct device_node
*next
;
701 raw_spin_lock_irqsave(&devtree_lock
, flags
);
702 next
= prev
? prev
->sibling
: node
->child
;
703 for (; next
; next
= next
->sibling
) {
704 if (!__of_device_is_available(next
))
706 if (of_node_get(next
))
710 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
713 EXPORT_SYMBOL(of_get_next_available_child
);
716 * of_get_child_by_name - Find the child node by name for a given parent
718 * @name: child name to look for.
720 * This function looks for child node for given matching name
722 * Returns a node pointer if found, with refcount incremented, use
723 * of_node_put() on it when done.
724 * Returns NULL if node is not found.
726 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
729 struct device_node
*child
;
731 for_each_child_of_node(node
, child
)
732 if (child
->name
&& (of_node_cmp(child
->name
, name
) == 0))
736 EXPORT_SYMBOL(of_get_child_by_name
);
738 static struct device_node
*__of_find_node_by_path(struct device_node
*parent
,
741 struct device_node
*child
;
744 len
= strcspn(path
, "/:");
748 __for_each_child_of_node(parent
, child
) {
749 const char *name
= strrchr(child
->full_name
, '/');
750 if (WARN(!name
, "malformed device_node %s\n", child
->full_name
))
753 if (strncmp(path
, name
, len
) == 0 && (strlen(name
) == len
))
760 * of_find_node_opts_by_path - Find a node matching a full OF path
761 * @path: Either the full path to match, or if the path does not
762 * start with '/', the name of a property of the /aliases
763 * node (an alias). In the case of an alias, the node
764 * matching the alias' value will be returned.
765 * @opts: Address of a pointer into which to store the start of
766 * an options string appended to the end of the path with
772 * foo/bar Valid alias + relative path
774 * Returns a node pointer with refcount incremented, use
775 * of_node_put() on it when done.
777 struct device_node
*of_find_node_opts_by_path(const char *path
, const char **opts
)
779 struct device_node
*np
= NULL
;
782 const char *separator
= strchr(path
, ':');
785 *opts
= separator
? separator
+ 1 : NULL
;
787 if (strcmp(path
, "/") == 0)
788 return of_node_get(of_root
);
790 /* The path could begin with an alias */
793 const char *p
= separator
;
796 p
= strchrnul(path
, '/');
799 /* of_aliases must not be NULL */
803 for_each_property_of_node(of_aliases
, pp
) {
804 if (strlen(pp
->name
) == len
&& !strncmp(pp
->name
, path
, len
)) {
805 np
= of_find_node_by_path(pp
->value
);
814 /* Step down the tree matching path components */
815 raw_spin_lock_irqsave(&devtree_lock
, flags
);
817 np
= of_node_get(of_root
);
818 while (np
&& *path
== '/') {
819 path
++; /* Increment past '/' delimiter */
820 np
= __of_find_node_by_path(np
, path
);
821 path
= strchrnul(path
, '/');
822 if (separator
&& separator
< path
)
825 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
828 EXPORT_SYMBOL(of_find_node_opts_by_path
);
831 * of_find_node_by_name - Find a node by its "name" property
832 * @from: The node to start searching from or NULL, the node
833 * you pass will not be searched, only the next one
834 * will; typically, you pass what the previous call
835 * returned. of_node_put() will be called on it
836 * @name: The name string to match against
838 * Returns a node pointer with refcount incremented, use
839 * of_node_put() on it when done.
841 struct device_node
*of_find_node_by_name(struct device_node
*from
,
844 struct device_node
*np
;
847 raw_spin_lock_irqsave(&devtree_lock
, flags
);
848 for_each_of_allnodes_from(from
, np
)
849 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
853 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
856 EXPORT_SYMBOL(of_find_node_by_name
);
859 * of_find_node_by_type - Find a node by its "device_type" property
860 * @from: The node to start searching from, or NULL to start searching
861 * the entire device tree. The node you pass will not be
862 * searched, only the next one will; typically, you pass
863 * what the previous call returned. of_node_put() will be
864 * called on from for you.
865 * @type: The type string to match against
867 * Returns a node pointer with refcount incremented, use
868 * of_node_put() on it when done.
870 struct device_node
*of_find_node_by_type(struct device_node
*from
,
873 struct device_node
*np
;
876 raw_spin_lock_irqsave(&devtree_lock
, flags
);
877 for_each_of_allnodes_from(from
, np
)
878 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
882 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
885 EXPORT_SYMBOL(of_find_node_by_type
);
888 * of_find_compatible_node - Find a node based on type and one of the
889 * tokens in its "compatible" property
890 * @from: The node to start searching from or NULL, the node
891 * you pass will not be searched, only the next one
892 * will; typically, you pass what the previous call
893 * returned. of_node_put() will be called on it
894 * @type: The type string to match "device_type" or NULL to ignore
895 * @compatible: The string to match to one of the tokens in the device
898 * Returns a node pointer with refcount incremented, use
899 * of_node_put() on it when done.
901 struct device_node
*of_find_compatible_node(struct device_node
*from
,
902 const char *type
, const char *compatible
)
904 struct device_node
*np
;
907 raw_spin_lock_irqsave(&devtree_lock
, flags
);
908 for_each_of_allnodes_from(from
, np
)
909 if (__of_device_is_compatible(np
, compatible
, type
, NULL
) &&
913 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
916 EXPORT_SYMBOL(of_find_compatible_node
);
919 * of_find_node_with_property - Find a node which has a property with
921 * @from: The node to start searching from or NULL, the node
922 * you pass will not be searched, only the next one
923 * will; typically, you pass what the previous call
924 * returned. of_node_put() will be called on it
925 * @prop_name: The name of the property to look for.
927 * Returns a node pointer with refcount incremented, use
928 * of_node_put() on it when done.
930 struct device_node
*of_find_node_with_property(struct device_node
*from
,
931 const char *prop_name
)
933 struct device_node
*np
;
937 raw_spin_lock_irqsave(&devtree_lock
, flags
);
938 for_each_of_allnodes_from(from
, np
) {
939 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
940 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
948 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
951 EXPORT_SYMBOL(of_find_node_with_property
);
954 const struct of_device_id
*__of_match_node(const struct of_device_id
*matches
,
955 const struct device_node
*node
)
957 const struct of_device_id
*best_match
= NULL
;
958 int score
, best_score
= 0;
963 for (; matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]; matches
++) {
964 score
= __of_device_is_compatible(node
, matches
->compatible
,
965 matches
->type
, matches
->name
);
966 if (score
> best_score
) {
967 best_match
= matches
;
976 * of_match_node - Tell if a device_node has a matching of_match structure
977 * @matches: array of of device match structures to search in
978 * @node: the of device structure to match against
980 * Low level utility function used by device matching.
982 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
983 const struct device_node
*node
)
985 const struct of_device_id
*match
;
988 raw_spin_lock_irqsave(&devtree_lock
, flags
);
989 match
= __of_match_node(matches
, node
);
990 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
993 EXPORT_SYMBOL(of_match_node
);
996 * of_find_matching_node_and_match - Find a node based on an of_device_id
998 * @from: The node to start searching from or NULL, the node
999 * you pass will not be searched, only the next one
1000 * will; typically, you pass what the previous call
1001 * returned. of_node_put() will be called on it
1002 * @matches: array of of device match structures to search in
1003 * @match Updated to point at the matches entry which matched
1005 * Returns a node pointer with refcount incremented, use
1006 * of_node_put() on it when done.
1008 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
1009 const struct of_device_id
*matches
,
1010 const struct of_device_id
**match
)
1012 struct device_node
*np
;
1013 const struct of_device_id
*m
;
1014 unsigned long flags
;
1019 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1020 for_each_of_allnodes_from(from
, np
) {
1021 m
= __of_match_node(matches
, np
);
1022 if (m
&& of_node_get(np
)) {
1029 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1032 EXPORT_SYMBOL(of_find_matching_node_and_match
);
1035 * of_modalias_node - Lookup appropriate modalias for a device node
1036 * @node: pointer to a device tree node
1037 * @modalias: Pointer to buffer that modalias value will be copied into
1038 * @len: Length of modalias value
1040 * Based on the value of the compatible property, this routine will attempt
1041 * to choose an appropriate modalias value for a particular device tree node.
1042 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1043 * from the first entry in the compatible list property.
1045 * This routine returns 0 on success, <0 on failure.
1047 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
1049 const char *compatible
, *p
;
1052 compatible
= of_get_property(node
, "compatible", &cplen
);
1053 if (!compatible
|| strlen(compatible
) > cplen
)
1055 p
= strchr(compatible
, ',');
1056 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
1059 EXPORT_SYMBOL_GPL(of_modalias_node
);
1062 * of_find_node_by_phandle - Find a node given a phandle
1063 * @handle: phandle of the node to find
1065 * Returns a node pointer with refcount incremented, use
1066 * of_node_put() on it when done.
1068 struct device_node
*of_find_node_by_phandle(phandle handle
)
1070 struct device_node
*np
;
1071 unsigned long flags
;
1076 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1077 for_each_of_allnodes(np
)
1078 if (np
->phandle
== handle
)
1081 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1084 EXPORT_SYMBOL(of_find_node_by_phandle
);
1087 * of_property_count_elems_of_size - Count the number of elements in a property
1089 * @np: device node from which the property value is to be read.
1090 * @propname: name of the property to be searched.
1091 * @elem_size: size of the individual element
1093 * Search for a property in a device node and count the number of elements of
1094 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1095 * property does not exist or its length does not match a multiple of elem_size
1096 * and -ENODATA if the property does not have a value.
1098 int of_property_count_elems_of_size(const struct device_node
*np
,
1099 const char *propname
, int elem_size
)
1101 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1108 if (prop
->length
% elem_size
!= 0) {
1109 pr_err("size of %s in node %s is not a multiple of %d\n",
1110 propname
, np
->full_name
, elem_size
);
1114 return prop
->length
/ elem_size
;
1116 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size
);
1119 * of_find_property_value_of_size
1121 * @np: device node from which the property value is to be read.
1122 * @propname: name of the property to be searched.
1123 * @len: requested length of property value
1125 * Search for a property in a device node and valid the requested size.
1126 * Returns the property value on success, -EINVAL if the property does not
1127 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1128 * property data isn't large enough.
1131 static void *of_find_property_value_of_size(const struct device_node
*np
,
1132 const char *propname
, u32 len
)
1134 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1137 return ERR_PTR(-EINVAL
);
1139 return ERR_PTR(-ENODATA
);
1140 if (len
> prop
->length
)
1141 return ERR_PTR(-EOVERFLOW
);
1147 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1149 * @np: device node from which the property value is to be read.
1150 * @propname: name of the property to be searched.
1151 * @index: index of the u32 in the list of values
1152 * @out_value: pointer to return value, modified only if no error.
1154 * Search for a property in a device node and read nth 32-bit value 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 * The out_value is modified only if a valid u32 value can be decoded.
1161 int of_property_read_u32_index(const struct device_node
*np
,
1162 const char *propname
,
1163 u32 index
, u32
*out_value
)
1165 const u32
*val
= of_find_property_value_of_size(np
, propname
,
1166 ((index
+ 1) * sizeof(*out_value
)));
1169 return PTR_ERR(val
);
1171 *out_value
= be32_to_cpup(((__be32
*)val
) + index
);
1174 EXPORT_SYMBOL_GPL(of_property_read_u32_index
);
1177 * of_property_read_u8_array - Find and read an array of u8 from a property.
1179 * @np: device node from which the property value is to be read.
1180 * @propname: name of the property to be searched.
1181 * @out_values: pointer to return value, modified only if return value is 0.
1182 * @sz: number of array elements to read
1184 * Search for a property in a device node and read 8-bit value(s) from
1185 * it. Returns 0 on success, -EINVAL if the property does not exist,
1186 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1187 * property data isn't large enough.
1189 * dts entry of array should be like:
1190 * property = /bits/ 8 <0x50 0x60 0x70>;
1192 * The out_values is modified only if a valid u8 value can be decoded.
1194 int of_property_read_u8_array(const struct device_node
*np
,
1195 const char *propname
, u8
*out_values
, size_t sz
)
1197 const u8
*val
= of_find_property_value_of_size(np
, propname
,
1198 (sz
* sizeof(*out_values
)));
1201 return PTR_ERR(val
);
1204 *out_values
++ = *val
++;
1207 EXPORT_SYMBOL_GPL(of_property_read_u8_array
);
1210 * of_property_read_u16_array - Find and read an array of u16 from a property.
1212 * @np: device node from which the property value is to be read.
1213 * @propname: name of the property to be searched.
1214 * @out_values: pointer to return value, modified only if return value is 0.
1215 * @sz: number of array elements to read
1217 * Search for a property in a device node and read 16-bit value(s) 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 * dts entry of array should be like:
1223 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1225 * The out_values is modified only if a valid u16 value can be decoded.
1227 int of_property_read_u16_array(const struct device_node
*np
,
1228 const char *propname
, u16
*out_values
, size_t sz
)
1230 const __be16
*val
= of_find_property_value_of_size(np
, propname
,
1231 (sz
* sizeof(*out_values
)));
1234 return PTR_ERR(val
);
1237 *out_values
++ = be16_to_cpup(val
++);
1240 EXPORT_SYMBOL_GPL(of_property_read_u16_array
);
1243 * of_property_read_u32_array - Find and read an array of 32 bit integers
1246 * @np: device node from which the property value is to be read.
1247 * @propname: name of the property to be searched.
1248 * @out_values: pointer to return value, modified only if return value is 0.
1249 * @sz: number of array elements to read
1251 * Search for a property in a device node and read 32-bit value(s) from
1252 * it. Returns 0 on success, -EINVAL if the property does not exist,
1253 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1254 * property data isn't large enough.
1256 * The out_values is modified only if a valid u32 value can be decoded.
1258 int of_property_read_u32_array(const struct device_node
*np
,
1259 const char *propname
, u32
*out_values
,
1262 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1263 (sz
* sizeof(*out_values
)));
1266 return PTR_ERR(val
);
1269 *out_values
++ = be32_to_cpup(val
++);
1272 EXPORT_SYMBOL_GPL(of_property_read_u32_array
);
1275 * of_property_read_u64 - Find and read a 64 bit integer from a property
1276 * @np: device node from which the property value is to be read.
1277 * @propname: name of the property to be searched.
1278 * @out_value: pointer to return value, modified only if return value is 0.
1280 * Search for a property in a device node and read a 64-bit value from
1281 * it. Returns 0 on success, -EINVAL if the property does not exist,
1282 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1283 * property data isn't large enough.
1285 * The out_value is modified only if a valid u64 value can be decoded.
1287 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
1290 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1291 sizeof(*out_value
));
1294 return PTR_ERR(val
);
1296 *out_value
= of_read_number(val
, 2);
1299 EXPORT_SYMBOL_GPL(of_property_read_u64
);
1302 * of_property_read_u64_array - Find and read an array of 64 bit integers
1305 * @np: device node from which the property value is to be read.
1306 * @propname: name of the property to be searched.
1307 * @out_values: pointer to return value, modified only if return value is 0.
1308 * @sz: number of array elements to read
1310 * Search for a property in a device node and read 64-bit value(s) from
1311 * it. Returns 0 on success, -EINVAL if the property does not exist,
1312 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1313 * property data isn't large enough.
1315 * The out_values is modified only if a valid u64 value can be decoded.
1317 int of_property_read_u64_array(const struct device_node
*np
,
1318 const char *propname
, u64
*out_values
,
1321 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1322 (sz
* sizeof(*out_values
)));
1325 return PTR_ERR(val
);
1328 *out_values
++ = of_read_number(val
, 2);
1333 EXPORT_SYMBOL_GPL(of_property_read_u64_array
);
1336 * of_property_read_string - Find and read a string from a property
1337 * @np: device node from which the property value is to be read.
1338 * @propname: name of the property to be searched.
1339 * @out_string: pointer to null terminated return string, modified only if
1340 * return value is 0.
1342 * Search for a property in a device tree node and retrieve a null
1343 * terminated string value (pointer to data, not a copy). Returns 0 on
1344 * success, -EINVAL if the property does not exist, -ENODATA if property
1345 * does not have a value, and -EILSEQ if the string is not null-terminated
1346 * within the length of the property data.
1348 * The out_string pointer is modified only if a valid string can be decoded.
1350 int of_property_read_string(struct device_node
*np
, const char *propname
,
1351 const char **out_string
)
1353 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1358 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
1360 *out_string
= prop
->value
;
1363 EXPORT_SYMBOL_GPL(of_property_read_string
);
1366 * of_property_match_string() - Find string in a list and return index
1367 * @np: pointer to node containing string list property
1368 * @propname: string list property name
1369 * @string: pointer to string to search for in string list
1371 * This function searches a string list property and returns the index
1372 * of a specific string value.
1374 int of_property_match_string(struct device_node
*np
, const char *propname
,
1377 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1380 const char *p
, *end
;
1388 end
= p
+ prop
->length
;
1390 for (i
= 0; p
< end
; i
++, p
+= l
) {
1391 l
= strnlen(p
, end
- p
) + 1;
1394 pr_debug("comparing %s with %s\n", string
, p
);
1395 if (strcmp(string
, p
) == 0)
1396 return i
; /* Found it; return index */
1400 EXPORT_SYMBOL_GPL(of_property_match_string
);
1403 * of_property_read_string_helper() - Utility helper for parsing string properties
1404 * @np: device node from which the property value is to be read.
1405 * @propname: name of the property to be searched.
1406 * @out_strs: output array of string pointers.
1407 * @sz: number of array elements to read.
1408 * @skip: Number of strings to skip over at beginning of list.
1410 * Don't call this function directly. It is a utility helper for the
1411 * of_property_read_string*() family of functions.
1413 int of_property_read_string_helper(struct device_node
*np
, const char *propname
,
1414 const char **out_strs
, size_t sz
, int skip
)
1416 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1418 const char *p
, *end
;
1425 end
= p
+ prop
->length
;
1427 for (i
= 0; p
< end
&& (!out_strs
|| i
< skip
+ sz
); i
++, p
+= l
) {
1428 l
= strnlen(p
, end
- p
) + 1;
1431 if (out_strs
&& i
>= skip
)
1435 return i
<= 0 ? -ENODATA
: i
;
1437 EXPORT_SYMBOL_GPL(of_property_read_string_helper
);
1439 void of_print_phandle_args(const char *msg
, const struct of_phandle_args
*args
)
1442 printk("%s %s", msg
, of_node_full_name(args
->np
));
1443 for (i
= 0; i
< args
->args_count
; i
++)
1444 printk(i
? ",%08x" : ":%08x", args
->args
[i
]);
1448 static int __of_parse_phandle_with_args(const struct device_node
*np
,
1449 const char *list_name
,
1450 const char *cells_name
,
1451 int cell_count
, int index
,
1452 struct of_phandle_args
*out_args
)
1454 const __be32
*list
, *list_end
;
1455 int rc
= 0, size
, cur_index
= 0;
1457 struct device_node
*node
= NULL
;
1460 /* Retrieve the phandle list property */
1461 list
= of_get_property(np
, list_name
, &size
);
1464 list_end
= list
+ size
/ sizeof(*list
);
1466 /* Loop over the phandles until all the requested entry is found */
1467 while (list
< list_end
) {
1472 * If phandle is 0, then it is an empty entry with no
1473 * arguments. Skip forward to the next entry.
1475 phandle
= be32_to_cpup(list
++);
1478 * Find the provider node and parse the #*-cells
1479 * property to determine the argument length.
1481 * This is not needed if the cell count is hard-coded
1482 * (i.e. cells_name not set, but cell_count is set),
1483 * except when we're going to return the found node
1486 if (cells_name
|| cur_index
== index
) {
1487 node
= of_find_node_by_phandle(phandle
);
1489 pr_err("%s: could not find phandle\n",
1496 if (of_property_read_u32(node
, cells_name
,
1498 pr_err("%s: could not get %s for %s\n",
1499 np
->full_name
, cells_name
,
1508 * Make sure that the arguments actually fit in the
1509 * remaining property data length
1511 if (list
+ count
> list_end
) {
1512 pr_err("%s: arguments longer than property\n",
1519 * All of the error cases above bail out of the loop, so at
1520 * this point, the parsing is successful. If the requested
1521 * index matches, then fill the out_args structure and return,
1522 * or return -ENOENT for an empty entry.
1525 if (cur_index
== index
) {
1531 if (WARN_ON(count
> MAX_PHANDLE_ARGS
))
1532 count
= MAX_PHANDLE_ARGS
;
1533 out_args
->np
= node
;
1534 out_args
->args_count
= count
;
1535 for (i
= 0; i
< count
; i
++)
1536 out_args
->args
[i
] = be32_to_cpup(list
++);
1541 /* Found it! return success */
1552 * Unlock node before returning result; will be one of:
1553 * -ENOENT : index is for empty phandle
1554 * -EINVAL : parsing error on data
1555 * [1..n] : Number of phandle (count mode; when index = -1)
1557 rc
= index
< 0 ? cur_index
: -ENOENT
;
1565 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1566 * @np: Pointer to device node holding phandle property
1567 * @phandle_name: Name of property holding a phandle value
1568 * @index: For properties holding a table of phandles, this is the index into
1571 * Returns the device_node pointer with refcount incremented. Use
1572 * of_node_put() on it when done.
1574 struct device_node
*of_parse_phandle(const struct device_node
*np
,
1575 const char *phandle_name
, int index
)
1577 struct of_phandle_args args
;
1582 if (__of_parse_phandle_with_args(np
, phandle_name
, NULL
, 0,
1588 EXPORT_SYMBOL(of_parse_phandle
);
1591 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1592 * @np: pointer to a device tree node containing a list
1593 * @list_name: property name that contains a list
1594 * @cells_name: property name that specifies phandles' arguments count
1595 * @index: index of a phandle to parse out
1596 * @out_args: optional pointer to output arguments structure (will be filled)
1598 * This function is useful to parse lists of phandles and their arguments.
1599 * Returns 0 on success and fills out_args, on error returns appropriate
1602 * Caller is responsible to call of_node_put() on the returned out_args->np
1608 * #list-cells = <2>;
1612 * #list-cells = <1>;
1616 * list = <&phandle1 1 2 &phandle2 3>;
1619 * To get a device_node of the `node2' node you may call this:
1620 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1622 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1623 const char *cells_name
, int index
,
1624 struct of_phandle_args
*out_args
)
1628 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, 0,
1631 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1634 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1635 * @np: pointer to a device tree node containing a list
1636 * @list_name: property name that contains a list
1637 * @cell_count: number of argument cells following the phandle
1638 * @index: index of a phandle to parse out
1639 * @out_args: optional pointer to output arguments structure (will be filled)
1641 * This function is useful to parse lists of phandles and their arguments.
1642 * Returns 0 on success and fills out_args, on error returns appropriate
1645 * Caller is responsible to call of_node_put() on the returned out_args->np
1657 * list = <&phandle1 0 2 &phandle2 2 3>;
1660 * To get a device_node of the `node2' node you may call this:
1661 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1663 int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
1664 const char *list_name
, int cell_count
,
1665 int index
, struct of_phandle_args
*out_args
)
1669 return __of_parse_phandle_with_args(np
, list_name
, NULL
, cell_count
,
1672 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args
);
1675 * of_count_phandle_with_args() - Find the number of phandles references in a property
1676 * @np: pointer to a device tree node containing a list
1677 * @list_name: property name that contains a list
1678 * @cells_name: property name that specifies phandles' arguments count
1680 * Returns the number of phandle + argument tuples within a property. It
1681 * is a typical pattern to encode a list of phandle and variable
1682 * arguments into a single property. The number of arguments is encoded
1683 * by a property in the phandle-target node. For example, a gpios
1684 * property would contain a list of GPIO specifies consisting of a
1685 * phandle and 1 or more arguments. The number of arguments are
1686 * determined by the #gpio-cells property in the node pointed to by the
1689 int of_count_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1690 const char *cells_name
)
1692 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, 0, -1,
1695 EXPORT_SYMBOL(of_count_phandle_with_args
);
1698 * __of_add_property - Add a property to a node without lock operations
1700 int __of_add_property(struct device_node
*np
, struct property
*prop
)
1702 struct property
**next
;
1705 next
= &np
->properties
;
1707 if (strcmp(prop
->name
, (*next
)->name
) == 0)
1708 /* duplicate ! don't insert it */
1711 next
= &(*next
)->next
;
1719 * of_add_property - Add a property to a node
1721 int of_add_property(struct device_node
*np
, struct property
*prop
)
1723 unsigned long flags
;
1726 mutex_lock(&of_mutex
);
1728 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1729 rc
= __of_add_property(np
, prop
);
1730 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1733 __of_add_property_sysfs(np
, prop
);
1735 mutex_unlock(&of_mutex
);
1738 of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
, NULL
);
1743 int __of_remove_property(struct device_node
*np
, struct property
*prop
)
1745 struct property
**next
;
1747 for (next
= &np
->properties
; *next
; next
= &(*next
)->next
) {
1754 /* found the node */
1756 prop
->next
= np
->deadprops
;
1757 np
->deadprops
= prop
;
1762 void __of_sysfs_remove_bin_file(struct device_node
*np
, struct property
*prop
)
1764 sysfs_remove_bin_file(&np
->kobj
, &prop
->attr
);
1765 kfree(prop
->attr
.attr
.name
);
1768 void __of_remove_property_sysfs(struct device_node
*np
, struct property
*prop
)
1770 if (!IS_ENABLED(CONFIG_SYSFS
))
1773 /* at early boot, bail here and defer setup to of_init() */
1774 if (of_kset
&& of_node_is_attached(np
))
1775 __of_sysfs_remove_bin_file(np
, prop
);
1779 * of_remove_property - Remove a property from a node.
1781 * Note that we don't actually remove it, since we have given out
1782 * who-knows-how-many pointers to the data using get-property.
1783 * Instead we just move the property to the "dead properties"
1784 * list, so it won't be found any more.
1786 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1788 unsigned long flags
;
1791 mutex_lock(&of_mutex
);
1793 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1794 rc
= __of_remove_property(np
, prop
);
1795 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1798 __of_remove_property_sysfs(np
, prop
);
1800 mutex_unlock(&of_mutex
);
1803 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
, NULL
);
1808 int __of_update_property(struct device_node
*np
, struct property
*newprop
,
1809 struct property
**oldpropp
)
1811 struct property
**next
, *oldprop
;
1813 for (next
= &np
->properties
; *next
; next
= &(*next
)->next
) {
1814 if (of_prop_cmp((*next
)->name
, newprop
->name
) == 0)
1817 *oldpropp
= oldprop
= *next
;
1820 /* replace the node */
1821 newprop
->next
= oldprop
->next
;
1823 oldprop
->next
= np
->deadprops
;
1824 np
->deadprops
= oldprop
;
1827 newprop
->next
= NULL
;
1834 void __of_update_property_sysfs(struct device_node
*np
, struct property
*newprop
,
1835 struct property
*oldprop
)
1837 if (!IS_ENABLED(CONFIG_SYSFS
))
1840 /* At early boot, bail out and defer setup to of_init() */
1845 __of_sysfs_remove_bin_file(np
, oldprop
);
1846 __of_add_property_sysfs(np
, newprop
);
1850 * of_update_property - Update a property in a node, if the property does
1851 * not exist, add it.
1853 * Note that we don't actually remove it, since we have given out
1854 * who-knows-how-many pointers to the data using get-property.
1855 * Instead we just move the property to the "dead properties" list,
1856 * and add the new property to the property list
1858 int of_update_property(struct device_node
*np
, struct property
*newprop
)
1860 struct property
*oldprop
;
1861 unsigned long flags
;
1867 mutex_lock(&of_mutex
);
1869 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1870 rc
= __of_update_property(np
, newprop
, &oldprop
);
1871 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1874 __of_update_property_sysfs(np
, newprop
, oldprop
);
1876 mutex_unlock(&of_mutex
);
1879 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
, oldprop
);
1884 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
1885 int id
, const char *stem
, int stem_len
)
1889 strncpy(ap
->stem
, stem
, stem_len
);
1890 ap
->stem
[stem_len
] = 0;
1891 list_add_tail(&ap
->link
, &aliases_lookup
);
1892 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1893 ap
->alias
, ap
->stem
, ap
->id
, of_node_full_name(np
));
1897 * of_alias_scan - Scan all properties of the 'aliases' node
1899 * The function scans all the properties of the 'aliases' node and populates
1900 * the global lookup table with the properties. It returns the
1901 * number of alias properties found, or an error code in case of failure.
1903 * @dt_alloc: An allocator that provides a virtual address to memory
1904 * for storing the resulting tree
1906 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
1908 struct property
*pp
;
1910 of_aliases
= of_find_node_by_path("/aliases");
1911 of_chosen
= of_find_node_by_path("/chosen");
1912 if (of_chosen
== NULL
)
1913 of_chosen
= of_find_node_by_path("/chosen@0");
1916 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
1917 const char *name
= of_get_property(of_chosen
, "stdout-path", NULL
);
1919 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
1920 if (IS_ENABLED(CONFIG_PPC
) && !name
)
1921 name
= of_get_property(of_aliases
, "stdout", NULL
);
1923 of_stdout
= of_find_node_opts_by_path(name
, &of_stdout_options
);
1929 for_each_property_of_node(of_aliases
, pp
) {
1930 const char *start
= pp
->name
;
1931 const char *end
= start
+ strlen(start
);
1932 struct device_node
*np
;
1933 struct alias_prop
*ap
;
1936 /* Skip those we do not want to proceed */
1937 if (!strcmp(pp
->name
, "name") ||
1938 !strcmp(pp
->name
, "phandle") ||
1939 !strcmp(pp
->name
, "linux,phandle"))
1942 np
= of_find_node_by_path(pp
->value
);
1946 /* walk the alias backwards to extract the id and work out
1947 * the 'stem' string */
1948 while (isdigit(*(end
-1)) && end
> start
)
1952 if (kstrtoint(end
, 10, &id
) < 0)
1955 /* Allocate an alias_prop with enough space for the stem */
1956 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, 4);
1959 memset(ap
, 0, sizeof(*ap
) + len
+ 1);
1961 of_alias_add(ap
, np
, id
, start
, len
);
1966 * of_alias_get_id - Get alias id for the given device_node
1967 * @np: Pointer to the given device_node
1968 * @stem: Alias stem of the given device_node
1970 * The function travels the lookup table to get the alias id for the given
1971 * device_node and alias stem. It returns the alias id if found.
1973 int of_alias_get_id(struct device_node
*np
, const char *stem
)
1975 struct alias_prop
*app
;
1978 mutex_lock(&of_mutex
);
1979 list_for_each_entry(app
, &aliases_lookup
, link
) {
1980 if (strcmp(app
->stem
, stem
) != 0)
1983 if (np
== app
->np
) {
1988 mutex_unlock(&of_mutex
);
1992 EXPORT_SYMBOL_GPL(of_alias_get_id
);
1995 * of_alias_get_highest_id - Get highest alias id for the given stem
1996 * @stem: Alias stem to be examined
1998 * The function travels the lookup table to get the highest alias id for the
1999 * given alias stem. It returns the alias id if found.
2001 int of_alias_get_highest_id(const char *stem
)
2003 struct alias_prop
*app
;
2006 mutex_lock(&of_mutex
);
2007 list_for_each_entry(app
, &aliases_lookup
, link
) {
2008 if (strcmp(app
->stem
, stem
) != 0)
2014 mutex_unlock(&of_mutex
);
2018 EXPORT_SYMBOL_GPL(of_alias_get_highest_id
);
2020 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
2023 const void *curv
= cur
;
2033 curv
+= sizeof(*cur
);
2034 if (curv
>= prop
->value
+ prop
->length
)
2038 *pu
= be32_to_cpup(curv
);
2041 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
2043 const char *of_prop_next_string(struct property
*prop
, const char *cur
)
2045 const void *curv
= cur
;
2053 curv
+= strlen(cur
) + 1;
2054 if (curv
>= prop
->value
+ prop
->length
)
2059 EXPORT_SYMBOL_GPL(of_prop_next_string
);
2062 * of_console_check() - Test and setup console for DT setup
2063 * @dn - Pointer to device node
2064 * @name - Name to use for preferred console without index. ex. "ttyS"
2065 * @index - Index to use for preferred console.
2067 * Check if the given device node matches the stdout-path property in the
2068 * /chosen node. If it does then register it as the preferred console and return
2069 * TRUE. Otherwise return FALSE.
2071 bool of_console_check(struct device_node
*dn
, char *name
, int index
)
2073 if (!dn
|| dn
!= of_stdout
|| console_set_on_cmdline
)
2075 return !add_preferred_console(name
, index
,
2076 kstrdup(of_stdout_options
, GFP_KERNEL
));
2078 EXPORT_SYMBOL_GPL(of_console_check
);
2081 * of_find_next_cache_node - Find a node's subsidiary cache
2082 * @np: node of type "cpu" or "cache"
2084 * Returns a node pointer with refcount incremented, use
2085 * of_node_put() on it when done. Caller should hold a reference
2088 struct device_node
*of_find_next_cache_node(const struct device_node
*np
)
2090 struct device_node
*child
;
2091 const phandle
*handle
;
2093 handle
= of_get_property(np
, "l2-cache", NULL
);
2095 handle
= of_get_property(np
, "next-level-cache", NULL
);
2098 return of_find_node_by_phandle(be32_to_cpup(handle
));
2100 /* OF on pmac has nodes instead of properties named "l2-cache"
2101 * beneath CPU nodes.
2103 if (!strcmp(np
->type
, "cpu"))
2104 for_each_child_of_node(np
, child
)
2105 if (!strcmp(child
->type
, "cache"))
2112 * of_graph_parse_endpoint() - parse common endpoint node properties
2113 * @node: pointer to endpoint device_node
2114 * @endpoint: pointer to the OF endpoint data structure
2116 * The caller should hold a reference to @node.
2118 int of_graph_parse_endpoint(const struct device_node
*node
,
2119 struct of_endpoint
*endpoint
)
2121 struct device_node
*port_node
= of_get_parent(node
);
2123 WARN_ONCE(!port_node
, "%s(): endpoint %s has no parent node\n",
2124 __func__
, node
->full_name
);
2126 memset(endpoint
, 0, sizeof(*endpoint
));
2128 endpoint
->local_node
= node
;
2130 * It doesn't matter whether the two calls below succeed.
2131 * If they don't then the default value 0 is used.
2133 of_property_read_u32(port_node
, "reg", &endpoint
->port
);
2134 of_property_read_u32(node
, "reg", &endpoint
->id
);
2136 of_node_put(port_node
);
2140 EXPORT_SYMBOL(of_graph_parse_endpoint
);
2143 * of_graph_get_port_by_id() - get the port matching a given id
2144 * @parent: pointer to the parent device node
2145 * @id: id of the port
2147 * Return: A 'port' node pointer with refcount incremented. The caller
2148 * has to use of_node_put() on it when done.
2150 struct device_node
*of_graph_get_port_by_id(struct device_node
*parent
, u32 id
)
2152 struct device_node
*node
, *port
;
2154 node
= of_get_child_by_name(parent
, "ports");
2158 for_each_child_of_node(parent
, port
) {
2161 if (of_node_cmp(port
->name
, "port") != 0)
2163 of_property_read_u32(port
, "reg", &port_id
);
2172 EXPORT_SYMBOL(of_graph_get_port_by_id
);
2175 * of_graph_get_next_endpoint() - get next endpoint node
2176 * @parent: pointer to the parent device node
2177 * @prev: previous endpoint node, or NULL to get first
2179 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2180 * of the passed @prev node is decremented.
2182 struct device_node
*of_graph_get_next_endpoint(const struct device_node
*parent
,
2183 struct device_node
*prev
)
2185 struct device_node
*endpoint
;
2186 struct device_node
*port
;
2192 * Start by locating the port node. If no previous endpoint is specified
2193 * search for the first port node, otherwise get the previous endpoint
2197 struct device_node
*node
;
2199 node
= of_get_child_by_name(parent
, "ports");
2203 port
= of_get_child_by_name(parent
, "port");
2207 pr_err("%s(): no port node found in %s\n",
2208 __func__
, parent
->full_name
);
2212 port
= of_get_parent(prev
);
2213 if (WARN_ONCE(!port
, "%s(): endpoint %s has no parent node\n",
2214 __func__
, prev
->full_name
))
2220 * Now that we have a port node, get the next endpoint by
2221 * getting the next child. If the previous endpoint is NULL this
2222 * will return the first child.
2224 endpoint
= of_get_next_child(port
, prev
);
2230 /* No more endpoints under this port, try the next one. */
2234 port
= of_get_next_child(parent
, port
);
2237 } while (of_node_cmp(port
->name
, "port"));
2240 EXPORT_SYMBOL(of_graph_get_next_endpoint
);
2243 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
2244 * @parent: pointer to the parent device node
2245 * @port_reg: identifier (value of reg property) of the parent port node
2246 * @reg: identifier (value of reg property) of the endpoint node
2248 * Return: An 'endpoint' node pointer which is identified by reg and at the same
2249 * is the child of a port node identified by port_reg. reg and port_reg are
2250 * ignored when they are -1.
2252 struct device_node
*of_graph_get_endpoint_by_regs(
2253 const struct device_node
*parent
, int port_reg
, int reg
)
2255 struct of_endpoint endpoint
;
2256 struct device_node
*node
= NULL
;
2258 for_each_endpoint_of_node(parent
, node
) {
2259 of_graph_parse_endpoint(node
, &endpoint
);
2260 if (((port_reg
== -1) || (endpoint
.port
== port_reg
)) &&
2261 ((reg
== -1) || (endpoint
.id
== reg
)))
2267 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs
);
2270 * of_graph_get_remote_port_parent() - get remote port's parent node
2271 * @node: pointer to a local endpoint device_node
2273 * Return: Remote device node associated with remote endpoint node linked
2274 * to @node. Use of_node_put() on it when done.
2276 struct device_node
*of_graph_get_remote_port_parent(
2277 const struct device_node
*node
)
2279 struct device_node
*np
;
2282 /* Get remote endpoint node. */
2283 np
= of_parse_phandle(node
, "remote-endpoint", 0);
2285 /* Walk 3 levels up only if there is 'ports' node. */
2286 for (depth
= 3; depth
&& np
; depth
--) {
2287 np
= of_get_next_parent(np
);
2288 if (depth
== 2 && of_node_cmp(np
->name
, "ports"))
2293 EXPORT_SYMBOL(of_graph_get_remote_port_parent
);
2296 * of_graph_get_remote_port() - get remote port node
2297 * @node: pointer to a local endpoint device_node
2299 * Return: Remote port node associated with remote endpoint node linked
2300 * to @node. Use of_node_put() on it when done.
2302 struct device_node
*of_graph_get_remote_port(const struct device_node
*node
)
2304 struct device_node
*np
;
2306 /* Get remote endpoint node. */
2307 np
= of_parse_phandle(node
, "remote-endpoint", 0);
2310 return of_get_next_parent(np
);
2312 EXPORT_SYMBOL(of_graph_get_remote_port
);