of_access.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Originally from Linux v4.9
  3. * Copyright (C) 1996-2005 Paul Mackerras.
  4. *
  5. * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
  6. * Updates for SPARC64 by David S. Miller
  7. * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
  8. *
  9. * Copyright (c) 2017 Google, Inc
  10. * Written by Simon Glass <sjg@chromium.org>
  11. *
  12. * Modified for U-Boot
  13. * Copyright (c) 2017 Google, Inc
  14. *
  15. * SPDX-License-Identifier: GPL-2.0+
  16. */
  17. #ifndef _DM_OF_ACCESS_H
  18. #define _DM_OF_ACCESS_H
  19. #include <dm/of.h>
  20. /**
  21. * of_find_all_nodes - Get next node in global list
  22. * @prev: Previous node or NULL to start iteration
  23. * of_node_put() will be called on it
  24. *
  25. * Returns a node pointer with refcount incremented, use
  26. * of_node_put() on it when done.
  27. */
  28. struct device_node *of_find_all_nodes(struct device_node *prev);
  29. #define for_each_of_allnodes_from(from, dn) \
  30. for (dn = of_find_all_nodes(from); dn; dn = of_find_all_nodes(dn))
  31. #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
  32. /* Dummy functions to mirror Linux. These are not used in U-Boot */
  33. #define of_node_get(x) (x)
  34. static inline void of_node_put(const struct device_node *np) { }
  35. /**
  36. * of_n_addr_cells() - Get the number of address cells for a node
  37. *
  38. * This walks back up the tree to find the closest #address-cells property
  39. * which controls the given node.
  40. *
  41. * @np: Node pointer to check
  42. * @return number of address cells this node uses
  43. */
  44. int of_n_addr_cells(const struct device_node *np);
  45. /**
  46. * of_n_size_cells() - Get the number of size cells for a node
  47. *
  48. * This walks back up the tree to find the closest #size-cells property
  49. * which controls the given node.
  50. *
  51. * @np: Node pointer to check
  52. * @return number of size cells this node uses
  53. */
  54. int of_n_size_cells(const struct device_node *np);
  55. /**
  56. * of_find_property() - find a property in a node
  57. *
  58. * @np: Pointer to device node holding property
  59. * @name: Name of property
  60. * @lenp: If non-NULL, returns length of property
  61. * @return pointer to property, or NULL if not found
  62. */
  63. struct property *of_find_property(const struct device_node *np,
  64. const char *name, int *lenp);
  65. /**
  66. * of_get_property() - get a property value
  67. *
  68. * Find a property with a given name for a given node and return the value.
  69. *
  70. * @np: Pointer to device node holding property
  71. * @name: Name of property
  72. * @lenp: If non-NULL, returns length of property
  73. * @return pointer to property value, or NULL if not found
  74. */
  75. const void *of_get_property(const struct device_node *np, const char *name,
  76. int *lenp);
  77. /**
  78. * of_device_is_compatible() - Check if the node matches given constraints
  79. * @device: pointer to node
  80. * @compat: required compatible string, NULL or "" for any match
  81. * @type: required device_type value, NULL or "" for any match
  82. * @name: required node name, NULL or "" for any match
  83. *
  84. * Checks if the given @compat, @type and @name strings match the
  85. * properties of the given @device. A constraints can be skipped by
  86. * passing NULL or an empty string as the constraint.
  87. *
  88. * @return 0 for no match, and a positive integer on match. The return
  89. * value is a relative score with larger values indicating better
  90. * matches. The score is weighted for the most specific compatible value
  91. * to get the highest score. Matching type is next, followed by matching
  92. * name. Practically speaking, this results in the following priority
  93. * order for matches:
  94. *
  95. * 1. specific compatible && type && name
  96. * 2. specific compatible && type
  97. * 3. specific compatible && name
  98. * 4. specific compatible
  99. * 5. general compatible && type && name
  100. * 6. general compatible && type
  101. * 7. general compatible && name
  102. * 8. general compatible
  103. * 9. type && name
  104. * 10. type
  105. * 11. name
  106. */
  107. int of_device_is_compatible(const struct device_node *np, const char *compat,
  108. const char *type, const char *name);
  109. /**
  110. * of_device_is_available() - check if a device is available for use
  111. *
  112. * @device: Node to check for availability
  113. *
  114. * @return true if the status property is absent or set to "okay", false
  115. * otherwise
  116. */
  117. bool of_device_is_available(const struct device_node *np);
  118. /**
  119. * of_get_parent() - Get a node's parent, if any
  120. *
  121. * @node: Node to check
  122. * @eturns a node pointer, or NULL if none
  123. */
  124. struct device_node *of_get_parent(const struct device_node *np);
  125. /**
  126. * of_find_node_opts_by_path() - Find a node matching a full OF path
  127. *
  128. * @path: Either the full path to match, or if the path does not start with
  129. * '/', the name of a property of the /aliases node (an alias). In the
  130. * case of an alias, the node matching the alias' value will be returned.
  131. * @opts: Address of a pointer into which to store the start of an options
  132. * string appended to the end of the path with a ':' separator. Can be NULL
  133. *
  134. * Valid paths:
  135. * /foo/bar Full path
  136. * foo Valid alias
  137. * foo/bar Valid alias + relative path
  138. *
  139. * @return a node pointer or NULL if not found
  140. */
  141. struct device_node *of_find_node_opts_by_path(const char *path,
  142. const char **opts);
  143. static inline struct device_node *of_find_node_by_path(const char *path)
  144. {
  145. return of_find_node_opts_by_path(path, NULL);
  146. }
  147. /**
  148. * of_find_compatible_node() - find a node based on its compatible string
  149. *
  150. * Find a node based on type and one of the tokens in its "compatible" property
  151. * @from: Node to start searching from or NULL. the node you pass will not be
  152. * searched, only the next one will; typically, you pass what the previous
  153. * call returned.
  154. * @type: The type string to match "device_type" or NULL to ignore
  155. * @compatible: The string to match to one of the tokens in the device
  156. * "compatible" list.
  157. * @return node pointer or NULL if not found
  158. */
  159. struct device_node *of_find_compatible_node(struct device_node *from,
  160. const char *type, const char *compatible);
  161. /**
  162. * of_find_node_by_phandle() - Find a node given a phandle
  163. *
  164. * @handle: phandle of the node to find
  165. *
  166. * @return node pointer, or NULL if not found
  167. */
  168. struct device_node *of_find_node_by_phandle(phandle handle);
  169. /**
  170. * of_read_u32() - Find and read a 32-bit integer from a property
  171. *
  172. * Search for a property in a device node and read a 32-bit value from
  173. * it.
  174. *
  175. * @np: device node from which the property value is to be read.
  176. * @propname: name of the property to be searched.
  177. * @outp: pointer to return value, modified only if return value is 0.
  178. *
  179. * @return 0 on success, -EINVAL if the property does not exist,
  180. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  181. * property data isn't large enough.
  182. */
  183. int of_read_u32(const struct device_node *np, const char *propname, u32 *outp);
  184. /**
  185. * of_read_u32_array() - Find and read an array of 32 bit integers
  186. *
  187. * Search for a property in a device node and read 32-bit value(s) from
  188. * it.
  189. *
  190. * @np: device node from which the property value is to be read.
  191. * @propname: name of the property to be searched.
  192. * @out_values: pointer to return value, modified only if return value is 0.
  193. * @sz: number of array elements to read
  194. * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
  195. * if property does not have a value, and -EOVERFLOW is longer than sz.
  196. */
  197. int of_read_u32_array(const struct device_node *np, const char *propname,
  198. u32 *out_values, size_t sz);
  199. /**
  200. * of_property_match_string() - Find string in a list and return index
  201. *
  202. * This function searches a string list property and returns the index
  203. * of a specific string value.
  204. *
  205. * @np: pointer to node containing string list property
  206. * @propname: string list property name
  207. * @string: pointer to string to search for in string list
  208. * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
  209. * if property does not have a value, and -EOVERFLOW is longer than sz.
  210. */
  211. int of_property_match_string(const struct device_node *np, const char *propname,
  212. const char *string);
  213. int of_property_read_string_helper(const struct device_node *np,
  214. const char *propname, const char **out_strs,
  215. size_t sz, int index);
  216. /**
  217. * of_property_read_string_index() - Find and read a string from a multiple
  218. * strings property.
  219. * @np: device node from which the property value is to be read.
  220. * @propname: name of the property to be searched.
  221. * @index: index of the string in the list of strings
  222. * @out_string: pointer to null terminated return string, modified only if
  223. * return value is 0.
  224. *
  225. * Search for a property in a device tree node and retrieve a null
  226. * terminated string value (pointer to data, not a copy) in the list of strings
  227. * contained in that property.
  228. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  229. * property does not have a value, and -EILSEQ if the string is not
  230. * null-terminated within the length of the property data.
  231. *
  232. * The out_string pointer is modified only if a valid string can be decoded.
  233. */
  234. static inline int of_property_read_string_index(const struct device_node *np,
  235. const char *propname,
  236. int index, const char **output)
  237. {
  238. int rc = of_property_read_string_helper(np, propname, output, 1, index);
  239. return rc < 0 ? rc : 0;
  240. }
  241. /**
  242. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  243. * @np: Pointer to device node holding phandle property
  244. * @phandle_name: Name of property holding a phandle value
  245. * @index: For properties holding a table of phandles, this is the index into
  246. * the table
  247. *
  248. * Returns the device_node pointer with refcount incremented. Use
  249. * of_node_put() on it when done.
  250. */
  251. struct device_node *of_parse_phandle(const struct device_node *np,
  252. const char *phandle_name, int index);
  253. /**
  254. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  255. *
  256. * @np: pointer to a device tree node containing a list
  257. * @list_name: property name that contains a list
  258. * @cells_name: property name that specifies phandles' arguments count
  259. * @index: index of a phandle to parse out
  260. * @out_args: optional pointer to output arguments structure (will be filled)
  261. * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
  262. * @list_name does not exist, -EINVAL if a phandle was not found,
  263. * @cells_name could not be found, the arguments were truncated or there
  264. * were too many arguments.
  265. *
  266. * This function is useful to parse lists of phandles and their arguments.
  267. * Returns 0 on success and fills out_args, on error returns appropriate
  268. * errno value.
  269. *
  270. * Caller is responsible to call of_node_put() on the returned out_args->np
  271. * pointer.
  272. *
  273. * Example:
  274. *
  275. * phandle1: node1 {
  276. * #list-cells = <2>;
  277. * }
  278. *
  279. * phandle2: node2 {
  280. * #list-cells = <1>;
  281. * }
  282. *
  283. * node3 {
  284. * list = <&phandle1 1 2 &phandle2 3>;
  285. * }
  286. *
  287. * To get a device_node of the `node2' node you may call this:
  288. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  289. */
  290. int of_parse_phandle_with_args(const struct device_node *np,
  291. const char *list_name, const char *cells_name,
  292. int index, struct of_phandle_args *out_args);
  293. /**
  294. * of_alias_scan() - Scan all properties of the 'aliases' node
  295. *
  296. * The function scans all the properties of the 'aliases' node and populates
  297. * the lookup table with the properties. It returns the number of alias
  298. * properties found, or an error code in case of failure.
  299. *
  300. * @return 9 if OK, -ENOMEM if not enough memory
  301. */
  302. int of_alias_scan(void);
  303. /**
  304. * of_alias_get_id - Get alias id for the given device_node
  305. *
  306. * Travels the lookup table to get the alias id for the given device_node and
  307. * alias stem.
  308. *
  309. * @np: Pointer to the given device_node
  310. * @stem: Alias stem of the given device_node
  311. * @return alias ID, if found, else -ENODEV
  312. */
  313. int of_alias_get_id(const struct device_node *np, const char *stem);
  314. /**
  315. * of_get_stdout() - Get node to use for stdout
  316. *
  317. * @return node referred to by stdout-path alias, or NULL if none
  318. */
  319. struct device_node *of_get_stdout(void);
  320. #endif