txx9wdt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs
  4. *
  5. * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/types.h>
  11. #include <linux/watchdog.h>
  12. #include <linux/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <asm/txx9tmr.h>
  18. #define WD_TIMER_CCD 7 /* 1/256 */
  19. #define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
  20. #define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
  21. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  22. static unsigned int timeout = TIMER_MARGIN; /* in seconds */
  23. module_param(timeout, uint, 0);
  24. MODULE_PARM_DESC(timeout,
  25. "Watchdog timeout in seconds. "
  26. "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), "
  27. "default=" __MODULE_STRING(TIMER_MARGIN) ")");
  28. static bool nowayout = WATCHDOG_NOWAYOUT;
  29. module_param(nowayout, bool, 0);
  30. MODULE_PARM_DESC(nowayout,
  31. "Watchdog cannot be stopped once started "
  32. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  33. static struct txx9_tmr_reg __iomem *txx9wdt_reg;
  34. static struct clk *txx9_imclk;
  35. static DEFINE_SPINLOCK(txx9_lock);
  36. static int txx9wdt_ping(struct watchdog_device *wdt_dev)
  37. {
  38. spin_lock(&txx9_lock);
  39. __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
  40. spin_unlock(&txx9_lock);
  41. return 0;
  42. }
  43. static int txx9wdt_start(struct watchdog_device *wdt_dev)
  44. {
  45. spin_lock(&txx9_lock);
  46. __raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra);
  47. __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
  48. __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */
  49. __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
  50. &txx9wdt_reg->tcr);
  51. __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
  52. spin_unlock(&txx9_lock);
  53. return 0;
  54. }
  55. static int txx9wdt_stop(struct watchdog_device *wdt_dev)
  56. {
  57. spin_lock(&txx9_lock);
  58. __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
  59. __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
  60. &txx9wdt_reg->tcr);
  61. spin_unlock(&txx9_lock);
  62. return 0;
  63. }
  64. static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev,
  65. unsigned int new_timeout)
  66. {
  67. wdt_dev->timeout = new_timeout;
  68. txx9wdt_stop(wdt_dev);
  69. txx9wdt_start(wdt_dev);
  70. return 0;
  71. }
  72. static const struct watchdog_info txx9wdt_info = {
  73. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  74. .identity = "Hardware Watchdog for TXx9",
  75. };
  76. static const struct watchdog_ops txx9wdt_ops = {
  77. .owner = THIS_MODULE,
  78. .start = txx9wdt_start,
  79. .stop = txx9wdt_stop,
  80. .ping = txx9wdt_ping,
  81. .set_timeout = txx9wdt_set_timeout,
  82. };
  83. static struct watchdog_device txx9wdt = {
  84. .info = &txx9wdt_info,
  85. .ops = &txx9wdt_ops,
  86. };
  87. static int __init txx9wdt_probe(struct platform_device *dev)
  88. {
  89. int ret;
  90. txx9_imclk = clk_get(NULL, "imbus_clk");
  91. if (IS_ERR(txx9_imclk)) {
  92. ret = PTR_ERR(txx9_imclk);
  93. txx9_imclk = NULL;
  94. goto exit;
  95. }
  96. ret = clk_prepare_enable(txx9_imclk);
  97. if (ret) {
  98. clk_put(txx9_imclk);
  99. txx9_imclk = NULL;
  100. goto exit;
  101. }
  102. txx9wdt_reg = devm_platform_ioremap_resource(dev, 0);
  103. if (IS_ERR(txx9wdt_reg)) {
  104. ret = PTR_ERR(txx9wdt_reg);
  105. goto exit;
  106. }
  107. if (timeout < 1 || timeout > WD_MAX_TIMEOUT)
  108. timeout = TIMER_MARGIN;
  109. txx9wdt.timeout = timeout;
  110. txx9wdt.min_timeout = 1;
  111. txx9wdt.max_timeout = WD_MAX_TIMEOUT;
  112. txx9wdt.parent = &dev->dev;
  113. watchdog_set_nowayout(&txx9wdt, nowayout);
  114. ret = watchdog_register_device(&txx9wdt);
  115. if (ret)
  116. goto exit;
  117. pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
  118. timeout, WD_MAX_TIMEOUT, nowayout);
  119. return 0;
  120. exit:
  121. if (txx9_imclk) {
  122. clk_disable_unprepare(txx9_imclk);
  123. clk_put(txx9_imclk);
  124. }
  125. return ret;
  126. }
  127. static int __exit txx9wdt_remove(struct platform_device *dev)
  128. {
  129. watchdog_unregister_device(&txx9wdt);
  130. clk_disable_unprepare(txx9_imclk);
  131. clk_put(txx9_imclk);
  132. return 0;
  133. }
  134. static void txx9wdt_shutdown(struct platform_device *dev)
  135. {
  136. txx9wdt_stop(&txx9wdt);
  137. }
  138. static struct platform_driver txx9wdt_driver = {
  139. .remove = __exit_p(txx9wdt_remove),
  140. .shutdown = txx9wdt_shutdown,
  141. .driver = {
  142. .name = "txx9wdt",
  143. },
  144. };
  145. module_platform_driver_probe(txx9wdt_driver, txx9wdt_probe);
  146. MODULE_DESCRIPTION("TXx9 Watchdog Driver");
  147. MODULE_LICENSE("GPL");
  148. MODULE_ALIAS("platform:txx9wdt");