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