drv_wdt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * Copyright (c) 2006-2025, RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #include <rtthread.h>
  31. #include <rtdevice.h>
  32. #include <riscv_io.h>
  33. #include "board.h"
  34. #include "ioremap.h"
  35. #include "sysctl_clk.h"
  36. #include "drv_wdt.h"
  37. #include <rtdbg.h>
  38. #include <sys/ioctl.h>
  39. struct k230_wdt_dev
  40. {
  41. struct rt_watchdog_device device;
  42. const char *name;
  43. rt_ubase_t base;
  44. size_t size;
  45. sysctl_clk_node_e clk; /* clock source */
  46. };
  47. /* There are sixteen TOPs (timeout periods) that can be set in the watchdog. */
  48. static const rt_uint32_t *tops;
  49. static const rt_uint32_t k230_wdt_fix_tops[KD_WDT_NUM_TOPS] = {
  50. KD_WDT_FIX_TOP(0), KD_WDT_FIX_TOP(1), KD_WDT_FIX_TOP(2),
  51. KD_WDT_FIX_TOP(3), KD_WDT_FIX_TOP(4), KD_WDT_FIX_TOP(5),
  52. KD_WDT_FIX_TOP(6), KD_WDT_FIX_TOP(7), KD_WDT_FIX_TOP(8),
  53. KD_WDT_FIX_TOP(9), KD_WDT_FIX_TOP(10), KD_WDT_FIX_TOP(11),
  54. KD_WDT_FIX_TOP(12), KD_WDT_FIX_TOP(13), KD_WDT_FIX_TOP(14),
  55. KD_WDT_FIX_TOP(15)
  56. };
  57. static struct k230_wdt_timeout timeouts[KD_WDT_NUM_TOPS];
  58. static char rmod; /* wdt reset mode, */
  59. static void k230_wdt_timeouts_init(struct k230_wdt_dev *dev)
  60. {
  61. rt_uint32_t wdt_clk;
  62. rt_uint32_t time_value;
  63. rt_uint32_t i, t;
  64. rt_uint64_t msec;
  65. struct k230_wdt_timeout tout, *dst;
  66. /* caculate timeout value */
  67. wdt_clk = sysctl_clk_get_leaf_freq(dev->clk);
  68. for (i = 0; i < KD_WDT_NUM_TOPS; ++i)
  69. {
  70. tout.top_val = i;
  71. tout.sec = tops[i] / wdt_clk;
  72. msec = (rt_uint64_t)tops[i] * (rt_uint64_t)1000L;
  73. msec = msec / wdt_clk;
  74. tout.msec = msec - (rt_uint64_t)tout.sec * (rt_uint64_t)1000L;
  75. for (t = 0; t < i; ++t)
  76. {
  77. dst = &timeouts[t];
  78. if (tout.sec > dst->sec || (tout.sec == dst->sec &&
  79. tout.msec >= dst->msec))
  80. continue;
  81. else
  82. swap(*dst, tout);
  83. }
  84. timeouts[i] = tout;
  85. }
  86. rt_kprintf("watchdog timeout table init OK!\n");
  87. }
  88. static rt_err_t k230_wdt_feed(struct k230_wdt_dev *dev)
  89. {
  90. k230_wdt_t *reg = (k230_wdt_t *)dev->base;
  91. reg->crr = 0x76;
  92. return RT_EOK;
  93. }
  94. static rt_err_t k230_wdt_enable(struct k230_wdt_dev *dev)
  95. {
  96. k230_wdt_t *reg = (k230_wdt_t *)dev->base;
  97. reg->crr = 0x76;
  98. reg->cr |= 0x1;
  99. return RT_EOK;
  100. }
  101. static rt_err_t k230_wdt_disable(struct k230_wdt_dev *dev)
  102. {
  103. k230_wdt_t *reg = (k230_wdt_t *)dev->base;
  104. reg->crr = 0x76;
  105. reg->cr &= ~0x1;
  106. return RT_EOK;
  107. }
  108. static rt_err_t k230_wdt_set_timeout(struct k230_wdt_dev *dev, rt_uint64_t timeout)
  109. {
  110. k230_wdt_t *reg = (k230_wdt_t *)dev->base;
  111. rt_uint32_t top_val;
  112. rt_uint32_t i;
  113. rt_uint32_t time = (timeout + rmod -1) / rmod;
  114. for (i = 0; i < KD_WDT_NUM_TOPS; ++i)
  115. {
  116. if (timeouts[i].sec >= time)
  117. break;
  118. }
  119. if (i == KD_WDT_NUM_TOPS)
  120. --i;
  121. top_val = timeouts[i].top_val;
  122. reg->torr = (top_val << 4) | (top_val << 0);
  123. return RT_EOK;
  124. }
  125. static rt_err_t k230_wdt_get_timeout(struct k230_wdt_dev *dev, void *timeout)
  126. {
  127. rt_uint64_t top_val;
  128. rt_uint32_t i;
  129. k230_wdt_t *reg = (k230_wdt_t *)dev->base;
  130. top_val = ((reg->torr) & 0xf);
  131. for (i = 0; i < KD_WDT_NUM_TOPS; ++i)
  132. {
  133. if (timeouts[i].top_val == top_val)
  134. break;
  135. }
  136. *((rt_uint64_t *)timeout) = timeouts[i].sec * rmod;
  137. return RT_EOK;
  138. }
  139. static rt_err_t k230_wdt_init(rt_watchdog_t *wdt)
  140. {
  141. RT_ASSERT(wdt != NULL);
  142. struct k230_wdt_dev *dev = rt_container_of(wdt, struct k230_wdt_dev, device);
  143. k230_wdt_t *reg = (k230_wdt_t *)dev->base;
  144. reg->cr &= ~(0x01 << 1);
  145. reg->cr |= (0x0 << 1); /* default set wdt reset mode */
  146. rmod = KD_WDT_RMOD_RESET;
  147. if (reg->comp_param_1 & (1 << 6))
  148. {
  149. tops = k230_wdt_fix_tops;
  150. }
  151. k230_wdt_timeouts_init(dev);
  152. if (!timeouts[KD_WDT_NUM_TOPS - 1].sec)
  153. {
  154. rt_kprintf("No any valid Timeout period detected\n");
  155. return -RT_EINVAL;
  156. }
  157. return RT_EOK;
  158. }
  159. static rt_err_t k230_wdt_control(rt_watchdog_t *wdt, int cmd, void *args)
  160. {
  161. RT_ASSERT(wdt != NULL);
  162. struct k230_wdt_dev *dev = rt_container_of(wdt, struct k230_wdt_dev, device);
  163. switch(cmd)
  164. {
  165. case KD_DEVICE_CTRL_WDT_GET_TIMEOUT:
  166. k230_wdt_get_timeout(dev, args);
  167. break;
  168. case KD_DEVICE_CTRL_WDT_SET_TIMEOUT:
  169. k230_wdt_set_timeout(dev, *((rt_uint32_t*)args));
  170. break;
  171. case KD_DEVICE_CTRL_WDT_KEEPALIVE:
  172. k230_wdt_feed(dev);
  173. break;
  174. case KD_DEVICE_CTRL_WDT_START:
  175. k230_wdt_enable(dev);
  176. break;
  177. case RT_DEVICE_CTRL_WDT_STOP:
  178. case KD_DEVICE_CTRL_WDT_STOP:
  179. k230_wdt_disable(dev);
  180. break;
  181. default:
  182. return -RT_EINVAL;
  183. }
  184. return RT_EOK;
  185. }
  186. static struct rt_watchdog_ops k230_wdt_ops =
  187. {
  188. .init = k230_wdt_init,
  189. .control = k230_wdt_control,
  190. };
  191. static struct k230_wdt_dev wdt_devices[] =
  192. {
  193. #ifdef BSP_USING_WDT0
  194. {
  195. .name = "wdt0",
  196. .base = WDT0_BASE_ADDR,
  197. .size = WDT0_IO_SIZE,
  198. .clk = SYSCTL_CLK_WDT0,
  199. },
  200. #endif /* BSP_USING_WDT0 */
  201. #ifdef BSP_USING_WDT1
  202. {
  203. .name = "wdt1",
  204. .base = WDT1_BASE_ADDR,
  205. .size = WDT1_IO_SIZE,
  206. .clk = SYSCTL_CLK_WDT1,
  207. },
  208. #endif /* BSP_USING_WDT1 */
  209. #if !defined(BSP_USING_WDT0) && !defined(BSP_USING_WDT1)
  210. #error "No watchdog device defined!"
  211. #endif
  212. };
  213. int rt_hw_wdt_init(void)
  214. {
  215. rt_uint8_t i;
  216. for (i = 0; i < sizeof(wdt_devices) / sizeof(struct k230_wdt_dev); i++)
  217. {
  218. wdt_devices[i].device.ops = &k230_wdt_ops;
  219. wdt_devices[i].base = (rt_ubase_t)rt_ioremap((void *)wdt_devices[i].base, wdt_devices[i].size);
  220. if (rt_hw_watchdog_register(&wdt_devices[i].device, wdt_devices[i].name, RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
  221. {
  222. LOG_E("%s register failed!", wdt_devices[i].name);
  223. return -RT_ERROR;
  224. }
  225. LOG_D("%s register OK!", wdt_devices[i].name);
  226. }
  227. return RT_EOK;
  228. }
  229. INIT_DEVICE_EXPORT(rt_hw_wdt_init);