blob: cb5a786c58914960c484a68556db0e82decb0d8f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +01002/*
3 * Cadence WDT driver - Used by Xilinx Zynq
4 * Reference: Linux kernel Cadence watchdog driver.
5 *
6 * Author(s): Shreenidhi Shedi <yesshedi@gmail.com>
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +01007 */
8
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +01009#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +010011#include <wdt.h>
12#include <clk.h>
Stefan Roese83af2762019-04-11 15:58:45 +020013#include <div64.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +010016#include <linux/io.h>
17
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +010018struct cdns_regs {
19 u32 zmr; /* WD Zero mode register, offset - 0x0 */
20 u32 ccr; /* Counter Control Register offset - 0x4 */
21 u32 restart; /* Restart key register, offset - 0x8 */
22 u32 status; /* Status Register, offset - 0xC */
23};
24
25struct cdns_wdt_priv {
26 bool rst;
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +010027 struct cdns_regs *regs;
28};
29
30#define CDNS_WDT_DEFAULT_TIMEOUT 10
31
32/* Supports 1 - 516 sec */
33#define CDNS_WDT_MIN_TIMEOUT 1
34#define CDNS_WDT_MAX_TIMEOUT 516
35
36/* Restart key */
37#define CDNS_WDT_RESTART_KEY 0x00001999
38
39/* Counter register access key */
40#define CDNS_WDT_REGISTER_ACCESS_KEY 0x00920000
41
42/* Counter value divisor */
43#define CDNS_WDT_COUNTER_VALUE_DIVISOR 0x1000
44
45/* Clock prescaler value and selection */
46#define CDNS_WDT_PRESCALE_64 64
47#define CDNS_WDT_PRESCALE_512 512
48#define CDNS_WDT_PRESCALE_4096 4096
49#define CDNS_WDT_PRESCALE_SELECT_64 1
50#define CDNS_WDT_PRESCALE_SELECT_512 2
51#define CDNS_WDT_PRESCALE_SELECT_4096 3
52
53/* Input clock frequency */
54#define CDNS_WDT_CLK_75MHZ 75000000
55
56/* Counter maximum value */
57#define CDNS_WDT_COUNTER_MAX 0xFFF
58
59/********************* Register Map **********************************/
60
61/*
62 * Zero Mode Register - This register controls how the time out is indicated
63 * and also contains the access code to allow writes to the register (0xABC).
64 */
65#define CDNS_WDT_ZMR_WDEN_MASK 0x00000001 /* Enable the WDT */
66#define CDNS_WDT_ZMR_RSTEN_MASK 0x00000002 /* Enable the reset output */
67#define CDNS_WDT_ZMR_IRQEN_MASK 0x00000004 /* Enable IRQ output */
68#define CDNS_WDT_ZMR_RSTLEN_16 0x00000030 /* Reset pulse of 16 pclk cycles */
69#define CDNS_WDT_ZMR_ZKEY_VAL 0x00ABC000 /* Access key, 0xABC << 12 */
70
71/*
72 * Counter Control register - This register controls how fast the timer runs
73 * and the reset value and also contains the access code to allow writes to
74 * the register.
75 */
76#define CDNS_WDT_CCR_CRV_MASK 0x00003FFC /* Counter reset value */
77
78/* Write access to Registers */
79static inline void cdns_wdt_writereg(u32 *addr, u32 val)
80{
81 writel(val, addr);
82}
83
84/**
85 * cdns_wdt_reset - Reload the watchdog timer (i.e. pat the watchdog).
86 *
87 * @dev: Watchdog device
88 *
89 * Write the restart key value (0x00001999) to the restart register.
90 *
91 * Return: Always 0
92 */
93static int cdns_wdt_reset(struct udevice *dev)
94{
95 struct cdns_wdt_priv *priv = dev_get_priv(dev);
96
97 debug("%s\n", __func__);
98
99 cdns_wdt_writereg(&priv->regs->restart, CDNS_WDT_RESTART_KEY);
100
101 return 0;
102}
103
104/**
105 * cdns_wdt_start - Enable and start the watchdog.
106 *
107 * @dev: Watchdog device
108 * @timeout: Timeout value
109 * @flags: Driver flags
110 *
111 * The counter value is calculated according to the formula:
112 * count = (timeout * clock) / prescaler + 1.
113 *
114 * The calculated count is divided by 0x1000 to obtain the field value
115 * to write to counter control register.
116 *
117 * Clears the contents of prescaler and counter reset value. Sets the
118 * prescaler to 4096 and the calculated count and access key
119 * to write to CCR Register.
120 *
121 * Sets the WDT (WDEN bit) and either the Reset signal(RSTEN bit)
122 * or Interrupt signal(IRQEN) with a specified cycles and the access
123 * key to write to ZMR Register.
124 *
125 * Return: Upon success 0, failure -1.
126 */
127static int cdns_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
128{
129 ulong clk_f;
130 u32 count, prescaler, ctrl_clksel, data = 0;
131 struct clk clock;
132 struct cdns_wdt_priv *priv = dev_get_priv(dev);
133
134 if (clk_get_by_index(dev, 0, &clock) < 0) {
135 dev_err(dev, "failed to get clock\n");
136 return -1;
137 }
138
139 clk_f = clk_get_rate(&clock);
140 if (IS_ERR_VALUE(clk_f)) {
141 dev_err(dev, "failed to get rate\n");
142 return -1;
143 }
144
Stefan Roese83af2762019-04-11 15:58:45 +0200145 /* Calculate timeout in seconds and restrict to min and max value */
146 do_div(timeout, 1000);
147 timeout = max_t(u64, timeout, CDNS_WDT_MIN_TIMEOUT);
148 timeout = min_t(u64, timeout, CDNS_WDT_MAX_TIMEOUT);
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100149
Michal Simek856b5ad2018-04-19 15:22:04 +0200150 debug("%s: CLK_FREQ %ld, timeout %lld\n", __func__, clk_f, timeout);
151
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100152 if (clk_f <= CDNS_WDT_CLK_75MHZ) {
153 prescaler = CDNS_WDT_PRESCALE_512;
154 ctrl_clksel = CDNS_WDT_PRESCALE_SELECT_512;
155 } else {
156 prescaler = CDNS_WDT_PRESCALE_4096;
157 ctrl_clksel = CDNS_WDT_PRESCALE_SELECT_4096;
158 }
159
160 /*
161 * Counter value divisor to obtain the value of
162 * counter reset to be written to control register.
163 */
164 count = (timeout * (clk_f / prescaler)) /
165 CDNS_WDT_COUNTER_VALUE_DIVISOR + 1;
166
167 if (count > CDNS_WDT_COUNTER_MAX)
168 count = CDNS_WDT_COUNTER_MAX;
169
170 cdns_wdt_writereg(&priv->regs->zmr, CDNS_WDT_ZMR_ZKEY_VAL);
171
172 count = (count << 2) & CDNS_WDT_CCR_CRV_MASK;
173
174 /* Write counter access key first to be able write to register */
175 data = count | CDNS_WDT_REGISTER_ACCESS_KEY | ctrl_clksel;
176 cdns_wdt_writereg(&priv->regs->ccr, data);
177
178 data = CDNS_WDT_ZMR_WDEN_MASK | CDNS_WDT_ZMR_RSTLEN_16 |
179 CDNS_WDT_ZMR_ZKEY_VAL;
180
181 /* Reset on timeout if specified in device tree. */
182 if (priv->rst) {
183 data |= CDNS_WDT_ZMR_RSTEN_MASK;
184 data &= ~CDNS_WDT_ZMR_IRQEN_MASK;
185 } else {
186 data &= ~CDNS_WDT_ZMR_RSTEN_MASK;
187 data |= CDNS_WDT_ZMR_IRQEN_MASK;
188 }
189
190 cdns_wdt_writereg(&priv->regs->zmr, data);
191 cdns_wdt_writereg(&priv->regs->restart, CDNS_WDT_RESTART_KEY);
192
193 return 0;
194}
195
196/**
197 * cdns_wdt_stop - Stop the watchdog.
198 *
199 * @dev: Watchdog device
200 *
201 * Read the contents of the ZMR register, clear the WDEN bit in the register
202 * and set the access key for successful write.
203 *
204 * Return: Always 0
205 */
206static int cdns_wdt_stop(struct udevice *dev)
207{
208 struct cdns_wdt_priv *priv = dev_get_priv(dev);
209
210 cdns_wdt_writereg(&priv->regs->zmr,
211 CDNS_WDT_ZMR_ZKEY_VAL & (~CDNS_WDT_ZMR_WDEN_MASK));
212
213 return 0;
214}
215
216/**
Michal Simek991dce72021-06-28 13:44:16 +0200217 * cdns_wdt_expire_now - Expire the watchdog.
218 *
219 * @dev: Watchdog device
220 * @flags: Driver flags
221 *
222 * Access WDT and configure with minimal counter value to expire ASAP.
223 * Expiration issues system reset. When DEBUG is enabled count should be
224 * bigger to at least see debug message.
225 *
226 * Return: Always 0
227 */
228static int cdns_wdt_expire_now(struct udevice *dev, ulong flags)
229{
230 struct cdns_wdt_priv *priv = dev_get_priv(dev);
231 u32 data, count = 0;
232
233#if defined(DEBUG)
234 count = 0x40; /* Increase the value if you need more time */
235 debug("%s: Expire wdt%u\n", __func__, dev_seq(dev));
236#endif
237
238 cdns_wdt_writereg(&priv->regs->zmr, CDNS_WDT_ZMR_ZKEY_VAL);
239
240 count = (count << 2) & CDNS_WDT_CCR_CRV_MASK;
241
242 /* Write counter access key first to be able write to register */
243 data = count | CDNS_WDT_REGISTER_ACCESS_KEY;
244 cdns_wdt_writereg(&priv->regs->ccr, data);
245
246 data = CDNS_WDT_ZMR_WDEN_MASK | CDNS_WDT_ZMR_RSTEN_MASK |
247 CDNS_WDT_ZMR_ZKEY_VAL;
248
249 cdns_wdt_writereg(&priv->regs->zmr, data);
250 cdns_wdt_writereg(&priv->regs->restart, CDNS_WDT_RESTART_KEY);
251
252 return 0;
253}
254
255/**
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100256 * cdns_wdt_probe - Probe call for the device.
257 *
258 * @dev: Handle to the udevice structure.
259 *
260 * Return: Always 0.
261 */
262static int cdns_wdt_probe(struct udevice *dev)
263{
Simon Glass75e534b2020-12-16 21:20:07 -0700264 debug("%s: Probing wdt%u\n", __func__, dev_seq(dev));
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100265
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100266 return 0;
267}
268
Simon Glassaad29ae2020-12-03 16:55:21 -0700269static int cdns_wdt_of_to_plat(struct udevice *dev)
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100270{
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100271 struct cdns_wdt_priv *priv = dev_get_priv(dev);
272
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100273 priv->regs = dev_read_addr_ptr(dev);
274 if (!priv->regs)
275 return -EINVAL;
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100276
Michal Simek83319222018-05-16 10:57:27 +0200277 priv->rst = dev_read_bool(dev, "reset-on-timeout");
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100278
Stefan Roese83af2762019-04-11 15:58:45 +0200279 debug("%s: reset %d\n", __func__, priv->rst);
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100280
281 return 0;
282}
283
284static const struct wdt_ops cdns_wdt_ops = {
285 .start = cdns_wdt_start,
286 .reset = cdns_wdt_reset,
287 .stop = cdns_wdt_stop,
Michal Simek991dce72021-06-28 13:44:16 +0200288 .expire_now = cdns_wdt_expire_now,
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100289};
290
291static const struct udevice_id cdns_wdt_ids[] = {
292 { .compatible = "cdns,wdt-r1p2" },
293 {}
294};
295
296U_BOOT_DRIVER(cdns_wdt) = {
297 .name = "cdns_wdt",
298 .id = UCLASS_WDT,
299 .of_match = cdns_wdt_ids,
300 .probe = cdns_wdt_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700301 .priv_auto = sizeof(struct cdns_wdt_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -0700302 .of_to_plat = cdns_wdt_of_to_plat,
Shreenidhi Shedi0787c7c2018-02-21 16:50:20 +0100303 .ops = &cdns_wdt_ops,
304};