atlas7_wdt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Watchdog driver for CSR Atlas7
  4. *
  5. * Copyright (c) 2015 Cambridge Silicon Radio Limited, a CSR plc group company.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/watchdog.h>
  14. #define ATLAS7_TIMER_WDT_INDEX 5
  15. #define ATLAS7_WDT_DEFAULT_TIMEOUT 20
  16. #define ATLAS7_WDT_CNT_CTRL (0 + 4 * ATLAS7_TIMER_WDT_INDEX)
  17. #define ATLAS7_WDT_CNT_MATCH (0x18 + 4 * ATLAS7_TIMER_WDT_INDEX)
  18. #define ATLAS7_WDT_CNT (0x48 + 4 * ATLAS7_TIMER_WDT_INDEX)
  19. #define ATLAS7_WDT_CNT_EN (BIT(0) | BIT(1))
  20. #define ATLAS7_WDT_EN 0x64
  21. static unsigned int timeout = ATLAS7_WDT_DEFAULT_TIMEOUT;
  22. static bool nowayout = WATCHDOG_NOWAYOUT;
  23. module_param(timeout, uint, 0);
  24. module_param(nowayout, bool, 0);
  25. MODULE_PARM_DESC(timeout, "Default watchdog timeout (in seconds)");
  26. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  27. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  28. struct atlas7_wdog {
  29. struct device *dev;
  30. void __iomem *base;
  31. unsigned long tick_rate;
  32. struct clk *clk;
  33. };
  34. static unsigned int atlas7_wdt_gettimeleft(struct watchdog_device *wdd)
  35. {
  36. struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
  37. u32 counter, match, delta;
  38. counter = readl(wdt->base + ATLAS7_WDT_CNT);
  39. match = readl(wdt->base + ATLAS7_WDT_CNT_MATCH);
  40. delta = match - counter;
  41. return delta / wdt->tick_rate;
  42. }
  43. static int atlas7_wdt_ping(struct watchdog_device *wdd)
  44. {
  45. struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
  46. u32 counter, match, delta;
  47. counter = readl(wdt->base + ATLAS7_WDT_CNT);
  48. delta = wdd->timeout * wdt->tick_rate;
  49. match = counter + delta;
  50. writel(match, wdt->base + ATLAS7_WDT_CNT_MATCH);
  51. return 0;
  52. }
  53. static int atlas7_wdt_enable(struct watchdog_device *wdd)
  54. {
  55. struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
  56. atlas7_wdt_ping(wdd);
  57. writel(readl(wdt->base + ATLAS7_WDT_CNT_CTRL) | ATLAS7_WDT_CNT_EN,
  58. wdt->base + ATLAS7_WDT_CNT_CTRL);
  59. writel(1, wdt->base + ATLAS7_WDT_EN);
  60. return 0;
  61. }
  62. static int atlas7_wdt_disable(struct watchdog_device *wdd)
  63. {
  64. struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
  65. writel(0, wdt->base + ATLAS7_WDT_EN);
  66. writel(readl(wdt->base + ATLAS7_WDT_CNT_CTRL) & ~ATLAS7_WDT_CNT_EN,
  67. wdt->base + ATLAS7_WDT_CNT_CTRL);
  68. return 0;
  69. }
  70. static int atlas7_wdt_settimeout(struct watchdog_device *wdd, unsigned int to)
  71. {
  72. wdd->timeout = to;
  73. return 0;
  74. }
  75. #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
  76. static const struct watchdog_info atlas7_wdt_ident = {
  77. .options = OPTIONS,
  78. .firmware_version = 0,
  79. .identity = "atlas7 Watchdog",
  80. };
  81. static const struct watchdog_ops atlas7_wdt_ops = {
  82. .owner = THIS_MODULE,
  83. .start = atlas7_wdt_enable,
  84. .stop = atlas7_wdt_disable,
  85. .get_timeleft = atlas7_wdt_gettimeleft,
  86. .ping = atlas7_wdt_ping,
  87. .set_timeout = atlas7_wdt_settimeout,
  88. };
  89. static struct watchdog_device atlas7_wdd = {
  90. .info = &atlas7_wdt_ident,
  91. .ops = &atlas7_wdt_ops,
  92. .timeout = ATLAS7_WDT_DEFAULT_TIMEOUT,
  93. };
  94. static const struct of_device_id atlas7_wdt_ids[] = {
  95. { .compatible = "sirf,atlas7-tick"},
  96. {}
  97. };
  98. static void atlas7_clk_disable_unprepare(void *data)
  99. {
  100. clk_disable_unprepare(data);
  101. }
  102. static int atlas7_wdt_probe(struct platform_device *pdev)
  103. {
  104. struct device *dev = &pdev->dev;
  105. struct atlas7_wdog *wdt;
  106. struct clk *clk;
  107. int ret;
  108. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  109. if (!wdt)
  110. return -ENOMEM;
  111. wdt->base = devm_platform_ioremap_resource(pdev, 0);
  112. if (IS_ERR(wdt->base))
  113. return PTR_ERR(wdt->base);
  114. clk = devm_clk_get(dev, NULL);
  115. if (IS_ERR(clk))
  116. return PTR_ERR(clk);
  117. ret = clk_prepare_enable(clk);
  118. if (ret) {
  119. dev_err(dev, "clk enable failed\n");
  120. return ret;
  121. }
  122. ret = devm_add_action_or_reset(dev, atlas7_clk_disable_unprepare, clk);
  123. if (ret)
  124. return ret;
  125. /* disable watchdog hardware */
  126. writel(0, wdt->base + ATLAS7_WDT_CNT_CTRL);
  127. wdt->tick_rate = clk_get_rate(clk);
  128. if (!wdt->tick_rate)
  129. return -EINVAL;
  130. wdt->clk = clk;
  131. atlas7_wdd.min_timeout = 1;
  132. atlas7_wdd.max_timeout = UINT_MAX / wdt->tick_rate;
  133. watchdog_init_timeout(&atlas7_wdd, 0, dev);
  134. watchdog_set_nowayout(&atlas7_wdd, nowayout);
  135. watchdog_set_drvdata(&atlas7_wdd, wdt);
  136. platform_set_drvdata(pdev, &atlas7_wdd);
  137. watchdog_stop_on_reboot(&atlas7_wdd);
  138. watchdog_stop_on_unregister(&atlas7_wdd);
  139. return devm_watchdog_register_device(dev, &atlas7_wdd);
  140. }
  141. static int __maybe_unused atlas7_wdt_suspend(struct device *dev)
  142. {
  143. /*
  144. * NOTE:timer controller registers settings are saved
  145. * and restored back by the timer-atlas7.c
  146. */
  147. return 0;
  148. }
  149. static int __maybe_unused atlas7_wdt_resume(struct device *dev)
  150. {
  151. struct watchdog_device *wdd = dev_get_drvdata(dev);
  152. /*
  153. * NOTE: Since timer controller registers settings are saved
  154. * and restored back by the timer-atlas7.c, so we need not
  155. * update WD settings except refreshing timeout.
  156. */
  157. atlas7_wdt_ping(wdd);
  158. return 0;
  159. }
  160. static SIMPLE_DEV_PM_OPS(atlas7_wdt_pm_ops,
  161. atlas7_wdt_suspend, atlas7_wdt_resume);
  162. MODULE_DEVICE_TABLE(of, atlas7_wdt_ids);
  163. static struct platform_driver atlas7_wdt_driver = {
  164. .driver = {
  165. .name = "atlas7-wdt",
  166. .pm = &atlas7_wdt_pm_ops,
  167. .of_match_table = atlas7_wdt_ids,
  168. },
  169. .probe = atlas7_wdt_probe,
  170. };
  171. module_platform_driver(atlas7_wdt_driver);
  172. MODULE_DESCRIPTION("CSRatlas7 watchdog driver");
  173. MODULE_AUTHOR("Guo Zeng <Guo.Zeng@csr.com>");
  174. MODULE_LICENSE("GPL v2");
  175. MODULE_ALIAS("platform:atlas7-wdt");