blob: ee8c44a71ec3b3d31bf462331f6d1192ad5c5b6f [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>
Stefan Roese45dbe752020-09-23 08:23:27 +020013#include <log.h>
Simon Glass9e512042017-05-18 20:08:58 -060014
15/* Enable checks to protect against invalid calls */
16#undef OF_CHECKS
17
Simon Glassdcf98852017-07-25 08:29:55 -060018struct resource;
19
Simon Glass4984de22017-05-17 17:18:10 -060020/**
21 * ofnode - reference to a device tree node
22 *
23 * This union can hold either a straightforward pointer to a struct device_node
24 * in the live device tree, or an offset within the flat device tree. In the
25 * latter case, the pointer value is just the integer offset within the flat DT.
26 *
27 * Thus we can reference nodes in both the live tree (once available) and the
28 * flat tree (until then). Functions are available to translate between an
29 * ofnode and either an offset or a struct device_node *.
30 *
31 * The reference can also hold a null offset, in which case the pointer value
Simon Glass9e512042017-05-18 20:08:58 -060032 * here is NULL. This corresponds to a struct device_node * value of
Simon Glass4984de22017-05-17 17:18:10 -060033 * NULL, or an offset of -1.
34 *
35 * There is no ambiguity as to whether ofnode holds an offset or a node
36 * pointer: when the live tree is active it holds a node pointer, otherwise it
37 * holds an offset. The value itself does not need to be unique and in theory
38 * the same value could point to a valid device node or a valid offset. We
39 * could arrange for a unique value to be used (e.g. by making the pointer
40 * point to an offset within the flat device tree in the case of an offset) but
41 * this increases code size slightly due to the subtraction. Since it offers no
42 * real benefit, the approach described here seems best.
43 *
44 * For now these points use constant types, since we don't allow writing
45 * the DT.
46 *
47 * @np: Pointer to device node, used for live tree
Baruch Siachafc1a782017-11-09 13:44:28 +020048 * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
Simon Glass4984de22017-05-17 17:18:10 -060049 * is not a really a pointer to a node: it is an offset value. See above.
50 */
51typedef union ofnode_union {
Heinrich Schuchardtb9390ce2020-07-24 18:39:43 +020052 const struct device_node *np;
Simon Glass4984de22017-05-17 17:18:10 -060053 long of_offset;
54} ofnode;
55
Simon Glass9e512042017-05-18 20:08:58 -060056struct ofnode_phandle_args {
57 ofnode node;
58 int args_count;
59 uint32_t args[OF_MAX_PHANDLE_ARGS];
60};
61
62/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +010063 * ofprop - reference to a property of a device tree node
64 *
65 * This struct hold the reference on one property of one node,
66 * using struct ofnode and an offset within the flat device tree or either
67 * a pointer to a struct property in the live device tree.
68 *
69 * Thus we can reference arguments in both the live tree and the flat tree.
70 *
71 * The property reference can also hold a null reference. This corresponds to
72 * a struct property NULL pointer or an offset of -1.
73 *
74 * @node: Pointer to device node
75 * @offset: Pointer into flat device tree, used for flat tree.
76 * @prop: Pointer to property, used for live treee.
77 */
78
79struct ofprop {
80 ofnode node;
81 union {
82 int offset;
83 const struct property *prop;
84 };
85};
86
87/**
Stefan Roese45dbe752020-09-23 08:23:27 +020088 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glass9e512042017-05-18 20:08:58 -060089 *
90 * This cannot be called if the reference contains an offset.
91 *
92 * @node: Reference containing struct device_node * (possibly invalid)
93 * @return pointer to device node (can be NULL)
94 */
95static inline const struct device_node *ofnode_to_np(ofnode node)
96{
97#ifdef OF_CHECKS
98 if (!of_live_active())
99 return NULL;
100#endif
101 return node.np;
102}
103
Simon Glass4984de22017-05-17 17:18:10 -0600104/**
105 * ofnode_to_offset() - convert an ofnode to a flat DT offset
106 *
107 * This cannot be called if the reference contains a node pointer.
108 *
109 * @node: Reference containing offset (possibly invalid)
110 * @return DT offset (can be -1)
111 */
112static inline int ofnode_to_offset(ofnode node)
113{
Simon Glass9e512042017-05-18 20:08:58 -0600114#ifdef OF_CHECKS
115 if (of_live_active())
116 return -1;
117#endif
Simon Glass4984de22017-05-17 17:18:10 -0600118 return node.of_offset;
119}
120
121/**
122 * ofnode_valid() - check if an ofnode is valid
123 *
124 * @return true if the reference contains a valid ofnode, false if it is NULL
125 */
126static inline bool ofnode_valid(ofnode node)
127{
Simon Glass9e512042017-05-18 20:08:58 -0600128 if (of_live_active())
129 return node.np != NULL;
130 else
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200131 return node.of_offset >= 0;
Simon Glass4984de22017-05-17 17:18:10 -0600132}
133
134/**
135 * offset_to_ofnode() - convert a DT offset to an ofnode
136 *
137 * @of_offset: DT offset (either valid, or -1)
138 * @return reference to the associated DT offset
139 */
140static inline ofnode offset_to_ofnode(int of_offset)
141{
142 ofnode node;
143
Simon Glass9e512042017-05-18 20:08:58 -0600144 if (of_live_active())
145 node.np = NULL;
146 else
Simon Glassb14c5332019-12-06 21:41:36 -0700147 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass4984de22017-05-17 17:18:10 -0600148
149 return node;
150}
151
152/**
Simon Glass9e512042017-05-18 20:08:58 -0600153 * np_to_ofnode() - convert a node pointer to an ofnode
154 *
155 * @np: Live node pointer (can be NULL)
156 * @return reference to the associated node pointer
157 */
158static inline ofnode np_to_ofnode(const struct device_node *np)
159{
160 ofnode node;
161
162 node.np = np;
163
164 return node;
165}
166
167/**
168 * ofnode_is_np() - check if a reference is a node pointer
169 *
170 * This function associated that if there is a valid live tree then all
171 * references will use it. This is because using the flat DT when the live tree
172 * is valid is not permitted.
173 *
174 * @node: reference to check (possibly invalid)
175 * @return true if the reference is a live node pointer, false if it is a DT
176 * offset
177 */
178static inline bool ofnode_is_np(ofnode node)
179{
180#ifdef OF_CHECKS
181 /*
182 * Check our assumption that flat tree offsets are not used when a
183 * live tree is in use.
184 */
185 assert(!ofnode_valid(node) ||
Stefan Roese45dbe752020-09-23 08:23:27 +0200186 (of_live_active() ? ofnode_to_np(node)
187 : ofnode_to_np(node)));
Simon Glass9e512042017-05-18 20:08:58 -0600188#endif
189 return of_live_active() && ofnode_valid(node);
190}
191
192/**
Simon Glass4984de22017-05-17 17:18:10 -0600193 * ofnode_equal() - check if two references are equal
194 *
195 * @return true if equal, else false
196 */
197static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
198{
199 /* We only need to compare the contents */
200 return ref1.of_offset == ref2.of_offset;
201}
202
Simon Glass9e512042017-05-18 20:08:58 -0600203/**
204 * ofnode_null() - Obtain a null ofnode
205 *
206 * This returns an ofnode which points to no node. It works both with the flat
207 * tree and livetree.
208 */
209static inline ofnode ofnode_null(void)
210{
211 ofnode node;
212
213 if (of_live_active())
214 node.np = NULL;
215 else
216 node.of_offset = -1;
217
218 return node;
219}
220
221/**
222 * ofnode_read_u32() - Read a 32-bit integer from a property
223 *
224 * @ref: valid node reference to read property from
225 * @propname: name of the property to read from
226 * @outp: place to put value (if found)
227 * @return 0 if OK, -ve on error
228 */
229int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
230
231/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200232 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
233 *
234 * @ref: valid node reference to read property from
235 * @propname: name of the property to read from
236 * @index: index of the integer to return
237 * @outp: place to put value (if found)
238 * @return 0 if OK, -ve on error
239 */
240int ofnode_read_u32_index(ofnode node, const char *propname, int index,
241 u32 *outp);
242
243/**
Simon Glass9e512042017-05-18 20:08:58 -0600244 * ofnode_read_s32() - Read a 32-bit integer from a property
245 *
246 * @ref: valid node reference to read property from
247 * @propname: name of the property to read from
248 * @outp: place to put value (if found)
249 * @return 0 if OK, -ve on error
250 */
251static inline int ofnode_read_s32(ofnode node, const char *propname,
252 s32 *out_value)
253{
254 return ofnode_read_u32(node, propname, (u32 *)out_value);
255}
256
257/**
258 * ofnode_read_u32_default() - Read a 32-bit integer from a property
259 *
260 * @ref: valid node reference to read property from
261 * @propname: name of the property to read from
262 * @def: default value to return if the property has no value
263 * @return property value, or @def if not found
264 */
Trent Piephob061ef32019-05-10 17:48:20 +0000265u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
Simon Glass9e512042017-05-18 20:08:58 -0600266
267/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200268 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
269 * property
270 *
271 * @ref: valid node reference to read property from
272 * @propname: name of the property to read from
273 * @index: index of the integer to return
274 * @def: default value to return if the property has no value
275 * @return property value, or @def if not found
276 */
277u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
278 u32 def);
279
280/**
Simon Glass9e512042017-05-18 20:08:58 -0600281 * ofnode_read_s32_default() - Read a 32-bit integer from a property
282 *
283 * @ref: valid node reference to read property from
284 * @propname: name of the property to read from
285 * @def: default value to return if the property has no value
286 * @return property value, or @def if not found
287 */
288int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
289
290/**
Lukas Auerafb30122018-11-22 11:26:35 +0100291 * ofnode_read_u64() - Read a 64-bit integer from a property
292 *
293 * @node: valid node reference to read property from
294 * @propname: name of the property to read from
295 * @outp: place to put value (if found)
296 * @return 0 if OK, -ve on error
297 */
298int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
299
300/**
Simon Glass7e5196c2018-06-11 13:07:10 -0600301 * ofnode_read_u64_default() - Read a 64-bit integer from a property
302 *
303 * @ref: valid node reference to read property from
304 * @propname: name of the property to read from
305 * @def: default value to return if the property has no value
306 * @return property value, or @def if not found
307 */
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200308u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass7e5196c2018-06-11 13:07:10 -0600309
310/**
Simon Glassa8167d82020-01-27 08:49:44 -0700311 * ofnode_read_prop() - Read a property from a node
312 *
313 * @node: valid node reference to read property from
314 * @propname: name of the property to read
315 * @sizep: if non-NULL, returns the size of the property, or an error code
316 if not found
317 * @return property value, or NULL if there is no such property
318 */
319const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
320
321/**
Simon Glass9e512042017-05-18 20:08:58 -0600322 * ofnode_read_string() - Read a string from a property
323 *
Simon Glassa8167d82020-01-27 08:49:44 -0700324 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600325 * @propname: name of the property to read
326 * @return string from property value, or NULL if there is no such property
327 */
328const char *ofnode_read_string(ofnode node, const char *propname);
329
330/**
Simon Glassbed77492017-05-18 20:09:01 -0600331 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glass9e512042017-05-18 20:08:58 -0600332 *
333 * @node: valid node reference to read property from
334 * @propname: name of the property to read
335 * @out_values: pointer to return value, modified only if return value is 0
336 * @sz: number of array elements to read
Simon Glassfbe8d032018-06-11 13:07:11 -0600337 * @return 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600338 *
339 * Search for a property in a device node and read 32-bit value(s) from
340 * it. Returns 0 on success, -EINVAL if the property does not exist,
341 * -ENODATA if property does not have a value, and -EOVERFLOW if the
342 * property data isn't large enough.
343 *
344 * The out_values is modified only if a valid u32 value can be decoded.
345 */
346int ofnode_read_u32_array(ofnode node, const char *propname,
347 u32 *out_values, size_t sz);
Simon Glass0de1b072020-11-28 17:50:02 -0700348/**
349 * ofnode_is_enabled() - Checks whether a node is enabled.
350 * This looks for a 'status' property. If this exists, then returns true if
351 * the status is 'okay' and false otherwise. If there is no status property,
352 * it returns true on the assumption that anything mentioned should be enabled
353 * by default.
354 *
355 * @node: node to examine
356 * @return false (not enabled) or true (enabled)
357 */
358bool ofnode_is_enabled(ofnode node);
Simon Glass9e512042017-05-18 20:08:58 -0600359
360/**
361 * ofnode_read_bool() - read a boolean value from a property
362 *
363 * @node: valid node reference to read property from
364 * @propname: name of property to read
365 * @return true if property is present (meaning true), false if not present
366 */
367bool ofnode_read_bool(ofnode node, const char *propname);
368
369/**
370 * ofnode_find_subnode() - find a named subnode of a parent node
371 *
372 * @node: valid reference to parent node
373 * @subnode_name: name of subnode to find
374 * @return reference to subnode (which can be invalid if there is no such
375 * subnode)
376 */
377ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
378
379/**
380 * ofnode_first_subnode() - find the first subnode of a parent node
381 *
382 * @node: valid reference to a valid parent node
383 * @return reference to the first subnode (which can be invalid if the parent
384 * node has no subnodes)
385 */
386ofnode ofnode_first_subnode(ofnode node);
387
388/**
389 * ofnode_next_subnode() - find the next sibling of a subnode
390 *
391 * @node: valid reference to previous node (sibling)
392 * @return reference to the next subnode (which can be invalid if the node
393 * has no more siblings)
394 */
395ofnode ofnode_next_subnode(ofnode node);
396
397/**
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100398 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
399 *
400 * @node: valid node to look up
401 * @return ofnode reference of the parent node
402 */
403ofnode ofnode_get_parent(ofnode node);
404
405/**
Simon Glass9e512042017-05-18 20:08:58 -0600406 * ofnode_get_name() - get the name of a node
407 *
408 * @node: valid node to look up
Baruch Siach33810b42018-11-18 14:39:20 +0200409 * @return name of node
Simon Glass9e512042017-05-18 20:08:58 -0600410 */
411const char *ofnode_get_name(ofnode node);
412
413/**
Kever Yangb4f20762018-02-23 17:38:50 +0100414 * ofnode_get_by_phandle() - get ofnode from phandle
415 *
416 * @phandle: phandle to look up
417 * @return ofnode reference to the phandle
418 */
419ofnode ofnode_get_by_phandle(uint phandle);
420
421/**
Simon Glass9e512042017-05-18 20:08:58 -0600422 * ofnode_read_size() - read the size of a property
423 *
424 * @node: node to check
425 * @propname: property to check
426 * @return size of property if present, or -EINVAL if not
427 */
428int ofnode_read_size(ofnode node, const char *propname);
429
430/**
Keerthye679d032019-04-24 17:19:53 +0530431 * ofnode_get_addr_size_index() - get an address/size from a node
432 * based on index
433 *
434 * This reads the register address/size from a node based on index
435 *
436 * @node: node to read from
437 * @index: Index of address to read (0 for first)
438 * @size: Pointer to size of the address
439 * @return address, or FDT_ADDR_T_NONE if not present or invalid
440 */
441phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
442 fdt_size_t *size);
443
444/**
Simon Glassbed77492017-05-18 20:09:01 -0600445 * ofnode_get_addr_index() - get an address from a node
446 *
447 * This reads the register address from a node
448 *
449 * @node: node to read from
450 * @index: Index of address to read (0 for first)
451 * @return address, or FDT_ADDR_T_NONE if not present or invalid
452 */
453phys_addr_t ofnode_get_addr_index(ofnode node, int index);
454
455/**
456 * ofnode_get_addr() - get an address from a node
457 *
458 * This reads the register address from a node
459 *
460 * @node: node to read from
461 * @return address, or FDT_ADDR_T_NONE if not present or invalid
462 */
463phys_addr_t ofnode_get_addr(ofnode node);
464
465/**
Simon Glass9e512042017-05-18 20:08:58 -0600466 * ofnode_stringlist_search() - find a string in a string list and return index
467 *
468 * Note that it is possible for this function to succeed on property values
469 * that are not NUL-terminated. That's because the function will stop after
470 * finding the first occurrence of @string. This can for example happen with
471 * small-valued cell properties, such as #address-cells, when searching for
472 * the empty string.
473 *
474 * @node: node to check
475 * @propname: name of the property containing the string list
476 * @string: string to look up in the string list
477 *
478 * @return:
479 * the index of the string in the list of strings
480 * -ENODATA if the property is not found
481 * -EINVAL on some other error
482 */
483int ofnode_stringlist_search(ofnode node, const char *propname,
484 const char *string);
485
486/**
Simon Glass8c293d62017-06-12 06:21:28 -0600487 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glass9e512042017-05-18 20:08:58 -0600488 *
489 * Note that this will successfully extract strings from properties with
490 * non-NUL-terminated values. For example on small-valued cell properties
491 * this function will return the empty string.
492 *
493 * If non-NULL, the length of the string (on success) or a negative error-code
494 * (on failure) will be stored in the integer pointer to by lenp.
495 *
496 * @node: node to check
497 * @propname: name of the property containing the string list
498 * @index: index of the string to return
499 * @lenp: return location for the string length or an error code on failure
500 *
501 * @return:
502 * length of string, if found or -ve error value if not found
503 */
504int ofnode_read_string_index(ofnode node, const char *propname, int index,
505 const char **outp);
506
507/**
Simon Glass8c293d62017-06-12 06:21:28 -0600508 * ofnode_read_string_count() - find the number of strings in a string list
509 *
510 * @node: node to check
511 * @propname: name of the property containing the string list
512 * @return:
513 * number of strings in the list, or -ve error value if not found
514 */
515int ofnode_read_string_count(ofnode node, const char *property);
516
517/**
Simon Glass9e512042017-05-18 20:08:58 -0600518 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
519 *
520 * This function is useful to parse lists of phandles and their arguments.
521 * Returns 0 on success and fills out_args, on error returns appropriate
522 * errno value.
523 *
524 * Caller is responsible to call of_node_put() on the returned out_args->np
525 * pointer.
526 *
527 * Example:
528 *
529 * phandle1: node1 {
530 * #list-cells = <2>;
531 * }
532 *
533 * phandle2: node2 {
534 * #list-cells = <1>;
535 * }
536 *
537 * node3 {
538 * list = <&phandle1 1 2 &phandle2 3>;
539 * }
540 *
541 * To get a device_node of the `node2' node you may call this:
542 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
543 *
544 * @node: device tree node containing a list
545 * @list_name: property name that contains a list
546 * @cells_name: property name that specifies phandles' arguments count
547 * @cells_count: Cell count to use if @cells_name is NULL
548 * @index: index of a phandle to parse out
549 * @out_args: optional pointer to output arguments structure (will be filled)
550 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
551 * @list_name does not exist, -EINVAL if a phandle was not found,
552 * @cells_name could not be found, the arguments were truncated or there
553 * were too many arguments.
554 */
555int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
556 const char *cells_name, int cell_count,
557 int index,
558 struct ofnode_phandle_args *out_args);
559
560/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200561 * ofnode_count_phandle_with_args() - Count number of phandle in a list
562 *
563 * This function is useful to count phandles into a list.
564 * Returns number of phandle on success, on error returns appropriate
565 * errno value.
566 *
567 * @node: device tree node containing a list
568 * @list_name: property name that contains a list
569 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay89f68302020-09-25 09:41:14 +0200570 * @cells_count: Cell count to use if @cells_name is NULL
Patrice Chotard642346a2017-07-18 11:57:08 +0200571 * @return number of phandle on success, -ENOENT if @list_name does not
572 * exist, -EINVAL if a phandle was not found, @cells_name could not
573 * be found.
574 */
575int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200576 const char *cells_name, int cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +0200577
578/**
Simon Glass9e512042017-05-18 20:08:58 -0600579 * ofnode_path() - find a node by full path
580 *
581 * @path: Full path to node, e.g. "/bus/spi@1"
582 * @return reference to the node found. Use ofnode_valid() to check if it exists
583 */
584ofnode ofnode_path(const char *path);
585
586/**
Simon Glassbd933bf2020-01-27 08:49:46 -0700587 * ofnode_read_chosen_prop() - get the value of a chosen property
588 *
589 * This looks for a property within the /chosen node and returns its value
590 *
591 * @propname: Property name to look for
592 * @sizep: Returns size of property, or FDT_ERR_... error code if function
593 * returns NULL
594 * @return property value if found, else NULL
595 */
596const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
597
598/**
Simon Glass14ca9f72020-01-27 08:49:43 -0700599 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glass9e512042017-05-18 20:08:58 -0600600 *
Simon Glass14ca9f72020-01-27 08:49:43 -0700601 * This looks for a property within the /chosen node and returns its value,
602 * checking that it is a valid nul-terminated string
Simon Glass9e512042017-05-18 20:08:58 -0600603 *
604 * @propname: Property name to look for
Simon Glass14ca9f72020-01-27 08:49:43 -0700605 * @return string value if found, else NULL
Simon Glass9e512042017-05-18 20:08:58 -0600606 */
Simon Glass14ca9f72020-01-27 08:49:43 -0700607const char *ofnode_read_chosen_string(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600608
609/**
Simon Glass74d594a2020-01-27 08:49:42 -0700610 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glass9e512042017-05-18 20:08:58 -0600611 *
Simon Glass74d594a2020-01-27 08:49:42 -0700612 * This looks up a named property in the chosen node and uses that as a path to
613 * look up a code.
614 *
615 * @return the referenced node if present, else ofnode_null()
Simon Glass9e512042017-05-18 20:08:58 -0600616 */
Simon Glass74d594a2020-01-27 08:49:42 -0700617ofnode ofnode_get_chosen_node(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600618
Michal Simek305d3182020-07-28 12:51:08 +0200619/**
620 * ofnode_read_aliases_prop() - get the value of a aliases property
621 *
622 * This looks for a property within the /aliases node and returns its value
623 *
624 * @propname: Property name to look for
625 * @sizep: Returns size of property, or FDT_ERR_... error code if function
626 * returns NULL
627 * @return property value if found, else NULL
628 */
629const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
630
631/**
632 * ofnode_get_aliases_node() - get a referenced node from the aliases node
633 *
634 * This looks up a named property in the aliases node and uses that as a path to
635 * look up a code.
636 *
637 * @return the referenced node if present, else ofnode_null()
638 */
639ofnode ofnode_get_aliases_node(const char *propname);
640
Simon Glass9e512042017-05-18 20:08:58 -0600641struct display_timing;
642/**
643 * ofnode_decode_display_timing() - decode display timings
644 *
645 * Decode display timings from the supplied 'display-timings' node.
646 * See doc/device-tree-bindings/video/display-timing.txt for binding
647 * information.
648 *
649 * @node 'display-timing' node containing the timing subnodes
650 * @index Index number to read (0=first timing subnode)
651 * @config Place to put timings
652 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
653 */
654int ofnode_decode_display_timing(ofnode node, int index,
655 struct display_timing *config);
656
657/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100658 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glass9e512042017-05-18 20:08:58 -0600659 *
660 * @node: node to read
661 * @propname: property to read
662 * @lenp: place to put length on success
663 * @return pointer to property, or NULL if not found
664 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900665const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glass9e512042017-05-18 20:08:58 -0600666
667/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100668 * ofnode_get_first_property()- get the reference of the first property
669 *
670 * Get reference to the first property of the node, it is used to iterate
671 * and read all the property with ofnode_get_property_by_prop().
672 *
673 * @node: node to read
674 * @prop: place to put argument reference
675 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
676 */
677int ofnode_get_first_property(ofnode node, struct ofprop *prop);
678
679/**
680 * ofnode_get_next_property() - get the reference of the next property
681 *
682 * Get reference to the next property of the node, it is used to iterate
683 * and read all the property with ofnode_get_property_by_prop().
684 *
685 * @prop: reference of current argument and place to put reference of next one
686 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
687 */
688int ofnode_get_next_property(struct ofprop *prop);
689
690/**
691 * ofnode_get_property_by_prop() - get a pointer to the value of a property
692 *
693 * Get value for the property identified by the provided reference.
694 *
695 * @prop: reference on property
696 * @propname: If non-NULL, place to property name on success,
697 * @lenp: If non-NULL, place to put length on success
698 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
699 */
700const void *ofnode_get_property_by_prop(const struct ofprop *prop,
701 const char **propname, int *lenp);
702
703/**
Simon Glass9e512042017-05-18 20:08:58 -0600704 * ofnode_is_available() - check if a node is marked available
705 *
706 * @node: node to check
707 * @return true if node's 'status' property is "okay" (or is missing)
708 */
709bool ofnode_is_available(ofnode node);
710
711/**
712 * ofnode_get_addr_size() - get address and size from a property
713 *
714 * This does no address translation. It simply reads an property that contains
715 * an address and a size value, one after the other.
716 *
717 * @node: node to read from
718 * @propname: property to read
719 * @sizep: place to put size value (on success)
720 * @return address value, or FDT_ADDR_T_NONE on error
721 */
722phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
723 phys_size_t *sizep);
724
725/**
726 * ofnode_read_u8_array_ptr() - find an 8-bit array
727 *
728 * Look up a property in a node and return a pointer to its contents as a
729 * byte array of given length. The property must have at least enough data
730 * for the array (count bytes). It may have more, but this will be ignored.
731 * The data is not copied.
732 *
733 * @node node to examine
734 * @propname name of property to find
735 * @sz number of array elements
736 * @return pointer to byte array if found, or NULL if the property is not
737 * found or there is not enough data
738 */
739const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
740 size_t sz);
741
742/**
743 * ofnode_read_pci_addr() - look up a PCI address
744 *
745 * Look at an address property in a node and return the PCI address which
746 * corresponds to the given type in the form of fdt_pci_addr.
747 * The property must hold one fdt_pci_addr with a lengh.
748 *
749 * @node node to examine
750 * @type pci address type (FDT_PCI_SPACE_xxx)
751 * @propname name of property to find
752 * @addr returns pci address in the form of fdt_pci_addr
753 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
754 * format of the property was invalid, -ENXIO if the requested
755 * address type was not found
756 */
757int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
758 const char *propname, struct fdt_pci_addr *addr);
759
760/**
Bin Meng7b9cbad2018-08-03 01:14:35 -0700761 * ofnode_read_pci_vendev() - look up PCI vendor and device id
762 *
763 * Look at the compatible property of a device node that represents a PCI
764 * device and extract pci vendor id and device id from it.
765 *
766 * @param node node to examine
767 * @param vendor vendor id of the pci device
768 * @param device device id of the pci device
769 * @return 0 if ok, negative on error
770 */
771int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
772
773/**
Simon Glass9e512042017-05-18 20:08:58 -0600774 * ofnode_read_addr_cells() - Get the number of address cells for a node
775 *
776 * This walks back up the tree to find the closest #address-cells property
777 * which controls the given node.
778 *
779 * @node: Node to check
780 * @return number of address cells this node uses
781 */
782int ofnode_read_addr_cells(ofnode node);
783
784/**
785 * ofnode_read_size_cells() - Get the number of size cells for a node
786 *
787 * This walks back up the tree to find the closest #size-cells property
788 * which controls the given node.
789 *
790 * @node: Node to check
791 * @return number of size cells this node uses
792 */
793int ofnode_read_size_cells(ofnode node);
794
795/**
Simon Glass878d68c2017-06-12 06:21:31 -0600796 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
797 *
798 * This function matches fdt_address_cells().
799 *
800 * @np: Node pointer to check
801 * @return value of #address-cells property in this node, or 2 if none
802 */
803int ofnode_read_simple_addr_cells(ofnode node);
804
805/**
806 * ofnode_read_simple_size_cells() - Get the size cells property in a node
807 *
808 * This function matches fdt_size_cells().
809 *
810 * @np: Node pointer to check
811 * @return value of #size-cells property in this node, or 2 if none
812 */
813int ofnode_read_simple_size_cells(ofnode node);
814
815/**
Simon Glass9e512042017-05-18 20:08:58 -0600816 * ofnode_pre_reloc() - check if a node should be bound before relocation
817 *
818 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
819 * via special device tree properties.
820 *
821 * Before relocation this function can be used to check if nodes are required
822 * in either SPL or TPL stages.
823 *
824 * After relocation and jumping into the real U-Boot binary it is possible to
825 * determine if a node was bound in one of SPL/TPL stages.
826 *
Patrick Delaunay54e12232019-05-21 19:19:13 +0200827 * There are 4 settings currently in use
828 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glass9e512042017-05-18 20:08:58 -0600829 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
830 * Existing platforms only use it to indicate nodes needed in
831 * SPL. Should probably be replaced by u-boot,dm-spl for
832 * new platforms.
Patrick Delaunay54e12232019-05-21 19:19:13 +0200833 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
834 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glass9e512042017-05-18 20:08:58 -0600835 *
836 * @node: node to check
Simon Glassfbe8d032018-06-11 13:07:11 -0600837 * @return true if node is needed in SPL/TL, false otherwise
Simon Glass9e512042017-05-18 20:08:58 -0600838 */
839bool ofnode_pre_reloc(ofnode node);
840
Simon Glassc98ad442018-06-11 13:07:12 -0600841/**
842 * ofnode_read_resource() - Read a resource from a node
843 *
844 * Read resource information from a node at the given index
845 *
846 * @node: Node to read from
847 * @index: Index of resource to read (0 = first)
848 * @res: Returns resource that was read, on success
849 * @return 0 if OK, -ve on error
850 */
Simon Glassdcf98852017-07-25 08:29:55 -0600851int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassc98ad442018-06-11 13:07:12 -0600852
853/**
854 * ofnode_read_resource_byname() - Read a resource from a node by name
855 *
856 * Read resource information from a node matching the given name. This uses a
857 * 'reg-names' string list property with the names matching the associated
858 * 'reg' property list.
859 *
860 * @node: Node to read from
861 * @name: Name of resource to read
862 * @res: Returns resource that was read, on success
863 * @return 0 if OK, -ve on error
864 */
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900865int ofnode_read_resource_byname(ofnode node, const char *name,
866 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -0600867
Simon Glass3991f422017-08-05 15:45:54 -0600868/**
Simon Glassc60f6712018-06-11 13:07:13 -0600869 * ofnode_by_compatible() - Find the next compatible node
870 *
871 * Find the next node after @from that is compatible with @compat
872 *
873 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
874 * @compat: Compatible string to match
875 * @return ofnode found, or ofnode_null() if none
876 */
877ofnode ofnode_by_compatible(ofnode from, const char *compat);
878
879/**
Jens Wiklander61fba0f2018-08-20 11:09:58 +0200880 * ofnode_by_prop_value() - Find the next node with given property value
881 *
882 * Find the next node after @from that has a @propname with a value
883 * @propval and a length @proplen.
884 *
885 * @from: ofnode to start from (use ofnode_null() to start at the
886 * beginning) @propname: property name to check @propval: property value to
887 * search for @proplen: length of the value in propval @return ofnode
888 * found, or ofnode_null() if none
889 */
890ofnode ofnode_by_prop_value(ofnode from, const char *propname,
891 const void *propval, int proplen);
892
893/**
Simon Glass3991f422017-08-05 15:45:54 -0600894 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
895 *
896 * @node: child node (ofnode, lvalue)
897 * @parent: parent node (ofnode)
898 *
899 * This is a wrapper around a for loop and is used like so:
900 *
901 * ofnode node;
902 *
903 * ofnode_for_each_subnode(node, parent) {
904 * Use node
905 * ...
906 * }
907 *
908 * Note that this is implemented as a macro and @node is used as
909 * iterator in the loop. The parent variable can be a constant or even a
910 * literal.
911 */
912#define ofnode_for_each_subnode(node, parent) \
913 for (node = ofnode_first_subnode(parent); \
914 ofnode_valid(node); \
915 node = ofnode_next_subnode(node))
916
Mario Six147c6072018-01-15 11:07:19 +0100917/**
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200918 * ofnode_get_child_count() - get the child count of a ofnode
919 *
920 * @node: valid node to get its child count
921 * @return the number of subnodes
922 */
923int ofnode_get_child_count(ofnode parent);
924
925/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200926 * ofnode_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +0100927 *
928 * Translate an address from the device-tree into a CPU physical address. This
929 * function walks up the tree and applies the various bus mappings along the
930 * way.
931 *
932 * @ofnode: Device tree node giving the context in which to translate the
933 * address
934 * @in_addr: pointer to the address to translate
935 * @return the translated address; OF_BAD_ADDR on error
936 */
937u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +0900938
939/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200940 * ofnode_translate_dma_address() - Translate a device-tree DMA address
941 *
942 * Translate a DMA address from the device-tree into a CPU physical address.
943 * This function walks up the tree and applies the various bus mappings along
944 * the way.
945 *
946 * @ofnode: Device tree node giving the context in which to translate the
947 * DMA address
948 * @in_addr: pointer to the DMA address to translate
949 * @return the translated DMA address; OF_BAD_ADDR on error
950 */
951u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
952
953/**
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +0900954 * ofnode_device_is_compatible() - check if the node is compatible with compat
955 *
956 * This allows to check whether the node is comaptible with the compat.
957 *
958 * @node: Device tree node for which compatible needs to be verified.
959 * @compat: Compatible string which needs to verified in the given node.
960 * @return true if OK, false if the compatible is not found
961 */
962int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Sixe369e582018-06-26 08:46:48 +0200963
964/**
965 * ofnode_write_prop() - Set a property of a ofnode
966 *
967 * Note that the value passed to the function is *not* allocated by the
968 * function itself, but must be allocated by the caller if necessary.
969 *
970 * @node: The node for whose property should be set
971 * @propname: The name of the property to set
972 * @len: The length of the new value of the property
973 * @value: The new value of the property (must be valid prior to calling
974 * the function)
975 * @return 0 if successful, -ve on error
976 */
977int ofnode_write_prop(ofnode node, const char *propname, int len,
978 const void *value);
979
980/**
981 * ofnode_write_string() - Set a string property of a ofnode
982 *
983 * Note that the value passed to the function is *not* allocated by the
984 * function itself, but must be allocated by the caller if necessary.
985 *
986 * @node: The node for whose string property should be set
987 * @propname: The name of the string property to set
988 * @value: The new value of the string property (must be valid prior to
989 * calling the function)
990 * @return 0 if successful, -ve on error
991 */
992int ofnode_write_string(ofnode node, const char *propname, const char *value);
993
994/**
995 * ofnode_set_enabled() - Enable or disable a device tree node given by its
996 * ofnode
997 *
998 * This function effectively sets the node's "status" property to either "okay"
999 * or "disable", hence making it available for driver model initialization or
1000 * not.
1001 *
1002 * @node: The node to enable
1003 * @value: Flag that tells the function to either disable or enable the
1004 * node
1005 * @return 0 if successful, -ve on error
1006 */
1007int ofnode_set_enabled(ofnode node, bool value);
1008
Simon Glass4984de22017-05-17 17:18:10 -06001009#endif