wdt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * RDC321x watchdog driver
  3. *
  4. * Copyright (C) 2007 Florian Fainelli <florian@openwrt.org>
  5. *
  6. * This driver is highly inspired from the cpu5_wdt driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/types.h>
  26. #include <linux/errno.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/fs.h>
  29. #include <linux/init.h>
  30. #include <linux/ioport.h>
  31. #include <linux/timer.h>
  32. #include <linux/completion.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/watchdog.h>
  36. #include <linux/io.h>
  37. #include <linux/uaccess.h>
  38. #include <asm/mach-rdc321x/rdc321x_defs.h>
  39. #define RDC_WDT_MASK 0x80000000 /* Mask */
  40. #define RDC_WDT_EN 0x00800000 /* Enable bit */
  41. #define RDC_WDT_WTI 0x00200000 /* Generate CPU reset/NMI/WDT on timeout */
  42. #define RDC_WDT_RST 0x00100000 /* Reset bit */
  43. #define RDC_WDT_WIF 0x00040000 /* WDT IRQ Flag */
  44. #define RDC_WDT_IRT 0x00000100 /* IRQ Routing table */
  45. #define RDC_WDT_CNT 0x00000001 /* WDT count */
  46. #define RDC_CLS_TMR 0x80003844 /* Clear timer */
  47. #define RDC_WDT_INTERVAL (HZ/10+1)
  48. int nowayout = WATCHDOG_NOWAYOUT;
  49. module_param(nowayout, int, 0);
  50. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  51. static int ticks = 1000;
  52. /* some device data */
  53. static struct {
  54. struct completion stop;
  55. volatile int running;
  56. struct timer_list timer;
  57. volatile int queue;
  58. int default_ticks;
  59. unsigned long inuse;
  60. } rdc321x_wdt_device;
  61. /* generic helper functions */
  62. static void rdc321x_wdt_trigger(unsigned long unused)
  63. {
  64. if (rdc321x_wdt_device.running)
  65. ticks--;
  66. /* keep watchdog alive */
  67. outl(RDC_WDT_EN|inl(RDC3210_CFGREG_DATA), RDC3210_CFGREG_DATA);
  68. /* requeue?? */
  69. if (rdc321x_wdt_device.queue && ticks)
  70. mod_timer(&rdc321x_wdt_device.timer,
  71. jiffies + RDC_WDT_INTERVAL);
  72. else {
  73. /* ticks doesn't matter anyway */
  74. complete(&rdc321x_wdt_device.stop);
  75. }
  76. }
  77. static void rdc321x_wdt_reset(void)
  78. {
  79. ticks = rdc321x_wdt_device.default_ticks;
  80. }
  81. static void rdc321x_wdt_start(void)
  82. {
  83. if (!rdc321x_wdt_device.queue) {
  84. rdc321x_wdt_device.queue = 1;
  85. /* Clear the timer */
  86. outl(RDC_CLS_TMR, RDC3210_CFGREG_ADDR);
  87. /* Enable watchdog and set the timeout to 81.92 us */
  88. outl(RDC_WDT_EN|RDC_WDT_CNT, RDC3210_CFGREG_DATA);
  89. mod_timer(&rdc321x_wdt_device.timer,
  90. jiffies + RDC_WDT_INTERVAL);
  91. }
  92. /* if process dies, counter is not decremented */
  93. rdc321x_wdt_device.running++;
  94. }
  95. static int rdc321x_wdt_stop(void)
  96. {
  97. if (rdc321x_wdt_device.running)
  98. rdc321x_wdt_device.running = 0;
  99. ticks = rdc321x_wdt_device.default_ticks;
  100. return -EIO;
  101. }
  102. /* filesystem operations */
  103. static int rdc321x_wdt_open(struct inode *inode, struct file *file)
  104. {
  105. if (test_and_set_bit(0, &rdc321x_wdt_device.inuse))
  106. return -EBUSY;
  107. return nonseekable_open(inode, file);
  108. }
  109. static int rdc321x_wdt_release(struct inode *inode, struct file *file)
  110. {
  111. clear_bit(0, &rdc321x_wdt_device.inuse);
  112. return 0;
  113. }
  114. static int rdc321x_wdt_ioctl(struct inode *inode, struct file *file,
  115. 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. .options = WDIOF_CARDRESET,
  121. .identity = "RDC321x WDT",
  122. };
  123. switch (cmd) {
  124. case WDIOC_KEEPALIVE:
  125. rdc321x_wdt_reset();
  126. break;
  127. case WDIOC_GETSTATUS:
  128. /* Read the value from the DATA register */
  129. value = inl(RDC3210_CFGREG_DATA);
  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. rdc321x_wdt_start();
  143. break;
  144. case WDIOS_DISABLECARD:
  145. return rdc321x_wdt_stop();
  146. default:
  147. return -EINVAL;
  148. }
  149. break;
  150. default:
  151. return -ENOTTY;
  152. }
  153. return 0;
  154. }
  155. static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf,
  156. size_t count, loff_t *ppos)
  157. {
  158. if (!count)
  159. return -EIO;
  160. rdc321x_wdt_reset();
  161. return count;
  162. }
  163. static const struct file_operations rdc321x_wdt_fops = {
  164. .owner = THIS_MODULE,
  165. .llseek = no_llseek,
  166. .ioctl = rdc321x_wdt_ioctl,
  167. .open = rdc321x_wdt_open,
  168. .write = rdc321x_wdt_write,
  169. .release = rdc321x_wdt_release,
  170. };
  171. static struct miscdevice rdc321x_wdt_misc = {
  172. .minor = WATCHDOG_MINOR,
  173. .name = "watchdog",
  174. .fops = &rdc321x_wdt_fops,
  175. };
  176. static int __devinit rdc321x_wdt_probe(struct platform_device *pdev)
  177. {
  178. int err;
  179. err = misc_register(&rdc321x_wdt_misc);
  180. if (err < 0) {
  181. printk(KERN_ERR PFX "watchdog misc_register failed\n");
  182. return err;
  183. }
  184. /* Reset the watchdog */
  185. outl(RDC_WDT_RST, RDC3210_CFGREG_DATA);
  186. init_completion(&rdc321x_wdt_device.stop);
  187. rdc321x_wdt_device.queue = 0;
  188. clear_bit(0, &rdc321x_wdt_device.inuse);
  189. setup_timer(&rdc321x_wdt_device.timer, rdc321x_wdt_trigger, 0);
  190. rdc321x_wdt_device.default_ticks = ticks;
  191. printk(KERN_INFO PFX "watchdog init success\n");
  192. return 0;
  193. }
  194. static int rdc321x_wdt_remove(struct platform_device *pdev)
  195. {
  196. if (rdc321x_wdt_device.queue) {
  197. rdc321x_wdt_device.queue = 0;
  198. wait_for_completion(&rdc321x_wdt_device.stop);
  199. }
  200. misc_deregister(&rdc321x_wdt_misc);
  201. return 0;
  202. }
  203. static struct platform_driver rdc321x_wdt_driver = {
  204. .probe = rdc321x_wdt_probe,
  205. .remove = rdc321x_wdt_remove,
  206. .driver = {
  207. .owner = THIS_MODULE,
  208. .name = "rdc321x-wdt",
  209. },
  210. };
  211. static int __init rdc321x_wdt_init(void)
  212. {
  213. return platform_driver_register(&rdc321x_wdt_driver);
  214. }
  215. static void __exit rdc321x_wdt_exit(void)
  216. {
  217. platform_driver_unregister(&rdc321x_wdt_driver);
  218. }
  219. module_init(rdc321x_wdt_init);
  220. module_exit(rdc321x_wdt_exit);
  221. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  222. MODULE_DESCRIPTION("RDC321x watchdog driver");
  223. MODULE_LICENSE("GPL");
  224. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);