1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Atmel SAMA5D4 Watchdog Timer
5 * Copyright (C) 2015-2019 Microchip Technology Inc. and its subsidiaries
8 #include <linux/delay.h>
9 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/of_irq.h>
15 #include <linux/platform_device.h>
16 #include <linux/reboot.h>
17 #include <linux/watchdog.h>
19 #include "at91sam9_wdt.h"
21 /* minimum and maximum watchdog timeout, in seconds */
22 #define MIN_WDT_TIMEOUT 1
23 #define MAX_WDT_TIMEOUT 16
24 #define WDT_DEFAULT_TIMEOUT MAX_WDT_TIMEOUT
26 #define WDT_SEC2TICKS(s) ((s) ? (((s) << 8) - 1) : 0)
29 struct watchdog_device wdd
;
30 void __iomem
*reg_base
;
33 unsigned long last_ping
;
38 static int wdt_timeout
;
39 static bool nowayout
= WATCHDOG_NOWAYOUT
;
41 module_param(wdt_timeout
, int, 0);
42 MODULE_PARM_DESC(wdt_timeout
,
43 "Watchdog timeout in seconds. (default = "
44 __MODULE_STRING(WDT_DEFAULT_TIMEOUT
) ")");
46 module_param(nowayout
, bool, 0);
47 MODULE_PARM_DESC(nowayout
,
48 "Watchdog cannot be stopped once started (default="
49 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
51 #define wdt_enabled (!(wdt->mr & AT91_WDT_WDDIS))
53 #define wdt_read(wdt, field) \
54 readl_relaxed((wdt)->reg_base + (field))
56 /* 4 slow clock periods is 4/32768 = 122.07µs*/
57 #define WDT_DELAY usecs_to_jiffies(123)
59 static void wdt_write(struct sama5d4_wdt
*wdt
, u32 field
, u32 val
)
62 * WDT_CR and WDT_MR must not be modified within three slow clock
63 * periods following a restart of the watchdog performed by a write
66 while (time_before(jiffies
, wdt
->last_ping
+ WDT_DELAY
))
67 usleep_range(30, 125);
68 writel_relaxed(val
, wdt
->reg_base
+ field
);
69 wdt
->last_ping
= jiffies
;
72 static void wdt_write_nosleep(struct sama5d4_wdt
*wdt
, u32 field
, u32 val
)
74 if (time_before(jiffies
, wdt
->last_ping
+ WDT_DELAY
))
76 writel_relaxed(val
, wdt
->reg_base
+ field
);
77 wdt
->last_ping
= jiffies
;
80 static int sama5d4_wdt_start(struct watchdog_device
*wdd
)
82 struct sama5d4_wdt
*wdt
= watchdog_get_drvdata(wdd
);
84 if (wdt
->sam9x60_support
) {
85 writel_relaxed(wdt
->ir
, wdt
->reg_base
+ AT91_SAM9X60_IER
);
86 wdt
->mr
&= ~AT91_SAM9X60_WDDIS
;
88 wdt
->mr
&= ~AT91_WDT_WDDIS
;
90 wdt_write(wdt
, AT91_WDT_MR
, wdt
->mr
);
95 static int sama5d4_wdt_stop(struct watchdog_device
*wdd
)
97 struct sama5d4_wdt
*wdt
= watchdog_get_drvdata(wdd
);
99 if (wdt
->sam9x60_support
) {
100 writel_relaxed(wdt
->ir
, wdt
->reg_base
+ AT91_SAM9X60_IDR
);
101 wdt
->mr
|= AT91_SAM9X60_WDDIS
;
103 wdt
->mr
|= AT91_WDT_WDDIS
;
105 wdt_write(wdt
, AT91_WDT_MR
, wdt
->mr
);
110 static int sama5d4_wdt_ping(struct watchdog_device
*wdd
)
112 struct sama5d4_wdt
*wdt
= watchdog_get_drvdata(wdd
);
114 wdt_write(wdt
, AT91_WDT_CR
, AT91_WDT_KEY
| AT91_WDT_WDRSTT
);
119 static int sama5d4_wdt_set_timeout(struct watchdog_device
*wdd
,
120 unsigned int timeout
)
122 struct sama5d4_wdt
*wdt
= watchdog_get_drvdata(wdd
);
123 u32 value
= WDT_SEC2TICKS(timeout
);
125 if (wdt
->sam9x60_support
) {
126 wdt_write(wdt
, AT91_SAM9X60_WLR
,
127 AT91_SAM9X60_SET_COUNTER(value
));
129 wdd
->timeout
= timeout
;
133 wdt
->mr
&= ~AT91_WDT_WDV
;
134 wdt
->mr
|= AT91_WDT_SET_WDV(value
);
137 * WDDIS has to be 0 when updating WDD/WDV. The datasheet states: When
138 * setting the WDDIS bit, and while it is set, the fields WDV and WDD
139 * must not be modified.
140 * If the watchdog is enabled, then the timeout can be updated. Else,
141 * wait that the user enables it.
144 wdt_write(wdt
, AT91_WDT_MR
, wdt
->mr
& ~AT91_WDT_WDDIS
);
146 wdd
->timeout
= timeout
;
151 static const struct watchdog_info sama5d4_wdt_info
= {
152 .options
= WDIOF_SETTIMEOUT
| WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
,
153 .identity
= "Atmel SAMA5D4 Watchdog",
156 static const struct watchdog_ops sama5d4_wdt_ops
= {
157 .owner
= THIS_MODULE
,
158 .start
= sama5d4_wdt_start
,
159 .stop
= sama5d4_wdt_stop
,
160 .ping
= sama5d4_wdt_ping
,
161 .set_timeout
= sama5d4_wdt_set_timeout
,
164 static irqreturn_t
sama5d4_wdt_irq_handler(int irq
, void *dev_id
)
166 struct sama5d4_wdt
*wdt
= platform_get_drvdata(dev_id
);
169 if (wdt
->sam9x60_support
)
170 reg
= wdt_read(wdt
, AT91_SAM9X60_ISR
);
172 reg
= wdt_read(wdt
, AT91_WDT_SR
);
175 pr_crit("Atmel Watchdog Software Reset\n");
177 pr_crit("Reboot didn't succeed\n");
183 static int of_sama5d4_wdt_init(struct device_node
*np
, struct sama5d4_wdt
*wdt
)
187 if (wdt
->sam9x60_support
)
188 wdt
->mr
= AT91_SAM9X60_WDDIS
;
190 wdt
->mr
= AT91_WDT_WDDIS
;
192 if (!of_property_read_string(np
, "atmel,watchdog-type", &tmp
) &&
193 !strcmp(tmp
, "software"))
194 wdt
->need_irq
= true;
196 if (of_property_read_bool(np
, "atmel,idle-halt"))
197 wdt
->mr
|= AT91_WDT_WDIDLEHLT
;
199 if (of_property_read_bool(np
, "atmel,dbg-halt"))
200 wdt
->mr
|= AT91_WDT_WDDBGHLT
;
205 static int sama5d4_wdt_init(struct sama5d4_wdt
*wdt
)
209 val
= WDT_SEC2TICKS(WDT_DEFAULT_TIMEOUT
);
211 * When booting and resuming, the bootloader may have changed the
212 * watchdog configuration.
213 * If the watchdog is already running, we can safely update it.
214 * Else, we have to disable it properly.
217 reg
= wdt_read(wdt
, AT91_WDT_MR
);
218 if (wdt
->sam9x60_support
&& (!(reg
& AT91_SAM9X60_WDDIS
)))
219 wdt_write_nosleep(wdt
, AT91_WDT_MR
,
220 reg
| AT91_SAM9X60_WDDIS
);
221 else if (!wdt
->sam9x60_support
&&
222 (!(reg
& AT91_WDT_WDDIS
)))
223 wdt_write_nosleep(wdt
, AT91_WDT_MR
,
224 reg
| AT91_WDT_WDDIS
);
227 if (wdt
->sam9x60_support
) {
229 wdt
->ir
= AT91_SAM9X60_PERINT
;
231 wdt
->mr
|= AT91_SAM9X60_PERIODRST
;
233 wdt_write(wdt
, AT91_SAM9X60_IER
, wdt
->ir
);
234 wdt_write(wdt
, AT91_SAM9X60_WLR
, AT91_SAM9X60_SET_COUNTER(val
));
236 wdt
->mr
|= AT91_WDT_SET_WDD(WDT_SEC2TICKS(MAX_WDT_TIMEOUT
));
237 wdt
->mr
|= AT91_WDT_SET_WDV(val
);
240 wdt
->mr
|= AT91_WDT_WDFIEN
;
242 wdt
->mr
|= AT91_WDT_WDRSTEN
;
245 wdt_write_nosleep(wdt
, AT91_WDT_MR
, wdt
->mr
);
250 static int sama5d4_wdt_probe(struct platform_device
*pdev
)
252 struct device
*dev
= &pdev
->dev
;
253 struct watchdog_device
*wdd
;
254 struct sama5d4_wdt
*wdt
;
260 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
265 wdd
->timeout
= WDT_DEFAULT_TIMEOUT
;
266 wdd
->info
= &sama5d4_wdt_info
;
267 wdd
->ops
= &sama5d4_wdt_ops
;
268 wdd
->min_timeout
= MIN_WDT_TIMEOUT
;
269 wdd
->max_timeout
= MAX_WDT_TIMEOUT
;
270 wdt
->last_ping
= jiffies
;
272 if (of_device_is_compatible(dev
->of_node
, "microchip,sam9x60-wdt") ||
273 of_device_is_compatible(dev
->of_node
, "microchip,sama7g5-wdt"))
274 wdt
->sam9x60_support
= true;
276 watchdog_set_drvdata(wdd
, wdt
);
278 regs
= devm_platform_ioremap_resource(pdev
, 0);
280 return PTR_ERR(regs
);
282 wdt
->reg_base
= regs
;
284 ret
= of_sama5d4_wdt_init(dev
->of_node
, wdt
);
289 irq
= irq_of_parse_and_map(dev
->of_node
, 0);
291 dev_warn(dev
, "failed to get IRQ from DT\n");
292 wdt
->need_irq
= false;
297 ret
= devm_request_irq(dev
, irq
, sama5d4_wdt_irq_handler
,
298 IRQF_SHARED
| IRQF_IRQPOLL
|
299 IRQF_NO_SUSPEND
, pdev
->name
, pdev
);
301 dev_err(dev
, "cannot register interrupt handler\n");
306 watchdog_init_timeout(wdd
, wdt_timeout
, dev
);
308 reg
= wdt_read(wdt
, AT91_WDT_MR
);
309 if (!(reg
& AT91_WDT_WDDIS
)) {
310 wdt
->mr
&= ~AT91_WDT_WDDIS
;
311 set_bit(WDOG_HW_RUNNING
, &wdd
->status
);
314 ret
= sama5d4_wdt_init(wdt
);
318 watchdog_set_nowayout(wdd
, nowayout
);
320 watchdog_stop_on_unregister(wdd
);
321 ret
= devm_watchdog_register_device(dev
, wdd
);
325 platform_set_drvdata(pdev
, wdt
);
327 dev_info(dev
, "initialized (timeout = %d sec, nowayout = %d)\n",
328 wdd
->timeout
, nowayout
);
333 static const struct of_device_id sama5d4_wdt_of_match
[] = {
335 .compatible
= "atmel,sama5d4-wdt",
338 .compatible
= "microchip,sam9x60-wdt",
341 .compatible
= "microchip,sama7g5-wdt",
346 MODULE_DEVICE_TABLE(of
, sama5d4_wdt_of_match
);
348 static int sama5d4_wdt_suspend_late(struct device
*dev
)
350 struct sama5d4_wdt
*wdt
= dev_get_drvdata(dev
);
352 if (watchdog_active(&wdt
->wdd
))
353 sama5d4_wdt_stop(&wdt
->wdd
);
358 static int sama5d4_wdt_resume_early(struct device
*dev
)
360 struct sama5d4_wdt
*wdt
= dev_get_drvdata(dev
);
363 * FIXME: writing MR also pings the watchdog which may not be desired.
364 * This should only be done when the registers are lost on suspend but
365 * there is no way to get this information right now.
367 sama5d4_wdt_init(wdt
);
369 if (watchdog_active(&wdt
->wdd
))
370 sama5d4_wdt_start(&wdt
->wdd
);
375 static const struct dev_pm_ops sama5d4_wdt_pm_ops
= {
376 LATE_SYSTEM_SLEEP_PM_OPS(sama5d4_wdt_suspend_late
,
377 sama5d4_wdt_resume_early
)
380 static struct platform_driver sama5d4_wdt_driver
= {
381 .probe
= sama5d4_wdt_probe
,
383 .name
= "sama5d4_wdt",
384 .pm
= pm_sleep_ptr(&sama5d4_wdt_pm_ops
),
385 .of_match_table
= sama5d4_wdt_of_match
,
388 module_platform_driver(sama5d4_wdt_driver
);
390 MODULE_AUTHOR("Atmel Corporation");
391 MODULE_DESCRIPTION("Atmel SAMA5D4 Watchdog Timer driver");
392 MODULE_LICENSE("GPL v2");