w83697ug_wdt.c 8.5 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. #define WATCHDOG_NAME "w83697ug/uf WDT"
  47. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  48. static unsigned long wdt_is_open;
  49. static char expect_close;
  50. static DEFINE_SPINLOCK(io_lock);
  51. static int wdt_io = 0x2e;
  52. module_param(wdt_io, int, 0);
  53. MODULE_PARM_DESC(wdt_io, "w83697ug/uf WDT io port (default 0x2e)");
  54. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  55. module_param(timeout, int, 0);
  56. MODULE_PARM_DESC(timeout,
  57. "Watchdog timeout in seconds. 1<= timeout <=255 (default="
  58. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  59. static bool nowayout = WATCHDOG_NOWAYOUT;
  60. module_param(nowayout, bool, 0);
  61. MODULE_PARM_DESC(nowayout,
  62. "Watchdog cannot be stopped once started (default="
  63. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  64. /*
  65. * Kernel methods.
  66. */
  67. #define WDT_EFER (wdt_io+0) /* Extended Function Enable Registers */
  68. #define WDT_EFIR (wdt_io+0) /* Extended Function Index Register
  69. (same as EFER) */
  70. #define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
  71. static int w83697ug_select_wd_register(void)
  72. {
  73. unsigned char c;
  74. unsigned char version;
  75. outb_p(0x87, WDT_EFER); /* Enter extended function mode */
  76. outb_p(0x87, WDT_EFER); /* Again according to manual */
  77. outb(0x20, WDT_EFER); /* check chip version */
  78. version = inb(WDT_EFDR);
  79. if (version == 0x68) { /* W83697UG */
  80. pr_info("Watchdog chip version 0x%02x = W83697UG/UF found at 0x%04x\n",
  81. version, wdt_io);
  82. outb_p(0x2b, WDT_EFER);
  83. c = inb_p(WDT_EFDR); /* select WDT0 */
  84. c &= ~0x04;
  85. outb_p(0x2b, WDT_EFER);
  86. outb_p(c, WDT_EFDR); /* set pin118 to WDT0 */
  87. } else {
  88. pr_err("No W83697UG/UF could be found\n");
  89. return -ENODEV;
  90. }
  91. outb_p(0x07, WDT_EFER); /* point to logical device number reg */
  92. outb_p(0x08, WDT_EFDR); /* select logical device 8 (GPIO2) */
  93. outb_p(0x30, WDT_EFER); /* select CR30 */
  94. c = inb_p(WDT_EFDR);
  95. outb_p(c | 0x01, WDT_EFDR); /* set bit 0 to activate GPIO2 */
  96. return 0;
  97. }
  98. static void w83697ug_unselect_wd_register(void)
  99. {
  100. outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
  101. }
  102. static int w83697ug_init(void)
  103. {
  104. int ret;
  105. unsigned char t;
  106. ret = w83697ug_select_wd_register();
  107. if (ret != 0)
  108. return ret;
  109. outb_p(0xF6, WDT_EFER); /* Select CRF6 */
  110. t = inb_p(WDT_EFDR); /* read CRF6 */
  111. if (t != 0) {
  112. pr_info("Watchdog already running. Resetting timeout to %d sec\n",
  113. timeout);
  114. outb_p(timeout, WDT_EFDR); /* Write back to CRF6 */
  115. }
  116. outb_p(0xF5, WDT_EFER); /* Select CRF5 */
  117. t = inb_p(WDT_EFDR); /* read CRF5 */
  118. t &= ~0x0C; /* set second mode &
  119. disable keyboard turning off watchdog */
  120. outb_p(t, WDT_EFDR); /* Write back to CRF5 */
  121. w83697ug_unselect_wd_register();
  122. return 0;
  123. }
  124. static void wdt_ctrl(int timeout)
  125. {
  126. spin_lock(&io_lock);
  127. if (w83697ug_select_wd_register() < 0) {
  128. spin_unlock(&io_lock);
  129. return;
  130. }
  131. outb_p(0xF4, WDT_EFER); /* Select CRF4 */
  132. outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF4 */
  133. w83697ug_unselect_wd_register();
  134. spin_unlock(&io_lock);
  135. }
  136. static int wdt_ping(void)
  137. {
  138. wdt_ctrl(timeout);
  139. return 0;
  140. }
  141. static int wdt_disable(void)
  142. {
  143. wdt_ctrl(0);
  144. return 0;
  145. }
  146. static int wdt_set_heartbeat(int t)
  147. {
  148. if (t < 1 || t > 255)
  149. return -EINVAL;
  150. timeout = t;
  151. return 0;
  152. }
  153. static ssize_t wdt_write(struct file *file, const char __user *buf,
  154. size_t count, loff_t *ppos)
  155. {
  156. if (count) {
  157. if (!nowayout) {
  158. size_t i;
  159. expect_close = 0;
  160. for (i = 0; i != count; i++) {
  161. char c;
  162. if (get_user(c, buf + i))
  163. return -EFAULT;
  164. if (c == 'V')
  165. expect_close = 42;
  166. }
  167. }
  168. wdt_ping();
  169. }
  170. return count;
  171. }
  172. static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  173. {
  174. void __user *argp = (void __user *)arg;
  175. int __user *p = argp;
  176. int new_timeout;
  177. static const struct watchdog_info ident = {
  178. .options = WDIOF_KEEPALIVEPING |
  179. WDIOF_SETTIMEOUT |
  180. WDIOF_MAGICCLOSE,
  181. .firmware_version = 1,
  182. .identity = "W83697UG WDT",
  183. };
  184. switch (cmd) {
  185. case WDIOC_GETSUPPORT:
  186. if (copy_to_user(argp, &ident, sizeof(ident)))
  187. return -EFAULT;
  188. break;
  189. case WDIOC_GETSTATUS:
  190. case WDIOC_GETBOOTSTATUS:
  191. return put_user(0, 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. case WDIOC_KEEPALIVE:
  208. wdt_ping();
  209. break;
  210. case WDIOC_SETTIMEOUT:
  211. if (get_user(new_timeout, p))
  212. return -EFAULT;
  213. if (wdt_set_heartbeat(new_timeout))
  214. return -EINVAL;
  215. wdt_ping();
  216. /* Fall */
  217. case WDIOC_GETTIMEOUT:
  218. return put_user(timeout, p);
  219. default:
  220. return -ENOTTY;
  221. }
  222. return 0;
  223. }
  224. static int wdt_open(struct inode *inode, struct file *file)
  225. {
  226. if (test_and_set_bit(0, &wdt_is_open))
  227. return -EBUSY;
  228. /*
  229. * Activate
  230. */
  231. wdt_ping();
  232. return nonseekable_open(inode, file);
  233. }
  234. static int wdt_close(struct inode *inode, struct file *file)
  235. {
  236. if (expect_close == 42)
  237. wdt_disable();
  238. else {
  239. pr_crit("Unexpected close, not stopping watchdog!\n");
  240. wdt_ping();
  241. }
  242. expect_close = 0;
  243. clear_bit(0, &wdt_is_open);
  244. return 0;
  245. }
  246. /*
  247. * Notifier for system down
  248. */
  249. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  250. void *unused)
  251. {
  252. if (code == SYS_DOWN || code == SYS_HALT)
  253. wdt_disable(); /* Turn the WDT off */
  254. return NOTIFY_DONE;
  255. }
  256. /*
  257. * Kernel Interfaces
  258. */
  259. static const struct file_operations wdt_fops = {
  260. .owner = THIS_MODULE,
  261. .llseek = no_llseek,
  262. .write = wdt_write,
  263. .unlocked_ioctl = wdt_ioctl,
  264. .open = wdt_open,
  265. .release = wdt_close,
  266. };
  267. static struct miscdevice wdt_miscdev = {
  268. .minor = WATCHDOG_MINOR,
  269. .name = "watchdog",
  270. .fops = &wdt_fops,
  271. };
  272. /*
  273. * The WDT needs to learn about soft shutdowns in order to
  274. * turn the timebomb registers off.
  275. */
  276. static struct notifier_block wdt_notifier = {
  277. .notifier_call = wdt_notify_sys,
  278. };
  279. static int __init wdt_init(void)
  280. {
  281. int ret;
  282. pr_info("WDT driver for the Winbond(TM) W83697UG/UF Super I/O chip initialising\n");
  283. if (wdt_set_heartbeat(timeout)) {
  284. wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  285. pr_info("timeout value must be 1<=timeout<=255, using %d\n",
  286. WATCHDOG_TIMEOUT);
  287. }
  288. if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
  289. pr_err("I/O address 0x%04x already in use\n", wdt_io);
  290. ret = -EIO;
  291. goto out;
  292. }
  293. ret = w83697ug_init();
  294. if (ret != 0)
  295. goto unreg_regions;
  296. ret = register_reboot_notifier(&wdt_notifier);
  297. if (ret != 0) {
  298. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  299. goto unreg_regions;
  300. }
  301. ret = misc_register(&wdt_miscdev);
  302. if (ret != 0) {
  303. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  304. WATCHDOG_MINOR, ret);
  305. goto unreg_reboot;
  306. }
  307. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  308. timeout, nowayout);
  309. out:
  310. return ret;
  311. unreg_reboot:
  312. unregister_reboot_notifier(&wdt_notifier);
  313. unreg_regions:
  314. release_region(wdt_io, 1);
  315. goto out;
  316. }
  317. static void __exit wdt_exit(void)
  318. {
  319. misc_deregister(&wdt_miscdev);
  320. unregister_reboot_notifier(&wdt_notifier);
  321. release_region(wdt_io, 1);
  322. }
  323. module_init(wdt_init);
  324. module_exit(wdt_exit);
  325. MODULE_LICENSE("GPL");
  326. MODULE_AUTHOR("Flemming Frandsen <ff@nrvissing.net>");
  327. MODULE_DESCRIPTION("w83697ug/uf WDT driver");
  328. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);