ofw.h 16 KB

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