s32g_wdt.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Watchdog driver for S32G SoC
  4. *
  5. * Copyright 2017-2019, 2021-2025 NXP.
  6. *
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/watchdog.h>
  17. #define DRIVER_NAME "s32g-swt"
  18. #define S32G_SWT_CR(__base) ((__base) + 0x00) /* Control Register offset */
  19. #define S32G_SWT_CR_SM (BIT(9) | BIT(10)) /* -> Service Mode */
  20. #define S32G_SWT_CR_STP BIT(2) /* -> Stop Mode Control */
  21. #define S32G_SWT_CR_FRZ BIT(1) /* -> Debug Mode Control */
  22. #define S32G_SWT_CR_WEN BIT(0) /* -> Watchdog Enable */
  23. #define S32G_SWT_TO(__base) ((__base) + 0x08) /* Timeout Register offset */
  24. #define S32G_SWT_SR(__base) ((__base) + 0x10) /* Service Register offset */
  25. #define S32G_WDT_SEQ1 0xA602 /* -> service sequence number 1 */
  26. #define S32G_WDT_SEQ2 0xB480 /* -> service sequence number 2 */
  27. #define S32G_SWT_CO(__base) ((__base) + 0x14) /* Counter output register */
  28. #define S32G_WDT_DEFAULT_TIMEOUT 30
  29. struct s32g_wdt_device {
  30. int rate;
  31. void __iomem *base;
  32. struct watchdog_device wdog;
  33. };
  34. static bool nowayout = WATCHDOG_NOWAYOUT;
  35. module_param(nowayout, bool, 0);
  36. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  37. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  38. static unsigned int timeout_param = S32G_WDT_DEFAULT_TIMEOUT;
  39. module_param(timeout_param, uint, 0);
  40. MODULE_PARM_DESC(timeout_param, "Watchdog timeout in seconds (default="
  41. __MODULE_STRING(S32G_WDT_DEFAULT_TIMEOUT) ")");
  42. static bool early_enable;
  43. module_param(early_enable, bool, 0);
  44. MODULE_PARM_DESC(early_enable,
  45. "Watchdog is started on module insertion (default=false)");
  46. static const struct watchdog_info s32g_wdt_info = {
  47. .identity = "s32g watchdog",
  48. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  49. WDIOC_GETTIMEOUT | WDIOC_GETTIMELEFT,
  50. };
  51. static struct s32g_wdt_device *wdd_to_s32g_wdt(struct watchdog_device *wdd)
  52. {
  53. return container_of(wdd, struct s32g_wdt_device, wdog);
  54. }
  55. static unsigned int wdog_sec_to_count(struct s32g_wdt_device *wdev, unsigned int timeout)
  56. {
  57. return wdev->rate * timeout;
  58. }
  59. static int s32g_wdt_ping(struct watchdog_device *wdog)
  60. {
  61. struct s32g_wdt_device *wdev = wdd_to_s32g_wdt(wdog);
  62. writel(S32G_WDT_SEQ1, S32G_SWT_SR(wdev->base));
  63. writel(S32G_WDT_SEQ2, S32G_SWT_SR(wdev->base));
  64. return 0;
  65. }
  66. static int s32g_wdt_start(struct watchdog_device *wdog)
  67. {
  68. struct s32g_wdt_device *wdev = wdd_to_s32g_wdt(wdog);
  69. unsigned long val;
  70. val = readl(S32G_SWT_CR(wdev->base));
  71. val |= S32G_SWT_CR_WEN;
  72. writel(val, S32G_SWT_CR(wdev->base));
  73. return 0;
  74. }
  75. static int s32g_wdt_stop(struct watchdog_device *wdog)
  76. {
  77. struct s32g_wdt_device *wdev = wdd_to_s32g_wdt(wdog);
  78. unsigned long val;
  79. val = readl(S32G_SWT_CR(wdev->base));
  80. val &= ~S32G_SWT_CR_WEN;
  81. writel(val, S32G_SWT_CR(wdev->base));
  82. return 0;
  83. }
  84. static int s32g_wdt_set_timeout(struct watchdog_device *wdog, unsigned int timeout)
  85. {
  86. struct s32g_wdt_device *wdev = wdd_to_s32g_wdt(wdog);
  87. writel(wdog_sec_to_count(wdev, timeout), S32G_SWT_TO(wdev->base));
  88. wdog->timeout = timeout;
  89. /*
  90. * Conforming to the documentation, the timeout counter is
  91. * loaded when servicing is operated (aka ping) or when the
  92. * counter is enabled. In case the watchdog is already started
  93. * it must be stopped and started again to update the timeout
  94. * register or a ping can be sent to refresh the counter. Here
  95. * we choose to send a ping to the watchdog which is harmless
  96. * if the watchdog is stopped.
  97. */
  98. return s32g_wdt_ping(wdog);
  99. }
  100. static unsigned int s32g_wdt_get_timeleft(struct watchdog_device *wdog)
  101. {
  102. struct s32g_wdt_device *wdev = wdd_to_s32g_wdt(wdog);
  103. unsigned long counter;
  104. bool is_running;
  105. /*
  106. * The counter output can be read only if the SWT is
  107. * disabled. Given the latency between the internal counter
  108. * and the counter output update, there can be very small
  109. * difference. However, we can accept this matter of fact
  110. * given the resolution is a second based unit for the output.
  111. */
  112. is_running = watchdog_hw_running(wdog);
  113. if (is_running)
  114. s32g_wdt_stop(wdog);
  115. counter = readl(S32G_SWT_CO(wdev->base));
  116. if (is_running)
  117. s32g_wdt_start(wdog);
  118. return counter / wdev->rate;
  119. }
  120. static const struct watchdog_ops s32g_wdt_ops = {
  121. .owner = THIS_MODULE,
  122. .start = s32g_wdt_start,
  123. .stop = s32g_wdt_stop,
  124. .ping = s32g_wdt_ping,
  125. .set_timeout = s32g_wdt_set_timeout,
  126. .get_timeleft = s32g_wdt_get_timeleft,
  127. };
  128. static void s32g_wdt_init(struct s32g_wdt_device *wdev)
  129. {
  130. unsigned long val;
  131. /* Set the watchdog's Time-Out value */
  132. val = wdog_sec_to_count(wdev, wdev->wdog.timeout);
  133. writel(val, S32G_SWT_TO(wdev->base));
  134. /*
  135. * Get the control register content. We are at init time, the
  136. * watchdog should not be started.
  137. */
  138. val = readl(S32G_SWT_CR(wdev->base));
  139. /*
  140. * We want to allow the watchdog timer to be stopped when
  141. * device enters debug mode.
  142. */
  143. val |= S32G_SWT_CR_FRZ;
  144. /*
  145. * However, when the CPU is in WFI or suspend mode, the
  146. * watchdog must continue. The documentation refers it as the
  147. * stopped mode.
  148. */
  149. val &= ~S32G_SWT_CR_STP;
  150. /*
  151. * Use Fixed Service Sequence to ping the watchdog which is
  152. * 0x00 configuration value for the service mode. It should be
  153. * already set because it is the default value but we reset it
  154. * in case.
  155. */
  156. val &= ~S32G_SWT_CR_SM;
  157. writel(val, S32G_SWT_CR(wdev->base));
  158. /*
  159. * When the 'early_enable' option is set, we start the
  160. * watchdog from the kernel.
  161. */
  162. if (early_enable) {
  163. s32g_wdt_start(&wdev->wdog);
  164. set_bit(WDOG_HW_RUNNING, &wdev->wdog.status);
  165. }
  166. }
  167. static int s32g_wdt_probe(struct platform_device *pdev)
  168. {
  169. struct device *dev = &pdev->dev;
  170. struct resource *res;
  171. struct clk *clk;
  172. struct s32g_wdt_device *wdev;
  173. struct watchdog_device *wdog;
  174. int ret;
  175. wdev = devm_kzalloc(dev, sizeof(*wdev), GFP_KERNEL);
  176. if (!wdev)
  177. return -ENOMEM;
  178. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  179. wdev->base = devm_ioremap_resource(dev, res);
  180. if (IS_ERR(wdev->base))
  181. return dev_err_probe(&pdev->dev, PTR_ERR(wdev->base), "Can not get resource\n");
  182. clk = devm_clk_get_enabled(dev, "counter");
  183. if (IS_ERR(clk))
  184. return dev_err_probe(dev, PTR_ERR(clk), "Can't get Watchdog clock\n");
  185. wdev->rate = clk_get_rate(clk);
  186. if (!wdev->rate) {
  187. dev_err(dev, "Input clock rate is not valid\n");
  188. return -EINVAL;
  189. }
  190. wdog = &wdev->wdog;
  191. wdog->info = &s32g_wdt_info;
  192. wdog->ops = &s32g_wdt_ops;
  193. /*
  194. * The code converts the timeout into a counter a value, if
  195. * the value is less than 0x100, then it is clamped by the SWT
  196. * module, so it is safe to specify a zero value as the
  197. * minimum timeout.
  198. */
  199. wdog->min_timeout = 0;
  200. /*
  201. * The counter register is a 32 bits long, so the maximum
  202. * counter value is UINT_MAX and the timeout in second is the
  203. * value divided by the rate.
  204. *
  205. * For instance, a rate of 51MHz lead to 84 seconds maximum
  206. * timeout.
  207. */
  208. wdog->max_timeout = UINT_MAX / wdev->rate;
  209. /*
  210. * The module param and the DT 'timeout-sec' property will
  211. * override the default value if they are specified.
  212. */
  213. ret = watchdog_init_timeout(wdog, timeout_param, dev);
  214. if (ret)
  215. return ret;
  216. /*
  217. * As soon as the watchdog is started, there is no way to stop
  218. * it if the 'nowayout' option is set at boot time
  219. */
  220. watchdog_set_nowayout(wdog, nowayout);
  221. /*
  222. * The devm_ version of the watchdog_register_device()
  223. * function will call watchdog_unregister_device() when the
  224. * device is removed.
  225. */
  226. watchdog_stop_on_unregister(wdog);
  227. s32g_wdt_init(wdev);
  228. ret = devm_watchdog_register_device(dev, wdog);
  229. if (ret)
  230. return dev_err_probe(dev, ret, "Cannot register watchdog device\n");
  231. dev_info(dev, "S32G Watchdog Timer Registered, timeout=%ds, nowayout=%d, early_enable=%d\n",
  232. wdog->timeout, nowayout, early_enable);
  233. return 0;
  234. }
  235. static const struct of_device_id s32g_wdt_dt_ids[] = {
  236. { .compatible = "nxp,s32g2-swt" },
  237. { /* sentinel */ }
  238. };
  239. MODULE_DEVICE_TABLE(of, s32g_wdt_dt_ids);
  240. static struct platform_driver s32g_wdt_driver = {
  241. .probe = s32g_wdt_probe,
  242. .driver = {
  243. .name = DRIVER_NAME,
  244. .of_match_table = s32g_wdt_dt_ids,
  245. },
  246. };
  247. module_platform_driver(s32g_wdt_driver);
  248. MODULE_AUTHOR("Daniel Lezcano <daniel.lezcano@linaro.org>");
  249. MODULE_DESCRIPTION("Watchdog driver for S32G SoC");
  250. MODULE_LICENSE("GPL");