ofnode.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2017 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #ifndef _DM_OFNODE_H
  7. #define _DM_OFNODE_H
  8. /* TODO(sjg@chromium.org): Drop fdtdec.h include */
  9. #include <fdtdec.h>
  10. #include <dm/of.h>
  11. #include <dm/of_access.h>
  12. #include <log.h>
  13. /* Enable checks to protect against invalid calls */
  14. #undef OF_CHECKS
  15. struct resource;
  16. /**
  17. * ofnode - reference to a device tree node
  18. *
  19. * This union can hold either a straightforward pointer to a struct device_node
  20. * in the live device tree, or an offset within the flat device tree. In the
  21. * latter case, the pointer value is just the integer offset within the flat DT.
  22. *
  23. * Thus we can reference nodes in both the live tree (once available) and the
  24. * flat tree (until then). Functions are available to translate between an
  25. * ofnode and either an offset or a struct device_node *.
  26. *
  27. * The reference can also hold a null offset, in which case the pointer value
  28. * here is NULL. This corresponds to a struct device_node * value of
  29. * NULL, or an offset of -1.
  30. *
  31. * There is no ambiguity as to whether ofnode holds an offset or a node
  32. * pointer: when the live tree is active it holds a node pointer, otherwise it
  33. * holds an offset. The value itself does not need to be unique and in theory
  34. * the same value could point to a valid device node or a valid offset. We
  35. * could arrange for a unique value to be used (e.g. by making the pointer
  36. * point to an offset within the flat device tree in the case of an offset) but
  37. * this increases code size slightly due to the subtraction. Since it offers no
  38. * real benefit, the approach described here seems best.
  39. *
  40. * For now these points use constant types, since we don't allow writing
  41. * the DT.
  42. *
  43. * @np: Pointer to device node, used for live tree
  44. * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
  45. * is not a really a pointer to a node: it is an offset value. See above.
  46. */
  47. typedef union ofnode_union {
  48. const struct device_node *np;
  49. long of_offset;
  50. } ofnode;
  51. struct ofnode_phandle_args {
  52. ofnode node;
  53. int args_count;
  54. uint32_t args[OF_MAX_PHANDLE_ARGS];
  55. };
  56. /**
  57. * ofprop - reference to a property of a device tree node
  58. *
  59. * This struct hold the reference on one property of one node,
  60. * using struct ofnode and an offset within the flat device tree or either
  61. * a pointer to a struct property in the live device tree.
  62. *
  63. * Thus we can reference arguments in both the live tree and the flat tree.
  64. *
  65. * The property reference can also hold a null reference. This corresponds to
  66. * a struct property NULL pointer or an offset of -1.
  67. *
  68. * @node: Pointer to device node
  69. * @offset: Pointer into flat device tree, used for flat tree.
  70. * @prop: Pointer to property, used for live treee.
  71. */
  72. struct ofprop {
  73. ofnode node;
  74. union {
  75. int offset;
  76. const struct property *prop;
  77. };
  78. };
  79. /**
  80. * ofnode_to_np() - convert an ofnode to a live DT node pointer
  81. *
  82. * This cannot be called if the reference contains an offset.
  83. *
  84. * @node: Reference containing struct device_node * (possibly invalid)
  85. * @return pointer to device node (can be NULL)
  86. */
  87. static inline const struct device_node *ofnode_to_np(ofnode node)
  88. {
  89. #ifdef OF_CHECKS
  90. if (!of_live_active())
  91. return NULL;
  92. #endif
  93. return node.np;
  94. }
  95. /**
  96. * ofnode_to_offset() - convert an ofnode to a flat DT offset
  97. *
  98. * This cannot be called if the reference contains a node pointer.
  99. *
  100. * @node: Reference containing offset (possibly invalid)
  101. * @return DT offset (can be -1)
  102. */
  103. static inline int ofnode_to_offset(ofnode node)
  104. {
  105. #ifdef OF_CHECKS
  106. if (of_live_active())
  107. return -1;
  108. #endif
  109. return node.of_offset;
  110. }
  111. /**
  112. * ofnode_valid() - check if an ofnode is valid
  113. *
  114. * @return true if the reference contains a valid ofnode, false if it is NULL
  115. */
  116. static inline bool ofnode_valid(ofnode node)
  117. {
  118. if (of_live_active())
  119. return node.np != NULL;
  120. else
  121. return node.of_offset >= 0;
  122. }
  123. /**
  124. * offset_to_ofnode() - convert a DT offset to an ofnode
  125. *
  126. * @of_offset: DT offset (either valid, or -1)
  127. * @return reference to the associated DT offset
  128. */
  129. static inline ofnode offset_to_ofnode(int of_offset)
  130. {
  131. ofnode node;
  132. if (of_live_active())
  133. node.np = NULL;
  134. else
  135. node.of_offset = of_offset >= 0 ? of_offset : -1;
  136. return node;
  137. }
  138. /**
  139. * np_to_ofnode() - convert a node pointer to an ofnode
  140. *
  141. * @np: Live node pointer (can be NULL)
  142. * @return reference to the associated node pointer
  143. */
  144. static inline ofnode np_to_ofnode(const struct device_node *np)
  145. {
  146. ofnode node;
  147. node.np = np;
  148. return node;
  149. }
  150. /**
  151. * ofnode_is_np() - check if a reference is a node pointer
  152. *
  153. * This function associated that if there is a valid live tree then all
  154. * references will use it. This is because using the flat DT when the live tree
  155. * is valid is not permitted.
  156. *
  157. * @node: reference to check (possibly invalid)
  158. * @return true if the reference is a live node pointer, false if it is a DT
  159. * offset
  160. */
  161. static inline bool ofnode_is_np(ofnode node)
  162. {
  163. #ifdef OF_CHECKS
  164. /*
  165. * Check our assumption that flat tree offsets are not used when a
  166. * live tree is in use.
  167. */
  168. assert(!ofnode_valid(node) ||
  169. (of_live_active() ? ofnode_to_np(node)
  170. : ofnode_to_np(node)));
  171. #endif
  172. return of_live_active() && ofnode_valid(node);
  173. }
  174. /**
  175. * ofnode_equal() - check if two references are equal
  176. *
  177. * @return true if equal, else false
  178. */
  179. static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
  180. {
  181. /* We only need to compare the contents */
  182. return ref1.of_offset == ref2.of_offset;
  183. }
  184. /**
  185. * ofnode_null() - Obtain a null ofnode
  186. *
  187. * This returns an ofnode which points to no node. It works both with the flat
  188. * tree and livetree.
  189. */
  190. static inline ofnode ofnode_null(void)
  191. {
  192. ofnode node;
  193. if (of_live_active())
  194. node.np = NULL;
  195. else
  196. node.of_offset = -1;
  197. return node;
  198. }
  199. static inline ofnode ofnode_root(void)
  200. {
  201. ofnode node;
  202. if (of_live_active())
  203. node.np = gd_of_root();
  204. else
  205. node.of_offset = 0;
  206. return node;
  207. }
  208. /**
  209. * ofnode_name_eq() - Check if the node name is equivalent to a given name
  210. * ignoring the unit address
  211. *
  212. * @node: valid node reference that has to be compared
  213. * @name: name that has to be compared with the node name
  214. * @return true if matches, false if it doesn't match
  215. */
  216. bool ofnode_name_eq(ofnode node, const char *name);
  217. /**
  218. * ofnode_read_u32() - Read a 32-bit integer from a property
  219. *
  220. * @ref: valid node reference to read property from
  221. * @propname: name of the property to read from
  222. * @outp: place to put value (if found)
  223. * @return 0 if OK, -ve on error
  224. */
  225. int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
  226. /**
  227. * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
  228. *
  229. * @ref: valid node reference to read property from
  230. * @propname: name of the property to read from
  231. * @index: index of the integer to return
  232. * @outp: place to put value (if found)
  233. * @return 0 if OK, -ve on error
  234. */
  235. int ofnode_read_u32_index(ofnode node, const char *propname, int index,
  236. u32 *outp);
  237. /**
  238. * ofnode_read_s32() - Read a 32-bit integer from a property
  239. *
  240. * @ref: valid node reference to read property from
  241. * @propname: name of the property to read from
  242. * @outp: place to put value (if found)
  243. * @return 0 if OK, -ve on error
  244. */
  245. static inline int ofnode_read_s32(ofnode node, const char *propname,
  246. s32 *out_value)
  247. {
  248. return ofnode_read_u32(node, propname, (u32 *)out_value);
  249. }
  250. /**
  251. * ofnode_read_u32_default() - Read a 32-bit integer from a property
  252. *
  253. * @ref: valid node reference to read property from
  254. * @propname: name of the property to read from
  255. * @def: default value to return if the property has no value
  256. * @return property value, or @def if not found
  257. */
  258. u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
  259. /**
  260. * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
  261. * property
  262. *
  263. * @ref: valid node reference to read property from
  264. * @propname: name of the property to read from
  265. * @index: index of the integer to return
  266. * @def: default value to return if the property has no value
  267. * @return property value, or @def if not found
  268. */
  269. u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
  270. u32 def);
  271. /**
  272. * ofnode_read_s32_default() - Read a 32-bit integer from a property
  273. *
  274. * @ref: valid node reference to read property from
  275. * @propname: name of the property to read from
  276. * @def: default value to return if the property has no value
  277. * @return property value, or @def if not found
  278. */
  279. int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
  280. /**
  281. * ofnode_read_u64() - Read a 64-bit integer from a property
  282. *
  283. * @node: valid node reference to read property from
  284. * @propname: name of the property to read from
  285. * @outp: place to put value (if found)
  286. * @return 0 if OK, -ve on error
  287. */
  288. int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
  289. /**
  290. * ofnode_read_u64_default() - Read a 64-bit integer from a property
  291. *
  292. * @ref: valid node reference to read property from
  293. * @propname: name of the property to read from
  294. * @def: default value to return if the property has no value
  295. * @return property value, or @def if not found
  296. */
  297. u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
  298. /**
  299. * ofnode_read_prop() - Read a property from a node
  300. *
  301. * @node: valid node reference to read property from
  302. * @propname: name of the property to read
  303. * @sizep: if non-NULL, returns the size of the property, or an error code
  304. if not found
  305. * @return property value, or NULL if there is no such property
  306. */
  307. const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
  308. /**
  309. * ofnode_read_string() - Read a string from a property
  310. *
  311. * @node: valid node reference to read property from
  312. * @propname: name of the property to read
  313. * @return string from property value, or NULL if there is no such property
  314. */
  315. const char *ofnode_read_string(ofnode node, const char *propname);
  316. /**
  317. * ofnode_read_u32_array() - Find and read an array of 32 bit integers
  318. *
  319. * @node: valid node reference to read property from
  320. * @propname: name of the property to read
  321. * @out_values: pointer to return value, modified only if return value is 0
  322. * @sz: number of array elements to read
  323. * @return 0 if OK, -ve on error
  324. *
  325. * Search for a property in a device node and read 32-bit value(s) from
  326. * it. Returns 0 on success, -EINVAL if the property does not exist,
  327. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  328. * property data isn't large enough.
  329. *
  330. * The out_values is modified only if a valid u32 value can be decoded.
  331. */
  332. int ofnode_read_u32_array(ofnode node, const char *propname,
  333. u32 *out_values, size_t sz);
  334. /**
  335. * ofnode_read_bool() - read a boolean value from a property
  336. *
  337. * @node: valid node reference to read property from
  338. * @propname: name of property to read
  339. * @return true if property is present (meaning true), false if not present
  340. */
  341. bool ofnode_read_bool(ofnode node, const char *propname);
  342. /**
  343. * ofnode_find_subnode() - find a named subnode of a parent node
  344. *
  345. * @node: valid reference to parent node
  346. * @subnode_name: name of subnode to find
  347. * @return reference to subnode (which can be invalid if there is no such
  348. * subnode)
  349. */
  350. ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
  351. #if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
  352. #include <asm/global_data.h>
  353. static inline bool ofnode_is_enabled(ofnode node)
  354. {
  355. if (ofnode_is_np(node)) {
  356. return of_device_is_available(ofnode_to_np(node));
  357. } else {
  358. return fdtdec_get_is_enabled(gd->fdt_blob,
  359. ofnode_to_offset(node));
  360. }
  361. }
  362. static inline ofnode ofnode_first_subnode(ofnode node)
  363. {
  364. assert(ofnode_valid(node));
  365. if (ofnode_is_np(node))
  366. return np_to_ofnode(node.np->child);
  367. return offset_to_ofnode(
  368. fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
  369. }
  370. static inline ofnode ofnode_next_subnode(ofnode node)
  371. {
  372. assert(ofnode_valid(node));
  373. if (ofnode_is_np(node))
  374. return np_to_ofnode(node.np->sibling);
  375. return offset_to_ofnode(
  376. fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
  377. }
  378. #else
  379. /**
  380. * ofnode_is_enabled() - Checks whether a node is enabled.
  381. * This looks for a 'status' property. If this exists, then returns true if
  382. * the status is 'okay' and false otherwise. If there is no status property,
  383. * it returns true on the assumption that anything mentioned should be enabled
  384. * by default.
  385. *
  386. * @node: node to examine
  387. * @return false (not enabled) or true (enabled)
  388. */
  389. bool ofnode_is_enabled(ofnode node);
  390. /**
  391. * ofnode_first_subnode() - find the first subnode of a parent node
  392. *
  393. * @node: valid reference to a valid parent node
  394. * @return reference to the first subnode (which can be invalid if the parent
  395. * node has no subnodes)
  396. */
  397. ofnode ofnode_first_subnode(ofnode node);
  398. /**
  399. * ofnode_next_subnode() - find the next sibling of a subnode
  400. *
  401. * @node: valid reference to previous node (sibling)
  402. * @return reference to the next subnode (which can be invalid if the node
  403. * has no more siblings)
  404. */
  405. ofnode ofnode_next_subnode(ofnode node);
  406. #endif /* DM_INLINE_OFNODE */
  407. /**
  408. * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
  409. *
  410. * @node: valid node to look up
  411. * @return ofnode reference of the parent node
  412. */
  413. ofnode ofnode_get_parent(ofnode node);
  414. /**
  415. * ofnode_get_name() - get the name of a node
  416. *
  417. * @node: valid node to look up
  418. * @return name of node
  419. */
  420. const char *ofnode_get_name(ofnode node);
  421. /**
  422. * ofnode_get_path() - get the full path of a node
  423. *
  424. * @node: valid node to look up
  425. * @buf: buffer to write the node path into
  426. * @buflen: buffer size
  427. * @return 0 if OK, -ve on error
  428. */
  429. int ofnode_get_path(ofnode node, char *buf, int buflen);
  430. /**
  431. * ofnode_get_by_phandle() - get ofnode from phandle
  432. *
  433. * @phandle: phandle to look up
  434. * @return ofnode reference to the phandle
  435. */
  436. ofnode ofnode_get_by_phandle(uint phandle);
  437. /**
  438. * ofnode_read_size() - read the size of a property
  439. *
  440. * @node: node to check
  441. * @propname: property to check
  442. * @return size of property if present, or -EINVAL if not
  443. */
  444. int ofnode_read_size(ofnode node, const char *propname);
  445. /**
  446. * ofnode_get_addr_size_index() - get an address/size from a node
  447. * based on index
  448. *
  449. * This reads the register address/size from a node based on index
  450. *
  451. * @node: node to read from
  452. * @index: Index of address to read (0 for first)
  453. * @size: Pointer to size of the address
  454. * @return address, or FDT_ADDR_T_NONE if not present or invalid
  455. */
  456. phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
  457. fdt_size_t *size);
  458. /**
  459. * ofnode_get_addr_size_index_notrans() - get an address/size from a node
  460. * based on index, without address
  461. * translation
  462. *
  463. * This reads the register address/size from a node based on index.
  464. * The resulting address is not translated. Useful for example for on-disk
  465. * addresses.
  466. *
  467. * @node: node to read from
  468. * @index: Index of address to read (0 for first)
  469. * @size: Pointer to size of the address
  470. * @return address, or FDT_ADDR_T_NONE if not present or invalid
  471. */
  472. phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
  473. fdt_size_t *size);
  474. /**
  475. * ofnode_get_addr_index() - get an address from a node
  476. *
  477. * This reads the register address from a node
  478. *
  479. * @node: node to read from
  480. * @index: Index of address to read (0 for first)
  481. * @return address, or FDT_ADDR_T_NONE if not present or invalid
  482. */
  483. phys_addr_t ofnode_get_addr_index(ofnode node, int index);
  484. /**
  485. * ofnode_get_addr() - get an address from a node
  486. *
  487. * This reads the register address from a node
  488. *
  489. * @node: node to read from
  490. * @return address, or FDT_ADDR_T_NONE if not present or invalid
  491. */
  492. phys_addr_t ofnode_get_addr(ofnode node);
  493. /**
  494. * ofnode_get_size() - get size from a node
  495. *
  496. * This reads the register size from a node
  497. *
  498. * @node: node to read from
  499. * @return size of the address, or FDT_SIZE_T_NONE if not present or invalid
  500. */
  501. fdt_size_t ofnode_get_size(ofnode node);
  502. /**
  503. * ofnode_stringlist_search() - find a string in a string list and return index
  504. *
  505. * Note that it is possible for this function to succeed on property values
  506. * that are not NUL-terminated. That's because the function will stop after
  507. * finding the first occurrence of @string. This can for example happen with
  508. * small-valued cell properties, such as #address-cells, when searching for
  509. * the empty string.
  510. *
  511. * @node: node to check
  512. * @propname: name of the property containing the string list
  513. * @string: string to look up in the string list
  514. *
  515. * @return:
  516. * the index of the string in the list of strings
  517. * -ENODATA if the property is not found
  518. * -EINVAL on some other error
  519. */
  520. int ofnode_stringlist_search(ofnode node, const char *propname,
  521. const char *string);
  522. /**
  523. * ofnode_read_string_index() - obtain an indexed string from a string list
  524. *
  525. * Note that this will successfully extract strings from properties with
  526. * non-NUL-terminated values. For example on small-valued cell properties
  527. * this function will return the empty string.
  528. *
  529. * If non-NULL, the length of the string (on success) or a negative error-code
  530. * (on failure) will be stored in the integer pointer to by lenp.
  531. *
  532. * @node: node to check
  533. * @propname: name of the property containing the string list
  534. * @index: index of the string to return
  535. * @lenp: return location for the string length or an error code on failure
  536. *
  537. * @return:
  538. * length of string, if found or -ve error value if not found
  539. */
  540. int ofnode_read_string_index(ofnode node, const char *propname, int index,
  541. const char **outp);
  542. /**
  543. * ofnode_read_string_count() - find the number of strings in a string list
  544. *
  545. * @node: node to check
  546. * @propname: name of the property containing the string list
  547. * @return:
  548. * number of strings in the list, or -ve error value if not found
  549. */
  550. int ofnode_read_string_count(ofnode node, const char *property);
  551. /**
  552. * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
  553. *
  554. * This function is useful to parse lists of phandles and their arguments.
  555. * Returns 0 on success and fills out_args, on error returns appropriate
  556. * errno value.
  557. *
  558. * Caller is responsible to call of_node_put() on the returned out_args->np
  559. * pointer.
  560. *
  561. * Example:
  562. *
  563. * phandle1: node1 {
  564. * #list-cells = <2>;
  565. * }
  566. *
  567. * phandle2: node2 {
  568. * #list-cells = <1>;
  569. * }
  570. *
  571. * node3 {
  572. * list = <&phandle1 1 2 &phandle2 3>;
  573. * }
  574. *
  575. * To get a device_node of the `node2' node you may call this:
  576. * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
  577. *
  578. * @node: device tree node containing a list
  579. * @list_name: property name that contains a list
  580. * @cells_name: property name that specifies phandles' arguments count
  581. * @cells_count: Cell count to use if @cells_name is NULL
  582. * @index: index of a phandle to parse out
  583. * @out_args: optional pointer to output arguments structure (will be filled)
  584. * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
  585. * @list_name does not exist, -EINVAL if a phandle was not found,
  586. * @cells_name could not be found, the arguments were truncated or there
  587. * were too many arguments.
  588. */
  589. int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
  590. const char *cells_name, int cell_count,
  591. int index,
  592. struct ofnode_phandle_args *out_args);
  593. /**
  594. * ofnode_count_phandle_with_args() - Count number of phandle in a list
  595. *
  596. * This function is useful to count phandles into a list.
  597. * Returns number of phandle on success, on error returns appropriate
  598. * errno value.
  599. *
  600. * @node: device tree node containing a list
  601. * @list_name: property name that contains a list
  602. * @cells_name: property name that specifies phandles' arguments count
  603. * @cells_count: Cell count to use if @cells_name is NULL
  604. * @return number of phandle on success, -ENOENT if @list_name does not
  605. * exist, -EINVAL if a phandle was not found, @cells_name could not
  606. * be found.
  607. */
  608. int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
  609. const char *cells_name, int cell_count);
  610. /**
  611. * ofnode_path() - find a node by full path
  612. *
  613. * @path: Full path to node, e.g. "/bus/spi@1"
  614. * @return reference to the node found. Use ofnode_valid() to check if it exists
  615. */
  616. ofnode ofnode_path(const char *path);
  617. /**
  618. * ofnode_read_chosen_prop() - get the value of a chosen property
  619. *
  620. * This looks for a property within the /chosen node and returns its value
  621. *
  622. * @propname: Property name to look for
  623. * @sizep: Returns size of property, or FDT_ERR_... error code if function
  624. * returns NULL
  625. * @return property value if found, else NULL
  626. */
  627. const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
  628. /**
  629. * ofnode_read_chosen_string() - get the string value of a chosen property
  630. *
  631. * This looks for a property within the /chosen node and returns its value,
  632. * checking that it is a valid nul-terminated string
  633. *
  634. * @propname: Property name to look for
  635. * @return string value if found, else NULL
  636. */
  637. const char *ofnode_read_chosen_string(const char *propname);
  638. /**
  639. * ofnode_get_chosen_node() - get a referenced node from the chosen node
  640. *
  641. * This looks up a named property in the chosen node and uses that as a path to
  642. * look up a code.
  643. *
  644. * @return the referenced node if present, else ofnode_null()
  645. */
  646. ofnode ofnode_get_chosen_node(const char *propname);
  647. /**
  648. * ofnode_read_aliases_prop() - get the value of a aliases property
  649. *
  650. * This looks for a property within the /aliases node and returns its value
  651. *
  652. * @propname: Property name to look for
  653. * @sizep: Returns size of property, or FDT_ERR_... error code if function
  654. * returns NULL
  655. * @return property value if found, else NULL
  656. */
  657. const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
  658. /**
  659. * ofnode_get_aliases_node() - get a referenced node from the aliases node
  660. *
  661. * This looks up a named property in the aliases node and uses that as a path to
  662. * look up a code.
  663. *
  664. * @return the referenced node if present, else ofnode_null()
  665. */
  666. ofnode ofnode_get_aliases_node(const char *propname);
  667. struct display_timing;
  668. /**
  669. * ofnode_decode_display_timing() - decode display timings
  670. *
  671. * Decode display timings from the supplied 'display-timings' node.
  672. * See doc/device-tree-bindings/video/display-timing.txt for binding
  673. * information.
  674. *
  675. * @node 'display-timing' node containing the timing subnodes
  676. * @index Index number to read (0=first timing subnode)
  677. * @config Place to put timings
  678. * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
  679. */
  680. int ofnode_decode_display_timing(ofnode node, int index,
  681. struct display_timing *config);
  682. /**
  683. * ofnode_get_property() - get a pointer to the value of a node property
  684. *
  685. * @node: node to read
  686. * @propname: property to read
  687. * @lenp: place to put length on success
  688. * @return pointer to property, or NULL if not found
  689. */
  690. const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
  691. /**
  692. * ofnode_get_first_property()- get the reference of the first property
  693. *
  694. * Get reference to the first property of the node, it is used to iterate
  695. * and read all the property with ofnode_get_property_by_prop().
  696. *
  697. * @node: node to read
  698. * @prop: place to put argument reference
  699. * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  700. */
  701. int ofnode_get_first_property(ofnode node, struct ofprop *prop);
  702. /**
  703. * ofnode_get_next_property() - get the reference of the next property
  704. *
  705. * Get reference to the next property of the node, it is used to iterate
  706. * and read all the property with ofnode_get_property_by_prop().
  707. *
  708. * @prop: reference of current argument and place to put reference of next one
  709. * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  710. */
  711. int ofnode_get_next_property(struct ofprop *prop);
  712. /**
  713. * ofnode_get_property_by_prop() - get a pointer to the value of a property
  714. *
  715. * Get value for the property identified by the provided reference.
  716. *
  717. * @prop: reference on property
  718. * @propname: If non-NULL, place to property name on success,
  719. * @lenp: If non-NULL, place to put length on success
  720. * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
  721. */
  722. const void *ofnode_get_property_by_prop(const struct ofprop *prop,
  723. const char **propname, int *lenp);
  724. /**
  725. * ofnode_is_available() - check if a node is marked available
  726. *
  727. * @node: node to check
  728. * @return true if node's 'status' property is "okay" (or is missing)
  729. */
  730. bool ofnode_is_available(ofnode node);
  731. /**
  732. * ofnode_get_addr_size() - get address and size from a property
  733. *
  734. * This does no address translation. It simply reads an property that contains
  735. * an address and a size value, one after the other.
  736. *
  737. * @node: node to read from
  738. * @propname: property to read
  739. * @sizep: place to put size value (on success)
  740. * @return address value, or FDT_ADDR_T_NONE on error
  741. */
  742. phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
  743. phys_size_t *sizep);
  744. /**
  745. * ofnode_read_u8_array_ptr() - find an 8-bit array
  746. *
  747. * Look up a property in a node and return a pointer to its contents as a
  748. * byte array of given length. The property must have at least enough data
  749. * for the array (count bytes). It may have more, but this will be ignored.
  750. * The data is not copied.
  751. *
  752. * @node node to examine
  753. * @propname name of property to find
  754. * @sz number of array elements
  755. * @return pointer to byte array if found, or NULL if the property is not
  756. * found or there is not enough data
  757. */
  758. const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
  759. size_t sz);
  760. /**
  761. * ofnode_read_pci_addr() - look up a PCI address
  762. *
  763. * Look at an address property in a node and return the PCI address which
  764. * corresponds to the given type in the form of fdt_pci_addr.
  765. * The property must hold one fdt_pci_addr with a lengh.
  766. *
  767. * @node node to examine
  768. * @type pci address type (FDT_PCI_SPACE_xxx)
  769. * @propname name of property to find
  770. * @addr returns pci address in the form of fdt_pci_addr
  771. * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
  772. * format of the property was invalid, -ENXIO if the requested
  773. * address type was not found
  774. */
  775. int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
  776. const char *propname, struct fdt_pci_addr *addr);
  777. /**
  778. * ofnode_read_pci_vendev() - look up PCI vendor and device id
  779. *
  780. * Look at the compatible property of a device node that represents a PCI
  781. * device and extract pci vendor id and device id from it.
  782. *
  783. * @param node node to examine
  784. * @param vendor vendor id of the pci device
  785. * @param device device id of the pci device
  786. * @return 0 if ok, negative on error
  787. */
  788. int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
  789. /**
  790. * ofnode_read_addr_cells() - Get the number of address cells for a node
  791. *
  792. * This walks back up the tree to find the closest #address-cells property
  793. * which controls the given node.
  794. *
  795. * @node: Node to check
  796. * @return number of address cells this node uses
  797. */
  798. int ofnode_read_addr_cells(ofnode node);
  799. /**
  800. * ofnode_read_size_cells() - Get the number of size cells for a node
  801. *
  802. * This walks back up the tree to find the closest #size-cells property
  803. * which controls the given node.
  804. *
  805. * @node: Node to check
  806. * @return number of size cells this node uses
  807. */
  808. int ofnode_read_size_cells(ofnode node);
  809. /**
  810. * ofnode_read_simple_addr_cells() - Get the address cells property in a node
  811. *
  812. * This function matches fdt_address_cells().
  813. *
  814. * @np: Node pointer to check
  815. * @return value of #address-cells property in this node, or 2 if none
  816. */
  817. int ofnode_read_simple_addr_cells(ofnode node);
  818. /**
  819. * ofnode_read_simple_size_cells() - Get the size cells property in a node
  820. *
  821. * This function matches fdt_size_cells().
  822. *
  823. * @np: Node pointer to check
  824. * @return value of #size-cells property in this node, or 2 if none
  825. */
  826. int ofnode_read_simple_size_cells(ofnode node);
  827. /**
  828. * ofnode_pre_reloc() - check if a node should be bound before relocation
  829. *
  830. * Device tree nodes can be marked as needing-to-be-bound in the loader stages
  831. * via special device tree properties.
  832. *
  833. * Before relocation this function can be used to check if nodes are required
  834. * in either SPL or TPL stages.
  835. *
  836. * After relocation and jumping into the real U-Boot binary it is possible to
  837. * determine if a node was bound in one of SPL/TPL stages.
  838. *
  839. * There are 4 settings currently in use
  840. * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
  841. * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
  842. * Existing platforms only use it to indicate nodes needed in
  843. * SPL. Should probably be replaced by u-boot,dm-spl for
  844. * new platforms.
  845. * - u-boot,dm-spl: SPL and U-Boot pre-relocation
  846. * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
  847. *
  848. * @node: node to check
  849. * @return true if node is needed in SPL/TL, false otherwise
  850. */
  851. bool ofnode_pre_reloc(ofnode node);
  852. /**
  853. * ofnode_read_resource() - Read a resource from a node
  854. *
  855. * Read resource information from a node at the given index
  856. *
  857. * @node: Node to read from
  858. * @index: Index of resource to read (0 = first)
  859. * @res: Returns resource that was read, on success
  860. * @return 0 if OK, -ve on error
  861. */
  862. int ofnode_read_resource(ofnode node, uint index, struct resource *res);
  863. /**
  864. * ofnode_read_resource_byname() - Read a resource from a node by name
  865. *
  866. * Read resource information from a node matching the given name. This uses a
  867. * 'reg-names' string list property with the names matching the associated
  868. * 'reg' property list.
  869. *
  870. * @node: Node to read from
  871. * @name: Name of resource to read
  872. * @res: Returns resource that was read, on success
  873. * @return 0 if OK, -ve on error
  874. */
  875. int ofnode_read_resource_byname(ofnode node, const char *name,
  876. struct resource *res);
  877. /**
  878. * ofnode_by_compatible() - Find the next compatible node
  879. *
  880. * Find the next node after @from that is compatible with @compat
  881. *
  882. * @from: ofnode to start from (use ofnode_null() to start at the beginning)
  883. * @compat: Compatible string to match
  884. * @return ofnode found, or ofnode_null() if none
  885. */
  886. ofnode ofnode_by_compatible(ofnode from, const char *compat);
  887. /**
  888. * ofnode_by_prop_value() - Find the next node with given property value
  889. *
  890. * Find the next node after @from that has a @propname with a value
  891. * @propval and a length @proplen.
  892. *
  893. * @from: ofnode to start from (use ofnode_null() to start at the
  894. * beginning) @propname: property name to check @propval: property value to
  895. * search for @proplen: length of the value in propval @return ofnode
  896. * found, or ofnode_null() if none
  897. */
  898. ofnode ofnode_by_prop_value(ofnode from, const char *propname,
  899. const void *propval, int proplen);
  900. /**
  901. * ofnode_for_each_subnode() - iterate over all subnodes of a parent
  902. *
  903. * @node: child node (ofnode, lvalue)
  904. * @parent: parent node (ofnode)
  905. *
  906. * This is a wrapper around a for loop and is used like so:
  907. *
  908. * ofnode node;
  909. *
  910. * ofnode_for_each_subnode(node, parent) {
  911. * Use node
  912. * ...
  913. * }
  914. *
  915. * Note that this is implemented as a macro and @node is used as
  916. * iterator in the loop. The parent variable can be a constant or even a
  917. * literal.
  918. */
  919. #define ofnode_for_each_subnode(node, parent) \
  920. for (node = ofnode_first_subnode(parent); \
  921. ofnode_valid(node); \
  922. node = ofnode_next_subnode(node))
  923. /**
  924. * ofnode_get_child_count() - get the child count of a ofnode
  925. *
  926. * @node: valid node to get its child count
  927. * @return the number of subnodes
  928. */
  929. int ofnode_get_child_count(ofnode parent);
  930. /**
  931. * ofnode_translate_address() - Translate a device-tree address
  932. *
  933. * Translate an address from the device-tree into a CPU physical address. This
  934. * function walks up the tree and applies the various bus mappings along the
  935. * way.
  936. *
  937. * @ofnode: Device tree node giving the context in which to translate the
  938. * address
  939. * @in_addr: pointer to the address to translate
  940. * @return the translated address; OF_BAD_ADDR on error
  941. */
  942. u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
  943. /**
  944. * ofnode_translate_dma_address() - Translate a device-tree DMA address
  945. *
  946. * Translate a DMA address from the device-tree into a CPU physical address.
  947. * This function walks up the tree and applies the various bus mappings along
  948. * the way.
  949. *
  950. * @ofnode: Device tree node giving the context in which to translate the
  951. * DMA address
  952. * @in_addr: pointer to the DMA address to translate
  953. * @return the translated DMA address; OF_BAD_ADDR on error
  954. */
  955. u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
  956. /**
  957. * ofnode_get_dma_range() - get dma-ranges for a specific DT node
  958. *
  959. * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
  960. * cpu->bus address translations
  961. *
  962. * @param blob Pointer to device tree blob
  963. * @param node_offset Node DT offset
  964. * @param cpu Pointer to variable storing the range's cpu address
  965. * @param bus Pointer to variable storing the range's bus address
  966. * @param size Pointer to variable storing the range's size
  967. * @return translated DMA address or OF_BAD_ADDR on error
  968. */
  969. int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
  970. u64 *size);
  971. /**
  972. * ofnode_device_is_compatible() - check if the node is compatible with compat
  973. *
  974. * This allows to check whether the node is comaptible with the compat.
  975. *
  976. * @node: Device tree node for which compatible needs to be verified.
  977. * @compat: Compatible string which needs to verified in the given node.
  978. * @return true if OK, false if the compatible is not found
  979. */
  980. int ofnode_device_is_compatible(ofnode node, const char *compat);
  981. /**
  982. * ofnode_write_prop() - Set a property of a ofnode
  983. *
  984. * Note that the value passed to the function is *not* allocated by the
  985. * function itself, but must be allocated by the caller if necessary.
  986. *
  987. * @node: The node for whose property should be set
  988. * @propname: The name of the property to set
  989. * @len: The length of the new value of the property
  990. * @value: The new value of the property (must be valid prior to calling
  991. * the function)
  992. * @return 0 if successful, -ve on error
  993. */
  994. int ofnode_write_prop(ofnode node, const char *propname, int len,
  995. const void *value);
  996. /**
  997. * ofnode_write_string() - Set a string property of a ofnode
  998. *
  999. * Note that the value passed to the function is *not* allocated by the
  1000. * function itself, but must be allocated by the caller if necessary.
  1001. *
  1002. * @node: The node for whose string property should be set
  1003. * @propname: The name of the string property to set
  1004. * @value: The new value of the string property (must be valid prior to
  1005. * calling the function)
  1006. * @return 0 if successful, -ve on error
  1007. */
  1008. int ofnode_write_string(ofnode node, const char *propname, const char *value);
  1009. /**
  1010. * ofnode_set_enabled() - Enable or disable a device tree node given by its
  1011. * ofnode
  1012. *
  1013. * This function effectively sets the node's "status" property to either "okay"
  1014. * or "disable", hence making it available for driver model initialization or
  1015. * not.
  1016. *
  1017. * @node: The node to enable
  1018. * @value: Flag that tells the function to either disable or enable the
  1019. * node
  1020. * @return 0 if successful, -ve on error
  1021. */
  1022. int ofnode_set_enabled(ofnode node, bool value);
  1023. #endif