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 <asm/global_data.h>
26 #include <linux/bug.h>
27 #include <linux/libfdt.h>
28 #include <dm/of_access.h>
29 #include <linux/ctype.h>
30 #include <linux/err.h>
31 #include <linux/ioport.h>
33 DECLARE_GLOBAL_DATA_PTR;
35 /* list of struct alias_prop aliases */
36 LIST_HEAD(aliases_lookup);
39 static struct device_node *of_aliases;
42 static struct device_node *of_chosen;
44 /* node pointed to by the stdout-path alias */
45 static struct device_node *of_stdout;
47 /* pointer to options given after the alias (separated by :) or NULL if none */
48 static const char *of_stdout_options;
51 * struct alias_prop - Alias property in 'aliases' node
53 * The structure represents one alias property of 'aliases' node as
54 * an entry in aliases_lookup list.
56 * @link: List node to link the structure in aliases_lookup list
57 * @alias: Alias property name
58 * @np: Pointer to device_node that the alias stands for
59 * @id: Index value from end of alias name
60 * @stem: Alias string without the index
63 struct list_head link;
65 struct device_node *np;
70 int of_n_addr_cells(const struct device_node *np)
77 ip = of_get_property(np, "#address-cells", NULL);
79 return be32_to_cpup(ip);
82 /* No #address-cells property for the root node */
83 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
86 int of_n_size_cells(const struct device_node *np)
93 ip = of_get_property(np, "#size-cells", NULL);
95 return be32_to_cpup(ip);
98 /* No #size-cells property for the root node */
99 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
102 int of_simple_addr_cells(const struct device_node *np)
106 ip = of_get_property(np, "#address-cells", NULL);
108 return be32_to_cpup(ip);
110 /* Return a default of 2 to match fdt_address_cells()*/
114 int of_simple_size_cells(const struct device_node *np)
118 ip = of_get_property(np, "#size-cells", NULL);
120 return be32_to_cpup(ip);
122 /* Return a default of 2 to match fdt_size_cells()*/
126 struct property *of_find_property(const struct device_node *np,
127 const char *name, int *lenp)
134 for (pp = np->properties; pp; pp = pp->next) {
135 if (strcmp(pp->name, name) == 0) {
142 *lenp = -FDT_ERR_NOTFOUND;
147 struct device_node *of_find_all_nodes(struct device_node *prev)
149 struct device_node *np;
153 } else if (prev->child) {
157 * Walk back up looking for a sibling, or the end of the
161 while (np->parent && !np->sibling)
163 np = np->sibling; /* Might be null at the end of the tree */
169 const void *of_get_property(const struct device_node *np, const char *name,
172 struct property *pp = of_find_property(np, name, lenp);
174 return pp ? pp->value : NULL;
177 const struct property *of_get_first_property(const struct device_node *np)
182 return np->properties;
185 const struct property *of_get_next_property(const struct device_node *np,
186 const struct property *property)
191 return property->next;
194 const void *of_get_property_by_prop(const struct device_node *np,
195 const struct property *property,
199 if (!np || !property)
202 *name = property->name;
204 *lenp = property->length;
206 return property->value;
209 static const char *of_prop_next_string(struct property *prop, const char *cur)
211 const void *curv = cur;
219 curv += strlen(cur) + 1;
220 if (curv >= prop->value + prop->length)
226 int of_device_is_compatible(const struct device_node *device,
227 const char *compat, const char *type,
230 struct property *prop;
232 int index = 0, score = 0;
234 /* Compatible match has highest priority */
235 if (compat && compat[0]) {
236 prop = of_find_property(device, "compatible", NULL);
237 for (cp = of_prop_next_string(prop, NULL); cp;
238 cp = of_prop_next_string(prop, cp), index++) {
239 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
240 score = INT_MAX/2 - (index << 2);
248 /* Matching type is better than matching name */
249 if (type && type[0]) {
250 if (!device->type || of_node_cmp(type, device->type))
255 /* Matching name is a bit better than not */
256 if (name && name[0]) {
257 if (!device->name || of_node_cmp(name, device->name))
265 bool of_device_is_available(const struct device_node *device)
273 status = of_get_property(device, "status", &statlen);
278 if (!strcmp(status, "okay"))
285 struct device_node *of_get_parent(const struct device_node *node)
287 const struct device_node *np;
292 np = of_node_get(node->parent);
294 return (struct device_node *)np;
297 static struct device_node *__of_get_next_child(const struct device_node *node,
298 struct device_node *prev)
300 struct device_node *next;
305 next = prev ? prev->sibling : node->child;
307 * coverity[dead_error_line : FALSE]
308 * Dead code here since our current implementation of of_node_get()
309 * always returns NULL (Coverity CID 163245). But we leave it as is
310 * since we may want to implement get/put later.
312 for (; next; next = next->sibling)
313 if (of_node_get(next))
319 #define __for_each_child_of_node(parent, child) \
320 for (child = __of_get_next_child(parent, NULL); child != NULL; \
321 child = __of_get_next_child(parent, child))
323 static struct device_node *__of_find_node_by_path(struct device_node *parent,
326 struct device_node *child;
329 len = strcspn(path, "/:");
333 __for_each_child_of_node(parent, child) {
334 const char *name = strrchr(child->full_name, '/');
337 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
343 #define for_each_property_of_node(dn, pp) \
344 for (pp = dn->properties; pp != NULL; pp = pp->next)
346 struct device_node *of_find_node_opts_by_path(struct device_node *root,
350 struct device_node *np = NULL;
352 const char *separator = strchr(path, ':');
357 *opts = separator ? separator + 1 : NULL;
359 if (strcmp(path, "/") == 0)
360 return of_node_get(root);
362 /* The path could begin with an alias */
365 const char *p = separator;
367 /* Only allow alias processing on the control FDT */
368 if (root != gd->of_root)
371 p = strchrnul(path, '/');
374 /* of_aliases must not be NULL */
378 for_each_property_of_node(of_aliases, pp) {
379 if (strlen(pp->name) == len && !strncmp(pp->name, path,
381 np = of_find_node_by_path(pp->value);
390 /* Step down the tree matching path components */
392 np = of_node_get(root);
393 while (np && *path == '/') {
394 struct device_node *tmp = np;
396 path++; /* Increment past '/' delimiter */
397 np = __of_find_node_by_path(np, path);
399 path = strchrnul(path, '/');
400 if (separator && separator < path)
407 struct device_node *of_find_compatible_node(struct device_node *from,
408 const char *type, const char *compatible)
410 struct device_node *np;
412 for_each_of_allnodes_from(from, np)
413 if (of_device_is_compatible(np, compatible, type, NULL) &&
421 static int of_device_has_prop_value(const struct device_node *device,
422 const char *propname, const void *propval,
425 struct property *prop = of_find_property(device, propname, NULL);
427 if (!prop || !prop->value || prop->length != proplen)
429 return !memcmp(prop->value, propval, proplen);
432 struct device_node *of_find_node_by_prop_value(struct device_node *from,
433 const char *propname,
434 const void *propval, int proplen)
436 struct device_node *np;
438 for_each_of_allnodes_from(from, np) {
439 if (of_device_has_prop_value(np, propname, propval, proplen) &&
448 struct device_node *of_find_node_by_phandle(struct device_node *root,
451 struct device_node *np;
456 for_each_of_allnodes_from(root, np)
457 if (np->phandle == handle)
459 (void)of_node_get(np);
465 * of_find_property_value_of_size() - find property of given size
467 * Search for a property in a device node and validate the requested size.
469 * @np: device node from which the property value is to be read.
470 * @propname: name of the property to be searched.
471 * @len: requested length of property value
473 * Return: the property value on success, -EINVAL if the property does not
474 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
475 * property data isn't large enough.
477 static void *of_find_property_value_of_size(const struct device_node *np,
478 const char *propname, u32 len)
480 struct property *prop = of_find_property(np, propname, NULL);
483 return ERR_PTR(-EINVAL);
485 return ERR_PTR(-ENODATA);
486 if (len > prop->length)
487 return ERR_PTR(-EOVERFLOW);
492 int of_read_u8(const struct device_node *np, const char *propname, u8 *outp)
496 debug("%s: %s: ", __func__, propname);
499 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
501 debug("(not found)\n");
506 debug("%#x (%d)\n", *outp, *outp);
511 int of_read_u16(const struct device_node *np, const char *propname, u16 *outp)
515 debug("%s: %s: ", __func__, propname);
518 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
520 debug("(not found)\n");
524 *outp = be16_to_cpup(val);
525 debug("%#x (%d)\n", *outp, *outp);
530 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
532 return of_read_u32_index(np, propname, 0, outp);
535 int of_read_u32_array(const struct device_node *np, const char *propname,
536 u32 *out_values, size_t sz)
540 debug("%s: %s: ", __func__, propname);
541 val = of_find_property_value_of_size(np, propname,
542 sz * sizeof(*out_values));
547 debug("size %zd\n", sz);
549 *out_values++ = be32_to_cpup(val++);
554 int of_read_u32_index(const struct device_node *np, const char *propname,
555 int index, u32 *outp)
559 debug("%s: %s: ", __func__, propname);
563 val = of_find_property_value_of_size(np, propname,
564 sizeof(*outp) * (index + 1));
566 debug("(not found)\n");
570 *outp = be32_to_cpup(val + index);
571 debug("%#x (%d)\n", *outp, *outp);
576 int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
580 debug("%s: %s: ", __func__, propname);
583 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
585 debug("(not found)\n");
589 *outp = be64_to_cpup(val);
590 debug("%#llx (%lld)\n", (unsigned long long)*outp,
591 (unsigned long long)*outp);
596 int of_property_match_string(const struct device_node *np, const char *propname,
599 const struct property *prop = of_find_property(np, propname, NULL);
610 end = p + prop->length;
612 for (i = 0; p < end; i++, p += l) {
613 l = strnlen(p, end - p) + 1;
616 debug("comparing %s with %s\n", string, p);
617 if (strcmp(string, p) == 0)
618 return i; /* Found it; return index */
624 * of_property_read_string_helper() - Utility helper for parsing string properties
625 * @np: device node from which the property value is to be read.
626 * @propname: name of the property to be searched.
627 * @out_strs: output array of string pointers.
628 * @sz: number of array elements to read.
629 * @skip: Number of strings to skip over at beginning of list (cannot be
632 * Don't call this function directly. It is a utility helper for the
633 * of_property_read_string*() family of functions.
635 int of_property_read_string_helper(const struct device_node *np,
636 const char *propname, const char **out_strs,
639 const struct property *prop = of_find_property(np, propname, NULL);
648 end = p + prop->length;
650 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
651 l = strnlen(p, end - p) + 1;
654 if (out_strs && i >= skip)
658 return i <= 0 ? -ENODATA : i;
661 static int __of_parse_phandle_with_args(const struct device_node *np,
662 const char *list_name,
663 const char *cells_name,
664 int cell_count, int index,
665 struct of_phandle_args *out_args)
667 const __be32 *list, *list_end;
668 int rc = 0, cur_index = 0;
670 struct device_node *node = NULL;
674 /* Retrieve the phandle list property */
675 list = of_get_property(np, list_name, &size);
678 list_end = list + size / sizeof(*list);
680 /* Loop over the phandles until all the requested entry is found */
681 while (list < list_end) {
686 * If phandle is 0, then it is an empty entry with no
687 * arguments. Skip forward to the next entry.
689 phandle = be32_to_cpup(list++);
692 * Find the provider node and parse the #*-cells
693 * property to determine the argument length.
695 * This is not needed if the cell count is hard-coded
696 * (i.e. cells_name not set, but cell_count is set),
697 * except when we're going to return the found node
700 if (cells_name || cur_index == index) {
701 node = of_find_node_by_phandle(NULL, phandle);
703 debug("%s: could not find phandle\n",
710 if (of_read_u32(node, cells_name, &count)) {
711 debug("%s: could not get %s for %s\n",
712 np->full_name, cells_name,
721 * Make sure that the arguments actually fit in the
722 * remaining property data length
724 if (list + count > list_end) {
725 debug("%s: arguments longer than property\n",
732 * All of the error cases above bail out of the loop, so at
733 * this point, the parsing is successful. If the requested
734 * index matches, then fill the out_args structure and return,
735 * or return -ENOENT for an empty entry.
738 if (cur_index == index) {
744 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
745 count = OF_MAX_PHANDLE_ARGS;
747 out_args->args_count = count;
748 for (i = 0; i < count; i++)
750 be32_to_cpup(list++);
755 /* Found it! return success */
766 * Unlock node before returning result; will be one of:
767 * -ENOENT : index is for empty phandle
768 * -EINVAL : parsing error on data
769 * [1..n] : Number of phandle (count mode; when index = -1)
771 rc = index < 0 ? cur_index : -ENOENT;
778 struct device_node *of_parse_phandle(const struct device_node *np,
779 const char *phandle_name, int index)
781 struct of_phandle_args args;
786 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
793 int of_parse_phandle_with_args(const struct device_node *np,
794 const char *list_name, const char *cells_name,
795 int cell_count, int index,
796 struct of_phandle_args *out_args)
801 return __of_parse_phandle_with_args(np, list_name, cells_name,
802 cell_count, index, out_args);
805 int of_count_phandle_with_args(const struct device_node *np,
806 const char *list_name, const char *cells_name,
809 return __of_parse_phandle_with_args(np, list_name, cells_name,
810 cell_count, -1, NULL);
813 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
814 int id, const char *stem, int stem_len)
818 strncpy(ap->stem, stem, stem_len);
819 ap->stem[stem_len] = 0;
820 list_add_tail(&ap->link, &aliases_lookup);
821 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
822 ap->alias, ap->stem, ap->id, of_node_full_name(np));
825 int of_alias_scan(void)
829 of_aliases = of_find_node_by_path("/aliases");
830 of_chosen = of_find_node_by_path("/chosen");
831 if (of_chosen == NULL)
832 of_chosen = of_find_node_by_path("/chosen@0");
837 name = of_get_property(of_chosen, "stdout-path", NULL);
839 of_stdout = of_find_node_opts_by_path(NULL, name,
846 for_each_property_of_node(of_aliases, pp) {
847 const char *start = pp->name;
848 const char *end = start + strlen(start);
849 struct device_node *np;
850 struct alias_prop *ap;
854 /* Skip those we do not want to proceed */
855 if (!strcmp(pp->name, "name") ||
856 !strcmp(pp->name, "phandle") ||
857 !strcmp(pp->name, "linux,phandle"))
860 np = of_find_node_by_path(pp->value);
865 * walk the alias backwards to extract the id and work out
868 while (isdigit(*(end-1)) && end > start)
872 if (strict_strtoul(end, 10, &id) < 0)
875 /* Allocate an alias_prop with enough space for the stem */
876 ap = malloc(sizeof(*ap) + len + 1);
879 memset(ap, 0, sizeof(*ap) + len + 1);
881 of_alias_add(ap, np, id, start, len);
887 int of_alias_get_id(const struct device_node *np, const char *stem)
889 struct alias_prop *app;
892 mutex_lock(&of_mutex);
893 list_for_each_entry(app, &aliases_lookup, link) {
894 if (strcmp(app->stem, stem) != 0)
902 mutex_unlock(&of_mutex);
907 int of_alias_get_highest_id(const char *stem)
909 struct alias_prop *app;
912 mutex_lock(&of_mutex);
913 list_for_each_entry(app, &aliases_lookup, link) {
914 if (strcmp(app->stem, stem) != 0)
920 mutex_unlock(&of_mutex);
925 struct device_node *of_get_stdout(void)
930 int of_write_prop(struct device_node *np, const char *propname, int len,
934 struct property *pp_last = NULL;
935 struct property *new;
940 for (pp = np->properties; pp; pp = pp->next) {
941 if (strcmp(pp->name, propname) == 0) {
942 /* Property exists -> change value */
943 pp->value = (void *)value;
950 /* Property does not exist -> append new property */
951 new = malloc(sizeof(struct property));
955 new->name = strdup(propname);
961 new->value = (void *)value;
968 np->properties = new;
973 int of_add_subnode(struct device_node *parent, const char *name, int len,
974 struct device_node **childp)
976 struct device_node *child, *new, *last_sibling = NULL;
977 char *new_name, *full_name;
982 __for_each_child_of_node(parent, child) {
984 * make sure we don't use a child called "trevor" when we are
985 * searching for "trev".
987 if (!strncmp(child->name, name, len) && strlen(name) == len) {
991 last_sibling = child;
994 /* Subnode does not exist -> append new subnode */
995 new = calloc(1, sizeof(struct device_node));
999 new_name = memdup(name, len + 1);
1004 new_name[len] = '\0';
1007 * if the parent is the root node (named "") we don't need to prepend
1010 parent_fnl = *parent->name ? strlen(parent->full_name) : 0;
1011 full_name = calloc(1, parent_fnl + 1 + len + 1);
1017 new->name = new_name; /* assign to constant pointer */
1019 strcpy(full_name, parent->full_name); /* "" for root node */
1020 full_name[parent_fnl] = '/';
1021 strlcpy(&full_name[parent_fnl + 1], name, len + 1);
1022 new->full_name = full_name;
1024 /* Add as last sibling of the parent */
1026 last_sibling->sibling = new;
1028 parent->child = new;
1029 new->parent = parent;