dtb_node.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _DTB_NODE_H__
  7. #define _DTB_NODE_H__
  8. #include "libfdt_env.h"
  9. #include "dtb_head.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdbool.h>
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <sys/types.h>
  16. //#define RT_DTB_DEBUG
  17. #ifdef RT_DTB_DEBUG
  18. #define debug(fmt, args...) printf(fmt, ##args)
  19. #else
  20. #define debug(fmt, args...)
  21. #endif
  22. #define ERR_PTR(err) ((void *)((long)(err)))
  23. #define PTR_ERR(ptr) ((long)(ptr))
  24. #define IS_ERR(ptr) ((unsigned long)(ptr) > (unsigned long)(-1000))
  25. #define DEV_ROOT_NODE_ADDR_CELLS_DEFAULT 2
  26. #define DEV_ROOT_NODE_SIZE_CELLS_DEFAULT 1
  27. /* will be optimized to only u64 or u32 by gcc */
  28. #define IN_64BITS_MODE (sizeof(void *) == 8)
  29. #define FDT_ROOT_ADDR_CELLS_DEFAULT 1
  30. #define FDT_ROOT_SIZE_CELLS_DEFAULT 1
  31. #define FDT_DTB_ALL_NODES_PATH_SIZE (32 * 1024)
  32. #define FDT_DTB_PAD_SIZE 1024
  33. #define FDT_RET_NO_MEMORY 2
  34. #define FDT_RET_NO_LOADED 1
  35. #define FDT_RET_GET_OK 0
  36. #define FDT_RET_GET_EMPTY (-1)
  37. typedef uint32_t phandle;
  38. struct dtb_memreserve
  39. {
  40. uintptr_t address;
  41. size_t size;
  42. };
  43. struct dtb_header
  44. {
  45. char root, zero; /* "/" */
  46. struct dtb_memreserve *memreserve;
  47. size_t memreserve_sz;
  48. };
  49. struct dtb_property
  50. {
  51. const char *name;
  52. int size;
  53. void *value;
  54. struct dtb_property *next;
  55. };
  56. struct dtb_node
  57. {
  58. union
  59. {
  60. const char *name;
  61. const struct dtb_header *header;
  62. };
  63. const char *path;
  64. phandle handle;
  65. struct dtb_property *properties;
  66. struct dtb_node *parent;
  67. struct dtb_node *child;
  68. struct dtb_node *sibling;
  69. };
  70. #define FDT_MAX_PHANDLE_ARGS 16
  71. /**
  72. * struct dtb_node_phandle_args - structure to hold phandle and arguments
  73. *
  74. * This is used when decoding a phandle in a device tree property. Typically
  75. * these look like this:
  76. *
  77. * wibble {
  78. * phandle = <5>;
  79. * };
  80. *
  81. * ...
  82. * some-prop = <&wibble 1 2 3>
  83. *
  84. * Here &node is the phandle of the node 'wibble', i.e. 5. There are three
  85. * arguments: 1, 2, 3.
  86. *
  87. * So when decoding the phandle in some-prop, np will point to wibble,
  88. * args_count will be 3 and the three arguments will be in args.
  89. *
  90. * @np: Node that the phandle refers to
  91. * @args_count: Number of arguments
  92. * @args: Argument values
  93. */
  94. struct fdt_phandle_args
  95. {
  96. struct dtb_node *np;
  97. int args_count;
  98. uint32_t args[FDT_MAX_PHANDLE_ARGS];
  99. };
  100. /*
  101. * A single signal can be specified via a range of minimal and maximal values
  102. * with a typical value, that lies somewhere inbetween.
  103. */
  104. struct timing_entry
  105. {
  106. uint32_t min;
  107. uint32_t typ;
  108. uint32_t max;
  109. };
  110. void *dtb_node_load_from_fs(char *dtb_filename);
  111. void *dtb_node_load_from_memory(void *dtb_ptr, bool is_clone);
  112. size_t dtb_node_set_linux_cmdline(void *fdt, char *cmdline);
  113. size_t dtb_node_set_linux_initrd(void *fdt, uint64_t initrd_addr, size_t initrd_size);
  114. size_t dtb_node_set_dtb_property(void *fdt, char *pathname, char *property_name, uint32_t *cells, size_t cells_size);
  115. size_t dtb_node_add_dtb_memreserve(void *fdt, uint64_t address, uint64_t size);
  116. size_t dtb_node_del_dtb_memreserve(void *fdt, uint64_t address);
  117. int dtb_node_get_exec_status();
  118. struct dtb_node *dtb_node_get_dtb_list(void *fdt);
  119. void dtb_node_free_dtb_list(struct dtb_node *dtb_node_head);
  120. void dtb_node_get_dts_dump(struct dtb_node *dtb_node_head);
  121. void dtb_node_get_enum_dtb_node(struct dtb_node *dtb_node_head, void(callback(struct dtb_node *dtb_node)));
  122. struct dtb_node *dtb_node_get_dtb_node_by_name_DFS(struct dtb_node *dtb_node, const char *nodename);
  123. struct dtb_node *dtb_node_get_dtb_node_by_name_BFS(struct dtb_node *dtb_node, const char *nodename);
  124. struct dtb_node *dtb_node_get_dtb_node_by_path(struct dtb_node *dtb_node, const char *pathname);
  125. struct dtb_node *dtb_node_get_dtb_node_by_phandle_DFS(struct dtb_node *dtb_node, phandle handle);
  126. struct dtb_node *dtb_node_get_dtb_node_by_phandle_BFS(struct dtb_node *dtb_node, phandle handle);
  127. void dtb_node_get_dtb_node_cells(struct dtb_node *dtb_node, int *addr_cells, int *size_cells);
  128. struct dtb_memreserve *dtb_node_get_dtb_memreserve(struct dtb_node *dtb_node, int *memreserve_size);
  129. uint8_t dtb_node_get_dtb_byte_value(void *value);
  130. char *dtb_node_get_dtb_string_list_value(void *value, int size, int index);
  131. char *dtb_node_get_dtb_string_list_value_next(void *value, void *end);
  132. uint32_t dtb_node_get_dtb_cell_value(void *value);
  133. bool dtb_node_get_dtb_node_status(const struct dtb_node *dtb_node);
  134. bool dtb_node_get_dtb_node_compatible_match(const struct dtb_node *dtb_node, const char *compatibles);
  135. /*dtb_node_access.c */
  136. int dtb_node_read_u32(const struct dtb_node *dn, const char *propname, uint32_t *outp);
  137. uint32_t dtb_node_read_u32_default(const struct dtb_node *node, const char *propname, uint32_t def);
  138. int dtb_node_read_u32_index(const struct dtb_node *node, const char *propname, int index,
  139. uint32_t *outp);
  140. uint32_t dtb_node_read_u32_index_default(const struct dtb_node *node, const char *propname, int index,
  141. uint32_t def);
  142. int dtb_node_read_u32_array(const struct dtb_node *dn, const char *propname,
  143. uint32_t *out_values, size_t sz);
  144. int dtb_node_read_u32_index(const struct dtb_node *dn, const char *propname,
  145. int index, uint32_t *outp);
  146. int dtb_node_read_s32_default(const struct dtb_node *node, const char *propname, int32_t def);
  147. int dtb_node_read_u64(const struct dtb_node *dn, const char *propname, uint64_t *outp);
  148. uint64_t dtb_node_read_u64_default(const struct dtb_node *node, const char *propname, uint64_t def);
  149. int dtb_node_n_addr_cells(const struct dtb_node *dn);
  150. int dtb_node_n_size_cells(const struct dtb_node *dn);
  151. int dtb_node_simple_addr_cells(const struct dtb_node *np);
  152. int dtb_node_simple_size_cells(const struct dtb_node *np);
  153. struct dtb_node *dtb_node_find_all_nodes(const struct dtb_node *prev);
  154. struct dtb_node *dtb_node_find_node_by_phandle(phandle handle);
  155. struct dtb_node *dtb_node_find_compatible_node(struct dtb_node *from, const char *compatible);
  156. void *dtb_node_get_dtb_node_property_value(const struct dtb_node *dtb_node, const char *property_name, int *property_size);
  157. struct dtb_property *dtb_node_get_dtb_node_property(const struct dtb_node *dtb_node, const char *property_name, int *property_size);
  158. const struct dtb_node *dtb_node_find_node_by_prop_value(struct dtb_node *from, const char *propname, const void *propval, int proplen);
  159. struct dtb_node *dtb_node_find_node_opts_by_path(const char *path,
  160. const char **opts);
  161. static inline struct dtb_node *dtb_node_find_node_by_path(const char *path)
  162. {
  163. return dtb_node_find_node_opts_by_path(path, NULL);
  164. }
  165. bool dtb_node_device_is_available(const struct dtb_node *device);
  166. struct dtb_node *dtb_node_get_parent(const struct dtb_node *node);
  167. int dtb_node_property_match_string(const struct dtb_node *dn, const char *propname,
  168. const char *string);
  169. int dtb_node_property_read_string_helper(const struct dtb_node *dn,
  170. const char *propname, const char **out_strs,
  171. size_t sz, int skip);
  172. /**
  173. * of_property_read_string_index() - Find and read a string from a multiple
  174. * strings property.
  175. * @np: device node from which the property value is to be read.
  176. * @propname: name of the property to be searched.
  177. * @index: index of the string in the list of strings
  178. * @out_string: pointer to null terminated return string, modified only if
  179. * return value is 0.
  180. *
  181. * Search for a property in a device tree node and retrieve a null
  182. * terminated string value (pointer to data, not a copy) in the list of strings
  183. * contained in that property.
  184. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  185. * property does not have a value, and -EILSEQ if the string is not
  186. * null-terminated within the length of the property data.
  187. *
  188. * The out_string pointer is modified only if a valid string can be decoded.
  189. */
  190. static inline int dtb_node_property_read_string_index(const struct dtb_node *dn,
  191. const char *propname,
  192. int index, const char **output)
  193. {
  194. int rc = dtb_node_property_read_string_helper(dn, propname, output, 1, index);
  195. return rc < 0 ? rc : 0;
  196. }
  197. /**
  198. * of_property_count_strings() - Find and return the number of strings from a
  199. * multiple strings property.
  200. * @np: device node from which the property value is to be read.
  201. * @propname: name of the property to be searched.
  202. *
  203. * Search for a property in a device tree node and retrieve the number of null
  204. * terminated string contain in it. Returns the number of strings on
  205. * success, -EINVAL if the property does not exist, -ENODATA if property
  206. * does not have a value, and -EILSEQ if the string is not null-terminated
  207. * within the length of the property data.
  208. */
  209. static inline int dtb_node_property_count_strings(const struct dtb_node *dn,
  210. const char *propname)
  211. {
  212. return dtb_node_property_read_string_helper(dn, propname, NULL, 0, 0);
  213. }
  214. struct dtb_node *dtb_node_parse_phandle(const struct dtb_node *dn,
  215. const char *phandle_name, int index);
  216. int dtb_node_parse_phandle_with_args(const struct dtb_node *dn,
  217. const char *list_name, const char *cells_name,
  218. int index, struct fdt_phandle_args *out_args);
  219. int dtb_node_count_phandle_with_args(const struct dtb_node *dn,
  220. const char *list_name, const char *cells_name);
  221. /* dtb_node_addr.c */
  222. const uint32_t *dtb_node_get_address(const struct dtb_node *dev, int index,
  223. uint64_t *size, unsigned int *flags);
  224. #define dtb_node_string_list(string, ...) ((char *[]){string, ##__VA_ARGS__, NULL})
  225. #define for_each_property_string(node_ptr, property_name, str, size) \
  226. for (str = dtb_node_get_dtb_node_property_value(node_ptr, property_name, &size), \
  227. size += (typeof(size))(size_t)str; \
  228. str && *str; \
  229. str = dtb_node_get_dtb_string_list_value_next((void *)str, (void *)(size_t)size))
  230. #define for_each_property_cell(node_ptr, property_name, value, list, size) \
  231. for (list = dtb_node_get_dtb_node_property_value(node_ptr, property_name, &size), \
  232. value = dtb_node_get_dtb_cell_value(list), \
  233. size /= sizeof(uint32_t); \
  234. size > 0; \
  235. value = dtb_node_get_dtb_cell_value(++list), --size)
  236. #define for_each_property_byte(node_ptr, property_name, value, list, size) \
  237. for (list = dtb_node_get_dtb_node_property_value(node_ptr, property_name, &size), \
  238. value = dtb_node_get_dtb_byte_value(list); \
  239. size > 0; \
  240. value = dtb_node_get_dtb_byte_value(++list), --size)
  241. #define for_each_node_child(node_ptr) \
  242. for (node_ptr = (node_ptr ? node_ptr->child : NULL); \
  243. node_ptr != NULL; \
  244. node_ptr = node_ptr->sibling)
  245. #define for_each_node_sibling(node_ptr) \
  246. for (node_ptr = (node_ptr ? node_ptr->sibling : NULL); \
  247. node_ptr != NULL; \
  248. node_ptr = node_ptr->sibling)
  249. #define for_each_of_allnodes_from(from, dn) \
  250. for (dn = dtb_node_find_all_nodes(from); dn; dn = dtb_node_find_all_nodes(dn))
  251. #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
  252. #define dtb_node_get(x) (x)
  253. static inline void dtb_node_put(const struct dtb_node *np)
  254. {
  255. }
  256. /* Helper to read a big number; size is in cells (not bytes) */
  257. static inline uint64_t dtb_node_read_number(const uint32_t *cell, int size)
  258. {
  259. uint64_t r = 0;
  260. while (size--)
  261. r = (r << 32) | fdt32_to_cpu(*(cell++));
  262. return r;
  263. }
  264. /**
  265. * ofnode_valid() - check if an ofnode is valid
  266. *
  267. * @return true if the reference contains a valid ofnode, false if it is NULL
  268. */
  269. static inline bool dtb_node_valid(const struct dtb_node *node)
  270. {
  271. if (dtb_node_active())
  272. return node != NULL;
  273. return false;
  274. }
  275. /*dtb_base.c */
  276. bool dtb_node_read_bool(const struct dtb_node *node, const char *propname);
  277. const void *dtb_node_read_prop(const struct dtb_node *node, const char *propname, int *sizep);
  278. const char *dtb_node_read_string(const struct dtb_node *node, const char *propname);
  279. const struct dtb_node *dtb_node_find_subnode(const struct dtb_node *node, const char *subnode_name);
  280. int dtb_node_read_u32_array(const struct dtb_node *node, const char *propname,
  281. uint32_t *out_values, size_t sz);
  282. struct dtb_node *dtb_node_first_subnode(const struct dtb_node *node);
  283. struct dtb_node *dtb_node_next_subnode(const struct dtb_node *node);
  284. struct dtb_node *dtb_node_get_parent(const struct dtb_node *node);
  285. const char *dtb_node_get_name(const struct dtb_node *node);
  286. struct dtb_node *dtb_node_get_by_phandle(uint phandle);
  287. int dtb_node_read_size(const struct dtb_node *node, const char *propname);
  288. size_t dtb_node_get_addr_index(const struct dtb_node *node, int index);
  289. size_t dtb_node_get_addr(const struct dtb_node *node);
  290. int dtb_node_stringlist_search(const struct dtb_node *node, const char *property,
  291. const char *string);
  292. int dtb_node_read_string_index(const struct dtb_node *node, const char *property, int index,
  293. const char **outp);
  294. int dtb_node_read_string_count(const struct dtb_node *node, const char *property);
  295. struct dtb_node *dtb_node_path(const char *path);
  296. const char *dtb_node_get_chosen_prop(const char *name);
  297. struct dtb_node *dtb_node_get_chosen_node(const char *name);
  298. const void *dtb_node_get_property(const struct dtb_node *node, const char *propname, int *lenp);
  299. bool dtb_node_is_available(const struct dtb_node *node);
  300. size_t dtb_node_get_addr_size(const struct dtb_node *node, const char *property,
  301. size_t *sizep);
  302. const uint8_t *dtb_node_read_u8_array_ptr(const struct dtb_node *node, const char *propname, size_t sz);
  303. int dtb_node_find_all_active_compatible_node(const struct dtb_node *from, const char *compatible, struct dtb_node **node_table, int max_num, int *node_num);
  304. int dtb_node_write_prop(const struct dtb_node *node, const char *propname, int len,
  305. const void *value);
  306. int dtb_node_write_string(const struct dtb_node *node, const char *propname, const char *value);
  307. int dtb_node_set_enabled(const struct dtb_node *node, bool value);
  308. int dtb_node_irq_get(struct dtb_node *dev, int index);
  309. int dtb_node_irq_get_byname(struct dtb_node *dev, const char *name);
  310. int dtb_node_irq_count(struct dtb_node *dev);
  311. /**
  312. * dtb_node_for_each_subnode() - iterate over all subnodes of a parent
  313. *
  314. * @node: child node (ofnode, lvalue)
  315. * @parent: parent node (ofnode)
  316. *
  317. * This is a wrapper around a for loop and is used like so:
  318. *
  319. * ofnode node;
  320. *
  321. * dtb_node_for_each_subnode(node, parent) {
  322. * Use node
  323. * ...
  324. * }
  325. *
  326. * Note that this is implemented as a macro and @node is used as
  327. * iterator in the loop. The parent variable can be a constant or even a
  328. * literal.
  329. */
  330. #define dtb_node_for_each_subnode(node, parent) \
  331. for (node = dtb_node_first_subnode(parent); \
  332. dtb_node_valid(node); \
  333. node = dtb_node_next_subnode(node))
  334. #endif /* RT_FDT_H__ */