st_lpc_wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 int st_wdog_probe(struct platform_device *pdev)
  117. {
  118. const struct of_device_id *match;
  119. struct device_node *np = pdev->dev.of_node;
  120. struct st_wdog *st_wdog;
  121. struct regmap *regmap;
  122. struct resource *res;
  123. struct clk *clk;
  124. void __iomem *base;
  125. uint32_t mode;
  126. int ret;
  127. ret = of_property_read_u32(np, "st,lpc-mode", &mode);
  128. if (ret) {
  129. dev_err(&pdev->dev, "An LPC mode must be provided\n");
  130. return -EINVAL;
  131. }
  132. /* LPC can either run as a Clocksource or in RTC or WDT mode */
  133. if (mode != ST_LPC_MODE_WDT)
  134. return -ENODEV;
  135. st_wdog = devm_kzalloc(&pdev->dev, sizeof(*st_wdog), GFP_KERNEL);
  136. if (!st_wdog)
  137. return -ENOMEM;
  138. match = of_match_device(st_wdog_match, &pdev->dev);
  139. if (!match) {
  140. dev_err(&pdev->dev, "Couldn't match device\n");
  141. return -ENODEV;
  142. }
  143. st_wdog->syscfg = (struct st_wdog_syscfg *)match->data;
  144. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  145. base = devm_ioremap_resource(&pdev->dev, res);
  146. if (IS_ERR(base))
  147. return PTR_ERR(base);
  148. regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  149. if (IS_ERR(regmap)) {
  150. dev_err(&pdev->dev, "No syscfg phandle specified\n");
  151. return PTR_ERR(regmap);
  152. }
  153. clk = devm_clk_get(&pdev->dev, NULL);
  154. if (IS_ERR(clk)) {
  155. dev_err(&pdev->dev, "Unable to request clock\n");
  156. return PTR_ERR(clk);
  157. }
  158. st_wdog->dev = &pdev->dev;
  159. st_wdog->base = base;
  160. st_wdog->clk = clk;
  161. st_wdog->regmap = regmap;
  162. st_wdog->warm_reset = of_property_read_bool(np, "st,warm_reset");
  163. st_wdog->clkrate = clk_get_rate(st_wdog->clk);
  164. if (!st_wdog->clkrate) {
  165. dev_err(&pdev->dev, "Unable to fetch clock rate\n");
  166. return -EINVAL;
  167. }
  168. st_wdog_dev.max_timeout = 0xFFFFFFFF / st_wdog->clkrate;
  169. st_wdog_dev.parent = &pdev->dev;
  170. ret = clk_prepare_enable(clk);
  171. if (ret) {
  172. dev_err(&pdev->dev, "Unable to enable clock\n");
  173. return ret;
  174. }
  175. watchdog_set_drvdata(&st_wdog_dev, st_wdog);
  176. watchdog_set_nowayout(&st_wdog_dev, WATCHDOG_NOWAYOUT);
  177. /* Init Watchdog timeout with value in DT */
  178. ret = watchdog_init_timeout(&st_wdog_dev, 0, &pdev->dev);
  179. if (ret) {
  180. dev_err(&pdev->dev, "Unable to initialise watchdog timeout\n");
  181. clk_disable_unprepare(clk);
  182. return ret;
  183. }
  184. ret = watchdog_register_device(&st_wdog_dev);
  185. if (ret) {
  186. dev_err(&pdev->dev, "Unable to register watchdog\n");
  187. clk_disable_unprepare(clk);
  188. return ret;
  189. }
  190. st_wdog_setup(st_wdog, true);
  191. dev_info(&pdev->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. watchdog_unregister_device(&st_wdog_dev);
  200. clk_disable_unprepare(st_wdog->clk);
  201. return 0;
  202. }
  203. #ifdef CONFIG_PM_SLEEP
  204. static int st_wdog_suspend(struct device *dev)
  205. {
  206. struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
  207. if (watchdog_active(&st_wdog_dev))
  208. st_wdog_stop(&st_wdog_dev);
  209. st_wdog_setup(st_wdog, false);
  210. clk_disable(st_wdog->clk);
  211. return 0;
  212. }
  213. static int st_wdog_resume(struct device *dev)
  214. {
  215. struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
  216. int ret;
  217. ret = clk_enable(st_wdog->clk);
  218. if (ret) {
  219. dev_err(dev, "Unable to re-enable clock\n");
  220. watchdog_unregister_device(&st_wdog_dev);
  221. clk_unprepare(st_wdog->clk);
  222. return ret;
  223. }
  224. st_wdog_setup(st_wdog, true);
  225. if (watchdog_active(&st_wdog_dev)) {
  226. st_wdog_load_timer(st_wdog, st_wdog_dev.timeout);
  227. st_wdog_start(&st_wdog_dev);
  228. }
  229. return 0;
  230. }
  231. #endif
  232. static SIMPLE_DEV_PM_OPS(st_wdog_pm_ops,
  233. st_wdog_suspend,
  234. st_wdog_resume);
  235. static struct platform_driver st_wdog_driver = {
  236. .driver = {
  237. .name = "st-lpc-wdt",
  238. .pm = &st_wdog_pm_ops,
  239. .of_match_table = st_wdog_match,
  240. },
  241. .probe = st_wdog_probe,
  242. .remove = st_wdog_remove,
  243. };
  244. module_platform_driver(st_wdog_driver);
  245. MODULE_AUTHOR("David Paris <david.paris@st.com>");
  246. MODULE_DESCRIPTION("ST LPC Watchdog Driver");
  247. MODULE_LICENSE("GPL");