1/*
2 * Mediatek Watchdog Driver
3 *
4 * Copyright (C) 2014 Matthias Brugger
5 *
6 * Matthias Brugger <matthias.bgg@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * Based on sunxi_wdt.c
19 */
20
21#include <linux/err.h>
22#include <linux/init.h>
23#include <linux/io.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/of.h>
28#include <linux/platform_device.h>
29#include <linux/types.h>
30#include <linux/watchdog.h>
31#include <linux/notifier.h>
32#include <linux/reboot.h>
33#include <linux/delay.h>
34
35#define WDT_MAX_TIMEOUT		31
36#define WDT_MIN_TIMEOUT		1
37#define WDT_LENGTH_TIMEOUT(n)	((n) << 5)
38
39#define WDT_LENGTH		0x04
40#define WDT_LENGTH_KEY		0x8
41
42#define WDT_RST			0x08
43#define WDT_RST_RELOAD		0x1971
44
45#define WDT_MODE		0x00
46#define WDT_MODE_EN		(1 << 0)
47#define WDT_MODE_EXT_POL_LOW	(0 << 1)
48#define WDT_MODE_EXT_POL_HIGH	(1 << 1)
49#define WDT_MODE_EXRST_EN	(1 << 2)
50#define WDT_MODE_IRQ_EN		(1 << 3)
51#define WDT_MODE_AUTO_START	(1 << 4)
52#define WDT_MODE_DUAL_EN	(1 << 6)
53#define WDT_MODE_KEY		0x22000000
54
55#define WDT_SWRST		0x14
56#define WDT_SWRST_KEY		0x1209
57
58#define DRV_NAME		"mtk-wdt"
59#define DRV_VERSION		"1.0"
60
61static bool nowayout = WATCHDOG_NOWAYOUT;
62static unsigned int timeout = WDT_MAX_TIMEOUT;
63
64struct mtk_wdt_dev {
65	struct watchdog_device wdt_dev;
66	void __iomem *wdt_base;
67	struct notifier_block restart_handler;
68};
69
70static int mtk_reset_handler(struct notifier_block *this, unsigned long mode,
71				void *cmd)
72{
73	struct mtk_wdt_dev *mtk_wdt;
74	void __iomem *wdt_base;
75
76	mtk_wdt = container_of(this, struct mtk_wdt_dev, restart_handler);
77	wdt_base = mtk_wdt->wdt_base;
78
79	while (1) {
80		writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST);
81		mdelay(5);
82	}
83
84	return NOTIFY_DONE;
85}
86
87static int mtk_wdt_ping(struct watchdog_device *wdt_dev)
88{
89	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
90	void __iomem *wdt_base = mtk_wdt->wdt_base;
91
92	iowrite32(WDT_RST_RELOAD, wdt_base + WDT_RST);
93
94	return 0;
95}
96
97static int mtk_wdt_set_timeout(struct watchdog_device *wdt_dev,
98				unsigned int timeout)
99{
100	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
101	void __iomem *wdt_base = mtk_wdt->wdt_base;
102	u32 reg;
103
104	wdt_dev->timeout = timeout;
105
106	/*
107	 * One bit is the value of 512 ticks
108	 * The clock has 32 KHz
109	 */
110	reg = WDT_LENGTH_TIMEOUT(timeout << 6) | WDT_LENGTH_KEY;
111	iowrite32(reg, wdt_base + WDT_LENGTH);
112
113	mtk_wdt_ping(wdt_dev);
114
115	return 0;
116}
117
118static int mtk_wdt_stop(struct watchdog_device *wdt_dev)
119{
120	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
121	void __iomem *wdt_base = mtk_wdt->wdt_base;
122	u32 reg;
123
124	reg = readl(wdt_base + WDT_MODE);
125	reg &= ~WDT_MODE_EN;
126	reg |= WDT_MODE_KEY;
127	iowrite32(reg, wdt_base + WDT_MODE);
128
129	return 0;
130}
131
132static int mtk_wdt_start(struct watchdog_device *wdt_dev)
133{
134	u32 reg;
135	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
136	void __iomem *wdt_base = mtk_wdt->wdt_base;
137	int ret;
138
139	ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
140	if (ret < 0)
141		return ret;
142
143	reg = ioread32(wdt_base + WDT_MODE);
144	reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
145	reg |= (WDT_MODE_EN | WDT_MODE_KEY);
146	iowrite32(reg, wdt_base + WDT_MODE);
147
148	return 0;
149}
150
151static const struct watchdog_info mtk_wdt_info = {
152	.identity	= DRV_NAME,
153	.options	= WDIOF_SETTIMEOUT |
154			  WDIOF_KEEPALIVEPING |
155			  WDIOF_MAGICCLOSE,
156};
157
158static const struct watchdog_ops mtk_wdt_ops = {
159	.owner		= THIS_MODULE,
160	.start		= mtk_wdt_start,
161	.stop		= mtk_wdt_stop,
162	.ping		= mtk_wdt_ping,
163	.set_timeout	= mtk_wdt_set_timeout,
164};
165
166static int mtk_wdt_probe(struct platform_device *pdev)
167{
168	struct mtk_wdt_dev *mtk_wdt;
169	struct resource *res;
170	int err;
171
172	mtk_wdt = devm_kzalloc(&pdev->dev, sizeof(*mtk_wdt), GFP_KERNEL);
173	if (!mtk_wdt)
174		return -ENOMEM;
175
176	platform_set_drvdata(pdev, mtk_wdt);
177
178	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
179	mtk_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
180	if (IS_ERR(mtk_wdt->wdt_base))
181		return PTR_ERR(mtk_wdt->wdt_base);
182
183	mtk_wdt->wdt_dev.info = &mtk_wdt_info;
184	mtk_wdt->wdt_dev.ops = &mtk_wdt_ops;
185	mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
186	mtk_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
187	mtk_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
188	mtk_wdt->wdt_dev.parent = &pdev->dev;
189
190	watchdog_init_timeout(&mtk_wdt->wdt_dev, timeout, &pdev->dev);
191	watchdog_set_nowayout(&mtk_wdt->wdt_dev, nowayout);
192
193	watchdog_set_drvdata(&mtk_wdt->wdt_dev, mtk_wdt);
194
195	mtk_wdt_stop(&mtk_wdt->wdt_dev);
196
197	err = watchdog_register_device(&mtk_wdt->wdt_dev);
198	if (unlikely(err))
199		return err;
200
201	mtk_wdt->restart_handler.notifier_call = mtk_reset_handler;
202	mtk_wdt->restart_handler.priority = 128;
203	err = register_restart_handler(&mtk_wdt->restart_handler);
204	if (err)
205		dev_warn(&pdev->dev,
206			"cannot register restart handler (err=%d)\n", err);
207
208	dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
209			mtk_wdt->wdt_dev.timeout, nowayout);
210
211	return 0;
212}
213
214static void mtk_wdt_shutdown(struct platform_device *pdev)
215{
216	struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
217
218	if (watchdog_active(&mtk_wdt->wdt_dev))
219		mtk_wdt_stop(&mtk_wdt->wdt_dev);
220}
221
222static int mtk_wdt_remove(struct platform_device *pdev)
223{
224	struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
225
226	unregister_restart_handler(&mtk_wdt->restart_handler);
227
228	watchdog_unregister_device(&mtk_wdt->wdt_dev);
229
230	return 0;
231}
232
233#ifdef CONFIG_PM_SLEEP
234static int mtk_wdt_suspend(struct device *dev)
235{
236	struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
237
238	if (watchdog_active(&mtk_wdt->wdt_dev))
239		mtk_wdt_stop(&mtk_wdt->wdt_dev);
240
241	return 0;
242}
243
244static int mtk_wdt_resume(struct device *dev)
245{
246	struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
247
248	if (watchdog_active(&mtk_wdt->wdt_dev)) {
249		mtk_wdt_start(&mtk_wdt->wdt_dev);
250		mtk_wdt_ping(&mtk_wdt->wdt_dev);
251	}
252
253	return 0;
254}
255#endif
256
257static const struct of_device_id mtk_wdt_dt_ids[] = {
258	{ .compatible = "mediatek,mt6589-wdt" },
259	{ /* sentinel */ }
260};
261MODULE_DEVICE_TABLE(of, mtk_wdt_dt_ids);
262
263static const struct dev_pm_ops mtk_wdt_pm_ops = {
264	SET_SYSTEM_SLEEP_PM_OPS(mtk_wdt_suspend,
265				mtk_wdt_resume)
266};
267
268static struct platform_driver mtk_wdt_driver = {
269	.probe		= mtk_wdt_probe,
270	.remove		= mtk_wdt_remove,
271	.shutdown	= mtk_wdt_shutdown,
272	.driver		= {
273		.name		= DRV_NAME,
274		.pm		= &mtk_wdt_pm_ops,
275		.of_match_table	= mtk_wdt_dt_ids,
276	},
277};
278
279module_platform_driver(mtk_wdt_driver);
280
281module_param(timeout, uint, 0);
282MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
283
284module_param(nowayout, bool, 0);
285MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
286			__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
287
288MODULE_LICENSE("GPL");
289MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
290MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
291MODULE_VERSION(DRV_VERSION);
292