xref: /linux/drivers/watchdog/max63xx_wdt.c (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
1*a0d261ccSBagas Sanjaya // SPDX-License-Identifier: GPL-2.0-only
266aaa7a5SMarc Zyngier /*
366aaa7a5SMarc Zyngier  * drivers/char/watchdog/max63xx_wdt.c
466aaa7a5SMarc Zyngier  *
566aaa7a5SMarc Zyngier  * Driver for max63{69,70,71,72,73,74} watchdog timers
666aaa7a5SMarc Zyngier  *
766aaa7a5SMarc Zyngier  * Copyright (C) 2009 Marc Zyngier <maz@misterjones.org>
866aaa7a5SMarc Zyngier  *
966aaa7a5SMarc Zyngier  * This driver assumes the watchdog pins are memory mapped (as it is
1066aaa7a5SMarc Zyngier  * the case for the Arcom Zeus). Should it be connected over GPIOs or
1166aaa7a5SMarc Zyngier  * another interface, some abstraction will have to be introduced.
1266aaa7a5SMarc Zyngier  */
1366aaa7a5SMarc Zyngier 
144c271bb6SThierry Reding #include <linux/err.h>
1566aaa7a5SMarc Zyngier #include <linux/module.h>
1666aaa7a5SMarc Zyngier #include <linux/moduleparam.h>
17ac316725SRandy Dunlap #include <linux/mod_devicetable.h>
1866aaa7a5SMarc Zyngier #include <linux/types.h>
1966aaa7a5SMarc Zyngier #include <linux/kernel.h>
2066aaa7a5SMarc Zyngier #include <linux/watchdog.h>
2166aaa7a5SMarc Zyngier #include <linux/bitops.h>
2266aaa7a5SMarc Zyngier #include <linux/platform_device.h>
2366aaa7a5SMarc Zyngier #include <linux/spinlock.h>
2466aaa7a5SMarc Zyngier #include <linux/io.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
26585ba602SLinus Walleij #include <linux/property.h>
2766aaa7a5SMarc Zyngier 
2866aaa7a5SMarc Zyngier #define DEFAULT_HEARTBEAT 60
2966aaa7a5SMarc Zyngier #define MAX_HEARTBEAT     60
3066aaa7a5SMarc Zyngier 
31a0f36833SAxel Lin static unsigned int heartbeat = DEFAULT_HEARTBEAT;
3286a1e189SWim Van Sebroeck static bool nowayout  = WATCHDOG_NOWAYOUT;
3366aaa7a5SMarc Zyngier 
3466aaa7a5SMarc Zyngier /*
3566aaa7a5SMarc Zyngier  * Memory mapping: a single byte, 3 first lower bits to select bit 3
3666aaa7a5SMarc Zyngier  * to ping the watchdog.
3766aaa7a5SMarc Zyngier  */
3866aaa7a5SMarc Zyngier #define MAX6369_WDSET	(7 << 0)
3966aaa7a5SMarc Zyngier #define MAX6369_WDI	(1 << 3)
4066aaa7a5SMarc Zyngier 
41b9be9660SVivien Didelot #define MAX6369_WDSET_DISABLED	3
4266aaa7a5SMarc Zyngier 
4366aaa7a5SMarc Zyngier static int nodelay;
44b9be9660SVivien Didelot 
45b9be9660SVivien Didelot struct max63xx_wdt {
46b9be9660SVivien Didelot 	struct watchdog_device wdd;
47b9be9660SVivien Didelot 	const struct max63xx_timeout *timeout;
48b9be9660SVivien Didelot 
49b9be9660SVivien Didelot 	/* memory mapping */
50b9be9660SVivien Didelot 	void __iomem *base;
51b9be9660SVivien Didelot 	spinlock_t lock;
52b9be9660SVivien Didelot 
53b9be9660SVivien Didelot 	/* WDI and WSET bits write access routines */
54b9be9660SVivien Didelot 	void (*ping)(struct max63xx_wdt *wdt);
55b9be9660SVivien Didelot 	void (*set)(struct max63xx_wdt *wdt, u8 set);
56b9be9660SVivien Didelot };
5766aaa7a5SMarc Zyngier 
5866aaa7a5SMarc Zyngier /*
5966aaa7a5SMarc Zyngier  * The timeout values used are actually the absolute minimum the chip
6066aaa7a5SMarc Zyngier  * offers. Typical values on my board are slightly over twice as long
6166aaa7a5SMarc Zyngier  * (10s setting ends up with a 25s timeout), and can be up to 3 times
6266aaa7a5SMarc Zyngier  * the nominal setting (according to the datasheet). So please take
6366aaa7a5SMarc Zyngier  * these values with a grain of salt. Same goes for the initial delay
6466aaa7a5SMarc Zyngier  * "feature". Only max6373/74 have a few settings without this initial
6566aaa7a5SMarc Zyngier  * delay (selected with the "nodelay" parameter).
6666aaa7a5SMarc Zyngier  *
6766aaa7a5SMarc Zyngier  * I also decided to remove from the tables any timeout smaller than a
6866aaa7a5SMarc Zyngier  * second, as it looked completly overkill...
6966aaa7a5SMarc Zyngier  */
7066aaa7a5SMarc Zyngier 
7166aaa7a5SMarc Zyngier /* Timeouts in second */
7266aaa7a5SMarc Zyngier struct max63xx_timeout {
73b9be9660SVivien Didelot 	const u8 wdset;
74b9be9660SVivien Didelot 	const u8 tdelay;
75b9be9660SVivien Didelot 	const u8 twd;
7666aaa7a5SMarc Zyngier };
7766aaa7a5SMarc Zyngier 
78b9be9660SVivien Didelot static const struct max63xx_timeout max6369_table[] = {
7966aaa7a5SMarc Zyngier 	{ 5,  1,  1 },
8066aaa7a5SMarc Zyngier 	{ 6, 10, 10 },
8166aaa7a5SMarc Zyngier 	{ 7, 60, 60 },
8266aaa7a5SMarc Zyngier 	{ },
8366aaa7a5SMarc Zyngier };
8466aaa7a5SMarc Zyngier 
85b9be9660SVivien Didelot static const struct max63xx_timeout max6371_table[] = {
8666aaa7a5SMarc Zyngier 	{ 6, 60,  3 },
8766aaa7a5SMarc Zyngier 	{ 7, 60, 60 },
8866aaa7a5SMarc Zyngier 	{ },
8966aaa7a5SMarc Zyngier };
9066aaa7a5SMarc Zyngier 
91b9be9660SVivien Didelot static const struct max63xx_timeout max6373_table[] = {
9266aaa7a5SMarc Zyngier 	{ 2, 60,  1 },
9366aaa7a5SMarc Zyngier 	{ 5,  0,  1 },
9466aaa7a5SMarc Zyngier 	{ 1,  3,  3 },
9566aaa7a5SMarc Zyngier 	{ 7, 60, 10 },
9666aaa7a5SMarc Zyngier 	{ 6,  0, 10 },
9766aaa7a5SMarc Zyngier 	{ },
9866aaa7a5SMarc Zyngier };
9966aaa7a5SMarc Zyngier 
100585ba602SLinus Walleij static const struct max63xx_timeout *
max63xx_select_timeout(const struct max63xx_timeout * table,int value)101585ba602SLinus Walleij max63xx_select_timeout(const struct max63xx_timeout *table, int value)
10266aaa7a5SMarc Zyngier {
10366aaa7a5SMarc Zyngier 	while (table->twd) {
10466aaa7a5SMarc Zyngier 		if (value <= table->twd) {
10566aaa7a5SMarc Zyngier 			if (nodelay && table->tdelay == 0)
10666aaa7a5SMarc Zyngier 				return table;
10766aaa7a5SMarc Zyngier 
10866aaa7a5SMarc Zyngier 			if (!nodelay)
10966aaa7a5SMarc Zyngier 				return table;
11066aaa7a5SMarc Zyngier 		}
11166aaa7a5SMarc Zyngier 
11266aaa7a5SMarc Zyngier 		table++;
11366aaa7a5SMarc Zyngier 	}
11466aaa7a5SMarc Zyngier 
11566aaa7a5SMarc Zyngier 	return NULL;
11666aaa7a5SMarc Zyngier }
11766aaa7a5SMarc Zyngier 
max63xx_wdt_ping(struct watchdog_device * wdd)118a0f36833SAxel Lin static int max63xx_wdt_ping(struct watchdog_device *wdd)
11966aaa7a5SMarc Zyngier {
120b9be9660SVivien Didelot 	struct max63xx_wdt *wdt = watchdog_get_drvdata(wdd);
12166aaa7a5SMarc Zyngier 
122b9be9660SVivien Didelot 	wdt->ping(wdt);
123a0f36833SAxel Lin 	return 0;
12466aaa7a5SMarc Zyngier }
12566aaa7a5SMarc Zyngier 
max63xx_wdt_start(struct watchdog_device * wdd)126a0f36833SAxel Lin static int max63xx_wdt_start(struct watchdog_device *wdd)
12766aaa7a5SMarc Zyngier {
128b9be9660SVivien Didelot 	struct max63xx_wdt *wdt = watchdog_get_drvdata(wdd);
12966aaa7a5SMarc Zyngier 
130b9be9660SVivien Didelot 	wdt->set(wdt, wdt->timeout->wdset);
13166aaa7a5SMarc Zyngier 
13266aaa7a5SMarc Zyngier 	/* check for a edge triggered startup */
133b9be9660SVivien Didelot 	if (wdt->timeout->tdelay == 0)
134b9be9660SVivien Didelot 		wdt->ping(wdt);
135a0f36833SAxel Lin 	return 0;
13666aaa7a5SMarc Zyngier }
13766aaa7a5SMarc Zyngier 
max63xx_wdt_stop(struct watchdog_device * wdd)138a0f36833SAxel Lin static int max63xx_wdt_stop(struct watchdog_device *wdd)
13966aaa7a5SMarc Zyngier {
140b9be9660SVivien Didelot 	struct max63xx_wdt *wdt = watchdog_get_drvdata(wdd);
141b1183e06SMarc Zyngier 
142b9be9660SVivien Didelot 	wdt->set(wdt, MAX6369_WDSET_DISABLED);
14366aaa7a5SMarc Zyngier 	return 0;
14466aaa7a5SMarc Zyngier }
14566aaa7a5SMarc Zyngier 
146a0f36833SAxel Lin static const struct watchdog_ops max63xx_wdt_ops = {
147a0f36833SAxel Lin 	.owner = THIS_MODULE,
148a0f36833SAxel Lin 	.start = max63xx_wdt_start,
149a0f36833SAxel Lin 	.stop = max63xx_wdt_stop,
150a0f36833SAxel Lin 	.ping = max63xx_wdt_ping,
151a0f36833SAxel Lin };
152a0f36833SAxel Lin 
153b9be9660SVivien Didelot static const struct watchdog_info max63xx_wdt_info = {
154b9be9660SVivien Didelot 	.options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
155b9be9660SVivien Didelot 	.identity = "max63xx Watchdog",
15666aaa7a5SMarc Zyngier };
15766aaa7a5SMarc Zyngier 
max63xx_mmap_ping(struct max63xx_wdt * wdt)158b9be9660SVivien Didelot static void max63xx_mmap_ping(struct max63xx_wdt *wdt)
159b9be9660SVivien Didelot {
160b9be9660SVivien Didelot 	u8 val;
161b9be9660SVivien Didelot 
162b9be9660SVivien Didelot 	spin_lock(&wdt->lock);
163b9be9660SVivien Didelot 
164b9be9660SVivien Didelot 	val = __raw_readb(wdt->base);
165b9be9660SVivien Didelot 
166b9be9660SVivien Didelot 	__raw_writeb(val | MAX6369_WDI, wdt->base);
167b9be9660SVivien Didelot 	__raw_writeb(val & ~MAX6369_WDI, wdt->base);
168b9be9660SVivien Didelot 
169b9be9660SVivien Didelot 	spin_unlock(&wdt->lock);
170b9be9660SVivien Didelot }
171b9be9660SVivien Didelot 
max63xx_mmap_set(struct max63xx_wdt * wdt,u8 set)172b9be9660SVivien Didelot static void max63xx_mmap_set(struct max63xx_wdt *wdt, u8 set)
173b9be9660SVivien Didelot {
174b9be9660SVivien Didelot 	u8 val;
175b9be9660SVivien Didelot 
176b9be9660SVivien Didelot 	spin_lock(&wdt->lock);
177b9be9660SVivien Didelot 
178b9be9660SVivien Didelot 	val = __raw_readb(wdt->base);
179b9be9660SVivien Didelot 	val &= ~MAX6369_WDSET;
180b9be9660SVivien Didelot 	val |= set & MAX6369_WDSET;
181b9be9660SVivien Didelot 	__raw_writeb(val, wdt->base);
182b9be9660SVivien Didelot 
183b9be9660SVivien Didelot 	spin_unlock(&wdt->lock);
184b9be9660SVivien Didelot }
185b9be9660SVivien Didelot 
max63xx_mmap_init(struct platform_device * p,struct max63xx_wdt * wdt)186b9be9660SVivien Didelot static int max63xx_mmap_init(struct platform_device *p, struct max63xx_wdt *wdt)
187b9be9660SVivien Didelot {
1880f0a6a28SGuenter Roeck 	wdt->base = devm_platform_ioremap_resource(p, 0);
189b9be9660SVivien Didelot 	if (IS_ERR(wdt->base))
190b9be9660SVivien Didelot 		return PTR_ERR(wdt->base);
191b9be9660SVivien Didelot 
192b9be9660SVivien Didelot 	spin_lock_init(&wdt->lock);
193b9be9660SVivien Didelot 
194b9be9660SVivien Didelot 	wdt->ping = max63xx_mmap_ping;
195b9be9660SVivien Didelot 	wdt->set = max63xx_mmap_set;
196b9be9660SVivien Didelot 	return 0;
197b9be9660SVivien Didelot }
198b9be9660SVivien Didelot 
max63xx_wdt_probe(struct platform_device * pdev)1992d991a16SBill Pemberton static int max63xx_wdt_probe(struct platform_device *pdev)
20066aaa7a5SMarc Zyngier {
20180cb6bddSGuenter Roeck 	struct device *dev = &pdev->dev;
202b9be9660SVivien Didelot 	struct max63xx_wdt *wdt;
203585ba602SLinus Walleij 	const struct max63xx_timeout *table;
204b9be9660SVivien Didelot 	int err;
205b9be9660SVivien Didelot 
20680cb6bddSGuenter Roeck 	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
207b9be9660SVivien Didelot 	if (!wdt)
208b9be9660SVivien Didelot 		return -ENOMEM;
20966aaa7a5SMarc Zyngier 
210585ba602SLinus Walleij 	/* Attempt to use fwnode first */
211585ba602SLinus Walleij 	table = device_get_match_data(dev);
212585ba602SLinus Walleij 	if (!table)
21366aaa7a5SMarc Zyngier 		table = (struct max63xx_timeout *)pdev->id_entry->driver_data;
21466aaa7a5SMarc Zyngier 
21566aaa7a5SMarc Zyngier 	if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
21666aaa7a5SMarc Zyngier 		heartbeat = DEFAULT_HEARTBEAT;
21766aaa7a5SMarc Zyngier 
218b9be9660SVivien Didelot 	wdt->timeout = max63xx_select_timeout(table, heartbeat);
219b9be9660SVivien Didelot 	if (!wdt->timeout) {
22080cb6bddSGuenter Roeck 		dev_err(dev, "unable to satisfy %ds heartbeat request\n",
221b9be9660SVivien Didelot 			heartbeat);
22266aaa7a5SMarc Zyngier 		return -EINVAL;
22366aaa7a5SMarc Zyngier 	}
22466aaa7a5SMarc Zyngier 
225b9be9660SVivien Didelot 	err = max63xx_mmap_init(pdev, wdt);
226b9be9660SVivien Didelot 	if (err)
227b9be9660SVivien Didelot 		return err;
228b9be9660SVivien Didelot 
229b9be9660SVivien Didelot 	platform_set_drvdata(pdev, &wdt->wdd);
230b9be9660SVivien Didelot 	watchdog_set_drvdata(&wdt->wdd, wdt);
231b9be9660SVivien Didelot 
23280cb6bddSGuenter Roeck 	wdt->wdd.parent = dev;
233b9be9660SVivien Didelot 	wdt->wdd.timeout = wdt->timeout->twd;
234b9be9660SVivien Didelot 	wdt->wdd.info = &max63xx_wdt_info;
235b9be9660SVivien Didelot 	wdt->wdd.ops = &max63xx_wdt_ops;
236b9be9660SVivien Didelot 
237b9be9660SVivien Didelot 	watchdog_set_nowayout(&wdt->wdd, nowayout);
238b9be9660SVivien Didelot 
23980cb6bddSGuenter Roeck 	err = devm_watchdog_register_device(dev, &wdt->wdd);
240b9be9660SVivien Didelot 	if (err)
241b9be9660SVivien Didelot 		return err;
242b9be9660SVivien Didelot 
24380cb6bddSGuenter Roeck 	dev_info(dev, "using %ds heartbeat with %ds initial delay\n",
244b9be9660SVivien Didelot 		 wdt->timeout->twd, wdt->timeout->tdelay);
245b9be9660SVivien Didelot 	return 0;
24666aaa7a5SMarc Zyngier }
24766aaa7a5SMarc Zyngier 
2488c7c72c9SKrzysztof Kozlowski static const struct platform_device_id max63xx_id_table[] = {
24966aaa7a5SMarc Zyngier 	{ "max6369_wdt", (kernel_ulong_t)max6369_table, },
25066aaa7a5SMarc Zyngier 	{ "max6370_wdt", (kernel_ulong_t)max6369_table, },
25166aaa7a5SMarc Zyngier 	{ "max6371_wdt", (kernel_ulong_t)max6371_table, },
25266aaa7a5SMarc Zyngier 	{ "max6372_wdt", (kernel_ulong_t)max6371_table, },
25366aaa7a5SMarc Zyngier 	{ "max6373_wdt", (kernel_ulong_t)max6373_table, },
25466aaa7a5SMarc Zyngier 	{ "max6374_wdt", (kernel_ulong_t)max6373_table, },
25566aaa7a5SMarc Zyngier 	{ },
25666aaa7a5SMarc Zyngier };
25766aaa7a5SMarc Zyngier MODULE_DEVICE_TABLE(platform, max63xx_id_table);
25866aaa7a5SMarc Zyngier 
259585ba602SLinus Walleij static const struct of_device_id max63xx_dt_id_table[] = {
260585ba602SLinus Walleij 	{ .compatible = "maxim,max6369", .data = max6369_table, },
261585ba602SLinus Walleij 	{ .compatible = "maxim,max6370", .data = max6369_table, },
262585ba602SLinus Walleij 	{ .compatible = "maxim,max6371", .data = max6371_table, },
263585ba602SLinus Walleij 	{ .compatible = "maxim,max6372", .data = max6371_table, },
264585ba602SLinus Walleij 	{ .compatible = "maxim,max6373", .data = max6373_table, },
265585ba602SLinus Walleij 	{ .compatible = "maxim,max6374", .data = max6373_table, },
266585ba602SLinus Walleij 	{ }
267585ba602SLinus Walleij };
268585ba602SLinus Walleij MODULE_DEVICE_TABLE(of, max63xx_dt_id_table);
269585ba602SLinus Walleij 
27066aaa7a5SMarc Zyngier static struct platform_driver max63xx_wdt_driver = {
27166aaa7a5SMarc Zyngier 	.probe		= max63xx_wdt_probe,
27266aaa7a5SMarc Zyngier 	.id_table	= max63xx_id_table,
27366aaa7a5SMarc Zyngier 	.driver		= {
27466aaa7a5SMarc Zyngier 		.name	= "max63xx_wdt",
275585ba602SLinus Walleij 		.of_match_table = max63xx_dt_id_table,
27666aaa7a5SMarc Zyngier 	},
27766aaa7a5SMarc Zyngier };
27866aaa7a5SMarc Zyngier 
279b8ec6118SAxel Lin module_platform_driver(max63xx_wdt_driver);
28066aaa7a5SMarc Zyngier 
28166aaa7a5SMarc Zyngier MODULE_AUTHOR("Marc Zyngier <maz@misterjones.org>");
28266aaa7a5SMarc Zyngier MODULE_DESCRIPTION("max63xx Watchdog Driver");
28366aaa7a5SMarc Zyngier 
28466aaa7a5SMarc Zyngier module_param(heartbeat, int, 0);
28566aaa7a5SMarc Zyngier MODULE_PARM_DESC(heartbeat,
28666aaa7a5SMarc Zyngier 		 "Watchdog heartbeat period in seconds from 1 to "
28766aaa7a5SMarc Zyngier 		 __MODULE_STRING(MAX_HEARTBEAT) ", default "
28866aaa7a5SMarc Zyngier 		 __MODULE_STRING(DEFAULT_HEARTBEAT));
28966aaa7a5SMarc Zyngier 
29086a1e189SWim Van Sebroeck module_param(nowayout, bool, 0);
29166aaa7a5SMarc Zyngier MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
29266aaa7a5SMarc Zyngier 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
29366aaa7a5SMarc Zyngier 
29466aaa7a5SMarc Zyngier module_param(nodelay, int, 0);
29566aaa7a5SMarc Zyngier MODULE_PARM_DESC(nodelay,
29666aaa7a5SMarc Zyngier 		 "Force selection of a timeout setting without initial delay "
29766aaa7a5SMarc Zyngier 		 "(max6373/74 only, default=0)");
29866aaa7a5SMarc Zyngier 
29929efefb9SUwe Kleine-König MODULE_LICENSE("GPL v2");
300