sbc7240_wdt.c 6.9 KB

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