blob: 49d69c990f6eb07f529c12f0af7b1b71b1a3cd1d [file] [log] [blame]
Simon Glass34ef0c22017-05-18 20:09:03 -06001/*
2 * Function to read values from the device tree node attached to a udevice.
3 *
4 * Copyright (c) 2017 Google, Inc
5 * Written by Simon Glass <sjg@chromium.org>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#ifndef _DM_READ_H
11#define _DM_READ_H
12
13#include <dm/fdtaddr.h>
14#include <dm/ofnode.h>
15#include <dm/uclass.h>
16
Simon Glass170e17c2017-06-12 06:21:29 -060017struct resource;
18
Simon Glass34ef0c22017-05-18 20:09:03 -060019#if CONFIG_IS_ENABLED(OF_LIVE)
20static inline const struct device_node *dev_np(struct udevice *dev)
21{
22 return ofnode_to_np(dev->node);
23}
24#else
25static inline const struct device_node *dev_np(struct udevice *dev)
26{
27 return NULL;
28}
29#endif
30
31/**
32 * dev_ofnode() - get the DT node reference associated with a udevice
33 *
34 * @dev: device to check
35 * @return reference of the the device's DT node
36 */
37static inline ofnode dev_ofnode(struct udevice *dev)
38{
39 return dev->node;
40}
41
42static inline bool dev_of_valid(struct udevice *dev)
43{
44 return ofnode_valid(dev_ofnode(dev));
45}
46
Simon Glasscfdae5c2017-05-18 20:09:04 -060047#ifndef CONFIG_DM_DEV_READ_INLINE
48/**
49 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
50 *
51 * @dev: device to read DT property from
52 * @propname: name of the property to read from
53 * @def: default value to return if the property has no value
54 * @return property value, or @def if not found
55 */
56int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
Simon Glass34ef0c22017-05-18 20:09:03 -060057
58/**
59 * dev_read_string() - Read a string from a device's DT property
60 *
61 * @dev: device to read DT property from
62 * @propname: name of the property to read
63 * @return string from property value, or NULL if there is no such property
64 */
Simon Glasscfdae5c2017-05-18 20:09:04 -060065const char *dev_read_string(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -060066
67/**
68 * dev_read_bool() - read a boolean value from a device's DT property
69 *
70 * @dev: device to read DT property from
71 * @propname: name of property to read
72 * @return true if property is present (meaning true), false if not present
73 */
Simon Glasscfdae5c2017-05-18 20:09:04 -060074bool dev_read_bool(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -060075
76/**
77 * dev_read_subnode() - find a named subnode of a device
78 *
79 * @dev: device whose DT node contains the subnode
80 * @subnode_name: name of subnode to find
81 * @return reference to subnode (which can be invalid if there is no such
82 * subnode)
83 */
Simon Glasscfdae5c2017-05-18 20:09:04 -060084ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
Simon Glass34ef0c22017-05-18 20:09:03 -060085
86/**
87 * dev_read_size() - read the size of a property
88 *
89 * @dev: device to check
90 * @propname: property to check
91 * @return size of property if present, or -EINVAL if not
92 */
Simon Glasscfdae5c2017-05-18 20:09:04 -060093int dev_read_size(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -060094
95/**
96 * dev_read_addr_index() - Get the indexed reg property of a device
97 *
98 * @dev: Device to read from
99 * @index: the 'reg' property can hold a list of <addr, size> pairs
100 * and @index is used to select which one is required
101 *
102 * @return address or FDT_ADDR_T_NONE if not found
103 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600104fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
Simon Glass34ef0c22017-05-18 20:09:03 -0600105
106/**
107 * dev_read_addr() - Get the reg property of a device
108 *
109 * @dev: Device to read from
110 *
111 * @return address or FDT_ADDR_T_NONE if not found
112 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600113fdt_addr_t dev_read_addr(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600114
115/**
116 * dev_read_addr_size() - get address and size from a device property
117 *
118 * This does no address translation. It simply reads an property that contains
119 * an address and a size value, one after the other.
120 *
121 * @dev: Device to read from
122 * @propname: property to read
123 * @sizep: place to put size value (on success)
124 * @return address value, or FDT_ADDR_T_NONE on error
125 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600126fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
127 fdt_size_t *sizep);
Simon Glass34ef0c22017-05-18 20:09:03 -0600128
129/**
130 * dev_read_name() - get the name of a device's node
131 *
132 * @node: valid node to look up
133 * @return name of node
134 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600135const char *dev_read_name(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600136
137/**
138 * dev_read_stringlist_search() - find string in a string list and return index
139 *
140 * Note that it is possible for this function to succeed on property values
141 * that are not NUL-terminated. That's because the function will stop after
142 * finding the first occurrence of @string. This can for example happen with
143 * small-valued cell properties, such as #address-cells, when searching for
144 * the empty string.
145 *
146 * @dev: device to check
147 * @propname: name of the property containing the string list
148 * @string: string to look up in the string list
149 *
150 * @return:
151 * the index of the string in the list of strings
152 * -ENODATA if the property is not found
153 * -EINVAL on some other error
154 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600155int dev_read_stringlist_search(struct udevice *dev, const char *property,
156 const char *string);
Simon Glass34ef0c22017-05-18 20:09:03 -0600157
158/**
159 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
160 *
161 * This function is useful to parse lists of phandles and their arguments.
162 * Returns 0 on success and fills out_args, on error returns appropriate
163 * errno value.
164 *
165 * Caller is responsible to call of_node_put() on the returned out_args->np
166 * pointer.
167 *
168 * Example:
169 *
170 * phandle1: node1 {
171 * #list-cells = <2>;
172 * }
173 *
174 * phandle2: node2 {
175 * #list-cells = <1>;
176 * }
177 *
178 * node3 {
179 * list = <&phandle1 1 2 &phandle2 3>;
180 * }
181 *
182 * To get a device_node of the `node2' node you may call this:
183 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
184 *
185 * @dev: device whose node containing a list
186 * @list_name: property name that contains a list
187 * @cells_name: property name that specifies phandles' arguments count
188 * @cells_count: Cell count to use if @cells_name is NULL
189 * @index: index of a phandle to parse out
190 * @out_args: optional pointer to output arguments structure (will be filled)
191 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
192 * @list_name does not exist, -EINVAL if a phandle was not found,
193 * @cells_name could not be found, the arguments were truncated or there
194 * were too many arguments.
195 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600196int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
197 const char *cells_name, int cell_count,
198 int index,
199 struct ofnode_phandle_args *out_args);
Simon Glass34ef0c22017-05-18 20:09:03 -0600200
201/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200202 * dev_count_phandle_with_args() - Return phandle number in a list
203 *
204 * This function is usefull to get phandle number contained in a property list.
205 * For example, this allows to allocate the right amount of memory to keep
206 * clock's reference contained into the "clocks" property.
207 *
208 *
209 * @dev: device whose node containing a list
210 * @list_name: property name that contains a list
211 * @cells_name: property name that specifies phandles' arguments count
212 * @Returns number of phandle found on success, on error returns appropriate
213 * errno value.
214 */
215
216int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
217 const char *cells_name);
218
219/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600220 * dev_read_addr_cells() - Get the number of address cells for a device's node
221 *
222 * This walks back up the tree to find the closest #address-cells property
223 * which controls the given node.
224 *
225 * @dev: devioe to check
226 * @return number of address cells this node uses
227 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600228int dev_read_addr_cells(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600229
230/**
231 * dev_read_size_cells() - Get the number of size cells for a device's node
232 *
233 * This walks back up the tree to find the closest #size-cells property
234 * which controls the given node.
235 *
236 * @dev: devioe to check
237 * @return number of size cells this node uses
238 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600239int dev_read_size_cells(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600240
241/**
Simon Glass4191dc12017-06-12 06:21:31 -0600242 * dev_read_addr_cells() - Get the address cells property in a node
243 *
244 * This function matches fdt_address_cells().
245 *
246 * @dev: devioe to check
247 * @return number of address cells this node uses
248 */
249int dev_read_simple_addr_cells(struct udevice *dev);
250
251/**
252 * dev_read_size_cells() - Get the size cells property in a node
253 *
254 * This function matches fdt_size_cells().
255 *
256 * @dev: devioe to check
257 * @return number of size cells this node uses
258 */
259int dev_read_simple_size_cells(struct udevice *dev);
260
261/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600262 * dev_read_phandle() - Get the phandle from a device
263 *
264 * @dev: device to check
265 * @return phandle (1 or greater), or 0 if no phandle or other error
266 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600267int dev_read_phandle(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600268
269/**
270 * dev_read_prop()- - read a property from a device's node
271 *
272 * @dev: device to check
273 * @propname: property to read
274 * @lenp: place to put length on success
275 * @return pointer to property, or NULL if not found
276 */
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900277const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
Simon Glass34ef0c22017-05-18 20:09:03 -0600278
279/**
280 * dev_read_alias_seq() - Get the alias sequence number of a node
281 *
282 * This works out whether a node is pointed to by an alias, and if so, the
283 * sequence number of that alias. Aliases are of the form <base><num> where
284 * <num> is the sequence number. For example spi2 would be sequence number 2.
285 *
286 * @dev: device to look up
287 * @devnump: set to the sequence number if one is found
288 * @return 0 if a sequence was found, -ve if not
289 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600290int dev_read_alias_seq(struct udevice *dev, int *devnump);
Simon Glass34ef0c22017-05-18 20:09:03 -0600291
292/**
293 * dev_read_u32_array() - Find and read an array of 32 bit integers
294 *
295 * Search for a property in a device node and read 32-bit value(s) from
296 * it.
297 *
298 * The out_values is modified only if a valid u32 value can be decoded.
299 *
300 * @dev: device to look up
301 * @propname: name of the property to read
302 * @out_values: pointer to return value, modified only if return value is 0
303 * @sz: number of array elements to read
304 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
305 * property does not have a value, and -EOVERFLOW if the property data isn't
306 * large enough.
307 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600308int dev_read_u32_array(struct udevice *dev, const char *propname,
309 u32 *out_values, size_t sz);
Simon Glass34ef0c22017-05-18 20:09:03 -0600310
311/**
312 * dev_read_first_subnode() - find the first subnode of a device's node
313 *
314 * @dev: device to look up
315 * @return reference to the first subnode (which can be invalid if the device's
316 * node has no subnodes)
317 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600318ofnode dev_read_first_subnode(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600319
320/**
321 * ofnode_next_subnode() - find the next sibling of a subnode
322 *
323 * @node: valid reference to previous node (sibling)
324 * @return reference to the next subnode (which can be invalid if the node
325 * has no more siblings)
326 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600327ofnode dev_read_next_subnode(ofnode node);
Simon Glass34ef0c22017-05-18 20:09:03 -0600328
329/**
330 * dev_read_u8_array_ptr() - find an 8-bit array
331 *
332 * Look up a device's node property and return a pointer to its contents as a
333 * byte array of given length. The property must have at least enough data
334 * for the array (count bytes). It may have more, but this will be ignored.
335 * The data is not copied.
336 *
337 * @dev: device to look up
338 * @propname: name of property to find
339 * @sz: number of array elements
340 * @return pointer to byte array if found, or NULL if the property is not
341 * found or there is not enough data
342 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600343const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
344 size_t sz);
345
Simon Glassfa031f82017-06-12 06:21:30 -0600346/**
347 * dev_read_enabled() - check whether a node is enabled
348 *
349 * This looks for a 'status' property. If this exists, then returns 1 if
350 * the status is 'ok' and 0 otherwise. If there is no status property,
351 * it returns 1 on the assumption that anything mentioned should be enabled
352 * by default.
353 *
354 * @dev: device to examine
355 * @return integer value 0 (not enabled) or 1 (enabled)
356 */
357int dev_read_enabled(struct udevice *dev);
358
Simon Glassf7bfcc42017-07-25 08:29:55 -0600359/**
360 * dev_read_resource() - obtain an indexed resource from a device.
361 *
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900362 * @dev: device to examine
Simon Glassf7bfcc42017-07-25 08:29:55 -0600363 * @index index of the resource to retrieve (0 = first)
364 * @res returns the resource
365 * @return 0 if ok, negative on error
366 */
367int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
368
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900369/**
370 * dev_read_resource_byname() - obtain a named resource from a device.
371 *
372 * @dev: device to examine
373 * @name: name of the resource to retrieve
374 * @res: returns the resource
375 * @return 0 if ok, negative on error
376 */
377int dev_read_resource_byname(struct udevice *dev, const char *name,
378 struct resource *res);
379
Simon Glasscfdae5c2017-05-18 20:09:04 -0600380#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
381
382static inline int dev_read_u32_default(struct udevice *dev,
383 const char *propname, int def)
384{
385 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
386}
387
388static inline const char *dev_read_string(struct udevice *dev,
389 const char *propname)
390{
391 return ofnode_read_string(dev_ofnode(dev), propname);
392}
393
394static inline bool dev_read_bool(struct udevice *dev, const char *propname)
395{
396 return ofnode_read_bool(dev_ofnode(dev), propname);
397}
398
399static inline ofnode dev_read_subnode(struct udevice *dev,
400 const char *subbnode_name)
401{
402 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
403}
404
405static inline int dev_read_size(struct udevice *dev, const char *propname)
406{
407 return ofnode_read_size(dev_ofnode(dev), propname);
408}
409
410static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
411{
412 return devfdt_get_addr_index(dev, index);
413}
414
415static inline fdt_addr_t dev_read_addr(struct udevice *dev)
416{
417 return devfdt_get_addr(dev);
418}
419
420static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
421 const char *propname,
422 fdt_size_t *sizep)
423{
424 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
425}
426
427static inline const char *dev_read_name(struct udevice *dev)
428{
429 return ofnode_get_name(dev_ofnode(dev));
430}
431
432static inline int dev_read_stringlist_search(struct udevice *dev,
433 const char *propname,
434 const char *string)
435{
436 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
437}
438
439static inline int dev_read_phandle_with_args(struct udevice *dev,
440 const char *list_name, const char *cells_name, int cell_count,
441 int index, struct ofnode_phandle_args *out_args)
442{
443 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
444 cells_name, cell_count, index,
445 out_args);
446}
447
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200448static inline int dev_count_phandle_with_args(struct udevice *dev,
449 const char *list_name, const char *cells_name)
450{
451 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
452 cells_name);
453}
454
Simon Glasscfdae5c2017-05-18 20:09:04 -0600455static inline int dev_read_addr_cells(struct udevice *dev)
456{
Simon Glass4191dc12017-06-12 06:21:31 -0600457 /* NOTE: this call should walk up the parent stack */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600458 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
459}
460
461static inline int dev_read_size_cells(struct udevice *dev)
462{
Simon Glass4191dc12017-06-12 06:21:31 -0600463 /* NOTE: this call should walk up the parent stack */
464 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
465}
466
467static inline int dev_read_simple_addr_cells(struct udevice *dev)
468{
469 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
470}
471
472static inline int dev_read_simple_size_cells(struct udevice *dev)
473{
Simon Glasscfdae5c2017-05-18 20:09:04 -0600474 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
475}
476
477static inline int dev_read_phandle(struct udevice *dev)
478{
479 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
480}
481
Masahiro Yamadabaebec12017-07-17 12:18:39 +0900482static inline const void *dev_read_prop(struct udevice *dev,
483 const char *propname, int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600484{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900485 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600486}
487
488static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
489{
490 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
491 dev_of_offset(dev), devnump);
492}
493
494static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
495 u32 *out_values, size_t sz)
496{
497 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
498}
499
500static inline ofnode dev_read_first_subnode(struct udevice *dev)
501{
502 return ofnode_first_subnode(dev_ofnode(dev));
503}
504
505static inline ofnode dev_read_next_subnode(ofnode node)
506{
507 return ofnode_next_subnode(node);
508}
509
Simon Glass34ef0c22017-05-18 20:09:03 -0600510static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
511 const char *propname, size_t sz)
512{
513 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
514}
515
Simon Glassfa031f82017-06-12 06:21:30 -0600516static inline int dev_read_enabled(struct udevice *dev)
517{
518 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
519}
520
Simon Glassf7bfcc42017-07-25 08:29:55 -0600521static inline int dev_read_resource(struct udevice *dev, uint index,
522 struct resource *res)
523{
524 return ofnode_read_resource(dev_ofnode(dev), index, res);
525}
526
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900527static inline int dev_read_resource_byname(struct udevice *dev,
528 const char *name,
529 struct resource *res)
530{
531 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
532}
533
Simon Glass34ef0c22017-05-18 20:09:03 -0600534#endif /* CONFIG_DM_DEV_READ_INLINE */
535
536/**
537 * dev_for_each_subnode() - Helper function to iterate through subnodes
538 *
539 * This creates a for() loop which works through the subnodes in a device's
540 * device-tree node.
541 *
542 * @subnode: ofnode holding the current subnode
543 * @dev: device to use for interation (struct udevice *)
544 */
545#define dev_for_each_subnode(subnode, dev) \
546 for (subnode = dev_read_first_subnode(dev); \
547 ofnode_valid(subnode); \
548 subnode = ofnode_next_subnode(subnode))
549
550#endif