blob: 1024a04596ff291407d02c5a904a82fbf816fcc1 [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>
12#include <asm/utils.h>
13
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 */
Marek Vasutcf8c8362019-06-27 01:19:23 +020037 i = log_2_n_round_up(timeout * clk_khz) - 16;
38 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
132 priv->clk_khz = clk_get_rate(&clk);
133 if (!priv->clk_khz)
134 return -EINVAL;
135#else
136 priv->clk_khz = CONFIG_DW_WDT_CLOCK_KHZ;
137#endif
138
139#if CONFIG_IS_ENABLED(DM_RESET)
140 struct reset_ctl_bulk resets;
141
142 ret = reset_get_bulk(dev, &resets);
143 if (ret)
144 return ret;
145
146 ret = reset_deassert_bulk(&resets);
147 if (ret)
148 return ret;
149#endif
150
Marek Vasutcf8c8362019-06-27 01:19:23 +0200151 /* reset to disable the watchdog */
152 return designware_wdt_stop(dev);
153}
154
155static const struct wdt_ops designware_wdt_ops = {
156 .start = designware_wdt_start,
157 .reset = designware_wdt_reset,
158 .stop = designware_wdt_stop,
159};
160
161static const struct udevice_id designware_wdt_ids[] = {
162 { .compatible = "snps,dw-wdt"},
163 {}
164};
165
166U_BOOT_DRIVER(designware_wdt) = {
167 .name = "designware_wdt",
168 .id = UCLASS_WDT,
169 .of_match = designware_wdt_ids,
170 .priv_auto_alloc_size = sizeof(struct designware_wdt_priv),
171 .probe = designware_wdt_probe,
172 .ops = &designware_wdt_ops,
173 .flags = DM_FLAG_PRE_RELOC,
174};
Chin Liang Seed8c67dc2014-06-10 01:10:21 -0500175#endif