summaryrefslogtreecommitdiff
path: root/drivers/clk/starfive/clk-starfive-jh71x0.c
blob: b372083d11c344e4177a4ec935f9403d68c0d2e9 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// SPDX-License-Identifier: GPL-2.0
/*
 * StarFive JH71X0 Clock Generator Driver
 *
 * Copyright (C) 2021-2022 Emil Renner Berthing <kernel@esmil.dk>
 */

#include <linux/clk-provider.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/io.h>

#include "clk-starfive-jh71x0.h"

static struct jh71x0_clk *jh71x0_clk_from(struct clk_hw *hw)
{
	return container_of(hw, struct jh71x0_clk, hw);
}

static struct jh71x0_clk_priv *jh71x0_priv_from(struct jh71x0_clk *clk)
{
	return container_of(clk, struct jh71x0_clk_priv, reg[clk->idx]);
}

static u32 jh71x0_clk_reg_get(struct jh71x0_clk *clk)
{
	struct jh71x0_clk_priv *priv = jh71x0_priv_from(clk);
	void __iomem *reg = priv->base + 4 * clk->idx;

	return readl_relaxed(reg);
}

static void jh71x0_clk_reg_rmw(struct jh71x0_clk *clk, u32 mask, u32 value)
{
	struct jh71x0_clk_priv *priv = jh71x0_priv_from(clk);
	void __iomem *reg = priv->base + 4 * clk->idx;
	unsigned long flags;

	spin_lock_irqsave(&priv->rmw_lock, flags);
	value |= readl_relaxed(reg) & ~mask;
	writel_relaxed(value, reg);
	spin_unlock_irqrestore(&priv->rmw_lock, flags);
}

static int jh71x0_clk_enable(struct clk_hw *hw)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);

	jh71x0_clk_reg_rmw(clk, JH71X0_CLK_ENABLE, JH71X0_CLK_ENABLE);
	return 0;
}

static void jh71x0_clk_disable(struct clk_hw *hw)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);

	jh71x0_clk_reg_rmw(clk, JH71X0_CLK_ENABLE, 0);
}

static int jh71x0_clk_is_enabled(struct clk_hw *hw)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);

	return !!(jh71x0_clk_reg_get(clk) & JH71X0_CLK_ENABLE);
}

static unsigned long jh71x0_clk_recalc_rate(struct clk_hw *hw,
					    unsigned long parent_rate)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	u32 div = jh71x0_clk_reg_get(clk) & JH71X0_CLK_DIV_MASK;

	return div ? parent_rate / div : 0;
}

static int jh71x0_clk_determine_rate(struct clk_hw *hw,
				     struct clk_rate_request *req)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	unsigned long parent = req->best_parent_rate;
	unsigned long rate = clamp(req->rate, req->min_rate, req->max_rate);
	unsigned long div = min_t(unsigned long, DIV_ROUND_UP(parent, rate), clk->max_div);
	unsigned long result = parent / div;

	/*
	 * we want the result clamped by min_rate and max_rate if possible:
	 * case 1: div hits the max divider value, which means it's less than
	 * parent / rate, so the result is greater than rate and min_rate in
	 * particular. we can't do anything about result > max_rate because the
	 * divider doesn't go any further.
	 * case 2: div = DIV_ROUND_UP(parent, rate) which means the result is
	 * always lower or equal to rate and max_rate. however the result may
	 * turn out lower than min_rate, but then the next higher rate is fine:
	 *   div - 1 = ceil(parent / rate) - 1 < parent / rate
	 * and thus
	 *   min_rate <= rate < parent / (div - 1)
	 */
	if (result < req->min_rate && div > 1)
		result = parent / (div - 1);

	req->rate = result;
	return 0;
}

static int jh71x0_clk_set_rate(struct clk_hw *hw,
			       unsigned long rate,
			       unsigned long parent_rate)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	unsigned long div = clamp(DIV_ROUND_CLOSEST(parent_rate, rate),
				  1UL, (unsigned long)clk->max_div);

	jh71x0_clk_reg_rmw(clk, JH71X0_CLK_DIV_MASK, div);
	return 0;
}

static unsigned long jh71x0_clk_frac_recalc_rate(struct clk_hw *hw,
						 unsigned long parent_rate)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	u32 reg = jh71x0_clk_reg_get(clk);
	unsigned long div100 = 100 * (reg & JH71X0_CLK_INT_MASK) +
			       ((reg & JH71X0_CLK_FRAC_MASK) >> JH71X0_CLK_FRAC_SHIFT);

	return (div100 >= JH71X0_CLK_FRAC_MIN) ? 100 * parent_rate / div100 : 0;
}

static int jh71x0_clk_frac_determine_rate(struct clk_hw *hw,
					  struct clk_rate_request *req)
{
	unsigned long parent100 = 100 * req->best_parent_rate;
	unsigned long rate = clamp(req->rate, req->min_rate, req->max_rate);
	unsigned long div100 = clamp(DIV_ROUND_CLOSEST(parent100, rate),
				     JH71X0_CLK_FRAC_MIN, JH71X0_CLK_FRAC_MAX);
	unsigned long result = parent100 / div100;

	/* clamp the result as in jh71x0_clk_determine_rate() above */
	if (result > req->max_rate && div100 < JH71X0_CLK_FRAC_MAX)
		result = parent100 / (div100 + 1);
	if (result < req->min_rate && div100 > JH71X0_CLK_FRAC_MIN)
		result = parent100 / (div100 - 1);

	req->rate = result;
	return 0;
}

static int jh71x0_clk_frac_set_rate(struct clk_hw *hw,
				    unsigned long rate,
				    unsigned long parent_rate)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	unsigned long div100 = clamp(DIV_ROUND_CLOSEST(100 * parent_rate, rate),
				     JH71X0_CLK_FRAC_MIN, JH71X0_CLK_FRAC_MAX);
	u32 value = ((div100 % 100) << JH71X0_CLK_FRAC_SHIFT) | (div100 / 100);

	jh71x0_clk_reg_rmw(clk, JH71X0_CLK_DIV_MASK, value);
	return 0;
}

static u8 jh71x0_clk_get_parent(struct clk_hw *hw)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	u32 value = jh71x0_clk_reg_get(clk);

	return (value & JH71X0_CLK_MUX_MASK) >> JH71X0_CLK_MUX_SHIFT;
}

static int jh71x0_clk_set_parent(struct clk_hw *hw, u8 index)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	u32 value = (u32)index << JH71X0_CLK_MUX_SHIFT;

	jh71x0_clk_reg_rmw(clk, JH71X0_CLK_MUX_MASK, value);
	return 0;
}

static int jh71x0_clk_mux_determine_rate(struct clk_hw *hw,
					 struct clk_rate_request *req)
{
	return clk_mux_determine_rate_flags(hw, req, 0);
}

static int jh71x0_clk_get_phase(struct clk_hw *hw)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	u32 value = jh71x0_clk_reg_get(clk);

	return (value & JH71X0_CLK_INVERT) ? 180 : 0;
}

static int jh71x0_clk_set_phase(struct clk_hw *hw, int degrees)
{
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	u32 value;

	if (degrees == 0)
		value = 0;
	else if (degrees == 180)
		value = JH71X0_CLK_INVERT;
	else
		return -EINVAL;

	jh71x0_clk_reg_rmw(clk, JH71X0_CLK_INVERT, value);
	return 0;
}

#ifdef CONFIG_DEBUG_FS
static void jh71x0_clk_debug_init(struct clk_hw *hw, struct dentry *dentry)
{
	static const struct debugfs_reg32 jh71x0_clk_reg = {
		.name = "CTRL",
		.offset = 0,
	};
	struct jh71x0_clk *clk = jh71x0_clk_from(hw);
	struct jh71x0_clk_priv *priv = jh71x0_priv_from(clk);
	struct debugfs_regset32 *regset;

	regset = devm_kzalloc(priv->dev, sizeof(*regset), GFP_KERNEL);
	if (!regset)
		return;

	regset->regs = &jh71x0_clk_reg;
	regset->nregs = 1;
	regset->base = priv->base + 4 * clk->idx;

	debugfs_create_regset32("registers", 0400, dentry, regset);
}
#else
#define jh71x0_clk_debug_init NULL
#endif

static const struct clk_ops jh71x0_clk_gate_ops = {
	.enable = jh71x0_clk_enable,
	.disable = jh71x0_clk_disable,
	.is_enabled = jh71x0_clk_is_enabled,
	.debug_init = jh71x0_clk_debug_init,
};

static const struct clk_ops jh71x0_clk_div_ops = {
	.recalc_rate = jh71x0_clk_recalc_rate,
	.determine_rate = jh71x0_clk_determine_rate,
	.set_rate = jh71x0_clk_set_rate,
	.debug_init = jh71x0_clk_debug_init,
};

static const struct clk_ops jh71x0_clk_fdiv_ops = {
	.recalc_rate = jh71x0_clk_frac_recalc_rate,
	.determine_rate = jh71x0_clk_frac_determine_rate,
	.set_rate = jh71x0_clk_frac_set_rate,
	.debug_init = jh71x0_clk_debug_init,
};

static const struct clk_ops jh71x0_clk_gdiv_ops = {
	.enable = jh71x0_clk_enable,
	.disable = jh71x0_clk_disable,
	.is_enabled = jh71x0_clk_is_enabled,
	.recalc_rate = jh71x0_clk_recalc_rate,
	.determine_rate = jh71x0_clk_determine_rate,
	.set_rate = jh71x0_clk_set_rate,
	.debug_init = jh71x0_clk_debug_init,
};

static const struct clk_ops jh71x0_clk_mux_ops = {
	.determine_rate = jh71x0_clk_mux_determine_rate,
	.set_parent = jh71x0_clk_set_parent,
	.get_parent = jh71x0_clk_get_parent,
	.debug_init = jh71x0_clk_debug_init,
};

static const struct clk_ops jh71x0_clk_gmux_ops = {
	.enable = jh71x0_clk_enable,
	.disable = jh71x0_clk_disable,
	.is_enabled = jh71x0_clk_is_enabled,
	.determine_rate = jh71x0_clk_mux_determine_rate,
	.set_parent = jh71x0_clk_set_parent,
	.get_parent = jh71x0_clk_get_parent,
	.debug_init = jh71x0_clk_debug_init,
};

static const struct clk_ops jh71x0_clk_mdiv_ops = {
	.recalc_rate = jh71x0_clk_recalc_rate,
	.determine_rate = jh71x0_clk_determine_rate,
	.get_parent = jh71x0_clk_get_parent,
	.set_parent = jh71x0_clk_set_parent,
	.set_rate = jh71x0_clk_set_rate,
	.debug_init = jh71x0_clk_debug_init,
};

static const struct clk_ops jh71x0_clk_gmd_ops = {
	.enable = jh71x0_clk_enable,
	.disable = jh71x0_clk_disable,
	.is_enabled = jh71x0_clk_is_enabled,
	.recalc_rate = jh71x0_clk_recalc_rate,
	.determine_rate = jh71x0_clk_determine_rate,
	.get_parent = jh71x0_clk_get_parent,
	.set_parent = jh71x0_clk_set_parent,
	.set_rate = jh71x0_clk_set_rate,
	.debug_init = jh71x0_clk_debug_init,
};

static const struct clk_ops jh71x0_clk_inv_ops = {
	.get_phase = jh71x0_clk_get_phase,
	.set_phase = jh71x0_clk_set_phase,
	.debug_init = jh71x0_clk_debug_init,
};

const struct clk_ops *starfive_jh71x0_clk_ops(u32 max)
{
	if (max & JH71X0_CLK_DIV_MASK) {
		if (max & JH71X0_CLK_MUX_MASK) {
			if (max & JH71X0_CLK_ENABLE)
				return &jh71x0_clk_gmd_ops;
			return &jh71x0_clk_mdiv_ops;
		}
		if (max & JH71X0_CLK_ENABLE)
			return &jh71x0_clk_gdiv_ops;
		if (max == JH71X0_CLK_FRAC_MAX)
			return &jh71x0_clk_fdiv_ops;
		return &jh71x0_clk_div_ops;
	}

	if (max & JH71X0_CLK_MUX_MASK) {
		if (max & JH71X0_CLK_ENABLE)
			return &jh71x0_clk_gmux_ops;
		return &jh71x0_clk_mux_ops;
	}

	if (max & JH71X0_CLK_ENABLE)
		return &jh71x0_clk_gate_ops;

	return &jh71x0_clk_inv_ops;
}
EXPORT_SYMBOL_GPL(starfive_jh71x0_clk_ops);