drv_wdt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * 2020-10-26 bigmagic first version
  9. */
  10. #include <rthw.h>
  11. #include "drv_wdt.h"
  12. #include "raspi4.h"
  13. #ifdef BSP_USING_WDT
  14. #define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  15. #define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  16. static struct raspi_wdt_driver bcm_wdt;
  17. void raspi_watchdog_init(rt_uint32_t time_init)
  18. {
  19. bcm_wdt.timeout = time_init;
  20. }
  21. void raspi_watchdog_start()
  22. {
  23. volatile rt_uint32_t cur;
  24. PM_WDOG = PM_PASSWORD | (SECS_TO_WDOG_TICKS(bcm_wdt.timeout) & PM_WDOG_TIME_SET);
  25. cur = (PM_RSTC);
  26. PM_RSTC = PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) | PM_RSTC_WRCFG_FULL_RESET;
  27. }
  28. void raspi_watchdog_stop()
  29. {
  30. PM_RSTC = PM_PASSWORD | PM_RSTC_RESET;
  31. }
  32. void raspi_watchdog_clr()
  33. {
  34. bcm_wdt.timeout = 0;
  35. }
  36. void raspi_watchdog_set_timeout(rt_uint32_t timeout_us)
  37. {
  38. bcm_wdt.timeout = timeout_us;
  39. }
  40. rt_uint64_t raspi_watchdog_get_timeout()
  41. {
  42. return bcm_wdt.timeout;
  43. }
  44. rt_uint64_t raspi_watchdog_get_timeleft()
  45. {
  46. rt_uint32_t ret = (PM_WDOG);
  47. return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET);
  48. }
  49. static rt_err_t raspi_wdg_init(rt_watchdog_t *wdt)
  50. {
  51. /* init for 10S */
  52. raspi_watchdog_init(1000000);
  53. raspi_watchdog_start();
  54. raspi_watchdog_stop();
  55. return RT_EOK;
  56. }
  57. static rt_err_t raspi_wdg_control(rt_watchdog_t *wdt, int cmd, void *arg)
  58. {
  59. rt_uint64_t timeout_us = 0;
  60. switch (cmd)
  61. {
  62. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  63. timeout_us = *((rt_uint32_t *)arg) * 1000000;
  64. if (timeout_us >= 0xFFFFFFFF)
  65. {
  66. timeout_us = 0xFFFFFFFF;
  67. }
  68. raspi_watchdog_set_timeout((rt_uint32_t)timeout_us);
  69. break;
  70. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  71. timeout_us = raspi_watchdog_get_timeout();
  72. *((rt_uint32_t *)arg) = timeout_us / 1000000;
  73. break;
  74. case RT_DEVICE_CTRL_WDT_GET_TIMELEFT:
  75. timeout_us = raspi_watchdog_get_timeleft();
  76. *((rt_uint32_t *)arg) = timeout_us / 1000000;
  77. break;
  78. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  79. raspi_watchdog_clr();
  80. break;
  81. case RT_DEVICE_CTRL_WDT_START:
  82. raspi_watchdog_start();
  83. break;
  84. case RT_DEVICE_CTRL_WDT_STOP:
  85. raspi_watchdog_stop();
  86. break;
  87. default:
  88. return RT_EIO;
  89. }
  90. return RT_EOK;
  91. }
  92. static const struct rt_watchdog_ops raspi_wdg_pos =
  93. {
  94. raspi_wdg_init,
  95. raspi_wdg_control,
  96. };
  97. static rt_watchdog_t raspi_wdg;
  98. int rt_hw_wdt_init(void)
  99. {
  100. raspi_wdg.ops = &raspi_wdg_pos;
  101. rt_hw_watchdog_register(&raspi_wdg, "wdg", 0, RT_NULL);
  102. return RT_EOK;
  103. }
  104. INIT_DEVICE_EXPORT(rt_hw_wdt_init);
  105. void reboot(void)
  106. {
  107. unsigned int r;
  108. rt_kprintf("reboot system...\n");
  109. rt_thread_mdelay(100);
  110. r = PM_RSTS;
  111. /* trigger a restart by instructing the GPU to boot from partition 0 */
  112. r &= ~0xfffffaaa;
  113. PM_RSTS |= (PM_PASSWORD | r); /* boot from partition 0 */
  114. PM_WDOG |= (PM_PASSWORD | 0x0A);
  115. PM_RSTC |= (PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET);
  116. while (1) {};
  117. }
  118. MSH_CMD_EXPORT(reboot, reboot system...);
  119. #endif /*BSP_USING_WDT */