blob: 9e5487168cd8b3a71411257fb24401a4e0b303ce [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chin Liang Seed8c67dc2014-06-10 01:10:21 -05002/*
3 * Copyright (C) 2013 Altera Corporation <www.altera.com>
Chin Liang Seed8c67dc2014-06-10 01:10:21 -05004 */
5
Marek Vasutcf89ef82019-10-03 14:47:07 +02006#include <clk.h>
Chin Liang Seed8c67dc2014-06-10 01:10:21 -05007#include <common.h>
Marek Vasutcf8c8362019-06-27 01:19:23 +02008#include <dm.h>
Marek Vasutcf89ef82019-10-03 14:47:07 +02009#include <reset.h>
Marek Vasutcf8c8362019-06-27 01:19:23 +020010#include <wdt.h>
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050011#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060012#include <linux/bitops.h>
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050013
14#define DW_WDT_CR 0x00
15#define DW_WDT_TORR 0x04
16#define DW_WDT_CRR 0x0C
17
18#define DW_WDT_CR_EN_OFFSET 0x00
19#define DW_WDT_CR_RMOD_OFFSET 0x01
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050020#define DW_WDT_CRR_RESTART_VAL 0x76
21
Marek Vasutcf8c8362019-06-27 01:19:23 +020022struct designware_wdt_priv {
23 void __iomem *base;
Marek Vasutcf89ef82019-10-03 14:47:07 +020024 unsigned int clk_khz;
Marek Vasutcf8c8362019-06-27 01:19:23 +020025};
26
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050027/*
28 * Set the watchdog time interval.
29 * Counter is 32 bit.
30 */
Marek Vasutcf8c8362019-06-27 01:19:23 +020031static int designware_wdt_settimeout(void __iomem *base, unsigned int clk_khz,
32 unsigned int timeout)
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050033{
34 signed int i;
35
36 /* calculate the timeout range value */
Sean Andersoncb578112021-03-10 21:02:17 -050037 i = fls(timeout * clk_khz - 1) - 16;
Marek Vasutcf8c8362019-06-27 01:19:23 +020038 i = clamp(i, 0, 15);
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050039
Marek Vasutcf8c8362019-06-27 01:19:23 +020040 writel(i | (i << 4), base + DW_WDT_TORR);
41
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050042 return 0;
43}
44
Marek Vasutcf8c8362019-06-27 01:19:23 +020045static void designware_wdt_enable(void __iomem *base)
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050046{
Marek Vasutcf89ef82019-10-03 14:47:07 +020047 writel(BIT(DW_WDT_CR_EN_OFFSET), base + DW_WDT_CR);
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050048}
49
Marek Vasutcf8c8362019-06-27 01:19:23 +020050static unsigned int designware_wdt_is_enabled(void __iomem *base)
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050051{
Marek Vasutcf8c8362019-06-27 01:19:23 +020052 return readl(base + DW_WDT_CR) & BIT(0);
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050053}
54
Marek Vasutcf8c8362019-06-27 01:19:23 +020055static void designware_wdt_reset_common(void __iomem *base)
56{
57 if (designware_wdt_is_enabled(base))
58 /* restart the watchdog counter */
59 writel(DW_WDT_CRR_RESTART_VAL, base + DW_WDT_CRR);
60}
61
62#if !CONFIG_IS_ENABLED(WDT)
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050063void hw_watchdog_reset(void)
64{
Marek Vasutcf8c8362019-06-27 01:19:23 +020065 designware_wdt_reset_common((void __iomem *)CONFIG_DW_WDT_BASE);
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050066}
67
68void hw_watchdog_init(void)
69{
70 /* reset to disable the watchdog */
71 hw_watchdog_reset();
72 /* set timer in miliseconds */
Marek Vasutcf8c8362019-06-27 01:19:23 +020073 designware_wdt_settimeout((void __iomem *)CONFIG_DW_WDT_BASE,
74 CONFIG_DW_WDT_CLOCK_KHZ,
75 CONFIG_WATCHDOG_TIMEOUT_MSECS);
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050076 /* enable the watchdog */
Marek Vasutcf8c8362019-06-27 01:19:23 +020077 designware_wdt_enable((void __iomem *)CONFIG_DW_WDT_BASE);
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050078 /* reset the watchdog */
79 hw_watchdog_reset();
80}
Marek Vasutcf8c8362019-06-27 01:19:23 +020081#else
82static int designware_wdt_reset(struct udevice *dev)
83{
84 struct designware_wdt_priv *priv = dev_get_priv(dev);
85
86 designware_wdt_reset_common(priv->base);
87
88 return 0;
89}
90
91static int designware_wdt_stop(struct udevice *dev)
92{
93 struct designware_wdt_priv *priv = dev_get_priv(dev);
94
95 designware_wdt_reset(dev);
Marek Vasutcf89ef82019-10-03 14:47:07 +020096 writel(0, priv->base + DW_WDT_CR);
Marek Vasutcf8c8362019-06-27 01:19:23 +020097
98 return 0;
99}
100
101static int designware_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
102{
103 struct designware_wdt_priv *priv = dev_get_priv(dev);
104
105 designware_wdt_stop(dev);
106
107 /* set timer in miliseconds */
Marek Vasutcf89ef82019-10-03 14:47:07 +0200108 designware_wdt_settimeout(priv->base, priv->clk_khz, timeout);
Marek Vasutcf8c8362019-06-27 01:19:23 +0200109
110 designware_wdt_enable(priv->base);
111
112 /* reset the watchdog */
113 return designware_wdt_reset(dev);
114}
115
116static int designware_wdt_probe(struct udevice *dev)
117{
118 struct designware_wdt_priv *priv = dev_get_priv(dev);
Marek Vasutcf89ef82019-10-03 14:47:07 +0200119 __maybe_unused int ret;
Marek Vasutcf8c8362019-06-27 01:19:23 +0200120
121 priv->base = dev_remap_addr(dev);
122 if (!priv->base)
123 return -EINVAL;
124
Marek Vasutcf89ef82019-10-03 14:47:07 +0200125#if CONFIG_IS_ENABLED(CLK)
126 struct clk clk;
127
128 ret = clk_get_by_index(dev, 0, &clk);
129 if (ret)
130 return ret;
131
Sean Anderson4cb0ab42021-03-10 21:02:19 -0500132 ret = clk_enable(&clk);
133 if (ret)
Sean Anderson97bcdd22021-03-10 21:02:20 -0500134 goto err;
Sean Anderson4cb0ab42021-03-10 21:02:19 -0500135
Jack Mitchelld9b9c912020-09-17 10:30:40 +0100136 priv->clk_khz = clk_get_rate(&clk) / 1000;
Sean Anderson97bcdd22021-03-10 21:02:20 -0500137 if (!priv->clk_khz) {
138 ret = -EINVAL;
139 goto err;
140 }
Marek Vasutcf89ef82019-10-03 14:47:07 +0200141#else
142 priv->clk_khz = CONFIG_DW_WDT_CLOCK_KHZ;
143#endif
144
Sean Anderson7d839432021-03-10 21:02:18 -0500145 if (CONFIG_IS_ENABLED(DM_RESET)) {
146 struct reset_ctl_bulk resets;
Marek Vasutcf89ef82019-10-03 14:47:07 +0200147
Sean Anderson7d839432021-03-10 21:02:18 -0500148 ret = reset_get_bulk(dev, &resets);
149 if (ret)
Sean Anderson97bcdd22021-03-10 21:02:20 -0500150 goto err;
Marek Vasutcf89ef82019-10-03 14:47:07 +0200151
Sean Anderson7d839432021-03-10 21:02:18 -0500152 ret = reset_deassert_bulk(&resets);
153 if (ret)
Sean Anderson97bcdd22021-03-10 21:02:20 -0500154 goto err;
Sean Anderson7d839432021-03-10 21:02:18 -0500155 }
Marek Vasutcf89ef82019-10-03 14:47:07 +0200156
Marek Vasutcf8c8362019-06-27 01:19:23 +0200157 /* reset to disable the watchdog */
158 return designware_wdt_stop(dev);
Sean Anderson97bcdd22021-03-10 21:02:20 -0500159
160err:
161#if CONFIG_IS_ENABLED(CLK)
162 clk_free(&clk);
163#endif
164 return ret;
Marek Vasutcf8c8362019-06-27 01:19:23 +0200165}
166
167static const struct wdt_ops designware_wdt_ops = {
168 .start = designware_wdt_start,
169 .reset = designware_wdt_reset,
170 .stop = designware_wdt_stop,
171};
172
173static const struct udevice_id designware_wdt_ids[] = {
174 { .compatible = "snps,dw-wdt"},
175 {}
176};
177
178U_BOOT_DRIVER(designware_wdt) = {
179 .name = "designware_wdt",
180 .id = UCLASS_WDT,
181 .of_match = designware_wdt_ids,
Simon Glass41575d82020-12-03 16:55:17 -0700182 .priv_auto = sizeof(struct designware_wdt_priv),
Marek Vasutcf8c8362019-06-27 01:19:23 +0200183 .probe = designware_wdt_probe,
184 .ops = &designware_wdt_ops,
185 .flags = DM_FLAG_PRE_RELOC,
186};
Chin Liang Seed8c67dc2014-06-10 01:10:21 -0500187#endif