ARM: tegra30: Separate out clk ops and clk data
[deliverable/linux.git] / arch / arm / mach-tegra / tegra30_clocks.c
1 /*
2 * arch/arm/mach-tegra/tegra30_clocks.c
3 *
4 * Copyright (c) 2010-2012 NVIDIA CORPORATION. All rights reserved.
5 *
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.
9 *
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
13 * more details.
14 *
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.
18 *
19 */
20
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>
27 #include <linux/io.h>
28 #include <linux/clk.h>
29 #include <linux/cpufreq.h>
30 #include <linux/syscore_ops.h>
31
32 #include <asm/clkdev.h>
33
34 #include <mach/iomap.h>
35
36 #include "clock.h"
37 #include "fuse.h"
38
39 #define USE_PLL_LOCK_BITS 0
40
41 #define RST_DEVICES_L 0x004
42 #define RST_DEVICES_H 0x008
43 #define RST_DEVICES_U 0x00C
44 #define RST_DEVICES_V 0x358
45 #define RST_DEVICES_W 0x35C
46 #define RST_DEVICES_SET_L 0x300
47 #define RST_DEVICES_CLR_L 0x304
48 #define RST_DEVICES_SET_V 0x430
49 #define RST_DEVICES_CLR_V 0x434
50 #define RST_DEVICES_NUM 5
51
52 #define CLK_OUT_ENB_L 0x010
53 #define CLK_OUT_ENB_H 0x014
54 #define CLK_OUT_ENB_U 0x018
55 #define CLK_OUT_ENB_V 0x360
56 #define CLK_OUT_ENB_W 0x364
57 #define CLK_OUT_ENB_SET_L 0x320
58 #define CLK_OUT_ENB_CLR_L 0x324
59 #define CLK_OUT_ENB_SET_V 0x440
60 #define CLK_OUT_ENB_CLR_V 0x444
61 #define CLK_OUT_ENB_NUM 5
62
63 #define RST_DEVICES_V_SWR_CPULP_RST_DIS (0x1 << 1)
64 #define CLK_OUT_ENB_V_CLK_ENB_CPULP_EN (0x1 << 1)
65
66 #define PERIPH_CLK_TO_BIT(c) (1 << (c->u.periph.clk_num % 32))
67 #define PERIPH_CLK_TO_RST_REG(c) \
68 periph_clk_to_reg((c), RST_DEVICES_L, RST_DEVICES_V, 4)
69 #define PERIPH_CLK_TO_RST_SET_REG(c) \
70 periph_clk_to_reg((c), RST_DEVICES_SET_L, RST_DEVICES_SET_V, 8)
71 #define PERIPH_CLK_TO_RST_CLR_REG(c) \
72 periph_clk_to_reg((c), RST_DEVICES_CLR_L, RST_DEVICES_CLR_V, 8)
73
74 #define PERIPH_CLK_TO_ENB_REG(c) \
75 periph_clk_to_reg((c), CLK_OUT_ENB_L, CLK_OUT_ENB_V, 4)
76 #define PERIPH_CLK_TO_ENB_SET_REG(c) \
77 periph_clk_to_reg((c), CLK_OUT_ENB_SET_L, CLK_OUT_ENB_SET_V, 8)
78 #define PERIPH_CLK_TO_ENB_CLR_REG(c) \
79 periph_clk_to_reg((c), CLK_OUT_ENB_CLR_L, CLK_OUT_ENB_CLR_V, 8)
80
81 #define CLK_MASK_ARM 0x44
82 #define MISC_CLK_ENB 0x48
83
84 #define OSC_CTRL 0x50
85 #define OSC_CTRL_OSC_FREQ_MASK (0xF<<28)
86 #define OSC_CTRL_OSC_FREQ_13MHZ (0x0<<28)
87 #define OSC_CTRL_OSC_FREQ_19_2MHZ (0x4<<28)
88 #define OSC_CTRL_OSC_FREQ_12MHZ (0x8<<28)
89 #define OSC_CTRL_OSC_FREQ_26MHZ (0xC<<28)
90 #define OSC_CTRL_OSC_FREQ_16_8MHZ (0x1<<28)
91 #define OSC_CTRL_OSC_FREQ_38_4MHZ (0x5<<28)
92 #define OSC_CTRL_OSC_FREQ_48MHZ (0x9<<28)
93 #define OSC_CTRL_MASK (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
94
95 #define OSC_CTRL_PLL_REF_DIV_MASK (3<<26)
96 #define OSC_CTRL_PLL_REF_DIV_1 (0<<26)
97 #define OSC_CTRL_PLL_REF_DIV_2 (1<<26)
98 #define OSC_CTRL_PLL_REF_DIV_4 (2<<26)
99
100 #define OSC_FREQ_DET 0x58
101 #define OSC_FREQ_DET_TRIG (1<<31)
102
103 #define OSC_FREQ_DET_STATUS 0x5C
104 #define OSC_FREQ_DET_BUSY (1<<31)
105 #define OSC_FREQ_DET_CNT_MASK 0xFFFF
106
107 #define PERIPH_CLK_SOURCE_I2S1 0x100
108 #define PERIPH_CLK_SOURCE_EMC 0x19c
109 #define PERIPH_CLK_SOURCE_OSC 0x1fc
110 #define PERIPH_CLK_SOURCE_NUM1 \
111 ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
112
113 #define PERIPH_CLK_SOURCE_G3D2 0x3b0
114 #define PERIPH_CLK_SOURCE_SE 0x42c
115 #define PERIPH_CLK_SOURCE_NUM2 \
116 ((PERIPH_CLK_SOURCE_SE - PERIPH_CLK_SOURCE_G3D2) / 4 + 1)
117
118 #define AUDIO_DLY_CLK 0x49c
119 #define AUDIO_SYNC_CLK_SPDIF 0x4b4
120 #define PERIPH_CLK_SOURCE_NUM3 \
121 ((AUDIO_SYNC_CLK_SPDIF - AUDIO_DLY_CLK) / 4 + 1)
122
123 #define PERIPH_CLK_SOURCE_NUM (PERIPH_CLK_SOURCE_NUM1 + \
124 PERIPH_CLK_SOURCE_NUM2 + \
125 PERIPH_CLK_SOURCE_NUM3)
126
127 #define CPU_SOFTRST_CTRL 0x380
128
129 #define PERIPH_CLK_SOURCE_DIVU71_MASK 0xFF
130 #define PERIPH_CLK_SOURCE_DIVU16_MASK 0xFFFF
131 #define PERIPH_CLK_SOURCE_DIV_SHIFT 0
132 #define PERIPH_CLK_SOURCE_DIVIDLE_SHIFT 8
133 #define PERIPH_CLK_SOURCE_DIVIDLE_VAL 50
134 #define PERIPH_CLK_UART_DIV_ENB (1<<24)
135 #define PERIPH_CLK_VI_SEL_EX_SHIFT 24
136 #define PERIPH_CLK_VI_SEL_EX_MASK (0x3<<PERIPH_CLK_VI_SEL_EX_SHIFT)
137 #define PERIPH_CLK_NAND_DIV_EX_ENB (1<<8)
138 #define PERIPH_CLK_DTV_POLARITY_INV (1<<25)
139
140 #define AUDIO_SYNC_SOURCE_MASK 0x0F
141 #define AUDIO_SYNC_DISABLE_BIT 0x10
142 #define AUDIO_SYNC_TAP_NIBBLE_SHIFT(c) ((c->reg_shift - 24) * 4)
143
144 #define PLL_BASE 0x0
145 #define PLL_BASE_BYPASS (1<<31)
146 #define PLL_BASE_ENABLE (1<<30)
147 #define PLL_BASE_REF_ENABLE (1<<29)
148 #define PLL_BASE_OVERRIDE (1<<28)
149 #define PLL_BASE_LOCK (1<<27)
150 #define PLL_BASE_DIVP_MASK (0x7<<20)
151 #define PLL_BASE_DIVP_SHIFT 20
152 #define PLL_BASE_DIVN_MASK (0x3FF<<8)
153 #define PLL_BASE_DIVN_SHIFT 8
154 #define PLL_BASE_DIVM_MASK (0x1F)
155 #define PLL_BASE_DIVM_SHIFT 0
156
157 #define PLL_OUT_RATIO_MASK (0xFF<<8)
158 #define PLL_OUT_RATIO_SHIFT 8
159 #define PLL_OUT_OVERRIDE (1<<2)
160 #define PLL_OUT_CLKEN (1<<1)
161 #define PLL_OUT_RESET_DISABLE (1<<0)
162
163 #define PLL_MISC(c) \
164 (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
165 #define PLL_MISC_LOCK_ENABLE(c) \
166 (((c)->flags & (PLLU | PLLD)) ? (1<<22) : (1<<18))
167
168 #define PLL_MISC_DCCON_SHIFT 20
169 #define PLL_MISC_CPCON_SHIFT 8
170 #define PLL_MISC_CPCON_MASK (0xF<<PLL_MISC_CPCON_SHIFT)
171 #define PLL_MISC_LFCON_SHIFT 4
172 #define PLL_MISC_LFCON_MASK (0xF<<PLL_MISC_LFCON_SHIFT)
173 #define PLL_MISC_VCOCON_SHIFT 0
174 #define PLL_MISC_VCOCON_MASK (0xF<<PLL_MISC_VCOCON_SHIFT)
175 #define PLLD_MISC_CLKENABLE (1<<30)
176
177 #define PLLU_BASE_POST_DIV (1<<20)
178
179 #define PLLD_BASE_DSIB_MUX_SHIFT 25
180 #define PLLD_BASE_DSIB_MUX_MASK (1<<PLLD_BASE_DSIB_MUX_SHIFT)
181 #define PLLD_BASE_CSI_CLKENABLE (1<<26)
182 #define PLLD_MISC_DSI_CLKENABLE (1<<30)
183 #define PLLD_MISC_DIV_RST (1<<23)
184 #define PLLD_MISC_DCCON_SHIFT 12
185
186 #define PLLDU_LFCON_SET_DIVN 600
187
188 /* FIXME: OUT_OF_TABLE_CPCON per pll */
189 #define OUT_OF_TABLE_CPCON 0x8
190
191 #define SUPER_CLK_MUX 0x00
192 #define SUPER_STATE_SHIFT 28
193 #define SUPER_STATE_MASK (0xF << SUPER_STATE_SHIFT)
194 #define SUPER_STATE_STANDBY (0x0 << SUPER_STATE_SHIFT)
195 #define SUPER_STATE_IDLE (0x1 << SUPER_STATE_SHIFT)
196 #define SUPER_STATE_RUN (0x2 << SUPER_STATE_SHIFT)
197 #define SUPER_STATE_IRQ (0x3 << SUPER_STATE_SHIFT)
198 #define SUPER_STATE_FIQ (0x4 << SUPER_STATE_SHIFT)
199 #define SUPER_LP_DIV2_BYPASS (0x1 << 16)
200 #define SUPER_SOURCE_MASK 0xF
201 #define SUPER_FIQ_SOURCE_SHIFT 12
202 #define SUPER_IRQ_SOURCE_SHIFT 8
203 #define SUPER_RUN_SOURCE_SHIFT 4
204 #define SUPER_IDLE_SOURCE_SHIFT 0
205
206 #define SUPER_CLK_DIVIDER 0x04
207 #define SUPER_CLOCK_DIV_U71_SHIFT 16
208 #define SUPER_CLOCK_DIV_U71_MASK (0xff << SUPER_CLOCK_DIV_U71_SHIFT)
209 /* guarantees safe cpu backup */
210 #define SUPER_CLOCK_DIV_U71_MIN 0x2
211
212 #define BUS_CLK_DISABLE (1<<3)
213 #define BUS_CLK_DIV_MASK 0x3
214
215 #define PMC_CTRL 0x0
216 #define PMC_CTRL_BLINK_ENB (1 << 7)
217
218 #define PMC_DPD_PADS_ORIDE 0x1c
219 #define PMC_DPD_PADS_ORIDE_BLINK_ENB (1 << 20)
220
221 #define PMC_BLINK_TIMER_DATA_ON_SHIFT 0
222 #define PMC_BLINK_TIMER_DATA_ON_MASK 0x7fff
223 #define PMC_BLINK_TIMER_ENB (1 << 15)
224 #define PMC_BLINK_TIMER_DATA_OFF_SHIFT 16
225 #define PMC_BLINK_TIMER_DATA_OFF_MASK 0xffff
226
227 #define PMC_PLLP_WB0_OVERRIDE 0xf8
228 #define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE (1 << 12)
229
230 #define UTMIP_PLL_CFG2 0x488
231 #define UTMIP_PLL_CFG2_STABLE_COUNT(x) (((x) & 0xfff) << 6)
232 #define UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(x) (((x) & 0x3f) << 18)
233 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN (1 << 0)
234 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN (1 << 2)
235 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN (1 << 4)
236
237 #define UTMIP_PLL_CFG1 0x484
238 #define UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
239 #define UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
240 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN (1 << 14)
241 #define UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN (1 << 12)
242 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN (1 << 16)
243
244 #define PLLE_BASE_CML_ENABLE (1<<31)
245 #define PLLE_BASE_ENABLE (1<<30)
246 #define PLLE_BASE_DIVCML_SHIFT 24
247 #define PLLE_BASE_DIVCML_MASK (0xf<<PLLE_BASE_DIVCML_SHIFT)
248 #define PLLE_BASE_DIVP_SHIFT 16
249 #define PLLE_BASE_DIVP_MASK (0x3f<<PLLE_BASE_DIVP_SHIFT)
250 #define PLLE_BASE_DIVN_SHIFT 8
251 #define PLLE_BASE_DIVN_MASK (0xFF<<PLLE_BASE_DIVN_SHIFT)
252 #define PLLE_BASE_DIVM_SHIFT 0
253 #define PLLE_BASE_DIVM_MASK (0xFF<<PLLE_BASE_DIVM_SHIFT)
254 #define PLLE_BASE_DIV_MASK \
255 (PLLE_BASE_DIVCML_MASK | PLLE_BASE_DIVP_MASK | \
256 PLLE_BASE_DIVN_MASK | PLLE_BASE_DIVM_MASK)
257 #define PLLE_BASE_DIV(m, n, p, cml) \
258 (((cml)<<PLLE_BASE_DIVCML_SHIFT) | ((p)<<PLLE_BASE_DIVP_SHIFT) | \
259 ((n)<<PLLE_BASE_DIVN_SHIFT) | ((m)<<PLLE_BASE_DIVM_SHIFT))
260
261 #define PLLE_MISC_SETUP_BASE_SHIFT 16
262 #define PLLE_MISC_SETUP_BASE_MASK (0xFFFF<<PLLE_MISC_SETUP_BASE_SHIFT)
263 #define PLLE_MISC_READY (1<<15)
264 #define PLLE_MISC_LOCK (1<<11)
265 #define PLLE_MISC_LOCK_ENABLE (1<<9)
266 #define PLLE_MISC_SETUP_EX_SHIFT 2
267 #define PLLE_MISC_SETUP_EX_MASK (0x3<<PLLE_MISC_SETUP_EX_SHIFT)
268 #define PLLE_MISC_SETUP_MASK \
269 (PLLE_MISC_SETUP_BASE_MASK | PLLE_MISC_SETUP_EX_MASK)
270 #define PLLE_MISC_SETUP_VALUE \
271 ((0x7<<PLLE_MISC_SETUP_BASE_SHIFT) | (0x0<<PLLE_MISC_SETUP_EX_SHIFT))
272
273 #define PLLE_SS_CTRL 0x68
274 #define PLLE_SS_INCINTRV_SHIFT 24
275 #define PLLE_SS_INCINTRV_MASK (0x3f<<PLLE_SS_INCINTRV_SHIFT)
276 #define PLLE_SS_INC_SHIFT 16
277 #define PLLE_SS_INC_MASK (0xff<<PLLE_SS_INC_SHIFT)
278 #define PLLE_SS_MAX_SHIFT 0
279 #define PLLE_SS_MAX_MASK (0x1ff<<PLLE_SS_MAX_SHIFT)
280 #define PLLE_SS_COEFFICIENTS_MASK \
281 (PLLE_SS_INCINTRV_MASK | PLLE_SS_INC_MASK | PLLE_SS_MAX_MASK)
282 #define PLLE_SS_COEFFICIENTS_12MHZ \
283 ((0x18<<PLLE_SS_INCINTRV_SHIFT) | (0x1<<PLLE_SS_INC_SHIFT) | \
284 (0x24<<PLLE_SS_MAX_SHIFT))
285 #define PLLE_SS_DISABLE ((1<<12) | (1<<11) | (1<<10))
286
287 #define PLLE_AUX 0x48c
288 #define PLLE_AUX_PLLP_SEL (1<<2)
289 #define PLLE_AUX_CML_SATA_ENABLE (1<<1)
290 #define PLLE_AUX_CML_PCIE_ENABLE (1<<0)
291
292 #define PMC_SATA_PWRGT 0x1ac
293 #define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE (1<<5)
294 #define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL (1<<4)
295
296 #define ROUND_DIVIDER_UP 0
297 #define ROUND_DIVIDER_DOWN 1
298
299 /* FIXME: recommended safety delay after lock is detected */
300 #define PLL_POST_LOCK_DELAY 100
301
302 /**
303 * Structure defining the fields for USB UTMI clocks Parameters.
304 */
305 struct utmi_clk_param {
306 /* Oscillator Frequency in KHz */
307 u32 osc_frequency;
308 /* UTMIP PLL Enable Delay Count */
309 u8 enable_delay_count;
310 /* UTMIP PLL Stable count */
311 u8 stable_count;
312 /* UTMIP PLL Active delay count */
313 u8 active_delay_count;
314 /* UTMIP PLL Xtal frequency count */
315 u8 xtal_freq_count;
316 };
317
318 static const struct utmi_clk_param utmi_parameters[] = {
319 {
320 .osc_frequency = 13000000,
321 .enable_delay_count = 0x02,
322 .stable_count = 0x33,
323 .active_delay_count = 0x05,
324 .xtal_freq_count = 0x7F
325 },
326 {
327 .osc_frequency = 19200000,
328 .enable_delay_count = 0x03,
329 .stable_count = 0x4B,
330 .active_delay_count = 0x06,
331 .xtal_freq_count = 0xBB},
332 {
333 .osc_frequency = 12000000,
334 .enable_delay_count = 0x02,
335 .stable_count = 0x2F,
336 .active_delay_count = 0x04,
337 .xtal_freq_count = 0x76
338 },
339 {
340 .osc_frequency = 26000000,
341 .enable_delay_count = 0x04,
342 .stable_count = 0x66,
343 .active_delay_count = 0x09,
344 .xtal_freq_count = 0xFE
345 },
346 {
347 .osc_frequency = 16800000,
348 .enable_delay_count = 0x03,
349 .stable_count = 0x41,
350 .active_delay_count = 0x0A,
351 .xtal_freq_count = 0xA4
352 },
353 };
354
355 static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
356 static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
357 static void __iomem *misc_gp_hidrev_base = IO_ADDRESS(TEGRA_APB_MISC_BASE);
358
359 #define MISC_GP_HIDREV 0x804
360
361 /*
362 * Some peripheral clocks share an enable bit, so refcount the enable bits
363 * in registers CLK_ENABLE_L, ... CLK_ENABLE_W
364 */
365 static int tegra_periph_clk_enable_refcount[CLK_OUT_ENB_NUM * 32];
366
367 #define clk_writel(value, reg) \
368 __raw_writel(value, (u32)reg_clk_base + (reg))
369 #define clk_readl(reg) \
370 __raw_readl((u32)reg_clk_base + (reg))
371 #define pmc_writel(value, reg) \
372 __raw_writel(value, (u32)reg_pmc_base + (reg))
373 #define pmc_readl(reg) \
374 __raw_readl((u32)reg_pmc_base + (reg))
375 #define chipid_readl() \
376 __raw_readl((u32)misc_gp_hidrev_base + MISC_GP_HIDREV)
377
378 #define clk_writel_delay(value, reg) \
379 do { \
380 __raw_writel((value), (u32)reg_clk_base + (reg)); \
381 udelay(2); \
382 } while (0)
383
384
385 static inline int clk_set_div(struct clk *c, u32 n)
386 {
387 return clk_set_rate(c, (clk_get_rate(c->parent) + n-1) / n);
388 }
389
390 static inline u32 periph_clk_to_reg(
391 struct clk *c, u32 reg_L, u32 reg_V, int offs)
392 {
393 u32 reg = c->u.periph.clk_num / 32;
394 BUG_ON(reg >= RST_DEVICES_NUM);
395 if (reg < 3)
396 reg = reg_L + (reg * offs);
397 else
398 reg = reg_V + ((reg - 3) * offs);
399 return reg;
400 }
401
402 static unsigned long clk_measure_input_freq(void)
403 {
404 u32 clock_autodetect;
405 clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
406 do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
407 clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
408 if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
409 return 12000000;
410 } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
411 return 13000000;
412 } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
413 return 19200000;
414 } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
415 return 26000000;
416 } else if (clock_autodetect >= 1025 - 3 && clock_autodetect <= 1025 + 3) {
417 return 16800000;
418 } else if (clock_autodetect >= 2344 - 3 && clock_autodetect <= 2344 + 3) {
419 return 38400000;
420 } else if (clock_autodetect >= 2928 - 3 && clock_autodetect <= 2928 + 3) {
421 return 48000000;
422 } else {
423 pr_err("%s: Unexpected clock autodetect value %d", __func__,
424 clock_autodetect);
425 BUG();
426 return 0;
427 }
428 }
429
430 static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate,
431 u32 flags, u32 round_mode)
432 {
433 s64 divider_u71 = parent_rate;
434 if (!rate)
435 return -EINVAL;
436
437 if (!(flags & DIV_U71_INT))
438 divider_u71 *= 2;
439 if (round_mode == ROUND_DIVIDER_UP)
440 divider_u71 += rate - 1;
441 do_div(divider_u71, rate);
442 if (flags & DIV_U71_INT)
443 divider_u71 *= 2;
444
445 if (divider_u71 - 2 < 0)
446 return 0;
447
448 if (divider_u71 - 2 > 255)
449 return -EINVAL;
450
451 return divider_u71 - 2;
452 }
453
454 static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
455 {
456 s64 divider_u16;
457
458 divider_u16 = parent_rate;
459 if (!rate)
460 return -EINVAL;
461 divider_u16 += rate - 1;
462 do_div(divider_u16, rate);
463
464 if (divider_u16 - 1 < 0)
465 return 0;
466
467 if (divider_u16 - 1 > 0xFFFF)
468 return -EINVAL;
469
470 return divider_u16 - 1;
471 }
472
473 /* clk_m functions */
474 static unsigned long tegra30_clk_m_autodetect_rate(struct clk *c)
475 {
476 u32 osc_ctrl = clk_readl(OSC_CTRL);
477 u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
478 u32 pll_ref_div = osc_ctrl & OSC_CTRL_PLL_REF_DIV_MASK;
479
480 c->rate = clk_measure_input_freq();
481 switch (c->rate) {
482 case 12000000:
483 auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
484 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
485 break;
486 case 13000000:
487 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
488 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
489 break;
490 case 19200000:
491 auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
492 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
493 break;
494 case 26000000:
495 auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
496 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
497 break;
498 case 16800000:
499 auto_clock_control |= OSC_CTRL_OSC_FREQ_16_8MHZ;
500 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
501 break;
502 case 38400000:
503 auto_clock_control |= OSC_CTRL_OSC_FREQ_38_4MHZ;
504 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_2);
505 break;
506 case 48000000:
507 auto_clock_control |= OSC_CTRL_OSC_FREQ_48MHZ;
508 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_4);
509 break;
510 default:
511 pr_err("%s: Unexpected clock rate %ld", __func__, c->rate);
512 BUG();
513 }
514 clk_writel(auto_clock_control, OSC_CTRL);
515 return c->rate;
516 }
517
518 static void tegra30_clk_m_init(struct clk *c)
519 {
520 pr_debug("%s on clock %s\n", __func__, c->name);
521 tegra30_clk_m_autodetect_rate(c);
522 }
523
524 static int tegra30_clk_m_enable(struct clk *c)
525 {
526 pr_debug("%s on clock %s\n", __func__, c->name);
527 return 0;
528 }
529
530 static void tegra30_clk_m_disable(struct clk *c)
531 {
532 pr_debug("%s on clock %s\n", __func__, c->name);
533 WARN(1, "Attempting to disable main SoC clock\n");
534 }
535
536 struct clk_ops tegra30_clk_m_ops = {
537 .init = tegra30_clk_m_init,
538 .enable = tegra30_clk_m_enable,
539 .disable = tegra30_clk_m_disable,
540 };
541
542 struct clk_ops tegra_clk_m_div_ops = {
543 .enable = tegra30_clk_m_enable,
544 };
545
546 /* PLL reference divider functions */
547 static void tegra30_pll_ref_init(struct clk *c)
548 {
549 u32 pll_ref_div = clk_readl(OSC_CTRL) & OSC_CTRL_PLL_REF_DIV_MASK;
550 pr_debug("%s on clock %s\n", __func__, c->name);
551
552 switch (pll_ref_div) {
553 case OSC_CTRL_PLL_REF_DIV_1:
554 c->div = 1;
555 break;
556 case OSC_CTRL_PLL_REF_DIV_2:
557 c->div = 2;
558 break;
559 case OSC_CTRL_PLL_REF_DIV_4:
560 c->div = 4;
561 break;
562 default:
563 pr_err("%s: Invalid pll ref divider %d", __func__, pll_ref_div);
564 BUG();
565 }
566 c->mul = 1;
567 c->state = ON;
568 }
569
570 struct clk_ops tegra_pll_ref_ops = {
571 .init = tegra30_pll_ref_init,
572 .enable = tegra30_clk_m_enable,
573 .disable = tegra30_clk_m_disable,
574 };
575
576 /* super clock functions */
577 /* "super clocks" on tegra30 have two-stage muxes, fractional 7.1 divider and
578 * clock skipping super divider. We will ignore the clock skipping divider,
579 * since we can't lower the voltage when using the clock skip, but we can if
580 * we lower the PLL frequency. We will use 7.1 divider for CPU super-clock
581 * only when its parent is a fixed rate PLL, since we can't change PLL rate
582 * in this case.
583 */
584 static void tegra30_super_clk_init(struct clk *c)
585 {
586 u32 val;
587 int source;
588 int shift;
589 const struct clk_mux_sel *sel;
590 val = clk_readl(c->reg + SUPER_CLK_MUX);
591 c->state = ON;
592 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
593 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
594 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
595 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
596 source = (val >> shift) & SUPER_SOURCE_MASK;
597 if (c->flags & DIV_2)
598 source |= val & SUPER_LP_DIV2_BYPASS;
599 for (sel = c->inputs; sel->input != NULL; sel++) {
600 if (sel->value == source)
601 break;
602 }
603 BUG_ON(sel->input == NULL);
604 c->parent = sel->input;
605
606 if (c->flags & DIV_U71) {
607 /* Init safe 7.1 divider value (does not affect PLLX path) */
608 clk_writel(SUPER_CLOCK_DIV_U71_MIN << SUPER_CLOCK_DIV_U71_SHIFT,
609 c->reg + SUPER_CLK_DIVIDER);
610 c->mul = 2;
611 c->div = 2;
612 if (!(c->parent->flags & PLLX))
613 c->div += SUPER_CLOCK_DIV_U71_MIN;
614 } else
615 clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
616 }
617
618 static int tegra30_super_clk_enable(struct clk *c)
619 {
620 return 0;
621 }
622
623 static void tegra30_super_clk_disable(struct clk *c)
624 {
625 /* since tegra 3 has 2 CPU super clocks - low power lp-mode clock and
626 geared up g-mode super clock - mode switch may request to disable
627 either of them; accept request with no affect on h/w */
628 }
629
630 static int tegra30_super_clk_set_parent(struct clk *c, struct clk *p)
631 {
632 u32 val;
633 const struct clk_mux_sel *sel;
634 int shift;
635
636 val = clk_readl(c->reg + SUPER_CLK_MUX);
637 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
638 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
639 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
640 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
641 for (sel = c->inputs; sel->input != NULL; sel++) {
642 if (sel->input == p) {
643 /* For LP mode super-clock switch between PLLX direct
644 and divided-by-2 outputs is allowed only when other
645 than PLLX clock source is current parent */
646 if ((c->flags & DIV_2) && (p->flags & PLLX) &&
647 ((sel->value ^ val) & SUPER_LP_DIV2_BYPASS)) {
648 if (c->parent->flags & PLLX)
649 return -EINVAL;
650 val ^= SUPER_LP_DIV2_BYPASS;
651 clk_writel_delay(val, c->reg);
652 }
653 val &= ~(SUPER_SOURCE_MASK << shift);
654 val |= (sel->value & SUPER_SOURCE_MASK) << shift;
655
656 /* 7.1 divider for CPU super-clock does not affect
657 PLLX path */
658 if (c->flags & DIV_U71) {
659 u32 div = 0;
660 if (!(p->flags & PLLX)) {
661 div = clk_readl(c->reg +
662 SUPER_CLK_DIVIDER);
663 div &= SUPER_CLOCK_DIV_U71_MASK;
664 div >>= SUPER_CLOCK_DIV_U71_SHIFT;
665 }
666 c->div = div + 2;
667 c->mul = 2;
668 }
669
670 if (c->refcnt)
671 clk_enable(p);
672
673 clk_writel_delay(val, c->reg);
674
675 if (c->refcnt && c->parent)
676 clk_disable(c->parent);
677
678 clk_reparent(c, p);
679 return 0;
680 }
681 }
682 return -EINVAL;
683 }
684
685 /*
686 * Do not use super clocks "skippers", since dividing using a clock skipper
687 * does not allow the voltage to be scaled down. Instead adjust the rate of
688 * the parent clock. This requires that the parent of a super clock have no
689 * other children, otherwise the rate will change underneath the other
690 * children. Special case: if fixed rate PLL is CPU super clock parent the
691 * rate of this PLL can't be changed, and it has many other children. In
692 * this case use 7.1 fractional divider to adjust the super clock rate.
693 */
694 static int tegra30_super_clk_set_rate(struct clk *c, unsigned long rate)
695 {
696 if ((c->flags & DIV_U71) && (c->parent->flags & PLL_FIXED)) {
697 int div = clk_div71_get_divider(c->parent->u.pll.fixed_rate,
698 rate, c->flags, ROUND_DIVIDER_DOWN);
699 div = max(div, SUPER_CLOCK_DIV_U71_MIN);
700
701 clk_writel(div << SUPER_CLOCK_DIV_U71_SHIFT,
702 c->reg + SUPER_CLK_DIVIDER);
703 c->div = div + 2;
704 c->mul = 2;
705 return 0;
706 }
707 return clk_set_rate(c->parent, rate);
708 }
709
710 struct clk_ops tegra30_super_ops = {
711 .init = tegra30_super_clk_init,
712 .enable = tegra30_super_clk_enable,
713 .disable = tegra30_super_clk_disable,
714 .set_parent = tegra30_super_clk_set_parent,
715 .set_rate = tegra30_super_clk_set_rate,
716 };
717
718 static int tegra30_twd_clk_set_rate(struct clk *c, unsigned long rate)
719 {
720 /* The input value 'rate' is the clock rate of the CPU complex. */
721 c->rate = (rate * c->mul) / c->div;
722 return 0;
723 }
724
725 struct clk_ops tegra30_twd_ops = {
726 .set_rate = tegra30_twd_clk_set_rate,
727 };
728
729 /* Blink output functions */
730
731 static void tegra30_blink_clk_init(struct clk *c)
732 {
733 u32 val;
734
735 val = pmc_readl(PMC_CTRL);
736 c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
737 c->mul = 1;
738 val = pmc_readl(c->reg);
739
740 if (val & PMC_BLINK_TIMER_ENB) {
741 unsigned int on_off;
742
743 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
744 PMC_BLINK_TIMER_DATA_ON_MASK;
745 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
746 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
747 on_off += val;
748 /* each tick in the blink timer is 4 32KHz clocks */
749 c->div = on_off * 4;
750 } else {
751 c->div = 1;
752 }
753 }
754
755 static int tegra30_blink_clk_enable(struct clk *c)
756 {
757 u32 val;
758
759 val = pmc_readl(PMC_DPD_PADS_ORIDE);
760 pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
761
762 val = pmc_readl(PMC_CTRL);
763 pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
764
765 return 0;
766 }
767
768 static void tegra30_blink_clk_disable(struct clk *c)
769 {
770 u32 val;
771
772 val = pmc_readl(PMC_CTRL);
773 pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
774
775 val = pmc_readl(PMC_DPD_PADS_ORIDE);
776 pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
777 }
778
779 static int tegra30_blink_clk_set_rate(struct clk *c, unsigned long rate)
780 {
781 unsigned long parent_rate = clk_get_rate(c->parent);
782 if (rate >= parent_rate) {
783 c->div = 1;
784 pmc_writel(0, c->reg);
785 } else {
786 unsigned int on_off;
787 u32 val;
788
789 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
790 c->div = on_off * 8;
791
792 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
793 PMC_BLINK_TIMER_DATA_ON_SHIFT;
794 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
795 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
796 val |= on_off;
797 val |= PMC_BLINK_TIMER_ENB;
798 pmc_writel(val, c->reg);
799 }
800
801 return 0;
802 }
803
804 struct clk_ops tegra30_blink_clk_ops = {
805 .init = &tegra30_blink_clk_init,
806 .enable = &tegra30_blink_clk_enable,
807 .disable = &tegra30_blink_clk_disable,
808 .set_rate = &tegra30_blink_clk_set_rate,
809 };
810
811 /* PLL Functions */
812 static int tegra30_pll_clk_wait_for_lock(struct clk *c, u32 lock_reg,
813 u32 lock_bit)
814 {
815 #if USE_PLL_LOCK_BITS
816 int i;
817 for (i = 0; i < c->u.pll.lock_delay; i++) {
818 if (clk_readl(lock_reg) & lock_bit) {
819 udelay(PLL_POST_LOCK_DELAY);
820 return 0;
821 }
822 udelay(2); /* timeout = 2 * lock time */
823 }
824 pr_err("Timed out waiting for lock bit on pll %s", c->name);
825 return -1;
826 #endif
827 udelay(c->u.pll.lock_delay);
828
829 return 0;
830 }
831
832 static void tegra30_utmi_param_configure(struct clk *c)
833 {
834 u32 reg;
835 int i;
836 unsigned long main_rate =
837 clk_get_rate(c->parent->parent);
838
839 for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
840 if (main_rate == utmi_parameters[i].osc_frequency)
841 break;
842 }
843
844 if (i >= ARRAY_SIZE(utmi_parameters)) {
845 pr_err("%s: Unexpected main rate %lu\n", __func__, main_rate);
846 return;
847 }
848
849 reg = clk_readl(UTMIP_PLL_CFG2);
850
851 /* Program UTMIP PLL stable and active counts */
852 /* [FIXME] arclk_rst.h says WRONG! This should be 1ms -> 0x50 Check! */
853 reg &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
854 reg |= UTMIP_PLL_CFG2_STABLE_COUNT(
855 utmi_parameters[i].stable_count);
856
857 reg &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
858
859 reg |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(
860 utmi_parameters[i].active_delay_count);
861
862 /* Remove power downs from UTMIP PLL control bits */
863 reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
864 reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
865 reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
866
867 clk_writel(reg, UTMIP_PLL_CFG2);
868
869 /* Program UTMIP PLL delay and oscillator frequency counts */
870 reg = clk_readl(UTMIP_PLL_CFG1);
871 reg &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
872
873 reg |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(
874 utmi_parameters[i].enable_delay_count);
875
876 reg &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
877 reg |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(
878 utmi_parameters[i].xtal_freq_count);
879
880 /* Remove power downs from UTMIP PLL control bits */
881 reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
882 reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
883 reg &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
884
885 clk_writel(reg, UTMIP_PLL_CFG1);
886 }
887
888 static void tegra30_pll_clk_init(struct clk *c)
889 {
890 u32 val = clk_readl(c->reg + PLL_BASE);
891
892 c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
893
894 if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
895 const struct clk_pll_freq_table *sel;
896 unsigned long input_rate = clk_get_rate(c->parent);
897 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
898 if (sel->input_rate == input_rate &&
899 sel->output_rate == c->u.pll.fixed_rate) {
900 c->mul = sel->n;
901 c->div = sel->m * sel->p;
902 return;
903 }
904 }
905 pr_err("Clock %s has unknown fixed frequency\n", c->name);
906 BUG();
907 } else if (val & PLL_BASE_BYPASS) {
908 c->mul = 1;
909 c->div = 1;
910 } else {
911 c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
912 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
913 if (c->flags & PLLU)
914 c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
915 else
916 c->div *= (0x1 << ((val & PLL_BASE_DIVP_MASK) >>
917 PLL_BASE_DIVP_SHIFT));
918 if (c->flags & PLL_FIXED) {
919 unsigned long rate = clk_get_rate_locked(c);
920 BUG_ON(rate != c->u.pll.fixed_rate);
921 }
922 }
923
924 if (c->flags & PLLU)
925 tegra30_utmi_param_configure(c);
926 }
927
928 static int tegra30_pll_clk_enable(struct clk *c)
929 {
930 u32 val;
931 pr_debug("%s on clock %s\n", __func__, c->name);
932
933 #if USE_PLL_LOCK_BITS
934 val = clk_readl(c->reg + PLL_MISC(c));
935 val |= PLL_MISC_LOCK_ENABLE(c);
936 clk_writel(val, c->reg + PLL_MISC(c));
937 #endif
938 val = clk_readl(c->reg + PLL_BASE);
939 val &= ~PLL_BASE_BYPASS;
940 val |= PLL_BASE_ENABLE;
941 clk_writel(val, c->reg + PLL_BASE);
942
943 if (c->flags & PLLM) {
944 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
945 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
946 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
947 }
948
949 tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
950
951 return 0;
952 }
953
954 static void tegra30_pll_clk_disable(struct clk *c)
955 {
956 u32 val;
957 pr_debug("%s on clock %s\n", __func__, c->name);
958
959 val = clk_readl(c->reg);
960 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
961 clk_writel(val, c->reg);
962
963 if (c->flags & PLLM) {
964 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
965 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
966 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
967 }
968 }
969
970 static int tegra30_pll_clk_set_rate(struct clk *c, unsigned long rate)
971 {
972 u32 val, p_div, old_base;
973 unsigned long input_rate;
974 const struct clk_pll_freq_table *sel;
975 struct clk_pll_freq_table cfg;
976
977 pr_debug("%s: %s %lu\n", __func__, c->name, rate);
978
979 if (c->flags & PLL_FIXED) {
980 int ret = 0;
981 if (rate != c->u.pll.fixed_rate) {
982 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
983 __func__, c->name, c->u.pll.fixed_rate, rate);
984 ret = -EINVAL;
985 }
986 return ret;
987 }
988
989 if (c->flags & PLLM) {
990 if (rate != clk_get_rate_locked(c)) {
991 pr_err("%s: Can not change memory %s rate in flight\n",
992 __func__, c->name);
993 return -EINVAL;
994 }
995 return 0;
996 }
997
998 p_div = 0;
999 input_rate = clk_get_rate(c->parent);
1000
1001 /* Check if the target rate is tabulated */
1002 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1003 if (sel->input_rate == input_rate && sel->output_rate == rate) {
1004 if (c->flags & PLLU) {
1005 BUG_ON(sel->p < 1 || sel->p > 2);
1006 if (sel->p == 1)
1007 p_div = PLLU_BASE_POST_DIV;
1008 } else {
1009 BUG_ON(sel->p < 1);
1010 for (val = sel->p; val > 1; val >>= 1)
1011 p_div++;
1012 p_div <<= PLL_BASE_DIVP_SHIFT;
1013 }
1014 break;
1015 }
1016 }
1017
1018 /* Configure out-of-table rate */
1019 if (sel->input_rate == 0) {
1020 unsigned long cfreq;
1021 BUG_ON(c->flags & PLLU);
1022 sel = &cfg;
1023
1024 switch (input_rate) {
1025 case 12000000:
1026 case 26000000:
1027 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
1028 break;
1029 case 13000000:
1030 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
1031 break;
1032 case 16800000:
1033 case 19200000:
1034 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
1035 break;
1036 default:
1037 pr_err("%s: Unexpected reference rate %lu\n",
1038 __func__, input_rate);
1039 BUG();
1040 }
1041
1042 /* Raise VCO to guarantee 0.5% accuracy */
1043 for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
1044 cfg.output_rate <<= 1)
1045 p_div++;
1046
1047 cfg.p = 0x1 << p_div;
1048 cfg.m = input_rate / cfreq;
1049 cfg.n = cfg.output_rate / cfreq;
1050 cfg.cpcon = OUT_OF_TABLE_CPCON;
1051
1052 if ((cfg.m > (PLL_BASE_DIVM_MASK >> PLL_BASE_DIVM_SHIFT)) ||
1053 (cfg.n > (PLL_BASE_DIVN_MASK >> PLL_BASE_DIVN_SHIFT)) ||
1054 (p_div > (PLL_BASE_DIVP_MASK >> PLL_BASE_DIVP_SHIFT)) ||
1055 (cfg.output_rate > c->u.pll.vco_max)) {
1056 pr_err("%s: Failed to set %s out-of-table rate %lu\n",
1057 __func__, c->name, rate);
1058 return -EINVAL;
1059 }
1060 p_div <<= PLL_BASE_DIVP_SHIFT;
1061 }
1062
1063 c->mul = sel->n;
1064 c->div = sel->m * sel->p;
1065
1066 old_base = val = clk_readl(c->reg + PLL_BASE);
1067 val &= ~(PLL_BASE_DIVM_MASK | PLL_BASE_DIVN_MASK |
1068 ((c->flags & PLLU) ? PLLU_BASE_POST_DIV : PLL_BASE_DIVP_MASK));
1069 val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
1070 (sel->n << PLL_BASE_DIVN_SHIFT) | p_div;
1071 if (val == old_base)
1072 return 0;
1073
1074 if (c->state == ON) {
1075 tegra30_pll_clk_disable(c);
1076 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
1077 }
1078 clk_writel(val, c->reg + PLL_BASE);
1079
1080 if (c->flags & PLL_HAS_CPCON) {
1081 val = clk_readl(c->reg + PLL_MISC(c));
1082 val &= ~PLL_MISC_CPCON_MASK;
1083 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
1084 if (c->flags & (PLLU | PLLD)) {
1085 val &= ~PLL_MISC_LFCON_MASK;
1086 if (sel->n >= PLLDU_LFCON_SET_DIVN)
1087 val |= 0x1 << PLL_MISC_LFCON_SHIFT;
1088 } else if (c->flags & (PLLX | PLLM)) {
1089 val &= ~(0x1 << PLL_MISC_DCCON_SHIFT);
1090 if (rate >= (c->u.pll.vco_max >> 1))
1091 val |= 0x1 << PLL_MISC_DCCON_SHIFT;
1092 }
1093 clk_writel(val, c->reg + PLL_MISC(c));
1094 }
1095
1096 if (c->state == ON)
1097 tegra30_pll_clk_enable(c);
1098
1099 return 0;
1100 }
1101
1102 struct clk_ops tegra30_pll_ops = {
1103 .init = tegra30_pll_clk_init,
1104 .enable = tegra30_pll_clk_enable,
1105 .disable = tegra30_pll_clk_disable,
1106 .set_rate = tegra30_pll_clk_set_rate,
1107 };
1108
1109 static int
1110 tegra30_plld_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
1111 {
1112 u32 val, mask, reg;
1113
1114 switch (p) {
1115 case TEGRA_CLK_PLLD_CSI_OUT_ENB:
1116 mask = PLLD_BASE_CSI_CLKENABLE;
1117 reg = c->reg + PLL_BASE;
1118 break;
1119 case TEGRA_CLK_PLLD_DSI_OUT_ENB:
1120 mask = PLLD_MISC_DSI_CLKENABLE;
1121 reg = c->reg + PLL_MISC(c);
1122 break;
1123 case TEGRA_CLK_PLLD_MIPI_MUX_SEL:
1124 if (!(c->flags & PLL_ALT_MISC_REG)) {
1125 mask = PLLD_BASE_DSIB_MUX_MASK;
1126 reg = c->reg + PLL_BASE;
1127 break;
1128 }
1129 /* fall through - error since PLLD2 does not have MUX_SEL control */
1130 default:
1131 return -EINVAL;
1132 }
1133
1134 val = clk_readl(reg);
1135 if (setting)
1136 val |= mask;
1137 else
1138 val &= ~mask;
1139 clk_writel(val, reg);
1140 return 0;
1141 }
1142
1143 struct clk_ops tegra_plld_ops = {
1144 .init = tegra30_pll_clk_init,
1145 .enable = tegra30_pll_clk_enable,
1146 .disable = tegra30_pll_clk_disable,
1147 .set_rate = tegra30_pll_clk_set_rate,
1148 .clk_cfg_ex = tegra30_plld_clk_cfg_ex,
1149 };
1150
1151 static void tegra30_plle_clk_init(struct clk *c)
1152 {
1153 u32 val;
1154
1155 val = clk_readl(PLLE_AUX);
1156 c->parent = (val & PLLE_AUX_PLLP_SEL) ?
1157 tegra_get_clock_by_name("pll_p") :
1158 tegra_get_clock_by_name("pll_ref");
1159
1160 val = clk_readl(c->reg + PLL_BASE);
1161 c->state = (val & PLLE_BASE_ENABLE) ? ON : OFF;
1162 c->mul = (val & PLLE_BASE_DIVN_MASK) >> PLLE_BASE_DIVN_SHIFT;
1163 c->div = (val & PLLE_BASE_DIVM_MASK) >> PLLE_BASE_DIVM_SHIFT;
1164 c->div *= (val & PLLE_BASE_DIVP_MASK) >> PLLE_BASE_DIVP_SHIFT;
1165 }
1166
1167 static void tegra30_plle_clk_disable(struct clk *c)
1168 {
1169 u32 val;
1170 pr_debug("%s on clock %s\n", __func__, c->name);
1171
1172 val = clk_readl(c->reg + PLL_BASE);
1173 val &= ~(PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
1174 clk_writel(val, c->reg + PLL_BASE);
1175 }
1176
1177 static void tegra30_plle_training(struct clk *c)
1178 {
1179 u32 val;
1180
1181 /* PLLE is already disabled, and setup cleared;
1182 * create falling edge on PLLE IDDQ input */
1183 val = pmc_readl(PMC_SATA_PWRGT);
1184 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
1185 pmc_writel(val, PMC_SATA_PWRGT);
1186
1187 val = pmc_readl(PMC_SATA_PWRGT);
1188 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
1189 pmc_writel(val, PMC_SATA_PWRGT);
1190
1191 val = pmc_readl(PMC_SATA_PWRGT);
1192 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
1193 pmc_writel(val, PMC_SATA_PWRGT);
1194
1195 do {
1196 val = clk_readl(c->reg + PLL_MISC(c));
1197 } while (!(val & PLLE_MISC_READY));
1198 }
1199
1200 static int tegra30_plle_configure(struct clk *c, bool force_training)
1201 {
1202 u32 val;
1203 const struct clk_pll_freq_table *sel;
1204 unsigned long rate = c->u.pll.fixed_rate;
1205 unsigned long input_rate = clk_get_rate(c->parent);
1206
1207 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1208 if (sel->input_rate == input_rate && sel->output_rate == rate)
1209 break;
1210 }
1211
1212 if (sel->input_rate == 0)
1213 return -ENOSYS;
1214
1215 /* disable PLLE, clear setup fiels */
1216 tegra30_plle_clk_disable(c);
1217
1218 val = clk_readl(c->reg + PLL_MISC(c));
1219 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
1220 clk_writel(val, c->reg + PLL_MISC(c));
1221
1222 /* training */
1223 val = clk_readl(c->reg + PLL_MISC(c));
1224 if (force_training || (!(val & PLLE_MISC_READY)))
1225 tegra30_plle_training(c);
1226
1227 /* configure dividers, setup, disable SS */
1228 val = clk_readl(c->reg + PLL_BASE);
1229 val &= ~PLLE_BASE_DIV_MASK;
1230 val |= PLLE_BASE_DIV(sel->m, sel->n, sel->p, sel->cpcon);
1231 clk_writel(val, c->reg + PLL_BASE);
1232 c->mul = sel->n;
1233 c->div = sel->m * sel->p;
1234
1235 val = clk_readl(c->reg + PLL_MISC(c));
1236 val |= PLLE_MISC_SETUP_VALUE;
1237 val |= PLLE_MISC_LOCK_ENABLE;
1238 clk_writel(val, c->reg + PLL_MISC(c));
1239
1240 val = clk_readl(PLLE_SS_CTRL);
1241 val |= PLLE_SS_DISABLE;
1242 clk_writel(val, PLLE_SS_CTRL);
1243
1244 /* enable and lock PLLE*/
1245 val = clk_readl(c->reg + PLL_BASE);
1246 val |= (PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
1247 clk_writel(val, c->reg + PLL_BASE);
1248
1249 tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_MISC(c), PLLE_MISC_LOCK);
1250
1251 return 0;
1252 }
1253
1254 static int tegra30_plle_clk_enable(struct clk *c)
1255 {
1256 pr_debug("%s on clock %s\n", __func__, c->name);
1257 return tegra30_plle_configure(c, !c->set);
1258 }
1259
1260 struct clk_ops tegra30_plle_ops = {
1261 .init = tegra30_plle_clk_init,
1262 .enable = tegra30_plle_clk_enable,
1263 .disable = tegra30_plle_clk_disable,
1264 };
1265
1266 /* Clock divider ops */
1267 static void tegra30_pll_div_clk_init(struct clk *c)
1268 {
1269 if (c->flags & DIV_U71) {
1270 u32 divu71;
1271 u32 val = clk_readl(c->reg);
1272 val >>= c->reg_shift;
1273 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
1274 if (!(val & PLL_OUT_RESET_DISABLE))
1275 c->state = OFF;
1276
1277 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
1278 c->div = (divu71 + 2);
1279 c->mul = 2;
1280 } else if (c->flags & DIV_2) {
1281 c->state = ON;
1282 if (c->flags & (PLLD | PLLX)) {
1283 c->div = 2;
1284 c->mul = 1;
1285 } else
1286 BUG();
1287 } else {
1288 c->state = ON;
1289 c->div = 1;
1290 c->mul = 1;
1291 }
1292 }
1293
1294 static int tegra30_pll_div_clk_enable(struct clk *c)
1295 {
1296 u32 val;
1297 u32 new_val;
1298
1299 pr_debug("%s: %s\n", __func__, c->name);
1300 if (c->flags & DIV_U71) {
1301 val = clk_readl(c->reg);
1302 new_val = val >> c->reg_shift;
1303 new_val &= 0xFFFF;
1304
1305 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
1306
1307 val &= ~(0xFFFF << c->reg_shift);
1308 val |= new_val << c->reg_shift;
1309 clk_writel_delay(val, c->reg);
1310 return 0;
1311 } else if (c->flags & DIV_2) {
1312 return 0;
1313 }
1314 return -EINVAL;
1315 }
1316
1317 static void tegra30_pll_div_clk_disable(struct clk *c)
1318 {
1319 u32 val;
1320 u32 new_val;
1321
1322 pr_debug("%s: %s\n", __func__, c->name);
1323 if (c->flags & DIV_U71) {
1324 val = clk_readl(c->reg);
1325 new_val = val >> c->reg_shift;
1326 new_val &= 0xFFFF;
1327
1328 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
1329
1330 val &= ~(0xFFFF << c->reg_shift);
1331 val |= new_val << c->reg_shift;
1332 clk_writel_delay(val, c->reg);
1333 }
1334 }
1335
1336 static int tegra30_pll_div_clk_set_rate(struct clk *c, unsigned long rate)
1337 {
1338 u32 val;
1339 u32 new_val;
1340 int divider_u71;
1341 unsigned long parent_rate = clk_get_rate(c->parent);
1342
1343 pr_debug("%s: %s %lu\n", __func__, c->name, rate);
1344 if (c->flags & DIV_U71) {
1345 divider_u71 = clk_div71_get_divider(
1346 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1347 if (divider_u71 >= 0) {
1348 val = clk_readl(c->reg);
1349 new_val = val >> c->reg_shift;
1350 new_val &= 0xFFFF;
1351 if (c->flags & DIV_U71_FIXED)
1352 new_val |= PLL_OUT_OVERRIDE;
1353 new_val &= ~PLL_OUT_RATIO_MASK;
1354 new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
1355
1356 val &= ~(0xFFFF << c->reg_shift);
1357 val |= new_val << c->reg_shift;
1358 clk_writel_delay(val, c->reg);
1359 c->div = divider_u71 + 2;
1360 c->mul = 2;
1361 return 0;
1362 }
1363 } else if (c->flags & DIV_2)
1364 return clk_set_rate(c->parent, rate * 2);
1365
1366 return -EINVAL;
1367 }
1368
1369 static long tegra30_pll_div_clk_round_rate(struct clk *c, unsigned long rate)
1370 {
1371 int divider;
1372 unsigned long parent_rate = clk_get_rate(c->parent);
1373 pr_debug("%s: %s %lu\n", __func__, c->name, rate);
1374
1375 if (c->flags & DIV_U71) {
1376 divider = clk_div71_get_divider(
1377 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1378 if (divider < 0)
1379 return divider;
1380 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1381 } else if (c->flags & DIV_2)
1382 /* no rounding - fixed DIV_2 dividers pass rate to parent PLL */
1383 return rate;
1384
1385 return -EINVAL;
1386 }
1387
1388 struct clk_ops tegra30_pll_div_ops = {
1389 .init = tegra30_pll_div_clk_init,
1390 .enable = tegra30_pll_div_clk_enable,
1391 .disable = tegra30_pll_div_clk_disable,
1392 .set_rate = tegra30_pll_div_clk_set_rate,
1393 .round_rate = tegra30_pll_div_clk_round_rate,
1394 };
1395
1396 /* Periph clk ops */
1397 static inline u32 periph_clk_source_mask(struct clk *c)
1398 {
1399 if (c->flags & MUX8)
1400 return 7 << 29;
1401 else if (c->flags & MUX_PWM)
1402 return 3 << 28;
1403 else if (c->flags & MUX_CLK_OUT)
1404 return 3 << (c->u.periph.clk_num + 4);
1405 else if (c->flags & PLLD)
1406 return PLLD_BASE_DSIB_MUX_MASK;
1407 else
1408 return 3 << 30;
1409 }
1410
1411 static inline u32 periph_clk_source_shift(struct clk *c)
1412 {
1413 if (c->flags & MUX8)
1414 return 29;
1415 else if (c->flags & MUX_PWM)
1416 return 28;
1417 else if (c->flags & MUX_CLK_OUT)
1418 return c->u.periph.clk_num + 4;
1419 else if (c->flags & PLLD)
1420 return PLLD_BASE_DSIB_MUX_SHIFT;
1421 else
1422 return 30;
1423 }
1424
1425 static void tegra30_periph_clk_init(struct clk *c)
1426 {
1427 u32 val = clk_readl(c->reg);
1428 const struct clk_mux_sel *mux = 0;
1429 const struct clk_mux_sel *sel;
1430 if (c->flags & MUX) {
1431 for (sel = c->inputs; sel->input != NULL; sel++) {
1432 if (((val & periph_clk_source_mask(c)) >>
1433 periph_clk_source_shift(c)) == sel->value)
1434 mux = sel;
1435 }
1436 BUG_ON(!mux);
1437
1438 c->parent = mux->input;
1439 } else {
1440 c->parent = c->inputs[0].input;
1441 }
1442
1443 if (c->flags & DIV_U71) {
1444 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
1445 if ((c->flags & DIV_U71_UART) &&
1446 (!(val & PERIPH_CLK_UART_DIV_ENB))) {
1447 divu71 = 0;
1448 }
1449 if (c->flags & DIV_U71_IDLE) {
1450 val &= ~(PERIPH_CLK_SOURCE_DIVU71_MASK <<
1451 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
1452 val |= (PERIPH_CLK_SOURCE_DIVIDLE_VAL <<
1453 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
1454 clk_writel(val, c->reg);
1455 }
1456 c->div = divu71 + 2;
1457 c->mul = 2;
1458 } else if (c->flags & DIV_U16) {
1459 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
1460 c->div = divu16 + 1;
1461 c->mul = 1;
1462 } else {
1463 c->div = 1;
1464 c->mul = 1;
1465 }
1466
1467 c->state = ON;
1468 if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
1469 c->state = OFF;
1470 if (!(c->flags & PERIPH_NO_RESET))
1471 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
1472 c->state = OFF;
1473 }
1474
1475 static int tegra30_periph_clk_enable(struct clk *c)
1476 {
1477 pr_debug("%s on clock %s\n", __func__, c->name);
1478
1479 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
1480 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
1481 return 0;
1482
1483 clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_SET_REG(c));
1484 if (!(c->flags & PERIPH_NO_RESET) &&
1485 !(c->flags & PERIPH_MANUAL_RESET)) {
1486 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) &
1487 PERIPH_CLK_TO_BIT(c)) {
1488 udelay(5); /* reset propagation delay */
1489 clk_writel(PERIPH_CLK_TO_BIT(c),
1490 PERIPH_CLK_TO_RST_CLR_REG(c));
1491 }
1492 }
1493 return 0;
1494 }
1495
1496 static void tegra30_periph_clk_disable(struct clk *c)
1497 {
1498 unsigned long val;
1499 pr_debug("%s on clock %s\n", __func__, c->name);
1500
1501 if (c->refcnt)
1502 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
1503
1504 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] == 0) {
1505 /* If peripheral is in the APB bus then read the APB bus to
1506 * flush the write operation in apb bus. This will avoid the
1507 * peripheral access after disabling clock*/
1508 if (c->flags & PERIPH_ON_APB)
1509 val = chipid_readl();
1510
1511 clk_writel_delay(
1512 PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_CLR_REG(c));
1513 }
1514 }
1515
1516 static void tegra30_periph_clk_reset(struct clk *c, bool assert)
1517 {
1518 unsigned long val;
1519 pr_debug("%s %s on clock %s\n", __func__,
1520 assert ? "assert" : "deassert", c->name);
1521
1522 if (!(c->flags & PERIPH_NO_RESET)) {
1523 if (assert) {
1524 /* If peripheral is in the APB bus then read the APB
1525 * bus to flush the write operation in apb bus. This
1526 * will avoid the peripheral access after disabling
1527 * clock */
1528 if (c->flags & PERIPH_ON_APB)
1529 val = chipid_readl();
1530
1531 clk_writel(PERIPH_CLK_TO_BIT(c),
1532 PERIPH_CLK_TO_RST_SET_REG(c));
1533 } else
1534 clk_writel(PERIPH_CLK_TO_BIT(c),
1535 PERIPH_CLK_TO_RST_CLR_REG(c));
1536 }
1537 }
1538
1539 static int tegra30_periph_clk_set_parent(struct clk *c, struct clk *p)
1540 {
1541 u32 val;
1542 const struct clk_mux_sel *sel;
1543 pr_debug("%s: %s %s\n", __func__, c->name, p->name);
1544
1545 if (!(c->flags & MUX))
1546 return (p == c->parent) ? 0 : (-EINVAL);
1547
1548 for (sel = c->inputs; sel->input != NULL; sel++) {
1549 if (sel->input == p) {
1550 val = clk_readl(c->reg);
1551 val &= ~periph_clk_source_mask(c);
1552 val |= (sel->value << periph_clk_source_shift(c));
1553
1554 if (c->refcnt)
1555 clk_enable(p);
1556
1557 clk_writel_delay(val, c->reg);
1558
1559 if (c->refcnt && c->parent)
1560 clk_disable(c->parent);
1561
1562 clk_reparent(c, p);
1563 return 0;
1564 }
1565 }
1566
1567 return -EINVAL;
1568 }
1569
1570 static int tegra30_periph_clk_set_rate(struct clk *c, unsigned long rate)
1571 {
1572 u32 val;
1573 int divider;
1574 unsigned long parent_rate = clk_get_rate(c->parent);
1575
1576 if (c->flags & DIV_U71) {
1577 divider = clk_div71_get_divider(
1578 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1579 if (divider >= 0) {
1580 val = clk_readl(c->reg);
1581 val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
1582 val |= divider;
1583 if (c->flags & DIV_U71_UART) {
1584 if (divider)
1585 val |= PERIPH_CLK_UART_DIV_ENB;
1586 else
1587 val &= ~PERIPH_CLK_UART_DIV_ENB;
1588 }
1589 clk_writel_delay(val, c->reg);
1590 c->div = divider + 2;
1591 c->mul = 2;
1592 return 0;
1593 }
1594 } else if (c->flags & DIV_U16) {
1595 divider = clk_div16_get_divider(parent_rate, rate);
1596 if (divider >= 0) {
1597 val = clk_readl(c->reg);
1598 val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
1599 val |= divider;
1600 clk_writel_delay(val, c->reg);
1601 c->div = divider + 1;
1602 c->mul = 1;
1603 return 0;
1604 }
1605 } else if (parent_rate <= rate) {
1606 c->div = 1;
1607 c->mul = 1;
1608 return 0;
1609 }
1610 return -EINVAL;
1611 }
1612
1613 static long tegra30_periph_clk_round_rate(struct clk *c,
1614 unsigned long rate)
1615 {
1616 int divider;
1617 unsigned long parent_rate = clk_get_rate(c->parent);
1618 pr_debug("%s: %s %lu\n", __func__, c->name, rate);
1619
1620 if (c->flags & DIV_U71) {
1621 divider = clk_div71_get_divider(
1622 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1623 if (divider < 0)
1624 return divider;
1625
1626 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1627 } else if (c->flags & DIV_U16) {
1628 divider = clk_div16_get_divider(parent_rate, rate);
1629 if (divider < 0)
1630 return divider;
1631 return DIV_ROUND_UP(parent_rate, divider + 1);
1632 }
1633 return -EINVAL;
1634 }
1635
1636 struct clk_ops tegra30_periph_clk_ops = {
1637 .init = &tegra30_periph_clk_init,
1638 .enable = &tegra30_periph_clk_enable,
1639 .disable = &tegra30_periph_clk_disable,
1640 .set_parent = &tegra30_periph_clk_set_parent,
1641 .set_rate = &tegra30_periph_clk_set_rate,
1642 .round_rate = &tegra30_periph_clk_round_rate,
1643 .reset = &tegra30_periph_clk_reset,
1644 };
1645
1646 /* Periph extended clock configuration ops */
1647 static int
1648 tegra30_vi_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
1649 {
1650 if (p == TEGRA_CLK_VI_INP_SEL) {
1651 u32 val = clk_readl(c->reg);
1652 val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
1653 val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
1654 PERIPH_CLK_VI_SEL_EX_MASK;
1655 clk_writel(val, c->reg);
1656 return 0;
1657 }
1658 return -EINVAL;
1659 }
1660
1661 struct clk_ops tegra_vi_clk_ops = {
1662 .init = &tegra30_periph_clk_init,
1663 .enable = &tegra30_periph_clk_enable,
1664 .disable = &tegra30_periph_clk_disable,
1665 .set_parent = &tegra30_periph_clk_set_parent,
1666 .set_rate = &tegra30_periph_clk_set_rate,
1667 .round_rate = &tegra30_periph_clk_round_rate,
1668 .clk_cfg_ex = &tegra30_vi_clk_cfg_ex,
1669 .reset = &tegra30_periph_clk_reset,
1670 };
1671
1672 static int
1673 tegra30_nand_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
1674 {
1675 if (p == TEGRA_CLK_NAND_PAD_DIV2_ENB) {
1676 u32 val = clk_readl(c->reg);
1677 if (setting)
1678 val |= PERIPH_CLK_NAND_DIV_EX_ENB;
1679 else
1680 val &= ~PERIPH_CLK_NAND_DIV_EX_ENB;
1681 clk_writel(val, c->reg);
1682 return 0;
1683 }
1684 return -EINVAL;
1685 }
1686
1687 struct clk_ops tegra_nand_clk_ops = {
1688 .init = &tegra30_periph_clk_init,
1689 .enable = &tegra30_periph_clk_enable,
1690 .disable = &tegra30_periph_clk_disable,
1691 .set_parent = &tegra30_periph_clk_set_parent,
1692 .set_rate = &tegra30_periph_clk_set_rate,
1693 .round_rate = &tegra30_periph_clk_round_rate,
1694 .clk_cfg_ex = &tegra30_nand_clk_cfg_ex,
1695 .reset = &tegra30_periph_clk_reset,
1696 };
1697
1698 static int
1699 tegra30_dtv_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
1700 {
1701 if (p == TEGRA_CLK_DTV_INVERT) {
1702 u32 val = clk_readl(c->reg);
1703 if (setting)
1704 val |= PERIPH_CLK_DTV_POLARITY_INV;
1705 else
1706 val &= ~PERIPH_CLK_DTV_POLARITY_INV;
1707 clk_writel(val, c->reg);
1708 return 0;
1709 }
1710 return -EINVAL;
1711 }
1712
1713 struct clk_ops tegra_dtv_clk_ops = {
1714 .init = &tegra30_periph_clk_init,
1715 .enable = &tegra30_periph_clk_enable,
1716 .disable = &tegra30_periph_clk_disable,
1717 .set_parent = &tegra30_periph_clk_set_parent,
1718 .set_rate = &tegra30_periph_clk_set_rate,
1719 .round_rate = &tegra30_periph_clk_round_rate,
1720 .clk_cfg_ex = &tegra30_dtv_clk_cfg_ex,
1721 .reset = &tegra30_periph_clk_reset,
1722 };
1723
1724 static int tegra30_dsib_clk_set_parent(struct clk *c, struct clk *p)
1725 {
1726 const struct clk_mux_sel *sel;
1727 struct clk *d = tegra_get_clock_by_name("pll_d");
1728
1729 pr_debug("%s: %s %s\n", __func__, c->name, p->name);
1730
1731 for (sel = c->inputs; sel->input != NULL; sel++) {
1732 if (sel->input == p) {
1733 if (c->refcnt)
1734 clk_enable(p);
1735
1736 /* The DSIB parent selection bit is in PLLD base
1737 register - can not do direct r-m-w, must be
1738 protected by PLLD lock */
1739 tegra_clk_cfg_ex(
1740 d, TEGRA_CLK_PLLD_MIPI_MUX_SEL, sel->value);
1741
1742 if (c->refcnt && c->parent)
1743 clk_disable(c->parent);
1744
1745 clk_reparent(c, p);
1746 return 0;
1747 }
1748 }
1749
1750 return -EINVAL;
1751 }
1752
1753 struct clk_ops tegra_dsib_clk_ops = {
1754 .init = &tegra30_periph_clk_init,
1755 .enable = &tegra30_periph_clk_enable,
1756 .disable = &tegra30_periph_clk_disable,
1757 .set_parent = &tegra30_dsib_clk_set_parent,
1758 .set_rate = &tegra30_periph_clk_set_rate,
1759 .round_rate = &tegra30_periph_clk_round_rate,
1760 .reset = &tegra30_periph_clk_reset,
1761 };
1762
1763 /* pciex clock support only reset function */
1764 struct clk_ops tegra_pciex_clk_ops = {
1765 .reset = tegra30_periph_clk_reset,
1766 };
1767
1768 /* Output clock ops */
1769
1770 static DEFINE_SPINLOCK(clk_out_lock);
1771
1772 static void tegra30_clk_out_init(struct clk *c)
1773 {
1774 const struct clk_mux_sel *mux = 0;
1775 const struct clk_mux_sel *sel;
1776 u32 val = pmc_readl(c->reg);
1777
1778 c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
1779 c->mul = 1;
1780 c->div = 1;
1781
1782 for (sel = c->inputs; sel->input != NULL; sel++) {
1783 if (((val & periph_clk_source_mask(c)) >>
1784 periph_clk_source_shift(c)) == sel->value)
1785 mux = sel;
1786 }
1787 BUG_ON(!mux);
1788 c->parent = mux->input;
1789 }
1790
1791 static int tegra30_clk_out_enable(struct clk *c)
1792 {
1793 u32 val;
1794 unsigned long flags;
1795
1796 pr_debug("%s on clock %s\n", __func__, c->name);
1797
1798 spin_lock_irqsave(&clk_out_lock, flags);
1799 val = pmc_readl(c->reg);
1800 val |= (0x1 << c->u.periph.clk_num);
1801 pmc_writel(val, c->reg);
1802 spin_unlock_irqrestore(&clk_out_lock, flags);
1803
1804 return 0;
1805 }
1806
1807 static void tegra30_clk_out_disable(struct clk *c)
1808 {
1809 u32 val;
1810 unsigned long flags;
1811
1812 pr_debug("%s on clock %s\n", __func__, c->name);
1813
1814 spin_lock_irqsave(&clk_out_lock, flags);
1815 val = pmc_readl(c->reg);
1816 val &= ~(0x1 << c->u.periph.clk_num);
1817 pmc_writel(val, c->reg);
1818 spin_unlock_irqrestore(&clk_out_lock, flags);
1819 }
1820
1821 static int tegra30_clk_out_set_parent(struct clk *c, struct clk *p)
1822 {
1823 u32 val;
1824 unsigned long flags;
1825 const struct clk_mux_sel *sel;
1826
1827 pr_debug("%s: %s %s\n", __func__, c->name, p->name);
1828
1829 for (sel = c->inputs; sel->input != NULL; sel++) {
1830 if (sel->input == p) {
1831 if (c->refcnt)
1832 clk_enable(p);
1833
1834 spin_lock_irqsave(&clk_out_lock, flags);
1835 val = pmc_readl(c->reg);
1836 val &= ~periph_clk_source_mask(c);
1837 val |= (sel->value << periph_clk_source_shift(c));
1838 pmc_writel(val, c->reg);
1839 spin_unlock_irqrestore(&clk_out_lock, flags);
1840
1841 if (c->refcnt && c->parent)
1842 clk_disable(c->parent);
1843
1844 clk_reparent(c, p);
1845 return 0;
1846 }
1847 }
1848 return -EINVAL;
1849 }
1850
1851 struct clk_ops tegra_clk_out_ops = {
1852 .init = &tegra30_clk_out_init,
1853 .enable = &tegra30_clk_out_enable,
1854 .disable = &tegra30_clk_out_disable,
1855 .set_parent = &tegra30_clk_out_set_parent,
1856 };
1857
1858 /* Clock doubler ops */
1859 static void tegra30_clk_double_init(struct clk *c)
1860 {
1861 u32 val = clk_readl(c->reg);
1862 c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
1863 c->div = 1;
1864 c->state = ON;
1865 if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
1866 c->state = OFF;
1867 };
1868
1869 static int tegra30_clk_double_set_rate(struct clk *c, unsigned long rate)
1870 {
1871 u32 val;
1872 unsigned long parent_rate = clk_get_rate(c->parent);
1873 if (rate == parent_rate) {
1874 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
1875 clk_writel(val, c->reg);
1876 c->mul = 1;
1877 c->div = 1;
1878 return 0;
1879 } else if (rate == 2 * parent_rate) {
1880 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
1881 clk_writel(val, c->reg);
1882 c->mul = 2;
1883 c->div = 1;
1884 return 0;
1885 }
1886 return -EINVAL;
1887 }
1888
1889 struct clk_ops tegra30_clk_double_ops = {
1890 .init = &tegra30_clk_double_init,
1891 .enable = &tegra30_periph_clk_enable,
1892 .disable = &tegra30_periph_clk_disable,
1893 .set_rate = &tegra30_clk_double_set_rate,
1894 };
1895
1896 /* Audio sync clock ops */
1897 static int tegra30_sync_source_set_rate(struct clk *c, unsigned long rate)
1898 {
1899 c->rate = rate;
1900 return 0;
1901 }
1902
1903 struct clk_ops tegra_sync_source_ops = {
1904 .set_rate = &tegra30_sync_source_set_rate,
1905 };
1906
1907 static void tegra30_audio_sync_clk_init(struct clk *c)
1908 {
1909 int source;
1910 const struct clk_mux_sel *sel;
1911 u32 val = clk_readl(c->reg);
1912 c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
1913 source = val & AUDIO_SYNC_SOURCE_MASK;
1914 for (sel = c->inputs; sel->input != NULL; sel++)
1915 if (sel->value == source)
1916 break;
1917 BUG_ON(sel->input == NULL);
1918 c->parent = sel->input;
1919 }
1920
1921 static int tegra30_audio_sync_clk_enable(struct clk *c)
1922 {
1923 u32 val = clk_readl(c->reg);
1924 clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
1925 return 0;
1926 }
1927
1928 static void tegra30_audio_sync_clk_disable(struct clk *c)
1929 {
1930 u32 val = clk_readl(c->reg);
1931 clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
1932 }
1933
1934 static int tegra30_audio_sync_clk_set_parent(struct clk *c, struct clk *p)
1935 {
1936 u32 val;
1937 const struct clk_mux_sel *sel;
1938 for (sel = c->inputs; sel->input != NULL; sel++) {
1939 if (sel->input == p) {
1940 val = clk_readl(c->reg);
1941 val &= ~AUDIO_SYNC_SOURCE_MASK;
1942 val |= sel->value;
1943
1944 if (c->refcnt)
1945 clk_enable(p);
1946
1947 clk_writel(val, c->reg);
1948
1949 if (c->refcnt && c->parent)
1950 clk_disable(c->parent);
1951
1952 clk_reparent(c, p);
1953 return 0;
1954 }
1955 }
1956
1957 return -EINVAL;
1958 }
1959
1960 struct clk_ops tegra30_audio_sync_clk_ops = {
1961 .init = tegra30_audio_sync_clk_init,
1962 .enable = tegra30_audio_sync_clk_enable,
1963 .disable = tegra30_audio_sync_clk_disable,
1964 .set_parent = tegra30_audio_sync_clk_set_parent,
1965 };
1966
1967 /* cml0 (pcie), and cml1 (sata) clock ops */
1968 static void tegra30_cml_clk_init(struct clk *c)
1969 {
1970 u32 val = clk_readl(c->reg);
1971 c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
1972 }
1973
1974 static int tegra30_cml_clk_enable(struct clk *c)
1975 {
1976 u32 val = clk_readl(c->reg);
1977 val |= (0x1 << c->u.periph.clk_num);
1978 clk_writel(val, c->reg);
1979 return 0;
1980 }
1981
1982 static void tegra30_cml_clk_disable(struct clk *c)
1983 {
1984 u32 val = clk_readl(c->reg);
1985 val &= ~(0x1 << c->u.periph.clk_num);
1986 clk_writel(val, c->reg);
1987 }
1988
1989 struct clk_ops tegra_cml_clk_ops = {
1990 .init = &tegra30_cml_clk_init,
1991 .enable = &tegra30_cml_clk_enable,
1992 .disable = &tegra30_cml_clk_disable,
1993 };
This page took 0.103595 seconds and 5 git commands to generate.