blob: 122b9cd15b80e9c6ee0766b79193d47a06962d17 [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
Dan Murphy9beacb62020-07-23 07:01:38 -050012#include <linux/errno.h>
13
Simon Glass2a64ada2020-07-19 10:15:39 -060014#include <dm/device.h>
Simon Glassf11c7ab2017-05-18 20:09:03 -060015#include <dm/fdtaddr.h>
16#include <dm/ofnode.h>
17#include <dm/uclass.h>
18
Simon Glassa4481012017-06-12 06:21:29 -060019struct resource;
20
Simon Glassf11c7ab2017-05-18 20:09:03 -060021#if CONFIG_IS_ENABLED(OF_LIVE)
Simon Glass88b3a372020-01-27 08:49:40 -070022static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glassf11c7ab2017-05-18 20:09:03 -060023{
Simon Glassf10643c2020-12-19 10:40:14 -070024 return ofnode_to_np(dev_ofnode(dev));
Simon Glassf11c7ab2017-05-18 20:09:03 -060025}
26#else
Simon Glass88b3a372020-01-27 08:49:40 -070027static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glassf11c7ab2017-05-18 20:09:03 -060028{
29 return NULL;
30}
31#endif
32
Simon Glassef79ef22021-01-21 13:57:10 -070033#if !defined(CONFIG_DM_DEV_READ_INLINE) || CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass47a0fd32017-05-18 20:09:04 -060034/**
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +020035 * dev_read_u8() - read a 8-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 */
42int dev_read_u8(const struct udevice *dev, const char *propname, u8 *outp);
43
44/**
45 * dev_read_u8_default() - read a 8-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 */
52u8 dev_read_u8_default(const struct udevice *dev, const char *propname, u8 def);
53
54/**
55 * dev_read_u16() - read a 16-bit integer from a device's DT property
56 *
57 * @dev: device to read DT property from
58 * @propname: name of the property to read from
59 * @outp: place to put value (if found)
60 * Return: 0 if OK, -ve on error
61 */
62int dev_read_u16(const struct udevice *dev, const char *propname, u16 *outp);
63
64/**
65 * dev_read_u16_default() - read a 16-bit integer from a device's DT property
66 *
67 * @dev: device to read DT property from
68 * @propname: name of the property to read from
69 * @def: default value to return if the property has no value
70 * Return: property value, or @def if not found
71 */
72u16 dev_read_u16_default(const struct udevice *dev, const char *propname,
73 u16 def);
74
75/**
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090076 * dev_read_u32() - read a 32-bit integer from a device's DT property
77 *
78 * @dev: device to read DT property from
79 * @propname: name of the property to read from
80 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +010081 * Return: 0 if OK, -ve on error
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090082 */
Simon Glass88b3a372020-01-27 08:49:40 -070083int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090084
85/**
Simon Glass47a0fd32017-05-18 20:09:04 -060086 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
87 *
88 * @dev: device to read DT property from
89 * @propname: name of the property to read from
90 * @def: default value to return if the property has no value
Patrick Delaunay6de6a612022-01-12 10:53:48 +010091 * Return: property value, or @def if not found
Simon Glass47a0fd32017-05-18 20:09:04 -060092 */
Simon Glass88b3a372020-01-27 08:49:40 -070093int dev_read_u32_default(const struct udevice *dev, const char *propname,
94 int def);
Simon Glassf11c7ab2017-05-18 20:09:03 -060095
96/**
Dario Binacchi4bb70752020-03-29 18:04:41 +020097 * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
98 * property
99 *
100 * @dev: device to read DT property from
101 * @propname: name of the property to read from
102 * @index: index of the integer to return
103 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100104 * Return: 0 if OK, -ve on error
Dario Binacchi4bb70752020-03-29 18:04:41 +0200105 */
106int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
107 u32 *outp);
108
109/**
110 * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
111 * DT property
112 *
113 * @dev: device to read DT property from
114 * @propname: name of the property to read from
115 * @index: index of the integer to return
116 * @def: default value to return if the property has no value
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100117 * Return: property value, or @def if not found
Dario Binacchi4bb70752020-03-29 18:04:41 +0200118 */
119u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
120 int index, u32 def);
121
122/**
Simon Glassa1b17e42018-12-10 10:37:37 -0700123 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
124 *
125 * @dev: device to read DT property from
126 * @propname: name of the property to read from
127 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100128 * Return: 0 if OK, -ve on error
Simon Glassa1b17e42018-12-10 10:37:37 -0700129 */
Simon Glass88b3a372020-01-27 08:49:40 -0700130int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
Simon Glassa1b17e42018-12-10 10:37:37 -0700131
132/**
133 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
134 *
135 * @dev: device to read DT property from
136 * @propname: name of the property to read from
137 * @def: default value to return if the property has no value
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100138 * Return: property value, or @def if not found
Simon Glassa1b17e42018-12-10 10:37:37 -0700139 */
Simon Glass88b3a372020-01-27 08:49:40 -0700140int dev_read_s32_default(const struct udevice *dev, const char *propname,
141 int def);
Simon Glassa1b17e42018-12-10 10:37:37 -0700142
143/**
144 * dev_read_u32u() - read a 32-bit integer from a device's DT property
145 *
146 * This version uses a standard uint type.
147 *
148 * @dev: device to read DT property from
149 * @propname: name of the property to read from
150 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100151 * Return: 0 if OK, -ve on error
Simon Glassa1b17e42018-12-10 10:37:37 -0700152 */
Simon Glass88b3a372020-01-27 08:49:40 -0700153int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
Simon Glassa1b17e42018-12-10 10:37:37 -0700154
155/**
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200156 * dev_read_u64() - read a 64-bit integer from a device's DT property
157 *
158 * @dev: device to read DT property from
159 * @propname: name of the property to read from
160 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100161 * Return: 0 if OK, -ve on error
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200162 */
Simon Glass88b3a372020-01-27 08:49:40 -0700163int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200164
165/**
166 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
167 *
168 * @dev: device to read DT property from
169 * @propname: name of the property to read from
170 * @def: default value to return if the property has no value
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100171 * Return: property value, or @def if not found
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200172 */
Simon Glass88b3a372020-01-27 08:49:40 -0700173u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
174 u64 def);
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200175
176/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600177 * dev_read_string() - Read a string from a device's DT property
178 *
179 * @dev: device to read DT property from
180 * @propname: name of the property to read
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100181 * Return: string from property value, or NULL if there is no such property
Simon Glassf11c7ab2017-05-18 20:09:03 -0600182 */
Simon Glass88b3a372020-01-27 08:49:40 -0700183const char *dev_read_string(const struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600184
185/**
186 * dev_read_bool() - read a boolean value from a device's DT property
187 *
188 * @dev: device to read DT property from
189 * @propname: name of property to read
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100190 * Return: true if property is present (meaning true), false if not present
Simon Glassf11c7ab2017-05-18 20:09:03 -0600191 */
Simon Glass88b3a372020-01-27 08:49:40 -0700192bool dev_read_bool(const struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600193
194/**
195 * dev_read_subnode() - find a named subnode of a device
196 *
197 * @dev: device whose DT node contains the subnode
198 * @subnode_name: name of subnode to find
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100199 * Return: reference to subnode (which can be invalid if there is no such
Simon Glassf11c7ab2017-05-18 20:09:03 -0600200 * subnode)
201 */
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100202ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600203
204/**
205 * dev_read_size() - read the size of a property
206 *
207 * @dev: device to check
208 * @propname: property to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100209 * Return: size of property if present, or -EINVAL if not
Simon Glassf11c7ab2017-05-18 20:09:03 -0600210 */
Simon Glass88b3a372020-01-27 08:49:40 -0700211int dev_read_size(const struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600212
213/**
214 * dev_read_addr_index() - Get the indexed reg property of a device
215 *
216 * @dev: Device to read from
217 * @index: the 'reg' property can hold a list of <addr, size> pairs
218 * and @index is used to select which one is required
219 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100220 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glassf11c7ab2017-05-18 20:09:03 -0600221 */
Simon Glass88b3a372020-01-27 08:49:40 -0700222fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600223
224/**
Bin Mengbdce9032021-09-12 11:15:13 +0800225 * dev_read_addr_index_ptr() - Get the indexed reg property of a device
226 * as a pointer
227 *
228 * @dev: Device to read from
229 * @index: the 'reg' property can hold a list of <addr, size> pairs
230 * and @index is used to select which one is required
231 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100232 * Return: pointer or NULL if not found
Bin Mengbdce9032021-09-12 11:15:13 +0800233 */
234void *dev_read_addr_index_ptr(const struct udevice *dev, int index);
235
236/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530237 * dev_read_addr_size_index() - Get the indexed reg property of a device
238 *
239 * @dev: Device to read from
240 * @index: the 'reg' property can hold a list of <addr, size> pairs
241 * and @index is used to select which one is required
242 * @size: place to put size value (on success)
243 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100244 * Return: address or FDT_ADDR_T_NONE if not found
Sekhar Norif5b90472019-08-01 19:12:56 +0530245 */
Simon Glass88b3a372020-01-27 08:49:40 -0700246fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif5b90472019-08-01 19:12:56 +0530247 fdt_size_t *size);
248
249/**
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200250 * dev_remap_addr_index() - Get the indexed reg property of a device
251 * as a memory-mapped I/O pointer
252 *
253 * @dev: Device to read from
254 * @index: the 'reg' property can hold a list of <addr, size> pairs
255 * and @index is used to select which one is required
256 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100257 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200258 */
Simon Glass88b3a372020-01-27 08:49:40 -0700259void *dev_remap_addr_index(const struct udevice *dev, int index);
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200260
261/**
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100262 * dev_read_addr_name() - Get the reg property of a device, indexed by name
263 *
264 * @dev: Device to read from
265 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
266 * 'reg-names' property providing named-based identification. @index
267 * indicates the value to search for in 'reg-names'.
268 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100269 * Return: address or FDT_ADDR_T_NONE if not found
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100270 */
Simon Glass88b3a372020-01-27 08:49:40 -0700271fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100272
273/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530274 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
275 *
276 * @dev: Device to read from
277 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
278 * 'reg-names' property providing named-based identification. @index
279 * indicates the value to search for in 'reg-names'.
280 * @size: place to put size value (on success)
281 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100282 * Return: address or FDT_ADDR_T_NONE if not found
Sekhar Norif5b90472019-08-01 19:12:56 +0530283 */
Simon Glass88b3a372020-01-27 08:49:40 -0700284fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif5b90472019-08-01 19:12:56 +0530285 fdt_size_t *size);
286
287/**
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100288 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
289 * as a memory-mapped I/O pointer
290 *
291 * @dev: Device to read from
292 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
293 * 'reg-names' property providing named-based identification. @index
294 * indicates the value to search for in 'reg-names'.
295 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100296 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100297 */
Simon Glass88b3a372020-01-27 08:49:40 -0700298void *dev_remap_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100299
300/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600301 * dev_read_addr() - Get the reg property of a device
302 *
303 * @dev: Device to read from
304 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100305 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glassf11c7ab2017-05-18 20:09:03 -0600306 */
Simon Glass88b3a372020-01-27 08:49:40 -0700307fdt_addr_t dev_read_addr(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600308
309/**
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200310 * dev_read_addr_ptr() - Get the reg property of a device
311 * as a pointer
312 *
313 * @dev: Device to read from
314 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100315 * Return: pointer or NULL if not found
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200316 */
Simon Glass88b3a372020-01-27 08:49:40 -0700317void *dev_read_addr_ptr(const struct udevice *dev);
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200318
319/**
Simon Glass33c215a2019-09-15 12:08:58 -0600320 * dev_read_addr_pci() - Read an address and handle PCI address translation
321 *
322 * At present U-Boot does not have address translation logic for PCI in the
323 * livetree implementation (of_addr.c). This special function supports this for
324 * the flat tree implementation.
325 *
326 * This function should be removed (and code should use dev_read() instead)
327 * once:
328 *
329 * 1. PCI address translation is added; and either
330 * 2. everything uses livetree where PCI translation is used (which is feasible
331 * in SPL and U-Boot proper) or PCI address translation is added to
332 * fdtdec_get_addr() and friends.
333 *
334 * @dev: Device to read from
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100335 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glass33c215a2019-09-15 12:08:58 -0600336 */
Simon Glass88b3a372020-01-27 08:49:40 -0700337fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
Simon Glass33c215a2019-09-15 12:08:58 -0600338
339/**
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200340 * dev_remap_addr() - Get the reg property of a device as a
341 * memory-mapped I/O pointer
342 *
343 * @dev: Device to read from
344 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100345 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200346 */
Simon Glass88b3a372020-01-27 08:49:40 -0700347void *dev_remap_addr(const struct udevice *dev);
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200348
349/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600350 * dev_read_addr_size() - get address and size from a device property
351 *
352 * This does no address translation. It simply reads an property that contains
353 * an address and a size value, one after the other.
354 *
355 * @dev: Device to read from
356 * @propname: property to read
357 * @sizep: place to put size value (on success)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100358 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glassf11c7ab2017-05-18 20:09:03 -0600359 */
Simon Glass88b3a372020-01-27 08:49:40 -0700360fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname,
361 fdt_size_t *sizep);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600362
363/**
364 * dev_read_name() - get the name of a device's node
365 *
Bin Meng15d61d02019-07-05 09:23:18 -0700366 * @dev: Device to read from
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100367 * Return: name of node
Simon Glassf11c7ab2017-05-18 20:09:03 -0600368 */
Simon Glass88b3a372020-01-27 08:49:40 -0700369const char *dev_read_name(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600370
371/**
372 * dev_read_stringlist_search() - find string in a string list and return index
373 *
374 * Note that it is possible for this function to succeed on property values
375 * that are not NUL-terminated. That's because the function will stop after
376 * finding the first occurrence of @string. This can for example happen with
377 * small-valued cell properties, such as #address-cells, when searching for
378 * the empty string.
379 *
380 * @dev: device to check
381 * @propname: name of the property containing the string list
382 * @string: string to look up in the string list
383 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100384 * Return:
Simon Glassf11c7ab2017-05-18 20:09:03 -0600385 * the index of the string in the list of strings
386 * -ENODATA if the property is not found
387 * -EINVAL on some other error
388 */
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100389int dev_read_stringlist_search(const struct udevice *dev, const char *propname,
Simon Glass88b3a372020-01-27 08:49:40 -0700390 const char *string);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600391
392/**
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200393 * dev_read_string_index() - obtain an indexed string from a string list
394 *
395 * @dev: device to examine
396 * @propname: name of the property containing the string list
397 * @index: index of the string to return
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100398 * @outp: return location for the string
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200399 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100400 * Return:
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200401 * length of string, if found or -ve error value if not found
402 */
Simon Glass88b3a372020-01-27 08:49:40 -0700403int dev_read_string_index(const struct udevice *dev, const char *propname,
404 int index, const char **outp);
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200405
406/**
407 * dev_read_string_count() - find the number of strings in a string list
408 *
409 * @dev: device to examine
410 * @propname: name of the property containing the string list
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100411 * Return:
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200412 * number of strings in the list, or -ve error value if not found
413 */
Simon Glass88b3a372020-01-27 08:49:40 -0700414int dev_read_string_count(const struct udevice *dev, const char *propname);
Simon Glass075bfc92021-10-23 17:26:07 -0600415
416/**
417 * dev_read_string_list() - read a list of strings
418 *
419 * This produces a list of string pointers with each one pointing to a string
420 * in the string list. If the property does not exist, it returns {NULL}.
421 *
422 * The data is allocated and the caller is reponsible for freeing the return
423 * value (the list of string pointers). The strings themselves may not be
424 * changed as they point directly into the devicetree property.
425 *
426 * @dev: device to examine
427 * @propname: name of the property containing the string list
428 * @listp: returns an allocated, NULL-terminated list of strings if the return
429 * value is > 0, else is set to NULL
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100430 * Return:
431 * number of strings in list, 0 if none, -ENOMEM if out of memory,
432 * -ENOENT if no such property
Simon Glass075bfc92021-10-23 17:26:07 -0600433 */
434int dev_read_string_list(const struct udevice *dev, const char *propname,
435 const char ***listp);
436
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200437/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600438 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
439 *
440 * This function is useful to parse lists of phandles and their arguments.
441 * Returns 0 on success and fills out_args, on error returns appropriate
442 * errno value.
443 *
444 * Caller is responsible to call of_node_put() on the returned out_args->np
445 * pointer.
446 *
447 * Example:
448 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100449 * .. code-block::
Simon Glassf11c7ab2017-05-18 20:09:03 -0600450 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100451 * phandle1: node1 {
452 * #list-cells = <2>;
453 * };
454 * phandle2: node2 {
455 * #list-cells = <1>;
456 * };
457 * node3 {
458 * list = <&phandle1 1 2 &phandle2 3>;
459 * };
Simon Glassf11c7ab2017-05-18 20:09:03 -0600460 *
461 * To get a device_node of the `node2' node you may call this:
462 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
463 *
464 * @dev: device whose node containing a list
465 * @list_name: property name that contains a list
466 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100467 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glassf11c7ab2017-05-18 20:09:03 -0600468 * @index: index of a phandle to parse out
469 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100470 * Return: 0 on success (with @out_args filled out if not NULL), -ENOENT if
Simon Glassf11c7ab2017-05-18 20:09:03 -0600471 * @list_name does not exist, -EINVAL if a phandle was not found,
472 * @cells_name could not be found, the arguments were truncated or there
473 * were too many arguments.
474 */
Simon Glass88b3a372020-01-27 08:49:40 -0700475int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
476 const char *cells_name, int cell_count,
477 int index, struct ofnode_phandle_args *out_args);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600478
479/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200480 * dev_count_phandle_with_args() - Return phandle number in a list
481 *
482 * This function is usefull to get phandle number contained in a property list.
483 * For example, this allows to allocate the right amount of memory to keep
484 * clock's reference contained into the "clocks" property.
485 *
Patrice Chotard642346a2017-07-18 11:57:08 +0200486 * @dev: device whose node containing a list
487 * @list_name: property name that contains a list
488 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100489 * @cell_count: Cell count to use if @cells_name is NULL
490 * Return: number of phandle found on success, on error returns appropriate
Patrice Chotard642346a2017-07-18 11:57:08 +0200491 * errno value.
492 */
493
Simon Glass88b3a372020-01-27 08:49:40 -0700494int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200495 const char *list_name, const char *cells_name,
496 int cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +0200497
498/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600499 * dev_read_addr_cells() - Get the number of address cells for a device's node
500 *
501 * This walks back up the tree to find the closest #address-cells property
502 * which controls the given node.
503 *
Mario Six7ba50412018-01-15 11:09:36 +0100504 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100505 * Return: number of address cells this node uses
Simon Glassf11c7ab2017-05-18 20:09:03 -0600506 */
Simon Glass88b3a372020-01-27 08:49:40 -0700507int dev_read_addr_cells(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600508
509/**
510 * dev_read_size_cells() - Get the number of size cells for a device's node
511 *
512 * This walks back up the tree to find the closest #size-cells property
513 * which controls the given node.
514 *
Mario Six7ba50412018-01-15 11:09:36 +0100515 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100516 * Return: number of size cells this node uses
Simon Glassf11c7ab2017-05-18 20:09:03 -0600517 */
Simon Glass88b3a372020-01-27 08:49:40 -0700518int dev_read_size_cells(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600519
520/**
Simon Glass878d68c2017-06-12 06:21:31 -0600521 * dev_read_addr_cells() - Get the address cells property in a node
522 *
523 * This function matches fdt_address_cells().
524 *
Mario Six7ba50412018-01-15 11:09:36 +0100525 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100526 * Return: number of address cells this node uses
Simon Glass878d68c2017-06-12 06:21:31 -0600527 */
Simon Glass88b3a372020-01-27 08:49:40 -0700528int dev_read_simple_addr_cells(const struct udevice *dev);
Simon Glass878d68c2017-06-12 06:21:31 -0600529
530/**
531 * dev_read_size_cells() - Get the size cells property in a node
532 *
533 * This function matches fdt_size_cells().
534 *
Mario Six7ba50412018-01-15 11:09:36 +0100535 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100536 * Return: number of size cells this node uses
Simon Glass878d68c2017-06-12 06:21:31 -0600537 */
Simon Glass88b3a372020-01-27 08:49:40 -0700538int dev_read_simple_size_cells(const struct udevice *dev);
Simon Glass878d68c2017-06-12 06:21:31 -0600539
540/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600541 * dev_read_phandle() - Get the phandle from a device
542 *
543 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100544 * Return: phandle (1 or greater), or 0 if no phandle or other error
Simon Glassf11c7ab2017-05-18 20:09:03 -0600545 */
Simon Glass88b3a372020-01-27 08:49:40 -0700546int dev_read_phandle(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600547
548/**
549 * dev_read_prop()- - read a property from a device's node
550 *
551 * @dev: device to check
552 * @propname: property to read
553 * @lenp: place to put length on success
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100554 * Return: pointer to property, or NULL if not found
Simon Glassf11c7ab2017-05-18 20:09:03 -0600555 */
Simon Glass88b3a372020-01-27 08:49:40 -0700556const void *dev_read_prop(const struct udevice *dev, const char *propname,
557 int *lenp);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600558
559/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100560 * dev_read_first_prop()- get the reference of the first property
561 *
562 * Get reference to the first property of the node, it is used to iterate
563 * and read all the property with dev_read_prop_by_prop().
564 *
565 * @dev: device to check
566 * @prop: place to put argument reference
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100567 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100568 */
569int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
570
571/**
572 * ofnode_get_next_property() - get the reference of the next property
573 *
574 * Get reference to the next property of the node, it is used to iterate
575 * and read all the property with dev_read_prop_by_prop().
576 *
577 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100578 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100579 */
580int dev_read_next_prop(struct ofprop *prop);
581
582/**
583 * dev_read_prop_by_prop() - get a pointer to the value of a property
584 *
585 * Get value for the property identified by the provided reference.
586 *
587 * @prop: reference on property
588 * @propname: If non-NULL, place to property name on success,
589 * @lenp: If non-NULL, place to put length on success
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100590 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100591 */
592const void *dev_read_prop_by_prop(struct ofprop *prop,
593 const char **propname, int *lenp);
594
595/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600596 * dev_read_alias_seq() - Get the alias sequence number of a node
597 *
598 * This works out whether a node is pointed to by an alias, and if so, the
599 * sequence number of that alias. Aliases are of the form <base><num> where
600 * <num> is the sequence number. For example spi2 would be sequence number 2.
601 *
602 * @dev: device to look up
603 * @devnump: set to the sequence number if one is found
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100604 * Return: 0 if a sequence was found, -ve if not
Simon Glassf11c7ab2017-05-18 20:09:03 -0600605 */
Simon Glass88b3a372020-01-27 08:49:40 -0700606int dev_read_alias_seq(const struct udevice *dev, int *devnump);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600607
608/**
609 * dev_read_u32_array() - Find and read an array of 32 bit integers
610 *
611 * Search for a property in a device node and read 32-bit value(s) from
612 * it.
613 *
614 * The out_values is modified only if a valid u32 value can be decoded.
615 *
616 * @dev: device to look up
617 * @propname: name of the property to read
618 * @out_values: pointer to return value, modified only if return value is 0
619 * @sz: number of array elements to read
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100620 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
Simon Glassf11c7ab2017-05-18 20:09:03 -0600621 * property does not have a value, and -EOVERFLOW if the property data isn't
622 * large enough.
623 */
Simon Glass88b3a372020-01-27 08:49:40 -0700624int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glass47a0fd32017-05-18 20:09:04 -0600625 u32 *out_values, size_t sz);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600626
627/**
628 * dev_read_first_subnode() - find the first subnode of a device's node
629 *
630 * @dev: device to look up
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100631 * Return: reference to the first subnode (which can be invalid if the device's
Simon Glassf11c7ab2017-05-18 20:09:03 -0600632 * node has no subnodes)
633 */
Simon Glass88b3a372020-01-27 08:49:40 -0700634ofnode dev_read_first_subnode(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600635
636/**
637 * ofnode_next_subnode() - find the next sibling of a subnode
638 *
639 * @node: valid reference to previous node (sibling)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100640 * Return: reference to the next subnode (which can be invalid if the node
Simon Glassf11c7ab2017-05-18 20:09:03 -0600641 * has no more siblings)
642 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600643ofnode dev_read_next_subnode(ofnode node);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600644
645/**
646 * dev_read_u8_array_ptr() - find an 8-bit array
647 *
648 * Look up a device's node property and return a pointer to its contents as a
649 * byte array of given length. The property must have at least enough data
650 * for the array (count bytes). It may have more, but this will be ignored.
651 * The data is not copied.
652 *
653 * @dev: device to look up
654 * @propname: name of property to find
655 * @sz: number of array elements
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100656 * Return:
657 * pointer to byte array if found, or NULL if the property is not found or
658 * there is not enough data
Simon Glassf11c7ab2017-05-18 20:09:03 -0600659 */
Simon Glass88b3a372020-01-27 08:49:40 -0700660const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
661 const char *propname, size_t sz);
Simon Glass47a0fd32017-05-18 20:09:04 -0600662
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600663/**
664 * dev_read_enabled() - check whether a node is enabled
665 *
666 * This looks for a 'status' property. If this exists, then returns 1 if
667 * the status is 'ok' and 0 otherwise. If there is no status property,
668 * it returns 1 on the assumption that anything mentioned should be enabled
669 * by default.
670 *
671 * @dev: device to examine
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100672 * Return: integer value 0 (not enabled) or 1 (enabled)
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600673 */
Simon Glass88b3a372020-01-27 08:49:40 -0700674int dev_read_enabled(const struct udevice *dev);
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600675
Simon Glassdcf98852017-07-25 08:29:55 -0600676/**
677 * dev_read_resource() - obtain an indexed resource from a device.
678 *
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900679 * @dev: device to examine
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100680 * @index: index of the resource to retrieve (0 = first)
681 * @res: returns the resource
682 * Return: 0 if ok, negative on error
Simon Glassdcf98852017-07-25 08:29:55 -0600683 */
Simon Glass88b3a372020-01-27 08:49:40 -0700684int dev_read_resource(const struct udevice *dev, uint index,
685 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -0600686
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900687/**
688 * dev_read_resource_byname() - obtain a named resource from a device.
689 *
690 * @dev: device to examine
691 * @name: name of the resource to retrieve
692 * @res: returns the resource
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100693 * Return: 0 if ok, negative on error
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900694 */
Simon Glass88b3a372020-01-27 08:49:40 -0700695int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900696 struct resource *res);
697
Mario Six147c6072018-01-15 11:07:19 +0100698/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200699 * dev_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +0100700 *
701 * Translate an address from the device-tree into a CPU physical address. This
702 * function walks up the tree and applies the various bus mappings along the
703 * way.
704 *
705 * @dev: device giving the context in which to translate the address
706 * @in_addr: pointer to the address to translate
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100707 * Return: the translated address; OF_BAD_ADDR on error
Mario Six147c6072018-01-15 11:07:19 +0100708 */
Simon Glass88b3a372020-01-27 08:49:40 -0700709u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
Michal Simek83e4c7e2019-01-31 16:30:59 +0100710
711/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200712 * dev_translate_dma_address() - Translate a device-tree DMA address
713 *
714 * Translate a DMA address from the device-tree into a CPU physical address.
715 * This function walks up the tree and applies the various bus mappings along
716 * the way.
717 *
718 * @dev: device giving the context in which to translate the DMA address
719 * @in_addr: pointer to the DMA address to translate
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100720 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne641067f2019-05-31 15:11:30 +0200721 */
Simon Glass88b3a372020-01-27 08:49:40 -0700722u64 dev_translate_dma_address(const struct udevice *dev,
723 const fdt32_t *in_addr);
Fabien Dessenne641067f2019-05-31 15:11:30 +0200724
725/**
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +0100726 * dev_get_dma_range() - Get a device's DMA constraints
727 *
728 * Provide the address bases and size of the linear mapping between the CPU and
729 * a device's BUS address space.
730 *
731 * @dev: device giving the context in which to translate the DMA address
732 * @cpu: base address for CPU's view of memory
733 * @bus: base address for BUS's view of memory
734 * @size: size of the address space
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100735 * Return: 0 if ok, negative on error
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +0100736 */
737int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
738 dma_addr_t *bus, u64 *size);
739
740/**
Michal Simek83e4c7e2019-01-31 16:30:59 +0100741 * dev_read_alias_highest_id - Get highest alias id for the given stem
742 * @stem: Alias stem to be examined
743 *
744 * The function travels the lookup table to get the highest alias id for the
745 * given alias stem.
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100746 * Return: alias ID, if found, else -1
Michal Simek83e4c7e2019-01-31 16:30:59 +0100747 */
748int dev_read_alias_highest_id(const char *stem);
749
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200750/**
751 * dev_get_child_count() - get the child count of a device
752 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100753 * @dev: device to use for interation (`struct udevice *`)
754 * Return: the count of child subnode
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200755 */
756int dev_get_child_count(const struct udevice *dev);
757
Stefan Roese68f81b82020-08-05 13:56:11 +0200758/**
759 * dev_read_pci_bus_range - Read PCI bus-range resource
760 *
761 * Look at the bus range property of a device node and return the pci bus
762 * range for this node.
763 *
764 * @dev: device to examine
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100765 * @res: returns the resource
766 * Return: 0 if ok, negative on error
Stefan Roese68f81b82020-08-05 13:56:11 +0200767 */
768int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
769
Dario Binacchi15daa482020-12-30 00:16:26 +0100770/**
771 * dev_decode_display_timing() - decode display timings
772 *
773 * Decode display timings from the supplied 'display-timings' node.
774 * See doc/device-tree-bindings/video/display-timing.txt for binding
775 * information.
776 *
777 * @dev: device to read DT display timings from. The node linked to the device
778 * contains a child node called 'display-timings' which in turn contains
779 * one or more display timing nodes.
780 * @index: index number to read (0=first timing subnode)
781 * @config: place to put timings
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100782 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Dario Binacchi15daa482020-12-30 00:16:26 +0100783 */
784int dev_decode_display_timing(const struct udevice *dev, int index,
785 struct display_timing *config);
786
Marek BehĂșnf3dd2132022-04-07 00:32:57 +0200787/**
788 * dev_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
789 *
790 * This function parses PHY handle from the Ethernet controller's ofnode
791 * (trying all possible PHY handle property names), and returns the PHY ofnode.
792 *
793 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
794 * if the result to that is true, this function should not be called.
795 *
796 * @dev: device representing the MAC
797 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
798 */
799ofnode dev_get_phy_node(const struct udevice *dev);
800
Marek BehĂșn123ca112022-04-07 00:33:01 +0200801/**
802 * dev_read_phy_mode() - Read PHY connection type from a MAC
803 *
804 * This function parses the "phy-mode" / "phy-connection-type" property and
805 * returns the corresponding PHY interface type.
806 *
807 * @dev: device representing the MAC
Marek BehĂșnffb0f6f2022-04-07 00:33:03 +0200808 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
Marek BehĂșn123ca112022-04-07 00:33:01 +0200809 * error
810 */
811phy_interface_t dev_read_phy_mode(const struct udevice *dev);
812
Simon Glass47a0fd32017-05-18 20:09:04 -0600813#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
Simon Glass401d1c42020-10-30 21:38:53 -0600814#include <asm/global_data.h>
Simon Glass47a0fd32017-05-18 20:09:04 -0600815
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +0200816static inline int dev_read_u8(const struct udevice *dev,
817 const char *propname, u8 *outp)
818{
819 return ofnode_read_u8(dev_ofnode(dev), propname, outp);
820}
821
822static inline int dev_read_u8_default(const struct udevice *dev,
823 const char *propname, u8 def)
824{
825 return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
826}
827
828static inline int dev_read_u16(const struct udevice *dev,
829 const char *propname, u16 *outp)
830{
831 return ofnode_read_u16(dev_ofnode(dev), propname, outp);
832}
833
834static inline int dev_read_u16_default(const struct udevice *dev,
835 const char *propname, u16 def)
836{
837 return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
838}
839
Simon Glass88b3a372020-01-27 08:49:40 -0700840static inline int dev_read_u32(const struct udevice *dev,
Masahiro Yamada3ab48f62017-12-30 02:00:05 +0900841 const char *propname, u32 *outp)
842{
843 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
844}
845
Simon Glass88b3a372020-01-27 08:49:40 -0700846static inline int dev_read_u32_default(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600847 const char *propname, int def)
848{
849 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
850}
851
Dario Binacchi4bb70752020-03-29 18:04:41 +0200852static inline int dev_read_u32_index(struct udevice *dev,
853 const char *propname, int index, u32 *outp)
854{
855 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
856}
857
858static inline u32 dev_read_u32_index_default(struct udevice *dev,
859 const char *propname, int index,
860 u32 def)
861{
862 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
863 def);
864}
865
Simon Glass88b3a372020-01-27 08:49:40 -0700866static inline int dev_read_s32(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700867 const char *propname, s32 *outp)
868{
869 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
870}
871
Simon Glass88b3a372020-01-27 08:49:40 -0700872static inline int dev_read_s32_default(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700873 const char *propname, int def)
874{
875 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
876}
877
Simon Glass88b3a372020-01-27 08:49:40 -0700878static inline int dev_read_u32u(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700879 const char *propname, uint *outp)
880{
881 u32 val;
882 int ret;
883
884 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
885 if (ret)
886 return ret;
887 *outp = val;
888
889 return 0;
890}
891
Simon Glass88b3a372020-01-27 08:49:40 -0700892static inline int dev_read_u64(const struct udevice *dev,
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200893 const char *propname, u64 *outp)
894{
895 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
896}
897
Simon Glass88b3a372020-01-27 08:49:40 -0700898static inline u64 dev_read_u64_default(const struct udevice *dev,
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200899 const char *propname, u64 def)
900{
901 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
902}
903
Simon Glass88b3a372020-01-27 08:49:40 -0700904static inline const char *dev_read_string(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600905 const char *propname)
906{
907 return ofnode_read_string(dev_ofnode(dev), propname);
908}
909
Simon Glass88b3a372020-01-27 08:49:40 -0700910static inline bool dev_read_bool(const struct udevice *dev,
911 const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -0600912{
913 return ofnode_read_bool(dev_ofnode(dev), propname);
914}
915
Simon Glass88b3a372020-01-27 08:49:40 -0700916static inline ofnode dev_read_subnode(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600917 const char *subbnode_name)
918{
919 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
920}
921
Simon Glass88b3a372020-01-27 08:49:40 -0700922static inline int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -0600923{
924 return ofnode_read_size(dev_ofnode(dev), propname);
925}
926
Simon Glass88b3a372020-01-27 08:49:40 -0700927static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
928 int index)
Simon Glass47a0fd32017-05-18 20:09:04 -0600929{
930 return devfdt_get_addr_index(dev, index);
931}
932
Bin Mengbdce9032021-09-12 11:15:13 +0800933static inline void *dev_read_addr_index_ptr(const struct udevice *dev,
934 int index)
935{
936 return devfdt_get_addr_index_ptr(dev, index);
937}
938
Simon Glass88b3a372020-01-27 08:49:40 -0700939static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
Sekhar Norif5b90472019-08-01 19:12:56 +0530940 int index,
941 fdt_size_t *size)
942{
943 return devfdt_get_addr_size_index(dev, index, size);
944}
945
Simon Glass88b3a372020-01-27 08:49:40 -0700946static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100947 const char *name)
948{
949 return devfdt_get_addr_name(dev, name);
950}
951
Simon Glass88b3a372020-01-27 08:49:40 -0700952static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
Sekhar Norif5b90472019-08-01 19:12:56 +0530953 const char *name,
954 fdt_size_t *size)
955{
956 return devfdt_get_addr_size_name(dev, name, size);
957}
958
Simon Glass88b3a372020-01-27 08:49:40 -0700959static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600960{
961 return devfdt_get_addr(dev);
962}
963
Simon Glass88b3a372020-01-27 08:49:40 -0700964static inline void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200965{
Ovidiu Panait3fe69d32020-08-03 22:17:35 +0300966 return devfdt_get_addr_ptr(dev);
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200967}
968
Simon Glass88b3a372020-01-27 08:49:40 -0700969static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass33c215a2019-09-15 12:08:58 -0600970{
971 return devfdt_get_addr_pci(dev);
972}
973
Simon Glass88b3a372020-01-27 08:49:40 -0700974static inline void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200975{
976 return devfdt_remap_addr(dev);
977}
978
Simon Glass88b3a372020-01-27 08:49:40 -0700979static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200980{
981 return devfdt_remap_addr_index(dev, index);
982}
983
Simon Glass88b3a372020-01-27 08:49:40 -0700984static inline void *dev_remap_addr_name(const struct udevice *dev,
985 const char *name)
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100986{
987 return devfdt_remap_addr_name(dev, name);
988}
989
Simon Glass88b3a372020-01-27 08:49:40 -0700990static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600991 const char *propname,
992 fdt_size_t *sizep)
993{
994 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
995}
996
Simon Glass88b3a372020-01-27 08:49:40 -0700997static inline const char *dev_read_name(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600998{
999 return ofnode_get_name(dev_ofnode(dev));
1000}
1001
Simon Glass88b3a372020-01-27 08:49:40 -07001002static inline int dev_read_stringlist_search(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -06001003 const char *propname,
1004 const char *string)
1005{
1006 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
1007}
1008
Simon Glass88b3a372020-01-27 08:49:40 -07001009static inline int dev_read_string_index(const struct udevice *dev,
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +02001010 const char *propname, int index,
1011 const char **outp)
1012{
1013 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
1014}
1015
Simon Glass88b3a372020-01-27 08:49:40 -07001016static inline int dev_read_string_count(const struct udevice *dev,
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +02001017 const char *propname)
1018{
1019 return ofnode_read_string_count(dev_ofnode(dev), propname);
1020}
1021
Simon Glass075bfc92021-10-23 17:26:07 -06001022static inline int dev_read_string_list(const struct udevice *dev,
1023 const char *propname,
1024 const char ***listp)
1025{
1026 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
1027}
1028
Simon Glass88b3a372020-01-27 08:49:40 -07001029static inline int dev_read_phandle_with_args(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -06001030 const char *list_name, const char *cells_name, int cell_count,
1031 int index, struct ofnode_phandle_args *out_args)
1032{
1033 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
1034 cells_name, cell_count, index,
1035 out_args);
1036}
1037
Simon Glass88b3a372020-01-27 08:49:40 -07001038static inline int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunay89f68302020-09-25 09:41:14 +02001039 const char *list_name, const char *cells_name, int cell_count)
Patrice Chotard642346a2017-07-18 11:57:08 +02001040{
1041 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +02001042 cells_name, cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +02001043}
1044
Simon Glass88b3a372020-01-27 08:49:40 -07001045static inline int dev_read_addr_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001046{
Heinrich Schuchardtae6b33d2020-07-25 21:38:49 +02001047 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1048
1049 return fdt_address_cells(gd->fdt_blob, parent);
Simon Glass47a0fd32017-05-18 20:09:04 -06001050}
1051
Simon Glass88b3a372020-01-27 08:49:40 -07001052static inline int dev_read_size_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001053{
Heinrich Schuchardtae6b33d2020-07-25 21:38:49 +02001054 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1055
1056 return fdt_size_cells(gd->fdt_blob, parent);
Simon Glass878d68c2017-06-12 06:21:31 -06001057}
1058
Simon Glass88b3a372020-01-27 08:49:40 -07001059static inline int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -06001060{
1061 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
1062}
1063
Simon Glass88b3a372020-01-27 08:49:40 -07001064static inline int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -06001065{
Simon Glass47a0fd32017-05-18 20:09:04 -06001066 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
1067}
1068
Simon Glass88b3a372020-01-27 08:49:40 -07001069static inline int dev_read_phandle(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001070{
1071 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
1072}
1073
Simon Glass88b3a372020-01-27 08:49:40 -07001074static inline const void *dev_read_prop(const struct udevice *dev,
Masahiro Yamadafd736212017-07-17 12:18:39 +09001075 const char *propname, int *lenp)
Simon Glass47a0fd32017-05-18 20:09:04 -06001076{
Masahiro Yamada61e51ba2017-06-22 16:54:05 +09001077 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glass47a0fd32017-05-18 20:09:04 -06001078}
1079
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001080static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
1081{
1082 return ofnode_get_first_property(dev_ofnode(dev), prop);
1083}
1084
1085static inline int dev_read_next_prop(struct ofprop *prop)
1086{
1087 return ofnode_get_next_property(prop);
1088}
1089
1090static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
1091 const char **propname,
1092 int *lenp)
1093{
1094 return ofnode_get_property_by_prop(prop, propname, lenp);
1095}
1096
Simon Glass88b3a372020-01-27 08:49:40 -07001097static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glass47a0fd32017-05-18 20:09:04 -06001098{
Marcel Ziswiler45224e82019-05-20 02:44:57 +02001099#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass47a0fd32017-05-18 20:09:04 -06001100 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
1101 dev_of_offset(dev), devnump);
Marcel Ziswiler45224e82019-05-20 02:44:57 +02001102#else
1103 return -ENOTSUPP;
1104#endif
Simon Glass47a0fd32017-05-18 20:09:04 -06001105}
1106
Simon Glass88b3a372020-01-27 08:49:40 -07001107static inline int dev_read_u32_array(const struct udevice *dev,
1108 const char *propname, u32 *out_values,
1109 size_t sz)
Simon Glass47a0fd32017-05-18 20:09:04 -06001110{
1111 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
1112}
1113
Simon Glass88b3a372020-01-27 08:49:40 -07001114static inline ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001115{
1116 return ofnode_first_subnode(dev_ofnode(dev));
1117}
1118
1119static inline ofnode dev_read_next_subnode(ofnode node)
1120{
1121 return ofnode_next_subnode(node);
1122}
1123
Simon Glass88b3a372020-01-27 08:49:40 -07001124static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
Simon Glassf262d4c2020-01-27 08:49:47 -07001125 const char *propname,
1126 size_t sz)
Simon Glassf11c7ab2017-05-18 20:09:03 -06001127{
1128 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
1129}
1130
Simon Glass88b3a372020-01-27 08:49:40 -07001131static inline int dev_read_enabled(const struct udevice *dev)
Simon Glassf7d6fcf2017-06-12 06:21:30 -06001132{
1133 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
1134}
1135
Simon Glass88b3a372020-01-27 08:49:40 -07001136static inline int dev_read_resource(const struct udevice *dev, uint index,
Simon Glassdcf98852017-07-25 08:29:55 -06001137 struct resource *res)
1138{
1139 return ofnode_read_resource(dev_ofnode(dev), index, res);
1140}
1141
Simon Glass88b3a372020-01-27 08:49:40 -07001142static inline int dev_read_resource_byname(const struct udevice *dev,
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +09001143 const char *name,
1144 struct resource *res)
1145{
1146 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
1147}
1148
Simon Glass88b3a372020-01-27 08:49:40 -07001149static inline u64 dev_translate_address(const struct udevice *dev,
1150 const fdt32_t *in_addr)
Mario Six147c6072018-01-15 11:07:19 +01001151{
1152 return ofnode_translate_address(dev_ofnode(dev), in_addr);
1153}
1154
Simon Glass88b3a372020-01-27 08:49:40 -07001155static inline u64 dev_translate_dma_address(const struct udevice *dev,
1156 const fdt32_t *in_addr)
Fabien Dessenne641067f2019-05-31 15:11:30 +02001157{
1158 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
1159}
1160
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001161static inline int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
1162 dma_addr_t *bus, u64 *size)
1163{
1164 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
1165}
1166
Michal Simek83e4c7e2019-01-31 16:30:59 +01001167static inline int dev_read_alias_highest_id(const char *stem)
1168{
Stanislav Pinchuka00e0f72021-01-20 21:52:23 +03001169 if (!CONFIG_IS_ENABLED(OF_LIBFDT) || !gd->fdt_blob)
Michael Walle0a6b75f2020-06-02 01:47:08 +02001170 return -1;
Michal Simek83e4c7e2019-01-31 16:30:59 +01001171 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
1172}
1173
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001174static inline int dev_get_child_count(const struct udevice *dev)
1175{
1176 return ofnode_get_child_count(dev_ofnode(dev));
1177}
1178
Dario Binacchi15daa482020-12-30 00:16:26 +01001179static inline int dev_decode_display_timing(const struct udevice *dev,
1180 int index,
1181 struct display_timing *config)
1182{
1183 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
1184}
1185
Marek BehĂșnf3dd2132022-04-07 00:32:57 +02001186static inline ofnode dev_get_phy_node(const struct udevice *dev)
1187{
1188 return ofnode_get_phy_node(dev_ofnode(dev));
1189}
1190
Marek BehĂșn123ca112022-04-07 00:33:01 +02001191static inline phy_interface_t dev_read_phy_mode(const struct udevice *dev)
1192{
1193 return ofnode_read_phy_mode(dev_ofnode(dev));
1194}
1195
Simon Glassf11c7ab2017-05-18 20:09:03 -06001196#endif /* CONFIG_DM_DEV_READ_INLINE */
1197
1198/**
1199 * dev_for_each_subnode() - Helper function to iterate through subnodes
1200 *
1201 * This creates a for() loop which works through the subnodes in a device's
1202 * device-tree node.
1203 *
1204 * @subnode: ofnode holding the current subnode
Patrick Delaunay6de6a612022-01-12 10:53:48 +01001205 * @dev: device to use for interation (`struct udevice *`)
Simon Glassf11c7ab2017-05-18 20:09:03 -06001206 */
1207#define dev_for_each_subnode(subnode, dev) \
1208 for (subnode = dev_read_first_subnode(dev); \
1209 ofnode_valid(subnode); \
1210 subnode = ofnode_next_subnode(subnode))
1211
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001212/**
1213 * dev_for_each_property() - Helper function to iterate through property
1214 *
1215 * This creates a for() loop which works through the property in a device's
1216 * device-tree node.
1217 *
1218 * @prop: struct ofprop holding the current property
Patrick Delaunay6de6a612022-01-12 10:53:48 +01001219 * @dev: device to use for interation (`struct udevice *`)
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001220 */
1221#define dev_for_each_property(prop, dev) \
1222 for (int ret_prop = dev_read_first_prop(dev, &prop); \
1223 !ret_prop; \
1224 ret_prop = dev_read_next_prop(&prop))
1225
Simon Glassf11c7ab2017-05-18 20:09:03 -06001226#endif