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,mt6735-wdt.h>
14 #include <dt-bindings/reset/mediatek,mt6795-resets.h>
15 #include <dt-bindings/reset/mt7986-resets.h>
16 #include <dt-bindings/reset/mt8183-resets.h>
17 #include <dt-bindings/reset/mt8186-resets.h>
18 #include <dt-bindings/reset/mt8188-resets.h>
19 #include <dt-bindings/reset/mt8192-resets.h>
20 #include <dt-bindings/reset/mt8195-resets.h>
21 #include <linux/delay.h>
22 #include <linux/err.h>
23 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
29 #include <linux/platform_device.h>
30 #include <linux/reset-controller.h>
31 #include <linux/types.h>
32 #include <linux/watchdog.h>
33 #include <linux/interrupt.h>
35 #define WDT_MAX_TIMEOUT 31
36 #define WDT_MIN_TIMEOUT 2
37 #define WDT_LENGTH_TIMEOUT(n) ((n) << 5)
39 #define WDT_LENGTH 0x04
40 #define WDT_LENGTH_KEY 0x8
43 #define WDT_RST_RELOAD 0x1971
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_CNT_SEL (1 << 8)
54 #define WDT_MODE_KEY 0x22000000
56 #define WDT_SWRST 0x14
57 #define WDT_SWRST_KEY 0x1209
59 #define WDT_SWSYSRST 0x18U
60 #define WDT_SWSYS_RST_KEY 0x88000000
62 #define WDT_SWSYSRST_EN 0xfc
64 #define DRV_NAME "mtk-wdt"
65 #define DRV_VERSION "1.0"
67 #define MT7988_TOPRGU_SW_RST_NUM 24
69 static bool nowayout
= WATCHDOG_NOWAYOUT
;
70 static unsigned int timeout
;
73 struct watchdog_device wdt_dev
;
74 void __iomem
*wdt_base
;
75 spinlock_t lock
; /* protects WDT_SWSYSRST reg */
76 struct reset_controller_dev rcdev
;
77 bool disable_wdt_extrst
;
83 int toprgu_sw_rst_num
;
87 static const struct mtk_wdt_data mt2712_data
= {
88 .toprgu_sw_rst_num
= MT2712_TOPRGU_SW_RST_NUM
,
91 static const struct mtk_wdt_data mt6735_data
= {
92 .toprgu_sw_rst_num
= MT6735_TOPRGU_RST_NUM
,
95 static const struct mtk_wdt_data mt6795_data
= {
96 .toprgu_sw_rst_num
= MT6795_TOPRGU_SW_RST_NUM
,
99 static const struct mtk_wdt_data mt7986_data
= {
100 .toprgu_sw_rst_num
= MT7986_TOPRGU_SW_RST_NUM
,
103 static const struct mtk_wdt_data mt7988_data
= {
104 .toprgu_sw_rst_num
= MT7988_TOPRGU_SW_RST_NUM
,
105 .has_swsysrst_en
= true,
108 static const struct mtk_wdt_data mt8183_data
= {
109 .toprgu_sw_rst_num
= MT8183_TOPRGU_SW_RST_NUM
,
112 static const struct mtk_wdt_data mt8186_data
= {
113 .toprgu_sw_rst_num
= MT8186_TOPRGU_SW_RST_NUM
,
116 static const struct mtk_wdt_data mt8188_data
= {
117 .toprgu_sw_rst_num
= MT8188_TOPRGU_SW_RST_NUM
,
120 static const struct mtk_wdt_data mt8192_data
= {
121 .toprgu_sw_rst_num
= MT8192_TOPRGU_SW_RST_NUM
,
124 static const struct mtk_wdt_data mt8195_data
= {
125 .toprgu_sw_rst_num
= MT8195_TOPRGU_SW_RST_NUM
,
129 * toprgu_reset_sw_en_unlocked() - enable/disable software control for reset bit
130 * @data: Pointer to instance of driver data.
131 * @id: Bit number identifying the reset to be enabled or disabled.
132 * @enable: If true, enable software control for that bit, disable otherwise.
134 * Context: The caller must hold lock of struct mtk_wdt_dev.
136 static void toprgu_reset_sw_en_unlocked(struct mtk_wdt_dev
*data
,
137 unsigned long id
, bool enable
)
141 tmp
= readl(data
->wdt_base
+ WDT_SWSYSRST_EN
);
147 writel(tmp
, data
->wdt_base
+ WDT_SWSYSRST_EN
);
150 static int toprgu_reset_update(struct reset_controller_dev
*rcdev
,
151 unsigned long id
, bool assert)
155 struct mtk_wdt_dev
*data
=
156 container_of(rcdev
, struct mtk_wdt_dev
, rcdev
);
158 spin_lock_irqsave(&data
->lock
, flags
);
160 if (assert && data
->has_swsysrst_en
)
161 toprgu_reset_sw_en_unlocked(data
, id
, true);
163 tmp
= readl(data
->wdt_base
+ WDT_SWSYSRST
);
168 tmp
|= WDT_SWSYS_RST_KEY
;
169 writel(tmp
, data
->wdt_base
+ WDT_SWSYSRST
);
171 if (!assert && data
->has_swsysrst_en
)
172 toprgu_reset_sw_en_unlocked(data
, id
, false);
174 spin_unlock_irqrestore(&data
->lock
, flags
);
179 static int toprgu_reset_assert(struct reset_controller_dev
*rcdev
,
182 return toprgu_reset_update(rcdev
, id
, true);
185 static int toprgu_reset_deassert(struct reset_controller_dev
*rcdev
,
188 return toprgu_reset_update(rcdev
, id
, false);
191 static int toprgu_reset(struct reset_controller_dev
*rcdev
,
196 ret
= toprgu_reset_assert(rcdev
, id
);
200 return toprgu_reset_deassert(rcdev
, id
);
203 static const struct reset_control_ops toprgu_reset_ops
= {
204 .assert = toprgu_reset_assert
,
205 .deassert
= toprgu_reset_deassert
,
206 .reset
= toprgu_reset
,
209 static int toprgu_register_reset_controller(struct platform_device
*pdev
,
213 struct mtk_wdt_dev
*mtk_wdt
= platform_get_drvdata(pdev
);
215 spin_lock_init(&mtk_wdt
->lock
);
217 mtk_wdt
->rcdev
.owner
= THIS_MODULE
;
218 mtk_wdt
->rcdev
.nr_resets
= rst_num
;
219 mtk_wdt
->rcdev
.ops
= &toprgu_reset_ops
;
220 mtk_wdt
->rcdev
.of_node
= pdev
->dev
.of_node
;
221 ret
= devm_reset_controller_register(&pdev
->dev
, &mtk_wdt
->rcdev
);
224 "couldn't register wdt reset controller: %d\n", ret
);
228 static int mtk_wdt_restart(struct watchdog_device
*wdt_dev
,
229 unsigned long action
, void *data
)
231 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
232 void __iomem
*wdt_base
;
235 wdt_base
= mtk_wdt
->wdt_base
;
237 /* Enable reset in order to issue a system reset instead of an IRQ */
238 reg
= readl(wdt_base
+ WDT_MODE
);
239 reg
&= ~WDT_MODE_IRQ_EN
;
240 writel(reg
| WDT_MODE_KEY
, wdt_base
+ WDT_MODE
);
243 writel(WDT_SWRST_KEY
, wdt_base
+ WDT_SWRST
);
250 static int mtk_wdt_ping(struct watchdog_device
*wdt_dev
)
252 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
253 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
255 iowrite32(WDT_RST_RELOAD
, wdt_base
+ WDT_RST
);
260 static int mtk_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
261 unsigned int timeout
)
263 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
264 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
267 wdt_dev
->timeout
= timeout
;
269 * In dual mode, irq will be triggered at timeout / 2
270 * the real timeout occurs at timeout
272 if (wdt_dev
->pretimeout
)
273 wdt_dev
->pretimeout
= timeout
/ 2;
276 * One bit is the value of 512 ticks
277 * The clock has 32 KHz
279 reg
= WDT_LENGTH_TIMEOUT((timeout
- wdt_dev
->pretimeout
) << 6)
281 iowrite32(reg
, wdt_base
+ WDT_LENGTH
);
283 mtk_wdt_ping(wdt_dev
);
288 static void mtk_wdt_init(struct watchdog_device
*wdt_dev
)
290 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
291 void __iomem
*wdt_base
;
293 wdt_base
= mtk_wdt
->wdt_base
;
295 if (readl(wdt_base
+ WDT_MODE
) & WDT_MODE_EN
) {
296 set_bit(WDOG_HW_RUNNING
, &wdt_dev
->status
);
297 mtk_wdt_set_timeout(wdt_dev
, wdt_dev
->timeout
);
301 static int mtk_wdt_stop(struct watchdog_device
*wdt_dev
)
303 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
304 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
307 reg
= readl(wdt_base
+ WDT_MODE
);
310 iowrite32(reg
, wdt_base
+ WDT_MODE
);
315 static int mtk_wdt_start(struct watchdog_device
*wdt_dev
)
318 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
319 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
322 ret
= mtk_wdt_set_timeout(wdt_dev
, wdt_dev
->timeout
);
326 reg
= ioread32(wdt_base
+ WDT_MODE
);
327 if (wdt_dev
->pretimeout
)
328 reg
|= (WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
330 reg
&= ~(WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
331 if (mtk_wdt
->disable_wdt_extrst
)
332 reg
&= ~WDT_MODE_EXRST_EN
;
333 if (mtk_wdt
->reset_by_toprgu
)
334 reg
|= WDT_MODE_CNT_SEL
;
335 reg
|= (WDT_MODE_EN
| WDT_MODE_KEY
);
336 iowrite32(reg
, wdt_base
+ WDT_MODE
);
341 static int mtk_wdt_set_pretimeout(struct watchdog_device
*wdd
,
342 unsigned int timeout
)
344 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdd
);
345 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
346 u32 reg
= ioread32(wdt_base
+ WDT_MODE
);
348 if (timeout
&& !wdd
->pretimeout
) {
349 wdd
->pretimeout
= wdd
->timeout
/ 2;
350 reg
|= (WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
351 } else if (!timeout
&& wdd
->pretimeout
) {
353 reg
&= ~(WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
359 iowrite32(reg
, wdt_base
+ WDT_MODE
);
361 return mtk_wdt_set_timeout(wdd
, wdd
->timeout
);
364 static irqreturn_t
mtk_wdt_isr(int irq
, void *arg
)
366 struct watchdog_device
*wdd
= arg
;
368 watchdog_notify_pretimeout(wdd
);
373 static const struct watchdog_info mtk_wdt_info
= {
374 .identity
= DRV_NAME
,
375 .options
= WDIOF_SETTIMEOUT
|
376 WDIOF_KEEPALIVEPING
|
380 static const struct watchdog_info mtk_wdt_pt_info
= {
381 .identity
= DRV_NAME
,
382 .options
= WDIOF_SETTIMEOUT
|
384 WDIOF_KEEPALIVEPING
|
388 static const struct watchdog_ops mtk_wdt_ops
= {
389 .owner
= THIS_MODULE
,
390 .start
= mtk_wdt_start
,
391 .stop
= mtk_wdt_stop
,
392 .ping
= mtk_wdt_ping
,
393 .set_timeout
= mtk_wdt_set_timeout
,
394 .set_pretimeout
= mtk_wdt_set_pretimeout
,
395 .restart
= mtk_wdt_restart
,
398 static int mtk_wdt_probe(struct platform_device
*pdev
)
400 struct device
*dev
= &pdev
->dev
;
401 struct mtk_wdt_dev
*mtk_wdt
;
402 const struct mtk_wdt_data
*wdt_data
;
405 mtk_wdt
= devm_kzalloc(dev
, sizeof(*mtk_wdt
), GFP_KERNEL
);
409 platform_set_drvdata(pdev
, mtk_wdt
);
411 mtk_wdt
->wdt_base
= devm_platform_ioremap_resource(pdev
, 0);
412 if (IS_ERR(mtk_wdt
->wdt_base
))
413 return PTR_ERR(mtk_wdt
->wdt_base
);
415 irq
= platform_get_irq_optional(pdev
, 0);
417 err
= devm_request_irq(&pdev
->dev
, irq
, mtk_wdt_isr
, 0, "wdt_bark",
422 mtk_wdt
->wdt_dev
.info
= &mtk_wdt_pt_info
;
423 mtk_wdt
->wdt_dev
.pretimeout
= WDT_MAX_TIMEOUT
/ 2;
425 if (irq
== -EPROBE_DEFER
)
426 return -EPROBE_DEFER
;
428 mtk_wdt
->wdt_dev
.info
= &mtk_wdt_info
;
431 mtk_wdt
->wdt_dev
.ops
= &mtk_wdt_ops
;
432 mtk_wdt
->wdt_dev
.timeout
= WDT_MAX_TIMEOUT
;
433 mtk_wdt
->wdt_dev
.max_hw_heartbeat_ms
= WDT_MAX_TIMEOUT
* 1000;
434 mtk_wdt
->wdt_dev
.min_timeout
= WDT_MIN_TIMEOUT
;
435 mtk_wdt
->wdt_dev
.parent
= dev
;
437 watchdog_init_timeout(&mtk_wdt
->wdt_dev
, timeout
, dev
);
438 watchdog_set_nowayout(&mtk_wdt
->wdt_dev
, nowayout
);
439 watchdog_set_restart_priority(&mtk_wdt
->wdt_dev
, 128);
441 watchdog_set_drvdata(&mtk_wdt
->wdt_dev
, mtk_wdt
);
443 mtk_wdt_init(&mtk_wdt
->wdt_dev
);
445 watchdog_stop_on_reboot(&mtk_wdt
->wdt_dev
);
446 err
= devm_watchdog_register_device(dev
, &mtk_wdt
->wdt_dev
);
450 dev_info(dev
, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
451 mtk_wdt
->wdt_dev
.timeout
, nowayout
);
453 wdt_data
= of_device_get_match_data(dev
);
455 err
= toprgu_register_reset_controller(pdev
,
456 wdt_data
->toprgu_sw_rst_num
);
460 mtk_wdt
->has_swsysrst_en
= wdt_data
->has_swsysrst_en
;
463 mtk_wdt
->disable_wdt_extrst
=
464 of_property_read_bool(dev
->of_node
, "mediatek,disable-extrst");
466 mtk_wdt
->reset_by_toprgu
=
467 of_property_read_bool(dev
->of_node
, "mediatek,reset-by-toprgu");
472 static int mtk_wdt_suspend(struct device
*dev
)
474 struct mtk_wdt_dev
*mtk_wdt
= dev_get_drvdata(dev
);
476 if (watchdog_active(&mtk_wdt
->wdt_dev
))
477 mtk_wdt_stop(&mtk_wdt
->wdt_dev
);
482 static int mtk_wdt_resume(struct device
*dev
)
484 struct mtk_wdt_dev
*mtk_wdt
= dev_get_drvdata(dev
);
486 if (watchdog_active(&mtk_wdt
->wdt_dev
)) {
487 mtk_wdt_start(&mtk_wdt
->wdt_dev
);
488 mtk_wdt_ping(&mtk_wdt
->wdt_dev
);
494 static const struct of_device_id mtk_wdt_dt_ids
[] = {
495 { .compatible
= "mediatek,mt2712-wdt", .data
= &mt2712_data
},
496 { .compatible
= "mediatek,mt6589-wdt" },
497 { .compatible
= "mediatek,mt6735-wdt", .data
= &mt6735_data
},
498 { .compatible
= "mediatek,mt6795-wdt", .data
= &mt6795_data
},
499 { .compatible
= "mediatek,mt7986-wdt", .data
= &mt7986_data
},
500 { .compatible
= "mediatek,mt7988-wdt", .data
= &mt7988_data
},
501 { .compatible
= "mediatek,mt8183-wdt", .data
= &mt8183_data
},
502 { .compatible
= "mediatek,mt8186-wdt", .data
= &mt8186_data
},
503 { .compatible
= "mediatek,mt8188-wdt", .data
= &mt8188_data
},
504 { .compatible
= "mediatek,mt8192-wdt", .data
= &mt8192_data
},
505 { .compatible
= "mediatek,mt8195-wdt", .data
= &mt8195_data
},
508 MODULE_DEVICE_TABLE(of
, mtk_wdt_dt_ids
);
510 static DEFINE_SIMPLE_DEV_PM_OPS(mtk_wdt_pm_ops
,
511 mtk_wdt_suspend
, mtk_wdt_resume
);
513 static struct platform_driver mtk_wdt_driver
= {
514 .probe
= mtk_wdt_probe
,
517 .pm
= pm_sleep_ptr(&mtk_wdt_pm_ops
),
518 .of_match_table
= mtk_wdt_dt_ids
,
522 module_platform_driver(mtk_wdt_driver
);
524 module_param(timeout
, uint
, 0);
525 MODULE_PARM_DESC(timeout
, "Watchdog heartbeat in seconds");
527 module_param(nowayout
, bool, 0);
528 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
529 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
531 MODULE_LICENSE("GPL");
532 MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
533 MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
534 MODULE_VERSION(DRV_VERSION
);