imx7ulp_wdt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 NXP.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/init.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/reboot.h>
  13. #include <linux/watchdog.h>
  14. #define WDOG_CS 0x0
  15. #define WDOG_CS_CMD32EN BIT(13)
  16. #define WDOG_CS_ULK BIT(11)
  17. #define WDOG_CS_RCS BIT(10)
  18. #define WDOG_CS_EN BIT(7)
  19. #define WDOG_CS_UPDATE BIT(5)
  20. #define WDOG_CNT 0x4
  21. #define WDOG_TOVAL 0x8
  22. #define REFRESH_SEQ0 0xA602
  23. #define REFRESH_SEQ1 0xB480
  24. #define REFRESH ((REFRESH_SEQ1 << 16) | REFRESH_SEQ0)
  25. #define UNLOCK_SEQ0 0xC520
  26. #define UNLOCK_SEQ1 0xD928
  27. #define UNLOCK ((UNLOCK_SEQ1 << 16) | UNLOCK_SEQ0)
  28. #define DEFAULT_TIMEOUT 60
  29. #define MAX_TIMEOUT 128
  30. #define WDOG_CLOCK_RATE 1000
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(nowayout, bool, 0000);
  33. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  34. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  35. struct imx7ulp_wdt_device {
  36. struct notifier_block restart_handler;
  37. struct watchdog_device wdd;
  38. void __iomem *base;
  39. struct clk *clk;
  40. };
  41. static inline void imx7ulp_wdt_enable(void __iomem *base, bool enable)
  42. {
  43. u32 val = readl(base + WDOG_CS);
  44. writel(UNLOCK, base + WDOG_CNT);
  45. if (enable)
  46. writel(val | WDOG_CS_EN, base + WDOG_CS);
  47. else
  48. writel(val & ~WDOG_CS_EN, base + WDOG_CS);
  49. }
  50. static inline bool imx7ulp_wdt_is_enabled(void __iomem *base)
  51. {
  52. u32 val = readl(base + WDOG_CS);
  53. return val & WDOG_CS_EN;
  54. }
  55. static int imx7ulp_wdt_ping(struct watchdog_device *wdog)
  56. {
  57. struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
  58. writel(REFRESH, wdt->base + WDOG_CNT);
  59. return 0;
  60. }
  61. static int imx7ulp_wdt_start(struct watchdog_device *wdog)
  62. {
  63. struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
  64. imx7ulp_wdt_enable(wdt->base, true);
  65. return 0;
  66. }
  67. static int imx7ulp_wdt_stop(struct watchdog_device *wdog)
  68. {
  69. struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
  70. imx7ulp_wdt_enable(wdt->base, false);
  71. return 0;
  72. }
  73. static int imx7ulp_wdt_set_timeout(struct watchdog_device *wdog,
  74. unsigned int timeout)
  75. {
  76. struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
  77. u32 val = WDOG_CLOCK_RATE * timeout;
  78. writel(UNLOCK, wdt->base + WDOG_CNT);
  79. writel(val, wdt->base + WDOG_TOVAL);
  80. wdog->timeout = timeout;
  81. return 0;
  82. }
  83. static int imx7ulp_wdt_restart(struct watchdog_device *wdog,
  84. unsigned long action, void *data)
  85. {
  86. struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
  87. imx7ulp_wdt_enable(wdt->base, true);
  88. imx7ulp_wdt_set_timeout(&wdt->wdd, 1);
  89. /* wait for wdog to fire */
  90. while (true)
  91. ;
  92. return NOTIFY_DONE;
  93. }
  94. static const struct watchdog_ops imx7ulp_wdt_ops = {
  95. .owner = THIS_MODULE,
  96. .start = imx7ulp_wdt_start,
  97. .stop = imx7ulp_wdt_stop,
  98. .ping = imx7ulp_wdt_ping,
  99. .set_timeout = imx7ulp_wdt_set_timeout,
  100. .restart = imx7ulp_wdt_restart,
  101. };
  102. static const struct watchdog_info imx7ulp_wdt_info = {
  103. .identity = "i.MX7ULP watchdog timer",
  104. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  105. WDIOF_MAGICCLOSE,
  106. };
  107. static inline void imx7ulp_wdt_init(void __iomem *base, unsigned int timeout)
  108. {
  109. u32 val;
  110. /* unlock the wdog for reconfiguration */
  111. writel_relaxed(UNLOCK_SEQ0, base + WDOG_CNT);
  112. writel_relaxed(UNLOCK_SEQ1, base + WDOG_CNT);
  113. /* set an initial timeout value in TOVAL */
  114. writel(timeout, base + WDOG_TOVAL);
  115. /* enable 32bit command sequence and reconfigure */
  116. val = BIT(13) | BIT(8) | BIT(5);
  117. writel(val, base + WDOG_CS);
  118. }
  119. static void imx7ulp_wdt_action(void *data)
  120. {
  121. clk_disable_unprepare(data);
  122. }
  123. static int imx7ulp_wdt_probe(struct platform_device *pdev)
  124. {
  125. struct imx7ulp_wdt_device *imx7ulp_wdt;
  126. struct device *dev = &pdev->dev;
  127. struct watchdog_device *wdog;
  128. int ret;
  129. imx7ulp_wdt = devm_kzalloc(dev, sizeof(*imx7ulp_wdt), GFP_KERNEL);
  130. if (!imx7ulp_wdt)
  131. return -ENOMEM;
  132. platform_set_drvdata(pdev, imx7ulp_wdt);
  133. imx7ulp_wdt->base = devm_platform_ioremap_resource(pdev, 0);
  134. if (IS_ERR(imx7ulp_wdt->base))
  135. return PTR_ERR(imx7ulp_wdt->base);
  136. imx7ulp_wdt->clk = devm_clk_get(dev, NULL);
  137. if (IS_ERR(imx7ulp_wdt->clk)) {
  138. dev_err(dev, "Failed to get watchdog clock\n");
  139. return PTR_ERR(imx7ulp_wdt->clk);
  140. }
  141. ret = clk_prepare_enable(imx7ulp_wdt->clk);
  142. if (ret)
  143. return ret;
  144. ret = devm_add_action_or_reset(dev, imx7ulp_wdt_action, imx7ulp_wdt->clk);
  145. if (ret)
  146. return ret;
  147. wdog = &imx7ulp_wdt->wdd;
  148. wdog->info = &imx7ulp_wdt_info;
  149. wdog->ops = &imx7ulp_wdt_ops;
  150. wdog->min_timeout = 1;
  151. wdog->max_timeout = MAX_TIMEOUT;
  152. wdog->parent = dev;
  153. wdog->timeout = DEFAULT_TIMEOUT;
  154. watchdog_init_timeout(wdog, 0, dev);
  155. watchdog_stop_on_reboot(wdog);
  156. watchdog_stop_on_unregister(wdog);
  157. watchdog_set_drvdata(wdog, imx7ulp_wdt);
  158. imx7ulp_wdt_init(imx7ulp_wdt->base, wdog->timeout * WDOG_CLOCK_RATE);
  159. return devm_watchdog_register_device(dev, wdog);
  160. }
  161. static int __maybe_unused imx7ulp_wdt_suspend(struct device *dev)
  162. {
  163. struct imx7ulp_wdt_device *imx7ulp_wdt = dev_get_drvdata(dev);
  164. if (watchdog_active(&imx7ulp_wdt->wdd))
  165. imx7ulp_wdt_stop(&imx7ulp_wdt->wdd);
  166. clk_disable_unprepare(imx7ulp_wdt->clk);
  167. return 0;
  168. }
  169. static int __maybe_unused imx7ulp_wdt_resume(struct device *dev)
  170. {
  171. struct imx7ulp_wdt_device *imx7ulp_wdt = dev_get_drvdata(dev);
  172. u32 timeout = imx7ulp_wdt->wdd.timeout * WDOG_CLOCK_RATE;
  173. int ret;
  174. ret = clk_prepare_enable(imx7ulp_wdt->clk);
  175. if (ret)
  176. return ret;
  177. if (imx7ulp_wdt_is_enabled(imx7ulp_wdt->base))
  178. imx7ulp_wdt_init(imx7ulp_wdt->base, timeout);
  179. if (watchdog_active(&imx7ulp_wdt->wdd))
  180. imx7ulp_wdt_start(&imx7ulp_wdt->wdd);
  181. return 0;
  182. }
  183. static SIMPLE_DEV_PM_OPS(imx7ulp_wdt_pm_ops, imx7ulp_wdt_suspend,
  184. imx7ulp_wdt_resume);
  185. static const struct of_device_id imx7ulp_wdt_dt_ids[] = {
  186. { .compatible = "fsl,imx7ulp-wdt", },
  187. { /* sentinel */ }
  188. };
  189. MODULE_DEVICE_TABLE(of, imx7ulp_wdt_dt_ids);
  190. static struct platform_driver imx7ulp_wdt_driver = {
  191. .probe = imx7ulp_wdt_probe,
  192. .driver = {
  193. .name = "imx7ulp-wdt",
  194. .pm = &imx7ulp_wdt_pm_ops,
  195. .of_match_table = imx7ulp_wdt_dt_ids,
  196. },
  197. };
  198. module_platform_driver(imx7ulp_wdt_driver);
  199. MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
  200. MODULE_DESCRIPTION("Freescale i.MX7ULP watchdog driver");
  201. MODULE_LICENSE("GPL v2");