read.h 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. * @cells_count: Cell count to use if @cells_name is NULL
  396. * @Returns number of phandle found on success, on error returns appropriate
  397. * errno value.
  398. */
  399. int dev_count_phandle_with_args(const struct udevice *dev,
  400. const char *list_name, const char *cells_name,
  401. int cell_count);
  402. /**
  403. * dev_read_addr_cells() - Get the number of address cells for a device's node
  404. *
  405. * This walks back up the tree to find the closest #address-cells property
  406. * which controls the given node.
  407. *
  408. * @dev: device to check
  409. * @return number of address cells this node uses
  410. */
  411. int dev_read_addr_cells(const struct udevice *dev);
  412. /**
  413. * dev_read_size_cells() - Get the number of size cells for a device's node
  414. *
  415. * This walks back up the tree to find the closest #size-cells property
  416. * which controls the given node.
  417. *
  418. * @dev: device to check
  419. * @return number of size cells this node uses
  420. */
  421. int dev_read_size_cells(const struct udevice *dev);
  422. /**
  423. * dev_read_addr_cells() - Get the address cells property in a node
  424. *
  425. * This function matches fdt_address_cells().
  426. *
  427. * @dev: device to check
  428. * @return number of address cells this node uses
  429. */
  430. int dev_read_simple_addr_cells(const struct udevice *dev);
  431. /**
  432. * dev_read_size_cells() - Get the size cells property in a node
  433. *
  434. * This function matches fdt_size_cells().
  435. *
  436. * @dev: device to check
  437. * @return number of size cells this node uses
  438. */
  439. int dev_read_simple_size_cells(const struct udevice *dev);
  440. /**
  441. * dev_read_phandle() - Get the phandle from a device
  442. *
  443. * @dev: device to check
  444. * @return phandle (1 or greater), or 0 if no phandle or other error
  445. */
  446. int dev_read_phandle(const struct udevice *dev);
  447. /**
  448. * dev_read_prop()- - read a property from a device's node
  449. *
  450. * @dev: device to check
  451. * @propname: property to read
  452. * @lenp: place to put length on success
  453. * @return pointer to property, or NULL if not found
  454. */
  455. const void *dev_read_prop(const struct udevice *dev, const char *propname,
  456. int *lenp);
  457. /**
  458. * dev_read_first_prop()- get the reference of the first property
  459. *
  460. * Get reference to the first property of the node, it is used to iterate
  461. * and read all the property with dev_read_prop_by_prop().
  462. *
  463. * @dev: device to check
  464. * @prop: place to put argument reference
  465. * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  466. */
  467. int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
  468. /**
  469. * ofnode_get_next_property() - get the reference of the next property
  470. *
  471. * Get reference to the next property of the node, it is used to iterate
  472. * and read all the property with dev_read_prop_by_prop().
  473. *
  474. * @prop: reference of current argument and place to put reference of next one
  475. * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  476. */
  477. int dev_read_next_prop(struct ofprop *prop);
  478. /**
  479. * dev_read_prop_by_prop() - get a pointer to the value of a property
  480. *
  481. * Get value for the property identified by the provided reference.
  482. *
  483. * @prop: reference on property
  484. * @propname: If non-NULL, place to property name on success,
  485. * @lenp: If non-NULL, place to put length on success
  486. * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  487. */
  488. const void *dev_read_prop_by_prop(struct ofprop *prop,
  489. const char **propname, int *lenp);
  490. /**
  491. * dev_read_alias_seq() - Get the alias sequence number of a node
  492. *
  493. * This works out whether a node is pointed to by an alias, and if so, the
  494. * sequence number of that alias. Aliases are of the form <base><num> where
  495. * <num> is the sequence number. For example spi2 would be sequence number 2.
  496. *
  497. * @dev: device to look up
  498. * @devnump: set to the sequence number if one is found
  499. * @return 0 if a sequence was found, -ve if not
  500. */
  501. int dev_read_alias_seq(const struct udevice *dev, int *devnump);
  502. /**
  503. * dev_read_u32_array() - Find and read an array of 32 bit integers
  504. *
  505. * Search for a property in a device node and read 32-bit value(s) from
  506. * it.
  507. *
  508. * The out_values is modified only if a valid u32 value can be decoded.
  509. *
  510. * @dev: device to look up
  511. * @propname: name of the property to read
  512. * @out_values: pointer to return value, modified only if return value is 0
  513. * @sz: number of array elements to read
  514. * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
  515. * property does not have a value, and -EOVERFLOW if the property data isn't
  516. * large enough.
  517. */
  518. int dev_read_u32_array(const struct udevice *dev, const char *propname,
  519. u32 *out_values, size_t sz);
  520. /**
  521. * dev_read_first_subnode() - find the first subnode of a device's node
  522. *
  523. * @dev: device to look up
  524. * @return reference to the first subnode (which can be invalid if the device's
  525. * node has no subnodes)
  526. */
  527. ofnode dev_read_first_subnode(const struct udevice *dev);
  528. /**
  529. * ofnode_next_subnode() - find the next sibling of a subnode
  530. *
  531. * @node: valid reference to previous node (sibling)
  532. * @return reference to the next subnode (which can be invalid if the node
  533. * has no more siblings)
  534. */
  535. ofnode dev_read_next_subnode(ofnode node);
  536. /**
  537. * dev_read_u8_array_ptr() - find an 8-bit array
  538. *
  539. * Look up a device's node property and return a pointer to its contents as a
  540. * byte array of given length. The property must have at least enough data
  541. * for the array (count bytes). It may have more, but this will be ignored.
  542. * The data is not copied.
  543. *
  544. * @dev: device to look up
  545. * @propname: name of property to find
  546. * @sz: number of array elements
  547. * @return pointer to byte array if found, or NULL if the property is not
  548. * found or there is not enough data
  549. */
  550. const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
  551. const char *propname, size_t sz);
  552. /**
  553. * dev_read_enabled() - check whether a node is enabled
  554. *
  555. * This looks for a 'status' property. If this exists, then returns 1 if
  556. * the status is 'ok' and 0 otherwise. If there is no status property,
  557. * it returns 1 on the assumption that anything mentioned should be enabled
  558. * by default.
  559. *
  560. * @dev: device to examine
  561. * @return integer value 0 (not enabled) or 1 (enabled)
  562. */
  563. int dev_read_enabled(const struct udevice *dev);
  564. /**
  565. * dev_read_resource() - obtain an indexed resource from a device.
  566. *
  567. * @dev: device to examine
  568. * @index index of the resource to retrieve (0 = first)
  569. * @res returns the resource
  570. * @return 0 if ok, negative on error
  571. */
  572. int dev_read_resource(const struct udevice *dev, uint index,
  573. struct resource *res);
  574. /**
  575. * dev_read_resource_byname() - obtain a named resource from a device.
  576. *
  577. * @dev: device to examine
  578. * @name: name of the resource to retrieve
  579. * @res: returns the resource
  580. * @return 0 if ok, negative on error
  581. */
  582. int dev_read_resource_byname(const struct udevice *dev, const char *name,
  583. struct resource *res);
  584. /**
  585. * dev_translate_address() - Translate a device-tree address
  586. *
  587. * Translate an address from the device-tree into a CPU physical address. This
  588. * function walks up the tree and applies the various bus mappings along the
  589. * way.
  590. *
  591. * @dev: device giving the context in which to translate the address
  592. * @in_addr: pointer to the address to translate
  593. * @return the translated address; OF_BAD_ADDR on error
  594. */
  595. u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
  596. /**
  597. * dev_translate_dma_address() - Translate a device-tree DMA address
  598. *
  599. * Translate a DMA address from the device-tree into a CPU physical address.
  600. * This function walks up the tree and applies the various bus mappings along
  601. * the way.
  602. *
  603. * @dev: device giving the context in which to translate the DMA address
  604. * @in_addr: pointer to the DMA address to translate
  605. * @return the translated DMA address; OF_BAD_ADDR on error
  606. */
  607. u64 dev_translate_dma_address(const struct udevice *dev,
  608. const fdt32_t *in_addr);
  609. /**
  610. * dev_read_alias_highest_id - Get highest alias id for the given stem
  611. * @stem: Alias stem to be examined
  612. *
  613. * The function travels the lookup table to get the highest alias id for the
  614. * given alias stem.
  615. * @return alias ID, if found, else -1
  616. */
  617. int dev_read_alias_highest_id(const char *stem);
  618. /**
  619. * dev_get_child_count() - get the child count of a device
  620. *
  621. * @dev: device to use for interation (struct udevice *)
  622. * @return the count of child subnode
  623. */
  624. int dev_get_child_count(const struct udevice *dev);
  625. /**
  626. * dev_read_pci_bus_range - Read PCI bus-range resource
  627. *
  628. * Look at the bus range property of a device node and return the pci bus
  629. * range for this node.
  630. *
  631. * @dev: device to examine
  632. * @res returns the resource
  633. * @return 0 if ok, negative on error
  634. */
  635. int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
  636. #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
  637. static inline int dev_read_u32(const struct udevice *dev,
  638. const char *propname, u32 *outp)
  639. {
  640. return ofnode_read_u32(dev_ofnode(dev), propname, outp);
  641. }
  642. static inline int dev_read_u32_default(const struct udevice *dev,
  643. const char *propname, int def)
  644. {
  645. return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
  646. }
  647. static inline int dev_read_u32_index(struct udevice *dev,
  648. const char *propname, int index, u32 *outp)
  649. {
  650. return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
  651. }
  652. static inline u32 dev_read_u32_index_default(struct udevice *dev,
  653. const char *propname, int index,
  654. u32 def)
  655. {
  656. return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
  657. def);
  658. }
  659. static inline int dev_read_s32(const struct udevice *dev,
  660. const char *propname, s32 *outp)
  661. {
  662. return ofnode_read_s32(dev_ofnode(dev), propname, outp);
  663. }
  664. static inline int dev_read_s32_default(const struct udevice *dev,
  665. const char *propname, int def)
  666. {
  667. return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
  668. }
  669. static inline int dev_read_u32u(const struct udevice *dev,
  670. const char *propname, uint *outp)
  671. {
  672. u32 val;
  673. int ret;
  674. ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
  675. if (ret)
  676. return ret;
  677. *outp = val;
  678. return 0;
  679. }
  680. static inline int dev_read_u64(const struct udevice *dev,
  681. const char *propname, u64 *outp)
  682. {
  683. return ofnode_read_u64(dev_ofnode(dev), propname, outp);
  684. }
  685. static inline u64 dev_read_u64_default(const struct udevice *dev,
  686. const char *propname, u64 def)
  687. {
  688. return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
  689. }
  690. static inline const char *dev_read_string(const struct udevice *dev,
  691. const char *propname)
  692. {
  693. return ofnode_read_string(dev_ofnode(dev), propname);
  694. }
  695. static inline bool dev_read_bool(const struct udevice *dev,
  696. const char *propname)
  697. {
  698. return ofnode_read_bool(dev_ofnode(dev), propname);
  699. }
  700. static inline ofnode dev_read_subnode(const struct udevice *dev,
  701. const char *subbnode_name)
  702. {
  703. return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
  704. }
  705. static inline int dev_read_size(const struct udevice *dev, const char *propname)
  706. {
  707. return ofnode_read_size(dev_ofnode(dev), propname);
  708. }
  709. static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
  710. int index)
  711. {
  712. return devfdt_get_addr_index(dev, index);
  713. }
  714. static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
  715. int index,
  716. fdt_size_t *size)
  717. {
  718. return devfdt_get_addr_size_index(dev, index, size);
  719. }
  720. static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
  721. const char *name)
  722. {
  723. return devfdt_get_addr_name(dev, name);
  724. }
  725. static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
  726. const char *name,
  727. fdt_size_t *size)
  728. {
  729. return devfdt_get_addr_size_name(dev, name, size);
  730. }
  731. static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
  732. {
  733. return devfdt_get_addr(dev);
  734. }
  735. static inline void *dev_read_addr_ptr(const struct udevice *dev)
  736. {
  737. return devfdt_get_addr_ptr(dev);
  738. }
  739. static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
  740. {
  741. return devfdt_get_addr_pci(dev);
  742. }
  743. static inline void *dev_remap_addr(const struct udevice *dev)
  744. {
  745. return devfdt_remap_addr(dev);
  746. }
  747. static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
  748. {
  749. return devfdt_remap_addr_index(dev, index);
  750. }
  751. static inline void *dev_remap_addr_name(const struct udevice *dev,
  752. const char *name)
  753. {
  754. return devfdt_remap_addr_name(dev, name);
  755. }
  756. static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
  757. const char *propname,
  758. fdt_size_t *sizep)
  759. {
  760. return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
  761. }
  762. static inline const char *dev_read_name(const struct udevice *dev)
  763. {
  764. return ofnode_get_name(dev_ofnode(dev));
  765. }
  766. static inline int dev_read_stringlist_search(const struct udevice *dev,
  767. const char *propname,
  768. const char *string)
  769. {
  770. return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
  771. }
  772. static inline int dev_read_string_index(const struct udevice *dev,
  773. const char *propname, int index,
  774. const char **outp)
  775. {
  776. return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
  777. }
  778. static inline int dev_read_string_count(const struct udevice *dev,
  779. const char *propname)
  780. {
  781. return ofnode_read_string_count(dev_ofnode(dev), propname);
  782. }
  783. static inline int dev_read_phandle_with_args(const struct udevice *dev,
  784. const char *list_name, const char *cells_name, int cell_count,
  785. int index, struct ofnode_phandle_args *out_args)
  786. {
  787. return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
  788. cells_name, cell_count, index,
  789. out_args);
  790. }
  791. static inline int dev_count_phandle_with_args(const struct udevice *dev,
  792. const char *list_name, const char *cells_name, int cell_count)
  793. {
  794. return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
  795. cells_name, cell_count);
  796. }
  797. static inline int dev_read_addr_cells(const struct udevice *dev)
  798. {
  799. int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
  800. return fdt_address_cells(gd->fdt_blob, parent);
  801. }
  802. static inline int dev_read_size_cells(const struct udevice *dev)
  803. {
  804. int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
  805. return fdt_size_cells(gd->fdt_blob, parent);
  806. }
  807. static inline int dev_read_simple_addr_cells(const struct udevice *dev)
  808. {
  809. return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
  810. }
  811. static inline int dev_read_simple_size_cells(const struct udevice *dev)
  812. {
  813. return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
  814. }
  815. static inline int dev_read_phandle(const struct udevice *dev)
  816. {
  817. return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
  818. }
  819. static inline const void *dev_read_prop(const struct udevice *dev,
  820. const char *propname, int *lenp)
  821. {
  822. return ofnode_get_property(dev_ofnode(dev), propname, lenp);
  823. }
  824. static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
  825. {
  826. return ofnode_get_first_property(dev_ofnode(dev), prop);
  827. }
  828. static inline int dev_read_next_prop(struct ofprop *prop)
  829. {
  830. return ofnode_get_next_property(prop);
  831. }
  832. static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
  833. const char **propname,
  834. int *lenp)
  835. {
  836. return ofnode_get_property_by_prop(prop, propname, lenp);
  837. }
  838. static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
  839. {
  840. #if CONFIG_IS_ENABLED(OF_CONTROL)
  841. return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
  842. dev_of_offset(dev), devnump);
  843. #else
  844. return -ENOTSUPP;
  845. #endif
  846. }
  847. static inline int dev_read_u32_array(const struct udevice *dev,
  848. const char *propname, u32 *out_values,
  849. size_t sz)
  850. {
  851. return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
  852. }
  853. static inline ofnode dev_read_first_subnode(const struct udevice *dev)
  854. {
  855. return ofnode_first_subnode(dev_ofnode(dev));
  856. }
  857. static inline ofnode dev_read_next_subnode(ofnode node)
  858. {
  859. return ofnode_next_subnode(node);
  860. }
  861. static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
  862. const char *propname,
  863. size_t sz)
  864. {
  865. return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
  866. }
  867. static inline int dev_read_enabled(const struct udevice *dev)
  868. {
  869. return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
  870. }
  871. static inline int dev_read_resource(const struct udevice *dev, uint index,
  872. struct resource *res)
  873. {
  874. return ofnode_read_resource(dev_ofnode(dev), index, res);
  875. }
  876. static inline int dev_read_resource_byname(const struct udevice *dev,
  877. const char *name,
  878. struct resource *res)
  879. {
  880. return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
  881. }
  882. static inline u64 dev_translate_address(const struct udevice *dev,
  883. const fdt32_t *in_addr)
  884. {
  885. return ofnode_translate_address(dev_ofnode(dev), in_addr);
  886. }
  887. static inline u64 dev_translate_dma_address(const struct udevice *dev,
  888. const fdt32_t *in_addr)
  889. {
  890. return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
  891. }
  892. static inline int dev_read_alias_highest_id(const char *stem)
  893. {
  894. if (!CONFIG_IS_ENABLED(OF_LIBFDT))
  895. return -1;
  896. return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
  897. }
  898. static inline int dev_get_child_count(const struct udevice *dev)
  899. {
  900. return ofnode_get_child_count(dev_ofnode(dev));
  901. }
  902. #endif /* CONFIG_DM_DEV_READ_INLINE */
  903. /**
  904. * dev_for_each_subnode() - Helper function to iterate through subnodes
  905. *
  906. * This creates a for() loop which works through the subnodes in a device's
  907. * device-tree node.
  908. *
  909. * @subnode: ofnode holding the current subnode
  910. * @dev: device to use for interation (struct udevice *)
  911. */
  912. #define dev_for_each_subnode(subnode, dev) \
  913. for (subnode = dev_read_first_subnode(dev); \
  914. ofnode_valid(subnode); \
  915. subnode = ofnode_next_subnode(subnode))
  916. /**
  917. * dev_for_each_property() - Helper function to iterate through property
  918. *
  919. * This creates a for() loop which works through the property in a device's
  920. * device-tree node.
  921. *
  922. * @prop: struct ofprop holding the current property
  923. * @dev: device to use for interation (struct udevice *)
  924. */
  925. #define dev_for_each_property(prop, dev) \
  926. for (int ret_prop = dev_read_first_prop(dev, &prop); \
  927. !ret_prop; \
  928. ret_prop = dev_read_next_prop(&prop))
  929. #endif