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(const char *path,
349 struct device_node *np = NULL;
351 const char *separator = strchr(path, ':');
354 *opts = separator ? separator + 1 : NULL;
356 if (strcmp(path, "/") == 0)
357 return of_node_get(gd->of_root);
359 /* The path could begin with an alias */
362 const char *p = separator;
365 p = strchrnul(path, '/');
368 /* of_aliases must not be NULL */
372 for_each_property_of_node(of_aliases, pp) {
373 if (strlen(pp->name) == len && !strncmp(pp->name, path,
375 np = of_find_node_by_path(pp->value);
384 /* Step down the tree matching path components */
386 np = of_node_get(gd->of_root);
387 while (np && *path == '/') {
388 struct device_node *tmp = np;
390 path++; /* Increment past '/' delimiter */
391 np = __of_find_node_by_path(np, path);
393 path = strchrnul(path, '/');
394 if (separator && separator < path)
401 struct device_node *of_find_compatible_node(struct device_node *from,
402 const char *type, const char *compatible)
404 struct device_node *np;
406 for_each_of_allnodes_from(from, np)
407 if (of_device_is_compatible(np, compatible, type, NULL) &&
415 static int of_device_has_prop_value(const struct device_node *device,
416 const char *propname, const void *propval,
419 struct property *prop = of_find_property(device, propname, NULL);
421 if (!prop || !prop->value || prop->length != proplen)
423 return !memcmp(prop->value, propval, proplen);
426 struct device_node *of_find_node_by_prop_value(struct device_node *from,
427 const char *propname,
428 const void *propval, int proplen)
430 struct device_node *np;
432 for_each_of_allnodes_from(from, np) {
433 if (of_device_has_prop_value(np, propname, propval, proplen) &&
442 struct device_node *of_find_node_by_phandle(phandle handle)
444 struct device_node *np;
449 for_each_of_allnodes(np)
450 if (np->phandle == handle)
452 (void)of_node_get(np);
458 * of_find_property_value_of_size() - find property of given size
460 * Search for a property in a device node and validate the requested size.
462 * @np: device node from which the property value is to be read.
463 * @propname: name of the property to be searched.
464 * @len: requested length of property value
466 * @return the property value on success, -EINVAL if the property does not
467 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
468 * property data isn't large enough.
470 static void *of_find_property_value_of_size(const struct device_node *np,
471 const char *propname, u32 len)
473 struct property *prop = of_find_property(np, propname, NULL);
476 return ERR_PTR(-EINVAL);
478 return ERR_PTR(-ENODATA);
479 if (len > prop->length)
480 return ERR_PTR(-EOVERFLOW);
485 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
487 return of_read_u32_index(np, propname, 0, outp);
490 int of_read_u32_array(const struct device_node *np, const char *propname,
491 u32 *out_values, size_t sz)
495 debug("%s: %s: ", __func__, propname);
496 val = of_find_property_value_of_size(np, propname,
497 sz * sizeof(*out_values));
502 debug("size %zd\n", sz);
504 *out_values++ = be32_to_cpup(val++);
509 int of_read_u32_index(const struct device_node *np, const char *propname,
510 int index, u32 *outp)
514 debug("%s: %s: ", __func__, propname);
518 val = of_find_property_value_of_size(np, propname,
519 sizeof(*outp) * (index + 1));
521 debug("(not found)\n");
525 *outp = be32_to_cpup(val + index);
526 debug("%#x (%d)\n", *outp, *outp);
531 int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
535 debug("%s: %s: ", __func__, propname);
538 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
540 debug("(not found)\n");
544 *outp = be64_to_cpup(val);
545 debug("%#llx (%lld)\n", (unsigned long long)*outp,
546 (unsigned long long)*outp);
551 int of_property_match_string(const struct device_node *np, const char *propname,
554 const struct property *prop = of_find_property(np, propname, NULL);
565 end = p + prop->length;
567 for (i = 0; p < end; i++, p += l) {
568 l = strnlen(p, end - p) + 1;
571 debug("comparing %s with %s\n", string, p);
572 if (strcmp(string, p) == 0)
573 return i; /* Found it; return index */
579 * of_property_read_string_helper() - Utility helper for parsing string properties
580 * @np: device node from which the property value is to be read.
581 * @propname: name of the property to be searched.
582 * @out_strs: output array of string pointers.
583 * @sz: number of array elements to read.
584 * @skip: Number of strings to skip over at beginning of list.
586 * Don't call this function directly. It is a utility helper for the
587 * of_property_read_string*() family of functions.
589 int of_property_read_string_helper(const struct device_node *np,
590 const char *propname, const char **out_strs,
593 const struct property *prop = of_find_property(np, propname, NULL);
602 end = p + prop->length;
604 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
605 l = strnlen(p, end - p) + 1;
608 if (out_strs && i >= skip)
612 return i <= 0 ? -ENODATA : i;
615 static int __of_parse_phandle_with_args(const struct device_node *np,
616 const char *list_name,
617 const char *cells_name,
618 int cell_count, int index,
619 struct of_phandle_args *out_args)
621 const __be32 *list, *list_end;
622 int rc = 0, cur_index = 0;
624 struct device_node *node = NULL;
628 /* Retrieve the phandle list property */
629 list = of_get_property(np, list_name, &size);
632 list_end = list + size / sizeof(*list);
634 /* Loop over the phandles until all the requested entry is found */
635 while (list < list_end) {
640 * If phandle is 0, then it is an empty entry with no
641 * arguments. Skip forward to the next entry.
643 phandle = be32_to_cpup(list++);
646 * Find the provider node and parse the #*-cells
647 * property to determine the argument length.
649 * This is not needed if the cell count is hard-coded
650 * (i.e. cells_name not set, but cell_count is set),
651 * except when we're going to return the found node
654 if (cells_name || cur_index == index) {
655 node = of_find_node_by_phandle(phandle);
657 debug("%s: could not find phandle\n",
664 if (of_read_u32(node, cells_name, &count)) {
665 debug("%s: could not get %s for %s\n",
666 np->full_name, cells_name,
675 * Make sure that the arguments actually fit in the
676 * remaining property data length
678 if (list + count > list_end) {
679 debug("%s: arguments longer than property\n",
686 * All of the error cases above bail out of the loop, so at
687 * this point, the parsing is successful. If the requested
688 * index matches, then fill the out_args structure and return,
689 * or return -ENOENT for an empty entry.
692 if (cur_index == index) {
698 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
699 count = OF_MAX_PHANDLE_ARGS;
701 out_args->args_count = count;
702 for (i = 0; i < count; i++)
704 be32_to_cpup(list++);
709 /* Found it! return success */
720 * Unlock node before returning result; will be one of:
721 * -ENOENT : index is for empty phandle
722 * -EINVAL : parsing error on data
723 * [1..n] : Number of phandle (count mode; when index = -1)
725 rc = index < 0 ? cur_index : -ENOENT;
732 struct device_node *of_parse_phandle(const struct device_node *np,
733 const char *phandle_name, int index)
735 struct of_phandle_args args;
740 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
747 int of_parse_phandle_with_args(const struct device_node *np,
748 const char *list_name, const char *cells_name,
749 int cell_count, int index,
750 struct of_phandle_args *out_args)
755 return __of_parse_phandle_with_args(np, list_name, cells_name,
756 cell_count, index, out_args);
759 int of_count_phandle_with_args(const struct device_node *np,
760 const char *list_name, const char *cells_name,
763 return __of_parse_phandle_with_args(np, list_name, cells_name,
764 cell_count, -1, NULL);
767 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
768 int id, const char *stem, int stem_len)
772 strncpy(ap->stem, stem, stem_len);
773 ap->stem[stem_len] = 0;
774 list_add_tail(&ap->link, &aliases_lookup);
775 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
776 ap->alias, ap->stem, ap->id, of_node_full_name(np));
779 int of_alias_scan(void)
783 of_aliases = of_find_node_by_path("/aliases");
784 of_chosen = of_find_node_by_path("/chosen");
785 if (of_chosen == NULL)
786 of_chosen = of_find_node_by_path("/chosen@0");
791 name = of_get_property(of_chosen, "stdout-path", NULL);
793 of_stdout = of_find_node_opts_by_path(name,
800 for_each_property_of_node(of_aliases, pp) {
801 const char *start = pp->name;
802 const char *end = start + strlen(start);
803 struct device_node *np;
804 struct alias_prop *ap;
808 /* Skip those we do not want to proceed */
809 if (!strcmp(pp->name, "name") ||
810 !strcmp(pp->name, "phandle") ||
811 !strcmp(pp->name, "linux,phandle"))
814 np = of_find_node_by_path(pp->value);
819 * walk the alias backwards to extract the id and work out
822 while (isdigit(*(end-1)) && end > start)
826 if (strict_strtoul(end, 10, &id) < 0)
829 /* Allocate an alias_prop with enough space for the stem */
830 ap = malloc(sizeof(*ap) + len + 1);
833 memset(ap, 0, sizeof(*ap) + len + 1);
835 of_alias_add(ap, np, id, start, len);
841 int of_alias_get_id(const struct device_node *np, const char *stem)
843 struct alias_prop *app;
846 mutex_lock(&of_mutex);
847 list_for_each_entry(app, &aliases_lookup, link) {
848 if (strcmp(app->stem, stem) != 0)
856 mutex_unlock(&of_mutex);
861 int of_alias_get_highest_id(const char *stem)
863 struct alias_prop *app;
866 mutex_lock(&of_mutex);
867 list_for_each_entry(app, &aliases_lookup, link) {
868 if (strcmp(app->stem, stem) != 0)
874 mutex_unlock(&of_mutex);
879 struct device_node *of_get_stdout(void)