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 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(const struct device_node *np, const char *propname, u64 *outp)
577 debug("%s: %s: ", __func__, propname);
580 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
582 debug("(not found)\n");
586 *outp = be64_to_cpup(val);
587 debug("%#llx (%lld)\n", (unsigned long long)*outp,
588 (unsigned long long)*outp);
593 int of_property_match_string(const struct device_node *np, const char *propname,
596 const struct property *prop = of_find_property(np, propname, NULL);
607 end = p + prop->length;
609 for (i = 0; p < end; i++, p += l) {
610 l = strnlen(p, end - p) + 1;
613 debug("comparing %s with %s\n", string, p);
614 if (strcmp(string, p) == 0)
615 return i; /* Found it; return index */
621 * of_property_read_string_helper() - Utility helper for parsing string properties
622 * @np: device node from which the property value is to be read.
623 * @propname: name of the property to be searched.
624 * @out_strs: output array of string pointers.
625 * @sz: number of array elements to read.
626 * @skip: Number of strings to skip over at beginning of list (cannot be
629 * Don't call this function directly. It is a utility helper for the
630 * of_property_read_string*() family of functions.
632 int of_property_read_string_helper(const struct device_node *np,
633 const char *propname, const char **out_strs,
636 const struct property *prop = of_find_property(np, propname, NULL);
645 end = p + prop->length;
647 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
648 l = strnlen(p, end - p) + 1;
651 if (out_strs && i >= skip)
655 return i <= 0 ? -ENODATA : i;
658 static int __of_parse_phandle_with_args(const struct device_node *np,
659 const char *list_name,
660 const char *cells_name,
661 int cell_count, int index,
662 struct of_phandle_args *out_args)
664 const __be32 *list, *list_end;
665 int rc = 0, cur_index = 0;
667 struct device_node *node = NULL;
671 /* Retrieve the phandle list property */
672 list = of_get_property(np, list_name, &size);
675 list_end = list + size / sizeof(*list);
677 /* Loop over the phandles until all the requested entry is found */
678 while (list < list_end) {
683 * If phandle is 0, then it is an empty entry with no
684 * arguments. Skip forward to the next entry.
686 phandle = be32_to_cpup(list++);
689 * Find the provider node and parse the #*-cells
690 * property to determine the argument length.
692 * This is not needed if the cell count is hard-coded
693 * (i.e. cells_name not set, but cell_count is set),
694 * except when we're going to return the found node
697 if (cells_name || cur_index == index) {
698 node = of_find_node_by_phandle(NULL, phandle);
700 debug("%s: could not find phandle\n",
707 if (of_read_u32(node, cells_name, &count)) {
708 debug("%s: could not get %s for %s\n",
709 np->full_name, cells_name,
718 * Make sure that the arguments actually fit in the
719 * remaining property data length
721 if (list + count > list_end) {
722 debug("%s: arguments longer than property\n",
729 * All of the error cases above bail out of the loop, so at
730 * this point, the parsing is successful. If the requested
731 * index matches, then fill the out_args structure and return,
732 * or return -ENOENT for an empty entry.
735 if (cur_index == index) {
741 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
742 count = OF_MAX_PHANDLE_ARGS;
744 out_args->args_count = count;
745 for (i = 0; i < count; i++)
747 be32_to_cpup(list++);
752 /* Found it! return success */
763 * Unlock node before returning result; will be one of:
764 * -ENOENT : index is for empty phandle
765 * -EINVAL : parsing error on data
766 * [1..n] : Number of phandle (count mode; when index = -1)
768 rc = index < 0 ? cur_index : -ENOENT;
775 struct device_node *of_parse_phandle(const struct device_node *np,
776 const char *phandle_name, int index)
778 struct of_phandle_args args;
783 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
790 int of_parse_phandle_with_args(const struct device_node *np,
791 const char *list_name, const char *cells_name,
792 int cell_count, int index,
793 struct of_phandle_args *out_args)
798 return __of_parse_phandle_with_args(np, list_name, cells_name,
799 cell_count, index, out_args);
802 int of_count_phandle_with_args(const struct device_node *np,
803 const char *list_name, const char *cells_name,
806 return __of_parse_phandle_with_args(np, list_name, cells_name,
807 cell_count, -1, NULL);
810 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
811 int id, const char *stem, int stem_len)
815 strncpy(ap->stem, stem, stem_len);
816 ap->stem[stem_len] = 0;
817 list_add_tail(&ap->link, &aliases_lookup);
818 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
819 ap->alias, ap->stem, ap->id, of_node_full_name(np));
822 int of_alias_scan(void)
826 of_aliases = of_find_node_by_path("/aliases");
827 of_chosen = of_find_node_by_path("/chosen");
828 if (of_chosen == NULL)
829 of_chosen = of_find_node_by_path("/chosen@0");
834 name = of_get_property(of_chosen, "stdout-path", NULL);
836 of_stdout = of_find_node_opts_by_path(NULL, name,
843 for_each_property_of_node(of_aliases, pp) {
844 const char *start = pp->name;
845 const char *end = start + strlen(start);
846 struct device_node *np;
847 struct alias_prop *ap;
851 /* Skip those we do not want to proceed */
852 if (!strcmp(pp->name, "name") ||
853 !strcmp(pp->name, "phandle") ||
854 !strcmp(pp->name, "linux,phandle"))
857 np = of_find_node_by_path(pp->value);
862 * walk the alias backwards to extract the id and work out
865 while (isdigit(*(end-1)) && end > start)
869 if (strict_strtoul(end, 10, &id) < 0)
872 /* Allocate an alias_prop with enough space for the stem */
873 ap = malloc(sizeof(*ap) + len + 1);
876 memset(ap, 0, sizeof(*ap) + len + 1);
878 of_alias_add(ap, np, id, start, len);
884 int of_alias_get_id(const struct device_node *np, const char *stem)
886 struct alias_prop *app;
889 mutex_lock(&of_mutex);
890 list_for_each_entry(app, &aliases_lookup, link) {
891 if (strcmp(app->stem, stem) != 0)
899 mutex_unlock(&of_mutex);
904 int of_alias_get_highest_id(const char *stem)
906 struct alias_prop *app;
909 mutex_lock(&of_mutex);
910 list_for_each_entry(app, &aliases_lookup, link) {
911 if (strcmp(app->stem, stem) != 0)
917 mutex_unlock(&of_mutex);
922 struct device_node *of_get_stdout(void)
927 int of_write_prop(struct device_node *np, const char *propname, int len,
931 struct property *pp_last = NULL;
932 struct property *new;
937 for (pp = np->properties; pp; pp = pp->next) {
938 if (strcmp(pp->name, propname) == 0) {
939 /* Property exists -> change value */
940 pp->value = (void *)value;
947 /* Property does not exist -> append new property */
948 new = malloc(sizeof(struct property));
952 new->name = strdup(propname);
958 new->value = (void *)value;
965 np->properties = new;
970 int of_add_subnode(struct device_node *parent, const char *name, int len,
971 struct device_node **childp)
973 struct device_node *child, *new, *last_sibling = NULL;
974 char *new_name, *full_name;
979 __for_each_child_of_node(parent, child) {
981 * make sure we don't use a child called "trevor" when we are
982 * searching for "trev".
984 if (!strncmp(child->name, name, len) && strlen(name) == len) {
988 last_sibling = child;
991 /* Subnode does not exist -> append new subnode */
992 new = calloc(1, sizeof(struct device_node));
996 new_name = memdup(name, len + 1);
1001 new_name[len] = '\0';
1004 * if the parent is the root node (named "") we don't need to prepend
1007 parent_fnl = *parent->name ? strlen(parent->full_name) : 0;
1008 full_name = calloc(1, parent_fnl + 1 + len + 1);
1014 new->name = new_name; /* assign to constant pointer */
1016 strcpy(full_name, parent->full_name); /* "" for root node */
1017 full_name[parent_fnl] = '/';
1018 strlcpy(&full_name[parent_fnl + 1], name, len + 1);
1019 new->full_name = full_name;
1021 /* Add as last sibling of the parent */
1023 last_sibling->sibling = new;
1025 parent->child = new;
1026 new->parent = parent;