bcm47xx_wdt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/bitops.h>
  14. #include <linux/errno.h>
  15. #include <linux/fs.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/reboot.h>
  22. #include <linux/types.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/timer.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/ssb/ssb_embedded.h>
  28. #include <asm/mach-bcm47xx/bcm47xx.h>
  29. #define DRV_NAME "bcm47xx_wdt"
  30. #define WDT_DEFAULT_TIME 30 /* seconds */
  31. #define WDT_MAX_TIME 255 /* seconds */
  32. static int wdt_time = WDT_DEFAULT_TIME;
  33. static bool nowayout = WATCHDOG_NOWAYOUT;
  34. module_param(wdt_time, int, 0);
  35. MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
  36. __MODULE_STRING(WDT_DEFAULT_TIME) ")");
  37. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  38. module_param(nowayout, bool, 0);
  39. MODULE_PARM_DESC(nowayout,
  40. "Watchdog cannot be stopped once started (default="
  41. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  42. #endif
  43. static unsigned long bcm47xx_wdt_busy;
  44. static char expect_release;
  45. static struct timer_list wdt_timer;
  46. static atomic_t ticks;
  47. static inline void bcm47xx_wdt_hw_start(void)
  48. {
  49. /* this is 2,5s on 100Mhz clock and 2s on 133 Mhz */
  50. switch (bcm47xx_bus_type) {
  51. #ifdef CONFIG_BCM47XX_SSB
  52. case BCM47XX_BUS_TYPE_SSB:
  53. ssb_watchdog_timer_set(&bcm47xx_bus.ssb, 0xfffffff);
  54. break;
  55. #endif
  56. #ifdef CONFIG_BCM47XX_BCMA
  57. case BCM47XX_BUS_TYPE_BCMA:
  58. bcma_chipco_watchdog_timer_set(&bcm47xx_bus.bcma.bus.drv_cc,
  59. 0xfffffff);
  60. break;
  61. #endif
  62. }
  63. }
  64. static inline int bcm47xx_wdt_hw_stop(void)
  65. {
  66. switch (bcm47xx_bus_type) {
  67. #ifdef CONFIG_BCM47XX_SSB
  68. case BCM47XX_BUS_TYPE_SSB:
  69. return ssb_watchdog_timer_set(&bcm47xx_bus.ssb, 0);
  70. #endif
  71. #ifdef CONFIG_BCM47XX_BCMA
  72. case BCM47XX_BUS_TYPE_BCMA:
  73. bcma_chipco_watchdog_timer_set(&bcm47xx_bus.bcma.bus.drv_cc, 0);
  74. return 0;
  75. #endif
  76. }
  77. return -EINVAL;
  78. }
  79. static void bcm47xx_timer_tick(unsigned long unused)
  80. {
  81. if (!atomic_dec_and_test(&ticks)) {
  82. bcm47xx_wdt_hw_start();
  83. mod_timer(&wdt_timer, jiffies + HZ);
  84. } else {
  85. pr_crit("Watchdog will fire soon!!!\n");
  86. }
  87. }
  88. static inline void bcm47xx_wdt_pet(void)
  89. {
  90. atomic_set(&ticks, wdt_time);
  91. }
  92. static void bcm47xx_wdt_start(void)
  93. {
  94. bcm47xx_wdt_pet();
  95. bcm47xx_timer_tick(0);
  96. }
  97. static void bcm47xx_wdt_pause(void)
  98. {
  99. del_timer_sync(&wdt_timer);
  100. bcm47xx_wdt_hw_stop();
  101. }
  102. static void bcm47xx_wdt_stop(void)
  103. {
  104. bcm47xx_wdt_pause();
  105. }
  106. static int bcm47xx_wdt_settimeout(int new_time)
  107. {
  108. if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
  109. return -EINVAL;
  110. wdt_time = new_time;
  111. return 0;
  112. }
  113. static int bcm47xx_wdt_open(struct inode *inode, struct file *file)
  114. {
  115. if (test_and_set_bit(0, &bcm47xx_wdt_busy))
  116. return -EBUSY;
  117. bcm47xx_wdt_start();
  118. return nonseekable_open(inode, file);
  119. }
  120. static int bcm47xx_wdt_release(struct inode *inode, struct file *file)
  121. {
  122. if (expect_release == 42) {
  123. bcm47xx_wdt_stop();
  124. } else {
  125. pr_crit("Unexpected close, not stopping watchdog!\n");
  126. bcm47xx_wdt_start();
  127. }
  128. clear_bit(0, &bcm47xx_wdt_busy);
  129. expect_release = 0;
  130. return 0;
  131. }
  132. static ssize_t bcm47xx_wdt_write(struct file *file, const char __user *data,
  133. size_t len, loff_t *ppos)
  134. {
  135. if (len) {
  136. if (!nowayout) {
  137. size_t i;
  138. expect_release = 0;
  139. for (i = 0; i != len; i++) {
  140. char c;
  141. if (get_user(c, data + i))
  142. return -EFAULT;
  143. if (c == 'V')
  144. expect_release = 42;
  145. }
  146. }
  147. bcm47xx_wdt_pet();
  148. }
  149. return len;
  150. }
  151. static const struct watchdog_info bcm47xx_wdt_info = {
  152. .identity = DRV_NAME,
  153. .options = WDIOF_SETTIMEOUT |
  154. WDIOF_KEEPALIVEPING |
  155. WDIOF_MAGICCLOSE,
  156. };
  157. static long bcm47xx_wdt_ioctl(struct file *file,
  158. unsigned int cmd, unsigned long arg)
  159. {
  160. void __user *argp = (void __user *)arg;
  161. int __user *p = argp;
  162. int new_value, retval = -EINVAL;
  163. switch (cmd) {
  164. case WDIOC_GETSUPPORT:
  165. return copy_to_user(argp, &bcm47xx_wdt_info,
  166. sizeof(bcm47xx_wdt_info)) ? -EFAULT : 0;
  167. case WDIOC_GETSTATUS:
  168. case WDIOC_GETBOOTSTATUS:
  169. return put_user(0, p);
  170. case WDIOC_SETOPTIONS:
  171. if (get_user(new_value, p))
  172. return -EFAULT;
  173. if (new_value & WDIOS_DISABLECARD) {
  174. bcm47xx_wdt_stop();
  175. retval = 0;
  176. }
  177. if (new_value & WDIOS_ENABLECARD) {
  178. bcm47xx_wdt_start();
  179. retval = 0;
  180. }
  181. return retval;
  182. case WDIOC_KEEPALIVE:
  183. bcm47xx_wdt_pet();
  184. return 0;
  185. case WDIOC_SETTIMEOUT:
  186. if (get_user(new_value, p))
  187. return -EFAULT;
  188. if (bcm47xx_wdt_settimeout(new_value))
  189. return -EINVAL;
  190. bcm47xx_wdt_pet();
  191. case WDIOC_GETTIMEOUT:
  192. return put_user(wdt_time, p);
  193. default:
  194. return -ENOTTY;
  195. }
  196. }
  197. static int bcm47xx_wdt_notify_sys(struct notifier_block *this,
  198. unsigned long code, void *unused)
  199. {
  200. if (code == SYS_DOWN || code == SYS_HALT)
  201. bcm47xx_wdt_stop();
  202. return NOTIFY_DONE;
  203. }
  204. static const struct file_operations bcm47xx_wdt_fops = {
  205. .owner = THIS_MODULE,
  206. .llseek = no_llseek,
  207. .unlocked_ioctl = bcm47xx_wdt_ioctl,
  208. .open = bcm47xx_wdt_open,
  209. .release = bcm47xx_wdt_release,
  210. .write = bcm47xx_wdt_write,
  211. };
  212. static struct miscdevice bcm47xx_wdt_miscdev = {
  213. .minor = WATCHDOG_MINOR,
  214. .name = "watchdog",
  215. .fops = &bcm47xx_wdt_fops,
  216. };
  217. static struct notifier_block bcm47xx_wdt_notifier = {
  218. .notifier_call = bcm47xx_wdt_notify_sys,
  219. };
  220. static int __init bcm47xx_wdt_init(void)
  221. {
  222. int ret;
  223. if (bcm47xx_wdt_hw_stop() < 0)
  224. return -ENODEV;
  225. setup_timer(&wdt_timer, bcm47xx_timer_tick, 0L);
  226. if (bcm47xx_wdt_settimeout(wdt_time)) {
  227. bcm47xx_wdt_settimeout(WDT_DEFAULT_TIME);
  228. pr_info("wdt_time value must be 0 < wdt_time < %d, using %d\n",
  229. (WDT_MAX_TIME + 1), wdt_time);
  230. }
  231. ret = register_reboot_notifier(&bcm47xx_wdt_notifier);
  232. if (ret)
  233. return ret;
  234. ret = misc_register(&bcm47xx_wdt_miscdev);
  235. if (ret) {
  236. unregister_reboot_notifier(&bcm47xx_wdt_notifier);
  237. return ret;
  238. }
  239. pr_info("BCM47xx Watchdog Timer enabled (%d seconds%s)\n",
  240. wdt_time, nowayout ? ", nowayout" : "");
  241. return 0;
  242. }
  243. static void __exit bcm47xx_wdt_exit(void)
  244. {
  245. if (!nowayout)
  246. bcm47xx_wdt_stop();
  247. misc_deregister(&bcm47xx_wdt_miscdev);
  248. unregister_reboot_notifier(&bcm47xx_wdt_notifier);
  249. }
  250. module_init(bcm47xx_wdt_init);
  251. module_exit(bcm47xx_wdt_exit);
  252. MODULE_AUTHOR("Aleksandar Radovanovic");
  253. MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
  254. MODULE_LICENSE("GPL");
  255. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);