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/libfdt.h>
26 #include <dm/of_access.h>
27 #include <linux/ctype.h>
28 #include <linux/err.h>
29 #include <linux/ioport.h>
31 DECLARE_GLOBAL_DATA_PTR;
33 /* list of struct alias_prop aliases */
34 LIST_HEAD(aliases_lookup);
37 static struct device_node *of_aliases;
40 static struct device_node *of_chosen;
42 /* node pointed to by the stdout-path alias */
43 static struct device_node *of_stdout;
45 /* pointer to options given after the alias (separated by :) or NULL if none */
46 static const char *of_stdout_options;
49 * struct alias_prop - Alias property in 'aliases' node
51 * The structure represents one alias property of 'aliases' node as
52 * an entry in aliases_lookup list.
54 * @link: List node to link the structure in aliases_lookup list
55 * @alias: Alias property name
56 * @np: Pointer to device_node that the alias stands for
57 * @id: Index value from end of alias name
58 * @stem: Alias string without the index
61 struct list_head link;
63 struct device_node *np;
68 int of_n_addr_cells(const struct device_node *np)
75 ip = of_get_property(np, "#address-cells", NULL);
77 return be32_to_cpup(ip);
80 /* No #address-cells property for the root node */
81 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
84 int of_n_size_cells(const struct device_node *np)
91 ip = of_get_property(np, "#size-cells", NULL);
93 return be32_to_cpup(ip);
96 /* No #size-cells property for the root node */
97 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
100 int of_simple_addr_cells(const struct device_node *np)
104 ip = of_get_property(np, "#address-cells", NULL);
106 return be32_to_cpup(ip);
108 /* Return a default of 2 to match fdt_address_cells()*/
112 int of_simple_size_cells(const struct device_node *np)
116 ip = of_get_property(np, "#size-cells", NULL);
118 return be32_to_cpup(ip);
120 /* Return a default of 2 to match fdt_size_cells()*/
124 struct property *of_find_property(const struct device_node *np,
125 const char *name, int *lenp)
132 for (pp = np->properties; pp; pp = pp->next) {
133 if (strcmp(pp->name, name) == 0) {
140 *lenp = -FDT_ERR_NOTFOUND;
145 struct device_node *of_find_all_nodes(struct device_node *prev)
147 struct device_node *np;
151 } else if (prev->child) {
155 * Walk back up looking for a sibling, or the end of the
159 while (np->parent && !np->sibling)
161 np = np->sibling; /* Might be null at the end of the tree */
167 const void *of_get_property(const struct device_node *np, const char *name,
170 struct property *pp = of_find_property(np, name, lenp);
172 return pp ? pp->value : NULL;
175 const struct property *of_get_first_property(const struct device_node *np)
180 return np->properties;
183 const struct property *of_get_next_property(const struct device_node *np,
184 const struct property *property)
189 return property->next;
192 const void *of_get_property_by_prop(const struct device_node *np,
193 const struct property *property,
197 if (!np || !property)
200 *name = property->name;
202 *lenp = property->length;
204 return property->value;
207 static const char *of_prop_next_string(struct property *prop, const char *cur)
209 const void *curv = cur;
217 curv += strlen(cur) + 1;
218 if (curv >= prop->value + prop->length)
224 int of_device_is_compatible(const struct device_node *device,
225 const char *compat, const char *type,
228 struct property *prop;
230 int index = 0, score = 0;
232 /* Compatible match has highest priority */
233 if (compat && compat[0]) {
234 prop = of_find_property(device, "compatible", NULL);
235 for (cp = of_prop_next_string(prop, NULL); cp;
236 cp = of_prop_next_string(prop, cp), index++) {
237 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
238 score = INT_MAX/2 - (index << 2);
246 /* Matching type is better than matching name */
247 if (type && type[0]) {
248 if (!device->type || of_node_cmp(type, device->type))
253 /* Matching name is a bit better than not */
254 if (name && name[0]) {
255 if (!device->name || of_node_cmp(name, device->name))
263 bool of_device_is_available(const struct device_node *device)
271 status = of_get_property(device, "status", &statlen);
276 if (!strcmp(status, "okay"))
283 struct device_node *of_get_parent(const struct device_node *node)
285 const struct device_node *np;
290 np = of_node_get(node->parent);
292 return (struct device_node *)np;
295 static struct device_node *__of_get_next_child(const struct device_node *node,
296 struct device_node *prev)
298 struct device_node *next;
303 next = prev ? prev->sibling : node->child;
305 * coverity[dead_error_line : FALSE]
306 * Dead code here since our current implementation of of_node_get()
307 * always returns NULL (Coverity CID 163245). But we leave it as is
308 * since we may want to implement get/put later.
310 for (; next; next = next->sibling)
311 if (of_node_get(next))
317 #define __for_each_child_of_node(parent, child) \
318 for (child = __of_get_next_child(parent, NULL); child != NULL; \
319 child = __of_get_next_child(parent, child))
321 static struct device_node *__of_find_node_by_path(struct device_node *parent,
324 struct device_node *child;
327 len = strcspn(path, "/:");
331 __for_each_child_of_node(parent, child) {
332 const char *name = strrchr(child->full_name, '/');
335 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
341 #define for_each_property_of_node(dn, pp) \
342 for (pp = dn->properties; pp != NULL; pp = pp->next)
344 struct device_node *of_find_node_opts_by_path(const char *path,
347 struct device_node *np = NULL;
349 const char *separator = strchr(path, ':');
352 *opts = separator ? separator + 1 : NULL;
354 if (strcmp(path, "/") == 0)
355 return of_node_get(gd->of_root);
357 /* The path could begin with an alias */
360 const char *p = separator;
363 p = strchrnul(path, '/');
366 /* of_aliases must not be NULL */
370 for_each_property_of_node(of_aliases, pp) {
371 if (strlen(pp->name) == len && !strncmp(pp->name, path,
373 np = of_find_node_by_path(pp->value);
382 /* Step down the tree matching path components */
384 np = of_node_get(gd->of_root);
385 while (np && *path == '/') {
386 struct device_node *tmp = np;
388 path++; /* Increment past '/' delimiter */
389 np = __of_find_node_by_path(np, path);
391 path = strchrnul(path, '/');
392 if (separator && separator < path)
399 struct device_node *of_find_compatible_node(struct device_node *from,
400 const char *type, const char *compatible)
402 struct device_node *np;
404 for_each_of_allnodes_from(from, np)
405 if (of_device_is_compatible(np, compatible, type, NULL) &&
413 static int of_device_has_prop_value(const struct device_node *device,
414 const char *propname, const void *propval,
417 struct property *prop = of_find_property(device, propname, NULL);
419 if (!prop || !prop->value || prop->length != proplen)
421 return !memcmp(prop->value, propval, proplen);
424 struct device_node *of_find_node_by_prop_value(struct device_node *from,
425 const char *propname,
426 const void *propval, int proplen)
428 struct device_node *np;
430 for_each_of_allnodes_from(from, np) {
431 if (of_device_has_prop_value(np, propname, propval, proplen) &&
440 struct device_node *of_find_node_by_phandle(phandle handle)
442 struct device_node *np;
447 for_each_of_allnodes(np)
448 if (np->phandle == handle)
450 (void)of_node_get(np);
456 * of_find_property_value_of_size() - find property of given size
458 * Search for a property in a device node and validate the requested size.
460 * @np: device node from which the property value is to be read.
461 * @propname: name of the property to be searched.
462 * @len: requested length of property value
464 * @return the property value on success, -EINVAL if the property does not
465 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
466 * property data isn't large enough.
468 static void *of_find_property_value_of_size(const struct device_node *np,
469 const char *propname, u32 len)
471 struct property *prop = of_find_property(np, propname, NULL);
474 return ERR_PTR(-EINVAL);
476 return ERR_PTR(-ENODATA);
477 if (len > prop->length)
478 return ERR_PTR(-EOVERFLOW);
483 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
485 return of_read_u32_index(np, propname, 0, outp);
488 int of_read_u32_array(const struct device_node *np, const char *propname,
489 u32 *out_values, size_t sz)
493 debug("%s: %s: ", __func__, propname);
494 val = of_find_property_value_of_size(np, propname,
495 sz * sizeof(*out_values));
500 debug("size %zd\n", sz);
502 *out_values++ = be32_to_cpup(val++);
507 int of_read_u32_index(const struct device_node *np, const char *propname,
508 int index, u32 *outp)
512 debug("%s: %s: ", __func__, propname);
516 val = of_find_property_value_of_size(np, propname,
517 sizeof(*outp) * (index + 1));
519 debug("(not found)\n");
523 *outp = be32_to_cpup(val + index);
524 debug("%#x (%d)\n", *outp, *outp);
529 int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
533 debug("%s: %s: ", __func__, propname);
536 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
538 debug("(not found)\n");
542 *outp = be64_to_cpup(val);
543 debug("%#llx (%lld)\n", (unsigned long long)*outp,
544 (unsigned long long)*outp);
549 int of_property_match_string(const struct device_node *np, const char *propname,
552 const struct property *prop = of_find_property(np, propname, NULL);
563 end = p + prop->length;
565 for (i = 0; p < end; i++, p += l) {
566 l = strnlen(p, end - p) + 1;
569 debug("comparing %s with %s\n", string, p);
570 if (strcmp(string, p) == 0)
571 return i; /* Found it; return index */
577 * of_property_read_string_helper() - Utility helper for parsing string properties
578 * @np: device node from which the property value is to be read.
579 * @propname: name of the property to be searched.
580 * @out_strs: output array of string pointers.
581 * @sz: number of array elements to read.
582 * @skip: Number of strings to skip over at beginning of list.
584 * Don't call this function directly. It is a utility helper for the
585 * of_property_read_string*() family of functions.
587 int of_property_read_string_helper(const struct device_node *np,
588 const char *propname, const char **out_strs,
591 const struct property *prop = of_find_property(np, propname, NULL);
600 end = p + prop->length;
602 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
603 l = strnlen(p, end - p) + 1;
606 if (out_strs && i >= skip)
610 return i <= 0 ? -ENODATA : i;
613 static int __of_parse_phandle_with_args(const struct device_node *np,
614 const char *list_name,
615 const char *cells_name,
616 int cell_count, int index,
617 struct of_phandle_args *out_args)
619 const __be32 *list, *list_end;
620 int rc = 0, cur_index = 0;
622 struct device_node *node = NULL;
626 /* Retrieve the phandle list property */
627 list = of_get_property(np, list_name, &size);
630 list_end = list + size / sizeof(*list);
632 /* Loop over the phandles until all the requested entry is found */
633 while (list < list_end) {
638 * If phandle is 0, then it is an empty entry with no
639 * arguments. Skip forward to the next entry.
641 phandle = be32_to_cpup(list++);
644 * Find the provider node and parse the #*-cells
645 * property to determine the argument length.
647 * This is not needed if the cell count is hard-coded
648 * (i.e. cells_name not set, but cell_count is set),
649 * except when we're going to return the found node
652 if (cells_name || cur_index == index) {
653 node = of_find_node_by_phandle(phandle);
655 debug("%s: could not find phandle\n",
662 if (of_read_u32(node, cells_name, &count)) {
663 debug("%s: could not get %s for %s\n",
664 np->full_name, cells_name,
673 * Make sure that the arguments actually fit in the
674 * remaining property data length
676 if (list + count > list_end) {
677 debug("%s: arguments longer than property\n",
684 * All of the error cases above bail out of the loop, so at
685 * this point, the parsing is successful. If the requested
686 * index matches, then fill the out_args structure and return,
687 * or return -ENOENT for an empty entry.
690 if (cur_index == index) {
696 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
697 count = OF_MAX_PHANDLE_ARGS;
699 out_args->args_count = count;
700 for (i = 0; i < count; i++)
702 be32_to_cpup(list++);
707 /* Found it! return success */
718 * Unlock node before returning result; will be one of:
719 * -ENOENT : index is for empty phandle
720 * -EINVAL : parsing error on data
721 * [1..n] : Number of phandle (count mode; when index = -1)
723 rc = index < 0 ? cur_index : -ENOENT;
730 struct device_node *of_parse_phandle(const struct device_node *np,
731 const char *phandle_name, int index)
733 struct of_phandle_args args;
738 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
745 int of_parse_phandle_with_args(const struct device_node *np,
746 const char *list_name, const char *cells_name,
747 int index, struct of_phandle_args *out_args)
752 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
756 int of_count_phandle_with_args(const struct device_node *np,
757 const char *list_name, const char *cells_name)
759 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
763 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
764 int id, const char *stem, int stem_len)
768 strncpy(ap->stem, stem, stem_len);
769 ap->stem[stem_len] = 0;
770 list_add_tail(&ap->link, &aliases_lookup);
771 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
772 ap->alias, ap->stem, ap->id, of_node_full_name(np));
775 int of_alias_scan(void)
779 of_aliases = of_find_node_by_path("/aliases");
780 of_chosen = of_find_node_by_path("/chosen");
781 if (of_chosen == NULL)
782 of_chosen = of_find_node_by_path("/chosen@0");
787 name = of_get_property(of_chosen, "stdout-path", NULL);
789 of_stdout = of_find_node_opts_by_path(name,
796 for_each_property_of_node(of_aliases, pp) {
797 const char *start = pp->name;
798 const char *end = start + strlen(start);
799 struct device_node *np;
800 struct alias_prop *ap;
804 /* Skip those we do not want to proceed */
805 if (!strcmp(pp->name, "name") ||
806 !strcmp(pp->name, "phandle") ||
807 !strcmp(pp->name, "linux,phandle"))
810 np = of_find_node_by_path(pp->value);
815 * walk the alias backwards to extract the id and work out
818 while (isdigit(*(end-1)) && end > start)
822 if (strict_strtoul(end, 10, &id) < 0)
825 /* Allocate an alias_prop with enough space for the stem */
826 ap = malloc(sizeof(*ap) + len + 1);
829 memset(ap, 0, sizeof(*ap) + len + 1);
831 of_alias_add(ap, np, id, start, len);
837 int of_alias_get_id(const struct device_node *np, const char *stem)
839 struct alias_prop *app;
842 mutex_lock(&of_mutex);
843 list_for_each_entry(app, &aliases_lookup, link) {
844 if (strcmp(app->stem, stem) != 0)
852 mutex_unlock(&of_mutex);
857 int of_alias_get_highest_id(const char *stem)
859 struct alias_prop *app;
862 mutex_lock(&of_mutex);
863 list_for_each_entry(app, &aliases_lookup, link) {
864 if (strcmp(app->stem, stem) != 0)
870 mutex_unlock(&of_mutex);
875 struct device_node *of_get_stdout(void)