w83697hf_wdt.c 8.0 KB

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