blob: fe2f6be5a74dbe1c7326c42a4f602e3c6eeff93c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
maxims@google.comdf35df22017-04-17 12:00:22 -07002/*
3 * Copyright 2017 Google, Inc
maxims@google.comdf35df22017-04-17 12:00:22 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <wdt.h>
10#include <asm/io.h>
11#include <asm/arch/wdt.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070012#include <linux/err.h>
maxims@google.comdf35df22017-04-17 12:00:22 -070013
14#define WDT_AST2500 2500
15#define WDT_AST2400 2400
16
maxims@google.comdf35df22017-04-17 12:00:22 -070017struct ast_wdt_priv {
18 struct ast_wdt *regs;
19};
20
21static int ast_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
22{
23 struct ast_wdt_priv *priv = dev_get_priv(dev);
24 ulong driver_data = dev_get_driver_data(dev);
25 u32 reset_mode = ast_reset_mode_from_flags(flags);
26
Joel Stanley53e1d3c2019-06-06 17:08:45 +093027 /* 32 bits at 1MHz is 4294967ms */
28 timeout = min_t(u64, timeout, 4294967);
29
30 /* WDT counts in ticks of 1MHz clock. 1ms / 1e3 * 1e6 */
31 timeout *= 1000;
32
maxims@google.comdf35df22017-04-17 12:00:22 -070033 clrsetbits_le32(&priv->regs->ctrl,
34 WDT_CTRL_RESET_MASK << WDT_CTRL_RESET_MODE_SHIFT,
35 reset_mode << WDT_CTRL_RESET_MODE_SHIFT);
36
37 if (driver_data >= WDT_AST2500 && reset_mode == WDT_CTRL_RESET_SOC)
38 writel(ast_reset_mask_from_flags(flags),
39 &priv->regs->reset_mask);
40
41 writel((u32) timeout, &priv->regs->counter_reload_val);
42 writel(WDT_COUNTER_RESTART_VAL, &priv->regs->counter_restart);
43 /*
44 * Setting CLK1MHZ bit is just for compatibility with ast2400 part.
45 * On ast2500 watchdog timer clock is fixed at 1MHz and the bit is
46 * read-only
47 */
48 setbits_le32(&priv->regs->ctrl,
49 WDT_CTRL_EN | WDT_CTRL_RESET | WDT_CTRL_CLK1MHZ);
50
51 return 0;
52}
53
54static int ast_wdt_stop(struct udevice *dev)
55{
56 struct ast_wdt_priv *priv = dev_get_priv(dev);
57
58 clrbits_le32(&priv->regs->ctrl, WDT_CTRL_EN);
59
Cédric Le Goatercb466db2018-10-16 13:57:11 +020060 writel(WDT_RESET_DEFAULT, &priv->regs->reset_mask);
maxims@google.comdf35df22017-04-17 12:00:22 -070061 return 0;
62}
63
64static int ast_wdt_reset(struct udevice *dev)
65{
66 struct ast_wdt_priv *priv = dev_get_priv(dev);
67
68 writel(WDT_COUNTER_RESTART_VAL, &priv->regs->counter_restart);
69
70 return 0;
71}
72
73static int ast_wdt_expire_now(struct udevice *dev, ulong flags)
74{
75 struct ast_wdt_priv *priv = dev_get_priv(dev);
76 int ret;
77
78 ret = ast_wdt_start(dev, 1, flags);
79 if (ret)
80 return ret;
81
82 while (readl(&priv->regs->ctrl) & WDT_CTRL_EN)
83 ;
84
85 return ast_wdt_stop(dev);
86}
87
88static int ast_wdt_ofdata_to_platdata(struct udevice *dev)
89{
90 struct ast_wdt_priv *priv = dev_get_priv(dev);
91
Simon Glassba1dea42017-05-17 17:18:05 -060092 priv->regs = devfdt_get_addr_ptr(dev);
maxims@google.comdf35df22017-04-17 12:00:22 -070093 if (IS_ERR(priv->regs))
94 return PTR_ERR(priv->regs);
95
96 return 0;
97}
98
99static const struct wdt_ops ast_wdt_ops = {
100 .start = ast_wdt_start,
101 .reset = ast_wdt_reset,
102 .stop = ast_wdt_stop,
103 .expire_now = ast_wdt_expire_now,
104};
105
106static const struct udevice_id ast_wdt_ids[] = {
107 { .compatible = "aspeed,wdt", .data = WDT_AST2500 },
108 { .compatible = "aspeed,ast2500-wdt", .data = WDT_AST2500 },
109 { .compatible = "aspeed,ast2400-wdt", .data = WDT_AST2400 },
110 {}
111};
112
113static int ast_wdt_probe(struct udevice *dev)
114{
115 debug("%s() wdt%u\n", __func__, dev->seq);
116 ast_wdt_stop(dev);
117
118 return 0;
119}
120
121U_BOOT_DRIVER(ast_wdt) = {
122 .name = "ast_wdt",
123 .id = UCLASS_WDT,
124 .of_match = ast_wdt_ids,
125 .probe = ast_wdt_probe,
126 .priv_auto_alloc_size = sizeof(struct ast_wdt_priv),
127 .ofdata_to_platdata = ast_wdt_ofdata_to_platdata,
128 .ops = &ast_wdt_ops,
maxims@google.comdf35df22017-04-17 12:00:22 -0700129};