blob: 5eabdf42d1870aa7fdc4e33db69391fadf32c8c0 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Mediatek Watchdog Driver
3 *
4 * Copyright (C) 2014 Matthias Brugger
5 *
6 * Matthias Brugger <matthias.bgg@gmail.com>
7 *
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.
12 *
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.
17 *
18 * Based on sunxi_wdt.c
19 */
20
21#include <linux/err.h>
22#include <linux/init.h>
23#include <linux/io.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/of.h>
28#include <linux/platform_device.h>
29#include <linux/types.h>
30#include <linux/watchdog.h>
31#include <linux/delay.h>
32#include <asm/system_misc.h>
33#include <linux/of_address.h>
34#include <drm.h>
35
36#define WDT_MAX_TIMEOUT 31
37#define WDT_MIN_TIMEOUT 1
38#define WDT_LENGTH_TIMEOUT(n) ((n) << 5)
39
40#define WDT_LENGTH 0x04
41#define WDT_LENGTH_KEY 0x8
42
43#define WDT_RST 0x08
44#define WDT_RST_RELOAD 0x1971
45
46#define WDT_MODE 0x00
47#define WDT_MODE_EN (1 << 0)
48#define WDT_MODE_EXT_POL_LOW (0 << 1)
49#define WDT_MODE_EXT_POL_HIGH (1 << 1)
50#define WDT_MODE_EXRST_EN (1 << 2)
51#define WDT_MODE_IRQ_EN (1 << 3)
52#define WDT_MODE_AUTO_START (1 << 4)
53#define WDT_MODE_DUAL_EN (1 << 6)
54#define WDT_MODE_KEY 0x22000000
55
56#define WDT_SWRST 0x14
57#define WDT_SWRST_KEY 0x1209
58
59#define WDT_NONRST_REG2 0x24
60#define WDT_BOOTMODE_MSK 0x1F
61
62#define WDT_REQ_MODE 0x30
63#define WDT_REQ_MODE_DEBUG_EN (1 << 19)
64#define WDT_REQ_MODE_SPM_THERMAL (1 << 0)
65#define WDT_REQ_MODE_SPM_SCPSYS (1 << 1)
66#define WDT_REQ_MODE_EINT (1 << 2)
67#define WDT_REQ_MODE_SYSRST (1 << 3)
68#define WDT_REQ_MODE_THERMAL (1 << 18)
69#define WDT_REQ_MODE_KEY 0x33000000
70
71#define WDT_REQ_IRQ_EN 0x34
72#define WDT_REQ_IRQ_DEBUG_EN (1 << 19)
73#define WDT_REQ_IRQ_SPM_THERMAL_EN (1 << 0)
74#define WDT_REQ_IRQ_SPM_SCPSYS_EN (1 << 1)
75#define WDT_REQ_IRQ_EINT_EN (1 << 2)
76#define WDT_REQ_IRQ_SYSRST_EN (1 << 3)
77#define WDT_REQ_IRQ_THERMAL_EN (1 << 18)
78#define WDT_REQ_IRQ_KEY 0x44000000
79
80#define WDT_EXT_REQ_CON 0x38
81#define WDT_EXT_REQ_CON_EN (1 << 0)
82#define WDT_EXT_REQ_CON_SHIFT 4
83#define WDT_EXT_REQ_CON_MASK 0xF
84#define WDT_EXT_REQ_CON_SEL(x) (((x) & WDT_EXT_REQ_CON_MASK) << \
85 WDT_EXT_REQ_CON_SHIFT)
86
87#define WDT_SYSDBG_DEG_EN1 0x88
88#define WDT_SYSDBG_DEG_EN1_KEY (0x1B2A)
89
90#define WDT_SYSDBG_DEG_EN2 0x8c
91#define WDT_SYSDBG_DEG_EN2_KEY (0x4F59)
92
93#define DRV_NAME "mtk-wdt"
94#define DRV_VERSION "1.0"
95
96enum ext_wdt_mode {
97 WDT_IRQ_ONLY_MODE,
98 WDT_HW_REBOOT_ONLY_MODE,
99 WDT_DUAL_MODE,
100};
101
102enum wk_req_en {
103 WD_REQ_DIS,
104 WD_REQ_EN,
105};
106
107enum wk_req_mode {
108 WD_REQ_IRQ_MODE,
109 WD_REQ_RST_MODE,
110};
111
112static bool nowayout = WATCHDOG_NOWAYOUT;
113static unsigned int timeout = WDT_MAX_TIMEOUT;
114
115struct mtk_wdt_dev {
116 struct watchdog_device wdt_dev;
117 void __iomem *wdt_base;
118 u32 eint;
119};
120
121void __iomem *toprgu_base;
122
123static int mtk_wdt_restart(struct watchdog_device *wdt_dev,
124 unsigned long action, void *cmd)
125{
126 struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
127 void __iomem *wdt_base;
128 u32 reg;
129
130 wdt_base = mtk_wdt->wdt_base;
131
132 iowrite32(ioread32(wdt_base + WDT_NONRST_REG2) & ~WDT_BOOTMODE_MSK,
133 wdt_base + WDT_NONRST_REG2);
134
135 if (cmd && (strcmp(cmd, "rpmbpk") == 0))
136 iowrite32(ioread32(wdt_base + WDT_NONRST_REG2) |
137 (1U << 0), wdt_base + WDT_NONRST_REG2);
138 else if (cmd && (strcmp(cmd, "recovery") == 0))
139 iowrite32(ioread32(wdt_base + WDT_NONRST_REG2) |
140 (1U << 1), wdt_base + WDT_NONRST_REG2);
141 else if (cmd && (strcmp(cmd, "bootloader") == 0))
142 iowrite32(ioread32(wdt_base + WDT_NONRST_REG2) |
143 (1U << 2), wdt_base + WDT_NONRST_REG2);
144 else if (cmd && !strcmp(cmd, "meta")) {
145 pr_info("arch_reset, reboot meta mode\n");
146 iowrite32(ioread32(wdt_base + WDT_NONRST_REG2) |
147 (1U << 3), wdt_base + WDT_NONRST_REG2);
148 } else if (cmd && !strcmp(cmd, "ddr-reserve")) {
149 pr_info("arch_reset, reboot ddr-reserve mode\n");
150 iowrite32(ioread32(wdt_base + WDT_NONRST_REG2) |
151 (1U << 4), wdt_base + WDT_NONRST_REG2);
152 }
153
154 reg = ioread32(wdt_base + WDT_MODE);
155 reg |= WDT_MODE_KEY | WDT_MODE_EXRST_EN;
156 reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
157
158 iowrite32(reg, wdt_base + WDT_MODE);
159
160 if (!(ioread32(wdt_base + WDT_NONRST_REG2) & (1U << 4)))
161 mtk_drm_dram_reserved(0);
162
163 if (!arm_pm_restart) {
164 while (1) {
165 writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST);
166 mdelay(5);
167 }
168 }
169
170 return NOTIFY_DONE;
171}
172
173static void mtk_wdt_request_en_set(struct mtk_wdt_dev *mtk_wdt,
174 int mark_bit, enum wk_req_en en)
175{
176 void __iomem *wdt_base = mtk_wdt->wdt_base;
177 u32 reg;
178
179 reg = readl(wdt_base + WDT_REQ_MODE);
180 reg |= WDT_REQ_MODE_KEY;
181
182 if (mark_bit == WDT_REQ_MODE_SPM_SCPSYS) {
183 if (en == WD_REQ_EN)
184 reg |= (WDT_REQ_MODE_SPM_SCPSYS);
185 if (en == WD_REQ_DIS)
186 reg &= ~(WDT_REQ_MODE_SPM_SCPSYS);
187 } else if (mark_bit == WDT_REQ_MODE_EINT) {
188 if (en == WD_REQ_EN) {
189 writel(WDT_EXT_REQ_CON_SEL(mtk_wdt->eint) |
190 WDT_EXT_REQ_CON_EN, wdt_base + WDT_EXT_REQ_CON);
191 reg |= (WDT_REQ_MODE_EINT);
192 }
193 if (en == WD_REQ_DIS)
194 reg &= ~(WDT_REQ_MODE_EINT);
195 } else if (mark_bit == WDT_REQ_MODE_SYSRST) {
196 if (en == WD_REQ_EN) {
197 writel(WDT_SYSDBG_DEG_EN1_KEY,
198 WDT_SYSDBG_DEG_EN1);
199 writel(WDT_SYSDBG_DEG_EN2_KEY,
200 WDT_SYSDBG_DEG_EN2);
201 reg |= (WDT_REQ_MODE_SYSRST);
202 }
203 if (en == WD_REQ_DIS) {
204 writel(0, WDT_SYSDBG_DEG_EN1);
205 writel(0, WDT_SYSDBG_DEG_EN2);
206 reg &= ~(WDT_REQ_MODE_SYSRST);
207 }
208 } else if (mark_bit == WDT_REQ_MODE_THERMAL) {
209 if (en == WD_REQ_EN)
210 reg |= (WDT_REQ_MODE_THERMAL);
211 if (en == WD_REQ_DIS)
212 reg &= ~(WDT_REQ_MODE_THERMAL);
213 }
214
215 writel(reg, wdt_base + WDT_REQ_MODE);
216}
217
218static void mtk_wdt_request_mode_set(struct mtk_wdt_dev *mtk_wdt,
219 int mark_bit, enum wk_req_mode mode)
220{
221 void __iomem *wdt_base = mtk_wdt->wdt_base;
222 u32 reg;
223
224 reg = readl(wdt_base + WDT_REQ_IRQ_EN);
225 reg |= WDT_REQ_IRQ_KEY;
226
227 if (mark_bit == WDT_REQ_MODE_SPM_SCPSYS) {
228 if (mode == WD_REQ_IRQ_MODE)
229 reg |= (WDT_REQ_IRQ_SPM_SCPSYS_EN);
230 if (mode == WD_REQ_RST_MODE)
231 reg &= ~(WDT_REQ_IRQ_SPM_SCPSYS_EN);
232 } else if (mark_bit == WDT_REQ_MODE_EINT) {
233 if (mode == WD_REQ_IRQ_MODE)
234 reg |= (WDT_REQ_IRQ_EINT_EN);
235 if (mode == WD_REQ_RST_MODE)
236 reg &= ~(WDT_REQ_IRQ_EINT_EN);
237 } else if (mark_bit == WDT_REQ_MODE_SYSRST) {
238 if (mode == WD_REQ_IRQ_MODE)
239 reg |= (WDT_REQ_IRQ_SYSRST_EN);
240 if (mode == WD_REQ_RST_MODE)
241 reg &= ~(WDT_REQ_IRQ_SYSRST_EN);
242 } else if (mark_bit == WDT_REQ_MODE_THERMAL) {
243 if (mode == WD_REQ_IRQ_MODE)
244 reg |= (WDT_REQ_IRQ_THERMAL_EN);
245 if (mode == WD_REQ_RST_MODE)
246 reg &= ~(WDT_REQ_IRQ_THERMAL_EN);
247 }
248
249 writel(reg, wdt_base + WDT_REQ_IRQ_EN);
250}
251
252static void mtk_wdt_request_default(struct mtk_wdt_dev *mtk_wdt)
253{
254
255 mtk_wdt_request_en_set(mtk_wdt, WDT_REQ_MODE_THERMAL, WD_REQ_EN);
256 mtk_wdt_request_mode_set(mtk_wdt, WDT_REQ_MODE_THERMAL,
257 WD_REQ_RST_MODE);
258
259 mtk_wdt_request_en_set(mtk_wdt, WDT_REQ_MODE_SPM_SCPSYS, WD_REQ_EN);
260 mtk_wdt_request_mode_set(mtk_wdt, WDT_REQ_MODE_SPM_SCPSYS,
261 WD_REQ_RST_MODE);
262}
263
264static int mtk_wdt_ping(struct watchdog_device *wdt_dev)
265{
266 struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
267 void __iomem *wdt_base = mtk_wdt->wdt_base;
268
269 iowrite32(WDT_RST_RELOAD, wdt_base + WDT_RST);
270
271 return 0;
272}
273
274static int mtk_wdt_set_timeout(struct watchdog_device *wdt_dev,
275 unsigned int timeout)
276{
277 struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
278 void __iomem *wdt_base = mtk_wdt->wdt_base;
279 u32 reg;
280
281 wdt_dev->timeout = timeout;
282
283 /*
284 * One bit is the value of 512 ticks
285 * The clock has 32 KHz
286 */
287 reg = WDT_LENGTH_TIMEOUT(timeout << 6) | WDT_LENGTH_KEY;
288 iowrite32(reg, wdt_base + WDT_LENGTH);
289
290 mtk_wdt_ping(wdt_dev);
291
292 return 0;
293}
294
295static int mtk_wdt_stop(struct watchdog_device *wdt_dev)
296{
297 struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
298 void __iomem *wdt_base = mtk_wdt->wdt_base;
299 u32 reg;
300
301 reg = readl(wdt_base + WDT_MODE);
302 reg &= ~WDT_MODE_EN;
303 reg |= WDT_MODE_KEY;
304 iowrite32(reg, wdt_base + WDT_MODE);
305
306 return 0;
307}
308
309static int mtk_wdt_start(struct watchdog_device *wdt_dev)
310{
311 u32 reg;
312 struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
313 void __iomem *wdt_base = mtk_wdt->wdt_base;
314 int ret;
315
316 ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
317 if (ret < 0)
318 return ret;
319
320 reg = ioread32(wdt_base + WDT_MODE);
321 reg |= (WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
322 reg |= (WDT_MODE_KEY | WDT_MODE_EXRST_EN);
323 iowrite32(reg, wdt_base + WDT_MODE);
324
325 return 0;
326}
327
328static const struct watchdog_info mtk_wdt_info = {
329 .identity = DRV_NAME,
330 .options = WDIOF_SETTIMEOUT |
331 WDIOF_KEEPALIVEPING |
332 WDIOF_MAGICCLOSE,
333};
334
335static const struct watchdog_ops mtk_wdt_ops = {
336 .owner = THIS_MODULE,
337 .start = mtk_wdt_start,
338 .stop = mtk_wdt_stop,
339 .ping = mtk_wdt_ping,
340 .set_timeout = mtk_wdt_set_timeout,
341 .restart = mtk_wdt_restart,
342};
343
344static int mtk_wdt_probe(struct platform_device *pdev)
345{
346 struct mtk_wdt_dev *mtk_wdt;
347 struct resource *res;
348 int err;
349
350 mtk_wdt = devm_kzalloc(&pdev->dev, sizeof(*mtk_wdt), GFP_KERNEL);
351 if (!mtk_wdt)
352 return -ENOMEM;
353
354 platform_set_drvdata(pdev, mtk_wdt);
355
356 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
357 mtk_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
358 if (IS_ERR(mtk_wdt->wdt_base))
359 return PTR_ERR(mtk_wdt->wdt_base);
360
361 toprgu_base = mtk_wdt->wdt_base;
362 arm_pm_restart = NULL;
363 mtk_wdt->wdt_dev.info = &mtk_wdt_info;
364 mtk_wdt->wdt_dev.ops = &mtk_wdt_ops;
365 mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
366 mtk_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
367 mtk_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
368 mtk_wdt->wdt_dev.parent = &pdev->dev;
369
370 err = of_property_read_u32(pdev->dev.of_node,
371 "eint", &mtk_wdt->eint);
372 if (err)
373 mtk_wdt->eint = 0;
374
375 mtk_wdt_request_default(mtk_wdt);
376
377 watchdog_init_timeout(&mtk_wdt->wdt_dev, timeout, &pdev->dev);
378 watchdog_set_nowayout(&mtk_wdt->wdt_dev, nowayout);
379 watchdog_set_restart_priority(&mtk_wdt->wdt_dev, 128);
380
381 watchdog_set_drvdata(&mtk_wdt->wdt_dev, mtk_wdt);
382
383 if (readl(mtk_wdt->wdt_base + WDT_MODE) & WDT_MODE_EN) {
384 set_bit(WDOG_HW_RUNNING, &mtk_wdt->wdt_dev.status);
385 mtk_wdt_start(&mtk_wdt->wdt_dev);
386 } else
387 mtk_wdt_stop(&mtk_wdt->wdt_dev);
388
389 err = watchdog_register_device(&mtk_wdt->wdt_dev);
390 if (unlikely(err))
391 return err;
392
393 dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
394 mtk_wdt->wdt_dev.timeout, nowayout);
395
396 return 0;
397}
398
399static void mtk_wdt_shutdown(struct platform_device *pdev)
400{
401 struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
402
403 if (watchdog_active(&mtk_wdt->wdt_dev))
404 mtk_wdt_stop(&mtk_wdt->wdt_dev);
405}
406
407static int mtk_wdt_remove(struct platform_device *pdev)
408{
409 struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
410
411 watchdog_unregister_device(&mtk_wdt->wdt_dev);
412
413 return 0;
414}
415
416#ifdef CONFIG_PM_SLEEP
417static int mtk_wdt_suspend(struct device *dev)
418{
419 struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
420
421 if (watchdog_active(&mtk_wdt->wdt_dev))
422 mtk_wdt_stop(&mtk_wdt->wdt_dev);
423
424 return 0;
425}
426
427static int mtk_wdt_resume(struct device *dev)
428{
429 struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
430
431 if (watchdog_active(&mtk_wdt->wdt_dev)) {
432 mtk_wdt_start(&mtk_wdt->wdt_dev);
433 mtk_wdt_ping(&mtk_wdt->wdt_dev);
434 }
435
436 return 0;
437}
438#endif
439
440static const struct of_device_id mtk_wdt_dt_ids[] = {
441 { .compatible = "mediatek,mt6589-wdt" },
442 { /* sentinel */ }
443};
444MODULE_DEVICE_TABLE(of, mtk_wdt_dt_ids);
445
446void __iomem *mtk_wd_Gettoprgu(void)
447{
448 struct device_node *np_rgu;
449
450 pr_info("get toprgu is %p\n", toprgu_base);
451
452 np_rgu = of_find_compatible_node(NULL, NULL,
453 mtk_wdt_dt_ids[0].compatible);
454
455 if (!toprgu_base)
456 toprgu_base = of_iomap(np_rgu, 0);
457 if (!toprgu_base)
458 pr_debug("RGU iomap failed\n");
459
460 return toprgu_base;
461}
462EXPORT_SYMBOL_GPL(mtk_wd_Gettoprgu);
463
464static const struct dev_pm_ops mtk_wdt_pm_ops = {
465 SET_SYSTEM_SLEEP_PM_OPS(mtk_wdt_suspend,
466 mtk_wdt_resume)
467};
468
469static struct platform_driver mtk_wdt_driver = {
470 .probe = mtk_wdt_probe,
471 .remove = mtk_wdt_remove,
472 .shutdown = mtk_wdt_shutdown,
473 .driver = {
474 .name = DRV_NAME,
475 .pm = &mtk_wdt_pm_ops,
476 .of_match_table = mtk_wdt_dt_ids,
477 },
478};
479
480module_platform_driver(mtk_wdt_driver);
481
482module_param(timeout, uint, 0);
483MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
484
485module_param(nowayout, bool, 0);
486MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
487 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
488
489MODULE_LICENSE("GPL");
490MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
491MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
492MODULE_VERSION(DRV_VERSION);