w83877f_wdt.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * W83877F Computer Watchdog Timer driver
  3. *
  4. * Based on acquirewdt.c by Alan Cox,
  5. * and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
  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. * The authors do NOT admit liability nor provide warranty for
  13. * any of this software. This material is provided "AS-IS" in
  14. * the hope that it may be useful for others.
  15. *
  16. * (c) Copyright 2001 Scott Jennings <linuxdrivers@oro.net>
  17. *
  18. * 4/19 - 2001 [Initial revision]
  19. * 9/27 - 2001 Added spinlocking
  20. * 4/12 - 2002 [rob@osinvestor.com] Eliminate extra comments
  21. * Eliminate fop_read
  22. * Eliminate extra spin_unlock
  23. * Added KERN_* tags to printks
  24. * add CONFIG_WATCHDOG_NOWAYOUT support
  25. * fix possible wdt_is_open race
  26. * changed watchdog_info to correctly reflect what
  27. * the driver offers
  28. * added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS,
  29. * WDIOC_SETTIMEOUT,
  30. * WDIOC_GETTIMEOUT, and WDIOC_SETOPTIONS ioctls
  31. * 09/8 - 2003 [wim@iguana.be] cleanup of trailing spaces
  32. * added extra printk's for startup problems
  33. * use module_param
  34. * made timeout (the emulated heartbeat) a
  35. * module_param
  36. * made the keepalive ping an internal subroutine
  37. *
  38. * This WDT driver is different from most other Linux WDT
  39. * drivers in that the driver will ping the watchdog by itself,
  40. * because this particular WDT has a very short timeout (1.6
  41. * seconds) and it would be insane to count on any userspace
  42. * daemon always getting scheduled within that time frame.
  43. */
  44. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  45. #include <linux/module.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/types.h>
  48. #include <linux/timer.h>
  49. #include <linux/jiffies.h>
  50. #include <linux/miscdevice.h>
  51. #include <linux/watchdog.h>
  52. #include <linux/fs.h>
  53. #include <linux/ioport.h>
  54. #include <linux/notifier.h>
  55. #include <linux/reboot.h>
  56. #include <linux/init.h>
  57. #include <linux/io.h>
  58. #include <linux/uaccess.h>
  59. #define OUR_NAME "w83877f_wdt"
  60. #define ENABLE_W83877F_PORT 0x3F0
  61. #define ENABLE_W83877F 0x87
  62. #define DISABLE_W83877F 0xAA
  63. #define WDT_PING 0x443
  64. #define WDT_REGISTER 0x14
  65. #define WDT_ENABLE 0x9C
  66. #define WDT_DISABLE 0x8C
  67. /*
  68. * The W83877F seems to be fixed at 1.6s timeout (at least on the
  69. * EMACS PC-104 board I'm using). If we reset the watchdog every
  70. * ~250ms we should be safe. */
  71. #define WDT_INTERVAL (HZ/4+1)
  72. /*
  73. * We must not require too good response from the userspace daemon.
  74. * Here we require the userspace daemon to send us a heartbeat
  75. * char to /dev/watchdog every 30 seconds.
  76. */
  77. #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
  78. /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
  79. static int timeout = WATCHDOG_TIMEOUT;
  80. module_param(timeout, int, 0);
  81. MODULE_PARM_DESC(timeout,
  82. "Watchdog timeout in seconds. (1<=timeout<=3600, default="
  83. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  84. static bool nowayout = WATCHDOG_NOWAYOUT;
  85. module_param(nowayout, bool, 0);
  86. MODULE_PARM_DESC(nowayout,
  87. "Watchdog cannot be stopped once started (default="
  88. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  89. static void wdt_timer_ping(unsigned long);
  90. static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0);
  91. static unsigned long next_heartbeat;
  92. static unsigned long wdt_is_open;
  93. static char wdt_expect_close;
  94. static DEFINE_SPINLOCK(wdt_spinlock);
  95. /*
  96. * Whack the dog
  97. */
  98. static void wdt_timer_ping(unsigned long data)
  99. {
  100. /* If we got a heartbeat pulse within the WDT_US_INTERVAL
  101. * we agree to ping the WDT
  102. */
  103. if (time_before(jiffies, next_heartbeat)) {
  104. /* Ping the WDT */
  105. spin_lock(&wdt_spinlock);
  106. /* Ping the WDT by reading from WDT_PING */
  107. inb_p(WDT_PING);
  108. /* Re-set the timer interval */
  109. mod_timer(&timer, jiffies + WDT_INTERVAL);
  110. spin_unlock(&wdt_spinlock);
  111. } else
  112. pr_warn("Heartbeat lost! Will not ping the watchdog\n");
  113. }
  114. /*
  115. * Utility routines
  116. */
  117. static void wdt_change(int writeval)
  118. {
  119. unsigned long flags;
  120. spin_lock_irqsave(&wdt_spinlock, flags);
  121. /* buy some time */
  122. inb_p(WDT_PING);
  123. /* make W83877F available */
  124. outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
  125. outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
  126. /* enable watchdog */
  127. outb_p(WDT_REGISTER, ENABLE_W83877F_PORT);
  128. outb_p(writeval, ENABLE_W83877F_PORT+1);
  129. /* lock the W8387FF away */
  130. outb_p(DISABLE_W83877F, ENABLE_W83877F_PORT);
  131. spin_unlock_irqrestore(&wdt_spinlock, flags);
  132. }
  133. static void wdt_startup(void)
  134. {
  135. next_heartbeat = jiffies + (timeout * HZ);
  136. /* Start the timer */
  137. mod_timer(&timer, jiffies + WDT_INTERVAL);
  138. wdt_change(WDT_ENABLE);
  139. pr_info("Watchdog timer is now enabled\n");
  140. }
  141. static void wdt_turnoff(void)
  142. {
  143. /* Stop the timer */
  144. del_timer(&timer);
  145. wdt_change(WDT_DISABLE);
  146. pr_info("Watchdog timer is now disabled...\n");
  147. }
  148. static void wdt_keepalive(void)
  149. {
  150. /* user land ping */
  151. next_heartbeat = jiffies + (timeout * HZ);
  152. }
  153. /*
  154. * /dev/watchdog handling
  155. */
  156. static ssize_t fop_write(struct file *file, const char __user *buf,
  157. size_t count, loff_t *ppos)
  158. {
  159. /* See if we got the magic character 'V' and reload the timer */
  160. if (count) {
  161. if (!nowayout) {
  162. size_t ofs;
  163. /* note: just in case someone wrote the magic
  164. character five months ago... */
  165. wdt_expect_close = 0;
  166. /* scan to see whether or not we got the
  167. magic character */
  168. for (ofs = 0; ofs != count; ofs++) {
  169. char c;
  170. if (get_user(c, buf + ofs))
  171. return -EFAULT;
  172. if (c == 'V')
  173. wdt_expect_close = 42;
  174. }
  175. }
  176. /* someone wrote to us, we should restart timer */
  177. wdt_keepalive();
  178. }
  179. return count;
  180. }
  181. static int fop_open(struct inode *inode, struct file *file)
  182. {
  183. /* Just in case we're already talking to someone... */
  184. if (test_and_set_bit(0, &wdt_is_open))
  185. return -EBUSY;
  186. /* Good, fire up the show */
  187. wdt_startup();
  188. return nonseekable_open(inode, file);
  189. }
  190. static int fop_close(struct inode *inode, struct file *file)
  191. {
  192. if (wdt_expect_close == 42)
  193. wdt_turnoff();
  194. else {
  195. del_timer(&timer);
  196. pr_crit("device file closed unexpectedly. Will not stop the WDT!\n");
  197. }
  198. clear_bit(0, &wdt_is_open);
  199. wdt_expect_close = 0;
  200. return 0;
  201. }
  202. static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  203. {
  204. void __user *argp = (void __user *)arg;
  205. int __user *p = argp;
  206. static const struct watchdog_info ident = {
  207. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
  208. | WDIOF_MAGICCLOSE,
  209. .firmware_version = 1,
  210. .identity = "W83877F",
  211. };
  212. switch (cmd) {
  213. case WDIOC_GETSUPPORT:
  214. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  215. case WDIOC_GETSTATUS:
  216. case WDIOC_GETBOOTSTATUS:
  217. return put_user(0, p);
  218. case WDIOC_SETOPTIONS:
  219. {
  220. int new_options, retval = -EINVAL;
  221. if (get_user(new_options, p))
  222. return -EFAULT;
  223. if (new_options & WDIOS_DISABLECARD) {
  224. wdt_turnoff();
  225. retval = 0;
  226. }
  227. if (new_options & WDIOS_ENABLECARD) {
  228. wdt_startup();
  229. retval = 0;
  230. }
  231. return retval;
  232. }
  233. case WDIOC_KEEPALIVE:
  234. wdt_keepalive();
  235. return 0;
  236. case WDIOC_SETTIMEOUT:
  237. {
  238. int new_timeout;
  239. if (get_user(new_timeout, p))
  240. return -EFAULT;
  241. /* arbitrary upper limit */
  242. if (new_timeout < 1 || new_timeout > 3600)
  243. return -EINVAL;
  244. timeout = new_timeout;
  245. wdt_keepalive();
  246. /* Fall through */
  247. }
  248. case WDIOC_GETTIMEOUT:
  249. return put_user(timeout, p);
  250. default:
  251. return -ENOTTY;
  252. }
  253. }
  254. static const struct file_operations wdt_fops = {
  255. .owner = THIS_MODULE,
  256. .llseek = no_llseek,
  257. .write = fop_write,
  258. .open = fop_open,
  259. .release = fop_close,
  260. .unlocked_ioctl = fop_ioctl,
  261. };
  262. static struct miscdevice wdt_miscdev = {
  263. .minor = WATCHDOG_MINOR,
  264. .name = "watchdog",
  265. .fops = &wdt_fops,
  266. };
  267. /*
  268. * Notifier for system down
  269. */
  270. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  271. void *unused)
  272. {
  273. if (code == SYS_DOWN || code == SYS_HALT)
  274. wdt_turnoff();
  275. return NOTIFY_DONE;
  276. }
  277. /*
  278. * The WDT needs to learn about soft shutdowns in order to
  279. * turn the timebomb registers off.
  280. */
  281. static struct notifier_block wdt_notifier = {
  282. .notifier_call = wdt_notify_sys,
  283. };
  284. static void __exit w83877f_wdt_unload(void)
  285. {
  286. wdt_turnoff();
  287. /* Deregister */
  288. misc_deregister(&wdt_miscdev);
  289. unregister_reboot_notifier(&wdt_notifier);
  290. release_region(WDT_PING, 1);
  291. release_region(ENABLE_W83877F_PORT, 2);
  292. }
  293. static int __init w83877f_wdt_init(void)
  294. {
  295. int rc = -EBUSY;
  296. if (timeout < 1 || timeout > 3600) { /* arbitrary upper limit */
  297. timeout = WATCHDOG_TIMEOUT;
  298. pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
  299. timeout);
  300. }
  301. if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT")) {
  302. pr_err("I/O address 0x%04x already in use\n",
  303. ENABLE_W83877F_PORT);
  304. rc = -EIO;
  305. goto err_out;
  306. }
  307. if (!request_region(WDT_PING, 1, "W8387FF WDT")) {
  308. pr_err("I/O address 0x%04x already in use\n", WDT_PING);
  309. rc = -EIO;
  310. goto err_out_region1;
  311. }
  312. rc = register_reboot_notifier(&wdt_notifier);
  313. if (rc) {
  314. pr_err("cannot register reboot notifier (err=%d)\n", rc);
  315. goto err_out_region2;
  316. }
  317. rc = misc_register(&wdt_miscdev);
  318. if (rc) {
  319. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  320. wdt_miscdev.minor, rc);
  321. goto err_out_reboot;
  322. }
  323. pr_info("WDT driver for W83877F initialised. timeout=%d sec (nowayout=%d)\n",
  324. timeout, nowayout);
  325. return 0;
  326. err_out_reboot:
  327. unregister_reboot_notifier(&wdt_notifier);
  328. err_out_region2:
  329. release_region(WDT_PING, 1);
  330. err_out_region1:
  331. release_region(ENABLE_W83877F_PORT, 2);
  332. err_out:
  333. return rc;
  334. }
  335. module_init(w83877f_wdt_init);
  336. module_exit(w83877f_wdt_unload);
  337. MODULE_AUTHOR("Scott and Bill Jennings");
  338. MODULE_DESCRIPTION("Driver for watchdog timer in w83877f chip");
  339. MODULE_LICENSE("GPL");