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/of_graph.h>
25 #include <linux/spinlock.h>
26 #include <linux/slab.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 DEFINE_MUTEX(of_aliases_mutex);
41 /* use when traversing tree through the allnext, child, sibling,
42 * or parent members of struct device_node.
44 DEFINE_RAW_SPINLOCK(devtree_lock);
46 int of_n_addr_cells(struct device_node *np)
53 ip = of_get_property(np, "#address-cells", NULL);
55 return be32_to_cpup(ip);
57 /* No #address-cells property for the root node */
58 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
60 EXPORT_SYMBOL(of_n_addr_cells);
62 int of_n_size_cells(struct device_node *np)
69 ip = of_get_property(np, "#size-cells", NULL);
71 return be32_to_cpup(ip);
73 /* No #size-cells property for the root node */
74 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
76 EXPORT_SYMBOL(of_n_size_cells);
78 #if defined(CONFIG_OF_DYNAMIC)
80 * of_node_get - Increment refcount of a node
81 * @node: Node to inc refcount, NULL is supported to
82 * simplify writing of callers
86 struct device_node *of_node_get(struct device_node *node)
89 kref_get(&node->kref);
92 EXPORT_SYMBOL(of_node_get);
94 static inline struct device_node *kref_to_device_node(struct kref *kref)
96 return container_of(kref, struct device_node, kref);
100 * of_node_release - release a dynamically allocated node
101 * @kref: kref element of the node to be released
103 * In of_node_put() this function is passed to kref_put()
106 static void of_node_release(struct kref *kref)
108 struct device_node *node = kref_to_device_node(kref);
109 struct property *prop = node->properties;
111 /* We should never be releasing nodes that haven't been detached. */
112 if (!of_node_check_flag(node, OF_DETACHED)) {
113 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
115 kref_init(&node->kref);
119 if (!of_node_check_flag(node, OF_DYNAMIC))
123 struct property *next = prop->next;
130 prop = node->deadprops;
131 node->deadprops = NULL;
134 kfree(node->full_name);
140 * of_node_put - Decrement refcount of a node
141 * @node: Node to dec refcount, NULL is supported to
142 * simplify writing of callers
145 void of_node_put(struct device_node *node)
148 kref_put(&node->kref, of_node_release);
150 EXPORT_SYMBOL(of_node_put);
151 #endif /* CONFIG_OF_DYNAMIC */
153 static struct property *__of_find_property(const struct device_node *np,
154 const char *name, int *lenp)
161 for (pp = np->properties; pp; pp = pp->next) {
162 if (of_prop_cmp(pp->name, name) == 0) {
172 struct property *of_find_property(const struct device_node *np,
179 raw_spin_lock_irqsave(&devtree_lock, flags);
180 pp = __of_find_property(np, name, lenp);
181 raw_spin_unlock_irqrestore(&devtree_lock, flags);
185 EXPORT_SYMBOL(of_find_property);
188 * of_find_all_nodes - Get next node in global list
189 * @prev: Previous node or NULL to start iteration
190 * of_node_put() will be called on it
192 * Returns a node pointer with refcount incremented, use
193 * of_node_put() on it when done.
195 struct device_node *of_find_all_nodes(struct device_node *prev)
197 struct device_node *np;
200 raw_spin_lock_irqsave(&devtree_lock, flags);
201 np = prev ? prev->allnext : of_allnodes;
202 for (; np != NULL; np = np->allnext)
206 raw_spin_unlock_irqrestore(&devtree_lock, flags);
209 EXPORT_SYMBOL(of_find_all_nodes);
212 * Find a property with a given name for a given node
213 * and return the value.
215 static const void *__of_get_property(const struct device_node *np,
216 const char *name, int *lenp)
218 struct property *pp = __of_find_property(np, name, lenp);
220 return pp ? pp->value : NULL;
224 * Find a property with a given name for a given node
225 * and return the value.
227 const void *of_get_property(const struct device_node *np, const char *name,
230 struct property *pp = of_find_property(np, name, lenp);
232 return pp ? pp->value : NULL;
234 EXPORT_SYMBOL(of_get_property);
237 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
239 * @cpu: logical cpu index of a core/thread
240 * @phys_id: physical identifier of a core/thread
242 * CPU logical to physical index mapping is architecture specific.
243 * However this __weak function provides a default match of physical
244 * id to logical cpu index. phys_id provided here is usually values read
245 * from the device tree which must match the hardware internal registers.
247 * Returns true if the physical identifier and the logical cpu index
248 * correspond to the same core/thread, false otherwise.
250 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
252 return (u32)phys_id == cpu;
256 * Checks if the given "prop_name" property holds the physical id of the
257 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
258 * NULL, local thread number within the core is returned in it.
260 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
261 const char *prop_name, int cpu, unsigned int *thread)
264 int ac, prop_len, tid;
267 ac = of_n_addr_cells(cpun);
268 cell = of_get_property(cpun, prop_name, &prop_len);
271 prop_len /= sizeof(*cell);
272 for (tid = 0; tid < prop_len; tid++) {
273 hwid = of_read_number(cell, ac);
274 if (arch_match_cpu_phys_id(cpu, hwid)) {
285 * of_get_cpu_node - Get device node associated with the given logical CPU
287 * @cpu: CPU number(logical index) for which device node is required
288 * @thread: if not NULL, local thread number within the physical core is
291 * The main purpose of this function is to retrieve the device node for the
292 * given logical CPU index. It should be used to initialize the of_node in
293 * cpu device. Once of_node in cpu device is populated, all the further
294 * references can use that instead.
296 * CPU logical to physical index mapping is architecture specific and is built
297 * before booting secondary cores. This function uses arch_match_cpu_phys_id
298 * which can be overridden by architecture specific implementation.
300 * Returns a node pointer for the logical cpu if found, else NULL.
302 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
304 struct device_node *cpun, *cpus;
306 cpus = of_find_node_by_path("/cpus");
308 pr_warn("Missing cpus node, bailing out\n");
312 for_each_child_of_node(cpus, cpun) {
313 if (of_node_cmp(cpun->type, "cpu"))
315 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
316 * for thread ids on PowerPC. If it doesn't exist fallback to
317 * standard "reg" property.
319 if (IS_ENABLED(CONFIG_PPC) &&
320 __of_find_n_match_cpu_property(cpun,
321 "ibm,ppc-interrupt-server#s", cpu, thread))
323 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
328 EXPORT_SYMBOL(of_get_cpu_node);
330 /** Checks if the given "compat" string matches one of the strings in
331 * the device's "compatible" property
333 static int __of_device_is_compatible(const struct device_node *device,
339 cp = __of_get_property(device, "compatible", &cplen);
343 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
353 /** Checks if the given "compat" string matches one of the strings in
354 * the device's "compatible" property
356 int of_device_is_compatible(const struct device_node *device,
362 raw_spin_lock_irqsave(&devtree_lock, flags);
363 res = __of_device_is_compatible(device, compat);
364 raw_spin_unlock_irqrestore(&devtree_lock, flags);
367 EXPORT_SYMBOL(of_device_is_compatible);
370 * of_machine_is_compatible - Test root of device tree for a given compatible value
371 * @compat: compatible string to look for in root node's compatible property.
373 * Returns true if the root node has the given value in its
374 * compatible property.
376 int of_machine_is_compatible(const char *compat)
378 struct device_node *root;
381 root = of_find_node_by_path("/");
383 rc = of_device_is_compatible(root, compat);
388 EXPORT_SYMBOL(of_machine_is_compatible);
391 * __of_device_is_available - check if a device is available for use
393 * @device: Node to check for availability, with locks already held
395 * Returns 1 if the status property is absent or set to "okay" or "ok",
398 static int __of_device_is_available(const struct device_node *device)
403 status = __of_get_property(device, "status", &statlen);
408 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
416 * of_device_is_available - check if a device is available for use
418 * @device: Node to check for availability
420 * Returns 1 if the status property is absent or set to "okay" or "ok",
423 int of_device_is_available(const struct device_node *device)
428 raw_spin_lock_irqsave(&devtree_lock, flags);
429 res = __of_device_is_available(device);
430 raw_spin_unlock_irqrestore(&devtree_lock, flags);
434 EXPORT_SYMBOL(of_device_is_available);
437 * of_get_parent - Get a node's parent if any
438 * @node: Node to get parent
440 * Returns a node pointer with refcount incremented, use
441 * of_node_put() on it when done.
443 struct device_node *of_get_parent(const struct device_node *node)
445 struct device_node *np;
451 raw_spin_lock_irqsave(&devtree_lock, flags);
452 np = of_node_get(node->parent);
453 raw_spin_unlock_irqrestore(&devtree_lock, flags);
456 EXPORT_SYMBOL(of_get_parent);
459 * of_get_next_parent - Iterate to a node's parent
460 * @node: Node to get parent of
462 * This is like of_get_parent() except that it drops the
463 * refcount on the passed node, making it suitable for iterating
464 * through a node's parents.
466 * Returns a node pointer with refcount incremented, use
467 * of_node_put() on it when done.
469 struct device_node *of_get_next_parent(struct device_node *node)
471 struct device_node *parent;
477 raw_spin_lock_irqsave(&devtree_lock, flags);
478 parent = of_node_get(node->parent);
480 raw_spin_unlock_irqrestore(&devtree_lock, flags);
483 EXPORT_SYMBOL(of_get_next_parent);
486 * of_get_next_child - Iterate a node childs
488 * @prev: previous child of the parent node, or NULL to get first
490 * Returns a node pointer with refcount incremented, use
491 * of_node_put() on it when done.
493 struct device_node *of_get_next_child(const struct device_node *node,
494 struct device_node *prev)
496 struct device_node *next;
499 raw_spin_lock_irqsave(&devtree_lock, flags);
500 next = prev ? prev->sibling : node->child;
501 for (; next; next = next->sibling)
502 if (of_node_get(next))
505 raw_spin_unlock_irqrestore(&devtree_lock, flags);
508 EXPORT_SYMBOL(of_get_next_child);
511 * of_get_next_available_child - Find the next available child node
513 * @prev: previous child of the parent node, or NULL to get first
515 * This function is like of_get_next_child(), except that it
516 * automatically skips any disabled nodes (i.e. status = "disabled").
518 struct device_node *of_get_next_available_child(const struct device_node *node,
519 struct device_node *prev)
521 struct device_node *next;
524 raw_spin_lock_irqsave(&devtree_lock, flags);
525 next = prev ? prev->sibling : node->child;
526 for (; next; next = next->sibling) {
527 if (!__of_device_is_available(next))
529 if (of_node_get(next))
533 raw_spin_unlock_irqrestore(&devtree_lock, flags);
536 EXPORT_SYMBOL(of_get_next_available_child);
539 * of_get_child_by_name - Find the child node by name for a given parent
541 * @name: child name to look for.
543 * This function looks for child node for given matching name
545 * Returns a node pointer if found, with refcount incremented, use
546 * of_node_put() on it when done.
547 * Returns NULL if node is not found.
549 struct device_node *of_get_child_by_name(const struct device_node *node,
552 struct device_node *child;
554 for_each_child_of_node(node, child)
555 if (child->name && (of_node_cmp(child->name, name) == 0))
559 EXPORT_SYMBOL(of_get_child_by_name);
562 * of_find_node_by_path - Find a node matching a full OF path
563 * @path: The full path to match
565 * Returns a node pointer with refcount incremented, use
566 * of_node_put() on it when done.
568 struct device_node *of_find_node_by_path(const char *path)
570 struct device_node *np = of_allnodes;
573 raw_spin_lock_irqsave(&devtree_lock, flags);
574 for (; np; np = np->allnext) {
575 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
579 raw_spin_unlock_irqrestore(&devtree_lock, flags);
582 EXPORT_SYMBOL(of_find_node_by_path);
585 * of_find_node_by_name - Find a node by its "name" property
586 * @from: The node to start searching from or NULL, the node
587 * you pass will not be searched, only the next one
588 * will; typically, you pass what the previous call
589 * returned. of_node_put() will be called on it
590 * @name: The name string to match against
592 * Returns a node pointer with refcount incremented, use
593 * of_node_put() on it when done.
595 struct device_node *of_find_node_by_name(struct device_node *from,
598 struct device_node *np;
601 raw_spin_lock_irqsave(&devtree_lock, flags);
602 np = from ? from->allnext : of_allnodes;
603 for (; np; np = np->allnext)
604 if (np->name && (of_node_cmp(np->name, name) == 0)
608 raw_spin_unlock_irqrestore(&devtree_lock, flags);
611 EXPORT_SYMBOL(of_find_node_by_name);
614 * of_find_node_by_type - Find a node by its "device_type" property
615 * @from: The node to start searching from, or NULL to start searching
616 * the entire device tree. The node you pass will not be
617 * searched, only the next one will; typically, you pass
618 * what the previous call returned. of_node_put() will be
619 * called on from for you.
620 * @type: The type string to match against
622 * Returns a node pointer with refcount incremented, use
623 * of_node_put() on it when done.
625 struct device_node *of_find_node_by_type(struct device_node *from,
628 struct device_node *np;
631 raw_spin_lock_irqsave(&devtree_lock, flags);
632 np = from ? from->allnext : of_allnodes;
633 for (; np; np = np->allnext)
634 if (np->type && (of_node_cmp(np->type, type) == 0)
638 raw_spin_unlock_irqrestore(&devtree_lock, flags);
641 EXPORT_SYMBOL(of_find_node_by_type);
644 * of_find_compatible_node - Find a node based on type and one of the
645 * tokens in its "compatible" property
646 * @from: The node to start searching from or NULL, the node
647 * you pass will not be searched, only the next one
648 * will; typically, you pass what the previous call
649 * returned. of_node_put() will be called on it
650 * @type: The type string to match "device_type" or NULL to ignore
651 * @compatible: The string to match to one of the tokens in the device
654 * Returns a node pointer with refcount incremented, use
655 * of_node_put() on it when done.
657 struct device_node *of_find_compatible_node(struct device_node *from,
658 const char *type, const char *compatible)
660 struct device_node *np;
663 raw_spin_lock_irqsave(&devtree_lock, flags);
664 np = from ? from->allnext : of_allnodes;
665 for (; np; np = np->allnext) {
667 && !(np->type && (of_node_cmp(np->type, type) == 0)))
669 if (__of_device_is_compatible(np, compatible) &&
674 raw_spin_unlock_irqrestore(&devtree_lock, flags);
677 EXPORT_SYMBOL(of_find_compatible_node);
680 * of_find_node_with_property - Find a node which has a property with
682 * @from: The node to start searching from or NULL, the node
683 * you pass will not be searched, only the next one
684 * will; typically, you pass what the previous call
685 * returned. of_node_put() will be called on it
686 * @prop_name: The name of the property to look for.
688 * Returns a node pointer with refcount incremented, use
689 * of_node_put() on it when done.
691 struct device_node *of_find_node_with_property(struct device_node *from,
692 const char *prop_name)
694 struct device_node *np;
698 raw_spin_lock_irqsave(&devtree_lock, flags);
699 np = from ? from->allnext : of_allnodes;
700 for (; np; np = np->allnext) {
701 for (pp = np->properties; pp; pp = pp->next) {
702 if (of_prop_cmp(pp->name, prop_name) == 0) {
710 raw_spin_unlock_irqrestore(&devtree_lock, flags);
713 EXPORT_SYMBOL(of_find_node_with_property);
716 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
717 const struct device_node *node)
722 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
724 if (matches->name[0])
726 && !strcmp(matches->name, node->name);
727 if (matches->type[0])
729 && !strcmp(matches->type, node->type);
730 if (matches->compatible[0])
731 match &= __of_device_is_compatible(node,
732 matches->compatible);
741 * of_match_node - Tell if an device_node has a matching of_match structure
742 * @matches: array of of device match structures to search in
743 * @node: the of device structure to match against
745 * Low level utility function used by device matching.
747 const struct of_device_id *of_match_node(const struct of_device_id *matches,
748 const struct device_node *node)
750 const struct of_device_id *match;
753 raw_spin_lock_irqsave(&devtree_lock, flags);
754 match = __of_match_node(matches, node);
755 raw_spin_unlock_irqrestore(&devtree_lock, flags);
758 EXPORT_SYMBOL(of_match_node);
761 * of_find_matching_node_and_match - Find a node based on an of_device_id
763 * @from: The node to start searching from or NULL, the node
764 * you pass will not be searched, only the next one
765 * will; typically, you pass what the previous call
766 * returned. of_node_put() will be called on it
767 * @matches: array of of device match structures to search in
768 * @match Updated to point at the matches entry which matched
770 * Returns a node pointer with refcount incremented, use
771 * of_node_put() on it when done.
773 struct device_node *of_find_matching_node_and_match(struct device_node *from,
774 const struct of_device_id *matches,
775 const struct of_device_id **match)
777 struct device_node *np;
778 const struct of_device_id *m;
784 raw_spin_lock_irqsave(&devtree_lock, flags);
785 np = from ? from->allnext : of_allnodes;
786 for (; np; np = np->allnext) {
787 m = __of_match_node(matches, np);
788 if (m && of_node_get(np)) {
795 raw_spin_unlock_irqrestore(&devtree_lock, flags);
798 EXPORT_SYMBOL(of_find_matching_node_and_match);
801 * of_modalias_node - Lookup appropriate modalias for a device node
802 * @node: pointer to a device tree node
803 * @modalias: Pointer to buffer that modalias value will be copied into
804 * @len: Length of modalias value
806 * Based on the value of the compatible property, this routine will attempt
807 * to choose an appropriate modalias value for a particular device tree node.
808 * It does this by stripping the manufacturer prefix (as delimited by a ',')
809 * from the first entry in the compatible list property.
811 * This routine returns 0 on success, <0 on failure.
813 int of_modalias_node(struct device_node *node, char *modalias, int len)
815 const char *compatible, *p;
818 compatible = of_get_property(node, "compatible", &cplen);
819 if (!compatible || strlen(compatible) > cplen)
821 p = strchr(compatible, ',');
822 strlcpy(modalias, p ? p + 1 : compatible, len);
825 EXPORT_SYMBOL_GPL(of_modalias_node);
828 * of_find_node_by_phandle - Find a node given a phandle
829 * @handle: phandle of the node to find
831 * Returns a node pointer with refcount incremented, use
832 * of_node_put() on it when done.
834 struct device_node *of_find_node_by_phandle(phandle handle)
836 struct device_node *np;
839 raw_spin_lock_irqsave(&devtree_lock, flags);
840 for (np = of_allnodes; np; np = np->allnext)
841 if (np->phandle == handle)
844 raw_spin_unlock_irqrestore(&devtree_lock, flags);
847 EXPORT_SYMBOL(of_find_node_by_phandle);
850 * of_find_property_value_of_size
852 * @np: device node from which the property value is to be read.
853 * @propname: name of the property to be searched.
854 * @len: requested length of property value
856 * Search for a property in a device node and valid the requested size.
857 * Returns the property value on success, -EINVAL if the property does not
858 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
859 * property data isn't large enough.
862 static void *of_find_property_value_of_size(const struct device_node *np,
863 const char *propname, u32 len)
865 struct property *prop = of_find_property(np, propname, NULL);
868 return ERR_PTR(-EINVAL);
870 return ERR_PTR(-ENODATA);
871 if (len > prop->length)
872 return ERR_PTR(-EOVERFLOW);
878 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
880 * @np: device node from which the property value is to be read.
881 * @propname: name of the property to be searched.
882 * @index: index of the u32 in the list of values
883 * @out_value: pointer to return value, modified only if no error.
885 * Search for a property in a device node and read nth 32-bit value from
886 * it. Returns 0 on success, -EINVAL if the property does not exist,
887 * -ENODATA if property does not have a value, and -EOVERFLOW if the
888 * property data isn't large enough.
890 * The out_value is modified only if a valid u32 value can be decoded.
892 int of_property_read_u32_index(const struct device_node *np,
893 const char *propname,
894 u32 index, u32 *out_value)
896 const u32 *val = of_find_property_value_of_size(np, propname,
897 ((index + 1) * sizeof(*out_value)));
902 *out_value = be32_to_cpup(((__be32 *)val) + index);
905 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
908 * of_property_read_u8_array - Find and read an array of u8 from a property.
910 * @np: device node from which the property value is to be read.
911 * @propname: name of the property to be searched.
912 * @out_value: pointer to return value, modified only if return value is 0.
913 * @sz: number of array elements to read
915 * Search for a property in a device node and read 8-bit value(s) from
916 * it. Returns 0 on success, -EINVAL if the property does not exist,
917 * -ENODATA if property does not have a value, and -EOVERFLOW if the
918 * property data isn't large enough.
920 * dts entry of array should be like:
921 * property = /bits/ 8 <0x50 0x60 0x70>;
923 * The out_value is modified only if a valid u8 value can be decoded.
925 int of_property_read_u8_array(const struct device_node *np,
926 const char *propname, u8 *out_values, size_t sz)
928 const u8 *val = of_find_property_value_of_size(np, propname,
929 (sz * sizeof(*out_values)));
935 *out_values++ = *val++;
938 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
941 * of_property_read_u16_array - Find and read an array of u16 from a property.
943 * @np: device node from which the property value is to be read.
944 * @propname: name of the property to be searched.
945 * @out_value: pointer to return value, modified only if return value is 0.
946 * @sz: number of array elements to read
948 * Search for a property in a device node and read 16-bit value(s) from
949 * it. Returns 0 on success, -EINVAL if the property does not exist,
950 * -ENODATA if property does not have a value, and -EOVERFLOW if the
951 * property data isn't large enough.
953 * dts entry of array should be like:
954 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
956 * The out_value is modified only if a valid u16 value can be decoded.
958 int of_property_read_u16_array(const struct device_node *np,
959 const char *propname, u16 *out_values, size_t sz)
961 const __be16 *val = of_find_property_value_of_size(np, propname,
962 (sz * sizeof(*out_values)));
968 *out_values++ = be16_to_cpup(val++);
971 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
974 * of_property_read_u32_array - Find and read an array of 32 bit integers
977 * @np: device node from which the property value is to be read.
978 * @propname: name of the property to be searched.
979 * @out_value: pointer to return value, modified only if return value is 0.
980 * @sz: number of array elements to read
982 * Search for a property in a device node and read 32-bit value(s) from
983 * it. Returns 0 on success, -EINVAL if the property does not exist,
984 * -ENODATA if property does not have a value, and -EOVERFLOW if the
985 * property data isn't large enough.
987 * The out_value is modified only if a valid u32 value can be decoded.
989 int of_property_read_u32_array(const struct device_node *np,
990 const char *propname, u32 *out_values,
993 const __be32 *val = of_find_property_value_of_size(np, propname,
994 (sz * sizeof(*out_values)));
1000 *out_values++ = be32_to_cpup(val++);
1003 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1006 * of_property_read_u64 - Find and read a 64 bit integer from a property
1007 * @np: device node from which the property value is to be read.
1008 * @propname: name of the property to be searched.
1009 * @out_value: pointer to return value, modified only if return value is 0.
1011 * Search for a property in a device node and read a 64-bit value from
1012 * it. Returns 0 on success, -EINVAL if the property does not exist,
1013 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1014 * property data isn't large enough.
1016 * The out_value is modified only if a valid u64 value can be decoded.
1018 int of_property_read_u64(const struct device_node *np, const char *propname,
1021 const __be32 *val = of_find_property_value_of_size(np, propname,
1022 sizeof(*out_value));
1025 return PTR_ERR(val);
1027 *out_value = of_read_number(val, 2);
1030 EXPORT_SYMBOL_GPL(of_property_read_u64);
1033 * of_property_read_string - Find and read a string from a property
1034 * @np: device node from which the property value is to be read.
1035 * @propname: name of the property to be searched.
1036 * @out_string: pointer to null terminated return string, modified only if
1037 * return value is 0.
1039 * Search for a property in a device tree node and retrieve a null
1040 * terminated string value (pointer to data, not a copy). Returns 0 on
1041 * success, -EINVAL if the property does not exist, -ENODATA if property
1042 * does not have a value, and -EILSEQ if the string is not null-terminated
1043 * within the length of the property data.
1045 * The out_string pointer is modified only if a valid string can be decoded.
1047 int of_property_read_string(struct device_node *np, const char *propname,
1048 const char **out_string)
1050 struct property *prop = of_find_property(np, propname, NULL);
1055 if (strnlen(prop->value, prop->length) >= prop->length)
1057 *out_string = prop->value;
1060 EXPORT_SYMBOL_GPL(of_property_read_string);
1063 * of_property_match_string() - Find string in a list and return index
1064 * @np: pointer to node containing string list property
1065 * @propname: string list property name
1066 * @string: pointer to string to search for in string list
1068 * This function searches a string list property and returns the index
1069 * of a specific string value.
1071 int of_property_match_string(struct device_node *np, const char *propname,
1074 struct property *prop = of_find_property(np, propname, NULL);
1077 const char *p, *end;
1085 end = p + prop->length;
1087 for (i = 0; p < end; i++, p += l) {
1088 l = strnlen(p, end - p) + 1;
1091 pr_debug("comparing %s with %s\n", string, p);
1092 if (strcmp(string, p) == 0)
1093 return i; /* Found it; return index */
1097 EXPORT_SYMBOL_GPL(of_property_match_string);
1100 * of_property_read_string_util() - Utility helper for parsing string properties
1101 * @np: device node from which the property value is to be read.
1102 * @propname: name of the property to be searched.
1103 * @out_strs: output array of string pointers.
1104 * @sz: number of array elements to read.
1105 * @skip: Number of strings to skip over at beginning of list.
1107 * Don't call this function directly. It is a utility helper for the
1108 * of_property_read_string*() family of functions.
1110 int of_property_read_string_helper(struct device_node *np, const char *propname,
1111 const char **out_strs, size_t sz, int skip)
1113 struct property *prop = of_find_property(np, propname, NULL);
1115 const char *p, *end;
1122 end = p + prop->length;
1124 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1125 l = strnlen(p, end - p) + 1;
1128 if (out_strs && i >= skip)
1132 return i <= 0 ? -ENODATA : i;
1134 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
1137 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1138 * @np: Pointer to device node holding phandle property
1139 * @phandle_name: Name of property holding a phandle value
1140 * @index: For properties holding a table of phandles, this is the index into
1143 * Returns the device_node pointer with refcount incremented. Use
1144 * of_node_put() on it when done.
1146 struct device_node *of_parse_phandle(const struct device_node *np,
1147 const char *phandle_name, int index)
1149 const __be32 *phandle;
1152 phandle = of_get_property(np, phandle_name, &size);
1153 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1156 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
1158 EXPORT_SYMBOL(of_parse_phandle);
1161 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1162 * @np: pointer to a device tree node containing a list
1163 * @list_name: property name that contains a list
1164 * @cells_name: property name that specifies phandles' arguments count
1165 * @index: index of a phandle to parse out
1166 * @out_args: optional pointer to output arguments structure (will be filled)
1168 * This function is useful to parse lists of phandles and their arguments.
1169 * Returns 0 on success and fills out_args, on error returns appropriate
1172 * Caller is responsible to call of_node_put() on the returned out_args->node
1178 * #list-cells = <2>;
1182 * #list-cells = <1>;
1186 * list = <&phandle1 1 2 &phandle2 3>;
1189 * To get a device_node of the `node2' node you may call this:
1190 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1192 static int __of_parse_phandle_with_args(const struct device_node *np,
1193 const char *list_name,
1194 const char *cells_name, int index,
1195 struct of_phandle_args *out_args)
1197 const __be32 *list, *list_end;
1198 int rc = 0, size, cur_index = 0;
1200 struct device_node *node = NULL;
1203 /* Retrieve the phandle list property */
1204 list = of_get_property(np, list_name, &size);
1207 list_end = list + size / sizeof(*list);
1209 /* Loop over the phandles until all the requested entry is found */
1210 while (list < list_end) {
1215 * If phandle is 0, then it is an empty entry with no
1216 * arguments. Skip forward to the next entry.
1218 phandle = be32_to_cpup(list++);
1221 * Find the provider node and parse the #*-cells
1222 * property to determine the argument length
1224 node = of_find_node_by_phandle(phandle);
1226 pr_err("%s: could not find phandle\n",
1230 if (of_property_read_u32(node, cells_name, &count)) {
1231 pr_err("%s: could not get %s for %s\n",
1232 np->full_name, cells_name,
1238 * Make sure that the arguments actually fit in the
1239 * remaining property data length
1241 if (list + count > list_end) {
1242 pr_err("%s: arguments longer than property\n",
1249 * All of the error cases above bail out of the loop, so at
1250 * this point, the parsing is successful. If the requested
1251 * index matches, then fill the out_args structure and return,
1252 * or return -ENOENT for an empty entry.
1255 if (cur_index == index) {
1261 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1262 count = MAX_PHANDLE_ARGS;
1263 out_args->np = node;
1264 out_args->args_count = count;
1265 for (i = 0; i < count; i++)
1266 out_args->args[i] = be32_to_cpup(list++);
1271 /* Found it! return success */
1282 * Unlock node before returning result; will be one of:
1283 * -ENOENT : index is for empty phandle
1284 * -EINVAL : parsing error on data
1285 * [1..n] : Number of phandle (count mode; when index = -1)
1287 rc = index < 0 ? cur_index : -ENOENT;
1294 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1295 const char *cells_name, int index,
1296 struct of_phandle_args *out_args)
1300 return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args);
1302 EXPORT_SYMBOL(of_parse_phandle_with_args);
1305 * of_count_phandle_with_args() - Find the number of phandles references in a property
1306 * @np: pointer to a device tree node containing a list
1307 * @list_name: property name that contains a list
1308 * @cells_name: property name that specifies phandles' arguments count
1310 * Returns the number of phandle + argument tuples within a property. It
1311 * is a typical pattern to encode a list of phandle and variable
1312 * arguments into a single property. The number of arguments is encoded
1313 * by a property in the phandle-target node. For example, a gpios
1314 * property would contain a list of GPIO specifies consisting of a
1315 * phandle and 1 or more arguments. The number of arguments are
1316 * determined by the #gpio-cells property in the node pointed to by the
1319 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1320 const char *cells_name)
1322 return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL);
1324 EXPORT_SYMBOL(of_count_phandle_with_args);
1326 #if defined(CONFIG_OF_DYNAMIC)
1327 static int of_property_notify(int action, struct device_node *np,
1328 struct property *prop)
1330 struct of_prop_reconfig pr;
1334 return of_reconfig_notify(action, &pr);
1337 static int of_property_notify(int action, struct device_node *np,
1338 struct property *prop)
1345 * of_add_property - Add a property to a node
1347 int of_add_property(struct device_node *np, struct property *prop)
1349 struct property **next;
1350 unsigned long flags;
1353 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1358 raw_spin_lock_irqsave(&devtree_lock, flags);
1359 next = &np->properties;
1361 if (strcmp(prop->name, (*next)->name) == 0) {
1362 /* duplicate ! don't insert it */
1363 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1366 next = &(*next)->next;
1369 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1371 #ifdef CONFIG_PROC_DEVICETREE
1372 /* try to add to proc as well if it was initialized */
1374 proc_device_tree_add_prop(np->pde, prop);
1375 #endif /* CONFIG_PROC_DEVICETREE */
1381 * of_remove_property - Remove a property from a node.
1383 * Note that we don't actually remove it, since we have given out
1384 * who-knows-how-many pointers to the data using get-property.
1385 * Instead we just move the property to the "dead properties"
1386 * list, so it won't be found any more.
1388 int of_remove_property(struct device_node *np, struct property *prop)
1390 struct property **next;
1391 unsigned long flags;
1395 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1399 raw_spin_lock_irqsave(&devtree_lock, flags);
1400 next = &np->properties;
1402 if (*next == prop) {
1403 /* found the node */
1405 prop->next = np->deadprops;
1406 np->deadprops = prop;
1410 next = &(*next)->next;
1412 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1417 #ifdef CONFIG_PROC_DEVICETREE
1418 /* try to remove the proc node as well */
1420 proc_device_tree_remove_prop(np->pde, prop);
1421 #endif /* CONFIG_PROC_DEVICETREE */
1427 * of_update_property - Update a property in a node, if the property does
1428 * not exist, add it.
1430 * Note that we don't actually remove it, since we have given out
1431 * who-knows-how-many pointers to the data using get-property.
1432 * Instead we just move the property to the "dead properties" list,
1433 * and add the new property to the property list
1435 int of_update_property(struct device_node *np, struct property *newprop)
1437 struct property **next, *oldprop;
1438 unsigned long flags;
1441 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1448 oldprop = of_find_property(np, newprop->name, NULL);
1450 return of_add_property(np, newprop);
1452 raw_spin_lock_irqsave(&devtree_lock, flags);
1453 next = &np->properties;
1455 if (*next == oldprop) {
1456 /* found the node */
1457 newprop->next = oldprop->next;
1459 oldprop->next = np->deadprops;
1460 np->deadprops = oldprop;
1464 next = &(*next)->next;
1466 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1471 #ifdef CONFIG_PROC_DEVICETREE
1472 /* try to add to proc as well if it was initialized */
1474 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1475 #endif /* CONFIG_PROC_DEVICETREE */
1480 #if defined(CONFIG_OF_DYNAMIC)
1482 * Support for dynamic device trees.
1484 * On some platforms, the device tree can be manipulated at runtime.
1485 * The routines in this section support adding, removing and changing
1486 * device tree nodes.
1489 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1491 int of_reconfig_notifier_register(struct notifier_block *nb)
1493 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1495 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1497 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1499 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1501 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1503 int of_reconfig_notify(unsigned long action, void *p)
1507 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1508 return notifier_to_errno(rc);
1511 #ifdef CONFIG_PROC_DEVICETREE
1512 static void of_add_proc_dt_entry(struct device_node *dn)
1514 struct proc_dir_entry *ent;
1516 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1518 proc_device_tree_add_node(dn, ent);
1521 static void of_add_proc_dt_entry(struct device_node *dn)
1528 * of_attach_node - Plug a device node into the tree and global list.
1530 int of_attach_node(struct device_node *np)
1532 unsigned long flags;
1535 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1539 raw_spin_lock_irqsave(&devtree_lock, flags);
1540 np->sibling = np->parent->child;
1541 np->allnext = of_allnodes;
1542 np->parent->child = np;
1544 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1546 of_add_proc_dt_entry(np);
1550 #ifdef CONFIG_PROC_DEVICETREE
1551 static void of_remove_proc_dt_entry(struct device_node *dn)
1553 proc_remove(dn->pde);
1556 static void of_remove_proc_dt_entry(struct device_node *dn)
1563 * of_detach_node - "Unplug" a node from the device tree.
1565 * The caller must hold a reference to the node. The memory associated with
1566 * the node is not freed until its refcount goes to zero.
1568 int of_detach_node(struct device_node *np)
1570 struct device_node *parent;
1571 unsigned long flags;
1574 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1578 raw_spin_lock_irqsave(&devtree_lock, flags);
1580 if (of_node_check_flag(np, OF_DETACHED)) {
1581 /* someone already detached it */
1582 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1586 parent = np->parent;
1588 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1592 if (of_allnodes == np)
1593 of_allnodes = np->allnext;
1595 struct device_node *prev;
1596 for (prev = of_allnodes;
1597 prev->allnext != np;
1598 prev = prev->allnext)
1600 prev->allnext = np->allnext;
1603 if (parent->child == np)
1604 parent->child = np->sibling;
1606 struct device_node *prevsib;
1607 for (prevsib = np->parent->child;
1608 prevsib->sibling != np;
1609 prevsib = prevsib->sibling)
1611 prevsib->sibling = np->sibling;
1614 of_node_set_flag(np, OF_DETACHED);
1615 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1617 of_remove_proc_dt_entry(np);
1620 #endif /* defined(CONFIG_OF_DYNAMIC) */
1622 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1623 int id, const char *stem, int stem_len)
1627 strncpy(ap->stem, stem, stem_len);
1628 ap->stem[stem_len] = 0;
1629 list_add_tail(&ap->link, &aliases_lookup);
1630 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1631 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1635 * of_alias_scan - Scan all properties of 'aliases' node
1637 * The function scans all the properties of 'aliases' node and populate
1638 * the the global lookup table with the properties. It returns the
1639 * number of alias_prop found, or error code in error case.
1641 * @dt_alloc: An allocator that provides a virtual address to memory
1642 * for the resulting tree
1644 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1646 struct property *pp;
1648 of_chosen = of_find_node_by_path("/chosen");
1649 if (of_chosen == NULL)
1650 of_chosen = of_find_node_by_path("/chosen@0");
1655 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1657 of_stdout = of_find_node_by_path(name);
1660 of_aliases = of_find_node_by_path("/aliases");
1664 for_each_property_of_node(of_aliases, pp) {
1665 const char *start = pp->name;
1666 const char *end = start + strlen(start);
1667 struct device_node *np;
1668 struct alias_prop *ap;
1671 /* Skip those we do not want to proceed */
1672 if (!strcmp(pp->name, "name") ||
1673 !strcmp(pp->name, "phandle") ||
1674 !strcmp(pp->name, "linux,phandle"))
1677 np = of_find_node_by_path(pp->value);
1681 /* walk the alias backwards to extract the id and work out
1682 * the 'stem' string */
1683 while (isdigit(*(end-1)) && end > start)
1687 if (kstrtoint(end, 10, &id) < 0)
1690 /* Allocate an alias_prop with enough space for the stem */
1691 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1694 memset(ap, 0, sizeof(*ap) + len + 1);
1696 of_alias_add(ap, np, id, start, len);
1701 * of_alias_get_id - Get alias id for the given device_node
1702 * @np: Pointer to the given device_node
1703 * @stem: Alias stem of the given device_node
1705 * The function travels the lookup table to get alias id for the given
1706 * device_node and alias stem. It returns the alias id if find it.
1708 int of_alias_get_id(struct device_node *np, const char *stem)
1710 struct alias_prop *app;
1713 mutex_lock(&of_aliases_mutex);
1714 list_for_each_entry(app, &aliases_lookup, link) {
1715 if (strcmp(app->stem, stem) != 0)
1718 if (np == app->np) {
1723 mutex_unlock(&of_aliases_mutex);
1727 EXPORT_SYMBOL_GPL(of_alias_get_id);
1729 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1732 const void *curv = cur;
1742 curv += sizeof(*cur);
1743 if (curv >= prop->value + prop->length)
1747 *pu = be32_to_cpup(curv);
1750 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1752 const char *of_prop_next_string(struct property *prop, const char *cur)
1754 const void *curv = cur;
1762 curv += strlen(cur) + 1;
1763 if (curv >= prop->value + prop->length)
1768 EXPORT_SYMBOL_GPL(of_prop_next_string);
1771 * of_device_is_stdout_path - check if a device node matches the
1772 * linux,stdout-path property
1774 * Check if this device node matches the linux,stdout-path property
1775 * in the chosen node. return true if yes, false otherwise.
1777 int of_device_is_stdout_path(struct device_node *dn)
1782 return of_stdout == dn;
1784 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
1787 * of_graph_parse_endpoint() - parse common endpoint node properties
1788 * @node: pointer to endpoint device_node
1789 * @endpoint: pointer to the OF endpoint data structure
1791 * The caller should hold a reference to @node.
1793 int of_graph_parse_endpoint(const struct device_node *node,
1794 struct of_endpoint *endpoint)
1796 struct device_node *port_node = of_get_parent(node);
1798 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
1799 __func__, node->full_name);
1801 memset(endpoint, 0, sizeof(*endpoint));
1803 endpoint->local_node = node;
1805 * It doesn't matter whether the two calls below succeed.
1806 * If they don't then the default value 0 is used.
1808 of_property_read_u32(port_node, "reg", &endpoint->port);
1809 of_property_read_u32(node, "reg", &endpoint->id);
1811 of_node_put(port_node);
1815 EXPORT_SYMBOL(of_graph_parse_endpoint);
1818 * of_graph_get_next_endpoint() - get next endpoint node
1819 * @parent: pointer to the parent device node
1820 * @prev: previous endpoint node, or NULL to get first
1822 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
1823 * of the passed @prev node is not decremented, the caller have to use
1824 * of_node_put() on it when done.
1826 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
1827 struct device_node *prev)
1829 struct device_node *endpoint;
1830 struct device_node *port;
1836 * Start by locating the port node. If no previous endpoint is specified
1837 * search for the first port node, otherwise get the previous endpoint
1841 struct device_node *node;
1843 node = of_get_child_by_name(parent, "ports");
1847 port = of_get_child_by_name(parent, "port");
1851 pr_err("%s(): no port node found in %s\n",
1852 __func__, parent->full_name);
1856 port = of_get_parent(prev);
1857 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
1858 __func__, prev->full_name))
1862 * Avoid dropping prev node refcount to 0 when getting the next
1870 * Now that we have a port node, get the next endpoint by
1871 * getting the next child. If the previous endpoint is NULL this
1872 * will return the first child.
1874 endpoint = of_get_next_child(port, prev);
1880 /* No more endpoints under this port, try the next one. */
1884 port = of_get_next_child(parent, port);
1887 } while (of_node_cmp(port->name, "port"));
1890 EXPORT_SYMBOL(of_graph_get_next_endpoint);
1893 * of_graph_get_remote_port_parent() - get remote port's parent node
1894 * @node: pointer to a local endpoint device_node
1896 * Return: Remote device node associated with remote endpoint node linked
1897 * to @node. Use of_node_put() on it when done.
1899 struct device_node *of_graph_get_remote_port_parent(
1900 const struct device_node *node)
1902 struct device_node *np;
1905 /* Get remote endpoint node. */
1906 np = of_parse_phandle(node, "remote-endpoint", 0);
1908 /* Walk 3 levels up only if there is 'ports' node. */
1909 for (depth = 3; depth && np; depth--) {
1910 np = of_get_next_parent(np);
1911 if (depth == 2 && of_node_cmp(np->name, "ports"))
1916 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
1919 * of_graph_get_remote_port() - get remote port node
1920 * @node: pointer to a local endpoint device_node
1922 * Return: Remote port node associated with remote endpoint node linked
1923 * to @node. Use of_node_put() on it when done.
1925 struct device_node *of_graph_get_remote_port(const struct device_node *node)
1927 struct device_node *np;
1929 /* Get remote endpoint node. */
1930 np = of_parse_phandle(node, "remote-endpoint", 0);
1933 return of_get_next_parent(np);
1935 EXPORT_SYMBOL(of_graph_get_remote_port);