stm32_iwdg.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for STM32 Independent Watchdog
  4. *
  5. * Copyright (C) STMicroelectronics 2017
  6. * Author: Yannick Fertre <yannick.fertre@st.com> for STMicroelectronics.
  7. *
  8. * This driver is based on tegra_wdt.c
  9. *
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/watchdog.h>
  22. /* IWDG registers */
  23. #define IWDG_KR 0x00 /* Key register */
  24. #define IWDG_PR 0x04 /* Prescaler Register */
  25. #define IWDG_RLR 0x08 /* ReLoad Register */
  26. #define IWDG_SR 0x0C /* Status Register */
  27. #define IWDG_WINR 0x10 /* Windows Register */
  28. /* IWDG_KR register bit mask */
  29. #define KR_KEY_RELOAD 0xAAAA /* reload counter enable */
  30. #define KR_KEY_ENABLE 0xCCCC /* peripheral enable */
  31. #define KR_KEY_EWA 0x5555 /* write access enable */
  32. #define KR_KEY_DWA 0x0000 /* write access disable */
  33. /* IWDG_PR register bit values */
  34. #define PR_4 0x00 /* prescaler set to 4 */
  35. #define PR_8 0x01 /* prescaler set to 8 */
  36. #define PR_16 0x02 /* prescaler set to 16 */
  37. #define PR_32 0x03 /* prescaler set to 32 */
  38. #define PR_64 0x04 /* prescaler set to 64 */
  39. #define PR_128 0x05 /* prescaler set to 128 */
  40. #define PR_256 0x06 /* prescaler set to 256 */
  41. /* IWDG_RLR register values */
  42. #define RLR_MIN 0x07C /* min value supported by reload register */
  43. #define RLR_MAX 0xFFF /* max value supported by reload register */
  44. /* IWDG_SR register bit mask */
  45. #define FLAG_PVU BIT(0) /* Watchdog prescaler value update */
  46. #define FLAG_RVU BIT(1) /* Watchdog counter reload value update */
  47. /* set timeout to 100000 us */
  48. #define TIMEOUT_US 100000
  49. #define SLEEP_US 1000
  50. #define HAS_PCLK true
  51. struct stm32_iwdg {
  52. struct watchdog_device wdd;
  53. void __iomem *regs;
  54. struct clk *clk_lsi;
  55. struct clk *clk_pclk;
  56. unsigned int rate;
  57. bool has_pclk;
  58. };
  59. static inline u32 reg_read(void __iomem *base, u32 reg)
  60. {
  61. return readl_relaxed(base + reg);
  62. }
  63. static inline void reg_write(void __iomem *base, u32 reg, u32 val)
  64. {
  65. writel_relaxed(val, base + reg);
  66. }
  67. static int stm32_iwdg_start(struct watchdog_device *wdd)
  68. {
  69. struct stm32_iwdg *wdt = watchdog_get_drvdata(wdd);
  70. u32 val = FLAG_PVU | FLAG_RVU;
  71. u32 reload;
  72. int ret;
  73. dev_dbg(wdd->parent, "%s\n", __func__);
  74. /* prescaler fixed to 256 */
  75. reload = clamp_t(unsigned int, ((wdd->timeout * wdt->rate) / 256) - 1,
  76. RLR_MIN, RLR_MAX);
  77. /* enable write access */
  78. reg_write(wdt->regs, IWDG_KR, KR_KEY_EWA);
  79. /* set prescaler & reload registers */
  80. reg_write(wdt->regs, IWDG_PR, PR_256); /* prescaler fix to 256 */
  81. reg_write(wdt->regs, IWDG_RLR, reload);
  82. reg_write(wdt->regs, IWDG_KR, KR_KEY_ENABLE);
  83. /* wait for the registers to be updated (max 100ms) */
  84. ret = readl_relaxed_poll_timeout(wdt->regs + IWDG_SR, val,
  85. !(val & (FLAG_PVU | FLAG_RVU)),
  86. SLEEP_US, TIMEOUT_US);
  87. if (ret) {
  88. dev_err(wdd->parent,
  89. "Fail to set prescaler or reload registers\n");
  90. return ret;
  91. }
  92. /* reload watchdog */
  93. reg_write(wdt->regs, IWDG_KR, KR_KEY_RELOAD);
  94. return 0;
  95. }
  96. static int stm32_iwdg_ping(struct watchdog_device *wdd)
  97. {
  98. struct stm32_iwdg *wdt = watchdog_get_drvdata(wdd);
  99. dev_dbg(wdd->parent, "%s\n", __func__);
  100. /* reload watchdog */
  101. reg_write(wdt->regs, IWDG_KR, KR_KEY_RELOAD);
  102. return 0;
  103. }
  104. static int stm32_iwdg_set_timeout(struct watchdog_device *wdd,
  105. unsigned int timeout)
  106. {
  107. dev_dbg(wdd->parent, "%s timeout: %d sec\n", __func__, timeout);
  108. wdd->timeout = timeout;
  109. if (watchdog_active(wdd))
  110. return stm32_iwdg_start(wdd);
  111. return 0;
  112. }
  113. static int stm32_iwdg_clk_init(struct platform_device *pdev,
  114. struct stm32_iwdg *wdt)
  115. {
  116. u32 ret;
  117. wdt->clk_lsi = devm_clk_get(&pdev->dev, "lsi");
  118. if (IS_ERR(wdt->clk_lsi)) {
  119. dev_err(&pdev->dev, "Unable to get lsi clock\n");
  120. return PTR_ERR(wdt->clk_lsi);
  121. }
  122. /* optional peripheral clock */
  123. if (wdt->has_pclk) {
  124. wdt->clk_pclk = devm_clk_get(&pdev->dev, "pclk");
  125. if (IS_ERR(wdt->clk_pclk)) {
  126. dev_err(&pdev->dev, "Unable to get pclk clock\n");
  127. return PTR_ERR(wdt->clk_pclk);
  128. }
  129. ret = clk_prepare_enable(wdt->clk_pclk);
  130. if (ret) {
  131. dev_err(&pdev->dev, "Unable to prepare pclk clock\n");
  132. return ret;
  133. }
  134. }
  135. ret = clk_prepare_enable(wdt->clk_lsi);
  136. if (ret) {
  137. dev_err(&pdev->dev, "Unable to prepare lsi clock\n");
  138. clk_disable_unprepare(wdt->clk_pclk);
  139. return ret;
  140. }
  141. wdt->rate = clk_get_rate(wdt->clk_lsi);
  142. return 0;
  143. }
  144. static const struct watchdog_info stm32_iwdg_info = {
  145. .options = WDIOF_SETTIMEOUT |
  146. WDIOF_MAGICCLOSE |
  147. WDIOF_KEEPALIVEPING,
  148. .identity = "STM32 Independent Watchdog",
  149. };
  150. static const struct watchdog_ops stm32_iwdg_ops = {
  151. .owner = THIS_MODULE,
  152. .start = stm32_iwdg_start,
  153. .ping = stm32_iwdg_ping,
  154. .set_timeout = stm32_iwdg_set_timeout,
  155. };
  156. static const struct of_device_id stm32_iwdg_of_match[] = {
  157. { .compatible = "st,stm32-iwdg", .data = (void *)!HAS_PCLK },
  158. { .compatible = "st,stm32mp1-iwdg", .data = (void *)HAS_PCLK },
  159. { /* end node */ }
  160. };
  161. MODULE_DEVICE_TABLE(of, stm32_iwdg_of_match);
  162. static int stm32_iwdg_probe(struct platform_device *pdev)
  163. {
  164. struct watchdog_device *wdd;
  165. const struct of_device_id *match;
  166. struct stm32_iwdg *wdt;
  167. struct resource *res;
  168. int ret;
  169. match = of_match_device(stm32_iwdg_of_match, &pdev->dev);
  170. if (!match)
  171. return -ENODEV;
  172. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  173. if (!wdt)
  174. return -ENOMEM;
  175. wdt->has_pclk = match->data;
  176. /* This is the timer base. */
  177. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  178. wdt->regs = devm_ioremap_resource(&pdev->dev, res);
  179. if (IS_ERR(wdt->regs)) {
  180. dev_err(&pdev->dev, "Could not get resource\n");
  181. return PTR_ERR(wdt->regs);
  182. }
  183. ret = stm32_iwdg_clk_init(pdev, wdt);
  184. if (ret)
  185. return ret;
  186. /* Initialize struct watchdog_device. */
  187. wdd = &wdt->wdd;
  188. wdd->info = &stm32_iwdg_info;
  189. wdd->ops = &stm32_iwdg_ops;
  190. wdd->min_timeout = ((RLR_MIN + 1) * 256) / wdt->rate;
  191. wdd->max_hw_heartbeat_ms = ((RLR_MAX + 1) * 256 * 1000) / wdt->rate;
  192. wdd->parent = &pdev->dev;
  193. watchdog_set_drvdata(wdd, wdt);
  194. watchdog_set_nowayout(wdd, WATCHDOG_NOWAYOUT);
  195. ret = watchdog_init_timeout(wdd, 0, &pdev->dev);
  196. if (ret)
  197. dev_warn(&pdev->dev,
  198. "unable to set timeout value, using default\n");
  199. ret = watchdog_register_device(wdd);
  200. if (ret) {
  201. dev_err(&pdev->dev, "failed to register watchdog device\n");
  202. goto err;
  203. }
  204. platform_set_drvdata(pdev, wdt);
  205. return 0;
  206. err:
  207. clk_disable_unprepare(wdt->clk_lsi);
  208. clk_disable_unprepare(wdt->clk_pclk);
  209. return ret;
  210. }
  211. static int stm32_iwdg_remove(struct platform_device *pdev)
  212. {
  213. struct stm32_iwdg *wdt = platform_get_drvdata(pdev);
  214. watchdog_unregister_device(&wdt->wdd);
  215. clk_disable_unprepare(wdt->clk_lsi);
  216. clk_disable_unprepare(wdt->clk_pclk);
  217. return 0;
  218. }
  219. static struct platform_driver stm32_iwdg_driver = {
  220. .probe = stm32_iwdg_probe,
  221. .remove = stm32_iwdg_remove,
  222. .driver = {
  223. .name = "iwdg",
  224. .of_match_table = of_match_ptr(stm32_iwdg_of_match),
  225. },
  226. };
  227. module_platform_driver(stm32_iwdg_driver);
  228. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  229. MODULE_DESCRIPTION("STMicroelectronics STM32 Independent Watchdog Driver");
  230. MODULE_LICENSE("GPL v2");