dw_wdt.c 8.4 KB

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