blob: 7ce1e4c6d91e4cbc776678af7bd2627b0441e02d [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/**
Simon Glass9e512042017-05-18 20:08:58 -0600225 * ofnode_read_u32() - Read a 32-bit integer from a property
226 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100227 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600228 * @propname: name of the property to read from
229 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100230 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600231 */
232int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
233
234/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200235 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
236 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100237 * @node: valid node reference to read property from
Dario Binacchi4bb70752020-03-29 18:04:41 +0200238 * @propname: name of the property to read from
239 * @index: index of the integer to return
240 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100241 * Return: 0 if OK, -ve on error
Dario Binacchi4bb70752020-03-29 18:04:41 +0200242 */
243int ofnode_read_u32_index(ofnode node, const char *propname, int index,
244 u32 *outp);
245
246/**
Simon Glass9e512042017-05-18 20:08:58 -0600247 * ofnode_read_s32() - Read a 32-bit integer from a property
248 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100249 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600250 * @propname: name of the property to read from
251 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100252 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600253 */
254static inline int ofnode_read_s32(ofnode node, const char *propname,
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100255 s32 *outp)
Simon Glass9e512042017-05-18 20:08:58 -0600256{
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100257 return ofnode_read_u32(node, propname, (u32 *)outp);
Simon Glass9e512042017-05-18 20:08:58 -0600258}
259
260/**
261 * ofnode_read_u32_default() - Read a 32-bit integer from a property
262 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100263 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600264 * @propname: name of the property to read from
265 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100266 * Return: property value, or @def if not found
Simon Glass9e512042017-05-18 20:08:58 -0600267 */
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100268u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
Simon Glass9e512042017-05-18 20:08:58 -0600269
270/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200271 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
272 * property
273 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100274 * @node: valid node reference to read property from
Dario Binacchi4bb70752020-03-29 18:04:41 +0200275 * @propname: name of the property to read from
276 * @index: index of the integer to return
277 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100278 * Return: property value, or @def if not found
Dario Binacchi4bb70752020-03-29 18:04:41 +0200279 */
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100280u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
Dario Binacchi4bb70752020-03-29 18:04:41 +0200281 u32 def);
282
283/**
Simon Glass9e512042017-05-18 20:08:58 -0600284 * ofnode_read_s32_default() - Read a 32-bit integer from a property
285 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100286 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600287 * @propname: name of the property to read from
288 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100289 * Return: property value, or @def if not found
Simon Glass9e512042017-05-18 20:08:58 -0600290 */
291int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
292
293/**
Lukas Auerafb30122018-11-22 11:26:35 +0100294 * ofnode_read_u64() - Read a 64-bit integer from a property
295 *
296 * @node: valid node reference to read property from
297 * @propname: name of the property to read from
298 * @outp: place to put value (if found)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100299 * Return: 0 if OK, -ve on error
Lukas Auerafb30122018-11-22 11:26:35 +0100300 */
301int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
302
303/**
Simon Glass7e5196c2018-06-11 13:07:10 -0600304 * ofnode_read_u64_default() - Read a 64-bit integer from a property
305 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100306 * @node: valid node reference to read property from
Simon Glass7e5196c2018-06-11 13:07:10 -0600307 * @propname: name of the property to read from
308 * @def: default value to return if the property has no value
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100309 * Return: property value, or @def if not found
Simon Glass7e5196c2018-06-11 13:07:10 -0600310 */
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200311u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass7e5196c2018-06-11 13:07:10 -0600312
313/**
Simon Glassa8167d82020-01-27 08:49:44 -0700314 * ofnode_read_prop() - Read a property from a node
315 *
316 * @node: valid node reference to read property from
317 * @propname: name of the property to read
318 * @sizep: if non-NULL, returns the size of the property, or an error code
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100319 * if not found
320 * Return: property value, or NULL if there is no such property
Simon Glassa8167d82020-01-27 08:49:44 -0700321 */
322const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
323
324/**
Simon Glass9e512042017-05-18 20:08:58 -0600325 * ofnode_read_string() - Read a string from a property
326 *
Simon Glassa8167d82020-01-27 08:49:44 -0700327 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600328 * @propname: name of the property to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100329 * Return: string from property value, or NULL if there is no such property
Simon Glass9e512042017-05-18 20:08:58 -0600330 */
331const char *ofnode_read_string(ofnode node, const char *propname);
332
333/**
Simon Glassbed77492017-05-18 20:09:01 -0600334 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glass9e512042017-05-18 20:08:58 -0600335 *
336 * @node: valid node reference to read property from
337 * @propname: name of the property to read
338 * @out_values: pointer to return value, modified only if return value is 0
339 * @sz: number of array elements to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100340 * Return: 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600341 *
342 * Search for a property in a device node and read 32-bit value(s) from
343 * it. Returns 0 on success, -EINVAL if the property does not exist,
344 * -ENODATA if property does not have a value, and -EOVERFLOW if the
345 * property data isn't large enough.
346 *
347 * The out_values is modified only if a valid u32 value can be decoded.
348 */
349int ofnode_read_u32_array(ofnode node, const char *propname,
350 u32 *out_values, size_t sz);
351
352/**
353 * ofnode_read_bool() - read a boolean value from a property
354 *
355 * @node: valid node reference to read property from
356 * @propname: name of property to read
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100357 * Return: true if property is present (meaning true), false if not present
Simon Glass9e512042017-05-18 20:08:58 -0600358 */
359bool ofnode_read_bool(ofnode node, const char *propname);
360
361/**
362 * ofnode_find_subnode() - find a named subnode of a parent node
363 *
364 * @node: valid reference to parent node
365 * @subnode_name: name of subnode to find
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100366 * Return: reference to subnode (which can be invalid if there is no such
Simon Glass9e512042017-05-18 20:08:58 -0600367 * subnode)
368 */
369ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
370
Simon Glassec1add12020-12-16 17:25:06 -0700371#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass401d1c42020-10-30 21:38:53 -0600372#include <asm/global_data.h>
373
Simon Glassec1add12020-12-16 17:25:06 -0700374static inline bool ofnode_is_enabled(ofnode node)
375{
376 if (ofnode_is_np(node)) {
377 return of_device_is_available(ofnode_to_np(node));
378 } else {
379 return fdtdec_get_is_enabled(gd->fdt_blob,
380 ofnode_to_offset(node));
381 }
382}
383
384static inline ofnode ofnode_first_subnode(ofnode node)
385{
386 assert(ofnode_valid(node));
387 if (ofnode_is_np(node))
388 return np_to_ofnode(node.np->child);
389
390 return offset_to_ofnode(
391 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
392}
393
394static inline ofnode ofnode_next_subnode(ofnode node)
395{
396 assert(ofnode_valid(node));
397 if (ofnode_is_np(node))
398 return np_to_ofnode(node.np->sibling);
399
400 return offset_to_ofnode(
401 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
402}
403#else
404/**
405 * ofnode_is_enabled() - Checks whether a node is enabled.
406 * This looks for a 'status' property. If this exists, then returns true if
407 * the status is 'okay' and false otherwise. If there is no status property,
408 * it returns true on the assumption that anything mentioned should be enabled
409 * by default.
410 *
411 * @node: node to examine
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100412 * Return: false (not enabled) or true (enabled)
Simon Glassec1add12020-12-16 17:25:06 -0700413 */
414bool ofnode_is_enabled(ofnode node);
415
Simon Glass9e512042017-05-18 20:08:58 -0600416/**
417 * ofnode_first_subnode() - find the first subnode of a parent node
418 *
419 * @node: valid reference to a valid parent node
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100420 * Return: reference to the first subnode (which can be invalid if the parent
Simon Glass9e512042017-05-18 20:08:58 -0600421 * node has no subnodes)
422 */
423ofnode ofnode_first_subnode(ofnode node);
424
425/**
426 * ofnode_next_subnode() - find the next sibling of a subnode
427 *
428 * @node: valid reference to previous node (sibling)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100429 * Return: reference to the next subnode (which can be invalid if the node
Simon Glass9e512042017-05-18 20:08:58 -0600430 * has no more siblings)
431 */
432ofnode ofnode_next_subnode(ofnode node);
Simon Glassec1add12020-12-16 17:25:06 -0700433#endif /* DM_INLINE_OFNODE */
Simon Glass9e512042017-05-18 20:08:58 -0600434
435/**
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100436 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
437 *
438 * @node: valid node to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100439 * Return: ofnode reference of the parent node
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100440 */
441ofnode ofnode_get_parent(ofnode node);
442
443/**
Simon Glass9e512042017-05-18 20:08:58 -0600444 * ofnode_get_name() - get the name of a node
445 *
446 * @node: valid node to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100447 * Return: name of node
Simon Glass9e512042017-05-18 20:08:58 -0600448 */
449const char *ofnode_get_name(ofnode node);
450
451/**
Marek Behún0e116be2021-05-26 14:08:18 +0200452 * ofnode_get_path() - get the full path of a node
453 *
454 * @node: valid node to look up
455 * @buf: buffer to write the node path into
456 * @buflen: buffer size
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100457 * Return: 0 if OK, -ve on error
Marek Behún0e116be2021-05-26 14:08:18 +0200458 */
459int ofnode_get_path(ofnode node, char *buf, int buflen);
460
461/**
Kever Yangb4f20762018-02-23 17:38:50 +0100462 * ofnode_get_by_phandle() - get ofnode from phandle
463 *
464 * @phandle: phandle to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100465 * Return: ofnode reference to the phandle
Kever Yangb4f20762018-02-23 17:38:50 +0100466 */
467ofnode ofnode_get_by_phandle(uint phandle);
468
469/**
Simon Glass9e512042017-05-18 20:08:58 -0600470 * ofnode_read_size() - read the size of a property
471 *
472 * @node: node to check
473 * @propname: property to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100474 * Return: size of property if present, or -EINVAL if not
Simon Glass9e512042017-05-18 20:08:58 -0600475 */
476int ofnode_read_size(ofnode node, const char *propname);
477
478/**
Keerthye679d032019-04-24 17:19:53 +0530479 * ofnode_get_addr_size_index() - get an address/size from a node
480 * based on index
481 *
482 * This reads the register address/size from a node based on index
483 *
484 * @node: node to read from
485 * @index: Index of address to read (0 for first)
486 * @size: Pointer to size of the address
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100487 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Keerthye679d032019-04-24 17:19:53 +0530488 */
489phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
490 fdt_size_t *size);
491
492/**
Marek Behún31a7b712021-05-26 14:08:17 +0200493 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
494 * based on index, without address
495 * translation
496 *
497 * This reads the register address/size from a node based on index.
498 * The resulting address is not translated. Useful for example for on-disk
499 * addresses.
500 *
501 * @node: node to read from
502 * @index: Index of address to read (0 for first)
503 * @size: Pointer to size of the address
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100504 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Marek Behún31a7b712021-05-26 14:08:17 +0200505 */
506phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
507 fdt_size_t *size);
508
509/**
Simon Glassbed77492017-05-18 20:09:01 -0600510 * ofnode_get_addr_index() - get an address from a node
511 *
512 * This reads the register address from a node
513 *
514 * @node: node to read from
515 * @index: Index of address to read (0 for first)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100516 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glassbed77492017-05-18 20:09:01 -0600517 */
518phys_addr_t ofnode_get_addr_index(ofnode node, int index);
519
520/**
521 * ofnode_get_addr() - get an address from a node
522 *
523 * This reads the register address from a node
524 *
525 * @node: node to read from
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100526 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glassbed77492017-05-18 20:09:01 -0600527 */
528phys_addr_t ofnode_get_addr(ofnode node);
529
530/**
Chen Guanqiaoaa351a12021-04-12 14:51:11 +0800531 * ofnode_get_size() - get size from a node
532 *
533 * This reads the register size from a node
534 *
535 * @node: node to read from
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100536 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
Chen Guanqiaoaa351a12021-04-12 14:51:11 +0800537 */
538fdt_size_t ofnode_get_size(ofnode node);
539
540/**
Simon Glass9e512042017-05-18 20:08:58 -0600541 * ofnode_stringlist_search() - find a string in a string list and return index
542 *
543 * Note that it is possible for this function to succeed on property values
544 * that are not NUL-terminated. That's because the function will stop after
545 * finding the first occurrence of @string. This can for example happen with
546 * small-valued cell properties, such as #address-cells, when searching for
547 * the empty string.
548 *
549 * @node: node to check
550 * @propname: name of the property containing the string list
551 * @string: string to look up in the string list
552 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100553 * Return:
Simon Glass9e512042017-05-18 20:08:58 -0600554 * the index of the string in the list of strings
555 * -ENODATA if the property is not found
556 * -EINVAL on some other error
557 */
558int ofnode_stringlist_search(ofnode node, const char *propname,
559 const char *string);
560
561/**
Simon Glass8c293d62017-06-12 06:21:28 -0600562 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glass9e512042017-05-18 20:08:58 -0600563 *
564 * Note that this will successfully extract strings from properties with
565 * non-NUL-terminated values. For example on small-valued cell properties
566 * this function will return the empty string.
567 *
568 * If non-NULL, the length of the string (on success) or a negative error-code
569 * (on failure) will be stored in the integer pointer to by lenp.
570 *
571 * @node: node to check
572 * @propname: name of the property containing the string list
Simon Glass32c6a8e2021-10-23 17:26:06 -0600573 * @index: index of the string to return (cannot be negative)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100574 * @outp: return location for the string
Simon Glass9e512042017-05-18 20:08:58 -0600575 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100576 * Return:
Simon Glass32c6a8e2021-10-23 17:26:06 -0600577 * 0 if found or -ve error value if not found
Simon Glass9e512042017-05-18 20:08:58 -0600578 */
579int ofnode_read_string_index(ofnode node, const char *propname, int index,
580 const char **outp);
581
582/**
Simon Glass8c293d62017-06-12 06:21:28 -0600583 * ofnode_read_string_count() - find the number of strings in a string list
584 *
585 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100586 * @property: name of the property containing the string list
587 * Return:
Simon Glass8c293d62017-06-12 06:21:28 -0600588 * number of strings in the list, or -ve error value if not found
589 */
590int ofnode_read_string_count(ofnode node, const char *property);
591
592/**
Simon Glass075bfc92021-10-23 17:26:07 -0600593 * ofnode_read_string_list() - read a list of strings
594 *
595 * This produces a list of string pointers with each one pointing to a string
596 * in the string list. If the property does not exist, it returns {NULL}.
597 *
598 * The data is allocated and the caller is reponsible for freeing the return
599 * value (the list of string pointers). The strings themselves may not be
600 * changed as they point directly into the devicetree property.
601 *
602 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100603 * @property: name of the property containing the string list
Simon Glass075bfc92021-10-23 17:26:07 -0600604 * @listp: returns an allocated, NULL-terminated list of strings if the return
605 * value is > 0, else is set to NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100606 * Return:
607 * number of strings in list, 0 if none, -ENOMEM if out of memory,
608 * -EINVAL if no such property, -EENODATA if property is empty
Simon Glass075bfc92021-10-23 17:26:07 -0600609 */
610int ofnode_read_string_list(ofnode node, const char *property,
611 const char ***listp);
612
613/**
Simon Glass9e512042017-05-18 20:08:58 -0600614 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
615 *
616 * This function is useful to parse lists of phandles and their arguments.
617 * Returns 0 on success and fills out_args, on error returns appropriate
618 * errno value.
619 *
620 * Caller is responsible to call of_node_put() on the returned out_args->np
621 * pointer.
622 *
623 * Example:
624 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100625 * .. code-block::
Simon Glass9e512042017-05-18 20:08:58 -0600626 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100627 * phandle1: node1 {
628 * #list-cells = <2>;
629 * };
630 * phandle2: node2 {
631 * #list-cells = <1>;
632 * };
633 * node3 {
634 * list = <&phandle1 1 2 &phandle2 3>;
635 * };
Simon Glass9e512042017-05-18 20:08:58 -0600636 *
637 * To get a device_node of the `node2' node you may call this:
638 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
639 *
640 * @node: device tree node containing a list
641 * @list_name: property name that contains a list
642 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100643 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glass9e512042017-05-18 20:08:58 -0600644 * @index: index of a phandle to parse out
645 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100646 * Return:
647 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
648 * @list_name does not exist, -EINVAL if a phandle was not found,
649 * @cells_name could not be found, the arguments were truncated or there
650 * were too many arguments.
Simon Glass9e512042017-05-18 20:08:58 -0600651 */
652int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
653 const char *cells_name, int cell_count,
654 int index,
655 struct ofnode_phandle_args *out_args);
656
657/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200658 * ofnode_count_phandle_with_args() - Count number of phandle in a list
659 *
660 * This function is useful to count phandles into a list.
661 * Returns number of phandle on success, on error returns appropriate
662 * errno value.
663 *
664 * @node: device tree node containing a list
665 * @list_name: property name that contains a list
666 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100667 * @cell_count: Cell count to use if @cells_name is NULL
668 * Return:
669 * number of phandle on success, -ENOENT if @list_name does not exist,
670 * -EINVAL if a phandle was not found, @cells_name could not be found.
Patrice Chotard642346a2017-07-18 11:57:08 +0200671 */
672int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200673 const char *cells_name, int cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +0200674
675/**
Simon Glass9e512042017-05-18 20:08:58 -0600676 * ofnode_path() - find a node by full path
677 *
Simon Glass33104842022-07-30 15:52:08 -0600678 * This uses the control FDT.
679 *
Simon Glass9e512042017-05-18 20:08:58 -0600680 * @path: Full path to node, e.g. "/bus/spi@1"
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100681 * Return: reference to the node found. Use ofnode_valid() to check if it exists
Simon Glass9e512042017-05-18 20:08:58 -0600682 */
683ofnode ofnode_path(const char *path);
684
685/**
Simon Glass33104842022-07-30 15:52:08 -0600686 * ofnode_path_root() - find a node by full path from a root node
687 *
688 * @tree: Device tree to use
689 * @path: Full path to node, e.g. "/bus/spi@1"
690 * Return: reference to the node found. Use ofnode_valid() to check if it exists
691 */
692ofnode ofnode_path_root(oftree tree, const char *path);
693
694/**
Simon Glassbd933bf2020-01-27 08:49:46 -0700695 * ofnode_read_chosen_prop() - get the value of a chosen property
696 *
697 * This looks for a property within the /chosen node and returns its value
698 *
699 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100700 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Simon Glassbd933bf2020-01-27 08:49:46 -0700701 * returns NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100702 * Return: property value if found, else NULL
Simon Glassbd933bf2020-01-27 08:49:46 -0700703 */
704const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
705
706/**
Simon Glass14ca9f72020-01-27 08:49:43 -0700707 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glass9e512042017-05-18 20:08:58 -0600708 *
Simon Glass14ca9f72020-01-27 08:49:43 -0700709 * This looks for a property within the /chosen node and returns its value,
710 * checking that it is a valid nul-terminated string
Simon Glass9e512042017-05-18 20:08:58 -0600711 *
712 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100713 * Return: string value if found, else NULL
Simon Glass9e512042017-05-18 20:08:58 -0600714 */
Simon Glass14ca9f72020-01-27 08:49:43 -0700715const char *ofnode_read_chosen_string(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600716
717/**
Simon Glass74d594a2020-01-27 08:49:42 -0700718 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glass9e512042017-05-18 20:08:58 -0600719 *
Simon Glass74d594a2020-01-27 08:49:42 -0700720 * This looks up a named property in the chosen node and uses that as a path to
721 * look up a code.
722 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100723 * @propname: Property name to look for
724 * Return: the referenced node if present, else ofnode_null()
Simon Glass9e512042017-05-18 20:08:58 -0600725 */
Simon Glass74d594a2020-01-27 08:49:42 -0700726ofnode ofnode_get_chosen_node(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600727
Michal Simek305d3182020-07-28 12:51:08 +0200728/**
729 * ofnode_read_aliases_prop() - get the value of a aliases property
730 *
731 * This looks for a property within the /aliases node and returns its value
732 *
733 * @propname: Property name to look for
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100734 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Michal Simek305d3182020-07-28 12:51:08 +0200735 * returns NULL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100736 * Return: property value if found, else NULL
Michal Simek305d3182020-07-28 12:51:08 +0200737 */
738const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
739
740/**
741 * ofnode_get_aliases_node() - get a referenced node from the aliases node
742 *
743 * This looks up a named property in the aliases node and uses that as a path to
744 * look up a code.
745 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100746 * @propname: Property name to look for
747 * Return: the referenced node if present, else ofnode_null()
Michal Simek305d3182020-07-28 12:51:08 +0200748 */
749ofnode ofnode_get_aliases_node(const char *propname);
750
Simon Glass9e512042017-05-18 20:08:58 -0600751struct display_timing;
752/**
753 * ofnode_decode_display_timing() - decode display timings
754 *
755 * Decode display timings from the supplied 'display-timings' node.
756 * See doc/device-tree-bindings/video/display-timing.txt for binding
757 * information.
758 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100759 * @node: 'display-timing' node containing the timing subnodes
760 * @index: Index number to read (0=first timing subnode)
761 * @config: Place to put timings
762 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Simon Glass9e512042017-05-18 20:08:58 -0600763 */
764int ofnode_decode_display_timing(ofnode node, int index,
765 struct display_timing *config);
766
767/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100768 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glass9e512042017-05-18 20:08:58 -0600769 *
770 * @node: node to read
771 * @propname: property to read
772 * @lenp: place to put length on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100773 * Return: pointer to property, or NULL if not found
Simon Glass9e512042017-05-18 20:08:58 -0600774 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900775const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glass9e512042017-05-18 20:08:58 -0600776
777/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100778 * ofnode_get_first_property()- get the reference of the first property
779 *
780 * Get reference to the first property of the node, it is used to iterate
781 * and read all the property with ofnode_get_property_by_prop().
782 *
783 * @node: node to read
784 * @prop: place to put argument reference
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100785 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100786 */
787int ofnode_get_first_property(ofnode node, struct ofprop *prop);
788
789/**
790 * ofnode_get_next_property() - get the reference of the next property
791 *
792 * Get reference to the next property of the node, it is used to iterate
793 * and read all the property with ofnode_get_property_by_prop().
794 *
795 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100796 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100797 */
798int ofnode_get_next_property(struct ofprop *prop);
799
800/**
801 * ofnode_get_property_by_prop() - get a pointer to the value of a property
802 *
803 * Get value for the property identified by the provided reference.
804 *
805 * @prop: reference on property
806 * @propname: If non-NULL, place to property name on success,
807 * @lenp: If non-NULL, place to put length on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100808 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100809 */
810const void *ofnode_get_property_by_prop(const struct ofprop *prop,
811 const char **propname, int *lenp);
812
813/**
Simon Glass9e512042017-05-18 20:08:58 -0600814 * ofnode_is_available() - check if a node is marked available
815 *
816 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100817 * Return: true if node's 'status' property is "okay" (or is missing)
Simon Glass9e512042017-05-18 20:08:58 -0600818 */
819bool ofnode_is_available(ofnode node);
820
821/**
822 * ofnode_get_addr_size() - get address and size from a property
823 *
824 * This does no address translation. It simply reads an property that contains
825 * an address and a size value, one after the other.
826 *
827 * @node: node to read from
828 * @propname: property to read
829 * @sizep: place to put size value (on success)
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100830 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glass9e512042017-05-18 20:08:58 -0600831 */
832phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
833 phys_size_t *sizep);
834
835/**
836 * ofnode_read_u8_array_ptr() - find an 8-bit array
837 *
838 * Look up a property in a node and return a pointer to its contents as a
839 * byte array of given length. The property must have at least enough data
840 * for the array (count bytes). It may have more, but this will be ignored.
841 * The data is not copied.
842 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100843 * @node: node to examine
844 * @propname: name of property to find
845 * @sz: number of array elements
846 * Return:
847 * pointer to byte array if found, or NULL if the property is not found or
848 * there is not enough data
Simon Glass9e512042017-05-18 20:08:58 -0600849 */
850const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
851 size_t sz);
852
853/**
854 * ofnode_read_pci_addr() - look up a PCI address
855 *
856 * Look at an address property in a node and return the PCI address which
857 * corresponds to the given type in the form of fdt_pci_addr.
858 * The property must hold one fdt_pci_addr with a lengh.
859 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100860 * @node: node to examine
861 * @type: pci address type (FDT_PCI_SPACE_xxx)
862 * @propname: name of property to find
863 * @addr: returns pci address in the form of fdt_pci_addr
864 * Return:
865 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
866 * format of the property was invalid, -ENXIO if the requested
867 * address type was not found
Simon Glass9e512042017-05-18 20:08:58 -0600868 */
869int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
870 const char *propname, struct fdt_pci_addr *addr);
871
872/**
Bin Meng7b9cbad2018-08-03 01:14:35 -0700873 * ofnode_read_pci_vendev() - look up PCI vendor and device id
874 *
875 * Look at the compatible property of a device node that represents a PCI
876 * device and extract pci vendor id and device id from it.
877 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100878 * @node: node to examine
879 * @vendor: vendor id of the pci device
880 * @device: device id of the pci device
881 * Return: 0 if ok, negative on error
Bin Meng7b9cbad2018-08-03 01:14:35 -0700882 */
883int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
884
885/**
Michal Simekdb681d42022-02-23 15:45:40 +0100886 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
887 *
888 * Look at the compatible property of a device node that represents a eth phy
889 * device and extract phy vendor id and device id from it.
890 *
Heinrich Schuchardtd23f2902022-03-24 16:22:32 +0100891 * @node: node to examine
892 * @vendor: vendor id of the eth phy device
893 * @device: device id of the eth phy device
894 * Return: 0 if ok, negative on error
Michal Simekdb681d42022-02-23 15:45:40 +0100895 */
896int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
897
898/**
Simon Glass9e512042017-05-18 20:08:58 -0600899 * ofnode_read_addr_cells() - Get the number of address cells for a node
900 *
901 * This walks back up the tree to find the closest #address-cells property
902 * which controls the given node.
903 *
904 * @node: Node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100905 * Return: number of address cells this node uses
Simon Glass9e512042017-05-18 20:08:58 -0600906 */
907int ofnode_read_addr_cells(ofnode node);
908
909/**
910 * ofnode_read_size_cells() - Get the number of size cells for a node
911 *
912 * This walks back up the tree to find the closest #size-cells property
913 * which controls the given node.
914 *
915 * @node: Node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100916 * Return: number of size cells this node uses
Simon Glass9e512042017-05-18 20:08:58 -0600917 */
918int ofnode_read_size_cells(ofnode node);
919
920/**
Simon Glass878d68c2017-06-12 06:21:31 -0600921 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
922 *
923 * This function matches fdt_address_cells().
924 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100925 * @node: Node to check
926 * Return: value of #address-cells property in this node, or 2 if none
Simon Glass878d68c2017-06-12 06:21:31 -0600927 */
928int ofnode_read_simple_addr_cells(ofnode node);
929
930/**
931 * ofnode_read_simple_size_cells() - Get the size cells property in a node
932 *
933 * This function matches fdt_size_cells().
934 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100935 * @node: Node to check
936 * Return: value of #size-cells property in this node, or 2 if none
Simon Glass878d68c2017-06-12 06:21:31 -0600937 */
938int ofnode_read_simple_size_cells(ofnode node);
939
940/**
Simon Glass9e512042017-05-18 20:08:58 -0600941 * ofnode_pre_reloc() - check if a node should be bound before relocation
942 *
943 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
944 * via special device tree properties.
945 *
946 * Before relocation this function can be used to check if nodes are required
947 * in either SPL or TPL stages.
948 *
949 * After relocation and jumping into the real U-Boot binary it is possible to
950 * determine if a node was bound in one of SPL/TPL stages.
951 *
Patrick Delaunay54e12232019-05-21 19:19:13 +0200952 * There are 4 settings currently in use
953 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glass9e512042017-05-18 20:08:58 -0600954 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100955 * Existing platforms only use it to indicate nodes needed in
956 * SPL. Should probably be replaced by u-boot,dm-spl for new platforms.
Patrick Delaunay54e12232019-05-21 19:19:13 +0200957 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
958 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glass9e512042017-05-18 20:08:58 -0600959 *
960 * @node: node to check
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100961 * Return: true if node is needed in SPL/TL, false otherwise
Simon Glass9e512042017-05-18 20:08:58 -0600962 */
963bool ofnode_pre_reloc(ofnode node);
964
Simon Glassc98ad442018-06-11 13:07:12 -0600965/**
966 * ofnode_read_resource() - Read a resource from a node
967 *
968 * Read resource information from a node at the given index
969 *
970 * @node: Node to read from
971 * @index: Index of resource to read (0 = first)
972 * @res: Returns resource that was read, on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100973 * Return: 0 if OK, -ve on error
Simon Glassc98ad442018-06-11 13:07:12 -0600974 */
Simon Glassdcf98852017-07-25 08:29:55 -0600975int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassc98ad442018-06-11 13:07:12 -0600976
977/**
978 * ofnode_read_resource_byname() - Read a resource from a node by name
979 *
980 * Read resource information from a node matching the given name. This uses a
981 * 'reg-names' string list property with the names matching the associated
982 * 'reg' property list.
983 *
984 * @node: Node to read from
985 * @name: Name of resource to read
986 * @res: Returns resource that was read, on success
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100987 * Return: 0 if OK, -ve on error
Simon Glassc98ad442018-06-11 13:07:12 -0600988 */
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900989int ofnode_read_resource_byname(ofnode node, const char *name,
990 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -0600991
Simon Glass3991f422017-08-05 15:45:54 -0600992/**
Simon Glassc60f6712018-06-11 13:07:13 -0600993 * ofnode_by_compatible() - Find the next compatible node
994 *
995 * Find the next node after @from that is compatible with @compat
996 *
997 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
998 * @compat: Compatible string to match
Patrick Delaunaybe74f712022-01-12 10:53:49 +0100999 * Return: ofnode found, or ofnode_null() if none
Simon Glassc60f6712018-06-11 13:07:13 -06001000 */
1001ofnode ofnode_by_compatible(ofnode from, const char *compat);
1002
1003/**
Jens Wiklander61fba0f2018-08-20 11:09:58 +02001004 * ofnode_by_prop_value() - Find the next node with given property value
1005 *
1006 * Find the next node after @from that has a @propname with a value
1007 * @propval and a length @proplen.
1008 *
1009 * @from: ofnode to start from (use ofnode_null() to start at the
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001010 * beginning)
1011 * @propname: property name to check
1012 * @propval: property value to search for
1013 * @proplen: length of the value in propval
1014 * Return: ofnode found, or ofnode_null() if none
Jens Wiklander61fba0f2018-08-20 11:09:58 +02001015 */
1016ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1017 const void *propval, int proplen);
1018
1019/**
Simon Glass3991f422017-08-05 15:45:54 -06001020 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1021 *
1022 * @node: child node (ofnode, lvalue)
1023 * @parent: parent node (ofnode)
1024 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001025 * This is a wrapper around a for loop and is used like so::
Simon Glass3991f422017-08-05 15:45:54 -06001026 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001027 * ofnode node;
1028 * ofnode_for_each_subnode(node, parent) {
1029 * Use node
1030 * ...
1031 * }
Simon Glass3991f422017-08-05 15:45:54 -06001032 *
1033 * Note that this is implemented as a macro and @node is used as
1034 * iterator in the loop. The parent variable can be a constant or even a
1035 * literal.
1036 */
1037#define ofnode_for_each_subnode(node, parent) \
1038 for (node = ofnode_first_subnode(parent); \
1039 ofnode_valid(node); \
1040 node = ofnode_next_subnode(node))
1041
Mario Six147c6072018-01-15 11:07:19 +01001042/**
Michael Walleb8ec9452021-10-15 15:15:17 +02001043 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1044 * compatible string
1045 *
1046 * @node: child node (ofnode, lvalue)
1047 * @compat: compatible string to match
1048 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001049 * This is a wrapper around a for loop and is used like so::
Michael Walleb8ec9452021-10-15 15:15:17 +02001050 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001051 * ofnode node;
1052 * ofnode_for_each_compatible_node(node, parent, compatible) {
1053 * Use node
1054 * ...
1055 * }
Michael Walleb8ec9452021-10-15 15:15:17 +02001056 *
1057 * Note that this is implemented as a macro and @node is used as
1058 * iterator in the loop.
1059 */
1060#define ofnode_for_each_compatible_node(node, compat) \
1061 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1062 ofnode_valid(node); \
1063 node = ofnode_by_compatible(node, compat))
1064
1065/**
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001066 * ofnode_get_child_count() - get the child count of a ofnode
1067 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001068 * @parent: valid node to get its child count
1069 * Return: the number of subnodes
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001070 */
1071int ofnode_get_child_count(ofnode parent);
1072
1073/**
Fabien Dessenne641067f2019-05-31 15:11:30 +02001074 * ofnode_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +01001075 *
1076 * Translate an address from the device-tree into a CPU physical address. This
1077 * function walks up the tree and applies the various bus mappings along the
1078 * way.
1079 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001080 * @node: Device tree node giving the context in which to translate the address
Mario Six147c6072018-01-15 11:07:19 +01001081 * @in_addr: pointer to the address to translate
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001082 * Return: the translated address; OF_BAD_ADDR on error
Mario Six147c6072018-01-15 11:07:19 +01001083 */
1084u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001085
1086/**
Fabien Dessenne641067f2019-05-31 15:11:30 +02001087 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1088 *
1089 * Translate a DMA address from the device-tree into a CPU physical address.
1090 * This function walks up the tree and applies the various bus mappings along
1091 * the way.
1092 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001093 * @node: Device tree node giving the context in which to translate the
1094 * DMA address
Fabien Dessenne641067f2019-05-31 15:11:30 +02001095 * @in_addr: pointer to the DMA address to translate
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001096 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne641067f2019-05-31 15:11:30 +02001097 */
1098u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1099
1100/**
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001101 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1102 *
1103 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1104 * cpu->bus address translations
1105 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001106 * @node: Device tree node
1107 * @cpu: Pointer to variable storing the range's cpu address
1108 * @bus: Pointer to variable storing the range's bus address
1109 * @size: Pointer to variable storing the range's size
1110 * Return: translated DMA address or OF_BAD_ADDR on error
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001111 */
1112int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1113 u64 *size);
1114
1115/**
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001116 * ofnode_device_is_compatible() - check if the node is compatible with compat
1117 *
1118 * This allows to check whether the node is comaptible with the compat.
1119 *
1120 * @node: Device tree node for which compatible needs to be verified.
1121 * @compat: Compatible string which needs to verified in the given node.
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001122 * Return: true if OK, false if the compatible is not found
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001123 */
1124int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Sixe369e582018-06-26 08:46:48 +02001125
1126/**
1127 * ofnode_write_prop() - Set a property of a ofnode
1128 *
1129 * Note that the value passed to the function is *not* allocated by the
Simon Glass72b338a2022-07-30 15:52:07 -06001130 * function itself, but must be allocated by the caller if necessary. However
1131 * it does allocate memory for the property struct and name.
Mario Sixe369e582018-06-26 08:46:48 +02001132 *
1133 * @node: The node for whose property should be set
1134 * @propname: The name of the property to set
Mario Sixe369e582018-06-26 08:46:48 +02001135 * @value: The new value of the property (must be valid prior to calling
1136 * the function)
Simon Glassbe0789a2022-07-30 15:52:10 -06001137 * @len: The length of the new value of the property
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001138 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001139 */
Simon Glassbe0789a2022-07-30 15:52:10 -06001140int ofnode_write_prop(ofnode node, const char *propname, const void *value,
1141 int len);
Mario Sixe369e582018-06-26 08:46:48 +02001142
1143/**
1144 * ofnode_write_string() - Set a string property of a ofnode
1145 *
1146 * Note that the value passed to the function is *not* allocated by the
1147 * function itself, but must be allocated by the caller if necessary.
1148 *
1149 * @node: The node for whose string property should be set
1150 * @propname: The name of the string property to set
1151 * @value: The new value of the string property (must be valid prior to
1152 * calling the function)
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001153 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001154 */
1155int ofnode_write_string(ofnode node, const char *propname, const char *value);
1156
1157/**
Simon Glass55f79902022-07-30 15:52:14 -06001158 * ofnode_write_u32() - Set an integer property of an ofnode
1159 *
1160 * @node: The node for whose string property should be set
1161 * @propname: The name of the string property to set
1162 * @value: The new value of the 32-bit integer property
1163 * Return: 0 if successful, -ve on error
1164 */
1165int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1166
1167/**
Mario Sixe369e582018-06-26 08:46:48 +02001168 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1169 * ofnode
1170 *
1171 * This function effectively sets the node's "status" property to either "okay"
1172 * or "disable", hence making it available for driver model initialization or
1173 * not.
1174 *
1175 * @node: The node to enable
1176 * @value: Flag that tells the function to either disable or enable the
1177 * node
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001178 * Return: 0 if successful, -ve on error
Mario Sixe369e582018-06-26 08:46:48 +02001179 */
1180int ofnode_set_enabled(ofnode node, bool value);
1181
Simon Glass7de8bd02021-08-07 07:24:01 -06001182/**
Sean Anderson8b52f232022-03-28 18:14:37 -04001183 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1184 *
1185 * This function parses PHY handle from the Ethernet controller's ofnode
1186 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1187 *
1188 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1189 * if the result to that is true, this function should not be called.
1190 *
1191 * @eth_node: ofnode belonging to the Ethernet controller
1192 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1193 */
1194ofnode ofnode_get_phy_node(ofnode eth_node);
1195
1196/**
1197 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1198 *
1199 * This function parses the "phy-mode" / "phy-connection-type" property and
1200 * returns the corresponding PHY interface type.
1201 *
1202 * @mac_node: ofnode containing the property
1203 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1204 * error
1205 */
1206phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1207
1208#if CONFIG_IS_ENABLED(DM)
1209/**
Simon Glass7de8bd02021-08-07 07:24:01 -06001210 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1211 *
1212 * This reads a property from the /config node of the devicetree.
1213 *
1214 * See doc/config.txt for bindings
1215 *
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001216 * @prop_name: property name to look up
1217 * Return: true, if it exists, false if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001218 */
1219bool ofnode_conf_read_bool(const char *prop_name);
1220
1221/**
1222 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1223 *
1224 * This reads a property from the /config node of the devicetree.
1225 *
1226 * See doc/config.txt for bindings
1227 *
1228 * @prop_name: property name to look up
1229 * @default_val: default value to return if the property is not found
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001230 * Return: integer value, if found, or @default_val if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001231 */
1232int ofnode_conf_read_int(const char *prop_name, int default_val);
1233
1234/**
1235 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1236 *
1237 * This reads a property from the /config node of the devicetree.
1238 *
1239 * See doc/config.txt for bindings
1240 *
1241 * @prop_name: property name to look up
Patrick Delaunaybe74f712022-01-12 10:53:49 +01001242 * Return: string value, if found, or NULL if not
Simon Glass7de8bd02021-08-07 07:24:01 -06001243 */
1244const char *ofnode_conf_read_str(const char *prop_name);
1245
Sean Anderson8b52f232022-03-28 18:14:37 -04001246#else /* CONFIG_DM */
1247static inline bool ofnode_conf_read_bool(const char *prop_name)
1248{
1249 return false;
1250}
Marek Behúnf3dd2132022-04-07 00:32:57 +02001251
Sean Anderson8b52f232022-03-28 18:14:37 -04001252static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1253{
1254 return default_val;
1255}
1256
1257static inline const char *ofnode_conf_read_str(const char *prop_name)
1258{
1259 return NULL;
1260}
1261#endif /* CONFIG_DM */
Marek Behún123ca112022-04-07 00:33:01 +02001262
Simon Glass4984de22017-05-17 17:18:10 -06001263#endif