2 * Driver for Atmel SAMA5D4 Watchdog Timer
4 * Copyright (C) 2015 Atmel Corporation
6 * Licensed under GPLv2.
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;
34 static int wdt_timeout = WDT_DEFAULT_TIMEOUT;
35 static bool nowayout = WATCHDOG_NOWAYOUT;
37 module_param(wdt_timeout, int, 0);
38 MODULE_PARM_DESC(wdt_timeout,
39 "Watchdog timeout in seconds. (default = "
40 __MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")");
42 module_param(nowayout, bool, 0);
43 MODULE_PARM_DESC(nowayout,
44 "Watchdog cannot be stopped once started (default="
45 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
47 #define wdt_read(wdt, field) \
48 readl_relaxed((wdt)->reg_base + (field))
50 #define wdt_write(wtd, field, val) \
51 writel_relaxed((val), (wdt)->reg_base + (field))
53 static int sama5d4_wdt_start(struct watchdog_device *wdd)
55 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
58 reg = wdt_read(wdt, AT91_WDT_MR);
59 reg &= ~AT91_WDT_WDDIS;
60 wdt_write(wdt, AT91_WDT_MR, reg);
65 static int sama5d4_wdt_stop(struct watchdog_device *wdd)
67 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
70 reg = wdt_read(wdt, AT91_WDT_MR);
71 reg |= AT91_WDT_WDDIS;
72 wdt_write(wdt, AT91_WDT_MR, reg);
77 static int sama5d4_wdt_ping(struct watchdog_device *wdd)
79 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
81 wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
86 static int sama5d4_wdt_set_timeout(struct watchdog_device *wdd,
89 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
90 u32 value = WDT_SEC2TICKS(timeout);
93 reg = wdt_read(wdt, AT91_WDT_MR);
96 reg |= AT91_WDT_SET_WDV(value);
97 reg |= AT91_WDT_SET_WDD(value);
98 wdt_write(wdt, AT91_WDT_MR, reg);
100 wdd->timeout = timeout;
105 static const struct watchdog_info sama5d4_wdt_info = {
106 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
107 .identity = "Atmel SAMA5D4 Watchdog",
110 static struct watchdog_ops sama5d4_wdt_ops = {
111 .owner = THIS_MODULE,
112 .start = sama5d4_wdt_start,
113 .stop = sama5d4_wdt_stop,
114 .ping = sama5d4_wdt_ping,
115 .set_timeout = sama5d4_wdt_set_timeout,
118 static irqreturn_t sama5d4_wdt_irq_handler(int irq, void *dev_id)
120 struct sama5d4_wdt *wdt = platform_get_drvdata(dev_id);
122 if (wdt_read(wdt, AT91_WDT_SR)) {
123 pr_crit("Atmel Watchdog Software Reset\n");
125 pr_crit("Reboot didn't succeed\n");
131 static int of_sama5d4_wdt_init(struct device_node *np, struct sama5d4_wdt *wdt)
135 wdt->config = AT91_WDT_WDDIS;
137 if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) &&
138 !strcmp(tmp, "software"))
139 wdt->config |= AT91_WDT_WDFIEN;
141 wdt->config |= AT91_WDT_WDRSTEN;
143 if (of_property_read_bool(np, "atmel,idle-halt"))
144 wdt->config |= AT91_WDT_WDIDLEHLT;
146 if (of_property_read_bool(np, "atmel,dbg-halt"))
147 wdt->config |= AT91_WDT_WDDBGHLT;
152 static int sama5d4_wdt_init(struct sama5d4_wdt *wdt)
154 struct watchdog_device *wdd = &wdt->wdd;
155 u32 value = WDT_SEC2TICKS(wdd->timeout);
159 * Because the fields WDV and WDD must not be modified when the WDDIS
160 * bit is set, so clear the WDDIS bit before writing the WDT_MR.
162 reg = wdt_read(wdt, AT91_WDT_MR);
163 reg &= ~AT91_WDT_WDDIS;
164 wdt_write(wdt, AT91_WDT_MR, reg);
167 reg |= AT91_WDT_SET_WDD(value);
168 reg |= AT91_WDT_SET_WDV(value);
170 wdt_write(wdt, AT91_WDT_MR, reg);
175 static int sama5d4_wdt_probe(struct platform_device *pdev)
177 struct watchdog_device *wdd;
178 struct sama5d4_wdt *wdt;
179 struct resource *res;
184 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
189 wdd->timeout = wdt_timeout;
190 wdd->info = &sama5d4_wdt_info;
191 wdd->ops = &sama5d4_wdt_ops;
192 wdd->min_timeout = MIN_WDT_TIMEOUT;
193 wdd->max_timeout = MAX_WDT_TIMEOUT;
195 watchdog_set_drvdata(wdd, wdt);
197 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
198 regs = devm_ioremap_resource(&pdev->dev, res);
200 return PTR_ERR(regs);
202 wdt->reg_base = regs;
204 if (pdev->dev.of_node) {
205 irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
207 dev_warn(&pdev->dev, "failed to get IRQ from DT\n");
209 ret = of_sama5d4_wdt_init(pdev->dev.of_node, wdt);
214 if ((wdt->config & AT91_WDT_WDFIEN) && irq) {
215 ret = devm_request_irq(&pdev->dev, irq, sama5d4_wdt_irq_handler,
216 IRQF_SHARED | IRQF_IRQPOLL |
217 IRQF_NO_SUSPEND, pdev->name, pdev);
220 "cannot register interrupt handler\n");
225 ret = watchdog_init_timeout(wdd, wdt_timeout, &pdev->dev);
227 dev_err(&pdev->dev, "unable to set timeout value\n");
231 ret = sama5d4_wdt_init(wdt);
235 watchdog_set_nowayout(wdd, nowayout);
237 ret = watchdog_register_device(wdd);
239 dev_err(&pdev->dev, "failed to register watchdog device\n");
243 platform_set_drvdata(pdev, wdt);
245 dev_info(&pdev->dev, "initialized (timeout = %d sec, nowayout = %d)\n",
246 wdt_timeout, nowayout);
251 static int sama5d4_wdt_remove(struct platform_device *pdev)
253 struct sama5d4_wdt *wdt = platform_get_drvdata(pdev);
255 sama5d4_wdt_stop(&wdt->wdd);
257 watchdog_unregister_device(&wdt->wdd);
262 static const struct of_device_id sama5d4_wdt_of_match[] = {
263 { .compatible = "atmel,sama5d4-wdt", },
266 MODULE_DEVICE_TABLE(of, sama5d4_wdt_of_match);
268 static struct platform_driver sama5d4_wdt_driver = {
269 .probe = sama5d4_wdt_probe,
270 .remove = sama5d4_wdt_remove,
272 .name = "sama5d4_wdt",
273 .of_match_table = sama5d4_wdt_of_match,
276 module_platform_driver(sama5d4_wdt_driver);
278 MODULE_AUTHOR("Atmel Corporation");
279 MODULE_DESCRIPTION("Atmel SAMA5D4 Watchdog Timer driver");
280 MODULE_LICENSE("GPL v2");