read.h 31 KB

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