1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
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>
7  */
8 
9 #ifndef _DM_READ_H
10 #define _DM_READ_H
11 
12 #include <linux/errno.h>
13 
14 #include <dm/device.h>
15 #include <dm/fdtaddr.h>
16 #include <dm/ofnode.h>
17 #include <dm/uclass.h>
18 
19 struct resource;
20 
21 #if CONFIG_IS_ENABLED(OF_LIVE)
dev_np(const struct udevice * dev)22 static inline const struct device_node *dev_np(const struct udevice *dev)
23 {
24 	return ofnode_to_np(dev_ofnode(dev));
25 }
26 #else
dev_np(const struct udevice * dev)27 static inline const struct device_node *dev_np(const struct udevice *dev)
28 {
29 	return NULL;
30 }
31 #endif
32 
33 #if !defined(CONFIG_DM_DEV_READ_INLINE) || CONFIG_IS_ENABLED(OF_PLATDATA)
34 /**
35  * dev_read_u32() - read a 32-bit integer from a device's DT property
36  *
37  * @dev:	device to read DT property from
38  * @propname:	name of the property to read from
39  * @outp:	place to put value (if found)
40  * @return 0 if OK, -ve on error
41  */
42 int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
43 
44 /**
45  * dev_read_u32_default() - read a 32-bit integer from a device's DT property
46  *
47  * @dev:	device to read DT property from
48  * @propname:	name of the property to read from
49  * @def:	default value to return if the property has no value
50  * @return property value, or @def if not found
51  */
52 int dev_read_u32_default(const struct udevice *dev, const char *propname,
53 			 int def);
54 
55 /**
56  * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
57  *                        property
58  *
59  * @dev:	device to read DT property from
60  * @propname:	name of the property to read from
61  * @index:	index of the integer to return
62  * @outp:	place to put value (if found)
63  * @return 0 if OK, -ve on error
64  */
65 int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
66 		       u32 *outp);
67 
68 /**
69  * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
70  *                                DT property
71  *
72  * @dev:	device to read DT property from
73  * @propname:	name of the property to read from
74  * @index:	index of the integer to return
75  * @def:	default value to return if the property has no value
76  * @return property value, or @def if not found
77  */
78 u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
79 			       int index, u32 def);
80 
81 /**
82  * dev_read_s32() - read a signed 32-bit integer from a device's DT property
83  *
84  * @dev:	device to read DT property from
85  * @propname:	name of the property to read from
86  * @outp:	place to put value (if found)
87  * @return 0 if OK, -ve on error
88  */
89 int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
90 
91 /**
92  * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
93  *
94  * @dev:	device to read DT property from
95  * @propname:	name of the property to read from
96  * @def:	default value to return if the property has no value
97  * @return property value, or @def if not found
98  */
99 int dev_read_s32_default(const struct udevice *dev, const char *propname,
100 			 int def);
101 
102 /**
103  * dev_read_u32u() - read a 32-bit integer from a device's DT property
104  *
105  * This version uses a standard uint type.
106  *
107  * @dev:	device to read DT property from
108  * @propname:	name of the property to read from
109  * @outp:	place to put value (if found)
110  * @return 0 if OK, -ve on error
111  */
112 int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
113 
114 /**
115  * dev_read_u64() - read a 64-bit integer from a device's DT property
116  *
117  * @dev:        device to read DT property from
118  * @propname:   name of the property to read from
119  * @outp:       place to put value (if found)
120  * @return 0 if OK, -ve on error
121  */
122 int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
123 
124 /**
125  * dev_read_u64_default() - read a 64-bit integer from a device's DT property
126  *
127  * @dev:        device to read DT property from
128  * @propname:   name of the property to read from
129  * @def:        default value to return if the property has no value
130  * @return property value, or @def if not found
131  */
132 u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
133 			 u64 def);
134 
135 /**
136  * dev_read_string() - Read a string from a device's DT property
137  *
138  * @dev:	device to read DT property from
139  * @propname:	name of the property to read
140  * @return string from property value, or NULL if there is no such property
141  */
142 const char *dev_read_string(const struct udevice *dev, const char *propname);
143 
144 /**
145  * dev_read_bool() - read a boolean value from a device's DT property
146  *
147  * @dev:	device to read DT property from
148  * @propname:	name of property to read
149  * @return true if property is present (meaning true), false if not present
150  */
151 bool dev_read_bool(const struct udevice *dev, const char *propname);
152 
153 /**
154  * dev_read_subnode() - find a named subnode of a device
155  *
156  * @dev:	device whose DT node contains the subnode
157  * @subnode_name: name of subnode to find
158  * @return reference to subnode (which can be invalid if there is no such
159  * subnode)
160  */
161 ofnode dev_read_subnode(const struct udevice *dev, const char *subbnode_name);
162 
163 /**
164  * dev_read_size() - read the size of a property
165  *
166  * @dev: device to check
167  * @propname: property to check
168  * @return size of property if present, or -EINVAL if not
169  */
170 int dev_read_size(const struct udevice *dev, const char *propname);
171 
172 /**
173  * dev_read_addr_index() - Get the indexed reg property of a device
174  *
175  * @dev: Device to read from
176  * @index: the 'reg' property can hold a list of <addr, size> pairs
177  *	   and @index is used to select which one is required
178  *
179  * @return address or FDT_ADDR_T_NONE if not found
180  */
181 fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
182 
183 /**
184  * dev_read_addr_size_index() - Get the indexed reg property of a device
185  *
186  * @dev: Device to read from
187  * @index: the 'reg' property can hold a list of <addr, size> pairs
188  *	   and @index is used to select which one is required
189  * @size: place to put size value (on success)
190  *
191  * @return address or FDT_ADDR_T_NONE if not found
192  */
193 fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
194 				    fdt_size_t *size);
195 
196 /**
197  * dev_remap_addr_index() - Get the indexed reg property of a device
198  *                               as a memory-mapped I/O pointer
199  *
200  * @dev: Device to read from
201  * @index: the 'reg' property can hold a list of <addr, size> pairs
202  *	   and @index is used to select which one is required
203  *
204  * @return pointer or NULL if not found
205  */
206 void *dev_remap_addr_index(const struct udevice *dev, int index);
207 
208 /**
209  * dev_read_addr_name() - Get the reg property of a device, indexed by name
210  *
211  * @dev: Device to read from
212  * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
213  *	  'reg-names' property providing named-based identification. @index
214  *	  indicates the value to search for in 'reg-names'.
215  *
216  * @return address or FDT_ADDR_T_NONE if not found
217  */
218 fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
219 
220 /**
221  * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
222  *
223  * @dev: Device to read from
224  * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
225  *	  'reg-names' property providing named-based identification. @index
226  *	  indicates the value to search for in 'reg-names'.
227  *  @size: place to put size value (on success)
228  *
229  * @return address or FDT_ADDR_T_NONE if not found
230  */
231 fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
232 				   fdt_size_t *size);
233 
234 /**
235  * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
236  *                         as a memory-mapped I/O pointer
237  *
238  * @dev: Device to read from
239  * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
240  *	  'reg-names' property providing named-based identification. @index
241  *	  indicates the value to search for in 'reg-names'.
242  *
243  * @return pointer or NULL if not found
244  */
245 void *dev_remap_addr_name(const struct udevice *dev, const char *name);
246 
247 /**
248  * dev_read_addr() - Get the reg property of a device
249  *
250  * @dev: Device to read from
251  *
252  * @return address or FDT_ADDR_T_NONE if not found
253  */
254 fdt_addr_t dev_read_addr(const struct udevice *dev);
255 
256 /**
257  * dev_read_addr_ptr() - Get the reg property of a device
258  *                       as a pointer
259  *
260  * @dev: Device to read from
261  *
262  * @return pointer or NULL if not found
263  */
264 void *dev_read_addr_ptr(const struct udevice *dev);
265 
266 /**
267  * dev_read_addr_pci() - Read an address and handle PCI address translation
268  *
269  * At present U-Boot does not have address translation logic for PCI in the
270  * livetree implementation (of_addr.c). This special function supports this for
271  * the flat tree implementation.
272  *
273  * This function should be removed (and code should use dev_read() instead)
274  * once:
275  *
276  * 1. PCI address translation is added; and either
277  * 2. everything uses livetree where PCI translation is used (which is feasible
278  *    in SPL and U-Boot proper) or PCI address translation is added to
279  *    fdtdec_get_addr() and friends.
280  *
281  * @dev: Device to read from
282  * @return address or FDT_ADDR_T_NONE if not found
283  */
284 fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
285 
286 /**
287  * dev_remap_addr() - Get the reg property of a device as a
288  *                         memory-mapped I/O pointer
289  *
290  * @dev: Device to read from
291  *
292  * @return pointer or NULL if not found
293  */
294 void *dev_remap_addr(const struct udevice *dev);
295 
296 /**
297  * dev_read_addr_size() - get address and size from a device property
298  *
299  * This does no address translation. It simply reads an property that contains
300  * an address and a size value, one after the other.
301  *
302  * @dev: Device to read from
303  * @propname: property to read
304  * @sizep: place to put size value (on success)
305  * @return address value, or FDT_ADDR_T_NONE on error
306  */
307 fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname,
308 			      fdt_size_t *sizep);
309 
310 /**
311  * dev_read_name() - get the name of a device's node
312  *
313  * @dev: Device to read from
314  * @return name of node
315  */
316 const char *dev_read_name(const struct udevice *dev);
317 
318 /**
319  * dev_read_stringlist_search() - find string in a string list and return index
320  *
321  * Note that it is possible for this function to succeed on property values
322  * that are not NUL-terminated. That's because the function will stop after
323  * finding the first occurrence of @string. This can for example happen with
324  * small-valued cell properties, such as #address-cells, when searching for
325  * the empty string.
326  *
327  * @dev: device to check
328  * @propname: name of the property containing the string list
329  * @string: string to look up in the string list
330  *
331  * @return:
332  *   the index of the string in the list of strings
333  *   -ENODATA if the property is not found
334  *   -EINVAL on some other error
335  */
336 int dev_read_stringlist_search(const struct udevice *dev, const char *property,
337 			       const char *string);
338 
339 /**
340  * dev_read_string_index() - obtain an indexed string from a string list
341  *
342  * @dev: device to examine
343  * @propname: name of the property containing the string list
344  * @index: index of the string to return
345  * @out: return location for the string
346  *
347  * @return:
348  *   length of string, if found or -ve error value if not found
349  */
350 int dev_read_string_index(const struct udevice *dev, const char *propname,
351 			  int index, const char **outp);
352 
353 /**
354  * dev_read_string_count() - find the number of strings in a string list
355  *
356  * @dev: device to examine
357  * @propname: name of the property containing the string list
358  * @return:
359  *   number of strings in the list, or -ve error value if not found
360  */
361 int dev_read_string_count(const struct udevice *dev, const char *propname);
362 /**
363  * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
364  *
365  * This function is useful to parse lists of phandles and their arguments.
366  * Returns 0 on success and fills out_args, on error returns appropriate
367  * errno value.
368  *
369  * Caller is responsible to call of_node_put() on the returned out_args->np
370  * pointer.
371  *
372  * Example:
373  *
374  * phandle1: node1 {
375  *	#list-cells = <2>;
376  * }
377  *
378  * phandle2: node2 {
379  *	#list-cells = <1>;
380  * }
381  *
382  * node3 {
383  *	list = <&phandle1 1 2 &phandle2 3>;
384  * }
385  *
386  * To get a device_node of the `node2' node you may call this:
387  * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
388  *
389  * @dev:	device whose node containing a list
390  * @list_name:	property name that contains a list
391  * @cells_name:	property name that specifies phandles' arguments count
392  * @cells_count: Cell count to use if @cells_name is NULL
393  * @index:	index of a phandle to parse out
394  * @out_args:	optional pointer to output arguments structure (will be filled)
395  * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
396  *	@list_name does not exist, -EINVAL if a phandle was not found,
397  *	@cells_name could not be found, the arguments were truncated or there
398  *	were too many arguments.
399  */
400 int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
401 			       const char *cells_name, int cell_count,
402 			       int index, struct ofnode_phandle_args *out_args);
403 
404 /**
405  * dev_count_phandle_with_args() - Return phandle number in a list
406  *
407  * This function is usefull to get phandle number contained in a property list.
408  * For example, this allows to allocate the right amount of memory to keep
409  * clock's reference contained into the "clocks" property.
410  *
411  *
412  * @dev:	device whose node containing a list
413  * @list_name:	property name that contains a list
414  * @cells_name:	property name that specifies phandles' arguments count
415  * @cells_count: Cell count to use if @cells_name is NULL
416  * @Returns number of phandle found on success, on error returns appropriate
417  * errno value.
418  */
419 
420 int dev_count_phandle_with_args(const struct udevice *dev,
421 				const char *list_name, const char *cells_name,
422 				int cell_count);
423 
424 /**
425  * dev_read_addr_cells() - Get the number of address cells for a device's node
426  *
427  * This walks back up the tree to find the closest #address-cells property
428  * which controls the given node.
429  *
430  * @dev: device to check
431  * @return number of address cells this node uses
432  */
433 int dev_read_addr_cells(const struct udevice *dev);
434 
435 /**
436  * dev_read_size_cells() - Get the number of size cells for a device's node
437  *
438  * This walks back up the tree to find the closest #size-cells property
439  * which controls the given node.
440  *
441  * @dev: device to check
442  * @return number of size cells this node uses
443  */
444 int dev_read_size_cells(const struct udevice *dev);
445 
446 /**
447  * dev_read_addr_cells() - Get the address cells property in a node
448  *
449  * This function matches fdt_address_cells().
450  *
451  * @dev: device to check
452  * @return number of address cells this node uses
453  */
454 int dev_read_simple_addr_cells(const struct udevice *dev);
455 
456 /**
457  * dev_read_size_cells() - Get the size cells property in a node
458  *
459  * This function matches fdt_size_cells().
460  *
461  * @dev: device to check
462  * @return number of size cells this node uses
463  */
464 int dev_read_simple_size_cells(const struct udevice *dev);
465 
466 /**
467  * dev_read_phandle() - Get the phandle from a device
468  *
469  * @dev: device to check
470  * @return phandle (1 or greater), or 0 if no phandle or other error
471  */
472 int dev_read_phandle(const struct udevice *dev);
473 
474 /**
475  * dev_read_prop()- - read a property from a device's node
476  *
477  * @dev: device to check
478  * @propname: property to read
479  * @lenp: place to put length on success
480  * @return pointer to property, or NULL if not found
481  */
482 const void *dev_read_prop(const struct udevice *dev, const char *propname,
483 			  int *lenp);
484 
485 /**
486  * dev_read_first_prop()- get the reference of the first property
487  *
488  * Get reference to the first property of the node, it is used to iterate
489  * and read all the property with dev_read_prop_by_prop().
490  *
491  * @dev: device to check
492  * @prop: place to put argument reference
493  * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
494  */
495 int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
496 
497 /**
498  * ofnode_get_next_property() - get the reference of the next property
499  *
500  * Get reference to the next property of the node, it is used to iterate
501  * and read all the property with dev_read_prop_by_prop().
502  *
503  * @prop: reference of current argument and place to put reference of next one
504  * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
505  */
506 int dev_read_next_prop(struct ofprop *prop);
507 
508 /**
509  * dev_read_prop_by_prop() - get a pointer to the value of a property
510  *
511  * Get value for the property identified by the provided reference.
512  *
513  * @prop: reference on property
514  * @propname: If non-NULL, place to property name on success,
515  * @lenp: If non-NULL, place to put length on success
516  * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
517  */
518 const void *dev_read_prop_by_prop(struct ofprop *prop,
519 				  const char **propname, int *lenp);
520 
521 /**
522  * dev_read_alias_seq() - Get the alias sequence number of a node
523  *
524  * This works out whether a node is pointed to by an alias, and if so, the
525  * sequence number of that alias. Aliases are of the form <base><num> where
526  * <num> is the sequence number. For example spi2 would be sequence number 2.
527  *
528  * @dev: device to look up
529  * @devnump: set to the sequence number if one is found
530  * @return 0 if a sequence was found, -ve if not
531  */
532 int dev_read_alias_seq(const struct udevice *dev, int *devnump);
533 
534 /**
535  * dev_read_u32_array() - Find and read an array of 32 bit integers
536  *
537  * Search for a property in a device node and read 32-bit value(s) from
538  * it.
539  *
540  * The out_values is modified only if a valid u32 value can be decoded.
541  *
542  * @dev: device to look up
543  * @propname:	name of the property to read
544  * @out_values:	pointer to return value, modified only if return value is 0
545  * @sz:		number of array elements to read
546  * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
547  * property does not have a value, and -EOVERFLOW if the property data isn't
548  * large enough.
549  */
550 int dev_read_u32_array(const struct udevice *dev, const char *propname,
551 		       u32 *out_values, size_t sz);
552 
553 /**
554  * dev_read_first_subnode() - find the first subnode of a device's node
555  *
556  * @dev: device to look up
557  * @return reference to the first subnode (which can be invalid if the device's
558  * node has no subnodes)
559  */
560 ofnode dev_read_first_subnode(const struct udevice *dev);
561 
562 /**
563  * ofnode_next_subnode() - find the next sibling of a subnode
564  *
565  * @node:	valid reference to previous node (sibling)
566  * @return reference to the next subnode (which can be invalid if the node
567  * has no more siblings)
568  */
569 ofnode dev_read_next_subnode(ofnode node);
570 
571 /**
572  * dev_read_u8_array_ptr() - find an 8-bit array
573  *
574  * Look up a device's node property and return a pointer to its contents as a
575  * byte array of given length. The property must have at least enough data
576  * for the array (count bytes). It may have more, but this will be ignored.
577  * The data is not copied.
578  *
579  * @dev: device to look up
580  * @propname: name of property to find
581  * @sz: number of array elements
582  * @return pointer to byte array if found, or NULL if the property is not
583  *		found or there is not enough data
584  */
585 const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
586 				     const char *propname, size_t sz);
587 
588 /**
589  * dev_read_enabled() - check whether a node is enabled
590  *
591  * This looks for a 'status' property. If this exists, then returns 1 if
592  * the status is 'ok' and 0 otherwise. If there is no status property,
593  * it returns 1 on the assumption that anything mentioned should be enabled
594  * by default.
595  *
596  * @dev: device to examine
597  * @return integer value 0 (not enabled) or 1 (enabled)
598  */
599 int dev_read_enabled(const struct udevice *dev);
600 
601 /**
602  * dev_read_resource() - obtain an indexed resource from a device.
603  *
604  * @dev: device to examine
605  * @index index of the resource to retrieve (0 = first)
606  * @res returns the resource
607  * @return 0 if ok, negative on error
608  */
609 int dev_read_resource(const struct udevice *dev, uint index,
610 		      struct resource *res);
611 
612 /**
613  * dev_read_resource_byname() - obtain a named resource from a device.
614  *
615  * @dev: device to examine
616  * @name: name of the resource to retrieve
617  * @res: returns the resource
618  * @return 0 if ok, negative on error
619  */
620 int dev_read_resource_byname(const struct udevice *dev, const char *name,
621 			     struct resource *res);
622 
623 /**
624  * dev_translate_address() - Translate a device-tree address
625  *
626  * Translate an address from the device-tree into a CPU physical address.  This
627  * function walks up the tree and applies the various bus mappings along the
628  * way.
629  *
630  * @dev: device giving the context in which to translate the address
631  * @in_addr: pointer to the address to translate
632  * @return the translated address; OF_BAD_ADDR on error
633  */
634 u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
635 
636 /**
637  * dev_translate_dma_address() - Translate a device-tree DMA address
638  *
639  * Translate a DMA address from the device-tree into a CPU physical address.
640  * This function walks up the tree and applies the various bus mappings along
641  * the way.
642  *
643  * @dev: device giving the context in which to translate the DMA address
644  * @in_addr: pointer to the DMA address to translate
645  * @return the translated DMA address; OF_BAD_ADDR on error
646  */
647 u64 dev_translate_dma_address(const struct udevice *dev,
648 			      const fdt32_t *in_addr);
649 
650 /**
651  * dev_get_dma_range() - Get a device's DMA constraints
652  *
653  * Provide the address bases and size of the linear mapping between the CPU and
654  * a device's BUS address space.
655  *
656  * @dev: device giving the context in which to translate the DMA address
657  * @cpu: base address for CPU's view of memory
658  * @bus: base address for BUS's view of memory
659  * @size: size of the address space
660  * @return 0 if ok, negative on error
661  */
662 int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
663 		      dma_addr_t *bus, u64 *size);
664 
665 /**
666  * dev_read_alias_highest_id - Get highest alias id for the given stem
667  * @stem:	Alias stem to be examined
668  *
669  * The function travels the lookup table to get the highest alias id for the
670  * given alias stem.
671  * @return alias ID, if found, else -1
672  */
673 int dev_read_alias_highest_id(const char *stem);
674 
675 /**
676  * dev_get_child_count() - get the child count of a device
677  *
678  * @dev: device to use for interation (struct udevice *)
679  * @return the count of child subnode
680  */
681 int dev_get_child_count(const struct udevice *dev);
682 
683 /**
684  * dev_read_pci_bus_range - Read PCI bus-range resource
685  *
686  * Look at the bus range property of a device node and return the pci bus
687  * range for this node.
688  *
689  * @dev: device to examine
690  * @res returns the resource
691  * @return 0 if ok, negative on error
692  */
693 int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
694 
695 /**
696  * dev_decode_display_timing() - decode display timings
697  *
698  * Decode display timings from the supplied 'display-timings' node.
699  * See doc/device-tree-bindings/video/display-timing.txt for binding
700  * information.
701  *
702  * @dev: device to read DT display timings from. The node linked to the device
703  *       contains a child node called 'display-timings' which in turn contains
704  *       one or more display timing nodes.
705  * @index: index number to read (0=first timing subnode)
706  * @config: place to put timings
707  * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
708  */
709 int dev_decode_display_timing(const struct udevice *dev, int index,
710 			      struct display_timing *config);
711 
712 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
713 #include <asm/global_data.h>
714 
dev_read_u32(const struct udevice * dev,const char * propname,u32 * outp)715 static inline int dev_read_u32(const struct udevice *dev,
716 			       const char *propname, u32 *outp)
717 {
718 	return ofnode_read_u32(dev_ofnode(dev), propname, outp);
719 }
720 
dev_read_u32_default(const struct udevice * dev,const char * propname,int def)721 static inline int dev_read_u32_default(const struct udevice *dev,
722 				       const char *propname, int def)
723 {
724 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
725 }
726 
dev_read_u32_index(struct udevice * dev,const char * propname,int index,u32 * outp)727 static inline int dev_read_u32_index(struct udevice *dev,
728 				     const char *propname, int index, u32 *outp)
729 {
730 	return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
731 }
732 
dev_read_u32_index_default(struct udevice * dev,const char * propname,int index,u32 def)733 static inline u32 dev_read_u32_index_default(struct udevice *dev,
734 					     const char *propname, int index,
735 					     u32 def)
736 {
737 	return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
738 					     def);
739 }
740 
dev_read_s32(const struct udevice * dev,const char * propname,s32 * outp)741 static inline int dev_read_s32(const struct udevice *dev,
742 			       const char *propname, s32 *outp)
743 {
744 	return ofnode_read_s32(dev_ofnode(dev), propname, outp);
745 }
746 
dev_read_s32_default(const struct udevice * dev,const char * propname,int def)747 static inline int dev_read_s32_default(const struct udevice *dev,
748 				       const char *propname, int def)
749 {
750 	return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
751 }
752 
dev_read_u32u(const struct udevice * dev,const char * propname,uint * outp)753 static inline int dev_read_u32u(const struct udevice *dev,
754 				const char *propname, uint *outp)
755 {
756 	u32 val;
757 	int ret;
758 
759 	ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
760 	if (ret)
761 		return ret;
762 	*outp = val;
763 
764 	return 0;
765 }
766 
dev_read_u64(const struct udevice * dev,const char * propname,u64 * outp)767 static inline int dev_read_u64(const struct udevice *dev,
768 			       const char *propname, u64 *outp)
769 {
770 	return ofnode_read_u64(dev_ofnode(dev), propname, outp);
771 }
772 
dev_read_u64_default(const struct udevice * dev,const char * propname,u64 def)773 static inline u64 dev_read_u64_default(const struct udevice *dev,
774 				       const char *propname, u64 def)
775 {
776 	return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
777 }
778 
dev_read_string(const struct udevice * dev,const char * propname)779 static inline const char *dev_read_string(const struct udevice *dev,
780 					  const char *propname)
781 {
782 	return ofnode_read_string(dev_ofnode(dev), propname);
783 }
784 
dev_read_bool(const struct udevice * dev,const char * propname)785 static inline bool dev_read_bool(const struct udevice *dev,
786 				 const char *propname)
787 {
788 	return ofnode_read_bool(dev_ofnode(dev), propname);
789 }
790 
dev_read_subnode(const struct udevice * dev,const char * subbnode_name)791 static inline ofnode dev_read_subnode(const struct udevice *dev,
792 				      const char *subbnode_name)
793 {
794 	return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
795 }
796 
dev_read_size(const struct udevice * dev,const char * propname)797 static inline int dev_read_size(const struct udevice *dev, const char *propname)
798 {
799 	return ofnode_read_size(dev_ofnode(dev), propname);
800 }
801 
dev_read_addr_index(const struct udevice * dev,int index)802 static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
803 					     int index)
804 {
805 	return devfdt_get_addr_index(dev, index);
806 }
807 
dev_read_addr_size_index(const struct udevice * dev,int index,fdt_size_t * size)808 static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
809 						  int index,
810 						  fdt_size_t *size)
811 {
812 	return devfdt_get_addr_size_index(dev, index, size);
813 }
814 
dev_read_addr_name(const struct udevice * dev,const char * name)815 static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
816 					    const char *name)
817 {
818 	return devfdt_get_addr_name(dev, name);
819 }
820 
dev_read_addr_size_name(const struct udevice * dev,const char * name,fdt_size_t * size)821 static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
822 						 const char *name,
823 						 fdt_size_t *size)
824 {
825 	return devfdt_get_addr_size_name(dev, name, size);
826 }
827 
dev_read_addr(const struct udevice * dev)828 static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
829 {
830 	return devfdt_get_addr(dev);
831 }
832 
dev_read_addr_ptr(const struct udevice * dev)833 static inline void *dev_read_addr_ptr(const struct udevice *dev)
834 {
835 	return devfdt_get_addr_ptr(dev);
836 }
837 
dev_read_addr_pci(const struct udevice * dev)838 static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
839 {
840 	return devfdt_get_addr_pci(dev);
841 }
842 
dev_remap_addr(const struct udevice * dev)843 static inline void *dev_remap_addr(const struct udevice *dev)
844 {
845 	return devfdt_remap_addr(dev);
846 }
847 
dev_remap_addr_index(const struct udevice * dev,int index)848 static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
849 {
850 	return devfdt_remap_addr_index(dev, index);
851 }
852 
dev_remap_addr_name(const struct udevice * dev,const char * name)853 static inline void *dev_remap_addr_name(const struct udevice *dev,
854 					const char *name)
855 {
856 	return devfdt_remap_addr_name(dev, name);
857 }
858 
dev_read_addr_size(const struct udevice * dev,const char * propname,fdt_size_t * sizep)859 static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
860 					    const char *propname,
861 					    fdt_size_t *sizep)
862 {
863 	return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
864 }
865 
dev_read_name(const struct udevice * dev)866 static inline const char *dev_read_name(const struct udevice *dev)
867 {
868 	return ofnode_get_name(dev_ofnode(dev));
869 }
870 
dev_read_stringlist_search(const struct udevice * dev,const char * propname,const char * string)871 static inline int dev_read_stringlist_search(const struct udevice *dev,
872 					     const char *propname,
873 					     const char *string)
874 {
875 	return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
876 }
877 
dev_read_string_index(const struct udevice * dev,const char * propname,int index,const char ** outp)878 static inline int dev_read_string_index(const struct udevice *dev,
879 					const char *propname, int index,
880 					const char **outp)
881 {
882 	return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
883 }
884 
dev_read_string_count(const struct udevice * dev,const char * propname)885 static inline int dev_read_string_count(const struct udevice *dev,
886 					const char *propname)
887 {
888 	return ofnode_read_string_count(dev_ofnode(dev), propname);
889 }
890 
dev_read_phandle_with_args(const struct udevice * dev,const char * list_name,const char * cells_name,int cell_count,int index,struct ofnode_phandle_args * out_args)891 static inline int dev_read_phandle_with_args(const struct udevice *dev,
892 		const char *list_name, const char *cells_name, int cell_count,
893 		int index, struct ofnode_phandle_args *out_args)
894 {
895 	return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
896 					      cells_name, cell_count, index,
897 					      out_args);
898 }
899 
dev_count_phandle_with_args(const struct udevice * dev,const char * list_name,const char * cells_name,int cell_count)900 static inline int dev_count_phandle_with_args(const struct udevice *dev,
901 		const char *list_name, const char *cells_name, int cell_count)
902 {
903 	return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
904 					      cells_name, cell_count);
905 }
906 
dev_read_addr_cells(const struct udevice * dev)907 static inline int dev_read_addr_cells(const struct udevice *dev)
908 {
909 	int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
910 
911 	return fdt_address_cells(gd->fdt_blob, parent);
912 }
913 
dev_read_size_cells(const struct udevice * dev)914 static inline int dev_read_size_cells(const struct udevice *dev)
915 {
916 	int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
917 
918 	return fdt_size_cells(gd->fdt_blob, parent);
919 }
920 
dev_read_simple_addr_cells(const struct udevice * dev)921 static inline int dev_read_simple_addr_cells(const struct udevice *dev)
922 {
923 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
924 }
925 
dev_read_simple_size_cells(const struct udevice * dev)926 static inline int dev_read_simple_size_cells(const struct udevice *dev)
927 {
928 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
929 }
930 
dev_read_phandle(const struct udevice * dev)931 static inline int dev_read_phandle(const struct udevice *dev)
932 {
933 	return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
934 }
935 
dev_read_prop(const struct udevice * dev,const char * propname,int * lenp)936 static inline const void *dev_read_prop(const struct udevice *dev,
937 					const char *propname, int *lenp)
938 {
939 	return ofnode_get_property(dev_ofnode(dev), propname, lenp);
940 }
941 
dev_read_first_prop(const struct udevice * dev,struct ofprop * prop)942 static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
943 {
944 	return ofnode_get_first_property(dev_ofnode(dev), prop);
945 }
946 
dev_read_next_prop(struct ofprop * prop)947 static inline int dev_read_next_prop(struct ofprop *prop)
948 {
949 	return ofnode_get_next_property(prop);
950 }
951 
dev_read_prop_by_prop(struct ofprop * prop,const char ** propname,int * lenp)952 static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
953 						const char **propname,
954 						int *lenp)
955 {
956 	return ofnode_get_property_by_prop(prop, propname, lenp);
957 }
958 
dev_read_alias_seq(const struct udevice * dev,int * devnump)959 static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
960 {
961 #if CONFIG_IS_ENABLED(OF_CONTROL)
962 	return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
963 				    dev_of_offset(dev), devnump);
964 #else
965 	return -ENOTSUPP;
966 #endif
967 }
968 
dev_read_u32_array(const struct udevice * dev,const char * propname,u32 * out_values,size_t sz)969 static inline int dev_read_u32_array(const struct udevice *dev,
970 				     const char *propname, u32 *out_values,
971 				     size_t sz)
972 {
973 	return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
974 }
975 
dev_read_first_subnode(const struct udevice * dev)976 static inline ofnode dev_read_first_subnode(const struct udevice *dev)
977 {
978 	return ofnode_first_subnode(dev_ofnode(dev));
979 }
980 
dev_read_next_subnode(ofnode node)981 static inline ofnode dev_read_next_subnode(ofnode node)
982 {
983 	return ofnode_next_subnode(node);
984 }
985 
dev_read_u8_array_ptr(const struct udevice * dev,const char * propname,size_t sz)986 static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
987 						   const char *propname,
988 						   size_t sz)
989 {
990 	return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
991 }
992 
dev_read_enabled(const struct udevice * dev)993 static inline int dev_read_enabled(const struct udevice *dev)
994 {
995 	return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
996 }
997 
dev_read_resource(const struct udevice * dev,uint index,struct resource * res)998 static inline int dev_read_resource(const struct udevice *dev, uint index,
999 				    struct resource *res)
1000 {
1001 	return ofnode_read_resource(dev_ofnode(dev), index, res);
1002 }
1003 
dev_read_resource_byname(const struct udevice * dev,const char * name,struct resource * res)1004 static inline int dev_read_resource_byname(const struct udevice *dev,
1005 					   const char *name,
1006 					   struct resource *res)
1007 {
1008 	return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
1009 }
1010 
dev_translate_address(const struct udevice * dev,const fdt32_t * in_addr)1011 static inline u64 dev_translate_address(const struct udevice *dev,
1012 					const fdt32_t *in_addr)
1013 {
1014 	return ofnode_translate_address(dev_ofnode(dev), in_addr);
1015 }
1016 
dev_translate_dma_address(const struct udevice * dev,const fdt32_t * in_addr)1017 static inline u64 dev_translate_dma_address(const struct udevice *dev,
1018 					    const fdt32_t *in_addr)
1019 {
1020 	return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
1021 }
1022 
dev_get_dma_range(const struct udevice * dev,phys_addr_t * cpu,dma_addr_t * bus,u64 * size)1023 static inline int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
1024 				    dma_addr_t *bus, u64 *size)
1025 {
1026 	return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
1027 }
1028 
dev_read_alias_highest_id(const char * stem)1029 static inline int dev_read_alias_highest_id(const char *stem)
1030 {
1031 	if (!CONFIG_IS_ENABLED(OF_LIBFDT) || !gd->fdt_blob)
1032 		return -1;
1033 	return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
1034 }
1035 
dev_get_child_count(const struct udevice * dev)1036 static inline int dev_get_child_count(const struct udevice *dev)
1037 {
1038 	return ofnode_get_child_count(dev_ofnode(dev));
1039 }
1040 
dev_decode_display_timing(const struct udevice * dev,int index,struct display_timing * config)1041 static inline int dev_decode_display_timing(const struct udevice *dev,
1042 					    int index,
1043 					    struct display_timing *config)
1044 {
1045 	return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
1046 }
1047 
1048 #endif /* CONFIG_DM_DEV_READ_INLINE */
1049 
1050 /**
1051  * dev_for_each_subnode() - Helper function to iterate through subnodes
1052  *
1053  * This creates a for() loop which works through the subnodes in a device's
1054  * device-tree node.
1055  *
1056  * @subnode: ofnode holding the current subnode
1057  * @dev: device to use for interation (struct udevice *)
1058  */
1059 #define dev_for_each_subnode(subnode, dev) \
1060 	for (subnode = dev_read_first_subnode(dev); \
1061 	     ofnode_valid(subnode); \
1062 	     subnode = ofnode_next_subnode(subnode))
1063 
1064 /**
1065  * dev_for_each_property() - Helper function to iterate through property
1066  *
1067  * This creates a for() loop which works through the property in a device's
1068  * device-tree node.
1069  *
1070  * @prop: struct ofprop holding the current property
1071  * @dev: device to use for interation (struct udevice *)
1072  */
1073 #define dev_for_each_property(prop, dev) \
1074 	for (int ret_prop = dev_read_first_prop(dev, &prop); \
1075 	     !ret_prop; \
1076 	     ret_prop = dev_read_next_prop(&prop))
1077 
1078 #endif
1079