summaryrefslogtreecommitdiffstats
path: root/drivers/clk/sophgo/clk-cv18xx-pll.c
blob: 29e24098bf5f91cf70341ea9f3a2101081939efd (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2023 Inochi Amaoto <inochiama@outlook.com>
 */

#include <linux/clk-provider.h>
#include <linux/io.h>
#include <linux/limits.h>
#include <linux/spinlock.h>

#include "clk-cv18xx-pll.h"

static inline struct cv1800_clk_pll *hw_to_cv1800_clk_pll(struct clk_hw *hw)
{
	struct cv1800_clk_common *common = hw_to_cv1800_clk_common(hw);

	return container_of(common, struct cv1800_clk_pll, common);
}

static unsigned long ipll_calc_rate(unsigned long parent_rate,
				    unsigned long pre_div_sel,
				    unsigned long div_sel,
				    unsigned long post_div_sel)
{
	uint64_t rate = parent_rate;

	rate *= div_sel;
	do_div(rate, pre_div_sel * post_div_sel);

	return rate;
}

static unsigned long ipll_recalc_rate(struct clk_hw *hw,
				      unsigned long parent_rate)
{
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);
	u32 value;

	value = readl(pll->common.base + pll->pll_reg);

	return ipll_calc_rate(parent_rate,
			      PLL_GET_PRE_DIV_SEL(value),
			      PLL_GET_DIV_SEL(value),
			      PLL_GET_POST_DIV_SEL(value));
}

static int ipll_find_rate(const struct cv1800_clk_pll_limit *limit,
			  unsigned long prate, unsigned long *rate,
			  u32 *value)
{
	unsigned long best_rate = 0;
	unsigned long trate = *rate;
	unsigned long pre_div_sel = 0, div_sel = 0, post_div_sel = 0;
	unsigned long pre, div, post;
	u32 detected = *value;
	unsigned long tmp;

	for_each_pll_limit_range(pre, &limit->pre_div) {
		for_each_pll_limit_range(div, &limit->div) {
			for_each_pll_limit_range(post, &limit->post_div) {
				tmp = ipll_calc_rate(prate, pre, div, post);

				if (tmp > trate)
					continue;

				if ((trate - tmp) < (trate - best_rate)) {
					best_rate = tmp;
					pre_div_sel = pre;
					div_sel = div;
					post_div_sel = post;
				}
			}
		}
	}

	if (best_rate) {
		detected = PLL_SET_PRE_DIV_SEL(detected, pre_div_sel);
		detected = PLL_SET_POST_DIV_SEL(detected, post_div_sel);
		detected = PLL_SET_DIV_SEL(detected, div_sel);
		*value = detected;
		*rate = best_rate;
		return 0;
	}

	return -EINVAL;
}

static int ipll_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
{
	u32 val;
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);

	return ipll_find_rate(pll->pll_limit, req->best_parent_rate,
			      &req->rate, &val);
}

static void pll_get_mode_ctrl(unsigned long div_sel,
			      bool (*mode_ctrl_check)(unsigned long,
						      unsigned long,
						      unsigned long),
			      const struct cv1800_clk_pll_limit *limit,
			      u32 *value)
{
	unsigned long ictrl = 0, mode = 0;
	u32 detected = *value;

	for_each_pll_limit_range(mode, &limit->mode) {
		for_each_pll_limit_range(ictrl, &limit->ictrl) {
			if (mode_ctrl_check(div_sel, ictrl, mode)) {
				detected = PLL_SET_SEL_MODE(detected, mode);
				detected = PLL_SET_ICTRL(detected, ictrl);
				*value = detected;
				return;
			}
		}
	}
}

static bool ipll_check_mode_ctrl_restrict(unsigned long div_sel,
					  unsigned long ictrl,
					  unsigned long mode)
{
	unsigned long left_rest = 20 * div_sel;
	unsigned long right_rest = 35 * div_sel;
	unsigned long test = 184 * (1 + mode) * (1 + ictrl) / 2;

	return test > left_rest && test <= right_rest;
}

static int ipll_set_rate(struct clk_hw *hw, unsigned long rate,
			 unsigned long parent_rate)
{
	u32 regval, detected = 0;
	unsigned long flags;
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);

	ipll_find_rate(pll->pll_limit, parent_rate, &rate, &detected);
	pll_get_mode_ctrl(PLL_GET_DIV_SEL(detected),
			  ipll_check_mode_ctrl_restrict,
			  pll->pll_limit, &detected);

	spin_lock_irqsave(pll->common.lock, flags);

	regval = readl(pll->common.base + pll->pll_reg);
	regval = PLL_COPY_REG(regval, detected);

	writel(regval, pll->common.base + pll->pll_reg);

	spin_unlock_irqrestore(pll->common.lock, flags);

	cv1800_clk_wait_for_lock(&pll->common, pll->pll_status.reg,
			      BIT(pll->pll_status.shift));

	return 0;
}

static int pll_enable(struct clk_hw *hw)
{
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);

	return cv1800_clk_clearbit(&pll->common, &pll->pll_pwd);
}

static void pll_disable(struct clk_hw *hw)
{
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);

	cv1800_clk_setbit(&pll->common, &pll->pll_pwd);
}

static int pll_is_enable(struct clk_hw *hw)
{
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);

	return cv1800_clk_checkbit(&pll->common, &pll->pll_pwd) == 0;
}

const struct clk_ops cv1800_clk_ipll_ops = {
	.disable = pll_disable,
	.enable = pll_enable,
	.is_enabled = pll_is_enable,

	.recalc_rate = ipll_recalc_rate,
	.determine_rate = ipll_determine_rate,
	.set_rate = ipll_set_rate,
};

#define PLL_SYN_FACTOR_DOT_POS		26
#define PLL_SYN_FACTOR_MINIMUM		((4 << PLL_SYN_FACTOR_DOT_POS) + 1)

static bool fpll_is_factional_mode(struct cv1800_clk_pll *pll)
{
	return cv1800_clk_checkbit(&pll->common, &pll->pll_syn->en);
}

static unsigned long fpll_calc_rate(unsigned long parent_rate,
				    unsigned long pre_div_sel,
				    unsigned long div_sel,
				    unsigned long post_div_sel,
				    unsigned long ssc_syn_set,
				    bool is_full_parent)
{
	u64 dividend = parent_rate * div_sel;
	u64 factor = ssc_syn_set * pre_div_sel * post_div_sel;
	unsigned long rate;

	dividend <<= PLL_SYN_FACTOR_DOT_POS - 1;
	rate = div64_u64_rem(dividend, factor, &dividend);

	if (is_full_parent) {
		dividend <<= 1;
		rate <<= 1;
	}

	rate += DIV64_U64_ROUND_CLOSEST(dividend, factor);

	return rate;
}

static unsigned long fpll_recalc_rate(struct clk_hw *hw,
					  unsigned long parent_rate)
{
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);
	u32 value;
	bool clk_full;
	u32 syn_set;

	if (!fpll_is_factional_mode(pll))
		return ipll_recalc_rate(hw, parent_rate);

	syn_set = readl(pll->common.base + pll->pll_syn->set);

	if (syn_set == 0)
		return 0;

	clk_full = cv1800_clk_checkbit(&pll->common,
					  &pll->pll_syn->clk_half);

	value = readl(pll->common.base + pll->pll_reg);

	return fpll_calc_rate(parent_rate,
			      PLL_GET_PRE_DIV_SEL(value),
			      PLL_GET_DIV_SEL(value),
			      PLL_GET_POST_DIV_SEL(value),
			      syn_set, clk_full);
}

static unsigned long fpll_find_synthesizer(unsigned long parent,
					   unsigned long rate,
					   unsigned long pre_div,
					   unsigned long div,
					   unsigned long post_div,
					   bool is_full_parent,
					   u32 *ssc_syn_set)
{
	u32 test_max = U32_MAX, test_min = PLL_SYN_FACTOR_MINIMUM;
	unsigned long trate;

	while (test_min < test_max) {
		u32 tssc = (test_max + test_min) / 2;

		trate = fpll_calc_rate(parent, pre_div, div, post_div,
				       tssc, is_full_parent);

		if (trate == rate) {
			test_min = tssc;
			break;
		}

		if (trate > rate)
			test_min = tssc + 1;
		else
			test_max = tssc - 1;
	}

	if (trate != 0)
		*ssc_syn_set = test_min;

	return trate;
}

static int fpll_find_rate(struct cv1800_clk_pll *pll,
			  const struct cv1800_clk_pll_limit *limit,
			  unsigned long prate,
			  unsigned long *rate,
			  u32 *value, u32 *ssc_syn_set)
{
	unsigned long best_rate = 0;
	unsigned long pre_div_sel = 0, div_sel = 0, post_div_sel = 0;
	unsigned long pre, div, post;
	unsigned long trate = *rate;
	u32 detected = *value;
	unsigned long tmp;
	bool clk_full = cv1800_clk_checkbit(&pll->common,
					       &pll->pll_syn->clk_half);

	for_each_pll_limit_range(pre, &limit->pre_div) {
		for_each_pll_limit_range(post, &limit->post_div) {
			for_each_pll_limit_range(div, &limit->div) {
				tmp = fpll_find_synthesizer(prate, trate,
							    pre, div, post,
							    clk_full,
							    ssc_syn_set);

				if ((trate - tmp) < (trate - best_rate)) {
					best_rate = tmp;
					pre_div_sel = pre;
					div_sel = div;
					post_div_sel = post;
				}
			}
		}
	}

	if (best_rate) {
		detected = PLL_SET_PRE_DIV_SEL(detected, pre_div_sel);
		detected = PLL_SET_POST_DIV_SEL(detected, post_div_sel);
		detected = PLL_SET_DIV_SEL(detected, div_sel);
		*value = detected;
		*rate = best_rate;
		return 0;
	}

	return -EINVAL;
}

static int fpll_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
{
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);
	u32 val, ssc_syn_set;

	if (!fpll_is_factional_mode(pll))
		return ipll_determine_rate(hw, req);

	fpll_find_rate(pll, &pll->pll_limit[2], req->best_parent_rate,
		       &req->rate, &val, &ssc_syn_set);

	return 0;
}

static bool fpll_check_mode_ctrl_restrict(unsigned long div_sel,
					  unsigned long ictrl,
					  unsigned long mode)
{
	unsigned long left_rest = 10 * div_sel;
	unsigned long right_rest = 24 * div_sel;
	unsigned long test = 184 * (1 + mode) * (1 + ictrl) / 2;

	return test > left_rest && test <= right_rest;
}

static int fpll_set_rate(struct clk_hw *hw, unsigned long rate,
			 unsigned long parent_rate)
{
	u32 regval;
	u32 detected = 0, detected_ssc = 0;
	unsigned long flags;
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);

	if (!fpll_is_factional_mode(pll))
		return ipll_set_rate(hw, rate, parent_rate);

	fpll_find_rate(pll, &pll->pll_limit[2], parent_rate,
		       &rate, &detected, &detected_ssc);
	pll_get_mode_ctrl(PLL_GET_DIV_SEL(detected),
			  fpll_check_mode_ctrl_restrict,
			  pll->pll_limit, &detected);

	spin_lock_irqsave(pll->common.lock, flags);

	writel(detected_ssc, pll->common.base + pll->pll_syn->set);

	regval = readl(pll->common.base + pll->pll_reg);
	regval = PLL_COPY_REG(regval, detected);

	writel(regval, pll->common.base + pll->pll_reg);

	spin_unlock_irqrestore(pll->common.lock, flags);

	cv1800_clk_wait_for_lock(&pll->common, pll->pll_status.reg,
			      BIT(pll->pll_status.shift));

	return 0;
}

static u8 fpll_get_parent(struct clk_hw *hw)
{
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);

	if (fpll_is_factional_mode(pll))
		return 1;

	return 0;
}

static int fpll_set_parent(struct clk_hw *hw, u8 index)
{
	struct cv1800_clk_pll *pll = hw_to_cv1800_clk_pll(hw);

	if (index)
		cv1800_clk_setbit(&pll->common, &pll->pll_syn->en);
	else
		cv1800_clk_clearbit(&pll->common, &pll->pll_syn->en);

	return 0;
}

const struct clk_ops cv1800_clk_fpll_ops = {
	.disable = pll_disable,
	.enable = pll_enable,
	.is_enabled = pll_is_enable,

	.recalc_rate = fpll_recalc_rate,
	.determine_rate = fpll_determine_rate,
	.set_rate = fpll_set_rate,

	.set_parent = fpll_set_parent,
	.get_parent = fpll_get_parent,
};