blob: c79d9539c13f64f3335dad038ce547e81d615e72 [file] [log] [blame]
Suneel Garapati7072b172019-10-21 16:09:36 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Marvell International Ltd.
4 *
5 * https://spdx.org/licenses
6 */
7
Suneel Garapatibaba2482020-09-23 11:01:31 +02008#include <clk.h>
Suneel Garapati7072b172019-10-21 16:09:36 -07009#include <dm.h>
10#include <errno.h>
11#include <wdt.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Suneel Garapati7072b172019-10-21 16:09:36 -070013#include <asm/io.h>
Suneel Garapatibaba2482020-09-23 11:01:31 +020014#include <linux/bitfield.h>
Suneel Garapati7072b172019-10-21 16:09:36 -070015
16DECLARE_GLOBAL_DATA_PTR;
17
Suneel Garapati7072b172019-10-21 16:09:36 -070018#define CORE0_POKE_OFFSET_MASK 0xfffffULL
19
Suneel Garapatibaba2482020-09-23 11:01:31 +020020#define WDOG_MODE GENMASK_ULL(1, 0)
21#define WDOG_LEN GENMASK_ULL(19, 4)
22#define WDOG_CNT GENMASK_ULL(43, 20)
23
Stefan Roeseaf3b2102022-05-11 09:08:47 +020024struct octeontx_wdt_data {
25 u32 wdog_offset;
26 u32 poke_offset;
27 int timer_shift;
28 bool has_clk;
29};
30
Suneel Garapati7072b172019-10-21 16:09:36 -070031struct octeontx_wdt {
32 void __iomem *reg;
Stefan Roeseaf3b2102022-05-11 09:08:47 +020033 const struct octeontx_wdt_data *data;
Suneel Garapatibaba2482020-09-23 11:01:31 +020034 struct clk clk;
Suneel Garapati7072b172019-10-21 16:09:36 -070035};
36
Suneel Garapatibaba2482020-09-23 11:01:31 +020037static int octeontx_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
38{
39 struct octeontx_wdt *priv = dev_get_priv(dev);
40 u64 clk_rate, val;
41 u64 tout_wdog;
42
Stefan Roeseaf3b2102022-05-11 09:08:47 +020043 if (priv->data->has_clk) {
44 clk_rate = clk_get_rate(&priv->clk);
45 if (IS_ERR_VALUE(clk_rate))
46 return -EINVAL;
47 } else {
48 clk_rate = gd->bus_clk;
49 }
Suneel Garapatibaba2482020-09-23 11:01:31 +020050
Stefan Roeseaf3b2102022-05-11 09:08:47 +020051 /* Watchdog counts in configured cycle steps */
52 tout_wdog = (clk_rate * timeout_ms / 1000) >> priv->data->timer_shift;
Suneel Garapatibaba2482020-09-23 11:01:31 +020053
54 /*
55 * We can only specify the upper 16 bits of a 24 bit value.
56 * Round up
57 */
58 tout_wdog = (tout_wdog + 0xff) >> 8;
59
60 /* If the timeout overflows the hardware limit, set max */
61 if (tout_wdog >= 0x10000)
62 tout_wdog = 0xffff;
63
64 val = FIELD_PREP(WDOG_MODE, 0x3) |
65 FIELD_PREP(WDOG_LEN, tout_wdog) |
66 FIELD_PREP(WDOG_CNT, tout_wdog << 8);
Stefan Roeseaf3b2102022-05-11 09:08:47 +020067 writeq(val, priv->reg + priv->data->wdog_offset);
Suneel Garapatibaba2482020-09-23 11:01:31 +020068
69 return 0;
70}
71
72static int octeontx_wdt_stop(struct udevice *dev)
73{
74 struct octeontx_wdt *priv = dev_get_priv(dev);
75
Stefan Roeseaf3b2102022-05-11 09:08:47 +020076 writeq(0, priv->reg + priv->data->wdog_offset);
Suneel Garapatibaba2482020-09-23 11:01:31 +020077
78 return 0;
79}
80
81static int octeontx_wdt_expire_now(struct udevice *dev, ulong flags)
82{
83 octeontx_wdt_stop(dev);
84
85 /* Start with 100ms timeout to expire immediately */
86 octeontx_wdt_start(dev, 100, flags);
87
88 return 0;
89}
90
Suneel Garapati7072b172019-10-21 16:09:36 -070091static int octeontx_wdt_reset(struct udevice *dev)
92{
93 struct octeontx_wdt *priv = dev_get_priv(dev);
94
Stefan Roeseaf3b2102022-05-11 09:08:47 +020095 writeq(~0ULL, priv->reg + priv->data->poke_offset);
Suneel Garapatibaba2482020-09-23 11:01:31 +020096
97 return 0;
98}
99
100static int octeontx_wdt_remove(struct udevice *dev)
101{
102 octeontx_wdt_stop(dev);
Suneel Garapati7072b172019-10-21 16:09:36 -0700103
104 return 0;
105}
106
107static int octeontx_wdt_probe(struct udevice *dev)
108{
109 struct octeontx_wdt *priv = dev_get_priv(dev);
Suneel Garapatibaba2482020-09-23 11:01:31 +0200110 int ret;
Suneel Garapati7072b172019-10-21 16:09:36 -0700111
112 priv->reg = dev_remap_addr(dev);
113 if (!priv->reg)
114 return -EINVAL;
115
Stefan Roeseaf3b2102022-05-11 09:08:47 +0200116 priv->data = (void *)dev_get_driver_data(dev);
117 if (!priv->data)
118 return -EINVAL;
119
Suneel Garapati7072b172019-10-21 16:09:36 -0700120 /*
Suneel Garapatibaba2482020-09-23 11:01:31 +0200121 * Save base register address in reg masking lower 20 bits
122 * as 0xa0000 appears when extracted from the DT
Suneel Garapati7072b172019-10-21 16:09:36 -0700123 */
124 priv->reg = (void __iomem *)(((u64)priv->reg &
Suneel Garapatibaba2482020-09-23 11:01:31 +0200125 ~CORE0_POKE_OFFSET_MASK));
126
Stefan Roeseaf3b2102022-05-11 09:08:47 +0200127 if (priv->data->has_clk) {
128 ret = clk_get_by_index(dev, 0, &priv->clk);
129 if (ret < 0)
130 return ret;
Suneel Garapatibaba2482020-09-23 11:01:31 +0200131
Stefan Roeseaf3b2102022-05-11 09:08:47 +0200132 ret = clk_enable(&priv->clk);
133 if (ret)
134 return ret;
135 }
Suneel Garapati7072b172019-10-21 16:09:36 -0700136
137 return 0;
138}
139
140static const struct wdt_ops octeontx_wdt_ops = {
141 .reset = octeontx_wdt_reset,
Suneel Garapatibaba2482020-09-23 11:01:31 +0200142 .start = octeontx_wdt_start,
143 .stop = octeontx_wdt_stop,
144 .expire_now = octeontx_wdt_expire_now,
Suneel Garapati7072b172019-10-21 16:09:36 -0700145};
146
Stefan Roeseaf3b2102022-05-11 09:08:47 +0200147static const struct octeontx_wdt_data octeontx_data = {
148 .wdog_offset = 0x40000,
149 .poke_offset = 0x50000,
150 .timer_shift = 10,
151 .has_clk = true,
152};
153
154static const struct octeontx_wdt_data octeon_data = {
155 .wdog_offset = 0x20000,
156 .poke_offset = 0x30000,
157 .timer_shift = 10,
158 .has_clk = false,
159};
160
Suneel Garapati7072b172019-10-21 16:09:36 -0700161static const struct udevice_id octeontx_wdt_ids[] = {
Stefan Roeseaf3b2102022-05-11 09:08:47 +0200162 { .compatible = "arm,sbsa-gwdt", .data = (ulong)&octeontx_data },
163 { .compatible = "cavium,octeon-7890-ciu3", .data = (ulong)&octeon_data },
Suneel Garapati7072b172019-10-21 16:09:36 -0700164 {}
165};
166
167U_BOOT_DRIVER(wdt_octeontx) = {
168 .name = "wdt_octeontx",
169 .id = UCLASS_WDT,
170 .of_match = octeontx_wdt_ids,
171 .ops = &octeontx_wdt_ops,
Stefan Roeseaf3b2102022-05-11 09:08:47 +0200172 .priv_auto = sizeof(struct octeontx_wdt),
Suneel Garapati7072b172019-10-21 16:09:36 -0700173 .probe = octeontx_wdt_probe,
Suneel Garapatibaba2482020-09-23 11:01:31 +0200174 .remove = octeontx_wdt_remove,
175 .flags = DM_FLAG_OS_PREPARE,
Suneel Garapati7072b172019-10-21 16:09:36 -0700176};