blob: 77d3bc8db5beeee637b4dbac574cf3d647c83699 [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)
Simon Glass2f9a6122020-01-27 08:49:40 -070019static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glass34ef0c22017-05-18 20:09:03 -060020{
21 return ofnode_to_np(dev->node);
22}
23#else
Simon Glass2f9a6122020-01-27 08:49:40 -070024static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glass34ef0c22017-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 Glass2a580882020-01-27 08:49:36 -070036static inline ofnode dev_ofnode(const struct udevice *dev)
Simon Glass34ef0c22017-05-18 20:09:03 -060037{
38 return dev->node;
39}
40
Simon Glass2a580882020-01-27 08:49:36 -070041static inline bool dev_of_valid(const struct udevice *dev)
Simon Glass34ef0c22017-05-18 20:09:03 -060042{
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 */
Simon Glass2f9a6122020-01-27 08:49:40 -070056int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
Masahiro Yamada71d115f2017-12-30 02:00:05 +090057
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 */
Simon Glass2f9a6122020-01-27 08:49:40 -070066int dev_read_u32_default(const struct udevice *dev, const char *propname,
67 int def);
Simon Glass34ef0c22017-05-18 20:09:03 -060068
69/**
Dario Binacchi81d80b52020-03-29 18:04:41 +020070 * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
71 * property
72 *
73 * @dev: device to read DT property from
74 * @propname: name of the property to read from
75 * @index: index of the integer to return
76 * @outp: place to put value (if found)
77 * @return 0 if OK, -ve on error
78 */
79int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
80 u32 *outp);
81
82/**
83 * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
84 * DT property
85 *
86 * @dev: device to read DT property from
87 * @propname: name of the property to read from
88 * @index: index of the integer to return
89 * @def: default value to return if the property has no value
90 * @return property value, or @def if not found
91 */
92u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
93 int index, u32 def);
94
95/**
Simon Glass6df01f92018-12-10 10:37:37 -070096 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
97 *
98 * @dev: device to read DT property from
99 * @propname: name of the property to read from
100 * @outp: place to put value (if found)
101 * @return 0 if OK, -ve on error
102 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700103int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
Simon Glass6df01f92018-12-10 10:37:37 -0700104
105/**
106 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
107 *
108 * @dev: device to read DT property from
109 * @propname: name of the property to read from
110 * @def: default value to return if the property has no value
111 * @return property value, or @def if not found
112 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700113int dev_read_s32_default(const struct udevice *dev, const char *propname,
114 int def);
Simon Glass6df01f92018-12-10 10:37:37 -0700115
116/**
117 * dev_read_u32u() - read a 32-bit integer from a device's DT property
118 *
119 * This version uses a standard uint type.
120 *
121 * @dev: device to read DT property from
122 * @propname: name of the property to read from
123 * @outp: place to put value (if found)
124 * @return 0 if OK, -ve on error
125 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700126int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
Simon Glass6df01f92018-12-10 10:37:37 -0700127
128/**
T Karthik Reddy478860d2019-09-02 16:34:30 +0200129 * dev_read_u64() - read a 64-bit integer from a device's DT property
130 *
131 * @dev: device to read DT property from
132 * @propname: name of the property to read from
133 * @outp: place to put value (if found)
134 * @return 0 if OK, -ve on error
135 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700136int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
T Karthik Reddy478860d2019-09-02 16:34:30 +0200137
138/**
139 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
140 *
141 * @dev: device to read DT property from
142 * @propname: name of the property to read from
143 * @def: default value to return if the property has no value
144 * @return property value, or @def if not found
145 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700146u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
147 u64 def);
T Karthik Reddy478860d2019-09-02 16:34:30 +0200148
149/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600150 * dev_read_string() - Read a string from a device's DT property
151 *
152 * @dev: device to read DT property from
153 * @propname: name of the property to read
154 * @return string from property value, or NULL if there is no such property
155 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700156const char *dev_read_string(const struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -0600157
158/**
159 * dev_read_bool() - read a boolean value from a device's DT property
160 *
161 * @dev: device to read DT property from
162 * @propname: name of property to read
163 * @return true if property is present (meaning true), false if not present
164 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700165bool dev_read_bool(const struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -0600166
167/**
168 * dev_read_subnode() - find a named subnode of a device
169 *
170 * @dev: device whose DT node contains the subnode
171 * @subnode_name: name of subnode to find
172 * @return reference to subnode (which can be invalid if there is no such
173 * subnode)
174 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700175ofnode dev_read_subnode(const struct udevice *dev, const char *subbnode_name);
Simon Glass34ef0c22017-05-18 20:09:03 -0600176
177/**
178 * dev_read_size() - read the size of a property
179 *
180 * @dev: device to check
181 * @propname: property to check
182 * @return size of property if present, or -EINVAL if not
183 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700184int dev_read_size(const struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -0600185
186/**
187 * dev_read_addr_index() - Get the indexed reg property of a device
188 *
189 * @dev: Device to read from
190 * @index: the 'reg' property can hold a list of <addr, size> pairs
191 * and @index is used to select which one is required
192 *
193 * @return address or FDT_ADDR_T_NONE if not found
194 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700195fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
Simon Glass34ef0c22017-05-18 20:09:03 -0600196
197/**
Sekhar Norif677c6f2019-08-01 19:12:56 +0530198 * dev_read_addr_size_index() - Get the indexed reg property of a device
199 *
200 * @dev: Device to read from
201 * @index: the 'reg' property can hold a list of <addr, size> pairs
202 * and @index is used to select which one is required
203 * @size: place to put size value (on success)
204 *
205 * @return address or FDT_ADDR_T_NONE if not found
206 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700207fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530208 fdt_size_t *size);
209
210/**
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200211 * dev_remap_addr_index() - Get the indexed reg property of a device
212 * as a memory-mapped I/O pointer
213 *
214 * @dev: Device to read from
215 * @index: the 'reg' property can hold a list of <addr, size> pairs
216 * and @index is used to select which one is required
217 *
218 * @return pointer or NULL if not found
219 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700220void *dev_remap_addr_index(const struct udevice *dev, int index);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200221
222/**
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100223 * dev_read_addr_name() - Get the reg property of a device, indexed by name
224 *
225 * @dev: Device to read from
226 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
227 * 'reg-names' property providing named-based identification. @index
228 * indicates the value to search for in 'reg-names'.
229 *
230 * @return address or FDT_ADDR_T_NONE if not found
231 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700232fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100233
234/**
Sekhar Norif677c6f2019-08-01 19:12:56 +0530235 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
236 *
237 * @dev: Device to read from
238 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
239 * 'reg-names' property providing named-based identification. @index
240 * indicates the value to search for in 'reg-names'.
241 * @size: place to put size value (on success)
242 *
243 * @return address or FDT_ADDR_T_NONE if not found
244 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700245fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530246 fdt_size_t *size);
247
248/**
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100249 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
250 * as a memory-mapped I/O pointer
251 *
252 * @dev: Device to read from
253 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
254 * 'reg-names' property providing named-based identification. @index
255 * indicates the value to search for in 'reg-names'.
256 *
257 * @return pointer or NULL if not found
258 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700259void *dev_remap_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100260
261/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600262 * dev_read_addr() - Get the reg property of a device
263 *
264 * @dev: Device to read from
265 *
266 * @return address or FDT_ADDR_T_NONE if not found
267 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700268fdt_addr_t dev_read_addr(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600269
270/**
Philipp Tomsich7719b392017-09-11 22:04:12 +0200271 * dev_read_addr_ptr() - Get the reg property of a device
272 * as a pointer
273 *
274 * @dev: Device to read from
275 *
276 * @return pointer or NULL if not found
277 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700278void *dev_read_addr_ptr(const struct udevice *dev);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200279
280/**
Simon Glass23b27592019-09-15 12:08:58 -0600281 * dev_read_addr_pci() - Read an address and handle PCI address translation
282 *
283 * At present U-Boot does not have address translation logic for PCI in the
284 * livetree implementation (of_addr.c). This special function supports this for
285 * the flat tree implementation.
286 *
287 * This function should be removed (and code should use dev_read() instead)
288 * once:
289 *
290 * 1. PCI address translation is added; and either
291 * 2. everything uses livetree where PCI translation is used (which is feasible
292 * in SPL and U-Boot proper) or PCI address translation is added to
293 * fdtdec_get_addr() and friends.
294 *
295 * @dev: Device to read from
296 * @return address or FDT_ADDR_T_NONE if not found
297 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700298fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
Simon Glass23b27592019-09-15 12:08:58 -0600299
300/**
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200301 * dev_remap_addr() - Get the reg property of a device as a
302 * memory-mapped I/O pointer
303 *
304 * @dev: Device to read from
305 *
306 * @return pointer or NULL if not found
307 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700308void *dev_remap_addr(const struct udevice *dev);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200309
310/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600311 * dev_read_addr_size() - get address and size from a device property
312 *
313 * This does no address translation. It simply reads an property that contains
314 * an address and a size value, one after the other.
315 *
316 * @dev: Device to read from
317 * @propname: property to read
318 * @sizep: place to put size value (on success)
319 * @return address value, or FDT_ADDR_T_NONE on error
320 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700321fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname,
322 fdt_size_t *sizep);
Simon Glass34ef0c22017-05-18 20:09:03 -0600323
324/**
325 * dev_read_name() - get the name of a device's node
326 *
Bin Meng4cf914d2019-07-05 09:23:18 -0700327 * @dev: Device to read from
Simon Glass34ef0c22017-05-18 20:09:03 -0600328 * @return name of node
329 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700330const char *dev_read_name(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600331
332/**
333 * dev_read_stringlist_search() - find string in a string list and return index
334 *
335 * Note that it is possible for this function to succeed on property values
336 * that are not NUL-terminated. That's because the function will stop after
337 * finding the first occurrence of @string. This can for example happen with
338 * small-valued cell properties, such as #address-cells, when searching for
339 * the empty string.
340 *
341 * @dev: device to check
342 * @propname: name of the property containing the string list
343 * @string: string to look up in the string list
344 *
345 * @return:
346 * the index of the string in the list of strings
347 * -ENODATA if the property is not found
348 * -EINVAL on some other error
349 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700350int dev_read_stringlist_search(const struct udevice *dev, const char *property,
351 const char *string);
Simon Glass34ef0c22017-05-18 20:09:03 -0600352
353/**
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200354 * dev_read_string_index() - obtain an indexed string from a string list
355 *
356 * @dev: device to examine
357 * @propname: name of the property containing the string list
358 * @index: index of the string to return
359 * @out: return location for the string
360 *
361 * @return:
362 * length of string, if found or -ve error value if not found
363 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700364int dev_read_string_index(const struct udevice *dev, const char *propname,
365 int index, const char **outp);
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200366
367/**
368 * dev_read_string_count() - find the number of strings in a string list
369 *
370 * @dev: device to examine
371 * @propname: name of the property containing the string list
372 * @return:
373 * number of strings in the list, or -ve error value if not found
374 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700375int dev_read_string_count(const struct udevice *dev, const char *propname);
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200376/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600377 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
378 *
379 * This function is useful to parse lists of phandles and their arguments.
380 * Returns 0 on success and fills out_args, on error returns appropriate
381 * errno value.
382 *
383 * Caller is responsible to call of_node_put() on the returned out_args->np
384 * pointer.
385 *
386 * Example:
387 *
388 * phandle1: node1 {
389 * #list-cells = <2>;
390 * }
391 *
392 * phandle2: node2 {
393 * #list-cells = <1>;
394 * }
395 *
396 * node3 {
397 * list = <&phandle1 1 2 &phandle2 3>;
398 * }
399 *
400 * To get a device_node of the `node2' node you may call this:
401 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
402 *
403 * @dev: device whose node containing a list
404 * @list_name: property name that contains a list
405 * @cells_name: property name that specifies phandles' arguments count
406 * @cells_count: Cell count to use if @cells_name is NULL
407 * @index: index of a phandle to parse out
408 * @out_args: optional pointer to output arguments structure (will be filled)
409 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
410 * @list_name does not exist, -EINVAL if a phandle was not found,
411 * @cells_name could not be found, the arguments were truncated or there
412 * were too many arguments.
413 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700414int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
415 const char *cells_name, int cell_count,
416 int index, struct ofnode_phandle_args *out_args);
Simon Glass34ef0c22017-05-18 20:09:03 -0600417
418/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200419 * dev_count_phandle_with_args() - Return phandle number in a list
420 *
421 * This function is usefull to get phandle number contained in a property list.
422 * For example, this allows to allocate the right amount of memory to keep
423 * clock's reference contained into the "clocks" property.
424 *
425 *
426 * @dev: device whose node containing a list
427 * @list_name: property name that contains a list
428 * @cells_name: property name that specifies phandles' arguments count
429 * @Returns number of phandle found on success, on error returns appropriate
430 * errno value.
431 */
432
Simon Glass2f9a6122020-01-27 08:49:40 -0700433int dev_count_phandle_with_args(const struct udevice *dev,
434 const char *list_name, const char *cells_name);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200435
436/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600437 * dev_read_addr_cells() - Get the number of address cells for a device's node
438 *
439 * This walks back up the tree to find the closest #address-cells property
440 * which controls the given node.
441 *
Mario Six16408ee2018-01-15 11:09:36 +0100442 * @dev: device to check
Simon Glass34ef0c22017-05-18 20:09:03 -0600443 * @return number of address cells this node uses
444 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700445int dev_read_addr_cells(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600446
447/**
448 * dev_read_size_cells() - Get the number of size cells for a device's node
449 *
450 * This walks back up the tree to find the closest #size-cells property
451 * which controls the given node.
452 *
Mario Six16408ee2018-01-15 11:09:36 +0100453 * @dev: device to check
Simon Glass34ef0c22017-05-18 20:09:03 -0600454 * @return number of size cells this node uses
455 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700456int dev_read_size_cells(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600457
458/**
Simon Glass4191dc12017-06-12 06:21:31 -0600459 * dev_read_addr_cells() - Get the address cells property in a node
460 *
461 * This function matches fdt_address_cells().
462 *
Mario Six16408ee2018-01-15 11:09:36 +0100463 * @dev: device to check
Simon Glass4191dc12017-06-12 06:21:31 -0600464 * @return number of address cells this node uses
465 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700466int dev_read_simple_addr_cells(const struct udevice *dev);
Simon Glass4191dc12017-06-12 06:21:31 -0600467
468/**
469 * dev_read_size_cells() - Get the size cells property in a node
470 *
471 * This function matches fdt_size_cells().
472 *
Mario Six16408ee2018-01-15 11:09:36 +0100473 * @dev: device to check
Simon Glass4191dc12017-06-12 06:21:31 -0600474 * @return number of size cells this node uses
475 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700476int dev_read_simple_size_cells(const struct udevice *dev);
Simon Glass4191dc12017-06-12 06:21:31 -0600477
478/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600479 * dev_read_phandle() - Get the phandle from a device
480 *
481 * @dev: device to check
482 * @return phandle (1 or greater), or 0 if no phandle or other error
483 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700484int dev_read_phandle(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600485
486/**
487 * dev_read_prop()- - read a property from a device's node
488 *
489 * @dev: device to check
490 * @propname: property to read
491 * @lenp: place to put length on success
492 * @return pointer to property, or NULL if not found
493 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700494const void *dev_read_prop(const struct udevice *dev, const char *propname,
495 int *lenp);
Simon Glass34ef0c22017-05-18 20:09:03 -0600496
497/**
498 * dev_read_alias_seq() - Get the alias sequence number of a node
499 *
500 * This works out whether a node is pointed to by an alias, and if so, the
501 * sequence number of that alias. Aliases are of the form <base><num> where
502 * <num> is the sequence number. For example spi2 would be sequence number 2.
503 *
504 * @dev: device to look up
505 * @devnump: set to the sequence number if one is found
506 * @return 0 if a sequence was found, -ve if not
507 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700508int dev_read_alias_seq(const struct udevice *dev, int *devnump);
Simon Glass34ef0c22017-05-18 20:09:03 -0600509
510/**
511 * dev_read_u32_array() - Find and read an array of 32 bit integers
512 *
513 * Search for a property in a device node and read 32-bit value(s) from
514 * it.
515 *
516 * The out_values is modified only if a valid u32 value can be decoded.
517 *
518 * @dev: device to look up
519 * @propname: name of the property to read
520 * @out_values: pointer to return value, modified only if return value is 0
521 * @sz: number of array elements to read
522 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
523 * property does not have a value, and -EOVERFLOW if the property data isn't
524 * large enough.
525 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700526int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600527 u32 *out_values, size_t sz);
Simon Glass34ef0c22017-05-18 20:09:03 -0600528
529/**
530 * dev_read_first_subnode() - find the first subnode of a device's node
531 *
532 * @dev: device to look up
533 * @return reference to the first subnode (which can be invalid if the device's
534 * node has no subnodes)
535 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700536ofnode dev_read_first_subnode(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600537
538/**
539 * ofnode_next_subnode() - find the next sibling of a subnode
540 *
541 * @node: valid reference to previous node (sibling)
542 * @return reference to the next subnode (which can be invalid if the node
543 * has no more siblings)
544 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600545ofnode dev_read_next_subnode(ofnode node);
Simon Glass34ef0c22017-05-18 20:09:03 -0600546
547/**
548 * dev_read_u8_array_ptr() - find an 8-bit array
549 *
550 * Look up a device's node property and return a pointer to its contents as a
551 * byte array of given length. The property must have at least enough data
552 * for the array (count bytes). It may have more, but this will be ignored.
553 * The data is not copied.
554 *
555 * @dev: device to look up
556 * @propname: name of property to find
557 * @sz: number of array elements
558 * @return pointer to byte array if found, or NULL if the property is not
559 * found or there is not enough data
560 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700561const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
562 const char *propname, size_t sz);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600563
Simon Glassfa031f82017-06-12 06:21:30 -0600564/**
565 * dev_read_enabled() - check whether a node is enabled
566 *
567 * This looks for a 'status' property. If this exists, then returns 1 if
568 * the status is 'ok' and 0 otherwise. If there is no status property,
569 * it returns 1 on the assumption that anything mentioned should be enabled
570 * by default.
571 *
572 * @dev: device to examine
573 * @return integer value 0 (not enabled) or 1 (enabled)
574 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700575int dev_read_enabled(const struct udevice *dev);
Simon Glassfa031f82017-06-12 06:21:30 -0600576
Simon Glassf7bfcc42017-07-25 08:29:55 -0600577/**
578 * dev_read_resource() - obtain an indexed resource from a device.
579 *
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900580 * @dev: device to examine
Simon Glassf7bfcc42017-07-25 08:29:55 -0600581 * @index index of the resource to retrieve (0 = first)
582 * @res returns the resource
583 * @return 0 if ok, negative on error
584 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700585int dev_read_resource(const struct udevice *dev, uint index,
586 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -0600587
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900588/**
589 * dev_read_resource_byname() - obtain a named resource from a device.
590 *
591 * @dev: device to examine
592 * @name: name of the resource to retrieve
593 * @res: returns the resource
594 * @return 0 if ok, negative on error
595 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700596int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900597 struct resource *res);
598
Mario Sixaefac062018-01-15 11:07:19 +0100599/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200600 * dev_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +0100601 *
602 * Translate an address from the device-tree into a CPU physical address. This
603 * function walks up the tree and applies the various bus mappings along the
604 * way.
605 *
606 * @dev: device giving the context in which to translate the address
607 * @in_addr: pointer to the address to translate
608 * @return the translated address; OF_BAD_ADDR on error
609 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700610u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
Michal Simekd0e30a02019-01-31 16:30:59 +0100611
612/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200613 * dev_translate_dma_address() - Translate a device-tree DMA address
614 *
615 * Translate a DMA address from the device-tree into a CPU physical address.
616 * This function walks up the tree and applies the various bus mappings along
617 * the way.
618 *
619 * @dev: device giving the context in which to translate the DMA address
620 * @in_addr: pointer to the DMA address to translate
621 * @return the translated DMA address; OF_BAD_ADDR on error
622 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700623u64 dev_translate_dma_address(const struct udevice *dev,
624 const fdt32_t *in_addr);
Fabien Dessenne22236e02019-05-31 15:11:30 +0200625
626/**
Michal Simekd0e30a02019-01-31 16:30:59 +0100627 * dev_read_alias_highest_id - Get highest alias id for the given stem
628 * @stem: Alias stem to be examined
629 *
630 * The function travels the lookup table to get the highest alias id for the
631 * given alias stem.
632 * @return alias ID, if found, else -1
633 */
634int dev_read_alias_highest_id(const char *stem);
635
Simon Glasscfdae5c2017-05-18 20:09:04 -0600636#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
637
Simon Glass2f9a6122020-01-27 08:49:40 -0700638static inline int dev_read_u32(const struct udevice *dev,
Masahiro Yamada71d115f2017-12-30 02:00:05 +0900639 const char *propname, u32 *outp)
640{
641 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
642}
643
Simon Glass2f9a6122020-01-27 08:49:40 -0700644static inline int dev_read_u32_default(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600645 const char *propname, int def)
646{
647 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
648}
649
Dario Binacchi81d80b52020-03-29 18:04:41 +0200650static inline int dev_read_u32_index(struct udevice *dev,
651 const char *propname, int index, u32 *outp)
652{
653 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
654}
655
656static inline u32 dev_read_u32_index_default(struct udevice *dev,
657 const char *propname, int index,
658 u32 def)
659{
660 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
661 def);
662}
663
Simon Glass2f9a6122020-01-27 08:49:40 -0700664static inline int dev_read_s32(const struct udevice *dev,
Simon Glass6df01f92018-12-10 10:37:37 -0700665 const char *propname, s32 *outp)
666{
667 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
668}
669
Simon Glass2f9a6122020-01-27 08:49:40 -0700670static inline int dev_read_s32_default(const struct udevice *dev,
Simon Glass6df01f92018-12-10 10:37:37 -0700671 const char *propname, int def)
672{
673 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
674}
675
Simon Glass2f9a6122020-01-27 08:49:40 -0700676static inline int dev_read_u32u(const struct udevice *dev,
Simon Glass6df01f92018-12-10 10:37:37 -0700677 const char *propname, uint *outp)
678{
679 u32 val;
680 int ret;
681
682 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
683 if (ret)
684 return ret;
685 *outp = val;
686
687 return 0;
688}
689
Simon Glass2f9a6122020-01-27 08:49:40 -0700690static inline int dev_read_u64(const struct udevice *dev,
T Karthik Reddy478860d2019-09-02 16:34:30 +0200691 const char *propname, u64 *outp)
692{
693 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
694}
695
Simon Glass2f9a6122020-01-27 08:49:40 -0700696static inline u64 dev_read_u64_default(const struct udevice *dev,
T Karthik Reddy478860d2019-09-02 16:34:30 +0200697 const char *propname, u64 def)
698{
699 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
700}
701
Simon Glass2f9a6122020-01-27 08:49:40 -0700702static inline const char *dev_read_string(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600703 const char *propname)
704{
705 return ofnode_read_string(dev_ofnode(dev), propname);
706}
707
Simon Glass2f9a6122020-01-27 08:49:40 -0700708static inline bool dev_read_bool(const struct udevice *dev,
709 const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600710{
711 return ofnode_read_bool(dev_ofnode(dev), propname);
712}
713
Simon Glass2f9a6122020-01-27 08:49:40 -0700714static inline ofnode dev_read_subnode(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600715 const char *subbnode_name)
716{
717 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
718}
719
Simon Glass2f9a6122020-01-27 08:49:40 -0700720static inline int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600721{
722 return ofnode_read_size(dev_ofnode(dev), propname);
723}
724
Simon Glass2f9a6122020-01-27 08:49:40 -0700725static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
726 int index)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600727{
728 return devfdt_get_addr_index(dev, index);
729}
730
Simon Glass2f9a6122020-01-27 08:49:40 -0700731static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530732 int index,
733 fdt_size_t *size)
734{
735 return devfdt_get_addr_size_index(dev, index, size);
736}
737
Simon Glass2f9a6122020-01-27 08:49:40 -0700738static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100739 const char *name)
740{
741 return devfdt_get_addr_name(dev, name);
742}
743
Simon Glass2f9a6122020-01-27 08:49:40 -0700744static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530745 const char *name,
746 fdt_size_t *size)
747{
748 return devfdt_get_addr_size_name(dev, name, size);
749}
750
Simon Glass2f9a6122020-01-27 08:49:40 -0700751static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600752{
753 return devfdt_get_addr(dev);
754}
755
Simon Glass2f9a6122020-01-27 08:49:40 -0700756static inline void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsich7719b392017-09-11 22:04:12 +0200757{
758 return devfdt_get_addr_ptr(dev);
759}
760
Simon Glass2f9a6122020-01-27 08:49:40 -0700761static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600762{
763 return devfdt_get_addr_pci(dev);
764}
765
Simon Glass2f9a6122020-01-27 08:49:40 -0700766static inline void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200767{
768 return devfdt_remap_addr(dev);
769}
770
Simon Glass2f9a6122020-01-27 08:49:40 -0700771static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200772{
773 return devfdt_remap_addr_index(dev, index);
774}
775
Simon Glass2f9a6122020-01-27 08:49:40 -0700776static inline void *dev_remap_addr_name(const struct udevice *dev,
777 const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100778{
779 return devfdt_remap_addr_name(dev, name);
780}
781
Simon Glass2f9a6122020-01-27 08:49:40 -0700782static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600783 const char *propname,
784 fdt_size_t *sizep)
785{
786 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
787}
788
Simon Glass2f9a6122020-01-27 08:49:40 -0700789static inline const char *dev_read_name(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600790{
791 return ofnode_get_name(dev_ofnode(dev));
792}
793
Simon Glass2f9a6122020-01-27 08:49:40 -0700794static inline int dev_read_stringlist_search(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600795 const char *propname,
796 const char *string)
797{
798 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
799}
800
Simon Glass2f9a6122020-01-27 08:49:40 -0700801static inline int dev_read_string_index(const struct udevice *dev,
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200802 const char *propname, int index,
803 const char **outp)
804{
805 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
806}
807
Simon Glass2f9a6122020-01-27 08:49:40 -0700808static inline int dev_read_string_count(const struct udevice *dev,
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200809 const char *propname)
810{
811 return ofnode_read_string_count(dev_ofnode(dev), propname);
812}
813
Simon Glass2f9a6122020-01-27 08:49:40 -0700814static inline int dev_read_phandle_with_args(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600815 const char *list_name, const char *cells_name, int cell_count,
816 int index, struct ofnode_phandle_args *out_args)
817{
818 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
819 cells_name, cell_count, index,
820 out_args);
821}
822
Simon Glass2f9a6122020-01-27 08:49:40 -0700823static inline int dev_count_phandle_with_args(const struct udevice *dev,
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200824 const char *list_name, const char *cells_name)
825{
826 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
827 cells_name);
828}
829
Simon Glass2f9a6122020-01-27 08:49:40 -0700830static inline int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600831{
Simon Glass4191dc12017-06-12 06:21:31 -0600832 /* NOTE: this call should walk up the parent stack */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600833 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
834}
835
Simon Glass2f9a6122020-01-27 08:49:40 -0700836static inline int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600837{
Simon Glass4191dc12017-06-12 06:21:31 -0600838 /* NOTE: this call should walk up the parent stack */
839 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
840}
841
Simon Glass2f9a6122020-01-27 08:49:40 -0700842static inline int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600843{
844 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
845}
846
Simon Glass2f9a6122020-01-27 08:49:40 -0700847static inline int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600848{
Simon Glasscfdae5c2017-05-18 20:09:04 -0600849 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
850}
851
Simon Glass2f9a6122020-01-27 08:49:40 -0700852static inline int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600853{
854 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
855}
856
Simon Glass2f9a6122020-01-27 08:49:40 -0700857static inline const void *dev_read_prop(const struct udevice *dev,
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900858 const char *propname, int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600859{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900860 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600861}
862
Simon Glass2f9a6122020-01-27 08:49:40 -0700863static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600864{
865 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
866 dev_of_offset(dev), devnump);
867}
868
Simon Glass2f9a6122020-01-27 08:49:40 -0700869static inline int dev_read_u32_array(const struct udevice *dev,
870 const char *propname, u32 *out_values,
871 size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600872{
873 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
874}
875
Simon Glass2f9a6122020-01-27 08:49:40 -0700876static inline ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600877{
878 return ofnode_first_subnode(dev_ofnode(dev));
879}
880
881static inline ofnode dev_read_next_subnode(ofnode node)
882{
883 return ofnode_next_subnode(node);
884}
885
Simon Glass2f9a6122020-01-27 08:49:40 -0700886static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
Simon Glass000676b2020-01-27 08:49:47 -0700887 const char *propname,
888 size_t sz)
Simon Glass34ef0c22017-05-18 20:09:03 -0600889{
890 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
891}
892
Simon Glass2f9a6122020-01-27 08:49:40 -0700893static inline int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -0600894{
895 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
896}
897
Simon Glass2f9a6122020-01-27 08:49:40 -0700898static inline int dev_read_resource(const struct udevice *dev, uint index,
Simon Glassf7bfcc42017-07-25 08:29:55 -0600899 struct resource *res)
900{
901 return ofnode_read_resource(dev_ofnode(dev), index, res);
902}
903
Simon Glass2f9a6122020-01-27 08:49:40 -0700904static inline int dev_read_resource_byname(const struct udevice *dev,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900905 const char *name,
906 struct resource *res)
907{
908 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
909}
910
Simon Glass2f9a6122020-01-27 08:49:40 -0700911static inline u64 dev_translate_address(const struct udevice *dev,
912 const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +0100913{
914 return ofnode_translate_address(dev_ofnode(dev), in_addr);
915}
916
Simon Glass2f9a6122020-01-27 08:49:40 -0700917static inline u64 dev_translate_dma_address(const struct udevice *dev,
918 const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +0200919{
920 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
921}
922
Michal Simekd0e30a02019-01-31 16:30:59 +0100923static inline int dev_read_alias_highest_id(const char *stem)
924{
925 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
926}
927
Simon Glass34ef0c22017-05-18 20:09:03 -0600928#endif /* CONFIG_DM_DEV_READ_INLINE */
929
930/**
931 * dev_for_each_subnode() - Helper function to iterate through subnodes
932 *
933 * This creates a for() loop which works through the subnodes in a device's
934 * device-tree node.
935 *
936 * @subnode: ofnode holding the current subnode
937 * @dev: device to use for interation (struct udevice *)
938 */
939#define dev_for_each_subnode(subnode, dev) \
940 for (subnode = dev_read_first_subnode(dev); \
941 ofnode_valid(subnode); \
942 subnode = ofnode_next_subnode(subnode))
943
944#endif