wdt-uclass.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2017 Google, Inc
  4. */
  5. #define LOG_CATEGORY UCLASS_WDT
  6. #include <common.h>
  7. #include <cyclic.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <hang.h>
  11. #include <log.h>
  12. #include <sysreset.h>
  13. #include <time.h>
  14. #include <wdt.h>
  15. #include <asm/global_data.h>
  16. #include <dm/device-internal.h>
  17. #include <dm/lists.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
  20. struct wdt_priv {
  21. /* Timeout, in seconds, to configure this device to. */
  22. u32 timeout;
  23. /*
  24. * Time, in milliseconds, between calling the device's ->reset()
  25. * method from watchdog_reset().
  26. */
  27. ulong reset_period;
  28. /*
  29. * Next time (as returned by get_timer(0)) to call
  30. * ->reset().
  31. */
  32. ulong next_reset;
  33. /* Whether watchdog_start() has been called on the device. */
  34. bool running;
  35. /* autostart */
  36. bool autostart;
  37. struct cyclic_info *cyclic;
  38. };
  39. static void wdt_cyclic(void *ctx)
  40. {
  41. struct udevice *dev = ctx;
  42. struct wdt_priv *priv;
  43. if (!device_active(dev))
  44. return;
  45. priv = dev_get_uclass_priv(dev);
  46. if (!priv->running)
  47. return;
  48. wdt_reset(dev);
  49. }
  50. static void init_watchdog_dev(struct udevice *dev)
  51. {
  52. struct wdt_priv *priv;
  53. int ret;
  54. priv = dev_get_uclass_priv(dev);
  55. if (IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO)) {
  56. ret = sysreset_register_wdt(dev);
  57. if (ret)
  58. printf("WDT: Failed to register %s for sysreset\n",
  59. dev->name);
  60. }
  61. if (!priv->autostart) {
  62. printf("WDT: Not starting %s\n", dev->name);
  63. return;
  64. }
  65. ret = wdt_start(dev, priv->timeout * 1000, 0);
  66. if (ret != 0) {
  67. printf("WDT: Failed to start %s\n", dev->name);
  68. return;
  69. }
  70. }
  71. int initr_watchdog(void)
  72. {
  73. struct udevice *dev;
  74. struct uclass *uc;
  75. int ret;
  76. ret = uclass_get(UCLASS_WDT, &uc);
  77. if (ret) {
  78. log_debug("Error getting UCLASS_WDT: %d\n", ret);
  79. return 0;
  80. }
  81. uclass_foreach_dev(dev, uc) {
  82. ret = device_probe(dev);
  83. if (ret) {
  84. log_debug("Error probing %s: %d\n", dev->name, ret);
  85. continue;
  86. }
  87. init_watchdog_dev(dev);
  88. }
  89. return 0;
  90. }
  91. int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
  92. {
  93. const struct wdt_ops *ops = device_get_ops(dev);
  94. int ret;
  95. if (!ops->start)
  96. return -ENOSYS;
  97. ret = ops->start(dev, timeout_ms, flags);
  98. if (ret == 0) {
  99. struct wdt_priv *priv = dev_get_uclass_priv(dev);
  100. char str[16];
  101. priv->running = true;
  102. memset(str, 0, 16);
  103. if (IS_ENABLED(CONFIG_WATCHDOG)) {
  104. /* Register the watchdog driver as a cyclic function */
  105. priv->cyclic = cyclic_register(wdt_cyclic,
  106. priv->reset_period * 1000,
  107. dev->name, dev);
  108. if (!priv->cyclic) {
  109. printf("cyclic_register for %s failed\n",
  110. dev->name);
  111. return -ENODEV;
  112. } else {
  113. snprintf(str, 16, "every %ldms",
  114. priv->reset_period);
  115. }
  116. }
  117. printf("WDT: Started %s with%s servicing %s (%ds timeout)\n",
  118. dev->name, IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out",
  119. str, priv->timeout);
  120. }
  121. return ret;
  122. }
  123. int wdt_stop(struct udevice *dev)
  124. {
  125. const struct wdt_ops *ops = device_get_ops(dev);
  126. int ret;
  127. if (!ops->stop)
  128. return -ENOSYS;
  129. ret = ops->stop(dev);
  130. if (ret == 0) {
  131. struct wdt_priv *priv = dev_get_uclass_priv(dev);
  132. priv->running = false;
  133. }
  134. return ret;
  135. }
  136. int wdt_stop_all(void)
  137. {
  138. struct wdt_priv *priv;
  139. struct udevice *dev;
  140. struct uclass *uc;
  141. int ret, err;
  142. ret = uclass_get(UCLASS_WDT, &uc);
  143. if (ret)
  144. return ret;
  145. uclass_foreach_dev(dev, uc) {
  146. if (!device_active(dev))
  147. continue;
  148. priv = dev_get_uclass_priv(dev);
  149. if (!priv->running)
  150. continue;
  151. err = wdt_stop(dev);
  152. if (!ret)
  153. ret = err;
  154. }
  155. return ret;
  156. }
  157. int wdt_reset(struct udevice *dev)
  158. {
  159. const struct wdt_ops *ops = device_get_ops(dev);
  160. if (!ops->reset)
  161. return -ENOSYS;
  162. return ops->reset(dev);
  163. }
  164. int wdt_expire_now(struct udevice *dev, ulong flags)
  165. {
  166. int ret = 0;
  167. const struct wdt_ops *ops;
  168. debug("WDT Resetting: %lu\n", flags);
  169. ops = device_get_ops(dev);
  170. if (ops->expire_now) {
  171. return ops->expire_now(dev, flags);
  172. } else {
  173. ret = wdt_start(dev, 1, flags);
  174. if (ret < 0)
  175. return ret;
  176. hang();
  177. }
  178. return ret;
  179. }
  180. #if defined(CONFIG_WATCHDOG)
  181. /*
  182. * Called by macro WATCHDOG_RESET. This function be called *very* early,
  183. * so we need to make sure, that the watchdog driver is ready before using
  184. * it in this function.
  185. */
  186. void watchdog_reset(void)
  187. {
  188. /*
  189. * Empty function for now. The actual WDT handling is now done in
  190. * the cyclic function instead.
  191. */
  192. }
  193. #endif
  194. static int wdt_post_bind(struct udevice *dev)
  195. {
  196. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  197. struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
  198. static int reloc_done;
  199. if (!reloc_done) {
  200. if (ops->start)
  201. ops->start += gd->reloc_off;
  202. if (ops->stop)
  203. ops->stop += gd->reloc_off;
  204. if (ops->reset)
  205. ops->reset += gd->reloc_off;
  206. if (ops->expire_now)
  207. ops->expire_now += gd->reloc_off;
  208. reloc_done++;
  209. }
  210. #endif
  211. return 0;
  212. }
  213. static int wdt_pre_probe(struct udevice *dev)
  214. {
  215. u32 timeout = WATCHDOG_TIMEOUT_SECS;
  216. /*
  217. * Reset every 1000ms, or however often is required as
  218. * indicated by a hw_margin_ms property.
  219. */
  220. ulong reset_period = 1000;
  221. bool autostart = IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART);
  222. struct wdt_priv *priv;
  223. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  224. timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
  225. reset_period = dev_read_u32_default(dev, "hw_margin_ms",
  226. 4 * reset_period) / 4;
  227. if (dev_read_bool(dev, "u-boot,noautostart"))
  228. autostart = false;
  229. else if (dev_read_bool(dev, "u-boot,autostart"))
  230. autostart = true;
  231. }
  232. priv = dev_get_uclass_priv(dev);
  233. priv->timeout = timeout;
  234. priv->reset_period = reset_period;
  235. priv->autostart = autostart;
  236. /*
  237. * Pretend this device was last reset "long" ago so the first
  238. * watchdog_reset will actually call its ->reset method.
  239. */
  240. priv->next_reset = get_timer(0);
  241. return 0;
  242. }
  243. UCLASS_DRIVER(wdt) = {
  244. .id = UCLASS_WDT,
  245. .name = "watchdog",
  246. .flags = DM_UC_FLAG_SEQ_ALIAS,
  247. .post_bind = wdt_post_bind,
  248. .pre_probe = wdt_pre_probe,
  249. .per_device_auto = sizeof(struct wdt_priv),
  250. };