blob: d193a60430b2e6e134523d912b683517b1bc2ba3 [file] [log] [blame]
Thomas Gleixnerf50a7f32019-05-28 09:57:18 -07001// SPDX-License-Identifier: GPL-2.0-only
Wenyou Yang76534862015-08-06 18:16:46 +08002/*
3 * Driver for Atmel SAMA5D4 Watchdog Timer
4 *
5 * Copyright (C) 2015 Atmel Corporation
Wenyou Yang76534862015-08-06 18:16:46 +08006 */
7
Alexandre Belloniddd6d242017-03-02 18:31:12 +01008#include <linux/delay.h>
Wenyou Yang76534862015-08-06 18:16:46 +08009#include <linux/interrupt.h>
10#include <linux/io.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/of_irq.h>
15#include <linux/platform_device.h>
16#include <linux/reboot.h>
17#include <linux/watchdog.h>
18
19#include "at91sam9_wdt.h"
20
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
25
26#define WDT_SEC2TICKS(s) ((s) ? (((s) << 8) - 1) : 0)
27
28struct sama5d4_wdt {
29 struct watchdog_device wdd;
30 void __iomem *reg_base;
Alexandre Belloni722ce632017-01-30 18:18:47 +010031 u32 mr;
Alexandre Belloniddd6d242017-03-02 18:31:12 +010032 unsigned long last_ping;
Wenyou Yang76534862015-08-06 18:16:46 +080033};
34
Marcus Folkesson976932e2018-02-11 21:08:41 +010035static int wdt_timeout;
Wenyou Yang76534862015-08-06 18:16:46 +080036static bool nowayout = WATCHDOG_NOWAYOUT;
37
38module_param(wdt_timeout, int, 0);
39MODULE_PARM_DESC(wdt_timeout,
40 "Watchdog timeout in seconds. (default = "
41 __MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")");
42
43module_param(nowayout, bool, 0);
44MODULE_PARM_DESC(nowayout,
45 "Watchdog cannot be stopped once started (default="
46 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
47
Alexandre Belloni015b5282017-03-02 18:31:11 +010048#define wdt_enabled (!(wdt->mr & AT91_WDT_WDDIS))
49
Wenyou Yang76534862015-08-06 18:16:46 +080050#define wdt_read(wdt, field) \
51 readl_relaxed((wdt)->reg_base + (field))
52
Alexandre Belloniddd6d242017-03-02 18:31:12 +010053/* 4 slow clock periods is 4/32768 = 122.07µs*/
54#define WDT_DELAY usecs_to_jiffies(123)
55
56static void wdt_write(struct sama5d4_wdt *wdt, u32 field, u32 val)
57{
58 /*
59 * WDT_CR and WDT_MR must not be modified within three slow clock
60 * periods following a restart of the watchdog performed by a write
61 * access in WDT_CR.
62 */
63 while (time_before(jiffies, wdt->last_ping + WDT_DELAY))
64 usleep_range(30, 125);
65 writel_relaxed(val, wdt->reg_base + field);
66 wdt->last_ping = jiffies;
67}
68
69static void wdt_write_nosleep(struct sama5d4_wdt *wdt, u32 field, u32 val)
70{
71 if (time_before(jiffies, wdt->last_ping + WDT_DELAY))
72 udelay(123);
73 writel_relaxed(val, wdt->reg_base + field);
74 wdt->last_ping = jiffies;
75}
Wenyou Yang76534862015-08-06 18:16:46 +080076
77static int sama5d4_wdt_start(struct watchdog_device *wdd)
78{
79 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
Wenyou Yang76534862015-08-06 18:16:46 +080080
Alexandre Belloni722ce632017-01-30 18:18:47 +010081 wdt->mr &= ~AT91_WDT_WDDIS;
82 wdt_write(wdt, AT91_WDT_MR, wdt->mr);
Wenyou Yang76534862015-08-06 18:16:46 +080083
84 return 0;
85}
86
87static int sama5d4_wdt_stop(struct watchdog_device *wdd)
88{
89 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
Wenyou Yang76534862015-08-06 18:16:46 +080090
Alexandre Belloni722ce632017-01-30 18:18:47 +010091 wdt->mr |= AT91_WDT_WDDIS;
92 wdt_write(wdt, AT91_WDT_MR, wdt->mr);
Wenyou Yang76534862015-08-06 18:16:46 +080093
94 return 0;
95}
96
97static int sama5d4_wdt_ping(struct watchdog_device *wdd)
98{
99 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
100
101 wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
102
103 return 0;
104}
105
106static int sama5d4_wdt_set_timeout(struct watchdog_device *wdd,
107 unsigned int timeout)
108{
109 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
110 u32 value = WDT_SEC2TICKS(timeout);
Wenyou Yang76534862015-08-06 18:16:46 +0800111
Alexandre Belloni722ce632017-01-30 18:18:47 +0100112 wdt->mr &= ~AT91_WDT_WDV;
Alexandre Belloni722ce632017-01-30 18:18:47 +0100113 wdt->mr |= AT91_WDT_SET_WDV(value);
Alexandre Belloni015b5282017-03-02 18:31:11 +0100114
115 /*
116 * WDDIS has to be 0 when updating WDD/WDV. The datasheet states: When
117 * setting the WDDIS bit, and while it is set, the fields WDV and WDD
118 * must not be modified.
119 * If the watchdog is enabled, then the timeout can be updated. Else,
120 * wait that the user enables it.
121 */
122 if (wdt_enabled)
123 wdt_write(wdt, AT91_WDT_MR, wdt->mr & ~AT91_WDT_WDDIS);
Wenyou Yang76534862015-08-06 18:16:46 +0800124
125 wdd->timeout = timeout;
126
127 return 0;
128}
129
130static const struct watchdog_info sama5d4_wdt_info = {
131 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
132 .identity = "Atmel SAMA5D4 Watchdog",
133};
134
Bhumika Goyalb893e342017-01-28 13:11:17 +0530135static const struct watchdog_ops sama5d4_wdt_ops = {
Wenyou Yang76534862015-08-06 18:16:46 +0800136 .owner = THIS_MODULE,
137 .start = sama5d4_wdt_start,
138 .stop = sama5d4_wdt_stop,
139 .ping = sama5d4_wdt_ping,
140 .set_timeout = sama5d4_wdt_set_timeout,
141};
142
143static irqreturn_t sama5d4_wdt_irq_handler(int irq, void *dev_id)
144{
145 struct sama5d4_wdt *wdt = platform_get_drvdata(dev_id);
146
147 if (wdt_read(wdt, AT91_WDT_SR)) {
148 pr_crit("Atmel Watchdog Software Reset\n");
149 emergency_restart();
150 pr_crit("Reboot didn't succeed\n");
151 }
152
153 return IRQ_HANDLED;
154}
155
156static int of_sama5d4_wdt_init(struct device_node *np, struct sama5d4_wdt *wdt)
157{
158 const char *tmp;
159
Alexandre Belloni722ce632017-01-30 18:18:47 +0100160 wdt->mr = AT91_WDT_WDDIS;
Wenyou Yang76534862015-08-06 18:16:46 +0800161
162 if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) &&
163 !strcmp(tmp, "software"))
Alexandre Belloni722ce632017-01-30 18:18:47 +0100164 wdt->mr |= AT91_WDT_WDFIEN;
Wenyou Yang76534862015-08-06 18:16:46 +0800165 else
Alexandre Belloni722ce632017-01-30 18:18:47 +0100166 wdt->mr |= AT91_WDT_WDRSTEN;
Wenyou Yang76534862015-08-06 18:16:46 +0800167
168 if (of_property_read_bool(np, "atmel,idle-halt"))
Alexandre Belloni722ce632017-01-30 18:18:47 +0100169 wdt->mr |= AT91_WDT_WDIDLEHLT;
Wenyou Yang76534862015-08-06 18:16:46 +0800170
171 if (of_property_read_bool(np, "atmel,dbg-halt"))
Alexandre Belloni722ce632017-01-30 18:18:47 +0100172 wdt->mr |= AT91_WDT_WDDBGHLT;
Wenyou Yang76534862015-08-06 18:16:46 +0800173
174 return 0;
175}
176
177static int sama5d4_wdt_init(struct sama5d4_wdt *wdt)
178{
Wenyou Yang76534862015-08-06 18:16:46 +0800179 u32 reg;
Wenyou Yang76534862015-08-06 18:16:46 +0800180 /*
Alexandre Belloni015b5282017-03-02 18:31:11 +0100181 * When booting and resuming, the bootloader may have changed the
182 * watchdog configuration.
183 * If the watchdog is already running, we can safely update it.
184 * Else, we have to disable it properly.
Wenyou Yang76534862015-08-06 18:16:46 +0800185 */
Alexandre Belloni015b5282017-03-02 18:31:11 +0100186 if (wdt_enabled) {
Alexandre Belloniddd6d242017-03-02 18:31:12 +0100187 wdt_write_nosleep(wdt, AT91_WDT_MR, wdt->mr);
Alexandre Belloni015b5282017-03-02 18:31:11 +0100188 } else {
189 reg = wdt_read(wdt, AT91_WDT_MR);
190 if (!(reg & AT91_WDT_WDDIS))
Alexandre Belloniddd6d242017-03-02 18:31:12 +0100191 wdt_write_nosleep(wdt, AT91_WDT_MR,
192 reg | AT91_WDT_WDDIS);
Alexandre Belloni015b5282017-03-02 18:31:11 +0100193 }
Wenyou Yang76534862015-08-06 18:16:46 +0800194 return 0;
195}
196
197static int sama5d4_wdt_probe(struct platform_device *pdev)
198{
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700199 struct device *dev = &pdev->dev;
Wenyou Yang76534862015-08-06 18:16:46 +0800200 struct watchdog_device *wdd;
201 struct sama5d4_wdt *wdt;
Wenyou Yang76534862015-08-06 18:16:46 +0800202 void __iomem *regs;
203 u32 irq = 0;
Alexandre Belloni015b5282017-03-02 18:31:11 +0100204 u32 timeout;
Wenyou Yang76534862015-08-06 18:16:46 +0800205 int ret;
206
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700207 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
Wenyou Yang76534862015-08-06 18:16:46 +0800208 if (!wdt)
209 return -ENOMEM;
210
211 wdd = &wdt->wdd;
Marcus Folkesson976932e2018-02-11 21:08:41 +0100212 wdd->timeout = WDT_DEFAULT_TIMEOUT;
Wenyou Yang76534862015-08-06 18:16:46 +0800213 wdd->info = &sama5d4_wdt_info;
214 wdd->ops = &sama5d4_wdt_ops;
215 wdd->min_timeout = MIN_WDT_TIMEOUT;
216 wdd->max_timeout = MAX_WDT_TIMEOUT;
Alexandre Belloniddd6d242017-03-02 18:31:12 +0100217 wdt->last_ping = jiffies;
Wenyou Yang76534862015-08-06 18:16:46 +0800218
219 watchdog_set_drvdata(wdd, wdt);
220
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700221 regs = devm_platform_ioremap_resource(pdev, 0);
Wenyou Yang76534862015-08-06 18:16:46 +0800222 if (IS_ERR(regs))
223 return PTR_ERR(regs);
224
225 wdt->reg_base = regs;
226
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700227 irq = irq_of_parse_and_map(dev->of_node, 0);
Alexandre Belloni39bd56d2017-03-02 18:31:13 +0100228 if (!irq)
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700229 dev_warn(dev, "failed to get IRQ from DT\n");
Wenyou Yang76534862015-08-06 18:16:46 +0800230
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700231 ret = of_sama5d4_wdt_init(dev->of_node, wdt);
Alexandre Belloni39bd56d2017-03-02 18:31:13 +0100232 if (ret)
233 return ret;
Wenyou Yang76534862015-08-06 18:16:46 +0800234
Alexandre Belloni722ce632017-01-30 18:18:47 +0100235 if ((wdt->mr & AT91_WDT_WDFIEN) && irq) {
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700236 ret = devm_request_irq(dev, irq, sama5d4_wdt_irq_handler,
Wenyou Yang76534862015-08-06 18:16:46 +0800237 IRQF_SHARED | IRQF_IRQPOLL |
238 IRQF_NO_SUSPEND, pdev->name, pdev);
239 if (ret) {
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700240 dev_err(dev, "cannot register interrupt handler\n");
Wenyou Yang76534862015-08-06 18:16:46 +0800241 return ret;
242 }
243 }
244
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700245 watchdog_init_timeout(wdd, wdt_timeout, dev);
Wenyou Yang76534862015-08-06 18:16:46 +0800246
Alexandre Belloni015b5282017-03-02 18:31:11 +0100247 timeout = WDT_SEC2TICKS(wdd->timeout);
248
Eugen Hristev86329442019-05-08 14:15:03 +0000249 wdt->mr |= AT91_WDT_SET_WDD(WDT_SEC2TICKS(MAX_WDT_TIMEOUT));
Alexandre Belloni015b5282017-03-02 18:31:11 +0100250 wdt->mr |= AT91_WDT_SET_WDV(timeout);
251
Wenyou Yang76534862015-08-06 18:16:46 +0800252 ret = sama5d4_wdt_init(wdt);
253 if (ret)
254 return ret;
255
256 watchdog_set_nowayout(wdd, nowayout);
257
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700258 watchdog_stop_on_unregister(wdd);
259 ret = devm_watchdog_register_device(dev, wdd);
Wolfram Sang24b8eb72019-05-18 23:27:51 +0200260 if (ret)
Wenyou Yang76534862015-08-06 18:16:46 +0800261 return ret;
Wenyou Yang76534862015-08-06 18:16:46 +0800262
263 platform_set_drvdata(pdev, wdt);
264
Guenter Roeckdcc3ce02019-04-09 10:23:54 -0700265 dev_info(dev, "initialized (timeout = %d sec, nowayout = %d)\n",
Marcus Folkesson976932e2018-02-11 21:08:41 +0100266 wdd->timeout, nowayout);
Wenyou Yang76534862015-08-06 18:16:46 +0800267
268 return 0;
269}
270
Wenyou Yang76534862015-08-06 18:16:46 +0800271static const struct of_device_id sama5d4_wdt_of_match[] = {
272 { .compatible = "atmel,sama5d4-wdt", },
273 { }
274};
275MODULE_DEVICE_TABLE(of, sama5d4_wdt_of_match);
276
Alexandre Bellonif2013532017-01-30 18:18:48 +0100277#ifdef CONFIG_PM_SLEEP
Ken Sloat8d209eb2019-06-14 12:53:22 +0000278static int sama5d4_wdt_suspend_late(struct device *dev)
279{
280 struct sama5d4_wdt *wdt = dev_get_drvdata(dev);
281
282 if (watchdog_active(&wdt->wdd))
283 sama5d4_wdt_stop(&wdt->wdd);
284
285 return 0;
286}
287
288static int sama5d4_wdt_resume_early(struct device *dev)
Alexandre Bellonif2013532017-01-30 18:18:48 +0100289{
290 struct sama5d4_wdt *wdt = dev_get_drvdata(dev);
291
Alexandre Belloni5dca80f62017-03-02 18:31:14 +0100292 /*
293 * FIXME: writing MR also pings the watchdog which may not be desired.
294 * This should only be done when the registers are lost on suspend but
295 * there is no way to get this information right now.
296 */
Alexandre Belloni015b5282017-03-02 18:31:11 +0100297 sama5d4_wdt_init(wdt);
Alexandre Bellonif2013532017-01-30 18:18:48 +0100298
Ken Sloat8d209eb2019-06-14 12:53:22 +0000299 if (watchdog_active(&wdt->wdd))
300 sama5d4_wdt_start(&wdt->wdd);
301
Alexandre Bellonif2013532017-01-30 18:18:48 +0100302 return 0;
303}
304#endif
305
Ken Sloat8d209eb2019-06-14 12:53:22 +0000306static const struct dev_pm_ops sama5d4_wdt_pm_ops = {
307 SET_LATE_SYSTEM_SLEEP_PM_OPS(sama5d4_wdt_suspend_late,
308 sama5d4_wdt_resume_early)
309};
Alexandre Bellonif2013532017-01-30 18:18:48 +0100310
Wenyou Yang76534862015-08-06 18:16:46 +0800311static struct platform_driver sama5d4_wdt_driver = {
312 .probe = sama5d4_wdt_probe,
Wenyou Yang76534862015-08-06 18:16:46 +0800313 .driver = {
314 .name = "sama5d4_wdt",
Alexandre Bellonif2013532017-01-30 18:18:48 +0100315 .pm = &sama5d4_wdt_pm_ops,
Wenyou Yang76534862015-08-06 18:16:46 +0800316 .of_match_table = sama5d4_wdt_of_match,
317 }
318};
319module_platform_driver(sama5d4_wdt_driver);
320
321MODULE_AUTHOR("Atmel Corporation");
322MODULE_DESCRIPTION("Atmel SAMA5D4 Watchdog Timer driver");
323MODULE_LICENSE("GPL v2");