realtek_otto_wdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Realtek Otto MIPS platform watchdog
  4. *
  5. * Watchdog timer that will reset the system after timeout, using the selected
  6. * reset mode.
  7. *
  8. * Counter scaling and timeouts:
  9. * - Base prescale of (2 << 25), providing tick duration T_0: 168ms @ 200MHz
  10. * - PRESCALE: logarithmic prescaler adding a factor of {1, 2, 4, 8}
  11. * - Phase 1: Times out after (PHASE1 + 1) × PRESCALE × T_0
  12. * Generates an interrupt, WDT cannot be stopped after phase 1
  13. * - Phase 2: starts after phase 1, times out after (PHASE2 + 1) × PRESCALE × T_0
  14. * Resets the system according to RST_MODE
  15. */
  16. #include <linux/bits.h>
  17. #include <linux/bitfield.h>
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/math.h>
  23. #include <linux/minmax.h>
  24. #include <linux/module.h>
  25. #include <linux/mod_devicetable.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/property.h>
  28. #include <linux/reboot.h>
  29. #include <linux/watchdog.h>
  30. #define OTTO_WDT_REG_CNTR 0x0
  31. #define OTTO_WDT_CNTR_PING BIT(31)
  32. #define OTTO_WDT_REG_INTR 0x4
  33. #define OTTO_WDT_INTR_PHASE_1 BIT(31)
  34. #define OTTO_WDT_INTR_PHASE_2 BIT(30)
  35. #define OTTO_WDT_REG_CTRL 0x8
  36. #define OTTO_WDT_CTRL_ENABLE BIT(31)
  37. #define OTTO_WDT_CTRL_PRESCALE GENMASK(30, 29)
  38. #define OTTO_WDT_CTRL_PHASE1 GENMASK(26, 22)
  39. #define OTTO_WDT_CTRL_PHASE2 GENMASK(19, 15)
  40. #define OTTO_WDT_CTRL_RST_MODE GENMASK(1, 0)
  41. #define OTTO_WDT_MODE_SOC 0
  42. #define OTTO_WDT_MODE_CPU 1
  43. #define OTTO_WDT_MODE_SOFTWARE 2
  44. #define OTTO_WDT_CTRL_DEFAULT OTTO_WDT_MODE_CPU
  45. #define OTTO_WDT_PRESCALE_MAX 3
  46. /*
  47. * One higher than the max values contained in PHASE{1,2}, since a value of 0
  48. * corresponds to one tick.
  49. */
  50. #define OTTO_WDT_PHASE_TICKS_MAX 32
  51. /*
  52. * The maximum reset delay is actually 2×32 ticks, but that would require large
  53. * pretimeout values for timeouts longer than 32 ticks. Limit the maximum timeout
  54. * to 32 + 1 to ensure small pretimeout values can be configured as expected.
  55. */
  56. #define OTTO_WDT_TIMEOUT_TICKS_MAX (OTTO_WDT_PHASE_TICKS_MAX + 1)
  57. struct otto_wdt_ctrl {
  58. struct watchdog_device wdev;
  59. struct device *dev;
  60. void __iomem *base;
  61. unsigned int clk_rate_khz;
  62. int irq_phase1;
  63. };
  64. static int otto_wdt_start(struct watchdog_device *wdev)
  65. {
  66. struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
  67. u32 v;
  68. v = ioread32(ctrl->base + OTTO_WDT_REG_CTRL);
  69. v |= OTTO_WDT_CTRL_ENABLE;
  70. iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
  71. return 0;
  72. }
  73. static int otto_wdt_stop(struct watchdog_device *wdev)
  74. {
  75. struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
  76. u32 v;
  77. v = ioread32(ctrl->base + OTTO_WDT_REG_CTRL);
  78. v &= ~OTTO_WDT_CTRL_ENABLE;
  79. iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
  80. return 0;
  81. }
  82. static int otto_wdt_ping(struct watchdog_device *wdev)
  83. {
  84. struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
  85. iowrite32(OTTO_WDT_CNTR_PING, ctrl->base + OTTO_WDT_REG_CNTR);
  86. return 0;
  87. }
  88. static int otto_wdt_tick_ms(struct otto_wdt_ctrl *ctrl, int prescale)
  89. {
  90. return DIV_ROUND_CLOSEST(1 << (25 + prescale), ctrl->clk_rate_khz);
  91. }
  92. /*
  93. * The timer asserts the PHASE1/PHASE2 IRQs when the number of ticks exceeds
  94. * the value stored in those fields. This means each phase will run for at least
  95. * one tick, so small values need to be clamped to correctly reflect the timeout.
  96. */
  97. static inline unsigned int div_round_ticks(unsigned int val, unsigned int tick_duration,
  98. unsigned int min_ticks)
  99. {
  100. return max(min_ticks, DIV_ROUND_UP(val, tick_duration));
  101. }
  102. static int otto_wdt_determine_timeouts(struct watchdog_device *wdev, unsigned int timeout,
  103. unsigned int pretimeout)
  104. {
  105. struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
  106. unsigned int pretimeout_ms = pretimeout * 1000;
  107. unsigned int timeout_ms = timeout * 1000;
  108. unsigned int prescale_next = 0;
  109. unsigned int phase1_ticks;
  110. unsigned int phase2_ticks;
  111. unsigned int total_ticks;
  112. unsigned int prescale;
  113. unsigned int tick_ms;
  114. u32 v;
  115. do {
  116. prescale = prescale_next;
  117. if (prescale > OTTO_WDT_PRESCALE_MAX)
  118. return -EINVAL;
  119. tick_ms = otto_wdt_tick_ms(ctrl, prescale);
  120. total_ticks = div_round_ticks(timeout_ms, tick_ms, 2);
  121. phase1_ticks = div_round_ticks(timeout_ms - pretimeout_ms, tick_ms, 1);
  122. phase2_ticks = total_ticks - phase1_ticks;
  123. prescale_next++;
  124. } while (phase1_ticks > OTTO_WDT_PHASE_TICKS_MAX
  125. || phase2_ticks > OTTO_WDT_PHASE_TICKS_MAX);
  126. v = ioread32(ctrl->base + OTTO_WDT_REG_CTRL);
  127. v &= ~(OTTO_WDT_CTRL_PRESCALE | OTTO_WDT_CTRL_PHASE1 | OTTO_WDT_CTRL_PHASE2);
  128. v |= FIELD_PREP(OTTO_WDT_CTRL_PHASE1, phase1_ticks - 1);
  129. v |= FIELD_PREP(OTTO_WDT_CTRL_PHASE2, phase2_ticks - 1);
  130. v |= FIELD_PREP(OTTO_WDT_CTRL_PRESCALE, prescale);
  131. iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
  132. timeout_ms = total_ticks * tick_ms;
  133. ctrl->wdev.timeout = timeout_ms / 1000;
  134. pretimeout_ms = phase2_ticks * tick_ms;
  135. ctrl->wdev.pretimeout = pretimeout_ms / 1000;
  136. return 0;
  137. }
  138. static int otto_wdt_set_timeout(struct watchdog_device *wdev, unsigned int val)
  139. {
  140. return otto_wdt_determine_timeouts(wdev, val, min(wdev->pretimeout, val - 1));
  141. }
  142. static int otto_wdt_set_pretimeout(struct watchdog_device *wdev, unsigned int val)
  143. {
  144. return otto_wdt_determine_timeouts(wdev, wdev->timeout, val);
  145. }
  146. static int otto_wdt_restart(struct watchdog_device *wdev, unsigned long reboot_mode,
  147. void *data)
  148. {
  149. struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
  150. u32 reset_mode;
  151. u32 v;
  152. disable_irq(ctrl->irq_phase1);
  153. switch (reboot_mode) {
  154. case REBOOT_SOFT:
  155. reset_mode = OTTO_WDT_MODE_SOFTWARE;
  156. break;
  157. case REBOOT_WARM:
  158. reset_mode = OTTO_WDT_MODE_CPU;
  159. break;
  160. default:
  161. reset_mode = OTTO_WDT_MODE_SOC;
  162. break;
  163. }
  164. /* Configure for shortest timeout and wait for reset to occur */
  165. v = FIELD_PREP(OTTO_WDT_CTRL_RST_MODE, reset_mode) | OTTO_WDT_CTRL_ENABLE;
  166. iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
  167. mdelay(3 * otto_wdt_tick_ms(ctrl, 0));
  168. return 0;
  169. }
  170. static irqreturn_t otto_wdt_phase1_isr(int irq, void *dev_id)
  171. {
  172. struct otto_wdt_ctrl *ctrl = dev_id;
  173. iowrite32(OTTO_WDT_INTR_PHASE_1, ctrl->base + OTTO_WDT_REG_INTR);
  174. dev_crit(ctrl->dev, "phase 1 timeout\n");
  175. watchdog_notify_pretimeout(&ctrl->wdev);
  176. return IRQ_HANDLED;
  177. }
  178. static const struct watchdog_ops otto_wdt_ops = {
  179. .owner = THIS_MODULE,
  180. .start = otto_wdt_start,
  181. .stop = otto_wdt_stop,
  182. .ping = otto_wdt_ping,
  183. .set_timeout = otto_wdt_set_timeout,
  184. .set_pretimeout = otto_wdt_set_pretimeout,
  185. .restart = otto_wdt_restart,
  186. };
  187. static const struct watchdog_info otto_wdt_info = {
  188. .identity = "Realtek Otto watchdog timer",
  189. .options = WDIOF_KEEPALIVEPING |
  190. WDIOF_MAGICCLOSE |
  191. WDIOF_SETTIMEOUT |
  192. WDIOF_PRETIMEOUT,
  193. };
  194. static void otto_wdt_clock_action(void *data)
  195. {
  196. clk_disable_unprepare(data);
  197. }
  198. static int otto_wdt_probe_clk(struct otto_wdt_ctrl *ctrl)
  199. {
  200. struct clk *clk = devm_clk_get(ctrl->dev, NULL);
  201. int ret;
  202. if (IS_ERR(clk))
  203. return dev_err_probe(ctrl->dev, PTR_ERR(clk), "Failed to get clock\n");
  204. ret = clk_prepare_enable(clk);
  205. if (ret)
  206. return dev_err_probe(ctrl->dev, ret, "Failed to enable clock\n");
  207. ret = devm_add_action_or_reset(ctrl->dev, otto_wdt_clock_action, clk);
  208. if (ret)
  209. return ret;
  210. ctrl->clk_rate_khz = clk_get_rate(clk) / 1000;
  211. if (ctrl->clk_rate_khz == 0)
  212. return dev_err_probe(ctrl->dev, -ENXIO, "Failed to get clock rate\n");
  213. return 0;
  214. }
  215. static int otto_wdt_probe_reset_mode(struct otto_wdt_ctrl *ctrl)
  216. {
  217. static const char *mode_property = "realtek,reset-mode";
  218. const struct fwnode_handle *node = ctrl->dev->fwnode;
  219. int mode_count;
  220. u32 mode;
  221. u32 v;
  222. if (!node)
  223. return -ENXIO;
  224. mode_count = fwnode_property_string_array_count(node, mode_property);
  225. if (mode_count < 0)
  226. return mode_count;
  227. else if (mode_count == 0)
  228. return 0;
  229. else if (mode_count != 1)
  230. return -EINVAL;
  231. if (fwnode_property_match_string(node, mode_property, "soc") == 0)
  232. mode = OTTO_WDT_MODE_SOC;
  233. else if (fwnode_property_match_string(node, mode_property, "cpu") == 0)
  234. mode = OTTO_WDT_MODE_CPU;
  235. else if (fwnode_property_match_string(node, mode_property, "software") == 0)
  236. mode = OTTO_WDT_MODE_SOFTWARE;
  237. else
  238. return -EINVAL;
  239. v = ioread32(ctrl->base + OTTO_WDT_REG_CTRL);
  240. v &= ~OTTO_WDT_CTRL_RST_MODE;
  241. v |= FIELD_PREP(OTTO_WDT_CTRL_RST_MODE, mode);
  242. iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
  243. return 0;
  244. }
  245. static int otto_wdt_probe(struct platform_device *pdev)
  246. {
  247. struct device *dev = &pdev->dev;
  248. struct otto_wdt_ctrl *ctrl;
  249. unsigned int max_tick_ms;
  250. int ret;
  251. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  252. if (!ctrl)
  253. return -ENOMEM;
  254. ctrl->dev = dev;
  255. ctrl->base = devm_platform_ioremap_resource(pdev, 0);
  256. if (IS_ERR(ctrl->base))
  257. return PTR_ERR(ctrl->base);
  258. /* Clear any old interrupts and reset initial state */
  259. iowrite32(OTTO_WDT_INTR_PHASE_1 | OTTO_WDT_INTR_PHASE_2,
  260. ctrl->base + OTTO_WDT_REG_INTR);
  261. iowrite32(OTTO_WDT_CTRL_DEFAULT, ctrl->base + OTTO_WDT_REG_CTRL);
  262. ret = otto_wdt_probe_clk(ctrl);
  263. if (ret)
  264. return ret;
  265. ctrl->irq_phase1 = platform_get_irq_byname(pdev, "phase1");
  266. if (ctrl->irq_phase1 < 0)
  267. return ctrl->irq_phase1;
  268. ret = devm_request_irq(dev, ctrl->irq_phase1, otto_wdt_phase1_isr, 0,
  269. "realtek-otto-wdt", ctrl);
  270. if (ret)
  271. return dev_err_probe(dev, ret, "Failed to get IRQ for phase1\n");
  272. ret = otto_wdt_probe_reset_mode(ctrl);
  273. if (ret)
  274. return dev_err_probe(dev, ret, "Invalid reset mode specified\n");
  275. ctrl->wdev.parent = dev;
  276. ctrl->wdev.info = &otto_wdt_info;
  277. ctrl->wdev.ops = &otto_wdt_ops;
  278. /*
  279. * Since pretimeout cannot be disabled, min. timeout is twice the
  280. * subsystem resolution. Max. timeout is ca. 43s at a bus clock of 200MHz.
  281. */
  282. ctrl->wdev.min_timeout = 2;
  283. max_tick_ms = otto_wdt_tick_ms(ctrl, OTTO_WDT_PRESCALE_MAX);
  284. ctrl->wdev.max_hw_heartbeat_ms = max_tick_ms * OTTO_WDT_TIMEOUT_TICKS_MAX;
  285. ctrl->wdev.timeout = min(30U, ctrl->wdev.max_hw_heartbeat_ms / 1000);
  286. watchdog_set_drvdata(&ctrl->wdev, ctrl);
  287. watchdog_init_timeout(&ctrl->wdev, 0, dev);
  288. watchdog_stop_on_reboot(&ctrl->wdev);
  289. watchdog_set_restart_priority(&ctrl->wdev, 128);
  290. ret = otto_wdt_determine_timeouts(&ctrl->wdev, ctrl->wdev.timeout, 1);
  291. if (ret)
  292. return dev_err_probe(dev, ret, "Failed to set timeout\n");
  293. return devm_watchdog_register_device(dev, &ctrl->wdev);
  294. }
  295. static const struct of_device_id otto_wdt_ids[] = {
  296. { .compatible = "realtek,rtl8380-wdt" },
  297. { .compatible = "realtek,rtl8390-wdt" },
  298. { .compatible = "realtek,rtl9300-wdt" },
  299. { .compatible = "realtek,rtl9310-wdt" },
  300. { }
  301. };
  302. MODULE_DEVICE_TABLE(of, otto_wdt_ids);
  303. static struct platform_driver otto_wdt_driver = {
  304. .probe = otto_wdt_probe,
  305. .driver = {
  306. .name = "realtek-otto-watchdog",
  307. .of_match_table = otto_wdt_ids,
  308. },
  309. };
  310. module_platform_driver(otto_wdt_driver);
  311. MODULE_LICENSE("GPL v2");
  312. MODULE_AUTHOR("Sander Vanheule <[email protected]>");
  313. MODULE_DESCRIPTION("Realtek Otto watchdog timer driver");