w83877f_wdt.c 10 KB

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