blob: b86a2f5feceb862967e912519003ff900a641b16 [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 Glass170e17c2017-06-12 06:21:29 -060047/**
48 * dev_read_resource() - obtain an indexed resource from a device.
49 *
50 * @dev: devuce to examine
51 * @index index of the resource to retrieve (0 = first)
52 * @res returns the resource
53 * @return 0 if ok, negative on error
54 */
55int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
56
Simon Glasscfdae5c2017-05-18 20:09:04 -060057#ifndef CONFIG_DM_DEV_READ_INLINE
58/**
59 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
60 *
61 * @dev: device to read DT property from
62 * @propname: name of the property to read from
63 * @def: default value to return if the property has no value
64 * @return property value, or @def if not found
65 */
66int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
Simon Glass34ef0c22017-05-18 20:09:03 -060067
68/**
69 * dev_read_string() - Read a string from a device's DT property
70 *
71 * @dev: device to read DT property from
72 * @propname: name of the property to read
73 * @return string from property value, or NULL if there is no such property
74 */
Simon Glasscfdae5c2017-05-18 20:09:04 -060075const char *dev_read_string(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -060076
77/**
78 * dev_read_bool() - read a boolean value from a device's DT property
79 *
80 * @dev: device to read DT property from
81 * @propname: name of property to read
82 * @return true if property is present (meaning true), false if not present
83 */
Simon Glasscfdae5c2017-05-18 20:09:04 -060084bool dev_read_bool(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -060085
86/**
87 * dev_read_subnode() - find a named subnode of a device
88 *
89 * @dev: device whose DT node contains the subnode
90 * @subnode_name: name of subnode to find
91 * @return reference to subnode (which can be invalid if there is no such
92 * subnode)
93 */
Simon Glasscfdae5c2017-05-18 20:09:04 -060094ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
Simon Glass34ef0c22017-05-18 20:09:03 -060095
96/**
97 * dev_read_size() - read the size of a property
98 *
99 * @dev: device to check
100 * @propname: property to check
101 * @return size of property if present, or -EINVAL if not
102 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600103int dev_read_size(struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-05-18 20:09:03 -0600104
105/**
106 * dev_read_addr_index() - Get the indexed reg property of a device
107 *
108 * @dev: Device to read from
109 * @index: the 'reg' property can hold a list of <addr, size> pairs
110 * and @index is used to select which one is required
111 *
112 * @return address or FDT_ADDR_T_NONE if not found
113 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600114fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
Simon Glass34ef0c22017-05-18 20:09:03 -0600115
116/**
117 * dev_read_addr() - Get the reg property of a device
118 *
119 * @dev: Device to read from
120 *
121 * @return address or FDT_ADDR_T_NONE if not found
122 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600123fdt_addr_t dev_read_addr(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600124
125/**
126 * dev_read_addr_size() - get address and size from a device property
127 *
128 * This does no address translation. It simply reads an property that contains
129 * an address and a size value, one after the other.
130 *
131 * @dev: Device to read from
132 * @propname: property to read
133 * @sizep: place to put size value (on success)
134 * @return address value, or FDT_ADDR_T_NONE on error
135 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600136fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
137 fdt_size_t *sizep);
Simon Glass34ef0c22017-05-18 20:09:03 -0600138
139/**
140 * dev_read_name() - get the name of a device's node
141 *
142 * @node: valid node to look up
143 * @return name of node
144 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600145const char *dev_read_name(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600146
147/**
148 * dev_read_stringlist_search() - find string in a string list and return index
149 *
150 * Note that it is possible for this function to succeed on property values
151 * that are not NUL-terminated. That's because the function will stop after
152 * finding the first occurrence of @string. This can for example happen with
153 * small-valued cell properties, such as #address-cells, when searching for
154 * the empty string.
155 *
156 * @dev: device to check
157 * @propname: name of the property containing the string list
158 * @string: string to look up in the string list
159 *
160 * @return:
161 * the index of the string in the list of strings
162 * -ENODATA if the property is not found
163 * -EINVAL on some other error
164 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600165int dev_read_stringlist_search(struct udevice *dev, const char *property,
166 const char *string);
Simon Glass34ef0c22017-05-18 20:09:03 -0600167
168/**
169 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
170 *
171 * This function is useful to parse lists of phandles and their arguments.
172 * Returns 0 on success and fills out_args, on error returns appropriate
173 * errno value.
174 *
175 * Caller is responsible to call of_node_put() on the returned out_args->np
176 * pointer.
177 *
178 * Example:
179 *
180 * phandle1: node1 {
181 * #list-cells = <2>;
182 * }
183 *
184 * phandle2: node2 {
185 * #list-cells = <1>;
186 * }
187 *
188 * node3 {
189 * list = <&phandle1 1 2 &phandle2 3>;
190 * }
191 *
192 * To get a device_node of the `node2' node you may call this:
193 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
194 *
195 * @dev: device whose node containing a list
196 * @list_name: property name that contains a list
197 * @cells_name: property name that specifies phandles' arguments count
198 * @cells_count: Cell count to use if @cells_name is NULL
199 * @index: index of a phandle to parse out
200 * @out_args: optional pointer to output arguments structure (will be filled)
201 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
202 * @list_name does not exist, -EINVAL if a phandle was not found,
203 * @cells_name could not be found, the arguments were truncated or there
204 * were too many arguments.
205 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600206int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
207 const char *cells_name, int cell_count,
208 int index,
209 struct ofnode_phandle_args *out_args);
Simon Glass34ef0c22017-05-18 20:09:03 -0600210
211/**
212 * dev_read_addr_cells() - Get the number of address cells for a device's node
213 *
214 * This walks back up the tree to find the closest #address-cells property
215 * which controls the given node.
216 *
217 * @dev: devioe to check
218 * @return number of address cells this node uses
219 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600220int dev_read_addr_cells(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600221
222/**
223 * dev_read_size_cells() - Get the number of size cells for a device's node
224 *
225 * This walks back up the tree to find the closest #size-cells property
226 * which controls the given node.
227 *
228 * @dev: devioe to check
229 * @return number of size cells this node uses
230 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600231int dev_read_size_cells(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600232
233/**
Simon Glass4191dc12017-06-12 06:21:31 -0600234 * dev_read_addr_cells() - Get the address cells property in a node
235 *
236 * This function matches fdt_address_cells().
237 *
238 * @dev: devioe to check
239 * @return number of address cells this node uses
240 */
241int dev_read_simple_addr_cells(struct udevice *dev);
242
243/**
244 * dev_read_size_cells() - Get the size cells property in a node
245 *
246 * This function matches fdt_size_cells().
247 *
248 * @dev: devioe to check
249 * @return number of size cells this node uses
250 */
251int dev_read_simple_size_cells(struct udevice *dev);
252
253/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600254 * dev_read_phandle() - Get the phandle from a device
255 *
256 * @dev: device to check
257 * @return phandle (1 or greater), or 0 if no phandle or other error
258 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600259int dev_read_phandle(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600260
261/**
262 * dev_read_prop()- - read a property from a device's node
263 *
264 * @dev: device to check
265 * @propname: property to read
266 * @lenp: place to put length on success
267 * @return pointer to property, or NULL if not found
268 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600269const u32 *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
Simon Glass34ef0c22017-05-18 20:09:03 -0600270
271/**
272 * dev_read_alias_seq() - Get the alias sequence number of a node
273 *
274 * This works out whether a node is pointed to by an alias, and if so, the
275 * sequence number of that alias. Aliases are of the form <base><num> where
276 * <num> is the sequence number. For example spi2 would be sequence number 2.
277 *
278 * @dev: device to look up
279 * @devnump: set to the sequence number if one is found
280 * @return 0 if a sequence was found, -ve if not
281 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600282int dev_read_alias_seq(struct udevice *dev, int *devnump);
Simon Glass34ef0c22017-05-18 20:09:03 -0600283
284/**
285 * dev_read_u32_array() - Find and read an array of 32 bit integers
286 *
287 * Search for a property in a device node and read 32-bit value(s) from
288 * it.
289 *
290 * The out_values is modified only if a valid u32 value can be decoded.
291 *
292 * @dev: device to look up
293 * @propname: name of the property to read
294 * @out_values: pointer to return value, modified only if return value is 0
295 * @sz: number of array elements to read
296 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
297 * property does not have a value, and -EOVERFLOW if the property data isn't
298 * large enough.
299 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600300int dev_read_u32_array(struct udevice *dev, const char *propname,
301 u32 *out_values, size_t sz);
Simon Glass34ef0c22017-05-18 20:09:03 -0600302
303/**
304 * dev_read_first_subnode() - find the first subnode of a device's node
305 *
306 * @dev: device to look up
307 * @return reference to the first subnode (which can be invalid if the device's
308 * node has no subnodes)
309 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600310ofnode dev_read_first_subnode(struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600311
312/**
313 * ofnode_next_subnode() - find the next sibling of a subnode
314 *
315 * @node: valid reference to previous node (sibling)
316 * @return reference to the next subnode (which can be invalid if the node
317 * has no more siblings)
318 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600319ofnode dev_read_next_subnode(ofnode node);
Simon Glass34ef0c22017-05-18 20:09:03 -0600320
321/**
322 * dev_read_u8_array_ptr() - find an 8-bit array
323 *
324 * Look up a device's node property and return a pointer to its contents as a
325 * byte array of given length. The property must have at least enough data
326 * for the array (count bytes). It may have more, but this will be ignored.
327 * The data is not copied.
328 *
329 * @dev: device to look up
330 * @propname: name of property to find
331 * @sz: number of array elements
332 * @return pointer to byte array if found, or NULL if the property is not
333 * found or there is not enough data
334 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600335const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
336 size_t sz);
337
Simon Glassfa031f82017-06-12 06:21:30 -0600338/**
339 * dev_read_enabled() - check whether a node is enabled
340 *
341 * This looks for a 'status' property. If this exists, then returns 1 if
342 * the status is 'ok' and 0 otherwise. If there is no status property,
343 * it returns 1 on the assumption that anything mentioned should be enabled
344 * by default.
345 *
346 * @dev: device to examine
347 * @return integer value 0 (not enabled) or 1 (enabled)
348 */
349int dev_read_enabled(struct udevice *dev);
350
Simon Glasscfdae5c2017-05-18 20:09:04 -0600351#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
352
353static inline int dev_read_u32_default(struct udevice *dev,
354 const char *propname, int def)
355{
356 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
357}
358
359static inline const char *dev_read_string(struct udevice *dev,
360 const char *propname)
361{
362 return ofnode_read_string(dev_ofnode(dev), propname);
363}
364
365static inline bool dev_read_bool(struct udevice *dev, const char *propname)
366{
367 return ofnode_read_bool(dev_ofnode(dev), propname);
368}
369
370static inline ofnode dev_read_subnode(struct udevice *dev,
371 const char *subbnode_name)
372{
373 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
374}
375
376static inline int dev_read_size(struct udevice *dev, const char *propname)
377{
378 return ofnode_read_size(dev_ofnode(dev), propname);
379}
380
381static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
382{
383 return devfdt_get_addr_index(dev, index);
384}
385
386static inline fdt_addr_t dev_read_addr(struct udevice *dev)
387{
388 return devfdt_get_addr(dev);
389}
390
391static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
392 const char *propname,
393 fdt_size_t *sizep)
394{
395 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
396}
397
398static inline const char *dev_read_name(struct udevice *dev)
399{
400 return ofnode_get_name(dev_ofnode(dev));
401}
402
403static inline int dev_read_stringlist_search(struct udevice *dev,
404 const char *propname,
405 const char *string)
406{
407 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
408}
409
410static inline int dev_read_phandle_with_args(struct udevice *dev,
411 const char *list_name, const char *cells_name, int cell_count,
412 int index, struct ofnode_phandle_args *out_args)
413{
414 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
415 cells_name, cell_count, index,
416 out_args);
417}
418
419static inline int dev_read_addr_cells(struct udevice *dev)
420{
Simon Glass4191dc12017-06-12 06:21:31 -0600421 /* NOTE: this call should walk up the parent stack */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600422 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
423}
424
425static inline int dev_read_size_cells(struct udevice *dev)
426{
Simon Glass4191dc12017-06-12 06:21:31 -0600427 /* NOTE: this call should walk up the parent stack */
428 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
429}
430
431static inline int dev_read_simple_addr_cells(struct udevice *dev)
432{
433 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
434}
435
436static inline int dev_read_simple_size_cells(struct udevice *dev)
437{
Simon Glasscfdae5c2017-05-18 20:09:04 -0600438 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
439}
440
441static inline int dev_read_phandle(struct udevice *dev)
442{
443 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
444}
445
446static inline const u32 *dev_read_prop(struct udevice *dev,
447 const char *propname, int *lenp)
448{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900449 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600450}
451
452static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
453{
454 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
455 dev_of_offset(dev), devnump);
456}
457
458static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
459 u32 *out_values, size_t sz)
460{
461 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
462}
463
464static inline ofnode dev_read_first_subnode(struct udevice *dev)
465{
466 return ofnode_first_subnode(dev_ofnode(dev));
467}
468
469static inline ofnode dev_read_next_subnode(ofnode node)
470{
471 return ofnode_next_subnode(node);
472}
473
Simon Glass34ef0c22017-05-18 20:09:03 -0600474static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
475 const char *propname, size_t sz)
476{
477 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
478}
479
Simon Glassfa031f82017-06-12 06:21:30 -0600480static inline int dev_read_enabled(struct udevice *dev)
481{
482 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
483}
484
Simon Glass34ef0c22017-05-18 20:09:03 -0600485#endif /* CONFIG_DM_DEV_READ_INLINE */
486
487/**
488 * dev_for_each_subnode() - Helper function to iterate through subnodes
489 *
490 * This creates a for() loop which works through the subnodes in a device's
491 * device-tree node.
492 *
493 * @subnode: ofnode holding the current subnode
494 * @dev: device to use for interation (struct udevice *)
495 */
496#define dev_for_each_subnode(subnode, dev) \
497 for (subnode = dev_read_first_subnode(dev); \
498 ofnode_valid(subnode); \
499 subnode = ofnode_next_subnode(subnode))
500
501#endif