wafer5823wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/watchdog.h>
  33. #include <linux/fs.h>
  34. #include <linux/ioport.h>
  35. #include <linux/notifier.h>
  36. #include <linux/reboot.h>
  37. #include <linux/init.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/io.h>
  40. #include <linux/uaccess.h>
  41. #define WATCHDOG_NAME "Wafer 5823 WDT"
  42. #define PFX WATCHDOG_NAME ": "
  43. #define WD_TIMO 60 /* 60 sec default timeout */
  44. static unsigned long wafwdt_is_open;
  45. static char expect_close;
  46. static DEFINE_SPINLOCK(wafwdt_lock);
  47. /*
  48. * You must set these - there is no sane way to probe for this board.
  49. *
  50. * To enable, write the timeout value in seconds (1 to 255) to I/O
  51. * port WDT_START, then read the port to start the watchdog. To pat
  52. * the dog, read port WDT_STOP to stop the timer, then read WDT_START
  53. * to restart it again.
  54. */
  55. static int wdt_stop = 0x843;
  56. static int wdt_start = 0x443;
  57. static int timeout = WD_TIMO; /* in seconds */
  58. module_param(timeout, int, 0);
  59. MODULE_PARM_DESC(timeout,
  60. "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
  61. __MODULE_STRING(WD_TIMO) ".");
  62. static bool nowayout = WATCHDOG_NOWAYOUT;
  63. module_param(nowayout, bool, 0);
  64. MODULE_PARM_DESC(nowayout,
  65. "Watchdog cannot be stopped once started (default="
  66. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  67. static void wafwdt_ping(void)
  68. {
  69. /* pat watchdog */
  70. spin_lock(&wafwdt_lock);
  71. inb_p(wdt_stop);
  72. inb_p(wdt_start);
  73. spin_unlock(&wafwdt_lock);
  74. }
  75. static void wafwdt_start(void)
  76. {
  77. /* start up watchdog */
  78. outb_p(timeout, wdt_start);
  79. inb_p(wdt_start);
  80. }
  81. static void wafwdt_stop(void)
  82. {
  83. /* stop watchdog */
  84. inb_p(wdt_stop);
  85. }
  86. static ssize_t wafwdt_write(struct file *file, const char __user *buf,
  87. size_t count, loff_t *ppos)
  88. {
  89. /* See if we got the magic character 'V' and reload the timer */
  90. if (count) {
  91. if (!nowayout) {
  92. size_t i;
  93. /* In case it was set long ago */
  94. expect_close = 0;
  95. /* scan to see whether or not we got the magic
  96. character */
  97. for (i = 0; i != count; i++) {
  98. char c;
  99. if (get_user(c, buf + i))
  100. return -EFAULT;
  101. if (c == 'V')
  102. expect_close = 42;
  103. }
  104. }
  105. /* Well, anyhow someone wrote to us, we should
  106. return that favour */
  107. wafwdt_ping();
  108. }
  109. return count;
  110. }
  111. static long wafwdt_ioctl(struct file *file, unsigned int cmd,
  112. unsigned long arg)
  113. {
  114. int new_timeout;
  115. void __user *argp = (void __user *)arg;
  116. int __user *p = argp;
  117. static const struct watchdog_info ident = {
  118. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  119. WDIOF_MAGICCLOSE,
  120. .firmware_version = 1,
  121. .identity = "Wafer 5823 WDT",
  122. };
  123. switch (cmd) {
  124. case WDIOC_GETSUPPORT:
  125. if (copy_to_user(argp, &ident, sizeof(ident)))
  126. return -EFAULT;
  127. break;
  128. case WDIOC_GETSTATUS:
  129. case WDIOC_GETBOOTSTATUS:
  130. return put_user(0, p);
  131. case WDIOC_SETOPTIONS:
  132. {
  133. int options, retval = -EINVAL;
  134. if (get_user(options, p))
  135. return -EFAULT;
  136. if (options & WDIOS_DISABLECARD) {
  137. wafwdt_stop();
  138. retval = 0;
  139. }
  140. if (options & WDIOS_ENABLECARD) {
  141. wafwdt_start();
  142. retval = 0;
  143. }
  144. return retval;
  145. }
  146. case WDIOC_KEEPALIVE:
  147. wafwdt_ping();
  148. break;
  149. case WDIOC_SETTIMEOUT:
  150. if (get_user(new_timeout, p))
  151. return -EFAULT;
  152. if ((new_timeout < 1) || (new_timeout > 255))
  153. return -EINVAL;
  154. timeout = new_timeout;
  155. wafwdt_stop();
  156. wafwdt_start();
  157. /* Fall */
  158. case WDIOC_GETTIMEOUT:
  159. return put_user(timeout, p);
  160. default:
  161. return -ENOTTY;
  162. }
  163. return 0;
  164. }
  165. static int wafwdt_open(struct inode *inode, struct file *file)
  166. {
  167. if (test_and_set_bit(0, &wafwdt_is_open))
  168. return -EBUSY;
  169. /*
  170. * Activate
  171. */
  172. wafwdt_start();
  173. return nonseekable_open(inode, file);
  174. }
  175. static int wafwdt_close(struct inode *inode, struct file *file)
  176. {
  177. if (expect_close == 42)
  178. wafwdt_stop();
  179. else {
  180. pr_crit("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. pr_info("WDT driver for Wafer 5823 single board computer initialising\n");
  224. if (timeout < 1 || timeout > 255) {
  225. timeout = WD_TIMO;
  226. pr_info("timeout value must be 1 <= x <= 255, using %d\n",
  227. timeout);
  228. }
  229. if (wdt_stop != wdt_start) {
  230. if (!request_region(wdt_stop, 1, "Wafer 5823 WDT")) {
  231. pr_err("I/O address 0x%04x already in use\n", wdt_stop);
  232. ret = -EIO;
  233. goto error;
  234. }
  235. }
  236. if (!request_region(wdt_start, 1, "Wafer 5823 WDT")) {
  237. pr_err("I/O address 0x%04x already in use\n", wdt_start);
  238. ret = -EIO;
  239. goto error2;
  240. }
  241. ret = register_reboot_notifier(&wafwdt_notifier);
  242. if (ret != 0) {
  243. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  244. goto error3;
  245. }
  246. ret = misc_register(&wafwdt_miscdev);
  247. if (ret != 0) {
  248. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  249. WATCHDOG_MINOR, ret);
  250. goto error4;
  251. }
  252. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  253. timeout, nowayout);
  254. return ret;
  255. error4:
  256. unregister_reboot_notifier(&wafwdt_notifier);
  257. error3:
  258. release_region(wdt_start, 1);
  259. error2:
  260. if (wdt_stop != wdt_start)
  261. release_region(wdt_stop, 1);
  262. error:
  263. return ret;
  264. }
  265. static void __exit wafwdt_exit(void)
  266. {
  267. misc_deregister(&wafwdt_miscdev);
  268. unregister_reboot_notifier(&wafwdt_notifier);
  269. if (wdt_stop != wdt_start)
  270. release_region(wdt_stop, 1);
  271. release_region(wdt_start, 1);
  272. }
  273. module_init(wafwdt_init);
  274. module_exit(wafwdt_exit);
  275. MODULE_AUTHOR("Justin Cormack");
  276. MODULE_DESCRIPTION("ICP Wafer 5823 Single Board Computer WDT driver");
  277. MODULE_LICENSE("GPL");
  278. /* end of wafer5823wdt.c */