~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 1568a1f4cd912e681c0c0af7ad4d599ab49fb2aa (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
420
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2022-23 StarFive Technology Co., Ltd.
 *
 * Author:	Yanhong Wang <yanhong.wang@starfivetech.com>
 *		Xingyu Wu <xingyu.wu@starfivetech.com>
 */

#include <common.h>
#include <asm/io.h>
#include <malloc.h>
#include <clk-uclass.h>
#include <div64.h>
#include <dm/device.h>
#include <dm/read.h>
#include <dt-bindings/clock/starfive,jh7110-crg.h>
#include <linux/bitops.h>
#include <linux/clk-provider.h>
#include <linux/delay.h>
#include <linux/err.h>

#include "clk.h"

#define UBOOT_DM_CLK_JH7110_PLLX "jh7110_clk_pllx"

#define PLL_PD_OFF		1
#define PLL_PD_ON		0

#define CLK_DDR_BUS_MASK	GENMASK(29, 24)
#define CLK_DDR_BUS_OFFSET	0xAC
#define CLK_DDR_BUS_OSC_DIV2	0
#define CLK_DDR_BUS_PLL1_DIV2	1
#define CLK_DDR_BUS_PLL1_DIV4	2
#define CLK_DDR_BUS_PLL1_DIV8	3

#define JH7110_PLL_ID_TRANS(id)	((id) + JH7110_EXTCLK_END)

enum starfive_pll_type {
	PLL0 = 0,
	PLL1,
	PLL2,
	PLL_MAX = PLL2
};

struct starfive_pllx_rate {
	u64 rate;
	u32 prediv;
	u32 fbdiv;
	u32 frac;
};

struct starfive_pllx_offset {
	u32 pd;
	u32 prediv;
	u32 fbdiv;
	u32 frac;
	u32 postdiv1;
	u32 dacpd;
	u32 dsmpd;
	u32 pd_mask;
	u32 prediv_mask;
	u32 fbdiv_mask;
	u32 frac_mask;
	u32 postdiv1_mask;
	u32 dacpd_mask;
	u32 dsmpd_mask;
};

struct starfive_pllx_clk {
	enum starfive_pll_type type;
	const struct starfive_pllx_offset *offset;
	const struct starfive_pllx_rate *rate_table;
	int rate_count;
	int flags;
};

struct clk_jh7110_pllx {
	struct clk		clk;
	void __iomem	*base;
	void __iomem	*sysreg;
	enum starfive_pll_type	type;
	const struct starfive_pllx_offset *offset;
	const struct starfive_pllx_rate *rate_table;
	int rate_count;
};

#define getbits_le32(addr, mask) ((in_le32(addr) & (mask)) >> __ffs((mask)))

#define PLLX_SET(offset, mask, val) do {\
		reg = readl((ulong *)((ulong)pll->base + (offset))); \
		reg &= ~(mask); \
		reg |= (mask) & ((val) << __ffs(mask)); \
		writel(reg, (ulong *)((ulong)pll->base + (offset))); \
	} while (0)

#define PLLX_RATE(_rate, _pd, _fd)	\
	{						\
		.rate		= (_rate),	\
		.prediv		= (_pd),	\
		.fbdiv		= (_fd),	\
	}

#define to_clk_pllx(_clk) container_of(_clk, struct clk_jh7110_pllx, clk)

static const struct starfive_pllx_rate jh7110_pll0_tbl[] = {
	PLLX_RATE(375000000UL, 8, 125),
	PLLX_RATE(500000000UL, 6, 125),
	PLLX_RATE(625000000UL, 24, 625),
	PLLX_RATE(750000000UL, 4, 125),
	PLLX_RATE(875000000UL, 24, 875),
	PLLX_RATE(1000000000UL, 3, 125),
	PLLX_RATE(1250000000UL, 12, 625),
	PLLX_RATE(1375000000UL, 24, 1375),
	PLLX_RATE(1500000000UL, 2, 125),
	PLLX_RATE(1625000000UL, 24, 1625),
	PLLX_RATE(1750000000UL, 12, 875),
	PLLX_RATE(1800000000UL, 3, 225),
};

static const struct starfive_pllx_rate jh7110_pll1_tbl[] = {
	PLLX_RATE(1066000000UL, 12, 533),
	PLLX_RATE(1200000000UL, 1, 50),
	PLLX_RATE(1400000000UL, 6, 350),
	PLLX_RATE(1600000000UL, 3, 200),
};

static const struct starfive_pllx_rate jh7110_pll2_tbl[] = {
	PLLX_RATE(1228800000UL, 15, 768),
	PLLX_RATE(1188000000UL, 2, 99),
};

static const struct starfive_pllx_offset jh7110_pll0_offset = {
	.pd = 0x20,
	.prediv = 0x24,
	.fbdiv = 0x1c,
	.frac = 0x20,
	.postdiv1 = 0x20,
	.dacpd = 0x18,
	.dsmpd = 0x18,
	.pd_mask = BIT(27),
	.prediv_mask = GENMASK(5, 0),
	.fbdiv_mask = GENMASK(11, 0),
	.frac_mask = GENMASK(23, 0),
	.postdiv1_mask = GENMASK(29, 28),
	.dacpd_mask = BIT(24),
	.dsmpd_mask = BIT(25)
};

static const struct starfive_pllx_offset jh7110_pll1_offset = {
	.pd = 0x28,
	.prediv = 0x2c,
	.fbdiv = 0x24,
	.frac = 0x28,
	.postdiv1 = 0x28,
	.dacpd = 0x24,
	.dsmpd = 0x24,
	.pd_mask = BIT(27),
	.prediv_mask = GENMASK(5, 0),
	.fbdiv_mask = GENMASK(28, 17),
	.frac_mask = GENMASK(23, 0),
	.postdiv1_mask = GENMASK(29, 28),
	.dacpd_mask = BIT(15),
	.dsmpd_mask = BIT(16)
};

static const struct starfive_pllx_offset jh7110_pll2_offset = {
	.pd = 0x30,
	.prediv = 0x34,
	.fbdiv = 0x2c,
	.frac = 0x30,
	.postdiv1 = 0x30,
	.dacpd = 0x2c,
	.dsmpd = 0x2c,
	.pd_mask = BIT(27),
	.prediv_mask = GENMASK(5, 0),
	.fbdiv_mask = GENMASK(28, 17),
	.frac_mask = GENMASK(23, 0),
	.postdiv1_mask = GENMASK(29, 28),
	.dacpd_mask = BIT(15),
	.dsmpd_mask = BIT(16)
};

struct starfive_pllx_clk starfive_jh7110_pll0 __initdata = {
	.type = PLL0,
	.offset = &jh7110_pll0_offset,
	.rate_table = jh7110_pll0_tbl,
	.rate_count = ARRAY_SIZE(jh7110_pll0_tbl),
};

struct starfive_pllx_clk starfive_jh7110_pll1 __initdata = {
	.type = PLL1,
	.offset = &jh7110_pll1_offset,
	.rate_table = jh7110_pll1_tbl,
	.rate_count = ARRAY_SIZE(jh7110_pll1_tbl),
};

struct starfive_pllx_clk starfive_jh7110_pll2 __initdata = {
	.type = PLL2,
	.offset = &jh7110_pll2_offset,
	.rate_table = jh7110_pll2_tbl,
	.rate_count = ARRAY_SIZE(jh7110_pll2_tbl),
};

static const struct starfive_pllx_rate *
jh7110_get_pll_settings(struct clk_jh7110_pllx *pll, unsigned long rate)
{
	for (int i = 0; i < pll->rate_count; i++)
		if (rate == pll->rate_table[i].rate)
			return &pll->rate_table[i];

	return NULL;
}

static void jh7110_pll_set_rate(struct clk_jh7110_pllx *pll,
				const struct starfive_pllx_rate *rate)
{
	u32 reg;
	bool set = (pll->type == PLL1) ? true : false;

	if (set) {
		reg = readl((ulong *)((ulong)pll->sysreg + CLK_DDR_BUS_OFFSET));
		reg &= ~CLK_DDR_BUS_MASK;
		reg |= CLK_DDR_BUS_OSC_DIV2 << __ffs(CLK_DDR_BUS_MASK);
		writel(reg, (ulong *)((ulong)pll->sysreg + CLK_DDR_BUS_OFFSET));
	}

	PLLX_SET(pll->offset->pd, pll->offset->pd_mask, PLL_PD_OFF);
	PLLX_SET(pll->offset->dacpd, pll->offset->dacpd_mask, 1);
	PLLX_SET(pll->offset->dsmpd, pll->offset->dsmpd_mask, 1);
	PLLX_SET(pll->offset->prediv, pll->offset->prediv_mask, rate->prediv);
	PLLX_SET(pll->offset->fbdiv, pll->offset->fbdiv_mask, rate->fbdiv);
	PLLX_SET(pll->offset->postdiv1, pll->offset->postdiv1_mask, 0);
	PLLX_SET(pll->offset->pd, pll->offset->pd_mask, PLL_PD_ON);

	if (set) {
		udelay(100);
		reg = readl((ulong *)((ulong)pll->sysreg + CLK_DDR_BUS_OFFSET));
		reg &= ~CLK_DDR_BUS_MASK;
		reg |= CLK_DDR_BUS_PLL1_DIV2 << __ffs(CLK_DDR_BUS_MASK);
		writel(reg, (ulong *)((ulong)pll->sysreg + CLK_DDR_BUS_OFFSET));
	}
}

static ulong jh7110_pllx_recalc_rate(struct clk *clk)
{
	struct clk_jh7110_pllx *pll = to_clk_pllx(dev_get_clk_ptr(clk->dev));
	u64 refclk = clk_get_parent_rate(clk);
	u32 dacpd, dsmpd;
	u32 prediv, fbdiv, postdiv1;
	u64 frac;

	dacpd = getbits_le32((ulong)pll->base + pll->offset->dacpd,
			     pll->offset->dacpd_mask);
	dsmpd = getbits_le32((ulong)pll->base + pll->offset->dsmpd,
			     pll->offset->dsmpd_mask);
	prediv = getbits_le32((ulong)pll->base + pll->offset->prediv,
			      pll->offset->prediv_mask);
	fbdiv = getbits_le32((ulong)pll->base + pll->offset->fbdiv,
			     pll->offset->fbdiv_mask);
	postdiv1 = 1 << getbits_le32((ulong)pll->base + pll->offset->postdiv1,
			pll->offset->postdiv1_mask);
	frac = (u64)getbits_le32((ulong)pll->base + pll->offset->frac,
			pll->offset->frac_mask);

	/* Integer Multiple Mode
	 * Both dacpd and dsmpd should be set as 1 while integer multiple mode.
	 *
	 * The frequency of outputs can be figured out as below.
	 *
     *       Fvco = Fref*Nl/M
	 * NI is integer frequency dividing ratio of feedback divider, set by fbdiv1[11:0] ,
	 *    NI = 8, 9, 10, 12.13....4095
	 * M is frequency dividing ratio of pre-divider, set by prediv[5:0],M = 1,2...63
	 *
     *      Fclko1 = Fvco/Q1
	 * Q1 is frequency dividing ratio of post divider, set by postdiv1[1:0],Q1= 1,2,4,8
	 *
	 * Fraction Multiple Mode
	 *
	 *  Both dacpd and dsmpd should be set as 0 while integer multiple mode.
	 *
     *      Fvco = Fref*(NI+NF)/M
	 * NI is integer frequency dividing ratio of feedback divider, set by fbdiv[11:0] ,
	 *     NI = 8, 9, 10, 12.13....4095
	 * NF is fractional frequency dividing ratio, set by frac[23:0],  NF =frac[23:0]/2^24= 0~0.99999994
	 * M is frequency dividing ratio of pre-divider, set by prediv[5:0],M = 1,2...63
	 *
     *     Fclko1 = Fvco/Q1
	 * Q1 is frequency dividing ratio of post divider, set by postdivl[1:0],Q1= 1,2,4,8
	 */
	if (dacpd == 1 && dsmpd == 1)
		frac = 0;
	else if (dacpd == 0 && dsmpd == 0)
		do_div(frac, 1 << 24);
	else
		return -EINVAL;

	refclk *= (fbdiv + frac);
	do_div(refclk, prediv * postdiv1);

	return refclk;
}

static ulong jh7110_pllx_set_rate(struct clk *clk, ulong drate)
{
	struct clk_jh7110_pllx *pll = to_clk_pllx(dev_get_clk_ptr(clk->dev));
	const struct starfive_pllx_rate *rate;

	rate = jh7110_get_pll_settings(pll, drate);
	if (!rate)
		return -EINVAL;

	jh7110_pll_set_rate(pll, rate);

	return jh7110_pllx_recalc_rate(clk);
}

static const struct clk_ops jh7110_clk_pllx_ops = {
	.set_rate	= jh7110_pllx_set_rate,
	.get_rate	= jh7110_pllx_recalc_rate,
};

struct clk *starfive_jh7110_pll(const char *name, const char *parent_name,
				void __iomem *base, void __iomem *sysreg,
				const struct starfive_pllx_clk *pll_clk)
{
	struct clk_jh7110_pllx *pll;
	struct clk *clk;
	int ret;

	if (!pll_clk || !base || !sysreg)
		return ERR_PTR(-EINVAL);

	pll = kzalloc(sizeof(*pll), GFP_KERNEL);
	if (!pll)
		return ERR_PTR(-ENOMEM);

	pll->base = base;
	pll->sysreg = sysreg;
	pll->type = pll_clk->type;
	pll->offset = pll_clk->offset;
	pll->rate_table = pll_clk->rate_table;
	pll->rate_count = pll_clk->rate_count;

	clk = &pll->clk;
	ret = clk_register(clk, UBOOT_DM_CLK_JH7110_PLLX, name, parent_name);
	if (ret) {
		kfree(pll);
		return ERR_PTR(ret);
	}

	if (IS_ENABLED(CONFIG_SPL_BUILD) && pll->type == PLL0)
		jh7110_pllx_set_rate(clk, 1000000000);

	if (IS_ENABLED(CONFIG_SPL_BUILD) && pll->type == PLL2)
		jh7110_pllx_set_rate(clk, 1188000000);

	return clk;
}

/* PLLx clock implementation */
U_BOOT_DRIVER(jh7110_clk_pllx) = {
	.name	= UBOOT_DM_CLK_JH7110_PLLX,
	.id	= UCLASS_CLK,
	.ops	= &jh7110_clk_pllx_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

static int jh7110_pll_clk_probe(struct udevice *dev)
{
	void __iomem *reg =  (void __iomem *)dev_read_addr_ptr(dev->parent);
	fdt_addr_t sysreg = ofnode_get_addr(ofnode_by_compatible(ofnode_null(),
					    "starfive,jh7110-syscrg"));

	if (sysreg == FDT_ADDR_T_NONE)
		return -EINVAL;

	clk_dm(JH7110_PLL_ID_TRANS(JH7110_SYSCLK_PLL0_OUT),
	       starfive_jh7110_pll("pll0_out", "oscillator", reg,
				   (void __iomem *)sysreg, &starfive_jh7110_pll0));
	clk_dm(JH7110_PLL_ID_TRANS(JH7110_SYSCLK_PLL1_OUT),
	       starfive_jh7110_pll("pll1_out", "oscillator", reg,
				   (void __iomem *)sysreg, &starfive_jh7110_pll1));
	clk_dm(JH7110_PLL_ID_TRANS(JH7110_SYSCLK_PLL2_OUT),
	       starfive_jh7110_pll("pll2_out", "oscillator", reg,
				   (void __iomem *)sysreg, &starfive_jh7110_pll2));

	return 0;
}

static int jh7110_pll_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
{
	if (args->args_count > 1) {
		debug("Invalid args_count: %d\n", args->args_count);
		return -EINVAL;
	}

	if (args->args_count)
		clk->id = JH7110_PLL_ID_TRANS(args->args[0]);
	else
		clk->id = 0;

	return 0;
}

static const struct udevice_id jh7110_pll_clk_of_match[] = {
	{ .compatible = "starfive,jh7110-pll", },
	{ }
};

JH7110_CLK_OPS(pll);

/* PLL clk device */
U_BOOT_DRIVER(jh7110_pll_clk) = {
	.name	= "jh7110_pll_clk",
	.id	= UCLASS_CLK,
	.of_match	= jh7110_pll_clk_of_match,
	.probe	= jh7110_pll_clk_probe,
	.ops	= &jh7110_pll_clk_ops,
};