1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2013-2015
4 * NVIDIA Corporation <www.nvidia.com>
7 /* Tegra124 Clock control functions */
11 #include <asm/arch/clock.h>
12 #include <asm/arch/sysctr.h>
13 #include <asm/arch/tegra.h>
14 #include <asm/arch-tegra/clk_rst.h>
15 #include <asm/arch-tegra/timer.h>
20 * Clock types that we can use as a source. The Tegra124 has muxes for the
21 * peripheral clocks, and in most cases there are four options for the clock
22 * source. This gives us a clock 'type' and exploits what commonality exists
25 * Letters are obvious, except for T which means CLK_M, and S which means the
26 * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
27 * datasheet) and PLL_M are different things. The former is the basic
28 * clock supplied to the SOC from an external oscillator. The latter is the
31 * See definitions in clock_id in the header file.
34 CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
35 CLOCK_TYPE_MCPA, /* and so on */
49 CLOCK_TYPE_PC2CC3M_T16, /* PC2CC3M_T, but w/16-bit divisor (I2C) */
52 CLOCK_TYPE_MCPTM2C2C3,
54 CLOCK_TYPE_AC2CC3P_TS2,
57 CLOCK_TYPE_NONE = -1, /* invalid clock type */
61 CLOCK_MAX_MUX = 8 /* number of source options for each clock */
65 * Clock source mux for each clock type. This just converts our enum into
66 * a list of mux sources for use by the code.
69 * The extra column in each clock source array is used to store the mask
70 * bits in its register for the source.
72 #define CLK(x) CLOCK_ID_ ## x
73 static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX+1] = {
74 { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(CLK_M),
75 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
77 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO),
78 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
80 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
81 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
83 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE),
84 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
86 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC),
87 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
89 { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC),
90 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
92 { CLK(AUDIO), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
93 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
95 { CLK(AUDIO), CLK(SFROM32KHZ), CLK(PERIPH), CLK(OSC),
96 CLK(EPCI), CLK(NONE), CLK(NONE), CLK(NONE),
98 { CLK(PERIPH), CLK(MEMORY), CLK(DISPLAY), CLK(AUDIO),
99 CLK(CGENERAL), CLK(DISPLAY2), CLK(OSC), CLK(NONE),
101 { CLK(PERIPH), CLK(CGENERAL), CLK(SFROM32KHZ), CLK(OSC),
102 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
105 { CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
106 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
109 /* Additional clock types on Tegra114+ */
110 /* CLOCK_TYPE_PC2CC3M */
111 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
112 CLK(MEMORY), CLK(NONE), CLK(NONE), CLK(NONE),
114 /* CLOCK_TYPE_PC2CC3S_T */
115 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
116 CLK(SFROM32KHZ), CLK(NONE), CLK(OSC), CLK(NONE),
118 /* CLOCK_TYPE_PC2CC3M_T */
119 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
120 CLK(MEMORY), CLK(NONE), CLK(OSC), CLK(NONE),
122 /* CLOCK_TYPE_PC2CC3M_T, w/16-bit divisor (I2C) */
123 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
124 CLK(MEMORY), CLK(NONE), CLK(OSC), CLK(NONE),
126 /* CLOCK_TYPE_MC2CC3P_A */
127 { CLK(MEMORY), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
128 CLK(PERIPH), CLK(NONE), CLK(AUDIO), CLK(NONE),
131 { CLK(MEMORY), CLK(NONE), CLK(NONE), CLK(NONE),
132 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
134 /* CLOCK_TYPE_MCPTM2C2C3 */
135 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
136 CLK(MEMORY2), CLK(CGENERAL2), CLK(CGENERAL3), CLK(NONE),
138 /* CLOCK_TYPE_PC2CC3T_S */
139 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
140 CLK(OSC), CLK(NONE), CLK(SFROM32KHZ), CLK(NONE),
142 /* CLOCK_TYPE_AC2CC3P_TS2 */
143 { CLK(AUDIO), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
144 CLK(PERIPH), CLK(NONE), CLK(OSC), CLK(SRC2),
149 * Clock type for each peripheral clock source. We put the name in each
150 * record just so it is easy to match things up
152 #define TYPE(name, type) type
153 static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
155 TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
156 TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
157 TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
158 TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PC2CC3M),
159 TYPE(PERIPHC_PWM, CLOCK_TYPE_PC2CC3S_T),
160 TYPE(PERIPHC_05h, CLOCK_TYPE_NONE),
161 TYPE(PERIPHC_SBC2, CLOCK_TYPE_PC2CC3M_T),
162 TYPE(PERIPHC_SBC3, CLOCK_TYPE_PC2CC3M_T),
165 TYPE(PERIPHC_08h, CLOCK_TYPE_NONE),
166 TYPE(PERIPHC_I2C1, CLOCK_TYPE_PC2CC3M_T16),
167 TYPE(PERIPHC_I2C5, CLOCK_TYPE_PC2CC3M_T16),
168 TYPE(PERIPHC_0bh, CLOCK_TYPE_NONE),
169 TYPE(PERIPHC_0ch, CLOCK_TYPE_NONE),
170 TYPE(PERIPHC_SBC1, CLOCK_TYPE_PC2CC3M_T),
171 TYPE(PERIPHC_DISP1, CLOCK_TYPE_PMDACD2T),
172 TYPE(PERIPHC_DISP2, CLOCK_TYPE_PMDACD2T),
175 TYPE(PERIPHC_10h, CLOCK_TYPE_NONE),
176 TYPE(PERIPHC_11h, CLOCK_TYPE_NONE),
177 TYPE(PERIPHC_VI, CLOCK_TYPE_MC2CC3P_A),
178 TYPE(PERIPHC_13h, CLOCK_TYPE_NONE),
179 TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PC2CC3M_T),
180 TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PC2CC3M_T),
181 TYPE(PERIPHC_16h, CLOCK_TYPE_NONE),
182 TYPE(PERIPHC_17h, CLOCK_TYPE_NONE),
185 TYPE(PERIPHC_18h, CLOCK_TYPE_NONE),
186 TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PC2CC3M_T),
187 TYPE(PERIPHC_VFIR, CLOCK_TYPE_PC2CC3M_T),
188 TYPE(PERIPHC_1Bh, CLOCK_TYPE_NONE),
189 TYPE(PERIPHC_1Ch, CLOCK_TYPE_NONE),
190 TYPE(PERIPHC_HSI, CLOCK_TYPE_PC2CC3M_T),
191 TYPE(PERIPHC_UART1, CLOCK_TYPE_PC2CC3M_T),
192 TYPE(PERIPHC_UART2, CLOCK_TYPE_PC2CC3M_T),
195 TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MC2CC3P_A),
196 TYPE(PERIPHC_21h, CLOCK_TYPE_NONE),
197 TYPE(PERIPHC_22h, CLOCK_TYPE_NONE),
198 TYPE(PERIPHC_HDMI, CLOCK_TYPE_PMDACD2T),
199 TYPE(PERIPHC_24h, CLOCK_TYPE_NONE),
200 TYPE(PERIPHC_25h, CLOCK_TYPE_NONE),
201 TYPE(PERIPHC_I2C2, CLOCK_TYPE_PC2CC3M_T16),
202 TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPTM2C2C3),
205 TYPE(PERIPHC_UART3, CLOCK_TYPE_PC2CC3M_T),
206 TYPE(PERIPHC_29h, CLOCK_TYPE_NONE),
207 TYPE(PERIPHC_VI_SENSOR, CLOCK_TYPE_MC2CC3P_A),
208 TYPE(PERIPHC_2bh, CLOCK_TYPE_NONE),
209 TYPE(PERIPHC_2ch, CLOCK_TYPE_NONE),
210 TYPE(PERIPHC_SBC4, CLOCK_TYPE_PC2CC3M_T),
211 TYPE(PERIPHC_I2C3, CLOCK_TYPE_PC2CC3M_T16),
212 TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PC2CC3M_T),
215 TYPE(PERIPHC_UART4, CLOCK_TYPE_PC2CC3M_T),
216 TYPE(PERIPHC_UART5, CLOCK_TYPE_PC2CC3M_T),
217 TYPE(PERIPHC_VDE, CLOCK_TYPE_PC2CC3M_T),
218 TYPE(PERIPHC_OWR, CLOCK_TYPE_PC2CC3M_T),
219 TYPE(PERIPHC_NOR, CLOCK_TYPE_PC2CC3M_T),
220 TYPE(PERIPHC_CSITE, CLOCK_TYPE_PC2CC3M_T),
221 TYPE(PERIPHC_I2S0, CLOCK_TYPE_AXPT),
222 TYPE(PERIPHC_DTV, CLOCK_TYPE_NONE),
225 TYPE(PERIPHC_38h, CLOCK_TYPE_NONE),
226 TYPE(PERIPHC_39h, CLOCK_TYPE_NONE),
227 TYPE(PERIPHC_3ah, CLOCK_TYPE_NONE),
228 TYPE(PERIPHC_3bh, CLOCK_TYPE_NONE),
229 TYPE(PERIPHC_MSENC, CLOCK_TYPE_MC2CC3P_A),
230 TYPE(PERIPHC_TSEC, CLOCK_TYPE_PC2CC3M_T),
231 TYPE(PERIPHC_3eh, CLOCK_TYPE_NONE),
232 TYPE(PERIPHC_OSC, CLOCK_TYPE_NONE),
235 TYPE(PERIPHC_40h, CLOCK_TYPE_NONE), /* start with 0x3b0 */
236 TYPE(PERIPHC_MSELECT, CLOCK_TYPE_PC2CC3M_T),
237 TYPE(PERIPHC_TSENSOR, CLOCK_TYPE_PC2CC3T_S),
238 TYPE(PERIPHC_I2S3, CLOCK_TYPE_AXPT),
239 TYPE(PERIPHC_I2S4, CLOCK_TYPE_AXPT),
240 TYPE(PERIPHC_I2C4, CLOCK_TYPE_PC2CC3M_T16),
241 TYPE(PERIPHC_SBC5, CLOCK_TYPE_PC2CC3M_T),
242 TYPE(PERIPHC_SBC6, CLOCK_TYPE_PC2CC3M_T),
245 TYPE(PERIPHC_AUDIO, CLOCK_TYPE_AC2CC3P_TS2),
246 TYPE(PERIPHC_49h, CLOCK_TYPE_NONE),
247 TYPE(PERIPHC_DAM0, CLOCK_TYPE_AC2CC3P_TS2),
248 TYPE(PERIPHC_DAM1, CLOCK_TYPE_AC2CC3P_TS2),
249 TYPE(PERIPHC_DAM2, CLOCK_TYPE_AC2CC3P_TS2),
250 TYPE(PERIPHC_HDA2CODEC2X, CLOCK_TYPE_PC2CC3M_T),
251 TYPE(PERIPHC_ACTMON, CLOCK_TYPE_PC2CC3S_T),
252 TYPE(PERIPHC_EXTPERIPH1, CLOCK_TYPE_ASPTE),
255 TYPE(PERIPHC_EXTPERIPH2, CLOCK_TYPE_ASPTE),
256 TYPE(PERIPHC_EXTPERIPH3, CLOCK_TYPE_ASPTE),
257 TYPE(PERIPHC_52h, CLOCK_TYPE_NONE),
258 TYPE(PERIPHC_I2CSLOW, CLOCK_TYPE_PC2CC3S_T),
259 TYPE(PERIPHC_SYS, CLOCK_TYPE_NONE),
260 TYPE(PERIPHC_55h, CLOCK_TYPE_NONE),
261 TYPE(PERIPHC_56h, CLOCK_TYPE_NONE),
262 TYPE(PERIPHC_57h, CLOCK_TYPE_NONE),
265 TYPE(PERIPHC_58h, CLOCK_TYPE_NONE),
266 TYPE(PERIPHC_SOR, CLOCK_TYPE_NONE),
267 TYPE(PERIPHC_5ah, CLOCK_TYPE_NONE),
268 TYPE(PERIPHC_5bh, CLOCK_TYPE_NONE),
269 TYPE(PERIPHC_SATAOOB, CLOCK_TYPE_PCMT),
270 TYPE(PERIPHC_SATA, CLOCK_TYPE_PCMT),
271 TYPE(PERIPHC_HDA, CLOCK_TYPE_PC2CC3M_T),
272 TYPE(PERIPHC_5fh, CLOCK_TYPE_NONE),
275 TYPE(PERIPHC_XUSB_CORE_HOST, CLOCK_TYPE_NONE),
276 TYPE(PERIPHC_XUSB_FALCON, CLOCK_TYPE_NONE),
277 TYPE(PERIPHC_XUSB_FS, CLOCK_TYPE_NONE),
278 TYPE(PERIPHC_XUSB_CORE_DEV, CLOCK_TYPE_NONE),
279 TYPE(PERIPHC_XUSB_SS, CLOCK_TYPE_NONE),
280 TYPE(PERIPHC_CILAB, CLOCK_TYPE_NONE),
281 TYPE(PERIPHC_CILCD, CLOCK_TYPE_NONE),
282 TYPE(PERIPHC_CILE, CLOCK_TYPE_NONE),
285 TYPE(PERIPHC_DSIA_LP, CLOCK_TYPE_NONE),
286 TYPE(PERIPHC_DSIB_LP, CLOCK_TYPE_NONE),
287 TYPE(PERIPHC_ENTROPY, CLOCK_TYPE_NONE),
288 TYPE(PERIPHC_DVFS_REF, CLOCK_TYPE_NONE),
289 TYPE(PERIPHC_DVFS_SOC, CLOCK_TYPE_NONE),
290 TYPE(PERIPHC_TRACECLKIN, CLOCK_TYPE_NONE),
291 TYPE(PERIPHC_ADX0, CLOCK_TYPE_NONE),
292 TYPE(PERIPHC_AMX0, CLOCK_TYPE_NONE),
295 TYPE(PERIPHC_EMC_LATENCY, CLOCK_TYPE_NONE),
296 TYPE(PERIPHC_SOC_THERM, CLOCK_TYPE_NONE),
297 TYPE(PERIPHC_72h, CLOCK_TYPE_NONE),
298 TYPE(PERIPHC_73h, CLOCK_TYPE_NONE),
299 TYPE(PERIPHC_74h, CLOCK_TYPE_NONE),
300 TYPE(PERIPHC_75h, CLOCK_TYPE_NONE),
301 TYPE(PERIPHC_VI_SENSOR2, CLOCK_TYPE_NONE),
302 TYPE(PERIPHC_I2C6, CLOCK_TYPE_PC2CC3M_T16),
305 TYPE(PERIPHC_78h, CLOCK_TYPE_NONE),
306 TYPE(PERIPHC_EMC_DLL, CLOCK_TYPE_MCPTM2C2C3),
307 TYPE(PERIPHC_HDMI_AUDIO, CLOCK_TYPE_NONE),
308 TYPE(PERIPHC_CLK72MHZ, CLOCK_TYPE_NONE),
309 TYPE(PERIPHC_ADX1, CLOCK_TYPE_AC2CC3P_TS2),
310 TYPE(PERIPHC_AMX1, CLOCK_TYPE_AC2CC3P_TS2),
311 TYPE(PERIPHC_VIC, CLOCK_TYPE_NONE),
312 TYPE(PERIPHC_7Fh, CLOCK_TYPE_NONE),
316 * This array translates a periph_id to a periphc_internal_id
318 * Not present/matched up:
319 * uint vi_sensor; _VI_SENSOR_0, 0x1A8
320 * SPDIF - which is both 0x08 and 0x0c
323 #define NONE(name) (-1)
324 #define OFFSET(name, value) PERIPHC_ ## name
325 static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
334 PERIPHC_UART2, /* and vfir 0x68 */
366 /* Middle word: 63:32 */
378 PERIPHC_SBC1, /* SBCx = SPIx */
406 /* Upper word 95:64 */
568 * PLL divider shift/mask tables for all PLL IDs.
570 struct clk_pll_info tegra_pll_info_table[CLOCK_ID_PLL_COUNT] = {
572 * T124: same as T114, some deviations from T2x/T30. Adds PLLDP.
573 * NOTE: If kcp_mask/kvco_mask == 0, they're not used in that PLL (PLLX, etc.)
574 * If lock_ena or lock_det are >31, they're not used in that PLL.
577 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x0F,
578 .lock_ena = 24, .lock_det = 27, .kcp_shift = 28, .kcp_mask = 3, .kvco_shift = 27, .kvco_mask = 1 }, /* PLLC */
579 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 0, .p_mask = 0,
580 .lock_ena = 0, .lock_det = 27, .kcp_shift = 1, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLM */
581 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
582 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLP */
583 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
584 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLA */
585 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x01,
586 .lock_ena = 22, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLU */
587 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
588 .lock_ena = 22, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLD */
589 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x0F,
590 .lock_ena = 18, .lock_det = 27, .kcp_shift = 0, .kcp_mask = 0, .kvco_shift = 0, .kvco_mask = 0 }, /* PLLX */
591 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 0, .p_mask = 0,
592 .lock_ena = 9, .lock_det = 11, .kcp_shift = 6, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLE */
593 { .m_shift = 0, .m_mask = 0x0F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
594 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLS (RESERVED) */
595 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0xF,
596 .lock_ena = 30, .lock_det = 27, .kcp_shift = 25, .kcp_mask = 3, .kvco_shift = 24, .kvco_mask = 1 }, /* PLLDP */
600 * Get the oscillator frequency, from the corresponding hardware configuration
601 * field. Note that Tegra30+ support 3 new higher freqs, but we map back
602 * to the old T20 freqs. Support for the higher oscillators is TBD.
604 enum clock_osc_freq clock_get_osc_freq(void)
606 struct clk_rst_ctlr *clkrst =
607 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
610 reg = readl(&clkrst->crc_osc_ctrl);
611 reg = (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
613 if (reg & 1) /* one of the newer freqs */
614 printf("Warning: OSC_FREQ is unsupported! (%d)\n", reg);
616 return reg >> 2; /* Map to most common (T20) freqs */
619 /* Returns a pointer to the clock source register for a peripheral */
620 u32 *get_periph_source_reg(enum periph_id periph_id)
622 struct clk_rst_ctlr *clkrst =
623 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
624 enum periphc_internal_id internal_id;
626 /* Coresight is a special case */
627 if (periph_id == PERIPH_ID_CSI)
628 return &clkrst->crc_clk_src[PERIPH_ID_CSI+1];
630 assert(periph_id >= PERIPH_ID_FIRST && periph_id < PERIPH_ID_COUNT);
631 internal_id = periph_id_to_internal_id[periph_id];
632 assert(internal_id != -1);
633 if (internal_id >= PERIPHC_X_FIRST) {
634 internal_id -= PERIPHC_X_FIRST;
635 return &clkrst->crc_clk_src_x[internal_id];
636 } else if (internal_id >= PERIPHC_VW_FIRST) {
637 internal_id -= PERIPHC_VW_FIRST;
638 return &clkrst->crc_clk_src_vw[internal_id];
640 return &clkrst->crc_clk_src[internal_id];
644 int get_periph_clock_info(enum periph_id periph_id, int *mux_bits,
645 int *divider_bits, int *type)
647 enum periphc_internal_id internal_id;
649 if (!clock_periph_id_isvalid(periph_id))
652 internal_id = periph_id_to_internal_id[periph_id];
653 if (!periphc_internal_id_isvalid(internal_id))
656 *type = clock_periph_type[internal_id];
657 if (!clock_type_id_isvalid(*type))
660 *mux_bits = clock_source[*type][CLOCK_MAX_MUX];
662 if (*type == CLOCK_TYPE_PC2CC3M_T16)
670 enum clock_id get_periph_clock_id(enum periph_id periph_id, int source)
672 enum periphc_internal_id internal_id;
675 if (!clock_periph_id_isvalid(periph_id))
676 return CLOCK_ID_NONE;
678 internal_id = periph_id_to_internal_id[periph_id];
679 if (!periphc_internal_id_isvalid(internal_id))
680 return CLOCK_ID_NONE;
682 type = clock_periph_type[internal_id];
683 if (!clock_type_id_isvalid(type))
684 return CLOCK_ID_NONE;
686 return clock_source[type][source];
690 * Given a peripheral ID and the required source clock, this returns which
691 * value should be programmed into the source mux for that peripheral.
693 * There is special code here to handle the one source type with 5 sources.
695 * @param periph_id peripheral to start
696 * @param source PLL id of required parent clock
697 * @param mux_bits Set to number of bits in mux register: 2 or 4
698 * @param divider_bits Set to number of divider bits (8 or 16)
699 * @return mux value (0-4, or -1 if not found)
701 int get_periph_clock_source(enum periph_id periph_id,
702 enum clock_id parent, int *mux_bits, int *divider_bits)
704 enum clock_type_id type;
707 err = get_periph_clock_info(periph_id, mux_bits, divider_bits, &type);
710 for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
711 if (clock_source[type][mux] == parent)
714 /* if we get here, either us or the caller has made a mistake */
715 printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
720 void clock_set_enable(enum periph_id periph_id, int enable)
722 struct clk_rst_ctlr *clkrst =
723 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
727 /* Enable/disable the clock to this peripheral */
728 assert(clock_periph_id_isvalid(periph_id));
729 if ((int)periph_id < (int)PERIPH_ID_VW_FIRST)
730 clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
731 else if ((int)periph_id < PERIPH_ID_X_FIRST)
732 clk = &clkrst->crc_clk_out_enb_vw[PERIPH_REG(periph_id)];
734 clk = &clkrst->crc_clk_out_enb_x;
737 reg |= PERIPH_MASK(periph_id);
739 reg &= ~PERIPH_MASK(periph_id);
743 void reset_set_enable(enum periph_id periph_id, int enable)
745 struct clk_rst_ctlr *clkrst =
746 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
750 /* Enable/disable reset to the peripheral */
751 assert(clock_periph_id_isvalid(periph_id));
752 if (periph_id < PERIPH_ID_VW_FIRST)
753 reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
754 else if ((int)periph_id < PERIPH_ID_X_FIRST)
755 reset = &clkrst->crc_rst_dev_vw[PERIPH_REG(periph_id)];
757 reset = &clkrst->crc_rst_devices_x;
760 reg |= PERIPH_MASK(periph_id);
762 reg &= ~PERIPH_MASK(periph_id);
766 #if CONFIG_IS_ENABLED(OF_CONTROL)
768 * Convert a device tree clock ID to our peripheral ID. They are mostly
769 * the same but we are very cautious so we check that a valid clock ID is
772 * @param clk_id Clock ID according to tegra124 device tree binding
773 * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
775 enum periph_id clk_id_to_periph_id(int clk_id)
777 if (clk_id > PERIPH_ID_COUNT)
778 return PERIPH_ID_NONE;
781 case PERIPH_ID_RESERVED4:
782 case PERIPH_ID_RESERVED25:
783 case PERIPH_ID_RESERVED35:
784 case PERIPH_ID_RESERVED36:
785 case PERIPH_ID_RESERVED38:
786 case PERIPH_ID_RESERVED43:
787 case PERIPH_ID_RESERVED49:
788 case PERIPH_ID_RESERVED53:
789 case PERIPH_ID_RESERVED64:
790 case PERIPH_ID_RESERVED84:
791 case PERIPH_ID_RESERVED85:
792 case PERIPH_ID_RESERVED86:
793 case PERIPH_ID_RESERVED88:
794 case PERIPH_ID_RESERVED90:
795 case PERIPH_ID_RESERVED92:
796 case PERIPH_ID_RESERVED93:
797 case PERIPH_ID_RESERVED94:
798 case PERIPH_ID_V_RESERVED2:
799 case PERIPH_ID_V_RESERVED4:
800 case PERIPH_ID_V_RESERVED17:
801 case PERIPH_ID_V_RESERVED18:
802 case PERIPH_ID_V_RESERVED19:
803 case PERIPH_ID_V_RESERVED20:
804 case PERIPH_ID_V_RESERVED21:
805 case PERIPH_ID_V_RESERVED22:
806 case PERIPH_ID_W_RESERVED2:
807 case PERIPH_ID_W_RESERVED3:
808 case PERIPH_ID_W_RESERVED4:
809 case PERIPH_ID_W_RESERVED5:
810 case PERIPH_ID_W_RESERVED6:
811 case PERIPH_ID_W_RESERVED7:
812 case PERIPH_ID_W_RESERVED9:
813 case PERIPH_ID_W_RESERVED10:
814 case PERIPH_ID_W_RESERVED11:
815 case PERIPH_ID_W_RESERVED12:
816 case PERIPH_ID_W_RESERVED13:
817 case PERIPH_ID_W_RESERVED15:
818 case PERIPH_ID_W_RESERVED16:
819 case PERIPH_ID_W_RESERVED17:
820 case PERIPH_ID_W_RESERVED18:
821 case PERIPH_ID_W_RESERVED19:
822 case PERIPH_ID_W_RESERVED20:
823 case PERIPH_ID_W_RESERVED23:
824 case PERIPH_ID_W_RESERVED29:
825 case PERIPH_ID_W_RESERVED30:
826 case PERIPH_ID_W_RESERVED31:
827 return PERIPH_ID_NONE;
832 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
834 void clock_early_init(void)
836 struct clk_rst_ctlr *clkrst =
837 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
838 struct clk_pll_info *pllinfo;
841 tegra30_set_up_pllp();
843 /* clear IDDQ before accessing any other PLLC registers */
844 pllinfo = &tegra_pll_info_table[CLOCK_ID_CGENERAL];
845 clrbits_le32(&clkrst->crc_pll[CLOCK_ID_CGENERAL].pll_misc, PLLC_IDDQ);
849 * PLLC output frequency set to 600Mhz
850 * PLLD output frequency set to 925Mhz
852 switch (clock_get_osc_freq()) {
853 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
854 clock_set_rate(CLOCK_ID_CGENERAL, 600, 12, 0, 8);
855 clock_set_rate(CLOCK_ID_DISPLAY, 925, 12, 0, 12);
858 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
859 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
860 clock_set_rate(CLOCK_ID_DISPLAY, 925, 26, 0, 12);
863 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
864 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
865 clock_set_rate(CLOCK_ID_DISPLAY, 925, 13, 0, 12);
867 case CLOCK_OSC_FREQ_19_2:
870 * These are not supported. It is too early to print a
871 * message and the UART likely won't work anyway due to the
872 * oscillator being wrong.
877 /* PLLC_MISC2: Set dynramp_stepA/B. MISC2 maps to pll_out[1] */
878 writel(0x00561600, &clkrst->crc_pll[CLOCK_ID_CGENERAL].pll_out[1]);
880 /* PLLC_MISC: Set LOCK_ENABLE */
881 pllinfo = &tegra_pll_info_table[CLOCK_ID_CGENERAL];
882 setbits_le32(&clkrst->crc_pll[CLOCK_ID_CGENERAL].pll_misc, (1 << pllinfo->lock_ena));
885 /* PLLD_MISC: Set CLKENABLE, CPCON 12, LFCON 1, and enable lock */
886 pllinfo = &tegra_pll_info_table[CLOCK_ID_DISPLAY];
887 data = (12 << pllinfo->kcp_shift) | (1 << pllinfo->kvco_shift);
888 data |= (1 << PLLD_CLKENABLE) | (1 << pllinfo->lock_ena);
889 writel(data, &clkrst->crc_pll[CLOCK_ID_DISPLAY].pll_misc);
894 * clock_early_init_done - Check if clock_early_init() has been called
896 * Check a register that we set up to see if clock_early_init() has already
899 * @return true if clock_early_init() was called, false if not
901 bool clock_early_init_done(void)
903 struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
906 val = readl(&clkrst->crc_sclk_brst_pol);
908 return val == 0x20002222;
911 void arch_timer_init(void)
913 struct sysctr_ctlr *sysctr = (struct sysctr_ctlr *)NV_PA_TSC_BASE;
916 freq = clock_get_rate(CLOCK_ID_CLK_M);
917 debug("%s: clk_m freq is %dHz [0x%08X]\n", __func__, freq, freq);
920 asm("mcr p15, 0, %0, c14, c0, 0\n" : : "r" (freq));
922 /* Only Tegra114+ has the System Counter regs */
923 debug("%s: setting CNTFID0 to 0x%08X\n", __func__, freq);
924 writel(freq, &sysctr->cntfid0);
926 val = readl(&sysctr->cntcr);
927 val |= TSC_CNTCR_ENABLE | TSC_CNTCR_HDBG;
928 writel(val, &sysctr->cntcr);
929 debug("%s: TSC CNTCR = 0x%08X\n", __func__, val);
932 #define PLLE_SS_CNTL 0x68
933 #define PLLE_SS_CNTL_SSCINCINTR(x) (((x) & 0x3f) << 24)
934 #define PLLE_SS_CNTL_SSCINC(x) (((x) & 0xff) << 16)
935 #define PLLE_SS_CNTL_SSCINVERT (1 << 15)
936 #define PLLE_SS_CNTL_SSCCENTER (1 << 14)
937 #define PLLE_SS_CNTL_SSCBYP (1 << 12)
938 #define PLLE_SS_CNTL_INTERP_RESET (1 << 11)
939 #define PLLE_SS_CNTL_BYPASS_SS (1 << 10)
940 #define PLLE_SS_CNTL_SSCMAX(x) (((x) & 0x1ff) << 0)
942 #define PLLE_BASE 0x0e8
943 #define PLLE_BASE_ENABLE (1 << 30)
944 #define PLLE_BASE_LOCK_OVERRIDE (1 << 29)
945 #define PLLE_BASE_PLDIV_CML(x) (((x) & 0xf) << 24)
946 #define PLLE_BASE_NDIV(x) (((x) & 0xff) << 8)
947 #define PLLE_BASE_MDIV(x) (((x) & 0xff) << 0)
949 #define PLLE_MISC 0x0ec
950 #define PLLE_MISC_IDDQ_SWCTL (1 << 14)
951 #define PLLE_MISC_IDDQ_OVERRIDE (1 << 13)
952 #define PLLE_MISC_LOCK_ENABLE (1 << 9)
953 #define PLLE_MISC_PTS (1 << 8)
954 #define PLLE_MISC_VREG_BG_CTRL(x) (((x) & 0x3) << 4)
955 #define PLLE_MISC_VREG_CTRL(x) (((x) & 0x3) << 2)
957 #define PLLE_AUX 0x48c
958 #define PLLE_AUX_SEQ_ENABLE (1 << 24)
959 #define PLLE_AUX_ENABLE_SWCTL (1 << 4)
961 int tegra_plle_enable(void)
963 unsigned int m = 1, n = 200, cpcon = 13;
966 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
967 value &= ~PLLE_BASE_LOCK_OVERRIDE;
968 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
970 value = readl(NV_PA_CLK_RST_BASE + PLLE_AUX);
971 value |= PLLE_AUX_ENABLE_SWCTL;
972 value &= ~PLLE_AUX_SEQ_ENABLE;
973 writel(value, NV_PA_CLK_RST_BASE + PLLE_AUX);
977 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
978 value |= PLLE_MISC_IDDQ_SWCTL;
979 value &= ~PLLE_MISC_IDDQ_OVERRIDE;
980 value |= PLLE_MISC_LOCK_ENABLE;
981 value |= PLLE_MISC_PTS;
982 value |= PLLE_MISC_VREG_BG_CTRL(3);
983 value |= PLLE_MISC_VREG_CTRL(2);
984 writel(value, NV_PA_CLK_RST_BASE + PLLE_MISC);
988 value = readl(NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
989 value |= PLLE_SS_CNTL_SSCBYP | PLLE_SS_CNTL_INTERP_RESET |
990 PLLE_SS_CNTL_BYPASS_SS;
991 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
993 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
994 value &= ~PLLE_BASE_PLDIV_CML(0xf);
995 value &= ~PLLE_BASE_NDIV(0xff);
996 value &= ~PLLE_BASE_MDIV(0xff);
997 value |= PLLE_BASE_PLDIV_CML(cpcon);
998 value |= PLLE_BASE_NDIV(n);
999 value |= PLLE_BASE_MDIV(m);
1000 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
1004 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
1005 value |= PLLE_BASE_ENABLE;
1006 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
1011 value = readl(NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
1012 value &= ~PLLE_SS_CNTL_SSCINVERT;
1013 value &= ~PLLE_SS_CNTL_SSCCENTER;
1015 value &= ~PLLE_SS_CNTL_SSCINCINTR(0x3f);
1016 value &= ~PLLE_SS_CNTL_SSCINC(0xff);
1017 value &= ~PLLE_SS_CNTL_SSCMAX(0x1ff);
1019 value |= PLLE_SS_CNTL_SSCINCINTR(0x20);
1020 value |= PLLE_SS_CNTL_SSCINC(0x01);
1021 value |= PLLE_SS_CNTL_SSCMAX(0x25);
1023 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
1025 value = readl(NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
1026 value &= ~PLLE_SS_CNTL_SSCBYP;
1027 value &= ~PLLE_SS_CNTL_BYPASS_SS;
1028 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
1032 value = readl(NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
1033 value &= ~PLLE_SS_CNTL_INTERP_RESET;
1034 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
1041 void clock_sor_enable_edp_clock(void)
1045 /* uses PLLP, has a non-standard bit layout. */
1046 reg = get_periph_source_reg(PERIPH_ID_SOR0);
1047 setbits_le32(reg, SOR0_CLK_SEL0);
1050 u32 clock_set_display_rate(u32 frequency)
1053 * plld (fo) = vco >> p, where 500MHz < vco < 1000MHz
1054 * = (cf * n) >> p, where 1MHz < cf < 6MHz
1055 * = ((ref / m) * n) >> p
1057 * Iterate the possible values of p (3 bits, 2^7) to find out a minimum
1058 * safe vco, then find best (m, n). since m has only 5 bits, we can
1059 * iterate all possible values. Note Tegra 124 supports 11 bits for n,
1060 * but our pll_fields has only 10 bits for n.
1062 * Note values undershoot or overshoot target output frequency may not
1063 * work if the values are not in "safe" range by panel specification.
1065 u32 ref = clock_get_rate(CLOCK_ID_OSC);
1066 u32 divm, divn, divp, cpcon;
1067 u32 cf, vco, rounded_rate = frequency;
1068 u32 diff, best_diff, best_m = 0, best_n = 0, best_p;
1069 const u32 max_m = 1 << 5, max_n = 1 << 10, max_p = 1 << 3,
1070 mhz = 1000 * 1000, min_vco = 500 * mhz, max_vco = 1000 * mhz,
1071 min_cf = 1 * mhz, max_cf = 6 * mhz;
1072 int mux_bits, divider_bits, source;
1074 for (divp = 0, vco = frequency; vco < min_vco && divp < max_p; divp++)
1077 if (vco < min_vco || vco > max_vco) {
1078 printf("%s: Cannot find out a supported VCO for Frequency (%u)\n",
1079 __func__, frequency);
1086 for (divm = 1; divm < max_m && best_diff; divm++) {
1097 diff = vco - divn * cf;
1098 if (divn + 1 < max_n && diff > cf / 2) {
1103 if (diff >= best_diff)
1113 else if (best_n < 300)
1115 else if (best_n < 600)
1121 printf("%s: Failed to match output frequency %u, best difference is %u\n",
1122 __func__, frequency, best_diff);
1123 rounded_rate = (ref / best_m * best_n) >> best_p;
1126 debug("%s: PLLD=%u ref=%u, m/n/p/cpcon=%u/%u/%u/%u\n",
1127 __func__, rounded_rate, ref, best_m, best_n, best_p, cpcon);
1129 source = get_periph_clock_source(PERIPH_ID_DISP1, CLOCK_ID_DISPLAY,
1130 &mux_bits, ÷r_bits);
1131 clock_ll_set_source_bits(PERIPH_ID_DISP1, mux_bits, source);
1132 clock_set_rate(CLOCK_ID_DISPLAY, best_n, best_m, best_p, cpcon);
1134 return rounded_rate;
1137 void clock_set_up_plldp(void)
1139 struct clk_rst_ctlr *clkrst =
1140 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
1143 value = PLLDP_SS_CFG_UNDOCUMENTED | PLLDP_SS_CFG_DITHER;
1144 writel(value | PLLDP_SS_CFG_CLAMP, &clkrst->crc_plldp_ss_cfg);
1145 clock_start_pll(CLOCK_ID_DP, 1, 90, 3, 0, 0);
1146 writel(value, &clkrst->crc_plldp_ss_cfg);
1149 struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid)
1151 struct clk_rst_ctlr *clkrst =
1152 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
1154 if (clkid == CLOCK_ID_DP)
1155 return &clkrst->plldp;
1160 struct periph_clk_init periph_clk_init_table[] = {
1161 { PERIPH_ID_SBC1, CLOCK_ID_PERIPH },
1162 { PERIPH_ID_SBC2, CLOCK_ID_PERIPH },
1163 { PERIPH_ID_SBC3, CLOCK_ID_PERIPH },
1164 { PERIPH_ID_SBC4, CLOCK_ID_PERIPH },
1165 { PERIPH_ID_SBC5, CLOCK_ID_PERIPH },
1166 { PERIPH_ID_SBC6, CLOCK_ID_PERIPH },
1167 { PERIPH_ID_HOST1X, CLOCK_ID_PERIPH },
1168 { PERIPH_ID_DISP1, CLOCK_ID_CGENERAL },
1169 { PERIPH_ID_SDMMC1, CLOCK_ID_PERIPH },
1170 { PERIPH_ID_SDMMC2, CLOCK_ID_PERIPH },
1171 { PERIPH_ID_SDMMC3, CLOCK_ID_PERIPH },
1172 { PERIPH_ID_SDMMC4, CLOCK_ID_PERIPH },
1173 { PERIPH_ID_PWM, CLOCK_ID_SFROM32KHZ },
1174 { PERIPH_ID_I2C1, CLOCK_ID_PERIPH },
1175 { PERIPH_ID_I2C2, CLOCK_ID_PERIPH },
1176 { PERIPH_ID_I2C3, CLOCK_ID_PERIPH },
1177 { PERIPH_ID_I2C4, CLOCK_ID_PERIPH },
1178 { PERIPH_ID_I2C5, CLOCK_ID_PERIPH },
1179 { PERIPH_ID_I2C6, CLOCK_ID_PERIPH },