w83697ug_wdt.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/module.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/types.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/watchdog.h>
  38. #include <linux/fs.h>
  39. #include <linux/ioport.h>
  40. #include <linux/notifier.h>
  41. #include <linux/reboot.h>
  42. #include <linux/init.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/io.h>
  45. #include <linux/uaccess.h>
  46. #include <asm/system.h>
  47. #define WATCHDOG_NAME "w83697ug/uf WDT"
  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. pr_info("Watchdog chip version 0x%02x = W83697UG/UF found at 0x%04x\n",
  82. 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. pr_err("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. pr_info("Watchdog already running. Resetting timeout to %d sec\n",
  114. 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. pr_crit("Unexpected close, not stopping watchdog!\n");
  241. wdt_ping();
  242. }
  243. expect_close = 0;
  244. clear_bit(0, &wdt_is_open);
  245. return 0;
  246. }
  247. /*
  248. * Notifier for system down
  249. */
  250. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  251. void *unused)
  252. {
  253. if (code == SYS_DOWN || code == SYS_HALT)
  254. wdt_disable(); /* Turn the WDT off */
  255. return NOTIFY_DONE;
  256. }
  257. /*
  258. * Kernel Interfaces
  259. */
  260. static const struct file_operations wdt_fops = {
  261. .owner = THIS_MODULE,
  262. .llseek = no_llseek,
  263. .write = wdt_write,
  264. .unlocked_ioctl = wdt_ioctl,
  265. .open = wdt_open,
  266. .release = wdt_close,
  267. };
  268. static struct miscdevice wdt_miscdev = {
  269. .minor = WATCHDOG_MINOR,
  270. .name = "watchdog",
  271. .fops = &wdt_fops,
  272. };
  273. /*
  274. * The WDT needs to learn about soft shutdowns in order to
  275. * turn the timebomb registers off.
  276. */
  277. static struct notifier_block wdt_notifier = {
  278. .notifier_call = wdt_notify_sys,
  279. };
  280. static int __init wdt_init(void)
  281. {
  282. int ret;
  283. pr_info("WDT driver for the Winbond(TM) W83697UG/UF Super I/O chip initialising\n");
  284. if (wdt_set_heartbeat(timeout)) {
  285. wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  286. pr_info("timeout value must be 1<=timeout<=255, using %d\n",
  287. WATCHDOG_TIMEOUT);
  288. }
  289. if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
  290. pr_err("I/O address 0x%04x already in use\n", wdt_io);
  291. ret = -EIO;
  292. goto out;
  293. }
  294. ret = w83697ug_init();
  295. if (ret != 0)
  296. goto unreg_regions;
  297. ret = register_reboot_notifier(&wdt_notifier);
  298. if (ret != 0) {
  299. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  300. goto unreg_regions;
  301. }
  302. ret = misc_register(&wdt_miscdev);
  303. if (ret != 0) {
  304. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  305. WATCHDOG_MINOR, ret);
  306. goto unreg_reboot;
  307. }
  308. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  309. timeout, nowayout);
  310. out:
  311. return ret;
  312. unreg_reboot:
  313. unregister_reboot_notifier(&wdt_notifier);
  314. unreg_regions:
  315. release_region(wdt_io, 1);
  316. goto out;
  317. }
  318. static void __exit wdt_exit(void)
  319. {
  320. misc_deregister(&wdt_miscdev);
  321. unregister_reboot_notifier(&wdt_notifier);
  322. release_region(wdt_io, 1);
  323. }
  324. module_init(wdt_init);
  325. module_exit(wdt_exit);
  326. MODULE_LICENSE("GPL");
  327. MODULE_AUTHOR("Flemming Frandsen <ff@nrvissing.net>");
  328. MODULE_DESCRIPTION("w83697ug/uf WDT driver");
  329. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);