1 // SPDX-License-Identifier: GPL-2.0+
3 * Watchdog driver for Atmel AT91SAM9x processors.
5 * Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr
10 * The Watchdog Timer Mode Register can be only written to once. If the
11 * timeout need to be set from Linux, be sure that the bootstrap or the
12 * bootloader doesn't write to this register.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/clk.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/platform_device.h>
26 #include <linux/reboot.h>
27 #include <linux/types.h>
28 #include <linux/watchdog.h>
29 #include <linux/jiffies.h>
30 #include <linux/timer.h>
31 #include <linux/bitops.h>
32 #include <linux/uaccess.h>
34 #include <linux/of_irq.h>
36 #include "at91sam9_wdt.h"
38 #define DRV_NAME "AT91SAM9 Watchdog"
40 #define wdt_read(wdt, field) \
41 readl_relaxed((wdt)->base + (field))
42 #define wdt_write(wtd, field, val) \
43 writel_relaxed((val), (wdt)->base + (field))
45 /* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
46 * use this to convert a watchdog
47 * value from/to milliseconds.
49 #define ticks_to_hz_rounddown(t) ((((t) + 1) * HZ) >> 8)
50 #define ticks_to_hz_roundup(t) (((((t) + 1) * HZ) + 255) >> 8)
51 #define ticks_to_secs(t) (((t) + 1) >> 8)
52 #define secs_to_ticks(s) ((s) ? (((s) << 8) - 1) : 0)
54 #define WDT_MR_RESET 0x3FFF2FFF
56 /* Watchdog max counter value in ticks */
57 #define WDT_COUNTER_MAX_TICKS 0xFFF
59 /* Watchdog max delta/value in secs */
60 #define WDT_COUNTER_MAX_SECS ticks_to_secs(WDT_COUNTER_MAX_TICKS)
62 /* Hardware timeout in seconds */
63 #define WDT_HW_TIMEOUT 2
65 /* Timer heartbeat (500ms) */
66 #define WDT_TIMEOUT (HZ/2)
68 /* User land timeout */
69 #define WDT_HEARTBEAT 15
71 module_param(heartbeat, int, 0);
72 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
73 "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
75 static bool nowayout = WATCHDOG_NOWAYOUT;
76 module_param(nowayout, bool, 0);
77 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
78 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
80 #define to_wdt(wdd) container_of(wdd, struct at91wdt, wdd)
82 struct watchdog_device wdd;
84 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
85 struct timer_list timer; /* The timer that pings the watchdog */
88 unsigned long heartbeat; /* WDT heartbeat in jiffies */
94 /* ......................................................................... */
96 static irqreturn_t wdt_interrupt(int irq, void *dev_id)
98 struct at91wdt *wdt = (struct at91wdt *)dev_id;
100 if (wdt_read(wdt, AT91_WDT_SR)) {
101 pr_crit("at91sam9 WDT software reset\n");
103 pr_crit("Reboot didn't ?????\n");
110 * Reload the watchdog timer. (ie, pat the watchdog)
112 static inline void at91_wdt_reset(struct at91wdt *wdt)
114 wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
120 static void at91_ping(struct timer_list *t)
122 struct at91wdt *wdt = from_timer(wdt, t, timer);
123 if (time_before(jiffies, wdt->next_heartbeat) ||
124 !watchdog_active(&wdt->wdd)) {
126 mod_timer(&wdt->timer, jiffies + wdt->heartbeat);
128 pr_crit("I will reset your machine !\n");
132 static int at91_wdt_start(struct watchdog_device *wdd)
134 struct at91wdt *wdt = to_wdt(wdd);
135 /* calculate when the next userspace timeout will be */
136 wdt->next_heartbeat = jiffies + wdd->timeout * HZ;
140 static int at91_wdt_stop(struct watchdog_device *wdd)
142 /* The watchdog timer hardware can not be stopped... */
146 static int at91_wdt_set_timeout(struct watchdog_device *wdd, unsigned int new_timeout)
148 wdd->timeout = new_timeout;
149 return at91_wdt_start(wdd);
152 static int at91_wdt_init(struct platform_device *pdev, struct at91wdt *wdt)
158 u32 mask = wdt->mr_mask;
159 unsigned long min_heartbeat = 1;
160 unsigned long max_heartbeat;
161 struct device *dev = &pdev->dev;
163 tmp = wdt_read(wdt, AT91_WDT_MR);
164 if ((tmp & mask) != (wdt->mr & mask)) {
165 if (tmp == WDT_MR_RESET) {
166 wdt_write(wdt, AT91_WDT_MR, wdt->mr);
167 tmp = wdt_read(wdt, AT91_WDT_MR);
171 if (tmp & AT91_WDT_WDDIS) {
172 if (wdt->mr & AT91_WDT_WDDIS)
174 dev_err(dev, "watchdog is disabled\n");
178 value = tmp & AT91_WDT_WDV;
179 delta = (tmp & AT91_WDT_WDD) >> 16;
182 min_heartbeat = ticks_to_hz_roundup(value - delta);
184 max_heartbeat = ticks_to_hz_rounddown(value);
185 if (!max_heartbeat) {
187 "heartbeat is too small for the system to handle it correctly\n");
192 * Try to reset the watchdog counter 4 or 2 times more often than
193 * actually requested, to avoid spurious watchdog reset.
194 * If this is not possible because of the min_heartbeat value, reset
195 * it at the min_heartbeat period.
197 if ((max_heartbeat / 4) >= min_heartbeat)
198 wdt->heartbeat = max_heartbeat / 4;
199 else if ((max_heartbeat / 2) >= min_heartbeat)
200 wdt->heartbeat = max_heartbeat / 2;
202 wdt->heartbeat = min_heartbeat;
204 if (max_heartbeat < min_heartbeat + 4)
206 "min heartbeat and max heartbeat might be too close for the system to handle it correctly\n");
208 if ((tmp & AT91_WDT_WDFIEN) && wdt->irq) {
209 err = devm_request_irq(dev, wdt->irq, wdt_interrupt,
210 IRQF_SHARED | IRQF_IRQPOLL | IRQF_NO_SUSPEND,
216 if ((tmp & wdt->mr_mask) != (wdt->mr & wdt->mr_mask))
218 "watchdog already configured differently (mr = %x expecting %x)\n",
219 tmp & wdt->mr_mask, wdt->mr & wdt->mr_mask);
221 timer_setup(&wdt->timer, at91_ping, 0);
224 * Use min_heartbeat the first time to avoid spurious watchdog reset:
225 * we don't know for how long the watchdog counter is running, and
226 * - resetting it right now might trigger a watchdog fault reset
227 * - waiting for heartbeat time might lead to a watchdog timeout
230 mod_timer(&wdt->timer, jiffies + min_heartbeat);
232 /* Try to set timeout from device tree first */
233 if (watchdog_init_timeout(&wdt->wdd, 0, dev))
234 watchdog_init_timeout(&wdt->wdd, heartbeat, dev);
235 watchdog_set_nowayout(&wdt->wdd, wdt->nowayout);
236 err = watchdog_register_device(&wdt->wdd);
240 wdt->next_heartbeat = jiffies + wdt->wdd.timeout * HZ;
245 del_timer(&wdt->timer);
249 /* ......................................................................... */
251 static const struct watchdog_info at91_wdt_info = {
252 .identity = DRV_NAME,
253 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
257 static const struct watchdog_ops at91_wdt_ops = {
258 .owner = THIS_MODULE,
259 .start = at91_wdt_start,
260 .stop = at91_wdt_stop,
261 .set_timeout = at91_wdt_set_timeout,
264 #if defined(CONFIG_OF)
265 static int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt)
268 u32 max = WDT_COUNTER_MAX_SECS;
271 /* Get the interrupts property */
272 wdt->irq = irq_of_parse_and_map(np, 0);
274 dev_warn(wdt->wdd.parent, "failed to get IRQ from DT\n");
276 if (!of_property_read_u32_index(np, "atmel,max-heartbeat-sec", 0,
278 if (!max || max > WDT_COUNTER_MAX_SECS)
279 max = WDT_COUNTER_MAX_SECS;
281 if (!of_property_read_u32_index(np, "atmel,min-heartbeat-sec",
288 min = secs_to_ticks(min);
289 max = secs_to_ticks(max);
291 wdt->mr_mask = 0x3FFFFFFF;
293 if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) &&
294 !strcmp(tmp, "software")) {
295 wdt->mr |= AT91_WDT_WDFIEN;
296 wdt->mr_mask &= ~AT91_WDT_WDRPROC;
298 wdt->mr |= AT91_WDT_WDRSTEN;
301 if (!of_property_read_string(np, "atmel,reset-type", &tmp) &&
302 !strcmp(tmp, "proc"))
303 wdt->mr |= AT91_WDT_WDRPROC;
305 if (of_property_read_bool(np, "atmel,disable")) {
306 wdt->mr |= AT91_WDT_WDDIS;
307 wdt->mr_mask &= AT91_WDT_WDDIS;
310 if (of_property_read_bool(np, "atmel,idle-halt"))
311 wdt->mr |= AT91_WDT_WDIDLEHLT;
313 if (of_property_read_bool(np, "atmel,dbg-halt"))
314 wdt->mr |= AT91_WDT_WDDBGHLT;
316 wdt->mr |= max | ((max - min) << 16);
321 static inline int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt)
327 static int __init at91wdt_probe(struct platform_device *pdev)
333 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
337 wdt->mr = (WDT_HW_TIMEOUT * 256) | AT91_WDT_WDRSTEN | AT91_WDT_WDD |
338 AT91_WDT_WDDBGHLT | AT91_WDT_WDIDLEHLT;
339 wdt->mr_mask = 0x3FFFFFFF;
340 wdt->nowayout = nowayout;
341 wdt->wdd.parent = &pdev->dev;
342 wdt->wdd.info = &at91_wdt_info;
343 wdt->wdd.ops = &at91_wdt_ops;
344 wdt->wdd.timeout = WDT_HEARTBEAT;
345 wdt->wdd.min_timeout = 1;
346 wdt->wdd.max_timeout = 0xFFFF;
348 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
349 wdt->base = devm_ioremap_resource(&pdev->dev, r);
350 if (IS_ERR(wdt->base))
351 return PTR_ERR(wdt->base);
353 wdt->sclk = devm_clk_get(&pdev->dev, NULL);
354 if (IS_ERR(wdt->sclk))
355 return PTR_ERR(wdt->sclk);
357 err = clk_prepare_enable(wdt->sclk);
359 dev_err(&pdev->dev, "Could not enable slow clock\n");
363 if (pdev->dev.of_node) {
364 err = of_at91wdt_init(pdev->dev.of_node, wdt);
369 err = at91_wdt_init(pdev, wdt);
373 platform_set_drvdata(pdev, wdt);
375 pr_info("enabled (heartbeat=%d sec, nowayout=%d)\n",
376 wdt->wdd.timeout, wdt->nowayout);
381 clk_disable_unprepare(wdt->sclk);
386 static int __exit at91wdt_remove(struct platform_device *pdev)
388 struct at91wdt *wdt = platform_get_drvdata(pdev);
389 watchdog_unregister_device(&wdt->wdd);
391 pr_warn("I quit now, hardware will probably reboot!\n");
392 del_timer(&wdt->timer);
393 clk_disable_unprepare(wdt->sclk);
398 #if defined(CONFIG_OF)
399 static const struct of_device_id at91_wdt_dt_ids[] = {
400 { .compatible = "atmel,at91sam9260-wdt" },
404 MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
407 static struct platform_driver at91wdt_driver = {
408 .remove = __exit_p(at91wdt_remove),
411 .of_match_table = of_match_ptr(at91_wdt_dt_ids),
415 module_platform_driver_probe(at91wdt_driver, at91wdt_probe);
417 MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>");
418 MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors");
419 MODULE_LICENSE("GPL");