of_access.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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_simple_addr_cells() - Get the address cells property in a node
  57. *
  58. * This function matches fdt_address_cells().
  59. *
  60. * @np: Node pointer to check
  61. * @return value of #address-cells property in this node, or 2 if none
  62. */
  63. int of_simple_addr_cells(const struct device_node *np);
  64. /**
  65. * of_simple_size_cells() - Get the size cells property in a node
  66. *
  67. * This function matches fdt_size_cells().
  68. *
  69. * @np: Node pointer to check
  70. * @return value of #size-cells property in this node, or 2 if none
  71. */
  72. int of_simple_size_cells(const struct device_node *np);
  73. /**
  74. * of_find_property() - find a property in a node
  75. *
  76. * @np: Pointer to device node holding property
  77. * @name: Name of property
  78. * @lenp: If non-NULL, returns length of property
  79. * @return pointer to property, or NULL if not found
  80. */
  81. struct property *of_find_property(const struct device_node *np,
  82. const char *name, int *lenp);
  83. /**
  84. * of_get_property() - get a property value
  85. *
  86. * Find a property with a given name for a given node and return the value.
  87. *
  88. * @np: Pointer to device node holding property
  89. * @name: Name of property
  90. * @lenp: If non-NULL, returns length of property
  91. * @return pointer to property value, or NULL if not found
  92. */
  93. const void *of_get_property(const struct device_node *np, const char *name,
  94. int *lenp);
  95. /**
  96. * of_device_is_compatible() - Check if the node matches given constraints
  97. * @device: pointer to node
  98. * @compat: required compatible string, NULL or "" for any match
  99. * @type: required device_type value, NULL or "" for any match
  100. * @name: required node name, NULL or "" for any match
  101. *
  102. * Checks if the given @compat, @type and @name strings match the
  103. * properties of the given @device. A constraints can be skipped by
  104. * passing NULL or an empty string as the constraint.
  105. *
  106. * @return 0 for no match, and a positive integer on match. The return
  107. * value is a relative score with larger values indicating better
  108. * matches. The score is weighted for the most specific compatible value
  109. * to get the highest score. Matching type is next, followed by matching
  110. * name. Practically speaking, this results in the following priority
  111. * order for matches:
  112. *
  113. * 1. specific compatible && type && name
  114. * 2. specific compatible && type
  115. * 3. specific compatible && name
  116. * 4. specific compatible
  117. * 5. general compatible && type && name
  118. * 6. general compatible && type
  119. * 7. general compatible && name
  120. * 8. general compatible
  121. * 9. type && name
  122. * 10. type
  123. * 11. name
  124. */
  125. int of_device_is_compatible(const struct device_node *np, const char *compat,
  126. const char *type, const char *name);
  127. /**
  128. * of_device_is_available() - check if a device is available for use
  129. *
  130. * @device: Node to check for availability
  131. *
  132. * @return true if the status property is absent or set to "okay", false
  133. * otherwise
  134. */
  135. bool of_device_is_available(const struct device_node *np);
  136. /**
  137. * of_get_parent() - Get a node's parent, if any
  138. *
  139. * @node: Node to check
  140. * @eturns a node pointer, or NULL if none
  141. */
  142. struct device_node *of_get_parent(const struct device_node *np);
  143. /**
  144. * of_find_node_opts_by_path() - Find a node matching a full OF path
  145. *
  146. * @path: Either the full path to match, or if the path does not start with
  147. * '/', the name of a property of the /aliases node (an alias). In the
  148. * case of an alias, the node matching the alias' value will be returned.
  149. * @opts: Address of a pointer into which to store the start of an options
  150. * string appended to the end of the path with a ':' separator. Can be NULL
  151. *
  152. * Valid paths:
  153. * /foo/bar Full path
  154. * foo Valid alias
  155. * foo/bar Valid alias + relative path
  156. *
  157. * @return a node pointer or NULL if not found
  158. */
  159. struct device_node *of_find_node_opts_by_path(const char *path,
  160. const char **opts);
  161. static inline struct device_node *of_find_node_by_path(const char *path)
  162. {
  163. return of_find_node_opts_by_path(path, NULL);
  164. }
  165. /**
  166. * of_find_compatible_node() - find a node based on its compatible string
  167. *
  168. * Find a node based on type and one of the tokens in its "compatible" property
  169. * @from: Node to start searching from or NULL. the node you pass will not be
  170. * searched, only the next one will; typically, you pass what the previous
  171. * call returned.
  172. * @type: The type string to match "device_type" or NULL to ignore
  173. * @compatible: The string to match to one of the tokens in the device
  174. * "compatible" list.
  175. * @return node pointer or NULL if not found
  176. */
  177. struct device_node *of_find_compatible_node(struct device_node *from,
  178. const char *type, const char *compatible);
  179. /**
  180. * of_find_node_by_phandle() - Find a node given a phandle
  181. *
  182. * @handle: phandle of the node to find
  183. *
  184. * @return node pointer, or NULL if not found
  185. */
  186. struct device_node *of_find_node_by_phandle(phandle handle);
  187. /**
  188. * of_read_u32() - Find and read a 32-bit integer from a property
  189. *
  190. * Search for a property in a device node and read a 32-bit value from
  191. * it.
  192. *
  193. * @np: device node from which the property value is to be read.
  194. * @propname: name of the property to be searched.
  195. * @outp: pointer to return value, modified only if return value is 0.
  196. *
  197. * @return 0 on success, -EINVAL if the property does not exist,
  198. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  199. * property data isn't large enough.
  200. */
  201. int of_read_u32(const struct device_node *np, const char *propname, u32 *outp);
  202. /**
  203. * of_read_u32_array() - Find and read an array of 32 bit integers
  204. *
  205. * Search for a property in a device node and read 32-bit value(s) from
  206. * it.
  207. *
  208. * @np: device node from which the property value is to be read.
  209. * @propname: name of the property to be searched.
  210. * @out_values: pointer to return value, modified only if return value is 0.
  211. * @sz: number of array elements to read
  212. * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
  213. * if property does not have a value, and -EOVERFLOW is longer than sz.
  214. */
  215. int of_read_u32_array(const struct device_node *np, const char *propname,
  216. u32 *out_values, size_t sz);
  217. /**
  218. * of_property_match_string() - Find string in a list and return index
  219. *
  220. * This function searches a string list property and returns the index
  221. * of a specific string value.
  222. *
  223. * @np: pointer to node containing string list property
  224. * @propname: string list property name
  225. * @string: pointer to string to search for in string list
  226. * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
  227. * if property does not have a value, and -EOVERFLOW is longer than sz.
  228. */
  229. int of_property_match_string(const struct device_node *np, const char *propname,
  230. const char *string);
  231. int of_property_read_string_helper(const struct device_node *np,
  232. const char *propname, const char **out_strs,
  233. size_t sz, int index);
  234. /**
  235. * of_property_read_string_index() - Find and read a string from a multiple
  236. * strings property.
  237. * @np: device node from which the property value is to be read.
  238. * @propname: name of the property to be searched.
  239. * @index: index of the string in the list of strings
  240. * @out_string: pointer to null terminated return string, modified only if
  241. * return value is 0.
  242. *
  243. * Search for a property in a device tree node and retrieve a null
  244. * terminated string value (pointer to data, not a copy) in the list of strings
  245. * contained in that property.
  246. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  247. * property does not have a value, and -EILSEQ if the string is not
  248. * null-terminated within the length of the property data.
  249. *
  250. * The out_string pointer is modified only if a valid string can be decoded.
  251. */
  252. static inline int of_property_read_string_index(const struct device_node *np,
  253. const char *propname,
  254. int index, const char **output)
  255. {
  256. int rc = of_property_read_string_helper(np, propname, output, 1, index);
  257. return rc < 0 ? rc : 0;
  258. }
  259. /**
  260. * of_property_count_strings() - Find and return the number of strings from a
  261. * multiple strings property.
  262. * @np: device node from which the property value is to be read.
  263. * @propname: name of the property to be searched.
  264. *
  265. * Search for a property in a device tree node and retrieve the number of null
  266. * terminated string contain in it. Returns the number of strings on
  267. * success, -EINVAL if the property does not exist, -ENODATA if property
  268. * does not have a value, and -EILSEQ if the string is not null-terminated
  269. * within the length of the property data.
  270. */
  271. static inline int of_property_count_strings(const struct device_node *np,
  272. const char *propname)
  273. {
  274. return of_property_read_string_helper(np, propname, NULL, 0, 0);
  275. }
  276. /**
  277. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  278. * @np: Pointer to device node holding phandle property
  279. * @phandle_name: Name of property holding a phandle value
  280. * @index: For properties holding a table of phandles, this is the index into
  281. * the table
  282. *
  283. * Returns the device_node pointer with refcount incremented. Use
  284. * of_node_put() on it when done.
  285. */
  286. struct device_node *of_parse_phandle(const struct device_node *np,
  287. const char *phandle_name, int index);
  288. /**
  289. * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
  290. *
  291. * @np: pointer to a device tree node containing a list
  292. * @list_name: property name that contains a list
  293. * @cells_name: property name that specifies phandles' arguments count
  294. * @index: index of a phandle to parse out
  295. * @out_args: optional pointer to output arguments structure (will be filled)
  296. * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
  297. * @list_name does not exist, -EINVAL if a phandle was not found,
  298. * @cells_name could not be found, the arguments were truncated or there
  299. * were too many arguments.
  300. *
  301. * This function is useful to parse lists of phandles and their arguments.
  302. * Returns 0 on success and fills out_args, on error returns appropriate
  303. * errno value.
  304. *
  305. * Caller is responsible to call of_node_put() on the returned out_args->np
  306. * pointer.
  307. *
  308. * Example:
  309. *
  310. * phandle1: node1 {
  311. * #list-cells = <2>;
  312. * }
  313. *
  314. * phandle2: node2 {
  315. * #list-cells = <1>;
  316. * }
  317. *
  318. * node3 {
  319. * list = <&phandle1 1 2 &phandle2 3>;
  320. * }
  321. *
  322. * To get a device_node of the `node2' node you may call this:
  323. * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
  324. */
  325. int of_parse_phandle_with_args(const struct device_node *np,
  326. const char *list_name, const char *cells_name,
  327. int index, struct of_phandle_args *out_args);
  328. /**
  329. * of_count_phandle_with_args() - Count the number of phandle in a list
  330. *
  331. * @np: pointer to a device tree node containing a list
  332. * @list_name: property name that contains a list
  333. * @cells_name: property name that specifies phandles' arguments count
  334. * @return number of phandle found, -ENOENT if
  335. * @list_name does not exist, -EINVAL if a phandle was not found,
  336. * @cells_name could not be found, the arguments were truncated or there
  337. * were too many arguments.
  338. *
  339. * Returns number of phandle found on success, on error returns appropriate
  340. * errno value.
  341. *
  342. */
  343. int of_count_phandle_with_args(const struct device_node *np,
  344. const char *list_name, const char *cells_name);
  345. /**
  346. * of_alias_scan() - Scan all properties of the 'aliases' node
  347. *
  348. * The function scans all the properties of the 'aliases' node and populates
  349. * the lookup table with the properties. It returns the number of alias
  350. * properties found, or an error code in case of failure.
  351. *
  352. * @return 9 if OK, -ENOMEM if not enough memory
  353. */
  354. int of_alias_scan(void);
  355. /**
  356. * of_alias_get_id - Get alias id for the given device_node
  357. *
  358. * Travels the lookup table to get the alias id for the given device_node and
  359. * alias stem.
  360. *
  361. * @np: Pointer to the given device_node
  362. * @stem: Alias stem of the given device_node
  363. * @return alias ID, if found, else -ENODEV
  364. */
  365. int of_alias_get_id(const struct device_node *np, const char *stem);
  366. /**
  367. * of_get_stdout() - Get node to use for stdout
  368. *
  369. * @return node referred to by stdout-path alias, or NULL if none
  370. */
  371. struct device_node *of_get_stdout(void);
  372. #endif