blob: c38596acbd057c04f7ab4bcbc30de05cb0d5fc3a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass4984de22017-05-17 17:18:10 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass4984de22017-05-17 17:18:10 -06005 */
6
7#ifndef _DM_OFNODE_H
8#define _DM_OFNODE_H
9
Simon Glass9e512042017-05-18 20:08:58 -060010/* TODO(sjg@chromium.org): Drop fdtdec.h include */
11#include <fdtdec.h>
12#include <dm/of.h>
Simon Glassec1add12020-12-16 17:25:06 -070013#include <dm/of_access.h>
Stefan Roese45dbe752020-09-23 08:23:27 +020014#include <log.h>
Marek Behún123ca112022-04-07 00:33:01 +020015#include <phy_interface.h>
Simon Glass9e512042017-05-18 20:08:58 -060016
17/* Enable checks to protect against invalid calls */
18#undef OF_CHECKS
19
Simon Glassdcf98852017-07-25 08:29:55 -060020struct resource;
21
Simon Glass5063ced2022-07-30 15:52:06 -060022#include <dm/ofnode_decl.h>
Michal Simekdb5e3492023-08-31 08:59:05 +020023#include <linux/errno.h>
Simon Glass4984de22017-05-17 17:18:10 -060024
Simon Glass9e512042017-05-18 20:08:58 -060025struct ofnode_phandle_args {
26 ofnode node;
27 int args_count;
28 uint32_t args[OF_MAX_PHANDLE_ARGS];
29};
30
Simon Glass92291652022-09-06 20:27:26 -060031#if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE)
Simon Glass9e512042017-05-18 20:08:58 -060032/**
Simon Glassee88ba72022-09-06 20:27:19 -060033 * oftree_reset() - reset the state of the oftree list
34 *
35 * Reset the oftree list so it can be started again. This should be called
36 * once the control FDT is in place, but before the ofnode interface is used.
37 */
Simon Glass92291652022-09-06 20:27:26 -060038void oftree_reset(void);
Simon Glassee88ba72022-09-06 20:27:19 -060039
40/**
Simon Glassa3f50d02022-09-06 20:27:20 -060041 * ofnode_to_fdt() - convert an ofnode to a flat DT pointer
42 *
43 * This cannot be called if the reference contains a node pointer.
44 *
45 * @node: Reference containing offset (possibly invalid)
46 * Return: DT offset (can be NULL)
47 */
Simon Glass92291652022-09-06 20:27:26 -060048__attribute_const__ void *ofnode_to_fdt(ofnode node);
Simon Glassa3f50d02022-09-06 20:27:20 -060049
50/**
Simon Glass2187cb72022-09-06 20:27:23 -060051 * ofnode_to_offset() - convert an ofnode to a flat DT offset
52 *
53 * This cannot be called if the reference contains a node pointer.
54 *
55 * @node: Reference containing offset (possibly invalid)
56 * Return: DT offset (can be -1)
57 */
Simon Glass92291652022-09-06 20:27:26 -060058__attribute_const__ int ofnode_to_offset(ofnode node);
59
60/**
61 * oftree_from_fdt() - Returns an oftree from a flat device tree pointer
62 *
Simon Glasse7a18f72022-10-11 09:47:19 -060063 * If @fdt is not already registered in the list of current device trees, it is
64 * added to the list.
65 *
Simon Glass92291652022-09-06 20:27:26 -060066 * @fdt: Device tree to use
67 *
68 * Returns: reference to the given node
69 */
70oftree oftree_from_fdt(void *fdt);
71
72/**
73 * noffset_to_ofnode() - convert a DT offset to an ofnode
74 *
75 * @other_node: Node in the same tree to use as a reference
76 * @of_offset: DT offset (either valid, or -1)
77 * Return: reference to the associated DT offset
78 */
79ofnode noffset_to_ofnode(ofnode other_node, int of_offset);
80
81#else /* !OFNODE_MULTI_TREE */
82static inline void oftree_reset(void) {}
83
84static inline void *ofnode_to_fdt(ofnode node)
85{
86#ifdef OF_CHECKS
87 if (of_live_active())
88 return NULL;
89#endif
90 /* Use the control FDT by default */
91 return (void *)gd->fdt_blob;
92}
93
94static inline __attribute_const__ int ofnode_to_offset(ofnode node)
Simon Glass2187cb72022-09-06 20:27:23 -060095{
96#ifdef OF_CHECKS
97 if (of_live_active())
98 return -1;
99#endif
100 return node.of_offset;
101}
102
Simon Glass92291652022-09-06 20:27:26 -0600103static inline oftree oftree_from_fdt(void *fdt)
104{
105 oftree tree;
106
107 /* we cannot access other trees without OFNODE_MULTI_TREE */
108 if (fdt == gd->fdt_blob)
109 tree.fdt = fdt;
110 else
111 tree.fdt = NULL;
112
113 return tree;
114}
115
116static inline ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
117{
118 ofnode node;
119
120 if (of_live_active())
121 node.np = NULL;
122 else
123 node.of_offset = of_offset;
124
125 return node;
126}
127
128#endif /* OFNODE_MULTI_TREE */
129
Simon Glass2187cb72022-09-06 20:27:23 -0600130/**
Stefan Roese45dbe752020-09-23 08:23:27 +0200131 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glass9e512042017-05-18 20:08:58 -0600132 *
133 * This cannot be called if the reference contains an offset.
134 *
135 * @node: Reference containing struct device_node * (possibly invalid)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100136 * Return: pointer to device node (can be NULL)
Simon Glass9e512042017-05-18 20:08:58 -0600137 */
Simon Glass98306982022-09-06 20:27:04 -0600138static inline struct device_node *ofnode_to_np(ofnode node)
Simon Glass9e512042017-05-18 20:08:58 -0600139{
140#ifdef OF_CHECKS
141 if (!of_live_active())
142 return NULL;
143#endif
144 return node.np;
145}
146
Simon Glass4984de22017-05-17 17:18:10 -0600147/**
Simon Glass4984de22017-05-17 17:18:10 -0600148 * ofnode_valid() - check if an ofnode is valid
149 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100150 * @node: Reference containing offset (possibly invalid)
Simon Glass92291652022-09-06 20:27:26 -0600151 * Return: true if the reference contains a valid ofnode, false if not
Simon Glass4984de22017-05-17 17:18:10 -0600152 */
153static inline bool ofnode_valid(ofnode node)
154{
Simon Glass9e512042017-05-18 20:08:58 -0600155 if (of_live_active())
156 return node.np != NULL;
157 else
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200158 return node.of_offset >= 0;
Simon Glass4984de22017-05-17 17:18:10 -0600159}
160
161/**
Simon Glass928d2672022-09-06 20:27:22 -0600162 * oftree_lookup_fdt() - obtain the FDT pointer from an oftree
163 *
164 * This can only be called when flat tree is enabled
165 *
166 * @tree: Tree to look at
167 * @return FDT pointer from the tree
168 */
169static inline void *oftree_lookup_fdt(oftree tree)
170{
171 if (of_live_active())
172 return NULL;
173 else
174 return tree.fdt;
175}
176
177/**
Simon Glass4984de22017-05-17 17:18:10 -0600178 * offset_to_ofnode() - convert a DT offset to an ofnode
179 *
180 * @of_offset: DT offset (either valid, or -1)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100181 * Return: reference to the associated DT offset
Simon Glass4984de22017-05-17 17:18:10 -0600182 */
183static inline ofnode offset_to_ofnode(int of_offset)
184{
185 ofnode node;
186
Simon Glass9e512042017-05-18 20:08:58 -0600187 if (of_live_active())
188 node.np = NULL;
189 else
Simon Glassb14c5332019-12-06 21:41:36 -0700190 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass4984de22017-05-17 17:18:10 -0600191
192 return node;
193}
194
195/**
Simon Glass9e512042017-05-18 20:08:58 -0600196 * np_to_ofnode() - convert a node pointer to an ofnode
197 *
198 * @np: Live node pointer (can be NULL)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100199 * Return: reference to the associated node pointer
Simon Glass9e512042017-05-18 20:08:58 -0600200 */
Simon Glass98306982022-09-06 20:27:04 -0600201static inline ofnode np_to_ofnode(struct device_node *np)
Simon Glass9e512042017-05-18 20:08:58 -0600202{
203 ofnode node;
204
205 node.np = np;
206
207 return node;
208}
209
210/**
211 * ofnode_is_np() - check if a reference is a node pointer
212 *
213 * This function associated that if there is a valid live tree then all
214 * references will use it. This is because using the flat DT when the live tree
215 * is valid is not permitted.
216 *
217 * @node: reference to check (possibly invalid)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100218 * Return: true if the reference is a live node pointer, false if it is a DT
Simon Glass9e512042017-05-18 20:08:58 -0600219 * offset
220 */
221static inline bool ofnode_is_np(ofnode node)
222{
223#ifdef OF_CHECKS
224 /*
225 * Check our assumption that flat tree offsets are not used when a
226 * live tree is in use.
227 */
228 assert(!ofnode_valid(node) ||
Stefan Roese45dbe752020-09-23 08:23:27 +0200229 (of_live_active() ? ofnode_to_np(node)
230 : ofnode_to_np(node)));
Simon Glass9e512042017-05-18 20:08:58 -0600231#endif
232 return of_live_active() && ofnode_valid(node);
233}
234
235/**
Simon Glass4984de22017-05-17 17:18:10 -0600236 * ofnode_equal() - check if two references are equal
237 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100238 * @ref1: first reference to check (possibly invalid)
239 * @ref2: second reference to check (possibly invalid)
240 * Return: true if equal, else false
Simon Glass4984de22017-05-17 17:18:10 -0600241 */
242static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
243{
244 /* We only need to compare the contents */
245 return ref1.of_offset == ref2.of_offset;
246}
247
Simon Glass9e512042017-05-18 20:08:58 -0600248/**
Simon Glass085d5942022-09-06 20:27:21 -0600249 * oftree_valid() - check if an oftree is valid
250 *
251 * @tree: Reference containing oftree
252 * Return: true if the reference contains a valid oftree, false if node
253 */
254static inline bool oftree_valid(oftree tree)
255{
256 if (of_live_active())
257 return tree.np;
258 else
259 return tree.fdt;
260}
261
262/**
263 * oftree_null() - Obtain a null oftree
264 *
265 * This returns an oftree which points to no tree. It works both with the flat
266 * tree and livetree.
267 */
268static inline oftree oftree_null(void)
269{
270 oftree tree;
271
272 if (of_live_active())
273 tree.np = NULL;
274 else
275 tree.fdt = NULL;
276
277 return tree;
278}
279
280/**
Simon Glass9e512042017-05-18 20:08:58 -0600281 * ofnode_null() - Obtain a null ofnode
282 *
283 * This returns an ofnode which points to no node. It works both with the flat
284 * tree and livetree.
285 */
286static inline ofnode ofnode_null(void)
287{
288 ofnode node;
289
290 if (of_live_active())
291 node.np = NULL;
292 else
293 node.of_offset = -1;
294
295 return node;
296}
297
Simon Glassd0c20ce2020-11-28 17:50:07 -0700298static inline ofnode ofnode_root(void)
299{
300 ofnode node;
301
302 if (of_live_active())
303 node.np = gd_of_root();
304 else
305 node.of_offset = 0;
306
307 return node;
308}
309
Simon Glass9e512042017-05-18 20:08:58 -0600310/**
Simon Glass52ad21a2022-09-06 20:27:16 -0600311 * ofprop_valid() - check if an ofprop is valid
312 *
313 * @prop: Pointer to ofprop to check
314 * Return: true if the reference contains a valid ofprop, false if not
315 */
316static inline bool ofprop_valid(struct ofprop *prop)
317{
318 if (of_live_active())
319 return prop->prop;
320 else
321 return prop->offset >= 0;
322}
323
324/**
Simon Glass33104842022-07-30 15:52:08 -0600325 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
326 *
327 * Returns: reference to the control FDT
328 */
329static inline oftree oftree_default(void)
330{
331 oftree tree;
332
333 if (of_live_active())
334 tree.np = gd_of_root();
335 else
336 tree.fdt = (void *)gd->fdt_blob;
337
338 return tree;
339}
340
341/**
Simon Glass085d5942022-09-06 20:27:21 -0600342 * oftree_from_np() - Returns an oftree from a node pointer
343 *
344 * @root: Root node of the tree
345 * Returns: reference to the given node
346 */
347static inline oftree oftree_from_np(struct device_node *root)
348{
349 oftree tree;
350
351 tree.np = root;
352
353 return tree;
354}
355
356/**
Simon Glassa8f2ac22023-06-01 10:22:42 -0600357 * oftree_dispose() - Dispose of an oftree
358 *
359 * This can be used to dispose of a tree that has been created (other than
360 * the control FDT which must not be disposed)
361 *
362 * @tree: Tree to dispose
363 */
364void oftree_dispose(oftree tree);
365
366/**
Kishon Vijay Abraham I77cbaf82021-07-21 21:28:30 +0530367 * ofnode_name_eq() - Check if the node name is equivalent to a given name
368 * ignoring the unit address
369 *
370 * @node: valid node reference that has to be compared
371 * @name: name that has to be compared with the node name
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100372 * Return: true if matches, false if it doesn't match
Kishon Vijay Abraham I77cbaf82021-07-21 21:28:30 +0530373 */
374bool ofnode_name_eq(ofnode node, const char *name);
375
376/**
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +0200377 * ofnode_read_u8() - Read a 8-bit integer from a property
378 *
379 * @node: valid node reference to read property from
380 * @propname: name of the property to read from
381 * @outp: place to put value (if found)
382 * Return: 0 if OK, -ve on error
383 */
384int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
385
386/**
387 * ofnode_read_u8_default() - Read a 8-bit integer from a property
388 *
389 * @node: valid node reference to read property from
390 * @propname: name of the property to read from
391 * @def: default value to return if the property has no value
392 * Return: property value, or @def if not found
393 */
394u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
395
396/**
397 * ofnode_read_u16() - Read a 16-bit integer from a property
398 *
399 * @node: valid node reference to read property from
400 * @propname: name of the property to read from
401 * @outp: place to put value (if found)
402 * Return: 0 if OK, -ve on error
403 */
404int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
405
406/**
407 * ofnode_read_u16_default() - Read a 16-bit integer from a property
408 *
409 * @node: valid node reference to read property from
410 * @propname: name of the property to read from
411 * @def: default value to return if the property has no value
412 * Return: property value, or @def if not found
413 */
414u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
415
416/**
Simon Glass9e512042017-05-18 20:08:58 -0600417 * ofnode_read_u32() - Read a 32-bit integer from a property
418 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100419 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600420 * @propname: name of the property to read from
421 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100422 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600423 */
424int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
425
426/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200427 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
428 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100429 * @node: valid node reference to read property from
Dario Binacchi4bb70752020-03-29 18:04:41 +0200430 * @propname: name of the property to read from
431 * @index: index of the integer to return
432 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100433 * Return: 0 if OK, -ve on error
Dario Binacchi4bb70752020-03-29 18:04:41 +0200434 */
435int ofnode_read_u32_index(ofnode node, const char *propname, int index,
436 u32 *outp);
437
438/**
Michal Simekfa12dfa2023-08-25 11:37:46 +0200439 * ofnode_read_u64_index() - Read a 64-bit integer from a multi-value property
440 *
441 * @node: valid node reference to read property from
442 * @propname: name of the property to read from
443 * @index: index of the integer to return
444 * @outp: place to put value (if found)
445 * Return: 0 if OK, -ve on error
446 */
447int ofnode_read_u64_index(ofnode node, const char *propname, int index,
448 u64 *outp);
449
450/**
Simon Glass9e512042017-05-18 20:08:58 -0600451 * ofnode_read_s32() - Read a 32-bit integer from a property
452 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100453 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600454 * @propname: name of the property to read from
455 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100456 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600457 */
458static inline int ofnode_read_s32(ofnode node, const char *propname,
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100459 s32 *outp)
Simon Glass9e512042017-05-18 20:08:58 -0600460{
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100461 return ofnode_read_u32(node, propname, (u32 *)outp);
Simon Glass9e512042017-05-18 20:08:58 -0600462}
463
464/**
465 * ofnode_read_u32_default() - Read a 32-bit integer from a property
466 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100467 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600468 * @propname: name of the property to read from
469 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100470 * Return: property value, or @def if not found
Simon Glass9e512042017-05-18 20:08:58 -0600471 */
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100472u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
Simon Glass9e512042017-05-18 20:08:58 -0600473
474/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200475 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
476 * property
477 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100478 * @node: valid node reference to read property from
Dario Binacchi4bb70752020-03-29 18:04:41 +0200479 * @propname: name of the property to read from
480 * @index: index of the integer to return
481 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100482 * Return: property value, or @def if not found
Dario Binacchi4bb70752020-03-29 18:04:41 +0200483 */
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100484u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
Dario Binacchi4bb70752020-03-29 18:04:41 +0200485 u32 def);
486
487/**
Simon Glass9e512042017-05-18 20:08:58 -0600488 * ofnode_read_s32_default() - Read a 32-bit integer from a property
489 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100490 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600491 * @propname: name of the property to read from
492 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100493 * Return: property value, or @def if not found
Simon Glass9e512042017-05-18 20:08:58 -0600494 */
495int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
496
497/**
Lukas Auerafb30122018-11-22 11:26:35 +0100498 * ofnode_read_u64() - Read a 64-bit integer from a property
499 *
500 * @node: valid node reference to read property from
501 * @propname: name of the property to read from
502 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100503 * Return: 0 if OK, -ve on error
Lukas Auerafb30122018-11-22 11:26:35 +0100504 */
505int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
506
507/**
Simon Glass7e5196c2018-06-11 13:07:10 -0600508 * ofnode_read_u64_default() - Read a 64-bit integer from a property
509 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100510 * @node: valid node reference to read property from
Simon Glass7e5196c2018-06-11 13:07:10 -0600511 * @propname: name of the property to read from
512 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100513 * Return: property value, or @def if not found
Simon Glass7e5196c2018-06-11 13:07:10 -0600514 */
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200515u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass7e5196c2018-06-11 13:07:10 -0600516
517/**
Simon Glassa8167d82020-01-27 08:49:44 -0700518 * ofnode_read_prop() - Read a property from a node
519 *
520 * @node: valid node reference to read property from
521 * @propname: name of the property to read
522 * @sizep: if non-NULL, returns the size of the property, or an error code
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100523 * if not found
524 * Return: property value, or NULL if there is no such property
Simon Glassa8167d82020-01-27 08:49:44 -0700525 */
526const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
527
528/**
Simon Glass9e512042017-05-18 20:08:58 -0600529 * ofnode_read_string() - Read a string from a property
530 *
Simon Glassa8167d82020-01-27 08:49:44 -0700531 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600532 * @propname: name of the property to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100533 * Return: string from property value, or NULL if there is no such property
Simon Glass9e512042017-05-18 20:08:58 -0600534 */
535const char *ofnode_read_string(ofnode node, const char *propname);
536
537/**
Simon Glassbed77492017-05-18 20:09:01 -0600538 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glass9e512042017-05-18 20:08:58 -0600539 *
540 * @node: valid node reference to read property from
541 * @propname: name of the property to read
542 * @out_values: pointer to return value, modified only if return value is 0
543 * @sz: number of array elements to read
Simon Glass66d0d0c2022-09-06 20:27:18 -0600544 * Return: 0 on success, -EINVAL if the property does not exist,
545 * -ENODATA if property does not have a value, and -EOVERFLOW if the
546 * property data isn't large enough
Simon Glass9e512042017-05-18 20:08:58 -0600547 *
548 * Search for a property in a device node and read 32-bit value(s) from
Simon Glass66d0d0c2022-09-06 20:27:18 -0600549 * it.
Simon Glass9e512042017-05-18 20:08:58 -0600550 *
551 * The out_values is modified only if a valid u32 value can be decoded.
552 */
553int ofnode_read_u32_array(ofnode node, const char *propname,
554 u32 *out_values, size_t sz);
555
556/**
557 * ofnode_read_bool() - read a boolean value from a property
558 *
559 * @node: valid node reference to read property from
560 * @propname: name of property to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100561 * Return: true if property is present (meaning true), false if not present
Simon Glass9e512042017-05-18 20:08:58 -0600562 */
563bool ofnode_read_bool(ofnode node, const char *propname);
564
565/**
566 * ofnode_find_subnode() - find a named subnode of a parent node
567 *
568 * @node: valid reference to parent node
569 * @subnode_name: name of subnode to find
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100570 * Return: reference to subnode (which can be invalid if there is no such
Simon Glass9e512042017-05-18 20:08:58 -0600571 * subnode)
572 */
573ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
574
Simon Glassec1add12020-12-16 17:25:06 -0700575#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass401d1c42020-10-30 21:38:53 -0600576#include <asm/global_data.h>
577
Simon Glassec1add12020-12-16 17:25:06 -0700578static inline bool ofnode_is_enabled(ofnode node)
579{
580 if (ofnode_is_np(node)) {
581 return of_device_is_available(ofnode_to_np(node));
582 } else {
583 return fdtdec_get_is_enabled(gd->fdt_blob,
584 ofnode_to_offset(node));
585 }
586}
587
588static inline ofnode ofnode_first_subnode(ofnode node)
589{
590 assert(ofnode_valid(node));
591 if (ofnode_is_np(node))
592 return np_to_ofnode(node.np->child);
593
594 return offset_to_ofnode(
595 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
596}
597
598static inline ofnode ofnode_next_subnode(ofnode node)
599{
600 assert(ofnode_valid(node));
601 if (ofnode_is_np(node))
602 return np_to_ofnode(node.np->sibling);
603
604 return offset_to_ofnode(
605 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
606}
607#else
608/**
609 * ofnode_is_enabled() - Checks whether a node is enabled.
610 * This looks for a 'status' property. If this exists, then returns true if
611 * the status is 'okay' and false otherwise. If there is no status property,
612 * it returns true on the assumption that anything mentioned should be enabled
613 * by default.
614 *
615 * @node: node to examine
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100616 * Return: false (not enabled) or true (enabled)
Simon Glassec1add12020-12-16 17:25:06 -0700617 */
618bool ofnode_is_enabled(ofnode node);
619
Simon Glass9e512042017-05-18 20:08:58 -0600620/**
621 * ofnode_first_subnode() - find the first subnode of a parent node
622 *
623 * @node: valid reference to a valid parent node
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100624 * Return: reference to the first subnode (which can be invalid if the parent
Simon Glass9e512042017-05-18 20:08:58 -0600625 * node has no subnodes)
626 */
627ofnode ofnode_first_subnode(ofnode node);
628
629/**
630 * ofnode_next_subnode() - find the next sibling of a subnode
631 *
632 * @node: valid reference to previous node (sibling)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100633 * Return: reference to the next subnode (which can be invalid if the node
Simon Glass9e512042017-05-18 20:08:58 -0600634 * has no more siblings)
635 */
636ofnode ofnode_next_subnode(ofnode node);
Simon Glassec1add12020-12-16 17:25:06 -0700637#endif /* DM_INLINE_OFNODE */
Simon Glass9e512042017-05-18 20:08:58 -0600638
639/**
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100640 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
641 *
642 * @node: valid node to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100643 * Return: ofnode reference of the parent node
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100644 */
645ofnode ofnode_get_parent(ofnode node);
646
647/**
Simon Glass9e512042017-05-18 20:08:58 -0600648 * ofnode_get_name() - get the name of a node
649 *
650 * @node: valid node to look up
Simon Glassf46ec932022-09-06 20:27:15 -0600651 * Return: name of node (for the root node this is "")
Simon Glass9e512042017-05-18 20:08:58 -0600652 */
653const char *ofnode_get_name(ofnode node);
654
655/**
Marek Behún0e116be2021-05-26 14:08:18 +0200656 * ofnode_get_path() - get the full path of a node
657 *
658 * @node: valid node to look up
659 * @buf: buffer to write the node path into
660 * @buflen: buffer size
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100661 * Return: 0 if OK, -ve on error
Marek Behún0e116be2021-05-26 14:08:18 +0200662 */
663int ofnode_get_path(ofnode node, char *buf, int buflen);
664
665/**
Kever Yangb4f20762018-02-23 17:38:50 +0100666 * ofnode_get_by_phandle() - get ofnode from phandle
667 *
Simon Glass829d5122022-09-06 20:26:57 -0600668 * This uses the default (control) device tree
669 *
Kever Yangb4f20762018-02-23 17:38:50 +0100670 * @phandle: phandle to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100671 * Return: ofnode reference to the phandle
Kever Yangb4f20762018-02-23 17:38:50 +0100672 */
673ofnode ofnode_get_by_phandle(uint phandle);
674
675/**
Simon Glass928d2672022-09-06 20:27:22 -0600676 * oftree_get_by_phandle() - get ofnode from phandle
677 *
678 * @tree: tree to use
679 * @phandle: phandle to look up
680 * Return: ofnode reference to the phandle
681 */
682ofnode oftree_get_by_phandle(oftree tree, uint phandle);
683
684/**
Simon Glass9e512042017-05-18 20:08:58 -0600685 * ofnode_read_size() - read the size of a property
686 *
687 * @node: node to check
688 * @propname: property to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100689 * Return: size of property if present, or -EINVAL if not
Simon Glass9e512042017-05-18 20:08:58 -0600690 */
691int ofnode_read_size(ofnode node, const char *propname);
692
693/**
Keerthye679d032019-04-24 17:19:53 +0530694 * ofnode_get_addr_size_index() - get an address/size from a node
695 * based on index
696 *
697 * This reads the register address/size from a node based on index
698 *
699 * @node: node to read from
700 * @index: Index of address to read (0 for first)
701 * @size: Pointer to size of the address
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100702 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Keerthye679d032019-04-24 17:19:53 +0530703 */
Johan Jonkeraecae812023-03-13 01:30:33 +0100704fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index,
705 fdt_size_t *size);
Keerthye679d032019-04-24 17:19:53 +0530706
707/**
Marek Behún31a7b712021-05-26 14:08:17 +0200708 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
709 * based on index, without address
710 * translation
711 *
712 * This reads the register address/size from a node based on index.
713 * The resulting address is not translated. Useful for example for on-disk
714 * addresses.
715 *
716 * @node: node to read from
717 * @index: Index of address to read (0 for first)
718 * @size: Pointer to size of the address
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100719 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Marek Behún31a7b712021-05-26 14:08:17 +0200720 */
Johan Jonkeraecae812023-03-13 01:30:33 +0100721fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
722 fdt_size_t *size);
Marek Behún31a7b712021-05-26 14:08:17 +0200723
724/**
Simon Glassbed77492017-05-18 20:09:01 -0600725 * ofnode_get_addr_index() - get an address from a node
726 *
727 * This reads the register address from a node
728 *
729 * @node: node to read from
730 * @index: Index of address to read (0 for first)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100731 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glassbed77492017-05-18 20:09:01 -0600732 */
Johan Jonkeraecae812023-03-13 01:30:33 +0100733fdt_addr_t ofnode_get_addr_index(ofnode node, int index);
Simon Glassbed77492017-05-18 20:09:01 -0600734
735/**
736 * ofnode_get_addr() - get an address from a node
737 *
738 * This reads the register address from a node
739 *
740 * @node: node to read from
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100741 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glassbed77492017-05-18 20:09:01 -0600742 */
Johan Jonkeraecae812023-03-13 01:30:33 +0100743fdt_addr_t ofnode_get_addr(ofnode node);
Simon Glassbed77492017-05-18 20:09:01 -0600744
745/**
Chen Guanqiaoaa351a12021-04-12 14:51:11 +0800746 * ofnode_get_size() - get size from a node
747 *
748 * This reads the register size from a node
749 *
750 * @node: node to read from
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100751 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
Chen Guanqiaoaa351a12021-04-12 14:51:11 +0800752 */
753fdt_size_t ofnode_get_size(ofnode node);
754
755/**
Simon Glass9e512042017-05-18 20:08:58 -0600756 * ofnode_stringlist_search() - find a string in a string list and return index
757 *
758 * Note that it is possible for this function to succeed on property values
759 * that are not NUL-terminated. That's because the function will stop after
760 * finding the first occurrence of @string. This can for example happen with
761 * small-valued cell properties, such as #address-cells, when searching for
762 * the empty string.
763 *
764 * @node: node to check
765 * @propname: name of the property containing the string list
766 * @string: string to look up in the string list
767 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100768 * Return:
Simon Glass9e512042017-05-18 20:08:58 -0600769 * the index of the string in the list of strings
770 * -ENODATA if the property is not found
771 * -EINVAL on some other error
772 */
773int ofnode_stringlist_search(ofnode node, const char *propname,
774 const char *string);
775
776/**
Simon Glass8c293d62017-06-12 06:21:28 -0600777 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glass9e512042017-05-18 20:08:58 -0600778 *
779 * Note that this will successfully extract strings from properties with
780 * non-NUL-terminated values. For example on small-valued cell properties
781 * this function will return the empty string.
782 *
783 * If non-NULL, the length of the string (on success) or a negative error-code
784 * (on failure) will be stored in the integer pointer to by lenp.
785 *
786 * @node: node to check
787 * @propname: name of the property containing the string list
Simon Glass32c6a8e2021-10-23 17:26:06 -0600788 * @index: index of the string to return (cannot be negative)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100789 * @outp: return location for the string
Simon Glass9e512042017-05-18 20:08:58 -0600790 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100791 * Return:
Simon Glass32c6a8e2021-10-23 17:26:06 -0600792 * 0 if found or -ve error value if not found
Simon Glass9e512042017-05-18 20:08:58 -0600793 */
794int ofnode_read_string_index(ofnode node, const char *propname, int index,
795 const char **outp);
796
797/**
Simon Glass8c293d62017-06-12 06:21:28 -0600798 * ofnode_read_string_count() - find the number of strings in a string list
799 *
800 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100801 * @property: name of the property containing the string list
802 * Return:
Simon Glass8c293d62017-06-12 06:21:28 -0600803 * number of strings in the list, or -ve error value if not found
804 */
805int ofnode_read_string_count(ofnode node, const char *property);
806
807/**
Simon Glass075bfc92021-10-23 17:26:07 -0600808 * ofnode_read_string_list() - read a list of strings
809 *
810 * This produces a list of string pointers with each one pointing to a string
811 * in the string list. If the property does not exist, it returns {NULL}.
812 *
813 * The data is allocated and the caller is reponsible for freeing the return
814 * value (the list of string pointers). The strings themselves may not be
815 * changed as they point directly into the devicetree property.
816 *
817 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100818 * @property: name of the property containing the string list
Simon Glass075bfc92021-10-23 17:26:07 -0600819 * @listp: returns an allocated, NULL-terminated list of strings if the return
820 * value is > 0, else is set to NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100821 * Return:
822 * number of strings in list, 0 if none, -ENOMEM if out of memory,
823 * -EINVAL if no such property, -EENODATA if property is empty
Simon Glass075bfc92021-10-23 17:26:07 -0600824 */
825int ofnode_read_string_list(ofnode node, const char *property,
826 const char ***listp);
827
828/**
Simon Glass9e512042017-05-18 20:08:58 -0600829 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
830 *
831 * This function is useful to parse lists of phandles and their arguments.
832 * Returns 0 on success and fills out_args, on error returns appropriate
833 * errno value.
834 *
835 * Caller is responsible to call of_node_put() on the returned out_args->np
836 * pointer.
837 *
838 * Example:
839 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100840 * .. code-block::
Simon Glass9e512042017-05-18 20:08:58 -0600841 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100842 * phandle1: node1 {
843 * #list-cells = <2>;
844 * };
845 * phandle2: node2 {
846 * #list-cells = <1>;
847 * };
848 * node3 {
849 * list = <&phandle1 1 2 &phandle2 3>;
850 * };
Simon Glass9e512042017-05-18 20:08:58 -0600851 *
852 * To get a device_node of the `node2' node you may call this:
853 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
854 *
855 * @node: device tree node containing a list
856 * @list_name: property name that contains a list
857 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100858 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glass9e512042017-05-18 20:08:58 -0600859 * @index: index of a phandle to parse out
860 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100861 * Return:
862 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
863 * @list_name does not exist, -EINVAL if a phandle was not found,
864 * @cells_name could not be found, the arguments were truncated or there
865 * were too many arguments.
Simon Glass9e512042017-05-18 20:08:58 -0600866 */
867int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
868 const char *cells_name, int cell_count,
869 int index,
870 struct ofnode_phandle_args *out_args);
871
872/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200873 * ofnode_count_phandle_with_args() - Count number of phandle in a list
874 *
875 * This function is useful to count phandles into a list.
876 * Returns number of phandle on success, on error returns appropriate
877 * errno value.
878 *
879 * @node: device tree node containing a list
880 * @list_name: property name that contains a list
881 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100882 * @cell_count: Cell count to use if @cells_name is NULL
883 * Return:
884 * number of phandle on success, -ENOENT if @list_name does not exist,
885 * -EINVAL if a phandle was not found, @cells_name could not be found.
Patrice Chotard642346a2017-07-18 11:57:08 +0200886 */
887int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200888 const char *cells_name, int cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +0200889
890/**
Simon Glass9e512042017-05-18 20:08:58 -0600891 * ofnode_path() - find a node by full path
892 *
Simon Glass33104842022-07-30 15:52:08 -0600893 * This uses the control FDT.
894 *
Simon Glass9e512042017-05-18 20:08:58 -0600895 * @path: Full path to node, e.g. "/bus/spi@1"
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100896 * Return: reference to the node found. Use ofnode_valid() to check if it exists
Simon Glass9e512042017-05-18 20:08:58 -0600897 */
898ofnode ofnode_path(const char *path);
899
900/**
Simon Glassb7bd94f2022-09-06 20:27:24 -0600901 * oftree_path() - find a node by full path from a root node
Simon Glass33104842022-07-30 15:52:08 -0600902 *
903 * @tree: Device tree to use
904 * @path: Full path to node, e.g. "/bus/spi@1"
905 * Return: reference to the node found. Use ofnode_valid() to check if it exists
906 */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600907ofnode oftree_path(oftree tree, const char *path);
908
909/**
910 * oftree_root() - get the root node of a tree
911 *
912 * @tree: Device tree to use
913 * Return: reference to the root node
914 */
915ofnode oftree_root(oftree tree);
Simon Glass33104842022-07-30 15:52:08 -0600916
917/**
Simon Glassbd933bf2020-01-27 08:49:46 -0700918 * ofnode_read_chosen_prop() - get the value of a chosen property
919 *
Simon Glassb7bd94f2022-09-06 20:27:24 -0600920 * This looks for a property within the /chosen node and returns its value.
921 *
922 * This only works with the control FDT.
Simon Glassbd933bf2020-01-27 08:49:46 -0700923 *
924 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100925 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Simon Glassbd933bf2020-01-27 08:49:46 -0700926 * returns NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100927 * Return: property value if found, else NULL
Simon Glassbd933bf2020-01-27 08:49:46 -0700928 */
929const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
930
931/**
Simon Glass14ca9f72020-01-27 08:49:43 -0700932 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glass9e512042017-05-18 20:08:58 -0600933 *
Simon Glass14ca9f72020-01-27 08:49:43 -0700934 * This looks for a property within the /chosen node and returns its value,
935 * checking that it is a valid nul-terminated string
Simon Glass9e512042017-05-18 20:08:58 -0600936 *
Simon Glass988f1462022-09-06 20:27:28 -0600937 * This only works with the control FDT.
938 *
Simon Glass9e512042017-05-18 20:08:58 -0600939 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100940 * Return: string value if found, else NULL
Simon Glass9e512042017-05-18 20:08:58 -0600941 */
Simon Glass14ca9f72020-01-27 08:49:43 -0700942const char *ofnode_read_chosen_string(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600943
944/**
Simon Glass74d594a2020-01-27 08:49:42 -0700945 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glass9e512042017-05-18 20:08:58 -0600946 *
Simon Glass74d594a2020-01-27 08:49:42 -0700947 * This looks up a named property in the chosen node and uses that as a path to
948 * look up a code.
949 *
Simon Glass988f1462022-09-06 20:27:28 -0600950 * This only works with the control FDT.
951 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100952 * @propname: Property name to look for
953 * Return: the referenced node if present, else ofnode_null()
Simon Glass9e512042017-05-18 20:08:58 -0600954 */
Simon Glass74d594a2020-01-27 08:49:42 -0700955ofnode ofnode_get_chosen_node(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600956
Michal Simek305d3182020-07-28 12:51:08 +0200957/**
958 * ofnode_read_aliases_prop() - get the value of a aliases property
959 *
960 * This looks for a property within the /aliases node and returns its value
961 *
Simon Glass988f1462022-09-06 20:27:28 -0600962 * This only works with the control FDT.
963 *
Michal Simek305d3182020-07-28 12:51:08 +0200964 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100965 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Michal Simek305d3182020-07-28 12:51:08 +0200966 * returns NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100967 * Return: property value if found, else NULL
Michal Simek305d3182020-07-28 12:51:08 +0200968 */
969const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
970
971/**
972 * ofnode_get_aliases_node() - get a referenced node from the aliases node
973 *
974 * This looks up a named property in the aliases node and uses that as a path to
975 * look up a code.
976 *
Simon Glass988f1462022-09-06 20:27:28 -0600977 * This only works with the control FDT.
978 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100979 * @propname: Property name to look for
980 * Return: the referenced node if present, else ofnode_null()
Michal Simek305d3182020-07-28 12:51:08 +0200981 */
982ofnode ofnode_get_aliases_node(const char *propname);
983
Simon Glass9e512042017-05-18 20:08:58 -0600984struct display_timing;
985/**
986 * ofnode_decode_display_timing() - decode display timings
987 *
988 * Decode display timings from the supplied 'display-timings' node.
989 * See doc/device-tree-bindings/video/display-timing.txt for binding
990 * information.
991 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100992 * @node: 'display-timing' node containing the timing subnodes
993 * @index: Index number to read (0=first timing subnode)
994 * @config: Place to put timings
995 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Simon Glass9e512042017-05-18 20:08:58 -0600996 */
997int ofnode_decode_display_timing(ofnode node, int index,
998 struct display_timing *config);
999
1000/**
Nikhil M Jain0347cc72023-01-31 15:35:14 +05301001 * ofnode_decode_panel_timing() - decode display timings
1002 *
1003 * Decode panel timings from the supplied 'panel-timings' node.
1004 *
1005 * @node: 'display-timing' node containing the timing subnodes
1006 * @config: Place to put timings
1007 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
1008 */
1009int ofnode_decode_panel_timing(ofnode node,
1010 struct display_timing *config);
1011
1012/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001013 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glass9e512042017-05-18 20:08:58 -06001014 *
1015 * @node: node to read
1016 * @propname: property to read
1017 * @lenp: place to put length on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001018 * Return: pointer to property, or NULL if not found
Simon Glass9e512042017-05-18 20:08:58 -06001019 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +09001020const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glass9e512042017-05-18 20:08:58 -06001021
1022/**
Simon Glass4b1f5712022-09-06 20:27:13 -06001023 * ofnode_first_property()- get the reference of the first property
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001024 *
1025 * Get reference to the first property of the node, it is used to iterate
Simon Glass92432242022-09-06 20:27:14 -06001026 * and read all the property with ofprop_get_property().
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001027 *
1028 * @node: node to read
1029 * @prop: place to put argument reference
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001030 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001031 */
Simon Glass4b1f5712022-09-06 20:27:13 -06001032int ofnode_first_property(ofnode node, struct ofprop *prop);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001033
1034/**
Simon Glass4b1f5712022-09-06 20:27:13 -06001035 * ofnode_next_property() - get the reference of the next property
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001036 *
1037 * Get reference to the next property of the node, it is used to iterate
Simon Glass92432242022-09-06 20:27:14 -06001038 * and read all the property with ofprop_get_property().
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001039 *
1040 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001041 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001042 */
Simon Glass4b1f5712022-09-06 20:27:13 -06001043int ofnode_next_property(struct ofprop *prop);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001044
1045/**
Simon Glass52ad21a2022-09-06 20:27:16 -06001046 * ofnode_for_each_prop() - iterate over all properties of a node
1047 *
1048 * @prop: struct ofprop
1049 * @node: node (lvalue, ofnode)
1050 *
1051 * This is a wrapper around a for loop and is used like this::
1052 *
1053 * ofnode node;
1054 * struct ofprop prop;
1055 *
1056 * ofnode_for_each_prop(prop, node) {
1057 * ...use prop...
1058 * }
1059 *
1060 * Note that this is implemented as a macro and @prop is used as
1061 * iterator in the loop. The parent variable can be a constant or even a
1062 * literal.
1063 */
1064#define ofnode_for_each_prop(prop, node) \
1065 for (ofnode_first_property(node, &prop); \
1066 ofprop_valid(&prop); \
1067 ofnode_next_property(&prop))
1068
1069/**
Simon Glass92432242022-09-06 20:27:14 -06001070 * ofprop_get_property() - get a pointer to the value of a property
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001071 *
1072 * Get value for the property identified by the provided reference.
1073 *
1074 * @prop: reference on property
1075 * @propname: If non-NULL, place to property name on success,
Simon Glass92432242022-09-06 20:27:14 -06001076 * @lenp: If non-NULL, place to put length on success, or error code on failure
1077 * Return: pointer to property, or NULL if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001078 */
Simon Glass92432242022-09-06 20:27:14 -06001079const void *ofprop_get_property(const struct ofprop *prop,
1080 const char **propname, int *lenp);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001081
1082/**
Simon Glass9e512042017-05-18 20:08:58 -06001083 * ofnode_get_addr_size() - get address and size from a property
1084 *
1085 * This does no address translation. It simply reads an property that contains
1086 * an address and a size value, one after the other.
1087 *
1088 * @node: node to read from
1089 * @propname: property to read
1090 * @sizep: place to put size value (on success)
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001091 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glass9e512042017-05-18 20:08:58 -06001092 */
Johan Jonkeraecae812023-03-13 01:30:33 +01001093fdt_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
1094 fdt_size_t *sizep);
Simon Glass9e512042017-05-18 20:08:58 -06001095
1096/**
1097 * ofnode_read_u8_array_ptr() - find an 8-bit array
1098 *
1099 * Look up a property in a node and return a pointer to its contents as a
1100 * byte array of given length. The property must have at least enough data
1101 * for the array (count bytes). It may have more, but this will be ignored.
1102 * The data is not copied.
1103 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001104 * @node: node to examine
1105 * @propname: name of property to find
1106 * @sz: number of array elements
1107 * Return:
1108 * pointer to byte array if found, or NULL if the property is not found or
1109 * there is not enough data
Simon Glass9e512042017-05-18 20:08:58 -06001110 */
1111const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
1112 size_t sz);
1113
1114/**
1115 * ofnode_read_pci_addr() - look up a PCI address
1116 *
1117 * Look at an address property in a node and return the PCI address which
1118 * corresponds to the given type in the form of fdt_pci_addr.
1119 * The property must hold one fdt_pci_addr with a lengh.
1120 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001121 * @node: node to examine
1122 * @type: pci address type (FDT_PCI_SPACE_xxx)
1123 * @propname: name of property to find
1124 * @addr: returns pci address in the form of fdt_pci_addr
1125 * Return:
1126 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
1127 * format of the property was invalid, -ENXIO if the requested
1128 * address type was not found
Simon Glass9e512042017-05-18 20:08:58 -06001129 */
1130int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
1131 const char *propname, struct fdt_pci_addr *addr);
1132
1133/**
Bin Meng7b9cbad2018-08-03 01:14:35 -07001134 * ofnode_read_pci_vendev() - look up PCI vendor and device id
1135 *
1136 * Look at the compatible property of a device node that represents a PCI
1137 * device and extract pci vendor id and device id from it.
1138 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001139 * @node: node to examine
1140 * @vendor: vendor id of the pci device
1141 * @device: device id of the pci device
1142 * Return: 0 if ok, negative on error
Bin Meng7b9cbad2018-08-03 01:14:35 -07001143 */
1144int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
1145
1146/**
Michal Simekdb681d42022-02-23 15:45:40 +01001147 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
1148 *
1149 * Look at the compatible property of a device node that represents a eth phy
1150 * device and extract phy vendor id and device id from it.
1151 *
Heinrich Schuchardtd23f2902022-03-24 16:22:32 +01001152 * @node: node to examine
1153 * @vendor: vendor id of the eth phy device
1154 * @device: device id of the eth phy device
1155 * Return: 0 if ok, negative on error
Michal Simekdb681d42022-02-23 15:45:40 +01001156 */
1157int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
1158
1159/**
Simon Glass9e512042017-05-18 20:08:58 -06001160 * ofnode_read_addr_cells() - Get the number of address cells for a node
1161 *
1162 * This walks back up the tree to find the closest #address-cells property
1163 * which controls the given node.
1164 *
1165 * @node: Node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001166 * Return: number of address cells this node uses
Simon Glass9e512042017-05-18 20:08:58 -06001167 */
1168int ofnode_read_addr_cells(ofnode node);
1169
1170/**
1171 * ofnode_read_size_cells() - Get the number of size cells for a node
1172 *
1173 * This walks back up the tree to find the closest #size-cells property
1174 * which controls the given node.
1175 *
1176 * @node: Node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001177 * Return: number of size cells this node uses
Simon Glass9e512042017-05-18 20:08:58 -06001178 */
1179int ofnode_read_size_cells(ofnode node);
1180
1181/**
Simon Glass878d68c2017-06-12 06:21:31 -06001182 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
1183 *
1184 * This function matches fdt_address_cells().
1185 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001186 * @node: Node to check
1187 * Return: value of #address-cells property in this node, or 2 if none
Simon Glass878d68c2017-06-12 06:21:31 -06001188 */
1189int ofnode_read_simple_addr_cells(ofnode node);
1190
1191/**
1192 * ofnode_read_simple_size_cells() - Get the size cells property in a node
1193 *
1194 * This function matches fdt_size_cells().
1195 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001196 * @node: Node to check
1197 * Return: value of #size-cells property in this node, or 2 if none
Simon Glass878d68c2017-06-12 06:21:31 -06001198 */
1199int ofnode_read_simple_size_cells(ofnode node);
1200
1201/**
Simon Glass9e512042017-05-18 20:08:58 -06001202 * ofnode_pre_reloc() - check if a node should be bound before relocation
1203 *
1204 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
1205 * via special device tree properties.
1206 *
1207 * Before relocation this function can be used to check if nodes are required
1208 * in either SPL or TPL stages.
1209 *
1210 * After relocation and jumping into the real U-Boot binary it is possible to
1211 * determine if a node was bound in one of SPL/TPL stages.
1212 *
Patrick Delaunay54e12232019-05-21 19:19:13 +02001213 * There are 4 settings currently in use
Simon Glasse316fba2023-02-13 08:56:34 -07001214 * - bootph-some-ram: U-Boot proper pre-relocation only
1215 * - bootph-all: all phases
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001216 * Existing platforms only use it to indicate nodes needed in
Simon Glasse316fba2023-02-13 08:56:34 -07001217 * SPL. Should probably be replaced by bootph-pre-ram for new platforms.
1218 * - bootph-pre-ram: SPL and U-Boot pre-relocation
1219 * - bootph-pre-sram: TPL and U-Boot pre-relocation
Simon Glass9e512042017-05-18 20:08:58 -06001220 *
1221 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001222 * Return: true if node is needed in SPL/TL, false otherwise
Simon Glass9e512042017-05-18 20:08:58 -06001223 */
1224bool ofnode_pre_reloc(ofnode node);
1225
Simon Glassc98ad442018-06-11 13:07:12 -06001226/**
1227 * ofnode_read_resource() - Read a resource from a node
1228 *
1229 * Read resource information from a node at the given index
1230 *
1231 * @node: Node to read from
1232 * @index: Index of resource to read (0 = first)
1233 * @res: Returns resource that was read, on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001234 * Return: 0 if OK, -ve on error
Simon Glassc98ad442018-06-11 13:07:12 -06001235 */
Simon Glassdcf98852017-07-25 08:29:55 -06001236int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassc98ad442018-06-11 13:07:12 -06001237
1238/**
1239 * ofnode_read_resource_byname() - Read a resource from a node by name
1240 *
1241 * Read resource information from a node matching the given name. This uses a
1242 * 'reg-names' string list property with the names matching the associated
1243 * 'reg' property list.
1244 *
1245 * @node: Node to read from
1246 * @name: Name of resource to read
1247 * @res: Returns resource that was read, on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001248 * Return: 0 if OK, -ve on error
Simon Glassc98ad442018-06-11 13:07:12 -06001249 */
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +09001250int ofnode_read_resource_byname(ofnode node, const char *name,
1251 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -06001252
Simon Glass3991f422017-08-05 15:45:54 -06001253/**
Simon Glassc60f6712018-06-11 13:07:13 -06001254 * ofnode_by_compatible() - Find the next compatible node
1255 *
1256 * Find the next node after @from that is compatible with @compat
1257 *
1258 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1259 * @compat: Compatible string to match
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001260 * Return: ofnode found, or ofnode_null() if none
Simon Glassc60f6712018-06-11 13:07:13 -06001261 */
1262ofnode ofnode_by_compatible(ofnode from, const char *compat);
1263
1264/**
Jens Wiklander61fba0f2018-08-20 11:09:58 +02001265 * ofnode_by_prop_value() - Find the next node with given property value
1266 *
1267 * Find the next node after @from that has a @propname with a value
1268 * @propval and a length @proplen.
1269 *
Simon Glass2187cb72022-09-06 20:27:23 -06001270 * @from: ofnode to start from. Use ofnode_null() to start at the
1271 * beginning, or the return value from oftree_root() to start at the first
1272 * child of the root
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001273 * @propname: property name to check
1274 * @propval: property value to search for
1275 * @proplen: length of the value in propval
1276 * Return: ofnode found, or ofnode_null() if none
Jens Wiklander61fba0f2018-08-20 11:09:58 +02001277 */
1278ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1279 const void *propval, int proplen);
1280
1281/**
Simon Glass3991f422017-08-05 15:45:54 -06001282 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1283 *
1284 * @node: child node (ofnode, lvalue)
1285 * @parent: parent node (ofnode)
1286 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001287 * This is a wrapper around a for loop and is used like so::
Simon Glass3991f422017-08-05 15:45:54 -06001288 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001289 * ofnode node;
1290 * ofnode_for_each_subnode(node, parent) {
1291 * Use node
1292 * ...
1293 * }
Simon Glass3991f422017-08-05 15:45:54 -06001294 *
1295 * Note that this is implemented as a macro and @node is used as
1296 * iterator in the loop. The parent variable can be a constant or even a
1297 * literal.
1298 */
1299#define ofnode_for_each_subnode(node, parent) \
1300 for (node = ofnode_first_subnode(parent); \
1301 ofnode_valid(node); \
1302 node = ofnode_next_subnode(node))
1303
Mario Six147c6072018-01-15 11:07:19 +01001304/**
Michael Walleb8ec9452021-10-15 15:15:17 +02001305 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1306 * compatible string
1307 *
1308 * @node: child node (ofnode, lvalue)
1309 * @compat: compatible string to match
1310 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001311 * This is a wrapper around a for loop and is used like so::
Michael Walleb8ec9452021-10-15 15:15:17 +02001312 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001313 * ofnode node;
1314 * ofnode_for_each_compatible_node(node, parent, compatible) {
1315 * Use node
1316 * ...
1317 * }
Michael Walleb8ec9452021-10-15 15:15:17 +02001318 *
1319 * Note that this is implemented as a macro and @node is used as
1320 * iterator in the loop.
1321 */
1322#define ofnode_for_each_compatible_node(node, compat) \
1323 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1324 ofnode_valid(node); \
1325 node = ofnode_by_compatible(node, compat))
1326
1327/**
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001328 * ofnode_get_child_count() - get the child count of a ofnode
1329 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001330 * @parent: valid node to get its child count
1331 * Return: the number of subnodes
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001332 */
1333int ofnode_get_child_count(ofnode parent);
1334
1335/**
Fabien Dessenne641067f2019-05-31 15:11:30 +02001336 * ofnode_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +01001337 *
1338 * Translate an address from the device-tree into a CPU physical address. This
1339 * function walks up the tree and applies the various bus mappings along the
1340 * way.
1341 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001342 * @node: Device tree node giving the context in which to translate the address
Mario Six147c6072018-01-15 11:07:19 +01001343 * @in_addr: pointer to the address to translate
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001344 * Return: the translated address; OF_BAD_ADDR on error
Mario Six147c6072018-01-15 11:07:19 +01001345 */
1346u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001347
1348/**
Fabien Dessenne641067f2019-05-31 15:11:30 +02001349 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1350 *
1351 * Translate a DMA address from the device-tree into a CPU physical address.
1352 * This function walks up the tree and applies the various bus mappings along
1353 * the way.
1354 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001355 * @node: Device tree node giving the context in which to translate the
1356 * DMA address
Fabien Dessenne641067f2019-05-31 15:11:30 +02001357 * @in_addr: pointer to the DMA address to translate
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001358 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne641067f2019-05-31 15:11:30 +02001359 */
1360u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1361
1362/**
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001363 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1364 *
1365 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1366 * cpu->bus address translations
1367 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001368 * @node: Device tree node
1369 * @cpu: Pointer to variable storing the range's cpu address
1370 * @bus: Pointer to variable storing the range's bus address
1371 * @size: Pointer to variable storing the range's size
1372 * Return: translated DMA address or OF_BAD_ADDR on error
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001373 */
1374int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1375 u64 *size);
1376
1377/**
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001378 * ofnode_device_is_compatible() - check if the node is compatible with compat
1379 *
1380 * This allows to check whether the node is comaptible with the compat.
1381 *
1382 * @node: Device tree node for which compatible needs to be verified.
1383 * @compat: Compatible string which needs to verified in the given node.
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001384 * Return: true if OK, false if the compatible is not found
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001385 */
1386int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Sixe369e582018-06-26 08:46:48 +02001387
1388/**
1389 * ofnode_write_prop() - Set a property of a ofnode
1390 *
Simon Glass0b58eaa2022-09-06 20:27:32 -06001391 * Note that if @copy is false, the value passed to the function is *not*
1392 * allocated by the function itself, but must be allocated by the caller if
1393 * necessary. However it does allocate memory for the property struct and name.
Mario Sixe369e582018-06-26 08:46:48 +02001394 *
1395 * @node: The node for whose property should be set
1396 * @propname: The name of the property to set
Mario Sixe369e582018-06-26 08:46:48 +02001397 * @value: The new value of the property (must be valid prior to calling
1398 * the function)
Simon Glassbe0789a2022-07-30 15:52:10 -06001399 * @len: The length of the new value of the property
Simon Glass0b58eaa2022-09-06 20:27:32 -06001400 * @copy: true to allocate memory for the value. This only has any effect with
1401 * live tree, since flat tree handles this automatically. It allows a
1402 * node's value to be written to the tree, without requiring that the
1403 * caller allocate it
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001404 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001405 */
Simon Glassbe0789a2022-07-30 15:52:10 -06001406int ofnode_write_prop(ofnode node, const char *propname, const void *value,
Simon Glass0b58eaa2022-09-06 20:27:32 -06001407 int len, bool copy);
Mario Sixe369e582018-06-26 08:46:48 +02001408
1409/**
1410 * ofnode_write_string() - Set a string property of a ofnode
1411 *
1412 * Note that the value passed to the function is *not* allocated by the
1413 * function itself, but must be allocated by the caller if necessary.
1414 *
1415 * @node: The node for whose string property should be set
1416 * @propname: The name of the string property to set
1417 * @value: The new value of the string property (must be valid prior to
1418 * calling the function)
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001419 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001420 */
1421int ofnode_write_string(ofnode node, const char *propname, const char *value);
1422
1423/**
Simon Glass55f79902022-07-30 15:52:14 -06001424 * ofnode_write_u32() - Set an integer property of an ofnode
1425 *
1426 * @node: The node for whose string property should be set
1427 * @propname: The name of the string property to set
1428 * @value: The new value of the 32-bit integer property
1429 * Return: 0 if successful, -ve on error
1430 */
1431int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1432
1433/**
Mario Sixe369e582018-06-26 08:46:48 +02001434 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1435 * ofnode
1436 *
1437 * This function effectively sets the node's "status" property to either "okay"
1438 * or "disable", hence making it available for driver model initialization or
1439 * not.
1440 *
1441 * @node: The node to enable
1442 * @value: Flag that tells the function to either disable or enable the
1443 * node
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001444 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001445 */
1446int ofnode_set_enabled(ofnode node, bool value);
1447
Simon Glass7de8bd02021-08-07 07:24:01 -06001448/**
Sean Anderson8b52f232022-03-28 18:14:37 -04001449 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1450 *
1451 * This function parses PHY handle from the Ethernet controller's ofnode
1452 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1453 *
1454 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1455 * if the result to that is true, this function should not be called.
1456 *
1457 * @eth_node: ofnode belonging to the Ethernet controller
1458 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1459 */
1460ofnode ofnode_get_phy_node(ofnode eth_node);
1461
1462/**
1463 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1464 *
1465 * This function parses the "phy-mode" / "phy-connection-type" property and
1466 * returns the corresponding PHY interface type.
1467 *
1468 * @mac_node: ofnode containing the property
1469 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1470 * error
1471 */
1472phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1473
1474#if CONFIG_IS_ENABLED(DM)
1475/**
Simon Glass7de8bd02021-08-07 07:24:01 -06001476 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1477 *
1478 * This reads a property from the /config node of the devicetree.
1479 *
Simon Glass988f1462022-09-06 20:27:28 -06001480 * This only works with the control FDT.
1481 *
1482 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass7de8bd02021-08-07 07:24:01 -06001483 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001484 * @prop_name: property name to look up
1485 * Return: true, if it exists, false if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001486 */
1487bool ofnode_conf_read_bool(const char *prop_name);
1488
1489/**
1490 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1491 *
1492 * This reads a property from the /config node of the devicetree.
1493 *
Simon Glass988f1462022-09-06 20:27:28 -06001494 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass7de8bd02021-08-07 07:24:01 -06001495 *
1496 * @prop_name: property name to look up
1497 * @default_val: default value to return if the property is not found
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001498 * Return: integer value, if found, or @default_val if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001499 */
1500int ofnode_conf_read_int(const char *prop_name, int default_val);
1501
1502/**
1503 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1504 *
1505 * This reads a property from the /config node of the devicetree.
1506 *
Simon Glass988f1462022-09-06 20:27:28 -06001507 * This only works with the control FDT.
1508 *
1509 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass7de8bd02021-08-07 07:24:01 -06001510 *
1511 * @prop_name: property name to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001512 * Return: string value, if found, or NULL if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001513 */
1514const char *ofnode_conf_read_str(const char *prop_name);
1515
Michal Simekdb5e3492023-08-31 08:59:05 +02001516/**
1517 * ofnode_read_bootscript_address() - Read bootscr-address or bootscr-ram-offset
1518 *
1519 * @bootscr_address: pointer to 64bit address where bootscr-address property value
1520 * is stored
1521 * @bootscr_offset: pointer to 64bit offset address where bootscr-ram-offset
1522 * property value is stored
1523 *
1524 * This reads a bootscr-address or bootscr-ram-offset property from
1525 * the /options/u-boot/ node of the devicetree. bootscr-address holds the full
1526 * address of the boot script file. bootscr-ram-offset holds the boot script
1527 * file offset from the start of the ram base address. When bootscr-address is
1528 * defined, bootscr-ram-offset property is ignored.
1529 *
1530 * This only works with the control FDT.
1531 *
1532 * Return: 0 if OK, -EINVAL if property is not found.
1533 */
1534int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset);
1535
Sean Anderson8b52f232022-03-28 18:14:37 -04001536#else /* CONFIG_DM */
1537static inline bool ofnode_conf_read_bool(const char *prop_name)
1538{
1539 return false;
1540}
Marek Behúnf3dd2132022-04-07 00:32:57 +02001541
Sean Anderson8b52f232022-03-28 18:14:37 -04001542static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1543{
1544 return default_val;
1545}
1546
1547static inline const char *ofnode_conf_read_str(const char *prop_name)
1548{
1549 return NULL;
1550}
Simon Glassffe90392022-09-06 20:27:02 -06001551
Michal Simekdb5e3492023-08-31 08:59:05 +02001552static inline int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset)
1553{
1554 return -EINVAL;
1555}
1556
Sean Anderson8b52f232022-03-28 18:14:37 -04001557#endif /* CONFIG_DM */
Marek Behún123ca112022-04-07 00:33:01 +02001558
Simon Glassffe90392022-09-06 20:27:02 -06001559/**
1560 * of_add_subnode() - add a new subnode to a node
1561 *
1562 * @parent: parent node to add to
1563 * @name: name of subnode
1564 * @nodep: returns pointer to new subnode (valid if the function returns 0
1565 * or -EEXIST)
1566 * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
1567 * -ve on other error
1568 */
1569int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);
1570
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001571/**
1572 * ofnode_copy_props() - copy all properties from one node to another
1573 *
1574 * Makes a copy of all properties from the source note in the destination node.
1575 * Existing properties in the destination node remain unchanged, except that
1576 * any with the same name are overwritten, including changing the size of the
1577 * property.
1578 *
1579 * For livetree, properties are copied / allocated, so the source tree does not
1580 * need to be present afterwards.
1581 *
1582 * @src: Source node to read properties from
1583 * @dst: Destination node to write properties too
1584 */
1585int ofnode_copy_props(ofnode src, ofnode dst);
1586
Simon Glass4984de22017-05-17 17:18:10 -06001587#endif