wdt977.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Wdt977 0.03: A Watchdog Device for Netwinder W83977AF chip
  3. *
  4. * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
  5. *
  6. * -----------------------
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * -----------------------
  14. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  15. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  16. * 19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface
  17. * 06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts
  18. * from minutes to seconds.
  19. * 07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in
  20. * nwwatchdog_init.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/config.h>
  25. #include <linux/types.h>
  26. #include <linux/kernel.h>
  27. #include <linux/fs.h>
  28. #include <linux/miscdevice.h>
  29. #include <linux/init.h>
  30. #include <linux/watchdog.h>
  31. #include <linux/notifier.h>
  32. #include <linux/reboot.h>
  33. #include <asm/io.h>
  34. #include <asm/system.h>
  35. #include <asm/mach-types.h>
  36. #include <asm/uaccess.h>
  37. #define PFX "Wdt977: "
  38. #define WATCHDOG_MINOR 130
  39. #define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
  40. static int timeout = DEFAULT_TIMEOUT;
  41. static int timeoutM; /* timeout in minutes */
  42. static unsigned long timer_alive;
  43. static int testmode;
  44. static char expect_close;
  45. module_param(timeout, int, 0);
  46. MODULE_PARM_DESC(timeout,"Watchdog timeout in seconds (60..15300), default=" __MODULE_STRING(DEFAULT_TIMEOUT) ")");
  47. module_param(testmode, int, 0);
  48. MODULE_PARM_DESC(testmode,"Watchdog testmode (1 = no reboot), default=0");
  49. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  50. static int nowayout = 1;
  51. #else
  52. static int nowayout = 0;
  53. #endif
  54. module_param(nowayout, int, 0);
  55. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  56. /*
  57. * Start the watchdog
  58. */
  59. static int wdt977_start(void)
  60. {
  61. /* unlock the SuperIO chip */
  62. outb(0x87,0x370);
  63. outb(0x87,0x370);
  64. /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
  65. * F2 has the timeout in minutes
  66. * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
  67. * at timeout, and to reset timer on kbd/mouse activity (not impl.)
  68. * F4 is used to just clear the TIMEOUT'ed state (bit 0)
  69. */
  70. outb(0x07,0x370);
  71. outb(0x08,0x371);
  72. outb(0xF2,0x370);
  73. outb(timeoutM,0x371);
  74. outb(0xF3,0x370);
  75. outb(0x00,0x371); /* another setting is 0E for kbd/mouse/LED */
  76. outb(0xF4,0x370);
  77. outb(0x00,0x371);
  78. /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
  79. /* in test mode watch the bit 1 on F4 to indicate "triggered" */
  80. if (!testmode)
  81. {
  82. outb(0x07,0x370);
  83. outb(0x07,0x371);
  84. outb(0xE6,0x370);
  85. outb(0x08,0x371);
  86. }
  87. /* lock the SuperIO chip */
  88. outb(0xAA,0x370);
  89. printk(KERN_INFO PFX "activated.\n");
  90. return 0;
  91. }
  92. /*
  93. * Stop the watchdog
  94. */
  95. static int wdt977_stop(void)
  96. {
  97. /* unlock the SuperIO chip */
  98. outb(0x87,0x370);
  99. outb(0x87,0x370);
  100. /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
  101. * F3 is reset to its default state
  102. * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
  103. * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
  104. */
  105. outb(0x07,0x370);
  106. outb(0x08,0x371);
  107. outb(0xF2,0x370);
  108. outb(0xFF,0x371);
  109. outb(0xF3,0x370);
  110. outb(0x00,0x371);
  111. outb(0xF4,0x370);
  112. outb(0x00,0x371);
  113. outb(0xF2,0x370);
  114. outb(0x00,0x371);
  115. /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
  116. outb(0x07,0x370);
  117. outb(0x07,0x371);
  118. outb(0xE6,0x370);
  119. outb(0x08,0x371);
  120. /* lock the SuperIO chip */
  121. outb(0xAA,0x370);
  122. printk(KERN_INFO PFX "shutdown.\n");
  123. return 0;
  124. }
  125. /*
  126. * Send a keepalive ping to the watchdog
  127. * This is done by simply re-writing the timeout to reg. 0xF2
  128. */
  129. static int wdt977_keepalive(void)
  130. {
  131. /* unlock the SuperIO chip */
  132. outb(0x87,0x370);
  133. outb(0x87,0x370);
  134. /* select device Aux2 (device=8) and kicks watchdog reg F2 */
  135. /* F2 has the timeout in minutes */
  136. outb(0x07,0x370);
  137. outb(0x08,0x371);
  138. outb(0xF2,0x370);
  139. outb(timeoutM,0x371);
  140. /* lock the SuperIO chip */
  141. outb(0xAA,0x370);
  142. return 0;
  143. }
  144. /*
  145. * Set the watchdog timeout value
  146. */
  147. static int wdt977_set_timeout(int t)
  148. {
  149. int tmrval;
  150. /* convert seconds to minutes, rounding up */
  151. tmrval = (t + 59) / 60;
  152. if (machine_is_netwinder()) {
  153. /* we have a hw bug somewhere, so each 977 minute is actually only 30sec
  154. * this limits the max timeout to half of device max of 255 minutes...
  155. */
  156. tmrval += tmrval;
  157. }
  158. if ((tmrval < 1) || (tmrval > 255))
  159. return -EINVAL;
  160. /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
  161. timeout = t;
  162. timeoutM = tmrval;
  163. return 0;
  164. }
  165. /*
  166. * Get the watchdog status
  167. */
  168. static int wdt977_get_status(int *status)
  169. {
  170. int new_status;
  171. *status=0;
  172. /* unlock the SuperIO chip */
  173. outb(0x87,0x370);
  174. outb(0x87,0x370);
  175. /* select device Aux2 (device=8) and read watchdog reg F4 */
  176. outb(0x07,0x370);
  177. outb(0x08,0x371);
  178. outb(0xF4,0x370);
  179. new_status = inb(0x371);
  180. /* lock the SuperIO chip */
  181. outb(0xAA,0x370);
  182. if (new_status & 1)
  183. *status |= WDIOF_CARDRESET;
  184. return 0;
  185. }
  186. /*
  187. * /dev/watchdog handling
  188. */
  189. static int wdt977_open(struct inode *inode, struct file *file)
  190. {
  191. /* If the watchdog is alive we don't need to start it again */
  192. if( test_and_set_bit(0,&timer_alive) )
  193. return -EBUSY;
  194. if (nowayout)
  195. __module_get(THIS_MODULE);
  196. wdt977_start();
  197. return nonseekable_open(inode, file);
  198. }
  199. static int wdt977_release(struct inode *inode, struct file *file)
  200. {
  201. /*
  202. * Shut off the timer.
  203. * Lock it in if it's a module and we set nowayout
  204. */
  205. if (expect_close == 42)
  206. {
  207. wdt977_stop();
  208. clear_bit(0,&timer_alive);
  209. } else {
  210. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  211. wdt977_keepalive();
  212. }
  213. expect_close = 0;
  214. return 0;
  215. }
  216. /*
  217. * wdt977_write:
  218. * @file: file handle to the watchdog
  219. * @buf: buffer to write (unused as data does not matter here
  220. * @count: count of bytes
  221. * @ppos: pointer to the position to write. No seeks allowed
  222. *
  223. * A write to a watchdog device is defined as a keepalive signal. Any
  224. * write of data will do, as we we don't define content meaning.
  225. */
  226. static ssize_t wdt977_write(struct file *file, const char __user *buf,
  227. size_t count, loff_t *ppos)
  228. {
  229. if (count) {
  230. if (!nowayout) {
  231. size_t i;
  232. /* In case it was set long ago */
  233. expect_close = 0;
  234. for (i = 0; i != count; i++) {
  235. char c;
  236. if (get_user(c, buf + i))
  237. return -EFAULT;
  238. if (c == 'V')
  239. expect_close = 42;
  240. }
  241. }
  242. wdt977_keepalive();
  243. }
  244. return count;
  245. }
  246. /*
  247. * wdt977_ioctl:
  248. * @inode: inode of the device
  249. * @file: file handle to the device
  250. * @cmd: watchdog command
  251. * @arg: argument pointer
  252. *
  253. * The watchdog API defines a common set of functions for all watchdogs
  254. * according to their available features.
  255. */
  256. static struct watchdog_info ident = {
  257. .options = WDIOF_SETTIMEOUT |
  258. WDIOF_MAGICCLOSE |
  259. WDIOF_KEEPALIVEPING,
  260. .firmware_version = 1,
  261. .identity = "Winbond 83977",
  262. };
  263. static int wdt977_ioctl(struct inode *inode, struct file *file,
  264. unsigned int cmd, unsigned long arg)
  265. {
  266. int status;
  267. int new_options, retval = -EINVAL;
  268. int new_timeout;
  269. union {
  270. struct watchdog_info __user *ident;
  271. int __user *i;
  272. } uarg;
  273. uarg.i = (int __user *)arg;
  274. switch(cmd)
  275. {
  276. default:
  277. return -ENOIOCTLCMD;
  278. case WDIOC_GETSUPPORT:
  279. return copy_to_user(uarg.ident, &ident,
  280. sizeof(ident)) ? -EFAULT : 0;
  281. case WDIOC_GETSTATUS:
  282. wdt977_get_status(&status);
  283. return put_user(status, uarg.i);
  284. case WDIOC_GETBOOTSTATUS:
  285. return put_user(0, uarg.i);
  286. case WDIOC_KEEPALIVE:
  287. wdt977_keepalive();
  288. return 0;
  289. case WDIOC_SETOPTIONS:
  290. if (get_user (new_options, uarg.i))
  291. return -EFAULT;
  292. if (new_options & WDIOS_DISABLECARD) {
  293. wdt977_stop();
  294. retval = 0;
  295. }
  296. if (new_options & WDIOS_ENABLECARD) {
  297. wdt977_start();
  298. retval = 0;
  299. }
  300. return retval;
  301. case WDIOC_SETTIMEOUT:
  302. if (get_user(new_timeout, uarg.i))
  303. return -EFAULT;
  304. if (wdt977_set_timeout(new_timeout))
  305. return -EINVAL;
  306. wdt977_keepalive();
  307. /* Fall */
  308. case WDIOC_GETTIMEOUT:
  309. return put_user(timeout, uarg.i);
  310. }
  311. }
  312. static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
  313. void *unused)
  314. {
  315. if(code==SYS_DOWN || code==SYS_HALT)
  316. wdt977_stop();
  317. return NOTIFY_DONE;
  318. }
  319. static struct file_operations wdt977_fops=
  320. {
  321. .owner = THIS_MODULE,
  322. .llseek = no_llseek,
  323. .write = wdt977_write,
  324. .ioctl = wdt977_ioctl,
  325. .open = wdt977_open,
  326. .release = wdt977_release,
  327. };
  328. static struct miscdevice wdt977_miscdev=
  329. {
  330. .minor = WATCHDOG_MINOR,
  331. .name = "watchdog",
  332. .fops = &wdt977_fops,
  333. };
  334. static struct notifier_block wdt977_notifier = {
  335. .notifier_call = wdt977_notify_sys,
  336. };
  337. static int __init nwwatchdog_init(void)
  338. {
  339. int retval;
  340. if (!machine_is_netwinder())
  341. return -ENODEV;
  342. /* Check that the timeout value is within it's range ; if not reset to the default */
  343. if (wdt977_set_timeout(timeout)) {
  344. wdt977_set_timeout(DEFAULT_TIMEOUT);
  345. printk(KERN_INFO PFX "timeout value must be 60<timeout<15300, using %d\n",
  346. DEFAULT_TIMEOUT);
  347. }
  348. retval = register_reboot_notifier(&wdt977_notifier);
  349. if (retval) {
  350. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  351. retval);
  352. return retval;
  353. }
  354. retval = misc_register(&wdt977_miscdev);
  355. if (retval) {
  356. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  357. WATCHDOG_MINOR, retval);
  358. unregister_reboot_notifier(&wdt977_notifier);
  359. return retval;
  360. }
  361. printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode = %i)\n",
  362. timeout, nowayout, testmode);
  363. return 0;
  364. }
  365. static void __exit nwwatchdog_exit(void)
  366. {
  367. misc_deregister(&wdt977_miscdev);
  368. unregister_reboot_notifier(&wdt977_notifier);
  369. }
  370. module_init(nwwatchdog_init);
  371. module_exit(nwwatchdog_exit);
  372. MODULE_AUTHOR("Woody Suwalski <woody@netwinder.org>");
  373. MODULE_DESCRIPTION("W83977AF Watchdog driver");
  374. MODULE_LICENSE("GPL");
  375. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);