blob: 77c8ad978e56d6ea658b63e21e78bd68e3df715a [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>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Tom Warren795f9d72013-01-23 14:01:01 -070022
23/*
24 * This is our record of the current clock rate of each clock. We don't
25 * fill all of these in since we are only really interested in clocks which
26 * we use as parents.
27 */
28static unsigned pll_rate[CLOCK_ID_COUNT];
29
30/*
31 * The oscillator frequency is fixed to one of four set values. Based on this
32 * the other clocks are set up appropriately.
33 */
34static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
35 13000000,
36 19200000,
37 12000000,
38 26000000,
Tom Warren27bce712015-06-22 13:03:44 -070039 38400000,
40 48000000,
Tom Warren795f9d72013-01-23 14:01:01 -070041};
42
43/* return 1 if a peripheral ID is in range */
44#define clock_type_id_isvalid(id) ((id) >= 0 && \
45 (id) < CLOCK_TYPE_COUNT)
46
47char pllp_valid = 1; /* PLLP is set up correctly */
48
49/* return 1 if a periphc_internal_id is in range */
50#define periphc_internal_id_isvalid(id) ((id) >= 0 && \
51 (id) < PERIPHC_COUNT)
52
53/* number of clock outputs of a PLL */
54static const u8 pll_num_clkouts[] = {
55 1, /* PLLC */
56 1, /* PLLM */
57 4, /* PLLP */
58 1, /* PLLA */
59 0, /* PLLU */
60 0, /* PLLD */
61};
62
63int clock_get_osc_bypass(void)
64{
65 struct clk_rst_ctlr *clkrst =
66 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
67 u32 reg;
68
69 reg = readl(&clkrst->crc_osc_ctrl);
70 return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT;
71}
72
73/* Returns a pointer to the registers of the given pll */
74static struct clk_pll *get_pll(enum clock_id clkid)
75{
76 struct clk_rst_ctlr *clkrst =
77 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
78
79 assert(clock_id_is_pll(clkid));
Simon Glass6017b9a2015-04-14 21:03:32 -060080 if (clkid >= (enum clock_id)TEGRA_CLK_PLLS) {
Simon Glass31689872015-06-05 14:39:37 -060081 debug("%s: Invalid PLL %d\n", __func__, clkid);
Simon Glass6017b9a2015-04-14 21:03:32 -060082 return NULL;
83 }
Tom Warren795f9d72013-01-23 14:01:01 -070084 return &clkrst->crc_pll[clkid];
85}
86
Simon Glass6017b9a2015-04-14 21:03:32 -060087__weak struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid)
88{
89 return NULL;
90}
91
Tom Warren795f9d72013-01-23 14:01:01 -070092int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
93 u32 *divp, u32 *cpcon, u32 *lfcon)
94{
95 struct clk_pll *pll = get_pll(clkid);
Tom Warrena8480ef2015-06-25 09:50:44 -070096 struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
Tom Warren795f9d72013-01-23 14:01:01 -070097 u32 data;
98
99 assert(clkid != CLOCK_ID_USB);
100
101 /* Safety check, adds to code size but is small */
102 if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB)
103 return -1;
104 data = readl(&pll->pll_base);
Tom Warrena8480ef2015-06-25 09:50:44 -0700105 *divm = (data >> pllinfo->m_shift) & pllinfo->m_mask;
106 *divn = (data >> pllinfo->n_shift) & pllinfo->n_mask;
107 *divp = (data >> pllinfo->p_shift) & pllinfo->p_mask;
Tom Warren795f9d72013-01-23 14:01:01 -0700108 data = readl(&pll->pll_misc);
Tom Warrena8480ef2015-06-25 09:50:44 -0700109 /* NOTE: On T210, cpcon/lfcon no longer exist, moved to KCP/KVCO */
110 *cpcon = (data >> pllinfo->kcp_shift) & pllinfo->kcp_mask;
111 *lfcon = (data >> pllinfo->kvco_shift) & pllinfo->kvco_mask;
112
Tom Warren795f9d72013-01-23 14:01:01 -0700113 return 0;
114}
115
116unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
117 u32 divp, u32 cpcon, u32 lfcon)
118{
Simon Glass31689872015-06-05 14:39:37 -0600119 struct clk_pll *pll = NULL;
Tom Warrena8480ef2015-06-25 09:50:44 -0700120 struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600121 struct clk_pll_simple *simple_pll = NULL;
Simon Glass6017b9a2015-04-14 21:03:32 -0600122 u32 misc_data, data;
Tom Warren795f9d72013-01-23 14:01:01 -0700123
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600124 if (clkid < (enum clock_id)TEGRA_CLK_PLLS) {
Simon Glass31689872015-06-05 14:39:37 -0600125 pll = get_pll(clkid);
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600126 } else {
127 simple_pll = clock_get_simple_pll(clkid);
128 if (!simple_pll) {
129 debug("%s: Uknown simple PLL %d\n", __func__, clkid);
130 return 0;
131 }
132 }
Simon Glass31689872015-06-05 14:39:37 -0600133
Tom Warren795f9d72013-01-23 14:01:01 -0700134 /*
Tom Warrena8480ef2015-06-25 09:50:44 -0700135 * pllinfo has the m/n/p and kcp/kvco mask and shift
136 * values for all of the PLLs used in U-Boot, with any
137 * SoC differences accounted for.
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600138 *
139 * Preserve EN_LOCKDET, etc.
Tom Warren795f9d72013-01-23 14:01:01 -0700140 */
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600141 if (pll)
142 misc_data = readl(&pll->pll_misc);
143 else
144 misc_data = readl(&simple_pll->pll_misc);
145 misc_data &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift);
146 misc_data |= cpcon << pllinfo->kcp_shift;
147 misc_data &= ~(pllinfo->kvco_mask << pllinfo->kvco_shift);
148 misc_data |= lfcon << pllinfo->kvco_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700149
Tom Warrena8480ef2015-06-25 09:50:44 -0700150 data = (divm << pllinfo->m_shift) | (divn << pllinfo->n_shift);
151 data |= divp << pllinfo->p_shift;
152 data |= (1 << PLL_ENABLE_SHIFT); /* BYPASS s/b 0 already */
Tom Warren795f9d72013-01-23 14:01:01 -0700153
Simon Glass6017b9a2015-04-14 21:03:32 -0600154 if (pll) {
155 writel(misc_data, &pll->pll_misc);
156 writel(data, &pll->pll_base);
157 } else {
Simon Glassb6ba3fb2015-08-10 07:14:36 -0600158 writel(misc_data, &simple_pll->pll_misc);
159 writel(data, &simple_pll->pll_base);
Simon Glass6017b9a2015-04-14 21:03:32 -0600160 }
Tom Warren795f9d72013-01-23 14:01:01 -0700161
162 /* calculate the stable time */
163 return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
164}
165
166void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
167 unsigned divisor)
168{
169 u32 *reg = get_periph_source_reg(periph_id);
170 u32 value;
171
172 value = readl(reg);
173
Stephen Warrendf5ed452014-01-24 10:16:19 -0700174 value &= ~OUT_CLK_SOURCE_31_30_MASK;
175 value |= source << OUT_CLK_SOURCE_31_30_SHIFT;
Tom Warren795f9d72013-01-23 14:01:01 -0700176
177 value &= ~OUT_CLK_DIVISOR_MASK;
178 value |= divisor << OUT_CLK_DIVISOR_SHIFT;
179
180 writel(value, reg);
181}
182
Simon Glassd2d1c3f2015-04-14 21:03:33 -0600183int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits,
184 unsigned source)
Tom Warren795f9d72013-01-23 14:01:01 -0700185{
186 u32 *reg = get_periph_source_reg(periph_id);
187
Simon Glassd2d1c3f2015-04-14 21:03:33 -0600188 switch (mux_bits) {
189 case MASK_BITS_31_30:
190 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_30_MASK,
191 source << OUT_CLK_SOURCE_31_30_SHIFT);
192 break;
193
194 case MASK_BITS_31_29:
195 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_29_MASK,
196 source << OUT_CLK_SOURCE_31_29_SHIFT);
197 break;
198
199 case MASK_BITS_31_28:
200 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_28_MASK,
201 source << OUT_CLK_SOURCE_31_28_SHIFT);
202 break;
203
204 default:
205 return -1;
206 }
207
208 return 0;
Tom Warren795f9d72013-01-23 14:01:01 -0700209}
210
Stephen Warren532543c2016-09-13 10:45:56 -0600211static int clock_ll_get_source_bits(enum periph_id periph_id, int mux_bits)
212{
213 u32 *reg = get_periph_source_reg(periph_id);
214 u32 val = readl(reg);
215
216 switch (mux_bits) {
217 case MASK_BITS_31_30:
218 val >>= OUT_CLK_SOURCE_31_30_SHIFT;
219 val &= OUT_CLK_SOURCE_31_30_MASK;
220 return val;
221 case MASK_BITS_31_29:
222 val >>= OUT_CLK_SOURCE_31_29_SHIFT;
223 val &= OUT_CLK_SOURCE_31_29_MASK;
224 return val;
225 case MASK_BITS_31_28:
226 val >>= OUT_CLK_SOURCE_31_28_SHIFT;
227 val &= OUT_CLK_SOURCE_31_28_MASK;
228 return val;
229 default:
230 return -1;
231 }
232}
233
Simon Glassd2d1c3f2015-04-14 21:03:33 -0600234void clock_ll_set_source(enum periph_id periph_id, unsigned source)
235{
236 clock_ll_set_source_bits(periph_id, MASK_BITS_31_30, source);
237}
238
Tom Warren795f9d72013-01-23 14:01:01 -0700239/**
240 * Given the parent's rate and the required rate for the children, this works
241 * out the peripheral clock divider to use, in 7.1 binary format.
242 *
243 * @param divider_bits number of divider bits (8 or 16)
244 * @param parent_rate clock rate of parent clock in Hz
245 * @param rate required clock rate for this clock
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100246 * Return: divider which should be used
Tom Warren795f9d72013-01-23 14:01:01 -0700247 */
248static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate,
249 unsigned long rate)
250{
251 u64 divider = parent_rate * 2;
252 unsigned max_divider = 1 << divider_bits;
253
254 divider += rate - 1;
255 do_div(divider, rate);
256
257 if ((s64)divider - 2 < 0)
258 return 0;
259
260 if ((s64)divider - 2 >= max_divider)
261 return -1;
262
263 return divider - 2;
264}
265
266int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate)
267{
268 struct clk_pll *pll = get_pll(clkid);
269 int data = 0, div = 0, offset = 0;
270
271 if (!clock_id_is_pll(clkid))
272 return -1;
273
274 if (pllout + 1 > pll_num_clkouts[clkid])
275 return -1;
276
277 div = clk_get_divider(8, pll_rate[clkid], rate);
278
279 if (div < 0)
280 return -1;
281
282 /* out2 and out4 are in the high part of the register */
283 if (pllout == PLL_OUT2 || pllout == PLL_OUT4)
284 offset = 16;
285
286 data = (div << PLL_OUT_RATIO_SHIFT) |
287 PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN;
288 clrsetbits_le32(&pll->pll_out[pllout >> 1],
289 PLL_OUT_RATIO_MASK << offset, data << offset);
290
291 return 0;
292}
293
294/**
295 * Given the parent's rate and the divider in 7.1 format, this works out the
296 * resulting peripheral clock rate.
297 *
298 * @param parent_rate clock rate of parent clock in Hz
299 * @param divider which should be used in 7.1 format
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100300 * Return: effective clock rate of peripheral
Tom Warren795f9d72013-01-23 14:01:01 -0700301 */
302static unsigned long get_rate_from_divider(unsigned long parent_rate,
303 int divider)
304{
305 u64 rate;
306
307 rate = (u64)parent_rate * 2;
308 do_div(rate, divider + 2);
309 return rate;
310}
311
312unsigned long clock_get_periph_rate(enum periph_id periph_id,
313 enum clock_id parent)
314{
315 u32 *reg = get_periph_source_reg(periph_id);
Stephen Warrene331b202016-09-23 16:44:51 -0600316 unsigned parent_rate = pll_rate[parent];
317 int div = (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT;
318
319 switch (periph_id) {
320 case PERIPH_ID_UART1:
321 case PERIPH_ID_UART2:
322 case PERIPH_ID_UART3:
323 case PERIPH_ID_UART4:
324 case PERIPH_ID_UART5:
325#ifdef CONFIG_TEGRA20
326 /* There's no divider for these clocks in this SoC. */
327 return parent_rate;
328#else
329 /*
330 * This undoes the +2 in get_rate_from_divider() which I
331 * believe is incorrect. Ideally we would fix
332 * get_rate_from_divider(), but... Removing the +2 from
333 * get_rate_from_divider() would probably require remove the -2
334 * from the tail of clk_get_divider() since I believe that's
335 * only there to invert get_rate_from_divider()'s +2. Observe
336 * how find_best_divider() uses those two functions together.
337 * However, doing so breaks other stuff, such as Seaboard's
338 * display, likely due to clock_set_pllout()'s call to
339 * clk_get_divider(). Attempting to fix that by making
340 * clock_set_pllout() subtract 2 from clk_get_divider()'s
341 * return value doesn't help. In summary this clock driver is
342 * quite broken but I'm afraid I have no idea how to fix it
343 * without completely replacing it.
Simon Glass86b89fd2017-05-31 17:57:22 -0600344 *
345 * Be careful to avoid a divide by zero error.
Stephen Warrene331b202016-09-23 16:44:51 -0600346 */
Simon Glass86b89fd2017-05-31 17:57:22 -0600347 if (div >= 1)
348 div -= 2;
Stephen Warrene331b202016-09-23 16:44:51 -0600349 break;
350#endif
351 default:
352 break;
353 }
Tom Warren795f9d72013-01-23 14:01:01 -0700354
Stephen Warrene331b202016-09-23 16:44:51 -0600355 return get_rate_from_divider(parent_rate, div);
Tom Warren795f9d72013-01-23 14:01:01 -0700356}
357
358/**
359 * Find the best available 7.1 format divisor given a parent clock rate and
360 * required child clock rate. This function assumes that a second-stage
361 * divisor is available which can divide by powers of 2 from 1 to 256.
362 *
363 * @param divider_bits number of divider bits (8 or 16)
364 * @param parent_rate clock rate of parent clock in Hz
365 * @param rate required clock rate for this clock
366 * @param extra_div value for the second-stage divisor (not set if this
367 * function returns -1.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100368 * Return: divider which should be used, or -1 if nothing is valid
Tom Warren795f9d72013-01-23 14:01:01 -0700369 *
370 */
371static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
372 unsigned long rate, int *extra_div)
373{
374 int shift;
375 int best_divider = -1;
376 int best_error = rate;
377
378 /* try dividers from 1 to 256 and find closest match */
379 for (shift = 0; shift <= 8 && best_error > 0; shift++) {
380 unsigned divided_parent = parent_rate >> shift;
381 int divider = clk_get_divider(divider_bits, divided_parent,
382 rate);
383 unsigned effective_rate = get_rate_from_divider(divided_parent,
384 divider);
385 int error = rate - effective_rate;
386
387 /* Given a valid divider, look for the lowest error */
388 if (divider != -1 && error < best_error) {
389 best_error = error;
390 *extra_div = 1 << shift;
391 best_divider = divider;
392 }
393 }
394
395 /* return what we found - *extra_div will already be set */
396 return best_divider;
397}
398
399/**
400 * Adjust peripheral PLL to use the given divider and source.
401 *
402 * @param periph_id peripheral to adjust
403 * @param source Source number (0-3 or 0-7)
404 * @param mux_bits Number of mux bits (2 or 4)
405 * @param divider Required divider in 7.1 or 15.1 format
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100406 * Return: 0 if ok, -1 on error (requesting a parent clock which is not valid
Tom Warren795f9d72013-01-23 14:01:01 -0700407 * for this peripheral)
408 */
409static int adjust_periph_pll(enum periph_id periph_id, int source,
410 int mux_bits, unsigned divider)
411{
412 u32 *reg = get_periph_source_reg(periph_id);
413
414 clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
415 divider << OUT_CLK_DIVISOR_SHIFT);
416 udelay(1);
417
418 /* work out the source clock and set it */
419 if (source < 0)
420 return -1;
Tom Warren2fde44e2014-01-24 10:16:22 -0700421
Simon Glassd2d1c3f2015-04-14 21:03:33 -0600422 clock_ll_set_source_bits(periph_id, mux_bits, source);
Tom Warren2fde44e2014-01-24 10:16:22 -0700423
Tom Warren795f9d72013-01-23 14:01:01 -0700424 udelay(2);
425 return 0;
426}
427
Stephen Warren532543c2016-09-13 10:45:56 -0600428enum clock_id clock_get_periph_parent(enum periph_id periph_id)
429{
430 int err, mux_bits, divider_bits, type;
431 int source;
432
433 err = get_periph_clock_info(periph_id, &mux_bits, &divider_bits, &type);
434 if (err)
435 return CLOCK_ID_NONE;
436
437 source = clock_ll_get_source_bits(periph_id, mux_bits);
438
439 return get_periph_clock_id(periph_id, source);
440}
441
Tom Warren795f9d72013-01-23 14:01:01 -0700442unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
443 enum clock_id parent, unsigned rate, int *extra_div)
444{
445 unsigned effective_rate;
446 int mux_bits, divider_bits, source;
447 int divider;
Allen Martin810a4e42013-05-10 16:56:55 +0000448 int xdiv = 0;
Tom Warren795f9d72013-01-23 14:01:01 -0700449
450 /* work out the source clock and set it */
451 source = get_periph_clock_source(periph_id, parent, &mux_bits,
452 &divider_bits);
453
Allen Martin810a4e42013-05-10 16:56:55 +0000454 divider = find_best_divider(divider_bits, pll_rate[parent],
455 rate, &xdiv);
Tom Warren795f9d72013-01-23 14:01:01 -0700456 if (extra_div)
Allen Martin810a4e42013-05-10 16:56:55 +0000457 *extra_div = xdiv;
458
Tom Warren795f9d72013-01-23 14:01:01 -0700459 assert(divider >= 0);
460 if (adjust_periph_pll(periph_id, source, mux_bits, divider))
461 return -1U;
462 debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
463 get_periph_source_reg(periph_id),
464 readl(get_periph_source_reg(periph_id)));
465
466 /* Check what we ended up with. This shouldn't matter though */
467 effective_rate = clock_get_periph_rate(periph_id, parent);
468 if (extra_div)
469 effective_rate /= *extra_div;
470 if (rate != effective_rate)
471 debug("Requested clock rate %u not honored (got %u)\n",
472 rate, effective_rate);
473 return effective_rate;
474}
475
476unsigned clock_start_periph_pll(enum periph_id periph_id,
477 enum clock_id parent, unsigned rate)
478{
479 unsigned effective_rate;
480
481 reset_set_enable(periph_id, 1);
482 clock_enable(periph_id);
Simon Glass1a62f102019-04-01 13:38:38 -0700483 udelay(2);
Tom Warren795f9d72013-01-23 14:01:01 -0700484
485 effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
486 NULL);
487
488 reset_set_enable(periph_id, 0);
489 return effective_rate;
490}
491
492void clock_enable(enum periph_id clkid)
493{
494 clock_set_enable(clkid, 1);
495}
496
497void clock_disable(enum periph_id clkid)
498{
499 clock_set_enable(clkid, 0);
500}
501
502void reset_periph(enum periph_id periph_id, int us_delay)
503{
504 /* Put peripheral into reset */
505 reset_set_enable(periph_id, 1);
506 udelay(us_delay);
507
508 /* Remove reset */
509 reset_set_enable(periph_id, 0);
510
511 udelay(us_delay);
512}
513
514void reset_cmplx_set_enable(int cpu, int which, int reset)
515{
516 struct clk_rst_ctlr *clkrst =
517 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
518 u32 mask;
519
520 /* Form the mask, which depends on the cpu chosen (2 or 4) */
521 assert(cpu >= 0 && cpu < MAX_NUM_CPU);
522 mask = which << cpu;
523
524 /* either enable or disable those reset for that CPU */
525 if (reset)
526 writel(mask, &clkrst->crc_cpu_cmplx_set);
527 else
528 writel(mask, &clkrst->crc_cpu_cmplx_clr);
529}
530
Thierry Redingfa6e24d2015-08-20 11:42:19 +0200531unsigned int __weak clk_m_get_rate(unsigned int parent_rate)
532{
533 return parent_rate;
534}
535
Tom Warren795f9d72013-01-23 14:01:01 -0700536unsigned clock_get_rate(enum clock_id clkid)
537{
538 struct clk_pll *pll;
Tom Warrena8480ef2015-06-25 09:50:44 -0700539 u32 base, divm;
540 u64 parent_rate, rate;
541 struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
Tom Warren795f9d72013-01-23 14:01:01 -0700542
543 parent_rate = osc_freq[clock_get_osc_freq()];
544 if (clkid == CLOCK_ID_OSC)
545 return parent_rate;
546
Thierry Redingfa6e24d2015-08-20 11:42:19 +0200547 if (clkid == CLOCK_ID_CLK_M)
548 return clk_m_get_rate(parent_rate);
549
Tom Warren795f9d72013-01-23 14:01:01 -0700550 pll = get_pll(clkid);
Simon Glass6017b9a2015-04-14 21:03:32 -0600551 if (!pll)
552 return 0;
Tom Warren795f9d72013-01-23 14:01:01 -0700553 base = readl(&pll->pll_base);
554
Tom Warrena8480ef2015-06-25 09:50:44 -0700555 rate = parent_rate * ((base >> pllinfo->n_shift) & pllinfo->n_mask);
556 divm = (base >> pllinfo->m_shift) & pllinfo->m_mask;
557 /*
558 * PLLU uses p_mask/p_shift for VCO on all but T210,
559 * T210 uses normal DIVP. Handled in pllinfo table.
560 */
Stephen Warrenb1c6a8a2015-08-19 17:03:59 -0600561#ifdef CONFIG_TEGRA210
562 /*
563 * PLLP's primary output (pllP_out0) on T210 is the VCO, and divp is
564 * not applied. pllP_out2 does have divp applied. All other pllP_outN
565 * are divided down from pllP_out0. We only support pllP_out0 in
566 * U-Boot at the time of writing this comment.
567 */
568 if (clkid != CLOCK_ID_PERIPH)
569#endif
570 divm <<= (base >> pllinfo->p_shift) & pllinfo->p_mask;
Tom Warren795f9d72013-01-23 14:01:01 -0700571 do_div(rate, divm);
572 return rate;
573}
574
575/**
576 * Set the output frequency you want for each PLL clock.
577 * PLL output frequencies are programmed by setting their N, M and P values.
578 * The governing equations are:
579 * VCO = (Fi / m) * n, Fo = VCO / (2^p)
580 * where Fo is the output frequency from the PLL.
581 * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
582 * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
583 * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
584 *
585 * @param n PLL feedback divider(DIVN)
586 * @param m PLL input divider(DIVN)
587 * @param p post divider(DIVP)
588 * @param cpcon base PLL charge pump(CPCON)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100589 * Return: 0 if ok, -1 on error (the requested PLL is incorrect and cannot
Robert P. J. Day8d56db92016-07-15 13:44:45 -0400590 * be overridden), 1 if PLL is already correct
Tom Warren795f9d72013-01-23 14:01:01 -0700591 */
592int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
593{
Tom Warrena8480ef2015-06-25 09:50:44 -0700594 u32 base_reg, misc_reg;
Tom Warren795f9d72013-01-23 14:01:01 -0700595 struct clk_pll *pll;
Tom Warrena8480ef2015-06-25 09:50:44 -0700596 struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
Tom Warren795f9d72013-01-23 14:01:01 -0700597
598 pll = get_pll(clkid);
599
600 base_reg = readl(&pll->pll_base);
601
602 /* Set BYPASS, m, n and p to PLL_BASE */
Tom Warrena8480ef2015-06-25 09:50:44 -0700603 base_reg &= ~(pllinfo->m_mask << pllinfo->m_shift);
604 base_reg |= m << pllinfo->m_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700605
Tom Warrena8480ef2015-06-25 09:50:44 -0700606 base_reg &= ~(pllinfo->n_mask << pllinfo->n_shift);
607 base_reg |= n << pllinfo->n_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700608
Tom Warrena8480ef2015-06-25 09:50:44 -0700609 base_reg &= ~(pllinfo->p_mask << pllinfo->p_shift);
610 base_reg |= p << pllinfo->p_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700611
612 if (clkid == CLOCK_ID_PERIPH) {
613 /*
614 * If the PLL is already set up, check that it is correct
615 * and record this info for clock_verify() to check.
616 */
617 if (base_reg & PLL_BASE_OVRRIDE_MASK) {
618 base_reg |= PLL_ENABLE_MASK;
619 if (base_reg != readl(&pll->pll_base))
620 pllp_valid = 0;
621 return pllp_valid ? 1 : -1;
622 }
623 base_reg |= PLL_BASE_OVRRIDE_MASK;
624 }
625
626 base_reg |= PLL_BYPASS_MASK;
627 writel(base_reg, &pll->pll_base);
628
Tom Warrena8480ef2015-06-25 09:50:44 -0700629 /* Set cpcon (KCP) to PLL_MISC */
Tom Warren795f9d72013-01-23 14:01:01 -0700630 misc_reg = readl(&pll->pll_misc);
Tom Warrena8480ef2015-06-25 09:50:44 -0700631 misc_reg &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift);
632 misc_reg |= cpcon << pllinfo->kcp_shift;
Tom Warren795f9d72013-01-23 14:01:01 -0700633 writel(misc_reg, &pll->pll_misc);
634
635 /* Enable PLL */
636 base_reg |= PLL_ENABLE_MASK;
637 writel(base_reg, &pll->pll_base);
638
639 /* Disable BYPASS */
640 base_reg &= ~PLL_BYPASS_MASK;
641 writel(base_reg, &pll->pll_base);
642
643 return 0;
644}
645
646void clock_ll_start_uart(enum periph_id periph_id)
647{
648 /* Assert UART reset and enable clock */
649 reset_set_enable(periph_id, 1);
650 clock_enable(periph_id);
651 clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
652
653 /* wait for 2us */
654 udelay(2);
655
656 /* De-assert reset to UART */
657 reset_set_enable(periph_id, 0);
658}
659
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900660#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassc3f26502017-07-25 08:30:00 -0600661int clock_decode_periph_id(struct udevice *dev)
Tom Warren795f9d72013-01-23 14:01:01 -0700662{
663 enum periph_id id;
664 u32 cell[2];
665 int err;
666
Simon Glassc3f26502017-07-25 08:30:00 -0600667 err = dev_read_u32_array(dev, "clocks", cell, ARRAY_SIZE(cell));
Tom Warren795f9d72013-01-23 14:01:01 -0700668 if (err)
669 return -1;
670 id = clk_id_to_periph_id(cell[1]);
671 assert(clock_periph_id_isvalid(id));
672 return id;
673}
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900674#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Tom Warren795f9d72013-01-23 14:01:01 -0700675
676int clock_verify(void)
677{
678 struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
679 u32 reg = readl(&pll->pll_base);
680
681 if (!pllp_valid) {
682 printf("Warning: PLLP %x is not correct\n", reg);
683 return -1;
684 }
685 debug("PLLP %x is correct\n", reg);
686 return 0;
687}
688
689void clock_init(void)
690{
Stephen Warren1453d102016-09-13 10:45:55 -0600691 int i;
692
Tom Warren27bce712015-06-22 13:03:44 -0700693 pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
Tom Warren795f9d72013-01-23 14:01:01 -0700694 pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
695 pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
Tom Warren27bce712015-06-22 13:03:44 -0700696 pll_rate[CLOCK_ID_USB] = clock_get_rate(CLOCK_ID_USB);
Simon Glass93a19952015-04-14 21:03:34 -0600697 pll_rate[CLOCK_ID_DISPLAY] = clock_get_rate(CLOCK_ID_DISPLAY);
Tom Warren795f9d72013-01-23 14:01:01 -0700698 pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU);
Tom Warren27bce712015-06-22 13:03:44 -0700699 pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
700 pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
Thierry Redingfa6e24d2015-08-20 11:42:19 +0200701 pll_rate[CLOCK_ID_CLK_M] = clock_get_rate(CLOCK_ID_CLK_M);
Tom Warren27bce712015-06-22 13:03:44 -0700702
Tom Warren795f9d72013-01-23 14:01:01 -0700703 debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
Thierry Redingfa6e24d2015-08-20 11:42:19 +0200704 debug("CLKM = %d\n", pll_rate[CLOCK_ID_CLK_M]);
Tom Warren27bce712015-06-22 13:03:44 -0700705 debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]);
Tom Warren795f9d72013-01-23 14:01:01 -0700706 debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
707 debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
Tom Warren27bce712015-06-22 13:03:44 -0700708 debug("PLLU = %d\n", pll_rate[CLOCK_ID_USB]);
Simon Glass93a19952015-04-14 21:03:34 -0600709 debug("PLLD = %d\n", pll_rate[CLOCK_ID_DISPLAY]);
Tom Warren795f9d72013-01-23 14:01:01 -0700710 debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]);
Stephen Warren1453d102016-09-13 10:45:55 -0600711
712 for (i = 0; periph_clk_init_table[i].periph_id != -1; i++) {
713 enum periph_id periph_id;
714 enum clock_id parent;
715 int source, mux_bits, divider_bits;
716
717 periph_id = periph_clk_init_table[i].periph_id;
718 parent = periph_clk_init_table[i].parent_clock_id;
719
720 source = get_periph_clock_source(periph_id, parent, &mux_bits,
721 &divider_bits);
722 clock_ll_set_source_bits(periph_id, mux_bits, source);
723 }
Tom Warren795f9d72013-01-23 14:01:01 -0700724}
Jimmy Zhang2a544db2014-01-24 10:37:36 -0700725
726static void set_avp_clock_source(u32 src)
727{
728 struct clk_rst_ctlr *clkrst =
729 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
730 u32 val;
731
732 val = (src << SCLK_SWAKEUP_FIQ_SOURCE_SHIFT) |
733 (src << SCLK_SWAKEUP_IRQ_SOURCE_SHIFT) |
734 (src << SCLK_SWAKEUP_RUN_SOURCE_SHIFT) |
735 (src << SCLK_SWAKEUP_IDLE_SOURCE_SHIFT) |
736 (SCLK_SYS_STATE_RUN << SCLK_SYS_STATE_SHIFT);
737 writel(val, &clkrst->crc_sclk_brst_pol);
738 udelay(3);
739}
740
741/*
742 * This function is useful on Tegra30, and any later SoCs that have compatible
743 * PLLP configuration registers.
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700744 * NOTE: Not used on Tegra210 - see tegra210_setup_pllp in T210 clock.c
Jimmy Zhang2a544db2014-01-24 10:37:36 -0700745 */
746void tegra30_set_up_pllp(void)
747{
748 struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
749 u32 reg;
750
751 /*
752 * Based on the Tegra TRM, the system clock (which is the AVP clock) can
753 * run up to 275MHz. On power on, the default sytem clock source is set
754 * to PLLP_OUT0. This function sets PLLP's (hence PLLP_OUT0's) rate to
755 * 408MHz which is beyond system clock's upper limit.
756 *
757 * The fix is to set the system clock to CLK_M before initializing PLLP,
758 * and then switch back to PLLP_OUT4, which has an appropriate divider
759 * configured, after PLLP has been configured
760 */
761 set_avp_clock_source(SCLK_SOURCE_CLKM);
762
763 /*
764 * PLLP output frequency set to 408Mhz
765 * PLLC output frequency set to 228Mhz
766 */
767 switch (clock_get_osc_freq()) {
768 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
769 clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8);
770 clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8);
771 break;
772
773 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
774 clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8);
775 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
776 break;
777
778 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
779 clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8);
780 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
781 break;
782 case CLOCK_OSC_FREQ_19_2:
783 default:
784 /*
785 * These are not supported. It is too early to print a
786 * message and the UART likely won't work anyway due to the
787 * oscillator being wrong.
788 */
789 break;
790 }
791
792 /* Set PLLP_OUT1, 2, 3 & 4 freqs to 9.6, 48, 102 & 204MHz */
793
794 /* OUT1, 2 */
795 /* Assert RSTN before enable */
796 reg = PLLP_OUT2_RSTN_EN | PLLP_OUT1_RSTN_EN;
797 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
798 /* Set divisor and reenable */
799 reg = (IN_408_OUT_48_DIVISOR << PLLP_OUT2_RATIO)
800 | PLLP_OUT2_OVR | PLLP_OUT2_CLKEN | PLLP_OUT2_RSTN_DIS
801 | (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO)
802 | PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS;
803 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
804
805 /* OUT3, 4 */
806 /* Assert RSTN before enable */
807 reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN;
808 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
809 /* Set divisor and reenable */
810 reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO)
811 | PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS
812 | (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO)
813 | PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS;
814 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
815
816 set_avp_clock_source(SCLK_SOURCE_PLLP_OUT4);
817}
Simon Glasscd4b59b2015-06-05 14:39:36 -0600818
819int clock_external_output(int clk_id)
820{
Thierry Redingce7eb162019-04-15 11:32:25 +0200821 u32 val;
Simon Glasscd4b59b2015-06-05 14:39:36 -0600822
823 if (clk_id >= 1 && clk_id <= 3) {
Thierry Redingce7eb162019-04-15 11:32:25 +0200824 val = tegra_pmc_readl(offsetof(struct pmc_ctlr,
825 pmc_clk_out_cntrl));
826 val |= 1 << (2 + (clk_id - 1) * 8);
827 tegra_pmc_writel(val,
828 offsetof(struct pmc_ctlr,
829 pmc_clk_out_cntrl));
830
Simon Glasscd4b59b2015-06-05 14:39:36 -0600831 } else {
832 printf("%s: Unknown output clock id %d\n", __func__, clk_id);
833 return -EINVAL;
834 }
835
836 return 0;
837}
Simon Glass2b4029a2017-05-31 17:57:16 -0600838
839__weak bool clock_early_init_done(void)
840{
841 return true;
842}