wdt977.c 12 KB

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