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 | * |
Marek Behún | d63726e | 2022-06-01 17:17:06 +0200 | [diff] [blame] | 5 | * Marek Behún <kabel@kernel.org> |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <wdt.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 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 | |
Pali Rohár | 33276e5 | 2022-02-23 14:21:40 +0100 | [diff] [blame] | 60 | static void 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)); |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 65 | |
| 66 | reg &= ~(CNTR_CTRL_MODE_MASK | CNTR_CTRL_PRESCALE_MASK | |
| 67 | CNTR_CTRL_TRIG_SRC_MASK); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 68 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 69 | /* 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static 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ún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 88 | /* counter 1 is retriggered by forcing end count on counter 0 */ |
| 89 | counter_disable(priv, 0); |
| 90 | counter_enable(priv, 0); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int a37xx_wdt_expire_now(struct udevice *dev, ulong flags) |
| 96 | { |
| 97 | struct a37xx_wdt *priv = dev_get_priv(dev); |
| 98 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 99 | /* 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int a37xx_wdt_start(struct udevice *dev, u64 ms, ulong flags) |
| 112 | { |
| 113 | struct a37xx_wdt *priv = dev_get_priv(dev); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 114 | |
Pali Rohár | 33276e5 | 2022-02-23 14:21:40 +0100 | [diff] [blame] | 115 | 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 117 | |
| 118 | priv->timeout = ms * priv->clk_rate / 1000 / CNTR_CTRL_PRESCALE_MIN; |
| 119 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 120 | set_counter_value(priv, 0, 0); |
| 121 | set_counter_value(priv, 1, priv->timeout); |
| 122 | counter_enable(priv, 1); |
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 | /* we have to force end count on counter 0 to start counter 1 */ |
| 125 | counter_enable(priv, 0); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int a37xx_wdt_stop(struct udevice *dev) |
| 131 | { |
| 132 | struct a37xx_wdt *priv = dev_get_priv(dev); |
| 133 | |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 134 | counter_disable(priv, 1); |
| 135 | counter_disable(priv, 0); |
| 136 | writel(0, priv->sel_reg); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static 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ár | 43691bb | 2022-02-14 11:34:25 +0100 | [diff] [blame] | 146 | priv->sel_reg = (void __iomem *)MVEBU_REGISTER(0x0d064); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 147 | |
Pali Rohár | 43691bb | 2022-02-14 11:34:25 +0100 | [diff] [blame] | 148 | addr = dev_read_addr(dev); |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 149 | 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 155 | /* |
Marek Behún | ae0ae01 | 2018-12-17 16:10:06 +0100 | [diff] [blame] | 156 | * 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ún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 159 | */ |
| 160 | writel(1 << 1, priv->sel_reg); |
| 161 | |
| 162 | return 0; |
| 163 | err: |
| 164 | dev_err(dev, "no io address\n"); |
| 165 | return -ENODEV; |
| 166 | } |
| 167 | |
| 168 | static 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 | |
| 175 | static const struct udevice_id a37xx_wdt_ids[] = { |
| 176 | { .compatible = "marvell,armada-3700-wdt" }, |
| 177 | {} |
| 178 | }; |
| 179 | |
| 180 | U_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 Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 185 | .priv_auto = sizeof(struct a37xx_wdt), |
Marek Behún | a86b97d | 2018-04-24 17:21:30 +0200 | [diff] [blame] | 186 | .ops = &a37xx_wdt_ops, |
| 187 | }; |