blob: a52f5a6b18b9aaff2cd7cb76fddb03f37510e568 [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 Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070024#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060025#include <asm/global_data.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060026#include <linux/bug.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090027#include <linux/libfdt.h>
Simon Glass1ccaa052017-05-18 20:08:54 -060028#include <dm/of_access.h>
29#include <linux/ctype.h>
30#include <linux/err.h>
31#include <linux/ioport.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
35/* list of struct alias_prop aliases */
36LIST_HEAD(aliases_lookup);
37
38/* "/aliaes" node */
39static struct device_node *of_aliases;
40
41/* "/chosen" node */
42static struct device_node *of_chosen;
43
44/* node pointed to by the stdout-path alias */
45static struct device_node *of_stdout;
46
47/* pointer to options given after the alias (separated by :) or NULL if none */
48static const char *of_stdout_options;
49
50/**
51 * struct alias_prop - Alias property in 'aliases' node
52 *
53 * The structure represents one alias property of 'aliases' node as
54 * an entry in aliases_lookup list.
55 *
56 * @link: List node to link the structure in aliases_lookup list
57 * @alias: Alias property name
58 * @np: Pointer to device_node that the alias stands for
59 * @id: Index value from end of alias name
60 * @stem: Alias string without the index
61 */
62struct alias_prop {
63 struct list_head link;
64 const char *alias;
65 struct device_node *np;
66 int id;
67 char stem[0];
68};
69
70int of_n_addr_cells(const struct device_node *np)
71{
72 const __be32 *ip;
73
74 do {
75 if (np->parent)
76 np = np->parent;
77 ip = of_get_property(np, "#address-cells", NULL);
78 if (ip)
79 return be32_to_cpup(ip);
80 } while (np->parent);
81
82 /* No #address-cells property for the root node */
83 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
84}
85
86int of_n_size_cells(const struct device_node *np)
87{
88 const __be32 *ip;
89
90 do {
91 if (np->parent)
92 np = np->parent;
93 ip = of_get_property(np, "#size-cells", NULL);
94 if (ip)
95 return be32_to_cpup(ip);
96 } while (np->parent);
97
98 /* No #size-cells property for the root node */
99 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
100}
101
Simon Glass4191dc12017-06-12 06:21:31 -0600102int of_simple_addr_cells(const struct device_node *np)
103{
104 const __be32 *ip;
105
106 ip = of_get_property(np, "#address-cells", NULL);
107 if (ip)
108 return be32_to_cpup(ip);
109
110 /* Return a default of 2 to match fdt_address_cells()*/
111 return 2;
112}
113
114int of_simple_size_cells(const struct device_node *np)
115{
116 const __be32 *ip;
117
118 ip = of_get_property(np, "#size-cells", NULL);
119 if (ip)
120 return be32_to_cpup(ip);
121
122 /* Return a default of 2 to match fdt_size_cells()*/
123 return 2;
124}
125
Simon Glass1ccaa052017-05-18 20:08:54 -0600126struct property *of_find_property(const struct device_node *np,
127 const char *name, int *lenp)
128{
129 struct property *pp;
130
131 if (!np)
132 return NULL;
133
134 for (pp = np->properties; pp; pp = pp->next) {
135 if (strcmp(pp->name, name) == 0) {
136 if (lenp)
137 *lenp = pp->length;
138 break;
139 }
140 }
141 if (!pp && lenp)
142 *lenp = -FDT_ERR_NOTFOUND;
143
144 return pp;
145}
146
147struct device_node *of_find_all_nodes(struct device_node *prev)
148{
149 struct device_node *np;
150
151 if (!prev) {
152 np = gd->of_root;
153 } else if (prev->child) {
154 np = prev->child;
155 } else {
156 /*
157 * Walk back up looking for a sibling, or the end of the
158 * structure
159 */
160 np = prev;
161 while (np->parent && !np->sibling)
162 np = np->parent;
163 np = np->sibling; /* Might be null at the end of the tree */
164 }
165
166 return np;
167}
168
169const void *of_get_property(const struct device_node *np, const char *name,
170 int *lenp)
171{
172 struct property *pp = of_find_property(np, name, lenp);
173
174 return pp ? pp->value : NULL;
175}
176
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100177const struct property *of_get_first_property(const struct device_node *np)
178{
179 if (!np)
180 return NULL;
181
182 return np->properties;
183}
184
185const struct property *of_get_next_property(const struct device_node *np,
186 const struct property *property)
187{
188 if (!np)
189 return NULL;
190
191 return property->next;
192}
193
194const void *of_get_property_by_prop(const struct device_node *np,
195 const struct property *property,
196 const char **name,
197 int *lenp)
198{
199 if (!np || !property)
200 return NULL;
201 if (name)
202 *name = property->name;
203 if (lenp)
204 *lenp = property->length;
205
206 return property->value;
207}
208
Simon Glass1ccaa052017-05-18 20:08:54 -0600209static const char *of_prop_next_string(struct property *prop, const char *cur)
210{
211 const void *curv = cur;
212
213 if (!prop)
214 return NULL;
215
216 if (!cur)
217 return prop->value;
218
219 curv += strlen(cur) + 1;
220 if (curv >= prop->value + prop->length)
221 return NULL;
222
223 return curv;
224}
225
226int of_device_is_compatible(const struct device_node *device,
227 const char *compat, const char *type,
228 const char *name)
229{
230 struct property *prop;
231 const char *cp;
232 int index = 0, score = 0;
233
234 /* Compatible match has highest priority */
235 if (compat && compat[0]) {
236 prop = of_find_property(device, "compatible", NULL);
237 for (cp = of_prop_next_string(prop, NULL); cp;
238 cp = of_prop_next_string(prop, cp), index++) {
239 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
240 score = INT_MAX/2 - (index << 2);
241 break;
242 }
243 }
244 if (!score)
245 return 0;
246 }
247
248 /* Matching type is better than matching name */
249 if (type && type[0]) {
250 if (!device->type || of_node_cmp(type, device->type))
251 return 0;
252 score += 2;
253 }
254
255 /* Matching name is a bit better than not */
256 if (name && name[0]) {
257 if (!device->name || of_node_cmp(name, device->name))
258 return 0;
259 score++;
260 }
261
262 return score;
263}
264
265bool of_device_is_available(const struct device_node *device)
266{
267 const char *status;
268 int statlen;
269
270 if (!device)
271 return false;
272
273 status = of_get_property(device, "status", &statlen);
274 if (status == NULL)
275 return true;
276
277 if (statlen > 0) {
278 if (!strcmp(status, "okay"))
279 return true;
280 }
281
282 return false;
283}
284
285struct device_node *of_get_parent(const struct device_node *node)
286{
287 const struct device_node *np;
288
289 if (!node)
290 return NULL;
291
292 np = of_node_get(node->parent);
293
294 return (struct device_node *)np;
295}
296
297static struct device_node *__of_get_next_child(const struct device_node *node,
298 struct device_node *prev)
299{
300 struct device_node *next;
301
302 if (!node)
303 return NULL;
304
305 next = prev ? prev->sibling : node->child;
Simon Glass3694ac52017-06-07 10:28:45 -0600306 /*
307 * coverity[dead_error_line : FALSE]
308 * Dead code here since our current implementation of of_node_get()
309 * always returns NULL (Coverity CID 163245). But we leave it as is
310 * since we may want to implement get/put later.
311 */
Simon Glass1ccaa052017-05-18 20:08:54 -0600312 for (; next; next = next->sibling)
313 if (of_node_get(next))
314 break;
315 of_node_put(prev);
316 return next;
317}
318
319#define __for_each_child_of_node(parent, child) \
320 for (child = __of_get_next_child(parent, NULL); child != NULL; \
321 child = __of_get_next_child(parent, child))
322
323static struct device_node *__of_find_node_by_path(struct device_node *parent,
324 const char *path)
325{
326 struct device_node *child;
327 int len;
328
329 len = strcspn(path, "/:");
330 if (!len)
331 return NULL;
332
333 __for_each_child_of_node(parent, child) {
334 const char *name = strrchr(child->full_name, '/');
335
336 name++;
337 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
338 return child;
339 }
340 return NULL;
341}
342
343#define for_each_property_of_node(dn, pp) \
344 for (pp = dn->properties; pp != NULL; pp = pp->next)
345
Simon Glassef75c592022-07-30 15:52:08 -0600346struct device_node *of_find_node_opts_by_path(struct device_node *root,
347 const char *path,
Simon Glass1ccaa052017-05-18 20:08:54 -0600348 const char **opts)
349{
350 struct device_node *np = NULL;
351 struct property *pp;
352 const char *separator = strchr(path, ':');
353
Simon Glassef75c592022-07-30 15:52:08 -0600354 if (!root)
355 root = gd->of_root;
Simon Glass1ccaa052017-05-18 20:08:54 -0600356 if (opts)
357 *opts = separator ? separator + 1 : NULL;
358
359 if (strcmp(path, "/") == 0)
Simon Glassef75c592022-07-30 15:52:08 -0600360 return of_node_get(root);
Simon Glass1ccaa052017-05-18 20:08:54 -0600361
362 /* The path could begin with an alias */
363 if (*path != '/') {
364 int len;
365 const char *p = separator;
366
Simon Glassef75c592022-07-30 15:52:08 -0600367 /* Only allow alias processing on the control FDT */
368 if (root != gd->of_root)
369 return NULL;
Simon Glass1ccaa052017-05-18 20:08:54 -0600370 if (!p)
371 p = strchrnul(path, '/');
372 len = p - path;
373
374 /* of_aliases must not be NULL */
375 if (!of_aliases)
376 return NULL;
377
378 for_each_property_of_node(of_aliases, pp) {
379 if (strlen(pp->name) == len && !strncmp(pp->name, path,
380 len)) {
381 np = of_find_node_by_path(pp->value);
382 break;
383 }
384 }
385 if (!np)
386 return NULL;
387 path = p;
388 }
389
390 /* Step down the tree matching path components */
391 if (!np)
Simon Glassef75c592022-07-30 15:52:08 -0600392 np = of_node_get(root);
Simon Glass1ccaa052017-05-18 20:08:54 -0600393 while (np && *path == '/') {
394 struct device_node *tmp = np;
395
396 path++; /* Increment past '/' delimiter */
397 np = __of_find_node_by_path(np, path);
398 of_node_put(tmp);
399 path = strchrnul(path, '/');
400 if (separator && separator < path)
401 break;
402 }
403
404 return np;
405}
406
407struct device_node *of_find_compatible_node(struct device_node *from,
408 const char *type, const char *compatible)
409{
410 struct device_node *np;
411
412 for_each_of_allnodes_from(from, np)
413 if (of_device_is_compatible(np, compatible, type, NULL) &&
414 of_node_get(np))
415 break;
416 of_node_put(from);
417
418 return np;
419}
420
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200421static int of_device_has_prop_value(const struct device_node *device,
422 const char *propname, const void *propval,
423 int proplen)
424{
425 struct property *prop = of_find_property(device, propname, NULL);
426
427 if (!prop || !prop->value || prop->length != proplen)
428 return 0;
429 return !memcmp(prop->value, propval, proplen);
430}
431
432struct device_node *of_find_node_by_prop_value(struct device_node *from,
433 const char *propname,
434 const void *propval, int proplen)
435{
436 struct device_node *np;
437
438 for_each_of_allnodes_from(from, np) {
439 if (of_device_has_prop_value(np, propname, propval, proplen) &&
440 of_node_get(np))
441 break;
442 }
443 of_node_put(from);
444
445 return np;
446}
447
Simon Glass1ccaa052017-05-18 20:08:54 -0600448struct device_node *of_find_node_by_phandle(phandle handle)
449{
450 struct device_node *np;
451
452 if (!handle)
453 return NULL;
454
455 for_each_of_allnodes(np)
456 if (np->phandle == handle)
457 break;
458 (void)of_node_get(np);
459
460 return np;
461}
462
463/**
464 * of_find_property_value_of_size() - find property of given size
465 *
466 * Search for a property in a device node and validate the requested size.
467 *
468 * @np: device node from which the property value is to be read.
469 * @propname: name of the property to be searched.
470 * @len: requested length of property value
471 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100472 * Return: the property value on success, -EINVAL if the property does not
Simon Glass1ccaa052017-05-18 20:08:54 -0600473 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
474 * property data isn't large enough.
475 */
476static void *of_find_property_value_of_size(const struct device_node *np,
477 const char *propname, u32 len)
478{
479 struct property *prop = of_find_property(np, propname, NULL);
480
481 if (!prop)
482 return ERR_PTR(-EINVAL);
483 if (!prop->value)
484 return ERR_PTR(-ENODATA);
485 if (len > prop->length)
486 return ERR_PTR(-EOVERFLOW);
487
488 return prop->value;
489}
490
491int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
492{
Dario Binacchib3f1cdd2020-03-29 18:04:42 +0200493 return of_read_u32_index(np, propname, 0, outp);
Simon Glass1ccaa052017-05-18 20:08:54 -0600494}
495
496int of_read_u32_array(const struct device_node *np, const char *propname,
497 u32 *out_values, size_t sz)
498{
499 const __be32 *val;
500
501 debug("%s: %s: ", __func__, propname);
502 val = of_find_property_value_of_size(np, propname,
503 sz * sizeof(*out_values));
504
505 if (IS_ERR(val))
506 return PTR_ERR(val);
507
508 debug("size %zd\n", sz);
509 while (sz--)
510 *out_values++ = be32_to_cpup(val++);
511
512 return 0;
513}
514
Dario Binacchi81d80b52020-03-29 18:04:41 +0200515int of_read_u32_index(const struct device_node *np, const char *propname,
516 int index, u32 *outp)
517{
518 const __be32 *val;
519
520 debug("%s: %s: ", __func__, propname);
521 if (!np)
522 return -EINVAL;
523
524 val = of_find_property_value_of_size(np, propname,
525 sizeof(*outp) * (index + 1));
526 if (IS_ERR(val)) {
527 debug("(not found)\n");
528 return PTR_ERR(val);
529 }
530
531 *outp = be32_to_cpup(val + index);
532 debug("%#x (%d)\n", *outp, *outp);
533
534 return 0;
535}
536
Simon Glass9d54a7a2018-06-11 13:07:10 -0600537int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
538{
539 const __be64 *val;
540
541 debug("%s: %s: ", __func__, propname);
542 if (!np)
543 return -EINVAL;
544 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
545 if (IS_ERR(val)) {
546 debug("(not found)\n");
547 return PTR_ERR(val);
548 }
549
550 *outp = be64_to_cpup(val);
551 debug("%#llx (%lld)\n", (unsigned long long)*outp,
552 (unsigned long long)*outp);
553
554 return 0;
555}
556
Simon Glass1ccaa052017-05-18 20:08:54 -0600557int of_property_match_string(const struct device_node *np, const char *propname,
558 const char *string)
559{
560 const struct property *prop = of_find_property(np, propname, NULL);
561 size_t l;
562 int i;
563 const char *p, *end;
564
565 if (!prop)
566 return -EINVAL;
567 if (!prop->value)
568 return -ENODATA;
569
570 p = prop->value;
571 end = p + prop->length;
572
573 for (i = 0; p < end; i++, p += l) {
574 l = strnlen(p, end - p) + 1;
575 if (p + l > end)
576 return -EILSEQ;
577 debug("comparing %s with %s\n", string, p);
578 if (strcmp(string, p) == 0)
579 return i; /* Found it; return index */
580 }
581 return -ENODATA;
582}
583
584/**
585 * of_property_read_string_helper() - Utility helper for parsing string properties
586 * @np: device node from which the property value is to be read.
587 * @propname: name of the property to be searched.
588 * @out_strs: output array of string pointers.
589 * @sz: number of array elements to read.
Simon Glass7a3a1672021-10-23 17:26:06 -0600590 * @skip: Number of strings to skip over at beginning of list (cannot be
591 * negative)
Simon Glass1ccaa052017-05-18 20:08:54 -0600592 *
593 * Don't call this function directly. It is a utility helper for the
594 * of_property_read_string*() family of functions.
595 */
596int of_property_read_string_helper(const struct device_node *np,
597 const char *propname, const char **out_strs,
598 size_t sz, int skip)
599{
600 const struct property *prop = of_find_property(np, propname, NULL);
601 int l = 0, i = 0;
602 const char *p, *end;
603
604 if (!prop)
605 return -EINVAL;
606 if (!prop->value)
607 return -ENODATA;
608 p = prop->value;
609 end = p + prop->length;
610
611 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
612 l = strnlen(p, end - p) + 1;
613 if (p + l > end)
614 return -EILSEQ;
615 if (out_strs && i >= skip)
616 *out_strs++ = p;
617 }
618 i -= skip;
619 return i <= 0 ? -ENODATA : i;
620}
621
622static int __of_parse_phandle_with_args(const struct device_node *np,
623 const char *list_name,
624 const char *cells_name,
625 int cell_count, int index,
626 struct of_phandle_args *out_args)
627{
628 const __be32 *list, *list_end;
629 int rc = 0, cur_index = 0;
Heinrich Schuchardtc13346e2020-02-15 21:46:04 +0100630 uint32_t count;
Simon Glass1ccaa052017-05-18 20:08:54 -0600631 struct device_node *node = NULL;
632 phandle phandle;
633 int size;
634
635 /* Retrieve the phandle list property */
636 list = of_get_property(np, list_name, &size);
637 if (!list)
638 return -ENOENT;
639 list_end = list + size / sizeof(*list);
640
641 /* Loop over the phandles until all the requested entry is found */
642 while (list < list_end) {
643 rc = -EINVAL;
644 count = 0;
645
646 /*
647 * If phandle is 0, then it is an empty entry with no
648 * arguments. Skip forward to the next entry.
649 */
650 phandle = be32_to_cpup(list++);
651 if (phandle) {
652 /*
653 * Find the provider node and parse the #*-cells
654 * property to determine the argument length.
655 *
656 * This is not needed if the cell count is hard-coded
657 * (i.e. cells_name not set, but cell_count is set),
658 * except when we're going to return the found node
659 * below.
660 */
661 if (cells_name || cur_index == index) {
662 node = of_find_node_by_phandle(phandle);
663 if (!node) {
664 debug("%s: could not find phandle\n",
665 np->full_name);
666 goto err;
667 }
668 }
669
670 if (cells_name) {
671 if (of_read_u32(node, cells_name, &count)) {
672 debug("%s: could not get %s for %s\n",
673 np->full_name, cells_name,
674 node->full_name);
675 goto err;
676 }
677 } else {
678 count = cell_count;
679 }
680
681 /*
682 * Make sure that the arguments actually fit in the
683 * remaining property data length
684 */
685 if (list + count > list_end) {
686 debug("%s: arguments longer than property\n",
687 np->full_name);
688 goto err;
689 }
690 }
691
692 /*
693 * All of the error cases above bail out of the loop, so at
694 * this point, the parsing is successful. If the requested
695 * index matches, then fill the out_args structure and return,
696 * or return -ENOENT for an empty entry.
697 */
698 rc = -ENOENT;
699 if (cur_index == index) {
700 if (!phandle)
701 goto err;
702
703 if (out_args) {
704 int i;
705 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
706 count = OF_MAX_PHANDLE_ARGS;
707 out_args->np = node;
708 out_args->args_count = count;
709 for (i = 0; i < count; i++)
710 out_args->args[i] =
711 be32_to_cpup(list++);
712 } else {
713 of_node_put(node);
714 }
715
716 /* Found it! return success */
717 return 0;
718 }
719
720 of_node_put(node);
721 node = NULL;
722 list += count;
723 cur_index++;
724 }
725
726 /*
727 * Unlock node before returning result; will be one of:
728 * -ENOENT : index is for empty phandle
729 * -EINVAL : parsing error on data
730 * [1..n] : Number of phandle (count mode; when index = -1)
731 */
732 rc = index < 0 ? cur_index : -ENOENT;
733 err:
734 if (node)
735 of_node_put(node);
736 return rc;
737}
738
739struct device_node *of_parse_phandle(const struct device_node *np,
740 const char *phandle_name, int index)
741{
742 struct of_phandle_args args;
743
744 if (index < 0)
745 return NULL;
746
747 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
748 &args))
749 return NULL;
750
751 return args.np;
752}
753
754int of_parse_phandle_with_args(const struct device_node *np,
755 const char *list_name, const char *cells_name,
Patrick Delaunay21e3b042020-09-10 18:26:17 +0200756 int cell_count, int index,
757 struct of_phandle_args *out_args)
Simon Glass1ccaa052017-05-18 20:08:54 -0600758{
759 if (index < 0)
760 return -EINVAL;
761
Patrick Delaunay21e3b042020-09-10 18:26:17 +0200762 return __of_parse_phandle_with_args(np, list_name, cells_name,
763 cell_count, index, out_args);
Simon Glass1ccaa052017-05-18 20:08:54 -0600764}
765
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200766int of_count_phandle_with_args(const struct device_node *np,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200767 const char *list_name, const char *cells_name,
768 int cell_count)
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200769{
Patrick Delaunayd776a842020-09-25 09:41:14 +0200770 return __of_parse_phandle_with_args(np, list_name, cells_name,
771 cell_count, -1, NULL);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200772}
773
Simon Glass1ccaa052017-05-18 20:08:54 -0600774static void of_alias_add(struct alias_prop *ap, struct device_node *np,
775 int id, const char *stem, int stem_len)
776{
777 ap->np = np;
778 ap->id = id;
779 strncpy(ap->stem, stem, stem_len);
780 ap->stem[stem_len] = 0;
781 list_add_tail(&ap->link, &aliases_lookup);
782 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
783 ap->alias, ap->stem, ap->id, of_node_full_name(np));
784}
785
786int of_alias_scan(void)
787{
788 struct property *pp;
789
790 of_aliases = of_find_node_by_path("/aliases");
791 of_chosen = of_find_node_by_path("/chosen");
792 if (of_chosen == NULL)
793 of_chosen = of_find_node_by_path("/chosen@0");
794
795 if (of_chosen) {
796 const char *name;
797
798 name = of_get_property(of_chosen, "stdout-path", NULL);
799 if (name)
Simon Glassef75c592022-07-30 15:52:08 -0600800 of_stdout = of_find_node_opts_by_path(NULL, name,
Simon Glass1ccaa052017-05-18 20:08:54 -0600801 &of_stdout_options);
802 }
803
804 if (!of_aliases)
805 return 0;
806
807 for_each_property_of_node(of_aliases, pp) {
808 const char *start = pp->name;
809 const char *end = start + strlen(start);
810 struct device_node *np;
811 struct alias_prop *ap;
812 ulong id;
813 int len;
814
815 /* Skip those we do not want to proceed */
816 if (!strcmp(pp->name, "name") ||
817 !strcmp(pp->name, "phandle") ||
818 !strcmp(pp->name, "linux,phandle"))
819 continue;
820
821 np = of_find_node_by_path(pp->value);
822 if (!np)
823 continue;
824
825 /*
826 * walk the alias backwards to extract the id and work out
827 * the 'stem' string
828 */
829 while (isdigit(*(end-1)) && end > start)
830 end--;
831 len = end - start;
832
833 if (strict_strtoul(end, 10, &id) < 0)
834 continue;
835
836 /* Allocate an alias_prop with enough space for the stem */
837 ap = malloc(sizeof(*ap) + len + 1);
838 if (!ap)
839 return -ENOMEM;
840 memset(ap, 0, sizeof(*ap) + len + 1);
841 ap->alias = start;
842 of_alias_add(ap, np, id, start, len);
843 }
844
845 return 0;
846}
847
848int of_alias_get_id(const struct device_node *np, const char *stem)
849{
850 struct alias_prop *app;
851 int id = -ENODEV;
852
853 mutex_lock(&of_mutex);
854 list_for_each_entry(app, &aliases_lookup, link) {
855 if (strcmp(app->stem, stem) != 0)
856 continue;
857
858 if (np == app->np) {
859 id = app->id;
860 break;
861 }
862 }
863 mutex_unlock(&of_mutex);
864
865 return id;
866}
867
Michal Simekc6203cb2019-01-31 16:30:57 +0100868int of_alias_get_highest_id(const char *stem)
869{
870 struct alias_prop *app;
871 int id = -1;
872
873 mutex_lock(&of_mutex);
874 list_for_each_entry(app, &aliases_lookup, link) {
875 if (strcmp(app->stem, stem) != 0)
876 continue;
877
878 if (app->id > id)
879 id = app->id;
880 }
881 mutex_unlock(&of_mutex);
882
883 return id;
884}
885
Simon Glass1ccaa052017-05-18 20:08:54 -0600886struct device_node *of_get_stdout(void)
887{
888 return of_stdout;
889}
Simon Glass3ee3d152022-07-30 15:52:13 -0600890
891int of_write_prop(struct device_node *np, const char *propname, int len,
892 const void *value)
893{
894 struct property *pp;
895 struct property *pp_last = NULL;
896 struct property *new;
897
898 if (!np)
899 return -EINVAL;
900
901 for (pp = np->properties; pp; pp = pp->next) {
902 if (strcmp(pp->name, propname) == 0) {
903 /* Property exists -> change value */
904 pp->value = (void *)value;
905 pp->length = len;
906 return 0;
907 }
908 pp_last = pp;
909 }
910
911 if (!pp_last)
912 return -ENOENT;
913
914 /* Property does not exist -> append new property */
915 new = malloc(sizeof(struct property));
916 if (!new)
917 return -ENOMEM;
918
919 new->name = strdup(propname);
920 if (!new->name) {
921 free(new);
922 return -ENOMEM;
923 }
924
925 new->value = (void *)value;
926 new->length = len;
927 new->next = NULL;
928
929 pp_last->next = new;
930
931 return 0;
932}