blob: da8c7f25e7ca08421fe4f4d13cc368b051ff31f1 [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)
Simon Glass88b3a372020-01-27 08:49:40 -070019static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glassf11c7ab2017-05-18 20:09:03 -060020{
21 return ofnode_to_np(dev->node);
22}
23#else
Simon Glass88b3a372020-01-27 08:49:40 -070024static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glassf11c7ab2017-05-18 20:09:03 -060025{
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 */
Simon Glassfc347fb2020-01-27 08:49:36 -070036static inline ofnode dev_ofnode(const struct udevice *dev)
Simon Glassf11c7ab2017-05-18 20:09:03 -060037{
38 return dev->node;
39}
40
Simon Glassfc347fb2020-01-27 08:49:36 -070041static inline bool dev_of_valid(const struct udevice *dev)
Simon Glassf11c7ab2017-05-18 20:09:03 -060042{
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 */
Simon Glass88b3a372020-01-27 08:49:40 -070056int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090057
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 */
Simon Glass88b3a372020-01-27 08:49:40 -070066int dev_read_u32_default(const struct udevice *dev, const char *propname,
67 int def);
Simon Glassf11c7ab2017-05-18 20:09:03 -060068
69/**
Simon Glassa1b17e42018-12-10 10:37:37 -070070 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
71 *
72 * @dev: device to read DT property from
73 * @propname: name of the property to read from
74 * @outp: place to put value (if found)
75 * @return 0 if OK, -ve on error
76 */
Simon Glass88b3a372020-01-27 08:49:40 -070077int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
Simon Glassa1b17e42018-12-10 10:37:37 -070078
79/**
80 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
81 *
82 * @dev: device to read DT property from
83 * @propname: name of the property to read from
84 * @def: default value to return if the property has no value
85 * @return property value, or @def if not found
86 */
Simon Glass88b3a372020-01-27 08:49:40 -070087int dev_read_s32_default(const struct udevice *dev, const char *propname,
88 int def);
Simon Glassa1b17e42018-12-10 10:37:37 -070089
90/**
91 * dev_read_u32u() - read a 32-bit integer from a device's DT property
92 *
93 * This version uses a standard uint type.
94 *
95 * @dev: device to read DT property from
96 * @propname: name of the property to read from
97 * @outp: place to put value (if found)
98 * @return 0 if OK, -ve on error
99 */
Simon Glass88b3a372020-01-27 08:49:40 -0700100int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
Simon Glassa1b17e42018-12-10 10:37:37 -0700101
102/**
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200103 * dev_read_u64() - read a 64-bit integer from a device's DT property
104 *
105 * @dev: device to read DT property from
106 * @propname: name of the property to read from
107 * @outp: place to put value (if found)
108 * @return 0 if OK, -ve on error
109 */
Simon Glass88b3a372020-01-27 08:49:40 -0700110int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200111
112/**
113 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
114 *
115 * @dev: device to read DT property from
116 * @propname: name of the property to read from
117 * @def: default value to return if the property has no value
118 * @return property value, or @def if not found
119 */
Simon Glass88b3a372020-01-27 08:49:40 -0700120u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
121 u64 def);
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200122
123/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600124 * dev_read_string() - Read a string from a device's DT property
125 *
126 * @dev: device to read DT property from
127 * @propname: name of the property to read
128 * @return string from property value, or NULL if there is no such property
129 */
Simon Glass88b3a372020-01-27 08:49:40 -0700130const char *dev_read_string(const struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600131
132/**
133 * dev_read_bool() - read a boolean value from a device's DT property
134 *
135 * @dev: device to read DT property from
136 * @propname: name of property to read
137 * @return true if property is present (meaning true), false if not present
138 */
Simon Glass88b3a372020-01-27 08:49:40 -0700139bool dev_read_bool(const struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600140
141/**
142 * dev_read_subnode() - find a named subnode of a device
143 *
144 * @dev: device whose DT node contains the subnode
145 * @subnode_name: name of subnode to find
146 * @return reference to subnode (which can be invalid if there is no such
147 * subnode)
148 */
Simon Glass88b3a372020-01-27 08:49:40 -0700149ofnode dev_read_subnode(const struct udevice *dev, const char *subbnode_name);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600150
151/**
152 * dev_read_size() - read the size of a property
153 *
154 * @dev: device to check
155 * @propname: property to check
156 * @return size of property if present, or -EINVAL if not
157 */
Simon Glass88b3a372020-01-27 08:49:40 -0700158int dev_read_size(const struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600159
160/**
161 * dev_read_addr_index() - Get the indexed reg property of a device
162 *
163 * @dev: Device to read from
164 * @index: the 'reg' property can hold a list of <addr, size> pairs
165 * and @index is used to select which one is required
166 *
167 * @return address or FDT_ADDR_T_NONE if not found
168 */
Simon Glass88b3a372020-01-27 08:49:40 -0700169fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600170
171/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530172 * dev_read_addr_size_index() - Get the indexed reg property of a device
173 *
174 * @dev: Device to read from
175 * @index: the 'reg' property can hold a list of <addr, size> pairs
176 * and @index is used to select which one is required
177 * @size: place to put size value (on success)
178 *
179 * @return address or FDT_ADDR_T_NONE if not found
180 */
Simon Glass88b3a372020-01-27 08:49:40 -0700181fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif5b90472019-08-01 19:12:56 +0530182 fdt_size_t *size);
183
184/**
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200185 * dev_remap_addr_index() - Get the indexed reg property of a device
186 * as a memory-mapped I/O pointer
187 *
188 * @dev: Device to read from
189 * @index: the 'reg' property can hold a list of <addr, size> pairs
190 * and @index is used to select which one is required
191 *
192 * @return pointer or NULL if not found
193 */
Simon Glass88b3a372020-01-27 08:49:40 -0700194void *dev_remap_addr_index(const struct udevice *dev, int index);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200195
196/**
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100197 * dev_read_addr_name() - Get the reg property of a device, indexed by name
198 *
199 * @dev: Device to read from
200 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
201 * 'reg-names' property providing named-based identification. @index
202 * indicates the value to search for in 'reg-names'.
203 *
204 * @return address or FDT_ADDR_T_NONE if not found
205 */
Simon Glass88b3a372020-01-27 08:49:40 -0700206fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100207
208/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530209 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
210 *
211 * @dev: Device to read from
212 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
213 * 'reg-names' property providing named-based identification. @index
214 * indicates the value to search for in 'reg-names'.
215 * @size: place to put size value (on success)
216 *
217 * @return address or FDT_ADDR_T_NONE if not found
218 */
Simon Glass88b3a372020-01-27 08:49:40 -0700219fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif5b90472019-08-01 19:12:56 +0530220 fdt_size_t *size);
221
222/**
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100223 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
224 * as a memory-mapped I/O pointer
225 *
226 * @dev: Device to read from
227 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
228 * 'reg-names' property providing named-based identification. @index
229 * indicates the value to search for in 'reg-names'.
230 *
231 * @return pointer or NULL if not found
232 */
Simon Glass88b3a372020-01-27 08:49:40 -0700233void *dev_remap_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100234
235/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600236 * dev_read_addr() - Get the reg property of a device
237 *
238 * @dev: Device to read from
239 *
240 * @return address or FDT_ADDR_T_NONE if not found
241 */
Simon Glass88b3a372020-01-27 08:49:40 -0700242fdt_addr_t dev_read_addr(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600243
244/**
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200245 * dev_read_addr_ptr() - Get the reg property of a device
246 * as a pointer
247 *
248 * @dev: Device to read from
249 *
250 * @return pointer or NULL if not found
251 */
Simon Glass88b3a372020-01-27 08:49:40 -0700252void *dev_read_addr_ptr(const struct udevice *dev);
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200253
254/**
Simon Glass33c215a2019-09-15 12:08:58 -0600255 * dev_read_addr_pci() - Read an address and handle PCI address translation
256 *
257 * At present U-Boot does not have address translation logic for PCI in the
258 * livetree implementation (of_addr.c). This special function supports this for
259 * the flat tree implementation.
260 *
261 * This function should be removed (and code should use dev_read() instead)
262 * once:
263 *
264 * 1. PCI address translation is added; and either
265 * 2. everything uses livetree where PCI translation is used (which is feasible
266 * in SPL and U-Boot proper) or PCI address translation is added to
267 * fdtdec_get_addr() and friends.
268 *
269 * @dev: Device to read from
270 * @return address or FDT_ADDR_T_NONE if not found
271 */
Simon Glass88b3a372020-01-27 08:49:40 -0700272fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
Simon Glass33c215a2019-09-15 12:08:58 -0600273
274/**
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200275 * dev_remap_addr() - Get the reg property of a device as a
276 * memory-mapped I/O pointer
277 *
278 * @dev: Device to read from
279 *
280 * @return pointer or NULL if not found
281 */
Simon Glass88b3a372020-01-27 08:49:40 -0700282void *dev_remap_addr(const struct udevice *dev);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200283
284/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600285 * dev_read_addr_size() - get address and size from a device property
286 *
287 * This does no address translation. It simply reads an property that contains
288 * an address and a size value, one after the other.
289 *
290 * @dev: Device to read from
291 * @propname: property to read
292 * @sizep: place to put size value (on success)
293 * @return address value, or FDT_ADDR_T_NONE on error
294 */
Simon Glass88b3a372020-01-27 08:49:40 -0700295fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname,
296 fdt_size_t *sizep);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600297
298/**
299 * dev_read_name() - get the name of a device's node
300 *
Bin Meng15d61d02019-07-05 09:23:18 -0700301 * @dev: Device to read from
Simon Glassf11c7ab2017-05-18 20:09:03 -0600302 * @return name of node
303 */
Simon Glass88b3a372020-01-27 08:49:40 -0700304const char *dev_read_name(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600305
306/**
307 * dev_read_stringlist_search() - find string in a string list and return index
308 *
309 * Note that it is possible for this function to succeed on property values
310 * that are not NUL-terminated. That's because the function will stop after
311 * finding the first occurrence of @string. This can for example happen with
312 * small-valued cell properties, such as #address-cells, when searching for
313 * the empty string.
314 *
315 * @dev: device to check
316 * @propname: name of the property containing the string list
317 * @string: string to look up in the string list
318 *
319 * @return:
320 * the index of the string in the list of strings
321 * -ENODATA if the property is not found
322 * -EINVAL on some other error
323 */
Simon Glass88b3a372020-01-27 08:49:40 -0700324int dev_read_stringlist_search(const struct udevice *dev, const char *property,
325 const char *string);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600326
327/**
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200328 * dev_read_string_index() - obtain an indexed string from a string list
329 *
330 * @dev: device to examine
331 * @propname: name of the property containing the string list
332 * @index: index of the string to return
333 * @out: return location for the string
334 *
335 * @return:
336 * length of string, if found or -ve error value if not found
337 */
Simon Glass88b3a372020-01-27 08:49:40 -0700338int dev_read_string_index(const struct udevice *dev, const char *propname,
339 int index, const char **outp);
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200340
341/**
342 * dev_read_string_count() - find the number of strings in a string list
343 *
344 * @dev: device to examine
345 * @propname: name of the property containing the string list
346 * @return:
347 * number of strings in the list, or -ve error value if not found
348 */
Simon Glass88b3a372020-01-27 08:49:40 -0700349int dev_read_string_count(const struct udevice *dev, const char *propname);
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200350/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600351 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
352 *
353 * This function is useful to parse lists of phandles and their arguments.
354 * Returns 0 on success and fills out_args, on error returns appropriate
355 * errno value.
356 *
357 * Caller is responsible to call of_node_put() on the returned out_args->np
358 * pointer.
359 *
360 * Example:
361 *
362 * phandle1: node1 {
363 * #list-cells = <2>;
364 * }
365 *
366 * phandle2: node2 {
367 * #list-cells = <1>;
368 * }
369 *
370 * node3 {
371 * list = <&phandle1 1 2 &phandle2 3>;
372 * }
373 *
374 * To get a device_node of the `node2' node you may call this:
375 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
376 *
377 * @dev: device whose node containing a list
378 * @list_name: property name that contains a list
379 * @cells_name: property name that specifies phandles' arguments count
380 * @cells_count: Cell count to use if @cells_name is NULL
381 * @index: index of a phandle to parse out
382 * @out_args: optional pointer to output arguments structure (will be filled)
383 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
384 * @list_name does not exist, -EINVAL if a phandle was not found,
385 * @cells_name could not be found, the arguments were truncated or there
386 * were too many arguments.
387 */
Simon Glass88b3a372020-01-27 08:49:40 -0700388int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
389 const char *cells_name, int cell_count,
390 int index, struct ofnode_phandle_args *out_args);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600391
392/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200393 * dev_count_phandle_with_args() - Return phandle number in a list
394 *
395 * This function is usefull to get phandle number contained in a property list.
396 * For example, this allows to allocate the right amount of memory to keep
397 * clock's reference contained into the "clocks" property.
398 *
399 *
400 * @dev: device whose node containing a list
401 * @list_name: property name that contains a list
402 * @cells_name: property name that specifies phandles' arguments count
403 * @Returns number of phandle found on success, on error returns appropriate
404 * errno value.
405 */
406
Simon Glass88b3a372020-01-27 08:49:40 -0700407int dev_count_phandle_with_args(const struct udevice *dev,
408 const char *list_name, const char *cells_name);
Patrice Chotard642346a2017-07-18 11:57:08 +0200409
410/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600411 * dev_read_addr_cells() - Get the number of address cells for a device's node
412 *
413 * This walks back up the tree to find the closest #address-cells property
414 * which controls the given node.
415 *
Mario Six7ba50412018-01-15 11:09:36 +0100416 * @dev: device to check
Simon Glassf11c7ab2017-05-18 20:09:03 -0600417 * @return number of address cells this node uses
418 */
Simon Glass88b3a372020-01-27 08:49:40 -0700419int dev_read_addr_cells(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600420
421/**
422 * dev_read_size_cells() - Get the number of size cells for a device's node
423 *
424 * This walks back up the tree to find the closest #size-cells property
425 * which controls the given node.
426 *
Mario Six7ba50412018-01-15 11:09:36 +0100427 * @dev: device to check
Simon Glassf11c7ab2017-05-18 20:09:03 -0600428 * @return number of size cells this node uses
429 */
Simon Glass88b3a372020-01-27 08:49:40 -0700430int dev_read_size_cells(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600431
432/**
Simon Glass878d68c2017-06-12 06:21:31 -0600433 * dev_read_addr_cells() - Get the address cells property in a node
434 *
435 * This function matches fdt_address_cells().
436 *
Mario Six7ba50412018-01-15 11:09:36 +0100437 * @dev: device to check
Simon Glass878d68c2017-06-12 06:21:31 -0600438 * @return number of address cells this node uses
439 */
Simon Glass88b3a372020-01-27 08:49:40 -0700440int dev_read_simple_addr_cells(const struct udevice *dev);
Simon Glass878d68c2017-06-12 06:21:31 -0600441
442/**
443 * dev_read_size_cells() - Get the size cells property in a node
444 *
445 * This function matches fdt_size_cells().
446 *
Mario Six7ba50412018-01-15 11:09:36 +0100447 * @dev: device to check
Simon Glass878d68c2017-06-12 06:21:31 -0600448 * @return number of size cells this node uses
449 */
Simon Glass88b3a372020-01-27 08:49:40 -0700450int dev_read_simple_size_cells(const struct udevice *dev);
Simon Glass878d68c2017-06-12 06:21:31 -0600451
452/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600453 * dev_read_phandle() - Get the phandle from a device
454 *
455 * @dev: device to check
456 * @return phandle (1 or greater), or 0 if no phandle or other error
457 */
Simon Glass88b3a372020-01-27 08:49:40 -0700458int dev_read_phandle(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600459
460/**
461 * dev_read_prop()- - read a property from a device's node
462 *
463 * @dev: device to check
464 * @propname: property to read
465 * @lenp: place to put length on success
466 * @return pointer to property, or NULL if not found
467 */
Simon Glass88b3a372020-01-27 08:49:40 -0700468const void *dev_read_prop(const struct udevice *dev, const char *propname,
469 int *lenp);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600470
471/**
472 * dev_read_alias_seq() - Get the alias sequence number of a node
473 *
474 * This works out whether a node is pointed to by an alias, and if so, the
475 * sequence number of that alias. Aliases are of the form <base><num> where
476 * <num> is the sequence number. For example spi2 would be sequence number 2.
477 *
478 * @dev: device to look up
479 * @devnump: set to the sequence number if one is found
480 * @return 0 if a sequence was found, -ve if not
481 */
Simon Glass88b3a372020-01-27 08:49:40 -0700482int dev_read_alias_seq(const struct udevice *dev, int *devnump);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600483
484/**
485 * dev_read_u32_array() - Find and read an array of 32 bit integers
486 *
487 * Search for a property in a device node and read 32-bit value(s) from
488 * it.
489 *
490 * The out_values is modified only if a valid u32 value can be decoded.
491 *
492 * @dev: device to look up
493 * @propname: name of the property to read
494 * @out_values: pointer to return value, modified only if return value is 0
495 * @sz: number of array elements to read
496 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
497 * property does not have a value, and -EOVERFLOW if the property data isn't
498 * large enough.
499 */
Simon Glass88b3a372020-01-27 08:49:40 -0700500int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glass47a0fd32017-05-18 20:09:04 -0600501 u32 *out_values, size_t sz);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600502
503/**
504 * dev_read_first_subnode() - find the first subnode of a device's node
505 *
506 * @dev: device to look up
507 * @return reference to the first subnode (which can be invalid if the device's
508 * node has no subnodes)
509 */
Simon Glass88b3a372020-01-27 08:49:40 -0700510ofnode dev_read_first_subnode(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600511
512/**
513 * ofnode_next_subnode() - find the next sibling of a subnode
514 *
515 * @node: valid reference to previous node (sibling)
516 * @return reference to the next subnode (which can be invalid if the node
517 * has no more siblings)
518 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600519ofnode dev_read_next_subnode(ofnode node);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600520
521/**
522 * dev_read_u8_array_ptr() - find an 8-bit array
523 *
524 * Look up a device's node property and return a pointer to its contents as a
525 * byte array of given length. The property must have at least enough data
526 * for the array (count bytes). It may have more, but this will be ignored.
527 * The data is not copied.
528 *
529 * @dev: device to look up
530 * @propname: name of property to find
531 * @sz: number of array elements
532 * @return pointer to byte array if found, or NULL if the property is not
533 * found or there is not enough data
534 */
Simon Glass88b3a372020-01-27 08:49:40 -0700535const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
536 const char *propname, size_t sz);
Simon Glass47a0fd32017-05-18 20:09:04 -0600537
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600538/**
539 * dev_read_enabled() - check whether a node is enabled
540 *
541 * This looks for a 'status' property. If this exists, then returns 1 if
542 * the status is 'ok' and 0 otherwise. If there is no status property,
543 * it returns 1 on the assumption that anything mentioned should be enabled
544 * by default.
545 *
546 * @dev: device to examine
547 * @return integer value 0 (not enabled) or 1 (enabled)
548 */
Simon Glass88b3a372020-01-27 08:49:40 -0700549int dev_read_enabled(const struct udevice *dev);
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600550
Simon Glassdcf98852017-07-25 08:29:55 -0600551/**
552 * dev_read_resource() - obtain an indexed resource from a device.
553 *
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900554 * @dev: device to examine
Simon Glassdcf98852017-07-25 08:29:55 -0600555 * @index index of the resource to retrieve (0 = first)
556 * @res returns the resource
557 * @return 0 if ok, negative on error
558 */
Simon Glass88b3a372020-01-27 08:49:40 -0700559int dev_read_resource(const struct udevice *dev, uint index,
560 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -0600561
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900562/**
563 * dev_read_resource_byname() - obtain a named resource from a device.
564 *
565 * @dev: device to examine
566 * @name: name of the resource to retrieve
567 * @res: returns the resource
568 * @return 0 if ok, negative on error
569 */
Simon Glass88b3a372020-01-27 08:49:40 -0700570int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900571 struct resource *res);
572
Mario Six147c6072018-01-15 11:07:19 +0100573/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200574 * dev_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +0100575 *
576 * Translate an address from the device-tree into a CPU physical address. This
577 * function walks up the tree and applies the various bus mappings along the
578 * way.
579 *
580 * @dev: device giving the context in which to translate the address
581 * @in_addr: pointer to the address to translate
582 * @return the translated address; OF_BAD_ADDR on error
583 */
Simon Glass88b3a372020-01-27 08:49:40 -0700584u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
Michal Simek83e4c7e2019-01-31 16:30:59 +0100585
586/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200587 * dev_translate_dma_address() - Translate a device-tree DMA address
588 *
589 * Translate a DMA address from the device-tree into a CPU physical address.
590 * This function walks up the tree and applies the various bus mappings along
591 * the way.
592 *
593 * @dev: device giving the context in which to translate the DMA address
594 * @in_addr: pointer to the DMA address to translate
595 * @return the translated DMA address; OF_BAD_ADDR on error
596 */
Simon Glass88b3a372020-01-27 08:49:40 -0700597u64 dev_translate_dma_address(const struct udevice *dev,
598 const fdt32_t *in_addr);
Fabien Dessenne641067f2019-05-31 15:11:30 +0200599
600/**
Michal Simek83e4c7e2019-01-31 16:30:59 +0100601 * dev_read_alias_highest_id - Get highest alias id for the given stem
602 * @stem: Alias stem to be examined
603 *
604 * The function travels the lookup table to get the highest alias id for the
605 * given alias stem.
606 * @return alias ID, if found, else -1
607 */
608int dev_read_alias_highest_id(const char *stem);
609
Simon Glass47a0fd32017-05-18 20:09:04 -0600610#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
611
Simon Glass88b3a372020-01-27 08:49:40 -0700612static inline int dev_read_u32(const struct udevice *dev,
Masahiro Yamada3ab48f62017-12-30 02:00:05 +0900613 const char *propname, u32 *outp)
614{
615 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
616}
617
Simon Glass88b3a372020-01-27 08:49:40 -0700618static inline int dev_read_u32_default(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600619 const char *propname, int def)
620{
621 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
622}
623
Simon Glass88b3a372020-01-27 08:49:40 -0700624static inline int dev_read_s32(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700625 const char *propname, s32 *outp)
626{
627 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
628}
629
Simon Glass88b3a372020-01-27 08:49:40 -0700630static inline int dev_read_s32_default(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700631 const char *propname, int def)
632{
633 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
634}
635
Simon Glass88b3a372020-01-27 08:49:40 -0700636static inline int dev_read_u32u(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700637 const char *propname, uint *outp)
638{
639 u32 val;
640 int ret;
641
642 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
643 if (ret)
644 return ret;
645 *outp = val;
646
647 return 0;
648}
649
Simon Glass88b3a372020-01-27 08:49:40 -0700650static inline int dev_read_u64(const struct udevice *dev,
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200651 const char *propname, u64 *outp)
652{
653 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
654}
655
Simon Glass88b3a372020-01-27 08:49:40 -0700656static inline u64 dev_read_u64_default(const struct udevice *dev,
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200657 const char *propname, u64 def)
658{
659 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
660}
661
Simon Glass88b3a372020-01-27 08:49:40 -0700662static inline const char *dev_read_string(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600663 const char *propname)
664{
665 return ofnode_read_string(dev_ofnode(dev), propname);
666}
667
Simon Glass88b3a372020-01-27 08:49:40 -0700668static inline bool dev_read_bool(const struct udevice *dev,
669 const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -0600670{
671 return ofnode_read_bool(dev_ofnode(dev), propname);
672}
673
Simon Glass88b3a372020-01-27 08:49:40 -0700674static inline ofnode dev_read_subnode(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600675 const char *subbnode_name)
676{
677 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
678}
679
Simon Glass88b3a372020-01-27 08:49:40 -0700680static inline int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -0600681{
682 return ofnode_read_size(dev_ofnode(dev), propname);
683}
684
Simon Glass88b3a372020-01-27 08:49:40 -0700685static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
686 int index)
Simon Glass47a0fd32017-05-18 20:09:04 -0600687{
688 return devfdt_get_addr_index(dev, index);
689}
690
Simon Glass88b3a372020-01-27 08:49:40 -0700691static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
Sekhar Norif5b90472019-08-01 19:12:56 +0530692 int index,
693 fdt_size_t *size)
694{
695 return devfdt_get_addr_size_index(dev, index, size);
696}
697
Simon Glass88b3a372020-01-27 08:49:40 -0700698static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100699 const char *name)
700{
701 return devfdt_get_addr_name(dev, name);
702}
703
Simon Glass88b3a372020-01-27 08:49:40 -0700704static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
Sekhar Norif5b90472019-08-01 19:12:56 +0530705 const char *name,
706 fdt_size_t *size)
707{
708 return devfdt_get_addr_size_name(dev, name, size);
709}
710
Simon Glass88b3a372020-01-27 08:49:40 -0700711static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600712{
713 return devfdt_get_addr(dev);
714}
715
Simon Glass88b3a372020-01-27 08:49:40 -0700716static inline void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200717{
718 return devfdt_get_addr_ptr(dev);
719}
720
Simon Glass88b3a372020-01-27 08:49:40 -0700721static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass33c215a2019-09-15 12:08:58 -0600722{
723 return devfdt_get_addr_pci(dev);
724}
725
Simon Glass88b3a372020-01-27 08:49:40 -0700726static inline void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200727{
728 return devfdt_remap_addr(dev);
729}
730
Simon Glass88b3a372020-01-27 08:49:40 -0700731static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200732{
733 return devfdt_remap_addr_index(dev, index);
734}
735
Simon Glass88b3a372020-01-27 08:49:40 -0700736static inline void *dev_remap_addr_name(const struct udevice *dev,
737 const char *name)
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100738{
739 return devfdt_remap_addr_name(dev, name);
740}
741
Simon Glass88b3a372020-01-27 08:49:40 -0700742static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600743 const char *propname,
744 fdt_size_t *sizep)
745{
746 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
747}
748
Simon Glass88b3a372020-01-27 08:49:40 -0700749static inline const char *dev_read_name(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600750{
751 return ofnode_get_name(dev_ofnode(dev));
752}
753
Simon Glass88b3a372020-01-27 08:49:40 -0700754static inline int dev_read_stringlist_search(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600755 const char *propname,
756 const char *string)
757{
758 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
759}
760
Simon Glass88b3a372020-01-27 08:49:40 -0700761static inline int dev_read_string_index(const struct udevice *dev,
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200762 const char *propname, int index,
763 const char **outp)
764{
765 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
766}
767
Simon Glass88b3a372020-01-27 08:49:40 -0700768static inline int dev_read_string_count(const struct udevice *dev,
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200769 const char *propname)
770{
771 return ofnode_read_string_count(dev_ofnode(dev), propname);
772}
773
Simon Glass88b3a372020-01-27 08:49:40 -0700774static inline int dev_read_phandle_with_args(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600775 const char *list_name, const char *cells_name, int cell_count,
776 int index, struct ofnode_phandle_args *out_args)
777{
778 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
779 cells_name, cell_count, index,
780 out_args);
781}
782
Simon Glass88b3a372020-01-27 08:49:40 -0700783static inline int dev_count_phandle_with_args(const struct udevice *dev,
Patrice Chotard642346a2017-07-18 11:57:08 +0200784 const char *list_name, const char *cells_name)
785{
786 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
787 cells_name);
788}
789
Simon Glass88b3a372020-01-27 08:49:40 -0700790static inline int dev_read_addr_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600791{
Simon Glass878d68c2017-06-12 06:21:31 -0600792 /* NOTE: this call should walk up the parent stack */
Simon Glass47a0fd32017-05-18 20:09:04 -0600793 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
794}
795
Simon Glass88b3a372020-01-27 08:49:40 -0700796static inline int dev_read_size_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600797{
Simon Glass878d68c2017-06-12 06:21:31 -0600798 /* NOTE: this call should walk up the parent stack */
799 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
800}
801
Simon Glass88b3a372020-01-27 08:49:40 -0700802static inline int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -0600803{
804 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
805}
806
Simon Glass88b3a372020-01-27 08:49:40 -0700807static inline int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -0600808{
Simon Glass47a0fd32017-05-18 20:09:04 -0600809 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
810}
811
Simon Glass88b3a372020-01-27 08:49:40 -0700812static inline int dev_read_phandle(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600813{
814 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
815}
816
Simon Glass88b3a372020-01-27 08:49:40 -0700817static inline const void *dev_read_prop(const struct udevice *dev,
Masahiro Yamadafd736212017-07-17 12:18:39 +0900818 const char *propname, int *lenp)
Simon Glass47a0fd32017-05-18 20:09:04 -0600819{
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900820 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glass47a0fd32017-05-18 20:09:04 -0600821}
822
Simon Glass88b3a372020-01-27 08:49:40 -0700823static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glass47a0fd32017-05-18 20:09:04 -0600824{
825 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
826 dev_of_offset(dev), devnump);
827}
828
Simon Glass88b3a372020-01-27 08:49:40 -0700829static inline int dev_read_u32_array(const struct udevice *dev,
830 const char *propname, u32 *out_values,
831 size_t sz)
Simon Glass47a0fd32017-05-18 20:09:04 -0600832{
833 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
834}
835
Simon Glass88b3a372020-01-27 08:49:40 -0700836static inline ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600837{
838 return ofnode_first_subnode(dev_ofnode(dev));
839}
840
841static inline ofnode dev_read_next_subnode(ofnode node)
842{
843 return ofnode_next_subnode(node);
844}
845
Simon Glass88b3a372020-01-27 08:49:40 -0700846static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
Simon Glassf262d4c2020-01-27 08:49:47 -0700847 const char *propname,
848 size_t sz)
Simon Glassf11c7ab2017-05-18 20:09:03 -0600849{
850 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
851}
852
Simon Glass88b3a372020-01-27 08:49:40 -0700853static inline int dev_read_enabled(const struct udevice *dev)
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600854{
855 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
856}
857
Simon Glass88b3a372020-01-27 08:49:40 -0700858static inline int dev_read_resource(const struct udevice *dev, uint index,
Simon Glassdcf98852017-07-25 08:29:55 -0600859 struct resource *res)
860{
861 return ofnode_read_resource(dev_ofnode(dev), index, res);
862}
863
Simon Glass88b3a372020-01-27 08:49:40 -0700864static inline int dev_read_resource_byname(const struct udevice *dev,
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900865 const char *name,
866 struct resource *res)
867{
868 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
869}
870
Simon Glass88b3a372020-01-27 08:49:40 -0700871static inline u64 dev_translate_address(const struct udevice *dev,
872 const fdt32_t *in_addr)
Mario Six147c6072018-01-15 11:07:19 +0100873{
874 return ofnode_translate_address(dev_ofnode(dev), in_addr);
875}
876
Simon Glass88b3a372020-01-27 08:49:40 -0700877static inline u64 dev_translate_dma_address(const struct udevice *dev,
878 const fdt32_t *in_addr)
Fabien Dessenne641067f2019-05-31 15:11:30 +0200879{
880 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
881}
882
Michal Simek83e4c7e2019-01-31 16:30:59 +0100883static inline int dev_read_alias_highest_id(const char *stem)
884{
885 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
886}
887
Simon Glassf11c7ab2017-05-18 20:09:03 -0600888#endif /* CONFIG_DM_DEV_READ_INLINE */
889
890/**
891 * dev_for_each_subnode() - Helper function to iterate through subnodes
892 *
893 * This creates a for() loop which works through the subnodes in a device's
894 * device-tree node.
895 *
896 * @subnode: ofnode holding the current subnode
897 * @dev: device to use for interation (struct udevice *)
898 */
899#define dev_for_each_subnode(subnode, dev) \
900 for (subnode = dev_read_first_subnode(dev); \
901 ofnode_valid(subnode); \
902 subnode = ofnode_next_subnode(subnode))
903
904#endif