w83627hf_wdt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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(0xF6, WDT_EFER); /* Select CRF6 */
  82. t=inb_p(WDT_EFDR); /* read CRF6 */
  83. if (t != 0) {
  84. printk (KERN_INFO PFX "Watchdog already running. Resetting timeout to %d sec\n", timeout);
  85. outb_p(timeout, WDT_EFDR); /* Write back to CRF6 */
  86. }
  87. outb_p(0xF5, WDT_EFER); /* Select CRF5 */
  88. t=inb_p(WDT_EFDR); /* read CRF5 */
  89. t&=~0x0C; /* set second mode & disable keyboard turning off watchdog */
  90. outb_p(t, WDT_EFDR); /* Write back to CRF5 */
  91. w83627hf_unselect_wd_register();
  92. }
  93. static void
  94. wdt_ctrl(int timeout)
  95. {
  96. w83627hf_select_wd_register();
  97. outb_p(0xF6, WDT_EFER); /* Select CRF6 */
  98. outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF6 */
  99. w83627hf_unselect_wd_register();
  100. }
  101. static int
  102. wdt_ping(void)
  103. {
  104. wdt_ctrl(timeout);
  105. return 0;
  106. }
  107. static int
  108. wdt_disable(void)
  109. {
  110. wdt_ctrl(0);
  111. return 0;
  112. }
  113. static int
  114. wdt_set_heartbeat(int t)
  115. {
  116. if ((t < 1) || (t > 63))
  117. return -EINVAL;
  118. timeout = t;
  119. return 0;
  120. }
  121. static ssize_t
  122. wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  123. {
  124. if (count) {
  125. if (!nowayout) {
  126. size_t i;
  127. expect_close = 0;
  128. for (i = 0; i != count; i++) {
  129. char c;
  130. if (get_user(c, buf+i))
  131. return -EFAULT;
  132. if (c == 'V')
  133. expect_close = 42;
  134. }
  135. }
  136. wdt_ping();
  137. }
  138. return count;
  139. }
  140. static int
  141. wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  142. unsigned long arg)
  143. {
  144. void __user *argp = (void __user *)arg;
  145. int __user *p = argp;
  146. int new_timeout;
  147. static struct watchdog_info ident = {
  148. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  149. .firmware_version = 1,
  150. .identity = "W83627HF WDT",
  151. };
  152. switch (cmd) {
  153. case WDIOC_GETSUPPORT:
  154. if (copy_to_user(argp, &ident, sizeof(ident)))
  155. return -EFAULT;
  156. break;
  157. case WDIOC_GETSTATUS:
  158. case WDIOC_GETBOOTSTATUS:
  159. return put_user(0, p);
  160. case WDIOC_KEEPALIVE:
  161. wdt_ping();
  162. break;
  163. case WDIOC_SETTIMEOUT:
  164. if (get_user(new_timeout, p))
  165. return -EFAULT;
  166. if (wdt_set_heartbeat(new_timeout))
  167. return -EINVAL;
  168. wdt_ping();
  169. /* Fall */
  170. case WDIOC_GETTIMEOUT:
  171. return put_user(timeout, p);
  172. case WDIOC_SETOPTIONS:
  173. {
  174. int options, retval = -EINVAL;
  175. if (get_user(options, p))
  176. return -EFAULT;
  177. if (options & WDIOS_DISABLECARD) {
  178. wdt_disable();
  179. retval = 0;
  180. }
  181. if (options & WDIOS_ENABLECARD) {
  182. wdt_ping();
  183. retval = 0;
  184. }
  185. return retval;
  186. }
  187. default:
  188. return -ENOIOCTLCMD;
  189. }
  190. return 0;
  191. }
  192. static int
  193. wdt_open(struct inode *inode, struct file *file)
  194. {
  195. if (test_and_set_bit(0, &wdt_is_open))
  196. return -EBUSY;
  197. /*
  198. * Activate
  199. */
  200. wdt_ping();
  201. return nonseekable_open(inode, file);
  202. }
  203. static int
  204. wdt_close(struct inode *inode, struct file *file)
  205. {
  206. if (expect_close == 42) {
  207. wdt_disable();
  208. } else {
  209. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  210. wdt_ping();
  211. }
  212. expect_close = 0;
  213. clear_bit(0, &wdt_is_open);
  214. return 0;
  215. }
  216. /*
  217. * Notifier for system down
  218. */
  219. static int
  220. wdt_notify_sys(struct notifier_block *this, unsigned long code,
  221. void *unused)
  222. {
  223. if (code == SYS_DOWN || code == SYS_HALT) {
  224. /* Turn the WDT off */
  225. wdt_disable();
  226. }
  227. return NOTIFY_DONE;
  228. }
  229. /*
  230. * Kernel Interfaces
  231. */
  232. static struct file_operations wdt_fops = {
  233. .owner = THIS_MODULE,
  234. .llseek = no_llseek,
  235. .write = wdt_write,
  236. .ioctl = wdt_ioctl,
  237. .open = wdt_open,
  238. .release = wdt_close,
  239. };
  240. static struct miscdevice wdt_miscdev = {
  241. .minor = WATCHDOG_MINOR,
  242. .name = "watchdog",
  243. .fops = &wdt_fops,
  244. };
  245. /*
  246. * The WDT needs to learn about soft shutdowns in order to
  247. * turn the timebomb registers off.
  248. */
  249. static struct notifier_block wdt_notifier = {
  250. .notifier_call = wdt_notify_sys,
  251. };
  252. static int __init
  253. wdt_init(void)
  254. {
  255. int ret;
  256. printk(KERN_INFO "WDT driver for the Winbond(TM) W83627HF Super I/O chip initialising.\n");
  257. if (wdt_set_heartbeat(timeout)) {
  258. wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  259. printk (KERN_INFO PFX "timeout value must be 1<=timeout<=63, using %d\n",
  260. WATCHDOG_TIMEOUT);
  261. }
  262. if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
  263. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  264. wdt_io);
  265. ret = -EIO;
  266. goto out;
  267. }
  268. w83627hf_init();
  269. ret = register_reboot_notifier(&wdt_notifier);
  270. if (ret != 0) {
  271. printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  272. ret);
  273. goto unreg_regions;
  274. }
  275. ret = misc_register(&wdt_miscdev);
  276. if (ret != 0) {
  277. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  278. WATCHDOG_MINOR, ret);
  279. goto unreg_reboot;
  280. }
  281. printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  282. timeout, nowayout);
  283. out:
  284. return ret;
  285. unreg_reboot:
  286. unregister_reboot_notifier(&wdt_notifier);
  287. unreg_regions:
  288. release_region(wdt_io, 1);
  289. goto out;
  290. }
  291. static void __exit
  292. wdt_exit(void)
  293. {
  294. misc_deregister(&wdt_miscdev);
  295. unregister_reboot_notifier(&wdt_notifier);
  296. release_region(wdt_io,1);
  297. }
  298. module_init(wdt_init);
  299. module_exit(wdt_exit);
  300. MODULE_LICENSE("GPL");
  301. MODULE_AUTHOR("Pádraig Brady <P@draigBrady.com>");
  302. MODULE_DESCRIPTION("w38627hf WDT driver");
  303. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);