wdt977.c 12 KB

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