]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - arch/arm/mach-tegra/tegra3_clocks.c
ARM: tegra: clock: Add Tegra3 EMC activity monitor support
[linux-2.6.git] / arch / arm / mach-tegra / tegra3_clocks.c
1 /*
2  * arch/arm/mach-tegra/tegra3_clocks.c
3  *
4  * Copyright (C) 2010-2011 NVIDIA Corporation
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; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  *
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
28 #include <linux/io.h>
29 #include <linux/clk.h>
30 #include <linux/cpufreq.h>
31
32 #include <asm/clkdev.h>
33
34 #include <mach/iomap.h>
35 #include <mach/edp.h>
36
37 #include "clock.h"
38 #include "fuse.h"
39 #include "dvfs.h"
40 #include "pm.h"
41 #include "sleep.h"
42
43 #define RST_DEVICES_L                   0x004
44 #define RST_DEVICES_H                   0x008
45 #define RST_DEVICES_U                   0x00C
46 #define RST_DEVICES_V                   0x358
47 #define RST_DEVICES_W                   0x35C
48 #define RST_DEVICES_SET_L               0x300
49 #define RST_DEVICES_CLR_L               0x304
50 #define RST_DEVICES_SET_V               0x430
51 #define RST_DEVICES_CLR_V               0x434
52 #define RST_DEVICES_NUM                 5
53
54 #define CLK_OUT_ENB_L                   0x010
55 #define CLK_OUT_ENB_H                   0x014
56 #define CLK_OUT_ENB_U                   0x018
57 #define CLK_OUT_ENB_V                   0x360
58 #define CLK_OUT_ENB_W                   0x364
59 #define CLK_OUT_ENB_SET_L               0x320
60 #define CLK_OUT_ENB_CLR_L               0x324
61 #define CLK_OUT_ENB_SET_V               0x440
62 #define CLK_OUT_ENB_CLR_V               0x444
63 #define CLK_OUT_ENB_NUM                 5
64
65 #define RST_DEVICES_V_SWR_CPULP_RST_DIS (0x1 << 1)
66 #define CLK_OUT_ENB_V_CLK_ENB_CPULP_EN  (0x1 << 1)
67
68 #define PERIPH_CLK_TO_BIT(c)            (1 << (c->u.periph.clk_num % 32))
69 #define PERIPH_CLK_TO_RST_REG(c)        \
70         periph_clk_to_reg((c), RST_DEVICES_L, RST_DEVICES_V, 4)
71 #define PERIPH_CLK_TO_RST_SET_REG(c)    \
72         periph_clk_to_reg((c), RST_DEVICES_SET_L, RST_DEVICES_SET_V, 8)
73 #define PERIPH_CLK_TO_RST_CLR_REG(c)    \
74         periph_clk_to_reg((c), RST_DEVICES_CLR_L, RST_DEVICES_CLR_V, 8)
75
76 #define PERIPH_CLK_TO_ENB_REG(c)        \
77         periph_clk_to_reg((c), CLK_OUT_ENB_L, CLK_OUT_ENB_V, 4)
78 #define PERIPH_CLK_TO_ENB_SET_REG(c)    \
79         periph_clk_to_reg((c), CLK_OUT_ENB_SET_L, CLK_OUT_ENB_SET_V, 8)
80 #define PERIPH_CLK_TO_ENB_CLR_REG(c)    \
81         periph_clk_to_reg((c), CLK_OUT_ENB_CLR_L, CLK_OUT_ENB_CLR_V, 8)
82
83 #define CLK_MASK_ARM                    0x44
84 #define MISC_CLK_ENB                    0x48
85
86 #define OSC_CTRL                        0x50
87 #define OSC_CTRL_OSC_FREQ_MASK          (0xF<<28)
88 #define OSC_CTRL_OSC_FREQ_13MHZ         (0x0<<28)
89 #define OSC_CTRL_OSC_FREQ_19_2MHZ       (0x4<<28)
90 #define OSC_CTRL_OSC_FREQ_12MHZ         (0x8<<28)
91 #define OSC_CTRL_OSC_FREQ_26MHZ         (0xC<<28)
92 #define OSC_CTRL_OSC_FREQ_16_8MHZ       (0x1<<28)
93 #define OSC_CTRL_OSC_FREQ_38_4MHZ       (0x5<<28)
94 #define OSC_CTRL_OSC_FREQ_48MHZ         (0x9<<28)
95 #define OSC_CTRL_MASK                   (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
96
97 #define OSC_CTRL_PLL_REF_DIV_MASK       (3<<26)
98 #define OSC_CTRL_PLL_REF_DIV_1          (0<<26)
99 #define OSC_CTRL_PLL_REF_DIV_2          (1<<26)
100 #define OSC_CTRL_PLL_REF_DIV_4          (2<<26)
101
102 #define OSC_FREQ_DET                    0x58
103 #define OSC_FREQ_DET_TRIG               (1<<31)
104
105 #define OSC_FREQ_DET_STATUS             0x5C
106 #define OSC_FREQ_DET_BUSY               (1<<31)
107 #define OSC_FREQ_DET_CNT_MASK           0xFFFF
108
109 #define PERIPH_CLK_SOURCE_I2S1          0x100
110 #define PERIPH_CLK_SOURCE_EMC           0x19c
111 #define PERIPH_CLK_SOURCE_OSC           0x1fc
112 #define PERIPH_CLK_SOURCE_NUM1 \
113         ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
114
115 #define PERIPH_CLK_SOURCE_G3D2          0x3b0
116 #define PERIPH_CLK_SOURCE_SE            0x42c
117 #define PERIPH_CLK_SOURCE_NUM2 \
118         ((PERIPH_CLK_SOURCE_SE - PERIPH_CLK_SOURCE_G3D2) / 4 + 1)
119
120 #define PERIPH_CLK_SOURCE_NUM           (PERIPH_CLK_SOURCE_NUM1 + \
121                                          PERIPH_CLK_SOURCE_NUM2)
122
123 #define CPU_SOFTRST_CTRL                0x380
124
125 #define PERIPH_CLK_SOURCE_DIVU71_MASK   0xFF
126 #define PERIPH_CLK_SOURCE_DIVU16_MASK   0xFFFF
127 #define PERIPH_CLK_SOURCE_DIV_SHIFT     0
128 #define PERIPH_CLK_UART_DIV_ENB         (1<<24)
129 #define PERIPH_CLK_VI_SEL_EX_SHIFT      24
130 #define PERIPH_CLK_VI_SEL_EX_MASK       (0x3<<PERIPH_CLK_VI_SEL_EX_SHIFT)
131 #define PERIPH_CLK_NAND_DIV_EX_ENB      (1<<8)
132 #define PERIPH_CLK_DTV_POLARITY_INV     (1<<25)
133
134 #define AUDIO_SYNC_SOURCE_MASK          0x0F
135 #define AUDIO_SYNC_DISABLE_BIT          0x10
136 #define AUDIO_SYNC_TAP_NIBBLE_SHIFT(c)  ((c->reg_shift - 24) * 4)
137
138 #define PLL_BASE                        0x0
139 #define PLL_BASE_BYPASS                 (1<<31)
140 #define PLL_BASE_ENABLE                 (1<<30)
141 #define PLL_BASE_REF_ENABLE             (1<<29)
142 #define PLL_BASE_OVERRIDE               (1<<28)
143 #define PLL_BASE_LOCK                   (1<<27)
144 #define PLL_BASE_DIVP_MASK              (0x7<<20)
145 #define PLL_BASE_DIVP_SHIFT             20
146 #define PLL_BASE_DIVN_MASK              (0x3FF<<8)
147 #define PLL_BASE_DIVN_SHIFT             8
148 #define PLL_BASE_DIVM_MASK              (0x1F)
149 #define PLL_BASE_DIVM_SHIFT             0
150
151 #define PLL_OUT_RATIO_MASK              (0xFF<<8)
152 #define PLL_OUT_RATIO_SHIFT             8
153 #define PLL_OUT_OVERRIDE                (1<<2)
154 #define PLL_OUT_CLKEN                   (1<<1)
155 #define PLL_OUT_RESET_DISABLE           (1<<0)
156
157 #define PLL_MISC(c)                     \
158         (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
159 #define PLL_MISC_LOCK_ENABLE(c) \
160         (((c)->flags & (PLLU | PLLD)) ? (1<<22) : (1<<18))
161
162 #define PLL_MISC_DCCON_SHIFT            20
163 #define PLL_MISC_CPCON_SHIFT            8
164 #define PLL_MISC_CPCON_MASK             (0xF<<PLL_MISC_CPCON_SHIFT)
165 #define PLL_MISC_LFCON_SHIFT            4
166 #define PLL_MISC_LFCON_MASK             (0xF<<PLL_MISC_LFCON_SHIFT)
167 #define PLL_MISC_VCOCON_SHIFT           0
168 #define PLL_MISC_VCOCON_MASK            (0xF<<PLL_MISC_VCOCON_SHIFT)
169 #define PLLD_MISC_CLKENABLE             (1<<30)
170
171 #define PLLU_BASE_POST_DIV              (1<<20)
172
173 #define PLLD_BASE_DSIB_MUX_SHIFT        25
174 #define PLLD_BASE_DSIB_MUX_MASK         (1<<PLLD_BASE_DSIB_MUX_SHIFT)
175 #define PLLD_BASE_CSI_CLKENABLE         (1<<26)
176 #define PLLD_MISC_DSI_CLKENABLE         (1<<30)
177 #define PLLD_MISC_DIV_RST               (1<<23)
178 #define PLLD_MISC_DCCON_SHIFT           12
179
180 #define PLLDU_LFCON_SET_DIVN            600
181
182 /* FIXME: OUT_OF_TABLE_CPCON per pll */
183 #define OUT_OF_TABLE_CPCON              0x8
184
185 #define SUPER_CLK_MUX                   0x00
186 #define SUPER_STATE_SHIFT               28
187 #define SUPER_STATE_MASK                (0xF << SUPER_STATE_SHIFT)
188 #define SUPER_STATE_STANDBY             (0x0 << SUPER_STATE_SHIFT)
189 #define SUPER_STATE_IDLE                (0x1 << SUPER_STATE_SHIFT)
190 #define SUPER_STATE_RUN                 (0x2 << SUPER_STATE_SHIFT)
191 #define SUPER_STATE_IRQ                 (0x3 << SUPER_STATE_SHIFT)
192 #define SUPER_STATE_FIQ                 (0x4 << SUPER_STATE_SHIFT)
193 #define SUPER_LP_DIV2_BYPASS            (0x1 << 16)
194 #define SUPER_SOURCE_MASK               0xF
195 #define SUPER_FIQ_SOURCE_SHIFT          12
196 #define SUPER_IRQ_SOURCE_SHIFT          8
197 #define SUPER_RUN_SOURCE_SHIFT          4
198 #define SUPER_IDLE_SOURCE_SHIFT         0
199
200 #define SUPER_CLK_DIVIDER               0x04
201 #define SUPER_CLOCK_DIV_U71_SHIFT       16
202 #define SUPER_CLOCK_DIV_U71_MASK        (0xff << SUPER_CLOCK_DIV_U71_SHIFT)
203 /* guarantees safe cpu backup */
204 #define SUPER_CLOCK_DIV_U71_MIN         0x2
205
206 #define BUS_CLK_DISABLE                 (1<<3)
207 #define BUS_CLK_DIV_MASK                0x3
208
209 #define PMC_CTRL                        0x0
210  #define PMC_CTRL_BLINK_ENB             (1 << 7)
211
212 #define PMC_DPD_PADS_ORIDE              0x1c
213  #define PMC_DPD_PADS_ORIDE_BLINK_ENB   (1 << 20)
214
215 #define PMC_BLINK_TIMER_DATA_ON_SHIFT   0
216 #define PMC_BLINK_TIMER_DATA_ON_MASK    0x7fff
217 #define PMC_BLINK_TIMER_ENB             (1 << 15)
218 #define PMC_BLINK_TIMER_DATA_OFF_SHIFT  16
219 #define PMC_BLINK_TIMER_DATA_OFF_MASK   0xffff
220
221 #define UTMIP_PLL_CFG2                                  0x488
222 #define UTMIP_PLL_CFG2_STABLE_COUNT(x)                  (((x) & 0xfff) << 6)
223 #define UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(x)              (((x) & 0x3f) << 18)
224 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN        (1 << 0)
225 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN        (1 << 2)
226 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN        (1 << 4)
227
228 #define UTMIP_PLL_CFG1                                  0x484
229 #define UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(x)              (((x) & 0x1f) << 27)
230 #define UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(x)               (((x) & 0xfff) << 0)
231 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN       (1 << 14)
232 #define UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN       (1 << 12)
233 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN             (1 << 16)
234
235 #define PLLE_BASE_CML_ENABLE            (1<<31)
236 #define PLLE_BASE_ENABLE                (1<<30)
237 #define PLLE_BASE_DIVCML_SHIFT          24
238 #define PLLE_BASE_DIVCML_MASK           (0xf<<PLLE_BASE_DIVCML_SHIFT)
239 #define PLLE_BASE_DIVP_SHIFT            16
240 #define PLLE_BASE_DIVP_MASK             (0x3f<<PLLE_BASE_DIVP_SHIFT)
241 #define PLLE_BASE_DIVN_SHIFT            8
242 #define PLLE_BASE_DIVN_MASK             (0xFF<<PLLE_BASE_DIVN_SHIFT)
243 #define PLLE_BASE_DIVM_SHIFT            0
244 #define PLLE_BASE_DIVM_MASK             (0xFF<<PLLE_BASE_DIVM_SHIFT)
245 #define PLLE_BASE_DIV_MASK              \
246         (PLLE_BASE_DIVCML_MASK | PLLE_BASE_DIVP_MASK | \
247          PLLE_BASE_DIVN_MASK | PLLE_BASE_DIVM_MASK)
248 #define PLLE_BASE_DIV(m, n, p, cml)             \
249          (((cml)<<PLLE_BASE_DIVCML_SHIFT) | ((p)<<PLLE_BASE_DIVP_SHIFT) | \
250           ((n)<<PLLE_BASE_DIVN_SHIFT) | ((m)<<PLLE_BASE_DIVM_SHIFT))
251
252 #define PLLE_MISC_SETUP_BASE_SHIFT      16
253 #define PLLE_MISC_SETUP_BASE_MASK       (0xFFFF<<PLLE_MISC_SETUP_BASE_SHIFT)
254 #define PLLE_MISC_READY                 (1<<15)
255 #define PLLE_MISC_LOCK                  (1<<11)
256 #define PLLE_MISC_LOCK_ENABLE           (1<<9)
257 #define PLLE_MISC_SETUP_EX_SHIFT        2
258 #define PLLE_MISC_SETUP_EX_MASK         (0x3<<PLLE_MISC_SETUP_EX_SHIFT)
259 #define PLLE_MISC_SETUP_MASK            \
260           (PLLE_MISC_SETUP_BASE_MASK | PLLE_MISC_SETUP_EX_MASK)
261 #define PLLE_MISC_SETUP_VALUE           \
262           ((0x7<<PLLE_MISC_SETUP_BASE_SHIFT) | (0x0<<PLLE_MISC_SETUP_EX_SHIFT))
263
264 #define PLLE_SS_CTRL                    0x68
265 #define PLLE_SS_INCINTRV_SHIFT          24
266 #define PLLE_SS_INCINTRV_MASK           (0x3f<<PLLE_SS_INCINTRV_SHIFT)
267 #define PLLE_SS_INC_SHIFT               16
268 #define PLLE_SS_INC_MASK                (0xff<<PLLE_SS_INC_SHIFT)
269 #define PLLE_SS_MAX_SHIFT               0
270 #define PLLE_SS_MAX_MASK                (0x1ff<<PLLE_SS_MAX_SHIFT)
271 #define PLLE_SS_COEFFICIENTS_MASK       \
272         (PLLE_SS_INCINTRV_MASK | PLLE_SS_INC_MASK | PLLE_SS_MAX_MASK)
273 #define PLLE_SS_COEFFICIENTS_12MHZ      \
274         ((0x1d<<PLLE_SS_INCINTRV_SHIFT) | (0x1<<PLLE_SS_INC_SHIFT) | \
275          (0x29<<PLLE_SS_MAX_SHIFT))
276 #define PLLE_SS_DISABLE                 ((1<<12) | (1<<11) | (1<<10))
277
278 #define PLLE_AUX                        0x48c
279 #define PLLE_AUX_PLLP_SEL               (1<<2)
280 #define PLLE_AUX_CML_SATA_ENABLE        (1<<1)
281 #define PLLE_AUX_CML_PCIE_ENABLE        (1<<0)
282
283 #define PMC_SATA_PWRGT                  0x1ac
284 #define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE  (1<<5)
285 #define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL  (1<<4)
286
287 #define ROUND_DIVIDER_UP        0
288 #define ROUND_DIVIDER_DOWN      1
289
290 /* FIXME: recommended safety delay after lock is detected */
291 #define PLL_POST_LOCK_DELAY             100
292
293 static bool detach_shared_bus;
294 module_param(detach_shared_bus, bool, 0644);
295
296 /**
297 * Structure defining the fields for USB UTMI clocks Parameters.
298 */
299 struct utmi_clk_param
300 {
301         /* Oscillator Frequency in KHz */
302         u32 osc_frequency;
303         /* UTMIP PLL Enable Delay Count  */
304         u8 enable_delay_count;
305         /* UTMIP PLL Stable count */
306         u8 stable_count;
307         /*  UTMIP PLL Active delay count */
308         u8 active_delay_count;
309         /* UTMIP PLL Xtal frequency count */
310         u8 xtal_freq_count;
311 };
312
313 static const struct utmi_clk_param utmi_parameters[] =
314 {
315 /*      OSC_FREQUENCY,  ENABLE_DLY,     STABLE_CNT,     ACTIVE_DLY,     XTAL_FREQ_CNT */
316         {13000000,      0x02,           0x33,           0x05,           0x7F},
317         {19200000,      0x03,           0x4B,           0x06,           0xBB},
318         {12000000,      0x02,           0x2F,           0x04,           0x76},
319         {26000000,      0x04,           0x66,           0x09,           0xFE},
320         {16800000,      0x03,           0x41,           0x0A,           0xA4},
321 };
322
323 static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
324 static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
325 static void __iomem *misc_gp_hidrev_base = IO_ADDRESS(TEGRA_APB_MISC_BASE);
326
327 #define MISC_GP_HIDREV                  0x804
328
329 /*
330  * Some peripheral clocks share an enable bit, so refcount the enable bits
331  * in registers CLK_ENABLE_L, ... CLK_ENABLE_W
332  */
333 static int tegra_periph_clk_enable_refcount[CLK_OUT_ENB_NUM * 32];
334
335 #define clk_writel(value, reg) \
336         __raw_writel(value, (u32)reg_clk_base + (reg))
337 #define clk_readl(reg) \
338         __raw_readl((u32)reg_clk_base + (reg))
339 #define pmc_writel(value, reg) \
340         __raw_writel(value, (u32)reg_pmc_base + (reg))
341 #define pmc_readl(reg) \
342         __raw_readl((u32)reg_pmc_base + (reg))
343 #define chipid_readl() \
344         __raw_readl((u32)misc_gp_hidrev_base + MISC_GP_HIDREV)
345
346 #define clk_writel_delay(value, reg)                                    \
347         do {                                                            \
348                 __raw_writel((value), (u32)reg_clk_base + (reg));       \
349                 udelay(2);                                              \
350         } while (0)
351
352
353 static inline int clk_set_div(struct clk *c, u32 n)
354 {
355         return clk_set_rate(c, (clk_get_rate(c->parent) + n-1) / n);
356 }
357
358 static inline u32 periph_clk_to_reg(
359         struct clk *c, u32 reg_L, u32 reg_V, int offs)
360 {
361         u32 reg = c->u.periph.clk_num / 32;
362         BUG_ON(reg >= RST_DEVICES_NUM);
363         if (reg < 3) {
364                 reg = reg_L + (reg * offs);
365         } else {
366                 reg = reg_V + ((reg - 3) * offs);
367         }
368         return reg;
369 }
370
371 unsigned long clk_measure_input_freq(void)
372 {
373         u32 clock_autodetect;
374         clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
375         do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
376         clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
377         if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
378                 return 12000000;
379         } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
380                 return 13000000;
381         } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
382                 return 19200000;
383         } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
384                 return 26000000;
385         } else if (clock_autodetect >= 1025 - 3 && clock_autodetect <= 1025 + 3) {
386                 return 16800000;
387         } else if (clock_autodetect >= 2344 - 3 && clock_autodetect <= 2344 + 3) {
388                 return 38400000;
389         } else if (clock_autodetect >= 2928 - 3 && clock_autodetect <= 2928 + 3) {
390                 return 48000000;
391         } else {
392                 pr_err("%s: Unexpected clock autodetect value %d", __func__, clock_autodetect);
393                 BUG();
394                 return 0;
395         }
396 }
397
398 static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate,
399                                  u32 flags, u32 round_mode)
400 {
401         s64 divider_u71 = parent_rate;
402         if (!rate)
403                 return -EINVAL;
404
405         if (!(flags & DIV_U71_INT))
406                 divider_u71 *= 2;
407         if (round_mode == ROUND_DIVIDER_UP)
408                 divider_u71 += rate - 1;
409         do_div(divider_u71, rate);
410         if (flags & DIV_U71_INT)
411                 divider_u71 *= 2;
412
413         if (divider_u71 - 2 < 0)
414                 return 0;
415
416         if (divider_u71 - 2 > 255)
417                 return -EINVAL;
418
419         return divider_u71 - 2;
420 }
421
422 static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
423 {
424         s64 divider_u16;
425
426         divider_u16 = parent_rate;
427         if (!rate)
428                 return -EINVAL;
429         divider_u16 += rate - 1;
430         do_div(divider_u16, rate);
431
432         if (divider_u16 - 1 < 0)
433                 return 0;
434
435         if (divider_u16 - 1 > 0xFFFF)
436                 return -EINVAL;
437
438         return divider_u16 - 1;
439 }
440
441 /* clk_m functions */
442 static unsigned long tegra3_clk_m_autodetect_rate(struct clk *c)
443 {
444         u32 osc_ctrl = clk_readl(OSC_CTRL);
445         u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
446         u32 pll_ref_div = osc_ctrl & OSC_CTRL_PLL_REF_DIV_MASK;
447
448         c->rate = clk_measure_input_freq();
449         switch (c->rate) {
450         case 12000000:
451                 auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
452                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
453                 break;
454         case 13000000:
455                 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
456                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
457                 break;
458         case 19200000:
459                 auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
460                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
461                 break;
462         case 26000000:
463                 auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
464                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
465                 break;
466         case 16800000:
467                 auto_clock_control |= OSC_CTRL_OSC_FREQ_16_8MHZ;
468                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
469                 break;
470         case 38400000:
471                 auto_clock_control |= OSC_CTRL_OSC_FREQ_38_4MHZ;
472                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_2);
473                 break;
474         case 48000000:
475                 auto_clock_control |= OSC_CTRL_OSC_FREQ_48MHZ;
476                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_4);
477                 break;
478         default:
479                 pr_err("%s: Unexpected clock rate %ld", __func__, c->rate);
480                 BUG();
481         }
482         clk_writel(auto_clock_control, OSC_CTRL);
483         return c->rate;
484 }
485
486 static void tegra3_clk_m_init(struct clk *c)
487 {
488         pr_debug("%s on clock %s\n", __func__, c->name);
489         tegra3_clk_m_autodetect_rate(c);
490 }
491
492 static int tegra3_clk_m_enable(struct clk *c)
493 {
494         pr_debug("%s on clock %s\n", __func__, c->name);
495         return 0;
496 }
497
498 static void tegra3_clk_m_disable(struct clk *c)
499 {
500         pr_debug("%s on clock %s\n", __func__, c->name);
501         BUG();
502 }
503
504 static struct clk_ops tegra_clk_m_ops = {
505         .init           = tegra3_clk_m_init,
506         .enable         = tegra3_clk_m_enable,
507         .disable        = tegra3_clk_m_disable,
508 };
509
510 static struct clk_ops tegra_clk_m_div_ops = {
511         .enable         = tegra3_clk_m_enable,
512 };
513
514 /* PLL reference divider functions */
515 static void tegra3_pll_ref_init(struct clk *c)
516 {
517         u32 pll_ref_div = clk_readl(OSC_CTRL) & OSC_CTRL_PLL_REF_DIV_MASK;
518         pr_debug("%s on clock %s\n", __func__, c->name);
519
520         switch (pll_ref_div) {
521         case OSC_CTRL_PLL_REF_DIV_1:
522                 c->div = 1;
523                 break;
524         case OSC_CTRL_PLL_REF_DIV_2:
525                 c->div = 2;
526                 break;
527         case OSC_CTRL_PLL_REF_DIV_4:
528                 c->div = 4;
529                 break;
530         default:
531                 pr_err("%s: Invalid pll ref divider %d", __func__, pll_ref_div);
532                 BUG();
533         }
534         c->mul = 1;
535         c->state = ON;
536 }
537
538 static struct clk_ops tegra_pll_ref_ops = {
539         .init           = tegra3_pll_ref_init,
540         .enable         = tegra3_clk_m_enable,
541         .disable        = tegra3_clk_m_disable,
542 };
543
544 /* super clock functions */
545 /* "super clocks" on tegra3 have two-stage muxes, fractional 7.1 divider and
546  * clock skipping super divider.  We will ignore the clock skipping divider,
547  * since we can't lower the voltage when using the clock skip, but we can if
548  * we lower the PLL frequency. We will use 7.1 divider for CPU super-clock
549  * only when its parent is a fixed rate PLL, since we can't change PLL rate
550  * in this case.
551  */
552 static void tegra3_super_clk_init(struct clk *c)
553 {
554         u32 val;
555         int source;
556         int shift;
557         const struct clk_mux_sel *sel;
558         val = clk_readl(c->reg + SUPER_CLK_MUX);
559         c->state = ON;
560         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
561                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
562         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
563                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
564         source = (val >> shift) & SUPER_SOURCE_MASK;
565         if (c->flags & DIV_2)
566                 source |= val & SUPER_LP_DIV2_BYPASS;
567         for (sel = c->inputs; sel->input != NULL; sel++) {
568                 if (sel->value == source)
569                         break;
570         }
571         BUG_ON(sel->input == NULL);
572         c->parent = sel->input;
573
574         if (c->flags & DIV_U71) {
575                 /* Init safe 7.1 divider value (does not affect PLLX path) */
576                 clk_writel(SUPER_CLOCK_DIV_U71_MIN << SUPER_CLOCK_DIV_U71_SHIFT,
577                            c->reg + SUPER_CLK_DIVIDER);
578                 c->mul = 2;
579                 c->div = 2;
580                 if (!(c->parent->flags & PLLX))
581                         c->div += SUPER_CLOCK_DIV_U71_MIN;
582         }
583         else
584                 clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
585 }
586
587 static int tegra3_super_clk_enable(struct clk *c)
588 {
589         return 0;
590 }
591
592 static void tegra3_super_clk_disable(struct clk *c)
593 {
594         /* since tegra 3 has 2 CPU super clocks - low power lp-mode clock and
595            geared up g-mode super clock - mode switch may request to disable
596            either of them; accept request with no affect on h/w */
597 }
598
599 static int tegra3_super_clk_set_parent(struct clk *c, struct clk *p)
600 {
601         u32 val;
602         const struct clk_mux_sel *sel;
603         int shift;
604
605         val = clk_readl(c->reg + SUPER_CLK_MUX);;
606         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
607                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
608         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
609                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
610         for (sel = c->inputs; sel->input != NULL; sel++) {
611                 if (sel->input == p) {
612                         /* For LP mode super-clock switch between PLLX direct
613                            and divided-by-2 outputs is allowed only when other
614                            than PLLX clock source is current parent */
615                         if ((c->flags & DIV_2) && (p->flags & PLLX) &&
616                             ((sel->value ^ val) & SUPER_LP_DIV2_BYPASS)) {
617                                 if (c->parent->flags & PLLX)
618                                         return -EINVAL;
619                                 val ^= SUPER_LP_DIV2_BYPASS;
620                                 clk_writel_delay(val, c->reg);
621                         }
622                         val &= ~(SUPER_SOURCE_MASK << shift);
623                         val |= (sel->value & SUPER_SOURCE_MASK) << shift;
624
625                         /* 7.1 divider for CPU super-clock does not affect
626                            PLLX path */
627                         if (c->flags & DIV_U71) {
628                                 u32 div = 0;
629                                 if (!(p->flags & PLLX)) {
630                                         div = clk_readl(c->reg +
631                                                         SUPER_CLK_DIVIDER);
632                                         div &= SUPER_CLOCK_DIV_U71_MASK;
633                                         div >>= SUPER_CLOCK_DIV_U71_SHIFT;
634                                 }
635                                 c->div = div + 2;
636                                 c->mul = 2;
637                         }
638
639                         if (c->refcnt)
640                                 clk_enable(p);
641
642                         clk_writel_delay(val, c->reg);
643
644                         if (c->refcnt && c->parent)
645                                 clk_disable(c->parent);
646
647                         clk_reparent(c, p);
648                         return 0;
649                 }
650         }
651         return -EINVAL;
652 }
653
654 /*
655  * Do not use super clocks "skippers", since dividing using a clock skipper
656  * does not allow the voltage to be scaled down. Instead adjust the rate of
657  * the parent clock. This requires that the parent of a super clock have no
658  * other children, otherwise the rate will change underneath the other
659  * children. Special case: if fixed rate PLL is CPU super clock parent the
660  * rate of this PLL can't be changed, and it has many other children. In
661  * this case use 7.1 fractional divider to adjust the super clock rate.
662  */
663 static int tegra3_super_clk_set_rate(struct clk *c, unsigned long rate)
664 {
665         if ((c->flags & DIV_U71) && (c->parent->flags & PLL_FIXED)) {
666                 int div = clk_div71_get_divider(c->parent->u.pll.fixed_rate,
667                                         rate, c->flags, ROUND_DIVIDER_DOWN);
668                 div = max(div, SUPER_CLOCK_DIV_U71_MIN);
669
670                 clk_writel(div << SUPER_CLOCK_DIV_U71_SHIFT,
671                            c->reg + SUPER_CLK_DIVIDER);
672                 c->div = div + 2;
673                 c->mul = 2;
674                 return 0;
675         }
676         return clk_set_rate(c->parent, rate);
677 }
678
679 static struct clk_ops tegra_super_ops = {
680         .init                   = tegra3_super_clk_init,
681         .enable                 = tegra3_super_clk_enable,
682         .disable                = tegra3_super_clk_disable,
683         .set_parent             = tegra3_super_clk_set_parent,
684         .set_rate               = tegra3_super_clk_set_rate,
685 };
686
687 static int tegra3_twd_clk_set_rate(struct clk *c, unsigned long rate)
688 {
689         /* The input value 'rate' is the clock rate of the CPU complex. */
690         c->rate = (rate * c->mul) / c->div;
691         return 0;
692 }
693
694 static struct clk_ops tegra3_twd_ops = {
695         .set_rate       = tegra3_twd_clk_set_rate,
696 };
697
698 static struct clk tegra3_clk_twd = {
699         /* NOTE: The twd clock must have *NO* parent. It's rate is directly
700                  updated by tegra3_cpu_cmplx_clk_set_rate() because the
701                  frequency change notifer for the twd is called in an
702                  atomic context which cannot take a mutex. */
703         .name     = "twd",
704         .ops      = &tegra3_twd_ops,
705         .max_rate = 1400000000, /* Same as tegra_clk_cpu_cmplx.max_rate */
706         .mul      = 1,
707         .div      = 2,
708 };
709
710 /* virtual cpu clock functions */
711 /* some clocks can not be stopped (cpu, memory bus) while the SoC is running.
712    To change the frequency of these clocks, the parent pll may need to be
713    reprogrammed, so the clock must be moved off the pll, the pll reprogrammed,
714    and then the clock moved back to the pll.  To hide this sequence, a virtual
715    clock handles it.
716  */
717 static void tegra3_cpu_clk_init(struct clk *c)
718 {
719         c->state = (!is_lp_cluster() == (c->u.cpu.mode == MODE_G))? ON : OFF;
720 }
721
722 static int tegra3_cpu_clk_enable(struct clk *c)
723 {
724         return 0;
725 }
726
727 static void tegra3_cpu_clk_disable(struct clk *c)
728 {
729         /* since tegra 3 has 2 virtual CPU clocks - low power lp-mode clock
730            and geared up g-mode clock - mode switch may request to disable
731            either of them; accept request with no affect on h/w */
732 }
733
734 static int tegra3_cpu_clk_set_rate(struct clk *c, unsigned long rate)
735 {
736         int ret = 0;
737
738         /* Hardware clock control is not possible on FPGA platforms.
739            Report success so that upper level layers don't complain
740            needlessly. */
741 #ifndef CONFIG_TEGRA_FPGA_PLATFORM
742         if (c->dvfs) {
743                 if (!c->dvfs->dvfs_rail)
744                         return -ENOSYS;
745                 else if ((!c->dvfs->dvfs_rail->reg) &&
746                           (clk_get_rate_locked(c) < rate)) {
747                         WARN(1, "Increasing CPU rate while regulator is not"
748                                 " ready may overclock CPU\n");
749                         return -ENOSYS;
750                 }
751         }
752
753         /*
754          * Take an extra reference to the main pll so it doesn't turn
755          * off when we move the cpu off of it
756          */
757         clk_enable(c->u.cpu.main);
758
759         ret = clk_set_parent(c->parent, c->u.cpu.backup);
760         if (ret) {
761                 pr_err("Failed to switch cpu to clock %s\n", c->u.cpu.backup->name);
762                 goto out;
763         }
764
765         ret = clk_set_rate(c->parent, rate);
766         if (!ret && (rate <= clk_get_rate(c->parent)))
767                 goto out;
768
769         if (rate != clk_get_rate(c->u.cpu.main)) {
770                 ret = clk_set_rate(c->u.cpu.main, rate);
771                 if (ret) {
772                         pr_err("Failed to change cpu pll to %lu\n", rate);
773                         goto out;
774                 }
775         }
776
777         ret = clk_set_parent(c->parent, c->u.cpu.main);
778         if (ret) {
779                 pr_err("Failed to switch cpu to clock %s\n", c->u.cpu.main->name);
780                 goto out;
781         }
782
783 out:
784         clk_disable(c->u.cpu.main);
785 #endif
786         return ret;
787 }
788
789 static struct clk_ops tegra_cpu_ops = {
790         .init     = tegra3_cpu_clk_init,
791         .enable   = tegra3_cpu_clk_enable,
792         .disable  = tegra3_cpu_clk_disable,
793         .set_rate = tegra3_cpu_clk_set_rate,
794 };
795
796
797 static void tegra3_cpu_cmplx_clk_init(struct clk *c)
798 {
799         int i = !!is_lp_cluster();
800
801         BUG_ON(c->inputs[0].input->u.cpu.mode != MODE_G);
802         BUG_ON(c->inputs[1].input->u.cpu.mode != MODE_LP);
803         c->parent = c->inputs[i].input;
804 }
805
806 /* cpu complex clock provides second level vitualization (on top of
807    cpu virtual cpu rate control) in order to hide the CPU mode switch
808    sequence */
809 #if PARAMETERIZE_CLUSTER_SWITCH
810 static unsigned int switch_delay;
811 static unsigned int switch_flags;
812 static DEFINE_SPINLOCK(parameters_lock);
813
814 void tegra_cluster_switch_set_parameters(unsigned int us, unsigned int flags)
815 {
816         spin_lock(&parameters_lock);
817         switch_delay = us;
818         switch_flags = flags;
819         spin_unlock(&parameters_lock);
820 }
821 #endif
822
823 static int tegra3_cpu_cmplx_clk_enable(struct clk *c)
824 {
825         return 0;
826 }
827
828 static void tegra3_cpu_cmplx_clk_disable(struct clk *c)
829 {
830         pr_debug("%s on clock %s\n", __func__, c->name);
831
832         /* oops - don't disable the CPU complex clock! */
833         BUG();
834 }
835
836 static int tegra3_cpu_cmplx_clk_set_rate(struct clk *c, unsigned long rate)
837 {
838         unsigned long flags;
839         int ret;
840         struct clk *parent = c->parent;
841
842         if (!parent->ops || !parent->ops->set_rate)
843                 return -ENOSYS;
844
845         clk_lock_save(parent, &flags);
846
847         ret = clk_set_rate_locked(parent, rate);
848
849         /* We can't parent the twd to directly to the CPU complex because
850            the TWD frequency update notifier is called in an atomic context
851            and the CPU frequency update requires a mutex. Update the twd
852            clock rate with the new CPU complex rate. */
853         clk_set_rate(&tegra3_clk_twd, clk_get_rate_locked(parent));
854
855         clk_unlock_restore(parent, &flags);
856
857         return ret;
858 }
859
860 static int tegra3_cpu_cmplx_clk_set_parent(struct clk *c, struct clk *p)
861 {
862         int ret;
863         unsigned int flags, delay;
864         const struct clk_mux_sel *sel;
865         unsigned long rate = clk_get_rate(c->parent);
866
867         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
868         BUG_ON(c->parent->u.cpu.mode != (is_lp_cluster() ? MODE_LP : MODE_G));
869
870         for (sel = c->inputs; sel->input != NULL; sel++) {
871                 if (sel->input == p)
872                         break;
873         }
874         if (!sel->input)
875                 return -EINVAL;
876
877 #if PARAMETERIZE_CLUSTER_SWITCH
878         spin_lock(&parameters_lock);
879         flags = switch_flags;
880         delay = switch_delay;
881         switch_flags = 0;
882         spin_unlock(&parameters_lock);
883
884         if (flags) {
885                 /* over-clocking after the switch - allow, but lower rate */
886                 if (rate > p->max_rate) {
887                         rate = p->max_rate;
888                         ret = clk_set_rate(c->parent, rate);
889                         if (ret) {
890                                 pr_err("%s: Failed to set rate %lu for %s\n",
891                                         __func__, rate, p->name);
892                                 return ret;
893                         }
894                 }
895         } else
896 #endif
897         {
898                 if (p == c->parent)             /* already switched - exit*/
899                         return 0;
900
901                 if (rate > p->max_rate) {       /* over-clocking - no switch */
902                         pr_warn("%s: No %s mode switch to %s at rate %lu\n",
903                                  __func__, c->name, p->name, rate);
904                         return -ECANCELED;
905                 }
906                 flags = TEGRA_POWER_CLUSTER_IMMEDIATE;
907                 delay = 0;
908         }
909         flags |= (p->u.cpu.mode == MODE_LP) ? TEGRA_POWER_CLUSTER_LP :
910                 TEGRA_POWER_CLUSTER_G;
911
912         /* Since in both LP and G mode CPU main and backup sources are the
913            same, set rate on the new parent just synchronizes super-clock
914            muxes before mode switch with no PLL re-locking */
915         ret = clk_set_rate(p, rate);
916         if (ret) {
917                 pr_err("%s: Failed to set rate %lu for %s\n",
918                        __func__, rate, p->name);
919                 return ret;
920         }
921
922         /* Enabling new parent scales new mode voltage rail in advanvce
923            before the switch happens*/
924         if (c->refcnt)
925                 clk_enable(p);
926
927         /* switch CPU mode */
928         ret = tegra_cluster_control(delay, flags);
929         if (ret) {
930                 if (c->refcnt)
931                         clk_disable(p);
932                 pr_err("%s: Failed to switch %s mode to %s\n",
933                        __func__, c->name, p->name);
934                 return ret;
935         }
936
937         /* Disabling old parent scales old mode voltage rail */
938         if (c->refcnt && c->parent)
939                 clk_disable(c->parent);
940
941         clk_reparent(c, p);
942         return 0;
943 }
944
945 static long tegra3_cpu_cmplx_round_rate(struct clk *c,
946         unsigned long rate)
947 {
948         if (rate > c->parent->max_rate)
949                 rate = c->parent->max_rate;
950         else if (rate < c->parent->min_rate)
951                 rate = c->parent->min_rate;
952         return rate;
953 }
954
955 static struct clk_ops tegra_cpu_cmplx_ops = {
956         .init     = tegra3_cpu_cmplx_clk_init,
957         .enable   = tegra3_cpu_cmplx_clk_enable,
958         .disable  = tegra3_cpu_cmplx_clk_disable,
959         .set_rate = tegra3_cpu_cmplx_clk_set_rate,
960         .set_parent = tegra3_cpu_cmplx_clk_set_parent,
961         .round_rate = tegra3_cpu_cmplx_round_rate,
962 };
963
964 /* virtual cop clock functions. Used to acquire the fake 'cop' clock to
965  * reset the COP block (i.e. AVP) */
966 static void tegra3_cop_clk_reset(struct clk *c, bool assert)
967 {
968         unsigned long reg = assert ? RST_DEVICES_SET_L : RST_DEVICES_CLR_L;
969
970         pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
971         clk_writel(1 << 1, reg);
972 }
973
974 static struct clk_ops tegra_cop_ops = {
975         .reset    = tegra3_cop_clk_reset,
976 };
977
978 /* bus clock functions */
979 static void tegra3_bus_clk_init(struct clk *c)
980 {
981         u32 val = clk_readl(c->reg);
982         c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
983         c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
984         c->mul = 1;
985 }
986
987 static int tegra3_bus_clk_enable(struct clk *c)
988 {
989         u32 val = clk_readl(c->reg);
990         val &= ~(BUS_CLK_DISABLE << c->reg_shift);
991         clk_writel(val, c->reg);
992         return 0;
993 }
994
995 static void tegra3_bus_clk_disable(struct clk *c)
996 {
997         u32 val = clk_readl(c->reg);
998         val |= BUS_CLK_DISABLE << c->reg_shift;
999         clk_writel(val, c->reg);
1000 }
1001
1002 static int tegra3_bus_clk_set_rate(struct clk *c, unsigned long rate)
1003 {
1004         u32 val = clk_readl(c->reg);
1005         unsigned long parent_rate = clk_get_rate(c->parent);
1006         int i;
1007         for (i = 1; i <= 4; i++) {
1008                 if (rate >= parent_rate / i) {
1009                         val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
1010                         val |= (i - 1) << c->reg_shift;
1011                         clk_writel(val, c->reg);
1012                         c->div = i;
1013                         c->mul = 1;
1014                         return 0;
1015                 }
1016         }
1017         return -EINVAL;
1018 }
1019
1020 static struct clk_ops tegra_bus_ops = {
1021         .init                   = tegra3_bus_clk_init,
1022         .enable                 = tegra3_bus_clk_enable,
1023         .disable                = tegra3_bus_clk_disable,
1024         .set_rate               = tegra3_bus_clk_set_rate,
1025 };
1026
1027 /* Virtual system bus complex clock is used to hide the sequence of
1028    changing sclk/hclk/pclk parents and dividers to configure requested
1029    sclk target rate. */
1030 static void tegra3_sbus_cmplx_init(struct clk *c)
1031 {
1032         unsigned long rate;
1033
1034         c->max_rate = c->parent->max_rate;
1035         c->min_rate = c->parent->min_rate;
1036
1037         /* Threshold must be an exact proper factor of low range parent,
1038            and both low/high range parents have 7.1 fractional dividers */
1039         rate = clk_get_rate(c->u.system.sclk_low->parent);
1040         if (c->u.system.threshold) {
1041                 BUG_ON(c->u.system.threshold > rate) ;
1042                 BUG_ON((rate % c->u.system.threshold) != 0);
1043         }
1044         BUG_ON(!(c->u.system.sclk_low->flags & DIV_U71));
1045         BUG_ON(!(c->u.system.sclk_high->flags & DIV_U71));
1046 }
1047
1048 static long tegra3_sbus_div_round_rate(
1049         struct clk *d, unsigned long max_rate, unsigned long rate)
1050 {
1051         int divider;
1052         unsigned long d_input = clk_get_rate(d->parent);
1053
1054         /* This special sbus round function is implemented because:
1055
1056            (a) system bus clock duty cycle requirements can not be met by
1057            1 : 1.5 divider, but we do need fractional division for better
1058            granularity - hence, special case with only 1.5 fraction skipped
1059
1060            (b) since sbus is a shared bus, and its frequency is set to the
1061            highest enabled shared_bus_user clock, the target rate should be
1062            rounded up divider ladder (if max limit allows it) - for pll_div
1063            and peripheral_div common is rounding down - special case again.
1064
1065            Note that final rate is trimmed, not rounded up to avoid multiple
1066            round spiral up. Lost 1Hz is added in tegra3_sbus_cmplx_set_rate
1067            just before actually setting divider rate */
1068
1069         if (rate > d_input / 2)
1070                 return (d_input > max_rate) ? (d_input / 2) : d_input;
1071
1072         /* round divider down => round rate up */
1073         divider = clk_div71_get_divider(
1074                 d_input, rate, d->flags, ROUND_DIVIDER_DOWN);
1075         if (divider < 0)
1076                 return divider;
1077
1078         rate = 2 * d_input / (divider + 2);
1079         if (rate > max_rate)
1080                 rate = 2 * d_input / (divider + 3);
1081
1082         return rate;
1083 }
1084
1085 static long tegra3_sbus_cmplx_round_rate(struct clk *c, unsigned long rate)
1086 {
1087         struct clk *new_parent;
1088         struct clk *new_parent_after_round;
1089
1090         new_parent = (rate <= c->u.system.threshold) ?
1091                 c->u.system.sclk_low : c->u.system.sclk_high;
1092
1093         rate = tegra3_sbus_div_round_rate(new_parent, c->max_rate, rate);
1094
1095         if(!IS_ERR_VALUE(rate)) {
1096                 new_parent_after_round = (rate <= c->u.system.threshold) ?
1097                         c->u.system.sclk_low : c->u.system.sclk_high;
1098                 /* if parent is oscillating across threshold -
1099                    use threshold as a target */
1100                 if (new_parent != new_parent_after_round)
1101                         return c->u.system.threshold;
1102         }
1103         return rate;
1104 }
1105
1106 static int tegra3_sbus_cmplx_set_rate(struct clk *c, unsigned long rate)
1107 {
1108         int ret;
1109         struct clk *new_parent;
1110
1111         /* - select the appropriate sclk parent
1112            - keep hclk at the same rate as sclk
1113            - set pclk at 1:2 rate of hclk unless pclk minimum is violated,
1114              in the latter case switch to 1:1 ratio */
1115
1116         if (rate >= c->u.system.pclk->min_rate * 2) {
1117                 ret = clk_set_div(c->u.system.pclk, 2);
1118                 if (ret) {
1119                         pr_err("Failed to set 1 : 2 pclk divider\n");
1120                         return ret;
1121                 }
1122         }
1123
1124         new_parent = (rate <= c->u.system.threshold) ?
1125                 c->u.system.sclk_low : c->u.system.sclk_high;
1126
1127         ret = clk_set_rate(new_parent, rate + 1);
1128         if (ret) {
1129                 pr_err("Failed to set sclk source %s to %lu\n",
1130                        new_parent->name, rate);
1131                 return ret;
1132         }
1133
1134         if (new_parent != clk_get_parent(c->parent)) {
1135                 ret = clk_set_parent(c->parent, new_parent);
1136                 if (ret) {
1137                         pr_err("Failed to switch sclk source to %s\n",
1138                                new_parent->name);
1139                         return ret;
1140                 }
1141         }
1142
1143         if (rate < c->u.system.pclk->min_rate * 2) {
1144                 ret = clk_set_div(c->u.system.pclk, 1);
1145                 if (ret) {
1146                         pr_err("Failed to set 1 : 1 pclk divider\n");
1147                         return ret;
1148                 }
1149         }
1150
1151         return 0;
1152 }
1153
1154 static struct clk_ops tegra_sbus_cmplx_ops = {
1155         .init = tegra3_sbus_cmplx_init,
1156         .set_rate = tegra3_sbus_cmplx_set_rate,
1157         .round_rate = tegra3_sbus_cmplx_round_rate,
1158 };
1159
1160 /* Blink output functions */
1161
1162 static void tegra3_blink_clk_init(struct clk *c)
1163 {
1164         u32 val;
1165
1166         val = pmc_readl(PMC_CTRL);
1167         c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
1168         c->mul = 1;
1169         val = pmc_readl(c->reg);
1170
1171         if (val & PMC_BLINK_TIMER_ENB) {
1172                 unsigned int on_off;
1173
1174                 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
1175                         PMC_BLINK_TIMER_DATA_ON_MASK;
1176                 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
1177                 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
1178                 on_off += val;
1179                 /* each tick in the blink timer is 4 32KHz clocks */
1180                 c->div = on_off * 4;
1181         } else {
1182                 c->div = 1;
1183         }
1184 }
1185
1186 static int tegra3_blink_clk_enable(struct clk *c)
1187 {
1188         u32 val;
1189
1190         val = pmc_readl(PMC_DPD_PADS_ORIDE);
1191         pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
1192
1193         val = pmc_readl(PMC_CTRL);
1194         pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
1195
1196         return 0;
1197 }
1198
1199 static void tegra3_blink_clk_disable(struct clk *c)
1200 {
1201         u32 val;
1202
1203         val = pmc_readl(PMC_CTRL);
1204         pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
1205
1206         val = pmc_readl(PMC_DPD_PADS_ORIDE);
1207         pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
1208 }
1209
1210 static int tegra3_blink_clk_set_rate(struct clk *c, unsigned long rate)
1211 {
1212         unsigned long parent_rate = clk_get_rate(c->parent);
1213         if (rate >= parent_rate) {
1214                 c->div = 1;
1215                 pmc_writel(0, c->reg);
1216         } else {
1217                 unsigned int on_off;
1218                 u32 val;
1219
1220                 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
1221                 c->div = on_off * 8;
1222
1223                 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
1224                         PMC_BLINK_TIMER_DATA_ON_SHIFT;
1225                 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
1226                 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
1227                 val |= on_off;
1228                 val |= PMC_BLINK_TIMER_ENB;
1229                 pmc_writel(val, c->reg);
1230         }
1231
1232         return 0;
1233 }
1234
1235 static struct clk_ops tegra_blink_clk_ops = {
1236         .init                   = &tegra3_blink_clk_init,
1237         .enable                 = &tegra3_blink_clk_enable,
1238         .disable                = &tegra3_blink_clk_disable,
1239         .set_rate               = &tegra3_blink_clk_set_rate,
1240 };
1241
1242 /* PLL Functions */
1243 static int tegra3_pll_clk_wait_for_lock(struct clk *c, u32 lock_reg, u32 lock_bit)
1244 {
1245 #if USE_PLL_LOCK_BITS
1246         int i;
1247         for (i = 0; i < c->u.pll.lock_delay; i++) {
1248                 if (clk_readl(lock_reg) & lock_bit) {
1249                         udelay(PLL_POST_LOCK_DELAY);
1250                         return 0;
1251                 }
1252                 udelay(2);              /* timeout = 2 * lock time */
1253         }
1254         pr_err("Timed out waiting for lock bit on pll %s", c->name);
1255         return -1;
1256 #endif
1257         udelay(c->u.pll.lock_delay);
1258
1259         return 0;
1260 }
1261
1262
1263 static void tegra3_utmi_param_configure(struct clk *c)
1264 {
1265         u32 reg;
1266         int i;
1267         unsigned long main_rate =
1268                 clk_get_rate(c->parent->parent);
1269
1270         for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
1271                 if (main_rate == utmi_parameters[i].osc_frequency) {
1272                         break;
1273                 }
1274         }
1275
1276         if (i >= ARRAY_SIZE(utmi_parameters)) {
1277                 pr_err("%s: Unexpected main rate %lu\n", __func__, main_rate);
1278                 return;
1279         }
1280
1281         reg = clk_readl(UTMIP_PLL_CFG2);
1282
1283         /* Program UTMIP PLL stable and active counts */
1284         /* [FIXME] arclk_rst.h says WRONG! This should be 1ms -> 0x50 Check! */
1285         reg &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
1286         reg |= UTMIP_PLL_CFG2_STABLE_COUNT(
1287                         utmi_parameters[i].stable_count);
1288
1289         reg &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
1290
1291         reg |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(
1292                         utmi_parameters[i].active_delay_count);
1293
1294         /* Remove power downs from UTMIP PLL control bits */
1295         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
1296         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
1297         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
1298
1299         clk_writel(reg, UTMIP_PLL_CFG2);
1300
1301         /* Program UTMIP PLL delay and oscillator frequency counts */
1302         reg = clk_readl(UTMIP_PLL_CFG1);
1303         reg &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
1304
1305         reg |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(
1306                 utmi_parameters[i].enable_delay_count);
1307
1308         reg &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
1309         reg |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(
1310                 utmi_parameters[i].xtal_freq_count);
1311
1312         /* Remove power downs from UTMIP PLL control bits */
1313         reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
1314         reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
1315         reg &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
1316
1317         clk_writel(reg, UTMIP_PLL_CFG1);
1318 }
1319
1320 static void tegra3_pll_clk_init(struct clk *c)
1321 {
1322         u32 val = clk_readl(c->reg + PLL_BASE);
1323
1324         c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
1325
1326         if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
1327                 const struct clk_pll_freq_table *sel;
1328                 unsigned long input_rate = clk_get_rate(c->parent);
1329                 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1330                         if (sel->input_rate == input_rate &&
1331                                 sel->output_rate == c->u.pll.fixed_rate) {
1332                                 c->mul = sel->n;
1333                                 c->div = sel->m * sel->p;
1334                                 return;
1335                         }
1336                 }
1337                 pr_err("Clock %s has unknown fixed frequency\n", c->name);
1338                 BUG();
1339         } else if (val & PLL_BASE_BYPASS) {
1340                 c->mul = 1;
1341                 c->div = 1;
1342         } else {
1343                 c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
1344                 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
1345                 if (c->flags & PLLU)
1346                         c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
1347                 else
1348                         c->div *= (0x1 << ((val & PLL_BASE_DIVP_MASK) >>
1349                                         PLL_BASE_DIVP_SHIFT));
1350                 if (c->flags & PLL_FIXED) {
1351                         unsigned long rate = clk_get_rate_locked(c);
1352                         BUG_ON(rate != c->u.pll.fixed_rate);
1353                 }
1354         }
1355
1356         if (c->flags & PLLU) {
1357                 tegra3_utmi_param_configure(c);
1358         }
1359 }
1360
1361 static int tegra3_pll_clk_enable(struct clk *c)
1362 {
1363         u32 val;
1364         pr_debug("%s on clock %s\n", __func__, c->name);
1365
1366 #if USE_PLL_LOCK_BITS
1367         val = clk_readl(c->reg + PLL_MISC(c));
1368         val |= PLL_MISC_LOCK_ENABLE(c);
1369         clk_writel(val, c->reg + PLL_MISC(c));
1370 #endif
1371         val = clk_readl(c->reg + PLL_BASE);
1372         val &= ~PLL_BASE_BYPASS;
1373         val |= PLL_BASE_ENABLE;
1374         clk_writel(val, c->reg + PLL_BASE);
1375
1376         if (c->flags & PLLD) {
1377                 val = clk_readl(c->reg + PLL_MISC(c) + PLL_BASE);
1378                 val |= PLLD_MISC_CLKENABLE;
1379                 clk_writel(val, c->reg + PLL_MISC(c) + PLL_BASE);
1380         }
1381
1382         tegra3_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
1383
1384         return 0;
1385 }
1386
1387 static void tegra3_pll_clk_disable(struct clk *c)
1388 {
1389         u32 val;
1390         pr_debug("%s on clock %s\n", __func__, c->name);
1391
1392         val = clk_readl(c->reg);
1393         val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
1394         clk_writel(val, c->reg);
1395
1396         if (c->flags & PLLD) {
1397                 val = clk_readl(c->reg + PLL_MISC(c) + PLL_BASE);
1398                 val &= ~PLLD_MISC_CLKENABLE;
1399                 clk_writel(val, c->reg + PLL_MISC(c) + PLL_BASE);
1400         }
1401 }
1402
1403 static int tegra3_pll_clk_set_rate(struct clk *c, unsigned long rate)
1404 {
1405         u32 val, p_div, old_base;
1406         unsigned long input_rate;
1407         const struct clk_pll_freq_table *sel;
1408         struct clk_pll_freq_table cfg;
1409
1410         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
1411
1412         if (c->flags & PLL_FIXED) {
1413                 int ret = 0;
1414                 if (rate != c->u.pll.fixed_rate) {
1415                         pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
1416                                __func__, c->name, c->u.pll.fixed_rate, rate);
1417                         ret = -EINVAL;
1418                 }
1419                 return ret;
1420         }
1421
1422         p_div = 0;
1423         input_rate = clk_get_rate(c->parent);
1424
1425         /* Check if the target rate is tabulated */
1426         for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1427                 if (sel->input_rate == input_rate && sel->output_rate == rate) {
1428                         if (c->flags & PLLU) {
1429                                 BUG_ON(sel->p < 1 || sel->p > 2);
1430                                 if (sel->p == 1)
1431                                         p_div = PLLU_BASE_POST_DIV;
1432                         } else {
1433                                 BUG_ON(sel->p < 1);
1434                                 for (val = sel->p; val > 1; val >>= 1, p_div++);
1435                                 p_div <<= PLL_BASE_DIVP_SHIFT;
1436                         }
1437                         break;
1438                 }
1439         }
1440
1441         /* Configure out-of-table rate */
1442         if (sel->input_rate == 0) {
1443                 unsigned long cfreq;
1444                 BUG_ON(c->flags & PLLU);
1445                 sel = &cfg;
1446
1447                 switch (input_rate) {
1448                 case 12000000:
1449                 case 26000000:
1450                         cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
1451                         break;
1452                 case 13000000:
1453                         cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
1454                         break;
1455                 case 16800000:
1456                 case 19200000:
1457                         cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
1458                         break;
1459                 default:
1460                         pr_err("%s: Unexpected reference rate %lu\n",
1461                                __func__, input_rate);
1462                         BUG();
1463                 }
1464
1465                 /* Raise VCO to guarantee 0.5% accuracy */
1466                 for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
1467                       cfg.output_rate <<= 1, p_div++);
1468
1469                 cfg.p = 0x1 << p_div;
1470                 cfg.m = input_rate / cfreq;
1471                 cfg.n = cfg.output_rate / cfreq;
1472                 cfg.cpcon = OUT_OF_TABLE_CPCON;
1473
1474                 if ((cfg.m > (PLL_BASE_DIVM_MASK >> PLL_BASE_DIVM_SHIFT)) ||
1475                     (cfg.n > (PLL_BASE_DIVN_MASK >> PLL_BASE_DIVN_SHIFT)) ||
1476                     (p_div > (PLL_BASE_DIVP_MASK >> PLL_BASE_DIVP_SHIFT)) ||
1477                     (cfg.output_rate > c->u.pll.vco_max)) {
1478                         pr_err("%s: Failed to set %s out-of-table rate %lu\n",
1479                                __func__, c->name, rate);
1480                         return -EINVAL;
1481                 }
1482                 p_div <<= PLL_BASE_DIVP_SHIFT;
1483         }
1484
1485         c->mul = sel->n;
1486         c->div = sel->m * sel->p;
1487
1488         old_base = val = clk_readl(c->reg + PLL_BASE);
1489         val &= ~(PLL_BASE_DIVM_MASK | PLL_BASE_DIVN_MASK |
1490                  ((c->flags & PLLU) ? PLLU_BASE_POST_DIV : PLL_BASE_DIVP_MASK));
1491         val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
1492                 (sel->n << PLL_BASE_DIVN_SHIFT) | p_div;
1493         if (val == old_base)
1494                 return 0;
1495
1496         if (c->state == ON) {
1497                 tegra3_pll_clk_disable(c);
1498                 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
1499         }
1500         clk_writel(val, c->reg + PLL_BASE);
1501
1502         if (c->flags & PLL_HAS_CPCON) {
1503                 val = clk_readl(c->reg + PLL_MISC(c));
1504                 val &= ~PLL_MISC_CPCON_MASK;
1505                 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
1506                 if (c->flags & (PLLU | PLLD)) {
1507                         val &= ~PLL_MISC_LFCON_MASK;
1508                         if (sel->n >= PLLDU_LFCON_SET_DIVN)
1509                                 val |= 0x1 << PLL_MISC_LFCON_SHIFT;
1510                 } else if (c->flags & (PLLX | PLLM)) {
1511                         val &= ~(0x1 << PLL_MISC_DCCON_SHIFT);
1512                         if (rate >= (c->u.pll.vco_max >> 1))
1513                                 val |= 0x1 << PLL_MISC_DCCON_SHIFT;
1514                 }
1515                 clk_writel(val, c->reg + PLL_MISC(c));
1516         }
1517
1518         if (c->state == ON)
1519                 tegra3_pll_clk_enable(c);
1520
1521         return 0;
1522 }
1523
1524 static struct clk_ops tegra_pll_ops = {
1525         .init                   = tegra3_pll_clk_init,
1526         .enable                 = tegra3_pll_clk_enable,
1527         .disable                = tegra3_pll_clk_disable,
1528         .set_rate               = tegra3_pll_clk_set_rate,
1529 };
1530
1531 static int
1532 tegra3_plld_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
1533 {
1534         u32 val, mask, reg;
1535
1536         switch (p) {
1537         case TEGRA_CLK_PLLD_CSI_OUT_ENB:
1538                 mask = PLLD_BASE_CSI_CLKENABLE;
1539                 reg = c->reg + PLL_BASE;
1540                 break;
1541         case TEGRA_CLK_PLLD_DSI_OUT_ENB:
1542                 mask = PLLD_MISC_DSI_CLKENABLE;
1543                 reg = c->reg + PLL_MISC(c);
1544                 break;
1545         case TEGRA_CLK_PLLD_MIPI_MUX_SEL:
1546                 if (!(c->flags & PLL_ALT_MISC_REG)) {
1547                         mask = PLLD_BASE_DSIB_MUX_MASK;
1548                         reg = c->reg + PLL_BASE;
1549                         break;
1550                 }
1551         /* fall through - error since PLLD2 does not have MUX_SEL control */
1552         default:
1553                 return -EINVAL;
1554         }
1555
1556         val = clk_readl(reg);
1557         if (setting)
1558                 val |= mask;
1559         else
1560                 val &= ~mask;
1561         clk_writel(val, reg);
1562         return 0;
1563 }
1564
1565 static struct clk_ops tegra_plld_ops = {
1566         .init                   = tegra3_pll_clk_init,
1567         .enable                 = tegra3_pll_clk_enable,
1568         .disable                = tegra3_pll_clk_disable,
1569         .set_rate               = tegra3_pll_clk_set_rate,
1570         .clk_cfg_ex             = tegra3_plld_clk_cfg_ex,
1571 };
1572
1573 static void tegra3_plle_clk_init(struct clk *c)
1574 {
1575         u32 val;
1576
1577         val = clk_readl(PLLE_AUX);
1578         c->parent = (val & PLLE_AUX_PLLP_SEL) ?
1579                 tegra_get_clock_by_name("pll_p") :
1580                 tegra_get_clock_by_name("pll_ref");
1581
1582         val = clk_readl(c->reg + PLL_BASE);
1583         c->state = (val & PLLE_BASE_ENABLE) ? ON : OFF;
1584         c->mul = (val & PLLE_BASE_DIVN_MASK) >> PLLE_BASE_DIVN_SHIFT;
1585         c->div = (val & PLLE_BASE_DIVM_MASK) >> PLLE_BASE_DIVM_SHIFT;
1586         c->div *= (val & PLLE_BASE_DIVP_MASK) >> PLLE_BASE_DIVP_SHIFT;
1587 }
1588
1589 static void tegra3_plle_clk_disable(struct clk *c)
1590 {
1591         u32 val;
1592         pr_debug("%s on clock %s\n", __func__, c->name);
1593
1594         val = clk_readl(c->reg + PLL_BASE);
1595         val &= ~(PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
1596         clk_writel(val, c->reg + PLL_BASE);
1597 }
1598
1599 static void tegra3_plle_training(struct clk *c)
1600 {
1601         u32 val;
1602
1603         /* PLLE is already disabled, and setup cleared;
1604          * create falling edge on PLLE IDDQ input */
1605         val = pmc_readl(PMC_SATA_PWRGT);
1606         val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
1607         pmc_writel(val, PMC_SATA_PWRGT);
1608
1609         val = pmc_readl(PMC_SATA_PWRGT);
1610         val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
1611         pmc_writel(val, PMC_SATA_PWRGT);
1612
1613         val = pmc_readl(PMC_SATA_PWRGT);
1614         val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
1615         pmc_writel(val, PMC_SATA_PWRGT);
1616
1617         do {
1618                 val = clk_readl(c->reg + PLL_MISC(c));
1619         } while (!(val & PLLE_MISC_READY));
1620 }
1621
1622 static int tegra3_plle_configure(struct clk *c, bool force_training)
1623 {
1624         u32 val;
1625         const struct clk_pll_freq_table *sel;
1626         unsigned long rate = c->u.pll.fixed_rate;
1627         unsigned long input_rate = clk_get_rate(c->parent);
1628
1629         for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
1630                 if (sel->input_rate == input_rate && sel->output_rate == rate)
1631                         break;
1632         }
1633
1634         if (sel->input_rate == 0)
1635                 return -ENOSYS;
1636
1637         /* disable PLLE, clear setup fiels */
1638         tegra3_plle_clk_disable(c);
1639
1640         val = clk_readl(c->reg + PLL_MISC(c));
1641         val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
1642         clk_writel(val, c->reg + PLL_MISC(c));
1643
1644         /* training */
1645         val = clk_readl(c->reg + PLL_MISC(c));
1646         if (force_training || (!(val & PLLE_MISC_READY)))
1647                 tegra3_plle_training(c);
1648
1649         /* configure dividers, setup, disable SS */
1650         val = clk_readl(c->reg + PLL_BASE);
1651         val &= ~PLLE_BASE_DIV_MASK;
1652         val |= PLLE_BASE_DIV(sel->m, sel->n, sel->p, sel->cpcon);
1653         clk_writel(val, c->reg + PLL_BASE);
1654         c->mul = sel->n;
1655         c->div = sel->m * sel->p;
1656
1657         val = clk_readl(c->reg + PLL_MISC(c));
1658         val |= PLLE_MISC_SETUP_VALUE;
1659         val |= PLLE_MISC_LOCK_ENABLE;
1660         clk_writel(val, c->reg + PLL_MISC(c));
1661
1662         val = clk_readl(PLLE_SS_CTRL);
1663         val |= PLLE_SS_DISABLE;
1664         clk_writel(val, PLLE_SS_CTRL);
1665
1666         /* enable and lock PLLE*/
1667         val = clk_readl(c->reg + PLL_BASE);
1668         val |= (PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
1669         clk_writel(val, c->reg + PLL_BASE);
1670
1671         tegra3_pll_clk_wait_for_lock(c, c->reg + PLL_MISC(c), PLLE_MISC_LOCK);
1672
1673 #if USE_PLLE_SS
1674         /* configure spread spectrum coefficients */
1675         /* FIXME: coefficients for 216MHZ input? */
1676 #ifndef CONFIG_TEGRA_FPGA_PLATFORM
1677         if (input_rate == 12000000)
1678 #endif
1679         {
1680                 val = clk_readl(PLLE_SS_CTRL);
1681                 val &= ~(PLLE_SS_COEFFICIENTS_MASK | PLLE_SS_DISABLE);
1682                 val |= PLLE_SS_COEFFICIENTS_12MHZ;
1683                 clk_writel(val, PLLE_SS_CTRL);
1684         }
1685 #endif
1686         return 0;
1687 }
1688
1689 static int tegra3_plle_clk_enable(struct clk *c)
1690 {
1691         pr_debug("%s on clock %s\n", __func__, c->name);
1692         return tegra3_plle_configure(c, !c->set);
1693 }
1694
1695 static struct clk_ops tegra_plle_ops = {
1696         .init                   = tegra3_plle_clk_init,
1697         .enable                 = tegra3_plle_clk_enable,
1698         .disable                = tegra3_plle_clk_disable,
1699 };
1700
1701 /* Clock divider ops */
1702 static void tegra3_pll_div_clk_init(struct clk *c)
1703 {
1704         if (c->flags & DIV_U71) {
1705                 u32 divu71;
1706                 u32 val = clk_readl(c->reg);
1707                 val >>= c->reg_shift;
1708                 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
1709                 if (!(val & PLL_OUT_RESET_DISABLE))
1710                         c->state = OFF;
1711
1712                 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
1713                 c->div = (divu71 + 2);
1714                 c->mul = 2;
1715         } else if (c->flags & DIV_2) {
1716                 c->state = ON;
1717                 if (c->flags & (PLLD | PLLX)) {
1718                         c->div = 2;
1719                         c->mul = 1;
1720                 }
1721                 else
1722                         BUG();
1723         } else {
1724                 c->state = ON;
1725                 c->div = 1;
1726                 c->mul = 1;
1727         }
1728 }
1729
1730 static int tegra3_pll_div_clk_enable(struct clk *c)
1731 {
1732         u32 val;
1733         u32 new_val;
1734
1735         pr_debug("%s: %s\n", __func__, c->name);
1736         if (c->flags & DIV_U71) {
1737                 val = clk_readl(c->reg);
1738                 new_val = val >> c->reg_shift;
1739                 new_val &= 0xFFFF;
1740
1741                 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
1742
1743                 val &= ~(0xFFFF << c->reg_shift);
1744                 val |= new_val << c->reg_shift;
1745                 clk_writel_delay(val, c->reg);
1746                 return 0;
1747         } else if (c->flags & DIV_2) {
1748                 return 0;
1749         }
1750         return -EINVAL;
1751 }
1752
1753 static void tegra3_pll_div_clk_disable(struct clk *c)
1754 {
1755         u32 val;
1756         u32 new_val;
1757
1758         pr_debug("%s: %s\n", __func__, c->name);
1759         if (c->flags & DIV_U71) {
1760                 val = clk_readl(c->reg);
1761                 new_val = val >> c->reg_shift;
1762                 new_val &= 0xFFFF;
1763
1764                 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
1765
1766                 val &= ~(0xFFFF << c->reg_shift);
1767                 val |= new_val << c->reg_shift;
1768                 clk_writel_delay(val, c->reg);
1769         }
1770 }
1771
1772 static int tegra3_pll_div_clk_set_rate(struct clk *c, unsigned long rate)
1773 {
1774         u32 val;
1775         u32 new_val;
1776         int divider_u71;
1777         unsigned long parent_rate = clk_get_rate(c->parent);
1778
1779         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
1780         if (c->flags & DIV_U71) {
1781                 divider_u71 = clk_div71_get_divider(
1782                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1783                 if (divider_u71 >= 0) {
1784                         val = clk_readl(c->reg);
1785                         new_val = val >> c->reg_shift;
1786                         new_val &= 0xFFFF;
1787                         if (c->flags & DIV_U71_FIXED)
1788                                 new_val |= PLL_OUT_OVERRIDE;
1789                         new_val &= ~PLL_OUT_RATIO_MASK;
1790                         new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
1791
1792                         val &= ~(0xFFFF << c->reg_shift);
1793                         val |= new_val << c->reg_shift;
1794                         clk_writel_delay(val, c->reg);
1795                         c->div = divider_u71 + 2;
1796                         c->mul = 2;
1797                         return 0;
1798                 }
1799         } else if (c->flags & DIV_2)
1800                 return clk_set_rate(c->parent, rate * 2);
1801
1802         return -EINVAL;
1803 }
1804
1805 static long tegra3_pll_div_clk_round_rate(struct clk *c, unsigned long rate)
1806 {
1807         int divider;
1808         unsigned long parent_rate = clk_get_rate(c->parent);
1809         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
1810
1811         if (c->flags & DIV_U71) {
1812                 divider = clk_div71_get_divider(
1813                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
1814                 if (divider < 0)
1815                         return divider;
1816                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1817         } else if (c->flags & DIV_2)
1818                 /* no rounding - fixed DIV_2 dividers pass rate to parent PLL */
1819                 return rate;
1820
1821         return -EINVAL;
1822 }
1823
1824 static struct clk_ops tegra_pll_div_ops = {
1825         .init                   = tegra3_pll_div_clk_init,
1826         .enable                 = tegra3_pll_div_clk_enable,
1827         .disable                = tegra3_pll_div_clk_disable,
1828         .set_rate               = tegra3_pll_div_clk_set_rate,
1829         .round_rate             = tegra3_pll_div_clk_round_rate,
1830 };
1831
1832 /* Periph clk ops */
1833 static inline u32 periph_clk_source_mask(struct clk *c)
1834 {
1835         if (c->flags & MUX8)
1836                  return 7 << 29;
1837         else if (c->flags & MUX_PWM)
1838                 return 3 << 28;
1839         else if (c->flags & MUX_CLK_OUT)
1840                 return 3 << (c->u.periph.clk_num + 4);
1841         else if (c->flags & PLLD)
1842                 return PLLD_BASE_DSIB_MUX_MASK;
1843         else
1844                 return 3 << 30;
1845 }
1846
1847 static inline u32 periph_clk_source_shift(struct clk *c)
1848 {
1849         if (c->flags & MUX8)
1850                  return 29;
1851         else if (c->flags & MUX_PWM)
1852                 return 28;
1853         else if (c->flags & MUX_CLK_OUT)
1854                 return c->u.periph.clk_num + 4;
1855         else if (c->flags & PLLD)
1856                 return PLLD_BASE_DSIB_MUX_SHIFT;
1857         else
1858                 return 30;
1859 }
1860
1861 static void tegra3_periph_clk_init(struct clk *c)
1862 {
1863         u32 val = clk_readl(c->reg);
1864         const struct clk_mux_sel *mux = 0;
1865         const struct clk_mux_sel *sel;
1866         if (c->flags & MUX) {
1867                 for (sel = c->inputs; sel->input != NULL; sel++) {
1868                         if (((val & periph_clk_source_mask(c)) >>
1869                             periph_clk_source_shift(c)) == sel->value)
1870                                 mux = sel;
1871                 }
1872                 BUG_ON(!mux);
1873
1874                 c->parent = mux->input;
1875         } else {
1876                 c->parent = c->inputs[0].input;
1877         }
1878
1879         if (c->flags & DIV_U71) {
1880                 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
1881                 if ((c->flags & DIV_U71_UART) &&
1882                     (!(val & PERIPH_CLK_UART_DIV_ENB))) {
1883                         divu71 = 0;
1884                 }
1885                 c->div = divu71 + 2;
1886                 c->mul = 2;
1887         } else if (c->flags & DIV_U16) {
1888                 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
1889                 c->div = divu16 + 1;
1890                 c->mul = 1;
1891         } else {
1892                 c->div = 1;
1893                 c->mul = 1;
1894         }
1895
1896         c->state = ON;
1897         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
1898                 c->state = OFF;
1899         if (!(c->flags & PERIPH_NO_RESET))
1900                 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
1901                         c->state = OFF;
1902 }
1903
1904 static int tegra3_periph_clk_enable(struct clk *c)
1905 {
1906         pr_debug("%s on clock %s\n", __func__, c->name);
1907
1908         tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
1909         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
1910                 return 0;
1911
1912         clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_SET_REG(c));
1913         if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET)) {
1914                 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c)) {
1915                         udelay(5);      /* reset propagation delay */
1916                         clk_writel(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_RST_CLR_REG(c));
1917                 }
1918         }
1919         return 0;
1920 }
1921
1922 static void tegra3_periph_clk_disable(struct clk *c)
1923 {
1924         unsigned long val;
1925         pr_debug("%s on clock %s\n", __func__, c->name);
1926
1927         if (c->refcnt)
1928                 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
1929
1930         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] == 0) {
1931                 /* If peripheral is in the APB bus then read the APB bus to
1932                  * flush the write operation in apb bus. This will avoid the
1933                  * peripheral access after disabling clock*/
1934                 if (c->flags & PERIPH_ON_APB)
1935                         val = chipid_readl();
1936
1937                 clk_writel_delay(
1938                         PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_CLR_REG(c));
1939         }
1940 }
1941
1942 static void tegra3_periph_clk_reset(struct clk *c, bool assert)
1943 {
1944         unsigned long val;
1945         pr_debug("%s %s on clock %s\n", __func__,
1946                  assert ? "assert" : "deassert", c->name);
1947
1948         if (!(c->flags & PERIPH_NO_RESET)) {
1949                 if (assert) {
1950                         /* If peripheral is in the APB bus then read the APB
1951                          * bus to flush the write operation in apb bus. This
1952                          * will avoid the peripheral access after disabling
1953                          * clock */
1954                         if (c->flags & PERIPH_ON_APB)
1955                                 val = chipid_readl();
1956
1957                         clk_writel(PERIPH_CLK_TO_BIT(c),
1958                                    PERIPH_CLK_TO_RST_SET_REG(c));
1959                 } else
1960                         clk_writel(PERIPH_CLK_TO_BIT(c),
1961                                    PERIPH_CLK_TO_RST_CLR_REG(c));
1962         }
1963 }
1964
1965 static int tegra3_periph_clk_set_parent(struct clk *c, struct clk *p)
1966 {
1967         u32 val;
1968         const struct clk_mux_sel *sel;
1969         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
1970
1971         if (!(c->flags & MUX))
1972                 return (p == c->parent) ? 0 : (-EINVAL);
1973
1974         for (sel = c->inputs; sel->input != NULL; sel++) {
1975                 if (sel->input == p) {
1976                         val = clk_readl(c->reg);
1977                         val &= ~periph_clk_source_mask(c);
1978                         val |= (sel->value << periph_clk_source_shift(c));
1979
1980                         if (c->refcnt)
1981                                 clk_enable(p);
1982
1983                         clk_writel_delay(val, c->reg);
1984
1985                         if (c->refcnt && c->parent)
1986                                 clk_disable(c->parent);
1987
1988                         clk_reparent(c, p);
1989                         return 0;
1990                 }
1991         }
1992
1993         return -EINVAL;
1994 }
1995
1996 static int tegra3_periph_clk_set_rate(struct clk *c, unsigned long rate)
1997 {
1998         u32 val;
1999         int divider;
2000         unsigned long parent_rate = clk_get_rate(c->parent);
2001
2002         if (c->flags & DIV_U71) {
2003                 divider = clk_div71_get_divider(
2004                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
2005                 if (divider >= 0) {
2006                         val = clk_readl(c->reg);
2007                         val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
2008                         val |= divider;
2009                         if (c->flags & DIV_U71_UART) {
2010                                 if (divider)
2011                                         val |= PERIPH_CLK_UART_DIV_ENB;
2012                                 else
2013                                         val &= ~PERIPH_CLK_UART_DIV_ENB;
2014                         }
2015                         clk_writel_delay(val, c->reg);
2016                         c->div = divider + 2;
2017                         c->mul = 2;
2018                         return 0;
2019                 }
2020         } else if (c->flags & DIV_U16) {
2021                 divider = clk_div16_get_divider(parent_rate, rate);
2022                 if (divider >= 0) {
2023                         val = clk_readl(c->reg);
2024                         val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
2025                         val |= divider;
2026                         clk_writel_delay(val, c->reg);
2027                         c->div = divider + 1;
2028                         c->mul = 1;
2029                         return 0;
2030                 }
2031         } else if (parent_rate <= rate) {
2032                 c->div = 1;
2033                 c->mul = 1;
2034                 return 0;
2035         }
2036         return -EINVAL;
2037 }
2038
2039 static long tegra3_periph_clk_round_rate(struct clk *c,
2040         unsigned long rate)
2041 {
2042         int divider;
2043         unsigned long parent_rate = clk_get_rate(c->parent);
2044         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
2045
2046         if (c->flags & DIV_U71) {
2047                 divider = clk_div71_get_divider(
2048                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
2049                 if (divider < 0)
2050                         return divider;
2051
2052                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
2053         } else if (c->flags & DIV_U16) {
2054                 divider = clk_div16_get_divider(parent_rate, rate);
2055                 if (divider < 0)
2056                         return divider;
2057                 return DIV_ROUND_UP(parent_rate, divider + 1);
2058         }
2059         return -EINVAL;
2060 }
2061
2062 static struct clk_ops tegra_periph_clk_ops = {
2063         .init                   = &tegra3_periph_clk_init,
2064         .enable                 = &tegra3_periph_clk_enable,
2065         .disable                = &tegra3_periph_clk_disable,
2066         .set_parent             = &tegra3_periph_clk_set_parent,
2067         .set_rate               = &tegra3_periph_clk_set_rate,
2068         .round_rate             = &tegra3_periph_clk_round_rate,
2069         .reset                  = &tegra3_periph_clk_reset,
2070 };
2071
2072
2073 /* Periph extended clock configuration ops */
2074 static int
2075 tegra3_vi_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
2076 {
2077         if (p == TEGRA_CLK_VI_INP_SEL) {
2078                 u32 val = clk_readl(c->reg);
2079                 val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
2080                 val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
2081                         PERIPH_CLK_VI_SEL_EX_MASK;
2082                 clk_writel(val, c->reg);
2083                 return 0;
2084         }
2085         return -EINVAL;
2086 }
2087
2088 static struct clk_ops tegra_vi_clk_ops = {
2089         .init                   = &tegra3_periph_clk_init,
2090         .enable                 = &tegra3_periph_clk_enable,
2091         .disable                = &tegra3_periph_clk_disable,
2092         .set_parent             = &tegra3_periph_clk_set_parent,
2093         .set_rate               = &tegra3_periph_clk_set_rate,
2094         .round_rate             = &tegra3_periph_clk_round_rate,
2095         .clk_cfg_ex             = &tegra3_vi_clk_cfg_ex,
2096         .reset                  = &tegra3_periph_clk_reset,
2097 };
2098
2099 static int
2100 tegra3_nand_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
2101 {
2102         if (p == TEGRA_CLK_NAND_PAD_DIV2_ENB) {
2103                 u32 val = clk_readl(c->reg);
2104                 if (setting)
2105                         val |= PERIPH_CLK_NAND_DIV_EX_ENB;
2106                 else
2107                         val &= ~PERIPH_CLK_NAND_DIV_EX_ENB;
2108                 clk_writel(val, c->reg);
2109                 return 0;
2110         }
2111         return -EINVAL;
2112 }
2113
2114 static struct clk_ops tegra_nand_clk_ops = {
2115         .init                   = &tegra3_periph_clk_init,
2116         .enable                 = &tegra3_periph_clk_enable,
2117         .disable                = &tegra3_periph_clk_disable,
2118         .set_parent             = &tegra3_periph_clk_set_parent,
2119         .set_rate               = &tegra3_periph_clk_set_rate,
2120         .round_rate             = &tegra3_periph_clk_round_rate,
2121         .clk_cfg_ex             = &tegra3_nand_clk_cfg_ex,
2122         .reset                  = &tegra3_periph_clk_reset,
2123 };
2124
2125
2126 static int
2127 tegra3_dtv_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
2128 {
2129         if (p == TEGRA_CLK_DTV_INVERT) {
2130                 u32 val = clk_readl(c->reg);
2131                 if (setting)
2132                         val |= PERIPH_CLK_DTV_POLARITY_INV;
2133                 else
2134                         val &= ~PERIPH_CLK_DTV_POLARITY_INV;
2135                 clk_writel(val, c->reg);
2136                 return 0;
2137         }
2138         return -EINVAL;
2139 }
2140
2141 static struct clk_ops tegra_dtv_clk_ops = {
2142         .init                   = &tegra3_periph_clk_init,
2143         .enable                 = &tegra3_periph_clk_enable,
2144         .disable                = &tegra3_periph_clk_disable,
2145         .set_parent             = &tegra3_periph_clk_set_parent,
2146         .set_rate               = &tegra3_periph_clk_set_rate,
2147         .round_rate             = &tegra3_periph_clk_round_rate,
2148         .clk_cfg_ex             = &tegra3_dtv_clk_cfg_ex,
2149         .reset                  = &tegra3_periph_clk_reset,
2150 };
2151
2152 static int tegra3_dsib_clk_set_parent(struct clk *c, struct clk *p)
2153 {
2154         const struct clk_mux_sel *sel;
2155         struct clk *d = tegra_get_clock_by_name("pll_d");
2156
2157         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
2158
2159         for (sel = c->inputs; sel->input != NULL; sel++) {
2160                 if (sel->input == p) {
2161                         if (c->refcnt)
2162                                 clk_enable(p);
2163
2164                         /* The DSIB parent selection bit is in PLLD base
2165                            register - can not do direct r-m-w, must be
2166                            protected by PLLD lock */
2167                         tegra_clk_cfg_ex(
2168                                 d, TEGRA_CLK_PLLD_MIPI_MUX_SEL, sel->value);
2169
2170                         if (c->refcnt && c->parent)
2171                                 clk_disable(c->parent);
2172
2173                         clk_reparent(c, p);
2174                         return 0;
2175                 }
2176         }
2177
2178         return -EINVAL;
2179 }
2180
2181 static struct clk_ops tegra_dsib_clk_ops = {
2182         .init                   = &tegra3_periph_clk_init,
2183         .enable                 = &tegra3_periph_clk_enable,
2184         .disable                = &tegra3_periph_clk_disable,
2185         .set_parent             = &tegra3_dsib_clk_set_parent,
2186         .set_rate               = &tegra3_periph_clk_set_rate,
2187         .round_rate             = &tegra3_periph_clk_round_rate,
2188         .reset                  = &tegra3_periph_clk_reset,
2189 };
2190
2191 /* pciex clock support only reset function */
2192 static struct clk_ops tegra_pciex_clk_ops = {
2193         .reset    = tegra3_periph_clk_reset,
2194 };
2195
2196 /* Output clock ops */
2197
2198 static DEFINE_SPINLOCK(clk_out_lock);
2199
2200 static void tegra3_clk_out_init(struct clk *c)
2201 {
2202         const struct clk_mux_sel *mux = 0;
2203         const struct clk_mux_sel *sel;
2204         u32 val = pmc_readl(c->reg);
2205
2206         c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
2207         c->mul = 1;
2208         c->div = 1;
2209
2210         for (sel = c->inputs; sel->input != NULL; sel++) {
2211                 if (((val & periph_clk_source_mask(c)) >>
2212                      periph_clk_source_shift(c)) == sel->value)
2213                         mux = sel;
2214         }
2215         BUG_ON(!mux);
2216         c->parent = mux->input;
2217 }
2218
2219 static int tegra3_clk_out_enable(struct clk *c)
2220 {
2221         u32 val;
2222         unsigned long flags;
2223
2224         pr_debug("%s on clock %s\n", __func__, c->name);
2225
2226         spin_lock_irqsave(&clk_out_lock, flags);
2227         val = pmc_readl(c->reg);
2228         val |= (0x1 << c->u.periph.clk_num);
2229         pmc_writel(val, c->reg);
2230         spin_unlock_irqrestore(&clk_out_lock, flags);
2231
2232         return 0;
2233 }
2234
2235 static void tegra3_clk_out_disable(struct clk *c)
2236 {
2237         u32 val;
2238         unsigned long flags;
2239
2240         pr_debug("%s on clock %s\n", __func__, c->name);
2241
2242         spin_lock_irqsave(&clk_out_lock, flags);
2243         val = pmc_readl(c->reg);
2244         val &= ~(0x1 << c->u.periph.clk_num);
2245         pmc_writel(val, c->reg);
2246         spin_unlock_irqrestore(&clk_out_lock, flags);
2247 }
2248
2249 static int tegra3_clk_out_set_parent(struct clk *c, struct clk *p)
2250 {
2251         u32 val;
2252         unsigned long flags;
2253         const struct clk_mux_sel *sel;
2254
2255         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
2256
2257         for (sel = c->inputs; sel->input != NULL; sel++) {
2258                 if (sel->input == p) {
2259                         if (c->refcnt)
2260                                 clk_enable(p);
2261
2262                         spin_lock_irqsave(&clk_out_lock, flags);
2263                         val = pmc_readl(c->reg);
2264                         val &= ~periph_clk_source_mask(c);
2265                         val |= (sel->value << periph_clk_source_shift(c));
2266                         pmc_writel(val, c->reg);
2267                         spin_unlock_irqrestore(&clk_out_lock, flags);
2268
2269                         if (c->refcnt && c->parent)
2270                                 clk_disable(c->parent);
2271
2272                         clk_reparent(c, p);
2273                         return 0;
2274                 }
2275         }
2276         return -EINVAL;
2277 }
2278
2279 static struct clk_ops tegra_clk_out_ops = {
2280         .init                   = &tegra3_clk_out_init,
2281         .enable                 = &tegra3_clk_out_enable,
2282         .disable                = &tegra3_clk_out_disable,
2283         .set_parent             = &tegra3_clk_out_set_parent,
2284 };
2285
2286
2287 /* External memory controller clock ops */
2288 static void tegra3_emc_clk_init(struct clk *c)
2289 {
2290         tegra3_periph_clk_init(c);
2291
2292         /* On A01 limit EMC maximum rate to boot frequency;
2293            starting with A02 full PLLM range should be supported */
2294         if (tegra_get_revision() == TEGRA_REVISION_A01)
2295                 c->max_rate = clk_get_rate_locked(c);
2296         else
2297                 c->max_rate = clk_get_rate(c->parent);
2298 }
2299
2300 static long tegra3_emc_clk_round_rate(struct clk *c, unsigned long rate)
2301 {
2302         long new_rate = rate;
2303
2304         new_rate = tegra_emc_round_rate(new_rate);
2305         if (new_rate < 0)
2306                 new_rate = c->max_rate;
2307
2308         return new_rate;
2309 }
2310
2311 static int tegra3_emc_clk_set_rate(struct clk *c, unsigned long rate)
2312 {
2313         int ret;
2314         u32 div_value;
2315         struct clk *p;
2316
2317         /* The tegra3 memory controller has an interlock with the clock
2318          * block that allows memory shadowed registers to be updated,
2319          * and then transfer them to the main registers at the same
2320          * time as the clock update without glitches. During clock change
2321          * operation both clock parent and divider may change simultaneously
2322          * to achieve requested rate. */
2323         p = tegra_emc_predict_parent(rate, &div_value);
2324         div_value += 2;         /* emc has fractional DIV_U71 divider */
2325         if (!p)
2326                 return -EINVAL;
2327
2328         if (p == c->parent) {
2329                 if (div_value == c->div)
2330                         return 0;
2331         } else if (c->refcnt)
2332                 clk_enable(p);
2333
2334         ret = tegra_emc_set_rate(rate);
2335         if (ret < 0)
2336                 return ret;
2337
2338         if (p != c->parent) {
2339                 if(c->refcnt && c->parent)
2340                         clk_disable(c->parent);
2341                 clk_reparent(c, p);
2342         }
2343         c->div = div_value;
2344         c->mul = 2;
2345         return 0;
2346 }
2347
2348 static struct clk_ops tegra_emc_clk_ops = {
2349         .init                   = &tegra3_emc_clk_init,
2350         .enable                 = &tegra3_periph_clk_enable,
2351         .disable                = &tegra3_periph_clk_disable,
2352         .set_rate               = &tegra3_emc_clk_set_rate,
2353         .round_rate             = &tegra3_emc_clk_round_rate,
2354         .reset                  = &tegra3_periph_clk_reset,
2355 };
2356
2357 /* Clock doubler ops */
2358 static void tegra3_clk_double_init(struct clk *c)
2359 {
2360         u32 val = clk_readl(c->reg);
2361         c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
2362         c->div = 1;
2363         c->state = ON;
2364         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
2365                 c->state = OFF;
2366 };
2367
2368 static int tegra3_clk_double_set_rate(struct clk *c, unsigned long rate)
2369 {
2370         u32 val;
2371         unsigned long parent_rate = clk_get_rate(c->parent);
2372         if (rate == parent_rate) {
2373                 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
2374                 clk_writel(val, c->reg);
2375                 c->mul = 1;
2376                 c->div = 1;
2377                 return 0;
2378         } else if (rate == 2 * parent_rate) {
2379                 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
2380                 clk_writel(val, c->reg);
2381                 c->mul = 2;
2382                 c->div = 1;
2383                 return 0;
2384         }
2385         return -EINVAL;
2386 }
2387
2388 static struct clk_ops tegra_clk_double_ops = {
2389         .init                   = &tegra3_clk_double_init,
2390         .enable                 = &tegra3_periph_clk_enable,
2391         .disable                = &tegra3_periph_clk_disable,
2392         .set_rate               = &tegra3_clk_double_set_rate,
2393 };
2394
2395 /* Audio sync clock ops */
2396 static int tegra3_sync_source_set_rate(struct clk *c, unsigned long rate)
2397 {
2398         c->rate = rate;
2399         return 0;
2400 }
2401
2402 static struct clk_ops tegra_sync_source_ops = {
2403         .set_rate               = &tegra3_sync_source_set_rate,
2404 };
2405
2406 static void tegra3_audio_sync_clk_init(struct clk *c)
2407 {
2408         int source;
2409         const struct clk_mux_sel *sel;
2410         u32 val = clk_readl(c->reg);
2411         c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
2412         source = val & AUDIO_SYNC_SOURCE_MASK;
2413         for (sel = c->inputs; sel->input != NULL; sel++)
2414                 if (sel->value == source)
2415                         break;
2416         BUG_ON(sel->input == NULL);
2417         c->parent = sel->input;
2418 }
2419
2420 static int tegra3_audio_sync_clk_enable(struct clk *c)
2421 {
2422         u32 val = clk_readl(c->reg);
2423         clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
2424         return 0;
2425 }
2426
2427 static void tegra3_audio_sync_clk_disable(struct clk *c)
2428 {
2429         u32 val = clk_readl(c->reg);
2430         clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
2431 }
2432
2433 static int tegra3_audio_sync_clk_set_parent(struct clk *c, struct clk *p)
2434 {
2435         u32 val;
2436         const struct clk_mux_sel *sel;
2437         for (sel = c->inputs; sel->input != NULL; sel++) {
2438                 if (sel->input == p) {
2439                         val = clk_readl(c->reg);
2440                         val &= ~AUDIO_SYNC_SOURCE_MASK;
2441                         val |= sel->value;
2442
2443                         if (c->refcnt)
2444                                 clk_enable(p);
2445
2446                         clk_writel(val, c->reg);
2447
2448                         if (c->refcnt && c->parent)
2449                                 clk_disable(c->parent);
2450
2451                         clk_reparent(c, p);
2452                         return 0;
2453                 }
2454         }
2455
2456         return -EINVAL;
2457 }
2458
2459 static struct clk_ops tegra_audio_sync_clk_ops = {
2460         .init       = tegra3_audio_sync_clk_init,
2461         .enable     = tegra3_audio_sync_clk_enable,
2462         .disable    = tegra3_audio_sync_clk_disable,
2463         .set_parent = tegra3_audio_sync_clk_set_parent,
2464 };
2465
2466 /* cml0 (pcie), and cml1 (sata) clock ops */
2467 static void tegra3_cml_clk_init(struct clk *c)
2468 {
2469         u32 val = clk_readl(c->reg);
2470         c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
2471 }
2472
2473 static int tegra3_cml_clk_enable(struct clk *c)
2474 {
2475         u32 val = clk_readl(c->reg);
2476         val |= (0x1 << c->u.periph.clk_num);
2477         clk_writel(val, c->reg);
2478         return 0;
2479 }
2480
2481 static void tegra3_cml_clk_disable(struct clk *c)
2482 {
2483         u32 val = clk_readl(c->reg);
2484         val &= ~(0x1 << c->u.periph.clk_num);
2485         clk_writel(val, c->reg);
2486 }
2487
2488 static struct clk_ops tegra_cml_clk_ops = {
2489         .init                   = &tegra3_cml_clk_init,
2490         .enable                 = &tegra3_cml_clk_enable,
2491         .disable                = &tegra3_cml_clk_disable,
2492 };
2493
2494
2495 /* cbus ops */
2496 /*
2497  * Some clocks require dynamic re-locking of source PLL in order to
2498  * achieve frequency scaling granularity that matches characterized
2499  * core voltage steps. The cbus clock creates a shared bus that
2500  * provides a virtual root for such clocks to hide and synchronize
2501  * parent PLL re-locking as well as backup operations.
2502 */
2503
2504 static void tegra3_clk_cbus_init(struct clk *c)
2505 {
2506         c->state = OFF;
2507         c->set = true;
2508 }
2509
2510 static int tegra3_clk_cbus_enable(struct clk *c)
2511 {
2512         return 0;
2513 }
2514
2515 static long tegra3_clk_cbus_round_rate(struct clk *c, unsigned long rate)
2516 {
2517         int i;
2518
2519         if (!c->dvfs)
2520                 return rate;
2521
2522         for (i = 0; i < (c->dvfs->num_freqs - 1); i++) {
2523                 unsigned long f = c->dvfs->freqs[i];
2524                 if (f >= rate)
2525                         break;
2526         }
2527         return c->dvfs->freqs[i];
2528 }
2529
2530 static int cbus_switch_one(struct clk *c, struct clk *p, u32 div, bool abort)
2531 {
2532         int ret = 0;
2533
2534         /* set new divider if it is bigger than the current one */
2535         if (c->div < c->mul * div) {
2536                 ret = clk_set_div(c, div);
2537                 if (ret) {
2538                         pr_err("%s: failed to set %s clock divider %u: %d\n",
2539                                __func__, c->name, div, ret);
2540                         if (abort)
2541                                 return ret;
2542                 }
2543         }
2544
2545         ret = clk_set_parent(c, p);
2546         if (ret) {
2547                 pr_err("%s: failed to set %s clock parent %s: %d\n",
2548                        __func__, c->name, p->name, ret);
2549                 if (abort)
2550                         return ret;
2551         }
2552
2553         /* set new divider if it is smaller than the current one */
2554         if (c->div > c->mul * div) {
2555                 ret = clk_set_div(c, div);
2556                 if (ret)
2557                         pr_err("%s: failed to set %s clock divider %u: %d\n",
2558                                __func__, c->name, div, ret);
2559         }
2560
2561         return ret;
2562 }
2563
2564 static int cbus_backup(struct clk *c)
2565 {
2566         int ret;
2567         struct clk *user;
2568
2569         list_for_each_entry(user, &c->shared_bus_list,
2570                         u.shared_bus_user.node) {
2571                 bool enabled = user->u.shared_bus_user.enabled ||
2572                         user->u.shared_bus_user.client->refcnt;
2573                 if (enabled) {
2574                         ret = cbus_switch_one(user->u.shared_bus_user.client,
2575                                               c->shared_bus_backup.input,
2576                                               c->shared_bus_backup.value *
2577                                               user->div, true);
2578                         if (ret)
2579                                 return ret;
2580                 }
2581         }
2582         return 0;
2583 }
2584
2585 static void cbus_restore(struct clk *c)
2586 {
2587         struct clk *user;
2588
2589         list_for_each_entry(user, &c->shared_bus_list,
2590                         u.shared_bus_user.node) {
2591                 bool back = (c->parent !=
2592                         user->u.shared_bus_user.client->parent);
2593                 if (back)
2594                         cbus_switch_one(user->u.shared_bus_user.client,
2595                                         c->parent, user->div, false);
2596         }
2597 }
2598
2599 static int tegra3_clk_cbus_set_rate(struct clk *c, unsigned long rate)
2600 {
2601         int ret;
2602
2603         if (rate == 0)
2604                 return 0;
2605
2606         ret = clk_enable(c->parent);
2607         if (ret) {
2608                 pr_err("%s: failed to enable %s clock: %d\n",
2609                        __func__, c->name, ret);
2610                 return ret;
2611         }
2612
2613         ret = cbus_backup(c);
2614         if (ret)
2615                 goto out;
2616
2617         ret = clk_set_rate(c->parent, rate);
2618         if (ret) {
2619                 pr_err("%s: failed to set %s clock rate %lu: %d\n",
2620                        __func__, c->name, rate, ret);
2621                 goto out;
2622         }
2623
2624         cbus_restore(c);
2625
2626 out:
2627         clk_disable(c->parent);
2628         return ret;
2629 }
2630
2631 static struct clk_ops tegra_clk_cbus_ops = {
2632         .init = tegra3_clk_cbus_init,
2633         .enable = tegra3_clk_cbus_enable,
2634         .set_rate = tegra3_clk_cbus_set_rate,
2635         .round_rate = tegra3_clk_cbus_round_rate,
2636 };
2637
2638 /* shared bus ops */
2639 /*
2640  * Some clocks may have multiple downstream users that need to request a
2641  * higher clock rate.  Shared bus clocks provide a unique shared_bus_user
2642  * clock to each user.  The frequency of the bus is set to the highest
2643  * enabled shared_bus_user clock, with a minimum value set by the
2644  * shared bus.
2645  */
2646 static void tegra_clk_shared_bus_update(struct clk *bus)
2647 {
2648         struct clk *c;
2649         unsigned long rate = bus->min_rate;
2650
2651         if (detach_shared_bus)
2652                 return;
2653
2654         list_for_each_entry(c, &bus->shared_bus_list,
2655                         u.shared_bus_user.node) {
2656                 /* Ignore requests from disabled users and from users with
2657                    fixed bus-to-client ratio */
2658                 if ((c->u.shared_bus_user.enabled) &&
2659                     (!c->u.shared_bus_user.client_div))
2660                         rate = max(c->u.shared_bus_user.rate, rate);
2661         }
2662
2663         if (rate != clk_get_rate(bus))
2664                 clk_set_rate(bus, rate);
2665 };
2666
2667 static void tegra_clk_shared_bus_init(struct clk *c)
2668 {
2669         c->max_rate = c->parent->max_rate;
2670         c->u.shared_bus_user.rate = c->parent->max_rate;
2671         c->state = OFF;
2672         c->set = true;
2673
2674         if (c->u.shared_bus_user.client_id) {
2675                 c->u.shared_bus_user.client =
2676                         tegra_get_clock_by_name(c->u.shared_bus_user.client_id);
2677                 if (!c->u.shared_bus_user.client) {
2678                         pr_err("%s: could not find clk %s\n", __func__,
2679                                c->u.shared_bus_user.client_id);
2680                         return;
2681                 }
2682                 c->div = c->u.shared_bus_user.client_div ? : 1;
2683                 c->mul = 1;
2684         }
2685
2686         list_add_tail(&c->u.shared_bus_user.node,
2687                 &c->parent->shared_bus_list);
2688 }
2689
2690 static int tegra_clk_shared_bus_set_rate(struct clk *c, unsigned long rate)
2691 {
2692         c->u.shared_bus_user.rate = rate;
2693         tegra_clk_shared_bus_update(c->parent);
2694         return 0;
2695 }
2696
2697 static long tegra_clk_shared_bus_round_rate(struct clk *c, unsigned long rate)
2698 {
2699         return clk_round_rate(c->parent, rate);
2700 }
2701
2702 static int tegra_clk_shared_bus_enable(struct clk *c)
2703 {
2704         c->u.shared_bus_user.enabled = true;
2705         tegra_clk_shared_bus_update(c->parent);
2706         if (c->u.shared_bus_user.client) {
2707                 return clk_enable(c->u.shared_bus_user.client);
2708         }
2709         return 0;
2710 }
2711
2712 static void tegra_clk_shared_bus_disable(struct clk *c)
2713 {
2714         if (c->u.shared_bus_user.client)
2715                 clk_disable(c->u.shared_bus_user.client);
2716         c->u.shared_bus_user.enabled = false;
2717         tegra_clk_shared_bus_update(c->parent);
2718 }
2719
2720 static void tegra_clk_shared_bus_reset(struct clk *c, bool assert)
2721 {
2722         if (c->u.shared_bus_user.client) {
2723                 if (c->u.shared_bus_user.client->ops &&
2724                     c->u.shared_bus_user.client->ops->reset)
2725                         c->u.shared_bus_user.client->ops->reset(
2726                                 c->u.shared_bus_user.client, assert);
2727         }
2728 }
2729
2730 static struct clk_ops tegra_clk_shared_bus_ops = {
2731         .init = tegra_clk_shared_bus_init,
2732         .enable = tegra_clk_shared_bus_enable,
2733         .disable = tegra_clk_shared_bus_disable,
2734         .set_rate = tegra_clk_shared_bus_set_rate,
2735         .round_rate = tegra_clk_shared_bus_round_rate,
2736         .reset = tegra_clk_shared_bus_reset,
2737 };
2738
2739
2740 /* Clock definitions */
2741 static struct clk tegra_clk_32k = {
2742         .name = "clk_32k",
2743         .rate = 32768,
2744         .ops  = NULL,
2745         .max_rate = 32768,
2746 };
2747
2748 static struct clk tegra_clk_m = {
2749         .name      = "clk_m",
2750         .flags     = ENABLE_ON_INIT,
2751         .ops       = &tegra_clk_m_ops,
2752         .reg       = 0x1fc,
2753         .reg_shift = 28,
2754         .max_rate  = 48000000,
2755 };
2756
2757 static struct clk tegra_clk_m_div2 = {
2758         .name      = "clk_m_div2",
2759         .ops       = &tegra_clk_m_div_ops,
2760         .parent    = &tegra_clk_m,
2761         .mul       = 1,
2762         .div       = 2,
2763         .state     = ON,
2764         .max_rate  = 24000000,
2765 };
2766
2767 static struct clk tegra_clk_m_div4 = {
2768         .name      = "clk_m_div4",
2769         .ops       = &tegra_clk_m_div_ops,
2770         .parent    = &tegra_clk_m,
2771         .mul       = 1,
2772         .div       = 4,
2773         .state     = ON,
2774         .max_rate  = 12000000,
2775 };
2776
2777 static struct clk tegra_pll_ref = {
2778         .name      = "pll_ref",
2779         .flags     = ENABLE_ON_INIT,
2780         .ops       = &tegra_pll_ref_ops,
2781         .parent    = &tegra_clk_m,
2782         .max_rate  = 26000000,
2783 };
2784
2785 static struct clk_pll_freq_table tegra_pll_c_freq_table[] = {
2786         { 12000000, 1040000000, 520,  6, 1, 8},
2787         { 13000000, 1040000000, 480,  6, 1, 8},
2788         { 16800000, 1040000000, 495,  8, 1, 8},         /* actual: 1039.5 MHz */
2789         { 19200000, 1040000000, 325,  6, 1, 6},
2790         { 26000000, 1040000000, 520, 13, 1, 8},
2791
2792         { 12000000, 832000000, 416,  6, 1, 8},
2793         { 13000000, 832000000, 832, 13, 1, 8},
2794         { 16800000, 832000000, 396,  8, 1, 8},          /* actual: 831.6 MHz */
2795         { 19200000, 832000000, 260,  6, 1, 8},
2796         { 26000000, 832000000, 416, 13, 1, 8},
2797
2798         { 12000000, 624000000, 624, 12, 1, 8},
2799         { 13000000, 624000000, 624, 13, 1, 8},
2800         { 16800000, 600000000, 520, 14, 1, 8},
2801         { 19200000, 624000000, 520, 16, 1, 8},
2802         { 26000000, 624000000, 624, 26, 1, 8},
2803
2804         { 12000000, 600000000, 600, 12, 1, 8},
2805         { 13000000, 600000000, 600, 13, 1, 8},
2806         { 16800000, 600000000, 500, 14, 1, 8},
2807         { 19200000, 600000000, 375, 12, 1, 6},
2808         { 26000000, 600000000, 600, 26, 1, 8},
2809
2810         { 12000000, 520000000, 520, 12, 1, 8},
2811         { 13000000, 520000000, 520, 13, 1, 8},
2812         { 16800000, 520000000, 495, 16, 1, 8},          /* actual: 519.75 MHz */
2813         { 19200000, 520000000, 325, 12, 1, 6},
2814         { 26000000, 520000000, 520, 26, 1, 8},
2815
2816         { 12000000, 416000000, 416, 12, 1, 8},
2817         { 13000000, 416000000, 416, 13, 1, 8},
2818         { 16800000, 416000000, 396, 16, 1, 8},          /* actual: 415.8 MHz */
2819         { 19200000, 416000000, 260, 12, 1, 6},
2820         { 26000000, 416000000, 416, 26, 1, 8},
2821         { 0, 0, 0, 0, 0, 0 },
2822 };
2823
2824 static struct clk tegra_pll_c = {
2825         .name      = "pll_c",
2826         .flags     = PLL_HAS_CPCON,
2827         .ops       = &tegra_pll_ops,
2828         .reg       = 0x80,
2829         .parent    = &tegra_pll_ref,
2830         .max_rate  = 1400000000,
2831         .u.pll = {
2832                 .input_min = 2000000,
2833                 .input_max = 31000000,
2834                 .cf_min    = 1000000,
2835                 .cf_max    = 6000000,
2836                 .vco_min   = 20000000,
2837                 .vco_max   = 1400000000,
2838                 .freq_table = tegra_pll_c_freq_table,
2839                 .lock_delay = 300,
2840         },
2841 };
2842
2843 static struct clk tegra_pll_c_out1 = {
2844         .name      = "pll_c_out1",
2845         .ops       = &tegra_pll_div_ops,
2846         .flags     = DIV_U71,
2847         .parent    = &tegra_pll_c,
2848         .reg       = 0x84,
2849         .reg_shift = 0,
2850         .max_rate  = 700000000,
2851 };
2852
2853 static struct clk_pll_freq_table tegra_pll_m_freq_table[] = {
2854         { 12000000, 666000000, 666, 12, 1, 8},
2855         { 13000000, 666000000, 666, 13, 1, 8},
2856         { 16800000, 666000000, 555, 14, 1, 8},
2857         { 19200000, 666000000, 555, 16, 1, 8},
2858         { 26000000, 666000000, 666, 26, 1, 8},
2859         { 12000000, 600000000, 600, 12, 1, 8},
2860         { 13000000, 600000000, 600, 13, 1, 8},
2861         { 16800000, 600000000, 500, 14, 1, 8},
2862         { 19200000, 600000000, 375, 12, 1, 6},
2863         { 26000000, 600000000, 600, 26, 1, 8},
2864         { 0, 0, 0, 0, 0, 0 },
2865 };
2866
2867 static struct clk tegra_pll_m = {
2868         .name      = "pll_m",
2869         .flags     = PLL_HAS_CPCON | PLLM,
2870         .ops       = &tegra_pll_ops,
2871         .reg       = 0x90,
2872         .parent    = &tegra_pll_ref,
2873         .max_rate  = 800000000,
2874         .u.pll = {
2875                 .input_min = 2000000,
2876                 .input_max = 31000000,
2877                 .cf_min    = 1000000,
2878                 .cf_max    = 6000000,
2879                 .vco_min   = 20000000,
2880                 .vco_max   = 1200000000,
2881                 .freq_table = tegra_pll_m_freq_table,
2882                 .lock_delay = 300,
2883         },
2884 };
2885
2886 static struct clk tegra_pll_m_out1 = {
2887         .name      = "pll_m_out1",
2888         .ops       = &tegra_pll_div_ops,
2889         .flags     = DIV_U71,
2890         .parent    = &tegra_pll_m,
2891         .reg       = 0x94,
2892         .reg_shift = 0,
2893         .max_rate  = 600000000,
2894 };
2895
2896 static struct clk_pll_freq_table tegra_pll_p_freq_table[] = {
2897         { 12000000, 216000000, 432, 12, 2, 8},
2898         { 13000000, 216000000, 432, 13, 2, 8},
2899         { 16800000, 216000000, 360, 14, 2, 8},
2900         { 19200000, 216000000, 360, 16, 2, 8},
2901         { 26000000, 216000000, 432, 26, 2, 8},
2902         { 0, 0, 0, 0, 0, 0 },
2903 };
2904
2905 static struct clk tegra_pll_p = {
2906         .name      = "pll_p",
2907         .flags     = ENABLE_ON_INIT | PLL_FIXED | PLL_HAS_CPCON,
2908         .ops       = &tegra_pll_ops,
2909         .reg       = 0xa0,
2910         .parent    = &tegra_pll_ref,
2911         .max_rate  = 432000000,
2912         .u.pll = {
2913                 .input_min = 2000000,
2914                 .input_max = 31000000,
2915                 .cf_min    = 1000000,
2916                 .cf_max    = 6000000,
2917                 .vco_min   = 20000000,
2918                 .vco_max   = 1400000000,
2919                 .freq_table = tegra_pll_p_freq_table,
2920                 .lock_delay = 300,
2921 #ifndef CONFIG_TEGRA_FPGA_PLATFORM
2922                 .fixed_rate = 408000000,
2923 #else
2924                 .fixed_rate = 216000000,
2925 #endif
2926         },
2927 };
2928
2929 static struct clk tegra_pll_p_out1 = {
2930         .name      = "pll_p_out1",
2931         .ops       = &tegra_pll_div_ops,
2932         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
2933         .parent    = &tegra_pll_p,
2934         .reg       = 0xa4,
2935         .reg_shift = 0,
2936         .max_rate  = 432000000,
2937 };
2938
2939 static struct clk tegra_pll_p_out2 = {
2940         .name      = "pll_p_out2",
2941         .ops       = &tegra_pll_div_ops,
2942         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
2943         .parent    = &tegra_pll_p,
2944         .reg       = 0xa4,
2945         .reg_shift = 16,
2946         .max_rate  = 432000000,
2947 };
2948
2949 static struct clk tegra_pll_p_out3 = {
2950         .name      = "pll_p_out3",
2951         .ops       = &tegra_pll_div_ops,
2952         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
2953         .parent    = &tegra_pll_p,
2954         .reg       = 0xa8,
2955         .reg_shift = 0,
2956         .max_rate  = 432000000,
2957 };
2958
2959 static struct clk tegra_pll_p_out4 = {
2960         .name      = "pll_p_out4",
2961         .ops       = &tegra_pll_div_ops,
2962         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
2963         .parent    = &tegra_pll_p,
2964         .reg       = 0xa8,
2965         .reg_shift = 16,
2966         .max_rate  = 432000000,
2967 };
2968
2969 static struct clk_pll_freq_table tegra_pll_a_freq_table[] = {
2970         { 9600000, 564480000, 294, 5, 1, 4},
2971         { 9600000, 552960000, 288, 5, 1, 4},
2972         { 9600000, 24000000,  5,   2, 1, 1},
2973
2974         { 28800000, 56448000, 49, 25, 1, 1},
2975         { 28800000, 73728000, 64, 25, 1, 1},
2976         { 28800000, 24000000,  5,  6, 1, 1},
2977         { 0, 0, 0, 0, 0, 0 },
2978 };
2979
2980 static struct clk tegra_pll_a = {
2981         .name      = "pll_a",
2982         .flags     = PLL_HAS_CPCON,
2983         .ops       = &tegra_pll_ops,
2984         .reg       = 0xb0,
2985         .parent    = &tegra_pll_p_out1,
2986         .max_rate  = 700000000,
2987         .u.pll = {
2988                 .input_min = 2000000,
2989                 .input_max = 31000000,
2990                 .cf_min    = 1000000,
2991                 .cf_max    = 6000000,
2992                 .vco_min   = 20000000,
2993                 .vco_max   = 1400000000,
2994                 .freq_table = tegra_pll_a_freq_table,
2995                 .lock_delay = 300,
2996         },
2997 };
2998
2999 static struct clk tegra_pll_a_out0 = {
3000         .name      = "pll_a_out0",
3001         .ops       = &tegra_pll_div_ops,
3002         .flags     = DIV_U71,
3003         .parent    = &tegra_pll_a,
3004         .reg       = 0xb4,
3005         .reg_shift = 0,
3006         .max_rate  = 100000000,
3007 };
3008
3009 static struct clk_pll_freq_table tegra_pll_d_freq_table[] = {
3010         { 12000000, 216000000, 216, 12, 1, 4},
3011         { 13000000, 216000000, 216, 13, 1, 4},
3012         { 16800000, 216000000, 180, 14, 1, 4},
3013         { 19200000, 216000000, 180, 16, 1, 4},
3014         { 26000000, 216000000, 216, 26, 1, 4},
3015
3016         { 12000000, 594000000, 594, 12, 1, 8},
3017         { 13000000, 594000000, 594, 13, 1, 8},
3018         { 16800000, 594000000, 495, 14, 1, 8},
3019         { 19200000, 594000000, 495, 16, 1, 8},
3020         { 26000000, 594000000, 594, 26, 1, 8},
3021
3022         { 12000000, 1000000000, 1000, 12, 1, 12},
3023         { 13000000, 1000000000, 1000, 13, 1, 12},
3024         { 19200000, 1000000000, 625,  12, 1, 8},
3025         { 26000000, 1000000000, 1000, 26, 1, 12},
3026
3027         { 0, 0, 0, 0, 0, 0 },
3028 };
3029
3030 static struct clk tegra_pll_d = {
3031         .name      = "pll_d",
3032         .flags     = PLL_HAS_CPCON | PLLD,
3033         .ops       = &tegra_plld_ops,
3034         .reg       = 0xd0,
3035         .parent    = &tegra_pll_ref,
3036         .max_rate  = 1000000000,
3037         .u.pll = {
3038                 .input_min = 2000000,
3039                 .input_max = 40000000,
3040                 .cf_min    = 1000000,
3041                 .cf_max    = 6000000,
3042                 .vco_min   = 40000000,
3043                 .vco_max   = 1000000000,
3044                 .freq_table = tegra_pll_d_freq_table,
3045                 .lock_delay = 1000,
3046         },
3047 };
3048
3049 static struct clk tegra_pll_d_out0 = {
3050         .name      = "pll_d_out0",
3051         .ops       = &tegra_pll_div_ops,
3052         .flags     = DIV_2 | PLLD,
3053         .parent    = &tegra_pll_d,
3054         .max_rate  = 500000000,
3055 };
3056
3057 static struct clk tegra_pll_d2 = {
3058         .name      = "pll_d2",
3059         .flags     = PLL_HAS_CPCON | PLL_ALT_MISC_REG | PLLD,
3060         .ops       = &tegra_plld_ops,
3061         .reg       = 0x4b8,
3062         .parent    = &tegra_pll_ref,
3063         .max_rate  = 1000000000,
3064         .u.pll = {
3065                 .input_min = 2000000,
3066                 .input_max = 40000000,
3067                 .cf_min    = 1000000,
3068                 .cf_max    = 6000000,
3069                 .vco_min   = 40000000,
3070                 .vco_max   = 1000000000,
3071                 .freq_table = tegra_pll_d_freq_table,
3072                 .lock_delay = 1000,
3073         },
3074 };
3075
3076 static struct clk tegra_pll_d2_out0 = {
3077         .name      = "pll_d2_out0",
3078         .ops       = &tegra_pll_div_ops,
3079         .flags     = DIV_2 | PLLD,
3080         .parent    = &tegra_pll_d2,
3081         .max_rate  = 500000000,
3082 };
3083
3084 static struct clk_pll_freq_table tegra_pll_u_freq_table[] = {
3085         { 12000000, 480000000, 960, 12, 2, 12},
3086         { 13000000, 480000000, 960, 13, 2, 12},
3087         { 16800000, 480000000, 400, 7,  2, 5},
3088         { 19200000, 480000000, 200, 4,  2, 3},
3089         { 26000000, 480000000, 960, 26, 2, 12},
3090         { 0, 0, 0, 0, 0, 0 },
3091 };
3092
3093 static struct clk tegra_pll_u = {
3094         .name      = "pll_u",
3095         .flags     = PLL_HAS_CPCON | PLLU,
3096         .ops       = &tegra_pll_ops,
3097         .reg       = 0xc0,
3098         .parent    = &tegra_pll_ref,
3099         .max_rate  = 480000000,
3100         .u.pll = {
3101                 .input_min = 2000000,
3102                 .input_max = 40000000,
3103                 .cf_min    = 1000000,
3104                 .cf_max    = 6000000,
3105                 .vco_min   = 480000000,
3106                 .vco_max   = 960000000,
3107                 .freq_table = tegra_pll_u_freq_table,
3108                 .lock_delay = 1000,
3109         },
3110 };
3111
3112 static struct clk_pll_freq_table tegra_pll_x_freq_table[] = {
3113         /* 1.4 GHz */
3114         { 12000000, 1400000000, 700,  6,  1, 8},
3115         { 13000000, 1400000000, 969,  9,  1, 8},        /* actual: 1399.7 MHz */
3116         { 16800000, 1400000000, 1000, 12, 1, 8},
3117         { 19200000, 1400000000, 875,  12, 1, 8},
3118         { 26000000, 1400000000, 700,  13, 1, 8},
3119
3120         /* 1.3 GHz */
3121         { 12000000, 1300000000, 975,  9,  1, 8},
3122         { 13000000, 1300000000, 1000, 10, 1, 8},
3123         { 16800000, 1300000000, 928,  12, 1, 8},        /* actual: 1299.2 MHz */
3124         { 19200000, 1300000000, 812,  12, 1, 8},        /* actual: 1299.2 MHz */
3125         { 26000000, 1300000000, 650,  13, 1, 8},
3126
3127         /* 1.2 GHz */
3128         { 12000000, 1200000000, 1000, 10, 1, 8},
3129         { 13000000, 1200000000, 923,  10, 1, 8},        /* actual: 1199.9 MHz */
3130         { 16800000, 1200000000, 1000, 14, 1, 8},
3131         { 19200000, 1200000000, 1000, 16, 1, 8},
3132         { 26000000, 1200000000, 600,  13, 1, 8},
3133
3134         /* 1.1 GHz */
3135         { 12000000, 1100000000, 825,  9,  1, 8},
3136         { 13000000, 1100000000, 846,  10, 1, 8},        /* actual: 1099.8 MHz */
3137         { 16800000, 1100000000, 982,  15, 1, 8},        /* actual: 1099.8 MHz */
3138         { 19200000, 1100000000, 859,  15, 1, 8},        /* actual: 1099.5 MHz */
3139         { 26000000, 1100000000, 550,  13, 1, 8},
3140
3141         /* 1 GHz */
3142         { 12000000, 1000000000, 1000, 12, 1, 8},
3143         { 13000000, 1000000000, 1000, 13, 1, 8},
3144         { 16800000, 1000000000, 833,  14, 1, 8},        /* actual: 999.6 MHz */
3145         { 19200000, 1000000000, 625,  12, 1, 8},
3146         { 26000000, 1000000000, 1000, 26, 1, 8},
3147
3148         { 0, 0, 0, 0, 0, 0 },
3149 };
3150
3151 static struct clk tegra_pll_x = {
3152         .name      = "pll_x",
3153         .flags     = PLL_HAS_CPCON | PLL_ALT_MISC_REG | PLLX,
3154         .ops       = &tegra_pll_ops,
3155         .reg       = 0xe0,
3156         .parent    = &tegra_pll_ref,
3157         .max_rate  = 1400000000,
3158         .u.pll = {
3159                 .input_min = 2000000,
3160                 .input_max = 31000000,
3161                 .cf_min    = 1000000,
3162                 .cf_max    = 6000000,
3163                 .vco_min   = 20000000,
3164                 .vco_max   = 1400000000,
3165                 .freq_table = tegra_pll_x_freq_table,
3166                 .lock_delay = 300,
3167         },
3168 };
3169
3170 static struct clk tegra_pll_x_out0 = {
3171         .name      = "pll_x_out0",
3172         .ops       = &tegra_pll_div_ops,
3173         .flags     = DIV_2 | PLLX,
3174         .parent    = &tegra_pll_x,
3175         .max_rate  = 700000000,
3176 };
3177
3178
3179 static struct clk_pll_freq_table tegra_pll_e_freq_table[] = {
3180         /* PLLE special case: use cpcon field to store cml divider value */
3181         { 12000000,  100000000, 150, 1,  18, 11},
3182         { 216000000, 100000000, 200, 18, 24, 13},
3183 #ifdef CONFIG_TEGRA_FPGA_PLATFORM
3184         { 13000000,  100000000, 200, 1,  26, 13},
3185 #endif
3186         { 0, 0, 0, 0, 0, 0 },
3187 };
3188
3189 static struct clk tegra_pll_e = {
3190         .name      = "pll_e",
3191         .flags     = PLL_ALT_MISC_REG,
3192         .ops       = &tegra_plle_ops,
3193         .reg       = 0xe8,
3194         .max_rate  = 100000000,
3195         .u.pll = {
3196                 .input_min = 12000000,
3197                 .input_max = 216000000,
3198                 .cf_min    = 12000000,
3199                 .cf_max    = 12000000,
3200                 .vco_min   = 1200000000,
3201                 .vco_max   = 2400000000U,
3202                 .freq_table = tegra_pll_e_freq_table,
3203                 .lock_delay = 300,
3204                 .fixed_rate = 100000000,
3205         },
3206 };
3207
3208 static struct clk tegra_cml0_clk = {
3209         .name      = "cml0",
3210         .parent    = &tegra_pll_e,
3211         .ops       = &tegra_cml_clk_ops,
3212         .reg       = PLLE_AUX,
3213         .max_rate  = 100000000,
3214         .u.periph  = {
3215                 .clk_num = 0,
3216         },
3217 };
3218
3219 static struct clk tegra_cml1_clk = {
3220         .name      = "cml1",
3221         .parent    = &tegra_pll_e,
3222         .ops       = &tegra_cml_clk_ops,
3223         .reg       = PLLE_AUX,
3224         .max_rate  = 100000000,
3225         .u.periph  = {
3226                 .clk_num   = 1,
3227         },
3228 };
3229
3230 static struct clk tegra_pciex_clk = {
3231         .name      = "pciex",
3232         .parent    = &tegra_pll_e,
3233         .ops       = &tegra_pciex_clk_ops,
3234         .max_rate  = 100000000,
3235         .u.periph  = {
3236                 .clk_num   = 74,
3237         },
3238 };
3239
3240 /* Audio sync clocks */
3241 #define SYNC_SOURCE(_id)                                \
3242         {                                               \
3243                 .name      = #_id "_sync",              \
3244                 .rate      = 24000000,                  \
3245                 .max_rate  = 24000000,                  \
3246                 .ops       = &tegra_sync_source_ops     \
3247         }
3248 static struct clk tegra_sync_source_list[] = {
3249         SYNC_SOURCE(spdif_in),
3250         SYNC_SOURCE(i2s0),
3251         SYNC_SOURCE(i2s1),
3252         SYNC_SOURCE(i2s2),
3253         SYNC_SOURCE(i2s3),
3254         SYNC_SOURCE(i2s4),
3255         SYNC_SOURCE(vimclk),
3256 };
3257
3258 static struct clk_mux_sel mux_audio_sync_clk[] =
3259 {
3260         { .input = &tegra_sync_source_list[0],  .value = 0},
3261         { .input = &tegra_sync_source_list[1],  .value = 1},
3262         { .input = &tegra_sync_source_list[2],  .value = 2},
3263         { .input = &tegra_sync_source_list[3],  .value = 3},
3264         { .input = &tegra_sync_source_list[4],  .value = 4},
3265         { .input = &tegra_sync_source_list[5],  .value = 5},
3266         { .input = &tegra_pll_a_out0,           .value = 6},
3267         { .input = &tegra_sync_source_list[6],  .value = 7},
3268         { 0, 0 }
3269 };
3270
3271 #define AUDIO_SYNC_CLK(_id, _index)                     \
3272         {                                               \
3273                 .name      = #_id,                      \
3274                 .inputs    = mux_audio_sync_clk,        \
3275                 .reg       = 0x4A0 + (_index) * 4,      \
3276                 .max_rate  = 24000000,                  \
3277                 .ops       = &tegra_audio_sync_clk_ops  \
3278         }
3279 static struct clk tegra_clk_audio_list[] = {
3280         AUDIO_SYNC_CLK(audio0, 0),
3281         AUDIO_SYNC_CLK(audio1, 1),
3282         AUDIO_SYNC_CLK(audio2, 2),
3283         AUDIO_SYNC_CLK(audio3, 3),
3284         AUDIO_SYNC_CLK(audio4, 4),
3285         AUDIO_SYNC_CLK(audio, 5),       /* SPDIF */
3286 };
3287
3288 #define AUDIO_SYNC_2X_CLK(_id, _index)                          \
3289         {                                                       \
3290                 .name      = #_id "_2x",                        \
3291                 .flags     = PERIPH_NO_RESET,                   \
3292                 .max_rate  = 48000000,                          \
3293                 .ops       = &tegra_clk_double_ops,             \
3294                 .reg       = 0x49C,                             \
3295                 .reg_shift = 24 + (_index),                     \
3296                 .parent    = &tegra_clk_audio_list[(_index)],   \
3297                 .u.periph = {                                   \
3298                         .clk_num = 113 + (_index),              \
3299                 },                                              \
3300         }
3301 static struct clk tegra_clk_audio_2x_list[] = {
3302         AUDIO_SYNC_2X_CLK(audio0, 0),
3303         AUDIO_SYNC_2X_CLK(audio1, 1),
3304         AUDIO_SYNC_2X_CLK(audio2, 2),
3305         AUDIO_SYNC_2X_CLK(audio3, 3),
3306         AUDIO_SYNC_2X_CLK(audio4, 4),
3307         AUDIO_SYNC_2X_CLK(audio, 5),    /* SPDIF */
3308 };
3309
3310 #define MUX_I2S_SPDIF(_id, _index)                                      \
3311 static struct clk_mux_sel mux_pllaout0_##_id##_2x_pllp_clkm[] = {       \
3312         {.input = &tegra_pll_a_out0, .value = 0},                       \
3313         {.input = &tegra_clk_audio_2x_list[(_index)], .value = 1},      \
3314         {.input = &tegra_pll_p, .value = 2},                            \
3315         {.input = &tegra_clk_m, .value = 3},                            \
3316         { 0, 0},                                                        \
3317 }
3318 MUX_I2S_SPDIF(audio0, 0);
3319 MUX_I2S_SPDIF(audio1, 1);
3320 MUX_I2S_SPDIF(audio2, 2);
3321 MUX_I2S_SPDIF(audio3, 3);
3322 MUX_I2S_SPDIF(audio4, 4);
3323 MUX_I2S_SPDIF(audio, 5);                /* SPDIF */
3324
3325 /* External clock outputs (through PMC) */
3326 #define MUX_EXTERN_OUT(_id)                                             \
3327 static struct clk_mux_sel mux_clkm_clkm2_clkm4_extern##_id[] = {        \
3328         {.input = &tegra_clk_m,         .value = 0},                    \
3329         {.input = &tegra_clk_m_div2,    .value = 1},                    \
3330         {.input = &tegra_clk_m_div4,    .value = 2},                    \
3331         {.input = NULL,                 .value = 3}, /* placeholder */  \
3332         { 0, 0},                                                        \
3333 }
3334 MUX_EXTERN_OUT(1);
3335 MUX_EXTERN_OUT(2);
3336 MUX_EXTERN_OUT(3);
3337
3338 static struct clk_mux_sel *mux_extern_out_list[] = {
3339         mux_clkm_clkm2_clkm4_extern1,
3340         mux_clkm_clkm2_clkm4_extern2,
3341         mux_clkm_clkm2_clkm4_extern3,
3342 };
3343
3344 #define CLK_OUT_CLK(_id)                                        \
3345         {                                                       \
3346                 .name      = "clk_out_" #_id,                   \
3347                 .lookup    = {                                  \
3348                         .dev_id    = "clk_out_" #_id,           \
3349                         .con_id    = "extern" #_id,             \
3350                 },                                              \
3351                 .ops       = &tegra_clk_out_ops,                \
3352                 .reg       = 0x1a8,                             \
3353                 .inputs    = mux_clkm_clkm2_clkm4_extern##_id,  \
3354                 .flags     = MUX_CLK_OUT,                       \
3355                 .max_rate  = 216000000,                         \
3356                 .u.periph = {                                   \
3357                         .clk_num   = (_id - 1) * 8 + 2,         \
3358                 },                                              \
3359         }
3360 static struct clk tegra_clk_out_list[] = {
3361         CLK_OUT_CLK(1),
3362         CLK_OUT_CLK(2),
3363         CLK_OUT_CLK(3),
3364 };
3365
3366 /* called after peripheral external clocks are initialized */
3367 static void init_clk_out_mux(void)
3368 {
3369         int i;
3370         struct clk *c;
3371
3372         /* output clock con_id is the name of peripheral
3373            external clock connected to input 3 of the output mux */
3374         for (i = 0; i < ARRAY_SIZE(tegra_clk_out_list); i++) {
3375                 c = tegra_get_clock_by_name(
3376                         tegra_clk_out_list[i].lookup.con_id);
3377                 if (!c)
3378                         pr_err("%s: could not find clk %s\n", __func__,
3379                                tegra_clk_out_list[i].lookup.con_id);
3380                 mux_extern_out_list[i][3].input = c;
3381         }
3382 }
3383
3384 /* Peripheral muxes */
3385 static struct clk_mux_sel mux_cclk_g[] = {
3386         { .input = &tegra_clk_m,        .value = 0},
3387         { .input = &tegra_pll_c,        .value = 1},
3388         { .input = &tegra_clk_32k,      .value = 2},
3389         { .input = &tegra_pll_m,        .value = 3},
3390         { .input = &tegra_pll_p,        .value = 4},
3391         { .input = &tegra_pll_p_out4,   .value = 5},
3392         { .input = &tegra_pll_p_out3,   .value = 6},
3393         /* { .input = &tegra_clk_d,     .value = 7}, - no use on tegra3 */
3394         { .input = &tegra_pll_x,        .value = 8},
3395         { 0, 0},
3396 };
3397
3398 static struct clk_mux_sel mux_cclk_lp[] = {
3399         { .input = &tegra_clk_m,        .value = 0},
3400         { .input = &tegra_pll_c,        .value = 1},
3401         { .input = &tegra_clk_32k,      .value = 2},
3402         { .input = &tegra_pll_m,        .value = 3},
3403         { .input = &tegra_pll_p,        .value = 4},
3404         { .input = &tegra_pll_p_out4,   .value = 5},
3405         { .input = &tegra_pll_p_out3,   .value = 6},
3406         /* { .input = &tegra_clk_d,     .value = 7}, - no use on tegra3 */
3407         { .input = &tegra_pll_x_out0,   .value = 8},
3408         { .input = &tegra_pll_x,        .value = 8 | SUPER_LP_DIV2_BYPASS},
3409         { 0, 0},
3410 };
3411
3412 static struct clk_mux_sel mux_sclk[] = {
3413         { .input = &tegra_clk_m,        .value = 0},
3414         { .input = &tegra_pll_c_out1,   .value = 1},
3415         { .input = &tegra_pll_p_out4,   .value = 2},
3416         { .input = &tegra_pll_p_out3,   .value = 3},
3417         { .input = &tegra_pll_p_out2,   .value = 4},
3418         /* { .input = &tegra_clk_d,     .value = 5}, - no use on tegra3 */
3419         { .input = &tegra_clk_32k,      .value = 6},
3420         { .input = &tegra_pll_m_out1,   .value = 7},
3421         { 0, 0},
3422 };
3423
3424 static struct clk tegra_clk_cclk_g = {
3425         .name   = "cclk_g",
3426         .flags  = DIV_U71 | DIV_U71_INT,
3427         .inputs = mux_cclk_g,
3428         .reg    = 0x368,
3429         .ops    = &tegra_super_ops,
3430         .max_rate = 1400000000,
3431 };
3432
3433 static struct clk tegra_clk_cclk_lp = {
3434         .name   = "cclk_lp",
3435         .flags  = DIV_2 | DIV_U71 | DIV_U71_INT,
3436         .inputs = mux_cclk_lp,
3437         .reg    = 0x370,
3438         .ops    = &tegra_super_ops,
3439         .max_rate = 620000000,
3440 };
3441
3442 static struct clk tegra_clk_sclk = {
3443         .name   = "sclk",
3444         .inputs = mux_sclk,
3445         .reg    = 0x28,
3446         .ops    = &tegra_super_ops,
3447         .max_rate = 300000000,
3448         .min_rate = 40000000,
3449 };
3450
3451 static struct clk tegra_clk_virtual_cpu_g = {
3452         .name      = "cpu_g",
3453         .parent    = &tegra_clk_cclk_g,
3454         .ops       = &tegra_cpu_ops,
3455         .max_rate  = 1400000000,
3456         .u.cpu = {
3457                 .main      = &tegra_pll_x,
3458                 .backup    = &tegra_pll_p,
3459                 .mode      = MODE_G,
3460         },
3461 };
3462
3463 static struct clk tegra_clk_virtual_cpu_lp = {
3464         .name      = "cpu_lp",
3465         .parent    = &tegra_clk_cclk_lp,
3466         .ops       = &tegra_cpu_ops,
3467         .max_rate  = 620000000,
3468         .u.cpu = {
3469                 .main      = &tegra_pll_x,
3470                 .backup    = &tegra_pll_p,
3471                 .mode      = MODE_LP,
3472         },
3473 };
3474
3475 static struct clk_mux_sel mux_cpu_cmplx[] = {
3476         { .input = &tegra_clk_virtual_cpu_g,    .value = 0},
3477         { .input = &tegra_clk_virtual_cpu_lp,   .value = 1},
3478         { 0, 0},
3479 };
3480
3481 static struct clk tegra_clk_cpu_cmplx = {
3482         .name      = "cpu",
3483         .inputs    = mux_cpu_cmplx,
3484         .ops       = &tegra_cpu_cmplx_ops,
3485         .max_rate  = 1400000000,
3486 };
3487
3488 static struct clk tegra_clk_cop = {
3489         .name      = "cop",
3490         .parent    = &tegra_clk_sclk,
3491         .ops       = &tegra_cop_ops,
3492         .max_rate  = 300000000,
3493 };
3494
3495 static struct clk tegra_clk_hclk = {
3496         .name           = "hclk",
3497         .flags          = DIV_BUS,
3498         .parent         = &tegra_clk_sclk,
3499         .reg            = 0x30,
3500         .reg_shift      = 4,
3501         .ops            = &tegra_bus_ops,
3502         .max_rate       = 300000000,
3503         .min_rate       = 40000000,
3504 };
3505
3506 static struct clk tegra_clk_pclk = {
3507         .name           = "pclk",
3508         .flags          = DIV_BUS,
3509         .parent         = &tegra_clk_hclk,
3510         .reg            = 0x30,
3511         .reg_shift      = 0,
3512         .ops            = &tegra_bus_ops,
3513         .max_rate       = 150000000,
3514         .min_rate       = 40000000,
3515 };
3516
3517 static struct clk tegra_clk_sbus_cmplx = {
3518         .name      = "sbus",
3519         .parent    = &tegra_clk_sclk,
3520         .ops       = &tegra_sbus_cmplx_ops,
3521         .u.system  = {
3522                 .pclk = &tegra_clk_pclk,
3523                 .hclk = &tegra_clk_hclk,
3524                 .sclk_low = &tegra_pll_p_out4,
3525                 .sclk_high = &tegra_pll_m_out1,
3526 #ifndef CONFIG_TEGRA_FPGA_PLATFORM
3527                 .threshold = 204000000, /* exact factor of low range pll_p */
3528 #else
3529                 .threshold = 108000000, /* exact factor of low range pll_p */
3530 #endif
3531         },
3532 };
3533
3534 static struct clk tegra_clk_blink = {
3535         .name           = "blink",
3536         .parent         = &tegra_clk_32k,
3537         .reg            = 0x40,
3538         .ops            = &tegra_blink_clk_ops,
3539         .max_rate       = 32768,
3540 };
3541
3542 static struct clk_mux_sel mux_pllm_pllc_pllp_plla[] = {
3543         { .input = &tegra_pll_m, .value = 0},
3544         { .input = &tegra_pll_c, .value = 1},
3545         { .input = &tegra_pll_p, .value = 2},
3546         { .input = &tegra_pll_a_out0, .value = 3},
3547         { 0, 0},
3548 };
3549
3550 static struct clk_mux_sel mux_pllm_pllc_pllp_clkm[] = {
3551         { .input = &tegra_pll_m, .value = 0},
3552         /* { .input = &tegra_pll_c, .value = 1}, not used on tegra3 */
3553         { .input = &tegra_pll_p, .value = 2},
3554         { .input = &tegra_clk_m, .value = 3},
3555         { 0, 0},
3556 };
3557
3558 static struct clk_mux_sel mux_pllp_pllc_pllm_clkm[] = {
3559         { .input = &tegra_pll_p, .value = 0},
3560         { .input = &tegra_pll_c, .value = 1},
3561         { .input = &tegra_pll_m, .value = 2},
3562         { .input = &tegra_clk_m, .value = 3},
3563         { 0, 0},
3564 };
3565
3566 static struct clk_mux_sel mux_pllp_plld_pllc_clkm[] = {
3567         {.input = &tegra_pll_p, .value = 0},
3568         {.input = &tegra_pll_d_out0, .value = 1},
3569         {.input = &tegra_pll_c, .value = 2},
3570         {.input = &tegra_clk_m, .value = 3},
3571         { 0, 0},
3572 };
3573
3574 static struct clk_mux_sel mux_pllp_pllm_plld_plla_pllc_plld2_clkm[] = {
3575         {.input = &tegra_pll_p, .value = 0},
3576         {.input = &tegra_pll_m, .value = 1},
3577         {.input = &tegra_pll_d_out0, .value = 2},
3578         {.input = &tegra_pll_a_out0, .value = 3},
3579         {.input = &tegra_pll_c, .value = 4},
3580         {.input = &tegra_pll_d2_out0, .value = 5},
3581         {.input = &tegra_clk_m, .value = 6},
3582         { 0, 0},
3583 };
3584
3585 static struct clk_mux_sel mux_plla_pllc_pllp_clkm[] = {
3586         { .input = &tegra_pll_a_out0, .value = 0},
3587         { .input = &tegra_pll_c, .value = 1},
3588         { .input = &tegra_pll_p, .value = 2},
3589         { .input = &tegra_clk_m, .value = 3},
3590         { 0, 0},
3591 };
3592
3593 static struct clk_mux_sel mux_pllp_pllc_clk32_clkm[] = {
3594         {.input = &tegra_pll_p,     .value = 0},
3595         {.input = &tegra_pll_c,     .value = 1},
3596         {.input = &tegra_clk_32k,   .value = 2},
3597         {.input = &tegra_clk_m,     .value = 3},
3598         { 0, 0},
3599 };
3600
3601 static struct clk_mux_sel mux_pllp_pllc_pllm[] = {
3602         {.input = &tegra_pll_p,     .value = 0},
3603         {.input = &tegra_pll_c,     .value = 1},
3604         {.input = &tegra_pll_m,     .value = 2},
3605         { 0, 0},
3606 };
3607
3608 static struct clk_mux_sel mux_clk_m[] = {
3609         { .input = &tegra_clk_m, .value = 0},
3610         { 0, 0},
3611 };
3612
3613 static struct clk_mux_sel mux_pllp_out3[] = {
3614         { .input = &tegra_pll_p_out3, .value = 0},
3615         { 0, 0},
3616 };
3617
3618 static struct clk_mux_sel mux_plld_out0[] = {
3619         { .input = &tegra_pll_d_out0, .value = 0},
3620         { 0, 0},
3621 };
3622
3623 static struct clk_mux_sel mux_plld_out0_plld2_out0[] = {
3624         { .input = &tegra_pll_d_out0,  .value = 0},
3625         { .input = &tegra_pll_d2_out0, .value = 1},
3626         { 0, 0},
3627 };
3628
3629 static struct clk_mux_sel mux_clk_32k[] = {
3630         { .input = &tegra_clk_32k, .value = 0},
3631         { 0, 0},
3632 };
3633
3634 static struct clk_mux_sel mux_plla_clk32_pllp_clkm_plle[] = {
3635         { .input = &tegra_pll_a_out0, .value = 0},
3636         { .input = &tegra_clk_32k,    .value = 1},
3637         { .input = &tegra_pll_p,      .value = 2},
3638         { .input = &tegra_clk_m,      .value = 3},
3639         { .input = &tegra_pll_e,      .value = 4},
3640         { 0, 0},
3641 };
3642
3643 static struct raw_notifier_head emc_rate_change_nh;
3644
3645 static struct clk tegra_clk_emc = {
3646         .name = "emc",
3647         .ops = &tegra_emc_clk_ops,
3648         .reg = 0x19c,
3649         .max_rate = 800000000,
3650         .min_rate = 50000000,
3651         .inputs = mux_pllm_pllc_pllp_clkm,
3652         .flags = MUX | DIV_U71 | PERIPH_EMC_ENB,
3653         .u.periph = {
3654                 .clk_num = 57,
3655         },
3656         .rate_change_nh = &emc_rate_change_nh,
3657 };
3658
3659 static struct clk tegra_clk_cbus = {
3660         .name      = "cbus",
3661         .parent    = &tegra_pll_c,
3662         .ops       = &tegra_clk_cbus_ops,
3663         .max_rate  = 700000000,
3664         .shared_bus_backup = {
3665                 .input = &tegra_pll_p,
3666                 .value = 2,
3667         }
3668 };
3669
3670 #define PERIPH_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags) \
3671         {                                               \
3672                 .name      = _name,                     \
3673                 .lookup    = {                          \
3674                         .dev_id    = _dev,              \
3675                         .con_id    = _con,              \
3676                 },                                      \
3677                 .ops       = &tegra_periph_clk_ops,     \
3678                 .reg       = _reg,                      \
3679                 .inputs    = _inputs,                   \
3680                 .flags     = _flags,                    \
3681                 .max_rate  = _max,                      \
3682                 .u.periph = {                           \
3683                         .clk_num   = _clk_num,          \
3684                 },                                      \
3685         }
3686
3687 #define PERIPH_CLK_EX(_name, _dev, _con, _clk_num, _reg, _max, _inputs, \
3688                         _flags, _ops)                                   \
3689         {                                               \
3690                 .name      = _name,                     \
3691                 .lookup    = {                          \
3692                         .dev_id    = _dev,              \
3693                         .con_id    = _con,              \
3694                 },                                      \
3695                 .ops       = _ops,                      \
3696                 .reg       = _reg,                      \
3697                 .inputs    = _inputs,                   \
3698                 .flags     = _flags,                    \
3699                 .max_rate  = _max,                      \
3700                 .u.periph = {                           \
3701                         .clk_num   = _clk_num,          \
3702                 },                                      \
3703         }
3704
3705 #define SHARED_CLK(_name, _dev, _con, _parent, _id, _div)\
3706         {                                               \
3707                 .name      = _name,                     \
3708                 .lookup    = {                          \
3709                         .dev_id    = _dev,              \
3710                         .con_id    = _con,              \
3711                 },                                      \
3712                 .ops       = &tegra_clk_shared_bus_ops, \
3713                 .parent = _parent,                      \
3714                 .u.shared_bus_user = {                  \
3715                         .client_id = _id,               \
3716                         .client_div = _div,             \
3717                 },                                      \
3718         }
3719 struct clk tegra_list_clks[] = {
3720         PERIPH_CLK("apbdma",    "tegra-dma",            NULL,   34,     0,      26000000,  mux_clk_m,                   0),
3721         PERIPH_CLK("rtc",       "rtc-tegra",            NULL,   4,      0,      32768,     mux_clk_32k,                 PERIPH_NO_RESET | PERIPH_ON_APB),
3722         PERIPH_CLK("kbc",       "tegra-kbc",            NULL,   36,     0,      32768,     mux_clk_32k,                 PERIPH_NO_RESET | PERIPH_ON_APB),
3723         PERIPH_CLK("timer",     "timer",                NULL,   5,      0,      26000000,  mux_clk_m,                   0),
3724         PERIPH_CLK("kfuse",     "kfuse-tegra",          NULL,   40,     0,      26000000,  mux_clk_m,                   0),
3725         PERIPH_CLK("apbif",     "tegra30-ahub",         "apbif", 107,   0,      26000000,  mux_clk_m,                   0),
3726         PERIPH_CLK("i2s0",      "tegra30-i2s.0",        NULL,   30,     0x1d8,  26000000,  mux_pllaout0_audio0_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
3727         PERIPH_CLK("i2s1",      "tegra30-i2s.1",        NULL,   11,     0x100,  26000000,  mux_pllaout0_audio1_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
3728         PERIPH_CLK("i2s2",      "tegra30-i2s.2",        NULL,   18,     0x104,  26000000,  mux_pllaout0_audio2_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
3729         PERIPH_CLK("i2s3",      "tegra30-i2s.3",        NULL,   101,    0x3bc,  26000000,  mux_pllaout0_audio3_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
3730         PERIPH_CLK("i2s4",      "tegra30-i2s.4",        NULL,   102,    0x3c0,  26000000,  mux_pllaout0_audio4_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
3731         PERIPH_CLK("spdif_out", "spdif_out",            NULL,   10,     0x108,  100000000, mux_pllaout0_audio_2x_pllp_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3732         PERIPH_CLK("spdif_in",  "spdif_in",             NULL,   10,     0x10c,  100000000, mux_pllp_pllc_pllm,          MUX | DIV_U71 | PERIPH_ON_APB),
3733         PERIPH_CLK("pwm",       "pwm",                  NULL,   17,     0x110,  432000000, mux_pllp_pllc_clk32_clkm,    MUX | MUX_PWM | DIV_U71 | PERIPH_ON_APB),
3734         PERIPH_CLK("d_audio",   "tegra30-ahub",         "d_audio", 106, 0x3d0,  48000000,  mux_plla_pllc_pllp_clkm,     MUX | DIV_U71),
3735         PERIPH_CLK("dam0",      "dam.0",                NULL,   108,    0x3d8,  48000000,  mux_plla_pllc_pllp_clkm,     MUX | DIV_U71),
3736         PERIPH_CLK("dam1",      "dam.1",                NULL,   109,    0x3dc,  48000000,  mux_plla_pllc_pllp_clkm,     MUX | DIV_U71),
3737         PERIPH_CLK("dam2",      "dam.2",                NULL,   110,    0x3e0,  48000000,  mux_plla_pllc_pllp_clkm,     MUX | DIV_U71),
3738         PERIPH_CLK("hda",       "hda",                  NULL,   125,    0x428,  108000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
3739         PERIPH_CLK("hda2codec_2x",      "hda2codec_2x", NULL,   111,    0x3e4,  48000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
3740         PERIPH_CLK("hda2hdmi",  "hda2hdmi",             NULL,   128,    0,      48000000,  mux_clk_m,                   0),
3741         PERIPH_CLK("xio",       "xio",                  NULL,   45,     0x120,  150000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
3742         PERIPH_CLK("twc",       "twc",                  NULL,   16,     0x12c,  150000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3743         PERIPH_CLK("sbc1",      "spi_tegra.0",          NULL,   41,     0x134,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3744         PERIPH_CLK("sbc2",      "spi_tegra.1",          NULL,   44,     0x118,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3745         PERIPH_CLK("sbc3",      "spi_tegra.2",          NULL,   46,     0x11c,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3746         PERIPH_CLK("sbc4",      "spi_tegra.3",          NULL,   68,     0x1b4,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3747         PERIPH_CLK("sbc5",      "spi_tegra.4",          NULL,   104,    0x3c8,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3748         PERIPH_CLK("sbc6",      "spi_tegra.5",          NULL,   105,    0x3cc,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3749         PERIPH_CLK("sata_oob",  "tegra_sata_oob",       NULL,   123,    0x420,  216000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
3750         PERIPH_CLK("sata",      "tegra_sata",           NULL,   124,    0x424,  216000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
3751         PERIPH_CLK("sata_cold", "tegra_sata_cold",      NULL,   129,    0,      48000000,  mux_clk_m,                   0),
3752         PERIPH_CLK_EX("ndflash","tegra_nand",           NULL,   13,     0x160,  240000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71,  &tegra_nand_clk_ops),
3753         PERIPH_CLK("ndspeed",   "tegra_nand_speed",     NULL,   80,     0x3f8,  240000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
3754         PERIPH_CLK("vfir",      "vfir",                 NULL,   7,      0x168,  72000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3755         PERIPH_CLK("sdmmc1",    "sdhci-tegra.0",        NULL,   14,     0x150,  208000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
3756         PERIPH_CLK("sdmmc2",    "sdhci-tegra.1",        NULL,   9,      0x154,  104000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
3757         PERIPH_CLK("sdmmc3",    "sdhci-tegra.2",        NULL,   69,     0x1bc,  208000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
3758         PERIPH_CLK("sdmmc4",    "sdhci-tegra.3",        NULL,   15,     0x164,  104000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
3759         PERIPH_CLK("vcp",       "tegra-avp",            "vcp",  29,     0,      250000000, mux_clk_m,                   0),
3760         PERIPH_CLK("bsea",      "tegra-avp",            "bsea", 62,     0,      250000000, mux_clk_m,                   0),
3761         PERIPH_CLK("bsev",      "tegra-aes",            "bsev", 63,     0,      250000000, mux_clk_m,                   0),
3762         PERIPH_CLK("vde",       "vde",                  NULL,   61,     0x1c8,  520000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | DIV_U71_INT),
3763         PERIPH_CLK("csite",     "csite",                NULL,   73,     0x1d4,  144000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* max rate ??? */
3764         PERIPH_CLK("la",        "la",                   NULL,   76,     0x1f8,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
3765         PERIPH_CLK("owr",       "tegra_w1",             NULL,   71,     0x1cc,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
3766         PERIPH_CLK("nor",       "nor",                  NULL,   42,     0x1d0,  127000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* requires min voltage */
3767         PERIPH_CLK("mipi",      "mipi",                 NULL,   50,     0x174,  60000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB), /* scales with voltage */
3768         PERIPH_CLK("i2c1",      "tegra-i2c.0",          NULL,   12,     0x124,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16 | PERIPH_ON_APB),
3769         PERIPH_CLK("i2c2",      "tegra-i2c.1",          NULL,   54,     0x198,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16 | PERIPH_ON_APB),
3770         PERIPH_CLK("i2c3",      "tegra-i2c.2",          NULL,   67,     0x1b8,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16 | PERIPH_ON_APB),
3771         PERIPH_CLK("i2c4",      "tegra-i2c.3",          NULL,   103,    0x3c4,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16 | PERIPH_ON_APB),
3772         PERIPH_CLK("i2c5",      "tegra-i2c.4",          NULL,   47,     0x128,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16 | PERIPH_ON_APB),
3773         PERIPH_CLK("uarta",     "tegra_uart.0",         NULL, 6,        0x178,  600000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | DIV_U71_UART | PERIPH_ON_APB),
3774         PERIPH_CLK("uartb",     "tegra_uart.1",         NULL, 7,        0x17c,  600000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | DIV_U71_UART | PERIPH_ON_APB),
3775         PERIPH_CLK("uartc",     "tegra_uart.2",         NULL, 55,       0x1a0,  600000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | DIV_U71_UART | PERIPH_ON_APB),
3776         PERIPH_CLK("uartd",     "tegra_uart.3",         NULL, 65,       0x1c0,  600000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | DIV_U71_UART | PERIPH_ON_APB),
3777         PERIPH_CLK("uarte",     "tegra_uart.4",         NULL, 66,       0x1c4,  600000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | DIV_U71_UART | PERIPH_ON_APB),
3778         PERIPH_CLK("3d",        "3d",                   NULL,   24,     0x158,  520000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | DIV_U71_INT | PERIPH_MANUAL_RESET),
3779         PERIPH_CLK("3d2",       "3d2",                  NULL,   98,     0x3b0,  520000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | DIV_U71_INT | PERIPH_MANUAL_RESET),
3780         PERIPH_CLK("2d",        "2d",                   NULL,   21,     0x15c,  520000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | DIV_U71_INT),
3781         PERIPH_CLK_EX("vi",     "tegra_camera",         "vi",   20,     0x148,  300000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | DIV_U71_INT,    &tegra_vi_clk_ops),
3782         PERIPH_CLK("vi_sensor", "tegra_camera",         "vi_sensor",    20,     0x1a8,  150000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_NO_RESET),
3783         PERIPH_CLK("epp",       "epp",                  NULL,   19,     0x16c,  520000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | DIV_U71_INT),
3784         PERIPH_CLK("mpe",       "mpe",                  NULL,   60,     0x170,  520000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | DIV_U71_INT),
3785         PERIPH_CLK("host1x",    "host1x",               NULL,   28,     0x180,  260000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | DIV_U71_INT),
3786         PERIPH_CLK("cve",       "cve",                  NULL,   49,     0x140,  250000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
3787         PERIPH_CLK("tvo",       "tvo",                  NULL,   49,     0x188,  250000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
3788         PERIPH_CLK_EX("dtv",    "dtv",                  NULL,   79,     0x1dc,  250000000, mux_clk_m,                   0,              &tegra_dtv_clk_ops),
3789         PERIPH_CLK("hdmi",      "hdmi",                 NULL,   51,     0x18c,  148500000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX | MUX8 | DIV_U71),
3790         PERIPH_CLK("tvdac",     "tvdac",                NULL,   53,     0x194,  250000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
3791         PERIPH_CLK("disp1",     "tegradc.0",            NULL,   27,     0x138,  600000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX | MUX8),
3792         PERIPH_CLK("disp2",     "tegradc.1",            NULL,   26,     0x13c,  600000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX | MUX8),
3793         PERIPH_CLK("usbd",      "fsl-tegra-udc",        NULL,   22,     0,      480000000, mux_clk_m,                   0), /* requires min voltage */
3794         PERIPH_CLK("usb2",      "tegra-ehci.1",         NULL,   58,     0,      480000000, mux_clk_m,                   0), /* requires min voltage */
3795         PERIPH_CLK("usb3",      "tegra-ehci.2",         NULL,   59,     0,      480000000, mux_clk_m,                   0), /* requires min voltage */
3796         PERIPH_CLK("dsia",      "tegradc.0",            "dsia", 48,     0,      500000000, mux_plld_out0,               0),
3797         PERIPH_CLK_EX("dsib",   "tegradc.1",            "dsib", 82,     0xd0,   500000000, mux_plld_out0_plld2_out0,    MUX | PLLD,     &tegra_dsib_clk_ops),
3798         PERIPH_CLK("csi",       "tegra_camera",         "csi",  52,     0,      102000000, mux_pllp_out3,               0),
3799         PERIPH_CLK("isp",       "tegra_camera",         "isp",  23,     0,      150000000, mux_clk_m,                   0), /* same frequency as VI */
3800         PERIPH_CLK("csus",      "tegra_camera",         "csus", 92,     0,      150000000, mux_clk_m,                   PERIPH_NO_RESET),
3801
3802         PERIPH_CLK("actmon",    "actmon",               NULL,   119,    0x3e8,  216000000, mux_pllp_pllc_clk32_clkm,    MUX | DIV_U71),
3803         PERIPH_CLK("extern1",   "extern1",              NULL,   120,    0x3ec,  216000000, mux_plla_clk32_pllp_clkm_plle,       MUX | MUX8 | DIV_U71),
3804         PERIPH_CLK("extern2",   "extern2",              NULL,   121,    0x3f0,  216000000, mux_plla_clk32_pllp_clkm_plle,       MUX | MUX8 | DIV_U71),
3805         PERIPH_CLK("extern3",   "extern3",              NULL,   122,    0x3f4,  216000000, mux_plla_clk32_pllp_clkm_plle,       MUX | MUX8 | DIV_U71),
3806         PERIPH_CLK("i2cslow",   "i2cslow",              NULL,   81,     0x3fc,  26000000,  mux_pllp_pllc_clk32_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
3807         PERIPH_CLK("pcie",      "tegra-pcie",           "pcie", 70,     0,      250000000, mux_clk_m,                   0),
3808         PERIPH_CLK("afi",       "tegra-pcie",           "afi",  72,     0,      250000000, mux_clk_m,                   0),
3809         PERIPH_CLK("se",        "se",                   NULL,   127,    0x42c,  520000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71| DIV_U71_INT),
3810
3811         SHARED_CLK("avp.sclk",  "tegra-avp",            "sclk", &tegra_clk_sbus_cmplx, NULL, 0),
3812         SHARED_CLK("bsea.sclk", "tegra-aes",            "sclk", &tegra_clk_sbus_cmplx, NULL, 0),
3813         SHARED_CLK("usbd.sclk", "fsl-tegra-udc",        "sclk", &tegra_clk_sbus_cmplx, NULL, 0),
3814         SHARED_CLK("usb1.sclk", "tegra-ehci.0",         "sclk", &tegra_clk_sbus_cmplx, NULL, 0),
3815         SHARED_CLK("usb2.sclk", "tegra-ehci.1",         "sclk", &tegra_clk_sbus_cmplx, NULL, 0),
3816         SHARED_CLK("usb3.sclk", "tegra-ehci.2",         "sclk", &tegra_clk_sbus_cmplx, NULL, 0),
3817         SHARED_CLK("avp.emc",   "tegra-avp",            "emc",  &tegra_clk_emc, NULL, 0),
3818         SHARED_CLK("cpu.emc",   "cpu",                  "emc",  &tegra_clk_emc, NULL, 0),
3819         SHARED_CLK("disp1.emc", "tegradc.0",            "emc",  &tegra_clk_emc, NULL, 0),
3820         SHARED_CLK("disp2.emc", "tegradc.1",            "emc",  &tegra_clk_emc, NULL, 0),
3821         SHARED_CLK("hdmi.emc",  "hdmi",                 "emc",  &tegra_clk_emc, NULL, 0),
3822         SHARED_CLK("host.emc",  "tegra_grhost",         "emc",  &tegra_clk_emc, NULL, 0),
3823         SHARED_CLK("usbd.emc",  "fsl-tegra-udc",        "emc",  &tegra_clk_emc, NULL, 0),
3824         SHARED_CLK("usb1.emc",  "tegra-ehci.0",         "emc",  &tegra_clk_emc, NULL, 0),
3825         SHARED_CLK("usb2.emc",  "tegra-ehci.1",         "emc",  &tegra_clk_emc, NULL, 0),
3826         SHARED_CLK("usb3.emc",  "tegra-ehci.2",         "emc",  &tegra_clk_emc, NULL, 0),
3827         SHARED_CLK("mon.emc",   "tegra_actmon",         "emc",  &tegra_clk_emc, NULL, 0),
3828
3829         SHARED_CLK("host1x.cbus","tegra_grhost",        "host1x", &tegra_clk_cbus, "host1x", 2),
3830         SHARED_CLK("3d.cbus",   "tegra_grhost",         "gr3d", &tegra_clk_cbus, "3d",  0),
3831         SHARED_CLK("3d2.cbus",  "tegra_grhost",         "gr3d2",&tegra_clk_cbus, "3d2", 0),
3832         SHARED_CLK("2d.cbus",   "tegra_grhost",         "gr2d", &tegra_clk_cbus, "2d",  0),
3833         SHARED_CLK("epp.cbus",  "tegra_grhost",         "epp",  &tegra_clk_cbus, "epp", 0),
3834         SHARED_CLK("mpe.cbus",  "tegra_grhost",         "mpe",  &tegra_clk_cbus, "mpe", 0),
3835         SHARED_CLK("vde.cbus",  "tegra-avp",            "vde",  &tegra_clk_cbus, "vde", 0),
3836         SHARED_CLK("se.cbus",   "tegra-se",             NULL,   &tegra_clk_cbus, "se",  0),
3837 };
3838
3839 #define CLK_DUPLICATE(_name, _dev, _con)                \
3840         {                                               \
3841                 .name   = _name,                        \
3842                 .lookup = {                             \
3843                         .dev_id = _dev,                 \
3844                         .con_id         = _con,         \
3845                 },                                      \
3846         }
3847
3848 /* Some clocks may be used by different drivers depending on the board
3849  * configuration.  List those here to register them twice in the clock lookup
3850  * table under two names.
3851  */
3852 struct clk_duplicate tegra_clk_duplicates[] = {
3853         CLK_DUPLICATE("uarta",  "serial8250.0", "uarta"),
3854         CLK_DUPLICATE("uartb",  "serial8250.0", "uartb"),
3855         CLK_DUPLICATE("uartc",  "serial8250.0", "uartc"),
3856         CLK_DUPLICATE("uartd",  "serial8250.0", "uartd"),
3857         CLK_DUPLICATE("uarte",  "serial8250.0", "uarte"),
3858         CLK_DUPLICATE("usbd", "utmip-pad", NULL),
3859         CLK_DUPLICATE("usbd", "tegra-ehci.0", NULL),
3860         CLK_DUPLICATE("usbd", "tegra-otg", NULL),
3861         CLK_DUPLICATE("hdmi", "tegradc.0", "hdmi"),
3862         CLK_DUPLICATE("hdmi", "tegradc.1", "hdmi"),
3863         CLK_DUPLICATE("dsib", "tegradc.0", "dsib"),
3864         CLK_DUPLICATE("dsia", "tegradc.1", "dsia"),
3865         CLK_DUPLICATE("pwm", "tegra_pwm.0", NULL),
3866         CLK_DUPLICATE("pwm", "tegra_pwm.1", NULL),
3867         CLK_DUPLICATE("pwm", "tegra_pwm.2", NULL),
3868         CLK_DUPLICATE("pwm", "tegra_pwm.3", NULL),
3869         CLK_DUPLICATE("cop", "tegra-avp", "cop"),
3870         CLK_DUPLICATE("vde", "tegra-aes", "vde"),
3871         CLK_DUPLICATE("cml1", "tegra_sata_cml", NULL),
3872         CLK_DUPLICATE("cml0", "tegra_pcie", "cml"),
3873         CLK_DUPLICATE("pciex", "tegra_pcie", "pciex"),
3874         CLK_DUPLICATE("i2c1", "tegra-i2c-slave.0", NULL),
3875         CLK_DUPLICATE("i2c2", "tegra-i2c-slave.1", NULL),
3876         CLK_DUPLICATE("i2c3", "tegra-i2c-slave.2", NULL),
3877         CLK_DUPLICATE("i2c4", "tegra-i2c-slave.3", NULL),
3878         CLK_DUPLICATE("i2c5", "tegra-i2c-slave.4", NULL),
3879         CLK_DUPLICATE("sbc1", "spi_slave_tegra.0", NULL),
3880         CLK_DUPLICATE("sbc2", "spi_slave_tegra.1", NULL),
3881         CLK_DUPLICATE("sbc3", "spi_slave_tegra.2", NULL),
3882         CLK_DUPLICATE("sbc4", "spi_slave_tegra.3", NULL),
3883         CLK_DUPLICATE("sbc5", "spi_slave_tegra.4", NULL),
3884         CLK_DUPLICATE("sbc6", "spi_slave_tegra.5", NULL),
3885         CLK_DUPLICATE("twd", "smp_twd", NULL),
3886 };
3887
3888 struct clk *tegra_ptr_clks[] = {
3889         &tegra_clk_32k,
3890         &tegra_clk_m,
3891         &tegra_clk_m_div2,
3892         &tegra_clk_m_div4,
3893         &tegra_pll_ref,
3894         &tegra_pll_m,
3895         &tegra_pll_m_out1,
3896         &tegra_pll_c,
3897         &tegra_pll_c_out1,
3898         &tegra_pll_p,
3899         &tegra_pll_p_out1,
3900         &tegra_pll_p_out2,
3901         &tegra_pll_p_out3,
3902         &tegra_pll_p_out4,
3903         &tegra_pll_a,
3904         &tegra_pll_a_out0,
3905         &tegra_pll_d,
3906         &tegra_pll_d_out0,
3907         &tegra_pll_d2,
3908         &tegra_pll_d2_out0,
3909         &tegra_pll_u,
3910         &tegra_pll_x,
3911         &tegra_pll_x_out0,
3912         &tegra_pll_e,
3913         &tegra_cml0_clk,
3914         &tegra_cml1_clk,
3915         &tegra_pciex_clk,
3916         &tegra_clk_cclk_g,
3917         &tegra_clk_cclk_lp,
3918         &tegra_clk_sclk,
3919         &tegra_clk_hclk,
3920         &tegra_clk_pclk,
3921         &tegra_clk_virtual_cpu_g,
3922         &tegra_clk_virtual_cpu_lp,
3923         &tegra_clk_cpu_cmplx,
3924         &tegra_clk_blink,
3925         &tegra_clk_cop,
3926         &tegra_clk_sbus_cmplx,
3927         &tegra_clk_emc,
3928         &tegra3_clk_twd,
3929         &tegra_clk_cbus,
3930 };
3931
3932 static struct tegra_edp_limits default_cpu_edp_limits[] = {
3933         {90, { 1000000, 1000000, 1000000, 1000000 } },
3934 };
3935
3936 static void tegra3_init_one_clock(struct clk *c)
3937 {
3938         clk_init(c);
3939         INIT_LIST_HEAD(&c->shared_bus_list);
3940         if (!c->lookup.dev_id && !c->lookup.con_id)
3941                 c->lookup.con_id = c->name;
3942         c->lookup.clk = c;
3943         clkdev_add(&c->lookup);
3944 }
3945
3946 void __init tegra_soc_init_clocks(void)
3947 {
3948         int i;
3949         struct clk *c;
3950
3951         for (i = 0; i < ARRAY_SIZE(tegra_ptr_clks); i++)
3952                 tegra3_init_one_clock(tegra_ptr_clks[i]);
3953
3954         for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++)
3955                 tegra3_init_one_clock(&tegra_list_clks[i]);
3956
3957         for (i = 0; i < ARRAY_SIZE(tegra_clk_duplicates); i++) {
3958                 c = tegra_get_clock_by_name(tegra_clk_duplicates[i].name);
3959                 if (!c) {
3960                         pr_err("%s: Unknown duplicate clock %s\n", __func__,
3961                                 tegra_clk_duplicates[i].name);
3962                         continue;
3963                 }
3964
3965                 tegra_clk_duplicates[i].lookup.clk = c;
3966                 clkdev_add(&tegra_clk_duplicates[i].lookup);
3967         }
3968
3969         for (i = 0; i < ARRAY_SIZE(tegra_sync_source_list); i++)
3970                 tegra3_init_one_clock(&tegra_sync_source_list[i]);
3971         for (i = 0; i < ARRAY_SIZE(tegra_clk_audio_list); i++)
3972                 tegra3_init_one_clock(&tegra_clk_audio_list[i]);
3973         for (i = 0; i < ARRAY_SIZE(tegra_clk_audio_2x_list); i++)
3974                 tegra3_init_one_clock(&tegra_clk_audio_2x_list[i]);
3975
3976         init_clk_out_mux();
3977         for (i = 0; i < ARRAY_SIZE(tegra_clk_out_list); i++)
3978                 tegra3_init_one_clock(&tegra_clk_out_list[i]);
3979
3980         tegra_init_cpu_edp_limits(default_cpu_edp_limits,
3981                                   ARRAY_SIZE(default_cpu_edp_limits));
3982 }
3983
3984 #ifdef CONFIG_CPU_FREQ
3985
3986 /*
3987  * Frequency table index must be sequential starting at 0 and frequencies
3988  * must be ascending.
3989  */
3990
3991 static struct cpufreq_frequency_table freq_table_300MHz[] = {
3992         { 0, 204000 },
3993         { 1, 300000 },
3994         { 2, CPUFREQ_TABLE_END },
3995 };
3996
3997 static struct cpufreq_frequency_table freq_table_1p0GHz[] = {
3998         { 0, 102000 },
3999         { 1, 204000 },
4000         { 2, 312000 },
4001         { 3, 456000 },
4002         { 4, 608000 },
4003         { 5, 760000 },
4004         { 6, 816000 },
4005         { 7, 912000 },
4006         { 8, 1000000 },
4007         { 9, CPUFREQ_TABLE_END },
4008 };
4009
4010 static struct cpufreq_frequency_table freq_table_1p3GHz[] = {
4011         { 0,  102000 },
4012         { 1,  204000 },
4013         { 2,  340000 },
4014         { 3,  480000 },
4015         { 4,  640000 },
4016         { 5,  760000 },
4017         { 6,  880000 },
4018         { 7, 1000000 },
4019         { 8, 1100000 },
4020         { 9, 1200000 },
4021         {10, 1300000 },
4022         {11, CPUFREQ_TABLE_END },
4023 };
4024
4025 static struct cpufreq_frequency_table freq_table_1p4GHz[] = {
4026         { 0,  102000 },
4027         { 1,  204000 },
4028         { 2,  370000 },
4029         { 3,  480000 },
4030         { 4,  620000 },
4031         { 5,  760000 },
4032         { 6,  880000 },
4033         { 7, 1000000 },
4034         { 8, 1100000 },
4035         { 9, 1200000 },
4036         {10, 1300000 },
4037         {11, 1400000 },
4038         {12, CPUFREQ_TABLE_END },
4039 };
4040
4041 static struct tegra_cpufreq_table_data cpufreq_tables[] = {
4042         { freq_table_300MHz, 0, 1 },
4043         { freq_table_1p0GHz, 2, 7 },
4044         { freq_table_1p3GHz, 2, 9, 1},
4045         { freq_table_1p4GHz, 2, 10, 1},
4046 };
4047
4048 static void clip_cpu_rate_limits(
4049         struct cpufreq_frequency_table *freq_table,
4050         struct cpufreq_policy *policy,
4051         struct clk *cpu_clk_g,
4052         struct clk *cpu_clk_lp)
4053 {
4054         int idx, ret;
4055
4056         /* clip CPU LP mode maximum frequency to table entry, and
4057            set CPU G mode minimum frequency one table step below */
4058         ret = cpufreq_frequency_table_target(policy, freq_table,
4059                 cpu_clk_lp->max_rate / 1000, CPUFREQ_RELATION_H, &idx);
4060         if (ret || !idx) {
4061                 pr_err("%s: LP CPU max rate %lu %s of cpufreq table", __func__,
4062                        cpu_clk_lp->max_rate, ret ? "outside" : "at the bottom");
4063                 BUG();
4064         }
4065         cpu_clk_lp->max_rate = freq_table[idx].frequency * 1000;
4066         cpu_clk_g->min_rate = freq_table[idx-1].frequency * 1000;
4067 }
4068
4069 struct tegra_cpufreq_table_data *tegra_cpufreq_table_get(void)
4070 {
4071         int i, ret;
4072         struct clk *cpu_clk_g = tegra_get_clock_by_name("cpu_g");
4073         struct clk *cpu_clk_lp = tegra_get_clock_by_name("cpu_lp");
4074
4075         for (i = 0; i < ARRAY_SIZE(cpufreq_tables); i++) {
4076                 struct cpufreq_policy policy;
4077                 policy.cpu = 0; /* any on-line cpu */
4078                 ret = cpufreq_frequency_table_cpuinfo(
4079                         &policy, cpufreq_tables[i].freq_table);
4080                 BUG_ON(ret);
4081                 if ((policy.max * 1000) == cpu_clk_g->max_rate) {
4082                         clip_cpu_rate_limits(cpufreq_tables[i].freq_table,
4083                                 &policy, cpu_clk_g, cpu_clk_lp);
4084                         return &cpufreq_tables[i];
4085                 }
4086         }
4087         pr_err("%s: No cpufreq table matching cpu range", __func__);
4088         BUG();
4089         return &cpufreq_tables[0];
4090 }
4091
4092 unsigned long tegra_emc_to_cpu_ratio(unsigned long cpu_rate)
4093 {
4094         static unsigned long emc_max_rate = 0;
4095
4096         if (emc_max_rate == 0)
4097                 emc_max_rate = clk_round_rate(
4098                         tegra_get_clock_by_name("emc"), ULONG_MAX);
4099
4100         /* Vote on memory bus frequency based on cpu frequency;
4101            cpu rate is in kHz, emc rate is in Hz */
4102         if (cpu_rate >= 750000)
4103                 return emc_max_rate;    /* cpu >= 750 MHz, emc max */
4104         else if (cpu_rate >= 450000)
4105                 return emc_max_rate/2;  /* cpu >= 500 MHz, emc max/2 */
4106         else if (cpu_rate >= 250000)
4107                 return 100000000;       /* cpu >= 250 MHz, emc 100 MHz */
4108         else
4109                 return 0;               /* emc min */
4110 }
4111 #endif
4112
4113 #ifdef CONFIG_PM_SLEEP
4114 static u32 clk_rst_suspend[RST_DEVICES_NUM + CLK_OUT_ENB_NUM +
4115                            PERIPH_CLK_SOURCE_NUM + 18];
4116
4117 void tegra_clk_suspend(void)
4118 {
4119         unsigned long off;
4120         u32 *ctx = clk_rst_suspend;
4121
4122         *ctx++ = clk_readl(OSC_CTRL) & OSC_CTRL_MASK;
4123         *ctx++ = clk_readl(CPU_SOFTRST_CTRL);
4124         *ctx++ = clk_readl(tegra_pll_c.reg + PLL_BASE);
4125         *ctx++ = clk_readl(tegra_pll_c.reg + PLL_MISC(&tegra_pll_c));
4126         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_BASE);
4127         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
4128
4129         *ctx++ = clk_readl(tegra_pll_m_out1.reg);
4130         *ctx++ = clk_readl(tegra_pll_a_out0.reg);
4131         *ctx++ = clk_readl(tegra_pll_c_out1.reg);
4132
4133         *ctx++ = clk_readl(tegra_clk_cclk_g.reg);
4134         *ctx++ = clk_readl(tegra_clk_cclk_g.reg + SUPER_CLK_DIVIDER);
4135         *ctx++ = clk_readl(tegra_clk_cclk_lp.reg);
4136         *ctx++ = clk_readl(tegra_clk_cclk_lp.reg + SUPER_CLK_DIVIDER);
4137
4138         *ctx++ = clk_readl(tegra_clk_sclk.reg);
4139         *ctx++ = clk_readl(tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
4140         *ctx++ = clk_readl(tegra_clk_pclk.reg);
4141
4142         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_OSC;
4143                         off += 4) {
4144                 if (off == PERIPH_CLK_SOURCE_EMC)
4145                         continue;
4146                 *ctx++ = clk_readl(off);
4147         }
4148         for (off = PERIPH_CLK_SOURCE_G3D2; off <= PERIPH_CLK_SOURCE_SE;
4149                         off+=4) {
4150                 *ctx++ = clk_readl(off);
4151         }
4152
4153         *ctx++ = clk_readl(RST_DEVICES_L);
4154         *ctx++ = clk_readl(RST_DEVICES_H);
4155         *ctx++ = clk_readl(RST_DEVICES_U);
4156         *ctx++ = clk_readl(RST_DEVICES_V);
4157         *ctx++ = clk_readl(RST_DEVICES_W);
4158
4159         *ctx++ = clk_readl(CLK_OUT_ENB_L);
4160         *ctx++ = clk_readl(CLK_OUT_ENB_H);
4161         *ctx++ = clk_readl(CLK_OUT_ENB_U);
4162         *ctx++ = clk_readl(CLK_OUT_ENB_V);
4163         *ctx++ = clk_readl(CLK_OUT_ENB_W);
4164
4165         *ctx++ = clk_readl(MISC_CLK_ENB);
4166         *ctx++ = clk_readl(CLK_MASK_ARM);
4167 }
4168
4169 void tegra_clk_resume(void)
4170 {
4171         unsigned long off;
4172         const u32 *ctx = clk_rst_suspend;
4173         u32 val;
4174
4175         val = clk_readl(OSC_CTRL) & ~OSC_CTRL_MASK;
4176         val |= *ctx++;
4177         clk_writel(val, OSC_CTRL);
4178         clk_writel(*ctx++, CPU_SOFTRST_CTRL);
4179
4180         /* FIXME: add plld, and wait for lock */
4181         clk_writel(*ctx++, tegra_pll_c.reg + PLL_BASE);
4182         clk_writel(*ctx++, tegra_pll_c.reg + PLL_MISC(&tegra_pll_c));
4183         clk_writel(*ctx++, tegra_pll_a.reg + PLL_BASE);
4184         clk_writel(*ctx++, tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
4185         udelay(300);
4186
4187         clk_writel(*ctx++, tegra_pll_m_out1.reg);
4188         clk_writel(*ctx++, tegra_pll_a_out0.reg);
4189         clk_writel(*ctx++, tegra_pll_c_out1.reg);
4190
4191         clk_writel(*ctx++, tegra_clk_cclk_g.reg);
4192         clk_writel(*ctx++, tegra_clk_cclk_g.reg + SUPER_CLK_DIVIDER);
4193         clk_writel(*ctx++, tegra_clk_cclk_lp.reg);
4194         clk_writel(*ctx++, tegra_clk_cclk_lp.reg + SUPER_CLK_DIVIDER);
4195
4196         clk_writel(*ctx++, tegra_clk_sclk.reg);
4197         clk_writel(*ctx++, tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
4198         clk_writel(*ctx++, tegra_clk_pclk.reg);
4199
4200         /* enable all clocks before configuring clock sources */
4201         clk_writel(0xfdfffff1ul, CLK_OUT_ENB_L);
4202         clk_writel(0xfefff7f7ul, CLK_OUT_ENB_H);
4203         clk_writel(0x75f79bfful, CLK_OUT_ENB_U);
4204         clk_writel(0xfffffffful, CLK_OUT_ENB_V);
4205         clk_writel(0x00003ffful, CLK_OUT_ENB_W);
4206         wmb();
4207
4208         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_OSC;
4209                         off += 4) {
4210                 if (off == PERIPH_CLK_SOURCE_EMC)
4211                         continue;
4212                 clk_writel(*ctx++, off);
4213         }
4214         for (off = PERIPH_CLK_SOURCE_G3D2; off <= PERIPH_CLK_SOURCE_SE;
4215                         off += 4) {
4216                 clk_writel(*ctx++, off);
4217         }
4218         wmb();
4219
4220         clk_writel(*ctx++, RST_DEVICES_L);
4221         clk_writel(*ctx++, RST_DEVICES_H);
4222         clk_writel(*ctx++, RST_DEVICES_U);
4223
4224         /* For LP0 resume, don't reset lpcpu, since we are running from it */
4225         val = *ctx++;
4226         val &= ~RST_DEVICES_V_SWR_CPULP_RST_DIS;
4227         clk_writel(val, RST_DEVICES_V);
4228
4229         clk_writel(*ctx++, RST_DEVICES_W);
4230         wmb();
4231
4232         clk_writel(*ctx++, CLK_OUT_ENB_L);
4233         clk_writel(*ctx++, CLK_OUT_ENB_H);
4234         clk_writel(*ctx++, CLK_OUT_ENB_U);
4235
4236         /* For LP0 resume, clk to lpcpu is required to be on */
4237         val = *ctx++;
4238         val |= CLK_OUT_ENB_V_CLK_ENB_CPULP_EN;
4239         clk_writel(val, CLK_OUT_ENB_V);
4240
4241         clk_writel(*ctx++, CLK_OUT_ENB_W);
4242         wmb();
4243
4244         clk_writel(*ctx++, MISC_CLK_ENB);
4245         clk_writel(*ctx++, CLK_MASK_ARM);
4246
4247         /* Since EMC clock is not restored update current state, and mark
4248            EMC DFS as out of sync */
4249         tegra3_periph_clk_init(&tegra_clk_emc);
4250         tegra_emc_timing_invalidate();
4251
4252         tegra3_pll_clk_init(&tegra_pll_u); /* Re-init utmi parameters */
4253         tegra3_pll_clk_init(&tegra_pll_p); /* Fire a bug if not restored */
4254 }
4255 #endif