w83697ug_wdt.c 8.7 KB

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