w83697ug_wdt.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. return;
  130. outb_p(0xF4, WDT_EFER); /* Select CRF4 */
  131. outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF4 */
  132. w83697ug_unselect_wd_register();
  133. spin_unlock(&io_lock);
  134. }
  135. static int wdt_ping(void)
  136. {
  137. wdt_ctrl(timeout);
  138. return 0;
  139. }
  140. static int wdt_disable(void)
  141. {
  142. wdt_ctrl(0);
  143. return 0;
  144. }
  145. static int wdt_set_heartbeat(int t)
  146. {
  147. if (t < 1 || t > 255)
  148. return -EINVAL;
  149. timeout = t;
  150. return 0;
  151. }
  152. static ssize_t wdt_write(struct file *file, const char __user *buf,
  153. size_t count, loff_t *ppos)
  154. {
  155. if (count) {
  156. if (!nowayout) {
  157. size_t i;
  158. expect_close = 0;
  159. for (i = 0; i != count; i++) {
  160. char c;
  161. if (get_user(c, buf + i))
  162. return -EFAULT;
  163. if (c == 'V')
  164. expect_close = 42;
  165. }
  166. }
  167. wdt_ping();
  168. }
  169. return count;
  170. }
  171. static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  172. {
  173. void __user *argp = (void __user *)arg;
  174. int __user *p = argp;
  175. int new_timeout;
  176. static const struct watchdog_info ident = {
  177. .options = WDIOF_KEEPALIVEPING |
  178. WDIOF_SETTIMEOUT |
  179. WDIOF_MAGICCLOSE,
  180. .firmware_version = 1,
  181. .identity = "W83697UG WDT",
  182. };
  183. switch (cmd) {
  184. case WDIOC_GETSUPPORT:
  185. if (copy_to_user(argp, &ident, sizeof(ident)))
  186. return -EFAULT;
  187. break;
  188. case WDIOC_GETSTATUS:
  189. case WDIOC_GETBOOTSTATUS:
  190. return put_user(0, p);
  191. case WDIOC_SETOPTIONS:
  192. {
  193. int options, retval = -EINVAL;
  194. if (get_user(options, p))
  195. return -EFAULT;
  196. if (options & WDIOS_DISABLECARD) {
  197. wdt_disable();
  198. retval = 0;
  199. }
  200. if (options & WDIOS_ENABLECARD) {
  201. wdt_ping();
  202. retval = 0;
  203. }
  204. return retval;
  205. }
  206. case WDIOC_KEEPALIVE:
  207. wdt_ping();
  208. break;
  209. case WDIOC_SETTIMEOUT:
  210. if (get_user(new_timeout, p))
  211. return -EFAULT;
  212. if (wdt_set_heartbeat(new_timeout))
  213. return -EINVAL;
  214. wdt_ping();
  215. /* Fall */
  216. case WDIOC_GETTIMEOUT:
  217. return put_user(timeout, p);
  218. default:
  219. return -ENOTTY;
  220. }
  221. return 0;
  222. }
  223. static int wdt_open(struct inode *inode, struct file *file)
  224. {
  225. if (test_and_set_bit(0, &wdt_is_open))
  226. return -EBUSY;
  227. /*
  228. * Activate
  229. */
  230. wdt_ping();
  231. return nonseekable_open(inode, file);
  232. }
  233. static int wdt_close(struct inode *inode, struct file *file)
  234. {
  235. if (expect_close == 42)
  236. wdt_disable();
  237. else {
  238. printk(KERN_CRIT PFX
  239. "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. printk(KERN_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. printk(KERN_INFO PFX
  286. "timeout value must be 1<=timeout<=255, using %d\n",
  287. WATCHDOG_TIMEOUT);
  288. }
  289. if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
  290. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  291. wdt_io);
  292. ret = -EIO;
  293. goto out;
  294. }
  295. ret = w83697ug_init();
  296. if (ret != 0)
  297. goto unreg_regions;
  298. ret = register_reboot_notifier(&wdt_notifier);
  299. if (ret != 0) {
  300. printk(KERN_ERR PFX
  301. "cannot register reboot notifier (err=%d)\n", ret);
  302. goto unreg_regions;
  303. }
  304. ret = misc_register(&wdt_miscdev);
  305. if (ret != 0) {
  306. printk(KERN_ERR PFX
  307. "cannot register miscdev on minor=%d (err=%d)\n",
  308. WATCHDOG_MINOR, ret);
  309. goto unreg_reboot;
  310. }
  311. printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  312. timeout, nowayout);
  313. out:
  314. return ret;
  315. unreg_reboot:
  316. unregister_reboot_notifier(&wdt_notifier);
  317. unreg_regions:
  318. release_region(wdt_io, 1);
  319. goto out;
  320. }
  321. static void __exit wdt_exit(void)
  322. {
  323. misc_deregister(&wdt_miscdev);
  324. unregister_reboot_notifier(&wdt_notifier);
  325. release_region(wdt_io, 1);
  326. }
  327. module_init(wdt_init);
  328. module_exit(wdt_exit);
  329. MODULE_LICENSE("GPL");
  330. MODULE_AUTHOR("Flemming Frandsen <ff@nrvissing.net>");
  331. MODULE_DESCRIPTION("w83697ug/uf WDT driver");
  332. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);