bcm47xx_wdt.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Watchdog driver for Broadcom BCM47XX
  4. *
  5. * Copyright (C) 2008 Aleksandar Radovanovic <biblbroks@sezampro.rs>
  6. * Copyright (C) 2009 Matthieu CASTET <castet.matthieu@free.fr>
  7. * Copyright (C) 2012-2013 Hauke Mehrtens <hauke@hauke-m.de>
  8. *
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/bcm47xx_wdt.h>
  12. #include <linux/bitops.h>
  13. #include <linux/errno.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/types.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/timer.h>
  21. #include <linux/jiffies.h>
  22. #define DRV_NAME "bcm47xx_wdt"
  23. #define WDT_DEFAULT_TIME 30 /* seconds */
  24. #define WDT_SOFTTIMER_MAX 255 /* seconds */
  25. #define WDT_SOFTTIMER_THRESHOLD 60 /* seconds */
  26. static int timeout = WDT_DEFAULT_TIME;
  27. static bool nowayout = WATCHDOG_NOWAYOUT;
  28. module_param(timeout, int, 0);
  29. MODULE_PARM_DESC(timeout, "Watchdog time in seconds. (default="
  30. __MODULE_STRING(WDT_DEFAULT_TIME) ")");
  31. module_param(nowayout, bool, 0);
  32. MODULE_PARM_DESC(nowayout,
  33. "Watchdog cannot be stopped once started (default="
  34. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  35. static inline struct bcm47xx_wdt *bcm47xx_wdt_get(struct watchdog_device *wdd)
  36. {
  37. return container_of(wdd, struct bcm47xx_wdt, wdd);
  38. }
  39. static int bcm47xx_wdt_hard_keepalive(struct watchdog_device *wdd)
  40. {
  41. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  42. wdt->timer_set_ms(wdt, wdd->timeout * 1000);
  43. return 0;
  44. }
  45. static int bcm47xx_wdt_hard_start(struct watchdog_device *wdd)
  46. {
  47. return 0;
  48. }
  49. static int bcm47xx_wdt_hard_stop(struct watchdog_device *wdd)
  50. {
  51. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  52. wdt->timer_set(wdt, 0);
  53. return 0;
  54. }
  55. static int bcm47xx_wdt_hard_set_timeout(struct watchdog_device *wdd,
  56. unsigned int new_time)
  57. {
  58. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  59. u32 max_timer = wdt->max_timer_ms;
  60. if (new_time < 1 || new_time > max_timer / 1000) {
  61. pr_warn("timeout value must be 1<=x<=%d, using %d\n",
  62. max_timer / 1000, new_time);
  63. return -EINVAL;
  64. }
  65. wdd->timeout = new_time;
  66. return 0;
  67. }
  68. static int bcm47xx_wdt_restart(struct watchdog_device *wdd,
  69. unsigned long action, void *data)
  70. {
  71. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  72. wdt->timer_set(wdt, 1);
  73. return 0;
  74. }
  75. static const struct watchdog_ops bcm47xx_wdt_hard_ops = {
  76. .owner = THIS_MODULE,
  77. .start = bcm47xx_wdt_hard_start,
  78. .stop = bcm47xx_wdt_hard_stop,
  79. .ping = bcm47xx_wdt_hard_keepalive,
  80. .set_timeout = bcm47xx_wdt_hard_set_timeout,
  81. .restart = bcm47xx_wdt_restart,
  82. };
  83. static void bcm47xx_wdt_soft_timer_tick(struct timer_list *t)
  84. {
  85. struct bcm47xx_wdt *wdt = from_timer(wdt, t, soft_timer);
  86. u32 next_tick = min(wdt->wdd.timeout * 1000, wdt->max_timer_ms);
  87. if (!atomic_dec_and_test(&wdt->soft_ticks)) {
  88. wdt->timer_set_ms(wdt, next_tick);
  89. mod_timer(&wdt->soft_timer, jiffies + HZ);
  90. } else {
  91. pr_crit("Watchdog will fire soon!!!\n");
  92. }
  93. }
  94. static int bcm47xx_wdt_soft_keepalive(struct watchdog_device *wdd)
  95. {
  96. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  97. atomic_set(&wdt->soft_ticks, wdd->timeout);
  98. return 0;
  99. }
  100. static int bcm47xx_wdt_soft_start(struct watchdog_device *wdd)
  101. {
  102. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  103. bcm47xx_wdt_soft_keepalive(wdd);
  104. bcm47xx_wdt_soft_timer_tick(&wdt->soft_timer);
  105. return 0;
  106. }
  107. static int bcm47xx_wdt_soft_stop(struct watchdog_device *wdd)
  108. {
  109. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  110. del_timer_sync(&wdt->soft_timer);
  111. wdt->timer_set(wdt, 0);
  112. return 0;
  113. }
  114. static int bcm47xx_wdt_soft_set_timeout(struct watchdog_device *wdd,
  115. unsigned int new_time)
  116. {
  117. if (new_time < 1 || new_time > WDT_SOFTTIMER_MAX) {
  118. pr_warn("timeout value must be 1<=x<=%d, using %d\n",
  119. WDT_SOFTTIMER_MAX, new_time);
  120. return -EINVAL;
  121. }
  122. wdd->timeout = new_time;
  123. return 0;
  124. }
  125. static const struct watchdog_info bcm47xx_wdt_info = {
  126. .identity = DRV_NAME,
  127. .options = WDIOF_SETTIMEOUT |
  128. WDIOF_KEEPALIVEPING |
  129. WDIOF_MAGICCLOSE,
  130. };
  131. static const struct watchdog_ops bcm47xx_wdt_soft_ops = {
  132. .owner = THIS_MODULE,
  133. .start = bcm47xx_wdt_soft_start,
  134. .stop = bcm47xx_wdt_soft_stop,
  135. .ping = bcm47xx_wdt_soft_keepalive,
  136. .set_timeout = bcm47xx_wdt_soft_set_timeout,
  137. .restart = bcm47xx_wdt_restart,
  138. };
  139. static int bcm47xx_wdt_probe(struct platform_device *pdev)
  140. {
  141. int ret;
  142. bool soft;
  143. struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
  144. if (!wdt)
  145. return -ENXIO;
  146. soft = wdt->max_timer_ms < WDT_SOFTTIMER_THRESHOLD * 1000;
  147. if (soft) {
  148. wdt->wdd.ops = &bcm47xx_wdt_soft_ops;
  149. timer_setup(&wdt->soft_timer, bcm47xx_wdt_soft_timer_tick, 0);
  150. } else {
  151. wdt->wdd.ops = &bcm47xx_wdt_hard_ops;
  152. }
  153. wdt->wdd.info = &bcm47xx_wdt_info;
  154. wdt->wdd.timeout = WDT_DEFAULT_TIME;
  155. wdt->wdd.parent = &pdev->dev;
  156. ret = wdt->wdd.ops->set_timeout(&wdt->wdd, timeout);
  157. if (ret)
  158. goto err_timer;
  159. watchdog_set_nowayout(&wdt->wdd, nowayout);
  160. watchdog_set_restart_priority(&wdt->wdd, 64);
  161. watchdog_stop_on_reboot(&wdt->wdd);
  162. ret = watchdog_register_device(&wdt->wdd);
  163. if (ret)
  164. goto err_timer;
  165. dev_info(&pdev->dev, "BCM47xx Watchdog Timer enabled (%d seconds%s%s)\n",
  166. timeout, nowayout ? ", nowayout" : "",
  167. soft ? ", Software Timer" : "");
  168. return 0;
  169. err_timer:
  170. if (soft)
  171. del_timer_sync(&wdt->soft_timer);
  172. return ret;
  173. }
  174. static int bcm47xx_wdt_remove(struct platform_device *pdev)
  175. {
  176. struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
  177. watchdog_unregister_device(&wdt->wdd);
  178. return 0;
  179. }
  180. static struct platform_driver bcm47xx_wdt_driver = {
  181. .driver = {
  182. .name = "bcm47xx-wdt",
  183. },
  184. .probe = bcm47xx_wdt_probe,
  185. .remove = bcm47xx_wdt_remove,
  186. };
  187. module_platform_driver(bcm47xx_wdt_driver);
  188. MODULE_AUTHOR("Aleksandar Radovanovic");
  189. MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
  190. MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
  191. MODULE_LICENSE("GPL");