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/string.h>
27 #include <linux/proc_fs.h>
29 #include "of_private.h"
31 LIST_HEAD(aliases_lookup);
33 struct device_node *of_allnodes;
34 EXPORT_SYMBOL(of_allnodes);
35 struct device_node *of_chosen;
36 struct device_node *of_aliases;
37 static struct device_node *of_stdout;
39 static struct kset *of_kset;
42 * Used to protect the of_aliases; but also overloaded to hold off addition of
45 DEFINE_MUTEX(of_aliases_mutex);
47 /* use when traversing tree through the allnext, child, sibling,
48 * or parent members of struct device_node.
50 DEFINE_RAW_SPINLOCK(devtree_lock);
52 int of_n_addr_cells(struct device_node *np)
59 ip = of_get_property(np, "#address-cells", NULL);
61 return be32_to_cpup(ip);
63 /* No #address-cells property for the root node */
64 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
66 EXPORT_SYMBOL(of_n_addr_cells);
68 int of_n_size_cells(struct device_node *np)
75 ip = of_get_property(np, "#size-cells", NULL);
77 return be32_to_cpup(ip);
79 /* No #size-cells property for the root node */
80 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
82 EXPORT_SYMBOL(of_n_size_cells);
85 int __weak of_node_to_nid(struct device_node *np)
87 return numa_node_id();
91 #if defined(CONFIG_OF_DYNAMIC)
93 * of_node_get - Increment refcount of a node
94 * @node: Node to inc refcount, NULL is supported to
95 * simplify writing of callers
99 struct device_node *of_node_get(struct device_node *node)
102 kobject_get(&node->kobj);
105 EXPORT_SYMBOL(of_node_get);
107 static inline struct device_node *kobj_to_device_node(struct kobject *kobj)
109 return container_of(kobj, struct device_node, kobj);
113 * of_node_release - release a dynamically allocated node
114 * @kref: kref element of the node to be released
116 * In of_node_put() this function is passed to kref_put()
119 static void of_node_release(struct kobject *kobj)
121 struct device_node *node = kobj_to_device_node(kobj);
122 struct property *prop = node->properties;
124 /* We should never be releasing nodes that haven't been detached. */
125 if (!of_node_check_flag(node, OF_DETACHED)) {
126 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
131 if (!of_node_check_flag(node, OF_DYNAMIC))
135 struct property *next = prop->next;
142 prop = node->deadprops;
143 node->deadprops = NULL;
146 kfree(node->full_name);
152 * of_node_put - Decrement refcount of a node
153 * @node: Node to dec refcount, NULL is supported to
154 * simplify writing of callers
157 void of_node_put(struct device_node *node)
160 kobject_put(&node->kobj);
162 EXPORT_SYMBOL(of_node_put);
164 static void of_node_release(struct kobject *kobj)
166 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
168 #endif /* CONFIG_OF_DYNAMIC */
170 struct kobj_type of_node_ktype = {
171 .release = of_node_release,
174 static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
175 struct bin_attribute *bin_attr, char *buf,
176 loff_t offset, size_t count)
178 struct property *pp = container_of(bin_attr, struct property, attr);
179 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
182 static const char *safe_name(struct kobject *kobj, const char *orig_name)
184 const char *name = orig_name;
185 struct kernfs_node *kn;
188 /* don't be a hero. After 16 tries give up */
189 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
191 if (name != orig_name)
193 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
196 if (name != orig_name)
197 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
198 kobject_name(kobj), name);
202 static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
206 /* Important: Don't leak passwords */
207 bool secure = strncmp(pp->name, "security-", 9) == 0;
209 sysfs_bin_attr_init(&pp->attr);
210 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
211 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
212 pp->attr.size = secure ? 0 : pp->length;
213 pp->attr.read = of_node_property_read;
215 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
216 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
220 static int __of_node_add(struct device_node *np)
226 np->kobj.kset = of_kset;
228 /* Nodes without parents are new top level trees */
229 rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base"));
231 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
232 if (!name || !name[0])
235 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
240 for_each_property_of_node(np, pp)
241 __of_add_property_sysfs(np, pp);
246 int of_node_add(struct device_node *np)
249 kobject_init(&np->kobj, &of_node_ktype);
250 mutex_lock(&of_aliases_mutex);
252 rc = __of_node_add(np);
253 mutex_unlock(&of_aliases_mutex);
257 #if defined(CONFIG_OF_DYNAMIC)
258 static void of_node_remove(struct device_node *np)
262 for_each_property_of_node(np, pp)
263 sysfs_remove_bin_file(&np->kobj, &pp->attr);
265 kobject_del(&np->kobj);
269 static int __init of_init(void)
271 struct device_node *np;
273 /* Create the kset, and register existing nodes */
274 mutex_lock(&of_aliases_mutex);
275 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
277 mutex_unlock(&of_aliases_mutex);
280 for_each_of_allnodes(np)
282 mutex_unlock(&of_aliases_mutex);
284 /* Symlink in /proc as required by userspace ABI */
286 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
290 core_initcall(of_init);
292 static struct property *__of_find_property(const struct device_node *np,
293 const char *name, int *lenp)
300 for (pp = np->properties; pp; pp = pp->next) {
301 if (of_prop_cmp(pp->name, name) == 0) {
311 struct property *of_find_property(const struct device_node *np,
318 raw_spin_lock_irqsave(&devtree_lock, flags);
319 pp = __of_find_property(np, name, lenp);
320 raw_spin_unlock_irqrestore(&devtree_lock, flags);
324 EXPORT_SYMBOL(of_find_property);
327 * of_find_all_nodes - Get next node in global list
328 * @prev: Previous node or NULL to start iteration
329 * of_node_put() will be called on it
331 * Returns a node pointer with refcount incremented, use
332 * of_node_put() on it when done.
334 struct device_node *of_find_all_nodes(struct device_node *prev)
336 struct device_node *np;
339 raw_spin_lock_irqsave(&devtree_lock, flags);
340 np = prev ? prev->allnext : of_allnodes;
341 for (; np != NULL; np = np->allnext)
345 raw_spin_unlock_irqrestore(&devtree_lock, flags);
348 EXPORT_SYMBOL(of_find_all_nodes);
351 * Find a property with a given name for a given node
352 * and return the value.
354 static const void *__of_get_property(const struct device_node *np,
355 const char *name, int *lenp)
357 struct property *pp = __of_find_property(np, name, lenp);
359 return pp ? pp->value : NULL;
363 * Find a property with a given name for a given node
364 * and return the value.
366 const void *of_get_property(const struct device_node *np, const char *name,
369 struct property *pp = of_find_property(np, name, lenp);
371 return pp ? pp->value : NULL;
373 EXPORT_SYMBOL(of_get_property);
376 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
378 * @cpu: logical cpu index of a core/thread
379 * @phys_id: physical identifier of a core/thread
381 * CPU logical to physical index mapping is architecture specific.
382 * However this __weak function provides a default match of physical
383 * id to logical cpu index. phys_id provided here is usually values read
384 * from the device tree which must match the hardware internal registers.
386 * Returns true if the physical identifier and the logical cpu index
387 * correspond to the same core/thread, false otherwise.
389 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
391 return (u32)phys_id == cpu;
395 * Checks if the given "prop_name" property holds the physical id of the
396 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
397 * NULL, local thread number within the core is returned in it.
399 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
400 const char *prop_name, int cpu, unsigned int *thread)
403 int ac, prop_len, tid;
406 ac = of_n_addr_cells(cpun);
407 cell = of_get_property(cpun, prop_name, &prop_len);
410 prop_len /= sizeof(*cell) * ac;
411 for (tid = 0; tid < prop_len; tid++) {
412 hwid = of_read_number(cell, ac);
413 if (arch_match_cpu_phys_id(cpu, hwid)) {
424 * arch_find_n_match_cpu_physical_id - See if the given device node is
425 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
426 * else false. If 'thread' is non-NULL, the local thread number within the
427 * core is returned in it.
429 bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
430 int cpu, unsigned int *thread)
432 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
433 * for thread ids on PowerPC. If it doesn't exist fallback to
434 * standard "reg" property.
436 if (IS_ENABLED(CONFIG_PPC) &&
437 __of_find_n_match_cpu_property(cpun,
438 "ibm,ppc-interrupt-server#s",
442 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
449 * of_get_cpu_node - Get device node associated with the given logical CPU
451 * @cpu: CPU number(logical index) for which device node is required
452 * @thread: if not NULL, local thread number within the physical core is
455 * The main purpose of this function is to retrieve the device node for the
456 * given logical CPU index. It should be used to initialize the of_node in
457 * cpu device. Once of_node in cpu device is populated, all the further
458 * references can use that instead.
460 * CPU logical to physical index mapping is architecture specific and is built
461 * before booting secondary cores. This function uses arch_match_cpu_phys_id
462 * which can be overridden by architecture specific implementation.
464 * Returns a node pointer for the logical cpu if found, else NULL.
466 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
468 struct device_node *cpun;
470 for_each_node_by_type(cpun, "cpu") {
471 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
476 EXPORT_SYMBOL(of_get_cpu_node);
479 * __of_device_is_compatible() - Check if the node matches given constraints
480 * @device: pointer to node
481 * @compat: required compatible string, NULL or "" for any match
482 * @type: required device_type value, NULL or "" for any match
483 * @name: required node name, NULL or "" for any match
485 * Checks if the given @compat, @type and @name strings match the
486 * properties of the given @device. A constraints can be skipped by
487 * passing NULL or an empty string as the constraint.
489 * Returns 0 for no match, and a positive integer on match. The return
490 * value is a relative score with larger values indicating better
491 * matches. The score is weighted for the most specific compatible value
492 * to get the highest score. Matching type is next, followed by matching
493 * name. Practically speaking, this results in the following priority
496 * 1. specific compatible && type && name
497 * 2. specific compatible && type
498 * 3. specific compatible && name
499 * 4. specific compatible
500 * 5. general compatible && type && name
501 * 6. general compatible && type
502 * 7. general compatible && name
503 * 8. general compatible
508 static int __of_device_is_compatible(const struct device_node *device,
509 const char *compat, const char *type, const char *name)
511 struct property *prop;
513 int index = 0, score = 0;
515 /* Compatible match has highest priority */
516 if (compat && compat[0]) {
517 prop = __of_find_property(device, "compatible", NULL);
518 for (cp = of_prop_next_string(prop, NULL); cp;
519 cp = of_prop_next_string(prop, cp), index++) {
520 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
521 score = INT_MAX/2 - (index << 2);
529 /* Matching type is better than matching name */
530 if (type && type[0]) {
531 if (!device->type || of_node_cmp(type, device->type))
536 /* Matching name is a bit better than not */
537 if (name && name[0]) {
538 if (!device->name || of_node_cmp(name, device->name))
546 /** Checks if the given "compat" string matches one of the strings in
547 * the device's "compatible" property
549 int of_device_is_compatible(const struct device_node *device,
555 raw_spin_lock_irqsave(&devtree_lock, flags);
556 res = __of_device_is_compatible(device, compat, NULL, NULL);
557 raw_spin_unlock_irqrestore(&devtree_lock, flags);
560 EXPORT_SYMBOL(of_device_is_compatible);
563 * of_machine_is_compatible - Test root of device tree for a given compatible value
564 * @compat: compatible string to look for in root node's compatible property.
566 * Returns true if the root node has the given value in its
567 * compatible property.
569 int of_machine_is_compatible(const char *compat)
571 struct device_node *root;
574 root = of_find_node_by_path("/");
576 rc = of_device_is_compatible(root, compat);
581 EXPORT_SYMBOL(of_machine_is_compatible);
584 * __of_device_is_available - check if a device is available for use
586 * @device: Node to check for availability, with locks already held
588 * Returns 1 if the status property is absent or set to "okay" or "ok",
591 static int __of_device_is_available(const struct device_node *device)
599 status = __of_get_property(device, "status", &statlen);
604 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
612 * of_device_is_available - check if a device is available for use
614 * @device: Node to check for availability
616 * Returns 1 if the status property is absent or set to "okay" or "ok",
619 int of_device_is_available(const struct device_node *device)
624 raw_spin_lock_irqsave(&devtree_lock, flags);
625 res = __of_device_is_available(device);
626 raw_spin_unlock_irqrestore(&devtree_lock, flags);
630 EXPORT_SYMBOL(of_device_is_available);
633 * of_get_parent - Get a node's parent if any
634 * @node: Node to get parent
636 * Returns a node pointer with refcount incremented, use
637 * of_node_put() on it when done.
639 struct device_node *of_get_parent(const struct device_node *node)
641 struct device_node *np;
647 raw_spin_lock_irqsave(&devtree_lock, flags);
648 np = of_node_get(node->parent);
649 raw_spin_unlock_irqrestore(&devtree_lock, flags);
652 EXPORT_SYMBOL(of_get_parent);
655 * of_get_next_parent - Iterate to a node's parent
656 * @node: Node to get parent of
658 * This is like of_get_parent() except that it drops the
659 * refcount on the passed node, making it suitable for iterating
660 * through a node's parents.
662 * Returns a node pointer with refcount incremented, use
663 * of_node_put() on it when done.
665 struct device_node *of_get_next_parent(struct device_node *node)
667 struct device_node *parent;
673 raw_spin_lock_irqsave(&devtree_lock, flags);
674 parent = of_node_get(node->parent);
676 raw_spin_unlock_irqrestore(&devtree_lock, flags);
679 EXPORT_SYMBOL(of_get_next_parent);
682 * of_get_next_child - Iterate a node childs
684 * @prev: previous child of the parent node, or NULL to get first
686 * Returns a node pointer with refcount incremented, use
687 * of_node_put() on it when done.
689 struct device_node *of_get_next_child(const struct device_node *node,
690 struct device_node *prev)
692 struct device_node *next;
695 raw_spin_lock_irqsave(&devtree_lock, flags);
696 next = prev ? prev->sibling : node->child;
697 for (; next; next = next->sibling)
698 if (of_node_get(next))
701 raw_spin_unlock_irqrestore(&devtree_lock, flags);
704 EXPORT_SYMBOL(of_get_next_child);
707 * of_get_next_available_child - Find the next available child node
709 * @prev: previous child of the parent node, or NULL to get first
711 * This function is like of_get_next_child(), except that it
712 * automatically skips any disabled nodes (i.e. status = "disabled").
714 struct device_node *of_get_next_available_child(const struct device_node *node,
715 struct device_node *prev)
717 struct device_node *next;
720 raw_spin_lock_irqsave(&devtree_lock, flags);
721 next = prev ? prev->sibling : node->child;
722 for (; next; next = next->sibling) {
723 if (!__of_device_is_available(next))
725 if (of_node_get(next))
729 raw_spin_unlock_irqrestore(&devtree_lock, flags);
732 EXPORT_SYMBOL(of_get_next_available_child);
735 * of_get_child_by_name - Find the child node by name for a given parent
737 * @name: child name to look for.
739 * This function looks for child node for given matching name
741 * Returns a node pointer if found, with refcount incremented, use
742 * of_node_put() on it when done.
743 * Returns NULL if node is not found.
745 struct device_node *of_get_child_by_name(const struct device_node *node,
748 struct device_node *child;
750 for_each_child_of_node(node, child)
751 if (child->name && (of_node_cmp(child->name, name) == 0))
755 EXPORT_SYMBOL(of_get_child_by_name);
758 * of_find_node_by_path - Find a node matching a full OF path
759 * @path: The full path to match
761 * Returns a node pointer with refcount incremented, use
762 * of_node_put() on it when done.
764 struct device_node *of_find_node_by_path(const char *path)
766 struct device_node *np = of_allnodes;
769 raw_spin_lock_irqsave(&devtree_lock, flags);
770 for (; np; np = np->allnext) {
771 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
775 raw_spin_unlock_irqrestore(&devtree_lock, flags);
778 EXPORT_SYMBOL(of_find_node_by_path);
781 * of_find_node_by_name - Find a node by its "name" property
782 * @from: The node to start searching from or NULL, the node
783 * you pass will not be searched, only the next one
784 * will; typically, you pass what the previous call
785 * returned. of_node_put() will be called on it
786 * @name: The name string to match against
788 * Returns a node pointer with refcount incremented, use
789 * of_node_put() on it when done.
791 struct device_node *of_find_node_by_name(struct device_node *from,
794 struct device_node *np;
797 raw_spin_lock_irqsave(&devtree_lock, flags);
798 np = from ? from->allnext : of_allnodes;
799 for (; np; np = np->allnext)
800 if (np->name && (of_node_cmp(np->name, name) == 0)
804 raw_spin_unlock_irqrestore(&devtree_lock, flags);
807 EXPORT_SYMBOL(of_find_node_by_name);
810 * of_find_node_by_type - Find a node by its "device_type" property
811 * @from: The node to start searching from, or NULL to start searching
812 * the entire device tree. The node you pass will not be
813 * searched, only the next one will; typically, you pass
814 * what the previous call returned. of_node_put() will be
815 * called on from for you.
816 * @type: The type string to match against
818 * Returns a node pointer with refcount incremented, use
819 * of_node_put() on it when done.
821 struct device_node *of_find_node_by_type(struct device_node *from,
824 struct device_node *np;
827 raw_spin_lock_irqsave(&devtree_lock, flags);
828 np = from ? from->allnext : of_allnodes;
829 for (; np; np = np->allnext)
830 if (np->type && (of_node_cmp(np->type, type) == 0)
834 raw_spin_unlock_irqrestore(&devtree_lock, flags);
837 EXPORT_SYMBOL(of_find_node_by_type);
840 * of_find_compatible_node - Find a node based on type and one of the
841 * tokens in its "compatible" property
842 * @from: The node to start searching from or NULL, the node
843 * you pass will not be searched, only the next one
844 * will; typically, you pass what the previous call
845 * returned. of_node_put() will be called on it
846 * @type: The type string to match "device_type" or NULL to ignore
847 * @compatible: The string to match to one of the tokens in the device
850 * Returns a node pointer with refcount incremented, use
851 * of_node_put() on it when done.
853 struct device_node *of_find_compatible_node(struct device_node *from,
854 const char *type, const char *compatible)
856 struct device_node *np;
859 raw_spin_lock_irqsave(&devtree_lock, flags);
860 np = from ? from->allnext : of_allnodes;
861 for (; np; np = np->allnext) {
862 if (__of_device_is_compatible(np, compatible, type, NULL) &&
867 raw_spin_unlock_irqrestore(&devtree_lock, flags);
870 EXPORT_SYMBOL(of_find_compatible_node);
873 * of_find_node_with_property - Find a node which has a property with
875 * @from: The node to start searching from or NULL, the node
876 * you pass will not be searched, only the next one
877 * will; typically, you pass what the previous call
878 * returned. of_node_put() will be called on it
879 * @prop_name: The name of the property to look for.
881 * Returns a node pointer with refcount incremented, use
882 * of_node_put() on it when done.
884 struct device_node *of_find_node_with_property(struct device_node *from,
885 const char *prop_name)
887 struct device_node *np;
891 raw_spin_lock_irqsave(&devtree_lock, flags);
892 np = from ? from->allnext : of_allnodes;
893 for (; np; np = np->allnext) {
894 for (pp = np->properties; pp; pp = pp->next) {
895 if (of_prop_cmp(pp->name, prop_name) == 0) {
903 raw_spin_unlock_irqrestore(&devtree_lock, flags);
906 EXPORT_SYMBOL(of_find_node_with_property);
909 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
910 const struct device_node *node)
912 const struct of_device_id *best_match = NULL;
913 int score, best_score = 0;
918 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
919 score = __of_device_is_compatible(node, matches->compatible,
920 matches->type, matches->name);
921 if (score > best_score) {
922 best_match = matches;
931 * of_match_node - Tell if an device_node has a matching of_match structure
932 * @matches: array of of device match structures to search in
933 * @node: the of device structure to match against
935 * Low level utility function used by device matching.
937 const struct of_device_id *of_match_node(const struct of_device_id *matches,
938 const struct device_node *node)
940 const struct of_device_id *match;
943 raw_spin_lock_irqsave(&devtree_lock, flags);
944 match = __of_match_node(matches, node);
945 raw_spin_unlock_irqrestore(&devtree_lock, flags);
948 EXPORT_SYMBOL(of_match_node);
951 * of_find_matching_node_and_match - Find a node based on an of_device_id
953 * @from: The node to start searching from or NULL, the node
954 * you pass will not be searched, only the next one
955 * will; typically, you pass what the previous call
956 * returned. of_node_put() will be called on it
957 * @matches: array of of device match structures to search in
958 * @match Updated to point at the matches entry which matched
960 * Returns a node pointer with refcount incremented, use
961 * of_node_put() on it when done.
963 struct device_node *of_find_matching_node_and_match(struct device_node *from,
964 const struct of_device_id *matches,
965 const struct of_device_id **match)
967 struct device_node *np;
968 const struct of_device_id *m;
974 raw_spin_lock_irqsave(&devtree_lock, flags);
975 np = from ? from->allnext : of_allnodes;
976 for (; np; np = np->allnext) {
977 m = __of_match_node(matches, np);
978 if (m && of_node_get(np)) {
985 raw_spin_unlock_irqrestore(&devtree_lock, flags);
988 EXPORT_SYMBOL(of_find_matching_node_and_match);
991 * of_modalias_node - Lookup appropriate modalias for a device node
992 * @node: pointer to a device tree node
993 * @modalias: Pointer to buffer that modalias value will be copied into
994 * @len: Length of modalias value
996 * Based on the value of the compatible property, this routine will attempt
997 * to choose an appropriate modalias value for a particular device tree node.
998 * It does this by stripping the manufacturer prefix (as delimited by a ',')
999 * from the first entry in the compatible list property.
1001 * This routine returns 0 on success, <0 on failure.
1003 int of_modalias_node(struct device_node *node, char *modalias, int len)
1005 const char *compatible, *p;
1008 compatible = of_get_property(node, "compatible", &cplen);
1009 if (!compatible || strlen(compatible) > cplen)
1011 p = strchr(compatible, ',');
1012 strlcpy(modalias, p ? p + 1 : compatible, len);
1015 EXPORT_SYMBOL_GPL(of_modalias_node);
1018 * of_find_node_by_phandle - Find a node given a phandle
1019 * @handle: phandle of the node to find
1021 * Returns a node pointer with refcount incremented, use
1022 * of_node_put() on it when done.
1024 struct device_node *of_find_node_by_phandle(phandle handle)
1026 struct device_node *np;
1027 unsigned long flags;
1029 raw_spin_lock_irqsave(&devtree_lock, flags);
1030 for (np = of_allnodes; np; np = np->allnext)
1031 if (np->phandle == handle)
1034 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1037 EXPORT_SYMBOL(of_find_node_by_phandle);
1040 * of_find_property_value_of_size
1042 * @np: device node from which the property value is to be read.
1043 * @propname: name of the property to be searched.
1044 * @len: requested length of property value
1046 * Search for a property in a device node and valid the requested size.
1047 * Returns the property value on success, -EINVAL if the property does not
1048 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1049 * property data isn't large enough.
1052 static void *of_find_property_value_of_size(const struct device_node *np,
1053 const char *propname, u32 len)
1055 struct property *prop = of_find_property(np, propname, NULL);
1058 return ERR_PTR(-EINVAL);
1060 return ERR_PTR(-ENODATA);
1061 if (len > prop->length)
1062 return ERR_PTR(-EOVERFLOW);
1068 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1070 * @np: device node from which the property value is to be read.
1071 * @propname: name of the property to be searched.
1072 * @index: index of the u32 in the list of values
1073 * @out_value: pointer to return value, modified only if no error.
1075 * Search for a property in a device node and read nth 32-bit value from
1076 * it. Returns 0 on success, -EINVAL if the property does not exist,
1077 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1078 * property data isn't large enough.
1080 * The out_value is modified only if a valid u32 value can be decoded.
1082 int of_property_read_u32_index(const struct device_node *np,
1083 const char *propname,
1084 u32 index, u32 *out_value)
1086 const u32 *val = of_find_property_value_of_size(np, propname,
1087 ((index + 1) * sizeof(*out_value)));
1090 return PTR_ERR(val);
1092 *out_value = be32_to_cpup(((__be32 *)val) + index);
1095 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1098 * of_property_read_u8_array - Find and read an array of u8 from a property.
1100 * @np: device node from which the property value is to be read.
1101 * @propname: name of the property to be searched.
1102 * @out_values: pointer to return value, modified only if return value is 0.
1103 * @sz: number of array elements to read
1105 * Search for a property in a device node and read 8-bit value(s) from
1106 * it. Returns 0 on success, -EINVAL if the property does not exist,
1107 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1108 * property data isn't large enough.
1110 * dts entry of array should be like:
1111 * property = /bits/ 8 <0x50 0x60 0x70>;
1113 * The out_values is modified only if a valid u8 value can be decoded.
1115 int of_property_read_u8_array(const struct device_node *np,
1116 const char *propname, u8 *out_values, size_t sz)
1118 const u8 *val = of_find_property_value_of_size(np, propname,
1119 (sz * sizeof(*out_values)));
1122 return PTR_ERR(val);
1125 *out_values++ = *val++;
1128 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1131 * of_property_read_u16_array - Find and read an array of u16 from 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 * @out_values: pointer to return value, modified only if return value is 0.
1136 * @sz: number of array elements to read
1138 * Search for a property in a device node and read 16-bit value(s) from
1139 * it. Returns 0 on success, -EINVAL if the property does not exist,
1140 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1141 * property data isn't large enough.
1143 * dts entry of array should be like:
1144 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1146 * The out_values is modified only if a valid u16 value can be decoded.
1148 int of_property_read_u16_array(const struct device_node *np,
1149 const char *propname, u16 *out_values, size_t sz)
1151 const __be16 *val = of_find_property_value_of_size(np, propname,
1152 (sz * sizeof(*out_values)));
1155 return PTR_ERR(val);
1158 *out_values++ = be16_to_cpup(val++);
1161 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1164 * of_property_read_u32_array - Find and read an array of 32 bit integers
1167 * @np: device node from which the property value is to be read.
1168 * @propname: name of the property to be searched.
1169 * @out_values: pointer to return value, modified only if return value is 0.
1170 * @sz: number of array elements to read
1172 * Search for a property in a device node and read 32-bit value(s) from
1173 * it. Returns 0 on success, -EINVAL if the property does not exist,
1174 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1175 * property data isn't large enough.
1177 * The out_values is modified only if a valid u32 value can be decoded.
1179 int of_property_read_u32_array(const struct device_node *np,
1180 const char *propname, u32 *out_values,
1183 const __be32 *val = of_find_property_value_of_size(np, propname,
1184 (sz * sizeof(*out_values)));
1187 return PTR_ERR(val);
1190 *out_values++ = be32_to_cpup(val++);
1193 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1196 * of_property_read_u64 - Find and read a 64 bit integer from a property
1197 * @np: device node from which the property value is to be read.
1198 * @propname: name of the property to be searched.
1199 * @out_value: pointer to return value, modified only if return value is 0.
1201 * Search for a property in a device node and read a 64-bit value from
1202 * it. Returns 0 on success, -EINVAL if the property does not exist,
1203 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1204 * property data isn't large enough.
1206 * The out_value is modified only if a valid u64 value can be decoded.
1208 int of_property_read_u64(const struct device_node *np, const char *propname,
1211 const __be32 *val = of_find_property_value_of_size(np, propname,
1212 sizeof(*out_value));
1215 return PTR_ERR(val);
1217 *out_value = of_read_number(val, 2);
1220 EXPORT_SYMBOL_GPL(of_property_read_u64);
1223 * of_property_read_string - Find and read a string from a property
1224 * @np: device node from which the property value is to be read.
1225 * @propname: name of the property to be searched.
1226 * @out_string: pointer to null terminated return string, modified only if
1227 * return value is 0.
1229 * Search for a property in a device tree node and retrieve a null
1230 * terminated string value (pointer to data, not a copy). Returns 0 on
1231 * success, -EINVAL if the property does not exist, -ENODATA if property
1232 * does not have a value, and -EILSEQ if the string is not null-terminated
1233 * within the length of the property data.
1235 * The out_string pointer is modified only if a valid string can be decoded.
1237 int of_property_read_string(struct device_node *np, const char *propname,
1238 const char **out_string)
1240 struct property *prop = of_find_property(np, propname, NULL);
1245 if (strnlen(prop->value, prop->length) >= prop->length)
1247 *out_string = prop->value;
1250 EXPORT_SYMBOL_GPL(of_property_read_string);
1253 * of_property_read_string_index - Find and read a string from a multiple
1255 * @np: device node from which the property value is to be read.
1256 * @propname: name of the property to be searched.
1257 * @index: index of the string in the list of strings
1258 * @out_string: pointer to null terminated return string, modified only if
1259 * return value is 0.
1261 * Search for a property in a device tree node and retrieve a null
1262 * terminated string value (pointer to data, not a copy) in the list of strings
1263 * contained in that property.
1264 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1265 * property does not have a value, and -EILSEQ if the string is not
1266 * null-terminated within the length of the property data.
1268 * The out_string pointer is modified only if a valid string can be decoded.
1270 int of_property_read_string_index(struct device_node *np, const char *propname,
1271 int index, const char **output)
1273 struct property *prop = of_find_property(np, propname, NULL);
1275 size_t l = 0, total = 0;
1282 if (strnlen(prop->value, prop->length) >= prop->length)
1287 for (i = 0; total < prop->length; total += l, p += l) {
1296 EXPORT_SYMBOL_GPL(of_property_read_string_index);
1299 * of_property_match_string() - Find string in a list and return index
1300 * @np: pointer to node containing string list property
1301 * @propname: string list property name
1302 * @string: pointer to string to search for in string list
1304 * This function searches a string list property and returns the index
1305 * of a specific string value.
1307 int of_property_match_string(struct device_node *np, const char *propname,
1310 struct property *prop = of_find_property(np, propname, NULL);
1313 const char *p, *end;
1321 end = p + prop->length;
1323 for (i = 0; p < end; i++, p += l) {
1327 pr_debug("comparing %s with %s\n", string, p);
1328 if (strcmp(string, p) == 0)
1329 return i; /* Found it; return index */
1333 EXPORT_SYMBOL_GPL(of_property_match_string);
1336 * of_property_count_strings - Find and return the number of strings from a
1337 * multiple strings property.
1338 * @np: device node from which the property value is to be read.
1339 * @propname: name of the property to be searched.
1341 * Search for a property in a device tree node and retrieve the number of null
1342 * terminated string contain in it. Returns the number of strings on
1343 * success, -EINVAL if the property does not exist, -ENODATA if property
1344 * does not have a value, and -EILSEQ if the string is not null-terminated
1345 * within the length of the property data.
1347 int of_property_count_strings(struct device_node *np, const char *propname)
1349 struct property *prop = of_find_property(np, propname, NULL);
1351 size_t l = 0, total = 0;
1358 if (strnlen(prop->value, prop->length) >= prop->length)
1363 for (i = 0; total < prop->length; total += l, p += l, i++)
1368 EXPORT_SYMBOL_GPL(of_property_count_strings);
1370 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1373 printk("%s %s", msg, of_node_full_name(args->np));
1374 for (i = 0; i < args->args_count; i++)
1375 printk(i ? ",%08x" : ":%08x", args->args[i]);
1379 static int __of_parse_phandle_with_args(const struct device_node *np,
1380 const char *list_name,
1381 const char *cells_name,
1382 int cell_count, int index,
1383 struct of_phandle_args *out_args)
1385 const __be32 *list, *list_end;
1386 int rc = 0, size, cur_index = 0;
1388 struct device_node *node = NULL;
1391 /* Retrieve the phandle list property */
1392 list = of_get_property(np, list_name, &size);
1395 list_end = list + size / sizeof(*list);
1397 /* Loop over the phandles until all the requested entry is found */
1398 while (list < list_end) {
1403 * If phandle is 0, then it is an empty entry with no
1404 * arguments. Skip forward to the next entry.
1406 phandle = be32_to_cpup(list++);
1409 * Find the provider node and parse the #*-cells
1410 * property to determine the argument length.
1412 * This is not needed if the cell count is hard-coded
1413 * (i.e. cells_name not set, but cell_count is set),
1414 * except when we're going to return the found node
1417 if (cells_name || cur_index == index) {
1418 node = of_find_node_by_phandle(phandle);
1420 pr_err("%s: could not find phandle\n",
1427 if (of_property_read_u32(node, cells_name,
1429 pr_err("%s: could not get %s for %s\n",
1430 np->full_name, cells_name,
1439 * Make sure that the arguments actually fit in the
1440 * remaining property data length
1442 if (list + count > list_end) {
1443 pr_err("%s: arguments longer than property\n",
1450 * All of the error cases above bail out of the loop, so at
1451 * this point, the parsing is successful. If the requested
1452 * index matches, then fill the out_args structure and return,
1453 * or return -ENOENT for an empty entry.
1456 if (cur_index == index) {
1462 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1463 count = MAX_PHANDLE_ARGS;
1464 out_args->np = node;
1465 out_args->args_count = count;
1466 for (i = 0; i < count; i++)
1467 out_args->args[i] = be32_to_cpup(list++);
1472 /* Found it! return success */
1483 * Unlock node before returning result; will be one of:
1484 * -ENOENT : index is for empty phandle
1485 * -EINVAL : parsing error on data
1486 * [1..n] : Number of phandle (count mode; when index = -1)
1488 rc = index < 0 ? cur_index : -ENOENT;
1496 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1497 * @np: Pointer to device node holding phandle property
1498 * @phandle_name: Name of property holding a phandle value
1499 * @index: For properties holding a table of phandles, this is the index into
1502 * Returns the device_node pointer with refcount incremented. Use
1503 * of_node_put() on it when done.
1505 struct device_node *of_parse_phandle(const struct device_node *np,
1506 const char *phandle_name, int index)
1508 struct of_phandle_args args;
1513 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1519 EXPORT_SYMBOL(of_parse_phandle);
1522 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1523 * @np: pointer to a device tree node containing a list
1524 * @list_name: property name that contains a list
1525 * @cells_name: property name that specifies phandles' arguments count
1526 * @index: index of a phandle to parse out
1527 * @out_args: optional pointer to output arguments structure (will be filled)
1529 * This function is useful to parse lists of phandles and their arguments.
1530 * Returns 0 on success and fills out_args, on error returns appropriate
1533 * Caller is responsible to call of_node_put() on the returned out_args->node
1539 * #list-cells = <2>;
1543 * #list-cells = <1>;
1547 * list = <&phandle1 1 2 &phandle2 3>;
1550 * To get a device_node of the `node2' node you may call this:
1551 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1553 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1554 const char *cells_name, int index,
1555 struct of_phandle_args *out_args)
1559 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1562 EXPORT_SYMBOL(of_parse_phandle_with_args);
1565 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1566 * @np: pointer to a device tree node containing a list
1567 * @list_name: property name that contains a list
1568 * @cell_count: number of argument cells following the phandle
1569 * @index: index of a phandle to parse out
1570 * @out_args: optional pointer to output arguments structure (will be filled)
1572 * This function is useful to parse lists of phandles and their arguments.
1573 * Returns 0 on success and fills out_args, on error returns appropriate
1576 * Caller is responsible to call of_node_put() on the returned out_args->node
1588 * list = <&phandle1 0 2 &phandle2 2 3>;
1591 * To get a device_node of the `node2' node you may call this:
1592 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1594 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1595 const char *list_name, int cell_count,
1596 int index, struct of_phandle_args *out_args)
1600 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1603 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1606 * of_count_phandle_with_args() - Find the number of phandles references in a property
1607 * @np: pointer to a device tree node containing a list
1608 * @list_name: property name that contains a list
1609 * @cells_name: property name that specifies phandles' arguments count
1611 * Returns the number of phandle + argument tuples within a property. It
1612 * is a typical pattern to encode a list of phandle and variable
1613 * arguments into a single property. The number of arguments is encoded
1614 * by a property in the phandle-target node. For example, a gpios
1615 * property would contain a list of GPIO specifies consisting of a
1616 * phandle and 1 or more arguments. The number of arguments are
1617 * determined by the #gpio-cells property in the node pointed to by the
1620 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1621 const char *cells_name)
1623 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1626 EXPORT_SYMBOL(of_count_phandle_with_args);
1628 #if defined(CONFIG_OF_DYNAMIC)
1629 static int of_property_notify(int action, struct device_node *np,
1630 struct property *prop)
1632 struct of_prop_reconfig pr;
1636 return of_reconfig_notify(action, &pr);
1639 static int of_property_notify(int action, struct device_node *np,
1640 struct property *prop)
1647 * __of_add_property - Add a property to a node without lock operations
1649 static int __of_add_property(struct device_node *np, struct property *prop)
1651 struct property **next;
1654 next = &np->properties;
1656 if (strcmp(prop->name, (*next)->name) == 0)
1657 /* duplicate ! don't insert it */
1660 next = &(*next)->next;
1668 * of_add_property - Add a property to a node
1670 int of_add_property(struct device_node *np, struct property *prop)
1672 unsigned long flags;
1675 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1679 raw_spin_lock_irqsave(&devtree_lock, flags);
1680 rc = __of_add_property(np, prop);
1681 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1685 /* at early boot, bail hear and defer setup to of_init() */
1689 __of_add_property_sysfs(np, prop);
1695 * of_remove_property - Remove a property from a node.
1697 * Note that we don't actually remove it, since we have given out
1698 * who-knows-how-many pointers to the data using get-property.
1699 * Instead we just move the property to the "dead properties"
1700 * list, so it won't be found any more.
1702 int of_remove_property(struct device_node *np, struct property *prop)
1704 struct property **next;
1705 unsigned long flags;
1709 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1713 raw_spin_lock_irqsave(&devtree_lock, flags);
1714 next = &np->properties;
1716 if (*next == prop) {
1717 /* found the node */
1719 prop->next = np->deadprops;
1720 np->deadprops = prop;
1724 next = &(*next)->next;
1726 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1731 /* at early boot, bail hear and defer setup to of_init() */
1735 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1741 * of_update_property - Update a property in a node, if the property does
1742 * not exist, add it.
1744 * Note that we don't actually remove it, since we have given out
1745 * who-knows-how-many pointers to the data using get-property.
1746 * Instead we just move the property to the "dead properties" list,
1747 * and add the new property to the property list
1749 int of_update_property(struct device_node *np, struct property *newprop)
1751 struct property **next, *oldprop;
1752 unsigned long flags;
1755 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1762 oldprop = of_find_property(np, newprop->name, NULL);
1764 return of_add_property(np, newprop);
1766 raw_spin_lock_irqsave(&devtree_lock, flags);
1767 next = &np->properties;
1769 if (*next == oldprop) {
1770 /* found the node */
1771 newprop->next = oldprop->next;
1773 oldprop->next = np->deadprops;
1774 np->deadprops = oldprop;
1778 next = &(*next)->next;
1780 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1784 /* Update the sysfs attribute */
1786 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1787 __of_add_property_sysfs(np, newprop);
1795 #if defined(CONFIG_OF_DYNAMIC)
1797 * Support for dynamic device trees.
1799 * On some platforms, the device tree can be manipulated at runtime.
1800 * The routines in this section support adding, removing and changing
1801 * device tree nodes.
1804 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1806 int of_reconfig_notifier_register(struct notifier_block *nb)
1808 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1810 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1812 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1814 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1816 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1818 int of_reconfig_notify(unsigned long action, void *p)
1822 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1823 return notifier_to_errno(rc);
1827 * of_attach_node - Plug a device node into the tree and global list.
1829 int of_attach_node(struct device_node *np)
1831 unsigned long flags;
1834 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1838 raw_spin_lock_irqsave(&devtree_lock, flags);
1839 np->sibling = np->parent->child;
1840 np->allnext = of_allnodes;
1841 np->parent->child = np;
1843 of_node_clear_flag(np, OF_DETACHED);
1844 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1851 * of_detach_node - "Unplug" a node from the device tree.
1853 * The caller must hold a reference to the node. The memory associated with
1854 * the node is not freed until its refcount goes to zero.
1856 int of_detach_node(struct device_node *np)
1858 struct device_node *parent;
1859 unsigned long flags;
1862 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1866 raw_spin_lock_irqsave(&devtree_lock, flags);
1868 if (of_node_check_flag(np, OF_DETACHED)) {
1869 /* someone already detached it */
1870 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1874 parent = np->parent;
1876 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1880 if (of_allnodes == np)
1881 of_allnodes = np->allnext;
1883 struct device_node *prev;
1884 for (prev = of_allnodes;
1885 prev->allnext != np;
1886 prev = prev->allnext)
1888 prev->allnext = np->allnext;
1891 if (parent->child == np)
1892 parent->child = np->sibling;
1894 struct device_node *prevsib;
1895 for (prevsib = np->parent->child;
1896 prevsib->sibling != np;
1897 prevsib = prevsib->sibling)
1899 prevsib->sibling = np->sibling;
1902 of_node_set_flag(np, OF_DETACHED);
1903 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1908 #endif /* defined(CONFIG_OF_DYNAMIC) */
1910 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1911 int id, const char *stem, int stem_len)
1915 strncpy(ap->stem, stem, stem_len);
1916 ap->stem[stem_len] = 0;
1917 list_add_tail(&ap->link, &aliases_lookup);
1918 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1919 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1923 * of_alias_scan - Scan all properties of 'aliases' node
1925 * The function scans all the properties of 'aliases' node and populate
1926 * the the global lookup table with the properties. It returns the
1927 * number of alias_prop found, or error code in error case.
1929 * @dt_alloc: An allocator that provides a virtual address to memory
1930 * for the resulting tree
1932 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1934 struct property *pp;
1936 of_chosen = of_find_node_by_path("/chosen");
1937 if (of_chosen == NULL)
1938 of_chosen = of_find_node_by_path("/chosen@0");
1943 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1945 of_stdout = of_find_node_by_path(name);
1948 of_aliases = of_find_node_by_path("/aliases");
1952 for_each_property_of_node(of_aliases, pp) {
1953 const char *start = pp->name;
1954 const char *end = start + strlen(start);
1955 struct device_node *np;
1956 struct alias_prop *ap;
1959 /* Skip those we do not want to proceed */
1960 if (!strcmp(pp->name, "name") ||
1961 !strcmp(pp->name, "phandle") ||
1962 !strcmp(pp->name, "linux,phandle"))
1965 np = of_find_node_by_path(pp->value);
1969 /* walk the alias backwards to extract the id and work out
1970 * the 'stem' string */
1971 while (isdigit(*(end-1)) && end > start)
1975 if (kstrtoint(end, 10, &id) < 0)
1978 /* Allocate an alias_prop with enough space for the stem */
1979 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1982 memset(ap, 0, sizeof(*ap) + len + 1);
1984 of_alias_add(ap, np, id, start, len);
1989 * of_alias_get_id - Get alias id for the given device_node
1990 * @np: Pointer to the given device_node
1991 * @stem: Alias stem of the given device_node
1993 * The function travels the lookup table to get alias id for the given
1994 * device_node and alias stem. It returns the alias id if find it.
1996 int of_alias_get_id(struct device_node *np, const char *stem)
1998 struct alias_prop *app;
2001 mutex_lock(&of_aliases_mutex);
2002 list_for_each_entry(app, &aliases_lookup, link) {
2003 if (strcmp(app->stem, stem) != 0)
2006 if (np == app->np) {
2011 mutex_unlock(&of_aliases_mutex);
2015 EXPORT_SYMBOL_GPL(of_alias_get_id);
2017 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2020 const void *curv = cur;
2030 curv += sizeof(*cur);
2031 if (curv >= prop->value + prop->length)
2035 *pu = be32_to_cpup(curv);
2038 EXPORT_SYMBOL_GPL(of_prop_next_u32);
2040 const char *of_prop_next_string(struct property *prop, const char *cur)
2042 const void *curv = cur;
2050 curv += strlen(cur) + 1;
2051 if (curv >= prop->value + prop->length)
2056 EXPORT_SYMBOL_GPL(of_prop_next_string);
2059 * of_device_is_stdout_path - check if a device node matches the
2060 * linux,stdout-path property
2062 * Check if this device node matches the linux,stdout-path property
2063 * in the chosen node. return true if yes, false otherwise.
2065 int of_device_is_stdout_path(struct device_node *dn)
2070 return of_stdout == dn;
2072 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
2075 * of_find_next_cache_node - Find a node's subsidiary cache
2076 * @np: node of type "cpu" or "cache"
2078 * Returns a node pointer with refcount incremented, use
2079 * of_node_put() on it when done. Caller should hold a reference
2082 struct device_node *of_find_next_cache_node(const struct device_node *np)
2084 struct device_node *child;
2085 const phandle *handle;
2087 handle = of_get_property(np, "l2-cache", NULL);
2089 handle = of_get_property(np, "next-level-cache", NULL);
2092 return of_find_node_by_phandle(be32_to_cpup(handle));
2094 /* OF on pmac has nodes instead of properties named "l2-cache"
2095 * beneath CPU nodes.
2097 if (!strcmp(np->type, "cpu"))
2098 for_each_child_of_node(np, child)
2099 if (!strcmp(child->type, "cache"))