blob: 9a50f559de2d927c56e156076a2845c8fce210fc [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
379struct device_node *of_find_node_by_phandle(phandle handle)
380{
381 struct device_node *np;
382
383 if (!handle)
384 return NULL;
385
386 for_each_of_allnodes(np)
387 if (np->phandle == handle)
388 break;
389 (void)of_node_get(np);
390
391 return np;
392}
393
394/**
395 * of_find_property_value_of_size() - find property of given size
396 *
397 * Search for a property in a device node and validate the requested size.
398 *
399 * @np: device node from which the property value is to be read.
400 * @propname: name of the property to be searched.
401 * @len: requested length of property value
402 *
403 * @return the property value on success, -EINVAL if the property does not
404 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
405 * property data isn't large enough.
406 */
407static void *of_find_property_value_of_size(const struct device_node *np,
408 const char *propname, u32 len)
409{
410 struct property *prop = of_find_property(np, propname, NULL);
411
412 if (!prop)
413 return ERR_PTR(-EINVAL);
414 if (!prop->value)
415 return ERR_PTR(-ENODATA);
416 if (len > prop->length)
417 return ERR_PTR(-EOVERFLOW);
418
419 return prop->value;
420}
421
422int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
423{
424 const __be32 *val;
425
426 debug("%s: %s: ", __func__, propname);
427 if (!np)
428 return -EINVAL;
429 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
430 if (IS_ERR(val)) {
431 debug("(not found)\n");
432 return PTR_ERR(val);
433 }
434
435 *outp = be32_to_cpup(val);
436 debug("%#x (%d)\n", *outp, *outp);
437
438 return 0;
439}
440
441int of_read_u32_array(const struct device_node *np, const char *propname,
442 u32 *out_values, size_t sz)
443{
444 const __be32 *val;
445
446 debug("%s: %s: ", __func__, propname);
447 val = of_find_property_value_of_size(np, propname,
448 sz * sizeof(*out_values));
449
450 if (IS_ERR(val))
451 return PTR_ERR(val);
452
453 debug("size %zd\n", sz);
454 while (sz--)
455 *out_values++ = be32_to_cpup(val++);
456
457 return 0;
458}
459
460int of_property_match_string(const struct device_node *np, const char *propname,
461 const char *string)
462{
463 const struct property *prop = of_find_property(np, propname, NULL);
464 size_t l;
465 int i;
466 const char *p, *end;
467
468 if (!prop)
469 return -EINVAL;
470 if (!prop->value)
471 return -ENODATA;
472
473 p = prop->value;
474 end = p + prop->length;
475
476 for (i = 0; p < end; i++, p += l) {
477 l = strnlen(p, end - p) + 1;
478 if (p + l > end)
479 return -EILSEQ;
480 debug("comparing %s with %s\n", string, p);
481 if (strcmp(string, p) == 0)
482 return i; /* Found it; return index */
483 }
484 return -ENODATA;
485}
486
487/**
488 * of_property_read_string_helper() - Utility helper for parsing string properties
489 * @np: device node from which the property value is to be read.
490 * @propname: name of the property to be searched.
491 * @out_strs: output array of string pointers.
492 * @sz: number of array elements to read.
493 * @skip: Number of strings to skip over at beginning of list.
494 *
495 * Don't call this function directly. It is a utility helper for the
496 * of_property_read_string*() family of functions.
497 */
498int of_property_read_string_helper(const struct device_node *np,
499 const char *propname, const char **out_strs,
500 size_t sz, int skip)
501{
502 const struct property *prop = of_find_property(np, propname, NULL);
503 int l = 0, i = 0;
504 const char *p, *end;
505
506 if (!prop)
507 return -EINVAL;
508 if (!prop->value)
509 return -ENODATA;
510 p = prop->value;
511 end = p + prop->length;
512
513 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
514 l = strnlen(p, end - p) + 1;
515 if (p + l > end)
516 return -EILSEQ;
517 if (out_strs && i >= skip)
518 *out_strs++ = p;
519 }
520 i -= skip;
521 return i <= 0 ? -ENODATA : i;
522}
523
524static int __of_parse_phandle_with_args(const struct device_node *np,
525 const char *list_name,
526 const char *cells_name,
527 int cell_count, int index,
528 struct of_phandle_args *out_args)
529{
530 const __be32 *list, *list_end;
531 int rc = 0, cur_index = 0;
532 uint32_t count = 0;
533 struct device_node *node = NULL;
534 phandle phandle;
535 int size;
536
537 /* Retrieve the phandle list property */
538 list = of_get_property(np, list_name, &size);
539 if (!list)
540 return -ENOENT;
541 list_end = list + size / sizeof(*list);
542
543 /* Loop over the phandles until all the requested entry is found */
544 while (list < list_end) {
545 rc = -EINVAL;
546 count = 0;
547
548 /*
549 * If phandle is 0, then it is an empty entry with no
550 * arguments. Skip forward to the next entry.
551 */
552 phandle = be32_to_cpup(list++);
553 if (phandle) {
554 /*
555 * Find the provider node and parse the #*-cells
556 * property to determine the argument length.
557 *
558 * This is not needed if the cell count is hard-coded
559 * (i.e. cells_name not set, but cell_count is set),
560 * except when we're going to return the found node
561 * below.
562 */
563 if (cells_name || cur_index == index) {
564 node = of_find_node_by_phandle(phandle);
565 if (!node) {
566 debug("%s: could not find phandle\n",
567 np->full_name);
568 goto err;
569 }
570 }
571
572 if (cells_name) {
573 if (of_read_u32(node, cells_name, &count)) {
574 debug("%s: could not get %s for %s\n",
575 np->full_name, cells_name,
576 node->full_name);
577 goto err;
578 }
579 } else {
580 count = cell_count;
581 }
582
583 /*
584 * Make sure that the arguments actually fit in the
585 * remaining property data length
586 */
587 if (list + count > list_end) {
588 debug("%s: arguments longer than property\n",
589 np->full_name);
590 goto err;
591 }
592 }
593
594 /*
595 * All of the error cases above bail out of the loop, so at
596 * this point, the parsing is successful. If the requested
597 * index matches, then fill the out_args structure and return,
598 * or return -ENOENT for an empty entry.
599 */
600 rc = -ENOENT;
601 if (cur_index == index) {
602 if (!phandle)
603 goto err;
604
605 if (out_args) {
606 int i;
607 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
608 count = OF_MAX_PHANDLE_ARGS;
609 out_args->np = node;
610 out_args->args_count = count;
611 for (i = 0; i < count; i++)
612 out_args->args[i] =
613 be32_to_cpup(list++);
614 } else {
615 of_node_put(node);
616 }
617
618 /* Found it! return success */
619 return 0;
620 }
621
622 of_node_put(node);
623 node = NULL;
624 list += count;
625 cur_index++;
626 }
627
628 /*
629 * Unlock node before returning result; will be one of:
630 * -ENOENT : index is for empty phandle
631 * -EINVAL : parsing error on data
632 * [1..n] : Number of phandle (count mode; when index = -1)
633 */
634 rc = index < 0 ? cur_index : -ENOENT;
635 err:
636 if (node)
637 of_node_put(node);
638 return rc;
639}
640
641struct device_node *of_parse_phandle(const struct device_node *np,
642 const char *phandle_name, int index)
643{
644 struct of_phandle_args args;
645
646 if (index < 0)
647 return NULL;
648
649 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
650 &args))
651 return NULL;
652
653 return args.np;
654}
655
656int of_parse_phandle_with_args(const struct device_node *np,
657 const char *list_name, const char *cells_name,
658 int index, struct of_phandle_args *out_args)
659{
660 if (index < 0)
661 return -EINVAL;
662
663 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
664 index, out_args);
665}
666
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200667int of_count_phandle_with_args(const struct device_node *np,
668 const char *list_name, const char *cells_name)
669{
670 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
671 -1, NULL);
672}
673
Simon Glass1ccaa052017-05-18 20:08:54 -0600674static void of_alias_add(struct alias_prop *ap, struct device_node *np,
675 int id, const char *stem, int stem_len)
676{
677 ap->np = np;
678 ap->id = id;
679 strncpy(ap->stem, stem, stem_len);
680 ap->stem[stem_len] = 0;
681 list_add_tail(&ap->link, &aliases_lookup);
682 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
683 ap->alias, ap->stem, ap->id, of_node_full_name(np));
684}
685
686int of_alias_scan(void)
687{
688 struct property *pp;
689
690 of_aliases = of_find_node_by_path("/aliases");
691 of_chosen = of_find_node_by_path("/chosen");
692 if (of_chosen == NULL)
693 of_chosen = of_find_node_by_path("/chosen@0");
694
695 if (of_chosen) {
696 const char *name;
697
698 name = of_get_property(of_chosen, "stdout-path", NULL);
699 if (name)
700 of_stdout = of_find_node_opts_by_path(name,
701 &of_stdout_options);
702 }
703
704 if (!of_aliases)
705 return 0;
706
707 for_each_property_of_node(of_aliases, pp) {
708 const char *start = pp->name;
709 const char *end = start + strlen(start);
710 struct device_node *np;
711 struct alias_prop *ap;
712 ulong id;
713 int len;
714
715 /* Skip those we do not want to proceed */
716 if (!strcmp(pp->name, "name") ||
717 !strcmp(pp->name, "phandle") ||
718 !strcmp(pp->name, "linux,phandle"))
719 continue;
720
721 np = of_find_node_by_path(pp->value);
722 if (!np)
723 continue;
724
725 /*
726 * walk the alias backwards to extract the id and work out
727 * the 'stem' string
728 */
729 while (isdigit(*(end-1)) && end > start)
730 end--;
731 len = end - start;
732
733 if (strict_strtoul(end, 10, &id) < 0)
734 continue;
735
736 /* Allocate an alias_prop with enough space for the stem */
737 ap = malloc(sizeof(*ap) + len + 1);
738 if (!ap)
739 return -ENOMEM;
740 memset(ap, 0, sizeof(*ap) + len + 1);
741 ap->alias = start;
742 of_alias_add(ap, np, id, start, len);
743 }
744
745 return 0;
746}
747
748int of_alias_get_id(const struct device_node *np, const char *stem)
749{
750 struct alias_prop *app;
751 int id = -ENODEV;
752
753 mutex_lock(&of_mutex);
754 list_for_each_entry(app, &aliases_lookup, link) {
755 if (strcmp(app->stem, stem) != 0)
756 continue;
757
758 if (np == app->np) {
759 id = app->id;
760 break;
761 }
762 }
763 mutex_unlock(&of_mutex);
764
765 return id;
766}
767
768struct device_node *of_get_stdout(void)
769{
770 return of_stdout;
771}