blob: 803daf7620c8ab623aa2b0e67fc0991849e49e69 [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
T Karthik Reddy3f3d7712019-09-02 16:34:30 +020047
Simon Glass47a0fd32017-05-18 20:09:04 -060048/**
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090049 * dev_read_u32() - read a 32-bit integer from a device's DT property
50 *
51 * @dev: device to read DT property from
52 * @propname: name of the property to read from
53 * @outp: place to put value (if found)
54 * @return 0 if OK, -ve on error
55 */
56int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp);
57
58/**
Simon Glass47a0fd32017-05-18 20:09:04 -060059 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
60 *
61 * @dev: device to read DT property from
62 * @propname: name of the property to read from
63 * @def: default value to return if the property has no value
64 * @return property value, or @def if not found
65 */
66int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
Simon Glassf11c7ab2017-05-18 20:09:03 -060067
68/**
Simon Glassa1b17e42018-12-10 10:37:37 -070069 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
70 *
71 * @dev: device to read DT property from
72 * @propname: name of the property to read from
73 * @outp: place to put value (if found)
74 * @return 0 if OK, -ve on error
75 */
76int dev_read_s32(struct udevice *dev, const char *propname, s32 *outp);
77
78/**
79 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
80 *
81 * @dev: device to read DT property from
82 * @propname: name of the property to read from
83 * @def: default value to return if the property has no value
84 * @return property value, or @def if not found
85 */
86int dev_read_s32_default(struct udevice *dev, const char *propname, int def);
87
88/**
89 * dev_read_u32u() - read a 32-bit integer from a device's DT property
90 *
91 * This version uses a standard uint type.
92 *
93 * @dev: device to read DT property from
94 * @propname: name of the property to read from
95 * @outp: place to put value (if found)
96 * @return 0 if OK, -ve on error
97 */
98int dev_read_u32u(struct udevice *dev, const char *propname, uint *outp);
99
100/**
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200101 * dev_read_u64() - read a 64-bit integer from a device's DT property
102 *
103 * @dev: device to read DT property from
104 * @propname: name of the property to read from
105 * @outp: place to put value (if found)
106 * @return 0 if OK, -ve on error
107 */
108int dev_read_u64(struct udevice *dev, const char *propname, u64 *outp);
109
110/**
111 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
112 *
113 * @dev: device to read DT property from
114 * @propname: name of the property to read from
115 * @def: default value to return if the property has no value
116 * @return property value, or @def if not found
117 */
118u64 dev_read_u64_default(struct udevice *dev, const char *propname, u64 def);
119
120/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600121 * dev_read_string() - Read a string from a device's DT property
122 *
123 * @dev: device to read DT property from
124 * @propname: name of the property to read
125 * @return string from property value, or NULL if there is no such property
126 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600127const char *dev_read_string(struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600128
129/**
130 * dev_read_bool() - read a boolean value from a device's DT property
131 *
132 * @dev: device to read DT property from
133 * @propname: name of property to read
134 * @return true if property is present (meaning true), false if not present
135 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600136bool dev_read_bool(struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600137
138/**
139 * dev_read_subnode() - find a named subnode of a device
140 *
141 * @dev: device whose DT node contains the subnode
142 * @subnode_name: name of subnode to find
143 * @return reference to subnode (which can be invalid if there is no such
144 * subnode)
145 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600146ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600147
148/**
149 * dev_read_size() - read the size of a property
150 *
151 * @dev: device to check
152 * @propname: property to check
153 * @return size of property if present, or -EINVAL if not
154 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600155int dev_read_size(struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600156
157/**
158 * dev_read_addr_index() - Get the indexed reg property of a device
159 *
160 * @dev: Device to read from
161 * @index: the 'reg' property can hold a list of <addr, size> pairs
162 * and @index is used to select which one is required
163 *
164 * @return address or FDT_ADDR_T_NONE if not found
165 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600166fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600167
168/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530169 * dev_read_addr_size_index() - Get the indexed reg property of a device
170 *
171 * @dev: Device to read from
172 * @index: the 'reg' property can hold a list of <addr, size> pairs
173 * and @index is used to select which one is required
174 * @size: place to put size value (on success)
175 *
176 * @return address or FDT_ADDR_T_NONE if not found
177 */
178fdt_addr_t dev_read_addr_size_index(struct udevice *dev, int index,
179 fdt_size_t *size);
180
181/**
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200182 * dev_remap_addr_index() - Get the indexed reg property of a device
183 * as a memory-mapped I/O pointer
184 *
185 * @dev: Device to read from
186 * @index: the 'reg' property can hold a list of <addr, size> pairs
187 * and @index is used to select which one is required
188 *
189 * @return pointer or NULL if not found
190 */
191void *dev_remap_addr_index(struct udevice *dev, int index);
192
193/**
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100194 * dev_read_addr_name() - Get the reg property of a device, indexed by name
195 *
196 * @dev: Device to read from
197 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
198 * 'reg-names' property providing named-based identification. @index
199 * indicates the value to search for in 'reg-names'.
200 *
201 * @return address or FDT_ADDR_T_NONE if not found
202 */
203fdt_addr_t dev_read_addr_name(struct udevice *dev, const char* name);
204
205/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530206 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
207 *
208 * @dev: Device to read from
209 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
210 * 'reg-names' property providing named-based identification. @index
211 * indicates the value to search for in 'reg-names'.
212 * @size: place to put size value (on success)
213 *
214 * @return address or FDT_ADDR_T_NONE if not found
215 */
216fdt_addr_t dev_read_addr_size_name(struct udevice *dev, const char *name,
217 fdt_size_t *size);
218
219/**
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100220 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
221 * as a memory-mapped I/O pointer
222 *
223 * @dev: Device to read from
224 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
225 * 'reg-names' property providing named-based identification. @index
226 * indicates the value to search for in 'reg-names'.
227 *
228 * @return pointer or NULL if not found
229 */
230void *dev_remap_addr_name(struct udevice *dev, const char* name);
231
232/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600233 * dev_read_addr() - Get the reg property of a device
234 *
235 * @dev: Device to read from
236 *
237 * @return address or FDT_ADDR_T_NONE if not found
238 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600239fdt_addr_t dev_read_addr(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600240
241/**
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200242 * dev_read_addr_ptr() - Get the reg property of a device
243 * as a pointer
244 *
245 * @dev: Device to read from
246 *
247 * @return pointer or NULL if not found
248 */
249void *dev_read_addr_ptr(struct udevice *dev);
250
251/**
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200252 * dev_remap_addr() - Get the reg property of a device as a
253 * memory-mapped I/O pointer
254 *
255 * @dev: Device to read from
256 *
257 * @return pointer or NULL if not found
258 */
259void *dev_remap_addr(struct udevice *dev);
260
261/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600262 * dev_read_addr_size() - get address and size from a device property
263 *
264 * This does no address translation. It simply reads an property that contains
265 * an address and a size value, one after the other.
266 *
267 * @dev: Device to read from
268 * @propname: property to read
269 * @sizep: place to put size value (on success)
270 * @return address value, or FDT_ADDR_T_NONE on error
271 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600272fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
273 fdt_size_t *sizep);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600274
275/**
276 * dev_read_name() - get the name of a device's node
277 *
Bin Meng15d61d02019-07-05 09:23:18 -0700278 * @dev: Device to read from
Simon Glassf11c7ab2017-05-18 20:09:03 -0600279 * @return name of node
280 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600281const char *dev_read_name(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600282
283/**
284 * dev_read_stringlist_search() - find string in a string list and return index
285 *
286 * Note that it is possible for this function to succeed on property values
287 * that are not NUL-terminated. That's because the function will stop after
288 * finding the first occurrence of @string. This can for example happen with
289 * small-valued cell properties, such as #address-cells, when searching for
290 * the empty string.
291 *
292 * @dev: device to check
293 * @propname: name of the property containing the string list
294 * @string: string to look up in the string list
295 *
296 * @return:
297 * the index of the string in the list of strings
298 * -ENODATA if the property is not found
299 * -EINVAL on some other error
300 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600301int dev_read_stringlist_search(struct udevice *dev, const char *property,
302 const char *string);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600303
304/**
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200305 * dev_read_string_index() - obtain an indexed string from a string list
306 *
307 * @dev: device to examine
308 * @propname: name of the property containing the string list
309 * @index: index of the string to return
310 * @out: return location for the string
311 *
312 * @return:
313 * length of string, if found or -ve error value if not found
314 */
315int dev_read_string_index(struct udevice *dev, const char *propname, int index,
316 const char **outp);
317
318/**
319 * dev_read_string_count() - find the number of strings in a string list
320 *
321 * @dev: device to examine
322 * @propname: name of the property containing the string list
323 * @return:
324 * number of strings in the list, or -ve error value if not found
325 */
326int dev_read_string_count(struct udevice *dev, const char *propname);
327/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600328 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
329 *
330 * This function is useful to parse lists of phandles and their arguments.
331 * Returns 0 on success and fills out_args, on error returns appropriate
332 * errno value.
333 *
334 * Caller is responsible to call of_node_put() on the returned out_args->np
335 * pointer.
336 *
337 * Example:
338 *
339 * phandle1: node1 {
340 * #list-cells = <2>;
341 * }
342 *
343 * phandle2: node2 {
344 * #list-cells = <1>;
345 * }
346 *
347 * node3 {
348 * list = <&phandle1 1 2 &phandle2 3>;
349 * }
350 *
351 * To get a device_node of the `node2' node you may call this:
352 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
353 *
354 * @dev: device whose node containing a list
355 * @list_name: property name that contains a list
356 * @cells_name: property name that specifies phandles' arguments count
357 * @cells_count: Cell count to use if @cells_name is NULL
358 * @index: index of a phandle to parse out
359 * @out_args: optional pointer to output arguments structure (will be filled)
360 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
361 * @list_name does not exist, -EINVAL if a phandle was not found,
362 * @cells_name could not be found, the arguments were truncated or there
363 * were too many arguments.
364 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600365int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
366 const char *cells_name, int cell_count,
367 int index,
368 struct ofnode_phandle_args *out_args);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600369
370/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200371 * dev_count_phandle_with_args() - Return phandle number in a list
372 *
373 * This function is usefull to get phandle number contained in a property list.
374 * For example, this allows to allocate the right amount of memory to keep
375 * clock's reference contained into the "clocks" property.
376 *
377 *
378 * @dev: device whose node containing a list
379 * @list_name: property name that contains a list
380 * @cells_name: property name that specifies phandles' arguments count
381 * @Returns number of phandle found on success, on error returns appropriate
382 * errno value.
383 */
384
385int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
386 const char *cells_name);
387
388/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600389 * dev_read_addr_cells() - Get the number of address cells for a device's node
390 *
391 * This walks back up the tree to find the closest #address-cells property
392 * which controls the given node.
393 *
Mario Six7ba50412018-01-15 11:09:36 +0100394 * @dev: device to check
Simon Glassf11c7ab2017-05-18 20:09:03 -0600395 * @return number of address cells this node uses
396 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600397int dev_read_addr_cells(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600398
399/**
400 * dev_read_size_cells() - Get the number of size cells for a device's node
401 *
402 * This walks back up the tree to find the closest #size-cells property
403 * which controls the given node.
404 *
Mario Six7ba50412018-01-15 11:09:36 +0100405 * @dev: device to check
Simon Glassf11c7ab2017-05-18 20:09:03 -0600406 * @return number of size cells this node uses
407 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600408int dev_read_size_cells(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600409
410/**
Simon Glass878d68c2017-06-12 06:21:31 -0600411 * dev_read_addr_cells() - Get the address cells property in a node
412 *
413 * This function matches fdt_address_cells().
414 *
Mario Six7ba50412018-01-15 11:09:36 +0100415 * @dev: device to check
Simon Glass878d68c2017-06-12 06:21:31 -0600416 * @return number of address cells this node uses
417 */
418int dev_read_simple_addr_cells(struct udevice *dev);
419
420/**
421 * dev_read_size_cells() - Get the size cells property in a node
422 *
423 * This function matches fdt_size_cells().
424 *
Mario Six7ba50412018-01-15 11:09:36 +0100425 * @dev: device to check
Simon Glass878d68c2017-06-12 06:21:31 -0600426 * @return number of size cells this node uses
427 */
428int dev_read_simple_size_cells(struct udevice *dev);
429
430/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600431 * dev_read_phandle() - Get the phandle from a device
432 *
433 * @dev: device to check
434 * @return phandle (1 or greater), or 0 if no phandle or other error
435 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600436int dev_read_phandle(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600437
438/**
439 * dev_read_prop()- - read a property from a device's node
440 *
441 * @dev: device to check
442 * @propname: property to read
443 * @lenp: place to put length on success
444 * @return pointer to property, or NULL if not found
445 */
Masahiro Yamadafd736212017-07-17 12:18:39 +0900446const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600447
448/**
449 * dev_read_alias_seq() - Get the alias sequence number of a node
450 *
451 * This works out whether a node is pointed to by an alias, and if so, the
452 * sequence number of that alias. Aliases are of the form <base><num> where
453 * <num> is the sequence number. For example spi2 would be sequence number 2.
454 *
455 * @dev: device to look up
456 * @devnump: set to the sequence number if one is found
457 * @return 0 if a sequence was found, -ve if not
458 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600459int dev_read_alias_seq(struct udevice *dev, int *devnump);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600460
461/**
462 * dev_read_u32_array() - Find and read an array of 32 bit integers
463 *
464 * Search for a property in a device node and read 32-bit value(s) from
465 * it.
466 *
467 * The out_values is modified only if a valid u32 value can be decoded.
468 *
469 * @dev: device to look up
470 * @propname: name of the property to read
471 * @out_values: pointer to return value, modified only if return value is 0
472 * @sz: number of array elements to read
473 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
474 * property does not have a value, and -EOVERFLOW if the property data isn't
475 * large enough.
476 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600477int dev_read_u32_array(struct udevice *dev, const char *propname,
478 u32 *out_values, size_t sz);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600479
480/**
481 * dev_read_first_subnode() - find the first subnode of a device's node
482 *
483 * @dev: device to look up
484 * @return reference to the first subnode (which can be invalid if the device's
485 * node has no subnodes)
486 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600487ofnode dev_read_first_subnode(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600488
489/**
490 * ofnode_next_subnode() - find the next sibling of a subnode
491 *
492 * @node: valid reference to previous node (sibling)
493 * @return reference to the next subnode (which can be invalid if the node
494 * has no more siblings)
495 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600496ofnode dev_read_next_subnode(ofnode node);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600497
498/**
499 * dev_read_u8_array_ptr() - find an 8-bit array
500 *
501 * Look up a device's node property and return a pointer to its contents as a
502 * byte array of given length. The property must have at least enough data
503 * for the array (count bytes). It may have more, but this will be ignored.
504 * The data is not copied.
505 *
506 * @dev: device to look up
507 * @propname: name of property to find
508 * @sz: number of array elements
509 * @return pointer to byte array if found, or NULL if the property is not
510 * found or there is not enough data
511 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600512const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
513 size_t sz);
514
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600515/**
516 * dev_read_enabled() - check whether a node is enabled
517 *
518 * This looks for a 'status' property. If this exists, then returns 1 if
519 * the status is 'ok' and 0 otherwise. If there is no status property,
520 * it returns 1 on the assumption that anything mentioned should be enabled
521 * by default.
522 *
523 * @dev: device to examine
524 * @return integer value 0 (not enabled) or 1 (enabled)
525 */
526int dev_read_enabled(struct udevice *dev);
527
Simon Glassdcf98852017-07-25 08:29:55 -0600528/**
529 * dev_read_resource() - obtain an indexed resource from a device.
530 *
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900531 * @dev: device to examine
Simon Glassdcf98852017-07-25 08:29:55 -0600532 * @index index of the resource to retrieve (0 = first)
533 * @res returns the resource
534 * @return 0 if ok, negative on error
535 */
536int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
537
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900538/**
539 * dev_read_resource_byname() - obtain a named resource from a device.
540 *
541 * @dev: device to examine
542 * @name: name of the resource to retrieve
543 * @res: returns the resource
544 * @return 0 if ok, negative on error
545 */
546int dev_read_resource_byname(struct udevice *dev, const char *name,
547 struct resource *res);
548
Mario Six147c6072018-01-15 11:07:19 +0100549/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200550 * dev_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +0100551 *
552 * Translate an address from the device-tree into a CPU physical address. This
553 * function walks up the tree and applies the various bus mappings along the
554 * way.
555 *
556 * @dev: device giving the context in which to translate the address
557 * @in_addr: pointer to the address to translate
558 * @return the translated address; OF_BAD_ADDR on error
559 */
560u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
Michal Simek83e4c7e2019-01-31 16:30:59 +0100561
562/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200563 * dev_translate_dma_address() - Translate a device-tree DMA address
564 *
565 * Translate a DMA address from the device-tree into a CPU physical address.
566 * This function walks up the tree and applies the various bus mappings along
567 * the way.
568 *
569 * @dev: device giving the context in which to translate the DMA address
570 * @in_addr: pointer to the DMA address to translate
571 * @return the translated DMA address; OF_BAD_ADDR on error
572 */
573u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr);
574
575/**
Michal Simek83e4c7e2019-01-31 16:30:59 +0100576 * dev_read_alias_highest_id - Get highest alias id for the given stem
577 * @stem: Alias stem to be examined
578 *
579 * The function travels the lookup table to get the highest alias id for the
580 * given alias stem.
581 * @return alias ID, if found, else -1
582 */
583int dev_read_alias_highest_id(const char *stem);
584
Simon Glass47a0fd32017-05-18 20:09:04 -0600585#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
586
Masahiro Yamada3ab48f62017-12-30 02:00:05 +0900587static inline int dev_read_u32(struct udevice *dev,
588 const char *propname, u32 *outp)
589{
590 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
591}
592
Simon Glass47a0fd32017-05-18 20:09:04 -0600593static inline int dev_read_u32_default(struct udevice *dev,
594 const char *propname, int def)
595{
596 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
597}
598
Simon Glassa1b17e42018-12-10 10:37:37 -0700599static inline int dev_read_s32(struct udevice *dev,
600 const char *propname, s32 *outp)
601{
602 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
603}
604
605static inline int dev_read_s32_default(struct udevice *dev,
606 const char *propname, int def)
607{
608 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
609}
610
611static inline int dev_read_u32u(struct udevice *dev,
612 const char *propname, uint *outp)
613{
614 u32 val;
615 int ret;
616
617 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
618 if (ret)
619 return ret;
620 *outp = val;
621
622 return 0;
623}
624
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200625static inline int dev_read_u64(struct udevice *dev,
626 const char *propname, u64 *outp)
627{
628 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
629}
630
631static inline u64 dev_read_u64_default(struct udevice *dev,
632 const char *propname, u64 def)
633{
634 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
635}
636
Simon Glass47a0fd32017-05-18 20:09:04 -0600637static inline const char *dev_read_string(struct udevice *dev,
638 const char *propname)
639{
640 return ofnode_read_string(dev_ofnode(dev), propname);
641}
642
643static inline bool dev_read_bool(struct udevice *dev, const char *propname)
644{
645 return ofnode_read_bool(dev_ofnode(dev), propname);
646}
647
648static inline ofnode dev_read_subnode(struct udevice *dev,
649 const char *subbnode_name)
650{
651 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
652}
653
654static inline int dev_read_size(struct udevice *dev, const char *propname)
655{
656 return ofnode_read_size(dev_ofnode(dev), propname);
657}
658
659static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
660{
661 return devfdt_get_addr_index(dev, index);
662}
663
Sekhar Norif5b90472019-08-01 19:12:56 +0530664static inline fdt_addr_t dev_read_addr_size_index(struct udevice *dev,
665 int index,
666 fdt_size_t *size)
667{
668 return devfdt_get_addr_size_index(dev, index, size);
669}
670
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100671static inline fdt_addr_t dev_read_addr_name(struct udevice *dev,
672 const char *name)
673{
674 return devfdt_get_addr_name(dev, name);
675}
676
Sekhar Norif5b90472019-08-01 19:12:56 +0530677static inline fdt_addr_t dev_read_addr_size_name(struct udevice *dev,
678 const char *name,
679 fdt_size_t *size)
680{
681 return devfdt_get_addr_size_name(dev, name, size);
682}
683
Simon Glass47a0fd32017-05-18 20:09:04 -0600684static inline fdt_addr_t dev_read_addr(struct udevice *dev)
685{
686 return devfdt_get_addr(dev);
687}
688
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200689static inline void *dev_read_addr_ptr(struct udevice *dev)
690{
691 return devfdt_get_addr_ptr(dev);
692}
693
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200694static inline void *dev_remap_addr(struct udevice *dev)
695{
696 return devfdt_remap_addr(dev);
697}
698
699static inline void *dev_remap_addr_index(struct udevice *dev, int index)
700{
701 return devfdt_remap_addr_index(dev, index);
702}
703
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100704static inline void *dev_remap_addr_name(struct udevice *dev, const char *name)
705{
706 return devfdt_remap_addr_name(dev, name);
707}
708
Simon Glass47a0fd32017-05-18 20:09:04 -0600709static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
710 const char *propname,
711 fdt_size_t *sizep)
712{
713 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
714}
715
716static inline const char *dev_read_name(struct udevice *dev)
717{
718 return ofnode_get_name(dev_ofnode(dev));
719}
720
721static inline int dev_read_stringlist_search(struct udevice *dev,
722 const char *propname,
723 const char *string)
724{
725 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
726}
727
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200728static inline int dev_read_string_index(struct udevice *dev,
729 const char *propname, int index,
730 const char **outp)
731{
732 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
733}
734
735static inline int dev_read_string_count(struct udevice *dev,
736 const char *propname)
737{
738 return ofnode_read_string_count(dev_ofnode(dev), propname);
739}
740
Simon Glass47a0fd32017-05-18 20:09:04 -0600741static inline int dev_read_phandle_with_args(struct udevice *dev,
742 const char *list_name, const char *cells_name, int cell_count,
743 int index, struct ofnode_phandle_args *out_args)
744{
745 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
746 cells_name, cell_count, index,
747 out_args);
748}
749
Patrice Chotard642346a2017-07-18 11:57:08 +0200750static inline int dev_count_phandle_with_args(struct udevice *dev,
751 const char *list_name, const char *cells_name)
752{
753 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
754 cells_name);
755}
756
Simon Glass47a0fd32017-05-18 20:09:04 -0600757static inline int dev_read_addr_cells(struct udevice *dev)
758{
Simon Glass878d68c2017-06-12 06:21:31 -0600759 /* NOTE: this call should walk up the parent stack */
Simon Glass47a0fd32017-05-18 20:09:04 -0600760 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
761}
762
763static inline int dev_read_size_cells(struct udevice *dev)
764{
Simon Glass878d68c2017-06-12 06:21:31 -0600765 /* NOTE: this call should walk up the parent stack */
766 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
767}
768
769static inline int dev_read_simple_addr_cells(struct udevice *dev)
770{
771 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
772}
773
774static inline int dev_read_simple_size_cells(struct udevice *dev)
775{
Simon Glass47a0fd32017-05-18 20:09:04 -0600776 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
777}
778
779static inline int dev_read_phandle(struct udevice *dev)
780{
781 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
782}
783
Masahiro Yamadafd736212017-07-17 12:18:39 +0900784static inline const void *dev_read_prop(struct udevice *dev,
785 const char *propname, int *lenp)
Simon Glass47a0fd32017-05-18 20:09:04 -0600786{
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900787 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glass47a0fd32017-05-18 20:09:04 -0600788}
789
790static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
791{
792 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
793 dev_of_offset(dev), devnump);
794}
795
796static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
797 u32 *out_values, size_t sz)
798{
799 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
800}
801
802static inline ofnode dev_read_first_subnode(struct udevice *dev)
803{
804 return ofnode_first_subnode(dev_ofnode(dev));
805}
806
807static inline ofnode dev_read_next_subnode(ofnode node)
808{
809 return ofnode_next_subnode(node);
810}
811
Simon Glassf11c7ab2017-05-18 20:09:03 -0600812static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
813 const char *propname, size_t sz)
814{
815 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
816}
817
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600818static inline int dev_read_enabled(struct udevice *dev)
819{
820 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
821}
822
Simon Glassdcf98852017-07-25 08:29:55 -0600823static inline int dev_read_resource(struct udevice *dev, uint index,
824 struct resource *res)
825{
826 return ofnode_read_resource(dev_ofnode(dev), index, res);
827}
828
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900829static inline int dev_read_resource_byname(struct udevice *dev,
830 const char *name,
831 struct resource *res)
832{
833 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
834}
835
Mario Six147c6072018-01-15 11:07:19 +0100836static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
837{
838 return ofnode_translate_address(dev_ofnode(dev), in_addr);
839}
840
Fabien Dessenne641067f2019-05-31 15:11:30 +0200841static inline u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr)
842{
843 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
844}
845
Michal Simek83e4c7e2019-01-31 16:30:59 +0100846static inline int dev_read_alias_highest_id(const char *stem)
847{
848 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
849}
850
Simon Glassf11c7ab2017-05-18 20:09:03 -0600851#endif /* CONFIG_DM_DEV_READ_INLINE */
852
853/**
854 * dev_for_each_subnode() - Helper function to iterate through subnodes
855 *
856 * This creates a for() loop which works through the subnodes in a device's
857 * device-tree node.
858 *
859 * @subnode: ofnode holding the current subnode
860 * @dev: device to use for interation (struct udevice *)
861 */
862#define dev_for_each_subnode(subnode, dev) \
863 for (subnode = dev_read_first_subnode(dev); \
864 ofnode_valid(subnode); \
865 subnode = ofnode_next_subnode(subnode))
866
867#endif