summaryrefslogtreecommitdiff
path: root/drivers/net/dsa/motorcomm/smi.c
blob: bf3adfd64165a5ab415f1c6b170feccba34660e0 (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
177
178
179
180
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2026 David Yang
 */

#include <linux/iopoll.h>

#include "chip.h"
#include "smi.h"

#define YT921X_POLL_SLEEP_US	10000
#define YT921X_POLL_TIMEOUT_US	100000

int yt921x_reg_read(struct yt921x_priv *priv, u32 reg, u32 *valp)
{
	lockdep_assert_held_once(&priv->reg_lock);

	return priv->reg_ops->read(priv->reg_ctx, reg, valp);
}

int yt921x_reg_write(struct yt921x_priv *priv, u32 reg, u32 val)
{
	lockdep_assert_held_once(&priv->reg_lock);

	return priv->reg_ops->write(priv->reg_ctx, reg, val);
}

int yt921x_reg_wait(struct yt921x_priv *priv, u32 reg, u32 mask, u32 *valp)
{
	u32 val;
	int res;
	int ret;

	ret = read_poll_timeout(yt921x_reg_read, res,
				res || (val & mask) == *valp,
				YT921X_POLL_SLEEP_US, YT921X_POLL_TIMEOUT_US,
				false, priv, reg, &val);
	if (ret)
		return ret;
	if (res)
		return res;

	*valp = val;
	return 0;
}

int yt921x_reg_update_bits(struct yt921x_priv *priv, u32 reg, u32 mask, u32 val)
{
	int res;
	u32 v;
	u32 u;

	res = yt921x_reg_read(priv, reg, &v);
	if (res)
		return res;

	u = v;
	u &= ~mask;
	u |= val;
	if (u == v)
		return 0;

	return yt921x_reg_write(priv, reg, u);
}

static int
yt921x_regs_read(struct yt921x_priv *priv, u32 reg, u32 *vals,
		 unsigned int num_regs)
{
	int res;

	for (unsigned int i = 0; i < num_regs; i++) {
		res = yt921x_reg_read(priv, reg + 4 * i, &vals[i]);
		if (res)
			return res;
	}

	return 0;
}

static int
yt921x_regs_write(struct yt921x_priv *priv, u32 reg, const u32 *vals,
		  unsigned int num_regs)
{
	int res;

	for (unsigned int i = 0; i < num_regs; i++) {
		res = yt921x_reg_write(priv, reg + 4 * i, vals[i]);
		if (res)
			return res;
	}

	return 0;
}

static int
yt921x_regs_update_bits(struct yt921x_priv *priv, u32 reg, const u32 *masks,
			const u32 *vals, unsigned int num_regs)
{
	bool changed = false;
	u32 vs[4];
	int res;

	BUILD_BUG_ON(num_regs > ARRAY_SIZE(vs));

	res = yt921x_regs_read(priv, reg, vs, num_regs);
	if (res)
		return res;

	for (unsigned int i = 0; i < num_regs; i++) {
		u32 u = vs[i];

		u &= ~masks[i];
		u |= vals[i];
		if (u != vs[i])
			changed = true;

		vs[i] = u;
	}

	if (!changed)
		return 0;

	return yt921x_regs_write(priv, reg, vs, num_regs);
}

static int
yt921x_regs_clear_bits(struct yt921x_priv *priv, u32 reg, const u32 *masks,
		       unsigned int num_regs)
{
	bool changed = false;
	u32 vs[4];
	int res;

	BUILD_BUG_ON(num_regs > ARRAY_SIZE(vs));

	res = yt921x_regs_read(priv, reg, vs, num_regs);
	if (res)
		return res;

	for (unsigned int i = 0; i < num_regs; i++) {
		u32 u = vs[i];

		u &= ~masks[i];
		if (u != vs[i])
			changed = true;

		vs[i] = u;
	}

	if (!changed)
		return 0;

	return yt921x_regs_write(priv, reg, vs, num_regs);
}

int
yt921x_reg64_write(struct yt921x_priv *priv, u32 reg, const u32 *vals)
{
	return yt921x_regs_write(priv, reg, vals, 2);
}

int
yt921x_reg64_update_bits(struct yt921x_priv *priv, u32 reg, const u32 *masks,
			 const u32 *vals)
{
	return yt921x_regs_update_bits(priv, reg, masks, vals, 2);
}

int
yt921x_reg64_clear_bits(struct yt921x_priv *priv, u32 reg, const u32 *masks)
{
	return yt921x_regs_clear_bits(priv, reg, masks, 2);
}

int
yt921x_reg96_write(struct yt921x_priv *priv, u32 reg, const u32 *vals)
{
	return yt921x_regs_write(priv, reg, vals, 3);
}