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/string.h>
28 #include <linux/proc_fs.h>
30 #include "of_private.h"
32 LIST_HEAD(aliases_lookup);
34 struct device_node *of_allnodes;
35 EXPORT_SYMBOL(of_allnodes);
36 struct device_node *of_chosen;
37 struct device_node *of_aliases;
38 static struct device_node *of_stdout;
43 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
44 * This mutex must be held whenever modifications are being made to the
45 * device tree. The of_{attach,detach}_node() and
46 * of_{add,remove,update}_property() helpers make sure this happens.
48 DEFINE_MUTEX(of_mutex);
50 /* use when traversing tree through the allnext, child, sibling,
51 * or parent members of struct device_node.
53 DEFINE_RAW_SPINLOCK(devtree_lock);
55 int of_n_addr_cells(struct device_node *np)
62 ip = of_get_property(np, "#address-cells", NULL);
64 return be32_to_cpup(ip);
66 /* No #address-cells property for the root node */
67 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
69 EXPORT_SYMBOL(of_n_addr_cells);
71 int of_n_size_cells(struct device_node *np)
78 ip = of_get_property(np, "#size-cells", NULL);
80 return be32_to_cpup(ip);
82 /* No #size-cells property for the root node */
83 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
85 EXPORT_SYMBOL(of_n_size_cells);
87 #ifndef CONFIG_OF_DYNAMIC
88 static void of_node_release(struct kobject *kobj)
90 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
92 #endif /* CONFIG_OF_DYNAMIC */
94 struct kobj_type of_node_ktype = {
95 .release = of_node_release,
98 static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
99 struct bin_attribute *bin_attr, char *buf,
100 loff_t offset, size_t count)
102 struct property *pp = container_of(bin_attr, struct property, attr);
103 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
106 static const char *safe_name(struct kobject *kobj, const char *orig_name)
108 const char *name = orig_name;
109 struct sysfs_dirent *kn;
112 /* don't be a hero. After 16 tries give up */
113 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, NULL, name))) {
115 if (name != orig_name)
117 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
120 if (name != orig_name)
121 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
122 kobject_name(kobj), name);
126 int __of_add_property_sysfs(struct device_node *np, struct property *pp)
130 /* Important: Don't leak passwords */
131 bool secure = strncmp(pp->name, "security-", 9) == 0;
133 if (!of_kset || !of_node_is_attached(np))
136 sysfs_bin_attr_init(&pp->attr);
137 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
138 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
139 pp->attr.size = secure ? 0 : pp->length;
140 pp->attr.read = of_node_property_read;
142 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
143 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
147 int __of_attach_node_sysfs(struct device_node *np)
156 np->kobj.kset = of_kset;
158 /* Nodes without parents are new top level trees */
159 rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base"));
161 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
162 if (!name || !name[0])
165 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
170 for_each_property_of_node(np, pp)
171 __of_add_property_sysfs(np, pp);
176 static int __init of_init(void)
178 struct device_node *np;
180 /* Create the kset, and register existing nodes */
181 mutex_lock(&of_mutex);
182 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
184 mutex_unlock(&of_mutex);
187 for_each_of_allnodes(np)
188 __of_attach_node_sysfs(np);
189 mutex_unlock(&of_mutex);
191 /* Symlink in /proc as required by userspace ABI */
193 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
197 core_initcall(of_init);
199 static struct property *__of_find_property(const struct device_node *np,
200 const char *name, int *lenp)
207 for (pp = np->properties; pp; pp = pp->next) {
208 if (of_prop_cmp(pp->name, name) == 0) {
218 struct property *of_find_property(const struct device_node *np,
225 raw_spin_lock_irqsave(&devtree_lock, flags);
226 pp = __of_find_property(np, name, lenp);
227 raw_spin_unlock_irqrestore(&devtree_lock, flags);
231 EXPORT_SYMBOL(of_find_property);
234 * of_find_all_nodes - Get next node in global list
235 * @prev: Previous node or NULL to start iteration
236 * of_node_put() will be called on it
238 * Returns a node pointer with refcount incremented, use
239 * of_node_put() on it when done.
241 struct device_node *of_find_all_nodes(struct device_node *prev)
243 struct device_node *np;
246 raw_spin_lock_irqsave(&devtree_lock, flags);
247 np = prev ? prev->allnext : of_allnodes;
248 for (; np != NULL; np = np->allnext)
252 raw_spin_unlock_irqrestore(&devtree_lock, flags);
255 EXPORT_SYMBOL(of_find_all_nodes);
258 * Find a property with a given name for a given node
259 * and return the value.
261 const void *__of_get_property(const struct device_node *np,
262 const char *name, int *lenp)
264 struct property *pp = __of_find_property(np, name, lenp);
266 return pp ? pp->value : NULL;
270 * Find a property with a given name for a given node
271 * and return the value.
273 const void *of_get_property(const struct device_node *np, const char *name,
276 struct property *pp = of_find_property(np, name, lenp);
278 return pp ? pp->value : NULL;
280 EXPORT_SYMBOL(of_get_property);
283 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
285 * @cpu: logical cpu index of a core/thread
286 * @phys_id: physical identifier of a core/thread
288 * CPU logical to physical index mapping is architecture specific.
289 * However this __weak function provides a default match of physical
290 * id to logical cpu index. phys_id provided here is usually values read
291 * from the device tree which must match the hardware internal registers.
293 * Returns true if the physical identifier and the logical cpu index
294 * correspond to the same core/thread, false otherwise.
296 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
298 return (u32)phys_id == cpu;
302 * Checks if the given "prop_name" property holds the physical id of the
303 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
304 * NULL, local thread number within the core is returned in it.
306 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
307 const char *prop_name, int cpu, unsigned int *thread)
310 int ac, prop_len, tid;
313 ac = of_n_addr_cells(cpun);
314 cell = of_get_property(cpun, prop_name, &prop_len);
317 prop_len /= sizeof(*cell);
318 for (tid = 0; tid < prop_len; tid++) {
319 hwid = of_read_number(cell, ac);
320 if (arch_match_cpu_phys_id(cpu, hwid)) {
331 * of_get_cpu_node - Get device node associated with the given logical CPU
333 * @cpu: CPU number(logical index) for which device node is required
334 * @thread: if not NULL, local thread number within the physical core is
337 * The main purpose of this function is to retrieve the device node for the
338 * given logical CPU index. It should be used to initialize the of_node in
339 * cpu device. Once of_node in cpu device is populated, all the further
340 * references can use that instead.
342 * CPU logical to physical index mapping is architecture specific and is built
343 * before booting secondary cores. This function uses arch_match_cpu_phys_id
344 * which can be overridden by architecture specific implementation.
346 * Returns a node pointer for the logical cpu if found, else NULL.
348 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
350 struct device_node *cpun, *cpus;
352 cpus = of_find_node_by_path("/cpus");
354 pr_warn("Missing cpus node, bailing out\n");
358 for_each_child_of_node(cpus, cpun) {
359 if (of_node_cmp(cpun->type, "cpu"))
361 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
362 * for thread ids on PowerPC. If it doesn't exist fallback to
363 * standard "reg" property.
365 if (IS_ENABLED(CONFIG_PPC) &&
366 __of_find_n_match_cpu_property(cpun,
367 "ibm,ppc-interrupt-server#s", cpu, thread))
369 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
374 EXPORT_SYMBOL(of_get_cpu_node);
376 /** Checks if the given "compat" string matches one of the strings in
377 * the device's "compatible" property
379 static int __of_device_is_compatible(const struct device_node *device,
385 cp = __of_get_property(device, "compatible", &cplen);
389 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
399 /** Checks if the given "compat" string matches one of the strings in
400 * the device's "compatible" property
402 int of_device_is_compatible(const struct device_node *device,
408 raw_spin_lock_irqsave(&devtree_lock, flags);
409 res = __of_device_is_compatible(device, compat);
410 raw_spin_unlock_irqrestore(&devtree_lock, flags);
413 EXPORT_SYMBOL(of_device_is_compatible);
416 * of_machine_is_compatible - Test root of device tree for a given compatible value
417 * @compat: compatible string to look for in root node's compatible property.
419 * Returns true if the root node has the given value in its
420 * compatible property.
422 int of_machine_is_compatible(const char *compat)
424 struct device_node *root;
427 root = of_find_node_by_path("/");
429 rc = of_device_is_compatible(root, compat);
434 EXPORT_SYMBOL(of_machine_is_compatible);
437 * __of_device_is_available - check if a device is available for use
439 * @device: Node to check for availability, with locks already held
441 * Returns 1 if the status property is absent or set to "okay" or "ok",
444 static int __of_device_is_available(const struct device_node *device)
449 status = __of_get_property(device, "status", &statlen);
454 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
462 * of_device_is_available - check if a device is available for use
464 * @device: Node to check for availability
466 * Returns 1 if the status property is absent or set to "okay" or "ok",
469 int of_device_is_available(const struct device_node *device)
474 raw_spin_lock_irqsave(&devtree_lock, flags);
475 res = __of_device_is_available(device);
476 raw_spin_unlock_irqrestore(&devtree_lock, flags);
480 EXPORT_SYMBOL(of_device_is_available);
483 * of_get_parent - Get a node's parent if any
484 * @node: Node to get parent
486 * Returns a node pointer with refcount incremented, use
487 * of_node_put() on it when done.
489 struct device_node *of_get_parent(const struct device_node *node)
491 struct device_node *np;
497 raw_spin_lock_irqsave(&devtree_lock, flags);
498 np = of_node_get(node->parent);
499 raw_spin_unlock_irqrestore(&devtree_lock, flags);
502 EXPORT_SYMBOL(of_get_parent);
505 * of_get_next_parent - Iterate to a node's parent
506 * @node: Node to get parent of
508 * This is like of_get_parent() except that it drops the
509 * refcount on the passed node, making it suitable for iterating
510 * through a node's parents.
512 * Returns a node pointer with refcount incremented, use
513 * of_node_put() on it when done.
515 struct device_node *of_get_next_parent(struct device_node *node)
517 struct device_node *parent;
523 raw_spin_lock_irqsave(&devtree_lock, flags);
524 parent = of_node_get(node->parent);
526 raw_spin_unlock_irqrestore(&devtree_lock, flags);
529 EXPORT_SYMBOL(of_get_next_parent);
532 * of_get_next_child - Iterate a node childs
534 * @prev: previous child of the parent node, or NULL to get first
536 * Returns a node pointer with refcount incremented, use
537 * of_node_put() on it when done.
539 struct device_node *of_get_next_child(const struct device_node *node,
540 struct device_node *prev)
542 struct device_node *next;
545 raw_spin_lock_irqsave(&devtree_lock, flags);
546 next = prev ? prev->sibling : node->child;
547 for (; next; next = next->sibling)
548 if (of_node_get(next))
551 raw_spin_unlock_irqrestore(&devtree_lock, flags);
554 EXPORT_SYMBOL(of_get_next_child);
557 * of_get_next_available_child - Find the next available child node
559 * @prev: previous child of the parent node, or NULL to get first
561 * This function is like of_get_next_child(), except that it
562 * automatically skips any disabled nodes (i.e. status = "disabled").
564 struct device_node *of_get_next_available_child(const struct device_node *node,
565 struct device_node *prev)
567 struct device_node *next;
570 raw_spin_lock_irqsave(&devtree_lock, flags);
571 next = prev ? prev->sibling : node->child;
572 for (; next; next = next->sibling) {
573 if (!__of_device_is_available(next))
575 if (of_node_get(next))
579 raw_spin_unlock_irqrestore(&devtree_lock, flags);
582 EXPORT_SYMBOL(of_get_next_available_child);
585 * of_get_child_by_name - Find the child node by name for a given parent
587 * @name: child name to look for.
589 * This function looks for child node for given matching name
591 * Returns a node pointer if found, with refcount incremented, use
592 * of_node_put() on it when done.
593 * Returns NULL if node is not found.
595 struct device_node *of_get_child_by_name(const struct device_node *node,
598 struct device_node *child;
600 for_each_child_of_node(node, child)
601 if (child->name && (of_node_cmp(child->name, name) == 0))
605 EXPORT_SYMBOL(of_get_child_by_name);
608 * of_find_node_by_path - Find a node matching a full OF path
609 * @path: The full path to match
611 * Returns a node pointer with refcount incremented, use
612 * of_node_put() on it when done.
614 struct device_node *of_find_node_by_path(const char *path)
616 struct device_node *np = of_allnodes;
619 raw_spin_lock_irqsave(&devtree_lock, flags);
620 for (; np; np = np->allnext) {
621 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
625 raw_spin_unlock_irqrestore(&devtree_lock, flags);
628 EXPORT_SYMBOL(of_find_node_by_path);
631 * of_find_node_by_name - Find a node by its "name" property
632 * @from: The node to start searching from or NULL, the node
633 * you pass will not be searched, only the next one
634 * will; typically, you pass what the previous call
635 * returned. of_node_put() will be called on it
636 * @name: The name string to match against
638 * Returns a node pointer with refcount incremented, use
639 * of_node_put() on it when done.
641 struct device_node *of_find_node_by_name(struct device_node *from,
644 struct device_node *np;
647 raw_spin_lock_irqsave(&devtree_lock, flags);
648 np = from ? from->allnext : of_allnodes;
649 for (; np; np = np->allnext)
650 if (np->name && (of_node_cmp(np->name, name) == 0)
654 raw_spin_unlock_irqrestore(&devtree_lock, flags);
657 EXPORT_SYMBOL(of_find_node_by_name);
660 * of_find_node_by_type - Find a node by its "device_type" property
661 * @from: The node to start searching from, or NULL to start searching
662 * the entire device tree. The node you pass will not be
663 * searched, only the next one will; typically, you pass
664 * what the previous call returned. of_node_put() will be
665 * called on from for you.
666 * @type: The type string to match against
668 * Returns a node pointer with refcount incremented, use
669 * of_node_put() on it when done.
671 struct device_node *of_find_node_by_type(struct device_node *from,
674 struct device_node *np;
677 raw_spin_lock_irqsave(&devtree_lock, flags);
678 np = from ? from->allnext : of_allnodes;
679 for (; np; np = np->allnext)
680 if (np->type && (of_node_cmp(np->type, type) == 0)
684 raw_spin_unlock_irqrestore(&devtree_lock, flags);
687 EXPORT_SYMBOL(of_find_node_by_type);
690 * of_find_compatible_node - Find a node based on type and one of the
691 * tokens in its "compatible" property
692 * @from: The node to start searching from or NULL, the node
693 * you pass will not be searched, only the next one
694 * will; typically, you pass what the previous call
695 * returned. of_node_put() will be called on it
696 * @type: The type string to match "device_type" or NULL to ignore
697 * @compatible: The string to match to one of the tokens in the device
700 * Returns a node pointer with refcount incremented, use
701 * of_node_put() on it when done.
703 struct device_node *of_find_compatible_node(struct device_node *from,
704 const char *type, const char *compatible)
706 struct device_node *np;
709 raw_spin_lock_irqsave(&devtree_lock, flags);
710 np = from ? from->allnext : of_allnodes;
711 for (; np; np = np->allnext) {
713 && !(np->type && (of_node_cmp(np->type, type) == 0)))
715 if (__of_device_is_compatible(np, compatible) &&
720 raw_spin_unlock_irqrestore(&devtree_lock, flags);
723 EXPORT_SYMBOL(of_find_compatible_node);
726 * of_find_node_with_property - Find a node which has a property with
728 * @from: The node to start searching from or NULL, the node
729 * you pass will not be searched, only the next one
730 * will; typically, you pass what the previous call
731 * returned. of_node_put() will be called on it
732 * @prop_name: The name of the property to look for.
734 * Returns a node pointer with refcount incremented, use
735 * of_node_put() on it when done.
737 struct device_node *of_find_node_with_property(struct device_node *from,
738 const char *prop_name)
740 struct device_node *np;
744 raw_spin_lock_irqsave(&devtree_lock, flags);
745 np = from ? from->allnext : of_allnodes;
746 for (; np; np = np->allnext) {
747 for (pp = np->properties; pp; pp = pp->next) {
748 if (of_prop_cmp(pp->name, prop_name) == 0) {
756 raw_spin_unlock_irqrestore(&devtree_lock, flags);
759 EXPORT_SYMBOL(of_find_node_with_property);
762 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
763 const struct device_node *node)
768 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
770 if (matches->name[0])
772 && !strcmp(matches->name, node->name);
773 if (matches->type[0])
775 && !strcmp(matches->type, node->type);
776 if (matches->compatible[0])
777 match &= __of_device_is_compatible(node,
778 matches->compatible);
787 * of_match_node - Tell if an device_node has a matching of_match structure
788 * @matches: array of of device match structures to search in
789 * @node: the of device structure to match against
791 * Low level utility function used by device matching.
793 const struct of_device_id *of_match_node(const struct of_device_id *matches,
794 const struct device_node *node)
796 const struct of_device_id *match;
799 raw_spin_lock_irqsave(&devtree_lock, flags);
800 match = __of_match_node(matches, node);
801 raw_spin_unlock_irqrestore(&devtree_lock, flags);
804 EXPORT_SYMBOL(of_match_node);
807 * of_find_matching_node_and_match - Find a node based on an of_device_id
809 * @from: The node to start searching from or NULL, the node
810 * you pass will not be searched, only the next one
811 * will; typically, you pass what the previous call
812 * returned. of_node_put() will be called on it
813 * @matches: array of of device match structures to search in
814 * @match Updated to point at the matches entry which matched
816 * Returns a node pointer with refcount incremented, use
817 * of_node_put() on it when done.
819 struct device_node *of_find_matching_node_and_match(struct device_node *from,
820 const struct of_device_id *matches,
821 const struct of_device_id **match)
823 struct device_node *np;
824 const struct of_device_id *m;
830 raw_spin_lock_irqsave(&devtree_lock, flags);
831 np = from ? from->allnext : of_allnodes;
832 for (; np; np = np->allnext) {
833 m = __of_match_node(matches, np);
834 if (m && of_node_get(np)) {
841 raw_spin_unlock_irqrestore(&devtree_lock, flags);
844 EXPORT_SYMBOL(of_find_matching_node_and_match);
847 * of_modalias_node - Lookup appropriate modalias for a device node
848 * @node: pointer to a device tree node
849 * @modalias: Pointer to buffer that modalias value will be copied into
850 * @len: Length of modalias value
852 * Based on the value of the compatible property, this routine will attempt
853 * to choose an appropriate modalias value for a particular device tree node.
854 * It does this by stripping the manufacturer prefix (as delimited by a ',')
855 * from the first entry in the compatible list property.
857 * This routine returns 0 on success, <0 on failure.
859 int of_modalias_node(struct device_node *node, char *modalias, int len)
861 const char *compatible, *p;
864 compatible = of_get_property(node, "compatible", &cplen);
865 if (!compatible || strlen(compatible) > cplen)
867 p = strchr(compatible, ',');
868 strlcpy(modalias, p ? p + 1 : compatible, len);
871 EXPORT_SYMBOL_GPL(of_modalias_node);
874 * of_find_node_by_phandle - Find a node given a phandle
875 * @handle: phandle of the node to find
877 * Returns a node pointer with refcount incremented, use
878 * of_node_put() on it when done.
880 struct device_node *of_find_node_by_phandle(phandle handle)
882 struct device_node *np;
885 raw_spin_lock_irqsave(&devtree_lock, flags);
886 for (np = of_allnodes; np; np = np->allnext)
887 if (np->phandle == handle)
890 raw_spin_unlock_irqrestore(&devtree_lock, flags);
893 EXPORT_SYMBOL(of_find_node_by_phandle);
896 * of_property_count_elems_of_size - Count the number of elements in a property
898 * @np: device node from which the property value is to be read.
899 * @propname: name of the property to be searched.
900 * @elem_size: size of the individual element
902 * Search for a property in a device node and count the number of elements of
903 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
904 * property does not exist or its length does not match a multiple of elem_size
905 * and -ENODATA if the property does not have a value.
907 int of_property_count_elems_of_size(const struct device_node *np,
908 const char *propname, int elem_size)
910 struct property *prop = of_find_property(np, propname, NULL);
917 if (prop->length % elem_size != 0) {
918 pr_err("size of %s in node %s is not a multiple of %d\n",
919 propname, np->full_name, elem_size);
923 return prop->length / elem_size;
925 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
928 * of_find_property_value_of_size
930 * @np: device node from which the property value is to be read.
931 * @propname: name of the property to be searched.
932 * @len: requested length of property value
934 * Search for a property in a device node and valid the requested size.
935 * Returns the property value on success, -EINVAL if the property does not
936 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
937 * property data isn't large enough.
940 static void *of_find_property_value_of_size(const struct device_node *np,
941 const char *propname, u32 len)
943 struct property *prop = of_find_property(np, propname, NULL);
946 return ERR_PTR(-EINVAL);
948 return ERR_PTR(-ENODATA);
949 if (len > prop->length)
950 return ERR_PTR(-EOVERFLOW);
956 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
958 * @np: device node from which the property value is to be read.
959 * @propname: name of the property to be searched.
960 * @index: index of the u32 in the list of values
961 * @out_value: pointer to return value, modified only if no error.
963 * Search for a property in a device node and read nth 32-bit value from
964 * it. Returns 0 on success, -EINVAL if the property does not exist,
965 * -ENODATA if property does not have a value, and -EOVERFLOW if the
966 * property data isn't large enough.
968 * The out_value is modified only if a valid u32 value can be decoded.
970 int of_property_read_u32_index(const struct device_node *np,
971 const char *propname,
972 u32 index, u32 *out_value)
974 const u32 *val = of_find_property_value_of_size(np, propname,
975 ((index + 1) * sizeof(*out_value)));
980 *out_value = be32_to_cpup(((__be32 *)val) + index);
983 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
986 * of_property_read_u8_array - Find and read an array of u8 from a property.
988 * @np: device node from which the property value is to be read.
989 * @propname: name of the property to be searched.
990 * @out_value: pointer to return value, modified only if return value is 0.
991 * @sz: number of array elements to read
993 * Search for a property in a device node and read 8-bit value(s) from
994 * it. Returns 0 on success, -EINVAL if the property does not exist,
995 * -ENODATA if property does not have a value, and -EOVERFLOW if the
996 * property data isn't large enough.
998 * dts entry of array should be like:
999 * property = /bits/ 8 <0x50 0x60 0x70>;
1001 * The out_value is modified only if a valid u8 value can be decoded.
1003 int of_property_read_u8_array(const struct device_node *np,
1004 const char *propname, u8 *out_values, size_t sz)
1006 const u8 *val = of_find_property_value_of_size(np, propname,
1007 (sz * sizeof(*out_values)));
1010 return PTR_ERR(val);
1013 *out_values++ = *val++;
1016 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1019 int of_property_read_u8_array_tp(const struct device_node *np,
1020 const char *propname, u8 *out_values, size_t sz)
1022 const __be32 *val = of_find_property_value_of_size(np, propname,
1023 (sz * sizeof(*out_values)));
1026 return PTR_ERR(val);
1029 *out_values++ = (unsigned char)(be32_to_cpup(val++));
1032 EXPORT_SYMBOL_GPL(of_property_read_u8_array_tp);
1037 * of_property_read_u16_array - Find and read an array of u16 from a property.
1039 * @np: device node from which the property value is to be read.
1040 * @propname: name of the property to be searched.
1041 * @out_value: pointer to return value, modified only if return value is 0.
1042 * @sz: number of array elements to read
1044 * Search for a property in a device node and read 16-bit value(s) from
1045 * it. Returns 0 on success, -EINVAL if the property does not exist,
1046 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1047 * property data isn't large enough.
1049 * dts entry of array should be like:
1050 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1052 * The out_value is modified only if a valid u16 value can be decoded.
1054 int of_property_read_u16_array(const struct device_node *np,
1055 const char *propname, u16 *out_values, size_t sz)
1057 const __be16 *val = of_find_property_value_of_size(np, propname,
1058 (sz * sizeof(*out_values)));
1061 return PTR_ERR(val);
1064 *out_values++ = be16_to_cpup(val++);
1067 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1070 * of_property_read_u32_array - Find and read an array of 32 bit integers
1073 * @np: device node from which the property value is to be read.
1074 * @propname: name of the property to be searched.
1075 * @out_value: pointer to return value, modified only if return value is 0.
1076 * @sz: number of array elements to read
1078 * Search for a property in a device node and read 32-bit value(s) from
1079 * it. Returns 0 on success, -EINVAL if the property does not exist,
1080 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1081 * property data isn't large enough.
1083 * The out_value is modified only if a valid u32 value can be decoded.
1085 int of_property_read_u32_array(const struct device_node *np,
1086 const char *propname, u32 *out_values,
1089 const __be32 *val = of_find_property_value_of_size(np, propname,
1090 (sz * sizeof(*out_values)));
1093 return PTR_ERR(val);
1096 *out_values++ = be32_to_cpup(val++);
1099 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1102 * of_property_read_u64 - Find and read a 64 bit integer from a property
1103 * @np: device node from which the property value is to be read.
1104 * @propname: name of the property to be searched.
1105 * @out_value: pointer to return value, modified only if return value is 0.
1107 * Search for a property in a device node and read a 64-bit value from
1108 * it. Returns 0 on success, -EINVAL if the property does not exist,
1109 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1110 * property data isn't large enough.
1112 * The out_value is modified only if a valid u64 value can be decoded.
1114 int of_property_read_u64(const struct device_node *np, const char *propname,
1117 const __be32 *val = of_find_property_value_of_size(np, propname,
1118 sizeof(*out_value));
1121 return PTR_ERR(val);
1123 *out_value = of_read_number(val, 2);
1126 EXPORT_SYMBOL_GPL(of_property_read_u64);
1129 * of_property_read_string - Find and read a string from a property
1130 * @np: device node from which the property value is to be read.
1131 * @propname: name of the property to be searched.
1132 * @out_string: pointer to null terminated return string, modified only if
1133 * return value is 0.
1135 * Search for a property in a device tree node and retrieve a null
1136 * terminated string value (pointer to data, not a copy). Returns 0 on
1137 * success, -EINVAL if the property does not exist, -ENODATA if property
1138 * does not have a value, and -EILSEQ if the string is not null-terminated
1139 * within the length of the property data.
1141 * The out_string pointer is modified only if a valid string can be decoded.
1143 int of_property_read_string(struct device_node *np, const char *propname,
1144 const char **out_string)
1146 struct property *prop = of_find_property(np, propname, NULL);
1151 if (strnlen(prop->value, prop->length) >= prop->length)
1153 *out_string = prop->value;
1156 EXPORT_SYMBOL_GPL(of_property_read_string);
1159 * of_property_match_string() - Find string in a list and return index
1160 * @np: pointer to node containing string list property
1161 * @propname: string list property name
1162 * @string: pointer to string to search for in string list
1164 * This function searches a string list property and returns the index
1165 * of a specific string value.
1167 int of_property_match_string(struct device_node *np, const char *propname,
1170 struct property *prop = of_find_property(np, propname, NULL);
1173 const char *p, *end;
1181 end = p + prop->length;
1183 for (i = 0; p < end; i++, p += l) {
1184 l = strnlen(p, end - p) + 1;
1187 pr_debug("comparing %s with %s\n", string, p);
1188 if (strcmp(string, p) == 0)
1189 return i; /* Found it; return index */
1193 EXPORT_SYMBOL_GPL(of_property_match_string);
1196 * of_property_read_string_util() - Utility helper for parsing string properties
1197 * @np: device node from which the property value is to be read.
1198 * @propname: name of the property to be searched.
1199 * @out_strs: output array of string pointers.
1200 * @sz: number of array elements to read.
1201 * @skip: Number of strings to skip over at beginning of list.
1203 * Don't call this function directly. It is a utility helper for the
1204 * of_property_read_string*() family of functions.
1206 int of_property_read_string_helper(struct device_node *np, const char *propname,
1207 const char **out_strs, size_t sz, int skip)
1209 struct property *prop = of_find_property(np, propname, NULL);
1211 const char *p, *end;
1218 end = p + prop->length;
1220 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1221 l = strnlen(p, end - p) + 1;
1224 if (out_strs && i >= skip)
1228 return i <= 0 ? -ENODATA : i;
1230 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
1232 static int __of_parse_phandle_with_args(const struct device_node *np,
1233 const char *list_name,
1234 const char *cells_name,
1235 int cell_count, int index,
1236 struct of_phandle_args *out_args)
1238 const __be32 *list, *list_end;
1239 int rc = 0, size, cur_index = 0;
1241 struct device_node *node = NULL;
1244 /* Retrieve the phandle list property */
1245 list = of_get_property(np, list_name, &size);
1248 list_end = list + size / sizeof(*list);
1250 /* Loop over the phandles until all the requested entry is found */
1251 while (list < list_end) {
1256 * If phandle is 0, then it is an empty entry with no
1257 * arguments. Skip forward to the next entry.
1259 phandle = be32_to_cpup(list++);
1262 * Find the provider node and parse the #*-cells
1263 * property to determine the argument length.
1265 * This is not needed if the cell count is hard-coded
1266 * (i.e. cells_name not set, but cell_count is set),
1267 * except when we're going to return the found node
1270 if (cells_name || cur_index == index) {
1271 node = of_find_node_by_phandle(phandle);
1273 pr_err("%s: could not find phandle\n",
1280 if (of_property_read_u32(node, cells_name,
1282 pr_err("%s: could not get %s for %s\n",
1283 np->full_name, cells_name,
1292 * Make sure that the arguments actually fit in the
1293 * remaining property data length
1295 if (list + count > list_end) {
1296 pr_err("%s: arguments longer than property\n",
1303 * All of the error cases above bail out of the loop, so at
1304 * this point, the parsing is successful. If the requested
1305 * index matches, then fill the out_args structure and return,
1306 * or return -ENOENT for an empty entry.
1309 if (cur_index == index) {
1315 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1316 count = MAX_PHANDLE_ARGS;
1317 out_args->np = node;
1318 out_args->args_count = count;
1319 for (i = 0; i < count; i++)
1320 out_args->args[i] = be32_to_cpup(list++);
1325 /* Found it! return success */
1336 * Unlock node before returning result; will be one of:
1337 * -ENOENT : index is for empty phandle
1338 * -EINVAL : parsing error on data
1339 * [1..n] : Number of phandle (count mode; when index = -1)
1341 rc = index < 0 ? cur_index : -ENOENT;
1349 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1350 * @np: Pointer to device node holding phandle property
1351 * @phandle_name: Name of property holding a phandle value
1352 * @index: For properties holding a table of phandles, this is the index into
1355 * Returns the device_node pointer with refcount incremented. Use
1356 * of_node_put() on it when done.
1358 struct device_node *of_parse_phandle(const struct device_node *np,
1359 const char *phandle_name, int index)
1361 struct of_phandle_args args;
1366 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1372 EXPORT_SYMBOL(of_parse_phandle);
1375 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1376 * @np: pointer to a device tree node containing a list
1377 * @list_name: property name that contains a list
1378 * @cells_name: property name that specifies phandles' arguments count
1379 * @index: index of a phandle to parse out
1380 * @out_args: optional pointer to output arguments structure (will be filled)
1382 * This function is useful to parse lists of phandles and their arguments.
1383 * Returns 0 on success and fills out_args, on error returns appropriate
1386 * Caller is responsible to call of_node_put() on the returned out_args->node
1392 * #list-cells = <2>;
1396 * #list-cells = <1>;
1400 * list = <&phandle1 1 2 &phandle2 3>;
1403 * To get a device_node of the `node2' node you may call this:
1404 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1406 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1407 const char *cells_name, int index,
1408 struct of_phandle_args *out_args)
1412 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1415 EXPORT_SYMBOL(of_parse_phandle_with_args);
1418 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1419 * @np: pointer to a device tree node containing a list
1420 * @list_name: property name that contains a list
1421 * @cell_count: number of argument cells following the phandle
1422 * @index: index of a phandle to parse out
1423 * @out_args: optional pointer to output arguments structure (will be filled)
1425 * This function is useful to parse lists of phandles and their arguments.
1426 * Returns 0 on success and fills out_args, on error returns appropriate
1429 * Caller is responsible to call of_node_put() on the returned out_args->node
1441 * list = <&phandle1 0 2 &phandle2 2 3>;
1444 * To get a device_node of the `node2' node you may call this:
1445 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1447 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1448 const char *list_name, int cell_count,
1449 int index, struct of_phandle_args *out_args)
1453 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1456 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1459 * of_count_phandle_with_args() - Find the number of phandles references in a property
1460 * @np: pointer to a device tree node containing a list
1461 * @list_name: property name that contains a list
1462 * @cells_name: property name that specifies phandles' arguments count
1464 * Returns the number of phandle + argument tuples within a property. It
1465 * is a typical pattern to encode a list of phandle and variable
1466 * arguments into a single property. The number of arguments is encoded
1467 * by a property in the phandle-target node. For example, a gpios
1468 * property would contain a list of GPIO specifies consisting of a
1469 * phandle and 1 or more arguments. The number of arguments are
1470 * determined by the #gpio-cells property in the node pointed to by the
1473 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1474 const char *cells_name)
1476 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1479 EXPORT_SYMBOL(of_count_phandle_with_args);
1482 * __of_add_property - Add a property to a node without lock operations
1484 int __of_add_property(struct device_node *np, struct property *prop)
1486 struct property **next;
1489 next = &np->properties;
1491 if (strcmp(prop->name, (*next)->name) == 0)
1492 /* duplicate ! don't insert it */
1495 next = &(*next)->next;
1503 * of_add_property - Add a property to a node
1505 int of_add_property(struct device_node *np, struct property *prop)
1507 unsigned long flags;
1510 mutex_lock(&of_mutex);
1512 raw_spin_lock_irqsave(&devtree_lock, flags);
1513 rc = __of_add_property(np, prop);
1514 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1517 __of_add_property_sysfs(np, prop);
1519 mutex_unlock(&of_mutex);
1522 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1527 int __of_remove_property(struct device_node *np, struct property *prop)
1529 struct property **next;
1531 for (next = &np->properties; *next; next = &(*next)->next) {
1538 /* found the node */
1540 prop->next = np->deadprops;
1541 np->deadprops = prop;
1546 void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1548 /* at early boot, bail here and defer setup to of_init() */
1549 if (of_kset && of_node_is_attached(np))
1550 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1554 * of_remove_property - Remove a property from a node.
1556 * Note that we don't actually remove it, since we have given out
1557 * who-knows-how-many pointers to the data using get-property.
1558 * Instead we just move the property to the "dead properties"
1559 * list, so it won't be found any more.
1561 int of_remove_property(struct device_node *np, struct property *prop)
1563 unsigned long flags;
1566 mutex_lock(&of_mutex);
1568 raw_spin_lock_irqsave(&devtree_lock, flags);
1569 rc = __of_remove_property(np, prop);
1570 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1573 __of_remove_property_sysfs(np, prop);
1575 mutex_unlock(&of_mutex);
1578 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1583 int __of_update_property(struct device_node *np, struct property *newprop,
1584 struct property **oldpropp)
1586 struct property **next, *oldprop;
1588 for (next = &np->properties; *next; next = &(*next)->next) {
1589 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1592 *oldpropp = oldprop = *next;
1595 /* replace the node */
1596 newprop->next = oldprop->next;
1598 oldprop->next = np->deadprops;
1599 np->deadprops = oldprop;
1602 newprop->next = NULL;
1609 void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1610 struct property *oldprop)
1612 /* At early boot, bail out and defer setup to of_init() */
1617 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1618 __of_add_property_sysfs(np, newprop);
1622 * of_update_property - Update a property in a node, if the property does
1623 * not exist, add it.
1625 * Note that we don't actually remove it, since we have given out
1626 * who-knows-how-many pointers to the data using get-property.
1627 * Instead we just move the property to the "dead properties" list,
1628 * and add the new property to the property list
1630 int of_update_property(struct device_node *np, struct property *newprop)
1632 struct property *oldprop;
1633 unsigned long flags;
1639 mutex_lock(&of_mutex);
1641 raw_spin_lock_irqsave(&devtree_lock, flags);
1642 rc = __of_update_property(np, newprop, &oldprop);
1643 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1646 __of_update_property_sysfs(np, newprop, oldprop);
1648 mutex_unlock(&of_mutex);
1651 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1656 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1657 int id, const char *stem, int stem_len)
1661 strncpy(ap->stem, stem, stem_len);
1662 ap->stem[stem_len] = 0;
1663 list_add_tail(&ap->link, &aliases_lookup);
1664 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1665 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1669 * of_alias_scan - Scan all properties of 'aliases' node
1671 * The function scans all the properties of 'aliases' node and populate
1672 * the the global lookup table with the properties. It returns the
1673 * number of alias_prop found, or error code in error case.
1675 * @dt_alloc: An allocator that provides a virtual address to memory
1676 * for the resulting tree
1678 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1680 struct property *pp;
1682 of_chosen = of_find_node_by_path("/chosen");
1683 if (of_chosen == NULL)
1684 of_chosen = of_find_node_by_path("/chosen@0");
1689 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1691 of_stdout = of_find_node_by_path(name);
1694 of_aliases = of_find_node_by_path("/aliases");
1698 for_each_property_of_node(of_aliases, pp) {
1699 const char *start = pp->name;
1700 const char *end = start + strlen(start);
1701 struct device_node *np;
1702 struct alias_prop *ap;
1705 /* Skip those we do not want to proceed */
1706 if (!strcmp(pp->name, "name") ||
1707 !strcmp(pp->name, "phandle") ||
1708 !strcmp(pp->name, "linux,phandle"))
1711 np = of_find_node_by_path(pp->value);
1715 /* walk the alias backwards to extract the id and work out
1716 * the 'stem' string */
1717 while (isdigit(*(end-1)) && end > start)
1721 if (kstrtoint(end, 10, &id) < 0)
1724 /* Allocate an alias_prop with enough space for the stem */
1725 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1728 memset(ap, 0, sizeof(*ap) + len + 1);
1730 of_alias_add(ap, np, id, start, len);
1735 * of_alias_get_id - Get alias id for the given device_node
1736 * @np: Pointer to the given device_node
1737 * @stem: Alias stem of the given device_node
1739 * The function travels the lookup table to get alias id for the given
1740 * device_node and alias stem. It returns the alias id if find it.
1742 int of_alias_get_id(struct device_node *np, const char *stem)
1744 struct alias_prop *app;
1747 mutex_lock(&of_mutex);
1748 list_for_each_entry(app, &aliases_lookup, link) {
1749 if (strcmp(app->stem, stem) != 0)
1752 if (np == app->np) {
1757 mutex_unlock(&of_mutex);
1761 EXPORT_SYMBOL_GPL(of_alias_get_id);
1763 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1766 const void *curv = cur;
1776 curv += sizeof(*cur);
1777 if (curv >= prop->value + prop->length)
1781 *pu = be32_to_cpup(curv);
1784 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1786 const char *of_prop_next_string(struct property *prop, const char *cur)
1788 const void *curv = cur;
1796 curv += strlen(cur) + 1;
1797 if (curv >= prop->value + prop->length)
1802 EXPORT_SYMBOL_GPL(of_prop_next_string);
1805 * of_device_is_stdout_path - check if a device node matches the
1806 * linux,stdout-path property
1808 * Check if this device node matches the linux,stdout-path property
1809 * in the chosen node. return true if yes, false otherwise.
1811 int of_device_is_stdout_path(struct device_node *dn)
1816 return of_stdout == dn;
1818 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
1821 * of_graph_parse_endpoint() - parse common endpoint node properties
1822 * @node: pointer to endpoint device_node
1823 * @endpoint: pointer to the OF endpoint data structure
1825 * The caller should hold a reference to @node.
1827 int of_graph_parse_endpoint(const struct device_node *node,
1828 struct of_endpoint *endpoint)
1830 struct device_node *port_node = of_get_parent(node);
1832 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
1833 __func__, node->full_name);
1835 memset(endpoint, 0, sizeof(*endpoint));
1837 endpoint->local_node = node;
1839 * It doesn't matter whether the two calls below succeed.
1840 * If they don't then the default value 0 is used.
1842 of_property_read_u32(port_node, "reg", &endpoint->port);
1843 of_property_read_u32(node, "reg", &endpoint->id);
1845 of_node_put(port_node);
1849 EXPORT_SYMBOL(of_graph_parse_endpoint);
1852 * of_graph_get_next_endpoint() - get next endpoint node
1853 * @parent: pointer to the parent device node
1854 * @prev: previous endpoint node, or NULL to get first
1856 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
1857 * of the passed @prev node is not decremented, the caller have to use
1858 * of_node_put() on it when done.
1860 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
1861 struct device_node *prev)
1863 struct device_node *endpoint;
1864 struct device_node *port;
1870 * Start by locating the port node. If no previous endpoint is specified
1871 * search for the first port node, otherwise get the previous endpoint
1875 struct device_node *node;
1877 node = of_get_child_by_name(parent, "ports");
1881 port = of_get_child_by_name(parent, "port");
1885 pr_err("%s(): no port node found in %s\n",
1886 __func__, parent->full_name);
1890 port = of_get_parent(prev);
1891 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
1892 __func__, prev->full_name))
1896 * Avoid dropping prev node refcount to 0 when getting the next
1904 * Now that we have a port node, get the next endpoint by
1905 * getting the next child. If the previous endpoint is NULL this
1906 * will return the first child.
1908 endpoint = of_get_next_child(port, prev);
1914 /* No more endpoints under this port, try the next one. */
1918 port = of_get_next_child(parent, port);
1921 } while (of_node_cmp(port->name, "port"));
1924 EXPORT_SYMBOL(of_graph_get_next_endpoint);
1927 * of_graph_get_remote_port_parent() - get remote port's parent node
1928 * @node: pointer to a local endpoint device_node
1930 * Return: Remote device node associated with remote endpoint node linked
1931 * to @node. Use of_node_put() on it when done.
1933 struct device_node *of_graph_get_remote_port_parent(
1934 const struct device_node *node)
1936 struct device_node *np;
1939 /* Get remote endpoint node. */
1940 np = of_parse_phandle(node, "remote-endpoint", 0);
1942 /* Walk 3 levels up only if there is 'ports' node. */
1943 for (depth = 3; depth && np; depth--) {
1944 np = of_get_next_parent(np);
1945 if (depth == 2 && of_node_cmp(np->name, "ports"))
1950 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
1953 * of_graph_get_remote_port() - get remote port node
1954 * @node: pointer to a local endpoint device_node
1956 * Return: Remote port node associated with remote endpoint node linked
1957 * to @node. Use of_node_put() on it when done.
1959 struct device_node *of_graph_get_remote_port(const struct device_node *node)
1961 struct device_node *np;
1963 /* Get remote endpoint node. */
1964 np = of_parse_phandle(node, "remote-endpoint", 0);
1967 return of_get_next_parent(np);
1969 EXPORT_SYMBOL(of_graph_get_remote_port);