w83697hf_wdt.c 8.0 KB

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