1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Derived from linux/drivers/watchdog/sunxi_wdt.c:
* Copyright (C) 2013 Carlo Caione
* Copyright (C) 2012 Henrik Nordstrom
*/
#include <dm.h>
#include <wdt.h>
#include <asm/io.h>
#include <linux/delay.h>
#define MSEC_PER_SEC 1000
#define WDT_MAX_TIMEOUT 16
#define WDT_TIMEOUT_MASK 0xf
#define WDT_CTRL_RELOAD ((1 << 0) | (0x0a57 << 1))
#define WDT_MODE_EN BIT(0)
struct sunxi_wdt_reg {
u8 wdt_ctrl;
u8 wdt_cfg;
u8 wdt_mode;
u8 wdt_timeout_shift;
u8 wdt_reset_mask;
u8 wdt_reset_val;
u32 wdt_key_val;
};
struct sunxi_wdt_priv {
void __iomem *base;
const struct sunxi_wdt_reg *regs;
};
/* Map of timeout in seconds to register value */
static const u8 wdt_timeout_map[1 + WDT_MAX_TIMEOUT] = {
[0] = 0x0,
[1] = 0x1,
[2] = 0x2,
[3] = 0x3,
[4] = 0x4,
[5] = 0x5,
[6] = 0x6,
[7] = 0x7,
[8] = 0x7,
[9] = 0x8,
[10] = 0x8,
[11] = 0x9,
[12] = 0x9,
[13] = 0xa,
[14] = 0xa,
[15] = 0xb,
[16] = 0xb,
};
static int sunxi_wdt_reset(struct udevice *dev)
{
struct sunxi_wdt_priv *priv = dev_get_priv(dev);
const struct sunxi_wdt_reg *regs = priv->regs;
void __iomem *base = priv->base;
writel(WDT_CTRL_RELOAD, base + regs->wdt_ctrl);
return 0;
}
static int sunxi_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
{
struct sunxi_wdt_priv *priv = dev_get_priv(dev);
const struct sunxi_wdt_reg *regs = priv->regs;
void __iomem *base = priv->base;
u32 val;
timeout /= MSEC_PER_SEC;
if (timeout > WDT_MAX_TIMEOUT)
timeout = WDT_MAX_TIMEOUT;
/* Set system reset function */
val = readl(base + regs->wdt_cfg);
val &= ~regs->wdt_reset_mask;
val |= regs->wdt_reset_val;
val |= regs->wdt_key_val;
writel(val, base + regs->wdt_cfg);
/* Set timeout and enable watchdog */
val = readl(base + regs->wdt_mode);
val &= ~(WDT_TIMEOUT_MASK << regs->wdt_timeout_shift);
val |= wdt_timeout_map[timeout] << regs->wdt_timeout_shift;
val |= WDT_MODE_EN;
val |= regs->wdt_key_val;
writel(val, base + regs->wdt_mode);
return sunxi_wdt_reset(dev);
}
static int sunxi_wdt_stop(struct udevice *dev)
{
struct sunxi_wdt_priv *priv = dev_get_priv(dev);
const struct sunxi_wdt_reg *regs = priv->regs;
void __iomem *base = priv->base;
writel(regs->wdt_key_val, base + regs->wdt_mode);
return 0;
}
static int sunxi_wdt_expire_now(struct udevice *dev, ulong flags)
{
int ret;
ret = sunxi_wdt_start(dev, 0, flags);
if (ret)
return ret;
mdelay(500);
return 0;
}
static const struct wdt_ops sunxi_wdt_ops = {
.reset = sunxi_wdt_reset,
.start = sunxi_wdt_start,
.stop = sunxi_wdt_stop,
.expire_now = sunxi_wdt_expire_now,
};
static const struct sunxi_wdt_reg sun4i_wdt_reg = {
.wdt_ctrl = 0x00,
.wdt_cfg = 0x04,
.wdt_mode = 0x04,
.wdt_timeout_shift = 3,
.wdt_reset_mask = 0x2,
.wdt_reset_val = 0x2,
};
static const struct sunxi_wdt_reg sun6i_wdt_reg = {
.wdt_ctrl = 0x10,
.wdt_cfg = 0x14,
.wdt_mode = 0x18,
.wdt_timeout_shift = 4,
.wdt_reset_mask = 0x3,
.wdt_reset_val = 0x1,
};
static const struct sunxi_wdt_reg sun20i_wdt_reg = {
.wdt_ctrl = 0x10,
.wdt_cfg = 0x14,
.wdt_mode = 0x18,
.wdt_timeout_shift = 4,
.wdt_reset_mask = 0x03,
.wdt_reset_val = 0x01,
.wdt_key_val = 0x16aa0000,
};
static const struct udevice_id sunxi_wdt_ids[] = {
{ .compatible = "allwinner,sun4i-a10-wdt", .data = (ulong)&sun4i_wdt_reg },
{ .compatible = "allwinner,sun6i-a31-wdt", .data = (ulong)&sun6i_wdt_reg },
{ .compatible = "allwinner,sun20i-d1-wdt", .data = (ulong)&sun20i_wdt_reg },
{ /* sentinel */ }
};
static int sunxi_wdt_probe(struct udevice *dev)
{
struct sunxi_wdt_priv *priv = dev_get_priv(dev);
priv->base = dev_remap_addr(dev);
if (!priv->base)
return -EINVAL;
priv->regs = (void *)dev_get_driver_data(dev);
if (!priv->regs)
return -EINVAL;
sunxi_wdt_stop(dev);
return 0;
}
U_BOOT_DRIVER(sunxi_wdt) = {
.name = "sunxi_wdt",
.id = UCLASS_WDT,
.of_match = sunxi_wdt_ids,
.probe = sunxi_wdt_probe,
.priv_auto = sizeof(struct sunxi_wdt_priv),
.ops = &sunxi_wdt_ops,
};
|