2 * Originally from Linux v4.9
3 * Paul Mackerras August 1996.
4 * Copyright (C) 1996-2005 Paul Mackerras.
6 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
7 * {engebret|bergner}@us.ibm.com
11 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
15 * Copyright (c) 2017 Google, Inc
17 * This file follows drivers/of/base.c with functions in the same order as the
20 * SPDX-License-Identifier: GPL-2.0+
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 struct property *of_find_property(const struct device_node *np,
100 const char *name, int *lenp)
107 for (pp = np->properties; pp; pp = pp->next) {
108 if (strcmp(pp->name, name) == 0) {
115 *lenp = -FDT_ERR_NOTFOUND;
120 struct device_node *of_find_all_nodes(struct device_node *prev)
122 struct device_node *np;
126 } else if (prev->child) {
130 * Walk back up looking for a sibling, or the end of the
134 while (np->parent && !np->sibling)
136 np = np->sibling; /* Might be null at the end of the tree */
142 const void *of_get_property(const struct device_node *np, const char *name,
145 struct property *pp = of_find_property(np, name, lenp);
147 return pp ? pp->value : NULL;
150 static const char *of_prop_next_string(struct property *prop, const char *cur)
152 const void *curv = cur;
160 curv += strlen(cur) + 1;
161 if (curv >= prop->value + prop->length)
167 int of_device_is_compatible(const struct device_node *device,
168 const char *compat, const char *type,
171 struct property *prop;
173 int index = 0, score = 0;
175 /* Compatible match has highest priority */
176 if (compat && compat[0]) {
177 prop = of_find_property(device, "compatible", NULL);
178 for (cp = of_prop_next_string(prop, NULL); cp;
179 cp = of_prop_next_string(prop, cp), index++) {
180 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
181 score = INT_MAX/2 - (index << 2);
189 /* Matching type is better than matching name */
190 if (type && type[0]) {
191 if (!device->type || of_node_cmp(type, device->type))
196 /* Matching name is a bit better than not */
197 if (name && name[0]) {
198 if (!device->name || of_node_cmp(name, device->name))
206 bool of_device_is_available(const struct device_node *device)
214 status = of_get_property(device, "status", &statlen);
219 if (!strcmp(status, "okay"))
226 struct device_node *of_get_parent(const struct device_node *node)
228 const struct device_node *np;
233 np = of_node_get(node->parent);
235 return (struct device_node *)np;
238 static struct device_node *__of_get_next_child(const struct device_node *node,
239 struct device_node *prev)
241 struct device_node *next;
246 next = prev ? prev->sibling : node->child;
247 for (; next; next = next->sibling)
248 if (of_node_get(next))
254 #define __for_each_child_of_node(parent, child) \
255 for (child = __of_get_next_child(parent, NULL); child != NULL; \
256 child = __of_get_next_child(parent, child))
258 static struct device_node *__of_find_node_by_path(struct device_node *parent,
261 struct device_node *child;
264 len = strcspn(path, "/:");
268 __for_each_child_of_node(parent, child) {
269 const char *name = strrchr(child->full_name, '/');
272 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
278 #define for_each_property_of_node(dn, pp) \
279 for (pp = dn->properties; pp != NULL; pp = pp->next)
281 struct device_node *of_find_node_opts_by_path(const char *path,
284 struct device_node *np = NULL;
286 const char *separator = strchr(path, ':');
289 *opts = separator ? separator + 1 : NULL;
291 if (strcmp(path, "/") == 0)
292 return of_node_get(gd->of_root);
294 /* The path could begin with an alias */
297 const char *p = separator;
300 p = strchrnul(path, '/');
303 /* of_aliases must not be NULL */
307 for_each_property_of_node(of_aliases, pp) {
308 if (strlen(pp->name) == len && !strncmp(pp->name, path,
310 np = of_find_node_by_path(pp->value);
319 /* Step down the tree matching path components */
321 np = of_node_get(gd->of_root);
322 while (np && *path == '/') {
323 struct device_node *tmp = np;
325 path++; /* Increment past '/' delimiter */
326 np = __of_find_node_by_path(np, path);
328 path = strchrnul(path, '/');
329 if (separator && separator < path)
336 struct device_node *of_find_compatible_node(struct device_node *from,
337 const char *type, const char *compatible)
339 struct device_node *np;
341 for_each_of_allnodes_from(from, np)
342 if (of_device_is_compatible(np, compatible, type, NULL) &&
350 struct device_node *of_find_node_by_phandle(phandle handle)
352 struct device_node *np;
357 for_each_of_allnodes(np)
358 if (np->phandle == handle)
360 (void)of_node_get(np);
366 * of_find_property_value_of_size() - find property of given size
368 * Search for a property in a device node and validate the requested size.
370 * @np: device node from which the property value is to be read.
371 * @propname: name of the property to be searched.
372 * @len: requested length of property value
374 * @return the property value on success, -EINVAL if the property does not
375 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
376 * property data isn't large enough.
378 static void *of_find_property_value_of_size(const struct device_node *np,
379 const char *propname, u32 len)
381 struct property *prop = of_find_property(np, propname, NULL);
384 return ERR_PTR(-EINVAL);
386 return ERR_PTR(-ENODATA);
387 if (len > prop->length)
388 return ERR_PTR(-EOVERFLOW);
393 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
397 debug("%s: %s: ", __func__, propname);
400 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
402 debug("(not found)\n");
406 *outp = be32_to_cpup(val);
407 debug("%#x (%d)\n", *outp, *outp);
412 int of_read_u32_array(const struct device_node *np, const char *propname,
413 u32 *out_values, size_t sz)
417 debug("%s: %s: ", __func__, propname);
418 val = of_find_property_value_of_size(np, propname,
419 sz * sizeof(*out_values));
424 debug("size %zd\n", sz);
426 *out_values++ = be32_to_cpup(val++);
431 int of_property_match_string(const struct device_node *np, const char *propname,
434 const struct property *prop = of_find_property(np, propname, NULL);
445 end = p + prop->length;
447 for (i = 0; p < end; i++, p += l) {
448 l = strnlen(p, end - p) + 1;
451 debug("comparing %s with %s\n", string, p);
452 if (strcmp(string, p) == 0)
453 return i; /* Found it; return index */
459 * of_property_read_string_helper() - Utility helper for parsing string properties
460 * @np: device node from which the property value is to be read.
461 * @propname: name of the property to be searched.
462 * @out_strs: output array of string pointers.
463 * @sz: number of array elements to read.
464 * @skip: Number of strings to skip over at beginning of list.
466 * Don't call this function directly. It is a utility helper for the
467 * of_property_read_string*() family of functions.
469 int of_property_read_string_helper(const struct device_node *np,
470 const char *propname, const char **out_strs,
473 const struct property *prop = of_find_property(np, propname, NULL);
482 end = p + prop->length;
484 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
485 l = strnlen(p, end - p) + 1;
488 if (out_strs && i >= skip)
492 return i <= 0 ? -ENODATA : i;
495 static int __of_parse_phandle_with_args(const struct device_node *np,
496 const char *list_name,
497 const char *cells_name,
498 int cell_count, int index,
499 struct of_phandle_args *out_args)
501 const __be32 *list, *list_end;
502 int rc = 0, cur_index = 0;
504 struct device_node *node = NULL;
508 /* Retrieve the phandle list property */
509 list = of_get_property(np, list_name, &size);
512 list_end = list + size / sizeof(*list);
514 /* Loop over the phandles until all the requested entry is found */
515 while (list < list_end) {
520 * If phandle is 0, then it is an empty entry with no
521 * arguments. Skip forward to the next entry.
523 phandle = be32_to_cpup(list++);
526 * Find the provider node and parse the #*-cells
527 * property to determine the argument length.
529 * This is not needed if the cell count is hard-coded
530 * (i.e. cells_name not set, but cell_count is set),
531 * except when we're going to return the found node
534 if (cells_name || cur_index == index) {
535 node = of_find_node_by_phandle(phandle);
537 debug("%s: could not find phandle\n",
544 if (of_read_u32(node, cells_name, &count)) {
545 debug("%s: could not get %s for %s\n",
546 np->full_name, cells_name,
555 * Make sure that the arguments actually fit in the
556 * remaining property data length
558 if (list + count > list_end) {
559 debug("%s: arguments longer than property\n",
566 * All of the error cases above bail out of the loop, so at
567 * this point, the parsing is successful. If the requested
568 * index matches, then fill the out_args structure and return,
569 * or return -ENOENT for an empty entry.
572 if (cur_index == index) {
578 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
579 count = OF_MAX_PHANDLE_ARGS;
581 out_args->args_count = count;
582 for (i = 0; i < count; i++)
584 be32_to_cpup(list++);
589 /* Found it! return success */
600 * Unlock node before returning result; will be one of:
601 * -ENOENT : index is for empty phandle
602 * -EINVAL : parsing error on data
603 * [1..n] : Number of phandle (count mode; when index = -1)
605 rc = index < 0 ? cur_index : -ENOENT;
612 struct device_node *of_parse_phandle(const struct device_node *np,
613 const char *phandle_name, int index)
615 struct of_phandle_args args;
620 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
627 int of_parse_phandle_with_args(const struct device_node *np,
628 const char *list_name, const char *cells_name,
629 int index, struct of_phandle_args *out_args)
634 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
638 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
639 int id, const char *stem, int stem_len)
643 strncpy(ap->stem, stem, stem_len);
644 ap->stem[stem_len] = 0;
645 list_add_tail(&ap->link, &aliases_lookup);
646 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
647 ap->alias, ap->stem, ap->id, of_node_full_name(np));
650 int of_alias_scan(void)
654 of_aliases = of_find_node_by_path("/aliases");
655 of_chosen = of_find_node_by_path("/chosen");
656 if (of_chosen == NULL)
657 of_chosen = of_find_node_by_path("/chosen@0");
662 name = of_get_property(of_chosen, "stdout-path", NULL);
664 of_stdout = of_find_node_opts_by_path(name,
671 for_each_property_of_node(of_aliases, pp) {
672 const char *start = pp->name;
673 const char *end = start + strlen(start);
674 struct device_node *np;
675 struct alias_prop *ap;
679 /* Skip those we do not want to proceed */
680 if (!strcmp(pp->name, "name") ||
681 !strcmp(pp->name, "phandle") ||
682 !strcmp(pp->name, "linux,phandle"))
685 np = of_find_node_by_path(pp->value);
690 * walk the alias backwards to extract the id and work out
693 while (isdigit(*(end-1)) && end > start)
697 if (strict_strtoul(end, 10, &id) < 0)
700 /* Allocate an alias_prop with enough space for the stem */
701 ap = malloc(sizeof(*ap) + len + 1);
704 memset(ap, 0, sizeof(*ap) + len + 1);
706 of_alias_add(ap, np, id, start, len);
712 int of_alias_get_id(const struct device_node *np, const char *stem)
714 struct alias_prop *app;
717 mutex_lock(&of_mutex);
718 list_for_each_entry(app, &aliases_lookup, link) {
719 if (strcmp(app->stem, stem) != 0)
727 mutex_unlock(&of_mutex);
732 struct device_node *of_get_stdout(void)