w83877f_wdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  82. static void wdt_timer_ping(unsigned long);
  83. static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0);
  84. static unsigned long next_heartbeat;
  85. static unsigned long wdt_is_open;
  86. static char wdt_expect_close;
  87. static DEFINE_SPINLOCK(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. mod_timer(&timer, jiffies + WDT_INTERVAL);
  104. spin_unlock(&wdt_spinlock);
  105. } else {
  106. printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
  107. }
  108. }
  109. /*
  110. * Utility routines
  111. */
  112. static void wdt_change(int writeval)
  113. {
  114. unsigned long flags;
  115. spin_lock_irqsave(&wdt_spinlock, flags);
  116. /* buy some time */
  117. inb_p(WDT_PING);
  118. /* make W83877F available */
  119. outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
  120. outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
  121. /* enable watchdog */
  122. outb_p(WDT_REGISTER, ENABLE_W83877F_PORT);
  123. outb_p(writeval, ENABLE_W83877F_PORT+1);
  124. /* lock the W8387FF away */
  125. outb_p(DISABLE_W83877F, ENABLE_W83877F_PORT);
  126. spin_unlock_irqrestore(&wdt_spinlock, flags);
  127. }
  128. static void wdt_startup(void)
  129. {
  130. next_heartbeat = jiffies + (timeout * HZ);
  131. /* Start the timer */
  132. mod_timer(&timer, jiffies + WDT_INTERVAL);
  133. wdt_change(WDT_ENABLE);
  134. printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
  135. }
  136. static void wdt_turnoff(void)
  137. {
  138. /* Stop the timer */
  139. del_timer(&timer);
  140. wdt_change(WDT_DISABLE);
  141. printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
  142. }
  143. static void wdt_keepalive(void)
  144. {
  145. /* user land ping */
  146. next_heartbeat = jiffies + (timeout * HZ);
  147. }
  148. /*
  149. * /dev/watchdog handling
  150. */
  151. static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
  152. {
  153. /* See if we got the magic character 'V' and reload the timer */
  154. if(count)
  155. {
  156. if (!nowayout)
  157. {
  158. size_t ofs;
  159. /* note: just in case someone wrote the magic character
  160. * five months ago... */
  161. wdt_expect_close = 0;
  162. /* scan to see whether or not we got the magic character */
  163. for(ofs = 0; ofs != count; ofs++)
  164. {
  165. char c;
  166. if (get_user(c, buf + ofs))
  167. return -EFAULT;
  168. if (c == 'V')
  169. wdt_expect_close = 42;
  170. }
  171. }
  172. /* someone wrote to us, we should restart timer */
  173. wdt_keepalive();
  174. }
  175. return count;
  176. }
  177. static int fop_open(struct inode * inode, struct file * file)
  178. {
  179. /* Just in case we're already talking to someone... */
  180. if(test_and_set_bit(0, &wdt_is_open))
  181. return -EBUSY;
  182. /* Good, fire up the show */
  183. wdt_startup();
  184. return nonseekable_open(inode, file);
  185. }
  186. static int fop_close(struct inode * inode, struct file * file)
  187. {
  188. if(wdt_expect_close == 42)
  189. wdt_turnoff();
  190. else {
  191. del_timer(&timer);
  192. printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n");
  193. }
  194. clear_bit(0, &wdt_is_open);
  195. wdt_expect_close = 0;
  196. return 0;
  197. }
  198. static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  199. unsigned long arg)
  200. {
  201. void __user *argp = (void __user *)arg;
  202. int __user *p = argp;
  203. static struct watchdog_info ident=
  204. {
  205. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  206. .firmware_version = 1,
  207. .identity = "W83877F",
  208. };
  209. switch(cmd)
  210. {
  211. default:
  212. return -ENOTTY;
  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_KEEPALIVE:
  219. wdt_keepalive();
  220. return 0;
  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_SETTIMEOUT:
  237. {
  238. int new_timeout;
  239. if(get_user(new_timeout, p))
  240. return -EFAULT;
  241. if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */
  242. return -EINVAL;
  243. timeout = new_timeout;
  244. wdt_keepalive();
  245. /* Fall through */
  246. }
  247. case WDIOC_GETTIMEOUT:
  248. return put_user(timeout, p);
  249. }
  250. }
  251. static const struct file_operations wdt_fops = {
  252. .owner = THIS_MODULE,
  253. .llseek = no_llseek,
  254. .write = fop_write,
  255. .open = fop_open,
  256. .release = fop_close,
  257. .ioctl = fop_ioctl,
  258. };
  259. static struct miscdevice wdt_miscdev = {
  260. .minor = WATCHDOG_MINOR,
  261. .name = "watchdog",
  262. .fops = &wdt_fops,
  263. };
  264. /*
  265. * Notifier for system down
  266. */
  267. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  268. void *unused)
  269. {
  270. if(code==SYS_DOWN || code==SYS_HALT)
  271. wdt_turnoff();
  272. return NOTIFY_DONE;
  273. }
  274. /*
  275. * The WDT needs to learn about soft shutdowns in order to
  276. * turn the timebomb registers off.
  277. */
  278. static struct notifier_block wdt_notifier=
  279. {
  280. .notifier_call = wdt_notify_sys,
  281. };
  282. static void __exit w83877f_wdt_unload(void)
  283. {
  284. wdt_turnoff();
  285. /* Deregister */
  286. misc_deregister(&wdt_miscdev);
  287. unregister_reboot_notifier(&wdt_notifier);
  288. release_region(WDT_PING,1);
  289. release_region(ENABLE_W83877F_PORT,2);
  290. }
  291. static int __init w83877f_wdt_init(void)
  292. {
  293. int rc = -EBUSY;
  294. if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */
  295. {
  296. timeout = WATCHDOG_TIMEOUT;
  297. printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n",
  298. timeout);
  299. }
  300. if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT"))
  301. {
  302. printk(KERN_ERR PFX "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. {
  309. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  310. WDT_PING);
  311. rc = -EIO;
  312. goto err_out_region1;
  313. }
  314. rc = misc_register(&wdt_miscdev);
  315. if (rc)
  316. {
  317. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  318. wdt_miscdev.minor, rc);
  319. goto err_out_region2;
  320. }
  321. rc = register_reboot_notifier(&wdt_notifier);
  322. if (rc)
  323. {
  324. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  325. rc);
  326. goto err_out_miscdev;
  327. }
  328. printk(KERN_INFO PFX "WDT driver for W83877F initialised. timeout=%d sec (nowayout=%d)\n",
  329. timeout, nowayout);
  330. return 0;
  331. err_out_miscdev:
  332. misc_deregister(&wdt_miscdev);
  333. err_out_region2:
  334. release_region(WDT_PING,1);
  335. err_out_region1:
  336. release_region(ENABLE_W83877F_PORT,2);
  337. err_out:
  338. return rc;
  339. }
  340. module_init(w83877f_wdt_init);
  341. module_exit(w83877f_wdt_unload);
  342. MODULE_AUTHOR("Scott and Bill Jennings");
  343. MODULE_DESCRIPTION("Driver for watchdog timer in w83877f chip");
  344. MODULE_LICENSE("GPL");
  345. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);