wdt.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright 2017 Google, Inc
  4. */
  5. #ifndef _WDT_H_
  6. #define _WDT_H_
  7. #include <dm.h>
  8. #include <dm/read.h>
  9. /*
  10. * Implement a simple watchdog uclass. Watchdog is basically a timer that
  11. * is used to detect or recover from malfunction. During normal operation
  12. * the watchdog would be regularly reset to prevent it from timing out.
  13. * If, due to a hardware fault or program error, the computer fails to reset
  14. * the watchdog, the timer will elapse and generate a timeout signal.
  15. * The timeout signal is used to initiate corrective action or actions,
  16. * which typically include placing the system in a safe, known state.
  17. */
  18. /*
  19. * Start the timer
  20. *
  21. * @dev: WDT Device
  22. * @timeout_ms: Number of ticks (milliseconds) before timer expires
  23. * @flags: Driver specific flags. This might be used to specify
  24. * which action needs to be executed when the timer expires
  25. * @return: 0 if OK, -ve on error
  26. */
  27. int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags);
  28. /*
  29. * Stop the timer, thus disabling the Watchdog. Use wdt_start to start it again.
  30. *
  31. * @dev: WDT Device
  32. * @return: 0 if OK, -ve on error
  33. */
  34. int wdt_stop(struct udevice *dev);
  35. /*
  36. * Reset the timer, typically restoring the counter to
  37. * the value configured by start()
  38. *
  39. * @dev: WDT Device
  40. * @return: 0 if OK, -ve on error
  41. */
  42. int wdt_reset(struct udevice *dev);
  43. /*
  44. * Expire the timer, thus executing its action immediately.
  45. * This is typically used to reset the board or peripherals.
  46. *
  47. * @dev: WDT Device
  48. * @flags: Driver specific flags
  49. * @return 0 if OK -ve on error. If wdt action is system reset,
  50. * this function may never return.
  51. */
  52. int wdt_expire_now(struct udevice *dev, ulong flags);
  53. /*
  54. * struct wdt_ops - Driver model wdt operations
  55. *
  56. * The uclass interface is implemented by all wdt devices which use
  57. * driver model.
  58. */
  59. struct wdt_ops {
  60. /*
  61. * Start the timer
  62. *
  63. * @dev: WDT Device
  64. * @timeout_ms: Number of ticks (milliseconds) before the timer expires
  65. * @flags: Driver specific flags. This might be used to specify
  66. * which action needs to be executed when the timer expires
  67. * @return: 0 if OK, -ve on error
  68. */
  69. int (*start)(struct udevice *dev, u64 timeout_ms, ulong flags);
  70. /*
  71. * Stop the timer
  72. *
  73. * @dev: WDT Device
  74. * @return: 0 if OK, -ve on error
  75. */
  76. int (*stop)(struct udevice *dev);
  77. /*
  78. * Reset the timer, typically restoring the counter to
  79. * the value configured by start()
  80. *
  81. * @dev: WDT Device
  82. * @return: 0 if OK, -ve on error
  83. */
  84. int (*reset)(struct udevice *dev);
  85. /*
  86. * Expire the timer, thus executing the action immediately (optional)
  87. *
  88. * If this function is not provided, a default implementation
  89. * will be used, which sets the counter to 1
  90. * and waits forever. This is good enough for system level
  91. * reset, where the function is not expected to return, but might not be
  92. * good enough for other use cases.
  93. *
  94. * @dev: WDT Device
  95. * @flags: Driver specific flags
  96. * @return 0 if OK -ve on error. May not return.
  97. */
  98. int (*expire_now)(struct udevice *dev, ulong flags);
  99. };
  100. #if CONFIG_IS_ENABLED(WDT)
  101. #ifndef CONFIG_WATCHDOG_TIMEOUT_MSECS
  102. #define CONFIG_WATCHDOG_TIMEOUT_MSECS (60 * 1000)
  103. #endif
  104. #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
  105. static inline int initr_watchdog(void)
  106. {
  107. u32 timeout = WATCHDOG_TIMEOUT_SECS;
  108. /*
  109. * Init watchdog: This will call the probe function of the
  110. * watchdog driver, enabling the use of the device
  111. */
  112. if (uclass_get_device_by_seq(UCLASS_WDT, 0,
  113. (struct udevice **)&gd->watchdog_dev)) {
  114. debug("WDT: Not found by seq!\n");
  115. if (uclass_get_device(UCLASS_WDT, 0,
  116. (struct udevice **)&gd->watchdog_dev)) {
  117. printf("WDT: Not found!\n");
  118. return 0;
  119. }
  120. }
  121. if (CONFIG_IS_ENABLED(OF_CONTROL)) {
  122. timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec",
  123. WATCHDOG_TIMEOUT_SECS);
  124. }
  125. wdt_start(gd->watchdog_dev, timeout * 1000, 0);
  126. gd->flags |= GD_FLG_WDT_READY;
  127. printf("WDT: Started with%s servicing (%ds timeout)\n",
  128. IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout);
  129. return 0;
  130. }
  131. #endif
  132. #endif /* _WDT_H_ */