ofnode.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. * @of_offset: 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_parent() - get the ofnode's parent (enclosing ofnode)
  275. *
  276. * @node: valid node to look up
  277. * @return ofnode reference of the parent node
  278. */
  279. ofnode ofnode_get_parent(ofnode node);
  280. /**
  281. * ofnode_get_name() - get the name of a node
  282. *
  283. * @node: valid node to look up
  284. * @return name or node
  285. */
  286. const char *ofnode_get_name(ofnode node);
  287. /**
  288. * ofnode_read_size() - read the size of a property
  289. *
  290. * @node: node to check
  291. * @propname: property to check
  292. * @return size of property if present, or -EINVAL if not
  293. */
  294. int ofnode_read_size(ofnode node, const char *propname);
  295. /**
  296. * ofnode_get_addr_index() - get an address from a node
  297. *
  298. * This reads the register address from a node
  299. *
  300. * @node: node to read from
  301. * @index: Index of address to read (0 for first)
  302. * @return address, or FDT_ADDR_T_NONE if not present or invalid
  303. */
  304. phys_addr_t ofnode_get_addr_index(ofnode node, int index);
  305. /**
  306. * ofnode_get_addr() - get an address from a node
  307. *
  308. * This reads the register address from a node
  309. *
  310. * @node: node to read from
  311. * @return address, or FDT_ADDR_T_NONE if not present or invalid
  312. */
  313. phys_addr_t ofnode_get_addr(ofnode node);
  314. /**
  315. * ofnode_stringlist_search() - find a string in a string list and return index
  316. *
  317. * Note that it is possible for this function to succeed on property values
  318. * that are not NUL-terminated. That's because the function will stop after
  319. * finding the first occurrence of @string. This can for example happen with
  320. * small-valued cell properties, such as #address-cells, when searching for
  321. * the empty string.
  322. *
  323. * @node: node to check
  324. * @propname: name of the property containing the string list
  325. * @string: string to look up in the string list
  326. *
  327. * @return:
  328. * the index of the string in the list of strings
  329. * -ENODATA if the property is not found
  330. * -EINVAL on some other error
  331. */
  332. int ofnode_stringlist_search(ofnode node, const char *propname,
  333. const char *string);
  334. /**
  335. * ofnode_read_string_index() - obtain an indexed string from a string list
  336. *
  337. * Note that this will successfully extract strings from properties with
  338. * non-NUL-terminated values. For example on small-valued cell properties
  339. * this function will return the empty string.
  340. *
  341. * If non-NULL, the length of the string (on success) or a negative error-code
  342. * (on failure) will be stored in the integer pointer to by lenp.
  343. *
  344. * @node: node to check
  345. * @propname: name of the property containing the string list
  346. * @index: index of the string to return
  347. * @lenp: return location for the string length or an error code on failure
  348. *
  349. * @return:
  350. * length of string, if found or -ve error value if not found
  351. */
  352. int ofnode_read_string_index(ofnode node, const char *propname, int index,
  353. const char **outp);
  354. /**
  355. * ofnode_read_string_count() - find the number of strings in a string list
  356. *
  357. * @node: node to check
  358. * @propname: name of the property containing the string list
  359. * @return:
  360. * number of strings in the list, or -ve error value if not found
  361. */
  362. int ofnode_read_string_count(ofnode node, const char *property);
  363. /**
  364. * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
  365. *
  366. * This function is useful to parse lists of phandles and their arguments.
  367. * Returns 0 on success and fills out_args, on error returns appropriate
  368. * errno value.
  369. *
  370. * Caller is responsible to call of_node_put() on the returned out_args->np
  371. * pointer.
  372. *
  373. * Example:
  374. *
  375. * phandle1: node1 {
  376. * #list-cells = <2>;
  377. * }
  378. *
  379. * phandle2: node2 {
  380. * #list-cells = <1>;
  381. * }
  382. *
  383. * node3 {
  384. * list = <&phandle1 1 2 &phandle2 3>;
  385. * }
  386. *
  387. * To get a device_node of the `node2' node you may call this:
  388. * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
  389. *
  390. * @node: device tree node containing a list
  391. * @list_name: property name that contains a list
  392. * @cells_name: property name that specifies phandles' arguments count
  393. * @cells_count: Cell count to use if @cells_name is NULL
  394. * @index: index of a phandle to parse out
  395. * @out_args: optional pointer to output arguments structure (will be filled)
  396. * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
  397. * @list_name does not exist, -EINVAL if a phandle was not found,
  398. * @cells_name could not be found, the arguments were truncated or there
  399. * were too many arguments.
  400. */
  401. int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
  402. const char *cells_name, int cell_count,
  403. int index,
  404. struct ofnode_phandle_args *out_args);
  405. /**
  406. * ofnode_count_phandle_with_args() - Count number of phandle in a list
  407. *
  408. * This function is useful to count phandles into a list.
  409. * Returns number of phandle on success, on error returns appropriate
  410. * errno value.
  411. *
  412. * @node: device tree node containing a list
  413. * @list_name: property name that contains a list
  414. * @cells_name: property name that specifies phandles' arguments count
  415. * @return number of phandle on success, -ENOENT if @list_name does not
  416. * exist, -EINVAL if a phandle was not found, @cells_name could not
  417. * be found.
  418. */
  419. int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
  420. const char *cells_name);
  421. /**
  422. * ofnode_path() - find a node by full path
  423. *
  424. * @path: Full path to node, e.g. "/bus/spi@1"
  425. * @return reference to the node found. Use ofnode_valid() to check if it exists
  426. */
  427. ofnode ofnode_path(const char *path);
  428. /**
  429. * ofnode_get_chosen_prop() - get the value of a chosen property
  430. *
  431. * This looks for a property within the /chosen node and returns its value
  432. *
  433. * @propname: Property name to look for
  434. */
  435. const char *ofnode_get_chosen_prop(const char *propname);
  436. /**
  437. * ofnode_get_chosen_node() - get the chosen node
  438. *
  439. * @return the chosen node if present, else ofnode_null()
  440. */
  441. ofnode ofnode_get_chosen_node(const char *name);
  442. struct display_timing;
  443. /**
  444. * ofnode_decode_display_timing() - decode display timings
  445. *
  446. * Decode display timings from the supplied 'display-timings' node.
  447. * See doc/device-tree-bindings/video/display-timing.txt for binding
  448. * information.
  449. *
  450. * @node 'display-timing' node containing the timing subnodes
  451. * @index Index number to read (0=first timing subnode)
  452. * @config Place to put timings
  453. * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
  454. */
  455. int ofnode_decode_display_timing(ofnode node, int index,
  456. struct display_timing *config);
  457. /**
  458. * ofnode_get_property()- - get a pointer to the value of a node property
  459. *
  460. * @node: node to read
  461. * @propname: property to read
  462. * @lenp: place to put length on success
  463. * @return pointer to property, or NULL if not found
  464. */
  465. const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
  466. /**
  467. * ofnode_is_available() - check if a node is marked available
  468. *
  469. * @node: node to check
  470. * @return true if node's 'status' property is "okay" (or is missing)
  471. */
  472. bool ofnode_is_available(ofnode node);
  473. /**
  474. * ofnode_get_addr_size() - get address and size from a property
  475. *
  476. * This does no address translation. It simply reads an property that contains
  477. * an address and a size value, one after the other.
  478. *
  479. * @node: node to read from
  480. * @propname: property to read
  481. * @sizep: place to put size value (on success)
  482. * @return address value, or FDT_ADDR_T_NONE on error
  483. */
  484. phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
  485. phys_size_t *sizep);
  486. /**
  487. * ofnode_read_u8_array_ptr() - find an 8-bit array
  488. *
  489. * Look up a property in a node and return a pointer to its contents as a
  490. * byte array of given length. The property must have at least enough data
  491. * for the array (count bytes). It may have more, but this will be ignored.
  492. * The data is not copied.
  493. *
  494. * @node node to examine
  495. * @propname name of property to find
  496. * @sz number of array elements
  497. * @return pointer to byte array if found, or NULL if the property is not
  498. * found or there is not enough data
  499. */
  500. const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
  501. size_t sz);
  502. /**
  503. * ofnode_read_pci_addr() - look up a PCI address
  504. *
  505. * Look at an address property in a node and return the PCI address which
  506. * corresponds to the given type in the form of fdt_pci_addr.
  507. * The property must hold one fdt_pci_addr with a lengh.
  508. *
  509. * @node node to examine
  510. * @type pci address type (FDT_PCI_SPACE_xxx)
  511. * @propname name of property to find
  512. * @addr returns pci address in the form of fdt_pci_addr
  513. * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
  514. * format of the property was invalid, -ENXIO if the requested
  515. * address type was not found
  516. */
  517. int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
  518. const char *propname, struct fdt_pci_addr *addr);
  519. /**
  520. * ofnode_read_addr_cells() - Get the number of address cells for a node
  521. *
  522. * This walks back up the tree to find the closest #address-cells property
  523. * which controls the given node.
  524. *
  525. * @node: Node to check
  526. * @return number of address cells this node uses
  527. */
  528. int ofnode_read_addr_cells(ofnode node);
  529. /**
  530. * ofnode_read_size_cells() - Get the number of size cells for a node
  531. *
  532. * This walks back up the tree to find the closest #size-cells property
  533. * which controls the given node.
  534. *
  535. * @node: Node to check
  536. * @return number of size cells this node uses
  537. */
  538. int ofnode_read_size_cells(ofnode node);
  539. /**
  540. * ofnode_read_simple_addr_cells() - Get the address cells property in a node
  541. *
  542. * This function matches fdt_address_cells().
  543. *
  544. * @np: Node pointer to check
  545. * @return value of #address-cells property in this node, or 2 if none
  546. */
  547. int ofnode_read_simple_addr_cells(ofnode node);
  548. /**
  549. * ofnode_read_simple_size_cells() - Get the size cells property in a node
  550. *
  551. * This function matches fdt_size_cells().
  552. *
  553. * @np: Node pointer to check
  554. * @return value of #size-cells property in this node, or 2 if none
  555. */
  556. int ofnode_read_simple_size_cells(ofnode node);
  557. /**
  558. * ofnode_pre_reloc() - check if a node should be bound before relocation
  559. *
  560. * Device tree nodes can be marked as needing-to-be-bound in the loader stages
  561. * via special device tree properties.
  562. *
  563. * Before relocation this function can be used to check if nodes are required
  564. * in either SPL or TPL stages.
  565. *
  566. * After relocation and jumping into the real U-Boot binary it is possible to
  567. * determine if a node was bound in one of SPL/TPL stages.
  568. *
  569. * There are 3 settings currently in use
  570. * -
  571. * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
  572. * Existing platforms only use it to indicate nodes needed in
  573. * SPL. Should probably be replaced by u-boot,dm-spl for
  574. * new platforms.
  575. *
  576. * @node: node to check
  577. * @eturns true if node is needed in SPL/TL, false otherwise
  578. */
  579. bool ofnode_pre_reloc(ofnode node);
  580. int ofnode_read_resource(ofnode node, uint index, struct resource *res);
  581. int ofnode_read_resource_byname(ofnode node, const char *name,
  582. struct resource *res);
  583. /**
  584. * ofnode_for_each_subnode() - iterate over all subnodes of a parent
  585. *
  586. * @node: child node (ofnode, lvalue)
  587. * @parent: parent node (ofnode)
  588. *
  589. * This is a wrapper around a for loop and is used like so:
  590. *
  591. * ofnode node;
  592. *
  593. * ofnode_for_each_subnode(node, parent) {
  594. * Use node
  595. * ...
  596. * }
  597. *
  598. * Note that this is implemented as a macro and @node is used as
  599. * iterator in the loop. The parent variable can be a constant or even a
  600. * literal.
  601. */
  602. #define ofnode_for_each_subnode(node, parent) \
  603. for (node = ofnode_first_subnode(parent); \
  604. ofnode_valid(node); \
  605. node = ofnode_next_subnode(node))
  606. /**
  607. * ofnode_translate_address() - Tranlate a device-tree address
  608. *
  609. * Translate an address from the device-tree into a CPU physical address. This
  610. * function walks up the tree and applies the various bus mappings along the
  611. * way.
  612. *
  613. * @ofnode: Device tree node giving the context in which to translate the
  614. * address
  615. * @in_addr: pointer to the address to translate
  616. * @return the translated address; OF_BAD_ADDR on error
  617. */
  618. u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
  619. #endif