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
23 #include <linux/libfdt.h>
24 #include <dm/of_access.h>
25 #include <linux/ctype.h>
26 #include <linux/err.h>
27 #include <linux/ioport.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 /* list of struct alias_prop aliases */
32 LIST_HEAD(aliases_lookup);
35 static struct device_node *of_aliases;
38 static struct device_node *of_chosen;
40 /* node pointed to by the stdout-path alias */
41 static struct device_node *of_stdout;
43 /* pointer to options given after the alias (separated by :) or NULL if none */
44 static const char *of_stdout_options;
47 * struct alias_prop - Alias property in 'aliases' node
49 * The structure represents one alias property of 'aliases' node as
50 * an entry in aliases_lookup list.
52 * @link: List node to link the structure in aliases_lookup list
53 * @alias: Alias property name
54 * @np: Pointer to device_node that the alias stands for
55 * @id: Index value from end of alias name
56 * @stem: Alias string without the index
59 struct list_head link;
61 struct device_node *np;
66 int of_n_addr_cells(const struct device_node *np)
73 ip = of_get_property(np, "#address-cells", NULL);
75 return be32_to_cpup(ip);
78 /* No #address-cells property for the root node */
79 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
82 int of_n_size_cells(const struct device_node *np)
89 ip = of_get_property(np, "#size-cells", NULL);
91 return be32_to_cpup(ip);
94 /* No #size-cells property for the root node */
95 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
98 int of_simple_addr_cells(const struct device_node *np)
102 ip = of_get_property(np, "#address-cells", NULL);
104 return be32_to_cpup(ip);
106 /* Return a default of 2 to match fdt_address_cells()*/
110 int of_simple_size_cells(const struct device_node *np)
114 ip = of_get_property(np, "#size-cells", NULL);
116 return be32_to_cpup(ip);
118 /* Return a default of 2 to match fdt_size_cells()*/
122 struct property *of_find_property(const struct device_node *np,
123 const char *name, int *lenp)
130 for (pp = np->properties; pp; pp = pp->next) {
131 if (strcmp(pp->name, name) == 0) {
138 *lenp = -FDT_ERR_NOTFOUND;
143 struct device_node *of_find_all_nodes(struct device_node *prev)
145 struct device_node *np;
149 } else if (prev->child) {
153 * Walk back up looking for a sibling, or the end of the
157 while (np->parent && !np->sibling)
159 np = np->sibling; /* Might be null at the end of the tree */
165 const void *of_get_property(const struct device_node *np, const char *name,
168 struct property *pp = of_find_property(np, name, lenp);
170 return pp ? pp->value : NULL;
173 static const char *of_prop_next_string(struct property *prop, const char *cur)
175 const void *curv = cur;
183 curv += strlen(cur) + 1;
184 if (curv >= prop->value + prop->length)
190 int of_device_is_compatible(const struct device_node *device,
191 const char *compat, const char *type,
194 struct property *prop;
196 int index = 0, score = 0;
198 /* Compatible match has highest priority */
199 if (compat && compat[0]) {
200 prop = of_find_property(device, "compatible", NULL);
201 for (cp = of_prop_next_string(prop, NULL); cp;
202 cp = of_prop_next_string(prop, cp), index++) {
203 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
204 score = INT_MAX/2 - (index << 2);
212 /* Matching type is better than matching name */
213 if (type && type[0]) {
214 if (!device->type || of_node_cmp(type, device->type))
219 /* Matching name is a bit better than not */
220 if (name && name[0]) {
221 if (!device->name || of_node_cmp(name, device->name))
229 bool of_device_is_available(const struct device_node *device)
237 status = of_get_property(device, "status", &statlen);
242 if (!strcmp(status, "okay"))
249 struct device_node *of_get_parent(const struct device_node *node)
251 const struct device_node *np;
256 np = of_node_get(node->parent);
258 return (struct device_node *)np;
261 static struct device_node *__of_get_next_child(const struct device_node *node,
262 struct device_node *prev)
264 struct device_node *next;
269 next = prev ? prev->sibling : node->child;
271 * coverity[dead_error_line : FALSE]
272 * Dead code here since our current implementation of of_node_get()
273 * always returns NULL (Coverity CID 163245). But we leave it as is
274 * since we may want to implement get/put later.
276 for (; next; next = next->sibling)
277 if (of_node_get(next))
283 #define __for_each_child_of_node(parent, child) \
284 for (child = __of_get_next_child(parent, NULL); child != NULL; \
285 child = __of_get_next_child(parent, child))
287 static struct device_node *__of_find_node_by_path(struct device_node *parent,
290 struct device_node *child;
293 len = strcspn(path, "/:");
297 __for_each_child_of_node(parent, child) {
298 const char *name = strrchr(child->full_name, '/');
301 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
307 #define for_each_property_of_node(dn, pp) \
308 for (pp = dn->properties; pp != NULL; pp = pp->next)
310 struct device_node *of_find_node_opts_by_path(const char *path,
313 struct device_node *np = NULL;
315 const char *separator = strchr(path, ':');
318 *opts = separator ? separator + 1 : NULL;
320 if (strcmp(path, "/") == 0)
321 return of_node_get(gd->of_root);
323 /* The path could begin with an alias */
326 const char *p = separator;
329 p = strchrnul(path, '/');
332 /* of_aliases must not be NULL */
336 for_each_property_of_node(of_aliases, pp) {
337 if (strlen(pp->name) == len && !strncmp(pp->name, path,
339 np = of_find_node_by_path(pp->value);
348 /* Step down the tree matching path components */
350 np = of_node_get(gd->of_root);
351 while (np && *path == '/') {
352 struct device_node *tmp = np;
354 path++; /* Increment past '/' delimiter */
355 np = __of_find_node_by_path(np, path);
357 path = strchrnul(path, '/');
358 if (separator && separator < path)
365 struct device_node *of_find_compatible_node(struct device_node *from,
366 const char *type, const char *compatible)
368 struct device_node *np;
370 for_each_of_allnodes_from(from, np)
371 if (of_device_is_compatible(np, compatible, type, NULL) &&
379 static int of_device_has_prop_value(const struct device_node *device,
380 const char *propname, const void *propval,
383 struct property *prop = of_find_property(device, propname, NULL);
385 if (!prop || !prop->value || prop->length != proplen)
387 return !memcmp(prop->value, propval, proplen);
390 struct device_node *of_find_node_by_prop_value(struct device_node *from,
391 const char *propname,
392 const void *propval, int proplen)
394 struct device_node *np;
396 for_each_of_allnodes_from(from, np) {
397 if (of_device_has_prop_value(np, propname, propval, proplen) &&
406 struct device_node *of_find_node_by_phandle(phandle handle)
408 struct device_node *np;
413 for_each_of_allnodes(np)
414 if (np->phandle == handle)
416 (void)of_node_get(np);
422 * of_find_property_value_of_size() - find property of given size
424 * Search for a property in a device node and validate the requested size.
426 * @np: device node from which the property value is to be read.
427 * @propname: name of the property to be searched.
428 * @len: requested length of property value
430 * @return the property value on success, -EINVAL if the property does not
431 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
432 * property data isn't large enough.
434 static void *of_find_property_value_of_size(const struct device_node *np,
435 const char *propname, u32 len)
437 struct property *prop = of_find_property(np, propname, NULL);
440 return ERR_PTR(-EINVAL);
442 return ERR_PTR(-ENODATA);
443 if (len > prop->length)
444 return ERR_PTR(-EOVERFLOW);
449 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
453 debug("%s: %s: ", __func__, propname);
456 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
458 debug("(not found)\n");
462 *outp = be32_to_cpup(val);
463 debug("%#x (%d)\n", *outp, *outp);
468 int of_read_u32_array(const struct device_node *np, const char *propname,
469 u32 *out_values, size_t sz)
473 debug("%s: %s: ", __func__, propname);
474 val = of_find_property_value_of_size(np, propname,
475 sz * sizeof(*out_values));
480 debug("size %zd\n", sz);
482 *out_values++ = be32_to_cpup(val++);
487 int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
491 debug("%s: %s: ", __func__, propname);
494 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
496 debug("(not found)\n");
500 *outp = be64_to_cpup(val);
501 debug("%#llx (%lld)\n", (unsigned long long)*outp,
502 (unsigned long long)*outp);
507 int of_property_match_string(const struct device_node *np, const char *propname,
510 const struct property *prop = of_find_property(np, propname, NULL);
521 end = p + prop->length;
523 for (i = 0; p < end; i++, p += l) {
524 l = strnlen(p, end - p) + 1;
527 debug("comparing %s with %s\n", string, p);
528 if (strcmp(string, p) == 0)
529 return i; /* Found it; return index */
535 * of_property_read_string_helper() - Utility helper for parsing string properties
536 * @np: device node from which the property value is to be read.
537 * @propname: name of the property to be searched.
538 * @out_strs: output array of string pointers.
539 * @sz: number of array elements to read.
540 * @skip: Number of strings to skip over at beginning of list.
542 * Don't call this function directly. It is a utility helper for the
543 * of_property_read_string*() family of functions.
545 int of_property_read_string_helper(const struct device_node *np,
546 const char *propname, const char **out_strs,
549 const struct property *prop = of_find_property(np, propname, NULL);
558 end = p + prop->length;
560 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
561 l = strnlen(p, end - p) + 1;
564 if (out_strs && i >= skip)
568 return i <= 0 ? -ENODATA : i;
571 static int __of_parse_phandle_with_args(const struct device_node *np,
572 const char *list_name,
573 const char *cells_name,
574 int cell_count, int index,
575 struct of_phandle_args *out_args)
577 const __be32 *list, *list_end;
578 int rc = 0, cur_index = 0;
580 struct device_node *node = NULL;
584 /* Retrieve the phandle list property */
585 list = of_get_property(np, list_name, &size);
588 list_end = list + size / sizeof(*list);
590 /* Loop over the phandles until all the requested entry is found */
591 while (list < list_end) {
596 * If phandle is 0, then it is an empty entry with no
597 * arguments. Skip forward to the next entry.
599 phandle = be32_to_cpup(list++);
602 * Find the provider node and parse the #*-cells
603 * property to determine the argument length.
605 * This is not needed if the cell count is hard-coded
606 * (i.e. cells_name not set, but cell_count is set),
607 * except when we're going to return the found node
610 if (cells_name || cur_index == index) {
611 node = of_find_node_by_phandle(phandle);
613 debug("%s: could not find phandle\n",
620 if (of_read_u32(node, cells_name, &count)) {
621 debug("%s: could not get %s for %s\n",
622 np->full_name, cells_name,
631 * Make sure that the arguments actually fit in the
632 * remaining property data length
634 if (list + count > list_end) {
635 debug("%s: arguments longer than property\n",
642 * All of the error cases above bail out of the loop, so at
643 * this point, the parsing is successful. If the requested
644 * index matches, then fill the out_args structure and return,
645 * or return -ENOENT for an empty entry.
648 if (cur_index == index) {
654 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
655 count = OF_MAX_PHANDLE_ARGS;
657 out_args->args_count = count;
658 for (i = 0; i < count; i++)
660 be32_to_cpup(list++);
665 /* Found it! return success */
676 * Unlock node before returning result; will be one of:
677 * -ENOENT : index is for empty phandle
678 * -EINVAL : parsing error on data
679 * [1..n] : Number of phandle (count mode; when index = -1)
681 rc = index < 0 ? cur_index : -ENOENT;
688 struct device_node *of_parse_phandle(const struct device_node *np,
689 const char *phandle_name, int index)
691 struct of_phandle_args args;
696 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
703 int of_parse_phandle_with_args(const struct device_node *np,
704 const char *list_name, const char *cells_name,
705 int index, struct of_phandle_args *out_args)
710 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
714 int of_count_phandle_with_args(const struct device_node *np,
715 const char *list_name, const char *cells_name)
717 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
721 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
722 int id, const char *stem, int stem_len)
726 strncpy(ap->stem, stem, stem_len);
727 ap->stem[stem_len] = 0;
728 list_add_tail(&ap->link, &aliases_lookup);
729 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
730 ap->alias, ap->stem, ap->id, of_node_full_name(np));
733 int of_alias_scan(void)
737 of_aliases = of_find_node_by_path("/aliases");
738 of_chosen = of_find_node_by_path("/chosen");
739 if (of_chosen == NULL)
740 of_chosen = of_find_node_by_path("/chosen@0");
745 name = of_get_property(of_chosen, "stdout-path", NULL);
747 of_stdout = of_find_node_opts_by_path(name,
754 for_each_property_of_node(of_aliases, pp) {
755 const char *start = pp->name;
756 const char *end = start + strlen(start);
757 struct device_node *np;
758 struct alias_prop *ap;
762 /* Skip those we do not want to proceed */
763 if (!strcmp(pp->name, "name") ||
764 !strcmp(pp->name, "phandle") ||
765 !strcmp(pp->name, "linux,phandle"))
768 np = of_find_node_by_path(pp->value);
773 * walk the alias backwards to extract the id and work out
776 while (isdigit(*(end-1)) && end > start)
780 if (strict_strtoul(end, 10, &id) < 0)
783 /* Allocate an alias_prop with enough space for the stem */
784 ap = malloc(sizeof(*ap) + len + 1);
787 memset(ap, 0, sizeof(*ap) + len + 1);
789 of_alias_add(ap, np, id, start, len);
795 int of_alias_get_id(const struct device_node *np, const char *stem)
797 struct alias_prop *app;
800 mutex_lock(&of_mutex);
801 list_for_each_entry(app, &aliases_lookup, link) {
802 if (strcmp(app->stem, stem) != 0)
810 mutex_unlock(&of_mutex);
815 int of_alias_get_highest_id(const char *stem)
817 struct alias_prop *app;
820 mutex_lock(&of_mutex);
821 list_for_each_entry(app, &aliases_lookup, link) {
822 if (strcmp(app->stem, stem) != 0)
828 mutex_unlock(&of_mutex);
833 struct device_node *of_get_stdout(void)