dtb_node.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Copyright (c) 2006-2023, 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 <rtthread.h>
  10. #include <stdint.h>
  11. //#define RT_DTB_DEBUG
  12. #ifdef RT_DTB_DEBUG
  13. #define debug(fmt, args...) rt_kprintf(fmt, ##args)
  14. #else
  15. #define debug(fmt, args...)
  16. #endif
  17. #define ERR_PTR(err) ((void *)((long)(err)))
  18. #define PTR_ERR(ptr) ((long)(ptr))
  19. #define IS_ERR(ptr) ((unsigned long)(ptr) > (unsigned long)(-1000))
  20. #define DEV_ROOT_NODE_ADDR_CELLS_DEFAULT 2
  21. #define DEV_ROOT_NODE_SIZE_CELLS_DEFAULT 1
  22. /* will be optimized to only u64 or u32 by gcc */
  23. #define IN_64BITS_MODE (sizeof(void *) == 8)
  24. #define FDT_ROOT_ADDR_CELLS_DEFAULT 1
  25. #define FDT_ROOT_SIZE_CELLS_DEFAULT 1
  26. #define FDT_DTB_ALL_NODES_PATH_SIZE (32 * 1024)
  27. #define FDT_DTB_PAD_SIZE 1024
  28. #define FDT_RET_NO_MEMORY 2
  29. #define FDT_RET_NO_LOADED 1
  30. #define FDT_RET_GET_OK 0
  31. #define FDT_RET_GET_EMPTY (-1)
  32. typedef uint32_t phandle;
  33. struct dtb_memreserve
  34. {
  35. uintptr_t address;
  36. size_t size;
  37. };
  38. struct dtb_header
  39. {
  40. char root, zero; /* "/" */
  41. struct dtb_memreserve *memreserve;
  42. size_t memreserve_sz;
  43. };
  44. struct dtb_property
  45. {
  46. const char *name;
  47. int size;
  48. void *value;
  49. struct dtb_property *next;
  50. };
  51. struct dtb_node
  52. {
  53. union
  54. {
  55. const char *name;
  56. const struct dtb_header *header;
  57. };
  58. const char *path;
  59. phandle handle;
  60. int level;
  61. struct dtb_property *properties;
  62. struct dtb_node *parent;
  63. struct dtb_node *child;
  64. struct dtb_node *sibling;
  65. };
  66. #define FDT_MAX_PHANDLE_ARGS 16
  67. /**
  68. * struct dtb_node_phandle_args - structure to hold phandle and arguments
  69. *
  70. * This is used when decoding a phandle in a device tree property. Typically
  71. * these look like this:
  72. *
  73. * wibble {
  74. * phandle = <5>;
  75. * };
  76. *
  77. * ...
  78. * some-prop = <&wibble 1 2 3>
  79. *
  80. * Here &node is the phandle of the node 'wibble', i.e. 5. There are three
  81. * arguments: 1, 2, 3.
  82. *
  83. * So when decoding the phandle in some-prop, np will point to wibble,
  84. * args_count will be 3 and the three arguments will be in args.
  85. *
  86. * @np: Node that the phandle refers to
  87. * @args_count: Number of arguments
  88. * @args: Argument values
  89. */
  90. struct fdt_phandle_args
  91. {
  92. struct dtb_node *np;
  93. int args_count;
  94. uint32_t args[FDT_MAX_PHANDLE_ARGS];
  95. };
  96. /*
  97. * A single signal can be specified via a range of minimal and maximal values
  98. * with a typical value, that lies somewhere inbetween.
  99. */
  100. struct timing_entry
  101. {
  102. uint32_t min;
  103. uint32_t typ;
  104. uint32_t max;
  105. };
  106. void *get_fdt_blob(void);
  107. struct dtb_node *get_dtb_node_head(void);
  108. rt_bool_t dtb_node_active(void);
  109. int device_tree_setup(void *mem_addr);
  110. void *dtb_node_load_from_fs(char *dtb_filename);
  111. void *dtb_node_load_from_memory(void *dtb_ptr, rt_bool_t 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. rt_bool_t dtb_node_get_dtb_node_status(const struct dtb_node *dtb_node);
  134. rt_bool_t 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. rt_bool_t 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_all_child_nodes_from(from, dn) \
  252. for (dn = dtb_node_find_all_nodes(from); dn && (dn->level > from->level); dn = dtb_node_find_all_nodes(dn))
  253. #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
  254. #define dtb_node_get(x) (x)
  255. static inline void dtb_node_put(const struct dtb_node *np)
  256. {
  257. }
  258. /* Helper to read a big number; size is in cells (not bytes) */
  259. static inline uint64_t dtb_node_read_number(const uint32_t *cell, int size)
  260. {
  261. uint64_t r = 0;
  262. while (size--)
  263. r = (r << 32) | fdt32_to_cpu(*(cell++));
  264. return r;
  265. }
  266. /**
  267. * ofnode_valid() - check if an ofnode is valid
  268. *
  269. * @return true if the reference contains a valid ofnode, RT_FALSE if it is NULL
  270. */
  271. static inline rt_bool_t dtb_node_valid(const struct dtb_node *node)
  272. {
  273. if (dtb_node_active())
  274. return node != NULL;
  275. return RT_FALSE;
  276. }
  277. /*dtb_base.c */
  278. rt_bool_t dtb_node_read_bool(const struct dtb_node *node, const char *propname);
  279. const void *dtb_node_read_prop(const struct dtb_node *node, const char *propname, int *sizep);
  280. const char *dtb_node_read_string(const struct dtb_node *node, const char *propname);
  281. const struct dtb_node *dtb_node_find_subnode(const struct dtb_node *node, const char *subnode_name);
  282. int dtb_node_read_u32_array(const struct dtb_node *node, const char *propname,
  283. uint32_t *out_values, size_t sz);
  284. struct dtb_node *dtb_node_first_subnode(const struct dtb_node *node);
  285. struct dtb_node *dtb_node_next_subnode(const struct dtb_node *node);
  286. struct dtb_node *dtb_node_get_parent(const struct dtb_node *node);
  287. const char *dtb_node_get_name(const struct dtb_node *node);
  288. struct dtb_node *dtb_node_get_by_phandle(uint32_t phandle);
  289. int dtb_node_read_size(const struct dtb_node *node, const char *propname);
  290. int dtb_node_get_addr_and_size_by_index(const struct dtb_node *node, int index, size_t *addr, size_t *size);
  291. size_t dtb_node_get_addr_index(const struct dtb_node *node, int index);
  292. size_t dtb_node_get_addr(const struct dtb_node *node);
  293. int dtb_node_stringlist_search(const struct dtb_node *node, const char *property,
  294. const char *string);
  295. int dtb_node_read_string_index(const struct dtb_node *node, const char *property, int index,
  296. const char **outp);
  297. int dtb_node_read_string_count(const struct dtb_node *node, const char *property);
  298. struct dtb_node *dtb_node_path(const char *path);
  299. const char *dtb_node_get_chosen_prop(const char *name);
  300. struct dtb_node *dtb_node_get_chosen_node(const char *name);
  301. const void *dtb_node_get_property(const struct dtb_node *node, const char *propname, int *lenp);
  302. rt_bool_t dtb_node_is_available(const struct dtb_node *node);
  303. size_t dtb_node_get_addr_size(const struct dtb_node *node, const char *property,
  304. size_t *sizep);
  305. const uint8_t *dtb_node_read_u8_array_ptr(const struct dtb_node *node, const char *propname, size_t sz);
  306. int dtb_node_find_all_compatible_node(const struct dtb_node *from, const char *compatible, struct dtb_node **node_table, int max_num, int *node_num);
  307. int dtb_node_write_prop(const struct dtb_node *node, const char *propname, int len,
  308. const void *value);
  309. int dtb_node_write_string(const struct dtb_node *node, const char *propname, const char *value);
  310. int dtb_node_set_enabled(const struct dtb_node *node, rt_bool_t value);
  311. int dtb_node_irq_get(struct dtb_node *dev, int index);
  312. int dtb_node_irq_get_byname(struct dtb_node *dev, const char *name);
  313. int dtb_node_irq_count(struct dtb_node *dev);
  314. /**
  315. * dtb_node_for_each_subnode() - iterate over all subnodes of a parent
  316. *
  317. * @node: child node (ofnode, lvalue)
  318. * @parent: parent node (ofnode)
  319. *
  320. * This is a wrapper around a for loop and is used like so:
  321. *
  322. * ofnode node;
  323. *
  324. * dtb_node_for_each_subnode(node, parent) {
  325. * Use node
  326. * ...
  327. * }
  328. *
  329. * Note that this is implemented as a macro and @node is used as
  330. * iterator in the loop. The parent variable can be a constant or even a
  331. * literal.
  332. */
  333. #define dtb_node_for_each_subnode(node, parent) \
  334. for (node = dtb_node_first_subnode(parent); \
  335. dtb_node_valid(node); \
  336. node = dtb_node_next_subnode(node))
  337. #endif /* RT_FDT_H__ */