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
);
126 iowrite32(reg
, wdt_base
+ WDT_MODE
);
131 static int mtk_wdt_start(struct watchdog_device
*wdt_dev
)
134 struct mtk_wdt_dev
*mtk_wdt
= watchdog_get_drvdata(wdt_dev
);
135 void __iomem
*wdt_base
= mtk_wdt
->wdt_base
;
138 ret
= mtk_wdt_set_timeout(wdt_dev
, wdt_dev
->timeout
);
142 reg
= ioread32(wdt_base
+ WDT_MODE
);
143 reg
&= ~(WDT_MODE_IRQ_EN
| WDT_MODE_DUAL_EN
);
144 reg
|= (WDT_MODE_EN
| WDT_MODE_KEY
);
145 iowrite32(reg
, wdt_base
+ WDT_MODE
);
150 static const struct watchdog_info mtk_wdt_info
= {
151 .identity
= DRV_NAME
,
152 .options
= WDIOF_SETTIMEOUT
|
153 WDIOF_KEEPALIVEPING
|
157 static const struct watchdog_ops mtk_wdt_ops
= {
158 .owner
= THIS_MODULE
,
159 .start
= mtk_wdt_start
,
160 .stop
= mtk_wdt_stop
,
161 .ping
= mtk_wdt_ping
,
162 .set_timeout
= mtk_wdt_set_timeout
,
165 static int mtk_wdt_probe(struct platform_device
*pdev
)
167 struct mtk_wdt_dev
*mtk_wdt
;
168 struct resource
*res
;
171 mtk_wdt
= devm_kzalloc(&pdev
->dev
, sizeof(*mtk_wdt
), GFP_KERNEL
);
175 platform_set_drvdata(pdev
, mtk_wdt
);
177 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
178 mtk_wdt
->wdt_base
= devm_ioremap_resource(&pdev
->dev
, res
);
179 if (IS_ERR(mtk_wdt
->wdt_base
))
180 return PTR_ERR(mtk_wdt
->wdt_base
);
182 mtk_wdt
->wdt_dev
.info
= &mtk_wdt_info
;
183 mtk_wdt
->wdt_dev
.ops
= &mtk_wdt_ops
;
184 mtk_wdt
->wdt_dev
.timeout
= WDT_MAX_TIMEOUT
;
185 mtk_wdt
->wdt_dev
.max_timeout
= WDT_MAX_TIMEOUT
;
186 mtk_wdt
->wdt_dev
.min_timeout
= WDT_MIN_TIMEOUT
;
187 mtk_wdt
->wdt_dev
.parent
= &pdev
->dev
;
189 watchdog_init_timeout(&mtk_wdt
->wdt_dev
, timeout
, &pdev
->dev
);
190 watchdog_set_nowayout(&mtk_wdt
->wdt_dev
, nowayout
);
192 watchdog_set_drvdata(&mtk_wdt
->wdt_dev
, mtk_wdt
);
194 mtk_wdt_stop(&mtk_wdt
->wdt_dev
);
196 err
= watchdog_register_device(&mtk_wdt
->wdt_dev
);
200 mtk_wdt
->restart_handler
.notifier_call
= mtk_reset_handler
;
201 mtk_wdt
->restart_handler
.priority
= 128;
202 err
= register_restart_handler(&mtk_wdt
->restart_handler
);
205 "cannot register restart handler (err=%d)\n", err
);
207 dev_info(&pdev
->dev
, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
208 mtk_wdt
->wdt_dev
.timeout
, nowayout
);
213 static int mtk_wdt_remove(struct platform_device
*pdev
)
215 struct mtk_wdt_dev
*mtk_wdt
= platform_get_drvdata(pdev
);
217 unregister_restart_handler(&mtk_wdt
->restart_handler
);
219 watchdog_unregister_device(&mtk_wdt
->wdt_dev
);
224 static const struct of_device_id mtk_wdt_dt_ids
[] = {
225 { .compatible
= "mediatek,mt6589-wdt" },
228 MODULE_DEVICE_TABLE(of
, mtk_wdt_dt_ids
);
230 static struct platform_driver mtk_wdt_driver
= {
231 .probe
= mtk_wdt_probe
,
232 .remove
= mtk_wdt_remove
,
235 .of_match_table
= mtk_wdt_dt_ids
,
239 module_platform_driver(mtk_wdt_driver
);
241 module_param(timeout
, uint
, 0);
242 MODULE_PARM_DESC(timeout
, "Watchdog heartbeat in seconds");
244 module_param(nowayout
, bool, 0);
245 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
246 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
248 MODULE_LICENSE("GPL");
249 MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
250 MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
251 MODULE_VERSION(DRV_VERSION
);