sbc7240_wdt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NANO7240 SBC Watchdog device driver
  4. *
  5. * Based on w83877f.c by Scott Jennings,
  6. *
  7. * (c) Copyright 2007 Gilles GIGAN <gilles.gigan@jcu.edu.au>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/fs.h>
  11. #include <linux/init.h>
  12. #include <linux/ioport.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/notifier.h>
  18. #include <linux/reboot.h>
  19. #include <linux/types.h>
  20. #include <linux/watchdog.h>
  21. #include <linux/io.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/atomic.h>
  24. #define SBC7240_ENABLE_PORT 0x443
  25. #define SBC7240_DISABLE_PORT 0x043
  26. #define SBC7240_SET_TIMEOUT_PORT SBC7240_ENABLE_PORT
  27. #define SBC7240_MAGIC_CHAR 'V'
  28. #define SBC7240_TIMEOUT 30
  29. #define SBC7240_MAX_TIMEOUT 255
  30. static int timeout = SBC7240_TIMEOUT; /* in seconds */
  31. module_param(timeout, int, 0);
  32. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<="
  33. __MODULE_STRING(SBC7240_MAX_TIMEOUT) ", default="
  34. __MODULE_STRING(SBC7240_TIMEOUT) ")");
  35. static bool nowayout = WATCHDOG_NOWAYOUT;
  36. module_param(nowayout, bool, 0);
  37. MODULE_PARM_DESC(nowayout, "Disable watchdog when closing device file");
  38. #define SBC7240_OPEN_STATUS_BIT 0
  39. #define SBC7240_ENABLED_STATUS_BIT 1
  40. #define SBC7240_EXPECT_CLOSE_STATUS_BIT 2
  41. static unsigned long wdt_status;
  42. /*
  43. * Utility routines
  44. */
  45. static void wdt_disable(void)
  46. {
  47. /* disable the watchdog */
  48. if (test_and_clear_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
  49. inb_p(SBC7240_DISABLE_PORT);
  50. pr_info("Watchdog timer is now disabled\n");
  51. }
  52. }
  53. static void wdt_enable(void)
  54. {
  55. /* enable the watchdog */
  56. if (!test_and_set_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
  57. inb_p(SBC7240_ENABLE_PORT);
  58. pr_info("Watchdog timer is now enabled\n");
  59. }
  60. }
  61. static int wdt_set_timeout(int t)
  62. {
  63. if (t < 1 || t > SBC7240_MAX_TIMEOUT) {
  64. pr_err("timeout value must be 1<=x<=%d\n", SBC7240_MAX_TIMEOUT);
  65. return -1;
  66. }
  67. /* set the timeout */
  68. outb_p((unsigned)t, SBC7240_SET_TIMEOUT_PORT);
  69. timeout = t;
  70. pr_info("timeout set to %d seconds\n", t);
  71. return 0;
  72. }
  73. /* Whack the dog */
  74. static inline void wdt_keepalive(void)
  75. {
  76. if (test_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status))
  77. inb_p(SBC7240_ENABLE_PORT);
  78. }
  79. /*
  80. * /dev/watchdog handling
  81. */
  82. static ssize_t fop_write(struct file *file, const char __user *buf,
  83. size_t count, loff_t *ppos)
  84. {
  85. size_t i;
  86. char c;
  87. if (count) {
  88. if (!nowayout) {
  89. clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
  90. &wdt_status);
  91. /* is there a magic char ? */
  92. for (i = 0; i != count; i++) {
  93. if (get_user(c, buf + i))
  94. return -EFAULT;
  95. if (c == SBC7240_MAGIC_CHAR) {
  96. set_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
  97. &wdt_status);
  98. break;
  99. }
  100. }
  101. }
  102. wdt_keepalive();
  103. }
  104. return count;
  105. }
  106. static int fop_open(struct inode *inode, struct file *file)
  107. {
  108. if (test_and_set_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status))
  109. return -EBUSY;
  110. wdt_enable();
  111. return stream_open(inode, file);
  112. }
  113. static int fop_close(struct inode *inode, struct file *file)
  114. {
  115. if (test_and_clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT, &wdt_status)
  116. || !nowayout) {
  117. wdt_disable();
  118. } else {
  119. pr_crit("Unexpected close, not stopping watchdog!\n");
  120. wdt_keepalive();
  121. }
  122. clear_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status);
  123. return 0;
  124. }
  125. static const struct watchdog_info ident = {
  126. .options = WDIOF_KEEPALIVEPING|
  127. WDIOF_SETTIMEOUT|
  128. WDIOF_MAGICCLOSE,
  129. .firmware_version = 1,
  130. .identity = "SBC7240",
  131. };
  132. static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  133. {
  134. switch (cmd) {
  135. case WDIOC_GETSUPPORT:
  136. return copy_to_user((void __user *)arg, &ident, sizeof(ident))
  137. ? -EFAULT : 0;
  138. case WDIOC_GETSTATUS:
  139. case WDIOC_GETBOOTSTATUS:
  140. return put_user(0, (int __user *)arg);
  141. case WDIOC_SETOPTIONS:
  142. {
  143. int options;
  144. int retval = -EINVAL;
  145. if (get_user(options, (int __user *)arg))
  146. return -EFAULT;
  147. if (options & WDIOS_DISABLECARD) {
  148. wdt_disable();
  149. retval = 0;
  150. }
  151. if (options & WDIOS_ENABLECARD) {
  152. wdt_enable();
  153. retval = 0;
  154. }
  155. return retval;
  156. }
  157. case WDIOC_KEEPALIVE:
  158. wdt_keepalive();
  159. return 0;
  160. case WDIOC_SETTIMEOUT:
  161. {
  162. int new_timeout;
  163. if (get_user(new_timeout, (int __user *)arg))
  164. return -EFAULT;
  165. if (wdt_set_timeout(new_timeout))
  166. return -EINVAL;
  167. }
  168. fallthrough;
  169. case WDIOC_GETTIMEOUT:
  170. return put_user(timeout, (int __user *)arg);
  171. default:
  172. return -ENOTTY;
  173. }
  174. }
  175. static const struct file_operations wdt_fops = {
  176. .owner = THIS_MODULE,
  177. .llseek = no_llseek,
  178. .write = fop_write,
  179. .open = fop_open,
  180. .release = fop_close,
  181. .unlocked_ioctl = fop_ioctl,
  182. .compat_ioctl = compat_ptr_ioctl,
  183. };
  184. static struct miscdevice wdt_miscdev = {
  185. .minor = WATCHDOG_MINOR,
  186. .name = "watchdog",
  187. .fops = &wdt_fops,
  188. };
  189. /*
  190. * Notifier for system down
  191. */
  192. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  193. void *unused)
  194. {
  195. if (code == SYS_DOWN || code == SYS_HALT)
  196. wdt_disable();
  197. return NOTIFY_DONE;
  198. }
  199. static struct notifier_block wdt_notifier = {
  200. .notifier_call = wdt_notify_sys,
  201. };
  202. static void __exit sbc7240_wdt_unload(void)
  203. {
  204. pr_info("Removing watchdog\n");
  205. misc_deregister(&wdt_miscdev);
  206. unregister_reboot_notifier(&wdt_notifier);
  207. release_region(SBC7240_ENABLE_PORT, 1);
  208. }
  209. static int __init sbc7240_wdt_init(void)
  210. {
  211. int rc = -EBUSY;
  212. if (!request_region(SBC7240_ENABLE_PORT, 1, "SBC7240 WDT")) {
  213. pr_err("I/O address 0x%04x already in use\n",
  214. SBC7240_ENABLE_PORT);
  215. rc = -EIO;
  216. goto err_out;
  217. }
  218. /* The IO port 0x043 used to disable the watchdog
  219. * is already claimed by the system timer, so we
  220. * can't request_region() it ...*/
  221. if (timeout < 1 || timeout > SBC7240_MAX_TIMEOUT) {
  222. timeout = SBC7240_TIMEOUT;
  223. pr_info("timeout value must be 1<=x<=%d, using %d\n",
  224. SBC7240_MAX_TIMEOUT, timeout);
  225. }
  226. wdt_set_timeout(timeout);
  227. wdt_disable();
  228. rc = register_reboot_notifier(&wdt_notifier);
  229. if (rc) {
  230. pr_err("cannot register reboot notifier (err=%d)\n", rc);
  231. goto err_out_region;
  232. }
  233. rc = misc_register(&wdt_miscdev);
  234. if (rc) {
  235. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  236. wdt_miscdev.minor, rc);
  237. goto err_out_reboot_notifier;
  238. }
  239. pr_info("Watchdog driver for SBC7240 initialised (nowayout=%d)\n",
  240. nowayout);
  241. return 0;
  242. err_out_reboot_notifier:
  243. unregister_reboot_notifier(&wdt_notifier);
  244. err_out_region:
  245. release_region(SBC7240_ENABLE_PORT, 1);
  246. err_out:
  247. return rc;
  248. }
  249. module_init(sbc7240_wdt_init);
  250. module_exit(sbc7240_wdt_unload);
  251. MODULE_AUTHOR("Gilles Gigan");
  252. MODULE_DESCRIPTION("Watchdog device driver for single board"
  253. " computers EPIC Nano 7240 from iEi");
  254. MODULE_LICENSE("GPL");