2 * Copyright (c) 2010-2014, NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 /* Tegra SoC common clock control functions */
21 #include <asm/arch/clock.h>
22 #include <asm/arch/tegra.h>
23 #include <asm/arch-tegra/ap.h>
24 #include <asm/arch-tegra/clk_rst.h>
25 #include <asm/arch-tegra/timer.h>
30 * This is our record of the current clock rate of each clock. We don't
31 * fill all of these in since we are only really interested in clocks which
34 static unsigned pll_rate[CLOCK_ID_COUNT];
37 * The oscillator frequency is fixed to one of four set values. Based on this
38 * the other clocks are set up appropriately.
40 static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
47 /* return 1 if a peripheral ID is in range */
48 #define clock_type_id_isvalid(id) ((id) >= 0 && \
49 (id) < CLOCK_TYPE_COUNT)
51 char pllp_valid = 1; /* PLLP is set up correctly */
53 /* return 1 if a periphc_internal_id is in range */
54 #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
57 /* number of clock outputs of a PLL */
58 static const u8 pll_num_clkouts[] = {
67 int clock_get_osc_bypass(void)
69 struct clk_rst_ctlr *clkrst =
70 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
73 reg = readl(&clkrst->crc_osc_ctrl);
74 return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT;
77 /* Returns a pointer to the registers of the given pll */
78 static struct clk_pll *get_pll(enum clock_id clkid)
80 struct clk_rst_ctlr *clkrst =
81 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
83 assert(clock_id_is_pll(clkid));
84 return &clkrst->crc_pll[clkid];
87 int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
88 u32 *divp, u32 *cpcon, u32 *lfcon)
90 struct clk_pll *pll = get_pll(clkid);
93 assert(clkid != CLOCK_ID_USB);
95 /* Safety check, adds to code size but is small */
96 if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB)
98 data = readl(&pll->pll_base);
99 *divm = (data & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
100 *divn = (data & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT;
101 *divp = (data & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
102 data = readl(&pll->pll_misc);
103 *cpcon = (data & PLL_CPCON_MASK) >> PLL_CPCON_SHIFT;
104 *lfcon = (data & PLL_LFCON_MASK) >> PLL_LFCON_SHIFT;
109 unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
110 u32 divp, u32 cpcon, u32 lfcon)
112 struct clk_pll *pll = get_pll(clkid);
116 * We cheat by treating all PLL (except PLLU) in the same fashion.
117 * This works only because:
118 * - same fields are always mapped at same offsets, except DCCON
119 * - DCCON is always 0, doesn't conflict
120 * - M,N, P of PLLP values are ignored for PLLP
122 data = (cpcon << PLL_CPCON_SHIFT) | (lfcon << PLL_LFCON_SHIFT);
123 writel(data, &pll->pll_misc);
125 data = (divm << PLL_DIVM_SHIFT) | (divn << PLL_DIVN_SHIFT) |
126 (0 << PLL_BYPASS_SHIFT) | (1 << PLL_ENABLE_SHIFT);
128 if (clkid == CLOCK_ID_USB)
129 data |= divp << PLLU_VCO_FREQ_SHIFT;
131 data |= divp << PLL_DIVP_SHIFT;
132 writel(data, &pll->pll_base);
134 /* calculate the stable time */
135 return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
138 void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
141 u32 *reg = get_periph_source_reg(periph_id);
146 value &= ~OUT_CLK_SOURCE_31_30_MASK;
147 value |= source << OUT_CLK_SOURCE_31_30_SHIFT;
149 value &= ~OUT_CLK_DIVISOR_MASK;
150 value |= divisor << OUT_CLK_DIVISOR_SHIFT;
155 void clock_ll_set_source(enum periph_id periph_id, unsigned source)
157 u32 *reg = get_periph_source_reg(periph_id);
159 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_30_MASK,
160 source << OUT_CLK_SOURCE_31_30_SHIFT);
164 * Given the parent's rate and the required rate for the children, this works
165 * out the peripheral clock divider to use, in 7.1 binary format.
167 * @param divider_bits number of divider bits (8 or 16)
168 * @param parent_rate clock rate of parent clock in Hz
169 * @param rate required clock rate for this clock
170 * @return divider which should be used
172 static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate,
175 u64 divider = parent_rate * 2;
176 unsigned max_divider = 1 << divider_bits;
179 do_div(divider, rate);
181 if ((s64)divider - 2 < 0)
184 if ((s64)divider - 2 >= max_divider)
190 int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate)
192 struct clk_pll *pll = get_pll(clkid);
193 int data = 0, div = 0, offset = 0;
195 if (!clock_id_is_pll(clkid))
198 if (pllout + 1 > pll_num_clkouts[clkid])
201 div = clk_get_divider(8, pll_rate[clkid], rate);
206 /* out2 and out4 are in the high part of the register */
207 if (pllout == PLL_OUT2 || pllout == PLL_OUT4)
210 data = (div << PLL_OUT_RATIO_SHIFT) |
211 PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN;
212 clrsetbits_le32(&pll->pll_out[pllout >> 1],
213 PLL_OUT_RATIO_MASK << offset, data << offset);
219 * Given the parent's rate and the divider in 7.1 format, this works out the
220 * resulting peripheral clock rate.
222 * @param parent_rate clock rate of parent clock in Hz
223 * @param divider which should be used in 7.1 format
224 * @return effective clock rate of peripheral
226 static unsigned long get_rate_from_divider(unsigned long parent_rate,
231 rate = (u64)parent_rate * 2;
232 do_div(rate, divider + 2);
236 unsigned long clock_get_periph_rate(enum periph_id periph_id,
237 enum clock_id parent)
239 u32 *reg = get_periph_source_reg(periph_id);
241 return get_rate_from_divider(pll_rate[parent],
242 (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT);
246 * Find the best available 7.1 format divisor given a parent clock rate and
247 * required child clock rate. This function assumes that a second-stage
248 * divisor is available which can divide by powers of 2 from 1 to 256.
250 * @param divider_bits number of divider bits (8 or 16)
251 * @param parent_rate clock rate of parent clock in Hz
252 * @param rate required clock rate for this clock
253 * @param extra_div value for the second-stage divisor (not set if this
254 * function returns -1.
255 * @return divider which should be used, or -1 if nothing is valid
258 static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
259 unsigned long rate, int *extra_div)
262 int best_divider = -1;
263 int best_error = rate;
265 /* try dividers from 1 to 256 and find closest match */
266 for (shift = 0; shift <= 8 && best_error > 0; shift++) {
267 unsigned divided_parent = parent_rate >> shift;
268 int divider = clk_get_divider(divider_bits, divided_parent,
270 unsigned effective_rate = get_rate_from_divider(divided_parent,
272 int error = rate - effective_rate;
274 /* Given a valid divider, look for the lowest error */
275 if (divider != -1 && error < best_error) {
277 *extra_div = 1 << shift;
278 best_divider = divider;
282 /* return what we found - *extra_div will already be set */
287 * Adjust peripheral PLL to use the given divider and source.
289 * @param periph_id peripheral to adjust
290 * @param source Source number (0-3 or 0-7)
291 * @param mux_bits Number of mux bits (2 or 4)
292 * @param divider Required divider in 7.1 or 15.1 format
293 * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
294 * for this peripheral)
296 static int adjust_periph_pll(enum periph_id periph_id, int source,
297 int mux_bits, unsigned divider)
299 u32 *reg = get_periph_source_reg(periph_id);
301 clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
302 divider << OUT_CLK_DIVISOR_SHIFT);
305 /* work out the source clock and set it */
310 case MASK_BITS_31_30:
311 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_30_MASK,
312 source << OUT_CLK_SOURCE_31_30_SHIFT);
315 case MASK_BITS_31_29:
316 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_29_MASK,
317 source << OUT_CLK_SOURCE_31_29_SHIFT);
320 case MASK_BITS_31_28:
321 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_28_MASK,
322 source << OUT_CLK_SOURCE_31_28_SHIFT);
333 unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
334 enum clock_id parent, unsigned rate, int *extra_div)
336 unsigned effective_rate;
337 int mux_bits, divider_bits, source;
341 /* work out the source clock and set it */
342 source = get_periph_clock_source(periph_id, parent, &mux_bits,
345 divider = find_best_divider(divider_bits, pll_rate[parent],
350 assert(divider >= 0);
351 if (adjust_periph_pll(periph_id, source, mux_bits, divider))
353 debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
354 get_periph_source_reg(periph_id),
355 readl(get_periph_source_reg(periph_id)));
357 /* Check what we ended up with. This shouldn't matter though */
358 effective_rate = clock_get_periph_rate(periph_id, parent);
360 effective_rate /= *extra_div;
361 if (rate != effective_rate)
362 debug("Requested clock rate %u not honored (got %u)\n",
363 rate, effective_rate);
364 return effective_rate;
367 unsigned clock_start_periph_pll(enum periph_id periph_id,
368 enum clock_id parent, unsigned rate)
370 unsigned effective_rate;
372 reset_set_enable(periph_id, 1);
373 clock_enable(periph_id);
375 effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
378 reset_set_enable(periph_id, 0);
379 return effective_rate;
382 void clock_enable(enum periph_id clkid)
384 clock_set_enable(clkid, 1);
387 void clock_disable(enum periph_id clkid)
389 clock_set_enable(clkid, 0);
392 void reset_periph(enum periph_id periph_id, int us_delay)
394 /* Put peripheral into reset */
395 reset_set_enable(periph_id, 1);
399 reset_set_enable(periph_id, 0);
404 void reset_cmplx_set_enable(int cpu, int which, int reset)
406 struct clk_rst_ctlr *clkrst =
407 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
410 /* Form the mask, which depends on the cpu chosen (2 or 4) */
411 assert(cpu >= 0 && cpu < MAX_NUM_CPU);
414 /* either enable or disable those reset for that CPU */
416 writel(mask, &clkrst->crc_cpu_cmplx_set);
418 writel(mask, &clkrst->crc_cpu_cmplx_clr);
421 unsigned clock_get_rate(enum clock_id clkid)
429 parent_rate = osc_freq[clock_get_osc_freq()];
430 if (clkid == CLOCK_ID_OSC)
433 pll = get_pll(clkid);
434 base = readl(&pll->pll_base);
436 /* Oh for bf_unpack()... */
437 rate = parent_rate * ((base & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT);
438 divm = (base & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
439 if (clkid == CLOCK_ID_USB)
440 divm <<= (base & PLLU_VCO_FREQ_MASK) >> PLLU_VCO_FREQ_SHIFT;
442 divm <<= (base & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
448 * Set the output frequency you want for each PLL clock.
449 * PLL output frequencies are programmed by setting their N, M and P values.
450 * The governing equations are:
451 * VCO = (Fi / m) * n, Fo = VCO / (2^p)
452 * where Fo is the output frequency from the PLL.
453 * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
454 * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
455 * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
457 * @param n PLL feedback divider(DIVN)
458 * @param m PLL input divider(DIVN)
459 * @param p post divider(DIVP)
460 * @param cpcon base PLL charge pump(CPCON)
461 * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
462 * be overriden), 1 if PLL is already correct
464 int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
470 pll = get_pll(clkid);
472 base_reg = readl(&pll->pll_base);
474 /* Set BYPASS, m, n and p to PLL_BASE */
475 base_reg &= ~PLL_DIVM_MASK;
476 base_reg |= m << PLL_DIVM_SHIFT;
478 base_reg &= ~PLL_DIVN_MASK;
479 base_reg |= n << PLL_DIVN_SHIFT;
481 base_reg &= ~PLL_DIVP_MASK;
482 base_reg |= p << PLL_DIVP_SHIFT;
484 if (clkid == CLOCK_ID_PERIPH) {
486 * If the PLL is already set up, check that it is correct
487 * and record this info for clock_verify() to check.
489 if (base_reg & PLL_BASE_OVRRIDE_MASK) {
490 base_reg |= PLL_ENABLE_MASK;
491 if (base_reg != readl(&pll->pll_base))
493 return pllp_valid ? 1 : -1;
495 base_reg |= PLL_BASE_OVRRIDE_MASK;
498 base_reg |= PLL_BYPASS_MASK;
499 writel(base_reg, &pll->pll_base);
501 /* Set cpcon to PLL_MISC */
502 misc_reg = readl(&pll->pll_misc);
503 misc_reg &= ~PLL_CPCON_MASK;
504 misc_reg |= cpcon << PLL_CPCON_SHIFT;
505 writel(misc_reg, &pll->pll_misc);
508 base_reg |= PLL_ENABLE_MASK;
509 writel(base_reg, &pll->pll_base);
512 base_reg &= ~PLL_BYPASS_MASK;
513 writel(base_reg, &pll->pll_base);
518 void clock_ll_start_uart(enum periph_id periph_id)
520 /* Assert UART reset and enable clock */
521 reset_set_enable(periph_id, 1);
522 clock_enable(periph_id);
523 clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
528 /* De-assert reset to UART */
529 reset_set_enable(periph_id, 0);
532 #ifdef CONFIG_OF_CONTROL
533 int clock_decode_periph_id(const void *blob, int node)
539 err = fdtdec_get_int_array(blob, node, "clocks", cell,
543 id = clk_id_to_periph_id(cell[1]);
544 assert(clock_periph_id_isvalid(id));
547 #endif /* CONFIG_OF_CONTROL */
549 int clock_verify(void)
551 struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
552 u32 reg = readl(&pll->pll_base);
555 printf("Warning: PLLP %x is not correct\n", reg);
558 debug("PLLP %x is correct\n", reg);
562 void clock_init(void)
564 pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
565 pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
566 pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
567 pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
568 pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
569 pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU);
570 debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
571 debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
572 debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
573 debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]);
574 debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]);
576 /* Do any special system timer/TSC setup */
577 #if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
578 if (!tegra_cpu_is_non_secure())
583 static void set_avp_clock_source(u32 src)
585 struct clk_rst_ctlr *clkrst =
586 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
589 val = (src << SCLK_SWAKEUP_FIQ_SOURCE_SHIFT) |
590 (src << SCLK_SWAKEUP_IRQ_SOURCE_SHIFT) |
591 (src << SCLK_SWAKEUP_RUN_SOURCE_SHIFT) |
592 (src << SCLK_SWAKEUP_IDLE_SOURCE_SHIFT) |
593 (SCLK_SYS_STATE_RUN << SCLK_SYS_STATE_SHIFT);
594 writel(val, &clkrst->crc_sclk_brst_pol);
599 * This function is useful on Tegra30, and any later SoCs that have compatible
600 * PLLP configuration registers.
602 void tegra30_set_up_pllp(void)
604 struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
608 * Based on the Tegra TRM, the system clock (which is the AVP clock) can
609 * run up to 275MHz. On power on, the default sytem clock source is set
610 * to PLLP_OUT0. This function sets PLLP's (hence PLLP_OUT0's) rate to
611 * 408MHz which is beyond system clock's upper limit.
613 * The fix is to set the system clock to CLK_M before initializing PLLP,
614 * and then switch back to PLLP_OUT4, which has an appropriate divider
615 * configured, after PLLP has been configured
617 set_avp_clock_source(SCLK_SOURCE_CLKM);
620 * PLLP output frequency set to 408Mhz
621 * PLLC output frequency set to 228Mhz
623 switch (clock_get_osc_freq()) {
624 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
625 clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8);
626 clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8);
629 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
630 clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8);
631 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
634 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
635 clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8);
636 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
638 case CLOCK_OSC_FREQ_19_2:
641 * These are not supported. It is too early to print a
642 * message and the UART likely won't work anyway due to the
643 * oscillator being wrong.
648 /* Set PLLP_OUT1, 2, 3 & 4 freqs to 9.6, 48, 102 & 204MHz */
651 /* Assert RSTN before enable */
652 reg = PLLP_OUT2_RSTN_EN | PLLP_OUT1_RSTN_EN;
653 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
654 /* Set divisor and reenable */
655 reg = (IN_408_OUT_48_DIVISOR << PLLP_OUT2_RATIO)
656 | PLLP_OUT2_OVR | PLLP_OUT2_CLKEN | PLLP_OUT2_RSTN_DIS
657 | (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO)
658 | PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS;
659 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
662 /* Assert RSTN before enable */
663 reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN;
664 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
665 /* Set divisor and reenable */
666 reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO)
667 | PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS
668 | (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO)
669 | PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS;
670 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
672 set_avp_clock_source(SCLK_SOURCE_PLLP_OUT4);