xref: /linux/drivers/watchdog/mtk_wdt.c (revision b82e6953acf46466b97bc592492f206b719f58b5)
1a44a4553SMatthias Brugger /*
2a44a4553SMatthias Brugger  * Mediatek Watchdog Driver
3a44a4553SMatthias Brugger  *
4a44a4553SMatthias Brugger  * Copyright (C) 2014 Matthias Brugger
5a44a4553SMatthias Brugger  *
6a44a4553SMatthias Brugger  * Matthias Brugger <matthias.bgg@gmail.com>
7a44a4553SMatthias Brugger  *
8a44a4553SMatthias Brugger  * This program is free software; you can redistribute it and/or modify
9a44a4553SMatthias Brugger  * it under the terms of the GNU General Public License as published by
10a44a4553SMatthias Brugger  * the Free Software Foundation; either version 2 of the License, or
11a44a4553SMatthias Brugger  * (at your option) any later version.
12a44a4553SMatthias Brugger  *
13a44a4553SMatthias Brugger  * This program is distributed in the hope that it will be useful,
14a44a4553SMatthias Brugger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15a44a4553SMatthias Brugger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16a44a4553SMatthias Brugger  * GNU General Public License for more details.
17a44a4553SMatthias Brugger  *
18a44a4553SMatthias Brugger  * Based on sunxi_wdt.c
19a44a4553SMatthias Brugger  */
20a44a4553SMatthias Brugger 
21a44a4553SMatthias Brugger #include <linux/err.h>
22a44a4553SMatthias Brugger #include <linux/init.h>
23a44a4553SMatthias Brugger #include <linux/io.h>
24a44a4553SMatthias Brugger #include <linux/kernel.h>
25a44a4553SMatthias Brugger #include <linux/module.h>
26a44a4553SMatthias Brugger #include <linux/moduleparam.h>
27a44a4553SMatthias Brugger #include <linux/of.h>
28a44a4553SMatthias Brugger #include <linux/platform_device.h>
29a44a4553SMatthias Brugger #include <linux/types.h>
30a44a4553SMatthias Brugger #include <linux/watchdog.h>
31a44a4553SMatthias Brugger #include <linux/delay.h>
32a44a4553SMatthias Brugger 
33a44a4553SMatthias Brugger #define WDT_MAX_TIMEOUT		31
34a44a4553SMatthias Brugger #define WDT_MIN_TIMEOUT		1
35a44a4553SMatthias Brugger #define WDT_LENGTH_TIMEOUT(n)	((n) << 5)
36a44a4553SMatthias Brugger 
37a44a4553SMatthias Brugger #define WDT_LENGTH		0x04
38a44a4553SMatthias Brugger #define WDT_LENGTH_KEY		0x8
39a44a4553SMatthias Brugger 
40a44a4553SMatthias Brugger #define WDT_RST			0x08
41a44a4553SMatthias Brugger #define WDT_RST_RELOAD		0x1971
42a44a4553SMatthias Brugger 
43a44a4553SMatthias Brugger #define WDT_MODE		0x00
44a44a4553SMatthias Brugger #define WDT_MODE_EN		(1 << 0)
45a44a4553SMatthias Brugger #define WDT_MODE_EXT_POL_LOW	(0 << 1)
46a44a4553SMatthias Brugger #define WDT_MODE_EXT_POL_HIGH	(1 << 1)
47a44a4553SMatthias Brugger #define WDT_MODE_EXRST_EN	(1 << 2)
48a44a4553SMatthias Brugger #define WDT_MODE_IRQ_EN		(1 << 3)
49a44a4553SMatthias Brugger #define WDT_MODE_AUTO_START	(1 << 4)
50a44a4553SMatthias Brugger #define WDT_MODE_DUAL_EN	(1 << 6)
51a44a4553SMatthias Brugger #define WDT_MODE_KEY		0x22000000
52a44a4553SMatthias Brugger 
53a44a4553SMatthias Brugger #define WDT_SWRST		0x14
54a44a4553SMatthias Brugger #define WDT_SWRST_KEY		0x1209
55a44a4553SMatthias Brugger 
56a44a4553SMatthias Brugger #define DRV_NAME		"mtk-wdt"
57a44a4553SMatthias Brugger #define DRV_VERSION		"1.0"
58a44a4553SMatthias Brugger 
59a44a4553SMatthias Brugger static bool nowayout = WATCHDOG_NOWAYOUT;
60*b82e6953SMarcus Folkesson static unsigned int timeout;
61a44a4553SMatthias Brugger 
62a44a4553SMatthias Brugger struct mtk_wdt_dev {
63a44a4553SMatthias Brugger 	struct watchdog_device wdt_dev;
64a44a4553SMatthias Brugger 	void __iomem *wdt_base;
65a44a4553SMatthias Brugger };
66a44a4553SMatthias Brugger 
674d8b229dSGuenter Roeck static int mtk_wdt_restart(struct watchdog_device *wdt_dev,
684d8b229dSGuenter Roeck 			   unsigned long action, void *data)
69a44a4553SMatthias Brugger {
70e86adc3fSDamien Riegel 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
71a44a4553SMatthias Brugger 	void __iomem *wdt_base;
72a44a4553SMatthias Brugger 
73a44a4553SMatthias Brugger 	wdt_base = mtk_wdt->wdt_base;
74a44a4553SMatthias Brugger 
75a44a4553SMatthias Brugger 	while (1) {
76a44a4553SMatthias Brugger 		writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST);
77a44a4553SMatthias Brugger 		mdelay(5);
78a44a4553SMatthias Brugger 	}
79a44a4553SMatthias Brugger 
80e86adc3fSDamien Riegel 	return 0;
81a44a4553SMatthias Brugger }
82a44a4553SMatthias Brugger 
83a44a4553SMatthias Brugger static int mtk_wdt_ping(struct watchdog_device *wdt_dev)
84a44a4553SMatthias Brugger {
85a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
86a44a4553SMatthias Brugger 	void __iomem *wdt_base = mtk_wdt->wdt_base;
87a44a4553SMatthias Brugger 
88a44a4553SMatthias Brugger 	iowrite32(WDT_RST_RELOAD, wdt_base + WDT_RST);
89a44a4553SMatthias Brugger 
90a44a4553SMatthias Brugger 	return 0;
91a44a4553SMatthias Brugger }
92a44a4553SMatthias Brugger 
93a44a4553SMatthias Brugger static int mtk_wdt_set_timeout(struct watchdog_device *wdt_dev,
94a44a4553SMatthias Brugger 				unsigned int timeout)
95a44a4553SMatthias Brugger {
96a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
97a44a4553SMatthias Brugger 	void __iomem *wdt_base = mtk_wdt->wdt_base;
98a44a4553SMatthias Brugger 	u32 reg;
99a44a4553SMatthias Brugger 
100a44a4553SMatthias Brugger 	wdt_dev->timeout = timeout;
101a44a4553SMatthias Brugger 
102a44a4553SMatthias Brugger 	/*
103a44a4553SMatthias Brugger 	 * One bit is the value of 512 ticks
104a44a4553SMatthias Brugger 	 * The clock has 32 KHz
105a44a4553SMatthias Brugger 	 */
106a44a4553SMatthias Brugger 	reg = WDT_LENGTH_TIMEOUT(timeout << 6) | WDT_LENGTH_KEY;
107a44a4553SMatthias Brugger 	iowrite32(reg, wdt_base + WDT_LENGTH);
108a44a4553SMatthias Brugger 
109a44a4553SMatthias Brugger 	mtk_wdt_ping(wdt_dev);
110a44a4553SMatthias Brugger 
111a44a4553SMatthias Brugger 	return 0;
112a44a4553SMatthias Brugger }
113a44a4553SMatthias Brugger 
114a44a4553SMatthias Brugger static int mtk_wdt_stop(struct watchdog_device *wdt_dev)
115a44a4553SMatthias Brugger {
116a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
117a44a4553SMatthias Brugger 	void __iomem *wdt_base = mtk_wdt->wdt_base;
118a44a4553SMatthias Brugger 	u32 reg;
119a44a4553SMatthias Brugger 
120a44a4553SMatthias Brugger 	reg = readl(wdt_base + WDT_MODE);
121a44a4553SMatthias Brugger 	reg &= ~WDT_MODE_EN;
1225da2bf1aSNicolas Boichat 	reg |= WDT_MODE_KEY;
123a44a4553SMatthias Brugger 	iowrite32(reg, wdt_base + WDT_MODE);
124a44a4553SMatthias Brugger 
125a44a4553SMatthias Brugger 	return 0;
126a44a4553SMatthias Brugger }
127a44a4553SMatthias Brugger 
128a44a4553SMatthias Brugger static int mtk_wdt_start(struct watchdog_device *wdt_dev)
129a44a4553SMatthias Brugger {
130a44a4553SMatthias Brugger 	u32 reg;
131a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
132a44a4553SMatthias Brugger 	void __iomem *wdt_base = mtk_wdt->wdt_base;
1339ffd906dSDan Carpenter 	int ret;
134a44a4553SMatthias Brugger 
135a44a4553SMatthias Brugger 	ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
136a44a4553SMatthias Brugger 	if (ret < 0)
137a44a4553SMatthias Brugger 		return ret;
138a44a4553SMatthias Brugger 
139a44a4553SMatthias Brugger 	reg = ioread32(wdt_base + WDT_MODE);
140a44a4553SMatthias Brugger 	reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
141a44a4553SMatthias Brugger 	reg |= (WDT_MODE_EN | WDT_MODE_KEY);
142a44a4553SMatthias Brugger 	iowrite32(reg, wdt_base + WDT_MODE);
143a44a4553SMatthias Brugger 
144a44a4553SMatthias Brugger 	return 0;
145a44a4553SMatthias Brugger }
146a44a4553SMatthias Brugger 
147a44a4553SMatthias Brugger static const struct watchdog_info mtk_wdt_info = {
148a44a4553SMatthias Brugger 	.identity	= DRV_NAME,
149a44a4553SMatthias Brugger 	.options	= WDIOF_SETTIMEOUT |
150a44a4553SMatthias Brugger 			  WDIOF_KEEPALIVEPING |
151a44a4553SMatthias Brugger 			  WDIOF_MAGICCLOSE,
152a44a4553SMatthias Brugger };
153a44a4553SMatthias Brugger 
154a44a4553SMatthias Brugger static const struct watchdog_ops mtk_wdt_ops = {
155a44a4553SMatthias Brugger 	.owner		= THIS_MODULE,
156a44a4553SMatthias Brugger 	.start		= mtk_wdt_start,
157a44a4553SMatthias Brugger 	.stop		= mtk_wdt_stop,
158a44a4553SMatthias Brugger 	.ping		= mtk_wdt_ping,
159a44a4553SMatthias Brugger 	.set_timeout	= mtk_wdt_set_timeout,
160e86adc3fSDamien Riegel 	.restart	= mtk_wdt_restart,
161a44a4553SMatthias Brugger };
162a44a4553SMatthias Brugger 
163a44a4553SMatthias Brugger static int mtk_wdt_probe(struct platform_device *pdev)
164a44a4553SMatthias Brugger {
165a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt;
166a44a4553SMatthias Brugger 	struct resource *res;
167a44a4553SMatthias Brugger 	int err;
168a44a4553SMatthias Brugger 
169a44a4553SMatthias Brugger 	mtk_wdt = devm_kzalloc(&pdev->dev, sizeof(*mtk_wdt), GFP_KERNEL);
170a44a4553SMatthias Brugger 	if (!mtk_wdt)
171a44a4553SMatthias Brugger 		return -ENOMEM;
172a44a4553SMatthias Brugger 
173a44a4553SMatthias Brugger 	platform_set_drvdata(pdev, mtk_wdt);
174a44a4553SMatthias Brugger 
175a44a4553SMatthias Brugger 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
176a44a4553SMatthias Brugger 	mtk_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
177a44a4553SMatthias Brugger 	if (IS_ERR(mtk_wdt->wdt_base))
178a44a4553SMatthias Brugger 		return PTR_ERR(mtk_wdt->wdt_base);
179a44a4553SMatthias Brugger 
180a44a4553SMatthias Brugger 	mtk_wdt->wdt_dev.info = &mtk_wdt_info;
181a44a4553SMatthias Brugger 	mtk_wdt->wdt_dev.ops = &mtk_wdt_ops;
182a44a4553SMatthias Brugger 	mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
183a44a4553SMatthias Brugger 	mtk_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
184a44a4553SMatthias Brugger 	mtk_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
185a44a4553SMatthias Brugger 	mtk_wdt->wdt_dev.parent = &pdev->dev;
186a44a4553SMatthias Brugger 
187a44a4553SMatthias Brugger 	watchdog_init_timeout(&mtk_wdt->wdt_dev, timeout, &pdev->dev);
188a44a4553SMatthias Brugger 	watchdog_set_nowayout(&mtk_wdt->wdt_dev, nowayout);
189e86adc3fSDamien Riegel 	watchdog_set_restart_priority(&mtk_wdt->wdt_dev, 128);
190a44a4553SMatthias Brugger 
191a44a4553SMatthias Brugger 	watchdog_set_drvdata(&mtk_wdt->wdt_dev, mtk_wdt);
192a44a4553SMatthias Brugger 
193a44a4553SMatthias Brugger 	mtk_wdt_stop(&mtk_wdt->wdt_dev);
194a44a4553SMatthias Brugger 
195a44a4553SMatthias Brugger 	err = watchdog_register_device(&mtk_wdt->wdt_dev);
196a44a4553SMatthias Brugger 	if (unlikely(err))
197a44a4553SMatthias Brugger 		return err;
198a44a4553SMatthias Brugger 
199a44a4553SMatthias Brugger 	dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
200a44a4553SMatthias Brugger 			mtk_wdt->wdt_dev.timeout, nowayout);
201a44a4553SMatthias Brugger 
202a44a4553SMatthias Brugger 	return 0;
203a44a4553SMatthias Brugger }
204a44a4553SMatthias Brugger 
2055724485bSGreta Zhang static void mtk_wdt_shutdown(struct platform_device *pdev)
2065724485bSGreta Zhang {
2075724485bSGreta Zhang 	struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
2085724485bSGreta Zhang 
2095724485bSGreta Zhang 	if (watchdog_active(&mtk_wdt->wdt_dev))
2105724485bSGreta Zhang 		mtk_wdt_stop(&mtk_wdt->wdt_dev);
2115724485bSGreta Zhang }
2125724485bSGreta Zhang 
213a44a4553SMatthias Brugger static int mtk_wdt_remove(struct platform_device *pdev)
214a44a4553SMatthias Brugger {
215a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
216a44a4553SMatthias Brugger 
217a44a4553SMatthias Brugger 	watchdog_unregister_device(&mtk_wdt->wdt_dev);
218a44a4553SMatthias Brugger 
219a44a4553SMatthias Brugger 	return 0;
220a44a4553SMatthias Brugger }
221a44a4553SMatthias Brugger 
2229fab0692SGreta Zhang #ifdef CONFIG_PM_SLEEP
2239fab0692SGreta Zhang static int mtk_wdt_suspend(struct device *dev)
2249fab0692SGreta Zhang {
2259fab0692SGreta Zhang 	struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
2269fab0692SGreta Zhang 
2279fab0692SGreta Zhang 	if (watchdog_active(&mtk_wdt->wdt_dev))
2289fab0692SGreta Zhang 		mtk_wdt_stop(&mtk_wdt->wdt_dev);
2299fab0692SGreta Zhang 
2309fab0692SGreta Zhang 	return 0;
2319fab0692SGreta Zhang }
2329fab0692SGreta Zhang 
2339fab0692SGreta Zhang static int mtk_wdt_resume(struct device *dev)
2349fab0692SGreta Zhang {
2359fab0692SGreta Zhang 	struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
2369fab0692SGreta Zhang 
2379fab0692SGreta Zhang 	if (watchdog_active(&mtk_wdt->wdt_dev)) {
2389fab0692SGreta Zhang 		mtk_wdt_start(&mtk_wdt->wdt_dev);
2399fab0692SGreta Zhang 		mtk_wdt_ping(&mtk_wdt->wdt_dev);
2409fab0692SGreta Zhang 	}
2419fab0692SGreta Zhang 
2429fab0692SGreta Zhang 	return 0;
2439fab0692SGreta Zhang }
2449fab0692SGreta Zhang #endif
2459fab0692SGreta Zhang 
246a44a4553SMatthias Brugger static const struct of_device_id mtk_wdt_dt_ids[] = {
247a44a4553SMatthias Brugger 	{ .compatible = "mediatek,mt6589-wdt" },
248a44a4553SMatthias Brugger 	{ /* sentinel */ }
249a44a4553SMatthias Brugger };
250a44a4553SMatthias Brugger MODULE_DEVICE_TABLE(of, mtk_wdt_dt_ids);
251a44a4553SMatthias Brugger 
2529fab0692SGreta Zhang static const struct dev_pm_ops mtk_wdt_pm_ops = {
2539fab0692SGreta Zhang 	SET_SYSTEM_SLEEP_PM_OPS(mtk_wdt_suspend,
2549fab0692SGreta Zhang 				mtk_wdt_resume)
2559fab0692SGreta Zhang };
2569fab0692SGreta Zhang 
257a44a4553SMatthias Brugger static struct platform_driver mtk_wdt_driver = {
258a44a4553SMatthias Brugger 	.probe		= mtk_wdt_probe,
259a44a4553SMatthias Brugger 	.remove		= mtk_wdt_remove,
2605724485bSGreta Zhang 	.shutdown	= mtk_wdt_shutdown,
261a44a4553SMatthias Brugger 	.driver		= {
262a44a4553SMatthias Brugger 		.name		= DRV_NAME,
2639fab0692SGreta Zhang 		.pm		= &mtk_wdt_pm_ops,
264a44a4553SMatthias Brugger 		.of_match_table	= mtk_wdt_dt_ids,
265a44a4553SMatthias Brugger 	},
266a44a4553SMatthias Brugger };
267a44a4553SMatthias Brugger 
268a44a4553SMatthias Brugger module_platform_driver(mtk_wdt_driver);
269a44a4553SMatthias Brugger 
270a44a4553SMatthias Brugger module_param(timeout, uint, 0);
271a44a4553SMatthias Brugger MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
272a44a4553SMatthias Brugger 
273a44a4553SMatthias Brugger module_param(nowayout, bool, 0);
274a44a4553SMatthias Brugger MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
275a44a4553SMatthias Brugger 			__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
276a44a4553SMatthias Brugger 
277a44a4553SMatthias Brugger MODULE_LICENSE("GPL");
278a44a4553SMatthias Brugger MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
279a44a4553SMatthias Brugger MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
280a44a4553SMatthias Brugger MODULE_VERSION(DRV_VERSION);
281