read.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  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. #ifndef _DM_READ_H
  10. #define _DM_READ_H
  11. #include <dm/fdtaddr.h>
  12. #include <dm/ofnode.h>
  13. #include <dm/uclass.h>
  14. struct resource;
  15. #if CONFIG_IS_ENABLED(OF_LIVE)
  16. static inline const struct device_node *dev_np(struct udevice *dev)
  17. {
  18. return ofnode_to_np(dev->node);
  19. }
  20. #else
  21. static inline const struct device_node *dev_np(struct udevice *dev)
  22. {
  23. return NULL;
  24. }
  25. #endif
  26. /**
  27. * dev_ofnode() - get the DT node reference associated with a udevice
  28. *
  29. * @dev: device to check
  30. * @return reference of the the device's DT node
  31. */
  32. static inline ofnode dev_ofnode(struct udevice *dev)
  33. {
  34. return dev->node;
  35. }
  36. static inline bool dev_of_valid(struct udevice *dev)
  37. {
  38. return ofnode_valid(dev_ofnode(dev));
  39. }
  40. #ifndef CONFIG_DM_DEV_READ_INLINE
  41. /**
  42. * dev_read_u32_default() - read a 32-bit integer from a device's DT property
  43. *
  44. * @dev: device to read DT property from
  45. * @propname: name of the property to read from
  46. * @def: default value to return if the property has no value
  47. * @return property value, or @def if not found
  48. */
  49. int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
  50. /**
  51. * dev_read_string() - Read a string from a device's DT property
  52. *
  53. * @dev: device to read DT property from
  54. * @propname: name of the property to read
  55. * @return string from property value, or NULL if there is no such property
  56. */
  57. const char *dev_read_string(struct udevice *dev, const char *propname);
  58. /**
  59. * dev_read_bool() - read a boolean value from a device's DT property
  60. *
  61. * @dev: device to read DT property from
  62. * @propname: name of property to read
  63. * @return true if property is present (meaning true), false if not present
  64. */
  65. bool dev_read_bool(struct udevice *dev, const char *propname);
  66. /**
  67. * dev_read_subnode() - find a named subnode of a device
  68. *
  69. * @dev: device whose DT node contains the subnode
  70. * @subnode_name: name of subnode to find
  71. * @return reference to subnode (which can be invalid if there is no such
  72. * subnode)
  73. */
  74. ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
  75. /**
  76. * dev_read_size() - read the size of a property
  77. *
  78. * @dev: device to check
  79. * @propname: property to check
  80. * @return size of property if present, or -EINVAL if not
  81. */
  82. int dev_read_size(struct udevice *dev, const char *propname);
  83. /**
  84. * dev_read_addr_index() - Get the indexed reg property of a device
  85. *
  86. * @dev: Device to read from
  87. * @index: the 'reg' property can hold a list of <addr, size> pairs
  88. * and @index is used to select which one is required
  89. *
  90. * @return address or FDT_ADDR_T_NONE if not found
  91. */
  92. fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
  93. /**
  94. * dev_read_addr() - Get the reg property of a device
  95. *
  96. * @dev: Device to read from
  97. *
  98. * @return address or FDT_ADDR_T_NONE if not found
  99. */
  100. fdt_addr_t dev_read_addr(struct udevice *dev);
  101. /**
  102. * dev_read_addr_size() - get address and size from a device property
  103. *
  104. * This does no address translation. It simply reads an property that contains
  105. * an address and a size value, one after the other.
  106. *
  107. * @dev: Device to read from
  108. * @propname: property to read
  109. * @sizep: place to put size value (on success)
  110. * @return address value, or FDT_ADDR_T_NONE on error
  111. */
  112. fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
  113. fdt_size_t *sizep);
  114. /**
  115. * dev_read_name() - get the name of a device's node
  116. *
  117. * @node: valid node to look up
  118. * @return name of node
  119. */
  120. const char *dev_read_name(struct udevice *dev);
  121. /**
  122. * dev_read_stringlist_search() - find string in a string list and return index
  123. *
  124. * Note that it is possible for this function to succeed on property values
  125. * that are not NUL-terminated. That's because the function will stop after
  126. * finding the first occurrence of @string. This can for example happen with
  127. * small-valued cell properties, such as #address-cells, when searching for
  128. * the empty string.
  129. *
  130. * @dev: device to check
  131. * @propname: name of the property containing the string list
  132. * @string: string to look up in the string list
  133. *
  134. * @return:
  135. * the index of the string in the list of strings
  136. * -ENODATA if the property is not found
  137. * -EINVAL on some other error
  138. */
  139. int dev_read_stringlist_search(struct udevice *dev, const char *property,
  140. const char *string);
  141. /**
  142. * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
  143. *
  144. * This function is useful to parse lists of phandles and their arguments.
  145. * Returns 0 on success and fills out_args, on error returns appropriate
  146. * errno value.
  147. *
  148. * Caller is responsible to call of_node_put() on the returned out_args->np
  149. * pointer.
  150. *
  151. * Example:
  152. *
  153. * phandle1: node1 {
  154. * #list-cells = <2>;
  155. * }
  156. *
  157. * phandle2: node2 {
  158. * #list-cells = <1>;
  159. * }
  160. *
  161. * node3 {
  162. * list = <&phandle1 1 2 &phandle2 3>;
  163. * }
  164. *
  165. * To get a device_node of the `node2' node you may call this:
  166. * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
  167. *
  168. * @dev: device whose node containing a list
  169. * @list_name: property name that contains a list
  170. * @cells_name: property name that specifies phandles' arguments count
  171. * @cells_count: Cell count to use if @cells_name is NULL
  172. * @index: index of a phandle to parse out
  173. * @out_args: optional pointer to output arguments structure (will be filled)
  174. * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
  175. * @list_name does not exist, -EINVAL if a phandle was not found,
  176. * @cells_name could not be found, the arguments were truncated or there
  177. * were too many arguments.
  178. */
  179. int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
  180. const char *cells_name, int cell_count,
  181. int index,
  182. struct ofnode_phandle_args *out_args);
  183. /**
  184. * dev_count_phandle_with_args() - Return phandle number in a list
  185. *
  186. * This function is usefull to get phandle number contained in a property list.
  187. * For example, this allows to allocate the right amount of memory to keep
  188. * clock's reference contained into the "clocks" property.
  189. *
  190. *
  191. * @dev: device whose node containing a list
  192. * @list_name: property name that contains a list
  193. * @cells_name: property name that specifies phandles' arguments count
  194. * @Returns number of phandle found on success, on error returns appropriate
  195. * errno value.
  196. */
  197. int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
  198. const char *cells_name);
  199. /**
  200. * dev_read_addr_cells() - Get the number of address cells for a device's node
  201. *
  202. * This walks back up the tree to find the closest #address-cells property
  203. * which controls the given node.
  204. *
  205. * @dev: devioe to check
  206. * @return number of address cells this node uses
  207. */
  208. int dev_read_addr_cells(struct udevice *dev);
  209. /**
  210. * dev_read_size_cells() - Get the number of size cells for a device's node
  211. *
  212. * This walks back up the tree to find the closest #size-cells property
  213. * which controls the given node.
  214. *
  215. * @dev: devioe to check
  216. * @return number of size cells this node uses
  217. */
  218. int dev_read_size_cells(struct udevice *dev);
  219. /**
  220. * dev_read_addr_cells() - Get the address cells property in a node
  221. *
  222. * This function matches fdt_address_cells().
  223. *
  224. * @dev: devioe to check
  225. * @return number of address cells this node uses
  226. */
  227. int dev_read_simple_addr_cells(struct udevice *dev);
  228. /**
  229. * dev_read_size_cells() - Get the size cells property in a node
  230. *
  231. * This function matches fdt_size_cells().
  232. *
  233. * @dev: devioe to check
  234. * @return number of size cells this node uses
  235. */
  236. int dev_read_simple_size_cells(struct udevice *dev);
  237. /**
  238. * dev_read_phandle() - Get the phandle from a device
  239. *
  240. * @dev: device to check
  241. * @return phandle (1 or greater), or 0 if no phandle or other error
  242. */
  243. int dev_read_phandle(struct udevice *dev);
  244. /**
  245. * dev_read_prop()- - read a property from a device's node
  246. *
  247. * @dev: device to check
  248. * @propname: property to read
  249. * @lenp: place to put length on success
  250. * @return pointer to property, or NULL if not found
  251. */
  252. const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
  253. /**
  254. * dev_read_alias_seq() - Get the alias sequence number of a node
  255. *
  256. * This works out whether a node is pointed to by an alias, and if so, the
  257. * sequence number of that alias. Aliases are of the form <base><num> where
  258. * <num> is the sequence number. For example spi2 would be sequence number 2.
  259. *
  260. * @dev: device to look up
  261. * @devnump: set to the sequence number if one is found
  262. * @return 0 if a sequence was found, -ve if not
  263. */
  264. int dev_read_alias_seq(struct udevice *dev, int *devnump);
  265. /**
  266. * dev_read_u32_array() - Find and read an array of 32 bit integers
  267. *
  268. * Search for a property in a device node and read 32-bit value(s) from
  269. * it.
  270. *
  271. * The out_values is modified only if a valid u32 value can be decoded.
  272. *
  273. * @dev: device to look up
  274. * @propname: name of the property to read
  275. * @out_values: pointer to return value, modified only if return value is 0
  276. * @sz: number of array elements to read
  277. * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
  278. * property does not have a value, and -EOVERFLOW if the property data isn't
  279. * large enough.
  280. */
  281. int dev_read_u32_array(struct udevice *dev, const char *propname,
  282. u32 *out_values, size_t sz);
  283. /**
  284. * dev_read_first_subnode() - find the first subnode of a device's node
  285. *
  286. * @dev: device to look up
  287. * @return reference to the first subnode (which can be invalid if the device's
  288. * node has no subnodes)
  289. */
  290. ofnode dev_read_first_subnode(struct udevice *dev);
  291. /**
  292. * ofnode_next_subnode() - find the next sibling of a subnode
  293. *
  294. * @node: valid reference to previous node (sibling)
  295. * @return reference to the next subnode (which can be invalid if the node
  296. * has no more siblings)
  297. */
  298. ofnode dev_read_next_subnode(ofnode node);
  299. /**
  300. * dev_read_u8_array_ptr() - find an 8-bit array
  301. *
  302. * Look up a device's node property and return a pointer to its contents as a
  303. * byte array of given length. The property must have at least enough data
  304. * for the array (count bytes). It may have more, but this will be ignored.
  305. * The data is not copied.
  306. *
  307. * @dev: device to look up
  308. * @propname: name of property to find
  309. * @sz: number of array elements
  310. * @return pointer to byte array if found, or NULL if the property is not
  311. * found or there is not enough data
  312. */
  313. const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
  314. size_t sz);
  315. /**
  316. * dev_read_enabled() - check whether a node is enabled
  317. *
  318. * This looks for a 'status' property. If this exists, then returns 1 if
  319. * the status is 'ok' and 0 otherwise. If there is no status property,
  320. * it returns 1 on the assumption that anything mentioned should be enabled
  321. * by default.
  322. *
  323. * @dev: device to examine
  324. * @return integer value 0 (not enabled) or 1 (enabled)
  325. */
  326. int dev_read_enabled(struct udevice *dev);
  327. /**
  328. * dev_read_resource() - obtain an indexed resource from a device.
  329. *
  330. * @dev: device to examine
  331. * @index index of the resource to retrieve (0 = first)
  332. * @res returns the resource
  333. * @return 0 if ok, negative on error
  334. */
  335. int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
  336. /**
  337. * dev_read_resource_byname() - obtain a named resource from a device.
  338. *
  339. * @dev: device to examine
  340. * @name: name of the resource to retrieve
  341. * @res: returns the resource
  342. * @return 0 if ok, negative on error
  343. */
  344. int dev_read_resource_byname(struct udevice *dev, const char *name,
  345. struct resource *res);
  346. #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
  347. static inline int dev_read_u32_default(struct udevice *dev,
  348. const char *propname, int def)
  349. {
  350. return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
  351. }
  352. static inline const char *dev_read_string(struct udevice *dev,
  353. const char *propname)
  354. {
  355. return ofnode_read_string(dev_ofnode(dev), propname);
  356. }
  357. static inline bool dev_read_bool(struct udevice *dev, const char *propname)
  358. {
  359. return ofnode_read_bool(dev_ofnode(dev), propname);
  360. }
  361. static inline ofnode dev_read_subnode(struct udevice *dev,
  362. const char *subbnode_name)
  363. {
  364. return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
  365. }
  366. static inline int dev_read_size(struct udevice *dev, const char *propname)
  367. {
  368. return ofnode_read_size(dev_ofnode(dev), propname);
  369. }
  370. static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
  371. {
  372. return devfdt_get_addr_index(dev, index);
  373. }
  374. static inline fdt_addr_t dev_read_addr(struct udevice *dev)
  375. {
  376. return devfdt_get_addr(dev);
  377. }
  378. static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
  379. const char *propname,
  380. fdt_size_t *sizep)
  381. {
  382. return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
  383. }
  384. static inline const char *dev_read_name(struct udevice *dev)
  385. {
  386. return ofnode_get_name(dev_ofnode(dev));
  387. }
  388. static inline int dev_read_stringlist_search(struct udevice *dev,
  389. const char *propname,
  390. const char *string)
  391. {
  392. return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
  393. }
  394. static inline int dev_read_phandle_with_args(struct udevice *dev,
  395. const char *list_name, const char *cells_name, int cell_count,
  396. int index, struct ofnode_phandle_args *out_args)
  397. {
  398. return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
  399. cells_name, cell_count, index,
  400. out_args);
  401. }
  402. static inline int dev_count_phandle_with_args(struct udevice *dev,
  403. const char *list_name, const char *cells_name)
  404. {
  405. return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
  406. cells_name);
  407. }
  408. static inline int dev_read_addr_cells(struct udevice *dev)
  409. {
  410. /* NOTE: this call should walk up the parent stack */
  411. return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
  412. }
  413. static inline int dev_read_size_cells(struct udevice *dev)
  414. {
  415. /* NOTE: this call should walk up the parent stack */
  416. return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
  417. }
  418. static inline int dev_read_simple_addr_cells(struct udevice *dev)
  419. {
  420. return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
  421. }
  422. static inline int dev_read_simple_size_cells(struct udevice *dev)
  423. {
  424. return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
  425. }
  426. static inline int dev_read_phandle(struct udevice *dev)
  427. {
  428. return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
  429. }
  430. static inline const void *dev_read_prop(struct udevice *dev,
  431. const char *propname, int *lenp)
  432. {
  433. return ofnode_get_property(dev_ofnode(dev), propname, lenp);
  434. }
  435. static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
  436. {
  437. return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
  438. dev_of_offset(dev), devnump);
  439. }
  440. static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
  441. u32 *out_values, size_t sz)
  442. {
  443. return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
  444. }
  445. static inline ofnode dev_read_first_subnode(struct udevice *dev)
  446. {
  447. return ofnode_first_subnode(dev_ofnode(dev));
  448. }
  449. static inline ofnode dev_read_next_subnode(ofnode node)
  450. {
  451. return ofnode_next_subnode(node);
  452. }
  453. static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
  454. const char *propname, size_t sz)
  455. {
  456. return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
  457. }
  458. static inline int dev_read_enabled(struct udevice *dev)
  459. {
  460. return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
  461. }
  462. static inline int dev_read_resource(struct udevice *dev, uint index,
  463. struct resource *res)
  464. {
  465. return ofnode_read_resource(dev_ofnode(dev), index, res);
  466. }
  467. static inline int dev_read_resource_byname(struct udevice *dev,
  468. const char *name,
  469. struct resource *res)
  470. {
  471. return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
  472. }
  473. #endif /* CONFIG_DM_DEV_READ_INLINE */
  474. /**
  475. * dev_for_each_subnode() - Helper function to iterate through subnodes
  476. *
  477. * This creates a for() loop which works through the subnodes in a device's
  478. * device-tree node.
  479. *
  480. * @subnode: ofnode holding the current subnode
  481. * @dev: device to use for interation (struct udevice *)
  482. */
  483. #define dev_for_each_subnode(subnode, dev) \
  484. for (subnode = dev_read_first_subnode(dev); \
  485. ofnode_valid(subnode); \
  486. subnode = ofnode_next_subnode(subnode))
  487. #endif