renesas_wdt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Watchdog driver for Renesas WDT watchdog
  3. *
  4. * Copyright (C) 2015-16 Wolfram Sang, Sang Engineering <[email protected]>
  5. * Copyright (C) 2015-16 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/watchdog.h>
  20. #define RWTCNT 0
  21. #define RWTCSRA 4
  22. #define RWTCSRA_WOVF BIT(4)
  23. #define RWTCSRA_WRFLG BIT(5)
  24. #define RWTCSRA_TME BIT(7)
  25. #define RWDT_DEFAULT_TIMEOUT 60U
  26. static const unsigned int clk_divs[] = { 1, 4, 16, 32, 64, 128, 1024 };
  27. static bool nowayout = WATCHDOG_NOWAYOUT;
  28. module_param(nowayout, bool, 0);
  29. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  30. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  31. struct rwdt_priv {
  32. void __iomem *base;
  33. struct watchdog_device wdev;
  34. struct clk *clk;
  35. unsigned int clks_per_sec;
  36. u8 cks;
  37. };
  38. static void rwdt_write(struct rwdt_priv *priv, u32 val, unsigned int reg)
  39. {
  40. if (reg == RWTCNT)
  41. val |= 0x5a5a0000;
  42. else
  43. val |= 0xa5a5a500;
  44. writel_relaxed(val, priv->base + reg);
  45. }
  46. static int rwdt_init_timeout(struct watchdog_device *wdev)
  47. {
  48. struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
  49. rwdt_write(priv, 65536 - wdev->timeout * priv->clks_per_sec, RWTCNT);
  50. return 0;
  51. }
  52. static int rwdt_start(struct watchdog_device *wdev)
  53. {
  54. struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
  55. clk_prepare_enable(priv->clk);
  56. rwdt_write(priv, priv->cks, RWTCSRA);
  57. rwdt_init_timeout(wdev);
  58. while (readb_relaxed(priv->base + RWTCSRA) & RWTCSRA_WRFLG)
  59. cpu_relax();
  60. rwdt_write(priv, priv->cks | RWTCSRA_TME, RWTCSRA);
  61. return 0;
  62. }
  63. static int rwdt_stop(struct watchdog_device *wdev)
  64. {
  65. struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
  66. rwdt_write(priv, priv->cks, RWTCSRA);
  67. clk_disable_unprepare(priv->clk);
  68. return 0;
  69. }
  70. static unsigned int rwdt_get_timeleft(struct watchdog_device *wdev)
  71. {
  72. struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
  73. u16 val = readw_relaxed(priv->base + RWTCNT);
  74. return DIV_ROUND_CLOSEST(65536 - val, priv->clks_per_sec);
  75. }
  76. static const struct watchdog_info rwdt_ident = {
  77. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
  78. .identity = "Renesas WDT Watchdog",
  79. };
  80. static const struct watchdog_ops rwdt_ops = {
  81. .owner = THIS_MODULE,
  82. .start = rwdt_start,
  83. .stop = rwdt_stop,
  84. .ping = rwdt_init_timeout,
  85. .get_timeleft = rwdt_get_timeleft,
  86. };
  87. static int rwdt_probe(struct platform_device *pdev)
  88. {
  89. struct rwdt_priv *priv;
  90. struct resource *res;
  91. unsigned long rate;
  92. unsigned int clks_per_sec;
  93. int ret, i;
  94. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  95. if (!priv)
  96. return -ENOMEM;
  97. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  98. priv->base = devm_ioremap_resource(&pdev->dev, res);
  99. if (IS_ERR(priv->base))
  100. return PTR_ERR(priv->base);
  101. priv->clk = devm_clk_get(&pdev->dev, NULL);
  102. if (IS_ERR(priv->clk))
  103. return PTR_ERR(priv->clk);
  104. rate = clk_get_rate(priv->clk);
  105. if (!rate)
  106. return -ENOENT;
  107. for (i = ARRAY_SIZE(clk_divs) - 1; i >= 0; i--) {
  108. clks_per_sec = DIV_ROUND_UP(rate, clk_divs[i]);
  109. if (clks_per_sec) {
  110. priv->clks_per_sec = clks_per_sec;
  111. priv->cks = i;
  112. break;
  113. }
  114. }
  115. if (!clks_per_sec) {
  116. dev_err(&pdev->dev, "Can't find suitable clock divider\n");
  117. return -ERANGE;
  118. }
  119. pm_runtime_enable(&pdev->dev);
  120. pm_runtime_get_sync(&pdev->dev);
  121. priv->wdev.info = &rwdt_ident,
  122. priv->wdev.ops = &rwdt_ops,
  123. priv->wdev.parent = &pdev->dev;
  124. priv->wdev.min_timeout = 1;
  125. priv->wdev.max_timeout = 65536 / clks_per_sec;
  126. priv->wdev.timeout = min(priv->wdev.max_timeout, RWDT_DEFAULT_TIMEOUT);
  127. platform_set_drvdata(pdev, priv);
  128. watchdog_set_drvdata(&priv->wdev, priv);
  129. watchdog_set_nowayout(&priv->wdev, nowayout);
  130. /* This overrides the default timeout only if DT configuration was found */
  131. ret = watchdog_init_timeout(&priv->wdev, 0, &pdev->dev);
  132. if (ret)
  133. dev_warn(&pdev->dev, "Specified timeout value invalid, using default\n");
  134. ret = watchdog_register_device(&priv->wdev);
  135. if (ret < 0) {
  136. pm_runtime_put(&pdev->dev);
  137. pm_runtime_disable(&pdev->dev);
  138. return ret;
  139. }
  140. return 0;
  141. }
  142. static int rwdt_remove(struct platform_device *pdev)
  143. {
  144. struct rwdt_priv *priv = platform_get_drvdata(pdev);
  145. watchdog_unregister_device(&priv->wdev);
  146. pm_runtime_put(&pdev->dev);
  147. pm_runtime_disable(&pdev->dev);
  148. return 0;
  149. }
  150. /*
  151. * This driver does also fit for R-Car Gen2 (r8a779[0-4]) WDT. However, for SMP
  152. * to work there, one also needs a RESET (RST) driver which does not exist yet
  153. * due to HW issues. This needs to be solved before adding compatibles here.
  154. */
  155. static const struct of_device_id rwdt_ids[] = {
  156. { .compatible = "renesas,rcar-gen3-wdt", },
  157. { /* sentinel */ }
  158. };
  159. MODULE_DEVICE_TABLE(of, rwdt_ids);
  160. static struct platform_driver rwdt_driver = {
  161. .driver = {
  162. .name = "renesas_wdt",
  163. .of_match_table = rwdt_ids,
  164. },
  165. .probe = rwdt_probe,
  166. .remove = rwdt_remove,
  167. };
  168. module_platform_driver(rwdt_driver);
  169. MODULE_DESCRIPTION("Renesas WDT Watchdog Driver");
  170. MODULE_LICENSE("GPL v2");
  171. MODULE_AUTHOR("Wolfram Sang <[email protected]>");