ofw.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-08-25 GuEe-GUI first version
  9. */
  10. #ifndef __OFW_H__
  11. #define __OFW_H__
  12. #include <rtthread.h>
  13. #include <ref.h>
  14. #include <bitmap.h>
  15. #include <libfdt/libfdt.h>
  16. typedef rt_uint32_t rt_phandle;
  17. struct rt_ofw_prop
  18. {
  19. const char *name;
  20. int length;
  21. void *value;
  22. struct rt_ofw_prop *next;
  23. };
  24. struct rt_ofw_node
  25. {
  26. const char *name;
  27. /* full_name is 'path/tag' or 'path/tag@reg' */
  28. const char *full_name;
  29. /* phandles range from 1 to 2^32-2 (0xfffffffe) */
  30. rt_phandle phandle;
  31. struct rt_ofw_prop *props;
  32. struct rt_ofw_node *parent;
  33. struct rt_ofw_node *child;
  34. struct rt_ofw_node *sibling;
  35. struct rt_ref ref;
  36. #define RT_OFW_F_SYSTEM 0 /* node is system node */
  37. #define RT_OFW_F_READLY 1 /* node has driver */
  38. #define RT_OFW_F_PLATFORM 2 /* node is platform device */
  39. #define RT_OFW_F_OVERLAY 3 /* node is from overlay */
  40. rt_bitmap_t flags;
  41. /* RT-Thread object prototype */
  42. void *rt_data;
  43. };
  44. #define RT_OFW_MAX_CELL_ARGS 16
  45. struct rt_ofw_cell_args
  46. {
  47. void *data;
  48. int args_count;
  49. rt_uint32_t args[RT_OFW_MAX_CELL_ARGS];
  50. };
  51. struct rt_ofw_node_id
  52. {
  53. /* The name string should consist name property (deprecated) */
  54. char name[32];
  55. /*
  56. * The type string should consist device_type property, such as pci, memory
  57. * serial. Because it's deprecated in <devicetree-basics>, we can use other
  58. * name (like "ttyS" or "ttyAMA" ...) to config with /chosen.
  59. */
  60. char type[32];
  61. /*
  62. * The compatible string should consist only of lowercase letters, digits
  63. * and dashes, and should start with a letter. A single comma is typically
  64. * only used following a vendor prefix. Underscores should not be used.
  65. */
  66. char compatible[128];
  67. const void *data;
  68. };
  69. struct rt_ofw_stub
  70. {
  71. const struct rt_ofw_node_id *ids;
  72. rt_err_t (*handler)(struct rt_ofw_node *np, const struct rt_ofw_node_id *id);
  73. };
  74. #define RT_OFW_SYMBOL(_class, _level) \
  75. rt_section(".rt_ofw_data." #_class "." #_level)
  76. #define RT_OFW_SYMBOL_TYPE_RANGE(_class, _type, _start, _end) \
  77. static const rt_used RT_OFW_SYMBOL(_class, 0) _type _start; \
  78. static const rt_used RT_OFW_SYMBOL(_class, end) _type _end; \
  79. #define RT_OFW_STUB_EXPORT(_name, _ids, _class, _handler, ...) \
  80. static const struct rt_ofw_stub __rt_ofw_##_name \
  81. rt_used RT_OFW_SYMBOL(_class, __VA_ARGS__ _) = \
  82. { \
  83. .ids = _ids, \
  84. .handler = _handler, \
  85. }
  86. #define RT_OFW_STUB_RANGE_EXPORT(_class, _start, _end) \
  87. RT_OFW_SYMBOL_TYPE_RANGE(_class, struct rt_ofw_stub, _start = {}, _end = {})
  88. #define rt_ofw_data(np) ((struct rt_ofw_node *)np)->rt_data
  89. rt_inline rt_bool_t rt_ofw_node_test_flag(const struct rt_ofw_node *np, int flag)
  90. {
  91. return rt_bitmap_test_bit((rt_bitmap_t *)&np->flags, flag);
  92. }
  93. rt_inline void rt_ofw_node_set_flag(struct rt_ofw_node *np, int flag)
  94. {
  95. rt_bitmap_set_bit(&np->flags, flag);
  96. }
  97. rt_inline rt_bool_t rt_ofw_node_test_and_set_flag(struct rt_ofw_node *np, int flag)
  98. {
  99. rt_bool_t res = rt_ofw_node_test_flag(np, flag);
  100. rt_ofw_node_set_flag(np, flag);
  101. return res;
  102. }
  103. rt_inline void rt_ofw_node_clear_flag(struct rt_ofw_node *np, int flag)
  104. {
  105. rt_bitmap_clear_bit(&np->flags, flag);
  106. }
  107. rt_err_t rt_ofw_node_destroy(struct rt_ofw_node *np);
  108. struct rt_ofw_node *rt_ofw_node_get(struct rt_ofw_node *np);
  109. void rt_ofw_node_put(struct rt_ofw_node *np);
  110. rt_bool_t rt_ofw_node_tag_equ(const struct rt_ofw_node *np, const char *tag);
  111. rt_bool_t rt_ofw_node_tag_prefix(const struct rt_ofw_node *np, const char *prefix);
  112. rt_inline const char *rt_ofw_node_name(const struct rt_ofw_node *np)
  113. {
  114. return np ? np->name : "<no-node>";
  115. }
  116. rt_inline const char *rt_ofw_node_full_name(const struct rt_ofw_node *np)
  117. {
  118. return np ? np->full_name : "<no-node>";
  119. }
  120. rt_bool_t rt_ofw_machine_is_compatible(const char *compatible);
  121. rt_bool_t rt_ofw_node_is_available(const struct rt_ofw_node *np);
  122. rt_bool_t rt_ofw_node_is_compatible(const struct rt_ofw_node *np, const char *compatible);
  123. struct rt_ofw_node_id *rt_ofw_prop_match(struct rt_ofw_prop *prop, const struct rt_ofw_node_id *ids);
  124. struct rt_ofw_node_id *rt_ofw_node_match(struct rt_ofw_node *np, const struct rt_ofw_node_id *ids);
  125. struct rt_ofw_node *rt_ofw_find_node_by_tag(struct rt_ofw_node *from, const char *tag);
  126. struct rt_ofw_node *rt_ofw_find_node_by_prop_r(struct rt_ofw_node *from, const char *propname,
  127. const struct rt_ofw_prop **out_prop);
  128. rt_inline struct rt_ofw_node *rt_ofw_find_node_by_prop(struct rt_ofw_node *from, const char *propname)
  129. {
  130. return rt_ofw_find_node_by_prop_r(from, propname, RT_NULL);
  131. }
  132. struct rt_ofw_node *rt_ofw_find_node_by_name(struct rt_ofw_node *from, const char *name);
  133. struct rt_ofw_node *rt_ofw_find_node_by_type(struct rt_ofw_node *from, const char *type);
  134. struct rt_ofw_node *rt_ofw_find_node_by_compatible(struct rt_ofw_node *from, const char *compatible);
  135. struct rt_ofw_node *rt_ofw_find_node_by_ids_r(struct rt_ofw_node *from, const struct rt_ofw_node_id *ids,
  136. const struct rt_ofw_node_id **out_id);
  137. struct rt_ofw_node *rt_ofw_find_node_by_path(const char *path);
  138. struct rt_ofw_node *rt_ofw_find_node_by_phandle(rt_phandle phandle);
  139. rt_inline struct rt_ofw_node *rt_ofw_find_node_by_ids(struct rt_ofw_node *from, const struct rt_ofw_node_id *ids)
  140. {
  141. return rt_ofw_find_node_by_ids_r(from, ids, RT_NULL);
  142. }
  143. struct rt_ofw_node *rt_ofw_get_parent(const struct rt_ofw_node *np);
  144. struct rt_ofw_node *rt_ofw_get_child_by_tag(const struct rt_ofw_node *parent, const char *tag);
  145. struct rt_ofw_node *rt_ofw_get_child_by_compatible(const struct rt_ofw_node *parent, const char *compatible);
  146. int rt_ofw_get_child_count(const struct rt_ofw_node *np);
  147. int rt_ofw_get_available_child_count(const struct rt_ofw_node *np);
  148. struct rt_ofw_node *rt_ofw_get_next_node(struct rt_ofw_node *prev);
  149. struct rt_ofw_node *rt_ofw_get_next_parent(struct rt_ofw_node *prev);
  150. struct rt_ofw_node *rt_ofw_get_next_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev);
  151. struct rt_ofw_node *rt_ofw_get_next_available_child(const struct rt_ofw_node *parent, struct rt_ofw_node *prev);
  152. struct rt_ofw_node *rt_ofw_get_cpu_node(int cpu, int *thread, rt_bool_t (*match_cpu_hwid)(int cpu, rt_uint64_t hwid));
  153. struct rt_ofw_node *rt_ofw_get_next_cpu_node(struct rt_ofw_node *prev);
  154. struct rt_ofw_node *rt_ofw_get_cpu_state_node(struct rt_ofw_node *cpu_np, int index);
  155. rt_uint64_t rt_ofw_get_cpu_id(struct rt_ofw_node *cpu_np);
  156. rt_uint64_t rt_ofw_get_cpu_hwid(struct rt_ofw_node *cpu_np, unsigned int thread);
  157. struct rt_ofw_node *rt_ofw_get_alias_node(const char *tag, int id);
  158. int rt_ofw_get_alias_id(struct rt_ofw_node *np, const char *tag);
  159. int rt_ofw_get_alias_last_id(const char *tag);
  160. struct rt_ofw_node *rt_ofw_parse_phandle(const struct rt_ofw_node *np, const char *phandle_name, int index);
  161. rt_err_t rt_ofw_parse_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name,
  162. int index, struct rt_ofw_cell_args *out_args);
  163. int rt_ofw_count_phandle_cells(const struct rt_ofw_node *np, const char *list_name, const char *cells_name);
  164. struct rt_ofw_prop *rt_ofw_get_prop(const struct rt_ofw_node *np, const char *name, rt_ssize_t *out_length);
  165. rt_inline const void *rt_ofw_prop_read_raw(const struct rt_ofw_node *np, const char *name, rt_ssize_t *out_length)
  166. {
  167. struct rt_ofw_prop *prop = rt_ofw_get_prop(np, name, out_length);
  168. return prop ? prop->value : RT_NULL;
  169. }
  170. int rt_ofw_prop_read_u8_array_index(const struct rt_ofw_node *np, const char *propname,
  171. int index, int nr, rt_uint8_t *out_values);
  172. int rt_ofw_prop_read_u16_array_index(const struct rt_ofw_node *np, const char *propname,
  173. int index, int nr, rt_uint16_t *out_values);
  174. int rt_ofw_prop_read_u32_array_index(const struct rt_ofw_node *np, const char *propname,
  175. int index, int nr, rt_uint32_t *out_values);
  176. int rt_ofw_prop_read_u64_array_index(const struct rt_ofw_node *np, const char *propname,
  177. int index, int nr, rt_uint64_t *out_values);
  178. int rt_ofw_prop_read_string_array_index(const struct rt_ofw_node *np, const char *propname,
  179. int index, int nr, const char **out_strings);
  180. int rt_ofw_prop_count_of_size(const struct rt_ofw_node *np, const char *propname, int size);
  181. int rt_ofw_prop_index_of_string(const struct rt_ofw_node *np, const char *propname, const char *string);
  182. const fdt32_t *rt_ofw_prop_next_u32(struct rt_ofw_prop *prop, const fdt32_t *cur, rt_uint32_t *out_value);
  183. const char *rt_ofw_prop_next_string(struct rt_ofw_prop *prop, const char *cur);
  184. rt_inline rt_err_t rt_ofw_prop_read_u8_index(const struct rt_ofw_node *np, const char *propname,
  185. int index, rt_uint8_t *out_value)
  186. {
  187. int nr = rt_ofw_prop_read_u8_array_index(np, propname, index, 1, out_value);
  188. return nr > 0 ? RT_EOK : (rt_err_t)nr;
  189. }
  190. rt_inline rt_err_t rt_ofw_prop_read_u16_index(const struct rt_ofw_node *np, const char *propname,
  191. int index, rt_uint16_t *out_value)
  192. {
  193. int nr = rt_ofw_prop_read_u16_array_index(np, propname, index, 1, out_value);
  194. return nr > 0 ? RT_EOK : (rt_err_t)nr;
  195. }
  196. rt_inline rt_err_t rt_ofw_prop_read_u32_index(const struct rt_ofw_node *np, const char *propname,
  197. int index, rt_uint32_t *out_value)
  198. {
  199. int nr = rt_ofw_prop_read_u32_array_index(np, propname, index, 1, out_value);
  200. return nr > 0 ? RT_EOK : (rt_err_t)nr;
  201. }
  202. rt_inline rt_err_t rt_ofw_prop_read_u64_index(const struct rt_ofw_node *np, const char *propname,
  203. int index, rt_uint64_t *out_value)
  204. {
  205. int nr = rt_ofw_prop_read_u64_array_index(np, propname, index, 1, out_value);
  206. return nr > 0 ? RT_EOK : (rt_err_t)nr;
  207. }
  208. rt_inline rt_err_t rt_ofw_prop_read_string_index(const struct rt_ofw_node *np, const char *propname,
  209. int index, const char **out_string)
  210. {
  211. int nr = rt_ofw_prop_read_string_array_index(np, propname, index, 1, out_string);
  212. return nr > 0 ? RT_EOK : (rt_err_t)nr;
  213. }
  214. rt_inline rt_err_t rt_ofw_prop_read_u8(const struct rt_ofw_node *np, const char *propname,
  215. rt_uint8_t *out_value)
  216. {
  217. return rt_ofw_prop_read_u8_index(np, propname, 0, out_value);
  218. }
  219. rt_inline rt_err_t rt_ofw_prop_read_u16(const struct rt_ofw_node *np, const char *propname,
  220. rt_uint16_t *out_value)
  221. {
  222. return rt_ofw_prop_read_u16_index(np, propname, 0, out_value);
  223. }
  224. rt_inline rt_err_t rt_ofw_prop_read_u32(const struct rt_ofw_node *np, const char *propname,
  225. rt_uint32_t *out_value)
  226. {
  227. return rt_ofw_prop_read_u32_index(np, propname, 0, out_value);
  228. }
  229. rt_inline rt_err_t rt_ofw_prop_read_s32(const struct rt_ofw_node *np, const char *propname,
  230. rt_int32_t *out_value)
  231. {
  232. return rt_ofw_prop_read_u32_index(np, propname, 0, (rt_uint32_t *)out_value);
  233. }
  234. rt_inline rt_err_t rt_ofw_prop_read_u64(const struct rt_ofw_node *np, const char *propname,
  235. rt_uint64_t *out_value)
  236. {
  237. return rt_ofw_prop_read_u64_index(np, propname, 0, out_value);
  238. }
  239. rt_inline rt_err_t rt_ofw_prop_read_string(const struct rt_ofw_node *np, const char *propname,
  240. const char **out_string)
  241. {
  242. return rt_ofw_prop_read_string_index(np, propname, 0, out_string);
  243. }
  244. rt_inline rt_bool_t rt_ofw_prop_read_bool(const struct rt_ofw_node *np, const char *propname)
  245. {
  246. return rt_ofw_get_prop(np, propname, RT_NULL) ? RT_TRUE : RT_FALSE;
  247. }
  248. rt_inline int rt_ofw_prop_count_of_u8(const struct rt_ofw_node *np, const char *propname)
  249. {
  250. return rt_ofw_prop_count_of_size(np, propname, sizeof(rt_uint8_t));
  251. }
  252. rt_inline int rt_ofw_prop_count_of_u16(const struct rt_ofw_node *np, const char *propname)
  253. {
  254. return rt_ofw_prop_count_of_size(np, propname, sizeof(rt_uint16_t));
  255. }
  256. rt_inline int rt_ofw_prop_count_of_u32(const struct rt_ofw_node *np, const char *propname)
  257. {
  258. return rt_ofw_prop_count_of_size(np, propname, sizeof(rt_uint32_t));
  259. }
  260. rt_inline int rt_ofw_prop_count_of_u64(const struct rt_ofw_node *np, const char *propname)
  261. {
  262. return rt_ofw_prop_count_of_size(np, propname, sizeof(rt_uint64_t));
  263. }
  264. rt_inline const char *rt_ofw_node_type(const struct rt_ofw_node *np)
  265. {
  266. return rt_ofw_prop_read_raw(np, "device_type", RT_NULL);
  267. }
  268. rt_inline rt_bool_t rt_ofw_node_is_type(const struct rt_ofw_node *np, const char *type)
  269. {
  270. const char *get_type = rt_ofw_node_type(np);
  271. return np && get_type && type && !rt_strcmp(get_type, type);
  272. }
  273. #define rt_ofw_foreach_node_by_tag(np, name) \
  274. for (np = rt_ofw_find_node_by_tag(RT_NULL, name); np; \
  275. np = rt_ofw_find_node_by_tag(np, name))
  276. #define rt_ofw_foreach_node_by_prop(np, prop_name) \
  277. for (np = rt_ofw_find_node_by_prop(RT_NULL, prop_name); \
  278. np; np = rt_ofw_find_node_by_prop(np, prop_name))
  279. #define rt_ofw_foreach_node_by_prop_r(np, prop_name, prop) \
  280. for (np = rt_ofw_find_node_by_prop_r(RT_NULL, prop_name, prop); \
  281. np; np = rt_ofw_find_node_by_prop_r(np, prop_name, prop))
  282. #define rt_ofw_foreach_node_by_name(np, name) \
  283. for (np = rt_ofw_find_node_by_name(RT_NULL, name); np; \
  284. np = rt_ofw_find_node_by_name(np, name))
  285. #define rt_ofw_foreach_node_by_type(np, type) \
  286. for (np = rt_ofw_find_node_by_type(RT_NULL, type); np; \
  287. np = rt_ofw_find_node_by_type(np, type))
  288. #define rt_ofw_foreach_node_by_compatible(np, type, compatible) \
  289. for (np = rt_ofw_find_node_by_compatible(RT_NULL, type, compatible); np; \
  290. np = rt_ofw_find_node_by_compatible(np, type, compatible))
  291. #define rt_ofw_foreach_node_by_ids_r(np, id, ids) \
  292. for (np = rt_ofw_find_node_by_ids_r(RT_NULL, ids, id); \
  293. np; np = rt_ofw_find_node_by_ids_r(np, ids, id))
  294. #define rt_ofw_foreach_node_by_ids(np, ids) \
  295. for (np = rt_ofw_find_node_by_ids(RT_NULL, ids); np; \
  296. np = rt_ofw_find_node_by_ids(np, ids))
  297. #define rt_ofw_foreach_nodes(from, np) \
  298. for (np = rt_ofw_get_next_node(from); \
  299. np; np = rt_ofw_get_next_node(np))
  300. #define rt_ofw_foreach_allnodes(np) \
  301. rt_ofw_foreach_nodes(RT_NULL, np)
  302. #define rt_ofw_foreach_parent_node(np) \
  303. for (np = rt_ofw_get_next_parent(rt_ofw_node_get(np)); \
  304. np; np = rt_ofw_get_next_parent(np))
  305. #define rt_ofw_foreach_child_node(parent, child) \
  306. for (child = rt_ofw_get_next_child(parent, RT_NULL); \
  307. child; child = rt_ofw_get_next_child(parent, child))
  308. #define rt_ofw_foreach_available_child_node(parent, child) \
  309. for (child = rt_ofw_get_next_available_child(parent, RT_NULL); child; \
  310. child = rt_ofw_get_next_available_child(parent, child))
  311. #define rt_ofw_foreach_cpu_node(cpu_np) \
  312. for (cpu_np = rt_ofw_get_next_cpu_node(RT_NULL); \
  313. cpu_np; cpu_np = rt_ofw_get_next_cpu_node(cpu_np))
  314. #define rt_ofw_foreach_prop(np, prop) \
  315. for (prop = np->props; prop; prop = prop->next)
  316. #define rt_ofw_foreach_prop_u32(np, propname, prop, p, u) \
  317. for (prop = rt_ofw_get_prop(np, propname, RT_NULL), \
  318. p = rt_ofw_prop_next_u32(prop, RT_NULL, &u); p; \
  319. p = rt_ofw_prop_next_u32(prop, p, &u))
  320. #define rt_ofw_foreach_prop_string(np, propname, prop, s) \
  321. for (prop = rt_ofw_get_prop(np, propname, RT_NULL), \
  322. s = rt_ofw_prop_next_string(prop, RT_NULL); s; \
  323. s = rt_ofw_prop_next_string(prop, s))
  324. #define rt_ofw_foreach_stub(stub, stub_start, stub_end) \
  325. for (stub = stub_start; stub <= stub_end; ++stub)
  326. struct rt_ofw_stub *rt_ofw_stub_probe_range(struct rt_ofw_node *np,
  327. const struct rt_ofw_stub *stub_start, const struct rt_ofw_stub *stub_end);
  328. rt_err_t rt_ofw_console_setup(void);
  329. const char *rt_ofw_bootargs_select(const char *key, int index);
  330. #ifdef RT_USING_CONSOLE
  331. void rt_ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too);
  332. #endif
  333. #endif /* __OFW_H__ */