1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2010-2019, NVIDIA CORPORATION.  All rights reserved.
4  */
5 
6 /* Tegra SoC common clock control functions */
7 
8 #include <div64.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <log.h>
12 #include <time.h>
13 #include <asm/io.h>
14 #include <asm/arch/clock.h>
15 #include <asm/arch/tegra.h>
16 #include <asm/arch-tegra/ap.h>
17 #include <asm/arch-tegra/clk_rst.h>
18 #include <asm/arch-tegra/pmc.h>
19 #include <asm/arch-tegra/timer.h>
20 #include <linux/delay.h>
21 
22 /*
23  * This is our record of the current clock rate of each clock. We don't
24  * fill all of these in since we are only really interested in clocks which
25  * we use as parents.
26  */
27 static unsigned pll_rate[CLOCK_ID_COUNT];
28 
29 /*
30  * The oscillator frequency is fixed to one of seven set values. Based on this
31  * the other clocks are set up appropriately.
32  */
33 static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
34 	13000000,
35 	16800000,
36 	       0,
37 	       0,
38 	19200000,
39 	38400000,
40 	       0,
41 	       0,
42 	12000000,
43 	48000000,
44 	       0,
45 	       0,
46 	26000000,
47 };
48 
49 /* return 1 if a peripheral ID is in range */
50 #define clock_type_id_isvalid(id) ((id) >= 0 && \
51 		(id) < CLOCK_TYPE_COUNT)
52 
53 char pllp_valid = 1;	/* PLLP is set up correctly */
54 
55 /* return 1 if a periphc_internal_id is in range */
56 #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
57 		(id) < PERIPHC_COUNT)
58 
59 /* number of clock outputs of a PLL */
60 static const u8 pll_num_clkouts[] = {
61 	1,	/* PLLC */
62 	1,	/* PLLM */
63 	4,	/* PLLP */
64 	1,	/* PLLA */
65 	0,	/* PLLU */
66 	0,	/* PLLD */
67 };
68 
clock_get_osc_bypass(void)69 int clock_get_osc_bypass(void)
70 {
71 	struct clk_rst_ctlr *clkrst =
72 			(struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
73 	u32 reg;
74 
75 	reg = readl(&clkrst->crc_osc_ctrl);
76 	return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT;
77 }
78 
79 /* Returns a pointer to the registers of the given pll */
get_pll(enum clock_id clkid)80 static struct clk_pll *get_pll(enum clock_id clkid)
81 {
82 	struct clk_rst_ctlr *clkrst =
83 			(struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
84 
85 	assert(clock_id_is_pll(clkid));
86 	if (clkid >= (enum clock_id)TEGRA_CLK_PLLS) {
87 		debug("%s: Invalid PLL %d\n", __func__, clkid);
88 		return NULL;
89 	}
90 	return &clkrst->crc_pll[clkid];
91 }
92 
clock_get_simple_pll(enum clock_id clkid)93 __weak struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid)
94 {
95 	return NULL;
96 }
97 
clock_ll_read_pll(enum clock_id clkid,u32 * divm,u32 * divn,u32 * divp,u32 * cpcon,u32 * lfcon)98 int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
99 		u32 *divp, u32 *cpcon, u32 *lfcon)
100 {
101 	struct clk_pll *pll = get_pll(clkid);
102 	struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
103 	u32 data;
104 
105 	assert(clkid != CLOCK_ID_USB);
106 
107 	/* Safety check, adds to code size but is small */
108 	if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB)
109 		return -1;
110 	data = readl(&pll->pll_base);
111 	*divm = (data >> pllinfo->m_shift) & pllinfo->m_mask;
112 	*divn = (data >> pllinfo->n_shift) & pllinfo->n_mask;
113 	*divp = (data >> pllinfo->p_shift) & pllinfo->p_mask;
114 	data = readl(&pll->pll_misc);
115 	/* NOTE: On T210, cpcon/lfcon no longer exist, moved to KCP/KVCO */
116 	*cpcon = (data >> pllinfo->kcp_shift) & pllinfo->kcp_mask;
117 	*lfcon = (data >> pllinfo->kvco_shift) & pllinfo->kvco_mask;
118 
119 	return 0;
120 }
121 
clock_start_pll(enum clock_id clkid,u32 divm,u32 divn,u32 divp,u32 cpcon,u32 lfcon)122 unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
123 		u32 divp, u32 cpcon, u32 lfcon)
124 {
125 	struct clk_pll *pll = NULL;
126 	struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
127 	struct clk_pll_simple *simple_pll = NULL;
128 	u32 misc_data, data;
129 
130 	if (clkid < (enum clock_id)TEGRA_CLK_PLLS)
131 		pll = get_pll(clkid);
132 	else
133 		simple_pll = clock_get_simple_pll(clkid);
134 
135 	if (!simple_pll && !pll) {
136 		log_err("Unknown PLL id %d\n", clkid);
137 		return 0;
138 	}
139 
140 	/*
141 	 * pllinfo has the m/n/p and kcp/kvco mask and shift
142 	 * values for all of the PLLs used in U-Boot, with any
143 	 * SoC differences accounted for.
144 	 *
145 	 * Preserve EN_LOCKDET, etc.
146 	 */
147 	if (pll)
148 		misc_data = readl(&pll->pll_misc);
149 	else
150 		misc_data = readl(&simple_pll->pll_misc);
151 	misc_data &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift);
152 	misc_data |= cpcon << pllinfo->kcp_shift;
153 	misc_data &= ~(pllinfo->kvco_mask << pllinfo->kvco_shift);
154 	misc_data |= lfcon << pllinfo->kvco_shift;
155 
156 	data = (divm << pllinfo->m_shift) | (divn << pllinfo->n_shift);
157 	data |= divp << pllinfo->p_shift;
158 	data |= (1 << PLL_ENABLE_SHIFT);	/* BYPASS s/b 0 already */
159 
160 	if (pll) {
161 		writel(misc_data, &pll->pll_misc);
162 		writel(data, &pll->pll_base);
163 	} else {
164 		writel(misc_data, &simple_pll->pll_misc);
165 		writel(data, &simple_pll->pll_base);
166 	}
167 
168 	/* calculate the stable time */
169 	return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
170 }
171 
clock_ll_set_source_divisor(enum periph_id periph_id,unsigned source,unsigned divisor)172 void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
173 			unsigned divisor)
174 {
175 	u32 *reg = get_periph_source_reg(periph_id);
176 	u32 value;
177 
178 	value = readl(reg);
179 
180 	value &= ~OUT_CLK_SOURCE_31_30_MASK;
181 	value |= source << OUT_CLK_SOURCE_31_30_SHIFT;
182 
183 	value &= ~OUT_CLK_DIVISOR_MASK;
184 	value |= divisor << OUT_CLK_DIVISOR_SHIFT;
185 
186 	writel(value, reg);
187 }
188 
clock_ll_set_source_bits(enum periph_id periph_id,int mux_bits,unsigned source)189 int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits,
190 			     unsigned source)
191 {
192 	u32 *reg = get_periph_source_reg(periph_id);
193 
194 	switch (mux_bits) {
195 	case MASK_BITS_31_30:
196 		clrsetbits_le32(reg, OUT_CLK_SOURCE_31_30_MASK,
197 				source << OUT_CLK_SOURCE_31_30_SHIFT);
198 		break;
199 
200 	case MASK_BITS_31_29:
201 		clrsetbits_le32(reg, OUT_CLK_SOURCE_31_29_MASK,
202 				source << OUT_CLK_SOURCE_31_29_SHIFT);
203 		break;
204 
205 	case MASK_BITS_31_28:
206 		clrsetbits_le32(reg, OUT_CLK_SOURCE_31_28_MASK,
207 				source << OUT_CLK_SOURCE_31_28_SHIFT);
208 		break;
209 
210 	default:
211 		return -1;
212 	}
213 
214 	return 0;
215 }
216 
clock_ll_get_source_bits(enum periph_id periph_id,int mux_bits)217 static int clock_ll_get_source_bits(enum periph_id periph_id, int mux_bits)
218 {
219 	u32 *reg = get_periph_source_reg(periph_id);
220 	u32 val = readl(reg);
221 
222 	switch (mux_bits) {
223 	case MASK_BITS_31_30:
224 		val >>= OUT_CLK_SOURCE_31_30_SHIFT;
225 		val &= OUT_CLK_SOURCE_31_30_MASK;
226 		return val;
227 	case MASK_BITS_31_29:
228 		val >>= OUT_CLK_SOURCE_31_29_SHIFT;
229 		val &= OUT_CLK_SOURCE_31_29_MASK;
230 		return val;
231 	case MASK_BITS_31_28:
232 		val >>= OUT_CLK_SOURCE_31_28_SHIFT;
233 		val &= OUT_CLK_SOURCE_31_28_MASK;
234 		return val;
235 	default:
236 		return -1;
237 	}
238 }
239 
clock_ll_set_source(enum periph_id periph_id,unsigned source)240 void clock_ll_set_source(enum periph_id periph_id, unsigned source)
241 {
242 	clock_ll_set_source_bits(periph_id, MASK_BITS_31_30, source);
243 }
244 
245 /**
246  * Given the parent's rate and the required rate for the children, this works
247  * out the peripheral clock divider to use, in 7.1 binary format.
248  *
249  * @param divider_bits	number of divider bits (8 or 16)
250  * @param parent_rate	clock rate of parent clock in Hz
251  * @param rate		required clock rate for this clock
252  * Return: divider which should be used
253  */
clk_get_divider(unsigned divider_bits,unsigned long parent_rate,unsigned long rate)254 static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate,
255 			   unsigned long rate)
256 {
257 	u64 divider = parent_rate * 2;
258 	unsigned max_divider = 1 << divider_bits;
259 
260 	divider += rate - 1;
261 	do_div(divider, rate);
262 
263 	if ((s64)divider - 2 < 0)
264 		return 0;
265 
266 	if ((s64)divider - 2 >= max_divider)
267 		return -1;
268 
269 	return divider - 2;
270 }
271 
clock_set_pllout(enum clock_id clkid,enum pll_out_id pllout,unsigned rate)272 int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate)
273 {
274 	struct clk_pll *pll = get_pll(clkid);
275 	int data = 0, div = 0, offset = 0;
276 
277 	if (!clock_id_is_pll(clkid))
278 		return -1;
279 
280 	if (pllout + 1 > pll_num_clkouts[clkid])
281 		return -1;
282 
283 	div = clk_get_divider(8, pll_rate[clkid], rate);
284 
285 	if (div < 0)
286 		return -1;
287 
288 	/* out2 and out4 are in the high part of the register */
289 	if (pllout == PLL_OUT2 || pllout == PLL_OUT4)
290 		offset = 16;
291 
292 	data = (div << PLL_OUT_RATIO_SHIFT) |
293 			PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN;
294 	clrsetbits_le32(&pll->pll_out[pllout >> 1],
295 			PLL_OUT_RATIO_MASK << offset, data << offset);
296 
297 	return 0;
298 }
299 
300 /**
301  * Given the parent's rate and the divider in 7.1 format, this works out the
302  * resulting peripheral clock rate.
303  *
304  * @param parent_rate	clock rate of parent clock in Hz
305  * @param divider which should be used in 7.1 format
306  * Return: effective clock rate of peripheral
307  */
get_rate_from_divider(unsigned long parent_rate,int divider)308 static unsigned long get_rate_from_divider(unsigned long parent_rate,
309 					   int divider)
310 {
311 	u64 rate;
312 
313 	rate = (u64)parent_rate * 2;
314 	do_div(rate, divider + 2);
315 	return rate;
316 }
317 
clock_get_periph_rate(enum periph_id periph_id,enum clock_id parent)318 unsigned long clock_get_periph_rate(enum periph_id periph_id,
319 		enum clock_id parent)
320 {
321 	u32 *reg = get_periph_source_reg(periph_id);
322 	unsigned parent_rate = pll_rate[parent];
323 	int div = (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT;
324 
325 	switch (periph_id) {
326 	case PERIPH_ID_UART1:
327 	case PERIPH_ID_UART2:
328 	case PERIPH_ID_UART3:
329 	case PERIPH_ID_UART4:
330 	case PERIPH_ID_UART5:
331 #ifdef CONFIG_TEGRA20
332 		/* There's no divider for these clocks in this SoC. */
333 		return parent_rate;
334 #else
335 		/*
336 		 * This undoes the +2 in get_rate_from_divider() which I
337 		 * believe is incorrect. Ideally we would fix
338 		 * get_rate_from_divider(), but... Removing the +2 from
339 		 * get_rate_from_divider() would probably require remove the -2
340 		 * from the tail of clk_get_divider() since I believe that's
341 		 * only there to invert get_rate_from_divider()'s +2. Observe
342 		 * how find_best_divider() uses those two functions together.
343 		 * However, doing so breaks other stuff, such as Seaboard's
344 		 * display, likely due to clock_set_pllout()'s call to
345 		 * clk_get_divider(). Attempting to fix that by making
346 		 * clock_set_pllout() subtract 2 from clk_get_divider()'s
347 		 * return value doesn't help. In summary this clock driver is
348 		 * quite broken but I'm afraid I have no idea how to fix it
349 		 * without completely replacing it.
350 		 *
351 		 * Be careful to avoid a divide by zero error.
352 		 */
353 		if (div >= 1)
354 			div -= 2;
355 		break;
356 #endif
357 	default:
358 		break;
359 	}
360 
361 	/*
362 	 * PLLD/PLLD2 raw clock rate is never used, instead plld_out0 is used
363 	 * that is PLLD/PLLD2 halved.
364 	 */
365 	if (parent == CLOCK_ID_DISPLAY || parent == CLOCK_ID_DISPLAY2)
366 		parent_rate /= 2;
367 
368 	return get_rate_from_divider(parent_rate, div);
369 }
370 
371 /**
372  * Find the best available 7.1 format divisor given a parent clock rate and
373  * required child clock rate. This function assumes that a second-stage
374  * divisor is available which can divide by powers of 2 from 1 to 256.
375  *
376  * @param divider_bits	number of divider bits (8 or 16)
377  * @param parent_rate	clock rate of parent clock in Hz
378  * @param rate		required clock rate for this clock
379  * @param extra_div	value for the second-stage divisor (not set if this
380  *			function returns -1.
381  * Return: divider which should be used, or -1 if nothing is valid
382  *
383  */
find_best_divider(unsigned divider_bits,unsigned long parent_rate,unsigned long rate,int * extra_div)384 static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
385 				unsigned long rate, int *extra_div)
386 {
387 	int shift;
388 	int best_divider = -1;
389 	int best_error = rate;
390 
391 	/* try dividers from 1 to 256 and find closest match */
392 	for (shift = 0; shift <= 8 && best_error > 0; shift++) {
393 		unsigned divided_parent = parent_rate >> shift;
394 		int divider = clk_get_divider(divider_bits, divided_parent,
395 						rate);
396 		unsigned effective_rate = get_rate_from_divider(divided_parent,
397 						divider);
398 		int error = rate - effective_rate;
399 
400 		/* Given a valid divider, look for the lowest error */
401 		if (divider != -1 && error < best_error) {
402 			best_error = error;
403 			*extra_div = 1 << shift;
404 			best_divider = divider;
405 		}
406 	}
407 
408 	/* return what we found - *extra_div will already be set */
409 	return best_divider;
410 }
411 
412 /**
413  * Adjust peripheral PLL to use the given divider and source.
414  *
415  * @param periph_id	peripheral to adjust
416  * @param source	Source number (0-3 or 0-7)
417  * @param mux_bits	Number of mux bits (2 or 4)
418  * @param divider	Required divider in 7.1 or 15.1 format
419  * Return: 0 if ok, -1 on error (requesting a parent clock which is not valid
420  *		for this peripheral)
421  */
adjust_periph_pll(enum periph_id periph_id,int source,int mux_bits,unsigned divider)422 static int adjust_periph_pll(enum periph_id periph_id, int source,
423 				int mux_bits, unsigned divider)
424 {
425 	u32 *reg = get_periph_source_reg(periph_id);
426 
427 	clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
428 			divider << OUT_CLK_DIVISOR_SHIFT);
429 	udelay(1);
430 
431 	/* work out the source clock and set it */
432 	if (source < 0)
433 		return -1;
434 
435 	clock_ll_set_source_bits(periph_id, mux_bits, source);
436 
437 	udelay(2);
438 	return 0;
439 }
440 
clock_get_periph_parent(enum periph_id periph_id)441 enum clock_id clock_get_periph_parent(enum periph_id periph_id)
442 {
443 	int err, mux_bits, divider_bits, type;
444 	int source;
445 
446 	err = get_periph_clock_info(periph_id, &mux_bits, &divider_bits, &type);
447 	if (err)
448 		return CLOCK_ID_NONE;
449 
450 	source = clock_ll_get_source_bits(periph_id, mux_bits);
451 
452 	return get_periph_clock_id(periph_id, source);
453 }
454 
clock_adjust_periph_pll_div(enum periph_id periph_id,enum clock_id parent,unsigned rate,int * extra_div)455 unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
456 		enum clock_id parent, unsigned rate, int *extra_div)
457 {
458 	unsigned effective_rate;
459 	unsigned int parent_rate;
460 	int mux_bits, divider_bits, source;
461 	int divider;
462 	int xdiv = 0;
463 
464 	/* work out the source clock and set it */
465 	source = get_periph_clock_source(periph_id, parent, &mux_bits,
466 					 &divider_bits);
467 
468 	/*
469 	 * Clocks derived from PLLD/D2 are actually sourced from its halved
470 	 * output, plld_out0/plld2_out0. No peripheral clocks use the raw
471 	 * PLLD/D2 frequency. This halving must be accounted for in derived
472 	 * clock calculations.
473 	 */
474 	parent_rate = pll_rate[parent];
475 	if (parent == CLOCK_ID_DISPLAY || parent == CLOCK_ID_DISPLAY2)
476 		parent_rate /= 2;
477 
478 	divider = find_best_divider(divider_bits, parent_rate,
479 				    rate, &xdiv);
480 	if (extra_div)
481 		*extra_div = xdiv;
482 
483 	assert(divider >= 0);
484 	if (adjust_periph_pll(periph_id, source, mux_bits, divider))
485 		return -1U;
486 	debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
487 		get_periph_source_reg(periph_id),
488 		readl(get_periph_source_reg(periph_id)));
489 
490 	/* Check what we ended up with. This shouldn't matter though */
491 	effective_rate = clock_get_periph_rate(periph_id, parent);
492 	if (extra_div)
493 		effective_rate /= *extra_div;
494 	if (rate != effective_rate)
495 		debug("Requested clock rate %u not honored (got %u)\n",
496 			rate, effective_rate);
497 	return effective_rate;
498 }
499 
clock_start_periph_pll(enum periph_id periph_id,enum clock_id parent,unsigned rate)500 unsigned clock_start_periph_pll(enum periph_id periph_id,
501 		enum clock_id parent, unsigned rate)
502 {
503 	unsigned effective_rate;
504 
505 	reset_set_enable(periph_id, 1);
506 	clock_enable(periph_id);
507 	udelay(2);
508 
509 	effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
510 						 NULL);
511 
512 	reset_set_enable(periph_id, 0);
513 	return effective_rate;
514 }
515 
clock_enable(enum periph_id clkid)516 void clock_enable(enum periph_id clkid)
517 {
518 	clock_set_enable(clkid, 1);
519 }
520 
clock_disable(enum periph_id clkid)521 void clock_disable(enum periph_id clkid)
522 {
523 	clock_set_enable(clkid, 0);
524 }
525 
reset_periph(enum periph_id periph_id,int us_delay)526 void reset_periph(enum periph_id periph_id, int us_delay)
527 {
528 	/* Put peripheral into reset */
529 	reset_set_enable(periph_id, 1);
530 	udelay(us_delay);
531 
532 	/* Remove reset */
533 	reset_set_enable(periph_id, 0);
534 
535 	udelay(us_delay);
536 }
537 
reset_cmplx_set_enable(int cpu,int which,int reset)538 void reset_cmplx_set_enable(int cpu, int which, int reset)
539 {
540 	struct clk_rst_ctlr *clkrst =
541 			(struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
542 	u32 mask;
543 
544 	/* Form the mask, which depends on the cpu chosen (2 or 4) */
545 	assert(cpu >= 0 && cpu < MAX_NUM_CPU);
546 	mask = which << cpu;
547 
548 	/* either enable or disable those reset for that CPU */
549 	if (reset)
550 		writel(mask, &clkrst->crc_cpu_cmplx_set);
551 	else
552 		writel(mask, &clkrst->crc_cpu_cmplx_clr);
553 }
554 
clk_m_get_rate(unsigned int parent_rate)555 unsigned int __weak clk_m_get_rate(unsigned int parent_rate)
556 {
557 	return parent_rate;
558 }
559 
clock_get_rate(enum clock_id clkid)560 unsigned clock_get_rate(enum clock_id clkid)
561 {
562 	struct clk_pll *pll = NULL;
563 	struct clk_pll_simple *simple_pll = NULL;
564 	u32 base, divm;
565 	u64 parent_rate, rate;
566 	struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
567 
568 	parent_rate = osc_freq[clock_get_osc_freq()];
569 	if (clkid == CLOCK_ID_OSC)
570 		return parent_rate;
571 
572 	if (clkid == CLOCK_ID_CLK_M)
573 		return clk_m_get_rate(parent_rate);
574 
575 	if (clkid < (enum clock_id)TEGRA_CLK_PLLS)
576 		pll = get_pll(clkid);
577 	else
578 		simple_pll = clock_get_simple_pll(clkid);
579 
580 	if (!simple_pll && !pll) {
581 		log_err("Unknown PLL id %d\n", clkid);
582 		return 0;
583 	}
584 
585 	if (pll)
586 		base = readl(&pll->pll_base);
587 	else
588 		base = readl(&simple_pll->pll_base);
589 
590 	rate = parent_rate * ((base >> pllinfo->n_shift) & pllinfo->n_mask);
591 	divm = (base >> pllinfo->m_shift) & pllinfo->m_mask;
592 	/*
593 	 * PLLU uses p_mask/p_shift for VCO on all but T210,
594 	 * T210 uses normal DIVP. Handled in pllinfo table.
595 	 */
596 #ifdef CONFIG_TEGRA210
597 	/*
598 	 * PLLP's primary output (pllP_out0) on T210 is the VCO, and divp is
599 	 * not applied. pllP_out2 does have divp applied. All other pllP_outN
600 	 * are divided down from pllP_out0. We only support pllP_out0 in
601 	 * U-Boot at the time of writing this comment.
602 	 */
603 	if (clkid != CLOCK_ID_PERIPH)
604 #endif
605 		divm <<= (base >> pllinfo->p_shift) & pllinfo->p_mask;
606 	do_div(rate, divm);
607 	return rate;
608 }
609 
610 /**
611  * Set the output frequency you want for each PLL clock.
612  * PLL output frequencies are programmed by setting their N, M and P values.
613  * The governing equations are:
614  *     VCO = (Fi / m) * n, Fo = VCO / (2^p)
615  *     where Fo is the output frequency from the PLL.
616  * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
617  *     216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
618  * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
619  *
620  * @param n PLL feedback divider(DIVN)
621  * @param m PLL input divider(DIVN)
622  * @param p post divider(DIVP)
623  * @param cpcon base PLL charge pump(CPCON)
624  * Return: 0 if ok, -1 on error (the requested PLL is incorrect and cannot
625  *		be overridden), 1 if PLL is already correct
626  */
clock_set_rate(enum clock_id clkid,u32 n,u32 m,u32 p,u32 cpcon)627 int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
628 {
629 	u32 base_reg, misc_reg;
630 	struct clk_pll *pll = NULL;
631 	struct clk_pll_simple *simple_pll = NULL;
632 	struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
633 
634 	if (clkid < (enum clock_id)TEGRA_CLK_PLLS)
635 		pll = get_pll(clkid);
636 	else
637 		simple_pll = clock_get_simple_pll(clkid);
638 
639 	if (!simple_pll && !pll) {
640 		log_err("Unknown PLL id %d\n", clkid);
641 		return 0;
642 	}
643 
644 	if (pll)
645 		base_reg = readl(&pll->pll_base);
646 	else
647 		base_reg = readl(&simple_pll->pll_base);
648 
649 	/* Set BYPASS, m, n and p to PLL_BASE */
650 	base_reg &= ~(pllinfo->m_mask << pllinfo->m_shift);
651 	base_reg |= m << pllinfo->m_shift;
652 
653 	base_reg &= ~(pllinfo->n_mask << pllinfo->n_shift);
654 	base_reg |= n << pllinfo->n_shift;
655 
656 	base_reg &= ~(pllinfo->p_mask << pllinfo->p_shift);
657 	base_reg |= p << pllinfo->p_shift;
658 
659 	if (clkid == CLOCK_ID_PERIPH) {
660 		/*
661 		 * If the PLL is already set up, check that it is correct
662 		 * and record this info for clock_verify() to check.
663 		 */
664 		if (base_reg & PLL_BASE_OVRRIDE_MASK) {
665 			base_reg |= PLL_ENABLE_MASK;
666 			if (base_reg != readl(&pll->pll_base))
667 				pllp_valid = 0;
668 			return pllp_valid ? 1 : -1;
669 		}
670 		base_reg |= PLL_BASE_OVRRIDE_MASK;
671 	}
672 
673 	base_reg |= PLL_BYPASS_MASK;
674 	if (pll)
675 		writel(base_reg, &pll->pll_base);
676 	else
677 		writel(base_reg, &simple_pll->pll_base);
678 
679 	/* Set cpcon (KCP) to PLL_MISC */
680 	if (pll)
681 		misc_reg = readl(&pll->pll_misc);
682 	else
683 		misc_reg = readl(&simple_pll->pll_misc);
684 
685 	misc_reg &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift);
686 	misc_reg |= cpcon << pllinfo->kcp_shift;
687 	if (pll)
688 		writel(misc_reg, &pll->pll_misc);
689 	else
690 		writel(misc_reg, &simple_pll->pll_misc);
691 
692 	/* Enable PLL */
693 	base_reg |= PLL_ENABLE_MASK;
694 	if (pll)
695 		writel(base_reg, &pll->pll_base);
696 	else
697 		writel(base_reg, &simple_pll->pll_base);
698 
699 	/* Disable BYPASS */
700 	base_reg &= ~PLL_BYPASS_MASK;
701 	if (pll)
702 		writel(base_reg, &pll->pll_base);
703 	else
704 		writel(base_reg, &simple_pll->pll_base);
705 
706 	/* PLLD and PLLD2 are only clocks which have ENABLE bit */
707 	if (clkid == CLOCK_ID_DISPLAY)
708 		setbits_le32(&pll->pll_misc, BIT(PLLD_CLKENABLE));
709 	if (clkid == CLOCK_ID_DISPLAY2)
710 		setbits_le32(&simple_pll->pll_misc, BIT(PLLD_CLKENABLE));
711 
712 	/*
713 	 * Changing clocks was never intended in the U-Boot for Tegra.
714 	 * If a clock is changed after clock_init() the parent rate is wrong.
715 	 * Usually there is no reason to change peripheral clocks, but Display
716 	 * PLLs which needs to generate a precise pixelclock might be adjusted.
717 	 * Especially in the case of HDMI display with changing and prior
718 	 * unknown resolution.
719 	 */
720 	pll_rate[clkid] = clock_get_rate(clkid);
721 
722 	return 0;
723 }
724 
clock_ll_start_uart(enum periph_id periph_id)725 void clock_ll_start_uart(enum periph_id periph_id)
726 {
727 	/* Assert UART reset and enable clock */
728 	reset_set_enable(periph_id, 1);
729 	clock_enable(periph_id);
730 	clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
731 
732 	/* wait for 2us */
733 	udelay(2);
734 
735 	/* De-assert reset to UART */
736 	reset_set_enable(periph_id, 0);
737 }
738 
739 #if CONFIG_IS_ENABLED(OF_CONTROL)
clock_decode_periph_id(struct udevice * dev)740 int clock_decode_periph_id(struct udevice *dev)
741 {
742 	enum periph_id id;
743 	u32 cell[2];
744 	int err;
745 
746 	err = dev_read_u32_array(dev, "clocks", cell, ARRAY_SIZE(cell));
747 	if (err)
748 		return -1;
749 	id = clk_id_to_periph_id(cell[1]);
750 	assert(clock_periph_id_isvalid(id));
751 	return id;
752 }
753 
754 /*
755  * Get periph clock id and its parent from device tree.
756  *
757  * @param dev		udevice associated with FDT node
758  * @param clk_id	pointer to u32 array of 2 values
759  *			first is periph clock, second is
760  *			its PLL parent according to FDT.
761  */
clock_decode_pair(struct udevice * dev,int * clk_id)762 int clock_decode_pair(struct udevice *dev, int *clk_id)
763 {
764 	u32 cell[4];
765 	int err;
766 
767 	err = dev_read_u32_array(dev, "clocks", cell, ARRAY_SIZE(cell));
768 	if (err)
769 		return -EINVAL;
770 
771 	clk_id[0] = clk_id_to_periph_id(cell[1]);
772 	clk_id[1] = clk_id_to_pll_id(cell[3]);
773 
774 	return 0;
775 }
776 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
777 
clock_verify(void)778 int clock_verify(void)
779 {
780 	struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
781 	u32 reg = readl(&pll->pll_base);
782 
783 	if (!pllp_valid) {
784 		printf("Warning: PLLP %x is not correct\n", reg);
785 		return -1;
786 	}
787 	debug("PLLP %x is correct\n", reg);
788 	return 0;
789 }
790 
clock_init(void)791 void clock_init(void)
792 {
793 	int i;
794 
795 	pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
796 	pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
797 	pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
798 	pll_rate[CLOCK_ID_USB] = clock_get_rate(CLOCK_ID_USB);
799 	pll_rate[CLOCK_ID_DISPLAY] = clock_get_rate(CLOCK_ID_DISPLAY);
800 	pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU);
801 	pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
802 	pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
803 	pll_rate[CLOCK_ID_CLK_M] = clock_get_rate(CLOCK_ID_CLK_M);
804 #ifndef CONFIG_TEGRA20
805 	pll_rate[CLOCK_ID_DISPLAY2] = clock_get_rate(CLOCK_ID_DISPLAY2);
806 #endif
807 
808 	debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
809 	debug("CLKM = %d\n", pll_rate[CLOCK_ID_CLK_M]);
810 	debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]);
811 	debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
812 	debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
813 	debug("PLLU = %d\n", pll_rate[CLOCK_ID_USB]);
814 	debug("PLLD = %d\n", pll_rate[CLOCK_ID_DISPLAY]);
815 	debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]);
816 
817 	for (i = 0; periph_clk_init_table[i].periph_id != -1; i++) {
818 		enum periph_id periph_id;
819 		enum clock_id parent;
820 		int source, mux_bits, divider_bits;
821 
822 		periph_id = periph_clk_init_table[i].periph_id;
823 		parent = periph_clk_init_table[i].parent_clock_id;
824 
825 		source = get_periph_clock_source(periph_id, parent, &mux_bits,
826 						 &divider_bits);
827 		clock_ll_set_source_bits(periph_id, mux_bits, source);
828 	}
829 }
830 
set_avp_clock_source(u32 src)831 static void set_avp_clock_source(u32 src)
832 {
833 	struct clk_rst_ctlr *clkrst =
834 			(struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
835 	u32 val;
836 
837 	val = (src << SCLK_SWAKEUP_FIQ_SOURCE_SHIFT) |
838 		(src << SCLK_SWAKEUP_IRQ_SOURCE_SHIFT) |
839 		(src << SCLK_SWAKEUP_RUN_SOURCE_SHIFT) |
840 		(src << SCLK_SWAKEUP_IDLE_SOURCE_SHIFT) |
841 		(SCLK_SYS_STATE_RUN << SCLK_SYS_STATE_SHIFT);
842 	writel(val, &clkrst->crc_sclk_brst_pol);
843 	udelay(3);
844 }
845 
846 /*
847  * This function is useful on Tegra30, and any later SoCs that have compatible
848  * PLLP configuration registers.
849  * NOTE: Not used on Tegra210 - see tegra210_setup_pllp in T210 clock.c
850  */
tegra30_set_up_pllp(void)851 void tegra30_set_up_pllp(void)
852 {
853 	struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
854 	u32 reg;
855 
856 	/*
857 	 * Based on the Tegra TRM, the system clock (which is the AVP clock) can
858 	 * run up to 275MHz. On power on, the default sytem clock source is set
859 	 * to PLLP_OUT0. This function sets PLLP's (hence PLLP_OUT0's) rate to
860 	 * 408MHz which is beyond system clock's upper limit.
861 	 *
862 	 * The fix is to set the system clock to CLK_M before initializing PLLP,
863 	 * and then switch back to PLLP_OUT4, which has an appropriate divider
864 	 * configured, after PLLP has been configured
865 	 */
866 	set_avp_clock_source(SCLK_SOURCE_CLKM);
867 
868 	/*
869 	 * PLLP output frequency set to 408Mhz
870 	 * PLLC output frequency set to 228Mhz
871 	 */
872 	switch (clock_get_osc_freq()) {
873 	case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
874 	case CLOCK_OSC_FREQ_48_0: /* OSC is 48Mhz */
875 		clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8);
876 		clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8);
877 		break;
878 
879 	case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
880 		clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8);
881 		clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
882 		break;
883 
884 	case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
885 	case CLOCK_OSC_FREQ_16_8: /* OSC is 16.8Mhz */
886 		clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8);
887 		clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
888 		break;
889 
890 	case CLOCK_OSC_FREQ_19_2:
891 	case CLOCK_OSC_FREQ_38_4:
892 	default:
893 		/*
894 		 * These are not supported. It is too early to print a
895 		 * message and the UART likely won't work anyway due to the
896 		 * oscillator being wrong.
897 		 */
898 		break;
899 	}
900 
901 	/* Set PLLP_OUT1, 2, 3 & 4 freqs to 9.6, 48, 102 & 204MHz */
902 
903 	/* OUT1, 2 */
904 	/* Assert RSTN before enable */
905 	reg = PLLP_OUT2_RSTN_EN | PLLP_OUT1_RSTN_EN;
906 	writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
907 	/* Set divisor and reenable */
908 	reg = (IN_408_OUT_48_DIVISOR << PLLP_OUT2_RATIO)
909 		| PLLP_OUT2_OVR | PLLP_OUT2_CLKEN | PLLP_OUT2_RSTN_DIS
910 		| (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO)
911 		| PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS;
912 	writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
913 
914 	/* OUT3, 4 */
915 	/* Assert RSTN before enable */
916 	reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN;
917 	writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
918 	/* Set divisor and reenable */
919 	reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO)
920 		| PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS
921 		| (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO)
922 		| PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS;
923 	writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
924 
925 	set_avp_clock_source(SCLK_SOURCE_PLLP_OUT4);
926 }
927 
clock_external_output(int clk_id)928 int clock_external_output(int clk_id)
929 {
930 	u32 val;
931 
932 	if (clk_id >= 1 && clk_id <= 3) {
933 		val = tegra_pmc_readl(offsetof(struct pmc_ctlr,
934 				      pmc_clk_out_cntrl));
935 		val |= 1 << (2 + (clk_id - 1) * 8);
936 		tegra_pmc_writel(val,
937 				 offsetof(struct pmc_ctlr,
938 				 pmc_clk_out_cntrl));
939 
940 	} else {
941 		printf("%s: Unknown output clock id %d\n", __func__, clk_id);
942 		return -EINVAL;
943 	}
944 
945 	return 0;
946 }
947 
clock_early_init_done(void)948 __weak bool clock_early_init_done(void)
949 {
950 	return true;
951 }
952