read.h 18 KB

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