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> |
| 11 | #include <asm/io.h> |
| 12 | #include <asm/arch/cpu.h> |
| 13 | #include <asm/arch/soc.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | struct a37xx_wdt { |
| 19 | void __iomem *sel_reg; |
| 20 | void __iomem *reg; |
| 21 | ulong clk_rate; |
| 22 | u64 timeout; |
| 23 | }; |
| 24 | |
| 25 | /* |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 26 | * 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] | 27 | */ |
| 28 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 29 | #define CNTR_CTRL(id) ((id) * 0x10) |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 30 | #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ún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 34 | #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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 37 | #define CNTR_CTRL_PRESCALE_MASK 0xff00 |
| 38 | #define CNTR_CTRL_PRESCALE_MIN 2 |
| 39 | #define CNTR_CTRL_PRESCALE_SHIFT 8 |
| 40 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 41 | #define CNTR_COUNT_LOW(id) (CNTR_CTRL(id) + 0x4) |
| 42 | #define CNTR_COUNT_HIGH(id) (CNTR_CTRL(id) + 0x8) |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 43 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 44 | 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] | 45 | { |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 46 | writel(val & 0xffffffff, priv->reg + CNTR_COUNT_LOW(id)); |
| 47 | writel(val >> 32, priv->reg + CNTR_COUNT_HIGH(id)); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 50 | static void counter_enable(struct a37xx_wdt *priv, int id) |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 51 | { |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 52 | setbits_le32(priv->reg + CNTR_CTRL(id), CNTR_CTRL_ENABLE); |
| 53 | } |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 54 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 55 | static void counter_disable(struct a37xx_wdt *priv, int id) |
| 56 | { |
| 57 | clrbits_le32(priv->reg + CNTR_CTRL(id), CNTR_CTRL_ENABLE); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 60 | 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] | 61 | { |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 62 | 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 70 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 71 | /* 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static 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ún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 92 | /* counter 1 is retriggered by forcing end count on counter 0 */ |
| 93 | counter_disable(priv, 0); |
| 94 | counter_enable(priv, 0); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int a37xx_wdt_expire_now(struct udevice *dev, ulong flags) |
| 100 | { |
| 101 | struct a37xx_wdt *priv = dev_get_priv(dev); |
| 102 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 103 | /* 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int a37xx_wdt_start(struct udevice *dev, u64 ms, ulong flags) |
| 116 | { |
| 117 | struct a37xx_wdt *priv = dev_get_priv(dev); |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 118 | int err; |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 119 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 120 | err = init_counter(priv, 0, CNTR_CTRL_MODE_ONESHOT, 0); |
| 121 | if (err < 0) |
| 122 | return err; |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 123 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 124 | 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 128 | |
| 129 | priv->timeout = ms * priv->clk_rate / 1000 / CNTR_CTRL_PRESCALE_MIN; |
| 130 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 131 | set_counter_value(priv, 0, 0); |
| 132 | set_counter_value(priv, 1, priv->timeout); |
| 133 | counter_enable(priv, 1); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 134 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 135 | /* we have to force end count on counter 0 to start counter 1 */ |
| 136 | counter_enable(priv, 0); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int a37xx_wdt_stop(struct udevice *dev) |
| 142 | { |
| 143 | struct a37xx_wdt *priv = dev_get_priv(dev); |
| 144 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 145 | counter_disable(priv, 1); |
| 146 | counter_disable(priv, 0); |
| 147 | writel(0, priv->sel_reg); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 169 | /* |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 170 | * 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 173 | */ |
| 174 | writel(1 << 1, priv->sel_reg); |
| 175 | |
| 176 | return 0; |
| 177 | err: |
| 178 | dev_err(dev, "no io address\n"); |
| 179 | return -ENODEV; |
| 180 | } |
| 181 | |
| 182 | static 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 | |
| 189 | static const struct udevice_id a37xx_wdt_ids[] = { |
| 190 | { .compatible = "marvell,armada-3700-wdt" }, |
| 191 | {} |
| 192 | }; |
| 193 | |
| 194 | U_BOOT_DRIVER(a37xx_wdt) = { |
| 195 | .name = "armada_37xx_wdt", |
| 196 | .id = UCLASS_WDT, |
| 197 | .of_match = a37xx_wdt_ids, |
| 198 | .probe = a37xx_wdt_probe, |
| 199 | .priv_auto_alloc_size = sizeof(struct a37xx_wdt), |
| 200 | .ops = &a37xx_wdt_ops, |
| 201 | }; |