w83877f_wdt.c 10 KB

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