w83977f_wdt.c 12 KB

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