mtx-1_wdt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. } mtx1_wdt_device;
  65. static void mtx1_wdt_trigger(unsigned long unused)
  66. {
  67. u32 tmp;
  68. spin_lock(&mtx1_wdt_device.lock);
  69. if (mtx1_wdt_device.running)
  70. ticks--;
  71. /*
  72. * toggle GPIO2_15
  73. */
  74. tmp = au_readl(GPIO2_DIR);
  75. tmp = (tmp & ~(1 << mtx1_wdt_device.gpio)) |
  76. ((~tmp) & (1 << mtx1_wdt_device.gpio));
  77. au_writel(tmp, GPIO2_DIR);
  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. spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
  91. if (!mtx1_wdt_device.queue) {
  92. mtx1_wdt_device.queue = 1;
  93. gpio_set_value(mtx1_wdt_device.gpio, 1);
  94. mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
  95. }
  96. mtx1_wdt_device.running++;
  97. spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
  98. }
  99. static int mtx1_wdt_stop(void)
  100. {
  101. spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
  102. if (mtx1_wdt_device.queue) {
  103. mtx1_wdt_device.queue = 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 mtx1_wdt_probe(struct platform_device *pdev)
  181. {
  182. int ret;
  183. mtx1_wdt_device.gpio = pdev->resource[0].start;
  184. spin_lock_init(&mtx1_wdt_device.lock);
  185. init_completion(&mtx1_wdt_device.stop);
  186. mtx1_wdt_device.queue = 0;
  187. clear_bit(0, &mtx1_wdt_device.inuse);
  188. setup_timer(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0L);
  189. mtx1_wdt_device.default_ticks = ticks;
  190. ret = misc_register(&mtx1_wdt_misc);
  191. if (ret < 0) {
  192. printk(KERN_ERR " mtx-1_wdt : failed to register\n");
  193. return ret;
  194. }
  195. mtx1_wdt_start();
  196. printk(KERN_INFO "MTX-1 Watchdog driver\n");
  197. return 0;
  198. }
  199. static int mtx1_wdt_remove(struct platform_device *pdev)
  200. {
  201. /* FIXME: do we need to lock this test ? */
  202. if (mtx1_wdt_device.queue) {
  203. mtx1_wdt_device.queue = 0;
  204. wait_for_completion(&mtx1_wdt_device.stop);
  205. }
  206. misc_deregister(&mtx1_wdt_misc);
  207. return 0;
  208. }
  209. static struct platform_driver mtx1_wdt = {
  210. .probe = mtx1_wdt_probe,
  211. .remove = mtx1_wdt_remove,
  212. .driver.name = "mtx1-wdt",
  213. .driver.owner = THIS_MODULE,
  214. };
  215. static int __init mtx1_wdt_init(void)
  216. {
  217. return platform_driver_register(&mtx1_wdt);
  218. }
  219. static void __exit mtx1_wdt_exit(void)
  220. {
  221. platform_driver_unregister(&mtx1_wdt);
  222. }
  223. module_init(mtx1_wdt_init);
  224. module_exit(mtx1_wdt_exit);
  225. MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
  226. MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
  227. MODULE_LICENSE("GPL");
  228. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  229. MODULE_ALIAS("platform:mtx1-wdt");