w83627hf_wdt.c 7.5 KB

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