cpu5wdt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * sma cpu5 watchdog driver
  3. *
  4. * Copyright (C) 2003 Heiko Ronsdorf <hero@ihg.uni-duisburg.de>
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/types.h>
  25. #include <linux/errno.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/fs.h>
  28. #include <linux/init.h>
  29. #include <linux/ioport.h>
  30. #include <linux/timer.h>
  31. #include <linux/completion.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/io.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/watchdog.h>
  36. /* adjustable parameters */
  37. static int verbose;
  38. static int port = 0x91;
  39. static int ticks = 10000;
  40. static DEFINE_SPINLOCK(cpu5wdt_lock);
  41. #define PFX "cpu5wdt: "
  42. #define CPU5WDT_EXTENT 0x0A
  43. #define CPU5WDT_STATUS_REG 0x00
  44. #define CPU5WDT_TIME_A_REG 0x02
  45. #define CPU5WDT_TIME_B_REG 0x03
  46. #define CPU5WDT_MODE_REG 0x04
  47. #define CPU5WDT_TRIGGER_REG 0x07
  48. #define CPU5WDT_ENABLE_REG 0x08
  49. #define CPU5WDT_RESET_REG 0x09
  50. #define CPU5WDT_INTERVAL (HZ/10+1)
  51. /* some device data */
  52. static struct {
  53. struct completion stop;
  54. int running;
  55. struct timer_list timer;
  56. int queue;
  57. int default_ticks;
  58. unsigned long inuse;
  59. } cpu5wdt_device;
  60. /* generic helper functions */
  61. static void cpu5wdt_trigger(unsigned long unused)
  62. {
  63. if (verbose > 2)
  64. pr_debug("trigger at %i ticks\n", ticks);
  65. if (cpu5wdt_device.running)
  66. ticks--;
  67. spin_lock(&cpu5wdt_lock);
  68. /* keep watchdog alive */
  69. outb(1, port + CPU5WDT_TRIGGER_REG);
  70. /* requeue?? */
  71. if (cpu5wdt_device.queue && ticks)
  72. mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
  73. else {
  74. /* ticks doesn't matter anyway */
  75. complete(&cpu5wdt_device.stop);
  76. }
  77. spin_unlock(&cpu5wdt_lock);
  78. }
  79. static void cpu5wdt_reset(void)
  80. {
  81. ticks = cpu5wdt_device.default_ticks;
  82. if (verbose)
  83. pr_debug("reset (%i ticks)\n", (int) ticks);
  84. }
  85. static void cpu5wdt_start(void)
  86. {
  87. unsigned long flags;
  88. spin_lock_irqsave(&cpu5wdt_lock, flags);
  89. if (!cpu5wdt_device.queue) {
  90. cpu5wdt_device.queue = 1;
  91. outb(0, port + CPU5WDT_TIME_A_REG);
  92. outb(0, port + CPU5WDT_TIME_B_REG);
  93. outb(1, port + CPU5WDT_MODE_REG);
  94. outb(0, port + CPU5WDT_RESET_REG);
  95. outb(0, port + CPU5WDT_ENABLE_REG);
  96. mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
  97. }
  98. /* if process dies, counter is not decremented */
  99. cpu5wdt_device.running++;
  100. spin_unlock_irqrestore(&cpu5wdt_lock, flags);
  101. }
  102. static int cpu5wdt_stop(void)
  103. {
  104. unsigned long flags;
  105. spin_lock_irqsave(&cpu5wdt_lock, flags);
  106. if (cpu5wdt_device.running)
  107. cpu5wdt_device.running = 0;
  108. ticks = cpu5wdt_device.default_ticks;
  109. spin_unlock_irqrestore(&cpu5wdt_lock, flags);
  110. if (verbose)
  111. pr_crit("stop not possible\n");
  112. return -EIO;
  113. }
  114. /* filesystem operations */
  115. static int cpu5wdt_open(struct inode *inode, struct file *file)
  116. {
  117. if (test_and_set_bit(0, &cpu5wdt_device.inuse))
  118. return -EBUSY;
  119. return nonseekable_open(inode, file);
  120. }
  121. static int cpu5wdt_release(struct inode *inode, struct file *file)
  122. {
  123. clear_bit(0, &cpu5wdt_device.inuse);
  124. return 0;
  125. }
  126. static long cpu5wdt_ioctl(struct file *file, unsigned int cmd,
  127. unsigned long arg)
  128. {
  129. void __user *argp = (void __user *)arg;
  130. int __user *p = argp;
  131. unsigned int value;
  132. static const struct watchdog_info ident = {
  133. .options = WDIOF_CARDRESET,
  134. .identity = "CPU5 WDT",
  135. };
  136. switch (cmd) {
  137. case WDIOC_GETSUPPORT:
  138. if (copy_to_user(argp, &ident, sizeof(ident)))
  139. return -EFAULT;
  140. break;
  141. case WDIOC_GETSTATUS:
  142. value = inb(port + CPU5WDT_STATUS_REG);
  143. value = (value >> 2) & 1;
  144. return put_user(value, p);
  145. case WDIOC_GETBOOTSTATUS:
  146. return put_user(0, p);
  147. case WDIOC_SETOPTIONS:
  148. if (get_user(value, p))
  149. return -EFAULT;
  150. if (value & WDIOS_ENABLECARD)
  151. cpu5wdt_start();
  152. if (value & WDIOS_DISABLECARD)
  153. cpu5wdt_stop();
  154. break;
  155. case WDIOC_KEEPALIVE:
  156. cpu5wdt_reset();
  157. break;
  158. default:
  159. return -ENOTTY;
  160. }
  161. return 0;
  162. }
  163. static ssize_t cpu5wdt_write(struct file *file, const char __user *buf,
  164. size_t count, loff_t *ppos)
  165. {
  166. if (!count)
  167. return -EIO;
  168. cpu5wdt_reset();
  169. return count;
  170. }
  171. static const struct file_operations cpu5wdt_fops = {
  172. .owner = THIS_MODULE,
  173. .llseek = no_llseek,
  174. .unlocked_ioctl = cpu5wdt_ioctl,
  175. .open = cpu5wdt_open,
  176. .write = cpu5wdt_write,
  177. .release = cpu5wdt_release,
  178. };
  179. static struct miscdevice cpu5wdt_misc = {
  180. .minor = WATCHDOG_MINOR,
  181. .name = "watchdog",
  182. .fops = &cpu5wdt_fops,
  183. };
  184. /* init/exit function */
  185. static int __devinit cpu5wdt_init(void)
  186. {
  187. unsigned int val;
  188. int err;
  189. if (verbose)
  190. pr_debug("port=0x%x, verbose=%i\n", port, verbose);
  191. init_completion(&cpu5wdt_device.stop);
  192. cpu5wdt_device.queue = 0;
  193. setup_timer(&cpu5wdt_device.timer, cpu5wdt_trigger, 0);
  194. cpu5wdt_device.default_ticks = ticks;
  195. if (!request_region(port, CPU5WDT_EXTENT, PFX)) {
  196. pr_err("request_region failed\n");
  197. err = -EBUSY;
  198. goto no_port;
  199. }
  200. /* watchdog reboot? */
  201. val = inb(port + CPU5WDT_STATUS_REG);
  202. val = (val >> 2) & 1;
  203. if (!val)
  204. pr_info("sorry, was my fault\n");
  205. err = misc_register(&cpu5wdt_misc);
  206. if (err < 0) {
  207. pr_err("misc_register failed\n");
  208. goto no_misc;
  209. }
  210. pr_info("init success\n");
  211. return 0;
  212. no_misc:
  213. release_region(port, CPU5WDT_EXTENT);
  214. no_port:
  215. return err;
  216. }
  217. static int __devinit cpu5wdt_init_module(void)
  218. {
  219. return cpu5wdt_init();
  220. }
  221. static void __devexit cpu5wdt_exit(void)
  222. {
  223. if (cpu5wdt_device.queue) {
  224. cpu5wdt_device.queue = 0;
  225. wait_for_completion(&cpu5wdt_device.stop);
  226. }
  227. misc_deregister(&cpu5wdt_misc);
  228. release_region(port, CPU5WDT_EXTENT);
  229. }
  230. static void __devexit cpu5wdt_exit_module(void)
  231. {
  232. cpu5wdt_exit();
  233. }
  234. /* module entry points */
  235. module_init(cpu5wdt_init_module);
  236. module_exit(cpu5wdt_exit_module);
  237. MODULE_AUTHOR("Heiko Ronsdorf <hero@ihg.uni-duisburg.de>");
  238. MODULE_DESCRIPTION("sma cpu5 watchdog driver");
  239. MODULE_SUPPORTED_DEVICE("sma cpu5 watchdog");
  240. MODULE_LICENSE("GPL");
  241. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  242. module_param(port, int, 0);
  243. MODULE_PARM_DESC(port, "base address of watchdog card, default is 0x91");
  244. module_param(verbose, int, 0);
  245. MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
  246. module_param(ticks, int, 0);
  247. MODULE_PARM_DESC(ticks, "count down ticks, default is 10000");