blob: d37fcb504d3471d48455fb769008a1321af820ab [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
T Karthik Reddy478860d2019-09-02 16:34:30 +020047
Simon Glasscfdae5c2017-05-18 20:09:04 -060048/**
Masahiro Yamada71d115f2017-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 Glasscfdae5c2017-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 Glass34ef0c22017-05-18 20:09:03 -060067
68/**
Simon Glass6df01f92018-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 Reddy478860d2019-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 Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600127const char *dev_read_string(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600136bool dev_read_bool(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600146ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
Simon Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600155int dev_read_size(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600166fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
Simon Glass34ef0c22017-05-18 20:09:03 -0600167
168/**
Sekhar Norif677c6f2019-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 Rojas670361292018-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 Rojasa3181152018-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 Norif677c6f2019-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 Rojasa3181152018-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 Glass34ef0c22017-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 Glasscfdae5c2017-05-18 20:09:04 -0600239fdt_addr_t dev_read_addr(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600240
241/**
Philipp Tomsich7719b392017-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/**
Simon Glass23b27592019-09-15 12:08:58 -0600252 * dev_read_addr_pci() - Read an address and handle PCI address translation
253 *
254 * At present U-Boot does not have address translation logic for PCI in the
255 * livetree implementation (of_addr.c). This special function supports this for
256 * the flat tree implementation.
257 *
258 * This function should be removed (and code should use dev_read() instead)
259 * once:
260 *
261 * 1. PCI address translation is added; and either
262 * 2. everything uses livetree where PCI translation is used (which is feasible
263 * in SPL and U-Boot proper) or PCI address translation is added to
264 * fdtdec_get_addr() and friends.
265 *
266 * @dev: Device to read from
267 * @return address or FDT_ADDR_T_NONE if not found
268 */
269fdt_addr_t dev_read_addr_pci(struct udevice *dev);
270
271/**
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200272 * dev_remap_addr() - Get the reg property of a device as a
273 * memory-mapped I/O pointer
274 *
275 * @dev: Device to read from
276 *
277 * @return pointer or NULL if not found
278 */
279void *dev_remap_addr(struct udevice *dev);
280
281/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600282 * dev_read_addr_size() - get address and size from a device property
283 *
284 * This does no address translation. It simply reads an property that contains
285 * an address and a size value, one after the other.
286 *
287 * @dev: Device to read from
288 * @propname: property to read
289 * @sizep: place to put size value (on success)
290 * @return address value, or FDT_ADDR_T_NONE on error
291 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600292fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
293 fdt_size_t *sizep);
Simon Glass34ef0c22017-05-18 20:09:03 -0600294
295/**
296 * dev_read_name() - get the name of a device's node
297 *
Bin Meng4cf914d2019-07-05 09:23:18 -0700298 * @dev: Device to read from
Simon Glass34ef0c22017-05-18 20:09:03 -0600299 * @return name of node
300 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600301const char *dev_read_name(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600302
303/**
304 * dev_read_stringlist_search() - find string in a string list and return index
305 *
306 * Note that it is possible for this function to succeed on property values
307 * that are not NUL-terminated. That's because the function will stop after
308 * finding the first occurrence of @string. This can for example happen with
309 * small-valued cell properties, such as #address-cells, when searching for
310 * the empty string.
311 *
312 * @dev: device to check
313 * @propname: name of the property containing the string list
314 * @string: string to look up in the string list
315 *
316 * @return:
317 * the index of the string in the list of strings
318 * -ENODATA if the property is not found
319 * -EINVAL on some other error
320 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600321int dev_read_stringlist_search(struct udevice *dev, const char *property,
322 const char *string);
Simon Glass34ef0c22017-05-18 20:09:03 -0600323
324/**
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200325 * dev_read_string_index() - obtain an indexed string from a string list
326 *
327 * @dev: device to examine
328 * @propname: name of the property containing the string list
329 * @index: index of the string to return
330 * @out: return location for the string
331 *
332 * @return:
333 * length of string, if found or -ve error value if not found
334 */
335int dev_read_string_index(struct udevice *dev, const char *propname, int index,
336 const char **outp);
337
338/**
339 * dev_read_string_count() - find the number of strings in a string list
340 *
341 * @dev: device to examine
342 * @propname: name of the property containing the string list
343 * @return:
344 * number of strings in the list, or -ve error value if not found
345 */
346int dev_read_string_count(struct udevice *dev, const char *propname);
347/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600348 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
349 *
350 * This function is useful to parse lists of phandles and their arguments.
351 * Returns 0 on success and fills out_args, on error returns appropriate
352 * errno value.
353 *
354 * Caller is responsible to call of_node_put() on the returned out_args->np
355 * pointer.
356 *
357 * Example:
358 *
359 * phandle1: node1 {
360 * #list-cells = <2>;
361 * }
362 *
363 * phandle2: node2 {
364 * #list-cells = <1>;
365 * }
366 *
367 * node3 {
368 * list = <&phandle1 1 2 &phandle2 3>;
369 * }
370 *
371 * To get a device_node of the `node2' node you may call this:
372 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
373 *
374 * @dev: device whose node containing a list
375 * @list_name: property name that contains a list
376 * @cells_name: property name that specifies phandles' arguments count
377 * @cells_count: Cell count to use if @cells_name is NULL
378 * @index: index of a phandle to parse out
379 * @out_args: optional pointer to output arguments structure (will be filled)
380 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
381 * @list_name does not exist, -EINVAL if a phandle was not found,
382 * @cells_name could not be found, the arguments were truncated or there
383 * were too many arguments.
384 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600385int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
386 const char *cells_name, int cell_count,
387 int index,
388 struct ofnode_phandle_args *out_args);
Simon Glass34ef0c22017-05-18 20:09:03 -0600389
390/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200391 * dev_count_phandle_with_args() - Return phandle number in a list
392 *
393 * This function is usefull to get phandle number contained in a property list.
394 * For example, this allows to allocate the right amount of memory to keep
395 * clock's reference contained into the "clocks" property.
396 *
397 *
398 * @dev: device whose node containing a list
399 * @list_name: property name that contains a list
400 * @cells_name: property name that specifies phandles' arguments count
401 * @Returns number of phandle found on success, on error returns appropriate
402 * errno value.
403 */
404
405int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
406 const char *cells_name);
407
408/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600409 * dev_read_addr_cells() - Get the number of address cells for a device's node
410 *
411 * This walks back up the tree to find the closest #address-cells property
412 * which controls the given node.
413 *
Mario Six16408ee2018-01-15 11:09:36 +0100414 * @dev: device to check
Simon Glass34ef0c22017-05-18 20:09:03 -0600415 * @return number of address cells this node uses
416 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600417int dev_read_addr_cells(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600418
419/**
420 * dev_read_size_cells() - Get the number of size cells for a device's node
421 *
422 * This walks back up the tree to find the closest #size-cells property
423 * which controls the given node.
424 *
Mario Six16408ee2018-01-15 11:09:36 +0100425 * @dev: device to check
Simon Glass34ef0c22017-05-18 20:09:03 -0600426 * @return number of size cells this node uses
427 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600428int dev_read_size_cells(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600429
430/**
Simon Glass4191dc12017-06-12 06:21:31 -0600431 * dev_read_addr_cells() - Get the address cells property in a node
432 *
433 * This function matches fdt_address_cells().
434 *
Mario Six16408ee2018-01-15 11:09:36 +0100435 * @dev: device to check
Simon Glass4191dc12017-06-12 06:21:31 -0600436 * @return number of address cells this node uses
437 */
438int dev_read_simple_addr_cells(struct udevice *dev);
439
440/**
441 * dev_read_size_cells() - Get the size cells property in a node
442 *
443 * This function matches fdt_size_cells().
444 *
Mario Six16408ee2018-01-15 11:09:36 +0100445 * @dev: device to check
Simon Glass4191dc12017-06-12 06:21:31 -0600446 * @return number of size cells this node uses
447 */
448int dev_read_simple_size_cells(struct udevice *dev);
449
450/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600451 * dev_read_phandle() - Get the phandle from a device
452 *
453 * @dev: device to check
454 * @return phandle (1 or greater), or 0 if no phandle or other error
455 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600456int dev_read_phandle(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600457
458/**
459 * dev_read_prop()- - read a property from a device's node
460 *
461 * @dev: device to check
462 * @propname: property to read
463 * @lenp: place to put length on success
464 * @return pointer to property, or NULL if not found
465 */
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900466const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
Simon Glass34ef0c22017-05-18 20:09:03 -0600467
468/**
469 * dev_read_alias_seq() - Get the alias sequence number of a node
470 *
471 * This works out whether a node is pointed to by an alias, and if so, the
472 * sequence number of that alias. Aliases are of the form <base><num> where
473 * <num> is the sequence number. For example spi2 would be sequence number 2.
474 *
475 * @dev: device to look up
476 * @devnump: set to the sequence number if one is found
477 * @return 0 if a sequence was found, -ve if not
478 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600479int dev_read_alias_seq(struct udevice *dev, int *devnump);
Simon Glass34ef0c22017-05-18 20:09:03 -0600480
481/**
482 * dev_read_u32_array() - Find and read an array of 32 bit integers
483 *
484 * Search for a property in a device node and read 32-bit value(s) from
485 * it.
486 *
487 * The out_values is modified only if a valid u32 value can be decoded.
488 *
489 * @dev: device to look up
490 * @propname: name of the property to read
491 * @out_values: pointer to return value, modified only if return value is 0
492 * @sz: number of array elements to read
493 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
494 * property does not have a value, and -EOVERFLOW if the property data isn't
495 * large enough.
496 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600497int dev_read_u32_array(struct udevice *dev, const char *propname,
498 u32 *out_values, size_t sz);
Simon Glass34ef0c22017-05-18 20:09:03 -0600499
500/**
501 * dev_read_first_subnode() - find the first subnode of a device's node
502 *
503 * @dev: device to look up
504 * @return reference to the first subnode (which can be invalid if the device's
505 * node has no subnodes)
506 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600507ofnode dev_read_first_subnode(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600508
509/**
510 * ofnode_next_subnode() - find the next sibling of a subnode
511 *
512 * @node: valid reference to previous node (sibling)
513 * @return reference to the next subnode (which can be invalid if the node
514 * has no more siblings)
515 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600516ofnode dev_read_next_subnode(ofnode node);
Simon Glass34ef0c22017-05-18 20:09:03 -0600517
518/**
519 * dev_read_u8_array_ptr() - find an 8-bit array
520 *
521 * Look up a device's node property and return a pointer to its contents as a
522 * byte array of given length. The property must have at least enough data
523 * for the array (count bytes). It may have more, but this will be ignored.
524 * The data is not copied.
525 *
526 * @dev: device to look up
527 * @propname: name of property to find
528 * @sz: number of array elements
529 * @return pointer to byte array if found, or NULL if the property is not
530 * found or there is not enough data
531 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600532const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
533 size_t sz);
534
Simon Glassfa031f82017-06-12 06:21:30 -0600535/**
536 * dev_read_enabled() - check whether a node is enabled
537 *
538 * This looks for a 'status' property. If this exists, then returns 1 if
539 * the status is 'ok' and 0 otherwise. If there is no status property,
540 * it returns 1 on the assumption that anything mentioned should be enabled
541 * by default.
542 *
543 * @dev: device to examine
544 * @return integer value 0 (not enabled) or 1 (enabled)
545 */
546int dev_read_enabled(struct udevice *dev);
547
Simon Glassf7bfcc42017-07-25 08:29:55 -0600548/**
549 * dev_read_resource() - obtain an indexed resource from a device.
550 *
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900551 * @dev: device to examine
Simon Glassf7bfcc42017-07-25 08:29:55 -0600552 * @index index of the resource to retrieve (0 = first)
553 * @res returns the resource
554 * @return 0 if ok, negative on error
555 */
556int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
557
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900558/**
559 * dev_read_resource_byname() - obtain a named resource from a device.
560 *
561 * @dev: device to examine
562 * @name: name of the resource to retrieve
563 * @res: returns the resource
564 * @return 0 if ok, negative on error
565 */
566int dev_read_resource_byname(struct udevice *dev, const char *name,
567 struct resource *res);
568
Mario Sixaefac062018-01-15 11:07:19 +0100569/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200570 * dev_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +0100571 *
572 * Translate an address from the device-tree into a CPU physical address. This
573 * function walks up the tree and applies the various bus mappings along the
574 * way.
575 *
576 * @dev: device giving the context in which to translate the address
577 * @in_addr: pointer to the address to translate
578 * @return the translated address; OF_BAD_ADDR on error
579 */
580u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
Michal Simekd0e30a02019-01-31 16:30:59 +0100581
582/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200583 * dev_translate_dma_address() - Translate a device-tree DMA address
584 *
585 * Translate a DMA address from the device-tree into a CPU physical address.
586 * This function walks up the tree and applies the various bus mappings along
587 * the way.
588 *
589 * @dev: device giving the context in which to translate the DMA address
590 * @in_addr: pointer to the DMA address to translate
591 * @return the translated DMA address; OF_BAD_ADDR on error
592 */
593u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr);
594
595/**
Michal Simekd0e30a02019-01-31 16:30:59 +0100596 * dev_read_alias_highest_id - Get highest alias id for the given stem
597 * @stem: Alias stem to be examined
598 *
599 * The function travels the lookup table to get the highest alias id for the
600 * given alias stem.
601 * @return alias ID, if found, else -1
602 */
603int dev_read_alias_highest_id(const char *stem);
604
Simon Glasscfdae5c2017-05-18 20:09:04 -0600605#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
606
Masahiro Yamada71d115f2017-12-30 02:00:05 +0900607static inline int dev_read_u32(struct udevice *dev,
608 const char *propname, u32 *outp)
609{
610 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
611}
612
Simon Glasscfdae5c2017-05-18 20:09:04 -0600613static inline int dev_read_u32_default(struct udevice *dev,
614 const char *propname, int def)
615{
616 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
617}
618
Simon Glass6df01f92018-12-10 10:37:37 -0700619static inline int dev_read_s32(struct udevice *dev,
620 const char *propname, s32 *outp)
621{
622 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
623}
624
625static inline int dev_read_s32_default(struct udevice *dev,
626 const char *propname, int def)
627{
628 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
629}
630
631static inline int dev_read_u32u(struct udevice *dev,
632 const char *propname, uint *outp)
633{
634 u32 val;
635 int ret;
636
637 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
638 if (ret)
639 return ret;
640 *outp = val;
641
642 return 0;
643}
644
T Karthik Reddy478860d2019-09-02 16:34:30 +0200645static inline int dev_read_u64(struct udevice *dev,
646 const char *propname, u64 *outp)
647{
648 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
649}
650
651static inline u64 dev_read_u64_default(struct udevice *dev,
652 const char *propname, u64 def)
653{
654 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
655}
656
Simon Glasscfdae5c2017-05-18 20:09:04 -0600657static inline const char *dev_read_string(struct udevice *dev,
658 const char *propname)
659{
660 return ofnode_read_string(dev_ofnode(dev), propname);
661}
662
663static inline bool dev_read_bool(struct udevice *dev, const char *propname)
664{
665 return ofnode_read_bool(dev_ofnode(dev), propname);
666}
667
668static inline ofnode dev_read_subnode(struct udevice *dev,
669 const char *subbnode_name)
670{
671 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
672}
673
674static inline int dev_read_size(struct udevice *dev, const char *propname)
675{
676 return ofnode_read_size(dev_ofnode(dev), propname);
677}
678
679static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
680{
681 return devfdt_get_addr_index(dev, index);
682}
683
Sekhar Norif677c6f2019-08-01 19:12:56 +0530684static inline fdt_addr_t dev_read_addr_size_index(struct udevice *dev,
685 int index,
686 fdt_size_t *size)
687{
688 return devfdt_get_addr_size_index(dev, index, size);
689}
690
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100691static inline fdt_addr_t dev_read_addr_name(struct udevice *dev,
692 const char *name)
693{
694 return devfdt_get_addr_name(dev, name);
695}
696
Sekhar Norif677c6f2019-08-01 19:12:56 +0530697static inline fdt_addr_t dev_read_addr_size_name(struct udevice *dev,
698 const char *name,
699 fdt_size_t *size)
700{
701 return devfdt_get_addr_size_name(dev, name, size);
702}
703
Simon Glasscfdae5c2017-05-18 20:09:04 -0600704static inline fdt_addr_t dev_read_addr(struct udevice *dev)
705{
706 return devfdt_get_addr(dev);
707}
708
Philipp Tomsich7719b392017-09-11 22:04:12 +0200709static inline void *dev_read_addr_ptr(struct udevice *dev)
710{
711 return devfdt_get_addr_ptr(dev);
712}
713
Simon Glass23b27592019-09-15 12:08:58 -0600714static inline fdt_addr_t dev_read_addr_pci(struct udevice *dev)
715{
716 return devfdt_get_addr_pci(dev);
717}
718
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200719static inline void *dev_remap_addr(struct udevice *dev)
720{
721 return devfdt_remap_addr(dev);
722}
723
724static inline void *dev_remap_addr_index(struct udevice *dev, int index)
725{
726 return devfdt_remap_addr_index(dev, index);
727}
728
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100729static inline void *dev_remap_addr_name(struct udevice *dev, const char *name)
730{
731 return devfdt_remap_addr_name(dev, name);
732}
733
Simon Glasscfdae5c2017-05-18 20:09:04 -0600734static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
735 const char *propname,
736 fdt_size_t *sizep)
737{
738 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
739}
740
741static inline const char *dev_read_name(struct udevice *dev)
742{
743 return ofnode_get_name(dev_ofnode(dev));
744}
745
746static inline int dev_read_stringlist_search(struct udevice *dev,
747 const char *propname,
748 const char *string)
749{
750 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
751}
752
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200753static inline int dev_read_string_index(struct udevice *dev,
754 const char *propname, int index,
755 const char **outp)
756{
757 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
758}
759
760static inline int dev_read_string_count(struct udevice *dev,
761 const char *propname)
762{
763 return ofnode_read_string_count(dev_ofnode(dev), propname);
764}
765
Simon Glasscfdae5c2017-05-18 20:09:04 -0600766static inline int dev_read_phandle_with_args(struct udevice *dev,
767 const char *list_name, const char *cells_name, int cell_count,
768 int index, struct ofnode_phandle_args *out_args)
769{
770 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
771 cells_name, cell_count, index,
772 out_args);
773}
774
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200775static inline int dev_count_phandle_with_args(struct udevice *dev,
776 const char *list_name, const char *cells_name)
777{
778 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
779 cells_name);
780}
781
Simon Glasscfdae5c2017-05-18 20:09:04 -0600782static inline int dev_read_addr_cells(struct udevice *dev)
783{
Simon Glass4191dc12017-06-12 06:21:31 -0600784 /* NOTE: this call should walk up the parent stack */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600785 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
786}
787
788static inline int dev_read_size_cells(struct udevice *dev)
789{
Simon Glass4191dc12017-06-12 06:21:31 -0600790 /* NOTE: this call should walk up the parent stack */
791 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
792}
793
794static inline int dev_read_simple_addr_cells(struct udevice *dev)
795{
796 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
797}
798
799static inline int dev_read_simple_size_cells(struct udevice *dev)
800{
Simon Glasscfdae5c2017-05-18 20:09:04 -0600801 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
802}
803
804static inline int dev_read_phandle(struct udevice *dev)
805{
806 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
807}
808
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900809static inline const void *dev_read_prop(struct udevice *dev,
810 const char *propname, int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600811{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900812 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600813}
814
815static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
816{
817 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
818 dev_of_offset(dev), devnump);
819}
820
821static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
822 u32 *out_values, size_t sz)
823{
824 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
825}
826
827static inline ofnode dev_read_first_subnode(struct udevice *dev)
828{
829 return ofnode_first_subnode(dev_ofnode(dev));
830}
831
832static inline ofnode dev_read_next_subnode(ofnode node)
833{
834 return ofnode_next_subnode(node);
835}
836
Simon Glass34ef0c22017-05-18 20:09:03 -0600837static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
838 const char *propname, size_t sz)
839{
840 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
841}
842
Simon Glassfa031f82017-06-12 06:21:30 -0600843static inline int dev_read_enabled(struct udevice *dev)
844{
845 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
846}
847
Simon Glassf7bfcc42017-07-25 08:29:55 -0600848static inline int dev_read_resource(struct udevice *dev, uint index,
849 struct resource *res)
850{
851 return ofnode_read_resource(dev_ofnode(dev), index, res);
852}
853
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900854static inline int dev_read_resource_byname(struct udevice *dev,
855 const char *name,
856 struct resource *res)
857{
858 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
859}
860
Mario Sixaefac062018-01-15 11:07:19 +0100861static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
862{
863 return ofnode_translate_address(dev_ofnode(dev), in_addr);
864}
865
Fabien Dessenne22236e02019-05-31 15:11:30 +0200866static inline u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr)
867{
868 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
869}
870
Michal Simekd0e30a02019-01-31 16:30:59 +0100871static inline int dev_read_alias_highest_id(const char *stem)
872{
873 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
874}
875
Simon Glass34ef0c22017-05-18 20:09:03 -0600876#endif /* CONFIG_DM_DEV_READ_INLINE */
877
878/**
879 * dev_for_each_subnode() - Helper function to iterate through subnodes
880 *
881 * This creates a for() loop which works through the subnodes in a device's
882 * device-tree node.
883 *
884 * @subnode: ofnode holding the current subnode
885 * @dev: device to use for interation (struct udevice *)
886 */
887#define dev_for_each_subnode(subnode, dev) \
888 for (subnode = dev_read_first_subnode(dev); \
889 ofnode_valid(subnode); \
890 subnode = ofnode_next_subnode(subnode))
891
892#endif