uniphier_wdt.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Watchdog driver for the UniPhier watchdog timer
  4. *
  5. * (c) Copyright 2014 Panasonic Corporation
  6. * (c) Copyright 2016 Socionext Inc.
  7. * All rights reserved.
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/watchdog.h>
  16. /* WDT timer setting register */
  17. #define WDTTIMSET 0x3004
  18. #define WDTTIMSET_PERIOD_MASK (0xf << 0)
  19. #define WDTTIMSET_PERIOD_1_SEC (0x3 << 0)
  20. /* WDT reset selection register */
  21. #define WDTRSTSEL 0x3008
  22. #define WDTRSTSEL_RSTSEL_MASK (0x3 << 0)
  23. #define WDTRSTSEL_RSTSEL_BOTH (0x0 << 0)
  24. #define WDTRSTSEL_RSTSEL_IRQ_ONLY (0x2 << 0)
  25. /* WDT control register */
  26. #define WDTCTRL 0x300c
  27. #define WDTCTRL_STATUS BIT(8)
  28. #define WDTCTRL_CLEAR BIT(1)
  29. #define WDTCTRL_ENABLE BIT(0)
  30. #define SEC_TO_WDTTIMSET_PRD(sec) \
  31. (ilog2(sec) + WDTTIMSET_PERIOD_1_SEC)
  32. #define WDTST_TIMEOUT 1000 /* usec */
  33. #define WDT_DEFAULT_TIMEOUT 64 /* Default is 64 seconds */
  34. #define WDT_PERIOD_MIN 1
  35. #define WDT_PERIOD_MAX 128
  36. static unsigned int timeout = 0;
  37. static bool nowayout = WATCHDOG_NOWAYOUT;
  38. struct uniphier_wdt_dev {
  39. struct watchdog_device wdt_dev;
  40. struct regmap *regmap;
  41. };
  42. /*
  43. * UniPhier Watchdog operations
  44. */
  45. static int uniphier_watchdog_ping(struct watchdog_device *w)
  46. {
  47. struct uniphier_wdt_dev *wdev = watchdog_get_drvdata(w);
  48. unsigned int val;
  49. int ret;
  50. /* Clear counter */
  51. ret = regmap_write_bits(wdev->regmap, WDTCTRL,
  52. WDTCTRL_CLEAR, WDTCTRL_CLEAR);
  53. if (!ret)
  54. /*
  55. * As SoC specification, after clear counter,
  56. * it needs to wait until counter status is 1.
  57. */
  58. ret = regmap_read_poll_timeout(wdev->regmap, WDTCTRL, val,
  59. (val & WDTCTRL_STATUS),
  60. 0, WDTST_TIMEOUT);
  61. return ret;
  62. }
  63. static int __uniphier_watchdog_start(struct regmap *regmap, unsigned int sec)
  64. {
  65. unsigned int val;
  66. int ret;
  67. ret = regmap_read_poll_timeout(regmap, WDTCTRL, val,
  68. !(val & WDTCTRL_STATUS),
  69. 0, WDTST_TIMEOUT);
  70. if (ret)
  71. return ret;
  72. /* Setup period */
  73. ret = regmap_write(regmap, WDTTIMSET,
  74. SEC_TO_WDTTIMSET_PRD(sec));
  75. if (ret)
  76. return ret;
  77. /* Enable and clear watchdog */
  78. ret = regmap_write(regmap, WDTCTRL, WDTCTRL_ENABLE | WDTCTRL_CLEAR);
  79. if (!ret)
  80. /*
  81. * As SoC specification, after clear counter,
  82. * it needs to wait until counter status is 1.
  83. */
  84. ret = regmap_read_poll_timeout(regmap, WDTCTRL, val,
  85. (val & WDTCTRL_STATUS),
  86. 0, WDTST_TIMEOUT);
  87. return ret;
  88. }
  89. static int __uniphier_watchdog_stop(struct regmap *regmap)
  90. {
  91. /* Disable and stop watchdog */
  92. return regmap_write_bits(regmap, WDTCTRL, WDTCTRL_ENABLE, 0);
  93. }
  94. static int __uniphier_watchdog_restart(struct regmap *regmap, unsigned int sec)
  95. {
  96. int ret;
  97. ret = __uniphier_watchdog_stop(regmap);
  98. if (ret)
  99. return ret;
  100. return __uniphier_watchdog_start(regmap, sec);
  101. }
  102. static int uniphier_watchdog_start(struct watchdog_device *w)
  103. {
  104. struct uniphier_wdt_dev *wdev = watchdog_get_drvdata(w);
  105. unsigned int tmp_timeout;
  106. tmp_timeout = roundup_pow_of_two(w->timeout);
  107. return __uniphier_watchdog_start(wdev->regmap, tmp_timeout);
  108. }
  109. static int uniphier_watchdog_stop(struct watchdog_device *w)
  110. {
  111. struct uniphier_wdt_dev *wdev = watchdog_get_drvdata(w);
  112. return __uniphier_watchdog_stop(wdev->regmap);
  113. }
  114. static int uniphier_watchdog_set_timeout(struct watchdog_device *w,
  115. unsigned int t)
  116. {
  117. struct uniphier_wdt_dev *wdev = watchdog_get_drvdata(w);
  118. unsigned int tmp_timeout;
  119. int ret;
  120. tmp_timeout = roundup_pow_of_two(t);
  121. if (tmp_timeout == w->timeout)
  122. return 0;
  123. if (watchdog_active(w)) {
  124. ret = __uniphier_watchdog_restart(wdev->regmap, tmp_timeout);
  125. if (ret)
  126. return ret;
  127. }
  128. w->timeout = tmp_timeout;
  129. return 0;
  130. }
  131. /*
  132. * Kernel Interfaces
  133. */
  134. static const struct watchdog_info uniphier_wdt_info = {
  135. .identity = "uniphier-wdt",
  136. .options = WDIOF_SETTIMEOUT |
  137. WDIOF_KEEPALIVEPING |
  138. WDIOF_MAGICCLOSE |
  139. WDIOF_OVERHEAT,
  140. };
  141. static const struct watchdog_ops uniphier_wdt_ops = {
  142. .owner = THIS_MODULE,
  143. .start = uniphier_watchdog_start,
  144. .stop = uniphier_watchdog_stop,
  145. .ping = uniphier_watchdog_ping,
  146. .set_timeout = uniphier_watchdog_set_timeout,
  147. };
  148. static int uniphier_wdt_probe(struct platform_device *pdev)
  149. {
  150. struct device *dev = &pdev->dev;
  151. struct uniphier_wdt_dev *wdev;
  152. struct regmap *regmap;
  153. struct device_node *parent;
  154. int ret;
  155. wdev = devm_kzalloc(dev, sizeof(*wdev), GFP_KERNEL);
  156. if (!wdev)
  157. return -ENOMEM;
  158. parent = of_get_parent(dev->of_node); /* parent should be syscon node */
  159. regmap = syscon_node_to_regmap(parent);
  160. of_node_put(parent);
  161. if (IS_ERR(regmap))
  162. return PTR_ERR(regmap);
  163. wdev->regmap = regmap;
  164. wdev->wdt_dev.info = &uniphier_wdt_info;
  165. wdev->wdt_dev.ops = &uniphier_wdt_ops;
  166. wdev->wdt_dev.max_timeout = WDT_PERIOD_MAX;
  167. wdev->wdt_dev.min_timeout = WDT_PERIOD_MIN;
  168. wdev->wdt_dev.timeout = WDT_DEFAULT_TIMEOUT;
  169. wdev->wdt_dev.parent = dev;
  170. watchdog_init_timeout(&wdev->wdt_dev, timeout, dev);
  171. watchdog_set_nowayout(&wdev->wdt_dev, nowayout);
  172. watchdog_stop_on_reboot(&wdev->wdt_dev);
  173. watchdog_set_drvdata(&wdev->wdt_dev, wdev);
  174. uniphier_watchdog_stop(&wdev->wdt_dev);
  175. ret = regmap_write(wdev->regmap, WDTRSTSEL, WDTRSTSEL_RSTSEL_BOTH);
  176. if (ret)
  177. return ret;
  178. ret = devm_watchdog_register_device(dev, &wdev->wdt_dev);
  179. if (ret)
  180. return ret;
  181. dev_info(dev, "watchdog driver (timeout=%d sec, nowayout=%d)\n",
  182. wdev->wdt_dev.timeout, nowayout);
  183. return 0;
  184. }
  185. static const struct of_device_id uniphier_wdt_dt_ids[] = {
  186. { .compatible = "socionext,uniphier-wdt" },
  187. { /* sentinel */ }
  188. };
  189. MODULE_DEVICE_TABLE(of, uniphier_wdt_dt_ids);
  190. static struct platform_driver uniphier_wdt_driver = {
  191. .probe = uniphier_wdt_probe,
  192. .driver = {
  193. .name = "uniphier-wdt",
  194. .of_match_table = uniphier_wdt_dt_ids,
  195. },
  196. };
  197. module_platform_driver(uniphier_wdt_driver);
  198. module_param(timeout, uint, 0000);
  199. MODULE_PARM_DESC(timeout,
  200. "Watchdog timeout seconds in power of 2. (0 < timeout < 128, default="
  201. __MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")");
  202. module_param(nowayout, bool, 0000);
  203. MODULE_PARM_DESC(nowayout,
  204. "Watchdog cannot be stopped once started (default="
  205. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  206. MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
  207. MODULE_DESCRIPTION("UniPhier Watchdog Device Driver");
  208. MODULE_LICENSE("GPL v2");