mtx-1_wdt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. unsigned int gstate;
  65. } mtx1_wdt_device;
  66. static void mtx1_wdt_trigger(unsigned long unused)
  67. {
  68. spin_lock(&mtx1_wdt_device.lock);
  69. if (mtx1_wdt_device.running)
  70. ticks--;
  71. /* toggle wdt gpio */
  72. mtx1_wdt_device.gstate = !mtx1_wdt_device.gstate;
  73. gpio_set_value(mtx1_wdt_device.gpio, mtx1_wdt_device.gstate);
  74. if (mtx1_wdt_device.queue && ticks)
  75. mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
  76. else
  77. complete(&mtx1_wdt_device.stop);
  78. spin_unlock(&mtx1_wdt_device.lock);
  79. }
  80. static void mtx1_wdt_reset(void)
  81. {
  82. ticks = mtx1_wdt_device.default_ticks;
  83. }
  84. static void mtx1_wdt_start(void)
  85. {
  86. unsigned long flags;
  87. spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
  88. if (!mtx1_wdt_device.queue) {
  89. mtx1_wdt_device.queue = 1;
  90. mtx1_wdt_device.gstate = 1;
  91. gpio_set_value(mtx1_wdt_device.gpio, 1);
  92. mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
  93. }
  94. mtx1_wdt_device.running++;
  95. spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
  96. }
  97. static int mtx1_wdt_stop(void)
  98. {
  99. unsigned long flags;
  100. spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
  101. if (mtx1_wdt_device.queue) {
  102. mtx1_wdt_device.queue = 0;
  103. mtx1_wdt_device.gstate = 0;
  104. gpio_set_value(mtx1_wdt_device.gpio, 0);
  105. }
  106. ticks = mtx1_wdt_device.default_ticks;
  107. spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
  108. return 0;
  109. }
  110. /* Filesystem functions */
  111. static int mtx1_wdt_open(struct inode *inode, struct file *file)
  112. {
  113. if (test_and_set_bit(0, &mtx1_wdt_device.inuse))
  114. return -EBUSY;
  115. return nonseekable_open(inode, file);
  116. }
  117. static int mtx1_wdt_release(struct inode *inode, struct file *file)
  118. {
  119. clear_bit(0, &mtx1_wdt_device.inuse);
  120. return 0;
  121. }
  122. static long mtx1_wdt_ioctl(struct file *file, unsigned int cmd,
  123. unsigned long arg)
  124. {
  125. void __user *argp = (void __user *)arg;
  126. int __user *p = (int __user *)argp;
  127. unsigned int value;
  128. static const struct watchdog_info ident = {
  129. .options = WDIOF_CARDRESET,
  130. .identity = "MTX-1 WDT",
  131. };
  132. switch (cmd) {
  133. case WDIOC_GETSUPPORT:
  134. if (copy_to_user(argp, &ident, sizeof(ident)))
  135. return -EFAULT;
  136. break;
  137. case WDIOC_GETSTATUS:
  138. case WDIOC_GETBOOTSTATUS:
  139. put_user(0, p);
  140. break;
  141. case WDIOC_SETOPTIONS:
  142. if (get_user(value, p))
  143. return -EFAULT;
  144. if (value & WDIOS_ENABLECARD)
  145. mtx1_wdt_start();
  146. else if (value & WDIOS_DISABLECARD)
  147. mtx1_wdt_stop();
  148. else
  149. return -EINVAL;
  150. return 0;
  151. case WDIOC_KEEPALIVE:
  152. mtx1_wdt_reset();
  153. break;
  154. default:
  155. return -ENOTTY;
  156. }
  157. return 0;
  158. }
  159. static ssize_t mtx1_wdt_write(struct file *file, const char *buf,
  160. size_t count, loff_t *ppos)
  161. {
  162. if (!count)
  163. return -EIO;
  164. mtx1_wdt_reset();
  165. return count;
  166. }
  167. static const struct file_operations mtx1_wdt_fops = {
  168. .owner = THIS_MODULE,
  169. .llseek = no_llseek,
  170. .unlocked_ioctl = mtx1_wdt_ioctl,
  171. .open = mtx1_wdt_open,
  172. .write = mtx1_wdt_write,
  173. .release = mtx1_wdt_release,
  174. };
  175. static struct miscdevice mtx1_wdt_misc = {
  176. .minor = WATCHDOG_MINOR,
  177. .name = "watchdog",
  178. .fops = &mtx1_wdt_fops,
  179. };
  180. static int __devinit mtx1_wdt_probe(struct platform_device *pdev)
  181. {
  182. int ret;
  183. mtx1_wdt_device.gpio = pdev->resource[0].start;
  184. ret = gpio_request_one(mtx1_wdt_device.gpio,
  185. GPIOF_OUT_INIT_HIGH, "mtx1-wdt");
  186. if (ret < 0) {
  187. dev_err(&pdev->dev, "failed to request gpio");
  188. return ret;
  189. }
  190. spin_lock_init(&mtx1_wdt_device.lock);
  191. init_completion(&mtx1_wdt_device.stop);
  192. mtx1_wdt_device.queue = 0;
  193. clear_bit(0, &mtx1_wdt_device.inuse);
  194. setup_timer(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0L);
  195. mtx1_wdt_device.default_ticks = ticks;
  196. ret = misc_register(&mtx1_wdt_misc);
  197. if (ret < 0) {
  198. printk(KERN_ERR " mtx-1_wdt : failed to register\n");
  199. return ret;
  200. }
  201. mtx1_wdt_start();
  202. printk(KERN_INFO "MTX-1 Watchdog driver\n");
  203. return 0;
  204. }
  205. static int __devexit mtx1_wdt_remove(struct platform_device *pdev)
  206. {
  207. /* FIXME: do we need to lock this test ? */
  208. if (mtx1_wdt_device.queue) {
  209. mtx1_wdt_device.queue = 0;
  210. wait_for_completion(&mtx1_wdt_device.stop);
  211. }
  212. gpio_free(mtx1_wdt_device.gpio);
  213. misc_deregister(&mtx1_wdt_misc);
  214. return 0;
  215. }
  216. static struct platform_driver mtx1_wdt_driver = {
  217. .probe = mtx1_wdt_probe,
  218. .remove = __devexit_p(mtx1_wdt_remove),
  219. .driver.name = "mtx1-wdt",
  220. .driver.owner = THIS_MODULE,
  221. };
  222. static int __init mtx1_wdt_init(void)
  223. {
  224. return platform_driver_register(&mtx1_wdt_driver);
  225. }
  226. static void __exit mtx1_wdt_exit(void)
  227. {
  228. platform_driver_unregister(&mtx1_wdt_driver);
  229. }
  230. module_init(mtx1_wdt_init);
  231. module_exit(mtx1_wdt_exit);
  232. MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
  233. MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
  234. MODULE_LICENSE("GPL");
  235. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  236. MODULE_ALIAS("platform:mtx1-wdt");