blob: a7d4c7a3b85b5e4f06bb17d92604af29a1e5bc20 [file] [log] [blame]
Ryder Lee090543f2018-11-15 10:07:57 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Watchdog driver for MediaTek SoCs
4 *
5 * Copyright (C) 2018 MediaTek Inc.
6 * Author: Ryder Lee <ryder.lee@mediatek.com>
7 */
8
9#include <common.h>
10#include <dm.h>
11#include <wdt.h>
12#include <asm/io.h>
13
14#define MTK_WDT_MODE 0x00
15#define MTK_WDT_LENGTH 0x04
16#define MTK_WDT_RESTART 0x08
17#define MTK_WDT_STATUS 0x0c
18#define MTK_WDT_INTERVAL 0x10
19#define MTK_WDT_SWRST 0x14
20#define MTK_WDT_REQ_MODE 0x30
21#define MTK_WDT_DEBUG_CTL 0x40
22
23#define WDT_MODE_KEY (0x22 << 24)
24#define WDT_MODE_EN BIT(0)
25#define WDT_MODE_EXTPOL BIT(1)
26#define WDT_MODE_EXTEN BIT(2)
27#define WDT_MODE_IRQ_EN BIT(3)
28#define WDT_MODE_DUAL_EN BIT(6)
29
30#define WDT_LENGTH_KEY 0x8
31#define WDT_LENGTH_TIMEOUT(n) ((n) << 5)
32
33#define WDT_RESTART_KEY 0x1971
34#define WDT_SWRST_KEY 0x1209
35
36struct mtk_wdt_priv {
37 void __iomem *base;
38};
39
40static int mtk_wdt_reset(struct udevice *dev)
41{
42 struct mtk_wdt_priv *priv = dev_get_priv(dev);
43
44 /* Reload watchdog duration */
45 writel(WDT_RESTART_KEY, priv->base + MTK_WDT_RESTART);
46
47 return 0;
48}
49
50static int mtk_wdt_stop(struct udevice *dev)
51{
52 struct mtk_wdt_priv *priv = dev_get_priv(dev);
53
54 clrsetbits_le32(priv->base + MTK_WDT_MODE, WDT_MODE_EN, WDT_MODE_KEY);
55
56 return 0;
57}
58
59static int mtk_wdt_expire_now(struct udevice *dev, ulong flags)
60{
61 struct mtk_wdt_priv *priv = dev_get_priv(dev);
62
63 /* Kick watchdog to prevent counter == 0 */
64 writel(WDT_RESTART_KEY, priv->base + MTK_WDT_RESTART);
65
66 /* Reset */
67 writel(WDT_SWRST_KEY, priv->base + MTK_WDT_SWRST);
68 hang();
69
70 return 0;
71}
72
Shannon Barber2bc18212019-06-07 20:48:19 +000073static void mtk_wdt_set_timeout(struct udevice *dev, unsigned int timeout_ms)
Ryder Lee090543f2018-11-15 10:07:57 +080074{
75 struct mtk_wdt_priv *priv = dev_get_priv(dev);
76
77 /*
Shannon Barber2bc18212019-06-07 20:48:19 +000078 * One WDT_LENGTH count is 512 ticks of the wdt clock
79 * Clock runs at 32768 Hz
80 * e.g. 15.625 ms per count (nominal)
81 * We want the ceiling after dividing timeout_ms by 15.625 ms
82 * We add 15624 prior to the divide to implement the ceiling
83 * We prevent over-flow by clamping the timeout_ms value here
84 * as the maximum WDT_LENGTH counts is 1023 -> 15.984375 sec
85 * We also enforce a minimum of 1 count
86 * Many watchdog peripherals have a self-imposed count of 1
87 * that is added to the register counts.
88 * The MediaTek docs lack details to know if this is the case here.
89 * So we enforce a minimum of 1 to guarantee operation.
Ryder Lee090543f2018-11-15 10:07:57 +080090 */
Shannon Barber2bc18212019-06-07 20:48:19 +000091 if(timeout_ms > 15984) timeout_ms = 15984;
92 u64 timeout_us = timeout_ms * 1000;
93 u32 timeout_cc = (u32) ( (15624 + timeout_us) / 15625 );
94 if(timeout_cc == 0) timeout_cc = 1;
95 u32 length = WDT_LENGTH_TIMEOUT(timeout_cc) | WDT_LENGTH_KEY;
96 writel(length, priv->base + MTK_WDT_LENGTH);
Ryder Lee090543f2018-11-15 10:07:57 +080097}
98
99static int mtk_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
100{
101 struct mtk_wdt_priv *priv = dev_get_priv(dev);
102
103 mtk_wdt_set_timeout(dev, timeout);
104
Shannon Barber2bc18212019-06-07 20:48:19 +0000105 mtk_wdt_reset(dev);
106
Ryder Lee090543f2018-11-15 10:07:57 +0800107 /* Enable watchdog reset signal */
108 setbits_le32(priv->base + MTK_WDT_MODE,
109 WDT_MODE_EN | WDT_MODE_KEY | WDT_MODE_EXTEN);
110
111 return 0;
112}
113
114static int mtk_wdt_probe(struct udevice *dev)
115{
116 struct mtk_wdt_priv *priv = dev_get_priv(dev);
117
118 priv->base = dev_read_addr_ptr(dev);
119 if (!priv->base)
120 return -ENOENT;
121
122 /* Clear status */
123 clrsetbits_le32(priv->base + MTK_WDT_MODE,
124 WDT_MODE_IRQ_EN | WDT_MODE_EXTPOL, WDT_MODE_KEY);
125
126 return mtk_wdt_stop(dev);
127}
128
129static const struct wdt_ops mtk_wdt_ops = {
130 .start = mtk_wdt_start,
131 .reset = mtk_wdt_reset,
132 .stop = mtk_wdt_stop,
133 .expire_now = mtk_wdt_expire_now,
134};
135
136static const struct udevice_id mtk_wdt_ids[] = {
137 { .compatible = "mediatek,wdt"},
138 {}
139};
140
141U_BOOT_DRIVER(mtk_wdt) = {
142 .name = "mtk_wdt",
143 .id = UCLASS_WDT,
144 .of_match = mtk_wdt_ids,
145 .priv_auto_alloc_size = sizeof(struct mtk_wdt_priv),
146 .probe = mtk_wdt_probe,
147 .ops = &mtk_wdt_ops,
148 .flags = DM_FLAG_PRE_RELOC,
149};