bcm47xx_wdt.c 6.2 KB

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