blob: a7b735979aec030bfabd77a0714b6ba0be5bee5b [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
6#include <common.h>
Marek Vasutcf8c8362019-06-27 01:19:23 +02007#include <dm.h>
8#include <wdt.h>
Chin Liang Seed8c67dc2014-06-10 01:10:21 -05009#include <asm/io.h>
10#include <asm/utils.h>
11
12#define DW_WDT_CR 0x00
13#define DW_WDT_TORR 0x04
14#define DW_WDT_CRR 0x0C
15
16#define DW_WDT_CR_EN_OFFSET 0x00
17#define DW_WDT_CR_RMOD_OFFSET 0x01
18#define DW_WDT_CR_RMOD_VAL 0x00
19#define DW_WDT_CRR_RESTART_VAL 0x76
20
Marek Vasutcf8c8362019-06-27 01:19:23 +020021struct designware_wdt_priv {
22 void __iomem *base;
23};
24
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050025/*
26 * Set the watchdog time interval.
27 * Counter is 32 bit.
28 */
Marek Vasutcf8c8362019-06-27 01:19:23 +020029static int designware_wdt_settimeout(void __iomem *base, unsigned int clk_khz,
30 unsigned int timeout)
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050031{
32 signed int i;
33
34 /* calculate the timeout range value */
Marek Vasutcf8c8362019-06-27 01:19:23 +020035 i = log_2_n_round_up(timeout * clk_khz) - 16;
36 i = clamp(i, 0, 15);
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050037
Marek Vasutcf8c8362019-06-27 01:19:23 +020038 writel(i | (i << 4), base + DW_WDT_TORR);
39
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050040 return 0;
41}
42
Marek Vasutcf8c8362019-06-27 01:19:23 +020043static void designware_wdt_enable(void __iomem *base)
Chin Liang Seed8c67dc2014-06-10 01:10:21 -050044{
Marek Vasutcf8c8362019-06-27 01:19:23 +020045 writel((DW_WDT_CR_RMOD_VAL << DW_WDT_CR_RMOD_OFFSET) |
46 BIT(DW_WDT_CR_EN_OFFSET),
47 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);
96 writel(DW_WDT_CR_RMOD_VAL << DW_WDT_CR_RMOD_OFFSET,
97 priv->base + DW_WDT_CR);
98
99 return 0;
100}
101
102static int designware_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
103{
104 struct designware_wdt_priv *priv = dev_get_priv(dev);
105
106 designware_wdt_stop(dev);
107
108 /* set timer in miliseconds */
109 designware_wdt_settimeout(priv->base, CONFIG_DW_WDT_CLOCK_KHZ, timeout);
110
111 designware_wdt_enable(priv->base);
112
113 /* reset the watchdog */
114 return designware_wdt_reset(dev);
115}
116
117static int designware_wdt_probe(struct udevice *dev)
118{
119 struct designware_wdt_priv *priv = dev_get_priv(dev);
120
121 priv->base = dev_remap_addr(dev);
122 if (!priv->base)
123 return -EINVAL;
124
125 /* reset to disable the watchdog */
126 return designware_wdt_stop(dev);
127}
128
129static const struct wdt_ops designware_wdt_ops = {
130 .start = designware_wdt_start,
131 .reset = designware_wdt_reset,
132 .stop = designware_wdt_stop,
133};
134
135static const struct udevice_id designware_wdt_ids[] = {
136 { .compatible = "snps,dw-wdt"},
137 {}
138};
139
140U_BOOT_DRIVER(designware_wdt) = {
141 .name = "designware_wdt",
142 .id = UCLASS_WDT,
143 .of_match = designware_wdt_ids,
144 .priv_auto_alloc_size = sizeof(struct designware_wdt_priv),
145 .probe = designware_wdt_probe,
146 .ops = &designware_wdt_ops,
147 .flags = DM_FLAG_PRE_RELOC,
148};
Chin Liang Seed8c67dc2014-06-10 01:10:21 -0500149#endif