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;
40 static struct kset *of_kset;
43 * Used to protect the of_aliases; but also overloaded to hold off addition of
46 DEFINE_MUTEX(of_aliases_mutex);
48 /* use when traversing tree through the allnext, child, sibling,
49 * or parent members of struct device_node.
51 DEFINE_RAW_SPINLOCK(devtree_lock);
53 int of_n_addr_cells(struct device_node *np)
60 ip = of_get_property(np, "#address-cells", NULL);
62 return be32_to_cpup(ip);
64 /* No #address-cells property for the root node */
65 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
67 EXPORT_SYMBOL(of_n_addr_cells);
69 int of_n_size_cells(struct device_node *np)
76 ip = of_get_property(np, "#size-cells", NULL);
78 return be32_to_cpup(ip);
80 /* No #size-cells property for the root node */
81 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
83 EXPORT_SYMBOL(of_n_size_cells);
86 int __weak of_node_to_nid(struct device_node *np)
88 return numa_node_id();
92 #if defined(CONFIG_OF_DYNAMIC)
94 * of_node_get - Increment refcount of a node
95 * @node: Node to inc refcount, NULL is supported to
96 * simplify writing of callers
100 struct device_node *of_node_get(struct device_node *node)
103 kobject_get(&node->kobj);
106 EXPORT_SYMBOL(of_node_get);
108 static inline struct device_node *kobj_to_device_node(struct kobject *kobj)
110 return container_of(kobj, struct device_node, kobj);
114 * of_node_release - release a dynamically allocated node
115 * @kref: kref element of the node to be released
117 * In of_node_put() this function is passed to kref_put()
120 static void of_node_release(struct kobject *kobj)
122 struct device_node *node = kobj_to_device_node(kobj);
123 struct property *prop = node->properties;
125 /* We should never be releasing nodes that haven't been detached. */
126 if (!of_node_check_flag(node, OF_DETACHED)) {
127 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
132 if (!of_node_check_flag(node, OF_DYNAMIC))
136 struct property *next = prop->next;
143 prop = node->deadprops;
144 node->deadprops = NULL;
147 kfree(node->full_name);
153 * of_node_put - Decrement refcount of a node
154 * @node: Node to dec refcount, NULL is supported to
155 * simplify writing of callers
158 void of_node_put(struct device_node *node)
161 kobject_put(&node->kobj);
163 EXPORT_SYMBOL(of_node_put);
165 static void of_node_release(struct kobject *kobj)
167 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
169 #endif /* CONFIG_OF_DYNAMIC */
171 struct kobj_type of_node_ktype = {
172 .release = of_node_release,
175 static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
176 struct bin_attribute *bin_attr, char *buf,
177 loff_t offset, size_t count)
179 struct property *pp = container_of(bin_attr, struct property, attr);
180 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
183 static const char *safe_name(struct kobject *kobj, const char *orig_name)
185 const char *name = orig_name;
186 struct kernfs_node *kn;
189 /* don't be a hero. After 16 tries give up */
190 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
192 if (name != orig_name)
194 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
197 if (name != orig_name)
198 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
199 kobject_name(kobj), name);
203 static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
207 /* Important: Don't leak passwords */
208 bool secure = strncmp(pp->name, "security-", 9) == 0;
210 sysfs_bin_attr_init(&pp->attr);
211 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
212 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
213 pp->attr.size = secure ? 0 : pp->length;
214 pp->attr.read = of_node_property_read;
216 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
217 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
221 static int __of_node_add(struct device_node *np)
227 np->kobj.kset = of_kset;
229 /* Nodes without parents are new top level trees */
230 rc = kobject_add(&np->kobj, NULL, "%s",
231 safe_name(&of_kset->kobj, "base"));
233 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
234 if (!name || !name[0])
237 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
242 for_each_property_of_node(np, pp)
243 __of_add_property_sysfs(np, pp);
248 int of_node_add(struct device_node *np)
252 BUG_ON(!of_node_is_initialized(np));
255 * Grab the mutex here so that in a race condition between of_init() and
256 * of_node_add(), node addition will still be consistent.
258 mutex_lock(&of_aliases_mutex);
260 rc = __of_node_add(np);
262 /* This scenario may be perfectly valid, but report it anyway */
263 pr_info("of_node_add(%s) before of_init()\n", np->full_name);
264 mutex_unlock(&of_aliases_mutex);
268 #if defined(CONFIG_OF_DYNAMIC)
269 static void of_node_remove(struct device_node *np)
273 BUG_ON(!of_node_is_initialized(np));
275 /* only remove properties if on sysfs */
276 if (of_node_is_attached(np)) {
277 for_each_property_of_node(np, pp)
278 sysfs_remove_bin_file(&np->kobj, &pp->attr);
279 kobject_del(&np->kobj);
282 /* finally remove the kobj_init ref */
287 static int __init of_init(void)
289 struct device_node *np;
291 /* Create the kset, and register existing nodes */
292 mutex_lock(&of_aliases_mutex);
293 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
295 mutex_unlock(&of_aliases_mutex);
298 for_each_of_allnodes(np)
300 mutex_unlock(&of_aliases_mutex);
302 /* Symlink in /proc as required by userspace ABI */
304 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
308 core_initcall(of_init);
310 static struct property *__of_find_property(const struct device_node *np,
311 const char *name, int *lenp)
318 for (pp = np->properties; pp; pp = pp->next) {
319 if (of_prop_cmp(pp->name, name) == 0) {
329 struct property *of_find_property(const struct device_node *np,
336 raw_spin_lock_irqsave(&devtree_lock, flags);
337 pp = __of_find_property(np, name, lenp);
338 raw_spin_unlock_irqrestore(&devtree_lock, flags);
342 EXPORT_SYMBOL(of_find_property);
345 * of_find_all_nodes - Get next node in global list
346 * @prev: Previous node or NULL to start iteration
347 * of_node_put() will be called on it
349 * Returns a node pointer with refcount incremented, use
350 * of_node_put() on it when done.
352 struct device_node *of_find_all_nodes(struct device_node *prev)
354 struct device_node *np;
357 raw_spin_lock_irqsave(&devtree_lock, flags);
358 np = prev ? prev->allnext : of_allnodes;
359 for (; np != NULL; np = np->allnext)
363 raw_spin_unlock_irqrestore(&devtree_lock, flags);
366 EXPORT_SYMBOL(of_find_all_nodes);
369 * Find a property with a given name for a given node
370 * and return the value.
372 static const void *__of_get_property(const struct device_node *np,
373 const char *name, int *lenp)
375 struct property *pp = __of_find_property(np, name, lenp);
377 return pp ? pp->value : NULL;
381 * Find a property with a given name for a given node
382 * and return the value.
384 const void *of_get_property(const struct device_node *np, const char *name,
387 struct property *pp = of_find_property(np, name, lenp);
389 return pp ? pp->value : NULL;
391 EXPORT_SYMBOL(of_get_property);
394 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
396 * @cpu: logical cpu index of a core/thread
397 * @phys_id: physical identifier of a core/thread
399 * CPU logical to physical index mapping is architecture specific.
400 * However this __weak function provides a default match of physical
401 * id to logical cpu index. phys_id provided here is usually values read
402 * from the device tree which must match the hardware internal registers.
404 * Returns true if the physical identifier and the logical cpu index
405 * correspond to the same core/thread, false otherwise.
407 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
409 return (u32)phys_id == cpu;
413 * Checks if the given "prop_name" property holds the physical id of the
414 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
415 * NULL, local thread number within the core is returned in it.
417 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
418 const char *prop_name, int cpu, unsigned int *thread)
421 int ac, prop_len, tid;
424 ac = of_n_addr_cells(cpun);
425 cell = of_get_property(cpun, prop_name, &prop_len);
428 prop_len /= sizeof(*cell) * ac;
429 for (tid = 0; tid < prop_len; tid++) {
430 hwid = of_read_number(cell, ac);
431 if (arch_match_cpu_phys_id(cpu, hwid)) {
442 * arch_find_n_match_cpu_physical_id - See if the given device node is
443 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
444 * else false. If 'thread' is non-NULL, the local thread number within the
445 * core is returned in it.
447 bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
448 int cpu, unsigned int *thread)
450 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
451 * for thread ids on PowerPC. If it doesn't exist fallback to
452 * standard "reg" property.
454 if (IS_ENABLED(CONFIG_PPC) &&
455 __of_find_n_match_cpu_property(cpun,
456 "ibm,ppc-interrupt-server#s",
460 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
467 * of_get_cpu_node - Get device node associated with the given logical CPU
469 * @cpu: CPU number(logical index) for which device node is required
470 * @thread: if not NULL, local thread number within the physical core is
473 * The main purpose of this function is to retrieve the device node for the
474 * given logical CPU index. It should be used to initialize the of_node in
475 * cpu device. Once of_node in cpu device is populated, all the further
476 * references can use that instead.
478 * CPU logical to physical index mapping is architecture specific and is built
479 * before booting secondary cores. This function uses arch_match_cpu_phys_id
480 * which can be overridden by architecture specific implementation.
482 * Returns a node pointer for the logical cpu if found, else NULL.
484 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
486 struct device_node *cpun;
488 for_each_node_by_type(cpun, "cpu") {
489 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
494 EXPORT_SYMBOL(of_get_cpu_node);
497 * __of_device_is_compatible() - Check if the node matches given constraints
498 * @device: pointer to node
499 * @compat: required compatible string, NULL or "" for any match
500 * @type: required device_type value, NULL or "" for any match
501 * @name: required node name, NULL or "" for any match
503 * Checks if the given @compat, @type and @name strings match the
504 * properties of the given @device. A constraints can be skipped by
505 * passing NULL or an empty string as the constraint.
507 * Returns 0 for no match, and a positive integer on match. The return
508 * value is a relative score with larger values indicating better
509 * matches. The score is weighted for the most specific compatible value
510 * to get the highest score. Matching type is next, followed by matching
511 * name. Practically speaking, this results in the following priority
514 * 1. specific compatible && type && name
515 * 2. specific compatible && type
516 * 3. specific compatible && name
517 * 4. specific compatible
518 * 5. general compatible && type && name
519 * 6. general compatible && type
520 * 7. general compatible && name
521 * 8. general compatible
526 static int __of_device_is_compatible(const struct device_node *device,
527 const char *compat, const char *type, const char *name)
529 struct property *prop;
531 int index = 0, score = 0;
533 /* Compatible match has highest priority */
534 if (compat && compat[0]) {
535 prop = __of_find_property(device, "compatible", NULL);
536 for (cp = of_prop_next_string(prop, NULL); cp;
537 cp = of_prop_next_string(prop, cp), index++) {
538 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
539 score = INT_MAX/2 - (index << 2);
547 /* Matching type is better than matching name */
548 if (type && type[0]) {
549 if (!device->type || of_node_cmp(type, device->type))
554 /* Matching name is a bit better than not */
555 if (name && name[0]) {
556 if (!device->name || of_node_cmp(name, device->name))
564 /** Checks if the given "compat" string matches one of the strings in
565 * the device's "compatible" property
567 int of_device_is_compatible(const struct device_node *device,
573 raw_spin_lock_irqsave(&devtree_lock, flags);
574 res = __of_device_is_compatible(device, compat, NULL, NULL);
575 raw_spin_unlock_irqrestore(&devtree_lock, flags);
578 EXPORT_SYMBOL(of_device_is_compatible);
581 * of_machine_is_compatible - Test root of device tree for a given compatible value
582 * @compat: compatible string to look for in root node's compatible property.
584 * Returns true if the root node has the given value in its
585 * compatible property.
587 int of_machine_is_compatible(const char *compat)
589 struct device_node *root;
592 root = of_find_node_by_path("/");
594 rc = of_device_is_compatible(root, compat);
599 EXPORT_SYMBOL(of_machine_is_compatible);
602 * __of_device_is_available - check if a device is available for use
604 * @device: Node to check for availability, with locks already held
606 * Returns 1 if the status property is absent or set to "okay" or "ok",
609 static int __of_device_is_available(const struct device_node *device)
617 status = __of_get_property(device, "status", &statlen);
622 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
630 * of_device_is_available - check if a device is available for use
632 * @device: Node to check for availability
634 * Returns 1 if the status property is absent or set to "okay" or "ok",
637 int of_device_is_available(const struct device_node *device)
642 raw_spin_lock_irqsave(&devtree_lock, flags);
643 res = __of_device_is_available(device);
644 raw_spin_unlock_irqrestore(&devtree_lock, flags);
648 EXPORT_SYMBOL(of_device_is_available);
651 * of_get_parent - Get a node's parent if any
652 * @node: Node to get parent
654 * Returns a node pointer with refcount incremented, use
655 * of_node_put() on it when done.
657 struct device_node *of_get_parent(const struct device_node *node)
659 struct device_node *np;
665 raw_spin_lock_irqsave(&devtree_lock, flags);
666 np = of_node_get(node->parent);
667 raw_spin_unlock_irqrestore(&devtree_lock, flags);
670 EXPORT_SYMBOL(of_get_parent);
673 * of_get_next_parent - Iterate to a node's parent
674 * @node: Node to get parent of
676 * This is like of_get_parent() except that it drops the
677 * refcount on the passed node, making it suitable for iterating
678 * through a node's parents.
680 * Returns a node pointer with refcount incremented, use
681 * of_node_put() on it when done.
683 struct device_node *of_get_next_parent(struct device_node *node)
685 struct device_node *parent;
691 raw_spin_lock_irqsave(&devtree_lock, flags);
692 parent = of_node_get(node->parent);
694 raw_spin_unlock_irqrestore(&devtree_lock, flags);
697 EXPORT_SYMBOL(of_get_next_parent);
699 static struct device_node *__of_get_next_child(const struct device_node *node,
700 struct device_node *prev)
702 struct device_node *next;
707 next = prev ? prev->sibling : node->child;
708 for (; next; next = next->sibling)
709 if (of_node_get(next))
714 #define __for_each_child_of_node(parent, child) \
715 for (child = __of_get_next_child(parent, NULL); child != NULL; \
716 child = __of_get_next_child(parent, child))
719 * of_get_next_child - Iterate a node childs
721 * @prev: previous child of the parent node, or NULL to get first
723 * Returns a node pointer with refcount incremented, use
724 * of_node_put() on it when done.
726 struct device_node *of_get_next_child(const struct device_node *node,
727 struct device_node *prev)
729 struct device_node *next;
732 raw_spin_lock_irqsave(&devtree_lock, flags);
733 next = __of_get_next_child(node, prev);
734 raw_spin_unlock_irqrestore(&devtree_lock, flags);
737 EXPORT_SYMBOL(of_get_next_child);
740 * of_get_next_available_child - Find the next available child node
742 * @prev: previous child of the parent node, or NULL to get first
744 * This function is like of_get_next_child(), except that it
745 * automatically skips any disabled nodes (i.e. status = "disabled").
747 struct device_node *of_get_next_available_child(const struct device_node *node,
748 struct device_node *prev)
750 struct device_node *next;
756 raw_spin_lock_irqsave(&devtree_lock, flags);
757 next = prev ? prev->sibling : node->child;
758 for (; next; next = next->sibling) {
759 if (!__of_device_is_available(next))
761 if (of_node_get(next))
765 raw_spin_unlock_irqrestore(&devtree_lock, flags);
768 EXPORT_SYMBOL(of_get_next_available_child);
771 * of_get_child_by_name - Find the child node by name for a given parent
773 * @name: child name to look for.
775 * This function looks for child node for given matching name
777 * Returns a node pointer if found, with refcount incremented, use
778 * of_node_put() on it when done.
779 * Returns NULL if node is not found.
781 struct device_node *of_get_child_by_name(const struct device_node *node,
784 struct device_node *child;
786 for_each_child_of_node(node, child)
787 if (child->name && (of_node_cmp(child->name, name) == 0))
791 EXPORT_SYMBOL(of_get_child_by_name);
793 static struct device_node *__of_find_node_by_path(struct device_node *parent,
796 struct device_node *child;
797 int len = strchrnul(path, '/') - path;
802 __for_each_child_of_node(parent, child) {
803 const char *name = strrchr(child->full_name, '/');
804 if (WARN(!name, "malformed device_node %s\n", child->full_name))
807 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
814 * of_find_node_by_path - Find a node matching a full OF path
815 * @path: Either the full path to match, or if the path does not
816 * start with '/', the name of a property of the /aliases
817 * node (an alias). In the case of an alias, the node
818 * matching the alias' value will be returned.
823 * foo/bar Valid alias + relative path
825 * Returns a node pointer with refcount incremented, use
826 * of_node_put() on it when done.
828 struct device_node *of_find_node_by_path(const char *path)
830 struct device_node *np = NULL;
834 if (strcmp(path, "/") == 0)
835 return of_node_get(of_allnodes);
837 /* The path could begin with an alias */
839 char *p = strchrnul(path, '/');
842 /* of_aliases must not be NULL */
846 for_each_property_of_node(of_aliases, pp) {
847 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
848 np = of_find_node_by_path(pp->value);
857 /* Step down the tree matching path components */
858 raw_spin_lock_irqsave(&devtree_lock, flags);
860 np = of_node_get(of_allnodes);
861 while (np && *path == '/') {
862 path++; /* Increment past '/' delimiter */
863 np = __of_find_node_by_path(np, path);
864 path = strchrnul(path, '/');
866 raw_spin_unlock_irqrestore(&devtree_lock, flags);
869 EXPORT_SYMBOL(of_find_node_by_path);
872 * of_find_node_by_name - Find a node by its "name" property
873 * @from: The node to start searching from or NULL, the node
874 * you pass will not be searched, only the next one
875 * will; typically, you pass what the previous call
876 * returned. of_node_put() will be called on it
877 * @name: The name string to match against
879 * Returns a node pointer with refcount incremented, use
880 * of_node_put() on it when done.
882 struct device_node *of_find_node_by_name(struct device_node *from,
885 struct device_node *np;
888 raw_spin_lock_irqsave(&devtree_lock, flags);
889 np = from ? from->allnext : of_allnodes;
890 for (; np; np = np->allnext)
891 if (np->name && (of_node_cmp(np->name, name) == 0)
895 raw_spin_unlock_irqrestore(&devtree_lock, flags);
898 EXPORT_SYMBOL(of_find_node_by_name);
901 * of_find_node_by_type - Find a node by its "device_type" property
902 * @from: The node to start searching from, or NULL to start searching
903 * the entire device tree. The node you pass will not be
904 * searched, only the next one will; typically, you pass
905 * what the previous call returned. of_node_put() will be
906 * called on from for you.
907 * @type: The type string to match against
909 * Returns a node pointer with refcount incremented, use
910 * of_node_put() on it when done.
912 struct device_node *of_find_node_by_type(struct device_node *from,
915 struct device_node *np;
918 raw_spin_lock_irqsave(&devtree_lock, flags);
919 np = from ? from->allnext : of_allnodes;
920 for (; np; np = np->allnext)
921 if (np->type && (of_node_cmp(np->type, type) == 0)
925 raw_spin_unlock_irqrestore(&devtree_lock, flags);
928 EXPORT_SYMBOL(of_find_node_by_type);
931 * of_find_compatible_node - Find a node based on type and one of the
932 * tokens in its "compatible" property
933 * @from: The node to start searching from or NULL, the node
934 * you pass will not be searched, only the next one
935 * will; typically, you pass what the previous call
936 * returned. of_node_put() will be called on it
937 * @type: The type string to match "device_type" or NULL to ignore
938 * @compatible: The string to match to one of the tokens in the device
941 * Returns a node pointer with refcount incremented, use
942 * of_node_put() on it when done.
944 struct device_node *of_find_compatible_node(struct device_node *from,
945 const char *type, const char *compatible)
947 struct device_node *np;
950 raw_spin_lock_irqsave(&devtree_lock, flags);
951 np = from ? from->allnext : of_allnodes;
952 for (; np; np = np->allnext) {
953 if (__of_device_is_compatible(np, compatible, type, NULL) &&
958 raw_spin_unlock_irqrestore(&devtree_lock, flags);
961 EXPORT_SYMBOL(of_find_compatible_node);
964 * of_find_node_with_property - Find a node which has a property with
966 * @from: The node to start searching from or NULL, the node
967 * you pass will not be searched, only the next one
968 * will; typically, you pass what the previous call
969 * returned. of_node_put() will be called on it
970 * @prop_name: The name of the property to look for.
972 * Returns a node pointer with refcount incremented, use
973 * of_node_put() on it when done.
975 struct device_node *of_find_node_with_property(struct device_node *from,
976 const char *prop_name)
978 struct device_node *np;
982 raw_spin_lock_irqsave(&devtree_lock, flags);
983 np = from ? from->allnext : of_allnodes;
984 for (; np; np = np->allnext) {
985 for (pp = np->properties; pp; pp = pp->next) {
986 if (of_prop_cmp(pp->name, prop_name) == 0) {
994 raw_spin_unlock_irqrestore(&devtree_lock, flags);
997 EXPORT_SYMBOL(of_find_node_with_property);
1000 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1001 const struct device_node *node)
1003 const struct of_device_id *best_match = NULL;
1004 int score, best_score = 0;
1009 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1010 score = __of_device_is_compatible(node, matches->compatible,
1011 matches->type, matches->name);
1012 if (score > best_score) {
1013 best_match = matches;
1022 * of_match_node - Tell if an device_node has a matching of_match structure
1023 * @matches: array of of device match structures to search in
1024 * @node: the of device structure to match against
1026 * Low level utility function used by device matching.
1028 const struct of_device_id *of_match_node(const struct of_device_id *matches,
1029 const struct device_node *node)
1031 const struct of_device_id *match;
1032 unsigned long flags;
1034 raw_spin_lock_irqsave(&devtree_lock, flags);
1035 match = __of_match_node(matches, node);
1036 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1039 EXPORT_SYMBOL(of_match_node);
1042 * of_find_matching_node_and_match - Find a node based on an of_device_id
1044 * @from: The node to start searching from or NULL, the node
1045 * you pass will not be searched, only the next one
1046 * will; typically, you pass what the previous call
1047 * returned. of_node_put() will be called on it
1048 * @matches: array of of device match structures to search in
1049 * @match Updated to point at the matches entry which matched
1051 * Returns a node pointer with refcount incremented, use
1052 * of_node_put() on it when done.
1054 struct device_node *of_find_matching_node_and_match(struct device_node *from,
1055 const struct of_device_id *matches,
1056 const struct of_device_id **match)
1058 struct device_node *np;
1059 const struct of_device_id *m;
1060 unsigned long flags;
1065 raw_spin_lock_irqsave(&devtree_lock, flags);
1066 np = from ? from->allnext : of_allnodes;
1067 for (; np; np = np->allnext) {
1068 m = __of_match_node(matches, np);
1069 if (m && of_node_get(np)) {
1076 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1079 EXPORT_SYMBOL(of_find_matching_node_and_match);
1082 * of_modalias_node - Lookup appropriate modalias for a device node
1083 * @node: pointer to a device tree node
1084 * @modalias: Pointer to buffer that modalias value will be copied into
1085 * @len: Length of modalias value
1087 * Based on the value of the compatible property, this routine will attempt
1088 * to choose an appropriate modalias value for a particular device tree node.
1089 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1090 * from the first entry in the compatible list property.
1092 * This routine returns 0 on success, <0 on failure.
1094 int of_modalias_node(struct device_node *node, char *modalias, int len)
1096 const char *compatible, *p;
1099 compatible = of_get_property(node, "compatible", &cplen);
1100 if (!compatible || strlen(compatible) > cplen)
1102 p = strchr(compatible, ',');
1103 strlcpy(modalias, p ? p + 1 : compatible, len);
1106 EXPORT_SYMBOL_GPL(of_modalias_node);
1109 * of_find_node_by_phandle - Find a node given a phandle
1110 * @handle: phandle of the node to find
1112 * Returns a node pointer with refcount incremented, use
1113 * of_node_put() on it when done.
1115 struct device_node *of_find_node_by_phandle(phandle handle)
1117 struct device_node *np;
1118 unsigned long flags;
1120 raw_spin_lock_irqsave(&devtree_lock, flags);
1121 for (np = of_allnodes; np; np = np->allnext)
1122 if (np->phandle == handle)
1125 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1128 EXPORT_SYMBOL(of_find_node_by_phandle);
1131 * of_property_count_elems_of_size - Count the number of elements in a property
1133 * @np: device node from which the property value is to be read.
1134 * @propname: name of the property to be searched.
1135 * @elem_size: size of the individual element
1137 * Search for a property in a device node and count the number of elements of
1138 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1139 * property does not exist or its length does not match a multiple of elem_size
1140 * and -ENODATA if the property does not have a value.
1142 int of_property_count_elems_of_size(const struct device_node *np,
1143 const char *propname, int elem_size)
1145 struct property *prop = of_find_property(np, propname, NULL);
1152 if (prop->length % elem_size != 0) {
1153 pr_err("size of %s in node %s is not a multiple of %d\n",
1154 propname, np->full_name, elem_size);
1158 return prop->length / elem_size;
1160 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1163 * of_find_property_value_of_size
1165 * @np: device node from which the property value is to be read.
1166 * @propname: name of the property to be searched.
1167 * @len: requested length of property value
1169 * Search for a property in a device node and valid the requested size.
1170 * Returns the property value on success, -EINVAL if the property does not
1171 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1172 * property data isn't large enough.
1175 static void *of_find_property_value_of_size(const struct device_node *np,
1176 const char *propname, u32 len)
1178 struct property *prop = of_find_property(np, propname, NULL);
1181 return ERR_PTR(-EINVAL);
1183 return ERR_PTR(-ENODATA);
1184 if (len > prop->length)
1185 return ERR_PTR(-EOVERFLOW);
1191 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1193 * @np: device node from which the property value is to be read.
1194 * @propname: name of the property to be searched.
1195 * @index: index of the u32 in the list of values
1196 * @out_value: pointer to return value, modified only if no error.
1198 * Search for a property in a device node and read nth 32-bit value from
1199 * it. Returns 0 on success, -EINVAL if the property does not exist,
1200 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1201 * property data isn't large enough.
1203 * The out_value is modified only if a valid u32 value can be decoded.
1205 int of_property_read_u32_index(const struct device_node *np,
1206 const char *propname,
1207 u32 index, u32 *out_value)
1209 const u32 *val = of_find_property_value_of_size(np, propname,
1210 ((index + 1) * sizeof(*out_value)));
1213 return PTR_ERR(val);
1215 *out_value = be32_to_cpup(((__be32 *)val) + index);
1218 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1221 * of_property_read_u8_array - Find and read an array of u8 from a property.
1223 * @np: device node from which the property value is to be read.
1224 * @propname: name of the property to be searched.
1225 * @out_values: pointer to return value, modified only if return value is 0.
1226 * @sz: number of array elements to read
1228 * Search for a property in a device node and read 8-bit value(s) from
1229 * it. Returns 0 on success, -EINVAL if the property does not exist,
1230 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1231 * property data isn't large enough.
1233 * dts entry of array should be like:
1234 * property = /bits/ 8 <0x50 0x60 0x70>;
1236 * The out_values is modified only if a valid u8 value can be decoded.
1238 int of_property_read_u8_array(const struct device_node *np,
1239 const char *propname, u8 *out_values, size_t sz)
1241 const u8 *val = of_find_property_value_of_size(np, propname,
1242 (sz * sizeof(*out_values)));
1245 return PTR_ERR(val);
1248 *out_values++ = *val++;
1251 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1254 * of_property_read_u16_array - Find and read an array of u16 from a property.
1256 * @np: device node from which the property value is to be read.
1257 * @propname: name of the property to be searched.
1258 * @out_values: pointer to return value, modified only if return value is 0.
1259 * @sz: number of array elements to read
1261 * Search for a property in a device node and read 16-bit value(s) from
1262 * it. Returns 0 on success, -EINVAL if the property does not exist,
1263 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1264 * property data isn't large enough.
1266 * dts entry of array should be like:
1267 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1269 * The out_values is modified only if a valid u16 value can be decoded.
1271 int of_property_read_u16_array(const struct device_node *np,
1272 const char *propname, u16 *out_values, size_t sz)
1274 const __be16 *val = of_find_property_value_of_size(np, propname,
1275 (sz * sizeof(*out_values)));
1278 return PTR_ERR(val);
1281 *out_values++ = be16_to_cpup(val++);
1284 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1287 * of_property_read_u32_array - Find and read an array of 32 bit integers
1290 * @np: device node from which the property value is to be read.
1291 * @propname: name of the property to be searched.
1292 * @out_values: pointer to return value, modified only if return value is 0.
1293 * @sz: number of array elements to read
1295 * Search for a property in a device node and read 32-bit value(s) from
1296 * it. Returns 0 on success, -EINVAL if the property does not exist,
1297 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1298 * property data isn't large enough.
1300 * The out_values is modified only if a valid u32 value can be decoded.
1302 int of_property_read_u32_array(const struct device_node *np,
1303 const char *propname, u32 *out_values,
1306 const __be32 *val = of_find_property_value_of_size(np, propname,
1307 (sz * sizeof(*out_values)));
1310 return PTR_ERR(val);
1313 *out_values++ = be32_to_cpup(val++);
1316 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1319 * of_property_read_u64 - Find and read a 64 bit integer from a property
1320 * @np: device node from which the property value is to be read.
1321 * @propname: name of the property to be searched.
1322 * @out_value: pointer to return value, modified only if return value is 0.
1324 * Search for a property in a device node and read a 64-bit value from
1325 * it. Returns 0 on success, -EINVAL if the property does not exist,
1326 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1327 * property data isn't large enough.
1329 * The out_value is modified only if a valid u64 value can be decoded.
1331 int of_property_read_u64(const struct device_node *np, const char *propname,
1334 const __be32 *val = of_find_property_value_of_size(np, propname,
1335 sizeof(*out_value));
1338 return PTR_ERR(val);
1340 *out_value = of_read_number(val, 2);
1343 EXPORT_SYMBOL_GPL(of_property_read_u64);
1346 * of_property_read_string - Find and read a string from a property
1347 * @np: device node from which the property value is to be read.
1348 * @propname: name of the property to be searched.
1349 * @out_string: pointer to null terminated return string, modified only if
1350 * return value is 0.
1352 * Search for a property in a device tree node and retrieve a null
1353 * terminated string value (pointer to data, not a copy). Returns 0 on
1354 * success, -EINVAL if the property does not exist, -ENODATA if property
1355 * does not have a value, and -EILSEQ if the string is not null-terminated
1356 * within the length of the property data.
1358 * The out_string pointer is modified only if a valid string can be decoded.
1360 int of_property_read_string(struct device_node *np, const char *propname,
1361 const char **out_string)
1363 struct property *prop = of_find_property(np, propname, NULL);
1368 if (strnlen(prop->value, prop->length) >= prop->length)
1370 *out_string = prop->value;
1373 EXPORT_SYMBOL_GPL(of_property_read_string);
1376 * of_property_read_string_index - Find and read a string from a multiple
1378 * @np: device node from which the property value is to be read.
1379 * @propname: name of the property to be searched.
1380 * @index: index of the string in the list of strings
1381 * @out_string: pointer to null terminated return string, modified only if
1382 * return value is 0.
1384 * Search for a property in a device tree node and retrieve a null
1385 * terminated string value (pointer to data, not a copy) in the list of strings
1386 * contained in that property.
1387 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1388 * property does not have a value, and -EILSEQ if the string is not
1389 * null-terminated within the length of the property data.
1391 * The out_string pointer is modified only if a valid string can be decoded.
1393 int of_property_read_string_index(struct device_node *np, const char *propname,
1394 int index, const char **output)
1396 struct property *prop = of_find_property(np, propname, NULL);
1398 size_t l = 0, total = 0;
1405 if (strnlen(prop->value, prop->length) >= prop->length)
1410 for (i = 0; total < prop->length; total += l, p += l) {
1419 EXPORT_SYMBOL_GPL(of_property_read_string_index);
1422 * of_property_match_string() - Find string in a list and return index
1423 * @np: pointer to node containing string list property
1424 * @propname: string list property name
1425 * @string: pointer to string to search for in string list
1427 * This function searches a string list property and returns the index
1428 * of a specific string value.
1430 int of_property_match_string(struct device_node *np, const char *propname,
1433 struct property *prop = of_find_property(np, propname, NULL);
1436 const char *p, *end;
1444 end = p + prop->length;
1446 for (i = 0; p < end; i++, p += l) {
1450 pr_debug("comparing %s with %s\n", string, p);
1451 if (strcmp(string, p) == 0)
1452 return i; /* Found it; return index */
1456 EXPORT_SYMBOL_GPL(of_property_match_string);
1459 * of_property_count_strings - Find and return the number of strings from a
1460 * multiple strings property.
1461 * @np: device node from which the property value is to be read.
1462 * @propname: name of the property to be searched.
1464 * Search for a property in a device tree node and retrieve the number of null
1465 * terminated string contain in it. Returns the number of strings on
1466 * success, -EINVAL if the property does not exist, -ENODATA if property
1467 * does not have a value, and -EILSEQ if the string is not null-terminated
1468 * within the length of the property data.
1470 int of_property_count_strings(struct device_node *np, const char *propname)
1472 struct property *prop = of_find_property(np, propname, NULL);
1474 size_t l = 0, total = 0;
1481 if (strnlen(prop->value, prop->length) >= prop->length)
1486 for (i = 0; total < prop->length; total += l, p += l, i++)
1491 EXPORT_SYMBOL_GPL(of_property_count_strings);
1493 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1496 printk("%s %s", msg, of_node_full_name(args->np));
1497 for (i = 0; i < args->args_count; i++)
1498 printk(i ? ",%08x" : ":%08x", args->args[i]);
1502 static int __of_parse_phandle_with_args(const struct device_node *np,
1503 const char *list_name,
1504 const char *cells_name,
1505 int cell_count, int index,
1506 struct of_phandle_args *out_args)
1508 const __be32 *list, *list_end;
1509 int rc = 0, size, cur_index = 0;
1511 struct device_node *node = NULL;
1514 /* Retrieve the phandle list property */
1515 list = of_get_property(np, list_name, &size);
1518 list_end = list + size / sizeof(*list);
1520 /* Loop over the phandles until all the requested entry is found */
1521 while (list < list_end) {
1526 * If phandle is 0, then it is an empty entry with no
1527 * arguments. Skip forward to the next entry.
1529 phandle = be32_to_cpup(list++);
1532 * Find the provider node and parse the #*-cells
1533 * property to determine the argument length.
1535 * This is not needed if the cell count is hard-coded
1536 * (i.e. cells_name not set, but cell_count is set),
1537 * except when we're going to return the found node
1540 if (cells_name || cur_index == index) {
1541 node = of_find_node_by_phandle(phandle);
1543 pr_err("%s: could not find phandle\n",
1550 if (of_property_read_u32(node, cells_name,
1552 pr_err("%s: could not get %s for %s\n",
1553 np->full_name, cells_name,
1562 * Make sure that the arguments actually fit in the
1563 * remaining property data length
1565 if (list + count > list_end) {
1566 pr_err("%s: arguments longer than property\n",
1573 * All of the error cases above bail out of the loop, so at
1574 * this point, the parsing is successful. If the requested
1575 * index matches, then fill the out_args structure and return,
1576 * or return -ENOENT for an empty entry.
1579 if (cur_index == index) {
1585 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1586 count = MAX_PHANDLE_ARGS;
1587 out_args->np = node;
1588 out_args->args_count = count;
1589 for (i = 0; i < count; i++)
1590 out_args->args[i] = be32_to_cpup(list++);
1595 /* Found it! return success */
1606 * Unlock node before returning result; will be one of:
1607 * -ENOENT : index is for empty phandle
1608 * -EINVAL : parsing error on data
1609 * [1..n] : Number of phandle (count mode; when index = -1)
1611 rc = index < 0 ? cur_index : -ENOENT;
1619 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1620 * @np: Pointer to device node holding phandle property
1621 * @phandle_name: Name of property holding a phandle value
1622 * @index: For properties holding a table of phandles, this is the index into
1625 * Returns the device_node pointer with refcount incremented. Use
1626 * of_node_put() on it when done.
1628 struct device_node *of_parse_phandle(const struct device_node *np,
1629 const char *phandle_name, int index)
1631 struct of_phandle_args args;
1636 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1642 EXPORT_SYMBOL(of_parse_phandle);
1645 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1646 * @np: pointer to a device tree node containing a list
1647 * @list_name: property name that contains a list
1648 * @cells_name: property name that specifies phandles' arguments count
1649 * @index: index of a phandle to parse out
1650 * @out_args: optional pointer to output arguments structure (will be filled)
1652 * This function is useful to parse lists of phandles and their arguments.
1653 * Returns 0 on success and fills out_args, on error returns appropriate
1656 * Caller is responsible to call of_node_put() on the returned out_args->node
1662 * #list-cells = <2>;
1666 * #list-cells = <1>;
1670 * list = <&phandle1 1 2 &phandle2 3>;
1673 * To get a device_node of the `node2' node you may call this:
1674 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1676 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1677 const char *cells_name, int index,
1678 struct of_phandle_args *out_args)
1682 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1685 EXPORT_SYMBOL(of_parse_phandle_with_args);
1688 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1689 * @np: pointer to a device tree node containing a list
1690 * @list_name: property name that contains a list
1691 * @cell_count: number of argument cells following the phandle
1692 * @index: index of a phandle to parse out
1693 * @out_args: optional pointer to output arguments structure (will be filled)
1695 * This function is useful to parse lists of phandles and their arguments.
1696 * Returns 0 on success and fills out_args, on error returns appropriate
1699 * Caller is responsible to call of_node_put() on the returned out_args->node
1711 * list = <&phandle1 0 2 &phandle2 2 3>;
1714 * To get a device_node of the `node2' node you may call this:
1715 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1717 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1718 const char *list_name, int cell_count,
1719 int index, struct of_phandle_args *out_args)
1723 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1726 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1729 * of_count_phandle_with_args() - Find the number of phandles references in a property
1730 * @np: pointer to a device tree node containing a list
1731 * @list_name: property name that contains a list
1732 * @cells_name: property name that specifies phandles' arguments count
1734 * Returns the number of phandle + argument tuples within a property. It
1735 * is a typical pattern to encode a list of phandle and variable
1736 * arguments into a single property. The number of arguments is encoded
1737 * by a property in the phandle-target node. For example, a gpios
1738 * property would contain a list of GPIO specifies consisting of a
1739 * phandle and 1 or more arguments. The number of arguments are
1740 * determined by the #gpio-cells property in the node pointed to by the
1743 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1744 const char *cells_name)
1746 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1749 EXPORT_SYMBOL(of_count_phandle_with_args);
1751 #if defined(CONFIG_OF_DYNAMIC)
1752 static int of_property_notify(int action, struct device_node *np,
1753 struct property *prop)
1755 struct of_prop_reconfig pr;
1757 /* only call notifiers if the node is attached */
1758 if (!of_node_is_attached(np))
1763 return of_reconfig_notify(action, &pr);
1766 static int of_property_notify(int action, struct device_node *np,
1767 struct property *prop)
1774 * __of_add_property - Add a property to a node without lock operations
1776 static int __of_add_property(struct device_node *np, struct property *prop)
1778 struct property **next;
1781 next = &np->properties;
1783 if (strcmp(prop->name, (*next)->name) == 0)
1784 /* duplicate ! don't insert it */
1787 next = &(*next)->next;
1795 * of_add_property - Add a property to a node
1797 int of_add_property(struct device_node *np, struct property *prop)
1799 unsigned long flags;
1802 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1806 raw_spin_lock_irqsave(&devtree_lock, flags);
1807 rc = __of_add_property(np, prop);
1808 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1812 if (of_node_is_attached(np))
1813 __of_add_property_sysfs(np, prop);
1819 * of_remove_property - Remove a property from a node.
1821 * Note that we don't actually remove it, since we have given out
1822 * who-knows-how-many pointers to the data using get-property.
1823 * Instead we just move the property to the "dead properties"
1824 * list, so it won't be found any more.
1826 int of_remove_property(struct device_node *np, struct property *prop)
1828 struct property **next;
1829 unsigned long flags;
1833 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1837 raw_spin_lock_irqsave(&devtree_lock, flags);
1838 next = &np->properties;
1840 if (*next == prop) {
1841 /* found the node */
1843 prop->next = np->deadprops;
1844 np->deadprops = prop;
1848 next = &(*next)->next;
1850 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1855 /* at early boot, bail hear and defer setup to of_init() */
1859 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1865 * of_update_property - Update a property in a node, if the property does
1866 * not exist, add it.
1868 * Note that we don't actually remove it, since we have given out
1869 * who-knows-how-many pointers to the data using get-property.
1870 * Instead we just move the property to the "dead properties" list,
1871 * and add the new property to the property list
1873 int of_update_property(struct device_node *np, struct property *newprop)
1875 struct property **next, *oldprop;
1876 unsigned long flags;
1879 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1886 raw_spin_lock_irqsave(&devtree_lock, flags);
1887 next = &np->properties;
1888 oldprop = __of_find_property(np, newprop->name, NULL);
1890 /* add the new node */
1891 rc = __of_add_property(np, newprop);
1892 } else while (*next) {
1893 /* replace the node */
1894 if (*next == oldprop) {
1895 newprop->next = oldprop->next;
1897 oldprop->next = np->deadprops;
1898 np->deadprops = oldprop;
1901 next = &(*next)->next;
1903 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1907 /* At early boot, bail out and defer setup to of_init() */
1911 /* Update the sysfs attribute */
1913 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1914 __of_add_property_sysfs(np, newprop);
1919 #if defined(CONFIG_OF_DYNAMIC)
1921 * Support for dynamic device trees.
1923 * On some platforms, the device tree can be manipulated at runtime.
1924 * The routines in this section support adding, removing and changing
1925 * device tree nodes.
1928 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1930 int of_reconfig_notifier_register(struct notifier_block *nb)
1932 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1934 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1936 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1938 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1940 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1942 int of_reconfig_notify(unsigned long action, void *p)
1946 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1947 return notifier_to_errno(rc);
1951 * of_attach_node - Plug a device node into the tree and global list.
1953 int of_attach_node(struct device_node *np)
1955 unsigned long flags;
1958 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1962 raw_spin_lock_irqsave(&devtree_lock, flags);
1963 np->sibling = np->parent->child;
1964 np->allnext = np->parent->allnext;
1965 np->parent->allnext = np;
1966 np->parent->child = np;
1967 of_node_clear_flag(np, OF_DETACHED);
1968 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1975 * of_detach_node - "Unplug" a node from the device tree.
1977 * The caller must hold a reference to the node. The memory associated with
1978 * the node is not freed until its refcount goes to zero.
1980 int of_detach_node(struct device_node *np)
1982 struct device_node *parent;
1983 unsigned long flags;
1986 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1990 raw_spin_lock_irqsave(&devtree_lock, flags);
1992 if (of_node_check_flag(np, OF_DETACHED)) {
1993 /* someone already detached it */
1994 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1998 parent = np->parent;
2000 raw_spin_unlock_irqrestore(&devtree_lock, flags);
2004 if (of_allnodes == np)
2005 of_allnodes = np->allnext;
2007 struct device_node *prev;
2008 for (prev = of_allnodes;
2009 prev->allnext != np;
2010 prev = prev->allnext)
2012 prev->allnext = np->allnext;
2015 if (parent->child == np)
2016 parent->child = np->sibling;
2018 struct device_node *prevsib;
2019 for (prevsib = np->parent->child;
2020 prevsib->sibling != np;
2021 prevsib = prevsib->sibling)
2023 prevsib->sibling = np->sibling;
2026 of_node_set_flag(np, OF_DETACHED);
2027 raw_spin_unlock_irqrestore(&devtree_lock, flags);
2032 #endif /* defined(CONFIG_OF_DYNAMIC) */
2034 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
2035 int id, const char *stem, int stem_len)
2039 strncpy(ap->stem, stem, stem_len);
2040 ap->stem[stem_len] = 0;
2041 list_add_tail(&ap->link, &aliases_lookup);
2042 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
2043 ap->alias, ap->stem, ap->id, of_node_full_name(np));
2047 * of_alias_scan - Scan all properties of 'aliases' node
2049 * The function scans all the properties of 'aliases' node and populate
2050 * the the global lookup table with the properties. It returns the
2051 * number of alias_prop found, or error code in error case.
2053 * @dt_alloc: An allocator that provides a virtual address to memory
2054 * for the resulting tree
2056 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
2058 struct property *pp;
2060 of_chosen = of_find_node_by_path("/chosen");
2061 if (of_chosen == NULL)
2062 of_chosen = of_find_node_by_path("/chosen@0");
2065 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
2067 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
2069 of_stdout = of_find_node_by_path(name);
2072 of_aliases = of_find_node_by_path("/aliases");
2076 for_each_property_of_node(of_aliases, pp) {
2077 const char *start = pp->name;
2078 const char *end = start + strlen(start);
2079 struct device_node *np;
2080 struct alias_prop *ap;
2083 /* Skip those we do not want to proceed */
2084 if (!strcmp(pp->name, "name") ||
2085 !strcmp(pp->name, "phandle") ||
2086 !strcmp(pp->name, "linux,phandle"))
2089 np = of_find_node_by_path(pp->value);
2093 /* walk the alias backwards to extract the id and work out
2094 * the 'stem' string */
2095 while (isdigit(*(end-1)) && end > start)
2099 if (kstrtoint(end, 10, &id) < 0)
2102 /* Allocate an alias_prop with enough space for the stem */
2103 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
2106 memset(ap, 0, sizeof(*ap) + len + 1);
2108 of_alias_add(ap, np, id, start, len);
2113 * of_alias_get_id - Get alias id for the given device_node
2114 * @np: Pointer to the given device_node
2115 * @stem: Alias stem of the given device_node
2117 * The function travels the lookup table to get the alias id for the given
2118 * device_node and alias stem. It returns the alias id if found.
2120 int of_alias_get_id(struct device_node *np, const char *stem)
2122 struct alias_prop *app;
2125 mutex_lock(&of_aliases_mutex);
2126 list_for_each_entry(app, &aliases_lookup, link) {
2127 if (strcmp(app->stem, stem) != 0)
2130 if (np == app->np) {
2135 mutex_unlock(&of_aliases_mutex);
2139 EXPORT_SYMBOL_GPL(of_alias_get_id);
2141 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2144 const void *curv = cur;
2154 curv += sizeof(*cur);
2155 if (curv >= prop->value + prop->length)
2159 *pu = be32_to_cpup(curv);
2162 EXPORT_SYMBOL_GPL(of_prop_next_u32);
2164 const char *of_prop_next_string(struct property *prop, const char *cur)
2166 const void *curv = cur;
2174 curv += strlen(cur) + 1;
2175 if (curv >= prop->value + prop->length)
2180 EXPORT_SYMBOL_GPL(of_prop_next_string);
2183 * of_device_is_stdout_path - check if a device node matches the
2184 * linux,stdout-path property
2186 * Check if this device node matches the linux,stdout-path property
2187 * in the chosen node. return true if yes, false otherwise.
2189 int of_device_is_stdout_path(struct device_node *dn)
2194 return of_stdout == dn;
2196 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
2199 * of_find_next_cache_node - Find a node's subsidiary cache
2200 * @np: node of type "cpu" or "cache"
2202 * Returns a node pointer with refcount incremented, use
2203 * of_node_put() on it when done. Caller should hold a reference
2206 struct device_node *of_find_next_cache_node(const struct device_node *np)
2208 struct device_node *child;
2209 const phandle *handle;
2211 handle = of_get_property(np, "l2-cache", NULL);
2213 handle = of_get_property(np, "next-level-cache", NULL);
2216 return of_find_node_by_phandle(be32_to_cpup(handle));
2218 /* OF on pmac has nodes instead of properties named "l2-cache"
2219 * beneath CPU nodes.
2221 if (!strcmp(np->type, "cpu"))
2222 for_each_child_of_node(np, child)
2223 if (!strcmp(child->type, "cache"))
2230 * of_graph_parse_endpoint() - parse common endpoint node properties
2231 * @node: pointer to endpoint device_node
2232 * @endpoint: pointer to the OF endpoint data structure
2234 * The caller should hold a reference to @node.
2236 int of_graph_parse_endpoint(const struct device_node *node,
2237 struct of_endpoint *endpoint)
2239 struct device_node *port_node = of_get_parent(node);
2241 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2242 __func__, node->full_name);
2244 memset(endpoint, 0, sizeof(*endpoint));
2246 endpoint->local_node = node;
2248 * It doesn't matter whether the two calls below succeed.
2249 * If they don't then the default value 0 is used.
2251 of_property_read_u32(port_node, "reg", &endpoint->port);
2252 of_property_read_u32(node, "reg", &endpoint->id);
2254 of_node_put(port_node);
2258 EXPORT_SYMBOL(of_graph_parse_endpoint);
2261 * of_graph_get_next_endpoint() - get next endpoint node
2262 * @parent: pointer to the parent device node
2263 * @prev: previous endpoint node, or NULL to get first
2265 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2266 * of the passed @prev node is not decremented, the caller have to use
2267 * of_node_put() on it when done.
2269 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2270 struct device_node *prev)
2272 struct device_node *endpoint;
2273 struct device_node *port;
2279 * Start by locating the port node. If no previous endpoint is specified
2280 * search for the first port node, otherwise get the previous endpoint
2284 struct device_node *node;
2286 node = of_get_child_by_name(parent, "ports");
2290 port = of_get_child_by_name(parent, "port");
2294 pr_err("%s(): no port node found in %s\n",
2295 __func__, parent->full_name);
2299 port = of_get_parent(prev);
2300 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2301 __func__, prev->full_name))
2305 * Avoid dropping prev node refcount to 0 when getting the next
2313 * Now that we have a port node, get the next endpoint by
2314 * getting the next child. If the previous endpoint is NULL this
2315 * will return the first child.
2317 endpoint = of_get_next_child(port, prev);
2323 /* No more endpoints under this port, try the next one. */
2327 port = of_get_next_child(parent, port);
2330 } while (of_node_cmp(port->name, "port"));
2333 EXPORT_SYMBOL(of_graph_get_next_endpoint);
2336 * of_graph_get_remote_port_parent() - get remote port's parent node
2337 * @node: pointer to a local endpoint device_node
2339 * Return: Remote device node associated with remote endpoint node linked
2340 * to @node. Use of_node_put() on it when done.
2342 struct device_node *of_graph_get_remote_port_parent(
2343 const struct device_node *node)
2345 struct device_node *np;
2348 /* Get remote endpoint node. */
2349 np = of_parse_phandle(node, "remote-endpoint", 0);
2351 /* Walk 3 levels up only if there is 'ports' node. */
2352 for (depth = 3; depth && np; depth--) {
2353 np = of_get_next_parent(np);
2354 if (depth == 2 && of_node_cmp(np->name, "ports"))
2359 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2362 * of_graph_get_remote_port() - get remote port node
2363 * @node: pointer to a local endpoint device_node
2365 * Return: Remote port node associated with remote endpoint node linked
2366 * to @node. Use of_node_put() on it when done.
2368 struct device_node *of_graph_get_remote_port(const struct device_node *node)
2370 struct device_node *np;
2372 /* Get remote endpoint node. */
2373 np = of_parse_phandle(node, "remote-endpoint", 0);
2376 return of_get_next_parent(np);
2378 EXPORT_SYMBOL(of_graph_get_remote_port);