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
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 static 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 and -EOVERFLOW if the property data isn't large enough.
476 static void *of_find_property_value_of_size(const struct device_node *np,
477 const char *propname, u32 len)
479 struct property *prop = of_find_property(np, propname, NULL);
482 return ERR_PTR(-EINVAL);
483 if (len > prop->length)
484 return ERR_PTR(-EOVERFLOW);
489 int of_read_u8(const struct device_node *np, const char *propname, u8 *outp)
493 debug("%s: %s: ", __func__, propname);
496 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
498 debug("(not found)\n");
503 debug("%#x (%d)\n", *outp, *outp);
508 int of_read_u16(const struct device_node *np, const char *propname, u16 *outp)
512 debug("%s: %s: ", __func__, propname);
515 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
517 debug("(not found)\n");
521 *outp = be16_to_cpup(val);
522 debug("%#x (%d)\n", *outp, *outp);
527 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
529 return of_read_u32_index(np, propname, 0, outp);
532 int of_read_u32_array(const struct device_node *np, const char *propname,
533 u32 *out_values, size_t sz)
537 debug("%s: %s: ", __func__, propname);
538 val = of_find_property_value_of_size(np, propname,
539 sz * sizeof(*out_values));
544 debug("size %zd\n", sz);
546 *out_values++ = be32_to_cpup(val++);
551 int of_read_u32_index(const struct device_node *np, const char *propname,
552 int index, u32 *outp)
556 debug("%s: %s: ", __func__, propname);
560 val = of_find_property_value_of_size(np, propname,
561 sizeof(*outp) * (index + 1));
563 debug("(not found)\n");
567 *outp = be32_to_cpup(val + index);
568 debug("%#x (%d)\n", *outp, *outp);
573 int of_read_u64_index(const struct device_node *np, const char *propname,
574 int index, u64 *outp)
578 debug("%s: %s: ", __func__, propname);
582 val = of_find_property_value_of_size(np, propname,
583 sizeof(*outp) * (index + 1));
585 debug("(not found)\n");
589 *outp = be64_to_cpup(val + index);
590 debug("%#llx (%lld)\n", (unsigned long long)*outp,
591 (unsigned long long)*outp);
596 int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
598 return of_read_u64_index(np, propname, 0, outp);
601 int of_property_match_string(const struct device_node *np, const char *propname,
605 const struct property *prop = of_find_property(np, propname, &len);
610 if (!prop && len == -FDT_ERR_NOTFOUND)
618 end = p + prop->length;
620 for (i = 0; p < end; i++, p += l) {
621 l = strnlen(p, end - p) + 1;
624 debug("comparing %s with %s\n", string, p);
625 if (strcmp(string, p) == 0)
626 return i; /* Found it; return index */
632 * of_property_read_string_helper() - Utility helper for parsing string properties
633 * @np: device node from which the property value is to be read.
634 * @propname: name of the property to be searched.
635 * @out_strs: output array of string pointers.
636 * @sz: number of array elements to read.
637 * @skip: Number of strings to skip over at beginning of list (cannot be
640 * Don't call this function directly. It is a utility helper for the
641 * of_property_read_string*() family of functions.
643 int of_property_read_string_helper(const struct device_node *np,
644 const char *propname, const char **out_strs,
647 const struct property *prop = of_find_property(np, propname, NULL);
656 end = p + prop->length;
658 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
659 l = strnlen(p, end - p) + 1;
662 if (out_strs && i >= skip)
666 return i <= 0 ? -ENODATA : i;
669 static int __of_parse_phandle_with_args(const struct device_node *np,
670 const char *list_name,
671 const char *cells_name,
672 int cell_count, int index,
673 struct of_phandle_args *out_args)
675 const __be32 *list, *list_end;
676 int rc = 0, cur_index = 0;
678 struct device_node *node = NULL;
682 /* Retrieve the phandle list property */
683 list = of_get_property(np, list_name, &size);
686 list_end = list + size / sizeof(*list);
688 /* Loop over the phandles until all the requested entry is found */
689 while (list < list_end) {
694 * If phandle is 0, then it is an empty entry with no
695 * arguments. Skip forward to the next entry.
697 phandle = be32_to_cpup(list++);
700 * Find the provider node and parse the #*-cells
701 * property to determine the argument length.
703 * This is not needed if the cell count is hard-coded
704 * (i.e. cells_name not set, but cell_count is set),
705 * except when we're going to return the found node
708 if (cells_name || cur_index == index) {
709 node = of_find_node_by_phandle(NULL, phandle);
711 debug("%s: could not find phandle\n",
718 if (of_read_u32(node, cells_name, &count)) {
719 debug("%s: could not get %s for %s\n",
720 np->full_name, cells_name,
729 * Make sure that the arguments actually fit in the
730 * remaining property data length
732 if (list + count > list_end) {
733 debug("%s: arguments longer than property\n",
740 * All of the error cases above bail out of the loop, so at
741 * this point, the parsing is successful. If the requested
742 * index matches, then fill the out_args structure and return,
743 * or return -ENOENT for an empty entry.
746 if (cur_index == index) {
752 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
753 count = OF_MAX_PHANDLE_ARGS;
755 out_args->args_count = count;
756 for (i = 0; i < count; i++)
758 be32_to_cpup(list++);
763 /* Found it! return success */
774 * Unlock node before returning result; will be one of:
775 * -ENOENT : index is for empty phandle
776 * -EINVAL : parsing error on data
777 * [1..n] : Number of phandle (count mode; when index = -1)
779 rc = index < 0 ? cur_index : -ENOENT;
786 struct device_node *of_parse_phandle(const struct device_node *np,
787 const char *phandle_name, int index)
789 struct of_phandle_args args;
794 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
801 int of_parse_phandle_with_args(const struct device_node *np,
802 const char *list_name, const char *cells_name,
803 int cell_count, int index,
804 struct of_phandle_args *out_args)
809 return __of_parse_phandle_with_args(np, list_name, cells_name,
810 cell_count, index, out_args);
813 int of_count_phandle_with_args(const struct device_node *np,
814 const char *list_name, const char *cells_name,
817 return __of_parse_phandle_with_args(np, list_name, cells_name,
818 cell_count, -1, NULL);
821 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
822 int id, const char *stem, int stem_len)
826 strncpy(ap->stem, stem, stem_len);
827 ap->stem[stem_len] = 0;
828 list_add_tail(&ap->link, &aliases_lookup);
829 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
830 ap->alias, ap->stem, ap->id, of_node_full_name(np));
833 int of_alias_scan(void)
837 of_aliases = of_find_node_by_path("/aliases");
838 of_chosen = of_find_node_by_path("/chosen");
839 if (of_chosen == NULL)
840 of_chosen = of_find_node_by_path("/chosen@0");
845 name = of_get_property(of_chosen, "stdout-path", NULL);
847 of_stdout = of_find_node_opts_by_path(NULL, name,
854 for_each_property_of_node(of_aliases, pp) {
855 const char *start = pp->name;
856 const char *end = start + strlen(start);
857 struct device_node *np;
858 struct alias_prop *ap;
862 /* Skip those we do not want to proceed */
863 if (!strcmp(pp->name, "name") ||
864 !strcmp(pp->name, "phandle") ||
865 !strcmp(pp->name, "linux,phandle"))
868 np = of_find_node_by_path(pp->value);
873 * walk the alias backwards to extract the id and work out
876 while (isdigit(*(end-1)) && end > start)
880 if (strict_strtoul(end, 10, &id) < 0)
883 /* Allocate an alias_prop with enough space for the stem */
884 ap = malloc(sizeof(*ap) + len + 1);
887 memset(ap, 0, sizeof(*ap) + len + 1);
889 of_alias_add(ap, np, id, start, len);
895 int of_alias_get_id(const struct device_node *np, const char *stem)
897 struct alias_prop *app;
900 mutex_lock(&of_mutex);
901 list_for_each_entry(app, &aliases_lookup, link) {
902 if (strcmp(app->stem, stem) != 0)
910 mutex_unlock(&of_mutex);
915 int of_alias_get_highest_id(const char *stem)
917 struct alias_prop *app;
920 mutex_lock(&of_mutex);
921 list_for_each_entry(app, &aliases_lookup, link) {
922 if (strcmp(app->stem, stem) != 0)
928 mutex_unlock(&of_mutex);
933 struct device_node *of_get_stdout(void)
938 int of_write_prop(struct device_node *np, const char *propname, int len,
942 struct property *pp_last = NULL;
943 struct property *new;
948 for (pp = np->properties; pp; pp = pp->next) {
949 if (strcmp(pp->name, propname) == 0) {
950 /* Property exists -> change value */
951 pp->value = (void *)value;
958 /* Property does not exist -> append new property */
959 new = malloc(sizeof(struct property));
963 new->name = strdup(propname);
969 new->value = (void *)value;
976 np->properties = new;
981 int of_add_subnode(struct device_node *parent, const char *name, int len,
982 struct device_node **childp)
984 struct device_node *child, *new, *last_sibling = NULL;
985 char *new_name, *full_name;
990 __for_each_child_of_node(parent, child) {
992 * make sure we don't use a child called "trevor" when we are
993 * searching for "trev".
995 if (!strncmp(child->name, name, len) && strlen(name) == len) {
999 last_sibling = child;
1002 /* Subnode does not exist -> append new subnode */
1003 new = calloc(1, sizeof(struct device_node));
1007 new_name = memdup(name, len + 1);
1012 new_name[len] = '\0';
1015 * if the parent is the root node (named "") we don't need to prepend
1018 parent_fnl = *parent->name ? strlen(parent->full_name) : 0;
1019 full_name = calloc(1, parent_fnl + 1 + len + 1);
1025 new->name = new_name; /* assign to constant pointer */
1027 strcpy(full_name, parent->full_name); /* "" for root node */
1028 full_name[parent_fnl] = '/';
1029 strlcpy(&full_name[parent_fnl + 1], name, len + 1);
1030 new->full_name = full_name;
1032 /* Add as last sibling of the parent */
1034 last_sibling->sibling = new;
1036 parent->child = new;
1037 new->parent = parent;
1044 int __of_remove_property(struct device_node *np, struct property *prop)
1046 struct property **next;
1048 for (next = &np->properties; *next; next = &(*next)->next) {
1055 /* found the node */
1061 int of_remove_property(struct device_node *np, struct property *prop)
1065 mutex_lock(&of_mutex);
1067 rc = __of_remove_property(np, prop);
1069 mutex_unlock(&of_mutex);
1074 int of_remove_node(struct device_node *to_remove)
1076 struct device_node *parent = to_remove->parent;
1077 struct device_node *np, *prev;
1082 __for_each_child_of_node(parent, np) {
1083 if (np == to_remove)
1090 /* if there is a previous node, link it to this one's sibling */
1092 prev->sibling = np->sibling;
1094 parent->child = np->sibling;
1097 * don't free it, since if this is an unflattened tree, all the memory
1098 * was alloced in one block; this pointer will be somewhere in the