2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 #include <linux/ctype.h>
21 #include <linux/cpu.h>
22 #include <linux/module.h>
24 #include <linux/of_graph.h>
25 #include <linux/spinlock.h>
26 #include <linux/slab.h>
27 #include <linux/proc_fs.h>
29 #include "of_private.h"
31 LIST_HEAD(aliases_lookup);
33 struct device_node *of_allnodes;
34 EXPORT_SYMBOL(of_allnodes);
35 struct device_node *of_chosen;
36 struct device_node *of_aliases;
37 static struct device_node *of_stdout;
39 DEFINE_MUTEX(of_aliases_mutex);
41 /* use when traversing tree through the allnext, child, sibling,
42 * or parent members of struct device_node.
44 DEFINE_RAW_SPINLOCK(devtree_lock);
46 int of_n_addr_cells(struct device_node *np)
53 ip = of_get_property(np, "#address-cells", NULL);
55 return be32_to_cpup(ip);
57 /* No #address-cells property for the root node */
58 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
60 EXPORT_SYMBOL(of_n_addr_cells);
62 int of_n_size_cells(struct device_node *np)
69 ip = of_get_property(np, "#size-cells", NULL);
71 return be32_to_cpup(ip);
73 /* No #size-cells property for the root node */
74 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
76 EXPORT_SYMBOL(of_n_size_cells);
79 int __weak of_node_to_nid(struct device_node *np)
81 return numa_node_id();
85 #if defined(CONFIG_OF_DYNAMIC)
87 * of_node_get - Increment refcount of a node
88 * @node: Node to inc refcount, NULL is supported to
89 * simplify writing of callers
93 struct device_node *of_node_get(struct device_node *node)
96 kref_get(&node->kref);
99 EXPORT_SYMBOL(of_node_get);
101 static inline struct device_node *kref_to_device_node(struct kref *kref)
103 return container_of(kref, struct device_node, kref);
107 * of_node_release - release a dynamically allocated node
108 * @kref: kref element of the node to be released
110 * In of_node_put() this function is passed to kref_put()
113 static void of_node_release(struct kref *kref)
115 struct device_node *node = kref_to_device_node(kref);
116 struct property *prop = node->properties;
118 /* We should never be releasing nodes that haven't been detached. */
119 if (!of_node_check_flag(node, OF_DETACHED)) {
120 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
122 kref_init(&node->kref);
126 if (!of_node_check_flag(node, OF_DYNAMIC))
130 struct property *next = prop->next;
137 prop = node->deadprops;
138 node->deadprops = NULL;
141 kfree(node->full_name);
147 * of_node_put - Decrement refcount of a node
148 * @node: Node to dec refcount, NULL is supported to
149 * simplify writing of callers
152 void of_node_put(struct device_node *node)
155 kref_put(&node->kref, of_node_release);
157 EXPORT_SYMBOL(of_node_put);
158 #endif /* CONFIG_OF_DYNAMIC */
160 static struct property *__of_find_property(const struct device_node *np,
161 const char *name, int *lenp)
168 for (pp = np->properties; pp; pp = pp->next) {
169 if (of_prop_cmp(pp->name, name) == 0) {
179 struct property *of_find_property(const struct device_node *np,
186 raw_spin_lock_irqsave(&devtree_lock, flags);
187 pp = __of_find_property(np, name, lenp);
188 raw_spin_unlock_irqrestore(&devtree_lock, flags);
192 EXPORT_SYMBOL(of_find_property);
195 * of_find_all_nodes - Get next node in global list
196 * @prev: Previous node or NULL to start iteration
197 * of_node_put() will be called on it
199 * Returns a node pointer with refcount incremented, use
200 * of_node_put() on it when done.
202 struct device_node *of_find_all_nodes(struct device_node *prev)
204 struct device_node *np;
207 raw_spin_lock_irqsave(&devtree_lock, flags);
208 np = prev ? prev->allnext : of_allnodes;
209 for (; np != NULL; np = np->allnext)
213 raw_spin_unlock_irqrestore(&devtree_lock, flags);
216 EXPORT_SYMBOL(of_find_all_nodes);
219 * Find a property with a given name for a given node
220 * and return the value.
222 static const void *__of_get_property(const struct device_node *np,
223 const char *name, int *lenp)
225 struct property *pp = __of_find_property(np, name, lenp);
227 return pp ? pp->value : NULL;
231 * Find a property with a given name for a given node
232 * and return the value.
234 const void *of_get_property(const struct device_node *np, const char *name,
237 struct property *pp = of_find_property(np, name, lenp);
239 return pp ? pp->value : NULL;
241 EXPORT_SYMBOL(of_get_property);
244 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
246 * @cpu: logical cpu index of a core/thread
247 * @phys_id: physical identifier of a core/thread
249 * CPU logical to physical index mapping is architecture specific.
250 * However this __weak function provides a default match of physical
251 * id to logical cpu index. phys_id provided here is usually values read
252 * from the device tree which must match the hardware internal registers.
254 * Returns true if the physical identifier and the logical cpu index
255 * correspond to the same core/thread, false otherwise.
257 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
259 return (u32)phys_id == cpu;
263 * Checks if the given "prop_name" property holds the physical id of the
264 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
265 * NULL, local thread number within the core is returned in it.
267 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
268 const char *prop_name, int cpu, unsigned int *thread)
271 int ac, prop_len, tid;
274 ac = of_n_addr_cells(cpun);
275 cell = of_get_property(cpun, prop_name, &prop_len);
278 prop_len /= sizeof(*cell) * ac;
279 for (tid = 0; tid < prop_len; tid++) {
280 hwid = of_read_number(cell, ac);
281 if (arch_match_cpu_phys_id(cpu, hwid)) {
292 * arch_find_n_match_cpu_physical_id - See if the given device node is
293 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
294 * else false. If 'thread' is non-NULL, the local thread number within the
295 * core is returned in it.
297 bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
298 int cpu, unsigned int *thread)
300 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
301 * for thread ids on PowerPC. If it doesn't exist fallback to
302 * standard "reg" property.
304 if (IS_ENABLED(CONFIG_PPC) &&
305 __of_find_n_match_cpu_property(cpun,
306 "ibm,ppc-interrupt-server#s",
310 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
317 * of_get_cpu_node - Get device node associated with the given logical CPU
319 * @cpu: CPU number(logical index) for which device node is required
320 * @thread: if not NULL, local thread number within the physical core is
323 * The main purpose of this function is to retrieve the device node for the
324 * given logical CPU index. It should be used to initialize the of_node in
325 * cpu device. Once of_node in cpu device is populated, all the further
326 * references can use that instead.
328 * CPU logical to physical index mapping is architecture specific and is built
329 * before booting secondary cores. This function uses arch_match_cpu_phys_id
330 * which can be overridden by architecture specific implementation.
332 * Returns a node pointer for the logical cpu if found, else NULL.
334 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
336 struct device_node *cpun;
338 for_each_node_by_type(cpun, "cpu") {
339 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
344 EXPORT_SYMBOL(of_get_cpu_node);
347 * __of_device_is_compatible() - Check if the node matches given constraints
348 * @device: pointer to node
349 * @compat: required compatible string, NULL or "" for any match
350 * @type: required device_type value, NULL or "" for any match
351 * @name: required node name, NULL or "" for any match
353 * Checks if the given @compat, @type and @name strings match the
354 * properties of the given @device. A constraints can be skipped by
355 * passing NULL or an empty string as the constraint.
357 * Returns 0 for no match, and a positive integer on match. The return
358 * value is a relative score with larger values indicating better
359 * matches. The score is weighted for the most specific compatible value
360 * to get the highest score. Matching type is next, followed by matching
361 * name. Practically speaking, this results in the following priority
364 * 1. specific compatible && type && name
365 * 2. specific compatible && type
366 * 3. specific compatible && name
367 * 4. specific compatible
368 * 5. general compatible && type && name
369 * 6. general compatible && type
370 * 7. general compatible && name
371 * 8. general compatible
376 static int __of_device_is_compatible(const struct device_node *device,
377 const char *compat, const char *type, const char *name)
379 struct property *prop;
381 int index = 0, score = 0;
383 /* Compatible match has highest priority */
384 if (compat && compat[0]) {
385 prop = __of_find_property(device, "compatible", NULL);
386 for (cp = of_prop_next_string(prop, NULL); cp;
387 cp = of_prop_next_string(prop, cp), index++) {
388 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
389 score = INT_MAX/2 - (index << 2);
397 /* Matching type is better than matching name */
398 if (type && type[0]) {
399 if (!device->type || of_node_cmp(type, device->type))
404 /* Matching name is a bit better than not */
405 if (name && name[0]) {
406 if (!device->name || of_node_cmp(name, device->name))
414 /** Checks if the given "compat" string matches one of the strings in
415 * the device's "compatible" property
417 int of_device_is_compatible(const struct device_node *device,
423 raw_spin_lock_irqsave(&devtree_lock, flags);
424 res = __of_device_is_compatible(device, compat, NULL, NULL);
425 raw_spin_unlock_irqrestore(&devtree_lock, flags);
428 EXPORT_SYMBOL(of_device_is_compatible);
431 * of_machine_is_compatible - Test root of device tree for a given compatible value
432 * @compat: compatible string to look for in root node's compatible property.
434 * Returns true if the root node has the given value in its
435 * compatible property.
437 int of_machine_is_compatible(const char *compat)
439 struct device_node *root;
442 root = of_find_node_by_path("/");
444 rc = of_device_is_compatible(root, compat);
449 EXPORT_SYMBOL(of_machine_is_compatible);
452 * __of_device_is_available - check if a device is available for use
454 * @device: Node to check for availability, with locks already held
456 * Returns 1 if the status property is absent or set to "okay" or "ok",
459 static int __of_device_is_available(const struct device_node *device)
467 status = __of_get_property(device, "status", &statlen);
472 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
480 * of_device_is_available - check if a device is available for use
482 * @device: Node to check for availability
484 * Returns 1 if the status property is absent or set to "okay" or "ok",
487 int of_device_is_available(const struct device_node *device)
492 raw_spin_lock_irqsave(&devtree_lock, flags);
493 res = __of_device_is_available(device);
494 raw_spin_unlock_irqrestore(&devtree_lock, flags);
498 EXPORT_SYMBOL(of_device_is_available);
501 * of_get_parent - Get a node's parent if any
502 * @node: Node to get parent
504 * Returns a node pointer with refcount incremented, use
505 * of_node_put() on it when done.
507 struct device_node *of_get_parent(const struct device_node *node)
509 struct device_node *np;
515 raw_spin_lock_irqsave(&devtree_lock, flags);
516 np = of_node_get(node->parent);
517 raw_spin_unlock_irqrestore(&devtree_lock, flags);
520 EXPORT_SYMBOL(of_get_parent);
523 * of_get_next_parent - Iterate to a node's parent
524 * @node: Node to get parent of
526 * This is like of_get_parent() except that it drops the
527 * refcount on the passed node, making it suitable for iterating
528 * through a node's parents.
530 * Returns a node pointer with refcount incremented, use
531 * of_node_put() on it when done.
533 struct device_node *of_get_next_parent(struct device_node *node)
535 struct device_node *parent;
541 raw_spin_lock_irqsave(&devtree_lock, flags);
542 parent = of_node_get(node->parent);
544 raw_spin_unlock_irqrestore(&devtree_lock, flags);
547 EXPORT_SYMBOL(of_get_next_parent);
550 * of_get_next_child - Iterate a node childs
552 * @prev: previous child of the parent node, or NULL to get first
554 * Returns a node pointer with refcount incremented, use
555 * of_node_put() on it when done.
557 struct device_node *of_get_next_child(const struct device_node *node,
558 struct device_node *prev)
560 struct device_node *next;
563 raw_spin_lock_irqsave(&devtree_lock, flags);
564 next = prev ? prev->sibling : node->child;
565 for (; next; next = next->sibling)
566 if (of_node_get(next))
569 raw_spin_unlock_irqrestore(&devtree_lock, flags);
572 EXPORT_SYMBOL(of_get_next_child);
575 * of_get_next_available_child - Find the next available child node
577 * @prev: previous child of the parent node, or NULL to get first
579 * This function is like of_get_next_child(), except that it
580 * automatically skips any disabled nodes (i.e. status = "disabled").
582 struct device_node *of_get_next_available_child(const struct device_node *node,
583 struct device_node *prev)
585 struct device_node *next;
588 raw_spin_lock_irqsave(&devtree_lock, flags);
589 next = prev ? prev->sibling : node->child;
590 for (; next; next = next->sibling) {
591 if (!__of_device_is_available(next))
593 if (of_node_get(next))
597 raw_spin_unlock_irqrestore(&devtree_lock, flags);
600 EXPORT_SYMBOL(of_get_next_available_child);
603 * of_get_child_by_name - Find the child node by name for a given parent
605 * @name: child name to look for.
607 * This function looks for child node for given matching name
609 * Returns a node pointer if found, with refcount incremented, use
610 * of_node_put() on it when done.
611 * Returns NULL if node is not found.
613 struct device_node *of_get_child_by_name(const struct device_node *node,
616 struct device_node *child;
618 for_each_child_of_node(node, child)
619 if (child->name && (of_node_cmp(child->name, name) == 0))
623 EXPORT_SYMBOL(of_get_child_by_name);
626 * of_find_node_by_path - Find a node matching a full OF path
627 * @path: The full path to match
629 * Returns a node pointer with refcount incremented, use
630 * of_node_put() on it when done.
632 struct device_node *of_find_node_by_path(const char *path)
634 struct device_node *np = of_allnodes;
637 raw_spin_lock_irqsave(&devtree_lock, flags);
638 for (; np; np = np->allnext) {
639 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
643 raw_spin_unlock_irqrestore(&devtree_lock, flags);
646 EXPORT_SYMBOL(of_find_node_by_path);
649 * of_find_node_by_name - Find a node by its "name" property
650 * @from: The node to start searching from or NULL, the node
651 * you pass will not be searched, only the next one
652 * will; typically, you pass what the previous call
653 * returned. of_node_put() will be called on it
654 * @name: The name string to match against
656 * Returns a node pointer with refcount incremented, use
657 * of_node_put() on it when done.
659 struct device_node *of_find_node_by_name(struct device_node *from,
662 struct device_node *np;
665 raw_spin_lock_irqsave(&devtree_lock, flags);
666 np = from ? from->allnext : of_allnodes;
667 for (; np; np = np->allnext)
668 if (np->name && (of_node_cmp(np->name, name) == 0)
672 raw_spin_unlock_irqrestore(&devtree_lock, flags);
675 EXPORT_SYMBOL(of_find_node_by_name);
678 * of_find_node_by_type - Find a node by its "device_type" property
679 * @from: The node to start searching from, or NULL to start searching
680 * the entire device tree. The node you pass will not be
681 * searched, only the next one will; typically, you pass
682 * what the previous call returned. of_node_put() will be
683 * called on from for you.
684 * @type: The type string to match against
686 * Returns a node pointer with refcount incremented, use
687 * of_node_put() on it when done.
689 struct device_node *of_find_node_by_type(struct device_node *from,
692 struct device_node *np;
695 raw_spin_lock_irqsave(&devtree_lock, flags);
696 np = from ? from->allnext : of_allnodes;
697 for (; np; np = np->allnext)
698 if (np->type && (of_node_cmp(np->type, type) == 0)
702 raw_spin_unlock_irqrestore(&devtree_lock, flags);
705 EXPORT_SYMBOL(of_find_node_by_type);
708 * of_find_compatible_node - Find a node based on type and one of the
709 * tokens in its "compatible" property
710 * @from: The node to start searching from or NULL, the node
711 * you pass will not be searched, only the next one
712 * will; typically, you pass what the previous call
713 * returned. of_node_put() will be called on it
714 * @type: The type string to match "device_type" or NULL to ignore
715 * @compatible: The string to match to one of the tokens in the device
718 * Returns a node pointer with refcount incremented, use
719 * of_node_put() on it when done.
721 struct device_node *of_find_compatible_node(struct device_node *from,
722 const char *type, const char *compatible)
724 struct device_node *np;
727 raw_spin_lock_irqsave(&devtree_lock, flags);
728 np = from ? from->allnext : of_allnodes;
729 for (; np; np = np->allnext) {
730 if (__of_device_is_compatible(np, compatible, type, NULL) &&
735 raw_spin_unlock_irqrestore(&devtree_lock, flags);
738 EXPORT_SYMBOL(of_find_compatible_node);
741 * of_find_node_with_property - Find a node which has a property with
743 * @from: The node to start searching from or NULL, the node
744 * you pass will not be searched, only the next one
745 * will; typically, you pass what the previous call
746 * returned. of_node_put() will be called on it
747 * @prop_name: The name of the property to look for.
749 * Returns a node pointer with refcount incremented, use
750 * of_node_put() on it when done.
752 struct device_node *of_find_node_with_property(struct device_node *from,
753 const char *prop_name)
755 struct device_node *np;
759 raw_spin_lock_irqsave(&devtree_lock, flags);
760 np = from ? from->allnext : of_allnodes;
761 for (; np; np = np->allnext) {
762 for (pp = np->properties; pp; pp = pp->next) {
763 if (of_prop_cmp(pp->name, prop_name) == 0) {
771 raw_spin_unlock_irqrestore(&devtree_lock, flags);
774 EXPORT_SYMBOL(of_find_node_with_property);
777 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
778 const struct device_node *node)
780 const struct of_device_id *best_match = NULL;
781 int score, best_score = 0;
786 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
787 score = __of_device_is_compatible(node, matches->compatible,
788 matches->type, matches->name);
789 if (score > best_score) {
790 best_match = matches;
799 * of_match_node - Tell if an device_node has a matching of_match structure
800 * @matches: array of of device match structures to search in
801 * @node: the of device structure to match against
803 * Low level utility function used by device matching.
805 const struct of_device_id *of_match_node(const struct of_device_id *matches,
806 const struct device_node *node)
808 const struct of_device_id *match;
811 raw_spin_lock_irqsave(&devtree_lock, flags);
812 match = __of_match_node(matches, node);
813 raw_spin_unlock_irqrestore(&devtree_lock, flags);
816 EXPORT_SYMBOL(of_match_node);
819 * of_find_matching_node_and_match - Find a node based on an of_device_id
821 * @from: The node to start searching from or NULL, the node
822 * you pass will not be searched, only the next one
823 * will; typically, you pass what the previous call
824 * returned. of_node_put() will be called on it
825 * @matches: array of of device match structures to search in
826 * @match Updated to point at the matches entry which matched
828 * Returns a node pointer with refcount incremented, use
829 * of_node_put() on it when done.
831 struct device_node *of_find_matching_node_and_match(struct device_node *from,
832 const struct of_device_id *matches,
833 const struct of_device_id **match)
835 struct device_node *np;
836 const struct of_device_id *m;
842 raw_spin_lock_irqsave(&devtree_lock, flags);
843 np = from ? from->allnext : of_allnodes;
844 for (; np; np = np->allnext) {
845 m = __of_match_node(matches, np);
846 if (m && of_node_get(np)) {
853 raw_spin_unlock_irqrestore(&devtree_lock, flags);
856 EXPORT_SYMBOL(of_find_matching_node_and_match);
859 * of_modalias_node - Lookup appropriate modalias for a device node
860 * @node: pointer to a device tree node
861 * @modalias: Pointer to buffer that modalias value will be copied into
862 * @len: Length of modalias value
864 * Based on the value of the compatible property, this routine will attempt
865 * to choose an appropriate modalias value for a particular device tree node.
866 * It does this by stripping the manufacturer prefix (as delimited by a ',')
867 * from the first entry in the compatible list property.
869 * This routine returns 0 on success, <0 on failure.
871 int of_modalias_node(struct device_node *node, char *modalias, int len)
873 const char *compatible, *p;
876 compatible = of_get_property(node, "compatible", &cplen);
877 if (!compatible || strlen(compatible) > cplen)
879 p = strchr(compatible, ',');
880 strlcpy(modalias, p ? p + 1 : compatible, len);
883 EXPORT_SYMBOL_GPL(of_modalias_node);
886 * of_find_node_by_phandle - Find a node given a phandle
887 * @handle: phandle of the node to find
889 * Returns a node pointer with refcount incremented, use
890 * of_node_put() on it when done.
892 struct device_node *of_find_node_by_phandle(phandle handle)
894 struct device_node *np;
897 raw_spin_lock_irqsave(&devtree_lock, flags);
898 for (np = of_allnodes; np; np = np->allnext)
899 if (np->phandle == handle)
902 raw_spin_unlock_irqrestore(&devtree_lock, flags);
905 EXPORT_SYMBOL(of_find_node_by_phandle);
908 * of_property_count_elems_of_size - Count the number of elements in a property
910 * @np: device node from which the property value is to be read.
911 * @propname: name of the property to be searched.
912 * @elem_size: size of the individual element
914 * Search for a property in a device node and count the number of elements of
915 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
916 * property does not exist or its length does not match a multiple of elem_size
917 * and -ENODATA if the property does not have a value.
919 int of_property_count_elems_of_size(const struct device_node *np,
920 const char *propname, int elem_size)
922 struct property *prop = of_find_property(np, propname, NULL);
929 if (prop->length % elem_size != 0) {
930 pr_err("size of %s in node %s is not a multiple of %d\n",
931 propname, np->full_name, elem_size);
935 return prop->length / elem_size;
937 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
940 * of_find_property_value_of_size
942 * @np: device node from which the property value is to be read.
943 * @propname: name of the property to be searched.
944 * @len: requested length of property value
946 * Search for a property in a device node and valid the requested size.
947 * Returns the property value on success, -EINVAL if the property does not
948 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
949 * property data isn't large enough.
952 static void *of_find_property_value_of_size(const struct device_node *np,
953 const char *propname, u32 len)
955 struct property *prop = of_find_property(np, propname, NULL);
958 return ERR_PTR(-EINVAL);
960 return ERR_PTR(-ENODATA);
961 if (len > prop->length)
962 return ERR_PTR(-EOVERFLOW);
968 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
970 * @np: device node from which the property value is to be read.
971 * @propname: name of the property to be searched.
972 * @index: index of the u32 in the list of values
973 * @out_value: pointer to return value, modified only if no error.
975 * Search for a property in a device node and read nth 32-bit value from
976 * it. Returns 0 on success, -EINVAL if the property does not exist,
977 * -ENODATA if property does not have a value, and -EOVERFLOW if the
978 * property data isn't large enough.
980 * The out_value is modified only if a valid u32 value can be decoded.
982 int of_property_read_u32_index(const struct device_node *np,
983 const char *propname,
984 u32 index, u32 *out_value)
986 const u32 *val = of_find_property_value_of_size(np, propname,
987 ((index + 1) * sizeof(*out_value)));
992 *out_value = be32_to_cpup(((__be32 *)val) + index);
995 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
998 * of_property_read_u8_array - Find and read an array of u8 from a property.
1000 * @np: device node from which the property value is to be read.
1001 * @propname: name of the property to be searched.
1002 * @out_values: pointer to return value, modified only if return value is 0.
1003 * @sz: number of array elements to read
1005 * Search for a property in a device node and read 8-bit value(s) from
1006 * it. Returns 0 on success, -EINVAL if the property does not exist,
1007 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1008 * property data isn't large enough.
1010 * dts entry of array should be like:
1011 * property = /bits/ 8 <0x50 0x60 0x70>;
1013 * The out_values is modified only if a valid u8 value can be decoded.
1015 int of_property_read_u8_array(const struct device_node *np,
1016 const char *propname, u8 *out_values, size_t sz)
1018 const u8 *val = of_find_property_value_of_size(np, propname,
1019 (sz * sizeof(*out_values)));
1022 return PTR_ERR(val);
1025 *out_values++ = *val++;
1028 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1031 * of_property_read_u16_array - Find and read an array of u16 from a property.
1033 * @np: device node from which the property value is to be read.
1034 * @propname: name of the property to be searched.
1035 * @out_values: pointer to return value, modified only if return value is 0.
1036 * @sz: number of array elements to read
1038 * Search for a property in a device node and read 16-bit value(s) from
1039 * it. Returns 0 on success, -EINVAL if the property does not exist,
1040 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1041 * property data isn't large enough.
1043 * dts entry of array should be like:
1044 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1046 * The out_values is modified only if a valid u16 value can be decoded.
1048 int of_property_read_u16_array(const struct device_node *np,
1049 const char *propname, u16 *out_values, size_t sz)
1051 const __be16 *val = of_find_property_value_of_size(np, propname,
1052 (sz * sizeof(*out_values)));
1055 return PTR_ERR(val);
1058 *out_values++ = be16_to_cpup(val++);
1061 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1064 * of_property_read_u32_array - Find and read an array of 32 bit integers
1067 * @np: device node from which the property value is to be read.
1068 * @propname: name of the property to be searched.
1069 * @out_values: pointer to return value, modified only if return value is 0.
1070 * @sz: number of array elements to read
1072 * Search for a property in a device node and read 32-bit value(s) from
1073 * it. Returns 0 on success, -EINVAL if the property does not exist,
1074 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1075 * property data isn't large enough.
1077 * The out_values is modified only if a valid u32 value can be decoded.
1079 int of_property_read_u32_array(const struct device_node *np,
1080 const char *propname, u32 *out_values,
1083 const __be32 *val = of_find_property_value_of_size(np, propname,
1084 (sz * sizeof(*out_values)));
1087 return PTR_ERR(val);
1090 *out_values++ = be32_to_cpup(val++);
1093 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1096 * of_property_read_u64 - Find and read a 64 bit integer from a property
1097 * @np: device node from which the property value is to be read.
1098 * @propname: name of the property to be searched.
1099 * @out_value: pointer to return value, modified only if return value is 0.
1101 * Search for a property in a device node and read a 64-bit value from
1102 * it. Returns 0 on success, -EINVAL if the property does not exist,
1103 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1104 * property data isn't large enough.
1106 * The out_value is modified only if a valid u64 value can be decoded.
1108 int of_property_read_u64(const struct device_node *np, const char *propname,
1111 const __be32 *val = of_find_property_value_of_size(np, propname,
1112 sizeof(*out_value));
1115 return PTR_ERR(val);
1117 *out_value = of_read_number(val, 2);
1120 EXPORT_SYMBOL_GPL(of_property_read_u64);
1123 * of_property_read_string - Find and read a string from a property
1124 * @np: device node from which the property value is to be read.
1125 * @propname: name of the property to be searched.
1126 * @out_string: pointer to null terminated return string, modified only if
1127 * return value is 0.
1129 * Search for a property in a device tree node and retrieve a null
1130 * terminated string value (pointer to data, not a copy). Returns 0 on
1131 * success, -EINVAL if the property does not exist, -ENODATA if property
1132 * does not have a value, and -EILSEQ if the string is not null-terminated
1133 * within the length of the property data.
1135 * The out_string pointer is modified only if a valid string can be decoded.
1137 int of_property_read_string(struct device_node *np, const char *propname,
1138 const char **out_string)
1140 struct property *prop = of_find_property(np, propname, NULL);
1145 if (strnlen(prop->value, prop->length) >= prop->length)
1147 *out_string = prop->value;
1150 EXPORT_SYMBOL_GPL(of_property_read_string);
1153 * of_property_read_string_index - Find and read a string from a multiple
1155 * @np: device node from which the property value is to be read.
1156 * @propname: name of the property to be searched.
1157 * @index: index of the string in the list of strings
1158 * @out_string: pointer to null terminated return string, modified only if
1159 * return value is 0.
1161 * Search for a property in a device tree node and retrieve a null
1162 * terminated string value (pointer to data, not a copy) in the list of strings
1163 * contained in that property.
1164 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1165 * property does not have a value, and -EILSEQ if the string is not
1166 * null-terminated within the length of the property data.
1168 * The out_string pointer is modified only if a valid string can be decoded.
1170 int of_property_read_string_index(struct device_node *np, const char *propname,
1171 int index, const char **output)
1173 struct property *prop = of_find_property(np, propname, NULL);
1175 size_t l = 0, total = 0;
1182 if (strnlen(prop->value, prop->length) >= prop->length)
1187 for (i = 0; total < prop->length; total += l, p += l) {
1196 EXPORT_SYMBOL_GPL(of_property_read_string_index);
1199 * of_property_match_string() - Find string in a list and return index
1200 * @np: pointer to node containing string list property
1201 * @propname: string list property name
1202 * @string: pointer to string to search for in string list
1204 * This function searches a string list property and returns the index
1205 * of a specific string value.
1207 int of_property_match_string(struct device_node *np, const char *propname,
1210 struct property *prop = of_find_property(np, propname, NULL);
1213 const char *p, *end;
1221 end = p + prop->length;
1223 for (i = 0; p < end; i++, p += l) {
1227 pr_debug("comparing %s with %s\n", string, p);
1228 if (strcmp(string, p) == 0)
1229 return i; /* Found it; return index */
1233 EXPORT_SYMBOL_GPL(of_property_match_string);
1236 * of_property_count_strings - Find and return the number of strings from a
1237 * multiple strings property.
1238 * @np: device node from which the property value is to be read.
1239 * @propname: name of the property to be searched.
1241 * Search for a property in a device tree node and retrieve the number of null
1242 * terminated string contain in it. Returns the number of strings on
1243 * success, -EINVAL if the property does not exist, -ENODATA if property
1244 * does not have a value, and -EILSEQ if the string is not null-terminated
1245 * within the length of the property data.
1247 int of_property_count_strings(struct device_node *np, const char *propname)
1249 struct property *prop = of_find_property(np, propname, NULL);
1251 size_t l = 0, total = 0;
1258 if (strnlen(prop->value, prop->length) >= prop->length)
1263 for (i = 0; total < prop->length; total += l, p += l, i++)
1268 EXPORT_SYMBOL_GPL(of_property_count_strings);
1270 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1273 printk("%s %s", msg, of_node_full_name(args->np));
1274 for (i = 0; i < args->args_count; i++)
1275 printk(i ? ",%08x" : ":%08x", args->args[i]);
1279 static int __of_parse_phandle_with_args(const struct device_node *np,
1280 const char *list_name,
1281 const char *cells_name,
1282 int cell_count, int index,
1283 struct of_phandle_args *out_args)
1285 const __be32 *list, *list_end;
1286 int rc = 0, size, cur_index = 0;
1288 struct device_node *node = NULL;
1291 /* Retrieve the phandle list property */
1292 list = of_get_property(np, list_name, &size);
1295 list_end = list + size / sizeof(*list);
1297 /* Loop over the phandles until all the requested entry is found */
1298 while (list < list_end) {
1303 * If phandle is 0, then it is an empty entry with no
1304 * arguments. Skip forward to the next entry.
1306 phandle = be32_to_cpup(list++);
1309 * Find the provider node and parse the #*-cells
1310 * property to determine the argument length.
1312 * This is not needed if the cell count is hard-coded
1313 * (i.e. cells_name not set, but cell_count is set),
1314 * except when we're going to return the found node
1317 if (cells_name || cur_index == index) {
1318 node = of_find_node_by_phandle(phandle);
1320 pr_err("%s: could not find phandle\n",
1327 if (of_property_read_u32(node, cells_name,
1329 pr_err("%s: could not get %s for %s\n",
1330 np->full_name, cells_name,
1339 * Make sure that the arguments actually fit in the
1340 * remaining property data length
1342 if (list + count > list_end) {
1343 pr_err("%s: arguments longer than property\n",
1350 * All of the error cases above bail out of the loop, so at
1351 * this point, the parsing is successful. If the requested
1352 * index matches, then fill the out_args structure and return,
1353 * or return -ENOENT for an empty entry.
1356 if (cur_index == index) {
1362 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1363 count = MAX_PHANDLE_ARGS;
1364 out_args->np = node;
1365 out_args->args_count = count;
1366 for (i = 0; i < count; i++)
1367 out_args->args[i] = be32_to_cpup(list++);
1372 /* Found it! return success */
1383 * Unlock node before returning result; will be one of:
1384 * -ENOENT : index is for empty phandle
1385 * -EINVAL : parsing error on data
1386 * [1..n] : Number of phandle (count mode; when index = -1)
1388 rc = index < 0 ? cur_index : -ENOENT;
1396 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1397 * @np: Pointer to device node holding phandle property
1398 * @phandle_name: Name of property holding a phandle value
1399 * @index: For properties holding a table of phandles, this is the index into
1402 * Returns the device_node pointer with refcount incremented. Use
1403 * of_node_put() on it when done.
1405 struct device_node *of_parse_phandle(const struct device_node *np,
1406 const char *phandle_name, int index)
1408 struct of_phandle_args args;
1413 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1419 EXPORT_SYMBOL(of_parse_phandle);
1422 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1423 * @np: pointer to a device tree node containing a list
1424 * @list_name: property name that contains a list
1425 * @cells_name: property name that specifies phandles' arguments count
1426 * @index: index of a phandle to parse out
1427 * @out_args: optional pointer to output arguments structure (will be filled)
1429 * This function is useful to parse lists of phandles and their arguments.
1430 * Returns 0 on success and fills out_args, on error returns appropriate
1433 * Caller is responsible to call of_node_put() on the returned out_args->node
1439 * #list-cells = <2>;
1443 * #list-cells = <1>;
1447 * list = <&phandle1 1 2 &phandle2 3>;
1450 * To get a device_node of the `node2' node you may call this:
1451 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1453 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1454 const char *cells_name, int index,
1455 struct of_phandle_args *out_args)
1459 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1462 EXPORT_SYMBOL(of_parse_phandle_with_args);
1465 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1466 * @np: pointer to a device tree node containing a list
1467 * @list_name: property name that contains a list
1468 * @cell_count: number of argument cells following the phandle
1469 * @index: index of a phandle to parse out
1470 * @out_args: optional pointer to output arguments structure (will be filled)
1472 * This function is useful to parse lists of phandles and their arguments.
1473 * Returns 0 on success and fills out_args, on error returns appropriate
1476 * Caller is responsible to call of_node_put() on the returned out_args->node
1488 * list = <&phandle1 0 2 &phandle2 2 3>;
1491 * To get a device_node of the `node2' node you may call this:
1492 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1494 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1495 const char *list_name, int cell_count,
1496 int index, struct of_phandle_args *out_args)
1500 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1503 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1506 * of_count_phandle_with_args() - Find the number of phandles references in a property
1507 * @np: pointer to a device tree node containing a list
1508 * @list_name: property name that contains a list
1509 * @cells_name: property name that specifies phandles' arguments count
1511 * Returns the number of phandle + argument tuples within a property. It
1512 * is a typical pattern to encode a list of phandle and variable
1513 * arguments into a single property. The number of arguments is encoded
1514 * by a property in the phandle-target node. For example, a gpios
1515 * property would contain a list of GPIO specifies consisting of a
1516 * phandle and 1 or more arguments. The number of arguments are
1517 * determined by the #gpio-cells property in the node pointed to by the
1520 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1521 const char *cells_name)
1523 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1526 EXPORT_SYMBOL(of_count_phandle_with_args);
1528 #if defined(CONFIG_OF_DYNAMIC)
1529 static int of_property_notify(int action, struct device_node *np,
1530 struct property *prop)
1532 struct of_prop_reconfig pr;
1536 return of_reconfig_notify(action, &pr);
1539 static int of_property_notify(int action, struct device_node *np,
1540 struct property *prop)
1547 * of_add_property - Add a property to a node
1549 int of_add_property(struct device_node *np, struct property *prop)
1551 struct property **next;
1552 unsigned long flags;
1555 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1560 raw_spin_lock_irqsave(&devtree_lock, flags);
1561 next = &np->properties;
1563 if (strcmp(prop->name, (*next)->name) == 0) {
1564 /* duplicate ! don't insert it */
1565 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1568 next = &(*next)->next;
1571 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1573 #ifdef CONFIG_PROC_DEVICETREE
1574 /* try to add to proc as well if it was initialized */
1576 proc_device_tree_add_prop(np->pde, prop);
1577 #endif /* CONFIG_PROC_DEVICETREE */
1583 * of_remove_property - Remove a property from a node.
1585 * Note that we don't actually remove it, since we have given out
1586 * who-knows-how-many pointers to the data using get-property.
1587 * Instead we just move the property to the "dead properties"
1588 * list, so it won't be found any more.
1590 int of_remove_property(struct device_node *np, struct property *prop)
1592 struct property **next;
1593 unsigned long flags;
1597 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1601 raw_spin_lock_irqsave(&devtree_lock, flags);
1602 next = &np->properties;
1604 if (*next == prop) {
1605 /* found the node */
1607 prop->next = np->deadprops;
1608 np->deadprops = prop;
1612 next = &(*next)->next;
1614 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1619 #ifdef CONFIG_PROC_DEVICETREE
1620 /* try to remove the proc node as well */
1622 proc_device_tree_remove_prop(np->pde, prop);
1623 #endif /* CONFIG_PROC_DEVICETREE */
1629 * of_update_property - Update a property in a node, if the property does
1630 * not exist, add it.
1632 * Note that we don't actually remove it, since we have given out
1633 * who-knows-how-many pointers to the data using get-property.
1634 * Instead we just move the property to the "dead properties" list,
1635 * and add the new property to the property list
1637 int of_update_property(struct device_node *np, struct property *newprop)
1639 struct property **next, *oldprop;
1640 unsigned long flags;
1643 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1650 oldprop = of_find_property(np, newprop->name, NULL);
1652 return of_add_property(np, newprop);
1654 raw_spin_lock_irqsave(&devtree_lock, flags);
1655 next = &np->properties;
1657 if (*next == oldprop) {
1658 /* found the node */
1659 newprop->next = oldprop->next;
1661 oldprop->next = np->deadprops;
1662 np->deadprops = oldprop;
1666 next = &(*next)->next;
1668 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1673 #ifdef CONFIG_PROC_DEVICETREE
1674 /* try to add to proc as well if it was initialized */
1676 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1677 #endif /* CONFIG_PROC_DEVICETREE */
1682 #if defined(CONFIG_OF_DYNAMIC)
1684 * Support for dynamic device trees.
1686 * On some platforms, the device tree can be manipulated at runtime.
1687 * The routines in this section support adding, removing and changing
1688 * device tree nodes.
1691 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1693 int of_reconfig_notifier_register(struct notifier_block *nb)
1695 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1697 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1699 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1701 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1703 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1705 int of_reconfig_notify(unsigned long action, void *p)
1709 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1710 return notifier_to_errno(rc);
1713 #ifdef CONFIG_PROC_DEVICETREE
1714 static void of_add_proc_dt_entry(struct device_node *dn)
1716 struct proc_dir_entry *ent;
1718 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1720 proc_device_tree_add_node(dn, ent);
1723 static void of_add_proc_dt_entry(struct device_node *dn)
1730 * of_attach_node - Plug a device node into the tree and global list.
1732 int of_attach_node(struct device_node *np)
1734 unsigned long flags;
1737 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1741 raw_spin_lock_irqsave(&devtree_lock, flags);
1742 np->sibling = np->parent->child;
1743 np->allnext = of_allnodes;
1744 np->parent->child = np;
1746 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1748 of_add_proc_dt_entry(np);
1752 #ifdef CONFIG_PROC_DEVICETREE
1753 static void of_remove_proc_dt_entry(struct device_node *dn)
1755 proc_remove(dn->pde);
1758 static void of_remove_proc_dt_entry(struct device_node *dn)
1765 * of_detach_node - "Unplug" a node from the device tree.
1767 * The caller must hold a reference to the node. The memory associated with
1768 * the node is not freed until its refcount goes to zero.
1770 int of_detach_node(struct device_node *np)
1772 struct device_node *parent;
1773 unsigned long flags;
1776 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1780 raw_spin_lock_irqsave(&devtree_lock, flags);
1782 if (of_node_check_flag(np, OF_DETACHED)) {
1783 /* someone already detached it */
1784 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1788 parent = np->parent;
1790 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1794 if (of_allnodes == np)
1795 of_allnodes = np->allnext;
1797 struct device_node *prev;
1798 for (prev = of_allnodes;
1799 prev->allnext != np;
1800 prev = prev->allnext)
1802 prev->allnext = np->allnext;
1805 if (parent->child == np)
1806 parent->child = np->sibling;
1808 struct device_node *prevsib;
1809 for (prevsib = np->parent->child;
1810 prevsib->sibling != np;
1811 prevsib = prevsib->sibling)
1813 prevsib->sibling = np->sibling;
1816 of_node_set_flag(np, OF_DETACHED);
1817 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1819 of_remove_proc_dt_entry(np);
1822 #endif /* defined(CONFIG_OF_DYNAMIC) */
1824 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1825 int id, const char *stem, int stem_len)
1829 strncpy(ap->stem, stem, stem_len);
1830 ap->stem[stem_len] = 0;
1831 list_add_tail(&ap->link, &aliases_lookup);
1832 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1833 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1837 * of_alias_scan - Scan all properties of 'aliases' node
1839 * The function scans all the properties of 'aliases' node and populate
1840 * the the global lookup table with the properties. It returns the
1841 * number of alias_prop found, or error code in error case.
1843 * @dt_alloc: An allocator that provides a virtual address to memory
1844 * for the resulting tree
1846 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1848 struct property *pp;
1850 of_chosen = of_find_node_by_path("/chosen");
1851 if (of_chosen == NULL)
1852 of_chosen = of_find_node_by_path("/chosen@0");
1857 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1859 of_stdout = of_find_node_by_path(name);
1862 of_aliases = of_find_node_by_path("/aliases");
1866 for_each_property_of_node(of_aliases, pp) {
1867 const char *start = pp->name;
1868 const char *end = start + strlen(start);
1869 struct device_node *np;
1870 struct alias_prop *ap;
1873 /* Skip those we do not want to proceed */
1874 if (!strcmp(pp->name, "name") ||
1875 !strcmp(pp->name, "phandle") ||
1876 !strcmp(pp->name, "linux,phandle"))
1879 np = of_find_node_by_path(pp->value);
1883 /* walk the alias backwards to extract the id and work out
1884 * the 'stem' string */
1885 while (isdigit(*(end-1)) && end > start)
1889 if (kstrtoint(end, 10, &id) < 0)
1892 /* Allocate an alias_prop with enough space for the stem */
1893 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1896 memset(ap, 0, sizeof(*ap) + len + 1);
1898 of_alias_add(ap, np, id, start, len);
1903 * of_alias_get_id - Get alias id for the given device_node
1904 * @np: Pointer to the given device_node
1905 * @stem: Alias stem of the given device_node
1907 * The function travels the lookup table to get alias id for the given
1908 * device_node and alias stem. It returns the alias id if find it.
1910 int of_alias_get_id(struct device_node *np, const char *stem)
1912 struct alias_prop *app;
1915 mutex_lock(&of_aliases_mutex);
1916 list_for_each_entry(app, &aliases_lookup, link) {
1917 if (strcmp(app->stem, stem) != 0)
1920 if (np == app->np) {
1925 mutex_unlock(&of_aliases_mutex);
1929 EXPORT_SYMBOL_GPL(of_alias_get_id);
1931 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1934 const void *curv = cur;
1944 curv += sizeof(*cur);
1945 if (curv >= prop->value + prop->length)
1949 *pu = be32_to_cpup(curv);
1952 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1954 const char *of_prop_next_string(struct property *prop, const char *cur)
1956 const void *curv = cur;
1964 curv += strlen(cur) + 1;
1965 if (curv >= prop->value + prop->length)
1970 EXPORT_SYMBOL_GPL(of_prop_next_string);
1973 * of_device_is_stdout_path - check if a device node matches the
1974 * linux,stdout-path property
1976 * Check if this device node matches the linux,stdout-path property
1977 * in the chosen node. return true if yes, false otherwise.
1979 int of_device_is_stdout_path(struct device_node *dn)
1984 return of_stdout == dn;
1986 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
1989 * of_find_next_cache_node - Find a node's subsidiary cache
1990 * @np: node of type "cpu" or "cache"
1992 * Returns a node pointer with refcount incremented, use
1993 * of_node_put() on it when done. Caller should hold a reference
1996 struct device_node *of_find_next_cache_node(const struct device_node *np)
1998 struct device_node *child;
1999 const phandle *handle;
2001 handle = of_get_property(np, "l2-cache", NULL);
2003 handle = of_get_property(np, "next-level-cache", NULL);
2006 return of_find_node_by_phandle(be32_to_cpup(handle));
2008 /* OF on pmac has nodes instead of properties named "l2-cache"
2009 * beneath CPU nodes.
2011 if (!strcmp(np->type, "cpu"))
2012 for_each_child_of_node(np, child)
2013 if (!strcmp(child->type, "cache"))
2020 * of_graph_parse_endpoint() - parse common endpoint node properties
2021 * @node: pointer to endpoint device_node
2022 * @endpoint: pointer to the OF endpoint data structure
2024 * The caller should hold a reference to @node.
2026 int of_graph_parse_endpoint(const struct device_node *node,
2027 struct of_endpoint *endpoint)
2029 struct device_node *port_node = of_get_parent(node);
2031 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2032 __func__, node->full_name);
2034 memset(endpoint, 0, sizeof(*endpoint));
2036 endpoint->local_node = node;
2038 * It doesn't matter whether the two calls below succeed.
2039 * If they don't then the default value 0 is used.
2041 of_property_read_u32(port_node, "reg", &endpoint->port);
2042 of_property_read_u32(node, "reg", &endpoint->id);
2044 of_node_put(port_node);
2048 EXPORT_SYMBOL(of_graph_parse_endpoint);
2051 * of_graph_get_next_endpoint() - get next endpoint node
2052 * @parent: pointer to the parent device node
2053 * @prev: previous endpoint node, or NULL to get first
2055 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2056 * of the passed @prev node is not decremented, the caller have to use
2057 * of_node_put() on it when done.
2059 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2060 struct device_node *prev)
2062 struct device_node *endpoint;
2063 struct device_node *port = NULL;
2069 struct device_node *node;
2071 * It's the first call, we have to find a port subnode
2072 * within this node or within an optional 'ports' node.
2074 node = of_get_child_by_name(parent, "ports");
2078 port = of_get_child_by_name(parent, "port");
2081 /* Found a port, get an endpoint. */
2082 endpoint = of_get_next_child(port, NULL);
2089 pr_err("%s(): no endpoint nodes specified for %s\n",
2090 __func__, parent->full_name);
2096 port = of_get_parent(prev);
2097 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2098 __func__, prev->full_name))
2101 /* Avoid dropping prev node refcount to 0. */
2103 endpoint = of_get_next_child(port, prev);
2109 /* No more endpoints under this port, try the next one. */
2111 port = of_get_next_child(parent, port);
2114 } while (of_node_cmp(port->name, "port"));
2116 /* Pick up the first endpoint in this port. */
2117 endpoint = of_get_next_child(port, NULL);
2122 EXPORT_SYMBOL(of_graph_get_next_endpoint);
2125 * of_graph_get_remote_port_parent() - get remote port's parent node
2126 * @node: pointer to a local endpoint device_node
2128 * Return: Remote device node associated with remote endpoint node linked
2129 * to @node. Use of_node_put() on it when done.
2131 struct device_node *of_graph_get_remote_port_parent(
2132 const struct device_node *node)
2134 struct device_node *np;
2137 /* Get remote endpoint node. */
2138 np = of_parse_phandle(node, "remote-endpoint", 0);
2140 /* Walk 3 levels up only if there is 'ports' node. */
2141 for (depth = 3; depth && np; depth--) {
2142 np = of_get_next_parent(np);
2143 if (depth == 2 && of_node_cmp(np->name, "ports"))
2148 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2151 * of_graph_get_remote_port() - get remote port node
2152 * @node: pointer to a local endpoint device_node
2154 * Return: Remote port node associated with remote endpoint node linked
2155 * to @node. Use of_node_put() on it when done.
2157 struct device_node *of_graph_get_remote_port(const struct device_node *node)
2159 struct device_node *np;
2161 /* Get remote endpoint node. */
2162 np = of_parse_phandle(node, "remote-endpoint", 0);
2165 return of_get_next_parent(np);
2167 EXPORT_SYMBOL(of_graph_get_remote_port);