kernel: bump 5.15 to 5.15.108
[openwrt/staging/dedeckeh.git] / target / linux / realtek / patches-5.15 / 008-5.17-watchdog-add-realtek-otto-watchdog-timer.patch
1 From 293903b9dfe43520f01374dc1661be11d6838c49 Mon Sep 17 00:00:00 2001
2 From: Sander Vanheule <sander@svanheule.net>
3 Date: Thu, 18 Nov 2021 17:29:52 +0100
4 Subject: watchdog: Add Realtek Otto watchdog timer
5
6 Realtek MIPS SoCs (platform name Otto) have a watchdog timer with
7 pretimeout notifitication support. The WDT can (partially) hard reset,
8 or soft reset the SoC.
9
10 This driver implements all features as described in the devicetree
11 binding, except the phase2 interrupt, and also functions as a restart
12 handler. The cpu reset mode is considered to be a "warm" restart, since
13 this mode does not reset all peripherals. Being an embedded system
14 though, the "cpu" and "software" modes will still cause the bootloader
15 to run on restart.
16
17 It is not known how a forced system reset can be disabled on the
18 supported platforms. This means that the phase2 interrupt will only fire
19 at the same time as reset, so implementing phase2 is of little use.
20
21 Signed-off-by: Sander Vanheule <sander@svanheule.net>
22 Reviewed-by: Guenter Roeck <linux@roeck-us.net>
23 Link: https://lore.kernel.org/r/6d060bccbdcc709cfa79203485db85aad3c3beb5.1637252610.git.sander@svanheule.net
24 Signed-off-by: Guenter Roeck <linux@roeck-us.net>
25 ---
26 MAINTAINERS | 7 +
27 drivers/watchdog/Kconfig | 13 ++
28 drivers/watchdog/Makefile | 1 +
29 drivers/watchdog/realtek_otto_wdt.c | 384 ++++++++++++++++++++++++++++++++++++
30 4 files changed, 405 insertions(+)
31 create mode 100644 drivers/watchdog/realtek_otto_wdt.c
32
33 --- a/MAINTAINERS
34 +++ b/MAINTAINERS
35 @@ -15898,6 +15898,13 @@ S: Maintained
36 F: include/sound/rt*.h
37 F: sound/soc/codecs/rt*
38
39 +REALTEK OTTO WATCHDOG
40 +M: Sander Vanheule <sander@svanheule.net>
41 +L: linux-watchdog@vger.kernel.org
42 +S: Maintained
43 +F: Documentation/devicetree/bindings/watchdog/realtek,otto-wdt.yaml
44 +F: driver/watchdog/realtek_otto_wdt.c
45 +
46 REALTEK RTL83xx SMI DSA ROUTER CHIPS
47 M: Linus Walleij <linus.walleij@linaro.org>
48 S: Maintained
49 --- a/drivers/watchdog/Kconfig
50 +++ b/drivers/watchdog/Kconfig
51 @@ -954,6 +954,19 @@ config RTD119X_WATCHDOG
52 Say Y here to include support for the watchdog timer in
53 Realtek RTD1295 SoCs.
54
55 +config REALTEK_OTTO_WDT
56 + tristate "Realtek Otto MIPS watchdog support"
57 + depends on MACH_REALTEK_RTL || COMPILE_TEST
58 + depends on COMMON_CLK
59 + select WATCHDOG_CORE
60 + default MACH_REALTEK_RTL
61 + help
62 + Say Y here to include support for the watchdog timer on Realtek
63 + RTL838x, RTL839x, RTL930x SoCs. This watchdog has pretimeout
64 + notifications and system reset on timeout.
65 +
66 + When built as a module this will be called realtek_otto_wdt.
67 +
68 config SPRD_WATCHDOG
69 tristate "Spreadtrum watchdog support"
70 depends on ARCH_SPRD || COMPILE_TEST
71 --- a/drivers/watchdog/Makefile
72 +++ b/drivers/watchdog/Makefile
73 @@ -171,6 +171,7 @@ obj-$(CONFIG_IMGPDC_WDT) += imgpdc_wdt.o
74 obj-$(CONFIG_MT7621_WDT) += mt7621_wdt.o
75 obj-$(CONFIG_PIC32_WDT) += pic32-wdt.o
76 obj-$(CONFIG_PIC32_DMT) += pic32-dmt.o
77 +obj-$(CONFIG_REALTEK_OTTO_WDT) += realtek_otto_wdt.o
78
79 # PARISC Architecture
80
81 --- /dev/null
82 +++ b/drivers/watchdog/realtek_otto_wdt.c
83 @@ -0,0 +1,384 @@
84 +// SPDX-License-Identifier: GPL-2.0-only
85 +
86 +/*
87 + * Realtek Otto MIPS platform watchdog
88 + *
89 + * Watchdog timer that will reset the system after timeout, using the selected
90 + * reset mode.
91 + *
92 + * Counter scaling and timeouts:
93 + * - Base prescale of (2 << 25), providing tick duration T_0: 168ms @ 200MHz
94 + * - PRESCALE: logarithmic prescaler adding a factor of {1, 2, 4, 8}
95 + * - Phase 1: Times out after (PHASE1 + 1) × PRESCALE × T_0
96 + * Generates an interrupt, WDT cannot be stopped after phase 1
97 + * - Phase 2: starts after phase 1, times out after (PHASE2 + 1) × PRESCALE × T_0
98 + * Resets the system according to RST_MODE
99 + */
100 +
101 +#include <linux/bits.h>
102 +#include <linux/bitfield.h>
103 +#include <linux/clk.h>
104 +#include <linux/delay.h>
105 +#include <linux/interrupt.h>
106 +#include <linux/io.h>
107 +#include <linux/math.h>
108 +#include <linux/minmax.h>
109 +#include <linux/module.h>
110 +#include <linux/mod_devicetable.h>
111 +#include <linux/platform_device.h>
112 +#include <linux/property.h>
113 +#include <linux/reboot.h>
114 +#include <linux/watchdog.h>
115 +
116 +#define OTTO_WDT_REG_CNTR 0x0
117 +#define OTTO_WDT_CNTR_PING BIT(31)
118 +
119 +#define OTTO_WDT_REG_INTR 0x4
120 +#define OTTO_WDT_INTR_PHASE_1 BIT(31)
121 +#define OTTO_WDT_INTR_PHASE_2 BIT(30)
122 +
123 +#define OTTO_WDT_REG_CTRL 0x8
124 +#define OTTO_WDT_CTRL_ENABLE BIT(31)
125 +#define OTTO_WDT_CTRL_PRESCALE GENMASK(30, 29)
126 +#define OTTO_WDT_CTRL_PHASE1 GENMASK(26, 22)
127 +#define OTTO_WDT_CTRL_PHASE2 GENMASK(19, 15)
128 +#define OTTO_WDT_CTRL_RST_MODE GENMASK(1, 0)
129 +#define OTTO_WDT_MODE_SOC 0
130 +#define OTTO_WDT_MODE_CPU 1
131 +#define OTTO_WDT_MODE_SOFTWARE 2
132 +#define OTTO_WDT_CTRL_DEFAULT OTTO_WDT_MODE_CPU
133 +
134 +#define OTTO_WDT_PRESCALE_MAX 3
135 +
136 +/*
137 + * One higher than the max values contained in PHASE{1,2}, since a value of 0
138 + * corresponds to one tick.
139 + */
140 +#define OTTO_WDT_PHASE_TICKS_MAX 32
141 +
142 +/*
143 + * The maximum reset delay is actually 2×32 ticks, but that would require large
144 + * pretimeout values for timeouts longer than 32 ticks. Limit the maximum timeout
145 + * to 32 + 1 to ensure small pretimeout values can be configured as expected.
146 + */
147 +#define OTTO_WDT_TIMEOUT_TICKS_MAX (OTTO_WDT_PHASE_TICKS_MAX + 1)
148 +
149 +struct otto_wdt_ctrl {
150 + struct watchdog_device wdev;
151 + struct device *dev;
152 + void __iomem *base;
153 + unsigned int clk_rate_khz;
154 + int irq_phase1;
155 +};
156 +
157 +static int otto_wdt_start(struct watchdog_device *wdev)
158 +{
159 + struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
160 + u32 v;
161 +
162 + v = ioread32(ctrl->base + OTTO_WDT_REG_CTRL);
163 + v |= OTTO_WDT_CTRL_ENABLE;
164 + iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
165 +
166 + return 0;
167 +}
168 +
169 +static int otto_wdt_stop(struct watchdog_device *wdev)
170 +{
171 + struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
172 + u32 v;
173 +
174 + v = ioread32(ctrl->base + OTTO_WDT_REG_CTRL);
175 + v &= ~OTTO_WDT_CTRL_ENABLE;
176 + iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
177 +
178 + return 0;
179 +}
180 +
181 +static int otto_wdt_ping(struct watchdog_device *wdev)
182 +{
183 + struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
184 +
185 + iowrite32(OTTO_WDT_CNTR_PING, ctrl->base + OTTO_WDT_REG_CNTR);
186 +
187 + return 0;
188 +}
189 +
190 +static int otto_wdt_tick_ms(struct otto_wdt_ctrl *ctrl, int prescale)
191 +{
192 + return DIV_ROUND_CLOSEST(1 << (25 + prescale), ctrl->clk_rate_khz);
193 +}
194 +
195 +/*
196 + * The timer asserts the PHASE1/PHASE2 IRQs when the number of ticks exceeds
197 + * the value stored in those fields. This means each phase will run for at least
198 + * one tick, so small values need to be clamped to correctly reflect the timeout.
199 + */
200 +static inline unsigned int div_round_ticks(unsigned int val, unsigned int tick_duration,
201 + unsigned int min_ticks)
202 +{
203 + return max(min_ticks, DIV_ROUND_UP(val, tick_duration));
204 +}
205 +
206 +static int otto_wdt_determine_timeouts(struct watchdog_device *wdev, unsigned int timeout,
207 + unsigned int pretimeout)
208 +{
209 + struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
210 + unsigned int pretimeout_ms = pretimeout * 1000;
211 + unsigned int timeout_ms = timeout * 1000;
212 + unsigned int prescale_next = 0;
213 + unsigned int phase1_ticks;
214 + unsigned int phase2_ticks;
215 + unsigned int total_ticks;
216 + unsigned int prescale;
217 + unsigned int tick_ms;
218 + u32 v;
219 +
220 + do {
221 + prescale = prescale_next;
222 + if (prescale > OTTO_WDT_PRESCALE_MAX)
223 + return -EINVAL;
224 +
225 + tick_ms = otto_wdt_tick_ms(ctrl, prescale);
226 + total_ticks = div_round_ticks(timeout_ms, tick_ms, 2);
227 + phase1_ticks = div_round_ticks(timeout_ms - pretimeout_ms, tick_ms, 1);
228 + phase2_ticks = total_ticks - phase1_ticks;
229 +
230 + prescale_next++;
231 + } while (phase1_ticks > OTTO_WDT_PHASE_TICKS_MAX
232 + || phase2_ticks > OTTO_WDT_PHASE_TICKS_MAX);
233 +
234 + v = ioread32(ctrl->base + OTTO_WDT_REG_CTRL);
235 +
236 + v &= ~(OTTO_WDT_CTRL_PRESCALE | OTTO_WDT_CTRL_PHASE1 | OTTO_WDT_CTRL_PHASE2);
237 + v |= FIELD_PREP(OTTO_WDT_CTRL_PHASE1, phase1_ticks - 1);
238 + v |= FIELD_PREP(OTTO_WDT_CTRL_PHASE2, phase2_ticks - 1);
239 + v |= FIELD_PREP(OTTO_WDT_CTRL_PRESCALE, prescale);
240 +
241 + iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
242 +
243 + timeout_ms = total_ticks * tick_ms;
244 + ctrl->wdev.timeout = timeout_ms / 1000;
245 +
246 + pretimeout_ms = phase2_ticks * tick_ms;
247 + ctrl->wdev.pretimeout = pretimeout_ms / 1000;
248 +
249 + return 0;
250 +}
251 +
252 +static int otto_wdt_set_timeout(struct watchdog_device *wdev, unsigned int val)
253 +{
254 + return otto_wdt_determine_timeouts(wdev, val, min(wdev->pretimeout, val - 1));
255 +}
256 +
257 +static int otto_wdt_set_pretimeout(struct watchdog_device *wdev, unsigned int val)
258 +{
259 + return otto_wdt_determine_timeouts(wdev, wdev->timeout, val);
260 +}
261 +
262 +static int otto_wdt_restart(struct watchdog_device *wdev, unsigned long reboot_mode,
263 + void *data)
264 +{
265 + struct otto_wdt_ctrl *ctrl = watchdog_get_drvdata(wdev);
266 + u32 reset_mode;
267 + u32 v;
268 +
269 + disable_irq(ctrl->irq_phase1);
270 +
271 + switch (reboot_mode) {
272 + case REBOOT_SOFT:
273 + reset_mode = OTTO_WDT_MODE_SOFTWARE;
274 + break;
275 + case REBOOT_WARM:
276 + reset_mode = OTTO_WDT_MODE_CPU;
277 + break;
278 + default:
279 + reset_mode = OTTO_WDT_MODE_SOC;
280 + break;
281 + }
282 +
283 + /* Configure for shortest timeout and wait for reset to occur */
284 + v = FIELD_PREP(OTTO_WDT_CTRL_RST_MODE, reset_mode) | OTTO_WDT_CTRL_ENABLE;
285 + iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
286 +
287 + mdelay(3 * otto_wdt_tick_ms(ctrl, 0));
288 +
289 + return 0;
290 +}
291 +
292 +static irqreturn_t otto_wdt_phase1_isr(int irq, void *dev_id)
293 +{
294 + struct otto_wdt_ctrl *ctrl = dev_id;
295 +
296 + iowrite32(OTTO_WDT_INTR_PHASE_1, ctrl->base + OTTO_WDT_REG_INTR);
297 + dev_crit(ctrl->dev, "phase 1 timeout\n");
298 + watchdog_notify_pretimeout(&ctrl->wdev);
299 +
300 + return IRQ_HANDLED;
301 +}
302 +
303 +static const struct watchdog_ops otto_wdt_ops = {
304 + .owner = THIS_MODULE,
305 + .start = otto_wdt_start,
306 + .stop = otto_wdt_stop,
307 + .ping = otto_wdt_ping,
308 + .set_timeout = otto_wdt_set_timeout,
309 + .set_pretimeout = otto_wdt_set_pretimeout,
310 + .restart = otto_wdt_restart,
311 +};
312 +
313 +static const struct watchdog_info otto_wdt_info = {
314 + .identity = "Realtek Otto watchdog timer",
315 + .options = WDIOF_KEEPALIVEPING |
316 + WDIOF_MAGICCLOSE |
317 + WDIOF_SETTIMEOUT |
318 + WDIOF_PRETIMEOUT,
319 +};
320 +
321 +static void otto_wdt_clock_action(void *data)
322 +{
323 + clk_disable_unprepare(data);
324 +}
325 +
326 +static int otto_wdt_probe_clk(struct otto_wdt_ctrl *ctrl)
327 +{
328 + struct clk *clk = devm_clk_get(ctrl->dev, NULL);
329 + int ret;
330 +
331 + if (IS_ERR(clk))
332 + return dev_err_probe(ctrl->dev, PTR_ERR(clk), "Failed to get clock\n");
333 +
334 + ret = clk_prepare_enable(clk);
335 + if (ret)
336 + return dev_err_probe(ctrl->dev, ret, "Failed to enable clock\n");
337 +
338 + ret = devm_add_action_or_reset(ctrl->dev, otto_wdt_clock_action, clk);
339 + if (ret)
340 + return ret;
341 +
342 + ctrl->clk_rate_khz = clk_get_rate(clk) / 1000;
343 + if (ctrl->clk_rate_khz == 0)
344 + return dev_err_probe(ctrl->dev, -ENXIO, "Failed to get clock rate\n");
345 +
346 + return 0;
347 +}
348 +
349 +static int otto_wdt_probe_reset_mode(struct otto_wdt_ctrl *ctrl)
350 +{
351 + static const char *mode_property = "realtek,reset-mode";
352 + const struct fwnode_handle *node = ctrl->dev->fwnode;
353 + int mode_count;
354 + u32 mode;
355 + u32 v;
356 +
357 + if (!node)
358 + return -ENXIO;
359 +
360 + mode_count = fwnode_property_string_array_count(node, mode_property);
361 + if (mode_count < 0)
362 + return mode_count;
363 + else if (mode_count == 0)
364 + return 0;
365 + else if (mode_count != 1)
366 + return -EINVAL;
367 +
368 + if (fwnode_property_match_string(node, mode_property, "soc") == 0)
369 + mode = OTTO_WDT_MODE_SOC;
370 + else if (fwnode_property_match_string(node, mode_property, "cpu") == 0)
371 + mode = OTTO_WDT_MODE_CPU;
372 + else if (fwnode_property_match_string(node, mode_property, "software") == 0)
373 + mode = OTTO_WDT_MODE_SOFTWARE;
374 + else
375 + return -EINVAL;
376 +
377 + v = ioread32(ctrl->base + OTTO_WDT_REG_CTRL);
378 + v &= ~OTTO_WDT_CTRL_RST_MODE;
379 + v |= FIELD_PREP(OTTO_WDT_CTRL_RST_MODE, mode);
380 + iowrite32(v, ctrl->base + OTTO_WDT_REG_CTRL);
381 +
382 + return 0;
383 +}
384 +
385 +static int otto_wdt_probe(struct platform_device *pdev)
386 +{
387 + struct device *dev = &pdev->dev;
388 + struct otto_wdt_ctrl *ctrl;
389 + unsigned int max_tick_ms;
390 + int ret;
391 +
392 + ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
393 + if (!ctrl)
394 + return -ENOMEM;
395 +
396 + ctrl->dev = dev;
397 + ctrl->base = devm_platform_ioremap_resource(pdev, 0);
398 + if (IS_ERR(ctrl->base))
399 + return PTR_ERR(ctrl->base);
400 +
401 + /* Clear any old interrupts and reset initial state */
402 + iowrite32(OTTO_WDT_INTR_PHASE_1 | OTTO_WDT_INTR_PHASE_2,
403 + ctrl->base + OTTO_WDT_REG_INTR);
404 + iowrite32(OTTO_WDT_CTRL_DEFAULT, ctrl->base + OTTO_WDT_REG_CTRL);
405 +
406 + ret = otto_wdt_probe_clk(ctrl);
407 + if (ret)
408 + return ret;
409 +
410 + ctrl->irq_phase1 = platform_get_irq_byname(pdev, "phase1");
411 + if (ctrl->irq_phase1 < 0)
412 + return ctrl->irq_phase1;
413 +
414 + ret = devm_request_irq(dev, ctrl->irq_phase1, otto_wdt_phase1_isr, 0,
415 + "realtek-otto-wdt", ctrl);
416 + if (ret)
417 + return dev_err_probe(dev, ret, "Failed to get IRQ for phase1\n");
418 +
419 + ret = otto_wdt_probe_reset_mode(ctrl);
420 + if (ret)
421 + return dev_err_probe(dev, ret, "Invalid reset mode specified\n");
422 +
423 + ctrl->wdev.parent = dev;
424 + ctrl->wdev.info = &otto_wdt_info;
425 + ctrl->wdev.ops = &otto_wdt_ops;
426 +
427 + /*
428 + * Since pretimeout cannot be disabled, min. timeout is twice the
429 + * subsystem resolution. Max. timeout is ca. 43s at a bus clock of 200MHz.
430 + */
431 + ctrl->wdev.min_timeout = 2;
432 + max_tick_ms = otto_wdt_tick_ms(ctrl, OTTO_WDT_PRESCALE_MAX);
433 + ctrl->wdev.max_hw_heartbeat_ms = max_tick_ms * OTTO_WDT_TIMEOUT_TICKS_MAX;
434 + ctrl->wdev.timeout = min(30U, ctrl->wdev.max_hw_heartbeat_ms / 1000);
435 +
436 + watchdog_set_drvdata(&ctrl->wdev, ctrl);
437 + watchdog_init_timeout(&ctrl->wdev, 0, dev);
438 + watchdog_stop_on_reboot(&ctrl->wdev);
439 + watchdog_set_restart_priority(&ctrl->wdev, 128);
440 +
441 + ret = otto_wdt_determine_timeouts(&ctrl->wdev, ctrl->wdev.timeout, 1);
442 + if (ret)
443 + return dev_err_probe(dev, ret, "Failed to set timeout\n");
444 +
445 + return devm_watchdog_register_device(dev, &ctrl->wdev);
446 +}
447 +
448 +static const struct of_device_id otto_wdt_ids[] = {
449 + { .compatible = "realtek,rtl8380-wdt" },
450 + { .compatible = "realtek,rtl8390-wdt" },
451 + { .compatible = "realtek,rtl9300-wdt" },
452 + { }
453 +};
454 +MODULE_DEVICE_TABLE(of, otto_wdt_ids);
455 +
456 +static struct platform_driver otto_wdt_driver = {
457 + .probe = otto_wdt_probe,
458 + .driver = {
459 + .name = "realtek-otto-watchdog",
460 + .of_match_table = otto_wdt_ids,
461 + },
462 +};
463 +module_platform_driver(otto_wdt_driver);
464 +
465 +MODULE_LICENSE("GPL v2");
466 +MODULE_AUTHOR("Sander Vanheule <sander@svanheule.net>");
467 +MODULE_DESCRIPTION("Realtek Otto watchdog timer driver");