st_lpc_wdt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ST's LPC Watchdog
  4. *
  5. * Copyright (C) 2014 STMicroelectronics -- All Rights Reserved
  6. *
  7. * Author: David Paris <david.paris@st.com> for STMicroelectronics
  8. * Lee Jones <lee.jones@linaro.org> for STMicroelectronics
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/init.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/watchdog.h>
  21. #include <dt-bindings/mfd/st-lpc.h>
  22. /* Low Power Alarm */
  23. #define LPC_LPA_LSB_OFF 0x410
  24. #define LPC_LPA_START_OFF 0x418
  25. /* LPC as WDT */
  26. #define LPC_WDT_OFF 0x510
  27. static struct watchdog_device st_wdog_dev;
  28. struct st_wdog_syscfg {
  29. unsigned int reset_type_reg;
  30. unsigned int reset_type_mask;
  31. unsigned int enable_reg;
  32. unsigned int enable_mask;
  33. };
  34. struct st_wdog {
  35. void __iomem *base;
  36. struct device *dev;
  37. struct regmap *regmap;
  38. struct st_wdog_syscfg *syscfg;
  39. struct clk *clk;
  40. unsigned long clkrate;
  41. bool warm_reset;
  42. };
  43. static struct st_wdog_syscfg stih407_syscfg = {
  44. .enable_reg = 0x204,
  45. .enable_mask = BIT(19),
  46. };
  47. static const struct of_device_id st_wdog_match[] = {
  48. {
  49. .compatible = "st,stih407-lpc",
  50. .data = &stih407_syscfg,
  51. },
  52. {},
  53. };
  54. MODULE_DEVICE_TABLE(of, st_wdog_match);
  55. static void st_wdog_setup(struct st_wdog *st_wdog, bool enable)
  56. {
  57. /* Type of watchdog reset - 0: Cold 1: Warm */
  58. if (st_wdog->syscfg->reset_type_reg)
  59. regmap_update_bits(st_wdog->regmap,
  60. st_wdog->syscfg->reset_type_reg,
  61. st_wdog->syscfg->reset_type_mask,
  62. st_wdog->warm_reset);
  63. /* Mask/unmask watchdog reset */
  64. regmap_update_bits(st_wdog->regmap,
  65. st_wdog->syscfg->enable_reg,
  66. st_wdog->syscfg->enable_mask,
  67. enable ? 0 : st_wdog->syscfg->enable_mask);
  68. }
  69. static void st_wdog_load_timer(struct st_wdog *st_wdog, unsigned int timeout)
  70. {
  71. unsigned long clkrate = st_wdog->clkrate;
  72. writel_relaxed(timeout * clkrate, st_wdog->base + LPC_LPA_LSB_OFF);
  73. writel_relaxed(1, st_wdog->base + LPC_LPA_START_OFF);
  74. }
  75. static int st_wdog_start(struct watchdog_device *wdd)
  76. {
  77. struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
  78. writel_relaxed(1, st_wdog->base + LPC_WDT_OFF);
  79. return 0;
  80. }
  81. static int st_wdog_stop(struct watchdog_device *wdd)
  82. {
  83. struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
  84. writel_relaxed(0, st_wdog->base + LPC_WDT_OFF);
  85. return 0;
  86. }
  87. static int st_wdog_set_timeout(struct watchdog_device *wdd,
  88. unsigned int timeout)
  89. {
  90. struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
  91. wdd->timeout = timeout;
  92. st_wdog_load_timer(st_wdog, timeout);
  93. return 0;
  94. }
  95. static int st_wdog_keepalive(struct watchdog_device *wdd)
  96. {
  97. struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
  98. st_wdog_load_timer(st_wdog, wdd->timeout);
  99. return 0;
  100. }
  101. static const struct watchdog_info st_wdog_info = {
  102. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  103. .identity = "ST LPC WDT",
  104. };
  105. static const struct watchdog_ops st_wdog_ops = {
  106. .owner = THIS_MODULE,
  107. .start = st_wdog_start,
  108. .stop = st_wdog_stop,
  109. .ping = st_wdog_keepalive,
  110. .set_timeout = st_wdog_set_timeout,
  111. };
  112. static struct watchdog_device st_wdog_dev = {
  113. .info = &st_wdog_info,
  114. .ops = &st_wdog_ops,
  115. };
  116. static void st_clk_disable_unprepare(void *data)
  117. {
  118. clk_disable_unprepare(data);
  119. }
  120. static int st_wdog_probe(struct platform_device *pdev)
  121. {
  122. struct device *dev = &pdev->dev;
  123. const struct of_device_id *match;
  124. struct device_node *np = dev->of_node;
  125. struct st_wdog *st_wdog;
  126. struct regmap *regmap;
  127. struct clk *clk;
  128. void __iomem *base;
  129. uint32_t mode;
  130. int ret;
  131. ret = of_property_read_u32(np, "st,lpc-mode", &mode);
  132. if (ret) {
  133. dev_err(dev, "An LPC mode must be provided\n");
  134. return -EINVAL;
  135. }
  136. /* LPC can either run as a Clocksource or in RTC or WDT mode */
  137. if (mode != ST_LPC_MODE_WDT)
  138. return -ENODEV;
  139. st_wdog = devm_kzalloc(dev, sizeof(*st_wdog), GFP_KERNEL);
  140. if (!st_wdog)
  141. return -ENOMEM;
  142. match = of_match_device(st_wdog_match, dev);
  143. if (!match) {
  144. dev_err(dev, "Couldn't match device\n");
  145. return -ENODEV;
  146. }
  147. st_wdog->syscfg = (struct st_wdog_syscfg *)match->data;
  148. base = devm_platform_ioremap_resource(pdev, 0);
  149. if (IS_ERR(base))
  150. return PTR_ERR(base);
  151. regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  152. if (IS_ERR(regmap)) {
  153. dev_err(dev, "No syscfg phandle specified\n");
  154. return PTR_ERR(regmap);
  155. }
  156. clk = devm_clk_get(dev, NULL);
  157. if (IS_ERR(clk)) {
  158. dev_err(dev, "Unable to request clock\n");
  159. return PTR_ERR(clk);
  160. }
  161. st_wdog->dev = dev;
  162. st_wdog->base = base;
  163. st_wdog->clk = clk;
  164. st_wdog->regmap = regmap;
  165. st_wdog->warm_reset = of_property_read_bool(np, "st,warm_reset");
  166. st_wdog->clkrate = clk_get_rate(st_wdog->clk);
  167. if (!st_wdog->clkrate) {
  168. dev_err(dev, "Unable to fetch clock rate\n");
  169. return -EINVAL;
  170. }
  171. st_wdog_dev.max_timeout = 0xFFFFFFFF / st_wdog->clkrate;
  172. st_wdog_dev.parent = dev;
  173. ret = clk_prepare_enable(clk);
  174. if (ret) {
  175. dev_err(dev, "Unable to enable clock\n");
  176. return ret;
  177. }
  178. ret = devm_add_action_or_reset(dev, st_clk_disable_unprepare, clk);
  179. if (ret)
  180. return ret;
  181. watchdog_set_drvdata(&st_wdog_dev, st_wdog);
  182. watchdog_set_nowayout(&st_wdog_dev, WATCHDOG_NOWAYOUT);
  183. /* Init Watchdog timeout with value in DT */
  184. ret = watchdog_init_timeout(&st_wdog_dev, 0, dev);
  185. if (ret)
  186. return ret;
  187. ret = devm_watchdog_register_device(dev, &st_wdog_dev);
  188. if (ret)
  189. return ret;
  190. st_wdog_setup(st_wdog, true);
  191. dev_info(dev, "LPC Watchdog driver registered, reset type is %s",
  192. st_wdog->warm_reset ? "warm" : "cold");
  193. return ret;
  194. }
  195. static int st_wdog_remove(struct platform_device *pdev)
  196. {
  197. struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
  198. st_wdog_setup(st_wdog, false);
  199. return 0;
  200. }
  201. #ifdef CONFIG_PM_SLEEP
  202. static int st_wdog_suspend(struct device *dev)
  203. {
  204. struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
  205. if (watchdog_active(&st_wdog_dev))
  206. st_wdog_stop(&st_wdog_dev);
  207. st_wdog_setup(st_wdog, false);
  208. clk_disable(st_wdog->clk);
  209. return 0;
  210. }
  211. static int st_wdog_resume(struct device *dev)
  212. {
  213. struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
  214. int ret;
  215. ret = clk_enable(st_wdog->clk);
  216. if (ret) {
  217. dev_err(dev, "Unable to re-enable clock\n");
  218. watchdog_unregister_device(&st_wdog_dev);
  219. clk_unprepare(st_wdog->clk);
  220. return ret;
  221. }
  222. st_wdog_setup(st_wdog, true);
  223. if (watchdog_active(&st_wdog_dev)) {
  224. st_wdog_load_timer(st_wdog, st_wdog_dev.timeout);
  225. st_wdog_start(&st_wdog_dev);
  226. }
  227. return 0;
  228. }
  229. #endif
  230. static SIMPLE_DEV_PM_OPS(st_wdog_pm_ops,
  231. st_wdog_suspend,
  232. st_wdog_resume);
  233. static struct platform_driver st_wdog_driver = {
  234. .driver = {
  235. .name = "st-lpc-wdt",
  236. .pm = &st_wdog_pm_ops,
  237. .of_match_table = st_wdog_match,
  238. },
  239. .probe = st_wdog_probe,
  240. .remove = st_wdog_remove,
  241. };
  242. module_platform_driver(st_wdog_driver);
  243. MODULE_AUTHOR("David Paris <david.paris@st.com>");
  244. MODULE_DESCRIPTION("ST LPC Watchdog Driver");
  245. MODULE_LICENSE("GPL");