mtx-1_wdt.c 6.3 KB

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