blob: 4b51178e1b8c8e6cb92287ad9ac9c27f4c839271 [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 *
Marek Behúnd63726e2022-06-01 17:17:06 +02005 * Marek Behún <kabel@kernel.org>
Marek Behúna86b97d2018-04-24 17:21:30 +02006 */
7
Marek Behúna86b97d2018-04-24 17:21:30 +02008#include <dm.h>
9#include <wdt.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Marek Behúna86b97d2018-04-24 17:21:30 +020011#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
Pali Rohár33276e52022-02-23 14:21:40 +010060static void 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));
Marek Behúnae0ae012018-12-17 16:10:06 +010065
66 reg &= ~(CNTR_CTRL_MODE_MASK | CNTR_CTRL_PRESCALE_MASK |
67 CNTR_CTRL_TRIG_SRC_MASK);
Marek Behúna86b97d2018-04-24 17:21:30 +020068
Marek Behúnae0ae012018-12-17 16:10:06 +010069 /* set mode */
70 reg |= mode;
71
72 /* set prescaler to the min value */
73 reg |= CNTR_CTRL_PRESCALE_MIN << CNTR_CTRL_PRESCALE_SHIFT;
74
75 /* set trigger source */
76 reg |= trig_src;
77
78 writel(reg, priv->reg + CNTR_CTRL(id));
Marek Behúna86b97d2018-04-24 17:21:30 +020079}
80
81static int a37xx_wdt_reset(struct udevice *dev)
82{
83 struct a37xx_wdt *priv = dev_get_priv(dev);
84
85 if (!priv->timeout)
86 return -EINVAL;
87
Marek Behúnae0ae012018-12-17 16:10:06 +010088 /* counter 1 is retriggered by forcing end count on counter 0 */
89 counter_disable(priv, 0);
90 counter_enable(priv, 0);
Marek Behúna86b97d2018-04-24 17:21:30 +020091
92 return 0;
93}
94
95static int a37xx_wdt_expire_now(struct udevice *dev, ulong flags)
96{
97 struct a37xx_wdt *priv = dev_get_priv(dev);
98
Marek Behúnae0ae012018-12-17 16:10:06 +010099 /* first we set timeout to 0 */
100 counter_disable(priv, 1);
101 set_counter_value(priv, 1, 0);
102 counter_enable(priv, 1);
103
104 /* and then we start counter 1 by forcing end count on counter 0 */
105 counter_disable(priv, 0);
106 counter_enable(priv, 0);
Marek Behúna86b97d2018-04-24 17:21:30 +0200107
108 return 0;
109}
110
111static int a37xx_wdt_start(struct udevice *dev, u64 ms, ulong flags)
112{
113 struct a37xx_wdt *priv = dev_get_priv(dev);
Marek Behúna86b97d2018-04-24 17:21:30 +0200114
Pali Rohár33276e52022-02-23 14:21:40 +0100115 init_counter(priv, 0, CNTR_CTRL_MODE_ONESHOT, 0);
116 init_counter(priv, 1, CNTR_CTRL_MODE_HWSIG, CNTR_CTRL_TRIG_SRC_PREV_CNTR);
Marek Behúna86b97d2018-04-24 17:21:30 +0200117
118 priv->timeout = ms * priv->clk_rate / 1000 / CNTR_CTRL_PRESCALE_MIN;
119
Marek Behúnae0ae012018-12-17 16:10:06 +0100120 set_counter_value(priv, 0, 0);
121 set_counter_value(priv, 1, priv->timeout);
122 counter_enable(priv, 1);
Marek Behúna86b97d2018-04-24 17:21:30 +0200123
Marek Behúnae0ae012018-12-17 16:10:06 +0100124 /* we have to force end count on counter 0 to start counter 1 */
125 counter_enable(priv, 0);
Marek Behúna86b97d2018-04-24 17:21:30 +0200126
127 return 0;
128}
129
130static int a37xx_wdt_stop(struct udevice *dev)
131{
132 struct a37xx_wdt *priv = dev_get_priv(dev);
133
Marek Behúnae0ae012018-12-17 16:10:06 +0100134 counter_disable(priv, 1);
135 counter_disable(priv, 0);
136 writel(0, priv->sel_reg);
Marek Behúna86b97d2018-04-24 17:21:30 +0200137
138 return 0;
139}
140
141static int a37xx_wdt_probe(struct udevice *dev)
142{
143 struct a37xx_wdt *priv = dev_get_priv(dev);
144 fdt_addr_t addr;
145
Pali Rohár43691bb2022-02-14 11:34:25 +0100146 priv->sel_reg = (void __iomem *)MVEBU_REGISTER(0x0d064);
Marek Behúna86b97d2018-04-24 17:21:30 +0200147
Pali Rohár43691bb2022-02-14 11:34:25 +0100148 addr = dev_read_addr(dev);
Marek Behúna86b97d2018-04-24 17:21:30 +0200149 if (addr == FDT_ADDR_T_NONE)
150 goto err;
151 priv->reg = (void __iomem *)addr;
152
153 priv->clk_rate = (ulong)get_ref_clk() * 1000000;
154
Marek Behúna86b97d2018-04-24 17:21:30 +0200155 /*
Marek Behúnae0ae012018-12-17 16:10:06 +0100156 * We use counter 1 as watchdog timer, therefore we only set bit
157 * TIMER1_IS_WCHDOG_TIMER. Counter 0 is only used to force re-trigger on
158 * counter 1.
Marek Behúna86b97d2018-04-24 17:21:30 +0200159 */
160 writel(1 << 1, priv->sel_reg);
161
162 return 0;
163err:
164 dev_err(dev, "no io address\n");
165 return -ENODEV;
166}
167
168static const struct wdt_ops a37xx_wdt_ops = {
169 .start = a37xx_wdt_start,
170 .reset = a37xx_wdt_reset,
171 .stop = a37xx_wdt_stop,
172 .expire_now = a37xx_wdt_expire_now,
173};
174
175static const struct udevice_id a37xx_wdt_ids[] = {
176 { .compatible = "marvell,armada-3700-wdt" },
177 {}
178};
179
180U_BOOT_DRIVER(a37xx_wdt) = {
181 .name = "armada_37xx_wdt",
182 .id = UCLASS_WDT,
183 .of_match = a37xx_wdt_ids,
184 .probe = a37xx_wdt_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700185 .priv_auto = sizeof(struct a37xx_wdt),
Marek Behúna86b97d2018-04-24 17:21:30 +0200186 .ops = &a37xx_wdt_ops,
187};