drv_wdt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. timeout_us = 0xFFFFFFFF;
  66. raspi_watchdog_set_timeout((rt_uint32_t)timeout_us);
  67. break;
  68. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  69. timeout_us = raspi_watchdog_get_timeout();
  70. *((rt_uint32_t *)arg) = timeout_us / 1000000;
  71. break;
  72. case RT_DEVICE_CTRL_WDT_GET_TIMELEFT:
  73. timeout_us = raspi_watchdog_get_timeleft();
  74. *((rt_uint32_t *)arg) = timeout_us / 1000000;
  75. break;
  76. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  77. raspi_watchdog_clr();
  78. break;
  79. case RT_DEVICE_CTRL_WDT_START:
  80. raspi_watchdog_start();
  81. break;
  82. case RT_DEVICE_CTRL_WDT_STOP:
  83. raspi_watchdog_stop();
  84. break;
  85. default:
  86. return -RT_EIO;
  87. }
  88. return RT_EOK;
  89. }
  90. static const struct rt_watchdog_ops raspi_wdg_pos =
  91. {
  92. raspi_wdg_init,
  93. raspi_wdg_control,
  94. };
  95. static rt_watchdog_t raspi_wdg;
  96. int rt_hw_wdt_init(void)
  97. {
  98. raspi_wdg.ops = &raspi_wdg_pos;
  99. rt_hw_watchdog_register(&raspi_wdg, "wdg", 0, RT_NULL);
  100. return RT_EOK;
  101. }
  102. INIT_DEVICE_EXPORT(rt_hw_wdt_init);
  103. void reboot(void)
  104. {
  105. unsigned int r;
  106. rt_kprintf("reboot system...\n");
  107. rt_thread_mdelay(100);
  108. r = PM_RSTS;
  109. // trigger a restart by instructing the GPU to boot from partition 0
  110. r &= ~0xfffffaaa;
  111. PM_RSTS |= (PM_PASSWORD | r); // boot from partition 0
  112. PM_WDOG |= (PM_PASSWORD | 0x0A);
  113. PM_RSTC |= (PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET);
  114. while (1);
  115. }
  116. MSH_CMD_EXPORT(reboot,reboot system...);
  117. #endif /*BSP_USING_WDT */