blob: c54baa140adc98a2cb1a4d49147f9ae70f0466ef [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass1ccaa052017-05-18 20:08:54 -06002/*
3 * Originally from Linux v4.9
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
6 *
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
9 *
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11 *
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
14 *
15 * Modified for U-Boot
16 * Copyright (c) 2017 Google, Inc
17 *
18 * This file follows drivers/of/base.c with functions in the same order as the
19 * Linux version.
Simon Glass1ccaa052017-05-18 20:08:54 -060020 */
21
22#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -070023#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090024#include <linux/libfdt.h>
Simon Glass1ccaa052017-05-18 20:08:54 -060025#include <dm/of_access.h>
26#include <linux/ctype.h>
27#include <linux/err.h>
28#include <linux/ioport.h>
29
30DECLARE_GLOBAL_DATA_PTR;
31
32/* list of struct alias_prop aliases */
33LIST_HEAD(aliases_lookup);
34
35/* "/aliaes" node */
36static struct device_node *of_aliases;
37
38/* "/chosen" node */
39static struct device_node *of_chosen;
40
41/* node pointed to by the stdout-path alias */
42static struct device_node *of_stdout;
43
44/* pointer to options given after the alias (separated by :) or NULL if none */
45static const char *of_stdout_options;
46
47/**
48 * struct alias_prop - Alias property in 'aliases' node
49 *
50 * The structure represents one alias property of 'aliases' node as
51 * an entry in aliases_lookup list.
52 *
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
58 */
59struct alias_prop {
60 struct list_head link;
61 const char *alias;
62 struct device_node *np;
63 int id;
64 char stem[0];
65};
66
67int of_n_addr_cells(const struct device_node *np)
68{
69 const __be32 *ip;
70
71 do {
72 if (np->parent)
73 np = np->parent;
74 ip = of_get_property(np, "#address-cells", NULL);
75 if (ip)
76 return be32_to_cpup(ip);
77 } while (np->parent);
78
79 /* No #address-cells property for the root node */
80 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
81}
82
83int of_n_size_cells(const struct device_node *np)
84{
85 const __be32 *ip;
86
87 do {
88 if (np->parent)
89 np = np->parent;
90 ip = of_get_property(np, "#size-cells", NULL);
91 if (ip)
92 return be32_to_cpup(ip);
93 } while (np->parent);
94
95 /* No #size-cells property for the root node */
96 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
97}
98
Simon Glass4191dc12017-06-12 06:21:31 -060099int of_simple_addr_cells(const struct device_node *np)
100{
101 const __be32 *ip;
102
103 ip = of_get_property(np, "#address-cells", NULL);
104 if (ip)
105 return be32_to_cpup(ip);
106
107 /* Return a default of 2 to match fdt_address_cells()*/
108 return 2;
109}
110
111int of_simple_size_cells(const struct device_node *np)
112{
113 const __be32 *ip;
114
115 ip = of_get_property(np, "#size-cells", NULL);
116 if (ip)
117 return be32_to_cpup(ip);
118
119 /* Return a default of 2 to match fdt_size_cells()*/
120 return 2;
121}
122
Simon Glass1ccaa052017-05-18 20:08:54 -0600123struct property *of_find_property(const struct device_node *np,
124 const char *name, int *lenp)
125{
126 struct property *pp;
127
128 if (!np)
129 return NULL;
130
131 for (pp = np->properties; pp; pp = pp->next) {
132 if (strcmp(pp->name, name) == 0) {
133 if (lenp)
134 *lenp = pp->length;
135 break;
136 }
137 }
138 if (!pp && lenp)
139 *lenp = -FDT_ERR_NOTFOUND;
140
141 return pp;
142}
143
144struct device_node *of_find_all_nodes(struct device_node *prev)
145{
146 struct device_node *np;
147
148 if (!prev) {
149 np = gd->of_root;
150 } else if (prev->child) {
151 np = prev->child;
152 } else {
153 /*
154 * Walk back up looking for a sibling, or the end of the
155 * structure
156 */
157 np = prev;
158 while (np->parent && !np->sibling)
159 np = np->parent;
160 np = np->sibling; /* Might be null at the end of the tree */
161 }
162
163 return np;
164}
165
166const void *of_get_property(const struct device_node *np, const char *name,
167 int *lenp)
168{
169 struct property *pp = of_find_property(np, name, lenp);
170
171 return pp ? pp->value : NULL;
172}
173
174static const char *of_prop_next_string(struct property *prop, const char *cur)
175{
176 const void *curv = cur;
177
178 if (!prop)
179 return NULL;
180
181 if (!cur)
182 return prop->value;
183
184 curv += strlen(cur) + 1;
185 if (curv >= prop->value + prop->length)
186 return NULL;
187
188 return curv;
189}
190
191int of_device_is_compatible(const struct device_node *device,
192 const char *compat, const char *type,
193 const char *name)
194{
195 struct property *prop;
196 const char *cp;
197 int index = 0, score = 0;
198
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);
206 break;
207 }
208 }
209 if (!score)
210 return 0;
211 }
212
213 /* Matching type is better than matching name */
214 if (type && type[0]) {
215 if (!device->type || of_node_cmp(type, device->type))
216 return 0;
217 score += 2;
218 }
219
220 /* Matching name is a bit better than not */
221 if (name && name[0]) {
222 if (!device->name || of_node_cmp(name, device->name))
223 return 0;
224 score++;
225 }
226
227 return score;
228}
229
230bool of_device_is_available(const struct device_node *device)
231{
232 const char *status;
233 int statlen;
234
235 if (!device)
236 return false;
237
238 status = of_get_property(device, "status", &statlen);
239 if (status == NULL)
240 return true;
241
242 if (statlen > 0) {
243 if (!strcmp(status, "okay"))
244 return true;
245 }
246
247 return false;
248}
249
250struct device_node *of_get_parent(const struct device_node *node)
251{
252 const struct device_node *np;
253
254 if (!node)
255 return NULL;
256
257 np = of_node_get(node->parent);
258
259 return (struct device_node *)np;
260}
261
262static struct device_node *__of_get_next_child(const struct device_node *node,
263 struct device_node *prev)
264{
265 struct device_node *next;
266
267 if (!node)
268 return NULL;
269
270 next = prev ? prev->sibling : node->child;
Simon Glass3694ac52017-06-07 10:28:45 -0600271 /*
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.
276 */
Simon Glass1ccaa052017-05-18 20:08:54 -0600277 for (; next; next = next->sibling)
278 if (of_node_get(next))
279 break;
280 of_node_put(prev);
281 return next;
282}
283
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))
287
288static struct device_node *__of_find_node_by_path(struct device_node *parent,
289 const char *path)
290{
291 struct device_node *child;
292 int len;
293
294 len = strcspn(path, "/:");
295 if (!len)
296 return NULL;
297
298 __for_each_child_of_node(parent, child) {
299 const char *name = strrchr(child->full_name, '/');
300
301 name++;
302 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
303 return child;
304 }
305 return NULL;
306}
307
308#define for_each_property_of_node(dn, pp) \
309 for (pp = dn->properties; pp != NULL; pp = pp->next)
310
311struct device_node *of_find_node_opts_by_path(const char *path,
312 const char **opts)
313{
314 struct device_node *np = NULL;
315 struct property *pp;
316 const char *separator = strchr(path, ':');
317
318 if (opts)
319 *opts = separator ? separator + 1 : NULL;
320
321 if (strcmp(path, "/") == 0)
322 return of_node_get(gd->of_root);
323
324 /* The path could begin with an alias */
325 if (*path != '/') {
326 int len;
327 const char *p = separator;
328
329 if (!p)
330 p = strchrnul(path, '/');
331 len = p - path;
332
333 /* of_aliases must not be NULL */
334 if (!of_aliases)
335 return NULL;
336
337 for_each_property_of_node(of_aliases, pp) {
338 if (strlen(pp->name) == len && !strncmp(pp->name, path,
339 len)) {
340 np = of_find_node_by_path(pp->value);
341 break;
342 }
343 }
344 if (!np)
345 return NULL;
346 path = p;
347 }
348
349 /* Step down the tree matching path components */
350 if (!np)
351 np = of_node_get(gd->of_root);
352 while (np && *path == '/') {
353 struct device_node *tmp = np;
354
355 path++; /* Increment past '/' delimiter */
356 np = __of_find_node_by_path(np, path);
357 of_node_put(tmp);
358 path = strchrnul(path, '/');
359 if (separator && separator < path)
360 break;
361 }
362
363 return np;
364}
365
366struct device_node *of_find_compatible_node(struct device_node *from,
367 const char *type, const char *compatible)
368{
369 struct device_node *np;
370
371 for_each_of_allnodes_from(from, np)
372 if (of_device_is_compatible(np, compatible, type, NULL) &&
373 of_node_get(np))
374 break;
375 of_node_put(from);
376
377 return np;
378}
379
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200380static int of_device_has_prop_value(const struct device_node *device,
381 const char *propname, const void *propval,
382 int proplen)
383{
384 struct property *prop = of_find_property(device, propname, NULL);
385
386 if (!prop || !prop->value || prop->length != proplen)
387 return 0;
388 return !memcmp(prop->value, propval, proplen);
389}
390
391struct device_node *of_find_node_by_prop_value(struct device_node *from,
392 const char *propname,
393 const void *propval, int proplen)
394{
395 struct device_node *np;
396
397 for_each_of_allnodes_from(from, np) {
398 if (of_device_has_prop_value(np, propname, propval, proplen) &&
399 of_node_get(np))
400 break;
401 }
402 of_node_put(from);
403
404 return np;
405}
406
Simon Glass1ccaa052017-05-18 20:08:54 -0600407struct device_node *of_find_node_by_phandle(phandle handle)
408{
409 struct device_node *np;
410
411 if (!handle)
412 return NULL;
413
414 for_each_of_allnodes(np)
415 if (np->phandle == handle)
416 break;
417 (void)of_node_get(np);
418
419 return np;
420}
421
422/**
423 * of_find_property_value_of_size() - find property of given size
424 *
425 * Search for a property in a device node and validate the requested size.
426 *
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
430 *
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.
434 */
435static void *of_find_property_value_of_size(const struct device_node *np,
436 const char *propname, u32 len)
437{
438 struct property *prop = of_find_property(np, propname, NULL);
439
440 if (!prop)
441 return ERR_PTR(-EINVAL);
442 if (!prop->value)
443 return ERR_PTR(-ENODATA);
444 if (len > prop->length)
445 return ERR_PTR(-EOVERFLOW);
446
447 return prop->value;
448}
449
450int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
451{
Dario Binacchib3f1cdd2020-03-29 18:04:42 +0200452 return of_read_u32_index(np, propname, 0, outp);
Simon Glass1ccaa052017-05-18 20:08:54 -0600453}
454
455int of_read_u32_array(const struct device_node *np, const char *propname,
456 u32 *out_values, size_t sz)
457{
458 const __be32 *val;
459
460 debug("%s: %s: ", __func__, propname);
461 val = of_find_property_value_of_size(np, propname,
462 sz * sizeof(*out_values));
463
464 if (IS_ERR(val))
465 return PTR_ERR(val);
466
467 debug("size %zd\n", sz);
468 while (sz--)
469 *out_values++ = be32_to_cpup(val++);
470
471 return 0;
472}
473
Dario Binacchi81d80b52020-03-29 18:04:41 +0200474int of_read_u32_index(const struct device_node *np, const char *propname,
475 int index, u32 *outp)
476{
477 const __be32 *val;
478
479 debug("%s: %s: ", __func__, propname);
480 if (!np)
481 return -EINVAL;
482
483 val = of_find_property_value_of_size(np, propname,
484 sizeof(*outp) * (index + 1));
485 if (IS_ERR(val)) {
486 debug("(not found)\n");
487 return PTR_ERR(val);
488 }
489
490 *outp = be32_to_cpup(val + index);
491 debug("%#x (%d)\n", *outp, *outp);
492
493 return 0;
494}
495
Simon Glass9d54a7a2018-06-11 13:07:10 -0600496int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
497{
498 const __be64 *val;
499
500 debug("%s: %s: ", __func__, propname);
501 if (!np)
502 return -EINVAL;
503 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
504 if (IS_ERR(val)) {
505 debug("(not found)\n");
506 return PTR_ERR(val);
507 }
508
509 *outp = be64_to_cpup(val);
510 debug("%#llx (%lld)\n", (unsigned long long)*outp,
511 (unsigned long long)*outp);
512
513 return 0;
514}
515
Simon Glass1ccaa052017-05-18 20:08:54 -0600516int of_property_match_string(const struct device_node *np, const char *propname,
517 const char *string)
518{
519 const struct property *prop = of_find_property(np, propname, NULL);
520 size_t l;
521 int i;
522 const char *p, *end;
523
524 if (!prop)
525 return -EINVAL;
526 if (!prop->value)
527 return -ENODATA;
528
529 p = prop->value;
530 end = p + prop->length;
531
532 for (i = 0; p < end; i++, p += l) {
533 l = strnlen(p, end - p) + 1;
534 if (p + l > end)
535 return -EILSEQ;
536 debug("comparing %s with %s\n", string, p);
537 if (strcmp(string, p) == 0)
538 return i; /* Found it; return index */
539 }
540 return -ENODATA;
541}
542
543/**
544 * of_property_read_string_helper() - Utility helper for parsing string properties
545 * @np: device node from which the property value is to be read.
546 * @propname: name of the property to be searched.
547 * @out_strs: output array of string pointers.
548 * @sz: number of array elements to read.
549 * @skip: Number of strings to skip over at beginning of list.
550 *
551 * Don't call this function directly. It is a utility helper for the
552 * of_property_read_string*() family of functions.
553 */
554int of_property_read_string_helper(const struct device_node *np,
555 const char *propname, const char **out_strs,
556 size_t sz, int skip)
557{
558 const struct property *prop = of_find_property(np, propname, NULL);
559 int l = 0, i = 0;
560 const char *p, *end;
561
562 if (!prop)
563 return -EINVAL;
564 if (!prop->value)
565 return -ENODATA;
566 p = prop->value;
567 end = p + prop->length;
568
569 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
570 l = strnlen(p, end - p) + 1;
571 if (p + l > end)
572 return -EILSEQ;
573 if (out_strs && i >= skip)
574 *out_strs++ = p;
575 }
576 i -= skip;
577 return i <= 0 ? -ENODATA : i;
578}
579
580static int __of_parse_phandle_with_args(const struct device_node *np,
581 const char *list_name,
582 const char *cells_name,
583 int cell_count, int index,
584 struct of_phandle_args *out_args)
585{
586 const __be32 *list, *list_end;
587 int rc = 0, cur_index = 0;
Heinrich Schuchardtc13346e2020-02-15 21:46:04 +0100588 uint32_t count;
Simon Glass1ccaa052017-05-18 20:08:54 -0600589 struct device_node *node = NULL;
590 phandle phandle;
591 int size;
592
593 /* Retrieve the phandle list property */
594 list = of_get_property(np, list_name, &size);
595 if (!list)
596 return -ENOENT;
597 list_end = list + size / sizeof(*list);
598
599 /* Loop over the phandles until all the requested entry is found */
600 while (list < list_end) {
601 rc = -EINVAL;
602 count = 0;
603
604 /*
605 * If phandle is 0, then it is an empty entry with no
606 * arguments. Skip forward to the next entry.
607 */
608 phandle = be32_to_cpup(list++);
609 if (phandle) {
610 /*
611 * Find the provider node and parse the #*-cells
612 * property to determine the argument length.
613 *
614 * This is not needed if the cell count is hard-coded
615 * (i.e. cells_name not set, but cell_count is set),
616 * except when we're going to return the found node
617 * below.
618 */
619 if (cells_name || cur_index == index) {
620 node = of_find_node_by_phandle(phandle);
621 if (!node) {
622 debug("%s: could not find phandle\n",
623 np->full_name);
624 goto err;
625 }
626 }
627
628 if (cells_name) {
629 if (of_read_u32(node, cells_name, &count)) {
630 debug("%s: could not get %s for %s\n",
631 np->full_name, cells_name,
632 node->full_name);
633 goto err;
634 }
635 } else {
636 count = cell_count;
637 }
638
639 /*
640 * Make sure that the arguments actually fit in the
641 * remaining property data length
642 */
643 if (list + count > list_end) {
644 debug("%s: arguments longer than property\n",
645 np->full_name);
646 goto err;
647 }
648 }
649
650 /*
651 * All of the error cases above bail out of the loop, so at
652 * this point, the parsing is successful. If the requested
653 * index matches, then fill the out_args structure and return,
654 * or return -ENOENT for an empty entry.
655 */
656 rc = -ENOENT;
657 if (cur_index == index) {
658 if (!phandle)
659 goto err;
660
661 if (out_args) {
662 int i;
663 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
664 count = OF_MAX_PHANDLE_ARGS;
665 out_args->np = node;
666 out_args->args_count = count;
667 for (i = 0; i < count; i++)
668 out_args->args[i] =
669 be32_to_cpup(list++);
670 } else {
671 of_node_put(node);
672 }
673
674 /* Found it! return success */
675 return 0;
676 }
677
678 of_node_put(node);
679 node = NULL;
680 list += count;
681 cur_index++;
682 }
683
684 /*
685 * Unlock node before returning result; will be one of:
686 * -ENOENT : index is for empty phandle
687 * -EINVAL : parsing error on data
688 * [1..n] : Number of phandle (count mode; when index = -1)
689 */
690 rc = index < 0 ? cur_index : -ENOENT;
691 err:
692 if (node)
693 of_node_put(node);
694 return rc;
695}
696
697struct device_node *of_parse_phandle(const struct device_node *np,
698 const char *phandle_name, int index)
699{
700 struct of_phandle_args args;
701
702 if (index < 0)
703 return NULL;
704
705 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
706 &args))
707 return NULL;
708
709 return args.np;
710}
711
712int of_parse_phandle_with_args(const struct device_node *np,
713 const char *list_name, const char *cells_name,
714 int index, struct of_phandle_args *out_args)
715{
716 if (index < 0)
717 return -EINVAL;
718
719 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
720 index, out_args);
721}
722
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200723int of_count_phandle_with_args(const struct device_node *np,
724 const char *list_name, const char *cells_name)
725{
726 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
727 -1, NULL);
728}
729
Simon Glass1ccaa052017-05-18 20:08:54 -0600730static void of_alias_add(struct alias_prop *ap, struct device_node *np,
731 int id, const char *stem, int stem_len)
732{
733 ap->np = np;
734 ap->id = id;
735 strncpy(ap->stem, stem, stem_len);
736 ap->stem[stem_len] = 0;
737 list_add_tail(&ap->link, &aliases_lookup);
738 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
739 ap->alias, ap->stem, ap->id, of_node_full_name(np));
740}
741
742int of_alias_scan(void)
743{
744 struct property *pp;
745
746 of_aliases = of_find_node_by_path("/aliases");
747 of_chosen = of_find_node_by_path("/chosen");
748 if (of_chosen == NULL)
749 of_chosen = of_find_node_by_path("/chosen@0");
750
751 if (of_chosen) {
752 const char *name;
753
754 name = of_get_property(of_chosen, "stdout-path", NULL);
755 if (name)
756 of_stdout = of_find_node_opts_by_path(name,
757 &of_stdout_options);
758 }
759
760 if (!of_aliases)
761 return 0;
762
763 for_each_property_of_node(of_aliases, pp) {
764 const char *start = pp->name;
765 const char *end = start + strlen(start);
766 struct device_node *np;
767 struct alias_prop *ap;
768 ulong id;
769 int len;
770
771 /* Skip those we do not want to proceed */
772 if (!strcmp(pp->name, "name") ||
773 !strcmp(pp->name, "phandle") ||
774 !strcmp(pp->name, "linux,phandle"))
775 continue;
776
777 np = of_find_node_by_path(pp->value);
778 if (!np)
779 continue;
780
781 /*
782 * walk the alias backwards to extract the id and work out
783 * the 'stem' string
784 */
785 while (isdigit(*(end-1)) && end > start)
786 end--;
787 len = end - start;
788
789 if (strict_strtoul(end, 10, &id) < 0)
790 continue;
791
792 /* Allocate an alias_prop with enough space for the stem */
793 ap = malloc(sizeof(*ap) + len + 1);
794 if (!ap)
795 return -ENOMEM;
796 memset(ap, 0, sizeof(*ap) + len + 1);
797 ap->alias = start;
798 of_alias_add(ap, np, id, start, len);
799 }
800
801 return 0;
802}
803
804int of_alias_get_id(const struct device_node *np, const char *stem)
805{
806 struct alias_prop *app;
807 int id = -ENODEV;
808
809 mutex_lock(&of_mutex);
810 list_for_each_entry(app, &aliases_lookup, link) {
811 if (strcmp(app->stem, stem) != 0)
812 continue;
813
814 if (np == app->np) {
815 id = app->id;
816 break;
817 }
818 }
819 mutex_unlock(&of_mutex);
820
821 return id;
822}
823
Michal Simekc6203cb2019-01-31 16:30:57 +0100824int of_alias_get_highest_id(const char *stem)
825{
826 struct alias_prop *app;
827 int id = -1;
828
829 mutex_lock(&of_mutex);
830 list_for_each_entry(app, &aliases_lookup, link) {
831 if (strcmp(app->stem, stem) != 0)
832 continue;
833
834 if (app->id > id)
835 id = app->id;
836 }
837 mutex_unlock(&of_mutex);
838
839 return id;
840}
841
Simon Glass1ccaa052017-05-18 20:08:54 -0600842struct device_node *of_get_stdout(void)
843{
844 return of_stdout;
845}