read.h 31 KB

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