watchdog: octeontx_wdt: Add support for start and stop
[platform/kernel/u-boot.git] / drivers / watchdog / octeontx_wdt.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 Marvell International Ltd.
4  *
5  * https://spdx.org/licenses
6  */
7
8 #include <clk.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <wdt.h>
12 #include <asm/io.h>
13 #include <linux/bitfield.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 #define CORE0_WDOG_OFFSET       0x40000
18 #define CORE0_POKE_OFFSET       0x50000
19 #define CORE0_POKE_OFFSET_MASK  0xfffffULL
20
21 #define WDOG_MODE               GENMASK_ULL(1, 0)
22 #define WDOG_LEN                GENMASK_ULL(19, 4)
23 #define WDOG_CNT                GENMASK_ULL(43, 20)
24
25 struct octeontx_wdt {
26         void __iomem *reg;
27         struct clk clk;
28 };
29
30 static int octeontx_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
31 {
32         struct octeontx_wdt *priv = dev_get_priv(dev);
33         u64 clk_rate, val;
34         u64 tout_wdog;
35
36         clk_rate = clk_get_rate(&priv->clk);
37         if (IS_ERR_VALUE(clk_rate))
38                 return -EINVAL;
39
40         /* Watchdog counts in 1024 cycle steps */
41         tout_wdog = (clk_rate * timeout_ms / 1000) >> 10;
42
43         /*
44          * We can only specify the upper 16 bits of a 24 bit value.
45          * Round up
46          */
47         tout_wdog = (tout_wdog + 0xff) >> 8;
48
49         /* If the timeout overflows the hardware limit, set max */
50         if (tout_wdog >= 0x10000)
51                 tout_wdog = 0xffff;
52
53         val = FIELD_PREP(WDOG_MODE, 0x3) |
54                 FIELD_PREP(WDOG_LEN, tout_wdog) |
55                 FIELD_PREP(WDOG_CNT, tout_wdog << 8);
56         writeq(val, priv->reg + CORE0_WDOG_OFFSET);
57
58         return 0;
59 }
60
61 static int octeontx_wdt_stop(struct udevice *dev)
62 {
63         struct octeontx_wdt *priv = dev_get_priv(dev);
64
65         writeq(0, priv->reg + CORE0_WDOG_OFFSET);
66
67         return 0;
68 }
69
70 static int octeontx_wdt_expire_now(struct udevice *dev, ulong flags)
71 {
72         octeontx_wdt_stop(dev);
73
74         /* Start with 100ms timeout to expire immediately */
75         octeontx_wdt_start(dev, 100, flags);
76
77         return 0;
78 }
79
80 static int octeontx_wdt_reset(struct udevice *dev)
81 {
82         struct octeontx_wdt *priv = dev_get_priv(dev);
83
84         writeq(~0ULL, priv->reg + CORE0_POKE_OFFSET);
85
86         return 0;
87 }
88
89 static int octeontx_wdt_remove(struct udevice *dev)
90 {
91         octeontx_wdt_stop(dev);
92
93         return 0;
94 }
95
96 static int octeontx_wdt_probe(struct udevice *dev)
97 {
98         struct octeontx_wdt *priv = dev_get_priv(dev);
99         int ret;
100
101         priv->reg = dev_remap_addr(dev);
102         if (!priv->reg)
103                 return -EINVAL;
104
105         /*
106          * Save base register address in reg masking lower 20 bits
107          * as 0xa0000 appears when extracted from the DT
108          */
109         priv->reg = (void __iomem *)(((u64)priv->reg &
110                                       ~CORE0_POKE_OFFSET_MASK));
111
112         ret = clk_get_by_index(dev, 0, &priv->clk);
113         if (ret < 0)
114                 return ret;
115
116         ret = clk_enable(&priv->clk);
117         if (ret)
118                 return ret;
119
120         return 0;
121 }
122
123 static const struct wdt_ops octeontx_wdt_ops = {
124         .reset = octeontx_wdt_reset,
125         .start = octeontx_wdt_start,
126         .stop = octeontx_wdt_stop,
127         .expire_now = octeontx_wdt_expire_now,
128 };
129
130 static const struct udevice_id octeontx_wdt_ids[] = {
131         { .compatible = "arm,sbsa-gwdt" },
132         {}
133 };
134
135 U_BOOT_DRIVER(wdt_octeontx) = {
136         .name = "wdt_octeontx",
137         .id = UCLASS_WDT,
138         .of_match = octeontx_wdt_ids,
139         .ops = &octeontx_wdt_ops,
140         .priv_auto_alloc_size = sizeof(struct octeontx_wdt),
141         .probe = octeontx_wdt_probe,
142         .remove = octeontx_wdt_remove,
143         .flags = DM_FLAG_OS_PREPARE,
144 };