wafer5823wdt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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@lxorguk.ukuu.org.uk>,
  14. * All Rights Reserved.
  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 <linux/io.h>
  39. #include <linux/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 DEFINE_SPINLOCK(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,
  59. "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
  60. __MODULE_STRING(WD_TIMO) ".");
  61. static int nowayout = WATCHDOG_NOWAYOUT;
  62. module_param(nowayout, int, 0);
  63. MODULE_PARM_DESC(nowayout,
  64. "Watchdog cannot be stopped once started (default="
  65. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  66. static void wafwdt_ping(void)
  67. {
  68. /* pat watchdog */
  69. spin_lock(&wafwdt_lock);
  70. inb_p(wdt_stop);
  71. inb_p(wdt_start);
  72. spin_unlock(&wafwdt_lock);
  73. }
  74. static void wafwdt_start(void)
  75. {
  76. /* start up watchdog */
  77. outb_p(timeout, wdt_start);
  78. inb_p(wdt_start);
  79. }
  80. static void wafwdt_stop(void)
  81. {
  82. /* stop watchdog */
  83. inb_p(wdt_stop);
  84. }
  85. static ssize_t wafwdt_write(struct file *file, const char __user *buf,
  86. size_t count, loff_t *ppos)
  87. {
  88. /* See if we got the magic character 'V' and reload the timer */
  89. if (count) {
  90. if (!nowayout) {
  91. size_t i;
  92. /* In case it was set long ago */
  93. expect_close = 0;
  94. /* scan to see whether or not we got the magic
  95. character */
  96. for (i = 0; i != count; i++) {
  97. char c;
  98. if (get_user(c, buf + i))
  99. return -EFAULT;
  100. if (c == 'V')
  101. expect_close = 42;
  102. }
  103. }
  104. /* Well, anyhow someone wrote to us, we should
  105. return that favour */
  106. wafwdt_ping();
  107. }
  108. return count;
  109. }
  110. static long wafwdt_ioctl(struct file *file, unsigned int cmd,
  111. unsigned long arg)
  112. {
  113. int new_timeout;
  114. void __user *argp = (void __user *)arg;
  115. int __user *p = argp;
  116. static const struct watchdog_info ident = {
  117. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  118. WDIOF_MAGICCLOSE,
  119. .firmware_version = 1,
  120. .identity = "Wafer 5823 WDT",
  121. };
  122. switch (cmd) {
  123. case WDIOC_GETSUPPORT:
  124. if (copy_to_user(argp, &ident, sizeof(ident)))
  125. return -EFAULT;
  126. break;
  127. case WDIOC_GETSTATUS:
  128. case WDIOC_GETBOOTSTATUS:
  129. return put_user(0, p);
  130. case WDIOC_SETOPTIONS:
  131. {
  132. int options, retval = -EINVAL;
  133. if (get_user(options, p))
  134. return -EFAULT;
  135. if (options & WDIOS_DISABLECARD) {
  136. wafwdt_start();
  137. retval = 0;
  138. }
  139. if (options & WDIOS_ENABLECARD) {
  140. wafwdt_stop();
  141. retval = 0;
  142. }
  143. return retval;
  144. }
  145. case WDIOC_KEEPALIVE:
  146. wafwdt_ping();
  147. break;
  148. case WDIOC_SETTIMEOUT:
  149. if (get_user(new_timeout, p))
  150. return -EFAULT;
  151. if ((new_timeout < 1) || (new_timeout > 255))
  152. return -EINVAL;
  153. timeout = new_timeout;
  154. wafwdt_stop();
  155. wafwdt_start();
  156. /* Fall */
  157. case WDIOC_GETTIMEOUT:
  158. return put_user(timeout, p);
  159. default:
  160. return -ENOTTY;
  161. }
  162. return 0;
  163. }
  164. static int wafwdt_open(struct inode *inode, struct file *file)
  165. {
  166. if (test_and_set_bit(0, &wafwdt_is_open))
  167. return -EBUSY;
  168. /*
  169. * Activate
  170. */
  171. wafwdt_start();
  172. return nonseekable_open(inode, file);
  173. }
  174. static int wafwdt_close(struct inode *inode, struct file *file)
  175. {
  176. if (expect_close == 42)
  177. wafwdt_stop();
  178. else {
  179. printk(KERN_CRIT PFX
  180. "WDT device closed unexpectedly. WDT will not stop!\n");
  181. wafwdt_ping();
  182. }
  183. clear_bit(0, &wafwdt_is_open);
  184. expect_close = 0;
  185. return 0;
  186. }
  187. /*
  188. * Notifier for system down
  189. */
  190. static int wafwdt_notify_sys(struct notifier_block *this, unsigned long code,
  191. void *unused)
  192. {
  193. if (code == SYS_DOWN || code == SYS_HALT)
  194. wafwdt_stop();
  195. return NOTIFY_DONE;
  196. }
  197. /*
  198. * Kernel Interfaces
  199. */
  200. static const struct file_operations wafwdt_fops = {
  201. .owner = THIS_MODULE,
  202. .llseek = no_llseek,
  203. .write = wafwdt_write,
  204. .unlocked_ioctl = wafwdt_ioctl,
  205. .open = wafwdt_open,
  206. .release = wafwdt_close,
  207. };
  208. static struct miscdevice wafwdt_miscdev = {
  209. .minor = WATCHDOG_MINOR,
  210. .name = "watchdog",
  211. .fops = &wafwdt_fops,
  212. };
  213. /*
  214. * The WDT needs to learn about soft shutdowns in order to
  215. * turn the timebomb registers off.
  216. */
  217. static struct notifier_block wafwdt_notifier = {
  218. .notifier_call = wafwdt_notify_sys,
  219. };
  220. static int __init wafwdt_init(void)
  221. {
  222. int ret;
  223. printk(KERN_INFO
  224. "WDT driver for Wafer 5823 single board computer initialising.\n");
  225. if (timeout < 1 || timeout > 255) {
  226. timeout = WD_TIMO;
  227. printk(KERN_INFO PFX
  228. "timeout value must be 1 <= x <= 255, using %d\n",
  229. timeout);
  230. }
  231. if (wdt_stop != wdt_start) {
  232. if (!request_region(wdt_stop, 1, "Wafer 5823 WDT")) {
  233. printk(KERN_ERR PFX
  234. "I/O address 0x%04x already in use\n",
  235. wdt_stop);
  236. ret = -EIO;
  237. goto error;
  238. }
  239. }
  240. if (!request_region(wdt_start, 1, "Wafer 5823 WDT")) {
  241. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  242. wdt_start);
  243. ret = -EIO;
  244. goto error2;
  245. }
  246. ret = register_reboot_notifier(&wafwdt_notifier);
  247. if (ret != 0) {
  248. printk(KERN_ERR PFX
  249. "cannot register reboot notifier (err=%d)\n", ret);
  250. goto error3;
  251. }
  252. ret = misc_register(&wafwdt_miscdev);
  253. if (ret != 0) {
  254. printk(KERN_ERR PFX
  255. "cannot register miscdev on minor=%d (err=%d)\n",
  256. WATCHDOG_MINOR, ret);
  257. goto error4;
  258. }
  259. printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  260. timeout, nowayout);
  261. return ret;
  262. error4:
  263. unregister_reboot_notifier(&wafwdt_notifier);
  264. error3:
  265. release_region(wdt_start, 1);
  266. error2:
  267. if (wdt_stop != wdt_start)
  268. release_region(wdt_stop, 1);
  269. error:
  270. return ret;
  271. }
  272. static void __exit wafwdt_exit(void)
  273. {
  274. misc_deregister(&wafwdt_miscdev);
  275. unregister_reboot_notifier(&wafwdt_notifier);
  276. if (wdt_stop != wdt_start)
  277. release_region(wdt_stop, 1);
  278. release_region(wdt_start, 1);
  279. }
  280. module_init(wafwdt_init);
  281. module_exit(wafwdt_exit);
  282. MODULE_AUTHOR("Justin Cormack");
  283. MODULE_DESCRIPTION("ICP Wafer 5823 Single Board Computer WDT driver");
  284. MODULE_LICENSE("GPL");
  285. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  286. /* end of wafer5823wdt.c */