blob: 190490f3692ba03e903701e2ed7812a097653e55 [file] [log] [blame]
Chia-Wei, Wang67ef5872020-12-14 13:54:25 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2020 Aspeed Technology, Inc
4 */
5
Chia-Wei, Wang67ef5872020-12-14 13:54:25 +08006#include <dm.h>
7#include <errno.h>
8#include <log.h>
9#include <wdt.h>
10#include <asm/io.h>
11#include <asm/arch/wdt_ast2600.h>
12#include <linux/err.h>
13
14struct ast2600_wdt_priv {
15 struct ast2600_wdt *regs;
16};
17
18static int ast2600_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
19{
20 struct ast2600_wdt_priv *priv = dev_get_priv(dev);
21 struct ast2600_wdt *wdt = priv->regs;
22
23 /* WDT counts in the 1MHz frequency, namely 1us */
24 writel((u32)(timeout_ms * 1000), &wdt->counter_reload_val);
25 writel(WDT_COUNTER_RESTART_VAL, &wdt->counter_restart);
26 writel(WDT_CTRL_EN | WDT_CTRL_RESET, &wdt->ctrl);
27
28 return 0;
29}
30
31static int ast2600_wdt_stop(struct udevice *dev)
32{
33 struct ast2600_wdt_priv *priv = dev_get_priv(dev);
34 struct ast2600_wdt *wdt = priv->regs;
35
36 clrbits_le32(&wdt->ctrl, WDT_CTRL_EN);
37
38 writel(WDT_RESET_MASK1_DEFAULT, &wdt->reset_mask1);
39 writel(WDT_RESET_MASK2_DEFAULT, &wdt->reset_mask2);
40
41 return 0;
42}
43
44static int ast2600_wdt_reset(struct udevice *dev)
45{
46 struct ast2600_wdt_priv *priv = dev_get_priv(dev);
47 struct ast2600_wdt *wdt = priv->regs;
48
49 writel(WDT_COUNTER_RESTART_VAL, &wdt->counter_restart);
50
51 return 0;
52}
53
54static int ast2600_wdt_expire_now(struct udevice *dev, ulong flags)
55{
56 int ret;
57 struct ast2600_wdt_priv *priv = dev_get_priv(dev);
58 struct ast2600_wdt *wdt = priv->regs;
59
60 ret = ast2600_wdt_start(dev, 1, flags);
61 if (ret)
62 return ret;
63
64 while (readl(&wdt->ctrl) & WDT_CTRL_EN)
65 ;
66
67 return ast2600_wdt_stop(dev);
68}
69
70static int ast2600_wdt_of_to_plat(struct udevice *dev)
71{
72 struct ast2600_wdt_priv *priv = dev_get_priv(dev);
73
74 priv->regs = dev_read_addr_ptr(dev);
75 if (!priv->regs)
76 return -EINVAL;
77
78 return 0;
79}
80
81static const struct wdt_ops ast2600_wdt_ops = {
82 .start = ast2600_wdt_start,
83 .reset = ast2600_wdt_reset,
84 .stop = ast2600_wdt_stop,
85 .expire_now = ast2600_wdt_expire_now,
86};
87
88static const struct udevice_id ast2600_wdt_ids[] = {
89 { .compatible = "aspeed,ast2600-wdt" },
90 { }
91};
92
93static int ast2600_wdt_probe(struct udevice *dev)
94{
95 debug("%s() wdt%u\n", __func__, dev_seq(dev));
96 ast2600_wdt_stop(dev);
97
98 return 0;
99}
100
101U_BOOT_DRIVER(ast2600_wdt) = {
102 .name = "ast2600_wdt",
103 .id = UCLASS_WDT,
104 .of_match = ast2600_wdt_ids,
105 .probe = ast2600_wdt_probe,
106 .priv_auto = sizeof(struct ast2600_wdt_priv),
107 .of_to_plat = ast2600_wdt_of_to_plat,
108 .ops = &ast2600_wdt_ops,
109};