dw_wdt.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 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/delay.h>
  23. #include <linux/device.h>
  24. #include <linux/err.h>
  25. #include <linux/fs.h>
  26. #include <linux/io.h>
  27. #include <linux/kernel.h>
  28. #include <linux/miscdevice.h>
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/notifier.h>
  32. #include <linux/of.h>
  33. #include <linux/pm.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/reboot.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/timer.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/watchdog.h>
  40. #define WDOG_CONTROL_REG_OFFSET 0x00
  41. #define WDOG_CONTROL_REG_WDT_EN_MASK 0x01
  42. #define WDOG_TIMEOUT_RANGE_REG_OFFSET 0x04
  43. #define WDOG_TIMEOUT_RANGE_TOPINIT_SHIFT 4
  44. #define WDOG_CURRENT_COUNT_REG_OFFSET 0x08
  45. #define WDOG_COUNTER_RESTART_REG_OFFSET 0x0c
  46. #define WDOG_COUNTER_RESTART_KICK_VALUE 0x76
  47. /* The maximum TOP (timeout period) value that can be set in the watchdog. */
  48. #define DW_WDT_MAX_TOP 15
  49. static bool nowayout = WATCHDOG_NOWAYOUT;
  50. module_param(nowayout, bool, 0);
  51. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  52. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  53. #define WDT_TIMEOUT (HZ / 2)
  54. static struct {
  55. spinlock_t lock;
  56. void __iomem *regs;
  57. struct clk *clk;
  58. unsigned long in_use;
  59. unsigned long next_heartbeat;
  60. struct timer_list timer;
  61. int expect_close;
  62. struct notifier_block restart_handler;
  63. } dw_wdt;
  64. static inline int dw_wdt_is_enabled(void)
  65. {
  66. return readl(dw_wdt.regs + WDOG_CONTROL_REG_OFFSET) &
  67. WDOG_CONTROL_REG_WDT_EN_MASK;
  68. }
  69. static inline int dw_wdt_top_in_seconds(unsigned top)
  70. {
  71. /*
  72. * There are 16 possible timeout values in 0..15 where the number of
  73. * cycles is 2 ^ (16 + i) and the watchdog counts down.
  74. */
  75. return (1 << (16 + top)) / clk_get_rate(dw_wdt.clk);
  76. }
  77. static int dw_wdt_get_top(void)
  78. {
  79. int top = readl(dw_wdt.regs + WDOG_TIMEOUT_RANGE_REG_OFFSET) & 0xF;
  80. return dw_wdt_top_in_seconds(top);
  81. }
  82. static inline void dw_wdt_set_next_heartbeat(void)
  83. {
  84. dw_wdt.next_heartbeat = jiffies + dw_wdt_get_top() * HZ;
  85. }
  86. static int dw_wdt_set_top(unsigned top_s)
  87. {
  88. int i, top_val = DW_WDT_MAX_TOP;
  89. /*
  90. * Iterate over the timeout values until we find the closest match. We
  91. * always look for >=.
  92. */
  93. for (i = 0; i <= DW_WDT_MAX_TOP; ++i)
  94. if (dw_wdt_top_in_seconds(i) >= top_s) {
  95. top_val = i;
  96. break;
  97. }
  98. /* Set the new value in the watchdog. */
  99. writel(top_val | top_val << WDOG_TIMEOUT_RANGE_TOPINIT_SHIFT,
  100. dw_wdt.regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  101. dw_wdt_set_next_heartbeat();
  102. return dw_wdt_top_in_seconds(top_val);
  103. }
  104. static void dw_wdt_keepalive(void)
  105. {
  106. writel(WDOG_COUNTER_RESTART_KICK_VALUE, dw_wdt.regs +
  107. WDOG_COUNTER_RESTART_REG_OFFSET);
  108. }
  109. static int dw_wdt_restart_handle(struct notifier_block *this,
  110. unsigned long mode, void *cmd)
  111. {
  112. u32 val;
  113. writel(0, dw_wdt.regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  114. val = readl(dw_wdt.regs + WDOG_CONTROL_REG_OFFSET);
  115. if (val & WDOG_CONTROL_REG_WDT_EN_MASK)
  116. writel(WDOG_COUNTER_RESTART_KICK_VALUE, dw_wdt.regs +
  117. WDOG_COUNTER_RESTART_REG_OFFSET);
  118. else
  119. writel(WDOG_CONTROL_REG_WDT_EN_MASK,
  120. dw_wdt.regs + WDOG_CONTROL_REG_OFFSET);
  121. /* wait for reset to assert... */
  122. mdelay(500);
  123. return NOTIFY_DONE;
  124. }
  125. static void dw_wdt_ping(unsigned long data)
  126. {
  127. if (time_before(jiffies, dw_wdt.next_heartbeat) ||
  128. (!nowayout && !dw_wdt.in_use)) {
  129. dw_wdt_keepalive();
  130. mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
  131. } else
  132. pr_crit("keepalive missed, machine will reset\n");
  133. }
  134. static int dw_wdt_open(struct inode *inode, struct file *filp)
  135. {
  136. if (test_and_set_bit(0, &dw_wdt.in_use))
  137. return -EBUSY;
  138. /* Make sure we don't get unloaded. */
  139. __module_get(THIS_MODULE);
  140. spin_lock(&dw_wdt.lock);
  141. if (!dw_wdt_is_enabled()) {
  142. /*
  143. * The watchdog is not currently enabled. Set the timeout to
  144. * the maximum and then start it.
  145. */
  146. dw_wdt_set_top(DW_WDT_MAX_TOP);
  147. writel(WDOG_CONTROL_REG_WDT_EN_MASK,
  148. dw_wdt.regs + WDOG_CONTROL_REG_OFFSET);
  149. }
  150. dw_wdt_set_next_heartbeat();
  151. spin_unlock(&dw_wdt.lock);
  152. return nonseekable_open(inode, filp);
  153. }
  154. static ssize_t dw_wdt_write(struct file *filp, const char __user *buf,
  155. size_t len, loff_t *offset)
  156. {
  157. if (!len)
  158. return 0;
  159. if (!nowayout) {
  160. size_t i;
  161. dw_wdt.expect_close = 0;
  162. for (i = 0; i < len; ++i) {
  163. char c;
  164. if (get_user(c, buf + i))
  165. return -EFAULT;
  166. if (c == 'V') {
  167. dw_wdt.expect_close = 1;
  168. break;
  169. }
  170. }
  171. }
  172. dw_wdt_set_next_heartbeat();
  173. mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
  174. return len;
  175. }
  176. static u32 dw_wdt_time_left(void)
  177. {
  178. return readl(dw_wdt.regs + WDOG_CURRENT_COUNT_REG_OFFSET) /
  179. clk_get_rate(dw_wdt.clk);
  180. }
  181. static const struct watchdog_info dw_wdt_ident = {
  182. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  183. WDIOF_MAGICCLOSE,
  184. .identity = "Synopsys DesignWare Watchdog",
  185. };
  186. static long dw_wdt_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  187. {
  188. unsigned long val;
  189. int timeout;
  190. switch (cmd) {
  191. case WDIOC_GETSUPPORT:
  192. return copy_to_user((void __user *)arg, &dw_wdt_ident,
  193. sizeof(dw_wdt_ident)) ? -EFAULT : 0;
  194. case WDIOC_GETSTATUS:
  195. case WDIOC_GETBOOTSTATUS:
  196. return put_user(0, (int __user *)arg);
  197. case WDIOC_KEEPALIVE:
  198. dw_wdt_set_next_heartbeat();
  199. return 0;
  200. case WDIOC_SETTIMEOUT:
  201. if (get_user(val, (int __user *)arg))
  202. return -EFAULT;
  203. timeout = dw_wdt_set_top(val);
  204. return put_user(timeout , (int __user *)arg);
  205. case WDIOC_GETTIMEOUT:
  206. return put_user(dw_wdt_get_top(), (int __user *)arg);
  207. case WDIOC_GETTIMELEFT:
  208. /* Get the time left until expiry. */
  209. if (get_user(val, (int __user *)arg))
  210. return -EFAULT;
  211. return put_user(dw_wdt_time_left(), (int __user *)arg);
  212. default:
  213. return -ENOTTY;
  214. }
  215. }
  216. static int dw_wdt_release(struct inode *inode, struct file *filp)
  217. {
  218. clear_bit(0, &dw_wdt.in_use);
  219. if (!dw_wdt.expect_close) {
  220. del_timer(&dw_wdt.timer);
  221. if (!nowayout)
  222. pr_crit("unexpected close, system will reboot soon\n");
  223. else
  224. pr_crit("watchdog cannot be disabled, system will reboot soon\n");
  225. }
  226. dw_wdt.expect_close = 0;
  227. return 0;
  228. }
  229. #ifdef CONFIG_PM_SLEEP
  230. static int dw_wdt_suspend(struct device *dev)
  231. {
  232. clk_disable_unprepare(dw_wdt.clk);
  233. return 0;
  234. }
  235. static int dw_wdt_resume(struct device *dev)
  236. {
  237. int err = clk_prepare_enable(dw_wdt.clk);
  238. if (err)
  239. return err;
  240. dw_wdt_keepalive();
  241. return 0;
  242. }
  243. #endif /* CONFIG_PM_SLEEP */
  244. static SIMPLE_DEV_PM_OPS(dw_wdt_pm_ops, dw_wdt_suspend, dw_wdt_resume);
  245. static const struct file_operations wdt_fops = {
  246. .owner = THIS_MODULE,
  247. .llseek = no_llseek,
  248. .open = dw_wdt_open,
  249. .write = dw_wdt_write,
  250. .unlocked_ioctl = dw_wdt_ioctl,
  251. .release = dw_wdt_release
  252. };
  253. static struct miscdevice dw_wdt_miscdev = {
  254. .fops = &wdt_fops,
  255. .name = "watchdog",
  256. .minor = WATCHDOG_MINOR,
  257. };
  258. static int dw_wdt_drv_probe(struct platform_device *pdev)
  259. {
  260. int ret;
  261. struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  262. dw_wdt.regs = devm_ioremap_resource(&pdev->dev, mem);
  263. if (IS_ERR(dw_wdt.regs))
  264. return PTR_ERR(dw_wdt.regs);
  265. dw_wdt.clk = devm_clk_get(&pdev->dev, NULL);
  266. if (IS_ERR(dw_wdt.clk))
  267. return PTR_ERR(dw_wdt.clk);
  268. ret = clk_prepare_enable(dw_wdt.clk);
  269. if (ret)
  270. return ret;
  271. spin_lock_init(&dw_wdt.lock);
  272. ret = misc_register(&dw_wdt_miscdev);
  273. if (ret)
  274. goto out_disable_clk;
  275. dw_wdt.restart_handler.notifier_call = dw_wdt_restart_handle;
  276. dw_wdt.restart_handler.priority = 128;
  277. ret = register_restart_handler(&dw_wdt.restart_handler);
  278. if (ret)
  279. pr_warn("cannot register restart handler\n");
  280. dw_wdt_set_next_heartbeat();
  281. setup_timer(&dw_wdt.timer, dw_wdt_ping, 0);
  282. mod_timer(&dw_wdt.timer, jiffies + WDT_TIMEOUT);
  283. return 0;
  284. out_disable_clk:
  285. clk_disable_unprepare(dw_wdt.clk);
  286. return ret;
  287. }
  288. static int dw_wdt_drv_remove(struct platform_device *pdev)
  289. {
  290. unregister_restart_handler(&dw_wdt.restart_handler);
  291. misc_deregister(&dw_wdt_miscdev);
  292. clk_disable_unprepare(dw_wdt.clk);
  293. return 0;
  294. }
  295. #ifdef CONFIG_OF
  296. static const struct of_device_id dw_wdt_of_match[] = {
  297. { .compatible = "snps,dw-wdt", },
  298. { /* sentinel */ }
  299. };
  300. MODULE_DEVICE_TABLE(of, dw_wdt_of_match);
  301. #endif
  302. static struct platform_driver dw_wdt_driver = {
  303. .probe = dw_wdt_drv_probe,
  304. .remove = dw_wdt_drv_remove,
  305. .driver = {
  306. .name = "dw_wdt",
  307. .of_match_table = of_match_ptr(dw_wdt_of_match),
  308. .pm = &dw_wdt_pm_ops,
  309. },
  310. };
  311. module_platform_driver(dw_wdt_driver);
  312. MODULE_AUTHOR("Jamie Iles");
  313. MODULE_DESCRIPTION("Synopsys DesignWare Watchdog Driver");
  314. MODULE_LICENSE("GPL");