Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Xilinx window watchdog timer driver. |
| 4 | * |
Michal Simek | a8c9436 | 2023-07-10 14:35:49 +0200 | [diff] [blame] | 5 | * Author(s): Michal Simek <michal.simek@amd.com> |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 6 | * Ashok Reddy Soma <ashok.reddy.soma@xilinx.com> |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 7 | * |
| 8 | * Copyright (c) 2020, Xilinx Inc. |
| 9 | */ |
| 10 | |
| 11 | #include <clk.h> |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <regmap.h> |
| 14 | #include <wdt.h> |
| 15 | #include <linux/compat.h> |
Ashok Reddy Soma | 4ec630b | 2021-08-10 00:16:12 -0600 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | |
| 19 | /* Refresh Register Masks */ |
| 20 | #define XWT_WWREF_GWRR_MASK BIT(0) /* Refresh and start new period */ |
| 21 | |
| 22 | /* Generic Control/Status Register Masks */ |
| 23 | #define XWT_WWCSR_GWEN_MASK BIT(0) /* Enable Bit */ |
| 24 | |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 25 | /* Register offsets for the WWDT device */ |
| 26 | #define XWT_WWDT_MWR_OFFSET 0x00 |
| 27 | #define XWT_WWDT_ESR_OFFSET 0x04 |
| 28 | #define XWT_WWDT_FCR_OFFSET 0x08 |
| 29 | #define XWT_WWDT_FWR_OFFSET 0x0c |
| 30 | #define XWT_WWDT_SWR_OFFSET 0x10 |
| 31 | #define XWT_WWDT_CNT_MIN 1 |
| 32 | #define XWT_WWDT_CNT_MAX 0xffffffff |
| 33 | |
| 34 | /* Master Write Control Register Masks */ |
| 35 | #define XWT_WWDT_MWR_MASK BIT(0) |
| 36 | |
| 37 | /* Enable and Status Register Masks */ |
| 38 | #define XWT_WWDT_ESR_WINT_MASK BIT(16) |
| 39 | #define XWT_WWDT_ESR_WSW_MASK BIT(8) |
| 40 | #define XWT_WWDT_ESR_WEN_MASK BIT(0) |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 41 | |
| 42 | struct xlnx_wwdt_priv { |
| 43 | bool enable_once; |
| 44 | struct regmap *regs; |
| 45 | struct clk clk; |
| 46 | }; |
| 47 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 48 | struct xlnx_wwdt_plat { |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 49 | bool enable_once; |
| 50 | }; |
| 51 | |
| 52 | static int xlnx_wwdt_reset(struct udevice *dev) |
| 53 | { |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 54 | u32 esr; |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 55 | struct xlnx_wwdt_priv *wdt = dev_get_priv(dev); |
| 56 | |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 57 | regmap_write(wdt->regs, XWT_WWDT_MWR_OFFSET, XWT_WWDT_MWR_MASK); |
| 58 | regmap_read(wdt->regs, XWT_WWDT_ESR_OFFSET, &esr); |
| 59 | esr |= XWT_WWDT_ESR_WINT_MASK; |
| 60 | esr &= ~XWT_WWDT_ESR_WSW_MASK; |
| 61 | regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, esr); |
| 62 | regmap_read(wdt->regs, XWT_WWDT_ESR_OFFSET, &esr); |
| 63 | esr |= XWT_WWDT_ESR_WSW_MASK; |
| 64 | regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, esr); |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int xlnx_wwdt_stop(struct udevice *dev) |
| 70 | { |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 71 | struct xlnx_wwdt_priv *wdt = dev_get_priv(dev); |
| 72 | |
| 73 | if (wdt->enable_once) { |
| 74 | dev_warn(dev, "Can't stop Xilinx watchdog.\n"); |
| 75 | return -EBUSY; |
| 76 | } |
| 77 | |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 78 | /* Disable the window watchdog timer */ |
| 79 | regmap_write(wdt->regs, XWT_WWDT_MWR_OFFSET, XWT_WWDT_MWR_MASK); |
| 80 | regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, ~(u32)XWT_WWDT_ESR_WEN_MASK); |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 81 | |
| 82 | clk_disable(&wdt->clk); |
| 83 | |
| 84 | dev_dbg(dev, "Watchdog disabled!\n"); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 89 | static int xlnx_wwdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 90 | { |
| 91 | int ret; |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 92 | u32 esr; |
| 93 | u64 count, timeout; |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 94 | unsigned long clock_f; |
| 95 | struct xlnx_wwdt_priv *wdt = dev_get_priv(dev); |
| 96 | |
| 97 | clock_f = clk_get_rate(&wdt->clk); |
| 98 | if (IS_ERR_VALUE(clock_f)) { |
| 99 | dev_err(dev, "failed to get rate\n"); |
| 100 | return clock_f; |
| 101 | } |
| 102 | |
| 103 | dev_dbg(dev, "%s: CLK %ld\n", __func__, clock_f); |
| 104 | |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 105 | /* Convert timeout from msec to sec */ |
| 106 | timeout = timeout_ms / 1000; |
| 107 | |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 108 | /* Calculate timeout count */ |
| 109 | count = timeout * clock_f; |
| 110 | |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 111 | /* Count should be at least 1 */ |
| 112 | if (count < XWT_WWDT_CNT_MIN) { |
| 113 | debug("%s: watchdog won't fire with 0 ticks\n", __func__); |
| 114 | count = XWT_WWDT_CNT_MIN; |
| 115 | } |
| 116 | |
| 117 | /* Limit the count to maximum possible value */ |
| 118 | if (count > XWT_WWDT_CNT_MAX) { |
| 119 | debug("%s: maximum watchdog timeout exceeded\n", __func__); |
| 120 | count = XWT_WWDT_CNT_MAX; |
| 121 | } |
| 122 | |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 123 | ret = clk_enable(&wdt->clk); |
Michal Simek | 4171095 | 2021-02-09 15:28:15 +0100 | [diff] [blame] | 124 | if (ret) { |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 125 | dev_err(dev, "failed to enable clock\n"); |
| 126 | return ret; |
| 127 | } |
| 128 | |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 129 | /* Disable the window watchdog timer */ |
| 130 | regmap_write(wdt->regs, XWT_WWDT_MWR_OFFSET, XWT_WWDT_MWR_MASK); |
| 131 | regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, ~(u32)XWT_WWDT_ESR_WEN_MASK); |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 132 | |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 133 | /* Set first window and second window registers with timeout */ |
| 134 | regmap_write(wdt->regs, XWT_WWDT_FWR_OFFSET, 0); /* No pre-timeout */ |
| 135 | regmap_write(wdt->regs, XWT_WWDT_SWR_OFFSET, (u32)count); |
| 136 | regmap_write(wdt->regs, XWT_WWDT_FCR_OFFSET, 0); |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 137 | |
Ashok Reddy Soma | db1aa0d | 2021-09-28 11:31:58 +0530 | [diff] [blame] | 138 | /* Enable the window watchdog timer */ |
| 139 | regmap_read(wdt->regs, XWT_WWDT_ESR_OFFSET, &esr); |
| 140 | esr |= XWT_WWDT_ESR_WEN_MASK; |
| 141 | regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, esr); |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Ashok Reddy Soma | d630edd | 2021-09-28 11:31:59 +0530 | [diff] [blame] | 146 | static int xlnx_wwdt_expire_now(struct udevice *dev, ulong flags) |
| 147 | { |
| 148 | return xlnx_wwdt_start(dev, XWT_WWDT_CNT_MIN, flags); |
| 149 | } |
| 150 | |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 151 | static int xlnx_wwdt_probe(struct udevice *dev) |
| 152 | { |
| 153 | int ret; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 154 | struct xlnx_wwdt_plat *plat = dev_get_plat(dev); |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 155 | struct xlnx_wwdt_priv *wdt = dev_get_priv(dev); |
| 156 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 157 | dev_dbg(dev, "%s: Probing wdt%u\n", __func__, dev_seq(dev)); |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 158 | |
| 159 | ret = regmap_init_mem(dev_ofnode(dev), &wdt->regs); |
| 160 | if (ret) { |
| 161 | dev_dbg(dev, "failed to get regbase of wwdt\n"); |
| 162 | return ret; |
| 163 | } |
| 164 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 165 | wdt->enable_once = plat->enable_once; |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 166 | |
| 167 | ret = clk_get_by_index(dev, 0, &wdt->clk); |
| 168 | if (ret < 0) |
| 169 | dev_err(dev, "failed to get clock\n"); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 174 | static int xlnx_wwdt_of_to_plat(struct udevice *dev) |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 175 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 176 | struct xlnx_wwdt_plat *plat = dev_get_plat(dev); |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 177 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 178 | plat->enable_once = dev_read_u32_default(dev, "xlnx,wdt-enable-once", |
| 179 | 0); |
| 180 | dev_dbg(dev, "wdt-enable-once %d\n", plat->enable_once); |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static const struct wdt_ops xlnx_wwdt_ops = { |
| 186 | .start = xlnx_wwdt_start, |
| 187 | .reset = xlnx_wwdt_reset, |
| 188 | .stop = xlnx_wwdt_stop, |
Ashok Reddy Soma | d630edd | 2021-09-28 11:31:59 +0530 | [diff] [blame] | 189 | .expire_now = xlnx_wwdt_expire_now, |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static const struct udevice_id xlnx_wwdt_ids[] = { |
Michal Simek | 8a351c3 | 2023-06-13 13:22:26 +0200 | [diff] [blame] | 193 | { .compatible = "xlnx,versal-wwdt", }, |
| 194 | { .compatible = "xlnx,versal-wwdt-1.0", }, /* deprecated */ |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 195 | {}, |
| 196 | }; |
| 197 | |
| 198 | U_BOOT_DRIVER(xlnx_wwdt) = { |
| 199 | .name = "xlnx_wwdt", |
| 200 | .id = UCLASS_WDT, |
| 201 | .of_match = xlnx_wwdt_ids, |
| 202 | .probe = xlnx_wwdt_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 203 | .priv_auto = sizeof(struct xlnx_wwdt_priv), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 204 | .plat_auto = sizeof(struct xlnx_wwdt_plat), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 205 | .of_to_plat = xlnx_wwdt_of_to_plat, |
Ashok Reddy Soma | f4aecf4 | 2020-03-11 03:06:04 -0600 | [diff] [blame] | 206 | .ops = &xlnx_wwdt_ops, |
| 207 | }; |