ks8695_wdt.c 7.4 KB

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