blob: 6ecd062e200cfaf6d5d823a0bcfa8a5420cdbe9b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass34ef0c22017-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 Glass34ef0c22017-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 Glass170e17c2017-06-12 06:21:29 -060016struct resource;
17
Simon Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -060046#ifndef CONFIG_DM_DEV_READ_INLINE
47/**
Masahiro Yamada71d115f2017-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 Glasscfdae5c2017-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 Glass34ef0c22017-05-18 20:09:03 -060066
67/**
Simon Glass6df01f92018-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 Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600106const char *dev_read_string(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600115bool dev_read_bool(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600125ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
Simon Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600134int dev_read_size(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600145fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
Simon Glass34ef0c22017-05-18 20:09:03 -0600146
147/**
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200148 * dev_remap_addr_index() - Get the indexed reg property of a device
149 * as a memory-mapped I/O pointer
150 *
151 * @dev: Device to read from
152 * @index: the 'reg' property can hold a list of <addr, size> pairs
153 * and @index is used to select which one is required
154 *
155 * @return pointer or NULL if not found
156 */
157void *dev_remap_addr_index(struct udevice *dev, int index);
158
159/**
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100160 * dev_read_addr_name() - Get the reg property of a device, indexed by name
161 *
162 * @dev: Device to read from
163 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
164 * 'reg-names' property providing named-based identification. @index
165 * indicates the value to search for in 'reg-names'.
166 *
167 * @return address or FDT_ADDR_T_NONE if not found
168 */
169fdt_addr_t dev_read_addr_name(struct udevice *dev, const char* name);
170
171/**
172 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
173 * as a memory-mapped I/O pointer
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 pointer or NULL if not found
181 */
182void *dev_remap_addr_name(struct udevice *dev, const char* name);
183
184/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600185 * dev_read_addr() - Get the reg property of a device
186 *
187 * @dev: Device to read from
188 *
189 * @return address or FDT_ADDR_T_NONE if not found
190 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600191fdt_addr_t dev_read_addr(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600192
193/**
Philipp Tomsich7719b392017-09-11 22:04:12 +0200194 * dev_read_addr_ptr() - Get the reg property of a device
195 * as a pointer
196 *
197 * @dev: Device to read from
198 *
199 * @return pointer or NULL if not found
200 */
201void *dev_read_addr_ptr(struct udevice *dev);
202
203/**
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200204 * dev_remap_addr() - Get the reg property of a device as a
205 * memory-mapped I/O pointer
206 *
207 * @dev: Device to read from
208 *
209 * @return pointer or NULL if not found
210 */
211void *dev_remap_addr(struct udevice *dev);
212
213/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600214 * dev_read_addr_size() - get address and size from a device property
215 *
216 * This does no address translation. It simply reads an property that contains
217 * an address and a size value, one after the other.
218 *
219 * @dev: Device to read from
220 * @propname: property to read
221 * @sizep: place to put size value (on success)
222 * @return address value, or FDT_ADDR_T_NONE on error
223 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600224fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
225 fdt_size_t *sizep);
Simon Glass34ef0c22017-05-18 20:09:03 -0600226
227/**
228 * dev_read_name() - get the name of a device's node
229 *
Bin Meng4cf914d2019-07-05 09:23:18 -0700230 * @dev: Device to read from
Simon Glass34ef0c22017-05-18 20:09:03 -0600231 * @return name of node
232 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600233const char *dev_read_name(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600234
235/**
236 * dev_read_stringlist_search() - find string in a string list and return index
237 *
238 * Note that it is possible for this function to succeed on property values
239 * that are not NUL-terminated. That's because the function will stop after
240 * finding the first occurrence of @string. This can for example happen with
241 * small-valued cell properties, such as #address-cells, when searching for
242 * the empty string.
243 *
244 * @dev: device to check
245 * @propname: name of the property containing the string list
246 * @string: string to look up in the string list
247 *
248 * @return:
249 * the index of the string in the list of strings
250 * -ENODATA if the property is not found
251 * -EINVAL on some other error
252 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600253int dev_read_stringlist_search(struct udevice *dev, const char *property,
254 const char *string);
Simon Glass34ef0c22017-05-18 20:09:03 -0600255
256/**
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200257 * dev_read_string_index() - obtain an indexed string from a string list
258 *
259 * @dev: device to examine
260 * @propname: name of the property containing the string list
261 * @index: index of the string to return
262 * @out: return location for the string
263 *
264 * @return:
265 * length of string, if found or -ve error value if not found
266 */
267int dev_read_string_index(struct udevice *dev, const char *propname, int index,
268 const char **outp);
269
270/**
271 * dev_read_string_count() - find the number of strings in a string list
272 *
273 * @dev: device to examine
274 * @propname: name of the property containing the string list
275 * @return:
276 * number of strings in the list, or -ve error value if not found
277 */
278int dev_read_string_count(struct udevice *dev, const char *propname);
279/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600280 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
281 *
282 * This function is useful to parse lists of phandles and their arguments.
283 * Returns 0 on success and fills out_args, on error returns appropriate
284 * errno value.
285 *
286 * Caller is responsible to call of_node_put() on the returned out_args->np
287 * pointer.
288 *
289 * Example:
290 *
291 * phandle1: node1 {
292 * #list-cells = <2>;
293 * }
294 *
295 * phandle2: node2 {
296 * #list-cells = <1>;
297 * }
298 *
299 * node3 {
300 * list = <&phandle1 1 2 &phandle2 3>;
301 * }
302 *
303 * To get a device_node of the `node2' node you may call this:
304 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
305 *
306 * @dev: device whose node containing a list
307 * @list_name: property name that contains a list
308 * @cells_name: property name that specifies phandles' arguments count
309 * @cells_count: Cell count to use if @cells_name is NULL
310 * @index: index of a phandle to parse out
311 * @out_args: optional pointer to output arguments structure (will be filled)
312 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
313 * @list_name does not exist, -EINVAL if a phandle was not found,
314 * @cells_name could not be found, the arguments were truncated or there
315 * were too many arguments.
316 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600317int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
318 const char *cells_name, int cell_count,
319 int index,
320 struct ofnode_phandle_args *out_args);
Simon Glass34ef0c22017-05-18 20:09:03 -0600321
322/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200323 * dev_count_phandle_with_args() - Return phandle number in a list
324 *
325 * This function is usefull to get phandle number contained in a property list.
326 * For example, this allows to allocate the right amount of memory to keep
327 * clock's reference contained into the "clocks" property.
328 *
329 *
330 * @dev: device whose node containing a list
331 * @list_name: property name that contains a list
332 * @cells_name: property name that specifies phandles' arguments count
333 * @Returns number of phandle found on success, on error returns appropriate
334 * errno value.
335 */
336
337int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
338 const char *cells_name);
339
340/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600341 * dev_read_addr_cells() - Get the number of address cells for a device's node
342 *
343 * This walks back up the tree to find the closest #address-cells property
344 * which controls the given node.
345 *
Mario Six16408ee2018-01-15 11:09:36 +0100346 * @dev: device to check
Simon Glass34ef0c22017-05-18 20:09:03 -0600347 * @return number of address cells this node uses
348 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600349int dev_read_addr_cells(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600350
351/**
352 * dev_read_size_cells() - Get the number of size cells for a device's node
353 *
354 * This walks back up the tree to find the closest #size-cells property
355 * which controls the given node.
356 *
Mario Six16408ee2018-01-15 11:09:36 +0100357 * @dev: device to check
Simon Glass34ef0c22017-05-18 20:09:03 -0600358 * @return number of size cells this node uses
359 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600360int dev_read_size_cells(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600361
362/**
Simon Glass4191dc12017-06-12 06:21:31 -0600363 * dev_read_addr_cells() - Get the address cells property in a node
364 *
365 * This function matches fdt_address_cells().
366 *
Mario Six16408ee2018-01-15 11:09:36 +0100367 * @dev: device to check
Simon Glass4191dc12017-06-12 06:21:31 -0600368 * @return number of address cells this node uses
369 */
370int dev_read_simple_addr_cells(struct udevice *dev);
371
372/**
373 * dev_read_size_cells() - Get the size cells property in a node
374 *
375 * This function matches fdt_size_cells().
376 *
Mario Six16408ee2018-01-15 11:09:36 +0100377 * @dev: device to check
Simon Glass4191dc12017-06-12 06:21:31 -0600378 * @return number of size cells this node uses
379 */
380int dev_read_simple_size_cells(struct udevice *dev);
381
382/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600383 * dev_read_phandle() - Get the phandle from a device
384 *
385 * @dev: device to check
386 * @return phandle (1 or greater), or 0 if no phandle or other error
387 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600388int dev_read_phandle(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600389
390/**
391 * dev_read_prop()- - read a property from a device's node
392 *
393 * @dev: device to check
394 * @propname: property to read
395 * @lenp: place to put length on success
396 * @return pointer to property, or NULL if not found
397 */
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900398const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
Simon Glass34ef0c22017-05-18 20:09:03 -0600399
400/**
401 * dev_read_alias_seq() - Get the alias sequence number of a node
402 *
403 * This works out whether a node is pointed to by an alias, and if so, the
404 * sequence number of that alias. Aliases are of the form <base><num> where
405 * <num> is the sequence number. For example spi2 would be sequence number 2.
406 *
407 * @dev: device to look up
408 * @devnump: set to the sequence number if one is found
409 * @return 0 if a sequence was found, -ve if not
410 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600411int dev_read_alias_seq(struct udevice *dev, int *devnump);
Simon Glass34ef0c22017-05-18 20:09:03 -0600412
413/**
414 * dev_read_u32_array() - Find and read an array of 32 bit integers
415 *
416 * Search for a property in a device node and read 32-bit value(s) from
417 * it.
418 *
419 * The out_values is modified only if a valid u32 value can be decoded.
420 *
421 * @dev: device to look up
422 * @propname: name of the property to read
423 * @out_values: pointer to return value, modified only if return value is 0
424 * @sz: number of array elements to read
425 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
426 * property does not have a value, and -EOVERFLOW if the property data isn't
427 * large enough.
428 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600429int dev_read_u32_array(struct udevice *dev, const char *propname,
430 u32 *out_values, size_t sz);
Simon Glass34ef0c22017-05-18 20:09:03 -0600431
432/**
433 * dev_read_first_subnode() - find the first subnode of a device's node
434 *
435 * @dev: device to look up
436 * @return reference to the first subnode (which can be invalid if the device's
437 * node has no subnodes)
438 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600439ofnode dev_read_first_subnode(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600440
441/**
442 * ofnode_next_subnode() - find the next sibling of a subnode
443 *
444 * @node: valid reference to previous node (sibling)
445 * @return reference to the next subnode (which can be invalid if the node
446 * has no more siblings)
447 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600448ofnode dev_read_next_subnode(ofnode node);
Simon Glass34ef0c22017-05-18 20:09:03 -0600449
450/**
451 * dev_read_u8_array_ptr() - find an 8-bit array
452 *
453 * Look up a device's node property and return a pointer to its contents as a
454 * byte array of given length. The property must have at least enough data
455 * for the array (count bytes). It may have more, but this will be ignored.
456 * The data is not copied.
457 *
458 * @dev: device to look up
459 * @propname: name of property to find
460 * @sz: number of array elements
461 * @return pointer to byte array if found, or NULL if the property is not
462 * found or there is not enough data
463 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600464const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
465 size_t sz);
466
Simon Glassfa031f82017-06-12 06:21:30 -0600467/**
468 * dev_read_enabled() - check whether a node is enabled
469 *
470 * This looks for a 'status' property. If this exists, then returns 1 if
471 * the status is 'ok' and 0 otherwise. If there is no status property,
472 * it returns 1 on the assumption that anything mentioned should be enabled
473 * by default.
474 *
475 * @dev: device to examine
476 * @return integer value 0 (not enabled) or 1 (enabled)
477 */
478int dev_read_enabled(struct udevice *dev);
479
Simon Glassf7bfcc42017-07-25 08:29:55 -0600480/**
481 * dev_read_resource() - obtain an indexed resource from a device.
482 *
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900483 * @dev: device to examine
Simon Glassf7bfcc42017-07-25 08:29:55 -0600484 * @index index of the resource to retrieve (0 = first)
485 * @res returns the resource
486 * @return 0 if ok, negative on error
487 */
488int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
489
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900490/**
491 * dev_read_resource_byname() - obtain a named resource from a device.
492 *
493 * @dev: device to examine
494 * @name: name of the resource to retrieve
495 * @res: returns the resource
496 * @return 0 if ok, negative on error
497 */
498int dev_read_resource_byname(struct udevice *dev, const char *name,
499 struct resource *res);
500
Mario Sixaefac062018-01-15 11:07:19 +0100501/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200502 * dev_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +0100503 *
504 * Translate an address from the device-tree into a CPU physical address. This
505 * function walks up the tree and applies the various bus mappings along the
506 * way.
507 *
508 * @dev: device giving the context in which to translate the address
509 * @in_addr: pointer to the address to translate
510 * @return the translated address; OF_BAD_ADDR on error
511 */
512u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
Michal Simekd0e30a02019-01-31 16:30:59 +0100513
514/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200515 * dev_translate_dma_address() - Translate a device-tree DMA address
516 *
517 * Translate a DMA address from the device-tree into a CPU physical address.
518 * This function walks up the tree and applies the various bus mappings along
519 * the way.
520 *
521 * @dev: device giving the context in which to translate the DMA address
522 * @in_addr: pointer to the DMA address to translate
523 * @return the translated DMA address; OF_BAD_ADDR on error
524 */
525u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr);
526
527/**
Michal Simekd0e30a02019-01-31 16:30:59 +0100528 * dev_read_alias_highest_id - Get highest alias id for the given stem
529 * @stem: Alias stem to be examined
530 *
531 * The function travels the lookup table to get the highest alias id for the
532 * given alias stem.
533 * @return alias ID, if found, else -1
534 */
535int dev_read_alias_highest_id(const char *stem);
536
Simon Glasscfdae5c2017-05-18 20:09:04 -0600537#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
538
Masahiro Yamada71d115f2017-12-30 02:00:05 +0900539static inline int dev_read_u32(struct udevice *dev,
540 const char *propname, u32 *outp)
541{
542 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
543}
544
Simon Glasscfdae5c2017-05-18 20:09:04 -0600545static inline int dev_read_u32_default(struct udevice *dev,
546 const char *propname, int def)
547{
548 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
549}
550
Simon Glass6df01f92018-12-10 10:37:37 -0700551static inline int dev_read_s32(struct udevice *dev,
552 const char *propname, s32 *outp)
553{
554 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
555}
556
557static inline int dev_read_s32_default(struct udevice *dev,
558 const char *propname, int def)
559{
560 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
561}
562
563static inline int dev_read_u32u(struct udevice *dev,
564 const char *propname, uint *outp)
565{
566 u32 val;
567 int ret;
568
569 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
570 if (ret)
571 return ret;
572 *outp = val;
573
574 return 0;
575}
576
Simon Glasscfdae5c2017-05-18 20:09:04 -0600577static inline const char *dev_read_string(struct udevice *dev,
578 const char *propname)
579{
580 return ofnode_read_string(dev_ofnode(dev), propname);
581}
582
583static inline bool dev_read_bool(struct udevice *dev, const char *propname)
584{
585 return ofnode_read_bool(dev_ofnode(dev), propname);
586}
587
588static inline ofnode dev_read_subnode(struct udevice *dev,
589 const char *subbnode_name)
590{
591 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
592}
593
594static inline int dev_read_size(struct udevice *dev, const char *propname)
595{
596 return ofnode_read_size(dev_ofnode(dev), propname);
597}
598
599static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
600{
601 return devfdt_get_addr_index(dev, index);
602}
603
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100604static inline fdt_addr_t dev_read_addr_name(struct udevice *dev,
605 const char *name)
606{
607 return devfdt_get_addr_name(dev, name);
608}
609
Simon Glasscfdae5c2017-05-18 20:09:04 -0600610static inline fdt_addr_t dev_read_addr(struct udevice *dev)
611{
612 return devfdt_get_addr(dev);
613}
614
Philipp Tomsich7719b392017-09-11 22:04:12 +0200615static inline void *dev_read_addr_ptr(struct udevice *dev)
616{
617 return devfdt_get_addr_ptr(dev);
618}
619
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200620static inline void *dev_remap_addr(struct udevice *dev)
621{
622 return devfdt_remap_addr(dev);
623}
624
625static inline void *dev_remap_addr_index(struct udevice *dev, int index)
626{
627 return devfdt_remap_addr_index(dev, index);
628}
629
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100630static inline void *dev_remap_addr_name(struct udevice *dev, const char *name)
631{
632 return devfdt_remap_addr_name(dev, name);
633}
634
Simon Glasscfdae5c2017-05-18 20:09:04 -0600635static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
636 const char *propname,
637 fdt_size_t *sizep)
638{
639 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
640}
641
642static inline const char *dev_read_name(struct udevice *dev)
643{
644 return ofnode_get_name(dev_ofnode(dev));
645}
646
647static inline int dev_read_stringlist_search(struct udevice *dev,
648 const char *propname,
649 const char *string)
650{
651 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
652}
653
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200654static inline int dev_read_string_index(struct udevice *dev,
655 const char *propname, int index,
656 const char **outp)
657{
658 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
659}
660
661static inline int dev_read_string_count(struct udevice *dev,
662 const char *propname)
663{
664 return ofnode_read_string_count(dev_ofnode(dev), propname);
665}
666
Simon Glasscfdae5c2017-05-18 20:09:04 -0600667static inline int dev_read_phandle_with_args(struct udevice *dev,
668 const char *list_name, const char *cells_name, int cell_count,
669 int index, struct ofnode_phandle_args *out_args)
670{
671 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
672 cells_name, cell_count, index,
673 out_args);
674}
675
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200676static inline int dev_count_phandle_with_args(struct udevice *dev,
677 const char *list_name, const char *cells_name)
678{
679 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
680 cells_name);
681}
682
Simon Glasscfdae5c2017-05-18 20:09:04 -0600683static inline int dev_read_addr_cells(struct udevice *dev)
684{
Simon Glass4191dc12017-06-12 06:21:31 -0600685 /* NOTE: this call should walk up the parent stack */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600686 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
687}
688
689static inline int dev_read_size_cells(struct udevice *dev)
690{
Simon Glass4191dc12017-06-12 06:21:31 -0600691 /* NOTE: this call should walk up the parent stack */
692 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
693}
694
695static inline int dev_read_simple_addr_cells(struct udevice *dev)
696{
697 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
698}
699
700static inline int dev_read_simple_size_cells(struct udevice *dev)
701{
Simon Glasscfdae5c2017-05-18 20:09:04 -0600702 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
703}
704
705static inline int dev_read_phandle(struct udevice *dev)
706{
707 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
708}
709
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900710static inline const void *dev_read_prop(struct udevice *dev,
711 const char *propname, int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600712{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900713 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600714}
715
716static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
717{
718 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
719 dev_of_offset(dev), devnump);
720}
721
722static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
723 u32 *out_values, size_t sz)
724{
725 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
726}
727
728static inline ofnode dev_read_first_subnode(struct udevice *dev)
729{
730 return ofnode_first_subnode(dev_ofnode(dev));
731}
732
733static inline ofnode dev_read_next_subnode(ofnode node)
734{
735 return ofnode_next_subnode(node);
736}
737
Simon Glass34ef0c22017-05-18 20:09:03 -0600738static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
739 const char *propname, size_t sz)
740{
741 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
742}
743
Simon Glassfa031f82017-06-12 06:21:30 -0600744static inline int dev_read_enabled(struct udevice *dev)
745{
746 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
747}
748
Simon Glassf7bfcc42017-07-25 08:29:55 -0600749static inline int dev_read_resource(struct udevice *dev, uint index,
750 struct resource *res)
751{
752 return ofnode_read_resource(dev_ofnode(dev), index, res);
753}
754
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900755static inline int dev_read_resource_byname(struct udevice *dev,
756 const char *name,
757 struct resource *res)
758{
759 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
760}
761
Mario Sixaefac062018-01-15 11:07:19 +0100762static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
763{
764 return ofnode_translate_address(dev_ofnode(dev), in_addr);
765}
766
Fabien Dessenne22236e02019-05-31 15:11:30 +0200767static inline u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr)
768{
769 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
770}
771
Michal Simekd0e30a02019-01-31 16:30:59 +0100772static inline int dev_read_alias_highest_id(const char *stem)
773{
774 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
775}
776
Simon Glass34ef0c22017-05-18 20:09:03 -0600777#endif /* CONFIG_DM_DEV_READ_INLINE */
778
779/**
780 * dev_for_each_subnode() - Helper function to iterate through subnodes
781 *
782 * This creates a for() loop which works through the subnodes in a device's
783 * device-tree node.
784 *
785 * @subnode: ofnode holding the current subnode
786 * @dev: device to use for interation (struct udevice *)
787 */
788#define dev_for_each_subnode(subnode, dev) \
789 for (subnode = dev_read_first_subnode(dev); \
790 ofnode_valid(subnode); \
791 subnode = ofnode_next_subnode(subnode))
792
793#endif