ep93xx_wdt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Watchdog driver for Cirrus Logic EP93xx family of devices.
  3. *
  4. * Copyright (c) 2004 Ray Lehtiniemi
  5. * Copyright (c) 2006 Tower Technologies
  6. * Based on ep93xx driver, bits from alim7101_wdt.c
  7. *
  8. * Authors: Ray Lehtiniemi <rayl@mail.com>,
  9. * Alessandro Zummo <a.zummo@towertech.it>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. *
  15. * This watchdog fires after 250msec, which is a too short interval
  16. * for us to rely on the user space daemon alone. So we ping the
  17. * wdt each ~200msec and eventually stop doing it if the user space
  18. * daemon dies.
  19. *
  20. * TODO:
  21. *
  22. * - Test last reset from watchdog status
  23. * - Add a few missing ioctls
  24. */
  25. #include <linux/module.h>
  26. #include <linux/fs.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/watchdog.h>
  29. #include <linux/timer.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/io.h>
  32. #include <mach/hardware.h>
  33. #define WDT_VERSION "0.3"
  34. #define PFX "ep93xx_wdt: "
  35. /* default timeout (secs) */
  36. #define WDT_TIMEOUT 30
  37. static int nowayout = WATCHDOG_NOWAYOUT;
  38. static int timeout = WDT_TIMEOUT;
  39. static struct timer_list timer;
  40. static unsigned long next_heartbeat;
  41. static unsigned long wdt_status;
  42. static unsigned long boot_status;
  43. #define WDT_IN_USE 0
  44. #define WDT_OK_TO_CLOSE 1
  45. #define EP93XX_WDT_REG(x) (EP93XX_WATCHDOG_BASE + (x))
  46. #define EP93XX_WDT_WATCHDOG EP93XX_WDT_REG(0x00)
  47. #define EP93XX_WDT_WDSTATUS EP93XX_WDT_REG(0x04)
  48. /* reset the wdt every ~200ms */
  49. #define WDT_INTERVAL (HZ/5)
  50. static void wdt_enable(void)
  51. {
  52. __raw_writew(0xaaaa, EP93XX_WDT_WATCHDOG);
  53. }
  54. static void wdt_disable(void)
  55. {
  56. __raw_writew(0xaa55, EP93XX_WDT_WATCHDOG);
  57. }
  58. static inline void wdt_ping(void)
  59. {
  60. __raw_writew(0x5555, EP93XX_WDT_WATCHDOG);
  61. }
  62. static void wdt_startup(void)
  63. {
  64. next_heartbeat = jiffies + (timeout * HZ);
  65. wdt_enable();
  66. mod_timer(&timer, jiffies + WDT_INTERVAL);
  67. }
  68. static void wdt_shutdown(void)
  69. {
  70. del_timer_sync(&timer);
  71. wdt_disable();
  72. }
  73. static void wdt_keepalive(void)
  74. {
  75. /* user land ping */
  76. next_heartbeat = jiffies + (timeout * HZ);
  77. }
  78. static int ep93xx_wdt_open(struct inode *inode, struct file *file)
  79. {
  80. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  81. return -EBUSY;
  82. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  83. wdt_startup();
  84. return nonseekable_open(inode, file);
  85. }
  86. static ssize_t
  87. ep93xx_wdt_write(struct file *file, const char __user *data, size_t len,
  88. loff_t *ppos)
  89. {
  90. if (len) {
  91. if (!nowayout) {
  92. size_t i;
  93. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  94. for (i = 0; i != len; i++) {
  95. char c;
  96. if (get_user(c, data + i))
  97. return -EFAULT;
  98. if (c == 'V')
  99. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  100. else
  101. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  102. }
  103. }
  104. wdt_keepalive();
  105. }
  106. return len;
  107. }
  108. static struct watchdog_info ident = {
  109. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE,
  110. .identity = "EP93xx Watchdog",
  111. };
  112. static long ep93xx_wdt_ioctl(struct file *file,
  113. unsigned int cmd, unsigned long arg)
  114. {
  115. int ret = -ENOTTY;
  116. switch (cmd) {
  117. case WDIOC_GETSUPPORT:
  118. ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
  119. sizeof(ident)) ? -EFAULT : 0;
  120. break;
  121. case WDIOC_GETSTATUS:
  122. ret = put_user(0, (int __user *)arg);
  123. break;
  124. case WDIOC_GETBOOTSTATUS:
  125. ret = put_user(boot_status, (int __user *)arg);
  126. break;
  127. case WDIOC_KEEPALIVE:
  128. wdt_keepalive();
  129. ret = 0;
  130. break;
  131. case WDIOC_GETTIMEOUT:
  132. /* actually, it is 0.250 seconds.... */
  133. ret = put_user(1, (int __user *)arg);
  134. break;
  135. }
  136. return ret;
  137. }
  138. static int ep93xx_wdt_release(struct inode *inode, struct file *file)
  139. {
  140. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  141. wdt_shutdown();
  142. else
  143. printk(KERN_CRIT PFX
  144. "Device closed unexpectedly - timer will not stop\n");
  145. clear_bit(WDT_IN_USE, &wdt_status);
  146. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  147. return 0;
  148. }
  149. static const struct file_operations ep93xx_wdt_fops = {
  150. .owner = THIS_MODULE,
  151. .write = ep93xx_wdt_write,
  152. .unlocked_ioctl = ep93xx_wdt_ioctl,
  153. .open = ep93xx_wdt_open,
  154. .release = ep93xx_wdt_release,
  155. };
  156. static struct miscdevice ep93xx_wdt_miscdev = {
  157. .minor = WATCHDOG_MINOR,
  158. .name = "watchdog",
  159. .fops = &ep93xx_wdt_fops,
  160. };
  161. static void ep93xx_timer_ping(unsigned long data)
  162. {
  163. if (time_before(jiffies, next_heartbeat))
  164. wdt_ping();
  165. /* Re-set the timer interval */
  166. mod_timer(&timer, jiffies + WDT_INTERVAL);
  167. }
  168. static int __init ep93xx_wdt_init(void)
  169. {
  170. int err;
  171. err = misc_register(&ep93xx_wdt_miscdev);
  172. boot_status = __raw_readl(EP93XX_WDT_WATCHDOG) & 0x01 ? 1 : 0;
  173. printk(KERN_INFO PFX "EP93XX watchdog, driver version "
  174. WDT_VERSION "%s\n",
  175. (__raw_readl(EP93XX_WDT_WATCHDOG) & 0x08)
  176. ? " (nCS1 disable detected)" : "");
  177. if (timeout < 1 || timeout > 3600) {
  178. timeout = WDT_TIMEOUT;
  179. printk(KERN_INFO PFX
  180. "timeout value must be 1<=x<=3600, using %d\n",
  181. timeout);
  182. }
  183. setup_timer(&timer, ep93xx_timer_ping, 1);
  184. return err;
  185. }
  186. static void __exit ep93xx_wdt_exit(void)
  187. {
  188. wdt_shutdown();
  189. misc_deregister(&ep93xx_wdt_miscdev);
  190. }
  191. module_init(ep93xx_wdt_init);
  192. module_exit(ep93xx_wdt_exit);
  193. module_param(nowayout, int, 0);
  194. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  195. module_param(timeout, int, 0);
  196. MODULE_PARM_DESC(timeout,
  197. "Watchdog timeout in seconds. (1<=timeout<=3600, default="
  198. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  199. MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>,"
  200. "Alessandro Zummo <a.zummo@towertech.it>");
  201. MODULE_DESCRIPTION("EP93xx Watchdog");
  202. MODULE_LICENSE("GPL");
  203. MODULE_VERSION(WDT_VERSION);
  204. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);