blob: 7eb04accd62cc79f9453f1bb74603a61f6f82803 [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/**
Simon Glasse0c3c212023-09-26 08:14:40 -0600131 * oftree_new() - Create a new, empty tree
132 *
133 * @treep: Returns a pointer to the tree, on success
134 * Returns: 0 on success, -ENOMEM if out of memory, -E2BIG if !OF_LIVE and
135 * there are too many (flattrees) already
136 */
137int oftree_new(oftree *treep);
138
139/**
Stefan Roese45dbe752020-09-23 08:23:27 +0200140 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glass9e512042017-05-18 20:08:58 -0600141 *
142 * This cannot be called if the reference contains an offset.
143 *
144 * @node: Reference containing struct device_node * (possibly invalid)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100145 * Return: pointer to device node (can be NULL)
Simon Glass9e512042017-05-18 20:08:58 -0600146 */
Simon Glass98306982022-09-06 20:27:04 -0600147static inline struct device_node *ofnode_to_np(ofnode node)
Simon Glass9e512042017-05-18 20:08:58 -0600148{
149#ifdef OF_CHECKS
150 if (!of_live_active())
151 return NULL;
152#endif
153 return node.np;
154}
155
Simon Glass4984de22017-05-17 17:18:10 -0600156/**
Simon Glass4984de22017-05-17 17:18:10 -0600157 * ofnode_valid() - check if an ofnode is valid
158 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100159 * @node: Reference containing offset (possibly invalid)
Simon Glass92291652022-09-06 20:27:26 -0600160 * Return: true if the reference contains a valid ofnode, false if not
Simon Glass4984de22017-05-17 17:18:10 -0600161 */
162static inline bool ofnode_valid(ofnode node)
163{
Simon Glass9e512042017-05-18 20:08:58 -0600164 if (of_live_active())
165 return node.np != NULL;
166 else
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200167 return node.of_offset >= 0;
Simon Glass4984de22017-05-17 17:18:10 -0600168}
169
170/**
Simon Glass928d2672022-09-06 20:27:22 -0600171 * oftree_lookup_fdt() - obtain the FDT pointer from an oftree
172 *
173 * This can only be called when flat tree is enabled
174 *
175 * @tree: Tree to look at
176 * @return FDT pointer from the tree
177 */
178static inline void *oftree_lookup_fdt(oftree tree)
179{
180 if (of_live_active())
181 return NULL;
182 else
183 return tree.fdt;
184}
185
186/**
Simon Glass4984de22017-05-17 17:18:10 -0600187 * offset_to_ofnode() - convert a DT offset to an ofnode
188 *
189 * @of_offset: DT offset (either valid, or -1)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100190 * Return: reference to the associated DT offset
Simon Glass4984de22017-05-17 17:18:10 -0600191 */
192static inline ofnode offset_to_ofnode(int of_offset)
193{
194 ofnode node;
195
Simon Glass9e512042017-05-18 20:08:58 -0600196 if (of_live_active())
197 node.np = NULL;
198 else
Simon Glassb14c5332019-12-06 21:41:36 -0700199 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass4984de22017-05-17 17:18:10 -0600200
201 return node;
202}
203
204/**
Simon Glass9e512042017-05-18 20:08:58 -0600205 * np_to_ofnode() - convert a node pointer to an ofnode
206 *
207 * @np: Live node pointer (can be NULL)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100208 * Return: reference to the associated node pointer
Simon Glass9e512042017-05-18 20:08:58 -0600209 */
Simon Glass98306982022-09-06 20:27:04 -0600210static inline ofnode np_to_ofnode(struct device_node *np)
Simon Glass9e512042017-05-18 20:08:58 -0600211{
212 ofnode node;
213
214 node.np = np;
215
216 return node;
217}
218
219/**
220 * ofnode_is_np() - check if a reference is a node pointer
221 *
222 * This function associated that if there is a valid live tree then all
223 * references will use it. This is because using the flat DT when the live tree
224 * is valid is not permitted.
225 *
226 * @node: reference to check (possibly invalid)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100227 * Return: true if the reference is a live node pointer, false if it is a DT
Simon Glass9e512042017-05-18 20:08:58 -0600228 * offset
229 */
230static inline bool ofnode_is_np(ofnode node)
231{
232#ifdef OF_CHECKS
233 /*
234 * Check our assumption that flat tree offsets are not used when a
235 * live tree is in use.
236 */
237 assert(!ofnode_valid(node) ||
Stefan Roese45dbe752020-09-23 08:23:27 +0200238 (of_live_active() ? ofnode_to_np(node)
239 : ofnode_to_np(node)));
Simon Glass9e512042017-05-18 20:08:58 -0600240#endif
241 return of_live_active() && ofnode_valid(node);
242}
243
244/**
Simon Glass4984de22017-05-17 17:18:10 -0600245 * ofnode_equal() - check if two references are equal
246 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100247 * @ref1: first reference to check (possibly invalid)
248 * @ref2: second reference to check (possibly invalid)
249 * Return: true if equal, else false
Simon Glass4984de22017-05-17 17:18:10 -0600250 */
251static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
252{
253 /* We only need to compare the contents */
254 return ref1.of_offset == ref2.of_offset;
255}
256
Simon Glass9e512042017-05-18 20:08:58 -0600257/**
Simon Glass085d5942022-09-06 20:27:21 -0600258 * oftree_valid() - check if an oftree is valid
259 *
260 * @tree: Reference containing oftree
261 * Return: true if the reference contains a valid oftree, false if node
262 */
263static inline bool oftree_valid(oftree tree)
264{
265 if (of_live_active())
266 return tree.np;
267 else
268 return tree.fdt;
269}
270
271/**
272 * oftree_null() - Obtain a null oftree
273 *
274 * This returns an oftree which points to no tree. It works both with the flat
275 * tree and livetree.
276 */
277static inline oftree oftree_null(void)
278{
279 oftree tree;
280
281 if (of_live_active())
282 tree.np = NULL;
283 else
284 tree.fdt = NULL;
285
286 return tree;
287}
288
289/**
Simon Glass9e512042017-05-18 20:08:58 -0600290 * ofnode_null() - Obtain a null ofnode
291 *
292 * This returns an ofnode which points to no node. It works both with the flat
293 * tree and livetree.
294 */
295static inline ofnode ofnode_null(void)
296{
297 ofnode node;
298
299 if (of_live_active())
300 node.np = NULL;
301 else
302 node.of_offset = -1;
303
304 return node;
305}
306
Simon Glassd0c20ce2020-11-28 17:50:07 -0700307static inline ofnode ofnode_root(void)
308{
309 ofnode node;
310
311 if (of_live_active())
312 node.np = gd_of_root();
313 else
314 node.of_offset = 0;
315
316 return node;
317}
318
Simon Glass9e512042017-05-18 20:08:58 -0600319/**
Simon Glass52ad21a2022-09-06 20:27:16 -0600320 * ofprop_valid() - check if an ofprop is valid
321 *
322 * @prop: Pointer to ofprop to check
323 * Return: true if the reference contains a valid ofprop, false if not
324 */
325static inline bool ofprop_valid(struct ofprop *prop)
326{
327 if (of_live_active())
328 return prop->prop;
329 else
330 return prop->offset >= 0;
331}
332
333/**
Simon Glass33104842022-07-30 15:52:08 -0600334 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
335 *
336 * Returns: reference to the control FDT
337 */
338static inline oftree oftree_default(void)
339{
340 oftree tree;
341
342 if (of_live_active())
343 tree.np = gd_of_root();
344 else
345 tree.fdt = (void *)gd->fdt_blob;
346
347 return tree;
348}
349
350/**
Simon Glass085d5942022-09-06 20:27:21 -0600351 * oftree_from_np() - Returns an oftree from a node pointer
352 *
353 * @root: Root node of the tree
354 * Returns: reference to the given node
355 */
356static inline oftree oftree_from_np(struct device_node *root)
357{
358 oftree tree;
359
360 tree.np = root;
361
362 return tree;
363}
364
365/**
Simon Glassa8f2ac22023-06-01 10:22:42 -0600366 * oftree_dispose() - Dispose of an oftree
367 *
368 * This can be used to dispose of a tree that has been created (other than
369 * the control FDT which must not be disposed)
370 *
371 * @tree: Tree to dispose
372 */
373void oftree_dispose(oftree tree);
374
375/**
Kishon Vijay Abraham I77cbaf82021-07-21 21:28:30 +0530376 * ofnode_name_eq() - Check if the node name is equivalent to a given name
377 * ignoring the unit address
378 *
379 * @node: valid node reference that has to be compared
380 * @name: name that has to be compared with the node name
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100381 * Return: true if matches, false if it doesn't match
Kishon Vijay Abraham I77cbaf82021-07-21 21:28:30 +0530382 */
383bool ofnode_name_eq(ofnode node, const char *name);
384
385/**
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +0200386 * ofnode_read_u8() - Read a 8-bit integer from a property
387 *
388 * @node: valid node reference to read property from
389 * @propname: name of the property to read from
390 * @outp: place to put value (if found)
391 * Return: 0 if OK, -ve on error
392 */
393int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
394
395/**
396 * ofnode_read_u8_default() - Read a 8-bit integer from a property
397 *
398 * @node: valid node reference to read property from
399 * @propname: name of the property to read from
400 * @def: default value to return if the property has no value
401 * Return: property value, or @def if not found
402 */
403u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
404
405/**
406 * ofnode_read_u16() - Read a 16-bit integer from a property
407 *
408 * @node: valid node reference to read property from
409 * @propname: name of the property to read from
410 * @outp: place to put value (if found)
411 * Return: 0 if OK, -ve on error
412 */
413int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
414
415/**
416 * ofnode_read_u16_default() - Read a 16-bit integer from a property
417 *
418 * @node: valid node reference to read property from
419 * @propname: name of the property to read from
420 * @def: default value to return if the property has no value
421 * Return: property value, or @def if not found
422 */
423u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
424
425/**
Simon Glass9e512042017-05-18 20:08:58 -0600426 * ofnode_read_u32() - Read a 32-bit integer from a property
427 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100428 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600429 * @propname: name of the property to read from
430 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100431 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600432 */
433int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
434
435/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200436 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
437 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100438 * @node: valid node reference to read property from
Dario Binacchi4bb70752020-03-29 18:04:41 +0200439 * @propname: name of the property to read from
440 * @index: index of the integer to return
441 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100442 * Return: 0 if OK, -ve on error
Dario Binacchi4bb70752020-03-29 18:04:41 +0200443 */
444int ofnode_read_u32_index(ofnode node, const char *propname, int index,
445 u32 *outp);
446
447/**
Michal Simekfa12dfa2023-08-25 11:37:46 +0200448 * ofnode_read_u64_index() - Read a 64-bit integer from a multi-value property
449 *
450 * @node: valid node reference to read property from
451 * @propname: name of the property to read from
452 * @index: index of the integer to return
453 * @outp: place to put value (if found)
454 * Return: 0 if OK, -ve on error
455 */
456int ofnode_read_u64_index(ofnode node, const char *propname, int index,
457 u64 *outp);
458
459/**
Simon Glass9e512042017-05-18 20:08:58 -0600460 * ofnode_read_s32() - Read a 32-bit integer from a property
461 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100462 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600463 * @propname: name of the property to read from
464 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100465 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600466 */
467static inline int ofnode_read_s32(ofnode node, const char *propname,
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100468 s32 *outp)
Simon Glass9e512042017-05-18 20:08:58 -0600469{
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100470 return ofnode_read_u32(node, propname, (u32 *)outp);
Simon Glass9e512042017-05-18 20:08:58 -0600471}
472
473/**
474 * ofnode_read_u32_default() - Read a 32-bit integer from a property
475 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100476 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600477 * @propname: name of the property to read from
478 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100479 * Return: property value, or @def if not found
Simon Glass9e512042017-05-18 20:08:58 -0600480 */
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100481u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
Simon Glass9e512042017-05-18 20:08:58 -0600482
483/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200484 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
485 * property
486 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100487 * @node: valid node reference to read property from
Dario Binacchi4bb70752020-03-29 18:04:41 +0200488 * @propname: name of the property to read from
489 * @index: index of the integer to return
490 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100491 * Return: property value, or @def if not found
Dario Binacchi4bb70752020-03-29 18:04:41 +0200492 */
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100493u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
Dario Binacchi4bb70752020-03-29 18:04:41 +0200494 u32 def);
495
496/**
Simon Glass9e512042017-05-18 20:08:58 -0600497 * ofnode_read_s32_default() - Read a 32-bit integer from a property
498 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100499 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600500 * @propname: name of the property to read from
501 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100502 * Return: property value, or @def if not found
Simon Glass9e512042017-05-18 20:08:58 -0600503 */
504int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
505
506/**
Lukas Auerafb30122018-11-22 11:26:35 +0100507 * ofnode_read_u64() - Read a 64-bit integer from a property
508 *
509 * @node: valid node reference to read property from
510 * @propname: name of the property to read from
511 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100512 * Return: 0 if OK, -ve on error
Lukas Auerafb30122018-11-22 11:26:35 +0100513 */
514int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
515
516/**
Simon Glass7e5196c2018-06-11 13:07:10 -0600517 * ofnode_read_u64_default() - Read a 64-bit integer from a property
518 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100519 * @node: valid node reference to read property from
Simon Glass7e5196c2018-06-11 13:07:10 -0600520 * @propname: name of the property to read from
521 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100522 * Return: property value, or @def if not found
Simon Glass7e5196c2018-06-11 13:07:10 -0600523 */
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200524u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass7e5196c2018-06-11 13:07:10 -0600525
526/**
Simon Glassa8167d82020-01-27 08:49:44 -0700527 * ofnode_read_prop() - Read a property from a node
528 *
529 * @node: valid node reference to read property from
530 * @propname: name of the property to read
531 * @sizep: if non-NULL, returns the size of the property, or an error code
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100532 * if not found
533 * Return: property value, or NULL if there is no such property
Simon Glassa8167d82020-01-27 08:49:44 -0700534 */
535const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
536
537/**
Simon Glass9e512042017-05-18 20:08:58 -0600538 * ofnode_read_string() - Read a string from a property
539 *
Simon Glassa8167d82020-01-27 08:49:44 -0700540 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600541 * @propname: name of the property to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100542 * Return: string from property value, or NULL if there is no such property
Simon Glass9e512042017-05-18 20:08:58 -0600543 */
544const char *ofnode_read_string(ofnode node, const char *propname);
545
546/**
Simon Glassbed77492017-05-18 20:09:01 -0600547 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glass9e512042017-05-18 20:08:58 -0600548 *
549 * @node: valid node reference to read property from
550 * @propname: name of the property to read
551 * @out_values: pointer to return value, modified only if return value is 0
552 * @sz: number of array elements to read
Simon Glass66d0d0c2022-09-06 20:27:18 -0600553 * Return: 0 on success, -EINVAL if the property does not exist,
554 * -ENODATA if property does not have a value, and -EOVERFLOW if the
555 * property data isn't large enough
Simon Glass9e512042017-05-18 20:08:58 -0600556 *
557 * Search for a property in a device node and read 32-bit value(s) from
Simon Glass66d0d0c2022-09-06 20:27:18 -0600558 * it.
Simon Glass9e512042017-05-18 20:08:58 -0600559 *
560 * The out_values is modified only if a valid u32 value can be decoded.
561 */
562int ofnode_read_u32_array(ofnode node, const char *propname,
563 u32 *out_values, size_t sz);
564
565/**
566 * ofnode_read_bool() - read a boolean value from a property
567 *
568 * @node: valid node reference to read property from
569 * @propname: name of property to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100570 * Return: true if property is present (meaning true), false if not present
Simon Glass9e512042017-05-18 20:08:58 -0600571 */
572bool ofnode_read_bool(ofnode node, const char *propname);
573
574/**
575 * ofnode_find_subnode() - find a named subnode of a parent node
576 *
577 * @node: valid reference to parent node
578 * @subnode_name: name of subnode to find
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100579 * Return: reference to subnode (which can be invalid if there is no such
Simon Glass9e512042017-05-18 20:08:58 -0600580 * subnode)
581 */
582ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
583
Simon Glassec1add12020-12-16 17:25:06 -0700584#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass401d1c42020-10-30 21:38:53 -0600585#include <asm/global_data.h>
586
Simon Glassec1add12020-12-16 17:25:06 -0700587static inline bool ofnode_is_enabled(ofnode node)
588{
589 if (ofnode_is_np(node)) {
590 return of_device_is_available(ofnode_to_np(node));
591 } else {
592 return fdtdec_get_is_enabled(gd->fdt_blob,
593 ofnode_to_offset(node));
594 }
595}
596
597static inline ofnode ofnode_first_subnode(ofnode node)
598{
599 assert(ofnode_valid(node));
600 if (ofnode_is_np(node))
601 return np_to_ofnode(node.np->child);
602
603 return offset_to_ofnode(
604 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
605}
606
607static inline ofnode ofnode_next_subnode(ofnode node)
608{
609 assert(ofnode_valid(node));
610 if (ofnode_is_np(node))
611 return np_to_ofnode(node.np->sibling);
612
613 return offset_to_ofnode(
614 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
615}
616#else
617/**
618 * ofnode_is_enabled() - Checks whether a node is enabled.
619 * This looks for a 'status' property. If this exists, then returns true if
620 * the status is 'okay' and false otherwise. If there is no status property,
621 * it returns true on the assumption that anything mentioned should be enabled
622 * by default.
623 *
624 * @node: node to examine
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100625 * Return: false (not enabled) or true (enabled)
Simon Glassec1add12020-12-16 17:25:06 -0700626 */
627bool ofnode_is_enabled(ofnode node);
628
Simon Glass9e512042017-05-18 20:08:58 -0600629/**
630 * ofnode_first_subnode() - find the first subnode of a parent node
631 *
632 * @node: valid reference to a valid parent node
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100633 * Return: reference to the first subnode (which can be invalid if the parent
Simon Glass9e512042017-05-18 20:08:58 -0600634 * node has no subnodes)
635 */
636ofnode ofnode_first_subnode(ofnode node);
637
638/**
639 * ofnode_next_subnode() - find the next sibling of a subnode
640 *
641 * @node: valid reference to previous node (sibling)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100642 * Return: reference to the next subnode (which can be invalid if the node
Simon Glass9e512042017-05-18 20:08:58 -0600643 * has no more siblings)
644 */
645ofnode ofnode_next_subnode(ofnode node);
Simon Glassec1add12020-12-16 17:25:06 -0700646#endif /* DM_INLINE_OFNODE */
Simon Glass9e512042017-05-18 20:08:58 -0600647
648/**
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100649 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
650 *
651 * @node: valid node to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100652 * Return: ofnode reference of the parent node
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100653 */
654ofnode ofnode_get_parent(ofnode node);
655
656/**
Simon Glass9e512042017-05-18 20:08:58 -0600657 * ofnode_get_name() - get the name of a node
658 *
659 * @node: valid node to look up
Simon Glassf46ec932022-09-06 20:27:15 -0600660 * Return: name of node (for the root node this is "")
Simon Glass9e512042017-05-18 20:08:58 -0600661 */
662const char *ofnode_get_name(ofnode node);
663
664/**
Marek Behún0e116be2021-05-26 14:08:18 +0200665 * ofnode_get_path() - get the full path of a node
666 *
667 * @node: valid node to look up
668 * @buf: buffer to write the node path into
669 * @buflen: buffer size
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100670 * Return: 0 if OK, -ve on error
Marek Behún0e116be2021-05-26 14:08:18 +0200671 */
672int ofnode_get_path(ofnode node, char *buf, int buflen);
673
674/**
Kever Yangb4f20762018-02-23 17:38:50 +0100675 * ofnode_get_by_phandle() - get ofnode from phandle
676 *
Simon Glass829d5122022-09-06 20:26:57 -0600677 * This uses the default (control) device tree
678 *
Kever Yangb4f20762018-02-23 17:38:50 +0100679 * @phandle: phandle to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100680 * Return: ofnode reference to the phandle
Kever Yangb4f20762018-02-23 17:38:50 +0100681 */
682ofnode ofnode_get_by_phandle(uint phandle);
683
684/**
Simon Glass928d2672022-09-06 20:27:22 -0600685 * oftree_get_by_phandle() - get ofnode from phandle
686 *
687 * @tree: tree to use
688 * @phandle: phandle to look up
689 * Return: ofnode reference to the phandle
690 */
691ofnode oftree_get_by_phandle(oftree tree, uint phandle);
692
693/**
Simon Glass9e512042017-05-18 20:08:58 -0600694 * ofnode_read_size() - read the size of a property
695 *
696 * @node: node to check
697 * @propname: property to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100698 * Return: size of property if present, or -EINVAL if not
Simon Glass9e512042017-05-18 20:08:58 -0600699 */
700int ofnode_read_size(ofnode node, const char *propname);
701
702/**
Keerthye679d032019-04-24 17:19:53 +0530703 * ofnode_get_addr_size_index() - get an address/size from a node
704 * based on index
705 *
706 * This reads the register address/size from a node based on index
707 *
708 * @node: node to read from
709 * @index: Index of address to read (0 for first)
710 * @size: Pointer to size of the address
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100711 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Keerthye679d032019-04-24 17:19:53 +0530712 */
Johan Jonkeraecae812023-03-13 01:30:33 +0100713fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index,
714 fdt_size_t *size);
Keerthye679d032019-04-24 17:19:53 +0530715
716/**
Marek Behún31a7b712021-05-26 14:08:17 +0200717 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
718 * based on index, without address
719 * translation
720 *
721 * This reads the register address/size from a node based on index.
722 * The resulting address is not translated. Useful for example for on-disk
723 * addresses.
724 *
725 * @node: node to read from
726 * @index: Index of address to read (0 for first)
727 * @size: Pointer to size of the address
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100728 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Marek Behún31a7b712021-05-26 14:08:17 +0200729 */
Johan Jonkeraecae812023-03-13 01:30:33 +0100730fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
731 fdt_size_t *size);
Marek Behún31a7b712021-05-26 14:08:17 +0200732
733/**
Simon Glassbed77492017-05-18 20:09:01 -0600734 * ofnode_get_addr_index() - get an address from a node
735 *
736 * This reads the register address from a node
737 *
738 * @node: node to read from
739 * @index: Index of address to read (0 for first)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100740 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glassbed77492017-05-18 20:09:01 -0600741 */
Johan Jonkeraecae812023-03-13 01:30:33 +0100742fdt_addr_t ofnode_get_addr_index(ofnode node, int index);
Simon Glassbed77492017-05-18 20:09:01 -0600743
744/**
745 * ofnode_get_addr() - get an address from a node
746 *
747 * This reads the register address from a node
748 *
749 * @node: node to read from
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100750 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glassbed77492017-05-18 20:09:01 -0600751 */
Johan Jonkeraecae812023-03-13 01:30:33 +0100752fdt_addr_t ofnode_get_addr(ofnode node);
Simon Glassbed77492017-05-18 20:09:01 -0600753
754/**
Chen Guanqiaoaa351a12021-04-12 14:51:11 +0800755 * ofnode_get_size() - get size from a node
756 *
757 * This reads the register size from a node
758 *
759 * @node: node to read from
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100760 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
Chen Guanqiaoaa351a12021-04-12 14:51:11 +0800761 */
762fdt_size_t ofnode_get_size(ofnode node);
763
764/**
Simon Glass9e512042017-05-18 20:08:58 -0600765 * ofnode_stringlist_search() - find a string in a string list and return index
766 *
767 * Note that it is possible for this function to succeed on property values
768 * that are not NUL-terminated. That's because the function will stop after
769 * finding the first occurrence of @string. This can for example happen with
770 * small-valued cell properties, such as #address-cells, when searching for
771 * the empty string.
772 *
773 * @node: node to check
774 * @propname: name of the property containing the string list
775 * @string: string to look up in the string list
776 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100777 * Return:
Simon Glass9e512042017-05-18 20:08:58 -0600778 * the index of the string in the list of strings
779 * -ENODATA if the property is not found
780 * -EINVAL on some other error
781 */
782int ofnode_stringlist_search(ofnode node, const char *propname,
783 const char *string);
784
785/**
Simon Glass8c293d62017-06-12 06:21:28 -0600786 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glass9e512042017-05-18 20:08:58 -0600787 *
788 * Note that this will successfully extract strings from properties with
789 * non-NUL-terminated values. For example on small-valued cell properties
790 * this function will return the empty string.
791 *
792 * If non-NULL, the length of the string (on success) or a negative error-code
793 * (on failure) will be stored in the integer pointer to by lenp.
794 *
795 * @node: node to check
796 * @propname: name of the property containing the string list
Simon Glass32c6a8e2021-10-23 17:26:06 -0600797 * @index: index of the string to return (cannot be negative)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100798 * @outp: return location for the string
Simon Glass9e512042017-05-18 20:08:58 -0600799 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100800 * Return:
Simon Glass32c6a8e2021-10-23 17:26:06 -0600801 * 0 if found or -ve error value if not found
Simon Glass9e512042017-05-18 20:08:58 -0600802 */
803int ofnode_read_string_index(ofnode node, const char *propname, int index,
804 const char **outp);
805
806/**
Simon Glass8c293d62017-06-12 06:21:28 -0600807 * ofnode_read_string_count() - find the number of strings in a string list
808 *
809 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100810 * @property: name of the property containing the string list
811 * Return:
Simon Glass8c293d62017-06-12 06:21:28 -0600812 * number of strings in the list, or -ve error value if not found
813 */
814int ofnode_read_string_count(ofnode node, const char *property);
815
816/**
Simon Glass075bfc92021-10-23 17:26:07 -0600817 * ofnode_read_string_list() - read a list of strings
818 *
819 * This produces a list of string pointers with each one pointing to a string
820 * in the string list. If the property does not exist, it returns {NULL}.
821 *
822 * The data is allocated and the caller is reponsible for freeing the return
823 * value (the list of string pointers). The strings themselves may not be
824 * changed as they point directly into the devicetree property.
825 *
826 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100827 * @property: name of the property containing the string list
Simon Glass075bfc92021-10-23 17:26:07 -0600828 * @listp: returns an allocated, NULL-terminated list of strings if the return
829 * value is > 0, else is set to NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100830 * Return:
831 * number of strings in list, 0 if none, -ENOMEM if out of memory,
832 * -EINVAL if no such property, -EENODATA if property is empty
Simon Glass075bfc92021-10-23 17:26:07 -0600833 */
834int ofnode_read_string_list(ofnode node, const char *property,
835 const char ***listp);
836
837/**
Simon Glass9e512042017-05-18 20:08:58 -0600838 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
839 *
840 * This function is useful to parse lists of phandles and their arguments.
841 * Returns 0 on success and fills out_args, on error returns appropriate
842 * errno value.
843 *
844 * Caller is responsible to call of_node_put() on the returned out_args->np
845 * pointer.
846 *
847 * Example:
848 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100849 * .. code-block::
Simon Glass9e512042017-05-18 20:08:58 -0600850 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100851 * phandle1: node1 {
852 * #list-cells = <2>;
853 * };
854 * phandle2: node2 {
855 * #list-cells = <1>;
856 * };
857 * node3 {
858 * list = <&phandle1 1 2 &phandle2 3>;
859 * };
Simon Glass9e512042017-05-18 20:08:58 -0600860 *
861 * To get a device_node of the `node2' node you may call this:
862 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
863 *
864 * @node: device tree node containing a list
865 * @list_name: property name that contains a list
866 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100867 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glass9e512042017-05-18 20:08:58 -0600868 * @index: index of a phandle to parse out
869 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100870 * Return:
871 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
872 * @list_name does not exist, -EINVAL if a phandle was not found,
873 * @cells_name could not be found, the arguments were truncated or there
874 * were too many arguments.
Simon Glass9e512042017-05-18 20:08:58 -0600875 */
876int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
877 const char *cells_name, int cell_count,
878 int index,
879 struct ofnode_phandle_args *out_args);
880
881/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200882 * ofnode_count_phandle_with_args() - Count number of phandle in a list
883 *
884 * This function is useful to count phandles into a list.
885 * Returns number of phandle on success, on error returns appropriate
886 * errno value.
887 *
888 * @node: device tree node containing a list
889 * @list_name: property name that contains a list
890 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100891 * @cell_count: Cell count to use if @cells_name is NULL
892 * Return:
893 * number of phandle on success, -ENOENT if @list_name does not exist,
894 * -EINVAL if a phandle was not found, @cells_name could not be found.
Patrice Chotard642346a2017-07-18 11:57:08 +0200895 */
896int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200897 const char *cells_name, int cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +0200898
899/**
Simon Glass9e512042017-05-18 20:08:58 -0600900 * ofnode_path() - find a node by full path
901 *
Simon Glass33104842022-07-30 15:52:08 -0600902 * This uses the control FDT.
903 *
Simon Glass9e512042017-05-18 20:08:58 -0600904 * @path: Full path to node, e.g. "/bus/spi@1"
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100905 * Return: reference to the node found. Use ofnode_valid() to check if it exists
Simon Glass9e512042017-05-18 20:08:58 -0600906 */
907ofnode ofnode_path(const char *path);
908
909/**
Simon Glassb7bd94f2022-09-06 20:27:24 -0600910 * oftree_path() - find a node by full path from a root node
Simon Glass33104842022-07-30 15:52:08 -0600911 *
912 * @tree: Device tree to use
913 * @path: Full path to node, e.g. "/bus/spi@1"
914 * Return: reference to the node found. Use ofnode_valid() to check if it exists
915 */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600916ofnode oftree_path(oftree tree, const char *path);
917
918/**
919 * oftree_root() - get the root node of a tree
920 *
921 * @tree: Device tree to use
922 * Return: reference to the root node
923 */
924ofnode oftree_root(oftree tree);
Simon Glass33104842022-07-30 15:52:08 -0600925
926/**
Simon Glassbd933bf2020-01-27 08:49:46 -0700927 * ofnode_read_chosen_prop() - get the value of a chosen property
928 *
Simon Glassb7bd94f2022-09-06 20:27:24 -0600929 * This looks for a property within the /chosen node and returns its value.
930 *
931 * This only works with the control FDT.
Simon Glassbd933bf2020-01-27 08:49:46 -0700932 *
933 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100934 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Simon Glassbd933bf2020-01-27 08:49:46 -0700935 * returns NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100936 * Return: property value if found, else NULL
Simon Glassbd933bf2020-01-27 08:49:46 -0700937 */
938const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
939
940/**
Simon Glass14ca9f72020-01-27 08:49:43 -0700941 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glass9e512042017-05-18 20:08:58 -0600942 *
Simon Glass14ca9f72020-01-27 08:49:43 -0700943 * This looks for a property within the /chosen node and returns its value,
944 * checking that it is a valid nul-terminated string
Simon Glass9e512042017-05-18 20:08:58 -0600945 *
Simon Glass988f1462022-09-06 20:27:28 -0600946 * This only works with the control FDT.
947 *
Simon Glass9e512042017-05-18 20:08:58 -0600948 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100949 * Return: string value if found, else NULL
Simon Glass9e512042017-05-18 20:08:58 -0600950 */
Simon Glass14ca9f72020-01-27 08:49:43 -0700951const char *ofnode_read_chosen_string(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600952
953/**
Simon Glass74d594a2020-01-27 08:49:42 -0700954 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glass9e512042017-05-18 20:08:58 -0600955 *
Simon Glass74d594a2020-01-27 08:49:42 -0700956 * This looks up a named property in the chosen node and uses that as a path to
957 * look up a code.
958 *
Simon Glass988f1462022-09-06 20:27:28 -0600959 * This only works with the control FDT.
960 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100961 * @propname: Property name to look for
962 * Return: the referenced node if present, else ofnode_null()
Simon Glass9e512042017-05-18 20:08:58 -0600963 */
Simon Glass74d594a2020-01-27 08:49:42 -0700964ofnode ofnode_get_chosen_node(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600965
Michal Simek305d3182020-07-28 12:51:08 +0200966/**
967 * ofnode_read_aliases_prop() - get the value of a aliases property
968 *
969 * This looks for a property within the /aliases node and returns its value
970 *
Simon Glass988f1462022-09-06 20:27:28 -0600971 * This only works with the control FDT.
972 *
Michal Simek305d3182020-07-28 12:51:08 +0200973 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100974 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Michal Simek305d3182020-07-28 12:51:08 +0200975 * returns NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100976 * Return: property value if found, else NULL
Michal Simek305d3182020-07-28 12:51:08 +0200977 */
978const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
979
980/**
981 * ofnode_get_aliases_node() - get a referenced node from the aliases node
982 *
983 * This looks up a named property in the aliases node and uses that as a path to
984 * look up a code.
985 *
Simon Glass988f1462022-09-06 20:27:28 -0600986 * This only works with the control FDT.
987 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100988 * @propname: Property name to look for
989 * Return: the referenced node if present, else ofnode_null()
Michal Simek305d3182020-07-28 12:51:08 +0200990 */
991ofnode ofnode_get_aliases_node(const char *propname);
992
Simon Glass9e512042017-05-18 20:08:58 -0600993struct display_timing;
994/**
995 * ofnode_decode_display_timing() - decode display timings
996 *
997 * Decode display timings from the supplied 'display-timings' node.
998 * See doc/device-tree-bindings/video/display-timing.txt for binding
999 * information.
1000 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001001 * @node: 'display-timing' node containing the timing subnodes
1002 * @index: Index number to read (0=first timing subnode)
1003 * @config: Place to put timings
1004 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Simon Glass9e512042017-05-18 20:08:58 -06001005 */
1006int ofnode_decode_display_timing(ofnode node, int index,
1007 struct display_timing *config);
1008
1009/**
Nikhil M Jain0347cc72023-01-31 15:35:14 +05301010 * ofnode_decode_panel_timing() - decode display timings
1011 *
1012 * Decode panel timings from the supplied 'panel-timings' node.
1013 *
1014 * @node: 'display-timing' node containing the timing subnodes
1015 * @config: Place to put timings
1016 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
1017 */
1018int ofnode_decode_panel_timing(ofnode node,
1019 struct display_timing *config);
1020
1021/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001022 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glass9e512042017-05-18 20:08:58 -06001023 *
1024 * @node: node to read
1025 * @propname: property to read
1026 * @lenp: place to put length on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001027 * Return: pointer to property, or NULL if not found
Simon Glass9e512042017-05-18 20:08:58 -06001028 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +09001029const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glass9e512042017-05-18 20:08:58 -06001030
1031/**
Simon Glass4b1f5712022-09-06 20:27:13 -06001032 * ofnode_first_property()- get the reference of the first property
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001033 *
1034 * Get reference to the first property of the node, it is used to iterate
Simon Glass92432242022-09-06 20:27:14 -06001035 * and read all the property with ofprop_get_property().
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001036 *
1037 * @node: node to read
1038 * @prop: place to put argument reference
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001039 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001040 */
Simon Glass4b1f5712022-09-06 20:27:13 -06001041int ofnode_first_property(ofnode node, struct ofprop *prop);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001042
1043/**
Simon Glass4b1f5712022-09-06 20:27:13 -06001044 * ofnode_next_property() - get the reference of the next property
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001045 *
1046 * Get reference to the next property of the node, it is used to iterate
Simon Glass92432242022-09-06 20:27:14 -06001047 * and read all the property with ofprop_get_property().
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001048 *
1049 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001050 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001051 */
Simon Glass4b1f5712022-09-06 20:27:13 -06001052int ofnode_next_property(struct ofprop *prop);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001053
1054/**
Simon Glass52ad21a2022-09-06 20:27:16 -06001055 * ofnode_for_each_prop() - iterate over all properties of a node
1056 *
1057 * @prop: struct ofprop
1058 * @node: node (lvalue, ofnode)
1059 *
1060 * This is a wrapper around a for loop and is used like this::
1061 *
1062 * ofnode node;
1063 * struct ofprop prop;
1064 *
1065 * ofnode_for_each_prop(prop, node) {
1066 * ...use prop...
1067 * }
1068 *
1069 * Note that this is implemented as a macro and @prop is used as
1070 * iterator in the loop. The parent variable can be a constant or even a
1071 * literal.
1072 */
1073#define ofnode_for_each_prop(prop, node) \
1074 for (ofnode_first_property(node, &prop); \
1075 ofprop_valid(&prop); \
1076 ofnode_next_property(&prop))
1077
1078/**
Simon Glass92432242022-09-06 20:27:14 -06001079 * ofprop_get_property() - get a pointer to the value of a property
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001080 *
1081 * Get value for the property identified by the provided reference.
1082 *
1083 * @prop: reference on property
1084 * @propname: If non-NULL, place to property name on success,
Simon Glass92432242022-09-06 20:27:14 -06001085 * @lenp: If non-NULL, place to put length on success, or error code on failure
1086 * Return: pointer to property, or NULL if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001087 */
Simon Glass92432242022-09-06 20:27:14 -06001088const void *ofprop_get_property(const struct ofprop *prop,
1089 const char **propname, int *lenp);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001090
1091/**
Simon Glass9e512042017-05-18 20:08:58 -06001092 * ofnode_get_addr_size() - get address and size from a property
1093 *
1094 * This does no address translation. It simply reads an property that contains
1095 * an address and a size value, one after the other.
1096 *
1097 * @node: node to read from
1098 * @propname: property to read
1099 * @sizep: place to put size value (on success)
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001100 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glass9e512042017-05-18 20:08:58 -06001101 */
Johan Jonkeraecae812023-03-13 01:30:33 +01001102fdt_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
1103 fdt_size_t *sizep);
Simon Glass9e512042017-05-18 20:08:58 -06001104
1105/**
1106 * ofnode_read_u8_array_ptr() - find an 8-bit array
1107 *
1108 * Look up a property in a node and return a pointer to its contents as a
1109 * byte array of given length. The property must have at least enough data
1110 * for the array (count bytes). It may have more, but this will be ignored.
1111 * The data is not copied.
1112 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001113 * @node: node to examine
1114 * @propname: name of property to find
1115 * @sz: number of array elements
1116 * Return:
1117 * pointer to byte array if found, or NULL if the property is not found or
1118 * there is not enough data
Simon Glass9e512042017-05-18 20:08:58 -06001119 */
1120const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
1121 size_t sz);
1122
1123/**
1124 * ofnode_read_pci_addr() - look up a PCI address
1125 *
1126 * Look at an address property in a node and return the PCI address which
1127 * corresponds to the given type in the form of fdt_pci_addr.
1128 * The property must hold one fdt_pci_addr with a lengh.
1129 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001130 * @node: node to examine
1131 * @type: pci address type (FDT_PCI_SPACE_xxx)
1132 * @propname: name of property to find
1133 * @addr: returns pci address in the form of fdt_pci_addr
1134 * Return:
1135 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
1136 * format of the property was invalid, -ENXIO if the requested
1137 * address type was not found
Simon Glass9e512042017-05-18 20:08:58 -06001138 */
1139int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
1140 const char *propname, struct fdt_pci_addr *addr);
1141
1142/**
Bin Meng7b9cbad2018-08-03 01:14:35 -07001143 * ofnode_read_pci_vendev() - look up PCI vendor and device id
1144 *
1145 * Look at the compatible property of a device node that represents a PCI
1146 * device and extract pci vendor id and device id from it.
1147 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001148 * @node: node to examine
1149 * @vendor: vendor id of the pci device
1150 * @device: device id of the pci device
1151 * Return: 0 if ok, negative on error
Bin Meng7b9cbad2018-08-03 01:14:35 -07001152 */
1153int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
1154
1155/**
Michal Simekdb681d42022-02-23 15:45:40 +01001156 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
1157 *
1158 * Look at the compatible property of a device node that represents a eth phy
1159 * device and extract phy vendor id and device id from it.
1160 *
Heinrich Schuchardtd23f2902022-03-24 16:22:32 +01001161 * @node: node to examine
1162 * @vendor: vendor id of the eth phy device
1163 * @device: device id of the eth phy device
1164 * Return: 0 if ok, negative on error
Michal Simekdb681d42022-02-23 15:45:40 +01001165 */
1166int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
1167
1168/**
Simon Glass9e512042017-05-18 20:08:58 -06001169 * ofnode_read_addr_cells() - Get the number of address cells for a node
1170 *
1171 * This walks back up the tree to find the closest #address-cells property
1172 * which controls the given node.
1173 *
1174 * @node: Node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001175 * Return: number of address cells this node uses
Simon Glass9e512042017-05-18 20:08:58 -06001176 */
1177int ofnode_read_addr_cells(ofnode node);
1178
1179/**
1180 * ofnode_read_size_cells() - Get the number of size cells for a node
1181 *
1182 * This walks back up the tree to find the closest #size-cells property
1183 * which controls the given node.
1184 *
1185 * @node: Node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001186 * Return: number of size cells this node uses
Simon Glass9e512042017-05-18 20:08:58 -06001187 */
1188int ofnode_read_size_cells(ofnode node);
1189
1190/**
Simon Glass878d68c2017-06-12 06:21:31 -06001191 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
1192 *
1193 * This function matches fdt_address_cells().
1194 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001195 * @node: Node to check
1196 * Return: value of #address-cells property in this node, or 2 if none
Simon Glass878d68c2017-06-12 06:21:31 -06001197 */
1198int ofnode_read_simple_addr_cells(ofnode node);
1199
1200/**
1201 * ofnode_read_simple_size_cells() - Get the size cells property in a node
1202 *
1203 * This function matches fdt_size_cells().
1204 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001205 * @node: Node to check
1206 * Return: value of #size-cells property in this node, or 2 if none
Simon Glass878d68c2017-06-12 06:21:31 -06001207 */
1208int ofnode_read_simple_size_cells(ofnode node);
1209
1210/**
Simon Glass9e512042017-05-18 20:08:58 -06001211 * ofnode_pre_reloc() - check if a node should be bound before relocation
1212 *
1213 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
1214 * via special device tree properties.
1215 *
1216 * Before relocation this function can be used to check if nodes are required
1217 * in either SPL or TPL stages.
1218 *
1219 * After relocation and jumping into the real U-Boot binary it is possible to
1220 * determine if a node was bound in one of SPL/TPL stages.
1221 *
Patrick Delaunay54e12232019-05-21 19:19:13 +02001222 * There are 4 settings currently in use
Jonas Karlman9e644282023-08-20 22:03:18 +00001223 * - bootph-some-ram: U-Boot proper pre-relocation phase
Simon Glasse316fba2023-02-13 08:56:34 -07001224 * - bootph-all: all phases
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001225 * Existing platforms only use it to indicate nodes needed in
Simon Glasse316fba2023-02-13 08:56:34 -07001226 * SPL. Should probably be replaced by bootph-pre-ram for new platforms.
Jonas Karlman9e644282023-08-20 22:03:18 +00001227 * - bootph-pre-ram: SPL phase
1228 * - bootph-pre-sram: TPL phase
Simon Glass9e512042017-05-18 20:08:58 -06001229 *
1230 * @node: node to check
Jonas Karlman9e644282023-08-20 22:03:18 +00001231 * Return: true if node should be or was bound, false otherwise
Simon Glass9e512042017-05-18 20:08:58 -06001232 */
1233bool ofnode_pre_reloc(ofnode node);
1234
Simon Glassc98ad442018-06-11 13:07:12 -06001235/**
1236 * ofnode_read_resource() - Read a resource from a node
1237 *
1238 * Read resource information from a node at the given index
1239 *
1240 * @node: Node to read from
1241 * @index: Index of resource to read (0 = first)
1242 * @res: Returns resource that was read, on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001243 * Return: 0 if OK, -ve on error
Simon Glassc98ad442018-06-11 13:07:12 -06001244 */
Simon Glassdcf98852017-07-25 08:29:55 -06001245int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassc98ad442018-06-11 13:07:12 -06001246
1247/**
1248 * ofnode_read_resource_byname() - Read a resource from a node by name
1249 *
1250 * Read resource information from a node matching the given name. This uses a
1251 * 'reg-names' string list property with the names matching the associated
1252 * 'reg' property list.
1253 *
1254 * @node: Node to read from
1255 * @name: Name of resource to read
1256 * @res: Returns resource that was read, on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001257 * Return: 0 if OK, -ve on error
Simon Glassc98ad442018-06-11 13:07:12 -06001258 */
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +09001259int ofnode_read_resource_byname(ofnode node, const char *name,
1260 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -06001261
Simon Glass3991f422017-08-05 15:45:54 -06001262/**
Simon Glassc60f6712018-06-11 13:07:13 -06001263 * ofnode_by_compatible() - Find the next compatible node
1264 *
1265 * Find the next node after @from that is compatible with @compat
1266 *
1267 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1268 * @compat: Compatible string to match
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001269 * Return: ofnode found, or ofnode_null() if none
Simon Glassc60f6712018-06-11 13:07:13 -06001270 */
1271ofnode ofnode_by_compatible(ofnode from, const char *compat);
1272
1273/**
Jens Wiklander61fba0f2018-08-20 11:09:58 +02001274 * ofnode_by_prop_value() - Find the next node with given property value
1275 *
1276 * Find the next node after @from that has a @propname with a value
1277 * @propval and a length @proplen.
1278 *
Simon Glass2187cb72022-09-06 20:27:23 -06001279 * @from: ofnode to start from. Use ofnode_null() to start at the
1280 * beginning, or the return value from oftree_root() to start at the first
1281 * child of the root
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001282 * @propname: property name to check
1283 * @propval: property value to search for
1284 * @proplen: length of the value in propval
1285 * Return: ofnode found, or ofnode_null() if none
Jens Wiklander61fba0f2018-08-20 11:09:58 +02001286 */
1287ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1288 const void *propval, int proplen);
1289
1290/**
Simon Glass3991f422017-08-05 15:45:54 -06001291 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1292 *
1293 * @node: child node (ofnode, lvalue)
1294 * @parent: parent node (ofnode)
1295 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001296 * This is a wrapper around a for loop and is used like so::
Simon Glass3991f422017-08-05 15:45:54 -06001297 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001298 * ofnode node;
1299 * ofnode_for_each_subnode(node, parent) {
1300 * Use node
1301 * ...
1302 * }
Simon Glass3991f422017-08-05 15:45:54 -06001303 *
1304 * Note that this is implemented as a macro and @node is used as
1305 * iterator in the loop. The parent variable can be a constant or even a
1306 * literal.
1307 */
1308#define ofnode_for_each_subnode(node, parent) \
1309 for (node = ofnode_first_subnode(parent); \
1310 ofnode_valid(node); \
1311 node = ofnode_next_subnode(node))
1312
Mario Six147c6072018-01-15 11:07:19 +01001313/**
Michael Walleb8ec9452021-10-15 15:15:17 +02001314 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1315 * compatible string
1316 *
1317 * @node: child node (ofnode, lvalue)
1318 * @compat: compatible string to match
1319 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001320 * This is a wrapper around a for loop and is used like so::
Michael Walleb8ec9452021-10-15 15:15:17 +02001321 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001322 * ofnode node;
1323 * ofnode_for_each_compatible_node(node, parent, compatible) {
1324 * Use node
1325 * ...
1326 * }
Michael Walleb8ec9452021-10-15 15:15:17 +02001327 *
1328 * Note that this is implemented as a macro and @node is used as
1329 * iterator in the loop.
1330 */
1331#define ofnode_for_each_compatible_node(node, compat) \
1332 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1333 ofnode_valid(node); \
1334 node = ofnode_by_compatible(node, compat))
1335
1336/**
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001337 * ofnode_get_child_count() - get the child count of a ofnode
1338 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001339 * @parent: valid node to get its child count
1340 * Return: the number of subnodes
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001341 */
1342int ofnode_get_child_count(ofnode parent);
1343
1344/**
Fabien Dessenne641067f2019-05-31 15:11:30 +02001345 * ofnode_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +01001346 *
1347 * Translate an address from the device-tree into a CPU physical address. This
1348 * function walks up the tree and applies the various bus mappings along the
1349 * way.
1350 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001351 * @node: Device tree node giving the context in which to translate the address
Mario Six147c6072018-01-15 11:07:19 +01001352 * @in_addr: pointer to the address to translate
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001353 * Return: the translated address; OF_BAD_ADDR on error
Mario Six147c6072018-01-15 11:07:19 +01001354 */
1355u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001356
1357/**
Fabien Dessenne641067f2019-05-31 15:11:30 +02001358 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1359 *
1360 * Translate a DMA address from the device-tree into a CPU physical address.
1361 * This function walks up the tree and applies the various bus mappings along
1362 * the way.
1363 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001364 * @node: Device tree node giving the context in which to translate the
1365 * DMA address
Fabien Dessenne641067f2019-05-31 15:11:30 +02001366 * @in_addr: pointer to the DMA address to translate
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001367 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne641067f2019-05-31 15:11:30 +02001368 */
1369u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1370
1371/**
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001372 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1373 *
1374 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1375 * cpu->bus address translations
1376 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001377 * @node: Device tree node
1378 * @cpu: Pointer to variable storing the range's cpu address
1379 * @bus: Pointer to variable storing the range's bus address
1380 * @size: Pointer to variable storing the range's size
1381 * Return: translated DMA address or OF_BAD_ADDR on error
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001382 */
1383int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1384 u64 *size);
1385
1386/**
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001387 * ofnode_device_is_compatible() - check if the node is compatible with compat
1388 *
1389 * This allows to check whether the node is comaptible with the compat.
1390 *
1391 * @node: Device tree node for which compatible needs to be verified.
1392 * @compat: Compatible string which needs to verified in the given node.
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001393 * Return: true if OK, false if the compatible is not found
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001394 */
1395int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Sixe369e582018-06-26 08:46:48 +02001396
1397/**
1398 * ofnode_write_prop() - Set a property of a ofnode
1399 *
Simon Glass0b58eaa2022-09-06 20:27:32 -06001400 * Note that if @copy is false, the value passed to the function is *not*
1401 * allocated by the function itself, but must be allocated by the caller if
1402 * necessary. However it does allocate memory for the property struct and name.
Mario Sixe369e582018-06-26 08:46:48 +02001403 *
1404 * @node: The node for whose property should be set
1405 * @propname: The name of the property to set
Mario Sixe369e582018-06-26 08:46:48 +02001406 * @value: The new value of the property (must be valid prior to calling
1407 * the function)
Simon Glassbe0789a2022-07-30 15:52:10 -06001408 * @len: The length of the new value of the property
Simon Glass0b58eaa2022-09-06 20:27:32 -06001409 * @copy: true to allocate memory for the value. This only has any effect with
1410 * live tree, since flat tree handles this automatically. It allows a
1411 * node's value to be written to the tree, without requiring that the
1412 * caller allocate it
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001413 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001414 */
Simon Glassbe0789a2022-07-30 15:52:10 -06001415int ofnode_write_prop(ofnode node, const char *propname, const void *value,
Simon Glass0b58eaa2022-09-06 20:27:32 -06001416 int len, bool copy);
Mario Sixe369e582018-06-26 08:46:48 +02001417
1418/**
1419 * ofnode_write_string() - Set a string property of a ofnode
1420 *
1421 * Note that the value passed to the function is *not* allocated by the
1422 * function itself, but must be allocated by the caller if necessary.
1423 *
1424 * @node: The node for whose string property should be set
1425 * @propname: The name of the string property to set
1426 * @value: The new value of the string property (must be valid prior to
1427 * calling the function)
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001428 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001429 */
1430int ofnode_write_string(ofnode node, const char *propname, const char *value);
1431
1432/**
Simon Glass55f79902022-07-30 15:52:14 -06001433 * ofnode_write_u32() - Set an integer property of an ofnode
1434 *
1435 * @node: The node for whose string property should be set
1436 * @propname: The name of the string property to set
1437 * @value: The new value of the 32-bit integer property
1438 * Return: 0 if successful, -ve on error
1439 */
1440int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1441
1442/**
Mario Sixe369e582018-06-26 08:46:48 +02001443 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1444 * ofnode
1445 *
1446 * This function effectively sets the node's "status" property to either "okay"
1447 * or "disable", hence making it available for driver model initialization or
1448 * not.
1449 *
1450 * @node: The node to enable
1451 * @value: Flag that tells the function to either disable or enable the
1452 * node
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001453 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001454 */
1455int ofnode_set_enabled(ofnode node, bool value);
1456
Simon Glass7de8bd02021-08-07 07:24:01 -06001457/**
Sean Anderson8b52f232022-03-28 18:14:37 -04001458 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1459 *
1460 * This function parses PHY handle from the Ethernet controller's ofnode
1461 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1462 *
1463 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1464 * if the result to that is true, this function should not be called.
1465 *
1466 * @eth_node: ofnode belonging to the Ethernet controller
1467 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1468 */
1469ofnode ofnode_get_phy_node(ofnode eth_node);
1470
1471/**
1472 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1473 *
1474 * This function parses the "phy-mode" / "phy-connection-type" property and
1475 * returns the corresponding PHY interface type.
1476 *
1477 * @mac_node: ofnode containing the property
1478 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1479 * error
1480 */
1481phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1482
1483#if CONFIG_IS_ENABLED(DM)
1484/**
Simon Glass7de8bd02021-08-07 07:24:01 -06001485 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1486 *
1487 * This reads a property from the /config node of the devicetree.
1488 *
Simon Glass988f1462022-09-06 20:27:28 -06001489 * This only works with the control FDT.
1490 *
1491 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass7de8bd02021-08-07 07:24:01 -06001492 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001493 * @prop_name: property name to look up
1494 * Return: true, if it exists, false if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001495 */
1496bool ofnode_conf_read_bool(const char *prop_name);
1497
1498/**
1499 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1500 *
1501 * This reads a property from the /config node of the devicetree.
1502 *
Simon Glass988f1462022-09-06 20:27:28 -06001503 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass7de8bd02021-08-07 07:24:01 -06001504 *
1505 * @prop_name: property name to look up
1506 * @default_val: default value to return if the property is not found
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001507 * Return: integer value, if found, or @default_val if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001508 */
1509int ofnode_conf_read_int(const char *prop_name, int default_val);
1510
1511/**
1512 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1513 *
1514 * This reads a property from the /config node of the devicetree.
1515 *
Simon Glass988f1462022-09-06 20:27:28 -06001516 * This only works with the control FDT.
1517 *
1518 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass7de8bd02021-08-07 07:24:01 -06001519 *
1520 * @prop_name: property name to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001521 * Return: string value, if found, or NULL if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001522 */
1523const char *ofnode_conf_read_str(const char *prop_name);
1524
Michal Simekdb5e3492023-08-31 08:59:05 +02001525/**
1526 * ofnode_read_bootscript_address() - Read bootscr-address or bootscr-ram-offset
1527 *
1528 * @bootscr_address: pointer to 64bit address where bootscr-address property value
1529 * is stored
1530 * @bootscr_offset: pointer to 64bit offset address where bootscr-ram-offset
1531 * property value is stored
1532 *
1533 * This reads a bootscr-address or bootscr-ram-offset property from
1534 * the /options/u-boot/ node of the devicetree. bootscr-address holds the full
1535 * address of the boot script file. bootscr-ram-offset holds the boot script
1536 * file offset from the start of the ram base address. When bootscr-address is
1537 * defined, bootscr-ram-offset property is ignored.
1538 *
1539 * This only works with the control FDT.
1540 *
1541 * Return: 0 if OK, -EINVAL if property is not found.
1542 */
1543int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset);
1544
Michal Simek44f35e12023-08-31 09:04:27 +02001545/**
1546 * ofnode_read_bootscript_flash() - Read bootscr-flash-offset/size
1547 *
1548 * @bootscr_flash_offset: pointer to 64bit offset where bootscr-flash-offset
1549 * property value is stored
1550 * @bootscr_flash_size: pointer to 64bit size where bootscr-flash-size property
1551 * value is stored
1552 *
1553 * This reads a bootscr-flash-offset and bootscr-flash-size properties from
1554 * the /options/u-boot/ node of the devicetree. bootscr-flash-offset holds
1555 * the offset of the boot script file from start of flash. bootscr-flash-size
1556 * holds the boot script size in flash. When bootscr-flash-size is not defined,
1557 * bootscr-flash-offset property is cleaned.
1558 *
1559 * This only works with the control FDT.
1560 *
1561 * Return: 0 if OK, -EINVAL if property is not found or incorrect.
1562 */
1563int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
1564 u64 *bootscr_flash_size);
1565
Sean Anderson8b52f232022-03-28 18:14:37 -04001566#else /* CONFIG_DM */
1567static inline bool ofnode_conf_read_bool(const char *prop_name)
1568{
1569 return false;
1570}
Marek Behúnf3dd2132022-04-07 00:32:57 +02001571
Sean Anderson8b52f232022-03-28 18:14:37 -04001572static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1573{
1574 return default_val;
1575}
1576
1577static inline const char *ofnode_conf_read_str(const char *prop_name)
1578{
1579 return NULL;
1580}
Simon Glassffe90392022-09-06 20:27:02 -06001581
Michal Simekdb5e3492023-08-31 08:59:05 +02001582static inline int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset)
1583{
1584 return -EINVAL;
1585}
1586
Michal Simek44f35e12023-08-31 09:04:27 +02001587static inline int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
1588 u64 *bootscr_flash_size)
1589{
1590 return -EINVAL;
1591}
1592
Sean Anderson8b52f232022-03-28 18:14:37 -04001593#endif /* CONFIG_DM */
Marek Behún123ca112022-04-07 00:33:01 +02001594
Simon Glassffe90392022-09-06 20:27:02 -06001595/**
1596 * of_add_subnode() - add a new subnode to a node
1597 *
1598 * @parent: parent node to add to
1599 * @name: name of subnode
1600 * @nodep: returns pointer to new subnode (valid if the function returns 0
1601 * or -EEXIST)
1602 * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
1603 * -ve on other error
1604 */
1605int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);
1606
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001607/**
1608 * ofnode_copy_props() - copy all properties from one node to another
1609 *
Simon Glass24797092023-09-26 08:14:37 -06001610 * Makes a copy of all properties from the source node to the destination node.
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001611 * Existing properties in the destination node remain unchanged, except that
1612 * any with the same name are overwritten, including changing the size of the
1613 * property.
1614 *
1615 * For livetree, properties are copied / allocated, so the source tree does not
1616 * need to be present afterwards.
1617 *
Simon Glass24797092023-09-26 08:14:37 -06001618 * @dst: Destination node to write properties to
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001619 * @src: Source node to read properties from
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001620 */
Simon Glass24797092023-09-26 08:14:37 -06001621int ofnode_copy_props(ofnode dst, ofnode src);
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001622
Simon Glassc15862f2023-09-26 08:14:41 -06001623/**
1624 * ofnode_copy_node() - Copy a node to another place
1625 *
1626 * If a node with this name already exists in dst_parent, this returns an
1627 * .error
1628 *
1629 * @dst_parent: Parent of the newly copied node
1630 * @name: Name to give the new node
1631 * @src: Source node to copy
1632 * @nodep: Returns the new node, or the existing node if there is one
1633 * Return: 0 if OK, -EEXIST if dst_parent already has a node with this parent
1634 */
1635int ofnode_copy_node(ofnode dst_parent, const char *name, ofnode src,
1636 ofnode *nodep);
1637
Simon Glass4984de22017-05-17 17:18:10 -06001638#endif