dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / of / base.c
blob31341290cd9136ab0b2830c67422eb334ed32f4e
1 /*
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
13 * Grant Likely.
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/console.h>
21 #include <linux/ctype.h>
22 #include <linux/cpu.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/of_graph.h>
26 #include <linux/spinlock.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <linux/proc_fs.h>
31 #include "of_private.h"
33 LIST_HEAD(aliases_lookup);
35 struct device_node *of_root;
36 EXPORT_SYMBOL(of_root);
37 struct device_node *of_chosen;
38 struct device_node *of_aliases;
39 struct device_node *of_stdout;
40 static const char *of_stdout_options;
42 struct kset *of_kset;
45 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
46 * This mutex must be held whenever modifications are being made to the
47 * device tree. The of_{attach,detach}_node() and
48 * of_{add,remove,update}_property() helpers make sure this happens.
50 DEFINE_MUTEX(of_mutex);
52 /* use when traversing tree through the child, sibling,
53 * or parent members of struct device_node.
55 DEFINE_RAW_SPINLOCK(devtree_lock);
57 int of_n_addr_cells(struct device_node *np)
59 const __be32 *ip;
61 do {
62 if (np->parent)
63 np = np->parent;
64 ip = of_get_property(np, "#address-cells", NULL);
65 if (ip)
66 return be32_to_cpup(ip);
67 } while (np->parent);
68 /* No #address-cells property for the root node */
69 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
71 EXPORT_SYMBOL(of_n_addr_cells);
73 int of_n_size_cells(struct device_node *np)
75 const __be32 *ip;
77 do {
78 if (np->parent)
79 np = np->parent;
80 ip = of_get_property(np, "#size-cells", NULL);
81 if (ip)
82 return be32_to_cpup(ip);
83 } while (np->parent);
84 /* No #size-cells property for the root node */
85 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
87 EXPORT_SYMBOL(of_n_size_cells);
89 #ifdef CONFIG_NUMA
90 int __weak of_node_to_nid(struct device_node *np)
92 return NUMA_NO_NODE;
94 #endif
96 #ifndef CONFIG_OF_DYNAMIC
97 static void of_node_release(struct kobject *kobj)
99 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
101 #endif /* CONFIG_OF_DYNAMIC */
103 struct kobj_type of_node_ktype = {
104 .release = of_node_release,
107 static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
108 struct bin_attribute *bin_attr, char *buf,
109 loff_t offset, size_t count)
111 struct property *pp = container_of(bin_attr, struct property, attr);
112 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
115 /* always return newly allocated name, caller must free after use */
116 static const char *safe_name(struct kobject *kobj, const char *orig_name)
118 const char *name = orig_name;
119 struct kernfs_node *kn;
120 int i = 0;
122 /* don't be a hero. After 16 tries give up */
123 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
124 sysfs_put(kn);
125 if (name != orig_name)
126 kfree(name);
127 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
130 if (name == orig_name) {
131 name = kstrdup(orig_name, GFP_KERNEL);
132 } else {
133 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
134 kobject_name(kobj), name);
136 return name;
139 int __of_add_property_sysfs(struct device_node *np, struct property *pp)
141 int rc;
143 /* Important: Don't leak passwords */
144 bool secure = strncmp(pp->name, "security-", 9) == 0;
146 if (!IS_ENABLED(CONFIG_SYSFS))
147 return 0;
149 if (!of_kset || !of_node_is_attached(np))
150 return 0;
152 sysfs_bin_attr_init(&pp->attr);
153 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
154 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
155 pp->attr.size = secure ? 0 : pp->length;
156 pp->attr.read = of_node_property_read;
158 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
159 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
160 return rc;
163 int __of_attach_node_sysfs(struct device_node *np)
165 const char *name;
166 struct kobject *parent;
167 struct property *pp;
168 int rc;
170 if (!IS_ENABLED(CONFIG_SYSFS))
171 return 0;
173 if (!of_kset)
174 return 0;
176 np->kobj.kset = of_kset;
177 if (!np->parent) {
178 /* Nodes without parents are new top level trees */
179 name = safe_name(&of_kset->kobj, "base");
180 parent = NULL;
181 } else {
182 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
183 parent = &np->parent->kobj;
185 if (!name)
186 return -ENOMEM;
187 rc = kobject_add(&np->kobj, parent, "%s", name);
188 kfree(name);
189 if (rc)
190 return rc;
192 for_each_property_of_node(np, pp)
193 __of_add_property_sysfs(np, pp);
195 return 0;
198 void __init of_core_init(void)
200 struct device_node *np;
202 /* Create the kset, and register existing nodes */
203 mutex_lock(&of_mutex);
204 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
205 if (!of_kset) {
206 mutex_unlock(&of_mutex);
207 pr_err("devicetree: failed to register existing nodes\n");
208 return;
210 for_each_of_allnodes(np)
211 __of_attach_node_sysfs(np);
212 mutex_unlock(&of_mutex);
214 /* Symlink in /proc as required by userspace ABI */
215 if (of_root)
216 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
219 static struct property *__of_find_property(const struct device_node *np,
220 const char *name, int *lenp)
222 struct property *pp;
224 if (!np)
225 return NULL;
227 for (pp = np->properties; pp; pp = pp->next) {
228 if (of_prop_cmp(pp->name, name) == 0) {
229 if (lenp)
230 *lenp = pp->length;
231 break;
235 return pp;
238 struct property *of_find_property(const struct device_node *np,
239 const char *name,
240 int *lenp)
242 struct property *pp;
243 unsigned long flags;
245 raw_spin_lock_irqsave(&devtree_lock, flags);
246 pp = __of_find_property(np, name, lenp);
247 raw_spin_unlock_irqrestore(&devtree_lock, flags);
249 return pp;
251 EXPORT_SYMBOL(of_find_property);
253 struct device_node *__of_find_all_nodes(struct device_node *prev)
255 struct device_node *np;
256 if (!prev) {
257 np = of_root;
258 } else if (prev->child) {
259 np = prev->child;
260 } else {
261 /* Walk back up looking for a sibling, or the end of the structure */
262 np = prev;
263 while (np->parent && !np->sibling)
264 np = np->parent;
265 np = np->sibling; /* Might be null at the end of the tree */
267 return np;
271 * of_find_all_nodes - Get next node in global list
272 * @prev: Previous node or NULL to start iteration
273 * of_node_put() will be called on it
275 * Returns a node pointer with refcount incremented, use
276 * of_node_put() on it when done.
278 struct device_node *of_find_all_nodes(struct device_node *prev)
280 struct device_node *np;
281 unsigned long flags;
283 raw_spin_lock_irqsave(&devtree_lock, flags);
284 np = __of_find_all_nodes(prev);
285 of_node_get(np);
286 of_node_put(prev);
287 raw_spin_unlock_irqrestore(&devtree_lock, flags);
288 return np;
290 EXPORT_SYMBOL(of_find_all_nodes);
293 * Find a property with a given name for a given node
294 * and return the value.
296 const void *__of_get_property(const struct device_node *np,
297 const char *name, int *lenp)
299 struct property *pp = __of_find_property(np, name, lenp);
301 return pp ? pp->value : NULL;
305 * Find a property with a given name for a given node
306 * and return the value.
308 const void *of_get_property(const struct device_node *np, const char *name,
309 int *lenp)
311 struct property *pp = of_find_property(np, name, lenp);
313 return pp ? pp->value : NULL;
315 EXPORT_SYMBOL(of_get_property);
318 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
320 * @cpu: logical cpu index of a core/thread
321 * @phys_id: physical identifier of a core/thread
323 * CPU logical to physical index mapping is architecture specific.
324 * However this __weak function provides a default match of physical
325 * id to logical cpu index. phys_id provided here is usually values read
326 * from the device tree which must match the hardware internal registers.
328 * Returns true if the physical identifier and the logical cpu index
329 * correspond to the same core/thread, false otherwise.
331 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
333 return (u32)phys_id == cpu;
337 * Checks if the given "prop_name" property holds the physical id of the
338 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
339 * NULL, local thread number within the core is returned in it.
341 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
342 const char *prop_name, int cpu, unsigned int *thread)
344 const __be32 *cell;
345 int ac, prop_len, tid;
346 u64 hwid;
348 ac = of_n_addr_cells(cpun);
349 cell = of_get_property(cpun, prop_name, &prop_len);
350 if (!cell || !ac)
351 return false;
352 prop_len /= sizeof(*cell) * ac;
353 for (tid = 0; tid < prop_len; tid++) {
354 hwid = of_read_number(cell, ac);
355 if (arch_match_cpu_phys_id(cpu, hwid)) {
356 if (thread)
357 *thread = tid;
358 return true;
360 cell += ac;
362 return false;
366 * arch_find_n_match_cpu_physical_id - See if the given device node is
367 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
368 * else false. If 'thread' is non-NULL, the local thread number within the
369 * core is returned in it.
371 bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
372 int cpu, unsigned int *thread)
374 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
375 * for thread ids on PowerPC. If it doesn't exist fallback to
376 * standard "reg" property.
378 if (IS_ENABLED(CONFIG_PPC) &&
379 __of_find_n_match_cpu_property(cpun,
380 "ibm,ppc-interrupt-server#s",
381 cpu, thread))
382 return true;
384 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
388 * of_get_cpu_node - Get device node associated with the given logical CPU
390 * @cpu: CPU number(logical index) for which device node is required
391 * @thread: if not NULL, local thread number within the physical core is
392 * returned
394 * The main purpose of this function is to retrieve the device node for the
395 * given logical CPU index. It should be used to initialize the of_node in
396 * cpu device. Once of_node in cpu device is populated, all the further
397 * references can use that instead.
399 * CPU logical to physical index mapping is architecture specific and is built
400 * before booting secondary cores. This function uses arch_match_cpu_phys_id
401 * which can be overridden by architecture specific implementation.
403 * Returns a node pointer for the logical cpu if found, else NULL.
405 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
407 struct device_node *cpun;
409 for_each_node_by_type(cpun, "cpu") {
410 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
411 return cpun;
413 return NULL;
415 EXPORT_SYMBOL(of_get_cpu_node);
418 * __of_device_is_compatible() - Check if the node matches given constraints
419 * @device: pointer to node
420 * @compat: required compatible string, NULL or "" for any match
421 * @type: required device_type value, NULL or "" for any match
422 * @name: required node name, NULL or "" for any match
424 * Checks if the given @compat, @type and @name strings match the
425 * properties of the given @device. A constraints can be skipped by
426 * passing NULL or an empty string as the constraint.
428 * Returns 0 for no match, and a positive integer on match. The return
429 * value is a relative score with larger values indicating better
430 * matches. The score is weighted for the most specific compatible value
431 * to get the highest score. Matching type is next, followed by matching
432 * name. Practically speaking, this results in the following priority
433 * order for matches:
435 * 1. specific compatible && type && name
436 * 2. specific compatible && type
437 * 3. specific compatible && name
438 * 4. specific compatible
439 * 5. general compatible && type && name
440 * 6. general compatible && type
441 * 7. general compatible && name
442 * 8. general compatible
443 * 9. type && name
444 * 10. type
445 * 11. name
447 static int __of_device_is_compatible(const struct device_node *device,
448 const char *compat, const char *type, const char *name)
450 struct property *prop;
451 const char *cp;
452 int index = 0, score = 0;
454 /* Compatible match has highest priority */
455 if (compat && compat[0]) {
456 prop = __of_find_property(device, "compatible", NULL);
457 for (cp = of_prop_next_string(prop, NULL); cp;
458 cp = of_prop_next_string(prop, cp), index++) {
459 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
460 score = INT_MAX/2 - (index << 2);
461 break;
464 if (!score)
465 return 0;
468 /* Matching type is better than matching name */
469 if (type && type[0]) {
470 if (!device->type || of_node_cmp(type, device->type))
471 return 0;
472 score += 2;
475 /* Matching name is a bit better than not */
476 if (name && name[0]) {
477 if (!device->name || of_node_cmp(name, device->name))
478 return 0;
479 score++;
482 return score;
485 /** Checks if the given "compat" string matches one of the strings in
486 * the device's "compatible" property
488 int of_device_is_compatible(const struct device_node *device,
489 const char *compat)
491 unsigned long flags;
492 int res;
494 raw_spin_lock_irqsave(&devtree_lock, flags);
495 res = __of_device_is_compatible(device, compat, NULL, NULL);
496 raw_spin_unlock_irqrestore(&devtree_lock, flags);
497 return res;
499 EXPORT_SYMBOL(of_device_is_compatible);
502 * of_machine_is_compatible - Test root of device tree for a given compatible value
503 * @compat: compatible string to look for in root node's compatible property.
505 * Returns a positive integer if the root node has the given value in its
506 * compatible property.
508 int of_machine_is_compatible(const char *compat)
510 struct device_node *root;
511 int rc = 0;
513 root = of_find_node_by_path("/");
514 if (root) {
515 rc = of_device_is_compatible(root, compat);
516 of_node_put(root);
518 return rc;
520 EXPORT_SYMBOL(of_machine_is_compatible);
523 * __of_device_is_available - check if a device is available for use
525 * @device: Node to check for availability, with locks already held
527 * Returns true if the status property is absent or set to "okay" or "ok",
528 * false otherwise
530 static bool __of_device_is_available(const struct device_node *device)
532 const char *status;
533 int statlen;
535 if (!device)
536 return false;
538 status = __of_get_property(device, "status", &statlen);
539 if (status == NULL)
540 return true;
542 if (statlen > 0) {
543 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
544 return true;
547 return false;
551 * of_device_is_available - check if a device is available for use
553 * @device: Node to check for availability
555 * Returns true if the status property is absent or set to "okay" or "ok",
556 * false otherwise
558 bool of_device_is_available(const struct device_node *device)
560 unsigned long flags;
561 bool res;
563 raw_spin_lock_irqsave(&devtree_lock, flags);
564 res = __of_device_is_available(device);
565 raw_spin_unlock_irqrestore(&devtree_lock, flags);
566 return res;
569 EXPORT_SYMBOL(of_device_is_available);
572 * of_device_is_big_endian - check if a device has BE registers
574 * @device: Node to check for endianness
576 * Returns true if the device has a "big-endian" property, or if the kernel
577 * was compiled for BE *and* the device has a "native-endian" property.
578 * Returns false otherwise.
580 * Callers would nominally use ioread32be/iowrite32be if
581 * of_device_is_big_endian() == true, or readl/writel otherwise.
583 bool of_device_is_big_endian(const struct device_node *device)
585 if (of_property_read_bool(device, "big-endian"))
586 return true;
587 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
588 of_property_read_bool(device, "native-endian"))
589 return true;
590 return false;
592 EXPORT_SYMBOL(of_device_is_big_endian);
595 * of_get_parent - Get a node's parent if any
596 * @node: Node to get parent
598 * Returns a node pointer with refcount incremented, use
599 * of_node_put() on it when done.
601 struct device_node *of_get_parent(const struct device_node *node)
603 struct device_node *np;
604 unsigned long flags;
606 if (!node)
607 return NULL;
609 raw_spin_lock_irqsave(&devtree_lock, flags);
610 np = of_node_get(node->parent);
611 raw_spin_unlock_irqrestore(&devtree_lock, flags);
612 return np;
614 EXPORT_SYMBOL(of_get_parent);
617 * of_get_next_parent - Iterate to a node's parent
618 * @node: Node to get parent of
620 * This is like of_get_parent() except that it drops the
621 * refcount on the passed node, making it suitable for iterating
622 * through a node's parents.
624 * Returns a node pointer with refcount incremented, use
625 * of_node_put() on it when done.
627 struct device_node *of_get_next_parent(struct device_node *node)
629 struct device_node *parent;
630 unsigned long flags;
632 if (!node)
633 return NULL;
635 raw_spin_lock_irqsave(&devtree_lock, flags);
636 parent = of_node_get(node->parent);
637 of_node_put(node);
638 raw_spin_unlock_irqrestore(&devtree_lock, flags);
639 return parent;
641 EXPORT_SYMBOL(of_get_next_parent);
643 static struct device_node *__of_get_next_child(const struct device_node *node,
644 struct device_node *prev)
646 struct device_node *next;
648 if (!node)
649 return NULL;
651 next = prev ? prev->sibling : node->child;
652 for (; next; next = next->sibling)
653 if (of_node_get(next))
654 break;
655 of_node_put(prev);
656 return next;
658 #define __for_each_child_of_node(parent, child) \
659 for (child = __of_get_next_child(parent, NULL); child != NULL; \
660 child = __of_get_next_child(parent, child))
663 * of_get_next_child - Iterate a node childs
664 * @node: parent node
665 * @prev: previous child of the parent node, or NULL to get first
667 * Returns a node pointer with refcount incremented, use of_node_put() on
668 * it when done. Returns NULL when prev is the last child. Decrements the
669 * refcount of prev.
671 struct device_node *of_get_next_child(const struct device_node *node,
672 struct device_node *prev)
674 struct device_node *next;
675 unsigned long flags;
677 raw_spin_lock_irqsave(&devtree_lock, flags);
678 next = __of_get_next_child(node, prev);
679 raw_spin_unlock_irqrestore(&devtree_lock, flags);
680 return next;
682 EXPORT_SYMBOL(of_get_next_child);
685 * of_get_next_available_child - Find the next available child node
686 * @node: parent node
687 * @prev: previous child of the parent node, or NULL to get first
689 * This function is like of_get_next_child(), except that it
690 * automatically skips any disabled nodes (i.e. status = "disabled").
692 struct device_node *of_get_next_available_child(const struct device_node *node,
693 struct device_node *prev)
695 struct device_node *next;
696 unsigned long flags;
698 if (!node)
699 return NULL;
701 raw_spin_lock_irqsave(&devtree_lock, flags);
702 next = prev ? prev->sibling : node->child;
703 for (; next; next = next->sibling) {
704 if (!__of_device_is_available(next))
705 continue;
706 if (of_node_get(next))
707 break;
709 of_node_put(prev);
710 raw_spin_unlock_irqrestore(&devtree_lock, flags);
711 return next;
713 EXPORT_SYMBOL(of_get_next_available_child);
716 * of_get_child_by_name - Find the child node by name for a given parent
717 * @node: parent node
718 * @name: child name to look for.
720 * This function looks for child node for given matching name
722 * Returns a node pointer if found, with refcount incremented, use
723 * of_node_put() on it when done.
724 * Returns NULL if node is not found.
726 struct device_node *of_get_child_by_name(const struct device_node *node,
727 const char *name)
729 struct device_node *child;
731 for_each_child_of_node(node, child)
732 if (child->name && (of_node_cmp(child->name, name) == 0))
733 break;
734 return child;
736 EXPORT_SYMBOL(of_get_child_by_name);
738 static struct device_node *__of_find_node_by_path(struct device_node *parent,
739 const char *path)
741 struct device_node *child;
742 int len;
744 len = strcspn(path, "/:");
745 if (!len)
746 return NULL;
748 __for_each_child_of_node(parent, child) {
749 const char *name = strrchr(child->full_name, '/');
750 if (WARN(!name, "malformed device_node %s\n", child->full_name))
751 continue;
752 name++;
753 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
754 return child;
756 return NULL;
760 * of_find_node_opts_by_path - Find a node matching a full OF path
761 * @path: Either the full path to match, or if the path does not
762 * start with '/', the name of a property of the /aliases
763 * node (an alias). In the case of an alias, the node
764 * matching the alias' value will be returned.
765 * @opts: Address of a pointer into which to store the start of
766 * an options string appended to the end of the path with
767 * a ':' separator.
769 * Valid paths:
770 * /foo/bar Full path
771 * foo Valid alias
772 * foo/bar Valid alias + relative path
774 * Returns a node pointer with refcount incremented, use
775 * of_node_put() on it when done.
777 struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
779 struct device_node *np = NULL;
780 struct property *pp;
781 unsigned long flags;
782 const char *separator = strchr(path, ':');
784 if (opts)
785 *opts = separator ? separator + 1 : NULL;
787 if (strcmp(path, "/") == 0)
788 return of_node_get(of_root);
790 /* The path could begin with an alias */
791 if (*path != '/') {
792 int len;
793 const char *p = separator;
795 if (!p)
796 p = strchrnul(path, '/');
797 len = p - path;
799 /* of_aliases must not be NULL */
800 if (!of_aliases)
801 return NULL;
803 for_each_property_of_node(of_aliases, pp) {
804 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
805 np = of_find_node_by_path(pp->value);
806 break;
809 if (!np)
810 return NULL;
811 path = p;
814 /* Step down the tree matching path components */
815 raw_spin_lock_irqsave(&devtree_lock, flags);
816 if (!np)
817 np = of_node_get(of_root);
818 while (np && *path == '/') {
819 path++; /* Increment past '/' delimiter */
820 np = __of_find_node_by_path(np, path);
821 path = strchrnul(path, '/');
822 if (separator && separator < path)
823 break;
825 raw_spin_unlock_irqrestore(&devtree_lock, flags);
826 return np;
828 EXPORT_SYMBOL(of_find_node_opts_by_path);
831 * of_find_node_by_name - Find a node by its "name" property
832 * @from: The node to start searching from or NULL, the node
833 * you pass will not be searched, only the next one
834 * will; typically, you pass what the previous call
835 * returned. of_node_put() will be called on it
836 * @name: The name string to match against
838 * Returns a node pointer with refcount incremented, use
839 * of_node_put() on it when done.
841 struct device_node *of_find_node_by_name(struct device_node *from,
842 const char *name)
844 struct device_node *np;
845 unsigned long flags;
847 raw_spin_lock_irqsave(&devtree_lock, flags);
848 for_each_of_allnodes_from(from, np)
849 if (np->name && (of_node_cmp(np->name, name) == 0)
850 && of_node_get(np))
851 break;
852 of_node_put(from);
853 raw_spin_unlock_irqrestore(&devtree_lock, flags);
854 return np;
856 EXPORT_SYMBOL(of_find_node_by_name);
859 * of_find_node_by_type - Find a node by its "device_type" property
860 * @from: The node to start searching from, or NULL to start searching
861 * the entire device tree. The node you pass will not be
862 * searched, only the next one will; typically, you pass
863 * what the previous call returned. of_node_put() will be
864 * called on from for you.
865 * @type: The type string to match against
867 * Returns a node pointer with refcount incremented, use
868 * of_node_put() on it when done.
870 struct device_node *of_find_node_by_type(struct device_node *from,
871 const char *type)
873 struct device_node *np;
874 unsigned long flags;
876 raw_spin_lock_irqsave(&devtree_lock, flags);
877 for_each_of_allnodes_from(from, np)
878 if (np->type && (of_node_cmp(np->type, type) == 0)
879 && of_node_get(np))
880 break;
881 of_node_put(from);
882 raw_spin_unlock_irqrestore(&devtree_lock, flags);
883 return np;
885 EXPORT_SYMBOL(of_find_node_by_type);
888 * of_find_compatible_node - Find a node based on type and one of the
889 * tokens in its "compatible" property
890 * @from: The node to start searching from or NULL, the node
891 * you pass will not be searched, only the next one
892 * will; typically, you pass what the previous call
893 * returned. of_node_put() will be called on it
894 * @type: The type string to match "device_type" or NULL to ignore
895 * @compatible: The string to match to one of the tokens in the device
896 * "compatible" list.
898 * Returns a node pointer with refcount incremented, use
899 * of_node_put() on it when done.
901 struct device_node *of_find_compatible_node(struct device_node *from,
902 const char *type, const char *compatible)
904 struct device_node *np;
905 unsigned long flags;
907 raw_spin_lock_irqsave(&devtree_lock, flags);
908 for_each_of_allnodes_from(from, np)
909 if (__of_device_is_compatible(np, compatible, type, NULL) &&
910 of_node_get(np))
911 break;
912 of_node_put(from);
913 raw_spin_unlock_irqrestore(&devtree_lock, flags);
914 return np;
916 EXPORT_SYMBOL(of_find_compatible_node);
919 * of_find_node_with_property - Find a node which has a property with
920 * the given name.
921 * @from: The node to start searching from or NULL, the node
922 * you pass will not be searched, only the next one
923 * will; typically, you pass what the previous call
924 * returned. of_node_put() will be called on it
925 * @prop_name: The name of the property to look for.
927 * Returns a node pointer with refcount incremented, use
928 * of_node_put() on it when done.
930 struct device_node *of_find_node_with_property(struct device_node *from,
931 const char *prop_name)
933 struct device_node *np;
934 struct property *pp;
935 unsigned long flags;
937 raw_spin_lock_irqsave(&devtree_lock, flags);
938 for_each_of_allnodes_from(from, np) {
939 for (pp = np->properties; pp; pp = pp->next) {
940 if (of_prop_cmp(pp->name, prop_name) == 0) {
941 of_node_get(np);
942 goto out;
946 out:
947 of_node_put(from);
948 raw_spin_unlock_irqrestore(&devtree_lock, flags);
949 return np;
951 EXPORT_SYMBOL(of_find_node_with_property);
953 static
954 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
955 const struct device_node *node)
957 const struct of_device_id *best_match = NULL;
958 int score, best_score = 0;
960 if (!matches)
961 return NULL;
963 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
964 score = __of_device_is_compatible(node, matches->compatible,
965 matches->type, matches->name);
966 if (score > best_score) {
967 best_match = matches;
968 best_score = score;
972 return best_match;
976 * of_match_node - Tell if a device_node has a matching of_match structure
977 * @matches: array of of device match structures to search in
978 * @node: the of device structure to match against
980 * Low level utility function used by device matching.
982 const struct of_device_id *of_match_node(const struct of_device_id *matches,
983 const struct device_node *node)
985 const struct of_device_id *match;
986 unsigned long flags;
988 raw_spin_lock_irqsave(&devtree_lock, flags);
989 match = __of_match_node(matches, node);
990 raw_spin_unlock_irqrestore(&devtree_lock, flags);
991 return match;
993 EXPORT_SYMBOL(of_match_node);
996 * of_find_matching_node_and_match - Find a node based on an of_device_id
997 * match table.
998 * @from: The node to start searching from or NULL, the node
999 * you pass will not be searched, only the next one
1000 * will; typically, you pass what the previous call
1001 * returned. of_node_put() will be called on it
1002 * @matches: array of of device match structures to search in
1003 * @match Updated to point at the matches entry which matched
1005 * Returns a node pointer with refcount incremented, use
1006 * of_node_put() on it when done.
1008 struct device_node *of_find_matching_node_and_match(struct device_node *from,
1009 const struct of_device_id *matches,
1010 const struct of_device_id **match)
1012 struct device_node *np;
1013 const struct of_device_id *m;
1014 unsigned long flags;
1016 if (match)
1017 *match = NULL;
1019 raw_spin_lock_irqsave(&devtree_lock, flags);
1020 for_each_of_allnodes_from(from, np) {
1021 m = __of_match_node(matches, np);
1022 if (m && of_node_get(np)) {
1023 if (match)
1024 *match = m;
1025 break;
1028 of_node_put(from);
1029 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1030 return np;
1032 EXPORT_SYMBOL(of_find_matching_node_and_match);
1035 * of_modalias_node - Lookup appropriate modalias for a device node
1036 * @node: pointer to a device tree node
1037 * @modalias: Pointer to buffer that modalias value will be copied into
1038 * @len: Length of modalias value
1040 * Based on the value of the compatible property, this routine will attempt
1041 * to choose an appropriate modalias value for a particular device tree node.
1042 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1043 * from the first entry in the compatible list property.
1045 * This routine returns 0 on success, <0 on failure.
1047 int of_modalias_node(struct device_node *node, char *modalias, int len)
1049 const char *compatible, *p;
1050 int cplen;
1052 compatible = of_get_property(node, "compatible", &cplen);
1053 if (!compatible || strlen(compatible) > cplen)
1054 return -ENODEV;
1055 p = strchr(compatible, ',');
1056 strlcpy(modalias, p ? p + 1 : compatible, len);
1057 return 0;
1059 EXPORT_SYMBOL_GPL(of_modalias_node);
1062 * of_find_node_by_phandle - Find a node given a phandle
1063 * @handle: phandle of the node to find
1065 * Returns a node pointer with refcount incremented, use
1066 * of_node_put() on it when done.
1068 struct device_node *of_find_node_by_phandle(phandle handle)
1070 struct device_node *np;
1071 unsigned long flags;
1073 if (!handle)
1074 return NULL;
1076 raw_spin_lock_irqsave(&devtree_lock, flags);
1077 for_each_of_allnodes(np)
1078 if (np->phandle == handle)
1079 break;
1080 of_node_get(np);
1081 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1082 return np;
1084 EXPORT_SYMBOL(of_find_node_by_phandle);
1087 * of_property_count_elems_of_size - Count the number of elements in a property
1089 * @np: device node from which the property value is to be read.
1090 * @propname: name of the property to be searched.
1091 * @elem_size: size of the individual element
1093 * Search for a property in a device node and count the number of elements of
1094 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1095 * property does not exist or its length does not match a multiple of elem_size
1096 * and -ENODATA if the property does not have a value.
1098 int of_property_count_elems_of_size(const struct device_node *np,
1099 const char *propname, int elem_size)
1101 struct property *prop = of_find_property(np, propname, NULL);
1103 if (!prop)
1104 return -EINVAL;
1105 if (!prop->value)
1106 return -ENODATA;
1108 if (prop->length % elem_size != 0) {
1109 pr_err("size of %s in node %s is not a multiple of %d\n",
1110 propname, np->full_name, elem_size);
1111 return -EINVAL;
1114 return prop->length / elem_size;
1116 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1119 * of_find_property_value_of_size
1121 * @np: device node from which the property value is to be read.
1122 * @propname: name of the property to be searched.
1123 * @len: requested length of property value
1125 * Search for a property in a device node and valid the requested size.
1126 * Returns the property value on success, -EINVAL if the property does not
1127 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1128 * property data isn't large enough.
1131 static void *of_find_property_value_of_size(const struct device_node *np,
1132 const char *propname, u32 len)
1134 struct property *prop = of_find_property(np, propname, NULL);
1136 if (!prop)
1137 return ERR_PTR(-EINVAL);
1138 if (!prop->value)
1139 return ERR_PTR(-ENODATA);
1140 if (len > prop->length)
1141 return ERR_PTR(-EOVERFLOW);
1143 return prop->value;
1147 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1149 * @np: device node from which the property value is to be read.
1150 * @propname: name of the property to be searched.
1151 * @index: index of the u32 in the list of values
1152 * @out_value: pointer to return value, modified only if no error.
1154 * Search for a property in a device node and read nth 32-bit value from
1155 * it. Returns 0 on success, -EINVAL if the property does not exist,
1156 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1157 * property data isn't large enough.
1159 * The out_value is modified only if a valid u32 value can be decoded.
1161 int of_property_read_u32_index(const struct device_node *np,
1162 const char *propname,
1163 u32 index, u32 *out_value)
1165 const u32 *val = of_find_property_value_of_size(np, propname,
1166 ((index + 1) * sizeof(*out_value)));
1168 if (IS_ERR(val))
1169 return PTR_ERR(val);
1171 *out_value = be32_to_cpup(((__be32 *)val) + index);
1172 return 0;
1174 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1177 * of_property_read_u8_array - Find and read an array of u8 from a property.
1179 * @np: device node from which the property value is to be read.
1180 * @propname: name of the property to be searched.
1181 * @out_values: pointer to return value, modified only if return value is 0.
1182 * @sz: number of array elements to read
1184 * Search for a property in a device node and read 8-bit value(s) from
1185 * it. Returns 0 on success, -EINVAL if the property does not exist,
1186 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1187 * property data isn't large enough.
1189 * dts entry of array should be like:
1190 * property = /bits/ 8 <0x50 0x60 0x70>;
1192 * The out_values is modified only if a valid u8 value can be decoded.
1194 int of_property_read_u8_array(const struct device_node *np,
1195 const char *propname, u8 *out_values, size_t sz)
1197 const u8 *val = of_find_property_value_of_size(np, propname,
1198 (sz * sizeof(*out_values)));
1200 if (IS_ERR(val))
1201 return PTR_ERR(val);
1203 while (sz--)
1204 *out_values++ = *val++;
1205 return 0;
1207 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1210 * of_property_read_u16_array - Find and read an array of u16 from a property.
1212 * @np: device node from which the property value is to be read.
1213 * @propname: name of the property to be searched.
1214 * @out_values: pointer to return value, modified only if return value is 0.
1215 * @sz: number of array elements to read
1217 * Search for a property in a device node and read 16-bit value(s) from
1218 * it. Returns 0 on success, -EINVAL if the property does not exist,
1219 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1220 * property data isn't large enough.
1222 * dts entry of array should be like:
1223 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1225 * The out_values is modified only if a valid u16 value can be decoded.
1227 int of_property_read_u16_array(const struct device_node *np,
1228 const char *propname, u16 *out_values, size_t sz)
1230 const __be16 *val = of_find_property_value_of_size(np, propname,
1231 (sz * sizeof(*out_values)));
1233 if (IS_ERR(val))
1234 return PTR_ERR(val);
1236 while (sz--)
1237 *out_values++ = be16_to_cpup(val++);
1238 return 0;
1240 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1243 * of_property_read_u32_array - Find and read an array of 32 bit integers
1244 * from a property.
1246 * @np: device node from which the property value is to be read.
1247 * @propname: name of the property to be searched.
1248 * @out_values: pointer to return value, modified only if return value is 0.
1249 * @sz: number of array elements to read
1251 * Search for a property in a device node and read 32-bit value(s) from
1252 * it. Returns 0 on success, -EINVAL if the property does not exist,
1253 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1254 * property data isn't large enough.
1256 * The out_values is modified only if a valid u32 value can be decoded.
1258 int of_property_read_u32_array(const struct device_node *np,
1259 const char *propname, u32 *out_values,
1260 size_t sz)
1262 const __be32 *val = of_find_property_value_of_size(np, propname,
1263 (sz * sizeof(*out_values)));
1265 if (IS_ERR(val))
1266 return PTR_ERR(val);
1268 while (sz--)
1269 *out_values++ = be32_to_cpup(val++);
1270 return 0;
1272 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1275 * of_property_read_u64 - Find and read a 64 bit integer from a property
1276 * @np: device node from which the property value is to be read.
1277 * @propname: name of the property to be searched.
1278 * @out_value: pointer to return value, modified only if return value is 0.
1280 * Search for a property in a device node and read a 64-bit value from
1281 * it. Returns 0 on success, -EINVAL if the property does not exist,
1282 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1283 * property data isn't large enough.
1285 * The out_value is modified only if a valid u64 value can be decoded.
1287 int of_property_read_u64(const struct device_node *np, const char *propname,
1288 u64 *out_value)
1290 const __be32 *val = of_find_property_value_of_size(np, propname,
1291 sizeof(*out_value));
1293 if (IS_ERR(val))
1294 return PTR_ERR(val);
1296 *out_value = of_read_number(val, 2);
1297 return 0;
1299 EXPORT_SYMBOL_GPL(of_property_read_u64);
1302 * of_property_read_u64_array - Find and read an array of 64 bit integers
1303 * from a property.
1305 * @np: device node from which the property value is to be read.
1306 * @propname: name of the property to be searched.
1307 * @out_values: pointer to return value, modified only if return value is 0.
1308 * @sz: number of array elements to read
1310 * Search for a property in a device node and read 64-bit value(s) from
1311 * it. Returns 0 on success, -EINVAL if the property does not exist,
1312 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1313 * property data isn't large enough.
1315 * The out_values is modified only if a valid u64 value can be decoded.
1317 int of_property_read_u64_array(const struct device_node *np,
1318 const char *propname, u64 *out_values,
1319 size_t sz)
1321 const __be32 *val = of_find_property_value_of_size(np, propname,
1322 (sz * sizeof(*out_values)));
1324 if (IS_ERR(val))
1325 return PTR_ERR(val);
1327 while (sz--) {
1328 *out_values++ = of_read_number(val, 2);
1329 val += 2;
1331 return 0;
1333 EXPORT_SYMBOL_GPL(of_property_read_u64_array);
1336 * of_property_read_string - Find and read a string from a property
1337 * @np: device node from which the property value is to be read.
1338 * @propname: name of the property to be searched.
1339 * @out_string: pointer to null terminated return string, modified only if
1340 * return value is 0.
1342 * Search for a property in a device tree node and retrieve a null
1343 * terminated string value (pointer to data, not a copy). Returns 0 on
1344 * success, -EINVAL if the property does not exist, -ENODATA if property
1345 * does not have a value, and -EILSEQ if the string is not null-terminated
1346 * within the length of the property data.
1348 * The out_string pointer is modified only if a valid string can be decoded.
1350 int of_property_read_string(struct device_node *np, const char *propname,
1351 const char **out_string)
1353 struct property *prop = of_find_property(np, propname, NULL);
1354 if (!prop)
1355 return -EINVAL;
1356 if (!prop->value)
1357 return -ENODATA;
1358 if (strnlen(prop->value, prop->length) >= prop->length)
1359 return -EILSEQ;
1360 *out_string = prop->value;
1361 return 0;
1363 EXPORT_SYMBOL_GPL(of_property_read_string);
1366 * of_property_match_string() - Find string in a list and return index
1367 * @np: pointer to node containing string list property
1368 * @propname: string list property name
1369 * @string: pointer to string to search for in string list
1371 * This function searches a string list property and returns the index
1372 * of a specific string value.
1374 int of_property_match_string(struct device_node *np, const char *propname,
1375 const char *string)
1377 struct property *prop = of_find_property(np, propname, NULL);
1378 size_t l;
1379 int i;
1380 const char *p, *end;
1382 if (!prop)
1383 return -EINVAL;
1384 if (!prop->value)
1385 return -ENODATA;
1387 p = prop->value;
1388 end = p + prop->length;
1390 for (i = 0; p < end; i++, p += l) {
1391 l = strnlen(p, end - p) + 1;
1392 if (p + l > end)
1393 return -EILSEQ;
1394 pr_debug("comparing %s with %s\n", string, p);
1395 if (strcmp(string, p) == 0)
1396 return i; /* Found it; return index */
1398 return -ENODATA;
1400 EXPORT_SYMBOL_GPL(of_property_match_string);
1403 * of_property_read_string_helper() - Utility helper for parsing string properties
1404 * @np: device node from which the property value is to be read.
1405 * @propname: name of the property to be searched.
1406 * @out_strs: output array of string pointers.
1407 * @sz: number of array elements to read.
1408 * @skip: Number of strings to skip over at beginning of list.
1410 * Don't call this function directly. It is a utility helper for the
1411 * of_property_read_string*() family of functions.
1413 int of_property_read_string_helper(struct device_node *np, const char *propname,
1414 const char **out_strs, size_t sz, int skip)
1416 struct property *prop = of_find_property(np, propname, NULL);
1417 int l = 0, i = 0;
1418 const char *p, *end;
1420 if (!prop)
1421 return -EINVAL;
1422 if (!prop->value)
1423 return -ENODATA;
1424 p = prop->value;
1425 end = p + prop->length;
1427 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1428 l = strnlen(p, end - p) + 1;
1429 if (p + l > end)
1430 return -EILSEQ;
1431 if (out_strs && i >= skip)
1432 *out_strs++ = p;
1434 i -= skip;
1435 return i <= 0 ? -ENODATA : i;
1437 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
1439 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1441 int i;
1442 printk("%s %s", msg, of_node_full_name(args->np));
1443 for (i = 0; i < args->args_count; i++)
1444 printk(i ? ",%08x" : ":%08x", args->args[i]);
1445 printk("\n");
1448 static int __of_parse_phandle_with_args(const struct device_node *np,
1449 const char *list_name,
1450 const char *cells_name,
1451 int cell_count, int index,
1452 struct of_phandle_args *out_args)
1454 const __be32 *list, *list_end;
1455 int rc = 0, size, cur_index = 0;
1456 uint32_t count = 0;
1457 struct device_node *node = NULL;
1458 phandle phandle;
1460 /* Retrieve the phandle list property */
1461 list = of_get_property(np, list_name, &size);
1462 if (!list)
1463 return -ENOENT;
1464 list_end = list + size / sizeof(*list);
1466 /* Loop over the phandles until all the requested entry is found */
1467 while (list < list_end) {
1468 rc = -EINVAL;
1469 count = 0;
1472 * If phandle is 0, then it is an empty entry with no
1473 * arguments. Skip forward to the next entry.
1475 phandle = be32_to_cpup(list++);
1476 if (phandle) {
1478 * Find the provider node and parse the #*-cells
1479 * property to determine the argument length.
1481 * This is not needed if the cell count is hard-coded
1482 * (i.e. cells_name not set, but cell_count is set),
1483 * except when we're going to return the found node
1484 * below.
1486 if (cells_name || cur_index == index) {
1487 node = of_find_node_by_phandle(phandle);
1488 if (!node) {
1489 pr_err("%s: could not find phandle\n",
1490 np->full_name);
1491 goto err;
1495 if (cells_name) {
1496 if (of_property_read_u32(node, cells_name,
1497 &count)) {
1498 pr_err("%s: could not get %s for %s\n",
1499 np->full_name, cells_name,
1500 node->full_name);
1501 goto err;
1503 } else {
1504 count = cell_count;
1508 * Make sure that the arguments actually fit in the
1509 * remaining property data length
1511 if (list + count > list_end) {
1512 pr_err("%s: arguments longer than property\n",
1513 np->full_name);
1514 goto err;
1519 * All of the error cases above bail out of the loop, so at
1520 * this point, the parsing is successful. If the requested
1521 * index matches, then fill the out_args structure and return,
1522 * or return -ENOENT for an empty entry.
1524 rc = -ENOENT;
1525 if (cur_index == index) {
1526 if (!phandle)
1527 goto err;
1529 if (out_args) {
1530 int i;
1531 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1532 count = MAX_PHANDLE_ARGS;
1533 out_args->np = node;
1534 out_args->args_count = count;
1535 for (i = 0; i < count; i++)
1536 out_args->args[i] = be32_to_cpup(list++);
1537 } else {
1538 of_node_put(node);
1541 /* Found it! return success */
1542 return 0;
1545 of_node_put(node);
1546 node = NULL;
1547 list += count;
1548 cur_index++;
1552 * Unlock node before returning result; will be one of:
1553 * -ENOENT : index is for empty phandle
1554 * -EINVAL : parsing error on data
1555 * [1..n] : Number of phandle (count mode; when index = -1)
1557 rc = index < 0 ? cur_index : -ENOENT;
1558 err:
1559 if (node)
1560 of_node_put(node);
1561 return rc;
1565 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1566 * @np: Pointer to device node holding phandle property
1567 * @phandle_name: Name of property holding a phandle value
1568 * @index: For properties holding a table of phandles, this is the index into
1569 * the table
1571 * Returns the device_node pointer with refcount incremented. Use
1572 * of_node_put() on it when done.
1574 struct device_node *of_parse_phandle(const struct device_node *np,
1575 const char *phandle_name, int index)
1577 struct of_phandle_args args;
1579 if (index < 0)
1580 return NULL;
1582 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1583 index, &args))
1584 return NULL;
1586 return args.np;
1588 EXPORT_SYMBOL(of_parse_phandle);
1591 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1592 * @np: pointer to a device tree node containing a list
1593 * @list_name: property name that contains a list
1594 * @cells_name: property name that specifies phandles' arguments count
1595 * @index: index of a phandle to parse out
1596 * @out_args: optional pointer to output arguments structure (will be filled)
1598 * This function is useful to parse lists of phandles and their arguments.
1599 * Returns 0 on success and fills out_args, on error returns appropriate
1600 * errno value.
1602 * Caller is responsible to call of_node_put() on the returned out_args->np
1603 * pointer.
1605 * Example:
1607 * phandle1: node1 {
1608 * #list-cells = <2>;
1611 * phandle2: node2 {
1612 * #list-cells = <1>;
1615 * node3 {
1616 * list = <&phandle1 1 2 &phandle2 3>;
1619 * To get a device_node of the `node2' node you may call this:
1620 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1622 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1623 const char *cells_name, int index,
1624 struct of_phandle_args *out_args)
1626 if (index < 0)
1627 return -EINVAL;
1628 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1629 index, out_args);
1631 EXPORT_SYMBOL(of_parse_phandle_with_args);
1634 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1635 * @np: pointer to a device tree node containing a list
1636 * @list_name: property name that contains a list
1637 * @cell_count: number of argument cells following the phandle
1638 * @index: index of a phandle to parse out
1639 * @out_args: optional pointer to output arguments structure (will be filled)
1641 * This function is useful to parse lists of phandles and their arguments.
1642 * Returns 0 on success and fills out_args, on error returns appropriate
1643 * errno value.
1645 * Caller is responsible to call of_node_put() on the returned out_args->np
1646 * pointer.
1648 * Example:
1650 * phandle1: node1 {
1653 * phandle2: node2 {
1656 * node3 {
1657 * list = <&phandle1 0 2 &phandle2 2 3>;
1660 * To get a device_node of the `node2' node you may call this:
1661 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1663 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1664 const char *list_name, int cell_count,
1665 int index, struct of_phandle_args *out_args)
1667 if (index < 0)
1668 return -EINVAL;
1669 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1670 index, out_args);
1672 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1675 * of_count_phandle_with_args() - Find the number of phandles references in a property
1676 * @np: pointer to a device tree node containing a list
1677 * @list_name: property name that contains a list
1678 * @cells_name: property name that specifies phandles' arguments count
1680 * Returns the number of phandle + argument tuples within a property. It
1681 * is a typical pattern to encode a list of phandle and variable
1682 * arguments into a single property. The number of arguments is encoded
1683 * by a property in the phandle-target node. For example, a gpios
1684 * property would contain a list of GPIO specifies consisting of a
1685 * phandle and 1 or more arguments. The number of arguments are
1686 * determined by the #gpio-cells property in the node pointed to by the
1687 * phandle.
1689 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1690 const char *cells_name)
1692 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1693 NULL);
1695 EXPORT_SYMBOL(of_count_phandle_with_args);
1698 * __of_add_property - Add a property to a node without lock operations
1700 int __of_add_property(struct device_node *np, struct property *prop)
1702 struct property **next;
1704 prop->next = NULL;
1705 next = &np->properties;
1706 while (*next) {
1707 if (strcmp(prop->name, (*next)->name) == 0)
1708 /* duplicate ! don't insert it */
1709 return -EEXIST;
1711 next = &(*next)->next;
1713 *next = prop;
1715 return 0;
1719 * of_add_property - Add a property to a node
1721 int of_add_property(struct device_node *np, struct property *prop)
1723 unsigned long flags;
1724 int rc;
1726 mutex_lock(&of_mutex);
1728 raw_spin_lock_irqsave(&devtree_lock, flags);
1729 rc = __of_add_property(np, prop);
1730 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1732 if (!rc)
1733 __of_add_property_sysfs(np, prop);
1735 mutex_unlock(&of_mutex);
1737 if (!rc)
1738 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1740 return rc;
1743 int __of_remove_property(struct device_node *np, struct property *prop)
1745 struct property **next;
1747 for (next = &np->properties; *next; next = &(*next)->next) {
1748 if (*next == prop)
1749 break;
1751 if (*next == NULL)
1752 return -ENODEV;
1754 /* found the node */
1755 *next = prop->next;
1756 prop->next = np->deadprops;
1757 np->deadprops = prop;
1759 return 0;
1762 void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
1764 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1765 kfree(prop->attr.attr.name);
1768 void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1770 if (!IS_ENABLED(CONFIG_SYSFS))
1771 return;
1773 /* at early boot, bail here and defer setup to of_init() */
1774 if (of_kset && of_node_is_attached(np))
1775 __of_sysfs_remove_bin_file(np, prop);
1779 * of_remove_property - Remove a property from a node.
1781 * Note that we don't actually remove it, since we have given out
1782 * who-knows-how-many pointers to the data using get-property.
1783 * Instead we just move the property to the "dead properties"
1784 * list, so it won't be found any more.
1786 int of_remove_property(struct device_node *np, struct property *prop)
1788 unsigned long flags;
1789 int rc;
1791 mutex_lock(&of_mutex);
1793 raw_spin_lock_irqsave(&devtree_lock, flags);
1794 rc = __of_remove_property(np, prop);
1795 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1797 if (!rc)
1798 __of_remove_property_sysfs(np, prop);
1800 mutex_unlock(&of_mutex);
1802 if (!rc)
1803 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1805 return rc;
1808 int __of_update_property(struct device_node *np, struct property *newprop,
1809 struct property **oldpropp)
1811 struct property **next, *oldprop;
1813 for (next = &np->properties; *next; next = &(*next)->next) {
1814 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1815 break;
1817 *oldpropp = oldprop = *next;
1819 if (oldprop) {
1820 /* replace the node */
1821 newprop->next = oldprop->next;
1822 *next = newprop;
1823 oldprop->next = np->deadprops;
1824 np->deadprops = oldprop;
1825 } else {
1826 /* new node */
1827 newprop->next = NULL;
1828 *next = newprop;
1831 return 0;
1834 void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1835 struct property *oldprop)
1837 if (!IS_ENABLED(CONFIG_SYSFS))
1838 return;
1840 /* At early boot, bail out and defer setup to of_init() */
1841 if (!of_kset)
1842 return;
1844 if (oldprop)
1845 __of_sysfs_remove_bin_file(np, oldprop);
1846 __of_add_property_sysfs(np, newprop);
1850 * of_update_property - Update a property in a node, if the property does
1851 * not exist, add it.
1853 * Note that we don't actually remove it, since we have given out
1854 * who-knows-how-many pointers to the data using get-property.
1855 * Instead we just move the property to the "dead properties" list,
1856 * and add the new property to the property list
1858 int of_update_property(struct device_node *np, struct property *newprop)
1860 struct property *oldprop;
1861 unsigned long flags;
1862 int rc;
1864 if (!newprop->name)
1865 return -EINVAL;
1867 mutex_lock(&of_mutex);
1869 raw_spin_lock_irqsave(&devtree_lock, flags);
1870 rc = __of_update_property(np, newprop, &oldprop);
1871 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1873 if (!rc)
1874 __of_update_property_sysfs(np, newprop, oldprop);
1876 mutex_unlock(&of_mutex);
1878 if (!rc)
1879 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1881 return rc;
1884 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1885 int id, const char *stem, int stem_len)
1887 ap->np = np;
1888 ap->id = id;
1889 strncpy(ap->stem, stem, stem_len);
1890 ap->stem[stem_len] = 0;
1891 list_add_tail(&ap->link, &aliases_lookup);
1892 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1893 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1897 * of_alias_scan - Scan all properties of the 'aliases' node
1899 * The function scans all the properties of the 'aliases' node and populates
1900 * the global lookup table with the properties. It returns the
1901 * number of alias properties found, or an error code in case of failure.
1903 * @dt_alloc: An allocator that provides a virtual address to memory
1904 * for storing the resulting tree
1906 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1908 struct property *pp;
1910 of_aliases = of_find_node_by_path("/aliases");
1911 of_chosen = of_find_node_by_path("/chosen");
1912 if (of_chosen == NULL)
1913 of_chosen = of_find_node_by_path("/chosen@0");
1915 if (of_chosen) {
1916 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
1917 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
1918 if (!name)
1919 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1920 if (IS_ENABLED(CONFIG_PPC) && !name)
1921 name = of_get_property(of_aliases, "stdout", NULL);
1922 if (name)
1923 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
1926 if (!of_aliases)
1927 return;
1929 for_each_property_of_node(of_aliases, pp) {
1930 const char *start = pp->name;
1931 const char *end = start + strlen(start);
1932 struct device_node *np;
1933 struct alias_prop *ap;
1934 int id, len;
1936 /* Skip those we do not want to proceed */
1937 if (!strcmp(pp->name, "name") ||
1938 !strcmp(pp->name, "phandle") ||
1939 !strcmp(pp->name, "linux,phandle"))
1940 continue;
1942 np = of_find_node_by_path(pp->value);
1943 if (!np)
1944 continue;
1946 /* walk the alias backwards to extract the id and work out
1947 * the 'stem' string */
1948 while (isdigit(*(end-1)) && end > start)
1949 end--;
1950 len = end - start;
1952 if (kstrtoint(end, 10, &id) < 0)
1953 continue;
1955 /* Allocate an alias_prop with enough space for the stem */
1956 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1957 if (!ap)
1958 continue;
1959 memset(ap, 0, sizeof(*ap) + len + 1);
1960 ap->alias = start;
1961 of_alias_add(ap, np, id, start, len);
1966 * of_alias_get_id - Get alias id for the given device_node
1967 * @np: Pointer to the given device_node
1968 * @stem: Alias stem of the given device_node
1970 * The function travels the lookup table to get the alias id for the given
1971 * device_node and alias stem. It returns the alias id if found.
1973 int of_alias_get_id(struct device_node *np, const char *stem)
1975 struct alias_prop *app;
1976 int id = -ENODEV;
1978 mutex_lock(&of_mutex);
1979 list_for_each_entry(app, &aliases_lookup, link) {
1980 if (strcmp(app->stem, stem) != 0)
1981 continue;
1983 if (np == app->np) {
1984 id = app->id;
1985 break;
1988 mutex_unlock(&of_mutex);
1990 return id;
1992 EXPORT_SYMBOL_GPL(of_alias_get_id);
1995 * of_alias_get_highest_id - Get highest alias id for the given stem
1996 * @stem: Alias stem to be examined
1998 * The function travels the lookup table to get the highest alias id for the
1999 * given alias stem. It returns the alias id if found.
2001 int of_alias_get_highest_id(const char *stem)
2003 struct alias_prop *app;
2004 int id = -ENODEV;
2006 mutex_lock(&of_mutex);
2007 list_for_each_entry(app, &aliases_lookup, link) {
2008 if (strcmp(app->stem, stem) != 0)
2009 continue;
2011 if (app->id > id)
2012 id = app->id;
2014 mutex_unlock(&of_mutex);
2016 return id;
2018 EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
2020 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2021 u32 *pu)
2023 const void *curv = cur;
2025 if (!prop)
2026 return NULL;
2028 if (!cur) {
2029 curv = prop->value;
2030 goto out_val;
2033 curv += sizeof(*cur);
2034 if (curv >= prop->value + prop->length)
2035 return NULL;
2037 out_val:
2038 *pu = be32_to_cpup(curv);
2039 return curv;
2041 EXPORT_SYMBOL_GPL(of_prop_next_u32);
2043 const char *of_prop_next_string(struct property *prop, const char *cur)
2045 const void *curv = cur;
2047 if (!prop)
2048 return NULL;
2050 if (!cur)
2051 return prop->value;
2053 curv += strlen(cur) + 1;
2054 if (curv >= prop->value + prop->length)
2055 return NULL;
2057 return curv;
2059 EXPORT_SYMBOL_GPL(of_prop_next_string);
2062 * of_console_check() - Test and setup console for DT setup
2063 * @dn - Pointer to device node
2064 * @name - Name to use for preferred console without index. ex. "ttyS"
2065 * @index - Index to use for preferred console.
2067 * Check if the given device node matches the stdout-path property in the
2068 * /chosen node. If it does then register it as the preferred console and return
2069 * TRUE. Otherwise return FALSE.
2071 bool of_console_check(struct device_node *dn, char *name, int index)
2073 if (!dn || dn != of_stdout || console_set_on_cmdline)
2074 return false;
2075 return !add_preferred_console(name, index,
2076 kstrdup(of_stdout_options, GFP_KERNEL));
2078 EXPORT_SYMBOL_GPL(of_console_check);
2081 * of_find_next_cache_node - Find a node's subsidiary cache
2082 * @np: node of type "cpu" or "cache"
2084 * Returns a node pointer with refcount incremented, use
2085 * of_node_put() on it when done. Caller should hold a reference
2086 * to np.
2088 struct device_node *of_find_next_cache_node(const struct device_node *np)
2090 struct device_node *child;
2091 const phandle *handle;
2093 handle = of_get_property(np, "l2-cache", NULL);
2094 if (!handle)
2095 handle = of_get_property(np, "next-level-cache", NULL);
2097 if (handle)
2098 return of_find_node_by_phandle(be32_to_cpup(handle));
2100 /* OF on pmac has nodes instead of properties named "l2-cache"
2101 * beneath CPU nodes.
2103 if (!strcmp(np->type, "cpu"))
2104 for_each_child_of_node(np, child)
2105 if (!strcmp(child->type, "cache"))
2106 return child;
2108 return NULL;
2112 * of_graph_parse_endpoint() - parse common endpoint node properties
2113 * @node: pointer to endpoint device_node
2114 * @endpoint: pointer to the OF endpoint data structure
2116 * The caller should hold a reference to @node.
2118 int of_graph_parse_endpoint(const struct device_node *node,
2119 struct of_endpoint *endpoint)
2121 struct device_node *port_node = of_get_parent(node);
2123 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2124 __func__, node->full_name);
2126 memset(endpoint, 0, sizeof(*endpoint));
2128 endpoint->local_node = node;
2130 * It doesn't matter whether the two calls below succeed.
2131 * If they don't then the default value 0 is used.
2133 of_property_read_u32(port_node, "reg", &endpoint->port);
2134 of_property_read_u32(node, "reg", &endpoint->id);
2136 of_node_put(port_node);
2138 return 0;
2140 EXPORT_SYMBOL(of_graph_parse_endpoint);
2143 * of_graph_get_port_by_id() - get the port matching a given id
2144 * @parent: pointer to the parent device node
2145 * @id: id of the port
2147 * Return: A 'port' node pointer with refcount incremented. The caller
2148 * has to use of_node_put() on it when done.
2150 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
2152 struct device_node *node, *port;
2154 node = of_get_child_by_name(parent, "ports");
2155 if (node)
2156 parent = node;
2158 for_each_child_of_node(parent, port) {
2159 u32 port_id = 0;
2161 if (of_node_cmp(port->name, "port") != 0)
2162 continue;
2163 of_property_read_u32(port, "reg", &port_id);
2164 if (id == port_id)
2165 break;
2168 of_node_put(node);
2170 return port;
2172 EXPORT_SYMBOL(of_graph_get_port_by_id);
2175 * of_graph_get_next_endpoint() - get next endpoint node
2176 * @parent: pointer to the parent device node
2177 * @prev: previous endpoint node, or NULL to get first
2179 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2180 * of the passed @prev node is decremented.
2182 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2183 struct device_node *prev)
2185 struct device_node *endpoint;
2186 struct device_node *port;
2188 if (!parent)
2189 return NULL;
2192 * Start by locating the port node. If no previous endpoint is specified
2193 * search for the first port node, otherwise get the previous endpoint
2194 * parent port node.
2196 if (!prev) {
2197 struct device_node *node;
2199 node = of_get_child_by_name(parent, "ports");
2200 if (node)
2201 parent = node;
2203 port = of_get_child_by_name(parent, "port");
2204 of_node_put(node);
2206 if (!port) {
2207 pr_err("%s(): no port node found in %s\n",
2208 __func__, parent->full_name);
2209 return NULL;
2211 } else {
2212 port = of_get_parent(prev);
2213 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2214 __func__, prev->full_name))
2215 return NULL;
2218 while (1) {
2220 * Now that we have a port node, get the next endpoint by
2221 * getting the next child. If the previous endpoint is NULL this
2222 * will return the first child.
2224 endpoint = of_get_next_child(port, prev);
2225 if (endpoint) {
2226 of_node_put(port);
2227 return endpoint;
2230 /* No more endpoints under this port, try the next one. */
2231 prev = NULL;
2233 do {
2234 port = of_get_next_child(parent, port);
2235 if (!port)
2236 return NULL;
2237 } while (of_node_cmp(port->name, "port"));
2240 EXPORT_SYMBOL(of_graph_get_next_endpoint);
2243 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
2244 * @parent: pointer to the parent device node
2245 * @port_reg: identifier (value of reg property) of the parent port node
2246 * @reg: identifier (value of reg property) of the endpoint node
2248 * Return: An 'endpoint' node pointer which is identified by reg and at the same
2249 * is the child of a port node identified by port_reg. reg and port_reg are
2250 * ignored when they are -1.
2252 struct device_node *of_graph_get_endpoint_by_regs(
2253 const struct device_node *parent, int port_reg, int reg)
2255 struct of_endpoint endpoint;
2256 struct device_node *node = NULL;
2258 for_each_endpoint_of_node(parent, node) {
2259 of_graph_parse_endpoint(node, &endpoint);
2260 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
2261 ((reg == -1) || (endpoint.id == reg)))
2262 return node;
2265 return NULL;
2267 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
2270 * of_graph_get_remote_port_parent() - get remote port's parent node
2271 * @node: pointer to a local endpoint device_node
2273 * Return: Remote device node associated with remote endpoint node linked
2274 * to @node. Use of_node_put() on it when done.
2276 struct device_node *of_graph_get_remote_port_parent(
2277 const struct device_node *node)
2279 struct device_node *np;
2280 unsigned int depth;
2282 /* Get remote endpoint node. */
2283 np = of_parse_phandle(node, "remote-endpoint", 0);
2285 /* Walk 3 levels up only if there is 'ports' node. */
2286 for (depth = 3; depth && np; depth--) {
2287 np = of_get_next_parent(np);
2288 if (depth == 2 && of_node_cmp(np->name, "ports"))
2289 break;
2291 return np;
2293 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2296 * of_graph_get_remote_port() - get remote port node
2297 * @node: pointer to a local endpoint device_node
2299 * Return: Remote port node associated with remote endpoint node linked
2300 * to @node. Use of_node_put() on it when done.
2302 struct device_node *of_graph_get_remote_port(const struct device_node *node)
2304 struct device_node *np;
2306 /* Get remote endpoint node. */
2307 np = of_parse_phandle(node, "remote-endpoint", 0);
2308 if (!np)
2309 return NULL;
2310 return of_get_next_parent(np);
2312 EXPORT_SYMBOL(of_graph_get_remote_port);