aboutsummaryrefslogtreecommitdiff
path: root/drivers/watchdog/octeontx_wdt.c
blob: c79d9539c13f64f3335dad038ce547e81d615e72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2019 Marvell International Ltd.
 *
 * https://spdx.org/licenses
 */

#include <clk.h>
#include <dm.h>
#include <errno.h>
#include <wdt.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include <linux/bitfield.h>

DECLARE_GLOBAL_DATA_PTR;

#define CORE0_POKE_OFFSET_MASK	0xfffffULL

#define WDOG_MODE		GENMASK_ULL(1, 0)
#define WDOG_LEN		GENMASK_ULL(19, 4)
#define WDOG_CNT		GENMASK_ULL(43, 20)

struct octeontx_wdt_data {
	u32 wdog_offset;
	u32 poke_offset;
	int timer_shift;
	bool has_clk;
};

struct octeontx_wdt {
	void __iomem *reg;
	const struct octeontx_wdt_data *data;
	struct clk clk;
};

static int octeontx_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
{
	struct octeontx_wdt *priv = dev_get_priv(dev);
	u64 clk_rate, val;
	u64 tout_wdog;

	if (priv->data->has_clk) {
		clk_rate = clk_get_rate(&priv->clk);
		if (IS_ERR_VALUE(clk_rate))
			return -EINVAL;
	} else {
		clk_rate = gd->bus_clk;
	}

	/* Watchdog counts in configured cycle steps */
	tout_wdog = (clk_rate * timeout_ms / 1000) >> priv->data->timer_shift;

	/*
	 * We can only specify the upper 16 bits of a 24 bit value.
	 * Round up
	 */
	tout_wdog = (tout_wdog + 0xff) >> 8;

	/* If the timeout overflows the hardware limit, set max */
	if (tout_wdog >= 0x10000)
		tout_wdog = 0xffff;

	val = FIELD_PREP(WDOG_MODE, 0x3) |
		FIELD_PREP(WDOG_LEN, tout_wdog) |
		FIELD_PREP(WDOG_CNT, tout_wdog << 8);
	writeq(val, priv->reg + priv->data->wdog_offset);

	return 0;
}

static int octeontx_wdt_stop(struct udevice *dev)
{
	struct octeontx_wdt *priv = dev_get_priv(dev);

	writeq(0, priv->reg + priv->data->wdog_offset);

	return 0;
}

static int octeontx_wdt_expire_now(struct udevice *dev, ulong flags)
{
	octeontx_wdt_stop(dev);

	/* Start with 100ms timeout to expire immediately */
	octeontx_wdt_start(dev, 100, flags);

	return 0;
}

static int octeontx_wdt_reset(struct udevice *dev)
{
	struct octeontx_wdt *priv = dev_get_priv(dev);

	writeq(~0ULL, priv->reg + priv->data->poke_offset);

	return 0;
}

static int octeontx_wdt_remove(struct udevice *dev)
{
	octeontx_wdt_stop(dev);

	return 0;
}

static int octeontx_wdt_probe(struct udevice *dev)
{
	struct octeontx_wdt *priv = dev_get_priv(dev);
	int ret;

	priv->reg = dev_remap_addr(dev);
	if (!priv->reg)
		return -EINVAL;

	priv->data = (void *)dev_get_driver_data(dev);
	if (!priv->data)
		return -EINVAL;

	/*
	 * Save base register address in reg masking lower 20 bits
	 * as 0xa0000 appears when extracted from the DT
	 */
	priv->reg = (void __iomem *)(((u64)priv->reg &
				      ~CORE0_POKE_OFFSET_MASK));

	if (priv->data->has_clk) {
		ret = clk_get_by_index(dev, 0, &priv->clk);
		if (ret < 0)
			return ret;

		ret = clk_enable(&priv->clk);
		if (ret)
			return ret;
	}

	return 0;
}

static const struct wdt_ops octeontx_wdt_ops = {
	.reset = octeontx_wdt_reset,
	.start = octeontx_wdt_start,
	.stop = octeontx_wdt_stop,
	.expire_now = octeontx_wdt_expire_now,
};

static const struct octeontx_wdt_data octeontx_data = {
	.wdog_offset = 0x40000,
	.poke_offset = 0x50000,
	.timer_shift = 10,
	.has_clk = true,
};

static const struct octeontx_wdt_data octeon_data = {
	.wdog_offset = 0x20000,
	.poke_offset = 0x30000,
	.timer_shift = 10,
	.has_clk = false,
};

static const struct udevice_id octeontx_wdt_ids[] = {
	{ .compatible = "arm,sbsa-gwdt", .data = (ulong)&octeontx_data },
	{ .compatible = "cavium,octeon-7890-ciu3", .data = (ulong)&octeon_data },
	{}
};

U_BOOT_DRIVER(wdt_octeontx) = {
	.name = "wdt_octeontx",
	.id = UCLASS_WDT,
	.of_match = octeontx_wdt_ids,
	.ops = &octeontx_wdt_ops,
	.priv_auto = sizeof(struct octeontx_wdt),
	.probe = octeontx_wdt_probe,
	.remove = octeontx_wdt_remove,
	.flags = DM_FLAG_OS_PREPARE,
};