zx2967_wdt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * watchdog driver for ZTE's zx2967 family
  3. *
  4. * Copyright (C) 2017 ZTE Ltd.
  5. *
  6. * Author: Baoyou Xie <baoyou.xie@linaro.org>
  7. *
  8. * License terms: GNU General Public License (GPL) version 2
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/io.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/of_address.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/reset.h>
  18. #include <linux/watchdog.h>
  19. #define ZX2967_WDT_CFG_REG 0x4
  20. #define ZX2967_WDT_LOAD_REG 0x8
  21. #define ZX2967_WDT_REFRESH_REG 0x18
  22. #define ZX2967_WDT_START_REG 0x1c
  23. #define ZX2967_WDT_REFRESH_MASK GENMASK(5, 0)
  24. #define ZX2967_WDT_CFG_DIV(n) ((((n) & 0xff) - 1) << 8)
  25. #define ZX2967_WDT_START_EN 0x1
  26. /*
  27. * Hardware magic number.
  28. * When watchdog reg is written, the lowest 16 bits are valid, but
  29. * the highest 16 bits should be always this number.
  30. */
  31. #define ZX2967_WDT_WRITEKEY (0x1234 << 16)
  32. #define ZX2967_WDT_VAL_MASK GENMASK(15, 0)
  33. #define ZX2967_WDT_DIV_DEFAULT 16
  34. #define ZX2967_WDT_DEFAULT_TIMEOUT 32
  35. #define ZX2967_WDT_MIN_TIMEOUT 1
  36. #define ZX2967_WDT_MAX_TIMEOUT 524
  37. #define ZX2967_WDT_MAX_COUNT 0xffff
  38. #define ZX2967_WDT_CLK_FREQ 0x8000
  39. #define ZX2967_WDT_FLAG_REBOOT_MON BIT(0)
  40. struct zx2967_wdt {
  41. struct watchdog_device wdt_device;
  42. void __iomem *reg_base;
  43. struct clk *clock;
  44. };
  45. static inline u32 zx2967_wdt_readl(struct zx2967_wdt *wdt, u16 reg)
  46. {
  47. return readl_relaxed(wdt->reg_base + reg);
  48. }
  49. static inline void zx2967_wdt_writel(struct zx2967_wdt *wdt, u16 reg, u32 val)
  50. {
  51. writel_relaxed(val | ZX2967_WDT_WRITEKEY, wdt->reg_base + reg);
  52. }
  53. static void zx2967_wdt_refresh(struct zx2967_wdt *wdt)
  54. {
  55. u32 val;
  56. val = zx2967_wdt_readl(wdt, ZX2967_WDT_REFRESH_REG);
  57. /*
  58. * Bit 4-5, 1 and 2: refresh config info
  59. * Bit 2-3, 1 and 2: refresh counter
  60. * Bit 0-1, 1 and 2: refresh int-value
  61. * we shift each group value between 1 and 2 to refresh all data.
  62. */
  63. val ^= ZX2967_WDT_REFRESH_MASK;
  64. zx2967_wdt_writel(wdt, ZX2967_WDT_REFRESH_REG,
  65. val & ZX2967_WDT_VAL_MASK);
  66. }
  67. static int
  68. zx2967_wdt_set_timeout(struct watchdog_device *wdd, unsigned int timeout)
  69. {
  70. struct zx2967_wdt *wdt = watchdog_get_drvdata(wdd);
  71. unsigned int divisor = ZX2967_WDT_DIV_DEFAULT;
  72. u32 count;
  73. count = timeout * ZX2967_WDT_CLK_FREQ;
  74. if (count > divisor * ZX2967_WDT_MAX_COUNT)
  75. divisor = DIV_ROUND_UP(count, ZX2967_WDT_MAX_COUNT);
  76. count = DIV_ROUND_UP(count, divisor);
  77. zx2967_wdt_writel(wdt, ZX2967_WDT_CFG_REG,
  78. ZX2967_WDT_CFG_DIV(divisor) & ZX2967_WDT_VAL_MASK);
  79. zx2967_wdt_writel(wdt, ZX2967_WDT_LOAD_REG,
  80. count & ZX2967_WDT_VAL_MASK);
  81. zx2967_wdt_refresh(wdt);
  82. wdd->timeout = (count * divisor) / ZX2967_WDT_CLK_FREQ;
  83. return 0;
  84. }
  85. static void __zx2967_wdt_start(struct zx2967_wdt *wdt)
  86. {
  87. u32 val;
  88. val = zx2967_wdt_readl(wdt, ZX2967_WDT_START_REG);
  89. val |= ZX2967_WDT_START_EN;
  90. zx2967_wdt_writel(wdt, ZX2967_WDT_START_REG,
  91. val & ZX2967_WDT_VAL_MASK);
  92. }
  93. static void __zx2967_wdt_stop(struct zx2967_wdt *wdt)
  94. {
  95. u32 val;
  96. val = zx2967_wdt_readl(wdt, ZX2967_WDT_START_REG);
  97. val &= ~ZX2967_WDT_START_EN;
  98. zx2967_wdt_writel(wdt, ZX2967_WDT_START_REG,
  99. val & ZX2967_WDT_VAL_MASK);
  100. }
  101. static int zx2967_wdt_start(struct watchdog_device *wdd)
  102. {
  103. struct zx2967_wdt *wdt = watchdog_get_drvdata(wdd);
  104. zx2967_wdt_set_timeout(wdd, wdd->timeout);
  105. __zx2967_wdt_start(wdt);
  106. return 0;
  107. }
  108. static int zx2967_wdt_stop(struct watchdog_device *wdd)
  109. {
  110. struct zx2967_wdt *wdt = watchdog_get_drvdata(wdd);
  111. __zx2967_wdt_stop(wdt);
  112. return 0;
  113. }
  114. static int zx2967_wdt_keepalive(struct watchdog_device *wdd)
  115. {
  116. struct zx2967_wdt *wdt = watchdog_get_drvdata(wdd);
  117. zx2967_wdt_refresh(wdt);
  118. return 0;
  119. }
  120. #define ZX2967_WDT_OPTIONS \
  121. (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
  122. static const struct watchdog_info zx2967_wdt_ident = {
  123. .options = ZX2967_WDT_OPTIONS,
  124. .identity = "zx2967 watchdog",
  125. };
  126. static const struct watchdog_ops zx2967_wdt_ops = {
  127. .owner = THIS_MODULE,
  128. .start = zx2967_wdt_start,
  129. .stop = zx2967_wdt_stop,
  130. .ping = zx2967_wdt_keepalive,
  131. .set_timeout = zx2967_wdt_set_timeout,
  132. };
  133. static void zx2967_wdt_reset_sysctrl(struct device *dev)
  134. {
  135. int ret;
  136. void __iomem *regmap;
  137. unsigned int offset, mask, config;
  138. struct of_phandle_args out_args;
  139. ret = of_parse_phandle_with_fixed_args(dev->of_node,
  140. "zte,wdt-reset-sysctrl", 3, 0, &out_args);
  141. if (ret)
  142. return;
  143. offset = out_args.args[0];
  144. config = out_args.args[1];
  145. mask = out_args.args[2];
  146. regmap = syscon_node_to_regmap(out_args.np);
  147. if (IS_ERR(regmap)) {
  148. of_node_put(out_args.np);
  149. return;
  150. }
  151. regmap_update_bits(regmap, offset, mask, config);
  152. of_node_put(out_args.np);
  153. }
  154. static int zx2967_wdt_probe(struct platform_device *pdev)
  155. {
  156. struct device *dev = &pdev->dev;
  157. struct zx2967_wdt *wdt;
  158. struct resource *base;
  159. int ret;
  160. struct reset_control *rstc;
  161. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  162. if (!wdt)
  163. return -ENOMEM;
  164. platform_set_drvdata(pdev, wdt);
  165. wdt->wdt_device.info = &zx2967_wdt_ident;
  166. wdt->wdt_device.ops = &zx2967_wdt_ops;
  167. wdt->wdt_device.timeout = ZX2967_WDT_DEFAULT_TIMEOUT;
  168. wdt->wdt_device.max_timeout = ZX2967_WDT_MAX_TIMEOUT;
  169. wdt->wdt_device.min_timeout = ZX2967_WDT_MIN_TIMEOUT;
  170. wdt->wdt_device.parent = &pdev->dev;
  171. base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  172. wdt->reg_base = devm_ioremap_resource(dev, base);
  173. if (IS_ERR(wdt->reg_base))
  174. return PTR_ERR(wdt->reg_base);
  175. zx2967_wdt_reset_sysctrl(dev);
  176. wdt->clock = devm_clk_get(dev, NULL);
  177. if (IS_ERR(wdt->clock)) {
  178. dev_err(dev, "failed to find watchdog clock source\n");
  179. return PTR_ERR(wdt->clock);
  180. }
  181. ret = clk_prepare_enable(wdt->clock);
  182. if (ret < 0) {
  183. dev_err(dev, "failed to enable clock\n");
  184. return ret;
  185. }
  186. clk_set_rate(wdt->clock, ZX2967_WDT_CLK_FREQ);
  187. rstc = devm_reset_control_get_exclusive(dev, NULL);
  188. if (IS_ERR(rstc)) {
  189. dev_err(dev, "failed to get rstc");
  190. ret = PTR_ERR(rstc);
  191. goto err;
  192. }
  193. reset_control_assert(rstc);
  194. reset_control_deassert(rstc);
  195. watchdog_set_drvdata(&wdt->wdt_device, wdt);
  196. watchdog_init_timeout(&wdt->wdt_device,
  197. ZX2967_WDT_DEFAULT_TIMEOUT, dev);
  198. watchdog_set_nowayout(&wdt->wdt_device, WATCHDOG_NOWAYOUT);
  199. ret = watchdog_register_device(&wdt->wdt_device);
  200. if (ret)
  201. goto err;
  202. dev_info(dev, "watchdog enabled (timeout=%d sec, nowayout=%d)",
  203. wdt->wdt_device.timeout, WATCHDOG_NOWAYOUT);
  204. return 0;
  205. err:
  206. clk_disable_unprepare(wdt->clock);
  207. return ret;
  208. }
  209. static int zx2967_wdt_remove(struct platform_device *pdev)
  210. {
  211. struct zx2967_wdt *wdt = platform_get_drvdata(pdev);
  212. watchdog_unregister_device(&wdt->wdt_device);
  213. clk_disable_unprepare(wdt->clock);
  214. return 0;
  215. }
  216. static const struct of_device_id zx2967_wdt_match[] = {
  217. { .compatible = "zte,zx296718-wdt", },
  218. {}
  219. };
  220. MODULE_DEVICE_TABLE(of, zx2967_wdt_match);
  221. static struct platform_driver zx2967_wdt_driver = {
  222. .probe = zx2967_wdt_probe,
  223. .remove = zx2967_wdt_remove,
  224. .driver = {
  225. .name = "zx2967-wdt",
  226. .of_match_table = of_match_ptr(zx2967_wdt_match),
  227. },
  228. };
  229. module_platform_driver(zx2967_wdt_driver);
  230. MODULE_AUTHOR("Baoyou Xie <baoyou.xie@linaro.org>");
  231. MODULE_DESCRIPTION("ZTE zx2967 Watchdog Device Driver");
  232. MODULE_LICENSE("GPL v2");