drv_wdt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-07-28 songchao first version
  9. */
  10. #include <rtconfig.h>
  11. #ifdef RT_USING_WDT
  12. #include <rtthread.h>
  13. #include <rtdbg.h>
  14. #include "drv_wdt.h"
  15. #include "fsl_wdog.h"
  16. #include "imx6ull.h"
  17. enum
  18. {
  19. #ifdef RT_USING_WDT1
  20. DEV_WDT1,
  21. #endif
  22. #ifdef RT_USING_WDT2
  23. DEV_WDT2,
  24. #endif
  25. #ifdef RT_USING_WDT3
  26. DEV_WDT3,
  27. #endif
  28. DEV_MAX,
  29. };
  30. #ifdef RT_USING_WDT1
  31. static wdog_config_t WDOG_1_config =
  32. {
  33. .timeoutValue = 0xffu,
  34. .enablePowerDown = false,
  35. .softwareResetExtension = false,
  36. .softwareAssertion = true,
  37. .softwareResetSignal = true,
  38. .enableWdog = true,
  39. .workMode =
  40. {
  41. .enableWait = false,
  42. .enableStop = false,
  43. .enableDebug = false,
  44. },
  45. .enableInterrupt = false,
  46. .interruptTimeValue = 0x04u,
  47. };
  48. #endif
  49. #ifdef RT_USING_WDT2
  50. static wdog_config_t WDOG_2_config =
  51. {
  52. .timeoutValue = 0xffu,
  53. .enablePowerDown = false,
  54. .softwareResetExtension = false,
  55. .softwareAssertion = true,
  56. .softwareResetSignal = true,
  57. .enableWdog = true,
  58. .workMode =
  59. {
  60. .enableWait = false,
  61. .enableStop = false,
  62. .enableDebug = false,
  63. },
  64. .enableInterrupt = false,
  65. .interruptTimeValue = 0x04u,
  66. };
  67. #endif
  68. #ifdef RT_USING_WDT3
  69. static wdog_config_t WDOG_3_config =
  70. {
  71. .timeoutValue = 0xffu,
  72. .enablePowerDown = false,
  73. .softwareResetExtension = false,
  74. .softwareAssertion = true,
  75. .softwareResetSignal = true,
  76. .enableWdog = true,
  77. .workMode =
  78. {
  79. .enableWait = false,
  80. .enableStop = false,
  81. .enableDebug = false,
  82. },
  83. .enableInterrupt = false,
  84. .interruptTimeValue = 0x04u,
  85. };
  86. #endif
  87. static rt_watchdog_t imx6ull_watchdog[DEV_MAX] =
  88. {
  89. #ifdef RT_USING_WDT1
  90. {
  91. .name = "wdt1",
  92. .paddr = IMX6ULL_WATCHDOG1_BASE,
  93. .config = &WDOG_1_config,
  94. },
  95. #endif
  96. #ifdef RT_USING_WDT2
  97. {
  98. .name = "wdt2",
  99. .paddr = IMX6ULL_WATCHDOG2_BASE,
  100. .config = &WDOG_2_config,
  101. },
  102. #endif
  103. #ifdef RT_USING_WDT3
  104. {
  105. .name = "wdt3",
  106. .paddr = IMX6ULL_WATCHDOG3_BASE,
  107. .config = &WDOG_3_config,
  108. },
  109. #endif
  110. };
  111. static rt_err_t imx6ull_wdog_init(rt_watchdog_t *wdt)
  112. {
  113. WDOG_Type *base = RT_NULL;
  114. base = (WDOG_Type *)wdt->vaddr;
  115. WDOG_Init(base, wdt->config);
  116. WDOG_Disable(base);
  117. return RT_EOK;
  118. }
  119. static rt_err_t imx6ull_wdog_control(rt_watchdog_t *wdt, int cmd, void *args)
  120. {
  121. RT_ASSERT(wdt != NULL);
  122. WDOG_Type *base = RT_NULL;
  123. base = (WDOG_Type *)wdt->vaddr;
  124. switch(cmd)
  125. {
  126. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  127. {
  128. *(uint16_t *)args = (base->WCR >> 8) / 2;
  129. }
  130. break;
  131. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  132. {
  133. RT_ASSERT(*(uint16_t *)args != 0);
  134. WDOG_SetTimeoutValue(base, (*(uint16_t *)args) * 2);
  135. WDOG_Disable(base);
  136. }
  137. break;
  138. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  139. {
  140. WDOG_Refresh(base);
  141. }
  142. break;
  143. case RT_DEVICE_CTRL_WDT_START:
  144. {
  145. WDOG_Enable(base);
  146. }
  147. break;
  148. case RT_DEVICE_CTRL_WDT_STOP:
  149. {
  150. WDOG_Disable(base);
  151. }
  152. break;
  153. default:
  154. return RT_EINVAL;
  155. }
  156. return RT_EOK;
  157. }
  158. static struct rt_watchdog_ops imx6ull_wdog_ops =
  159. {
  160. .init = imx6ull_wdog_init,
  161. .control = imx6ull_wdog_control,
  162. };
  163. int rt_hw_wdt_init(void)
  164. {
  165. rt_err_t ret = RT_EOK;
  166. for(int idx = 0; idx < GET_ARRAY_NUM(imx6ull_watchdog); idx++)
  167. {
  168. imx6ull_watchdog[idx].ops = &imx6ull_wdog_ops;
  169. imx6ull_watchdog[idx].vaddr = platform_get_periph_vaddr(imx6ull_watchdog[idx].paddr);
  170. ret = rt_hw_watchdog_register(&imx6ull_watchdog[idx], imx6ull_watchdog[idx].name,
  171. RT_DEVICE_FLAG_DEACTIVATE, RT_NULL);
  172. if (ret != RT_EOK)
  173. {
  174. LOG_E("rt device register failed %d\n", ret);
  175. }
  176. }
  177. return ret;
  178. }
  179. INIT_DEVICE_EXPORT(rt_hw_wdt_init);
  180. #endif