mtx-1_wdt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Driver for the MTX-1 Watchdog.
  3. *
  4. * (C) Copyright 2005 4G Systems <info@4g-systems.biz>, All Rights Reserved.
  5. * http://www.4g-systems.biz
  6. *
  7. * (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Neither Michael Stickel nor 4G Systems admit liability nor provide
  15. * warranty for any of this software. This material is provided
  16. * "AS-IS" and at no charge.
  17. *
  18. * (c) Copyright 2005 4G Systems <info@4g-systems.biz>
  19. *
  20. * Release 0.01.
  21. * Author: Michael Stickel michael.stickel@4g-systems.biz
  22. *
  23. * Release 0.02.
  24. * Author: Florian Fainelli florian@openwrt.org
  25. * use the Linux watchdog/timer APIs
  26. *
  27. * The Watchdog is configured to reset the MTX-1
  28. * if it is not triggered for 100 seconds.
  29. * It should not be triggered more often than 1.6 seconds.
  30. *
  31. * A timer triggers the watchdog every 5 seconds, until
  32. * it is opened for the first time. After the first open
  33. * it MUST be triggered every 2..95 seconds.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/types.h>
  38. #include <linux/errno.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/fs.h>
  41. #include <linux/init.h>
  42. #include <linux/ioport.h>
  43. #include <linux/timer.h>
  44. #include <linux/completion.h>
  45. #include <linux/jiffies.h>
  46. #include <linux/watchdog.h>
  47. #include <asm/io.h>
  48. #include <asm/uaccess.h>
  49. #include <asm/mach-au1x00/au1000.h>
  50. #define MTX1_WDT_INTERVAL (5 * HZ)
  51. static int ticks = 100 * HZ;
  52. static struct {
  53. struct completion stop;
  54. volatile int running;
  55. struct timer_list timer;
  56. volatile int queue;
  57. int default_ticks;
  58. unsigned long inuse;
  59. } mtx1_wdt_device;
  60. static void mtx1_wdt_trigger(unsigned long unused)
  61. {
  62. u32 tmp;
  63. if (mtx1_wdt_device.running)
  64. ticks--;
  65. /*
  66. * toggle GPIO2_15
  67. */
  68. tmp = au_readl(GPIO2_DIR);
  69. tmp = (tmp & ~(1<<15)) | ((~tmp) & (1<<15));
  70. au_writel (tmp, GPIO2_DIR);
  71. if (mtx1_wdt_device.queue && ticks)
  72. mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
  73. else {
  74. complete(&mtx1_wdt_device.stop);
  75. }
  76. }
  77. static void mtx1_wdt_reset(void)
  78. {
  79. ticks = mtx1_wdt_device.default_ticks;
  80. }
  81. static void mtx1_wdt_start(void)
  82. {
  83. if (!mtx1_wdt_device.queue) {
  84. mtx1_wdt_device.queue = 1;
  85. au_writel (au_readl(GPIO2_DIR) | (u32)(1<<15), GPIO2_DIR);
  86. mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
  87. }
  88. mtx1_wdt_device.running++;
  89. }
  90. static int mtx1_wdt_stop(void)
  91. {
  92. if (mtx1_wdt_device.queue) {
  93. mtx1_wdt_device.queue = 0;
  94. au_writel (au_readl(GPIO2_DIR) & ~((u32)(1<<15)), GPIO2_DIR);
  95. }
  96. ticks = mtx1_wdt_device.default_ticks;
  97. return 0;
  98. }
  99. /* Filesystem functions */
  100. static int mtx1_wdt_open(struct inode *inode, struct file *file)
  101. {
  102. if (test_and_set_bit(0, &mtx1_wdt_device.inuse))
  103. return -EBUSY;
  104. return nonseekable_open(inode, file);
  105. }
  106. static int mtx1_wdt_release(struct inode *inode, struct file *file)
  107. {
  108. clear_bit(0, &mtx1_wdt_device.inuse);
  109. return 0;
  110. }
  111. static int mtx1_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  112. {
  113. void __user *argp = (void __user *)arg;
  114. unsigned int value;
  115. static struct watchdog_info ident =
  116. {
  117. .options = WDIOF_CARDRESET,
  118. .identity = "MTX-1 WDT",
  119. };
  120. switch(cmd) {
  121. case WDIOC_KEEPALIVE:
  122. mtx1_wdt_reset();
  123. break;
  124. case WDIOC_GETSTATUS:
  125. case WDIOC_GETBOOTSTATUS:
  126. if ( copy_to_user(argp, &value, sizeof(int)) )
  127. return -EFAULT;
  128. break;
  129. case WDIOC_GETSUPPORT:
  130. if ( copy_to_user(argp, &ident, sizeof(ident)) )
  131. return -EFAULT;
  132. break;
  133. case WDIOC_SETOPTIONS:
  134. if ( copy_from_user(&value, argp, sizeof(int)) )
  135. return -EFAULT;
  136. switch(value) {
  137. case WDIOS_ENABLECARD:
  138. mtx1_wdt_start();
  139. break;
  140. case WDIOS_DISABLECARD:
  141. return mtx1_wdt_stop();
  142. default:
  143. return -EINVAL;
  144. }
  145. break;
  146. default:
  147. return -ENOTTY;
  148. }
  149. return 0;
  150. }
  151. static ssize_t mtx1_wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
  152. {
  153. if (!count)
  154. return -EIO;
  155. mtx1_wdt_reset();
  156. return count;
  157. }
  158. static struct file_operations mtx1_wdt_fops = {
  159. .owner = THIS_MODULE,
  160. .llseek = no_llseek,
  161. .ioctl = mtx1_wdt_ioctl,
  162. .open = mtx1_wdt_open,
  163. .write = mtx1_wdt_write,
  164. .release = mtx1_wdt_release
  165. };
  166. static struct miscdevice mtx1_wdt_misc = {
  167. .minor = WATCHDOG_MINOR,
  168. .name = "watchdog",
  169. .fops = &mtx1_wdt_fops
  170. };
  171. static int __init mtx1_wdt_init(void)
  172. {
  173. int ret;
  174. if ((ret = misc_register(&mtx1_wdt_misc)) < 0) {
  175. printk(KERN_ERR " mtx-1_wdt : failed to register\n");
  176. return ret;
  177. }
  178. init_completion(&mtx1_wdt_device.stop);
  179. mtx1_wdt_device.queue = 0;
  180. clear_bit(0, &mtx1_wdt_device.inuse);
  181. setup_timer(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0L);
  182. mtx1_wdt_device.default_ticks = ticks;
  183. mtx1_wdt_start();
  184. printk(KERN_INFO "MTX-1 Watchdog driver\n");
  185. return 0;
  186. }
  187. static void __exit mtx1_wdt_exit(void)
  188. {
  189. if (mtx1_wdt_device.queue) {
  190. mtx1_wdt_device.queue = 0;
  191. wait_for_completion(&mtx1_wdt_device.stop);
  192. }
  193. misc_deregister(&mtx1_wdt_misc);
  194. }
  195. module_init(mtx1_wdt_init);
  196. module_exit(mtx1_wdt_exit);
  197. MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
  198. MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
  199. MODULE_LICENSE("GPL");