xref: /linux/drivers/watchdog/sama5d4_wdt.c (revision 0f0a6a285ec0c7b0ac0b532f87a784605322f9ce)
176534860SWenyou Yang /*
276534860SWenyou Yang  * Driver for Atmel SAMA5D4 Watchdog Timer
376534860SWenyou Yang  *
476534860SWenyou Yang  * Copyright (C) 2015 Atmel Corporation
576534860SWenyou Yang  *
676534860SWenyou Yang  * Licensed under GPLv2.
776534860SWenyou Yang  */
876534860SWenyou Yang 
9ddd6d240SAlexandre Belloni #include <linux/delay.h>
1076534860SWenyou Yang #include <linux/interrupt.h>
1176534860SWenyou Yang #include <linux/io.h>
1276534860SWenyou Yang #include <linux/kernel.h>
1376534860SWenyou Yang #include <linux/module.h>
1476534860SWenyou Yang #include <linux/of.h>
1576534860SWenyou Yang #include <linux/of_irq.h>
1676534860SWenyou Yang #include <linux/platform_device.h>
1776534860SWenyou Yang #include <linux/reboot.h>
1876534860SWenyou Yang #include <linux/watchdog.h>
1976534860SWenyou Yang 
2076534860SWenyou Yang #include "at91sam9_wdt.h"
2176534860SWenyou Yang 
2276534860SWenyou Yang /* minimum and maximum watchdog timeout, in seconds */
2376534860SWenyou Yang #define MIN_WDT_TIMEOUT		1
2476534860SWenyou Yang #define MAX_WDT_TIMEOUT		16
2576534860SWenyou Yang #define WDT_DEFAULT_TIMEOUT	MAX_WDT_TIMEOUT
2676534860SWenyou Yang 
2776534860SWenyou Yang #define WDT_SEC2TICKS(s)	((s) ? (((s) << 8) - 1) : 0)
2876534860SWenyou Yang 
2976534860SWenyou Yang struct sama5d4_wdt {
3076534860SWenyou Yang 	struct watchdog_device	wdd;
3176534860SWenyou Yang 	void __iomem		*reg_base;
32722ce635SAlexandre Belloni 	u32			mr;
33ddd6d240SAlexandre Belloni 	unsigned long		last_ping;
3476534860SWenyou Yang };
3576534860SWenyou Yang 
36976932e4SMarcus Folkesson static int wdt_timeout;
3776534860SWenyou Yang static bool nowayout = WATCHDOG_NOWAYOUT;
3876534860SWenyou Yang 
3976534860SWenyou Yang module_param(wdt_timeout, int, 0);
4076534860SWenyou Yang MODULE_PARM_DESC(wdt_timeout,
4176534860SWenyou Yang 	"Watchdog timeout in seconds. (default = "
4276534860SWenyou Yang 	__MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")");
4376534860SWenyou Yang 
4476534860SWenyou Yang module_param(nowayout, bool, 0);
4576534860SWenyou Yang MODULE_PARM_DESC(nowayout,
4676534860SWenyou Yang 	"Watchdog cannot be stopped once started (default="
4776534860SWenyou Yang 	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
4876534860SWenyou Yang 
49015b5286SAlexandre Belloni #define wdt_enabled (!(wdt->mr & AT91_WDT_WDDIS))
50015b5286SAlexandre Belloni 
5176534860SWenyou Yang #define wdt_read(wdt, field) \
5276534860SWenyou Yang 	readl_relaxed((wdt)->reg_base + (field))
5376534860SWenyou Yang 
54ddd6d240SAlexandre Belloni /* 4 slow clock periods is 4/32768 = 122.07µs*/
55ddd6d240SAlexandre Belloni #define WDT_DELAY	usecs_to_jiffies(123)
56ddd6d240SAlexandre Belloni 
57ddd6d240SAlexandre Belloni static void wdt_write(struct sama5d4_wdt *wdt, u32 field, u32 val)
58ddd6d240SAlexandre Belloni {
59ddd6d240SAlexandre Belloni 	/*
60ddd6d240SAlexandre Belloni 	 * WDT_CR and WDT_MR must not be modified within three slow clock
61ddd6d240SAlexandre Belloni 	 * periods following a restart of the watchdog performed by a write
62ddd6d240SAlexandre Belloni 	 * access in WDT_CR.
63ddd6d240SAlexandre Belloni 	 */
64ddd6d240SAlexandre Belloni 	while (time_before(jiffies, wdt->last_ping + WDT_DELAY))
65ddd6d240SAlexandre Belloni 		usleep_range(30, 125);
66ddd6d240SAlexandre Belloni 	writel_relaxed(val, wdt->reg_base + field);
67ddd6d240SAlexandre Belloni 	wdt->last_ping = jiffies;
68ddd6d240SAlexandre Belloni }
69ddd6d240SAlexandre Belloni 
70ddd6d240SAlexandre Belloni static void wdt_write_nosleep(struct sama5d4_wdt *wdt, u32 field, u32 val)
71ddd6d240SAlexandre Belloni {
72ddd6d240SAlexandre Belloni 	if (time_before(jiffies, wdt->last_ping + WDT_DELAY))
73ddd6d240SAlexandre Belloni 		udelay(123);
74ddd6d240SAlexandre Belloni 	writel_relaxed(val, wdt->reg_base + field);
75ddd6d240SAlexandre Belloni 	wdt->last_ping = jiffies;
76ddd6d240SAlexandre Belloni }
7776534860SWenyou Yang 
7876534860SWenyou Yang static int sama5d4_wdt_start(struct watchdog_device *wdd)
7976534860SWenyou Yang {
8076534860SWenyou Yang 	struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
8176534860SWenyou Yang 
82722ce635SAlexandre Belloni 	wdt->mr &= ~AT91_WDT_WDDIS;
83722ce635SAlexandre Belloni 	wdt_write(wdt, AT91_WDT_MR, wdt->mr);
8476534860SWenyou Yang 
8576534860SWenyou Yang 	return 0;
8676534860SWenyou Yang }
8776534860SWenyou Yang 
8876534860SWenyou Yang static int sama5d4_wdt_stop(struct watchdog_device *wdd)
8976534860SWenyou Yang {
9076534860SWenyou Yang 	struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
9176534860SWenyou Yang 
92722ce635SAlexandre Belloni 	wdt->mr |= AT91_WDT_WDDIS;
93722ce635SAlexandre Belloni 	wdt_write(wdt, AT91_WDT_MR, wdt->mr);
9476534860SWenyou Yang 
9576534860SWenyou Yang 	return 0;
9676534860SWenyou Yang }
9776534860SWenyou Yang 
9876534860SWenyou Yang static int sama5d4_wdt_ping(struct watchdog_device *wdd)
9976534860SWenyou Yang {
10076534860SWenyou Yang 	struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
10176534860SWenyou Yang 
10276534860SWenyou Yang 	wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
10376534860SWenyou Yang 
10476534860SWenyou Yang 	return 0;
10576534860SWenyou Yang }
10676534860SWenyou Yang 
10776534860SWenyou Yang static int sama5d4_wdt_set_timeout(struct watchdog_device *wdd,
10876534860SWenyou Yang 				 unsigned int timeout)
10976534860SWenyou Yang {
11076534860SWenyou Yang 	struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
11176534860SWenyou Yang 	u32 value = WDT_SEC2TICKS(timeout);
11276534860SWenyou Yang 
113722ce635SAlexandre Belloni 	wdt->mr &= ~AT91_WDT_WDV;
114722ce635SAlexandre Belloni 	wdt->mr &= ~AT91_WDT_WDD;
115722ce635SAlexandre Belloni 	wdt->mr |= AT91_WDT_SET_WDV(value);
116722ce635SAlexandre Belloni 	wdt->mr |= AT91_WDT_SET_WDD(value);
117015b5286SAlexandre Belloni 
118015b5286SAlexandre Belloni 	/*
119015b5286SAlexandre Belloni 	 * WDDIS has to be 0 when updating WDD/WDV. The datasheet states: When
120015b5286SAlexandre Belloni 	 * setting the WDDIS bit, and while it is set, the fields WDV and WDD
121015b5286SAlexandre Belloni 	 * must not be modified.
122015b5286SAlexandre Belloni 	 * If the watchdog is enabled, then the timeout can be updated. Else,
123015b5286SAlexandre Belloni 	 * wait that the user enables it.
124015b5286SAlexandre Belloni 	 */
125015b5286SAlexandre Belloni 	if (wdt_enabled)
126015b5286SAlexandre Belloni 		wdt_write(wdt, AT91_WDT_MR, wdt->mr & ~AT91_WDT_WDDIS);
12776534860SWenyou Yang 
12876534860SWenyou Yang 	wdd->timeout = timeout;
12976534860SWenyou Yang 
13076534860SWenyou Yang 	return 0;
13176534860SWenyou Yang }
13276534860SWenyou Yang 
13376534860SWenyou Yang static const struct watchdog_info sama5d4_wdt_info = {
13476534860SWenyou Yang 	.options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
13576534860SWenyou Yang 	.identity = "Atmel SAMA5D4 Watchdog",
13676534860SWenyou Yang };
13776534860SWenyou Yang 
138b893e344SBhumika Goyal static const struct watchdog_ops sama5d4_wdt_ops = {
13976534860SWenyou Yang 	.owner = THIS_MODULE,
14076534860SWenyou Yang 	.start = sama5d4_wdt_start,
14176534860SWenyou Yang 	.stop = sama5d4_wdt_stop,
14276534860SWenyou Yang 	.ping = sama5d4_wdt_ping,
14376534860SWenyou Yang 	.set_timeout = sama5d4_wdt_set_timeout,
14476534860SWenyou Yang };
14576534860SWenyou Yang 
14676534860SWenyou Yang static irqreturn_t sama5d4_wdt_irq_handler(int irq, void *dev_id)
14776534860SWenyou Yang {
14876534860SWenyou Yang 	struct sama5d4_wdt *wdt = platform_get_drvdata(dev_id);
14976534860SWenyou Yang 
15076534860SWenyou Yang 	if (wdt_read(wdt, AT91_WDT_SR)) {
15176534860SWenyou Yang 		pr_crit("Atmel Watchdog Software Reset\n");
15276534860SWenyou Yang 		emergency_restart();
15376534860SWenyou Yang 		pr_crit("Reboot didn't succeed\n");
15476534860SWenyou Yang 	}
15576534860SWenyou Yang 
15676534860SWenyou Yang 	return IRQ_HANDLED;
15776534860SWenyou Yang }
15876534860SWenyou Yang 
15976534860SWenyou Yang static int of_sama5d4_wdt_init(struct device_node *np, struct sama5d4_wdt *wdt)
16076534860SWenyou Yang {
16176534860SWenyou Yang 	const char *tmp;
16276534860SWenyou Yang 
163722ce635SAlexandre Belloni 	wdt->mr = AT91_WDT_WDDIS;
16476534860SWenyou Yang 
16576534860SWenyou Yang 	if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) &&
16676534860SWenyou Yang 	    !strcmp(tmp, "software"))
167722ce635SAlexandre Belloni 		wdt->mr |= AT91_WDT_WDFIEN;
16876534860SWenyou Yang 	else
169722ce635SAlexandre Belloni 		wdt->mr |= AT91_WDT_WDRSTEN;
17076534860SWenyou Yang 
17176534860SWenyou Yang 	if (of_property_read_bool(np, "atmel,idle-halt"))
172722ce635SAlexandre Belloni 		wdt->mr |= AT91_WDT_WDIDLEHLT;
17376534860SWenyou Yang 
17476534860SWenyou Yang 	if (of_property_read_bool(np, "atmel,dbg-halt"))
175722ce635SAlexandre Belloni 		wdt->mr |= AT91_WDT_WDDBGHLT;
17676534860SWenyou Yang 
17776534860SWenyou Yang 	return 0;
17876534860SWenyou Yang }
17976534860SWenyou Yang 
18076534860SWenyou Yang static int sama5d4_wdt_init(struct sama5d4_wdt *wdt)
18176534860SWenyou Yang {
18276534860SWenyou Yang 	u32 reg;
18376534860SWenyou Yang 	/*
184015b5286SAlexandre Belloni 	 * When booting and resuming, the bootloader may have changed the
185015b5286SAlexandre Belloni 	 * watchdog configuration.
186015b5286SAlexandre Belloni 	 * If the watchdog is already running, we can safely update it.
187015b5286SAlexandre Belloni 	 * Else, we have to disable it properly.
18876534860SWenyou Yang 	 */
189015b5286SAlexandre Belloni 	if (wdt_enabled) {
190ddd6d240SAlexandre Belloni 		wdt_write_nosleep(wdt, AT91_WDT_MR, wdt->mr);
191015b5286SAlexandre Belloni 	} else {
192015b5286SAlexandre Belloni 		reg = wdt_read(wdt, AT91_WDT_MR);
193015b5286SAlexandre Belloni 		if (!(reg & AT91_WDT_WDDIS))
194ddd6d240SAlexandre Belloni 			wdt_write_nosleep(wdt, AT91_WDT_MR,
195ddd6d240SAlexandre Belloni 					  reg | AT91_WDT_WDDIS);
196015b5286SAlexandre Belloni 	}
19776534860SWenyou Yang 	return 0;
19876534860SWenyou Yang }
19976534860SWenyou Yang 
20076534860SWenyou Yang static int sama5d4_wdt_probe(struct platform_device *pdev)
20176534860SWenyou Yang {
20276534860SWenyou Yang 	struct watchdog_device *wdd;
20376534860SWenyou Yang 	struct sama5d4_wdt *wdt;
20476534860SWenyou Yang 	void __iomem *regs;
20576534860SWenyou Yang 	u32 irq = 0;
206015b5286SAlexandre Belloni 	u32 timeout;
20776534860SWenyou Yang 	int ret;
20876534860SWenyou Yang 
20976534860SWenyou Yang 	wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
21076534860SWenyou Yang 	if (!wdt)
21176534860SWenyou Yang 		return -ENOMEM;
21276534860SWenyou Yang 
21376534860SWenyou Yang 	wdd = &wdt->wdd;
214976932e4SMarcus Folkesson 	wdd->timeout = WDT_DEFAULT_TIMEOUT;
21576534860SWenyou Yang 	wdd->info = &sama5d4_wdt_info;
21676534860SWenyou Yang 	wdd->ops = &sama5d4_wdt_ops;
21776534860SWenyou Yang 	wdd->min_timeout = MIN_WDT_TIMEOUT;
21876534860SWenyou Yang 	wdd->max_timeout = MAX_WDT_TIMEOUT;
219ddd6d240SAlexandre Belloni 	wdt->last_ping = jiffies;
22076534860SWenyou Yang 
22176534860SWenyou Yang 	watchdog_set_drvdata(wdd, wdt);
22276534860SWenyou Yang 
223*0f0a6a28SGuenter Roeck 	regs = devm_platform_ioremap_resource(pdev, 0);
22476534860SWenyou Yang 	if (IS_ERR(regs))
22576534860SWenyou Yang 		return PTR_ERR(regs);
22676534860SWenyou Yang 
22776534860SWenyou Yang 	wdt->reg_base = regs;
22876534860SWenyou Yang 
22976534860SWenyou Yang 	irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
23076534860SWenyou Yang 	if (!irq)
23176534860SWenyou Yang 		dev_warn(&pdev->dev, "failed to get IRQ from DT\n");
23276534860SWenyou Yang 
23376534860SWenyou Yang 	ret = of_sama5d4_wdt_init(pdev->dev.of_node, wdt);
23476534860SWenyou Yang 	if (ret)
23576534860SWenyou Yang 		return ret;
23676534860SWenyou Yang 
237722ce635SAlexandre Belloni 	if ((wdt->mr & AT91_WDT_WDFIEN) && irq) {
23876534860SWenyou Yang 		ret = devm_request_irq(&pdev->dev, irq, sama5d4_wdt_irq_handler,
23976534860SWenyou Yang 				       IRQF_SHARED | IRQF_IRQPOLL |
24076534860SWenyou Yang 				       IRQF_NO_SUSPEND, pdev->name, pdev);
24176534860SWenyou Yang 		if (ret) {
24276534860SWenyou Yang 			dev_err(&pdev->dev,
24376534860SWenyou Yang 				"cannot register interrupt handler\n");
24476534860SWenyou Yang 			return ret;
24576534860SWenyou Yang 		}
24676534860SWenyou Yang 	}
24776534860SWenyou Yang 
2482e0432f8SRomain Izard 	watchdog_init_timeout(wdd, wdt_timeout, &pdev->dev);
24976534860SWenyou Yang 
250015b5286SAlexandre Belloni 	timeout = WDT_SEC2TICKS(wdd->timeout);
251015b5286SAlexandre Belloni 
252015b5286SAlexandre Belloni 	wdt->mr |= AT91_WDT_SET_WDD(timeout);
253015b5286SAlexandre Belloni 	wdt->mr |= AT91_WDT_SET_WDV(timeout);
254015b5286SAlexandre Belloni 
25576534860SWenyou Yang 	ret = sama5d4_wdt_init(wdt);
25676534860SWenyou Yang 	if (ret)
25776534860SWenyou Yang 		return ret;
25876534860SWenyou Yang 
25976534860SWenyou Yang 	watchdog_set_nowayout(wdd, nowayout);
26076534860SWenyou Yang 
26176534860SWenyou Yang 	ret = watchdog_register_device(wdd);
26276534860SWenyou Yang 	if (ret) {
26376534860SWenyou Yang 		dev_err(&pdev->dev, "failed to register watchdog device\n");
26476534860SWenyou Yang 		return ret;
26576534860SWenyou Yang 	}
26676534860SWenyou Yang 
26776534860SWenyou Yang 	platform_set_drvdata(pdev, wdt);
26876534860SWenyou Yang 
26976534860SWenyou Yang 	dev_info(&pdev->dev, "initialized (timeout = %d sec, nowayout = %d)\n",
270976932e4SMarcus Folkesson 		 wdd->timeout, nowayout);
27176534860SWenyou Yang 
27276534860SWenyou Yang 	return 0;
27376534860SWenyou Yang }
27476534860SWenyou Yang 
27576534860SWenyou Yang static int sama5d4_wdt_remove(struct platform_device *pdev)
27676534860SWenyou Yang {
27776534860SWenyou Yang 	struct sama5d4_wdt *wdt = platform_get_drvdata(pdev);
27876534860SWenyou Yang 
27976534860SWenyou Yang 	sama5d4_wdt_stop(&wdt->wdd);
28076534860SWenyou Yang 
28176534860SWenyou Yang 	watchdog_unregister_device(&wdt->wdd);
28276534860SWenyou Yang 
28376534860SWenyou Yang 	return 0;
28476534860SWenyou Yang }
28576534860SWenyou Yang 
28676534860SWenyou Yang static const struct of_device_id sama5d4_wdt_of_match[] = {
28776534860SWenyou Yang 	{ .compatible = "atmel,sama5d4-wdt", },
28876534860SWenyou Yang 	{ }
28976534860SWenyou Yang };
29076534860SWenyou Yang MODULE_DEVICE_TABLE(of, sama5d4_wdt_of_match);
29176534860SWenyou Yang 
292f2013532SAlexandre Belloni #ifdef CONFIG_PM_SLEEP
293f2013532SAlexandre Belloni static int sama5d4_wdt_resume(struct device *dev)
294f2013532SAlexandre Belloni {
295f2013532SAlexandre Belloni 	struct sama5d4_wdt *wdt = dev_get_drvdata(dev);
296f2013532SAlexandre Belloni 
2975dca80f6SAlexandre Belloni 	/*
2985dca80f6SAlexandre Belloni 	 * FIXME: writing MR also pings the watchdog which may not be desired.
2995dca80f6SAlexandre Belloni 	 * This should only be done when the registers are lost on suspend but
3005dca80f6SAlexandre Belloni 	 * there is no way to get this information right now.
3015dca80f6SAlexandre Belloni 	 */
302015b5286SAlexandre Belloni 	sama5d4_wdt_init(wdt);
303f2013532SAlexandre Belloni 
304f2013532SAlexandre Belloni 	return 0;
305f2013532SAlexandre Belloni }
306f2013532SAlexandre Belloni #endif
307f2013532SAlexandre Belloni 
308f2013532SAlexandre Belloni static SIMPLE_DEV_PM_OPS(sama5d4_wdt_pm_ops, NULL,
309f2013532SAlexandre Belloni 			 sama5d4_wdt_resume);
310f2013532SAlexandre Belloni 
31176534860SWenyou Yang static struct platform_driver sama5d4_wdt_driver = {
31276534860SWenyou Yang 	.probe		= sama5d4_wdt_probe,
31376534860SWenyou Yang 	.remove		= sama5d4_wdt_remove,
31476534860SWenyou Yang 	.driver		= {
31576534860SWenyou Yang 		.name	= "sama5d4_wdt",
316f2013532SAlexandre Belloni 		.pm	= &sama5d4_wdt_pm_ops,
31776534860SWenyou Yang 		.of_match_table = sama5d4_wdt_of_match,
31876534860SWenyou Yang 	}
31976534860SWenyou Yang };
32076534860SWenyou Yang module_platform_driver(sama5d4_wdt_driver);
32176534860SWenyou Yang 
32276534860SWenyou Yang MODULE_AUTHOR("Atmel Corporation");
32376534860SWenyou Yang MODULE_DESCRIPTION("Atmel SAMA5D4 Watchdog Timer driver");
32476534860SWenyou Yang MODULE_LICENSE("GPL v2");
325