blob: b22e0ee06a4e5cf4386a4fc91feaf0a756963273 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chin Liang See9d800092014-06-10 01:10:21 -05002/*
3 * Copyright (C) 2013 Altera Corporation <www.altera.com>
Chin Liang See9d800092014-06-10 01:10:21 -05004 */
5
Marek Vasutb1fdd3a2019-10-03 14:47:07 +02006#include <clk.h>
Chin Liang See9d800092014-06-10 01:10:21 -05007#include <common.h>
Marek Vasut8655f672019-06-27 01:19:23 +02008#include <dm.h>
Marek Vasutb1fdd3a2019-10-03 14:47:07 +02009#include <reset.h>
Marek Vasut8655f672019-06-27 01:19:23 +020010#include <wdt.h>
Chin Liang See9d800092014-06-10 01:10:21 -050011#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Chin Liang See9d800092014-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 See9d800092014-06-10 01:10:21 -050020#define DW_WDT_CRR_RESTART_VAL 0x76
21
Marek Vasut8655f672019-06-27 01:19:23 +020022struct designware_wdt_priv {
23 void __iomem *base;
Marek Vasutb1fdd3a2019-10-03 14:47:07 +020024 unsigned int clk_khz;
Sean Andersoncedadbe2021-09-11 15:11:30 -040025 struct reset_ctl_bulk resets;
Marek Vasut8655f672019-06-27 01:19:23 +020026};
27
Chin Liang See9d800092014-06-10 01:10:21 -050028/*
29 * Set the watchdog time interval.
30 * Counter is 32 bit.
31 */
Marek Vasut8655f672019-06-27 01:19:23 +020032static int designware_wdt_settimeout(void __iomem *base, unsigned int clk_khz,
33 unsigned int timeout)
Chin Liang See9d800092014-06-10 01:10:21 -050034{
35 signed int i;
36
37 /* calculate the timeout range value */
Sean Andersona202a3c2021-03-10 21:02:17 -050038 i = fls(timeout * clk_khz - 1) - 16;
Marek Vasut8655f672019-06-27 01:19:23 +020039 i = clamp(i, 0, 15);
Chin Liang See9d800092014-06-10 01:10:21 -050040
Marek Vasut8655f672019-06-27 01:19:23 +020041 writel(i | (i << 4), base + DW_WDT_TORR);
42
Chin Liang See9d800092014-06-10 01:10:21 -050043 return 0;
44}
45
Marek Vasut8655f672019-06-27 01:19:23 +020046static void designware_wdt_enable(void __iomem *base)
Chin Liang See9d800092014-06-10 01:10:21 -050047{
Marek Vasutb1fdd3a2019-10-03 14:47:07 +020048 writel(BIT(DW_WDT_CR_EN_OFFSET), base + DW_WDT_CR);
Chin Liang See9d800092014-06-10 01:10:21 -050049}
50
Marek Vasut8655f672019-06-27 01:19:23 +020051static unsigned int designware_wdt_is_enabled(void __iomem *base)
Chin Liang See9d800092014-06-10 01:10:21 -050052{
Marek Vasut8655f672019-06-27 01:19:23 +020053 return readl(base + DW_WDT_CR) & BIT(0);
Chin Liang See9d800092014-06-10 01:10:21 -050054}
55
Marek Vasut8655f672019-06-27 01:19:23 +020056static void designware_wdt_reset_common(void __iomem *base)
Chin Liang See9d800092014-06-10 01:10:21 -050057{
Marek Vasut8655f672019-06-27 01:19:23 +020058 if (designware_wdt_is_enabled(base))
Chin Liang See9d800092014-06-10 01:10:21 -050059 /* restart the watchdog counter */
Marek Vasut8655f672019-06-27 01:19:23 +020060 writel(DW_WDT_CRR_RESTART_VAL, base + DW_WDT_CRR);
Chin Liang See9d800092014-06-10 01:10:21 -050061}
62
Marek Vasut8655f672019-06-27 01:19:23 +020063static int designware_wdt_reset(struct udevice *dev)
64{
65 struct designware_wdt_priv *priv = dev_get_priv(dev);
66
67 designware_wdt_reset_common(priv->base);
68
69 return 0;
70}
71
72static int designware_wdt_stop(struct udevice *dev)
73{
74 struct designware_wdt_priv *priv = dev_get_priv(dev);
Quentin Schulz1cf63382022-11-15 11:20:14 +010075 __maybe_unused int ret;
Marek Vasut8655f672019-06-27 01:19:23 +020076
77 designware_wdt_reset(dev);
Marek Vasutb1fdd3a2019-10-03 14:47:07 +020078 writel(0, priv->base + DW_WDT_CR);
Marek Vasut8655f672019-06-27 01:19:23 +020079
Quentin Schulz1cf63382022-11-15 11:20:14 +010080 if (CONFIG_IS_ENABLED(DM_RESET) &&
81 ofnode_read_prop(dev_ofnode(dev), "resets", &ret)) {
Sean Andersoncedadbe2021-09-11 15:11:30 -040082 ret = reset_assert_bulk(&priv->resets);
MengLi72da3d12021-05-24 10:22:48 +080083 if (ret)
84 return ret;
85
Sean Andersoncedadbe2021-09-11 15:11:30 -040086 ret = reset_deassert_bulk(&priv->resets);
MengLi72da3d12021-05-24 10:22:48 +080087 if (ret)
88 return ret;
89 }
90
Marek Vasut8655f672019-06-27 01:19:23 +020091 return 0;
92}
93
94static int designware_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
95{
96 struct designware_wdt_priv *priv = dev_get_priv(dev);
97
98 designware_wdt_stop(dev);
99
100 /* set timer in miliseconds */
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200101 designware_wdt_settimeout(priv->base, priv->clk_khz, timeout);
Marek Vasut8655f672019-06-27 01:19:23 +0200102
103 designware_wdt_enable(priv->base);
104
105 /* reset the watchdog */
106 return designware_wdt_reset(dev);
107}
108
109static int designware_wdt_probe(struct udevice *dev)
110{
111 struct designware_wdt_priv *priv = dev_get_priv(dev);
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200112 __maybe_unused int ret;
Marek Vasut8655f672019-06-27 01:19:23 +0200113
114 priv->base = dev_remap_addr(dev);
115 if (!priv->base)
116 return -EINVAL;
117
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200118#if CONFIG_IS_ENABLED(CLK)
119 struct clk clk;
120
121 ret = clk_get_by_index(dev, 0, &clk);
122 if (ret)
123 return ret;
124
Sean Anderson85c92702021-03-10 21:02:19 -0500125 ret = clk_enable(&clk);
126 if (ret)
Sean Andersond318eb32023-12-16 14:38:42 -0500127 return ret;
Sean Anderson85c92702021-03-10 21:02:19 -0500128
Jack Mitchellf23419c2020-09-17 10:30:40 +0100129 priv->clk_khz = clk_get_rate(&clk) / 1000;
Sean Andersond318eb32023-12-16 14:38:42 -0500130 if (!priv->clk_khz)
131 return -EINVAL;
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200132#else
Tom Rini79088cf2022-12-04 10:03:39 -0500133 priv->clk_khz = CFG_DW_WDT_CLOCK_KHZ;
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200134#endif
135
Quentin Schulz1cf63382022-11-15 11:20:14 +0100136 if (CONFIG_IS_ENABLED(DM_RESET) &&
137 ofnode_read_prop(dev_ofnode(dev), "resets", &ret)) {
Sean Andersoncedadbe2021-09-11 15:11:30 -0400138 ret = reset_get_bulk(dev, &priv->resets);
Sean Anderson955c95d2021-03-10 21:02:18 -0500139 if (ret)
Sean Andersond318eb32023-12-16 14:38:42 -0500140 return ret;
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200141
Sean Andersoncedadbe2021-09-11 15:11:30 -0400142 ret = reset_deassert_bulk(&priv->resets);
Sean Anderson955c95d2021-03-10 21:02:18 -0500143 if (ret)
Sean Andersond318eb32023-12-16 14:38:42 -0500144 return ret;
Sean Anderson955c95d2021-03-10 21:02:18 -0500145 }
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200146
Marek Vasut8655f672019-06-27 01:19:23 +0200147 /* reset to disable the watchdog */
148 return designware_wdt_stop(dev);
149}
150
151static const struct wdt_ops designware_wdt_ops = {
152 .start = designware_wdt_start,
153 .reset = designware_wdt_reset,
154 .stop = designware_wdt_stop,
155};
156
157static const struct udevice_id designware_wdt_ids[] = {
158 { .compatible = "snps,dw-wdt"},
159 {}
160};
161
162U_BOOT_DRIVER(designware_wdt) = {
163 .name = "designware_wdt",
164 .id = UCLASS_WDT,
165 .of_match = designware_wdt_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700166 .priv_auto = sizeof(struct designware_wdt_priv),
Marek Vasut8655f672019-06-27 01:19:23 +0200167 .probe = designware_wdt_probe,
168 .ops = &designware_wdt_ops,
169 .flags = DM_FLAG_PRE_RELOC,
170};