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