read.h 26 KB

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