1 // SPDX-License-Identifier: GPL-2.0+
3 * Mediatek Watchdog Driver
5 * Copyright (C) 2014 Matthias Brugger
7 * Matthias Brugger <matthias.bgg@gmail.com>
12 #include <dt-bindings/reset-controller/mt2712-resets.h>
13 #include <dt-bindings/reset-controller/mt8183-resets.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/reset-controller.h>
25 #include <linux/types.h>
26 #include <linux/watchdog.h>
28 #define WDT_MAX_TIMEOUT 31
29 #define WDT_MIN_TIMEOUT 1
30 #define WDT_LENGTH_TIMEOUT(n) ((n) << 5)
32 #define WDT_LENGTH 0x04
33 #define WDT_LENGTH_KEY 0x8
36 #define WDT_RST_RELOAD 0x1971
39 #define WDT_MODE_EN (1 << 0)
40 #define WDT_MODE_EXT_POL_LOW (0 << 1)
41 #define WDT_MODE_EXT_POL_HIGH (1 << 1)
42 #define WDT_MODE_EXRST_EN (1 << 2)
43 #define WDT_MODE_IRQ_EN (1 << 3)
44 #define WDT_MODE_AUTO_START (1 << 4)
45 #define WDT_MODE_DUAL_EN (1 << 6)
46 #define WDT_MODE_KEY 0x22000000
48 #define WDT_SWRST 0x14
49 #define WDT_SWRST_KEY 0x1209
51 #define WDT_SWSYSRST 0x18U
52 #define WDT_SWSYS_RST_KEY 0x88000000
54 #define DRV_NAME "mtk-wdt"
55 #define DRV_VERSION "1.0"
57 static bool nowayout
= WATCHDOG_NOWAYOUT
;
58 static unsigned int timeout
;
61 struct watchdog_device wdt_dev
;
62 void __iomem
*wdt_base
;
63 spinlock_t lock
; /* protects WDT_SWSYSRST reg */
64 struct reset_controller_dev rcdev
;
68 int toprgu_sw_rst_num
;
71 static const struct mtk_wdt_data mt2712_data
= {
72 .toprgu_sw_rst_num
= MT2712_TOPRGU_SW_RST_NUM
,
75 static const struct mtk_wdt_data mt8183_data
= {
76 .toprgu_sw_rst_num
= MT8183_TOPRGU_SW_RST_NUM
,
79 static int toprgu_reset_update(struct reset_controller_dev
*rcdev
,
80 unsigned long id
, bool assert)
84 struct mtk_wdt_dev
*data
=
85 container_of(rcdev
, struct mtk_wdt_dev
, rcdev
);
87 spin_lock_irqsave(&data
->lock
, flags
);
89 tmp
= readl(data
->wdt_base
+ WDT_SWSYSRST
);
94 tmp
|= WDT_SWSYS_RST_KEY
;
95 writel(tmp
, data
->wdt_base
+ WDT_SWSYSRST
);
97 spin_unlock_irqrestore(&data
->lock
, flags
);
102 static int toprgu_reset_assert(struct reset_controller_dev
*rcdev
,
105 return toprgu_reset_update(rcdev
, id
, true);
108 static int toprgu_reset_deassert(struct reset_controller_dev
*rcdev
,
111 return toprgu_reset_update(rcdev
, id
, false);
114 static int toprgu_reset(struct reset_controller_dev
*rcdev
,
119 ret
= toprgu_reset_assert(rcdev
, id
);
123 return toprgu_reset_deassert(rcdev
, id
);
126 static const struct reset_control_ops toprgu_reset_ops
= {
127 .assert = toprgu_reset_assert
,
128 .deassert
= toprgu_reset_deassert
,
129 .reset
= toprgu_reset
,
132 static int toprgu_register_reset_controller(struct platform_device
*pdev
,
136 struct mtk_wdt_dev
*mtk_wdt
= platform_get_drvdata(pdev
);
138 spin_lock_init(&mtk_wdt
->lock
);
140 mtk_wdt
->rcdev
.owner
= THIS_MODULE
;
141 mtk_wdt
->rcdev
.nr_resets
= rst_num
;
142 mtk_wdt
->rcdev
.ops
= &toprgu_reset_ops
;
143 mtk_wdt
->rcdev
.of_node
= pdev
->dev
.of_node
;
144 ret
= devm_reset_controller_register(&pdev
->dev
, &mtk_wdt
->rcdev
);
147 "couldn't register wdt reset controller: %d\n", ret
);
151 static int mtk_wdt_restart(struct watchdog_device
*wdt_dev
,
152 unsigned long action
, void *data
)
154 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
155 void __iomem
*wdt_base
;
157 wdt_base
= mtk_wdt
->wdt_base
;
160 writel(WDT_SWRST_KEY
, wdt_base
+ WDT_SWRST
);
167 static int mtk_wdt_ping(struct watchdog_device
*wdt_dev
)
169 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
170 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
172 iowrite32(WDT_RST_RELOAD
, wdt_base
+ WDT_RST
);
177 static int mtk_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
178 unsigned int timeout
)
180 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
181 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
184 wdt_dev
->timeout
= timeout
;
187 * One bit is the value of 512 ticks
188 * The clock has 32 KHz
190 reg
= WDT_LENGTH_TIMEOUT(timeout
<< 6) | WDT_LENGTH_KEY
;
191 iowrite32(reg
, wdt_base
+ WDT_LENGTH
);
193 mtk_wdt_ping(wdt_dev
);
198 static int mtk_wdt_stop(struct watchdog_device
*wdt_dev
)
200 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
201 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
204 reg
= readl(wdt_base
+ WDT_MODE
);
207 iowrite32(reg
, wdt_base
+ WDT_MODE
);
212 static int mtk_wdt_start(struct watchdog_device
*wdt_dev
)
215 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
216 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
219 ret
= mtk_wdt_set_timeout(wdt_dev
, wdt_dev
->timeout
);
223 reg
= ioread32(wdt_base
+ WDT_MODE
);
224 reg
&= ~(WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
225 reg
|= (WDT_MODE_EN
| WDT_MODE_KEY
);
226 iowrite32(reg
, wdt_base
+ WDT_MODE
);
231 static const struct watchdog_info mtk_wdt_info
= {
232 .identity
= DRV_NAME
,
233 .options
= WDIOF_SETTIMEOUT
|
234 WDIOF_KEEPALIVEPING
|
238 static const struct watchdog_ops mtk_wdt_ops
= {
239 .owner
= THIS_MODULE
,
240 .start
= mtk_wdt_start
,
241 .stop
= mtk_wdt_stop
,
242 .ping
= mtk_wdt_ping
,
243 .set_timeout
= mtk_wdt_set_timeout
,
244 .restart
= mtk_wdt_restart
,
247 static int mtk_wdt_probe(struct platform_device
*pdev
)
249 struct device
*dev
= &pdev
->dev
;
250 struct mtk_wdt_dev
*mtk_wdt
;
251 const struct mtk_wdt_data
*wdt_data
;
254 mtk_wdt
= devm_kzalloc(dev
, sizeof(*mtk_wdt
), GFP_KERNEL
);
258 platform_set_drvdata(pdev
, mtk_wdt
);
260 mtk_wdt
->wdt_base
= devm_platform_ioremap_resource(pdev
, 0);
261 if (IS_ERR(mtk_wdt
->wdt_base
))
262 return PTR_ERR(mtk_wdt
->wdt_base
);
264 mtk_wdt
->wdt_dev
.info
= &mtk_wdt_info
;
265 mtk_wdt
->wdt_dev
.ops
= &mtk_wdt_ops
;
266 mtk_wdt
->wdt_dev
.timeout
= WDT_MAX_TIMEOUT
;
267 mtk_wdt
->wdt_dev
.max_timeout
= WDT_MAX_TIMEOUT
;
268 mtk_wdt
->wdt_dev
.min_timeout
= WDT_MIN_TIMEOUT
;
269 mtk_wdt
->wdt_dev
.parent
= dev
;
271 watchdog_init_timeout(&mtk_wdt
->wdt_dev
, timeout
, dev
);
272 watchdog_set_nowayout(&mtk_wdt
->wdt_dev
, nowayout
);
273 watchdog_set_restart_priority(&mtk_wdt
->wdt_dev
, 128);
275 watchdog_set_drvdata(&mtk_wdt
->wdt_dev
, mtk_wdt
);
277 mtk_wdt_stop(&mtk_wdt
->wdt_dev
);
279 watchdog_stop_on_reboot(&mtk_wdt
->wdt_dev
);
280 err
= devm_watchdog_register_device(dev
, &mtk_wdt
->wdt_dev
);
284 dev_info(dev
, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
285 mtk_wdt
->wdt_dev
.timeout
, nowayout
);
287 wdt_data
= of_device_get_match_data(dev
);
289 err
= toprgu_register_reset_controller(pdev
,
290 wdt_data
->toprgu_sw_rst_num
);
297 #ifdef CONFIG_PM_SLEEP
298 static int mtk_wdt_suspend(struct device
*dev
)
300 struct mtk_wdt_dev
*mtk_wdt
= dev_get_drvdata(dev
);
302 if (watchdog_active(&mtk_wdt
->wdt_dev
))
303 mtk_wdt_stop(&mtk_wdt
->wdt_dev
);
308 static int mtk_wdt_resume(struct device
*dev
)
310 struct mtk_wdt_dev
*mtk_wdt
= dev_get_drvdata(dev
);
312 if (watchdog_active(&mtk_wdt
->wdt_dev
)) {
313 mtk_wdt_start(&mtk_wdt
->wdt_dev
);
314 mtk_wdt_ping(&mtk_wdt
->wdt_dev
);
321 static const struct of_device_id mtk_wdt_dt_ids
[] = {
322 { .compatible
= "mediatek,mt2712-wdt", .data
= &mt2712_data
},
323 { .compatible
= "mediatek,mt6589-wdt" },
324 { .compatible
= "mediatek,mt8183-wdt", .data
= &mt8183_data
},
327 MODULE_DEVICE_TABLE(of
, mtk_wdt_dt_ids
);
329 static const struct dev_pm_ops mtk_wdt_pm_ops
= {
330 SET_SYSTEM_SLEEP_PM_OPS(mtk_wdt_suspend
,
334 static struct platform_driver mtk_wdt_driver
= {
335 .probe
= mtk_wdt_probe
,
338 .pm
= &mtk_wdt_pm_ops
,
339 .of_match_table
= mtk_wdt_dt_ids
,
343 module_platform_driver(mtk_wdt_driver
);
345 module_param(timeout
, uint
, 0);
346 MODULE_PARM_DESC(timeout
, "Watchdog heartbeat in seconds");
348 module_param(nowayout
, bool, 0);
349 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
350 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
352 MODULE_LICENSE("GPL");
353 MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
354 MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
355 MODULE_VERSION(DRV_VERSION
);