~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 8d71f2a24b82f27531e8972edb60109fa8cb487b (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2022 Nuvoton Technology Corp.
 *
 * Formula for calculating clock rate:
 * Fout = ((Fin / PRE_DIV) / div) / POST_DIV
 */

#include <div64.h>
#include <dm.h>
#include <asm/io.h>
#include <linux/bitfield.h>
#include <linux/log2.h>
#include "clk_npcm.h"

static int clkid_to_clksel(struct npcm_clk_select *selector, int id)
{
	int i;

	for (i = 0; i < selector->num_parents; i++) {
		if (selector->parents[i].id == id)
			return selector->parents[i].clksel;
	}

	return -EINVAL;
}

static int clksel_to_clkid(struct npcm_clk_select *selector, int clksel)
{
	int i;

	for (i = 0; i < selector->num_parents; i++) {
		if (selector->parents[i].clksel == clksel)
			return selector->parents[i].id;
	}

	return -EINVAL;
}

static struct npcm_clk_pll *npcm_clk_pll_get(struct npcm_clk_data *clk_data, int id)
{
	struct npcm_clk_pll *pll = clk_data->clk_plls;
	int i;

	for (i = 0; i < clk_data->num_plls; i++) {
		if (pll->id == id)
			return pll;
		pll++;
	}

	return NULL;
}

static struct npcm_clk_select *npcm_clk_selector_get(struct npcm_clk_data *clk_data,
						     int id)
{
	struct npcm_clk_select *selector = clk_data->clk_selectors;
	int i;

	for (i = 0; i < clk_data->num_selectors; i++) {
		if (selector->id == id)
			return selector;
		selector++;
	}

	return NULL;
}

static struct npcm_clk_div *npcm_clk_divider_get(struct npcm_clk_data *clk_data,
						 int id)
{
	struct npcm_clk_div *divider = clk_data->clk_dividers;
	int i;

	for (i = 0; i < clk_data->num_dividers; i++) {
		if (divider->id == id)
			return divider;
		divider++;
	}

	return NULL;
}

static ulong npcm_clk_get_fin(struct clk *clk)
{
	struct npcm_clk_priv *priv = dev_get_priv(clk->dev);
	struct npcm_clk_select *selector;
	struct clk parent;
	ulong parent_rate;
	u32 val, clksel;
	int ret;

	selector = npcm_clk_selector_get(priv->clk_data, clk->id);
	if (!selector)
		return 0;

	if (selector->flags & FIXED_PARENT) {
		clksel = 0;
	} else {
		val = readl(priv->base + selector->reg);
		clksel = (val & selector->mask) >> (ffs(selector->mask) - 1);
	}
	parent.id = clksel_to_clkid(selector, clksel);

	ret = clk_request(clk->dev, &parent);
	if (ret)
		return 0;

	parent_rate = clk_get_rate(&parent);

	debug("fin of clk%lu = %lu\n", clk->id, parent_rate);
	return parent_rate;
}

static u32 npcm_clk_get_div(struct clk *clk)
{
	struct npcm_clk_priv *priv = dev_get_priv(clk->dev);
	struct npcm_clk_div *divider;
	u32 val, div;

	divider = npcm_clk_divider_get(priv->clk_data, clk->id);
	if (!divider)
		return 0;

	val = readl(priv->base + divider->reg);
	div = (val & divider->mask) >> (ffs(divider->mask) - 1);
	if (divider->flags & DIV_TYPE1)
		div = div + 1;
	else
		div = 1 << div;

	if (divider->flags & PRE_DIV2)
		div = div << 1;

	return div;
}

static u32 npcm_clk_set_div(struct clk *clk, u32 div)
{
	struct npcm_clk_priv *priv = dev_get_priv(clk->dev);
	struct npcm_clk_div *divider;
	u32 val, clkdiv;

	divider = npcm_clk_divider_get(priv->clk_data, clk->id);
	if (!divider)
		return -EINVAL;

	if (divider->flags & PRE_DIV2)
		div = div >> 1;

	if (divider->flags & DIV_TYPE1)
		clkdiv = div - 1;
	else
		clkdiv = ilog2(div);

	val = readl(priv->base + divider->reg);
	val &= ~divider->mask;
	val |= (clkdiv << (ffs(divider->mask) - 1)) & divider->mask;
	writel(val, priv->base + divider->reg);

	return 0;
}

static ulong npcm_clk_get_fout(struct clk *clk)
{
	ulong parent_rate;
	u32 div;

	parent_rate = npcm_clk_get_fin(clk);
	if (!parent_rate)
		return -EINVAL;

	div = npcm_clk_get_div(clk);
	if (!div)
		return -EINVAL;

	debug("fout of clk%lu = (%lu / %u)\n", clk->id, parent_rate, div);
	return (parent_rate / div);
}

static ulong npcm_clk_get_pll_fout(struct clk *clk)
{
	struct npcm_clk_priv *priv = dev_get_priv(clk->dev);
	struct npcm_clk_pll *pll;
	struct clk parent;
	ulong parent_rate;
	ulong fbdv, indv, otdv1, otdv2;
	u32 val;
	u64 ret;

	pll = npcm_clk_pll_get(priv->clk_data, clk->id);
	if (!pll)
		return -ENODEV;

	parent.id = pll->parent_id;
	ret = clk_request(clk->dev, &parent);
	if (ret)
		return ret;

	parent_rate = clk_get_rate(&parent);

	val = readl(priv->base + pll->reg);
	indv = FIELD_GET(PLLCON_INDV, val);
	fbdv = FIELD_GET(PLLCON_FBDV, val);
	otdv1 = FIELD_GET(PLLCON_OTDV1, val);
	otdv2 = FIELD_GET(PLLCON_OTDV2, val);

	ret = (u64)parent_rate * fbdv;
	do_div(ret, indv * otdv1 * otdv2);
	if (pll->flags & POST_DIV2)
		do_div(ret, 2);

	debug("fout of pll(id %lu) = %llu\n", clk->id, ret);
	return ret;
}

static ulong npcm_clk_get_rate(struct clk *clk)
{
	struct npcm_clk_priv *priv = dev_get_priv(clk->dev);
	struct npcm_clk_data *clk_data = priv->clk_data;
	struct clk refclk;
	int ret;

	debug("%s: id %lu\n", __func__, clk->id);
	if (clk->id == clk_data->refclk_id) {
		ret = clk_get_by_name(clk->dev, "refclk", &refclk);
		if (!ret)
			return clk_get_rate(&refclk);
		else
			return ret;
	}

	if (clk->id >= clk_data->pll0_id &&
	    clk->id < clk_data->pll0_id + clk_data->num_plls)
		return npcm_clk_get_pll_fout(clk);
	else
		return npcm_clk_get_fout(clk);
}

static ulong npcm_clk_set_rate(struct clk *clk, ulong rate)
{
	ulong parent_rate;
	u32 div;
	int ret;

	debug("%s: id %lu, rate %lu\n", __func__, clk->id, rate);
	parent_rate = npcm_clk_get_fin(clk);
	if (!parent_rate)
		return -EINVAL;

	div = DIV_ROUND_UP(parent_rate, rate);
	ret = npcm_clk_set_div(clk, div);
	if (ret)
		return ret;

	debug("%s: rate %lu, new rate (%lu / %u)\n", __func__, rate, parent_rate, div);
	return (parent_rate / div);
}

static int npcm_clk_set_parent(struct clk *clk, struct clk *parent)
{
	struct npcm_clk_priv *priv = dev_get_priv(clk->dev);
	struct npcm_clk_select *selector;
	int clksel;
	u32 val;

	debug("%s: id %lu, parent %lu\n", __func__, clk->id, parent->id);
	selector = npcm_clk_selector_get(priv->clk_data, clk->id);
	if (!selector)
		return -EINVAL;

	clksel = clkid_to_clksel(selector, parent->id);
	if (clksel < 0)
		return -EINVAL;

	val = readl(priv->base + selector->reg);
	val &= ~selector->mask;
	val |= clksel << (ffs(selector->mask) - 1);
	writel(val, priv->base + selector->reg);

	return 0;
}

static int npcm_clk_request(struct clk *clk)
{
	struct npcm_clk_priv *priv = dev_get_priv(clk->dev);

	if (clk->id >= priv->num_clks)
		return -EINVAL;

	return 0;
}

const struct clk_ops npcm_clk_ops = {
	.get_rate = npcm_clk_get_rate,
	.set_rate = npcm_clk_set_rate,
	.set_parent = npcm_clk_set_parent,
	.request = npcm_clk_request,
};