wdt977.c 12 KB

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