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
24 #include <linux/libfdt.h>
25 #include <dm/of_access.h>
26 #include <linux/ctype.h>
27 #include <linux/err.h>
28 #include <linux/ioport.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 /* list of struct alias_prop aliases */
33 LIST_HEAD(aliases_lookup);
36 static struct device_node *of_aliases;
39 static struct device_node *of_chosen;
41 /* node pointed to by the stdout-path alias */
42 static struct device_node *of_stdout;
44 /* pointer to options given after the alias (separated by :) or NULL if none */
45 static const char *of_stdout_options;
48 * struct alias_prop - Alias property in 'aliases' node
50 * The structure represents one alias property of 'aliases' node as
51 * an entry in aliases_lookup list.
53 * @link: List node to link the structure in aliases_lookup list
54 * @alias: Alias property name
55 * @np: Pointer to device_node that the alias stands for
56 * @id: Index value from end of alias name
57 * @stem: Alias string without the index
60 struct list_head link;
62 struct device_node *np;
67 int of_n_addr_cells(const struct device_node *np)
74 ip = of_get_property(np, "#address-cells", NULL);
76 return be32_to_cpup(ip);
79 /* No #address-cells property for the root node */
80 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
83 int of_n_size_cells(const struct device_node *np)
90 ip = of_get_property(np, "#size-cells", NULL);
92 return be32_to_cpup(ip);
95 /* No #size-cells property for the root node */
96 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
99 int of_simple_addr_cells(const struct device_node *np)
103 ip = of_get_property(np, "#address-cells", NULL);
105 return be32_to_cpup(ip);
107 /* Return a default of 2 to match fdt_address_cells()*/
111 int of_simple_size_cells(const struct device_node *np)
115 ip = of_get_property(np, "#size-cells", NULL);
117 return be32_to_cpup(ip);
119 /* Return a default of 2 to match fdt_size_cells()*/
123 struct property *of_find_property(const struct device_node *np,
124 const char *name, int *lenp)
131 for (pp = np->properties; pp; pp = pp->next) {
132 if (strcmp(pp->name, name) == 0) {
139 *lenp = -FDT_ERR_NOTFOUND;
144 struct device_node *of_find_all_nodes(struct device_node *prev)
146 struct device_node *np;
150 } else if (prev->child) {
154 * Walk back up looking for a sibling, or the end of the
158 while (np->parent && !np->sibling)
160 np = np->sibling; /* Might be null at the end of the tree */
166 const void *of_get_property(const struct device_node *np, const char *name,
169 struct property *pp = of_find_property(np, name, lenp);
171 return pp ? pp->value : NULL;
174 static const char *of_prop_next_string(struct property *prop, const char *cur)
176 const void *curv = cur;
184 curv += strlen(cur) + 1;
185 if (curv >= prop->value + prop->length)
191 int of_device_is_compatible(const struct device_node *device,
192 const char *compat, const char *type,
195 struct property *prop;
197 int index = 0, score = 0;
199 /* Compatible match has highest priority */
200 if (compat && compat[0]) {
201 prop = of_find_property(device, "compatible", NULL);
202 for (cp = of_prop_next_string(prop, NULL); cp;
203 cp = of_prop_next_string(prop, cp), index++) {
204 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
205 score = INT_MAX/2 - (index << 2);
213 /* Matching type is better than matching name */
214 if (type && type[0]) {
215 if (!device->type || of_node_cmp(type, device->type))
220 /* Matching name is a bit better than not */
221 if (name && name[0]) {
222 if (!device->name || of_node_cmp(name, device->name))
230 bool of_device_is_available(const struct device_node *device)
238 status = of_get_property(device, "status", &statlen);
243 if (!strcmp(status, "okay"))
250 struct device_node *of_get_parent(const struct device_node *node)
252 const struct device_node *np;
257 np = of_node_get(node->parent);
259 return (struct device_node *)np;
262 static struct device_node *__of_get_next_child(const struct device_node *node,
263 struct device_node *prev)
265 struct device_node *next;
270 next = prev ? prev->sibling : node->child;
272 * coverity[dead_error_line : FALSE]
273 * Dead code here since our current implementation of of_node_get()
274 * always returns NULL (Coverity CID 163245). But we leave it as is
275 * since we may want to implement get/put later.
277 for (; next; next = next->sibling)
278 if (of_node_get(next))
284 #define __for_each_child_of_node(parent, child) \
285 for (child = __of_get_next_child(parent, NULL); child != NULL; \
286 child = __of_get_next_child(parent, child))
288 static struct device_node *__of_find_node_by_path(struct device_node *parent,
291 struct device_node *child;
294 len = strcspn(path, "/:");
298 __for_each_child_of_node(parent, child) {
299 const char *name = strrchr(child->full_name, '/');
302 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
308 #define for_each_property_of_node(dn, pp) \
309 for (pp = dn->properties; pp != NULL; pp = pp->next)
311 struct device_node *of_find_node_opts_by_path(const char *path,
314 struct device_node *np = NULL;
316 const char *separator = strchr(path, ':');
319 *opts = separator ? separator + 1 : NULL;
321 if (strcmp(path, "/") == 0)
322 return of_node_get(gd->of_root);
324 /* The path could begin with an alias */
327 const char *p = separator;
330 p = strchrnul(path, '/');
333 /* of_aliases must not be NULL */
337 for_each_property_of_node(of_aliases, pp) {
338 if (strlen(pp->name) == len && !strncmp(pp->name, path,
340 np = of_find_node_by_path(pp->value);
349 /* Step down the tree matching path components */
351 np = of_node_get(gd->of_root);
352 while (np && *path == '/') {
353 struct device_node *tmp = np;
355 path++; /* Increment past '/' delimiter */
356 np = __of_find_node_by_path(np, path);
358 path = strchrnul(path, '/');
359 if (separator && separator < path)
366 struct device_node *of_find_compatible_node(struct device_node *from,
367 const char *type, const char *compatible)
369 struct device_node *np;
371 for_each_of_allnodes_from(from, np)
372 if (of_device_is_compatible(np, compatible, type, NULL) &&
380 static int of_device_has_prop_value(const struct device_node *device,
381 const char *propname, const void *propval,
384 struct property *prop = of_find_property(device, propname, NULL);
386 if (!prop || !prop->value || prop->length != proplen)
388 return !memcmp(prop->value, propval, proplen);
391 struct device_node *of_find_node_by_prop_value(struct device_node *from,
392 const char *propname,
393 const void *propval, int proplen)
395 struct device_node *np;
397 for_each_of_allnodes_from(from, np) {
398 if (of_device_has_prop_value(np, propname, propval, proplen) &&
407 struct device_node *of_find_node_by_phandle(phandle handle)
409 struct device_node *np;
414 for_each_of_allnodes(np)
415 if (np->phandle == handle)
417 (void)of_node_get(np);
423 * of_find_property_value_of_size() - find property of given size
425 * Search for a property in a device node and validate the requested size.
427 * @np: device node from which the property value is to be read.
428 * @propname: name of the property to be searched.
429 * @len: requested length of property value
431 * @return the property value on success, -EINVAL if the property does not
432 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
433 * property data isn't large enough.
435 static void *of_find_property_value_of_size(const struct device_node *np,
436 const char *propname, u32 len)
438 struct property *prop = of_find_property(np, propname, NULL);
441 return ERR_PTR(-EINVAL);
443 return ERR_PTR(-ENODATA);
444 if (len > prop->length)
445 return ERR_PTR(-EOVERFLOW);
450 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
454 debug("%s: %s: ", __func__, propname);
457 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
459 debug("(not found)\n");
463 *outp = be32_to_cpup(val);
464 debug("%#x (%d)\n", *outp, *outp);
469 int of_read_u32_array(const struct device_node *np, const char *propname,
470 u32 *out_values, size_t sz)
474 debug("%s: %s: ", __func__, propname);
475 val = of_find_property_value_of_size(np, propname,
476 sz * sizeof(*out_values));
481 debug("size %zd\n", sz);
483 *out_values++ = be32_to_cpup(val++);
488 int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
492 debug("%s: %s: ", __func__, propname);
495 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
497 debug("(not found)\n");
501 *outp = be64_to_cpup(val);
502 debug("%#llx (%lld)\n", (unsigned long long)*outp,
503 (unsigned long long)*outp);
508 int of_property_match_string(const struct device_node *np, const char *propname,
511 const struct property *prop = of_find_property(np, propname, NULL);
522 end = p + prop->length;
524 for (i = 0; p < end; i++, p += l) {
525 l = strnlen(p, end - p) + 1;
528 debug("comparing %s with %s\n", string, p);
529 if (strcmp(string, p) == 0)
530 return i; /* Found it; return index */
536 * of_property_read_string_helper() - Utility helper for parsing string properties
537 * @np: device node from which the property value is to be read.
538 * @propname: name of the property to be searched.
539 * @out_strs: output array of string pointers.
540 * @sz: number of array elements to read.
541 * @skip: Number of strings to skip over at beginning of list.
543 * Don't call this function directly. It is a utility helper for the
544 * of_property_read_string*() family of functions.
546 int of_property_read_string_helper(const struct device_node *np,
547 const char *propname, const char **out_strs,
550 const struct property *prop = of_find_property(np, propname, NULL);
559 end = p + prop->length;
561 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
562 l = strnlen(p, end - p) + 1;
565 if (out_strs && i >= skip)
569 return i <= 0 ? -ENODATA : i;
572 static int __of_parse_phandle_with_args(const struct device_node *np,
573 const char *list_name,
574 const char *cells_name,
575 int cell_count, int index,
576 struct of_phandle_args *out_args)
578 const __be32 *list, *list_end;
579 int rc = 0, cur_index = 0;
581 struct device_node *node = NULL;
585 /* Retrieve the phandle list property */
586 list = of_get_property(np, list_name, &size);
589 list_end = list + size / sizeof(*list);
591 /* Loop over the phandles until all the requested entry is found */
592 while (list < list_end) {
597 * If phandle is 0, then it is an empty entry with no
598 * arguments. Skip forward to the next entry.
600 phandle = be32_to_cpup(list++);
603 * Find the provider node and parse the #*-cells
604 * property to determine the argument length.
606 * This is not needed if the cell count is hard-coded
607 * (i.e. cells_name not set, but cell_count is set),
608 * except when we're going to return the found node
611 if (cells_name || cur_index == index) {
612 node = of_find_node_by_phandle(phandle);
614 debug("%s: could not find phandle\n",
621 if (of_read_u32(node, cells_name, &count)) {
622 debug("%s: could not get %s for %s\n",
623 np->full_name, cells_name,
632 * Make sure that the arguments actually fit in the
633 * remaining property data length
635 if (list + count > list_end) {
636 debug("%s: arguments longer than property\n",
643 * All of the error cases above bail out of the loop, so at
644 * this point, the parsing is successful. If the requested
645 * index matches, then fill the out_args structure and return,
646 * or return -ENOENT for an empty entry.
649 if (cur_index == index) {
655 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
656 count = OF_MAX_PHANDLE_ARGS;
658 out_args->args_count = count;
659 for (i = 0; i < count; i++)
661 be32_to_cpup(list++);
666 /* Found it! return success */
677 * Unlock node before returning result; will be one of:
678 * -ENOENT : index is for empty phandle
679 * -EINVAL : parsing error on data
680 * [1..n] : Number of phandle (count mode; when index = -1)
682 rc = index < 0 ? cur_index : -ENOENT;
689 struct device_node *of_parse_phandle(const struct device_node *np,
690 const char *phandle_name, int index)
692 struct of_phandle_args args;
697 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
704 int of_parse_phandle_with_args(const struct device_node *np,
705 const char *list_name, const char *cells_name,
706 int index, struct of_phandle_args *out_args)
711 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
715 int of_count_phandle_with_args(const struct device_node *np,
716 const char *list_name, const char *cells_name)
718 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
722 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
723 int id, const char *stem, int stem_len)
727 strncpy(ap->stem, stem, stem_len);
728 ap->stem[stem_len] = 0;
729 list_add_tail(&ap->link, &aliases_lookup);
730 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
731 ap->alias, ap->stem, ap->id, of_node_full_name(np));
734 int of_alias_scan(void)
738 of_aliases = of_find_node_by_path("/aliases");
739 of_chosen = of_find_node_by_path("/chosen");
740 if (of_chosen == NULL)
741 of_chosen = of_find_node_by_path("/chosen@0");
746 name = of_get_property(of_chosen, "stdout-path", NULL);
748 of_stdout = of_find_node_opts_by_path(name,
755 for_each_property_of_node(of_aliases, pp) {
756 const char *start = pp->name;
757 const char *end = start + strlen(start);
758 struct device_node *np;
759 struct alias_prop *ap;
763 /* Skip those we do not want to proceed */
764 if (!strcmp(pp->name, "name") ||
765 !strcmp(pp->name, "phandle") ||
766 !strcmp(pp->name, "linux,phandle"))
769 np = of_find_node_by_path(pp->value);
774 * walk the alias backwards to extract the id and work out
777 while (isdigit(*(end-1)) && end > start)
781 if (strict_strtoul(end, 10, &id) < 0)
784 /* Allocate an alias_prop with enough space for the stem */
785 ap = malloc(sizeof(*ap) + len + 1);
788 memset(ap, 0, sizeof(*ap) + len + 1);
790 of_alias_add(ap, np, id, start, len);
796 int of_alias_get_id(const struct device_node *np, const char *stem)
798 struct alias_prop *app;
801 mutex_lock(&of_mutex);
802 list_for_each_entry(app, &aliases_lookup, link) {
803 if (strcmp(app->stem, stem) != 0)
811 mutex_unlock(&of_mutex);
816 int of_alias_get_highest_id(const char *stem)
818 struct alias_prop *app;
821 mutex_lock(&of_mutex);
822 list_for_each_entry(app, &aliases_lookup, link) {
823 if (strcmp(app->stem, stem) != 0)
829 mutex_unlock(&of_mutex);
834 struct device_node *of_get_stdout(void)