wdt-uclass.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <dm.h>
  8. #include <errno.h>
  9. #include <hang.h>
  10. #include <log.h>
  11. #include <time.h>
  12. #include <wdt.h>
  13. #include <asm/global_data.h>
  14. #include <dm/device-internal.h>
  15. #include <dm/lists.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
  18. /*
  19. * Reset every 1000ms, or however often is required as indicated by a
  20. * hw_margin_ms property.
  21. */
  22. static ulong reset_period = 1000;
  23. int initr_watchdog(void)
  24. {
  25. u32 timeout = WATCHDOG_TIMEOUT_SECS;
  26. int ret;
  27. /*
  28. * Init watchdog: This will call the probe function of the
  29. * watchdog driver, enabling the use of the device
  30. */
  31. if (uclass_get_device_by_seq(UCLASS_WDT, 0,
  32. (struct udevice **)&gd->watchdog_dev)) {
  33. debug("WDT: Not found by seq!\n");
  34. if (uclass_get_device(UCLASS_WDT, 0,
  35. (struct udevice **)&gd->watchdog_dev)) {
  36. printf("WDT: Not found!\n");
  37. return 0;
  38. }
  39. }
  40. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  41. timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec",
  42. WATCHDOG_TIMEOUT_SECS);
  43. reset_period = dev_read_u32_default(gd->watchdog_dev,
  44. "hw_margin_ms",
  45. 4 * reset_period) / 4;
  46. }
  47. if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) {
  48. printf("WDT: Not starting\n");
  49. return 0;
  50. }
  51. ret = wdt_start(gd->watchdog_dev, timeout * 1000, 0);
  52. if (ret != 0) {
  53. printf("WDT: Failed to start\n");
  54. return 0;
  55. }
  56. printf("WDT: Started with%s servicing (%ds timeout)\n",
  57. IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout);
  58. return 0;
  59. }
  60. int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
  61. {
  62. const struct wdt_ops *ops = device_get_ops(dev);
  63. int ret;
  64. if (!ops->start)
  65. return -ENOSYS;
  66. ret = ops->start(dev, timeout_ms, flags);
  67. if (ret == 0)
  68. gd->flags |= GD_FLG_WDT_READY;
  69. return ret;
  70. }
  71. int wdt_stop(struct udevice *dev)
  72. {
  73. const struct wdt_ops *ops = device_get_ops(dev);
  74. int ret;
  75. if (!ops->stop)
  76. return -ENOSYS;
  77. ret = ops->stop(dev);
  78. if (ret == 0)
  79. gd->flags &= ~GD_FLG_WDT_READY;
  80. return ret;
  81. }
  82. int wdt_reset(struct udevice *dev)
  83. {
  84. const struct wdt_ops *ops = device_get_ops(dev);
  85. if (!ops->reset)
  86. return -ENOSYS;
  87. return ops->reset(dev);
  88. }
  89. int wdt_expire_now(struct udevice *dev, ulong flags)
  90. {
  91. int ret = 0;
  92. const struct wdt_ops *ops;
  93. debug("WDT Resetting: %lu\n", flags);
  94. ops = device_get_ops(dev);
  95. if (ops->expire_now) {
  96. return ops->expire_now(dev, flags);
  97. } else {
  98. if (!ops->start)
  99. return -ENOSYS;
  100. ret = ops->start(dev, 1, flags);
  101. if (ret < 0)
  102. return ret;
  103. hang();
  104. }
  105. return ret;
  106. }
  107. #if defined(CONFIG_WATCHDOG)
  108. /*
  109. * Called by macro WATCHDOG_RESET. This function be called *very* early,
  110. * so we need to make sure, that the watchdog driver is ready before using
  111. * it in this function.
  112. */
  113. void watchdog_reset(void)
  114. {
  115. static ulong next_reset;
  116. ulong now;
  117. /* Exit if GD is not ready or watchdog is not initialized yet */
  118. if (!gd || !(gd->flags & GD_FLG_WDT_READY))
  119. return;
  120. /* Do not reset the watchdog too often */
  121. now = get_timer(0);
  122. if (time_after_eq(now, next_reset)) {
  123. next_reset = now + reset_period;
  124. wdt_reset(gd->watchdog_dev);
  125. }
  126. }
  127. #endif
  128. static int wdt_post_bind(struct udevice *dev)
  129. {
  130. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  131. struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
  132. static int reloc_done;
  133. if (!reloc_done) {
  134. if (ops->start)
  135. ops->start += gd->reloc_off;
  136. if (ops->stop)
  137. ops->stop += gd->reloc_off;
  138. if (ops->reset)
  139. ops->reset += gd->reloc_off;
  140. if (ops->expire_now)
  141. ops->expire_now += gd->reloc_off;
  142. reloc_done++;
  143. }
  144. #endif
  145. return 0;
  146. }
  147. UCLASS_DRIVER(wdt) = {
  148. .id = UCLASS_WDT,
  149. .name = "watchdog",
  150. .flags = DM_UC_FLAG_SEQ_ALIAS,
  151. .post_bind = wdt_post_bind,
  152. };