w83627hf_wdt.c 8.5 KB

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