dtb_node.h 16 KB

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