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_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  */
42 int 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  */
52 u8 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  */
62 int 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  */
72 u16 dev_read_u16_default(const struct udevice *dev, const char *propname,
73 			 u16 def);
74 
75 /**
76  * 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)
81  * Return: 0 if OK, -ve on error
82  */
83 int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
84 
85 /**
86  * 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
91  * Return: property value, or @def if not found
92  */
93 int dev_read_u32_default(const struct udevice *dev, const char *propname,
94 			 int def);
95 
96 /**
97  * 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)
104  * Return: 0 if OK, -ve on error
105  */
106 int 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
117  * Return: property value, or @def if not found
118  */
119 u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
120 			       int index, u32 def);
121 
122 /**
123  * 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)
128  * Return: 0 if OK, -ve on error
129  */
130 int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
131 
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
138  * Return: property value, or @def if not found
139  */
140 int dev_read_s32_default(const struct udevice *dev, const char *propname,
141 			 int def);
142 
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)
151  * Return: 0 if OK, -ve on error
152  */
153 int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
154 
155 /**
156  * 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)
161  * Return: 0 if OK, -ve on error
162  */
163 int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
164 
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
171  * Return: property value, or @def if not found
172  */
173 u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
174 			 u64 def);
175 
176 /**
177  * 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
181  * Return: string from property value, or NULL if there is no such property
182  */
183 const char *dev_read_string(const struct udevice *dev, const char *propname);
184 
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
190  * Return: true if property is present (meaning true), false if not present
191  */
192 bool dev_read_bool(const struct udevice *dev, const char *propname);
193 
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
199  * Return: reference to subnode (which can be invalid if there is no such
200  * subnode)
201  */
202 ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name);
203 
204 /**
205  * dev_read_size() - read the size of a property
206  *
207  * @dev: device to check
208  * @propname: property to check
209  * Return: size of property if present, or -EINVAL if not
210  */
211 int dev_read_size(const struct udevice *dev, const char *propname);
212 
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  *
220  * Return: address or FDT_ADDR_T_NONE if not found
221  */
222 fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
223 
224 /**
225  * 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  *
232  * Return: pointer or NULL if not found
233  */
234 void *dev_read_addr_index_ptr(const struct udevice *dev, int index);
235 
236 /**
237  * 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  *
244  * Return: address or FDT_ADDR_T_NONE if not found
245  */
246 fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
247 				    fdt_size_t *size);
248 
249 /**
250  * 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  *
257  * Return: pointer or NULL if not found
258  */
259 void *dev_remap_addr_index(const struct udevice *dev, int index);
260 
261 /**
262  * 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  *
269  * Return: address or FDT_ADDR_T_NONE if not found
270  */
271 fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
272 
273 /**
274  * 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  *
282  * Return: address or FDT_ADDR_T_NONE if not found
283  */
284 fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
285 				   fdt_size_t *size);
286 
287 /**
288  * 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  *
296  * Return: pointer or NULL if not found
297  */
298 void *dev_remap_addr_name(const struct udevice *dev, const char *name);
299 
300 /**
301  * dev_read_addr() - Get the reg property of a device
302  *
303  * @dev: Device to read from
304  *
305  * Return: address or FDT_ADDR_T_NONE if not found
306  */
307 fdt_addr_t dev_read_addr(const struct udevice *dev);
308 
309 /**
310  * dev_read_addr_ptr() - Get the reg property of a device
311  *                       as a pointer
312  *
313  * @dev: Device to read from
314  *
315  * Return: pointer or NULL if not found
316  */
317 void *dev_read_addr_ptr(const struct udevice *dev);
318 
319 /**
320  * 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
335  * Return: address or FDT_ADDR_T_NONE if not found
336  */
337 fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
338 
339 /**
340  * 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  *
345  * Return: pointer or NULL if not found
346  */
347 void *dev_remap_addr(const struct udevice *dev);
348 
349 /**
350  * 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)
358  * Return: address value, or FDT_ADDR_T_NONE on error
359  */
360 fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname,
361 			      fdt_size_t *sizep);
362 
363 /**
364  * dev_read_name() - get the name of a device's node
365  *
366  * @dev: Device to read from
367  * Return: name of node
368  */
369 const char *dev_read_name(const struct udevice *dev);
370 
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  *
384  * Return:
385  *   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  */
389 int dev_read_stringlist_search(const struct udevice *dev, const char *propname,
390 			       const char *string);
391 
392 /**
393  * 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
398  * @outp: return location for the string
399  *
400  * Return:
401  *   length of string, if found or -ve error value if not found
402  */
403 int dev_read_string_index(const struct udevice *dev, const char *propname,
404 			  int index, const char **outp);
405 
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
411  * Return:
412  *   number of strings in the list, or -ve error value if not found
413  */
414 int dev_read_string_count(const struct udevice *dev, const char *propname);
415 
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
430  * Return:
431  * number of strings in list, 0 if none, -ENOMEM if out of memory,
432  * -ENOENT if no such property
433  */
434 int dev_read_string_list(const struct udevice *dev, const char *propname,
435 			 const char ***listp);
436 
437 /**
438  * 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  *
449  * .. code-block::
450  *
451  *   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  *   };
460  *
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
467  * @cell_count: Cell count to use if @cells_name is NULL
468  * @index:	index of a phandle to parse out
469  * @out_args:	optional pointer to output arguments structure (will be filled)
470  * Return: 0 on success (with @out_args filled out if not NULL), -ENOENT if
471  *	@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  */
475 int 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);
478 
479 /**
480  * 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  *
486  * @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
489  * @cell_count: Cell count to use if @cells_name is NULL
490  * Return: number of phandle found on success, on error returns appropriate
491  * errno value.
492  */
493 
494 int dev_count_phandle_with_args(const struct udevice *dev,
495 				const char *list_name, const char *cells_name,
496 				int cell_count);
497 
498 /**
499  * 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  *
504  * @dev: device to check
505  * Return: number of address cells this node uses
506  */
507 int dev_read_addr_cells(const struct udevice *dev);
508 
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  *
515  * @dev: device to check
516  * Return: number of size cells this node uses
517  */
518 int dev_read_size_cells(const struct udevice *dev);
519 
520 /**
521  * dev_read_addr_cells() - Get the address cells property in a node
522  *
523  * This function matches fdt_address_cells().
524  *
525  * @dev: device to check
526  * Return: number of address cells this node uses
527  */
528 int dev_read_simple_addr_cells(const struct udevice *dev);
529 
530 /**
531  * dev_read_size_cells() - Get the size cells property in a node
532  *
533  * This function matches fdt_size_cells().
534  *
535  * @dev: device to check
536  * Return: number of size cells this node uses
537  */
538 int dev_read_simple_size_cells(const struct udevice *dev);
539 
540 /**
541  * dev_read_phandle() - Get the phandle from a device
542  *
543  * @dev: device to check
544  * Return: phandle (1 or greater), or 0 if no phandle or other error
545  */
546 int dev_read_phandle(const struct udevice *dev);
547 
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
554  * Return: pointer to property, or NULL if not found
555  */
556 const void *dev_read_prop(const struct udevice *dev, const char *propname,
557 			  int *lenp);
558 
559 /**
560  * 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
567  * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
568  */
569 int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
570 
571 /**
572  * ofnode_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
578  * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
579  */
580 int 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
590  * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
591  */
592 const void *dev_read_prop_by_prop(struct ofprop *prop,
593 				  const char **propname, int *lenp);
594 
595 /**
596  * 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
604  * Return: 0 if a sequence was found, -ve if not
605  */
606 int dev_read_alias_seq(const struct udevice *dev, int *devnump);
607 
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
620  * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
621  * property does not have a value, and -EOVERFLOW if the property data isn't
622  * large enough.
623  */
624 int dev_read_u32_array(const struct udevice *dev, const char *propname,
625 		       u32 *out_values, size_t sz);
626 
627 /**
628  * dev_read_first_subnode() - find the first subnode of a device's node
629  *
630  * @dev: device to look up
631  * Return: reference to the first subnode (which can be invalid if the device's
632  * node has no subnodes)
633  */
634 ofnode dev_read_first_subnode(const struct udevice *dev);
635 
636 /**
637  * ofnode_next_subnode() - find the next sibling of a subnode
638  *
639  * @node:	valid reference to previous node (sibling)
640  * Return: reference to the next subnode (which can be invalid if the node
641  * has no more siblings)
642  */
643 ofnode dev_read_next_subnode(ofnode node);
644 
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
656  * Return:
657  * pointer to byte array if found, or NULL if the property is not found or
658  * there is not enough data
659  */
660 const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
661 				     const char *propname, size_t sz);
662 
663 /**
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
672  * Return: integer value 0 (not enabled) or 1 (enabled)
673  */
674 int dev_read_enabled(const struct udevice *dev);
675 
676 /**
677  * dev_read_resource() - obtain an indexed resource from a device.
678  *
679  * @dev: device to examine
680  * @index: index of the resource to retrieve (0 = first)
681  * @res: returns the resource
682  * Return: 0 if ok, negative on error
683  */
684 int dev_read_resource(const struct udevice *dev, uint index,
685 		      struct resource *res);
686 
687 /**
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
693  * Return: 0 if ok, negative on error
694  */
695 int dev_read_resource_byname(const struct udevice *dev, const char *name,
696 			     struct resource *res);
697 
698 /**
699  * dev_translate_address() - Translate a device-tree address
700  *
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
707  * Return: the translated address; OF_BAD_ADDR on error
708  */
709 u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
710 
711 /**
712  * 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
720  * Return: the translated DMA address; OF_BAD_ADDR on error
721  */
722 u64 dev_translate_dma_address(const struct udevice *dev,
723 			      const fdt32_t *in_addr);
724 
725 /**
726  * 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
735  * Return: 0 if ok, negative on error
736  */
737 int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
738 		      dma_addr_t *bus, u64 *size);
739 
740 /**
741  * 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.
746  * Return: alias ID, if found, else -1
747  */
748 int dev_read_alias_highest_id(const char *stem);
749 
750 /**
751  * dev_get_child_count() - get the child count of a device
752  *
753  * @dev: device to use for interation (`struct udevice *`)
754  * Return: the count of child subnode
755  */
756 int dev_get_child_count(const struct udevice *dev);
757 
758 /**
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
765  * @res: returns the resource
766  * Return: 0 if ok, negative on error
767  */
768 int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
769 
770 /**
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
782  * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
783  */
784 int dev_decode_display_timing(const struct udevice *dev, int index,
785 			      struct display_timing *config);
786 
787 /**
788  * dev_decode_panel_timing() - decode panel timings
789  *
790  * Decode display timings from the supplied 'panel-timings' node.
791  *
792  * @dev: device to read DT display timings from. The node linked to the device
793  *       contains a child node called 'display-timings' which in turn contains
794  *       one or more display timing nodes.
795  * @config: place to put timings
796  * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
797  */
798 int dev_decode_panel_timing(const struct udevice *dev,
799 			    struct display_timing *config);
800 
801 /**
802  * dev_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
803  *
804  * This function parses PHY handle from the Ethernet controller's ofnode
805  * (trying all possible PHY handle property names), and returns the PHY ofnode.
806  *
807  * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
808  * if the result to that is true, this function should not be called.
809  *
810  * @dev: device representing the MAC
811  * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
812  */
813 ofnode dev_get_phy_node(const struct udevice *dev);
814 
815 /**
816  * dev_read_phy_mode() - Read PHY connection type from a MAC
817  *
818  * This function parses the "phy-mode" / "phy-connection-type" property and
819  * returns the corresponding PHY interface type.
820  *
821  * @dev: device representing the MAC
822  * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
823  *	   error
824  */
825 phy_interface_t dev_read_phy_mode(const struct udevice *dev);
826 
827 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
828 #include <asm/global_data.h>
829 
dev_read_u8(const struct udevice * dev,const char * propname,u8 * outp)830 static inline int dev_read_u8(const struct udevice *dev,
831 			      const char *propname, u8 *outp)
832 {
833 	return ofnode_read_u8(dev_ofnode(dev), propname, outp);
834 }
835 
dev_read_u8_default(const struct udevice * dev,const char * propname,u8 def)836 static inline int dev_read_u8_default(const struct udevice *dev,
837 				      const char *propname, u8 def)
838 {
839 	return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
840 }
841 
dev_read_u16(const struct udevice * dev,const char * propname,u16 * outp)842 static inline int dev_read_u16(const struct udevice *dev,
843 			       const char *propname, u16 *outp)
844 {
845 	return ofnode_read_u16(dev_ofnode(dev), propname, outp);
846 }
847 
dev_read_u16_default(const struct udevice * dev,const char * propname,u16 def)848 static inline int dev_read_u16_default(const struct udevice *dev,
849 				       const char *propname, u16 def)
850 {
851 	return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
852 }
853 
dev_read_u32(const struct udevice * dev,const char * propname,u32 * outp)854 static inline int dev_read_u32(const struct udevice *dev,
855 			       const char *propname, u32 *outp)
856 {
857 	return ofnode_read_u32(dev_ofnode(dev), propname, outp);
858 }
859 
dev_read_u32_default(const struct udevice * dev,const char * propname,int def)860 static inline int dev_read_u32_default(const struct udevice *dev,
861 				       const char *propname, int def)
862 {
863 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
864 }
865 
dev_read_u32_index(struct udevice * dev,const char * propname,int index,u32 * outp)866 static inline int dev_read_u32_index(struct udevice *dev,
867 				     const char *propname, int index, u32 *outp)
868 {
869 	return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
870 }
871 
dev_read_u32_index_default(struct udevice * dev,const char * propname,int index,u32 def)872 static inline u32 dev_read_u32_index_default(struct udevice *dev,
873 					     const char *propname, int index,
874 					     u32 def)
875 {
876 	return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
877 					     def);
878 }
879 
dev_read_s32(const struct udevice * dev,const char * propname,s32 * outp)880 static inline int dev_read_s32(const struct udevice *dev,
881 			       const char *propname, s32 *outp)
882 {
883 	return ofnode_read_s32(dev_ofnode(dev), propname, outp);
884 }
885 
dev_read_s32_default(const struct udevice * dev,const char * propname,int def)886 static inline int dev_read_s32_default(const struct udevice *dev,
887 				       const char *propname, int def)
888 {
889 	return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
890 }
891 
dev_read_u32u(const struct udevice * dev,const char * propname,uint * outp)892 static inline int dev_read_u32u(const struct udevice *dev,
893 				const char *propname, uint *outp)
894 {
895 	u32 val;
896 	int ret;
897 
898 	ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
899 	if (ret)
900 		return ret;
901 	*outp = val;
902 
903 	return 0;
904 }
905 
dev_read_u64(const struct udevice * dev,const char * propname,u64 * outp)906 static inline int dev_read_u64(const struct udevice *dev,
907 			       const char *propname, u64 *outp)
908 {
909 	return ofnode_read_u64(dev_ofnode(dev), propname, outp);
910 }
911 
dev_read_u64_default(const struct udevice * dev,const char * propname,u64 def)912 static inline u64 dev_read_u64_default(const struct udevice *dev,
913 				       const char *propname, u64 def)
914 {
915 	return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
916 }
917 
dev_read_string(const struct udevice * dev,const char * propname)918 static inline const char *dev_read_string(const struct udevice *dev,
919 					  const char *propname)
920 {
921 	return ofnode_read_string(dev_ofnode(dev), propname);
922 }
923 
dev_read_bool(const struct udevice * dev,const char * propname)924 static inline bool dev_read_bool(const struct udevice *dev,
925 				 const char *propname)
926 {
927 	return ofnode_read_bool(dev_ofnode(dev), propname);
928 }
929 
dev_read_subnode(const struct udevice * dev,const char * subbnode_name)930 static inline ofnode dev_read_subnode(const struct udevice *dev,
931 				      const char *subbnode_name)
932 {
933 	return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
934 }
935 
dev_read_size(const struct udevice * dev,const char * propname)936 static inline int dev_read_size(const struct udevice *dev, const char *propname)
937 {
938 	return ofnode_read_size(dev_ofnode(dev), propname);
939 }
940 
dev_read_addr_index(const struct udevice * dev,int index)941 static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
942 					     int index)
943 {
944 	return devfdt_get_addr_index(dev, index);
945 }
946 
dev_read_addr_index_ptr(const struct udevice * dev,int index)947 static inline void *dev_read_addr_index_ptr(const struct udevice *dev,
948 					    int index)
949 {
950 	return devfdt_get_addr_index_ptr(dev, index);
951 }
952 
dev_read_addr_size_index(const struct udevice * dev,int index,fdt_size_t * size)953 static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
954 						  int index,
955 						  fdt_size_t *size)
956 {
957 	return devfdt_get_addr_size_index(dev, index, size);
958 }
959 
dev_read_addr_name(const struct udevice * dev,const char * name)960 static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
961 					    const char *name)
962 {
963 	return devfdt_get_addr_name(dev, name);
964 }
965 
dev_read_addr_size_name(const struct udevice * dev,const char * name,fdt_size_t * size)966 static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
967 						 const char *name,
968 						 fdt_size_t *size)
969 {
970 	return devfdt_get_addr_size_name(dev, name, size);
971 }
972 
dev_read_addr(const struct udevice * dev)973 static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
974 {
975 	return devfdt_get_addr(dev);
976 }
977 
dev_read_addr_ptr(const struct udevice * dev)978 static inline void *dev_read_addr_ptr(const struct udevice *dev)
979 {
980 	return devfdt_get_addr_ptr(dev);
981 }
982 
dev_read_addr_pci(const struct udevice * dev)983 static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
984 {
985 	return devfdt_get_addr_pci(dev);
986 }
987 
dev_remap_addr(const struct udevice * dev)988 static inline void *dev_remap_addr(const struct udevice *dev)
989 {
990 	return devfdt_remap_addr(dev);
991 }
992 
dev_remap_addr_index(const struct udevice * dev,int index)993 static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
994 {
995 	return devfdt_remap_addr_index(dev, index);
996 }
997 
dev_remap_addr_name(const struct udevice * dev,const char * name)998 static inline void *dev_remap_addr_name(const struct udevice *dev,
999 					const char *name)
1000 {
1001 	return devfdt_remap_addr_name(dev, name);
1002 }
1003 
dev_read_addr_size(const struct udevice * dev,const char * propname,fdt_size_t * sizep)1004 static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
1005 					    const char *propname,
1006 					    fdt_size_t *sizep)
1007 {
1008 	return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
1009 }
1010 
dev_read_name(const struct udevice * dev)1011 static inline const char *dev_read_name(const struct udevice *dev)
1012 {
1013 	return ofnode_get_name(dev_ofnode(dev));
1014 }
1015 
dev_read_stringlist_search(const struct udevice * dev,const char * propname,const char * string)1016 static inline int dev_read_stringlist_search(const struct udevice *dev,
1017 					     const char *propname,
1018 					     const char *string)
1019 {
1020 	return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
1021 }
1022 
dev_read_string_index(const struct udevice * dev,const char * propname,int index,const char ** outp)1023 static inline int dev_read_string_index(const struct udevice *dev,
1024 					const char *propname, int index,
1025 					const char **outp)
1026 {
1027 	return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
1028 }
1029 
dev_read_string_count(const struct udevice * dev,const char * propname)1030 static inline int dev_read_string_count(const struct udevice *dev,
1031 					const char *propname)
1032 {
1033 	return ofnode_read_string_count(dev_ofnode(dev), propname);
1034 }
1035 
dev_read_string_list(const struct udevice * dev,const char * propname,const char *** listp)1036 static inline int dev_read_string_list(const struct udevice *dev,
1037 				       const char *propname,
1038 				       const char ***listp)
1039 {
1040 	return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
1041 }
1042 
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)1043 static inline int dev_read_phandle_with_args(const struct udevice *dev,
1044 		const char *list_name, const char *cells_name, int cell_count,
1045 		int index, struct ofnode_phandle_args *out_args)
1046 {
1047 	return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
1048 					      cells_name, cell_count, index,
1049 					      out_args);
1050 }
1051 
dev_count_phandle_with_args(const struct udevice * dev,const char * list_name,const char * cells_name,int cell_count)1052 static inline int dev_count_phandle_with_args(const struct udevice *dev,
1053 		const char *list_name, const char *cells_name, int cell_count)
1054 {
1055 	return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
1056 					      cells_name, cell_count);
1057 }
1058 
dev_read_addr_cells(const struct udevice * dev)1059 static inline int dev_read_addr_cells(const struct udevice *dev)
1060 {
1061 	int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1062 
1063 	return fdt_address_cells(gd->fdt_blob, parent);
1064 }
1065 
dev_read_size_cells(const struct udevice * dev)1066 static inline int dev_read_size_cells(const struct udevice *dev)
1067 {
1068 	int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1069 
1070 	return fdt_size_cells(gd->fdt_blob, parent);
1071 }
1072 
dev_read_simple_addr_cells(const struct udevice * dev)1073 static inline int dev_read_simple_addr_cells(const struct udevice *dev)
1074 {
1075 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
1076 }
1077 
dev_read_simple_size_cells(const struct udevice * dev)1078 static inline int dev_read_simple_size_cells(const struct udevice *dev)
1079 {
1080 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
1081 }
1082 
dev_read_phandle(const struct udevice * dev)1083 static inline int dev_read_phandle(const struct udevice *dev)
1084 {
1085 	return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
1086 }
1087 
dev_read_prop(const struct udevice * dev,const char * propname,int * lenp)1088 static inline const void *dev_read_prop(const struct udevice *dev,
1089 					const char *propname, int *lenp)
1090 {
1091 	return ofnode_get_property(dev_ofnode(dev), propname, lenp);
1092 }
1093 
dev_read_first_prop(const struct udevice * dev,struct ofprop * prop)1094 static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
1095 {
1096 	return ofnode_first_property(dev_ofnode(dev), prop);
1097 }
1098 
dev_read_next_prop(struct ofprop * prop)1099 static inline int dev_read_next_prop(struct ofprop *prop)
1100 {
1101 	return ofnode_next_property(prop);
1102 }
1103 
dev_read_prop_by_prop(struct ofprop * prop,const char ** propname,int * lenp)1104 static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
1105 						const char **propname,
1106 						int *lenp)
1107 {
1108 	return ofprop_get_property(prop, propname, lenp);
1109 }
1110 
dev_read_alias_seq(const struct udevice * dev,int * devnump)1111 static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
1112 {
1113 #if CONFIG_IS_ENABLED(OF_CONTROL)
1114 	return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
1115 				    dev_of_offset(dev), devnump);
1116 #else
1117 	return -ENOTSUPP;
1118 #endif
1119 }
1120 
dev_read_u32_array(const struct udevice * dev,const char * propname,u32 * out_values,size_t sz)1121 static inline int dev_read_u32_array(const struct udevice *dev,
1122 				     const char *propname, u32 *out_values,
1123 				     size_t sz)
1124 {
1125 	return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
1126 }
1127 
dev_read_first_subnode(const struct udevice * dev)1128 static inline ofnode dev_read_first_subnode(const struct udevice *dev)
1129 {
1130 	return ofnode_first_subnode(dev_ofnode(dev));
1131 }
1132 
dev_read_next_subnode(ofnode node)1133 static inline ofnode dev_read_next_subnode(ofnode node)
1134 {
1135 	return ofnode_next_subnode(node);
1136 }
1137 
dev_read_u8_array_ptr(const struct udevice * dev,const char * propname,size_t sz)1138 static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
1139 						   const char *propname,
1140 						   size_t sz)
1141 {
1142 	return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
1143 }
1144 
dev_read_enabled(const struct udevice * dev)1145 static inline int dev_read_enabled(const struct udevice *dev)
1146 {
1147 	return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
1148 }
1149 
dev_read_resource(const struct udevice * dev,uint index,struct resource * res)1150 static inline int dev_read_resource(const struct udevice *dev, uint index,
1151 				    struct resource *res)
1152 {
1153 	return ofnode_read_resource(dev_ofnode(dev), index, res);
1154 }
1155 
dev_read_resource_byname(const struct udevice * dev,const char * name,struct resource * res)1156 static inline int dev_read_resource_byname(const struct udevice *dev,
1157 					   const char *name,
1158 					   struct resource *res)
1159 {
1160 	return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
1161 }
1162 
dev_translate_address(const struct udevice * dev,const fdt32_t * in_addr)1163 static inline u64 dev_translate_address(const struct udevice *dev,
1164 					const fdt32_t *in_addr)
1165 {
1166 	return ofnode_translate_address(dev_ofnode(dev), in_addr);
1167 }
1168 
dev_translate_dma_address(const struct udevice * dev,const fdt32_t * in_addr)1169 static inline u64 dev_translate_dma_address(const struct udevice *dev,
1170 					    const fdt32_t *in_addr)
1171 {
1172 	return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
1173 }
1174 
dev_get_dma_range(const struct udevice * dev,phys_addr_t * cpu,dma_addr_t * bus,u64 * size)1175 static inline int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
1176 				    dma_addr_t *bus, u64 *size)
1177 {
1178 	return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
1179 }
1180 
dev_read_alias_highest_id(const char * stem)1181 static inline int dev_read_alias_highest_id(const char *stem)
1182 {
1183 	if (!CONFIG_IS_ENABLED(OF_LIBFDT) || !gd->fdt_blob)
1184 		return -1;
1185 	return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
1186 }
1187 
dev_get_child_count(const struct udevice * dev)1188 static inline int dev_get_child_count(const struct udevice *dev)
1189 {
1190 	return ofnode_get_child_count(dev_ofnode(dev));
1191 }
1192 
dev_decode_display_timing(const struct udevice * dev,int index,struct display_timing * config)1193 static inline int dev_decode_display_timing(const struct udevice *dev,
1194 					    int index,
1195 					    struct display_timing *config)
1196 {
1197 	return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
1198 }
1199 
dev_decode_panel_timing(const struct udevice * dev,struct display_timing * config)1200 static inline int dev_decode_panel_timing(const struct udevice *dev,
1201 					  struct display_timing *config)
1202 {
1203 	return ofnode_decode_panel_timing(dev_ofnode(dev), config);
1204 }
1205 
dev_get_phy_node(const struct udevice * dev)1206 static inline ofnode dev_get_phy_node(const struct udevice *dev)
1207 {
1208 	return ofnode_get_phy_node(dev_ofnode(dev));
1209 }
1210 
dev_read_phy_mode(const struct udevice * dev)1211 static inline phy_interface_t dev_read_phy_mode(const struct udevice *dev)
1212 {
1213 	return ofnode_read_phy_mode(dev_ofnode(dev));
1214 }
1215 
1216 #endif /* CONFIG_DM_DEV_READ_INLINE */
1217 
1218 /**
1219  * dev_for_each_subnode() - Helper function to iterate through subnodes
1220  *
1221  * This creates a for() loop which works through the subnodes in a device's
1222  * device-tree node.
1223  *
1224  * @subnode: ofnode holding the current subnode
1225  * @dev: device to use for interation (`struct udevice *`)
1226  */
1227 #define dev_for_each_subnode(subnode, dev) \
1228 	for (subnode = dev_read_first_subnode(dev); \
1229 	     ofnode_valid(subnode); \
1230 	     subnode = ofnode_next_subnode(subnode))
1231 
1232 /**
1233  * dev_for_each_property() - Helper function to iterate through property
1234  *
1235  * This creates a for() loop which works through the property in a device's
1236  * device-tree node.
1237  *
1238  * @prop: struct ofprop holding the current property
1239  * @dev: device to use for interation (`struct udevice *`)
1240  */
1241 #define dev_for_each_property(prop, dev) \
1242 	for (int ret_prop = dev_read_first_prop(dev, &prop); \
1243 	     !ret_prop; \
1244 	     ret_prop = dev_read_next_prop(&prop))
1245 
1246 #endif
1247