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_match_string() - Find string in a list and return index
1154 * @np: pointer to node containing string list property
1155 * @propname: string list property name
1156 * @string: pointer to string to search for in string list
1158 * This function searches a string list property and returns the index
1159 * of a specific string value.
1161 int of_property_match_string(struct device_node *np, const char *propname,
1164 struct property *prop = of_find_property(np, propname, NULL);
1167 const char *p, *end;
1175 end = p + prop->length;
1177 for (i = 0; p < end; i++, p += l) {
1178 l = strnlen(p, end - p) + 1;
1181 pr_debug("comparing %s with %s\n", string, p);
1182 if (strcmp(string, p) == 0)
1183 return i; /* Found it; return index */
1187 EXPORT_SYMBOL_GPL(of_property_match_string);
1190 * of_property_read_string_util() - Utility helper for parsing string properties
1191 * @np: device node from which the property value is to be read.
1192 * @propname: name of the property to be searched.
1193 * @out_strs: output array of string pointers.
1194 * @sz: number of array elements to read.
1195 * @skip: Number of strings to skip over at beginning of list.
1197 * Don't call this function directly. It is a utility helper for the
1198 * of_property_read_string*() family of functions.
1200 int of_property_read_string_helper(struct device_node *np, const char *propname,
1201 const char **out_strs, size_t sz, int skip)
1203 struct property *prop = of_find_property(np, propname, NULL);
1205 const char *p, *end;
1212 end = p + prop->length;
1214 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1215 l = strnlen(p, end - p) + 1;
1218 if (out_strs && i >= skip)
1222 return i <= 0 ? -ENODATA : i;
1224 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
1226 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1229 printk("%s %s", msg, of_node_full_name(args->np));
1230 for (i = 0; i < args->args_count; i++)
1231 printk(i ? ",%08x" : ":%08x", args->args[i]);
1235 static int __of_parse_phandle_with_args(const struct device_node *np,
1236 const char *list_name,
1237 const char *cells_name,
1238 int cell_count, int index,
1239 struct of_phandle_args *out_args)
1241 const __be32 *list, *list_end;
1242 int rc = 0, size, cur_index = 0;
1244 struct device_node *node = NULL;
1247 /* Retrieve the phandle list property */
1248 list = of_get_property(np, list_name, &size);
1251 list_end = list + size / sizeof(*list);
1253 /* Loop over the phandles until all the requested entry is found */
1254 while (list < list_end) {
1259 * If phandle is 0, then it is an empty entry with no
1260 * arguments. Skip forward to the next entry.
1262 phandle = be32_to_cpup(list++);
1265 * Find the provider node and parse the #*-cells
1266 * property to determine the argument length.
1268 * This is not needed if the cell count is hard-coded
1269 * (i.e. cells_name not set, but cell_count is set),
1270 * except when we're going to return the found node
1273 if (cells_name || cur_index == index) {
1274 node = of_find_node_by_phandle(phandle);
1276 pr_err("%s: could not find phandle\n",
1283 if (of_property_read_u32(node, cells_name,
1285 pr_err("%s: could not get %s for %s\n",
1286 np->full_name, cells_name,
1295 * Make sure that the arguments actually fit in the
1296 * remaining property data length
1298 if (list + count > list_end) {
1299 pr_err("%s: arguments longer than property\n",
1306 * All of the error cases above bail out of the loop, so at
1307 * this point, the parsing is successful. If the requested
1308 * index matches, then fill the out_args structure and return,
1309 * or return -ENOENT for an empty entry.
1312 if (cur_index == index) {
1318 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1319 count = MAX_PHANDLE_ARGS;
1320 out_args->np = node;
1321 out_args->args_count = count;
1322 for (i = 0; i < count; i++)
1323 out_args->args[i] = be32_to_cpup(list++);
1328 /* Found it! return success */
1339 * Unlock node before returning result; will be one of:
1340 * -ENOENT : index is for empty phandle
1341 * -EINVAL : parsing error on data
1342 * [1..n] : Number of phandle (count mode; when index = -1)
1344 rc = index < 0 ? cur_index : -ENOENT;
1352 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1353 * @np: Pointer to device node holding phandle property
1354 * @phandle_name: Name of property holding a phandle value
1355 * @index: For properties holding a table of phandles, this is the index into
1358 * Returns the device_node pointer with refcount incremented. Use
1359 * of_node_put() on it when done.
1361 struct device_node *of_parse_phandle(const struct device_node *np,
1362 const char *phandle_name, int index)
1364 struct of_phandle_args args;
1369 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1375 EXPORT_SYMBOL(of_parse_phandle);
1378 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1379 * @np: pointer to a device tree node containing a list
1380 * @list_name: property name that contains a list
1381 * @cells_name: property name that specifies phandles' arguments count
1382 * @index: index of a phandle to parse out
1383 * @out_args: optional pointer to output arguments structure (will be filled)
1385 * This function is useful to parse lists of phandles and their arguments.
1386 * Returns 0 on success and fills out_args, on error returns appropriate
1389 * Caller is responsible to call of_node_put() on the returned out_args->node
1395 * #list-cells = <2>;
1399 * #list-cells = <1>;
1403 * list = <&phandle1 1 2 &phandle2 3>;
1406 * To get a device_node of the `node2' node you may call this:
1407 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1409 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1410 const char *cells_name, int index,
1411 struct of_phandle_args *out_args)
1415 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1418 EXPORT_SYMBOL(of_parse_phandle_with_args);
1421 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1422 * @np: pointer to a device tree node containing a list
1423 * @list_name: property name that contains a list
1424 * @cell_count: number of argument cells following the phandle
1425 * @index: index of a phandle to parse out
1426 * @out_args: optional pointer to output arguments structure (will be filled)
1428 * This function is useful to parse lists of phandles and their arguments.
1429 * Returns 0 on success and fills out_args, on error returns appropriate
1432 * Caller is responsible to call of_node_put() on the returned out_args->node
1444 * list = <&phandle1 0 2 &phandle2 2 3>;
1447 * To get a device_node of the `node2' node you may call this:
1448 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1450 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1451 const char *list_name, int cell_count,
1452 int index, struct of_phandle_args *out_args)
1456 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1459 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1462 * of_count_phandle_with_args() - Find the number of phandles references in a property
1463 * @np: pointer to a device tree node containing a list
1464 * @list_name: property name that contains a list
1465 * @cells_name: property name that specifies phandles' arguments count
1467 * Returns the number of phandle + argument tuples within a property. It
1468 * is a typical pattern to encode a list of phandle and variable
1469 * arguments into a single property. The number of arguments is encoded
1470 * by a property in the phandle-target node. For example, a gpios
1471 * property would contain a list of GPIO specifies consisting of a
1472 * phandle and 1 or more arguments. The number of arguments are
1473 * determined by the #gpio-cells property in the node pointed to by the
1476 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1477 const char *cells_name)
1479 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1482 EXPORT_SYMBOL(of_count_phandle_with_args);
1484 #if defined(CONFIG_OF_DYNAMIC)
1485 static int of_property_notify(int action, struct device_node *np,
1486 struct property *prop)
1488 struct of_prop_reconfig pr;
1492 return of_reconfig_notify(action, &pr);
1495 static int of_property_notify(int action, struct device_node *np,
1496 struct property *prop)
1503 * of_add_property - Add a property to a node
1505 int of_add_property(struct device_node *np, struct property *prop)
1507 struct property **next;
1508 unsigned long flags;
1511 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1516 raw_spin_lock_irqsave(&devtree_lock, flags);
1517 next = &np->properties;
1519 if (strcmp(prop->name, (*next)->name) == 0) {
1520 /* duplicate ! don't insert it */
1521 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1524 next = &(*next)->next;
1527 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1529 #ifdef CONFIG_PROC_DEVICETREE
1530 /* try to add to proc as well if it was initialized */
1532 proc_device_tree_add_prop(np->pde, prop);
1533 #endif /* CONFIG_PROC_DEVICETREE */
1539 * of_remove_property - Remove a property from a node.
1541 * Note that we don't actually remove it, since we have given out
1542 * who-knows-how-many pointers to the data using get-property.
1543 * Instead we just move the property to the "dead properties"
1544 * list, so it won't be found any more.
1546 int of_remove_property(struct device_node *np, struct property *prop)
1548 struct property **next;
1549 unsigned long flags;
1553 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1557 raw_spin_lock_irqsave(&devtree_lock, flags);
1558 next = &np->properties;
1560 if (*next == prop) {
1561 /* found the node */
1563 prop->next = np->deadprops;
1564 np->deadprops = prop;
1568 next = &(*next)->next;
1570 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1575 #ifdef CONFIG_PROC_DEVICETREE
1576 /* try to remove the proc node as well */
1578 proc_device_tree_remove_prop(np->pde, prop);
1579 #endif /* CONFIG_PROC_DEVICETREE */
1585 * of_update_property - Update a property in a node, if the property does
1586 * not exist, add it.
1588 * Note that we don't actually remove it, since we have given out
1589 * who-knows-how-many pointers to the data using get-property.
1590 * Instead we just move the property to the "dead properties" list,
1591 * and add the new property to the property list
1593 int of_update_property(struct device_node *np, struct property *newprop)
1595 struct property **next, *oldprop;
1596 unsigned long flags;
1599 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1606 oldprop = of_find_property(np, newprop->name, NULL);
1608 return of_add_property(np, newprop);
1610 raw_spin_lock_irqsave(&devtree_lock, flags);
1611 next = &np->properties;
1613 if (*next == oldprop) {
1614 /* found the node */
1615 newprop->next = oldprop->next;
1617 oldprop->next = np->deadprops;
1618 np->deadprops = oldprop;
1622 next = &(*next)->next;
1624 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1629 #ifdef CONFIG_PROC_DEVICETREE
1630 /* try to add to proc as well if it was initialized */
1632 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1633 #endif /* CONFIG_PROC_DEVICETREE */
1638 #if defined(CONFIG_OF_DYNAMIC)
1640 * Support for dynamic device trees.
1642 * On some platforms, the device tree can be manipulated at runtime.
1643 * The routines in this section support adding, removing and changing
1644 * device tree nodes.
1647 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1649 int of_reconfig_notifier_register(struct notifier_block *nb)
1651 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1653 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1655 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1657 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1659 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1661 int of_reconfig_notify(unsigned long action, void *p)
1665 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1666 return notifier_to_errno(rc);
1669 #ifdef CONFIG_PROC_DEVICETREE
1670 static void of_add_proc_dt_entry(struct device_node *dn)
1672 struct proc_dir_entry *ent;
1674 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1676 proc_device_tree_add_node(dn, ent);
1679 static void of_add_proc_dt_entry(struct device_node *dn)
1686 * of_attach_node - Plug a device node into the tree and global list.
1688 int of_attach_node(struct device_node *np)
1690 unsigned long flags;
1693 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1697 raw_spin_lock_irqsave(&devtree_lock, flags);
1698 np->sibling = np->parent->child;
1699 np->allnext = of_allnodes;
1700 np->parent->child = np;
1702 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1704 of_add_proc_dt_entry(np);
1708 #ifdef CONFIG_PROC_DEVICETREE
1709 static void of_remove_proc_dt_entry(struct device_node *dn)
1711 proc_remove(dn->pde);
1714 static void of_remove_proc_dt_entry(struct device_node *dn)
1721 * of_detach_node - "Unplug" a node from the device tree.
1723 * The caller must hold a reference to the node. The memory associated with
1724 * the node is not freed until its refcount goes to zero.
1726 int of_detach_node(struct device_node *np)
1728 struct device_node *parent;
1729 unsigned long flags;
1732 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1736 raw_spin_lock_irqsave(&devtree_lock, flags);
1738 if (of_node_check_flag(np, OF_DETACHED)) {
1739 /* someone already detached it */
1740 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1744 parent = np->parent;
1746 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1750 if (of_allnodes == np)
1751 of_allnodes = np->allnext;
1753 struct device_node *prev;
1754 for (prev = of_allnodes;
1755 prev->allnext != np;
1756 prev = prev->allnext)
1758 prev->allnext = np->allnext;
1761 if (parent->child == np)
1762 parent->child = np->sibling;
1764 struct device_node *prevsib;
1765 for (prevsib = np->parent->child;
1766 prevsib->sibling != np;
1767 prevsib = prevsib->sibling)
1769 prevsib->sibling = np->sibling;
1772 of_node_set_flag(np, OF_DETACHED);
1773 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1775 of_remove_proc_dt_entry(np);
1778 #endif /* defined(CONFIG_OF_DYNAMIC) */
1780 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1781 int id, const char *stem, int stem_len)
1785 strncpy(ap->stem, stem, stem_len);
1786 ap->stem[stem_len] = 0;
1787 list_add_tail(&ap->link, &aliases_lookup);
1788 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1789 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1793 * of_alias_scan - Scan all properties of 'aliases' node
1795 * The function scans all the properties of 'aliases' node and populate
1796 * the the global lookup table with the properties. It returns the
1797 * number of alias_prop found, or error code in error case.
1799 * @dt_alloc: An allocator that provides a virtual address to memory
1800 * for the resulting tree
1802 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1804 struct property *pp;
1806 of_chosen = of_find_node_by_path("/chosen");
1807 if (of_chosen == NULL)
1808 of_chosen = of_find_node_by_path("/chosen@0");
1813 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1815 of_stdout = of_find_node_by_path(name);
1818 of_aliases = of_find_node_by_path("/aliases");
1822 for_each_property_of_node(of_aliases, pp) {
1823 const char *start = pp->name;
1824 const char *end = start + strlen(start);
1825 struct device_node *np;
1826 struct alias_prop *ap;
1829 /* Skip those we do not want to proceed */
1830 if (!strcmp(pp->name, "name") ||
1831 !strcmp(pp->name, "phandle") ||
1832 !strcmp(pp->name, "linux,phandle"))
1835 np = of_find_node_by_path(pp->value);
1839 /* walk the alias backwards to extract the id and work out
1840 * the 'stem' string */
1841 while (isdigit(*(end-1)) && end > start)
1845 if (kstrtoint(end, 10, &id) < 0)
1848 /* Allocate an alias_prop with enough space for the stem */
1849 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1852 memset(ap, 0, sizeof(*ap) + len + 1);
1854 of_alias_add(ap, np, id, start, len);
1859 * of_alias_get_id - Get alias id for the given device_node
1860 * @np: Pointer to the given device_node
1861 * @stem: Alias stem of the given device_node
1863 * The function travels the lookup table to get alias id for the given
1864 * device_node and alias stem. It returns the alias id if find it.
1866 int of_alias_get_id(struct device_node *np, const char *stem)
1868 struct alias_prop *app;
1871 mutex_lock(&of_aliases_mutex);
1872 list_for_each_entry(app, &aliases_lookup, link) {
1873 if (strcmp(app->stem, stem) != 0)
1876 if (np == app->np) {
1881 mutex_unlock(&of_aliases_mutex);
1885 EXPORT_SYMBOL_GPL(of_alias_get_id);
1887 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1890 const void *curv = cur;
1900 curv += sizeof(*cur);
1901 if (curv >= prop->value + prop->length)
1905 *pu = be32_to_cpup(curv);
1908 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1910 const char *of_prop_next_string(struct property *prop, const char *cur)
1912 const void *curv = cur;
1920 curv += strlen(cur) + 1;
1921 if (curv >= prop->value + prop->length)
1926 EXPORT_SYMBOL_GPL(of_prop_next_string);
1929 * of_device_is_stdout_path - check if a device node matches the
1930 * linux,stdout-path property
1932 * Check if this device node matches the linux,stdout-path property
1933 * in the chosen node. return true if yes, false otherwise.
1935 int of_device_is_stdout_path(struct device_node *dn)
1940 return of_stdout == dn;
1942 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
1945 * of_find_next_cache_node - Find a node's subsidiary cache
1946 * @np: node of type "cpu" or "cache"
1948 * Returns a node pointer with refcount incremented, use
1949 * of_node_put() on it when done. Caller should hold a reference
1952 struct device_node *of_find_next_cache_node(const struct device_node *np)
1954 struct device_node *child;
1955 const phandle *handle;
1957 handle = of_get_property(np, "l2-cache", NULL);
1959 handle = of_get_property(np, "next-level-cache", NULL);
1962 return of_find_node_by_phandle(be32_to_cpup(handle));
1964 /* OF on pmac has nodes instead of properties named "l2-cache"
1965 * beneath CPU nodes.
1967 if (!strcmp(np->type, "cpu"))
1968 for_each_child_of_node(np, child)
1969 if (!strcmp(child->type, "cache"))
1976 * of_graph_parse_endpoint() - parse common endpoint node properties
1977 * @node: pointer to endpoint device_node
1978 * @endpoint: pointer to the OF endpoint data structure
1980 * The caller should hold a reference to @node.
1982 int of_graph_parse_endpoint(const struct device_node *node,
1983 struct of_endpoint *endpoint)
1985 struct device_node *port_node = of_get_parent(node);
1987 memset(endpoint, 0, sizeof(*endpoint));
1989 endpoint->local_node = node;
1991 * It doesn't matter whether the two calls below succeed.
1992 * If they don't then the default value 0 is used.
1994 of_property_read_u32(port_node, "reg", &endpoint->port);
1995 of_property_read_u32(node, "reg", &endpoint->id);
1997 of_node_put(port_node);
2001 EXPORT_SYMBOL(of_graph_parse_endpoint);
2004 * of_graph_get_next_endpoint() - get next endpoint node
2005 * @parent: pointer to the parent device node
2006 * @prev: previous endpoint node, or NULL to get first
2008 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2009 * of the passed @prev node is not decremented, the caller have to use
2010 * of_node_put() on it when done.
2012 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2013 struct device_node *prev)
2015 struct device_node *endpoint;
2016 struct device_node *port = NULL;
2022 struct device_node *node;
2024 * It's the first call, we have to find a port subnode
2025 * within this node or within an optional 'ports' node.
2027 node = of_get_child_by_name(parent, "ports");
2031 port = of_get_child_by_name(parent, "port");
2034 /* Found a port, get an endpoint. */
2035 endpoint = of_get_next_child(port, NULL);
2042 pr_err("%s(): no endpoint nodes specified for %s\n",
2043 __func__, parent->full_name);
2046 port = of_get_parent(prev);
2048 /* Hm, has someone given us the root node ?... */
2051 /* Avoid dropping prev node refcount to 0. */
2053 endpoint = of_get_next_child(port, prev);
2059 /* No more endpoints under this port, try the next one. */
2061 port = of_get_next_child(parent, port);
2064 } while (of_node_cmp(port->name, "port"));
2066 /* Pick up the first endpoint in this port. */
2067 endpoint = of_get_next_child(port, NULL);
2073 EXPORT_SYMBOL(of_graph_get_next_endpoint);
2076 * of_graph_get_remote_port_parent() - get remote port's parent node
2077 * @node: pointer to a local endpoint device_node
2079 * Return: Remote device node associated with remote endpoint node linked
2080 * to @node. Use of_node_put() on it when done.
2082 struct device_node *of_graph_get_remote_port_parent(
2083 const struct device_node *node)
2085 struct device_node *np;
2088 /* Get remote endpoint node. */
2089 np = of_parse_phandle(node, "remote-endpoint", 0);
2091 /* Walk 3 levels up only if there is 'ports' node. */
2092 for (depth = 3; depth && np; depth--) {
2093 np = of_get_next_parent(np);
2094 if (depth == 2 && of_node_cmp(np->name, "ports"))
2099 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2102 * of_graph_get_remote_port() - get remote port node
2103 * @node: pointer to a local endpoint device_node
2105 * Return: Remote port node associated with remote endpoint node linked
2106 * to @node. Use of_node_put() on it when done.
2108 struct device_node *of_graph_get_remote_port(const struct device_node *node)
2110 struct device_node *np;
2112 /* Get remote endpoint node. */
2113 np = of_parse_phandle(node, "remote-endpoint", 0);
2116 return of_get_next_parent(np);
2118 EXPORT_SYMBOL(of_graph_get_remote_port);