blob: 75c6ad6ee49e452f0eed11f40be0ad79cedc70c6 [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
Dan Murphyd3aa90e2020-07-23 07:01:38 -050012#include <linux/errno.h>
13
Simon Glass992b6032020-07-19 10:15:39 -060014#include <dm/device.h>
Simon Glass34ef0c22017-05-18 20:09:03 -060015#include <dm/fdtaddr.h>
16#include <dm/ofnode.h>
17#include <dm/uclass.h>
18
Simon Glass170e17c2017-06-12 06:21:29 -060019struct resource;
20
Simon Glass34ef0c22017-05-18 20:09:03 -060021#if CONFIG_IS_ENABLED(OF_LIVE)
Simon Glass2f9a6122020-01-27 08:49:40 -070022static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glass34ef0c22017-05-18 20:09:03 -060023{
Simon Glassa7ece582020-12-19 10:40:14 -070024 return ofnode_to_np(dev_ofnode(dev));
Simon Glass34ef0c22017-05-18 20:09:03 -060025}
26#else
Simon Glass2f9a6122020-01-27 08:49:40 -070027static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glass34ef0c22017-05-18 20:09:03 -060028{
29 return NULL;
30}
31#endif
32
Simon Glass77f7c1e2021-01-21 13:57:10 -070033#if !defined(CONFIG_DM_DEV_READ_INLINE) || CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasscfdae5c2017-05-18 20:09:04 -060034/**
Masahiro Yamada71d115f2017-12-30 02:00:05 +090035 * dev_read_u32() - read a 32-bit integer from a device's DT property
36 *
37 * @dev: device to read DT property from
38 * @propname: name of the property to read from
39 * @outp: place to put value (if found)
40 * @return 0 if OK, -ve on error
41 */
Simon Glass2f9a6122020-01-27 08:49:40 -070042int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
Masahiro Yamada71d115f2017-12-30 02:00:05 +090043
44/**
Simon Glasscfdae5c2017-05-18 20:09:04 -060045 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
46 *
47 * @dev: device to read DT property from
48 * @propname: name of the property to read from
49 * @def: default value to return if the property has no value
50 * @return property value, or @def if not found
51 */
Simon Glass2f9a6122020-01-27 08:49:40 -070052int dev_read_u32_default(const struct udevice *dev, const char *propname,
53 int def);
Simon Glass34ef0c22017-05-18 20:09:03 -060054
55/**
Dario Binacchi81d80b52020-03-29 18:04:41 +020056 * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
57 * property
58 *
59 * @dev: device to read DT property from
60 * @propname: name of the property to read from
61 * @index: index of the integer to return
62 * @outp: place to put value (if found)
63 * @return 0 if OK, -ve on error
64 */
65int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
66 u32 *outp);
67
68/**
69 * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
70 * DT property
71 *
72 * @dev: device to read DT property from
73 * @propname: name of the property to read from
74 * @index: index of the integer to return
75 * @def: default value to return if the property has no value
76 * @return property value, or @def if not found
77 */
78u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
79 int index, u32 def);
80
81/**
Simon Glass6df01f92018-12-10 10:37:37 -070082 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
83 *
84 * @dev: device to read DT property from
85 * @propname: name of the property to read from
86 * @outp: place to put value (if found)
87 * @return 0 if OK, -ve on error
88 */
Simon Glass2f9a6122020-01-27 08:49:40 -070089int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
Simon Glass6df01f92018-12-10 10:37:37 -070090
91/**
92 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
93 *
94 * @dev: device to read DT property from
95 * @propname: name of the property to read from
96 * @def: default value to return if the property has no value
97 * @return property value, or @def if not found
98 */
Simon Glass2f9a6122020-01-27 08:49:40 -070099int dev_read_s32_default(const struct udevice *dev, const char *propname,
100 int def);
Simon Glass6df01f92018-12-10 10:37:37 -0700101
102/**
103 * dev_read_u32u() - read a 32-bit integer from a device's DT property
104 *
105 * This version uses a standard uint type.
106 *
107 * @dev: device to read DT property from
108 * @propname: name of the property to read from
109 * @outp: place to put value (if found)
110 * @return 0 if OK, -ve on error
111 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700112int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
Simon Glass6df01f92018-12-10 10:37:37 -0700113
114/**
T Karthik Reddy478860d2019-09-02 16:34:30 +0200115 * dev_read_u64() - read a 64-bit integer from a device's DT property
116 *
117 * @dev: device to read DT property from
118 * @propname: name of the property to read from
119 * @outp: place to put value (if found)
120 * @return 0 if OK, -ve on error
121 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700122int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
T Karthik Reddy478860d2019-09-02 16:34:30 +0200123
124/**
125 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
126 *
127 * @dev: device to read DT property from
128 * @propname: name of the property to read from
129 * @def: default value to return if the property has no value
130 * @return property value, or @def if not found
131 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700132u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
133 u64 def);
T Karthik Reddy478860d2019-09-02 16:34:30 +0200134
135/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600136 * dev_read_string() - Read a string from a device's DT property
137 *
138 * @dev: device to read DT property from
139 * @propname: name of the property to read
140 * @return string from property value, or NULL if there is no such property
141 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700142const char *dev_read_string(const struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -0600143
144/**
145 * dev_read_bool() - read a boolean value from a device's DT property
146 *
147 * @dev: device to read DT property from
148 * @propname: name of property to read
149 * @return true if property is present (meaning true), false if not present
150 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700151bool dev_read_bool(const struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -0600152
153/**
154 * dev_read_subnode() - find a named subnode of a device
155 *
156 * @dev: device whose DT node contains the subnode
157 * @subnode_name: name of subnode to find
158 * @return reference to subnode (which can be invalid if there is no such
159 * subnode)
160 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700161ofnode dev_read_subnode(const struct udevice *dev, const char *subbnode_name);
Simon Glass34ef0c22017-05-18 20:09:03 -0600162
163/**
164 * dev_read_size() - read the size of a property
165 *
166 * @dev: device to check
167 * @propname: property to check
168 * @return size of property if present, or -EINVAL if not
169 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700170int dev_read_size(const struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -0600171
172/**
173 * dev_read_addr_index() - Get the indexed reg property of a device
174 *
175 * @dev: Device to read from
176 * @index: the 'reg' property can hold a list of <addr, size> pairs
177 * and @index is used to select which one is required
178 *
179 * @return address or FDT_ADDR_T_NONE if not found
180 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700181fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
Simon Glass34ef0c22017-05-18 20:09:03 -0600182
183/**
Bin Meng21cdfea2021-09-12 11:15:13 +0800184 * dev_read_addr_index_ptr() - Get the indexed reg property of a device
185 * as a pointer
186 *
187 * @dev: Device to read from
188 * @index: the 'reg' property can hold a list of <addr, size> pairs
189 * and @index is used to select which one is required
190 *
191 * @return pointer or NULL if not found
192 */
193void *dev_read_addr_index_ptr(const struct udevice *dev, int index);
194
195/**
Sekhar Norif677c6f2019-08-01 19:12:56 +0530196 * dev_read_addr_size_index() - Get the indexed reg property of a device
197 *
198 * @dev: Device to read from
199 * @index: the 'reg' property can hold a list of <addr, size> pairs
200 * and @index is used to select which one is required
201 * @size: place to put size value (on success)
202 *
203 * @return address or FDT_ADDR_T_NONE if not found
204 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700205fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530206 fdt_size_t *size);
207
208/**
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200209 * dev_remap_addr_index() - Get the indexed reg property of a device
210 * as a memory-mapped I/O pointer
211 *
212 * @dev: Device to read from
213 * @index: the 'reg' property can hold a list of <addr, size> pairs
214 * and @index is used to select which one is required
215 *
216 * @return pointer or NULL if not found
217 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700218void *dev_remap_addr_index(const struct udevice *dev, int index);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200219
220/**
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100221 * dev_read_addr_name() - Get the reg property of a device, indexed by name
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 address or FDT_ADDR_T_NONE if not found
229 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700230fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100231
232/**
Sekhar Norif677c6f2019-08-01 19:12:56 +0530233 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
234 *
235 * @dev: Device to read from
236 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
237 * 'reg-names' property providing named-based identification. @index
238 * indicates the value to search for in 'reg-names'.
239 * @size: place to put size value (on success)
240 *
241 * @return address or FDT_ADDR_T_NONE if not found
242 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700243fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530244 fdt_size_t *size);
245
246/**
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100247 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
248 * as a memory-mapped I/O pointer
249 *
250 * @dev: Device to read from
251 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
252 * 'reg-names' property providing named-based identification. @index
253 * indicates the value to search for in 'reg-names'.
254 *
255 * @return pointer or NULL if not found
256 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700257void *dev_remap_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100258
259/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600260 * dev_read_addr() - Get the reg property of a device
261 *
262 * @dev: Device to read from
263 *
264 * @return address or FDT_ADDR_T_NONE if not found
265 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700266fdt_addr_t dev_read_addr(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600267
268/**
Philipp Tomsich7719b392017-09-11 22:04:12 +0200269 * dev_read_addr_ptr() - Get the reg property of a device
270 * as a pointer
271 *
272 * @dev: Device to read from
273 *
274 * @return pointer or NULL if not found
275 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700276void *dev_read_addr_ptr(const struct udevice *dev);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200277
278/**
Simon Glass23b27592019-09-15 12:08:58 -0600279 * dev_read_addr_pci() - Read an address and handle PCI address translation
280 *
281 * At present U-Boot does not have address translation logic for PCI in the
282 * livetree implementation (of_addr.c). This special function supports this for
283 * the flat tree implementation.
284 *
285 * This function should be removed (and code should use dev_read() instead)
286 * once:
287 *
288 * 1. PCI address translation is added; and either
289 * 2. everything uses livetree where PCI translation is used (which is feasible
290 * in SPL and U-Boot proper) or PCI address translation is added to
291 * fdtdec_get_addr() and friends.
292 *
293 * @dev: Device to read from
294 * @return address or FDT_ADDR_T_NONE if not found
295 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700296fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
Simon Glass23b27592019-09-15 12:08:58 -0600297
298/**
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200299 * dev_remap_addr() - Get the reg property of a device as a
300 * memory-mapped I/O pointer
301 *
302 * @dev: Device to read from
303 *
304 * @return pointer or NULL if not found
305 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700306void *dev_remap_addr(const struct udevice *dev);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200307
308/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600309 * dev_read_addr_size() - get address and size from a device property
310 *
311 * This does no address translation. It simply reads an property that contains
312 * an address and a size value, one after the other.
313 *
314 * @dev: Device to read from
315 * @propname: property to read
316 * @sizep: place to put size value (on success)
317 * @return address value, or FDT_ADDR_T_NONE on error
318 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700319fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname,
320 fdt_size_t *sizep);
Simon Glass34ef0c22017-05-18 20:09:03 -0600321
322/**
323 * dev_read_name() - get the name of a device's node
324 *
Bin Meng4cf914d2019-07-05 09:23:18 -0700325 * @dev: Device to read from
Simon Glass34ef0c22017-05-18 20:09:03 -0600326 * @return name of node
327 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700328const char *dev_read_name(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600329
330/**
331 * dev_read_stringlist_search() - find string in a string list and return index
332 *
333 * Note that it is possible for this function to succeed on property values
334 * that are not NUL-terminated. That's because the function will stop after
335 * finding the first occurrence of @string. This can for example happen with
336 * small-valued cell properties, such as #address-cells, when searching for
337 * the empty string.
338 *
339 * @dev: device to check
340 * @propname: name of the property containing the string list
341 * @string: string to look up in the string list
342 *
343 * @return:
344 * the index of the string in the list of strings
345 * -ENODATA if the property is not found
346 * -EINVAL on some other error
347 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700348int dev_read_stringlist_search(const struct udevice *dev, const char *property,
349 const char *string);
Simon Glass34ef0c22017-05-18 20:09:03 -0600350
351/**
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200352 * dev_read_string_index() - obtain an indexed string from a string list
353 *
354 * @dev: device to examine
355 * @propname: name of the property containing the string list
356 * @index: index of the string to return
357 * @out: return location for the string
358 *
359 * @return:
360 * length of string, if found or -ve error value if not found
361 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700362int dev_read_string_index(const struct udevice *dev, const char *propname,
363 int index, const char **outp);
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200364
365/**
366 * dev_read_string_count() - find the number of strings in a string list
367 *
368 * @dev: device to examine
369 * @propname: name of the property containing the string list
370 * @return:
371 * number of strings in the list, or -ve error value if not found
372 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700373int dev_read_string_count(const struct udevice *dev, const char *propname);
Simon Glass9580bfc2021-10-23 17:26:07 -0600374
375/**
376 * dev_read_string_list() - read a list of strings
377 *
378 * This produces a list of string pointers with each one pointing to a string
379 * in the string list. If the property does not exist, it returns {NULL}.
380 *
381 * The data is allocated and the caller is reponsible for freeing the return
382 * value (the list of string pointers). The strings themselves may not be
383 * changed as they point directly into the devicetree property.
384 *
385 * @dev: device to examine
386 * @propname: name of the property containing the string list
387 * @listp: returns an allocated, NULL-terminated list of strings if the return
388 * value is > 0, else is set to NULL
389 * @return number of strings in list, 0 if none, -ENOMEM if out of memory,
390 * -ENOENT if no such property
391 */
392int dev_read_string_list(const struct udevice *dev, const char *propname,
393 const char ***listp);
394
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200395/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600396 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
397 *
398 * This function is useful to parse lists of phandles and their arguments.
399 * Returns 0 on success and fills out_args, on error returns appropriate
400 * errno value.
401 *
402 * Caller is responsible to call of_node_put() on the returned out_args->np
403 * pointer.
404 *
405 * Example:
406 *
407 * phandle1: node1 {
408 * #list-cells = <2>;
409 * }
410 *
411 * phandle2: node2 {
412 * #list-cells = <1>;
413 * }
414 *
415 * node3 {
416 * list = <&phandle1 1 2 &phandle2 3>;
417 * }
418 *
419 * To get a device_node of the `node2' node you may call this:
420 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
421 *
422 * @dev: device whose node containing a list
423 * @list_name: property name that contains a list
424 * @cells_name: property name that specifies phandles' arguments count
425 * @cells_count: Cell count to use if @cells_name is NULL
426 * @index: index of a phandle to parse out
427 * @out_args: optional pointer to output arguments structure (will be filled)
428 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
429 * @list_name does not exist, -EINVAL if a phandle was not found,
430 * @cells_name could not be found, the arguments were truncated or there
431 * were too many arguments.
432 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700433int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
434 const char *cells_name, int cell_count,
435 int index, struct ofnode_phandle_args *out_args);
Simon Glass34ef0c22017-05-18 20:09:03 -0600436
437/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200438 * dev_count_phandle_with_args() - Return phandle number in a list
439 *
440 * This function is usefull to get phandle number contained in a property list.
441 * For example, this allows to allocate the right amount of memory to keep
442 * clock's reference contained into the "clocks" property.
443 *
444 *
445 * @dev: device whose node containing a list
446 * @list_name: property name that contains a list
447 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunayd776a842020-09-25 09:41:14 +0200448 * @cells_count: Cell count to use if @cells_name is NULL
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200449 * @Returns number of phandle found on success, on error returns appropriate
450 * errno value.
451 */
452
Simon Glass2f9a6122020-01-27 08:49:40 -0700453int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200454 const char *list_name, const char *cells_name,
455 int cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200456
457/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600458 * dev_read_addr_cells() - Get the number of address cells for a device's node
459 *
460 * This walks back up the tree to find the closest #address-cells property
461 * which controls the given node.
462 *
Mario Six16408ee2018-01-15 11:09:36 +0100463 * @dev: device to check
Simon Glass34ef0c22017-05-18 20:09:03 -0600464 * @return number of address cells this node uses
465 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700466int dev_read_addr_cells(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600467
468/**
469 * dev_read_size_cells() - Get the number of size cells for a device's node
470 *
471 * This walks back up the tree to find the closest #size-cells property
472 * which controls the given node.
473 *
Mario Six16408ee2018-01-15 11:09:36 +0100474 * @dev: device to check
Simon Glass34ef0c22017-05-18 20:09:03 -0600475 * @return number of size cells this node uses
476 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700477int dev_read_size_cells(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600478
479/**
Simon Glass4191dc12017-06-12 06:21:31 -0600480 * dev_read_addr_cells() - Get the address cells property in a node
481 *
482 * This function matches fdt_address_cells().
483 *
Mario Six16408ee2018-01-15 11:09:36 +0100484 * @dev: device to check
Simon Glass4191dc12017-06-12 06:21:31 -0600485 * @return number of address cells this node uses
486 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700487int dev_read_simple_addr_cells(const struct udevice *dev);
Simon Glass4191dc12017-06-12 06:21:31 -0600488
489/**
490 * dev_read_size_cells() - Get the size cells property in a node
491 *
492 * This function matches fdt_size_cells().
493 *
Mario Six16408ee2018-01-15 11:09:36 +0100494 * @dev: device to check
Simon Glass4191dc12017-06-12 06:21:31 -0600495 * @return number of size cells this node uses
496 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700497int dev_read_simple_size_cells(const struct udevice *dev);
Simon Glass4191dc12017-06-12 06:21:31 -0600498
499/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600500 * dev_read_phandle() - Get the phandle from a device
501 *
502 * @dev: device to check
503 * @return phandle (1 or greater), or 0 if no phandle or other error
504 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700505int dev_read_phandle(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600506
507/**
508 * dev_read_prop()- - read a property from a device's node
509 *
510 * @dev: device to check
511 * @propname: property to read
512 * @lenp: place to put length on success
513 * @return pointer to property, or NULL if not found
514 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700515const void *dev_read_prop(const struct udevice *dev, const char *propname,
516 int *lenp);
Simon Glass34ef0c22017-05-18 20:09:03 -0600517
518/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100519 * dev_read_first_prop()- get the reference of the first property
520 *
521 * Get reference to the first property of the node, it is used to iterate
522 * and read all the property with dev_read_prop_by_prop().
523 *
524 * @dev: device to check
525 * @prop: place to put argument reference
526 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
527 */
528int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
529
530/**
531 * ofnode_get_next_property() - get the reference of the next property
532 *
533 * Get reference to the next property of the node, it is used to iterate
534 * and read all the property with dev_read_prop_by_prop().
535 *
536 * @prop: reference of current argument and place to put reference of next one
537 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
538 */
539int dev_read_next_prop(struct ofprop *prop);
540
541/**
542 * dev_read_prop_by_prop() - get a pointer to the value of a property
543 *
544 * Get value for the property identified by the provided reference.
545 *
546 * @prop: reference on property
547 * @propname: If non-NULL, place to property name on success,
548 * @lenp: If non-NULL, place to put length on success
549 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
550 */
551const void *dev_read_prop_by_prop(struct ofprop *prop,
552 const char **propname, int *lenp);
553
554/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600555 * dev_read_alias_seq() - Get the alias sequence number of a node
556 *
557 * This works out whether a node is pointed to by an alias, and if so, the
558 * sequence number of that alias. Aliases are of the form <base><num> where
559 * <num> is the sequence number. For example spi2 would be sequence number 2.
560 *
561 * @dev: device to look up
562 * @devnump: set to the sequence number if one is found
563 * @return 0 if a sequence was found, -ve if not
564 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700565int dev_read_alias_seq(const struct udevice *dev, int *devnump);
Simon Glass34ef0c22017-05-18 20:09:03 -0600566
567/**
568 * dev_read_u32_array() - Find and read an array of 32 bit integers
569 *
570 * Search for a property in a device node and read 32-bit value(s) from
571 * it.
572 *
573 * The out_values is modified only if a valid u32 value can be decoded.
574 *
575 * @dev: device to look up
576 * @propname: name of the property to read
577 * @out_values: pointer to return value, modified only if return value is 0
578 * @sz: number of array elements to read
579 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
580 * property does not have a value, and -EOVERFLOW if the property data isn't
581 * large enough.
582 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700583int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600584 u32 *out_values, size_t sz);
Simon Glass34ef0c22017-05-18 20:09:03 -0600585
586/**
587 * dev_read_first_subnode() - find the first subnode of a device's node
588 *
589 * @dev: device to look up
590 * @return reference to the first subnode (which can be invalid if the device's
591 * node has no subnodes)
592 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700593ofnode dev_read_first_subnode(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600594
595/**
596 * ofnode_next_subnode() - find the next sibling of a subnode
597 *
598 * @node: valid reference to previous node (sibling)
599 * @return reference to the next subnode (which can be invalid if the node
600 * has no more siblings)
601 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600602ofnode dev_read_next_subnode(ofnode node);
Simon Glass34ef0c22017-05-18 20:09:03 -0600603
604/**
605 * dev_read_u8_array_ptr() - find an 8-bit array
606 *
607 * Look up a device's node property and return a pointer to its contents as a
608 * byte array of given length. The property must have at least enough data
609 * for the array (count bytes). It may have more, but this will be ignored.
610 * The data is not copied.
611 *
612 * @dev: device to look up
613 * @propname: name of property to find
614 * @sz: number of array elements
615 * @return pointer to byte array if found, or NULL if the property is not
616 * found or there is not enough data
617 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700618const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
619 const char *propname, size_t sz);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600620
Simon Glassfa031f82017-06-12 06:21:30 -0600621/**
622 * dev_read_enabled() - check whether a node is enabled
623 *
624 * This looks for a 'status' property. If this exists, then returns 1 if
625 * the status is 'ok' and 0 otherwise. If there is no status property,
626 * it returns 1 on the assumption that anything mentioned should be enabled
627 * by default.
628 *
629 * @dev: device to examine
630 * @return integer value 0 (not enabled) or 1 (enabled)
631 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700632int dev_read_enabled(const struct udevice *dev);
Simon Glassfa031f82017-06-12 06:21:30 -0600633
Simon Glassf7bfcc42017-07-25 08:29:55 -0600634/**
635 * dev_read_resource() - obtain an indexed resource from a device.
636 *
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900637 * @dev: device to examine
Simon Glassf7bfcc42017-07-25 08:29:55 -0600638 * @index index of the resource to retrieve (0 = first)
639 * @res returns the resource
640 * @return 0 if ok, negative on error
641 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700642int dev_read_resource(const struct udevice *dev, uint index,
643 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -0600644
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900645/**
646 * dev_read_resource_byname() - obtain a named resource from a device.
647 *
648 * @dev: device to examine
649 * @name: name of the resource to retrieve
650 * @res: returns the resource
651 * @return 0 if ok, negative on error
652 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700653int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900654 struct resource *res);
655
Mario Sixaefac062018-01-15 11:07:19 +0100656/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200657 * dev_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +0100658 *
659 * Translate an address from the device-tree into a CPU physical address. This
660 * function walks up the tree and applies the various bus mappings along the
661 * way.
662 *
663 * @dev: device giving the context in which to translate the address
664 * @in_addr: pointer to the address to translate
665 * @return the translated address; OF_BAD_ADDR on error
666 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700667u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
Michal Simekd0e30a02019-01-31 16:30:59 +0100668
669/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200670 * dev_translate_dma_address() - Translate a device-tree DMA address
671 *
672 * Translate a DMA address from the device-tree into a CPU physical address.
673 * This function walks up the tree and applies the various bus mappings along
674 * the way.
675 *
676 * @dev: device giving the context in which to translate the DMA address
677 * @in_addr: pointer to the DMA address to translate
678 * @return the translated DMA address; OF_BAD_ADDR on error
679 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700680u64 dev_translate_dma_address(const struct udevice *dev,
681 const fdt32_t *in_addr);
Fabien Dessenne22236e02019-05-31 15:11:30 +0200682
683/**
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +0100684 * dev_get_dma_range() - Get a device's DMA constraints
685 *
686 * Provide the address bases and size of the linear mapping between the CPU and
687 * a device's BUS address space.
688 *
689 * @dev: device giving the context in which to translate the DMA address
690 * @cpu: base address for CPU's view of memory
691 * @bus: base address for BUS's view of memory
692 * @size: size of the address space
693 * @return 0 if ok, negative on error
694 */
695int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
696 dma_addr_t *bus, u64 *size);
697
698/**
Michal Simekd0e30a02019-01-31 16:30:59 +0100699 * dev_read_alias_highest_id - Get highest alias id for the given stem
700 * @stem: Alias stem to be examined
701 *
702 * The function travels the lookup table to get the highest alias id for the
703 * given alias stem.
704 * @return alias ID, if found, else -1
705 */
706int dev_read_alias_highest_id(const char *stem);
707
developerd93c8b42020-05-02 11:35:09 +0200708/**
709 * dev_get_child_count() - get the child count of a device
710 *
711 * @dev: device to use for interation (struct udevice *)
712 * @return the count of child subnode
713 */
714int dev_get_child_count(const struct udevice *dev);
715
Stefan Roese0a9ecc52020-08-05 13:56:11 +0200716/**
717 * dev_read_pci_bus_range - Read PCI bus-range resource
718 *
719 * Look at the bus range property of a device node and return the pci bus
720 * range for this node.
721 *
722 * @dev: device to examine
723 * @res returns the resource
724 * @return 0 if ok, negative on error
725 */
726int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
727
Dario Binacchi836cc9d2020-12-30 00:16:26 +0100728/**
729 * dev_decode_display_timing() - decode display timings
730 *
731 * Decode display timings from the supplied 'display-timings' node.
732 * See doc/device-tree-bindings/video/display-timing.txt for binding
733 * information.
734 *
735 * @dev: device to read DT display timings from. The node linked to the device
736 * contains a child node called 'display-timings' which in turn contains
737 * one or more display timing nodes.
738 * @index: index number to read (0=first timing subnode)
739 * @config: place to put timings
740 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
741 */
742int dev_decode_display_timing(const struct udevice *dev, int index,
743 struct display_timing *config);
744
Simon Glasscfdae5c2017-05-18 20:09:04 -0600745#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
Simon Glass3ba929a2020-10-30 21:38:53 -0600746#include <asm/global_data.h>
Simon Glasscfdae5c2017-05-18 20:09:04 -0600747
Simon Glass2f9a6122020-01-27 08:49:40 -0700748static inline int dev_read_u32(const struct udevice *dev,
Masahiro Yamada71d115f2017-12-30 02:00:05 +0900749 const char *propname, u32 *outp)
750{
751 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
752}
753
Simon Glass2f9a6122020-01-27 08:49:40 -0700754static inline int dev_read_u32_default(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600755 const char *propname, int def)
756{
757 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
758}
759
Dario Binacchi81d80b52020-03-29 18:04:41 +0200760static inline int dev_read_u32_index(struct udevice *dev,
761 const char *propname, int index, u32 *outp)
762{
763 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
764}
765
766static inline u32 dev_read_u32_index_default(struct udevice *dev,
767 const char *propname, int index,
768 u32 def)
769{
770 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
771 def);
772}
773
Simon Glass2f9a6122020-01-27 08:49:40 -0700774static inline int dev_read_s32(const struct udevice *dev,
Simon Glass6df01f92018-12-10 10:37:37 -0700775 const char *propname, s32 *outp)
776{
777 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
778}
779
Simon Glass2f9a6122020-01-27 08:49:40 -0700780static inline int dev_read_s32_default(const struct udevice *dev,
Simon Glass6df01f92018-12-10 10:37:37 -0700781 const char *propname, int def)
782{
783 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
784}
785
Simon Glass2f9a6122020-01-27 08:49:40 -0700786static inline int dev_read_u32u(const struct udevice *dev,
Simon Glass6df01f92018-12-10 10:37:37 -0700787 const char *propname, uint *outp)
788{
789 u32 val;
790 int ret;
791
792 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
793 if (ret)
794 return ret;
795 *outp = val;
796
797 return 0;
798}
799
Simon Glass2f9a6122020-01-27 08:49:40 -0700800static inline int dev_read_u64(const struct udevice *dev,
T Karthik Reddy478860d2019-09-02 16:34:30 +0200801 const char *propname, u64 *outp)
802{
803 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
804}
805
Simon Glass2f9a6122020-01-27 08:49:40 -0700806static inline u64 dev_read_u64_default(const struct udevice *dev,
T Karthik Reddy478860d2019-09-02 16:34:30 +0200807 const char *propname, u64 def)
808{
809 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
810}
811
Simon Glass2f9a6122020-01-27 08:49:40 -0700812static inline const char *dev_read_string(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600813 const char *propname)
814{
815 return ofnode_read_string(dev_ofnode(dev), propname);
816}
817
Simon Glass2f9a6122020-01-27 08:49:40 -0700818static inline bool dev_read_bool(const struct udevice *dev,
819 const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600820{
821 return ofnode_read_bool(dev_ofnode(dev), propname);
822}
823
Simon Glass2f9a6122020-01-27 08:49:40 -0700824static inline ofnode dev_read_subnode(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600825 const char *subbnode_name)
826{
827 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
828}
829
Simon Glass2f9a6122020-01-27 08:49:40 -0700830static inline int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600831{
832 return ofnode_read_size(dev_ofnode(dev), propname);
833}
834
Simon Glass2f9a6122020-01-27 08:49:40 -0700835static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
836 int index)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600837{
838 return devfdt_get_addr_index(dev, index);
839}
840
Bin Meng21cdfea2021-09-12 11:15:13 +0800841static inline void *dev_read_addr_index_ptr(const struct udevice *dev,
842 int index)
843{
844 return devfdt_get_addr_index_ptr(dev, index);
845}
846
Simon Glass2f9a6122020-01-27 08:49:40 -0700847static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530848 int index,
849 fdt_size_t *size)
850{
851 return devfdt_get_addr_size_index(dev, index, size);
852}
853
Simon Glass2f9a6122020-01-27 08:49:40 -0700854static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100855 const char *name)
856{
857 return devfdt_get_addr_name(dev, name);
858}
859
Simon Glass2f9a6122020-01-27 08:49:40 -0700860static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530861 const char *name,
862 fdt_size_t *size)
863{
864 return devfdt_get_addr_size_name(dev, name, size);
865}
866
Simon Glass2f9a6122020-01-27 08:49:40 -0700867static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600868{
869 return devfdt_get_addr(dev);
870}
871
Simon Glass2f9a6122020-01-27 08:49:40 -0700872static inline void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsich7719b392017-09-11 22:04:12 +0200873{
Ovidiu Panaita633f002020-08-03 22:17:35 +0300874 return devfdt_get_addr_ptr(dev);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200875}
876
Simon Glass2f9a6122020-01-27 08:49:40 -0700877static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -0600878{
879 return devfdt_get_addr_pci(dev);
880}
881
Simon Glass2f9a6122020-01-27 08:49:40 -0700882static inline void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200883{
884 return devfdt_remap_addr(dev);
885}
886
Simon Glass2f9a6122020-01-27 08:49:40 -0700887static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200888{
889 return devfdt_remap_addr_index(dev, index);
890}
891
Simon Glass2f9a6122020-01-27 08:49:40 -0700892static inline void *dev_remap_addr_name(const struct udevice *dev,
893 const char *name)
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100894{
895 return devfdt_remap_addr_name(dev, name);
896}
897
Simon Glass2f9a6122020-01-27 08:49:40 -0700898static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600899 const char *propname,
900 fdt_size_t *sizep)
901{
902 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
903}
904
Simon Glass2f9a6122020-01-27 08:49:40 -0700905static inline const char *dev_read_name(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600906{
907 return ofnode_get_name(dev_ofnode(dev));
908}
909
Simon Glass2f9a6122020-01-27 08:49:40 -0700910static inline int dev_read_stringlist_search(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600911 const char *propname,
912 const char *string)
913{
914 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
915}
916
Simon Glass2f9a6122020-01-27 08:49:40 -0700917static inline int dev_read_string_index(const struct udevice *dev,
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200918 const char *propname, int index,
919 const char **outp)
920{
921 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
922}
923
Simon Glass2f9a6122020-01-27 08:49:40 -0700924static inline int dev_read_string_count(const struct udevice *dev,
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200925 const char *propname)
926{
927 return ofnode_read_string_count(dev_ofnode(dev), propname);
928}
929
Simon Glass9580bfc2021-10-23 17:26:07 -0600930static inline int dev_read_string_list(const struct udevice *dev,
931 const char *propname,
932 const char ***listp)
933{
934 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
935}
936
Simon Glass2f9a6122020-01-27 08:49:40 -0700937static inline int dev_read_phandle_with_args(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600938 const char *list_name, const char *cells_name, int cell_count,
939 int index, struct ofnode_phandle_args *out_args)
940{
941 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
942 cells_name, cell_count, index,
943 out_args);
944}
945
Simon Glass2f9a6122020-01-27 08:49:40 -0700946static inline int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200947 const char *list_name, const char *cells_name, int cell_count)
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200948{
949 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200950 cells_name, cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200951}
952
Simon Glass2f9a6122020-01-27 08:49:40 -0700953static inline int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600954{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200955 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
956
957 return fdt_address_cells(gd->fdt_blob, parent);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600958}
959
Simon Glass2f9a6122020-01-27 08:49:40 -0700960static inline int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600961{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200962 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
963
964 return fdt_size_cells(gd->fdt_blob, parent);
Simon Glass4191dc12017-06-12 06:21:31 -0600965}
966
Simon Glass2f9a6122020-01-27 08:49:40 -0700967static inline int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600968{
969 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
970}
971
Simon Glass2f9a6122020-01-27 08:49:40 -0700972static inline int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -0600973{
Simon Glasscfdae5c2017-05-18 20:09:04 -0600974 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
975}
976
Simon Glass2f9a6122020-01-27 08:49:40 -0700977static inline int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600978{
979 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
980}
981
Simon Glass2f9a6122020-01-27 08:49:40 -0700982static inline const void *dev_read_prop(const struct udevice *dev,
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900983 const char *propname, int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600984{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900985 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600986}
987
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100988static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
989{
990 return ofnode_get_first_property(dev_ofnode(dev), prop);
991}
992
993static inline int dev_read_next_prop(struct ofprop *prop)
994{
995 return ofnode_get_next_property(prop);
996}
997
998static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
999 const char **propname,
1000 int *lenp)
1001{
1002 return ofnode_get_property_by_prop(prop, propname, lenp);
1003}
1004
Simon Glass2f9a6122020-01-27 08:49:40 -07001005static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001006{
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +02001007#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001008 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
1009 dev_of_offset(dev), devnump);
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +02001010#else
1011 return -ENOTSUPP;
1012#endif
Simon Glasscfdae5c2017-05-18 20:09:04 -06001013}
1014
Simon Glass2f9a6122020-01-27 08:49:40 -07001015static inline int dev_read_u32_array(const struct udevice *dev,
1016 const char *propname, u32 *out_values,
1017 size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001018{
1019 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
1020}
1021
Simon Glass2f9a6122020-01-27 08:49:40 -07001022static inline ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001023{
1024 return ofnode_first_subnode(dev_ofnode(dev));
1025}
1026
1027static inline ofnode dev_read_next_subnode(ofnode node)
1028{
1029 return ofnode_next_subnode(node);
1030}
1031
Simon Glass2f9a6122020-01-27 08:49:40 -07001032static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
Simon Glass000676b2020-01-27 08:49:47 -07001033 const char *propname,
1034 size_t sz)
Simon Glass34ef0c22017-05-18 20:09:03 -06001035{
1036 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
1037}
1038
Simon Glass2f9a6122020-01-27 08:49:40 -07001039static inline int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -06001040{
1041 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
1042}
1043
Simon Glass2f9a6122020-01-27 08:49:40 -07001044static inline int dev_read_resource(const struct udevice *dev, uint index,
Simon Glassf7bfcc42017-07-25 08:29:55 -06001045 struct resource *res)
1046{
1047 return ofnode_read_resource(dev_ofnode(dev), index, res);
1048}
1049
Simon Glass2f9a6122020-01-27 08:49:40 -07001050static inline int dev_read_resource_byname(const struct udevice *dev,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +09001051 const char *name,
1052 struct resource *res)
1053{
1054 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
1055}
1056
Simon Glass2f9a6122020-01-27 08:49:40 -07001057static inline u64 dev_translate_address(const struct udevice *dev,
1058 const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +01001059{
1060 return ofnode_translate_address(dev_ofnode(dev), in_addr);
1061}
1062
Simon Glass2f9a6122020-01-27 08:49:40 -07001063static inline u64 dev_translate_dma_address(const struct udevice *dev,
1064 const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +02001065{
1066 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
1067}
1068
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001069static inline int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
1070 dma_addr_t *bus, u64 *size)
1071{
1072 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
1073}
1074
Michal Simekd0e30a02019-01-31 16:30:59 +01001075static inline int dev_read_alias_highest_id(const char *stem)
1076{
Stanislav Pinchuke8649402021-01-20 21:52:23 +03001077 if (!CONFIG_IS_ENABLED(OF_LIBFDT) || !gd->fdt_blob)
Michael Walleeccf2d02020-06-02 01:47:08 +02001078 return -1;
Michal Simekd0e30a02019-01-31 16:30:59 +01001079 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
1080}
1081
developerd93c8b42020-05-02 11:35:09 +02001082static inline int dev_get_child_count(const struct udevice *dev)
1083{
1084 return ofnode_get_child_count(dev_ofnode(dev));
1085}
1086
Dario Binacchi836cc9d2020-12-30 00:16:26 +01001087static inline int dev_decode_display_timing(const struct udevice *dev,
1088 int index,
1089 struct display_timing *config)
1090{
1091 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
1092}
1093
Simon Glass34ef0c22017-05-18 20:09:03 -06001094#endif /* CONFIG_DM_DEV_READ_INLINE */
1095
1096/**
1097 * dev_for_each_subnode() - Helper function to iterate through subnodes
1098 *
1099 * This creates a for() loop which works through the subnodes in a device's
1100 * device-tree node.
1101 *
1102 * @subnode: ofnode holding the current subnode
1103 * @dev: device to use for interation (struct udevice *)
1104 */
1105#define dev_for_each_subnode(subnode, dev) \
1106 for (subnode = dev_read_first_subnode(dev); \
1107 ofnode_valid(subnode); \
1108 subnode = ofnode_next_subnode(subnode))
1109
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001110/**
1111 * dev_for_each_property() - Helper function to iterate through property
1112 *
1113 * This creates a for() loop which works through the property in a device's
1114 * device-tree node.
1115 *
1116 * @prop: struct ofprop holding the current property
1117 * @dev: device to use for interation (struct udevice *)
1118 */
1119#define dev_for_each_property(prop, dev) \
1120 for (int ret_prop = dev_read_first_prop(dev, &prop); \
1121 !ret_prop; \
1122 ret_prop = dev_read_next_prop(&prop))
1123
Simon Glass34ef0c22017-05-18 20:09:03 -06001124#endif