w83977f_wdt.c 12 KB

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