ks8695_wdt.c 7.4 KB

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