w83977f_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * W83977F Watchdog Timer Driver for Winbond W83977F I/O Chip
  3. *
  4. * (c) Copyright 2005 Jose Goncalves <jose.goncalves@inov.pt>
  5. *
  6. * Based on w83877f_wdt.c by Scott Jennings,
  7. * and wdt977.c by Woody Suwalski
  8. *
  9. * -----------------------
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/fs.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/init.h>
  24. #include <linux/ioport.h>
  25. #include <linux/watchdog.h>
  26. #include <linux/notifier.h>
  27. #include <linux/reboot.h>
  28. #include <asm/io.h>
  29. #include <asm/system.h>
  30. #include <asm/uaccess.h>
  31. #define WATCHDOG_VERSION "1.00"
  32. #define WATCHDOG_NAME "W83977F WDT"
  33. #define PFX WATCHDOG_NAME ": "
  34. #define DRIVER_VERSION WATCHDOG_NAME " driver, v" WATCHDOG_VERSION "\n"
  35. #define IO_INDEX_PORT 0x3F0
  36. #define IO_DATA_PORT (IO_INDEX_PORT+1)
  37. #define UNLOCK_DATA 0x87
  38. #define LOCK_DATA 0xAA
  39. #define DEVICE_REGISTER 0x07
  40. #define DEFAULT_TIMEOUT 45 /* default timeout in seconds */
  41. static int timeout = DEFAULT_TIMEOUT;
  42. static int timeoutW; /* timeout in watchdog counter units */
  43. static unsigned long timer_alive;
  44. static int testmode;
  45. static char expect_close;
  46. static DEFINE_SPINLOCK(spinlock);
  47. module_param(timeout, int, 0);
  48. MODULE_PARM_DESC(timeout,"Watchdog timeout in seconds (15..7635), default=" __MODULE_STRING(DEFAULT_TIMEOUT) ")");
  49. module_param(testmode, int, 0);
  50. MODULE_PARM_DESC(testmode,"Watchdog testmode (1 = no reboot), default=0");
  51. static int nowayout = WATCHDOG_NOWAYOUT;
  52. module_param(nowayout, int, 0);
  53. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  54. /*
  55. * Start the watchdog
  56. */
  57. static int wdt_start(void)
  58. {
  59. unsigned long flags;
  60. spin_lock_irqsave(&spinlock, flags);
  61. /* Unlock the SuperIO chip */
  62. outb_p(UNLOCK_DATA,IO_INDEX_PORT);
  63. outb_p(UNLOCK_DATA,IO_INDEX_PORT);
  64. /*
  65. * Select device Aux2 (device=8) to set watchdog regs F2, F3 and F4.
  66. * F2 has the timeout in watchdog counter units.
  67. * F3 is set to enable watchdog LED blink at timeout.
  68. * F4 is used to just clear the TIMEOUT'ed state (bit 0).
  69. */
  70. outb_p(DEVICE_REGISTER,IO_INDEX_PORT);
  71. outb_p(0x08,IO_DATA_PORT);
  72. outb_p(0xF2,IO_INDEX_PORT);
  73. outb_p(timeoutW,IO_DATA_PORT);
  74. outb_p(0xF3,IO_INDEX_PORT);
  75. outb_p(0x08,IO_DATA_PORT);
  76. outb_p(0xF4,IO_INDEX_PORT);
  77. outb_p(0x00,IO_DATA_PORT);
  78. /* Set device Aux2 active */
  79. outb_p(0x30,IO_INDEX_PORT);
  80. outb_p(0x01,IO_DATA_PORT);
  81. /*
  82. * Select device Aux1 (dev=7) to set GP16 as the watchdog output
  83. * (in reg E6) and GP13 as the watchdog LED output (in reg E3).
  84. * Map GP16 at pin 119.
  85. * In test mode watch the bit 0 on F4 to indicate "triggered" or
  86. * check watchdog LED on SBC.
  87. */
  88. outb_p(DEVICE_REGISTER,IO_INDEX_PORT);
  89. outb_p(0x07,IO_DATA_PORT);
  90. if (!testmode)
  91. {
  92. unsigned pin_map;
  93. outb_p(0xE6,IO_INDEX_PORT);
  94. outb_p(0x0A,IO_DATA_PORT);
  95. outb_p(0x2C,IO_INDEX_PORT);
  96. pin_map = inb_p(IO_DATA_PORT);
  97. pin_map |= 0x10;
  98. pin_map &= ~(0x20);
  99. outb_p(0x2C,IO_INDEX_PORT);
  100. outb_p(pin_map,IO_DATA_PORT);
  101. }
  102. outb_p(0xE3,IO_INDEX_PORT);
  103. outb_p(0x08,IO_DATA_PORT);
  104. /* Set device Aux1 active */
  105. outb_p(0x30,IO_INDEX_PORT);
  106. outb_p(0x01,IO_DATA_PORT);
  107. /* Lock the SuperIO chip */
  108. outb_p(LOCK_DATA,IO_INDEX_PORT);
  109. spin_unlock_irqrestore(&spinlock, flags);
  110. printk(KERN_INFO PFX "activated.\n");
  111. return 0;
  112. }
  113. /*
  114. * Stop the watchdog
  115. */
  116. static int wdt_stop(void)
  117. {
  118. unsigned long flags;
  119. spin_lock_irqsave(&spinlock, flags);
  120. /* Unlock the SuperIO chip */
  121. outb_p(UNLOCK_DATA,IO_INDEX_PORT);
  122. outb_p(UNLOCK_DATA,IO_INDEX_PORT);
  123. /*
  124. * Select device Aux2 (device=8) to set watchdog regs F2, F3 and F4.
  125. * F2 is reset to its default value (watchdog timer disabled).
  126. * F3 is reset to its default state.
  127. * F4 clears the TIMEOUT'ed state (bit 0) - back to default.
  128. */
  129. outb_p(DEVICE_REGISTER,IO_INDEX_PORT);
  130. outb_p(0x08,IO_DATA_PORT);
  131. outb_p(0xF2,IO_INDEX_PORT);
  132. outb_p(0xFF,IO_DATA_PORT);
  133. outb_p(0xF3,IO_INDEX_PORT);
  134. outb_p(0x00,IO_DATA_PORT);
  135. outb_p(0xF4,IO_INDEX_PORT);
  136. outb_p(0x00,IO_DATA_PORT);
  137. outb_p(0xF2,IO_INDEX_PORT);
  138. outb_p(0x00,IO_DATA_PORT);
  139. /*
  140. * Select device Aux1 (dev=7) to set GP16 (in reg E6) and
  141. * Gp13 (in reg E3) as inputs.
  142. */
  143. outb_p(DEVICE_REGISTER,IO_INDEX_PORT);
  144. outb_p(0x07,IO_DATA_PORT);
  145. if (!testmode)
  146. {
  147. outb_p(0xE6,IO_INDEX_PORT);
  148. outb_p(0x01,IO_DATA_PORT);
  149. }
  150. outb_p(0xE3,IO_INDEX_PORT);
  151. outb_p(0x01,IO_DATA_PORT);
  152. /* Lock the SuperIO chip */
  153. outb_p(LOCK_DATA,IO_INDEX_PORT);
  154. spin_unlock_irqrestore(&spinlock, flags);
  155. printk(KERN_INFO PFX "shutdown.\n");
  156. return 0;
  157. }
  158. /*
  159. * Send a keepalive ping to the watchdog
  160. * This is done by simply re-writing the timeout to reg. 0xF2
  161. */
  162. static int wdt_keepalive(void)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&spinlock, flags);
  166. /* Unlock the SuperIO chip */
  167. outb_p(UNLOCK_DATA,IO_INDEX_PORT);
  168. outb_p(UNLOCK_DATA,IO_INDEX_PORT);
  169. /* Select device Aux2 (device=8) to kick watchdog reg F2 */
  170. outb_p(DEVICE_REGISTER,IO_INDEX_PORT);
  171. outb_p(0x08,IO_DATA_PORT);
  172. outb_p(0xF2,IO_INDEX_PORT);
  173. outb_p(timeoutW,IO_DATA_PORT);
  174. /* Lock the SuperIO chip */
  175. outb_p(LOCK_DATA,IO_INDEX_PORT);
  176. spin_unlock_irqrestore(&spinlock, flags);
  177. return 0;
  178. }
  179. /*
  180. * Set the watchdog timeout value
  181. */
  182. static int wdt_set_timeout(int t)
  183. {
  184. int tmrval;
  185. /*
  186. * Convert seconds to watchdog counter time units, rounding up.
  187. * On PCM-5335 watchdog units are 30 seconds/step with 15 sec startup
  188. * value. This information is supplied in the PCM-5335 manual and was
  189. * checked by me on a real board. This is a bit strange because W83977f
  190. * datasheet says counter unit is in minutes!
  191. */
  192. if (t < 15)
  193. return -EINVAL;
  194. tmrval = ((t + 15) + 29) / 30;
  195. if (tmrval > 255)
  196. return -EINVAL;
  197. /*
  198. * timeout is the timeout in seconds,
  199. * timeoutW is the timeout in watchdog counter units.
  200. */
  201. timeoutW = tmrval;
  202. timeout = (timeoutW * 30) - 15;
  203. return 0;
  204. }
  205. /*
  206. * Get the watchdog status
  207. */
  208. static int wdt_get_status(int *status)
  209. {
  210. int new_status;
  211. unsigned long flags;
  212. spin_lock_irqsave(&spinlock, flags);
  213. /* Unlock the SuperIO chip */
  214. outb_p(UNLOCK_DATA,IO_INDEX_PORT);
  215. outb_p(UNLOCK_DATA,IO_INDEX_PORT);
  216. /* Select device Aux2 (device=8) to read watchdog reg F4 */
  217. outb_p(DEVICE_REGISTER,IO_INDEX_PORT);
  218. outb_p(0x08,IO_DATA_PORT);
  219. outb_p(0xF4,IO_INDEX_PORT);
  220. new_status = inb_p(IO_DATA_PORT);
  221. /* Lock the SuperIO chip */
  222. outb_p(LOCK_DATA,IO_INDEX_PORT);
  223. spin_unlock_irqrestore(&spinlock, flags);
  224. *status = 0;
  225. if (new_status & 1)
  226. *status |= WDIOF_CARDRESET;
  227. return 0;
  228. }
  229. /*
  230. * /dev/watchdog handling
  231. */
  232. static int wdt_open(struct inode *inode, struct file *file)
  233. {
  234. /* If the watchdog is alive we don't need to start it again */
  235. if( test_and_set_bit(0, &timer_alive) )
  236. return -EBUSY;
  237. if (nowayout)
  238. __module_get(THIS_MODULE);
  239. wdt_start();
  240. return nonseekable_open(inode, file);
  241. }
  242. static int wdt_release(struct inode *inode, struct file *file)
  243. {
  244. /*
  245. * Shut off the timer.
  246. * Lock it in if it's a module and we set nowayout
  247. */
  248. if (expect_close == 42)
  249. {
  250. wdt_stop();
  251. clear_bit(0, &timer_alive);
  252. } else {
  253. wdt_keepalive();
  254. printk(KERN_CRIT PFX "unexpected close, not stopping watchdog!\n");
  255. }
  256. expect_close = 0;
  257. return 0;
  258. }
  259. /*
  260. * wdt_write:
  261. * @file: file handle to the watchdog
  262. * @buf: buffer to write (unused as data does not matter here
  263. * @count: count of bytes
  264. * @ppos: pointer to the position to write. No seeks allowed
  265. *
  266. * A write to a watchdog device is defined as a keepalive signal. Any
  267. * write of data will do, as we we don't define content meaning.
  268. */
  269. static ssize_t wdt_write(struct file *file, const char __user *buf,
  270. size_t count, loff_t *ppos)
  271. {
  272. /* See if we got the magic character 'V' and reload the timer */
  273. if(count)
  274. {
  275. if (!nowayout)
  276. {
  277. size_t ofs;
  278. /* note: just in case someone wrote the magic character long ago */
  279. expect_close = 0;
  280. /* scan to see whether or not we got the magic character */
  281. for(ofs = 0; ofs != count; ofs++)
  282. {
  283. char c;
  284. if (get_user(c, buf + ofs))
  285. return -EFAULT;
  286. if (c == 'V') {
  287. expect_close = 42;
  288. }
  289. }
  290. }
  291. /* someone wrote to us, we should restart timer */
  292. wdt_keepalive();
  293. }
  294. return count;
  295. }
  296. /*
  297. * wdt_ioctl:
  298. * @inode: inode of the device
  299. * @file: file handle to the device
  300. * @cmd: watchdog command
  301. * @arg: argument pointer
  302. *
  303. * The watchdog API defines a common set of functions for all watchdogs
  304. * according to their available features.
  305. */
  306. static struct watchdog_info ident = {
  307. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  308. .firmware_version = 1,
  309. .identity = WATCHDOG_NAME,
  310. };
  311. static int wdt_ioctl(struct inode *inode, struct file *file,
  312. unsigned int cmd, unsigned long arg)
  313. {
  314. int status;
  315. int new_options, retval = -EINVAL;
  316. int new_timeout;
  317. union {
  318. struct watchdog_info __user *ident;
  319. int __user *i;
  320. } uarg;
  321. uarg.i = (int __user *)arg;
  322. switch(cmd)
  323. {
  324. default:
  325. return -ENOTTY;
  326. case WDIOC_GETSUPPORT:
  327. return copy_to_user(uarg.ident, &ident, sizeof(ident)) ? -EFAULT : 0;
  328. case WDIOC_GETSTATUS:
  329. wdt_get_status(&status);
  330. return put_user(status, uarg.i);
  331. case WDIOC_GETBOOTSTATUS:
  332. return put_user(0, uarg.i);
  333. case WDIOC_KEEPALIVE:
  334. wdt_keepalive();
  335. return 0;
  336. case WDIOC_SETOPTIONS:
  337. if (get_user (new_options, uarg.i))
  338. return -EFAULT;
  339. if (new_options & WDIOS_DISABLECARD) {
  340. wdt_stop();
  341. retval = 0;
  342. }
  343. if (new_options & WDIOS_ENABLECARD) {
  344. wdt_start();
  345. retval = 0;
  346. }
  347. return retval;
  348. case WDIOC_SETTIMEOUT:
  349. if (get_user(new_timeout, uarg.i))
  350. return -EFAULT;
  351. if (wdt_set_timeout(new_timeout))
  352. return -EINVAL;
  353. wdt_keepalive();
  354. /* Fall */
  355. case WDIOC_GETTIMEOUT:
  356. return put_user(timeout, uarg.i);
  357. }
  358. }
  359. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  360. void *unused)
  361. {
  362. if (code==SYS_DOWN || code==SYS_HALT)
  363. wdt_stop();
  364. return NOTIFY_DONE;
  365. }
  366. static const struct file_operations wdt_fops=
  367. {
  368. .owner = THIS_MODULE,
  369. .llseek = no_llseek,
  370. .write = wdt_write,
  371. .ioctl = wdt_ioctl,
  372. .open = wdt_open,
  373. .release = wdt_release,
  374. };
  375. static struct miscdevice wdt_miscdev=
  376. {
  377. .minor = WATCHDOG_MINOR,
  378. .name = "watchdog",
  379. .fops = &wdt_fops,
  380. };
  381. static struct notifier_block wdt_notifier = {
  382. .notifier_call = wdt_notify_sys,
  383. };
  384. static int __init w83977f_wdt_init(void)
  385. {
  386. int rc;
  387. printk(KERN_INFO PFX DRIVER_VERSION);
  388. /*
  389. * Check that the timeout value is within it's range ;
  390. * if not reset to the default
  391. */
  392. if (wdt_set_timeout(timeout)) {
  393. wdt_set_timeout(DEFAULT_TIMEOUT);
  394. printk(KERN_INFO PFX "timeout value must be 15<=timeout<=7635, using %d\n",
  395. DEFAULT_TIMEOUT);
  396. }
  397. if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME))
  398. {
  399. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  400. IO_INDEX_PORT);
  401. rc = -EIO;
  402. goto err_out;
  403. }
  404. rc = misc_register(&wdt_miscdev);
  405. if (rc)
  406. {
  407. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  408. wdt_miscdev.minor, rc);
  409. goto err_out_region;
  410. }
  411. rc = register_reboot_notifier(&wdt_notifier);
  412. if (rc)
  413. {
  414. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  415. rc);
  416. goto err_out_miscdev;
  417. }
  418. printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
  419. timeout, nowayout, testmode);
  420. return 0;
  421. err_out_miscdev:
  422. misc_deregister(&wdt_miscdev);
  423. err_out_region:
  424. release_region(IO_INDEX_PORT,2);
  425. err_out:
  426. return rc;
  427. }
  428. static void __exit w83977f_wdt_exit(void)
  429. {
  430. wdt_stop();
  431. misc_deregister(&wdt_miscdev);
  432. unregister_reboot_notifier(&wdt_notifier);
  433. release_region(IO_INDEX_PORT,2);
  434. }
  435. module_init(w83977f_wdt_init);
  436. module_exit(w83977f_wdt_exit);
  437. MODULE_AUTHOR("Jose Goncalves <jose.goncalves@inov.pt>");
  438. MODULE_DESCRIPTION("Driver for watchdog timer in W83977F I/O chip");
  439. MODULE_LICENSE("GPL");
  440. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);