blob: ce5e366c06273f643c45a0926752cea08cf56997 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass9a148602017-05-17 17:18:10 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass9a148602017-05-17 17:18:10 -06005 */
6
7#ifndef _DM_OFNODE_H
8#define _DM_OFNODE_H
9
Simon Glassc4fc5622017-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 Glassf7bfcc42017-07-25 08:29:55 -060017struct resource;
18
Simon Glass9a148602017-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 Glassc4fc5622017-05-18 20:08:58 -060031 * here is NULL. This corresponds to a struct device_node * value of
Simon Glass9a148602017-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 Siach6bcca142017-11-09 13:44:28 +020047 * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
Simon Glass9a148602017-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 Glassc4fc5622017-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/**
62 * _ofnode_to_np() - convert an ofnode to a live DT node pointer
63 *
64 * This cannot be called if the reference contains an offset.
65 *
66 * @node: Reference containing struct device_node * (possibly invalid)
67 * @return pointer to device node (can be NULL)
68 */
69static inline const struct device_node *ofnode_to_np(ofnode node)
70{
71#ifdef OF_CHECKS
72 if (!of_live_active())
73 return NULL;
74#endif
75 return node.np;
76}
77
Simon Glass9a148602017-05-17 17:18:10 -060078/**
79 * ofnode_to_offset() - convert an ofnode to a flat DT offset
80 *
81 * This cannot be called if the reference contains a node pointer.
82 *
83 * @node: Reference containing offset (possibly invalid)
84 * @return DT offset (can be -1)
85 */
86static inline int ofnode_to_offset(ofnode node)
87{
Simon Glassc4fc5622017-05-18 20:08:58 -060088#ifdef OF_CHECKS
89 if (of_live_active())
90 return -1;
91#endif
Simon Glass9a148602017-05-17 17:18:10 -060092 return node.of_offset;
93}
94
95/**
96 * ofnode_valid() - check if an ofnode is valid
97 *
98 * @return true if the reference contains a valid ofnode, false if it is NULL
99 */
100static inline bool ofnode_valid(ofnode node)
101{
Simon Glassc4fc5622017-05-18 20:08:58 -0600102 if (of_live_active())
103 return node.np != NULL;
104 else
105 return node.of_offset != -1;
Simon Glass9a148602017-05-17 17:18:10 -0600106}
107
108/**
109 * offset_to_ofnode() - convert a DT offset to an ofnode
110 *
111 * @of_offset: DT offset (either valid, or -1)
112 * @return reference to the associated DT offset
113 */
114static inline ofnode offset_to_ofnode(int of_offset)
115{
116 ofnode node;
117
Simon Glassc4fc5622017-05-18 20:08:58 -0600118 if (of_live_active())
119 node.np = NULL;
120 else
Simon Glass1ab8b892019-12-06 21:41:36 -0700121 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass9a148602017-05-17 17:18:10 -0600122
123 return node;
124}
125
126/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600127 * np_to_ofnode() - convert a node pointer to an ofnode
128 *
129 * @np: Live node pointer (can be NULL)
130 * @return reference to the associated node pointer
131 */
132static inline ofnode np_to_ofnode(const struct device_node *np)
133{
134 ofnode node;
135
136 node.np = np;
137
138 return node;
139}
140
141/**
142 * ofnode_is_np() - check if a reference is a node pointer
143 *
144 * This function associated that if there is a valid live tree then all
145 * references will use it. This is because using the flat DT when the live tree
146 * is valid is not permitted.
147 *
148 * @node: reference to check (possibly invalid)
149 * @return true if the reference is a live node pointer, false if it is a DT
150 * offset
151 */
152static inline bool ofnode_is_np(ofnode node)
153{
154#ifdef OF_CHECKS
155 /*
156 * Check our assumption that flat tree offsets are not used when a
157 * live tree is in use.
158 */
159 assert(!ofnode_valid(node) ||
160 (of_live_active() ? _ofnode_to_np(node)
161 : _ofnode_to_np(node)));
162#endif
163 return of_live_active() && ofnode_valid(node);
164}
165
166/**
Simon Glass9a148602017-05-17 17:18:10 -0600167 * ofnode_equal() - check if two references are equal
168 *
169 * @return true if equal, else false
170 */
171static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
172{
173 /* We only need to compare the contents */
174 return ref1.of_offset == ref2.of_offset;
175}
176
Simon Glassc4fc5622017-05-18 20:08:58 -0600177/**
178 * ofnode_null() - Obtain a null ofnode
179 *
180 * This returns an ofnode which points to no node. It works both with the flat
181 * tree and livetree.
182 */
183static inline ofnode ofnode_null(void)
184{
185 ofnode node;
186
187 if (of_live_active())
188 node.np = NULL;
189 else
190 node.of_offset = -1;
191
192 return node;
193}
194
195/**
196 * ofnode_read_u32() - Read a 32-bit integer from a property
197 *
198 * @ref: valid node reference to read property from
199 * @propname: name of the property to read from
200 * @outp: place to put value (if found)
201 * @return 0 if OK, -ve on error
202 */
203int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
204
205/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200206 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
207 *
208 * @ref: valid node reference to read property from
209 * @propname: name of the property to read from
210 * @index: index of the integer to return
211 * @outp: place to put value (if found)
212 * @return 0 if OK, -ve on error
213 */
214int ofnode_read_u32_index(ofnode node, const char *propname, int index,
215 u32 *outp);
216
217/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600218 * ofnode_read_s32() - Read a 32-bit integer from a property
219 *
220 * @ref: valid node reference to read property from
221 * @propname: name of the property to read from
222 * @outp: place to put value (if found)
223 * @return 0 if OK, -ve on error
224 */
225static inline int ofnode_read_s32(ofnode node, const char *propname,
226 s32 *out_value)
227{
228 return ofnode_read_u32(node, propname, (u32 *)out_value);
229}
230
231/**
232 * ofnode_read_u32_default() - Read a 32-bit integer from a property
233 *
234 * @ref: valid node reference to read property from
235 * @propname: name of the property to read from
236 * @def: default value to return if the property has no value
237 * @return property value, or @def if not found
238 */
Trent Piepho5b775412019-05-10 17:48:20 +0000239u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
Simon Glassc4fc5622017-05-18 20:08:58 -0600240
241/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200242 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
243 * property
244 *
245 * @ref: valid node reference to read property from
246 * @propname: name of the property to read from
247 * @index: index of the integer to return
248 * @def: default value to return if the property has no value
249 * @return property value, or @def if not found
250 */
251u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
252 u32 def);
253
254/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600255 * ofnode_read_s32_default() - Read a 32-bit integer from a property
256 *
257 * @ref: valid node reference to read property from
258 * @propname: name of the property to read from
259 * @def: default value to return if the property has no value
260 * @return property value, or @def if not found
261 */
262int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
263
264/**
Lukas Auerb03a60b2018-11-22 11:26:35 +0100265 * ofnode_read_u64() - Read a 64-bit integer from a property
266 *
267 * @node: valid node reference to read property from
268 * @propname: name of the property to read from
269 * @outp: place to put value (if found)
270 * @return 0 if OK, -ve on error
271 */
272int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
273
274/**
Simon Glass9d54a7a2018-06-11 13:07:10 -0600275 * ofnode_read_u64_default() - Read a 64-bit integer from a property
276 *
277 * @ref: valid node reference to read property from
278 * @propname: name of the property to read from
279 * @def: default value to return if the property has no value
280 * @return property value, or @def if not found
281 */
T Karthik Reddy478860d2019-09-02 16:34:30 +0200282u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass9d54a7a2018-06-11 13:07:10 -0600283
284/**
Simon Glass0c2e9802020-01-27 08:49:44 -0700285 * ofnode_read_prop() - Read a property from a node
286 *
287 * @node: valid node reference to read property from
288 * @propname: name of the property to read
289 * @sizep: if non-NULL, returns the size of the property, or an error code
290 if not found
291 * @return property value, or NULL if there is no such property
292 */
293const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
294
295/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600296 * ofnode_read_string() - Read a string from a property
297 *
Simon Glass0c2e9802020-01-27 08:49:44 -0700298 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600299 * @propname: name of the property to read
300 * @return string from property value, or NULL if there is no such property
301 */
302const char *ofnode_read_string(ofnode node, const char *propname);
303
304/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600305 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glassc4fc5622017-05-18 20:08:58 -0600306 *
307 * @node: valid node reference to read property from
308 * @propname: name of the property to read
309 * @out_values: pointer to return value, modified only if return value is 0
310 * @sz: number of array elements to read
Simon Glass1c682342018-06-11 13:07:11 -0600311 * @return 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600312 *
313 * Search for a property in a device node and read 32-bit value(s) from
314 * it. Returns 0 on success, -EINVAL if the property does not exist,
315 * -ENODATA if property does not have a value, and -EOVERFLOW if the
316 * property data isn't large enough.
317 *
318 * The out_values is modified only if a valid u32 value can be decoded.
319 */
320int ofnode_read_u32_array(ofnode node, const char *propname,
321 u32 *out_values, size_t sz);
322
323/**
324 * ofnode_read_bool() - read a boolean value from a property
325 *
326 * @node: valid node reference to read property from
327 * @propname: name of property to read
328 * @return true if property is present (meaning true), false if not present
329 */
330bool ofnode_read_bool(ofnode node, const char *propname);
331
332/**
333 * ofnode_find_subnode() - find a named subnode of a parent node
334 *
335 * @node: valid reference to parent node
336 * @subnode_name: name of subnode to find
337 * @return reference to subnode (which can be invalid if there is no such
338 * subnode)
339 */
340ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
341
342/**
343 * ofnode_first_subnode() - find the first subnode of a parent node
344 *
345 * @node: valid reference to a valid parent node
346 * @return reference to the first subnode (which can be invalid if the parent
347 * node has no subnodes)
348 */
349ofnode ofnode_first_subnode(ofnode node);
350
351/**
352 * ofnode_next_subnode() - find the next sibling of a subnode
353 *
354 * @node: valid reference to previous node (sibling)
355 * @return reference to the next subnode (which can be invalid if the node
356 * has no more siblings)
357 */
358ofnode ofnode_next_subnode(ofnode node);
359
360/**
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100361 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
362 *
363 * @node: valid node to look up
364 * @return ofnode reference of the parent node
365 */
366ofnode ofnode_get_parent(ofnode node);
367
368/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600369 * ofnode_get_name() - get the name of a node
370 *
371 * @node: valid node to look up
Baruch Siachf14cb212018-11-18 14:39:20 +0200372 * @return name of node
Simon Glassc4fc5622017-05-18 20:08:58 -0600373 */
374const char *ofnode_get_name(ofnode node);
375
376/**
Kever Yang37df0e02018-02-23 17:38:50 +0100377 * ofnode_get_by_phandle() - get ofnode from phandle
378 *
379 * @phandle: phandle to look up
380 * @return ofnode reference to the phandle
381 */
382ofnode ofnode_get_by_phandle(uint phandle);
383
384/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600385 * ofnode_read_size() - read the size of a property
386 *
387 * @node: node to check
388 * @propname: property to check
389 * @return size of property if present, or -EINVAL if not
390 */
391int ofnode_read_size(ofnode node, const char *propname);
392
393/**
Keerthyd332e6e2019-04-24 17:19:53 +0530394 * ofnode_get_addr_size_index() - get an address/size from a node
395 * based on index
396 *
397 * This reads the register address/size from a node based on index
398 *
399 * @node: node to read from
400 * @index: Index of address to read (0 for first)
401 * @size: Pointer to size of the address
402 * @return address, or FDT_ADDR_T_NONE if not present or invalid
403 */
404phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
405 fdt_size_t *size);
406
407/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600408 * ofnode_get_addr_index() - get an address from a node
409 *
410 * This reads the register address from a node
411 *
412 * @node: node to read from
413 * @index: Index of address to read (0 for first)
414 * @return address, or FDT_ADDR_T_NONE if not present or invalid
415 */
416phys_addr_t ofnode_get_addr_index(ofnode node, int index);
417
418/**
419 * ofnode_get_addr() - get an address from a node
420 *
421 * This reads the register address from a node
422 *
423 * @node: node to read from
424 * @return address, or FDT_ADDR_T_NONE if not present or invalid
425 */
426phys_addr_t ofnode_get_addr(ofnode node);
427
428/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600429 * ofnode_stringlist_search() - find a string in a string list and return index
430 *
431 * Note that it is possible for this function to succeed on property values
432 * that are not NUL-terminated. That's because the function will stop after
433 * finding the first occurrence of @string. This can for example happen with
434 * small-valued cell properties, such as #address-cells, when searching for
435 * the empty string.
436 *
437 * @node: node to check
438 * @propname: name of the property containing the string list
439 * @string: string to look up in the string list
440 *
441 * @return:
442 * the index of the string in the list of strings
443 * -ENODATA if the property is not found
444 * -EINVAL on some other error
445 */
446int ofnode_stringlist_search(ofnode node, const char *propname,
447 const char *string);
448
449/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600450 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glassc4fc5622017-05-18 20:08:58 -0600451 *
452 * Note that this will successfully extract strings from properties with
453 * non-NUL-terminated values. For example on small-valued cell properties
454 * this function will return the empty string.
455 *
456 * If non-NULL, the length of the string (on success) or a negative error-code
457 * (on failure) will be stored in the integer pointer to by lenp.
458 *
459 * @node: node to check
460 * @propname: name of the property containing the string list
461 * @index: index of the string to return
462 * @lenp: return location for the string length or an error code on failure
463 *
464 * @return:
465 * length of string, if found or -ve error value if not found
466 */
467int ofnode_read_string_index(ofnode node, const char *propname, int index,
468 const char **outp);
469
470/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600471 * ofnode_read_string_count() - find the number of strings in a string list
472 *
473 * @node: node to check
474 * @propname: name of the property containing the string list
475 * @return:
476 * number of strings in the list, or -ve error value if not found
477 */
478int ofnode_read_string_count(ofnode node, const char *property);
479
480/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600481 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
482 *
483 * This function is useful to parse lists of phandles and their arguments.
484 * Returns 0 on success and fills out_args, on error returns appropriate
485 * errno value.
486 *
487 * Caller is responsible to call of_node_put() on the returned out_args->np
488 * pointer.
489 *
490 * Example:
491 *
492 * phandle1: node1 {
493 * #list-cells = <2>;
494 * }
495 *
496 * phandle2: node2 {
497 * #list-cells = <1>;
498 * }
499 *
500 * node3 {
501 * list = <&phandle1 1 2 &phandle2 3>;
502 * }
503 *
504 * To get a device_node of the `node2' node you may call this:
505 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
506 *
507 * @node: device tree node containing a list
508 * @list_name: property name that contains a list
509 * @cells_name: property name that specifies phandles' arguments count
510 * @cells_count: Cell count to use if @cells_name is NULL
511 * @index: index of a phandle to parse out
512 * @out_args: optional pointer to output arguments structure (will be filled)
513 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
514 * @list_name does not exist, -EINVAL if a phandle was not found,
515 * @cells_name could not be found, the arguments were truncated or there
516 * were too many arguments.
517 */
518int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
519 const char *cells_name, int cell_count,
520 int index,
521 struct ofnode_phandle_args *out_args);
522
523/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200524 * ofnode_count_phandle_with_args() - Count number of phandle in a list
525 *
526 * This function is useful to count phandles into a list.
527 * Returns number of phandle on success, on error returns appropriate
528 * errno value.
529 *
530 * @node: device tree node containing a list
531 * @list_name: property name that contains a list
532 * @cells_name: property name that specifies phandles' arguments count
533 * @return number of phandle on success, -ENOENT if @list_name does not
534 * exist, -EINVAL if a phandle was not found, @cells_name could not
535 * be found.
536 */
537int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
538 const char *cells_name);
539
540/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600541 * ofnode_path() - find a node by full path
542 *
543 * @path: Full path to node, e.g. "/bus/spi@1"
544 * @return reference to the node found. Use ofnode_valid() to check if it exists
545 */
546ofnode ofnode_path(const char *path);
547
548/**
Simon Glasse09223c2020-01-27 08:49:46 -0700549 * ofnode_read_chosen_prop() - get the value of a chosen property
550 *
551 * This looks for a property within the /chosen node and returns its value
552 *
553 * @propname: Property name to look for
554 * @sizep: Returns size of property, or FDT_ERR_... error code if function
555 * returns NULL
556 * @return property value if found, else NULL
557 */
558const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
559
560/**
Simon Glassf3455962020-01-27 08:49:43 -0700561 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glassc4fc5622017-05-18 20:08:58 -0600562 *
Simon Glassf3455962020-01-27 08:49:43 -0700563 * This looks for a property within the /chosen node and returns its value,
564 * checking that it is a valid nul-terminated string
Simon Glassc4fc5622017-05-18 20:08:58 -0600565 *
566 * @propname: Property name to look for
Simon Glassf3455962020-01-27 08:49:43 -0700567 * @return string value if found, else NULL
Simon Glassc4fc5622017-05-18 20:08:58 -0600568 */
Simon Glassf3455962020-01-27 08:49:43 -0700569const char *ofnode_read_chosen_string(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600570
571/**
Simon Glassc99ba912020-01-27 08:49:42 -0700572 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glassc4fc5622017-05-18 20:08:58 -0600573 *
Simon Glassc99ba912020-01-27 08:49:42 -0700574 * This looks up a named property in the chosen node and uses that as a path to
575 * look up a code.
576 *
577 * @return the referenced node if present, else ofnode_null()
Simon Glassc4fc5622017-05-18 20:08:58 -0600578 */
Simon Glassc99ba912020-01-27 08:49:42 -0700579ofnode ofnode_get_chosen_node(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600580
581struct display_timing;
582/**
583 * ofnode_decode_display_timing() - decode display timings
584 *
585 * Decode display timings from the supplied 'display-timings' node.
586 * See doc/device-tree-bindings/video/display-timing.txt for binding
587 * information.
588 *
589 * @node 'display-timing' node containing the timing subnodes
590 * @index Index number to read (0=first timing subnode)
591 * @config Place to put timings
592 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
593 */
594int ofnode_decode_display_timing(ofnode node, int index,
595 struct display_timing *config);
596
597/**
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900598 * ofnode_get_property()- - get a pointer to the value of a node property
Simon Glassc4fc5622017-05-18 20:08:58 -0600599 *
600 * @node: node to read
601 * @propname: property to read
602 * @lenp: place to put length on success
603 * @return pointer to property, or NULL if not found
604 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900605const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600606
607/**
608 * ofnode_is_available() - check if a node is marked available
609 *
610 * @node: node to check
611 * @return true if node's 'status' property is "okay" (or is missing)
612 */
613bool ofnode_is_available(ofnode node);
614
615/**
616 * ofnode_get_addr_size() - get address and size from a property
617 *
618 * This does no address translation. It simply reads an property that contains
619 * an address and a size value, one after the other.
620 *
621 * @node: node to read from
622 * @propname: property to read
623 * @sizep: place to put size value (on success)
624 * @return address value, or FDT_ADDR_T_NONE on error
625 */
626phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
627 phys_size_t *sizep);
628
629/**
630 * ofnode_read_u8_array_ptr() - find an 8-bit array
631 *
632 * Look up a property in a node and return a pointer to its contents as a
633 * byte array of given length. The property must have at least enough data
634 * for the array (count bytes). It may have more, but this will be ignored.
635 * The data is not copied.
636 *
637 * @node node to examine
638 * @propname name of property to find
639 * @sz number of array elements
640 * @return pointer to byte array if found, or NULL if the property is not
641 * found or there is not enough data
642 */
643const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
644 size_t sz);
645
646/**
647 * ofnode_read_pci_addr() - look up a PCI address
648 *
649 * Look at an address property in a node and return the PCI address which
650 * corresponds to the given type in the form of fdt_pci_addr.
651 * The property must hold one fdt_pci_addr with a lengh.
652 *
653 * @node node to examine
654 * @type pci address type (FDT_PCI_SPACE_xxx)
655 * @propname name of property to find
656 * @addr returns pci address in the form of fdt_pci_addr
657 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
658 * format of the property was invalid, -ENXIO if the requested
659 * address type was not found
660 */
661int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
662 const char *propname, struct fdt_pci_addr *addr);
663
664/**
Bin Mengfa157712018-08-03 01:14:35 -0700665 * ofnode_read_pci_vendev() - look up PCI vendor and device id
666 *
667 * Look at the compatible property of a device node that represents a PCI
668 * device and extract pci vendor id and device id from it.
669 *
670 * @param node node to examine
671 * @param vendor vendor id of the pci device
672 * @param device device id of the pci device
673 * @return 0 if ok, negative on error
674 */
675int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
676
677/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600678 * ofnode_read_addr_cells() - Get the number of address cells for a node
679 *
680 * This walks back up the tree to find the closest #address-cells property
681 * which controls the given node.
682 *
683 * @node: Node to check
684 * @return number of address cells this node uses
685 */
686int ofnode_read_addr_cells(ofnode node);
687
688/**
689 * ofnode_read_size_cells() - Get the number of size cells for a node
690 *
691 * This walks back up the tree to find the closest #size-cells property
692 * which controls the given node.
693 *
694 * @node: Node to check
695 * @return number of size cells this node uses
696 */
697int ofnode_read_size_cells(ofnode node);
698
699/**
Simon Glass4191dc12017-06-12 06:21:31 -0600700 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
701 *
702 * This function matches fdt_address_cells().
703 *
704 * @np: Node pointer to check
705 * @return value of #address-cells property in this node, or 2 if none
706 */
707int ofnode_read_simple_addr_cells(ofnode node);
708
709/**
710 * ofnode_read_simple_size_cells() - Get the size cells property in a node
711 *
712 * This function matches fdt_size_cells().
713 *
714 * @np: Node pointer to check
715 * @return value of #size-cells property in this node, or 2 if none
716 */
717int ofnode_read_simple_size_cells(ofnode node);
718
719/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600720 * ofnode_pre_reloc() - check if a node should be bound before relocation
721 *
722 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
723 * via special device tree properties.
724 *
725 * Before relocation this function can be used to check if nodes are required
726 * in either SPL or TPL stages.
727 *
728 * After relocation and jumping into the real U-Boot binary it is possible to
729 * determine if a node was bound in one of SPL/TPL stages.
730 *
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200731 * There are 4 settings currently in use
732 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glassc4fc5622017-05-18 20:08:58 -0600733 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
734 * Existing platforms only use it to indicate nodes needed in
735 * SPL. Should probably be replaced by u-boot,dm-spl for
736 * new platforms.
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200737 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
738 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glassc4fc5622017-05-18 20:08:58 -0600739 *
740 * @node: node to check
Simon Glass1c682342018-06-11 13:07:11 -0600741 * @return true if node is needed in SPL/TL, false otherwise
Simon Glassc4fc5622017-05-18 20:08:58 -0600742 */
743bool ofnode_pre_reloc(ofnode node);
744
Simon Glassa8173d62018-06-11 13:07:12 -0600745/**
746 * ofnode_read_resource() - Read a resource from a node
747 *
748 * Read resource information from a node at the given index
749 *
750 * @node: Node to read from
751 * @index: Index of resource to read (0 = first)
752 * @res: Returns resource that was read, on success
753 * @return 0 if OK, -ve on error
754 */
Simon Glassf7bfcc42017-07-25 08:29:55 -0600755int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassa8173d62018-06-11 13:07:12 -0600756
757/**
758 * ofnode_read_resource_byname() - Read a resource from a node by name
759 *
760 * Read resource information from a node matching the given name. This uses a
761 * 'reg-names' string list property with the names matching the associated
762 * 'reg' property list.
763 *
764 * @node: Node to read from
765 * @name: Name of resource to read
766 * @res: Returns resource that was read, on success
767 * @return 0 if OK, -ve on error
768 */
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900769int ofnode_read_resource_byname(ofnode node, const char *name,
770 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -0600771
Simon Glass28529762017-08-05 15:45:54 -0600772/**
Simon Glass954eeae2018-06-11 13:07:13 -0600773 * ofnode_by_compatible() - Find the next compatible node
774 *
775 * Find the next node after @from that is compatible with @compat
776 *
777 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
778 * @compat: Compatible string to match
779 * @return ofnode found, or ofnode_null() if none
780 */
781ofnode ofnode_by_compatible(ofnode from, const char *compat);
782
783/**
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200784 * ofnode_by_prop_value() - Find the next node with given property value
785 *
786 * Find the next node after @from that has a @propname with a value
787 * @propval and a length @proplen.
788 *
789 * @from: ofnode to start from (use ofnode_null() to start at the
790 * beginning) @propname: property name to check @propval: property value to
791 * search for @proplen: length of the value in propval @return ofnode
792 * found, or ofnode_null() if none
793 */
794ofnode ofnode_by_prop_value(ofnode from, const char *propname,
795 const void *propval, int proplen);
796
797/**
Simon Glass28529762017-08-05 15:45:54 -0600798 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
799 *
800 * @node: child node (ofnode, lvalue)
801 * @parent: parent node (ofnode)
802 *
803 * This is a wrapper around a for loop and is used like so:
804 *
805 * ofnode node;
806 *
807 * ofnode_for_each_subnode(node, parent) {
808 * Use node
809 * ...
810 * }
811 *
812 * Note that this is implemented as a macro and @node is used as
813 * iterator in the loop. The parent variable can be a constant or even a
814 * literal.
815 */
816#define ofnode_for_each_subnode(node, parent) \
817 for (node = ofnode_first_subnode(parent); \
818 ofnode_valid(node); \
819 node = ofnode_next_subnode(node))
820
Mario Sixaefac062018-01-15 11:07:19 +0100821/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200822 * ofnode_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +0100823 *
824 * Translate an address from the device-tree into a CPU physical address. This
825 * function walks up the tree and applies the various bus mappings along the
826 * way.
827 *
828 * @ofnode: Device tree node giving the context in which to translate the
829 * address
830 * @in_addr: pointer to the address to translate
831 * @return the translated address; OF_BAD_ADDR on error
832 */
833u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900834
835/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200836 * ofnode_translate_dma_address() - Translate a device-tree DMA address
837 *
838 * Translate a DMA address from the device-tree into a CPU physical address.
839 * This function walks up the tree and applies the various bus mappings along
840 * the way.
841 *
842 * @ofnode: Device tree node giving the context in which to translate the
843 * DMA address
844 * @in_addr: pointer to the DMA address to translate
845 * @return the translated DMA address; OF_BAD_ADDR on error
846 */
847u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
848
849/**
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900850 * ofnode_device_is_compatible() - check if the node is compatible with compat
851 *
852 * This allows to check whether the node is comaptible with the compat.
853 *
854 * @node: Device tree node for which compatible needs to be verified.
855 * @compat: Compatible string which needs to verified in the given node.
856 * @return true if OK, false if the compatible is not found
857 */
858int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Six047dafc2018-06-26 08:46:48 +0200859
860/**
861 * ofnode_write_prop() - Set a property of a ofnode
862 *
863 * Note that the value passed to the function is *not* allocated by the
864 * function itself, but must be allocated by the caller if necessary.
865 *
866 * @node: The node for whose property should be set
867 * @propname: The name of the property to set
868 * @len: The length of the new value of the property
869 * @value: The new value of the property (must be valid prior to calling
870 * the function)
871 * @return 0 if successful, -ve on error
872 */
873int ofnode_write_prop(ofnode node, const char *propname, int len,
874 const void *value);
875
876/**
877 * ofnode_write_string() - Set a string property of a ofnode
878 *
879 * Note that the value passed to the function is *not* allocated by the
880 * function itself, but must be allocated by the caller if necessary.
881 *
882 * @node: The node for whose string property should be set
883 * @propname: The name of the string property to set
884 * @value: The new value of the string property (must be valid prior to
885 * calling the function)
886 * @return 0 if successful, -ve on error
887 */
888int ofnode_write_string(ofnode node, const char *propname, const char *value);
889
890/**
891 * ofnode_set_enabled() - Enable or disable a device tree node given by its
892 * ofnode
893 *
894 * This function effectively sets the node's "status" property to either "okay"
895 * or "disable", hence making it available for driver model initialization or
896 * not.
897 *
898 * @node: The node to enable
899 * @value: Flag that tells the function to either disable or enable the
900 * node
901 * @return 0 if successful, -ve on error
902 */
903int ofnode_set_enabled(ofnode node, bool value);
904
Simon Glass9a148602017-05-17 17:18:10 -0600905#endif