ks8695_wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Watchdog driver for Kendin/Micrel KS8695.
  3. *
  4. * (C) 2007 Andrew Victor
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/errno.h>
  12. #include <linux/fs.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/types.h>
  20. #include <linux/watchdog.h>
  21. #include <asm/io.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/arch/regs-timer.h>
  24. #define WDT_DEFAULT_TIME 5 /* seconds */
  25. #define WDT_MAX_TIME 171 /* seconds */
  26. static int wdt_time = WDT_DEFAULT_TIME;
  27. static int nowayout = WATCHDOG_NOWAYOUT;
  28. module_param(wdt_time, int, 0);
  29. MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="__MODULE_STRING(WDT_DEFAULT_TIME) ")");
  30. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  31. module_param(nowayout, int, 0);
  32. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  33. #endif
  34. static unsigned long ks8695wdt_busy;
  35. /* ......................................................................... */
  36. /*
  37. * Disable the watchdog.
  38. */
  39. static void inline ks8695_wdt_stop(void)
  40. {
  41. unsigned long tmcon;
  42. /* disable timer0 */
  43. tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
  44. __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
  45. }
  46. /*
  47. * Enable and reset the watchdog.
  48. */
  49. static void inline ks8695_wdt_start(void)
  50. {
  51. unsigned long tmcon;
  52. unsigned long tval = wdt_time * CLOCK_TICK_RATE;
  53. /* disable timer0 */
  54. tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
  55. __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
  56. /* program timer0 */
  57. __raw_writel(tval | T0TC_WATCHDOG, KS8695_TMR_VA + KS8695_T0TC);
  58. /* re-enable timer0 */
  59. tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
  60. __raw_writel(tmcon | TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
  61. }
  62. /*
  63. * Reload the watchdog timer. (ie, pat the watchdog)
  64. */
  65. static void inline ks8695_wdt_reload(void)
  66. {
  67. unsigned long tmcon;
  68. /* disable, then re-enable timer0 */
  69. tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
  70. __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
  71. __raw_writel(tmcon | TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
  72. }
  73. /*
  74. * Change the watchdog time interval.
  75. */
  76. static int ks8695_wdt_settimeout(int new_time)
  77. {
  78. /*
  79. * All counting occurs at SLOW_CLOCK / 128 = 0.256 Hz
  80. *
  81. * Since WDV is a 16-bit counter, the maximum period is
  82. * 65536 / 0.256 = 256 seconds.
  83. */
  84. if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
  85. return -EINVAL;
  86. /* Set new watchdog time. It will be used when ks8695_wdt_start() is called. */
  87. wdt_time = new_time;
  88. return 0;
  89. }
  90. /* ......................................................................... */
  91. /*
  92. * Watchdog device is opened, and watchdog starts running.
  93. */
  94. static int ks8695_wdt_open(struct inode *inode, struct file *file)
  95. {
  96. if (test_and_set_bit(0, &ks8695wdt_busy))
  97. return -EBUSY;
  98. ks8695_wdt_start();
  99. return nonseekable_open(inode, file);
  100. }
  101. /*
  102. * Close the watchdog device.
  103. * If CONFIG_WATCHDOG_NOWAYOUT is NOT defined then the watchdog is also
  104. * disabled.
  105. */
  106. static int ks8695_wdt_close(struct inode *inode, struct file *file)
  107. {
  108. if (!nowayout)
  109. ks8695_wdt_stop(); /* Disable the watchdog when file is closed */
  110. clear_bit(0, &ks8695wdt_busy);
  111. return 0;
  112. }
  113. static struct watchdog_info ks8695_wdt_info = {
  114. .identity = "ks8695 watchdog",
  115. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  116. };
  117. /*
  118. * Handle commands from user-space.
  119. */
  120. static int ks8695_wdt_ioctl(struct inode *inode, struct file *file,
  121. unsigned int cmd, unsigned long arg)
  122. {
  123. void __user *argp = (void __user *)arg;
  124. int __user *p = argp;
  125. int new_value;
  126. switch(cmd) {
  127. case WDIOC_KEEPALIVE:
  128. ks8695_wdt_reload(); /* pat the watchdog */
  129. return 0;
  130. case WDIOC_GETSUPPORT:
  131. return copy_to_user(argp, &ks8695_wdt_info, sizeof(ks8695_wdt_info)) ? -EFAULT : 0;
  132. case WDIOC_SETTIMEOUT:
  133. if (get_user(new_value, p))
  134. return -EFAULT;
  135. if (ks8695_wdt_settimeout(new_value))
  136. return -EINVAL;
  137. /* Enable new time value */
  138. ks8695_wdt_start();
  139. /* Return current value */
  140. return put_user(wdt_time, p);
  141. case WDIOC_GETTIMEOUT:
  142. return put_user(wdt_time, p);
  143. case WDIOC_GETSTATUS:
  144. case WDIOC_GETBOOTSTATUS:
  145. return put_user(0, p);
  146. case WDIOC_SETOPTIONS:
  147. if (get_user(new_value, p))
  148. return -EFAULT;
  149. if (new_value & WDIOS_DISABLECARD)
  150. ks8695_wdt_stop();
  151. if (new_value & WDIOS_ENABLECARD)
  152. ks8695_wdt_start();
  153. return 0;
  154. default:
  155. return -ENOTTY;
  156. }
  157. }
  158. /*
  159. * Pat the watchdog whenever device is written to.
  160. */
  161. static ssize_t ks8695_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  162. {
  163. ks8695_wdt_reload(); /* pat the watchdog */
  164. return len;
  165. }
  166. /* ......................................................................... */
  167. static const struct file_operations ks8695wdt_fops = {
  168. .owner = THIS_MODULE,
  169. .llseek = no_llseek,
  170. .ioctl = ks8695_wdt_ioctl,
  171. .open = ks8695_wdt_open,
  172. .release = ks8695_wdt_close,
  173. .write = ks8695_wdt_write,
  174. };
  175. static struct miscdevice ks8695wdt_miscdev = {
  176. .minor = WATCHDOG_MINOR,
  177. .name = "watchdog",
  178. .fops = &ks8695wdt_fops,
  179. };
  180. static int __init ks8695wdt_probe(struct platform_device *pdev)
  181. {
  182. int res;
  183. if (ks8695wdt_miscdev.parent)
  184. return -EBUSY;
  185. ks8695wdt_miscdev.parent = &pdev->dev;
  186. res = misc_register(&ks8695wdt_miscdev);
  187. if (res)
  188. return res;
  189. printk("KS8695 Watchdog Timer enabled (%d seconds%s)\n", wdt_time, nowayout ? ", nowayout" : "");
  190. return 0;
  191. }
  192. static int __exit ks8695wdt_remove(struct platform_device *pdev)
  193. {
  194. int res;
  195. res = misc_deregister(&ks8695wdt_miscdev);
  196. if (!res)
  197. ks8695wdt_miscdev.parent = NULL;
  198. return res;
  199. }
  200. static void ks8695wdt_shutdown(struct platform_device *pdev)
  201. {
  202. ks8695_wdt_stop();
  203. }
  204. #ifdef CONFIG_PM
  205. static int ks8695wdt_suspend(struct platform_device *pdev, pm_message_t message)
  206. {
  207. ks8695_wdt_stop();
  208. return 0;
  209. }
  210. static int ks8695wdt_resume(struct platform_device *pdev)
  211. {
  212. if (ks8695wdt_busy)
  213. ks8695_wdt_start();
  214. return 0;
  215. }
  216. #else
  217. #define ks8695wdt_suspend NULL
  218. #define ks8695wdt_resume NULL
  219. #endif
  220. static struct platform_driver ks8695wdt_driver = {
  221. .probe = ks8695wdt_probe,
  222. .remove = __exit_p(ks8695wdt_remove),
  223. .shutdown = ks8695wdt_shutdown,
  224. .suspend = ks8695wdt_suspend,
  225. .resume = ks8695wdt_resume,
  226. .driver = {
  227. .name = "ks8695_wdt",
  228. .owner = THIS_MODULE,
  229. },
  230. };
  231. static int __init ks8695_wdt_init(void)
  232. {
  233. /* Check that the heartbeat value is within range; if not reset to the default */
  234. if (ks8695_wdt_settimeout(wdt_time)) {
  235. ks8695_wdt_settimeout(WDT_DEFAULT_TIME);
  236. pr_info("ks8695_wdt: wdt_time value must be 1 <= wdt_time <= %i, using %d\n", wdt_time, WDT_MAX_TIME);
  237. }
  238. return platform_driver_register(&ks8695wdt_driver);
  239. }
  240. static void __exit ks8695_wdt_exit(void)
  241. {
  242. platform_driver_unregister(&ks8695wdt_driver);
  243. }
  244. module_init(ks8695_wdt_init);
  245. module_exit(ks8695_wdt_exit);
  246. MODULE_AUTHOR("Andrew Victor");
  247. MODULE_DESCRIPTION("Watchdog driver for KS8695");
  248. MODULE_LICENSE("GPL");
  249. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);