blob: 41eff1a464505609bda2f7344ff4abd18bd5a653 [file] [log] [blame]
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -06001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Xilinx window watchdog timer driver.
4 *
Michal Simeka8c94362023-07-10 14:35:49 +02005 * Author(s): Michal Simek <michal.simek@amd.com>
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +05306 * Ashok Reddy Soma <ashok.reddy.soma@xilinx.com>
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -06007 *
8 * Copyright (c) 2020, Xilinx Inc.
9 */
10
11#include <clk.h>
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060012#include <dm.h>
13#include <regmap.h>
14#include <wdt.h>
15#include <linux/compat.h>
Ashok Reddy Soma4ec630b2021-08-10 00:16:12 -060016#include <dm/device_compat.h>
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060017#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 Somadb1aa0d2021-09-28 11:31:58 +053025/* 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 Somaf4aecf42020-03-11 03:06:04 -060041
42struct xlnx_wwdt_priv {
43 bool enable_once;
44 struct regmap *regs;
45 struct clk clk;
46};
47
Simon Glassb75b15b2020-12-03 16:55:23 -070048struct xlnx_wwdt_plat {
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060049 bool enable_once;
50};
51
52static int xlnx_wwdt_reset(struct udevice *dev)
53{
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053054 u32 esr;
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060055 struct xlnx_wwdt_priv *wdt = dev_get_priv(dev);
56
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053057 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 Somaf4aecf42020-03-11 03:06:04 -060065
66 return 0;
67}
68
69static int xlnx_wwdt_stop(struct udevice *dev)
70{
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060071 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 Somadb1aa0d2021-09-28 11:31:58 +053078 /* 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 Somaf4aecf42020-03-11 03:06:04 -060081
82 clk_disable(&wdt->clk);
83
84 dev_dbg(dev, "Watchdog disabled!\n");
85
86 return 0;
87}
88
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053089static int xlnx_wwdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060090{
91 int ret;
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053092 u32 esr;
93 u64 count, timeout;
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060094 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 Somadb1aa0d2021-09-28 11:31:58 +0530105 /* Convert timeout from msec to sec */
106 timeout = timeout_ms / 1000;
107
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600108 /* Calculate timeout count */
109 count = timeout * clock_f;
110
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +0530111 /* 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 Somaf4aecf42020-03-11 03:06:04 -0600123 ret = clk_enable(&wdt->clk);
Michal Simek41710952021-02-09 15:28:15 +0100124 if (ret) {
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600125 dev_err(dev, "failed to enable clock\n");
126 return ret;
127 }
128
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +0530129 /* 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 Somaf4aecf42020-03-11 03:06:04 -0600132
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +0530133 /* 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 Somaf4aecf42020-03-11 03:06:04 -0600137
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +0530138 /* 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 Somaf4aecf42020-03-11 03:06:04 -0600142
143 return 0;
144}
145
Ashok Reddy Somad630edd2021-09-28 11:31:59 +0530146static 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 Somaf4aecf42020-03-11 03:06:04 -0600151static int xlnx_wwdt_probe(struct udevice *dev)
152{
153 int ret;
Simon Glassb75b15b2020-12-03 16:55:23 -0700154 struct xlnx_wwdt_plat *plat = dev_get_plat(dev);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600155 struct xlnx_wwdt_priv *wdt = dev_get_priv(dev);
156
Simon Glass75e534b2020-12-16 21:20:07 -0700157 dev_dbg(dev, "%s: Probing wdt%u\n", __func__, dev_seq(dev));
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600158
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 Glass71fa5b42020-12-03 16:55:18 -0700165 wdt->enable_once = plat->enable_once;
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600166
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 Glassaad29ae2020-12-03 16:55:21 -0700174static int xlnx_wwdt_of_to_plat(struct udevice *dev)
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600175{
Simon Glassb75b15b2020-12-03 16:55:23 -0700176 struct xlnx_wwdt_plat *plat = dev_get_plat(dev);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600177
Simon Glass71fa5b42020-12-03 16:55:18 -0700178 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 Somaf4aecf42020-03-11 03:06:04 -0600181
182 return 0;
183}
184
185static const struct wdt_ops xlnx_wwdt_ops = {
186 .start = xlnx_wwdt_start,
187 .reset = xlnx_wwdt_reset,
188 .stop = xlnx_wwdt_stop,
Ashok Reddy Somad630edd2021-09-28 11:31:59 +0530189 .expire_now = xlnx_wwdt_expire_now,
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600190};
191
192static const struct udevice_id xlnx_wwdt_ids[] = {
Michal Simek8a351c32023-06-13 13:22:26 +0200193 { .compatible = "xlnx,versal-wwdt", },
194 { .compatible = "xlnx,versal-wwdt-1.0", }, /* deprecated */
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600195 {},
196};
197
198U_BOOT_DRIVER(xlnx_wwdt) = {
199 .name = "xlnx_wwdt",
200 .id = UCLASS_WDT,
201 .of_match = xlnx_wwdt_ids,
202 .probe = xlnx_wwdt_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700203 .priv_auto = sizeof(struct xlnx_wwdt_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700204 .plat_auto = sizeof(struct xlnx_wwdt_plat),
Simon Glassaad29ae2020-12-03 16:55:21 -0700205 .of_to_plat = xlnx_wwdt_of_to_plat,
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600206 .ops = &xlnx_wwdt_ops,
207};