blob: 91cd8a6e6a2ac3b74ae9c3dde9fa6adaffe0560e [file] [log] [blame]
Marek Behún2b69a672018-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>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17struct a37xx_wdt {
18 void __iomem *sel_reg;
19 void __iomem *reg;
20 ulong clk_rate;
21 u64 timeout;
22};
23
24/*
Marek Behún7b03e992018-12-17 16:10:06 +010025 * We use Counter 1 as watchdog timer, and Counter 0 for re-triggering Counter 1
Marek Behún2b69a672018-04-24 17:21:30 +020026 */
27
Marek Behún7b03e992018-12-17 16:10:06 +010028#define CNTR_CTRL(id) ((id) * 0x10)
Marek Behún2b69a672018-04-24 17:21:30 +020029#define CNTR_CTRL_ENABLE 0x0001
30#define CNTR_CTRL_ACTIVE 0x0002
31#define CNTR_CTRL_MODE_MASK 0x000c
32#define CNTR_CTRL_MODE_ONESHOT 0x0000
Marek Behún7b03e992018-12-17 16:10:06 +010033#define CNTR_CTRL_MODE_HWSIG 0x000c
34#define CNTR_CTRL_TRIG_SRC_MASK 0x00f0
35#define CNTR_CTRL_TRIG_SRC_PREV_CNTR 0x0050
Marek Behún2b69a672018-04-24 17:21:30 +020036#define CNTR_CTRL_PRESCALE_MASK 0xff00
37#define CNTR_CTRL_PRESCALE_MIN 2
38#define CNTR_CTRL_PRESCALE_SHIFT 8
39
Marek Behún7b03e992018-12-17 16:10:06 +010040#define CNTR_COUNT_LOW(id) (CNTR_CTRL(id) + 0x4)
41#define CNTR_COUNT_HIGH(id) (CNTR_CTRL(id) + 0x8)
Marek Behún2b69a672018-04-24 17:21:30 +020042
Marek Behún7b03e992018-12-17 16:10:06 +010043static void set_counter_value(struct a37xx_wdt *priv, int id, u64 val)
Marek Behún2b69a672018-04-24 17:21:30 +020044{
Marek Behún7b03e992018-12-17 16:10:06 +010045 writel(val & 0xffffffff, priv->reg + CNTR_COUNT_LOW(id));
46 writel(val >> 32, priv->reg + CNTR_COUNT_HIGH(id));
Marek Behún2b69a672018-04-24 17:21:30 +020047}
48
Marek Behún7b03e992018-12-17 16:10:06 +010049static void counter_enable(struct a37xx_wdt *priv, int id)
Marek Behún2b69a672018-04-24 17:21:30 +020050{
Marek Behún7b03e992018-12-17 16:10:06 +010051 setbits_le32(priv->reg + CNTR_CTRL(id), CNTR_CTRL_ENABLE);
Marek Behún2b69a672018-04-24 17:21:30 +020052}
53
Marek Behún7b03e992018-12-17 16:10:06 +010054static void counter_disable(struct a37xx_wdt *priv, int id)
Marek Behún2b69a672018-04-24 17:21:30 +020055{
Marek Behún7b03e992018-12-17 16:10:06 +010056 clrbits_le32(priv->reg + CNTR_CTRL(id), CNTR_CTRL_ENABLE);
57}
Marek Behún2b69a672018-04-24 17:21:30 +020058
Marek Behún7b03e992018-12-17 16:10:06 +010059static int init_counter(struct a37xx_wdt *priv, int id, u32 mode, u32 trig_src)
60{
61 u32 reg;
62
63 reg = readl(priv->reg + CNTR_CTRL(id));
64 if (reg & CNTR_CTRL_ACTIVE)
65 return -EBUSY;
66
67 reg &= ~(CNTR_CTRL_MODE_MASK | CNTR_CTRL_PRESCALE_MASK |
68 CNTR_CTRL_TRIG_SRC_MASK);
69
70 /* set mode */
71 reg |= mode;
72
73 /* set prescaler to the min value */
74 reg |= CNTR_CTRL_PRESCALE_MIN << CNTR_CTRL_PRESCALE_SHIFT;
75
76 /* set trigger source */
77 reg |= trig_src;
78
79 writel(reg, priv->reg + CNTR_CTRL(id));
80
81 return 0;
Marek Behún2b69a672018-04-24 17:21:30 +020082}
83
84static int a37xx_wdt_reset(struct udevice *dev)
85{
86 struct a37xx_wdt *priv = dev_get_priv(dev);
87
88 if (!priv->timeout)
89 return -EINVAL;
90
Marek Behún7b03e992018-12-17 16:10:06 +010091 /* counter 1 is retriggered by forcing end count on counter 0 */
92 counter_disable(priv, 0);
93 counter_enable(priv, 0);
Marek Behún2b69a672018-04-24 17:21:30 +020094
95 return 0;
96}
97
98static int a37xx_wdt_expire_now(struct udevice *dev, ulong flags)
99{
100 struct a37xx_wdt *priv = dev_get_priv(dev);
101
Marek Behún7b03e992018-12-17 16:10:06 +0100102 /* first we set timeout to 0 */
103 counter_disable(priv, 1);
104 set_counter_value(priv, 1, 0);
105 counter_enable(priv, 1);
106
107 /* and then we start counter 1 by forcing end count on counter 0 */
108 counter_disable(priv, 0);
109 counter_enable(priv, 0);
Marek Behún2b69a672018-04-24 17:21:30 +0200110
111 return 0;
112}
113
114static int a37xx_wdt_start(struct udevice *dev, u64 ms, ulong flags)
115{
116 struct a37xx_wdt *priv = dev_get_priv(dev);
Marek Behún7b03e992018-12-17 16:10:06 +0100117 int err;
Marek Behún2b69a672018-04-24 17:21:30 +0200118
Marek Behún7b03e992018-12-17 16:10:06 +0100119 err = init_counter(priv, 0, CNTR_CTRL_MODE_ONESHOT, 0);
120 if (err < 0)
121 return err;
Marek Behún2b69a672018-04-24 17:21:30 +0200122
Marek Behún7b03e992018-12-17 16:10:06 +0100123 err = init_counter(priv, 1, CNTR_CTRL_MODE_HWSIG,
124 CNTR_CTRL_TRIG_SRC_PREV_CNTR);
125 if (err < 0)
126 return err;
Marek Behún2b69a672018-04-24 17:21:30 +0200127
128 priv->timeout = ms * priv->clk_rate / 1000 / CNTR_CTRL_PRESCALE_MIN;
129
Marek Behún7b03e992018-12-17 16:10:06 +0100130 set_counter_value(priv, 0, 0);
131 set_counter_value(priv, 1, priv->timeout);
132 counter_enable(priv, 1);
Marek Behún2b69a672018-04-24 17:21:30 +0200133
Marek Behún7b03e992018-12-17 16:10:06 +0100134 /* we have to force end count on counter 0 to start counter 1 */
135 counter_enable(priv, 0);
Marek Behún2b69a672018-04-24 17:21:30 +0200136
137 return 0;
138}
139
140static int a37xx_wdt_stop(struct udevice *dev)
141{
142 struct a37xx_wdt *priv = dev_get_priv(dev);
143
Marek Behún7b03e992018-12-17 16:10:06 +0100144 counter_disable(priv, 1);
145 counter_disable(priv, 0);
146 writel(0, priv->sel_reg);
Marek Behún2b69a672018-04-24 17:21:30 +0200147
148 return 0;
149}
150
151static int a37xx_wdt_probe(struct udevice *dev)
152{
153 struct a37xx_wdt *priv = dev_get_priv(dev);
154 fdt_addr_t addr;
155
156 addr = dev_read_addr_index(dev, 0);
157 if (addr == FDT_ADDR_T_NONE)
158 goto err;
159 priv->sel_reg = (void __iomem *)addr;
160
161 addr = dev_read_addr_index(dev, 1);
162 if (addr == FDT_ADDR_T_NONE)
163 goto err;
164 priv->reg = (void __iomem *)addr;
165
166 priv->clk_rate = (ulong)get_ref_clk() * 1000000;
167
Marek Behún2b69a672018-04-24 17:21:30 +0200168 /*
Marek Behún7b03e992018-12-17 16:10:06 +0100169 * We use counter 1 as watchdog timer, therefore we only set bit
170 * TIMER1_IS_WCHDOG_TIMER. Counter 0 is only used to force re-trigger on
171 * counter 1.
Marek Behún2b69a672018-04-24 17:21:30 +0200172 */
173 writel(1 << 1, priv->sel_reg);
174
175 return 0;
176err:
177 dev_err(dev, "no io address\n");
178 return -ENODEV;
179}
180
181static const struct wdt_ops a37xx_wdt_ops = {
182 .start = a37xx_wdt_start,
183 .reset = a37xx_wdt_reset,
184 .stop = a37xx_wdt_stop,
185 .expire_now = a37xx_wdt_expire_now,
186};
187
188static const struct udevice_id a37xx_wdt_ids[] = {
189 { .compatible = "marvell,armada-3700-wdt" },
190 {}
191};
192
193U_BOOT_DRIVER(a37xx_wdt) = {
194 .name = "armada_37xx_wdt",
195 .id = UCLASS_WDT,
196 .of_match = a37xx_wdt_ids,
197 .probe = a37xx_wdt_probe,
198 .priv_auto_alloc_size = sizeof(struct a37xx_wdt),
199 .ops = &a37xx_wdt_ops,
200};