xref: /rk3399_rockchip-uboot/include/dm/read.h (revision 3cf4841077bc170e305243e4ba2aac4420b9fb55)
1f11c7ab9SSimon Glass /*
2f11c7ab9SSimon Glass  * Function to read values from the device tree node attached to a udevice.
3f11c7ab9SSimon Glass  *
4f11c7ab9SSimon Glass  * Copyright (c) 2017 Google, Inc
5f11c7ab9SSimon Glass  * Written by Simon Glass <sjg@chromium.org>
6f11c7ab9SSimon Glass  *
7f11c7ab9SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
8f11c7ab9SSimon Glass  */
9f11c7ab9SSimon Glass 
10f11c7ab9SSimon Glass #ifndef _DM_READ_H
11f11c7ab9SSimon Glass #define _DM_READ_H
12f11c7ab9SSimon Glass 
13f11c7ab9SSimon Glass #include <dm/fdtaddr.h>
14f11c7ab9SSimon Glass #include <dm/ofnode.h>
15f11c7ab9SSimon Glass #include <dm/uclass.h>
16f11c7ab9SSimon Glass 
17a4481012SSimon Glass struct resource;
18a4481012SSimon Glass 
19f11c7ab9SSimon Glass #if CONFIG_IS_ENABLED(OF_LIVE)
20f11c7ab9SSimon Glass static inline const struct device_node *dev_np(struct udevice *dev)
21f11c7ab9SSimon Glass {
22f11c7ab9SSimon Glass 	return ofnode_to_np(dev->node);
23f11c7ab9SSimon Glass }
24f11c7ab9SSimon Glass #else
25f11c7ab9SSimon Glass static inline const struct device_node *dev_np(struct udevice *dev)
26f11c7ab9SSimon Glass {
27f11c7ab9SSimon Glass 	return NULL;
28f11c7ab9SSimon Glass }
29f11c7ab9SSimon Glass #endif
30f11c7ab9SSimon Glass 
31f11c7ab9SSimon Glass /**
32f11c7ab9SSimon Glass  * dev_ofnode() - get the DT node reference associated with a udevice
33f11c7ab9SSimon Glass  *
34f11c7ab9SSimon Glass  * @dev:	device to check
35f11c7ab9SSimon Glass  * @return reference of the the device's DT node
36f11c7ab9SSimon Glass  */
37f11c7ab9SSimon Glass static inline ofnode dev_ofnode(struct udevice *dev)
38f11c7ab9SSimon Glass {
39f11c7ab9SSimon Glass 	return dev->node;
40f11c7ab9SSimon Glass }
41f11c7ab9SSimon Glass 
42f11c7ab9SSimon Glass static inline bool dev_of_valid(struct udevice *dev)
43f11c7ab9SSimon Glass {
44f11c7ab9SSimon Glass 	return ofnode_valid(dev_ofnode(dev));
45f11c7ab9SSimon Glass }
46f11c7ab9SSimon Glass 
4747a0fd3bSSimon Glass #ifndef CONFIG_DM_DEV_READ_INLINE
4847a0fd3bSSimon Glass /**
4947a0fd3bSSimon Glass  * dev_read_u32_default() - read a 32-bit integer from a device's DT property
5047a0fd3bSSimon Glass  *
5147a0fd3bSSimon Glass  * @dev:	device to read DT property from
5247a0fd3bSSimon Glass  * @propname:	name of the property to read from
5347a0fd3bSSimon Glass  * @def:	default value to return if the property has no value
5447a0fd3bSSimon Glass  * @return property value, or @def if not found
5547a0fd3bSSimon Glass  */
5647a0fd3bSSimon Glass int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
57f11c7ab9SSimon Glass 
58f11c7ab9SSimon Glass /**
59270d4d86SJoseph Chen  * dev_read_s32_default() - read a signed 32-bit integer from a device's DT property
60270d4d86SJoseph Chen  *
61270d4d86SJoseph Chen  * @dev:	device to read DT property from
62270d4d86SJoseph Chen  * @propname:	name of the property to read from
63270d4d86SJoseph Chen  * @def:	default value to return if the property has no value
64270d4d86SJoseph Chen  * @return property value, or @def if not found
65270d4d86SJoseph Chen  */
66270d4d86SJoseph Chen int dev_read_s32_default(struct udevice *dev, const char *propname, int def);
67270d4d86SJoseph Chen 
68270d4d86SJoseph Chen /**
69f11c7ab9SSimon Glass  * dev_read_string() - Read a string from a device's DT property
70f11c7ab9SSimon Glass  *
71f11c7ab9SSimon Glass  * @dev:	device to read DT property from
72f11c7ab9SSimon Glass  * @propname:	name of the property to read
73f11c7ab9SSimon Glass  * @return string from property value, or NULL if there is no such property
74f11c7ab9SSimon Glass  */
7547a0fd3bSSimon Glass const char *dev_read_string(struct udevice *dev, const char *propname);
76f11c7ab9SSimon Glass 
77f11c7ab9SSimon Glass /**
78f11c7ab9SSimon Glass  * dev_read_bool() - read a boolean value from a device's DT property
79f11c7ab9SSimon Glass  *
80f11c7ab9SSimon Glass  * @dev:	device to read DT property from
81f11c7ab9SSimon Glass  * @propname:	name of property to read
82f11c7ab9SSimon Glass  * @return true if property is present (meaning true), false if not present
83f11c7ab9SSimon Glass  */
8447a0fd3bSSimon Glass bool dev_read_bool(struct udevice *dev, const char *propname);
85f11c7ab9SSimon Glass 
86f11c7ab9SSimon Glass /**
87f11c7ab9SSimon Glass  * dev_read_subnode() - find a named subnode of a device
88f11c7ab9SSimon Glass  *
89f11c7ab9SSimon Glass  * @dev:	device whose DT node contains the subnode
90f11c7ab9SSimon Glass  * @subnode_name: name of subnode to find
91f11c7ab9SSimon Glass  * @return reference to subnode (which can be invalid if there is no such
92f11c7ab9SSimon Glass  * subnode)
93f11c7ab9SSimon Glass  */
9447a0fd3bSSimon Glass ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
95f11c7ab9SSimon Glass 
96f11c7ab9SSimon Glass /**
97f11c7ab9SSimon Glass  * dev_read_size() - read the size of a property
98f11c7ab9SSimon Glass  *
99f11c7ab9SSimon Glass  * @dev: device to check
100f11c7ab9SSimon Glass  * @propname: property to check
101f11c7ab9SSimon Glass  * @return size of property if present, or -EINVAL if not
102f11c7ab9SSimon Glass  */
10347a0fd3bSSimon Glass int dev_read_size(struct udevice *dev, const char *propname);
104f11c7ab9SSimon Glass 
105f11c7ab9SSimon Glass /**
106f11c7ab9SSimon Glass  * dev_read_addr_index() - Get the indexed reg property of a device
107f11c7ab9SSimon Glass  *
108f11c7ab9SSimon Glass  * @dev: Device to read from
109f11c7ab9SSimon Glass  * @index: the 'reg' property can hold a list of <addr, size> pairs
110f11c7ab9SSimon Glass  *	   and @index is used to select which one is required
111f11c7ab9SSimon Glass  *
112f11c7ab9SSimon Glass  * @return address or FDT_ADDR_T_NONE if not found
113f11c7ab9SSimon Glass  */
11447a0fd3bSSimon Glass fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
115f11c7ab9SSimon Glass 
116f11c7ab9SSimon Glass /**
117f11c7ab9SSimon Glass  * dev_read_addr() - Get the reg property of a device
118f11c7ab9SSimon Glass  *
119f11c7ab9SSimon Glass  * @dev: Device to read from
120f11c7ab9SSimon Glass  *
121f11c7ab9SSimon Glass  * @return address or FDT_ADDR_T_NONE if not found
122f11c7ab9SSimon Glass  */
12347a0fd3bSSimon Glass fdt_addr_t dev_read_addr(struct udevice *dev);
124f11c7ab9SSimon Glass 
125f11c7ab9SSimon Glass /**
12618a0c4a6SPhilipp Tomsich  * dev_read_addr_ptr() - Get the reg property of a device
12718a0c4a6SPhilipp Tomsich  *                       as a pointer
12818a0c4a6SPhilipp Tomsich  *
12918a0c4a6SPhilipp Tomsich  * @dev: Device to read from
13018a0c4a6SPhilipp Tomsich  *
13118a0c4a6SPhilipp Tomsich  * @return pointer or NULL if not found
13218a0c4a6SPhilipp Tomsich  */
13318a0c4a6SPhilipp Tomsich void *dev_read_addr_ptr(struct udevice *dev);
13418a0c4a6SPhilipp Tomsich 
13518a0c4a6SPhilipp Tomsich /**
136f11c7ab9SSimon Glass  * dev_read_addr_size() - get address and size from a device property
137f11c7ab9SSimon Glass  *
138f11c7ab9SSimon Glass  * This does no address translation. It simply reads an property that contains
139f11c7ab9SSimon Glass  * an address and a size value, one after the other.
140f11c7ab9SSimon Glass  *
141f11c7ab9SSimon Glass  * @dev: Device to read from
142f11c7ab9SSimon Glass  * @propname: property to read
143f11c7ab9SSimon Glass  * @sizep: place to put size value (on success)
144f11c7ab9SSimon Glass  * @return address value, or FDT_ADDR_T_NONE on error
145f11c7ab9SSimon Glass  */
14647a0fd3bSSimon Glass fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
14747a0fd3bSSimon Glass 				fdt_size_t *sizep);
148f11c7ab9SSimon Glass 
149f11c7ab9SSimon Glass /**
150f11c7ab9SSimon Glass  * dev_read_name() - get the name of a device's node
151f11c7ab9SSimon Glass  *
152f11c7ab9SSimon Glass  * @node: valid node to look up
153f11c7ab9SSimon Glass  * @return name of node
154f11c7ab9SSimon Glass  */
15547a0fd3bSSimon Glass const char *dev_read_name(struct udevice *dev);
156f11c7ab9SSimon Glass 
157f11c7ab9SSimon Glass /**
158f11c7ab9SSimon Glass  * dev_read_stringlist_search() - find string in a string list and return index
159f11c7ab9SSimon Glass  *
160f11c7ab9SSimon Glass  * Note that it is possible for this function to succeed on property values
161f11c7ab9SSimon Glass  * that are not NUL-terminated. That's because the function will stop after
162f11c7ab9SSimon Glass  * finding the first occurrence of @string. This can for example happen with
163f11c7ab9SSimon Glass  * small-valued cell properties, such as #address-cells, when searching for
164f11c7ab9SSimon Glass  * the empty string.
165f11c7ab9SSimon Glass  *
166f11c7ab9SSimon Glass  * @dev: device to check
167f11c7ab9SSimon Glass  * @propname: name of the property containing the string list
168f11c7ab9SSimon Glass  * @string: string to look up in the string list
169f11c7ab9SSimon Glass  *
170f11c7ab9SSimon Glass  * @return:
171f11c7ab9SSimon Glass  *   the index of the string in the list of strings
172f11c7ab9SSimon Glass  *   -ENODATA if the property is not found
173f11c7ab9SSimon Glass  *   -EINVAL on some other error
174f11c7ab9SSimon Glass  */
17547a0fd3bSSimon Glass int dev_read_stringlist_search(struct udevice *dev, const char *property,
17647a0fd3bSSimon Glass 			  const char *string);
177f11c7ab9SSimon Glass 
178f11c7ab9SSimon Glass /**
17957a9c706SJean-Jacques Hiblot  * dev_read_string_index() - obtain an indexed string from a string list
18057a9c706SJean-Jacques Hiblot  *
18157a9c706SJean-Jacques Hiblot  * @dev: device to examine
18257a9c706SJean-Jacques Hiblot  * @propname: name of the property containing the string list
18357a9c706SJean-Jacques Hiblot  * @index: index of the string to return
18457a9c706SJean-Jacques Hiblot  * @out: return location for the string
18557a9c706SJean-Jacques Hiblot  *
18657a9c706SJean-Jacques Hiblot  * @return:
18757a9c706SJean-Jacques Hiblot  *   length of string, if found or -ve error value if not found
18857a9c706SJean-Jacques Hiblot  */
18957a9c706SJean-Jacques Hiblot int dev_read_string_index(struct udevice *dev, const char *propname, int index,
19057a9c706SJean-Jacques Hiblot 			  const char **outp);
19157a9c706SJean-Jacques Hiblot 
19257a9c706SJean-Jacques Hiblot /**
19357a9c706SJean-Jacques Hiblot  * dev_read_string_count() - find the number of strings in a string list
19457a9c706SJean-Jacques Hiblot  *
19557a9c706SJean-Jacques Hiblot  * @dev: device to examine
19657a9c706SJean-Jacques Hiblot  * @propname: name of the property containing the string list
19757a9c706SJean-Jacques Hiblot  * @return:
19857a9c706SJean-Jacques Hiblot  *   number of strings in the list, or -ve error value if not found
19957a9c706SJean-Jacques Hiblot  */
20057a9c706SJean-Jacques Hiblot int dev_read_string_count(struct udevice *dev, const char *propname);
20157a9c706SJean-Jacques Hiblot /**
202f11c7ab9SSimon Glass  * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
203f11c7ab9SSimon Glass  *
204f11c7ab9SSimon Glass  * This function is useful to parse lists of phandles and their arguments.
205f11c7ab9SSimon Glass  * Returns 0 on success and fills out_args, on error returns appropriate
206f11c7ab9SSimon Glass  * errno value.
207f11c7ab9SSimon Glass  *
208f11c7ab9SSimon Glass  * Caller is responsible to call of_node_put() on the returned out_args->np
209f11c7ab9SSimon Glass  * pointer.
210f11c7ab9SSimon Glass  *
211f11c7ab9SSimon Glass  * Example:
212f11c7ab9SSimon Glass  *
213f11c7ab9SSimon Glass  * phandle1: node1 {
214f11c7ab9SSimon Glass  *	#list-cells = <2>;
215f11c7ab9SSimon Glass  * }
216f11c7ab9SSimon Glass  *
217f11c7ab9SSimon Glass  * phandle2: node2 {
218f11c7ab9SSimon Glass  *	#list-cells = <1>;
219f11c7ab9SSimon Glass  * }
220f11c7ab9SSimon Glass  *
221f11c7ab9SSimon Glass  * node3 {
222f11c7ab9SSimon Glass  *	list = <&phandle1 1 2 &phandle2 3>;
223f11c7ab9SSimon Glass  * }
224f11c7ab9SSimon Glass  *
225f11c7ab9SSimon Glass  * To get a device_node of the `node2' node you may call this:
226f11c7ab9SSimon Glass  * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
227f11c7ab9SSimon Glass  *
228f11c7ab9SSimon Glass  * @dev:	device whose node containing a list
229f11c7ab9SSimon Glass  * @list_name:	property name that contains a list
230f11c7ab9SSimon Glass  * @cells_name:	property name that specifies phandles' arguments count
231f11c7ab9SSimon Glass  * @cells_count: Cell count to use if @cells_name is NULL
232f11c7ab9SSimon Glass  * @index:	index of a phandle to parse out
233f11c7ab9SSimon Glass  * @out_args:	optional pointer to output arguments structure (will be filled)
234f11c7ab9SSimon Glass  * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
235f11c7ab9SSimon Glass  *	@list_name does not exist, -EINVAL if a phandle was not found,
236f11c7ab9SSimon Glass  *	@cells_name could not be found, the arguments were truncated or there
237f11c7ab9SSimon Glass  *	were too many arguments.
238f11c7ab9SSimon Glass  */
23947a0fd3bSSimon Glass int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
24047a0fd3bSSimon Glass 				const char *cells_name, int cell_count,
24147a0fd3bSSimon Glass 				int index,
24247a0fd3bSSimon Glass 				struct ofnode_phandle_args *out_args);
243f11c7ab9SSimon Glass 
244f11c7ab9SSimon Glass /**
245642346aeSPatrice Chotard  * dev_count_phandle_with_args() - Return phandle number in a list
246642346aeSPatrice Chotard  *
247642346aeSPatrice Chotard  * This function is usefull to get phandle number contained in a property list.
248642346aeSPatrice Chotard  * For example, this allows to allocate the right amount of memory to keep
249642346aeSPatrice Chotard  * clock's reference contained into the "clocks" property.
250642346aeSPatrice Chotard  *
251642346aeSPatrice Chotard  *
252642346aeSPatrice Chotard  * @dev:	device whose node containing a list
253642346aeSPatrice Chotard  * @list_name:	property name that contains a list
254642346aeSPatrice Chotard  * @cells_name:	property name that specifies phandles' arguments count
255642346aeSPatrice Chotard  * @Returns number of phandle found on success, on error returns appropriate
256642346aeSPatrice Chotard  * errno value.
257642346aeSPatrice Chotard  */
258642346aeSPatrice Chotard 
259642346aeSPatrice Chotard int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
260642346aeSPatrice Chotard 				const char *cells_name);
261642346aeSPatrice Chotard 
262642346aeSPatrice Chotard /**
263f11c7ab9SSimon Glass  * dev_read_addr_cells() - Get the number of address cells for a device's node
264f11c7ab9SSimon Glass  *
265f11c7ab9SSimon Glass  * This walks back up the tree to find the closest #address-cells property
266f11c7ab9SSimon Glass  * which controls the given node.
267f11c7ab9SSimon Glass  *
268f11c7ab9SSimon Glass  * @dev: devioe to check
269f11c7ab9SSimon Glass  * @return number of address cells this node uses
270f11c7ab9SSimon Glass  */
27147a0fd3bSSimon Glass int dev_read_addr_cells(struct udevice *dev);
272f11c7ab9SSimon Glass 
273f11c7ab9SSimon Glass /**
274f11c7ab9SSimon Glass  * dev_read_size_cells() - Get the number of size cells for a device's node
275f11c7ab9SSimon Glass  *
276f11c7ab9SSimon Glass  * This walks back up the tree to find the closest #size-cells property
277f11c7ab9SSimon Glass  * which controls the given node.
278f11c7ab9SSimon Glass  *
279f11c7ab9SSimon Glass  * @dev: devioe to check
280f11c7ab9SSimon Glass  * @return number of size cells this node uses
281f11c7ab9SSimon Glass  */
28247a0fd3bSSimon Glass int dev_read_size_cells(struct udevice *dev);
283f11c7ab9SSimon Glass 
284f11c7ab9SSimon Glass /**
285878d68c0SSimon Glass  * dev_read_addr_cells() - Get the address cells property in a node
286878d68c0SSimon Glass  *
287878d68c0SSimon Glass  * This function matches fdt_address_cells().
288878d68c0SSimon Glass  *
289878d68c0SSimon Glass  * @dev: devioe to check
290878d68c0SSimon Glass  * @return number of address cells this node uses
291878d68c0SSimon Glass  */
292878d68c0SSimon Glass int dev_read_simple_addr_cells(struct udevice *dev);
293878d68c0SSimon Glass 
294878d68c0SSimon Glass /**
295878d68c0SSimon Glass  * dev_read_size_cells() - Get the size cells property in a node
296878d68c0SSimon Glass  *
297878d68c0SSimon Glass  * This function matches fdt_size_cells().
298878d68c0SSimon Glass  *
299878d68c0SSimon Glass  * @dev: devioe to check
300878d68c0SSimon Glass  * @return number of size cells this node uses
301878d68c0SSimon Glass  */
302878d68c0SSimon Glass int dev_read_simple_size_cells(struct udevice *dev);
303878d68c0SSimon Glass 
304878d68c0SSimon Glass /**
305f11c7ab9SSimon Glass  * dev_read_phandle() - Get the phandle from a device
306f11c7ab9SSimon Glass  *
307f11c7ab9SSimon Glass  * @dev: device to check
308f11c7ab9SSimon Glass  * @return phandle (1 or greater), or 0 if no phandle or other error
309f11c7ab9SSimon Glass  */
31047a0fd3bSSimon Glass int dev_read_phandle(struct udevice *dev);
311f11c7ab9SSimon Glass 
312f11c7ab9SSimon Glass /**
313f11c7ab9SSimon Glass  * dev_read_prop()- - read a property from a device's node
314f11c7ab9SSimon Glass  *
315f11c7ab9SSimon Glass  * @dev: device to check
316f11c7ab9SSimon Glass  * @propname: property to read
317f11c7ab9SSimon Glass  * @lenp: place to put length on success
318f11c7ab9SSimon Glass  * @return pointer to property, or NULL if not found
319f11c7ab9SSimon Glass  */
320fd73621cSMasahiro Yamada const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
321f11c7ab9SSimon Glass 
322f11c7ab9SSimon Glass /**
323*3cf48410SPatrick Delaunay  * dev_read_first_prop()- get the reference of the first property
324*3cf48410SPatrick Delaunay  *
325*3cf48410SPatrick Delaunay  * Get reference to the first property of the node, it is used to iterate
326*3cf48410SPatrick Delaunay  * and read all the property with dev_read_prop_by_prop().
327*3cf48410SPatrick Delaunay  *
328*3cf48410SPatrick Delaunay  * @dev: device to check
329*3cf48410SPatrick Delaunay  * @prop: place to put argument reference
330*3cf48410SPatrick Delaunay  * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
331*3cf48410SPatrick Delaunay  */
332*3cf48410SPatrick Delaunay int dev_read_first_prop(struct udevice *dev, struct ofprop *prop);
333*3cf48410SPatrick Delaunay 
334*3cf48410SPatrick Delaunay /**
335*3cf48410SPatrick Delaunay  * ofnode_get_next_property() - get the reference of the next property
336*3cf48410SPatrick Delaunay  *
337*3cf48410SPatrick Delaunay  * Get reference to the next property of the node, it is used to iterate
338*3cf48410SPatrick Delaunay  * and read all the property with dev_read_prop_by_prop().
339*3cf48410SPatrick Delaunay  *
340*3cf48410SPatrick Delaunay  * @prop: reference of current argument and place to put reference of next one
341*3cf48410SPatrick Delaunay  * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
342*3cf48410SPatrick Delaunay  */
343*3cf48410SPatrick Delaunay int dev_read_next_prop(struct ofprop *prop);
344*3cf48410SPatrick Delaunay 
345*3cf48410SPatrick Delaunay /**
346*3cf48410SPatrick Delaunay  * dev_read_prop_by_prop() - get a pointer to the value of a property
347*3cf48410SPatrick Delaunay  *
348*3cf48410SPatrick Delaunay  * Get value for the property identified by the provided reference.
349*3cf48410SPatrick Delaunay  *
350*3cf48410SPatrick Delaunay  * @prop: reference on property
351*3cf48410SPatrick Delaunay  * @propname: If non-NULL, place to property name on success,
352*3cf48410SPatrick Delaunay  * @lenp: If non-NULL, place to put length on success
353*3cf48410SPatrick Delaunay  * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
354*3cf48410SPatrick Delaunay  */
355*3cf48410SPatrick Delaunay const void *dev_read_prop_by_prop(struct ofprop *prop,
356*3cf48410SPatrick Delaunay 				  const char **propname, int *lenp);
357*3cf48410SPatrick Delaunay 
358*3cf48410SPatrick Delaunay /**
359f11c7ab9SSimon Glass  * dev_read_alias_seq() - Get the alias sequence number of a node
360f11c7ab9SSimon Glass  *
361f11c7ab9SSimon Glass  * This works out whether a node is pointed to by an alias, and if so, the
362f11c7ab9SSimon Glass  * sequence number of that alias. Aliases are of the form <base><num> where
363f11c7ab9SSimon Glass  * <num> is the sequence number. For example spi2 would be sequence number 2.
364f11c7ab9SSimon Glass  *
365f11c7ab9SSimon Glass  * @dev: device to look up
366f11c7ab9SSimon Glass  * @devnump: set to the sequence number if one is found
367f11c7ab9SSimon Glass  * @return 0 if a sequence was found, -ve if not
368f11c7ab9SSimon Glass  */
36947a0fd3bSSimon Glass int dev_read_alias_seq(struct udevice *dev, int *devnump);
370f11c7ab9SSimon Glass 
371f11c7ab9SSimon Glass /**
372f11c7ab9SSimon Glass  * dev_read_u32_array() - Find and read an array of 32 bit integers
373f11c7ab9SSimon Glass  *
374f11c7ab9SSimon Glass  * Search for a property in a device node and read 32-bit value(s) from
375f11c7ab9SSimon Glass  * it.
376f11c7ab9SSimon Glass  *
377f11c7ab9SSimon Glass  * The out_values is modified only if a valid u32 value can be decoded.
378f11c7ab9SSimon Glass  *
379f11c7ab9SSimon Glass  * @dev: device to look up
380f11c7ab9SSimon Glass  * @propname:	name of the property to read
381f11c7ab9SSimon Glass  * @out_values:	pointer to return value, modified only if return value is 0
382f11c7ab9SSimon Glass  * @sz:		number of array elements to read
383f11c7ab9SSimon Glass  * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
384f11c7ab9SSimon Glass  * property does not have a value, and -EOVERFLOW if the property data isn't
385f11c7ab9SSimon Glass  * large enough.
386f11c7ab9SSimon Glass  */
38747a0fd3bSSimon Glass int dev_read_u32_array(struct udevice *dev, const char *propname,
38847a0fd3bSSimon Glass 		       u32 *out_values, size_t sz);
389f11c7ab9SSimon Glass 
390f11c7ab9SSimon Glass /**
39104539b46SJoseph Chen  * dev_write_u32_array() - Find and write an array of 32 bit integers
39204539b46SJoseph Chen  *
39304539b46SJoseph Chen  * Search for a property in a device node and write 32-bit value(s) to
39404539b46SJoseph Chen  * it.
39504539b46SJoseph Chen  *
39604539b46SJoseph Chen  * The out_values is modified only if a valid u32 value can be decoded.
39704539b46SJoseph Chen  *
39804539b46SJoseph Chen  * @dev: device to look up
39904539b46SJoseph Chen  * @propname:	name of the property to read
40004539b46SJoseph Chen  * @values:	pointer to update value, modified only if return value is 0
40104539b46SJoseph Chen  * @sz:		number of array elements to read
40204539b46SJoseph Chen  * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
40304539b46SJoseph Chen  * property does not have a value, and -EOVERFLOW if the property data isn't
40404539b46SJoseph Chen  * large enough.
40504539b46SJoseph Chen  */
40604539b46SJoseph Chen int dev_write_u32_array(struct udevice *dev, const char *propname,
40704539b46SJoseph Chen 			u32 *values, size_t sz);
40804539b46SJoseph Chen 
40904539b46SJoseph Chen /**
410f11c7ab9SSimon Glass  * dev_read_first_subnode() - find the first subnode of a device's node
411f11c7ab9SSimon Glass  *
412f11c7ab9SSimon Glass  * @dev: device to look up
413f11c7ab9SSimon Glass  * @return reference to the first subnode (which can be invalid if the device's
414f11c7ab9SSimon Glass  * node has no subnodes)
415f11c7ab9SSimon Glass  */
41647a0fd3bSSimon Glass ofnode dev_read_first_subnode(struct udevice *dev);
417f11c7ab9SSimon Glass 
418f11c7ab9SSimon Glass /**
419f11c7ab9SSimon Glass  * ofnode_next_subnode() - find the next sibling of a subnode
420f11c7ab9SSimon Glass  *
421f11c7ab9SSimon Glass  * @node:	valid reference to previous node (sibling)
422f11c7ab9SSimon Glass  * @return reference to the next subnode (which can be invalid if the node
423f11c7ab9SSimon Glass  * has no more siblings)
424f11c7ab9SSimon Glass  */
42547a0fd3bSSimon Glass ofnode dev_read_next_subnode(ofnode node);
426f11c7ab9SSimon Glass 
427f11c7ab9SSimon Glass /**
428f11c7ab9SSimon Glass  * dev_read_u8_array_ptr() - find an 8-bit array
429f11c7ab9SSimon Glass  *
430f11c7ab9SSimon Glass  * Look up a device's node property and return a pointer to its contents as a
431f11c7ab9SSimon Glass  * byte array of given length. The property must have at least enough data
432f11c7ab9SSimon Glass  * for the array (count bytes). It may have more, but this will be ignored.
433f11c7ab9SSimon Glass  * The data is not copied.
434f11c7ab9SSimon Glass  *
435f11c7ab9SSimon Glass  * @dev: device to look up
436f11c7ab9SSimon Glass  * @propname: name of property to find
437f11c7ab9SSimon Glass  * @sz: number of array elements
438f11c7ab9SSimon Glass  * @return pointer to byte array if found, or NULL if the property is not
439f11c7ab9SSimon Glass  *		found or there is not enough data
440f11c7ab9SSimon Glass  */
44147a0fd3bSSimon Glass const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
44247a0fd3bSSimon Glass 				     size_t sz);
44347a0fd3bSSimon Glass 
444f7d6fcf7SSimon Glass /**
445f7d6fcf7SSimon Glass  * dev_read_enabled() - check whether a node is enabled
446f7d6fcf7SSimon Glass  *
447f7d6fcf7SSimon Glass  * This looks for a 'status' property. If this exists, then returns 1 if
448f7d6fcf7SSimon Glass  * the status is 'ok' and 0 otherwise. If there is no status property,
449f7d6fcf7SSimon Glass  * it returns 1 on the assumption that anything mentioned should be enabled
450f7d6fcf7SSimon Glass  * by default.
451f7d6fcf7SSimon Glass  *
452f7d6fcf7SSimon Glass  * @dev: device to examine
453f7d6fcf7SSimon Glass  * @return integer value 0 (not enabled) or 1 (enabled)
454f7d6fcf7SSimon Glass  */
455f7d6fcf7SSimon Glass int dev_read_enabled(struct udevice *dev);
456f7d6fcf7SSimon Glass 
457dcf98852SSimon Glass /**
458dcf98852SSimon Glass  * dev_read_resource() - obtain an indexed resource from a device.
459dcf98852SSimon Glass  *
4607b8b47bdSMasahiro Yamada  * @dev: device to examine
461dcf98852SSimon Glass  * @index index of the resource to retrieve (0 = first)
462dcf98852SSimon Glass  * @res returns the resource
463dcf98852SSimon Glass  * @return 0 if ok, negative on error
464dcf98852SSimon Glass  */
465dcf98852SSimon Glass int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
466dcf98852SSimon Glass 
4677b8b47bdSMasahiro Yamada /**
4687b8b47bdSMasahiro Yamada  * dev_read_resource_byname() - obtain a named resource from a device.
4697b8b47bdSMasahiro Yamada  *
4707b8b47bdSMasahiro Yamada  * @dev: device to examine
4717b8b47bdSMasahiro Yamada  * @name: name of the resource to retrieve
4727b8b47bdSMasahiro Yamada  * @res: returns the resource
4737b8b47bdSMasahiro Yamada  * @return 0 if ok, negative on error
4747b8b47bdSMasahiro Yamada  */
4757b8b47bdSMasahiro Yamada int dev_read_resource_byname(struct udevice *dev, const char *name,
4767b8b47bdSMasahiro Yamada 			     struct resource *res);
4777b8b47bdSMasahiro Yamada 
478a9bd1c73SMario Six /**
479a9bd1c73SMario Six  * dev_translate_address() - Tranlate a device-tree address
480a9bd1c73SMario Six  *
481a9bd1c73SMario Six  * Translate an address from the device-tree into a CPU physical address.  This
482a9bd1c73SMario Six  * function walks up the tree and applies the various bus mappings along the
483a9bd1c73SMario Six  * way.
484a9bd1c73SMario Six  *
485a9bd1c73SMario Six  * @dev: device giving the context in which to translate the address
486a9bd1c73SMario Six  * @in_addr: pointer to the address to translate
487a9bd1c73SMario Six  * @return the translated address; OF_BAD_ADDR on error
488a9bd1c73SMario Six  */
489a9bd1c73SMario Six u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
49047a0fd3bSSimon Glass #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
49147a0fd3bSSimon Glass 
49247a0fd3bSSimon Glass static inline int dev_read_u32_default(struct udevice *dev,
49347a0fd3bSSimon Glass 				       const char *propname, int def)
49447a0fd3bSSimon Glass {
49547a0fd3bSSimon Glass 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
49647a0fd3bSSimon Glass }
49747a0fd3bSSimon Glass 
49847a0fd3bSSimon Glass static inline const char *dev_read_string(struct udevice *dev,
49947a0fd3bSSimon Glass 					  const char *propname)
50047a0fd3bSSimon Glass {
50147a0fd3bSSimon Glass 	return ofnode_read_string(dev_ofnode(dev), propname);
50247a0fd3bSSimon Glass }
50347a0fd3bSSimon Glass 
50447a0fd3bSSimon Glass static inline bool dev_read_bool(struct udevice *dev, const char *propname)
50547a0fd3bSSimon Glass {
50647a0fd3bSSimon Glass 	return ofnode_read_bool(dev_ofnode(dev), propname);
50747a0fd3bSSimon Glass }
50847a0fd3bSSimon Glass 
50947a0fd3bSSimon Glass static inline ofnode dev_read_subnode(struct udevice *dev,
51047a0fd3bSSimon Glass 				      const char *subbnode_name)
51147a0fd3bSSimon Glass {
51247a0fd3bSSimon Glass 	return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
51347a0fd3bSSimon Glass }
51447a0fd3bSSimon Glass 
51547a0fd3bSSimon Glass static inline int dev_read_size(struct udevice *dev, const char *propname)
51647a0fd3bSSimon Glass {
51747a0fd3bSSimon Glass 	return ofnode_read_size(dev_ofnode(dev), propname);
51847a0fd3bSSimon Glass }
51947a0fd3bSSimon Glass 
52047a0fd3bSSimon Glass static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
52147a0fd3bSSimon Glass {
52247a0fd3bSSimon Glass 	return devfdt_get_addr_index(dev, index);
52347a0fd3bSSimon Glass }
52447a0fd3bSSimon Glass 
52547a0fd3bSSimon Glass static inline fdt_addr_t dev_read_addr(struct udevice *dev)
52647a0fd3bSSimon Glass {
52747a0fd3bSSimon Glass 	return devfdt_get_addr(dev);
52847a0fd3bSSimon Glass }
52947a0fd3bSSimon Glass 
53018a0c4a6SPhilipp Tomsich static inline void *dev_read_addr_ptr(struct udevice *dev)
53118a0c4a6SPhilipp Tomsich {
53218a0c4a6SPhilipp Tomsich 	return devfdt_get_addr_ptr(dev);
53318a0c4a6SPhilipp Tomsich }
53418a0c4a6SPhilipp Tomsich 
53547a0fd3bSSimon Glass static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
53647a0fd3bSSimon Glass 					    const char *propname,
53747a0fd3bSSimon Glass 					    fdt_size_t *sizep)
53847a0fd3bSSimon Glass {
53947a0fd3bSSimon Glass 	return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
54047a0fd3bSSimon Glass }
54147a0fd3bSSimon Glass 
54247a0fd3bSSimon Glass static inline const char *dev_read_name(struct udevice *dev)
54347a0fd3bSSimon Glass {
544b283d2aeSKever Yang 	if (!dev_of_valid(dev))
545b283d2aeSKever Yang 		return NULL;
54647a0fd3bSSimon Glass 	return ofnode_get_name(dev_ofnode(dev));
54747a0fd3bSSimon Glass }
54847a0fd3bSSimon Glass 
54947a0fd3bSSimon Glass static inline int dev_read_stringlist_search(struct udevice *dev,
55047a0fd3bSSimon Glass 					     const char *propname,
55147a0fd3bSSimon Glass 					     const char *string)
55247a0fd3bSSimon Glass {
55347a0fd3bSSimon Glass 	return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
55447a0fd3bSSimon Glass }
55547a0fd3bSSimon Glass 
55657a9c706SJean-Jacques Hiblot static inline int dev_read_string_index(struct udevice *dev,
55757a9c706SJean-Jacques Hiblot 					const char *propname, int index,
55857a9c706SJean-Jacques Hiblot 					const char **outp)
55957a9c706SJean-Jacques Hiblot {
56057a9c706SJean-Jacques Hiblot 	return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
56157a9c706SJean-Jacques Hiblot }
56257a9c706SJean-Jacques Hiblot 
56357a9c706SJean-Jacques Hiblot static inline int dev_read_string_count(struct udevice *dev,
56457a9c706SJean-Jacques Hiblot 					const char *propname)
56557a9c706SJean-Jacques Hiblot {
56657a9c706SJean-Jacques Hiblot 	return ofnode_read_string_count(dev_ofnode(dev), propname);
56757a9c706SJean-Jacques Hiblot }
56857a9c706SJean-Jacques Hiblot 
56947a0fd3bSSimon Glass static inline int dev_read_phandle_with_args(struct udevice *dev,
57047a0fd3bSSimon Glass 		const char *list_name, const char *cells_name, int cell_count,
57147a0fd3bSSimon Glass 		int index, struct ofnode_phandle_args *out_args)
57247a0fd3bSSimon Glass {
57347a0fd3bSSimon Glass 	return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
57447a0fd3bSSimon Glass 					      cells_name, cell_count, index,
57547a0fd3bSSimon Glass 					      out_args);
57647a0fd3bSSimon Glass }
57747a0fd3bSSimon Glass 
578642346aeSPatrice Chotard static inline int dev_count_phandle_with_args(struct udevice *dev,
579642346aeSPatrice Chotard 		const char *list_name, const char *cells_name)
580642346aeSPatrice Chotard {
581642346aeSPatrice Chotard 	return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
582642346aeSPatrice Chotard 					      cells_name);
583642346aeSPatrice Chotard }
584642346aeSPatrice Chotard 
58547a0fd3bSSimon Glass static inline int dev_read_addr_cells(struct udevice *dev)
58647a0fd3bSSimon Glass {
587878d68c0SSimon Glass 	/* NOTE: this call should walk up the parent stack */
58847a0fd3bSSimon Glass 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
58947a0fd3bSSimon Glass }
59047a0fd3bSSimon Glass 
59147a0fd3bSSimon Glass static inline int dev_read_size_cells(struct udevice *dev)
59247a0fd3bSSimon Glass {
593878d68c0SSimon Glass 	/* NOTE: this call should walk up the parent stack */
594878d68c0SSimon Glass 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
595878d68c0SSimon Glass }
596878d68c0SSimon Glass 
597878d68c0SSimon Glass static inline int dev_read_simple_addr_cells(struct udevice *dev)
598878d68c0SSimon Glass {
599878d68c0SSimon Glass 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
600878d68c0SSimon Glass }
601878d68c0SSimon Glass 
602878d68c0SSimon Glass static inline int dev_read_simple_size_cells(struct udevice *dev)
603878d68c0SSimon Glass {
60447a0fd3bSSimon Glass 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
60547a0fd3bSSimon Glass }
60647a0fd3bSSimon Glass 
60747a0fd3bSSimon Glass static inline int dev_read_phandle(struct udevice *dev)
60847a0fd3bSSimon Glass {
60947a0fd3bSSimon Glass 	return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
61047a0fd3bSSimon Glass }
61147a0fd3bSSimon Glass 
612fd73621cSMasahiro Yamada static inline const void *dev_read_prop(struct udevice *dev,
61347a0fd3bSSimon Glass 					const char *propname, int *lenp)
61447a0fd3bSSimon Glass {
61561e51babSMasahiro Yamada 	return ofnode_get_property(dev_ofnode(dev), propname, lenp);
61647a0fd3bSSimon Glass }
61747a0fd3bSSimon Glass 
618*3cf48410SPatrick Delaunay static inline int dev_read_first_prop(struct udevice *dev, struct ofprop *prop)
619*3cf48410SPatrick Delaunay {
620*3cf48410SPatrick Delaunay 	return ofnode_get_first_property(dev_ofnode(dev), prop);
621*3cf48410SPatrick Delaunay }
622*3cf48410SPatrick Delaunay 
623*3cf48410SPatrick Delaunay static inline int dev_read_next_prop(struct ofprop *prop)
624*3cf48410SPatrick Delaunay {
625*3cf48410SPatrick Delaunay 	return ofnode_get_next_property(prop);
626*3cf48410SPatrick Delaunay }
627*3cf48410SPatrick Delaunay 
628*3cf48410SPatrick Delaunay static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
629*3cf48410SPatrick Delaunay 						const char **propname,
630*3cf48410SPatrick Delaunay 						int *lenp)
631*3cf48410SPatrick Delaunay {
632*3cf48410SPatrick Delaunay 	return ofnode_get_property_by_prop(prop, propname, lenp);
633*3cf48410SPatrick Delaunay }
634*3cf48410SPatrick Delaunay 
63547a0fd3bSSimon Glass static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
63647a0fd3bSSimon Glass {
63747a0fd3bSSimon Glass 	return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
63847a0fd3bSSimon Glass 				    dev_of_offset(dev), devnump);
63947a0fd3bSSimon Glass }
64047a0fd3bSSimon Glass 
64147a0fd3bSSimon Glass static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
64247a0fd3bSSimon Glass 				     u32 *out_values, size_t sz)
64347a0fd3bSSimon Glass {
644b283d2aeSKever Yang 	if (!dev_of_valid(dev))
645b283d2aeSKever Yang 		return -EINVAL;
64647a0fd3bSSimon Glass 	return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
64747a0fd3bSSimon Glass }
64847a0fd3bSSimon Glass 
64947a0fd3bSSimon Glass static inline ofnode dev_read_first_subnode(struct udevice *dev)
65047a0fd3bSSimon Glass {
65147a0fd3bSSimon Glass 	return ofnode_first_subnode(dev_ofnode(dev));
65247a0fd3bSSimon Glass }
65347a0fd3bSSimon Glass 
65447a0fd3bSSimon Glass static inline ofnode dev_read_next_subnode(ofnode node)
65547a0fd3bSSimon Glass {
65647a0fd3bSSimon Glass 	return ofnode_next_subnode(node);
65747a0fd3bSSimon Glass }
65847a0fd3bSSimon Glass 
659f11c7ab9SSimon Glass static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
660f11c7ab9SSimon Glass 					const char *propname, size_t sz)
661f11c7ab9SSimon Glass {
662f11c7ab9SSimon Glass 	return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
663f11c7ab9SSimon Glass }
664f11c7ab9SSimon Glass 
665f7d6fcf7SSimon Glass static inline int dev_read_enabled(struct udevice *dev)
666f7d6fcf7SSimon Glass {
667f7d6fcf7SSimon Glass 	return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
668f7d6fcf7SSimon Glass }
669f7d6fcf7SSimon Glass 
670dcf98852SSimon Glass static inline int dev_read_resource(struct udevice *dev, uint index,
671dcf98852SSimon Glass 				    struct resource *res)
672dcf98852SSimon Glass {
673dcf98852SSimon Glass 	return ofnode_read_resource(dev_ofnode(dev), index, res);
674dcf98852SSimon Glass }
675dcf98852SSimon Glass 
6767b8b47bdSMasahiro Yamada static inline int dev_read_resource_byname(struct udevice *dev,
6777b8b47bdSMasahiro Yamada 					   const char *name,
6787b8b47bdSMasahiro Yamada 					   struct resource *res)
6797b8b47bdSMasahiro Yamada {
6807b8b47bdSMasahiro Yamada 	return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
6817b8b47bdSMasahiro Yamada }
6827b8b47bdSMasahiro Yamada 
683a9bd1c73SMario Six static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
684a9bd1c73SMario Six {
685a9bd1c73SMario Six 	return ofnode_translate_address(dev_ofnode(dev), in_addr);
686a9bd1c73SMario Six }
687a9bd1c73SMario Six 
688f11c7ab9SSimon Glass #endif /* CONFIG_DM_DEV_READ_INLINE */
689f11c7ab9SSimon Glass 
690f11c7ab9SSimon Glass /**
691f11c7ab9SSimon Glass  * dev_for_each_subnode() - Helper function to iterate through subnodes
692f11c7ab9SSimon Glass  *
693f11c7ab9SSimon Glass  * This creates a for() loop which works through the subnodes in a device's
694f11c7ab9SSimon Glass  * device-tree node.
695f11c7ab9SSimon Glass  *
696f11c7ab9SSimon Glass  * @subnode: ofnode holding the current subnode
697f11c7ab9SSimon Glass  * @dev: device to use for interation (struct udevice *)
698f11c7ab9SSimon Glass  */
699f11c7ab9SSimon Glass #define dev_for_each_subnode(subnode, dev) \
700f11c7ab9SSimon Glass 	for (subnode = dev_read_first_subnode(dev); \
701f11c7ab9SSimon Glass 	     ofnode_valid(subnode); \
702f11c7ab9SSimon Glass 	     subnode = ofnode_next_subnode(subnode))
703f11c7ab9SSimon Glass 
704*3cf48410SPatrick Delaunay /**
705*3cf48410SPatrick Delaunay  * dev_for_each_property() - Helper function to iterate through property
706*3cf48410SPatrick Delaunay  *
707*3cf48410SPatrick Delaunay  * This creates a for() loop which works through the property in a device's
708*3cf48410SPatrick Delaunay  * device-tree node.
709*3cf48410SPatrick Delaunay  *
710*3cf48410SPatrick Delaunay  * @prop: struct ofprop holding the current property
711*3cf48410SPatrick Delaunay  * @dev: device to use for interation (struct udevice *)
712*3cf48410SPatrick Delaunay  */
713*3cf48410SPatrick Delaunay #define dev_for_each_property(prop, dev) \
714*3cf48410SPatrick Delaunay 	for (int ret_prop = dev_read_first_prop(dev, &prop); \
715*3cf48410SPatrick Delaunay 	     !ret_prop; \
716*3cf48410SPatrick Delaunay 	     ret_prop = dev_read_next_prop(&prop))
717*3cf48410SPatrick Delaunay 
718f11c7ab9SSimon Glass #endif
719