mtx-1_wdt.c 6.2 KB

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