w83627hf_wdt.c 7.9 KB

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