rza_wdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Renesas RZ/A Series WDT Driver
  3. *
  4. * Copyright (C) 2017 Renesas Electronics America, Inc.
  5. * Copyright (C) 2017 Chris Brandt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/watchdog.h>
  18. #define DEFAULT_TIMEOUT 30
  19. /* Watchdog Timer Registers */
  20. #define WTCSR 0
  21. #define WTCSR_MAGIC 0xA500
  22. #define WTSCR_WT BIT(6)
  23. #define WTSCR_TME BIT(5)
  24. #define WTSCR_CKS(i) (i)
  25. #define WTCNT 2
  26. #define WTCNT_MAGIC 0x5A00
  27. #define WRCSR 4
  28. #define WRCSR_MAGIC 0x5A00
  29. #define WRCSR_RSTE BIT(6)
  30. #define WRCSR_CLEAR_WOVF 0xA500 /* special value */
  31. struct rza_wdt {
  32. struct watchdog_device wdev;
  33. void __iomem *base;
  34. struct clk *clk;
  35. };
  36. static int rza_wdt_start(struct watchdog_device *wdev)
  37. {
  38. struct rza_wdt *priv = watchdog_get_drvdata(wdev);
  39. /* Stop timer */
  40. writew(WTCSR_MAGIC | 0, priv->base + WTCSR);
  41. /* Must dummy read WRCSR:WOVF at least once before clearing */
  42. readb(priv->base + WRCSR);
  43. writew(WRCSR_CLEAR_WOVF, priv->base + WRCSR);
  44. /*
  45. * Start timer with slowest clock source and reset option enabled.
  46. */
  47. writew(WRCSR_MAGIC | WRCSR_RSTE, priv->base + WRCSR);
  48. writew(WTCNT_MAGIC | 0, priv->base + WTCNT);
  49. writew(WTCSR_MAGIC | WTSCR_WT | WTSCR_TME | WTSCR_CKS(7),
  50. priv->base + WTCSR);
  51. return 0;
  52. }
  53. static int rza_wdt_stop(struct watchdog_device *wdev)
  54. {
  55. struct rza_wdt *priv = watchdog_get_drvdata(wdev);
  56. writew(WTCSR_MAGIC | 0, priv->base + WTCSR);
  57. return 0;
  58. }
  59. static int rza_wdt_ping(struct watchdog_device *wdev)
  60. {
  61. struct rza_wdt *priv = watchdog_get_drvdata(wdev);
  62. writew(WTCNT_MAGIC | 0, priv->base + WTCNT);
  63. return 0;
  64. }
  65. static int rza_wdt_restart(struct watchdog_device *wdev, unsigned long action,
  66. void *data)
  67. {
  68. struct rza_wdt *priv = watchdog_get_drvdata(wdev);
  69. /* Stop timer */
  70. writew(WTCSR_MAGIC | 0, priv->base + WTCSR);
  71. /* Must dummy read WRCSR:WOVF at least once before clearing */
  72. readb(priv->base + WRCSR);
  73. writew(WRCSR_CLEAR_WOVF, priv->base + WRCSR);
  74. /*
  75. * Start timer with fastest clock source and only 1 clock left before
  76. * overflow with reset option enabled.
  77. */
  78. writew(WRCSR_MAGIC | WRCSR_RSTE, priv->base + WRCSR);
  79. writew(WTCNT_MAGIC | 255, priv->base + WTCNT);
  80. writew(WTCSR_MAGIC | WTSCR_WT | WTSCR_TME, priv->base + WTCSR);
  81. /*
  82. * Actually make sure the above sequence hits hardware before sleeping.
  83. */
  84. wmb();
  85. /* Wait for WDT overflow (reset) */
  86. udelay(20);
  87. return 0;
  88. }
  89. static const struct watchdog_info rza_wdt_ident = {
  90. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
  91. .identity = "Renesas RZ/A WDT Watchdog",
  92. };
  93. static const struct watchdog_ops rza_wdt_ops = {
  94. .owner = THIS_MODULE,
  95. .start = rza_wdt_start,
  96. .stop = rza_wdt_stop,
  97. .ping = rza_wdt_ping,
  98. .restart = rza_wdt_restart,
  99. };
  100. static int rza_wdt_probe(struct platform_device *pdev)
  101. {
  102. struct rza_wdt *priv;
  103. struct resource *res;
  104. unsigned long rate;
  105. int ret;
  106. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  107. if (!priv)
  108. return -ENOMEM;
  109. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  110. priv->base = devm_ioremap_resource(&pdev->dev, res);
  111. if (IS_ERR(priv->base))
  112. return PTR_ERR(priv->base);
  113. priv->clk = devm_clk_get(&pdev->dev, NULL);
  114. if (IS_ERR(priv->clk))
  115. return PTR_ERR(priv->clk);
  116. rate = clk_get_rate(priv->clk);
  117. if (rate < 16384) {
  118. dev_err(&pdev->dev, "invalid clock rate (%ld)\n", rate);
  119. return -ENOENT;
  120. }
  121. /* Assume slowest clock rate possible (CKS=7) */
  122. rate /= 16384;
  123. priv->wdev.info = &rza_wdt_ident,
  124. priv->wdev.ops = &rza_wdt_ops,
  125. priv->wdev.parent = &pdev->dev;
  126. /*
  127. * Since the max possible timeout of our 8-bit count register is less
  128. * than a second, we must use max_hw_heartbeat_ms.
  129. */
  130. priv->wdev.max_hw_heartbeat_ms = (1000 * U8_MAX) / rate;
  131. dev_dbg(&pdev->dev, "max hw timeout of %dms\n",
  132. priv->wdev.max_hw_heartbeat_ms);
  133. priv->wdev.min_timeout = 1;
  134. priv->wdev.timeout = DEFAULT_TIMEOUT;
  135. watchdog_init_timeout(&priv->wdev, 0, &pdev->dev);
  136. watchdog_set_drvdata(&priv->wdev, priv);
  137. ret = devm_watchdog_register_device(&pdev->dev, &priv->wdev);
  138. if (ret)
  139. dev_err(&pdev->dev, "Cannot register watchdog device\n");
  140. return ret;
  141. }
  142. static const struct of_device_id rza_wdt_of_match[] = {
  143. { .compatible = "renesas,rza-wdt", },
  144. { /* sentinel */ }
  145. };
  146. MODULE_DEVICE_TABLE(of, rza_wdt_of_match);
  147. static struct platform_driver rza_wdt_driver = {
  148. .probe = rza_wdt_probe,
  149. .driver = {
  150. .name = "rza_wdt",
  151. .of_match_table = rza_wdt_of_match,
  152. },
  153. };
  154. module_platform_driver(rza_wdt_driver);
  155. MODULE_DESCRIPTION("Renesas RZ/A WDT Driver");
  156. MODULE_AUTHOR("Chris Brandt <chris.brandt@renesas.com>");
  157. MODULE_LICENSE("GPL v2");