wdt977.c 10.0 KB

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