ib700wdt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * IB700 Single Board Computer WDT driver
  4. *
  5. * (c) Copyright 2001 Charles Howes <chowes@vsol.net>
  6. *
  7. * Based on advantechwdt.c which is based on acquirewdt.c which
  8. * is based on wdt.c.
  9. *
  10. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  11. *
  12. * Based on acquirewdt.c which is based on wdt.c.
  13. * Original copyright messages:
  14. *
  15. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  16. * All Rights Reserved.
  17. *
  18. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  19. * warranty for any of this software. This material is provided
  20. * "AS-IS" and at no charge.
  21. *
  22. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  23. *
  24. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  25. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  26. * Added timeout module option to override default
  27. *
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/miscdevice.h>
  33. #include <linux/watchdog.h>
  34. #include <linux/ioport.h>
  35. #include <linux/fs.h>
  36. #include <linux/init.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/io.h>
  41. #include <linux/uaccess.h>
  42. static struct platform_device *ibwdt_platform_device;
  43. static unsigned long ibwdt_is_open;
  44. static DEFINE_SPINLOCK(ibwdt_lock);
  45. static char expect_close;
  46. /* Module information */
  47. #define DRV_NAME "ib700wdt"
  48. /*
  49. *
  50. * Watchdog Timer Configuration
  51. *
  52. * The function of the watchdog timer is to reset the system
  53. * automatically and is defined at I/O port 0443H. To enable the
  54. * watchdog timer and allow the system to reset, write I/O port 0443H.
  55. * To disable the timer, write I/O port 0441H for the system to stop the
  56. * watchdog function. The timer has a tolerance of 20% for its
  57. * intervals.
  58. *
  59. * The following describes how the timer should be programmed.
  60. *
  61. * Enabling Watchdog:
  62. * MOV AX,000FH (Choose the values from 0 to F)
  63. * MOV DX,0443H
  64. * OUT DX,AX
  65. *
  66. * Disabling Watchdog:
  67. * MOV AX,000FH (Any value is fine.)
  68. * MOV DX,0441H
  69. * OUT DX,AX
  70. *
  71. * Watchdog timer control table:
  72. * Level Value Time/sec | Level Value Time/sec
  73. * 1 F 0 | 9 7 16
  74. * 2 E 2 | 10 6 18
  75. * 3 D 4 | 11 5 20
  76. * 4 C 6 | 12 4 22
  77. * 5 B 8 | 13 3 24
  78. * 6 A 10 | 14 2 26
  79. * 7 9 12 | 15 1 28
  80. * 8 8 14 | 16 0 30
  81. *
  82. */
  83. #define WDT_STOP 0x441
  84. #define WDT_START 0x443
  85. /* Default timeout */
  86. #define WATCHDOG_TIMEOUT 30 /* 30 seconds +/- 20% */
  87. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  88. module_param(timeout, int, 0);
  89. MODULE_PARM_DESC(timeout,
  90. "Watchdog timeout in seconds. 0<= timeout <=30, default="
  91. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  92. static bool nowayout = WATCHDOG_NOWAYOUT;
  93. module_param(nowayout, bool, 0);
  94. MODULE_PARM_DESC(nowayout,
  95. "Watchdog cannot be stopped once started (default="
  96. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  97. /*
  98. * Watchdog Operations
  99. */
  100. static void ibwdt_ping(void)
  101. {
  102. int wd_margin = 15 - ((timeout + 1) / 2);
  103. spin_lock(&ibwdt_lock);
  104. /* Write a watchdog value */
  105. outb_p(wd_margin, WDT_START);
  106. spin_unlock(&ibwdt_lock);
  107. }
  108. static void ibwdt_disable(void)
  109. {
  110. spin_lock(&ibwdt_lock);
  111. outb_p(0, WDT_STOP);
  112. spin_unlock(&ibwdt_lock);
  113. }
  114. static int ibwdt_set_heartbeat(int t)
  115. {
  116. if (t < 0 || t > 30)
  117. return -EINVAL;
  118. timeout = t;
  119. return 0;
  120. }
  121. /*
  122. * /dev/watchdog handling
  123. */
  124. static ssize_t ibwdt_write(struct file *file, const char __user *buf,
  125. size_t count, loff_t *ppos)
  126. {
  127. if (count) {
  128. if (!nowayout) {
  129. size_t i;
  130. /* In case it was set long ago */
  131. expect_close = 0;
  132. for (i = 0; i != count; i++) {
  133. char c;
  134. if (get_user(c, buf + i))
  135. return -EFAULT;
  136. if (c == 'V')
  137. expect_close = 42;
  138. }
  139. }
  140. ibwdt_ping();
  141. }
  142. return count;
  143. }
  144. static long ibwdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  145. {
  146. int new_margin;
  147. void __user *argp = (void __user *)arg;
  148. int __user *p = argp;
  149. static const struct watchdog_info ident = {
  150. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
  151. | WDIOF_MAGICCLOSE,
  152. .firmware_version = 1,
  153. .identity = "IB700 WDT",
  154. };
  155. switch (cmd) {
  156. case WDIOC_GETSUPPORT:
  157. if (copy_to_user(argp, &ident, sizeof(ident)))
  158. return -EFAULT;
  159. break;
  160. case WDIOC_GETSTATUS:
  161. case WDIOC_GETBOOTSTATUS:
  162. return put_user(0, p);
  163. case WDIOC_SETOPTIONS:
  164. {
  165. int options, retval = -EINVAL;
  166. if (get_user(options, p))
  167. return -EFAULT;
  168. if (options & WDIOS_DISABLECARD) {
  169. ibwdt_disable();
  170. retval = 0;
  171. }
  172. if (options & WDIOS_ENABLECARD) {
  173. ibwdt_ping();
  174. retval = 0;
  175. }
  176. return retval;
  177. }
  178. case WDIOC_KEEPALIVE:
  179. ibwdt_ping();
  180. break;
  181. case WDIOC_SETTIMEOUT:
  182. if (get_user(new_margin, p))
  183. return -EFAULT;
  184. if (ibwdt_set_heartbeat(new_margin))
  185. return -EINVAL;
  186. ibwdt_ping();
  187. fallthrough;
  188. case WDIOC_GETTIMEOUT:
  189. return put_user(timeout, p);
  190. default:
  191. return -ENOTTY;
  192. }
  193. return 0;
  194. }
  195. static int ibwdt_open(struct inode *inode, struct file *file)
  196. {
  197. if (test_and_set_bit(0, &ibwdt_is_open))
  198. return -EBUSY;
  199. if (nowayout)
  200. __module_get(THIS_MODULE);
  201. /* Activate */
  202. ibwdt_ping();
  203. return stream_open(inode, file);
  204. }
  205. static int ibwdt_close(struct inode *inode, struct file *file)
  206. {
  207. if (expect_close == 42) {
  208. ibwdt_disable();
  209. } else {
  210. pr_crit("WDT device closed unexpectedly. WDT will not stop!\n");
  211. ibwdt_ping();
  212. }
  213. clear_bit(0, &ibwdt_is_open);
  214. expect_close = 0;
  215. return 0;
  216. }
  217. /*
  218. * Kernel Interfaces
  219. */
  220. static const struct file_operations ibwdt_fops = {
  221. .owner = THIS_MODULE,
  222. .llseek = no_llseek,
  223. .write = ibwdt_write,
  224. .unlocked_ioctl = ibwdt_ioctl,
  225. .compat_ioctl = compat_ptr_ioctl,
  226. .open = ibwdt_open,
  227. .release = ibwdt_close,
  228. };
  229. static struct miscdevice ibwdt_miscdev = {
  230. .minor = WATCHDOG_MINOR,
  231. .name = "watchdog",
  232. .fops = &ibwdt_fops,
  233. };
  234. /*
  235. * Init & exit routines
  236. */
  237. static int __init ibwdt_probe(struct platform_device *dev)
  238. {
  239. int res;
  240. #if WDT_START != WDT_STOP
  241. if (!request_region(WDT_STOP, 1, "IB700 WDT")) {
  242. pr_err("STOP method I/O %X is not available\n", WDT_STOP);
  243. res = -EIO;
  244. goto out_nostopreg;
  245. }
  246. #endif
  247. if (!request_region(WDT_START, 1, "IB700 WDT")) {
  248. pr_err("START method I/O %X is not available\n", WDT_START);
  249. res = -EIO;
  250. goto out_nostartreg;
  251. }
  252. /* Check that the heartbeat value is within it's range ;
  253. * if not reset to the default */
  254. if (ibwdt_set_heartbeat(timeout)) {
  255. ibwdt_set_heartbeat(WATCHDOG_TIMEOUT);
  256. pr_info("timeout value must be 0<=x<=30, using %d\n", timeout);
  257. }
  258. res = misc_register(&ibwdt_miscdev);
  259. if (res) {
  260. pr_err("failed to register misc device\n");
  261. goto out_nomisc;
  262. }
  263. return 0;
  264. out_nomisc:
  265. release_region(WDT_START, 1);
  266. out_nostartreg:
  267. #if WDT_START != WDT_STOP
  268. release_region(WDT_STOP, 1);
  269. #endif
  270. out_nostopreg:
  271. return res;
  272. }
  273. static int ibwdt_remove(struct platform_device *dev)
  274. {
  275. misc_deregister(&ibwdt_miscdev);
  276. release_region(WDT_START, 1);
  277. #if WDT_START != WDT_STOP
  278. release_region(WDT_STOP, 1);
  279. #endif
  280. return 0;
  281. }
  282. static void ibwdt_shutdown(struct platform_device *dev)
  283. {
  284. /* Turn the WDT off if we have a soft shutdown */
  285. ibwdt_disable();
  286. }
  287. static struct platform_driver ibwdt_driver = {
  288. .remove = ibwdt_remove,
  289. .shutdown = ibwdt_shutdown,
  290. .driver = {
  291. .name = DRV_NAME,
  292. },
  293. };
  294. static int __init ibwdt_init(void)
  295. {
  296. int err;
  297. pr_info("WDT driver for IB700 single board computer initialising\n");
  298. ibwdt_platform_device = platform_device_register_simple(DRV_NAME,
  299. -1, NULL, 0);
  300. if (IS_ERR(ibwdt_platform_device))
  301. return PTR_ERR(ibwdt_platform_device);
  302. err = platform_driver_probe(&ibwdt_driver, ibwdt_probe);
  303. if (err)
  304. goto unreg_platform_device;
  305. return 0;
  306. unreg_platform_device:
  307. platform_device_unregister(ibwdt_platform_device);
  308. return err;
  309. }
  310. static void __exit ibwdt_exit(void)
  311. {
  312. platform_device_unregister(ibwdt_platform_device);
  313. platform_driver_unregister(&ibwdt_driver);
  314. pr_info("Watchdog Module Unloaded\n");
  315. }
  316. module_init(ibwdt_init);
  317. module_exit(ibwdt_exit);
  318. MODULE_AUTHOR("Charles Howes <chowes@vsol.net>");
  319. MODULE_DESCRIPTION("IB700 SBC watchdog driver");
  320. MODULE_LICENSE("GPL");
  321. /* end of ib700wdt.c */