blob: bd9d7105366ba67cd136adc961c0e319a15b1567 [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>
Marek Vasut8655f672019-06-27 01:19:23 +02007#include <dm.h>
Marek Vasutb1fdd3a2019-10-03 14:47:07 +02008#include <reset.h>
Marek Vasut8655f672019-06-27 01:19:23 +02009#include <wdt.h>
Chin Liang See9d800092014-06-10 01:10:21 -050010#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
Chin Liang See9d800092014-06-10 01:10:21 -050012
13#define DW_WDT_CR 0x00
14#define DW_WDT_TORR 0x04
15#define DW_WDT_CRR 0x0C
16
17#define DW_WDT_CR_EN_OFFSET 0x00
18#define DW_WDT_CR_RMOD_OFFSET 0x01
Chin Liang See9d800092014-06-10 01:10:21 -050019#define DW_WDT_CRR_RESTART_VAL 0x76
20
Marek Vasut8655f672019-06-27 01:19:23 +020021struct designware_wdt_priv {
22 void __iomem *base;
Marek Vasutb1fdd3a2019-10-03 14:47:07 +020023 unsigned int clk_khz;
Sean Andersoncedadbe2021-09-11 15:11:30 -040024 struct reset_ctl_bulk resets;
Marek Vasut8655f672019-06-27 01:19:23 +020025};
26
Chin Liang See9d800092014-06-10 01:10:21 -050027/*
28 * Set the watchdog time interval.
29 * Counter is 32 bit.
30 */
Marek Vasut8655f672019-06-27 01:19:23 +020031static int designware_wdt_settimeout(void __iomem *base, unsigned int clk_khz,
32 unsigned int timeout)
Chin Liang See9d800092014-06-10 01:10:21 -050033{
34 signed int i;
35
36 /* calculate the timeout range value */
Sean Andersona202a3c2021-03-10 21:02:17 -050037 i = fls(timeout * clk_khz - 1) - 16;
Marek Vasut8655f672019-06-27 01:19:23 +020038 i = clamp(i, 0, 15);
Chin Liang See9d800092014-06-10 01:10:21 -050039
Marek Vasut8655f672019-06-27 01:19:23 +020040 writel(i | (i << 4), base + DW_WDT_TORR);
41
Chin Liang See9d800092014-06-10 01:10:21 -050042 return 0;
43}
44
Marek Vasut8655f672019-06-27 01:19:23 +020045static void designware_wdt_enable(void __iomem *base)
Chin Liang See9d800092014-06-10 01:10:21 -050046{
Marek Vasutb1fdd3a2019-10-03 14:47:07 +020047 writel(BIT(DW_WDT_CR_EN_OFFSET), base + DW_WDT_CR);
Chin Liang See9d800092014-06-10 01:10:21 -050048}
49
Marek Vasut8655f672019-06-27 01:19:23 +020050static unsigned int designware_wdt_is_enabled(void __iomem *base)
Chin Liang See9d800092014-06-10 01:10:21 -050051{
Marek Vasut8655f672019-06-27 01:19:23 +020052 return readl(base + DW_WDT_CR) & BIT(0);
Chin Liang See9d800092014-06-10 01:10:21 -050053}
54
Marek Vasut8655f672019-06-27 01:19:23 +020055static void designware_wdt_reset_common(void __iomem *base)
Chin Liang See9d800092014-06-10 01:10:21 -050056{
Marek Vasut8655f672019-06-27 01:19:23 +020057 if (designware_wdt_is_enabled(base))
Chin Liang See9d800092014-06-10 01:10:21 -050058 /* restart the watchdog counter */
Marek Vasut8655f672019-06-27 01:19:23 +020059 writel(DW_WDT_CRR_RESTART_VAL, base + DW_WDT_CRR);
Chin Liang See9d800092014-06-10 01:10:21 -050060}
61
Marek Vasut8655f672019-06-27 01:19:23 +020062static int designware_wdt_reset(struct udevice *dev)
63{
64 struct designware_wdt_priv *priv = dev_get_priv(dev);
65
66 designware_wdt_reset_common(priv->base);
67
68 return 0;
69}
70
71static int designware_wdt_stop(struct udevice *dev)
72{
73 struct designware_wdt_priv *priv = dev_get_priv(dev);
Quentin Schulz1cf63382022-11-15 11:20:14 +010074 __maybe_unused int ret;
Marek Vasut8655f672019-06-27 01:19:23 +020075
76 designware_wdt_reset(dev);
Marek Vasutb1fdd3a2019-10-03 14:47:07 +020077 writel(0, priv->base + DW_WDT_CR);
Marek Vasut8655f672019-06-27 01:19:23 +020078
Quentin Schulz1cf63382022-11-15 11:20:14 +010079 if (CONFIG_IS_ENABLED(DM_RESET) &&
80 ofnode_read_prop(dev_ofnode(dev), "resets", &ret)) {
Sean Andersoncedadbe2021-09-11 15:11:30 -040081 ret = reset_assert_bulk(&priv->resets);
MengLi72da3d12021-05-24 10:22:48 +080082 if (ret)
83 return ret;
84
Sean Andersoncedadbe2021-09-11 15:11:30 -040085 ret = reset_deassert_bulk(&priv->resets);
MengLi72da3d12021-05-24 10:22:48 +080086 if (ret)
87 return ret;
88 }
89
Marek Vasut8655f672019-06-27 01:19:23 +020090 return 0;
91}
92
93static int designware_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
94{
95 struct designware_wdt_priv *priv = dev_get_priv(dev);
96
97 designware_wdt_stop(dev);
98
99 /* set timer in miliseconds */
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200100 designware_wdt_settimeout(priv->base, priv->clk_khz, timeout);
Marek Vasut8655f672019-06-27 01:19:23 +0200101
102 designware_wdt_enable(priv->base);
103
104 /* reset the watchdog */
105 return designware_wdt_reset(dev);
106}
107
108static int designware_wdt_probe(struct udevice *dev)
109{
110 struct designware_wdt_priv *priv = dev_get_priv(dev);
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200111 __maybe_unused int ret;
Marek Vasut8655f672019-06-27 01:19:23 +0200112
113 priv->base = dev_remap_addr(dev);
114 if (!priv->base)
115 return -EINVAL;
116
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200117#if CONFIG_IS_ENABLED(CLK)
118 struct clk clk;
119
120 ret = clk_get_by_index(dev, 0, &clk);
121 if (ret)
122 return ret;
123
Sean Anderson85c92702021-03-10 21:02:19 -0500124 ret = clk_enable(&clk);
125 if (ret)
Sean Andersond318eb32023-12-16 14:38:42 -0500126 return ret;
Sean Anderson85c92702021-03-10 21:02:19 -0500127
Jack Mitchellf23419c2020-09-17 10:30:40 +0100128 priv->clk_khz = clk_get_rate(&clk) / 1000;
Sean Andersond318eb32023-12-16 14:38:42 -0500129 if (!priv->clk_khz)
130 return -EINVAL;
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200131#else
Tom Rini79088cf2022-12-04 10:03:39 -0500132 priv->clk_khz = CFG_DW_WDT_CLOCK_KHZ;
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200133#endif
134
Quentin Schulz1cf63382022-11-15 11:20:14 +0100135 if (CONFIG_IS_ENABLED(DM_RESET) &&
136 ofnode_read_prop(dev_ofnode(dev), "resets", &ret)) {
Sean Andersoncedadbe2021-09-11 15:11:30 -0400137 ret = reset_get_bulk(dev, &priv->resets);
Sean Anderson955c95d2021-03-10 21:02:18 -0500138 if (ret)
Sean Andersond318eb32023-12-16 14:38:42 -0500139 return ret;
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200140
Sean Andersoncedadbe2021-09-11 15:11:30 -0400141 ret = reset_deassert_bulk(&priv->resets);
Sean Anderson955c95d2021-03-10 21:02:18 -0500142 if (ret)
Sean Andersond318eb32023-12-16 14:38:42 -0500143 return ret;
Sean Anderson955c95d2021-03-10 21:02:18 -0500144 }
Marek Vasutb1fdd3a2019-10-03 14:47:07 +0200145
Marek Vasut8655f672019-06-27 01:19:23 +0200146 /* reset to disable the watchdog */
147 return designware_wdt_stop(dev);
148}
149
150static const struct wdt_ops designware_wdt_ops = {
151 .start = designware_wdt_start,
152 .reset = designware_wdt_reset,
153 .stop = designware_wdt_stop,
154};
155
156static const struct udevice_id designware_wdt_ids[] = {
157 { .compatible = "snps,dw-wdt"},
158 {}
159};
160
161U_BOOT_DRIVER(designware_wdt) = {
162 .name = "designware_wdt",
163 .id = UCLASS_WDT,
164 .of_match = designware_wdt_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700165 .priv_auto = sizeof(struct designware_wdt_priv),
Marek Vasut8655f672019-06-27 01:19:23 +0200166 .probe = designware_wdt_probe,
167 .ops = &designware_wdt_ops,
168 .flags = DM_FLAG_PRE_RELOC,
169};