rdc321x_wdt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. static int ticks = 1000;
  49. /* some device data */
  50. static struct {
  51. struct completion stop;
  52. int running;
  53. struct timer_list timer;
  54. int queue;
  55. int default_ticks;
  56. unsigned long inuse;
  57. spinlock_t lock;
  58. } rdc321x_wdt_device;
  59. /* generic helper functions */
  60. static void rdc321x_wdt_trigger(unsigned long unused)
  61. {
  62. unsigned long flags;
  63. if (rdc321x_wdt_device.running)
  64. ticks--;
  65. /* keep watchdog alive */
  66. spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
  67. outl(RDC_WDT_EN | inl(RDC3210_CFGREG_DATA),
  68. RDC3210_CFGREG_DATA);
  69. spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
  70. /* requeue?? */
  71. if (rdc321x_wdt_device.queue && ticks)
  72. mod_timer(&rdc321x_wdt_device.timer,
  73. jiffies + RDC_WDT_INTERVAL);
  74. else {
  75. /* ticks doesn't matter anyway */
  76. complete(&rdc321x_wdt_device.stop);
  77. }
  78. }
  79. static void rdc321x_wdt_reset(void)
  80. {
  81. ticks = rdc321x_wdt_device.default_ticks;
  82. }
  83. static void rdc321x_wdt_start(void)
  84. {
  85. unsigned long flags;
  86. if (!rdc321x_wdt_device.queue) {
  87. rdc321x_wdt_device.queue = 1;
  88. /* Clear the timer */
  89. spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
  90. outl(RDC_CLS_TMR, RDC3210_CFGREG_ADDR);
  91. /* Enable watchdog and set the timeout to 81.92 us */
  92. outl(RDC_WDT_EN | RDC_WDT_CNT, RDC3210_CFGREG_DATA);
  93. spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
  94. mod_timer(&rdc321x_wdt_device.timer,
  95. jiffies + RDC_WDT_INTERVAL);
  96. }
  97. /* if process dies, counter is not decremented */
  98. rdc321x_wdt_device.running++;
  99. }
  100. static int rdc321x_wdt_stop(void)
  101. {
  102. if (rdc321x_wdt_device.running)
  103. rdc321x_wdt_device.running = 0;
  104. ticks = rdc321x_wdt_device.default_ticks;
  105. return -EIO;
  106. }
  107. /* filesystem operations */
  108. static int rdc321x_wdt_open(struct inode *inode, struct file *file)
  109. {
  110. if (test_and_set_bit(0, &rdc321x_wdt_device.inuse))
  111. return -EBUSY;
  112. return nonseekable_open(inode, file);
  113. }
  114. static int rdc321x_wdt_release(struct inode *inode, struct file *file)
  115. {
  116. clear_bit(0, &rdc321x_wdt_device.inuse);
  117. return 0;
  118. }
  119. static long rdc321x_wdt_ioctl(struct file *file, unsigned int cmd,
  120. unsigned long arg)
  121. {
  122. void __user *argp = (void __user *)arg;
  123. unsigned int value;
  124. static struct watchdog_info ident = {
  125. .options = WDIOF_CARDRESET,
  126. .identity = "RDC321x WDT",
  127. };
  128. unsigned long flags;
  129. switch (cmd) {
  130. case WDIOC_KEEPALIVE:
  131. rdc321x_wdt_reset();
  132. break;
  133. case WDIOC_GETSTATUS:
  134. /* Read the value from the DATA register */
  135. spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
  136. value = inl(RDC3210_CFGREG_DATA);
  137. spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
  138. if (copy_to_user(argp, &value, sizeof(int)))
  139. return -EFAULT;
  140. break;
  141. case WDIOC_GETSUPPORT:
  142. if (copy_to_user(argp, &ident, sizeof(ident)))
  143. return -EFAULT;
  144. break;
  145. case WDIOC_SETOPTIONS:
  146. if (copy_from_user(&value, argp, sizeof(int)))
  147. return -EFAULT;
  148. switch (value) {
  149. case WDIOS_ENABLECARD:
  150. rdc321x_wdt_start();
  151. break;
  152. case WDIOS_DISABLECARD:
  153. return rdc321x_wdt_stop();
  154. default:
  155. return -EINVAL;
  156. }
  157. break;
  158. default:
  159. return -ENOTTY;
  160. }
  161. return 0;
  162. }
  163. static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf,
  164. size_t count, loff_t *ppos)
  165. {
  166. if (!count)
  167. return -EIO;
  168. rdc321x_wdt_reset();
  169. return count;
  170. }
  171. static const struct file_operations rdc321x_wdt_fops = {
  172. .owner = THIS_MODULE,
  173. .llseek = no_llseek,
  174. .unlocked_ioctl = rdc321x_wdt_ioctl,
  175. .open = rdc321x_wdt_open,
  176. .write = rdc321x_wdt_write,
  177. .release = rdc321x_wdt_release,
  178. };
  179. static struct miscdevice rdc321x_wdt_misc = {
  180. .minor = WATCHDOG_MINOR,
  181. .name = "watchdog",
  182. .fops = &rdc321x_wdt_fops,
  183. };
  184. static int __devinit rdc321x_wdt_probe(struct platform_device *pdev)
  185. {
  186. int err;
  187. err = misc_register(&rdc321x_wdt_misc);
  188. if (err < 0) {
  189. printk(KERN_ERR PFX "watchdog misc_register failed\n");
  190. return err;
  191. }
  192. spin_lock_init(&rdc321x_wdt_device.lock);
  193. /* Reset the watchdog */
  194. outl(RDC_WDT_RST, RDC3210_CFGREG_DATA);
  195. init_completion(&rdc321x_wdt_device.stop);
  196. rdc321x_wdt_device.queue = 0;
  197. clear_bit(0, &rdc321x_wdt_device.inuse);
  198. setup_timer(&rdc321x_wdt_device.timer, rdc321x_wdt_trigger, 0);
  199. rdc321x_wdt_device.default_ticks = ticks;
  200. printk(KERN_INFO PFX "watchdog init success\n");
  201. return 0;
  202. }
  203. static int rdc321x_wdt_remove(struct platform_device *pdev)
  204. {
  205. if (rdc321x_wdt_device.queue) {
  206. rdc321x_wdt_device.queue = 0;
  207. wait_for_completion(&rdc321x_wdt_device.stop);
  208. }
  209. misc_deregister(&rdc321x_wdt_misc);
  210. return 0;
  211. }
  212. static struct platform_driver rdc321x_wdt_driver = {
  213. .probe = rdc321x_wdt_probe,
  214. .remove = rdc321x_wdt_remove,
  215. .driver = {
  216. .owner = THIS_MODULE,
  217. .name = "rdc321x-wdt",
  218. },
  219. };
  220. static int __init rdc321x_wdt_init(void)
  221. {
  222. return platform_driver_register(&rdc321x_wdt_driver);
  223. }
  224. static void __exit rdc321x_wdt_exit(void)
  225. {
  226. platform_driver_unregister(&rdc321x_wdt_driver);
  227. }
  228. module_init(rdc321x_wdt_init);
  229. module_exit(rdc321x_wdt_exit);
  230. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  231. MODULE_DESCRIPTION("RDC321x watchdog driver");
  232. MODULE_LICENSE("GPL");
  233. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);