wafer5823wdt.c 6.9 KB

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