mtx-1_wdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for the MTX-1 Watchdog.
  4. *
  5. * (C) Copyright 2005 4G Systems <info@4g-systems.biz>,
  6. * All Rights Reserved.
  7. * http://www.4g-systems.biz
  8. *
  9. * (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org>
  10. * (c) Copyright 2005 4G Systems <info@4g-systems.biz>
  11. *
  12. * Release 0.01.
  13. * Author: Michael Stickel michael.stickel@4g-systems.biz
  14. *
  15. * Release 0.02.
  16. * Author: Florian Fainelli florian@openwrt.org
  17. * use the Linux watchdog/timer APIs
  18. *
  19. * The Watchdog is configured to reset the MTX-1
  20. * if it is not triggered for 100 seconds.
  21. * It should not be triggered more often than 1.6 seconds.
  22. *
  23. * A timer triggers the watchdog every 5 seconds, until
  24. * it is opened for the first time. After the first open
  25. * it MUST be triggered every 2..95 seconds.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/types.h>
  30. #include <linux/errno.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/fs.h>
  33. #include <linux/ioport.h>
  34. #include <linux/timer.h>
  35. #include <linux/completion.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/watchdog.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/io.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/gpio/consumer.h>
  42. #include <asm/mach-au1x00/au1000.h>
  43. #define MTX1_WDT_INTERVAL (5 * HZ)
  44. static int ticks = 100 * HZ;
  45. static struct {
  46. struct completion stop;
  47. spinlock_t lock;
  48. int running;
  49. struct timer_list timer;
  50. int queue;
  51. int default_ticks;
  52. unsigned long inuse;
  53. struct gpio_desc *gpiod;
  54. unsigned int gstate;
  55. } mtx1_wdt_device;
  56. static void mtx1_wdt_trigger(struct timer_list *unused)
  57. {
  58. spin_lock(&mtx1_wdt_device.lock);
  59. if (mtx1_wdt_device.running)
  60. ticks--;
  61. /* toggle wdt gpio */
  62. mtx1_wdt_device.gstate = !mtx1_wdt_device.gstate;
  63. gpiod_set_value(mtx1_wdt_device.gpiod, mtx1_wdt_device.gstate);
  64. if (mtx1_wdt_device.queue && ticks)
  65. mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
  66. else
  67. complete(&mtx1_wdt_device.stop);
  68. spin_unlock(&mtx1_wdt_device.lock);
  69. }
  70. static void mtx1_wdt_reset(void)
  71. {
  72. ticks = mtx1_wdt_device.default_ticks;
  73. }
  74. static void mtx1_wdt_start(void)
  75. {
  76. unsigned long flags;
  77. spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
  78. if (!mtx1_wdt_device.queue) {
  79. mtx1_wdt_device.queue = 1;
  80. mtx1_wdt_device.gstate = 1;
  81. gpiod_set_value(mtx1_wdt_device.gpiod, 1);
  82. mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
  83. }
  84. mtx1_wdt_device.running++;
  85. spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
  86. }
  87. static int mtx1_wdt_stop(void)
  88. {
  89. unsigned long flags;
  90. spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
  91. if (mtx1_wdt_device.queue) {
  92. mtx1_wdt_device.queue = 0;
  93. mtx1_wdt_device.gstate = 0;
  94. gpiod_set_value(mtx1_wdt_device.gpiod, 0);
  95. }
  96. ticks = mtx1_wdt_device.default_ticks;
  97. spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
  98. return 0;
  99. }
  100. /* Filesystem functions */
  101. static int mtx1_wdt_open(struct inode *inode, struct file *file)
  102. {
  103. if (test_and_set_bit(0, &mtx1_wdt_device.inuse))
  104. return -EBUSY;
  105. return stream_open(inode, file);
  106. }
  107. static int mtx1_wdt_release(struct inode *inode, struct file *file)
  108. {
  109. clear_bit(0, &mtx1_wdt_device.inuse);
  110. return 0;
  111. }
  112. static long mtx1_wdt_ioctl(struct file *file, unsigned int cmd,
  113. unsigned long arg)
  114. {
  115. void __user *argp = (void __user *)arg;
  116. int __user *p = (int __user *)argp;
  117. unsigned int value;
  118. static const struct watchdog_info ident = {
  119. .options = WDIOF_CARDRESET,
  120. .identity = "MTX-1 WDT",
  121. };
  122. switch (cmd) {
  123. case WDIOC_GETSUPPORT:
  124. if (copy_to_user(argp, &ident, sizeof(ident)))
  125. return -EFAULT;
  126. break;
  127. case WDIOC_GETSTATUS:
  128. case WDIOC_GETBOOTSTATUS:
  129. put_user(0, p);
  130. break;
  131. case WDIOC_SETOPTIONS:
  132. if (get_user(value, p))
  133. return -EFAULT;
  134. if (value & WDIOS_ENABLECARD)
  135. mtx1_wdt_start();
  136. else if (value & WDIOS_DISABLECARD)
  137. mtx1_wdt_stop();
  138. else
  139. return -EINVAL;
  140. return 0;
  141. case WDIOC_KEEPALIVE:
  142. mtx1_wdt_reset();
  143. break;
  144. default:
  145. return -ENOTTY;
  146. }
  147. return 0;
  148. }
  149. static ssize_t mtx1_wdt_write(struct file *file, const char *buf,
  150. size_t count, loff_t *ppos)
  151. {
  152. if (!count)
  153. return -EIO;
  154. mtx1_wdt_reset();
  155. return count;
  156. }
  157. static const struct file_operations mtx1_wdt_fops = {
  158. .owner = THIS_MODULE,
  159. .llseek = no_llseek,
  160. .unlocked_ioctl = mtx1_wdt_ioctl,
  161. .compat_ioctl = compat_ptr_ioctl,
  162. .open = mtx1_wdt_open,
  163. .write = mtx1_wdt_write,
  164. .release = mtx1_wdt_release,
  165. };
  166. static struct miscdevice mtx1_wdt_misc = {
  167. .minor = WATCHDOG_MINOR,
  168. .name = "watchdog",
  169. .fops = &mtx1_wdt_fops,
  170. };
  171. static int mtx1_wdt_probe(struct platform_device *pdev)
  172. {
  173. int ret;
  174. mtx1_wdt_device.gpiod = devm_gpiod_get(&pdev->dev,
  175. NULL, GPIOD_OUT_HIGH);
  176. if (IS_ERR(mtx1_wdt_device.gpiod)) {
  177. dev_err(&pdev->dev, "failed to request gpio");
  178. return PTR_ERR(mtx1_wdt_device.gpiod);
  179. }
  180. spin_lock_init(&mtx1_wdt_device.lock);
  181. init_completion(&mtx1_wdt_device.stop);
  182. mtx1_wdt_device.queue = 0;
  183. clear_bit(0, &mtx1_wdt_device.inuse);
  184. timer_setup(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0);
  185. mtx1_wdt_device.default_ticks = ticks;
  186. ret = misc_register(&mtx1_wdt_misc);
  187. if (ret < 0) {
  188. dev_err(&pdev->dev, "failed to register\n");
  189. return ret;
  190. }
  191. mtx1_wdt_start();
  192. dev_info(&pdev->dev, "MTX-1 Watchdog driver\n");
  193. return 0;
  194. }
  195. static int mtx1_wdt_remove(struct platform_device *pdev)
  196. {
  197. /* FIXME: do we need to lock this test ? */
  198. if (mtx1_wdt_device.queue) {
  199. mtx1_wdt_device.queue = 0;
  200. wait_for_completion(&mtx1_wdt_device.stop);
  201. }
  202. misc_deregister(&mtx1_wdt_misc);
  203. return 0;
  204. }
  205. static struct platform_driver mtx1_wdt_driver = {
  206. .probe = mtx1_wdt_probe,
  207. .remove = mtx1_wdt_remove,
  208. .driver.name = "mtx1-wdt",
  209. .driver.owner = THIS_MODULE,
  210. };
  211. module_platform_driver(mtx1_wdt_driver);
  212. MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
  213. MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
  214. MODULE_LICENSE("GPL");
  215. MODULE_ALIAS("platform:mtx1-wdt");