14984de2bSSimon Glass /*
24984de2bSSimon Glass * Copyright (c) 2017 Google, Inc
34984de2bSSimon Glass * Written by Simon Glass <sjg@chromium.org>
44984de2bSSimon Glass *
54984de2bSSimon Glass * SPDX-License-Identifier: GPL-2.0+
64984de2bSSimon Glass */
74984de2bSSimon Glass
84984de2bSSimon Glass #ifndef _DM_OFNODE_H
94984de2bSSimon Glass #define _DM_OFNODE_H
104984de2bSSimon Glass
119e512045SSimon Glass /* TODO(sjg@chromium.org): Drop fdtdec.h include */
129e512045SSimon Glass #include <fdtdec.h>
139e512045SSimon Glass #include <dm/of.h>
149e512045SSimon Glass
159e512045SSimon Glass /* Enable checks to protect against invalid calls */
169e512045SSimon Glass #undef OF_CHECKS
179e512045SSimon Glass
18dcf98852SSimon Glass struct resource;
19dcf98852SSimon Glass
204984de2bSSimon Glass /**
214984de2bSSimon Glass * ofnode - reference to a device tree node
224984de2bSSimon Glass *
234984de2bSSimon Glass * This union can hold either a straightforward pointer to a struct device_node
244984de2bSSimon Glass * in the live device tree, or an offset within the flat device tree. In the
254984de2bSSimon Glass * latter case, the pointer value is just the integer offset within the flat DT.
264984de2bSSimon Glass *
274984de2bSSimon Glass * Thus we can reference nodes in both the live tree (once available) and the
284984de2bSSimon Glass * flat tree (until then). Functions are available to translate between an
294984de2bSSimon Glass * ofnode and either an offset or a struct device_node *.
304984de2bSSimon Glass *
314984de2bSSimon Glass * The reference can also hold a null offset, in which case the pointer value
329e512045SSimon Glass * here is NULL. This corresponds to a struct device_node * value of
334984de2bSSimon Glass * NULL, or an offset of -1.
344984de2bSSimon Glass *
354984de2bSSimon Glass * There is no ambiguity as to whether ofnode holds an offset or a node
364984de2bSSimon Glass * pointer: when the live tree is active it holds a node pointer, otherwise it
374984de2bSSimon Glass * holds an offset. The value itself does not need to be unique and in theory
384984de2bSSimon Glass * the same value could point to a valid device node or a valid offset. We
394984de2bSSimon Glass * could arrange for a unique value to be used (e.g. by making the pointer
404984de2bSSimon Glass * point to an offset within the flat device tree in the case of an offset) but
414984de2bSSimon Glass * this increases code size slightly due to the subtraction. Since it offers no
424984de2bSSimon Glass * real benefit, the approach described here seems best.
434984de2bSSimon Glass *
444984de2bSSimon Glass * For now these points use constant types, since we don't allow writing
454984de2bSSimon Glass * the DT.
464984de2bSSimon Glass *
474984de2bSSimon Glass * @np: Pointer to device node, used for live tree
486c82a31eSBaruch Siach * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
494984de2bSSimon Glass * is not a really a pointer to a node: it is an offset value. See above.
504984de2bSSimon Glass */
514984de2bSSimon Glass typedef union ofnode_union {
524984de2bSSimon Glass const struct device_node *np; /* will be used for future live tree */
534984de2bSSimon Glass long of_offset;
544984de2bSSimon Glass } ofnode;
554984de2bSSimon Glass
569e512045SSimon Glass struct ofnode_phandle_args {
579e512045SSimon Glass ofnode node;
589e512045SSimon Glass int args_count;
599e512045SSimon Glass uint32_t args[OF_MAX_PHANDLE_ARGS];
609e512045SSimon Glass };
619e512045SSimon Glass
629e512045SSimon Glass /**
63*3cf48410SPatrick Delaunay * ofprop - reference to a property of a device tree node
64*3cf48410SPatrick Delaunay *
65*3cf48410SPatrick Delaunay * This struct hold the reference on one property of one node,
66*3cf48410SPatrick Delaunay * using struct ofnode and an offset within the flat device tree or either
67*3cf48410SPatrick Delaunay * a pointer to a struct property in the live device tree.
68*3cf48410SPatrick Delaunay *
69*3cf48410SPatrick Delaunay * Thus we can reference arguments in both the live tree and the flat tree.
70*3cf48410SPatrick Delaunay *
71*3cf48410SPatrick Delaunay * The property reference can also hold a null reference. This corresponds to
72*3cf48410SPatrick Delaunay * a struct property NULL pointer or an offset of -1.
73*3cf48410SPatrick Delaunay *
74*3cf48410SPatrick Delaunay * @node: Pointer to device node
75*3cf48410SPatrick Delaunay * @offset: Pointer into flat device tree, used for flat tree.
76*3cf48410SPatrick Delaunay * @prop: Pointer to property, used for live treee.
77*3cf48410SPatrick Delaunay */
78*3cf48410SPatrick Delaunay
79*3cf48410SPatrick Delaunay struct ofprop {
80*3cf48410SPatrick Delaunay ofnode node;
81*3cf48410SPatrick Delaunay union {
82*3cf48410SPatrick Delaunay int offset;
83*3cf48410SPatrick Delaunay const struct property *prop;
84*3cf48410SPatrick Delaunay };
85*3cf48410SPatrick Delaunay };
86*3cf48410SPatrick Delaunay
87*3cf48410SPatrick Delaunay /**
889e512045SSimon Glass * _ofnode_to_np() - convert an ofnode to a live DT node pointer
899e512045SSimon Glass *
909e512045SSimon Glass * This cannot be called if the reference contains an offset.
919e512045SSimon Glass *
929e512045SSimon Glass * @node: Reference containing struct device_node * (possibly invalid)
939e512045SSimon Glass * @return pointer to device node (can be NULL)
949e512045SSimon Glass */
ofnode_to_np(ofnode node)959e512045SSimon Glass static inline const struct device_node *ofnode_to_np(ofnode node)
969e512045SSimon Glass {
979e512045SSimon Glass #ifdef OF_CHECKS
989e512045SSimon Glass if (!of_live_active())
999e512045SSimon Glass return NULL;
1009e512045SSimon Glass #endif
1019e512045SSimon Glass return node.np;
1029e512045SSimon Glass }
1039e512045SSimon Glass
1044984de2bSSimon Glass /**
1054984de2bSSimon Glass * ofnode_to_offset() - convert an ofnode to a flat DT offset
1064984de2bSSimon Glass *
1074984de2bSSimon Glass * This cannot be called if the reference contains a node pointer.
1084984de2bSSimon Glass *
1094984de2bSSimon Glass * @node: Reference containing offset (possibly invalid)
1104984de2bSSimon Glass * @return DT offset (can be -1)
1114984de2bSSimon Glass */
ofnode_to_offset(ofnode node)1124984de2bSSimon Glass static inline int ofnode_to_offset(ofnode node)
1134984de2bSSimon Glass {
1149e512045SSimon Glass #ifdef OF_CHECKS
1159e512045SSimon Glass if (of_live_active())
1169e512045SSimon Glass return -1;
1179e512045SSimon Glass #endif
1184984de2bSSimon Glass return node.of_offset;
1194984de2bSSimon Glass }
1204984de2bSSimon Glass
1214984de2bSSimon Glass /**
1224984de2bSSimon Glass * ofnode_valid() - check if an ofnode is valid
1234984de2bSSimon Glass *
1244984de2bSSimon Glass * @return true if the reference contains a valid ofnode, false if it is NULL
1254984de2bSSimon Glass */
ofnode_valid(ofnode node)1264984de2bSSimon Glass static inline bool ofnode_valid(ofnode node)
1274984de2bSSimon Glass {
1289e512045SSimon Glass if (of_live_active())
1299e512045SSimon Glass return node.np != NULL;
1309e512045SSimon Glass else
1314984de2bSSimon Glass return node.of_offset != -1;
1324984de2bSSimon Glass }
1334984de2bSSimon Glass
1344984de2bSSimon Glass /**
1354984de2bSSimon Glass * offset_to_ofnode() - convert a DT offset to an ofnode
1364984de2bSSimon Glass *
1374984de2bSSimon Glass * @of_offset: DT offset (either valid, or -1)
1384984de2bSSimon Glass * @return reference to the associated DT offset
1394984de2bSSimon Glass */
offset_to_ofnode(int of_offset)1404984de2bSSimon Glass static inline ofnode offset_to_ofnode(int of_offset)
1414984de2bSSimon Glass {
1424984de2bSSimon Glass ofnode node;
1434984de2bSSimon Glass
1449e512045SSimon Glass if (of_live_active())
1459e512045SSimon Glass node.np = NULL;
1469e512045SSimon Glass else
1474984de2bSSimon Glass node.of_offset = of_offset;
1484984de2bSSimon Glass
1494984de2bSSimon Glass return node;
1504984de2bSSimon Glass }
1514984de2bSSimon Glass
1524984de2bSSimon Glass /**
1539e512045SSimon Glass * np_to_ofnode() - convert a node pointer to an ofnode
1549e512045SSimon Glass *
1559e512045SSimon Glass * @np: Live node pointer (can be NULL)
1569e512045SSimon Glass * @return reference to the associated node pointer
1579e512045SSimon Glass */
np_to_ofnode(const struct device_node * np)1589e512045SSimon Glass static inline ofnode np_to_ofnode(const struct device_node *np)
1599e512045SSimon Glass {
1609e512045SSimon Glass ofnode node;
1619e512045SSimon Glass
1629e512045SSimon Glass node.np = np;
1639e512045SSimon Glass
1649e512045SSimon Glass return node;
1659e512045SSimon Glass }
1669e512045SSimon Glass
1679e512045SSimon Glass /**
1689e512045SSimon Glass * ofnode_is_np() - check if a reference is a node pointer
1699e512045SSimon Glass *
1709e512045SSimon Glass * This function associated that if there is a valid live tree then all
1719e512045SSimon Glass * references will use it. This is because using the flat DT when the live tree
1729e512045SSimon Glass * is valid is not permitted.
1739e512045SSimon Glass *
1749e512045SSimon Glass * @node: reference to check (possibly invalid)
1759e512045SSimon Glass * @return true if the reference is a live node pointer, false if it is a DT
1769e512045SSimon Glass * offset
1779e512045SSimon Glass */
ofnode_is_np(ofnode node)1789e512045SSimon Glass static inline bool ofnode_is_np(ofnode node)
1799e512045SSimon Glass {
1809e512045SSimon Glass #ifdef OF_CHECKS
1819e512045SSimon Glass /*
1829e512045SSimon Glass * Check our assumption that flat tree offsets are not used when a
1839e512045SSimon Glass * live tree is in use.
1849e512045SSimon Glass */
1859e512045SSimon Glass assert(!ofnode_valid(node) ||
1869e512045SSimon Glass (of_live_active() ? _ofnode_to_np(node)
1879e512045SSimon Glass : _ofnode_to_np(node)));
1889e512045SSimon Glass #endif
1899e512045SSimon Glass return of_live_active() && ofnode_valid(node);
1909e512045SSimon Glass }
1919e512045SSimon Glass
1929e512045SSimon Glass /**
1934984de2bSSimon Glass * ofnode_equal() - check if two references are equal
1944984de2bSSimon Glass *
1954984de2bSSimon Glass * @return true if equal, else false
1964984de2bSSimon Glass */
ofnode_equal(ofnode ref1,ofnode ref2)1974984de2bSSimon Glass static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
1984984de2bSSimon Glass {
1994984de2bSSimon Glass /* We only need to compare the contents */
2004984de2bSSimon Glass return ref1.of_offset == ref2.of_offset;
2014984de2bSSimon Glass }
2024984de2bSSimon Glass
2039e512045SSimon Glass /**
2049e512045SSimon Glass * ofnode_null() - Obtain a null ofnode
2059e512045SSimon Glass *
2069e512045SSimon Glass * This returns an ofnode which points to no node. It works both with the flat
2079e512045SSimon Glass * tree and livetree.
2089e512045SSimon Glass */
ofnode_null(void)2099e512045SSimon Glass static inline ofnode ofnode_null(void)
2109e512045SSimon Glass {
2119e512045SSimon Glass ofnode node;
2129e512045SSimon Glass
2139e512045SSimon Glass if (of_live_active())
2149e512045SSimon Glass node.np = NULL;
2159e512045SSimon Glass else
2169e512045SSimon Glass node.of_offset = -1;
2179e512045SSimon Glass
2189e512045SSimon Glass return node;
2199e512045SSimon Glass }
2209e512045SSimon Glass
2219e512045SSimon Glass /**
2229e512045SSimon Glass * ofnode_read_u32() - Read a 32-bit integer from a property
2239e512045SSimon Glass *
2249e512045SSimon Glass * @ref: valid node reference to read property from
2259e512045SSimon Glass * @propname: name of the property to read from
2269e512045SSimon Glass * @outp: place to put value (if found)
2279e512045SSimon Glass * @return 0 if OK, -ve on error
2289e512045SSimon Glass */
2299e512045SSimon Glass int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
2309e512045SSimon Glass
2319e512045SSimon Glass /**
2329e512045SSimon Glass * ofnode_read_s32() - Read a 32-bit integer from a property
2339e512045SSimon Glass *
2349e512045SSimon Glass * @ref: valid node reference to read property from
2359e512045SSimon Glass * @propname: name of the property to read from
2369e512045SSimon Glass * @outp: place to put value (if found)
2379e512045SSimon Glass * @return 0 if OK, -ve on error
2389e512045SSimon Glass */
ofnode_read_s32(ofnode node,const char * propname,s32 * out_value)2399e512045SSimon Glass static inline int ofnode_read_s32(ofnode node, const char *propname,
2409e512045SSimon Glass s32 *out_value)
2419e512045SSimon Glass {
2429e512045SSimon Glass return ofnode_read_u32(node, propname, (u32 *)out_value);
2439e512045SSimon Glass }
2449e512045SSimon Glass
2459e512045SSimon Glass /**
2469e512045SSimon Glass * ofnode_read_u32_default() - Read a 32-bit integer from a property
2479e512045SSimon Glass *
2489e512045SSimon Glass * @ref: valid node reference to read property from
2499e512045SSimon Glass * @propname: name of the property to read from
2509e512045SSimon Glass * @def: default value to return if the property has no value
2519e512045SSimon Glass * @return property value, or @def if not found
2529e512045SSimon Glass */
2539e512045SSimon Glass int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
2549e512045SSimon Glass
2559e512045SSimon Glass /**
256d59cf5aeSJoseph Chen * ofnode_read_u64() - Read a 64-bit integer from a property
257d59cf5aeSJoseph Chen *
258d59cf5aeSJoseph Chen * @ref: valid node reference to read property from
259d59cf5aeSJoseph Chen * @propname: name of the property to read from
260d59cf5aeSJoseph Chen * @outp: place to put value (if found)
261d59cf5aeSJoseph Chen * @return 0 if OK, -ve on error
262d59cf5aeSJoseph Chen */
263d59cf5aeSJoseph Chen int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
264d59cf5aeSJoseph Chen
265d59cf5aeSJoseph Chen /**
2669e512045SSimon Glass * ofnode_read_s32_default() - Read a 32-bit integer from a property
2679e512045SSimon Glass *
2689e512045SSimon Glass * @ref: valid node reference to read property from
2699e512045SSimon Glass * @propname: name of the property to read from
2709e512045SSimon Glass * @def: default value to return if the property has no value
2719e512045SSimon Glass * @return property value, or @def if not found
2729e512045SSimon Glass */
2739e512045SSimon Glass int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
2749e512045SSimon Glass
2759e512045SSimon Glass /**
2769e512045SSimon Glass * ofnode_read_string() - Read a string from a property
2779e512045SSimon Glass *
2789e512045SSimon Glass * @ref: valid node reference to read property from
2799e512045SSimon Glass * @propname: name of the property to read
2809e512045SSimon Glass * @return string from property value, or NULL if there is no such property
2819e512045SSimon Glass */
2829e512045SSimon Glass const char *ofnode_read_string(ofnode node, const char *propname);
2839e512045SSimon Glass
2849e512045SSimon Glass /**
285bed77496SSimon Glass * ofnode_read_u32_array() - Find and read an array of 32 bit integers
2869e512045SSimon Glass *
2879e512045SSimon Glass * @node: valid node reference to read property from
2889e512045SSimon Glass * @propname: name of the property to read
2899e512045SSimon Glass * @out_values: pointer to return value, modified only if return value is 0
2909e512045SSimon Glass * @sz: number of array elements to read
2919e512045SSimon Glass *
2929e512045SSimon Glass * Search for a property in a device node and read 32-bit value(s) from
2939e512045SSimon Glass * it. Returns 0 on success, -EINVAL if the property does not exist,
2949e512045SSimon Glass * -ENODATA if property does not have a value, and -EOVERFLOW if the
2959e512045SSimon Glass * property data isn't large enough.
2969e512045SSimon Glass *
2979e512045SSimon Glass * The out_values is modified only if a valid u32 value can be decoded.
2989e512045SSimon Glass */
2999e512045SSimon Glass int ofnode_read_u32_array(ofnode node, const char *propname,
3009e512045SSimon Glass u32 *out_values, size_t sz);
3019e512045SSimon Glass
3029e512045SSimon Glass /**
30304539b46SJoseph Chen * ofnode_write_u32_array() - Find and write an array of 32 bit integers
30404539b46SJoseph Chen *
30504539b46SJoseph Chen * @node: valid node reference to read property from
30604539b46SJoseph Chen * @propname: name of the property to read
30704539b46SJoseph Chen * @values: pointer to update value, modified only if return value is 0
30804539b46SJoseph Chen * @sz: number of array elements to read
30904539b46SJoseph Chen * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
31004539b46SJoseph Chen * if property does not have a value, and -EOVERFLOW is longer than sz.
31104539b46SJoseph Chen */
31204539b46SJoseph Chen int ofnode_write_u32_array(ofnode node, const char *propname,
31304539b46SJoseph Chen u32 *values, size_t sz);
31404539b46SJoseph Chen
31504539b46SJoseph Chen /**
3169e512045SSimon Glass * ofnode_read_bool() - read a boolean value from a property
3179e512045SSimon Glass *
3189e512045SSimon Glass * @node: valid node reference to read property from
3199e512045SSimon Glass * @propname: name of property to read
3209e512045SSimon Glass * @return true if property is present (meaning true), false if not present
3219e512045SSimon Glass */
3229e512045SSimon Glass bool ofnode_read_bool(ofnode node, const char *propname);
3239e512045SSimon Glass
3249e512045SSimon Glass /**
3259e512045SSimon Glass * ofnode_find_subnode() - find a named subnode of a parent node
3269e512045SSimon Glass *
3279e512045SSimon Glass * @node: valid reference to parent node
3289e512045SSimon Glass * @subnode_name: name of subnode to find
3299e512045SSimon Glass * @return reference to subnode (which can be invalid if there is no such
3309e512045SSimon Glass * subnode)
3319e512045SSimon Glass */
3329e512045SSimon Glass ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
3339e512045SSimon Glass
3349e512045SSimon Glass /**
3359e512045SSimon Glass * ofnode_first_subnode() - find the first subnode of a parent node
3369e512045SSimon Glass *
3379e512045SSimon Glass * @node: valid reference to a valid parent node
3389e512045SSimon Glass * @return reference to the first subnode (which can be invalid if the parent
3399e512045SSimon Glass * node has no subnodes)
3409e512045SSimon Glass */
3419e512045SSimon Glass ofnode ofnode_first_subnode(ofnode node);
3429e512045SSimon Glass
3439e512045SSimon Glass /**
3449e512045SSimon Glass * ofnode_next_subnode() - find the next sibling of a subnode
3459e512045SSimon Glass *
3469e512045SSimon Glass * @node: valid reference to previous node (sibling)
3479e512045SSimon Glass * @return reference to the next subnode (which can be invalid if the node
3489e512045SSimon Glass * has no more siblings)
3499e512045SSimon Glass */
3509e512045SSimon Glass ofnode ofnode_next_subnode(ofnode node);
3519e512045SSimon Glass
3529e512045SSimon Glass /**
353e7ca7e39SPhilipp Tomsich * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
354e7ca7e39SPhilipp Tomsich *
355e7ca7e39SPhilipp Tomsich * @node: valid node to look up
356e7ca7e39SPhilipp Tomsich * @return ofnode reference of the parent node
357e7ca7e39SPhilipp Tomsich */
358e7ca7e39SPhilipp Tomsich ofnode ofnode_get_parent(ofnode node);
359e7ca7e39SPhilipp Tomsich
360e7ca7e39SPhilipp Tomsich /**
3619e512045SSimon Glass * ofnode_get_name() - get the name of a node
3629e512045SSimon Glass *
3639e512045SSimon Glass * @node: valid node to look up
3649e512045SSimon Glass * @return name or node
3659e512045SSimon Glass */
3669e512045SSimon Glass const char *ofnode_get_name(ofnode node);
3679e512045SSimon Glass
3689e512045SSimon Glass /**
369df5ceb01SKever Yang * ofnode_get_by_phandle() - get ofnode from phandle
370df5ceb01SKever Yang *
371df5ceb01SKever Yang * @phandle: phandle to look up
372df5ceb01SKever Yang * @return ofnode reference to the phandle
373df5ceb01SKever Yang */
374df5ceb01SKever Yang ofnode ofnode_get_by_phandle(uint phandle);
375df5ceb01SKever Yang
376df5ceb01SKever Yang /**
3779e512045SSimon Glass * ofnode_read_size() - read the size of a property
3789e512045SSimon Glass *
3799e512045SSimon Glass * @node: node to check
3809e512045SSimon Glass * @propname: property to check
3819e512045SSimon Glass * @return size of property if present, or -EINVAL if not
3829e512045SSimon Glass */
3839e512045SSimon Glass int ofnode_read_size(ofnode node, const char *propname);
3849e512045SSimon Glass
3859e512045SSimon Glass /**
386bed77496SSimon Glass * ofnode_get_addr_index() - get an address from a node
387bed77496SSimon Glass *
388bed77496SSimon Glass * This reads the register address from a node
389bed77496SSimon Glass *
390bed77496SSimon Glass * @node: node to read from
391bed77496SSimon Glass * @index: Index of address to read (0 for first)
392bed77496SSimon Glass * @return address, or FDT_ADDR_T_NONE if not present or invalid
393bed77496SSimon Glass */
394bed77496SSimon Glass phys_addr_t ofnode_get_addr_index(ofnode node, int index);
395bed77496SSimon Glass
396bed77496SSimon Glass /**
397bed77496SSimon Glass * ofnode_get_addr() - get an address from a node
398bed77496SSimon Glass *
399bed77496SSimon Glass * This reads the register address from a node
400bed77496SSimon Glass *
401bed77496SSimon Glass * @node: node to read from
402bed77496SSimon Glass * @return address, or FDT_ADDR_T_NONE if not present or invalid
403bed77496SSimon Glass */
404bed77496SSimon Glass phys_addr_t ofnode_get_addr(ofnode node);
405bed77496SSimon Glass
406bed77496SSimon Glass /**
4079e512045SSimon Glass * ofnode_stringlist_search() - find a string in a string list and return index
4089e512045SSimon Glass *
4099e512045SSimon Glass * Note that it is possible for this function to succeed on property values
4109e512045SSimon Glass * that are not NUL-terminated. That's because the function will stop after
4119e512045SSimon Glass * finding the first occurrence of @string. This can for example happen with
4129e512045SSimon Glass * small-valued cell properties, such as #address-cells, when searching for
4139e512045SSimon Glass * the empty string.
4149e512045SSimon Glass *
4159e512045SSimon Glass * @node: node to check
4169e512045SSimon Glass * @propname: name of the property containing the string list
4179e512045SSimon Glass * @string: string to look up in the string list
4189e512045SSimon Glass *
4199e512045SSimon Glass * @return:
4209e512045SSimon Glass * the index of the string in the list of strings
4219e512045SSimon Glass * -ENODATA if the property is not found
4229e512045SSimon Glass * -EINVAL on some other error
4239e512045SSimon Glass */
4249e512045SSimon Glass int ofnode_stringlist_search(ofnode node, const char *propname,
4259e512045SSimon Glass const char *string);
4269e512045SSimon Glass
4279e512045SSimon Glass /**
4288c293d6aSSimon Glass * ofnode_read_string_index() - obtain an indexed string from a string list
4299e512045SSimon Glass *
4309e512045SSimon Glass * Note that this will successfully extract strings from properties with
4319e512045SSimon Glass * non-NUL-terminated values. For example on small-valued cell properties
4329e512045SSimon Glass * this function will return the empty string.
4339e512045SSimon Glass *
4349e512045SSimon Glass * If non-NULL, the length of the string (on success) or a negative error-code
4359e512045SSimon Glass * (on failure) will be stored in the integer pointer to by lenp.
4369e512045SSimon Glass *
4379e512045SSimon Glass * @node: node to check
4389e512045SSimon Glass * @propname: name of the property containing the string list
4399e512045SSimon Glass * @index: index of the string to return
4409e512045SSimon Glass * @lenp: return location for the string length or an error code on failure
4419e512045SSimon Glass *
4429e512045SSimon Glass * @return:
4439e512045SSimon Glass * length of string, if found or -ve error value if not found
4449e512045SSimon Glass */
4459e512045SSimon Glass int ofnode_read_string_index(ofnode node, const char *propname, int index,
4469e512045SSimon Glass const char **outp);
4479e512045SSimon Glass
4489e512045SSimon Glass /**
4498c293d6aSSimon Glass * ofnode_read_string_count() - find the number of strings in a string list
4508c293d6aSSimon Glass *
4518c293d6aSSimon Glass * @node: node to check
4528c293d6aSSimon Glass * @propname: name of the property containing the string list
4538c293d6aSSimon Glass * @return:
4548c293d6aSSimon Glass * number of strings in the list, or -ve error value if not found
4558c293d6aSSimon Glass */
4568c293d6aSSimon Glass int ofnode_read_string_count(ofnode node, const char *property);
4578c293d6aSSimon Glass
4588c293d6aSSimon Glass /**
4599e512045SSimon Glass * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
4609e512045SSimon Glass *
4619e512045SSimon Glass * This function is useful to parse lists of phandles and their arguments.
4629e512045SSimon Glass * Returns 0 on success and fills out_args, on error returns appropriate
4639e512045SSimon Glass * errno value.
4649e512045SSimon Glass *
4659e512045SSimon Glass * Caller is responsible to call of_node_put() on the returned out_args->np
4669e512045SSimon Glass * pointer.
4679e512045SSimon Glass *
4689e512045SSimon Glass * Example:
4699e512045SSimon Glass *
4709e512045SSimon Glass * phandle1: node1 {
4719e512045SSimon Glass * #list-cells = <2>;
4729e512045SSimon Glass * }
4739e512045SSimon Glass *
4749e512045SSimon Glass * phandle2: node2 {
4759e512045SSimon Glass * #list-cells = <1>;
4769e512045SSimon Glass * }
4779e512045SSimon Glass *
4789e512045SSimon Glass * node3 {
4799e512045SSimon Glass * list = <&phandle1 1 2 &phandle2 3>;
4809e512045SSimon Glass * }
4819e512045SSimon Glass *
4829e512045SSimon Glass * To get a device_node of the `node2' node you may call this:
4839e512045SSimon Glass * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
4849e512045SSimon Glass *
4859e512045SSimon Glass * @node: device tree node containing a list
4869e512045SSimon Glass * @list_name: property name that contains a list
4879e512045SSimon Glass * @cells_name: property name that specifies phandles' arguments count
4889e512045SSimon Glass * @cells_count: Cell count to use if @cells_name is NULL
4899e512045SSimon Glass * @index: index of a phandle to parse out
4909e512045SSimon Glass * @out_args: optional pointer to output arguments structure (will be filled)
4919e512045SSimon Glass * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
4929e512045SSimon Glass * @list_name does not exist, -EINVAL if a phandle was not found,
4939e512045SSimon Glass * @cells_name could not be found, the arguments were truncated or there
4949e512045SSimon Glass * were too many arguments.
4959e512045SSimon Glass */
4969e512045SSimon Glass int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
4979e512045SSimon Glass const char *cells_name, int cell_count,
4989e512045SSimon Glass int index,
4999e512045SSimon Glass struct ofnode_phandle_args *out_args);
5009e512045SSimon Glass
5019e512045SSimon Glass /**
502642346aeSPatrice Chotard * ofnode_count_phandle_with_args() - Count number of phandle in a list
503642346aeSPatrice Chotard *
504642346aeSPatrice Chotard * This function is useful to count phandles into a list.
505642346aeSPatrice Chotard * Returns number of phandle on success, on error returns appropriate
506642346aeSPatrice Chotard * errno value.
507642346aeSPatrice Chotard *
508642346aeSPatrice Chotard * @node: device tree node containing a list
509642346aeSPatrice Chotard * @list_name: property name that contains a list
510642346aeSPatrice Chotard * @cells_name: property name that specifies phandles' arguments count
511642346aeSPatrice Chotard * @return number of phandle on success, -ENOENT if @list_name does not
512642346aeSPatrice Chotard * exist, -EINVAL if a phandle was not found, @cells_name could not
513642346aeSPatrice Chotard * be found.
514642346aeSPatrice Chotard */
515642346aeSPatrice Chotard int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
516642346aeSPatrice Chotard const char *cells_name);
517642346aeSPatrice Chotard
518642346aeSPatrice Chotard /**
5199e512045SSimon Glass * ofnode_path() - find a node by full path
5209e512045SSimon Glass *
5219e512045SSimon Glass * @path: Full path to node, e.g. "/bus/spi@1"
5229e512045SSimon Glass * @return reference to the node found. Use ofnode_valid() to check if it exists
5239e512045SSimon Glass */
5249e512045SSimon Glass ofnode ofnode_path(const char *path);
5259e512045SSimon Glass
5269e512045SSimon Glass /**
5279e512045SSimon Glass * ofnode_get_chosen_prop() - get the value of a chosen property
5289e512045SSimon Glass *
5299e512045SSimon Glass * This looks for a property within the /chosen node and returns its value
5309e512045SSimon Glass *
5319e512045SSimon Glass * @propname: Property name to look for
5329e512045SSimon Glass */
5339e512045SSimon Glass const char *ofnode_get_chosen_prop(const char *propname);
5349e512045SSimon Glass
5359e512045SSimon Glass /**
5369e512045SSimon Glass * ofnode_get_chosen_node() - get the chosen node
5379e512045SSimon Glass *
5389e512045SSimon Glass * @return the chosen node if present, else ofnode_null()
5399e512045SSimon Glass */
5409e512045SSimon Glass ofnode ofnode_get_chosen_node(const char *name);
5419e512045SSimon Glass
5429e512045SSimon Glass struct display_timing;
5439e512045SSimon Glass /**
5449e512045SSimon Glass * ofnode_decode_display_timing() - decode display timings
5459e512045SSimon Glass *
5469e512045SSimon Glass * Decode display timings from the supplied 'display-timings' node.
5479e512045SSimon Glass * See doc/device-tree-bindings/video/display-timing.txt for binding
5489e512045SSimon Glass * information.
5499e512045SSimon Glass *
5509e512045SSimon Glass * @node 'display-timing' node containing the timing subnodes
5519e512045SSimon Glass * @index Index number to read (0=first timing subnode)
5529e512045SSimon Glass * @config Place to put timings
5539e512045SSimon Glass * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
5549e512045SSimon Glass */
5559e512045SSimon Glass int ofnode_decode_display_timing(ofnode node, int index,
5569e512045SSimon Glass struct display_timing *config);
5579e512045SSimon Glass
5589e512045SSimon Glass /**
559*3cf48410SPatrick Delaunay * ofnode_get_property() - get a pointer to the value of a node property
5609e512045SSimon Glass *
5619e512045SSimon Glass * @node: node to read
5629e512045SSimon Glass * @propname: property to read
5639e512045SSimon Glass * @lenp: place to put length on success
5649e512045SSimon Glass * @return pointer to property, or NULL if not found
5659e512045SSimon Glass */
56661e51babSMasahiro Yamada const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
5679e512045SSimon Glass
5689e512045SSimon Glass /**
569*3cf48410SPatrick Delaunay * ofnode_get_first_property()- get the reference of the first property
570*3cf48410SPatrick Delaunay *
571*3cf48410SPatrick Delaunay * Get reference to the first property of the node, it is used to iterate
572*3cf48410SPatrick Delaunay * and read all the property with ofnode_get_property_by_prop().
573*3cf48410SPatrick Delaunay *
574*3cf48410SPatrick Delaunay * @node: node to read
575*3cf48410SPatrick Delaunay * @prop: place to put argument reference
576*3cf48410SPatrick Delaunay * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
577*3cf48410SPatrick Delaunay */
578*3cf48410SPatrick Delaunay int ofnode_get_first_property(ofnode node, struct ofprop *prop);
579*3cf48410SPatrick Delaunay
580*3cf48410SPatrick Delaunay /**
581*3cf48410SPatrick Delaunay * ofnode_get_next_property() - get the reference of the next property
582*3cf48410SPatrick Delaunay *
583*3cf48410SPatrick Delaunay * Get reference to the next property of the node, it is used to iterate
584*3cf48410SPatrick Delaunay * and read all the property with ofnode_get_property_by_prop().
585*3cf48410SPatrick Delaunay *
586*3cf48410SPatrick Delaunay * @prop: reference of current argument and place to put reference of next one
587*3cf48410SPatrick Delaunay * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
588*3cf48410SPatrick Delaunay */
589*3cf48410SPatrick Delaunay int ofnode_get_next_property(struct ofprop *prop);
590*3cf48410SPatrick Delaunay
591*3cf48410SPatrick Delaunay /**
592*3cf48410SPatrick Delaunay * ofnode_get_property_by_prop() - get a pointer to the value of a property
593*3cf48410SPatrick Delaunay *
594*3cf48410SPatrick Delaunay * Get value for the property identified by the provided reference.
595*3cf48410SPatrick Delaunay *
596*3cf48410SPatrick Delaunay * @prop: reference on property
597*3cf48410SPatrick Delaunay * @propname: If non-NULL, place to property name on success,
598*3cf48410SPatrick Delaunay * @lenp: If non-NULL, place to put length on success
599*3cf48410SPatrick Delaunay * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
600*3cf48410SPatrick Delaunay */
601*3cf48410SPatrick Delaunay const void *ofnode_get_property_by_prop(const struct ofprop *prop,
602*3cf48410SPatrick Delaunay const char **propname, int *lenp);
603*3cf48410SPatrick Delaunay
604*3cf48410SPatrick Delaunay /**
6059e512045SSimon Glass * ofnode_is_available() - check if a node is marked available
6069e512045SSimon Glass *
6079e512045SSimon Glass * @node: node to check
6089e512045SSimon Glass * @return true if node's 'status' property is "okay" (or is missing)
6099e512045SSimon Glass */
6109e512045SSimon Glass bool ofnode_is_available(ofnode node);
6119e512045SSimon Glass
6129e512045SSimon Glass /**
6139e512045SSimon Glass * ofnode_get_addr_size() - get address and size from a property
6149e512045SSimon Glass *
6159e512045SSimon Glass * This does no address translation. It simply reads an property that contains
6169e512045SSimon Glass * an address and a size value, one after the other.
6179e512045SSimon Glass *
6189e512045SSimon Glass * @node: node to read from
6199e512045SSimon Glass * @propname: property to read
6209e512045SSimon Glass * @sizep: place to put size value (on success)
6219e512045SSimon Glass * @return address value, or FDT_ADDR_T_NONE on error
6229e512045SSimon Glass */
6239e512045SSimon Glass phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
6249e512045SSimon Glass phys_size_t *sizep);
6259e512045SSimon Glass
6269e512045SSimon Glass /**
6279e512045SSimon Glass * ofnode_read_u8_array_ptr() - find an 8-bit array
6289e512045SSimon Glass *
6299e512045SSimon Glass * Look up a property in a node and return a pointer to its contents as a
6309e512045SSimon Glass * byte array of given length. The property must have at least enough data
6319e512045SSimon Glass * for the array (count bytes). It may have more, but this will be ignored.
6329e512045SSimon Glass * The data is not copied.
6339e512045SSimon Glass *
6349e512045SSimon Glass * @node node to examine
6359e512045SSimon Glass * @propname name of property to find
6369e512045SSimon Glass * @sz number of array elements
6379e512045SSimon Glass * @return pointer to byte array if found, or NULL if the property is not
6389e512045SSimon Glass * found or there is not enough data
6399e512045SSimon Glass */
6409e512045SSimon Glass const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
6419e512045SSimon Glass size_t sz);
6429e512045SSimon Glass
6439e512045SSimon Glass /**
6449e512045SSimon Glass * ofnode_read_pci_addr() - look up a PCI address
6459e512045SSimon Glass *
6469e512045SSimon Glass * Look at an address property in a node and return the PCI address which
6479e512045SSimon Glass * corresponds to the given type in the form of fdt_pci_addr.
6489e512045SSimon Glass * The property must hold one fdt_pci_addr with a lengh.
6499e512045SSimon Glass *
6509e512045SSimon Glass * @node node to examine
6519e512045SSimon Glass * @type pci address type (FDT_PCI_SPACE_xxx)
6529e512045SSimon Glass * @propname name of property to find
6539e512045SSimon Glass * @addr returns pci address in the form of fdt_pci_addr
6549e512045SSimon Glass * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
6559e512045SSimon Glass * format of the property was invalid, -ENXIO if the requested
6569e512045SSimon Glass * address type was not found
6579e512045SSimon Glass */
6589e512045SSimon Glass int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
6599e512045SSimon Glass const char *propname, struct fdt_pci_addr *addr);
6609e512045SSimon Glass
6619e512045SSimon Glass /**
6629e512045SSimon Glass * ofnode_read_addr_cells() - Get the number of address cells for a node
6639e512045SSimon Glass *
6649e512045SSimon Glass * This walks back up the tree to find the closest #address-cells property
6659e512045SSimon Glass * which controls the given node.
6669e512045SSimon Glass *
6679e512045SSimon Glass * @node: Node to check
6689e512045SSimon Glass * @return number of address cells this node uses
6699e512045SSimon Glass */
6709e512045SSimon Glass int ofnode_read_addr_cells(ofnode node);
6719e512045SSimon Glass
6729e512045SSimon Glass /**
6739e512045SSimon Glass * ofnode_read_size_cells() - Get the number of size cells for a node
6749e512045SSimon Glass *
6759e512045SSimon Glass * This walks back up the tree to find the closest #size-cells property
6769e512045SSimon Glass * which controls the given node.
6779e512045SSimon Glass *
6789e512045SSimon Glass * @node: Node to check
6799e512045SSimon Glass * @return number of size cells this node uses
6809e512045SSimon Glass */
6819e512045SSimon Glass int ofnode_read_size_cells(ofnode node);
6829e512045SSimon Glass
6839e512045SSimon Glass /**
684878d68c0SSimon Glass * ofnode_read_simple_addr_cells() - Get the address cells property in a node
685878d68c0SSimon Glass *
686878d68c0SSimon Glass * This function matches fdt_address_cells().
687878d68c0SSimon Glass *
688878d68c0SSimon Glass * @np: Node pointer to check
689878d68c0SSimon Glass * @return value of #address-cells property in this node, or 2 if none
690878d68c0SSimon Glass */
691878d68c0SSimon Glass int ofnode_read_simple_addr_cells(ofnode node);
692878d68c0SSimon Glass
693878d68c0SSimon Glass /**
694878d68c0SSimon Glass * ofnode_read_simple_size_cells() - Get the size cells property in a node
695878d68c0SSimon Glass *
696878d68c0SSimon Glass * This function matches fdt_size_cells().
697878d68c0SSimon Glass *
698878d68c0SSimon Glass * @np: Node pointer to check
699878d68c0SSimon Glass * @return value of #size-cells property in this node, or 2 if none
700878d68c0SSimon Glass */
701878d68c0SSimon Glass int ofnode_read_simple_size_cells(ofnode node);
702878d68c0SSimon Glass
703878d68c0SSimon Glass /**
7049e512045SSimon Glass * ofnode_pre_reloc() - check if a node should be bound before relocation
7059e512045SSimon Glass *
7069e512045SSimon Glass * Device tree nodes can be marked as needing-to-be-bound in the loader stages
7079e512045SSimon Glass * via special device tree properties.
7089e512045SSimon Glass *
7099e512045SSimon Glass * Before relocation this function can be used to check if nodes are required
7109e512045SSimon Glass * in either SPL or TPL stages.
7119e512045SSimon Glass *
7129e512045SSimon Glass * After relocation and jumping into the real U-Boot binary it is possible to
7139e512045SSimon Glass * determine if a node was bound in one of SPL/TPL stages.
7149e512045SSimon Glass *
7159e512045SSimon Glass * There are 3 settings currently in use
7169e512045SSimon Glass * -
7179e512045SSimon Glass * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
7189e512045SSimon Glass * Existing platforms only use it to indicate nodes needed in
7199e512045SSimon Glass * SPL. Should probably be replaced by u-boot,dm-spl for
7209e512045SSimon Glass * new platforms.
7219e512045SSimon Glass *
7229e512045SSimon Glass * @node: node to check
7239e512045SSimon Glass * @eturns true if node is needed in SPL/TL, false otherwise
7249e512045SSimon Glass */
7259e512045SSimon Glass bool ofnode_pre_reloc(ofnode node);
7269e512045SSimon Glass
727dcf98852SSimon Glass int ofnode_read_resource(ofnode node, uint index, struct resource *res);
7287b8b47bdSMasahiro Yamada int ofnode_read_resource_byname(ofnode node, const char *name,
7297b8b47bdSMasahiro Yamada struct resource *res);
730dcf98852SSimon Glass
73117c82fdcSSimon Glass /**
73217c82fdcSSimon Glass * ofnode_for_each_subnode() - iterate over all subnodes of a parent
73317c82fdcSSimon Glass *
73417c82fdcSSimon Glass * @node: child node (ofnode, lvalue)
73517c82fdcSSimon Glass * @parent: parent node (ofnode)
73617c82fdcSSimon Glass *
73717c82fdcSSimon Glass * This is a wrapper around a for loop and is used like so:
73817c82fdcSSimon Glass *
73917c82fdcSSimon Glass * ofnode node;
74017c82fdcSSimon Glass *
74117c82fdcSSimon Glass * ofnode_for_each_subnode(node, parent) {
74217c82fdcSSimon Glass * Use node
74317c82fdcSSimon Glass * ...
74417c82fdcSSimon Glass * }
74517c82fdcSSimon Glass *
74617c82fdcSSimon Glass * Note that this is implemented as a macro and @node is used as
74717c82fdcSSimon Glass * iterator in the loop. The parent variable can be a constant or even a
74817c82fdcSSimon Glass * literal.
74917c82fdcSSimon Glass */
75017c82fdcSSimon Glass #define ofnode_for_each_subnode(node, parent) \
75117c82fdcSSimon Glass for (node = ofnode_first_subnode(parent); \
75217c82fdcSSimon Glass ofnode_valid(node); \
75317c82fdcSSimon Glass node = ofnode_next_subnode(node))
75417c82fdcSSimon Glass
755a9bd1c73SMario Six /**
756a9bd1c73SMario Six * ofnode_translate_address() - Tranlate a device-tree address
757a9bd1c73SMario Six *
758a9bd1c73SMario Six * Translate an address from the device-tree into a CPU physical address. This
759a9bd1c73SMario Six * function walks up the tree and applies the various bus mappings along the
760a9bd1c73SMario Six * way.
761a9bd1c73SMario Six *
762a9bd1c73SMario Six * @ofnode: Device tree node giving the context in which to translate the
763a9bd1c73SMario Six * address
764a9bd1c73SMario Six * @in_addr: pointer to the address to translate
765a9bd1c73SMario Six * @return the translated address; OF_BAD_ADDR on error
766a9bd1c73SMario Six */
767a9bd1c73SMario Six u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
7684984de2bSSimon Glass #endif
769