dw_wdt.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. * do not implement a stop function. The watchdog core will continue to send
  16. * heartbeat requests after the watchdog device has been closed.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/bitops.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/err.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/of.h>
  28. #include <linux/pm.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/reset.h>
  31. #include <linux/watchdog.h>
  32. #define WDOG_CONTROL_REG_OFFSET 0x00
  33. #define WDOG_CONTROL_REG_WDT_EN_MASK 0x01
  34. #define WDOG_CONTROL_REG_RESP_MODE_MASK 0x02
  35. #define WDOG_TIMEOUT_RANGE_REG_OFFSET 0x04
  36. #define WDOG_TIMEOUT_RANGE_TOPINIT_SHIFT 4
  37. #define WDOG_CURRENT_COUNT_REG_OFFSET 0x08
  38. #define WDOG_COUNTER_RESTART_REG_OFFSET 0x0c
  39. #define WDOG_COUNTER_RESTART_KICK_VALUE 0x76
  40. /* The maximum TOP (timeout period) value that can be set in the watchdog. */
  41. #define DW_WDT_MAX_TOP 15
  42. #define DW_WDT_DEFAULT_SECONDS 30
  43. static bool nowayout = WATCHDOG_NOWAYOUT;
  44. module_param(nowayout, bool, 0);
  45. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  46. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  47. struct dw_wdt {
  48. void __iomem *regs;
  49. struct clk *clk;
  50. unsigned long rate;
  51. struct watchdog_device wdd;
  52. struct reset_control *rst;
  53. /* Save/restore */
  54. u32 control;
  55. u32 timeout;
  56. };
  57. #define to_dw_wdt(wdd) container_of(wdd, struct dw_wdt, wdd)
  58. static inline int dw_wdt_is_enabled(struct dw_wdt *dw_wdt)
  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(struct dw_wdt *dw_wdt, 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 (1U << (16 + top)) / dw_wdt->rate;
  70. }
  71. static int dw_wdt_get_top(struct dw_wdt *dw_wdt)
  72. {
  73. int top = readl(dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET) & 0xF;
  74. return dw_wdt_top_in_seconds(dw_wdt, top);
  75. }
  76. static int dw_wdt_ping(struct watchdog_device *wdd)
  77. {
  78. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  79. writel(WDOG_COUNTER_RESTART_KICK_VALUE, dw_wdt->regs +
  80. WDOG_COUNTER_RESTART_REG_OFFSET);
  81. return 0;
  82. }
  83. static int dw_wdt_set_timeout(struct watchdog_device *wdd, unsigned int top_s)
  84. {
  85. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  86. int i, top_val = DW_WDT_MAX_TOP;
  87. /*
  88. * Iterate over the timeout values until we find the closest match. We
  89. * always look for >=.
  90. */
  91. for (i = 0; i <= DW_WDT_MAX_TOP; ++i)
  92. if (dw_wdt_top_in_seconds(dw_wdt, i) >= top_s) {
  93. top_val = i;
  94. break;
  95. }
  96. /*
  97. * Set the new value in the watchdog. Some versions of dw_wdt
  98. * have have TOPINIT in the TIMEOUT_RANGE register (as per
  99. * CP_WDT_DUAL_TOP in WDT_COMP_PARAMS_1). On those we
  100. * effectively get a pat of the watchdog right here.
  101. */
  102. writel(top_val | top_val << WDOG_TIMEOUT_RANGE_TOPINIT_SHIFT,
  103. dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  104. wdd->timeout = dw_wdt_top_in_seconds(dw_wdt, top_val);
  105. return 0;
  106. }
  107. static void dw_wdt_arm_system_reset(struct dw_wdt *dw_wdt)
  108. {
  109. u32 val = readl(dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  110. /* Disable interrupt mode; always perform system reset. */
  111. val &= ~WDOG_CONTROL_REG_RESP_MODE_MASK;
  112. /* Enable watchdog. */
  113. val |= WDOG_CONTROL_REG_WDT_EN_MASK;
  114. writel(val, dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  115. }
  116. static int dw_wdt_start(struct watchdog_device *wdd)
  117. {
  118. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  119. dw_wdt_set_timeout(wdd, wdd->timeout);
  120. dw_wdt_arm_system_reset(dw_wdt);
  121. return 0;
  122. }
  123. static int dw_wdt_stop(struct watchdog_device *wdd)
  124. {
  125. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  126. if (!dw_wdt->rst) {
  127. set_bit(WDOG_HW_RUNNING, &wdd->status);
  128. return 0;
  129. }
  130. reset_control_assert(dw_wdt->rst);
  131. reset_control_deassert(dw_wdt->rst);
  132. return 0;
  133. }
  134. static int dw_wdt_restart(struct watchdog_device *wdd,
  135. unsigned long action, void *data)
  136. {
  137. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  138. writel(0, dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  139. if (dw_wdt_is_enabled(dw_wdt))
  140. writel(WDOG_COUNTER_RESTART_KICK_VALUE,
  141. dw_wdt->regs + WDOG_COUNTER_RESTART_REG_OFFSET);
  142. else
  143. dw_wdt_arm_system_reset(dw_wdt);
  144. /* wait for reset to assert... */
  145. mdelay(500);
  146. return 0;
  147. }
  148. static unsigned int dw_wdt_get_timeleft(struct watchdog_device *wdd)
  149. {
  150. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  151. return readl(dw_wdt->regs + WDOG_CURRENT_COUNT_REG_OFFSET) /
  152. dw_wdt->rate;
  153. }
  154. static const struct watchdog_info dw_wdt_ident = {
  155. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  156. WDIOF_MAGICCLOSE,
  157. .identity = "Synopsys DesignWare Watchdog",
  158. };
  159. static const struct watchdog_ops dw_wdt_ops = {
  160. .owner = THIS_MODULE,
  161. .start = dw_wdt_start,
  162. .stop = dw_wdt_stop,
  163. .ping = dw_wdt_ping,
  164. .set_timeout = dw_wdt_set_timeout,
  165. .get_timeleft = dw_wdt_get_timeleft,
  166. .restart = dw_wdt_restart,
  167. };
  168. #ifdef CONFIG_PM_SLEEP
  169. static int dw_wdt_suspend(struct device *dev)
  170. {
  171. struct dw_wdt *dw_wdt = dev_get_drvdata(dev);
  172. dw_wdt->control = readl(dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  173. dw_wdt->timeout = readl(dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  174. clk_disable_unprepare(dw_wdt->clk);
  175. return 0;
  176. }
  177. static int dw_wdt_resume(struct device *dev)
  178. {
  179. struct dw_wdt *dw_wdt = dev_get_drvdata(dev);
  180. int err = clk_prepare_enable(dw_wdt->clk);
  181. if (err)
  182. return err;
  183. writel(dw_wdt->timeout, dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  184. writel(dw_wdt->control, dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  185. dw_wdt_ping(&dw_wdt->wdd);
  186. return 0;
  187. }
  188. #endif /* CONFIG_PM_SLEEP */
  189. static SIMPLE_DEV_PM_OPS(dw_wdt_pm_ops, dw_wdt_suspend, dw_wdt_resume);
  190. static int dw_wdt_drv_probe(struct platform_device *pdev)
  191. {
  192. struct device *dev = &pdev->dev;
  193. struct watchdog_device *wdd;
  194. struct dw_wdt *dw_wdt;
  195. struct resource *mem;
  196. int ret;
  197. dw_wdt = devm_kzalloc(dev, sizeof(*dw_wdt), GFP_KERNEL);
  198. if (!dw_wdt)
  199. return -ENOMEM;
  200. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  201. dw_wdt->regs = devm_ioremap_resource(dev, mem);
  202. if (IS_ERR(dw_wdt->regs))
  203. return PTR_ERR(dw_wdt->regs);
  204. dw_wdt->clk = devm_clk_get(dev, NULL);
  205. if (IS_ERR(dw_wdt->clk))
  206. return PTR_ERR(dw_wdt->clk);
  207. ret = clk_prepare_enable(dw_wdt->clk);
  208. if (ret)
  209. return ret;
  210. dw_wdt->rate = clk_get_rate(dw_wdt->clk);
  211. if (dw_wdt->rate == 0) {
  212. ret = -EINVAL;
  213. goto out_disable_clk;
  214. }
  215. dw_wdt->rst = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
  216. if (IS_ERR(dw_wdt->rst)) {
  217. ret = PTR_ERR(dw_wdt->rst);
  218. goto out_disable_clk;
  219. }
  220. reset_control_deassert(dw_wdt->rst);
  221. wdd = &dw_wdt->wdd;
  222. wdd->info = &dw_wdt_ident;
  223. wdd->ops = &dw_wdt_ops;
  224. wdd->min_timeout = 1;
  225. wdd->max_hw_heartbeat_ms =
  226. dw_wdt_top_in_seconds(dw_wdt, DW_WDT_MAX_TOP) * 1000;
  227. wdd->parent = dev;
  228. watchdog_set_drvdata(wdd, dw_wdt);
  229. watchdog_set_nowayout(wdd, nowayout);
  230. watchdog_init_timeout(wdd, 0, dev);
  231. /*
  232. * If the watchdog is already running, use its already configured
  233. * timeout. Otherwise use the default or the value provided through
  234. * devicetree.
  235. */
  236. if (dw_wdt_is_enabled(dw_wdt)) {
  237. wdd->timeout = dw_wdt_get_top(dw_wdt);
  238. set_bit(WDOG_HW_RUNNING, &wdd->status);
  239. } else {
  240. wdd->timeout = DW_WDT_DEFAULT_SECONDS;
  241. watchdog_init_timeout(wdd, 0, dev);
  242. }
  243. platform_set_drvdata(pdev, dw_wdt);
  244. watchdog_set_restart_priority(wdd, 128);
  245. ret = watchdog_register_device(wdd);
  246. if (ret)
  247. goto out_disable_clk;
  248. return 0;
  249. out_disable_clk:
  250. clk_disable_unprepare(dw_wdt->clk);
  251. return ret;
  252. }
  253. static int dw_wdt_drv_remove(struct platform_device *pdev)
  254. {
  255. struct dw_wdt *dw_wdt = platform_get_drvdata(pdev);
  256. watchdog_unregister_device(&dw_wdt->wdd);
  257. reset_control_assert(dw_wdt->rst);
  258. clk_disable_unprepare(dw_wdt->clk);
  259. return 0;
  260. }
  261. #ifdef CONFIG_OF
  262. static const struct of_device_id dw_wdt_of_match[] = {
  263. { .compatible = "snps,dw-wdt", },
  264. { /* sentinel */ }
  265. };
  266. MODULE_DEVICE_TABLE(of, dw_wdt_of_match);
  267. #endif
  268. static struct platform_driver dw_wdt_driver = {
  269. .probe = dw_wdt_drv_probe,
  270. .remove = dw_wdt_drv_remove,
  271. .driver = {
  272. .name = "dw_wdt",
  273. .of_match_table = of_match_ptr(dw_wdt_of_match),
  274. .pm = &dw_wdt_pm_ops,
  275. },
  276. };
  277. module_platform_driver(dw_wdt_driver);
  278. MODULE_AUTHOR("Jamie Iles");
  279. MODULE_DESCRIPTION("Synopsys DesignWare Watchdog Driver");
  280. MODULE_LICENSE("GPL");