blob: f6085231bbd87d32788586691d0ef3557f072042 [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>
Simon Glass4984de22017-05-17 17:18:10 -060023
Simon Glass9e512042017-05-18 20:08:58 -060024struct ofnode_phandle_args {
25 ofnode node;
26 int args_count;
27 uint32_t args[OF_MAX_PHANDLE_ARGS];
28};
29
30/**
Stefan Roese45dbe752020-09-23 08:23:27 +020031 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glass9e512042017-05-18 20:08:58 -060032 *
33 * This cannot be called if the reference contains an offset.
34 *
35 * @node: Reference containing struct device_node * (possibly invalid)
Patrick Delaunaybe74f712022-01-12 10:53:49 +010036 * Return: pointer to device node (can be NULL)
Simon Glass9e512042017-05-18 20:08:58 -060037 */
38static inline const struct device_node *ofnode_to_np(ofnode node)
39{
40#ifdef OF_CHECKS
41 if (!of_live_active())
42 return NULL;
43#endif
44 return node.np;
45}
46
Simon Glass4984de22017-05-17 17:18:10 -060047/**
Simon Glass39e42be2022-07-30 15:52:13 -060048 * ofnode_to_npw() - convert an ofnode to a writeable live DT node pointer
49 *
50 * This cannot be called if the reference contains an offset.
51 *
52 * @node: Reference containing struct device_node * (possibly invalid)
53 * Return: pointer to device node (can be NULL)
54 */
55static inline struct device_node *ofnode_to_npw(ofnode node)
56{
57#ifdef OF_CHECKS
58 if (!of_live_active())
59 return NULL;
60#endif
61 /* Drop constant */
62 return (struct device_node *)node.np;
63}
64
65/**
Simon Glass4984de22017-05-17 17:18:10 -060066 * ofnode_to_offset() - convert an ofnode to a flat DT offset
67 *
68 * This cannot be called if the reference contains a node pointer.
69 *
70 * @node: Reference containing offset (possibly invalid)
Patrick Delaunaybe74f712022-01-12 10:53:49 +010071 * Return: DT offset (can be -1)
Simon Glass4984de22017-05-17 17:18:10 -060072 */
73static inline int ofnode_to_offset(ofnode node)
74{
Simon Glass9e512042017-05-18 20:08:58 -060075#ifdef OF_CHECKS
76 if (of_live_active())
77 return -1;
78#endif
Simon Glass4984de22017-05-17 17:18:10 -060079 return node.of_offset;
80}
81
82/**
83 * ofnode_valid() - check if an ofnode is valid
84 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +010085 * @node: Reference containing offset (possibly invalid)
86 * Return: true if the reference contains a valid ofnode, false if it is NULL
Simon Glass4984de22017-05-17 17:18:10 -060087 */
88static inline bool ofnode_valid(ofnode node)
89{
Simon Glass9e512042017-05-18 20:08:58 -060090 if (of_live_active())
91 return node.np != NULL;
92 else
Patrick Delaunay6d9949f2020-09-24 17:26:20 +020093 return node.of_offset >= 0;
Simon Glass4984de22017-05-17 17:18:10 -060094}
95
96/**
97 * offset_to_ofnode() - convert a DT offset to an ofnode
98 *
99 * @of_offset: DT offset (either valid, or -1)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100100 * Return: reference to the associated DT offset
Simon Glass4984de22017-05-17 17:18:10 -0600101 */
102static inline ofnode offset_to_ofnode(int of_offset)
103{
104 ofnode node;
105
Simon Glass9e512042017-05-18 20:08:58 -0600106 if (of_live_active())
107 node.np = NULL;
108 else
Simon Glassb14c5332019-12-06 21:41:36 -0700109 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass4984de22017-05-17 17:18:10 -0600110
111 return node;
112}
113
114/**
Simon Glass9e512042017-05-18 20:08:58 -0600115 * np_to_ofnode() - convert a node pointer to an ofnode
116 *
117 * @np: Live node pointer (can be NULL)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100118 * Return: reference to the associated node pointer
Simon Glass9e512042017-05-18 20:08:58 -0600119 */
120static inline ofnode np_to_ofnode(const struct device_node *np)
121{
122 ofnode node;
123
124 node.np = np;
125
126 return node;
127}
128
129/**
130 * ofnode_is_np() - check if a reference is a node pointer
131 *
132 * This function associated that if there is a valid live tree then all
133 * references will use it. This is because using the flat DT when the live tree
134 * is valid is not permitted.
135 *
136 * @node: reference to check (possibly invalid)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100137 * Return: true if the reference is a live node pointer, false if it is a DT
Simon Glass9e512042017-05-18 20:08:58 -0600138 * offset
139 */
140static inline bool ofnode_is_np(ofnode node)
141{
142#ifdef OF_CHECKS
143 /*
144 * Check our assumption that flat tree offsets are not used when a
145 * live tree is in use.
146 */
147 assert(!ofnode_valid(node) ||
Stefan Roese45dbe752020-09-23 08:23:27 +0200148 (of_live_active() ? ofnode_to_np(node)
149 : ofnode_to_np(node)));
Simon Glass9e512042017-05-18 20:08:58 -0600150#endif
151 return of_live_active() && ofnode_valid(node);
152}
153
154/**
Simon Glass4984de22017-05-17 17:18:10 -0600155 * ofnode_equal() - check if two references are equal
156 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100157 * @ref1: first reference to check (possibly invalid)
158 * @ref2: second reference to check (possibly invalid)
159 * Return: true if equal, else false
Simon Glass4984de22017-05-17 17:18:10 -0600160 */
161static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
162{
163 /* We only need to compare the contents */
164 return ref1.of_offset == ref2.of_offset;
165}
166
Simon Glass9e512042017-05-18 20:08:58 -0600167/**
168 * ofnode_null() - Obtain a null ofnode
169 *
170 * This returns an ofnode which points to no node. It works both with the flat
171 * tree and livetree.
172 */
173static inline ofnode ofnode_null(void)
174{
175 ofnode node;
176
177 if (of_live_active())
178 node.np = NULL;
179 else
180 node.of_offset = -1;
181
182 return node;
183}
184
Simon Glassd0c20ce2020-11-28 17:50:07 -0700185static inline ofnode ofnode_root(void)
186{
187 ofnode node;
188
189 if (of_live_active())
190 node.np = gd_of_root();
191 else
192 node.of_offset = 0;
193
194 return node;
195}
196
Simon Glass9e512042017-05-18 20:08:58 -0600197/**
Simon Glass33104842022-07-30 15:52:08 -0600198 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
199 *
200 * Returns: reference to the control FDT
201 */
202static inline oftree oftree_default(void)
203{
204 oftree tree;
205
206 if (of_live_active())
207 tree.np = gd_of_root();
208 else
209 tree.fdt = (void *)gd->fdt_blob;
210
211 return tree;
212}
213
214/**
Kishon Vijay Abraham I77cbaf82021-07-21 21:28:30 +0530215 * ofnode_name_eq() - Check if the node name is equivalent to a given name
216 * ignoring the unit address
217 *
218 * @node: valid node reference that has to be compared
219 * @name: name that has to be compared with the node name
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100220 * Return: true if matches, false if it doesn't match
Kishon Vijay Abraham I77cbaf82021-07-21 21:28:30 +0530221 */
222bool ofnode_name_eq(ofnode node, const char *name);
223
224/**
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +0200225 * ofnode_read_u8() - Read a 8-bit integer from a property
226 *
227 * @node: valid node reference to read property from
228 * @propname: name of the property to read from
229 * @outp: place to put value (if found)
230 * Return: 0 if OK, -ve on error
231 */
232int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
233
234/**
235 * ofnode_read_u8_default() - Read a 8-bit integer from a property
236 *
237 * @node: valid node reference to read property from
238 * @propname: name of the property to read from
239 * @def: default value to return if the property has no value
240 * Return: property value, or @def if not found
241 */
242u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
243
244/**
245 * ofnode_read_u16() - Read a 16-bit integer from a property
246 *
247 * @node: valid node reference to read property from
248 * @propname: name of the property to read from
249 * @outp: place to put value (if found)
250 * Return: 0 if OK, -ve on error
251 */
252int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
253
254/**
255 * ofnode_read_u16_default() - Read a 16-bit integer from a property
256 *
257 * @node: valid node reference to read property from
258 * @propname: name of the property to read from
259 * @def: default value to return if the property has no value
260 * Return: property value, or @def if not found
261 */
262u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
263
264/**
Simon Glass9e512042017-05-18 20:08:58 -0600265 * ofnode_read_u32() - Read a 32-bit integer from a property
266 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100267 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600268 * @propname: name of the property to read from
269 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100270 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600271 */
272int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
273
274/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200275 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
276 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100277 * @node: valid node reference to read property from
Dario Binacchi4bb70752020-03-29 18:04:41 +0200278 * @propname: name of the property to read from
279 * @index: index of the integer to return
280 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100281 * Return: 0 if OK, -ve on error
Dario Binacchi4bb70752020-03-29 18:04:41 +0200282 */
283int ofnode_read_u32_index(ofnode node, const char *propname, int index,
284 u32 *outp);
285
286/**
Simon Glass9e512042017-05-18 20:08:58 -0600287 * ofnode_read_s32() - Read a 32-bit integer from a property
288 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100289 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600290 * @propname: name of the property to read from
291 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100292 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600293 */
294static inline int ofnode_read_s32(ofnode node, const char *propname,
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100295 s32 *outp)
Simon Glass9e512042017-05-18 20:08:58 -0600296{
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100297 return ofnode_read_u32(node, propname, (u32 *)outp);
Simon Glass9e512042017-05-18 20:08:58 -0600298}
299
300/**
301 * ofnode_read_u32_default() - Read a 32-bit integer from a property
302 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100303 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600304 * @propname: name of the property to read from
305 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100306 * Return: property value, or @def if not found
Simon Glass9e512042017-05-18 20:08:58 -0600307 */
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100308u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
Simon Glass9e512042017-05-18 20:08:58 -0600309
310/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200311 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
312 * property
313 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100314 * @node: valid node reference to read property from
Dario Binacchi4bb70752020-03-29 18:04:41 +0200315 * @propname: name of the property to read from
316 * @index: index of the integer to return
317 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100318 * Return: property value, or @def if not found
Dario Binacchi4bb70752020-03-29 18:04:41 +0200319 */
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100320u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
Dario Binacchi4bb70752020-03-29 18:04:41 +0200321 u32 def);
322
323/**
Simon Glass9e512042017-05-18 20:08:58 -0600324 * ofnode_read_s32_default() - Read a 32-bit integer from a property
325 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100326 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600327 * @propname: name of the property to read from
328 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100329 * Return: property value, or @def if not found
Simon Glass9e512042017-05-18 20:08:58 -0600330 */
331int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
332
333/**
Lukas Auerafb30122018-11-22 11:26:35 +0100334 * ofnode_read_u64() - Read a 64-bit integer from a property
335 *
336 * @node: valid node reference to read property from
337 * @propname: name of the property to read from
338 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100339 * Return: 0 if OK, -ve on error
Lukas Auerafb30122018-11-22 11:26:35 +0100340 */
341int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
342
343/**
Simon Glass7e5196c2018-06-11 13:07:10 -0600344 * ofnode_read_u64_default() - Read a 64-bit integer from a property
345 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100346 * @node: valid node reference to read property from
Simon Glass7e5196c2018-06-11 13:07:10 -0600347 * @propname: name of the property to read from
348 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100349 * Return: property value, or @def if not found
Simon Glass7e5196c2018-06-11 13:07:10 -0600350 */
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200351u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass7e5196c2018-06-11 13:07:10 -0600352
353/**
Simon Glassa8167d82020-01-27 08:49:44 -0700354 * ofnode_read_prop() - Read a property from a node
355 *
356 * @node: valid node reference to read property from
357 * @propname: name of the property to read
358 * @sizep: if non-NULL, returns the size of the property, or an error code
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100359 * if not found
360 * Return: property value, or NULL if there is no such property
Simon Glassa8167d82020-01-27 08:49:44 -0700361 */
362const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
363
364/**
Simon Glass9e512042017-05-18 20:08:58 -0600365 * ofnode_read_string() - Read a string from a property
366 *
Simon Glassa8167d82020-01-27 08:49:44 -0700367 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600368 * @propname: name of the property to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100369 * Return: string from property value, or NULL if there is no such property
Simon Glass9e512042017-05-18 20:08:58 -0600370 */
371const char *ofnode_read_string(ofnode node, const char *propname);
372
373/**
Simon Glassbed77492017-05-18 20:09:01 -0600374 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glass9e512042017-05-18 20:08:58 -0600375 *
376 * @node: valid node reference to read property from
377 * @propname: name of the property to read
378 * @out_values: pointer to return value, modified only if return value is 0
379 * @sz: number of array elements to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100380 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600381 *
382 * Search for a property in a device node and read 32-bit value(s) from
383 * it. Returns 0 on success, -EINVAL if the property does not exist,
384 * -ENODATA if property does not have a value, and -EOVERFLOW if the
385 * property data isn't large enough.
386 *
387 * The out_values is modified only if a valid u32 value can be decoded.
388 */
389int ofnode_read_u32_array(ofnode node, const char *propname,
390 u32 *out_values, size_t sz);
391
392/**
393 * ofnode_read_bool() - read a boolean value from a property
394 *
395 * @node: valid node reference to read property from
396 * @propname: name of property to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100397 * Return: true if property is present (meaning true), false if not present
Simon Glass9e512042017-05-18 20:08:58 -0600398 */
399bool ofnode_read_bool(ofnode node, const char *propname);
400
401/**
402 * ofnode_find_subnode() - find a named subnode of a parent node
403 *
404 * @node: valid reference to parent node
405 * @subnode_name: name of subnode to find
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100406 * Return: reference to subnode (which can be invalid if there is no such
Simon Glass9e512042017-05-18 20:08:58 -0600407 * subnode)
408 */
409ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
410
Simon Glassec1add12020-12-16 17:25:06 -0700411#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass401d1c42020-10-30 21:38:53 -0600412#include <asm/global_data.h>
413
Simon Glassec1add12020-12-16 17:25:06 -0700414static inline bool ofnode_is_enabled(ofnode node)
415{
416 if (ofnode_is_np(node)) {
417 return of_device_is_available(ofnode_to_np(node));
418 } else {
419 return fdtdec_get_is_enabled(gd->fdt_blob,
420 ofnode_to_offset(node));
421 }
422}
423
424static inline ofnode ofnode_first_subnode(ofnode node)
425{
426 assert(ofnode_valid(node));
427 if (ofnode_is_np(node))
428 return np_to_ofnode(node.np->child);
429
430 return offset_to_ofnode(
431 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
432}
433
434static inline ofnode ofnode_next_subnode(ofnode node)
435{
436 assert(ofnode_valid(node));
437 if (ofnode_is_np(node))
438 return np_to_ofnode(node.np->sibling);
439
440 return offset_to_ofnode(
441 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
442}
443#else
444/**
445 * ofnode_is_enabled() - Checks whether a node is enabled.
446 * This looks for a 'status' property. If this exists, then returns true if
447 * the status is 'okay' and false otherwise. If there is no status property,
448 * it returns true on the assumption that anything mentioned should be enabled
449 * by default.
450 *
451 * @node: node to examine
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100452 * Return: false (not enabled) or true (enabled)
Simon Glassec1add12020-12-16 17:25:06 -0700453 */
454bool ofnode_is_enabled(ofnode node);
455
Simon Glass9e512042017-05-18 20:08:58 -0600456/**
457 * ofnode_first_subnode() - find the first subnode of a parent node
458 *
459 * @node: valid reference to a valid parent node
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100460 * Return: reference to the first subnode (which can be invalid if the parent
Simon Glass9e512042017-05-18 20:08:58 -0600461 * node has no subnodes)
462 */
463ofnode ofnode_first_subnode(ofnode node);
464
465/**
466 * ofnode_next_subnode() - find the next sibling of a subnode
467 *
468 * @node: valid reference to previous node (sibling)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100469 * Return: reference to the next subnode (which can be invalid if the node
Simon Glass9e512042017-05-18 20:08:58 -0600470 * has no more siblings)
471 */
472ofnode ofnode_next_subnode(ofnode node);
Simon Glassec1add12020-12-16 17:25:06 -0700473#endif /* DM_INLINE_OFNODE */
Simon Glass9e512042017-05-18 20:08:58 -0600474
475/**
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100476 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
477 *
478 * @node: valid node to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100479 * Return: ofnode reference of the parent node
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100480 */
481ofnode ofnode_get_parent(ofnode node);
482
483/**
Simon Glass9e512042017-05-18 20:08:58 -0600484 * ofnode_get_name() - get the name of a node
485 *
486 * @node: valid node to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100487 * Return: name of node
Simon Glass9e512042017-05-18 20:08:58 -0600488 */
489const char *ofnode_get_name(ofnode node);
490
491/**
Marek Behún0e116be2021-05-26 14:08:18 +0200492 * ofnode_get_path() - get the full path of a node
493 *
494 * @node: valid node to look up
495 * @buf: buffer to write the node path into
496 * @buflen: buffer size
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100497 * Return: 0 if OK, -ve on error
Marek Behún0e116be2021-05-26 14:08:18 +0200498 */
499int ofnode_get_path(ofnode node, char *buf, int buflen);
500
501/**
Kever Yangb4f20762018-02-23 17:38:50 +0100502 * ofnode_get_by_phandle() - get ofnode from phandle
503 *
504 * @phandle: phandle to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100505 * Return: ofnode reference to the phandle
Kever Yangb4f20762018-02-23 17:38:50 +0100506 */
507ofnode ofnode_get_by_phandle(uint phandle);
508
509/**
Simon Glass9e512042017-05-18 20:08:58 -0600510 * ofnode_read_size() - read the size of a property
511 *
512 * @node: node to check
513 * @propname: property to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100514 * Return: size of property if present, or -EINVAL if not
Simon Glass9e512042017-05-18 20:08:58 -0600515 */
516int ofnode_read_size(ofnode node, const char *propname);
517
518/**
Keerthye679d032019-04-24 17:19:53 +0530519 * ofnode_get_addr_size_index() - get an address/size from a node
520 * based on index
521 *
522 * This reads the register address/size from a node based on index
523 *
524 * @node: node to read from
525 * @index: Index of address to read (0 for first)
526 * @size: Pointer to size of the address
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100527 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Keerthye679d032019-04-24 17:19:53 +0530528 */
529phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
530 fdt_size_t *size);
531
532/**
Marek Behún31a7b712021-05-26 14:08:17 +0200533 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
534 * based on index, without address
535 * translation
536 *
537 * This reads the register address/size from a node based on index.
538 * The resulting address is not translated. Useful for example for on-disk
539 * addresses.
540 *
541 * @node: node to read from
542 * @index: Index of address to read (0 for first)
543 * @size: Pointer to size of the address
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100544 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Marek Behún31a7b712021-05-26 14:08:17 +0200545 */
546phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
547 fdt_size_t *size);
548
549/**
Simon Glassbed77492017-05-18 20:09:01 -0600550 * ofnode_get_addr_index() - get an address from a node
551 *
552 * This reads the register address from a node
553 *
554 * @node: node to read from
555 * @index: Index of address to read (0 for first)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100556 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glassbed77492017-05-18 20:09:01 -0600557 */
558phys_addr_t ofnode_get_addr_index(ofnode node, int index);
559
560/**
561 * ofnode_get_addr() - get an address from a node
562 *
563 * This reads the register address from a node
564 *
565 * @node: node to read from
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100566 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glassbed77492017-05-18 20:09:01 -0600567 */
568phys_addr_t ofnode_get_addr(ofnode node);
569
570/**
Chen Guanqiaoaa351a12021-04-12 14:51:11 +0800571 * ofnode_get_size() - get size from a node
572 *
573 * This reads the register size from a node
574 *
575 * @node: node to read from
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100576 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
Chen Guanqiaoaa351a12021-04-12 14:51:11 +0800577 */
578fdt_size_t ofnode_get_size(ofnode node);
579
580/**
Simon Glass9e512042017-05-18 20:08:58 -0600581 * ofnode_stringlist_search() - find a string in a string list and return index
582 *
583 * Note that it is possible for this function to succeed on property values
584 * that are not NUL-terminated. That's because the function will stop after
585 * finding the first occurrence of @string. This can for example happen with
586 * small-valued cell properties, such as #address-cells, when searching for
587 * the empty string.
588 *
589 * @node: node to check
590 * @propname: name of the property containing the string list
591 * @string: string to look up in the string list
592 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100593 * Return:
Simon Glass9e512042017-05-18 20:08:58 -0600594 * the index of the string in the list of strings
595 * -ENODATA if the property is not found
596 * -EINVAL on some other error
597 */
598int ofnode_stringlist_search(ofnode node, const char *propname,
599 const char *string);
600
601/**
Simon Glass8c293d62017-06-12 06:21:28 -0600602 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glass9e512042017-05-18 20:08:58 -0600603 *
604 * Note that this will successfully extract strings from properties with
605 * non-NUL-terminated values. For example on small-valued cell properties
606 * this function will return the empty string.
607 *
608 * If non-NULL, the length of the string (on success) or a negative error-code
609 * (on failure) will be stored in the integer pointer to by lenp.
610 *
611 * @node: node to check
612 * @propname: name of the property containing the string list
Simon Glass32c6a8e2021-10-23 17:26:06 -0600613 * @index: index of the string to return (cannot be negative)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100614 * @outp: return location for the string
Simon Glass9e512042017-05-18 20:08:58 -0600615 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100616 * Return:
Simon Glass32c6a8e2021-10-23 17:26:06 -0600617 * 0 if found or -ve error value if not found
Simon Glass9e512042017-05-18 20:08:58 -0600618 */
619int ofnode_read_string_index(ofnode node, const char *propname, int index,
620 const char **outp);
621
622/**
Simon Glass8c293d62017-06-12 06:21:28 -0600623 * ofnode_read_string_count() - find the number of strings in a string list
624 *
625 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100626 * @property: name of the property containing the string list
627 * Return:
Simon Glass8c293d62017-06-12 06:21:28 -0600628 * number of strings in the list, or -ve error value if not found
629 */
630int ofnode_read_string_count(ofnode node, const char *property);
631
632/**
Simon Glass075bfc92021-10-23 17:26:07 -0600633 * ofnode_read_string_list() - read a list of strings
634 *
635 * This produces a list of string pointers with each one pointing to a string
636 * in the string list. If the property does not exist, it returns {NULL}.
637 *
638 * The data is allocated and the caller is reponsible for freeing the return
639 * value (the list of string pointers). The strings themselves may not be
640 * changed as they point directly into the devicetree property.
641 *
642 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100643 * @property: name of the property containing the string list
Simon Glass075bfc92021-10-23 17:26:07 -0600644 * @listp: returns an allocated, NULL-terminated list of strings if the return
645 * value is > 0, else is set to NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100646 * Return:
647 * number of strings in list, 0 if none, -ENOMEM if out of memory,
648 * -EINVAL if no such property, -EENODATA if property is empty
Simon Glass075bfc92021-10-23 17:26:07 -0600649 */
650int ofnode_read_string_list(ofnode node, const char *property,
651 const char ***listp);
652
653/**
Simon Glass9e512042017-05-18 20:08:58 -0600654 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
655 *
656 * This function is useful to parse lists of phandles and their arguments.
657 * Returns 0 on success and fills out_args, on error returns appropriate
658 * errno value.
659 *
660 * Caller is responsible to call of_node_put() on the returned out_args->np
661 * pointer.
662 *
663 * Example:
664 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100665 * .. code-block::
Simon Glass9e512042017-05-18 20:08:58 -0600666 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100667 * phandle1: node1 {
668 * #list-cells = <2>;
669 * };
670 * phandle2: node2 {
671 * #list-cells = <1>;
672 * };
673 * node3 {
674 * list = <&phandle1 1 2 &phandle2 3>;
675 * };
Simon Glass9e512042017-05-18 20:08:58 -0600676 *
677 * To get a device_node of the `node2' node you may call this:
678 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
679 *
680 * @node: device tree node containing a list
681 * @list_name: property name that contains a list
682 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100683 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glass9e512042017-05-18 20:08:58 -0600684 * @index: index of a phandle to parse out
685 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100686 * Return:
687 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
688 * @list_name does not exist, -EINVAL if a phandle was not found,
689 * @cells_name could not be found, the arguments were truncated or there
690 * were too many arguments.
Simon Glass9e512042017-05-18 20:08:58 -0600691 */
692int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
693 const char *cells_name, int cell_count,
694 int index,
695 struct ofnode_phandle_args *out_args);
696
697/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200698 * ofnode_count_phandle_with_args() - Count number of phandle in a list
699 *
700 * This function is useful to count phandles into a list.
701 * Returns number of phandle on success, on error returns appropriate
702 * errno value.
703 *
704 * @node: device tree node containing a list
705 * @list_name: property name that contains a list
706 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100707 * @cell_count: Cell count to use if @cells_name is NULL
708 * Return:
709 * number of phandle on success, -ENOENT if @list_name does not exist,
710 * -EINVAL if a phandle was not found, @cells_name could not be found.
Patrice Chotard642346a2017-07-18 11:57:08 +0200711 */
712int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200713 const char *cells_name, int cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +0200714
715/**
Simon Glass9e512042017-05-18 20:08:58 -0600716 * ofnode_path() - find a node by full path
717 *
Simon Glass33104842022-07-30 15:52:08 -0600718 * This uses the control FDT.
719 *
Simon Glass9e512042017-05-18 20:08:58 -0600720 * @path: Full path to node, e.g. "/bus/spi@1"
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100721 * Return: reference to the node found. Use ofnode_valid() to check if it exists
Simon Glass9e512042017-05-18 20:08:58 -0600722 */
723ofnode ofnode_path(const char *path);
724
725/**
Simon Glass33104842022-07-30 15:52:08 -0600726 * ofnode_path_root() - find a node by full path from a root node
727 *
728 * @tree: Device tree to use
729 * @path: Full path to node, e.g. "/bus/spi@1"
730 * Return: reference to the node found. Use ofnode_valid() to check if it exists
731 */
732ofnode ofnode_path_root(oftree tree, const char *path);
733
734/**
Simon Glassbd933bf2020-01-27 08:49:46 -0700735 * ofnode_read_chosen_prop() - get the value of a chosen property
736 *
737 * This looks for a property within the /chosen node and returns its value
738 *
739 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100740 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Simon Glassbd933bf2020-01-27 08:49:46 -0700741 * returns NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100742 * Return: property value if found, else NULL
Simon Glassbd933bf2020-01-27 08:49:46 -0700743 */
744const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
745
746/**
Simon Glass14ca9f72020-01-27 08:49:43 -0700747 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glass9e512042017-05-18 20:08:58 -0600748 *
Simon Glass14ca9f72020-01-27 08:49:43 -0700749 * This looks for a property within the /chosen node and returns its value,
750 * checking that it is a valid nul-terminated string
Simon Glass9e512042017-05-18 20:08:58 -0600751 *
752 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100753 * Return: string value if found, else NULL
Simon Glass9e512042017-05-18 20:08:58 -0600754 */
Simon Glass14ca9f72020-01-27 08:49:43 -0700755const char *ofnode_read_chosen_string(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600756
757/**
Simon Glass74d594a2020-01-27 08:49:42 -0700758 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glass9e512042017-05-18 20:08:58 -0600759 *
Simon Glass74d594a2020-01-27 08:49:42 -0700760 * This looks up a named property in the chosen node and uses that as a path to
761 * look up a code.
762 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100763 * @propname: Property name to look for
764 * Return: the referenced node if present, else ofnode_null()
Simon Glass9e512042017-05-18 20:08:58 -0600765 */
Simon Glass74d594a2020-01-27 08:49:42 -0700766ofnode ofnode_get_chosen_node(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600767
Michal Simek305d3182020-07-28 12:51:08 +0200768/**
769 * ofnode_read_aliases_prop() - get the value of a aliases property
770 *
771 * This looks for a property within the /aliases node and returns its value
772 *
773 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100774 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Michal Simek305d3182020-07-28 12:51:08 +0200775 * returns NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100776 * Return: property value if found, else NULL
Michal Simek305d3182020-07-28 12:51:08 +0200777 */
778const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
779
780/**
781 * ofnode_get_aliases_node() - get a referenced node from the aliases node
782 *
783 * This looks up a named property in the aliases node and uses that as a path to
784 * look up a code.
785 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100786 * @propname: Property name to look for
787 * Return: the referenced node if present, else ofnode_null()
Michal Simek305d3182020-07-28 12:51:08 +0200788 */
789ofnode ofnode_get_aliases_node(const char *propname);
790
Simon Glass9e512042017-05-18 20:08:58 -0600791struct display_timing;
792/**
793 * ofnode_decode_display_timing() - decode display timings
794 *
795 * Decode display timings from the supplied 'display-timings' node.
796 * See doc/device-tree-bindings/video/display-timing.txt for binding
797 * information.
798 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100799 * @node: 'display-timing' node containing the timing subnodes
800 * @index: Index number to read (0=first timing subnode)
801 * @config: Place to put timings
802 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Simon Glass9e512042017-05-18 20:08:58 -0600803 */
804int ofnode_decode_display_timing(ofnode node, int index,
805 struct display_timing *config);
806
807/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100808 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glass9e512042017-05-18 20:08:58 -0600809 *
810 * @node: node to read
811 * @propname: property to read
812 * @lenp: place to put length on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100813 * Return: pointer to property, or NULL if not found
Simon Glass9e512042017-05-18 20:08:58 -0600814 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900815const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glass9e512042017-05-18 20:08:58 -0600816
817/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100818 * ofnode_get_first_property()- get the reference of the first property
819 *
820 * Get reference to the first property of the node, it is used to iterate
821 * and read all the property with ofnode_get_property_by_prop().
822 *
823 * @node: node to read
824 * @prop: place to put argument reference
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100825 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100826 */
827int ofnode_get_first_property(ofnode node, struct ofprop *prop);
828
829/**
830 * ofnode_get_next_property() - get the reference of the next property
831 *
832 * Get reference to the next property of the node, it is used to iterate
833 * and read all the property with ofnode_get_property_by_prop().
834 *
835 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100836 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100837 */
838int ofnode_get_next_property(struct ofprop *prop);
839
840/**
841 * ofnode_get_property_by_prop() - get a pointer to the value of a property
842 *
843 * Get value for the property identified by the provided reference.
844 *
845 * @prop: reference on property
846 * @propname: If non-NULL, place to property name on success,
847 * @lenp: If non-NULL, place to put length on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100848 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100849 */
850const void *ofnode_get_property_by_prop(const struct ofprop *prop,
851 const char **propname, int *lenp);
852
853/**
Simon Glass9e512042017-05-18 20:08:58 -0600854 * ofnode_is_available() - check if a node is marked available
855 *
856 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100857 * Return: true if node's 'status' property is "okay" (or is missing)
Simon Glass9e512042017-05-18 20:08:58 -0600858 */
859bool ofnode_is_available(ofnode node);
860
861/**
862 * ofnode_get_addr_size() - get address and size from a property
863 *
864 * This does no address translation. It simply reads an property that contains
865 * an address and a size value, one after the other.
866 *
867 * @node: node to read from
868 * @propname: property to read
869 * @sizep: place to put size value (on success)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100870 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glass9e512042017-05-18 20:08:58 -0600871 */
872phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
873 phys_size_t *sizep);
874
875/**
876 * ofnode_read_u8_array_ptr() - find an 8-bit array
877 *
878 * Look up a property in a node and return a pointer to its contents as a
879 * byte array of given length. The property must have at least enough data
880 * for the array (count bytes). It may have more, but this will be ignored.
881 * The data is not copied.
882 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100883 * @node: node to examine
884 * @propname: name of property to find
885 * @sz: number of array elements
886 * Return:
887 * pointer to byte array if found, or NULL if the property is not found or
888 * there is not enough data
Simon Glass9e512042017-05-18 20:08:58 -0600889 */
890const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
891 size_t sz);
892
893/**
894 * ofnode_read_pci_addr() - look up a PCI address
895 *
896 * Look at an address property in a node and return the PCI address which
897 * corresponds to the given type in the form of fdt_pci_addr.
898 * The property must hold one fdt_pci_addr with a lengh.
899 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100900 * @node: node to examine
901 * @type: pci address type (FDT_PCI_SPACE_xxx)
902 * @propname: name of property to find
903 * @addr: returns pci address in the form of fdt_pci_addr
904 * Return:
905 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
906 * format of the property was invalid, -ENXIO if the requested
907 * address type was not found
Simon Glass9e512042017-05-18 20:08:58 -0600908 */
909int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
910 const char *propname, struct fdt_pci_addr *addr);
911
912/**
Bin Meng7b9cbad2018-08-03 01:14:35 -0700913 * ofnode_read_pci_vendev() - look up PCI vendor and device id
914 *
915 * Look at the compatible property of a device node that represents a PCI
916 * device and extract pci vendor id and device id from it.
917 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100918 * @node: node to examine
919 * @vendor: vendor id of the pci device
920 * @device: device id of the pci device
921 * Return: 0 if ok, negative on error
Bin Meng7b9cbad2018-08-03 01:14:35 -0700922 */
923int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
924
925/**
Michal Simekdb681d42022-02-23 15:45:40 +0100926 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
927 *
928 * Look at the compatible property of a device node that represents a eth phy
929 * device and extract phy vendor id and device id from it.
930 *
Heinrich Schuchardtd23f2902022-03-24 16:22:32 +0100931 * @node: node to examine
932 * @vendor: vendor id of the eth phy device
933 * @device: device id of the eth phy device
934 * Return: 0 if ok, negative on error
Michal Simekdb681d42022-02-23 15:45:40 +0100935 */
936int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
937
938/**
Simon Glass9e512042017-05-18 20:08:58 -0600939 * ofnode_read_addr_cells() - Get the number of address cells for a node
940 *
941 * This walks back up the tree to find the closest #address-cells property
942 * which controls the given node.
943 *
944 * @node: Node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100945 * Return: number of address cells this node uses
Simon Glass9e512042017-05-18 20:08:58 -0600946 */
947int ofnode_read_addr_cells(ofnode node);
948
949/**
950 * ofnode_read_size_cells() - Get the number of size cells for a node
951 *
952 * This walks back up the tree to find the closest #size-cells property
953 * which controls the given node.
954 *
955 * @node: Node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100956 * Return: number of size cells this node uses
Simon Glass9e512042017-05-18 20:08:58 -0600957 */
958int ofnode_read_size_cells(ofnode node);
959
960/**
Simon Glass878d68c2017-06-12 06:21:31 -0600961 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
962 *
963 * This function matches fdt_address_cells().
964 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100965 * @node: Node to check
966 * Return: value of #address-cells property in this node, or 2 if none
Simon Glass878d68c2017-06-12 06:21:31 -0600967 */
968int ofnode_read_simple_addr_cells(ofnode node);
969
970/**
971 * ofnode_read_simple_size_cells() - Get the size cells property in a node
972 *
973 * This function matches fdt_size_cells().
974 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100975 * @node: Node to check
976 * Return: value of #size-cells property in this node, or 2 if none
Simon Glass878d68c2017-06-12 06:21:31 -0600977 */
978int ofnode_read_simple_size_cells(ofnode node);
979
980/**
Simon Glass9e512042017-05-18 20:08:58 -0600981 * ofnode_pre_reloc() - check if a node should be bound before relocation
982 *
983 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
984 * via special device tree properties.
985 *
986 * Before relocation this function can be used to check if nodes are required
987 * in either SPL or TPL stages.
988 *
989 * After relocation and jumping into the real U-Boot binary it is possible to
990 * determine if a node was bound in one of SPL/TPL stages.
991 *
Patrick Delaunay54e12232019-05-21 19:19:13 +0200992 * There are 4 settings currently in use
993 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glass9e512042017-05-18 20:08:58 -0600994 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100995 * Existing platforms only use it to indicate nodes needed in
996 * SPL. Should probably be replaced by u-boot,dm-spl for new platforms.
Patrick Delaunay54e12232019-05-21 19:19:13 +0200997 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
998 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glass9e512042017-05-18 20:08:58 -0600999 *
1000 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001001 * Return: true if node is needed in SPL/TL, false otherwise
Simon Glass9e512042017-05-18 20:08:58 -06001002 */
1003bool ofnode_pre_reloc(ofnode node);
1004
Simon Glassc98ad442018-06-11 13:07:12 -06001005/**
1006 * ofnode_read_resource() - Read a resource from a node
1007 *
1008 * Read resource information from a node at the given index
1009 *
1010 * @node: Node to read from
1011 * @index: Index of resource to read (0 = first)
1012 * @res: Returns resource that was read, on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001013 * Return: 0 if OK, -ve on error
Simon Glassc98ad442018-06-11 13:07:12 -06001014 */
Simon Glassdcf98852017-07-25 08:29:55 -06001015int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassc98ad442018-06-11 13:07:12 -06001016
1017/**
1018 * ofnode_read_resource_byname() - Read a resource from a node by name
1019 *
1020 * Read resource information from a node matching the given name. This uses a
1021 * 'reg-names' string list property with the names matching the associated
1022 * 'reg' property list.
1023 *
1024 * @node: Node to read from
1025 * @name: Name of resource to read
1026 * @res: Returns resource that was read, on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001027 * Return: 0 if OK, -ve on error
Simon Glassc98ad442018-06-11 13:07:12 -06001028 */
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +09001029int ofnode_read_resource_byname(ofnode node, const char *name,
1030 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -06001031
Simon Glass3991f422017-08-05 15:45:54 -06001032/**
Simon Glassc60f6712018-06-11 13:07:13 -06001033 * ofnode_by_compatible() - Find the next compatible node
1034 *
1035 * Find the next node after @from that is compatible with @compat
1036 *
1037 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1038 * @compat: Compatible string to match
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001039 * Return: ofnode found, or ofnode_null() if none
Simon Glassc60f6712018-06-11 13:07:13 -06001040 */
1041ofnode ofnode_by_compatible(ofnode from, const char *compat);
1042
1043/**
Jens Wiklander61fba0f2018-08-20 11:09:58 +02001044 * ofnode_by_prop_value() - Find the next node with given property value
1045 *
1046 * Find the next node after @from that has a @propname with a value
1047 * @propval and a length @proplen.
1048 *
1049 * @from: ofnode to start from (use ofnode_null() to start at the
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001050 * beginning)
1051 * @propname: property name to check
1052 * @propval: property value to search for
1053 * @proplen: length of the value in propval
1054 * Return: ofnode found, or ofnode_null() if none
Jens Wiklander61fba0f2018-08-20 11:09:58 +02001055 */
1056ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1057 const void *propval, int proplen);
1058
1059/**
Simon Glass3991f422017-08-05 15:45:54 -06001060 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1061 *
1062 * @node: child node (ofnode, lvalue)
1063 * @parent: parent node (ofnode)
1064 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001065 * This is a wrapper around a for loop and is used like so::
Simon Glass3991f422017-08-05 15:45:54 -06001066 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001067 * ofnode node;
1068 * ofnode_for_each_subnode(node, parent) {
1069 * Use node
1070 * ...
1071 * }
Simon Glass3991f422017-08-05 15:45:54 -06001072 *
1073 * Note that this is implemented as a macro and @node is used as
1074 * iterator in the loop. The parent variable can be a constant or even a
1075 * literal.
1076 */
1077#define ofnode_for_each_subnode(node, parent) \
1078 for (node = ofnode_first_subnode(parent); \
1079 ofnode_valid(node); \
1080 node = ofnode_next_subnode(node))
1081
Mario Six147c6072018-01-15 11:07:19 +01001082/**
Michael Walleb8ec9452021-10-15 15:15:17 +02001083 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1084 * compatible string
1085 *
1086 * @node: child node (ofnode, lvalue)
1087 * @compat: compatible string to match
1088 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001089 * This is a wrapper around a for loop and is used like so::
Michael Walleb8ec9452021-10-15 15:15:17 +02001090 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001091 * ofnode node;
1092 * ofnode_for_each_compatible_node(node, parent, compatible) {
1093 * Use node
1094 * ...
1095 * }
Michael Walleb8ec9452021-10-15 15:15:17 +02001096 *
1097 * Note that this is implemented as a macro and @node is used as
1098 * iterator in the loop.
1099 */
1100#define ofnode_for_each_compatible_node(node, compat) \
1101 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1102 ofnode_valid(node); \
1103 node = ofnode_by_compatible(node, compat))
1104
1105/**
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001106 * ofnode_get_child_count() - get the child count of a ofnode
1107 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001108 * @parent: valid node to get its child count
1109 * Return: the number of subnodes
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001110 */
1111int ofnode_get_child_count(ofnode parent);
1112
1113/**
Fabien Dessenne641067f2019-05-31 15:11:30 +02001114 * ofnode_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +01001115 *
1116 * Translate an address from the device-tree into a CPU physical address. This
1117 * function walks up the tree and applies the various bus mappings along the
1118 * way.
1119 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001120 * @node: Device tree node giving the context in which to translate the address
Mario Six147c6072018-01-15 11:07:19 +01001121 * @in_addr: pointer to the address to translate
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001122 * Return: the translated address; OF_BAD_ADDR on error
Mario Six147c6072018-01-15 11:07:19 +01001123 */
1124u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001125
1126/**
Fabien Dessenne641067f2019-05-31 15:11:30 +02001127 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1128 *
1129 * Translate a DMA address from the device-tree into a CPU physical address.
1130 * This function walks up the tree and applies the various bus mappings along
1131 * the way.
1132 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001133 * @node: Device tree node giving the context in which to translate the
1134 * DMA address
Fabien Dessenne641067f2019-05-31 15:11:30 +02001135 * @in_addr: pointer to the DMA address to translate
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001136 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne641067f2019-05-31 15:11:30 +02001137 */
1138u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1139
1140/**
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001141 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1142 *
1143 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1144 * cpu->bus address translations
1145 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001146 * @node: Device tree node
1147 * @cpu: Pointer to variable storing the range's cpu address
1148 * @bus: Pointer to variable storing the range's bus address
1149 * @size: Pointer to variable storing the range's size
1150 * Return: translated DMA address or OF_BAD_ADDR on error
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001151 */
1152int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1153 u64 *size);
1154
1155/**
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001156 * ofnode_device_is_compatible() - check if the node is compatible with compat
1157 *
1158 * This allows to check whether the node is comaptible with the compat.
1159 *
1160 * @node: Device tree node for which compatible needs to be verified.
1161 * @compat: Compatible string which needs to verified in the given node.
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001162 * Return: true if OK, false if the compatible is not found
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001163 */
1164int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Sixe369e582018-06-26 08:46:48 +02001165
1166/**
1167 * ofnode_write_prop() - Set a property of a ofnode
1168 *
1169 * Note that the value passed to the function is *not* allocated by the
Simon Glass72b338a2022-07-30 15:52:07 -06001170 * function itself, but must be allocated by the caller if necessary. However
1171 * it does allocate memory for the property struct and name.
Mario Sixe369e582018-06-26 08:46:48 +02001172 *
1173 * @node: The node for whose property should be set
1174 * @propname: The name of the property to set
Mario Sixe369e582018-06-26 08:46:48 +02001175 * @value: The new value of the property (must be valid prior to calling
1176 * the function)
Simon Glassbe0789a2022-07-30 15:52:10 -06001177 * @len: The length of the new value of the property
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001178 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001179 */
Simon Glassbe0789a2022-07-30 15:52:10 -06001180int ofnode_write_prop(ofnode node, const char *propname, const void *value,
1181 int len);
Mario Sixe369e582018-06-26 08:46:48 +02001182
1183/**
1184 * ofnode_write_string() - Set a string property of a ofnode
1185 *
1186 * Note that the value passed to the function is *not* allocated by the
1187 * function itself, but must be allocated by the caller if necessary.
1188 *
1189 * @node: The node for whose string property should be set
1190 * @propname: The name of the string property to set
1191 * @value: The new value of the string property (must be valid prior to
1192 * calling the function)
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001193 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001194 */
1195int ofnode_write_string(ofnode node, const char *propname, const char *value);
1196
1197/**
Simon Glass55f79902022-07-30 15:52:14 -06001198 * ofnode_write_u32() - Set an integer property of an ofnode
1199 *
1200 * @node: The node for whose string property should be set
1201 * @propname: The name of the string property to set
1202 * @value: The new value of the 32-bit integer property
1203 * Return: 0 if successful, -ve on error
1204 */
1205int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1206
1207/**
Mario Sixe369e582018-06-26 08:46:48 +02001208 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1209 * ofnode
1210 *
1211 * This function effectively sets the node's "status" property to either "okay"
1212 * or "disable", hence making it available for driver model initialization or
1213 * not.
1214 *
1215 * @node: The node to enable
1216 * @value: Flag that tells the function to either disable or enable the
1217 * node
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001218 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001219 */
1220int ofnode_set_enabled(ofnode node, bool value);
1221
Simon Glass7de8bd02021-08-07 07:24:01 -06001222/**
Sean Anderson8b52f232022-03-28 18:14:37 -04001223 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1224 *
1225 * This function parses PHY handle from the Ethernet controller's ofnode
1226 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1227 *
1228 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1229 * if the result to that is true, this function should not be called.
1230 *
1231 * @eth_node: ofnode belonging to the Ethernet controller
1232 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1233 */
1234ofnode ofnode_get_phy_node(ofnode eth_node);
1235
1236/**
1237 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1238 *
1239 * This function parses the "phy-mode" / "phy-connection-type" property and
1240 * returns the corresponding PHY interface type.
1241 *
1242 * @mac_node: ofnode containing the property
1243 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1244 * error
1245 */
1246phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1247
1248#if CONFIG_IS_ENABLED(DM)
1249/**
Simon Glass7de8bd02021-08-07 07:24:01 -06001250 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1251 *
1252 * This reads a property from the /config node of the devicetree.
1253 *
1254 * See doc/config.txt for bindings
1255 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001256 * @prop_name: property name to look up
1257 * Return: true, if it exists, false if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001258 */
1259bool ofnode_conf_read_bool(const char *prop_name);
1260
1261/**
1262 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1263 *
1264 * This reads a property from the /config node of the devicetree.
1265 *
1266 * See doc/config.txt for bindings
1267 *
1268 * @prop_name: property name to look up
1269 * @default_val: default value to return if the property is not found
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001270 * Return: integer value, if found, or @default_val if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001271 */
1272int ofnode_conf_read_int(const char *prop_name, int default_val);
1273
1274/**
1275 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1276 *
1277 * This reads a property from the /config node of the devicetree.
1278 *
1279 * See doc/config.txt for bindings
1280 *
1281 * @prop_name: property name to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001282 * Return: string value, if found, or NULL if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001283 */
1284const char *ofnode_conf_read_str(const char *prop_name);
1285
Sean Anderson8b52f232022-03-28 18:14:37 -04001286#else /* CONFIG_DM */
1287static inline bool ofnode_conf_read_bool(const char *prop_name)
1288{
1289 return false;
1290}
Marek Behúnf3dd2132022-04-07 00:32:57 +02001291
Sean Anderson8b52f232022-03-28 18:14:37 -04001292static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1293{
1294 return default_val;
1295}
1296
1297static inline const char *ofnode_conf_read_str(const char *prop_name)
1298{
1299 return NULL;
1300}
1301#endif /* CONFIG_DM */
Marek Behún123ca112022-04-07 00:33:01 +02001302
Simon Glass4984de22017-05-17 17:18:10 -06001303#endif