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