sbc7240_wdt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. /* Fall through */
  168. }
  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. };
  183. static struct miscdevice wdt_miscdev = {
  184. .minor = WATCHDOG_MINOR,
  185. .name = "watchdog",
  186. .fops = &wdt_fops,
  187. };
  188. /*
  189. * Notifier for system down
  190. */
  191. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  192. void *unused)
  193. {
  194. if (code == SYS_DOWN || code == SYS_HALT)
  195. wdt_disable();
  196. return NOTIFY_DONE;
  197. }
  198. static struct notifier_block wdt_notifier = {
  199. .notifier_call = wdt_notify_sys,
  200. };
  201. static void __exit sbc7240_wdt_unload(void)
  202. {
  203. pr_info("Removing watchdog\n");
  204. misc_deregister(&wdt_miscdev);
  205. unregister_reboot_notifier(&wdt_notifier);
  206. release_region(SBC7240_ENABLE_PORT, 1);
  207. }
  208. static int __init sbc7240_wdt_init(void)
  209. {
  210. int rc = -EBUSY;
  211. if (!request_region(SBC7240_ENABLE_PORT, 1, "SBC7240 WDT")) {
  212. pr_err("I/O address 0x%04x already in use\n",
  213. SBC7240_ENABLE_PORT);
  214. rc = -EIO;
  215. goto err_out;
  216. }
  217. /* The IO port 0x043 used to disable the watchdog
  218. * is already claimed by the system timer, so we
  219. * can't request_region() it ...*/
  220. if (timeout < 1 || timeout > SBC7240_MAX_TIMEOUT) {
  221. timeout = SBC7240_TIMEOUT;
  222. pr_info("timeout value must be 1<=x<=%d, using %d\n",
  223. SBC7240_MAX_TIMEOUT, timeout);
  224. }
  225. wdt_set_timeout(timeout);
  226. wdt_disable();
  227. rc = register_reboot_notifier(&wdt_notifier);
  228. if (rc) {
  229. pr_err("cannot register reboot notifier (err=%d)\n", rc);
  230. goto err_out_region;
  231. }
  232. rc = misc_register(&wdt_miscdev);
  233. if (rc) {
  234. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  235. wdt_miscdev.minor, rc);
  236. goto err_out_reboot_notifier;
  237. }
  238. pr_info("Watchdog driver for SBC7240 initialised (nowayout=%d)\n",
  239. nowayout);
  240. return 0;
  241. err_out_reboot_notifier:
  242. unregister_reboot_notifier(&wdt_notifier);
  243. err_out_region:
  244. release_region(SBC7240_ENABLE_PORT, 1);
  245. err_out:
  246. return rc;
  247. }
  248. module_init(sbc7240_wdt_init);
  249. module_exit(sbc7240_wdt_unload);
  250. MODULE_AUTHOR("Gilles Gigan");
  251. MODULE_DESCRIPTION("Watchdog device driver for single board"
  252. " computers EPIC Nano 7240 from iEi");
  253. MODULE_LICENSE("GPL");