blob: 3129c2f21d81a55caaeeb01d8cf6caad3ad101f5 [file] [log] [blame]
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001/*
2 * arch/arm/mach-tegra/tegra30_clocks.c
3 *
Prashant Gaikwad88e790a2012-08-06 11:57:39 +05304 * Copyright (c) 2010-2012 NVIDIA CORPORATION. All rights reserved.
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00005 *
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*/
305struct 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
318static 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
355static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
356static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
357static 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 */
365static 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
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530384static inline int clk_set_div(struct clk_tegra *c, u32 n)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000385{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530386 struct clk *clk = c->hw.clk;
387
388 return clk_set_rate(clk,
389 (__clk_get_rate(__clk_get_parent(clk)) + n - 1) / n);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000390}
391
392static inline u32 periph_clk_to_reg(
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530393 struct clk_tegra *c, u32 reg_L, u32 reg_V, int offs)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000394{
395 u32 reg = c->u.periph.clk_num / 32;
396 BUG_ON(reg >= RST_DEVICES_NUM);
397 if (reg < 3)
398 reg = reg_L + (reg * offs);
399 else
400 reg = reg_V + ((reg - 3) * offs);
401 return reg;
402}
403
404static unsigned long clk_measure_input_freq(void)
405{
406 u32 clock_autodetect;
407 clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
408 do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
409 clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
410 if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
411 return 12000000;
412 } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
413 return 13000000;
414 } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
415 return 19200000;
416 } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
417 return 26000000;
418 } else if (clock_autodetect >= 1025 - 3 && clock_autodetect <= 1025 + 3) {
419 return 16800000;
420 } else if (clock_autodetect >= 2344 - 3 && clock_autodetect <= 2344 + 3) {
421 return 38400000;
422 } else if (clock_autodetect >= 2928 - 3 && clock_autodetect <= 2928 + 3) {
423 return 48000000;
424 } else {
425 pr_err("%s: Unexpected clock autodetect value %d", __func__,
426 clock_autodetect);
427 BUG();
428 return 0;
429 }
430}
431
432static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate,
433 u32 flags, u32 round_mode)
434{
435 s64 divider_u71 = parent_rate;
436 if (!rate)
437 return -EINVAL;
438
439 if (!(flags & DIV_U71_INT))
440 divider_u71 *= 2;
441 if (round_mode == ROUND_DIVIDER_UP)
442 divider_u71 += rate - 1;
443 do_div(divider_u71, rate);
444 if (flags & DIV_U71_INT)
445 divider_u71 *= 2;
446
447 if (divider_u71 - 2 < 0)
448 return 0;
449
450 if (divider_u71 - 2 > 255)
451 return -EINVAL;
452
453 return divider_u71 - 2;
454}
455
456static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
457{
458 s64 divider_u16;
459
460 divider_u16 = parent_rate;
461 if (!rate)
462 return -EINVAL;
463 divider_u16 += rate - 1;
464 do_div(divider_u16, rate);
465
466 if (divider_u16 - 1 < 0)
467 return 0;
468
469 if (divider_u16 - 1 > 0xFFFF)
470 return -EINVAL;
471
472 return divider_u16 - 1;
473}
474
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530475static unsigned long tegra30_clk_fixed_recalc_rate(struct clk_hw *hw,
476 unsigned long parent_rate)
477{
478 return to_clk_tegra(hw)->fixed_rate;
479}
480
Joseph Lob78c0302012-08-17 14:51:21 +0800481struct clk_ops tegra30_clk_32k_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530482 .recalc_rate = tegra30_clk_fixed_recalc_rate,
483};
484
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000485/* clk_m functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530486static unsigned long tegra30_clk_m_recalc_rate(struct clk_hw *hw,
487 unsigned long parent_rate)
488{
489 if (!to_clk_tegra(hw)->fixed_rate)
490 to_clk_tegra(hw)->fixed_rate = clk_measure_input_freq();
491 return to_clk_tegra(hw)->fixed_rate;
492}
493
494static void tegra30_clk_m_init(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000495{
496 u32 osc_ctrl = clk_readl(OSC_CTRL);
497 u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
498 u32 pll_ref_div = osc_ctrl & OSC_CTRL_PLL_REF_DIV_MASK;
499
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530500 switch (to_clk_tegra(hw)->fixed_rate) {
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000501 case 12000000:
502 auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
503 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
504 break;
505 case 13000000:
506 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
507 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
508 break;
509 case 19200000:
510 auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
511 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
512 break;
513 case 26000000:
514 auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
515 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
516 break;
517 case 16800000:
518 auto_clock_control |= OSC_CTRL_OSC_FREQ_16_8MHZ;
519 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
520 break;
521 case 38400000:
522 auto_clock_control |= OSC_CTRL_OSC_FREQ_38_4MHZ;
523 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_2);
524 break;
525 case 48000000:
526 auto_clock_control |= OSC_CTRL_OSC_FREQ_48MHZ;
527 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_4);
528 break;
529 default:
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530530 pr_err("%s: Unexpected clock rate %ld", __func__,
531 to_clk_tegra(hw)->fixed_rate);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000532 BUG();
533 }
534 clk_writel(auto_clock_control, OSC_CTRL);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000535}
536
Prashant Gaikwad88e790a2012-08-06 11:57:39 +0530537struct clk_ops tegra30_clk_m_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530538 .init = tegra30_clk_m_init,
539 .recalc_rate = tegra30_clk_m_recalc_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000540};
541
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530542static unsigned long tegra30_clk_m_div_recalc_rate(struct clk_hw *hw,
543 unsigned long parent_rate)
544{
545 struct clk_tegra *c = to_clk_tegra(hw);
546 u64 rate = parent_rate;
547
548 if (c->mul != 0 && c->div != 0) {
549 rate *= c->mul;
550 rate += c->div - 1; /* round up */
551 do_div(rate, c->div);
552 }
553
554 return rate;
555}
556
Prashant Gaikwad88e790a2012-08-06 11:57:39 +0530557struct clk_ops tegra_clk_m_div_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530558 .recalc_rate = tegra30_clk_m_div_recalc_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000559};
560
561/* PLL reference divider functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530562static unsigned long tegra30_pll_ref_recalc_rate(struct clk_hw *hw,
563 unsigned long parent_rate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000564{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530565 struct clk_tegra *c = to_clk_tegra(hw);
566 unsigned long rate = parent_rate;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000567 u32 pll_ref_div = clk_readl(OSC_CTRL) & OSC_CTRL_PLL_REF_DIV_MASK;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000568
569 switch (pll_ref_div) {
570 case OSC_CTRL_PLL_REF_DIV_1:
571 c->div = 1;
572 break;
573 case OSC_CTRL_PLL_REF_DIV_2:
574 c->div = 2;
575 break;
576 case OSC_CTRL_PLL_REF_DIV_4:
577 c->div = 4;
578 break;
579 default:
580 pr_err("%s: Invalid pll ref divider %d", __func__, pll_ref_div);
581 BUG();
582 }
583 c->mul = 1;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530584
585 if (c->mul != 0 && c->div != 0) {
586 rate *= c->mul;
587 rate += c->div - 1; /* round up */
588 do_div(rate, c->div);
589 }
590
591 return rate;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000592}
593
Prashant Gaikwad88e790a2012-08-06 11:57:39 +0530594struct clk_ops tegra_pll_ref_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530595 .recalc_rate = tegra30_pll_ref_recalc_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000596};
597
598/* super clock functions */
599/* "super clocks" on tegra30 have two-stage muxes, fractional 7.1 divider and
600 * clock skipping super divider. We will ignore the clock skipping divider,
601 * since we can't lower the voltage when using the clock skip, but we can if
602 * we lower the PLL frequency. We will use 7.1 divider for CPU super-clock
603 * only when its parent is a fixed rate PLL, since we can't change PLL rate
604 * in this case.
605 */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530606static void tegra30_super_clk_init(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000607{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530608 struct clk_tegra *c = to_clk_tegra(hw);
609 struct clk_tegra *p =
610 to_clk_tegra(__clk_get_hw(__clk_get_parent(hw->clk)));
611
612 c->state = ON;
613 if (c->flags & DIV_U71) {
614 /* Init safe 7.1 divider value (does not affect PLLX path) */
615 clk_writel(SUPER_CLOCK_DIV_U71_MIN << SUPER_CLOCK_DIV_U71_SHIFT,
616 c->reg + SUPER_CLK_DIVIDER);
617 c->mul = 2;
618 c->div = 2;
619 if (!(p->flags & PLLX))
620 c->div += SUPER_CLOCK_DIV_U71_MIN;
621 } else
622 clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
623}
624
625static u8 tegra30_super_clk_get_parent(struct clk_hw *hw)
626{
627 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000628 u32 val;
629 int source;
630 int shift;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530631
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000632 val = clk_readl(c->reg + SUPER_CLK_MUX);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000633 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
634 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
635 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
636 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
637 source = (val >> shift) & SUPER_SOURCE_MASK;
638 if (c->flags & DIV_2)
639 source |= val & SUPER_LP_DIV2_BYPASS;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000640
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530641 return source;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000642}
643
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530644static int tegra30_super_clk_set_parent(struct clk_hw *hw, u8 index)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000645{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530646 struct clk_tegra *c = to_clk_tegra(hw);
647 struct clk_tegra *p =
648 to_clk_tegra(__clk_get_hw(clk_get_parent(hw->clk)));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000649 u32 val;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000650 int shift;
651
652 val = clk_readl(c->reg + SUPER_CLK_MUX);
653 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
654 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
655 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
656 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000657
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530658 /* For LP mode super-clock switch between PLLX direct
659 and divided-by-2 outputs is allowed only when other
660 than PLLX clock source is current parent */
661 if ((c->flags & DIV_2) && (p->flags & PLLX) &&
662 ((index ^ val) & SUPER_LP_DIV2_BYPASS)) {
663 if (p->flags & PLLX)
664 return -EINVAL;
665 val ^= SUPER_LP_DIV2_BYPASS;
666 clk_writel_delay(val, c->reg);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000667 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530668 val &= ~(SUPER_SOURCE_MASK << shift);
669 val |= (index & SUPER_SOURCE_MASK) << shift;
670
671 /* 7.1 divider for CPU super-clock does not affect
672 PLLX path */
673 if (c->flags & DIV_U71) {
674 u32 div = 0;
675 if (!(p->flags & PLLX)) {
676 div = clk_readl(c->reg +
677 SUPER_CLK_DIVIDER);
678 div &= SUPER_CLOCK_DIV_U71_MASK;
679 div >>= SUPER_CLOCK_DIV_U71_SHIFT;
680 }
681 c->div = div + 2;
682 c->mul = 2;
683 }
684 clk_writel_delay(val, c->reg);
685
686 return 0;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000687}
688
689/*
690 * Do not use super clocks "skippers", since dividing using a clock skipper
691 * does not allow the voltage to be scaled down. Instead adjust the rate of
692 * the parent clock. This requires that the parent of a super clock have no
693 * other children, otherwise the rate will change underneath the other
694 * children. Special case: if fixed rate PLL is CPU super clock parent the
695 * rate of this PLL can't be changed, and it has many other children. In
696 * this case use 7.1 fractional divider to adjust the super clock rate.
697 */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530698static int tegra30_super_clk_set_rate(struct clk_hw *hw, unsigned long rate,
699 unsigned long parent_rate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000700{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530701 struct clk_tegra *c = to_clk_tegra(hw);
702 struct clk *parent = __clk_get_parent(hw->clk);
703 struct clk_tegra *cparent = to_clk_tegra(__clk_get_hw(parent));
704
705 if ((c->flags & DIV_U71) && (cparent->flags & PLL_FIXED)) {
706 int div = clk_div71_get_divider(parent_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000707 rate, c->flags, ROUND_DIVIDER_DOWN);
708 div = max(div, SUPER_CLOCK_DIV_U71_MIN);
709
710 clk_writel(div << SUPER_CLOCK_DIV_U71_SHIFT,
711 c->reg + SUPER_CLK_DIVIDER);
712 c->div = div + 2;
713 c->mul = 2;
714 return 0;
715 }
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000716 return 0;
717}
718
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530719static unsigned long tegra30_super_clk_recalc_rate(struct clk_hw *hw,
720 unsigned long parent_rate)
721{
722 struct clk_tegra *c = to_clk_tegra(hw);
723 u64 rate = parent_rate;
724
725 if (c->mul != 0 && c->div != 0) {
726 rate *= c->mul;
727 rate += c->div - 1; /* round up */
728 do_div(rate, c->div);
729 }
730
731 return rate;
732}
733
734static long tegra30_super_clk_round_rate(struct clk_hw *hw, unsigned long rate,
735 unsigned long *prate)
736{
737 struct clk_tegra *c = to_clk_tegra(hw);
738 struct clk *parent = __clk_get_parent(hw->clk);
739 struct clk_tegra *cparent = to_clk_tegra(__clk_get_hw(parent));
740 int mul = 2;
741 int div;
742
743 if ((c->flags & DIV_U71) && (cparent->flags & PLL_FIXED)) {
744 div = clk_div71_get_divider(*prate,
745 rate, c->flags, ROUND_DIVIDER_DOWN);
746 div = max(div, SUPER_CLOCK_DIV_U71_MIN) + 2;
747 rate = *prate * mul;
748 rate += div - 1; /* round up */
749 do_div(rate, c->div);
750
751 return rate;
752 }
753 return *prate;
754}
755
756struct clk_ops tegra30_super_ops = {
757 .init = tegra30_super_clk_init,
758 .set_parent = tegra30_super_clk_set_parent,
759 .get_parent = tegra30_super_clk_get_parent,
760 .recalc_rate = tegra30_super_clk_recalc_rate,
761 .round_rate = tegra30_super_clk_round_rate,
762 .set_rate = tegra30_super_clk_set_rate,
763};
764
765static unsigned long tegra30_twd_clk_recalc_rate(struct clk_hw *hw,
766 unsigned long parent_rate)
767{
768 struct clk_tegra *c = to_clk_tegra(hw);
769 u64 rate = parent_rate;
770
771 if (c->mul != 0 && c->div != 0) {
772 rate *= c->mul;
773 rate += c->div - 1; /* round up */
774 do_div(rate, c->div);
775 }
776
777 return rate;
778}
779
Prashant Gaikwad88e790a2012-08-06 11:57:39 +0530780struct clk_ops tegra30_twd_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530781 .recalc_rate = tegra30_twd_clk_recalc_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000782};
783
784/* Blink output functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530785static int tegra30_blink_clk_is_enabled(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000786{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530787 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000788 u32 val;
789
790 val = pmc_readl(PMC_CTRL);
791 c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530792 return c->state;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000793}
794
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530795static int tegra30_blink_clk_enable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000796{
797 u32 val;
798
799 val = pmc_readl(PMC_DPD_PADS_ORIDE);
800 pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
801
802 val = pmc_readl(PMC_CTRL);
803 pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
804
805 return 0;
806}
807
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530808static void tegra30_blink_clk_disable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000809{
810 u32 val;
811
812 val = pmc_readl(PMC_CTRL);
813 pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
814
815 val = pmc_readl(PMC_DPD_PADS_ORIDE);
816 pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
817}
818
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530819static int tegra30_blink_clk_set_rate(struct clk_hw *hw, unsigned long rate,
820 unsigned long parent_rate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000821{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530822 struct clk_tegra *c = to_clk_tegra(hw);
823
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000824 if (rate >= parent_rate) {
825 c->div = 1;
826 pmc_writel(0, c->reg);
827 } else {
828 unsigned int on_off;
829 u32 val;
830
831 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
832 c->div = on_off * 8;
833
834 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
835 PMC_BLINK_TIMER_DATA_ON_SHIFT;
836 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
837 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
838 val |= on_off;
839 val |= PMC_BLINK_TIMER_ENB;
840 pmc_writel(val, c->reg);
841 }
842
843 return 0;
844}
845
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530846static unsigned long tegra30_blink_clk_recalc_rate(struct clk_hw *hw,
847 unsigned long parent_rate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000848{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530849 struct clk_tegra *c = to_clk_tegra(hw);
850 u64 rate = parent_rate;
851 u32 val;
852 u32 mul;
853 u32 div;
854 u32 on_off;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000855
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530856 mul = 1;
857 val = pmc_readl(c->reg);
858
859 if (val & PMC_BLINK_TIMER_ENB) {
860 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
861 PMC_BLINK_TIMER_DATA_ON_MASK;
862 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
863 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
864 on_off += val;
865 /* each tick in the blink timer is 4 32KHz clocks */
866 div = on_off * 4;
867 } else {
868 div = 1;
869 }
870
871 if (mul != 0 && div != 0) {
872 rate *= mul;
873 rate += div - 1; /* round up */
874 do_div(rate, div);
875 }
876 return rate;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000877}
878
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530879static long tegra30_blink_clk_round_rate(struct clk_hw *hw, unsigned long rate,
880 unsigned long *prate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000881{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530882 int div;
883 int mul;
884 long round_rate = *prate;
885
886 mul = 1;
887
888 if (rate >= *prate) {
889 div = 1;
890 } else {
891 div = DIV_ROUND_UP(*prate / 8, rate);
892 div *= 8;
893 }
894
895 round_rate *= mul;
896 round_rate += div - 1;
897 do_div(round_rate, div);
898
899 return round_rate;
900}
901
902struct clk_ops tegra30_blink_clk_ops = {
903 .is_enabled = tegra30_blink_clk_is_enabled,
904 .enable = tegra30_blink_clk_enable,
905 .disable = tegra30_blink_clk_disable,
906 .recalc_rate = tegra30_blink_clk_recalc_rate,
907 .round_rate = tegra30_blink_clk_round_rate,
908 .set_rate = tegra30_blink_clk_set_rate,
909};
910
911static void tegra30_utmi_param_configure(struct clk_hw *hw)
912{
913 unsigned long main_rate =
914 __clk_get_rate(__clk_get_parent(__clk_get_parent(hw->clk)));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000915 u32 reg;
916 int i;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000917
918 for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
919 if (main_rate == utmi_parameters[i].osc_frequency)
920 break;
921 }
922
923 if (i >= ARRAY_SIZE(utmi_parameters)) {
924 pr_err("%s: Unexpected main rate %lu\n", __func__, main_rate);
925 return;
926 }
927
928 reg = clk_readl(UTMIP_PLL_CFG2);
929
930 /* Program UTMIP PLL stable and active counts */
931 /* [FIXME] arclk_rst.h says WRONG! This should be 1ms -> 0x50 Check! */
932 reg &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
933 reg |= UTMIP_PLL_CFG2_STABLE_COUNT(
934 utmi_parameters[i].stable_count);
935
936 reg &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
937
938 reg |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(
939 utmi_parameters[i].active_delay_count);
940
941 /* Remove power downs from UTMIP PLL control bits */
942 reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
943 reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
944 reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
945
946 clk_writel(reg, UTMIP_PLL_CFG2);
947
948 /* Program UTMIP PLL delay and oscillator frequency counts */
949 reg = clk_readl(UTMIP_PLL_CFG1);
950 reg &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
951
952 reg |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(
953 utmi_parameters[i].enable_delay_count);
954
955 reg &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
956 reg |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(
957 utmi_parameters[i].xtal_freq_count);
958
959 /* Remove power downs from UTMIP PLL control bits */
960 reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
961 reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
962 reg &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
963
964 clk_writel(reg, UTMIP_PLL_CFG1);
965}
966
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530967/* PLL Functions */
968static int tegra30_pll_clk_wait_for_lock(struct clk_tegra *c, u32 lock_reg,
969 u32 lock_bit)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000970{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530971 int ret = 0;
972
973#if USE_PLL_LOCK_BITS
974 int i;
975 for (i = 0; i < c->u.pll.lock_delay; i++) {
976 if (clk_readl(lock_reg) & lock_bit) {
977 udelay(PLL_POST_LOCK_DELAY);
978 return 0;
979 }
980 udelay(2); /* timeout = 2 * lock time */
981 }
982 pr_err("Timed out waiting for lock bit on pll %s",
983 __clk_get_name(hw->clk));
984 ret = -1;
985#else
986 udelay(c->u.pll.lock_delay);
987#endif
988 return ret;
989}
990
991static int tegra30_pll_clk_is_enabled(struct clk_hw *hw)
992{
993 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000994 u32 val = clk_readl(c->reg + PLL_BASE);
995
996 c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530997 return c->state;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +0000998}
999
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301000static void tegra30_pll_clk_init(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001001{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301002 struct clk_tegra *c = to_clk_tegra(hw);
1003
1004 if (c->flags & PLLU)
1005 tegra30_utmi_param_configure(hw);
1006}
1007
1008static int tegra30_pll_clk_enable(struct clk_hw *hw)
1009{
1010 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001011 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301012 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001013
1014#if USE_PLL_LOCK_BITS
1015 val = clk_readl(c->reg + PLL_MISC(c));
1016 val |= PLL_MISC_LOCK_ENABLE(c);
1017 clk_writel(val, c->reg + PLL_MISC(c));
1018#endif
1019 val = clk_readl(c->reg + PLL_BASE);
1020 val &= ~PLL_BASE_BYPASS;
1021 val |= PLL_BASE_ENABLE;
1022 clk_writel(val, c->reg + PLL_BASE);
1023
1024 if (c->flags & PLLM) {
1025 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
1026 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
1027 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
1028 }
1029
1030 tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
1031
1032 return 0;
1033}
1034
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301035static void tegra30_pll_clk_disable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001036{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301037 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001038 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301039 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001040
1041 val = clk_readl(c->reg);
1042 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
1043 clk_writel(val, c->reg);
1044
1045 if (c->flags & PLLM) {
1046 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
1047 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
1048 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
1049 }
1050}
1051
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301052static int tegra30_pll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1053 unsigned long parent_rate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001054{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301055 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001056 u32 val, p_div, old_base;
1057 unsigned long input_rate;
1058 const struct clk_pll_freq_table *sel;
1059 struct clk_pll_freq_table cfg;
1060
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001061 if (c->flags & PLL_FIXED) {
1062 int ret = 0;
1063 if (rate != c->u.pll.fixed_rate) {
1064 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301065 __func__, __clk_get_name(hw->clk),
1066 c->u.pll.fixed_rate, rate);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001067 ret = -EINVAL;
1068 }
1069 return ret;
1070 }
1071
1072 if (c->flags & PLLM) {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301073 if (rate != __clk_get_rate(hw->clk)) {
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001074 pr_err("%s: Can not change memory %s rate in flight\n",
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301075 __func__, __clk_get_name(hw->clk));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001076 return -EINVAL;
1077 }
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001078 }
1079
1080 p_div = 0;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301081 input_rate = parent_rate;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001082
1083 /* Check if the target rate is tabulated */
1084 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1085 if (sel->input_rate == input_rate && sel->output_rate == rate) {
1086 if (c->flags & PLLU) {
1087 BUG_ON(sel->p < 1 || sel->p > 2);
1088 if (sel->p == 1)
1089 p_div = PLLU_BASE_POST_DIV;
1090 } else {
1091 BUG_ON(sel->p < 1);
1092 for (val = sel->p; val > 1; val >>= 1)
1093 p_div++;
1094 p_div <<= PLL_BASE_DIVP_SHIFT;
1095 }
1096 break;
1097 }
1098 }
1099
1100 /* Configure out-of-table rate */
1101 if (sel->input_rate == 0) {
1102 unsigned long cfreq;
1103 BUG_ON(c->flags & PLLU);
1104 sel = &cfg;
1105
1106 switch (input_rate) {
1107 case 12000000:
1108 case 26000000:
1109 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
1110 break;
1111 case 13000000:
1112 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
1113 break;
1114 case 16800000:
1115 case 19200000:
1116 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
1117 break;
1118 default:
1119 pr_err("%s: Unexpected reference rate %lu\n",
1120 __func__, input_rate);
1121 BUG();
1122 }
1123
1124 /* Raise VCO to guarantee 0.5% accuracy */
1125 for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
1126 cfg.output_rate <<= 1)
1127 p_div++;
1128
1129 cfg.p = 0x1 << p_div;
1130 cfg.m = input_rate / cfreq;
1131 cfg.n = cfg.output_rate / cfreq;
1132 cfg.cpcon = OUT_OF_TABLE_CPCON;
1133
1134 if ((cfg.m > (PLL_BASE_DIVM_MASK >> PLL_BASE_DIVM_SHIFT)) ||
1135 (cfg.n > (PLL_BASE_DIVN_MASK >> PLL_BASE_DIVN_SHIFT)) ||
1136 (p_div > (PLL_BASE_DIVP_MASK >> PLL_BASE_DIVP_SHIFT)) ||
1137 (cfg.output_rate > c->u.pll.vco_max)) {
1138 pr_err("%s: Failed to set %s out-of-table rate %lu\n",
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301139 __func__, __clk_get_name(hw->clk), rate);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001140 return -EINVAL;
1141 }
1142 p_div <<= PLL_BASE_DIVP_SHIFT;
1143 }
1144
1145 c->mul = sel->n;
1146 c->div = sel->m * sel->p;
1147
1148 old_base = val = clk_readl(c->reg + PLL_BASE);
1149 val &= ~(PLL_BASE_DIVM_MASK | PLL_BASE_DIVN_MASK |
1150 ((c->flags & PLLU) ? PLLU_BASE_POST_DIV : PLL_BASE_DIVP_MASK));
1151 val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
1152 (sel->n << PLL_BASE_DIVN_SHIFT) | p_div;
1153 if (val == old_base)
1154 return 0;
1155
1156 if (c->state == ON) {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301157 tegra30_pll_clk_disable(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001158 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
1159 }
1160 clk_writel(val, c->reg + PLL_BASE);
1161
1162 if (c->flags & PLL_HAS_CPCON) {
1163 val = clk_readl(c->reg + PLL_MISC(c));
1164 val &= ~PLL_MISC_CPCON_MASK;
1165 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
1166 if (c->flags & (PLLU | PLLD)) {
1167 val &= ~PLL_MISC_LFCON_MASK;
1168 if (sel->n >= PLLDU_LFCON_SET_DIVN)
1169 val |= 0x1 << PLL_MISC_LFCON_SHIFT;
1170 } else if (c->flags & (PLLX | PLLM)) {
1171 val &= ~(0x1 << PLL_MISC_DCCON_SHIFT);
1172 if (rate >= (c->u.pll.vco_max >> 1))
1173 val |= 0x1 << PLL_MISC_DCCON_SHIFT;
1174 }
1175 clk_writel(val, c->reg + PLL_MISC(c));
1176 }
1177
1178 if (c->state == ON)
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301179 tegra30_pll_clk_enable(hw);
1180
1181 c->u.pll.fixed_rate = rate;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001182
1183 return 0;
1184}
1185
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301186static long tegra30_pll_round_rate(struct clk_hw *hw, unsigned long rate,
1187 unsigned long *prate)
1188{
1189 struct clk_tegra *c = to_clk_tegra(hw);
1190 unsigned long input_rate = *prate;
1191 unsigned long output_rate = *prate;
1192 const struct clk_pll_freq_table *sel;
1193 struct clk_pll_freq_table cfg;
1194 int mul;
1195 int div;
1196 u32 p_div;
1197 u32 val;
1198
1199 if (c->flags & PLL_FIXED)
1200 return c->u.pll.fixed_rate;
1201
1202 if (c->flags & PLLM)
1203 return __clk_get_rate(hw->clk);
1204
1205 p_div = 0;
1206 /* Check if the target rate is tabulated */
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 if (c->flags & PLLU) {
1210 BUG_ON(sel->p < 1 || sel->p > 2);
1211 if (sel->p == 1)
1212 p_div = PLLU_BASE_POST_DIV;
1213 } else {
1214 BUG_ON(sel->p < 1);
1215 for (val = sel->p; val > 1; val >>= 1)
1216 p_div++;
1217 p_div <<= PLL_BASE_DIVP_SHIFT;
1218 }
1219 break;
1220 }
1221 }
1222
1223 if (sel->input_rate == 0) {
1224 unsigned long cfreq;
1225 BUG_ON(c->flags & PLLU);
1226 sel = &cfg;
1227
1228 switch (input_rate) {
1229 case 12000000:
1230 case 26000000:
1231 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
1232 break;
1233 case 13000000:
1234 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
1235 break;
1236 case 16800000:
1237 case 19200000:
1238 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
1239 break;
1240 default:
1241 pr_err("%s: Unexpected reference rate %lu\n",
1242 __func__, input_rate);
1243 BUG();
1244 }
1245
1246 /* Raise VCO to guarantee 0.5% accuracy */
1247 for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
1248 cfg.output_rate <<= 1)
1249 p_div++;
1250
1251 cfg.p = 0x1 << p_div;
1252 cfg.m = input_rate / cfreq;
1253 cfg.n = cfg.output_rate / cfreq;
1254 }
1255
1256 mul = sel->n;
1257 div = sel->m * sel->p;
1258
1259 output_rate *= mul;
1260 output_rate += div - 1; /* round up */
1261 do_div(output_rate, div);
1262
1263 return output_rate;
1264}
1265
1266static unsigned long tegra30_pll_recalc_rate(struct clk_hw *hw,
1267 unsigned long parent_rate)
1268{
1269 struct clk_tegra *c = to_clk_tegra(hw);
1270 u64 rate = parent_rate;
1271 u32 val = clk_readl(c->reg + PLL_BASE);
1272
1273 if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
1274 const struct clk_pll_freq_table *sel;
1275 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1276 if (sel->input_rate == parent_rate &&
1277 sel->output_rate == c->u.pll.fixed_rate) {
1278 c->mul = sel->n;
1279 c->div = sel->m * sel->p;
1280 break;
1281 }
1282 }
1283 pr_err("Clock %s has unknown fixed frequency\n",
1284 __clk_get_name(hw->clk));
1285 BUG();
1286 } else if (val & PLL_BASE_BYPASS) {
1287 c->mul = 1;
1288 c->div = 1;
1289 } else {
1290 c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
1291 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
1292 if (c->flags & PLLU)
1293 c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
1294 else
1295 c->div *= (0x1 << ((val & PLL_BASE_DIVP_MASK) >>
1296 PLL_BASE_DIVP_SHIFT));
1297 }
1298
1299 if (c->mul != 0 && c->div != 0) {
1300 rate *= c->mul;
1301 rate += c->div - 1; /* round up */
1302 do_div(rate, c->div);
1303 }
1304
1305 return rate;
1306}
1307
Prashant Gaikwad88e790a2012-08-06 11:57:39 +05301308struct clk_ops tegra30_pll_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301309 .is_enabled = tegra30_pll_clk_is_enabled,
1310 .init = tegra30_pll_clk_init,
1311 .enable = tegra30_pll_clk_enable,
1312 .disable = tegra30_pll_clk_disable,
1313 .recalc_rate = tegra30_pll_recalc_rate,
1314 .round_rate = tegra30_pll_round_rate,
1315 .set_rate = tegra30_pll_clk_set_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001316};
1317
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301318int tegra30_plld_clk_cfg_ex(struct clk_hw *hw,
1319 enum tegra_clk_ex_param p, u32 setting)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001320{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301321 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001322 u32 val, mask, reg;
1323
1324 switch (p) {
1325 case TEGRA_CLK_PLLD_CSI_OUT_ENB:
1326 mask = PLLD_BASE_CSI_CLKENABLE;
1327 reg = c->reg + PLL_BASE;
1328 break;
1329 case TEGRA_CLK_PLLD_DSI_OUT_ENB:
1330 mask = PLLD_MISC_DSI_CLKENABLE;
1331 reg = c->reg + PLL_MISC(c);
1332 break;
1333 case TEGRA_CLK_PLLD_MIPI_MUX_SEL:
1334 if (!(c->flags & PLL_ALT_MISC_REG)) {
1335 mask = PLLD_BASE_DSIB_MUX_MASK;
1336 reg = c->reg + PLL_BASE;
1337 break;
1338 }
1339 /* fall through - error since PLLD2 does not have MUX_SEL control */
1340 default:
1341 return -EINVAL;
1342 }
1343
1344 val = clk_readl(reg);
1345 if (setting)
1346 val |= mask;
1347 else
1348 val &= ~mask;
1349 clk_writel(val, reg);
1350 return 0;
1351}
1352
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301353static int tegra30_plle_clk_is_enabled(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001354{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301355 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001356 u32 val;
1357
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001358 val = clk_readl(c->reg + PLL_BASE);
1359 c->state = (val & PLLE_BASE_ENABLE) ? ON : OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301360 return c->state;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001361}
1362
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301363static void tegra30_plle_clk_disable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001364{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301365 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001366 u32 val;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001367
1368 val = clk_readl(c->reg + PLL_BASE);
1369 val &= ~(PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
1370 clk_writel(val, c->reg + PLL_BASE);
1371}
1372
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301373static void tegra30_plle_training(struct clk_tegra *c)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001374{
1375 u32 val;
1376
1377 /* PLLE is already disabled, and setup cleared;
1378 * create falling edge on PLLE IDDQ input */
1379 val = pmc_readl(PMC_SATA_PWRGT);
1380 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
1381 pmc_writel(val, PMC_SATA_PWRGT);
1382
1383 val = pmc_readl(PMC_SATA_PWRGT);
1384 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
1385 pmc_writel(val, PMC_SATA_PWRGT);
1386
1387 val = pmc_readl(PMC_SATA_PWRGT);
1388 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
1389 pmc_writel(val, PMC_SATA_PWRGT);
1390
1391 do {
1392 val = clk_readl(c->reg + PLL_MISC(c));
1393 } while (!(val & PLLE_MISC_READY));
1394}
1395
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301396static int tegra30_plle_configure(struct clk_hw *hw, bool force_training)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001397{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301398 struct clk_tegra *c = to_clk_tegra(hw);
1399 struct clk *parent = __clk_get_parent(hw->clk);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001400 const struct clk_pll_freq_table *sel;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301401 u32 val;
1402
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001403 unsigned long rate = c->u.pll.fixed_rate;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301404 unsigned long input_rate = __clk_get_rate(parent);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001405
1406 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1407 if (sel->input_rate == input_rate && sel->output_rate == rate)
1408 break;
1409 }
1410
1411 if (sel->input_rate == 0)
1412 return -ENOSYS;
1413
1414 /* disable PLLE, clear setup fiels */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301415 tegra30_plle_clk_disable(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001416
1417 val = clk_readl(c->reg + PLL_MISC(c));
1418 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
1419 clk_writel(val, c->reg + PLL_MISC(c));
1420
1421 /* training */
1422 val = clk_readl(c->reg + PLL_MISC(c));
1423 if (force_training || (!(val & PLLE_MISC_READY)))
1424 tegra30_plle_training(c);
1425
1426 /* configure dividers, setup, disable SS */
1427 val = clk_readl(c->reg + PLL_BASE);
1428 val &= ~PLLE_BASE_DIV_MASK;
1429 val |= PLLE_BASE_DIV(sel->m, sel->n, sel->p, sel->cpcon);
1430 clk_writel(val, c->reg + PLL_BASE);
1431 c->mul = sel->n;
1432 c->div = sel->m * sel->p;
1433
1434 val = clk_readl(c->reg + PLL_MISC(c));
1435 val |= PLLE_MISC_SETUP_VALUE;
1436 val |= PLLE_MISC_LOCK_ENABLE;
1437 clk_writel(val, c->reg + PLL_MISC(c));
1438
1439 val = clk_readl(PLLE_SS_CTRL);
1440 val |= PLLE_SS_DISABLE;
1441 clk_writel(val, PLLE_SS_CTRL);
1442
1443 /* enable and lock PLLE*/
1444 val = clk_readl(c->reg + PLL_BASE);
1445 val |= (PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
1446 clk_writel(val, c->reg + PLL_BASE);
1447
1448 tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_MISC(c), PLLE_MISC_LOCK);
1449
1450 return 0;
1451}
1452
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301453static int tegra30_plle_clk_enable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001454{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301455 struct clk_tegra *c = to_clk_tegra(hw);
1456
1457 return tegra30_plle_configure(hw, !c->set);
1458}
1459
1460static unsigned long tegra30_plle_clk_recalc_rate(struct clk_hw *hw,
1461 unsigned long parent_rate)
1462{
1463 struct clk_tegra *c = to_clk_tegra(hw);
1464 unsigned long rate = parent_rate;
1465 u32 val;
1466
1467 val = clk_readl(c->reg + PLL_BASE);
1468 c->mul = (val & PLLE_BASE_DIVN_MASK) >> PLLE_BASE_DIVN_SHIFT;
1469 c->div = (val & PLLE_BASE_DIVM_MASK) >> PLLE_BASE_DIVM_SHIFT;
1470 c->div *= (val & PLLE_BASE_DIVP_MASK) >> PLLE_BASE_DIVP_SHIFT;
1471
1472 if (c->mul != 0 && c->div != 0) {
1473 rate *= c->mul;
1474 rate += c->div - 1; /* round up */
1475 do_div(rate, c->div);
1476 }
1477 return rate;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001478}
1479
Prashant Gaikwad88e790a2012-08-06 11:57:39 +05301480struct clk_ops tegra30_plle_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301481 .is_enabled = tegra30_plle_clk_is_enabled,
1482 .enable = tegra30_plle_clk_enable,
1483 .disable = tegra30_plle_clk_disable,
1484 .recalc_rate = tegra30_plle_clk_recalc_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001485};
1486
1487/* Clock divider ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301488static int tegra30_pll_div_clk_is_enabled(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001489{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301490 struct clk_tegra *c = to_clk_tegra(hw);
1491
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001492 if (c->flags & DIV_U71) {
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001493 u32 val = clk_readl(c->reg);
1494 val >>= c->reg_shift;
1495 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
1496 if (!(val & PLL_OUT_RESET_DISABLE))
1497 c->state = OFF;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001498 } else {
1499 c->state = ON;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001500 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301501 return c->state;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001502}
1503
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301504static int tegra30_pll_div_clk_enable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001505{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301506 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001507 u32 val;
1508 u32 new_val;
1509
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301510 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001511 if (c->flags & DIV_U71) {
1512 val = clk_readl(c->reg);
1513 new_val = val >> c->reg_shift;
1514 new_val &= 0xFFFF;
1515
1516 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
1517
1518 val &= ~(0xFFFF << c->reg_shift);
1519 val |= new_val << c->reg_shift;
1520 clk_writel_delay(val, c->reg);
1521 return 0;
1522 } else if (c->flags & DIV_2) {
1523 return 0;
1524 }
1525 return -EINVAL;
1526}
1527
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301528static void tegra30_pll_div_clk_disable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001529{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301530 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001531 u32 val;
1532 u32 new_val;
1533
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301534 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001535 if (c->flags & DIV_U71) {
1536 val = clk_readl(c->reg);
1537 new_val = val >> c->reg_shift;
1538 new_val &= 0xFFFF;
1539
1540 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
1541
1542 val &= ~(0xFFFF << c->reg_shift);
1543 val |= new_val << c->reg_shift;
1544 clk_writel_delay(val, c->reg);
1545 }
1546}
1547
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301548static int tegra30_pll_div_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1549 unsigned long parent_rate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001550{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301551 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001552 u32 val;
1553 u32 new_val;
1554 int divider_u71;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001555
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001556 if (c->flags & DIV_U71) {
1557 divider_u71 = clk_div71_get_divider(
1558 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1559 if (divider_u71 >= 0) {
1560 val = clk_readl(c->reg);
1561 new_val = val >> c->reg_shift;
1562 new_val &= 0xFFFF;
1563 if (c->flags & DIV_U71_FIXED)
1564 new_val |= PLL_OUT_OVERRIDE;
1565 new_val &= ~PLL_OUT_RATIO_MASK;
1566 new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
1567
1568 val &= ~(0xFFFF << c->reg_shift);
1569 val |= new_val << c->reg_shift;
1570 clk_writel_delay(val, c->reg);
1571 c->div = divider_u71 + 2;
1572 c->mul = 2;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301573 c->fixed_rate = rate;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001574 return 0;
1575 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301576 } else if (c->flags & DIV_2) {
1577 c->fixed_rate = rate;
1578 return 0;
1579 }
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001580
1581 return -EINVAL;
1582}
1583
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301584static unsigned long tegra30_pll_div_clk_recalc_rate(struct clk_hw *hw,
1585 unsigned long parent_rate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001586{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301587 struct clk_tegra *c = to_clk_tegra(hw);
1588 u64 rate = parent_rate;
1589
1590 if (c->flags & DIV_U71) {
1591 u32 divu71;
1592 u32 val = clk_readl(c->reg);
1593 val >>= c->reg_shift;
1594
1595 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
1596 c->div = (divu71 + 2);
1597 c->mul = 2;
1598 } else if (c->flags & DIV_2) {
1599 if (c->flags & (PLLD | PLLX)) {
1600 c->div = 2;
1601 c->mul = 1;
1602 } else
1603 BUG();
1604 } else {
1605 c->div = 1;
1606 c->mul = 1;
1607 }
1608 if (c->mul != 0 && c->div != 0) {
1609 rate *= c->mul;
1610 rate += c->div - 1; /* round up */
1611 do_div(rate, c->div);
1612 }
1613
1614 return rate;
1615}
1616
1617static long tegra30_pll_div_clk_round_rate(struct clk_hw *hw,
1618 unsigned long rate, unsigned long *prate)
1619{
1620 struct clk_tegra *c = to_clk_tegra(hw);
1621 unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001622 int divider;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301623
1624 if (prate)
1625 parent_rate = *prate;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001626
1627 if (c->flags & DIV_U71) {
1628 divider = clk_div71_get_divider(
1629 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1630 if (divider < 0)
1631 return divider;
1632 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301633 } else if (c->flags & DIV_2) {
1634 *prate = rate * 2;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001635 return rate;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301636 }
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001637
1638 return -EINVAL;
1639}
1640
Prashant Gaikwad88e790a2012-08-06 11:57:39 +05301641struct clk_ops tegra30_pll_div_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301642 .is_enabled = tegra30_pll_div_clk_is_enabled,
1643 .enable = tegra30_pll_div_clk_enable,
1644 .disable = tegra30_pll_div_clk_disable,
1645 .set_rate = tegra30_pll_div_clk_set_rate,
1646 .recalc_rate = tegra30_pll_div_clk_recalc_rate,
1647 .round_rate = tegra30_pll_div_clk_round_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001648};
1649
1650/* Periph clk ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301651static inline u32 periph_clk_source_mask(struct clk_tegra *c)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001652{
1653 if (c->flags & MUX8)
1654 return 7 << 29;
1655 else if (c->flags & MUX_PWM)
1656 return 3 << 28;
1657 else if (c->flags & MUX_CLK_OUT)
1658 return 3 << (c->u.periph.clk_num + 4);
1659 else if (c->flags & PLLD)
1660 return PLLD_BASE_DSIB_MUX_MASK;
1661 else
1662 return 3 << 30;
1663}
1664
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301665static inline u32 periph_clk_source_shift(struct clk_tegra *c)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001666{
1667 if (c->flags & MUX8)
1668 return 29;
1669 else if (c->flags & MUX_PWM)
1670 return 28;
1671 else if (c->flags & MUX_CLK_OUT)
1672 return c->u.periph.clk_num + 4;
1673 else if (c->flags & PLLD)
1674 return PLLD_BASE_DSIB_MUX_SHIFT;
1675 else
1676 return 30;
1677}
1678
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301679static int tegra30_periph_clk_is_enabled(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001680{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301681 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001682
1683 c->state = ON;
1684 if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
1685 c->state = OFF;
1686 if (!(c->flags & PERIPH_NO_RESET))
1687 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
1688 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301689 return c->state;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001690}
1691
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301692static int tegra30_periph_clk_enable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001693{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301694 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001695
1696 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
1697 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
1698 return 0;
1699
1700 clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_SET_REG(c));
1701 if (!(c->flags & PERIPH_NO_RESET) &&
1702 !(c->flags & PERIPH_MANUAL_RESET)) {
1703 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) &
1704 PERIPH_CLK_TO_BIT(c)) {
1705 udelay(5); /* reset propagation delay */
1706 clk_writel(PERIPH_CLK_TO_BIT(c),
1707 PERIPH_CLK_TO_RST_CLR_REG(c));
1708 }
1709 }
1710 return 0;
1711}
1712
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301713static void tegra30_periph_clk_disable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001714{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301715 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001716 unsigned long val;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001717
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301718 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001719
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301720 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 0)
1721 return;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001722
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301723 /* If peripheral is in the APB bus then read the APB bus to
1724 * flush the write operation in apb bus. This will avoid the
1725 * peripheral access after disabling clock*/
1726 if (c->flags & PERIPH_ON_APB)
1727 val = chipid_readl();
1728
1729 clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_CLR_REG(c));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001730}
1731
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301732void tegra30_periph_clk_reset(struct clk_hw *hw, bool assert)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001733{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301734 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001735 unsigned long val;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001736
1737 if (!(c->flags & PERIPH_NO_RESET)) {
1738 if (assert) {
1739 /* If peripheral is in the APB bus then read the APB
1740 * bus to flush the write operation in apb bus. This
1741 * will avoid the peripheral access after disabling
1742 * clock */
1743 if (c->flags & PERIPH_ON_APB)
1744 val = chipid_readl();
1745
1746 clk_writel(PERIPH_CLK_TO_BIT(c),
1747 PERIPH_CLK_TO_RST_SET_REG(c));
1748 } else
1749 clk_writel(PERIPH_CLK_TO_BIT(c),
1750 PERIPH_CLK_TO_RST_CLR_REG(c));
1751 }
1752}
1753
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301754static int tegra30_periph_clk_set_parent(struct clk_hw *hw, u8 index)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001755{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301756 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001757 u32 val;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001758
1759 if (!(c->flags & MUX))
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301760 return (index == 0) ? 0 : (-EINVAL);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001761
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301762 val = clk_readl(c->reg);
1763 val &= ~periph_clk_source_mask(c);
1764 val |= (index << periph_clk_source_shift(c));
1765 clk_writel_delay(val, c->reg);
1766 return 0;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001767}
1768
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301769static u8 tegra30_periph_clk_get_parent(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001770{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301771 struct clk_tegra *c = to_clk_tegra(hw);
1772 u32 val = clk_readl(c->reg);
1773 int source = (val & periph_clk_source_mask(c)) >>
1774 periph_clk_source_shift(c);
1775
1776 if (!(c->flags & MUX))
1777 return 0;
1778
1779 return source;
1780}
1781
1782static int tegra30_periph_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1783 unsigned long parent_rate)
1784{
1785 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001786 u32 val;
1787 int divider;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001788
1789 if (c->flags & DIV_U71) {
1790 divider = clk_div71_get_divider(
1791 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1792 if (divider >= 0) {
1793 val = clk_readl(c->reg);
1794 val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
1795 val |= divider;
1796 if (c->flags & DIV_U71_UART) {
1797 if (divider)
1798 val |= PERIPH_CLK_UART_DIV_ENB;
1799 else
1800 val &= ~PERIPH_CLK_UART_DIV_ENB;
1801 }
1802 clk_writel_delay(val, c->reg);
1803 c->div = divider + 2;
1804 c->mul = 2;
1805 return 0;
1806 }
1807 } else if (c->flags & DIV_U16) {
1808 divider = clk_div16_get_divider(parent_rate, rate);
1809 if (divider >= 0) {
1810 val = clk_readl(c->reg);
1811 val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
1812 val |= divider;
1813 clk_writel_delay(val, c->reg);
1814 c->div = divider + 1;
1815 c->mul = 1;
1816 return 0;
1817 }
1818 } else if (parent_rate <= rate) {
1819 c->div = 1;
1820 c->mul = 1;
1821 return 0;
1822 }
1823 return -EINVAL;
1824}
1825
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301826static long tegra30_periph_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1827 unsigned long *prate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001828{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301829 struct clk_tegra *c = to_clk_tegra(hw);
1830 unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001831 int divider;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301832
1833 if (prate)
1834 parent_rate = *prate;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001835
1836 if (c->flags & DIV_U71) {
1837 divider = clk_div71_get_divider(
1838 parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1839 if (divider < 0)
1840 return divider;
1841
1842 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1843 } else if (c->flags & DIV_U16) {
1844 divider = clk_div16_get_divider(parent_rate, rate);
1845 if (divider < 0)
1846 return divider;
1847 return DIV_ROUND_UP(parent_rate, divider + 1);
1848 }
1849 return -EINVAL;
1850}
1851
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301852static unsigned long tegra30_periph_clk_recalc_rate(struct clk_hw *hw,
1853 unsigned long parent_rate)
1854{
1855 struct clk_tegra *c = to_clk_tegra(hw);
1856 u64 rate = parent_rate;
1857 u32 val = clk_readl(c->reg);
1858
1859 if (c->flags & DIV_U71) {
1860 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
1861 if ((c->flags & DIV_U71_UART) &&
1862 (!(val & PERIPH_CLK_UART_DIV_ENB))) {
1863 divu71 = 0;
1864 }
1865 if (c->flags & DIV_U71_IDLE) {
1866 val &= ~(PERIPH_CLK_SOURCE_DIVU71_MASK <<
1867 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
1868 val |= (PERIPH_CLK_SOURCE_DIVIDLE_VAL <<
1869 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
1870 clk_writel(val, c->reg);
1871 }
1872 c->div = divu71 + 2;
1873 c->mul = 2;
1874 } else if (c->flags & DIV_U16) {
1875 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
1876 c->div = divu16 + 1;
1877 c->mul = 1;
1878 } else {
1879 c->div = 1;
1880 c->mul = 1;
1881 }
1882
1883 if (c->mul != 0 && c->div != 0) {
1884 rate *= c->mul;
1885 rate += c->div - 1; /* round up */
1886 do_div(rate, c->div);
1887 }
1888 return rate;
1889}
1890
Prashant Gaikwad88e790a2012-08-06 11:57:39 +05301891struct clk_ops tegra30_periph_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301892 .is_enabled = tegra30_periph_clk_is_enabled,
1893 .enable = tegra30_periph_clk_enable,
1894 .disable = tegra30_periph_clk_disable,
1895 .set_parent = tegra30_periph_clk_set_parent,
1896 .get_parent = tegra30_periph_clk_get_parent,
1897 .set_rate = tegra30_periph_clk_set_rate,
1898 .round_rate = tegra30_periph_clk_round_rate,
1899 .recalc_rate = tegra30_periph_clk_recalc_rate,
1900};
1901
1902static int tegra30_dsib_clk_set_parent(struct clk_hw *hw, u8 index)
1903{
1904 struct clk *d = clk_get_sys(NULL, "pll_d");
1905 /* The DSIB parent selection bit is in PLLD base
1906 register - can not do direct r-m-w, must be
1907 protected by PLLD lock */
1908 tegra_clk_cfg_ex(
1909 d, TEGRA_CLK_PLLD_MIPI_MUX_SEL, index);
1910
1911 return 0;
1912}
1913
1914struct clk_ops tegra30_dsib_clk_ops = {
1915 .is_enabled = tegra30_periph_clk_is_enabled,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001916 .enable = &tegra30_periph_clk_enable,
1917 .disable = &tegra30_periph_clk_disable,
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301918 .set_parent = &tegra30_dsib_clk_set_parent,
1919 .get_parent = &tegra30_periph_clk_get_parent,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001920 .set_rate = &tegra30_periph_clk_set_rate,
1921 .round_rate = &tegra30_periph_clk_round_rate,
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301922 .recalc_rate = &tegra30_periph_clk_recalc_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001923};
1924
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001925/* Periph extended clock configuration ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301926int tegra30_vi_clk_cfg_ex(struct clk_hw *hw,
1927 enum tegra_clk_ex_param p, u32 setting)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001928{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301929 struct clk_tegra *c = to_clk_tegra(hw);
1930
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001931 if (p == TEGRA_CLK_VI_INP_SEL) {
1932 u32 val = clk_readl(c->reg);
1933 val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
1934 val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
1935 PERIPH_CLK_VI_SEL_EX_MASK;
1936 clk_writel(val, c->reg);
1937 return 0;
1938 }
1939 return -EINVAL;
1940}
1941
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301942int tegra30_nand_clk_cfg_ex(struct clk_hw *hw,
1943 enum tegra_clk_ex_param p, u32 setting)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001944{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301945 struct clk_tegra *c = to_clk_tegra(hw);
1946
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001947 if (p == TEGRA_CLK_NAND_PAD_DIV2_ENB) {
1948 u32 val = clk_readl(c->reg);
1949 if (setting)
1950 val |= PERIPH_CLK_NAND_DIV_EX_ENB;
1951 else
1952 val &= ~PERIPH_CLK_NAND_DIV_EX_ENB;
1953 clk_writel(val, c->reg);
1954 return 0;
1955 }
1956 return -EINVAL;
1957}
1958
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301959int tegra30_dtv_clk_cfg_ex(struct clk_hw *hw,
1960 enum tegra_clk_ex_param p, u32 setting)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001961{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301962 struct clk_tegra *c = to_clk_tegra(hw);
1963
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001964 if (p == TEGRA_CLK_DTV_INVERT) {
1965 u32 val = clk_readl(c->reg);
1966 if (setting)
1967 val |= PERIPH_CLK_DTV_POLARITY_INV;
1968 else
1969 val &= ~PERIPH_CLK_DTV_POLARITY_INV;
1970 clk_writel(val, c->reg);
1971 return 0;
1972 }
1973 return -EINVAL;
1974}
1975
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001976/* Output clock ops */
1977
1978static DEFINE_SPINLOCK(clk_out_lock);
1979
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301980static int tegra30_clk_out_is_enabled(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001981{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301982 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001983 u32 val = pmc_readl(c->reg);
1984
1985 c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
1986 c->mul = 1;
1987 c->div = 1;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301988 return c->state;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001989}
1990
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301991static int tegra30_clk_out_enable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001992{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301993 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001994 u32 val;
1995 unsigned long flags;
1996
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00001997 spin_lock_irqsave(&clk_out_lock, flags);
1998 val = pmc_readl(c->reg);
1999 val |= (0x1 << c->u.periph.clk_num);
2000 pmc_writel(val, c->reg);
2001 spin_unlock_irqrestore(&clk_out_lock, flags);
2002
2003 return 0;
2004}
2005
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302006static void tegra30_clk_out_disable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002007{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302008 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002009 u32 val;
2010 unsigned long flags;
2011
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002012 spin_lock_irqsave(&clk_out_lock, flags);
2013 val = pmc_readl(c->reg);
2014 val &= ~(0x1 << c->u.periph.clk_num);
2015 pmc_writel(val, c->reg);
2016 spin_unlock_irqrestore(&clk_out_lock, flags);
2017}
2018
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302019static int tegra30_clk_out_set_parent(struct clk_hw *hw, u8 index)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002020{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302021 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002022 u32 val;
2023 unsigned long flags;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002024
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302025 spin_lock_irqsave(&clk_out_lock, flags);
2026 val = pmc_readl(c->reg);
2027 val &= ~periph_clk_source_mask(c);
2028 val |= (index << periph_clk_source_shift(c));
2029 pmc_writel(val, c->reg);
2030 spin_unlock_irqrestore(&clk_out_lock, flags);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002031
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302032 return 0;
2033}
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002034
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302035static u8 tegra30_clk_out_get_parent(struct clk_hw *hw)
2036{
2037 struct clk_tegra *c = to_clk_tegra(hw);
2038 u32 val = pmc_readl(c->reg);
2039 int source;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002040
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302041 source = (val & periph_clk_source_mask(c)) >>
2042 periph_clk_source_shift(c);
2043 return source;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002044}
2045
Prashant Gaikwad88e790a2012-08-06 11:57:39 +05302046struct clk_ops tegra_clk_out_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302047 .is_enabled = tegra30_clk_out_is_enabled,
2048 .enable = tegra30_clk_out_enable,
2049 .disable = tegra30_clk_out_disable,
2050 .set_parent = tegra30_clk_out_set_parent,
2051 .get_parent = tegra30_clk_out_get_parent,
2052 .recalc_rate = tegra30_clk_fixed_recalc_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002053};
2054
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002055/* Clock doubler ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302056static int tegra30_clk_double_is_enabled(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002057{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302058 struct clk_tegra *c = to_clk_tegra(hw);
2059
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002060 c->state = ON;
2061 if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
2062 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302063 return c->state;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002064};
2065
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302066static int tegra30_clk_double_set_rate(struct clk_hw *hw, unsigned long rate,
2067 unsigned long parent_rate)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002068{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302069 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002070 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302071
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002072 if (rate == parent_rate) {
2073 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
2074 clk_writel(val, c->reg);
2075 c->mul = 1;
2076 c->div = 1;
2077 return 0;
2078 } else if (rate == 2 * parent_rate) {
2079 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
2080 clk_writel(val, c->reg);
2081 c->mul = 2;
2082 c->div = 1;
2083 return 0;
2084 }
2085 return -EINVAL;
2086}
2087
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302088static unsigned long tegra30_clk_double_recalc_rate(struct clk_hw *hw,
2089 unsigned long parent_rate)
2090{
2091 struct clk_tegra *c = to_clk_tegra(hw);
2092 u64 rate = parent_rate;
2093
2094 u32 val = clk_readl(c->reg);
2095 c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
2096 c->div = 1;
2097
2098 if (c->mul != 0 && c->div != 0) {
2099 rate *= c->mul;
2100 rate += c->div - 1; /* round up */
2101 do_div(rate, c->div);
2102 }
2103
2104 return rate;
2105}
2106
2107static long tegra30_clk_double_round_rate(struct clk_hw *hw, unsigned long rate,
2108 unsigned long *prate)
2109{
2110 unsigned long output_rate = *prate;
2111
2112 do_div(output_rate, 2);
2113 return output_rate;
2114}
2115
Prashant Gaikwad88e790a2012-08-06 11:57:39 +05302116struct clk_ops tegra30_clk_double_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302117 .is_enabled = tegra30_clk_double_is_enabled,
2118 .enable = tegra30_periph_clk_enable,
2119 .disable = tegra30_periph_clk_disable,
2120 .recalc_rate = tegra30_clk_double_recalc_rate,
2121 .round_rate = tegra30_clk_double_round_rate,
2122 .set_rate = tegra30_clk_double_set_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002123};
2124
2125/* Audio sync clock ops */
Prashant Gaikwad88e790a2012-08-06 11:57:39 +05302126struct clk_ops tegra_sync_source_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302127 .recalc_rate = tegra30_clk_fixed_recalc_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002128};
2129
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302130static int tegra30_audio_sync_clk_is_enabled(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002131{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302132 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002133 u32 val = clk_readl(c->reg);
2134 c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302135 return c->state;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002136}
2137
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302138static int tegra30_audio_sync_clk_enable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002139{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302140 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002141 u32 val = clk_readl(c->reg);
2142 clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
2143 return 0;
2144}
2145
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302146static void tegra30_audio_sync_clk_disable(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002147{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302148 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002149 u32 val = clk_readl(c->reg);
2150 clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
2151}
2152
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302153static int tegra30_audio_sync_clk_set_parent(struct clk_hw *hw, u8 index)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002154{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302155 struct clk_tegra *c = to_clk_tegra(hw);
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002156 u32 val;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002157
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302158 val = clk_readl(c->reg);
2159 val &= ~AUDIO_SYNC_SOURCE_MASK;
2160 val |= index;
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002161
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002162 clk_writel(val, c->reg);
2163 return 0;
2164}
2165
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302166static u8 tegra30_audio_sync_clk_get_parent(struct clk_hw *hw)
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002167{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302168 struct clk_tegra *c = to_clk_tegra(hw);
2169 u32 val = clk_readl(c->reg);
2170 int source;
2171
2172 source = val & AUDIO_SYNC_SOURCE_MASK;
2173 return source;
2174}
2175
2176struct clk_ops tegra30_audio_sync_clk_ops = {
2177 .is_enabled = tegra30_audio_sync_clk_is_enabled,
2178 .enable = tegra30_audio_sync_clk_enable,
2179 .disable = tegra30_audio_sync_clk_disable,
2180 .set_parent = tegra30_audio_sync_clk_set_parent,
2181 .get_parent = tegra30_audio_sync_clk_get_parent,
2182 .recalc_rate = tegra30_clk_fixed_recalc_rate,
2183};
2184
2185/* cml0 (pcie), and cml1 (sata) clock ops */
2186static int tegra30_cml_clk_is_enabled(struct clk_hw *hw)
2187{
2188 struct clk_tegra *c = to_clk_tegra(hw);
2189 u32 val = clk_readl(c->reg);
2190 c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
2191 return c->state;
2192}
2193
2194static int tegra30_cml_clk_enable(struct clk_hw *hw)
2195{
2196 struct clk_tegra *c = to_clk_tegra(hw);
2197
2198 u32 val = clk_readl(c->reg);
2199 val |= (0x1 << c->u.periph.clk_num);
2200 clk_writel(val, c->reg);
2201
2202 return 0;
2203}
2204
2205static void tegra30_cml_clk_disable(struct clk_hw *hw)
2206{
2207 struct clk_tegra *c = to_clk_tegra(hw);
2208
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002209 u32 val = clk_readl(c->reg);
2210 val &= ~(0x1 << c->u.periph.clk_num);
2211 clk_writel(val, c->reg);
2212}
2213
Prashant Gaikwad88e790a2012-08-06 11:57:39 +05302214struct clk_ops tegra_cml_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302215 .is_enabled = tegra30_cml_clk_is_enabled,
2216 .enable = tegra30_cml_clk_enable,
2217 .disable = tegra30_cml_clk_disable,
2218 .recalc_rate = tegra30_clk_fixed_recalc_rate,
2219};
2220
2221struct clk_ops tegra_pciex_clk_ops = {
2222 .recalc_rate = tegra30_clk_fixed_recalc_rate,
Peter De Schrijverd9cc6f72012-01-09 05:35:12 +00002223};