read.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Function to read values from the device tree node attached to a udevice.
  4. *
  5. * Copyright (c) 2017 Google, Inc
  6. * Written by Simon Glass <sjg@chromium.org>
  7. */
  8. #ifndef _DM_READ_H
  9. #define _DM_READ_H
  10. #include <linux/errno.h>
  11. #include <dm/device.h>
  12. #include <dm/fdtaddr.h>
  13. #include <dm/ofnode.h>
  14. #include <dm/uclass.h>
  15. struct resource;
  16. #if CONFIG_IS_ENABLED(OF_LIVE)
  17. static inline const struct device_node *dev_np(const struct udevice *dev)
  18. {
  19. return ofnode_to_np(dev->node);
  20. }
  21. #else
  22. static inline const struct device_node *dev_np(const struct udevice *dev)
  23. {
  24. return NULL;
  25. }
  26. #endif
  27. /**
  28. * dev_ofnode() - get the DT node reference associated with a udevice
  29. *
  30. * @dev: device to check
  31. * @return reference of the the device's DT node
  32. */
  33. static inline ofnode dev_ofnode(const struct udevice *dev)
  34. {
  35. return dev->node;
  36. }
  37. static inline bool dev_of_valid(const struct udevice *dev)
  38. {
  39. return ofnode_valid(dev_ofnode(dev));
  40. }
  41. #ifndef CONFIG_DM_DEV_READ_INLINE
  42. /**
  43. * dev_read_u32() - read a 32-bit integer from a device's DT property
  44. *
  45. * @dev: device to read DT property from
  46. * @propname: name of the property to read from
  47. * @outp: place to put value (if found)
  48. * @return 0 if OK, -ve on error
  49. */
  50. int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
  51. /**
  52. * dev_read_u32_default() - read a 32-bit integer from a device's DT property
  53. *
  54. * @dev: device to read DT property from
  55. * @propname: name of the property to read from
  56. * @def: default value to return if the property has no value
  57. * @return property value, or @def if not found
  58. */
  59. int dev_read_u32_default(const struct udevice *dev, const char *propname,
  60. int def);
  61. /**
  62. * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
  63. * property
  64. *
  65. * @dev: device to read DT property from
  66. * @propname: name of the property to read from
  67. * @index: index of the integer to return
  68. * @outp: place to put value (if found)
  69. * @return 0 if OK, -ve on error
  70. */
  71. int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
  72. u32 *outp);
  73. /**
  74. * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
  75. * DT property
  76. *
  77. * @dev: device to read DT property from
  78. * @propname: name of the property to read from
  79. * @index: index of the integer to return
  80. * @def: default value to return if the property has no value
  81. * @return property value, or @def if not found
  82. */
  83. u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
  84. int index, u32 def);
  85. /**
  86. * dev_read_s32() - read a signed 32-bit integer from a device's DT property
  87. *
  88. * @dev: device to read DT property from
  89. * @propname: name of the property to read from
  90. * @outp: place to put value (if found)
  91. * @return 0 if OK, -ve on error
  92. */
  93. int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
  94. /**
  95. * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
  96. *
  97. * @dev: device to read DT property from
  98. * @propname: name of the property to read from
  99. * @def: default value to return if the property has no value
  100. * @return property value, or @def if not found
  101. */
  102. int dev_read_s32_default(const struct udevice *dev, const char *propname,
  103. int def);
  104. /**
  105. * dev_read_u32u() - read a 32-bit integer from a device's DT property
  106. *
  107. * This version uses a standard uint type.
  108. *
  109. * @dev: device to read DT property from
  110. * @propname: name of the property to read from
  111. * @outp: place to put value (if found)
  112. * @return 0 if OK, -ve on error
  113. */
  114. int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
  115. /**
  116. * dev_read_u64() - read a 64-bit integer from a device's DT property
  117. *
  118. * @dev: device to read DT property from
  119. * @propname: name of the property to read from
  120. * @outp: place to put value (if found)
  121. * @return 0 if OK, -ve on error
  122. */
  123. int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
  124. /**
  125. * dev_read_u64_default() - read a 64-bit integer from a device's DT property
  126. *
  127. * @dev: device to read DT property from
  128. * @propname: name of the property to read from
  129. * @def: default value to return if the property has no value
  130. * @return property value, or @def if not found
  131. */
  132. u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
  133. u64 def);
  134. /**
  135. * dev_read_string() - Read a string from a device's DT property
  136. *
  137. * @dev: device to read DT property from
  138. * @propname: name of the property to read
  139. * @return string from property value, or NULL if there is no such property
  140. */
  141. const char *dev_read_string(const struct udevice *dev, const char *propname);
  142. /**
  143. * dev_read_bool() - read a boolean value from a device's DT property
  144. *
  145. * @dev: device to read DT property from
  146. * @propname: name of property to read
  147. * @return true if property is present (meaning true), false if not present
  148. */
  149. bool dev_read_bool(const struct udevice *dev, const char *propname);
  150. /**
  151. * dev_read_subnode() - find a named subnode of a device
  152. *
  153. * @dev: device whose DT node contains the subnode
  154. * @subnode_name: name of subnode to find
  155. * @return reference to subnode (which can be invalid if there is no such
  156. * subnode)
  157. */
  158. ofnode dev_read_subnode(const struct udevice *dev, const char *subbnode_name);
  159. /**
  160. * dev_read_size() - read the size of a property
  161. *
  162. * @dev: device to check
  163. * @propname: property to check
  164. * @return size of property if present, or -EINVAL if not
  165. */
  166. int dev_read_size(const struct udevice *dev, const char *propname);
  167. /**
  168. * dev_read_addr_index() - Get the indexed reg property of a device
  169. *
  170. * @dev: Device to read from
  171. * @index: the 'reg' property can hold a list of <addr, size> pairs
  172. * and @index is used to select which one is required
  173. *
  174. * @return address or FDT_ADDR_T_NONE if not found
  175. */
  176. fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
  177. /**
  178. * dev_read_addr_size_index() - Get the indexed reg property of a device
  179. *
  180. * @dev: Device to read from
  181. * @index: the 'reg' property can hold a list of <addr, size> pairs
  182. * and @index is used to select which one is required
  183. * @size: place to put size value (on success)
  184. *
  185. * @return address or FDT_ADDR_T_NONE if not found
  186. */
  187. fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
  188. fdt_size_t *size);
  189. /**
  190. * dev_remap_addr_index() - Get the indexed reg property of a device
  191. * as a memory-mapped I/O pointer
  192. *
  193. * @dev: Device to read from
  194. * @index: the 'reg' property can hold a list of <addr, size> pairs
  195. * and @index is used to select which one is required
  196. *
  197. * @return pointer or NULL if not found
  198. */
  199. void *dev_remap_addr_index(const struct udevice *dev, int index);
  200. /**
  201. * dev_read_addr_name() - Get the reg property of a device, indexed by name
  202. *
  203. * @dev: Device to read from
  204. * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
  205. * 'reg-names' property providing named-based identification. @index
  206. * indicates the value to search for in 'reg-names'.
  207. *
  208. * @return address or FDT_ADDR_T_NONE if not found
  209. */
  210. fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
  211. /**
  212. * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
  213. *
  214. * @dev: Device to read from
  215. * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
  216. * 'reg-names' property providing named-based identification. @index
  217. * indicates the value to search for in 'reg-names'.
  218. * @size: place to put size value (on success)
  219. *
  220. * @return address or FDT_ADDR_T_NONE if not found
  221. */
  222. fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
  223. fdt_size_t *size);
  224. /**
  225. * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
  226. * as a memory-mapped I/O pointer
  227. *
  228. * @dev: Device to read from
  229. * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
  230. * 'reg-names' property providing named-based identification. @index
  231. * indicates the value to search for in 'reg-names'.
  232. *
  233. * @return pointer or NULL if not found
  234. */
  235. void *dev_remap_addr_name(const struct udevice *dev, const char *name);
  236. /**
  237. * dev_read_addr() - Get the reg property of a device
  238. *
  239. * @dev: Device to read from
  240. *
  241. * @return address or FDT_ADDR_T_NONE if not found
  242. */
  243. fdt_addr_t dev_read_addr(const struct udevice *dev);
  244. /**
  245. * dev_read_addr_ptr() - Get the reg property of a device
  246. * as a pointer
  247. *
  248. * @dev: Device to read from
  249. *
  250. * @return pointer or NULL if not found
  251. */
  252. void *dev_read_addr_ptr(const struct udevice *dev);
  253. /**
  254. * dev_read_addr_pci() - Read an address and handle PCI address translation
  255. *
  256. * At present U-Boot does not have address translation logic for PCI in the
  257. * livetree implementation (of_addr.c). This special function supports this for
  258. * the flat tree implementation.
  259. *
  260. * This function should be removed (and code should use dev_read() instead)
  261. * once:
  262. *
  263. * 1. PCI address translation is added; and either
  264. * 2. everything uses livetree where PCI translation is used (which is feasible
  265. * in SPL and U-Boot proper) or PCI address translation is added to
  266. * fdtdec_get_addr() and friends.
  267. *
  268. * @dev: Device to read from
  269. * @return address or FDT_ADDR_T_NONE if not found
  270. */
  271. fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
  272. /**
  273. * dev_remap_addr() - Get the reg property of a device as a
  274. * memory-mapped I/O pointer
  275. *
  276. * @dev: Device to read from
  277. *
  278. * @return pointer or NULL if not found
  279. */
  280. void *dev_remap_addr(const struct udevice *dev);
  281. /**
  282. * dev_read_addr_size() - get address and size from a device property
  283. *
  284. * This does no address translation. It simply reads an property that contains
  285. * an address and a size value, one after the other.
  286. *
  287. * @dev: Device to read from
  288. * @propname: property to read
  289. * @sizep: place to put size value (on success)
  290. * @return address value, or FDT_ADDR_T_NONE on error
  291. */
  292. fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname,
  293. fdt_size_t *sizep);
  294. /**
  295. * dev_read_name() - get the name of a device's node
  296. *
  297. * @dev: Device to read from
  298. * @return name of node
  299. */
  300. const char *dev_read_name(const struct udevice *dev);
  301. /**
  302. * dev_read_stringlist_search() - find string in a string list and return index
  303. *
  304. * Note that it is possible for this function to succeed on property values
  305. * that are not NUL-terminated. That's because the function will stop after
  306. * finding the first occurrence of @string. This can for example happen with
  307. * small-valued cell properties, such as #address-cells, when searching for
  308. * the empty string.
  309. *
  310. * @dev: device to check
  311. * @propname: name of the property containing the string list
  312. * @string: string to look up in the string list
  313. *
  314. * @return:
  315. * the index of the string in the list of strings
  316. * -ENODATA if the property is not found
  317. * -EINVAL on some other error
  318. */
  319. int dev_read_stringlist_search(const struct udevice *dev, const char *property,
  320. const char *string);
  321. /**
  322. * dev_read_string_index() - obtain an indexed string from a string list
  323. *
  324. * @dev: device to examine
  325. * @propname: name of the property containing the string list
  326. * @index: index of the string to return
  327. * @out: return location for the string
  328. *
  329. * @return:
  330. * length of string, if found or -ve error value if not found
  331. */
  332. int dev_read_string_index(const struct udevice *dev, const char *propname,
  333. int index, const char **outp);
  334. /**
  335. * dev_read_string_count() - find the number of strings in a string list
  336. *
  337. * @dev: device to examine
  338. * @propname: name of the property containing the string list
  339. * @return:
  340. * number of strings in the list, or -ve error value if not found
  341. */
  342. int dev_read_string_count(const struct udevice *dev, const char *propname);
  343. /**
  344. * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
  345. *
  346. * This function is useful to parse lists of phandles and their arguments.
  347. * Returns 0 on success and fills out_args, on error returns appropriate
  348. * errno value.
  349. *
  350. * Caller is responsible to call of_node_put() on the returned out_args->np
  351. * pointer.
  352. *
  353. * Example:
  354. *
  355. * phandle1: node1 {
  356. * #list-cells = <2>;
  357. * }
  358. *
  359. * phandle2: node2 {
  360. * #list-cells = <1>;
  361. * }
  362. *
  363. * node3 {
  364. * list = <&phandle1 1 2 &phandle2 3>;
  365. * }
  366. *
  367. * To get a device_node of the `node2' node you may call this:
  368. * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
  369. *
  370. * @dev: device whose node containing a list
  371. * @list_name: property name that contains a list
  372. * @cells_name: property name that specifies phandles' arguments count
  373. * @cells_count: Cell count to use if @cells_name is NULL
  374. * @index: index of a phandle to parse out
  375. * @out_args: optional pointer to output arguments structure (will be filled)
  376. * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
  377. * @list_name does not exist, -EINVAL if a phandle was not found,
  378. * @cells_name could not be found, the arguments were truncated or there
  379. * were too many arguments.
  380. */
  381. int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
  382. const char *cells_name, int cell_count,
  383. int index, struct ofnode_phandle_args *out_args);
  384. /**
  385. * dev_count_phandle_with_args() - Return phandle number in a list
  386. *
  387. * This function is usefull to get phandle number contained in a property list.
  388. * For example, this allows to allocate the right amount of memory to keep
  389. * clock's reference contained into the "clocks" property.
  390. *
  391. *
  392. * @dev: device whose node containing a list
  393. * @list_name: property name that contains a list
  394. * @cells_name: property name that specifies phandles' arguments count
  395. * @Returns number of phandle found on success, on error returns appropriate
  396. * errno value.
  397. */
  398. int dev_count_phandle_with_args(const struct udevice *dev,
  399. const char *list_name, const char *cells_name);
  400. /**
  401. * dev_read_addr_cells() - Get the number of address cells for a device's node
  402. *
  403. * This walks back up the tree to find the closest #address-cells property
  404. * which controls the given node.
  405. *
  406. * @dev: device to check
  407. * @return number of address cells this node uses
  408. */
  409. int dev_read_addr_cells(const struct udevice *dev);
  410. /**
  411. * dev_read_size_cells() - Get the number of size cells for a device's node
  412. *
  413. * This walks back up the tree to find the closest #size-cells property
  414. * which controls the given node.
  415. *
  416. * @dev: device to check
  417. * @return number of size cells this node uses
  418. */
  419. int dev_read_size_cells(const struct udevice *dev);
  420. /**
  421. * dev_read_addr_cells() - Get the address cells property in a node
  422. *
  423. * This function matches fdt_address_cells().
  424. *
  425. * @dev: device to check
  426. * @return number of address cells this node uses
  427. */
  428. int dev_read_simple_addr_cells(const struct udevice *dev);
  429. /**
  430. * dev_read_size_cells() - Get the size cells property in a node
  431. *
  432. * This function matches fdt_size_cells().
  433. *
  434. * @dev: device to check
  435. * @return number of size cells this node uses
  436. */
  437. int dev_read_simple_size_cells(const struct udevice *dev);
  438. /**
  439. * dev_read_phandle() - Get the phandle from a device
  440. *
  441. * @dev: device to check
  442. * @return phandle (1 or greater), or 0 if no phandle or other error
  443. */
  444. int dev_read_phandle(const struct udevice *dev);
  445. /**
  446. * dev_read_prop()- - read a property from a device's node
  447. *
  448. * @dev: device to check
  449. * @propname: property to read
  450. * @lenp: place to put length on success
  451. * @return pointer to property, or NULL if not found
  452. */
  453. const void *dev_read_prop(const struct udevice *dev, const char *propname,
  454. int *lenp);
  455. /**
  456. * dev_read_first_prop()- get the reference of the first property
  457. *
  458. * Get reference to the first property of the node, it is used to iterate
  459. * and read all the property with dev_read_prop_by_prop().
  460. *
  461. * @dev: device to check
  462. * @prop: place to put argument reference
  463. * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  464. */
  465. int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
  466. /**
  467. * ofnode_get_next_property() - get the reference of the next property
  468. *
  469. * Get reference to the next property of the node, it is used to iterate
  470. * and read all the property with dev_read_prop_by_prop().
  471. *
  472. * @prop: reference of current argument and place to put reference of next one
  473. * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  474. */
  475. int dev_read_next_prop(struct ofprop *prop);
  476. /**
  477. * dev_read_prop_by_prop() - get a pointer to the value of a property
  478. *
  479. * Get value for the property identified by the provided reference.
  480. *
  481. * @prop: reference on property
  482. * @propname: If non-NULL, place to property name on success,
  483. * @lenp: If non-NULL, place to put length on success
  484. * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  485. */
  486. const void *dev_read_prop_by_prop(struct ofprop *prop,
  487. const char **propname, int *lenp);
  488. /**
  489. * dev_read_alias_seq() - Get the alias sequence number of a node
  490. *
  491. * This works out whether a node is pointed to by an alias, and if so, the
  492. * sequence number of that alias. Aliases are of the form <base><num> where
  493. * <num> is the sequence number. For example spi2 would be sequence number 2.
  494. *
  495. * @dev: device to look up
  496. * @devnump: set to the sequence number if one is found
  497. * @return 0 if a sequence was found, -ve if not
  498. */
  499. int dev_read_alias_seq(const struct udevice *dev, int *devnump);
  500. /**
  501. * dev_read_u32_array() - Find and read an array of 32 bit integers
  502. *
  503. * Search for a property in a device node and read 32-bit value(s) from
  504. * it.
  505. *
  506. * The out_values is modified only if a valid u32 value can be decoded.
  507. *
  508. * @dev: device to look up
  509. * @propname: name of the property to read
  510. * @out_values: pointer to return value, modified only if return value is 0
  511. * @sz: number of array elements to read
  512. * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
  513. * property does not have a value, and -EOVERFLOW if the property data isn't
  514. * large enough.
  515. */
  516. int dev_read_u32_array(const struct udevice *dev, const char *propname,
  517. u32 *out_values, size_t sz);
  518. /**
  519. * dev_read_first_subnode() - find the first subnode of a device's node
  520. *
  521. * @dev: device to look up
  522. * @return reference to the first subnode (which can be invalid if the device's
  523. * node has no subnodes)
  524. */
  525. ofnode dev_read_first_subnode(const struct udevice *dev);
  526. /**
  527. * ofnode_next_subnode() - find the next sibling of a subnode
  528. *
  529. * @node: valid reference to previous node (sibling)
  530. * @return reference to the next subnode (which can be invalid if the node
  531. * has no more siblings)
  532. */
  533. ofnode dev_read_next_subnode(ofnode node);
  534. /**
  535. * dev_read_u8_array_ptr() - find an 8-bit array
  536. *
  537. * Look up a device's node property and return a pointer to its contents as a
  538. * byte array of given length. The property must have at least enough data
  539. * for the array (count bytes). It may have more, but this will be ignored.
  540. * The data is not copied.
  541. *
  542. * @dev: device to look up
  543. * @propname: name of property to find
  544. * @sz: number of array elements
  545. * @return pointer to byte array if found, or NULL if the property is not
  546. * found or there is not enough data
  547. */
  548. const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
  549. const char *propname, size_t sz);
  550. /**
  551. * dev_read_enabled() - check whether a node is enabled
  552. *
  553. * This looks for a 'status' property. If this exists, then returns 1 if
  554. * the status is 'ok' and 0 otherwise. If there is no status property,
  555. * it returns 1 on the assumption that anything mentioned should be enabled
  556. * by default.
  557. *
  558. * @dev: device to examine
  559. * @return integer value 0 (not enabled) or 1 (enabled)
  560. */
  561. int dev_read_enabled(const struct udevice *dev);
  562. /**
  563. * dev_read_resource() - obtain an indexed resource from a device.
  564. *
  565. * @dev: device to examine
  566. * @index index of the resource to retrieve (0 = first)
  567. * @res returns the resource
  568. * @return 0 if ok, negative on error
  569. */
  570. int dev_read_resource(const struct udevice *dev, uint index,
  571. struct resource *res);
  572. /**
  573. * dev_read_resource_byname() - obtain a named resource from a device.
  574. *
  575. * @dev: device to examine
  576. * @name: name of the resource to retrieve
  577. * @res: returns the resource
  578. * @return 0 if ok, negative on error
  579. */
  580. int dev_read_resource_byname(const struct udevice *dev, const char *name,
  581. struct resource *res);
  582. /**
  583. * dev_translate_address() - Translate a device-tree address
  584. *
  585. * Translate an address from the device-tree into a CPU physical address. This
  586. * function walks up the tree and applies the various bus mappings along the
  587. * way.
  588. *
  589. * @dev: device giving the context in which to translate the address
  590. * @in_addr: pointer to the address to translate
  591. * @return the translated address; OF_BAD_ADDR on error
  592. */
  593. u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
  594. /**
  595. * dev_translate_dma_address() - Translate a device-tree DMA address
  596. *
  597. * Translate a DMA address from the device-tree into a CPU physical address.
  598. * This function walks up the tree and applies the various bus mappings along
  599. * the way.
  600. *
  601. * @dev: device giving the context in which to translate the DMA address
  602. * @in_addr: pointer to the DMA address to translate
  603. * @return the translated DMA address; OF_BAD_ADDR on error
  604. */
  605. u64 dev_translate_dma_address(const struct udevice *dev,
  606. const fdt32_t *in_addr);
  607. /**
  608. * dev_read_alias_highest_id - Get highest alias id for the given stem
  609. * @stem: Alias stem to be examined
  610. *
  611. * The function travels the lookup table to get the highest alias id for the
  612. * given alias stem.
  613. * @return alias ID, if found, else -1
  614. */
  615. int dev_read_alias_highest_id(const char *stem);
  616. /**
  617. * dev_get_child_count() - get the child count of a device
  618. *
  619. * @dev: device to use for interation (struct udevice *)
  620. * @return the count of child subnode
  621. */
  622. int dev_get_child_count(const struct udevice *dev);
  623. #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
  624. static inline int dev_read_u32(const struct udevice *dev,
  625. const char *propname, u32 *outp)
  626. {
  627. return ofnode_read_u32(dev_ofnode(dev), propname, outp);
  628. }
  629. static inline int dev_read_u32_default(const struct udevice *dev,
  630. const char *propname, int def)
  631. {
  632. return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
  633. }
  634. static inline int dev_read_u32_index(struct udevice *dev,
  635. const char *propname, int index, u32 *outp)
  636. {
  637. return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
  638. }
  639. static inline u32 dev_read_u32_index_default(struct udevice *dev,
  640. const char *propname, int index,
  641. u32 def)
  642. {
  643. return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
  644. def);
  645. }
  646. static inline int dev_read_s32(const struct udevice *dev,
  647. const char *propname, s32 *outp)
  648. {
  649. return ofnode_read_s32(dev_ofnode(dev), propname, outp);
  650. }
  651. static inline int dev_read_s32_default(const struct udevice *dev,
  652. const char *propname, int def)
  653. {
  654. return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
  655. }
  656. static inline int dev_read_u32u(const struct udevice *dev,
  657. const char *propname, uint *outp)
  658. {
  659. u32 val;
  660. int ret;
  661. ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
  662. if (ret)
  663. return ret;
  664. *outp = val;
  665. return 0;
  666. }
  667. static inline int dev_read_u64(const struct udevice *dev,
  668. const char *propname, u64 *outp)
  669. {
  670. return ofnode_read_u64(dev_ofnode(dev), propname, outp);
  671. }
  672. static inline u64 dev_read_u64_default(const struct udevice *dev,
  673. const char *propname, u64 def)
  674. {
  675. return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
  676. }
  677. static inline const char *dev_read_string(const struct udevice *dev,
  678. const char *propname)
  679. {
  680. return ofnode_read_string(dev_ofnode(dev), propname);
  681. }
  682. static inline bool dev_read_bool(const struct udevice *dev,
  683. const char *propname)
  684. {
  685. return ofnode_read_bool(dev_ofnode(dev), propname);
  686. }
  687. static inline ofnode dev_read_subnode(const struct udevice *dev,
  688. const char *subbnode_name)
  689. {
  690. return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
  691. }
  692. static inline int dev_read_size(const struct udevice *dev, const char *propname)
  693. {
  694. return ofnode_read_size(dev_ofnode(dev), propname);
  695. }
  696. static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
  697. int index)
  698. {
  699. return devfdt_get_addr_index(dev, index);
  700. }
  701. static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
  702. int index,
  703. fdt_size_t *size)
  704. {
  705. return devfdt_get_addr_size_index(dev, index, size);
  706. }
  707. static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
  708. const char *name)
  709. {
  710. return devfdt_get_addr_name(dev, name);
  711. }
  712. static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
  713. const char *name,
  714. fdt_size_t *size)
  715. {
  716. return devfdt_get_addr_size_name(dev, name, size);
  717. }
  718. static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
  719. {
  720. return devfdt_get_addr(dev);
  721. }
  722. static inline void *dev_read_addr_ptr(const struct udevice *dev)
  723. {
  724. void *addr = devfdt_get_addr_ptr(dev);
  725. return ((fdt_addr_t)(uintptr_t)addr == FDT_ADDR_T_NONE) ? NULL : addr;
  726. }
  727. static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
  728. {
  729. return devfdt_get_addr_pci(dev);
  730. }
  731. static inline void *dev_remap_addr(const struct udevice *dev)
  732. {
  733. return devfdt_remap_addr(dev);
  734. }
  735. static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
  736. {
  737. return devfdt_remap_addr_index(dev, index);
  738. }
  739. static inline void *dev_remap_addr_name(const struct udevice *dev,
  740. const char *name)
  741. {
  742. return devfdt_remap_addr_name(dev, name);
  743. }
  744. static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
  745. const char *propname,
  746. fdt_size_t *sizep)
  747. {
  748. return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
  749. }
  750. static inline const char *dev_read_name(const struct udevice *dev)
  751. {
  752. return ofnode_get_name(dev_ofnode(dev));
  753. }
  754. static inline int dev_read_stringlist_search(const struct udevice *dev,
  755. const char *propname,
  756. const char *string)
  757. {
  758. return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
  759. }
  760. static inline int dev_read_string_index(const struct udevice *dev,
  761. const char *propname, int index,
  762. const char **outp)
  763. {
  764. return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
  765. }
  766. static inline int dev_read_string_count(const struct udevice *dev,
  767. const char *propname)
  768. {
  769. return ofnode_read_string_count(dev_ofnode(dev), propname);
  770. }
  771. static inline int dev_read_phandle_with_args(const struct udevice *dev,
  772. const char *list_name, const char *cells_name, int cell_count,
  773. int index, struct ofnode_phandle_args *out_args)
  774. {
  775. return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
  776. cells_name, cell_count, index,
  777. out_args);
  778. }
  779. static inline int dev_count_phandle_with_args(const struct udevice *dev,
  780. const char *list_name, const char *cells_name)
  781. {
  782. return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
  783. cells_name);
  784. }
  785. static inline int dev_read_addr_cells(const struct udevice *dev)
  786. {
  787. /* NOTE: this call should walk up the parent stack */
  788. return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
  789. }
  790. static inline int dev_read_size_cells(const struct udevice *dev)
  791. {
  792. /* NOTE: this call should walk up the parent stack */
  793. return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
  794. }
  795. static inline int dev_read_simple_addr_cells(const struct udevice *dev)
  796. {
  797. return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
  798. }
  799. static inline int dev_read_simple_size_cells(const struct udevice *dev)
  800. {
  801. return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
  802. }
  803. static inline int dev_read_phandle(const struct udevice *dev)
  804. {
  805. return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
  806. }
  807. static inline const void *dev_read_prop(const struct udevice *dev,
  808. const char *propname, int *lenp)
  809. {
  810. return ofnode_get_property(dev_ofnode(dev), propname, lenp);
  811. }
  812. static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
  813. {
  814. return ofnode_get_first_property(dev_ofnode(dev), prop);
  815. }
  816. static inline int dev_read_next_prop(struct ofprop *prop)
  817. {
  818. return ofnode_get_next_property(prop);
  819. }
  820. static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
  821. const char **propname,
  822. int *lenp)
  823. {
  824. return ofnode_get_property_by_prop(prop, propname, lenp);
  825. }
  826. static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
  827. {
  828. #if CONFIG_IS_ENABLED(OF_CONTROL)
  829. return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
  830. dev_of_offset(dev), devnump);
  831. #else
  832. return -ENOTSUPP;
  833. #endif
  834. }
  835. static inline int dev_read_u32_array(const struct udevice *dev,
  836. const char *propname, u32 *out_values,
  837. size_t sz)
  838. {
  839. return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
  840. }
  841. static inline ofnode dev_read_first_subnode(const struct udevice *dev)
  842. {
  843. return ofnode_first_subnode(dev_ofnode(dev));
  844. }
  845. static inline ofnode dev_read_next_subnode(ofnode node)
  846. {
  847. return ofnode_next_subnode(node);
  848. }
  849. static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
  850. const char *propname,
  851. size_t sz)
  852. {
  853. return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
  854. }
  855. static inline int dev_read_enabled(const struct udevice *dev)
  856. {
  857. return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
  858. }
  859. static inline int dev_read_resource(const struct udevice *dev, uint index,
  860. struct resource *res)
  861. {
  862. return ofnode_read_resource(dev_ofnode(dev), index, res);
  863. }
  864. static inline int dev_read_resource_byname(const struct udevice *dev,
  865. const char *name,
  866. struct resource *res)
  867. {
  868. return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
  869. }
  870. static inline u64 dev_translate_address(const struct udevice *dev,
  871. const fdt32_t *in_addr)
  872. {
  873. return ofnode_translate_address(dev_ofnode(dev), in_addr);
  874. }
  875. static inline u64 dev_translate_dma_address(const struct udevice *dev,
  876. const fdt32_t *in_addr)
  877. {
  878. return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
  879. }
  880. static inline int dev_read_alias_highest_id(const char *stem)
  881. {
  882. if (!CONFIG_IS_ENABLED(OF_LIBFDT))
  883. return -1;
  884. return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
  885. }
  886. static inline int dev_get_child_count(const struct udevice *dev)
  887. {
  888. return ofnode_get_child_count(dev_ofnode(dev));
  889. }
  890. #endif /* CONFIG_DM_DEV_READ_INLINE */
  891. /**
  892. * dev_for_each_subnode() - Helper function to iterate through subnodes
  893. *
  894. * This creates a for() loop which works through the subnodes in a device's
  895. * device-tree node.
  896. *
  897. * @subnode: ofnode holding the current subnode
  898. * @dev: device to use for interation (struct udevice *)
  899. */
  900. #define dev_for_each_subnode(subnode, dev) \
  901. for (subnode = dev_read_first_subnode(dev); \
  902. ofnode_valid(subnode); \
  903. subnode = ofnode_next_subnode(subnode))
  904. /**
  905. * dev_for_each_property() - Helper function to iterate through property
  906. *
  907. * This creates a for() loop which works through the property in a device's
  908. * device-tree node.
  909. *
  910. * @prop: struct ofprop holding the current property
  911. * @dev: device to use for interation (struct udevice *)
  912. */
  913. #define dev_for_each_property(prop, dev) \
  914. for (int ret_prop = dev_read_first_prop(dev, &prop); \
  915. !ret_prop; \
  916. ret_prop = dev_read_next_prop(&prop))
  917. #endif