blob: a4dc9bde085cce514bc56c0deb791fcc8ea7be3d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc4fc5622017-05-18 20:08:58 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassc4fc5622017-05-18 20:08:58 -06005 */
6
Simon Glasscb13a1b2022-09-06 20:27:26 -06007#define LOG_CATEGORY LOGC_DT
8
Simon Glassc4fc5622017-05-18 20:08:58 -06009#include <common.h>
10#include <dm.h>
11#include <fdtdec.h>
12#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glass722281b2023-06-01 10:22:42 -060015#include <of_live.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060017#include <dm/of_access.h>
Simon Glass049ae1b2017-05-18 20:09:01 -060018#include <dm/of_addr.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060019#include <dm/ofnode.h>
20#include <linux/err.h>
Simon Glassf7bfcc42017-07-25 08:29:55 -060021#include <linux/ioport.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060023
Simon Glasscb13a1b2022-09-06 20:27:26 -060024DECLARE_GLOBAL_DATA_PTR;
25
26#if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE)
27static void *oftree_list[CONFIG_OFNODE_MULTI_TREE_MAX];
28static int oftree_count;
29
30void oftree_reset(void)
31{
32 if (gd->flags & GD_FLG_RELOC) {
33 oftree_count = 0;
34 oftree_list[oftree_count++] = (void *)gd->fdt_blob;
35 }
36}
37
38static int oftree_find(const void *fdt)
39{
40 int i;
41
42 for (i = 0; i < oftree_count; i++) {
43 if (fdt == oftree_list[i])
44 return i;
45 }
46
47 return -1;
48}
49
50static oftree oftree_ensure(void *fdt)
51{
52 oftree tree;
53 int i;
54
Simon Glass722281b2023-06-01 10:22:42 -060055 if (of_live_active()) {
56 struct device_node *root;
57 int ret;
58
59 ret = unflatten_device_tree(fdt, &root);
60 if (ret) {
61 log_err("Failed to create live tree: err=%d\n", ret);
62 return oftree_null();
63 }
64 tree = oftree_from_np(root);
65
66 return tree;
67 }
68
Simon Glasscb13a1b2022-09-06 20:27:26 -060069 if (gd->flags & GD_FLG_RELOC) {
70 i = oftree_find(fdt);
71 if (i == -1) {
72 if (oftree_count == CONFIG_OFNODE_MULTI_TREE_MAX) {
73 log_warning("Too many registered device trees (max %d)\n",
74 CONFIG_OFNODE_MULTI_TREE_MAX);
75 return oftree_null();
76 }
77
78 /* register the new tree */
79 i = oftree_count++;
80 oftree_list[i] = fdt;
81 log_debug("oftree: registered tree %d: %p\n", i, fdt);
82 }
83 } else {
84 if (fdt != gd->fdt_blob) {
Simon Glass67f8e112023-06-01 10:22:41 -060085 log_debug("Only the control FDT can be accessed before relocation\n");
Simon Glasscb13a1b2022-09-06 20:27:26 -060086 return oftree_null();
87 }
88 }
89
90 tree.fdt = fdt;
91
92 return tree;
93}
94
Simon Glass722281b2023-06-01 10:22:42 -060095void oftree_dispose(oftree tree)
96{
97 if (of_live_active())
98 of_live_free(tree.np);
99}
100
Simon Glasscb13a1b2022-09-06 20:27:26 -0600101void *ofnode_lookup_fdt(ofnode node)
102{
103 if (gd->flags & GD_FLG_RELOC) {
104 uint i = OFTREE_TREE_ID(node.of_offset);
105
Dan Carpenter408ccac2023-07-26 09:59:52 +0300106 if (i >= oftree_count) {
Simon Glasscb13a1b2022-09-06 20:27:26 -0600107 log_debug("Invalid tree ID %x\n", i);
108 return NULL;
109 }
110
111 return oftree_list[i];
112 } else {
113 return (void *)gd->fdt_blob;
114 }
115}
116
117void *ofnode_to_fdt(ofnode node)
118{
119#ifdef OF_CHECKS
120 if (of_live_active())
121 return NULL;
122#endif
123 if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && ofnode_valid(node))
124 return ofnode_lookup_fdt(node);
125
126 /* Use the control FDT by default */
127 return (void *)gd->fdt_blob;
128}
129
Simon Glass45ae59d2022-09-06 20:27:24 -0600130/**
Simon Glasscb13a1b2022-09-06 20:27:26 -0600131 * ofnode_to_offset() - convert an ofnode to a flat DT offset
132 *
133 * This cannot be called if the reference contains a node pointer.
134 *
135 * @node: Reference containing offset (possibly invalid)
136 * Return: DT offset (can be -1)
137 */
138int ofnode_to_offset(ofnode node)
139{
140#ifdef OF_CHECKS
141 if (of_live_active())
142 return -1;
143#endif
144 if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && node.of_offset >= 0)
145 return OFTREE_OFFSET(node.of_offset);
146
147 return node.of_offset;
148}
149
150oftree oftree_from_fdt(void *fdt)
151{
152 oftree tree;
153
154 if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE))
155 return oftree_ensure(fdt);
156
Simon Glass994048b2023-06-01 10:22:31 -0600157#ifdef OF_CHECKS
158 if (of_live_active())
159 return oftree_null();
160#endif
Simon Glasscb13a1b2022-09-06 20:27:26 -0600161 tree.fdt = fdt;
162
163 return tree;
164}
165
166/**
167 * noffset_to_ofnode() - convert a DT offset to an ofnode
168 *
169 * @other_node: Node in the same tree to use as a reference
170 * @of_offset: DT offset (either valid, or -1)
171 * Return: reference to the associated DT offset
172 */
173ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
174{
175 ofnode node;
176
177 if (of_live_active())
178 node.np = NULL;
179 else if (!CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) || of_offset < 0 ||
180 !ofnode_valid(other_node))
181 node.of_offset = of_offset;
182 else
183 node.of_offset = OFTREE_MAKE_NODE(other_node.of_offset,
184 of_offset);
185
186 return node;
187}
188
189#else /* !OFNODE_MULTI_TREE */
190
191static inline int oftree_find(const void *fdt)
192{
193 return 0;
194}
195
196#endif /* OFNODE_MULTI_TREE */
197
198/**
Simon Glass45ae59d2022-09-06 20:27:24 -0600199 * ofnode_from_tree_offset() - get an ofnode from a tree offset (flat tree)
200 *
Simon Glasscb13a1b2022-09-06 20:27:26 -0600201 * Looks up the tree and returns an ofnode with the correct of_offset (i.e.
202 * containing the tree ID).
Simon Glass45ae59d2022-09-06 20:27:24 -0600203 *
Simon Glasscb13a1b2022-09-06 20:27:26 -0600204 * If @offset is < 0 then this returns an ofnode with that offset and no tree
205 * ID.
Simon Glass45ae59d2022-09-06 20:27:24 -0600206 *
207 * @tree: tree to check
208 * @offset: offset within that tree (can be < 0)
Simon Glasscb13a1b2022-09-06 20:27:26 -0600209 * @return node for that offset, with the correct ID
Simon Glass45ae59d2022-09-06 20:27:24 -0600210 */
211static ofnode ofnode_from_tree_offset(oftree tree, int offset)
212{
213 ofnode node;
214
Simon Glasscb13a1b2022-09-06 20:27:26 -0600215 if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && offset >= 0) {
216 int tree_id = oftree_find(tree.fdt);
217
218 if (tree_id == -1)
219 return ofnode_null();
220 node.of_offset = OFTREE_NODE(tree_id, offset);
221 } else {
222 node.of_offset = offset;
223 }
Simon Glass45ae59d2022-09-06 20:27:24 -0600224
225 return node;
226}
227
Kishon Vijay Abraham Id6388f22021-07-21 21:28:30 +0530228bool ofnode_name_eq(ofnode node, const char *name)
229{
230 const char *node_name;
231 size_t len;
232
233 assert(ofnode_valid(node));
234
235 node_name = ofnode_get_name(node);
236 len = strchrnul(node_name, '@') - node_name;
237
238 return (strlen(name) == len) && !strncmp(node_name, name, len);
239}
240
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +0200241int ofnode_read_u8(ofnode node, const char *propname, u8 *outp)
242{
243 const u8 *cell;
244 int len;
245
246 assert(ofnode_valid(node));
247 debug("%s: %s: ", __func__, propname);
248
249 if (ofnode_is_np(node))
250 return of_read_u8(ofnode_to_np(node), propname, outp);
251
252 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
253 &len);
254 if (!cell || len < sizeof(*cell)) {
255 debug("(not found)\n");
256 return -EINVAL;
257 }
258 *outp = *cell;
259 debug("%#x (%d)\n", *outp, *outp);
260
261 return 0;
262}
263
264u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def)
265{
266 assert(ofnode_valid(node));
267 ofnode_read_u8(node, propname, &def);
268
269 return def;
270}
271
272int ofnode_read_u16(ofnode node, const char *propname, u16 *outp)
273{
274 const fdt16_t *cell;
275 int len;
276
277 assert(ofnode_valid(node));
278 debug("%s: %s: ", __func__, propname);
279
280 if (ofnode_is_np(node))
281 return of_read_u16(ofnode_to_np(node), propname, outp);
282
283 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
284 &len);
285 if (!cell || len < sizeof(*cell)) {
286 debug("(not found)\n");
287 return -EINVAL;
288 }
289 *outp = be16_to_cpup(cell);
290 debug("%#x (%d)\n", *outp, *outp);
291
292 return 0;
293}
294
295u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def)
296{
297 assert(ofnode_valid(node));
298 ofnode_read_u16(node, propname, &def);
299
300 return def;
301}
302
Simon Glassc4fc5622017-05-18 20:08:58 -0600303int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
304{
Dario Binacchib3f1cdd2020-03-29 18:04:42 +0200305 return ofnode_read_u32_index(node, propname, 0, outp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600306}
307
Trent Piepho5b775412019-05-10 17:48:20 +0000308u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
Simon Glassc4fc5622017-05-18 20:08:58 -0600309{
310 assert(ofnode_valid(node));
Dario Binacchib3f1cdd2020-03-29 18:04:42 +0200311 ofnode_read_u32_index(node, propname, 0, &def);
Simon Glassc4fc5622017-05-18 20:08:58 -0600312
313 return def;
314}
315
Dario Binacchi81d80b52020-03-29 18:04:41 +0200316int ofnode_read_u32_index(ofnode node, const char *propname, int index,
317 u32 *outp)
318{
319 const fdt32_t *cell;
320 int len;
321
322 assert(ofnode_valid(node));
323 debug("%s: %s: ", __func__, propname);
324
325 if (ofnode_is_np(node))
326 return of_read_u32_index(ofnode_to_np(node), propname, index,
327 outp);
328
Simon Glass04fa09a2022-09-06 20:27:20 -0600329 cell = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
330 propname, &len);
Dario Binacchi81d80b52020-03-29 18:04:41 +0200331 if (!cell) {
332 debug("(not found)\n");
333 return -EINVAL;
334 }
335
336 if (len < (sizeof(int) * (index + 1))) {
337 debug("(not large enough)\n");
338 return -EOVERFLOW;
339 }
340
341 *outp = fdt32_to_cpu(cell[index]);
342 debug("%#x (%d)\n", *outp, *outp);
343
344 return 0;
345}
346
347u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
348 u32 def)
349{
350 assert(ofnode_valid(node));
351 ofnode_read_u32_index(node, propname, index, &def);
352
353 return def;
354}
355
Simon Glassc4fc5622017-05-18 20:08:58 -0600356int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
357{
358 assert(ofnode_valid(node));
359 ofnode_read_u32(node, propname, (u32 *)&def);
360
361 return def;
362}
363
Simon Glass9d54a7a2018-06-11 13:07:10 -0600364int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
365{
Jean-Jacques Hiblot487f9172019-10-22 10:05:22 +0200366 const unaligned_fdt64_t *cell;
Simon Glass9d54a7a2018-06-11 13:07:10 -0600367 int len;
368
369 assert(ofnode_valid(node));
370 debug("%s: %s: ", __func__, propname);
371
372 if (ofnode_is_np(node))
373 return of_read_u64(ofnode_to_np(node), propname, outp);
374
Simon Glass04fa09a2022-09-06 20:27:20 -0600375 cell = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
376 propname, &len);
Simon Glass9d54a7a2018-06-11 13:07:10 -0600377 if (!cell || len < sizeof(*cell)) {
378 debug("(not found)\n");
379 return -EINVAL;
380 }
381 *outp = fdt64_to_cpu(cell[0]);
382 debug("%#llx (%lld)\n", (unsigned long long)*outp,
383 (unsigned long long)*outp);
384
385 return 0;
386}
387
T Karthik Reddy478860d2019-09-02 16:34:30 +0200388u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
Simon Glass9d54a7a2018-06-11 13:07:10 -0600389{
390 assert(ofnode_valid(node));
391 ofnode_read_u64(node, propname, &def);
392
393 return def;
394}
395
Simon Glassc4fc5622017-05-18 20:08:58 -0600396bool ofnode_read_bool(ofnode node, const char *propname)
397{
Masahiro Yamada5d434452017-06-22 16:54:07 +0900398 const void *prop;
Simon Glassc4fc5622017-05-18 20:08:58 -0600399
400 assert(ofnode_valid(node));
401 debug("%s: %s: ", __func__, propname);
402
Masahiro Yamada5d434452017-06-22 16:54:07 +0900403 prop = ofnode_get_property(node, propname, NULL);
404
405 debug("%s\n", prop ? "true" : "false");
Simon Glassc4fc5622017-05-18 20:08:58 -0600406
Masahiro Yamada5d434452017-06-22 16:54:07 +0900407 return prop ? true : false;
Simon Glassc4fc5622017-05-18 20:08:58 -0600408}
409
Simon Glass0c2e9802020-01-27 08:49:44 -0700410const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600411{
Simon Glass0c2e9802020-01-27 08:49:44 -0700412 const char *val = NULL;
413 int len;
Simon Glassc4fc5622017-05-18 20:08:58 -0600414
415 assert(ofnode_valid(node));
416 debug("%s: %s: ", __func__, propname);
417
418 if (ofnode_is_np(node)) {
419 struct property *prop = of_find_property(
Simon Glass0c2e9802020-01-27 08:49:44 -0700420 ofnode_to_np(node), propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600421
422 if (prop) {
Simon Glass0c2e9802020-01-27 08:49:44 -0700423 val = prop->value;
Simon Glassc4fc5622017-05-18 20:08:58 -0600424 len = prop->length;
425 }
426 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -0600427 val = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
Simon Glassc4fc5622017-05-18 20:08:58 -0600428 propname, &len);
429 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700430 if (!val) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600431 debug("<not found>\n");
Simon Glass0c2e9802020-01-27 08:49:44 -0700432 if (sizep)
433 *sizep = -FDT_ERR_NOTFOUND;
Simon Glassc4fc5622017-05-18 20:08:58 -0600434 return NULL;
435 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700436 if (sizep)
437 *sizep = len;
438
439 return val;
440}
441
442const char *ofnode_read_string(ofnode node, const char *propname)
443{
444 const char *str;
445 int len;
446
447 str = ofnode_read_prop(node, propname, &len);
448 if (!str)
449 return NULL;
450
Simon Glassc4fc5622017-05-18 20:08:58 -0600451 if (strnlen(str, len) >= len) {
452 debug("<invalid>\n");
453 return NULL;
454 }
455 debug("%s\n", str);
456
457 return str;
458}
459
Simon Glass81c54b32020-01-27 08:49:45 -0700460int ofnode_read_size(ofnode node, const char *propname)
461{
462 int len;
463
464 if (!ofnode_read_prop(node, propname, &len))
465 return -EINVAL;
466
467 return len;
468}
469
Simon Glassc4fc5622017-05-18 20:08:58 -0600470ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
471{
472 ofnode subnode;
473
474 assert(ofnode_valid(node));
475 debug("%s: %s: ", __func__, subnode_name);
476
477 if (ofnode_is_np(node)) {
Simon Glass9036c5c2022-09-06 20:27:04 -0600478 struct device_node *np = ofnode_to_np(node);
Simon Glassc4fc5622017-05-18 20:08:58 -0600479
480 for (np = np->child; np; np = np->sibling) {
481 if (!strcmp(subnode_name, np->name))
482 break;
483 }
484 subnode = np_to_ofnode(np);
485 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -0600486 int ooffset = fdt_subnode_offset(ofnode_to_fdt(node),
Simon Glassc4fc5622017-05-18 20:08:58 -0600487 ofnode_to_offset(node), subnode_name);
Simon Glass37dcd912022-09-06 20:27:23 -0600488 subnode = noffset_to_ofnode(node, ooffset);
Simon Glassc4fc5622017-05-18 20:08:58 -0600489 }
490 debug("%s\n", ofnode_valid(subnode) ?
491 ofnode_get_name(subnode) : "<none>");
492
493 return subnode;
494}
495
496int ofnode_read_u32_array(ofnode node, const char *propname,
497 u32 *out_values, size_t sz)
498{
499 assert(ofnode_valid(node));
500 debug("%s: %s: ", __func__, propname);
501
502 if (ofnode_is_np(node)) {
503 return of_read_u32_array(ofnode_to_np(node), propname,
504 out_values, sz);
505 } else {
Simon Glasse3be5fc2022-09-06 20:27:18 -0600506 int ret;
507
Simon Glass04fa09a2022-09-06 20:27:20 -0600508 ret = fdtdec_get_int_array(ofnode_to_fdt(node),
Simon Glasse3be5fc2022-09-06 20:27:18 -0600509 ofnode_to_offset(node), propname,
510 out_values, sz);
511
512 /* get the error right, but space is more important in SPL */
513 if (!IS_ENABLED(CONFIG_SPL_BUILD)) {
514 if (ret == -FDT_ERR_NOTFOUND)
515 return -EINVAL;
516 else if (ret == -FDT_ERR_BADLAYOUT)
517 return -EOVERFLOW;
518 }
519 return ret;
Simon Glassc4fc5622017-05-18 20:08:58 -0600520 }
521}
522
Simon Glass39f1d282020-12-16 17:25:06 -0700523#if !CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass5de5b3b2020-11-28 17:50:02 -0700524bool ofnode_is_enabled(ofnode node)
525{
526 if (ofnode_is_np(node)) {
527 return of_device_is_available(ofnode_to_np(node));
528 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -0600529 return fdtdec_get_is_enabled(ofnode_to_fdt(node),
Simon Glass5de5b3b2020-11-28 17:50:02 -0700530 ofnode_to_offset(node));
531 }
532}
533
Simon Glassc4fc5622017-05-18 20:08:58 -0600534ofnode ofnode_first_subnode(ofnode node)
535{
536 assert(ofnode_valid(node));
537 if (ofnode_is_np(node))
538 return np_to_ofnode(node.np->child);
539
Simon Glass37dcd912022-09-06 20:27:23 -0600540 return noffset_to_ofnode(node,
Simon Glass04fa09a2022-09-06 20:27:20 -0600541 fdt_first_subnode(ofnode_to_fdt(node), ofnode_to_offset(node)));
Simon Glassc4fc5622017-05-18 20:08:58 -0600542}
543
544ofnode ofnode_next_subnode(ofnode node)
545{
546 assert(ofnode_valid(node));
547 if (ofnode_is_np(node))
548 return np_to_ofnode(node.np->sibling);
549
Simon Glass37dcd912022-09-06 20:27:23 -0600550 return noffset_to_ofnode(node,
Simon Glass04fa09a2022-09-06 20:27:20 -0600551 fdt_next_subnode(ofnode_to_fdt(node), ofnode_to_offset(node)));
Simon Glassc4fc5622017-05-18 20:08:58 -0600552}
Simon Glass39f1d282020-12-16 17:25:06 -0700553#endif /* !DM_INLINE_OFNODE */
Simon Glassc4fc5622017-05-18 20:08:58 -0600554
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100555ofnode ofnode_get_parent(ofnode node)
556{
557 ofnode parent;
558
559 assert(ofnode_valid(node));
560 if (ofnode_is_np(node))
561 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
562 else
Simon Glass04fa09a2022-09-06 20:27:20 -0600563 parent.of_offset = fdt_parent_offset(ofnode_to_fdt(node),
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100564 ofnode_to_offset(node));
565
566 return parent;
567}
568
Simon Glassc4fc5622017-05-18 20:08:58 -0600569const char *ofnode_get_name(ofnode node)
570{
Kever Yang8d7976d2019-07-19 11:23:47 +0800571 if (!ofnode_valid(node)) {
572 debug("%s node not valid\n", __func__);
573 return NULL;
574 }
575
Simon Glassc4fc5622017-05-18 20:08:58 -0600576 if (ofnode_is_np(node))
Simon Glass91d89a82022-09-06 20:27:15 -0600577 return node.np->name;
Simon Glassc4fc5622017-05-18 20:08:58 -0600578
Simon Glass04fa09a2022-09-06 20:27:20 -0600579 return fdt_get_name(ofnode_to_fdt(node), ofnode_to_offset(node), NULL);
Simon Glassc4fc5622017-05-18 20:08:58 -0600580}
581
Marek Behúne897e3c2021-05-26 14:08:18 +0200582int ofnode_get_path(ofnode node, char *buf, int buflen)
583{
584 assert(ofnode_valid(node));
585
586 if (ofnode_is_np(node)) {
587 if (strlen(node.np->full_name) >= buflen)
588 return -ENOSPC;
589
590 strcpy(buf, node.np->full_name);
591
592 return 0;
593 } else {
594 int res;
595
Simon Glass04fa09a2022-09-06 20:27:20 -0600596 res = fdt_get_path(ofnode_to_fdt(node), ofnode_to_offset(node), buf,
Marek Behúne897e3c2021-05-26 14:08:18 +0200597 buflen);
598 if (!res)
599 return res;
600 else if (res == -FDT_ERR_NOSPACE)
601 return -ENOSPC;
602 else
603 return -EINVAL;
604 }
605}
606
Kever Yang37df0e02018-02-23 17:38:50 +0100607ofnode ofnode_get_by_phandle(uint phandle)
608{
609 ofnode node;
610
611 if (of_live_active())
Simon Glass176dd432022-09-06 20:26:57 -0600612 node = np_to_ofnode(of_find_node_by_phandle(NULL, phandle));
Kever Yang37df0e02018-02-23 17:38:50 +0100613 else
614 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
615 phandle);
616
617 return node;
618}
619
Simon Glass95fd2092022-09-06 20:27:22 -0600620ofnode oftree_get_by_phandle(oftree tree, uint phandle)
621{
622 ofnode node;
623
624 if (of_live_active())
625 node = np_to_ofnode(of_find_node_by_phandle(tree.np, phandle));
626 else
Simon Glassf912a722022-09-06 20:27:27 -0600627 node = ofnode_from_tree_offset(tree,
Simon Glass95fd2092022-09-06 20:27:22 -0600628 fdt_node_offset_by_phandle(oftree_lookup_fdt(tree),
Simon Glassf912a722022-09-06 20:27:27 -0600629 phandle));
Simon Glass95fd2092022-09-06 20:27:22 -0600630
631 return node;
632}
633
Marek Behún177ab7f2021-05-26 14:08:17 +0200634static fdt_addr_t __ofnode_get_addr_size_index(ofnode node, int index,
635 fdt_size_t *size, bool translate)
Simon Glass049ae1b2017-05-18 20:09:01 -0600636{
Keerthy34222a32018-11-19 11:44:47 +0530637 int na, ns;
Keerthy34222a32018-11-19 11:44:47 +0530638
Chen Guanqiao2d1034b2021-07-12 15:40:20 +0800639 if (size)
640 *size = FDT_SIZE_T_NONE;
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800641
Simon Glass049ae1b2017-05-18 20:09:01 -0600642 if (ofnode_is_np(node)) {
643 const __be32 *prop_val;
Simon Glass47f85de2019-09-25 08:55:50 -0600644 u64 size64;
Simon Glass049ae1b2017-05-18 20:09:01 -0600645 uint flags;
Simon Glass049ae1b2017-05-18 20:09:01 -0600646
Simon Glass47f85de2019-09-25 08:55:50 -0600647 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
648 &flags);
Simon Glass049ae1b2017-05-18 20:09:01 -0600649 if (!prop_val)
650 return FDT_ADDR_T_NONE;
Chen Guanqiao2d1034b2021-07-12 15:40:20 +0800651
Simon Glass47f85de2019-09-25 08:55:50 -0600652 if (size)
653 *size = size64;
Mario Sixd007ebc2017-12-20 09:52:12 +0100654
Mario Six35616ef2018-03-12 14:53:33 +0100655 ns = of_n_size_cells(ofnode_to_np(node));
656
Marek Behún177ab7f2021-05-26 14:08:17 +0200657 if (translate && IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100658 return of_translate_address(ofnode_to_np(node), prop_val);
659 } else {
660 na = of_n_addr_cells(ofnode_to_np(node));
661 return of_read_number(prop_val, na);
662 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600663 } else {
Keerthy34222a32018-11-19 11:44:47 +0530664 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
665 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
Simon Glass04fa09a2022-09-06 20:27:20 -0600666 return fdtdec_get_addr_size_fixed(ofnode_to_fdt(node),
Keerthy34222a32018-11-19 11:44:47 +0530667 ofnode_to_offset(node), "reg",
Marek Behún177ab7f2021-05-26 14:08:17 +0200668 index, na, ns, size,
669 translate);
Simon Glass049ae1b2017-05-18 20:09:01 -0600670 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600671}
672
Marek Behún177ab7f2021-05-26 14:08:17 +0200673fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
674{
675 return __ofnode_get_addr_size_index(node, index, size, true);
676}
677
678fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
679 fdt_size_t *size)
680{
681 return __ofnode_get_addr_size_index(node, index, size, false);
682}
683
Keerthyd332e6e2019-04-24 17:19:53 +0530684fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
685{
686 fdt_size_t size;
687
688 return ofnode_get_addr_size_index(node, index, &size);
689}
690
Simon Glass049ae1b2017-05-18 20:09:01 -0600691fdt_addr_t ofnode_get_addr(ofnode node)
692{
693 return ofnode_get_addr_index(node, 0);
694}
695
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800696fdt_size_t ofnode_get_size(ofnode node)
697{
698 fdt_size_t size;
699
700 ofnode_get_addr_size_index(node, 0, &size);
701
702 return size;
703}
704
Simon Glassc4fc5622017-05-18 20:08:58 -0600705int ofnode_stringlist_search(ofnode node, const char *property,
706 const char *string)
707{
708 if (ofnode_is_np(node)) {
709 return of_property_match_string(ofnode_to_np(node),
710 property, string);
711 } else {
712 int ret;
713
Simon Glass04fa09a2022-09-06 20:27:20 -0600714 ret = fdt_stringlist_search(ofnode_to_fdt(node),
Simon Glassc4fc5622017-05-18 20:08:58 -0600715 ofnode_to_offset(node), property,
716 string);
717 if (ret == -FDT_ERR_NOTFOUND)
718 return -ENODATA;
719 else if (ret < 0)
720 return -EINVAL;
721
722 return ret;
723 }
724}
725
726int ofnode_read_string_index(ofnode node, const char *property, int index,
727 const char **outp)
728{
729 if (ofnode_is_np(node)) {
730 return of_property_read_string_index(ofnode_to_np(node),
731 property, index, outp);
732 } else {
733 int len;
734
Simon Glass04fa09a2022-09-06 20:27:20 -0600735 *outp = fdt_stringlist_get(ofnode_to_fdt(node),
736 ofnode_to_offset(node),
Simon Glassc4fc5622017-05-18 20:08:58 -0600737 property, index, &len);
738 if (len < 0)
739 return -EINVAL;
740 return 0;
741 }
742}
743
Simon Glass5fdb0052017-06-12 06:21:28 -0600744int ofnode_read_string_count(ofnode node, const char *property)
745{
746 if (ofnode_is_np(node)) {
747 return of_property_count_strings(ofnode_to_np(node), property);
748 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -0600749 return fdt_stringlist_count(ofnode_to_fdt(node),
Simon Glass5fdb0052017-06-12 06:21:28 -0600750 ofnode_to_offset(node), property);
751 }
752}
753
Simon Glass9580bfc2021-10-23 17:26:07 -0600754int ofnode_read_string_list(ofnode node, const char *property,
755 const char ***listp)
756{
757 const char **prop;
758 int count;
759 int i;
760
761 *listp = NULL;
762 count = ofnode_read_string_count(node, property);
763 if (count < 0)
764 return count;
765 if (!count)
766 return 0;
767
768 prop = calloc(count + 1, sizeof(char *));
769 if (!prop)
770 return -ENOMEM;
771
772 for (i = 0; i < count; i++)
773 ofnode_read_string_index(node, property, i, &prop[i]);
774 prop[count] = NULL;
775 *listp = prop;
776
777 return count;
778}
779
Simon Glassc4fc5622017-05-18 20:08:58 -0600780static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
781 struct ofnode_phandle_args *out)
782{
783 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
784 out->node = offset_to_ofnode(in->node);
785 out->args_count = in->args_count;
786 memcpy(out->args, in->args, sizeof(out->args));
787}
788
789static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
790 struct ofnode_phandle_args *out)
791{
792 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
793 out->node = np_to_ofnode(in->np);
794 out->args_count = in->args_count;
795 memcpy(out->args, in->args, sizeof(out->args));
796}
797
798int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
799 const char *cells_name, int cell_count,
800 int index,
801 struct ofnode_phandle_args *out_args)
802{
803 if (ofnode_is_np(node)) {
804 struct of_phandle_args args;
805 int ret;
806
807 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Patrick Delaunay21e3b042020-09-10 18:26:17 +0200808 list_name, cells_name,
809 cell_count, index,
Mario Sixf40d82c2018-01-15 11:07:17 +0100810 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600811 if (ret)
812 return ret;
813 ofnode_from_of_phandle_args(&args, out_args);
814 } else {
815 struct fdtdec_phandle_args args;
816 int ret;
817
Simon Glass04fa09a2022-09-06 20:27:20 -0600818 ret = fdtdec_parse_phandle_with_args(ofnode_to_fdt(node),
Mario Sixf40d82c2018-01-15 11:07:17 +0100819 ofnode_to_offset(node),
820 list_name, cells_name,
821 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600822 if (ret)
823 return ret;
824 ofnode_from_fdtdec_phandle_args(&args, out_args);
825 }
826
827 return 0;
828}
829
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200830int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200831 const char *cells_name, int cell_count)
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200832{
833 if (ofnode_is_np(node))
834 return of_count_phandle_with_args(ofnode_to_np(node),
Patrick Delaunayd776a842020-09-25 09:41:14 +0200835 list_name, cells_name, cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200836 else
Simon Glass04fa09a2022-09-06 20:27:20 -0600837 return fdtdec_parse_phandle_with_args(ofnode_to_fdt(node),
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200838 ofnode_to_offset(node), list_name, cells_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200839 cell_count, -1, NULL);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200840}
841
Simon Glassc4fc5622017-05-18 20:08:58 -0600842ofnode ofnode_path(const char *path)
843{
844 if (of_live_active())
845 return np_to_ofnode(of_find_node_by_path(path));
846 else
847 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
848}
849
Simon Glass45ae59d2022-09-06 20:27:24 -0600850ofnode oftree_root(oftree tree)
Simon Glassef75c592022-07-30 15:52:08 -0600851{
Simon Glass45ae59d2022-09-06 20:27:24 -0600852 if (of_live_active()) {
853 return np_to_ofnode(tree.np);
854 } else {
855 return ofnode_from_tree_offset(tree, 0);
856 }
857}
858
859ofnode oftree_path(oftree tree, const char *path)
860{
861 if (of_live_active()) {
Simon Glassef75c592022-07-30 15:52:08 -0600862 return np_to_ofnode(of_find_node_opts_by_path(tree.np, path,
863 NULL));
Simon Glass45ae59d2022-09-06 20:27:24 -0600864 } else if (*path != '/' && tree.fdt != gd->fdt_blob) {
Simon Glassef75c592022-07-30 15:52:08 -0600865 return ofnode_null(); /* Aliases only on control FDT */
Simon Glass45ae59d2022-09-06 20:27:24 -0600866 } else {
867 int offset = fdt_path_offset(tree.fdt, path);
868
869 return ofnode_from_tree_offset(tree, offset);
870 }
Simon Glassef75c592022-07-30 15:52:08 -0600871}
872
Simon Glasse09223c2020-01-27 08:49:46 -0700873const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600874{
875 ofnode chosen_node;
876
877 chosen_node = ofnode_path("/chosen");
878
Simon Glasse09223c2020-01-27 08:49:46 -0700879 return ofnode_read_prop(chosen_node, propname, sizep);
Simon Glassc4fc5622017-05-18 20:08:58 -0600880}
881
Simon Glasse09223c2020-01-27 08:49:46 -0700882const char *ofnode_read_chosen_string(const char *propname)
883{
884 return ofnode_read_chosen_prop(propname, NULL);
885}
886
Simon Glassc4fc5622017-05-18 20:08:58 -0600887ofnode ofnode_get_chosen_node(const char *name)
888{
889 const char *prop;
890
Simon Glasse09223c2020-01-27 08:49:46 -0700891 prop = ofnode_read_chosen_prop(name, NULL);
Simon Glassc4fc5622017-05-18 20:08:58 -0600892 if (!prop)
893 return ofnode_null();
894
895 return ofnode_path(prop);
896}
897
Michal Simek92a88622020-07-28 12:51:08 +0200898const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
899{
900 ofnode node;
901
902 node = ofnode_path("/aliases");
903
904 return ofnode_read_prop(node, propname, sizep);
905}
906
907ofnode ofnode_get_aliases_node(const char *name)
908{
909 const char *prop;
910
911 prop = ofnode_read_aliases_prop(name, NULL);
912 if (!prop)
913 return ofnode_null();
914
915 debug("%s: node_path: %s\n", __func__, prop);
916
917 return ofnode_path(prop);
918}
919
developerd93c8b42020-05-02 11:35:09 +0200920int ofnode_get_child_count(ofnode parent)
921{
922 ofnode child;
923 int num = 0;
924
925 ofnode_for_each_subnode(child, parent)
926 num++;
927
928 return num;
929}
930
Simon Glassc4fc5622017-05-18 20:08:58 -0600931static int decode_timing_property(ofnode node, const char *name,
932 struct timing_entry *result)
933{
934 int length, ret = 0;
935
936 length = ofnode_read_size(node, name);
937 if (length < 0) {
938 debug("%s: could not find property %s\n",
939 ofnode_get_name(node), name);
940 return length;
941 }
942
943 if (length == sizeof(u32)) {
944 result->typ = ofnode_read_u32_default(node, name, 0);
945 result->min = result->typ;
946 result->max = result->typ;
947 } else {
948 ret = ofnode_read_u32_array(node, name, &result->min, 3);
949 }
950
951 return ret;
952}
953
954int ofnode_decode_display_timing(ofnode parent, int index,
955 struct display_timing *dt)
956{
957 int i;
958 ofnode timings, node;
959 u32 val = 0;
960 int ret = 0;
961
962 timings = ofnode_find_subnode(parent, "display-timings");
963 if (!ofnode_valid(timings))
964 return -EINVAL;
965
Simon Glass28529762017-08-05 15:45:54 -0600966 i = 0;
967 ofnode_for_each_subnode(node, timings) {
968 if (i++ == index)
969 break;
970 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600971
972 if (!ofnode_valid(node))
973 return -EINVAL;
974
975 memset(dt, 0, sizeof(*dt));
976
977 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
978 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
979 ret |= decode_timing_property(node, "hactive", &dt->hactive);
980 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
981 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
982 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
983 ret |= decode_timing_property(node, "vactive", &dt->vactive);
984 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
985 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
986
987 dt->flags = 0;
988 val = ofnode_read_u32_default(node, "vsync-active", -1);
989 if (val != -1) {
990 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
991 DISPLAY_FLAGS_VSYNC_LOW;
992 }
993 val = ofnode_read_u32_default(node, "hsync-active", -1);
994 if (val != -1) {
995 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
996 DISPLAY_FLAGS_HSYNC_LOW;
997 }
998 val = ofnode_read_u32_default(node, "de-active", -1);
999 if (val != -1) {
1000 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
1001 DISPLAY_FLAGS_DE_LOW;
1002 }
1003 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
1004 if (val != -1) {
1005 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
1006 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
1007 }
1008
1009 if (ofnode_read_bool(node, "interlaced"))
1010 dt->flags |= DISPLAY_FLAGS_INTERLACED;
1011 if (ofnode_read_bool(node, "doublescan"))
1012 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
1013 if (ofnode_read_bool(node, "doubleclk"))
1014 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
1015
1016 return ret;
1017}
1018
Nikhil M Jainff407062023-01-31 15:35:14 +05301019int ofnode_decode_panel_timing(ofnode parent,
1020 struct display_timing *dt)
1021{
1022 ofnode timings;
1023 u32 val = 0;
1024 int ret = 0;
1025
Raphael Gallais-Poua853b922023-05-11 16:36:52 +02001026 timings = ofnode_find_subnode(parent, "panel-timing");
Nikhil M Jainff407062023-01-31 15:35:14 +05301027 if (!ofnode_valid(timings))
1028 return -EINVAL;
1029 memset(dt, 0, sizeof(*dt));
1030 ret |= decode_timing_property(timings, "hback-porch", &dt->hback_porch);
1031 ret |= decode_timing_property(timings, "hfront-porch", &dt->hfront_porch);
1032 ret |= decode_timing_property(timings, "hactive", &dt->hactive);
1033 ret |= decode_timing_property(timings, "hsync-len", &dt->hsync_len);
1034 ret |= decode_timing_property(timings, "vback-porch", &dt->vback_porch);
1035 ret |= decode_timing_property(timings, "vfront-porch", &dt->vfront_porch);
1036 ret |= decode_timing_property(timings, "vactive", &dt->vactive);
1037 ret |= decode_timing_property(timings, "vsync-len", &dt->vsync_len);
1038 ret |= decode_timing_property(timings, "clock-frequency", &dt->pixelclock);
1039 dt->flags = 0;
1040 if (!ofnode_read_u32(timings, "vsync-active", &val)) {
1041 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
1042 DISPLAY_FLAGS_VSYNC_LOW;
1043 }
1044 if (!ofnode_read_u32(timings, "hsync-active", &val)) {
1045 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
1046 DISPLAY_FLAGS_HSYNC_LOW;
1047 }
1048 if (!ofnode_read_u32(timings, "de-active", &val)) {
1049 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
1050 DISPLAY_FLAGS_DE_LOW;
1051 }
1052 if (!ofnode_read_u32(timings, "pixelclk-active", &val)) {
1053 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
1054 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
1055 }
1056 if (ofnode_read_bool(timings, "interlaced"))
1057 dt->flags |= DISPLAY_FLAGS_INTERLACED;
1058 if (ofnode_read_bool(timings, "doublescan"))
1059 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
1060 if (ofnode_read_bool(timings, "doubleclk"))
1061 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
1062
1063 return ret;
1064}
1065
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +09001066const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -06001067{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +09001068 if (ofnode_is_np(node))
1069 return of_get_property(ofnode_to_np(node), propname, lenp);
1070 else
Simon Glass04fa09a2022-09-06 20:27:20 -06001071 return fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
Simon Glassc4fc5622017-05-18 20:08:58 -06001072 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -06001073}
1074
Simon Glassfec058d2022-09-06 20:27:13 -06001075int ofnode_first_property(ofnode node, struct ofprop *prop)
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001076{
1077 prop->node = node;
1078
1079 if (ofnode_is_np(node)) {
1080 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
1081 if (!prop->prop)
1082 return -FDT_ERR_NOTFOUND;
1083 } else {
1084 prop->offset =
Simon Glass04fa09a2022-09-06 20:27:20 -06001085 fdt_first_property_offset(ofnode_to_fdt(node),
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001086 ofnode_to_offset(prop->node));
1087 if (prop->offset < 0)
1088 return prop->offset;
1089 }
1090
1091 return 0;
1092}
1093
Simon Glassfec058d2022-09-06 20:27:13 -06001094int ofnode_next_property(struct ofprop *prop)
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001095{
1096 if (ofnode_is_np(prop->node)) {
1097 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
1098 prop->prop);
1099 if (!prop->prop)
1100 return -FDT_ERR_NOTFOUND;
1101 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -06001102 prop->offset =
1103 fdt_next_property_offset(ofnode_to_fdt(prop->node),
1104 prop->offset);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001105 if (prop->offset < 0)
1106 return prop->offset;
1107 }
1108
1109 return 0;
1110}
1111
Simon Glassd0aff8b2022-09-06 20:27:14 -06001112const void *ofprop_get_property(const struct ofprop *prop,
1113 const char **propname, int *lenp)
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001114{
1115 if (ofnode_is_np(prop->node))
1116 return of_get_property_by_prop(ofnode_to_np(prop->node),
1117 prop->prop, propname, lenp);
1118 else
Simon Glass04fa09a2022-09-06 20:27:20 -06001119 return fdt_getprop_by_offset(ofnode_to_fdt(prop->node),
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001120 prop->offset,
1121 propname, lenp);
1122}
1123
Simon Glassc4fc5622017-05-18 20:08:58 -06001124fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
1125 fdt_size_t *sizep)
1126{
1127 if (ofnode_is_np(node)) {
1128 int na, ns;
1129 int psize;
1130 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +02001131 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -06001132
Klaus Gogeraf4b0212017-09-20 13:50:41 +02001133 if (!prop)
1134 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -06001135 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +02001136 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -06001137 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +02001138
Dario Binacchif8fc7032021-05-01 17:05:26 +02001139 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
Marek Vasuta9dac492018-10-01 12:37:20 +02001140 return of_translate_address(np, prop);
1141 else
1142 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -06001143 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -06001144 return fdtdec_get_addr_size(ofnode_to_fdt(node),
Simon Glassc4fc5622017-05-18 20:08:58 -06001145 ofnode_to_offset(node), property,
1146 sizep);
1147 }
1148}
1149
1150const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
1151 size_t sz)
1152{
1153 if (ofnode_is_np(node)) {
1154 const struct device_node *np = ofnode_to_np(node);
1155 int psize;
1156 const __be32 *prop = of_get_property(np, propname, &psize);
1157
1158 if (!prop || sz != psize)
1159 return NULL;
1160 return (uint8_t *)prop;
1161
1162 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -06001163 return fdtdec_locate_byte_array(ofnode_to_fdt(node),
Simon Glassc4fc5622017-05-18 20:08:58 -06001164 ofnode_to_offset(node), propname, sz);
1165 }
1166}
1167
1168int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
1169 const char *propname, struct fdt_pci_addr *addr)
1170{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +09001171 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -06001172 int len;
1173 int ret = -ENOENT;
1174
1175 debug("%s: %s: ", __func__, propname);
1176
1177 /*
1178 * If we follow the pci bus bindings strictly, we should check
1179 * the value of the node's parent node's #address-cells and
1180 * #size-cells. They need to be 3 and 2 accordingly. However,
1181 * for simplicity we skip the check here.
1182 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +09001183 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -06001184 if (!cell)
1185 goto fail;
1186
1187 if ((len % FDT_PCI_REG_SIZE) == 0) {
1188 int num = len / FDT_PCI_REG_SIZE;
1189 int i;
1190
1191 for (i = 0; i < num; i++) {
1192 debug("pci address #%d: %08lx %08lx %08lx\n", i,
1193 (ulong)fdt32_to_cpu(cell[0]),
1194 (ulong)fdt32_to_cpu(cell[1]),
1195 (ulong)fdt32_to_cpu(cell[2]));
1196 if ((fdt32_to_cpu(*cell) & type) == type) {
1197 addr->phys_hi = fdt32_to_cpu(cell[0]);
1198 addr->phys_mid = fdt32_to_cpu(cell[1]);
Simon Glassdfd43152019-09-25 08:55:46 -06001199 addr->phys_lo = fdt32_to_cpu(cell[2]);
Simon Glassc4fc5622017-05-18 20:08:58 -06001200 break;
Simon Glassc4fc5622017-05-18 20:08:58 -06001201 }
Mario Sixf40d82c2018-01-15 11:07:17 +01001202
1203 cell += (FDT_PCI_ADDR_CELLS +
1204 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -06001205 }
1206
1207 if (i == num) {
1208 ret = -ENXIO;
1209 goto fail;
1210 }
1211
1212 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -06001213 }
1214
Mario Sixf40d82c2018-01-15 11:07:17 +01001215 ret = -EINVAL;
1216
Simon Glassc4fc5622017-05-18 20:08:58 -06001217fail:
1218 debug("(not found)\n");
1219 return ret;
1220}
1221
Bin Mengfa157712018-08-03 01:14:35 -07001222int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
1223{
1224 const char *list, *end;
1225 int len;
1226
1227 list = ofnode_get_property(node, "compatible", &len);
1228 if (!list)
1229 return -ENOENT;
1230
1231 end = list + len;
1232 while (list < end) {
1233 len = strlen(list);
1234 if (len >= strlen("pciVVVV,DDDD")) {
1235 char *s = strstr(list, "pci");
1236
1237 /*
1238 * check if the string is something like pciVVVV,DDDD.RR
1239 * or just pciVVVV,DDDD
1240 */
1241 if (s && s[7] == ',' &&
1242 (s[12] == '.' || s[12] == 0)) {
1243 s += 3;
1244 *vendor = simple_strtol(s, NULL, 16);
1245
1246 s += 5;
1247 *device = simple_strtol(s, NULL, 16);
1248
1249 return 0;
1250 }
1251 }
1252 list += (len + 1);
1253 }
1254
1255 return -ENOENT;
1256}
1257
Michal Simeka253c3b2022-02-23 15:45:40 +01001258int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device)
1259{
1260 const char *list, *end;
1261 int len;
1262
1263 list = ofnode_get_property(node, "compatible", &len);
1264
1265 if (!list)
1266 return -ENOENT;
1267
1268 end = list + len;
1269 while (list < end) {
1270 len = strlen(list);
1271
Michal Simekc633cdf2022-10-31 17:08:44 -07001272 if (len >= strlen("ethernet-phy-idVVVV.DDDD")) {
Michal Simeka253c3b2022-02-23 15:45:40 +01001273 char *s = strstr(list, "ethernet-phy-id");
1274
1275 /*
1276 * check if the string is something like
Michal Simekc633cdf2022-10-31 17:08:44 -07001277 * ethernet-phy-idVVVV.DDDD
Michal Simeka253c3b2022-02-23 15:45:40 +01001278 */
1279 if (s && s[19] == '.') {
1280 s += strlen("ethernet-phy-id");
1281 *vendor = simple_strtol(s, NULL, 16);
1282 s += 5;
1283 *device = simple_strtol(s, NULL, 16);
1284
1285 return 0;
1286 }
1287 }
1288 list += (len + 1);
1289 }
1290
1291 return -ENOENT;
1292}
1293
Simon Glassc4fc5622017-05-18 20:08:58 -06001294int ofnode_read_addr_cells(ofnode node)
1295{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001296 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -06001297 return of_n_addr_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001298 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -06001299 int parent = fdt_parent_offset(ofnode_to_fdt(node),
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001300 ofnode_to_offset(node));
1301
Simon Glass04fa09a2022-09-06 20:27:20 -06001302 return fdt_address_cells(ofnode_to_fdt(node), parent);
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001303 }
Simon Glassc4fc5622017-05-18 20:08:58 -06001304}
1305
1306int ofnode_read_size_cells(ofnode node)
1307{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001308 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -06001309 return of_n_size_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001310 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -06001311 int parent = fdt_parent_offset(ofnode_to_fdt(node),
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001312 ofnode_to_offset(node));
1313
Simon Glass04fa09a2022-09-06 20:27:20 -06001314 return fdt_size_cells(ofnode_to_fdt(node), parent);
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001315 }
Simon Glass4191dc12017-06-12 06:21:31 -06001316}
1317
1318int ofnode_read_simple_addr_cells(ofnode node)
1319{
1320 if (ofnode_is_np(node))
1321 return of_simple_addr_cells(ofnode_to_np(node));
1322 else
Simon Glass04fa09a2022-09-06 20:27:20 -06001323 return fdt_address_cells(ofnode_to_fdt(node),
1324 ofnode_to_offset(node));
Simon Glass4191dc12017-06-12 06:21:31 -06001325}
1326
1327int ofnode_read_simple_size_cells(ofnode node)
1328{
1329 if (ofnode_is_np(node))
1330 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -06001331 else
Simon Glass04fa09a2022-09-06 20:27:20 -06001332 return fdt_size_cells(ofnode_to_fdt(node),
1333 ofnode_to_offset(node));
Simon Glassc4fc5622017-05-18 20:08:58 -06001334}
1335
1336bool ofnode_pre_reloc(ofnode node)
1337{
Patrick Delaunay0b025b82019-02-11 12:49:57 +01001338#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
1339 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
Simon Glassfc1aa352023-02-13 08:56:34 -07001340 * had property bootph-all or bootph-pre-sram/bootph-pre-ram.
Patrick Delaunay0b025b82019-02-11 12:49:57 +01001341 * They are removed in final dtb (fdtgrep 2nd pass)
1342 */
1343 return true;
1344#else
Simon Glassfc1aa352023-02-13 08:56:34 -07001345 if (ofnode_read_bool(node, "bootph-all"))
Simon Glassc4fc5622017-05-18 20:08:58 -06001346 return true;
Simon Glassfc1aa352023-02-13 08:56:34 -07001347 if (ofnode_read_bool(node, "bootph-some-ram"))
Simon Glass23f22842018-10-01 12:22:18 -06001348 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -06001349
Simon Glassc4fc5622017-05-18 20:08:58 -06001350 /*
1351 * In regular builds individual spl and tpl handling both
1352 * count as handled pre-relocation for later second init.
1353 */
Simon Glassfc1aa352023-02-13 08:56:34 -07001354 if (ofnode_read_bool(node, "bootph-pre-ram") ||
1355 ofnode_read_bool(node, "bootph-pre-sram"))
Simon Glassc4fc5622017-05-18 20:08:58 -06001356 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -06001357
Simon Glassc2727e52023-02-13 08:56:32 -07001358 if (IS_ENABLED(CONFIG_OF_TAG_MIGRATE)) {
1359 /* detect and handle old tags */
1360 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc") ||
1361 ofnode_read_bool(node, "u-boot,dm-pre-proper") ||
1362 ofnode_read_bool(node, "u-boot,dm-spl") ||
1363 ofnode_read_bool(node, "u-boot,dm-tpl") ||
1364 ofnode_read_bool(node, "u-boot,dm-vpl")) {
1365 gd->flags |= GD_FLG_OF_TAG_MIGRATE;
1366 return true;
1367 }
1368 }
1369
Simon Glassc4fc5622017-05-18 20:08:58 -06001370 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +01001371#endif
Simon Glassc4fc5622017-05-18 20:08:58 -06001372}
Simon Glassf7bfcc42017-07-25 08:29:55 -06001373
1374int ofnode_read_resource(ofnode node, uint index, struct resource *res)
1375{
1376 if (ofnode_is_np(node)) {
1377 return of_address_to_resource(ofnode_to_np(node), index, res);
1378 } else {
1379 struct fdt_resource fres;
1380 int ret;
1381
Simon Glass04fa09a2022-09-06 20:27:20 -06001382 ret = fdt_get_resource(ofnode_to_fdt(node),
1383 ofnode_to_offset(node),
Simon Glassf7bfcc42017-07-25 08:29:55 -06001384 "reg", index, &fres);
1385 if (ret < 0)
1386 return -EINVAL;
1387 memset(res, '\0', sizeof(*res));
1388 res->start = fres.start;
1389 res->end = fres.end;
1390
1391 return 0;
1392 }
1393}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +09001394
1395int ofnode_read_resource_byname(ofnode node, const char *name,
1396 struct resource *res)
1397{
1398 int index;
1399
1400 index = ofnode_stringlist_search(node, "reg-names", name);
1401 if (index < 0)
1402 return index;
1403
1404 return ofnode_read_resource(node, index, res);
1405}
Mario Sixaefac062018-01-15 11:07:19 +01001406
1407u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
1408{
1409 if (ofnode_is_np(node))
1410 return of_translate_address(ofnode_to_np(node), in_addr);
1411 else
Simon Glass04fa09a2022-09-06 20:27:20 -06001412 return fdt_translate_address(ofnode_to_fdt(node),
1413 ofnode_to_offset(node), in_addr);
Mario Sixaefac062018-01-15 11:07:19 +01001414}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001415
Fabien Dessenne22236e02019-05-31 15:11:30 +02001416u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
1417{
1418 if (ofnode_is_np(node))
1419 return of_translate_dma_address(ofnode_to_np(node), in_addr);
1420 else
Simon Glass04fa09a2022-09-06 20:27:20 -06001421 return fdt_translate_dma_address(ofnode_to_fdt(node),
1422 ofnode_to_offset(node), in_addr);
Fabien Dessenne22236e02019-05-31 15:11:30 +02001423}
1424
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001425int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size)
1426{
1427 if (ofnode_is_np(node))
1428 return of_get_dma_range(ofnode_to_np(node), cpu, bus, size);
1429 else
Simon Glass04fa09a2022-09-06 20:27:20 -06001430 return fdt_get_dma_range(ofnode_to_fdt(node),
1431 ofnode_to_offset(node),
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001432 cpu, bus, size);
1433}
1434
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001435int ofnode_device_is_compatible(ofnode node, const char *compat)
1436{
1437 if (ofnode_is_np(node))
1438 return of_device_is_compatible(ofnode_to_np(node), compat,
1439 NULL, NULL);
1440 else
Simon Glass04fa09a2022-09-06 20:27:20 -06001441 return !fdt_node_check_compatible(ofnode_to_fdt(node),
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001442 ofnode_to_offset(node),
1443 compat);
1444}
Simon Glass954eeae2018-06-11 13:07:13 -06001445
1446ofnode ofnode_by_compatible(ofnode from, const char *compat)
1447{
1448 if (of_live_active()) {
1449 return np_to_ofnode(of_find_compatible_node(
1450 (struct device_node *)ofnode_to_np(from), NULL,
1451 compat));
1452 } else {
Simon Glass37dcd912022-09-06 20:27:23 -06001453 return noffset_to_ofnode(from,
1454 fdt_node_offset_by_compatible(ofnode_to_fdt(from),
Simon Glass04fa09a2022-09-06 20:27:20 -06001455 ofnode_to_offset(from), compat));
Simon Glass954eeae2018-06-11 13:07:13 -06001456 }
1457}
Jens Wiklander7b68dad2018-08-20 11:09:58 +02001458
1459ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1460 const void *propval, int proplen)
1461{
1462 if (of_live_active()) {
1463 return np_to_ofnode(of_find_node_by_prop_value(
1464 (struct device_node *)ofnode_to_np(from), propname,
1465 propval, proplen));
1466 } else {
Simon Glass37dcd912022-09-06 20:27:23 -06001467 return noffset_to_ofnode(from,
1468 fdt_node_offset_by_prop_value(ofnode_to_fdt(from),
1469 ofnode_to_offset(from), propname, propval,
1470 proplen));
Jens Wiklander7b68dad2018-08-20 11:09:58 +02001471 }
1472}
Mario Six047dafc2018-06-26 08:46:48 +02001473
Simon Glass5e2cd5e2022-07-30 15:52:10 -06001474int ofnode_write_prop(ofnode node, const char *propname, const void *value,
Simon Glass17abed02022-09-06 20:27:32 -06001475 int len, bool copy)
Mario Six047dafc2018-06-26 08:46:48 +02001476{
Simon Glass17abed02022-09-06 20:27:32 -06001477 if (of_live_active()) {
1478 void *newval;
1479 int ret;
1480
1481 if (copy) {
1482 newval = malloc(len);
1483 if (!newval)
1484 return log_ret(-ENOMEM);
1485 memcpy(newval, value, len);
1486 value = newval;
1487 }
1488 ret = of_write_prop(ofnode_to_np(node), propname, len, value);
1489 if (ret && copy)
1490 free(newval);
1491 return ret;
1492 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -06001493 return fdt_setprop(ofnode_to_fdt(node), ofnode_to_offset(node),
Simon Glass3ee3d152022-07-30 15:52:13 -06001494 propname, value, len);
Simon Glass17abed02022-09-06 20:27:32 -06001495 }
Mario Six047dafc2018-06-26 08:46:48 +02001496}
1497
1498int ofnode_write_string(ofnode node, const char *propname, const char *value)
1499{
Mario Six047dafc2018-06-26 08:46:48 +02001500 assert(ofnode_valid(node));
1501
1502 debug("%s: %s = %s", __func__, propname, value);
1503
Simon Glass17abed02022-09-06 20:27:32 -06001504 return ofnode_write_prop(node, propname, value, strlen(value) + 1,
1505 false);
Mario Six047dafc2018-06-26 08:46:48 +02001506}
1507
Simon Glassd28e31e2022-07-30 15:52:14 -06001508int ofnode_write_u32(ofnode node, const char *propname, u32 value)
1509{
1510 fdt32_t *val;
1511
1512 assert(ofnode_valid(node));
1513
1514 log_debug("%s = %x", propname, value);
1515 val = malloc(sizeof(*val));
1516 if (!val)
1517 return -ENOMEM;
1518 *val = cpu_to_fdt32(value);
1519
Simon Glass17abed02022-09-06 20:27:32 -06001520 return ofnode_write_prop(node, propname, val, sizeof(value), false);
Simon Glassd28e31e2022-07-30 15:52:14 -06001521}
1522
Mario Six047dafc2018-06-26 08:46:48 +02001523int ofnode_set_enabled(ofnode node, bool value)
1524{
Mario Six047dafc2018-06-26 08:46:48 +02001525 assert(ofnode_valid(node));
1526
1527 if (value)
1528 return ofnode_write_string(node, "status", "okay");
1529 else
Bin Menga9274aa2019-07-05 09:23:17 -07001530 return ofnode_write_string(node, "status", "disabled");
Mario Six047dafc2018-06-26 08:46:48 +02001531}
Simon Glass0034d962021-08-07 07:24:01 -06001532
1533bool ofnode_conf_read_bool(const char *prop_name)
1534{
1535 ofnode node;
1536
1537 node = ofnode_path("/config");
1538 if (!ofnode_valid(node))
1539 return false;
1540
1541 return ofnode_read_bool(node, prop_name);
1542}
1543
1544int ofnode_conf_read_int(const char *prop_name, int default_val)
1545{
1546 ofnode node;
1547
1548 node = ofnode_path("/config");
1549 if (!ofnode_valid(node))
1550 return default_val;
1551
1552 return ofnode_read_u32_default(node, prop_name, default_val);
1553}
1554
1555const char *ofnode_conf_read_str(const char *prop_name)
1556{
1557 ofnode node;
1558
1559 node = ofnode_path("/config");
1560 if (!ofnode_valid(node))
1561 return NULL;
1562
1563 return ofnode_read_string(node, prop_name);
1564}
Marek Behúnf4f1ddc2022-04-07 00:32:57 +02001565
1566ofnode ofnode_get_phy_node(ofnode node)
1567{
1568 /* DT node properties that reference a PHY node */
1569 static const char * const phy_handle_str[] = {
1570 "phy-handle", "phy", "phy-device",
1571 };
1572 struct ofnode_phandle_args args = {
1573 .node = ofnode_null()
1574 };
1575 int i;
1576
1577 assert(ofnode_valid(node));
1578
1579 for (i = 0; i < ARRAY_SIZE(phy_handle_str); i++)
1580 if (!ofnode_parse_phandle_with_args(node, phy_handle_str[i],
1581 NULL, 0, 0, &args))
1582 break;
1583
1584 return args.node;
1585}
Marek Behúnbc194772022-04-07 00:33:01 +02001586
1587phy_interface_t ofnode_read_phy_mode(ofnode node)
1588{
1589 const char *mode;
1590 int i;
1591
1592 assert(ofnode_valid(node));
1593
1594 mode = ofnode_read_string(node, "phy-mode");
1595 if (!mode)
1596 mode = ofnode_read_string(node, "phy-connection-type");
1597
1598 if (!mode)
Marek Behún48631e42022-04-07 00:33:03 +02001599 return PHY_INTERFACE_MODE_NA;
Marek Behúnbc194772022-04-07 00:33:01 +02001600
Marek Behún21a18362022-04-07 00:33:02 +02001601 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
Marek Behúnbc194772022-04-07 00:33:01 +02001602 if (!strcmp(mode, phy_interface_strings[i]))
1603 return i;
1604
1605 debug("%s: Invalid PHY interface '%s'\n", __func__, mode);
1606
Marek Behún48631e42022-04-07 00:33:03 +02001607 return PHY_INTERFACE_MODE_NA;
Marek Behúnbc194772022-04-07 00:33:01 +02001608}
Simon Glass56bc3322022-09-06 20:27:02 -06001609
1610int ofnode_add_subnode(ofnode node, const char *name, ofnode *subnodep)
1611{
1612 ofnode subnode;
1613 int ret = 0;
1614
1615 assert(ofnode_valid(node));
1616
1617 if (ofnode_is_np(node)) {
1618 struct device_node *np, *child;
1619
1620 np = (struct device_node *)ofnode_to_np(node);
1621 ret = of_add_subnode(np, name, -1, &child);
1622 if (ret && ret != -EEXIST)
1623 return ret;
1624 subnode = np_to_ofnode(child);
1625 } else {
Simon Glass04fa09a2022-09-06 20:27:20 -06001626 void *fdt = ofnode_to_fdt(node);
Simon Glass56bc3322022-09-06 20:27:02 -06001627 int poffset = ofnode_to_offset(node);
1628 int offset;
1629
1630 offset = fdt_add_subnode(fdt, poffset, name);
1631 if (offset == -FDT_ERR_EXISTS) {
1632 offset = fdt_subnode_offset(fdt, poffset, name);
1633 ret = -EEXIST;
1634 }
1635 if (offset < 0)
1636 return -EINVAL;
Simon Glass37dcd912022-09-06 20:27:23 -06001637 subnode = noffset_to_ofnode(node, offset);
Simon Glass56bc3322022-09-06 20:27:02 -06001638 }
1639
1640 *subnodep = subnode;
1641
1642 return ret; /* 0 or -EEXIST */
1643}
Simon Glass7a7229a2022-09-06 20:27:33 -06001644
1645int ofnode_copy_props(ofnode src, ofnode dst)
1646{
1647 struct ofprop prop;
1648
1649 ofnode_for_each_prop(prop, src) {
1650 const char *name;
1651 const char *val;
1652 int len, ret;
1653
1654 val = ofprop_get_property(&prop, &name, &len);
1655 if (!val) {
1656 log_debug("Cannot read prop (err=%d)\n", len);
1657 return log_msg_ret("get", -EINVAL);
1658 }
1659 ret = ofnode_write_prop(dst, name, val, len, true);
1660 if (ret) {
1661 log_debug("Cannot write prop (err=%d)\n", ret);
1662 return log_msg_ret("wr", -EINVAL);
1663 }
1664 }
1665
1666 return 0;
1667}