xref: /linux/drivers/watchdog/sbsa_gwdt.c (revision b152ca999c5f11fefdd156a1182e022fa22ebed3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SBSA(Server Base System Architecture) Generic Watchdog driver
4  *
5  * Copyright (c) 2015, Linaro Ltd.
6  * Author: Fu Wei <fu.wei@linaro.org>
7  *         Suravee Suthikulpanit <Suravee.Suthikulpanit@amd.com>
8  *         Al Stone <al.stone@linaro.org>
9  *         Timur Tabi <timur@codeaurora.org>
10  *
11  * ARM SBSA Generic Watchdog has two stage timeouts:
12  * the first signal (WS0) is for alerting the system by interrupt,
13  * the second one (WS1) is a real hardware reset.
14  * More details about the hardware specification of this device:
15  * ARM DEN0029B - Server Base System Architecture (SBSA)
16  *
17  * This driver can operate ARM SBSA Generic Watchdog as a single stage watchdog
18  * or a two stages watchdog, it's set up by the module parameter "action".
19  * In the single stage mode, when the timeout is reached, your system
20  * will be reset by WS1. The first signal (WS0) is ignored.
21  * In the two stages mode, when the timeout is reached, the first signal (WS0)
22  * will trigger panic. If the system is getting into trouble and cannot be reset
23  * by panic or restart properly by the kdump kernel(if supported), then the
24  * second stage (as long as the first stage) will be reached, system will be
25  * reset by WS1. This function can help administrator to backup the system
26  * context info by panic console output or kdump.
27  *
28  * SBSA GWDT:
29  * if action is 1 (the two stages mode):
30  * |--------WOR-------WS0--------WOR-------WS1
31  * |----timeout-----(panic)----timeout-----reset
32  *
33  * if action is 0 (the single stage mode):
34  * |------WOR-----WS0(ignored)-----WOR------WS1
35  * |--------------timeout-------------------reset
36  *
37  * Note: Since this watchdog timer has two stages, and each stage is determined
38  * by WOR, in the single stage mode, the timeout is (WOR * 2); in the two
39  * stages mode, the timeout is WOR. The maximum timeout in the two stages mode
40  * is half of that in the single stage mode.
41  */
42 
43 #include <linux/io.h>
44 #include <linux/io-64-nonatomic-lo-hi.h>
45 #include <linux/interrupt.h>
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/platform_device.h>
49 #include <linux/stringify.h>
50 #include <linux/uaccess.h>
51 #include <linux/watchdog.h>
52 #include <asm/arch_timer.h>
53 
54 #define DRV_NAME		"sbsa-gwdt"
55 #define WATCHDOG_NAME		"SBSA Generic Watchdog"
56 
57 /* SBSA Generic Watchdog register definitions */
58 /* refresh frame */
59 #define SBSA_GWDT_WRR		0x000
60 
61 /* control frame */
62 #define SBSA_GWDT_WCS		0x000
63 #define SBSA_GWDT_WOR		0x008
64 #define SBSA_GWDT_WCV		0x010
65 
66 /* refresh/control frame */
67 #define SBSA_GWDT_W_IIDR	0xfcc
68 #define SBSA_GWDT_IDR		0xfd0
69 
70 /* Watchdog Control and Status Register */
71 #define SBSA_GWDT_WCS_EN	BIT(0)
72 #define SBSA_GWDT_WCS_WS0	BIT(1)
73 #define SBSA_GWDT_WCS_WS1	BIT(2)
74 
75 #define SBSA_GWDT_VERSION_MASK  GENMASK(3, 0)
76 #define SBSA_GWDT_VERSION_SHIFT 16
77 
78 #define SBSA_GWDT_IMPL_MASK	GENMASK(11, 0)
79 #define SBSA_GWDT_IMPL_SHIFT	0
80 #define SBSA_GWDT_IMPL_MEDIATEK	0x426
81 
82 /**
83  * struct sbsa_gwdt - Internal representation of the SBSA GWDT
84  * @wdd:		kernel watchdog_device structure
85  * @clk:		store the System Counter clock frequency, in Hz.
86  * @version:            store the architecture version
87  * @need_ws0_race_workaround:
88  *			indicate whether to adjust wdd->timeout to avoid a race with WS0
89  * @refresh_base:	Virtual address of the watchdog refresh frame
90  * @control_base:	Virtual address of the watchdog control frame
91  */
92 struct sbsa_gwdt {
93 	struct watchdog_device	wdd;
94 	u32			clk;
95 	int			version;
96 	bool			need_ws0_race_workaround;
97 	void __iomem		*refresh_base;
98 	void __iomem		*control_base;
99 };
100 
101 #define DEFAULT_TIMEOUT		10 /* seconds */
102 
103 static unsigned int timeout;
104 module_param(timeout, uint, 0);
105 MODULE_PARM_DESC(timeout,
106 		 "Watchdog timeout in seconds. (>=0, default="
107 		 __stringify(DEFAULT_TIMEOUT) ")");
108 
109 /*
110  * action refers to action taken when watchdog gets WS0
111  * 0 = skip
112  * 1 = panic
113  * defaults to skip (0)
114  */
115 static int action;
116 module_param(action, int, 0);
117 MODULE_PARM_DESC(action, "after watchdog gets WS0 interrupt, do: "
118 		 "0 = skip(*)  1 = panic");
119 
120 static bool nowayout = WATCHDOG_NOWAYOUT;
121 module_param(nowayout, bool, S_IRUGO);
122 MODULE_PARM_DESC(nowayout,
123 		 "Watchdog cannot be stopped once started (default="
124 		 __stringify(WATCHDOG_NOWAYOUT) ")");
125 
126 static bool early_enable;
127 module_param(early_enable, bool, 0);
128 MODULE_PARM_DESC(early_enable,
129 		 "Watchdog is started on module insertion (default=0)");
130 
131 /*
132  * Arm Base System Architecture 1.0 introduces watchdog v1 which
133  * increases the length watchdog offset register to 48 bits.
134  * - For version 0: WOR is 32 bits;
135  * - For version 1: WOR is 48 bits which comprises the register
136  * offset 0x8 and 0xC, and the bits [63:48] are reserved which are
137  * Read-As-Zero and Writes-Ignored.
138  */
139 static u64 sbsa_gwdt_reg_read(struct sbsa_gwdt *gwdt)
140 {
141 	if (gwdt->version == 0)
142 		return readl(gwdt->control_base + SBSA_GWDT_WOR);
143 	else
144 		return lo_hi_readq(gwdt->control_base + SBSA_GWDT_WOR);
145 }
146 
147 static void sbsa_gwdt_reg_write(u64 val, struct sbsa_gwdt *gwdt)
148 {
149 	if (gwdt->version == 0)
150 		writel((u32)val, gwdt->control_base + SBSA_GWDT_WOR);
151 	else
152 		lo_hi_writeq(val, gwdt->control_base + SBSA_GWDT_WOR);
153 }
154 
155 /*
156  * watchdog operation functions
157  */
158 static int sbsa_gwdt_set_timeout(struct watchdog_device *wdd,
159 				 unsigned int timeout)
160 {
161 	struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
162 
163 	wdd->timeout = timeout;
164 	timeout = clamp_t(unsigned int, timeout, 1, wdd->max_hw_heartbeat_ms / 1000);
165 
166 	if (action)
167 		sbsa_gwdt_reg_write((u64)gwdt->clk * timeout, gwdt);
168 	else
169 		/*
170 		 * In the single stage mode, The first signal (WS0) is ignored,
171 		 * the timeout is (WOR * 2), so the WOR should be configured
172 		 * to half value of timeout.
173 		 */
174 		sbsa_gwdt_reg_write(((u64)gwdt->clk / 2) * timeout, gwdt);
175 
176 	/*
177 	 * Some watchdog hardware has a race condition where it will ignore
178 	 * sbsa_gwdt_keepalive() if it is called at the exact moment that a
179 	 * timeout occurs and WS0 is being asserted. Unfortunately, the default
180 	 * behavior of the watchdog core is very likely to trigger this race
181 	 * when action=0 because it programs WOR to be half of the desired
182 	 * timeout, and watchdog_next_keepalive() chooses the exact same time to
183 	 * send keepalive pings.
184 	 *
185 	 * This triggers a race where sbsa_gwdt_keepalive() can be called right
186 	 * as WS0 is being asserted, and affected hardware will ignore that
187 	 * write and continue to assert WS0. After another (timeout / 2)
188 	 * seconds, the same race happens again. If the driver wins then the
189 	 * explicit refresh will reset WS0 to false but if the hardware wins,
190 	 * then WS1 is asserted and the system resets.
191 	 *
192 	 * Avoid the problem by scheduling keepalive heartbeats one second later
193 	 * than the WOR timeout.
194 	 *
195 	 * This workaround might not be needed in a future revision of the
196 	 * hardware.
197 	 */
198 	if (gwdt->need_ws0_race_workaround)
199 		wdd->min_hw_heartbeat_ms = timeout * 500 + 1000;
200 
201 	return 0;
202 }
203 
204 static unsigned int sbsa_gwdt_get_timeleft(struct watchdog_device *wdd)
205 {
206 	struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
207 	u64 timeleft = 0;
208 
209 	/*
210 	 * In the single stage mode, if WS0 is deasserted
211 	 * (watchdog is in the first stage),
212 	 * timeleft = WOR + (WCV - system counter)
213 	 */
214 	if (!action &&
215 	    !(readl(gwdt->control_base + SBSA_GWDT_WCS) & SBSA_GWDT_WCS_WS0))
216 		timeleft += sbsa_gwdt_reg_read(gwdt);
217 
218 	timeleft += lo_hi_readq(gwdt->control_base + SBSA_GWDT_WCV) -
219 		    arch_timer_read_counter();
220 
221 	do_div(timeleft, gwdt->clk);
222 
223 	return timeleft;
224 }
225 
226 static int sbsa_gwdt_keepalive(struct watchdog_device *wdd)
227 {
228 	struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
229 
230 	/*
231 	 * Writing WRR for an explicit watchdog refresh.
232 	 * You can write anyting (like 0).
233 	 */
234 	writel(0, gwdt->refresh_base + SBSA_GWDT_WRR);
235 
236 	return 0;
237 }
238 
239 static void sbsa_gwdt_get_version(struct watchdog_device *wdd)
240 {
241 	struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
242 	int iidr, ver, impl;
243 
244 	iidr = readl(gwdt->control_base + SBSA_GWDT_W_IIDR);
245 	ver = (iidr >> SBSA_GWDT_VERSION_SHIFT) & SBSA_GWDT_VERSION_MASK;
246 	impl = (iidr >> SBSA_GWDT_IMPL_SHIFT) & SBSA_GWDT_IMPL_MASK;
247 
248 	gwdt->version = ver;
249 	gwdt->need_ws0_race_workaround =
250 		!action && (impl == SBSA_GWDT_IMPL_MEDIATEK);
251 }
252 
253 static int sbsa_gwdt_start(struct watchdog_device *wdd)
254 {
255 	struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
256 
257 	/* writing WCS will cause an explicit watchdog refresh */
258 	writel(SBSA_GWDT_WCS_EN, gwdt->control_base + SBSA_GWDT_WCS);
259 
260 	return 0;
261 }
262 
263 static int sbsa_gwdt_stop(struct watchdog_device *wdd)
264 {
265 	struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
266 
267 	/* Simply write 0 to WCS to clean WCS_EN bit */
268 	writel(0, gwdt->control_base + SBSA_GWDT_WCS);
269 
270 	return 0;
271 }
272 
273 static irqreturn_t sbsa_gwdt_interrupt(int irq, void *dev_id)
274 {
275 	panic(WATCHDOG_NAME " timeout");
276 
277 	return IRQ_HANDLED;
278 }
279 
280 static const struct watchdog_info sbsa_gwdt_info = {
281 	.identity	= WATCHDOG_NAME,
282 	.options	= WDIOF_SETTIMEOUT |
283 			  WDIOF_KEEPALIVEPING |
284 			  WDIOF_MAGICCLOSE |
285 			  WDIOF_CARDRESET,
286 };
287 
288 static const struct watchdog_ops sbsa_gwdt_ops = {
289 	.owner		= THIS_MODULE,
290 	.start		= sbsa_gwdt_start,
291 	.stop		= sbsa_gwdt_stop,
292 	.ping		= sbsa_gwdt_keepalive,
293 	.set_timeout	= sbsa_gwdt_set_timeout,
294 	.get_timeleft	= sbsa_gwdt_get_timeleft,
295 };
296 
297 static int sbsa_gwdt_probe(struct platform_device *pdev)
298 {
299 	void __iomem *rf_base, *cf_base;
300 	struct device *dev = &pdev->dev;
301 	struct watchdog_device *wdd;
302 	struct sbsa_gwdt *gwdt;
303 	int ret, irq;
304 	u32 status;
305 	bool early_action;
306 
307 	gwdt = devm_kzalloc(dev, sizeof(*gwdt), GFP_KERNEL);
308 	if (!gwdt)
309 		return -ENOMEM;
310 	platform_set_drvdata(pdev, gwdt);
311 
312 	cf_base = devm_platform_ioremap_resource(pdev, 0);
313 	if (IS_ERR(cf_base))
314 		return PTR_ERR(cf_base);
315 
316 	rf_base = devm_platform_ioremap_resource(pdev, 1);
317 	if (IS_ERR(rf_base))
318 		return PTR_ERR(rf_base);
319 
320 	/*
321 	 * Get the frequency of system counter from the cp15 interface of ARM
322 	 * Generic timer. We don't need to check it, because if it returns "0",
323 	 * system would panic in very early stage.
324 	 */
325 	gwdt->clk = arch_timer_get_cntfrq();
326 	gwdt->refresh_base = rf_base;
327 	gwdt->control_base = cf_base;
328 
329 	wdd = &gwdt->wdd;
330 	wdd->parent = dev;
331 	wdd->info = &sbsa_gwdt_info;
332 	wdd->ops = &sbsa_gwdt_ops;
333 	wdd->min_timeout = 1;
334 	wdd->timeout = DEFAULT_TIMEOUT;
335 	watchdog_set_drvdata(wdd, gwdt);
336 	watchdog_set_nowayout(wdd, nowayout);
337 	sbsa_gwdt_get_version(wdd);
338 	if (gwdt->version == 0)
339 		wdd->max_hw_heartbeat_ms = U32_MAX / gwdt->clk * 1000;
340 	else
341 		wdd->max_hw_heartbeat_ms = GENMASK_ULL(47, 0) / gwdt->clk * 1000;
342 
343 	if (gwdt->need_ws0_race_workaround) {
344 		/*
345 		 * A timeout of 3 seconds means that WOR will be set to 1.5
346 		 * seconds and the heartbeat will be scheduled every 2.5
347 		 * seconds.
348 		 */
349 		wdd->min_timeout = 3;
350 	}
351 
352 	status = readl(cf_base + SBSA_GWDT_WCS);
353 	if (status & SBSA_GWDT_WCS_WS1) {
354 		dev_warn(dev, "System reset by WDT.\n");
355 		wdd->bootstatus |= WDIOF_CARDRESET;
356 	}
357 	if (status & SBSA_GWDT_WCS_EN)
358 		set_bit(WDOG_HW_RUNNING, &wdd->status);
359 
360 	if (action) {
361 		irq = platform_get_irq(pdev, 0);
362 		if (irq < 0) {
363 			action = 0;
364 			dev_warn(dev, "unable to get ws0 interrupt.\n");
365 		} else {
366 			/*
367 			 * In case there is a pending ws0 interrupt, just ping
368 			 * the watchdog before registering the interrupt routine
369 			 */
370 			writel(0, rf_base + SBSA_GWDT_WRR);
371 			if (devm_request_irq(dev, irq, sbsa_gwdt_interrupt, 0,
372 					     pdev->name, gwdt)) {
373 				action = 0;
374 				dev_warn(dev, "unable to request IRQ %d.\n",
375 					 irq);
376 			}
377 		}
378 		if (!action)
379 			dev_warn(dev, "falling back to single stage mode.\n");
380 	}
381 	/*
382 	 * In the single stage mode, The first signal (WS0) is ignored,
383 	 * the timeout is (WOR * 2), so the maximum timeout should be doubled.
384 	 */
385 	if (!action)
386 		wdd->max_hw_heartbeat_ms *= 2;
387 
388 	watchdog_init_timeout(wdd, timeout, dev);
389 	/*
390 	 * Update timeout to WOR.
391 	 * Because of the explicit watchdog refresh mechanism,
392 	 * it's also a ping, if watchdog is enabled.
393 	 */
394 	sbsa_gwdt_set_timeout(wdd, wdd->timeout);
395 
396 	early_action = early_enable && !(status & SBSA_GWDT_WCS_EN);
397 	if (early_action) {
398 		sbsa_gwdt_start(wdd);
399 		set_bit(WDOG_HW_RUNNING, &wdd->status);
400 	}
401 
402 	watchdog_stop_on_reboot(wdd);
403 	ret = devm_watchdog_register_device(dev, wdd);
404 	if (ret) {
405 		if (early_action)
406 			sbsa_gwdt_stop(wdd);
407 		return ret;
408 	}
409 
410 	dev_info(dev, "Initialized with %ds timeout @ %u Hz, action=%d.%s\n",
411 		 wdd->timeout, gwdt->clk, action,
412 		 watchdog_hw_running(wdd) ? " [enabled]" : "");
413 
414 	return 0;
415 }
416 
417 /* Disable watchdog if it is active during suspend */
418 static int __maybe_unused sbsa_gwdt_suspend(struct device *dev)
419 {
420 	struct sbsa_gwdt *gwdt = dev_get_drvdata(dev);
421 
422 	if (watchdog_hw_running(&gwdt->wdd))
423 		sbsa_gwdt_stop(&gwdt->wdd);
424 
425 	return 0;
426 }
427 
428 /* Enable watchdog if necessary */
429 static int __maybe_unused sbsa_gwdt_resume(struct device *dev)
430 {
431 	struct sbsa_gwdt *gwdt = dev_get_drvdata(dev);
432 
433 	if (watchdog_hw_running(&gwdt->wdd))
434 		sbsa_gwdt_start(&gwdt->wdd);
435 
436 	return 0;
437 }
438 
439 static const struct dev_pm_ops sbsa_gwdt_pm_ops = {
440 	SET_SYSTEM_SLEEP_PM_OPS(sbsa_gwdt_suspend, sbsa_gwdt_resume)
441 };
442 
443 static const struct of_device_id sbsa_gwdt_of_match[] = {
444 	{ .compatible = "arm,sbsa-gwdt", },
445 	{},
446 };
447 MODULE_DEVICE_TABLE(of, sbsa_gwdt_of_match);
448 
449 static const struct platform_device_id sbsa_gwdt_pdev_match[] = {
450 	{ .name = DRV_NAME, },
451 	{},
452 };
453 MODULE_DEVICE_TABLE(platform, sbsa_gwdt_pdev_match);
454 
455 static struct platform_driver sbsa_gwdt_driver = {
456 	.driver = {
457 		.name = DRV_NAME,
458 		.pm = &sbsa_gwdt_pm_ops,
459 		.of_match_table = sbsa_gwdt_of_match,
460 	},
461 	.probe = sbsa_gwdt_probe,
462 	.id_table = sbsa_gwdt_pdev_match,
463 };
464 
465 module_platform_driver(sbsa_gwdt_driver);
466 
467 MODULE_DESCRIPTION("SBSA Generic Watchdog Driver");
468 MODULE_AUTHOR("Fu Wei <fu.wei@linaro.org>");
469 MODULE_AUTHOR("Suravee Suthikulpanit <Suravee.Suthikulpanit@amd.com>");
470 MODULE_AUTHOR("Al Stone <al.stone@linaro.org>");
471 MODULE_AUTHOR("Timur Tabi <timur@codeaurora.org>");
472 MODULE_LICENSE("GPL v2");
473