blob: efcbee15ecaea90c05d6d80dcfb75f3a34a3b43a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassf11c7ab2017-05-18 20:09:03 -06002/*
3 * Function to read values from the device tree node attached to a udevice.
4 *
5 * Copyright (c) 2017 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glassf11c7ab2017-05-18 20:09:03 -06007 */
8
9#ifndef _DM_READ_H
10#define _DM_READ_H
11
12#include <dm/fdtaddr.h>
13#include <dm/ofnode.h>
14#include <dm/uclass.h>
15
Simon Glassa4481012017-06-12 06:21:29 -060016struct resource;
17
Simon Glassf11c7ab2017-05-18 20:09:03 -060018#if CONFIG_IS_ENABLED(OF_LIVE)
19static inline const struct device_node *dev_np(struct udevice *dev)
20{
21 return ofnode_to_np(dev->node);
22}
23#else
24static inline const struct device_node *dev_np(struct udevice *dev)
25{
26 return NULL;
27}
28#endif
29
30/**
31 * dev_ofnode() - get the DT node reference associated with a udevice
32 *
33 * @dev: device to check
34 * @return reference of the the device's DT node
35 */
36static inline ofnode dev_ofnode(struct udevice *dev)
37{
38 return dev->node;
39}
40
41static inline bool dev_of_valid(struct udevice *dev)
42{
43 return ofnode_valid(dev_ofnode(dev));
44}
45
Simon Glass47a0fd32017-05-18 20:09:04 -060046#ifndef CONFIG_DM_DEV_READ_INLINE
47/**
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090048 * dev_read_u32() - read a 32-bit integer from a device's DT property
49 *
50 * @dev: device to read DT property from
51 * @propname: name of the property to read from
52 * @outp: place to put value (if found)
53 * @return 0 if OK, -ve on error
54 */
55int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp);
56
57/**
Simon Glass47a0fd32017-05-18 20:09:04 -060058 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
59 *
60 * @dev: device to read DT property from
61 * @propname: name of the property to read from
62 * @def: default value to return if the property has no value
63 * @return property value, or @def if not found
64 */
65int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
Simon Glassf11c7ab2017-05-18 20:09:03 -060066
67/**
68 * dev_read_string() - Read a string from a device's DT property
69 *
70 * @dev: device to read DT property from
71 * @propname: name of the property to read
72 * @return string from property value, or NULL if there is no such property
73 */
Simon Glass47a0fd32017-05-18 20:09:04 -060074const char *dev_read_string(struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -060075
76/**
77 * dev_read_bool() - read a boolean value from a device's DT property
78 *
79 * @dev: device to read DT property from
80 * @propname: name of property to read
81 * @return true if property is present (meaning true), false if not present
82 */
Simon Glass47a0fd32017-05-18 20:09:04 -060083bool dev_read_bool(struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -060084
85/**
86 * dev_read_subnode() - find a named subnode of a device
87 *
88 * @dev: device whose DT node contains the subnode
89 * @subnode_name: name of subnode to find
90 * @return reference to subnode (which can be invalid if there is no such
91 * subnode)
92 */
Simon Glass47a0fd32017-05-18 20:09:04 -060093ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
Simon Glassf11c7ab2017-05-18 20:09:03 -060094
95/**
96 * dev_read_size() - read the size of a property
97 *
98 * @dev: device to check
99 * @propname: property to check
100 * @return size of property if present, or -EINVAL if not
101 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600102int dev_read_size(struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600103
104/**
105 * dev_read_addr_index() - Get the indexed reg property of a device
106 *
107 * @dev: Device to read from
108 * @index: the 'reg' property can hold a list of <addr, size> pairs
109 * and @index is used to select which one is required
110 *
111 * @return address or FDT_ADDR_T_NONE if not found
112 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600113fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600114
115/**
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200116 * dev_remap_addr_index() - Get the indexed reg property of a device
117 * as a memory-mapped I/O pointer
118 *
119 * @dev: Device to read from
120 * @index: the 'reg' property can hold a list of <addr, size> pairs
121 * and @index is used to select which one is required
122 *
123 * @return pointer or NULL if not found
124 */
125void *dev_remap_addr_index(struct udevice *dev, int index);
126
127/**
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100128 * dev_read_addr_name() - Get the reg property of a device, indexed by name
129 *
130 * @dev: Device to read from
131 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
132 * 'reg-names' property providing named-based identification. @index
133 * indicates the value to search for in 'reg-names'.
134 *
135 * @return address or FDT_ADDR_T_NONE if not found
136 */
137fdt_addr_t dev_read_addr_name(struct udevice *dev, const char* name);
138
139/**
140 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
141 * as a memory-mapped I/O pointer
142 *
143 * @dev: Device to read from
144 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
145 * 'reg-names' property providing named-based identification. @index
146 * indicates the value to search for in 'reg-names'.
147 *
148 * @return pointer or NULL if not found
149 */
150void *dev_remap_addr_name(struct udevice *dev, const char* name);
151
152/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600153 * dev_read_addr() - Get the reg property of a device
154 *
155 * @dev: Device to read from
156 *
157 * @return address or FDT_ADDR_T_NONE if not found
158 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600159fdt_addr_t dev_read_addr(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600160
161/**
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200162 * dev_read_addr_ptr() - Get the reg property of a device
163 * as a pointer
164 *
165 * @dev: Device to read from
166 *
167 * @return pointer or NULL if not found
168 */
169void *dev_read_addr_ptr(struct udevice *dev);
170
171/**
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200172 * dev_remap_addr() - Get the reg property of a device as a
173 * memory-mapped I/O pointer
174 *
175 * @dev: Device to read from
176 *
177 * @return pointer or NULL if not found
178 */
179void *dev_remap_addr(struct udevice *dev);
180
181/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600182 * dev_read_addr_size() - get address and size from a device property
183 *
184 * This does no address translation. It simply reads an property that contains
185 * an address and a size value, one after the other.
186 *
187 * @dev: Device to read from
188 * @propname: property to read
189 * @sizep: place to put size value (on success)
190 * @return address value, or FDT_ADDR_T_NONE on error
191 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600192fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
193 fdt_size_t *sizep);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600194
195/**
196 * dev_read_name() - get the name of a device's node
197 *
198 * @node: valid node to look up
199 * @return name of node
200 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600201const char *dev_read_name(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600202
203/**
204 * dev_read_stringlist_search() - find string in a string list and return index
205 *
206 * Note that it is possible for this function to succeed on property values
207 * that are not NUL-terminated. That's because the function will stop after
208 * finding the first occurrence of @string. This can for example happen with
209 * small-valued cell properties, such as #address-cells, when searching for
210 * the empty string.
211 *
212 * @dev: device to check
213 * @propname: name of the property containing the string list
214 * @string: string to look up in the string list
215 *
216 * @return:
217 * the index of the string in the list of strings
218 * -ENODATA if the property is not found
219 * -EINVAL on some other error
220 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600221int dev_read_stringlist_search(struct udevice *dev, const char *property,
222 const char *string);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600223
224/**
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200225 * dev_read_string_index() - obtain an indexed string from a string list
226 *
227 * @dev: device to examine
228 * @propname: name of the property containing the string list
229 * @index: index of the string to return
230 * @out: return location for the string
231 *
232 * @return:
233 * length of string, if found or -ve error value if not found
234 */
235int dev_read_string_index(struct udevice *dev, const char *propname, int index,
236 const char **outp);
237
238/**
239 * dev_read_string_count() - find the number of strings in a string list
240 *
241 * @dev: device to examine
242 * @propname: name of the property containing the string list
243 * @return:
244 * number of strings in the list, or -ve error value if not found
245 */
246int dev_read_string_count(struct udevice *dev, const char *propname);
247/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600248 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
249 *
250 * This function is useful to parse lists of phandles and their arguments.
251 * Returns 0 on success and fills out_args, on error returns appropriate
252 * errno value.
253 *
254 * Caller is responsible to call of_node_put() on the returned out_args->np
255 * pointer.
256 *
257 * Example:
258 *
259 * phandle1: node1 {
260 * #list-cells = <2>;
261 * }
262 *
263 * phandle2: node2 {
264 * #list-cells = <1>;
265 * }
266 *
267 * node3 {
268 * list = <&phandle1 1 2 &phandle2 3>;
269 * }
270 *
271 * To get a device_node of the `node2' node you may call this:
272 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
273 *
274 * @dev: device whose node containing a list
275 * @list_name: property name that contains a list
276 * @cells_name: property name that specifies phandles' arguments count
277 * @cells_count: Cell count to use if @cells_name is NULL
278 * @index: index of a phandle to parse out
279 * @out_args: optional pointer to output arguments structure (will be filled)
280 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
281 * @list_name does not exist, -EINVAL if a phandle was not found,
282 * @cells_name could not be found, the arguments were truncated or there
283 * were too many arguments.
284 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600285int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
286 const char *cells_name, int cell_count,
287 int index,
288 struct ofnode_phandle_args *out_args);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600289
290/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200291 * dev_count_phandle_with_args() - Return phandle number in a list
292 *
293 * This function is usefull to get phandle number contained in a property list.
294 * For example, this allows to allocate the right amount of memory to keep
295 * clock's reference contained into the "clocks" property.
296 *
297 *
298 * @dev: device whose node containing a list
299 * @list_name: property name that contains a list
300 * @cells_name: property name that specifies phandles' arguments count
301 * @Returns number of phandle found on success, on error returns appropriate
302 * errno value.
303 */
304
305int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
306 const char *cells_name);
307
308/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600309 * dev_read_addr_cells() - Get the number of address cells for a device's node
310 *
311 * This walks back up the tree to find the closest #address-cells property
312 * which controls the given node.
313 *
Mario Six7ba50412018-01-15 11:09:36 +0100314 * @dev: device to check
Simon Glassf11c7ab2017-05-18 20:09:03 -0600315 * @return number of address cells this node uses
316 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600317int dev_read_addr_cells(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600318
319/**
320 * dev_read_size_cells() - Get the number of size cells for a device's node
321 *
322 * This walks back up the tree to find the closest #size-cells property
323 * which controls the given node.
324 *
Mario Six7ba50412018-01-15 11:09:36 +0100325 * @dev: device to check
Simon Glassf11c7ab2017-05-18 20:09:03 -0600326 * @return number of size cells this node uses
327 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600328int dev_read_size_cells(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600329
330/**
Simon Glass878d68c2017-06-12 06:21:31 -0600331 * dev_read_addr_cells() - Get the address cells property in a node
332 *
333 * This function matches fdt_address_cells().
334 *
Mario Six7ba50412018-01-15 11:09:36 +0100335 * @dev: device to check
Simon Glass878d68c2017-06-12 06:21:31 -0600336 * @return number of address cells this node uses
337 */
338int dev_read_simple_addr_cells(struct udevice *dev);
339
340/**
341 * dev_read_size_cells() - Get the size cells property in a node
342 *
343 * This function matches fdt_size_cells().
344 *
Mario Six7ba50412018-01-15 11:09:36 +0100345 * @dev: device to check
Simon Glass878d68c2017-06-12 06:21:31 -0600346 * @return number of size cells this node uses
347 */
348int dev_read_simple_size_cells(struct udevice *dev);
349
350/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600351 * dev_read_phandle() - Get the phandle from a device
352 *
353 * @dev: device to check
354 * @return phandle (1 or greater), or 0 if no phandle or other error
355 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600356int dev_read_phandle(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600357
358/**
359 * dev_read_prop()- - read a property from a device's node
360 *
361 * @dev: device to check
362 * @propname: property to read
363 * @lenp: place to put length on success
364 * @return pointer to property, or NULL if not found
365 */
Masahiro Yamadafd736212017-07-17 12:18:39 +0900366const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600367
368/**
369 * dev_read_alias_seq() - Get the alias sequence number of a node
370 *
371 * This works out whether a node is pointed to by an alias, and if so, the
372 * sequence number of that alias. Aliases are of the form <base><num> where
373 * <num> is the sequence number. For example spi2 would be sequence number 2.
374 *
375 * @dev: device to look up
376 * @devnump: set to the sequence number if one is found
377 * @return 0 if a sequence was found, -ve if not
378 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600379int dev_read_alias_seq(struct udevice *dev, int *devnump);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600380
381/**
382 * dev_read_u32_array() - Find and read an array of 32 bit integers
383 *
384 * Search for a property in a device node and read 32-bit value(s) from
385 * it.
386 *
387 * The out_values is modified only if a valid u32 value can be decoded.
388 *
389 * @dev: device to look up
390 * @propname: name of the property to read
391 * @out_values: pointer to return value, modified only if return value is 0
392 * @sz: number of array elements to read
393 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
394 * property does not have a value, and -EOVERFLOW if the property data isn't
395 * large enough.
396 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600397int dev_read_u32_array(struct udevice *dev, const char *propname,
398 u32 *out_values, size_t sz);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600399
400/**
401 * dev_read_first_subnode() - find the first subnode of a device's node
402 *
403 * @dev: device to look up
404 * @return reference to the first subnode (which can be invalid if the device's
405 * node has no subnodes)
406 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600407ofnode dev_read_first_subnode(struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600408
409/**
410 * ofnode_next_subnode() - find the next sibling of a subnode
411 *
412 * @node: valid reference to previous node (sibling)
413 * @return reference to the next subnode (which can be invalid if the node
414 * has no more siblings)
415 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600416ofnode dev_read_next_subnode(ofnode node);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600417
418/**
419 * dev_read_u8_array_ptr() - find an 8-bit array
420 *
421 * Look up a device's node property and return a pointer to its contents as a
422 * byte array of given length. The property must have at least enough data
423 * for the array (count bytes). It may have more, but this will be ignored.
424 * The data is not copied.
425 *
426 * @dev: device to look up
427 * @propname: name of property to find
428 * @sz: number of array elements
429 * @return pointer to byte array if found, or NULL if the property is not
430 * found or there is not enough data
431 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600432const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
433 size_t sz);
434
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600435/**
436 * dev_read_enabled() - check whether a node is enabled
437 *
438 * This looks for a 'status' property. If this exists, then returns 1 if
439 * the status is 'ok' and 0 otherwise. If there is no status property,
440 * it returns 1 on the assumption that anything mentioned should be enabled
441 * by default.
442 *
443 * @dev: device to examine
444 * @return integer value 0 (not enabled) or 1 (enabled)
445 */
446int dev_read_enabled(struct udevice *dev);
447
Simon Glassdcf98852017-07-25 08:29:55 -0600448/**
449 * dev_read_resource() - obtain an indexed resource from a device.
450 *
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900451 * @dev: device to examine
Simon Glassdcf98852017-07-25 08:29:55 -0600452 * @index index of the resource to retrieve (0 = first)
453 * @res returns the resource
454 * @return 0 if ok, negative on error
455 */
456int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
457
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900458/**
459 * dev_read_resource_byname() - obtain a named resource from a device.
460 *
461 * @dev: device to examine
462 * @name: name of the resource to retrieve
463 * @res: returns the resource
464 * @return 0 if ok, negative on error
465 */
466int dev_read_resource_byname(struct udevice *dev, const char *name,
467 struct resource *res);
468
Mario Six147c6072018-01-15 11:07:19 +0100469/**
470 * dev_translate_address() - Tranlate a device-tree address
471 *
472 * Translate an address from the device-tree into a CPU physical address. This
473 * function walks up the tree and applies the various bus mappings along the
474 * way.
475 *
476 * @dev: device giving the context in which to translate the address
477 * @in_addr: pointer to the address to translate
478 * @return the translated address; OF_BAD_ADDR on error
479 */
480u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
Simon Glass47a0fd32017-05-18 20:09:04 -0600481#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
482
Masahiro Yamada3ab48f62017-12-30 02:00:05 +0900483static inline int dev_read_u32(struct udevice *dev,
484 const char *propname, u32 *outp)
485{
486 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
487}
488
Simon Glass47a0fd32017-05-18 20:09:04 -0600489static inline int dev_read_u32_default(struct udevice *dev,
490 const char *propname, int def)
491{
492 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
493}
494
495static inline const char *dev_read_string(struct udevice *dev,
496 const char *propname)
497{
498 return ofnode_read_string(dev_ofnode(dev), propname);
499}
500
501static inline bool dev_read_bool(struct udevice *dev, const char *propname)
502{
503 return ofnode_read_bool(dev_ofnode(dev), propname);
504}
505
506static inline ofnode dev_read_subnode(struct udevice *dev,
507 const char *subbnode_name)
508{
509 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
510}
511
512static inline int dev_read_size(struct udevice *dev, const char *propname)
513{
514 return ofnode_read_size(dev_ofnode(dev), propname);
515}
516
517static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
518{
519 return devfdt_get_addr_index(dev, index);
520}
521
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100522static inline fdt_addr_t dev_read_addr_name(struct udevice *dev,
523 const char *name)
524{
525 return devfdt_get_addr_name(dev, name);
526}
527
Simon Glass47a0fd32017-05-18 20:09:04 -0600528static inline fdt_addr_t dev_read_addr(struct udevice *dev)
529{
530 return devfdt_get_addr(dev);
531}
532
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200533static inline void *dev_read_addr_ptr(struct udevice *dev)
534{
535 return devfdt_get_addr_ptr(dev);
536}
537
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200538static inline void *dev_remap_addr(struct udevice *dev)
539{
540 return devfdt_remap_addr(dev);
541}
542
543static inline void *dev_remap_addr_index(struct udevice *dev, int index)
544{
545 return devfdt_remap_addr_index(dev, index);
546}
547
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100548static inline void *dev_remap_addr_name(struct udevice *dev, const char *name)
549{
550 return devfdt_remap_addr_name(dev, name);
551}
552
Simon Glass47a0fd32017-05-18 20:09:04 -0600553static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
554 const char *propname,
555 fdt_size_t *sizep)
556{
557 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
558}
559
560static inline const char *dev_read_name(struct udevice *dev)
561{
562 return ofnode_get_name(dev_ofnode(dev));
563}
564
565static inline int dev_read_stringlist_search(struct udevice *dev,
566 const char *propname,
567 const char *string)
568{
569 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
570}
571
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200572static inline int dev_read_string_index(struct udevice *dev,
573 const char *propname, int index,
574 const char **outp)
575{
576 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
577}
578
579static inline int dev_read_string_count(struct udevice *dev,
580 const char *propname)
581{
582 return ofnode_read_string_count(dev_ofnode(dev), propname);
583}
584
Simon Glass47a0fd32017-05-18 20:09:04 -0600585static inline int dev_read_phandle_with_args(struct udevice *dev,
586 const char *list_name, const char *cells_name, int cell_count,
587 int index, struct ofnode_phandle_args *out_args)
588{
589 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
590 cells_name, cell_count, index,
591 out_args);
592}
593
Patrice Chotard642346a2017-07-18 11:57:08 +0200594static inline int dev_count_phandle_with_args(struct udevice *dev,
595 const char *list_name, const char *cells_name)
596{
597 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
598 cells_name);
599}
600
Simon Glass47a0fd32017-05-18 20:09:04 -0600601static inline int dev_read_addr_cells(struct udevice *dev)
602{
Simon Glass878d68c2017-06-12 06:21:31 -0600603 /* NOTE: this call should walk up the parent stack */
Simon Glass47a0fd32017-05-18 20:09:04 -0600604 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
605}
606
607static inline int dev_read_size_cells(struct udevice *dev)
608{
Simon Glass878d68c2017-06-12 06:21:31 -0600609 /* NOTE: this call should walk up the parent stack */
610 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
611}
612
613static inline int dev_read_simple_addr_cells(struct udevice *dev)
614{
615 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
616}
617
618static inline int dev_read_simple_size_cells(struct udevice *dev)
619{
Simon Glass47a0fd32017-05-18 20:09:04 -0600620 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
621}
622
623static inline int dev_read_phandle(struct udevice *dev)
624{
625 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
626}
627
Masahiro Yamadafd736212017-07-17 12:18:39 +0900628static inline const void *dev_read_prop(struct udevice *dev,
629 const char *propname, int *lenp)
Simon Glass47a0fd32017-05-18 20:09:04 -0600630{
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900631 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glass47a0fd32017-05-18 20:09:04 -0600632}
633
634static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
635{
636 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
637 dev_of_offset(dev), devnump);
638}
639
640static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
641 u32 *out_values, size_t sz)
642{
643 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
644}
645
646static inline ofnode dev_read_first_subnode(struct udevice *dev)
647{
648 return ofnode_first_subnode(dev_ofnode(dev));
649}
650
651static inline ofnode dev_read_next_subnode(ofnode node)
652{
653 return ofnode_next_subnode(node);
654}
655
Simon Glassf11c7ab2017-05-18 20:09:03 -0600656static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
657 const char *propname, size_t sz)
658{
659 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
660}
661
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600662static inline int dev_read_enabled(struct udevice *dev)
663{
664 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
665}
666
Simon Glassdcf98852017-07-25 08:29:55 -0600667static inline int dev_read_resource(struct udevice *dev, uint index,
668 struct resource *res)
669{
670 return ofnode_read_resource(dev_ofnode(dev), index, res);
671}
672
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900673static inline int dev_read_resource_byname(struct udevice *dev,
674 const char *name,
675 struct resource *res)
676{
677 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
678}
679
Mario Six147c6072018-01-15 11:07:19 +0100680static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
681{
682 return ofnode_translate_address(dev_ofnode(dev), in_addr);
683}
684
Simon Glassf11c7ab2017-05-18 20:09:03 -0600685#endif /* CONFIG_DM_DEV_READ_INLINE */
686
687/**
688 * dev_for_each_subnode() - Helper function to iterate through subnodes
689 *
690 * This creates a for() loop which works through the subnodes in a device's
691 * device-tree node.
692 *
693 * @subnode: ofnode holding the current subnode
694 * @dev: device to use for interation (struct udevice *)
695 */
696#define dev_for_each_subnode(subnode, dev) \
697 for (subnode = dev_read_first_subnode(dev); \
698 ofnode_valid(subnode); \
699 subnode = ofnode_next_subnode(subnode))
700
701#endif