1 // SPDX-License-Identifier: GPL-2.0+
3 * Originally from Linux v4.9
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
16 * Copyright (c) 2017 Google, Inc
18 * This file follows drivers/of/base.c with functions in the same order as the
25 #include <linux/bug.h>
26 #include <linux/libfdt.h>
27 #include <dm/of_access.h>
28 #include <linux/ctype.h>
29 #include <linux/err.h>
30 #include <linux/ioport.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 /* list of struct alias_prop aliases */
35 LIST_HEAD(aliases_lookup);
38 static struct device_node *of_aliases;
41 static struct device_node *of_chosen;
43 /* node pointed to by the stdout-path alias */
44 static struct device_node *of_stdout;
46 /* pointer to options given after the alias (separated by :) or NULL if none */
47 static const char *of_stdout_options;
50 * struct alias_prop - Alias property in 'aliases' node
52 * The structure represents one alias property of 'aliases' node as
53 * an entry in aliases_lookup list.
55 * @link: List node to link the structure in aliases_lookup list
56 * @alias: Alias property name
57 * @np: Pointer to device_node that the alias stands for
58 * @id: Index value from end of alias name
59 * @stem: Alias string without the index
62 struct list_head link;
64 struct device_node *np;
69 int of_n_addr_cells(const struct device_node *np)
76 ip = of_get_property(np, "#address-cells", NULL);
78 return be32_to_cpup(ip);
81 /* No #address-cells property for the root node */
82 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
85 int of_n_size_cells(const struct device_node *np)
92 ip = of_get_property(np, "#size-cells", NULL);
94 return be32_to_cpup(ip);
97 /* No #size-cells property for the root node */
98 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
101 int of_simple_addr_cells(const struct device_node *np)
105 ip = of_get_property(np, "#address-cells", NULL);
107 return be32_to_cpup(ip);
109 /* Return a default of 2 to match fdt_address_cells()*/
113 int of_simple_size_cells(const struct device_node *np)
117 ip = of_get_property(np, "#size-cells", NULL);
119 return be32_to_cpup(ip);
121 /* Return a default of 2 to match fdt_size_cells()*/
125 struct property *of_find_property(const struct device_node *np,
126 const char *name, int *lenp)
133 for (pp = np->properties; pp; pp = pp->next) {
134 if (strcmp(pp->name, name) == 0) {
141 *lenp = -FDT_ERR_NOTFOUND;
146 struct device_node *of_find_all_nodes(struct device_node *prev)
148 struct device_node *np;
152 } else if (prev->child) {
156 * Walk back up looking for a sibling, or the end of the
160 while (np->parent && !np->sibling)
162 np = np->sibling; /* Might be null at the end of the tree */
168 const void *of_get_property(const struct device_node *np, const char *name,
171 struct property *pp = of_find_property(np, name, lenp);
173 return pp ? pp->value : NULL;
176 const struct property *of_get_first_property(const struct device_node *np)
181 return np->properties;
184 const struct property *of_get_next_property(const struct device_node *np,
185 const struct property *property)
190 return property->next;
193 const void *of_get_property_by_prop(const struct device_node *np,
194 const struct property *property,
198 if (!np || !property)
201 *name = property->name;
203 *lenp = property->length;
205 return property->value;
208 static const char *of_prop_next_string(struct property *prop, const char *cur)
210 const void *curv = cur;
218 curv += strlen(cur) + 1;
219 if (curv >= prop->value + prop->length)
225 int of_device_is_compatible(const struct device_node *device,
226 const char *compat, const char *type,
229 struct property *prop;
231 int index = 0, score = 0;
233 /* Compatible match has highest priority */
234 if (compat && compat[0]) {
235 prop = of_find_property(device, "compatible", NULL);
236 for (cp = of_prop_next_string(prop, NULL); cp;
237 cp = of_prop_next_string(prop, cp), index++) {
238 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
239 score = INT_MAX/2 - (index << 2);
247 /* Matching type is better than matching name */
248 if (type && type[0]) {
249 if (!device->type || of_node_cmp(type, device->type))
254 /* Matching name is a bit better than not */
255 if (name && name[0]) {
256 if (!device->name || of_node_cmp(name, device->name))
264 bool of_device_is_available(const struct device_node *device)
272 status = of_get_property(device, "status", &statlen);
277 if (!strcmp(status, "okay"))
284 struct device_node *of_get_parent(const struct device_node *node)
286 const struct device_node *np;
291 np = of_node_get(node->parent);
293 return (struct device_node *)np;
296 static struct device_node *__of_get_next_child(const struct device_node *node,
297 struct device_node *prev)
299 struct device_node *next;
304 next = prev ? prev->sibling : node->child;
306 * coverity[dead_error_line : FALSE]
307 * Dead code here since our current implementation of of_node_get()
308 * always returns NULL (Coverity CID 163245). But we leave it as is
309 * since we may want to implement get/put later.
311 for (; next; next = next->sibling)
312 if (of_node_get(next))
318 #define __for_each_child_of_node(parent, child) \
319 for (child = __of_get_next_child(parent, NULL); child != NULL; \
320 child = __of_get_next_child(parent, child))
322 static struct device_node *__of_find_node_by_path(struct device_node *parent,
325 struct device_node *child;
328 len = strcspn(path, "/:");
332 __for_each_child_of_node(parent, child) {
333 const char *name = strrchr(child->full_name, '/');
336 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
342 #define for_each_property_of_node(dn, pp) \
343 for (pp = dn->properties; pp != NULL; pp = pp->next)
345 struct device_node *of_find_node_opts_by_path(const char *path,
348 struct device_node *np = NULL;
350 const char *separator = strchr(path, ':');
353 *opts = separator ? separator + 1 : NULL;
355 if (strcmp(path, "/") == 0)
356 return of_node_get(gd->of_root);
358 /* The path could begin with an alias */
361 const char *p = separator;
364 p = strchrnul(path, '/');
367 /* of_aliases must not be NULL */
371 for_each_property_of_node(of_aliases, pp) {
372 if (strlen(pp->name) == len && !strncmp(pp->name, path,
374 np = of_find_node_by_path(pp->value);
383 /* Step down the tree matching path components */
385 np = of_node_get(gd->of_root);
386 while (np && *path == '/') {
387 struct device_node *tmp = np;
389 path++; /* Increment past '/' delimiter */
390 np = __of_find_node_by_path(np, path);
392 path = strchrnul(path, '/');
393 if (separator && separator < path)
400 struct device_node *of_find_compatible_node(struct device_node *from,
401 const char *type, const char *compatible)
403 struct device_node *np;
405 for_each_of_allnodes_from(from, np)
406 if (of_device_is_compatible(np, compatible, type, NULL) &&
414 static int of_device_has_prop_value(const struct device_node *device,
415 const char *propname, const void *propval,
418 struct property *prop = of_find_property(device, propname, NULL);
420 if (!prop || !prop->value || prop->length != proplen)
422 return !memcmp(prop->value, propval, proplen);
425 struct device_node *of_find_node_by_prop_value(struct device_node *from,
426 const char *propname,
427 const void *propval, int proplen)
429 struct device_node *np;
431 for_each_of_allnodes_from(from, np) {
432 if (of_device_has_prop_value(np, propname, propval, proplen) &&
441 struct device_node *of_find_node_by_phandle(phandle handle)
443 struct device_node *np;
448 for_each_of_allnodes(np)
449 if (np->phandle == handle)
451 (void)of_node_get(np);
457 * of_find_property_value_of_size() - find property of given size
459 * Search for a property in a device node and validate the requested size.
461 * @np: device node from which the property value is to be read.
462 * @propname: name of the property to be searched.
463 * @len: requested length of property value
465 * @return the property value on success, -EINVAL if the property does not
466 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
467 * property data isn't large enough.
469 static void *of_find_property_value_of_size(const struct device_node *np,
470 const char *propname, u32 len)
472 struct property *prop = of_find_property(np, propname, NULL);
475 return ERR_PTR(-EINVAL);
477 return ERR_PTR(-ENODATA);
478 if (len > prop->length)
479 return ERR_PTR(-EOVERFLOW);
484 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
486 return of_read_u32_index(np, propname, 0, outp);
489 int of_read_u32_array(const struct device_node *np, const char *propname,
490 u32 *out_values, size_t sz)
494 debug("%s: %s: ", __func__, propname);
495 val = of_find_property_value_of_size(np, propname,
496 sz * sizeof(*out_values));
501 debug("size %zd\n", sz);
503 *out_values++ = be32_to_cpup(val++);
508 int of_read_u32_index(const struct device_node *np, const char *propname,
509 int index, u32 *outp)
513 debug("%s: %s: ", __func__, propname);
517 val = of_find_property_value_of_size(np, propname,
518 sizeof(*outp) * (index + 1));
520 debug("(not found)\n");
524 *outp = be32_to_cpup(val + index);
525 debug("%#x (%d)\n", *outp, *outp);
530 int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
534 debug("%s: %s: ", __func__, propname);
537 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
539 debug("(not found)\n");
543 *outp = be64_to_cpup(val);
544 debug("%#llx (%lld)\n", (unsigned long long)*outp,
545 (unsigned long long)*outp);
550 int of_property_match_string(const struct device_node *np, const char *propname,
553 const struct property *prop = of_find_property(np, propname, NULL);
564 end = p + prop->length;
566 for (i = 0; p < end; i++, p += l) {
567 l = strnlen(p, end - p) + 1;
570 debug("comparing %s with %s\n", string, p);
571 if (strcmp(string, p) == 0)
572 return i; /* Found it; return index */
578 * of_property_read_string_helper() - Utility helper for parsing string properties
579 * @np: device node from which the property value is to be read.
580 * @propname: name of the property to be searched.
581 * @out_strs: output array of string pointers.
582 * @sz: number of array elements to read.
583 * @skip: Number of strings to skip over at beginning of list.
585 * Don't call this function directly. It is a utility helper for the
586 * of_property_read_string*() family of functions.
588 int of_property_read_string_helper(const struct device_node *np,
589 const char *propname, const char **out_strs,
592 const struct property *prop = of_find_property(np, propname, NULL);
601 end = p + prop->length;
603 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
604 l = strnlen(p, end - p) + 1;
607 if (out_strs && i >= skip)
611 return i <= 0 ? -ENODATA : i;
614 static int __of_parse_phandle_with_args(const struct device_node *np,
615 const char *list_name,
616 const char *cells_name,
617 int cell_count, int index,
618 struct of_phandle_args *out_args)
620 const __be32 *list, *list_end;
621 int rc = 0, cur_index = 0;
623 struct device_node *node = NULL;
627 /* Retrieve the phandle list property */
628 list = of_get_property(np, list_name, &size);
631 list_end = list + size / sizeof(*list);
633 /* Loop over the phandles until all the requested entry is found */
634 while (list < list_end) {
639 * If phandle is 0, then it is an empty entry with no
640 * arguments. Skip forward to the next entry.
642 phandle = be32_to_cpup(list++);
645 * Find the provider node and parse the #*-cells
646 * property to determine the argument length.
648 * This is not needed if the cell count is hard-coded
649 * (i.e. cells_name not set, but cell_count is set),
650 * except when we're going to return the found node
653 if (cells_name || cur_index == index) {
654 node = of_find_node_by_phandle(phandle);
656 debug("%s: could not find phandle\n",
663 if (of_read_u32(node, cells_name, &count)) {
664 debug("%s: could not get %s for %s\n",
665 np->full_name, cells_name,
674 * Make sure that the arguments actually fit in the
675 * remaining property data length
677 if (list + count > list_end) {
678 debug("%s: arguments longer than property\n",
685 * All of the error cases above bail out of the loop, so at
686 * this point, the parsing is successful. If the requested
687 * index matches, then fill the out_args structure and return,
688 * or return -ENOENT for an empty entry.
691 if (cur_index == index) {
697 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
698 count = OF_MAX_PHANDLE_ARGS;
700 out_args->args_count = count;
701 for (i = 0; i < count; i++)
703 be32_to_cpup(list++);
708 /* Found it! return success */
719 * Unlock node before returning result; will be one of:
720 * -ENOENT : index is for empty phandle
721 * -EINVAL : parsing error on data
722 * [1..n] : Number of phandle (count mode; when index = -1)
724 rc = index < 0 ? cur_index : -ENOENT;
731 struct device_node *of_parse_phandle(const struct device_node *np,
732 const char *phandle_name, int index)
734 struct of_phandle_args args;
739 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
746 int of_parse_phandle_with_args(const struct device_node *np,
747 const char *list_name, const char *cells_name,
748 int index, struct of_phandle_args *out_args)
753 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
757 int of_count_phandle_with_args(const struct device_node *np,
758 const char *list_name, const char *cells_name)
760 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
764 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
765 int id, const char *stem, int stem_len)
769 strncpy(ap->stem, stem, stem_len);
770 ap->stem[stem_len] = 0;
771 list_add_tail(&ap->link, &aliases_lookup);
772 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
773 ap->alias, ap->stem, ap->id, of_node_full_name(np));
776 int of_alias_scan(void)
780 of_aliases = of_find_node_by_path("/aliases");
781 of_chosen = of_find_node_by_path("/chosen");
782 if (of_chosen == NULL)
783 of_chosen = of_find_node_by_path("/chosen@0");
788 name = of_get_property(of_chosen, "stdout-path", NULL);
790 of_stdout = of_find_node_opts_by_path(name,
797 for_each_property_of_node(of_aliases, pp) {
798 const char *start = pp->name;
799 const char *end = start + strlen(start);
800 struct device_node *np;
801 struct alias_prop *ap;
805 /* Skip those we do not want to proceed */
806 if (!strcmp(pp->name, "name") ||
807 !strcmp(pp->name, "phandle") ||
808 !strcmp(pp->name, "linux,phandle"))
811 np = of_find_node_by_path(pp->value);
816 * walk the alias backwards to extract the id and work out
819 while (isdigit(*(end-1)) && end > start)
823 if (strict_strtoul(end, 10, &id) < 0)
826 /* Allocate an alias_prop with enough space for the stem */
827 ap = malloc(sizeof(*ap) + len + 1);
830 memset(ap, 0, sizeof(*ap) + len + 1);
832 of_alias_add(ap, np, id, start, len);
838 int of_alias_get_id(const struct device_node *np, const char *stem)
840 struct alias_prop *app;
843 mutex_lock(&of_mutex);
844 list_for_each_entry(app, &aliases_lookup, link) {
845 if (strcmp(app->stem, stem) != 0)
853 mutex_unlock(&of_mutex);
858 int of_alias_get_highest_id(const char *stem)
860 struct alias_prop *app;
863 mutex_lock(&of_mutex);
864 list_for_each_entry(app, &aliases_lookup, link) {
865 if (strcmp(app->stem, stem) != 0)
871 mutex_unlock(&of_mutex);
876 struct device_node *of_get_stdout(void)