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/mt2712-resets.h>
13 #include <dt-bindings/reset/mediatek,mt6795-resets.h>
14 #include <dt-bindings/reset/mt7986-resets.h>
15 #include <dt-bindings/reset/mt8183-resets.h>
16 #include <dt-bindings/reset/mt8186-resets.h>
17 #include <dt-bindings/reset/mt8188-resets.h>
18 #include <dt-bindings/reset/mt8192-resets.h>
19 #include <dt-bindings/reset/mt8195-resets.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
28 #include <linux/platform_device.h>
29 #include <linux/reset-controller.h>
30 #include <linux/types.h>
31 #include <linux/watchdog.h>
32 #include <linux/interrupt.h>
34 #define WDT_MAX_TIMEOUT 31
35 #define WDT_MIN_TIMEOUT 2
36 #define WDT_LENGTH_TIMEOUT(n) ((n) << 5)
38 #define WDT_LENGTH 0x04
39 #define WDT_LENGTH_KEY 0x8
42 #define WDT_RST_RELOAD 0x1971
45 #define WDT_MODE_EN (1 << 0)
46 #define WDT_MODE_EXT_POL_LOW (0 << 1)
47 #define WDT_MODE_EXT_POL_HIGH (1 << 1)
48 #define WDT_MODE_EXRST_EN (1 << 2)
49 #define WDT_MODE_IRQ_EN (1 << 3)
50 #define WDT_MODE_AUTO_START (1 << 4)
51 #define WDT_MODE_DUAL_EN (1 << 6)
52 #define WDT_MODE_CNT_SEL (1 << 8)
53 #define WDT_MODE_KEY 0x22000000
55 #define WDT_SWRST 0x14
56 #define WDT_SWRST_KEY 0x1209
58 #define WDT_SWSYSRST 0x18U
59 #define WDT_SWSYS_RST_KEY 0x88000000
61 #define WDT_SWSYSRST_EN 0xfc
63 #define DRV_NAME "mtk-wdt"
64 #define DRV_VERSION "1.0"
66 #define MT7988_TOPRGU_SW_RST_NUM 24
68 static bool nowayout
= WATCHDOG_NOWAYOUT
;
69 static unsigned int timeout
;
72 struct watchdog_device wdt_dev
;
73 void __iomem
*wdt_base
;
74 spinlock_t lock
; /* protects WDT_SWSYSRST reg */
75 struct reset_controller_dev rcdev
;
76 bool disable_wdt_extrst
;
82 int toprgu_sw_rst_num
;
86 static const struct mtk_wdt_data mt2712_data
= {
87 .toprgu_sw_rst_num
= MT2712_TOPRGU_SW_RST_NUM
,
90 static const struct mtk_wdt_data mt6795_data
= {
91 .toprgu_sw_rst_num
= MT6795_TOPRGU_SW_RST_NUM
,
94 static const struct mtk_wdt_data mt7986_data
= {
95 .toprgu_sw_rst_num
= MT7986_TOPRGU_SW_RST_NUM
,
98 static const struct mtk_wdt_data mt7988_data
= {
99 .toprgu_sw_rst_num
= MT7988_TOPRGU_SW_RST_NUM
,
100 .has_swsysrst_en
= true,
103 static const struct mtk_wdt_data mt8183_data
= {
104 .toprgu_sw_rst_num
= MT8183_TOPRGU_SW_RST_NUM
,
107 static const struct mtk_wdt_data mt8186_data
= {
108 .toprgu_sw_rst_num
= MT8186_TOPRGU_SW_RST_NUM
,
111 static const struct mtk_wdt_data mt8188_data
= {
112 .toprgu_sw_rst_num
= MT8188_TOPRGU_SW_RST_NUM
,
115 static const struct mtk_wdt_data mt8192_data
= {
116 .toprgu_sw_rst_num
= MT8192_TOPRGU_SW_RST_NUM
,
119 static const struct mtk_wdt_data mt8195_data
= {
120 .toprgu_sw_rst_num
= MT8195_TOPRGU_SW_RST_NUM
,
124 * toprgu_reset_sw_en_unlocked() - enable/disable software control for reset bit
125 * @data: Pointer to instance of driver data.
126 * @id: Bit number identifying the reset to be enabled or disabled.
127 * @enable: If true, enable software control for that bit, disable otherwise.
129 * Context: The caller must hold lock of struct mtk_wdt_dev.
131 static void toprgu_reset_sw_en_unlocked(struct mtk_wdt_dev
*data
,
132 unsigned long id
, bool enable
)
136 tmp
= readl(data
->wdt_base
+ WDT_SWSYSRST_EN
);
142 writel(tmp
, data
->wdt_base
+ WDT_SWSYSRST_EN
);
145 static int toprgu_reset_update(struct reset_controller_dev
*rcdev
,
146 unsigned long id
, bool assert)
150 struct mtk_wdt_dev
*data
=
151 container_of(rcdev
, struct mtk_wdt_dev
, rcdev
);
153 spin_lock_irqsave(&data
->lock
, flags
);
155 if (assert && data
->has_swsysrst_en
)
156 toprgu_reset_sw_en_unlocked(data
, id
, true);
158 tmp
= readl(data
->wdt_base
+ WDT_SWSYSRST
);
163 tmp
|= WDT_SWSYS_RST_KEY
;
164 writel(tmp
, data
->wdt_base
+ WDT_SWSYSRST
);
166 if (!assert && data
->has_swsysrst_en
)
167 toprgu_reset_sw_en_unlocked(data
, id
, false);
169 spin_unlock_irqrestore(&data
->lock
, flags
);
174 static int toprgu_reset_assert(struct reset_controller_dev
*rcdev
,
177 return toprgu_reset_update(rcdev
, id
, true);
180 static int toprgu_reset_deassert(struct reset_controller_dev
*rcdev
,
183 return toprgu_reset_update(rcdev
, id
, false);
186 static int toprgu_reset(struct reset_controller_dev
*rcdev
,
191 ret
= toprgu_reset_assert(rcdev
, id
);
195 return toprgu_reset_deassert(rcdev
, id
);
198 static const struct reset_control_ops toprgu_reset_ops
= {
199 .assert = toprgu_reset_assert
,
200 .deassert
= toprgu_reset_deassert
,
201 .reset
= toprgu_reset
,
204 static int toprgu_register_reset_controller(struct platform_device
*pdev
,
208 struct mtk_wdt_dev
*mtk_wdt
= platform_get_drvdata(pdev
);
210 spin_lock_init(&mtk_wdt
->lock
);
212 mtk_wdt
->rcdev
.owner
= THIS_MODULE
;
213 mtk_wdt
->rcdev
.nr_resets
= rst_num
;
214 mtk_wdt
->rcdev
.ops
= &toprgu_reset_ops
;
215 mtk_wdt
->rcdev
.of_node
= pdev
->dev
.of_node
;
216 ret
= devm_reset_controller_register(&pdev
->dev
, &mtk_wdt
->rcdev
);
219 "couldn't register wdt reset controller: %d\n", ret
);
223 static int mtk_wdt_restart(struct watchdog_device
*wdt_dev
,
224 unsigned long action
, void *data
)
226 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
227 void __iomem
*wdt_base
;
229 wdt_base
= mtk_wdt
->wdt_base
;
232 writel(WDT_SWRST_KEY
, wdt_base
+ WDT_SWRST
);
239 static int mtk_wdt_ping(struct watchdog_device
*wdt_dev
)
241 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
242 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
244 iowrite32(WDT_RST_RELOAD
, wdt_base
+ WDT_RST
);
249 static int mtk_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
250 unsigned int timeout
)
252 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
253 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
256 wdt_dev
->timeout
= timeout
;
258 * In dual mode, irq will be triggered at timeout / 2
259 * the real timeout occurs at timeout
261 if (wdt_dev
->pretimeout
)
262 wdt_dev
->pretimeout
= timeout
/ 2;
265 * One bit is the value of 512 ticks
266 * The clock has 32 KHz
268 reg
= WDT_LENGTH_TIMEOUT((timeout
- wdt_dev
->pretimeout
) << 6)
270 iowrite32(reg
, wdt_base
+ WDT_LENGTH
);
272 mtk_wdt_ping(wdt_dev
);
277 static void mtk_wdt_init(struct watchdog_device
*wdt_dev
)
279 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
280 void __iomem
*wdt_base
;
282 wdt_base
= mtk_wdt
->wdt_base
;
284 if (readl(wdt_base
+ WDT_MODE
) & WDT_MODE_EN
) {
285 set_bit(WDOG_HW_RUNNING
, &wdt_dev
->status
);
286 mtk_wdt_set_timeout(wdt_dev
, wdt_dev
->timeout
);
290 static int mtk_wdt_stop(struct watchdog_device
*wdt_dev
)
292 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
293 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
296 reg
= readl(wdt_base
+ WDT_MODE
);
299 iowrite32(reg
, wdt_base
+ WDT_MODE
);
304 static int mtk_wdt_start(struct watchdog_device
*wdt_dev
)
307 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
308 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
311 ret
= mtk_wdt_set_timeout(wdt_dev
, wdt_dev
->timeout
);
315 reg
= ioread32(wdt_base
+ WDT_MODE
);
316 if (wdt_dev
->pretimeout
)
317 reg
|= (WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
319 reg
&= ~(WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
320 if (mtk_wdt
->disable_wdt_extrst
)
321 reg
&= ~WDT_MODE_EXRST_EN
;
322 if (mtk_wdt
->reset_by_toprgu
)
323 reg
|= WDT_MODE_CNT_SEL
;
324 reg
|= (WDT_MODE_EN
| WDT_MODE_KEY
);
325 iowrite32(reg
, wdt_base
+ WDT_MODE
);
330 static int mtk_wdt_set_pretimeout(struct watchdog_device
*wdd
,
331 unsigned int timeout
)
333 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdd
);
334 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
335 u32 reg
= ioread32(wdt_base
+ WDT_MODE
);
337 if (timeout
&& !wdd
->pretimeout
) {
338 wdd
->pretimeout
= wdd
->timeout
/ 2;
339 reg
|= (WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
340 } else if (!timeout
&& wdd
->pretimeout
) {
342 reg
&= ~(WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
348 iowrite32(reg
, wdt_base
+ WDT_MODE
);
350 return mtk_wdt_set_timeout(wdd
, wdd
->timeout
);
353 static irqreturn_t
mtk_wdt_isr(int irq
, void *arg
)
355 struct watchdog_device
*wdd
= arg
;
357 watchdog_notify_pretimeout(wdd
);
362 static const struct watchdog_info mtk_wdt_info
= {
363 .identity
= DRV_NAME
,
364 .options
= WDIOF_SETTIMEOUT
|
365 WDIOF_KEEPALIVEPING
|
369 static const struct watchdog_info mtk_wdt_pt_info
= {
370 .identity
= DRV_NAME
,
371 .options
= WDIOF_SETTIMEOUT
|
373 WDIOF_KEEPALIVEPING
|
377 static const struct watchdog_ops mtk_wdt_ops
= {
378 .owner
= THIS_MODULE
,
379 .start
= mtk_wdt_start
,
380 .stop
= mtk_wdt_stop
,
381 .ping
= mtk_wdt_ping
,
382 .set_timeout
= mtk_wdt_set_timeout
,
383 .set_pretimeout
= mtk_wdt_set_pretimeout
,
384 .restart
= mtk_wdt_restart
,
387 static int mtk_wdt_probe(struct platform_device
*pdev
)
389 struct device
*dev
= &pdev
->dev
;
390 struct mtk_wdt_dev
*mtk_wdt
;
391 const struct mtk_wdt_data
*wdt_data
;
394 mtk_wdt
= devm_kzalloc(dev
, sizeof(*mtk_wdt
), GFP_KERNEL
);
398 platform_set_drvdata(pdev
, mtk_wdt
);
400 mtk_wdt
->wdt_base
= devm_platform_ioremap_resource(pdev
, 0);
401 if (IS_ERR(mtk_wdt
->wdt_base
))
402 return PTR_ERR(mtk_wdt
->wdt_base
);
404 irq
= platform_get_irq_optional(pdev
, 0);
406 err
= devm_request_irq(&pdev
->dev
, irq
, mtk_wdt_isr
, 0, "wdt_bark",
411 mtk_wdt
->wdt_dev
.info
= &mtk_wdt_pt_info
;
412 mtk_wdt
->wdt_dev
.pretimeout
= WDT_MAX_TIMEOUT
/ 2;
414 if (irq
== -EPROBE_DEFER
)
415 return -EPROBE_DEFER
;
417 mtk_wdt
->wdt_dev
.info
= &mtk_wdt_info
;
420 mtk_wdt
->wdt_dev
.ops
= &mtk_wdt_ops
;
421 mtk_wdt
->wdt_dev
.timeout
= WDT_MAX_TIMEOUT
;
422 mtk_wdt
->wdt_dev
.max_hw_heartbeat_ms
= WDT_MAX_TIMEOUT
* 1000;
423 mtk_wdt
->wdt_dev
.min_timeout
= WDT_MIN_TIMEOUT
;
424 mtk_wdt
->wdt_dev
.parent
= dev
;
426 watchdog_init_timeout(&mtk_wdt
->wdt_dev
, timeout
, dev
);
427 watchdog_set_nowayout(&mtk_wdt
->wdt_dev
, nowayout
);
428 watchdog_set_restart_priority(&mtk_wdt
->wdt_dev
, 128);
430 watchdog_set_drvdata(&mtk_wdt
->wdt_dev
, mtk_wdt
);
432 mtk_wdt_init(&mtk_wdt
->wdt_dev
);
434 watchdog_stop_on_reboot(&mtk_wdt
->wdt_dev
);
435 err
= devm_watchdog_register_device(dev
, &mtk_wdt
->wdt_dev
);
439 dev_info(dev
, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
440 mtk_wdt
->wdt_dev
.timeout
, nowayout
);
442 wdt_data
= of_device_get_match_data(dev
);
444 err
= toprgu_register_reset_controller(pdev
,
445 wdt_data
->toprgu_sw_rst_num
);
449 mtk_wdt
->has_swsysrst_en
= wdt_data
->has_swsysrst_en
;
452 mtk_wdt
->disable_wdt_extrst
=
453 of_property_read_bool(dev
->of_node
, "mediatek,disable-extrst");
455 mtk_wdt
->reset_by_toprgu
=
456 of_property_read_bool(dev
->of_node
, "mediatek,reset-by-toprgu");
461 static int mtk_wdt_suspend(struct device
*dev
)
463 struct mtk_wdt_dev
*mtk_wdt
= dev_get_drvdata(dev
);
465 if (watchdog_active(&mtk_wdt
->wdt_dev
))
466 mtk_wdt_stop(&mtk_wdt
->wdt_dev
);
471 static int mtk_wdt_resume(struct device
*dev
)
473 struct mtk_wdt_dev
*mtk_wdt
= dev_get_drvdata(dev
);
475 if (watchdog_active(&mtk_wdt
->wdt_dev
)) {
476 mtk_wdt_start(&mtk_wdt
->wdt_dev
);
477 mtk_wdt_ping(&mtk_wdt
->wdt_dev
);
483 static const struct of_device_id mtk_wdt_dt_ids
[] = {
484 { .compatible
= "mediatek,mt2712-wdt", .data
= &mt2712_data
},
485 { .compatible
= "mediatek,mt6589-wdt" },
486 { .compatible
= "mediatek,mt6795-wdt", .data
= &mt6795_data
},
487 { .compatible
= "mediatek,mt7986-wdt", .data
= &mt7986_data
},
488 { .compatible
= "mediatek,mt7988-wdt", .data
= &mt7988_data
},
489 { .compatible
= "mediatek,mt8183-wdt", .data
= &mt8183_data
},
490 { .compatible
= "mediatek,mt8186-wdt", .data
= &mt8186_data
},
491 { .compatible
= "mediatek,mt8188-wdt", .data
= &mt8188_data
},
492 { .compatible
= "mediatek,mt8192-wdt", .data
= &mt8192_data
},
493 { .compatible
= "mediatek,mt8195-wdt", .data
= &mt8195_data
},
496 MODULE_DEVICE_TABLE(of
, mtk_wdt_dt_ids
);
498 static DEFINE_SIMPLE_DEV_PM_OPS(mtk_wdt_pm_ops
,
499 mtk_wdt_suspend
, mtk_wdt_resume
);
501 static struct platform_driver mtk_wdt_driver
= {
502 .probe
= mtk_wdt_probe
,
505 .pm
= pm_sleep_ptr(&mtk_wdt_pm_ops
),
506 .of_match_table
= mtk_wdt_dt_ids
,
510 module_platform_driver(mtk_wdt_driver
);
512 module_param(timeout
, uint
, 0);
513 MODULE_PARM_DESC(timeout
, "Watchdog heartbeat in seconds");
515 module_param(nowayout
, bool, 0);
516 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
517 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
519 MODULE_LICENSE("GPL");
520 MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
521 MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
522 MODULE_VERSION(DRV_VERSION
);