bcm47xx_wdt.c 6.0 KB

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