Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 1 | // 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> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <asm/arch/cpu.h> |
| 14 | #include <asm/arch/soc.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | struct a37xx_wdt { |
| 20 | void __iomem *sel_reg; |
| 21 | void __iomem *reg; |
| 22 | ulong clk_rate; |
| 23 | u64 timeout; |
| 24 | }; |
| 25 | |
| 26 | /* |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 27 | * We use Counter 1 as watchdog timer, and Counter 0 for re-triggering Counter 1 |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 28 | */ |
| 29 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 30 | #define CNTR_CTRL(id) ((id) * 0x10) |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 31 | #define CNTR_CTRL_ENABLE 0x0001 |
| 32 | #define CNTR_CTRL_ACTIVE 0x0002 |
| 33 | #define CNTR_CTRL_MODE_MASK 0x000c |
| 34 | #define CNTR_CTRL_MODE_ONESHOT 0x0000 |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 35 | #define CNTR_CTRL_MODE_HWSIG 0x000c |
| 36 | #define CNTR_CTRL_TRIG_SRC_MASK 0x00f0 |
| 37 | #define CNTR_CTRL_TRIG_SRC_PREV_CNTR 0x0050 |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 38 | #define CNTR_CTRL_PRESCALE_MASK 0xff00 |
| 39 | #define CNTR_CTRL_PRESCALE_MIN 2 |
| 40 | #define CNTR_CTRL_PRESCALE_SHIFT 8 |
| 41 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 42 | #define CNTR_COUNT_LOW(id) (CNTR_CTRL(id) + 0x4) |
| 43 | #define CNTR_COUNT_HIGH(id) (CNTR_CTRL(id) + 0x8) |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 44 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 45 | static void set_counter_value(struct a37xx_wdt *priv, int id, u64 val) |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 46 | { |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 47 | writel(val & 0xffffffff, priv->reg + CNTR_COUNT_LOW(id)); |
| 48 | writel(val >> 32, priv->reg + CNTR_COUNT_HIGH(id)); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 51 | static void counter_enable(struct a37xx_wdt *priv, int id) |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 52 | { |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 53 | setbits_le32(priv->reg + CNTR_CTRL(id), CNTR_CTRL_ENABLE); |
| 54 | } |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 55 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 56 | static void counter_disable(struct a37xx_wdt *priv, int id) |
| 57 | { |
| 58 | clrbits_le32(priv->reg + CNTR_CTRL(id), CNTR_CTRL_ENABLE); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 61 | static int init_counter(struct a37xx_wdt *priv, int id, u32 mode, u32 trig_src) |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 62 | { |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 63 | u32 reg; |
| 64 | |
| 65 | reg = readl(priv->reg + CNTR_CTRL(id)); |
| 66 | if (reg & CNTR_CTRL_ACTIVE) |
| 67 | return -EBUSY; |
| 68 | |
| 69 | reg &= ~(CNTR_CTRL_MODE_MASK | CNTR_CTRL_PRESCALE_MASK | |
| 70 | CNTR_CTRL_TRIG_SRC_MASK); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 71 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 72 | /* set mode */ |
| 73 | reg |= mode; |
| 74 | |
| 75 | /* set prescaler to the min value */ |
| 76 | reg |= CNTR_CTRL_PRESCALE_MIN << CNTR_CTRL_PRESCALE_SHIFT; |
| 77 | |
| 78 | /* set trigger source */ |
| 79 | reg |= trig_src; |
| 80 | |
| 81 | writel(reg, priv->reg + CNTR_CTRL(id)); |
| 82 | |
| 83 | return 0; |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static int a37xx_wdt_reset(struct udevice *dev) |
| 87 | { |
| 88 | struct a37xx_wdt *priv = dev_get_priv(dev); |
| 89 | |
| 90 | if (!priv->timeout) |
| 91 | return -EINVAL; |
| 92 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 93 | /* counter 1 is retriggered by forcing end count on counter 0 */ |
| 94 | counter_disable(priv, 0); |
| 95 | counter_enable(priv, 0); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int a37xx_wdt_expire_now(struct udevice *dev, ulong flags) |
| 101 | { |
| 102 | struct a37xx_wdt *priv = dev_get_priv(dev); |
| 103 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 104 | /* first we set timeout to 0 */ |
| 105 | counter_disable(priv, 1); |
| 106 | set_counter_value(priv, 1, 0); |
| 107 | counter_enable(priv, 1); |
| 108 | |
| 109 | /* and then we start counter 1 by forcing end count on counter 0 */ |
| 110 | counter_disable(priv, 0); |
| 111 | counter_enable(priv, 0); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int a37xx_wdt_start(struct udevice *dev, u64 ms, ulong flags) |
| 117 | { |
| 118 | struct a37xx_wdt *priv = dev_get_priv(dev); |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 119 | int err; |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 120 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 121 | err = init_counter(priv, 0, CNTR_CTRL_MODE_ONESHOT, 0); |
| 122 | if (err < 0) |
| 123 | return err; |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 124 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 125 | err = init_counter(priv, 1, CNTR_CTRL_MODE_HWSIG, |
| 126 | CNTR_CTRL_TRIG_SRC_PREV_CNTR); |
| 127 | if (err < 0) |
| 128 | return err; |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 129 | |
| 130 | priv->timeout = ms * priv->clk_rate / 1000 / CNTR_CTRL_PRESCALE_MIN; |
| 131 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 132 | set_counter_value(priv, 0, 0); |
| 133 | set_counter_value(priv, 1, priv->timeout); |
| 134 | counter_enable(priv, 1); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 135 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 136 | /* we have to force end count on counter 0 to start counter 1 */ |
| 137 | counter_enable(priv, 0); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int a37xx_wdt_stop(struct udevice *dev) |
| 143 | { |
| 144 | struct a37xx_wdt *priv = dev_get_priv(dev); |
| 145 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 146 | counter_disable(priv, 1); |
| 147 | counter_disable(priv, 0); |
| 148 | writel(0, priv->sel_reg); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int a37xx_wdt_probe(struct udevice *dev) |
| 154 | { |
| 155 | struct a37xx_wdt *priv = dev_get_priv(dev); |
| 156 | fdt_addr_t addr; |
| 157 | |
| 158 | addr = dev_read_addr_index(dev, 0); |
| 159 | if (addr == FDT_ADDR_T_NONE) |
| 160 | goto err; |
| 161 | priv->sel_reg = (void __iomem *)addr; |
| 162 | |
| 163 | addr = dev_read_addr_index(dev, 1); |
| 164 | if (addr == FDT_ADDR_T_NONE) |
| 165 | goto err; |
| 166 | priv->reg = (void __iomem *)addr; |
| 167 | |
| 168 | priv->clk_rate = (ulong)get_ref_clk() * 1000000; |
| 169 | |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 170 | /* |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 171 | * We use counter 1 as watchdog timer, therefore we only set bit |
| 172 | * TIMER1_IS_WCHDOG_TIMER. Counter 0 is only used to force re-trigger on |
| 173 | * counter 1. |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 174 | */ |
| 175 | writel(1 << 1, priv->sel_reg); |
| 176 | |
| 177 | return 0; |
| 178 | err: |
| 179 | dev_err(dev, "no io address\n"); |
| 180 | return -ENODEV; |
| 181 | } |
| 182 | |
| 183 | static const struct wdt_ops a37xx_wdt_ops = { |
| 184 | .start = a37xx_wdt_start, |
| 185 | .reset = a37xx_wdt_reset, |
| 186 | .stop = a37xx_wdt_stop, |
| 187 | .expire_now = a37xx_wdt_expire_now, |
| 188 | }; |
| 189 | |
| 190 | static const struct udevice_id a37xx_wdt_ids[] = { |
| 191 | { .compatible = "marvell,armada-3700-wdt" }, |
| 192 | {} |
| 193 | }; |
| 194 | |
| 195 | U_BOOT_DRIVER(a37xx_wdt) = { |
| 196 | .name = "armada_37xx_wdt", |
| 197 | .id = UCLASS_WDT, |
| 198 | .of_match = a37xx_wdt_ids, |
| 199 | .probe = a37xx_wdt_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 200 | .priv_auto = sizeof(struct a37xx_wdt), |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 201 | .ops = &a37xx_wdt_ops, |
| 202 | }; |