blob: 0c62d62f114892e1328021053ce39f9216c114d9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassf11c7ab2017-05-18 20:09:03 -06002/*
3 * Function to read values from the device tree node attached to a udevice.
4 *
5 * Copyright (c) 2017 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glassf11c7ab2017-05-18 20:09:03 -06007 */
8
9#ifndef _DM_READ_H
10#define _DM_READ_H
11
12#include <dm/fdtaddr.h>
13#include <dm/ofnode.h>
14#include <dm/uclass.h>
15
Simon Glassa4481012017-06-12 06:21:29 -060016struct resource;
17
Simon Glassf11c7ab2017-05-18 20:09:03 -060018#if CONFIG_IS_ENABLED(OF_LIVE)
19static inline const struct device_node *dev_np(struct udevice *dev)
20{
21 return ofnode_to_np(dev->node);
22}
23#else
24static inline const struct device_node *dev_np(struct udevice *dev)
25{
26 return NULL;
27}
28#endif
29
30/**
31 * dev_ofnode() - get the DT node reference associated with a udevice
32 *
33 * @dev: device to check
34 * @return reference of the the device's DT node
35 */
36static inline ofnode dev_ofnode(struct udevice *dev)
37{
38 return dev->node;
39}
40
41static inline bool dev_of_valid(struct udevice *dev)
42{
43 return ofnode_valid(dev_ofnode(dev));
44}
45
Simon Glass47a0fd32017-05-18 20:09:04 -060046#ifndef CONFIG_DM_DEV_READ_INLINE
47/**
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090048 * dev_read_u32() - read a 32-bit integer from a device's DT property
49 *
50 * @dev: device to read DT property from
51 * @propname: name of the property to read from
52 * @outp: place to put value (if found)
53 * @return 0 if OK, -ve on error
54 */
55int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp);
56
57/**
Simon Glass47a0fd32017-05-18 20:09:04 -060058 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
59 *
60 * @dev: device to read DT property from
61 * @propname: name of the property to read from
62 * @def: default value to return if the property has no value
63 * @return property value, or @def if not found
64 */
65int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
Simon Glassf11c7ab2017-05-18 20:09:03 -060066
67/**
Simon Glassa1b17e42018-12-10 10:37:37 -070068 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
69 *
70 * @dev: device to read DT property from
71 * @propname: name of the property to read from
72 * @outp: place to put value (if found)
73 * @return 0 if OK, -ve on error
74 */
75int dev_read_s32(struct udevice *dev, const char *propname, s32 *outp);
76
77/**
78 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
79 *
80 * @dev: device to read DT property from
81 * @propname: name of the property to read from
82 * @def: default value to return if the property has no value
83 * @return property value, or @def if not found
84 */
85int dev_read_s32_default(struct udevice *dev, const char *propname, int def);
86
87/**
88 * dev_read_u32u() - read a 32-bit integer from a device's DT property
89 *
90 * This version uses a standard uint type.
91 *
92 * @dev: device to read DT property from
93 * @propname: name of the property to read from
94 * @outp: place to put value (if found)
95 * @return 0 if OK, -ve on error
96 */
97int dev_read_u32u(struct udevice *dev, const char *propname, uint *outp);
98
99/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600100 * dev_read_string() - Read a string from a device's DT property
101 *
102 * @dev: device to read DT property from
103 * @propname: name of the property to read
104 * @return string from property value, or NULL if there is no such property
105 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600106const char *dev_read_string(struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600107
108/**
109 * dev_read_bool() - read a boolean value from a device's DT property
110 *
111 * @dev: device to read DT property from
112 * @propname: name of property to read
113 * @return true if property is present (meaning true), false if not present
114 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600115bool dev_read_bool(struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600116
117/**
118 * dev_read_subnode() - find a named subnode of a device
119 *
120 * @dev: device whose DT node contains the subnode
121 * @subnode_name: name of subnode to find
122 * @return reference to subnode (which can be invalid if there is no such
123 * subnode)
124 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600125ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600126
127/**
128 * dev_read_size() - read the size of a property
129 *
130 * @dev: device to check
131 * @propname: property to check
132 * @return size of property if present, or -EINVAL if not
133 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600134int dev_read_size(struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600135
136/**
137 * dev_read_addr_index() - Get the indexed reg property of a device
138 *
139 * @dev: Device to read from
140 * @index: the 'reg' property can hold a list of <addr, size> pairs
141 * and @index is used to select which one is required
142 *
143 * @return address or FDT_ADDR_T_NONE if not found
144 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600145fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600146
147/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530148 * dev_read_addr_size_index() - Get the indexed reg property of a device
149 *
150 * @dev: Device to read from
151 * @index: the 'reg' property can hold a list of <addr, size> pairs
152 * and @index is used to select which one is required
153 * @size: place to put size value (on success)
154 *
155 * @return address or FDT_ADDR_T_NONE if not found
156 */
157fdt_addr_t dev_read_addr_size_index(struct udevice *dev, int index,
158 fdt_size_t *size);
159
160/**
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200161 * dev_remap_addr_index() - Get the indexed reg property of a device
162 * as a memory-mapped I/O pointer
163 *
164 * @dev: Device to read from
165 * @index: the 'reg' property can hold a list of <addr, size> pairs
166 * and @index is used to select which one is required
167 *
168 * @return pointer or NULL if not found
169 */
170void *dev_remap_addr_index(struct udevice *dev, int index);
171
172/**
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100173 * dev_read_addr_name() - Get the reg property of a device, indexed by name
174 *
175 * @dev: Device to read from
176 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
177 * 'reg-names' property providing named-based identification. @index
178 * indicates the value to search for in 'reg-names'.
179 *
180 * @return address or FDT_ADDR_T_NONE if not found
181 */
182fdt_addr_t dev_read_addr_name(struct udevice *dev, const char* name);
183
184/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530185 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
186 *
187 * @dev: Device to read from
188 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
189 * 'reg-names' property providing named-based identification. @index
190 * indicates the value to search for in 'reg-names'.
191 * @size: place to put size value (on success)
192 *
193 * @return address or FDT_ADDR_T_NONE if not found
194 */
195fdt_addr_t dev_read_addr_size_name(struct udevice *dev, const char *name,
196 fdt_size_t *size);
197
198/**
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100199 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
200 * as a memory-mapped I/O pointer
201 *
202 * @dev: Device to read from
203 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
204 * 'reg-names' property providing named-based identification. @index
205 * indicates the value to search for in 'reg-names'.
206 *
207 * @return pointer or NULL if not found
208 */
209void *dev_remap_addr_name(struct udevice *dev, const char* name);
210
211/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600212 * dev_read_addr() - Get the reg property of a device
213 *
214 * @dev: Device to read from
215 *
216 * @return address or FDT_ADDR_T_NONE if not found
217 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600218fdt_addr_t dev_read_addr(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600219
220/**
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200221 * dev_read_addr_ptr() - Get the reg property of a device
222 * as a pointer
223 *
224 * @dev: Device to read from
225 *
226 * @return pointer or NULL if not found
227 */
228void *dev_read_addr_ptr(struct udevice *dev);
229
230/**
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200231 * dev_remap_addr() - Get the reg property of a device as a
232 * memory-mapped I/O pointer
233 *
234 * @dev: Device to read from
235 *
236 * @return pointer or NULL if not found
237 */
238void *dev_remap_addr(struct udevice *dev);
239
240/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600241 * dev_read_addr_size() - get address and size from a device property
242 *
243 * This does no address translation. It simply reads an property that contains
244 * an address and a size value, one after the other.
245 *
246 * @dev: Device to read from
247 * @propname: property to read
248 * @sizep: place to put size value (on success)
249 * @return address value, or FDT_ADDR_T_NONE on error
250 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600251fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
252 fdt_size_t *sizep);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600253
254/**
255 * dev_read_name() - get the name of a device's node
256 *
Bin Meng15d61d02019-07-05 09:23:18 -0700257 * @dev: Device to read from
Simon Glassf11c7ab2017-05-18 20:09:03 -0600258 * @return name of node
259 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600260const char *dev_read_name(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600261
262/**
263 * dev_read_stringlist_search() - find string in a string list and return index
264 *
265 * Note that it is possible for this function to succeed on property values
266 * that are not NUL-terminated. That's because the function will stop after
267 * finding the first occurrence of @string. This can for example happen with
268 * small-valued cell properties, such as #address-cells, when searching for
269 * the empty string.
270 *
271 * @dev: device to check
272 * @propname: name of the property containing the string list
273 * @string: string to look up in the string list
274 *
275 * @return:
276 * the index of the string in the list of strings
277 * -ENODATA if the property is not found
278 * -EINVAL on some other error
279 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600280int dev_read_stringlist_search(struct udevice *dev, const char *property,
281 const char *string);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600282
283/**
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200284 * dev_read_string_index() - obtain an indexed string from a string list
285 *
286 * @dev: device to examine
287 * @propname: name of the property containing the string list
288 * @index: index of the string to return
289 * @out: return location for the string
290 *
291 * @return:
292 * length of string, if found or -ve error value if not found
293 */
294int dev_read_string_index(struct udevice *dev, const char *propname, int index,
295 const char **outp);
296
297/**
298 * dev_read_string_count() - find the number of strings in a string list
299 *
300 * @dev: device to examine
301 * @propname: name of the property containing the string list
302 * @return:
303 * number of strings in the list, or -ve error value if not found
304 */
305int dev_read_string_count(struct udevice *dev, const char *propname);
306/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600307 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
308 *
309 * This function is useful to parse lists of phandles and their arguments.
310 * Returns 0 on success and fills out_args, on error returns appropriate
311 * errno value.
312 *
313 * Caller is responsible to call of_node_put() on the returned out_args->np
314 * pointer.
315 *
316 * Example:
317 *
318 * phandle1: node1 {
319 * #list-cells = <2>;
320 * }
321 *
322 * phandle2: node2 {
323 * #list-cells = <1>;
324 * }
325 *
326 * node3 {
327 * list = <&phandle1 1 2 &phandle2 3>;
328 * }
329 *
330 * To get a device_node of the `node2' node you may call this:
331 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
332 *
333 * @dev: device whose node containing a list
334 * @list_name: property name that contains a list
335 * @cells_name: property name that specifies phandles' arguments count
336 * @cells_count: Cell count to use if @cells_name is NULL
337 * @index: index of a phandle to parse out
338 * @out_args: optional pointer to output arguments structure (will be filled)
339 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
340 * @list_name does not exist, -EINVAL if a phandle was not found,
341 * @cells_name could not be found, the arguments were truncated or there
342 * were too many arguments.
343 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600344int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
345 const char *cells_name, int cell_count,
346 int index,
347 struct ofnode_phandle_args *out_args);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600348
349/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200350 * dev_count_phandle_with_args() - Return phandle number in a list
351 *
352 * This function is usefull to get phandle number contained in a property list.
353 * For example, this allows to allocate the right amount of memory to keep
354 * clock's reference contained into the "clocks" property.
355 *
356 *
357 * @dev: device whose node containing a list
358 * @list_name: property name that contains a list
359 * @cells_name: property name that specifies phandles' arguments count
360 * @Returns number of phandle found on success, on error returns appropriate
361 * errno value.
362 */
363
364int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
365 const char *cells_name);
366
367/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600368 * dev_read_addr_cells() - Get the number of address cells for a device's node
369 *
370 * This walks back up the tree to find the closest #address-cells property
371 * which controls the given node.
372 *
Mario Six7ba50412018-01-15 11:09:36 +0100373 * @dev: device to check
Simon Glassf11c7ab2017-05-18 20:09:03 -0600374 * @return number of address cells this node uses
375 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600376int dev_read_addr_cells(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600377
378/**
379 * dev_read_size_cells() - Get the number of size cells for a device's node
380 *
381 * This walks back up the tree to find the closest #size-cells property
382 * which controls the given node.
383 *
Mario Six7ba50412018-01-15 11:09:36 +0100384 * @dev: device to check
Simon Glassf11c7ab2017-05-18 20:09:03 -0600385 * @return number of size cells this node uses
386 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600387int dev_read_size_cells(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600388
389/**
Simon Glass878d68c2017-06-12 06:21:31 -0600390 * dev_read_addr_cells() - Get the address cells property in a node
391 *
392 * This function matches fdt_address_cells().
393 *
Mario Six7ba50412018-01-15 11:09:36 +0100394 * @dev: device to check
Simon Glass878d68c2017-06-12 06:21:31 -0600395 * @return number of address cells this node uses
396 */
397int dev_read_simple_addr_cells(struct udevice *dev);
398
399/**
400 * dev_read_size_cells() - Get the size cells property in a node
401 *
402 * This function matches fdt_size_cells().
403 *
Mario Six7ba50412018-01-15 11:09:36 +0100404 * @dev: device to check
Simon Glass878d68c2017-06-12 06:21:31 -0600405 * @return number of size cells this node uses
406 */
407int dev_read_simple_size_cells(struct udevice *dev);
408
409/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600410 * dev_read_phandle() - Get the phandle from a device
411 *
412 * @dev: device to check
413 * @return phandle (1 or greater), or 0 if no phandle or other error
414 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600415int dev_read_phandle(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600416
417/**
418 * dev_read_prop()- - read a property from a device's node
419 *
420 * @dev: device to check
421 * @propname: property to read
422 * @lenp: place to put length on success
423 * @return pointer to property, or NULL if not found
424 */
Masahiro Yamadafd736212017-07-17 12:18:39 +0900425const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600426
427/**
428 * dev_read_alias_seq() - Get the alias sequence number of a node
429 *
430 * This works out whether a node is pointed to by an alias, and if so, the
431 * sequence number of that alias. Aliases are of the form <base><num> where
432 * <num> is the sequence number. For example spi2 would be sequence number 2.
433 *
434 * @dev: device to look up
435 * @devnump: set to the sequence number if one is found
436 * @return 0 if a sequence was found, -ve if not
437 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600438int dev_read_alias_seq(struct udevice *dev, int *devnump);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600439
440/**
441 * dev_read_u32_array() - Find and read an array of 32 bit integers
442 *
443 * Search for a property in a device node and read 32-bit value(s) from
444 * it.
445 *
446 * The out_values is modified only if a valid u32 value can be decoded.
447 *
448 * @dev: device to look up
449 * @propname: name of the property to read
450 * @out_values: pointer to return value, modified only if return value is 0
451 * @sz: number of array elements to read
452 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
453 * property does not have a value, and -EOVERFLOW if the property data isn't
454 * large enough.
455 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600456int dev_read_u32_array(struct udevice *dev, const char *propname,
457 u32 *out_values, size_t sz);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600458
459/**
460 * dev_read_first_subnode() - find the first subnode of a device's node
461 *
462 * @dev: device to look up
463 * @return reference to the first subnode (which can be invalid if the device's
464 * node has no subnodes)
465 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600466ofnode dev_read_first_subnode(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600467
468/**
469 * ofnode_next_subnode() - find the next sibling of a subnode
470 *
471 * @node: valid reference to previous node (sibling)
472 * @return reference to the next subnode (which can be invalid if the node
473 * has no more siblings)
474 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600475ofnode dev_read_next_subnode(ofnode node);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600476
477/**
478 * dev_read_u8_array_ptr() - find an 8-bit array
479 *
480 * Look up a device's node property and return a pointer to its contents as a
481 * byte array of given length. The property must have at least enough data
482 * for the array (count bytes). It may have more, but this will be ignored.
483 * The data is not copied.
484 *
485 * @dev: device to look up
486 * @propname: name of property to find
487 * @sz: number of array elements
488 * @return pointer to byte array if found, or NULL if the property is not
489 * found or there is not enough data
490 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600491const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
492 size_t sz);
493
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600494/**
495 * dev_read_enabled() - check whether a node is enabled
496 *
497 * This looks for a 'status' property. If this exists, then returns 1 if
498 * the status is 'ok' and 0 otherwise. If there is no status property,
499 * it returns 1 on the assumption that anything mentioned should be enabled
500 * by default.
501 *
502 * @dev: device to examine
503 * @return integer value 0 (not enabled) or 1 (enabled)
504 */
505int dev_read_enabled(struct udevice *dev);
506
Simon Glassdcf98852017-07-25 08:29:55 -0600507/**
508 * dev_read_resource() - obtain an indexed resource from a device.
509 *
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900510 * @dev: device to examine
Simon Glassdcf98852017-07-25 08:29:55 -0600511 * @index index of the resource to retrieve (0 = first)
512 * @res returns the resource
513 * @return 0 if ok, negative on error
514 */
515int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
516
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900517/**
518 * dev_read_resource_byname() - obtain a named resource from a device.
519 *
520 * @dev: device to examine
521 * @name: name of the resource to retrieve
522 * @res: returns the resource
523 * @return 0 if ok, negative on error
524 */
525int dev_read_resource_byname(struct udevice *dev, const char *name,
526 struct resource *res);
527
Mario Six147c6072018-01-15 11:07:19 +0100528/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200529 * dev_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +0100530 *
531 * Translate an address from the device-tree into a CPU physical address. This
532 * function walks up the tree and applies the various bus mappings along the
533 * way.
534 *
535 * @dev: device giving the context in which to translate the address
536 * @in_addr: pointer to the address to translate
537 * @return the translated address; OF_BAD_ADDR on error
538 */
539u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
Michal Simek83e4c7e2019-01-31 16:30:59 +0100540
541/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200542 * dev_translate_dma_address() - Translate a device-tree DMA address
543 *
544 * Translate a DMA address from the device-tree into a CPU physical address.
545 * This function walks up the tree and applies the various bus mappings along
546 * the way.
547 *
548 * @dev: device giving the context in which to translate the DMA address
549 * @in_addr: pointer to the DMA address to translate
550 * @return the translated DMA address; OF_BAD_ADDR on error
551 */
552u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr);
553
554/**
Michal Simek83e4c7e2019-01-31 16:30:59 +0100555 * dev_read_alias_highest_id - Get highest alias id for the given stem
556 * @stem: Alias stem to be examined
557 *
558 * The function travels the lookup table to get the highest alias id for the
559 * given alias stem.
560 * @return alias ID, if found, else -1
561 */
562int dev_read_alias_highest_id(const char *stem);
563
Simon Glass47a0fd32017-05-18 20:09:04 -0600564#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
565
Masahiro Yamada3ab48f62017-12-30 02:00:05 +0900566static inline int dev_read_u32(struct udevice *dev,
567 const char *propname, u32 *outp)
568{
569 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
570}
571
Simon Glass47a0fd32017-05-18 20:09:04 -0600572static inline int dev_read_u32_default(struct udevice *dev,
573 const char *propname, int def)
574{
575 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
576}
577
Simon Glassa1b17e42018-12-10 10:37:37 -0700578static inline int dev_read_s32(struct udevice *dev,
579 const char *propname, s32 *outp)
580{
581 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
582}
583
584static inline int dev_read_s32_default(struct udevice *dev,
585 const char *propname, int def)
586{
587 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
588}
589
590static inline int dev_read_u32u(struct udevice *dev,
591 const char *propname, uint *outp)
592{
593 u32 val;
594 int ret;
595
596 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
597 if (ret)
598 return ret;
599 *outp = val;
600
601 return 0;
602}
603
Simon Glass47a0fd32017-05-18 20:09:04 -0600604static inline const char *dev_read_string(struct udevice *dev,
605 const char *propname)
606{
607 return ofnode_read_string(dev_ofnode(dev), propname);
608}
609
610static inline bool dev_read_bool(struct udevice *dev, const char *propname)
611{
612 return ofnode_read_bool(dev_ofnode(dev), propname);
613}
614
615static inline ofnode dev_read_subnode(struct udevice *dev,
616 const char *subbnode_name)
617{
618 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
619}
620
621static inline int dev_read_size(struct udevice *dev, const char *propname)
622{
623 return ofnode_read_size(dev_ofnode(dev), propname);
624}
625
626static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
627{
628 return devfdt_get_addr_index(dev, index);
629}
630
Sekhar Norif5b90472019-08-01 19:12:56 +0530631static inline fdt_addr_t dev_read_addr_size_index(struct udevice *dev,
632 int index,
633 fdt_size_t *size)
634{
635 return devfdt_get_addr_size_index(dev, index, size);
636}
637
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100638static inline fdt_addr_t dev_read_addr_name(struct udevice *dev,
639 const char *name)
640{
641 return devfdt_get_addr_name(dev, name);
642}
643
Sekhar Norif5b90472019-08-01 19:12:56 +0530644static inline fdt_addr_t dev_read_addr_size_name(struct udevice *dev,
645 const char *name,
646 fdt_size_t *size)
647{
648 return devfdt_get_addr_size_name(dev, name, size);
649}
650
Simon Glass47a0fd32017-05-18 20:09:04 -0600651static inline fdt_addr_t dev_read_addr(struct udevice *dev)
652{
653 return devfdt_get_addr(dev);
654}
655
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200656static inline void *dev_read_addr_ptr(struct udevice *dev)
657{
658 return devfdt_get_addr_ptr(dev);
659}
660
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200661static inline void *dev_remap_addr(struct udevice *dev)
662{
663 return devfdt_remap_addr(dev);
664}
665
666static inline void *dev_remap_addr_index(struct udevice *dev, int index)
667{
668 return devfdt_remap_addr_index(dev, index);
669}
670
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100671static inline void *dev_remap_addr_name(struct udevice *dev, const char *name)
672{
673 return devfdt_remap_addr_name(dev, name);
674}
675
Simon Glass47a0fd32017-05-18 20:09:04 -0600676static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
677 const char *propname,
678 fdt_size_t *sizep)
679{
680 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
681}
682
683static inline const char *dev_read_name(struct udevice *dev)
684{
685 return ofnode_get_name(dev_ofnode(dev));
686}
687
688static inline int dev_read_stringlist_search(struct udevice *dev,
689 const char *propname,
690 const char *string)
691{
692 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
693}
694
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200695static inline int dev_read_string_index(struct udevice *dev,
696 const char *propname, int index,
697 const char **outp)
698{
699 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
700}
701
702static inline int dev_read_string_count(struct udevice *dev,
703 const char *propname)
704{
705 return ofnode_read_string_count(dev_ofnode(dev), propname);
706}
707
Simon Glass47a0fd32017-05-18 20:09:04 -0600708static inline int dev_read_phandle_with_args(struct udevice *dev,
709 const char *list_name, const char *cells_name, int cell_count,
710 int index, struct ofnode_phandle_args *out_args)
711{
712 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
713 cells_name, cell_count, index,
714 out_args);
715}
716
Patrice Chotard642346a2017-07-18 11:57:08 +0200717static inline int dev_count_phandle_with_args(struct udevice *dev,
718 const char *list_name, const char *cells_name)
719{
720 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
721 cells_name);
722}
723
Simon Glass47a0fd32017-05-18 20:09:04 -0600724static inline int dev_read_addr_cells(struct udevice *dev)
725{
Simon Glass878d68c2017-06-12 06:21:31 -0600726 /* NOTE: this call should walk up the parent stack */
Simon Glass47a0fd32017-05-18 20:09:04 -0600727 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
728}
729
730static inline int dev_read_size_cells(struct udevice *dev)
731{
Simon Glass878d68c2017-06-12 06:21:31 -0600732 /* NOTE: this call should walk up the parent stack */
733 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
734}
735
736static inline int dev_read_simple_addr_cells(struct udevice *dev)
737{
738 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
739}
740
741static inline int dev_read_simple_size_cells(struct udevice *dev)
742{
Simon Glass47a0fd32017-05-18 20:09:04 -0600743 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
744}
745
746static inline int dev_read_phandle(struct udevice *dev)
747{
748 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
749}
750
Masahiro Yamadafd736212017-07-17 12:18:39 +0900751static inline const void *dev_read_prop(struct udevice *dev,
752 const char *propname, int *lenp)
Simon Glass47a0fd32017-05-18 20:09:04 -0600753{
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900754 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glass47a0fd32017-05-18 20:09:04 -0600755}
756
757static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
758{
759 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
760 dev_of_offset(dev), devnump);
761}
762
763static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
764 u32 *out_values, size_t sz)
765{
766 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
767}
768
769static inline ofnode dev_read_first_subnode(struct udevice *dev)
770{
771 return ofnode_first_subnode(dev_ofnode(dev));
772}
773
774static inline ofnode dev_read_next_subnode(ofnode node)
775{
776 return ofnode_next_subnode(node);
777}
778
Simon Glassf11c7ab2017-05-18 20:09:03 -0600779static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
780 const char *propname, size_t sz)
781{
782 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
783}
784
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600785static inline int dev_read_enabled(struct udevice *dev)
786{
787 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
788}
789
Simon Glassdcf98852017-07-25 08:29:55 -0600790static inline int dev_read_resource(struct udevice *dev, uint index,
791 struct resource *res)
792{
793 return ofnode_read_resource(dev_ofnode(dev), index, res);
794}
795
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900796static inline int dev_read_resource_byname(struct udevice *dev,
797 const char *name,
798 struct resource *res)
799{
800 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
801}
802
Mario Six147c6072018-01-15 11:07:19 +0100803static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
804{
805 return ofnode_translate_address(dev_ofnode(dev), in_addr);
806}
807
Fabien Dessenne641067f2019-05-31 15:11:30 +0200808static inline u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr)
809{
810 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
811}
812
Michal Simek83e4c7e2019-01-31 16:30:59 +0100813static inline int dev_read_alias_highest_id(const char *stem)
814{
815 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
816}
817
Simon Glassf11c7ab2017-05-18 20:09:03 -0600818#endif /* CONFIG_DM_DEV_READ_INLINE */
819
820/**
821 * dev_for_each_subnode() - Helper function to iterate through subnodes
822 *
823 * This creates a for() loop which works through the subnodes in a device's
824 * device-tree node.
825 *
826 * @subnode: ofnode holding the current subnode
827 * @dev: device to use for interation (struct udevice *)
828 */
829#define dev_for_each_subnode(subnode, dev) \
830 for (subnode = dev_read_first_subnode(dev); \
831 ofnode_valid(subnode); \
832 subnode = ofnode_next_subnode(subnode))
833
834#endif