bcm47xx_wdt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/reboot.h>
  22. #include <linux/types.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/timer.h>
  25. #include <linux/jiffies.h>
  26. #define DRV_NAME "bcm47xx_wdt"
  27. #define WDT_DEFAULT_TIME 30 /* seconds */
  28. #define WDT_SOFTTIMER_MAX 255 /* seconds */
  29. #define WDT_SOFTTIMER_THRESHOLD 60 /* seconds */
  30. static int timeout = WDT_DEFAULT_TIME;
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(timeout, int, 0);
  33. MODULE_PARM_DESC(timeout, "Watchdog time in seconds. (default="
  34. __MODULE_STRING(WDT_DEFAULT_TIME) ")");
  35. module_param(nowayout, bool, 0);
  36. MODULE_PARM_DESC(nowayout,
  37. "Watchdog cannot be stopped once started (default="
  38. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  39. static inline struct bcm47xx_wdt *bcm47xx_wdt_get(struct watchdog_device *wdd)
  40. {
  41. return container_of(wdd, struct bcm47xx_wdt, wdd);
  42. }
  43. static int bcm47xx_wdt_hard_keepalive(struct watchdog_device *wdd)
  44. {
  45. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  46. wdt->timer_set_ms(wdt, wdd->timeout * 1000);
  47. return 0;
  48. }
  49. static int bcm47xx_wdt_hard_start(struct watchdog_device *wdd)
  50. {
  51. return 0;
  52. }
  53. static int bcm47xx_wdt_hard_stop(struct watchdog_device *wdd)
  54. {
  55. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  56. wdt->timer_set(wdt, 0);
  57. return 0;
  58. }
  59. static int bcm47xx_wdt_hard_set_timeout(struct watchdog_device *wdd,
  60. unsigned int new_time)
  61. {
  62. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  63. u32 max_timer = wdt->max_timer_ms;
  64. if (new_time < 1 || new_time > max_timer / 1000) {
  65. pr_warn("timeout value must be 1<=x<=%d, using %d\n",
  66. max_timer / 1000, new_time);
  67. return -EINVAL;
  68. }
  69. wdd->timeout = new_time;
  70. return 0;
  71. }
  72. static struct watchdog_ops bcm47xx_wdt_hard_ops = {
  73. .owner = THIS_MODULE,
  74. .start = bcm47xx_wdt_hard_start,
  75. .stop = bcm47xx_wdt_hard_stop,
  76. .ping = bcm47xx_wdt_hard_keepalive,
  77. .set_timeout = bcm47xx_wdt_hard_set_timeout,
  78. };
  79. static void bcm47xx_wdt_soft_timer_tick(unsigned long data)
  80. {
  81. struct bcm47xx_wdt *wdt = (struct bcm47xx_wdt *)data;
  82. u32 next_tick = min(wdt->wdd.timeout * 1000, wdt->max_timer_ms);
  83. if (!atomic_dec_and_test(&wdt->soft_ticks)) {
  84. wdt->timer_set_ms(wdt, next_tick);
  85. mod_timer(&wdt->soft_timer, jiffies + HZ);
  86. } else {
  87. pr_crit("Watchdog will fire soon!!!\n");
  88. }
  89. }
  90. static int bcm47xx_wdt_soft_keepalive(struct watchdog_device *wdd)
  91. {
  92. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  93. atomic_set(&wdt->soft_ticks, wdd->timeout);
  94. return 0;
  95. }
  96. static int bcm47xx_wdt_soft_start(struct watchdog_device *wdd)
  97. {
  98. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  99. bcm47xx_wdt_soft_keepalive(wdd);
  100. bcm47xx_wdt_soft_timer_tick((unsigned long)wdt);
  101. return 0;
  102. }
  103. static int bcm47xx_wdt_soft_stop(struct watchdog_device *wdd)
  104. {
  105. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  106. del_timer_sync(&wdt->soft_timer);
  107. wdt->timer_set(wdt, 0);
  108. return 0;
  109. }
  110. static int bcm47xx_wdt_soft_set_timeout(struct watchdog_device *wdd,
  111. unsigned int new_time)
  112. {
  113. if (new_time < 1 || new_time > WDT_SOFTTIMER_MAX) {
  114. pr_warn("timeout value must be 1<=x<=%d, using %d\n",
  115. WDT_SOFTTIMER_MAX, new_time);
  116. return -EINVAL;
  117. }
  118. wdd->timeout = new_time;
  119. return 0;
  120. }
  121. static const struct watchdog_info bcm47xx_wdt_info = {
  122. .identity = DRV_NAME,
  123. .options = WDIOF_SETTIMEOUT |
  124. WDIOF_KEEPALIVEPING |
  125. WDIOF_MAGICCLOSE,
  126. };
  127. static int bcm47xx_wdt_notify_sys(struct notifier_block *this,
  128. unsigned long code, void *unused)
  129. {
  130. struct bcm47xx_wdt *wdt;
  131. wdt = container_of(this, struct bcm47xx_wdt, notifier);
  132. if (code == SYS_DOWN || code == SYS_HALT)
  133. wdt->wdd.ops->stop(&wdt->wdd);
  134. return NOTIFY_DONE;
  135. }
  136. static int bcm47xx_wdt_restart(struct notifier_block *this, unsigned long mode,
  137. void *cmd)
  138. {
  139. struct bcm47xx_wdt *wdt;
  140. wdt = container_of(this, struct bcm47xx_wdt, restart_handler);
  141. wdt->timer_set(wdt, 1);
  142. return NOTIFY_DONE;
  143. }
  144. static struct watchdog_ops bcm47xx_wdt_soft_ops = {
  145. .owner = THIS_MODULE,
  146. .start = bcm47xx_wdt_soft_start,
  147. .stop = bcm47xx_wdt_soft_stop,
  148. .ping = bcm47xx_wdt_soft_keepalive,
  149. .set_timeout = bcm47xx_wdt_soft_set_timeout,
  150. };
  151. static int bcm47xx_wdt_probe(struct platform_device *pdev)
  152. {
  153. int ret;
  154. bool soft;
  155. struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
  156. if (!wdt)
  157. return -ENXIO;
  158. soft = wdt->max_timer_ms < WDT_SOFTTIMER_THRESHOLD * 1000;
  159. if (soft) {
  160. wdt->wdd.ops = &bcm47xx_wdt_soft_ops;
  161. setup_timer(&wdt->soft_timer, bcm47xx_wdt_soft_timer_tick,
  162. (long unsigned int)wdt);
  163. } else {
  164. wdt->wdd.ops = &bcm47xx_wdt_hard_ops;
  165. }
  166. wdt->wdd.info = &bcm47xx_wdt_info;
  167. wdt->wdd.timeout = WDT_DEFAULT_TIME;
  168. wdt->wdd.parent = &pdev->dev;
  169. ret = wdt->wdd.ops->set_timeout(&wdt->wdd, timeout);
  170. if (ret)
  171. goto err_timer;
  172. watchdog_set_nowayout(&wdt->wdd, nowayout);
  173. wdt->notifier.notifier_call = &bcm47xx_wdt_notify_sys;
  174. ret = register_reboot_notifier(&wdt->notifier);
  175. if (ret)
  176. goto err_timer;
  177. wdt->restart_handler.notifier_call = &bcm47xx_wdt_restart;
  178. wdt->restart_handler.priority = 64;
  179. ret = register_restart_handler(&wdt->restart_handler);
  180. if (ret)
  181. goto err_notifier;
  182. ret = watchdog_register_device(&wdt->wdd);
  183. if (ret)
  184. goto err_handler;
  185. dev_info(&pdev->dev, "BCM47xx Watchdog Timer enabled (%d seconds%s%s)\n",
  186. timeout, nowayout ? ", nowayout" : "",
  187. soft ? ", Software Timer" : "");
  188. return 0;
  189. err_handler:
  190. unregister_restart_handler(&wdt->restart_handler);
  191. err_notifier:
  192. unregister_reboot_notifier(&wdt->notifier);
  193. err_timer:
  194. if (soft)
  195. del_timer_sync(&wdt->soft_timer);
  196. return ret;
  197. }
  198. static int bcm47xx_wdt_remove(struct platform_device *pdev)
  199. {
  200. struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
  201. if (!wdt)
  202. return -ENXIO;
  203. watchdog_unregister_device(&wdt->wdd);
  204. unregister_reboot_notifier(&wdt->notifier);
  205. return 0;
  206. }
  207. static struct platform_driver bcm47xx_wdt_driver = {
  208. .driver = {
  209. .name = "bcm47xx-wdt",
  210. },
  211. .probe = bcm47xx_wdt_probe,
  212. .remove = bcm47xx_wdt_remove,
  213. };
  214. module_platform_driver(bcm47xx_wdt_driver);
  215. MODULE_AUTHOR("Aleksandar Radovanovic");
  216. MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
  217. MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
  218. MODULE_LICENSE("GPL");