blob: a0d3df77868434e7ae17df8a72ff5eee456a13fc [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>
13
14/* Enable checks to protect against invalid calls */
15#undef OF_CHECKS
16
Simon Glassdcf98852017-07-25 08:29:55 -060017struct resource;
18
Simon Glass4984de22017-05-17 17:18:10 -060019/**
20 * ofnode - reference to a device tree node
21 *
22 * This union can hold either a straightforward pointer to a struct device_node
23 * in the live device tree, or an offset within the flat device tree. In the
24 * latter case, the pointer value is just the integer offset within the flat DT.
25 *
26 * Thus we can reference nodes in both the live tree (once available) and the
27 * flat tree (until then). Functions are available to translate between an
28 * ofnode and either an offset or a struct device_node *.
29 *
30 * The reference can also hold a null offset, in which case the pointer value
Simon Glass9e512042017-05-18 20:08:58 -060031 * here is NULL. This corresponds to a struct device_node * value of
Simon Glass4984de22017-05-17 17:18:10 -060032 * NULL, or an offset of -1.
33 *
34 * There is no ambiguity as to whether ofnode holds an offset or a node
35 * pointer: when the live tree is active it holds a node pointer, otherwise it
36 * holds an offset. The value itself does not need to be unique and in theory
37 * the same value could point to a valid device node or a valid offset. We
38 * could arrange for a unique value to be used (e.g. by making the pointer
39 * point to an offset within the flat device tree in the case of an offset) but
40 * this increases code size slightly due to the subtraction. Since it offers no
41 * real benefit, the approach described here seems best.
42 *
43 * For now these points use constant types, since we don't allow writing
44 * the DT.
45 *
46 * @np: Pointer to device node, used for live tree
Baruch Siachafc1a782017-11-09 13:44:28 +020047 * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
Simon Glass4984de22017-05-17 17:18:10 -060048 * is not a really a pointer to a node: it is an offset value. See above.
49 */
50typedef union ofnode_union {
51 const struct device_node *np; /* will be used for future live tree */
52 long of_offset;
53} ofnode;
54
Simon Glass9e512042017-05-18 20:08:58 -060055struct ofnode_phandle_args {
56 ofnode node;
57 int args_count;
58 uint32_t args[OF_MAX_PHANDLE_ARGS];
59};
60
61/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +010062 * ofprop - reference to a property of a device tree node
63 *
64 * This struct hold the reference on one property of one node,
65 * using struct ofnode and an offset within the flat device tree or either
66 * a pointer to a struct property in the live device tree.
67 *
68 * Thus we can reference arguments in both the live tree and the flat tree.
69 *
70 * The property reference can also hold a null reference. This corresponds to
71 * a struct property NULL pointer or an offset of -1.
72 *
73 * @node: Pointer to device node
74 * @offset: Pointer into flat device tree, used for flat tree.
75 * @prop: Pointer to property, used for live treee.
76 */
77
78struct ofprop {
79 ofnode node;
80 union {
81 int offset;
82 const struct property *prop;
83 };
84};
85
86/**
Simon Glass9e512042017-05-18 20:08:58 -060087 * _ofnode_to_np() - convert an ofnode to a live DT node pointer
88 *
89 * This cannot be called if the reference contains an offset.
90 *
91 * @node: Reference containing struct device_node * (possibly invalid)
92 * @return pointer to device node (can be NULL)
93 */
94static inline const struct device_node *ofnode_to_np(ofnode node)
95{
96#ifdef OF_CHECKS
97 if (!of_live_active())
98 return NULL;
99#endif
100 return node.np;
101}
102
Simon Glass4984de22017-05-17 17:18:10 -0600103/**
104 * ofnode_to_offset() - convert an ofnode to a flat DT offset
105 *
106 * This cannot be called if the reference contains a node pointer.
107 *
108 * @node: Reference containing offset (possibly invalid)
109 * @return DT offset (can be -1)
110 */
111static inline int ofnode_to_offset(ofnode node)
112{
Simon Glass9e512042017-05-18 20:08:58 -0600113#ifdef OF_CHECKS
114 if (of_live_active())
115 return -1;
116#endif
Simon Glass4984de22017-05-17 17:18:10 -0600117 return node.of_offset;
118}
119
120/**
121 * ofnode_valid() - check if an ofnode is valid
122 *
123 * @return true if the reference contains a valid ofnode, false if it is NULL
124 */
125static inline bool ofnode_valid(ofnode node)
126{
Simon Glass9e512042017-05-18 20:08:58 -0600127 if (of_live_active())
128 return node.np != NULL;
129 else
130 return node.of_offset != -1;
Simon Glass4984de22017-05-17 17:18:10 -0600131}
132
133/**
134 * offset_to_ofnode() - convert a DT offset to an ofnode
135 *
136 * @of_offset: DT offset (either valid, or -1)
137 * @return reference to the associated DT offset
138 */
139static inline ofnode offset_to_ofnode(int of_offset)
140{
141 ofnode node;
142
Simon Glass9e512042017-05-18 20:08:58 -0600143 if (of_live_active())
144 node.np = NULL;
145 else
Simon Glassb14c5332019-12-06 21:41:36 -0700146 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass4984de22017-05-17 17:18:10 -0600147
148 return node;
149}
150
151/**
Simon Glass9e512042017-05-18 20:08:58 -0600152 * np_to_ofnode() - convert a node pointer to an ofnode
153 *
154 * @np: Live node pointer (can be NULL)
155 * @return reference to the associated node pointer
156 */
157static inline ofnode np_to_ofnode(const struct device_node *np)
158{
159 ofnode node;
160
161 node.np = np;
162
163 return node;
164}
165
166/**
167 * ofnode_is_np() - check if a reference is a node pointer
168 *
169 * This function associated that if there is a valid live tree then all
170 * references will use it. This is because using the flat DT when the live tree
171 * is valid is not permitted.
172 *
173 * @node: reference to check (possibly invalid)
174 * @return true if the reference is a live node pointer, false if it is a DT
175 * offset
176 */
177static inline bool ofnode_is_np(ofnode node)
178{
179#ifdef OF_CHECKS
180 /*
181 * Check our assumption that flat tree offsets are not used when a
182 * live tree is in use.
183 */
184 assert(!ofnode_valid(node) ||
185 (of_live_active() ? _ofnode_to_np(node)
186 : _ofnode_to_np(node)));
187#endif
188 return of_live_active() && ofnode_valid(node);
189}
190
191/**
Simon Glass4984de22017-05-17 17:18:10 -0600192 * ofnode_equal() - check if two references are equal
193 *
194 * @return true if equal, else false
195 */
196static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
197{
198 /* We only need to compare the contents */
199 return ref1.of_offset == ref2.of_offset;
200}
201
Simon Glass9e512042017-05-18 20:08:58 -0600202/**
203 * ofnode_null() - Obtain a null ofnode
204 *
205 * This returns an ofnode which points to no node. It works both with the flat
206 * tree and livetree.
207 */
208static inline ofnode ofnode_null(void)
209{
210 ofnode node;
211
212 if (of_live_active())
213 node.np = NULL;
214 else
215 node.of_offset = -1;
216
217 return node;
218}
219
220/**
221 * ofnode_read_u32() - Read a 32-bit integer from a property
222 *
223 * @ref: valid node reference to read property from
224 * @propname: name of the property to read from
225 * @outp: place to put value (if found)
226 * @return 0 if OK, -ve on error
227 */
228int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
229
230/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200231 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
232 *
233 * @ref: valid node reference to read property from
234 * @propname: name of the property to read from
235 * @index: index of the integer to return
236 * @outp: place to put value (if found)
237 * @return 0 if OK, -ve on error
238 */
239int ofnode_read_u32_index(ofnode node, const char *propname, int index,
240 u32 *outp);
241
242/**
Simon Glass9e512042017-05-18 20:08:58 -0600243 * ofnode_read_s32() - Read a 32-bit integer from a property
244 *
245 * @ref: valid node reference to read property from
246 * @propname: name of the property to read from
247 * @outp: place to put value (if found)
248 * @return 0 if OK, -ve on error
249 */
250static inline int ofnode_read_s32(ofnode node, const char *propname,
251 s32 *out_value)
252{
253 return ofnode_read_u32(node, propname, (u32 *)out_value);
254}
255
256/**
257 * ofnode_read_u32_default() - Read a 32-bit integer from a property
258 *
259 * @ref: valid node reference to read property from
260 * @propname: name of the property to read from
261 * @def: default value to return if the property has no value
262 * @return property value, or @def if not found
263 */
Trent Piephob061ef32019-05-10 17:48:20 +0000264u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
Simon Glass9e512042017-05-18 20:08:58 -0600265
266/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200267 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
268 * property
269 *
270 * @ref: valid node reference to read property from
271 * @propname: name of the property to read from
272 * @index: index of the integer to return
273 * @def: default value to return if the property has no value
274 * @return property value, or @def if not found
275 */
276u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
277 u32 def);
278
279/**
Simon Glass9e512042017-05-18 20:08:58 -0600280 * ofnode_read_s32_default() - Read a 32-bit integer from a property
281 *
282 * @ref: valid node reference to read property from
283 * @propname: name of the property to read from
284 * @def: default value to return if the property has no value
285 * @return property value, or @def if not found
286 */
287int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
288
289/**
Lukas Auerafb30122018-11-22 11:26:35 +0100290 * ofnode_read_u64() - Read a 64-bit integer from a property
291 *
292 * @node: valid node reference to read property from
293 * @propname: name of the property to read from
294 * @outp: place to put value (if found)
295 * @return 0 if OK, -ve on error
296 */
297int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
298
299/**
Simon Glass7e5196c2018-06-11 13:07:10 -0600300 * ofnode_read_u64_default() - Read a 64-bit integer from a property
301 *
302 * @ref: valid node reference to read property from
303 * @propname: name of the property to read from
304 * @def: default value to return if the property has no value
305 * @return property value, or @def if not found
306 */
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200307u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass7e5196c2018-06-11 13:07:10 -0600308
309/**
Simon Glassa8167d82020-01-27 08:49:44 -0700310 * ofnode_read_prop() - Read a property from a node
311 *
312 * @node: valid node reference to read property from
313 * @propname: name of the property to read
314 * @sizep: if non-NULL, returns the size of the property, or an error code
315 if not found
316 * @return property value, or NULL if there is no such property
317 */
318const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
319
320/**
Simon Glass9e512042017-05-18 20:08:58 -0600321 * ofnode_read_string() - Read a string from a property
322 *
Simon Glassa8167d82020-01-27 08:49:44 -0700323 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600324 * @propname: name of the property to read
325 * @return string from property value, or NULL if there is no such property
326 */
327const char *ofnode_read_string(ofnode node, const char *propname);
328
329/**
Simon Glassbed77492017-05-18 20:09:01 -0600330 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glass9e512042017-05-18 20:08:58 -0600331 *
332 * @node: valid node reference to read property from
333 * @propname: name of the property to read
334 * @out_values: pointer to return value, modified only if return value is 0
335 * @sz: number of array elements to read
Simon Glassfbe8d032018-06-11 13:07:11 -0600336 * @return 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600337 *
338 * Search for a property in a device node and read 32-bit value(s) from
339 * it. Returns 0 on success, -EINVAL if the property does not exist,
340 * -ENODATA if property does not have a value, and -EOVERFLOW if the
341 * property data isn't large enough.
342 *
343 * The out_values is modified only if a valid u32 value can be decoded.
344 */
345int ofnode_read_u32_array(ofnode node, const char *propname,
346 u32 *out_values, size_t sz);
347
348/**
349 * ofnode_read_bool() - read a boolean value from a property
350 *
351 * @node: valid node reference to read property from
352 * @propname: name of property to read
353 * @return true if property is present (meaning true), false if not present
354 */
355bool ofnode_read_bool(ofnode node, const char *propname);
356
357/**
358 * ofnode_find_subnode() - find a named subnode of a parent node
359 *
360 * @node: valid reference to parent node
361 * @subnode_name: name of subnode to find
362 * @return reference to subnode (which can be invalid if there is no such
363 * subnode)
364 */
365ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
366
367/**
368 * ofnode_first_subnode() - find the first subnode of a parent node
369 *
370 * @node: valid reference to a valid parent node
371 * @return reference to the first subnode (which can be invalid if the parent
372 * node has no subnodes)
373 */
374ofnode ofnode_first_subnode(ofnode node);
375
376/**
377 * ofnode_next_subnode() - find the next sibling of a subnode
378 *
379 * @node: valid reference to previous node (sibling)
380 * @return reference to the next subnode (which can be invalid if the node
381 * has no more siblings)
382 */
383ofnode ofnode_next_subnode(ofnode node);
384
385/**
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100386 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
387 *
388 * @node: valid node to look up
389 * @return ofnode reference of the parent node
390 */
391ofnode ofnode_get_parent(ofnode node);
392
393/**
Simon Glass9e512042017-05-18 20:08:58 -0600394 * ofnode_get_name() - get the name of a node
395 *
396 * @node: valid node to look up
Baruch Siach33810b42018-11-18 14:39:20 +0200397 * @return name of node
Simon Glass9e512042017-05-18 20:08:58 -0600398 */
399const char *ofnode_get_name(ofnode node);
400
401/**
Kever Yangb4f20762018-02-23 17:38:50 +0100402 * ofnode_get_by_phandle() - get ofnode from phandle
403 *
404 * @phandle: phandle to look up
405 * @return ofnode reference to the phandle
406 */
407ofnode ofnode_get_by_phandle(uint phandle);
408
409/**
Simon Glass9e512042017-05-18 20:08:58 -0600410 * ofnode_read_size() - read the size of a property
411 *
412 * @node: node to check
413 * @propname: property to check
414 * @return size of property if present, or -EINVAL if not
415 */
416int ofnode_read_size(ofnode node, const char *propname);
417
418/**
Keerthye679d032019-04-24 17:19:53 +0530419 * ofnode_get_addr_size_index() - get an address/size from a node
420 * based on index
421 *
422 * This reads the register address/size from a node based on index
423 *
424 * @node: node to read from
425 * @index: Index of address to read (0 for first)
426 * @size: Pointer to size of the address
427 * @return address, or FDT_ADDR_T_NONE if not present or invalid
428 */
429phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
430 fdt_size_t *size);
431
432/**
Simon Glassbed77492017-05-18 20:09:01 -0600433 * ofnode_get_addr_index() - get an address from a node
434 *
435 * This reads the register address from a node
436 *
437 * @node: node to read from
438 * @index: Index of address to read (0 for first)
439 * @return address, or FDT_ADDR_T_NONE if not present or invalid
440 */
441phys_addr_t ofnode_get_addr_index(ofnode node, int index);
442
443/**
444 * ofnode_get_addr() - get an address from a node
445 *
446 * This reads the register address from a node
447 *
448 * @node: node to read from
449 * @return address, or FDT_ADDR_T_NONE if not present or invalid
450 */
451phys_addr_t ofnode_get_addr(ofnode node);
452
453/**
Simon Glass9e512042017-05-18 20:08:58 -0600454 * ofnode_stringlist_search() - find a string in a string list and return index
455 *
456 * Note that it is possible for this function to succeed on property values
457 * that are not NUL-terminated. That's because the function will stop after
458 * finding the first occurrence of @string. This can for example happen with
459 * small-valued cell properties, such as #address-cells, when searching for
460 * the empty string.
461 *
462 * @node: node to check
463 * @propname: name of the property containing the string list
464 * @string: string to look up in the string list
465 *
466 * @return:
467 * the index of the string in the list of strings
468 * -ENODATA if the property is not found
469 * -EINVAL on some other error
470 */
471int ofnode_stringlist_search(ofnode node, const char *propname,
472 const char *string);
473
474/**
Simon Glass8c293d62017-06-12 06:21:28 -0600475 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glass9e512042017-05-18 20:08:58 -0600476 *
477 * Note that this will successfully extract strings from properties with
478 * non-NUL-terminated values. For example on small-valued cell properties
479 * this function will return the empty string.
480 *
481 * If non-NULL, the length of the string (on success) or a negative error-code
482 * (on failure) will be stored in the integer pointer to by lenp.
483 *
484 * @node: node to check
485 * @propname: name of the property containing the string list
486 * @index: index of the string to return
487 * @lenp: return location for the string length or an error code on failure
488 *
489 * @return:
490 * length of string, if found or -ve error value if not found
491 */
492int ofnode_read_string_index(ofnode node, const char *propname, int index,
493 const char **outp);
494
495/**
Simon Glass8c293d62017-06-12 06:21:28 -0600496 * ofnode_read_string_count() - find the number of strings in a string list
497 *
498 * @node: node to check
499 * @propname: name of the property containing the string list
500 * @return:
501 * number of strings in the list, or -ve error value if not found
502 */
503int ofnode_read_string_count(ofnode node, const char *property);
504
505/**
Simon Glass9e512042017-05-18 20:08:58 -0600506 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
507 *
508 * This function is useful to parse lists of phandles and their arguments.
509 * Returns 0 on success and fills out_args, on error returns appropriate
510 * errno value.
511 *
512 * Caller is responsible to call of_node_put() on the returned out_args->np
513 * pointer.
514 *
515 * Example:
516 *
517 * phandle1: node1 {
518 * #list-cells = <2>;
519 * }
520 *
521 * phandle2: node2 {
522 * #list-cells = <1>;
523 * }
524 *
525 * node3 {
526 * list = <&phandle1 1 2 &phandle2 3>;
527 * }
528 *
529 * To get a device_node of the `node2' node you may call this:
530 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
531 *
532 * @node: device tree node containing a list
533 * @list_name: property name that contains a list
534 * @cells_name: property name that specifies phandles' arguments count
535 * @cells_count: Cell count to use if @cells_name is NULL
536 * @index: index of a phandle to parse out
537 * @out_args: optional pointer to output arguments structure (will be filled)
538 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
539 * @list_name does not exist, -EINVAL if a phandle was not found,
540 * @cells_name could not be found, the arguments were truncated or there
541 * were too many arguments.
542 */
543int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
544 const char *cells_name, int cell_count,
545 int index,
546 struct ofnode_phandle_args *out_args);
547
548/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200549 * ofnode_count_phandle_with_args() - Count number of phandle in a list
550 *
551 * This function is useful to count phandles into a list.
552 * Returns number of phandle on success, on error returns appropriate
553 * errno value.
554 *
555 * @node: device tree node containing a list
556 * @list_name: property name that contains a list
557 * @cells_name: property name that specifies phandles' arguments count
558 * @return number of phandle on success, -ENOENT if @list_name does not
559 * exist, -EINVAL if a phandle was not found, @cells_name could not
560 * be found.
561 */
562int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
563 const char *cells_name);
564
565/**
Simon Glass9e512042017-05-18 20:08:58 -0600566 * ofnode_path() - find a node by full path
567 *
568 * @path: Full path to node, e.g. "/bus/spi@1"
569 * @return reference to the node found. Use ofnode_valid() to check if it exists
570 */
571ofnode ofnode_path(const char *path);
572
573/**
Simon Glassbd933bf2020-01-27 08:49:46 -0700574 * ofnode_read_chosen_prop() - get the value of a chosen property
575 *
576 * This looks for a property within the /chosen node and returns its value
577 *
578 * @propname: Property name to look for
579 * @sizep: Returns size of property, or FDT_ERR_... error code if function
580 * returns NULL
581 * @return property value if found, else NULL
582 */
583const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
584
585/**
Simon Glass14ca9f72020-01-27 08:49:43 -0700586 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glass9e512042017-05-18 20:08:58 -0600587 *
Simon Glass14ca9f72020-01-27 08:49:43 -0700588 * This looks for a property within the /chosen node and returns its value,
589 * checking that it is a valid nul-terminated string
Simon Glass9e512042017-05-18 20:08:58 -0600590 *
591 * @propname: Property name to look for
Simon Glass14ca9f72020-01-27 08:49:43 -0700592 * @return string value if found, else NULL
Simon Glass9e512042017-05-18 20:08:58 -0600593 */
Simon Glass14ca9f72020-01-27 08:49:43 -0700594const char *ofnode_read_chosen_string(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600595
596/**
Simon Glass74d594a2020-01-27 08:49:42 -0700597 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glass9e512042017-05-18 20:08:58 -0600598 *
Simon Glass74d594a2020-01-27 08:49:42 -0700599 * This looks up a named property in the chosen node and uses that as a path to
600 * look up a code.
601 *
602 * @return the referenced node if present, else ofnode_null()
Simon Glass9e512042017-05-18 20:08:58 -0600603 */
Simon Glass74d594a2020-01-27 08:49:42 -0700604ofnode ofnode_get_chosen_node(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600605
606struct display_timing;
607/**
608 * ofnode_decode_display_timing() - decode display timings
609 *
610 * Decode display timings from the supplied 'display-timings' node.
611 * See doc/device-tree-bindings/video/display-timing.txt for binding
612 * information.
613 *
614 * @node 'display-timing' node containing the timing subnodes
615 * @index Index number to read (0=first timing subnode)
616 * @config Place to put timings
617 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
618 */
619int ofnode_decode_display_timing(ofnode node, int index,
620 struct display_timing *config);
621
622/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100623 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glass9e512042017-05-18 20:08:58 -0600624 *
625 * @node: node to read
626 * @propname: property to read
627 * @lenp: place to put length on success
628 * @return pointer to property, or NULL if not found
629 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900630const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glass9e512042017-05-18 20:08:58 -0600631
632/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100633 * ofnode_get_first_property()- get the reference of the first property
634 *
635 * Get reference to the first property of the node, it is used to iterate
636 * and read all the property with ofnode_get_property_by_prop().
637 *
638 * @node: node to read
639 * @prop: place to put argument reference
640 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
641 */
642int ofnode_get_first_property(ofnode node, struct ofprop *prop);
643
644/**
645 * ofnode_get_next_property() - get the reference of the next property
646 *
647 * Get reference to the next property of the node, it is used to iterate
648 * and read all the property with ofnode_get_property_by_prop().
649 *
650 * @prop: reference of current argument and place to put reference of next one
651 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
652 */
653int ofnode_get_next_property(struct ofprop *prop);
654
655/**
656 * ofnode_get_property_by_prop() - get a pointer to the value of a property
657 *
658 * Get value for the property identified by the provided reference.
659 *
660 * @prop: reference on property
661 * @propname: If non-NULL, place to property name on success,
662 * @lenp: If non-NULL, place to put length on success
663 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
664 */
665const void *ofnode_get_property_by_prop(const struct ofprop *prop,
666 const char **propname, int *lenp);
667
668/**
Simon Glass9e512042017-05-18 20:08:58 -0600669 * ofnode_is_available() - check if a node is marked available
670 *
671 * @node: node to check
672 * @return true if node's 'status' property is "okay" (or is missing)
673 */
674bool ofnode_is_available(ofnode node);
675
676/**
677 * ofnode_get_addr_size() - get address and size from a property
678 *
679 * This does no address translation. It simply reads an property that contains
680 * an address and a size value, one after the other.
681 *
682 * @node: node to read from
683 * @propname: property to read
684 * @sizep: place to put size value (on success)
685 * @return address value, or FDT_ADDR_T_NONE on error
686 */
687phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
688 phys_size_t *sizep);
689
690/**
691 * ofnode_read_u8_array_ptr() - find an 8-bit array
692 *
693 * Look up a property in a node and return a pointer to its contents as a
694 * byte array of given length. The property must have at least enough data
695 * for the array (count bytes). It may have more, but this will be ignored.
696 * The data is not copied.
697 *
698 * @node node to examine
699 * @propname name of property to find
700 * @sz number of array elements
701 * @return pointer to byte array if found, or NULL if the property is not
702 * found or there is not enough data
703 */
704const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
705 size_t sz);
706
707/**
708 * ofnode_read_pci_addr() - look up a PCI address
709 *
710 * Look at an address property in a node and return the PCI address which
711 * corresponds to the given type in the form of fdt_pci_addr.
712 * The property must hold one fdt_pci_addr with a lengh.
713 *
714 * @node node to examine
715 * @type pci address type (FDT_PCI_SPACE_xxx)
716 * @propname name of property to find
717 * @addr returns pci address in the form of fdt_pci_addr
718 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
719 * format of the property was invalid, -ENXIO if the requested
720 * address type was not found
721 */
722int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
723 const char *propname, struct fdt_pci_addr *addr);
724
725/**
Bin Meng7b9cbad2018-08-03 01:14:35 -0700726 * ofnode_read_pci_vendev() - look up PCI vendor and device id
727 *
728 * Look at the compatible property of a device node that represents a PCI
729 * device and extract pci vendor id and device id from it.
730 *
731 * @param node node to examine
732 * @param vendor vendor id of the pci device
733 * @param device device id of the pci device
734 * @return 0 if ok, negative on error
735 */
736int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
737
738/**
Simon Glass9e512042017-05-18 20:08:58 -0600739 * ofnode_read_addr_cells() - Get the number of address cells for a node
740 *
741 * This walks back up the tree to find the closest #address-cells property
742 * which controls the given node.
743 *
744 * @node: Node to check
745 * @return number of address cells this node uses
746 */
747int ofnode_read_addr_cells(ofnode node);
748
749/**
750 * ofnode_read_size_cells() - Get the number of size cells for a node
751 *
752 * This walks back up the tree to find the closest #size-cells property
753 * which controls the given node.
754 *
755 * @node: Node to check
756 * @return number of size cells this node uses
757 */
758int ofnode_read_size_cells(ofnode node);
759
760/**
Simon Glass878d68c2017-06-12 06:21:31 -0600761 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
762 *
763 * This function matches fdt_address_cells().
764 *
765 * @np: Node pointer to check
766 * @return value of #address-cells property in this node, or 2 if none
767 */
768int ofnode_read_simple_addr_cells(ofnode node);
769
770/**
771 * ofnode_read_simple_size_cells() - Get the size cells property in a node
772 *
773 * This function matches fdt_size_cells().
774 *
775 * @np: Node pointer to check
776 * @return value of #size-cells property in this node, or 2 if none
777 */
778int ofnode_read_simple_size_cells(ofnode node);
779
780/**
Simon Glass9e512042017-05-18 20:08:58 -0600781 * ofnode_pre_reloc() - check if a node should be bound before relocation
782 *
783 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
784 * via special device tree properties.
785 *
786 * Before relocation this function can be used to check if nodes are required
787 * in either SPL or TPL stages.
788 *
789 * After relocation and jumping into the real U-Boot binary it is possible to
790 * determine if a node was bound in one of SPL/TPL stages.
791 *
Patrick Delaunay54e12232019-05-21 19:19:13 +0200792 * There are 4 settings currently in use
793 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glass9e512042017-05-18 20:08:58 -0600794 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
795 * Existing platforms only use it to indicate nodes needed in
796 * SPL. Should probably be replaced by u-boot,dm-spl for
797 * new platforms.
Patrick Delaunay54e12232019-05-21 19:19:13 +0200798 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
799 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glass9e512042017-05-18 20:08:58 -0600800 *
801 * @node: node to check
Simon Glassfbe8d032018-06-11 13:07:11 -0600802 * @return true if node is needed in SPL/TL, false otherwise
Simon Glass9e512042017-05-18 20:08:58 -0600803 */
804bool ofnode_pre_reloc(ofnode node);
805
Simon Glassc98ad442018-06-11 13:07:12 -0600806/**
807 * ofnode_read_resource() - Read a resource from a node
808 *
809 * Read resource information from a node at the given index
810 *
811 * @node: Node to read from
812 * @index: Index of resource to read (0 = first)
813 * @res: Returns resource that was read, on success
814 * @return 0 if OK, -ve on error
815 */
Simon Glassdcf98852017-07-25 08:29:55 -0600816int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassc98ad442018-06-11 13:07:12 -0600817
818/**
819 * ofnode_read_resource_byname() - Read a resource from a node by name
820 *
821 * Read resource information from a node matching the given name. This uses a
822 * 'reg-names' string list property with the names matching the associated
823 * 'reg' property list.
824 *
825 * @node: Node to read from
826 * @name: Name of resource to read
827 * @res: Returns resource that was read, on success
828 * @return 0 if OK, -ve on error
829 */
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900830int ofnode_read_resource_byname(ofnode node, const char *name,
831 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -0600832
Simon Glass3991f422017-08-05 15:45:54 -0600833/**
Simon Glassc60f6712018-06-11 13:07:13 -0600834 * ofnode_by_compatible() - Find the next compatible node
835 *
836 * Find the next node after @from that is compatible with @compat
837 *
838 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
839 * @compat: Compatible string to match
840 * @return ofnode found, or ofnode_null() if none
841 */
842ofnode ofnode_by_compatible(ofnode from, const char *compat);
843
844/**
Jens Wiklander61fba0f2018-08-20 11:09:58 +0200845 * ofnode_by_prop_value() - Find the next node with given property value
846 *
847 * Find the next node after @from that has a @propname with a value
848 * @propval and a length @proplen.
849 *
850 * @from: ofnode to start from (use ofnode_null() to start at the
851 * beginning) @propname: property name to check @propval: property value to
852 * search for @proplen: length of the value in propval @return ofnode
853 * found, or ofnode_null() if none
854 */
855ofnode ofnode_by_prop_value(ofnode from, const char *propname,
856 const void *propval, int proplen);
857
858/**
Simon Glass3991f422017-08-05 15:45:54 -0600859 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
860 *
861 * @node: child node (ofnode, lvalue)
862 * @parent: parent node (ofnode)
863 *
864 * This is a wrapper around a for loop and is used like so:
865 *
866 * ofnode node;
867 *
868 * ofnode_for_each_subnode(node, parent) {
869 * Use node
870 * ...
871 * }
872 *
873 * Note that this is implemented as a macro and @node is used as
874 * iterator in the loop. The parent variable can be a constant or even a
875 * literal.
876 */
877#define ofnode_for_each_subnode(node, parent) \
878 for (node = ofnode_first_subnode(parent); \
879 ofnode_valid(node); \
880 node = ofnode_next_subnode(node))
881
Mario Six147c6072018-01-15 11:07:19 +0100882/**
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200883 * ofnode_get_child_count() - get the child count of a ofnode
884 *
885 * @node: valid node to get its child count
886 * @return the number of subnodes
887 */
888int ofnode_get_child_count(ofnode parent);
889
890/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200891 * ofnode_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +0100892 *
893 * Translate an address from the device-tree into a CPU physical address. This
894 * function walks up the tree and applies the various bus mappings along the
895 * way.
896 *
897 * @ofnode: Device tree node giving the context in which to translate the
898 * address
899 * @in_addr: pointer to the address to translate
900 * @return the translated address; OF_BAD_ADDR on error
901 */
902u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +0900903
904/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200905 * ofnode_translate_dma_address() - Translate a device-tree DMA address
906 *
907 * Translate a DMA address from the device-tree into a CPU physical address.
908 * This function walks up the tree and applies the various bus mappings along
909 * the way.
910 *
911 * @ofnode: Device tree node giving the context in which to translate the
912 * DMA address
913 * @in_addr: pointer to the DMA address to translate
914 * @return the translated DMA address; OF_BAD_ADDR on error
915 */
916u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
917
918/**
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +0900919 * ofnode_device_is_compatible() - check if the node is compatible with compat
920 *
921 * This allows to check whether the node is comaptible with the compat.
922 *
923 * @node: Device tree node for which compatible needs to be verified.
924 * @compat: Compatible string which needs to verified in the given node.
925 * @return true if OK, false if the compatible is not found
926 */
927int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Sixe369e582018-06-26 08:46:48 +0200928
929/**
930 * ofnode_write_prop() - Set a property of a ofnode
931 *
932 * Note that the value passed to the function is *not* allocated by the
933 * function itself, but must be allocated by the caller if necessary.
934 *
935 * @node: The node for whose property should be set
936 * @propname: The name of the property to set
937 * @len: The length of the new value of the property
938 * @value: The new value of the property (must be valid prior to calling
939 * the function)
940 * @return 0 if successful, -ve on error
941 */
942int ofnode_write_prop(ofnode node, const char *propname, int len,
943 const void *value);
944
945/**
946 * ofnode_write_string() - Set a string property of a ofnode
947 *
948 * Note that the value passed to the function is *not* allocated by the
949 * function itself, but must be allocated by the caller if necessary.
950 *
951 * @node: The node for whose string property should be set
952 * @propname: The name of the string property to set
953 * @value: The new value of the string property (must be valid prior to
954 * calling the function)
955 * @return 0 if successful, -ve on error
956 */
957int ofnode_write_string(ofnode node, const char *propname, const char *value);
958
959/**
960 * ofnode_set_enabled() - Enable or disable a device tree node given by its
961 * ofnode
962 *
963 * This function effectively sets the node's "status" property to either "okay"
964 * or "disable", hence making it available for driver model initialization or
965 * not.
966 *
967 * @node: The node to enable
968 * @value: Flag that tells the function to either disable or enable the
969 * node
970 * @return 0 if successful, -ve on error
971 */
972int ofnode_set_enabled(ofnode node, bool value);
973
Simon Glass4984de22017-05-17 17:18:10 -0600974#endif