blob: dd97a17a70146f73a5b4f5f42ed463433aee6cd9 [file] [log] [blame]
Marek Behúna86b97d2018-04-24 17:21:30 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Marvell Armada 37xx SoC Watchdog Driver
4 *
5 * Marek Behun <marek.behun@nic.cz>
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <wdt.h>
11#include <asm/io.h>
12#include <asm/arch/cpu.h>
13#include <asm/arch/soc.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Marek Behúna86b97d2018-04-24 17:21:30 +020015
16DECLARE_GLOBAL_DATA_PTR;
17
18struct a37xx_wdt {
19 void __iomem *sel_reg;
20 void __iomem *reg;
21 ulong clk_rate;
22 u64 timeout;
23};
24
25/*
Marek Behúnae0ae012018-12-17 16:10:06 +010026 * We use Counter 1 as watchdog timer, and Counter 0 for re-triggering Counter 1
Marek Behúna86b97d2018-04-24 17:21:30 +020027 */
28
Marek Behúnae0ae012018-12-17 16:10:06 +010029#define CNTR_CTRL(id) ((id) * 0x10)
Marek Behúna86b97d2018-04-24 17:21:30 +020030#define CNTR_CTRL_ENABLE 0x0001
31#define CNTR_CTRL_ACTIVE 0x0002
32#define CNTR_CTRL_MODE_MASK 0x000c
33#define CNTR_CTRL_MODE_ONESHOT 0x0000
Marek Behúnae0ae012018-12-17 16:10:06 +010034#define CNTR_CTRL_MODE_HWSIG 0x000c
35#define CNTR_CTRL_TRIG_SRC_MASK 0x00f0
36#define CNTR_CTRL_TRIG_SRC_PREV_CNTR 0x0050
Marek Behúna86b97d2018-04-24 17:21:30 +020037#define CNTR_CTRL_PRESCALE_MASK 0xff00
38#define CNTR_CTRL_PRESCALE_MIN 2
39#define CNTR_CTRL_PRESCALE_SHIFT 8
40
Marek Behúnae0ae012018-12-17 16:10:06 +010041#define CNTR_COUNT_LOW(id) (CNTR_CTRL(id) + 0x4)
42#define CNTR_COUNT_HIGH(id) (CNTR_CTRL(id) + 0x8)
Marek Behúna86b97d2018-04-24 17:21:30 +020043
Marek Behúnae0ae012018-12-17 16:10:06 +010044static void set_counter_value(struct a37xx_wdt *priv, int id, u64 val)
Marek Behúna86b97d2018-04-24 17:21:30 +020045{
Marek Behúnae0ae012018-12-17 16:10:06 +010046 writel(val & 0xffffffff, priv->reg + CNTR_COUNT_LOW(id));
47 writel(val >> 32, priv->reg + CNTR_COUNT_HIGH(id));
Marek Behúna86b97d2018-04-24 17:21:30 +020048}
49
Marek Behúnae0ae012018-12-17 16:10:06 +010050static void counter_enable(struct a37xx_wdt *priv, int id)
Marek Behúna86b97d2018-04-24 17:21:30 +020051{
Marek Behúnae0ae012018-12-17 16:10:06 +010052 setbits_le32(priv->reg + CNTR_CTRL(id), CNTR_CTRL_ENABLE);
53}
Marek Behúna86b97d2018-04-24 17:21:30 +020054
Marek Behúnae0ae012018-12-17 16:10:06 +010055static void counter_disable(struct a37xx_wdt *priv, int id)
56{
57 clrbits_le32(priv->reg + CNTR_CTRL(id), CNTR_CTRL_ENABLE);
Marek Behúna86b97d2018-04-24 17:21:30 +020058}
59
Marek Behúnae0ae012018-12-17 16:10:06 +010060static int init_counter(struct a37xx_wdt *priv, int id, u32 mode, u32 trig_src)
Marek Behúna86b97d2018-04-24 17:21:30 +020061{
Marek Behúnae0ae012018-12-17 16:10:06 +010062 u32 reg;
63
64 reg = readl(priv->reg + CNTR_CTRL(id));
65 if (reg & CNTR_CTRL_ACTIVE)
66 return -EBUSY;
67
68 reg &= ~(CNTR_CTRL_MODE_MASK | CNTR_CTRL_PRESCALE_MASK |
69 CNTR_CTRL_TRIG_SRC_MASK);
Marek Behúna86b97d2018-04-24 17:21:30 +020070
Marek Behúnae0ae012018-12-17 16:10:06 +010071 /* set mode */
72 reg |= mode;
73
74 /* set prescaler to the min value */
75 reg |= CNTR_CTRL_PRESCALE_MIN << CNTR_CTRL_PRESCALE_SHIFT;
76
77 /* set trigger source */
78 reg |= trig_src;
79
80 writel(reg, priv->reg + CNTR_CTRL(id));
81
82 return 0;
Marek Behúna86b97d2018-04-24 17:21:30 +020083}
84
85static int a37xx_wdt_reset(struct udevice *dev)
86{
87 struct a37xx_wdt *priv = dev_get_priv(dev);
88
89 if (!priv->timeout)
90 return -EINVAL;
91
Marek Behúnae0ae012018-12-17 16:10:06 +010092 /* counter 1 is retriggered by forcing end count on counter 0 */
93 counter_disable(priv, 0);
94 counter_enable(priv, 0);
Marek Behúna86b97d2018-04-24 17:21:30 +020095
96 return 0;
97}
98
99static int a37xx_wdt_expire_now(struct udevice *dev, ulong flags)
100{
101 struct a37xx_wdt *priv = dev_get_priv(dev);
102
Marek Behúnae0ae012018-12-17 16:10:06 +0100103 /* first we set timeout to 0 */
104 counter_disable(priv, 1);
105 set_counter_value(priv, 1, 0);
106 counter_enable(priv, 1);
107
108 /* and then we start counter 1 by forcing end count on counter 0 */
109 counter_disable(priv, 0);
110 counter_enable(priv, 0);
Marek Behúna86b97d2018-04-24 17:21:30 +0200111
112 return 0;
113}
114
115static int a37xx_wdt_start(struct udevice *dev, u64 ms, ulong flags)
116{
117 struct a37xx_wdt *priv = dev_get_priv(dev);
Marek Behúnae0ae012018-12-17 16:10:06 +0100118 int err;
Marek Behúna86b97d2018-04-24 17:21:30 +0200119
Marek Behúnae0ae012018-12-17 16:10:06 +0100120 err = init_counter(priv, 0, CNTR_CTRL_MODE_ONESHOT, 0);
121 if (err < 0)
122 return err;
Marek Behúna86b97d2018-04-24 17:21:30 +0200123
Marek Behúnae0ae012018-12-17 16:10:06 +0100124 err = init_counter(priv, 1, CNTR_CTRL_MODE_HWSIG,
125 CNTR_CTRL_TRIG_SRC_PREV_CNTR);
126 if (err < 0)
127 return err;
Marek Behúna86b97d2018-04-24 17:21:30 +0200128
129 priv->timeout = ms * priv->clk_rate / 1000 / CNTR_CTRL_PRESCALE_MIN;
130
Marek Behúnae0ae012018-12-17 16:10:06 +0100131 set_counter_value(priv, 0, 0);
132 set_counter_value(priv, 1, priv->timeout);
133 counter_enable(priv, 1);
Marek Behúna86b97d2018-04-24 17:21:30 +0200134
Marek Behúnae0ae012018-12-17 16:10:06 +0100135 /* we have to force end count on counter 0 to start counter 1 */
136 counter_enable(priv, 0);
Marek Behúna86b97d2018-04-24 17:21:30 +0200137
138 return 0;
139}
140
141static int a37xx_wdt_stop(struct udevice *dev)
142{
143 struct a37xx_wdt *priv = dev_get_priv(dev);
144
Marek Behúnae0ae012018-12-17 16:10:06 +0100145 counter_disable(priv, 1);
146 counter_disable(priv, 0);
147 writel(0, priv->sel_reg);
Marek Behúna86b97d2018-04-24 17:21:30 +0200148
149 return 0;
150}
151
152static int a37xx_wdt_probe(struct udevice *dev)
153{
154 struct a37xx_wdt *priv = dev_get_priv(dev);
155 fdt_addr_t addr;
156
157 addr = dev_read_addr_index(dev, 0);
158 if (addr == FDT_ADDR_T_NONE)
159 goto err;
160 priv->sel_reg = (void __iomem *)addr;
161
162 addr = dev_read_addr_index(dev, 1);
163 if (addr == FDT_ADDR_T_NONE)
164 goto err;
165 priv->reg = (void __iomem *)addr;
166
167 priv->clk_rate = (ulong)get_ref_clk() * 1000000;
168
Marek Behúna86b97d2018-04-24 17:21:30 +0200169 /*
Marek Behúnae0ae012018-12-17 16:10:06 +0100170 * We use counter 1 as watchdog timer, therefore we only set bit
171 * TIMER1_IS_WCHDOG_TIMER. Counter 0 is only used to force re-trigger on
172 * counter 1.
Marek Behúna86b97d2018-04-24 17:21:30 +0200173 */
174 writel(1 << 1, priv->sel_reg);
175
176 return 0;
177err:
178 dev_err(dev, "no io address\n");
179 return -ENODEV;
180}
181
182static const struct wdt_ops a37xx_wdt_ops = {
183 .start = a37xx_wdt_start,
184 .reset = a37xx_wdt_reset,
185 .stop = a37xx_wdt_stop,
186 .expire_now = a37xx_wdt_expire_now,
187};
188
189static const struct udevice_id a37xx_wdt_ids[] = {
190 { .compatible = "marvell,armada-3700-wdt" },
191 {}
192};
193
194U_BOOT_DRIVER(a37xx_wdt) = {
195 .name = "armada_37xx_wdt",
196 .id = UCLASS_WDT,
197 .of_match = a37xx_wdt_ids,
198 .probe = a37xx_wdt_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700199 .priv_auto = sizeof(struct a37xx_wdt),
Marek Behúna86b97d2018-04-24 17:21:30 +0200200 .ops = &a37xx_wdt_ops,
201};