ep93xx_wdt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <mach/hardware.h>
  32. #define WDT_VERSION "0.3"
  33. #define PFX "ep93xx_wdt: "
  34. /* default timeout (secs) */
  35. #define WDT_TIMEOUT 30
  36. static int nowayout = WATCHDOG_NOWAYOUT;
  37. static int timeout = WDT_TIMEOUT;
  38. static struct timer_list timer;
  39. static unsigned long next_heartbeat;
  40. static unsigned long wdt_status;
  41. static unsigned long boot_status;
  42. #define WDT_IN_USE 0
  43. #define WDT_OK_TO_CLOSE 1
  44. #define EP93XX_WDT_REG(x) (EP93XX_WATCHDOG_BASE + (x))
  45. #define EP93XX_WDT_WATCHDOG EP93XX_WDT_REG(0x00)
  46. #define EP93XX_WDT_WDSTATUS EP93XX_WDT_REG(0x04)
  47. /* reset the wdt every ~200ms */
  48. #define WDT_INTERVAL (HZ/5)
  49. static void wdt_enable(void)
  50. {
  51. __raw_writew(0xaaaa, EP93XX_WDT_WATCHDOG);
  52. }
  53. static void wdt_disable(void)
  54. {
  55. __raw_writew(0xaa55, EP93XX_WDT_WATCHDOG);
  56. }
  57. static inline void wdt_ping(void)
  58. {
  59. __raw_writew(0x5555, EP93XX_WDT_WATCHDOG);
  60. }
  61. static void wdt_startup(void)
  62. {
  63. next_heartbeat = jiffies + (timeout * HZ);
  64. wdt_enable();
  65. mod_timer(&timer, jiffies + WDT_INTERVAL);
  66. }
  67. static void wdt_shutdown(void)
  68. {
  69. del_timer_sync(&timer);
  70. wdt_disable();
  71. }
  72. static void wdt_keepalive(void)
  73. {
  74. /* user land ping */
  75. next_heartbeat = jiffies + (timeout * HZ);
  76. }
  77. static int ep93xx_wdt_open(struct inode *inode, struct file *file)
  78. {
  79. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  80. return -EBUSY;
  81. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  82. wdt_startup();
  83. return nonseekable_open(inode, file);
  84. }
  85. static ssize_t
  86. ep93xx_wdt_write(struct file *file, const char __user *data, size_t len,
  87. loff_t *ppos)
  88. {
  89. if (len) {
  90. if (!nowayout) {
  91. size_t i;
  92. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  93. for (i = 0; i != len; i++) {
  94. char c;
  95. if (get_user(c, data + i))
  96. return -EFAULT;
  97. if (c == 'V')
  98. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  99. else
  100. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  101. }
  102. }
  103. wdt_keepalive();
  104. }
  105. return len;
  106. }
  107. static struct watchdog_info ident = {
  108. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE,
  109. .identity = "EP93xx Watchdog",
  110. };
  111. static long ep93xx_wdt_ioctl(struct file *file,
  112. unsigned int cmd, unsigned long arg)
  113. {
  114. int ret = -ENOTTY;
  115. switch (cmd) {
  116. case WDIOC_GETSUPPORT:
  117. ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
  118. sizeof(ident)) ? -EFAULT : 0;
  119. break;
  120. case WDIOC_GETSTATUS:
  121. ret = put_user(0, (int __user *)arg);
  122. break;
  123. case WDIOC_GETBOOTSTATUS:
  124. ret = put_user(boot_status, (int __user *)arg);
  125. break;
  126. case WDIOC_KEEPALIVE:
  127. wdt_keepalive();
  128. ret = 0;
  129. break;
  130. case WDIOC_GETTIMEOUT:
  131. /* actually, it is 0.250 seconds.... */
  132. ret = put_user(1, (int __user *)arg);
  133. break;
  134. }
  135. return ret;
  136. }
  137. static int ep93xx_wdt_release(struct inode *inode, struct file *file)
  138. {
  139. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  140. wdt_shutdown();
  141. else
  142. printk(KERN_CRIT PFX
  143. "Device closed unexpectedly - timer will not stop\n");
  144. clear_bit(WDT_IN_USE, &wdt_status);
  145. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  146. return 0;
  147. }
  148. static const struct file_operations ep93xx_wdt_fops = {
  149. .owner = THIS_MODULE,
  150. .write = ep93xx_wdt_write,
  151. .unlocked_ioctl = ep93xx_wdt_ioctl,
  152. .open = ep93xx_wdt_open,
  153. .release = ep93xx_wdt_release,
  154. };
  155. static struct miscdevice ep93xx_wdt_miscdev = {
  156. .minor = WATCHDOG_MINOR,
  157. .name = "watchdog",
  158. .fops = &ep93xx_wdt_fops,
  159. };
  160. static void ep93xx_timer_ping(unsigned long data)
  161. {
  162. if (time_before(jiffies, next_heartbeat))
  163. wdt_ping();
  164. /* Re-set the timer interval */
  165. mod_timer(&timer, jiffies + WDT_INTERVAL);
  166. }
  167. static int __init ep93xx_wdt_init(void)
  168. {
  169. int err;
  170. err = misc_register(&ep93xx_wdt_miscdev);
  171. boot_status = __raw_readl(EP93XX_WDT_WATCHDOG) & 0x01 ? 1 : 0;
  172. printk(KERN_INFO PFX "EP93XX watchdog, driver version "
  173. WDT_VERSION "%s\n",
  174. (__raw_readl(EP93XX_WDT_WATCHDOG) & 0x08)
  175. ? " (nCS1 disable detected)" : "");
  176. if (timeout < 1 || timeout > 3600) {
  177. timeout = WDT_TIMEOUT;
  178. printk(KERN_INFO PFX
  179. "timeout value must be 1<=x<=3600, using %d\n",
  180. timeout);
  181. }
  182. setup_timer(&timer, ep93xx_timer_ping, 1);
  183. return err;
  184. }
  185. static void __exit ep93xx_wdt_exit(void)
  186. {
  187. wdt_shutdown();
  188. misc_deregister(&ep93xx_wdt_miscdev);
  189. }
  190. module_init(ep93xx_wdt_init);
  191. module_exit(ep93xx_wdt_exit);
  192. module_param(nowayout, int, 0);
  193. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  194. module_param(timeout, int, 0);
  195. MODULE_PARM_DESC(timeout,
  196. "Watchdog timeout in seconds. (1<=timeout<=3600, default="
  197. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  198. MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>,"
  199. "Alessandro Zummo <a.zummo@towertech.it>");
  200. MODULE_DESCRIPTION("EP93xx Watchdog");
  201. MODULE_LICENSE("GPL");
  202. MODULE_VERSION(WDT_VERSION);
  203. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);