mtx-1_wdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Driver for the MTX-1 Watchdog.
  3. *
  4. * (C) Copyright 2005 4G Systems <info@4g-systems.biz>, All Rights Reserved.
  5. * http://www.4g-systems.biz
  6. *
  7. * (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Neither Michael Stickel nor 4G Systems admit liability nor provide
  15. * warranty for any of this software. This material is provided
  16. * "AS-IS" and at no charge.
  17. *
  18. * (c) Copyright 2005 4G Systems <info@4g-systems.biz>
  19. *
  20. * Release 0.01.
  21. * Author: Michael Stickel michael.stickel@4g-systems.biz
  22. *
  23. * Release 0.02.
  24. * Author: Florian Fainelli florian@openwrt.org
  25. * use the Linux watchdog/timer APIs
  26. *
  27. * The Watchdog is configured to reset the MTX-1
  28. * if it is not triggered for 100 seconds.
  29. * It should not be triggered more often than 1.6 seconds.
  30. *
  31. * A timer triggers the watchdog every 5 seconds, until
  32. * it is opened for the first time. After the first open
  33. * it MUST be triggered every 2..95 seconds.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/types.h>
  38. #include <linux/errno.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/fs.h>
  41. #include <linux/init.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 <asm/io.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/mach-au1x00/au1000.h>
  51. #include <asm/gpio.h>
  52. #define MTX1_WDT_INTERVAL (5 * HZ)
  53. static int ticks = 100 * HZ;
  54. static struct {
  55. struct completion stop;
  56. int running;
  57. struct timer_list timer;
  58. int queue;
  59. int default_ticks;
  60. unsigned long inuse;
  61. unsigned gpio;
  62. } mtx1_wdt_device;
  63. static void mtx1_wdt_trigger(unsigned long unused)
  64. {
  65. u32 tmp;
  66. if (mtx1_wdt_device.running)
  67. ticks--;
  68. /*
  69. * toggle GPIO2_15
  70. */
  71. tmp = au_readl(GPIO2_DIR);
  72. tmp = (tmp & ~(1 << mtx1_wdt_device.gpio)) |
  73. ((~tmp) & (1 << mtx1_wdt_device.gpio));
  74. au_writel (tmp, GPIO2_DIR);
  75. if (mtx1_wdt_device.queue && ticks)
  76. mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
  77. else {
  78. complete(&mtx1_wdt_device.stop);
  79. }
  80. }
  81. static void mtx1_wdt_reset(void)
  82. {
  83. ticks = mtx1_wdt_device.default_ticks;
  84. }
  85. static void mtx1_wdt_start(void)
  86. {
  87. if (!mtx1_wdt_device.queue) {
  88. mtx1_wdt_device.queue = 1;
  89. gpio_set_value(mtx1_wdt_device.gpio, 1);
  90. mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
  91. }
  92. mtx1_wdt_device.running++;
  93. }
  94. static int mtx1_wdt_stop(void)
  95. {
  96. if (mtx1_wdt_device.queue) {
  97. mtx1_wdt_device.queue = 0;
  98. gpio_set_value(mtx1_wdt_device.gpio, 0);
  99. }
  100. ticks = mtx1_wdt_device.default_ticks;
  101. return 0;
  102. }
  103. /* Filesystem functions */
  104. static int mtx1_wdt_open(struct inode *inode, struct file *file)
  105. {
  106. if (test_and_set_bit(0, &mtx1_wdt_device.inuse))
  107. return -EBUSY;
  108. return nonseekable_open(inode, file);
  109. }
  110. static int mtx1_wdt_release(struct inode *inode, struct file *file)
  111. {
  112. clear_bit(0, &mtx1_wdt_device.inuse);
  113. return 0;
  114. }
  115. static int mtx1_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  116. {
  117. void __user *argp = (void __user *)arg;
  118. unsigned int value;
  119. static struct watchdog_info ident =
  120. {
  121. .options = WDIOF_CARDRESET,
  122. .identity = "MTX-1 WDT",
  123. };
  124. switch(cmd) {
  125. case WDIOC_KEEPALIVE:
  126. mtx1_wdt_reset();
  127. break;
  128. case WDIOC_GETSTATUS:
  129. case WDIOC_GETBOOTSTATUS:
  130. if ( copy_to_user(argp, &value, sizeof(int)) )
  131. return -EFAULT;
  132. break;
  133. case WDIOC_GETSUPPORT:
  134. if ( copy_to_user(argp, &ident, sizeof(ident)) )
  135. return -EFAULT;
  136. break;
  137. case WDIOC_SETOPTIONS:
  138. if ( copy_from_user(&value, argp, sizeof(int)) )
  139. return -EFAULT;
  140. switch(value) {
  141. case WDIOS_ENABLECARD:
  142. mtx1_wdt_start();
  143. break;
  144. case WDIOS_DISABLECARD:
  145. return mtx1_wdt_stop();
  146. default:
  147. return -EINVAL;
  148. }
  149. break;
  150. default:
  151. return -ENOTTY;
  152. }
  153. return 0;
  154. }
  155. static ssize_t mtx1_wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
  156. {
  157. if (!count)
  158. return -EIO;
  159. mtx1_wdt_reset();
  160. return count;
  161. }
  162. static const struct file_operations mtx1_wdt_fops = {
  163. .owner = THIS_MODULE,
  164. .llseek = no_llseek,
  165. .ioctl = mtx1_wdt_ioctl,
  166. .open = mtx1_wdt_open,
  167. .write = mtx1_wdt_write,
  168. .release = mtx1_wdt_release
  169. };
  170. static struct miscdevice mtx1_wdt_misc = {
  171. .minor = WATCHDOG_MINOR,
  172. .name = "watchdog",
  173. .fops = &mtx1_wdt_fops
  174. };
  175. static int mtx1_wdt_probe(struct platform_device *pdev)
  176. {
  177. int ret;
  178. mtx1_wdt_device.gpio = pdev->resource[0].start;
  179. if ((ret = misc_register(&mtx1_wdt_misc)) < 0) {
  180. printk(KERN_ERR " mtx-1_wdt : failed to register\n");
  181. return ret;
  182. }
  183. init_completion(&mtx1_wdt_device.stop);
  184. mtx1_wdt_device.queue = 0;
  185. clear_bit(0, &mtx1_wdt_device.inuse);
  186. setup_timer(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0L);
  187. mtx1_wdt_device.default_ticks = ticks;
  188. mtx1_wdt_start();
  189. printk(KERN_INFO "MTX-1 Watchdog driver\n");
  190. return 0;
  191. }
  192. static int mtx1_wdt_remove(struct platform_device *pdev)
  193. {
  194. if (mtx1_wdt_device.queue) {
  195. mtx1_wdt_device.queue = 0;
  196. wait_for_completion(&mtx1_wdt_device.stop);
  197. }
  198. misc_deregister(&mtx1_wdt_misc);
  199. return 0;
  200. }
  201. static struct platform_driver mtx1_wdt = {
  202. .probe = mtx1_wdt_probe,
  203. .remove = mtx1_wdt_remove,
  204. .driver.name = "mtx1-wdt",
  205. .driver.owner = THIS_MODULE,
  206. };
  207. static int __init mtx1_wdt_init(void)
  208. {
  209. return platform_driver_register(&mtx1_wdt);
  210. }
  211. static void __exit mtx1_wdt_exit(void)
  212. {
  213. platform_driver_unregister(&mtx1_wdt);
  214. }
  215. module_init(mtx1_wdt_init);
  216. module_exit(mtx1_wdt_exit);
  217. MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
  218. MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
  219. MODULE_LICENSE("GPL");
  220. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  221. MODULE_ALIAS("platform:mtx1-wdt");