2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 #include <linux/ctype.h>
21 #include <linux/cpu.h>
22 #include <linux/module.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/proc_fs.h>
28 #include "of_private.h"
30 LIST_HEAD(aliases_lookup);
32 struct device_node *of_allnodes;
33 EXPORT_SYMBOL(of_allnodes);
34 struct device_node *of_chosen;
35 struct device_node *of_aliases;
37 DEFINE_MUTEX(of_aliases_mutex);
39 /* use when traversing tree through the allnext, child, sibling,
40 * or parent members of struct device_node.
42 DEFINE_RAW_SPINLOCK(devtree_lock);
44 int of_n_addr_cells(struct device_node *np)
51 ip = of_get_property(np, "#address-cells", NULL);
53 return be32_to_cpup(ip);
55 /* No #address-cells property for the root node */
56 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
58 EXPORT_SYMBOL(of_n_addr_cells);
60 int of_n_size_cells(struct device_node *np)
67 ip = of_get_property(np, "#size-cells", NULL);
69 return be32_to_cpup(ip);
71 /* No #size-cells property for the root node */
72 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
74 EXPORT_SYMBOL(of_n_size_cells);
76 #if defined(CONFIG_OF_DYNAMIC)
78 * of_node_get - Increment refcount of a node
79 * @node: Node to inc refcount, NULL is supported to
80 * simplify writing of callers
84 struct device_node *of_node_get(struct device_node *node)
87 kref_get(&node->kref);
90 EXPORT_SYMBOL(of_node_get);
92 static inline struct device_node *kref_to_device_node(struct kref *kref)
94 return container_of(kref, struct device_node, kref);
98 * of_node_release - release a dynamically allocated node
99 * @kref: kref element of the node to be released
101 * In of_node_put() this function is passed to kref_put()
104 static void of_node_release(struct kref *kref)
106 struct device_node *node = kref_to_device_node(kref);
107 struct property *prop = node->properties;
109 /* We should never be releasing nodes that haven't been detached. */
110 if (!of_node_check_flag(node, OF_DETACHED)) {
111 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
113 kref_init(&node->kref);
117 if (!of_node_check_flag(node, OF_DYNAMIC))
121 struct property *next = prop->next;
128 prop = node->deadprops;
129 node->deadprops = NULL;
132 kfree(node->full_name);
138 * of_node_put - Decrement refcount of a node
139 * @node: Node to dec refcount, NULL is supported to
140 * simplify writing of callers
143 void of_node_put(struct device_node *node)
146 kref_put(&node->kref, of_node_release);
148 EXPORT_SYMBOL(of_node_put);
149 #endif /* CONFIG_OF_DYNAMIC */
151 static struct property *__of_find_property(const struct device_node *np,
152 const char *name, int *lenp)
159 for (pp = np->properties; pp; pp = pp->next) {
160 if (of_prop_cmp(pp->name, name) == 0) {
170 struct property *of_find_property(const struct device_node *np,
177 raw_spin_lock_irqsave(&devtree_lock, flags);
178 pp = __of_find_property(np, name, lenp);
179 raw_spin_unlock_irqrestore(&devtree_lock, flags);
183 EXPORT_SYMBOL(of_find_property);
186 * of_find_all_nodes - Get next node in global list
187 * @prev: Previous node or NULL to start iteration
188 * of_node_put() will be called on it
190 * Returns a node pointer with refcount incremented, use
191 * of_node_put() on it when done.
193 struct device_node *of_find_all_nodes(struct device_node *prev)
195 struct device_node *np;
198 raw_spin_lock_irqsave(&devtree_lock, flags);
199 np = prev ? prev->allnext : of_allnodes;
200 for (; np != NULL; np = np->allnext)
204 raw_spin_unlock_irqrestore(&devtree_lock, flags);
207 EXPORT_SYMBOL(of_find_all_nodes);
210 * Find a property with a given name for a given node
211 * and return the value.
213 static const void *__of_get_property(const struct device_node *np,
214 const char *name, int *lenp)
216 struct property *pp = __of_find_property(np, name, lenp);
218 return pp ? pp->value : NULL;
222 * Find a property with a given name for a given node
223 * and return the value.
225 const void *of_get_property(const struct device_node *np, const char *name,
228 struct property *pp = of_find_property(np, name, lenp);
230 return pp ? pp->value : NULL;
232 EXPORT_SYMBOL(of_get_property);
235 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
237 * @cpu: logical cpu index of a core/thread
238 * @phys_id: physical identifier of a core/thread
240 * CPU logical to physical index mapping is architecture specific.
241 * However this __weak function provides a default match of physical
242 * id to logical cpu index. phys_id provided here is usually values read
243 * from the device tree which must match the hardware internal registers.
245 * Returns true if the physical identifier and the logical cpu index
246 * correspond to the same core/thread, false otherwise.
248 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
250 return (u32)phys_id == cpu;
254 * Checks if the given "prop_name" property holds the physical id of the
255 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
256 * NULL, local thread number within the core is returned in it.
258 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
259 const char *prop_name, int cpu, unsigned int *thread)
262 int ac, prop_len, tid;
265 ac = of_n_addr_cells(cpun);
266 cell = of_get_property(cpun, prop_name, &prop_len);
269 prop_len /= sizeof(*cell);
270 for (tid = 0; tid < prop_len; tid++) {
271 hwid = of_read_number(cell, ac);
272 if (arch_match_cpu_phys_id(cpu, hwid)) {
283 * of_get_cpu_node - Get device node associated with the given logical CPU
285 * @cpu: CPU number(logical index) for which device node is required
286 * @thread: if not NULL, local thread number within the physical core is
289 * The main purpose of this function is to retrieve the device node for the
290 * given logical CPU index. It should be used to initialize the of_node in
291 * cpu device. Once of_node in cpu device is populated, all the further
292 * references can use that instead.
294 * CPU logical to physical index mapping is architecture specific and is built
295 * before booting secondary cores. This function uses arch_match_cpu_phys_id
296 * which can be overridden by architecture specific implementation.
298 * Returns a node pointer for the logical cpu if found, else NULL.
300 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
302 struct device_node *cpun, *cpus;
304 cpus = of_find_node_by_path("/cpus");
306 pr_warn("Missing cpus node, bailing out\n");
310 for_each_child_of_node(cpus, cpun) {
311 if (of_node_cmp(cpun->type, "cpu"))
313 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
314 * for thread ids on PowerPC. If it doesn't exist fallback to
315 * standard "reg" property.
317 if (IS_ENABLED(CONFIG_PPC) &&
318 __of_find_n_match_cpu_property(cpun,
319 "ibm,ppc-interrupt-server#s", cpu, thread))
321 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
326 EXPORT_SYMBOL(of_get_cpu_node);
328 /** Checks if the given "compat" string matches one of the strings in
329 * the device's "compatible" property
331 static int __of_device_is_compatible(const struct device_node *device,
337 cp = __of_get_property(device, "compatible", &cplen);
341 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
351 /** Checks if the given "compat" string matches one of the strings in
352 * the device's "compatible" property
354 int of_device_is_compatible(const struct device_node *device,
360 raw_spin_lock_irqsave(&devtree_lock, flags);
361 res = __of_device_is_compatible(device, compat);
362 raw_spin_unlock_irqrestore(&devtree_lock, flags);
365 EXPORT_SYMBOL(of_device_is_compatible);
368 * of_machine_is_compatible - Test root of device tree for a given compatible value
369 * @compat: compatible string to look for in root node's compatible property.
371 * Returns true if the root node has the given value in its
372 * compatible property.
374 int of_machine_is_compatible(const char *compat)
376 struct device_node *root;
379 root = of_find_node_by_path("/");
381 rc = of_device_is_compatible(root, compat);
386 EXPORT_SYMBOL(of_machine_is_compatible);
389 * __of_device_is_available - check if a device is available for use
391 * @device: Node to check for availability, with locks already held
393 * Returns 1 if the status property is absent or set to "okay" or "ok",
396 static int __of_device_is_available(const struct device_node *device)
401 status = __of_get_property(device, "status", &statlen);
406 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
414 * of_device_is_available - check if a device is available for use
416 * @device: Node to check for availability
418 * Returns 1 if the status property is absent or set to "okay" or "ok",
421 int of_device_is_available(const struct device_node *device)
426 raw_spin_lock_irqsave(&devtree_lock, flags);
427 res = __of_device_is_available(device);
428 raw_spin_unlock_irqrestore(&devtree_lock, flags);
432 EXPORT_SYMBOL(of_device_is_available);
435 * of_get_parent - Get a node's parent if any
436 * @node: Node to get parent
438 * Returns a node pointer with refcount incremented, use
439 * of_node_put() on it when done.
441 struct device_node *of_get_parent(const struct device_node *node)
443 struct device_node *np;
449 raw_spin_lock_irqsave(&devtree_lock, flags);
450 np = of_node_get(node->parent);
451 raw_spin_unlock_irqrestore(&devtree_lock, flags);
454 EXPORT_SYMBOL(of_get_parent);
457 * of_get_next_parent - Iterate to a node's parent
458 * @node: Node to get parent of
460 * This is like of_get_parent() except that it drops the
461 * refcount on the passed node, making it suitable for iterating
462 * through a node's parents.
464 * Returns a node pointer with refcount incremented, use
465 * of_node_put() on it when done.
467 struct device_node *of_get_next_parent(struct device_node *node)
469 struct device_node *parent;
475 raw_spin_lock_irqsave(&devtree_lock, flags);
476 parent = of_node_get(node->parent);
478 raw_spin_unlock_irqrestore(&devtree_lock, flags);
481 EXPORT_SYMBOL(of_get_next_parent);
484 * of_get_next_child - Iterate a node childs
486 * @prev: previous child of the parent node, or NULL to get first
488 * Returns a node pointer with refcount incremented, use
489 * of_node_put() on it when done.
491 struct device_node *of_get_next_child(const struct device_node *node,
492 struct device_node *prev)
494 struct device_node *next;
497 raw_spin_lock_irqsave(&devtree_lock, flags);
498 next = prev ? prev->sibling : node->child;
499 for (; next; next = next->sibling)
500 if (of_node_get(next))
503 raw_spin_unlock_irqrestore(&devtree_lock, flags);
506 EXPORT_SYMBOL(of_get_next_child);
509 * of_get_next_available_child - Find the next available child node
511 * @prev: previous child of the parent node, or NULL to get first
513 * This function is like of_get_next_child(), except that it
514 * automatically skips any disabled nodes (i.e. status = "disabled").
516 struct device_node *of_get_next_available_child(const struct device_node *node,
517 struct device_node *prev)
519 struct device_node *next;
522 raw_spin_lock_irqsave(&devtree_lock, flags);
523 next = prev ? prev->sibling : node->child;
524 for (; next; next = next->sibling) {
525 if (!__of_device_is_available(next))
527 if (of_node_get(next))
531 raw_spin_unlock_irqrestore(&devtree_lock, flags);
534 EXPORT_SYMBOL(of_get_next_available_child);
537 * of_get_child_by_name - Find the child node by name for a given parent
539 * @name: child name to look for.
541 * This function looks for child node for given matching name
543 * Returns a node pointer if found, with refcount incremented, use
544 * of_node_put() on it when done.
545 * Returns NULL if node is not found.
547 struct device_node *of_get_child_by_name(const struct device_node *node,
550 struct device_node *child;
552 for_each_child_of_node(node, child)
553 if (child->name && (of_node_cmp(child->name, name) == 0))
557 EXPORT_SYMBOL(of_get_child_by_name);
560 * of_find_node_by_path - Find a node matching a full OF path
561 * @path: The full path to match
563 * Returns a node pointer with refcount incremented, use
564 * of_node_put() on it when done.
566 struct device_node *of_find_node_by_path(const char *path)
568 struct device_node *np = of_allnodes;
571 raw_spin_lock_irqsave(&devtree_lock, flags);
572 for (; np; np = np->allnext) {
573 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
577 raw_spin_unlock_irqrestore(&devtree_lock, flags);
580 EXPORT_SYMBOL(of_find_node_by_path);
583 * of_find_node_by_name - Find a node by its "name" property
584 * @from: The node to start searching from or NULL, the node
585 * you pass will not be searched, only the next one
586 * will; typically, you pass what the previous call
587 * returned. of_node_put() will be called on it
588 * @name: The name string to match against
590 * Returns a node pointer with refcount incremented, use
591 * of_node_put() on it when done.
593 struct device_node *of_find_node_by_name(struct device_node *from,
596 struct device_node *np;
599 raw_spin_lock_irqsave(&devtree_lock, flags);
600 np = from ? from->allnext : of_allnodes;
601 for (; np; np = np->allnext)
602 if (np->name && (of_node_cmp(np->name, name) == 0)
606 raw_spin_unlock_irqrestore(&devtree_lock, flags);
609 EXPORT_SYMBOL(of_find_node_by_name);
612 * of_find_node_by_type - Find a node by its "device_type" property
613 * @from: The node to start searching from, or NULL to start searching
614 * the entire device tree. The node you pass will not be
615 * searched, only the next one will; typically, you pass
616 * what the previous call returned. of_node_put() will be
617 * called on from for you.
618 * @type: The type string to match against
620 * Returns a node pointer with refcount incremented, use
621 * of_node_put() on it when done.
623 struct device_node *of_find_node_by_type(struct device_node *from,
626 struct device_node *np;
629 raw_spin_lock_irqsave(&devtree_lock, flags);
630 np = from ? from->allnext : of_allnodes;
631 for (; np; np = np->allnext)
632 if (np->type && (of_node_cmp(np->type, type) == 0)
636 raw_spin_unlock_irqrestore(&devtree_lock, flags);
639 EXPORT_SYMBOL(of_find_node_by_type);
642 * of_find_compatible_node - Find a node based on type and one of the
643 * tokens in its "compatible" property
644 * @from: The node to start searching from or NULL, the node
645 * you pass will not be searched, only the next one
646 * will; typically, you pass what the previous call
647 * returned. of_node_put() will be called on it
648 * @type: The type string to match "device_type" or NULL to ignore
649 * @compatible: The string to match to one of the tokens in the device
652 * Returns a node pointer with refcount incremented, use
653 * of_node_put() on it when done.
655 struct device_node *of_find_compatible_node(struct device_node *from,
656 const char *type, const char *compatible)
658 struct device_node *np;
661 raw_spin_lock_irqsave(&devtree_lock, flags);
662 np = from ? from->allnext : of_allnodes;
663 for (; np; np = np->allnext) {
665 && !(np->type && (of_node_cmp(np->type, type) == 0)))
667 if (__of_device_is_compatible(np, compatible) &&
672 raw_spin_unlock_irqrestore(&devtree_lock, flags);
675 EXPORT_SYMBOL(of_find_compatible_node);
678 * of_find_node_with_property - Find a node which has a property with
680 * @from: The node to start searching from or NULL, the node
681 * you pass will not be searched, only the next one
682 * will; typically, you pass what the previous call
683 * returned. of_node_put() will be called on it
684 * @prop_name: The name of the property to look for.
686 * Returns a node pointer with refcount incremented, use
687 * of_node_put() on it when done.
689 struct device_node *of_find_node_with_property(struct device_node *from,
690 const char *prop_name)
692 struct device_node *np;
696 raw_spin_lock_irqsave(&devtree_lock, flags);
697 np = from ? from->allnext : of_allnodes;
698 for (; np; np = np->allnext) {
699 for (pp = np->properties; pp; pp = pp->next) {
700 if (of_prop_cmp(pp->name, prop_name) == 0) {
708 raw_spin_unlock_irqrestore(&devtree_lock, flags);
711 EXPORT_SYMBOL(of_find_node_with_property);
714 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
715 const struct device_node *node)
720 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
722 if (matches->name[0])
724 && !strcmp(matches->name, node->name);
725 if (matches->type[0])
727 && !strcmp(matches->type, node->type);
728 if (matches->compatible[0])
729 match &= __of_device_is_compatible(node,
730 matches->compatible);
739 * of_match_node - Tell if an device_node has a matching of_match structure
740 * @matches: array of of device match structures to search in
741 * @node: the of device structure to match against
743 * Low level utility function used by device matching.
745 const struct of_device_id *of_match_node(const struct of_device_id *matches,
746 const struct device_node *node)
748 const struct of_device_id *match;
751 raw_spin_lock_irqsave(&devtree_lock, flags);
752 match = __of_match_node(matches, node);
753 raw_spin_unlock_irqrestore(&devtree_lock, flags);
756 EXPORT_SYMBOL(of_match_node);
759 * of_find_matching_node_and_match - Find a node based on an of_device_id
761 * @from: The node to start searching from or NULL, the node
762 * you pass will not be searched, only the next one
763 * will; typically, you pass what the previous call
764 * returned. of_node_put() will be called on it
765 * @matches: array of of device match structures to search in
766 * @match Updated to point at the matches entry which matched
768 * Returns a node pointer with refcount incremented, use
769 * of_node_put() on it when done.
771 struct device_node *of_find_matching_node_and_match(struct device_node *from,
772 const struct of_device_id *matches,
773 const struct of_device_id **match)
775 struct device_node *np;
776 const struct of_device_id *m;
782 raw_spin_lock_irqsave(&devtree_lock, flags);
783 np = from ? from->allnext : of_allnodes;
784 for (; np; np = np->allnext) {
785 m = __of_match_node(matches, np);
786 if (m && of_node_get(np)) {
793 raw_spin_unlock_irqrestore(&devtree_lock, flags);
796 EXPORT_SYMBOL(of_find_matching_node_and_match);
799 * of_modalias_node - Lookup appropriate modalias for a device node
800 * @node: pointer to a device tree node
801 * @modalias: Pointer to buffer that modalias value will be copied into
802 * @len: Length of modalias value
804 * Based on the value of the compatible property, this routine will attempt
805 * to choose an appropriate modalias value for a particular device tree node.
806 * It does this by stripping the manufacturer prefix (as delimited by a ',')
807 * from the first entry in the compatible list property.
809 * This routine returns 0 on success, <0 on failure.
811 int of_modalias_node(struct device_node *node, char *modalias, int len)
813 const char *compatible, *p;
816 compatible = of_get_property(node, "compatible", &cplen);
817 if (!compatible || strlen(compatible) > cplen)
819 p = strchr(compatible, ',');
820 strlcpy(modalias, p ? p + 1 : compatible, len);
823 EXPORT_SYMBOL_GPL(of_modalias_node);
826 * of_find_node_by_phandle - Find a node given a phandle
827 * @handle: phandle of the node to find
829 * Returns a node pointer with refcount incremented, use
830 * of_node_put() on it when done.
832 struct device_node *of_find_node_by_phandle(phandle handle)
834 struct device_node *np;
837 raw_spin_lock_irqsave(&devtree_lock, flags);
838 for (np = of_allnodes; np; np = np->allnext)
839 if (np->phandle == handle)
842 raw_spin_unlock_irqrestore(&devtree_lock, flags);
845 EXPORT_SYMBOL(of_find_node_by_phandle);
848 * of_find_property_value_of_size
850 * @np: device node from which the property value is to be read.
851 * @propname: name of the property to be searched.
852 * @len: requested length of property value
854 * Search for a property in a device node and valid the requested size.
855 * Returns the property value on success, -EINVAL if the property does not
856 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
857 * property data isn't large enough.
860 static void *of_find_property_value_of_size(const struct device_node *np,
861 const char *propname, u32 len)
863 struct property *prop = of_find_property(np, propname, NULL);
866 return ERR_PTR(-EINVAL);
868 return ERR_PTR(-ENODATA);
869 if (len > prop->length)
870 return ERR_PTR(-EOVERFLOW);
876 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
878 * @np: device node from which the property value is to be read.
879 * @propname: name of the property to be searched.
880 * @index: index of the u32 in the list of values
881 * @out_value: pointer to return value, modified only if no error.
883 * Search for a property in a device node and read nth 32-bit value from
884 * it. Returns 0 on success, -EINVAL if the property does not exist,
885 * -ENODATA if property does not have a value, and -EOVERFLOW if the
886 * property data isn't large enough.
888 * The out_value is modified only if a valid u32 value can be decoded.
890 int of_property_read_u32_index(const struct device_node *np,
891 const char *propname,
892 u32 index, u32 *out_value)
894 const u32 *val = of_find_property_value_of_size(np, propname,
895 ((index + 1) * sizeof(*out_value)));
900 *out_value = be32_to_cpup(((__be32 *)val) + index);
903 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
906 * of_property_read_u8_array - Find and read an array of u8 from a property.
908 * @np: device node from which the property value is to be read.
909 * @propname: name of the property to be searched.
910 * @out_value: pointer to return value, modified only if return value is 0.
911 * @sz: number of array elements to read
913 * Search for a property in a device node and read 8-bit value(s) from
914 * it. Returns 0 on success, -EINVAL if the property does not exist,
915 * -ENODATA if property does not have a value, and -EOVERFLOW if the
916 * property data isn't large enough.
918 * dts entry of array should be like:
919 * property = /bits/ 8 <0x50 0x60 0x70>;
921 * The out_value is modified only if a valid u8 value can be decoded.
923 int of_property_read_u8_array(const struct device_node *np,
924 const char *propname, u8 *out_values, size_t sz)
926 const u8 *val = of_find_property_value_of_size(np, propname,
927 (sz * sizeof(*out_values)));
933 *out_values++ = *val++;
936 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
939 * of_property_read_u16_array - Find and read an array of u16 from a property.
941 * @np: device node from which the property value is to be read.
942 * @propname: name of the property to be searched.
943 * @out_value: pointer to return value, modified only if return value is 0.
944 * @sz: number of array elements to read
946 * Search for a property in a device node and read 16-bit value(s) from
947 * it. Returns 0 on success, -EINVAL if the property does not exist,
948 * -ENODATA if property does not have a value, and -EOVERFLOW if the
949 * property data isn't large enough.
951 * dts entry of array should be like:
952 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
954 * The out_value is modified only if a valid u16 value can be decoded.
956 int of_property_read_u16_array(const struct device_node *np,
957 const char *propname, u16 *out_values, size_t sz)
959 const __be16 *val = of_find_property_value_of_size(np, propname,
960 (sz * sizeof(*out_values)));
966 *out_values++ = be16_to_cpup(val++);
969 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
972 * of_property_read_u32_array - Find and read an array of 32 bit integers
975 * @np: device node from which the property value is to be read.
976 * @propname: name of the property to be searched.
977 * @out_value: pointer to return value, modified only if return value is 0.
978 * @sz: number of array elements to read
980 * Search for a property in a device node and read 32-bit value(s) from
981 * it. Returns 0 on success, -EINVAL if the property does not exist,
982 * -ENODATA if property does not have a value, and -EOVERFLOW if the
983 * property data isn't large enough.
985 * The out_value is modified only if a valid u32 value can be decoded.
987 int of_property_read_u32_array(const struct device_node *np,
988 const char *propname, u32 *out_values,
991 const __be32 *val = of_find_property_value_of_size(np, propname,
992 (sz * sizeof(*out_values)));
998 *out_values++ = be32_to_cpup(val++);
1001 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1004 * of_property_read_u64 - Find and read a 64 bit integer from a property
1005 * @np: device node from which the property value is to be read.
1006 * @propname: name of the property to be searched.
1007 * @out_value: pointer to return value, modified only if return value is 0.
1009 * Search for a property in a device node and read a 64-bit value from
1010 * it. Returns 0 on success, -EINVAL if the property does not exist,
1011 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1012 * property data isn't large enough.
1014 * The out_value is modified only if a valid u64 value can be decoded.
1016 int of_property_read_u64(const struct device_node *np, const char *propname,
1019 const __be32 *val = of_find_property_value_of_size(np, propname,
1020 sizeof(*out_value));
1023 return PTR_ERR(val);
1025 *out_value = of_read_number(val, 2);
1028 EXPORT_SYMBOL_GPL(of_property_read_u64);
1031 * of_property_read_string - Find and read a string from a property
1032 * @np: device node from which the property value is to be read.
1033 * @propname: name of the property to be searched.
1034 * @out_string: pointer to null terminated return string, modified only if
1035 * return value is 0.
1037 * Search for a property in a device tree node and retrieve a null
1038 * terminated string value (pointer to data, not a copy). Returns 0 on
1039 * success, -EINVAL if the property does not exist, -ENODATA if property
1040 * does not have a value, and -EILSEQ if the string is not null-terminated
1041 * within the length of the property data.
1043 * The out_string pointer is modified only if a valid string can be decoded.
1045 int of_property_read_string(struct device_node *np, const char *propname,
1046 const char **out_string)
1048 struct property *prop = of_find_property(np, propname, NULL);
1053 if (strnlen(prop->value, prop->length) >= prop->length)
1055 *out_string = prop->value;
1058 EXPORT_SYMBOL_GPL(of_property_read_string);
1061 * of_property_read_string_index - Find and read a string from a multiple
1063 * @np: device node from which the property value is to be read.
1064 * @propname: name of the property to be searched.
1065 * @index: index of the string in the list of strings
1066 * @out_string: pointer to null terminated return string, modified only if
1067 * return value is 0.
1069 * Search for a property in a device tree node and retrieve a null
1070 * terminated string value (pointer to data, not a copy) in the list of strings
1071 * contained in that property.
1072 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1073 * property does not have a value, and -EILSEQ if the string is not
1074 * null-terminated within the length of the property data.
1076 * The out_string pointer is modified only if a valid string can be decoded.
1078 int of_property_read_string_index(struct device_node *np, const char *propname,
1079 int index, const char **output)
1081 struct property *prop = of_find_property(np, propname, NULL);
1083 size_t l = 0, total = 0;
1090 if (strnlen(prop->value, prop->length) >= prop->length)
1095 for (i = 0; total < prop->length; total += l, p += l) {
1104 EXPORT_SYMBOL_GPL(of_property_read_string_index);
1107 * of_property_match_string() - Find string in a list and return index
1108 * @np: pointer to node containing string list property
1109 * @propname: string list property name
1110 * @string: pointer to string to search for in string list
1112 * This function searches a string list property and returns the index
1113 * of a specific string value.
1115 int of_property_match_string(struct device_node *np, const char *propname,
1118 struct property *prop = of_find_property(np, propname, NULL);
1121 const char *p, *end;
1129 end = p + prop->length;
1131 for (i = 0; p < end; i++, p += l) {
1135 pr_debug("comparing %s with %s\n", string, p);
1136 if (strcmp(string, p) == 0)
1137 return i; /* Found it; return index */
1141 EXPORT_SYMBOL_GPL(of_property_match_string);
1144 * of_property_count_strings - Find and return the number of strings from a
1145 * multiple strings property.
1146 * @np: device node from which the property value is to be read.
1147 * @propname: name of the property to be searched.
1149 * Search for a property in a device tree node and retrieve the number of null
1150 * terminated string contain in it. Returns the number of strings on
1151 * success, -EINVAL if the property does not exist, -ENODATA if property
1152 * does not have a value, and -EILSEQ if the string is not null-terminated
1153 * within the length of the property data.
1155 int of_property_count_strings(struct device_node *np, const char *propname)
1157 struct property *prop = of_find_property(np, propname, NULL);
1159 size_t l = 0, total = 0;
1166 if (strnlen(prop->value, prop->length) >= prop->length)
1171 for (i = 0; total < prop->length; total += l, p += l, i++)
1176 EXPORT_SYMBOL_GPL(of_property_count_strings);
1179 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1180 * @np: Pointer to device node holding phandle property
1181 * @phandle_name: Name of property holding a phandle value
1182 * @index: For properties holding a table of phandles, this is the index into
1185 * Returns the device_node pointer with refcount incremented. Use
1186 * of_node_put() on it when done.
1188 struct device_node *of_parse_phandle(const struct device_node *np,
1189 const char *phandle_name, int index)
1191 const __be32 *phandle;
1194 phandle = of_get_property(np, phandle_name, &size);
1195 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1198 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
1200 EXPORT_SYMBOL(of_parse_phandle);
1203 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1204 * @np: pointer to a device tree node containing a list
1205 * @list_name: property name that contains a list
1206 * @cells_name: property name that specifies phandles' arguments count
1207 * @index: index of a phandle to parse out
1208 * @out_args: optional pointer to output arguments structure (will be filled)
1210 * This function is useful to parse lists of phandles and their arguments.
1211 * Returns 0 on success and fills out_args, on error returns appropriate
1214 * Caller is responsible to call of_node_put() on the returned out_args->node
1220 * #list-cells = <2>;
1224 * #list-cells = <1>;
1228 * list = <&phandle1 1 2 &phandle2 3>;
1231 * To get a device_node of the `node2' node you may call this:
1232 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1234 static int __of_parse_phandle_with_args(const struct device_node *np,
1235 const char *list_name,
1236 const char *cells_name, int index,
1237 struct of_phandle_args *out_args)
1239 const __be32 *list, *list_end;
1240 int rc = 0, size, cur_index = 0;
1242 struct device_node *node = NULL;
1245 /* Retrieve the phandle list property */
1246 list = of_get_property(np, list_name, &size);
1249 list_end = list + size / sizeof(*list);
1251 /* Loop over the phandles until all the requested entry is found */
1252 while (list < list_end) {
1257 * If phandle is 0, then it is an empty entry with no
1258 * arguments. Skip forward to the next entry.
1260 phandle = be32_to_cpup(list++);
1263 * Find the provider node and parse the #*-cells
1264 * property to determine the argument length
1266 node = of_find_node_by_phandle(phandle);
1268 pr_err("%s: could not find phandle\n",
1272 if (of_property_read_u32(node, cells_name, &count)) {
1273 pr_err("%s: could not get %s for %s\n",
1274 np->full_name, cells_name,
1280 * Make sure that the arguments actually fit in the
1281 * remaining property data length
1283 if (list + count > list_end) {
1284 pr_err("%s: arguments longer than property\n",
1291 * All of the error cases above bail out of the loop, so at
1292 * this point, the parsing is successful. If the requested
1293 * index matches, then fill the out_args structure and return,
1294 * or return -ENOENT for an empty entry.
1297 if (cur_index == index) {
1303 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1304 count = MAX_PHANDLE_ARGS;
1305 out_args->np = node;
1306 out_args->args_count = count;
1307 for (i = 0; i < count; i++)
1308 out_args->args[i] = be32_to_cpup(list++);
1313 /* Found it! return success */
1324 * Unlock node before returning result; will be one of:
1325 * -ENOENT : index is for empty phandle
1326 * -EINVAL : parsing error on data
1327 * [1..n] : Number of phandle (count mode; when index = -1)
1329 rc = index < 0 ? cur_index : -ENOENT;
1336 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1337 const char *cells_name, int index,
1338 struct of_phandle_args *out_args)
1342 return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args);
1344 EXPORT_SYMBOL(of_parse_phandle_with_args);
1347 * of_count_phandle_with_args() - Find the number of phandles references in a property
1348 * @np: pointer to a device tree node containing a list
1349 * @list_name: property name that contains a list
1350 * @cells_name: property name that specifies phandles' arguments count
1352 * Returns the number of phandle + argument tuples within a property. It
1353 * is a typical pattern to encode a list of phandle and variable
1354 * arguments into a single property. The number of arguments is encoded
1355 * by a property in the phandle-target node. For example, a gpios
1356 * property would contain a list of GPIO specifies consisting of a
1357 * phandle and 1 or more arguments. The number of arguments are
1358 * determined by the #gpio-cells property in the node pointed to by the
1361 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1362 const char *cells_name)
1364 return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL);
1366 EXPORT_SYMBOL(of_count_phandle_with_args);
1368 #if defined(CONFIG_OF_DYNAMIC)
1369 static int of_property_notify(int action, struct device_node *np,
1370 struct property *prop)
1372 struct of_prop_reconfig pr;
1376 return of_reconfig_notify(action, &pr);
1379 static int of_property_notify(int action, struct device_node *np,
1380 struct property *prop)
1387 * of_add_property - Add a property to a node
1389 int of_add_property(struct device_node *np, struct property *prop)
1391 struct property **next;
1392 unsigned long flags;
1395 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1400 raw_spin_lock_irqsave(&devtree_lock, flags);
1401 next = &np->properties;
1403 if (strcmp(prop->name, (*next)->name) == 0) {
1404 /* duplicate ! don't insert it */
1405 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1408 next = &(*next)->next;
1411 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1413 #ifdef CONFIG_PROC_DEVICETREE
1414 /* try to add to proc as well if it was initialized */
1416 proc_device_tree_add_prop(np->pde, prop);
1417 #endif /* CONFIG_PROC_DEVICETREE */
1423 * of_remove_property - Remove a property from a node.
1425 * Note that we don't actually remove it, since we have given out
1426 * who-knows-how-many pointers to the data using get-property.
1427 * Instead we just move the property to the "dead properties"
1428 * list, so it won't be found any more.
1430 int of_remove_property(struct device_node *np, struct property *prop)
1432 struct property **next;
1433 unsigned long flags;
1437 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1441 raw_spin_lock_irqsave(&devtree_lock, flags);
1442 next = &np->properties;
1444 if (*next == prop) {
1445 /* found the node */
1447 prop->next = np->deadprops;
1448 np->deadprops = prop;
1452 next = &(*next)->next;
1454 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1459 #ifdef CONFIG_PROC_DEVICETREE
1460 /* try to remove the proc node as well */
1462 proc_device_tree_remove_prop(np->pde, prop);
1463 #endif /* CONFIG_PROC_DEVICETREE */
1469 * of_update_property - Update a property in a node, if the property does
1470 * not exist, add it.
1472 * Note that we don't actually remove it, since we have given out
1473 * who-knows-how-many pointers to the data using get-property.
1474 * Instead we just move the property to the "dead properties" list,
1475 * and add the new property to the property list
1477 int of_update_property(struct device_node *np, struct property *newprop)
1479 struct property **next, *oldprop;
1480 unsigned long flags;
1483 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1490 oldprop = of_find_property(np, newprop->name, NULL);
1492 return of_add_property(np, newprop);
1494 raw_spin_lock_irqsave(&devtree_lock, flags);
1495 next = &np->properties;
1497 if (*next == oldprop) {
1498 /* found the node */
1499 newprop->next = oldprop->next;
1501 oldprop->next = np->deadprops;
1502 np->deadprops = oldprop;
1506 next = &(*next)->next;
1508 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1513 #ifdef CONFIG_PROC_DEVICETREE
1514 /* try to add to proc as well if it was initialized */
1516 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1517 #endif /* CONFIG_PROC_DEVICETREE */
1522 #if defined(CONFIG_OF_DYNAMIC)
1524 * Support for dynamic device trees.
1526 * On some platforms, the device tree can be manipulated at runtime.
1527 * The routines in this section support adding, removing and changing
1528 * device tree nodes.
1531 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1533 int of_reconfig_notifier_register(struct notifier_block *nb)
1535 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1537 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1539 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1541 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1543 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1545 int of_reconfig_notify(unsigned long action, void *p)
1549 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1550 return notifier_to_errno(rc);
1553 #ifdef CONFIG_PROC_DEVICETREE
1554 static void of_add_proc_dt_entry(struct device_node *dn)
1556 struct proc_dir_entry *ent;
1558 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1560 proc_device_tree_add_node(dn, ent);
1563 static void of_add_proc_dt_entry(struct device_node *dn)
1570 * of_attach_node - Plug a device node into the tree and global list.
1572 int of_attach_node(struct device_node *np)
1574 unsigned long flags;
1577 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1581 raw_spin_lock_irqsave(&devtree_lock, flags);
1582 np->sibling = np->parent->child;
1583 np->allnext = of_allnodes;
1584 np->parent->child = np;
1586 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1588 of_add_proc_dt_entry(np);
1592 #ifdef CONFIG_PROC_DEVICETREE
1593 static void of_remove_proc_dt_entry(struct device_node *dn)
1595 proc_remove(dn->pde);
1598 static void of_remove_proc_dt_entry(struct device_node *dn)
1605 * of_detach_node - "Unplug" a node from the device tree.
1607 * The caller must hold a reference to the node. The memory associated with
1608 * the node is not freed until its refcount goes to zero.
1610 int of_detach_node(struct device_node *np)
1612 struct device_node *parent;
1613 unsigned long flags;
1616 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1620 raw_spin_lock_irqsave(&devtree_lock, flags);
1622 if (of_node_check_flag(np, OF_DETACHED)) {
1623 /* someone already detached it */
1624 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1628 parent = np->parent;
1630 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1634 if (of_allnodes == np)
1635 of_allnodes = np->allnext;
1637 struct device_node *prev;
1638 for (prev = of_allnodes;
1639 prev->allnext != np;
1640 prev = prev->allnext)
1642 prev->allnext = np->allnext;
1645 if (parent->child == np)
1646 parent->child = np->sibling;
1648 struct device_node *prevsib;
1649 for (prevsib = np->parent->child;
1650 prevsib->sibling != np;
1651 prevsib = prevsib->sibling)
1653 prevsib->sibling = np->sibling;
1656 of_node_set_flag(np, OF_DETACHED);
1657 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1659 of_remove_proc_dt_entry(np);
1662 #endif /* defined(CONFIG_OF_DYNAMIC) */
1664 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1665 int id, const char *stem, int stem_len)
1669 strncpy(ap->stem, stem, stem_len);
1670 ap->stem[stem_len] = 0;
1671 list_add_tail(&ap->link, &aliases_lookup);
1672 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1673 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1677 * of_alias_scan - Scan all properties of 'aliases' node
1679 * The function scans all the properties of 'aliases' node and populate
1680 * the the global lookup table with the properties. It returns the
1681 * number of alias_prop found, or error code in error case.
1683 * @dt_alloc: An allocator that provides a virtual address to memory
1684 * for the resulting tree
1686 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1688 struct property *pp;
1690 of_chosen = of_find_node_by_path("/chosen");
1691 if (of_chosen == NULL)
1692 of_chosen = of_find_node_by_path("/chosen@0");
1693 of_aliases = of_find_node_by_path("/aliases");
1697 for_each_property_of_node(of_aliases, pp) {
1698 const char *start = pp->name;
1699 const char *end = start + strlen(start);
1700 struct device_node *np;
1701 struct alias_prop *ap;
1704 /* Skip those we do not want to proceed */
1705 if (!strcmp(pp->name, "name") ||
1706 !strcmp(pp->name, "phandle") ||
1707 !strcmp(pp->name, "linux,phandle"))
1710 np = of_find_node_by_path(pp->value);
1714 /* walk the alias backwards to extract the id and work out
1715 * the 'stem' string */
1716 while (isdigit(*(end-1)) && end > start)
1720 if (kstrtoint(end, 10, &id) < 0)
1723 /* Allocate an alias_prop with enough space for the stem */
1724 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1727 memset(ap, 0, sizeof(*ap) + len + 1);
1729 of_alias_add(ap, np, id, start, len);
1734 * of_alias_get_id - Get alias id for the given device_node
1735 * @np: Pointer to the given device_node
1736 * @stem: Alias stem of the given device_node
1738 * The function travels the lookup table to get alias id for the given
1739 * device_node and alias stem. It returns the alias id if find it.
1741 int of_alias_get_id(struct device_node *np, const char *stem)
1743 struct alias_prop *app;
1746 mutex_lock(&of_aliases_mutex);
1747 list_for_each_entry(app, &aliases_lookup, link) {
1748 if (strcmp(app->stem, stem) != 0)
1751 if (np == app->np) {
1756 mutex_unlock(&of_aliases_mutex);
1760 EXPORT_SYMBOL_GPL(of_alias_get_id);
1762 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1765 const void *curv = cur;
1775 curv += sizeof(*cur);
1776 if (curv >= prop->value + prop->length)
1780 *pu = be32_to_cpup(curv);
1783 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1785 const char *of_prop_next_string(struct property *prop, const char *cur)
1787 const void *curv = cur;
1795 curv += strlen(cur) + 1;
1796 if (curv >= prop->value + prop->length)
1801 EXPORT_SYMBOL_GPL(of_prop_next_string);