reset.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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-11-26 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtservice.h>
  12. #define DBG_TAG "rtdm.reset"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #include <drivers/ofw.h>
  16. #include <drivers/misc.h>
  17. #include <drivers/reset.h>
  18. #include <drivers/platform.h>
  19. struct reset_control_array
  20. {
  21. struct rt_reset_control captain;
  22. rt_size_t count;
  23. struct rt_reset_control *rstcs[];
  24. };
  25. #define reset_control_to_array(rstc) rt_container_of(rstc, struct reset_control_array, captain)
  26. rt_err_t rt_reset_controller_register(struct rt_reset_controller *rstcer)
  27. {
  28. if (!rstcer)
  29. {
  30. return -RT_EINVAL;
  31. }
  32. #if RT_NAME_MAX > 0
  33. rt_strncpy(rstcer->parent.name, RT_RESET_CONTROLLER_OBJ_NAME, RT_NAME_MAX);
  34. #else
  35. rstcer->parent.name = RT_RESET_CONTROLLER_OBJ_NAME;
  36. #endif
  37. rt_list_init(&rstcer->rstc_nodes);
  38. rt_spin_lock_init(&rstcer->spinlock);
  39. if (rstcer->ofw_node)
  40. {
  41. if (!rt_ofw_data(rstcer->ofw_node))
  42. {
  43. rt_ofw_data(rstcer->ofw_node) = rstcer;
  44. }
  45. }
  46. return RT_EOK;
  47. }
  48. rt_err_t rt_reset_controller_unregister(struct rt_reset_controller *rstcer)
  49. {
  50. rt_err_t err = RT_EOK;
  51. if (!rstcer)
  52. {
  53. return -RT_EINVAL;
  54. }
  55. rt_spin_lock(&rstcer->spinlock);
  56. if (!rt_list_isempty(&rstcer->rstc_nodes))
  57. {
  58. err = -RT_EBUSY;
  59. goto _out_lock;
  60. }
  61. _out_lock:
  62. rt_spin_unlock(&rstcer->spinlock);
  63. return err;
  64. }
  65. rt_err_t rt_reset_control_reset(struct rt_reset_control *rstc)
  66. {
  67. rt_err_t err;
  68. if (!rstc)
  69. {
  70. return RT_EOK;
  71. }
  72. if (rstc->rstcer->ops->reset)
  73. {
  74. if ((err = rstc->rstcer->ops->reset(rstc)))
  75. {
  76. return err;
  77. }
  78. }
  79. if (rstc->is_array)
  80. {
  81. struct reset_control_array *rstc_arr = reset_control_to_array(rstc);
  82. for (int i = 0; i < rstc_arr->count; ++i)
  83. {
  84. if ((err = rt_reset_control_reset(rstc_arr->rstcs[i])))
  85. {
  86. return err;
  87. }
  88. }
  89. }
  90. return RT_EOK;
  91. }
  92. rt_err_t rt_reset_control_assert(struct rt_reset_control *rstc)
  93. {
  94. rt_err_t err;
  95. if (!rstc)
  96. {
  97. return RT_EOK;
  98. }
  99. if (rstc->rstcer->ops->assert)
  100. {
  101. if ((err = rstc->rstcer->ops->assert(rstc)))
  102. {
  103. return err;
  104. }
  105. }
  106. if (rstc->is_array)
  107. {
  108. struct reset_control_array *rstc_arr = reset_control_to_array(rstc);
  109. for (int i = 0; i < rstc_arr->count; ++i)
  110. {
  111. if ((err = rt_reset_control_assert(rstc_arr->rstcs[i])))
  112. {
  113. if (rstc->rstcer->ops->deassert)
  114. {
  115. rstc->rstcer->ops->deassert(rstc);
  116. }
  117. while (i --> 0)
  118. {
  119. rt_reset_control_deassert(rstc_arr->rstcs[i]);
  120. }
  121. return err;
  122. }
  123. }
  124. }
  125. return RT_EOK;
  126. }
  127. rt_err_t rt_reset_control_deassert(struct rt_reset_control *rstc)
  128. {
  129. rt_err_t err;
  130. if (!rstc)
  131. {
  132. return RT_EOK;
  133. }
  134. if (rstc->rstcer->ops->deassert)
  135. {
  136. if ((err = rstc->rstcer->ops->deassert(rstc)))
  137. {
  138. return err;
  139. }
  140. }
  141. if (rstc->is_array)
  142. {
  143. struct reset_control_array *rstc_arr = reset_control_to_array(rstc);
  144. for (int i = 0; i < rstc_arr->count; ++i)
  145. {
  146. if ((err = rt_reset_control_deassert(rstc_arr->rstcs[i])))
  147. {
  148. if (rstc->rstcer->ops->assert)
  149. {
  150. rstc->rstcer->ops->assert(rstc);
  151. }
  152. while (i --> 0)
  153. {
  154. rt_reset_control_assert(rstc_arr->rstcs[i]);
  155. }
  156. return err;
  157. }
  158. }
  159. }
  160. return RT_EOK;
  161. }
  162. int rt_reset_control_status(struct rt_reset_control *rstc)
  163. {
  164. if (!rstc)
  165. {
  166. return RT_EOK;
  167. }
  168. if (rstc->rstcer->ops->status)
  169. {
  170. return rstc->rstcer->ops->status(rstc);
  171. }
  172. return -RT_ENOSYS;
  173. }
  174. static void reset_free(struct rt_reset_control *rstc)
  175. {
  176. if (rstc->is_array)
  177. {
  178. struct reset_control_array *rstc_arr = reset_control_to_array(rstc);
  179. for (int i = 0; i < rstc_arr->count; ++i)
  180. {
  181. rt_reset_control_put(rstc_arr->rstcs[i]);
  182. }
  183. }
  184. rt_free(rstc);
  185. }
  186. struct rt_reset_control *rt_reset_control_get_array(struct rt_device *dev)
  187. {
  188. return rt_ofw_get_reset_control_array(dev->ofw_node);
  189. }
  190. struct rt_reset_control *rt_reset_control_get_by_index(struct rt_device *dev, int index)
  191. {
  192. return rt_ofw_get_reset_control_by_index(dev->ofw_node, index);
  193. }
  194. struct rt_reset_control *rt_reset_control_get_by_name(struct rt_device *dev, const char *name)
  195. {
  196. return rt_ofw_get_reset_control_by_name(dev->ofw_node, name);
  197. }
  198. void rt_reset_control_put(struct rt_reset_control *rstc)
  199. {
  200. struct rt_reset_controller *rstcer;
  201. if (!rstc)
  202. {
  203. return;
  204. }
  205. rstcer = rstc->rstcer;
  206. rt_spin_lock(&rstcer->spinlock);
  207. rt_list_remove(&rstc->list);
  208. rt_spin_unlock(&rstcer->spinlock);
  209. reset_free(rstc);
  210. }
  211. static struct rt_reset_control *ofw_get_reset_control(struct rt_ofw_node *np, int index,
  212. const char *name, rt_bool_t is_array)
  213. {
  214. rt_err_t err = RT_EOK;
  215. struct rt_reset_control *rstc;
  216. struct rt_ofw_cell_args reset_args = {};
  217. struct rt_reset_controller *rstcer = RT_NULL;
  218. if (is_array)
  219. {
  220. rt_size_t rstc_nr;
  221. struct reset_control_array *rstc_arr;
  222. rstc_nr = rt_ofw_count_phandle_cells(np, "resets", "#reset-cells");
  223. if (!rstc_nr)
  224. {
  225. return RT_NULL;
  226. }
  227. rstc_arr = rt_calloc(1, sizeof(*rstc_arr) + sizeof(struct rt_reset_control *) * rstc_nr);
  228. if (!rstc_arr)
  229. {
  230. LOG_E("No memory to create %s[%d] reset control",
  231. rt_ofw_node_full_name(np), index);
  232. return rt_err_ptr(-RT_ENOMEM);
  233. }
  234. rstc_arr->count = rstc_nr - 1;
  235. for (int i = 0; i < rstc_arr->count; ++i)
  236. {
  237. rstc_arr->rstcs[i] = ofw_get_reset_control(np, i + 1, RT_NULL, RT_FALSE);
  238. if (rt_is_err(rstc_arr->rstcs[i]))
  239. {
  240. err = rt_ptr_err(rstc_arr->rstcs[i]);
  241. while (i --> 0)
  242. {
  243. rt_reset_control_put(rstc_arr->rstcs[i]);
  244. }
  245. rt_free(rstc_arr);
  246. return rt_err_ptr(err);
  247. }
  248. }
  249. rstc = &rstc_arr->captain;
  250. rstc->is_array = RT_TRUE;
  251. }
  252. else
  253. {
  254. rstc = rt_calloc(1, sizeof(*rstc));
  255. if (!rstc)
  256. {
  257. LOG_E("No memory to create %s[%d] reset control",
  258. rt_ofw_node_full_name(np), index);
  259. return rt_err_ptr(-RT_ENOMEM);
  260. }
  261. }
  262. if (!rt_ofw_parse_phandle_cells(np, "resets", "#reset-cells", index, &reset_args))
  263. {
  264. void *rt_data;
  265. struct rt_object *obj;
  266. struct rt_ofw_node *reset_np = reset_args.data;
  267. if (!rt_ofw_data(reset_np))
  268. {
  269. rt_platform_ofw_request(reset_np);
  270. }
  271. rt_data = rt_ofw_data(reset_np);
  272. if (rt_data && (obj = rt_ofw_parse_object(reset_args.data,
  273. RT_RESET_CONTROLLER_OBJ_NAME, "#reset-cells")))
  274. {
  275. rstcer = rt_container_of(obj, struct rt_reset_controller, parent);
  276. }
  277. rt_ofw_node_put(reset_np);
  278. if (!rstcer)
  279. {
  280. err = -RT_EINVAL;
  281. goto _fail;
  282. }
  283. }
  284. else
  285. {
  286. /* Not reset */
  287. goto _fail;
  288. }
  289. if (!name && rt_ofw_prop_read_bool(np, "reset-names"))
  290. {
  291. rt_ofw_prop_read_string_index(np, "reset-names", index, &name);
  292. }
  293. rstc->con_id = name;
  294. rstc->rstcer = rstcer;
  295. if (rstcer->ops->ofw_parse)
  296. {
  297. err = rstcer->ops->ofw_parse(rstc, &reset_args);
  298. if (err)
  299. {
  300. LOG_E("Parse %s reset control error = %s",
  301. rt_ofw_node_full_name(np), rt_strerror(err));
  302. goto _fail;
  303. }
  304. }
  305. rstc->id = reset_args.args[0];
  306. rt_list_init(&rstc->list);
  307. rt_spin_lock(&rstcer->spinlock);
  308. rt_list_insert_after(&rstcer->rstc_nodes, &rstc->list);
  309. rt_spin_unlock(&rstcer->spinlock);
  310. return rstc;
  311. _fail:
  312. if (rstc && !rstc->is_array)
  313. {
  314. rt_free(rstc);
  315. }
  316. return rt_err_ptr(err);
  317. }
  318. struct rt_reset_control *rt_ofw_get_reset_control_array(struct rt_ofw_node *np)
  319. {
  320. return ofw_get_reset_control(np, 0, RT_NULL, RT_TRUE);
  321. }
  322. struct rt_reset_control *rt_ofw_get_reset_control_by_index(struct rt_ofw_node *np, int index)
  323. {
  324. return ofw_get_reset_control(np, index, RT_NULL, RT_FALSE);
  325. }
  326. struct rt_reset_control *rt_ofw_get_reset_control_by_name(struct rt_ofw_node *np, const char *name)
  327. {
  328. if (np)
  329. {
  330. int index = rt_ofw_prop_index_of_string(np, "reset-names", name);
  331. if (index >= 0)
  332. {
  333. return ofw_get_reset_control(np, index, name, RT_FALSE);
  334. }
  335. }
  336. return RT_NULL;
  337. }