2 * Mediatek Watchdog Driver
4 * Copyright (C) 2014 Matthias Brugger
6 * Matthias Brugger <matthias.bgg@gmail.com>
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.
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.
18 * Based on sunxi_wdt.c
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/types.h>
30 #include <linux/watchdog.h>
31 #include <linux/notifier.h>
32 #include <linux/reboot.h>
33 #include <linux/delay.h>
35 #define WDT_MAX_TIMEOUT 31
36 #define WDT_MIN_TIMEOUT 1
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_KEY 0x22000000
55 #define WDT_SWRST 0x14
56 #define WDT_SWRST_KEY 0x1209
58 #define DRV_NAME "mtk-wdt"
59 #define DRV_VERSION "1.0"
61 static bool nowayout
= WATCHDOG_NOWAYOUT
;
62 static unsigned int timeout
= WDT_MAX_TIMEOUT
;
65 struct watchdog_device wdt_dev
;
66 void __iomem
*wdt_base
;
67 struct notifier_block restart_handler
;
70 static int mtk_reset_handler(struct notifier_block
*this, unsigned long mode
,
73 struct mtk_wdt_dev
*mtk_wdt
;
74 void __iomem
*wdt_base
;
76 mtk_wdt
= container_of(this, struct mtk_wdt_dev
, restart_handler
);
77 wdt_base
= mtk_wdt
->wdt_base
;
80 writel(WDT_SWRST_KEY
, wdt_base
+ WDT_SWRST
);
87 static int mtk_wdt_ping(struct watchdog_device
*wdt_dev
)
89 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
90 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
92 iowrite32(WDT_RST_RELOAD
, wdt_base
+ WDT_RST
);
97 static int mtk_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
100 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
101 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
104 wdt_dev
->timeout
= timeout
;
107 * One bit is the value of 512 ticks
108 * The clock has 32 KHz
110 reg
= WDT_LENGTH_TIMEOUT(timeout
<< 6) | WDT_LENGTH_KEY
;
111 iowrite32(reg
, wdt_base
+ WDT_LENGTH
);
113 mtk_wdt_ping(wdt_dev
);
118 static int mtk_wdt_stop(struct watchdog_device
*wdt_dev
)
120 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
121 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
124 reg
= readl(wdt_base
+ WDT_MODE
);
127 iowrite32(reg
, wdt_base
+ WDT_MODE
);
132 static int mtk_wdt_start(struct watchdog_device
*wdt_dev
)
135 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
136 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
139 ret
= mtk_wdt_set_timeout(wdt_dev
, wdt_dev
->timeout
);
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
);
151 static const struct watchdog_info mtk_wdt_info
= {
152 .identity
= DRV_NAME
,
153 .options
= WDIOF_SETTIMEOUT
|
154 WDIOF_KEEPALIVEPING
|
158 static 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
,
166 static int mtk_wdt_probe(struct platform_device
*pdev
)
168 struct mtk_wdt_dev
*mtk_wdt
;
169 struct resource
*res
;
172 mtk_wdt
= devm_kzalloc(&pdev
->dev
, sizeof(*mtk_wdt
), GFP_KERNEL
);
176 platform_set_drvdata(pdev
, mtk_wdt
);
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
);
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
;
190 watchdog_init_timeout(&mtk_wdt
->wdt_dev
, timeout
, &pdev
->dev
);
191 watchdog_set_nowayout(&mtk_wdt
->wdt_dev
, nowayout
);
193 watchdog_set_drvdata(&mtk_wdt
->wdt_dev
, mtk_wdt
);
195 mtk_wdt_stop(&mtk_wdt
->wdt_dev
);
197 err
= watchdog_register_device(&mtk_wdt
->wdt_dev
);
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
);
206 "cannot register restart handler (err=%d)\n", err
);
208 dev_info(&pdev
->dev
, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
209 mtk_wdt
->wdt_dev
.timeout
, nowayout
);
214 static void mtk_wdt_shutdown(struct platform_device
*pdev
)
216 struct mtk_wdt_dev
*mtk_wdt
= platform_get_drvdata(pdev
);
218 if (watchdog_active(&mtk_wdt
->wdt_dev
))
219 mtk_wdt_stop(&mtk_wdt
->wdt_dev
);
222 static int mtk_wdt_remove(struct platform_device
*pdev
)
224 struct mtk_wdt_dev
*mtk_wdt
= platform_get_drvdata(pdev
);
226 unregister_restart_handler(&mtk_wdt
->restart_handler
);
228 watchdog_unregister_device(&mtk_wdt
->wdt_dev
);
233 #ifdef CONFIG_PM_SLEEP
234 static int mtk_wdt_suspend(struct device
*dev
)
236 struct mtk_wdt_dev
*mtk_wdt
= dev_get_drvdata(dev
);
238 if (watchdog_active(&mtk_wdt
->wdt_dev
))
239 mtk_wdt_stop(&mtk_wdt
->wdt_dev
);
244 static int mtk_wdt_resume(struct device
*dev
)
246 struct mtk_wdt_dev
*mtk_wdt
= dev_get_drvdata(dev
);
248 if (watchdog_active(&mtk_wdt
->wdt_dev
)) {
249 mtk_wdt_start(&mtk_wdt
->wdt_dev
);
250 mtk_wdt_ping(&mtk_wdt
->wdt_dev
);
257 static const struct of_device_id mtk_wdt_dt_ids
[] = {
258 { .compatible
= "mediatek,mt6589-wdt" },
261 MODULE_DEVICE_TABLE(of
, mtk_wdt_dt_ids
);
263 static const struct dev_pm_ops mtk_wdt_pm_ops
= {
264 SET_SYSTEM_SLEEP_PM_OPS(mtk_wdt_suspend
,
268 static struct platform_driver mtk_wdt_driver
= {
269 .probe
= mtk_wdt_probe
,
270 .remove
= mtk_wdt_remove
,
271 .shutdown
= mtk_wdt_shutdown
,
274 .pm
= &mtk_wdt_pm_ops
,
275 .of_match_table
= mtk_wdt_dt_ids
,
279 module_platform_driver(mtk_wdt_driver
);
281 module_param(timeout
, uint
, 0);
282 MODULE_PARM_DESC(timeout
, "Watchdog heartbeat in seconds");
284 module_param(nowayout
, bool, 0);
285 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
286 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
288 MODULE_LICENSE("GPL");
289 MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
290 MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
291 MODULE_VERSION(DRV_VERSION
);