blob: 963ab22fb452898507f2e13a3ba19847461983fa [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>
12#include <common.h>
13#include <dm.h>
14#include <regmap.h>
15#include <wdt.h>
16#include <linux/compat.h>
Ashok Reddy Soma4ec630b2021-08-10 00:16:12 -060017#include <dm/device_compat.h>
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060018#include <linux/io.h>
19
20/* Refresh Register Masks */
21#define XWT_WWREF_GWRR_MASK BIT(0) /* Refresh and start new period */
22
23/* Generic Control/Status Register Masks */
24#define XWT_WWCSR_GWEN_MASK BIT(0) /* Enable Bit */
25
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053026/* Register offsets for the WWDT device */
27#define XWT_WWDT_MWR_OFFSET 0x00
28#define XWT_WWDT_ESR_OFFSET 0x04
29#define XWT_WWDT_FCR_OFFSET 0x08
30#define XWT_WWDT_FWR_OFFSET 0x0c
31#define XWT_WWDT_SWR_OFFSET 0x10
32#define XWT_WWDT_CNT_MIN 1
33#define XWT_WWDT_CNT_MAX 0xffffffff
34
35/* Master Write Control Register Masks */
36#define XWT_WWDT_MWR_MASK BIT(0)
37
38/* Enable and Status Register Masks */
39#define XWT_WWDT_ESR_WINT_MASK BIT(16)
40#define XWT_WWDT_ESR_WSW_MASK BIT(8)
41#define XWT_WWDT_ESR_WEN_MASK BIT(0)
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060042
43struct xlnx_wwdt_priv {
44 bool enable_once;
45 struct regmap *regs;
46 struct clk clk;
47};
48
Simon Glassb75b15b2020-12-03 16:55:23 -070049struct xlnx_wwdt_plat {
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060050 bool enable_once;
51};
52
53static int xlnx_wwdt_reset(struct udevice *dev)
54{
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053055 u32 esr;
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060056 struct xlnx_wwdt_priv *wdt = dev_get_priv(dev);
57
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053058 regmap_write(wdt->regs, XWT_WWDT_MWR_OFFSET, XWT_WWDT_MWR_MASK);
59 regmap_read(wdt->regs, XWT_WWDT_ESR_OFFSET, &esr);
60 esr |= XWT_WWDT_ESR_WINT_MASK;
61 esr &= ~XWT_WWDT_ESR_WSW_MASK;
62 regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, esr);
63 regmap_read(wdt->regs, XWT_WWDT_ESR_OFFSET, &esr);
64 esr |= XWT_WWDT_ESR_WSW_MASK;
65 regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, esr);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060066
67 return 0;
68}
69
70static int xlnx_wwdt_stop(struct udevice *dev)
71{
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060072 struct xlnx_wwdt_priv *wdt = dev_get_priv(dev);
73
74 if (wdt->enable_once) {
75 dev_warn(dev, "Can't stop Xilinx watchdog.\n");
76 return -EBUSY;
77 }
78
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053079 /* Disable the window watchdog timer */
80 regmap_write(wdt->regs, XWT_WWDT_MWR_OFFSET, XWT_WWDT_MWR_MASK);
81 regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, ~(u32)XWT_WWDT_ESR_WEN_MASK);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060082
83 clk_disable(&wdt->clk);
84
85 dev_dbg(dev, "Watchdog disabled!\n");
86
87 return 0;
88}
89
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053090static int xlnx_wwdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060091{
92 int ret;
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +053093 u32 esr;
94 u64 count, timeout;
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -060095 unsigned long clock_f;
96 struct xlnx_wwdt_priv *wdt = dev_get_priv(dev);
97
98 clock_f = clk_get_rate(&wdt->clk);
99 if (IS_ERR_VALUE(clock_f)) {
100 dev_err(dev, "failed to get rate\n");
101 return clock_f;
102 }
103
104 dev_dbg(dev, "%s: CLK %ld\n", __func__, clock_f);
105
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +0530106 /* Convert timeout from msec to sec */
107 timeout = timeout_ms / 1000;
108
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600109 /* Calculate timeout count */
110 count = timeout * clock_f;
111
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +0530112 /* Count should be at least 1 */
113 if (count < XWT_WWDT_CNT_MIN) {
114 debug("%s: watchdog won't fire with 0 ticks\n", __func__);
115 count = XWT_WWDT_CNT_MIN;
116 }
117
118 /* Limit the count to maximum possible value */
119 if (count > XWT_WWDT_CNT_MAX) {
120 debug("%s: maximum watchdog timeout exceeded\n", __func__);
121 count = XWT_WWDT_CNT_MAX;
122 }
123
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600124 ret = clk_enable(&wdt->clk);
Michal Simek41710952021-02-09 15:28:15 +0100125 if (ret) {
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600126 dev_err(dev, "failed to enable clock\n");
127 return ret;
128 }
129
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +0530130 /* Disable the window watchdog timer */
131 regmap_write(wdt->regs, XWT_WWDT_MWR_OFFSET, XWT_WWDT_MWR_MASK);
132 regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, ~(u32)XWT_WWDT_ESR_WEN_MASK);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600133
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +0530134 /* Set first window and second window registers with timeout */
135 regmap_write(wdt->regs, XWT_WWDT_FWR_OFFSET, 0); /* No pre-timeout */
136 regmap_write(wdt->regs, XWT_WWDT_SWR_OFFSET, (u32)count);
137 regmap_write(wdt->regs, XWT_WWDT_FCR_OFFSET, 0);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600138
Ashok Reddy Somadb1aa0d2021-09-28 11:31:58 +0530139 /* Enable the window watchdog timer */
140 regmap_read(wdt->regs, XWT_WWDT_ESR_OFFSET, &esr);
141 esr |= XWT_WWDT_ESR_WEN_MASK;
142 regmap_write(wdt->regs, XWT_WWDT_ESR_OFFSET, esr);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600143
144 return 0;
145}
146
Ashok Reddy Somad630edd2021-09-28 11:31:59 +0530147static int xlnx_wwdt_expire_now(struct udevice *dev, ulong flags)
148{
149 return xlnx_wwdt_start(dev, XWT_WWDT_CNT_MIN, flags);
150}
151
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600152static int xlnx_wwdt_probe(struct udevice *dev)
153{
154 int ret;
Simon Glassb75b15b2020-12-03 16:55:23 -0700155 struct xlnx_wwdt_plat *plat = dev_get_plat(dev);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600156 struct xlnx_wwdt_priv *wdt = dev_get_priv(dev);
157
Simon Glass75e534b2020-12-16 21:20:07 -0700158 dev_dbg(dev, "%s: Probing wdt%u\n", __func__, dev_seq(dev));
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600159
160 ret = regmap_init_mem(dev_ofnode(dev), &wdt->regs);
161 if (ret) {
162 dev_dbg(dev, "failed to get regbase of wwdt\n");
163 return ret;
164 }
165
Simon Glass71fa5b42020-12-03 16:55:18 -0700166 wdt->enable_once = plat->enable_once;
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600167
168 ret = clk_get_by_index(dev, 0, &wdt->clk);
169 if (ret < 0)
170 dev_err(dev, "failed to get clock\n");
171
172 return ret;
173}
174
Simon Glassaad29ae2020-12-03 16:55:21 -0700175static int xlnx_wwdt_of_to_plat(struct udevice *dev)
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600176{
Simon Glassb75b15b2020-12-03 16:55:23 -0700177 struct xlnx_wwdt_plat *plat = dev_get_plat(dev);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600178
Simon Glass71fa5b42020-12-03 16:55:18 -0700179 plat->enable_once = dev_read_u32_default(dev, "xlnx,wdt-enable-once",
180 0);
181 dev_dbg(dev, "wdt-enable-once %d\n", plat->enable_once);
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600182
183 return 0;
184}
185
186static const struct wdt_ops xlnx_wwdt_ops = {
187 .start = xlnx_wwdt_start,
188 .reset = xlnx_wwdt_reset,
189 .stop = xlnx_wwdt_stop,
Ashok Reddy Somad630edd2021-09-28 11:31:59 +0530190 .expire_now = xlnx_wwdt_expire_now,
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600191};
192
193static const struct udevice_id xlnx_wwdt_ids[] = {
Michal Simek8a351c32023-06-13 13:22:26 +0200194 { .compatible = "xlnx,versal-wwdt", },
195 { .compatible = "xlnx,versal-wwdt-1.0", }, /* deprecated */
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600196 {},
197};
198
199U_BOOT_DRIVER(xlnx_wwdt) = {
200 .name = "xlnx_wwdt",
201 .id = UCLASS_WDT,
202 .of_match = xlnx_wwdt_ids,
203 .probe = xlnx_wwdt_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700204 .priv_auto = sizeof(struct xlnx_wwdt_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700205 .plat_auto = sizeof(struct xlnx_wwdt_plat),
Simon Glassaad29ae2020-12-03 16:55:21 -0700206 .of_to_plat = xlnx_wwdt_of_to_plat,
Ashok Reddy Somaf4aecf42020-03-11 03:06:04 -0600207 .ops = &xlnx_wwdt_ops,
208};