scx200_wdt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* drivers/char/watchdog/scx200_wdt.c
  2. National Semiconductor SCx200 Watchdog support
  3. Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
  4. Some code taken from:
  5. National Semiconductor PC87307/PC97307 (ala SC1200) WDT driver
  6. (c) Copyright 2002 Zwane Mwaikambo <zwane@commfireservices.com>
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11. The author(s) of this software shall not be held liable for damages
  12. of any nature resulting due to the use of this software. This
  13. software is provided AS-IS with no warranties. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/notifier.h>
  20. #include <linux/reboot.h>
  21. #include <linux/fs.h>
  22. #include <linux/ioport.h>
  23. #include <linux/scx200.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/io.h>
  26. #define NAME "scx200_wdt"
  27. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  28. MODULE_DESCRIPTION("NatSemi SCx200 Watchdog Driver");
  29. MODULE_LICENSE("GPL");
  30. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  31. static int margin = 60; /* in seconds */
  32. module_param(margin, int, 0);
  33. MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
  34. static int nowayout = WATCHDOG_NOWAYOUT;
  35. module_param(nowayout, int, 0);
  36. MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
  37. static u16 wdto_restart;
  38. static char expect_close;
  39. static unsigned long open_lock;
  40. static DEFINE_SPINLOCK(scx_lock);
  41. /* Bits of the WDCNFG register */
  42. #define W_ENABLE 0x00fa /* Enable watchdog */
  43. #define W_DISABLE 0x0000 /* Disable watchdog */
  44. /* The scaling factor for the timer, this depends on the value of W_ENABLE */
  45. #define W_SCALE (32768/1024)
  46. static void scx200_wdt_ping(void)
  47. {
  48. spin_lock(&scx_lock);
  49. outw(wdto_restart, scx200_cb_base + SCx200_WDT_WDTO);
  50. spin_unlock(&scx_lock);
  51. }
  52. static void scx200_wdt_update_margin(void)
  53. {
  54. printk(KERN_INFO NAME ": timer margin %d seconds\n", margin);
  55. wdto_restart = margin * W_SCALE;
  56. }
  57. static void scx200_wdt_enable(void)
  58. {
  59. printk(KERN_DEBUG NAME ": enabling watchdog timer, wdto_restart = %d\n",
  60. wdto_restart);
  61. spin_lock(&scx_lock);
  62. outw(0, scx200_cb_base + SCx200_WDT_WDTO);
  63. outb(SCx200_WDT_WDSTS_WDOVF, scx200_cb_base + SCx200_WDT_WDSTS);
  64. outw(W_ENABLE, scx200_cb_base + SCx200_WDT_WDCNFG);
  65. spin_unlock(&scx_lock);
  66. scx200_wdt_ping();
  67. }
  68. static void scx200_wdt_disable(void)
  69. {
  70. printk(KERN_DEBUG NAME ": disabling watchdog timer\n");
  71. spin_lock(&scx_lock);
  72. outw(0, scx200_cb_base + SCx200_WDT_WDTO);
  73. outb(SCx200_WDT_WDSTS_WDOVF, scx200_cb_base + SCx200_WDT_WDSTS);
  74. outw(W_DISABLE, scx200_cb_base + SCx200_WDT_WDCNFG);
  75. spin_unlock(&scx_lock);
  76. }
  77. static int scx200_wdt_open(struct inode *inode, struct file *file)
  78. {
  79. /* only allow one at a time */
  80. if (test_and_set_bit(0, &open_lock))
  81. return -EBUSY;
  82. scx200_wdt_enable();
  83. return nonseekable_open(inode, file);
  84. }
  85. static int scx200_wdt_release(struct inode *inode, struct file *file)
  86. {
  87. if (expect_close != 42)
  88. printk(KERN_WARNING NAME
  89. ": watchdog device closed unexpectedly, "
  90. "will not disable the watchdog timer\n");
  91. else if (!nowayout)
  92. scx200_wdt_disable();
  93. expect_close = 0;
  94. clear_bit(0, &open_lock);
  95. return 0;
  96. }
  97. static int scx200_wdt_notify_sys(struct notifier_block *this,
  98. unsigned long code, void *unused)
  99. {
  100. if (code == SYS_HALT || code == SYS_POWER_OFF)
  101. if (!nowayout)
  102. scx200_wdt_disable();
  103. return NOTIFY_DONE;
  104. }
  105. static struct notifier_block scx200_wdt_notifier = {
  106. .notifier_call = scx200_wdt_notify_sys,
  107. };
  108. static ssize_t scx200_wdt_write(struct file *file, const char __user *data,
  109. size_t len, loff_t *ppos)
  110. {
  111. /* check for a magic close character */
  112. if (len) {
  113. size_t i;
  114. scx200_wdt_ping();
  115. expect_close = 0;
  116. for (i = 0; i < len; ++i) {
  117. char c;
  118. if (get_user(c, data + i))
  119. return -EFAULT;
  120. if (c == 'V')
  121. expect_close = 42;
  122. }
  123. return len;
  124. }
  125. return 0;
  126. }
  127. static long scx200_wdt_ioctl(struct file *file, unsigned int cmd,
  128. unsigned long arg)
  129. {
  130. void __user *argp = (void __user *)arg;
  131. int __user *p = argp;
  132. static const struct watchdog_info ident = {
  133. .identity = "NatSemi SCx200 Watchdog",
  134. .firmware_version = 1,
  135. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  136. WDIOF_MAGICCLOSE,
  137. };
  138. int new_margin;
  139. switch (cmd) {
  140. case WDIOC_GETSUPPORT:
  141. if (copy_to_user(argp, &ident, sizeof(ident)))
  142. return -EFAULT;
  143. return 0;
  144. case WDIOC_GETSTATUS:
  145. case WDIOC_GETBOOTSTATUS:
  146. if (put_user(0, p))
  147. return -EFAULT;
  148. return 0;
  149. case WDIOC_KEEPALIVE:
  150. scx200_wdt_ping();
  151. return 0;
  152. case WDIOC_SETTIMEOUT:
  153. if (get_user(new_margin, p))
  154. return -EFAULT;
  155. if (new_margin < 1)
  156. return -EINVAL;
  157. margin = new_margin;
  158. scx200_wdt_update_margin();
  159. scx200_wdt_ping();
  160. case WDIOC_GETTIMEOUT:
  161. if (put_user(margin, p))
  162. return -EFAULT;
  163. return 0;
  164. default:
  165. return -ENOTTY;
  166. }
  167. }
  168. static const struct file_operations scx200_wdt_fops = {
  169. .owner = THIS_MODULE,
  170. .llseek = no_llseek,
  171. .write = scx200_wdt_write,
  172. .unlocked_ioctl = scx200_wdt_ioctl,
  173. .open = scx200_wdt_open,
  174. .release = scx200_wdt_release,
  175. };
  176. static struct miscdevice scx200_wdt_miscdev = {
  177. .minor = WATCHDOG_MINOR,
  178. .name = "watchdog",
  179. .fops = &scx200_wdt_fops,
  180. };
  181. static int __init scx200_wdt_init(void)
  182. {
  183. int r;
  184. printk(KERN_DEBUG NAME ": NatSemi SCx200 Watchdog Driver\n");
  185. /* check that we have found the configuration block */
  186. if (!scx200_cb_present())
  187. return -ENODEV;
  188. if (!request_region(scx200_cb_base + SCx200_WDT_OFFSET,
  189. SCx200_WDT_SIZE,
  190. "NatSemi SCx200 Watchdog")) {
  191. printk(KERN_WARNING NAME ": watchdog I/O region busy\n");
  192. return -EBUSY;
  193. }
  194. scx200_wdt_update_margin();
  195. scx200_wdt_disable();
  196. r = register_reboot_notifier(&scx200_wdt_notifier);
  197. if (r) {
  198. printk(KERN_ERR NAME ": unable to register reboot notifier");
  199. release_region(scx200_cb_base + SCx200_WDT_OFFSET,
  200. SCx200_WDT_SIZE);
  201. return r;
  202. }
  203. r = misc_register(&scx200_wdt_miscdev);
  204. if (r) {
  205. unregister_reboot_notifier(&scx200_wdt_notifier);
  206. release_region(scx200_cb_base + SCx200_WDT_OFFSET,
  207. SCx200_WDT_SIZE);
  208. return r;
  209. }
  210. return 0;
  211. }
  212. static void __exit scx200_wdt_cleanup(void)
  213. {
  214. misc_deregister(&scx200_wdt_miscdev);
  215. unregister_reboot_notifier(&scx200_wdt_notifier);
  216. release_region(scx200_cb_base + SCx200_WDT_OFFSET,
  217. SCx200_WDT_SIZE);
  218. }
  219. module_init(scx200_wdt_init);
  220. module_exit(scx200_wdt_cleanup);
  221. /*
  222. Local variables:
  223. compile-command: "make -k -C ../.. SUBDIRS=drivers/char modules"
  224. c-basic-offset: 8
  225. End:
  226. */