advantechwdt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Advantech Single Board Computer WDT driver
  4. *
  5. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  6. *
  7. * Based on acquirewdt.c which is based on wdt.c.
  8. * Original copyright messages:
  9. *
  10. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  11. * All Rights Reserved.
  12. *
  13. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  14. * warranty for any of this software. This material is provided
  15. * "AS-IS" and at no charge.
  16. *
  17. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  18. *
  19. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  20. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  21. *
  22. * 16-Oct-2002 Rob Radez <rob@osinvestor.com>
  23. * Clean up ioctls, clean up init + exit, add expect close support,
  24. * add wdt_start and wdt_stop as parameters.
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/types.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/watchdog.h>
  32. #include <linux/fs.h>
  33. #include <linux/ioport.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/init.h>
  36. #include <linux/io.h>
  37. #include <linux/uaccess.h>
  38. #define DRV_NAME "advantechwdt"
  39. #define WATCHDOG_NAME "Advantech WDT"
  40. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  41. /* the watchdog platform device */
  42. static struct platform_device *advwdt_platform_device;
  43. static unsigned long advwdt_is_open;
  44. static char adv_expect_close;
  45. /*
  46. * You must set these - there is no sane way to probe for this board.
  47. *
  48. * To enable or restart, write the timeout value in seconds (1 to 63)
  49. * to I/O port wdt_start. To disable, read I/O port wdt_stop.
  50. * Both are 0x443 for most boards (tested on a PCA-6276VE-00B1), but
  51. * check your manual (at least the PCA-6159 seems to be different -
  52. * the manual says wdt_stop is 0x43, not 0x443).
  53. * (0x43 is also a write-only control register for the 8254 timer!)
  54. */
  55. static int wdt_stop = 0x443;
  56. module_param(wdt_stop, int, 0);
  57. MODULE_PARM_DESC(wdt_stop, "Advantech WDT 'stop' io port (default 0x443)");
  58. static int wdt_start = 0x443;
  59. module_param(wdt_start, int, 0);
  60. MODULE_PARM_DESC(wdt_start, "Advantech WDT 'start' io port (default 0x443)");
  61. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  62. module_param(timeout, int, 0);
  63. MODULE_PARM_DESC(timeout,
  64. "Watchdog timeout in seconds. 1<= timeout <=63, default="
  65. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  66. static bool nowayout = WATCHDOG_NOWAYOUT;
  67. module_param(nowayout, bool, 0);
  68. MODULE_PARM_DESC(nowayout,
  69. "Watchdog cannot be stopped once started (default="
  70. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  71. /*
  72. * Watchdog Operations
  73. */
  74. static void advwdt_ping(void)
  75. {
  76. /* Write a watchdog value */
  77. outb_p(timeout, wdt_start);
  78. }
  79. static void advwdt_disable(void)
  80. {
  81. inb_p(wdt_stop);
  82. }
  83. static int advwdt_set_heartbeat(int t)
  84. {
  85. if (t < 1 || t > 63)
  86. return -EINVAL;
  87. timeout = t;
  88. return 0;
  89. }
  90. /*
  91. * /dev/watchdog handling
  92. */
  93. static ssize_t advwdt_write(struct file *file, const char __user *buf,
  94. size_t count, loff_t *ppos)
  95. {
  96. if (count) {
  97. if (!nowayout) {
  98. size_t i;
  99. adv_expect_close = 0;
  100. for (i = 0; i != count; i++) {
  101. char c;
  102. if (get_user(c, buf + i))
  103. return -EFAULT;
  104. if (c == 'V')
  105. adv_expect_close = 42;
  106. }
  107. }
  108. advwdt_ping();
  109. }
  110. return count;
  111. }
  112. static long advwdt_ioctl(struct file *file, unsigned int cmd, 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 |
  119. WDIOF_SETTIMEOUT |
  120. WDIOF_MAGICCLOSE,
  121. .firmware_version = 1,
  122. .identity = WATCHDOG_NAME,
  123. };
  124. switch (cmd) {
  125. case WDIOC_GETSUPPORT:
  126. if (copy_to_user(argp, &ident, sizeof(ident)))
  127. return -EFAULT;
  128. break;
  129. case WDIOC_GETSTATUS:
  130. case WDIOC_GETBOOTSTATUS:
  131. return put_user(0, p);
  132. case WDIOC_SETOPTIONS:
  133. {
  134. int options, retval = -EINVAL;
  135. if (get_user(options, p))
  136. return -EFAULT;
  137. if (options & WDIOS_DISABLECARD) {
  138. advwdt_disable();
  139. retval = 0;
  140. }
  141. if (options & WDIOS_ENABLECARD) {
  142. advwdt_ping();
  143. retval = 0;
  144. }
  145. return retval;
  146. }
  147. case WDIOC_KEEPALIVE:
  148. advwdt_ping();
  149. break;
  150. case WDIOC_SETTIMEOUT:
  151. if (get_user(new_timeout, p))
  152. return -EFAULT;
  153. if (advwdt_set_heartbeat(new_timeout))
  154. return -EINVAL;
  155. advwdt_ping();
  156. fallthrough;
  157. case WDIOC_GETTIMEOUT:
  158. return put_user(timeout, p);
  159. default:
  160. return -ENOTTY;
  161. }
  162. return 0;
  163. }
  164. static int advwdt_open(struct inode *inode, struct file *file)
  165. {
  166. if (test_and_set_bit(0, &advwdt_is_open))
  167. return -EBUSY;
  168. /*
  169. * Activate
  170. */
  171. advwdt_ping();
  172. return stream_open(inode, file);
  173. }
  174. static int advwdt_close(struct inode *inode, struct file *file)
  175. {
  176. if (adv_expect_close == 42) {
  177. advwdt_disable();
  178. } else {
  179. pr_crit("Unexpected close, not stopping watchdog!\n");
  180. advwdt_ping();
  181. }
  182. clear_bit(0, &advwdt_is_open);
  183. adv_expect_close = 0;
  184. return 0;
  185. }
  186. /*
  187. * Kernel Interfaces
  188. */
  189. static const struct file_operations advwdt_fops = {
  190. .owner = THIS_MODULE,
  191. .llseek = no_llseek,
  192. .write = advwdt_write,
  193. .unlocked_ioctl = advwdt_ioctl,
  194. .compat_ioctl = compat_ptr_ioctl,
  195. .open = advwdt_open,
  196. .release = advwdt_close,
  197. };
  198. static struct miscdevice advwdt_miscdev = {
  199. .minor = WATCHDOG_MINOR,
  200. .name = "watchdog",
  201. .fops = &advwdt_fops,
  202. };
  203. /*
  204. * Init & exit routines
  205. */
  206. static int __init advwdt_probe(struct platform_device *dev)
  207. {
  208. int ret;
  209. if (wdt_stop != wdt_start) {
  210. if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
  211. pr_err("I/O address 0x%04x already in use\n",
  212. wdt_stop);
  213. ret = -EIO;
  214. goto out;
  215. }
  216. }
  217. if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
  218. pr_err("I/O address 0x%04x already in use\n", wdt_start);
  219. ret = -EIO;
  220. goto unreg_stop;
  221. }
  222. /* Check that the heartbeat value is within it's range ;
  223. * if not reset to the default */
  224. if (advwdt_set_heartbeat(timeout)) {
  225. advwdt_set_heartbeat(WATCHDOG_TIMEOUT);
  226. pr_info("timeout value must be 1<=x<=63, using %d\n", timeout);
  227. }
  228. ret = misc_register(&advwdt_miscdev);
  229. if (ret != 0) {
  230. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  231. WATCHDOG_MINOR, ret);
  232. goto unreg_regions;
  233. }
  234. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  235. timeout, nowayout);
  236. out:
  237. return ret;
  238. unreg_regions:
  239. release_region(wdt_start, 1);
  240. unreg_stop:
  241. if (wdt_stop != wdt_start)
  242. release_region(wdt_stop, 1);
  243. goto out;
  244. }
  245. static int advwdt_remove(struct platform_device *dev)
  246. {
  247. misc_deregister(&advwdt_miscdev);
  248. release_region(wdt_start, 1);
  249. if (wdt_stop != wdt_start)
  250. release_region(wdt_stop, 1);
  251. return 0;
  252. }
  253. static void advwdt_shutdown(struct platform_device *dev)
  254. {
  255. /* Turn the WDT off if we have a soft shutdown */
  256. advwdt_disable();
  257. }
  258. static struct platform_driver advwdt_driver = {
  259. .remove = advwdt_remove,
  260. .shutdown = advwdt_shutdown,
  261. .driver = {
  262. .name = DRV_NAME,
  263. },
  264. };
  265. static int __init advwdt_init(void)
  266. {
  267. int err;
  268. pr_info("WDT driver for Advantech single board computer initialising\n");
  269. advwdt_platform_device = platform_device_register_simple(DRV_NAME,
  270. -1, NULL, 0);
  271. if (IS_ERR(advwdt_platform_device))
  272. return PTR_ERR(advwdt_platform_device);
  273. err = platform_driver_probe(&advwdt_driver, advwdt_probe);
  274. if (err)
  275. goto unreg_platform_device;
  276. return 0;
  277. unreg_platform_device:
  278. platform_device_unregister(advwdt_platform_device);
  279. return err;
  280. }
  281. static void __exit advwdt_exit(void)
  282. {
  283. platform_device_unregister(advwdt_platform_device);
  284. platform_driver_unregister(&advwdt_driver);
  285. pr_info("Watchdog Module Unloaded\n");
  286. }
  287. module_init(advwdt_init);
  288. module_exit(advwdt_exit);
  289. MODULE_LICENSE("GPL");
  290. MODULE_AUTHOR("Marek Michalkiewicz <marekm@linux.org.pl>");
  291. MODULE_DESCRIPTION("Advantech Single Board Computer WDT driver");