ks8695_wdt.c 7.4 KB

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