xref: /linux/drivers/watchdog/mtk_wdt.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
12e62c498SMarcus Folkesson // SPDX-License-Identifier: GPL-2.0+
2a44a4553SMatthias Brugger /*
3a44a4553SMatthias Brugger  * Mediatek Watchdog Driver
4a44a4553SMatthias Brugger  *
5a44a4553SMatthias Brugger  * Copyright (C) 2014 Matthias Brugger
6a44a4553SMatthias Brugger  *
7a44a4553SMatthias Brugger  * Matthias Brugger <matthias.bgg@gmail.com>
8a44a4553SMatthias Brugger  *
9a44a4553SMatthias Brugger  * Based on sunxi_wdt.c
10a44a4553SMatthias Brugger  */
11a44a4553SMatthias Brugger 
12f07c776fSEnric Balletbo i Serra #include <dt-bindings/reset/mt2712-resets.h>
1328927f6cSAngeloGioacchino Del Regno #include <dt-bindings/reset/mediatek,mt6795-resets.h>
14711a5b25SSam Shih #include <dt-bindings/reset/mt7986-resets.h>
15f07c776fSEnric Balletbo i Serra #include <dt-bindings/reset/mt8183-resets.h>
164dbabc4dSRunyang Chen #include <dt-bindings/reset/mt8186-resets.h>
17bc731365SRunyang Chen #include <dt-bindings/reset/mt8188-resets.h>
18f07c776fSEnric Balletbo i Serra #include <dt-bindings/reset/mt8192-resets.h>
198c6b5ea6SChristine Zhu #include <dt-bindings/reset/mt8195-resets.h>
20c254e103Syong.liang #include <linux/delay.h>
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>
29c254e103Syong.liang #include <linux/reset-controller.h>
30a44a4553SMatthias Brugger #include <linux/types.h>
31a44a4553SMatthias Brugger #include <linux/watchdog.h>
321bbce779SWang Qing #include <linux/interrupt.h>
33a44a4553SMatthias Brugger 
34a44a4553SMatthias Brugger #define WDT_MAX_TIMEOUT		31
351bbce779SWang Qing #define WDT_MIN_TIMEOUT		2
36a44a4553SMatthias Brugger #define WDT_LENGTH_TIMEOUT(n)	((n) << 5)
37a44a4553SMatthias Brugger 
38a44a4553SMatthias Brugger #define WDT_LENGTH		0x04
39a44a4553SMatthias Brugger #define WDT_LENGTH_KEY		0x8
40a44a4553SMatthias Brugger 
41a44a4553SMatthias Brugger #define WDT_RST			0x08
42a44a4553SMatthias Brugger #define WDT_RST_RELOAD		0x1971
43a44a4553SMatthias Brugger 
44a44a4553SMatthias Brugger #define WDT_MODE		0x00
45a44a4553SMatthias Brugger #define WDT_MODE_EN		(1 << 0)
46a44a4553SMatthias Brugger #define WDT_MODE_EXT_POL_LOW	(0 << 1)
47a44a4553SMatthias Brugger #define WDT_MODE_EXT_POL_HIGH	(1 << 1)
48a44a4553SMatthias Brugger #define WDT_MODE_EXRST_EN	(1 << 2)
49a44a4553SMatthias Brugger #define WDT_MODE_IRQ_EN		(1 << 3)
50a44a4553SMatthias Brugger #define WDT_MODE_AUTO_START	(1 << 4)
51a44a4553SMatthias Brugger #define WDT_MODE_DUAL_EN	(1 << 6)
52a224764fSAllen-KH Cheng #define WDT_MODE_CNT_SEL	(1 << 8)
53a44a4553SMatthias Brugger #define WDT_MODE_KEY		0x22000000
54a44a4553SMatthias Brugger 
55a44a4553SMatthias Brugger #define WDT_SWRST		0x14
56a44a4553SMatthias Brugger #define WDT_SWRST_KEY		0x1209
57a44a4553SMatthias Brugger 
58c254e103Syong.liang #define WDT_SWSYSRST		0x18U
59c254e103Syong.liang #define WDT_SWSYS_RST_KEY	0x88000000
60c254e103Syong.liang 
61*137c9e08SDaniel Golle #define WDT_SWSYSRST_EN		0xfc
62*137c9e08SDaniel Golle 
63a44a4553SMatthias Brugger #define DRV_NAME		"mtk-wdt"
64a44a4553SMatthias Brugger #define DRV_VERSION		"1.0"
65a44a4553SMatthias Brugger 
66*137c9e08SDaniel Golle #define MT7988_TOPRGU_SW_RST_NUM	24
67*137c9e08SDaniel Golle 
68a44a4553SMatthias Brugger static bool nowayout = WATCHDOG_NOWAYOUT;
69b82e6953SMarcus Folkesson static unsigned int timeout;
70a44a4553SMatthias Brugger 
71a44a4553SMatthias Brugger struct mtk_wdt_dev {
72a44a4553SMatthias Brugger 	struct watchdog_device wdt_dev;
73a44a4553SMatthias Brugger 	void __iomem *wdt_base;
74c254e103Syong.liang 	spinlock_t lock; /* protects WDT_SWSYSRST reg */
75c254e103Syong.liang 	struct reset_controller_dev rcdev;
7659b0f513SFengquan Chen 	bool disable_wdt_extrst;
77a224764fSAllen-KH Cheng 	bool reset_by_toprgu;
78*137c9e08SDaniel Golle 	bool has_swsysrst_en;
79a44a4553SMatthias Brugger };
80a44a4553SMatthias Brugger 
81c254e103Syong.liang struct mtk_wdt_data {
82c254e103Syong.liang 	int toprgu_sw_rst_num;
83*137c9e08SDaniel Golle 	bool has_swsysrst_en;
84c254e103Syong.liang };
85c254e103Syong.liang 
869e5236e7Syong.liang static const struct mtk_wdt_data mt2712_data = {
879e5236e7Syong.liang 	.toprgu_sw_rst_num = MT2712_TOPRGU_SW_RST_NUM,
889e5236e7Syong.liang };
899e5236e7Syong.liang 
9028927f6cSAngeloGioacchino Del Regno static const struct mtk_wdt_data mt6795_data = {
9128927f6cSAngeloGioacchino Del Regno 	.toprgu_sw_rst_num = MT6795_TOPRGU_SW_RST_NUM,
9228927f6cSAngeloGioacchino Del Regno };
9328927f6cSAngeloGioacchino Del Regno 
94711a5b25SSam Shih static const struct mtk_wdt_data mt7986_data = {
95711a5b25SSam Shih 	.toprgu_sw_rst_num = MT7986_TOPRGU_SW_RST_NUM,
96711a5b25SSam Shih };
97711a5b25SSam Shih 
98*137c9e08SDaniel Golle static const struct mtk_wdt_data mt7988_data = {
99*137c9e08SDaniel Golle 	.toprgu_sw_rst_num = MT7988_TOPRGU_SW_RST_NUM,
100*137c9e08SDaniel Golle 	.has_swsysrst_en = true,
101*137c9e08SDaniel Golle };
102*137c9e08SDaniel Golle 
103c254e103Syong.liang static const struct mtk_wdt_data mt8183_data = {
104c254e103Syong.liang 	.toprgu_sw_rst_num = MT8183_TOPRGU_SW_RST_NUM,
105c254e103Syong.liang };
106c254e103Syong.liang 
1074dbabc4dSRunyang Chen static const struct mtk_wdt_data mt8186_data = {
1084dbabc4dSRunyang Chen 	.toprgu_sw_rst_num = MT8186_TOPRGU_SW_RST_NUM,
1094dbabc4dSRunyang Chen };
1104dbabc4dSRunyang Chen 
111bc731365SRunyang Chen static const struct mtk_wdt_data mt8188_data = {
112bc731365SRunyang Chen 	.toprgu_sw_rst_num = MT8188_TOPRGU_SW_RST_NUM,
113bc731365SRunyang Chen };
114bc731365SRunyang Chen 
115adc318a3SCrystal Guo static const struct mtk_wdt_data mt8192_data = {
116adc318a3SCrystal Guo 	.toprgu_sw_rst_num = MT8192_TOPRGU_SW_RST_NUM,
117adc318a3SCrystal Guo };
118adc318a3SCrystal Guo 
1198c6b5ea6SChristine Zhu static const struct mtk_wdt_data mt8195_data = {
1208c6b5ea6SChristine Zhu 	.toprgu_sw_rst_num = MT8195_TOPRGU_SW_RST_NUM,
1218c6b5ea6SChristine Zhu };
1228c6b5ea6SChristine Zhu 
123*137c9e08SDaniel Golle /**
124*137c9e08SDaniel Golle  * toprgu_reset_sw_en_unlocked() - enable/disable software control for reset bit
125*137c9e08SDaniel Golle  * @data: Pointer to instance of driver data.
126*137c9e08SDaniel Golle  * @id: Bit number identifying the reset to be enabled or disabled.
127*137c9e08SDaniel Golle  * @enable: If true, enable software control for that bit, disable otherwise.
128*137c9e08SDaniel Golle  *
129*137c9e08SDaniel Golle  * Context: The caller must hold lock of struct mtk_wdt_dev.
130*137c9e08SDaniel Golle  */
toprgu_reset_sw_en_unlocked(struct mtk_wdt_dev * data,unsigned long id,bool enable)131*137c9e08SDaniel Golle static void toprgu_reset_sw_en_unlocked(struct mtk_wdt_dev *data,
132*137c9e08SDaniel Golle 					unsigned long id, bool enable)
133*137c9e08SDaniel Golle {
134*137c9e08SDaniel Golle 	u32 tmp;
135*137c9e08SDaniel Golle 
136*137c9e08SDaniel Golle 	tmp = readl(data->wdt_base + WDT_SWSYSRST_EN);
137*137c9e08SDaniel Golle 	if (enable)
138*137c9e08SDaniel Golle 		tmp |= BIT(id);
139*137c9e08SDaniel Golle 	else
140*137c9e08SDaniel Golle 		tmp &= ~BIT(id);
141*137c9e08SDaniel Golle 
142*137c9e08SDaniel Golle 	writel(tmp, data->wdt_base + WDT_SWSYSRST_EN);
143*137c9e08SDaniel Golle }
144*137c9e08SDaniel Golle 
toprgu_reset_update(struct reset_controller_dev * rcdev,unsigned long id,bool assert)145c254e103Syong.liang static int toprgu_reset_update(struct reset_controller_dev *rcdev,
146c254e103Syong.liang 			       unsigned long id, bool assert)
147c254e103Syong.liang {
148c254e103Syong.liang 	unsigned int tmp;
149c254e103Syong.liang 	unsigned long flags;
150c254e103Syong.liang 	struct mtk_wdt_dev *data =
151c254e103Syong.liang 		 container_of(rcdev, struct mtk_wdt_dev, rcdev);
152c254e103Syong.liang 
153c254e103Syong.liang 	spin_lock_irqsave(&data->lock, flags);
154c254e103Syong.liang 
155*137c9e08SDaniel Golle 	if (assert && data->has_swsysrst_en)
156*137c9e08SDaniel Golle 		toprgu_reset_sw_en_unlocked(data, id, true);
157*137c9e08SDaniel Golle 
158c254e103Syong.liang 	tmp = readl(data->wdt_base + WDT_SWSYSRST);
159c254e103Syong.liang 	if (assert)
160c254e103Syong.liang 		tmp |= BIT(id);
161c254e103Syong.liang 	else
162c254e103Syong.liang 		tmp &= ~BIT(id);
163c254e103Syong.liang 	tmp |= WDT_SWSYS_RST_KEY;
164c254e103Syong.liang 	writel(tmp, data->wdt_base + WDT_SWSYSRST);
165c254e103Syong.liang 
166*137c9e08SDaniel Golle 	if (!assert && data->has_swsysrst_en)
167*137c9e08SDaniel Golle 		toprgu_reset_sw_en_unlocked(data, id, false);
168*137c9e08SDaniel Golle 
169c254e103Syong.liang 	spin_unlock_irqrestore(&data->lock, flags);
170c254e103Syong.liang 
171c254e103Syong.liang 	return 0;
172c254e103Syong.liang }
173c254e103Syong.liang 
toprgu_reset_assert(struct reset_controller_dev * rcdev,unsigned long id)174c254e103Syong.liang static int toprgu_reset_assert(struct reset_controller_dev *rcdev,
175c254e103Syong.liang 			       unsigned long id)
176c254e103Syong.liang {
177c254e103Syong.liang 	return toprgu_reset_update(rcdev, id, true);
178c254e103Syong.liang }
179c254e103Syong.liang 
toprgu_reset_deassert(struct reset_controller_dev * rcdev,unsigned long id)180c254e103Syong.liang static int toprgu_reset_deassert(struct reset_controller_dev *rcdev,
181c254e103Syong.liang 				 unsigned long id)
182c254e103Syong.liang {
183c254e103Syong.liang 	return toprgu_reset_update(rcdev, id, false);
184c254e103Syong.liang }
185c254e103Syong.liang 
toprgu_reset(struct reset_controller_dev * rcdev,unsigned long id)186c254e103Syong.liang static int toprgu_reset(struct reset_controller_dev *rcdev,
187c254e103Syong.liang 			unsigned long id)
188c254e103Syong.liang {
189c254e103Syong.liang 	int ret;
190c254e103Syong.liang 
191c254e103Syong.liang 	ret = toprgu_reset_assert(rcdev, id);
192c254e103Syong.liang 	if (ret)
193c254e103Syong.liang 		return ret;
194c254e103Syong.liang 
195c254e103Syong.liang 	return toprgu_reset_deassert(rcdev, id);
196c254e103Syong.liang }
197c254e103Syong.liang 
198c254e103Syong.liang static const struct reset_control_ops toprgu_reset_ops = {
199c254e103Syong.liang 	.assert = toprgu_reset_assert,
200c254e103Syong.liang 	.deassert = toprgu_reset_deassert,
201c254e103Syong.liang 	.reset = toprgu_reset,
202c254e103Syong.liang };
203c254e103Syong.liang 
toprgu_register_reset_controller(struct platform_device * pdev,int rst_num)204c254e103Syong.liang static int toprgu_register_reset_controller(struct platform_device *pdev,
205c254e103Syong.liang 					    int rst_num)
206c254e103Syong.liang {
207c254e103Syong.liang 	int ret;
208c254e103Syong.liang 	struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
209c254e103Syong.liang 
210c254e103Syong.liang 	spin_lock_init(&mtk_wdt->lock);
211c254e103Syong.liang 
212c254e103Syong.liang 	mtk_wdt->rcdev.owner = THIS_MODULE;
213c254e103Syong.liang 	mtk_wdt->rcdev.nr_resets = rst_num;
214c254e103Syong.liang 	mtk_wdt->rcdev.ops = &toprgu_reset_ops;
215c254e103Syong.liang 	mtk_wdt->rcdev.of_node = pdev->dev.of_node;
216c254e103Syong.liang 	ret = devm_reset_controller_register(&pdev->dev, &mtk_wdt->rcdev);
217c254e103Syong.liang 	if (ret != 0)
218c254e103Syong.liang 		dev_err(&pdev->dev,
219c254e103Syong.liang 			"couldn't register wdt reset controller: %d\n", ret);
220c254e103Syong.liang 	return ret;
221c254e103Syong.liang }
222c254e103Syong.liang 
mtk_wdt_restart(struct watchdog_device * wdt_dev,unsigned long action,void * data)2234d8b229dSGuenter Roeck static int mtk_wdt_restart(struct watchdog_device *wdt_dev,
2244d8b229dSGuenter Roeck 			   unsigned long action, void *data)
225a44a4553SMatthias Brugger {
226e86adc3fSDamien Riegel 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
227a44a4553SMatthias Brugger 	void __iomem *wdt_base;
228a44a4553SMatthias Brugger 
229a44a4553SMatthias Brugger 	wdt_base = mtk_wdt->wdt_base;
230a44a4553SMatthias Brugger 
231a44a4553SMatthias Brugger 	while (1) {
232a44a4553SMatthias Brugger 		writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST);
233a44a4553SMatthias Brugger 		mdelay(5);
234a44a4553SMatthias Brugger 	}
235a44a4553SMatthias Brugger 
236e86adc3fSDamien Riegel 	return 0;
237a44a4553SMatthias Brugger }
238a44a4553SMatthias Brugger 
mtk_wdt_ping(struct watchdog_device * wdt_dev)239a44a4553SMatthias Brugger static int mtk_wdt_ping(struct watchdog_device *wdt_dev)
240a44a4553SMatthias Brugger {
241a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
242a44a4553SMatthias Brugger 	void __iomem *wdt_base = mtk_wdt->wdt_base;
243a44a4553SMatthias Brugger 
244a44a4553SMatthias Brugger 	iowrite32(WDT_RST_RELOAD, wdt_base + WDT_RST);
245a44a4553SMatthias Brugger 
246a44a4553SMatthias Brugger 	return 0;
247a44a4553SMatthias Brugger }
248a44a4553SMatthias Brugger 
mtk_wdt_set_timeout(struct watchdog_device * wdt_dev,unsigned int timeout)249a44a4553SMatthias Brugger static int mtk_wdt_set_timeout(struct watchdog_device *wdt_dev,
250a44a4553SMatthias Brugger 				unsigned int timeout)
251a44a4553SMatthias Brugger {
252a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
253a44a4553SMatthias Brugger 	void __iomem *wdt_base = mtk_wdt->wdt_base;
254a44a4553SMatthias Brugger 	u32 reg;
255a44a4553SMatthias Brugger 
256a44a4553SMatthias Brugger 	wdt_dev->timeout = timeout;
2571bbce779SWang Qing 	/*
2581bbce779SWang Qing 	 * In dual mode, irq will be triggered at timeout / 2
2591bbce779SWang Qing 	 * the real timeout occurs at timeout
2601bbce779SWang Qing 	 */
2611bbce779SWang Qing 	if (wdt_dev->pretimeout)
2621bbce779SWang Qing 		wdt_dev->pretimeout = timeout / 2;
263a44a4553SMatthias Brugger 
264a44a4553SMatthias Brugger 	/*
265a44a4553SMatthias Brugger 	 * One bit is the value of 512 ticks
266a44a4553SMatthias Brugger 	 * The clock has 32 KHz
267a44a4553SMatthias Brugger 	 */
2681bbce779SWang Qing 	reg = WDT_LENGTH_TIMEOUT((timeout - wdt_dev->pretimeout) << 6)
2691bbce779SWang Qing 			| WDT_LENGTH_KEY;
270a44a4553SMatthias Brugger 	iowrite32(reg, wdt_base + WDT_LENGTH);
271a44a4553SMatthias Brugger 
272a44a4553SMatthias Brugger 	mtk_wdt_ping(wdt_dev);
273a44a4553SMatthias Brugger 
274a44a4553SMatthias Brugger 	return 0;
275a44a4553SMatthias Brugger }
276a44a4553SMatthias Brugger 
mtk_wdt_init(struct watchdog_device * wdt_dev)277bbece05cSfreddy.hsin static void mtk_wdt_init(struct watchdog_device *wdt_dev)
278bbece05cSfreddy.hsin {
279bbece05cSfreddy.hsin 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
280bbece05cSfreddy.hsin 	void __iomem *wdt_base;
281bbece05cSfreddy.hsin 
282bbece05cSfreddy.hsin 	wdt_base = mtk_wdt->wdt_base;
283bbece05cSfreddy.hsin 
284bbece05cSfreddy.hsin 	if (readl(wdt_base + WDT_MODE) & WDT_MODE_EN) {
285bbece05cSfreddy.hsin 		set_bit(WDOG_HW_RUNNING, &wdt_dev->status);
286bbece05cSfreddy.hsin 		mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
287bbece05cSfreddy.hsin 	}
288bbece05cSfreddy.hsin }
289bbece05cSfreddy.hsin 
mtk_wdt_stop(struct watchdog_device * wdt_dev)290a44a4553SMatthias Brugger static int mtk_wdt_stop(struct watchdog_device *wdt_dev)
291a44a4553SMatthias Brugger {
292a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
293a44a4553SMatthias Brugger 	void __iomem *wdt_base = mtk_wdt->wdt_base;
294a44a4553SMatthias Brugger 	u32 reg;
295a44a4553SMatthias Brugger 
296a44a4553SMatthias Brugger 	reg = readl(wdt_base + WDT_MODE);
297a44a4553SMatthias Brugger 	reg &= ~WDT_MODE_EN;
2985da2bf1aSNicolas Boichat 	reg |= WDT_MODE_KEY;
299a44a4553SMatthias Brugger 	iowrite32(reg, wdt_base + WDT_MODE);
300a44a4553SMatthias Brugger 
301a44a4553SMatthias Brugger 	return 0;
302a44a4553SMatthias Brugger }
303a44a4553SMatthias Brugger 
mtk_wdt_start(struct watchdog_device * wdt_dev)304a44a4553SMatthias Brugger static int mtk_wdt_start(struct watchdog_device *wdt_dev)
305a44a4553SMatthias Brugger {
306a44a4553SMatthias Brugger 	u32 reg;
307a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
308a44a4553SMatthias Brugger 	void __iomem *wdt_base = mtk_wdt->wdt_base;
3099ffd906dSDan Carpenter 	int ret;
310a44a4553SMatthias Brugger 
311a44a4553SMatthias Brugger 	ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
312a44a4553SMatthias Brugger 	if (ret < 0)
313a44a4553SMatthias Brugger 		return ret;
314a44a4553SMatthias Brugger 
315a44a4553SMatthias Brugger 	reg = ioread32(wdt_base + WDT_MODE);
3161bbce779SWang Qing 	if (wdt_dev->pretimeout)
3171bbce779SWang Qing 		reg |= (WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
3181bbce779SWang Qing 	else
319a44a4553SMatthias Brugger 		reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
32059b0f513SFengquan Chen 	if (mtk_wdt->disable_wdt_extrst)
32159b0f513SFengquan Chen 		reg &= ~WDT_MODE_EXRST_EN;
322a224764fSAllen-KH Cheng 	if (mtk_wdt->reset_by_toprgu)
323a224764fSAllen-KH Cheng 		reg |= WDT_MODE_CNT_SEL;
324a44a4553SMatthias Brugger 	reg |= (WDT_MODE_EN | WDT_MODE_KEY);
325a44a4553SMatthias Brugger 	iowrite32(reg, wdt_base + WDT_MODE);
326a44a4553SMatthias Brugger 
327a44a4553SMatthias Brugger 	return 0;
328a44a4553SMatthias Brugger }
329a44a4553SMatthias Brugger 
mtk_wdt_set_pretimeout(struct watchdog_device * wdd,unsigned int timeout)3301bbce779SWang Qing static int mtk_wdt_set_pretimeout(struct watchdog_device *wdd,
3311bbce779SWang Qing 				  unsigned int timeout)
3321bbce779SWang Qing {
3331bbce779SWang Qing 	struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdd);
3341bbce779SWang Qing 	void __iomem *wdt_base = mtk_wdt->wdt_base;
3351bbce779SWang Qing 	u32 reg = ioread32(wdt_base + WDT_MODE);
3361bbce779SWang Qing 
3371bbce779SWang Qing 	if (timeout && !wdd->pretimeout) {
3381bbce779SWang Qing 		wdd->pretimeout = wdd->timeout / 2;
3391bbce779SWang Qing 		reg |= (WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
3401bbce779SWang Qing 	} else if (!timeout && wdd->pretimeout) {
3411bbce779SWang Qing 		wdd->pretimeout = 0;
3421bbce779SWang Qing 		reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
3431bbce779SWang Qing 	} else {
3441bbce779SWang Qing 		return 0;
3451bbce779SWang Qing 	}
3461bbce779SWang Qing 
3471bbce779SWang Qing 	reg |= WDT_MODE_KEY;
3481bbce779SWang Qing 	iowrite32(reg, wdt_base + WDT_MODE);
3491bbce779SWang Qing 
3501bbce779SWang Qing 	return mtk_wdt_set_timeout(wdd, wdd->timeout);
3511bbce779SWang Qing }
3521bbce779SWang Qing 
mtk_wdt_isr(int irq,void * arg)3531bbce779SWang Qing static irqreturn_t mtk_wdt_isr(int irq, void *arg)
3541bbce779SWang Qing {
3551bbce779SWang Qing 	struct watchdog_device *wdd = arg;
3561bbce779SWang Qing 
3571bbce779SWang Qing 	watchdog_notify_pretimeout(wdd);
3581bbce779SWang Qing 
3591bbce779SWang Qing 	return IRQ_HANDLED;
3601bbce779SWang Qing }
3611bbce779SWang Qing 
362a44a4553SMatthias Brugger static const struct watchdog_info mtk_wdt_info = {
363a44a4553SMatthias Brugger 	.identity	= DRV_NAME,
364a44a4553SMatthias Brugger 	.options	= WDIOF_SETTIMEOUT |
365a44a4553SMatthias Brugger 			  WDIOF_KEEPALIVEPING |
366a44a4553SMatthias Brugger 			  WDIOF_MAGICCLOSE,
367a44a4553SMatthias Brugger };
368a44a4553SMatthias Brugger 
3691bbce779SWang Qing static const struct watchdog_info mtk_wdt_pt_info = {
3701bbce779SWang Qing 	.identity	= DRV_NAME,
3711bbce779SWang Qing 	.options	= WDIOF_SETTIMEOUT |
3721bbce779SWang Qing 			  WDIOF_PRETIMEOUT |
3731bbce779SWang Qing 			  WDIOF_KEEPALIVEPING |
3741bbce779SWang Qing 			  WDIOF_MAGICCLOSE,
3751bbce779SWang Qing };
3761bbce779SWang Qing 
377a44a4553SMatthias Brugger static const struct watchdog_ops mtk_wdt_ops = {
378a44a4553SMatthias Brugger 	.owner		= THIS_MODULE,
379a44a4553SMatthias Brugger 	.start		= mtk_wdt_start,
380a44a4553SMatthias Brugger 	.stop		= mtk_wdt_stop,
381a44a4553SMatthias Brugger 	.ping		= mtk_wdt_ping,
382a44a4553SMatthias Brugger 	.set_timeout	= mtk_wdt_set_timeout,
3831bbce779SWang Qing 	.set_pretimeout	= mtk_wdt_set_pretimeout,
384e86adc3fSDamien Riegel 	.restart	= mtk_wdt_restart,
385a44a4553SMatthias Brugger };
386a44a4553SMatthias Brugger 
mtk_wdt_probe(struct platform_device * pdev)387a44a4553SMatthias Brugger static int mtk_wdt_probe(struct platform_device *pdev)
388a44a4553SMatthias Brugger {
389a15f6e64SGuenter Roeck 	struct device *dev = &pdev->dev;
390a44a4553SMatthias Brugger 	struct mtk_wdt_dev *mtk_wdt;
391c254e103Syong.liang 	const struct mtk_wdt_data *wdt_data;
3921bbce779SWang Qing 	int err, irq;
393a44a4553SMatthias Brugger 
394a15f6e64SGuenter Roeck 	mtk_wdt = devm_kzalloc(dev, sizeof(*mtk_wdt), GFP_KERNEL);
395a44a4553SMatthias Brugger 	if (!mtk_wdt)
396a44a4553SMatthias Brugger 		return -ENOMEM;
397a44a4553SMatthias Brugger 
398a44a4553SMatthias Brugger 	platform_set_drvdata(pdev, mtk_wdt);
399a44a4553SMatthias Brugger 
4000f0a6a28SGuenter Roeck 	mtk_wdt->wdt_base = devm_platform_ioremap_resource(pdev, 0);
401a44a4553SMatthias Brugger 	if (IS_ERR(mtk_wdt->wdt_base))
402a44a4553SMatthias Brugger 		return PTR_ERR(mtk_wdt->wdt_base);
403a44a4553SMatthias Brugger 
4041bafac47STzung-Bi Shih 	irq = platform_get_irq_optional(pdev, 0);
4051bbce779SWang Qing 	if (irq > 0) {
4061bbce779SWang Qing 		err = devm_request_irq(&pdev->dev, irq, mtk_wdt_isr, 0, "wdt_bark",
4071bbce779SWang Qing 				       &mtk_wdt->wdt_dev);
4081bbce779SWang Qing 		if (err)
4091bbce779SWang Qing 			return err;
4101bbce779SWang Qing 
4111bbce779SWang Qing 		mtk_wdt->wdt_dev.info = &mtk_wdt_pt_info;
4121bbce779SWang Qing 		mtk_wdt->wdt_dev.pretimeout = WDT_MAX_TIMEOUT / 2;
4131bbce779SWang Qing 	} else {
4141bbce779SWang Qing 		if (irq == -EPROBE_DEFER)
4151bbce779SWang Qing 			return -EPROBE_DEFER;
4161bbce779SWang Qing 
417a44a4553SMatthias Brugger 		mtk_wdt->wdt_dev.info = &mtk_wdt_info;
4181bbce779SWang Qing 	}
4191bbce779SWang Qing 
420a44a4553SMatthias Brugger 	mtk_wdt->wdt_dev.ops = &mtk_wdt_ops;
421a44a4553SMatthias Brugger 	mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
422bbece05cSfreddy.hsin 	mtk_wdt->wdt_dev.max_hw_heartbeat_ms = WDT_MAX_TIMEOUT * 1000;
423a44a4553SMatthias Brugger 	mtk_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
424a15f6e64SGuenter Roeck 	mtk_wdt->wdt_dev.parent = dev;
425a44a4553SMatthias Brugger 
426a15f6e64SGuenter Roeck 	watchdog_init_timeout(&mtk_wdt->wdt_dev, timeout, dev);
427a44a4553SMatthias Brugger 	watchdog_set_nowayout(&mtk_wdt->wdt_dev, nowayout);
428e86adc3fSDamien Riegel 	watchdog_set_restart_priority(&mtk_wdt->wdt_dev, 128);
429a44a4553SMatthias Brugger 
430a44a4553SMatthias Brugger 	watchdog_set_drvdata(&mtk_wdt->wdt_dev, mtk_wdt);
431a44a4553SMatthias Brugger 
432bbece05cSfreddy.hsin 	mtk_wdt_init(&mtk_wdt->wdt_dev);
433a44a4553SMatthias Brugger 
434a15f6e64SGuenter Roeck 	watchdog_stop_on_reboot(&mtk_wdt->wdt_dev);
435a15f6e64SGuenter Roeck 	err = devm_watchdog_register_device(dev, &mtk_wdt->wdt_dev);
436a44a4553SMatthias Brugger 	if (unlikely(err))
437a44a4553SMatthias Brugger 		return err;
438a44a4553SMatthias Brugger 
439a15f6e64SGuenter Roeck 	dev_info(dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
440a44a4553SMatthias Brugger 		 mtk_wdt->wdt_dev.timeout, nowayout);
441a44a4553SMatthias Brugger 
442c254e103Syong.liang 	wdt_data = of_device_get_match_data(dev);
443c254e103Syong.liang 	if (wdt_data) {
444c254e103Syong.liang 		err = toprgu_register_reset_controller(pdev,
445c254e103Syong.liang 						       wdt_data->toprgu_sw_rst_num);
446c254e103Syong.liang 		if (err)
447c254e103Syong.liang 			return err;
448*137c9e08SDaniel Golle 
449*137c9e08SDaniel Golle 		mtk_wdt->has_swsysrst_en = wdt_data->has_swsysrst_en;
450c254e103Syong.liang 	}
45159b0f513SFengquan Chen 
45259b0f513SFengquan Chen 	mtk_wdt->disable_wdt_extrst =
45359b0f513SFengquan Chen 		of_property_read_bool(dev->of_node, "mediatek,disable-extrst");
45459b0f513SFengquan Chen 
455a224764fSAllen-KH Cheng 	mtk_wdt->reset_by_toprgu =
456a224764fSAllen-KH Cheng 		of_property_read_bool(dev->of_node, "mediatek,reset-by-toprgu");
457a224764fSAllen-KH Cheng 
458a44a4553SMatthias Brugger 	return 0;
459a44a4553SMatthias Brugger }
460a44a4553SMatthias Brugger 
mtk_wdt_suspend(struct device * dev)4619fab0692SGreta Zhang static int mtk_wdt_suspend(struct device *dev)
4629fab0692SGreta Zhang {
4639fab0692SGreta Zhang 	struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
4649fab0692SGreta Zhang 
4659fab0692SGreta Zhang 	if (watchdog_active(&mtk_wdt->wdt_dev))
4669fab0692SGreta Zhang 		mtk_wdt_stop(&mtk_wdt->wdt_dev);
4679fab0692SGreta Zhang 
4689fab0692SGreta Zhang 	return 0;
4699fab0692SGreta Zhang }
4709fab0692SGreta Zhang 
mtk_wdt_resume(struct device * dev)4719fab0692SGreta Zhang static int mtk_wdt_resume(struct device *dev)
4729fab0692SGreta Zhang {
4739fab0692SGreta Zhang 	struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
4749fab0692SGreta Zhang 
4759fab0692SGreta Zhang 	if (watchdog_active(&mtk_wdt->wdt_dev)) {
4769fab0692SGreta Zhang 		mtk_wdt_start(&mtk_wdt->wdt_dev);
4779fab0692SGreta Zhang 		mtk_wdt_ping(&mtk_wdt->wdt_dev);
4789fab0692SGreta Zhang 	}
4799fab0692SGreta Zhang 
4809fab0692SGreta Zhang 	return 0;
4819fab0692SGreta Zhang }
4829fab0692SGreta Zhang 
483a44a4553SMatthias Brugger static const struct of_device_id mtk_wdt_dt_ids[] = {
4849e5236e7Syong.liang 	{ .compatible = "mediatek,mt2712-wdt", .data = &mt2712_data },
485a44a4553SMatthias Brugger 	{ .compatible = "mediatek,mt6589-wdt" },
48628927f6cSAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt6795-wdt", .data = &mt6795_data },
487711a5b25SSam Shih 	{ .compatible = "mediatek,mt7986-wdt", .data = &mt7986_data },
488*137c9e08SDaniel Golle 	{ .compatible = "mediatek,mt7988-wdt", .data = &mt7988_data },
489c254e103Syong.liang 	{ .compatible = "mediatek,mt8183-wdt", .data = &mt8183_data },
4904dbabc4dSRunyang Chen 	{ .compatible = "mediatek,mt8186-wdt", .data = &mt8186_data },
491bc731365SRunyang Chen 	{ .compatible = "mediatek,mt8188-wdt", .data = &mt8188_data },
492adc318a3SCrystal Guo 	{ .compatible = "mediatek,mt8192-wdt", .data = &mt8192_data },
4938c6b5ea6SChristine Zhu 	{ .compatible = "mediatek,mt8195-wdt", .data = &mt8195_data },
494a44a4553SMatthias Brugger 	{ /* sentinel */ }
495a44a4553SMatthias Brugger };
496a44a4553SMatthias Brugger MODULE_DEVICE_TABLE(of, mtk_wdt_dt_ids);
497a44a4553SMatthias Brugger 
498d4777d0fSPaul Cercueil static DEFINE_SIMPLE_DEV_PM_OPS(mtk_wdt_pm_ops,
499d4777d0fSPaul Cercueil 				mtk_wdt_suspend, mtk_wdt_resume);
5009fab0692SGreta Zhang 
501a44a4553SMatthias Brugger static struct platform_driver mtk_wdt_driver = {
502a44a4553SMatthias Brugger 	.probe		= mtk_wdt_probe,
503a44a4553SMatthias Brugger 	.driver		= {
504a44a4553SMatthias Brugger 		.name		= DRV_NAME,
505d4777d0fSPaul Cercueil 		.pm		= pm_sleep_ptr(&mtk_wdt_pm_ops),
506a44a4553SMatthias Brugger 		.of_match_table	= mtk_wdt_dt_ids,
507a44a4553SMatthias Brugger 	},
508a44a4553SMatthias Brugger };
509a44a4553SMatthias Brugger 
510a44a4553SMatthias Brugger module_platform_driver(mtk_wdt_driver);
511a44a4553SMatthias Brugger 
512a44a4553SMatthias Brugger module_param(timeout, uint, 0);
513a44a4553SMatthias Brugger MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
514a44a4553SMatthias Brugger 
515a44a4553SMatthias Brugger module_param(nowayout, bool, 0);
516a44a4553SMatthias Brugger MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
517a44a4553SMatthias Brugger 			__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
518a44a4553SMatthias Brugger 
519a44a4553SMatthias Brugger MODULE_LICENSE("GPL");
520a44a4553SMatthias Brugger MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
521a44a4553SMatthias Brugger MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
522a44a4553SMatthias Brugger MODULE_VERSION(DRV_VERSION);
523