cpu5wdt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/fs.h>
  27. #include <linux/init.h>
  28. #include <linux/ioport.h>
  29. #include <linux/timer.h>
  30. #include <asm/io.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/watchdog.h>
  33. /* adjustable parameters */
  34. static int verbose = 0;
  35. static int port = 0x91;
  36. static int ticks = 10000;
  37. #define PFX "cpu5wdt: "
  38. #define CPU5WDT_EXTENT 0x0A
  39. #define CPU5WDT_STATUS_REG 0x00
  40. #define CPU5WDT_TIME_A_REG 0x02
  41. #define CPU5WDT_TIME_B_REG 0x03
  42. #define CPU5WDT_MODE_REG 0x04
  43. #define CPU5WDT_TRIGGER_REG 0x07
  44. #define CPU5WDT_ENABLE_REG 0x08
  45. #define CPU5WDT_RESET_REG 0x09
  46. #define CPU5WDT_INTERVAL (HZ/10+1)
  47. /* some device data */
  48. static struct {
  49. struct semaphore stop;
  50. volatile int running;
  51. struct timer_list timer;
  52. volatile int queue;
  53. int default_ticks;
  54. unsigned long inuse;
  55. } cpu5wdt_device;
  56. /* generic helper functions */
  57. static void cpu5wdt_trigger(unsigned long unused)
  58. {
  59. if ( verbose > 2 )
  60. printk(KERN_DEBUG PFX "trigger at %i ticks\n", ticks);
  61. if( cpu5wdt_device.running )
  62. ticks--;
  63. /* keep watchdog alive */
  64. outb(1, port + CPU5WDT_TRIGGER_REG);
  65. /* requeue?? */
  66. if( cpu5wdt_device.queue && ticks ) {
  67. cpu5wdt_device.timer.expires = jiffies + CPU5WDT_INTERVAL;
  68. add_timer(&cpu5wdt_device.timer);
  69. }
  70. else {
  71. /* ticks doesn't matter anyway */
  72. up(&cpu5wdt_device.stop);
  73. }
  74. }
  75. static void cpu5wdt_reset(void)
  76. {
  77. ticks = cpu5wdt_device.default_ticks;
  78. if ( verbose )
  79. printk(KERN_DEBUG PFX "reset (%i ticks)\n", (int) ticks);
  80. }
  81. static void cpu5wdt_start(void)
  82. {
  83. if ( !cpu5wdt_device.queue ) {
  84. cpu5wdt_device.queue = 1;
  85. outb(0, port + CPU5WDT_TIME_A_REG);
  86. outb(0, port + CPU5WDT_TIME_B_REG);
  87. outb(1, port + CPU5WDT_MODE_REG);
  88. outb(0, port + CPU5WDT_RESET_REG);
  89. outb(0, port + CPU5WDT_ENABLE_REG);
  90. cpu5wdt_device.timer.expires = jiffies + CPU5WDT_INTERVAL;
  91. add_timer(&cpu5wdt_device.timer);
  92. }
  93. /* if process dies, counter is not decremented */
  94. cpu5wdt_device.running++;
  95. }
  96. static int cpu5wdt_stop(void)
  97. {
  98. if ( cpu5wdt_device.running )
  99. cpu5wdt_device.running = 0;
  100. ticks = cpu5wdt_device.default_ticks;
  101. if ( verbose )
  102. printk(KERN_CRIT PFX "stop not possible\n");
  103. return -EIO;
  104. }
  105. /* filesystem operations */
  106. static int cpu5wdt_open(struct inode *inode, struct file *file)
  107. {
  108. if ( test_and_set_bit(0, &cpu5wdt_device.inuse) )
  109. return -EBUSY;
  110. return nonseekable_open(inode, file);
  111. }
  112. static int cpu5wdt_release(struct inode *inode, struct file *file)
  113. {
  114. clear_bit(0, &cpu5wdt_device.inuse);
  115. return 0;
  116. }
  117. static int cpu5wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  118. {
  119. void __user *argp = (void __user *)arg;
  120. unsigned int value;
  121. static struct watchdog_info ident =
  122. {
  123. .options = WDIOF_CARDRESET,
  124. .identity = "CPU5 WDT",
  125. };
  126. switch(cmd) {
  127. case WDIOC_KEEPALIVE:
  128. cpu5wdt_reset();
  129. break;
  130. case WDIOC_GETSTATUS:
  131. value = inb(port + CPU5WDT_STATUS_REG);
  132. value = (value >> 2) & 1;
  133. if ( copy_to_user(argp, &value, sizeof(int)) )
  134. return -EFAULT;
  135. break;
  136. case WDIOC_GETSUPPORT:
  137. if ( copy_to_user(argp, &ident, sizeof(ident)) )
  138. return -EFAULT;
  139. break;
  140. case WDIOC_SETOPTIONS:
  141. if ( copy_from_user(&value, argp, sizeof(int)) )
  142. return -EFAULT;
  143. switch(value) {
  144. case WDIOS_ENABLECARD:
  145. cpu5wdt_start();
  146. break;
  147. case WDIOS_DISABLECARD:
  148. return cpu5wdt_stop();
  149. default:
  150. return -EINVAL;
  151. }
  152. break;
  153. default:
  154. return -ENOIOCTLCMD;
  155. }
  156. return 0;
  157. }
  158. static ssize_t cpu5wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  159. {
  160. if ( !count )
  161. return -EIO;
  162. cpu5wdt_reset();
  163. return count;
  164. }
  165. static struct file_operations cpu5wdt_fops = {
  166. .owner = THIS_MODULE,
  167. .llseek = no_llseek,
  168. .ioctl = cpu5wdt_ioctl,
  169. .open = cpu5wdt_open,
  170. .write = cpu5wdt_write,
  171. .release = cpu5wdt_release,
  172. };
  173. static struct miscdevice cpu5wdt_misc = {
  174. .minor = WATCHDOG_MINOR,
  175. .name = "watchdog",
  176. .fops = &cpu5wdt_fops,
  177. };
  178. /* init/exit function */
  179. static int __devinit cpu5wdt_init(void)
  180. {
  181. unsigned int val;
  182. int err;
  183. if ( verbose )
  184. printk(KERN_DEBUG PFX "port=0x%x, verbose=%i\n", port, verbose);
  185. if ( (err = misc_register(&cpu5wdt_misc)) < 0 ) {
  186. printk(KERN_ERR PFX "misc_register failed\n");
  187. goto no_misc;
  188. }
  189. if ( !request_region(port, CPU5WDT_EXTENT, PFX) ) {
  190. printk(KERN_ERR PFX "request_region failed\n");
  191. err = -EBUSY;
  192. goto no_port;
  193. }
  194. /* watchdog reboot? */
  195. val = inb(port + CPU5WDT_STATUS_REG);
  196. val = (val >> 2) & 1;
  197. if ( !val )
  198. printk(KERN_INFO PFX "sorry, was my fault\n");
  199. init_MUTEX_LOCKED(&cpu5wdt_device.stop);
  200. cpu5wdt_device.queue = 0;
  201. clear_bit(0, &cpu5wdt_device.inuse);
  202. init_timer(&cpu5wdt_device.timer);
  203. cpu5wdt_device.timer.function = cpu5wdt_trigger;
  204. cpu5wdt_device.timer.data = 0;
  205. cpu5wdt_device.default_ticks = ticks;
  206. printk(KERN_INFO PFX "init success\n");
  207. return 0;
  208. no_port:
  209. misc_deregister(&cpu5wdt_misc);
  210. no_misc:
  211. return err;
  212. }
  213. static int __devinit cpu5wdt_init_module(void)
  214. {
  215. return cpu5wdt_init();
  216. }
  217. static void __devexit cpu5wdt_exit(void)
  218. {
  219. if ( cpu5wdt_device.queue ) {
  220. cpu5wdt_device.queue = 0;
  221. down(&cpu5wdt_device.stop);
  222. }
  223. misc_deregister(&cpu5wdt_misc);
  224. release_region(port, CPU5WDT_EXTENT);
  225. }
  226. static void __devexit cpu5wdt_exit_module(void)
  227. {
  228. cpu5wdt_exit();
  229. }
  230. /* module entry points */
  231. module_init(cpu5wdt_init_module);
  232. module_exit(cpu5wdt_exit_module);
  233. MODULE_AUTHOR("Heiko Ronsdorf <hero@ihg.uni-duisburg.de>");
  234. MODULE_DESCRIPTION("sma cpu5 watchdog driver");
  235. MODULE_SUPPORTED_DEVICE("sma cpu5 watchdog");
  236. MODULE_LICENSE("GPL");
  237. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  238. module_param(port, int, 0);
  239. MODULE_PARM_DESC(port, "base address of watchdog card, default is 0x91");
  240. module_param(verbose, int, 0);
  241. MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
  242. module_param(ticks, int, 0);
  243. MODULE_PARM_DESC(ticks, "count down ticks, default is 10000");