2 * arch/arm/mach-tegra/tegra30_clocks.c
4 * Copyright (c) 2010-2012 NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
28 #include <linux/clk.h>
29 #include <linux/cpufreq.h>
30 #include <linux/syscore_ops.h>
32 #include <asm/clkdev.h>
34 #include <mach/powergate.h>
39 #include "tegra_cpu_car.h"
41 #define USE_PLL_LOCK_BITS 0
43 #define RST_DEVICES_L 0x004
44 #define RST_DEVICES_H 0x008
45 #define RST_DEVICES_U 0x00C
46 #define RST_DEVICES_V 0x358
47 #define RST_DEVICES_W 0x35C
48 #define RST_DEVICES_SET_L 0x300
49 #define RST_DEVICES_CLR_L 0x304
50 #define RST_DEVICES_SET_V 0x430
51 #define RST_DEVICES_CLR_V 0x434
52 #define RST_DEVICES_NUM 5
54 #define CLK_OUT_ENB_L 0x010
55 #define CLK_OUT_ENB_H 0x014
56 #define CLK_OUT_ENB_U 0x018
57 #define CLK_OUT_ENB_V 0x360
58 #define CLK_OUT_ENB_W 0x364
59 #define CLK_OUT_ENB_SET_L 0x320
60 #define CLK_OUT_ENB_CLR_L 0x324
61 #define CLK_OUT_ENB_SET_V 0x440
62 #define CLK_OUT_ENB_CLR_V 0x444
63 #define CLK_OUT_ENB_NUM 5
65 #define RST_DEVICES_V_SWR_CPULP_RST_DIS (0x1 << 1)
66 #define CLK_OUT_ENB_V_CLK_ENB_CPULP_EN (0x1 << 1)
68 #define PERIPH_CLK_TO_BIT(c) (1 << (c->u.periph.clk_num % 32))
69 #define PERIPH_CLK_TO_RST_REG(c) \
70 periph_clk_to_reg((c), RST_DEVICES_L, RST_DEVICES_V, 4)
71 #define PERIPH_CLK_TO_RST_SET_REG(c) \
72 periph_clk_to_reg((c), RST_DEVICES_SET_L, RST_DEVICES_SET_V, 8)
73 #define PERIPH_CLK_TO_RST_CLR_REG(c) \
74 periph_clk_to_reg((c), RST_DEVICES_CLR_L, RST_DEVICES_CLR_V, 8)
76 #define PERIPH_CLK_TO_ENB_REG(c) \
77 periph_clk_to_reg((c), CLK_OUT_ENB_L, CLK_OUT_ENB_V, 4)
78 #define PERIPH_CLK_TO_ENB_SET_REG(c) \
79 periph_clk_to_reg((c), CLK_OUT_ENB_SET_L, CLK_OUT_ENB_SET_V, 8)
80 #define PERIPH_CLK_TO_ENB_CLR_REG(c) \
81 periph_clk_to_reg((c), CLK_OUT_ENB_CLR_L, CLK_OUT_ENB_CLR_V, 8)
83 #define CLK_MASK_ARM 0x44
84 #define MISC_CLK_ENB 0x48
87 #define OSC_CTRL_OSC_FREQ_MASK (0xF<<28)
88 #define OSC_CTRL_OSC_FREQ_13MHZ (0x0<<28)
89 #define OSC_CTRL_OSC_FREQ_19_2MHZ (0x4<<28)
90 #define OSC_CTRL_OSC_FREQ_12MHZ (0x8<<28)
91 #define OSC_CTRL_OSC_FREQ_26MHZ (0xC<<28)
92 #define OSC_CTRL_OSC_FREQ_16_8MHZ (0x1<<28)
93 #define OSC_CTRL_OSC_FREQ_38_4MHZ (0x5<<28)
94 #define OSC_CTRL_OSC_FREQ_48MHZ (0x9<<28)
95 #define OSC_CTRL_MASK (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
97 #define OSC_CTRL_PLL_REF_DIV_MASK (3<<26)
98 #define OSC_CTRL_PLL_REF_DIV_1 (0<<26)
99 #define OSC_CTRL_PLL_REF_DIV_2 (1<<26)
100 #define OSC_CTRL_PLL_REF_DIV_4 (2<<26)
102 #define OSC_FREQ_DET 0x58
103 #define OSC_FREQ_DET_TRIG (1<<31)
105 #define OSC_FREQ_DET_STATUS 0x5C
106 #define OSC_FREQ_DET_BUSY (1<<31)
107 #define OSC_FREQ_DET_CNT_MASK 0xFFFF
109 #define PERIPH_CLK_SOURCE_I2S1 0x100
110 #define PERIPH_CLK_SOURCE_EMC 0x19c
111 #define PERIPH_CLK_SOURCE_OSC 0x1fc
112 #define PERIPH_CLK_SOURCE_NUM1 \
113 ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
115 #define PERIPH_CLK_SOURCE_G3D2 0x3b0
116 #define PERIPH_CLK_SOURCE_SE 0x42c
117 #define PERIPH_CLK_SOURCE_NUM2 \
118 ((PERIPH_CLK_SOURCE_SE - PERIPH_CLK_SOURCE_G3D2) / 4 + 1)
120 #define AUDIO_DLY_CLK 0x49c
121 #define AUDIO_SYNC_CLK_SPDIF 0x4b4
122 #define PERIPH_CLK_SOURCE_NUM3 \
123 ((AUDIO_SYNC_CLK_SPDIF - AUDIO_DLY_CLK) / 4 + 1)
125 #define PERIPH_CLK_SOURCE_NUM (PERIPH_CLK_SOURCE_NUM1 + \
126 PERIPH_CLK_SOURCE_NUM2 + \
127 PERIPH_CLK_SOURCE_NUM3)
129 #define CPU_SOFTRST_CTRL 0x380
131 #define PERIPH_CLK_SOURCE_DIVU71_MASK 0xFF
132 #define PERIPH_CLK_SOURCE_DIVU16_MASK 0xFFFF
133 #define PERIPH_CLK_SOURCE_DIV_SHIFT 0
134 #define PERIPH_CLK_SOURCE_DIVIDLE_SHIFT 8
135 #define PERIPH_CLK_SOURCE_DIVIDLE_VAL 50
136 #define PERIPH_CLK_UART_DIV_ENB (1<<24)
137 #define PERIPH_CLK_VI_SEL_EX_SHIFT 24
138 #define PERIPH_CLK_VI_SEL_EX_MASK (0x3<<PERIPH_CLK_VI_SEL_EX_SHIFT)
139 #define PERIPH_CLK_NAND_DIV_EX_ENB (1<<8)
140 #define PERIPH_CLK_DTV_POLARITY_INV (1<<25)
142 #define AUDIO_SYNC_SOURCE_MASK 0x0F
143 #define AUDIO_SYNC_DISABLE_BIT 0x10
144 #define AUDIO_SYNC_TAP_NIBBLE_SHIFT(c) ((c->reg_shift - 24) * 4)
147 #define PLL_BASE_BYPASS (1<<31)
148 #define PLL_BASE_ENABLE (1<<30)
149 #define PLL_BASE_REF_ENABLE (1<<29)
150 #define PLL_BASE_OVERRIDE (1<<28)
151 #define PLL_BASE_LOCK (1<<27)
152 #define PLL_BASE_DIVP_MASK (0x7<<20)
153 #define PLL_BASE_DIVP_SHIFT 20
154 #define PLL_BASE_DIVN_MASK (0x3FF<<8)
155 #define PLL_BASE_DIVN_SHIFT 8
156 #define PLL_BASE_DIVM_MASK (0x1F)
157 #define PLL_BASE_DIVM_SHIFT 0
159 #define PLL_OUT_RATIO_MASK (0xFF<<8)
160 #define PLL_OUT_RATIO_SHIFT 8
161 #define PLL_OUT_OVERRIDE (1<<2)
162 #define PLL_OUT_CLKEN (1<<1)
163 #define PLL_OUT_RESET_DISABLE (1<<0)
165 #define PLL_MISC(c) \
166 (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
167 #define PLL_MISC_LOCK_ENABLE(c) \
168 (((c)->flags & (PLLU | PLLD)) ? (1<<22) : (1<<18))
170 #define PLL_MISC_DCCON_SHIFT 20
171 #define PLL_MISC_CPCON_SHIFT 8
172 #define PLL_MISC_CPCON_MASK (0xF<<PLL_MISC_CPCON_SHIFT)
173 #define PLL_MISC_LFCON_SHIFT 4
174 #define PLL_MISC_LFCON_MASK (0xF<<PLL_MISC_LFCON_SHIFT)
175 #define PLL_MISC_VCOCON_SHIFT 0
176 #define PLL_MISC_VCOCON_MASK (0xF<<PLL_MISC_VCOCON_SHIFT)
177 #define PLLD_MISC_CLKENABLE (1<<30)
179 #define PLLU_BASE_POST_DIV (1<<20)
181 #define PLLD_BASE_DSIB_MUX_SHIFT 25
182 #define PLLD_BASE_DSIB_MUX_MASK (1<<PLLD_BASE_DSIB_MUX_SHIFT)
183 #define PLLD_BASE_CSI_CLKENABLE (1<<26)
184 #define PLLD_MISC_DSI_CLKENABLE (1<<30)
185 #define PLLD_MISC_DIV_RST (1<<23)
186 #define PLLD_MISC_DCCON_SHIFT 12
188 #define PLLDU_LFCON_SET_DIVN 600
190 /* FIXME: OUT_OF_TABLE_CPCON per pll */
191 #define OUT_OF_TABLE_CPCON 0x8
193 #define SUPER_CLK_MUX 0x00
194 #define SUPER_STATE_SHIFT 28
195 #define SUPER_STATE_MASK (0xF << SUPER_STATE_SHIFT)
196 #define SUPER_STATE_STANDBY (0x0 << SUPER_STATE_SHIFT)
197 #define SUPER_STATE_IDLE (0x1 << SUPER_STATE_SHIFT)
198 #define SUPER_STATE_RUN (0x2 << SUPER_STATE_SHIFT)
199 #define SUPER_STATE_IRQ (0x3 << SUPER_STATE_SHIFT)
200 #define SUPER_STATE_FIQ (0x4 << SUPER_STATE_SHIFT)
201 #define SUPER_LP_DIV2_BYPASS (0x1 << 16)
202 #define SUPER_SOURCE_MASK 0xF
203 #define SUPER_FIQ_SOURCE_SHIFT 12
204 #define SUPER_IRQ_SOURCE_SHIFT 8
205 #define SUPER_RUN_SOURCE_SHIFT 4
206 #define SUPER_IDLE_SOURCE_SHIFT 0
208 #define SUPER_CLK_DIVIDER 0x04
209 #define SUPER_CLOCK_DIV_U71_SHIFT 16
210 #define SUPER_CLOCK_DIV_U71_MASK (0xff << SUPER_CLOCK_DIV_U71_SHIFT)
211 /* guarantees safe cpu backup */
212 #define SUPER_CLOCK_DIV_U71_MIN 0x2
214 #define BUS_CLK_DISABLE (1<<3)
215 #define BUS_CLK_DIV_MASK 0x3
218 #define PMC_CTRL_BLINK_ENB (1 << 7)
220 #define PMC_DPD_PADS_ORIDE 0x1c
221 #define PMC_DPD_PADS_ORIDE_BLINK_ENB (1 << 20)
223 #define PMC_BLINK_TIMER_DATA_ON_SHIFT 0
224 #define PMC_BLINK_TIMER_DATA_ON_MASK 0x7fff
225 #define PMC_BLINK_TIMER_ENB (1 << 15)
226 #define PMC_BLINK_TIMER_DATA_OFF_SHIFT 16
227 #define PMC_BLINK_TIMER_DATA_OFF_MASK 0xffff
229 #define PMC_PLLP_WB0_OVERRIDE 0xf8
230 #define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE (1 << 12)
232 #define UTMIP_PLL_CFG2 0x488
233 #define UTMIP_PLL_CFG2_STABLE_COUNT(x) (((x) & 0xfff) << 6)
234 #define UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(x) (((x) & 0x3f) << 18)
235 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN (1 << 0)
236 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN (1 << 2)
237 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN (1 << 4)
239 #define UTMIP_PLL_CFG1 0x484
240 #define UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
241 #define UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
242 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN (1 << 14)
243 #define UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN (1 << 12)
244 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN (1 << 16)
246 #define PLLE_BASE_CML_ENABLE (1<<31)
247 #define PLLE_BASE_ENABLE (1<<30)
248 #define PLLE_BASE_DIVCML_SHIFT 24
249 #define PLLE_BASE_DIVCML_MASK (0xf<<PLLE_BASE_DIVCML_SHIFT)
250 #define PLLE_BASE_DIVP_SHIFT 16
251 #define PLLE_BASE_DIVP_MASK (0x3f<<PLLE_BASE_DIVP_SHIFT)
252 #define PLLE_BASE_DIVN_SHIFT 8
253 #define PLLE_BASE_DIVN_MASK (0xFF<<PLLE_BASE_DIVN_SHIFT)
254 #define PLLE_BASE_DIVM_SHIFT 0
255 #define PLLE_BASE_DIVM_MASK (0xFF<<PLLE_BASE_DIVM_SHIFT)
256 #define PLLE_BASE_DIV_MASK \
257 (PLLE_BASE_DIVCML_MASK | PLLE_BASE_DIVP_MASK | \
258 PLLE_BASE_DIVN_MASK | PLLE_BASE_DIVM_MASK)
259 #define PLLE_BASE_DIV(m, n, p, cml) \
260 (((cml)<<PLLE_BASE_DIVCML_SHIFT) | ((p)<<PLLE_BASE_DIVP_SHIFT) | \
261 ((n)<<PLLE_BASE_DIVN_SHIFT) | ((m)<<PLLE_BASE_DIVM_SHIFT))
263 #define PLLE_MISC_SETUP_BASE_SHIFT 16
264 #define PLLE_MISC_SETUP_BASE_MASK (0xFFFF<<PLLE_MISC_SETUP_BASE_SHIFT)
265 #define PLLE_MISC_READY (1<<15)
266 #define PLLE_MISC_LOCK (1<<11)
267 #define PLLE_MISC_LOCK_ENABLE (1<<9)
268 #define PLLE_MISC_SETUP_EX_SHIFT 2
269 #define PLLE_MISC_SETUP_EX_MASK (0x3<<PLLE_MISC_SETUP_EX_SHIFT)
270 #define PLLE_MISC_SETUP_MASK \
271 (PLLE_MISC_SETUP_BASE_MASK | PLLE_MISC_SETUP_EX_MASK)
272 #define PLLE_MISC_SETUP_VALUE \
273 ((0x7<<PLLE_MISC_SETUP_BASE_SHIFT) | (0x0<<PLLE_MISC_SETUP_EX_SHIFT))
275 #define PLLE_SS_CTRL 0x68
276 #define PLLE_SS_INCINTRV_SHIFT 24
277 #define PLLE_SS_INCINTRV_MASK (0x3f<<PLLE_SS_INCINTRV_SHIFT)
278 #define PLLE_SS_INC_SHIFT 16
279 #define PLLE_SS_INC_MASK (0xff<<PLLE_SS_INC_SHIFT)
280 #define PLLE_SS_MAX_SHIFT 0
281 #define PLLE_SS_MAX_MASK (0x1ff<<PLLE_SS_MAX_SHIFT)
282 #define PLLE_SS_COEFFICIENTS_MASK \
283 (PLLE_SS_INCINTRV_MASK | PLLE_SS_INC_MASK | PLLE_SS_MAX_MASK)
284 #define PLLE_SS_COEFFICIENTS_12MHZ \
285 ((0x18<<PLLE_SS_INCINTRV_SHIFT) | (0x1<<PLLE_SS_INC_SHIFT) | \
286 (0x24<<PLLE_SS_MAX_SHIFT))
287 #define PLLE_SS_DISABLE ((1<<12) | (1<<11) | (1<<10))
289 #define PLLE_AUX 0x48c
290 #define PLLE_AUX_PLLP_SEL (1<<2)
291 #define PLLE_AUX_CML_SATA_ENABLE (1<<1)
292 #define PLLE_AUX_CML_PCIE_ENABLE (1<<0)
294 #define PMC_SATA_PWRGT 0x1ac
295 #define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE (1<<5)
296 #define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL (1<<4)
298 #define ROUND_DIVIDER_UP 0
299 #define ROUND_DIVIDER_DOWN 1
301 /* FIXME: recommended safety delay after lock is detected */
302 #define PLL_POST_LOCK_DELAY 100
304 /* Tegra CPU clock and reset control regs */
305 #define TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX 0x4c
306 #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET 0x340
307 #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR 0x344
308 #define TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR 0x34c
309 #define TEGRA30_CLK_RST_CONTROLLER_CPU_CMPLX_STATUS 0x470
311 #define CPU_CLOCK(cpu) (0x1 << (8 + cpu))
312 #define CPU_RESET(cpu) (0x1111ul << (cpu))
314 #define CLK_RESET_CCLK_BURST 0x20
315 #define CLK_RESET_CCLK_DIVIDER 0x24
316 #define CLK_RESET_PLLX_BASE 0xe0
317 #define CLK_RESET_PLLX_MISC 0xe4
319 #define CLK_RESET_SOURCE_CSITE 0x1d4
321 #define CLK_RESET_CCLK_BURST_POLICY_SHIFT 28
322 #define CLK_RESET_CCLK_RUN_POLICY_SHIFT 4
323 #define CLK_RESET_CCLK_IDLE_POLICY_SHIFT 0
324 #define CLK_RESET_CCLK_IDLE_POLICY 1
325 #define CLK_RESET_CCLK_RUN_POLICY 2
326 #define CLK_RESET_CCLK_BURST_POLICY_PLLX 8
328 #ifdef CONFIG_PM_SLEEP
329 static struct cpu_clk_suspend_context {
336 } tegra30_cpu_clk_sctx;
340 * Structure defining the fields for USB UTMI clocks Parameters.
342 struct utmi_clk_param {
343 /* Oscillator Frequency in KHz */
345 /* UTMIP PLL Enable Delay Count */
346 u8 enable_delay_count;
347 /* UTMIP PLL Stable count */
349 /* UTMIP PLL Active delay count */
350 u8 active_delay_count;
351 /* UTMIP PLL Xtal frequency count */
355 static const struct utmi_clk_param utmi_parameters[] = {
357 .osc_frequency = 13000000,
358 .enable_delay_count = 0x02,
359 .stable_count = 0x33,
360 .active_delay_count = 0x05,
361 .xtal_freq_count = 0x7F
364 .osc_frequency = 19200000,
365 .enable_delay_count = 0x03,
366 .stable_count = 0x4B,
367 .active_delay_count = 0x06,
368 .xtal_freq_count = 0xBB},
370 .osc_frequency = 12000000,
371 .enable_delay_count = 0x02,
372 .stable_count = 0x2F,
373 .active_delay_count = 0x04,
374 .xtal_freq_count = 0x76
377 .osc_frequency = 26000000,
378 .enable_delay_count = 0x04,
379 .stable_count = 0x66,
380 .active_delay_count = 0x09,
381 .xtal_freq_count = 0xFE
384 .osc_frequency = 16800000,
385 .enable_delay_count = 0x03,
386 .stable_count = 0x41,
387 .active_delay_count = 0x0A,
388 .xtal_freq_count = 0xA4
392 static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
393 static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
394 static void __iomem *misc_gp_hidrev_base = IO_ADDRESS(TEGRA_APB_MISC_BASE);
396 #define MISC_GP_HIDREV 0x804
399 * Some peripheral clocks share an enable bit, so refcount the enable bits
400 * in registers CLK_ENABLE_L, ... CLK_ENABLE_W
402 static int tegra_periph_clk_enable_refcount[CLK_OUT_ENB_NUM * 32];
404 #define clk_writel(value, reg) \
405 __raw_writel(value, reg_clk_base + (reg))
406 #define clk_readl(reg) \
407 __raw_readl(reg_clk_base + (reg))
408 #define pmc_writel(value, reg) \
409 __raw_writel(value, reg_pmc_base + (reg))
410 #define pmc_readl(reg) \
411 __raw_readl(reg_pmc_base + (reg))
412 #define chipid_readl() \
413 __raw_readl(misc_gp_hidrev_base + MISC_GP_HIDREV)
415 #define clk_writel_delay(value, reg) \
417 __raw_writel((value), reg_clk_base + (reg)); \
421 static inline int clk_set_div(struct clk_tegra *c, u32 n)
423 struct clk *clk = c->hw.clk;
425 return clk_set_rate(clk,
426 (__clk_get_rate(__clk_get_parent(clk)) + n - 1) / n);
429 static inline u32 periph_clk_to_reg(
430 struct clk_tegra *c, u32 reg_L, u32 reg_V, int offs)
432 u32 reg = c->u.periph.clk_num / 32;
433 BUG_ON(reg >= RST_DEVICES_NUM);
435 reg = reg_L + (reg * offs);
437 reg = reg_V + ((reg - 3) * offs);
441 static unsigned long clk_measure_input_freq(void)
443 u32 clock_autodetect;
444 clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
445 do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
446 clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
447 if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
449 } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
451 } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
453 } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
455 } else if (clock_autodetect >= 1025 - 3 && clock_autodetect <= 1025 + 3) {
457 } else if (clock_autodetect >= 2344 - 3 && clock_autodetect <= 2344 + 3) {
459 } else if (clock_autodetect >= 2928 - 3 && clock_autodetect <= 2928 + 3) {
462 pr_err("%s: Unexpected clock autodetect value %d", __func__,
469 static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate,
470 u32 flags, u32 round_mode)
472 s64 divider_u71 = parent_rate;
476 if (!(flags & DIV_U71_INT))
478 if (round_mode == ROUND_DIVIDER_UP)
479 divider_u71 += rate - 1;
480 do_div(divider_u71, rate);
481 if (flags & DIV_U71_INT)
484 if (divider_u71 - 2 < 0)
487 if (divider_u71 - 2 > 255)
490 return divider_u71 - 2;
493 static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
497 divider_u16 = parent_rate;
500 divider_u16 += rate - 1;
501 do_div(divider_u16, rate);
503 if (divider_u16 - 1 < 0)
506 if (divider_u16 - 1 > 0xFFFF)
509 return divider_u16 - 1;
512 static unsigned long tegra30_clk_fixed_recalc_rate(struct clk_hw *hw,
513 unsigned long parent_rate)
515 return to_clk_tegra(hw)->fixed_rate;
518 struct clk_ops tegra30_clk_32k_ops = {
519 .recalc_rate = tegra30_clk_fixed_recalc_rate,
522 /* clk_m functions */
523 static unsigned long tegra30_clk_m_recalc_rate(struct clk_hw *hw,
524 unsigned long parent_rate)
526 if (!to_clk_tegra(hw)->fixed_rate)
527 to_clk_tegra(hw)->fixed_rate = clk_measure_input_freq();
528 return to_clk_tegra(hw)->fixed_rate;
531 static void tegra30_clk_m_init(struct clk_hw *hw)
533 u32 osc_ctrl = clk_readl(OSC_CTRL);
534 u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
535 u32 pll_ref_div = osc_ctrl & OSC_CTRL_PLL_REF_DIV_MASK;
537 switch (to_clk_tegra(hw)->fixed_rate) {
539 auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
540 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
543 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
544 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
547 auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
548 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
551 auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
552 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
555 auto_clock_control |= OSC_CTRL_OSC_FREQ_16_8MHZ;
556 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
559 auto_clock_control |= OSC_CTRL_OSC_FREQ_38_4MHZ;
560 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_2);
563 auto_clock_control |= OSC_CTRL_OSC_FREQ_48MHZ;
564 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_4);
567 pr_err("%s: Unexpected clock rate %ld", __func__,
568 to_clk_tegra(hw)->fixed_rate);
571 clk_writel(auto_clock_control, OSC_CTRL);
574 struct clk_ops tegra30_clk_m_ops = {
575 .init = tegra30_clk_m_init,
576 .recalc_rate = tegra30_clk_m_recalc_rate,
579 static unsigned long tegra30_clk_m_div_recalc_rate(struct clk_hw *hw,
580 unsigned long parent_rate)
582 struct clk_tegra *c = to_clk_tegra(hw);
583 u64 rate = parent_rate;
585 if (c->mul != 0 && c->div != 0) {
587 rate += c->div - 1; /* round up */
588 do_div(rate, c->div);
594 struct clk_ops tegra_clk_m_div_ops = {
595 .recalc_rate = tegra30_clk_m_div_recalc_rate,
598 /* PLL reference divider functions */
599 static unsigned long tegra30_pll_ref_recalc_rate(struct clk_hw *hw,
600 unsigned long parent_rate)
602 struct clk_tegra *c = to_clk_tegra(hw);
603 unsigned long rate = parent_rate;
604 u32 pll_ref_div = clk_readl(OSC_CTRL) & OSC_CTRL_PLL_REF_DIV_MASK;
606 switch (pll_ref_div) {
607 case OSC_CTRL_PLL_REF_DIV_1:
610 case OSC_CTRL_PLL_REF_DIV_2:
613 case OSC_CTRL_PLL_REF_DIV_4:
617 pr_err("%s: Invalid pll ref divider %d", __func__, pll_ref_div);
622 if (c->mul != 0 && c->div != 0) {
624 rate += c->div - 1; /* round up */
625 do_div(rate, c->div);
631 struct clk_ops tegra_pll_ref_ops = {
632 .recalc_rate = tegra30_pll_ref_recalc_rate,
635 /* super clock functions */
636 /* "super clocks" on tegra30 have two-stage muxes, fractional 7.1 divider and
637 * clock skipping super divider. We will ignore the clock skipping divider,
638 * since we can't lower the voltage when using the clock skip, but we can if
639 * we lower the PLL frequency. We will use 7.1 divider for CPU super-clock
640 * only when its parent is a fixed rate PLL, since we can't change PLL rate
643 static void tegra30_super_clk_init(struct clk_hw *hw)
645 struct clk_tegra *c = to_clk_tegra(hw);
646 struct clk_tegra *p =
647 to_clk_tegra(__clk_get_hw(__clk_get_parent(hw->clk)));
650 if (c->flags & DIV_U71) {
651 /* Init safe 7.1 divider value (does not affect PLLX path) */
652 clk_writel(SUPER_CLOCK_DIV_U71_MIN << SUPER_CLOCK_DIV_U71_SHIFT,
653 c->reg + SUPER_CLK_DIVIDER);
656 if (!(p->flags & PLLX))
657 c->div += SUPER_CLOCK_DIV_U71_MIN;
659 clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
662 static u8 tegra30_super_clk_get_parent(struct clk_hw *hw)
664 struct clk_tegra *c = to_clk_tegra(hw);
669 val = clk_readl(c->reg + SUPER_CLK_MUX);
670 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
671 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
672 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
673 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
674 source = (val >> shift) & SUPER_SOURCE_MASK;
675 if (c->flags & DIV_2)
676 source |= val & SUPER_LP_DIV2_BYPASS;
681 static int tegra30_super_clk_set_parent(struct clk_hw *hw, u8 index)
683 struct clk_tegra *c = to_clk_tegra(hw);
684 struct clk_tegra *p =
685 to_clk_tegra(__clk_get_hw(clk_get_parent(hw->clk)));
689 val = clk_readl(c->reg + SUPER_CLK_MUX);
690 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
691 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
692 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
693 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
695 /* For LP mode super-clock switch between PLLX direct
696 and divided-by-2 outputs is allowed only when other
697 than PLLX clock source is current parent */
698 if ((c->flags & DIV_2) && (p->flags & PLLX) &&
699 ((index ^ val) & SUPER_LP_DIV2_BYPASS)) {
702 val ^= SUPER_LP_DIV2_BYPASS;
703 clk_writel_delay(val, c->reg);
705 val &= ~(SUPER_SOURCE_MASK << shift);
706 val |= (index & SUPER_SOURCE_MASK) << shift;
708 /* 7.1 divider for CPU super-clock does not affect
710 if (c->flags & DIV_U71) {
712 if (!(p->flags & PLLX)) {
713 div = clk_readl(c->reg +
715 div &= SUPER_CLOCK_DIV_U71_MASK;
716 div >>= SUPER_CLOCK_DIV_U71_SHIFT;
721 clk_writel_delay(val, c->reg);
727 * Do not use super clocks "skippers", since dividing using a clock skipper
728 * does not allow the voltage to be scaled down. Instead adjust the rate of
729 * the parent clock. This requires that the parent of a super clock have no
730 * other children, otherwise the rate will change underneath the other
731 * children. Special case: if fixed rate PLL is CPU super clock parent the
732 * rate of this PLL can't be changed, and it has many other children. In
733 * this case use 7.1 fractional divider to adjust the super clock rate.
735 static int tegra30_super_clk_set_rate(struct clk_hw *hw, unsigned long rate,
736 unsigned long parent_rate)
738 struct clk_tegra *c = to_clk_tegra(hw);
739 struct clk *parent = __clk_get_parent(hw->clk);
740 struct clk_tegra *cparent = to_clk_tegra(__clk_get_hw(parent));
742 if ((c->flags & DIV_U71) && (cparent->flags & PLL_FIXED)) {
743 int div = clk_div71_get_divider(parent_rate,
744 rate, c->flags, ROUND_DIVIDER_DOWN);
745 div = max(div, SUPER_CLOCK_DIV_U71_MIN);
747 clk_writel(div << SUPER_CLOCK_DIV_U71_SHIFT,
748 c->reg + SUPER_CLK_DIVIDER);
756 static unsigned long tegra30_super_clk_recalc_rate(struct clk_hw *hw,
757 unsigned long parent_rate)
759 struct clk_tegra *c = to_clk_tegra(hw);
760 u64 rate = parent_rate;
762 if (c->mul != 0 && c->div != 0) {
764 rate += c->div - 1; /* round up */
765 do_div(rate, c->div);
771 static long tegra30_super_clk_round_rate(struct clk_hw *hw, unsigned long rate,
772 unsigned long *prate)
774 struct clk_tegra *c = to_clk_tegra(hw);
775 struct clk *parent = __clk_get_parent(hw->clk);
776 struct clk_tegra *cparent = to_clk_tegra(__clk_get_hw(parent));
780 if ((c->flags & DIV_U71) && (cparent->flags & PLL_FIXED)) {
781 div = clk_div71_get_divider(*prate,
782 rate, c->flags, ROUND_DIVIDER_DOWN);
783 div = max(div, SUPER_CLOCK_DIV_U71_MIN) + 2;
785 rate += div - 1; /* round up */
786 do_div(rate, c->div);
793 struct clk_ops tegra30_super_ops = {
794 .init = tegra30_super_clk_init,
795 .set_parent = tegra30_super_clk_set_parent,
796 .get_parent = tegra30_super_clk_get_parent,
797 .recalc_rate = tegra30_super_clk_recalc_rate,
798 .round_rate = tegra30_super_clk_round_rate,
799 .set_rate = tegra30_super_clk_set_rate,
802 static unsigned long tegra30_twd_clk_recalc_rate(struct clk_hw *hw,
803 unsigned long parent_rate)
805 struct clk_tegra *c = to_clk_tegra(hw);
806 u64 rate = parent_rate;
808 if (c->mul != 0 && c->div != 0) {
810 rate += c->div - 1; /* round up */
811 do_div(rate, c->div);
817 struct clk_ops tegra30_twd_ops = {
818 .recalc_rate = tegra30_twd_clk_recalc_rate,
821 /* bus clock functions */
822 static int tegra30_bus_clk_is_enabled(struct clk_hw *hw)
824 struct clk_tegra *c = to_clk_tegra(hw);
825 u32 val = clk_readl(c->reg);
827 c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
831 static int tegra30_bus_clk_enable(struct clk_hw *hw)
833 struct clk_tegra *c = to_clk_tegra(hw);
836 val = clk_readl(c->reg);
837 val &= ~(BUS_CLK_DISABLE << c->reg_shift);
838 clk_writel(val, c->reg);
843 static void tegra30_bus_clk_disable(struct clk_hw *hw)
845 struct clk_tegra *c = to_clk_tegra(hw);
848 val = clk_readl(c->reg);
849 val |= BUS_CLK_DISABLE << c->reg_shift;
850 clk_writel(val, c->reg);
853 static unsigned long tegra30_bus_clk_recalc_rate(struct clk_hw *hw,
856 struct clk_tegra *c = to_clk_tegra(hw);
857 u32 val = clk_readl(c->reg);
860 c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
863 if (c->mul != 0 && c->div != 0) {
865 rate += c->div - 1; /* round up */
866 do_div(rate, c->div);
871 static int tegra30_bus_clk_set_rate(struct clk_hw *hw, unsigned long rate,
872 unsigned long parent_rate)
874 struct clk_tegra *c = to_clk_tegra(hw);
879 val = clk_readl(c->reg);
880 for (i = 1; i <= 4; i++) {
881 if (rate == parent_rate / i) {
882 val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
883 val |= (i - 1) << c->reg_shift;
884 clk_writel(val, c->reg);
895 static long tegra30_bus_clk_round_rate(struct clk_hw *hw, unsigned long rate,
896 unsigned long *prate)
898 unsigned long parent_rate = *prate;
901 if (rate >= parent_rate)
904 divider = parent_rate;
906 do_div(divider, rate);
913 do_div(parent_rate, divider);
918 struct clk_ops tegra30_bus_ops = {
919 .is_enabled = tegra30_bus_clk_is_enabled,
920 .enable = tegra30_bus_clk_enable,
921 .disable = tegra30_bus_clk_disable,
922 .set_rate = tegra30_bus_clk_set_rate,
923 .round_rate = tegra30_bus_clk_round_rate,
924 .recalc_rate = tegra30_bus_clk_recalc_rate,
927 /* Blink output functions */
928 static int tegra30_blink_clk_is_enabled(struct clk_hw *hw)
930 struct clk_tegra *c = to_clk_tegra(hw);
933 val = pmc_readl(PMC_CTRL);
934 c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
938 static int tegra30_blink_clk_enable(struct clk_hw *hw)
942 val = pmc_readl(PMC_DPD_PADS_ORIDE);
943 pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
945 val = pmc_readl(PMC_CTRL);
946 pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
951 static void tegra30_blink_clk_disable(struct clk_hw *hw)
955 val = pmc_readl(PMC_CTRL);
956 pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
958 val = pmc_readl(PMC_DPD_PADS_ORIDE);
959 pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
962 static int tegra30_blink_clk_set_rate(struct clk_hw *hw, unsigned long rate,
963 unsigned long parent_rate)
965 struct clk_tegra *c = to_clk_tegra(hw);
967 if (rate >= parent_rate) {
969 pmc_writel(0, c->reg);
974 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
977 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
978 PMC_BLINK_TIMER_DATA_ON_SHIFT;
979 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
980 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
982 val |= PMC_BLINK_TIMER_ENB;
983 pmc_writel(val, c->reg);
989 static unsigned long tegra30_blink_clk_recalc_rate(struct clk_hw *hw,
990 unsigned long parent_rate)
992 struct clk_tegra *c = to_clk_tegra(hw);
993 u64 rate = parent_rate;
1000 val = pmc_readl(c->reg);
1002 if (val & PMC_BLINK_TIMER_ENB) {
1003 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
1004 PMC_BLINK_TIMER_DATA_ON_MASK;
1005 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
1006 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
1008 /* each tick in the blink timer is 4 32KHz clocks */
1014 if (mul != 0 && div != 0) {
1016 rate += div - 1; /* round up */
1022 static long tegra30_blink_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1023 unsigned long *prate)
1027 long round_rate = *prate;
1031 if (rate >= *prate) {
1034 div = DIV_ROUND_UP(*prate / 8, rate);
1039 round_rate += div - 1;
1040 do_div(round_rate, div);
1045 struct clk_ops tegra30_blink_clk_ops = {
1046 .is_enabled = tegra30_blink_clk_is_enabled,
1047 .enable = tegra30_blink_clk_enable,
1048 .disable = tegra30_blink_clk_disable,
1049 .recalc_rate = tegra30_blink_clk_recalc_rate,
1050 .round_rate = tegra30_blink_clk_round_rate,
1051 .set_rate = tegra30_blink_clk_set_rate,
1054 static void tegra30_utmi_param_configure(struct clk_hw *hw)
1056 unsigned long main_rate =
1057 __clk_get_rate(__clk_get_parent(__clk_get_parent(hw->clk)));
1061 for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
1062 if (main_rate == utmi_parameters[i].osc_frequency)
1066 if (i >= ARRAY_SIZE(utmi_parameters)) {
1067 pr_err("%s: Unexpected main rate %lu\n", __func__, main_rate);
1071 reg = clk_readl(UTMIP_PLL_CFG2);
1073 /* Program UTMIP PLL stable and active counts */
1074 /* [FIXME] arclk_rst.h says WRONG! This should be 1ms -> 0x50 Check! */
1075 reg &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
1076 reg |= UTMIP_PLL_CFG2_STABLE_COUNT(
1077 utmi_parameters[i].stable_count);
1079 reg &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
1081 reg |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(
1082 utmi_parameters[i].active_delay_count);
1084 /* Remove power downs from UTMIP PLL control bits */
1085 reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
1086 reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
1087 reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
1089 clk_writel(reg, UTMIP_PLL_CFG2);
1091 /* Program UTMIP PLL delay and oscillator frequency counts */
1092 reg = clk_readl(UTMIP_PLL_CFG1);
1093 reg &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
1095 reg |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(
1096 utmi_parameters[i].enable_delay_count);
1098 reg &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
1099 reg |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(
1100 utmi_parameters[i].xtal_freq_count);
1102 /* Remove power downs from UTMIP PLL control bits */
1103 reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
1104 reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
1105 reg &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
1107 clk_writel(reg, UTMIP_PLL_CFG1);
1111 static int tegra30_pll_clk_wait_for_lock(struct clk_tegra *c, u32 lock_reg,
1116 #if USE_PLL_LOCK_BITS
1118 for (i = 0; i < c->u.pll.lock_delay; i++) {
1119 if (clk_readl(lock_reg) & lock_bit) {
1120 udelay(PLL_POST_LOCK_DELAY);
1123 udelay(2); /* timeout = 2 * lock time */
1125 pr_err("Timed out waiting for lock bit on pll %s",
1126 __clk_get_name(hw->clk));
1129 udelay(c->u.pll.lock_delay);
1134 static int tegra30_pll_clk_is_enabled(struct clk_hw *hw)
1136 struct clk_tegra *c = to_clk_tegra(hw);
1137 u32 val = clk_readl(c->reg + PLL_BASE);
1139 c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
1143 static void tegra30_pll_clk_init(struct clk_hw *hw)
1145 struct clk_tegra *c = to_clk_tegra(hw);
1147 if (c->flags & PLLU)
1148 tegra30_utmi_param_configure(hw);
1151 static int tegra30_pll_clk_enable(struct clk_hw *hw)
1153 struct clk_tegra *c = to_clk_tegra(hw);
1155 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
1157 #if USE_PLL_LOCK_BITS
1158 val = clk_readl(c->reg + PLL_MISC(c));
1159 val |= PLL_MISC_LOCK_ENABLE(c);
1160 clk_writel(val, c->reg + PLL_MISC(c));
1162 val = clk_readl(c->reg + PLL_BASE);
1163 val &= ~PLL_BASE_BYPASS;
1164 val |= PLL_BASE_ENABLE;
1165 clk_writel(val, c->reg + PLL_BASE);
1167 if (c->flags & PLLM) {
1168 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
1169 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
1170 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
1173 tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
1178 static void tegra30_pll_clk_disable(struct clk_hw *hw)
1180 struct clk_tegra *c = to_clk_tegra(hw);
1182 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
1184 val = clk_readl(c->reg);
1185 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
1186 clk_writel(val, c->reg);
1188 if (c->flags & PLLM) {
1189 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
1190 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
1191 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
1195 static int tegra30_pll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1196 unsigned long parent_rate)
1198 struct clk_tegra *c = to_clk_tegra(hw);
1199 u32 val, p_div, old_base;
1200 unsigned long input_rate;
1201 const struct clk_pll_freq_table *sel;
1202 struct clk_pll_freq_table cfg;
1204 if (c->flags & PLL_FIXED) {
1206 if (rate != c->u.pll.fixed_rate) {
1207 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
1208 __func__, __clk_get_name(hw->clk),
1209 c->u.pll.fixed_rate, rate);
1215 if (c->flags & PLLM) {
1216 if (rate != __clk_get_rate(hw->clk)) {
1217 pr_err("%s: Can not change memory %s rate in flight\n",
1218 __func__, __clk_get_name(hw->clk));
1224 input_rate = parent_rate;
1226 /* Check if the target rate is tabulated */
1227 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1228 if (sel->input_rate == input_rate && sel->output_rate == rate) {
1229 if (c->flags & PLLU) {
1230 BUG_ON(sel->p < 1 || sel->p > 2);
1232 p_div = PLLU_BASE_POST_DIV;
1235 for (val = sel->p; val > 1; val >>= 1)
1237 p_div <<= PLL_BASE_DIVP_SHIFT;
1243 /* Configure out-of-table rate */
1244 if (sel->input_rate == 0) {
1245 unsigned long cfreq;
1246 BUG_ON(c->flags & PLLU);
1249 switch (input_rate) {
1252 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
1255 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
1259 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
1262 pr_err("%s: Unexpected reference rate %lu\n",
1263 __func__, input_rate);
1267 /* Raise VCO to guarantee 0.5% accuracy */
1268 for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
1269 cfg.output_rate <<= 1)
1272 cfg.p = 0x1 << p_div;
1273 cfg.m = input_rate / cfreq;
1274 cfg.n = cfg.output_rate / cfreq;
1275 cfg.cpcon = OUT_OF_TABLE_CPCON;
1277 if ((cfg.m > (PLL_BASE_DIVM_MASK >> PLL_BASE_DIVM_SHIFT)) ||
1278 (cfg.n > (PLL_BASE_DIVN_MASK >> PLL_BASE_DIVN_SHIFT)) ||
1279 (p_div > (PLL_BASE_DIVP_MASK >> PLL_BASE_DIVP_SHIFT)) ||
1280 (cfg.output_rate > c->u.pll.vco_max)) {
1281 pr_err("%s: Failed to set %s out-of-table rate %lu\n",
1282 __func__, __clk_get_name(hw->clk), rate);
1285 p_div <<= PLL_BASE_DIVP_SHIFT;
1289 c->div = sel->m * sel->p;
1291 old_base = val = clk_readl(c->reg + PLL_BASE);
1292 val &= ~(PLL_BASE_DIVM_MASK | PLL_BASE_DIVN_MASK |
1293 ((c->flags & PLLU) ? PLLU_BASE_POST_DIV : PLL_BASE_DIVP_MASK));
1294 val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
1295 (sel->n << PLL_BASE_DIVN_SHIFT) | p_div;
1296 if (val == old_base)
1299 if (c->state == ON) {
1300 tegra30_pll_clk_disable(hw);
1301 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
1303 clk_writel(val, c->reg + PLL_BASE);
1305 if (c->flags & PLL_HAS_CPCON) {
1306 val = clk_readl(c->reg + PLL_MISC(c));
1307 val &= ~PLL_MISC_CPCON_MASK;
1308 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
1309 if (c->flags & (PLLU | PLLD)) {
1310 val &= ~PLL_MISC_LFCON_MASK;
1311 if (sel->n >= PLLDU_LFCON_SET_DIVN)
1312 val |= 0x1 << PLL_MISC_LFCON_SHIFT;
1313 } else if (c->flags & (PLLX | PLLM)) {
1314 val &= ~(0x1 << PLL_MISC_DCCON_SHIFT);
1315 if (rate >= (c->u.pll.vco_max >> 1))
1316 val |= 0x1 << PLL_MISC_DCCON_SHIFT;
1318 clk_writel(val, c->reg + PLL_MISC(c));
1322 tegra30_pll_clk_enable(hw);
1324 c->u.pll.fixed_rate = rate;
1329 static long tegra30_pll_round_rate(struct clk_hw *hw, unsigned long rate,
1330 unsigned long *prate)
1332 struct clk_tegra *c = to_clk_tegra(hw);
1333 unsigned long input_rate = *prate;
1334 u64 output_rate = *prate;
1335 const struct clk_pll_freq_table *sel;
1336 struct clk_pll_freq_table cfg;
1342 if (c->flags & PLL_FIXED)
1343 return c->u.pll.fixed_rate;
1345 if (c->flags & PLLM)
1346 return __clk_get_rate(hw->clk);
1349 /* Check if the target rate is tabulated */
1350 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1351 if (sel->input_rate == input_rate && sel->output_rate == rate) {
1352 if (c->flags & PLLU) {
1353 BUG_ON(sel->p < 1 || sel->p > 2);
1355 p_div = PLLU_BASE_POST_DIV;
1358 for (val = sel->p; val > 1; val >>= 1)
1360 p_div <<= PLL_BASE_DIVP_SHIFT;
1366 if (sel->input_rate == 0) {
1367 unsigned long cfreq;
1368 BUG_ON(c->flags & PLLU);
1371 switch (input_rate) {
1374 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
1377 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
1381 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
1384 pr_err("%s: Unexpected reference rate %lu\n",
1385 __func__, input_rate);
1389 /* Raise VCO to guarantee 0.5% accuracy */
1390 for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
1391 cfg.output_rate <<= 1)
1394 cfg.p = 0x1 << p_div;
1395 cfg.m = input_rate / cfreq;
1396 cfg.n = cfg.output_rate / cfreq;
1400 div = sel->m * sel->p;
1403 output_rate += div - 1; /* round up */
1404 do_div(output_rate, div);
1409 static unsigned long tegra30_pll_recalc_rate(struct clk_hw *hw,
1410 unsigned long parent_rate)
1412 struct clk_tegra *c = to_clk_tegra(hw);
1413 u64 rate = parent_rate;
1414 u32 val = clk_readl(c->reg + PLL_BASE);
1416 if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
1417 const struct clk_pll_freq_table *sel;
1418 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1419 if (sel->input_rate == parent_rate &&
1420 sel->output_rate == c->u.pll.fixed_rate) {
1422 c->div = sel->m * sel->p;
1426 pr_err("Clock %s has unknown fixed frequency\n",
1427 __clk_get_name(hw->clk));
1429 } else if (val & PLL_BASE_BYPASS) {
1433 c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
1434 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
1435 if (c->flags & PLLU)
1436 c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
1438 c->div *= (0x1 << ((val & PLL_BASE_DIVP_MASK) >>
1439 PLL_BASE_DIVP_SHIFT));
1442 if (c->mul != 0 && c->div != 0) {
1444 rate += c->div - 1; /* round up */
1445 do_div(rate, c->div);
1451 struct clk_ops tegra30_pll_ops = {
1452 .is_enabled = tegra30_pll_clk_is_enabled,
1453 .init = tegra30_pll_clk_init,
1454 .enable = tegra30_pll_clk_enable,
1455 .disable = tegra30_pll_clk_disable,
1456 .recalc_rate = tegra30_pll_recalc_rate,
1457 .round_rate = tegra30_pll_round_rate,
1458 .set_rate = tegra30_pll_clk_set_rate,
1461 int tegra30_plld_clk_cfg_ex(struct clk_hw *hw,
1462 enum tegra_clk_ex_param p, u32 setting)
1464 struct clk_tegra *c = to_clk_tegra(hw);
1468 case TEGRA_CLK_PLLD_CSI_OUT_ENB:
1469 mask = PLLD_BASE_CSI_CLKENABLE;
1470 reg = c->reg + PLL_BASE;
1472 case TEGRA_CLK_PLLD_DSI_OUT_ENB:
1473 mask = PLLD_MISC_DSI_CLKENABLE;
1474 reg = c->reg + PLL_MISC(c);
1476 case TEGRA_CLK_PLLD_MIPI_MUX_SEL:
1477 if (!(c->flags & PLL_ALT_MISC_REG)) {
1478 mask = PLLD_BASE_DSIB_MUX_MASK;
1479 reg = c->reg + PLL_BASE;
1482 /* fall through - error since PLLD2 does not have MUX_SEL control */
1487 val = clk_readl(reg);
1492 clk_writel(val, reg);
1496 static int tegra30_plle_clk_is_enabled(struct clk_hw *hw)
1498 struct clk_tegra *c = to_clk_tegra(hw);
1501 val = clk_readl(c->reg + PLL_BASE);
1502 c->state = (val & PLLE_BASE_ENABLE) ? ON : OFF;
1506 static void tegra30_plle_clk_disable(struct clk_hw *hw)
1508 struct clk_tegra *c = to_clk_tegra(hw);
1511 val = clk_readl(c->reg + PLL_BASE);
1512 val &= ~(PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
1513 clk_writel(val, c->reg + PLL_BASE);
1516 static void tegra30_plle_training(struct clk_tegra *c)
1520 /* PLLE is already disabled, and setup cleared;
1521 * create falling edge on PLLE IDDQ input */
1522 val = pmc_readl(PMC_SATA_PWRGT);
1523 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
1524 pmc_writel(val, PMC_SATA_PWRGT);
1526 val = pmc_readl(PMC_SATA_PWRGT);
1527 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
1528 pmc_writel(val, PMC_SATA_PWRGT);
1530 val = pmc_readl(PMC_SATA_PWRGT);
1531 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
1532 pmc_writel(val, PMC_SATA_PWRGT);
1535 val = clk_readl(c->reg + PLL_MISC(c));
1536 } while (!(val & PLLE_MISC_READY));
1539 static int tegra30_plle_configure(struct clk_hw *hw, bool force_training)
1541 struct clk_tegra *c = to_clk_tegra(hw);
1542 struct clk *parent = __clk_get_parent(hw->clk);
1543 const struct clk_pll_freq_table *sel;
1546 unsigned long rate = c->u.pll.fixed_rate;
1547 unsigned long input_rate = __clk_get_rate(parent);
1549 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1550 if (sel->input_rate == input_rate && sel->output_rate == rate)
1554 if (sel->input_rate == 0)
1557 /* disable PLLE, clear setup fiels */
1558 tegra30_plle_clk_disable(hw);
1560 val = clk_readl(c->reg + PLL_MISC(c));
1561 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
1562 clk_writel(val, c->reg + PLL_MISC(c));
1565 val = clk_readl(c->reg + PLL_MISC(c));
1566 if (force_training || (!(val & PLLE_MISC_READY)))
1567 tegra30_plle_training(c);
1569 /* configure dividers, setup, disable SS */
1570 val = clk_readl(c->reg + PLL_BASE);
1571 val &= ~PLLE_BASE_DIV_MASK;
1572 val |= PLLE_BASE_DIV(sel->m, sel->n, sel->p, sel->cpcon);
1573 clk_writel(val, c->reg + PLL_BASE);
1575 c->div = sel->m * sel->p;
1577 val = clk_readl(c->reg + PLL_MISC(c));
1578 val |= PLLE_MISC_SETUP_VALUE;
1579 val |= PLLE_MISC_LOCK_ENABLE;
1580 clk_writel(val, c->reg + PLL_MISC(c));
1582 val = clk_readl(PLLE_SS_CTRL);
1583 val |= PLLE_SS_DISABLE;
1584 clk_writel(val, PLLE_SS_CTRL);
1586 /* enable and lock PLLE*/
1587 val = clk_readl(c->reg + PLL_BASE);
1588 val |= (PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
1589 clk_writel(val, c->reg + PLL_BASE);
1591 tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_MISC(c), PLLE_MISC_LOCK);
1596 static int tegra30_plle_clk_enable(struct clk_hw *hw)
1598 struct clk_tegra *c = to_clk_tegra(hw);
1600 return tegra30_plle_configure(hw, !c->set);
1603 static unsigned long tegra30_plle_clk_recalc_rate(struct clk_hw *hw,
1604 unsigned long parent_rate)
1606 struct clk_tegra *c = to_clk_tegra(hw);
1607 unsigned long rate = parent_rate;
1610 val = clk_readl(c->reg + PLL_BASE);
1611 c->mul = (val & PLLE_BASE_DIVN_MASK) >> PLLE_BASE_DIVN_SHIFT;
1612 c->div = (val & PLLE_BASE_DIVM_MASK) >> PLLE_BASE_DIVM_SHIFT;
1613 c->div *= (val & PLLE_BASE_DIVP_MASK) >> PLLE_BASE_DIVP_SHIFT;
1615 if (c->mul != 0 && c->div != 0) {
1617 rate += c->div - 1; /* round up */
1618 do_div(rate, c->div);
1623 struct clk_ops tegra30_plle_ops = {
1624 .is_enabled = tegra30_plle_clk_is_enabled,
1625 .enable = tegra30_plle_clk_enable,
1626 .disable = tegra30_plle_clk_disable,
1627 .recalc_rate = tegra30_plle_clk_recalc_rate,
1630 /* Clock divider ops */
1631 static int tegra30_pll_div_clk_is_enabled(struct clk_hw *hw)
1633 struct clk_tegra *c = to_clk_tegra(hw);
1635 if (c->flags & DIV_U71) {
1636 u32 val = clk_readl(c->reg);
1637 val >>= c->reg_shift;
1638 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
1639 if (!(val & PLL_OUT_RESET_DISABLE))
1647 static int tegra30_pll_div_clk_enable(struct clk_hw *hw)
1649 struct clk_tegra *c = to_clk_tegra(hw);
1653 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
1654 if (c->flags & DIV_U71) {
1655 val = clk_readl(c->reg);
1656 new_val = val >> c->reg_shift;
1659 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
1661 val &= ~(0xFFFF << c->reg_shift);
1662 val |= new_val << c->reg_shift;
1663 clk_writel_delay(val, c->reg);
1665 } else if (c->flags & DIV_2) {
1671 static void tegra30_pll_div_clk_disable(struct clk_hw *hw)
1673 struct clk_tegra *c = to_clk_tegra(hw);
1677 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
1678 if (c->flags & DIV_U71) {
1679 val = clk_readl(c->reg);
1680 new_val = val >> c->reg_shift;
1683 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
1685 val &= ~(0xFFFF << c->reg_shift);
1686 val |= new_val << c->reg_shift;
1687 clk_writel_delay(val, c->reg);
1691 static int tegra30_pll_div_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1692 unsigned long parent_rate)
1694 struct clk_tegra *c = to_clk_tegra(hw);
1699 if (c->flags & DIV_U71) {
1700 divider_u71 = clk_div71_get_divider(
1701 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1702 if (divider_u71 >= 0) {
1703 val = clk_readl(c->reg);
1704 new_val = val >> c->reg_shift;
1706 if (c->flags & DIV_U71_FIXED)
1707 new_val |= PLL_OUT_OVERRIDE;
1708 new_val &= ~PLL_OUT_RATIO_MASK;
1709 new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
1711 val &= ~(0xFFFF << c->reg_shift);
1712 val |= new_val << c->reg_shift;
1713 clk_writel_delay(val, c->reg);
1714 c->div = divider_u71 + 2;
1716 c->fixed_rate = rate;
1719 } else if (c->flags & DIV_2) {
1720 c->fixed_rate = rate;
1727 static unsigned long tegra30_pll_div_clk_recalc_rate(struct clk_hw *hw,
1728 unsigned long parent_rate)
1730 struct clk_tegra *c = to_clk_tegra(hw);
1731 u64 rate = parent_rate;
1733 if (c->flags & DIV_U71) {
1735 u32 val = clk_readl(c->reg);
1736 val >>= c->reg_shift;
1738 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
1739 c->div = (divu71 + 2);
1741 } else if (c->flags & DIV_2) {
1742 if (c->flags & (PLLD | PLLX)) {
1751 if (c->mul != 0 && c->div != 0) {
1753 rate += c->div - 1; /* round up */
1754 do_div(rate, c->div);
1760 static long tegra30_pll_div_clk_round_rate(struct clk_hw *hw,
1761 unsigned long rate, unsigned long *prate)
1763 struct clk_tegra *c = to_clk_tegra(hw);
1764 unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
1768 parent_rate = *prate;
1770 if (c->flags & DIV_U71) {
1771 divider = clk_div71_get_divider(
1772 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1775 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1776 } else if (c->flags & DIV_2) {
1784 struct clk_ops tegra30_pll_div_ops = {
1785 .is_enabled = tegra30_pll_div_clk_is_enabled,
1786 .enable = tegra30_pll_div_clk_enable,
1787 .disable = tegra30_pll_div_clk_disable,
1788 .set_rate = tegra30_pll_div_clk_set_rate,
1789 .recalc_rate = tegra30_pll_div_clk_recalc_rate,
1790 .round_rate = tegra30_pll_div_clk_round_rate,
1793 /* Periph clk ops */
1794 static inline u32 periph_clk_source_mask(struct clk_tegra *c)
1796 if (c->flags & MUX8)
1798 else if (c->flags & MUX_PWM)
1800 else if (c->flags & MUX_CLK_OUT)
1801 return 3 << (c->u.periph.clk_num + 4);
1802 else if (c->flags & PLLD)
1803 return PLLD_BASE_DSIB_MUX_MASK;
1808 static inline u32 periph_clk_source_shift(struct clk_tegra *c)
1810 if (c->flags & MUX8)
1812 else if (c->flags & MUX_PWM)
1814 else if (c->flags & MUX_CLK_OUT)
1815 return c->u.periph.clk_num + 4;
1816 else if (c->flags & PLLD)
1817 return PLLD_BASE_DSIB_MUX_SHIFT;
1822 static int tegra30_periph_clk_is_enabled(struct clk_hw *hw)
1824 struct clk_tegra *c = to_clk_tegra(hw);
1827 if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
1829 if (!(c->flags & PERIPH_NO_RESET))
1830 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
1835 static int tegra30_periph_clk_enable(struct clk_hw *hw)
1837 struct clk_tegra *c = to_clk_tegra(hw);
1839 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
1840 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
1843 clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_SET_REG(c));
1844 if (!(c->flags & PERIPH_NO_RESET) &&
1845 !(c->flags & PERIPH_MANUAL_RESET)) {
1846 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) &
1847 PERIPH_CLK_TO_BIT(c)) {
1848 udelay(5); /* reset propagation delay */
1849 clk_writel(PERIPH_CLK_TO_BIT(c),
1850 PERIPH_CLK_TO_RST_CLR_REG(c));
1856 static void tegra30_periph_clk_disable(struct clk_hw *hw)
1858 struct clk_tegra *c = to_clk_tegra(hw);
1861 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
1863 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 0)
1866 /* If peripheral is in the APB bus then read the APB bus to
1867 * flush the write operation in apb bus. This will avoid the
1868 * peripheral access after disabling clock*/
1869 if (c->flags & PERIPH_ON_APB)
1870 val = chipid_readl();
1872 clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_CLR_REG(c));
1875 void tegra30_periph_clk_reset(struct clk_hw *hw, bool assert)
1877 struct clk_tegra *c = to_clk_tegra(hw);
1880 if (!(c->flags & PERIPH_NO_RESET)) {
1882 /* If peripheral is in the APB bus then read the APB
1883 * bus to flush the write operation in apb bus. This
1884 * will avoid the peripheral access after disabling
1886 if (c->flags & PERIPH_ON_APB)
1887 val = chipid_readl();
1889 clk_writel(PERIPH_CLK_TO_BIT(c),
1890 PERIPH_CLK_TO_RST_SET_REG(c));
1892 clk_writel(PERIPH_CLK_TO_BIT(c),
1893 PERIPH_CLK_TO_RST_CLR_REG(c));
1897 static int tegra30_periph_clk_set_parent(struct clk_hw *hw, u8 index)
1899 struct clk_tegra *c = to_clk_tegra(hw);
1902 if (!(c->flags & MUX))
1903 return (index == 0) ? 0 : (-EINVAL);
1905 val = clk_readl(c->reg);
1906 val &= ~periph_clk_source_mask(c);
1907 val |= (index << periph_clk_source_shift(c));
1908 clk_writel_delay(val, c->reg);
1912 static u8 tegra30_periph_clk_get_parent(struct clk_hw *hw)
1914 struct clk_tegra *c = to_clk_tegra(hw);
1915 u32 val = clk_readl(c->reg);
1916 int source = (val & periph_clk_source_mask(c)) >>
1917 periph_clk_source_shift(c);
1919 if (!(c->flags & MUX))
1925 static int tegra30_periph_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1926 unsigned long parent_rate)
1928 struct clk_tegra *c = to_clk_tegra(hw);
1932 if (c->flags & DIV_U71) {
1933 divider = clk_div71_get_divider(
1934 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1936 val = clk_readl(c->reg);
1937 val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
1939 if (c->flags & DIV_U71_UART) {
1941 val |= PERIPH_CLK_UART_DIV_ENB;
1943 val &= ~PERIPH_CLK_UART_DIV_ENB;
1945 clk_writel_delay(val, c->reg);
1946 c->div = divider + 2;
1950 } else if (c->flags & DIV_U16) {
1951 divider = clk_div16_get_divider(parent_rate, rate);
1953 val = clk_readl(c->reg);
1954 val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
1956 clk_writel_delay(val, c->reg);
1957 c->div = divider + 1;
1961 } else if (parent_rate <= rate) {
1969 static long tegra30_periph_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1970 unsigned long *prate)
1972 struct clk_tegra *c = to_clk_tegra(hw);
1973 unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
1977 parent_rate = *prate;
1979 if (c->flags & DIV_U71) {
1980 divider = clk_div71_get_divider(
1981 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1985 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1986 } else if (c->flags & DIV_U16) {
1987 divider = clk_div16_get_divider(parent_rate, rate);
1990 return DIV_ROUND_UP(parent_rate, divider + 1);
1995 static unsigned long tegra30_periph_clk_recalc_rate(struct clk_hw *hw,
1996 unsigned long parent_rate)
1998 struct clk_tegra *c = to_clk_tegra(hw);
1999 u64 rate = parent_rate;
2000 u32 val = clk_readl(c->reg);
2002 if (c->flags & DIV_U71) {
2003 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
2004 if ((c->flags & DIV_U71_UART) &&
2005 (!(val & PERIPH_CLK_UART_DIV_ENB))) {
2008 if (c->flags & DIV_U71_IDLE) {
2009 val &= ~(PERIPH_CLK_SOURCE_DIVU71_MASK <<
2010 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
2011 val |= (PERIPH_CLK_SOURCE_DIVIDLE_VAL <<
2012 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
2013 clk_writel(val, c->reg);
2015 c->div = divu71 + 2;
2017 } else if (c->flags & DIV_U16) {
2018 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
2019 c->div = divu16 + 1;
2026 if (c->mul != 0 && c->div != 0) {
2028 rate += c->div - 1; /* round up */
2029 do_div(rate, c->div);
2034 struct clk_ops tegra30_periph_clk_ops = {
2035 .is_enabled = tegra30_periph_clk_is_enabled,
2036 .enable = tegra30_periph_clk_enable,
2037 .disable = tegra30_periph_clk_disable,
2038 .set_parent = tegra30_periph_clk_set_parent,
2039 .get_parent = tegra30_periph_clk_get_parent,
2040 .set_rate = tegra30_periph_clk_set_rate,
2041 .round_rate = tegra30_periph_clk_round_rate,
2042 .recalc_rate = tegra30_periph_clk_recalc_rate,
2045 static int tegra30_dsib_clk_set_parent(struct clk_hw *hw, u8 index)
2047 struct clk *d = clk_get_sys(NULL, "pll_d");
2048 /* The DSIB parent selection bit is in PLLD base
2049 register - can not do direct r-m-w, must be
2050 protected by PLLD lock */
2052 d, TEGRA_CLK_PLLD_MIPI_MUX_SEL, index);
2057 struct clk_ops tegra30_dsib_clk_ops = {
2058 .is_enabled = tegra30_periph_clk_is_enabled,
2059 .enable = &tegra30_periph_clk_enable,
2060 .disable = &tegra30_periph_clk_disable,
2061 .set_parent = &tegra30_dsib_clk_set_parent,
2062 .get_parent = &tegra30_periph_clk_get_parent,
2063 .set_rate = &tegra30_periph_clk_set_rate,
2064 .round_rate = &tegra30_periph_clk_round_rate,
2065 .recalc_rate = &tegra30_periph_clk_recalc_rate,
2068 /* Periph extended clock configuration ops */
2069 int tegra30_vi_clk_cfg_ex(struct clk_hw *hw,
2070 enum tegra_clk_ex_param p, u32 setting)
2072 struct clk_tegra *c = to_clk_tegra(hw);
2074 if (p == TEGRA_CLK_VI_INP_SEL) {
2075 u32 val = clk_readl(c->reg);
2076 val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
2077 val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
2078 PERIPH_CLK_VI_SEL_EX_MASK;
2079 clk_writel(val, c->reg);
2085 int tegra30_nand_clk_cfg_ex(struct clk_hw *hw,
2086 enum tegra_clk_ex_param p, u32 setting)
2088 struct clk_tegra *c = to_clk_tegra(hw);
2090 if (p == TEGRA_CLK_NAND_PAD_DIV2_ENB) {
2091 u32 val = clk_readl(c->reg);
2093 val |= PERIPH_CLK_NAND_DIV_EX_ENB;
2095 val &= ~PERIPH_CLK_NAND_DIV_EX_ENB;
2096 clk_writel(val, c->reg);
2102 int tegra30_dtv_clk_cfg_ex(struct clk_hw *hw,
2103 enum tegra_clk_ex_param p, u32 setting)
2105 struct clk_tegra *c = to_clk_tegra(hw);
2107 if (p == TEGRA_CLK_DTV_INVERT) {
2108 u32 val = clk_readl(c->reg);
2110 val |= PERIPH_CLK_DTV_POLARITY_INV;
2112 val &= ~PERIPH_CLK_DTV_POLARITY_INV;
2113 clk_writel(val, c->reg);
2119 /* Output clock ops */
2121 static DEFINE_SPINLOCK(clk_out_lock);
2123 static int tegra30_clk_out_is_enabled(struct clk_hw *hw)
2125 struct clk_tegra *c = to_clk_tegra(hw);
2126 u32 val = pmc_readl(c->reg);
2128 c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
2134 static int tegra30_clk_out_enable(struct clk_hw *hw)
2136 struct clk_tegra *c = to_clk_tegra(hw);
2138 unsigned long flags;
2140 spin_lock_irqsave(&clk_out_lock, flags);
2141 val = pmc_readl(c->reg);
2142 val |= (0x1 << c->u.periph.clk_num);
2143 pmc_writel(val, c->reg);
2144 spin_unlock_irqrestore(&clk_out_lock, flags);
2149 static void tegra30_clk_out_disable(struct clk_hw *hw)
2151 struct clk_tegra *c = to_clk_tegra(hw);
2153 unsigned long flags;
2155 spin_lock_irqsave(&clk_out_lock, flags);
2156 val = pmc_readl(c->reg);
2157 val &= ~(0x1 << c->u.periph.clk_num);
2158 pmc_writel(val, c->reg);
2159 spin_unlock_irqrestore(&clk_out_lock, flags);
2162 static int tegra30_clk_out_set_parent(struct clk_hw *hw, u8 index)
2164 struct clk_tegra *c = to_clk_tegra(hw);
2166 unsigned long flags;
2168 spin_lock_irqsave(&clk_out_lock, flags);
2169 val = pmc_readl(c->reg);
2170 val &= ~periph_clk_source_mask(c);
2171 val |= (index << periph_clk_source_shift(c));
2172 pmc_writel(val, c->reg);
2173 spin_unlock_irqrestore(&clk_out_lock, flags);
2178 static u8 tegra30_clk_out_get_parent(struct clk_hw *hw)
2180 struct clk_tegra *c = to_clk_tegra(hw);
2181 u32 val = pmc_readl(c->reg);
2184 source = (val & periph_clk_source_mask(c)) >>
2185 periph_clk_source_shift(c);
2189 struct clk_ops tegra_clk_out_ops = {
2190 .is_enabled = tegra30_clk_out_is_enabled,
2191 .enable = tegra30_clk_out_enable,
2192 .disable = tegra30_clk_out_disable,
2193 .set_parent = tegra30_clk_out_set_parent,
2194 .get_parent = tegra30_clk_out_get_parent,
2195 .recalc_rate = tegra30_clk_fixed_recalc_rate,
2198 /* Clock doubler ops */
2199 static int tegra30_clk_double_is_enabled(struct clk_hw *hw)
2201 struct clk_tegra *c = to_clk_tegra(hw);
2204 if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
2209 static int tegra30_clk_double_set_rate(struct clk_hw *hw, unsigned long rate,
2210 unsigned long parent_rate)
2212 struct clk_tegra *c = to_clk_tegra(hw);
2215 if (rate == parent_rate) {
2216 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
2217 clk_writel(val, c->reg);
2221 } else if (rate == 2 * parent_rate) {
2222 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
2223 clk_writel(val, c->reg);
2231 static unsigned long tegra30_clk_double_recalc_rate(struct clk_hw *hw,
2232 unsigned long parent_rate)
2234 struct clk_tegra *c = to_clk_tegra(hw);
2235 u64 rate = parent_rate;
2237 u32 val = clk_readl(c->reg);
2238 c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
2241 if (c->mul != 0 && c->div != 0) {
2243 rate += c->div - 1; /* round up */
2244 do_div(rate, c->div);
2250 static long tegra30_clk_double_round_rate(struct clk_hw *hw, unsigned long rate,
2251 unsigned long *prate)
2253 unsigned long output_rate = *prate;
2255 do_div(output_rate, 2);
2259 struct clk_ops tegra30_clk_double_ops = {
2260 .is_enabled = tegra30_clk_double_is_enabled,
2261 .enable = tegra30_periph_clk_enable,
2262 .disable = tegra30_periph_clk_disable,
2263 .recalc_rate = tegra30_clk_double_recalc_rate,
2264 .round_rate = tegra30_clk_double_round_rate,
2265 .set_rate = tegra30_clk_double_set_rate,
2268 /* Audio sync clock ops */
2269 struct clk_ops tegra_sync_source_ops = {
2270 .recalc_rate = tegra30_clk_fixed_recalc_rate,
2273 static int tegra30_audio_sync_clk_is_enabled(struct clk_hw *hw)
2275 struct clk_tegra *c = to_clk_tegra(hw);
2276 u32 val = clk_readl(c->reg);
2277 c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
2281 static int tegra30_audio_sync_clk_enable(struct clk_hw *hw)
2283 struct clk_tegra *c = to_clk_tegra(hw);
2284 u32 val = clk_readl(c->reg);
2285 clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
2289 static void tegra30_audio_sync_clk_disable(struct clk_hw *hw)
2291 struct clk_tegra *c = to_clk_tegra(hw);
2292 u32 val = clk_readl(c->reg);
2293 clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
2296 static int tegra30_audio_sync_clk_set_parent(struct clk_hw *hw, u8 index)
2298 struct clk_tegra *c = to_clk_tegra(hw);
2301 val = clk_readl(c->reg);
2302 val &= ~AUDIO_SYNC_SOURCE_MASK;
2305 clk_writel(val, c->reg);
2309 static u8 tegra30_audio_sync_clk_get_parent(struct clk_hw *hw)
2311 struct clk_tegra *c = to_clk_tegra(hw);
2312 u32 val = clk_readl(c->reg);
2315 source = val & AUDIO_SYNC_SOURCE_MASK;
2319 struct clk_ops tegra30_audio_sync_clk_ops = {
2320 .is_enabled = tegra30_audio_sync_clk_is_enabled,
2321 .enable = tegra30_audio_sync_clk_enable,
2322 .disable = tegra30_audio_sync_clk_disable,
2323 .set_parent = tegra30_audio_sync_clk_set_parent,
2324 .get_parent = tegra30_audio_sync_clk_get_parent,
2325 .recalc_rate = tegra30_clk_fixed_recalc_rate,
2328 /* cml0 (pcie), and cml1 (sata) clock ops */
2329 static int tegra30_cml_clk_is_enabled(struct clk_hw *hw)
2331 struct clk_tegra *c = to_clk_tegra(hw);
2332 u32 val = clk_readl(c->reg);
2333 c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
2337 static int tegra30_cml_clk_enable(struct clk_hw *hw)
2339 struct clk_tegra *c = to_clk_tegra(hw);
2341 u32 val = clk_readl(c->reg);
2342 val |= (0x1 << c->u.periph.clk_num);
2343 clk_writel(val, c->reg);
2348 static void tegra30_cml_clk_disable(struct clk_hw *hw)
2350 struct clk_tegra *c = to_clk_tegra(hw);
2352 u32 val = clk_readl(c->reg);
2353 val &= ~(0x1 << c->u.periph.clk_num);
2354 clk_writel(val, c->reg);
2357 struct clk_ops tegra_cml_clk_ops = {
2358 .is_enabled = tegra30_cml_clk_is_enabled,
2359 .enable = tegra30_cml_clk_enable,
2360 .disable = tegra30_cml_clk_disable,
2361 .recalc_rate = tegra30_clk_fixed_recalc_rate,
2364 struct clk_ops tegra_pciex_clk_ops = {
2365 .recalc_rate = tegra30_clk_fixed_recalc_rate,
2368 /* Tegra30 CPU clock and reset control functions */
2369 static void tegra30_wait_cpu_in_reset(u32 cpu)
2374 reg = readl(reg_clk_base +
2375 TEGRA30_CLK_RST_CONTROLLER_CPU_CMPLX_STATUS);
2377 } while (!(reg & (1 << cpu))); /* check CPU been reset or not */
2382 static void tegra30_put_cpu_in_reset(u32 cpu)
2384 writel(CPU_RESET(cpu),
2385 reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
2389 static void tegra30_cpu_out_of_reset(u32 cpu)
2391 writel(CPU_RESET(cpu),
2392 reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR);
2396 static void tegra30_enable_cpu_clock(u32 cpu)
2400 writel(CPU_CLOCK(cpu),
2401 reg_clk_base + TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR);
2402 reg = readl(reg_clk_base +
2403 TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR);
2406 static void tegra30_disable_cpu_clock(u32 cpu)
2411 reg = readl(reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
2412 writel(reg | CPU_CLOCK(cpu),
2413 reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
2416 #ifdef CONFIG_PM_SLEEP
2417 static bool tegra30_cpu_rail_off_ready(void)
2419 unsigned int cpu_rst_status;
2422 cpu_rst_status = readl(reg_clk_base +
2423 TEGRA30_CLK_RST_CONTROLLER_CPU_CMPLX_STATUS);
2424 cpu_pwr_status = tegra_powergate_is_powered(TEGRA_POWERGATE_CPU1) ||
2425 tegra_powergate_is_powered(TEGRA_POWERGATE_CPU2) ||
2426 tegra_powergate_is_powered(TEGRA_POWERGATE_CPU3);
2428 if (((cpu_rst_status & 0xE) != 0xE) || cpu_pwr_status)
2434 static void tegra30_cpu_clock_suspend(void)
2436 /* switch coresite to clk_m, save off original source */
2437 tegra30_cpu_clk_sctx.clk_csite_src =
2438 readl(reg_clk_base + CLK_RESET_SOURCE_CSITE);
2439 writel(3<<30, reg_clk_base + CLK_RESET_SOURCE_CSITE);
2441 tegra30_cpu_clk_sctx.cpu_burst =
2442 readl(reg_clk_base + CLK_RESET_CCLK_BURST);
2443 tegra30_cpu_clk_sctx.pllx_base =
2444 readl(reg_clk_base + CLK_RESET_PLLX_BASE);
2445 tegra30_cpu_clk_sctx.pllx_misc =
2446 readl(reg_clk_base + CLK_RESET_PLLX_MISC);
2447 tegra30_cpu_clk_sctx.cclk_divider =
2448 readl(reg_clk_base + CLK_RESET_CCLK_DIVIDER);
2451 static void tegra30_cpu_clock_resume(void)
2453 unsigned int reg, policy;
2455 /* Is CPU complex already running on PLLX? */
2456 reg = readl(reg_clk_base + CLK_RESET_CCLK_BURST);
2457 policy = (reg >> CLK_RESET_CCLK_BURST_POLICY_SHIFT) & 0xF;
2459 if (policy == CLK_RESET_CCLK_IDLE_POLICY)
2460 reg = (reg >> CLK_RESET_CCLK_IDLE_POLICY_SHIFT) & 0xF;
2461 else if (policy == CLK_RESET_CCLK_RUN_POLICY)
2462 reg = (reg >> CLK_RESET_CCLK_RUN_POLICY_SHIFT) & 0xF;
2466 if (reg != CLK_RESET_CCLK_BURST_POLICY_PLLX) {
2467 /* restore PLLX settings if CPU is on different PLL */
2468 writel(tegra30_cpu_clk_sctx.pllx_misc,
2469 reg_clk_base + CLK_RESET_PLLX_MISC);
2470 writel(tegra30_cpu_clk_sctx.pllx_base,
2471 reg_clk_base + CLK_RESET_PLLX_BASE);
2473 /* wait for PLL stabilization if PLLX was enabled */
2474 if (tegra30_cpu_clk_sctx.pllx_base & (1 << 30))
2479 * Restore original burst policy setting for calls resulting from CPU
2480 * LP2 in idle or system suspend.
2482 writel(tegra30_cpu_clk_sctx.cclk_divider,
2483 reg_clk_base + CLK_RESET_CCLK_DIVIDER);
2484 writel(tegra30_cpu_clk_sctx.cpu_burst,
2485 reg_clk_base + CLK_RESET_CCLK_BURST);
2487 writel(tegra30_cpu_clk_sctx.clk_csite_src,
2488 reg_clk_base + CLK_RESET_SOURCE_CSITE);
2492 static struct tegra_cpu_car_ops tegra30_cpu_car_ops = {
2493 .wait_for_reset = tegra30_wait_cpu_in_reset,
2494 .put_in_reset = tegra30_put_cpu_in_reset,
2495 .out_of_reset = tegra30_cpu_out_of_reset,
2496 .enable_clock = tegra30_enable_cpu_clock,
2497 .disable_clock = tegra30_disable_cpu_clock,
2498 #ifdef CONFIG_PM_SLEEP
2499 .rail_off_ready = tegra30_cpu_rail_off_ready,
2500 .suspend = tegra30_cpu_clock_suspend,
2501 .resume = tegra30_cpu_clock_resume,
2505 void __init tegra30_cpu_car_ops_init(void)
2507 tegra_cpu_car_ops = &tegra30_cpu_car_ops;