renesas_wdt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Watchdog driver for Renesas WDT watchdog
  3. *
  4. * Copyright (C) 2015-17 Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
  5. * Copyright (C) 2015-17 Renesas Electronics Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/clk.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/smp.h>
  20. #include <linux/sys_soc.h>
  21. #include <linux/watchdog.h>
  22. #define RWTCNT 0
  23. #define RWTCSRA 4
  24. #define RWTCSRA_WOVF BIT(4)
  25. #define RWTCSRA_WRFLG BIT(5)
  26. #define RWTCSRA_TME BIT(7)
  27. #define RWTCSRB 8
  28. #define RWDT_DEFAULT_TIMEOUT 60U
  29. /*
  30. * In probe, clk_rate is checked to be not more than 16 bit * biggest clock
  31. * divider (12 bits). d is only a factor to fully utilize the WDT counter and
  32. * will not exceed its 16 bits. Thus, no overflow, we stay below 32 bits.
  33. */
  34. #define MUL_BY_CLKS_PER_SEC(p, d) \
  35. DIV_ROUND_UP((d) * (p)->clk_rate, clk_divs[(p)->cks])
  36. /* d is 16 bit, clk_divs 12 bit -> no 32 bit overflow */
  37. #define DIV_BY_CLKS_PER_SEC(p, d) ((d) * clk_divs[(p)->cks] / (p)->clk_rate)
  38. static const unsigned int clk_divs[] = { 1, 4, 16, 32, 64, 128, 1024, 4096 };
  39. static bool nowayout = WATCHDOG_NOWAYOUT;
  40. module_param(nowayout, bool, 0);
  41. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  42. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  43. struct rwdt_priv {
  44. void __iomem *base;
  45. struct watchdog_device wdev;
  46. unsigned long clk_rate;
  47. u16 time_left;
  48. u8 cks;
  49. };
  50. static void rwdt_write(struct rwdt_priv *priv, u32 val, unsigned int reg)
  51. {
  52. if (reg == RWTCNT)
  53. val |= 0x5a5a0000;
  54. else
  55. val |= 0xa5a5a500;
  56. writel_relaxed(val, priv->base + reg);
  57. }
  58. static int rwdt_init_timeout(struct watchdog_device *wdev)
  59. {
  60. struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
  61. rwdt_write(priv, 65536 - MUL_BY_CLKS_PER_SEC(priv, wdev->timeout), RWTCNT);
  62. return 0;
  63. }
  64. static int rwdt_start(struct watchdog_device *wdev)
  65. {
  66. struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
  67. u8 val;
  68. pm_runtime_get_sync(wdev->parent);
  69. /* Stop the timer before we modify any register */
  70. val = readb_relaxed(priv->base + RWTCSRA) & ~RWTCSRA_TME;
  71. rwdt_write(priv, val, RWTCSRA);
  72. rwdt_init_timeout(wdev);
  73. rwdt_write(priv, priv->cks, RWTCSRA);
  74. rwdt_write(priv, 0, RWTCSRB);
  75. while (readb_relaxed(priv->base + RWTCSRA) & RWTCSRA_WRFLG)
  76. cpu_relax();
  77. rwdt_write(priv, priv->cks | RWTCSRA_TME, RWTCSRA);
  78. return 0;
  79. }
  80. static int rwdt_stop(struct watchdog_device *wdev)
  81. {
  82. struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
  83. rwdt_write(priv, priv->cks, RWTCSRA);
  84. pm_runtime_put(wdev->parent);
  85. return 0;
  86. }
  87. static unsigned int rwdt_get_timeleft(struct watchdog_device *wdev)
  88. {
  89. struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
  90. u16 val = readw_relaxed(priv->base + RWTCNT);
  91. return DIV_BY_CLKS_PER_SEC(priv, 65536 - val);
  92. }
  93. static int rwdt_restart(struct watchdog_device *wdev, unsigned long action,
  94. void *data)
  95. {
  96. struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
  97. rwdt_start(wdev);
  98. rwdt_write(priv, 0xffff, RWTCNT);
  99. return 0;
  100. }
  101. static const struct watchdog_info rwdt_ident = {
  102. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  103. WDIOF_CARDRESET,
  104. .identity = "Renesas WDT Watchdog",
  105. };
  106. static const struct watchdog_ops rwdt_ops = {
  107. .owner = THIS_MODULE,
  108. .start = rwdt_start,
  109. .stop = rwdt_stop,
  110. .ping = rwdt_init_timeout,
  111. .get_timeleft = rwdt_get_timeleft,
  112. .restart = rwdt_restart,
  113. };
  114. #if defined(CONFIG_ARCH_RCAR_GEN2) && defined(CONFIG_SMP)
  115. /*
  116. * Watchdog-reset integration is broken on early revisions of R-Car Gen2 SoCs
  117. */
  118. static const struct soc_device_attribute rwdt_quirks_match[] = {
  119. {
  120. .soc_id = "r8a7790",
  121. .revision = "ES1.*",
  122. .data = (void *)1, /* needs single CPU */
  123. }, {
  124. .soc_id = "r8a7791",
  125. .revision = "ES1.*",
  126. .data = (void *)1, /* needs single CPU */
  127. }, {
  128. .soc_id = "r8a7792",
  129. .revision = "*",
  130. .data = (void *)0, /* needs SMP disabled */
  131. },
  132. { /* sentinel */ }
  133. };
  134. static bool rwdt_blacklisted(struct device *dev)
  135. {
  136. const struct soc_device_attribute *attr;
  137. attr = soc_device_match(rwdt_quirks_match);
  138. if (attr && setup_max_cpus > (uintptr_t)attr->data) {
  139. dev_info(dev, "Watchdog blacklisted on %s %s\n", attr->soc_id,
  140. attr->revision);
  141. return true;
  142. }
  143. return false;
  144. }
  145. #else /* !CONFIG_ARCH_RCAR_GEN2 || !CONFIG_SMP */
  146. static inline bool rwdt_blacklisted(struct device *dev) { return false; }
  147. #endif /* !CONFIG_ARCH_RCAR_GEN2 || !CONFIG_SMP */
  148. static int rwdt_probe(struct platform_device *pdev)
  149. {
  150. struct rwdt_priv *priv;
  151. struct resource *res;
  152. struct clk *clk;
  153. unsigned long clks_per_sec;
  154. int ret, i;
  155. if (rwdt_blacklisted(&pdev->dev))
  156. return -ENODEV;
  157. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  158. if (!priv)
  159. return -ENOMEM;
  160. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  161. priv->base = devm_ioremap_resource(&pdev->dev, res);
  162. if (IS_ERR(priv->base))
  163. return PTR_ERR(priv->base);
  164. clk = devm_clk_get(&pdev->dev, NULL);
  165. if (IS_ERR(clk))
  166. return PTR_ERR(clk);
  167. pm_runtime_enable(&pdev->dev);
  168. pm_runtime_get_sync(&pdev->dev);
  169. priv->clk_rate = clk_get_rate(clk);
  170. priv->wdev.bootstatus = (readb_relaxed(priv->base + RWTCSRA) &
  171. RWTCSRA_WOVF) ? WDIOF_CARDRESET : 0;
  172. pm_runtime_put(&pdev->dev);
  173. if (!priv->clk_rate) {
  174. ret = -ENOENT;
  175. goto out_pm_disable;
  176. }
  177. for (i = ARRAY_SIZE(clk_divs) - 1; i >= 0; i--) {
  178. clks_per_sec = priv->clk_rate / clk_divs[i];
  179. if (clks_per_sec && clks_per_sec < 65536) {
  180. priv->cks = i;
  181. break;
  182. }
  183. }
  184. if (i < 0) {
  185. dev_err(&pdev->dev, "Can't find suitable clock divider\n");
  186. ret = -ERANGE;
  187. goto out_pm_disable;
  188. }
  189. priv->wdev.info = &rwdt_ident,
  190. priv->wdev.ops = &rwdt_ops,
  191. priv->wdev.parent = &pdev->dev;
  192. priv->wdev.min_timeout = 1;
  193. priv->wdev.max_timeout = DIV_BY_CLKS_PER_SEC(priv, 65536);
  194. priv->wdev.timeout = min(priv->wdev.max_timeout, RWDT_DEFAULT_TIMEOUT);
  195. platform_set_drvdata(pdev, priv);
  196. watchdog_set_drvdata(&priv->wdev, priv);
  197. watchdog_set_nowayout(&priv->wdev, nowayout);
  198. watchdog_set_restart_priority(&priv->wdev, 0);
  199. watchdog_stop_on_unregister(&priv->wdev);
  200. /* This overrides the default timeout only if DT configuration was found */
  201. ret = watchdog_init_timeout(&priv->wdev, 0, &pdev->dev);
  202. if (ret)
  203. dev_warn(&pdev->dev, "Specified timeout value invalid, using default\n");
  204. ret = watchdog_register_device(&priv->wdev);
  205. if (ret < 0)
  206. goto out_pm_disable;
  207. return 0;
  208. out_pm_disable:
  209. pm_runtime_disable(&pdev->dev);
  210. return ret;
  211. }
  212. static int rwdt_remove(struct platform_device *pdev)
  213. {
  214. struct rwdt_priv *priv = platform_get_drvdata(pdev);
  215. watchdog_unregister_device(&priv->wdev);
  216. pm_runtime_disable(&pdev->dev);
  217. return 0;
  218. }
  219. static int __maybe_unused rwdt_suspend(struct device *dev)
  220. {
  221. struct rwdt_priv *priv = dev_get_drvdata(dev);
  222. if (watchdog_active(&priv->wdev)) {
  223. priv->time_left = readw(priv->base + RWTCNT);
  224. rwdt_stop(&priv->wdev);
  225. }
  226. return 0;
  227. }
  228. static int __maybe_unused rwdt_resume(struct device *dev)
  229. {
  230. struct rwdt_priv *priv = dev_get_drvdata(dev);
  231. if (watchdog_active(&priv->wdev)) {
  232. rwdt_start(&priv->wdev);
  233. rwdt_write(priv, priv->time_left, RWTCNT);
  234. }
  235. return 0;
  236. }
  237. static SIMPLE_DEV_PM_OPS(rwdt_pm_ops, rwdt_suspend, rwdt_resume);
  238. static const struct of_device_id rwdt_ids[] = {
  239. { .compatible = "renesas,rcar-gen2-wdt", },
  240. { .compatible = "renesas,rcar-gen3-wdt", },
  241. { /* sentinel */ }
  242. };
  243. MODULE_DEVICE_TABLE(of, rwdt_ids);
  244. static struct platform_driver rwdt_driver = {
  245. .driver = {
  246. .name = "renesas_wdt",
  247. .of_match_table = rwdt_ids,
  248. .pm = &rwdt_pm_ops,
  249. },
  250. .probe = rwdt_probe,
  251. .remove = rwdt_remove,
  252. };
  253. module_platform_driver(rwdt_driver);
  254. MODULE_DESCRIPTION("Renesas WDT Watchdog Driver");
  255. MODULE_LICENSE("GPL v2");
  256. MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");