blob: c4225b8b5bf87adf1026cb51302466e998484214 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Tom Warren795f9d72013-01-23 14:01:01 -07002/*
Thierry Redingce7eb162019-04-15 11:32:25 +02003 * Copyright (c) 2010-2019, NVIDIA CORPORATION. All rights reserved.
Tom Warren795f9d72013-01-23 14:01:01 -07004 */
5
6/* Tegra SoC common clock control functions */
7
8#include <common.h>
Simon Glass15023922017-06-12 06:21:39 -06009#include <div64.h>
10#include <dm.h>
Simon Glasscd4b59b2015-06-05 14:39:36 -060011#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070013#include <time.h>
Tom Warren795f9d72013-01-23 14:01:01 -070014#include <asm/io.h>
15#include <asm/arch/clock.h>
16#include <asm/arch/tegra.h>
Stephen Warren8d1fb312015-01-19 16:25:52 -070017#include <asm/arch-tegra/ap.h>
Tom Warren795f9d72013-01-23 14:01:01 -070018#include <asm/arch-tegra/clk_rst.h>
Simon Glasscd4b59b2015-06-05 14:39:36 -060019#include <asm/arch-tegra/pmc.h>
Tom Warren795f9d72013-01-23 14:01:01 -070020#include <asm/arch-tegra/timer.h>
Tom Warren795f9d72013-01-23 14:01:01 -070021
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 */
27static unsigned pll_rate[CLOCK_ID_COUNT];
28
29/*
30 * The oscillator frequency is fixed to one of four set values. Based on this
31 * the other clocks are set up appropriately.
32 */
33static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
34 13000000,
35 19200000,
36 12000000,
37 26000000,
Tom Warren27bce712015-06-22 13:03:44 -070038 38400000,
39 48000000,
Tom Warren795f9d72013-01-23 14:01:01 -070040};
41
42/* return 1 if a peripheral ID is in range */
43#define clock_type_id_isvalid(id) ((id) >= 0 && \
44 (id) < CLOCK_TYPE_COUNT)
45
46char pllp_valid = 1; /* PLLP is set up correctly */
47
48/* return 1 if a periphc_internal_id is in range */
49#define periphc_internal_id_isvalid(id) ((id) >= 0 && \
50 (id) < PERIPHC_COUNT)
51
52/* number of clock outputs of a PLL */
53static const u8 pll_num_clkouts[] = {
54 1, /* PLLC */
55 1, /* PLLM */
56 4, /* PLLP */
57 1, /* PLLA */
58 0, /* PLLU */
59 0, /* PLLD */
60};
61
62int clock_get_osc_bypass(void)
63{
64 struct clk_rst_ctlr *clkrst =
65 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
66 u32 reg;
67
68 reg = readl(&clkrst->crc_osc_ctrl);
69 return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT;
70}
71
72/* Returns a pointer to the registers of the given pll */
73static struct clk_pll *get_pll(enum clock_id clkid)
74{
75 struct clk_rst_ctlr *clkrst =
76 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
77
78 assert(clock_id_is_pll(clkid));
Simon Glass6017b9a2015-04-14 21:03:32 -060079 if (clkid >= (enum clock_id)TEGRA_CLK_PLLS) {
Simon Glass31689872015-06-05 14:39:37 -060080 debug("%s: Invalid PLL %d\n", __func__, clkid);
Simon Glass6017b9a2015-04-14 21:03:32 -060081 return NULL;
82 }
Tom Warren795f9d72013-01-23 14:01:01 -070083 return &clkrst->crc_pll[clkid];
84}
85
Simon Glass6017b9a2015-04-14 21:03:32 -060086__weak struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid)
87{
88 return NULL;
89}
90
Tom Warren795f9d72013-01-23 14:01:01 -070091int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
92 u32 *divp, u32 *cpcon, u32 *lfcon)
93{
94 struct clk_pll *pll = get_pll(clkid);
Tom Warrena8480ef2015-06-25 09:50:44 -070095 struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
Tom Warren795f9d72013-01-23 14:01:01 -070096 u32 data;
97
98 assert(clkid != CLOCK_ID_USB);
99
100 /* Safety check, adds to code size but is small */
101 if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB)
102 return -1;
103 data = readl(&pll->pll_base);
Tom Warrena8480ef2015-06-25 09:50:44 -0700104 *divm = (data >> pllinfo->m_shift) & pllinfo->m_mask;
105 *divn = (data >> pllinfo->n_shift) & pllinfo->n_mask;
106 *divp = (data >> pllinfo->p_shift) & pllinfo->p_mask;
Tom Warren795f9d72013-01-23 14:01:01 -0700107 data = readl(&pll->pll_misc);
Tom Warrena8480ef2015-06-25 09:50:44 -0700108 /* NOTE: On T210, cpcon/lfcon no longer exist, moved to KCP/KVCO */
109 *cpcon = (data >> pllinfo->kcp_shift) & pllinfo->kcp_mask;
110 *lfcon = (data >> pllinfo->kvco_shift) & pllinfo->kvco_mask;
111
Tom Warren795f9d72013-01-23 14:01:01 -0700112 return 0;
113}
114
115unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
116 u32 divp, u32 cpcon, u32 lfcon)
117{
Simon Glass31689872015-06-05 14:39:37 -0600118 struct clk_pll *pll = NULL;
Tom Warrena8480ef2015-06-25 09:50:44 -0700119 struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600120 struct clk_pll_simple *simple_pll = NULL;
Simon Glass6017b9a2015-04-14 21:03:32 -0600121 u32 misc_data, data;
Tom Warren795f9d72013-01-23 14:01:01 -0700122
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600123 if (clkid < (enum clock_id)TEGRA_CLK_PLLS) {
Simon Glass31689872015-06-05 14:39:37 -0600124 pll = get_pll(clkid);
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600125 } else {
126 simple_pll = clock_get_simple_pll(clkid);
127 if (!simple_pll) {
128 debug("%s: Uknown simple PLL %d\n", __func__, clkid);
129 return 0;
130 }
131 }
Simon Glass31689872015-06-05 14:39:37 -0600132
Tom Warren795f9d72013-01-23 14:01:01 -0700133 /*
Tom Warrena8480ef2015-06-25 09:50:44 -0700134 * pllinfo has the m/n/p and kcp/kvco mask and shift
135 * values for all of the PLLs used in U-Boot, with any
136 * SoC differences accounted for.
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600137 *
138 * Preserve EN_LOCKDET, etc.
Tom Warren795f9d72013-01-23 14:01:01 -0700139 */
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600140 if (pll)
141 misc_data = readl(&pll->pll_misc);
142 else
143 misc_data = readl(&simple_pll->pll_misc);
144 misc_data &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift);
145 misc_data |= cpcon << pllinfo->kcp_shift;
146 misc_data &= ~(pllinfo->kvco_mask << pllinfo->kvco_shift);
147 misc_data |= lfcon << pllinfo->kvco_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700148
Tom Warrena8480ef2015-06-25 09:50:44 -0700149 data = (divm << pllinfo->m_shift) | (divn << pllinfo->n_shift);
150 data |= divp << pllinfo->p_shift;
151 data |= (1 << PLL_ENABLE_SHIFT); /* BYPASS s/b 0 already */
Tom Warren795f9d72013-01-23 14:01:01 -0700152
Simon Glass6017b9a2015-04-14 21:03:32 -0600153 if (pll) {
154 writel(misc_data, &pll->pll_misc);
155 writel(data, &pll->pll_base);
156 } else {
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600157 writel(misc_data, &simple_pll->pll_misc);
158 writel(data, &simple_pll->pll_base);
Simon Glass6017b9a2015-04-14 21:03:32 -0600159 }
Tom Warren795f9d72013-01-23 14:01:01 -0700160
161 /* calculate the stable time */
162 return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
163}
164
165void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
166 unsigned divisor)
167{
168 u32 *reg = get_periph_source_reg(periph_id);
169 u32 value;
170
171 value = readl(reg);
172
Stephen Warrendf5ed452014-01-24 10:16:19 -0700173 value &= ~OUT_CLK_SOURCE_31_30_MASK;
174 value |= source << OUT_CLK_SOURCE_31_30_SHIFT;
Tom Warren795f9d72013-01-23 14:01:01 -0700175
176 value &= ~OUT_CLK_DIVISOR_MASK;
177 value |= divisor << OUT_CLK_DIVISOR_SHIFT;
178
179 writel(value, reg);
180}
181
Simon Glassd2d1c3f2015-04-14 21:03:33 -0600182int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits,
183 unsigned source)
Tom Warren795f9d72013-01-23 14:01:01 -0700184{
185 u32 *reg = get_periph_source_reg(periph_id);
186
Simon Glassd2d1c3f2015-04-14 21:03:33 -0600187 switch (mux_bits) {
188 case MASK_BITS_31_30:
189 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_30_MASK,
190 source << OUT_CLK_SOURCE_31_30_SHIFT);
191 break;
192
193 case MASK_BITS_31_29:
194 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_29_MASK,
195 source << OUT_CLK_SOURCE_31_29_SHIFT);
196 break;
197
198 case MASK_BITS_31_28:
199 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_28_MASK,
200 source << OUT_CLK_SOURCE_31_28_SHIFT);
201 break;
202
203 default:
204 return -1;
205 }
206
207 return 0;
Tom Warren795f9d72013-01-23 14:01:01 -0700208}
209
Stephen Warren532543c2016-09-13 10:45:56 -0600210static int clock_ll_get_source_bits(enum periph_id periph_id, int mux_bits)
211{
212 u32 *reg = get_periph_source_reg(periph_id);
213 u32 val = readl(reg);
214
215 switch (mux_bits) {
216 case MASK_BITS_31_30:
217 val >>= OUT_CLK_SOURCE_31_30_SHIFT;
218 val &= OUT_CLK_SOURCE_31_30_MASK;
219 return val;
220 case MASK_BITS_31_29:
221 val >>= OUT_CLK_SOURCE_31_29_SHIFT;
222 val &= OUT_CLK_SOURCE_31_29_MASK;
223 return val;
224 case MASK_BITS_31_28:
225 val >>= OUT_CLK_SOURCE_31_28_SHIFT;
226 val &= OUT_CLK_SOURCE_31_28_MASK;
227 return val;
228 default:
229 return -1;
230 }
231}
232
Simon Glassd2d1c3f2015-04-14 21:03:33 -0600233void clock_ll_set_source(enum periph_id periph_id, unsigned source)
234{
235 clock_ll_set_source_bits(periph_id, MASK_BITS_31_30, source);
236}
237
Tom Warren795f9d72013-01-23 14:01:01 -0700238/**
239 * Given the parent's rate and the required rate for the children, this works
240 * out the peripheral clock divider to use, in 7.1 binary format.
241 *
242 * @param divider_bits number of divider bits (8 or 16)
243 * @param parent_rate clock rate of parent clock in Hz
244 * @param rate required clock rate for this clock
245 * @return divider which should be used
246 */
247static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate,
248 unsigned long rate)
249{
250 u64 divider = parent_rate * 2;
251 unsigned max_divider = 1 << divider_bits;
252
253 divider += rate - 1;
254 do_div(divider, rate);
255
256 if ((s64)divider - 2 < 0)
257 return 0;
258
259 if ((s64)divider - 2 >= max_divider)
260 return -1;
261
262 return divider - 2;
263}
264
265int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate)
266{
267 struct clk_pll *pll = get_pll(clkid);
268 int data = 0, div = 0, offset = 0;
269
270 if (!clock_id_is_pll(clkid))
271 return -1;
272
273 if (pllout + 1 > pll_num_clkouts[clkid])
274 return -1;
275
276 div = clk_get_divider(8, pll_rate[clkid], rate);
277
278 if (div < 0)
279 return -1;
280
281 /* out2 and out4 are in the high part of the register */
282 if (pllout == PLL_OUT2 || pllout == PLL_OUT4)
283 offset = 16;
284
285 data = (div << PLL_OUT_RATIO_SHIFT) |
286 PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN;
287 clrsetbits_le32(&pll->pll_out[pllout >> 1],
288 PLL_OUT_RATIO_MASK << offset, data << offset);
289
290 return 0;
291}
292
293/**
294 * Given the parent's rate and the divider in 7.1 format, this works out the
295 * resulting peripheral clock rate.
296 *
297 * @param parent_rate clock rate of parent clock in Hz
298 * @param divider which should be used in 7.1 format
299 * @return effective clock rate of peripheral
300 */
301static unsigned long get_rate_from_divider(unsigned long parent_rate,
302 int divider)
303{
304 u64 rate;
305
306 rate = (u64)parent_rate * 2;
307 do_div(rate, divider + 2);
308 return rate;
309}
310
311unsigned long clock_get_periph_rate(enum periph_id periph_id,
312 enum clock_id parent)
313{
314 u32 *reg = get_periph_source_reg(periph_id);
Stephen Warrene331b202016-09-23 16:44:51 -0600315 unsigned parent_rate = pll_rate[parent];
316 int div = (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT;
317
318 switch (periph_id) {
319 case PERIPH_ID_UART1:
320 case PERIPH_ID_UART2:
321 case PERIPH_ID_UART3:
322 case PERIPH_ID_UART4:
323 case PERIPH_ID_UART5:
324#ifdef CONFIG_TEGRA20
325 /* There's no divider for these clocks in this SoC. */
326 return parent_rate;
327#else
328 /*
329 * This undoes the +2 in get_rate_from_divider() which I
330 * believe is incorrect. Ideally we would fix
331 * get_rate_from_divider(), but... Removing the +2 from
332 * get_rate_from_divider() would probably require remove the -2
333 * from the tail of clk_get_divider() since I believe that's
334 * only there to invert get_rate_from_divider()'s +2. Observe
335 * how find_best_divider() uses those two functions together.
336 * However, doing so breaks other stuff, such as Seaboard's
337 * display, likely due to clock_set_pllout()'s call to
338 * clk_get_divider(). Attempting to fix that by making
339 * clock_set_pllout() subtract 2 from clk_get_divider()'s
340 * return value doesn't help. In summary this clock driver is
341 * quite broken but I'm afraid I have no idea how to fix it
342 * without completely replacing it.
Simon Glass86b89fd2017-05-31 17:57:22 -0600343 *
344 * Be careful to avoid a divide by zero error.
Stephen Warrene331b202016-09-23 16:44:51 -0600345 */
Simon Glass86b89fd2017-05-31 17:57:22 -0600346 if (div >= 1)
347 div -= 2;
Stephen Warrene331b202016-09-23 16:44:51 -0600348 break;
349#endif
350 default:
351 break;
352 }
Tom Warren795f9d72013-01-23 14:01:01 -0700353
Stephen Warrene331b202016-09-23 16:44:51 -0600354 return get_rate_from_divider(parent_rate, div);
Tom Warren795f9d72013-01-23 14:01:01 -0700355}
356
357/**
358 * Find the best available 7.1 format divisor given a parent clock rate and
359 * required child clock rate. This function assumes that a second-stage
360 * divisor is available which can divide by powers of 2 from 1 to 256.
361 *
362 * @param divider_bits number of divider bits (8 or 16)
363 * @param parent_rate clock rate of parent clock in Hz
364 * @param rate required clock rate for this clock
365 * @param extra_div value for the second-stage divisor (not set if this
366 * function returns -1.
367 * @return divider which should be used, or -1 if nothing is valid
368 *
369 */
370static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
371 unsigned long rate, int *extra_div)
372{
373 int shift;
374 int best_divider = -1;
375 int best_error = rate;
376
377 /* try dividers from 1 to 256 and find closest match */
378 for (shift = 0; shift <= 8 && best_error > 0; shift++) {
379 unsigned divided_parent = parent_rate >> shift;
380 int divider = clk_get_divider(divider_bits, divided_parent,
381 rate);
382 unsigned effective_rate = get_rate_from_divider(divided_parent,
383 divider);
384 int error = rate - effective_rate;
385
386 /* Given a valid divider, look for the lowest error */
387 if (divider != -1 && error < best_error) {
388 best_error = error;
389 *extra_div = 1 << shift;
390 best_divider = divider;
391 }
392 }
393
394 /* return what we found - *extra_div will already be set */
395 return best_divider;
396}
397
398/**
399 * Adjust peripheral PLL to use the given divider and source.
400 *
401 * @param periph_id peripheral to adjust
402 * @param source Source number (0-3 or 0-7)
403 * @param mux_bits Number of mux bits (2 or 4)
404 * @param divider Required divider in 7.1 or 15.1 format
405 * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
406 * for this peripheral)
407 */
408static int adjust_periph_pll(enum periph_id periph_id, int source,
409 int mux_bits, unsigned divider)
410{
411 u32 *reg = get_periph_source_reg(periph_id);
412
413 clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
414 divider << OUT_CLK_DIVISOR_SHIFT);
415 udelay(1);
416
417 /* work out the source clock and set it */
418 if (source < 0)
419 return -1;
Tom Warren2fde44e2014-01-24 10:16:22 -0700420
Simon Glassd2d1c3f2015-04-14 21:03:33 -0600421 clock_ll_set_source_bits(periph_id, mux_bits, source);
Tom Warren2fde44e2014-01-24 10:16:22 -0700422
Tom Warren795f9d72013-01-23 14:01:01 -0700423 udelay(2);
424 return 0;
425}
426
Stephen Warren532543c2016-09-13 10:45:56 -0600427enum clock_id clock_get_periph_parent(enum periph_id periph_id)
428{
429 int err, mux_bits, divider_bits, type;
430 int source;
431
432 err = get_periph_clock_info(periph_id, &mux_bits, &divider_bits, &type);
433 if (err)
434 return CLOCK_ID_NONE;
435
436 source = clock_ll_get_source_bits(periph_id, mux_bits);
437
438 return get_periph_clock_id(periph_id, source);
439}
440
Tom Warren795f9d72013-01-23 14:01:01 -0700441unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
442 enum clock_id parent, unsigned rate, int *extra_div)
443{
444 unsigned effective_rate;
445 int mux_bits, divider_bits, source;
446 int divider;
Allen Martin810a4e42013-05-10 16:56:55 +0000447 int xdiv = 0;
Tom Warren795f9d72013-01-23 14:01:01 -0700448
449 /* work out the source clock and set it */
450 source = get_periph_clock_source(periph_id, parent, &mux_bits,
451 &divider_bits);
452
Allen Martin810a4e42013-05-10 16:56:55 +0000453 divider = find_best_divider(divider_bits, pll_rate[parent],
454 rate, &xdiv);
Tom Warren795f9d72013-01-23 14:01:01 -0700455 if (extra_div)
Allen Martin810a4e42013-05-10 16:56:55 +0000456 *extra_div = xdiv;
457
Tom Warren795f9d72013-01-23 14:01:01 -0700458 assert(divider >= 0);
459 if (adjust_periph_pll(periph_id, source, mux_bits, divider))
460 return -1U;
461 debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
462 get_periph_source_reg(periph_id),
463 readl(get_periph_source_reg(periph_id)));
464
465 /* Check what we ended up with. This shouldn't matter though */
466 effective_rate = clock_get_periph_rate(periph_id, parent);
467 if (extra_div)
468 effective_rate /= *extra_div;
469 if (rate != effective_rate)
470 debug("Requested clock rate %u not honored (got %u)\n",
471 rate, effective_rate);
472 return effective_rate;
473}
474
475unsigned clock_start_periph_pll(enum periph_id periph_id,
476 enum clock_id parent, unsigned rate)
477{
478 unsigned effective_rate;
479
480 reset_set_enable(periph_id, 1);
481 clock_enable(periph_id);
Simon Glass1a62f102019-04-01 13:38:38 -0700482 udelay(2);
Tom Warren795f9d72013-01-23 14:01:01 -0700483
484 effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
485 NULL);
486
487 reset_set_enable(periph_id, 0);
488 return effective_rate;
489}
490
491void clock_enable(enum periph_id clkid)
492{
493 clock_set_enable(clkid, 1);
494}
495
496void clock_disable(enum periph_id clkid)
497{
498 clock_set_enable(clkid, 0);
499}
500
501void reset_periph(enum periph_id periph_id, int us_delay)
502{
503 /* Put peripheral into reset */
504 reset_set_enable(periph_id, 1);
505 udelay(us_delay);
506
507 /* Remove reset */
508 reset_set_enable(periph_id, 0);
509
510 udelay(us_delay);
511}
512
513void reset_cmplx_set_enable(int cpu, int which, int reset)
514{
515 struct clk_rst_ctlr *clkrst =
516 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
517 u32 mask;
518
519 /* Form the mask, which depends on the cpu chosen (2 or 4) */
520 assert(cpu >= 0 && cpu < MAX_NUM_CPU);
521 mask = which << cpu;
522
523 /* either enable or disable those reset for that CPU */
524 if (reset)
525 writel(mask, &clkrst->crc_cpu_cmplx_set);
526 else
527 writel(mask, &clkrst->crc_cpu_cmplx_clr);
528}
529
Thierry Redingfa6e24d2015-08-20 11:42:19 +0200530unsigned int __weak clk_m_get_rate(unsigned int parent_rate)
531{
532 return parent_rate;
533}
534
Tom Warren795f9d72013-01-23 14:01:01 -0700535unsigned clock_get_rate(enum clock_id clkid)
536{
537 struct clk_pll *pll;
Tom Warrena8480ef2015-06-25 09:50:44 -0700538 u32 base, divm;
539 u64 parent_rate, rate;
540 struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
Tom Warren795f9d72013-01-23 14:01:01 -0700541
542 parent_rate = osc_freq[clock_get_osc_freq()];
543 if (clkid == CLOCK_ID_OSC)
544 return parent_rate;
545
Thierry Redingfa6e24d2015-08-20 11:42:19 +0200546 if (clkid == CLOCK_ID_CLK_M)
547 return clk_m_get_rate(parent_rate);
548
Tom Warren795f9d72013-01-23 14:01:01 -0700549 pll = get_pll(clkid);
Simon Glass6017b9a2015-04-14 21:03:32 -0600550 if (!pll)
551 return 0;
Tom Warren795f9d72013-01-23 14:01:01 -0700552 base = readl(&pll->pll_base);
553
Tom Warrena8480ef2015-06-25 09:50:44 -0700554 rate = parent_rate * ((base >> pllinfo->n_shift) & pllinfo->n_mask);
555 divm = (base >> pllinfo->m_shift) & pllinfo->m_mask;
556 /*
557 * PLLU uses p_mask/p_shift for VCO on all but T210,
558 * T210 uses normal DIVP. Handled in pllinfo table.
559 */
Stephen Warrenb1c6a8a2015-08-19 17:03:59 -0600560#ifdef CONFIG_TEGRA210
561 /*
562 * PLLP's primary output (pllP_out0) on T210 is the VCO, and divp is
563 * not applied. pllP_out2 does have divp applied. All other pllP_outN
564 * are divided down from pllP_out0. We only support pllP_out0 in
565 * U-Boot at the time of writing this comment.
566 */
567 if (clkid != CLOCK_ID_PERIPH)
568#endif
569 divm <<= (base >> pllinfo->p_shift) & pllinfo->p_mask;
Tom Warren795f9d72013-01-23 14:01:01 -0700570 do_div(rate, divm);
571 return rate;
572}
573
574/**
575 * Set the output frequency you want for each PLL clock.
576 * PLL output frequencies are programmed by setting their N, M and P values.
577 * The governing equations are:
578 * VCO = (Fi / m) * n, Fo = VCO / (2^p)
579 * where Fo is the output frequency from the PLL.
580 * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
581 * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
582 * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
583 *
584 * @param n PLL feedback divider(DIVN)
585 * @param m PLL input divider(DIVN)
586 * @param p post divider(DIVP)
587 * @param cpcon base PLL charge pump(CPCON)
588 * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
Robert P. J. Day8d56db92016-07-15 13:44:45 -0400589 * be overridden), 1 if PLL is already correct
Tom Warren795f9d72013-01-23 14:01:01 -0700590 */
591int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
592{
Tom Warrena8480ef2015-06-25 09:50:44 -0700593 u32 base_reg, misc_reg;
Tom Warren795f9d72013-01-23 14:01:01 -0700594 struct clk_pll *pll;
Tom Warrena8480ef2015-06-25 09:50:44 -0700595 struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
Tom Warren795f9d72013-01-23 14:01:01 -0700596
597 pll = get_pll(clkid);
598
599 base_reg = readl(&pll->pll_base);
600
601 /* Set BYPASS, m, n and p to PLL_BASE */
Tom Warrena8480ef2015-06-25 09:50:44 -0700602 base_reg &= ~(pllinfo->m_mask << pllinfo->m_shift);
603 base_reg |= m << pllinfo->m_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700604
Tom Warrena8480ef2015-06-25 09:50:44 -0700605 base_reg &= ~(pllinfo->n_mask << pllinfo->n_shift);
606 base_reg |= n << pllinfo->n_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700607
Tom Warrena8480ef2015-06-25 09:50:44 -0700608 base_reg &= ~(pllinfo->p_mask << pllinfo->p_shift);
609 base_reg |= p << pllinfo->p_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700610
611 if (clkid == CLOCK_ID_PERIPH) {
612 /*
613 * If the PLL is already set up, check that it is correct
614 * and record this info for clock_verify() to check.
615 */
616 if (base_reg & PLL_BASE_OVRRIDE_MASK) {
617 base_reg |= PLL_ENABLE_MASK;
618 if (base_reg != readl(&pll->pll_base))
619 pllp_valid = 0;
620 return pllp_valid ? 1 : -1;
621 }
622 base_reg |= PLL_BASE_OVRRIDE_MASK;
623 }
624
625 base_reg |= PLL_BYPASS_MASK;
626 writel(base_reg, &pll->pll_base);
627
Tom Warrena8480ef2015-06-25 09:50:44 -0700628 /* Set cpcon (KCP) to PLL_MISC */
Tom Warren795f9d72013-01-23 14:01:01 -0700629 misc_reg = readl(&pll->pll_misc);
Tom Warrena8480ef2015-06-25 09:50:44 -0700630 misc_reg &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift);
631 misc_reg |= cpcon << pllinfo->kcp_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700632 writel(misc_reg, &pll->pll_misc);
633
634 /* Enable PLL */
635 base_reg |= PLL_ENABLE_MASK;
636 writel(base_reg, &pll->pll_base);
637
638 /* Disable BYPASS */
639 base_reg &= ~PLL_BYPASS_MASK;
640 writel(base_reg, &pll->pll_base);
641
642 return 0;
643}
644
645void clock_ll_start_uart(enum periph_id periph_id)
646{
647 /* Assert UART reset and enable clock */
648 reset_set_enable(periph_id, 1);
649 clock_enable(periph_id);
650 clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
651
652 /* wait for 2us */
653 udelay(2);
654
655 /* De-assert reset to UART */
656 reset_set_enable(periph_id, 0);
657}
658
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900659#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassc3f26502017-07-25 08:30:00 -0600660int clock_decode_periph_id(struct udevice *dev)
Tom Warren795f9d72013-01-23 14:01:01 -0700661{
662 enum periph_id id;
663 u32 cell[2];
664 int err;
665
Simon Glassc3f26502017-07-25 08:30:00 -0600666 err = dev_read_u32_array(dev, "clocks", cell, ARRAY_SIZE(cell));
Tom Warren795f9d72013-01-23 14:01:01 -0700667 if (err)
668 return -1;
669 id = clk_id_to_periph_id(cell[1]);
670 assert(clock_periph_id_isvalid(id));
671 return id;
672}
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900673#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Tom Warren795f9d72013-01-23 14:01:01 -0700674
675int clock_verify(void)
676{
677 struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
678 u32 reg = readl(&pll->pll_base);
679
680 if (!pllp_valid) {
681 printf("Warning: PLLP %x is not correct\n", reg);
682 return -1;
683 }
684 debug("PLLP %x is correct\n", reg);
685 return 0;
686}
687
688void clock_init(void)
689{
Stephen Warren1453d102016-09-13 10:45:55 -0600690 int i;
691
Tom Warren27bce712015-06-22 13:03:44 -0700692 pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
Tom Warren795f9d72013-01-23 14:01:01 -0700693 pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
694 pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
Tom Warren27bce712015-06-22 13:03:44 -0700695 pll_rate[CLOCK_ID_USB] = clock_get_rate(CLOCK_ID_USB);
Simon Glass93a19952015-04-14 21:03:34 -0600696 pll_rate[CLOCK_ID_DISPLAY] = clock_get_rate(CLOCK_ID_DISPLAY);
Tom Warren795f9d72013-01-23 14:01:01 -0700697 pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU);
Tom Warren27bce712015-06-22 13:03:44 -0700698 pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
699 pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
Thierry Redingfa6e24d2015-08-20 11:42:19 +0200700 pll_rate[CLOCK_ID_CLK_M] = clock_get_rate(CLOCK_ID_CLK_M);
Tom Warren27bce712015-06-22 13:03:44 -0700701
Tom Warren795f9d72013-01-23 14:01:01 -0700702 debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
Thierry Redingfa6e24d2015-08-20 11:42:19 +0200703 debug("CLKM = %d\n", pll_rate[CLOCK_ID_CLK_M]);
Tom Warren27bce712015-06-22 13:03:44 -0700704 debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]);
Tom Warren795f9d72013-01-23 14:01:01 -0700705 debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
706 debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
Tom Warren27bce712015-06-22 13:03:44 -0700707 debug("PLLU = %d\n", pll_rate[CLOCK_ID_USB]);
Simon Glass93a19952015-04-14 21:03:34 -0600708 debug("PLLD = %d\n", pll_rate[CLOCK_ID_DISPLAY]);
Tom Warren795f9d72013-01-23 14:01:01 -0700709 debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]);
Stephen Warren1453d102016-09-13 10:45:55 -0600710
711 for (i = 0; periph_clk_init_table[i].periph_id != -1; i++) {
712 enum periph_id periph_id;
713 enum clock_id parent;
714 int source, mux_bits, divider_bits;
715
716 periph_id = periph_clk_init_table[i].periph_id;
717 parent = periph_clk_init_table[i].parent_clock_id;
718
719 source = get_periph_clock_source(periph_id, parent, &mux_bits,
720 &divider_bits);
721 clock_ll_set_source_bits(periph_id, mux_bits, source);
722 }
Tom Warren795f9d72013-01-23 14:01:01 -0700723}
Jimmy Zhang2a544db2014-01-24 10:37:36 -0700724
725static void set_avp_clock_source(u32 src)
726{
727 struct clk_rst_ctlr *clkrst =
728 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
729 u32 val;
730
731 val = (src << SCLK_SWAKEUP_FIQ_SOURCE_SHIFT) |
732 (src << SCLK_SWAKEUP_IRQ_SOURCE_SHIFT) |
733 (src << SCLK_SWAKEUP_RUN_SOURCE_SHIFT) |
734 (src << SCLK_SWAKEUP_IDLE_SOURCE_SHIFT) |
735 (SCLK_SYS_STATE_RUN << SCLK_SYS_STATE_SHIFT);
736 writel(val, &clkrst->crc_sclk_brst_pol);
737 udelay(3);
738}
739
740/*
741 * This function is useful on Tegra30, and any later SoCs that have compatible
742 * PLLP configuration registers.
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700743 * NOTE: Not used on Tegra210 - see tegra210_setup_pllp in T210 clock.c
Jimmy Zhang2a544db2014-01-24 10:37:36 -0700744 */
745void tegra30_set_up_pllp(void)
746{
747 struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
748 u32 reg;
749
750 /*
751 * Based on the Tegra TRM, the system clock (which is the AVP clock) can
752 * run up to 275MHz. On power on, the default sytem clock source is set
753 * to PLLP_OUT0. This function sets PLLP's (hence PLLP_OUT0's) rate to
754 * 408MHz which is beyond system clock's upper limit.
755 *
756 * The fix is to set the system clock to CLK_M before initializing PLLP,
757 * and then switch back to PLLP_OUT4, which has an appropriate divider
758 * configured, after PLLP has been configured
759 */
760 set_avp_clock_source(SCLK_SOURCE_CLKM);
761
762 /*
763 * PLLP output frequency set to 408Mhz
764 * PLLC output frequency set to 228Mhz
765 */
766 switch (clock_get_osc_freq()) {
767 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
768 clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8);
769 clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8);
770 break;
771
772 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
773 clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8);
774 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
775 break;
776
777 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
778 clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8);
779 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
780 break;
781 case CLOCK_OSC_FREQ_19_2:
782 default:
783 /*
784 * These are not supported. It is too early to print a
785 * message and the UART likely won't work anyway due to the
786 * oscillator being wrong.
787 */
788 break;
789 }
790
791 /* Set PLLP_OUT1, 2, 3 & 4 freqs to 9.6, 48, 102 & 204MHz */
792
793 /* OUT1, 2 */
794 /* Assert RSTN before enable */
795 reg = PLLP_OUT2_RSTN_EN | PLLP_OUT1_RSTN_EN;
796 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
797 /* Set divisor and reenable */
798 reg = (IN_408_OUT_48_DIVISOR << PLLP_OUT2_RATIO)
799 | PLLP_OUT2_OVR | PLLP_OUT2_CLKEN | PLLP_OUT2_RSTN_DIS
800 | (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO)
801 | PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS;
802 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
803
804 /* OUT3, 4 */
805 /* Assert RSTN before enable */
806 reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN;
807 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
808 /* Set divisor and reenable */
809 reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO)
810 | PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS
811 | (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO)
812 | PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS;
813 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
814
815 set_avp_clock_source(SCLK_SOURCE_PLLP_OUT4);
816}
Simon Glasscd4b59b2015-06-05 14:39:36 -0600817
818int clock_external_output(int clk_id)
819{
Thierry Redingce7eb162019-04-15 11:32:25 +0200820 u32 val;
Simon Glasscd4b59b2015-06-05 14:39:36 -0600821
822 if (clk_id >= 1 && clk_id <= 3) {
Thierry Redingce7eb162019-04-15 11:32:25 +0200823 val = tegra_pmc_readl(offsetof(struct pmc_ctlr,
824 pmc_clk_out_cntrl));
825 val |= 1 << (2 + (clk_id - 1) * 8);
826 tegra_pmc_writel(val,
827 offsetof(struct pmc_ctlr,
828 pmc_clk_out_cntrl));
829
Simon Glasscd4b59b2015-06-05 14:39:36 -0600830 } else {
831 printf("%s: Unknown output clock id %d\n", __func__, clk_id);
832 return -EINVAL;
833 }
834
835 return 0;
836}
Simon Glass2b4029a2017-05-31 17:57:16 -0600837
838__weak bool clock_early_init_done(void)
839{
840 return true;
841}