wafer5823wdt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * ICP Wafer 5823 Single Board Computer WDT driver
  3. * http://www.icpamerica.com/wafer_5823.php
  4. * May also work on other similar models
  5. *
  6. * (c) Copyright 2002 Justin Cormack <justin@street-vision.com>
  7. *
  8. * Release 0.02
  9. *
  10. * Based on advantechwdt.c which is based on wdt.c.
  11. * Original copyright messages:
  12. *
  13. * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
  14. * http://www.redhat.com
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  22. * warranty for any of this software. This material is provided
  23. * "AS-IS" and at no charge.
  24. *
  25. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/watchdog.h>
  32. #include <linux/fs.h>
  33. #include <linux/ioport.h>
  34. #include <linux/notifier.h>
  35. #include <linux/reboot.h>
  36. #include <linux/init.h>
  37. #include <linux/spinlock.h>
  38. #include <asm/io.h>
  39. #include <asm/uaccess.h>
  40. #define WATCHDOG_NAME "Wafer 5823 WDT"
  41. #define PFX WATCHDOG_NAME ": "
  42. #define WD_TIMO 60 /* 60 sec default timeout */
  43. static unsigned long wafwdt_is_open;
  44. static char expect_close;
  45. static spinlock_t wafwdt_lock;
  46. /*
  47. * You must set these - there is no sane way to probe for this board.
  48. *
  49. * To enable, write the timeout value in seconds (1 to 255) to I/O
  50. * port WDT_START, then read the port to start the watchdog. To pat
  51. * the dog, read port WDT_STOP to stop the timer, then read WDT_START
  52. * to restart it again.
  53. */
  54. static int wdt_stop = 0x843;
  55. static int wdt_start = 0x443;
  56. static int timeout = WD_TIMO; /* in seconds */
  57. module_param(timeout, int, 0);
  58. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=255, default=" __MODULE_STRING(WD_TIMO) ".");
  59. static int nowayout = WATCHDOG_NOWAYOUT;
  60. module_param(nowayout, int, 0);
  61. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  62. static void wafwdt_ping(void)
  63. {
  64. /* pat watchdog */
  65. spin_lock(&wafwdt_lock);
  66. inb_p(wdt_stop);
  67. inb_p(wdt_start);
  68. spin_unlock(&wafwdt_lock);
  69. }
  70. static void wafwdt_start(void)
  71. {
  72. /* start up watchdog */
  73. outb_p(timeout, wdt_start);
  74. inb_p(wdt_start);
  75. }
  76. static void
  77. wafwdt_stop(void)
  78. {
  79. /* stop watchdog */
  80. inb_p(wdt_stop);
  81. }
  82. static ssize_t wafwdt_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  83. {
  84. /* See if we got the magic character 'V' and reload the timer */
  85. if (count) {
  86. if (!nowayout) {
  87. size_t i;
  88. /* In case it was set long ago */
  89. expect_close = 0;
  90. /* scan to see whether or not we got the magic character */
  91. for (i = 0; i != count; i++) {
  92. char c;
  93. if (get_user(c, buf + i))
  94. return -EFAULT;
  95. if (c == 'V')
  96. expect_close = 42;
  97. }
  98. }
  99. /* Well, anyhow someone wrote to us, we should return that favour */
  100. wafwdt_ping();
  101. }
  102. return count;
  103. }
  104. static int wafwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  105. unsigned long arg)
  106. {
  107. int new_timeout;
  108. void __user *argp = (void __user *)arg;
  109. int __user *p = argp;
  110. static struct watchdog_info ident = {
  111. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  112. .firmware_version = 1,
  113. .identity = "Wafer 5823 WDT",
  114. };
  115. switch (cmd) {
  116. case WDIOC_GETSUPPORT:
  117. if (copy_to_user(argp, &ident, sizeof (ident)))
  118. return -EFAULT;
  119. break;
  120. case WDIOC_GETSTATUS:
  121. case WDIOC_GETBOOTSTATUS:
  122. return put_user(0, p);
  123. case WDIOC_KEEPALIVE:
  124. wafwdt_ping();
  125. break;
  126. case WDIOC_SETTIMEOUT:
  127. if (get_user(new_timeout, p))
  128. return -EFAULT;
  129. if ((new_timeout < 1) || (new_timeout > 255))
  130. return -EINVAL;
  131. timeout = new_timeout;
  132. wafwdt_stop();
  133. wafwdt_start();
  134. /* Fall */
  135. case WDIOC_GETTIMEOUT:
  136. return put_user(timeout, p);
  137. case WDIOC_SETOPTIONS:
  138. {
  139. int options, retval = -EINVAL;
  140. if (get_user(options, p))
  141. return -EFAULT;
  142. if (options & WDIOS_DISABLECARD) {
  143. wafwdt_start();
  144. retval = 0;
  145. }
  146. if (options & WDIOS_ENABLECARD) {
  147. wafwdt_stop();
  148. retval = 0;
  149. }
  150. return retval;
  151. }
  152. default:
  153. return -ENOTTY;
  154. }
  155. return 0;
  156. }
  157. static int wafwdt_open(struct inode *inode, struct file *file)
  158. {
  159. if (test_and_set_bit(0, &wafwdt_is_open))
  160. return -EBUSY;
  161. /*
  162. * Activate
  163. */
  164. wafwdt_start();
  165. return nonseekable_open(inode, file);
  166. }
  167. static int
  168. wafwdt_close(struct inode *inode, struct file *file)
  169. {
  170. if (expect_close == 42) {
  171. wafwdt_stop();
  172. } else {
  173. printk(KERN_CRIT PFX "WDT device closed unexpectedly. WDT will not stop!\n");
  174. wafwdt_ping();
  175. }
  176. clear_bit(0, &wafwdt_is_open);
  177. expect_close = 0;
  178. return 0;
  179. }
  180. /*
  181. * Notifier for system down
  182. */
  183. static int wafwdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
  184. {
  185. if (code == SYS_DOWN || code == SYS_HALT) {
  186. /* Turn the WDT off */
  187. wafwdt_stop();
  188. }
  189. return NOTIFY_DONE;
  190. }
  191. /*
  192. * Kernel Interfaces
  193. */
  194. static const struct file_operations wafwdt_fops = {
  195. .owner = THIS_MODULE,
  196. .llseek = no_llseek,
  197. .write = wafwdt_write,
  198. .ioctl = wafwdt_ioctl,
  199. .open = wafwdt_open,
  200. .release = wafwdt_close,
  201. };
  202. static struct miscdevice wafwdt_miscdev = {
  203. .minor = WATCHDOG_MINOR,
  204. .name = "watchdog",
  205. .fops = &wafwdt_fops,
  206. };
  207. /*
  208. * The WDT needs to learn about soft shutdowns in order to
  209. * turn the timebomb registers off.
  210. */
  211. static struct notifier_block wafwdt_notifier = {
  212. .notifier_call = wafwdt_notify_sys,
  213. };
  214. static int __init wafwdt_init(void)
  215. {
  216. int ret;
  217. printk(KERN_INFO "WDT driver for Wafer 5823 single board computer initialising.\n");
  218. spin_lock_init(&wafwdt_lock);
  219. if (timeout < 1 || timeout > 255) {
  220. timeout = WD_TIMO;
  221. printk (KERN_INFO PFX "timeout value must be 1<=x<=255, using %d\n",
  222. timeout);
  223. }
  224. if (wdt_stop != wdt_start) {
  225. if(!request_region(wdt_stop, 1, "Wafer 5823 WDT")) {
  226. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  227. wdt_stop);
  228. ret = -EIO;
  229. goto error;
  230. }
  231. }
  232. if(!request_region(wdt_start, 1, "Wafer 5823 WDT")) {
  233. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  234. wdt_start);
  235. ret = -EIO;
  236. goto error2;
  237. }
  238. ret = register_reboot_notifier(&wafwdt_notifier);
  239. if (ret != 0) {
  240. printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  241. ret);
  242. goto error3;
  243. }
  244. ret = misc_register(&wafwdt_miscdev);
  245. if (ret != 0) {
  246. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  247. WATCHDOG_MINOR, ret);
  248. goto error4;
  249. }
  250. printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  251. timeout, nowayout);
  252. return ret;
  253. error4:
  254. unregister_reboot_notifier(&wafwdt_notifier);
  255. error3:
  256. release_region(wdt_start, 1);
  257. error2:
  258. if (wdt_stop != wdt_start)
  259. release_region(wdt_stop, 1);
  260. error:
  261. return ret;
  262. }
  263. static void __exit wafwdt_exit(void)
  264. {
  265. misc_deregister(&wafwdt_miscdev);
  266. unregister_reboot_notifier(&wafwdt_notifier);
  267. if(wdt_stop != wdt_start)
  268. release_region(wdt_stop, 1);
  269. release_region(wdt_start, 1);
  270. }
  271. module_init(wafwdt_init);
  272. module_exit(wafwdt_exit);
  273. MODULE_AUTHOR("Justin Cormack");
  274. MODULE_DESCRIPTION("ICP Wafer 5823 Single Board Computer WDT driver");
  275. MODULE_LICENSE("GPL");
  276. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  277. /* end of wafer5823wdt.c */