wdt-uclass.c 3.4 KB

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