w83697hf_wdt.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * w83697hf/hg WDT driver
  3. *
  4. * (c) Copyright 2006 Marcus Junker <junker@anduras.de>
  5. *
  6. * Based on w83627hf_wdt.c which is based on advantechwdt.c
  7. * which is based on wdt.c.
  8. * Original copyright messages:
  9. *
  10. * (c) Copyright 2003 Pádraig Brady <P@draigBrady.com>
  11. *
  12. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  13. *
  14. * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
  15. * http://www.redhat.com
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. *
  22. * Neither Marcus Junker nor ANDURAS AG admit liability nor provide
  23. * warranty for any of this software. This material is provided
  24. * "AS-IS" and at no charge.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/types.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/watchdog.h>
  31. #include <linux/fs.h>
  32. #include <linux/ioport.h>
  33. #include <linux/notifier.h>
  34. #include <linux/reboot.h>
  35. #include <linux/init.h>
  36. #include <linux/spinlock.h>
  37. #include <asm/io.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/system.h>
  40. #define WATCHDOG_NAME "w83697hf/hg WDT"
  41. #define PFX WATCHDOG_NAME ": "
  42. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  43. static unsigned long wdt_is_open;
  44. static char expect_close;
  45. static spinlock_t io_lock;
  46. /* You must set this - there is no sane way to probe for this board. */
  47. static int wdt_io = 0x2e;
  48. module_param(wdt_io, int, 0);
  49. MODULE_PARM_DESC(wdt_io, "w83697hf/hg WDT io port (default 0x2e, 0 = autodetect)");
  50. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  51. module_param(timeout, int, 0);
  52. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=255, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  53. static int nowayout = WATCHDOG_NOWAYOUT;
  54. module_param(nowayout, int, 0);
  55. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  56. /*
  57. * Kernel methods.
  58. */
  59. #define W83697HF_EFER (wdt_io+0) /* Extended Function Enable Register */
  60. #define W83697HF_EFIR (wdt_io+0) /* Extended Function Index Register (same as EFER) */
  61. #define W83697HF_EFDR (wdt_io+1) /* Extended Function Data Register */
  62. static inline void
  63. w83697hf_unlock(void)
  64. {
  65. outb_p(0x87, W83697HF_EFER); /* Enter extended function mode */
  66. outb_p(0x87, W83697HF_EFER); /* Again according to manual */
  67. }
  68. static inline void
  69. w83697hf_lock(void)
  70. {
  71. outb_p(0xAA, W83697HF_EFER); /* Leave extended function mode */
  72. }
  73. /*
  74. * The three functions w83697hf_get_reg(), w83697hf_set_reg() and
  75. * w83697hf_write_timeout() must be called with the device unlocked.
  76. */
  77. static unsigned char
  78. w83697hf_get_reg(unsigned char reg)
  79. {
  80. outb_p(reg, W83697HF_EFIR);
  81. return inb_p(W83697HF_EFDR);
  82. }
  83. static void
  84. w83697hf_set_reg(unsigned char reg, unsigned char data)
  85. {
  86. outb_p(reg, W83697HF_EFIR);
  87. outb_p(data, W83697HF_EFDR);
  88. }
  89. static void
  90. w83697hf_write_timeout(int timeout)
  91. {
  92. w83697hf_set_reg(0xF4, timeout); /* Write Timeout counter to CRF4 */
  93. }
  94. static void
  95. w83697hf_select_wdt(void)
  96. {
  97. w83697hf_unlock();
  98. w83697hf_set_reg(0x07, 0x08); /* Switch to logic device 8 (GPIO2) */
  99. }
  100. static inline void
  101. w83697hf_deselect_wdt(void)
  102. {
  103. w83697hf_lock();
  104. }
  105. static void
  106. w83697hf_init(void)
  107. {
  108. unsigned char t;
  109. w83697hf_select_wdt();
  110. w83697hf_set_reg(0x29, 0x20); /* Set pin 119 to WDTO# mode (= CR29, WDT0) */
  111. t = w83697hf_get_reg(0xF3); /* Read CRF3 */
  112. if (t != 0) {
  113. printk (KERN_INFO PFX "Watchdog already running. Resetting timeout to %d sec\n", timeout);
  114. w83697hf_set_reg(0xF3, timeout); /* Write new timeout */
  115. }
  116. t = w83697hf_get_reg(0xF4); /* Read CRF4 */
  117. t&=~0x0C; /* set second mode & disable keyboard turning off watchdog */
  118. w83697hf_set_reg(0xF4, t); /* Write back to CRF4 */
  119. w83697hf_deselect_wdt();
  120. }
  121. static int
  122. wdt_ping(void)
  123. {
  124. spin_lock(&io_lock);
  125. w83697hf_select_wdt();
  126. w83697hf_write_timeout(timeout);
  127. w83697hf_deselect_wdt();
  128. spin_unlock(&io_lock);
  129. return 0;
  130. }
  131. static int
  132. wdt_enable(void)
  133. {
  134. spin_lock(&io_lock);
  135. w83697hf_select_wdt();
  136. w83697hf_write_timeout(timeout);
  137. w83697hf_set_reg(0x30, 1); /* Enable timer */
  138. w83697hf_deselect_wdt();
  139. spin_unlock(&io_lock);
  140. return 0;
  141. }
  142. static int
  143. wdt_disable(void)
  144. {
  145. spin_lock(&io_lock);
  146. w83697hf_select_wdt();
  147. w83697hf_set_reg(0x30, 0); /* Disable timer */
  148. w83697hf_write_timeout(0);
  149. w83697hf_deselect_wdt();
  150. spin_unlock(&io_lock);
  151. return 0;
  152. }
  153. static int
  154. wdt_set_heartbeat(int t)
  155. {
  156. if ((t < 1) || (t > 255))
  157. return -EINVAL;
  158. timeout = t;
  159. return 0;
  160. }
  161. static ssize_t
  162. wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  163. {
  164. if (count) {
  165. if (!nowayout) {
  166. size_t i;
  167. expect_close = 0;
  168. for (i = 0; i != count; i++) {
  169. char c;
  170. if (get_user(c, buf+i))
  171. return -EFAULT;
  172. if (c == 'V')
  173. expect_close = 42;
  174. }
  175. }
  176. wdt_ping();
  177. }
  178. return count;
  179. }
  180. static int
  181. wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  182. unsigned long arg)
  183. {
  184. void __user *argp = (void __user *)arg;
  185. int __user *p = argp;
  186. int new_timeout;
  187. static struct watchdog_info ident = {
  188. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  189. .firmware_version = 1,
  190. .identity = "W83697HF WDT",
  191. };
  192. switch (cmd) {
  193. case WDIOC_GETSUPPORT:
  194. if (copy_to_user(argp, &ident, sizeof(ident)))
  195. return -EFAULT;
  196. break;
  197. case WDIOC_GETSTATUS:
  198. case WDIOC_GETBOOTSTATUS:
  199. return put_user(0, p);
  200. case WDIOC_KEEPALIVE:
  201. wdt_ping();
  202. break;
  203. case WDIOC_SETTIMEOUT:
  204. if (get_user(new_timeout, p))
  205. return -EFAULT;
  206. if (wdt_set_heartbeat(new_timeout))
  207. return -EINVAL;
  208. wdt_ping();
  209. /* Fall */
  210. case WDIOC_GETTIMEOUT:
  211. return put_user(timeout, p);
  212. case WDIOC_SETOPTIONS:
  213. {
  214. int options, retval = -EINVAL;
  215. if (get_user(options, p))
  216. return -EFAULT;
  217. if (options & WDIOS_DISABLECARD) {
  218. wdt_disable();
  219. retval = 0;
  220. }
  221. if (options & WDIOS_ENABLECARD) {
  222. wdt_enable();
  223. retval = 0;
  224. }
  225. return retval;
  226. }
  227. default:
  228. return -ENOTTY;
  229. }
  230. return 0;
  231. }
  232. static int
  233. wdt_open(struct inode *inode, struct file *file)
  234. {
  235. if (test_and_set_bit(0, &wdt_is_open))
  236. return -EBUSY;
  237. /*
  238. * Activate
  239. */
  240. wdt_enable();
  241. return nonseekable_open(inode, file);
  242. }
  243. static int
  244. wdt_close(struct inode *inode, struct file *file)
  245. {
  246. if (expect_close == 42) {
  247. wdt_disable();
  248. } else {
  249. printk (KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  250. wdt_ping();
  251. }
  252. expect_close = 0;
  253. clear_bit(0, &wdt_is_open);
  254. return 0;
  255. }
  256. /*
  257. * Notifier for system down
  258. */
  259. static int
  260. wdt_notify_sys(struct notifier_block *this, unsigned long code,
  261. void *unused)
  262. {
  263. if (code == SYS_DOWN || code == SYS_HALT) {
  264. /* Turn the WDT off */
  265. wdt_disable();
  266. }
  267. return NOTIFY_DONE;
  268. }
  269. /*
  270. * Kernel Interfaces
  271. */
  272. static struct file_operations wdt_fops = {
  273. .owner = THIS_MODULE,
  274. .llseek = no_llseek,
  275. .write = wdt_write,
  276. .ioctl = wdt_ioctl,
  277. .open = wdt_open,
  278. .release = wdt_close,
  279. };
  280. static struct miscdevice wdt_miscdev = {
  281. .minor = WATCHDOG_MINOR,
  282. .name = "watchdog",
  283. .fops = &wdt_fops,
  284. };
  285. /*
  286. * The WDT needs to learn about soft shutdowns in order to
  287. * turn the timebomb registers off.
  288. */
  289. static struct notifier_block wdt_notifier = {
  290. .notifier_call = wdt_notify_sys,
  291. };
  292. static int
  293. w83697hf_check_wdt(void)
  294. {
  295. if (!request_region(wdt_io, 2, WATCHDOG_NAME)) {
  296. printk (KERN_ERR PFX "I/O address 0x%x already in use\n", wdt_io);
  297. return -EIO;
  298. }
  299. printk (KERN_DEBUG PFX "Looking for watchdog at address 0x%x\n", wdt_io);
  300. w83697hf_unlock();
  301. if (w83697hf_get_reg(0x20) == 0x60) {
  302. printk (KERN_INFO PFX "watchdog found at address 0x%x\n", wdt_io);
  303. w83697hf_lock();
  304. return 0;
  305. }
  306. w83697hf_lock(); /* Reprotect in case it was a compatible device */
  307. printk (KERN_INFO PFX "watchdog not found at address 0x%x\n", wdt_io);
  308. release_region(wdt_io, 2);
  309. return -EIO;
  310. }
  311. static int __init
  312. wdt_init(void)
  313. {
  314. int ret, autodetect;
  315. spin_lock_init(&io_lock);
  316. printk (KERN_INFO PFX "WDT driver for W83697HF/HG initializing\n");
  317. autodetect = wdt_io == 0;
  318. if (autodetect)
  319. wdt_io = 0x2e;
  320. if (!w83697hf_check_wdt())
  321. goto found;
  322. if (autodetect) {
  323. wdt_io = 0x4e;
  324. if (!w83697hf_check_wdt())
  325. goto found;
  326. }
  327. printk (KERN_ERR PFX "No W83697HF/HG could be found\n");
  328. ret = -EIO;
  329. goto out;
  330. found:
  331. w83697hf_init();
  332. wdt_disable(); /* Disable watchdog until first use */
  333. if (wdt_set_heartbeat(timeout)) {
  334. wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  335. printk (KERN_INFO PFX "timeout value must be 1<=timeout<=255, using %d\n",
  336. WATCHDOG_TIMEOUT);
  337. }
  338. ret = register_reboot_notifier(&wdt_notifier);
  339. if (ret != 0) {
  340. printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  341. ret);
  342. goto unreg_regions;
  343. }
  344. ret = misc_register(&wdt_miscdev);
  345. if (ret != 0) {
  346. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  347. WATCHDOG_MINOR, ret);
  348. goto unreg_reboot;
  349. }
  350. printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  351. timeout, nowayout);
  352. out:
  353. return ret;
  354. unreg_reboot:
  355. unregister_reboot_notifier(&wdt_notifier);
  356. unreg_regions:
  357. release_region(wdt_io, 2);
  358. goto out;
  359. }
  360. static void __exit
  361. wdt_exit(void)
  362. {
  363. misc_deregister(&wdt_miscdev);
  364. unregister_reboot_notifier(&wdt_notifier);
  365. release_region(wdt_io, 2);
  366. }
  367. module_init(wdt_init);
  368. module_exit(wdt_exit);
  369. MODULE_LICENSE("GPL");
  370. MODULE_AUTHOR("Marcus Junker <junker@anduras.de>");
  371. MODULE_DESCRIPTION("w83697hf/hg WDT driver");
  372. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);