blob: 14c020a687b75c97d6fc85e8d8f33b8e95f652c6 [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>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090023#include <linux/libfdt.h>
Simon Glass1ccaa052017-05-18 20:08:54 -060024#include <dm/of_access.h>
25#include <linux/ctype.h>
26#include <linux/err.h>
27#include <linux/ioport.h>
28
29DECLARE_GLOBAL_DATA_PTR;
30
31/* list of struct alias_prop aliases */
32LIST_HEAD(aliases_lookup);
33
34/* "/aliaes" node */
35static struct device_node *of_aliases;
36
37/* "/chosen" node */
38static struct device_node *of_chosen;
39
40/* node pointed to by the stdout-path alias */
41static struct device_node *of_stdout;
42
43/* pointer to options given after the alias (separated by :) or NULL if none */
44static const char *of_stdout_options;
45
46/**
47 * struct alias_prop - Alias property in 'aliases' node
48 *
49 * The structure represents one alias property of 'aliases' node as
50 * an entry in aliases_lookup list.
51 *
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
57 */
58struct alias_prop {
59 struct list_head link;
60 const char *alias;
61 struct device_node *np;
62 int id;
63 char stem[0];
64};
65
66int of_n_addr_cells(const struct device_node *np)
67{
68 const __be32 *ip;
69
70 do {
71 if (np->parent)
72 np = np->parent;
73 ip = of_get_property(np, "#address-cells", NULL);
74 if (ip)
75 return be32_to_cpup(ip);
76 } while (np->parent);
77
78 /* No #address-cells property for the root node */
79 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
80}
81
82int of_n_size_cells(const struct device_node *np)
83{
84 const __be32 *ip;
85
86 do {
87 if (np->parent)
88 np = np->parent;
89 ip = of_get_property(np, "#size-cells", NULL);
90 if (ip)
91 return be32_to_cpup(ip);
92 } while (np->parent);
93
94 /* No #size-cells property for the root node */
95 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
96}
97
Simon Glass4191dc12017-06-12 06:21:31 -060098int of_simple_addr_cells(const struct device_node *np)
99{
100 const __be32 *ip;
101
102 ip = of_get_property(np, "#address-cells", NULL);
103 if (ip)
104 return be32_to_cpup(ip);
105
106 /* Return a default of 2 to match fdt_address_cells()*/
107 return 2;
108}
109
110int of_simple_size_cells(const struct device_node *np)
111{
112 const __be32 *ip;
113
114 ip = of_get_property(np, "#size-cells", NULL);
115 if (ip)
116 return be32_to_cpup(ip);
117
118 /* Return a default of 2 to match fdt_size_cells()*/
119 return 2;
120}
121
Simon Glass1ccaa052017-05-18 20:08:54 -0600122struct property *of_find_property(const struct device_node *np,
123 const char *name, int *lenp)
124{
125 struct property *pp;
126
127 if (!np)
128 return NULL;
129
130 for (pp = np->properties; pp; pp = pp->next) {
131 if (strcmp(pp->name, name) == 0) {
132 if (lenp)
133 *lenp = pp->length;
134 break;
135 }
136 }
137 if (!pp && lenp)
138 *lenp = -FDT_ERR_NOTFOUND;
139
140 return pp;
141}
142
143struct device_node *of_find_all_nodes(struct device_node *prev)
144{
145 struct device_node *np;
146
147 if (!prev) {
148 np = gd->of_root;
149 } else if (prev->child) {
150 np = prev->child;
151 } else {
152 /*
153 * Walk back up looking for a sibling, or the end of the
154 * structure
155 */
156 np = prev;
157 while (np->parent && !np->sibling)
158 np = np->parent;
159 np = np->sibling; /* Might be null at the end of the tree */
160 }
161
162 return np;
163}
164
165const void *of_get_property(const struct device_node *np, const char *name,
166 int *lenp)
167{
168 struct property *pp = of_find_property(np, name, lenp);
169
170 return pp ? pp->value : NULL;
171}
172
173static const char *of_prop_next_string(struct property *prop, const char *cur)
174{
175 const void *curv = cur;
176
177 if (!prop)
178 return NULL;
179
180 if (!cur)
181 return prop->value;
182
183 curv += strlen(cur) + 1;
184 if (curv >= prop->value + prop->length)
185 return NULL;
186
187 return curv;
188}
189
190int of_device_is_compatible(const struct device_node *device,
191 const char *compat, const char *type,
192 const char *name)
193{
194 struct property *prop;
195 const char *cp;
196 int index = 0, score = 0;
197
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);
205 break;
206 }
207 }
208 if (!score)
209 return 0;
210 }
211
212 /* Matching type is better than matching name */
213 if (type && type[0]) {
214 if (!device->type || of_node_cmp(type, device->type))
215 return 0;
216 score += 2;
217 }
218
219 /* Matching name is a bit better than not */
220 if (name && name[0]) {
221 if (!device->name || of_node_cmp(name, device->name))
222 return 0;
223 score++;
224 }
225
226 return score;
227}
228
229bool of_device_is_available(const struct device_node *device)
230{
231 const char *status;
232 int statlen;
233
234 if (!device)
235 return false;
236
237 status = of_get_property(device, "status", &statlen);
238 if (status == NULL)
239 return true;
240
241 if (statlen > 0) {
242 if (!strcmp(status, "okay"))
243 return true;
244 }
245
246 return false;
247}
248
249struct device_node *of_get_parent(const struct device_node *node)
250{
251 const struct device_node *np;
252
253 if (!node)
254 return NULL;
255
256 np = of_node_get(node->parent);
257
258 return (struct device_node *)np;
259}
260
261static struct device_node *__of_get_next_child(const struct device_node *node,
262 struct device_node *prev)
263{
264 struct device_node *next;
265
266 if (!node)
267 return NULL;
268
269 next = prev ? prev->sibling : node->child;
Simon Glass3694ac52017-06-07 10:28:45 -0600270 /*
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.
275 */
Simon Glass1ccaa052017-05-18 20:08:54 -0600276 for (; next; next = next->sibling)
277 if (of_node_get(next))
278 break;
279 of_node_put(prev);
280 return next;
281}
282
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))
286
287static struct device_node *__of_find_node_by_path(struct device_node *parent,
288 const char *path)
289{
290 struct device_node *child;
291 int len;
292
293 len = strcspn(path, "/:");
294 if (!len)
295 return NULL;
296
297 __for_each_child_of_node(parent, child) {
298 const char *name = strrchr(child->full_name, '/');
299
300 name++;
301 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
302 return child;
303 }
304 return NULL;
305}
306
307#define for_each_property_of_node(dn, pp) \
308 for (pp = dn->properties; pp != NULL; pp = pp->next)
309
310struct device_node *of_find_node_opts_by_path(const char *path,
311 const char **opts)
312{
313 struct device_node *np = NULL;
314 struct property *pp;
315 const char *separator = strchr(path, ':');
316
317 if (opts)
318 *opts = separator ? separator + 1 : NULL;
319
320 if (strcmp(path, "/") == 0)
321 return of_node_get(gd->of_root);
322
323 /* The path could begin with an alias */
324 if (*path != '/') {
325 int len;
326 const char *p = separator;
327
328 if (!p)
329 p = strchrnul(path, '/');
330 len = p - path;
331
332 /* of_aliases must not be NULL */
333 if (!of_aliases)
334 return NULL;
335
336 for_each_property_of_node(of_aliases, pp) {
337 if (strlen(pp->name) == len && !strncmp(pp->name, path,
338 len)) {
339 np = of_find_node_by_path(pp->value);
340 break;
341 }
342 }
343 if (!np)
344 return NULL;
345 path = p;
346 }
347
348 /* Step down the tree matching path components */
349 if (!np)
350 np = of_node_get(gd->of_root);
351 while (np && *path == '/') {
352 struct device_node *tmp = np;
353
354 path++; /* Increment past '/' delimiter */
355 np = __of_find_node_by_path(np, path);
356 of_node_put(tmp);
357 path = strchrnul(path, '/');
358 if (separator && separator < path)
359 break;
360 }
361
362 return np;
363}
364
365struct device_node *of_find_compatible_node(struct device_node *from,
366 const char *type, const char *compatible)
367{
368 struct device_node *np;
369
370 for_each_of_allnodes_from(from, np)
371 if (of_device_is_compatible(np, compatible, type, NULL) &&
372 of_node_get(np))
373 break;
374 of_node_put(from);
375
376 return np;
377}
378
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200379static int of_device_has_prop_value(const struct device_node *device,
380 const char *propname, const void *propval,
381 int proplen)
382{
383 struct property *prop = of_find_property(device, propname, NULL);
384
385 if (!prop || !prop->value || prop->length != proplen)
386 return 0;
387 return !memcmp(prop->value, propval, proplen);
388}
389
390struct device_node *of_find_node_by_prop_value(struct device_node *from,
391 const char *propname,
392 const void *propval, int proplen)
393{
394 struct device_node *np;
395
396 for_each_of_allnodes_from(from, np) {
397 if (of_device_has_prop_value(np, propname, propval, proplen) &&
398 of_node_get(np))
399 break;
400 }
401 of_node_put(from);
402
403 return np;
404}
405
Simon Glass1ccaa052017-05-18 20:08:54 -0600406struct device_node *of_find_node_by_phandle(phandle handle)
407{
408 struct device_node *np;
409
410 if (!handle)
411 return NULL;
412
413 for_each_of_allnodes(np)
414 if (np->phandle == handle)
415 break;
416 (void)of_node_get(np);
417
418 return np;
419}
420
421/**
422 * of_find_property_value_of_size() - find property of given size
423 *
424 * Search for a property in a device node and validate the requested size.
425 *
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
429 *
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.
433 */
434static void *of_find_property_value_of_size(const struct device_node *np,
435 const char *propname, u32 len)
436{
437 struct property *prop = of_find_property(np, propname, NULL);
438
439 if (!prop)
440 return ERR_PTR(-EINVAL);
441 if (!prop->value)
442 return ERR_PTR(-ENODATA);
443 if (len > prop->length)
444 return ERR_PTR(-EOVERFLOW);
445
446 return prop->value;
447}
448
449int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
450{
451 const __be32 *val;
452
453 debug("%s: %s: ", __func__, propname);
454 if (!np)
455 return -EINVAL;
456 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
457 if (IS_ERR(val)) {
458 debug("(not found)\n");
459 return PTR_ERR(val);
460 }
461
462 *outp = be32_to_cpup(val);
463 debug("%#x (%d)\n", *outp, *outp);
464
465 return 0;
466}
467
468int of_read_u32_array(const struct device_node *np, const char *propname,
469 u32 *out_values, size_t sz)
470{
471 const __be32 *val;
472
473 debug("%s: %s: ", __func__, propname);
474 val = of_find_property_value_of_size(np, propname,
475 sz * sizeof(*out_values));
476
477 if (IS_ERR(val))
478 return PTR_ERR(val);
479
480 debug("size %zd\n", sz);
481 while (sz--)
482 *out_values++ = be32_to_cpup(val++);
483
484 return 0;
485}
486
Simon Glass9d54a7a2018-06-11 13:07:10 -0600487int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
488{
489 const __be64 *val;
490
491 debug("%s: %s: ", __func__, propname);
492 if (!np)
493 return -EINVAL;
494 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
495 if (IS_ERR(val)) {
496 debug("(not found)\n");
497 return PTR_ERR(val);
498 }
499
500 *outp = be64_to_cpup(val);
501 debug("%#llx (%lld)\n", (unsigned long long)*outp,
502 (unsigned long long)*outp);
503
504 return 0;
505}
506
Simon Glass1ccaa052017-05-18 20:08:54 -0600507int of_property_match_string(const struct device_node *np, const char *propname,
508 const char *string)
509{
510 const struct property *prop = of_find_property(np, propname, NULL);
511 size_t l;
512 int i;
513 const char *p, *end;
514
515 if (!prop)
516 return -EINVAL;
517 if (!prop->value)
518 return -ENODATA;
519
520 p = prop->value;
521 end = p + prop->length;
522
523 for (i = 0; p < end; i++, p += l) {
524 l = strnlen(p, end - p) + 1;
525 if (p + l > end)
526 return -EILSEQ;
527 debug("comparing %s with %s\n", string, p);
528 if (strcmp(string, p) == 0)
529 return i; /* Found it; return index */
530 }
531 return -ENODATA;
532}
533
534/**
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.
541 *
542 * Don't call this function directly. It is a utility helper for the
543 * of_property_read_string*() family of functions.
544 */
545int of_property_read_string_helper(const struct device_node *np,
546 const char *propname, const char **out_strs,
547 size_t sz, int skip)
548{
549 const struct property *prop = of_find_property(np, propname, NULL);
550 int l = 0, i = 0;
551 const char *p, *end;
552
553 if (!prop)
554 return -EINVAL;
555 if (!prop->value)
556 return -ENODATA;
557 p = prop->value;
558 end = p + prop->length;
559
560 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
561 l = strnlen(p, end - p) + 1;
562 if (p + l > end)
563 return -EILSEQ;
564 if (out_strs && i >= skip)
565 *out_strs++ = p;
566 }
567 i -= skip;
568 return i <= 0 ? -ENODATA : i;
569}
570
571static 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)
576{
577 const __be32 *list, *list_end;
578 int rc = 0, cur_index = 0;
579 uint32_t count = 0;
580 struct device_node *node = NULL;
581 phandle phandle;
582 int size;
583
584 /* Retrieve the phandle list property */
585 list = of_get_property(np, list_name, &size);
586 if (!list)
587 return -ENOENT;
588 list_end = list + size / sizeof(*list);
589
590 /* Loop over the phandles until all the requested entry is found */
591 while (list < list_end) {
592 rc = -EINVAL;
593 count = 0;
594
595 /*
596 * If phandle is 0, then it is an empty entry with no
597 * arguments. Skip forward to the next entry.
598 */
599 phandle = be32_to_cpup(list++);
600 if (phandle) {
601 /*
602 * Find the provider node and parse the #*-cells
603 * property to determine the argument length.
604 *
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
608 * below.
609 */
610 if (cells_name || cur_index == index) {
611 node = of_find_node_by_phandle(phandle);
612 if (!node) {
613 debug("%s: could not find phandle\n",
614 np->full_name);
615 goto err;
616 }
617 }
618
619 if (cells_name) {
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,
623 node->full_name);
624 goto err;
625 }
626 } else {
627 count = cell_count;
628 }
629
630 /*
631 * Make sure that the arguments actually fit in the
632 * remaining property data length
633 */
634 if (list + count > list_end) {
635 debug("%s: arguments longer than property\n",
636 np->full_name);
637 goto err;
638 }
639 }
640
641 /*
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.
646 */
647 rc = -ENOENT;
648 if (cur_index == index) {
649 if (!phandle)
650 goto err;
651
652 if (out_args) {
653 int i;
654 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
655 count = OF_MAX_PHANDLE_ARGS;
656 out_args->np = node;
657 out_args->args_count = count;
658 for (i = 0; i < count; i++)
659 out_args->args[i] =
660 be32_to_cpup(list++);
661 } else {
662 of_node_put(node);
663 }
664
665 /* Found it! return success */
666 return 0;
667 }
668
669 of_node_put(node);
670 node = NULL;
671 list += count;
672 cur_index++;
673 }
674
675 /*
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)
680 */
681 rc = index < 0 ? cur_index : -ENOENT;
682 err:
683 if (node)
684 of_node_put(node);
685 return rc;
686}
687
688struct device_node *of_parse_phandle(const struct device_node *np,
689 const char *phandle_name, int index)
690{
691 struct of_phandle_args args;
692
693 if (index < 0)
694 return NULL;
695
696 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
697 &args))
698 return NULL;
699
700 return args.np;
701}
702
703int 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)
706{
707 if (index < 0)
708 return -EINVAL;
709
710 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
711 index, out_args);
712}
713
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200714int of_count_phandle_with_args(const struct device_node *np,
715 const char *list_name, const char *cells_name)
716{
717 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
718 -1, NULL);
719}
720
Simon Glass1ccaa052017-05-18 20:08:54 -0600721static void of_alias_add(struct alias_prop *ap, struct device_node *np,
722 int id, const char *stem, int stem_len)
723{
724 ap->np = np;
725 ap->id = id;
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));
731}
732
733int of_alias_scan(void)
734{
735 struct property *pp;
736
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");
741
742 if (of_chosen) {
743 const char *name;
744
745 name = of_get_property(of_chosen, "stdout-path", NULL);
746 if (name)
747 of_stdout = of_find_node_opts_by_path(name,
748 &of_stdout_options);
749 }
750
751 if (!of_aliases)
752 return 0;
753
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;
759 ulong id;
760 int len;
761
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"))
766 continue;
767
768 np = of_find_node_by_path(pp->value);
769 if (!np)
770 continue;
771
772 /*
773 * walk the alias backwards to extract the id and work out
774 * the 'stem' string
775 */
776 while (isdigit(*(end-1)) && end > start)
777 end--;
778 len = end - start;
779
780 if (strict_strtoul(end, 10, &id) < 0)
781 continue;
782
783 /* Allocate an alias_prop with enough space for the stem */
784 ap = malloc(sizeof(*ap) + len + 1);
785 if (!ap)
786 return -ENOMEM;
787 memset(ap, 0, sizeof(*ap) + len + 1);
788 ap->alias = start;
789 of_alias_add(ap, np, id, start, len);
790 }
791
792 return 0;
793}
794
795int of_alias_get_id(const struct device_node *np, const char *stem)
796{
797 struct alias_prop *app;
798 int id = -ENODEV;
799
800 mutex_lock(&of_mutex);
801 list_for_each_entry(app, &aliases_lookup, link) {
802 if (strcmp(app->stem, stem) != 0)
803 continue;
804
805 if (np == app->np) {
806 id = app->id;
807 break;
808 }
809 }
810 mutex_unlock(&of_mutex);
811
812 return id;
813}
814
815struct device_node *of_get_stdout(void)
816{
817 return of_stdout;
818}