wdt-uclass.c 3.4 KB

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