mtx-1_wdt.c 6.4 KB

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