dw_wdt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright 2010-2011 Picochip Ltd., Jamie Iles
  3. * http://www.picochip.com
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * This file implements a driver for the Synopsys DesignWare watchdog device
  11. * in the many ARM subsystems. The watchdog has 16 different timeout periods
  12. * and these are a function of the input clock frequency.
  13. *
  14. * The DesignWare watchdog cannot be stopped once it has been started so we
  15. * use a software timer to implement a ping that will keep the watchdog alive.
  16. * If we receive an expected close for the watchdog then we keep the timer
  17. * running, otherwise the timer is stopped and the watchdog will expire.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/bitops.h>
  21. #include <linux/clk.h>
  22. #include <linux/device.h>
  23. #include <linux/err.h>
  24. #include <linux/fs.h>
  25. #include <linux/io.h>
  26. #include <linux/kernel.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/pm.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/timer.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/watchdog.h>
  36. #define WDOG_CONTROL_REG_OFFSET 0x00
  37. #define WDOG_CONTROL_REG_WDT_EN_MASK 0x01
  38. #define WDOG_TIMEOUT_RANGE_REG_OFFSET 0x04
  39. #define WDOG_CURRENT_COUNT_REG_OFFSET 0x08
  40. #define WDOG_COUNTER_RESTART_REG_OFFSET 0x0c
  41. #define WDOG_COUNTER_RESTART_KICK_VALUE 0x76
  42. /* The maximum TOP (timeout period) value that can be set in the watchdog. */
  43. #define DW_WDT_MAX_TOP 15
  44. static bool nowayout = WATCHDOG_NOWAYOUT;
  45. module_param(nowayout, bool, 0);
  46. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  47. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  48. #define WDT_TIMEOUT (HZ / 2)
  49. static struct {
  50. spinlock_t lock;
  51. void __iomem *regs;
  52. struct clk *clk;
  53. unsigned long in_use;
  54. unsigned long next_heartbeat;
  55. struct timer_list timer;
  56. int expect_close;
  57. } dw_wdt;
  58. static inline int dw_wdt_is_enabled(void)
  59. {
  60. return readl(dw_wdt.regs + WDOG_CONTROL_REG_OFFSET) &
  61. WDOG_CONTROL_REG_WDT_EN_MASK;
  62. }
  63. static inline int dw_wdt_top_in_seconds(unsigned top)
  64. {
  65. /*
  66. * There are 16 possible timeout values in 0..15 where the number of
  67. * cycles is 2 ^ (16 + i) and the watchdog counts down.
  68. */
  69. return (1 << (16 + top)) / clk_get_rate(dw_wdt.clk);
  70. }
  71. static int dw_wdt_get_top(void)
  72. {
  73. int top = readl(dw_wdt.regs + WDOG_TIMEOUT_RANGE_REG_OFFSET) & 0xF;
  74. return dw_wdt_top_in_seconds(top);
  75. }
  76. static inline void dw_wdt_set_next_heartbeat(void)
  77. {
  78. dw_wdt.next_heartbeat = jiffies + dw_wdt_get_top() * HZ;
  79. }
  80. static int dw_wdt_set_top(unsigned top_s)
  81. {
  82. int i, top_val = DW_WDT_MAX_TOP;
  83. /*
  84. * Iterate over the timeout values until we find the closest match. We
  85. * always look for >=.
  86. */
  87. for (i = 0; i <= DW_WDT_MAX_TOP; ++i)
  88. if (dw_wdt_top_in_seconds(i) >= top_s) {
  89. top_val = i;
  90. break;
  91. }
  92. /* Set the new value in the watchdog. */
  93. writel(top_val, dw_wdt.regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  94. dw_wdt_set_next_heartbeat();
  95. return dw_wdt_top_in_seconds(top_val);
  96. }
  97. static void dw_wdt_keepalive(void)
  98. {
  99. writel(WDOG_COUNTER_RESTART_KICK_VALUE, dw_wdt.regs +
  100. WDOG_COUNTER_RESTART_REG_OFFSET);
  101. }
  102. static void dw_wdt_ping(unsigned long data)
  103. {
  104. if (time_before(jiffies, dw_wdt.next_heartbeat) ||
  105. (!nowayout && !dw_wdt.in_use)) {
  106. dw_wdt_keepalive();
  107. mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
  108. } else
  109. pr_crit("keepalive missed, machine will reset\n");
  110. }
  111. static int dw_wdt_open(struct inode *inode, struct file *filp)
  112. {
  113. if (test_and_set_bit(0, &dw_wdt.in_use))
  114. return -EBUSY;
  115. /* Make sure we don't get unloaded. */
  116. __module_get(THIS_MODULE);
  117. spin_lock(&dw_wdt.lock);
  118. if (!dw_wdt_is_enabled()) {
  119. /*
  120. * The watchdog is not currently enabled. Set the timeout to
  121. * the maximum and then start it.
  122. */
  123. dw_wdt_set_top(DW_WDT_MAX_TOP);
  124. writel(WDOG_CONTROL_REG_WDT_EN_MASK,
  125. dw_wdt.regs + WDOG_CONTROL_REG_OFFSET);
  126. }
  127. dw_wdt_set_next_heartbeat();
  128. spin_unlock(&dw_wdt.lock);
  129. return nonseekable_open(inode, filp);
  130. }
  131. ssize_t dw_wdt_write(struct file *filp, const char __user *buf, size_t len,
  132. loff_t *offset)
  133. {
  134. if (!len)
  135. return 0;
  136. if (!nowayout) {
  137. size_t i;
  138. dw_wdt.expect_close = 0;
  139. for (i = 0; i < len; ++i) {
  140. char c;
  141. if (get_user(c, buf + i))
  142. return -EFAULT;
  143. if (c == 'V') {
  144. dw_wdt.expect_close = 1;
  145. break;
  146. }
  147. }
  148. }
  149. dw_wdt_set_next_heartbeat();
  150. mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
  151. return len;
  152. }
  153. static u32 dw_wdt_time_left(void)
  154. {
  155. return readl(dw_wdt.regs + WDOG_CURRENT_COUNT_REG_OFFSET) /
  156. clk_get_rate(dw_wdt.clk);
  157. }
  158. static const struct watchdog_info dw_wdt_ident = {
  159. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  160. WDIOF_MAGICCLOSE,
  161. .identity = "Synopsys DesignWare Watchdog",
  162. };
  163. static long dw_wdt_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  164. {
  165. unsigned long val;
  166. int timeout;
  167. switch (cmd) {
  168. case WDIOC_GETSUPPORT:
  169. return copy_to_user((struct watchdog_info *)arg, &dw_wdt_ident,
  170. sizeof(dw_wdt_ident)) ? -EFAULT : 0;
  171. case WDIOC_GETSTATUS:
  172. case WDIOC_GETBOOTSTATUS:
  173. return put_user(0, (int *)arg);
  174. case WDIOC_KEEPALIVE:
  175. dw_wdt_set_next_heartbeat();
  176. return 0;
  177. case WDIOC_SETTIMEOUT:
  178. if (get_user(val, (int __user *)arg))
  179. return -EFAULT;
  180. timeout = dw_wdt_set_top(val);
  181. return put_user(timeout , (int __user *)arg);
  182. case WDIOC_GETTIMEOUT:
  183. return put_user(dw_wdt_get_top(), (int __user *)arg);
  184. case WDIOC_GETTIMELEFT:
  185. /* Get the time left until expiry. */
  186. if (get_user(val, (int __user *)arg))
  187. return -EFAULT;
  188. return put_user(dw_wdt_time_left(), (int __user *)arg);
  189. default:
  190. return -ENOTTY;
  191. }
  192. }
  193. static int dw_wdt_release(struct inode *inode, struct file *filp)
  194. {
  195. clear_bit(0, &dw_wdt.in_use);
  196. if (!dw_wdt.expect_close) {
  197. del_timer(&dw_wdt.timer);
  198. if (!nowayout)
  199. pr_crit("unexpected close, system will reboot soon\n");
  200. else
  201. pr_crit("watchdog cannot be disabled, system will reboot soon\n");
  202. }
  203. dw_wdt.expect_close = 0;
  204. return 0;
  205. }
  206. #ifdef CONFIG_PM
  207. static int dw_wdt_suspend(struct device *dev)
  208. {
  209. clk_disable(dw_wdt.clk);
  210. return 0;
  211. }
  212. static int dw_wdt_resume(struct device *dev)
  213. {
  214. int err = clk_enable(dw_wdt.clk);
  215. if (err)
  216. return err;
  217. dw_wdt_keepalive();
  218. return 0;
  219. }
  220. static const struct dev_pm_ops dw_wdt_pm_ops = {
  221. .suspend = dw_wdt_suspend,
  222. .resume = dw_wdt_resume,
  223. };
  224. #endif /* CONFIG_PM */
  225. static const struct file_operations wdt_fops = {
  226. .owner = THIS_MODULE,
  227. .llseek = no_llseek,
  228. .open = dw_wdt_open,
  229. .write = dw_wdt_write,
  230. .unlocked_ioctl = dw_wdt_ioctl,
  231. .release = dw_wdt_release
  232. };
  233. static struct miscdevice dw_wdt_miscdev = {
  234. .fops = &wdt_fops,
  235. .name = "watchdog",
  236. .minor = WATCHDOG_MINOR,
  237. };
  238. static int __devinit dw_wdt_drv_probe(struct platform_device *pdev)
  239. {
  240. int ret;
  241. struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  242. if (!mem)
  243. return -EINVAL;
  244. dw_wdt.regs = devm_request_and_ioremap(&pdev->dev, mem);
  245. if (!dw_wdt.regs)
  246. return -ENOMEM;
  247. dw_wdt.clk = clk_get(&pdev->dev, NULL);
  248. if (IS_ERR(dw_wdt.clk))
  249. return PTR_ERR(dw_wdt.clk);
  250. ret = clk_enable(dw_wdt.clk);
  251. if (ret)
  252. goto out_put_clk;
  253. spin_lock_init(&dw_wdt.lock);
  254. ret = misc_register(&dw_wdt_miscdev);
  255. if (ret)
  256. goto out_disable_clk;
  257. dw_wdt_set_next_heartbeat();
  258. setup_timer(&dw_wdt.timer, dw_wdt_ping, 0);
  259. mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
  260. return 0;
  261. out_disable_clk:
  262. clk_disable(dw_wdt.clk);
  263. out_put_clk:
  264. clk_put(dw_wdt.clk);
  265. return ret;
  266. }
  267. static int __devexit dw_wdt_drv_remove(struct platform_device *pdev)
  268. {
  269. misc_deregister(&dw_wdt_miscdev);
  270. clk_disable(dw_wdt.clk);
  271. clk_put(dw_wdt.clk);
  272. return 0;
  273. }
  274. static struct platform_driver dw_wdt_driver = {
  275. .probe = dw_wdt_drv_probe,
  276. .remove = __devexit_p(dw_wdt_drv_remove),
  277. .driver = {
  278. .name = "dw_wdt",
  279. .owner = THIS_MODULE,
  280. #ifdef CONFIG_PM
  281. .pm = &dw_wdt_pm_ops,
  282. #endif /* CONFIG_PM */
  283. },
  284. };
  285. module_platform_driver(dw_wdt_driver);
  286. MODULE_AUTHOR("Jamie Iles");
  287. MODULE_DESCRIPTION("Synopsys DesignWare Watchdog Driver");
  288. MODULE_LICENSE("GPL");
  289. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);