blob: e61e13fdc49d71153f3c423693493b98c07b4664 [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
maxims@google.comdf35df22017-04-17 12:00:22 -07006#include <dm.h>
7#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
maxims@google.comdf35df22017-04-17 12:00:22 -07009#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
Simon Glassaad29ae2020-12-03 16:55:21 -070088static int ast_wdt_of_to_plat(struct udevice *dev)
maxims@google.comdf35df22017-04-17 12:00:22 -070089{
90 struct ast_wdt_priv *priv = dev_get_priv(dev);
91
Masahiro Yamada32822d02020-08-04 14:14:43 +090092 priv->regs = dev_read_addr_ptr(dev);
Ovidiu Panaita633f002020-08-03 22:17:35 +030093 if (!priv->regs)
94 return -EINVAL;
maxims@google.comdf35df22017-04-17 12:00:22 -070095
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{
Simon Glass75e534b2020-12-16 21:20:07 -0700115 debug("%s() wdt%u\n", __func__, dev_seq(dev));
maxims@google.comdf35df22017-04-17 12:00:22 -0700116 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,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700126 .priv_auto = sizeof(struct ast_wdt_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -0700127 .of_to_plat = ast_wdt_of_to_plat,
maxims@google.comdf35df22017-04-17 12:00:22 -0700128 .ops = &ast_wdt_ops,
maxims@google.comdf35df22017-04-17 12:00:22 -0700129};