ep93xx_wdt.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <asm/hardware.h>
  31. #include <asm/uaccess.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. /* Can't seek (pwrite) on this device */
  90. if (*ppos != file->f_pos)
  91. return -ESPIPE;
  92. if (len) {
  93. if (!nowayout) {
  94. size_t i;
  95. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  96. for (i = 0; i != len; i++) {
  97. char c;
  98. if (get_user(c, data + i))
  99. return -EFAULT;
  100. if (c == 'V')
  101. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  102. else
  103. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  104. }
  105. }
  106. wdt_keepalive();
  107. }
  108. return len;
  109. }
  110. static struct watchdog_info ident = {
  111. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE,
  112. .identity = "EP93xx Watchdog",
  113. };
  114. static int
  115. ep93xx_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  116. unsigned long arg)
  117. {
  118. int ret = -ENOTTY;
  119. switch (cmd) {
  120. case WDIOC_GETSUPPORT:
  121. ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
  122. sizeof(ident)) ? -EFAULT : 0;
  123. break;
  124. case WDIOC_GETSTATUS:
  125. ret = put_user(0, (int __user *)arg);
  126. break;
  127. case WDIOC_GETBOOTSTATUS:
  128. ret = put_user(boot_status, (int __user *)arg);
  129. break;
  130. case WDIOC_GETTIMEOUT:
  131. /* actually, it is 0.250 seconds.... */
  132. ret = put_user(1, (int __user *)arg);
  133. break;
  134. case WDIOC_KEEPALIVE:
  135. wdt_keepalive();
  136. ret = 0;
  137. break;
  138. }
  139. return ret;
  140. }
  141. static int ep93xx_wdt_release(struct inode *inode, struct file *file)
  142. {
  143. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  144. wdt_shutdown();
  145. else
  146. printk(KERN_CRIT PFX "Device closed unexpectedly - "
  147. "timer will not stop\n");
  148. clear_bit(WDT_IN_USE, &wdt_status);
  149. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  150. return 0;
  151. }
  152. static const struct file_operations ep93xx_wdt_fops = {
  153. .owner = THIS_MODULE,
  154. .write = ep93xx_wdt_write,
  155. .ioctl = ep93xx_wdt_ioctl,
  156. .open = ep93xx_wdt_open,
  157. .release = ep93xx_wdt_release,
  158. };
  159. static struct miscdevice ep93xx_wdt_miscdev = {
  160. .minor = WATCHDOG_MINOR,
  161. .name = "watchdog",
  162. .fops = &ep93xx_wdt_fops,
  163. };
  164. static void ep93xx_timer_ping(unsigned long data)
  165. {
  166. if (time_before(jiffies, next_heartbeat))
  167. wdt_ping();
  168. /* Re-set the timer interval */
  169. mod_timer(&timer, jiffies + WDT_INTERVAL);
  170. }
  171. static int __init ep93xx_wdt_init(void)
  172. {
  173. int err;
  174. err = misc_register(&ep93xx_wdt_miscdev);
  175. boot_status = __raw_readl(EP93XX_WDT_WATCHDOG) & 0x01 ? 1 : 0;
  176. printk(KERN_INFO PFX "EP93XX watchdog, driver version "
  177. WDT_VERSION "%s\n",
  178. (__raw_readl(EP93XX_WDT_WATCHDOG) & 0x08)
  179. ? " (nCS1 disable detected)" : "");
  180. if (timeout < 1 || timeout > 3600) {
  181. timeout = WDT_TIMEOUT;
  182. printk(KERN_INFO PFX
  183. "timeout value must be 1<=x<=3600, using %d\n",
  184. timeout);
  185. }
  186. setup_timer(&timer, ep93xx_timer_ping, 1);
  187. return err;
  188. }
  189. static void __exit ep93xx_wdt_exit(void)
  190. {
  191. wdt_shutdown();
  192. misc_deregister(&ep93xx_wdt_miscdev);
  193. }
  194. module_init(ep93xx_wdt_init);
  195. module_exit(ep93xx_wdt_exit);
  196. module_param(nowayout, int, 0);
  197. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  198. module_param(timeout, int, 0);
  199. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  200. MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>,"
  201. "Alessandro Zummo <a.zummo@towertech.it>");
  202. MODULE_DESCRIPTION("EP93xx Watchdog");
  203. MODULE_LICENSE("GPL");
  204. MODULE_VERSION(WDT_VERSION);
  205. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);