sbc7240_wdt.c 7.3 KB

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