2 * Copyright (c) 2010-2012, 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 /* Tegra30 Clock control functions */
21 #include <asm/arch/clock.h>
22 #include <asm/arch/tegra.h>
23 #include <asm/arch-tegra/clk_rst.h>
24 #include <asm/arch-tegra/timer.h>
29 * This is our record of the current clock rate of each clock. We don't
30 * fill all of these in since we are only really interested in clocks which
33 static unsigned pll_rate[CLOCK_ID_COUNT];
36 * The oscillator frequency is fixed to one of four set values. Based on this
37 * the other clocks are set up appropriately.
39 static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
47 * Clock types that we can use as a source. The Tegra3 has muxes for the
48 * peripheral clocks, and in most cases there are four options for the clock
49 * source. This gives us a clock 'type' and exploits what commonality exists
52 * Letters are obvious, except for T which means CLK_M, and S which means the
53 * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
54 * datasheet) and PLL_M are different things. The former is the basic
55 * clock supplied to the SOC from an external oscillator. The latter is the
58 * See definitions in clock_id in the header file.
61 CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
62 CLOCK_TYPE_MCPA, /* and so on */
73 CLOCK_TYPE_NONE = -1, /* invalid clock type */
76 /* return 1 if a peripheral ID is in range */
77 #define clock_type_id_isvalid(id) ((id) >= 0 && \
78 (id) < CLOCK_TYPE_COUNT)
80 char pllp_valid = 1; /* PLLP is set up correctly */
83 CLOCK_MAX_MUX = 8 /* number of source options for each clock */
87 MASK_BITS_31_30 = 2, /* num of bits used to specify clock source */
93 * Clock source mux for each clock type. This just converts our enum into
94 * a list of mux sources for use by the code.
97 * The extra column in each clock source array is used to store the mask
98 * bits in its register for the source.
100 #define CLK(x) CLOCK_ID_ ## x
101 static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX+1] = {
102 { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(OSC),
103 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
105 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO),
106 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
108 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
109 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
111 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE),
112 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
114 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC),
115 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
117 { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC),
118 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
120 { CLK(AUDIO), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
121 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
123 { CLK(AUDIO), CLK(SFROM32KHZ), CLK(PERIPH), CLK(OSC),
124 CLK(EPCI), CLK(NONE), CLK(NONE), CLK(NONE),
126 { CLK(PERIPH), CLK(MEMORY), CLK(DISPLAY), CLK(AUDIO),
127 CLK(CGENERAL), CLK(DISPLAY2), CLK(OSC), CLK(NONE),
129 { CLK(PERIPH), CLK(CGENERAL), CLK(SFROM32KHZ), CLK(OSC),
130 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
134 /* return 1 if a periphc_internal_id is in range */
135 #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
136 (id) < PERIPHC_COUNT)
139 * Clock type for each peripheral clock source. We put the name in each
140 * record just so it is easy to match things up
142 #define TYPE(name, type) type
143 static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
145 TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
146 TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
147 TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
148 TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PCM),
149 TYPE(PERIPHC_PWM, CLOCK_TYPE_PCST),
150 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
151 TYPE(PERIPHC_SBC2, CLOCK_TYPE_PCMT),
152 TYPE(PERIPHC_SBC3, CLOCK_TYPE_PCMT),
155 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
156 TYPE(PERIPHC_I2C1, CLOCK_TYPE_PCMT),
157 TYPE(PERIPHC_DVC_I2C, CLOCK_TYPE_PCMT),
158 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
159 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
160 TYPE(PERIPHC_SBC1, CLOCK_TYPE_PCMT),
161 TYPE(PERIPHC_DISP1, CLOCK_TYPE_PMDACD2T),
162 TYPE(PERIPHC_DISP2, CLOCK_TYPE_PMDACD2T),
165 TYPE(PERIPHC_CVE, CLOCK_TYPE_PDCT),
166 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
167 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
168 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
169 TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PCMT),
170 TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PCMT),
171 TYPE(PERIPHC_G3D, CLOCK_TYPE_MCPA),
172 TYPE(PERIPHC_G2D, CLOCK_TYPE_MCPA),
175 TYPE(PERIPHC_NDFLASH, CLOCK_TYPE_PCMT),
176 TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PCMT),
177 TYPE(PERIPHC_VFIR, CLOCK_TYPE_PCMT),
178 TYPE(PERIPHC_EPP, CLOCK_TYPE_MCPA),
179 TYPE(PERIPHC_MPE, CLOCK_TYPE_MCPA),
180 TYPE(PERIPHC_MIPI, CLOCK_TYPE_PCMT),
181 TYPE(PERIPHC_UART1, CLOCK_TYPE_PCMT),
182 TYPE(PERIPHC_UART2, CLOCK_TYPE_PCMT),
185 TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MCPA),
186 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
187 TYPE(PERIPHC_TVO, CLOCK_TYPE_PDCT),
188 TYPE(PERIPHC_HDMI, CLOCK_TYPE_PMDACD2T),
189 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
190 TYPE(PERIPHC_TVDAC, CLOCK_TYPE_PDCT),
191 TYPE(PERIPHC_I2C2, CLOCK_TYPE_PCMT),
192 TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPT),
195 TYPE(PERIPHC_UART3, CLOCK_TYPE_PCMT),
196 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
197 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
198 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
199 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
200 TYPE(PERIPHC_SBC4, CLOCK_TYPE_PCMT),
201 TYPE(PERIPHC_I2C3, CLOCK_TYPE_PCMT),
202 TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PCMT),
205 TYPE(PERIPHC_UART4, CLOCK_TYPE_PCMT),
206 TYPE(PERIPHC_UART5, CLOCK_TYPE_PCMT),
207 TYPE(PERIPHC_VDE, CLOCK_TYPE_PCMT),
208 TYPE(PERIPHC_OWR, CLOCK_TYPE_PCMT),
209 TYPE(PERIPHC_NOR, CLOCK_TYPE_PCMT),
210 TYPE(PERIPHC_CSITE, CLOCK_TYPE_PCMT),
211 TYPE(PERIPHC_I2S0, CLOCK_TYPE_AXPT),
212 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
215 TYPE(PERIPHC_G3D2, CLOCK_TYPE_MCPA),
216 TYPE(PERIPHC_MSELECT, CLOCK_TYPE_PCMT),
217 TYPE(PERIPHC_TSENSOR, CLOCK_TYPE_PCM),
218 TYPE(PERIPHC_I2S3, CLOCK_TYPE_AXPT),
219 TYPE(PERIPHC_I2S4, CLOCK_TYPE_AXPT),
220 TYPE(PERIPHC_I2C4, CLOCK_TYPE_PCMT),
221 TYPE(PERIPHC_SBC5, CLOCK_TYPE_PCMT),
222 TYPE(PERIPHC_SBC6, CLOCK_TYPE_PCMT),
225 TYPE(PERIPHC_AUDIO, CLOCK_TYPE_ACPT),
226 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
227 TYPE(PERIPHC_DAM0, CLOCK_TYPE_ACPT),
228 TYPE(PERIPHC_DAM1, CLOCK_TYPE_ACPT),
229 TYPE(PERIPHC_DAM2, CLOCK_TYPE_ACPT),
230 TYPE(PERIPHC_HDA2CODEC2X, CLOCK_TYPE_PCMT),
231 TYPE(PERIPHC_ACTMON, CLOCK_TYPE_PCM),
232 TYPE(PERIPHC_EXTPERIPH1, CLOCK_TYPE_ASPTE),
235 TYPE(PERIPHC_EXTPERIPH2, CLOCK_TYPE_ASPTE),
236 TYPE(PERIPHC_EXTPERIPH3, CLOCK_TYPE_ASPTE),
237 TYPE(PERIPHC_NANDSPEED, CLOCK_TYPE_PCMT),
238 TYPE(PERIPHC_I2CSLOW, CLOCK_TYPE_PCMT),
239 TYPE(PERIPHC_SYS, CLOCK_TYPE_NONE),
240 TYPE(PERIPHC_SPEEDO, CLOCK_TYPE_PCMT),
241 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
242 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
245 TYPE(PERIPHC_SATAOOB, CLOCK_TYPE_PCMT),
246 TYPE(PERIPHC_SATA, CLOCK_TYPE_PCMT),
247 TYPE(PERIPHC_HDA, CLOCK_TYPE_PCMT),
251 * This array translates a periph_id to a periphc_internal_id
253 * Not present/matched up:
254 * uint vi_sensor; _VI_SENSOR_0, 0x1A8
255 * SPDIF - which is both 0x08 and 0x0c
258 #define NONE(name) (-1)
259 #define OFFSET(name, value) PERIPHC_ ## name
260 static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
269 PERIPHC_UART2, /* and vfir 0x68 */
274 NONE(SPDIF), /* 0x08 and 0x0c, unclear which to use */
301 /* Middle word: 63:32 */
313 NONE(SBC1), /* SBC1, 0x34, is this SPI1? */
323 PERIPHC_TVO, /* also CVE 0x40 */
341 /* Upper word 95:64 */
424 NONE(RESERVED0_PCIERX0),
425 NONE(RESERVED1_PCIERX1),
426 NONE(RESERVED2_PCIERX2),
427 NONE(RESERVED3_PCIERX3),
428 NONE(RESERVED4_PCIERX4),
429 NONE(RESERVED5_PCIERX5),
433 NONE(RESERVED6_PCIE2),
435 NONE(RESERVED8_HDMI),
436 NONE(RESERVED9_SATA),
437 NONE(RESERVED10_MIPI),
443 * Get the oscillator frequency, from the corresponding hardware configuration
446 enum clock_osc_freq clock_get_osc_freq(void)
448 struct clk_rst_ctlr *clkrst =
449 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
452 reg = readl(&clkrst->crc_osc_ctrl);
453 return (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
456 int clock_get_osc_bypass(void)
458 struct clk_rst_ctlr *clkrst =
459 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
462 reg = readl(&clkrst->crc_osc_ctrl);
463 return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT;
466 /* Returns a pointer to the registers of the given pll */
467 static struct clk_pll *get_pll(enum clock_id clkid)
469 struct clk_rst_ctlr *clkrst =
470 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
472 assert(clock_id_is_pll(clkid));
473 return &clkrst->crc_pll[clkid];
476 int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
477 u32 *divp, u32 *cpcon, u32 *lfcon)
479 struct clk_pll *pll = get_pll(clkid);
482 assert(clkid != CLOCK_ID_USB);
484 /* Safety check, adds to code size but is small */
485 if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB)
487 data = readl(&pll->pll_base);
488 *divm = (data & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
489 *divn = (data & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT;
490 *divp = (data & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
491 data = readl(&pll->pll_misc);
492 *cpcon = (data & PLL_CPCON_MASK) >> PLL_CPCON_SHIFT;
493 *lfcon = (data & PLL_LFCON_MASK) >> PLL_LFCON_SHIFT;
497 unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
498 u32 divp, u32 cpcon, u32 lfcon)
500 struct clk_pll *pll = get_pll(clkid);
504 * We cheat by treating all PLL (except PLLU) in the same fashion.
505 * This works only because:
506 * - same fields are always mapped at same offsets, except DCCON
507 * - DCCON is always 0, doesn't conflict
508 * - M,N, P of PLLP values are ignored for PLLP
510 data = (cpcon << PLL_CPCON_SHIFT) | (lfcon << PLL_LFCON_SHIFT);
511 writel(data, &pll->pll_misc);
513 data = (divm << PLL_DIVM_SHIFT) | (divn << PLL_DIVN_SHIFT) |
514 (0 << PLL_BYPASS_SHIFT) | (1 << PLL_ENABLE_SHIFT);
516 if (clkid == CLOCK_ID_USB)
517 data |= divp << PLLU_VCO_FREQ_SHIFT;
519 data |= divp << PLL_DIVP_SHIFT;
520 writel(data, &pll->pll_base);
522 /* calculate the stable time */
523 return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
526 /* Returns a pointer to the clock source register for a peripheral */
527 static u32 *get_periph_source_reg(enum periph_id periph_id)
529 struct clk_rst_ctlr *clkrst =
530 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
531 enum periphc_internal_id internal_id;
533 /* Coresight is a special case */
534 if (periph_id == PERIPH_ID_CSI)
535 return &clkrst->crc_clk_src[PERIPH_ID_CSI+1];
537 assert(periph_id >= PERIPH_ID_FIRST && periph_id < PERIPH_ID_COUNT);
538 internal_id = periph_id_to_internal_id[periph_id];
539 assert(internal_id != -1);
540 if (internal_id >= PERIPHC_VW_FIRST) {
541 internal_id -= PERIPHC_VW_FIRST;
542 return &clkrst->crc_clk_src_vw[internal_id];
544 return &clkrst->crc_clk_src[internal_id];
547 void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
550 u32 *reg = get_periph_source_reg(periph_id);
555 value &= ~OUT_CLK_SOURCE_MASK;
556 value |= source << OUT_CLK_SOURCE_SHIFT;
558 value &= ~OUT_CLK_DIVISOR_MASK;
559 value |= divisor << OUT_CLK_DIVISOR_SHIFT;
564 void clock_ll_set_source(enum periph_id periph_id, unsigned source)
566 u32 *reg = get_periph_source_reg(periph_id);
568 clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
569 source << OUT_CLK_SOURCE_SHIFT);
573 * Given the parent's rate and the required rate for the children, this works
574 * out the peripheral clock divider to use, in 7.1 binary format.
576 * @param divider_bits number of divider bits (8 or 16)
577 * @param parent_rate clock rate of parent clock in Hz
578 * @param rate required clock rate for this clock
579 * @return divider which should be used
581 static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate,
584 u64 divider = parent_rate * 2;
585 unsigned max_divider = 1 << divider_bits;
588 do_div(divider, rate);
590 if ((s64)divider - 2 < 0)
593 if ((s64)divider - 2 >= max_divider)
600 * Given the parent's rate and the divider in 7.1 format, this works out the
601 * resulting peripheral clock rate.
603 * @param parent_rate clock rate of parent clock in Hz
604 * @param divider which should be used in 7.1 format
605 * @return effective clock rate of peripheral
607 static unsigned long get_rate_from_divider(unsigned long parent_rate,
612 rate = (u64)parent_rate * 2;
613 do_div(rate, divider + 2);
617 unsigned long clock_get_periph_rate(enum periph_id periph_id,
618 enum clock_id parent)
620 u32 *reg = get_periph_source_reg(periph_id);
622 return get_rate_from_divider(pll_rate[parent],
623 (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT);
627 * Find the best available 7.1 format divisor given a parent clock rate and
628 * required child clock rate. This function assumes that a second-stage
629 * divisor is available which can divide by powers of 2 from 1 to 256.
631 * @param divider_bits number of divider bits (8 or 16)
632 * @param parent_rate clock rate of parent clock in Hz
633 * @param rate required clock rate for this clock
634 * @param extra_div value for the second-stage divisor (not set if this
635 * function returns -1.
636 * @return divider which should be used, or -1 if nothing is valid
639 static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
640 unsigned long rate, int *extra_div)
643 int best_divider = -1;
644 int best_error = rate;
646 /* try dividers from 1 to 256 and find closest match */
647 for (shift = 0; shift <= 8 && best_error > 0; shift++) {
648 unsigned divided_parent = parent_rate >> shift;
649 int divider = clk_get_divider(divider_bits, divided_parent,
651 unsigned effective_rate = get_rate_from_divider(divided_parent,
653 int error = rate - effective_rate;
655 /* Given a valid divider, look for the lowest error */
656 if (divider != -1 && error < best_error) {
658 *extra_div = 1 << shift;
659 best_divider = divider;
663 /* return what we found - *extra_div will already be set */
668 * Given a peripheral ID and the required source clock, this returns which
669 * value should be programmed into the source mux for that peripheral.
671 * There is special code here to handle the one source type with 5 sources.
673 * @param periph_id peripheral to start
674 * @param source PLL id of required parent clock
675 * @param mux_bits Set to number of bits in mux register: 2 or 4
676 * @param divider_bits Set to number of divider bits (8 or 16)
677 * @return mux value (0-4, or -1 if not found)
679 static int get_periph_clock_source(enum periph_id periph_id,
680 enum clock_id parent, int *mux_bits, int *divider_bits)
682 enum clock_type_id type;
683 enum periphc_internal_id internal_id;
686 assert(clock_periph_id_isvalid(periph_id));
688 internal_id = periph_id_to_internal_id[periph_id];
689 assert(periphc_internal_id_isvalid(internal_id));
691 type = clock_periph_type[internal_id];
692 assert(clock_type_id_isvalid(type));
694 *mux_bits = clock_source[type][CLOCK_MAX_MUX];
696 for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
697 if (clock_source[type][mux] == parent)
700 /* if we get here, either us or the caller has made a mistake */
701 printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
707 * Adjust peripheral PLL to use the given divider and source.
709 * @param periph_id peripheral to adjust
710 * @param source Source number (0-3 or 0-7)
711 * @param mux_bits Number of mux bits (2 or 4)
712 * @param divider Required divider in 7.1 or 15.1 format
713 * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
714 * for this peripheral)
716 static int adjust_periph_pll(enum periph_id periph_id, int source,
717 int mux_bits, unsigned divider)
719 u32 *reg = get_periph_source_reg(periph_id);
721 clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
722 divider << OUT_CLK_DIVISOR_SHIFT);
725 /* work out the source clock and set it */
729 clrsetbits_le32(reg, OUT_CLK_SOURCE4_MASK,
730 source << OUT_CLK_SOURCE4_SHIFT);
732 clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
733 source << OUT_CLK_SOURCE_SHIFT);
739 unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
740 enum clock_id parent, unsigned rate, int *extra_div)
742 unsigned effective_rate;
743 int mux_bits, source;
744 int divider, divider_bits = 0;
746 /* work out the source clock and set it */
747 source = get_periph_clock_source(periph_id, parent, &mux_bits,
751 divider = find_best_divider(divider_bits, pll_rate[parent],
754 divider = clk_get_divider(divider_bits, pll_rate[parent],
756 assert(divider >= 0);
757 if (adjust_periph_pll(periph_id, source, mux_bits, divider))
759 debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
760 get_periph_source_reg(periph_id),
761 readl(get_periph_source_reg(periph_id)));
763 /* Check what we ended up with. This shouldn't matter though */
764 effective_rate = clock_get_periph_rate(periph_id, parent);
766 effective_rate /= *extra_div;
767 if (rate != effective_rate)
768 debug("Requested clock rate %u not honored (got %u)\n",
769 rate, effective_rate);
770 return effective_rate;
773 unsigned clock_start_periph_pll(enum periph_id periph_id,
774 enum clock_id parent, unsigned rate)
776 unsigned effective_rate;
778 reset_set_enable(periph_id, 1);
779 clock_enable(periph_id);
781 effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
784 reset_set_enable(periph_id, 0);
785 return effective_rate;
788 void clock_set_enable(enum periph_id periph_id, int enable)
790 struct clk_rst_ctlr *clkrst =
791 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
795 /* Enable/disable the clock to this peripheral */
796 assert(clock_periph_id_isvalid(periph_id));
797 if ((int)periph_id < (int)PERIPH_ID_VW_FIRST)
798 clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
800 clk = &clkrst->crc_clk_out_enb_vw[PERIPH_REG(periph_id)];
803 reg |= PERIPH_MASK(periph_id);
805 reg &= ~PERIPH_MASK(periph_id);
809 void clock_enable(enum periph_id clkid)
811 clock_set_enable(clkid, 1);
814 void clock_disable(enum periph_id clkid)
816 clock_set_enable(clkid, 0);
819 void reset_set_enable(enum periph_id periph_id, int enable)
821 struct clk_rst_ctlr *clkrst =
822 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
826 /* Enable/disable reset to the peripheral */
827 assert(clock_periph_id_isvalid(periph_id));
828 if (periph_id < PERIPH_ID_VW_FIRST)
829 reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
831 reset = &clkrst->crc_rst_dev_vw[PERIPH_REG(periph_id)];
834 reg |= PERIPH_MASK(periph_id);
836 reg &= ~PERIPH_MASK(periph_id);
840 void reset_periph(enum periph_id periph_id, int us_delay)
842 /* Put peripheral into reset */
843 reset_set_enable(periph_id, 1);
847 reset_set_enable(periph_id, 0);
852 void reset_cmplx_set_enable(int cpu, int which, int reset)
854 struct clk_rst_ctlr *clkrst =
855 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
858 /* Form the mask, which depends on the cpu chosen. Tegra3 has 4 */
859 assert(cpu >= 0 && cpu < 4);
862 /* either enable or disable those reset for that CPU */
864 writel(mask, &clkrst->crc_cpu_cmplx_set);
866 writel(mask, &clkrst->crc_cpu_cmplx_clr);
869 unsigned clock_get_rate(enum clock_id clkid)
877 parent_rate = osc_freq[clock_get_osc_freq()];
878 if (clkid == CLOCK_ID_OSC)
881 pll = get_pll(clkid);
882 base = readl(&pll->pll_base);
884 /* Oh for bf_unpack()... */
885 rate = parent_rate * ((base & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT);
886 divm = (base & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
887 if (clkid == CLOCK_ID_USB)
888 divm <<= (base & PLLU_VCO_FREQ_MASK) >> PLLU_VCO_FREQ_SHIFT;
890 divm <<= (base & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
896 * Set the output frequency you want for each PLL clock.
897 * PLL output frequencies are programmed by setting their N, M and P values.
898 * The governing equations are:
899 * VCO = (Fi / m) * n, Fo = VCO / (2^p)
900 * where Fo is the output frequency from the PLL.
901 * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
902 * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
903 * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
905 * @param n PLL feedback divider(DIVN)
906 * @param m PLL input divider(DIVN)
907 * @param p post divider(DIVP)
908 * @param cpcon base PLL charge pump(CPCON)
909 * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
910 * be overriden), 1 if PLL is already correct
912 static int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
918 pll = get_pll(clkid);
920 base_reg = readl(&pll->pll_base);
922 /* Set BYPASS, m, n and p to PLL_BASE */
923 base_reg &= ~PLL_DIVM_MASK;
924 base_reg |= m << PLL_DIVM_SHIFT;
926 base_reg &= ~PLL_DIVN_MASK;
927 base_reg |= n << PLL_DIVN_SHIFT;
929 base_reg &= ~PLL_DIVP_MASK;
930 base_reg |= p << PLL_DIVP_SHIFT;
932 if (clkid == CLOCK_ID_PERIPH) {
934 * If the PLL is already set up, check that it is correct
935 * and record this info for clock_verify() to check.
937 if (base_reg & PLL_BASE_OVRRIDE_MASK) {
938 base_reg |= PLL_ENABLE_MASK;
939 if (base_reg != readl(&pll->pll_base))
941 return pllp_valid ? 1 : -1;
943 base_reg |= PLL_BASE_OVRRIDE_MASK;
946 base_reg |= PLL_BYPASS_MASK;
947 writel(base_reg, &pll->pll_base);
949 /* Set cpcon to PLL_MISC */
950 misc_reg = readl(&pll->pll_misc);
951 misc_reg &= ~PLL_CPCON_MASK;
952 misc_reg |= cpcon << PLL_CPCON_SHIFT;
953 writel(misc_reg, &pll->pll_misc);
956 base_reg |= PLL_ENABLE_MASK;
957 writel(base_reg, &pll->pll_base);
960 base_reg &= ~PLL_BYPASS_MASK;
961 writel(base_reg, &pll->pll_base);
966 void clock_ll_start_uart(enum periph_id periph_id)
968 /* Assert UART reset and enable clock */
969 reset_set_enable(periph_id, 1);
970 clock_enable(periph_id);
971 clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
976 /* De-assert reset to UART */
977 reset_set_enable(periph_id, 0);
980 #ifdef CONFIG_OF_CONTROL
982 * Convert a device tree clock ID to our peripheral ID. They are mostly
983 * the same but we are very cautious so we check that a valid clock ID is
986 * @param clk_id Clock ID according to tegra20 device tree binding
987 * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
989 static enum periph_id clk_id_to_periph_id(int clk_id)
992 return PERIPH_ID_NONE;
1015 return PERIPH_ID_NONE;
1021 int clock_decode_periph_id(const void *blob, int node)
1027 err = fdtdec_get_int_array(blob, node, "clocks", cell,
1031 id = clk_id_to_periph_id(cell[1]);
1032 assert(clock_periph_id_isvalid(id));
1035 #endif /* CONFIG_OF_CONTROL */
1037 int clock_verify(void)
1039 struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
1040 u32 reg = readl(&pll->pll_base);
1043 printf("Warning: PLLP %x is not correct\n", reg);
1046 debug("PLLP %x is correct\n", reg);
1050 void clock_early_init(void)
1053 * PLLP output frequency set to 408Mhz
1054 * PLLC output frequency set to 228Mhz
1056 switch (clock_get_osc_freq()) {
1057 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
1058 clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8);
1059 clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8);
1062 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
1063 clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8);
1064 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
1067 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
1068 clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8);
1069 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
1071 case CLOCK_OSC_FREQ_19_2:
1074 * These are not supported. It is too early to print a
1075 * message and the UART likely won't work anyway due to the
1076 * oscillator being wrong.
1082 void clock_init(void)
1084 pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
1085 pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
1086 pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
1087 pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
1088 pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
1089 debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
1090 debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
1091 debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);