w83697hf_wdt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 WDT io port (default 0x2E)");
  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 void
  69. w83697hf_select_wd_register(void)
  70. {
  71. w83697hf_unlock();
  72. outb_p(0x29, W83697HF_EFER); /* select CR29 */
  73. outb_p(0x20, W83697HF_EFDR); /* select WDTO */
  74. outb_p(0x07, W83697HF_EFER); /* point to logical device number reg */
  75. outb_p(0x08, W83697HF_EFDR); /* select logical device 8 (GPIO2) */
  76. outb_p(0x30, W83697HF_EFER); /* select CR30 */
  77. outb_p(0x01, W83697HF_EFDR); /* set bit 0 to activate GPIO2 */
  78. }
  79. static void
  80. w83697hf_unselect_wd_register(void)
  81. {
  82. outb_p(0xAA, W83697HF_EFER); /* Leave extended function mode */
  83. }
  84. static void
  85. w83697hf_init(void)
  86. {
  87. unsigned char t;
  88. w83697hf_select_wd_register();
  89. outb_p(0xF3, W83697HF_EFER); /* Select CRF3 */
  90. t=inb_p(W83697HF_EFDR); /* read CRF3 */
  91. if (t != 0) {
  92. printk (KERN_INFO PFX "Watchdog already running. Resetting timeout to %d sec\n", timeout);
  93. outb_p(timeout, W83697HF_EFDR); /* Write back to CRF3 */
  94. }
  95. outb_p(0xF4, W83697HF_EFER); /* Select CRF4 */
  96. t=inb_p(W83697HF_EFDR); /* read CRF4 */
  97. t&=~0x0C; /* set second mode & disable keyboard turning off watchdog */
  98. outb_p(t, W83697HF_EFDR); /* Write back to CRF4 */
  99. w83697hf_unselect_wd_register();
  100. }
  101. static void
  102. wdt_ctrl(int timeout)
  103. {
  104. spin_lock(&io_lock);
  105. w83697hf_select_wd_register();
  106. outb_p(0xF4, W83697HF_EFER); /* Select CRF4 */
  107. outb_p(timeout, W83697HF_EFDR); /* Write Timeout counter to CRF4 */
  108. w83697hf_unselect_wd_register();
  109. spin_unlock(&io_lock);
  110. }
  111. static int
  112. wdt_ping(void)
  113. {
  114. wdt_ctrl(timeout);
  115. return 0;
  116. }
  117. static int
  118. wdt_disable(void)
  119. {
  120. wdt_ctrl(0);
  121. return 0;
  122. }
  123. static int
  124. wdt_set_heartbeat(int t)
  125. {
  126. if ((t < 1) || (t > 255))
  127. return -EINVAL;
  128. timeout = t;
  129. return 0;
  130. }
  131. static ssize_t
  132. wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  133. {
  134. if (count) {
  135. if (!nowayout) {
  136. size_t i;
  137. expect_close = 0;
  138. for (i = 0; i != count; i++) {
  139. char c;
  140. if (get_user(c, buf+i))
  141. return -EFAULT;
  142. if (c == 'V')
  143. expect_close = 42;
  144. }
  145. }
  146. wdt_ping();
  147. }
  148. return count;
  149. }
  150. static int
  151. wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  152. unsigned long arg)
  153. {
  154. void __user *argp = (void __user *)arg;
  155. int __user *p = argp;
  156. int new_timeout;
  157. static struct watchdog_info ident = {
  158. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  159. .firmware_version = 1,
  160. .identity = "W83697HF WDT",
  161. };
  162. switch (cmd) {
  163. case WDIOC_GETSUPPORT:
  164. if (copy_to_user(argp, &ident, sizeof(ident)))
  165. return -EFAULT;
  166. break;
  167. case WDIOC_GETSTATUS:
  168. case WDIOC_GETBOOTSTATUS:
  169. return put_user(0, p);
  170. case WDIOC_KEEPALIVE:
  171. wdt_ping();
  172. break;
  173. case WDIOC_SETTIMEOUT:
  174. if (get_user(new_timeout, p))
  175. return -EFAULT;
  176. if (wdt_set_heartbeat(new_timeout))
  177. return -EINVAL;
  178. wdt_ping();
  179. /* Fall */
  180. case WDIOC_GETTIMEOUT:
  181. return put_user(timeout, p);
  182. case WDIOC_SETOPTIONS:
  183. {
  184. int options, retval = -EINVAL;
  185. if (get_user(options, p))
  186. return -EFAULT;
  187. if (options & WDIOS_DISABLECARD) {
  188. wdt_disable();
  189. retval = 0;
  190. }
  191. if (options & WDIOS_ENABLECARD) {
  192. wdt_ping();
  193. retval = 0;
  194. }
  195. return retval;
  196. }
  197. default:
  198. return -ENOTTY;
  199. }
  200. return 0;
  201. }
  202. static int
  203. wdt_open(struct inode *inode, struct file *file)
  204. {
  205. if (test_and_set_bit(0, &wdt_is_open))
  206. return -EBUSY;
  207. /*
  208. * Activate
  209. */
  210. wdt_ping();
  211. return nonseekable_open(inode, file);
  212. }
  213. static int
  214. wdt_close(struct inode *inode, struct file *file)
  215. {
  216. if (expect_close == 42) {
  217. wdt_disable();
  218. } else {
  219. printk (KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  220. wdt_ping();
  221. }
  222. expect_close = 0;
  223. clear_bit(0, &wdt_is_open);
  224. return 0;
  225. }
  226. /*
  227. * Notifier for system down
  228. */
  229. static int
  230. wdt_notify_sys(struct notifier_block *this, unsigned long code,
  231. void *unused)
  232. {
  233. if (code == SYS_DOWN || code == SYS_HALT) {
  234. /* Turn the WDT off */
  235. wdt_disable();
  236. }
  237. return NOTIFY_DONE;
  238. }
  239. /*
  240. * Kernel Interfaces
  241. */
  242. static struct file_operations wdt_fops = {
  243. .owner = THIS_MODULE,
  244. .llseek = no_llseek,
  245. .write = wdt_write,
  246. .ioctl = wdt_ioctl,
  247. .open = wdt_open,
  248. .release = wdt_close,
  249. };
  250. static struct miscdevice wdt_miscdev = {
  251. .minor = WATCHDOG_MINOR,
  252. .name = "watchdog",
  253. .fops = &wdt_fops,
  254. };
  255. /*
  256. * The WDT needs to learn about soft shutdowns in order to
  257. * turn the timebomb registers off.
  258. */
  259. static struct notifier_block wdt_notifier = {
  260. .notifier_call = wdt_notify_sys,
  261. };
  262. static int __init
  263. wdt_init(void)
  264. {
  265. int ret;
  266. spin_lock_init(&io_lock);
  267. printk (KERN_INFO PFX "WDT driver for W83697HF/HG initializing\n");
  268. if (wdt_set_heartbeat(timeout)) {
  269. wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  270. printk (KERN_INFO PFX "timeout value must be 1<=timeout<=255, using %d\n",
  271. WATCHDOG_TIMEOUT);
  272. }
  273. if (!request_region(wdt_io, 2, WATCHDOG_NAME)) {
  274. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  275. wdt_io);
  276. ret = -EIO;
  277. goto out;
  278. }
  279. w83697hf_init();
  280. ret = register_reboot_notifier(&wdt_notifier);
  281. if (ret != 0) {
  282. printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  283. ret);
  284. goto unreg_regions;
  285. }
  286. ret = misc_register(&wdt_miscdev);
  287. if (ret != 0) {
  288. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  289. WATCHDOG_MINOR, ret);
  290. goto unreg_reboot;
  291. }
  292. printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  293. timeout, nowayout);
  294. out:
  295. return ret;
  296. unreg_reboot:
  297. unregister_reboot_notifier(&wdt_notifier);
  298. unreg_regions:
  299. release_region(wdt_io, 2);
  300. goto out;
  301. }
  302. static void __exit
  303. wdt_exit(void)
  304. {
  305. misc_deregister(&wdt_miscdev);
  306. unregister_reboot_notifier(&wdt_notifier);
  307. release_region(wdt_io, 2);
  308. }
  309. module_init(wdt_init);
  310. module_exit(wdt_exit);
  311. MODULE_LICENSE("GPL");
  312. MODULE_AUTHOR("Marcus Junker <junker@anduras.de>");
  313. MODULE_DESCRIPTION("w83697hf/hg WDT driver");
  314. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);