wdt977.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Wdt977 0.04: 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. * 25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
  22. * remove limitiation to be used on
  23. * Netwinders only
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/types.h>
  29. #include <linux/kernel.h>
  30. #include <linux/fs.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/init.h>
  33. #include <linux/ioport.h>
  34. #include <linux/watchdog.h>
  35. #include <linux/notifier.h>
  36. #include <linux/reboot.h>
  37. #include <linux/io.h>
  38. #include <linux/uaccess.h>
  39. #include <asm/mach-types.h>
  40. #define WATCHDOG_VERSION "0.04"
  41. #define WATCHDOG_NAME "Wdt977"
  42. #define IO_INDEX_PORT 0x370 /* on some systems it can be 0x3F0 */
  43. #define IO_DATA_PORT (IO_INDEX_PORT + 1)
  44. #define UNLOCK_DATA 0x87
  45. #define LOCK_DATA 0xAA
  46. #define DEVICE_REGISTER 0x07
  47. #define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
  48. static int timeout = DEFAULT_TIMEOUT;
  49. static int timeoutM; /* timeout in minutes */
  50. static unsigned long timer_alive;
  51. static int testmode;
  52. static char expect_close;
  53. static DEFINE_SPINLOCK(spinlock);
  54. module_param(timeout, int, 0);
  55. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (60..15300, default="
  56. __MODULE_STRING(DEFAULT_TIMEOUT) ")");
  57. module_param(testmode, int, 0);
  58. MODULE_PARM_DESC(testmode, "Watchdog testmode (1 = no reboot), default=0");
  59. static bool nowayout = WATCHDOG_NOWAYOUT;
  60. module_param(nowayout, bool, 0);
  61. MODULE_PARM_DESC(nowayout,
  62. "Watchdog cannot be stopped once started (default="
  63. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  64. /*
  65. * Start the watchdog
  66. */
  67. static int wdt977_start(void)
  68. {
  69. unsigned long flags;
  70. spin_lock_irqsave(&spinlock, flags);
  71. /* unlock the SuperIO chip */
  72. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  73. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  74. /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
  75. * F2 has the timeout in minutes
  76. * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
  77. * at timeout, and to reset timer on kbd/mouse activity (not impl.)
  78. * F4 is used to just clear the TIMEOUT'ed state (bit 0)
  79. */
  80. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  81. outb_p(0x08, IO_DATA_PORT);
  82. outb_p(0xF2, IO_INDEX_PORT);
  83. outb_p(timeoutM, IO_DATA_PORT);
  84. outb_p(0xF3, IO_INDEX_PORT);
  85. outb_p(0x00, IO_DATA_PORT); /* another setting is 0E for
  86. kbd/mouse/LED */
  87. outb_p(0xF4, IO_INDEX_PORT);
  88. outb_p(0x00, IO_DATA_PORT);
  89. /* At last select device Aux1 (dev=7) and set GP16 as a
  90. * watchdog output. In test mode watch the bit 1 on F4 to
  91. * indicate "triggered"
  92. */
  93. if (!testmode) {
  94. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  95. outb_p(0x07, IO_DATA_PORT);
  96. outb_p(0xE6, IO_INDEX_PORT);
  97. outb_p(0x08, IO_DATA_PORT);
  98. }
  99. /* lock the SuperIO chip */
  100. outb_p(LOCK_DATA, IO_INDEX_PORT);
  101. spin_unlock_irqrestore(&spinlock, flags);
  102. pr_info("activated\n");
  103. return 0;
  104. }
  105. /*
  106. * Stop the watchdog
  107. */
  108. static int wdt977_stop(void)
  109. {
  110. unsigned long flags;
  111. spin_lock_irqsave(&spinlock, flags);
  112. /* unlock the SuperIO chip */
  113. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  114. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  115. /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
  116. * F3 is reset to its default state
  117. * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
  118. * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
  119. */
  120. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  121. outb_p(0x08, IO_DATA_PORT);
  122. outb_p(0xF2, IO_INDEX_PORT);
  123. outb_p(0xFF, IO_DATA_PORT);
  124. outb_p(0xF3, IO_INDEX_PORT);
  125. outb_p(0x00, IO_DATA_PORT);
  126. outb_p(0xF4, IO_INDEX_PORT);
  127. outb_p(0x00, IO_DATA_PORT);
  128. outb_p(0xF2, IO_INDEX_PORT);
  129. outb_p(0x00, IO_DATA_PORT);
  130. /* at last select device Aux1 (dev=7) and set
  131. GP16 as a watchdog output */
  132. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  133. outb_p(0x07, IO_DATA_PORT);
  134. outb_p(0xE6, IO_INDEX_PORT);
  135. outb_p(0x08, IO_DATA_PORT);
  136. /* lock the SuperIO chip */
  137. outb_p(LOCK_DATA, IO_INDEX_PORT);
  138. spin_unlock_irqrestore(&spinlock, flags);
  139. pr_info("shutdown\n");
  140. return 0;
  141. }
  142. /*
  143. * Send a keepalive ping to the watchdog
  144. * This is done by simply re-writing the timeout to reg. 0xF2
  145. */
  146. static int wdt977_keepalive(void)
  147. {
  148. unsigned long flags;
  149. spin_lock_irqsave(&spinlock, flags);
  150. /* unlock the SuperIO chip */
  151. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  152. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  153. /* select device Aux2 (device=8) and kicks watchdog reg F2 */
  154. /* F2 has the timeout in minutes */
  155. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  156. outb_p(0x08, IO_DATA_PORT);
  157. outb_p(0xF2, IO_INDEX_PORT);
  158. outb_p(timeoutM, IO_DATA_PORT);
  159. /* lock the SuperIO chip */
  160. outb_p(LOCK_DATA, IO_INDEX_PORT);
  161. spin_unlock_irqrestore(&spinlock, flags);
  162. return 0;
  163. }
  164. /*
  165. * Set the watchdog timeout value
  166. */
  167. static int wdt977_set_timeout(int t)
  168. {
  169. int tmrval;
  170. /* convert seconds to minutes, rounding up */
  171. tmrval = (t + 59) / 60;
  172. if (machine_is_netwinder()) {
  173. /* we have a hw bug somewhere, so each 977 minute is actually
  174. * only 30sec. This limits the max timeout to half of device
  175. * max of 255 minutes...
  176. */
  177. tmrval += tmrval;
  178. }
  179. if (tmrval < 1 || tmrval > 255)
  180. return -EINVAL;
  181. /* timeout is the timeout in seconds, timeoutM is
  182. the timeout in minutes) */
  183. timeout = t;
  184. timeoutM = tmrval;
  185. return 0;
  186. }
  187. /*
  188. * Get the watchdog status
  189. */
  190. static int wdt977_get_status(int *status)
  191. {
  192. int new_status;
  193. unsigned long flags;
  194. spin_lock_irqsave(&spinlock, flags);
  195. /* unlock the SuperIO chip */
  196. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  197. outb_p(UNLOCK_DATA, IO_INDEX_PORT);
  198. /* select device Aux2 (device=8) and read watchdog reg F4 */
  199. outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
  200. outb_p(0x08, IO_DATA_PORT);
  201. outb_p(0xF4, IO_INDEX_PORT);
  202. new_status = inb_p(IO_DATA_PORT);
  203. /* lock the SuperIO chip */
  204. outb_p(LOCK_DATA, IO_INDEX_PORT);
  205. spin_unlock_irqrestore(&spinlock, flags);
  206. *status = 0;
  207. if (new_status & 1)
  208. *status |= WDIOF_CARDRESET;
  209. return 0;
  210. }
  211. /*
  212. * /dev/watchdog handling
  213. */
  214. static int wdt977_open(struct inode *inode, struct file *file)
  215. {
  216. /* If the watchdog is alive we don't need to start it again */
  217. if (test_and_set_bit(0, &timer_alive))
  218. return -EBUSY;
  219. if (nowayout)
  220. __module_get(THIS_MODULE);
  221. wdt977_start();
  222. return nonseekable_open(inode, file);
  223. }
  224. static int wdt977_release(struct inode *inode, struct file *file)
  225. {
  226. /*
  227. * Shut off the timer.
  228. * Lock it in if it's a module and we set nowayout
  229. */
  230. if (expect_close == 42) {
  231. wdt977_stop();
  232. clear_bit(0, &timer_alive);
  233. } else {
  234. wdt977_keepalive();
  235. pr_crit("Unexpected close, not stopping watchdog!\n");
  236. }
  237. expect_close = 0;
  238. return 0;
  239. }
  240. /*
  241. * wdt977_write:
  242. * @file: file handle to the watchdog
  243. * @buf: buffer to write (unused as data does not matter here
  244. * @count: count of bytes
  245. * @ppos: pointer to the position to write. No seeks allowed
  246. *
  247. * A write to a watchdog device is defined as a keepalive signal. Any
  248. * write of data will do, as we we don't define content meaning.
  249. */
  250. static ssize_t wdt977_write(struct file *file, const char __user *buf,
  251. size_t count, loff_t *ppos)
  252. {
  253. if (count) {
  254. if (!nowayout) {
  255. size_t i;
  256. /* In case it was set long ago */
  257. expect_close = 0;
  258. for (i = 0; i != count; i++) {
  259. char c;
  260. if (get_user(c, buf + i))
  261. return -EFAULT;
  262. if (c == 'V')
  263. expect_close = 42;
  264. }
  265. }
  266. /* someone wrote to us, we should restart timer */
  267. wdt977_keepalive();
  268. }
  269. return count;
  270. }
  271. static const struct watchdog_info ident = {
  272. .options = WDIOF_SETTIMEOUT |
  273. WDIOF_MAGICCLOSE |
  274. WDIOF_KEEPALIVEPING,
  275. .firmware_version = 1,
  276. .identity = WATCHDOG_NAME,
  277. };
  278. /*
  279. * wdt977_ioctl:
  280. * @inode: inode of the device
  281. * @file: file handle to the device
  282. * @cmd: watchdog command
  283. * @arg: argument pointer
  284. *
  285. * The watchdog API defines a common set of functions for all watchdogs
  286. * according to their available features.
  287. */
  288. static long wdt977_ioctl(struct file *file, unsigned int cmd,
  289. unsigned long arg)
  290. {
  291. int status;
  292. int new_options, retval = -EINVAL;
  293. int new_timeout;
  294. union {
  295. struct watchdog_info __user *ident;
  296. int __user *i;
  297. } uarg;
  298. uarg.i = (int __user *)arg;
  299. switch (cmd) {
  300. case WDIOC_GETSUPPORT:
  301. return copy_to_user(uarg.ident, &ident,
  302. sizeof(ident)) ? -EFAULT : 0;
  303. case WDIOC_GETSTATUS:
  304. wdt977_get_status(&status);
  305. return put_user(status, uarg.i);
  306. case WDIOC_GETBOOTSTATUS:
  307. return put_user(0, uarg.i);
  308. case WDIOC_SETOPTIONS:
  309. if (get_user(new_options, uarg.i))
  310. return -EFAULT;
  311. if (new_options & WDIOS_DISABLECARD) {
  312. wdt977_stop();
  313. retval = 0;
  314. }
  315. if (new_options & WDIOS_ENABLECARD) {
  316. wdt977_start();
  317. retval = 0;
  318. }
  319. return retval;
  320. case WDIOC_KEEPALIVE:
  321. wdt977_keepalive();
  322. return 0;
  323. case WDIOC_SETTIMEOUT:
  324. if (get_user(new_timeout, uarg.i))
  325. return -EFAULT;
  326. if (wdt977_set_timeout(new_timeout))
  327. return -EINVAL;
  328. wdt977_keepalive();
  329. /* Fall */
  330. case WDIOC_GETTIMEOUT:
  331. return put_user(timeout, uarg.i);
  332. default:
  333. return -ENOTTY;
  334. }
  335. }
  336. static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
  337. void *unused)
  338. {
  339. if (code == SYS_DOWN || code == SYS_HALT)
  340. wdt977_stop();
  341. return NOTIFY_DONE;
  342. }
  343. static const struct file_operations wdt977_fops = {
  344. .owner = THIS_MODULE,
  345. .llseek = no_llseek,
  346. .write = wdt977_write,
  347. .unlocked_ioctl = wdt977_ioctl,
  348. .open = wdt977_open,
  349. .release = wdt977_release,
  350. };
  351. static struct miscdevice wdt977_miscdev = {
  352. .minor = WATCHDOG_MINOR,
  353. .name = "watchdog",
  354. .fops = &wdt977_fops,
  355. };
  356. static struct notifier_block wdt977_notifier = {
  357. .notifier_call = wdt977_notify_sys,
  358. };
  359. static int __init wd977_init(void)
  360. {
  361. int rc;
  362. pr_info("driver v%s\n", WATCHDOG_VERSION);
  363. /* Check that the timeout value is within its range;
  364. if not reset to the default */
  365. if (wdt977_set_timeout(timeout)) {
  366. wdt977_set_timeout(DEFAULT_TIMEOUT);
  367. pr_info("timeout value must be 60 < timeout < 15300, using %d\n",
  368. DEFAULT_TIMEOUT);
  369. }
  370. /* on Netwinder the IOports are already reserved by
  371. * arch/arm/mach-footbridge/netwinder-hw.c
  372. */
  373. if (!machine_is_netwinder()) {
  374. if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME)) {
  375. pr_err("I/O address 0x%04x already in use\n",
  376. IO_INDEX_PORT);
  377. rc = -EIO;
  378. goto err_out;
  379. }
  380. }
  381. rc = register_reboot_notifier(&wdt977_notifier);
  382. if (rc) {
  383. pr_err("cannot register reboot notifier (err=%d)\n", rc);
  384. goto err_out_region;
  385. }
  386. rc = misc_register(&wdt977_miscdev);
  387. if (rc) {
  388. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  389. wdt977_miscdev.minor, rc);
  390. goto err_out_reboot;
  391. }
  392. pr_info("initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
  393. timeout, nowayout, testmode);
  394. return 0;
  395. err_out_reboot:
  396. unregister_reboot_notifier(&wdt977_notifier);
  397. err_out_region:
  398. if (!machine_is_netwinder())
  399. release_region(IO_INDEX_PORT, 2);
  400. err_out:
  401. return rc;
  402. }
  403. static void __exit wd977_exit(void)
  404. {
  405. wdt977_stop();
  406. misc_deregister(&wdt977_miscdev);
  407. unregister_reboot_notifier(&wdt977_notifier);
  408. release_region(IO_INDEX_PORT, 2);
  409. }
  410. module_init(wd977_init);
  411. module_exit(wd977_exit);
  412. MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
  413. MODULE_DESCRIPTION("W83977AF Watchdog driver");
  414. MODULE_LICENSE("GPL");