ks8695_wdt.c 7.7 KB

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