ofnode.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * Copyright (c) 2017 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef _DM_OFNODE_H
  8. #define _DM_OFNODE_H
  9. /* TODO(sjg@chromium.org): Drop fdtdec.h include */
  10. #include <fdtdec.h>
  11. #include <dm/of.h>
  12. /* Enable checks to protect against invalid calls */
  13. #undef OF_CHECKS
  14. struct resource;
  15. /**
  16. * ofnode - reference to a device tree node
  17. *
  18. * This union can hold either a straightforward pointer to a struct device_node
  19. * in the live device tree, or an offset within the flat device tree. In the
  20. * latter case, the pointer value is just the integer offset within the flat DT.
  21. *
  22. * Thus we can reference nodes in both the live tree (once available) and the
  23. * flat tree (until then). Functions are available to translate between an
  24. * ofnode and either an offset or a struct device_node *.
  25. *
  26. * The reference can also hold a null offset, in which case the pointer value
  27. * here is NULL. This corresponds to a struct device_node * value of
  28. * NULL, or an offset of -1.
  29. *
  30. * There is no ambiguity as to whether ofnode holds an offset or a node
  31. * pointer: when the live tree is active it holds a node pointer, otherwise it
  32. * holds an offset. The value itself does not need to be unique and in theory
  33. * the same value could point to a valid device node or a valid offset. We
  34. * could arrange for a unique value to be used (e.g. by making the pointer
  35. * point to an offset within the flat device tree in the case of an offset) but
  36. * this increases code size slightly due to the subtraction. Since it offers no
  37. * real benefit, the approach described here seems best.
  38. *
  39. * For now these points use constant types, since we don't allow writing
  40. * the DT.
  41. *
  42. * @np: Pointer to device node, used for live tree
  43. * @flat_ptr: Pointer into flat device tree, used for flat tree. Note that this
  44. * is not a really a pointer to a node: it is an offset value. See above.
  45. */
  46. typedef union ofnode_union {
  47. const struct device_node *np; /* will be used for future live tree */
  48. long of_offset;
  49. } ofnode;
  50. struct ofnode_phandle_args {
  51. ofnode node;
  52. int args_count;
  53. uint32_t args[OF_MAX_PHANDLE_ARGS];
  54. };
  55. /**
  56. * _ofnode_to_np() - convert an ofnode to a live DT node pointer
  57. *
  58. * This cannot be called if the reference contains an offset.
  59. *
  60. * @node: Reference containing struct device_node * (possibly invalid)
  61. * @return pointer to device node (can be NULL)
  62. */
  63. static inline const struct device_node *ofnode_to_np(ofnode node)
  64. {
  65. #ifdef OF_CHECKS
  66. if (!of_live_active())
  67. return NULL;
  68. #endif
  69. return node.np;
  70. }
  71. /**
  72. * ofnode_to_offset() - convert an ofnode to a flat DT offset
  73. *
  74. * This cannot be called if the reference contains a node pointer.
  75. *
  76. * @node: Reference containing offset (possibly invalid)
  77. * @return DT offset (can be -1)
  78. */
  79. static inline int ofnode_to_offset(ofnode node)
  80. {
  81. #ifdef OF_CHECKS
  82. if (of_live_active())
  83. return -1;
  84. #endif
  85. return node.of_offset;
  86. }
  87. /**
  88. * ofnode_valid() - check if an ofnode is valid
  89. *
  90. * @return true if the reference contains a valid ofnode, false if it is NULL
  91. */
  92. static inline bool ofnode_valid(ofnode node)
  93. {
  94. if (of_live_active())
  95. return node.np != NULL;
  96. else
  97. return node.of_offset != -1;
  98. }
  99. /**
  100. * offset_to_ofnode() - convert a DT offset to an ofnode
  101. *
  102. * @of_offset: DT offset (either valid, or -1)
  103. * @return reference to the associated DT offset
  104. */
  105. static inline ofnode offset_to_ofnode(int of_offset)
  106. {
  107. ofnode node;
  108. if (of_live_active())
  109. node.np = NULL;
  110. else
  111. node.of_offset = of_offset;
  112. return node;
  113. }
  114. /**
  115. * np_to_ofnode() - convert a node pointer to an ofnode
  116. *
  117. * @np: Live node pointer (can be NULL)
  118. * @return reference to the associated node pointer
  119. */
  120. static inline ofnode np_to_ofnode(const struct device_node *np)
  121. {
  122. ofnode node;
  123. node.np = np;
  124. return node;
  125. }
  126. /**
  127. * ofnode_is_np() - check if a reference is a node pointer
  128. *
  129. * This function associated that if there is a valid live tree then all
  130. * references will use it. This is because using the flat DT when the live tree
  131. * is valid is not permitted.
  132. *
  133. * @node: reference to check (possibly invalid)
  134. * @return true if the reference is a live node pointer, false if it is a DT
  135. * offset
  136. */
  137. static inline bool ofnode_is_np(ofnode node)
  138. {
  139. #ifdef OF_CHECKS
  140. /*
  141. * Check our assumption that flat tree offsets are not used when a
  142. * live tree is in use.
  143. */
  144. assert(!ofnode_valid(node) ||
  145. (of_live_active() ? _ofnode_to_np(node)
  146. : _ofnode_to_np(node)));
  147. #endif
  148. return of_live_active() && ofnode_valid(node);
  149. }
  150. /**
  151. * ofnode_equal() - check if two references are equal
  152. *
  153. * @return true if equal, else false
  154. */
  155. static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
  156. {
  157. /* We only need to compare the contents */
  158. return ref1.of_offset == ref2.of_offset;
  159. }
  160. /**
  161. * ofnode_null() - Obtain a null ofnode
  162. *
  163. * This returns an ofnode which points to no node. It works both with the flat
  164. * tree and livetree.
  165. */
  166. static inline ofnode ofnode_null(void)
  167. {
  168. ofnode node;
  169. if (of_live_active())
  170. node.np = NULL;
  171. else
  172. node.of_offset = -1;
  173. return node;
  174. }
  175. /**
  176. * ofnode_read_u32() - Read a 32-bit integer from a property
  177. *
  178. * @ref: valid node reference to read property from
  179. * @propname: name of the property to read from
  180. * @outp: place to put value (if found)
  181. * @return 0 if OK, -ve on error
  182. */
  183. int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
  184. /**
  185. * ofnode_read_s32() - Read a 32-bit integer from a property
  186. *
  187. * @ref: valid node reference to read property from
  188. * @propname: name of the property to read from
  189. * @outp: place to put value (if found)
  190. * @return 0 if OK, -ve on error
  191. */
  192. static inline int ofnode_read_s32(ofnode node, const char *propname,
  193. s32 *out_value)
  194. {
  195. return ofnode_read_u32(node, propname, (u32 *)out_value);
  196. }
  197. /**
  198. * ofnode_read_u32_default() - Read a 32-bit integer from a property
  199. *
  200. * @ref: valid node reference to read property from
  201. * @propname: name of the property to read from
  202. * @def: default value to return if the property has no value
  203. * @return property value, or @def if not found
  204. */
  205. int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
  206. /**
  207. * ofnode_read_s32_default() - Read a 32-bit integer from a property
  208. *
  209. * @ref: valid node reference to read property from
  210. * @propname: name of the property to read from
  211. * @def: default value to return if the property has no value
  212. * @return property value, or @def if not found
  213. */
  214. int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
  215. /**
  216. * ofnode_read_string() - Read a string from a property
  217. *
  218. * @ref: valid node reference to read property from
  219. * @propname: name of the property to read
  220. * @return string from property value, or NULL if there is no such property
  221. */
  222. const char *ofnode_read_string(ofnode node, const char *propname);
  223. /**
  224. * ofnode_read_u32_array() - Find and read an array of 32 bit integers
  225. *
  226. * @node: valid node reference to read property from
  227. * @propname: name of the property to read
  228. * @out_values: pointer to return value, modified only if return value is 0
  229. * @sz: number of array elements to read
  230. *
  231. * Search for a property in a device node and read 32-bit value(s) from
  232. * it. Returns 0 on success, -EINVAL if the property does not exist,
  233. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  234. * property data isn't large enough.
  235. *
  236. * The out_values is modified only if a valid u32 value can be decoded.
  237. */
  238. int ofnode_read_u32_array(ofnode node, const char *propname,
  239. u32 *out_values, size_t sz);
  240. /**
  241. * ofnode_read_bool() - read a boolean value from a property
  242. *
  243. * @node: valid node reference to read property from
  244. * @propname: name of property to read
  245. * @return true if property is present (meaning true), false if not present
  246. */
  247. bool ofnode_read_bool(ofnode node, const char *propname);
  248. /**
  249. * ofnode_find_subnode() - find a named subnode of a parent node
  250. *
  251. * @node: valid reference to parent node
  252. * @subnode_name: name of subnode to find
  253. * @return reference to subnode (which can be invalid if there is no such
  254. * subnode)
  255. */
  256. ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
  257. /**
  258. * ofnode_first_subnode() - find the first subnode of a parent node
  259. *
  260. * @node: valid reference to a valid parent node
  261. * @return reference to the first subnode (which can be invalid if the parent
  262. * node has no subnodes)
  263. */
  264. ofnode ofnode_first_subnode(ofnode node);
  265. /**
  266. * ofnode_next_subnode() - find the next sibling of a subnode
  267. *
  268. * @node: valid reference to previous node (sibling)
  269. * @return reference to the next subnode (which can be invalid if the node
  270. * has no more siblings)
  271. */
  272. ofnode ofnode_next_subnode(ofnode node);
  273. /**
  274. * ofnode_get_name() - get the name of a node
  275. *
  276. * @node: valid node to look up
  277. * @return name or node
  278. */
  279. const char *ofnode_get_name(ofnode node);
  280. /**
  281. * ofnode_read_size() - read the size of a property
  282. *
  283. * @node: node to check
  284. * @propname: property to check
  285. * @return size of property if present, or -EINVAL if not
  286. */
  287. int ofnode_read_size(ofnode node, const char *propname);
  288. /**
  289. * ofnode_get_addr_index() - get an address from a node
  290. *
  291. * This reads the register address from a node
  292. *
  293. * @node: node to read from
  294. * @index: Index of address to read (0 for first)
  295. * @return address, or FDT_ADDR_T_NONE if not present or invalid
  296. */
  297. phys_addr_t ofnode_get_addr_index(ofnode node, int index);
  298. /**
  299. * ofnode_get_addr() - get an address from a node
  300. *
  301. * This reads the register address from a node
  302. *
  303. * @node: node to read from
  304. * @return address, or FDT_ADDR_T_NONE if not present or invalid
  305. */
  306. phys_addr_t ofnode_get_addr(ofnode node);
  307. /**
  308. * ofnode_stringlist_search() - find a string in a string list and return index
  309. *
  310. * Note that it is possible for this function to succeed on property values
  311. * that are not NUL-terminated. That's because the function will stop after
  312. * finding the first occurrence of @string. This can for example happen with
  313. * small-valued cell properties, such as #address-cells, when searching for
  314. * the empty string.
  315. *
  316. * @node: node to check
  317. * @propname: name of the property containing the string list
  318. * @string: string to look up in the string list
  319. *
  320. * @return:
  321. * the index of the string in the list of strings
  322. * -ENODATA if the property is not found
  323. * -EINVAL on some other error
  324. */
  325. int ofnode_stringlist_search(ofnode node, const char *propname,
  326. const char *string);
  327. /**
  328. * ofnode_read_string_index() - obtain an indexed string from a string list
  329. *
  330. * Note that this will successfully extract strings from properties with
  331. * non-NUL-terminated values. For example on small-valued cell properties
  332. * this function will return the empty string.
  333. *
  334. * If non-NULL, the length of the string (on success) or a negative error-code
  335. * (on failure) will be stored in the integer pointer to by lenp.
  336. *
  337. * @node: node to check
  338. * @propname: name of the property containing the string list
  339. * @index: index of the string to return
  340. * @lenp: return location for the string length or an error code on failure
  341. *
  342. * @return:
  343. * length of string, if found or -ve error value if not found
  344. */
  345. int ofnode_read_string_index(ofnode node, const char *propname, int index,
  346. const char **outp);
  347. /**
  348. * ofnode_read_string_count() - find the number of strings in a string list
  349. *
  350. * @node: node to check
  351. * @propname: name of the property containing the string list
  352. * @return:
  353. * number of strings in the list, or -ve error value if not found
  354. */
  355. int ofnode_read_string_count(ofnode node, const char *property);
  356. /**
  357. * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
  358. *
  359. * This function is useful to parse lists of phandles and their arguments.
  360. * Returns 0 on success and fills out_args, on error returns appropriate
  361. * errno value.
  362. *
  363. * Caller is responsible to call of_node_put() on the returned out_args->np
  364. * pointer.
  365. *
  366. * Example:
  367. *
  368. * phandle1: node1 {
  369. * #list-cells = <2>;
  370. * }
  371. *
  372. * phandle2: node2 {
  373. * #list-cells = <1>;
  374. * }
  375. *
  376. * node3 {
  377. * list = <&phandle1 1 2 &phandle2 3>;
  378. * }
  379. *
  380. * To get a device_node of the `node2' node you may call this:
  381. * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
  382. *
  383. * @node: device tree node containing a list
  384. * @list_name: property name that contains a list
  385. * @cells_name: property name that specifies phandles' arguments count
  386. * @cells_count: Cell count to use if @cells_name is NULL
  387. * @index: index of a phandle to parse out
  388. * @out_args: optional pointer to output arguments structure (will be filled)
  389. * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
  390. * @list_name does not exist, -EINVAL if a phandle was not found,
  391. * @cells_name could not be found, the arguments were truncated or there
  392. * were too many arguments.
  393. */
  394. int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
  395. const char *cells_name, int cell_count,
  396. int index,
  397. struct ofnode_phandle_args *out_args);
  398. /**
  399. * ofnode_count_phandle_with_args() - Count number of phandle in a list
  400. *
  401. * This function is useful to count phandles into a list.
  402. * Returns number of phandle on success, on error returns appropriate
  403. * errno value.
  404. *
  405. * @node: device tree node containing a list
  406. * @list_name: property name that contains a list
  407. * @cells_name: property name that specifies phandles' arguments count
  408. * @return number of phandle on success, -ENOENT if @list_name does not
  409. * exist, -EINVAL if a phandle was not found, @cells_name could not
  410. * be found.
  411. */
  412. int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
  413. const char *cells_name);
  414. /**
  415. * ofnode_path() - find a node by full path
  416. *
  417. * @path: Full path to node, e.g. "/bus/spi@1"
  418. * @return reference to the node found. Use ofnode_valid() to check if it exists
  419. */
  420. ofnode ofnode_path(const char *path);
  421. /**
  422. * ofnode_get_chosen_prop() - get the value of a chosen property
  423. *
  424. * This looks for a property within the /chosen node and returns its value
  425. *
  426. * @propname: Property name to look for
  427. */
  428. const char *ofnode_get_chosen_prop(const char *propname);
  429. /**
  430. * ofnode_get_chosen_node() - get the chosen node
  431. *
  432. * @return the chosen node if present, else ofnode_null()
  433. */
  434. ofnode ofnode_get_chosen_node(const char *name);
  435. struct display_timing;
  436. /**
  437. * ofnode_decode_display_timing() - decode display timings
  438. *
  439. * Decode display timings from the supplied 'display-timings' node.
  440. * See doc/device-tree-bindings/video/display-timing.txt for binding
  441. * information.
  442. *
  443. * @node 'display-timing' node containing the timing subnodes
  444. * @index Index number to read (0=first timing subnode)
  445. * @config Place to put timings
  446. * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
  447. */
  448. int ofnode_decode_display_timing(ofnode node, int index,
  449. struct display_timing *config);
  450. /**
  451. * ofnode_get_property()- - get a pointer to the value of a node property
  452. *
  453. * @node: node to read
  454. * @propname: property to read
  455. * @lenp: place to put length on success
  456. * @return pointer to property, or NULL if not found
  457. */
  458. const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
  459. /**
  460. * ofnode_is_available() - check if a node is marked available
  461. *
  462. * @node: node to check
  463. * @return true if node's 'status' property is "okay" (or is missing)
  464. */
  465. bool ofnode_is_available(ofnode node);
  466. /**
  467. * ofnode_get_addr_size() - get address and size from a property
  468. *
  469. * This does no address translation. It simply reads an property that contains
  470. * an address and a size value, one after the other.
  471. *
  472. * @node: node to read from
  473. * @propname: property to read
  474. * @sizep: place to put size value (on success)
  475. * @return address value, or FDT_ADDR_T_NONE on error
  476. */
  477. phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
  478. phys_size_t *sizep);
  479. /**
  480. * ofnode_read_u8_array_ptr() - find an 8-bit array
  481. *
  482. * Look up a property in a node and return a pointer to its contents as a
  483. * byte array of given length. The property must have at least enough data
  484. * for the array (count bytes). It may have more, but this will be ignored.
  485. * The data is not copied.
  486. *
  487. * @node node to examine
  488. * @propname name of property to find
  489. * @sz number of array elements
  490. * @return pointer to byte array if found, or NULL if the property is not
  491. * found or there is not enough data
  492. */
  493. const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
  494. size_t sz);
  495. /**
  496. * ofnode_read_pci_addr() - look up a PCI address
  497. *
  498. * Look at an address property in a node and return the PCI address which
  499. * corresponds to the given type in the form of fdt_pci_addr.
  500. * The property must hold one fdt_pci_addr with a lengh.
  501. *
  502. * @node node to examine
  503. * @type pci address type (FDT_PCI_SPACE_xxx)
  504. * @propname name of property to find
  505. * @addr returns pci address in the form of fdt_pci_addr
  506. * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
  507. * format of the property was invalid, -ENXIO if the requested
  508. * address type was not found
  509. */
  510. int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
  511. const char *propname, struct fdt_pci_addr *addr);
  512. /**
  513. * ofnode_read_addr_cells() - Get the number of address cells for a node
  514. *
  515. * This walks back up the tree to find the closest #address-cells property
  516. * which controls the given node.
  517. *
  518. * @node: Node to check
  519. * @return number of address cells this node uses
  520. */
  521. int ofnode_read_addr_cells(ofnode node);
  522. /**
  523. * ofnode_read_size_cells() - Get the number of size cells for a node
  524. *
  525. * This walks back up the tree to find the closest #size-cells property
  526. * which controls the given node.
  527. *
  528. * @node: Node to check
  529. * @return number of size cells this node uses
  530. */
  531. int ofnode_read_size_cells(ofnode node);
  532. /**
  533. * ofnode_read_simple_addr_cells() - Get the address cells property in a node
  534. *
  535. * This function matches fdt_address_cells().
  536. *
  537. * @np: Node pointer to check
  538. * @return value of #address-cells property in this node, or 2 if none
  539. */
  540. int ofnode_read_simple_addr_cells(ofnode node);
  541. /**
  542. * ofnode_read_simple_size_cells() - Get the size cells property in a node
  543. *
  544. * This function matches fdt_size_cells().
  545. *
  546. * @np: Node pointer to check
  547. * @return value of #size-cells property in this node, or 2 if none
  548. */
  549. int ofnode_read_simple_size_cells(ofnode node);
  550. /**
  551. * ofnode_pre_reloc() - check if a node should be bound before relocation
  552. *
  553. * Device tree nodes can be marked as needing-to-be-bound in the loader stages
  554. * via special device tree properties.
  555. *
  556. * Before relocation this function can be used to check if nodes are required
  557. * in either SPL or TPL stages.
  558. *
  559. * After relocation and jumping into the real U-Boot binary it is possible to
  560. * determine if a node was bound in one of SPL/TPL stages.
  561. *
  562. * There are 3 settings currently in use
  563. * -
  564. * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
  565. * Existing platforms only use it to indicate nodes needed in
  566. * SPL. Should probably be replaced by u-boot,dm-spl for
  567. * new platforms.
  568. *
  569. * @node: node to check
  570. * @eturns true if node is needed in SPL/TL, false otherwise
  571. */
  572. bool ofnode_pre_reloc(ofnode node);
  573. int ofnode_read_resource(ofnode node, uint index, struct resource *res);
  574. int ofnode_read_resource_byname(ofnode node, const char *name,
  575. struct resource *res);
  576. /**
  577. * ofnode_for_each_subnode() - iterate over all subnodes of a parent
  578. *
  579. * @node: child node (ofnode, lvalue)
  580. * @parent: parent node (ofnode)
  581. *
  582. * This is a wrapper around a for loop and is used like so:
  583. *
  584. * ofnode node;
  585. *
  586. * ofnode_for_each_subnode(node, parent) {
  587. * Use node
  588. * ...
  589. * }
  590. *
  591. * Note that this is implemented as a macro and @node is used as
  592. * iterator in the loop. The parent variable can be a constant or even a
  593. * literal.
  594. */
  595. #define ofnode_for_each_subnode(node, parent) \
  596. for (node = ofnode_first_subnode(parent); \
  597. ofnode_valid(node); \
  598. node = ofnode_next_subnode(node))
  599. #endif