wdt-uclass.c 3.2 KB

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