mm: vma_adjust: remove superfluous check for next not NULL
[linux/fpc-iii.git] / drivers / watchdog / sama5d4_wdt.c
bloba49634cdc1ccbc987576bae3480f6669db3c0cde
1 /*
2 * Driver for Atmel SAMA5D4 Watchdog Timer
4 * Copyright (C) 2015 Atmel Corporation
6 * Licensed under GPLv2.
7 */
9 #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>
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)
28 struct sama5d4_wdt {
29 struct watchdog_device wdd;
30 void __iomem *reg_base;
31 u32 config;
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);
56 u32 reg;
58 reg = wdt_read(wdt, AT91_WDT_MR);
59 reg &= ~AT91_WDT_WDDIS;
60 wdt_write(wdt, AT91_WDT_MR, reg);
62 return 0;
65 static int sama5d4_wdt_stop(struct watchdog_device *wdd)
67 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
68 u32 reg;
70 reg = wdt_read(wdt, AT91_WDT_MR);
71 reg |= AT91_WDT_WDDIS;
72 wdt_write(wdt, AT91_WDT_MR, reg);
74 return 0;
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);
83 return 0;
86 static int sama5d4_wdt_set_timeout(struct watchdog_device *wdd,
87 unsigned int timeout)
89 struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
90 u32 value = WDT_SEC2TICKS(timeout);
91 u32 reg;
93 reg = wdt_read(wdt, AT91_WDT_MR);
94 reg &= ~AT91_WDT_WDV;
95 reg &= ~AT91_WDT_WDD;
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;
102 return 0;
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");
124 emergency_restart();
125 pr_crit("Reboot didn't succeed\n");
128 return IRQ_HANDLED;
131 static int of_sama5d4_wdt_init(struct device_node *np, struct sama5d4_wdt *wdt)
133 const char *tmp;
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;
140 else
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;
149 return 0;
152 static int sama5d4_wdt_init(struct sama5d4_wdt *wdt)
154 struct watchdog_device *wdd = &wdt->wdd;
155 u32 value = WDT_SEC2TICKS(wdd->timeout);
156 u32 reg;
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);
166 reg = wdt->config;
167 reg |= AT91_WDT_SET_WDD(value);
168 reg |= AT91_WDT_SET_WDV(value);
170 wdt_write(wdt, AT91_WDT_MR, reg);
172 return 0;
175 static int sama5d4_wdt_probe(struct platform_device *pdev)
177 struct watchdog_device *wdd;
178 struct sama5d4_wdt *wdt;
179 struct resource *res;
180 void __iomem *regs;
181 u32 irq = 0;
182 int ret;
184 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
185 if (!wdt)
186 return -ENOMEM;
188 wdd = &wdt->wdd;
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);
199 if (IS_ERR(regs))
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);
206 if (!irq)
207 dev_warn(&pdev->dev, "failed to get IRQ from DT\n");
209 ret = of_sama5d4_wdt_init(pdev->dev.of_node, wdt);
210 if (ret)
211 return ret;
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);
218 if (ret) {
219 dev_err(&pdev->dev,
220 "cannot register interrupt handler\n");
221 return ret;
225 ret = watchdog_init_timeout(wdd, wdt_timeout, &pdev->dev);
226 if (ret) {
227 dev_err(&pdev->dev, "unable to set timeout value\n");
228 return ret;
231 ret = sama5d4_wdt_init(wdt);
232 if (ret)
233 return ret;
235 watchdog_set_nowayout(wdd, nowayout);
237 ret = watchdog_register_device(wdd);
238 if (ret) {
239 dev_err(&pdev->dev, "failed to register watchdog device\n");
240 return ret;
243 platform_set_drvdata(pdev, wdt);
245 dev_info(&pdev->dev, "initialized (timeout = %d sec, nowayout = %d)\n",
246 wdt_timeout, nowayout);
248 return 0;
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);
259 return 0;
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,
271 .driver = {
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");