sbc7240_wdt.c 6.9 KB

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