]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/platform/tegra/tegra21_clocks.c
clock: tegra21: boosting sclk clock
[sojka/nv-tegra/linux-3.10.git] / drivers / platform / tegra / tegra21_clocks.c
1 /*
2  * arch/arm/mach-tegra/tegra21_clocks.c
3  *
4  * Copyright (C) 2013-2015 NVIDIA Corporation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/err.h>
26 #include <linux/clk.h>
27 #include <linux/syscore_ops.h>
28 #include <linux/platform_device.h>
29 #include <linux/tegra_soctherm.h>
30 #include <soc/tegra/tegra_bpmp.h>
31 #include <dt-bindings/clk/tegra210-clk.h>
32
33 #include <asm/clkdev.h>
34 #include <asm/arch_timer.h>
35
36 #include <mach/edp.h>
37 #include <mach/hardware.h>
38 #include <mach/tegra_emc.h>
39
40 #include <tegra/mc.h>
41
42 #include <linux/platform/tegra/clock.h>
43 #include "tegra_clocks_ops.h"
44 #include <linux/platform/tegra/dvfs.h>
45 #include "pm.h"
46 #include "sleep.h"
47 #include "devices.h"
48 #include <linux/platform/tegra/tegra_cl_dvfs.h>
49
50 #undef USE_PLLE_SS
51 #define USE_PLLE_SS 1
52
53 #define RST_DEVICES_L                   0x004
54 #define RST_DEVICES_H                   0x008
55 #define RST_DEVICES_U                   0x00C
56 #define RST_DEVICES_V                   0x358
57 #define RST_DEVICES_W                   0x35C
58 #define RST_DEVICES_X                   0x28C
59 #define RST_DEVICES_Y                   0x2A4
60 #define RST_DEVICES_SET_L               0x300
61 #define RST_DEVICES_CLR_L               0x304
62 #define RST_DEVICES_SET_V               0x430
63 #define RST_DEVICES_CLR_V               0x434
64 #define RST_DEVICES_SET_X               0x290
65 #define RST_DEVICES_CLR_X               0x294
66 #define RST_DEVICES_SET_Y               0x2A8
67 #define RST_DEVICES_CLR_Y               0x2AC
68 #define RST_DEVICES_NUM                 7
69
70 #define CLK_OUT_ENB_L                   0x010
71 #define CLK_OUT_ENB_H                   0x014
72 #define CLK_OUT_ENB_U                   0x018
73 #define CLK_OUT_ENB_V                   0x360
74 #define CLK_OUT_ENB_W                   0x364
75 #define CLK_OUT_ENB_X                   0x280
76 #define CLK_OUT_ENB_Y                   0x298
77 #define CLK_OUT_ENB_SET_L               0x320
78 #define CLK_OUT_ENB_CLR_L               0x324
79 #define CLK_OUT_ENB_SET_V               0x440
80 #define CLK_OUT_ENB_CLR_V               0x444
81 #define CLK_OUT_ENB_SET_X               0x284
82 #define CLK_OUT_ENB_CLR_X               0x288
83 #define CLK_OUT_ENB_SET_Y               0x29C
84 #define CLK_OUT_ENB_CLR_Y               0x2A0
85 #define CLK_OUT_ENB_NUM                 7
86
87 #define CLK_OUT_ENB_L_RESET_MASK        0xdcd7dff9
88 #define CLK_OUT_ENB_H_RESET_MASK        0x87d1f3e7
89 #define CLK_OUT_ENB_U_RESET_MASK        0xf3fed3fa
90 #define CLK_OUT_ENB_V_RESET_MASK        0xffc18cfb
91 #define CLK_OUT_ENB_W_RESET_MASK        0x793fb7ff
92 #define CLK_OUT_ENB_X_RESET_MASK        0x3fe66fff
93 #define CLK_OUT_ENB_Y_RESET_MASK        0xfc1fc7ff
94
95 #define RST_DEVICES_V_SWR_CPULP_RST_DIS (0x1 << 1) /* Reserved on Tegra11 */
96 #define CLK_OUT_ENB_V_CLK_ENB_CPULP_EN  (0x1 << 1)
97
98 #define PERIPH_CLK_TO_BIT(c)            (1 << (c->u.periph.clk_num % 32))
99 #define PERIPH_CLK_TO_RST_REG(c)        \
100         periph_clk_to_reg((c), RST_DEVICES_L, RST_DEVICES_V, \
101                 RST_DEVICES_X, RST_DEVICES_Y, 4)
102 #define PERIPH_CLK_TO_RST_SET_REG(c)    \
103         periph_clk_to_reg((c), RST_DEVICES_SET_L, RST_DEVICES_SET_V, \
104                 RST_DEVICES_SET_X, RST_DEVICES_SET_Y, 8)
105 #define PERIPH_CLK_TO_RST_CLR_REG(c)    \
106         periph_clk_to_reg((c), RST_DEVICES_CLR_L, RST_DEVICES_CLR_V, \
107                 RST_DEVICES_CLR_X, RST_DEVICES_CLR_Y, 8)
108
109 #define PERIPH_CLK_TO_ENB_REG(c)        \
110         periph_clk_to_reg((c), CLK_OUT_ENB_L, CLK_OUT_ENB_V, \
111                 CLK_OUT_ENB_X, CLK_OUT_ENB_Y, 4)
112 #define PERIPH_CLK_TO_ENB_SET_REG(c)    \
113         periph_clk_to_reg((c), CLK_OUT_ENB_SET_L, CLK_OUT_ENB_SET_V, \
114                 CLK_OUT_ENB_SET_X, CLK_OUT_ENB_SET_Y, 8)
115 #define PERIPH_CLK_TO_ENB_CLR_REG(c)    \
116         periph_clk_to_reg((c), CLK_OUT_ENB_CLR_L, CLK_OUT_ENB_CLR_V, \
117                 CLK_OUT_ENB_CLR_X, CLK_OUT_ENB_CLR_Y, 8)
118
119 #define CLK_MASK_ARM                    0x44
120 #define MISC_CLK_ENB                    0x48
121
122 #define OSC_CTRL                        0x50
123 #define OSC_CTRL_OSC_FREQ_MASK          (0xF<<28)
124 #define OSC_CTRL_OSC_FREQ_12MHZ         (0x8<<28)
125 #define OSC_CTRL_OSC_FREQ_13MHZ         (0x0<<28)
126 #define OSC_CTRL_OSC_FREQ_19_2MHZ       (0x4<<28)
127 #define OSC_CTRL_OSC_FREQ_38_4MHZ       (0x5<<28)
128 #define OSC_CTRL_MASK                   (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
129
130 #define OSC_CTRL_PLL_REF_DIV_MASK       (3<<26)
131 #define OSC_CTRL_PLL_REF_DIV_1          (0<<26)
132 #define OSC_CTRL_PLL_REF_DIV_2          (1<<26)
133 #define OSC_CTRL_PLL_REF_DIV_4          (2<<26)
134
135 #define PERIPH_CLK_SOURCE_I2S1          0x100
136 #define PERIPH_CLK_SOURCE_EMC           0x19c
137 #define PERIPH_CLK_SOURCE_EMC_MC_SAME   (1<<16)
138 #define PERIPH_CLK_SOURCE_EMC_DIV2_EN   (1<<15)
139 #define PERIPH_CLK_SOURCE_QSPI_DIV2_EN  (1<<8)
140 #define PERIPH_CLK_SOURCE_LA            0x1f8
141 #define PERIPH_CLK_SOURCE_NUM1 \
142         ((PERIPH_CLK_SOURCE_LA - PERIPH_CLK_SOURCE_I2S1) / 4)
143
144 #define PERIPH_CLK_SOURCE_MSELECT       0x3b4
145 #define PERIPH_CLK_SOURCE_SE            0x42c
146 #define PERIPH_CLK_SOURCE_NUM2 \
147         ((PERIPH_CLK_SOURCE_SE - PERIPH_CLK_SOURCE_MSELECT) / 4 + 1)
148
149 #define AUDIO_DLY_CLK                   0x49c
150 #define AUDIO_SYNC_CLK_SPDIF            0x4b4
151 #define PERIPH_CLK_SOURCE_NUM3 \
152         ((AUDIO_SYNC_CLK_SPDIF - AUDIO_DLY_CLK) / 4 + 1)
153
154 #define SPARE_REG                       0x55c
155 #define SPARE_REG_CLK_M_DIVISOR_SHIFT   2
156 #define SPARE_REG_CLK_M_DIVISOR_MASK    (3 << SPARE_REG_CLK_M_DIVISOR_SHIFT)
157
158 #define AUDIO_SYNC_CLK_DMIC1            0x560
159 #define AUDIO_SYNC_CLK_DMIC2            0x564
160 #define PERIPH_CLK_SOURCE_NUM4 \
161         ((AUDIO_SYNC_CLK_DMIC2 - AUDIO_SYNC_CLK_DMIC1) / 4 + 1)
162
163 #define PERIPH_CLK_SOURCE_XUSB_HOST     0x600
164 #define PERIPH_CLK_SOURCE_EMC_DLL       0x664
165 #define PERIPH_CLK_SOURCE_VIC           0x678
166 #define PERIPH_CLK_SOURCE_NUM5 \
167         ((PERIPH_CLK_SOURCE_VIC - PERIPH_CLK_SOURCE_XUSB_HOST) / 4)
168
169 #define PERIPH_CLK_SOURCE_SDMMC_LEGACY  0x694
170 #define PERIPH_CLK_SOURCE_NVENC         0x6a0
171 #define PERIPH_CLK_SOURCE_NUM6 \
172         ((PERIPH_CLK_SOURCE_NVENC - PERIPH_CLK_SOURCE_SDMMC_LEGACY) / 4 + 1)
173
174 #define AUDIO_SYNC_CLK_DMIC3            0x6b8
175 #define PERIPH_CLK_SOURCE_DBGAPB        0x718
176 #define PERIPH_CLK_SOURCE_NUM7 \
177         ((PERIPH_CLK_SOURCE_DBGAPB - AUDIO_SYNC_CLK_DMIC3) / 4 + 1)
178
179 #define PERIPH_CLK_SOURCE_NUM           (PERIPH_CLK_SOURCE_NUM1 + \
180                                          PERIPH_CLK_SOURCE_NUM2 + \
181                                          PERIPH_CLK_SOURCE_NUM3 + \
182                                          PERIPH_CLK_SOURCE_NUM4 + \
183                                          PERIPH_CLK_SOURCE_NUM5 + \
184                                          PERIPH_CLK_SOURCE_NUM6 + \
185                                          PERIPH_CLK_SOURCE_NUM7)
186
187 #define CPU_SOFTRST_CTRL                0x380
188 #define CPU_SOFTRST_CTRL1               0x384
189 #define CPU_SOFTRST_CTRL2               0x388
190
191 #define PERIPH_CLK_SOURCE_DIVU71_MASK   0xFF
192 #define PERIPH_CLK_SOURCE_DIVU16_MASK   0xFFFF
193 #define PERIPH_CLK_SOURCE_DIV_SHIFT     0
194 #define PERIPH_CLK_SOURCE_DIVIDLE_SHIFT 8
195 #define PERIPH_CLK_SOURCE_DIVIDLE_VAL   50
196 #define PERIPH_CLK_UART_DIV_ENB         (1<<24)
197 #define PERIPH_CLK_VI_SEL_EX_SHIFT      24
198 #define PERIPH_CLK_VI_SEL_EX_MASK       (0x3<<PERIPH_CLK_VI_SEL_EX_SHIFT)
199 #define PERIPH_CLK_NAND_DIV_EX_ENB      (1<<8)
200 #define PERIPH_CLK_DTV_POLARITY_INV     (1<<25)
201
202 #define AUDIO_SYNC_SOURCE_MASK          0x0F
203 #define AUDIO_SYNC_DISABLE_BIT          0x10
204 #define AUDIO_SYNC_TAP_NIBBLE_SHIFT(c)  ((c->reg_shift - 24) * 4)
205
206 #define PERIPH_CLK_SOR_CLK_SEL_SHIFT    14
207 #define PERIPH_CLK_SOR0_CLK_SEL_MASK    (0x1<<PERIPH_CLK_SOR_CLK_SEL_SHIFT)
208 #define PERIPH_CLK_SOR1_CLK_SEL_MASK    (0x3<<PERIPH_CLK_SOR_CLK_SEL_SHIFT)
209
210 /* Secondary PLL dividers */
211 #define PLL_OUT_RATIO_MASK              (0xFF<<8)
212 #define PLL_OUT_RATIO_SHIFT             8
213 #define PLL_OUT_OVERRIDE                (1<<2)
214 #define PLL_OUT_CLKEN                   (1<<1)
215 #define PLL_OUT_RESET_DISABLE           (1<<0)
216
217 /* PLLC, PLLC2, PLLC3 and PLLA1 */
218 #define PLLCX_USE_DYN_RAMP              0
219 #define PLLCX_BASE_LOCK                 (1 << 26)
220
221 #define PLLCX_MISC0_RESET               (1 << 30)
222 #define PLLCX_MISC0_LOOP_CTRL_SHIFT     0
223 #define PLLCX_MISC0_LOOP_CTRL_MASK      (0x3 << PLLCX_MISC0_LOOP_CTRL_SHIFT)
224
225 #define PLLCX_MISC1_IDDQ                (1 << 27)
226
227 #define PLLCX_MISC0_DEFAULT_VALUE       0x40080000
228 #define PLLCX_MISC0_WRITE_MASK          0x400ffffb
229 #define PLLCX_MISC1_DEFAULT_VALUE       0x08000000
230 #define PLLCX_MISC1_WRITE_MASK          0x08003cff
231 #define PLLCX_MISC2_DEFAULT_VALUE       0x1f720f05
232 #define PLLCX_MISC2_WRITE_MASK          0xffffff17
233 #define PLLCX_MISC3_DEFAULT_VALUE       0x000000c4
234 #define PLLCX_MISC3_WRITE_MASK          0x00ffffff
235
236 /* PLLA */
237 #define PLLA_BASE_LOCK                  (1 << 27)
238 #define PLLA_BASE_IDDQ                  (1 << 25)
239
240 #define PLLA_MISC0_LOCK_ENABLE          (1 << 28)
241 #define PLLA_MISC0_LOCK_OVERRIDE        (1 << 27)
242
243 #define PLLA_MISC2_EN_SDM               (1 << 26)
244 #define PLLA_MISC2_EN_DYNRAMP           (1 << 25)
245
246 #define PLLA_MISC0_DEFAULT_VALUE        0x12000020
247 #define PLLA_MISC0_WRITE_MASK           0x7fffffff
248 #define PLLA_MISC2_DEFAULT_VALUE        0x0
249 #define PLLA_MISC2_WRITE_MASK           0x06ffffff
250
251 /* PLLD */
252 #define PLLD_BASE_LOCK                  (1 << 27)
253 #define PLLD_BASE_DSI_MUX_SHIFT         25
254 #define PLLD_BASE_DSI_MUX_MASK          (0x1 << PLLD_BASE_DSI_MUX_SHIFT)
255 #define PLLD_BASE_CSI_CLKSOURCE         (1 << 23)
256
257 #define PLLD_MISC0_DSI_CLKENABLE        (1 << 21)
258 #define PLLD_MISC0_IDDQ                 (1 << 20)
259 #define PLLD_MISC0_LOCK_ENABLE          (1 << 18)
260 #define PLLD_MISC0_LOCK_OVERRIDE        (1 << 17)
261 #define PLLD_MISC0_EN_SDM               (1 << 16)
262
263 #define PLLD_MISC0_DEFAULT_VALUE        0x00140000
264 #define PLLD_MISC0_WRITE_MASK           0x3ff7ffff
265 #define PLLD_MISC1_DEFAULT_VALUE        0x20
266 #define PLLD_MISC1_WRITE_MASK           0x00ffffff
267
268 /* PLLD2 and PLLDP  and PLLC4 */
269 #define PLLDSS_BASE_LOCK                (1 << 27)
270 #define PLLDSS_BASE_LOCK_OVERRIDE       (1 << 24)
271 #define PLLDSS_BASE_IDDQ                (1 << 18)
272 #define PLLDSS_BASE_REF_SEL_SHIFT       25
273 #define PLLDSS_BASE_REF_SEL_MASK        (0x3 << PLLDSS_BASE_REF_SEL_SHIFT)
274
275 #define PLLDSS_MISC0_LOCK_ENABLE        (1 << 30)
276
277 #define PLLDSS_MISC1_CFG_EN_SDM         (1 << 31)
278 #define PLLDSS_MISC1_CFG_EN_SSC         (1 << 30)
279
280 #define PLLD2_MISC0_DEFAULT_VALUE       0x40000020
281 #define PLLD2_MISC1_CFG_DEFAULT_VALUE   0x10000000
282 #define PLLD2_MISC2_CTRL1_DEFAULT_VALUE 0x0
283 #define PLLD2_MISC3_CTRL2_DEFAULT_VALUE 0x0
284
285 #define PLLDP_MISC0_DEFAULT_VALUE       0x40000020
286 #define PLLDP_MISC1_CFG_DEFAULT_VALUE   0xc0000000
287 #define PLLDP_MISC2_CTRL1_DEFAULT_VALUE 0xf400f0da
288 #define PLLDP_MISC3_CTRL2_DEFAULT_VALUE 0x2004f400
289
290 #define PLLDSS_MISC0_WRITE_MASK         0x47ffffff
291 #define PLLDSS_MISC1_CFG_WRITE_MASK     0xf8000000
292 #define PLLDSS_MISC2_CTRL1_WRITE_MASK   0xffffffff
293 #define PLLDSS_MISC3_CTRL2_WRITE_MASK   0xffffffff
294
295 #define PLLC4_MISC0_DEFAULT_VALUE       0x40000000
296
297 /* PLLRE */
298 #define PLLRE_MISC0_LOCK_ENABLE         (1 << 30)
299 #define PLLRE_MISC0_LOCK_OVERRIDE       (1 << 29)
300 #define PLLRE_MISC0_LOCK                (1 << 27)
301 #define PLLRE_MISC0_IDDQ                (1 << 24)
302
303 #define PLLRE_BASE_DEFAULT_VALUE        0x0
304 #define PLLRE_MISC0_DEFAULT_VALUE       0x41000000
305
306 #define PLLRE_BASE_DEFAULT_MASK         0x1c000000
307 #define PLLRE_MISC0_WRITE_MASK          0x67ffffff
308
309 /* PLLU */
310 #define PLLU_BASE_LOCK                  (1 << 27)
311 #define PLLU_BASE_OVERRIDE              (1 << 24)
312 #define PLLU_BASE_CLKENABLE_USB         (1 << 21)
313
314 #define PLLU_MISC0_IDDQ                 (1 << 31)
315 #define PLLU_MISC0_LOCK_ENABLE          (1 << 29)
316 #define PLLU_MISC1_LOCK_OVERRIDE        (1 << 0)
317
318 #define PLLU_MISC0_DEFAULT_VALUE        0xa0000000
319 #define PLLU_MISC1_DEFAULT_VALUE        0x0
320
321 #define PLLU_MISC0_WRITE_MASK           0xbfffffff
322 #define PLLU_MISC1_WRITE_MASK           0x00000007
323
324 /* PLLP */
325 #define PLLP_BASE_OVERRIDE              (1 << 28)
326 #define PLLP_BASE_LOCK                  (1 << 27)
327
328 #define PLLP_MISC0_LOCK_ENABLE          (1 << 18)
329 #define PLLP_MISC0_LOCK_OVERRIDE        (1 << 17)
330 #define PLLP_MISC0_IDDQ                 (1 << 3)
331
332 #define PLLP_MISC1_HSIO_EN_SHIFT        29
333 #define PLLP_MISC1_HSIO_EN              (1 << PLLP_MISC1_HSIO_EN_SHIFT)
334 #define PLLP_MISC1_XUSB_EN_SHIFT        28
335 #define PLLP_MISC1_XUSB_EN              (1 << PLLP_MISC1_XUSB_EN_SHIFT)
336
337 #define PLLP_MISC0_DEFAULT_VALUE        0x00040008
338 #define PLLP_MISC1_DEFAULT_VALUE        0x0
339
340 #define PLLP_MISC0_WRITE_MASK           0xdc6000f
341 #define PLLP_MISC1_WRITE_MASK           0x70ffffff
342
343 /* PLLX */
344 #define PLLX_USE_DYN_RAMP               1
345 #define PLLX_BASE_LOCK                  (1 << 27)
346
347 #define PLLX_MISC0_FO_G_DISABLE         (0x1 << 28)
348 #define PLLX_MISC0_LOCK_ENABLE          (0x1 << 18)
349
350 #define PLLX_MISC2_DYNRAMP_STEPB_SHIFT  24
351 #define PLLX_MISC2_DYNRAMP_STEPB_MASK   (0xFF << PLLX_MISC2_DYNRAMP_STEPB_SHIFT)
352 #define PLLX_MISC2_DYNRAMP_STEPA_SHIFT  16
353 #define PLLX_MISC2_DYNRAMP_STEPA_MASK   (0xFF << PLLX_MISC2_DYNRAMP_STEPA_SHIFT)
354 #define PLLX_MISC2_NDIV_NEW_SHIFT       8
355 #define PLLX_MISC2_NDIV_NEW_MASK        (0xFF << PLLX_MISC2_NDIV_NEW_SHIFT)
356 #define PLLX_MISC2_LOCK_OVERRIDE        (0x1 << 4)
357 #define PLLX_MISC2_DYNRAMP_DONE         (0x1 << 2)
358 #define PLLX_MISC2_EN_DYNRAMP           (0x1 << 0)
359
360 #define PLLX_MISC3_IDDQ                 (0x1 << 3)
361
362 #define PLLX_MISC0_DEFAULT_VALUE        PLLX_MISC0_LOCK_ENABLE
363 #define PLLX_MISC0_WRITE_MASK           0x10c40000
364 #define PLLX_MISC1_DEFAULT_VALUE        0x20
365 #define PLLX_MISC1_WRITE_MASK           0x00ffffff
366 #define PLLX_MISC2_DEFAULT_VALUE        0x0
367 #define PLLX_MISC2_WRITE_MASK           0xffffff11
368 #define PLLX_MISC3_DEFAULT_VALUE        PLLX_MISC3_IDDQ
369 #define PLLX_MISC3_WRITE_MASK           0x01ff0f0f
370 #define PLLX_MISC4_DEFAULT_VALUE        0x0
371 #define PLLX_MISC4_WRITE_MASK           0x8000ffff
372 #define PLLX_MISC5_DEFAULT_VALUE        0x0
373 #define PLLX_MISC5_WRITE_MASK           0x0000ffff
374
375 #define PLLX_HW_CTRL_CFG                0x548
376 #define PLLX_HW_CTRL_CFG_SWCTRL         (0x1 << 0)
377
378 /* PLLM */
379 #define PLLM_BASE_LOCK                  (1 << 27)
380
381 #define PLLM_MISC0_SYNCMUX_CTRL_SHIFT   10
382 #define PLLM_MISC0_SYNCMUX_CTRL_MASK    (0xF << PLLM_MISC0_SYNCMUX_CTRL_SHIFT)
383 #define PLLM_MISC0_IDDQ                 (1 << 5)
384 #define PLLM_MISC0_LOCK_ENABLE          (1 << 4)
385 #define PLLM_MISC0_LOCK_OVERRIDE        (1 << 3)
386
387 #define PMC_PLLP_WB0_OVERRIDE                   0xf8
388 #define PMC_PLLP_WB0_OVERRIDE_PLLM_IDDQ         (1 << 14)
389 #define PMC_PLLP_WB0_OVERRIDE_PLLM_SEL_VCO      (1 << 13)
390 #define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE       (1 << 12)
391 #define PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE     (1 << 11)
392
393 /* M, N layout for PLLM override and base registers are the same */
394 #define PMC_PLLM_WB0_OVERRIDE                   0x1dc
395
396 /* PDIV override and base layouts are different */
397 #define PMC_PLLM_WB0_OVERRIDE_2                 0x2b0
398 #define PMC_PLLM_WB0_OVERRIDE_2_DIVP_SHIFT      27
399 #define PMC_PLLM_WB0_OVERRIDE_2_DIVP_MASK       (0x1F << 27)
400
401 /* PLLMB */
402 #define PLLMB_BASE_LOCK                 (1 << 27)
403
404 #define PLLMB_MISC0_LOCK_OVERRIDE       (1 << 18)
405 #define PLLMB_MISC0_IDDQ                (1 << 17)
406 #define PLLMB_MISC0_LOCK_ENABLE         (1 << 16)
407
408 #define PLLMB_MISC0_DEFAULT_VALUE       0x00030000
409 #define PLLMB_MISC0_WRITE_MASK          0x0007ffff
410
411 #define SUPER_CLK_MUX                   0x00
412 #define SUPER_STATE_SHIFT               28
413 #define SUPER_STATE_MASK                (0xF << SUPER_STATE_SHIFT)
414 #define SUPER_STATE_STANDBY             (0x0 << SUPER_STATE_SHIFT)
415 #define SUPER_STATE_IDLE                (0x1 << SUPER_STATE_SHIFT)
416 #define SUPER_STATE_RUN                 (0x2 << SUPER_STATE_SHIFT)
417 #define SUPER_STATE_IRQ                 (0x3 << SUPER_STATE_SHIFT)
418 #define SUPER_STATE_FIQ                 (0x4 << SUPER_STATE_SHIFT)
419 #define SUPER_SOURCE_MASK               0xF
420 #define SUPER_FIQ_SOURCE_SHIFT          12
421 #define SUPER_IRQ_SOURCE_SHIFT          8
422 #define SUPER_RUN_SOURCE_SHIFT          4
423 #define SUPER_IDLE_SOURCE_SHIFT         0
424
425 #define SUPER_CLK_DIVIDER               0x04
426 #define SUPER_CLOCK_DIV_U71_SHIFT       16
427 #define SUPER_CLOCK_DIV_U71_MASK        (0xff << SUPER_CLOCK_DIV_U71_SHIFT)
428
429 #define SUPER_SKIPPER_ENABLE            (1 << 31)
430 #define SUPER_SKIPPER_TERM_SIZE         8
431 #define SUPER_SKIPPER_MUL_SHIFT         8
432 #define SUPER_SKIPPER_MUL_MASK          (((1 << SUPER_SKIPPER_TERM_SIZE) - 1) \
433                                         << SUPER_SKIPPER_MUL_SHIFT)
434 #define SUPER_SKIPPER_DIV_SHIFT         0
435 #define SUPER_SKIPPER_DIV_MASK          (((1 << SUPER_SKIPPER_TERM_SIZE) - 1) \
436                                         << SUPER_SKIPPER_DIV_SHIFT)
437
438 #define BUS_CLK_DISABLE                 (1<<3)
439 #define BUS_CLK_DIV_MASK                0x3
440
441 #define PMC_CTRL                        0x0
442  #define PMC_CTRL_BLINK_ENB             (1 << 7)
443
444 #define PMC_DPD_PADS_ORIDE              0x1c
445  #define PMC_DPD_PADS_ORIDE_BLINK_ENB   (1 << 20)
446
447 #define PMC_BLINK_TIMER_DATA_ON_SHIFT   0
448 #define PMC_BLINK_TIMER_DATA_ON_MASK    0x7fff
449 #define PMC_BLINK_TIMER_ENB             (1 << 15)
450 #define PMC_BLINK_TIMER_DATA_OFF_SHIFT  16
451 #define PMC_BLINK_TIMER_DATA_OFF_MASK   0xffff
452
453 #define UTMIP_PLL_CFG2                                  0x488
454 #define UTMIP_PLL_CFG2_STABLE_COUNT(x)                  (((x) & 0xfff) << 6)
455 #define UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(x)              (((x) & 0x3f) << 18)
456 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN        (1 << 0)
457 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERUP          (1 << 1)
458 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN        (1 << 2)
459 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERUP          (1 << 3)
460 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN        (1 << 4)
461 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERUP          (1 << 5)
462 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERDOWN        (1 << 24)
463 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERUP          (1 << 25)
464
465 #define UTMIP_PLL_CFG1                                  0x484
466 #define UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(x)              (((x) & 0x1f) << 27)
467 #define UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(x)               (((x) & 0xfff) << 0)
468 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP (1 << 15)
469 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN       (1 << 14)
470 #define UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN       (1 << 12)
471 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERUP               (1 << 17)
472 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN             (1 << 16)
473
474 /* PLLE */
475 #define PLLE_BASE_ENABLE                (1 << 31)
476 #define PLLE_BASE_LOCK_OVERRIDE         (1 << 30)
477 #define PLLE_BASE_DIVCML_SHIFT          24
478 #define PLLE_BASE_DIVCML_MASK           (0x1F<<PLLE_BASE_DIVCML_SHIFT)
479 #define PLLE_BASE_DIVN_SHIFT            8
480 #define PLLE_BASE_DIVN_MASK             (0xFF<<PLLE_BASE_DIVN_SHIFT)
481 #define PLLE_BASE_DIVM_SHIFT            0
482 #define PLLE_BASE_DIVM_MASK             (0xFF<<PLLE_BASE_DIVM_SHIFT)
483
484 #define PLLE_MISC_IDDQ_SW_CTRL          (1<<14)
485 #define PLLE_MISC_IDDQ_SW_VALUE         (1<<13)
486 #define PLLE_MISC_LOCK                  (1<<11)
487 #define PLLE_MISC_LOCK_ENABLE           (1<<9)
488 #define PLLE_MISC_PLLE_PTS              (1<<8)
489 #define PLLE_MISC_VREG_BG_CTRL_SHIFT    4
490 #define PLLE_MISC_VREG_BG_CTRL_MASK     (0x3<<PLLE_MISC_VREG_BG_CTRL_SHIFT)
491 #define PLLE_MISC_VREG_CTRL_SHIFT       2
492 #define PLLE_MISC_VREG_CTRL_MASK        (0x3<<PLLE_MISC_VREG_CTRL_SHIFT)
493
494 #define PLLE_SS_CTRL                    0x68
495 #define PLLE_SS_INCINTRV_SHIFT          24
496 #define PLLE_SS_INCINTRV_MASK           (0x3f<<PLLE_SS_INCINTRV_SHIFT)
497 #define PLLE_SS_INC_SHIFT               16
498 #define PLLE_SS_INC_MASK                (0xff<<PLLE_SS_INC_SHIFT)
499 #define PLLE_SS_CNTL_INVERT             (0x1 << 15)
500 #define PLLE_SS_CNTL_CENTER             (0x1 << 14)
501 #define PLLE_SS_CNTL_SSC_BYP            (0x1 << 12)
502 #define PLLE_SS_CNTL_INTERP_RESET       (0x1 << 11)
503 #define PLLE_SS_CNTL_BYPASS_SS          (0x1 << 10)
504 #define PLLE_SS_MAX_SHIFT               0
505 #define PLLE_SS_MAX_MASK                (0x1ff<<PLLE_SS_MAX_SHIFT)
506 #define PLLE_SS_COEFFICIENTS_MASK       \
507         (PLLE_SS_INCINTRV_MASK | PLLE_SS_INC_MASK | PLLE_SS_MAX_MASK)
508 #define PLLE_SS_COEFFICIENTS_VAL        \
509         ((0x23<<PLLE_SS_INCINTRV_SHIFT) | (0x1<<PLLE_SS_INC_SHIFT) | \
510          (0x21<<PLLE_SS_MAX_SHIFT))
511 #define PLLE_SS_DISABLE                 (PLLE_SS_CNTL_SSC_BYP |\
512         PLLE_SS_CNTL_INTERP_RESET | PLLE_SS_CNTL_BYPASS_SS)
513
514 #define PLLE_AUX                        0x48c
515 #define PLLE_AUX_PLLRE_SEL              (1<<28)
516 #define PLLE_AUX_SEQ_STATE_SHIFT        26
517 #define PLLE_AUX_SEQ_STATE_MASK         (0x3<<PLLE_AUX_SEQ_STATE_SHIFT)
518 #define PLLE_AUX_SS_SEQ_INCLUDE         (1<<31)
519 #define PLLE_AUX_SEQ_START_STATE        (1<<25)
520 #define PLLE_AUX_SEQ_ENABLE             (1<<24)
521 #define PLLE_AUX_SS_SWCTL               (1<<6)
522 #define PLLE_AUX_ENABLE_SWCTL           (1<<4)
523 #define PLLE_AUX_USE_LOCKDET            (1<<3)
524 #define PLLE_AUX_PLLP_SEL               (1<<2)
525
526 #define PLLE_AUX_CML_SATA_ENABLE        (1<<1)
527 #define PLLE_AUX_CML_PCIE_ENABLE        (1<<0)
528
529 #define SOURCE_SATA                     0x424
530
531 /* USB PLLs PD HW controls */
532 #define XUSBIO_PLL_CFG0                         0x51c
533 #define XUSBIO_PLL_CFG0_SEQ_START_STATE         (1<<25)
534 #define XUSBIO_PLL_CFG0_SEQ_ENABLE              (1<<24)
535 #define XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET      (1<<6)
536 #define XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL        (1<<2)
537 #define XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL      (1<<0)
538
539 #define UTMIPLL_HW_PWRDN_CFG0                   0x52c
540 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_START_STATE   (1<<25)
541 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE        (1<<24)
542 #define UTMIPLL_HW_PWRDN_CFG0_USE_LOCKDET       (1<<6)
543 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_RESET_INPUT_VALUE     (1<<5)
544 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_IN_SWCTL      (1<<4)
545 #define UTMIPLL_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL  (1<<2)
546 #define UTMIPLL_HW_PWRDN_CFG0_IDDQ_OVERRIDE     (1<<1)
547 #define UTMIPLL_HW_PWRDN_CFG0_IDDQ_SWCTL        (1<<0)
548
549 #define PLLU_HW_PWRDN_CFG0                      0x530
550 #define PLLU_HW_PWRDN_CFG0_IDDQ_PD_INCLUDE      (1<<28)
551 #define PLLU_HW_PWRDN_CFG0_SEQ_START_STATE      (1<<25)
552 #define PLLU_HW_PWRDN_CFG0_SEQ_ENABLE           (1<<24)
553 #define PLLU_HW_PWRDN_CFG0_USE_SWITCH_DETECT    (1<<7)
554 #define PLLU_HW_PWRDN_CFG0_USE_LOCKDET          (1<<6)
555 #define PLLU_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL     (1<<2)
556 #define PLLU_HW_PWRDN_CFG0_CLK_SWITCH_SWCTL     (1<<0)
557
558 #define XUSB_PLL_CFG0                           0x534
559 #define XUSB_PLL_CFG0_UTMIPLL_LOCK_DLY          (0x3ff<<0)
560 #define XUSB_PLL_CFG0_PLLU_LOCK_DLY_SHIFT       14
561 #define XUSB_PLL_CFG0_PLLU_LOCK_DLY_MASK        \
562         (0x3ff<<XUSB_PLL_CFG0_PLLU_LOCK_DLY_SHIFT)
563
564 /* DFLL */
565 #define DFLL_BASE                               0x2f4
566 #define DFLL_BASE_RESET                         (1<<0)
567
568 /* ADSP */
569 #define ADSP_NEON               (1 << 26)
570 #define ADSP_SCU                (1 << 25)
571 #define ADSP_WDT                (1 << 24)
572 #define ADSP_DBG                (1 << 23)
573 #define ADSP_PERIPH             (1 << 22)
574 #define ADSP_INTF               (1 << 21)
575 #define ADSP_CORE               (1 << 7)
576
577 #define ROUND_DIVIDER_UP        0
578 #define ROUND_DIVIDER_DOWN      1
579 #define DIVIDER_1_5_ALLOWED     0
580
581 /* PLLP default fixed rate in h/w controlled mode */
582 #define PLLP_DEFAULT_FIXED_RATE         408000000
583
584 /* Use PLL_RE as PLLE input (default - OSC via pll reference divider) */
585 #define USE_PLLE_INPUT_PLLRE    0
586
587 static void pllc4_set_fixed_rates(unsigned long cf);
588 static void tegra21_dfll_cpu_late_init(struct clk *c);
589 static void tegra21_pllp_init_dependencies(unsigned long pllp_rate);
590
591 static unsigned long tegra21_clk_shared_bus_update(struct clk *bus,
592         struct clk **bus_top, struct clk **bus_slow, unsigned long *rate_cap);
593 static unsigned long tegra21_clk_cap_shared_bus(struct clk *bus,
594         unsigned long rate, unsigned long ceiling);
595
596 static struct clk *pll_u;
597
598 static bool detach_shared_bus;
599 module_param(detach_shared_bus, bool, 0644);
600
601 /* Defines default range for dynamic frequency lock loop (DFLL)
602    to be used as CPU clock source:
603    "0" - DFLL is not used,
604    "1" - DFLL is used as a source for all CPU rates
605    "2" - DFLL is used only for high rates above crossover with PLL dvfs curve
606 */
607 static int use_dfll;
608
609 /**
610 * Structure defining the fields for USB UTMI clocks Parameters.
611 */
612 struct utmi_clk_param
613 {
614         /* Oscillator Frequency in KHz */
615         u32 osc_frequency;
616         /* UTMIP PLL Enable Delay Count  */
617         u8 enable_delay_count;
618         /* UTMIP PLL Stable count */
619         u8 stable_count;
620         /*  UTMIP PLL Active delay count */
621         u8 active_delay_count;
622         /* UTMIP PLL Xtal frequency count */
623         u8 xtal_freq_count;
624 };
625
626 static const struct utmi_clk_param utmi_parameters[] =
627 {
628 /*      OSC_FREQUENCY,  ENABLE_DLY,     STABLE_CNT,     ACTIVE_DLY,     XTAL_FREQ_CNT */
629         {13000000,      0x02,           0x33,           0x05,           0x7F},
630         {12000000,      0x02,           0x2F,           0x04,           0x76},
631
632         {19200000,      0x03,           0x4B,           0x06,           0xBB},
633         /* HACK!!! FIXME!!! following entry for 38.4MHz is a stub */
634         {38400000,      0x0,           0x0,           0x06,           0x80}
635 };
636
637 static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
638 static void __iomem *misc_gp_base = IO_ADDRESS(TEGRA_APB_MISC_BASE);
639
640 #define MISC_GP_HIDREV                          0x804
641 #define MISC_GP_TRANSACTOR_SCRATCH_0            0x864
642 #define MISC_GP_TRANSACTOR_SCRATCH_LA_ENABLE    (0x1 << 1)
643 #define MISC_GP_TRANSACTOR_SCRATCH_DDS_ENABLE   (0x1 << 2)
644 #define MISC_GP_TRANSACTOR_SCRATCH_DP2_ENABLE   (0x1 << 3)
645
646 #define pmc_writel(value, reg) \
647         __raw_writel(value, reg_pmc_base + (reg))
648 #define pmc_readl(reg) \
649         readl(reg_pmc_base + (reg))
650 #define chipid_readl() \
651         __raw_readl(misc_gp_base + MISC_GP_HIDREV)
652
653 /*
654  * Some peripheral clocks share an enable bit, so refcount the enable bits
655  * in registers CLK_ENABLE_L, ... CLK_ENABLE_W, and protect refcount updates
656  * with lock
657  */
658 static DEFINE_SPINLOCK(periph_refcount_lock);
659 static int tegra_periph_clk_enable_refcount[CLK_OUT_ENB_NUM * 32];
660
661 static inline int clk_set_div(struct clk *c, u32 n)
662 {
663         return clk_set_rate(c, (clk_get_rate(c->parent) + n-1) / n);
664 }
665
666 static inline u32 periph_clk_to_reg(
667         struct clk *c, u32 reg_L, u32 reg_V, u32 reg_X, u32 reg_Y, int offs)
668 {
669         u32 reg = c->u.periph.clk_num / 32;
670         BUG_ON(reg >= RST_DEVICES_NUM);
671         if (reg < 3)
672                 reg = reg_L + (reg * offs);
673         else if (reg < 5)
674                 reg = reg_V + ((reg - 3) * offs);
675         else if (reg == 5)
676                 reg = reg_X;
677         else
678                 reg = reg_Y;
679         return reg;
680 }
681
682 static int clk_div_x1_get_divider(unsigned long parent_rate, unsigned long rate,
683                         u32 max_x,
684                                  u32 flags, u32 round_mode)
685 {
686         s64 divider_ux1 = parent_rate;
687         if (!rate)
688                 return -EINVAL;
689
690         if (!(flags & DIV_U71_INT))
691                 divider_ux1 *= 2;
692         if (round_mode == ROUND_DIVIDER_UP)
693                 divider_ux1 += rate - 1;
694         do_div(divider_ux1, rate);
695         if (flags & DIV_U71_INT)
696                 divider_ux1 *= 2;
697
698         if (divider_ux1 - 2 < 0)
699                 return 0;
700
701         if (divider_ux1 - 2 > max_x)
702                 return -EINVAL;
703
704 #if !DIVIDER_1_5_ALLOWED
705         if (divider_ux1 == 3)
706                 divider_ux1 = (round_mode == ROUND_DIVIDER_UP) ? 4 : 2;
707 #endif
708         return divider_ux1 - 2;
709 }
710
711 static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate,
712                                  u32 flags, u32 round_mode)
713 {
714         return clk_div_x1_get_divider(parent_rate, rate, 0xFF,
715                         flags, round_mode);
716 }
717
718 static int clk_div151_get_divider(unsigned long parent_rate, unsigned long rate,
719                                  u32 flags, u32 round_mode)
720 {
721         return clk_div_x1_get_divider(parent_rate, rate, 0xFFFF,
722                         flags, round_mode);
723 }
724
725 static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
726 {
727         s64 divider_u16;
728
729         divider_u16 = parent_rate;
730         if (!rate)
731                 return -EINVAL;
732         divider_u16 += rate - 1;
733         do_div(divider_u16, rate);
734
735         if (divider_u16 - 1 < 0)
736                 return 0;
737
738         if (divider_u16 - 1 > 0xFFFF)
739                 return -EINVAL;
740
741         return divider_u16 - 1;
742 }
743
744 static long fixed_src_bus_round_updown(struct clk *c, struct clk *src,
745                         u32 flags, unsigned long rate, bool up, u32 *div)
746 {
747         int divider;
748         unsigned long source_rate, round_rate;
749
750         source_rate = clk_get_rate(src);
751
752         divider = clk_div71_get_divider(source_rate, rate + (up ? -1 : 1),
753                 flags, up ? ROUND_DIVIDER_DOWN : ROUND_DIVIDER_UP);
754         if (divider < 0) {
755                 divider = flags & DIV_U71_INT ? 0xFE : 0xFF;
756                 round_rate = source_rate * 2 / (divider + 2);
757                 goto _out;
758         }
759
760         round_rate = source_rate * 2 / (divider + 2);
761
762         if (round_rate > c->max_rate) {
763                 divider += flags & DIV_U71_INT ? 2 : 1;
764 #if !DIVIDER_1_5_ALLOWED
765                 divider = max(2, divider);
766 #endif
767                 round_rate = source_rate * 2 / (divider + 2);
768         }
769 _out:
770         if (div)
771                 *div = divider + 2;
772         return round_rate;
773 }
774
775 static inline bool bus_user_is_slower(struct clk *a, struct clk *b)
776 {
777         return a->u.shared_bus_user.client->max_rate <
778                 b->u.shared_bus_user.client->max_rate;
779 }
780
781 static inline bool bus_user_request_is_lower(struct clk *a, struct clk *b)
782 {
783         return a->u.shared_bus_user.rate <
784                 b->u.shared_bus_user.rate;
785 }
786
787 static void super_clk_set_u71_div_no_skip(struct clk *c)
788 {
789         clk_writel_delay(c->u.cclk.div71 << SUPER_CLOCK_DIV_U71_SHIFT,
790                          c->reg + SUPER_CLK_DIVIDER);
791         c->mul = 2;
792         c->div = c->u.cclk.div71 + 2;
793 }
794
795 static void super_clk_clr_u71_div_no_skip(struct clk *c)
796 {
797         clk_writel_delay(0, c->reg + SUPER_CLK_DIVIDER);
798         c->mul = 2;
799         c->div = 2;
800 }
801
802 /* clk_m functions */
803 static unsigned long tegra21_osc_autodetect_rate(struct clk *c)
804 {
805         u32 osc_ctrl = clk_readl(OSC_CTRL);
806         u32 pll_ref_div = clk_readl(OSC_CTRL) & OSC_CTRL_PLL_REF_DIV_MASK;
807
808         switch (osc_ctrl & OSC_CTRL_OSC_FREQ_MASK) {
809         case OSC_CTRL_OSC_FREQ_12MHZ:
810                 c->rate = 12000000;
811                 break;
812         case OSC_CTRL_OSC_FREQ_13MHZ:
813                 /* 13MHz for FPGA only, BUG_ON otherwise */
814                 BUG_ON(!tegra_platform_is_fpga());
815                 c->rate = 13000000;
816                 break;
817         case OSC_CTRL_OSC_FREQ_38_4MHZ:
818                 c->rate = 38400000;
819                 break;
820         default:
821                 pr_err("supported OSC freq: %08x\n", osc_ctrl);
822                 BUG();
823         }
824
825         BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
826
827         return c->rate;
828 }
829
830 static void tegra21_osc_init(struct clk *c)
831 {
832         pr_debug("%s on clock %s\n", __func__, c->name);
833         tegra21_osc_autodetect_rate(c);
834         c->state = ON;
835 }
836
837 static int tegra21_osc_enable(struct clk *c)
838 {
839         pr_debug("%s on clock %s\n", __func__, c->name);
840         return 0;
841 }
842
843 static void tegra21_osc_disable(struct clk *c)
844 {
845         pr_debug("%s on clock %s\n", __func__, c->name);
846         WARN(1, "Attempting to disable main SoC clock\n");
847 }
848
849 static struct clk_ops tegra_osc_ops = {
850         .init           = tegra21_osc_init,
851         .enable         = tegra21_osc_enable,
852         .disable        = tegra21_osc_disable,
853 };
854
855 static void tegra21_clk_m_init(struct clk *c)
856 {
857         u32 rate;
858         u32 spare = clk_readl(SPARE_REG);
859
860         pr_debug("%s on clock %s\n", __func__, c->name);
861
862         rate = clk_get_rate(c->parent); /* the rate of osc clock */
863
864         if (tegra_platform_is_fpga()) {
865                 if (rate == 38400000) {
866                         /* Set divider to (2 + 1) to still maintain
867                         clk_m to 13MHz instead of reporting clk_m as
868                         19.2 MHz when it is actually set to 13MHz */
869                         spare &= ~SPARE_REG_CLK_M_DIVISOR_MASK;
870                         spare |= (2 << SPARE_REG_CLK_M_DIVISOR_SHIFT);
871                         clk_writel(spare, SPARE_REG);
872                 }
873         }
874
875         c->div = ((spare & SPARE_REG_CLK_M_DIVISOR_MASK)
876                 >> SPARE_REG_CLK_M_DIVISOR_SHIFT) + 1;
877         c->mul = 1;
878         c->state = ON;
879 }
880
881 static int tegra21_clk_m_enable(struct clk *c)
882 {
883         pr_debug("%s on clock %s\n", __func__, c->name);
884         return 0;
885 }
886
887 static void tegra21_clk_m_disable(struct clk *c)
888 {
889         pr_debug("%s on clock %s\n", __func__, c->name);
890         WARN(1, "Attempting to disable main SoC clock\n");
891 }
892
893 static struct clk_ops tegra_clk_m_ops = {
894         .init           = tegra21_clk_m_init,
895         .enable         = tegra21_clk_m_enable,
896         .disable        = tegra21_clk_m_disable,
897 };
898
899 static struct clk_ops tegra_clk_m_div_ops = {
900         .enable         = tegra21_clk_m_enable,
901 };
902
903 /* PLL reference divider functions */
904 static void tegra21_pll_ref_init(struct clk *c)
905 {
906         u32 pll_ref_div = clk_readl(OSC_CTRL) & OSC_CTRL_PLL_REF_DIV_MASK;
907         pr_debug("%s on clock %s\n", __func__, c->name);
908
909         switch (pll_ref_div) {
910         case OSC_CTRL_PLL_REF_DIV_1:
911                 c->div = 1;
912                 break;
913         case OSC_CTRL_PLL_REF_DIV_2:
914                 c->div = 2;
915                 break;
916         case OSC_CTRL_PLL_REF_DIV_4:
917                 c->div = 4;
918                 break;
919         default:
920                 pr_err("%s: Invalid pll ref divider %d", __func__, pll_ref_div);
921                 BUG();
922         }
923         c->mul = 1;
924         c->state = ON;
925 }
926
927 static struct clk_ops tegra_pll_ref_ops = {
928         .init           = tegra21_pll_ref_init,
929         .enable         = tegra21_clk_m_enable,
930         .disable        = tegra21_clk_m_disable,
931 };
932
933 /* super clock functions */
934 /* "super clocks" on tegra21x have two-stage muxes, fractional 7.1 divider and
935  * clock skipping super divider.  We will ignore the clock skipping divider,
936  * since we can't lower the voltage when using the clock skip, but we can if
937  * we lower the PLL frequency. Note that skipping divider can and will be used
938  * by thermal control h/w for automatic throttling. There is also a 7.1 divider
939  * that most CPU super-clock inputs can be routed through. We will not use it
940  * as well (keep default 1:1 state), to avoid high jitter on PLLX and DFLL path
941  * and possible concurrency access issues with thermal h/w (7.1 divider setting
942  * share register with clock skipping divider)
943  */
944 static void tegra21_super_clk_init(struct clk *c)
945 {
946         u32 val;
947         int source;
948         int shift;
949         const struct clk_mux_sel *sel;
950         val = clk_readl(c->reg + SUPER_CLK_MUX);
951         c->state = ON;
952         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
953                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
954         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
955                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
956         source = (val >> shift) & SUPER_SOURCE_MASK;
957
958         for (sel = c->inputs; sel->input != NULL; sel++) {
959                 if (sel->value == source)
960                         break;
961         }
962         BUG_ON(sel->input == NULL);
963         c->parent = sel->input;
964
965         if (c->flags & DIV_U71) {
966                 c->mul = 2;
967                 c->div = 2;
968
969                 /*
970                  * Make sure 7.1 divider is 1:1, clear h/w skipper control -
971                  * it will be enabled by soctherm later
972                  */
973                 val = clk_readl(c->reg + SUPER_CLK_DIVIDER);
974                 BUG_ON(val & SUPER_CLOCK_DIV_U71_MASK);
975                 val = 0;
976                 clk_writel(val, c->reg + SUPER_CLK_DIVIDER);
977         }
978         else
979                 clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
980 }
981
982 static int tegra21_super_clk_enable(struct clk *c)
983 {
984         return 0;
985 }
986
987 static void tegra21_super_clk_disable(struct clk *c)
988 {
989         /* since tegra 3 has 2 CPU super clocks - low power lp-mode clock and
990            geared up g-mode super clock - mode switch may request to disable
991            either of them; accept request with no affect on h/w */
992 }
993
994 static int tegra21_super_clk_set_parent(struct clk *c, struct clk *p)
995 {
996         u32 val;
997         const struct clk_mux_sel *sel;
998         int shift;
999
1000         val = clk_readl(c->reg + SUPER_CLK_MUX);
1001         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
1002                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
1003         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
1004                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
1005         for (sel = c->inputs; sel->input != NULL; sel++) {
1006                 if (sel->input == p) {
1007                         val &= ~(SUPER_SOURCE_MASK << shift);
1008                         val |= (sel->value & SUPER_SOURCE_MASK) << shift;
1009
1010                         if ((c->flags & DIV_U71) && !c->u.cclk.div71) {
1011                                 /* Make sure 7.1 divider is 1:1 */
1012                                 u32 div = clk_readl(c->reg + SUPER_CLK_DIVIDER);
1013                                 BUG_ON(div & SUPER_CLOCK_DIV_U71_MASK);
1014                         }
1015
1016                         if (c->refcnt)
1017                                 tegra_clk_prepare_enable(p);
1018
1019                         clk_writel_delay(val, c->reg);
1020
1021                         if (c->refcnt && c->parent)
1022                                 tegra_clk_disable_unprepare(c->parent);
1023
1024                         clk_reparent(c, p);
1025                         return 0;
1026                 }
1027         }
1028         return -EINVAL;
1029 }
1030
1031 /*
1032  * Do not use super clocks "skippers", since dividing using a clock skipper
1033  * does not allow the voltage to be scaled down. Instead adjust the rate of
1034  * the parent clock. This requires that the parent of a super clock have no
1035  * other children, otherwise the rate will change underneath the other
1036  * children.
1037  */
1038 static int tegra21_super_clk_set_rate(struct clk *c, unsigned long rate)
1039 {
1040         /*
1041          * In tegra21_cpu_clk_set_plls(), tegra21_sbus_cmplx_set_rate(), and
1042          * tegra21_adsp_bus_clk_set_rate() this interface is skipped by directly
1043          * setting rate of source plls.
1044          */
1045         return clk_set_rate(c->parent, rate);
1046 }
1047
1048 static struct clk_ops tegra_super_ops = {
1049         .init                   = tegra21_super_clk_init,
1050         .enable                 = tegra21_super_clk_enable,
1051         .disable                = tegra21_super_clk_disable,
1052         .set_parent             = tegra21_super_clk_set_parent,
1053         .set_rate               = tegra21_super_clk_set_rate,
1054 };
1055
1056 /* virtual cpu clock functions */
1057 /* some clocks can not be stopped (cpu, memory bus) while the SoC is running.
1058    To change the frequency of these clocks, the parent pll may need to be
1059    reprogrammed, so the clock must be moved off the pll, the pll reprogrammed,
1060    and then the clock moved back to the pll.  To hide this sequence, a virtual
1061    clock handles it.
1062  */
1063 static void tegra21_cpu_clk_init(struct clk *c)
1064 {
1065         c->state = (!is_lp_cluster() == (c->u.cpu.mode == MODE_G)) ? ON : OFF;
1066 }
1067
1068 static int tegra21_cpu_clk_enable(struct clk *c)
1069 {
1070         return 0;
1071 }
1072
1073 static void tegra21_cpu_clk_disable(struct clk *c)
1074 {
1075         /* since tegra 3 has 2 virtual CPU clocks - low power lp-mode clock
1076            and geared up g-mode clock - mode switch may request to disable
1077            either of them; accept request with no affect on h/w */
1078 }
1079
1080 static int tegra21_cpu_clk_set_plls(struct clk *c, unsigned long rate,
1081                                     unsigned long old_rate)
1082 {
1083         int ret = 0;
1084         bool dramp = false;
1085         bool on_main = false;
1086         unsigned long backup_rate, main_rate;
1087         struct clk *main_pll = c->u.cpu.main;
1088
1089         /*
1090          * Take an extra reference to the main pll so it doesn't turn off when
1091          * we move the cpu off of it. If possible, use main pll dynamic ramp
1092          * to reach target rate in one shot. Otherwise, use dynamic ramp to
1093          * lower current rate to pll VCO minimum level before switching to
1094          * backup source.
1095          */
1096         if (c->parent->parent == main_pll) {
1097                 tegra_clk_prepare_enable(main_pll);
1098                 on_main = true;
1099                 main_rate = rate;
1100
1101                 dramp = (rate > c->u.cpu.backup_rate) &&
1102                         tegra_pll_can_ramp_to_rate(main_pll, rate);
1103
1104                 if (dramp || tegra_pll_can_ramp_to_min(main_pll, &main_rate)) {
1105                         ret = clk_set_rate(main_pll, main_rate);
1106                         if (ret) {
1107                                 pr_err("Failed to set cpu rate %lu on source"
1108                                        " %s\n", main_rate, main_pll->name);
1109                                 goto out;
1110                         }
1111
1112                         if (main_rate == rate)
1113                                 goto out;
1114                 } else if (main_pll->u.pll.dyn_ramp) {
1115                         pr_warn("%s: not ready for dynamic ramp to %lu\n",
1116                                 main_pll->name, rate);
1117                 }
1118         }
1119
1120         /* Switch to back-up source, and stay on it if target rate is below
1121            backup rate */
1122         if (c->parent->parent != c->u.cpu.backup) {
1123                 ret = clk_set_parent(c->parent, c->u.cpu.backup);
1124                 if (ret) {
1125                         pr_err("Failed to switch cpu to %s\n",
1126                                c->u.cpu.backup->name);
1127                         goto out;
1128                 }
1129         }
1130
1131         backup_rate = min(rate, c->u.cpu.backup_rate);
1132         if (backup_rate != clk_get_rate_locked(c)) {
1133                 ret = clk_set_rate(c->u.cpu.backup, backup_rate);
1134                 if (ret) {
1135                         pr_err("Failed to set cpu rate %lu on backup source\n",
1136                                backup_rate);
1137                         goto out;
1138                 }
1139         }
1140         if (rate == backup_rate)
1141                 goto out;
1142
1143         /* Switch from backup source to main at rate not exceeding pll VCO
1144            minimum. Use dynamic ramp to reach target rate if it is above VCO
1145            minimum. */
1146         main_rate = rate;
1147         if (!tegra_pll_can_ramp_from_min(main_pll, rate, &main_rate)) {
1148                 if (main_pll->u.pll.dyn_ramp)
1149                         pr_warn("%s: not ready for dynamic ramp to %lu\n",
1150                                 main_pll->name, rate);
1151         }
1152
1153         ret = clk_set_rate(main_pll, main_rate);
1154         if (ret) {
1155                 pr_err("Failed to set cpu rate %lu on source"
1156                        " %s\n", main_rate, main_pll->name);
1157                 goto out;
1158         }
1159
1160         ret = clk_set_parent(c->parent, main_pll);
1161         if (ret) {
1162                 pr_err("Failed to switch cpu to %s\n", main_pll->name);
1163                 goto out;
1164         }
1165
1166         if (rate != main_rate) {
1167                 ret = clk_set_rate(main_pll, rate);
1168                 if (ret) {
1169                         pr_err("Failed to set cpu rate %lu on source"
1170                                " %s\n", rate, main_pll->name);
1171                         goto out;
1172                 }
1173         }
1174
1175 out:
1176         if (on_main)
1177                 tegra_clk_disable_unprepare(main_pll);
1178
1179         return ret;
1180 }
1181
1182 static int tegra21_cpu_clk_dfll_on(struct clk *c, unsigned long rate,
1183                                    unsigned long old_rate)
1184 {
1185         int ret;
1186         struct clk *dfll = c->u.cpu.dynamic;
1187         unsigned long dfll_rate_min = c->dvfs->dfll_data.use_dfll_rate_min;
1188
1189         /* dfll rate request */
1190         ret = clk_set_rate(dfll, rate);
1191         if (ret) {
1192                 pr_err("Failed to set cpu rate %lu on source"
1193                        " %s\n", rate, dfll->name);
1194                 return ret;
1195         }
1196
1197         /* 1st time - switch to dfll */
1198         if (c->parent->parent != dfll) {
1199                 if (max(old_rate, rate) < dfll_rate_min) {
1200                         /* set interim cpu dvfs rate at dfll_rate_min to
1201                            prevent voltage drop below dfll Vmin */
1202                         ret = tegra_dvfs_set_rate(c, dfll_rate_min);
1203                         if (ret) {
1204                                 pr_err("Failed to set cpu dvfs rate %lu\n",
1205                                        dfll_rate_min);
1206                                 return ret;
1207                         }
1208                 }
1209
1210                 tegra_dvfs_rail_mode_updating(tegra_cpu_rail, true);
1211                 ret = clk_set_parent(c->parent, dfll);
1212                 if (ret) {
1213                         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1214                         pr_err("Failed to switch cpu to %s\n", dfll->name);
1215                         return ret;
1216                 }
1217                 ret = tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 1);
1218                 WARN(ret, "Failed to lock %s at rate %lu\n", dfll->name, rate);
1219
1220                 /* prevent legacy dvfs voltage scaling */
1221                 tegra_dvfs_dfll_mode_set(c->dvfs, rate);
1222                 tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1223         }
1224         return 0;
1225 }
1226
1227 static int tegra21_cpu_clk_dfll_off(struct clk *c, unsigned long rate,
1228                                     unsigned long old_rate)
1229 {
1230         int ret;
1231         struct clk *pll;
1232         struct clk *dfll = c->u.cpu.dynamic;
1233         unsigned long dfll_rate_min = c->dvfs->dfll_data.use_dfll_rate_min;
1234
1235         rate = min(rate, c->max_rate - c->dvfs->dfll_data.max_rate_boost);
1236         pll = (rate <= c->u.cpu.backup_rate) ? c->u.cpu.backup : c->u.cpu.main;
1237         dfll_rate_min = max(rate, dfll_rate_min);
1238
1239         /* set target rate last time in dfll mode */
1240         if (old_rate != dfll_rate_min) {
1241                 ret = tegra_dvfs_set_rate(c, dfll_rate_min);
1242                 if (!ret)
1243                         ret = clk_set_rate(dfll, dfll_rate_min);
1244
1245                 if (ret) {
1246                         pr_err("Failed to set cpu rate %lu on source %s\n",
1247                                dfll_rate_min, dfll->name);
1248                         return ret;
1249                 }
1250         }
1251
1252         /* unlock dfll - release volatge rail control */
1253         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, true);
1254         ret = tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 0);
1255         if (ret) {
1256                 pr_err("Failed to unlock %s\n", dfll->name);
1257                 goto back_to_dfll;
1258         }
1259
1260         /* restore legacy dvfs operations and set appropriate voltage */
1261         ret = tegra_dvfs_dfll_mode_clear(c->dvfs, dfll_rate_min);
1262         if (ret) {
1263                 pr_err("Failed to set cpu rail for rate %lu\n", rate);
1264                 goto back_to_dfll;
1265         }
1266
1267         /* set pll to target rate and return to pll source */
1268         ret = clk_set_rate(pll, rate);
1269         if (ret) {
1270                 pr_err("Failed to set cpu rate %lu on source"
1271                        " %s\n", rate, pll->name);
1272                 goto back_to_dfll;
1273         }
1274         ret = clk_set_parent(c->parent, pll);
1275         if (ret) {
1276                 pr_err("Failed to switch cpu to %s\n", pll->name);
1277                 goto back_to_dfll;
1278         }
1279
1280         /* If going up, adjust voltage here (down path is taken care of by the
1281            framework after set rate exit) */
1282         if (old_rate <= rate)
1283                 tegra_dvfs_set_rate(c, rate);
1284
1285         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1286         return 0;
1287
1288 back_to_dfll:
1289         tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 1);
1290         tegra_dvfs_dfll_mode_set(c->dvfs, old_rate);
1291         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1292         return ret;
1293 }
1294
1295 static int tegra21_cpu_clk_set_rate(struct clk *c, unsigned long rate)
1296 {
1297         unsigned long old_rate = clk_get_rate_locked(c);
1298         bool has_dfll = c->u.cpu.dynamic &&
1299                 (c->u.cpu.dynamic->state != UNINITIALIZED);
1300         bool is_dfll = c->parent->parent == c->u.cpu.dynamic;
1301
1302         /* On SILICON allow CPU rate change only if cpu regulator is connected.
1303            Ignore regulator connection on FPGA and SIMULATION platforms. */
1304         if (c->dvfs && tegra_platform_is_silicon()) {
1305                 if (!c->dvfs->dvfs_rail)
1306                         return -ENOSYS;
1307                 else if ((!c->dvfs->dvfs_rail->reg) && (old_rate < rate) &&
1308                          (c->boot_rate < rate)) {
1309                         WARN(1, "Increasing CPU rate while regulator is not"
1310                                 " ready is not allowed\n");
1311                         return -ENOSYS;
1312                 }
1313         }
1314         if (has_dfll && c->dvfs && c->dvfs->dvfs_rail) {
1315                 if (tegra_dvfs_is_dfll_range(c->dvfs, rate))
1316                         return tegra21_cpu_clk_dfll_on(c, rate, old_rate);
1317                 else if (is_dfll)
1318                         return tegra21_cpu_clk_dfll_off(c, rate, old_rate);
1319         }
1320         return tegra21_cpu_clk_set_plls(c, rate, old_rate);
1321 }
1322
1323 static long tegra21_cpu_clk_round_rate(struct clk *c, unsigned long rate)
1324 {
1325         unsigned long max_rate = c->max_rate;
1326
1327         /* Remove dfll boost to maximum rate when running on PLL */
1328         if (c->dvfs && !tegra_dvfs_is_dfll_scale(c->dvfs, rate))
1329                 max_rate -= c->dvfs->dfll_data.max_rate_boost;
1330
1331         if (rate > max_rate)
1332                 rate = max_rate;
1333         else if (rate < c->min_rate)
1334                 rate = c->min_rate;
1335         return rate;
1336 }
1337
1338 static struct clk_ops tegra_cpu_ops = {
1339         .init     = tegra21_cpu_clk_init,
1340         .enable   = tegra21_cpu_clk_enable,
1341         .disable  = tegra21_cpu_clk_disable,
1342         .set_rate = tegra21_cpu_clk_set_rate,
1343         .round_rate = tegra21_cpu_clk_round_rate,
1344 };
1345
1346
1347 static void tegra21_cpu_cmplx_clk_init(struct clk *c)
1348 {
1349         int i = is_lp_cluster() ? 1 : 0;
1350
1351         BUG_ON(c->inputs[0].input->u.cpu.mode != MODE_G);
1352         BUG_ON(c->inputs[1].input->u.cpu.mode != MODE_LP);
1353
1354         c->parent = c->inputs[i].input;
1355 }
1356
1357 /* cpu complex clock provides second level vitualization (on top of
1358    cpu virtual cpu rate control) in order to hide the CPU mode switch
1359    sequence */
1360 #if PARAMETERIZE_CLUSTER_SWITCH
1361 static unsigned int switch_delay;
1362 static unsigned int switch_flags;
1363 static DEFINE_SPINLOCK(parameters_lock);
1364
1365 void tegra_cluster_switch_set_parameters(unsigned int us, unsigned int flags)
1366 {
1367         spin_lock(&parameters_lock);
1368         switch_delay = us;
1369         switch_flags = flags;
1370         spin_unlock(&parameters_lock);
1371 }
1372 #endif
1373
1374 static int tegra21_cpu_cmplx_clk_enable(struct clk *c)
1375 {
1376         return 0;
1377 }
1378
1379 static void tegra21_cpu_cmplx_clk_disable(struct clk *c)
1380 {
1381         pr_debug("%s on clock %s\n", __func__, c->name);
1382
1383         /* oops - don't disable the CPU complex clock! */
1384         BUG();
1385 }
1386
1387 static int tegra21_cpu_cmplx_clk_set_rate(struct clk *c, unsigned long rate)
1388 {
1389         unsigned long flags;
1390         int ret;
1391         struct clk *parent = c->parent;
1392
1393         if (!parent->ops || !parent->ops->set_rate)
1394                 return -ENOSYS;
1395
1396         clk_lock_save(parent, &flags);
1397
1398         ret = clk_set_rate_locked(parent, rate);
1399
1400         clk_unlock_restore(parent, &flags);
1401
1402         return ret;
1403 }
1404
1405 static int cpu_cmplx_find_edp_safe_rates(unsigned long rate,
1406         struct clk *c_from, unsigned long *rate_from,
1407         struct clk *c_to, unsigned long *rate_to)
1408 {
1409         int ret;
1410         *rate_from = rate; /* current rate is EDP safe on current cluster */
1411         *rate_to = clk_get_cpu_edp_safe_rate(c_to);
1412
1413         /* EDP safe rate on target cluster is not specified: iso rate switch */
1414         if (*rate_to == 0) {
1415                 rate = max(rate, c_to->min_rate);
1416                 rate = min(rate, c_to->max_rate);
1417                 BUG_ON((rate < c_from->min_rate) || (rate > c_from->max_rate));
1418                 *rate_from = rate;
1419                 *rate_to = rate;
1420                 return 0;
1421         }
1422
1423         /* Determine EDP safe rates, given DVFS voltage interdependencies */
1424         ret = tegra_dvfs_butterfly_throttle(c_from, rate_from, c_to, rate_to);
1425         if (ret)
1426                 return ret;
1427
1428         if ((*rate_from < c_from->min_rate) ||
1429             (*rate_from > c_from->max_rate)) {
1430                 pr_err("%s: EDP safe %s rate %lu out of range (%lu %lu)\n",
1431                        __func__, c_from->name, *rate_from,
1432                        c_from->min_rate, c_from->max_rate);
1433                 return -EINVAL;
1434         }
1435
1436         if ((*rate_to < c_to->min_rate) || (*rate_to > c_to->max_rate)) {
1437                 pr_err("%s: EDP safe %s rate %lu out of range (%lu %lu)\n",
1438                        __func__, c_to->name, *rate_to,
1439                        c_to->min_rate, c_to->max_rate);
1440                 return -EINVAL;
1441         }
1442
1443         return 0;
1444 }
1445
1446 static int cpu_cmplx_set_rate_from(struct clk *c_from, unsigned long rate_from,
1447                                    unsigned long rate, struct clk **c_source)
1448 {
1449         if (rate_from != rate) {
1450                 int ret = clk_set_rate(c_from, rate_from);
1451                 if (ret) {
1452                         pr_err("%s: Failed to set rate %lu for %s\n",
1453                                __func__, rate_from, c_from->name);
1454                         return ret;
1455                 }
1456                 *c_source = c_from->parent->parent;
1457         }
1458         return 0;
1459 }
1460
1461 static int tegra21_cpu_cmplx_clk_set_parent(struct clk *c, struct clk *p)
1462 {
1463         int ret;
1464         unsigned int flags = 0, delay = 0;
1465         const struct clk_mux_sel *sel;
1466         unsigned long rate_from, rate_to;
1467         unsigned long rate = clk_get_rate(c->parent);
1468         struct clk *dfll = c->parent->u.cpu.dynamic ? : p->u.cpu.dynamic;
1469         struct clk *c_source, *p_source;
1470         bool dfll_range_to;
1471
1472         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
1473         if (!c->refcnt) {
1474                 WARN(1, "%s: cpu refcnt is 0: aborted switch from %s to %s\n",
1475                      __func__, c->parent->name, p->name);
1476                 return -EINVAL;
1477         }
1478         BUG_ON(c->parent->u.cpu.mode != (is_lp_cluster() ? MODE_LP : MODE_G));
1479
1480         for (sel = c->inputs; sel->input != NULL; sel++) {
1481                 if (sel->input == p)
1482                         break;
1483         }
1484         if (!sel->input)
1485                 return -EINVAL;
1486
1487 #if PARAMETERIZE_CLUSTER_SWITCH
1488         spin_lock(&parameters_lock);
1489         flags = switch_flags;
1490         delay = switch_delay;
1491         switch_flags = 0;
1492         spin_unlock(&parameters_lock);
1493 #endif
1494         if (!flags) {
1495                 flags = TEGRA_POWER_CLUSTER_IMMEDIATE;
1496                 flags |= TEGRA_POWER_CLUSTER_PART_DEFAULT;
1497                 delay = 0;
1498         }
1499         flags |= (p->u.cpu.mode == MODE_LP) ? TEGRA_POWER_CLUSTER_LP :
1500                 TEGRA_POWER_CLUSTER_G;
1501
1502         if (p == c->parent) {
1503                 if (flags & TEGRA_POWER_CLUSTER_FORCE) {
1504                         /* Allow parameterized switch to the same mode */
1505                         ret = tegra_cluster_control(delay, flags);
1506                         if (ret)
1507                                 pr_err("%s: Failed to force %s mode to %s\n",
1508                                        __func__, c->name, p->name);
1509                         return ret;
1510                 }
1511                 return 0;       /* already switched - exit */
1512         }
1513
1514         /* Find EDP safe rates for switch */
1515         ret = cpu_cmplx_find_edp_safe_rates(
1516                 rate, c->parent, &rate_from, p, &rate_to);
1517         if (ret) {
1518                 pr_err("%s: Didn't find EDP safe rates for %s to %s switch\n",
1519                        __func__, c->parent->name, p->name);
1520                 return ret;
1521         }
1522
1523         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, true);
1524         c_source = c->parent->parent->parent;
1525         dfll_range_to = p->dvfs && tegra_dvfs_is_dfll_range(p->dvfs, rate_to);
1526         if (c_source == dfll) {
1527                 /*
1528                  * G (DFLL selected as clock source) => LP switch:
1529                  * - set EDP safe rate on current cluster
1530                  * - turn DFLL into open loop mode ("release" VDD_CPU rail)
1531                  * - select target p_source for LP, and get EDP safe rate ready
1532                  */
1533                 ret = cpu_cmplx_set_rate_from(c->parent, rate_from, rate,
1534                                               &c_source);
1535                 if (ret)
1536                         goto abort;
1537
1538                 ret = tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 0);
1539                 if (ret)
1540                         goto abort;
1541
1542                 p_source = rate_to <= p->u.cpu.backup_rate ?
1543                         p->u.cpu.backup : p->u.cpu.main;
1544                 ret = clk_set_rate(p_source, rate_to);
1545                 if (ret)
1546                         goto abort;
1547         } else if ((p->parent->parent == dfll) || dfll_range_to) {
1548                 /*
1549                  * LP => G (DFLL selected as clock source) switch:
1550                  * - set EDP safe rate on current cluster
1551                  * - select target p_source as dfll, and set DFLL rate ready.
1552                  * DFLL is still disabled, initial rate only set skipper so
1553                  * that when enabled in open loop DFLL is running at low EDP
1554                  * safe rate. In case when the entire G cluster rate range is
1555                  * sourced from DFLL any EDP safe rate also guarantees voltage
1556                  * above DFLL Vmin. In case when there is low PLL sourced rate
1557                  * range, we need explicitly apply DFLL rate floor, which may
1558                  * violate EDP limitations. Only former case is productized on
1559                  * Tegra21.
1560                  */
1561                 ret = cpu_cmplx_set_rate_from(c->parent, rate_from, rate,
1562                                               &c_source);
1563                 if (ret)
1564                         goto abort;
1565
1566                 BUG_ON(!dfll_range_to);
1567
1568                 p_source = dfll;
1569                 ret = clk_set_rate(dfll, dfll_range_to ? rate_to :
1570                         max(rate_to, p->dvfs->dfll_data.use_dfll_rate_min));
1571                 if (ret)
1572                         goto abort;
1573         } else {
1574                 /*
1575                  * DFLL is not selected on either side of the switch:
1576                  * - set common EDP safe rate (since we use the same PLL on
1577                  *   both clusters in this case)
1578                  * - set target p_source equal to current clock source
1579                  */
1580                 rate_from = min(rate_from, rate_to);
1581                 if ((rate_from < c->parent->min_rate) ||
1582                     (rate_from < p->min_rate)) {
1583                         pr_err("%s: common EDP safe rate %lu out of range\n",
1584                                __func__, rate_from);
1585                         ret = -EINVAL;
1586                         goto abort;
1587                 }
1588
1589                 ret = cpu_cmplx_set_rate_from(c->parent, rate_from, rate,
1590                                               &c_source);
1591                 if (ret)
1592                         goto abort;
1593
1594                 p_source = c_source;
1595         }
1596
1597         /* Switch new parent to target clock source if necessary */
1598         if (p->parent->parent != p_source) {
1599                 ret = clk_set_parent(p->parent, p_source);
1600                 if (ret) {
1601                         pr_err("%s: Failed to set parent %s for %s\n",
1602                                __func__, p_source->name, p->name);
1603                         goto abort;
1604                 }
1605         }
1606
1607         /*
1608          * G=>LP switch with c_source DFLL in open loop mode: restore legacy
1609          * DVFS rail control with no changes in voltage (DFLL is still sourcing
1610          * G cluster, and no need to bump up voltage to PLLX V/F curve); then
1611          * enabling new parent (LP cluster clock) will set voltage to the max of
1612          * G-on-DFLL and LP-on-PLLX required levels.
1613          *
1614          * For any other combination of sources, rail is already under legacy
1615          * DVFS control, and enabling new parent scales new mode voltage rail
1616          * in advance before the switch happens. If target p_source is DFLL it
1617          * is switched here to open loop mode.
1618          */
1619         if (c_source == dfll)
1620                 tegra_dvfs_dfll_mode_clear(c->parent->dvfs, 0);
1621
1622         ret = tegra_clk_prepare_enable(p);
1623         if (ret) {
1624                 pr_err("%s: Failed to enable parent clock %s\n",
1625                         __func__, p->name);
1626                 goto abort;
1627         }
1628
1629         pr_debug("%s: switch %s(rate %lu) to %s(rate %lu) at %d mV\n", __func__,
1630                  c->parent->name, clk_get_rate(c->parent),
1631                  p->name, clk_get_rate(p),
1632                  tegra_dvfs_rail_get_current_millivolts(tegra_cpu_rail));
1633
1634         /* switch CPU mode */
1635         ret = tegra_cluster_control(delay, flags);
1636         if (ret) {
1637                 tegra_clk_disable_unprepare(p);
1638                 pr_err("%s: Failed to switch %s mode to %s\n",
1639                        __func__, c->name, p->name);
1640                 goto abort;
1641         }
1642
1643         /*
1644          * Lock DFLL now (resume closed loop VDD_CPU control).
1645          * G CPU operations are resumed on DFLL if it was the last G CPU
1646          * clock source, or if resume rate is in DFLL usage range in case
1647          * when auto-switch between PLL and DFLL is enabled.
1648          */
1649         if (p_source == dfll) {
1650                 clk_set_rate(dfll, rate_to);
1651                 tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 1);
1652                 tegra_dvfs_dfll_mode_set(p->dvfs, rate_to);
1653         }
1654
1655         /* Disabling old parent scales old mode voltage rail */
1656         tegra_clk_disable_unprepare(c->parent);
1657
1658         clk_reparent(c, p);
1659
1660         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1661         return 0;
1662
1663 abort:
1664         /* Restore rate, and re-lock DFLL if necessary after aborted switch */
1665         clk_set_rate(c->parent, rate);
1666         if (c_source == dfll) {
1667                 tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 1);
1668                 tegra_dvfs_dfll_mode_set(c->parent->dvfs, rate);
1669         }
1670         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1671
1672         pr_err("%s: aborted switch from %s to %s\n",
1673                __func__, c->parent->name, p->name);
1674         return ret;
1675 }
1676
1677 static long tegra21_cpu_cmplx_round_rate(struct clk *c,
1678         unsigned long rate)
1679 {
1680         return clk_round_rate(c->parent, rate);
1681 }
1682
1683 static struct clk_ops tegra_cpu_cmplx_ops = {
1684         .init     = tegra21_cpu_cmplx_clk_init,
1685         .enable   = tegra21_cpu_cmplx_clk_enable,
1686         .disable  = tegra21_cpu_cmplx_clk_disable,
1687         .set_rate = tegra21_cpu_cmplx_clk_set_rate,
1688         .set_parent = tegra21_cpu_cmplx_clk_set_parent,
1689         .round_rate = tegra21_cpu_cmplx_round_rate,
1690 };
1691
1692 /* virtual cop clock functions. Used to acquire the fake 'cop' clock to
1693  * reset the COP block (i.e. AVP) */
1694 static void tegra21_cop_clk_reset(struct clk *c, bool assert)
1695 {
1696         unsigned long reg = assert ? RST_DEVICES_SET_L : RST_DEVICES_CLR_L;
1697
1698         pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
1699         clk_writel(1 << 1, reg);
1700 }
1701
1702 static struct clk_ops tegra_cop_ops = {
1703         .reset    = tegra21_cop_clk_reset,
1704 };
1705
1706 /* bus clock functions */
1707 static DEFINE_SPINLOCK(bus_clk_lock);
1708
1709 static int bus_set_div(struct clk *c, int div)
1710 {
1711         u32 val;
1712         unsigned long flags;
1713
1714         if (!div || (div > (BUS_CLK_DIV_MASK + 1)))
1715                 return -EINVAL;
1716
1717         spin_lock_irqsave(&bus_clk_lock, flags);
1718         val = clk_readl(c->reg);
1719         val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
1720         val |= (div - 1) << c->reg_shift;
1721         clk_writel(val, c->reg);
1722         c->div = div;
1723         spin_unlock_irqrestore(&bus_clk_lock, flags);
1724
1725         return 0;
1726 }
1727
1728 static void tegra21_bus_clk_init(struct clk *c)
1729 {
1730         u32 val = clk_readl(c->reg);
1731         c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
1732         c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
1733         c->mul = 1;
1734 }
1735
1736 static int tegra21_bus_clk_enable(struct clk *c)
1737 {
1738         u32 val = clk_readl(c->reg);
1739         val &= ~(BUS_CLK_DISABLE << c->reg_shift);
1740         clk_writel(val, c->reg);
1741         return 0;
1742 }
1743
1744 static void tegra21_bus_clk_disable(struct clk *c)
1745 {
1746         u32 val = clk_readl(c->reg);
1747         val |= BUS_CLK_DISABLE << c->reg_shift;
1748         clk_writel(val, c->reg);
1749 }
1750
1751 static int tegra21_bus_clk_set_rate(struct clk *c, unsigned long rate)
1752 {
1753         unsigned long parent_rate = clk_get_rate(c->parent);
1754         int i;
1755
1756         for (i = 1; i <= 4; i++) {
1757                 if (rate >= parent_rate / i)
1758                         return bus_set_div(c, i);
1759         }
1760         return -EINVAL;
1761 }
1762
1763 static struct clk_ops tegra_bus_ops = {
1764         .init                   = tegra21_bus_clk_init,
1765         .enable                 = tegra21_bus_clk_enable,
1766         .disable                = tegra21_bus_clk_disable,
1767         .set_rate               = tegra21_bus_clk_set_rate,
1768 };
1769
1770 /*
1771  * Virtual system bus complex clock is used to hide the sequence of
1772  * changing sclk/hclk/pclk parents and dividers to configure requested
1773  * sclk target rate.
1774  */
1775 #define BUS_AHB_DIV_MAX                 (BUS_CLK_DIV_MASK + 1UL)
1776 #define BUS_APB_DIV_MAX                 (BUS_CLK_DIV_MASK + 1UL)
1777
1778 static unsigned long sclk_pclk_unity_ratio_rate_max = 136000000;
1779
1780 struct clk_div_sel {
1781         struct clk *src;
1782         u32 div;
1783         unsigned long rate;
1784 };
1785 static struct clk_div_sel sbus_round_table[MAX_DVFS_FREQS + 1];
1786 static int sbus_round_table_size;
1787
1788 static int last_round_idx;
1789 static int get_start_idx(unsigned long rate)
1790 {
1791         int i = last_round_idx;
1792         if (rate == sbus_round_table[i].rate)
1793                 return i;
1794         return 0;
1795 }
1796
1797
1798 static void tegra21_sbus_cmplx_init(struct clk *c)
1799 {
1800         unsigned long rate;
1801         struct clk *sclk_div = c->parent->parent;
1802
1803         c->max_rate = sclk_div->max_rate;
1804         c->min_rate = sclk_div->min_rate;
1805
1806         rate = clk_get_rate(c->u.system.sclk_low);
1807         if (tegra_platform_is_qt())
1808                 return;
1809
1810         /*
1811          * Unity and low range thresholds must be an exact proper factors of
1812          * low range parent. Unity threshold must be set within low range
1813          */
1814         if (c->u.system.threshold &&
1815             (sclk_pclk_unity_ratio_rate_max > c->u.system.threshold))
1816                 sclk_pclk_unity_ratio_rate_max = c->u.system.threshold;
1817         if (sclk_pclk_unity_ratio_rate_max)
1818                 BUG_ON((rate % sclk_pclk_unity_ratio_rate_max) != 0);
1819         if (c->u.system.threshold)
1820                 BUG_ON((rate % c->u.system.threshold) != 0);
1821         BUG_ON(!(sclk_div->flags & DIV_U71));
1822 }
1823
1824 /* This special sbus round function is implemented because:
1825  *
1826  * (a) sbus complex clock source is selected automatically based on rate
1827  *
1828  * (b) since sbus is a shared bus, and its frequency is set to the highest
1829  * enabled shared_bus_user clock, the target rate should be rounded up divider
1830  * ladder (if max limit allows it) - for pll_div and peripheral_div common is
1831  * rounding down - special case again.
1832  *
1833  * Note that final rate is trimmed (not rounded up) to avoid spiraling up in
1834  * recursive calls. Lost 1Hz is added in tegra21_sbus_cmplx_set_rate before
1835  * actually setting divider rate.
1836  */
1837 static void sbus_build_round_table_one(struct clk *c, unsigned long rate, int j)
1838 {
1839         struct clk_div_sel sel;
1840         struct clk *sclk_div = c->parent->parent;
1841         u32 flags = sclk_div->flags;
1842
1843         sel.src = c->u.system.sclk_low;
1844         sel.rate = fixed_src_bus_round_updown(
1845                 c, sel.src, flags, rate, false, &sel.div);
1846         sbus_round_table[j] = sel;
1847
1848         /* Don't use high frequency source above threshold */
1849         if (rate <= c->u.system.threshold)
1850                 return;
1851
1852         sel.src = c->u.system.sclk_high;
1853         sel.rate = fixed_src_bus_round_updown(
1854                 c, sel.src, flags, rate, false, &sel.div);
1855         if (sbus_round_table[j].rate < sel.rate)
1856                 sbus_round_table[j] = sel;
1857 }
1858
1859 /* Populate sbus (not Avalon) round table with dvfs entries (not knights) */
1860 static void sbus_build_round_table(struct clk *c)
1861 {
1862         int i, j = 0;
1863         unsigned long rate;
1864         bool inserted_u = false;
1865         bool inserted_t = false;
1866         unsigned long threshold = c->u.system.threshold;
1867
1868         /*
1869          * Make sure unity ratio threshold always inserted into the table.
1870          * If no dvfs specified, just add maximum rate entry. Othrwise, add
1871          * entries for all dvfs rates.
1872          */
1873         if (!c->dvfs || !c->dvfs->num_freqs) {
1874                 sbus_build_round_table_one(
1875                         c, sclk_pclk_unity_ratio_rate_max, j++);
1876                 sbus_build_round_table_one(c, threshold, j++);
1877                 sbus_build_round_table_one(c, c->max_rate, j++);
1878                 sbus_round_table_size = j;
1879                 return;
1880         }
1881
1882         for (i = 0; i < c->dvfs->num_freqs; i++) {
1883                 rate = c->dvfs->freqs[i];
1884                 if (rate <= 1 * c->dvfs->freqs_mult)
1885                         continue; /* skip 1kHz place holders */
1886
1887                 if (i && (rate == c->dvfs->freqs[i - 1]))
1888                         continue; /* skip duplicated rate */
1889
1890                 if (!inserted_u && (rate >= sclk_pclk_unity_ratio_rate_max)) {
1891                         inserted_u = true;
1892                         if (sclk_pclk_unity_ratio_rate_max == threshold)
1893                                 inserted_t = true;
1894
1895                         if (rate > sclk_pclk_unity_ratio_rate_max)
1896                                 sbus_build_round_table_one(
1897                                         c, sclk_pclk_unity_ratio_rate_max, j++);
1898                 }
1899                 if (!inserted_t && (rate >= threshold)) {
1900                         inserted_t = true;
1901                         if (rate > threshold)
1902                                 sbus_build_round_table_one(c, threshold, j++);
1903                 }
1904                 sbus_build_round_table_one(c, rate, j++);
1905         }
1906         sbus_round_table_size = j;
1907 }
1908
1909 /* Clip requested rate to the entry in the round table. Allow +/-1Hz slack. */
1910 static long tegra21_sbus_cmplx_round_updown(struct clk *c, unsigned long rate,
1911                                             bool up)
1912 {
1913         int i;
1914
1915         if (!sbus_round_table_size) {
1916                 sbus_build_round_table(c);
1917                 if (!sbus_round_table_size) {
1918                         WARN(1, "Invalid sbus round table\n");
1919                         return -EINVAL;
1920                 }
1921         }
1922
1923         rate = max(rate, c->min_rate);
1924
1925         i = get_start_idx(rate);
1926         for (; i < sbus_round_table_size - 1; i++) {
1927                 unsigned long sel_rate = sbus_round_table[i].rate;
1928                 if (abs(rate - sel_rate) <= 1) {
1929                         break;
1930                 } else if (rate < sel_rate) {
1931                         if (!up && i)
1932                                 i--;
1933                         break;
1934                 }
1935         }
1936         last_round_idx = i;
1937         return sbus_round_table[i].rate;
1938 }
1939
1940 static long tegra21_sbus_cmplx_round_rate(struct clk *c, unsigned long rate)
1941 {
1942         return tegra21_sbus_cmplx_round_updown(c, rate, true);
1943 }
1944
1945 /*
1946  * Select {source : divider} setting from pre-built round table, and actually
1947  * change the configuration. Since voltage during switch is at safe level for
1948  * current and new sbus rates (but not above) over-clocking during the switch
1949  * is not allowed. Hence, the order of switch: 1st change divider if its setting
1950  * increases, then switch source clock, and finally change divider if it goes
1951  * down. No over-clocking is guaranteed, but dip below both initial and final
1952  * rates is possible.
1953  */
1954 static int tegra21_sbus_cmplx_set_rate(struct clk *c, unsigned long rate)
1955 {
1956         int ret, i;
1957         struct clk *skipper = c->parent;
1958         struct clk *sclk_div = skipper->parent;
1959         struct clk *sclk_mux = sclk_div->parent;
1960         struct clk_div_sel *new_sel = NULL;
1961         unsigned long sclk_div_rate = clk_get_rate(sclk_div);
1962
1963         /*
1964          * Configure SCLK/HCLK/PCLK guranteed safe combination:
1965          * - keep hclk at the same rate as sclk
1966          * - set pclk at 1:2 rate of hclk
1967          * - disable sclk skipper
1968          */
1969         bus_set_div(c->u.system.pclk, 2);
1970         bus_set_div(c->u.system.hclk, 1);
1971         c->child_bus->child_bus->div = 2;
1972         c->child_bus->div = 1;
1973         clk_set_rate(skipper, c->max_rate);
1974
1975         /* Select new source/divider */
1976         i = get_start_idx(rate);
1977         for (; i < sbus_round_table_size; i++) {
1978                 if (rate == sbus_round_table[i].rate) {
1979                         new_sel = &sbus_round_table[i];
1980                         break;
1981                 }
1982         }
1983         if (!new_sel)
1984                 return -EINVAL;
1985
1986         if (sclk_div_rate == rate) {
1987                 pr_debug("sbus_set_rate: no change in rate %lu on parent %s\n",
1988                          clk_get_rate_locked(c), sclk_mux->parent->name);
1989                 return 0;
1990         }
1991
1992         /* Raise voltage on the way up */
1993         if (c->dvfs && (rate > sclk_div_rate)) {
1994                 ret = tegra_dvfs_set_rate(c, rate);
1995                 if (ret)
1996                         return ret;
1997                 pr_debug("sbus_set_rate: set %d mV\n", c->dvfs->cur_millivolts);
1998         }
1999
2000         /* Do switch */
2001         if (sclk_div->div < new_sel->div) {
2002                 unsigned long sdiv_rate = sclk_div_rate * sclk_div->div;
2003                 sdiv_rate = DIV_ROUND_UP(sdiv_rate, new_sel->div);
2004                 ret = clk_set_rate(sclk_div, sdiv_rate);
2005                 if (ret) {
2006                         pr_err("%s: Failed to set %s rate to %lu\n",
2007                                __func__, sclk_div->name, sdiv_rate);
2008                         return ret;
2009                 }
2010                 pr_debug("sbus_set_rate: rate %lu on parent %s\n",
2011                          clk_get_rate_locked(c), sclk_mux->parent->name);
2012
2013         }
2014
2015         if (new_sel->src != sclk_mux->parent) {
2016                 ret = clk_set_parent(sclk_mux, new_sel->src);
2017                 if (ret) {
2018                         pr_err("%s: Failed to switch sclk source to %s\n",
2019                                __func__, new_sel->src->name);
2020                         return ret;
2021                 }
2022                 pr_debug("sbus_set_rate: rate %lu on parent %s\n",
2023                          clk_get_rate_locked(c), sclk_mux->parent->name);
2024         }
2025
2026         if (sclk_div->div > new_sel->div) {
2027                 ret = clk_set_rate(sclk_div, rate + 1);
2028                 if (ret) {
2029                         pr_err("%s: Failed to set %s rate to %lu\n",
2030                                __func__, sclk_div->name, rate);
2031                         return ret;
2032                 }
2033                 pr_debug("sbus_set_rate: rate %lu on parent %s\n",
2034                          clk_get_rate_locked(c), sclk_mux->parent->name);
2035         }
2036
2037         /* Lower voltage on the way down */
2038         if (c->dvfs && (rate < sclk_div_rate)) {
2039                 ret = tegra_dvfs_set_rate(c, rate);
2040                 if (ret)
2041                         return ret;
2042                 pr_debug("sbus_set_rate: set %d mV\n", c->dvfs->cur_millivolts);
2043         }
2044
2045         return 0;
2046 }
2047
2048 /*
2049  * Limitations on SCLK/HCLK/PCLK ratios:
2050  * (A) H/w limitation:
2051  *      if SCLK >= 136MHz, SCLK:PCLK >= 2
2052  * (B) S/w policy limitation, in addition to (A):
2053  *      if any APB bus shared user request is enabled, HCLK:PCLK >= 2
2054  *  Reason for (B): assuming APB bus shared user has requested X < 136MHz,
2055  *  HCLK = PCLK = X, and new AHB user is coming on-line requesting Y >= 136MHz,
2056  *  we can consider 2 paths depending on order of changing HCLK rate and
2057  *  HCLK:PCLK ratio
2058  *  (i)  HCLK:PCLK = X:X => Y:Y* => Y:Y/2,   (*) violates rule (A)
2059  *  (ii) HCLK:PCLK = X:X => X:X/2* => Y:Y/2, (*) under-clocks APB user
2060  *  In this case we can not guarantee safe transition from HCLK:PCLK = 1:1
2061  *  below 60MHz to HCLK rate above 60MHz without under-clocking APB user.
2062  *  Hence, policy (B).
2063  *
2064  *  When there are no request from APB users, path (ii) can be used to
2065  *  increase HCLK above 136MHz, and HCLK:PCLK = 1:1 is allowed.
2066  *
2067  *  Note: with common divider used in the path for all SCLK sources SCLK rate
2068  *  during switching may dip down, anyway. So, in general, policy (ii) does not
2069  *  prevent underclocking users during clock transition.
2070  */
2071 static int tegra21_clk_sbus_update(struct clk *bus)
2072 {
2073         int ret, div;
2074         bool p_requested;
2075         unsigned long s_rate, h_rate, p_rate, ceiling, s_rate_raw;
2076         struct clk *ahb, *apb;
2077         struct clk *skipper = bus->parent;
2078
2079         if (detach_shared_bus)
2080                 return 0;
2081
2082         s_rate = tegra21_clk_shared_bus_update(bus, &ahb, &apb, &ceiling);
2083         if (bus->override_rate) {
2084                 ret = clk_set_rate_locked(bus, s_rate);
2085                 if (!ret)
2086                         clk_set_rate(skipper, s_rate);
2087                 return ret;
2088         }
2089
2090         ahb = bus->child_bus;
2091         apb = ahb->child_bus;
2092         h_rate = ahb->u.shared_bus_user.rate;
2093         p_rate = apb->u.shared_bus_user.rate;
2094         p_requested = apb->refcnt > 1;
2095
2096         /* Propagate ratio requirements up from PCLK to SCLK */
2097         if (p_requested)
2098                 h_rate = max(h_rate, p_rate * 2);
2099         s_rate = max(s_rate, h_rate);
2100         if (s_rate >= sclk_pclk_unity_ratio_rate_max)
2101                 s_rate = max(s_rate, p_rate * 2);
2102
2103         /* Propagate cap requirements down from SCLK to PCLK */
2104         s_rate_raw = s_rate;
2105         s_rate = tegra21_clk_cap_shared_bus(bus, s_rate, ceiling);
2106         if (s_rate >= sclk_pclk_unity_ratio_rate_max)
2107                 p_rate = min(p_rate, s_rate / 2);
2108         h_rate = min(h_rate, s_rate);
2109         if (p_requested)
2110                 p_rate = min(p_rate, h_rate / 2);
2111
2112
2113         /* Set new sclk rate in safe 1:1:2, rounded "up" configuration */
2114         ret = clk_set_rate_locked(bus, s_rate);
2115         if (ret)
2116                 return ret;
2117         clk_set_rate(skipper, s_rate_raw);
2118
2119         /* Finally settle new bus divider values */
2120         s_rate = clk_get_rate_locked(bus);
2121         div = min(s_rate / h_rate, BUS_AHB_DIV_MAX);
2122         if (div != 1) {
2123                 bus_set_div(bus->u.system.hclk, div);
2124                 ahb->div = div;
2125         }
2126
2127         h_rate = clk_get_rate(bus->u.system.hclk);
2128         div = min(h_rate / p_rate, BUS_APB_DIV_MAX);
2129         if (div != 2) {
2130                 bus_set_div(bus->u.system.pclk, div);
2131                 apb->div = div;
2132         }
2133
2134         return 0;
2135 }
2136
2137 static struct clk_ops tegra_sbus_cmplx_ops = {
2138         .init = tegra21_sbus_cmplx_init,
2139         .set_rate = tegra21_sbus_cmplx_set_rate,
2140         .round_rate = tegra21_sbus_cmplx_round_rate,
2141         .round_rate_updown = tegra21_sbus_cmplx_round_updown,
2142         .shared_bus_update = tegra21_clk_sbus_update,
2143 };
2144
2145 /*
2146  * Virtual ADSP bus clock operations. Used to hide the sequence of changing
2147  * and re-locking ADSP source PLLA1 in flight to configure requested ADSP
2148  * target rate.
2149  */
2150 static void tegra21_adsp_bus_clk_init(struct clk *c)
2151 {
2152         c->state = c->parent->state;
2153         c->min_rate = c->u.cpu.main->min_rate;
2154 }
2155
2156 static int tegra21_adsp_bus_clk_enable(struct clk *c)
2157 {
2158         return 0;
2159 }
2160
2161 static void tegra21_adsp_bus_clk_disable(struct clk *c)
2162 {
2163 }
2164
2165 static int tegra21_adsp_bus_clk_set_rate(struct clk *c, unsigned long rate)
2166 {
2167         int ret = 0;
2168         struct clk *main_pll = c->u.cpu.main;
2169
2170         /*
2171          * If ADSP clock is disabled or it is not on main pll (may happen after
2172          * boot), set main pll rate, and make sure it is selected as adsp clock
2173          * source.
2174          */
2175         if (!c->refcnt || (c->parent->parent != main_pll)) {
2176                 ret = clk_set_rate(main_pll, rate);
2177                 if (ret) {
2178                         pr_err("Failed to set adsp rate %lu on %s\n",
2179                                rate, main_pll->name);
2180                         return ret;
2181                 }
2182
2183                 if (c->parent->parent == main_pll)
2184                         return ret;
2185
2186                 ret = clk_set_parent(c->parent, main_pll);
2187                 if (ret)
2188                         pr_err("Failed to switch adsp to %s\n", main_pll->name);
2189
2190                 return ret;
2191         }
2192
2193         /*
2194          * Take an extra reference to the main pll so it doesn't turn off when
2195          * we move the cpu off of it. If possible, use main pll dynamic ramp
2196          * to reach target rate in one shot. Otherwise use backup source while
2197          * re-locking main.
2198          */
2199         tegra_clk_prepare_enable(main_pll);
2200
2201         if (tegra_pll_can_ramp_to_rate(main_pll, rate)) {
2202                 ret = clk_set_rate(main_pll, rate);
2203                 if (ret)
2204                         pr_err("Failed to ramp %s to adsp rate %lu\n",
2205                                main_pll->name, rate);
2206                 goto out;
2207         }
2208
2209         /* Set backup divider, and switch to backup source */
2210         super_clk_set_u71_div_no_skip(c->parent);
2211         ret = clk_set_parent(c->parent, c->u.cpu.backup);
2212         if (ret) {
2213                 super_clk_clr_u71_div_no_skip(c->parent);
2214                 pr_err("Failed to switch adsp to %s\n", c->u.cpu.backup->name);
2215                 goto out;
2216         }
2217
2218         /* Set main pll rate, switch back to main, and clear backup divider */
2219         ret = clk_set_rate(main_pll, rate);
2220         if (ret) {
2221                 pr_err("Failed set adsp rate %lu on %s\n",
2222                        rate, main_pll->name);
2223                 goto out;
2224         }
2225
2226         ret = clk_set_parent(c->parent, main_pll);
2227         if (ret) {
2228                 pr_err("Failed to switch adsp to %s\n", main_pll->name);
2229                 goto out;
2230         }
2231         super_clk_clr_u71_div_no_skip(c->parent);
2232
2233 out:
2234         tegra_clk_disable_unprepare(main_pll);
2235         return ret;
2236 }
2237
2238 static long tegra21_adsp_bus_clk_round_rate(struct clk *c, unsigned long rate)
2239 {
2240         if (rate > c->max_rate)
2241                 rate = c->max_rate;
2242         else if (rate < c->min_rate)
2243                 rate = c->min_rate;
2244         return rate;
2245 }
2246
2247 /* Blink output functions */
2248 static void tegra21_blink_clk_init(struct clk *c)
2249 {
2250         u32 val;
2251
2252         val = pmc_readl(PMC_CTRL);
2253         c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
2254         c->mul = 1;
2255         val = pmc_readl(c->reg);
2256
2257         if (val & PMC_BLINK_TIMER_ENB) {
2258                 unsigned int on_off;
2259
2260                 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
2261                         PMC_BLINK_TIMER_DATA_ON_MASK;
2262                 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
2263                 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
2264                 on_off += val;
2265                 /* each tick in the blink timer is 4 32KHz clocks */
2266                 c->div = on_off * 4;
2267         } else {
2268                 c->div = 1;
2269         }
2270 }
2271
2272 static int tegra21_blink_clk_enable(struct clk *c)
2273 {
2274         u32 val;
2275
2276         val = pmc_readl(PMC_DPD_PADS_ORIDE);
2277         pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
2278
2279         val = pmc_readl(PMC_CTRL);
2280         pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
2281         pmc_readl(PMC_CTRL);
2282
2283         return 0;
2284 }
2285
2286 static void tegra21_blink_clk_disable(struct clk *c)
2287 {
2288         u32 val;
2289
2290         val = pmc_readl(PMC_CTRL);
2291         pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
2292
2293         val = pmc_readl(PMC_DPD_PADS_ORIDE);
2294         pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
2295         pmc_readl(PMC_DPD_PADS_ORIDE);
2296 }
2297
2298 static int tegra21_blink_clk_set_rate(struct clk *c, unsigned long rate)
2299 {
2300         unsigned long parent_rate = clk_get_rate(c->parent);
2301         if (rate >= parent_rate) {
2302                 c->div = 1;
2303                 pmc_writel(0, c->reg);
2304         } else {
2305                 unsigned int on_off;
2306                 u32 val;
2307
2308                 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
2309                 c->div = on_off * 8;
2310
2311                 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
2312                         PMC_BLINK_TIMER_DATA_ON_SHIFT;
2313                 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
2314                 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
2315                 val |= on_off;
2316                 val |= PMC_BLINK_TIMER_ENB;
2317                 pmc_writel(val, c->reg);
2318         }
2319         pmc_readl(c->reg);
2320
2321         return 0;
2322 }
2323
2324 static struct clk_ops tegra_blink_clk_ops = {
2325         .init                   = &tegra21_blink_clk_init,
2326         .enable                 = &tegra21_blink_clk_enable,
2327         .disable                = &tegra21_blink_clk_disable,
2328         .set_rate               = &tegra21_blink_clk_set_rate,
2329 };
2330
2331 /* UTMIP PLL configuration */
2332 static void tegra21_utmi_param_configure(struct clk *c)
2333 {
2334         u32 reg;
2335         int i;
2336         unsigned long main_rate =
2337                 clk_get_rate(c->parent->parent);
2338
2339         for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
2340                 if (main_rate == utmi_parameters[i].osc_frequency) {
2341                         break;
2342                 }
2343         }
2344
2345         if (i >= ARRAY_SIZE(utmi_parameters)) {
2346                 pr_err("%s: Unexpected main rate %lu\n", __func__, main_rate);
2347                 return;
2348         }
2349
2350         reg = clk_readl(UTMIPLL_HW_PWRDN_CFG0);
2351         reg &= ~UTMIPLL_HW_PWRDN_CFG0_IDDQ_OVERRIDE;
2352         pll_writel_delay(reg, UTMIPLL_HW_PWRDN_CFG0);
2353
2354         reg = clk_readl(UTMIP_PLL_CFG2);
2355         /* Program UTMIP PLL stable and active counts */
2356         /* [FIXME] arclk_rst.h says WRONG! This should be 1ms -> 0x50 Check! */
2357         reg &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
2358         reg |= UTMIP_PLL_CFG2_STABLE_COUNT(
2359                         utmi_parameters[i].stable_count);
2360         clk_writel(reg, UTMIP_PLL_CFG2);
2361
2362         /* Program UTMIP PLL delay and oscillator frequency counts */
2363         reg = clk_readl(UTMIP_PLL_CFG1);
2364         reg &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
2365         reg |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(
2366                 utmi_parameters[i].enable_delay_count);
2367
2368         reg &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
2369         reg |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(
2370                 utmi_parameters[i].xtal_freq_count);
2371
2372         /* Remove power downs from UTMIP PLL control bits */
2373         reg |= UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
2374         clk_writel(reg, UTMIP_PLL_CFG1);
2375
2376         /* Enable PLL with SW programming */
2377         reg = clk_readl(UTMIP_PLL_CFG1);
2378         reg |= UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP;
2379         reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
2380         pll_writel_delay(reg, UTMIP_PLL_CFG1);
2381
2382         /* Enable Samplers for SNPS IP, XUSB_HOST, XUSB_DEV */
2383         reg = clk_readl(UTMIP_PLL_CFG2);
2384         reg |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERUP;
2385         reg |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERUP;
2386         reg |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERUP;
2387         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
2388         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
2389         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERDOWN;
2390         clk_writel(reg, UTMIP_PLL_CFG2);
2391
2392         /* Enable HW Power Sequencer */
2393         reg = clk_readl(UTMIP_PLL_CFG1);
2394         reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP;
2395         reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
2396         clk_writel(reg, UTMIP_PLL_CFG1);
2397
2398         reg = clk_readl(UTMIPLL_HW_PWRDN_CFG0);
2399         reg &= ~UTMIPLL_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL;
2400         reg |= UTMIPLL_HW_PWRDN_CFG0_USE_LOCKDET;
2401         pll_writel_delay(reg, UTMIPLL_HW_PWRDN_CFG0);
2402
2403         reg = clk_readl(XUSB_PLL_CFG0);
2404         reg &= ~XUSB_PLL_CFG0_UTMIPLL_LOCK_DLY;
2405         pll_writel_delay(reg, XUSB_PLL_CFG0);
2406
2407         /* Enable HW control UTMIPLL */
2408         reg = clk_readl(UTMIPLL_HW_PWRDN_CFG0);
2409         reg |= UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE;
2410         pll_writel_delay(reg, UTMIPLL_HW_PWRDN_CFG0);
2411 }
2412
2413 /*
2414  * PLL post divider maps - two types: quasi-linear and exponential
2415  * post divider.
2416  */
2417 #define PLL_QLIN_PDIV_MAX       16
2418 static u8 pll_qlin_pdiv_to_p[PLL_QLIN_PDIV_MAX + 1] = {
2419 /* PDIV: 0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10, 11, 12, 13, 14, 15, 16 */
2420 /* p: */ 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 30, 32 };
2421
2422 static u32 pll_qlin_p_to_pdiv(u32 p, u32 *pdiv)
2423 {
2424         int i;
2425
2426         if (p) {
2427                 for (i = 0; i <= PLL_QLIN_PDIV_MAX; i++) {
2428                         if (p <= pll_qlin_pdiv_to_p[i]) {
2429                                 if (pdiv)
2430                                         *pdiv = i;
2431                                 return pll_qlin_pdiv_to_p[i];
2432                         }
2433                 }
2434         }
2435         return -EINVAL;
2436 }
2437
2438 #define PLL_EXPO_PDIV_MAX       7
2439 static u8 pll_expo_pdiv_to_p[PLL_EXPO_PDIV_MAX + 1] = {
2440 /* PDIV: 0, 1, 2, 3,  4,  5,  6,   7 */
2441 /* p: */ 1, 2, 4, 8, 16, 32, 64, 128 };
2442
2443 static u32 pll_expo_p_to_pdiv(u32 p, u32 *pdiv)
2444 {
2445         if (p) {
2446                 u32 i = fls(p);
2447                 if (i == ffs(p))
2448                         i--;
2449
2450                 if (i <= PLL_EXPO_PDIV_MAX) {
2451                         if (pdiv)
2452                                 *pdiv = i;
2453                         return 1 << i;
2454                 }
2455         }
2456         return -EINVAL;
2457 }
2458
2459 /*
2460  * PLLCX: PLLC, PLLC2, PLLC3, PLLA1
2461  * Hybrid PLLs with dynamic ramp. Dynamic ramp is allowed for any transition
2462  * that changes NDIV only, while PLL is already locked.
2463  */
2464 static void pllcx_check_defaults(struct clk *c, unsigned long input_rate)
2465 {
2466         u32 default_val;
2467
2468         default_val = PLLCX_MISC0_DEFAULT_VALUE & (~PLLCX_MISC0_RESET);
2469         PLL_MISC_CHK_DEFAULT(c, 0, default_val, PLLCX_MISC0_WRITE_MASK);
2470
2471         default_val = PLLCX_MISC1_DEFAULT_VALUE & (~PLLCX_MISC1_IDDQ);
2472         PLL_MISC_CHK_DEFAULT(c, 1, default_val, PLLCX_MISC1_WRITE_MASK);
2473
2474         default_val = PLLCX_MISC2_DEFAULT_VALUE;
2475         PLL_MISC_CHK_DEFAULT(c, 2, default_val, PLLCX_MISC2_WRITE_MASK);
2476
2477         default_val = PLLCX_MISC3_DEFAULT_VALUE;
2478         PLL_MISC_CHK_DEFAULT(c, 3, default_val, PLLCX_MISC3_WRITE_MASK);
2479 }
2480
2481 static void pllcx_set_defaults(struct clk *c, unsigned long input_rate)
2482 {
2483         c->u.pll.defaults_set = true;
2484
2485         if (clk_readl(c->reg) & c->u.pll.controls->enable_mask) {
2486                 /* PLL is ON: only check if defaults already set */
2487                 pllcx_check_defaults(c, input_rate);
2488                 return;
2489         }
2490
2491         /* Defaults assert PLL reset, and set IDDQ */
2492         clk_writel(PLLCX_MISC0_DEFAULT_VALUE, c->reg + c->u.pll.misc0);
2493         clk_writel(PLLCX_MISC1_DEFAULT_VALUE, c->reg + c->u.pll.misc1);
2494         clk_writel(PLLCX_MISC2_DEFAULT_VALUE, c->reg + c->u.pll.misc2);
2495         pll_writel_delay(PLLCX_MISC3_DEFAULT_VALUE, c->reg + c->u.pll.misc3);
2496 }
2497
2498 #if PLLCX_USE_DYN_RAMP
2499 static int pllcx_dyn_ramp(struct clk *c, struct clk_pll_freq_table *cfg)
2500 {
2501         u32 reg;
2502         struct clk_pll_controls *ctrl = c->u.pll.controls;
2503         struct clk_pll_div_layout *divs = c->u.pll.div_layout;
2504
2505         u32 val = clk_readl(c->reg);
2506         val &= ~divs->ndiv_mask;
2507         val |= cfg->n << divs->ndiv_shift;
2508         pll_writel_delay(val, c->reg);
2509
2510         reg = pll_reg_idx_to_addr(c, ctrl->lock_reg_idx);
2511         tegra_pll_clk_wait_for_lock(c, reg, ctrl->lock_mask);
2512
2513         return 0;
2514 }
2515 #else
2516 #define pllcx_dyn_ramp NULL
2517 #endif
2518
2519 static void tegra21_pllcx_clk_init(struct clk *c)
2520 {
2521         tegra_pll_clk_init(c);
2522 }
2523
2524 static struct clk_ops tegra_pllcx_ops = {
2525         .init                   = tegra21_pllcx_clk_init,
2526         .enable                 = tegra_pll_clk_enable,
2527         .disable                = tegra_pll_clk_disable,
2528         .set_rate               = tegra_pll_clk_set_rate,
2529 };
2530
2531 /*
2532  * PLLA
2533  * PLL with dynamic ramp and fractional SDM. Dynamic ramp is not used.
2534  * Fractional SDM is allowed to provide exact audio rates.
2535  */
2536 static void plla_set_defaults(struct clk *c, unsigned long input_rate)
2537 {
2538         u32 mask;
2539         u32 val = clk_readl(c->reg);
2540         c->u.pll.defaults_set = true;
2541
2542         if (val & c->u.pll.controls->enable_mask) {
2543                 /*
2544                  * PLL is ON: check if defaults already set, then set those
2545                  * that can be updated in flight.
2546                  */
2547                 if (val & PLLA_BASE_IDDQ) {
2548                         pr_warn("%s boot enabled with IDDQ set\n", c->name);
2549                         c->u.pll.defaults_set = false;
2550                 }
2551
2552                 val = PLLA_MISC0_DEFAULT_VALUE; /* ignore lock enable */
2553                 mask = PLLA_MISC0_LOCK_ENABLE | PLLA_MISC0_LOCK_OVERRIDE;
2554                 PLL_MISC_CHK_DEFAULT(c, 0, val, ~mask & PLLA_MISC0_WRITE_MASK);
2555
2556                 val = PLLA_MISC2_DEFAULT_VALUE; /* ignore all but control bit */
2557                 PLL_MISC_CHK_DEFAULT(c, 2, val, PLLA_MISC2_EN_DYNRAMP);
2558
2559                 /* Enable lock detect */
2560                 val = clk_readl(c->reg + c->u.pll.misc0);
2561                 val &= ~mask;
2562                 val |= PLLA_MISC0_DEFAULT_VALUE & mask;
2563                 pll_writel_delay(val, c->reg + c->u.pll.misc0);
2564
2565                 return;
2566         }
2567
2568         /* set IDDQ, enable lock detect, disable dynamic ramp and SDM */
2569         val |= PLLA_BASE_IDDQ;
2570         clk_writel(val, c->reg);
2571         clk_writel(PLLA_MISC0_DEFAULT_VALUE, c->reg + c->u.pll.misc0);
2572         pll_writel_delay(PLLA_MISC2_DEFAULT_VALUE, c->reg + c->u.pll.misc2);
2573 }
2574
2575 static void tegra21_plla_clk_init(struct clk *c)
2576 {
2577         tegra_pll_clk_init(c);
2578 }
2579
2580 static struct clk_ops tegra_plla_ops = {
2581         .init                   = tegra21_plla_clk_init,
2582         .enable                 = tegra_pll_clk_enable,
2583         .disable                = tegra_pll_clk_disable,
2584         .set_rate               = tegra_pll_clk_set_rate,
2585 };
2586
2587 /*
2588  * PLLD
2589  * PLL with fractional SDM.
2590  */
2591 static void plld_set_defaults(struct clk *c, unsigned long input_rate)
2592 {
2593         u32 val = clk_readl(c->reg);
2594         u32 mask = c->u.pll.div_layout->sdm_din_mask;
2595
2596         c->u.pll.defaults_set = true;
2597
2598         if (val & c->u.pll.controls->enable_mask) {
2599                 /*
2600                  * PLL is ON: check if defaults already set, then set those
2601                  * that can be updated in flight.
2602                  */
2603                 val = PLLD_MISC1_DEFAULT_VALUE;
2604                 PLL_MISC_CHK_DEFAULT(c, 1, val, PLLD_MISC1_WRITE_MASK);
2605
2606                 /* ignore lock, DSI and SDM controls, make sure IDDQ not set */
2607                 val = PLLD_MISC0_DEFAULT_VALUE & (~PLLD_MISC0_IDDQ);
2608                 mask |= PLLD_MISC0_DSI_CLKENABLE | PLLD_MISC0_LOCK_ENABLE |
2609                         PLLD_MISC0_LOCK_OVERRIDE | PLLD_MISC0_EN_SDM;
2610                 PLL_MISC_CHK_DEFAULT(c, 0, val, ~mask & PLLD_MISC0_WRITE_MASK);
2611
2612                 /* Enable lock detect */
2613                 mask = PLLD_MISC0_LOCK_ENABLE | PLLD_MISC0_LOCK_OVERRIDE;
2614                 val = clk_readl(c->reg + c->u.pll.misc0);
2615                 val &= ~mask;
2616                 val |= PLLD_MISC0_DEFAULT_VALUE & mask;
2617                 pll_writel_delay(val, c->reg + c->u.pll.misc0);
2618
2619                 return;
2620         }
2621
2622         /* set IDDQ, enable lock detect, disable SDM */
2623         val = clk_readl(c->reg + c->u.pll.misc0) & PLLD_MISC0_DSI_CLKENABLE;
2624         clk_writel(val | PLLD_MISC0_DEFAULT_VALUE, c->reg + c->u.pll.misc0);
2625         pll_writel_delay(PLLD_MISC1_DEFAULT_VALUE, c->reg + c->u.pll.misc1);
2626 }
2627
2628 static void tegra21_plld_clk_init(struct clk *c)
2629 {
2630         tegra_pll_clk_init(c);
2631 }
2632
2633 static int
2634 tegra21_plld_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
2635 {
2636         u32 val, mask, reg;
2637         u32 clear = 0;
2638
2639         switch (p) {
2640         case TEGRA_CLK_PLLD_CSI_OUT_ENB:
2641                 mask = PLLD_BASE_CSI_CLKSOURCE;
2642                 reg = c->reg;
2643                 break;
2644         case TEGRA_CLK_MIPI_CSI_OUT_ENB:
2645                 mask = 0;
2646                 clear = PLLD_BASE_CSI_CLKSOURCE;
2647                 reg = c->reg;
2648                 break;
2649         case TEGRA_CLK_PLLD_DSI_OUT_ENB:
2650                 mask = PLLD_MISC0_DSI_CLKENABLE;
2651                 reg = c->reg + c->u.pll.misc0;
2652                 break;
2653         case TEGRA_CLK_PLLD_MIPI_MUX_SEL:
2654                 mask = PLLD_BASE_DSI_MUX_MASK;
2655                 reg = c->reg;
2656                 break;
2657         default:
2658                 return -EINVAL;
2659         }
2660
2661         val = clk_readl(reg);
2662         if (setting) {
2663                 val |= mask;
2664                 val &= ~clear;
2665         } else
2666                 val &= ~mask;
2667         clk_writel(val, reg);
2668         return 0;
2669 }
2670
2671 static struct clk_ops tegra_plld_ops = {
2672         .init                   = tegra21_plld_clk_init,
2673         .enable                 = tegra_pll_clk_enable,
2674         .disable                = tegra_pll_clk_disable,
2675         .set_rate               = tegra_pll_clk_set_rate,
2676         .clk_cfg_ex             = tegra21_plld_clk_cfg_ex,
2677 };
2678
2679 /*
2680  * PLLD2, PLLDP
2681  * PLL with fractional SDM and Spread Spectrum (SDM is a must if SSC is used).
2682  */
2683 static void plldss_defaults(struct clk *c, u32 misc0_val, u32 misc1_val,
2684                             u32 misc2_val, u32 misc3_val)
2685 {
2686         u32 default_val;
2687         u32 val = clk_readl(c->reg);
2688         c->u.pll.defaults_set = true;
2689
2690         if (val & c->u.pll.controls->enable_mask) {
2691                 /*
2692                  * PLL is ON: check if defaults already set, then set those
2693                  * that can be updated in flight.
2694                  */
2695                 if (val & PLLDSS_BASE_IDDQ) {
2696                         pr_warn("%s boot enabled with IDDQ set\n", c->name);
2697                         c->u.pll.defaults_set = false;
2698                 }
2699
2700                 default_val = misc0_val;
2701                 PLL_MISC_CHK_DEFAULT(c, 0, default_val, /* ignore lock enable */
2702                                      PLLDSS_MISC0_WRITE_MASK &
2703                                      (~PLLDSS_MISC0_LOCK_ENABLE));
2704
2705                 /*
2706                  * If SSC is used, check all settings, otherwise just confirm
2707                  * that SSC is not used on boot as well. Do nothing when using
2708                  * this function for PLLC4 that has only MISC0.
2709                  */
2710                 if (c->u.pll.controls->ssc_en_mask) {
2711                         default_val = misc1_val;
2712                         PLL_MISC_CHK_DEFAULT(c, 1, default_val,
2713                                              PLLDSS_MISC1_CFG_WRITE_MASK);
2714                         default_val = misc2_val;
2715                         PLL_MISC_CHK_DEFAULT(c, 2, default_val,
2716                                              PLLDSS_MISC2_CTRL1_WRITE_MASK);
2717                         default_val = misc3_val;
2718                         PLL_MISC_CHK_DEFAULT(c, 3, default_val,
2719                                              PLLDSS_MISC3_CTRL2_WRITE_MASK);
2720                 } else if (c->u.pll.misc1) {
2721                         default_val = misc1_val;
2722                         PLL_MISC_CHK_DEFAULT(c, 1, default_val,
2723                                              PLLDSS_MISC1_CFG_WRITE_MASK &
2724                                              (~PLLDSS_MISC1_CFG_EN_SDM));
2725                 }
2726
2727                 /* Enable lock detect */
2728                 if (val & PLLDSS_BASE_LOCK_OVERRIDE) {
2729                         val &= ~PLLDSS_BASE_LOCK_OVERRIDE;
2730                         clk_writel(val, c->reg);
2731                 }
2732
2733                 val = clk_readl(c->reg + c->u.pll.misc0);
2734                 val &= ~PLLDSS_MISC0_LOCK_ENABLE;
2735                 val |= misc0_val & PLLDSS_MISC0_LOCK_ENABLE;
2736                 pll_writel_delay(val, c->reg + c->u.pll.misc0);
2737
2738                 return;
2739         }
2740
2741         /* set IDDQ, enable lock detect, configure SDM/SSC  */
2742         val |= PLLDSS_BASE_IDDQ;
2743         val &= ~PLLDSS_BASE_LOCK_OVERRIDE;
2744         clk_writel(val, c->reg);
2745
2746         /* When using this function for PLLC4 exit here */
2747         if (!c->u.pll.misc1) {
2748                 pll_writel_delay(misc0_val, c->reg + c->u.pll.misc0);
2749                 return;
2750         }
2751
2752         clk_writel(misc0_val, c->reg + c->u.pll.misc0);
2753         clk_writel(misc1_val & (~PLLDSS_MISC1_CFG_EN_SSC),
2754                    c->reg + c->u.pll.misc1); /* if SSC used set by 1st enable */
2755         clk_writel(misc2_val, c->reg + c->u.pll.misc2);
2756         pll_writel_delay(misc3_val, c->reg + c->u.pll.misc3);
2757 }
2758
2759 static void plldss_select_ref(struct clk *c)
2760 {
2761         u32 val = clk_readl(c->reg);
2762         u32 ref = (val & PLLDSS_BASE_REF_SEL_MASK) >> PLLDSS_BASE_REF_SEL_SHIFT;
2763
2764         /*
2765          * The only productized reference clock is tegra_pll_ref. Made sure
2766          * it is selected on boot. If pll is enabled, shut it down before
2767          * changing reference selection.
2768          */
2769         if (ref) {
2770                 if (val & c->u.pll.controls->enable_mask) {
2771                         WARN(1, "%s boot enabled with not supported ref %u\n",
2772                              c->name, ref);
2773                         val &= ~c->u.pll.controls->enable_mask;
2774                         pll_writel_delay(val, c->reg);
2775                 }
2776                 val &= ~PLLDSS_BASE_REF_SEL_MASK;
2777                 pll_writel_delay(val, c->reg);
2778         }
2779 }
2780
2781 static void plld2_set_defaults(struct clk *c, unsigned long input_rate)
2782 {
2783         plldss_defaults(c, PLLD2_MISC0_DEFAULT_VALUE,
2784                         PLLD2_MISC1_CFG_DEFAULT_VALUE,
2785                         PLLD2_MISC2_CTRL1_DEFAULT_VALUE,
2786                         PLLD2_MISC3_CTRL2_DEFAULT_VALUE);
2787 }
2788
2789 static void tegra21_plld2_clk_init(struct clk *c)
2790 {
2791         if (PLLD2_MISC1_CFG_DEFAULT_VALUE & PLLDSS_MISC1_CFG_EN_SSC) {
2792                 /* SSC requires SDM enabled */
2793                 BUILD_BUG_ON(!(PLLD2_MISC1_CFG_DEFAULT_VALUE &
2794                                PLLDSS_MISC1_CFG_EN_SDM));
2795         } else {
2796                 /* SSC should be disabled */
2797                 c->u.pll.controls->ssc_en_mask = 0;
2798         }
2799         plldss_select_ref(c);
2800
2801         tegra_pll_clk_init(c);
2802 }
2803
2804 static struct clk_ops tegra_plld2_ops = {
2805         .init                   = tegra21_plld2_clk_init,
2806         .enable                 = tegra_pll_clk_enable,
2807         .disable                = tegra_pll_clk_disable,
2808         .set_rate               = tegra_pll_clk_set_rate,
2809 };
2810
2811 static void plldp_set_defaults(struct clk *c, unsigned long input_rate)
2812 {
2813         plldss_defaults(c, PLLDP_MISC0_DEFAULT_VALUE,
2814                         PLLDP_MISC1_CFG_DEFAULT_VALUE,
2815                         PLLDP_MISC2_CTRL1_DEFAULT_VALUE,
2816                         PLLDP_MISC3_CTRL2_DEFAULT_VALUE);
2817 }
2818
2819 static void tegra21_plldp_clk_init(struct clk *c)
2820 {
2821
2822         if (PLLDP_MISC1_CFG_DEFAULT_VALUE & PLLDSS_MISC1_CFG_EN_SSC) {
2823                 /* SSC requires SDM enabled */
2824                 BUILD_BUG_ON(!(PLLDP_MISC1_CFG_DEFAULT_VALUE &
2825                                PLLDSS_MISC1_CFG_EN_SDM));
2826         } else {
2827                 /* SSC should be disabled */
2828                 c->u.pll.controls->ssc_en_mask = 0;
2829         }
2830         plldss_select_ref(c);
2831
2832         tegra_pll_clk_init(c);
2833 }
2834
2835 static struct clk_ops tegra_plldp_ops = {
2836         .init                   = tegra21_plldp_clk_init,
2837         .enable                 = tegra_pll_clk_enable,
2838         .disable                = tegra_pll_clk_disable,
2839         .set_rate               = tegra_pll_clk_set_rate,
2840 };
2841
2842 /*
2843  * PLLC4
2844  * Base and misc0 layout is the same as PLLD2/PLLDP, but no SDM/SSC support.
2845  * VCO is exposed to the clock tree via fixed 1/3 and 1/5 dividers.
2846  */
2847 static void pllc4_set_defaults(struct clk *c, unsigned long input_rate)
2848 {
2849         plldss_defaults(c, PLLC4_MISC0_DEFAULT_VALUE, 0, 0, 0);
2850 }
2851
2852 static void tegra21_pllc4_vco_init(struct clk *c)
2853 {
2854         unsigned long input_rate = clk_get_rate(c->parent);
2855         unsigned long cf = input_rate / pll_get_fixed_mdiv(c, input_rate);
2856         pllc4_set_fixed_rates(cf);
2857
2858         plldss_select_ref(c);
2859         tegra_pll_clk_init(c);
2860 }
2861
2862 static struct clk_ops tegra_pllc4_vco_ops = {
2863         .init                   = tegra21_pllc4_vco_init,
2864         .enable                 = tegra_pll_clk_enable,
2865         .disable                = tegra_pll_clk_disable,
2866         .set_rate               = tegra_pll_clk_set_rate,
2867 };
2868
2869 /*
2870  * PLLRE
2871  * VCO is exposed to the clock tree directly along with post-divider output
2872  */
2873 static void pllre_set_defaults(struct clk *c, unsigned long input_rate)
2874 {
2875         u32 mask;
2876         u32 val = clk_readl(c->reg);
2877         c->u.pll.defaults_set = true;
2878
2879         if (val & c->u.pll.controls->enable_mask) {
2880                 /*
2881                  * PLL is ON: check if defaults already set, then set those
2882                  * that can be updated in flight.
2883                  */
2884                 val &= PLLRE_BASE_DEFAULT_MASK;
2885                 if (val != PLLRE_BASE_DEFAULT_VALUE) {
2886                         pr_warn("%s boot base 0x%x : expected 0x%x\n",
2887                                 (c)->name, val, PLLRE_BASE_DEFAULT_VALUE);
2888                         pr_warn("(comparison mask = 0x%x)\n",
2889                                 PLLRE_BASE_DEFAULT_MASK);
2890                         c->u.pll.defaults_set = false;
2891                 }
2892
2893                 /* Ignore lock enable */
2894                 val = PLLRE_MISC0_DEFAULT_VALUE & (~PLLRE_MISC0_IDDQ);
2895                 mask = PLLRE_MISC0_LOCK_ENABLE | PLLRE_MISC0_LOCK_OVERRIDE;
2896                 PLL_MISC_CHK_DEFAULT(c, 0, val, ~mask & PLLRE_MISC0_WRITE_MASK);
2897
2898                 /* Enable lock detect */
2899                 val = clk_readl(c->reg + c->u.pll.misc0);
2900                 val &= ~mask;
2901                 val |= PLLRE_MISC0_DEFAULT_VALUE & mask;
2902                 pll_writel_delay(val, c->reg + c->u.pll.misc0);
2903
2904                 return;
2905         }
2906
2907         /* set IDDQ, enable lock detect */
2908         val &= ~PLLRE_BASE_DEFAULT_MASK;
2909         val |= PLLRE_BASE_DEFAULT_VALUE & PLLRE_BASE_DEFAULT_MASK;
2910         clk_writel(val, c->reg);
2911         pll_writel_delay(PLLRE_MISC0_DEFAULT_VALUE, c->reg + c->u.pll.misc0);
2912 }
2913
2914 static void tegra21_pllre_vco_init(struct clk *c)
2915 {
2916         tegra_pll_clk_init(c);
2917 }
2918
2919 static struct clk_ops tegra_pllre_vco_ops = {
2920         .init                   = tegra21_pllre_vco_init,
2921         .enable                 = tegra_pll_clk_enable,
2922         .disable                = tegra_pll_clk_disable,
2923         .set_rate               = tegra_pll_clk_set_rate,
2924 };
2925
2926 /*
2927  * PLLU
2928  * VCO is exposed to the clock tree directly along with post-divider output.
2929  * Both VCO and post-divider output rates are fixed at 480MHz and 240MHz,
2930  * respectively.
2931  */
2932 static void pllu_check_defaults(struct clk *c, bool hw_control)
2933 {
2934         u32 val, mask;
2935
2936         /* Ignore lock enable (will be set) and IDDQ if under h/w control */
2937         val = PLLU_MISC0_DEFAULT_VALUE & (~PLLU_MISC0_IDDQ);
2938         mask = PLLU_MISC0_LOCK_ENABLE | (hw_control ? PLLU_MISC0_IDDQ : 0);
2939         PLL_MISC_CHK_DEFAULT(c, 0, val, ~mask & PLLU_MISC0_WRITE_MASK);
2940
2941         val = PLLU_MISC1_DEFAULT_VALUE;
2942         mask = PLLU_MISC1_LOCK_OVERRIDE;
2943         PLL_MISC_CHK_DEFAULT(c, 1, val, ~mask & PLLU_MISC1_WRITE_MASK);
2944 }
2945
2946 static void pllu_set_defaults(struct clk *c, unsigned long input_rate)
2947 {
2948         u32 val = clk_readl(c->reg);
2949         c->u.pll.defaults_set = true;
2950
2951         if (val & c->u.pll.controls->enable_mask) {
2952                 /*
2953                  * PLL is ON: check if defaults already set, then set those
2954                  * that can be updated in flight.
2955                  */
2956                 pllu_check_defaults(c, false);
2957
2958                 /* Enable lock detect */
2959                 val = clk_readl(c->reg + c->u.pll.misc0);
2960                 val &= ~PLLU_MISC0_LOCK_ENABLE;
2961                 val |= PLLU_MISC0_DEFAULT_VALUE & PLLU_MISC0_LOCK_ENABLE;
2962                 pll_writel_delay(val, c->reg + c->u.pll.misc0);
2963
2964                 val = clk_readl(c->reg + c->u.pll.misc1);
2965                 val &= ~PLLU_MISC1_LOCK_OVERRIDE;
2966                 val |= PLLU_MISC1_DEFAULT_VALUE & PLLU_MISC1_LOCK_OVERRIDE;
2967                 pll_writel_delay(val, c->reg + c->u.pll.misc1);
2968
2969                 return;
2970         }
2971
2972         /* set IDDQ, enable lock detect */
2973         clk_writel(PLLU_MISC0_DEFAULT_VALUE, c->reg + c->u.pll.misc0);
2974         pll_writel_delay(PLLU_MISC1_DEFAULT_VALUE, c->reg + c->u.pll.misc1);
2975 }
2976
2977 static void tegra21_pllu_vco_init(struct clk *c)
2978 {
2979         u32 val = clk_readl(c->reg);
2980         pll_u = c;
2981
2982         /*
2983          * If PLLU state is already under h/w control just check defaults, and
2984          * verify expected fixed VCO rate (pll dividers can still be read from
2985          * the base register).
2986          */
2987         if (!(val & PLLU_BASE_OVERRIDE)) {
2988                 struct clk_pll_freq_table cfg = { };
2989                 c->state = (val & c->u.pll.controls->enable_mask) ? ON : OFF;
2990
2991                 c->u.pll.defaults_set = true;
2992                 pllu_check_defaults(c, true);
2993                 pll_base_parse_cfg(c, &cfg);
2994                 pll_clk_set_gain(c, &cfg);
2995
2996                 pll_clk_verify_fixed_rate(c);
2997                 pr_info("%s: boot with h/w control already set\n", c->name);
2998                 return;
2999         }
3000
3001         /* S/w controlled initialization */
3002         tegra_pll_clk_init(c);
3003 }
3004
3005 static void tegra21_pllu_hw_ctrl_set(struct clk *c)
3006 {
3007         u32 val = clk_readl(c->reg);
3008
3009         /* Put PLLU under h/w control (if not already) */
3010         if (val & PLLU_BASE_OVERRIDE) {
3011                 val &= ~PLLU_BASE_OVERRIDE;
3012                 pll_writel_delay(val, c->reg);
3013
3014                 val = clk_readl(PLLU_HW_PWRDN_CFG0);
3015                 val |= PLLU_HW_PWRDN_CFG0_IDDQ_PD_INCLUDE |
3016                         PLLU_HW_PWRDN_CFG0_USE_SWITCH_DETECT |
3017                         PLLU_HW_PWRDN_CFG0_USE_LOCKDET;
3018                 val &= ~(PLLU_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL |
3019                          PLLU_HW_PWRDN_CFG0_CLK_SWITCH_SWCTL);
3020                 clk_writel(val, PLLU_HW_PWRDN_CFG0);
3021
3022                 val = clk_readl(XUSB_PLL_CFG0);
3023                 val &= ~XUSB_PLL_CFG0_PLLU_LOCK_DLY_MASK;
3024                 pll_writel_delay(val, XUSB_PLL_CFG0);
3025
3026                 val = clk_readl(PLLU_HW_PWRDN_CFG0);
3027                 val |= PLLU_HW_PWRDN_CFG0_SEQ_ENABLE;
3028                 pll_writel_delay(val, PLLU_HW_PWRDN_CFG0);
3029         }
3030
3031         /* Disable clock branch to UTMIP PLL (using OSC directly) */
3032         val = clk_readl(c->reg);
3033         val &= ~PLLU_BASE_CLKENABLE_USB;
3034         clk_writel(val, c->reg);
3035
3036         /* Put UTMIP PLL under h/w control  (if not already) */
3037         val = clk_readl(UTMIPLL_HW_PWRDN_CFG0);
3038         if (!(val & UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE))
3039                 tegra21_utmi_param_configure(c);
3040 }
3041
3042 static int tegra21_pllu_clk_enable(struct clk *c)
3043 {
3044         int ret = tegra_pll_clk_enable(c);
3045         if (ret)
3046                 return ret;
3047
3048         tegra21_pllu_hw_ctrl_set(c);
3049         return 0;
3050 }
3051
3052 static struct clk_ops tegra_pllu_vco_ops = {
3053         .init                   = tegra21_pllu_vco_init,
3054         .enable                 = tegra21_pllu_clk_enable,
3055         .disable                = tegra_pll_clk_disable,
3056         .set_rate               = tegra_pll_clk_set_rate,
3057 };
3058
3059 /*
3060  * PLLP
3061  * VCO is exposed to the clock tree directly along with post-divider output.
3062  * Both VCO and post-divider output rates are fixed at 408MHz and 204MHz,
3063  * respectively.
3064  */
3065 static void pllp_check_defaults(struct clk *c, bool enabled)
3066 {
3067         u32 val, mask;
3068
3069         /* Ignore lock enable (will be set), make sure not in IDDQ if enabled */
3070         val = PLLP_MISC0_DEFAULT_VALUE & (~PLLP_MISC0_IDDQ);
3071         mask = PLLP_MISC0_LOCK_ENABLE | PLLP_MISC0_LOCK_OVERRIDE;
3072         if (!enabled)
3073                 mask |= PLLP_MISC0_IDDQ;
3074         PLL_MISC_CHK_DEFAULT(c, 0, val, ~mask & PLLP_MISC0_WRITE_MASK);
3075
3076         /* Ignore branch controls */
3077         val = PLLP_MISC1_DEFAULT_VALUE;
3078         mask = PLLP_MISC1_HSIO_EN | PLLP_MISC1_XUSB_EN;
3079         PLL_MISC_CHK_DEFAULT(c, 1, val, ~mask & PLLP_MISC1_WRITE_MASK);
3080 }
3081
3082 static void pllp_set_defaults(struct clk *c, unsigned long input_rate)
3083 {
3084         u32 mask;
3085         u32 val = clk_readl(c->reg);
3086         c->u.pll.defaults_set = true;
3087
3088         if (val & c->u.pll.controls->enable_mask) {
3089                 /*
3090                  * PLL is ON: check if defaults already set, then set those
3091                  * that can be updated in flight.
3092                  */
3093                 pllp_check_defaults(c, true);
3094
3095                 /* Enable lock detect */
3096                 val = clk_readl(c->reg + c->u.pll.misc0);
3097                 mask = PLLP_MISC0_LOCK_ENABLE | PLLP_MISC0_LOCK_OVERRIDE;
3098                 val &= ~mask;
3099                 val |= PLLP_MISC0_DEFAULT_VALUE & mask;
3100                 pll_writel_delay(val, c->reg + c->u.pll.misc0);
3101
3102                 return;
3103         }
3104
3105         /* set IDDQ, enable lock detect */
3106         clk_writel(PLLP_MISC0_DEFAULT_VALUE, c->reg + c->u.pll.misc0);
3107
3108         /* Preserve branch control */
3109         val = clk_readl(c->reg + c->u.pll.misc1);
3110         mask = PLLP_MISC1_HSIO_EN | PLLP_MISC1_XUSB_EN;
3111         val &= mask;
3112         val |= ~mask & PLLP_MISC1_DEFAULT_VALUE;
3113         pll_writel_delay(val, c->reg + c->u.pll.misc1);
3114 }
3115
3116 static void pllp_vco_init(struct clk *c)
3117 {
3118         u32 val = clk_readl(c->reg);
3119
3120         /*
3121          * If PLLP state is already under h/w control just check defaults, and
3122          * set expected fixed VCO rate (pll dividers cannot be read in this case
3123          * from the base register).
3124          */
3125         if (!(val & PLLP_BASE_OVERRIDE)) {
3126                 const struct clk_pll_freq_table *sel;
3127                 unsigned long input_rate = clk_get_rate(c->parent);
3128                 c->state = (val & c->u.pll.controls->enable_mask) ? ON : OFF;
3129
3130                 c->u.pll.defaults_set = true;
3131                 pllp_check_defaults(c, c->state == ON);
3132
3133                 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
3134                         if (sel->input_rate == input_rate &&
3135                                 sel->output_rate == c->u.pll.fixed_rate) {
3136                                 c->mul = sel->n;
3137                                 c->div = sel->m;
3138                                 return;
3139                         }
3140                 }
3141                 WARN(1, "%s: unexpected fixed rate %lu\n",
3142                      c->name, c->u.pll.fixed_rate);
3143
3144                 c->mul = c->u.pll.fixed_rate / 100000;
3145                 c->div = input_rate / 100000;
3146                 pll_clk_verify_fixed_rate(c);
3147                 return;
3148         }
3149
3150         /* S/w controlled initialization */
3151         tegra_pll_clk_init(c);
3152 }
3153
3154 static void tegra21_pllp_vco_init(struct clk *c)
3155 {
3156         pllp_vco_init(c);
3157         tegra21_pllp_init_dependencies(c->u.pll.fixed_rate);
3158 }
3159
3160 static struct clk_ops tegra_pllp_vco_ops = {
3161         .init                   = tegra21_pllp_vco_init,
3162         .enable                 = tegra_pll_clk_enable,
3163         .disable                = tegra_pll_clk_disable,
3164         .set_rate               = tegra_pll_clk_set_rate,
3165 };
3166
3167 /*
3168  * PLLX
3169  * PLL with dynamic ramp and fractional SDM. Dynamic ramp is allowed for any
3170  * transition that changes NDIV only, while PLL is already locked. SDM is not
3171  * used and always disabled.
3172  */
3173 static void pllx_get_dyn_steps(struct clk *c, unsigned long input_rate,
3174                                 u32 *step_a, u32 *step_b)
3175 {
3176         input_rate /= pll_get_fixed_mdiv(c, input_rate); /* cf rate */
3177         switch (input_rate) {
3178         case 12000000:
3179         case 12800000:
3180         case 13000000:
3181                 *step_a = 0x2B;
3182                 *step_b = 0x0B;
3183                 return;
3184         case 19200000:
3185                 *step_a = 0x12;
3186                 *step_b = 0x08;
3187                 return;
3188         case 38400000:
3189                 *step_a = 0x04;
3190                 *step_b = 0x05;
3191                 return;
3192         default:
3193                 pr_err("%s: Unexpected reference rate %lu\n",
3194                         __func__, input_rate);
3195                 BUG();
3196         }
3197 }
3198
3199 static void pllx_check_defaults(struct clk *c, unsigned long input_rate)
3200 {
3201         u32 default_val;
3202
3203         default_val = PLLX_MISC0_DEFAULT_VALUE;
3204         PLL_MISC_CHK_DEFAULT(c, 0, default_val, /* ignore lock enable */
3205                              PLLX_MISC0_WRITE_MASK & (~PLLX_MISC0_LOCK_ENABLE));
3206
3207         default_val = PLLX_MISC1_DEFAULT_VALUE;
3208         PLL_MISC_CHK_DEFAULT(c, 1, default_val, PLLX_MISC1_WRITE_MASK);
3209
3210         default_val = PLLX_MISC2_DEFAULT_VALUE; /* ignore all but control bit */
3211         PLL_MISC_CHK_DEFAULT(c, 2, default_val, PLLX_MISC2_EN_DYNRAMP);
3212
3213         default_val = PLLX_MISC3_DEFAULT_VALUE & (~PLLX_MISC3_IDDQ);
3214         PLL_MISC_CHK_DEFAULT(c, 3, default_val, PLLX_MISC3_WRITE_MASK);
3215
3216         default_val = PLLX_MISC4_DEFAULT_VALUE;
3217         PLL_MISC_CHK_DEFAULT(c, 4, default_val, PLLX_MISC4_WRITE_MASK);
3218
3219         default_val = PLLX_MISC5_DEFAULT_VALUE;
3220         PLL_MISC_CHK_DEFAULT(c, 5, default_val, PLLX_MISC5_WRITE_MASK);
3221 }
3222
3223 static void pllx_set_defaults(struct clk *c, unsigned long input_rate)
3224 {
3225         u32 val;
3226         u32 step_a, step_b;
3227
3228         c->u.pll.defaults_set = true;
3229
3230         /* Get ready dyn ramp state machine settings */
3231         pllx_get_dyn_steps(c, input_rate, &step_a, &step_b);
3232         val = PLLX_MISC2_DEFAULT_VALUE & (~PLLX_MISC2_DYNRAMP_STEPA_MASK) &
3233                 (~PLLX_MISC2_DYNRAMP_STEPB_MASK);
3234         val |= step_a << PLLX_MISC2_DYNRAMP_STEPA_SHIFT;
3235         val |= step_b << PLLX_MISC2_DYNRAMP_STEPB_SHIFT;
3236
3237         if (clk_readl(c->reg) & c->u.pll.controls->enable_mask) {
3238                 /*
3239                  * PLL is ON: check if defaults already set, then set those
3240                  * that can be updated in flight.
3241                  */
3242                 pllx_check_defaults(c, input_rate);
3243
3244                 /* Configure dyn ramp, disable lock override */
3245                 clk_writel(val, c->reg + c->u.pll.misc2);
3246
3247                 /* Enable lock detect */
3248                 val = clk_readl(c->reg + c->u.pll.misc0);
3249                 val &= ~PLLX_MISC0_LOCK_ENABLE;
3250                 val |= PLLX_MISC0_DEFAULT_VALUE & PLLX_MISC0_LOCK_ENABLE;
3251                 pll_writel_delay(val, c->reg + c->u.pll.misc0);
3252
3253                 return;
3254         }
3255
3256         /* Enable lock detect and CPU output */
3257         clk_writel(PLLX_MISC0_DEFAULT_VALUE, c->reg + c->u.pll.misc0);
3258
3259         /* Setup */
3260         clk_writel(PLLX_MISC1_DEFAULT_VALUE, c->reg + c->u.pll.misc1);
3261
3262         /* Configure dyn ramp state machine, disable lock override */
3263         clk_writel(val, c->reg + c->u.pll.misc2);
3264
3265         /* Set IDDQ */
3266         clk_writel(PLLX_MISC3_DEFAULT_VALUE, c->reg + c->u.pll.misc3);
3267
3268         /* Disable SDM */
3269         clk_writel(PLLX_MISC4_DEFAULT_VALUE, c->reg + c->u.pll.misc4);
3270         pll_writel_delay(PLLX_MISC5_DEFAULT_VALUE, c->reg + c->u.pll.misc5);
3271 }
3272
3273 #if PLLX_USE_DYN_RAMP
3274 static int pllx_dyn_ramp(struct clk *c, struct clk_pll_freq_table *cfg)
3275 {
3276         u32 reg, val, base, ndiv_new_mask;
3277         struct clk_pll_controls *ctrl = c->u.pll.controls;
3278         struct clk_pll_div_layout *divs = c->u.pll.div_layout;
3279
3280         reg = pll_reg_idx_to_addr(c, divs->ndiv_new_reg_idx);
3281         ndiv_new_mask = (divs->ndiv_mask >> divs->ndiv_shift) <<
3282                 divs->ndiv_new_shift;
3283         val = clk_readl(reg) & (~ndiv_new_mask);
3284         val |= cfg->n << divs->ndiv_new_shift;
3285         pll_writel_delay(val, reg);
3286
3287         reg = pll_reg_idx_to_addr(c, ctrl->dramp_ctrl_reg_idx);
3288         val = clk_readl(reg);
3289         val |= ctrl->dramp_en_mask;
3290         pll_writel_delay(val, reg);
3291         tegra_pll_clk_wait_for_lock(c, reg, ctrl->dramp_done_mask);
3292
3293         base = clk_readl(c->reg) & (~divs->ndiv_mask);
3294         base |= cfg->n << divs->ndiv_shift;
3295         pll_writel_delay(base, c->reg);
3296
3297         val &= ~ctrl->dramp_en_mask;
3298         pll_writel_delay(val, reg);
3299
3300         pr_debug("%s: dynamic ramp to m = %u n = %u p = %u, Fout = %lu kHz\n",
3301                  c->name, cfg->m, cfg->n, cfg->p,
3302                  cfg->input_rate / cfg->m * cfg->n / cfg->p / 1000);
3303
3304         return 0;
3305 }
3306 #endif
3307
3308 static void tegra21_pllx_clk_init(struct clk *c)
3309 {
3310         /* Only s/w dyn ramp control is supported */
3311         u32 val = clk_readl(PLLX_HW_CTRL_CFG);
3312         BUG_ON(!(val & PLLX_HW_CTRL_CFG_SWCTRL) && !tegra_platform_is_linsim());
3313
3314         tegra_pll_clk_init(c);
3315 }
3316
3317 static struct clk_ops tegra_pllx_ops = {
3318         .init                   = tegra21_pllx_clk_init,
3319         .enable                 = tegra_pll_clk_enable,
3320         .disable                = tegra_pll_clk_disable,
3321         .set_rate               = tegra_pll_clk_set_rate,
3322 };
3323
3324 /*
3325  * Dual PLLM/PLLMB
3326  *
3327  * Identical PLLs used exclusively as memory clock source. Configuration
3328  * and control fields in CAR module are separate for both PLL, and can
3329  * be set independently. Most control fields (ENABLE, IDDQ, M/N/P dividers)
3330  * have override counterparts in PMC module, however there is only one set
3331  * of registers that applied either to PLLM or PLLMB depending on override
3332  * VCO selection.
3333  *
3334  * By default selection between PLLM and PLLMB outputs is controlled by EMC
3335  * clock switch h/w state machine. If any s/w override either in CAR, or in
3336  * PMC is enabled on boot, PLLMB is no longer can be used as EMC parent.
3337  *
3338  * Only PLLM is used as boot PLL, setup by boot-rom.
3339  */
3340 static void pllm_check_defaults(struct clk *c)
3341 {
3342         u32 val;
3343         c->u.pll.defaults_set = true;
3344
3345         /*
3346          * PLLM is setup on boot/suspend exit by boot-rom.
3347          * Just enable lock, and check default configuration:
3348          * - PLLM syncmux is under h/w control
3349          * - PMC override is disabled
3350          */
3351         val = clk_readl(c->reg + c->u.pll.misc0);
3352         val &= ~PLLM_MISC0_LOCK_OVERRIDE;
3353         val |= PLLM_MISC0_LOCK_ENABLE;
3354         pll_writel_delay(val, c->reg + c->u.pll.misc0);
3355
3356         /* FIXME: no support for invalid configurations; replace with BUG() */
3357         if (val & PLLM_MISC0_SYNCMUX_CTRL_MASK) {
3358                 WARN(1, "%s: Unsupported config: PLLM syncmux s/w control\n",
3359                      __func__);
3360                 c->u.pll.defaults_set = false;
3361         }
3362
3363         val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3364         if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) {
3365                 WARN(1, "%s: Unsupported config: PMC override enabled\n",
3366                      __func__);
3367                 c->u.pll.defaults_set = false;
3368
3369                 /* No boot on PLLMB */
3370                 BUG_ON(val & PMC_PLLP_WB0_OVERRIDE_PLLM_SEL_VCO);
3371         }
3372 }
3373
3374 static void tegra21_pllm_clk_init(struct clk *c)
3375 {
3376         u32 val;
3377         unsigned long input_rate = clk_get_rate(c->parent);
3378         unsigned long cf = input_rate / pll_get_fixed_mdiv(c, input_rate);
3379
3380         struct clk_pll_freq_table cfg = { };
3381         struct clk_pll_controls *ctrl = c->u.pll.controls;
3382         struct clk_pll_div_layout *divs = c->u.pll.div_layout;
3383         BUG_ON(!ctrl || !divs);
3384
3385         /* clip vco_min to exact multiple of comparison rate */
3386         c->u.pll.vco_min = DIV_ROUND_UP(c->u.pll.vco_min, cf) * cf;
3387         c->min_rate = DIV_ROUND_UP(c->u.pll.vco_min,
3388                                    divs->pdiv_to_p[divs->pdiv_max]);
3389
3390         /* Check PLLM setup */
3391         pllm_check_defaults(c);
3392
3393         val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3394         if ((val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) &&
3395             (~val & PMC_PLLP_WB0_OVERRIDE_PLLM_SEL_VCO)) {
3396                 c->state = (val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE) ? ON : OFF;
3397                 if (c->state == ON) {
3398                         /* PLLM boot control from PMC + enabled: record rate */
3399                         val = pmc_readl(PMC_PLLM_WB0_OVERRIDE);
3400                         cfg.m = (val & divs->mdiv_mask) >> divs->mdiv_shift;
3401                         cfg.n = (val & divs->ndiv_mask) >> divs->ndiv_shift;
3402
3403                         val = pmc_readl(PMC_PLLM_WB0_OVERRIDE_2);
3404                         cfg.p = (val & PMC_PLLM_WB0_OVERRIDE_2_DIVP_MASK) >>
3405                                 PMC_PLLM_WB0_OVERRIDE_2_DIVP_SHIFT;
3406                         cfg.p = divs->pdiv_to_p[cfg.p];
3407                         pll_clk_set_gain(c, &cfg);
3408                         return;
3409                 }
3410         } else {
3411                 val = clk_readl(c->reg);
3412                 c->state = (val & ctrl->enable_mask) ? ON : OFF;
3413                 if (c->state == ON) {
3414                         /* PLLM boot control from CAR + enabled: record rate */
3415                         pll_base_parse_cfg(c, &cfg);
3416                         pll_clk_set_gain(c, &cfg);
3417                         return;
3418                 }
3419         }
3420
3421         /* PLLM is disabled on boot: set rate close to to 1/4 of minimum VCO */
3422         c->ops->set_rate(c, c->u.pll.vco_min / 4);
3423 }
3424
3425 static int tegra21_pllm_clk_enable(struct clk *c)
3426 {
3427         u32 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3428
3429         if ((val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) &&
3430             (~val & PMC_PLLP_WB0_OVERRIDE_PLLM_SEL_VCO)) {
3431                 pr_debug("%s on clock %s\n", __func__, c->name);
3432
3433                 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_IDDQ;
3434                 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
3435                 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3436                 udelay(5); /* out of IDDQ delay to 5us */
3437
3438                 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
3439                 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
3440                 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3441
3442                 tegra_pll_clk_wait_for_lock(c, c->reg, PLLM_BASE_LOCK);
3443                 return 0;
3444         }
3445         return tegra_pll_clk_enable(c);
3446 }
3447
3448 static void tegra21_pllm_clk_disable(struct clk *c)
3449 {
3450         u32 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3451
3452         if ((val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) &&
3453             (~val & PMC_PLLP_WB0_OVERRIDE_PLLM_SEL_VCO)) {
3454                 pr_debug("%s on clock %s\n", __func__, c->name);
3455
3456                 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
3457                 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
3458                 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3459                 udelay(1);
3460
3461                 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_IDDQ;
3462                 pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
3463                 val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3464                 udelay(1);
3465                 return;
3466         }
3467         tegra_pll_clk_disable(c);
3468 }
3469
3470 static int tegra21_pllm_clk_set_rate(struct clk *c, unsigned long rate)
3471 {
3472         u32 val, pdiv;
3473         struct clk_pll_freq_table cfg = { };
3474         unsigned long input_rate = clk_get_rate(c->parent);
3475
3476         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
3477
3478         if (c->state == ON) {
3479                 if (rate != clk_get_rate_locked(c)) {
3480                         pr_err("%s: Can not change memory %s rate in flight\n",
3481                                __func__, c->name);
3482                         return -EINVAL;
3483                 }
3484                 return 0;
3485         }
3486
3487         if (pll_clk_find_cfg(c, &cfg, rate, input_rate, &pdiv))
3488                 return -EINVAL;
3489
3490         pll_clk_set_gain(c, &cfg);
3491
3492         val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3493         if ((val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) &&
3494             (~val & PMC_PLLP_WB0_OVERRIDE_PLLM_SEL_VCO)) {
3495                 struct clk_pll_div_layout *divs = c->u.pll.div_layout;
3496
3497                 val = pmc_readl(PMC_PLLM_WB0_OVERRIDE_2);
3498                 val &= ~PMC_PLLM_WB0_OVERRIDE_2_DIVP_MASK;
3499                 val |= pdiv << PMC_PLLM_WB0_OVERRIDE_2_DIVP_SHIFT;
3500                 pmc_writel(val, PMC_PLLM_WB0_OVERRIDE_2);
3501
3502                 val = pmc_readl(PMC_PLLM_WB0_OVERRIDE);
3503                 val &= ~(divs->mdiv_mask | divs->ndiv_mask);
3504                 val |= cfg.m << divs->mdiv_shift | cfg.n << divs->ndiv_shift;
3505                 pmc_writel(val, PMC_PLLM_WB0_OVERRIDE);
3506                 val = pmc_readl(PMC_PLLM_WB0_OVERRIDE);
3507                 udelay(1);
3508         } else {
3509                 val = clk_readl(c->reg);
3510                 pll_base_set_div(c, cfg.m, cfg.n, pdiv, val);
3511         }
3512
3513         return 0;
3514 }
3515
3516 static struct clk_ops tegra_pllm_ops = {
3517         .init                   = tegra21_pllm_clk_init,
3518         .enable                 = tegra21_pllm_clk_enable,
3519         .disable                = tegra21_pllm_clk_disable,
3520         .set_rate               = tegra21_pllm_clk_set_rate,
3521 };
3522
3523 /* PLLMB */
3524 static void pllmb_set_defaults(struct clk *c, unsigned long input_rate)
3525 {
3526         u32 mask, val = clk_readl(c->reg);
3527         c->u.pll.defaults_set = true;
3528
3529         if (val & c->u.pll.controls->enable_mask) {
3530                 /*
3531                  * PLL is ON: check if defaults already set, then set those
3532                  * that can be updated in flight.
3533                  */
3534                 val = PLLMB_MISC0_DEFAULT_VALUE & (~PLLMB_MISC0_IDDQ);
3535                 mask = PLLMB_MISC0_LOCK_ENABLE | PLLMB_MISC0_LOCK_OVERRIDE;
3536                 PLL_MISC_CHK_DEFAULT(c, 0, val, ~mask & PLLMB_MISC0_WRITE_MASK);
3537
3538                 /* Enable lock detect */
3539                 val = clk_readl(c->reg + c->u.pll.misc0);
3540                 val &= ~mask;
3541                 val |= PLLMB_MISC0_DEFAULT_VALUE & mask;
3542                 pll_writel_delay(val, c->reg + c->u.pll.misc0);
3543
3544                 return;
3545         }
3546
3547         /* set IDDQ, enable lock detect */
3548         pll_writel_delay(PLLMB_MISC0_DEFAULT_VALUE, c->reg + c->u.pll.misc0);
3549 }
3550
3551 static int tegra21_pllmb_clk_set_rate(struct clk *c, unsigned long rate)
3552 {
3553         if (c->state == ON) {
3554                 if (rate != clk_get_rate_locked(c)) {
3555                         pr_err("%s: Can not change memory %s rate in flight\n",
3556                                __func__, c->name);
3557                         return -EINVAL;
3558                 }
3559                 return 0;
3560         }
3561         return tegra_pll_clk_set_rate(c, rate);
3562 }
3563
3564 static void tegra21_pllmb_clk_init(struct clk *c)
3565 {
3566         tegra_pll_clk_init(c);
3567 }
3568
3569 static struct clk_ops tegra_pllmb_ops = {
3570         .init                   = tegra21_pllmb_clk_init,
3571         .enable                 = tegra_pll_clk_enable,
3572         .disable                = tegra_pll_clk_disable,
3573         .set_rate               = tegra21_pllmb_clk_set_rate,
3574 };
3575
3576 /*
3577  * PLLE
3578  * Analog interpolator based SS PLL (with optional SDM SS - not used).
3579  */
3580 static void select_pll_e_input(struct clk *c)
3581 {
3582         struct clk *p;
3583         u32 aux_reg = clk_readl(PLLE_AUX);
3584
3585 #if USE_PLLE_INPUT_PLLRE
3586         aux_reg |= PLLE_AUX_PLLRE_SEL;
3587         p = c->inputs[2].input;
3588 #else
3589         aux_reg &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
3590         p = c->inputs[0].input;
3591 #endif
3592         if (p != c->parent)
3593                 tegra_clk_prepare_enable(p);
3594
3595         pll_writel_delay(aux_reg, PLLE_AUX);
3596
3597         if (p != c->parent) {
3598                 tegra_clk_disable_unprepare(c->parent);
3599                 clk_reparent(c, p);
3600         }
3601 }
3602
3603 static void tegra21_plle_clk_init(struct clk *c)
3604 {
3605         u32 val, p;
3606         struct clk *pll_ref = tegra_get_clock_by_name("pll_ref");
3607         struct clk *re_vco = tegra_get_clock_by_name("pll_re_vco");
3608         struct clk *pllp = tegra_get_clock_by_name("pllp");
3609 #if USE_PLLE_INPUT_PLLRE
3610         struct clk *ref = re_vco;
3611 #else
3612         struct clk *ref = pll_ref;
3613 #endif
3614
3615         val = clk_readl(c->reg);
3616         c->state = (val & PLLE_BASE_ENABLE) ? ON : OFF;
3617
3618         c->mul = (val & PLLE_BASE_DIVN_MASK) >> PLLE_BASE_DIVN_SHIFT;
3619         c->div = (val & PLLE_BASE_DIVM_MASK) >> PLLE_BASE_DIVM_SHIFT;
3620         p = (val & PLLE_BASE_DIVCML_MASK) >> PLLE_BASE_DIVCML_SHIFT;
3621         c->div *= pll_qlin_pdiv_to_p[p];
3622
3623         val = clk_readl(PLLE_AUX);
3624         c->parent = (val & PLLE_AUX_PLLRE_SEL) ? re_vco :
3625                 (val & PLLE_AUX_PLLP_SEL) ? pllp : pll_ref;
3626         if (c->parent != ref) {
3627                 if (c->state == ON) {
3628                         WARN(1, "%s: pll_e is left enabled with %s input\n",
3629                              __func__, c->parent->name);
3630                 }
3631         }
3632 }
3633
3634 static void tegra21_plle_clk_disable(struct clk *c)
3635 {
3636         u32 val;
3637         pr_debug("%s on clock %s\n", __func__, c->name);
3638
3639         /* FIXME: do we need to restore other s/w controls ? */
3640         val = clk_readl(c->reg);
3641         val &= ~PLLE_BASE_ENABLE;
3642         clk_writel(val, c->reg);
3643
3644         val = clk_readl(c->reg + c->u.pll.misc0);
3645         val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
3646         pll_writel_delay(val, c->reg + c->u.pll.misc0);
3647 }
3648
3649 static int tegra21_plle_clk_enable(struct clk *c)
3650 {
3651         int p;
3652         u32 val, pdiv;
3653         const struct clk_pll_freq_table *sel;
3654         unsigned long rate = c->u.pll.fixed_rate;
3655         unsigned long input_rate = clk_get_rate(c->parent);
3656
3657         if (c->state == ON) {
3658                 /* BL left plle enabled - don't change configuartion */
3659                 pr_warn("%s: pll_e is already enabled\n", __func__);
3660                 return 0;
3661         }
3662
3663         /* Fixed per prod settings input */
3664         select_pll_e_input(c);
3665         input_rate = clk_get_rate(c->parent);
3666
3667         /* PLLE config must be tabulated */
3668         for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
3669                 if (sel->input_rate == input_rate && sel->output_rate == rate)
3670                         break;
3671         }
3672
3673         if (sel->input_rate == 0) {
3674                 pr_err("%s: %s input rate %lu is out-of-table\n",
3675                        __func__, c->name, input_rate);
3676                 return -EINVAL;
3677         }
3678
3679         p = c->u.pll.round_p_to_pdiv(sel->p, &pdiv);
3680         BUG_ON(IS_ERR_VALUE(p) || (p != sel->p));
3681
3682         /* setup locking configuration, s/w control of IDDQ and enable modes,
3683            take pll out of IDDQ via s/w control, setup VREG */
3684         val = clk_readl(c->reg);
3685         val &= ~PLLE_BASE_LOCK_OVERRIDE;
3686         clk_writel(val, c->reg);
3687
3688         val = clk_readl(c->reg + c->u.pll.misc0);
3689         val |= PLLE_MISC_LOCK_ENABLE;
3690         val |= PLLE_MISC_IDDQ_SW_CTRL;
3691         val &= ~PLLE_MISC_IDDQ_SW_VALUE;
3692         val |= PLLE_MISC_PLLE_PTS;
3693         clk_writel(val, c->reg + c->u.pll.misc0);
3694         udelay(5);
3695
3696         /* configure dividers, disable SS */
3697         val = clk_readl(PLLE_SS_CTRL);
3698         val |= PLLE_SS_DISABLE;
3699         clk_writel(val, PLLE_SS_CTRL);
3700
3701         val = clk_readl(c->reg);
3702         val &= ~(PLLE_BASE_DIVM_MASK | PLLE_BASE_DIVN_MASK |
3703                  PLLE_BASE_DIVCML_MASK);
3704         val |= (sel->m << PLLE_BASE_DIVM_SHIFT) |
3705                 (sel->n << PLLE_BASE_DIVN_SHIFT) |
3706                 (pdiv << PLLE_BASE_DIVCML_SHIFT);
3707         pll_writel_delay(val, c->reg);
3708         c->mul = sel->n;
3709         c->div = sel->m * sel->p;
3710
3711         /* enable and lock pll */
3712         val |= PLLE_BASE_ENABLE;
3713         clk_writel(val, c->reg);
3714         tegra_pll_clk_wait_for_lock(
3715                 c, c->reg + c->u.pll.misc0, PLLE_MISC_LOCK);
3716
3717 #if USE_PLLE_SS
3718         val = clk_readl(PLLE_SS_CTRL);
3719         val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
3720         val &= ~PLLE_SS_COEFFICIENTS_MASK;
3721         val |= PLLE_SS_COEFFICIENTS_VAL;
3722         clk_writel(val, PLLE_SS_CTRL);
3723         val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
3724         pll_writel_delay(val, PLLE_SS_CTRL);
3725         val &= ~PLLE_SS_CNTL_INTERP_RESET;
3726         pll_writel_delay(val, PLLE_SS_CTRL);
3727 #endif
3728
3729 #if !USE_PLLE_SWCTL
3730         /* switch pll under h/w control */
3731         val = clk_readl(c->reg + c->u.pll.misc0);
3732         val &= ~PLLE_MISC_IDDQ_SW_CTRL;
3733         clk_writel(val, c->reg + c->u.pll.misc0);
3734
3735         val = clk_readl(PLLE_AUX);
3736         val |= PLLE_AUX_USE_LOCKDET | PLLE_AUX_SS_SEQ_INCLUDE;
3737         val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
3738         pll_writel_delay(val, PLLE_AUX);
3739         val |= PLLE_AUX_SEQ_ENABLE;
3740         pll_writel_delay(val, PLLE_AUX);
3741 #endif
3742         return 0;
3743 }
3744
3745 static struct clk_ops tegra_plle_ops = {
3746         .init                   = tegra21_plle_clk_init,
3747         .enable                 = tegra21_plle_clk_enable,
3748         .disable                = tegra21_plle_clk_disable,
3749 };
3750
3751 /*
3752  * Tegra21 includes dynamic frequency lock loop (DFLL) with automatic voltage
3753  * control as possible CPU clock source. It is included in the Tegra12 clock
3754  * tree as "complex PLL" with standard Tegra clock framework APIs. However,
3755  * DFLL locking logic h/w access APIs are separated in the tegra_cl_dvfs.c
3756  * module. Hence, DFLL operations, with the exception of initialization, are
3757  * basically cl-dvfs wrappers.
3758  */
3759
3760 /* DFLL operations */
3761 #ifdef  CONFIG_ARCH_TEGRA_HAS_CL_DVFS
3762 static void tune_cpu_trimmers(bool trim_high)
3763 {
3764         tegra_soctherm_adjust_cpu_zone(trim_high);
3765         pr_info_once("%s: init soctherm cpu zone %s\n", __func__,
3766                  trim_high ? "HIGH" : "LOW");
3767         pr_debug("%s: adjust soctherm cpu zone %s\n", __func__,
3768                  trim_high ? "HIGH" : "LOW");
3769 }
3770 #endif
3771
3772 static void __init tegra21_dfll_clk_init(struct clk *c)
3773 {
3774         unsigned long int dfll_boot_req_khz = tegra_dfll_boot_req_khz();
3775         c->ops->init = tegra21_dfll_cpu_late_init;
3776
3777         /*
3778          * If boot loader has set dfll clock, then dfll freq is
3779          * passed in kernel command line from bootloader
3780          */
3781         if (dfll_boot_req_khz)
3782                 c->rate = dfll_boot_req_khz * 1000;
3783 }
3784
3785 static int tegra21_dfll_clk_enable(struct clk *c)
3786 {
3787         if (c->u.dfll.cl_dvfs)
3788                 return tegra_cl_dvfs_enable(c->u.dfll.cl_dvfs);
3789         return 0;
3790 }
3791
3792 static void tegra21_dfll_clk_disable(struct clk *c)
3793 {
3794         if (c->u.dfll.cl_dvfs)
3795                 tegra_cl_dvfs_disable(c->u.dfll.cl_dvfs);
3796 }
3797
3798 static unsigned long boost_dfll_rate(struct clk *c, unsigned long rate)
3799 {
3800         struct clk *consumer = c->u.dfll.consumer;
3801         if (!consumer || !consumer->dvfs || !consumer->dvfs->boost_table ||
3802             (consumer->dvfs->num_freqs <= 1))
3803                 return rate;
3804
3805         /* Only one top DVFS step boost is allowed */
3806         if (rate < consumer->dvfs->freqs[consumer->dvfs->num_freqs-2])
3807                 return rate;
3808
3809         return consumer->dvfs->freqs[consumer->dvfs->num_freqs-1];
3810 }
3811
3812 static int tegra21_dfll_clk_set_rate(struct clk *c, unsigned long rate)
3813 {
3814         if (c->u.dfll.cl_dvfs) {
3815                 unsigned long boost_rate = boost_dfll_rate(c, rate);
3816                 int ret = tegra_cl_dvfs_request_rate(c->u.dfll.cl_dvfs,
3817                                                      boost_rate);
3818                 /*
3819                  * Record requested rate. Thus, non boosted rate is reported.
3820                  * It also ensures correct clock settings when switching back
3821                  * and forth to a clock source with different than DFLL
3822                  * granularity, and prevents other than DFLL clock source to run
3823                  * at boost rate that is supported on DFLL only.
3824                  */
3825                 if (!ret)
3826                         c->rate = rate;
3827
3828                 return ret;
3829         }
3830         return 0;
3831 }
3832
3833 static void tegra21_dfll_clk_reset(struct clk *c, bool assert)
3834 {
3835         u32 val = assert ? DFLL_BASE_RESET : 0;
3836         clk_writel_delay(val, c->reg);
3837 }
3838
3839 static int
3840 tegra21_dfll_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
3841 {
3842         if (p == TEGRA_CLK_DFLL_LOCK) {
3843                 if (c->u.dfll.cl_dvfs)
3844                         return setting ? tegra_cl_dvfs_lock(c->u.dfll.cl_dvfs) :
3845                                         tegra_cl_dvfs_unlock(c->u.dfll.cl_dvfs);
3846                 return 0;
3847         }
3848         return -EINVAL;
3849 }
3850
3851 #ifdef CONFIG_PM_SLEEP
3852 static void tegra21_dfll_clk_resume(struct clk *c)
3853 {
3854         if (!(clk_readl(c->reg) & DFLL_BASE_RESET))
3855                 return;         /* already resumed */
3856
3857         if (c->u.dfll.cl_dvfs) {
3858                 tegra_periph_reset_deassert(c);
3859                 tegra_cl_dvfs_resume(c->u.dfll.cl_dvfs);
3860         }
3861 }
3862 #endif
3863
3864 static struct clk_ops tegra_dfll_ops = {
3865         .init                   = tegra21_dfll_clk_init,
3866         .enable                 = tegra21_dfll_clk_enable,
3867         .disable                = tegra21_dfll_clk_disable,
3868         .set_rate               = tegra21_dfll_clk_set_rate,
3869         .reset                  = tegra21_dfll_clk_reset,
3870         .clk_cfg_ex             = tegra21_dfll_clk_cfg_ex,
3871 };
3872
3873 /* DFLL sysfs interface */
3874 static int tegra21_use_dfll_cb(const char *arg, const struct kernel_param *kp)
3875 {
3876         int ret = 0;
3877         unsigned int old_use_dfll;
3878         if (CONFIG_TEGRA_USE_DFLL_RANGE != TEGRA_USE_DFLL_CDEV_CNTRL) {
3879                 old_use_dfll = use_dfll;
3880                 param_set_int(arg, kp);
3881                 ret =  tegra_clk_dfll_range_control(use_dfll);
3882                 if (ret)
3883                         use_dfll = old_use_dfll;
3884                 return ret;
3885         } else {
3886                 pr_warn("\n%s: Failed to set use_dfll\n", __func__);
3887                 pr_warn("DFLL usage is under thermal cooling device control\n");
3888                 return -EACCES;
3889         }
3890 }
3891
3892 static struct kernel_param_ops tegra21_use_dfll_ops = {
3893         .set = tegra21_use_dfll_cb,
3894         .get = param_get_int,
3895 };
3896 module_param_cb(use_dfll, &tegra21_use_dfll_ops, &use_dfll, 0644);
3897
3898 /*
3899  * PLL internal post divider ops for PLLs that have both VCO and post-divider
3900  * output separtely connected to the clock tree.
3901  */
3902 static struct clk_ops tegra_pll_out_ops = {
3903         .init                   = tegra_pll_out_clk_init,
3904         .enable                 = tegra_pll_out_clk_enable,
3905         .disable                = tegra_pll_out_clk_disable,
3906         .set_rate               = tegra_pll_out_clk_set_rate,
3907 };
3908
3909 static struct clk_ops tegra_pll_out_fixed_ops = {
3910         .init                   = tegra_pll_out_clk_init,
3911         .enable                 = tegra_pll_out_clk_enable,
3912         .disable                = tegra_pll_out_clk_disable,
3913 };
3914
3915 static void tegra21_pllu_out_clk_init(struct clk *c)
3916 {
3917         u32 p, val;
3918         struct clk *pll = c->parent;
3919         val = clk_readl(pll->reg);
3920
3921         /*
3922          * If PLLU state is already under h/w control just record output ratio,
3923          * and verify expected fixed output rate.
3924          */
3925         if (!(val & PLLU_BASE_OVERRIDE)) {
3926                 struct clk_pll_div_layout *divs = pll->u.pll.div_layout;
3927
3928                 c->state = c->parent->state;
3929                 c->max_rate = pll->u.pll.vco_max;
3930
3931                 p = (val & divs->pdiv_mask) >> divs->pdiv_shift;
3932                 if (p > divs->pdiv_max)
3933                         p = divs->pdiv_max; /* defer invalid p WARN to verify */
3934                 p = divs->pdiv_to_p[p];
3935                 c->div = p;
3936                 c->mul = 1;
3937
3938                 pll_clk_verify_fixed_rate(c);
3939         } else {
3940                 /* Complete PLLU output s/w controlled initialization */
3941                 tegra_pll_out_clk_init(c);
3942         }
3943 }
3944
3945 static struct clk_ops tegra_pllu_out_ops = {
3946         .init                   = tegra21_pllu_out_clk_init,
3947         .enable                 = tegra_pll_out_clk_enable,
3948         .disable                = tegra_pll_out_clk_disable,
3949         .set_rate               = tegra_pll_out_clk_set_rate,
3950 };
3951
3952 static void tegra21_pllp_out_clk_init(struct clk *c)
3953 {
3954         u32 p, val;
3955         struct clk *pll = c->parent;
3956         val = clk_readl(pll->reg);
3957
3958         /*
3959          * If PLLP state is already under h/w control just record expected
3960          * fixed output rate.
3961          */
3962         if (!(val & PLLP_BASE_OVERRIDE)) {
3963                 c->state = c->parent->state;
3964                 c->max_rate = pll->u.pll.vco_max;
3965                 c->div = pll->u.pll.fixed_rate / c->u.pll.fixed_rate;
3966                 c->mul = 1;
3967                 pll_clk_verify_fixed_rate(c);
3968         } else {
3969                 /* Complete pllp output s/w controlled initialization */
3970                 tegra_pll_out_clk_init(c);
3971         }
3972
3973         /* Reset not used PLL_P_OUT1 branch. */
3974         clk_writel(0x0, pll->reg + 0x4);
3975 }
3976
3977 static struct clk_ops tegra_pllp_out_ops = {
3978         .init                   = tegra21_pllp_out_clk_init,
3979         .enable                 = tegra_pll_out_clk_enable,
3980         .disable                = tegra_pll_out_clk_disable,
3981         .set_rate               = tegra_pll_out_clk_set_rate,
3982 };
3983
3984 #ifdef CONFIG_PM_SLEEP
3985 static void tegra_pllu_out_resume_enable(struct clk *c)
3986 {
3987         u32 val = clk_readl(c->parent->reg);
3988         if (!(val & PLLU_BASE_OVERRIDE) ||
3989             val & c->parent->u.pll.controls->enable_mask)
3990                 return;         /* already resumed */
3991
3992         tegra_pll_out_resume_enable(c);
3993 }
3994
3995 static void tegra_pllp_out_resume_enable(struct clk *c)
3996 {
3997         u32 val = clk_readl(c->parent->reg);
3998
3999         /* Reset not used PLL_P_OUT1 branch. */
4000         clk_writel(0x0, c->parent->reg + 0x4);
4001
4002         if (!(val & PLLP_BASE_OVERRIDE) ||
4003             val & c->parent->u.pll.controls->enable_mask)
4004                 return;         /* already resumed */
4005
4006         tegra_pll_out_resume_enable(c);
4007 }
4008
4009 #endif
4010
4011 /* PLL external secondary divider ops (non-atomic shared register access) */
4012 static DEFINE_SPINLOCK(pll_div_lock);
4013
4014 static int tegra21_pll_div_clk_set_rate(struct clk *c, unsigned long rate);
4015 static void tegra21_pll_div_clk_init(struct clk *c)
4016 {
4017         if (c->flags & DIV_U71) {
4018                 u32 val, divu71;
4019                 if ((c->parent->state == OFF) || (!c->parent->ops->disable &&
4020                      (c->parent->parent->state == OFF)))
4021                         c->ops->disable(c);
4022
4023                 val = clk_readl(c->reg);
4024                 val >>= c->reg_shift;
4025                 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
4026                 if (!(val & PLL_OUT_RESET_DISABLE))
4027                         c->state = OFF;
4028
4029                 if (c->u.pll_div.default_rate) {
4030                         int ret = tegra21_pll_div_clk_set_rate(
4031                                         c, c->u.pll_div.default_rate);
4032                         if (!ret)
4033                                 return;
4034                 }
4035                 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
4036                 c->div = (divu71 + 2);
4037                 c->mul = 2;
4038         } else if (c->flags & DIV_2) {
4039                 c->state = ON;
4040                 if (c->flags & (PLLD | PLLX)) {
4041                         c->div = 2;
4042                         c->mul = 1;
4043                 }
4044                 else
4045                         BUG();
4046         } else {
4047                 u32 val = clk_readl(c->reg);
4048                 c->state = val & (0x1 << c->reg_shift) ? ON : OFF;
4049                 c->max_rate = c->parent->max_rate;
4050         }
4051 }
4052
4053 static int tegra21_pll_div_clk_enable(struct clk *c)
4054 {
4055         u32 val;
4056         u32 new_val;
4057         unsigned long flags;
4058
4059         pr_debug("%s: %s\n", __func__, c->name);
4060         if (c->flags & DIV_U71) {
4061                 spin_lock_irqsave(&pll_div_lock, flags);
4062                 val = clk_readl(c->reg);
4063                 new_val = val >> c->reg_shift;
4064                 new_val &= 0xFFFF;
4065
4066                 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
4067
4068                 val &= ~(0xFFFF << c->reg_shift);
4069                 val |= new_val << c->reg_shift;
4070                 clk_writel_delay(val, c->reg);
4071                 spin_unlock_irqrestore(&pll_div_lock, flags);
4072                 return 0;
4073         } else if (c->flags & DIV_2) {
4074                 return 0;
4075         } else if (c->flags & PLLU) {
4076                 clk_lock_save(pll_u, &flags);
4077                 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
4078                 clk_writel_delay(val, c->reg);
4079                 clk_unlock_restore(pll_u, &flags);
4080                 return 0;
4081         } else {
4082                 spin_lock_irqsave(&pll_div_lock, flags);
4083                 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
4084                 clk_writel_delay(val, c->reg);
4085                 spin_unlock_irqrestore(&pll_div_lock, flags);
4086                 return 0;
4087         }
4088         return -EINVAL;
4089 }
4090
4091 static void tegra21_pll_div_clk_disable(struct clk *c)
4092 {
4093         u32 val;
4094         u32 new_val;
4095         unsigned long flags;
4096
4097         pr_debug("%s: %s\n", __func__, c->name);
4098         if (c->flags & DIV_U71) {
4099                 spin_lock_irqsave(&pll_div_lock, flags);
4100                 val = clk_readl(c->reg);
4101                 new_val = val >> c->reg_shift;
4102                 new_val &= 0xFFFF;
4103
4104                 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
4105
4106                 val &= ~(0xFFFF << c->reg_shift);
4107                 val |= new_val << c->reg_shift;
4108                 clk_writel_delay(val, c->reg);
4109                 spin_unlock_irqrestore(&pll_div_lock, flags);
4110         } else if (c->flags & PLLU) {
4111                 clk_lock_save(pll_u, &flags);
4112                 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
4113                 clk_writel_delay(val, c->reg);
4114                 clk_unlock_restore(pll_u, &flags);
4115         } else {
4116                 spin_lock_irqsave(&pll_div_lock, flags);
4117                 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
4118                 clk_writel_delay(val, c->reg);
4119                 spin_unlock_irqrestore(&pll_div_lock, flags);
4120         }
4121 }
4122
4123 static int tegra21_pll_div_clk_set_rate(struct clk *c, unsigned long rate)
4124 {
4125         u32 val;
4126         u32 new_val;
4127         int divider_u71;
4128         unsigned long parent_rate = clk_get_rate(c->parent);
4129         unsigned long flags;
4130
4131         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4132         if (c->flags & DIV_U71) {
4133                 divider_u71 = clk_div71_get_divider(
4134                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4135                 if (divider_u71 >= 0) {
4136                         spin_lock_irqsave(&pll_div_lock, flags);
4137                         val = clk_readl(c->reg);
4138                         new_val = val >> c->reg_shift;
4139                         new_val &= 0xFFFF;
4140                         if (c->flags & DIV_U71_FIXED)
4141                                 new_val |= PLL_OUT_OVERRIDE;
4142                         new_val &= ~PLL_OUT_RATIO_MASK;
4143                         new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
4144
4145                         val &= ~(0xFFFF << c->reg_shift);
4146                         val |= new_val << c->reg_shift;
4147                         clk_writel_delay(val, c->reg);
4148                         c->div = divider_u71 + 2;
4149                         c->mul = 2;
4150                         spin_unlock_irqrestore(&pll_div_lock, flags);
4151                         return 0;
4152                 }
4153         } else if (c->flags & DIV_2)
4154                 return clk_set_rate(c->parent, rate * 2);
4155
4156         return -EINVAL;
4157 }
4158
4159 static long tegra21_pll_div_clk_round_rate(struct clk *c, unsigned long rate)
4160 {
4161         int divider;
4162         unsigned long parent_rate = clk_get_rate(c->parent);
4163         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4164
4165         if (c->flags & DIV_U71) {
4166                 divider = clk_div71_get_divider(
4167                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4168                 if (divider < 0)
4169                         return divider;
4170                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
4171         } else if (c->flags & DIV_2)
4172                 /* no rounding - fixed DIV_2 dividers pass rate to parent PLL */
4173                 return rate;
4174
4175         return -EINVAL;
4176 }
4177
4178 static struct clk_ops tegra_pll_div_ops = {
4179         .init                   = tegra21_pll_div_clk_init,
4180         .enable                 = tegra21_pll_div_clk_enable,
4181         .disable                = tegra21_pll_div_clk_disable,
4182         .set_rate               = tegra21_pll_div_clk_set_rate,
4183         .round_rate             = tegra21_pll_div_clk_round_rate,
4184 };
4185
4186 /* Periph clk ops */
4187 static inline u32 periph_clk_source_mask(struct clk *c)
4188 {
4189         if (c->u.periph.src_mask)
4190                 return c->u.periph.src_mask;
4191         else if (c->flags & MUX_PWM)
4192                 return 3 << 28;
4193         else if (c->flags & MUX_CLK_OUT)
4194                 return 3 << (c->u.periph.clk_num + 4);
4195         else if (c->flags & PLLD)
4196                 return PLLD_BASE_DSI_MUX_MASK;
4197         else
4198                 return 7 << 29;
4199 }
4200
4201 static inline u32 periph_clk_source_shift(struct clk *c)
4202 {
4203         if (c->u.periph.src_shift)
4204                 return c->u.periph.src_shift;
4205         else if (c->flags & MUX_PWM)
4206                 return 28;
4207         else if (c->flags & MUX_CLK_OUT)
4208                 return c->u.periph.clk_num + 4;
4209         else if (c->flags & PLLD)
4210                 return PLLD_BASE_DSI_MUX_SHIFT;
4211         else
4212                 return 29;
4213 }
4214
4215 static void periph_clk_state_init(struct clk *c)
4216 {
4217         if (c->flags & PERIPH_NO_ENB) {
4218                 c->state = c->parent->state;
4219                 return;
4220         }
4221
4222         c->state = ON;
4223
4224         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
4225                 c->state = OFF;
4226         if (!(c->flags & PERIPH_NO_RESET))
4227                 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
4228                         c->state = OFF;
4229 }
4230
4231 static bool periph_clk_can_force_safe_rate(struct clk *c)
4232 {
4233         /*
4234          * As a general rule, force safe rate always for clocks supplied
4235          * to peripherals under reset. If clock does not have associated reset
4236          * at all, force safe rate if clock is disabled (can do it on Tegra21
4237          * that allow to change dividers of the disabled clocks).
4238          */
4239         if (c->flags & PERIPH_NO_RESET)
4240                         return c->state == OFF;
4241
4242         return clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c);
4243 }
4244
4245 static void tegra21_periph_clk_init(struct clk *c)
4246 {
4247         u32 val = clk_readl(c->reg);
4248         const struct clk_mux_sel *mux = NULL;
4249         const struct clk_mux_sel *sel;
4250         if (c->flags & MUX) {
4251                 for (sel = c->inputs; sel->input != NULL; sel++) {
4252                         if (((val & periph_clk_source_mask(c)) >>
4253                             periph_clk_source_shift(c)) == sel->value)
4254                                 mux = sel;
4255                 }
4256                 BUG_ON(!mux);
4257
4258                 c->parent = mux->input;
4259         } else {
4260                 c->parent = c->inputs[0].input;
4261         }
4262
4263         if (c->flags & DIV_U71) {
4264                 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
4265                 if (c->flags & DIV_U71_IDLE) {
4266                         val &= ~(PERIPH_CLK_SOURCE_DIVU71_MASK <<
4267                                 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
4268                         val |= (PERIPH_CLK_SOURCE_DIVIDLE_VAL <<
4269                                 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
4270                         clk_writel(val, c->reg);
4271                 }
4272                 c->div = divu71 + 2;
4273                 c->mul = 2;
4274         } else if (c->flags & DIV_U151) {
4275                 u32 divu151 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
4276                 if ((c->flags & DIV_U151_UART) &&
4277                     (!(val & PERIPH_CLK_UART_DIV_ENB))) {
4278                         divu151 = 0;
4279                 }
4280                 c->div = divu151 + 2;
4281                 c->mul = 2;
4282         } else if (c->flags & DIV_U16) {
4283                 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
4284                 c->div = divu16 + 1;
4285                 c->mul = 1;
4286         } else if (!c->div || !c->mul) {
4287                 c->div = 1;
4288                 c->mul = 1;
4289         }
4290
4291         periph_clk_state_init(c);
4292
4293         /* if peripheral is left under reset - enforce safe rate */
4294         if (periph_clk_can_force_safe_rate(c))
4295                 tegra_periph_clk_safe_rate_init(c);
4296 }
4297
4298 static int tegra21_periph_clk_enable(struct clk *c)
4299 {
4300         unsigned long flags;
4301         pr_debug("%s on clock %s\n", __func__, c->name);
4302
4303         if (c->flags & PERIPH_NO_ENB)
4304                 return 0;
4305
4306         spin_lock_irqsave(&periph_refcount_lock, flags);
4307
4308         tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
4309         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1) {
4310                 spin_unlock_irqrestore(&periph_refcount_lock, flags);
4311                 return 0;
4312         }
4313
4314         clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_SET_REG(c));
4315
4316         if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET)) {
4317                 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c)) {
4318                         udelay(RESET_PROPAGATION_DELAY);
4319                         clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4320                                          PERIPH_CLK_TO_RST_CLR_REG(c));
4321                 }
4322         }
4323         spin_unlock_irqrestore(&periph_refcount_lock, flags);
4324         return 0;
4325 }
4326
4327 static void tegra21_periph_clk_disable(struct clk *c)
4328 {
4329         unsigned long val, flags;
4330         pr_debug("%s on clock %s\n", __func__, c->name);
4331
4332         if (c->flags & PERIPH_NO_ENB)
4333                 return;
4334
4335         spin_lock_irqsave(&periph_refcount_lock, flags);
4336
4337         if (c->refcnt)
4338                 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
4339
4340         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] == 0) {
4341                 /* If peripheral is in the APB bus then read the APB bus to
4342                  * flush the write operation in apb bus. This will avoid the
4343                  * peripheral access after disabling clock*/
4344                 if (c->flags & PERIPH_ON_APB)
4345                         val = chipid_readl();
4346
4347                 clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4348                                  PERIPH_CLK_TO_ENB_CLR_REG(c));
4349         }
4350         spin_unlock_irqrestore(&periph_refcount_lock, flags);
4351 }
4352
4353 static void tegra21_periph_clk_reset(struct clk *c, bool assert)
4354 {
4355         unsigned long val;
4356         pr_debug("%s %s on clock %s\n", __func__,
4357                  assert ? "assert" : "deassert", c->name);
4358
4359         if (!(c->flags & PERIPH_NO_RESET)) {
4360                 if (assert) {
4361                         /* If peripheral is in the APB bus then read the APB
4362                          * bus to flush the write operation in apb bus. This
4363                          * will avoid the peripheral access after disabling
4364                          * clock */
4365                         if (c->flags & PERIPH_ON_APB)
4366                                 val = chipid_readl();
4367
4368                         clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4369                                          PERIPH_CLK_TO_RST_SET_REG(c));
4370                 } else
4371                         clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4372                                          PERIPH_CLK_TO_RST_CLR_REG(c));
4373         }
4374 }
4375
4376 static int tegra21_periph_clk_set_parent(struct clk *c, struct clk *p)
4377 {
4378         u32 val;
4379         const struct clk_mux_sel *sel;
4380         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
4381
4382         if (!(c->flags & MUX))
4383                 return (p == c->parent) ? 0 : (-EINVAL);
4384
4385         for (sel = c->inputs; sel->input != NULL; sel++) {
4386                 if (sel->input == p) {
4387                         val = clk_readl(c->reg);
4388                         val &= ~periph_clk_source_mask(c);
4389                         val |= (sel->value << periph_clk_source_shift(c));
4390
4391                         if (c->refcnt)
4392                                 tegra_clk_prepare_enable(p);
4393
4394                         clk_writel_delay(val, c->reg);
4395
4396                         if (c->refcnt && c->parent)
4397                                 tegra_clk_disable_unprepare(c->parent);
4398
4399                         clk_reparent(c, p);
4400                         return 0;
4401                 }
4402         }
4403
4404         return -EINVAL;
4405 }
4406
4407 static int tegra21_periph_clk_set_rate(struct clk *c, unsigned long rate)
4408 {
4409         u32 val;
4410         int divider;
4411         unsigned long parent_rate = clk_get_rate(c->parent);
4412
4413         if (c->flags & DIV_U71) {
4414                 divider = clk_div71_get_divider(
4415                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4416                 if (divider >= 0) {
4417                         val = clk_readl(c->reg);
4418                         val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
4419                         val |= divider;
4420                         clk_writel_delay(val, c->reg);
4421                         c->div = divider + 2;
4422                         c->mul = 2;
4423                         return 0;
4424                 }
4425         } else if (c->flags & DIV_U151) {
4426                 divider = clk_div151_get_divider(
4427                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4428                 if (divider >= 0) {
4429                         val = clk_readl(c->reg);
4430                         val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
4431                         val |= divider;
4432                         if (c->flags & DIV_U151_UART) {
4433                                 if (divider)
4434                                         val |= PERIPH_CLK_UART_DIV_ENB;
4435                                 else
4436                                         val &= ~PERIPH_CLK_UART_DIV_ENB;
4437                         }
4438                         clk_writel_delay(val, c->reg);
4439                         c->div = divider + 2;
4440                         c->mul = 2;
4441                         return 0;
4442                 }
4443         } else if (c->flags & DIV_U16) {
4444                 divider = clk_div16_get_divider(parent_rate, rate);
4445                 if (divider >= 0) {
4446                         val = clk_readl(c->reg);
4447                         val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
4448                         val |= divider;
4449                         clk_writel_delay(val, c->reg);
4450                         c->div = divider + 1;
4451                         c->mul = 1;
4452                         return 0;
4453                 }
4454         } else if (parent_rate <= rate) {
4455                 c->div = 1;
4456                 c->mul = 1;
4457                 return 0;
4458         }
4459         return -EINVAL;
4460 }
4461
4462 static long tegra21_periph_clk_round_rate(struct clk *c,
4463         unsigned long rate)
4464 {
4465         int divider;
4466         unsigned long parent_rate = clk_get_rate(c->parent);
4467         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4468
4469         if (c->flags & DIV_U71) {
4470                 divider = clk_div71_get_divider(
4471                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4472                 if (divider < 0)
4473                         return divider;
4474
4475                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
4476         } else if (c->flags & DIV_U151) {
4477                 divider = clk_div151_get_divider(
4478                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4479                 if (divider < 0)
4480                         return divider;
4481
4482                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
4483         } else if (c->flags & DIV_U16) {
4484                 divider = clk_div16_get_divider(parent_rate, rate);
4485                 if (divider < 0)
4486                         return divider;
4487                 return DIV_ROUND_UP(parent_rate, divider + 1);
4488         }
4489         return -EINVAL;
4490 }
4491
4492 static struct clk_ops tegra_periph_clk_ops = {
4493         .init                   = &tegra21_periph_clk_init,
4494         .enable                 = &tegra21_periph_clk_enable,
4495         .disable                = &tegra21_periph_clk_disable,
4496         .set_parent             = &tegra21_periph_clk_set_parent,
4497         .set_rate               = &tegra21_periph_clk_set_rate,
4498         .round_rate             = &tegra21_periph_clk_round_rate,
4499         .reset                  = &tegra21_periph_clk_reset,
4500 };
4501
4502 static int tegra21_qspi_clk_cfg_ex(struct clk *c,
4503                 enum tegra_clk_ex_param p, u32 setting)
4504 {
4505         if (p == TEGRA_CLK_QSPI_DIV2_ENB) {
4506                 u32 div2_val = 0;
4507                 u32 val = clk_readl(c->reg);
4508
4509                 div2_val = val & PERIPH_CLK_SOURCE_QSPI_DIV2_EN;
4510
4511                 if (setting && !div2_val) {
4512                         val |= PERIPH_CLK_SOURCE_QSPI_DIV2_EN;
4513                         clk_writel_delay(val, c->reg);
4514                 } else if (!setting && div2_val) {
4515                         val &= ~PERIPH_CLK_SOURCE_QSPI_DIV2_EN;
4516                         clk_writel_delay(val, c->reg);
4517                 }
4518                 return 0;
4519         }
4520         return -EINVAL;
4521 }
4522
4523 static struct clk_ops tegra_qspi_clk_ops = {
4524         .init                   = &tegra21_periph_clk_init,
4525         .enable                 = &tegra21_periph_clk_enable,
4526         .disable                = &tegra21_periph_clk_disable,
4527         .set_parent             = &tegra21_periph_clk_set_parent,
4528         .set_rate               = &tegra21_periph_clk_set_rate,
4529         .round_rate             = &tegra21_periph_clk_round_rate,
4530         .reset                  = &tegra21_periph_clk_reset,
4531         .clk_cfg_ex             = &tegra21_qspi_clk_cfg_ex,
4532 };
4533
4534 /* Supper skipper ops */
4535 static void tegra21_clk_super_skip_init(struct clk *c)
4536 {
4537         if (!c->parent)
4538                 c->parent = c - 1;
4539         c->parent->skipper = c;
4540
4541         /* Skipper is always ON (does not gate the clock) */
4542         c->state = ON;
4543         c->max_rate = c->parent->max_rate;
4544
4545         /* Always set initial 1:1 ratio */
4546         clk_writel(0, c->reg);
4547         c->div = 1;
4548         c->mul = 1;
4549 }
4550
4551 static int tegra21_clk_super_skip_enable(struct clk *c)
4552 {
4553         /* no clock gate in skipper, just pass thru to parent */
4554         return 0;
4555 }
4556
4557 #ifdef CONFIG_TEGRA_BPMP_SCLK_SKIP
4558 static void tegra_bpmp_sclk_skip_set_rate(unsigned long input_rate,
4559                 unsigned long rate)
4560 {
4561         uint32_t mb[] = { cpu_to_le32(input_rate), cpu_to_le32(rate) };
4562         int r = tegra_bpmp_send_receive_atomic(MRQ_SCLK_SKIP_SET_RATE,
4563                         &mb, sizeof(mb), NULL, 0);
4564         WARN_ON(r);
4565 }
4566 #endif
4567
4568 static int tegra21_clk_super_skip_set_rate(struct clk *c, unsigned long rate)
4569 {
4570         u32 val, mul, div;
4571         u64 output_rate = rate;
4572         unsigned long input_rate, flags;
4573
4574         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4575
4576         /*
4577          * Locking parent clock prevents parent rate change while super skipper
4578          * is updated. It also takes care of super skippers that share h/w
4579          * register with parent clock divider. Skipper output rate can be
4580          * rounded up, since volatge is set based on source clock rate.
4581          */
4582         clk_lock_save(c->parent, &flags);
4583         input_rate = clk_get_rate_locked(c->parent);
4584
4585         div = 1 << SUPER_SKIPPER_TERM_SIZE;
4586         output_rate <<= SUPER_SKIPPER_TERM_SIZE;
4587         output_rate += input_rate - 1;
4588         do_div(output_rate, input_rate);
4589         mul = output_rate ? : 1;
4590
4591         if (mul < div) {
4592                 val = SUPER_SKIPPER_ENABLE |
4593                         ((mul - 1) << SUPER_SKIPPER_MUL_SHIFT) |
4594                         ((div - 1) << SUPER_SKIPPER_DIV_SHIFT);
4595                 c->div = div;
4596                 c->mul = mul;
4597         } else {
4598                 val = 0;
4599                 c->div = 1;
4600                 c->mul = 1;
4601         }
4602
4603 #ifdef CONFIG_TEGRA_BPMP_SCLK_SKIP
4604         /* For SCLK do not touch the register directly - send IPC to BPMP */
4605         if (c->flags & DIV_BUS)
4606                 tegra_bpmp_sclk_skip_set_rate(input_rate, rate);
4607         else
4608 #endif
4609                 clk_writel(val, c->reg);
4610
4611         clk_unlock_restore(c->parent, &flags);
4612         return 0;
4613 }
4614
4615 static struct clk_ops tegra_clk_super_skip_ops = {
4616         .init                   = &tegra21_clk_super_skip_init,
4617         .enable                 = &tegra21_clk_super_skip_enable,
4618         .set_rate               = &tegra21_clk_super_skip_set_rate,
4619 };
4620
4621 /* 1x shared bus ops */
4622 static long _1x_round_updown(struct clk *c, struct clk *src,
4623                                 unsigned long rate, bool up)
4624 {
4625         return fixed_src_bus_round_updown(c, src, c->flags, rate, up, NULL);
4626 }
4627
4628 static long tegra21_1xbus_round_updown(struct clk *c, unsigned long rate,
4629                                             bool up)
4630 {
4631         unsigned long pll_low_rate, pll_high_rate;
4632
4633         rate = max(rate, c->min_rate);
4634
4635         pll_low_rate = _1x_round_updown(c, c->u.periph.pll_low, rate, up);
4636         if (rate <= c->u.periph.threshold) {
4637                 c->u.periph.pll_selected = c->u.periph.pll_low;
4638                 return pll_low_rate;
4639         }
4640
4641         pll_high_rate = _1x_round_updown(c, c->u.periph.pll_high, rate, up);
4642         if (pll_high_rate <= c->u.periph.threshold) {
4643                 c->u.periph.pll_selected = c->u.periph.pll_low;
4644                 return pll_low_rate;  /* prevent oscillation across threshold */
4645         }
4646
4647         if (up) {
4648                 /* rounding up: both plls may hit max, and round down */
4649                 if (pll_high_rate < rate) {
4650                         if (pll_low_rate < pll_high_rate) {
4651                                 c->u.periph.pll_selected = c->u.periph.pll_high;
4652                                 return pll_high_rate;
4653                         }
4654                 } else {
4655                         if ((pll_low_rate < rate) ||
4656                             (pll_low_rate > pll_high_rate)) {
4657                                 c->u.periph.pll_selected = c->u.periph.pll_high;
4658                                 return pll_high_rate;
4659                         }
4660                 }
4661         } else if (pll_low_rate < pll_high_rate) {
4662                 /* rounding down: to get here both plls able to round down */
4663                 c->u.periph.pll_selected = c->u.periph.pll_high;
4664                 return pll_high_rate;
4665         }
4666         c->u.periph.pll_selected = c->u.periph.pll_low;
4667         return pll_low_rate;
4668 }
4669
4670 static long tegra21_1xbus_round_rate(struct clk *c, unsigned long rate)
4671 {
4672         return tegra21_1xbus_round_updown(c, rate, true);
4673 }
4674
4675 static int tegra21_1xbus_set_rate(struct clk *c, unsigned long rate)
4676 {
4677         /* Compensate rate truncating during rounding */
4678         return tegra21_periph_clk_set_rate(c, rate + 1);
4679 }
4680
4681 static int tegra21_clk_1xbus_update(struct clk *c)
4682 {
4683         int ret;
4684         struct clk *new_parent;
4685         unsigned long rate, old_rate;
4686
4687         if (detach_shared_bus)
4688                 return 0;
4689
4690         rate = tegra21_clk_shared_bus_update(c, NULL, NULL, NULL);
4691
4692         old_rate = clk_get_rate_locked(c);
4693         pr_debug("\n1xbus %s: rate %lu on parent %s: new request %lu\n",
4694                  c->name, old_rate, c->parent->name, rate);
4695         if (rate == old_rate)
4696                 return 0;
4697
4698         if (!c->u.periph.min_div_low || !c->u.periph.min_div_high) {
4699                 unsigned long r, m = c->max_rate;
4700                 r = clk_get_rate(c->u.periph.pll_low);
4701                 c->u.periph.min_div_low = DIV_ROUND_UP(r, m) * c->mul;
4702                 r = clk_get_rate(c->u.periph.pll_high);
4703                 c->u.periph.min_div_high = DIV_ROUND_UP(r, m) * c->mul;
4704         }
4705
4706         new_parent = c->u.periph.pll_selected;
4707
4708         /*
4709          * The transition procedure below is guaranteed to switch to the target
4710          * parent/rate without violation of max clock limits. It would attempt
4711          * to switch without dip in bus rate if it is possible, but this cannot
4712          * be guaranteed (example: switch from 408 MHz : 1 to 624 MHz : 2 with
4713          * maximum bus limit 408 MHz will be executed as 408 => 204 => 312 MHz,
4714          * and there is no way to avoid rate dip in this case).
4715          */
4716         if (new_parent != c->parent) {
4717                 int interim_div = 0;
4718                 /* Switching to pll_high may over-clock bus if current divider
4719                    is too small - increase divider to safe value */
4720                 if ((new_parent == c->u.periph.pll_high) &&
4721                     (c->div < c->u.periph.min_div_high))
4722                         interim_div = c->u.periph.min_div_high;
4723
4724                 /* Switching to pll_low may dip down rate if current divider
4725                    is too big - decrease divider as much as we can */
4726                 if ((new_parent == c->u.periph.pll_low) &&
4727                     (c->div > c->u.periph.min_div_low) &&
4728                     (c->div > c->u.periph.min_div_high))
4729                         interim_div = c->u.periph.min_div_low;
4730
4731                 if (interim_div) {
4732                         u64 interim_rate = old_rate * c->div;
4733                         do_div(interim_rate, interim_div);
4734                         ret = clk_set_rate_locked(c, interim_rate);
4735                         if (ret) {
4736                                 pr_err("Failed to set %s rate to %lu\n",
4737                                        c->name, (unsigned long)interim_rate);
4738                                 return ret;
4739                         }
4740                         pr_debug("1xbus %s: rate %lu on parent %s\n", c->name,
4741                                  clk_get_rate_locked(c), c->parent->name);
4742                 }
4743
4744                 ret = clk_set_parent_locked(c, new_parent);
4745                 if (ret) {
4746                         pr_err("Failed to set %s parent %s\n",
4747                                c->name, new_parent->name);
4748                         return ret;
4749                 }
4750
4751                 old_rate = clk_get_rate_locked(c);
4752                 pr_debug("1xbus %s: rate %lu on parent %s\n", c->name,
4753                          old_rate, c->parent->name);
4754                 if (rate == old_rate)
4755                         return 0;
4756         }
4757
4758         ret = clk_set_rate_locked(c, rate);
4759         if (ret) {
4760                 pr_err("Failed to set %s rate to %lu\n", c->name, rate);
4761                 return ret;
4762         }
4763         pr_debug("1xbus %s: rate %lu on parent %s\n", c->name,
4764                  clk_get_rate_locked(c), c->parent->name);
4765         return 0;
4766
4767 }
4768
4769 static struct clk_ops tegra_1xbus_clk_ops = {
4770         .init                   = &tegra21_periph_clk_init,
4771         .enable                 = &tegra21_periph_clk_enable,
4772         .disable                = &tegra21_periph_clk_disable,
4773         .set_parent             = &tegra21_periph_clk_set_parent,
4774         .set_rate               = &tegra21_1xbus_set_rate,
4775         .round_rate             = &tegra21_1xbus_round_rate,
4776         .round_rate_updown      = &tegra21_1xbus_round_updown,
4777         .reset                  = &tegra21_periph_clk_reset,
4778         .shared_bus_update      = &tegra21_clk_1xbus_update,
4779 };
4780
4781 /* Periph VI extended clock configuration ops */
4782 static int
4783 tegra21_vi_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
4784 {
4785         if (p == TEGRA_CLK_VI_INP_SEL) {
4786                 u32 val = clk_readl(c->reg);
4787                 val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
4788                 val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
4789                         PERIPH_CLK_VI_SEL_EX_MASK;
4790                 clk_writel(val, c->reg);
4791                 return 0;
4792         }
4793         return -EINVAL;
4794 }
4795
4796 static struct clk_ops tegra_vi_clk_ops = {
4797         .init                   = &tegra21_periph_clk_init,
4798         .enable                 = &tegra21_periph_clk_enable,
4799         .disable                = &tegra21_periph_clk_disable,
4800         .set_parent             = &tegra21_periph_clk_set_parent,
4801         .set_rate               = &tegra21_periph_clk_set_rate,
4802         .round_rate             = &tegra21_periph_clk_round_rate,
4803         .clk_cfg_ex             = &tegra21_vi_clk_cfg_ex,
4804         .reset                  = &tegra21_periph_clk_reset,
4805 };
4806
4807 /* SOR clocks operations */
4808 static void tegra21_sor_brick_init(struct clk *c)
4809 {
4810         /*
4811          * Brick configuration is under DC driver control - wiil be in sync
4812          * with h/w after DC SOR init.
4813          */
4814         c->parent = c->inputs[0].input;
4815         c->state = c->parent->state;
4816         c->div = 10;
4817         c->mul = 10;
4818         return;
4819 }
4820
4821 static int tegra21_sor_brick_set_rate(struct clk *c, unsigned long rate)
4822 {
4823         /*
4824          * Brick rate is under DC driver control, this interface is used for
4825          * information  purposes, as well as for DVFS update.
4826          */
4827         unsigned long parent_rate = clk_get_rate(c->parent);
4828         u64 mul = 10;
4829         c->div = 10;    /* brick link divisor */
4830
4831         mul = mul * rate + parent_rate / 2;
4832         do_div(mul, parent_rate);
4833         c->mul = mul;
4834
4835         return 0;
4836 }
4837
4838 static int tegra21_sor_brick_set_parent(struct clk *c, struct clk *p)
4839 {
4840         u32 val;
4841         const struct clk_mux_sel *sel;
4842
4843         if (!(c->flags & MUX))
4844                 return (p == c->parent) ? 0 : (-EINVAL);
4845         /*
4846          * Brick parent selection is under DC driver control, this interface
4847          * is used to propagate enable/disable relations, for information
4848          * purposes, as well as for DVFS update.
4849          */
4850         for (sel = c->inputs; sel->input != NULL; sel++) {
4851                 if (sel->input == p) {
4852                         if (c->refcnt) {
4853                                 tegra_clk_prepare_enable(p);
4854                                 tegra_clk_disable_unprepare(c->parent);
4855                         }
4856                         clk_reparent(c, p);
4857                         return 0;
4858                 }
4859         }
4860         return -EINVAL;
4861 }
4862
4863 static struct clk_ops tegra_sor_brick_ops = {
4864         .init                   = &tegra21_sor_brick_init,
4865         .enable                 = &tegra21_periph_clk_enable,
4866         .disable                = &tegra21_periph_clk_disable,
4867         .set_parent             = &tegra21_sor_brick_set_parent,
4868         .set_rate               = &tegra21_sor_brick_set_rate,
4869 };
4870
4871 static void tegra21_sor0_clk_init(struct clk *c)
4872 {
4873         c->u.periph.src_mask = PERIPH_CLK_SOR0_CLK_SEL_MASK;
4874         c->u.periph.src_shift = PERIPH_CLK_SOR_CLK_SEL_SHIFT;
4875         tegra21_periph_clk_init(c);
4876 }
4877
4878 static int
4879 tegra21_sor0_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
4880 {
4881         if (p == TEGRA_CLK_SOR_CLK_SEL) {
4882                 int i = setting ? 1 : 0;
4883                 return clk_set_parent_locked(c, c->inputs[i].input);
4884         }
4885         return -EINVAL;
4886 }
4887
4888 static struct clk_ops tegra_sor0_clk_ops = {
4889         .init                   = &tegra21_sor0_clk_init,
4890         .enable                 = &tegra21_periph_clk_enable,
4891         .disable                = &tegra21_periph_clk_disable,
4892         .set_parent             = &tegra21_periph_clk_set_parent,
4893         .clk_cfg_ex             = &tegra21_sor0_clk_cfg_ex,
4894         .reset                  = &tegra21_periph_clk_reset,
4895 };
4896
4897 static void tegra21_sor1_clk_init(struct clk *c)
4898 {
4899         c->u.periph.src_mask = PERIPH_CLK_SOR1_CLK_SEL_MASK;
4900         c->u.periph.src_shift = PERIPH_CLK_SOR_CLK_SEL_SHIFT;
4901         tegra21_periph_clk_init(c);
4902 }
4903
4904 static int tegra21_sor1_clk_set_parent(struct clk *c, struct clk *p)
4905 {
4906         u32 val;
4907         unsigned long flags;
4908         const struct clk_mux_sel *sel;
4909         struct clk *src = c->inputs[2].input;   /* sor1_src entry in sor1 mux */
4910
4911         for (sel = c->inputs; sel->input != NULL; sel++) {
4912                 if (sel->input == p) {
4913                         if (c->refcnt)
4914                                 tegra_clk_prepare_enable(p);
4915                         /*
4916                          * Since sor1 output mux control setting share the same
4917                          * register with source mux/divider switch, use switch
4918                          * lock to protect r-m-w of output mux controls
4919                          */
4920                         clk_lock_save(src, &flags);
4921                         val = clk_readl(c->reg);
4922                         val &= ~periph_clk_source_mask(c);
4923                         val |= (sel->value << periph_clk_source_shift(c));
4924                         clk_writel_delay(val, c->reg);
4925                         clk_unlock_restore(src, &flags);
4926
4927                         if (c->refcnt)
4928                                 tegra_clk_disable_unprepare(c->parent);
4929
4930                         clk_reparent(c, p);
4931                         return 0;
4932                 }
4933         }
4934         return -EINVAL;
4935 }
4936
4937 static int
4938 tegra21_sor1_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
4939 {
4940
4941         if ((p != TEGRA_CLK_SOR_CLK_SEL) || (setting > 3))
4942                 return -EINVAL;
4943         /*
4944          * If LSb is set, sor1_brick is selected regardless of MSb. As such
4945          * setting 3 and 1 result in the same clock source. The set parent api
4946          * always selects the 1st match for requested parent, so effectively
4947          * setting 3 is reduced to 1.
4948          */
4949         return clk_set_parent_locked(c, c->inputs[setting].input);
4950 }
4951
4952 static struct clk_ops tegra_sor1_clk_ops = {
4953         .init                   = &tegra21_sor1_clk_init,
4954         .enable                 = &tegra21_periph_clk_enable,
4955         .disable                = &tegra21_periph_clk_disable,
4956         .set_parent             = &tegra21_sor1_clk_set_parent,
4957         .clk_cfg_ex             = &tegra21_sor1_clk_cfg_ex,
4958         .reset                  = &tegra21_periph_clk_reset,
4959 };
4960
4961 /* Periph DTV extended clock configuration ops */
4962 static int
4963 tegra21_dtv_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
4964 {
4965         if (p == TEGRA_CLK_DTV_INVERT) {
4966                 u32 val = clk_readl(c->reg);
4967                 if (setting)
4968                         val |= PERIPH_CLK_DTV_POLARITY_INV;
4969                 else
4970                         val &= ~PERIPH_CLK_DTV_POLARITY_INV;
4971                 clk_writel(val, c->reg);
4972                 return 0;
4973         }
4974         return -EINVAL;
4975 }
4976
4977 static struct clk_ops tegra_dtv_clk_ops = {
4978         .init                   = &tegra21_periph_clk_init,
4979         .enable                 = &tegra21_periph_clk_enable,
4980         .disable                = &tegra21_periph_clk_disable,
4981         .set_parent             = &tegra21_periph_clk_set_parent,
4982         .set_rate               = &tegra21_periph_clk_set_rate,
4983         .round_rate             = &tegra21_periph_clk_round_rate,
4984         .clk_cfg_ex             = &tegra21_dtv_clk_cfg_ex,
4985         .reset                  = &tegra21_periph_clk_reset,
4986 };
4987
4988 static void tegra21_dpaux_clk_init(struct clk *c)
4989 {
4990         c->mul = 1;
4991         c->div = 17;;
4992         tegra21_periph_clk_init(c);
4993 }
4994
4995 static struct clk_ops tegra_dpaux_clk_ops = {
4996         .init                   = &tegra21_dpaux_clk_init,
4997         .enable                 = &tegra21_periph_clk_enable,
4998         .disable                = &tegra21_periph_clk_disable,
4999         .set_parent             = &tegra21_periph_clk_set_parent,
5000         .set_rate               = &tegra21_periph_clk_set_rate,
5001         .reset                  = &tegra21_periph_clk_reset,
5002 };
5003
5004 /* XUSB SS clock ops */
5005 static DEFINE_SPINLOCK(xusb_ss_lock);
5006
5007 static int tegra21_xusb_ss_set_rate(struct clk *c, unsigned long rate)
5008 {
5009         int ret;
5010         unsigned long flags;
5011
5012         spin_lock_irqsave(&xusb_ss_lock, flags);
5013         ret = tegra21_periph_clk_set_rate(c, rate);
5014         spin_unlock_irqrestore(&xusb_ss_lock, flags);
5015         return ret;
5016 }
5017
5018 static int tegra21_xusb_ss_set_parent(struct clk *c, struct clk *p)
5019 {
5020         u32 val;
5021         unsigned long flags;
5022         const struct clk_mux_sel *sel;
5023         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
5024
5025         for (sel = c->inputs; sel->input != NULL; sel++) {
5026                 if (sel->input == p) {
5027                         if (c->refcnt)
5028                                 tegra_clk_prepare_enable(p);
5029
5030                         spin_lock_irqsave(&xusb_ss_lock, flags);
5031                         val = clk_readl(c->reg);
5032                         val &= ~periph_clk_source_mask(c);
5033                         val |= (sel->value << periph_clk_source_shift(c));
5034                         clk_writel_delay(val, c->reg);
5035                         spin_unlock_irqrestore(&xusb_ss_lock, flags);
5036
5037                         if (c->refcnt && c->parent)
5038                                 tegra_clk_disable_unprepare(c->parent);
5039
5040                         clk_reparent(c, p);
5041                         return 0;
5042                 }
5043         }
5044
5045         return -EINVAL;
5046 }
5047
5048 static struct clk_ops tegra_xusb_ss_ops = {
5049         .init                   = &tegra21_periph_clk_init,
5050         .enable                 = &tegra21_periph_clk_enable,
5051         .disable                = &tegra21_periph_clk_disable,
5052         .set_parent             = &tegra21_xusb_ss_set_parent,
5053         .set_rate               = &tegra21_xusb_ss_set_rate,
5054         .round_rate             = &tegra21_periph_clk_round_rate,
5055         .reset                  = &tegra21_periph_clk_reset,
5056 };
5057
5058 /* pciex clock support only reset function */
5059 static void tegra21_pciex_clk_init(struct clk *c)
5060 {
5061         c->state = c->parent->state;
5062 }
5063
5064 static int tegra21_pciex_clk_enable(struct clk *c)
5065 {
5066         return 0;
5067 }
5068
5069 static void tegra21_pciex_clk_disable(struct clk *c)
5070 {
5071 }
5072
5073 static int tegra21_pciex_clk_set_rate(struct clk *c, unsigned long rate)
5074 {
5075         unsigned long parent_rate = clk_get_rate(c->parent);
5076
5077         /*
5078          * the only supported pcie configurations:
5079          * Gen1: plle = 100MHz, link at 250MHz
5080          * Gen2: plle = 100MHz, link at 500MHz
5081          */
5082         if (parent_rate == 100000000) {
5083                 if (rate == 500000000) {
5084                         c->mul = 5;
5085                         c->div = 1;
5086                         return 0;
5087                 } else if (rate == 250000000) {
5088                         c->mul = 5;
5089                         c->div = 2;
5090                         return 0;
5091                 }
5092         }
5093         return -EINVAL;
5094 }
5095
5096 static struct clk_ops tegra_pciex_clk_ops = {
5097         .init     = tegra21_pciex_clk_init,
5098         .enable   = tegra21_pciex_clk_enable,
5099         .disable  = tegra21_pciex_clk_disable,
5100         .set_rate = tegra21_pciex_clk_set_rate,
5101         .reset    = tegra21_periph_clk_reset,
5102 };
5103
5104
5105 /* SDMMC2 and SDMMC4 ops */
5106 static u32 sdmmc24_input_to_lj[] = {
5107         /* 0, 1, 2, 3, 4, 5, 6, 7 */
5108            0, 1, 2, 1, 5, 5, 6, 2
5109 };
5110
5111 static u32 sdmmc24_input_from_lj[] = {
5112         /* 0, 1, 2, 3, 4, 5, 6, 7 */
5113            0, 3, 7, 3, 4, 4, 6, 7
5114 };
5115
5116 /*
5117  * The same PLLC4 output branches connected to the low jitter (LJ) and divided
5118  * inputs of the SDMMC2/4 source selection mux. The difference is that clock
5119  * from LJ input does not go through SDMMC2/4 divider. Although it is possible
5120  * to run output clock at the same rate as source from divided input (with
5121  * divider 1:1 setting), the implementation below automatically switches to LJ
5122  * input when SDMMC2/4 clock rate is set equal to the source rate, and one of
5123  * PLLC4 branches is used as a source. Changing SDMMC rate-to-source ratio
5124  * from 1:1 automatically selects divided input. This switching mechanism has
5125  * no effect when PLLP or CLK_M is used as clock source.
5126  *
5127  * See also mux_pllp_clk_m_pllc4_out2_out1_out0_lj definition for detailed list
5128  * of LJ and divided inputs.
5129 */
5130
5131 static void sdmmc24_remap_inputs(struct clk *c)
5132 {
5133         u32 sel_out, sel_in;
5134         u32 val = clk_readl(c->reg);
5135
5136         sel_in = (val & periph_clk_source_mask(c)) >>
5137                 periph_clk_source_shift(c);
5138
5139         sel_out = (c->mul == c->div) ? sdmmc24_input_to_lj[sel_in] :
5140                 sdmmc24_input_from_lj[sel_in];
5141
5142         if (sel_out != sel_in) {
5143                 val &= ~periph_clk_source_mask(c);
5144                 val |= (sel_out << periph_clk_source_shift(c));
5145
5146                 clk_writel_delay(val, c->reg);
5147         }
5148
5149 }
5150
5151 static void tegra21_sdmmc24_clk_init(struct clk *c)
5152 {
5153         tegra21_periph_clk_init(c);
5154         sdmmc24_remap_inputs(c);
5155 }
5156
5157 static int tegra21_sdmmc24_clk_set_rate(struct clk *c, unsigned long rate)
5158 {
5159         int ret = tegra21_periph_clk_set_rate(c, rate);
5160         if (!ret)
5161                 sdmmc24_remap_inputs(c);
5162         return ret;
5163 }
5164
5165 static int tegra21_sdmmc24_clk_set_parent(struct clk *c, struct clk *p)
5166 {
5167         u32 val, sel_val;
5168         const struct clk_mux_sel *sel;
5169         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
5170
5171         for (sel = c->inputs; sel->input != NULL; sel++) {
5172                 if (sel->input == p) {
5173                         sel_val = (c->mul == c->div) ?
5174                                 sdmmc24_input_to_lj[sel->value] :
5175                                 sdmmc24_input_from_lj[sel->value];
5176
5177                         val = clk_readl(c->reg);
5178                         val &= ~periph_clk_source_mask(c);
5179                         val |= (sel_val << periph_clk_source_shift(c));
5180
5181                         if (c->refcnt)
5182                                 tegra_clk_prepare_enable(p);
5183
5184                         clk_writel_delay(val, c->reg);
5185
5186                         if (c->refcnt && c->parent)
5187                                 tegra_clk_disable_unprepare(c->parent);
5188
5189                         clk_reparent(c, p);
5190                         return 0;
5191                 }
5192         }
5193         return -EINVAL;
5194 }
5195
5196 static struct clk_ops tegra_sdmmc24_clk_ops = {
5197         .init                   = &tegra21_sdmmc24_clk_init,
5198         .enable                 = &tegra21_periph_clk_enable,
5199         .disable                = &tegra21_periph_clk_disable,
5200         .set_parent             = &tegra21_sdmmc24_clk_set_parent,
5201         .set_rate               = &tegra21_sdmmc24_clk_set_rate,
5202         .round_rate             = &tegra21_periph_clk_round_rate,
5203         .reset                  = &tegra21_periph_clk_reset,
5204 };
5205
5206 /* SLCG clock ops */
5207 static DEFINE_SPINLOCK(clk_slcg_lock);
5208
5209 static void tegra21_clk_slcg_init(struct clk *c)
5210 {
5211         char *end;
5212         char root_name[32];
5213         u32 val = clk_readl(c->reg);
5214
5215         c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
5216
5217         strcpy(root_name, c->name);
5218         end = strstr(root_name, "_slcg_ovr");
5219         if (end) {
5220                 *end = '\0';
5221                 c->parent = tegra_get_clock_by_name(root_name);
5222         }
5223
5224         if (WARN(!c->parent, "%s: %s parent %s not found\n",
5225                  __func__, c->name, root_name))
5226                 return;
5227
5228         c->max_rate = c->parent->max_rate;
5229 }
5230
5231
5232 static int tegra21_clk_slcg_enable(struct clk *c)
5233 {
5234         u32 val;
5235         unsigned long flags;
5236
5237         pr_debug("%s on clock %s\n", __func__, c->name);
5238
5239         spin_lock_irqsave(&clk_slcg_lock, flags);
5240         val = clk_readl(c->reg);
5241         val |= (0x1 << c->u.periph.clk_num);
5242         clk_writel_delay(val, c->reg);
5243         spin_unlock_irqrestore(&clk_slcg_lock, flags);
5244
5245         return 0;
5246 }
5247
5248 static void tegra21_clk_slcg_disable(struct clk *c)
5249 {
5250         u32 val;
5251         unsigned long flags;
5252
5253         pr_debug("%s on clock %s\n", __func__, c->name);
5254
5255         spin_lock_irqsave(&clk_slcg_lock, flags);
5256         val = clk_readl(c->reg);
5257         val &= ~(0x1 << c->u.periph.clk_num);
5258         clk_writel_delay(val, c->reg);
5259         spin_unlock_irqrestore(&clk_slcg_lock, flags);
5260 }
5261
5262 static struct clk_ops tegra_clk_slcg_ops = {
5263         .init                   = &tegra21_clk_slcg_init,
5264         .enable                 = &tegra21_clk_slcg_enable,
5265         .disable                = &tegra21_clk_slcg_disable,
5266 };
5267
5268 /* Output clock ops */
5269 static DEFINE_SPINLOCK(clk_out_lock);
5270
5271 static void tegra21_clk_out_init(struct clk *c)
5272 {
5273         const struct clk_mux_sel *mux = NULL;
5274         const struct clk_mux_sel *sel;
5275         u32 val = pmc_readl(c->reg);
5276
5277         c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
5278         c->mul = 1;
5279         c->div = 1;
5280
5281         for (sel = c->inputs; sel->input != NULL; sel++) {
5282                 if (((val & periph_clk_source_mask(c)) >>
5283                      periph_clk_source_shift(c)) == sel->value)
5284                         mux = sel;
5285         }
5286         BUG_ON(!mux);
5287         c->parent = mux->input;
5288 }
5289
5290 static int tegra21_clk_out_enable(struct clk *c)
5291 {
5292         u32 val;
5293         unsigned long flags;
5294
5295         pr_debug("%s on clock %s\n", __func__, c->name);
5296
5297         spin_lock_irqsave(&clk_out_lock, flags);
5298         val = pmc_readl(c->reg);
5299         val |= (0x1 << c->u.periph.clk_num);
5300         pmc_writel(val, c->reg);
5301         pmc_readl(c->reg);
5302         spin_unlock_irqrestore(&clk_out_lock, flags);
5303
5304         return 0;
5305 }
5306
5307 static void tegra21_clk_out_disable(struct clk *c)
5308 {
5309         u32 val;
5310         unsigned long flags;
5311
5312         pr_debug("%s on clock %s\n", __func__, c->name);
5313
5314         spin_lock_irqsave(&clk_out_lock, flags);
5315         val = pmc_readl(c->reg);
5316         val &= ~(0x1 << c->u.periph.clk_num);
5317         pmc_writel(val, c->reg);
5318         pmc_readl(c->reg);
5319         spin_unlock_irqrestore(&clk_out_lock, flags);
5320 }
5321
5322 static int tegra21_clk_out_set_parent(struct clk *c, struct clk *p)
5323 {
5324         u32 val;
5325         unsigned long flags;
5326         const struct clk_mux_sel *sel;
5327
5328         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
5329
5330         for (sel = c->inputs; sel->input != NULL; sel++) {
5331                 if (sel->input == p) {
5332                         if (c->refcnt)
5333                                 tegra_clk_prepare_enable(p);
5334
5335                         spin_lock_irqsave(&clk_out_lock, flags);
5336                         val = pmc_readl(c->reg);
5337                         val &= ~periph_clk_source_mask(c);
5338                         val |= (sel->value << periph_clk_source_shift(c));
5339                         pmc_writel(val, c->reg);
5340                         pmc_readl(c->reg);
5341                         spin_unlock_irqrestore(&clk_out_lock, flags);
5342
5343                         if (c->refcnt && c->parent)
5344                                 tegra_clk_disable_unprepare(c->parent);
5345
5346                         clk_reparent(c, p);
5347                         return 0;
5348                 }
5349         }
5350         return -EINVAL;
5351 }
5352
5353 static struct clk_ops tegra_clk_out_ops = {
5354         .init                   = &tegra21_clk_out_init,
5355         .enable                 = &tegra21_clk_out_enable,
5356         .disable                = &tegra21_clk_out_disable,
5357         .set_parent             = &tegra21_clk_out_set_parent,
5358 };
5359
5360
5361 /* External memory controller clock ops */
5362 static void tegra21_emc_clk_init(struct clk *c)
5363 {
5364         tegra21_periph_clk_init(c);
5365         tegra_emc_dram_type_init(c);
5366 }
5367
5368 static long tegra21_emc_clk_round_updown(struct clk *c, unsigned long rate,
5369                                          bool up)
5370 {
5371         unsigned long new_rate = max(rate, c->min_rate);
5372
5373         new_rate = tegra_emc_round_rate_updown(new_rate, up);
5374         if (IS_ERR_VALUE(new_rate))
5375                 new_rate = c->max_rate;
5376
5377         return new_rate;
5378 }
5379
5380 static long tegra21_emc_clk_round_rate(struct clk *c, unsigned long rate)
5381 {
5382         return tegra21_emc_clk_round_updown(c, rate, true);
5383 }
5384
5385 void tegra_mc_divider_update(struct clk *emc)
5386 {
5387         u32 val = clk_readl(emc->reg) &
5388                 (PERIPH_CLK_SOURCE_EMC_MC_SAME | PERIPH_CLK_SOURCE_EMC_DIV2_EN);
5389         emc->child_bus->div = val == 1 ? 4 : (val == 2 ? 1 : 2);
5390 }
5391
5392 static int tegra21_emc_clk_set_rate(struct clk *c, unsigned long rate)
5393 {
5394         int ret;
5395         u32 div_value;
5396         struct clk *p;
5397
5398         /* The tegra21x memory controller has an interlock with the clock
5399          * block that allows memory shadowed registers to be updated,
5400          * and then transfer them to the main registers at the same
5401          * time as the clock update without glitches. During clock change
5402          * operation both clock parent and divider may change simultaneously
5403          * to achieve requested rate. */
5404         p = tegra_emc_predict_parent(rate, &div_value);
5405         div_value += 2;         /* emc has fractional DIV_U71 divider */
5406         if (IS_ERR_OR_NULL(p)) {
5407                 pr_err("%s: Failed to predict emc parent for rate %lu\n",
5408                        __func__, rate);
5409                 return -EINVAL;
5410         }
5411
5412         if (p == c->parent) {
5413                 if (div_value == c->div)
5414                         return 0;
5415         } else if (c->refcnt)
5416                 tegra_clk_prepare_enable(p);
5417
5418         ret = tegra_emc_set_rate_on_parent(rate, p);
5419         if (ret < 0)
5420                 return ret;
5421
5422         if (p != c->parent) {
5423                 if(c->refcnt && c->parent)
5424                         tegra_clk_disable_unprepare(c->parent);
5425                 clk_reparent(c, p);
5426         }
5427         c->div = div_value;
5428         c->mul = 2;
5429         return 0;
5430 }
5431
5432 static int tegra21_clk_emc_bus_update(struct clk *bus)
5433 {
5434         struct clk *p = NULL;
5435         unsigned long rate, parent_rate, backup_rate;
5436
5437         if (detach_shared_bus)
5438                 return 0;
5439
5440         rate = tegra21_clk_shared_bus_update(bus, NULL, NULL, NULL);
5441
5442         if (rate == clk_get_rate_locked(bus))
5443                 return 0;
5444
5445         return clk_set_rate_locked(bus, rate);
5446 }
5447
5448 static struct clk_ops tegra_emc_clk_ops = {
5449         .init                   = &tegra21_emc_clk_init,
5450         .enable                 = &tegra21_periph_clk_enable,
5451         .disable                = &tegra21_periph_clk_disable,
5452         .set_rate               = &tegra21_emc_clk_set_rate,
5453         .round_rate             = &tegra21_emc_clk_round_rate,
5454         .round_rate_updown      = &tegra21_emc_clk_round_updown,
5455         .reset                  = &tegra21_periph_clk_reset,
5456         .shared_bus_update      = &tegra21_clk_emc_bus_update,
5457 };
5458
5459 static void tegra21_mc_clk_init(struct clk *c)
5460 {
5461         c->state = ON;
5462         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
5463                 c->state = OFF;
5464
5465         c->parent->child_bus = c;
5466         tegra_mc_divider_update(c->parent);
5467         c->mul = 1;
5468 }
5469
5470 static struct clk_ops tegra_mc_clk_ops = {
5471         .init                   = &tegra21_mc_clk_init,
5472         .enable                 = &tegra21_periph_clk_enable,
5473         .disable                = &tegra21_periph_clk_disable,
5474 };
5475
5476 /*
5477  * EMC parent selection algorithm always keeps PLLM and/or PLLMB disabled when
5478  * the respective PLL is not used as EMC clock source. However, boot-loader may
5479  * leave both PLLs enabled. To allow proper start of the algorithm, make sure
5480  * PLLs are disabled if not used.
5481  */
5482 static void tegra21_emc_sync_plls(struct clk *emc,
5483                                   struct clk *pllm, struct clk *pllmb)
5484 {
5485         if (emc->parent != pllm) {
5486                 if (pllm->state == ON) {
5487                         pllm->ops->disable(pllm);
5488                         pllm->state = OFF;
5489                 }
5490         }
5491
5492         if (emc->parent != pllmb) {
5493                 if (pllmb->state == ON) {
5494                         pllmb->ops->disable(pllmb);
5495                         pllmb->state = OFF;
5496                 }
5497         }
5498 }
5499
5500 #ifdef CONFIG_PM_SLEEP
5501
5502 /*
5503  * WAR: fixing SC7 entry rate is to ensure stable boot up after SC7 resume on
5504  * LP4 platforms. This ensures that there are no MRWs necesary by the bootrom
5505  * when waking the DRAM (since the MRs will already have valid settings for 204
5506  * MHz). Problem is that the BR is not aware of what FSP is in use.
5507  */
5508 #define FIXED_SC7_ENTRY_RATE    204000000
5509
5510 static void tegra21_emc_clk_suspend(struct clk *c, unsigned long rate)
5511 {
5512         /* No change if other than LPDDR4 */
5513         if (tegra_emc_get_dram_type() != DRAM_TYPE_LPDDR4)
5514                 return;
5515
5516         /* No change in emc configuration for LP1 */
5517         if (!tegra_is_lp0_suspend_mode())
5518                 return;
5519
5520         /*
5521          * Scale EMC rate at boot rate - required for entering SC7(LP0)
5522          * on LPDDR4.
5523          */
5524         if (rate != FIXED_SC7_ENTRY_RATE)
5525                 tegra21_emc_clk_set_rate(c, FIXED_SC7_ENTRY_RATE);
5526 }
5527 #endif
5528
5529 /* Clock doubler ops (non-atomic shared register access) */
5530 static DEFINE_SPINLOCK(doubler_lock);
5531
5532 static void tegra21_clk_double_init(struct clk *c)
5533 {
5534         u32 val = clk_readl(c->reg);
5535         c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
5536         c->div = 1;
5537         c->state = ON;
5538         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
5539                 c->state = OFF;
5540 };
5541
5542 static int tegra21_clk_double_set_rate(struct clk *c, unsigned long rate)
5543 {
5544         u32 val;
5545         unsigned long parent_rate = clk_get_rate(c->parent);
5546         unsigned long flags;
5547
5548         if (rate == parent_rate) {
5549                 spin_lock_irqsave(&doubler_lock, flags);
5550                 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
5551                 clk_writel(val, c->reg);
5552                 c->mul = 1;
5553                 c->div = 1;
5554                 spin_unlock_irqrestore(&doubler_lock, flags);
5555                 return 0;
5556         } else if (rate == 2 * parent_rate) {
5557                 spin_lock_irqsave(&doubler_lock, flags);
5558                 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
5559                 clk_writel(val, c->reg);
5560                 c->mul = 2;
5561                 c->div = 1;
5562                 spin_unlock_irqrestore(&doubler_lock, flags);
5563                 return 0;
5564         }
5565         return -EINVAL;
5566 }
5567
5568 static struct clk_ops tegra_clk_double_ops = {
5569         .init                   = &tegra21_clk_double_init,
5570         .enable                 = &tegra21_periph_clk_enable,
5571         .disable                = &tegra21_periph_clk_disable,
5572         .set_rate               = &tegra21_clk_double_set_rate,
5573 };
5574
5575 /* Audio sync clock ops */
5576 static int tegra21_sync_source_set_rate(struct clk *c, unsigned long rate)
5577 {
5578         c->rate = rate;
5579         return 0;
5580 }
5581
5582 static struct clk_ops tegra_sync_source_ops = {
5583         .set_rate               = &tegra21_sync_source_set_rate,
5584 };
5585
5586 static void tegra21_audio_sync_clk_init(struct clk *c)
5587 {
5588         int source;
5589         const struct clk_mux_sel *sel;
5590         u32 val = clk_readl(c->reg);
5591         c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
5592         source = val & AUDIO_SYNC_SOURCE_MASK;
5593         for (sel = c->inputs; sel->input != NULL; sel++)
5594                 if (sel->value == source)
5595                         break;
5596         BUG_ON(sel->input == NULL);
5597         c->parent = sel->input;
5598 }
5599
5600 static int tegra21_audio_sync_clk_enable(struct clk *c)
5601 {
5602         u32 val = clk_readl(c->reg);
5603         clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
5604         return 0;
5605 }
5606
5607 static void tegra21_audio_sync_clk_disable(struct clk *c)
5608 {
5609         u32 val = clk_readl(c->reg);
5610         clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
5611 }
5612
5613 static int tegra21_audio_sync_clk_set_parent(struct clk *c, struct clk *p)
5614 {
5615         u32 val;
5616         const struct clk_mux_sel *sel;
5617         for (sel = c->inputs; sel->input != NULL; sel++) {
5618                 if (sel->input == p) {
5619                         val = clk_readl(c->reg);
5620                         val &= ~AUDIO_SYNC_SOURCE_MASK;
5621                         val |= sel->value;
5622
5623                         if (c->refcnt)
5624                                 tegra_clk_prepare_enable(p);
5625
5626                         clk_writel(val, c->reg);
5627
5628                         if (c->refcnt && c->parent)
5629                                 tegra_clk_disable_unprepare(c->parent);
5630
5631                         clk_reparent(c, p);
5632                         return 0;
5633                 }
5634         }
5635
5636         return -EINVAL;
5637 }
5638
5639 static struct clk_ops tegra_audio_sync_clk_ops = {
5640         .init       = tegra21_audio_sync_clk_init,
5641         .enable     = tegra21_audio_sync_clk_enable,
5642         .disable    = tegra21_audio_sync_clk_disable,
5643         .set_parent = tegra21_audio_sync_clk_set_parent,
5644 };
5645
5646 /* cml0 (pcie), and cml1 (sata) clock ops */
5647 static void tegra21_cml_clk_init(struct clk *c)
5648 {
5649         u32 val = clk_readl(c->reg);
5650         c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
5651 }
5652
5653 static int tegra21_cml_clk_enable(struct clk *c)
5654 {
5655         u32 val;
5656         unsigned long flags;
5657         struct clk *parent = c->parent;
5658
5659         clk_lock_save(parent, &flags);
5660         val = clk_readl(c->reg);
5661         val |= (0x1 << c->u.periph.clk_num);
5662         clk_writel(val, c->reg);
5663         clk_unlock_restore(parent, &flags);
5664
5665         return 0;
5666 }
5667
5668 static void tegra21_cml_clk_disable(struct clk *c)
5669 {
5670         u32 val;
5671         unsigned long flags;
5672         struct clk *parent = c->parent;
5673
5674         clk_lock_save(parent, &flags);
5675         val = clk_readl(c->reg);
5676         val &= ~(0x1 << c->u.periph.clk_num);
5677         clk_writel(val, c->reg);
5678         clk_unlock_restore(parent, &flags);
5679 }
5680
5681 static struct clk_ops tegra_cml_clk_ops = {
5682         .init                   = &tegra21_cml_clk_init,
5683         .enable                 = &tegra21_cml_clk_enable,
5684         .disable                = &tegra21_cml_clk_disable,
5685 };
5686
5687 /* cbus ops */
5688 /*
5689  * Some clocks require dynamic re-locking of source PLL in order to
5690  * achieve frequency scaling granularity that matches characterized
5691  * core voltage steps. The cbus clock creates a shared bus that
5692  * provides a virtual root for such clocks to hide and synchronize
5693  * parent PLL re-locking as well as backup operations.
5694 */
5695
5696 static void tegra21_clk_cbus_init(struct clk *c)
5697 {
5698         c->state = OFF;
5699         c->set = true;
5700 }
5701
5702 static int tegra21_clk_cbus_enable(struct clk *c)
5703 {
5704         return 0;
5705 }
5706
5707 static long tegra21_clk_cbus_round_updown(struct clk *c, unsigned long rate,
5708                                           bool up)
5709 {
5710         int i;
5711
5712         if (!c->dvfs) {
5713                 if (!c->min_rate)
5714                         c->min_rate = c->parent->min_rate;
5715                 rate = max(rate, c->min_rate);
5716                 return rate;
5717         }
5718
5719         /* update min now, since no dvfs table was available during init
5720            (skip placeholder entries set to 1 kHz) */
5721         if (!c->min_rate) {
5722                 for (i = 0; i < c->dvfs->num_freqs; i++) {
5723                         if (c->dvfs->freqs[i] > 1 * c->dvfs->freqs_mult) {
5724                                 c->min_rate = c->dvfs->freqs[i];
5725                                 break;
5726                         }
5727                 }
5728                 BUG_ON(!c->min_rate);
5729
5730                 if (c->stats.histogram) {
5731                         c->stats.histogram->rates_num = c->dvfs->num_freqs;
5732                         tegra_shared_bus_stats_allocate(c, c->stats.histogram);
5733                 }
5734         }
5735         rate = max(rate, c->min_rate);
5736
5737         for (i = 0; ; i++) {
5738                 unsigned long f = c->dvfs->freqs[i];
5739                 if ((f >= rate) || ((i + 1) >=  c->dvfs->num_freqs)) {
5740                         if (!up && i && (f > rate))
5741                                 i--;
5742                         break;
5743                 }
5744         }
5745
5746         if (c->stats.histogram)
5747                 c->stats.histogram->new_rate_idx = i;
5748         return c->dvfs->freqs[i];
5749 }
5750
5751 static long tegra21_clk_cbus_round_rate(struct clk *c, unsigned long rate)
5752 {
5753         return tegra21_clk_cbus_round_updown(c, rate, true);
5754 }
5755
5756 static int cbus_switch_one(struct clk *c, struct clk *p, u32 div, bool abort)
5757 {
5758         int ret = 0;
5759
5760         /* set new divider if it is bigger than the current one */
5761         if (c->div < c->mul * div) {
5762                 ret = clk_set_div(c, div);
5763                 if (ret) {
5764                         pr_err("%s: failed to set %s clock divider %u: %d\n",
5765                                __func__, c->name, div, ret);
5766                         if (abort)
5767                                 return ret;
5768                 }
5769         }
5770
5771         if (c->parent != p) {
5772                 ret = clk_set_parent(c, p);
5773                 if (ret) {
5774                         pr_err("%s: failed to set %s clock parent %s: %d\n",
5775                                __func__, c->name, p->name, ret);
5776                         if (abort)
5777                                 return ret;
5778                 }
5779         }
5780
5781         /* set new divider if it is smaller than the current one */
5782         if (c->div > c->mul * div) {
5783                 ret = clk_set_div(c, div);
5784                 if (ret)
5785                         pr_err("%s: failed to set %s clock divider %u: %d\n",
5786                                __func__, c->name, div, ret);
5787         }
5788
5789         return ret;
5790 }
5791
5792 static int cbus_backup(struct clk *c)
5793 {
5794         int ret;
5795         struct clk *user;
5796
5797         list_for_each_entry(user, &c->shared_bus_list,
5798                         u.shared_bus_user.node) {
5799                 struct clk *client = user->u.shared_bus_user.client;
5800                 if (client && (client->state == ON) &&
5801                     (client->parent == c->parent)) {
5802                         ret = cbus_switch_one(
5803                                 client, c->shared_bus_backup.input,
5804                                 c->shared_bus_backup.value, true);
5805                         if (ret)
5806                                 return ret;
5807                 }
5808         }
5809         return 0;
5810 }
5811
5812 static int cbus_dvfs_set_rate(struct clk *c, unsigned long rate)
5813 {
5814         int ret;
5815         struct clk *user;
5816
5817         list_for_each_entry(user, &c->shared_bus_list,
5818                         u.shared_bus_user.node) {
5819                 struct clk *client =  user->u.shared_bus_user.client;
5820                 if (client && client->refcnt && (client->parent == c->parent)) {
5821                         ret = tegra_dvfs_set_rate(client, rate);
5822                         if (ret)
5823                                 return ret;
5824                 }
5825         }
5826         return 0;
5827 }
5828
5829 static void cbus_restore(struct clk *c)
5830 {
5831         struct clk *user;
5832
5833         list_for_each_entry(user, &c->shared_bus_list,
5834                         u.shared_bus_user.node) {
5835                 struct clk *client = user->u.shared_bus_user.client;
5836                 if (client)
5837                         cbus_switch_one(client, c->parent, c->div, false);
5838         }
5839 }
5840
5841 static void cbus_skip(struct clk *c, unsigned long bus_rate)
5842 {
5843         struct clk *user;
5844         unsigned long rate;
5845
5846         if (!(c->shared_bus_flags & SHARED_BUS_USE_SKIPPERS))
5847                 return;
5848
5849         list_for_each_entry(user, &c->shared_bus_list,
5850                         u.shared_bus_user.node) {
5851                 struct clk *client = user->u.shared_bus_user.client;
5852                 if (client && client->skipper &&
5853                     user->u.shared_bus_user.enabled) {
5854                         /* Make sure skipper output is above the target */
5855                         rate = user->u.shared_bus_user.rate;
5856                         rate += bus_rate >> SUPER_SKIPPER_TERM_SIZE;
5857
5858                         clk_set_rate(client->skipper, rate);
5859                         user->div = client->skipper->div;
5860                         user->mul = client->skipper->mul;
5861                 }
5862         }
5863 }
5864
5865 static int get_next_backup_div(struct clk *c, unsigned long rate)
5866 {
5867         u32 div = c->div;
5868         unsigned long backup_rate = clk_get_rate(c->shared_bus_backup.input);
5869
5870         rate = max(rate, clk_get_rate_locked(c));
5871         rate = rate - (rate >> 2);      /* 25% margin for backup rate */
5872         if ((u64)rate * div < backup_rate)
5873                 div = DIV_ROUND_UP(backup_rate, rate);
5874
5875         BUG_ON(!div);
5876         return div;
5877 }
5878
5879 static int tegra21_clk_cbus_set_rate(struct clk *c, unsigned long rate)
5880 {
5881         int ret;
5882         bool dramp;
5883
5884         if (rate == 0)
5885                 return 0;
5886
5887         ret = tegra_clk_prepare_enable(c->parent);
5888         if (ret) {
5889                 pr_err("%s: failed to enable %s clock: %d\n",
5890                        __func__, c->name, ret);
5891                 return ret;
5892         }
5893
5894         dramp = tegra_pll_can_ramp_to_rate(c->parent, rate * c->div);
5895         if (!dramp) {
5896                 c->shared_bus_backup.value = get_next_backup_div(c, rate);
5897                 ret = cbus_backup(c);
5898                 if (ret)
5899                         goto out;
5900         }
5901
5902         ret = clk_set_rate(c->parent, rate * c->div);
5903         if (ret) {
5904                 pr_err("%s: failed to set %s clock rate %lu: %d\n",
5905                        __func__, c->name, rate, ret);
5906                 goto out;
5907         }
5908
5909         /* Safe voltage setting is taken care of by cbus clock dvfs; the call
5910          * below only records requirements for each enabled client.
5911          */
5912         if (dramp)
5913                 ret = cbus_dvfs_set_rate(c, rate);
5914
5915         cbus_restore(c);
5916         cbus_skip(c, rate);
5917
5918 out:
5919         tegra_clk_disable_unprepare(c->parent);
5920         return ret;
5921 }
5922
5923 static inline void cbus_move_enabled_user(
5924         struct clk *user, struct clk *dst, struct clk *src)
5925 {
5926         tegra_clk_prepare_enable(dst);
5927         list_move_tail(&user->u.shared_bus_user.node, &dst->shared_bus_list);
5928         tegra_clk_disable_unprepare(src);
5929         clk_reparent(user, dst);
5930 }
5931
5932 #ifdef CONFIG_TEGRA_DYNAMIC_CBUS
5933 static int tegra21_clk_cbus_update(struct clk *bus)
5934 {
5935         int ret, mv;
5936         struct clk *slow = NULL;
5937         struct clk *top = NULL;
5938         unsigned long rate;
5939         unsigned long old_rate;
5940         unsigned long ceiling;
5941
5942         if (detach_shared_bus)
5943                 return 0;
5944
5945         rate = tegra21_clk_shared_bus_update(bus, &top, &slow, &ceiling);
5946
5947         /* use dvfs table of the slowest enabled client as cbus dvfs table */
5948         if (bus->dvfs && slow && (slow != bus->u.cbus.slow_user)) {
5949                 unsigned long *dest = &bus->dvfs->freqs[0];
5950                 unsigned long *src =
5951                         &slow->u.shared_bus_user.client->dvfs->freqs[0];
5952                 memcpy(dest, src, sizeof(*dest) * bus->dvfs->num_freqs);
5953         }
5954
5955         /* update bus state variables and rate */
5956         bus->u.cbus.slow_user = slow;
5957         bus->u.cbus.top_user = top;
5958
5959         rate = tegra21_clk_cap_shared_bus(bus, rate, ceiling);
5960         mv = tegra_dvfs_predict_mv_at_hz_no_tfloor(bus, rate);
5961         if (IS_ERR_VALUE(mv))
5962                 return -EINVAL;
5963
5964         if (bus->dvfs) {
5965                 mv -= bus->dvfs->cur_millivolts;
5966                 if (bus->refcnt && (mv > 0)) {
5967                         ret = tegra_dvfs_set_rate(bus, rate);
5968                         if (ret)
5969                                 return ret;
5970                 }
5971         }
5972
5973         old_rate = clk_get_rate_locked(bus);
5974         if (IS_ENABLED(CONFIG_TEGRA_MIGRATE_CBUS_USERS) || (old_rate != rate)) {
5975                 ret = bus->ops->set_rate(bus, rate);
5976                 if (ret)
5977                         return ret;
5978         } else {
5979                 /* Skippers may change even if bus rate is the same */
5980                 cbus_skip(bus, rate);
5981         }
5982
5983         if (bus->dvfs) {
5984                 if (bus->refcnt && (mv <= 0)) {
5985                         ret = tegra_dvfs_set_rate(bus, rate);
5986                         if (ret)
5987                                 return ret;
5988                 }
5989         }
5990
5991         clk_rate_change_notify(bus, rate);
5992         return 0;
5993 };
5994 #else
5995 static int tegra21_clk_cbus_update(struct clk *bus)
5996 {
5997         unsigned long rate, old_rate;
5998
5999         if (detach_shared_bus)
6000                 return 0;
6001
6002         rate = tegra21_clk_shared_bus_update(bus, NULL, NULL, NULL);
6003
6004         old_rate = clk_get_rate_locked(bus);
6005         if (rate == old_rate) {
6006                 /* Skippers may change even if bus rate is the same */
6007                 cbus_skip(bus, rate);
6008                 return 0;
6009         }
6010
6011         return clk_set_rate_locked(bus, rate);
6012 }
6013 #endif
6014
6015 static int tegra21_clk_cbus_migrate_users(struct clk *user)
6016 {
6017 #ifdef CONFIG_TEGRA_MIGRATE_CBUS_USERS
6018         struct clk *src_bus, *dst_bus, *top_user, *c;
6019         struct list_head *pos, *n;
6020
6021         if (!user->u.shared_bus_user.client || !user->inputs)
6022                 return 0;
6023
6024         /* Dual cbus on Tegra12 */
6025         src_bus = user->inputs[0].input;
6026         dst_bus = user->inputs[1].input;
6027
6028         if (!src_bus->u.cbus.top_user && !dst_bus->u.cbus.top_user)
6029                 return 0;
6030
6031         /* Make sure top user on the source bus is requesting highest rate */
6032         if (!src_bus->u.cbus.top_user || (dst_bus->u.cbus.top_user &&
6033                 bus_user_request_is_lower(src_bus->u.cbus.top_user,
6034                                            dst_bus->u.cbus.top_user)))
6035                 swap(src_bus, dst_bus);
6036
6037         /* If top user is the slow one on its own (source) bus, do nothing */
6038         top_user = src_bus->u.cbus.top_user;
6039         BUG_ON(!top_user->u.shared_bus_user.client);
6040         if (!bus_user_is_slower(src_bus->u.cbus.slow_user, top_user))
6041                 return 0;
6042
6043         /* If source bus top user is slower than all users on destination bus,
6044            move top user; otherwise move all users slower than the top one */
6045         if (!dst_bus->u.cbus.slow_user ||
6046             !bus_user_is_slower(dst_bus->u.cbus.slow_user, top_user)) {
6047                 cbus_move_enabled_user(top_user, dst_bus, src_bus);
6048         } else {
6049                 list_for_each_safe(pos, n, &src_bus->shared_bus_list) {
6050                         c = list_entry(pos, struct clk, u.shared_bus_user.node);
6051                         if (c->u.shared_bus_user.enabled &&
6052                             c->u.shared_bus_user.client &&
6053                             bus_user_is_slower(c, top_user))
6054                                 cbus_move_enabled_user(c, dst_bus, src_bus);
6055                 }
6056         }
6057
6058         /* Update destination bus 1st (move clients), then source */
6059         tegra_clk_shared_bus_update(dst_bus);
6060         tegra_clk_shared_bus_update(src_bus);
6061 #endif
6062         return 0;
6063 }
6064
6065 static struct clk_ops tegra_clk_cbus_ops = {
6066         .init = tegra21_clk_cbus_init,
6067         .enable = tegra21_clk_cbus_enable,
6068         .set_rate = tegra21_clk_cbus_set_rate,
6069         .round_rate = tegra21_clk_cbus_round_rate,
6070         .round_rate_updown = tegra21_clk_cbus_round_updown,
6071         .shared_bus_update = tegra21_clk_cbus_update,
6072 };
6073
6074 /* shared bus ops */
6075 /*
6076  * Some clocks may have multiple downstream users that need to request a
6077  * higher clock rate.  Shared bus clocks provide a unique shared_bus_user
6078  * clock to each user.  The frequency of the bus is set to the highest
6079  * enabled shared_bus_user clock, with a minimum value set by the
6080  * shared bus.
6081  *
6082  * Optionally shared bus may support users migration. Since shared bus and
6083  * its * children (users) have reversed rate relations: user rates determine
6084  * bus rate, * switching user from one parent/bus to another may change rates
6085  * of both parents. Therefore we need a cross-bus lock on top of individual
6086  * user and bus locks. For now, limit bus switch support to cbus only if
6087  * CONFIG_TEGRA_MIGRATE_CBUS_USERS is set.
6088  */
6089
6090 static unsigned long tegra21_clk_shared_bus_update(struct clk *bus,
6091         struct clk **bus_top, struct clk **bus_slow, unsigned long *rate_cap)
6092 {
6093         struct clk *c;
6094         struct clk *slow = NULL;
6095         struct clk *top = NULL;
6096
6097         unsigned long override_rate = 0;
6098         unsigned long top_rate = 0;
6099         unsigned long rate = bus->min_rate;
6100         unsigned long bw = 0;
6101         unsigned long iso_bw = 0;
6102         unsigned long ceiling = bus->max_rate;
6103         unsigned long ceiling_but_iso = bus->max_rate;
6104         u32 usage_flags = 0;
6105         bool rate_set = false;
6106
6107         struct clk *crit_cap = NULL;
6108         struct clk *crit_cap_but_iso = NULL;
6109         struct clk *crit_floor = NULL;
6110         struct clk *override_clk = NULL;
6111
6112         list_for_each_entry(c, &bus->shared_bus_list,
6113                         u.shared_bus_user.node) {
6114                 bool cap_user = (c->u.shared_bus_user.mode == SHARED_CEILING) ||
6115                         (c->u.shared_bus_user.mode == SHARED_CEILING_BUT_ISO);
6116                 /*
6117                  * Ignore requests from disabled floor and bw users, and from
6118                  * auto-users riding the bus. Always honor ceiling users, even
6119                  * if they are disabled - we do not want to keep enabled parent
6120                  * bus just because ceiling is set.
6121                  */
6122                 if (c->u.shared_bus_user.enabled || cap_user) {
6123                         unsigned long request_rate = c->u.shared_bus_user.rate;
6124                         usage_flags |= c->u.shared_bus_user.usage_flag;
6125
6126                         if (!(c->flags & BUS_RATE_LIMIT))
6127                                 rate_set = true;
6128
6129                         switch (c->u.shared_bus_user.mode) {
6130                         case SHARED_ISO_BW:
6131                                 iso_bw += request_rate;
6132                                 if (iso_bw > bus->max_rate)
6133                                         iso_bw = bus->max_rate;
6134                                 /* fall thru */
6135                         case SHARED_BW:
6136                                 bw += request_rate;
6137                                 if (bw > bus->max_rate)
6138                                         bw = bus->max_rate;
6139                                 break;
6140                         case SHARED_CEILING_BUT_ISO:
6141                                 if (ceiling_but_iso > request_rate) {
6142                                         ceiling_but_iso = request_rate;
6143                                         crit_cap_but_iso = c;
6144                                 }
6145                                 break;
6146                         case SHARED_CEILING:
6147                                 if (ceiling > request_rate) {
6148                                         ceiling = request_rate;
6149                                         crit_cap = c;
6150                                 }
6151                                 break;
6152                         case SHARED_OVERRIDE:
6153                                 if (override_rate == 0) {
6154                                         override_rate = request_rate;
6155                                         override_clk = c;
6156                                 }
6157                                 break;
6158                         case SHARED_AUTO:
6159                                 break;
6160                         case SHARED_FLOOR:
6161                         default:
6162                                 if (rate <= request_rate) {
6163                                         if (!(c->flags & BUS_RATE_LIMIT) ||
6164                                             (rate < request_rate))
6165                                                 crit_floor = c;
6166                                         rate = request_rate;
6167                                 }
6168                                 if (c->u.shared_bus_user.client
6169                                                         && request_rate) {
6170                                         if (top_rate < request_rate) {
6171                                                 top_rate = request_rate;
6172                                                 top = c;
6173                                         } else if ((top_rate == request_rate) &&
6174                                                 bus_user_is_slower(c, top)) {
6175                                                 top = c;
6176                                         }
6177                                 }
6178                         }
6179                         if (c->u.shared_bus_user.client &&
6180                                 (!slow || bus_user_is_slower(c, slow)))
6181                                 slow = c;
6182                 }
6183         }
6184
6185         if (bus->flags & PERIPH_EMC_ENB) {
6186                 unsigned long iso_bw_min;
6187                 bw = tegra_emc_apply_efficiency(
6188                         bw, iso_bw, bus->max_rate, usage_flags, &iso_bw_min);
6189                 if (bus->ops && bus->ops->round_rate)
6190                         iso_bw_min = bus->ops->round_rate(bus, iso_bw_min);
6191                 ceiling_but_iso = max(ceiling_but_iso, iso_bw_min);
6192         }
6193
6194         if (rate < bw) {
6195                 rate = bw;
6196                 crit_floor = NULL;
6197         }
6198         rate = override_rate ? : rate;
6199
6200         if (ceiling > ceiling_but_iso) {
6201                 ceiling = ceiling_but_iso;
6202                 crit_cap = crit_cap_but_iso;
6203         }
6204
6205         if (override_rate) {
6206                 ceiling = bus->max_rate;
6207                 crit_cap = NULL;
6208                 crit_floor = override_clk;
6209         }
6210         bus->override_rate = override_rate;
6211
6212         if (bus->stats.histogram) {
6213                 struct bus_stats *stats = bus->stats.histogram;
6214
6215                 if (!rate_set) {
6216                         crit_cap = NULL;
6217                         crit_floor = NULL;
6218                 }
6219                 stats->new_cap_idx = !crit_cap ? stats->users_num :
6220                         crit_cap->u.shared_bus_user.stats_idx;
6221                 stats->new_floor_idx = !crit_floor ? stats->users_num :
6222                         crit_floor->u.shared_bus_user.stats_idx;
6223         }
6224
6225         if (bus_top && bus_slow && rate_cap) {
6226                 /* If dynamic bus dvfs table, let the caller to complete
6227                    rounding and aggregation */
6228                 *bus_top = top;
6229                 *bus_slow = slow;
6230                 *rate_cap = ceiling;
6231         } else {
6232                 /*
6233                  * If satic bus dvfs table, complete rounding and aggregation.
6234                  * In case when no user requested bus rate, and bus retention
6235                  * is enabled, don't scale down - keep current rate.
6236                  */
6237                 if (!rate_set && (bus->shared_bus_flags & SHARED_BUS_RETENTION))
6238                         rate = clk_get_rate_locked(bus);
6239
6240                 rate = tegra21_clk_cap_shared_bus(bus, rate, ceiling);
6241         }
6242
6243         return rate;
6244 };
6245
6246 static unsigned long tegra21_clk_cap_shared_bus(struct clk *bus,
6247         unsigned long rate, unsigned long ceiling)
6248 {
6249         bool capped;
6250         struct bus_stats *stats = bus->stats.histogram;
6251
6252         if (bus->ops && bus->ops->round_rate_updown)
6253                 ceiling = bus->ops->round_rate_updown(bus, ceiling, false);
6254
6255         capped = rate > ceiling;
6256         if (capped) {
6257                 rate = ceiling;
6258                 if (stats)
6259                         tegra_shared_bus_stats_update(stats,
6260                                 stats->new_cap_idx, stats->new_rate_idx);
6261         }
6262
6263         if (bus->ops && bus->ops->round_rate)
6264                 rate = bus->ops->round_rate(bus, rate);
6265
6266         if (!capped && stats) {
6267                 tegra_shared_bus_stats_update(stats,
6268                         stats->new_floor_idx, stats->new_rate_idx);
6269         }
6270
6271         return rate;
6272 }
6273
6274 static int tegra_clk_shared_bus_migrate_users(struct clk *user)
6275 {
6276         if (detach_shared_bus)
6277                 return 0;
6278
6279         /* Only cbus migration is supported */
6280         if (user->flags & PERIPH_ON_CBUS)
6281                 return tegra21_clk_cbus_migrate_users(user);
6282         return -ENOSYS;
6283 }
6284
6285 static void tegra_clk_shared_bus_user_init(struct clk *c)
6286 {
6287         c->max_rate = c->parent->max_rate;
6288         c->u.shared_bus_user.rate = c->parent->max_rate;
6289         c->state = OFF;
6290         c->set = true;
6291
6292         if ((c->u.shared_bus_user.mode == SHARED_CEILING) ||
6293             (c->u.shared_bus_user.mode == SHARED_CEILING_BUT_ISO)) {
6294                 c->state = ON;
6295                 c->refcnt++;
6296         }
6297
6298         if (c->parent->stats.histogram) {
6299                 int i = c->parent->stats.histogram->users_num++;
6300                 c->u.shared_bus_user.stats_idx = i;
6301                 if (i < STATS_USERS_LIST_SIZE)
6302                         c->parent->stats.histogram->users_list[i] = c;
6303         }
6304
6305         if (c->u.shared_bus_user.client_id) {
6306                 c->u.shared_bus_user.client =
6307                         tegra_get_clock_by_name(c->u.shared_bus_user.client_id);
6308                 if (!c->u.shared_bus_user.client) {
6309                         pr_err("%s: could not find clk %s\n", __func__,
6310                                c->u.shared_bus_user.client_id);
6311                         return;
6312                 }
6313                 c->u.shared_bus_user.client->flags |=
6314                         c->parent->flags & PERIPH_ON_CBUS;
6315                 c->flags |= c->parent->flags & PERIPH_ON_CBUS;
6316                 c->div = 1;
6317                 c->mul = 1;
6318         }
6319
6320         list_add_tail(&c->u.shared_bus_user.node,
6321                 &c->parent->shared_bus_list);
6322 }
6323
6324 static int tegra_clk_shared_bus_user_set_parent(struct clk *c, struct clk *p)
6325 {
6326         int ret;
6327         const struct clk_mux_sel *sel;
6328
6329         if (detach_shared_bus)
6330                 return 0;
6331
6332         if (c->parent == p)
6333                 return 0;
6334
6335         if (!(c->inputs && c->cross_clk_mutex && clk_cansleep(c)))
6336                 return -ENOSYS;
6337
6338         for (sel = c->inputs; sel->input != NULL; sel++) {
6339                 if (sel->input == p)
6340                         break;
6341         }
6342         if (!sel->input)
6343                 return -EINVAL;
6344
6345         if (c->refcnt)
6346                 tegra_clk_prepare_enable(p);
6347
6348         list_move_tail(&c->u.shared_bus_user.node, &p->shared_bus_list);
6349         ret = tegra_clk_shared_bus_update(p);
6350         if (ret) {
6351                 list_move_tail(&c->u.shared_bus_user.node,
6352                                &c->parent->shared_bus_list);
6353                 tegra_clk_shared_bus_update(c->parent);
6354                 tegra_clk_disable_unprepare(p);
6355                 return ret;
6356         }
6357
6358         tegra_clk_shared_bus_update(c->parent);
6359
6360         if (c->refcnt)
6361                 tegra_clk_disable_unprepare(c->parent);
6362
6363         clk_reparent(c, p);
6364
6365         return 0;
6366 }
6367
6368 static int tegra_clk_shared_bus_user_set_rate(struct clk *c, unsigned long rate)
6369 {
6370         int ret;
6371         unsigned long flags;
6372
6373         clk_lock_save(c->parent, &flags);
6374         c->u.shared_bus_user.rate = rate;
6375         ret = tegra_clk_shared_bus_update_locked(c->parent);
6376         clk_unlock_restore(c->parent, &flags);
6377
6378         if (!ret && c->cross_clk_mutex && clk_cansleep(c))
6379                 tegra_clk_shared_bus_migrate_users(c);
6380
6381         return ret;
6382 }
6383
6384 static long tegra_clk_shared_bus_user_round_rate(
6385         struct clk *c, unsigned long rate)
6386 {
6387         /*
6388          * Defer rounding requests until aggregated. BW users must not be
6389          * rounded at all, others just clipped to bus range (some clients
6390          * may use round api to find limits).
6391          */
6392
6393         if ((c->u.shared_bus_user.mode != SHARED_BW) &&
6394             (c->u.shared_bus_user.mode != SHARED_ISO_BW)) {
6395                 if (rate > c->parent->max_rate) {
6396                         rate = c->parent->max_rate;
6397                 } else {
6398                         /* Skippers allow to run below bus minimum */
6399                         bool use_skip = c->parent->shared_bus_flags &
6400                                 SHARED_BUS_USE_SKIPPERS;
6401                         struct clk *client = c->u.shared_bus_user.client;
6402                         int skip = (use_skip && client && client->skipper) ?
6403                                 SUPER_SKIPPER_TERM_SIZE : 0;
6404                         unsigned long min_rate = c->parent->min_rate >> skip;
6405
6406                         if (rate < min_rate)
6407                                 rate = min_rate;
6408                 }
6409         }
6410         return rate;
6411 }
6412
6413 static int tegra_clk_shared_bus_user_enable(struct clk *c)
6414 {
6415         int ret;
6416         unsigned long flags;
6417
6418         clk_lock_save(c->parent, &flags);
6419         c->u.shared_bus_user.enabled = true;
6420         ret = tegra_clk_shared_bus_update_locked(c->parent);
6421         if (!ret && c->u.shared_bus_user.client)
6422                 ret = tegra_clk_prepare_enable(c->u.shared_bus_user.client);
6423         clk_unlock_restore(c->parent, &flags);
6424
6425         if (!ret && c->cross_clk_mutex && clk_cansleep(c))
6426                 tegra_clk_shared_bus_migrate_users(c);
6427
6428         return ret;
6429 }
6430
6431 static void tegra_clk_shared_bus_user_disable(struct clk *c)
6432 {
6433         unsigned long flags;
6434
6435         clk_lock_save(c->parent, &flags);
6436         if (c->u.shared_bus_user.client)
6437                 tegra_clk_disable_unprepare(c->u.shared_bus_user.client);
6438         c->u.shared_bus_user.enabled = false;
6439         tegra_clk_shared_bus_update_locked(c->parent);
6440         clk_unlock_restore(c->parent, &flags);
6441
6442         if (c->cross_clk_mutex && clk_cansleep(c))
6443                 tegra_clk_shared_bus_migrate_users(c);
6444 }
6445
6446 static void tegra_clk_shared_bus_user_reset(struct clk *c, bool assert)
6447 {
6448         if (c->u.shared_bus_user.client) {
6449                 if (c->u.shared_bus_user.client->ops &&
6450                     c->u.shared_bus_user.client->ops->reset)
6451                         c->u.shared_bus_user.client->ops->reset(
6452                                 c->u.shared_bus_user.client, assert);
6453         }
6454 }
6455
6456 static struct clk_ops tegra_clk_shared_bus_user_ops = {
6457         .init = tegra_clk_shared_bus_user_init,
6458         .enable = tegra_clk_shared_bus_user_enable,
6459         .disable = tegra_clk_shared_bus_user_disable,
6460         .set_parent = tegra_clk_shared_bus_user_set_parent,
6461         .set_rate = tegra_clk_shared_bus_user_set_rate,
6462         .round_rate = tegra_clk_shared_bus_user_round_rate,
6463         .reset = tegra_clk_shared_bus_user_reset,
6464 };
6465
6466 /* shared bus connector ops (user/bus connector to cascade shared buses) */
6467 static int tegra21_clk_shared_connector_update(struct clk *bus)
6468 {
6469         unsigned long rate, old_rate;
6470
6471         if (detach_shared_bus)
6472                 return 0;
6473
6474         rate = tegra21_clk_shared_bus_update(bus, NULL, NULL, NULL);
6475
6476         old_rate = clk_get_rate_locked(bus);
6477         if (rate == old_rate)
6478                 return 0;
6479
6480         return clk_set_rate_locked(bus, rate);
6481 }
6482
6483 static struct clk_ops tegra_clk_shared_connector_ops = {
6484         .init = tegra_clk_shared_bus_user_init,
6485         .enable = tegra_clk_shared_bus_user_enable,
6486         .disable = tegra_clk_shared_bus_user_disable,
6487         .set_parent = tegra_clk_shared_bus_user_set_parent,
6488         .set_rate = tegra_clk_shared_bus_user_set_rate,
6489         .round_rate = tegra_clk_shared_bus_user_round_rate,
6490         .reset = tegra_clk_shared_bus_user_reset,
6491         .shared_bus_update = tegra21_clk_shared_connector_update,
6492 };
6493
6494 /* coupled gate ops */
6495 /*
6496  * Some clocks may have common enable/disable control, but run at different
6497  * rates, and have different dvfs tables. Coupled gate clock synchronize
6498  * enable/disable operations for such clocks.
6499  */
6500
6501 static int tegra21_clk_coupled_gate_enable(struct clk *c)
6502 {
6503         int ret;
6504         const struct clk_mux_sel *sel;
6505
6506         BUG_ON(!c->inputs);
6507         pr_debug("%s on clock %s\n", __func__, c->name);
6508
6509         for (sel = c->inputs; sel->input != NULL; sel++) {
6510                 if (sel->input == c->parent)
6511                         continue;
6512
6513                 ret = tegra_clk_prepare_enable(sel->input);
6514                 if (ret) {
6515                         while (sel != c->inputs) {
6516                                 sel--;
6517                                 if (sel->input == c->parent)
6518                                         continue;
6519                                 tegra_clk_disable_unprepare(sel->input);
6520                         }
6521                         return ret;
6522                 }
6523         }
6524
6525         return tegra21_periph_clk_enable(c);
6526 }
6527
6528 static void tegra21_clk_coupled_gate_disable(struct clk *c)
6529 {
6530         const struct clk_mux_sel *sel;
6531
6532         BUG_ON(!c->inputs);
6533         pr_debug("%s on clock %s\n", __func__, c->name);
6534
6535         tegra21_periph_clk_disable(c);
6536
6537         if (!c->refcnt) /* happens only on boot clean-up: don't propagate */
6538                 return;
6539
6540         for (sel = c->inputs; sel->input != NULL; sel++) {
6541                 if (sel->input == c->parent)
6542                         continue;
6543
6544                 if (sel->input->set)    /* enforce coupling after boot only */
6545                         tegra_clk_disable_unprepare(sel->input);
6546         }
6547 }
6548
6549 /*
6550  * AHB and APB shared bus operations
6551  * APB shared bus is a user of AHB shared bus
6552  * AHB shared bus is a user of SCLK complex shared bus
6553  * SCLK/AHB and AHB/APB dividers can be dynamically changed. When AHB and APB
6554  * users requests are propagated to SBUS target rate, current values of the
6555  * dividers are ignored, and flat maximum request is selected as SCLK bus final
6556  * target. Then the dividers will be re-evaluated, based on AHB and APB targets.
6557  * Both AHB and APB buses are always enabled.
6558  */
6559 static void tegra21_clk_ahb_apb_init(struct clk *c, struct clk *bus_clk)
6560 {
6561         tegra_clk_shared_bus_user_init(c);
6562         c->max_rate = bus_clk->max_rate;
6563         c->min_rate = bus_clk->min_rate;
6564         c->mul = bus_clk->mul;
6565         c->div = bus_clk->div;
6566
6567         c->u.shared_bus_user.rate = clk_get_rate(bus_clk);
6568         c->u.shared_bus_user.enabled = true;
6569         c->parent->child_bus = c;
6570 }
6571
6572 static void tegra21_clk_ahb_init(struct clk *c)
6573 {
6574         struct clk *bus_clk = c->parent->u.system.hclk;
6575         tegra21_clk_ahb_apb_init(c, bus_clk);
6576 }
6577
6578 static void tegra21_clk_apb_init(struct clk *c)
6579 {
6580         struct clk *bus_clk = c->parent->parent->u.system.pclk;
6581         tegra21_clk_ahb_apb_init(c, bus_clk);
6582 }
6583
6584 static int tegra21_clk_ahb_apb_update(struct clk *bus)
6585 {
6586         unsigned long rate;
6587
6588         if (detach_shared_bus)
6589                 return 0;
6590
6591         rate = tegra21_clk_shared_bus_update(bus, NULL, NULL, NULL);
6592         return clk_set_rate_locked(bus, rate);
6593 }
6594
6595 static struct clk_ops tegra_clk_ahb_ops = {
6596         .init = tegra21_clk_ahb_init,
6597         .set_rate = tegra_clk_shared_bus_user_set_rate,
6598         .round_rate = tegra_clk_shared_bus_user_round_rate,
6599         .shared_bus_update = tegra21_clk_ahb_apb_update,
6600 };
6601
6602 static struct clk_ops tegra_clk_apb_ops = {
6603         .init = tegra21_clk_apb_init,
6604         .set_rate = tegra_clk_shared_bus_user_set_rate,
6605         .round_rate = tegra_clk_shared_bus_user_round_rate,
6606         .shared_bus_update = tegra21_clk_ahb_apb_update,
6607 };
6608
6609 static struct clk_ops tegra_clk_coupled_gate_ops = {
6610         .init                   = tegra21_periph_clk_init,
6611         .enable                 = tegra21_clk_coupled_gate_enable,
6612         .disable                = tegra21_clk_coupled_gate_disable,
6613         .reset                  = &tegra21_periph_clk_reset,
6614 };
6615
6616
6617 /* Clock definitions */
6618 static struct clk tegra_clk_32k = {
6619         .name = "clk_32k",
6620         .clk_id = TEGRA210_CLK_ID_CLK_32K,
6621         .rate = 32768,
6622         .ops  = NULL,
6623         .max_rate = 32768,
6624 };
6625
6626 static struct clk tegra_clk_osc = {
6627         .name      = "osc",
6628         .clk_id    = TEGRA210_CLK_ID_CLK_OSC,
6629         .flags     = ENABLE_ON_INIT,
6630         .ops       = &tegra_osc_ops,
6631         .max_rate  = 48000000,
6632 };
6633
6634 static struct clk tegra_clk_m = {
6635         .name      = "clk_m",
6636         .clk_id    = TEGRA210_CLK_ID_CLK_M,
6637         .flags     = ENABLE_ON_INIT,
6638         .parent    = &tegra_clk_osc,
6639         .ops       = &tegra_clk_m_ops,
6640         .max_rate  = 48000000,
6641 };
6642
6643 static struct clk tegra_clk_m_div2 = {
6644         .name      = "clk_m_div2",
6645         .clk_id    = TEGRA210_CLK_ID_CLK_M_DIV2,
6646         .ops       = &tegra_clk_m_div_ops,
6647         .parent    = &tegra_clk_m,
6648         .mul       = 1,
6649         .div       = 2,
6650         .state     = ON,
6651         .max_rate  = 24000000,
6652 };
6653
6654 static struct clk tegra_clk_m_div4 = {
6655         .name      = "clk_m_div4",
6656         .clk_id    = TEGRA210_CLK_ID_CLK_M_DIV4,
6657         .ops       = &tegra_clk_m_div_ops,
6658         .parent    = &tegra_clk_m,
6659         .mul       = 1,
6660         .div       = 4,
6661         .state     = ON,
6662         .max_rate  = 12000000,
6663 };
6664
6665 static struct clk tegra_pll_ref = {
6666         .name      = "pll_ref",
6667         .clk_id    = TEGRA210_CLK_ID_PLL_REF,
6668         .flags     = ENABLE_ON_INIT,
6669         .ops       = &tegra_pll_ref_ops,
6670         .parent    = &tegra_clk_osc,
6671         .max_rate  = 38400000,
6672 };
6673
6674 static struct clk_pll_freq_table tegra_pll_cx_freq_table[] = {
6675         { 12000000, 510000000,  85, 1, 2},
6676         { 13000000, 510000000,  78, 1, 2},      /* actual: 507.0 MHz */
6677         { 38400000, 510000000,  79, 3, 2},      /* actual: 505.6 MHz */
6678         { 0, 0, 0, 0, 0, 0 },
6679 };
6680
6681 static struct clk_pll_controls pllcx_controls = {
6682         .enable_mask = PLL_BASE_ENABLE,
6683         .bypass_mask = PLL_BASE_BYPASS,
6684         .reset_mask = PLLCX_MISC0_RESET,
6685         .reset_reg_idx = PLL_MISC0_IDX,
6686         .iddq_mask = PLLCX_MISC1_IDDQ,
6687         .iddq_reg_idx = PLL_MISC1_IDX,
6688         .lock_mask = PLLCX_BASE_LOCK,
6689         .lock_reg_idx = PLL_BASE_IDX,
6690 };
6691
6692 static struct clk_pll_div_layout pllcx_div_layout = {
6693         .mdiv_shift = 0,
6694         .mdiv_mask = 0xff,
6695         .ndiv_shift = 10,
6696         .ndiv_mask = 0xff << 10,
6697         .pdiv_shift = 20,
6698         .pdiv_mask = 0x1f << 20,
6699         .pdiv_to_p = pll_qlin_pdiv_to_p,
6700         .pdiv_max = PLL_QLIN_PDIV_MAX,
6701 };
6702
6703 static struct clk tegra_pll_c = {
6704         .name      = "pll_c",
6705         .clk_id    = TEGRA210_CLK_ID_PLL_C,
6706         .ops       = &tegra_pllcx_ops,
6707         .reg       = 0x80,
6708         .parent    = &tegra_pll_ref,
6709         .max_rate  = 1200000000,
6710         .u.pll = {
6711                 .input_min = 12000000,
6712                 .input_max = 700000000,
6713                 .cf_min    = 12000000,
6714                 .cf_max    = 50000000,
6715                 .vco_min   = 600000000,
6716                 .vco_max   = 1200000000,
6717                 .freq_table = tegra_pll_cx_freq_table,
6718                 .lock_delay = 300,
6719                 .misc0 = 0x88 - 0x80,
6720                 .misc1 = 0x8c - 0x80,
6721                 .misc2 = 0x5d0 - 0x80,
6722                 .misc3 = 0x5d4 - 0x80,
6723                 .controls = &pllcx_controls,
6724                 .div_layout = &pllcx_div_layout,
6725                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
6726                 .dyn_ramp = pllcx_dyn_ramp,
6727                 .set_defaults = pllcx_set_defaults,
6728         },
6729 };
6730
6731 static struct clk tegra_pll_c_out1 = {
6732         .name      = "pll_c_out1",
6733         .clk_id    = TEGRA210_CLK_ID_PLL_C_OUT1,
6734         .ops       = &tegra_pll_div_ops,
6735         .flags     = DIV_U71 | PERIPH_ON_CBUS,
6736         .parent    = &tegra_pll_c,
6737         .reg       = 0x84,
6738         .reg_shift = 0,
6739         .max_rate  = 700000000,
6740 };
6741
6742 static struct clk tegra_pll_c2 = {
6743         .name      = "pll_c2",
6744         .clk_id    = TEGRA210_CLK_ID_PLL_C2,
6745         .ops       = &tegra_pllcx_ops,
6746         .reg       = 0x4e8,
6747         .parent    = &tegra_pll_ref,
6748         .max_rate  = 1200000000,
6749         .u.pll = {
6750                 .input_min = 12000000,
6751                 .input_max = 700000000,
6752                 .cf_min    = 12000000,
6753                 .cf_max    = 50000000,
6754                 .vco_min   = 600000000,
6755                 .vco_max   = 1200000000,
6756                 .freq_table = tegra_pll_cx_freq_table,
6757                 .lock_delay = 300,
6758                 .misc0 = 0x4ec - 0x4e8,
6759                 .misc1 = 0x4f0 - 0x4e8,
6760                 .misc2 = 0x4f4 - 0x4e8,
6761                 .misc3 = 0x4f8 - 0x4e8,
6762                 .controls = &pllcx_controls,
6763                 .div_layout = &pllcx_div_layout,
6764                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
6765                 .dyn_ramp = pllcx_dyn_ramp,
6766                 .set_defaults = pllcx_set_defaults,
6767         },
6768 };
6769
6770 static struct clk tegra_pll_c3 = {
6771         .name      = "pll_c3",
6772         .clk_id    = TEGRA210_CLK_ID_PLL_C3,
6773         .ops       = &tegra_pllcx_ops,
6774         .reg       = 0x4fc,
6775         .parent    = &tegra_pll_ref,
6776         .max_rate  = 1200000000,
6777         .u.pll = {
6778                 .input_min = 12000000,
6779                 .input_max = 700000000,
6780                 .cf_min    = 12000000,
6781                 .cf_max    = 50000000,
6782                 .vco_min   = 600000000,
6783                 .vco_max   = 1200000000,
6784                 .freq_table = tegra_pll_cx_freq_table,
6785                 .lock_delay = 300,
6786                 .misc0 = 0x500 - 0x4fc,
6787                 .misc1 = 0x504 - 0x4fc,
6788                 .misc2 = 0x508 - 0x4fc,
6789                 .misc3 = 0x50c - 0x4fc,
6790                 .controls = &pllcx_controls,
6791                 .div_layout = &pllcx_div_layout,
6792                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
6793                 .dyn_ramp = pllcx_dyn_ramp,
6794                 .set_defaults = pllcx_set_defaults,
6795         },
6796 };
6797
6798 static struct clk tegra_pll_a1 = {
6799         .name      = "pll_a1",
6800         .clk_id    = TEGRA210_CLK_ID_PLL_A1,
6801         .ops       = &tegra_pllcx_ops,
6802         .reg       = 0x6a4,
6803         .parent    = &tegra_pll_ref,
6804         .max_rate  = 1200000000,
6805         .u.pll = {
6806                 .input_min = 12000000,
6807                 .input_max = 700000000,
6808                 .cf_min    = 12000000,
6809                 .cf_max    = 50000000,
6810                 .vco_min   = 600000000,
6811                 .vco_max   = 1200000000,
6812                 .freq_table = tegra_pll_cx_freq_table,
6813                 .lock_delay = 300,
6814                 .misc0 = 0x6a8 - 0x6a4,
6815                 .misc1 = 0x6ac - 0x6a4,
6816                 .misc2 = 0x6b0 - 0x6a4,
6817                 .misc3 = 0x6b4 - 0x6a4,
6818                 .controls = &pllcx_controls,
6819                 .div_layout = &pllcx_div_layout,
6820                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
6821                 .dyn_ramp = pllcx_dyn_ramp,
6822                 .set_defaults = pllcx_set_defaults,
6823         },
6824 };
6825
6826 static struct clk_pll_freq_table tegra_pll_a_freq_table[] = {
6827         { 12000000, 282240000, 47, 1, 2, 1, 0xf148},    /* actual: 282240234 */
6828         { 12000000, 368640000, 61, 1, 2, 1, 0xfe15},    /* actual: 368640381 */
6829         { 12000000, 240000000, 60, 1, 3, 1, },
6830
6831         { 13000000, 282240000, 43, 1, 2, 1, 0xfd7d},    /* actual: 282239807 */
6832         { 13000000, 368640000, 56, 1, 2, 1, 0x06d8},    /* actual: 368640137 */
6833         { 13000000, 240000000, 55, 1, 3, 1, },          /* actual: 238.3 MHz */
6834
6835         { 38400000, 282240000, 44, 3, 2, 1, 0xf333},    /* actual: 282239844 */
6836         { 38400000, 368640000, 57, 3, 2, 1, 0x0333},    /* actual: 368639844 */
6837         { 38400000, 240000000, 75, 3, 4, 1,},
6838         { 0, 0, 0, 0, 0, 0 },
6839 };
6840
6841 static struct clk_pll_controls plla_controls = {
6842         .enable_mask = PLL_BASE_ENABLE,
6843         .bypass_mask = PLL_BASE_BYPASS,
6844         .iddq_mask = PLLA_BASE_IDDQ,
6845         .iddq_reg_idx = PLL_BASE_IDX,
6846         .lock_mask = PLLA_BASE_LOCK,
6847         .lock_reg_idx = PLL_BASE_IDX,
6848
6849         .sdm_en_mask = PLLA_MISC2_EN_SDM,
6850         .sdm_ctrl_reg_idx = PLL_MISC2_IDX,
6851 };
6852
6853 static struct clk_pll_div_layout plla_div_layout = {
6854         .mdiv_shift = 0,
6855         .mdiv_mask = 0xff,
6856         .ndiv_shift = 8,
6857         .ndiv_mask = 0xff << 8,
6858         .pdiv_shift = 20,
6859         .pdiv_mask = 0x1f << 20,
6860         .pdiv_to_p = pll_qlin_pdiv_to_p,
6861         .pdiv_max = PLL_QLIN_PDIV_MAX,
6862
6863         .sdm_din_shift = 0,
6864         .sdm_din_mask = 0xffff,
6865         .sdm_din_reg_idx = PLL_MISC1_IDX,
6866 };
6867
6868 static struct clk tegra_pll_a = {
6869         .name      = "pll_a",
6870         .clk_id    = TEGRA210_CLK_ID_PLL_A,
6871         .ops       = &tegra_plla_ops,
6872         .reg       = 0xb0,
6873         .parent    = &tegra_pll_ref,
6874         .max_rate  = 1000000000,
6875         .u.pll = {
6876                 .input_min = 12000000,
6877                 .input_max = 800000000,
6878                 .cf_min    = 12000000,
6879                 .cf_max    = 19200000,
6880                 .vco_min   = 500000000,
6881                 .vco_max   = 1000000000,
6882                 .freq_table = tegra_pll_a_freq_table,
6883                 .lock_delay = 300,
6884                 .misc0 = 0xbc - 0xb0,
6885                 .misc1 = 0xb8 - 0xb0,
6886                 .misc2 = 0x5d8 - 0xb0,
6887                 .controls = &plla_controls,
6888                 .div_layout = &plla_div_layout,
6889                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
6890                 .set_defaults = plla_set_defaults,
6891         },
6892 };
6893
6894 static struct clk tegra_pll_a_out0 = {
6895         .name      = "pll_a_out0",
6896         .clk_id    = TEGRA210_CLK_ID_PLL_A_OUT0,
6897         .ops       = &tegra_pll_div_ops,
6898         .flags     = DIV_U71,
6899         .parent    = &tegra_pll_a,
6900         .reg       = 0xb4,
6901         .reg_shift = 0,
6902         .max_rate  = 600000000,
6903 };
6904
6905 static struct clk_mux_sel mux_plla[] = {
6906         { .input = &tegra_pll_a, .value = 0},
6907         { 0, 0},
6908 };
6909
6910 static struct clk tegra_pll_a_out_adsp = {
6911         .name       = "pll_a_out_adsp",
6912         .clk_id     = TEGRA210_CLK_ID_PLL_A_OUT_ADSP,
6913         .ops        = &tegra_periph_clk_ops,
6914         .inputs     = mux_plla,
6915         .flags      = PERIPH_NO_RESET,
6916         .max_rate   = 1000000000,
6917         .u.periph = {
6918                 .clk_num = 188,
6919         },
6920 };
6921
6922 static struct clk_mux_sel mux_plla_out0[] = {
6923         { .input = &tegra_pll_a_out0, .value = 0},
6924         { 0, 0},
6925 };
6926
6927 static struct clk tegra_pll_a_out0_out_adsp = {
6928         .name       = "pll_a_out0_out_adsp",
6929         .clk_id     = TEGRA210_CLK_ID_PLL_A_OUT0_OUT_ADSP,
6930         .ops        = &tegra_periph_clk_ops,
6931         .inputs     = mux_plla_out0,
6932         .flags      = PERIPH_NO_RESET,
6933         .max_rate   = 600000000,
6934         .u.periph = {
6935                 .clk_num = 188,
6936         },
6937 };
6938
6939 static struct clk_pll_freq_table tegra_pll_d_freq_table[] = {
6940         { 12000000, 594000000,  99, 1, 2},
6941         { 13000000, 594000000,  91, 1, 2, 0, 0xfc4f},   /* actual: 594000183 */
6942         { 38400000, 594000000,  30, 1, 2, 0, 0x0e00},
6943         { 0, 0, 0, 0, 0, 0 },
6944 };
6945
6946 static struct clk_pll_controls plld_controls = {
6947         .enable_mask = PLL_BASE_ENABLE,
6948         .bypass_mask = PLL_BASE_BYPASS,
6949         .iddq_mask = PLLD_MISC0_IDDQ,
6950         .iddq_reg_idx = PLL_MISC0_IDX,
6951         .lock_mask = PLLD_BASE_LOCK,
6952         .lock_reg_idx = PLL_BASE_IDX,
6953
6954         .sdm_en_mask = PLLD_MISC0_EN_SDM,
6955         .sdm_ctrl_reg_idx = PLL_MISC0_IDX,
6956 };
6957
6958 static struct clk_pll_div_layout plld_div_layout = {
6959         .mdiv_shift = 0,
6960         .mdiv_mask = 0xff,
6961         .ndiv_shift = 11,
6962         .ndiv_mask = 0xff << 11,
6963         .pdiv_shift = 20,
6964         .pdiv_mask = 0x7 << 20,
6965         .pdiv_to_p = pll_expo_pdiv_to_p,
6966         .pdiv_max = PLL_EXPO_PDIV_MAX,
6967
6968         .sdm_din_shift = 0,
6969         .sdm_din_mask = 0xffff,
6970         .sdm_din_reg_idx = PLL_MISC0_IDX,
6971 };
6972
6973 static struct clk tegra_pll_d = {
6974         .name      = "pll_d",
6975         .clk_id    = TEGRA210_CLK_ID_PLL_D,
6976         .flags     = PLLD,
6977         .ops       = &tegra_plld_ops,
6978         .reg       = 0xd0,
6979         .parent    = &tegra_pll_ref,
6980         .max_rate  = 1500000000,
6981         .u.pll = {
6982                 .input_min = 12000000,
6983                 .input_max = 800000000,
6984                 .cf_min    = 12000000,
6985                 .cf_max    = 38400000,
6986                 .vco_min   = 750000000,
6987                 .vco_max   = 1500000000,
6988                 .mdiv_default = 1,
6989                 .freq_table = tegra_pll_d_freq_table,
6990                 .lock_delay = 300,
6991                 .misc0 = 0xdc - 0xd0,
6992                 .misc1 = 0xd8 - 0xd0,
6993                 .controls = &plld_controls,
6994                 .div_layout = &plld_div_layout,
6995                 .round_p_to_pdiv = pll_expo_p_to_pdiv,
6996                 .set_defaults = plld_set_defaults,
6997         },
6998 };
6999
7000 static struct clk tegra_pll_d_out0 = {
7001         .name      = "pll_d_out0",
7002         .clk_id    = TEGRA210_CLK_ID_PLL_D_OUT0,
7003         .ops       = &tegra_pll_div_ops,
7004         .flags     = DIV_2 | PLLD,
7005         .parent    = &tegra_pll_d,
7006         .max_rate  = 750000000,
7007 };
7008
7009 static struct clk_pll_freq_table tegra_pll_d2_freq_table[] = {
7010         { 12000000, 594000000,  99, 1, 2, 0, 0xf000},
7011         { 13000000, 594000000,  91, 1, 2, 0, 0xfc4f},   /* actual: 594000183 */
7012         { 38400000, 594000000,  30, 1, 2, 0, 0x0e00},
7013         { 0, 0, 0, 0, 0, 0 },
7014 };
7015
7016 static struct clk_pll_controls plld2_controls = {
7017         .enable_mask = PLL_BASE_ENABLE,
7018         .bypass_mask = PLL_BASE_BYPASS,
7019         .iddq_mask = PLLDSS_BASE_IDDQ,
7020         .iddq_reg_idx = PLL_BASE_IDX,
7021         .lock_mask = PLLDSS_BASE_LOCK,
7022         .lock_reg_idx = PLL_BASE_IDX,
7023
7024         .sdm_en_mask = PLLDSS_MISC1_CFG_EN_SDM,
7025         .ssc_en_mask = PLLDSS_MISC1_CFG_EN_SSC,
7026         .sdm_ctrl_reg_idx = PLL_MISC1_IDX,
7027 };
7028
7029 static struct clk_pll_div_layout plldss_div_layout = {
7030         .mdiv_shift = 0,
7031         .mdiv_mask = 0xff,
7032         .ndiv_shift = 8,
7033         .ndiv_mask = 0xff << 8,
7034         .pdiv_shift = 19,
7035         .pdiv_mask = 0x1f << 19,
7036         .pdiv_to_p = pll_qlin_pdiv_to_p,
7037         .pdiv_max = PLL_QLIN_PDIV_MAX,
7038
7039         .sdm_din_shift = 0,
7040         .sdm_din_mask = 0xffff,
7041         .sdm_din_reg_idx = PLL_MISC3_IDX,
7042 };
7043
7044 static struct clk tegra_pll_d2 = {
7045         .name      = "pll_d2",
7046         .clk_id    = TEGRA210_CLK_ID_PLL_D2,
7047         .ops       = &tegra_plld2_ops,
7048         .reg       = 0x4b8,
7049         .parent    = &tegra_pll_ref,    /* s/w policy, always tegra_pll_ref */
7050         .max_rate  = 1500000000,
7051         .u.pll = {
7052                 .input_min = 12000000,
7053                 .input_max = 800000000,
7054                 .cf_min    = 12000000,
7055                 .cf_max    = 38400000,
7056                 .vco_min   = 750000000,
7057                 .vco_max   = 1500000000,
7058                 .mdiv_default = 1,
7059                 .freq_table = tegra_pll_d2_freq_table,
7060                 .lock_delay = 300,
7061                 .misc0 = 0x4bc - 0x4b8,
7062                 .misc1 = 0x570 - 0x4b8,
7063                 .misc2 = 0x574 - 0x4b8,
7064                 .misc3 = 0x578 - 0x4b8,
7065                 .controls = &plld2_controls,
7066                 .div_layout = &plldss_div_layout,
7067                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7068                 .set_defaults = plld2_set_defaults,
7069         },
7070 };
7071
7072 static struct clk_pll_freq_table tegra_pll_dp_freq_table[] = {
7073         { 12000000, 270000000,  90, 1, 4, 0, 0xf000},
7074         { 13000000, 270000000,  83, 1, 4, 0, 0xf000},   /* actual: 269.8 MHz */
7075         { 38400000, 270000000,  28, 1, 4, 0, 0xf400},
7076         { 0, 0, 0, 0, 0, 0 },
7077 };
7078
7079 static struct clk_pll_controls plldp_controls = {
7080         .enable_mask = PLL_BASE_ENABLE,
7081         .bypass_mask = PLL_BASE_BYPASS,
7082         .iddq_mask = PLLDSS_BASE_IDDQ,
7083         .iddq_reg_idx = PLL_BASE_IDX,
7084         .lock_mask = PLLDSS_BASE_LOCK,
7085         .lock_reg_idx = PLL_BASE_IDX,
7086
7087         .sdm_en_mask = PLLDSS_MISC1_CFG_EN_SDM,
7088         .ssc_en_mask = PLLDSS_MISC1_CFG_EN_SSC,
7089         .sdm_ctrl_reg_idx = PLL_MISC1_IDX,
7090 };
7091
7092 static struct clk tegra_pll_dp = {
7093         .name      = "pll_dp",
7094         .clk_id    = TEGRA210_CLK_ID_PLL_DP,
7095         .flags     = PLL_FIXED,
7096         .ops       = &tegra_plldp_ops,
7097         .reg       = 0x590,
7098         .parent    = &tegra_pll_ref,    /* s/w policy, always tegra_pll_ref */
7099         .max_rate  = 1500000000,
7100         .u.pll = {
7101                 .input_min = 12000000,
7102                 .input_max = 800000000,
7103                 .cf_min    = 12000000,
7104                 .cf_max    = 38400000,
7105                 .vco_min   = 750000000,
7106                 .vco_max   = 1500000000,
7107                 .mdiv_default = 1,
7108                 .freq_table = tegra_pll_dp_freq_table,
7109                 .lock_delay = 300,
7110                 .misc0 = 0x594 - 0x590,
7111                 .misc1 = 0x598 - 0x590,
7112                 .misc2 = 0x59c - 0x590,
7113                 .misc3 = 0x5a0 - 0x590,
7114                 .controls = &plldp_controls,
7115                 .div_layout = &plldss_div_layout,
7116                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7117                 .set_defaults = plldp_set_defaults,
7118                 .fixed_rate = 270000000,
7119         },
7120 };
7121
7122 static struct clk_pll_freq_table tegra_pllc4_vco_freq_table[] = {
7123         { 12000000, 600000000,  50, 1, 1},
7124         { 13000000, 600000000,  46, 1, 1},      /* actual: 598.0 MHz */
7125         { 38400000, 600000000,  62, 4, 1},      /* actual: 595.2 MHz */
7126         { 0, 0, 0, 0, 0, 0 },
7127 };
7128
7129 static struct clk_pll_controls pllc4_controls = {
7130         .enable_mask = PLL_BASE_ENABLE,
7131         .bypass_mask = PLL_BASE_BYPASS,
7132         .iddq_mask = PLLDSS_BASE_IDDQ,
7133         .iddq_reg_idx = PLL_BASE_IDX,
7134         .lock_mask = PLLDSS_BASE_LOCK,
7135         .lock_reg_idx = PLL_BASE_IDX,
7136 };
7137
7138 static struct clk_pll_div_layout pllc4_div_layout = {
7139         .mdiv_shift = 0,
7140         .mdiv_mask = 0xff,
7141         .ndiv_shift = 8,
7142         .ndiv_mask = 0xff << 8,
7143         .pdiv_shift = 19,
7144         .pdiv_mask = 0x1f << 19,
7145         .pdiv_to_p = pll_qlin_pdiv_to_p,
7146         .pdiv_max = PLL_QLIN_PDIV_MAX,
7147 };
7148
7149 static struct clk tegra_pll_c4_vco = {
7150         .name      = "pll_c4",
7151         .clk_id    = TEGRA210_CLK_ID_PLL_C4,
7152         .ops       = &tegra_pllc4_vco_ops,
7153         .reg       = 0x5a4,
7154         .parent    = &tegra_pll_ref,    /* s/w policy, always tegra_pll_ref */
7155         .max_rate  = 1080000000,
7156         .u.pll = {
7157                 .input_min = 9600000,
7158                 .input_max = 800000000,
7159                 .cf_min    = 9600000,
7160                 .cf_max    = 19200000,
7161                 .vco_min   = 500000000,
7162                 .vco_max   = 1080000000,
7163                 .freq_table = tegra_pllc4_vco_freq_table,
7164                 .lock_delay = 300,
7165                 .misc0 = 0x5a8 - 0x5a4,
7166                 .controls = &pllc4_controls,
7167                 .div_layout = &pllc4_div_layout,
7168                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7169                 .set_defaults = pllc4_set_defaults,
7170                 .vco_out = true,
7171         },
7172 };
7173
7174 static struct clk tegra_pll_c4_out0 = {
7175         .name      = "pll_c4_out0",
7176         .clk_id    = TEGRA210_CLK_ID_PLL_C4_OUT0,
7177         .ops       = &tegra_pll_out_ops,
7178         .parent    = &tegra_pll_c4_vco,
7179 };
7180
7181 static struct clk tegra_pll_c4_out1 = {
7182         .name      = "pll_c4_out1",
7183         .clk_id    = TEGRA210_CLK_ID_PLL_C4_OUT1,
7184         .ops       = &tegra_pll_out_fixed_ops,
7185         .parent    = &tegra_pll_c4_vco,
7186         .mul       = 1,
7187         .div       = 3,
7188 };
7189
7190 static struct clk tegra_pll_c4_out2 = {
7191         .name      = "pll_c4_out2",
7192         .clk_id    = TEGRA210_CLK_ID_PLL_C4_OUT2,
7193         .ops       = &tegra_pll_out_fixed_ops,
7194         .parent    = &tegra_pll_c4_vco,
7195         .mul       = 1,
7196         .div       = 5,
7197 };
7198
7199 static struct clk tegra_pll_c4_out3 = {
7200         .name      = "pll_c4_out3",
7201         .clk_id    = TEGRA210_CLK_ID_PLL_C4_OUT3,
7202         .ops       = &tegra_pll_div_ops,
7203         .flags     = DIV_U71,
7204         .parent    = &tegra_pll_c4_out0,
7205         .reg       = 0x5e4,
7206         .reg_shift = 0,
7207         .max_rate  = 1080000000,
7208 };
7209
7210 static struct clk_pll_freq_table tegra_pllre_vco_freq_table[] = {
7211         { 12000000, 672000000,  56, 1, 1},
7212         { 13000000, 672000000,  51, 1, 1},      /* actual: 663.0 MHz */
7213         { 38400000, 672000000,  70, 4, 1},
7214         { 0, 0, 0, 0, 0, 0 },
7215 };
7216
7217 static struct clk_pll_controls pllre_controls = {
7218         .enable_mask = PLL_BASE_ENABLE,
7219         .bypass_mask = PLL_BASE_BYPASS,
7220         .iddq_mask = PLLRE_MISC0_IDDQ,
7221         .iddq_reg_idx = PLL_MISC0_IDX,
7222         .lock_mask = PLLRE_MISC0_LOCK,
7223         .lock_reg_idx = PLL_MISC0_IDX,
7224 };
7225
7226 static struct clk_pll_div_layout pllre_div_layout = {
7227         .mdiv_shift = 0,
7228         .mdiv_mask = 0xff,
7229         .ndiv_shift = 8,
7230         .ndiv_mask = 0xff << 8,
7231         .pdiv_shift = 16,
7232         .pdiv_mask = 0x1f << 16,
7233         .pdiv_to_p = pll_qlin_pdiv_to_p,
7234         .pdiv_max = PLL_QLIN_PDIV_MAX,
7235 };
7236
7237 static struct clk tegra_pll_re_vco = {
7238         .name      = "pll_re_vco",
7239         .clk_id    = TEGRA210_CLK_ID_PLL_RE,
7240         .ops       = &tegra_pllre_vco_ops,
7241         .reg       = 0x4c4,
7242         .parent    = &tegra_pll_ref,
7243         .max_rate  = 700000000,
7244         .u.pll = {
7245                 .input_min = 9600000,
7246                 .input_max = 800000000,
7247                 .cf_min    = 9600000,
7248                 .cf_max    = 19200000,
7249                 .vco_min   = 350000000,
7250                 .vco_max   = 700000000,
7251                 .freq_table = tegra_pllre_vco_freq_table,
7252                 .lock_delay = 300,
7253                 .misc0 = 0x4c8 - 0x4c4,
7254                 .controls = &pllre_controls,
7255                 .div_layout = &pllre_div_layout,
7256                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7257                 .set_defaults = pllre_set_defaults,
7258                 .vco_out = true,
7259         },
7260 };
7261
7262 static struct clk tegra_pll_re_out = {
7263         .name      = "pll_re_out",
7264         .clk_id    = TEGRA210_CLK_ID_PLL_RE_OUT,
7265         .ops       = &tegra_pll_out_ops,
7266         .parent    = &tegra_pll_re_vco,
7267 };
7268
7269 static struct clk tegra_pll_re_out1 = {
7270         .name      = "pll_re_out1",
7271         .clk_id    = TEGRA210_CLK_ID_PLL_RE_OUT1,
7272         .ops       = &tegra_pll_div_ops,
7273         .flags     = DIV_U71,
7274         .parent    = &tegra_pll_re_vco,
7275         .reg       = 0x4cc,
7276         .reg_shift = 0,
7277         .max_rate  = 700000000,
7278 };
7279
7280 static struct clk_pll_freq_table tegra_pll_x_freq_table[] = {
7281         /* 1 GHz */
7282         { 12000000, 1000000000, 166, 1, 2},     /* actual: 996.0 MHz */
7283         { 13000000, 1000000000, 153, 1, 2},     /* actual: 994.0 MHz */
7284         { 38400000, 1000000000, 156, 3, 2},     /* actual: 998.4 MHz */
7285         { 0, 0, 0, 0, 0, 0 },
7286 };
7287
7288 static struct clk_pll_controls pllx_controls = {
7289         .enable_mask = PLL_BASE_ENABLE,
7290         .bypass_mask = PLL_BASE_BYPASS,
7291         .iddq_mask = PLLX_MISC3_IDDQ,
7292         .iddq_reg_idx = PLL_MISC3_IDX,
7293         .lock_mask = PLLX_BASE_LOCK,
7294         .lock_reg_idx = PLL_BASE_IDX,
7295
7296         .dramp_en_mask = PLLX_MISC2_EN_DYNRAMP,
7297         .dramp_done_mask = PLLX_MISC2_DYNRAMP_DONE,
7298         .dramp_ctrl_reg_idx = PLL_MISC2_IDX,
7299 };
7300
7301 static struct clk_pll_div_layout pllx_div_layout = {
7302         .mdiv_shift = 0,
7303         .mdiv_mask = 0xff,
7304         .ndiv_shift = 8,
7305         .ndiv_mask = 0xff << 8,
7306         .pdiv_shift = 20,
7307         .pdiv_mask = 0x1f << 20,
7308         .pdiv_to_p = pll_qlin_pdiv_to_p,
7309         .pdiv_max = PLL_QLIN_PDIV_MAX,
7310
7311         .ndiv_new_shift = PLLX_MISC2_NDIV_NEW_SHIFT,
7312         .ndiv_new_reg_idx = PLL_MISC2_IDX,
7313 };
7314
7315 static struct clk tegra_pll_x = {
7316         .name      = "pll_x",
7317         .clk_id    = TEGRA210_CLK_ID_PLL_X,
7318         .flags     = PLLX,
7319         .ops       = &tegra_pllx_ops,
7320         .reg       = 0xe0,
7321         .parent    = &tegra_pll_ref,
7322         .max_rate  = 3000000000UL,
7323         .u.pll = {
7324                 .input_min = 12000000,
7325                 .input_max = 800000000,
7326                 .cf_min    = 12000000,
7327                 .cf_max    = 38400000,
7328                 .vco_min   = 1350000000,
7329                 .vco_max   = 3000000000UL,
7330                 .mdiv_default = 2,
7331                 .freq_table = tegra_pll_x_freq_table,
7332                 .lock_delay = 300,
7333                 .misc0 = 0xe4 - 0xe0,
7334                 .misc1 = 0x510 - 0xe0,
7335                 .misc2 = 0x514 - 0xe0,
7336                 .misc3 = 0x518 - 0xe0,
7337                 .misc4 = 0x5f0 - 0xe0,
7338                 .misc5 = 0x5f4 - 0xe0,
7339                 .controls = &pllx_controls,
7340                 .div_layout = &pllx_div_layout,
7341                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7342 #if PLLX_USE_DYN_RAMP
7343                 .dyn_ramp = pllx_dyn_ramp,
7344 #endif
7345                 .set_defaults = pllx_set_defaults,
7346         },
7347 };
7348
7349 static struct clk_pll_freq_table tegra_pll_m_freq_table[] = {
7350         { 12000000,  800000000,  66, 1, 1},     /* actual: 792.0 MHz */
7351         { 13000000,  800000000,  61, 1, 1},     /* actual: 793.0 MHz */
7352         { 38400000,  297600000,  93, 4, 3},
7353         { 38400000,  400000000, 125, 4, 3},
7354         { 38400000,  532800000, 111, 4, 2},
7355         { 38400000,  665600000, 104, 3, 2},
7356         { 38400000,  800000000, 125, 3, 2},
7357         { 38400000,  931200000,  97, 4, 1},
7358         { 38400000, 1065600000, 111, 4, 1},
7359         { 38400000, 1200000000, 125, 4, 1},
7360         { 38400000, 1331200000, 104, 3, 1},
7361         { 38400000, 1459200000,  76, 2, 1},
7362         { 38400000, 1600000000, 125, 3, 1},
7363         { 0, 0, 0, 0, 0, 0 },
7364 };
7365
7366 static struct clk_pll_controls pllm_controls = {
7367         .enable_mask = PLL_BASE_ENABLE,
7368         .iddq_mask = PLLM_MISC0_IDDQ,
7369         .iddq_reg_idx = PLL_MISC0_IDX,
7370         .lock_mask = PLLM_BASE_LOCK,
7371         .lock_reg_idx = PLL_BASE_IDX,
7372 };
7373
7374 static struct clk_pll_div_layout pllm_div_layout = {
7375         .mdiv_shift = 0,
7376         .mdiv_mask = 0xff,
7377         .ndiv_shift = 8,
7378         .ndiv_mask = 0xff << 8,
7379         .pdiv_shift = 20,
7380         .pdiv_mask = 0x1f << 20,
7381         .pdiv_to_p = pll_qlin_pdiv_to_p,
7382         .pdiv_max = PLL_QLIN_PDIV_MAX,
7383 };
7384
7385 static struct clk tegra_pll_m = {
7386         .name      = "pll_m",
7387         .clk_id    = TEGRA210_CLK_ID_PLL_M,
7388         .flags     = PLLM,
7389         .ops       = &tegra_pllm_ops,
7390         .reg       = 0x90,
7391         .parent    = &tegra_clk_osc,
7392         .max_rate  = 1866000000,
7393         .u.pll = {
7394                 .input_min = 9600000,
7395                 .input_max = 500000000,
7396                 .cf_min    = 9600000,
7397                 .cf_max    = 19200000,
7398                 .vco_min   = 800000000,
7399                 .vco_max   = 1866000000,
7400                 .freq_table = tegra_pll_m_freq_table,
7401                 .lock_delay = 300,
7402                 .misc0 = 0x9c - 0x90,
7403                 .misc1 = 0x98 - 0x90,
7404                 .controls = &pllm_controls,
7405                 .div_layout = &pllm_div_layout,
7406                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7407         },
7408 };
7409
7410 static struct clk_pll_controls pllmb_controls = {
7411         .enable_mask = PLL_BASE_ENABLE,
7412         .iddq_mask = PLLMB_MISC0_IDDQ,
7413         .iddq_reg_idx = PLL_MISC0_IDX,
7414         .lock_mask = PLLMB_BASE_LOCK,
7415         .lock_reg_idx = PLL_BASE_IDX,
7416 };
7417
7418 static struct clk tegra_pll_mb = {
7419         .name      = "pll_mb",
7420         .clk_id    = TEGRA210_CLK_ID_PLL_MB,
7421         .ops       = &tegra_pllmb_ops,
7422         .reg       = 0x5e8,
7423         .parent    = &tegra_clk_osc,
7424         .max_rate  = 1866000000,
7425         .u.pll = {
7426                 .input_min = 9600000,
7427                 .input_max = 500000000,
7428                 .cf_min    = 9600000,
7429                 .cf_max    = 19200000,
7430                 .vco_min   = 800000000,
7431                 .vco_max   = 1866000000,
7432                 .freq_table = tegra_pll_m_freq_table,
7433                 .lock_delay = 300,
7434                 .misc0 = 0x5ec - 0x5e8,
7435                 .controls = &pllmb_controls,
7436                 .div_layout = &pllm_div_layout,         /* same, re-used */
7437                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7438                 .set_defaults = pllmb_set_defaults,
7439         },
7440 };
7441
7442 static struct clk_pll_freq_table tegra_pll_p_vco_freq_table[] = {
7443         { 12000000, 408000000,  34,  1, 1},
7444         { 13000000, 408000000, 408, 13, 1}, /* cf = 1MHz - only on FPGA */
7445         { 38400000, 408000000,  85,  8, 1}, /* cf = 4.8MHz, allowed exception */
7446         { 0, 0, 0, 0, 0, 0 },
7447 };
7448
7449 static struct clk_pll_controls pllp_controls = {
7450         .enable_mask = PLL_BASE_ENABLE,
7451         .bypass_mask = PLL_BASE_BYPASS,
7452         .iddq_mask = PLLP_MISC0_IDDQ,
7453         .iddq_reg_idx = PLL_MISC0_IDX,
7454         .lock_mask = PLLP_BASE_LOCK,
7455         .lock_reg_idx = PLL_BASE_IDX,
7456 };
7457
7458 static struct clk tegra_pll_p = {
7459         .name      = "pll_p",
7460         .clk_id    = TEGRA210_CLK_ID_PLL_P,
7461         .flags     = ENABLE_ON_INIT | PLL_FIXED,
7462         .ops       = &tegra_pllp_vco_ops,
7463         .reg       = 0xa0,
7464         .parent    = &tegra_pll_ref,
7465         .max_rate  = 700000000,
7466         .u.pll = {
7467                 .input_min = 9600000,
7468                 .input_max = 800000000,
7469                 .cf_min    = 9600000,
7470                 .cf_max    = 19200000,
7471                 .vco_min   = 350000000,
7472                 .vco_max   = 700000000,
7473                 .freq_table = tegra_pll_p_vco_freq_table,
7474                 .lock_delay = 300,
7475                 .misc0 = 0xac - 0xa0,
7476                 .misc1 = 0x680 - 0xa0,
7477                 .controls = &pllp_controls,
7478                 .div_layout = &pllre_div_layout,        /* same, re-used */
7479                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7480                 .set_defaults = pllp_set_defaults,
7481                 .vco_out = true,
7482                 .fixed_rate = 408000000,
7483         },
7484 };
7485
7486 static struct clk_mux_sel mux_clk_osc[] = {
7487         { .input = &tegra_clk_osc, .value = 0},
7488         { NULL, 0},
7489 };
7490
7491 static struct clk_mux_sel mux_pllp[] = {
7492         { .input = &tegra_pll_p, .value = 0},
7493         { NULL, 0},
7494 };
7495
7496 static struct clk tegra_pll_p_out_adsp = {
7497         .name      = "pll_p_out_adsp",
7498         .clk_id    = TEGRA210_CLK_ID_PLL_P_OUT_ADSP,
7499         .ops       = &tegra_periph_clk_ops,
7500         .inputs    = mux_pllp,
7501         .flags     = PERIPH_NO_RESET,
7502         .max_rate  = 700000000,
7503         .u.periph = {
7504                 .clk_num   = 187,
7505         },
7506 };
7507
7508 static struct clk tegra_pll_p_out_cpu = {
7509         .name      = "pll_p_out_cpu",
7510         .clk_id    = TEGRA210_CLK_ID_PLL_P_OUT_CPU,
7511         .ops       = &tegra_periph_clk_ops,
7512         .inputs    = mux_pllp,
7513         .flags     = PERIPH_NO_RESET,
7514         .max_rate  = 700000000,
7515         .u.periph = {
7516                 .clk_num   = 223,
7517         },
7518 };
7519
7520 static struct clk tegra_pll_p_out_hsio = {
7521         .name      = "pll_p_out_hsio",
7522         .clk_id    = TEGRA210_CLK_ID_PLL_P_OUT_HSIO,
7523         .ops       = &tegra_pll_div_ops,
7524         .parent    = &tegra_pll_p,
7525         .reg       = 0x680,
7526         .reg_shift = PLLP_MISC1_HSIO_EN_SHIFT,
7527 };
7528
7529 static struct clk tegra_pll_p_out_xusb = {
7530         .name      = "pll_p_out_xusb",
7531         .clk_id    = TEGRA210_CLK_ID_PLL_P_OUT_XUSB,
7532         .ops       = &tegra_pll_div_ops,
7533         .parent    = &tegra_pll_p_out_hsio,
7534         .reg       = 0x680,
7535         .reg_shift = PLLP_MISC1_XUSB_EN_SHIFT,
7536 };
7537
7538 static struct clk tegra_pll_p_out2 = {
7539         .name      = "pll_p_out2",
7540         .clk_id    = TEGRA210_CLK_ID_PLL_P_OUT2,
7541         .flags     = PLL_FIXED,
7542         .ops       = &tegra_pllp_out_ops,
7543         .parent    = &tegra_pll_p,
7544         .u.pll = {
7545                 .fixed_rate = 204000000,
7546         },
7547 };
7548
7549 static struct clk tegra_pll_p_out3 = {
7550         .name      = "pll_p_out3",
7551         .clk_id    = TEGRA210_CLK_ID_PLL_P_OUT3,
7552         .ops       = &tegra_pll_div_ops,
7553         .flags     = DIV_U71 | DIV_U71_FIXED,
7554         .parent    = &tegra_pll_p,
7555         .reg       = 0xa8,
7556         .reg_shift = 0,
7557         .max_rate  = 408000000,
7558 };
7559
7560 static struct clk tegra_pll_p_out4 = {
7561         .name      = "pll_p_out4",
7562         .clk_id    = TEGRA210_CLK_ID_PLL_P_OUT4,
7563         .ops       = &tegra_pll_div_ops,
7564         .flags     = DIV_U71 | DIV_U71_INT | DIV_U71_FIXED,
7565         .parent    = &tegra_pll_p_out_cpu,
7566         .reg       = 0xa8,
7567         .reg_shift = 16,
7568         .max_rate  = 408000000,
7569 };
7570
7571 static struct clk tegra_pll_p_out5 = {
7572         .name      = "pll_p_out5",
7573         .clk_id    = TEGRA210_CLK_ID_PLL_P_OUT5,
7574         .ops       = &tegra_pll_div_ops,
7575         .flags     = DIV_U71 | DIV_U71_FIXED,
7576         .parent    = &tegra_pll_p,
7577         .reg       = 0x67c,
7578         .reg_shift = 16,
7579         .max_rate  = 408000000,
7580 };
7581
7582 static struct clk tegra_pll_p_out_sor = {
7583         .name      = "sor_safe",
7584         .clk_id    = TEGRA210_CLK_ID_PLL_P_OUT_SOR,
7585         .ops       = &tegra_periph_clk_ops,
7586         .inputs    = mux_pllp,
7587         .flags     = PERIPH_NO_RESET,
7588         .max_rate  = 24000000,
7589         .mul       = 1,
7590         .div       = 17,
7591         .u.periph = {
7592                 .clk_num   = 222,
7593         },
7594 };
7595
7596 static struct clk_pll_freq_table tegra_pll_u_vco_freq_table[] = {
7597         { 12000000, 480000000, 40, 1, 1},
7598         { 13000000, 480000000, 36, 1, 1},       /* actual: 468.0 MHz */
7599         { 38400000, 480000000, 25, 2, 1},
7600         { 0, 0, 0, 0, 0, 0 },
7601 };
7602
7603 static struct clk_pll_controls pllu_controls = {
7604         .enable_mask = PLL_BASE_ENABLE,
7605         .bypass_mask = PLL_BASE_BYPASS,
7606         .iddq_mask = PLLU_MISC0_IDDQ,
7607         .iddq_reg_idx = PLL_MISC0_IDX,
7608         .lock_mask = PLLU_BASE_LOCK,
7609         .lock_reg_idx = PLL_BASE_IDX,
7610 };
7611
7612 static struct clk tegra_pll_u_vco = {
7613         .name      = "pll_u",
7614         .clk_id    = TEGRA210_CLK_ID_PLL_U,
7615         .flags     = PLLU | PLL_FIXED,
7616         .ops       = &tegra_pllu_vco_ops,
7617         .reg       = 0xc0,
7618         .parent    = &tegra_pll_ref,
7619         .max_rate  = 700000000,
7620         .u.pll = {
7621                 .input_min = 9600000,
7622                 .input_max = 800000000,
7623                 .cf_min    = 9600000,
7624                 .cf_max    = 19200000,
7625                 .vco_min   = 350000000,
7626                 .vco_max   = 700000000,
7627                 .freq_table = tegra_pll_u_vco_freq_table,
7628                 .lock_delay = 300,
7629                 .misc0 = 0xcc - 0xc0,
7630                 .misc1 = 0xc8 - 0xc0,
7631                 .controls = &pllu_controls,
7632                 .div_layout = &pllre_div_layout,        /* same, re-used */
7633                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7634                 .set_defaults = pllu_set_defaults,
7635                 .vco_out = true,
7636                 .fixed_rate = 480000000,
7637         },
7638 };
7639
7640 static struct clk tegra_pll_u_out = {
7641         .name      = "pll_u_out",
7642         .clk_id    = TEGRA210_CLK_ID_PLL_U_OUT,
7643         .flags     = PLLU | PLL_FIXED,
7644         .ops       = &tegra_pllu_out_ops,
7645         .parent    = &tegra_pll_u_vco,
7646         .u.pll = {
7647                 .fixed_rate = 240000000,
7648         },
7649 };
7650
7651 static struct clk tegra_pll_u_out1 = {
7652         .name      = "pll_u_out1",
7653         .clk_id    = TEGRA210_CLK_ID_PLL_U_OUT1,
7654         .ops       = &tegra_pll_div_ops,
7655         .flags     = DIV_U71 | DIV_U71_FIXED,
7656         .parent    = &tegra_pll_u_out,
7657         .reg       = 0xc4,
7658         .reg_shift = 0,
7659         .max_rate  = 480000000,
7660         .u.pll_div = {
7661                 .default_rate = 48000000,
7662         },
7663 };
7664
7665 static struct clk tegra_pll_u_out2 = {
7666         .name      = "pll_u_out2",
7667         .clk_id    = TEGRA210_CLK_ID_PLL_U_OUT2,
7668         .ops       = &tegra_pll_div_ops,
7669         .flags     = DIV_U71 | DIV_U71_FIXED,
7670         .parent    = &tegra_pll_u_out,
7671         .reg       = 0xc4,
7672         .reg_shift = 16,
7673         .max_rate  = 480000000,
7674         .u.pll_div = {
7675                 .default_rate = 60000000,
7676         },
7677 };
7678
7679 static struct clk tegra_pll_u_480M = {
7680         .name      = "pll_u_480M",
7681         .clk_id    = TEGRA210_CLK_ID_PLL_U_480M,
7682         .flags     = PLLU,
7683         .ops       = &tegra_pll_div_ops,
7684         .reg       = 0xc0,
7685         .reg_shift = 22,
7686         .parent    = &tegra_pll_u_vco,
7687         .mul       = 1,
7688         .div       = 1,
7689 };
7690
7691 static struct clk tegra_pll_u_60M = {
7692         .name      = "pll_u_60M",
7693         .clk_id    = TEGRA210_CLK_ID_PLL_U_60M,
7694         .flags     = PLLU,
7695         .ops       = &tegra_pll_div_ops,
7696         .reg       = 0xc0,
7697         .reg_shift = 23,
7698         .parent    = &tegra_pll_u_out2,
7699         .mul       = 1,
7700         .div       = 1,
7701 };
7702
7703 static struct clk tegra_pll_u_48M = {
7704         .name      = "pll_u_48M",
7705         .clk_id    = TEGRA210_CLK_ID_PLL_U_48M,
7706         .flags     = PLLU,
7707         .ops       = &tegra_pll_div_ops,
7708         .reg       = 0xc0,
7709         .reg_shift = 25,
7710         .parent    = &tegra_pll_u_out1,
7711         .mul       = 1,
7712         .div       = 1,
7713 };
7714
7715 static struct clk tegra_dfll_cpu = {
7716         .name      = "dfll_cpu",
7717         .clk_id    = TEGRA210_CLK_ID_DFLL_CPU,
7718         .flags     = DFLL,
7719         .ops       = &tegra_dfll_ops,
7720         .reg       = 0x2f4,
7721         .max_rate  = 3000000000UL,
7722 };
7723
7724 static struct clk_pll_freq_table tegra_pll_e_freq_table[] = {
7725         { 672000000, 100000000, 125, 42,  20,},
7726         { 624000000, 100000000, 125, 39,  20,},
7727         { 336000000, 100000000, 125, 21,  20,},
7728         { 312000000, 100000000, 200, 26,  24,},
7729         { 38400000,  100000000, 125, 2,   24,},
7730         { 13000000,  100000000, 184, 1,   24,}, /* actual: 99.6MHz (FPGA) */
7731         { 12000000,  100000000, 200, 1,   24,},
7732         { 0, 0, 0, 0, 0, 0 },
7733 };
7734
7735 static struct clk_mux_sel mux_pll_e_inputs[] = {
7736         { .input = &tegra_pll_ref,      .value = 0},
7737         { .input = &tegra_pll_p,        .value = 1},
7738         { .input = &tegra_pll_re_vco,   .value = 2},
7739         { .input = &tegra_pll_re_vco,   .value = 3},
7740         { NULL, 0},
7741 };
7742
7743 static struct clk tegra_pll_e = {
7744         .name      = "pll_e",
7745         .clk_id    = TEGRA210_CLK_ID_PLL_E,
7746         .flags     = PLL_FIXED,
7747         .ops       = &tegra_plle_ops,
7748         .reg       = 0xe8,
7749         .max_rate  = 100000000,
7750         .inputs    = mux_pll_e_inputs,
7751         .u.pll = {
7752                 .input_min = 12000000,
7753                 .input_max = 800000000,
7754                 .cf_min    = 12000000,
7755                 .cf_max    = 38400000,
7756                 .vco_min   = 1600000000,
7757                 .vco_max   = 2500000000U,
7758                 .freq_table = tegra_pll_e_freq_table,
7759                 .lock_delay = 300,
7760                 .fixed_rate = 100000000,
7761                 .misc0 = 0xec - 0xe8,
7762                 .round_p_to_pdiv = pll_qlin_p_to_pdiv,
7763         },
7764 };
7765
7766 static struct clk tegra_pll_e_gate = {
7767         .name      = "plle_gate",
7768         .clk_id    = TEGRA210_CLK_ID_PLL_E_GATE,
7769         .parent    = &tegra_pll_e,
7770         .ops       = &tegra_cml_clk_ops,
7771         .reg       = 0xec,
7772         .max_rate  = 100000000,
7773         .u.periph  = {
7774                 .clk_num = 15,
7775         },
7776 };
7777
7778 static struct clk tegra_cml0_clk = {
7779         .name      = "cml0",
7780         .clk_id    = TEGRA210_CLK_ID_PLL_E_CML0,
7781         .parent    = &tegra_pll_e,
7782         .ops       = &tegra_cml_clk_ops,
7783         .reg       = PLLE_AUX,
7784         .max_rate  = 100000000,
7785         .u.periph  = {
7786                 .clk_num = 0,
7787         },
7788 };
7789
7790 static struct clk tegra_cml1_clk = {
7791         .name      = "cml1",
7792         .clk_id    = TEGRA210_CLK_ID_PLL_E_CML1,
7793         .parent    = &tegra_pll_e,
7794         .ops       = &tegra_cml_clk_ops,
7795         .reg       = PLLE_AUX,
7796         .max_rate  = 100000000,
7797         .u.periph  = {
7798                 .clk_num   = 1,
7799         },
7800 };
7801
7802 static struct clk tegra_pciex_clk = {
7803         .name      = "pciex",
7804         .clk_id    = TEGRA210_CLK_ID_PCIEX,
7805         .parent    = &tegra_pll_e,
7806         .ops       = &tegra_pciex_clk_ops,
7807         .max_rate  = 500000000,
7808         .u.periph  = {
7809                 .clk_num   = 74,
7810         },
7811 };
7812
7813 /* Audio sync clocks */
7814 #define SYNC_SOURCE(_id, _dev, _clk_id)                 \
7815         {                                               \
7816                 .name      = #_id "_sync",              \
7817                 .clk_id    = TEGRA210_CLK_ID_##_clk_id##_SYNC, \
7818                 .lookup    = {                          \
7819                         .dev_id    = #_dev ,            \
7820                         .con_id    = "ext_audio_sync",  \
7821                 },                                      \
7822                 .rate      = 24000000,                  \
7823                 .max_rate  = 24576000,                  \
7824                 .ops       = &tegra_sync_source_ops     \
7825         }
7826 static struct clk tegra_sync_source_list[] = {
7827         SYNC_SOURCE(spdif_in, tegra210-spdif, SPDIF_IN),
7828         SYNC_SOURCE(i2s0, tegra210-i2s.0, I2S0),
7829         SYNC_SOURCE(i2s1, tegra210-i2s.1, I2S1),
7830         SYNC_SOURCE(i2s2, tegra210-i2s.2, I2S2),
7831         SYNC_SOURCE(i2s3, tegra210-i2s.3, I2S3),
7832         SYNC_SOURCE(i2s4, tegra210-i2s.4, I2S4),
7833         SYNC_SOURCE(vimclk, vimclk, VIMCLK),
7834 };
7835
7836 static struct clk_mux_sel mux_d_audio_clk[] = {
7837         { .input = &tegra_pll_a_out0,           .value = 0},
7838         { .input = &tegra_pll_p,                .value = 0x8000},
7839         { .input = &tegra_clk_m,                .value = 0xc000},
7840         { .input = &tegra_sync_source_list[0],  .value = 0xE000},
7841         { .input = &tegra_sync_source_list[1],  .value = 0xE001},
7842         { .input = &tegra_sync_source_list[2],  .value = 0xE002},
7843         { .input = &tegra_sync_source_list[3],  .value = 0xE003},
7844         { .input = &tegra_sync_source_list[4],  .value = 0xE004},
7845         { .input = &tegra_sync_source_list[5],  .value = 0xE005},
7846         { .input = &tegra_pll_a_out0,           .value = 0xE006},
7847         { .input = &tegra_sync_source_list[6],  .value = 0xE007},
7848         { NULL, 0 }
7849 };
7850
7851 static struct clk_mux_sel mux_audio_sync_clk[] =
7852 {
7853         { .input = &tegra_sync_source_list[0],  .value = 0},
7854         { .input = &tegra_sync_source_list[1],  .value = 1},
7855         { .input = &tegra_sync_source_list[2],  .value = 2},
7856         { .input = &tegra_sync_source_list[3],  .value = 3},
7857         { .input = &tegra_sync_source_list[4],  .value = 4},
7858         { .input = &tegra_sync_source_list[5],  .value = 5},
7859         { .input = &tegra_pll_a_out0,           .value = 6},
7860         { .input = &tegra_sync_source_list[6],  .value = 7},
7861         { NULL, 0 }
7862 };
7863
7864 #define AUDIO_SYNC_CLK(_id, _dev, _index, _clk_id)      \
7865         {                                               \
7866                 .name      = #_id,                      \
7867                 .clk_id    = TEGRA210_CLK_ID_##_clk_id, \
7868                 .lookup    = {                          \
7869                         .dev_id    = #_dev,             \
7870                         .con_id    = "audio_sync",      \
7871                 },                                      \
7872                 .inputs    = mux_audio_sync_clk,        \
7873                 .reg       = 0x4A0 + (_index) * 4,      \
7874                 .max_rate  = 24576000,                  \
7875                 .ops       = &tegra_audio_sync_clk_ops  \
7876         }
7877 static struct clk tegra_clk_audio_list[] = {
7878         AUDIO_SYNC_CLK(audio0, tegra210-i2s.0, 0, AUDIO0),
7879         AUDIO_SYNC_CLK(audio1, tegra210-i2s.1, 1, AUDIO1),
7880         AUDIO_SYNC_CLK(audio2, tegra210-i2s.2, 2, AUDIO2),
7881         AUDIO_SYNC_CLK(audio3, tegra210-i2s.3, 3, AUDIO3),
7882         AUDIO_SYNC_CLK(audio4, tegra210-i2s.4, 4, AUDIO4),
7883         AUDIO_SYNC_CLK(audio, tegra210-spdif,  5, AUDIO),       /* SPDIF */
7884 };
7885
7886 #define AUDIO_SYNC_2X_CLK(_id, _dev, _index, _clk_id)           \
7887         {                                                       \
7888                 .name      = #_id "_2x",                        \
7889                 .clk_id    = TEGRA210_CLK_ID_##_clk_id##_2x,    \
7890                 .lookup    = {                                  \
7891                         .dev_id    = #_dev,                     \
7892                         .con_id    = "audio_sync_2x"            \
7893                 },                                              \
7894                 .flags     = PERIPH_NO_RESET,                   \
7895                 .max_rate  = 49152000,                          \
7896                 .ops       = &tegra_clk_double_ops,             \
7897                 .reg       = 0x49C,                             \
7898                 .reg_shift = 24 + (_index),                     \
7899                 .parent    = &tegra_clk_audio_list[(_index)],   \
7900                 .u.periph = {                                   \
7901                         .clk_num = 113 + (_index),              \
7902                 },                                              \
7903         }
7904 static struct clk tegra_clk_audio_2x_list[] = {
7905         AUDIO_SYNC_2X_CLK(audio, tegra210-spdif, 5, AUDIO),     /* SPDIF */
7906 };
7907
7908 #define MUX_I2S_SPDIF(_id, _index)                                      \
7909 static struct clk_mux_sel mux_pllaout0_##_id##_pllp_clkm[] = {          \
7910         {.input = &tegra_pll_a_out0, .value = 0},                       \
7911         {.input = &tegra_clk_audio_list[(_index)], .value = 2},         \
7912         {.input = &tegra_pll_p, .value = 4},                            \
7913         {.input = &tegra_clk_m, .value = 6},                            \
7914         { NULL, 0},                                                     \
7915 }
7916 MUX_I2S_SPDIF(audio0, 0);
7917 MUX_I2S_SPDIF(audio1, 1);
7918 MUX_I2S_SPDIF(audio2, 2);
7919 MUX_I2S_SPDIF(audio3, 3);
7920 MUX_I2S_SPDIF(audio4, 4);
7921
7922 /* SPDIF */
7923 static struct clk_mux_sel mux_pllaout0_audio_2x_pllp_clkm[] = {
7924         {.input = &tegra_pll_a_out0, .value = 0},
7925         {.input = &tegra_clk_audio_2x_list[0], .value = 2},
7926         {.input = &tegra_pll_p, .value = 4},
7927         {.input = &tegra_clk_m, .value = 6},
7928         { NULL, 0},
7929 };
7930
7931 /* Audio sync dmic clocks */
7932 #define AUDIO_SYNC_DMIC_CLK(_id, _dev, _reg, _clk_id)   \
7933         {                                               \
7934                 .name      = #_id "_dmic",              \
7935                 .clk_id    = TEGRA210_CLK_ID_##_clk_id##_DMIC, \
7936                 .lookup    = {                          \
7937                         .dev_id    = #_dev,             \
7938                         .con_id    = "audio_sync_dmic", \
7939                 },                                      \
7940                 .inputs    = mux_audio_sync_clk,        \
7941                 .reg       = _reg,                      \
7942                 .max_rate  = 24576000,                  \
7943                 .ops       = &tegra_audio_sync_clk_ops  \
7944         }
7945
7946 static struct clk tegra_clk_audio_dmic_list[] = {
7947         AUDIO_SYNC_DMIC_CLK(audio0, tegra210-i2s.0, 0x560, AUDIO0),
7948         AUDIO_SYNC_DMIC_CLK(audio1, tegra210-i2s.1, 0x564, AUDIO1),
7949         AUDIO_SYNC_DMIC_CLK(audio2, tegra210-i2s.2, 0x6b8, AUDIO2),
7950 };
7951
7952 #define MUX_AUDIO_DMIC(_id, _index)                                     \
7953 static struct clk_mux_sel mux_pllaout0_##_id##_dmic_pllp_clkm[] = {     \
7954         {.input = &tegra_pll_a_out0, .value = 0},                       \
7955         {.input = &tegra_clk_audio_dmic_list[(_index)], .value = 1},            \
7956         {.input = &tegra_pll_p, .value = 2},                            \
7957         {.input = &tegra_clk_m, .value = 3},                            \
7958         { NULL, 0},                                                     \
7959 }
7960 MUX_AUDIO_DMIC(audio0, 0);
7961 MUX_AUDIO_DMIC(audio1, 1);
7962 MUX_AUDIO_DMIC(audio2, 2);
7963
7964 /* External clock outputs (through PMC) */
7965 #define MUX_EXTERN_OUT(_id)                                             \
7966 static struct clk_mux_sel mux_clkm_clkm2_clkm4_extern##_id[] = {        \
7967         {.input = &tegra_clk_m,         .value = 0},                    \
7968         {.input = &tegra_clk_m_div2,    .value = 1},                    \
7969         {.input = &tegra_clk_m_div4,    .value = 2},                    \
7970         {.input = NULL,                 .value = 3}, /* placeholder */  \
7971         { NULL, 0},                                                     \
7972 }
7973 MUX_EXTERN_OUT(1);
7974 MUX_EXTERN_OUT(2);
7975 MUX_EXTERN_OUT(3);
7976
7977 static struct clk_mux_sel *mux_extern_out_list[] = {
7978         mux_clkm_clkm2_clkm4_extern1,
7979         mux_clkm_clkm2_clkm4_extern2,
7980         mux_clkm_clkm2_clkm4_extern3,
7981 };
7982
7983 #define CLK_OUT_CLK(_id, _max_rate)                             \
7984         {                                                       \
7985                 .name      = "clk_out_" #_id,                   \
7986                 .clk_id    = TEGRA210_CLK_ID_CLK_OUT_##_id,     \
7987                 .lookup    = {                                  \
7988                         .dev_id    = "clk_out_" #_id,           \
7989                         .con_id    = "extern" #_id,             \
7990                 },                                              \
7991                 .ops       = &tegra_clk_out_ops,                \
7992                 .reg       = 0x1a8,                             \
7993                 .inputs    = mux_clkm_clkm2_clkm4_extern##_id,  \
7994                 .flags     = MUX_CLK_OUT,                       \
7995                 .max_rate  = _max_rate,                         \
7996                 .u.periph = {                                   \
7997                         .clk_num   = (_id - 1) * 8 + 2,         \
7998                 },                                              \
7999         }
8000 static struct clk tegra_clk_out_list[] = {
8001         CLK_OUT_CLK(1, 40800000),
8002         CLK_OUT_CLK(2, 40800000),
8003         CLK_OUT_CLK(3, 40800000),
8004 };
8005
8006 /* called after peripheral external clocks are initialized */
8007 static void init_clk_out_mux(void)
8008 {
8009         int i;
8010         struct clk *c;
8011
8012         /* output clock con_id is the name of peripheral
8013            external clock connected to input 3 of the output mux */
8014         for (i = 0; i < ARRAY_SIZE(tegra_clk_out_list); i++) {
8015                 c = tegra_get_clock_by_name(
8016                         tegra_clk_out_list[i].lookup.con_id);
8017                 if (!c)
8018                         pr_err("%s: could not find clk %s\n", __func__,
8019                                tegra_clk_out_list[i].lookup.con_id);
8020                 mux_extern_out_list[i][3].input = c;
8021         }
8022 }
8023
8024 /* Peripheral muxes */
8025 static struct clk_mux_sel mux_cclk_g[] = {
8026         { .input = &tegra_clk_m,        .value = 0},
8027         { .input = &tegra_clk_32k,      .value = 2},
8028         { .input = &tegra_pll_p_out_cpu, .value = 4},
8029         { .input = &tegra_pll_p_out4,   .value = 5},
8030         { .input = &tegra_pll_x,        .value = 8},
8031         { .input = &tegra_dfll_cpu,     .value = 15},
8032         { NULL, 0},
8033 };
8034
8035 static struct clk_mux_sel mux_cclk_lp[] = {
8036         { .input = &tegra_clk_m,        .value = 0},
8037         { .input = &tegra_clk_32k,      .value = 2},
8038         { .input = &tegra_pll_p_out_cpu, .value = 4},
8039         { .input = &tegra_pll_p_out4,   .value = 5},
8040         { .input = &tegra_pll_x,        .value = 8},
8041         { NULL, 0},
8042 };
8043
8044 static struct clk_mux_sel mux_sclk[] = {
8045         { .input = &tegra_clk_m,        .value = 0},
8046         { .input = &tegra_pll_c_out1,   .value = 1},
8047         { .input = &tegra_pll_c4_out3,  .value = 2},
8048         { .input = &tegra_pll_p,        .value = 3},
8049         { .input = &tegra_pll_p_out2,   .value = 4},
8050         { .input = &tegra_pll_c4_out1,  .value = 5},
8051         { .input = &tegra_clk_32k,      .value = 6},
8052         { .input = &tegra_pll_c4_out2,  .value = 7},
8053         { NULL, 0},
8054 };
8055
8056 /* ADSP cluster clocks */
8057 static struct clk_mux_sel mux_aclk_adsp[] = {
8058         { .input = &tegra_pll_p_out_adsp, .value = 2},
8059         { .input = &tegra_pll_a_out0_out_adsp, .value = 3},
8060         { .input = &tegra_clk_m,        .value = 6},
8061         { .input = &tegra_pll_a_out_adsp, .value = 7},
8062         { .input = &tegra_pll_a1,       .value = 8},
8063         { NULL, 0},
8064 };
8065
8066 static void tegra21_adsp_clk_reset(struct clk *c, bool assert)
8067 {
8068         unsigned long reg = assert ? RST_DEVICES_SET_Y : RST_DEVICES_CLR_Y;
8069         u32 val = ADSP_NEON | ADSP_SCU | ADSP_WDT | ADSP_DBG
8070                 | ADSP_PERIPH | ADSP_CORE;
8071
8072         pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
8073
8074         if (assert)
8075                 clk_writel_delay(val | ADSP_INTF, reg);
8076         else {
8077                 clk_writel_delay(ADSP_INTF, reg);
8078                 /*
8079                  * Considering adsp cpu clock (min: 12.5MHZ, max: 1GHz)
8080                  * a delay of 5us ensures that it's at least
8081                  * 6 * adsp_cpu_cycle_period long.
8082                  */
8083                 udelay(5);
8084                 clk_writel_delay(val, reg);
8085         }
8086 }
8087
8088 static int tegra21_adsp_clk_enable(struct clk *c)
8089 {
8090         u32 val = ADSP_NEON | ADSP_CORE;
8091
8092         clk_writel_delay(val, CLK_OUT_ENB_SET_Y);
8093         return 0;
8094 }
8095
8096 static void tegra21_adsp_clk_disable(struct clk *c)
8097 {
8098         u32 val = ADSP_NEON | ADSP_CORE;
8099
8100         clk_writel_delay(val, CLK_OUT_ENB_CLR_Y);
8101 }
8102
8103 static void tegra21_adsp_clk_init(struct clk *c)
8104 {
8105         /*
8106          * Check/enforce ADSP clock default configuration:
8107          * - parent clk_m
8108          * - disabled
8109          */
8110         tegra21_super_clk_set_parent(c, &tegra_clk_m);
8111         tegra21_super_clk_init(c);
8112
8113         /*
8114          * CPU and system super clocks cannot be disabled, and super clock init
8115          * always marks clock sate ON, which is not true for ADSP. Need explicit
8116          * check here.
8117          */
8118         c->state = clk_readl(CLK_OUT_ENB_Y)&(ADSP_NEON | ADSP_CORE) ? ON : OFF;
8119         if (c->state == ON) {
8120                 WARN(1, "Tegra ADSP clock is running on boot: turning Off\n");
8121                 tegra21_adsp_clk_disable(c);
8122                 c->state = OFF;
8123         }
8124 }
8125
8126 static struct clk_ops tegra_adsp_ops = {
8127         .init           = tegra21_adsp_clk_init,
8128         .enable         = tegra21_adsp_clk_enable,
8129         .disable        = tegra21_adsp_clk_disable,
8130         .set_parent     = tegra21_super_clk_set_parent,
8131         .reset          = tegra21_adsp_clk_reset,
8132 };
8133
8134 static struct clk tegra_clk_aclk_adsp = {
8135         .name   = "adsp",
8136         .clk_id = TEGRA210_CLK_ID_ADSP,
8137         .flags  = DIV_U71 | DIV_U71_INT,
8138         .inputs = mux_aclk_adsp,
8139         .reg    = 0x6e0,
8140         .ops    = &tegra_adsp_ops,
8141         .max_rate = 1200000000,
8142 };
8143
8144 static struct clk_ops tegra_adsp_bus_ops = {
8145         .init     = tegra21_adsp_bus_clk_init,
8146         .enable   = tegra21_adsp_bus_clk_enable,
8147         .disable  = tegra21_adsp_bus_clk_disable,
8148         .set_rate = tegra21_adsp_bus_clk_set_rate,
8149         .round_rate = tegra21_adsp_bus_clk_round_rate,
8150         .shared_bus_update = tegra21_clk_shared_connector_update, /* re-use */
8151 };
8152
8153 static struct raw_notifier_head adsp_bus_rate_change_nh;
8154 static struct clk tegra_clk_virtual_adsp_bus = {
8155         .name      = "adsp_bus",
8156         .clk_id    = TEGRA210_CLK_ID_ADSP_BUS,
8157         .parent    = &tegra_clk_aclk_adsp,
8158         .ops       = &tegra_adsp_bus_ops,
8159         .shared_bus_flags = SHARED_BUS_RETENTION,
8160         .max_rate  = 1200000000,
8161         .u.cpu = {
8162                 .main      = &tegra_pll_a1,
8163                 .backup    = &tegra_pll_p_out_adsp,
8164         },
8165         .rate_change_nh = &adsp_bus_rate_change_nh,
8166 };
8167
8168 /* CPU clusters clocks */
8169 static struct clk tegra_clk_cclk_g = {
8170         .name   = "cclk_g",
8171         .clk_id = TEGRA210_CLK_ID_CCLK_G,
8172         .flags  = DIV_U71 | DIV_U71_INT,
8173         .inputs = mux_cclk_g,
8174         .reg    = 0x368,
8175         .ops    = &tegra_super_ops,
8176         .max_rate = 3000000000UL,
8177 };
8178
8179 static struct clk tegra_clk_cclk_lp = {
8180         .name   = "cclk_lp",
8181         .clk_id = TEGRA210_CLK_ID_CCLK_LP,
8182         .flags  = DIV_U71 | DIV_U71_INT,
8183         .inputs = mux_cclk_lp,
8184         .reg    = 0x370,
8185         .ops    = &tegra_super_ops,
8186         .max_rate = 1350000000,
8187 };
8188
8189 static struct clk tegra_clk_virtual_cpu_g = {
8190         .name      = "cpu_g",
8191         .clk_id    = TEGRA210_CLK_ID_CPU_G,
8192         .parent    = &tegra_clk_cclk_g,
8193         .ops       = &tegra_cpu_ops,
8194         .max_rate  = 3000000000UL,
8195         .u.cpu = {
8196                 .main      = &tegra_pll_x,
8197                 .backup    = &tegra_pll_p_out4,
8198                 .dynamic   = &tegra_dfll_cpu,
8199                 .mode      = MODE_G,
8200         },
8201 };
8202
8203 static struct clk tegra_clk_virtual_cpu_lp = {
8204         .name      = "cpu_lp",
8205         .clk_id    = TEGRA210_CLK_ID_CPU_LP,
8206         .parent    = &tegra_clk_cclk_lp,
8207         .ops       = &tegra_cpu_ops,
8208         .max_rate  = 1350000000,
8209         .min_rate  = 3187500,
8210         .u.cpu = {
8211                 .main      = &tegra_pll_x,
8212                 .backup    = &tegra_pll_p_out4,
8213                 .mode      = MODE_LP,
8214         },
8215 };
8216
8217 static struct clk_mux_sel mux_cpu_cmplx[] = {
8218         { .input = &tegra_clk_virtual_cpu_g,    .value = 0},
8219         { .input = &tegra_clk_virtual_cpu_lp,   .value = 1},
8220         { NULL, 0},
8221 };
8222
8223 static struct clk tegra_clk_cpu_cmplx = {
8224         .name      = "cpu",
8225         .clk_id    = TEGRA210_CLK_ID_CCPLEX,
8226         .inputs    = mux_cpu_cmplx,
8227         .ops       = &tegra_cpu_cmplx_ops,
8228         .max_rate  = 3000000000UL,
8229 };
8230
8231 /* System bus clocks */
8232 static struct clk tegra_clk_sclk_mux = {
8233         .name   = "sclk_mux",
8234         .clk_id = TEGRA210_CLK_ID_SCLK_MUX,
8235         .inputs = mux_sclk,
8236         .reg    = 0x28,
8237         .ops    = &tegra_super_ops,
8238         .max_rate = 1200000000,
8239         .min_rate = 12000000,
8240 };
8241
8242 static struct clk_mux_sel sclk_mux_out[] = {
8243         { .input = &tegra_clk_sclk_mux, .value = 0},
8244         { NULL, 0},
8245 };
8246
8247 static struct clk tegra_clk_sclk_div = {
8248         .name = "sclk_div",
8249         .clk_id = TEGRA210_CLK_ID_SCLK_DIV,
8250         .ops = &tegra_periph_clk_ops,
8251         .reg = 0x400,
8252         .max_rate = 600000000,
8253         .min_rate = 12000000,
8254         .inputs = sclk_mux_out,
8255         .flags = DIV_U71 | PERIPH_NO_ENB | PERIPH_NO_RESET,
8256 };
8257
8258 static struct clk tegra_clk_sclk = {
8259         .name = "sclk",
8260         .clk_id = TEGRA210_CLK_ID_SCLK_SKIP,
8261         .flags  = DIV_BUS,
8262         .ops = &tegra_clk_super_skip_ops,
8263         .reg = 0x2c,
8264         .parent = &tegra_clk_sclk_div,
8265 };
8266
8267 static struct clk tegra_clk_cop = {
8268         .name      = "cop",
8269         .parent    = &tegra_clk_sclk,
8270         .ops       = &tegra_cop_ops,
8271         .max_rate  = 600000000,
8272 };
8273
8274 static struct clk tegra_clk_hclk = {
8275         .name           = "hclk",
8276         .clk_id         = TEGRA210_CLK_ID_HCLK,
8277         .flags          = DIV_BUS,
8278         .parent         = &tegra_clk_sclk,
8279         .reg            = 0x30,
8280         .reg_shift      = 4,
8281         .ops            = &tegra_bus_ops,
8282         .max_rate       = 600000000,
8283         .min_rate       = 12000000,
8284 };
8285
8286 static struct clk tegra_clk_pclk = {
8287         .name           = "pclk",
8288         .clk_id         = TEGRA210_CLK_ID_PCLK,
8289         .flags          = DIV_BUS,
8290         .parent         = &tegra_clk_hclk,
8291         .reg            = 0x30,
8292         .reg_shift      = 0,
8293         .ops            = &tegra_bus_ops,
8294         .max_rate       = 600000000,
8295         .min_rate       = 12000000,
8296 };
8297
8298 static struct raw_notifier_head sbus_rate_change_nh;
8299
8300 static struct clk tegra_clk_sbus_cmplx = {
8301         .name      = "sbus",
8302         .clk_id    = TEGRA210_CLK_ID_SBUS,
8303         .parent    = &tegra_clk_sclk,
8304         .ops       = &tegra_sbus_cmplx_ops,
8305         .u.system  = {
8306                 .pclk = &tegra_clk_pclk,
8307                 .hclk = &tegra_clk_hclk,
8308                 .sclk_low = &tegra_pll_p,
8309                 .sclk_high = &tegra_pll_p,
8310         },
8311         .rate_change_nh = &sbus_rate_change_nh,
8312 };
8313
8314 static struct clk tegra_clk_ahb = {
8315         .name      = "ahb.sclk",
8316         .flags     = DIV_BUS,
8317         .parent    = &tegra_clk_sbus_cmplx,
8318         .ops       = &tegra_clk_ahb_ops,
8319 };
8320
8321 static struct clk tegra_clk_apb = {
8322         .name      = "apb.sclk",
8323         .flags     = DIV_BUS,
8324         .parent    = &tegra_clk_ahb,
8325         .ops       = &tegra_clk_apb_ops,
8326 };
8327
8328 static struct clk tegra_clk_blink = {
8329         .name           = "blink",
8330         .clk_id         = TEGRA210_CLK_ID_BLINK,
8331         .parent         = &tegra_clk_32k,
8332         .reg            = 0x40,
8333         .ops            = &tegra_blink_clk_ops,
8334         .max_rate       = 32768,
8335 };
8336
8337 static struct clk_ops tegra_xusb_padctl_ops = {
8338         .reset    = tegra21_periph_clk_reset,
8339 };
8340
8341 static struct clk tegra_clk_xusb_padctl = {
8342         .name      = "xusb_padctl",
8343         .clk_id = TEGRA210_CLK_ID_XUSB_PADCTRL,
8344         .ops       = &tegra_xusb_padctl_ops,
8345         .u.periph  = {
8346                 .clk_num = 142,
8347         },
8348 };
8349
8350 static struct clk tegra_clk_sata_uphy = {
8351         .name      = "sata_uphy",
8352         .clk_id = TEGRA210_CLK_ID_SATA_UPHY,
8353         .ops       = &tegra_xusb_padctl_ops,
8354         .u.periph  = {
8355                 .clk_num = 204,
8356         },
8357 };
8358
8359 static struct clk tegra_pex_uphy_clk = {
8360         .name      = "pex_uphy",
8361         .clk_id = TEGRA210_CLK_ID_PEX_UPHY,
8362         .ops       = &tegra_xusb_padctl_ops,
8363         .u.periph  = {
8364                 .clk_num   = 205,
8365         },
8366 };
8367
8368 static struct clk tegra_clk_usb2_hsic_trk = {
8369         .name      = "usb2_hsic_trk",
8370         .flags     = DIV_U71 | PERIPH_NO_ENB | PERIPH_NO_RESET,
8371         .inputs    = mux_clk_osc,
8372         .reg       = 0x6cc,
8373         .ops       = &tegra_periph_clk_ops,
8374         .max_rate  = 38400000,
8375 };
8376
8377 /* Multimedia modules muxes */
8378 static struct clk_mux_sel mux_pllc2_c_c3_pllp_plla1_clkm[] = {
8379         { .input = &tegra_pll_c2, .value = 1},
8380         { .input = &tegra_pll_c,  .value = 2},
8381         { .input = &tegra_pll_c3, .value = 3},
8382         { .input = &tegra_pll_p,  .value = 4},
8383         { .input = &tegra_pll_a1, .value = 6},
8384         { .input = &tegra_clk_m, .value = 7},
8385         { NULL, 0},
8386 };
8387
8388 static struct clk_mux_sel mux_pllc4_out1_pllc_pllc4_out2_pllp_clkm_plla_pllc4_out0[] = {
8389         { .input = &tegra_pll_c4_out1,  .value = 0},
8390         { .input = &tegra_pll_c,  .value = 2},
8391         { .input = &tegra_pll_c4_out2,  .value = 3},
8392         { .input = &tegra_pll_p,  .value = 4},
8393         { .input = &tegra_clk_m,  .value = 5},
8394         { .input = &tegra_pll_a_out0, .value = 6},
8395         { .input = &tegra_pll_c4_out0, .value = 7},
8396         { NULL, 0},
8397 };
8398
8399 static struct clk_mux_sel mux_pllc_pllp_plla[] = {
8400         { .input = &tegra_pll_c, .value = 2},
8401         { .input = &tegra_pll_p, .value = 4},
8402         { .input = &tegra_pll_a_out0, .value = 6},
8403         { NULL, 0},
8404 };
8405
8406 static struct clk_mux_sel mux_clkm_pllc_pllp_plla[] = {
8407         { .input = &tegra_clk_m, .value = 0},
8408         { .input = &tegra_pll_c, .value = 1},
8409         { .input = &tegra_pll_p, .value = 2},
8410         { .input = &tegra_pll_a_out0, .value = 3},
8411         { NULL, 0},
8412 };
8413
8414 static struct clk_mux_sel mux_pllc_pllp_plla1_pllc2_c3_clkm[] = {
8415         { .input = &tegra_pll_c, .value = 1},
8416         { .input = &tegra_pll_p, .value = 2},
8417         { .input = &tegra_pll_a1, .value = 3},
8418         { .input = &tegra_pll_c2, .value = 4},
8419         { .input = &tegra_pll_c3, .value = 5},
8420         { .input = &tegra_clk_m, .value = 6},
8421         { NULL, 0},
8422 };
8423
8424 static struct clk_mux_sel mux_pllc2_c_c3_pllp_clkm_plla1_pllc4[] = {
8425         { .input = &tegra_pll_c2, .value = 1},
8426         { .input = &tegra_pll_c, .value = 2},
8427         { .input = &tegra_pll_c3, .value = 3},
8428         { .input = &tegra_pll_p, .value = 4},
8429         { .input = &tegra_clk_m, .value = 5},
8430         { .input = &tegra_pll_a1, .value = 6},
8431         { .input = &tegra_pll_c4_out0, .value = 7},
8432         { NULL, 0},
8433 };
8434
8435 static struct clk_mux_sel mux_pllc_pllp_plla1_pllc2_c3_clkm_pllc4[] = {
8436         { .input = &tegra_pll_c, .value = 1},
8437         { .input = &tegra_pll_p, .value = 2},
8438         { .input = &tegra_pll_a1, .value = 3},
8439         { .input = &tegra_pll_c2, .value = 4},
8440         { .input = &tegra_pll_c3, .value = 5},
8441         { .input = &tegra_clk_m, .value = 6},
8442         { .input = &tegra_pll_c4_out0, .value = 7},
8443         { NULL, 0},
8444 };
8445
8446 static struct clk_mux_sel mux_plla_pllc4_out0_pllc_pllc4_out1_pllp_clkm_pllc4_out2[] = {
8447         { .input = &tegra_pll_a_out0, .value = 0},
8448         { .input = &tegra_pll_c4_out0, .value = 1},
8449         { .input = &tegra_pll_c, .value = 2},
8450         { .input = &tegra_pll_c4_out1, .value = 3},
8451         { .input = &tegra_pll_p, .value = 4},
8452         { .input = &tegra_pll_c4_out2, .value = 5},
8453         { .input = &tegra_clk_m, .value = 6},
8454         { NULL, 0},
8455 };
8456
8457 /* EMC muxes */
8458 /* FIXME: add EMC latency mux */
8459 /* FIXME: add EMC PLLP branch */
8460 static struct clk_mux_sel mux_pllm_pllc_pllp_clkm[] = {
8461         { .input = &tegra_pll_m, .value = 0},
8462         { .input = &tegra_pll_c, .value = 1},
8463         { .input = &tegra_pll_p, .value = 2},
8464         { .input = &tegra_clk_m, .value = 3},
8465         { .input = &tegra_pll_m, .value = 4},  /* low jitter PLLM output */
8466         { .input = &tegra_pll_mb, .value = 5}, /* low jitter PLLMB output */
8467         { .input = &tegra_pll_mb, .value = 6},
8468         { .input = &tegra_pll_p, .value = 7}, /* low jitter PLLP output */
8469         { NULL, 0},
8470 };
8471
8472
8473 /* Display subsystem muxes */
8474 static struct clk_mux_sel mux_pllp_plld_plld2_clkm[] = {
8475         {.input = &tegra_pll_p_out_hsio, .value = 0},
8476         {.input = &tegra_pll_d_out0, .value = 2},
8477         {.input = &tegra_pll_d2, .value = 5},
8478         {.input = &tegra_clk_m, .value = 6},
8479         { NULL, 0},
8480 };
8481
8482 static struct clk_mux_sel mux_plld_out0[] = {
8483         { .input = &tegra_pll_d_out0,  .value = 0},
8484         { NULL, 0},
8485 };
8486
8487 static struct clk_mux_sel mux_plldp[] = {
8488         { .input = &tegra_pll_dp,  .value = 0},
8489         { NULL, 0},
8490 };
8491
8492 static struct clk tegra_clk_sor0_brick = {
8493         .name = "sor0_brick",
8494         .clk_id = TEGRA210_CLK_ID_SOR0_BRICK,
8495         .ops = &tegra_sor_brick_ops,
8496         .max_rate = 600000000,
8497         .inputs = mux_plldp,
8498         .flags = PERIPH_NO_ENB | PERIPH_NO_RESET,
8499 };
8500
8501 static struct clk_mux_sel mux_pllp_sor_sor0_brick[] = {
8502         { .input = &tegra_pll_p_out_sor,  .value = 0},
8503         { .input = &tegra_clk_sor0_brick,  .value = 1},
8504         { NULL, 0},
8505 };
8506
8507 static struct clk tegra_clk_sor1_src = {
8508         .name = "sor1_src",
8509         .clk_id = TEGRA210_CLK_ID_SOR1_SRC,
8510         .ops = &tegra_periph_clk_ops,
8511         .reg = 0x410,
8512         .max_rate = 600000000,
8513         .inputs = mux_pllp_plld_plld2_clkm,
8514         .flags = MUX | DIV_U71 | PERIPH_NO_ENB | PERIPH_NO_RESET,
8515 };
8516
8517 static struct clk_mux_sel mux_plldp_sor1_src[] = {
8518         { .input = &tegra_pll_dp,  .value = 0},
8519         { .input = &tegra_clk_sor1_src,  .value = 1},
8520         { NULL, 0},
8521 };
8522
8523 static struct clk tegra_clk_sor1_brick = {
8524         .name = "sor1_brick",
8525         .clk_id = TEGRA210_CLK_ID_SOR1_BRICK,
8526         .ops = &tegra_sor_brick_ops,
8527         .max_rate = 600000000,
8528         .inputs = mux_plldp_sor1_src,
8529         .flags = MUX | PERIPH_NO_ENB | PERIPH_NO_RESET,
8530 };
8531
8532 static struct clk_mux_sel mux_pllp_sor_sor1_brick_sor1_src[] = {
8533         { .input = &tegra_pll_p_out_sor, .value = 0},
8534         { .input = &tegra_clk_sor1_brick, .value = 1},
8535         { .input = &tegra_clk_sor1_src, .value = 2},
8536         { .input = &tegra_clk_sor1_brick, .value = 3},
8537         { NULL, 0},
8538 };
8539
8540 static struct clk_mux_sel mux_pllp_pllre_clkm[] = {
8541         { .input = &tegra_pll_p,    .value = 0 },
8542         { .input = &tegra_pll_re_out1, .value = 4 },
8543         { .input = &tegra_clk_m,    .value = 6 },
8544 };
8545
8546 /* Peripheral muxes */
8547 static struct clk_mux_sel mux_pllp_pllc_clkm[] = {
8548         {.input = &tegra_pll_p,     .value = 0},
8549         {.input = &tegra_pll_c,     .value = 2},
8550         {.input = &tegra_clk_m,     .value = 6},
8551         { NULL, 0},
8552 };
8553
8554 static struct clk_mux_sel mux_pllp_pllc_plla_clkm[] = {
8555         {.input = &tegra_pll_p,     .value = 0},
8556         {.input = &tegra_pll_c,     .value = 2},
8557         {.input = &tegra_pll_a_out0,     .value = 4},
8558         {.input = &tegra_clk_m,     .value = 6},
8559         { NULL, 0},
8560 };
8561
8562 static struct clk_mux_sel mux_pllp_pllc_pllc4_out0_pllc4_out1_clkm_pllc4_out2[] = {
8563         {.input = &tegra_pll_p,     .value = 0},
8564         {.input = &tegra_pll_c,     .value = 2},
8565         {.input = &tegra_pll_c4_out0,     .value = 3},
8566         {.input = &tegra_pll_c4_out1,     .value = 5},
8567         {.input = &tegra_clk_m,     .value = 6},
8568         {.input = &tegra_pll_c4_out2,     .value = 7},
8569         { NULL, 0},
8570 };
8571
8572 static struct clk_mux_sel mux_pllp_pllc_pllc_out1_pllc4_out2_pllc4_out1_clkm_pllc4_out0[] = {
8573         {.input = &tegra_pll_p,             .value = 0},
8574         {.input = &tegra_pll_c_out1,        .value = 1},
8575         {.input = &tegra_pll_c,             .value = 2},
8576         {.input = &tegra_pll_c4_out2,       .value = 4},
8577         {.input = &tegra_pll_c4_out1,       .value = 5},
8578         {.input = &tegra_clk_m,             .value = 6},
8579         {.input = &tegra_pll_c4_out0,       .value = 7},
8580         { NULL, 0},
8581 };
8582
8583 static struct clk_mux_sel mux_pllp_pllc4_out2_pllc4_out1_clkm_pllc4_out0[] = {
8584         {.input = &tegra_pll_p,             .value = 0},
8585         {.input = &tegra_pll_c4_out2,       .value = 3},
8586         {.input = &tegra_pll_c4_out1,       .value = 4},
8587         {.input = &tegra_clk_m,             .value = 6},
8588         {.input = &tegra_pll_c4_out0,       .value = 7},
8589         { NULL, 0},
8590 };
8591
8592 static struct clk_mux_sel mux_pllp_clk_m_pllc4_out2_out1_out0_lj[] = {
8593         {.input = &tegra_pll_p,             .value = 0},
8594         {.input = &tegra_pll_c4_out2,       .value = 1},  /* LJ input */
8595         {.input = &tegra_pll_c4_out0,       .value = 2},  /* LJ input */
8596         {.input = &tegra_pll_c4_out2,       .value = 3},
8597         {.input = &tegra_pll_c4_out1,       .value = 4},
8598         {.input = &tegra_pll_c4_out1,       .value = 5},  /* LJ input */
8599         {.input = &tegra_clk_m,             .value = 6},
8600         {.input = &tegra_pll_c4_out0,       .value = 7},
8601         { NULL, 0},
8602 };
8603
8604 static struct clk_mux_sel mux_pllp_pllc2_c_c3_clkm[] = {
8605         { .input = &tegra_pll_p,  .value = 0},
8606         { .input = &tegra_pll_c2, .value = 1},
8607         { .input = &tegra_pll_c,  .value = 2},
8608         { .input = &tegra_pll_c3, .value = 3},
8609         { .input = &tegra_clk_m,  .value = 6},
8610         { NULL, 0},
8611 };
8612
8613 static struct clk_mux_sel mux_pllp_pllc_clkm_1[] = {
8614         {.input = &tegra_pll_p,     .value = 0},
8615         {.input = &tegra_pll_c,     .value = 2},
8616         {.input = &tegra_clk_m,     .value = 5},
8617         { NULL, 0},
8618 };
8619
8620 static struct clk_mux_sel mux_pllp_clkm_2[] = {
8621         { .input = &tegra_pll_p, .value = 2},
8622         { .input = &tegra_clk_m, .value = 6},
8623         { NULL, 0},
8624 };
8625
8626 static struct clk_mux_sel mux_pllp_clkm_clk32_plle[] = {
8627         { .input = &tegra_pll_p, .value = 0},
8628         { .input = &tegra_clk_m, .value = 2},
8629         { .input = &tegra_clk_32k, .value = 4},
8630         { .input = &tegra_pll_e_gate, .value = 6},
8631         { NULL, 0},
8632 };
8633
8634 static struct clk_mux_sel mux_pllp_clkm[] = {
8635         { .input = &tegra_pll_p, .value = 0},
8636         { .input = &tegra_clk_m, .value = 6},
8637         { NULL, 0},
8638 };
8639
8640 static struct clk_mux_sel mux_pllp_pllp_out3_clkm_clk32k_plla[] = {
8641         { .input = &tegra_pll_p, .value = 0},
8642         { .input = &tegra_pll_p_out3, .value = 1},
8643         { .input = &tegra_clk_m, .value = 2},
8644         { .input = &tegra_clk_32k, .value = 3},
8645         { .input = &tegra_pll_a_out0, .value = 4},
8646         { NULL, 0},
8647 };
8648
8649 static struct clk_mux_sel mux_pllp_out3_clkm_pllp_pllc4[] = {
8650         { .input = &tegra_pll_p_out3, .value = 0},
8651         { .input = &tegra_clk_m, .value = 3},
8652         { .input = &tegra_pll_p, .value = 4},
8653         { .input = &tegra_pll_c4_out0, .value = 5},
8654         { .input = &tegra_pll_c4_out1, .value = 6},
8655         { .input = &tegra_pll_c4_out2, .value = 7},
8656         { NULL, 0},
8657 };
8658
8659 static struct clk_mux_sel mux_pllp_pllc_clk32_clkm[] = {
8660         {.input = &tegra_pll_p,     .value = 0},
8661         {.input = &tegra_pll_c,     .value = 2},
8662         {.input = &tegra_clk_32k,   .value = 4},
8663         {.input = &tegra_clk_m,     .value = 6},
8664         { NULL, 0},
8665 };
8666
8667 static struct clk_mux_sel mux_pllp_pllc_clkm_clk32[] = {
8668         {.input = &tegra_pll_p,     .value = 0},
8669         {.input = &tegra_pll_c,     .value = 2},
8670         {.input = &tegra_clk_m,     .value = 4},
8671         {.input = &tegra_clk_32k,   .value = 6},
8672         { NULL, 0},
8673 };
8674
8675 static struct clk_mux_sel mux_plla_clk32_pllp_clkm_plle[] = {
8676         { .input = &tegra_pll_a_out0, .value = 0},
8677         { .input = &tegra_clk_32k,    .value = 1},
8678         { .input = &tegra_pll_p,      .value = 2},
8679         { .input = &tegra_clk_m,      .value = 3},
8680         { .input = &tegra_pll_e_gate, .value = 4},
8681         { NULL, 0},
8682 };
8683
8684 static struct clk_mux_sel mux_clkm_pllp_pllre[] = {
8685         { .input = &tegra_clk_m,  .value = 0},
8686         { .input = &tegra_pll_p_out_xusb, .value = 1},
8687         { .input = &tegra_pll_re_out,  .value = 5},
8688         { NULL, 0},
8689 };
8690
8691 static struct clk_mux_sel mux_clkm_48M_pllp_480M[] = {
8692         { .input = &tegra_clk_m,      .value = 0},
8693         { .input = &tegra_pll_u_48M,  .value = 2},
8694         { .input = &tegra_pll_p_out_xusb, .value = 4},
8695         { .input = &tegra_pll_u_480M, .value = 6},
8696         { NULL, 0},
8697 };
8698
8699 static struct clk_mux_sel mux_clkm_pllre_clk32_480M[] = {
8700         { .input = &tegra_clk_m,      .value = 0},
8701         { .input = &tegra_pll_re_out, .value = 1},
8702         { .input = &tegra_clk_32k,    .value = 2},
8703         { .input = &tegra_pll_u_480M, .value = 3},
8704         { NULL, 0},
8705 };
8706
8707 static struct clk_mux_sel mux_pllp_out3_pllp_pllc_clkm[] = {
8708         { .input = &tegra_pll_p_out3, .value = 0},
8709         { .input = &tegra_pll_p,  .value = 1},
8710         { .input = &tegra_pll_c,  .value = 2},
8711         { .input = &tegra_clk_m,  .value = 6},
8712         { NULL, 0},
8713 };
8714
8715 /* Single clock source ("fake") muxes */
8716 static struct clk_mux_sel mux_clk_m[] = {
8717         { .input = &tegra_clk_m, .value = 0},
8718         { NULL, 0},
8719 };
8720
8721 static struct clk_mux_sel mux_pllp_out3[] = {
8722         { .input = &tegra_pll_p_out3, .value = 0},
8723         { NULL, 0},
8724 };
8725
8726 static struct clk_mux_sel mux_clk_32k[] = {
8727         { .input = &tegra_clk_32k, .value = 0},
8728         { NULL, 0},
8729 };
8730
8731 static struct clk_mux_sel mux_clk_usb2_hsic_trk[] = {
8732         { .input = &tegra_clk_usb2_hsic_trk, .value = 0},
8733         { NULL, 0},
8734 };
8735
8736 static struct clk tegra_clk_mc;
8737 static struct clk_mux_sel mux_clk_mc[] = {
8738         { .input = &tegra_clk_mc, .value = 0},
8739         { NULL, 0},
8740 };
8741
8742 static struct raw_notifier_head emc_rate_change_nh;
8743
8744 static struct clk tegra_clk_sata = {
8745         .name      = "sata",
8746         .clk_id = TEGRA210_CLK_ID_SATA,
8747         .lookup    = {
8748                 .dev_id = "tegra_sata",
8749         },
8750         .ops       = &tegra_periph_clk_ops,
8751         .reg       = SOURCE_SATA,
8752         .max_rate  = 102000000,
8753         .u.periph  = {
8754                 .clk_num = 124,
8755         },
8756         .inputs    = mux_pllp_pllc_clkm,
8757         .flags     = MUX | DIV_U71 | PERIPH_ON_APB,
8758 };
8759
8760 static struct clk tegra_sata_aux_clk = {
8761         .name      = "sata_aux",
8762         .clk_id = TEGRA210_CLK_ID_SATA_AUX,
8763         .lookup    = {
8764                 .dev_id = "tegra_sata_aux",
8765         },
8766         .parent    = &tegra_clk_sata,
8767         .ops       = &tegra_cml_clk_ops,
8768         .reg       = SOURCE_SATA,
8769         .max_rate  = 102000000,
8770         .u.periph  = {
8771                 .clk_num = 24,
8772         },
8773 };
8774
8775 static struct clk tegra_clk_emc = {
8776         .name = "emc",
8777         .clk_id = TEGRA210_CLK_ID_EMC,
8778         .lookup = {
8779                 .dev_id = "tegra_emc",
8780                 .con_id = "emc",
8781         },
8782         .ops = &tegra_emc_clk_ops,
8783         .reg = 0x19c,
8784         .max_rate = 1800000000,
8785         .min_rate = 12750000,
8786         .inputs = mux_pllm_pllc_pllp_clkm,
8787         .flags = MUX | DIV_U71 | PERIPH_EMC_ENB,
8788         .u.periph = {
8789                 .clk_num = 57,
8790         },
8791         .rate_change_nh = &emc_rate_change_nh,
8792 };
8793
8794 static struct clk tegra_clk_mc = {
8795         .name = "mc",
8796         .clk_id = TEGRA210_CLK_ID_MC,
8797         .ops = &tegra_mc_clk_ops,
8798         .max_rate = 1066000000,
8799         .parent = &tegra_clk_emc,
8800         .flags = PERIPH_NO_RESET,
8801         .u.periph = {
8802                 .clk_num = 32,
8803         },
8804 };
8805
8806 static struct raw_notifier_head host1x_rate_change_nh;
8807
8808 static struct clk tegra_clk_host1x = {
8809         .name      = "host1x",
8810         .clk_id = TEGRA210_CLK_ID_HOST1X,
8811         .lookup    = {
8812                 .dev_id = "host1x",
8813         },
8814         .ops       = &tegra_1xbus_clk_ops,
8815         .reg       = 0x180,
8816         .inputs    = mux_pllc4_out1_pllc_pllc4_out2_pllp_clkm_plla_pllc4_out0,
8817         .flags     = MUX | DIV_U71 | DIV_U71_INT,
8818         .max_rate  = 600000000,
8819         .min_rate  = 12000000,
8820         .u.periph = {
8821                 .clk_num   = 28,
8822                 .pll_low = &tegra_pll_p,
8823                 .pll_high = &tegra_pll_p,
8824         },
8825         .rate_change_nh = &host1x_rate_change_nh,
8826 };
8827
8828 static struct raw_notifier_head mselect_rate_change_nh;
8829
8830 static struct clk tegra_clk_mselect = {
8831         .name      = "mselect",
8832         .clk_id = TEGRA210_CLK_ID_MSELECT,
8833         .lookup    = {
8834                 .dev_id = "mselect",
8835         },
8836         .ops       = &tegra_1xbus_clk_ops,
8837         .reg       = 0x3b4,
8838         .inputs    = mux_pllp_clkm,
8839         .flags     = MUX | DIV_U71 | DIV_U71_INT,
8840         .max_rate  = 408000000,
8841         .min_rate  = 12000000,
8842         .u.periph = {
8843                 .clk_num   = 99,
8844                 .pll_low = &tegra_pll_p,
8845                 .pll_high = &tegra_pll_p,
8846                 .threshold = 408000000,
8847         },
8848         .rate_change_nh = &mselect_rate_change_nh,
8849 };
8850
8851 static struct raw_notifier_head ape_rate_change_nh;
8852
8853 static struct clk tegra_clk_ape = {
8854         .name      = "ape",
8855         .clk_id = TEGRA210_CLK_ID_APE,
8856         .ops       = &tegra_1xbus_clk_ops,
8857         .reg       = 0x6c0,
8858         .inputs    = mux_plla_pllc4_out0_pllc_pllc4_out1_pllp_clkm_pllc4_out2,
8859         .flags     = MUX | DIV_U71 | PERIPH_ON_APB,
8860         .shared_bus_flags = SHARED_BUS_RETENTION,
8861         .max_rate  = 408000000,
8862         .min_rate  = 12000000,
8863         .u.periph = {
8864                 .clk_num   = 198,
8865                 .pll_low = &tegra_pll_p,
8866                 .pll_high = &tegra_pll_p,
8867                 .threshold = 408000000,
8868         },
8869         .rate_change_nh = &ape_rate_change_nh,
8870 };
8871
8872 static struct raw_notifier_head c2bus_rate_change_nh;
8873 static struct raw_notifier_head c3bus_rate_change_nh;
8874
8875 static struct clk tegra_clk_c2bus = {
8876         .name      = "c2bus",
8877         .clk_id    = TEGRA210_CLK_ID_C2BUS,
8878         .parent    = &tegra_pll_c2,
8879         .ops       = &tegra_clk_cbus_ops,
8880         .max_rate  = 1000000000,
8881         .mul       = 1,
8882         .div       = 1,
8883         .flags     = PERIPH_ON_CBUS,
8884         .shared_bus_backup = {
8885                 .input = &tegra_pll_p,
8886         },
8887 #ifdef  CONFIG_TEGRA_CBUS_CAN_USE_SKIPPERS
8888         .shared_bus_flags = SHARED_BUS_USE_SKIPPERS,
8889 #endif
8890         .rate_change_nh = &c2bus_rate_change_nh,
8891 };
8892 static struct clk tegra_clk_c3bus = {
8893         .name      = "c3bus",
8894         .clk_id    = TEGRA210_CLK_ID_C3BUS,
8895         .parent    = &tegra_pll_c3,
8896         .ops       = &tegra_clk_cbus_ops,
8897         .max_rate  = 1000000000,
8898         .mul       = 1,
8899         .div       = 1,
8900         .flags     = PERIPH_ON_CBUS,
8901         .shared_bus_backup = {
8902                 .input = &tegra_pll_p,
8903         },
8904 #ifdef  CONFIG_TEGRA_CBUS_CAN_USE_SKIPPERS
8905         .shared_bus_flags = SHARED_BUS_USE_SKIPPERS,
8906 #endif
8907         .rate_change_nh = &c3bus_rate_change_nh,
8908 };
8909
8910 #ifdef CONFIG_TEGRA_MIGRATE_CBUS_USERS
8911 static DEFINE_MUTEX(cbus_mutex);
8912 #define CROSS_CBUS_MUTEX (&cbus_mutex)
8913 #else
8914 #define CROSS_CBUS_MUTEX NULL
8915 #endif
8916
8917
8918 static struct clk_mux_sel mux_clk_cbus[] = {
8919         { .input = &tegra_clk_c2bus, .value = 0},
8920         { .input = &tegra_clk_c3bus, .value = 1},
8921         { NULL, 0},
8922 };
8923
8924 #define DUAL_CBUS_CLK(_name, _dev, _con, _parent, _id, _div, _mode, _clk_id)\
8925         {                                               \
8926                 .name      = _name,                     \
8927                 .clk_id    = _clk_id,                   \
8928                 .lookup    = {                          \
8929                         .dev_id    = _dev,              \
8930                         .con_id    = _con,              \
8931                 },                                      \
8932                 .ops = &tegra_clk_shared_bus_user_ops,  \
8933                 .parent = _parent,                      \
8934                 .inputs = mux_clk_cbus,                 \
8935                 .flags = MUX,                           \
8936                 .u.shared_bus_user = {                  \
8937                         .client_id = _id,               \
8938                         .client_div = _div,             \
8939                         .mode = _mode,                  \
8940                 },                                      \
8941                 .cross_clk_mutex = CROSS_CBUS_MUTEX,    \
8942         }
8943
8944 static struct clk_ops tegra_clk_gpu_ops = {
8945         .enable         = &tegra21_periph_clk_enable,
8946         .disable        = &tegra21_periph_clk_disable,
8947         .reset          = &tegra21_periph_clk_reset,
8948 };
8949
8950 /*
8951  * This clock provides a common gate control for branches of different clocks
8952  * supplied to GPU: mselect, memory controller, pll_p_out5, jtag and la. The
8953  * mselect and mc clocks are always running; pll_p_out5 is explicitly controlled
8954  * by GPU driver; jtag and la clocks are set by debug interface configuration.
8955  * Therefore we don't need to propagate refcount to those clocks, and just set
8956  * GPU gate clock as OSC child.
8957  */
8958 static struct clk tegra_clk_gpu_gate = {
8959         .name      = "gpu_gate",
8960         .clk_id    = TEGRA210_CLK_ID_GPU_GATE,
8961         .parent    = &tegra_clk_osc,
8962         .ops       = &tegra_clk_gpu_ops,
8963         .u.periph  = {
8964                 .clk_num = 184,
8965         },
8966         .flags     = PERIPH_MANUAL_RESET,
8967         .max_rate  = 48000000,
8968         .min_rate  = 12000000,
8969 };
8970
8971 /*
8972  * This is a reference clock for GPU. The enable/disable routine control input
8973  * clock of the GPCPLL. The reference clock itself has a fixed frequency. The
8974  * actual GPU clock's frequency is controlled via gbus, which is a child of this
8975  * clock.
8976  */
8977 static struct clk tegra_clk_gpu_ref = {
8978         .name      = "gpu_ref",
8979         .clk_id    = TEGRA210_CLK_ID_GPU_REF,
8980         .ops       = &tegra_clk_gpu_ops,
8981         .parent    = &tegra_pll_ref,
8982         .u.periph  = {
8983                 .clk_num = 189,
8984         },
8985         .flags     = PERIPH_NO_RESET,
8986         .max_rate  = 48000000,
8987         .min_rate  = 12000000,
8988 };
8989
8990 #define RATE_GRANULARITY        100000 /* 0.1 MHz */
8991 #if defined(CONFIG_TEGRA_CLOCK_DEBUG_FUNC)
8992 static int gbus_round_pass_thru;
8993 void tegra_gbus_round_pass_thru_enable(bool enable)
8994 {
8995         if (enable)
8996                 gbus_round_pass_thru = 1;
8997         else
8998                 gbus_round_pass_thru = 0;
8999 }
9000 int tegra_gbus_round_pass_thru_get(void)
9001 {
9002         return gbus_round_pass_thru;
9003 }
9004 EXPORT_SYMBOL(tegra_gbus_round_pass_thru_enable);
9005 EXPORT_SYMBOL(tegra_gbus_round_pass_thru_get);
9006 #else
9007 #define gbus_round_pass_thru    0
9008 #endif
9009
9010 static void tegra21_clk_gbus_init(struct clk *c)
9011 {
9012         unsigned long rate;
9013         bool enabled;
9014
9015         pr_debug("%s on clock %s (export ops %s)\n", __func__,
9016                  c->name, c->u.export_clk.ops ? "ready" : "not ready");
9017
9018         if (!c->u.export_clk.ops || !c->u.export_clk.ops->init)
9019                 return;
9020
9021         c->u.export_clk.ops->init(c->u.export_clk.ops->data, &rate, &enabled);
9022         c->div = clk_get_rate(c->parent) / RATE_GRANULARITY;
9023         c->mul = rate / RATE_GRANULARITY;
9024         c->state = enabled ? ON : OFF;
9025 }
9026
9027 static int tegra21_clk_gbus_enable(struct clk *c)
9028 {
9029         pr_debug("%s on clock %s (export ops %s)\n", __func__,
9030                  c->name, c->u.export_clk.ops ? "ready" : "not ready");
9031
9032         if (!c->u.export_clk.ops || !c->u.export_clk.ops->enable)
9033                 return -ENOENT;
9034
9035         return c->u.export_clk.ops->enable(c->u.export_clk.ops->data);
9036 }
9037
9038 static void tegra21_clk_gbus_disable(struct clk *c)
9039 {
9040         pr_debug("%s on clock %s (export ops %s)\n", __func__,
9041                  c->name, c->u.export_clk.ops ? "ready" : "not ready");
9042
9043         if (!c->u.export_clk.ops || !c->u.export_clk.ops->disable)
9044                 return;
9045
9046         c->u.export_clk.ops->disable(c->u.export_clk.ops->data);
9047 }
9048
9049 static int tegra21_clk_gbus_set_rate(struct clk *c, unsigned long rate)
9050 {
9051         int ret;
9052
9053         pr_debug("%s %lu on clock %s (export ops %s)\n", __func__,
9054                  rate, c->name, c->u.export_clk.ops ? "ready" : "not ready");
9055
9056         if (!c->u.export_clk.ops || !c->u.export_clk.ops->set_rate)
9057                 return -ENOENT;
9058
9059         ret = c->u.export_clk.ops->set_rate(c->u.export_clk.ops->data, &rate);
9060         if (!ret)
9061                 c->mul = rate / RATE_GRANULARITY;
9062         return ret;
9063 }
9064
9065 static long tegra21_clk_gbus_round_updown(struct clk *c, unsigned long rate,
9066                                           bool up)
9067 {
9068         return gbus_round_pass_thru ? rate :
9069                 tegra21_clk_cbus_round_updown(c, rate, up);
9070 }
9071
9072 static long tegra21_clk_gbus_round_rate(struct clk *c, unsigned long rate)
9073 {
9074         return tegra21_clk_gbus_round_updown(c, rate, true);
9075 }
9076
9077 static struct clk_ops tegra_clk_gbus_ops = {
9078         .init           = tegra21_clk_gbus_init,
9079         .enable         = tegra21_clk_gbus_enable,
9080         .disable        = tegra21_clk_gbus_disable,
9081         .set_rate       = tegra21_clk_gbus_set_rate,
9082         .round_rate     = tegra21_clk_gbus_round_rate,
9083         .round_rate_updown = tegra21_clk_gbus_round_updown,
9084         .shared_bus_update = tegra21_clk_shared_connector_update, /* re-use */
9085 };
9086
9087 static struct raw_notifier_head gbus_rate_change_nh;
9088 static wait_queue_head_t gbus_poll_rate;
9089 static struct bus_stats gpu_histogram;
9090
9091 static struct clk tegra_clk_gbus = {
9092         .name      = "gbus",
9093         .clk_id    = TEGRA210_CLK_ID_GBUS,
9094         .ops       = &tegra_clk_gbus_ops,
9095         .parent    = &tegra_clk_gpu_ref,
9096         .max_rate  = 1300000000,
9097         .shared_bus_flags = SHARED_BUS_RETENTION,
9098         .stats = {
9099                 .histogram = &gpu_histogram,
9100         },
9101         .rate_change_nh = &gbus_rate_change_nh,
9102         .debug_poll_qh  = &gbus_poll_rate,
9103 };
9104
9105 static void tegra21_camera_mclk_init(struct clk *c)
9106 {
9107         c->state = OFF;
9108         c->set = true;
9109
9110         if (!strcmp(c->name, "mclk")) {
9111                 c->parent = tegra_get_clock_by_name("vi_sensor");
9112                 c->max_rate = c->parent->max_rate;
9113         } else if (!strcmp(c->name, "mclk2")) {
9114                 c->parent = tegra_get_clock_by_name("vi_sensor2");
9115                 c->max_rate = c->parent->max_rate;
9116         } else if (!strcmp(c->name, "mclk3")) {
9117                 c->parent = tegra_get_clock_by_name("extern3");
9118                 c->max_rate = c->parent->max_rate;
9119         } else if (!strcmp(c->name, "cam-mipi-cal")) {
9120                 c->parent = tegra_get_clock_by_name("uart_mipi_cal");
9121                 c->max_rate = c->parent->max_rate;
9122         }
9123 }
9124
9125 static int tegra21_camera_mclk_set_rate(struct clk *c, unsigned long rate)
9126 {
9127         return clk_set_rate(c->parent, rate);
9128 }
9129
9130 static struct clk_ops tegra_camera_mclk_ops = {
9131         .init     = tegra21_camera_mclk_init,
9132         .enable   = tegra21_periph_clk_enable,
9133         .disable  = tegra21_periph_clk_disable,
9134         .set_rate = tegra21_camera_mclk_set_rate,
9135 };
9136
9137 static struct clk tegra_camera_mclk = {
9138         .name = "mclk",
9139         .clk_id = TEGRA210_CLK_ID_MCLK,
9140         .ops = &tegra_camera_mclk_ops,
9141         .u.periph = {
9142                 .clk_num = 92, /* csus */
9143         },
9144         .flags = PERIPH_NO_RESET,
9145 };
9146
9147 static struct clk tegra_camera_mclk2 = {
9148         .name = "mclk2",
9149         .clk_id = TEGRA210_CLK_ID_MCLK2,
9150         .ops = &tegra_camera_mclk_ops,
9151         .u.periph = {
9152                 .clk_num = 171, /* vim2_clk */
9153         },
9154         .flags = PERIPH_NO_RESET,
9155 };
9156
9157 static struct clk tegra_camera_mipical = {
9158         .name = "cam-mipi-cal",
9159         .clk_id = TEGRA210_CLK_ID_CAM_MIPI_CAL,
9160         .ops = &tegra_camera_mclk_ops,
9161         .u.periph = {
9162                 .clk_num = 56, /* mipi_cal */
9163         },
9164         .flags = PERIPH_NO_RESET,
9165 };
9166
9167 static struct clk_ops tegra_camera_mclk3_ops = {
9168         .init           = &tegra21_camera_mclk_init,
9169         .enable         = &tegra21_clk_out_enable,
9170         .disable        = &tegra21_clk_out_disable,
9171         .set_rate       = &tegra21_camera_mclk_set_rate,
9172 };
9173
9174 static struct clk tegra_camera_mclk3 = {
9175         .name = "mclk3",
9176         .clk_id = TEGRA210_CLK_ID_MCLK3,
9177         .ops = &tegra_camera_mclk3_ops,
9178         .reg = 0x1a8,
9179         .flags = MUX_CLK_OUT,
9180         .u.periph = {
9181                 .clk_num   = (3 - 1) * 8 + 2,
9182         },
9183 };
9184
9185 static struct clk tegra_clk_isp = {
9186         .name = "isp",
9187         .clk_id = TEGRA210_CLK_ID_ISP,
9188         .ops = &tegra_periph_clk_ops,
9189         .reg = 0x144,
9190         .max_rate = 1000000000,
9191         .inputs = mux_pllc_pllp_plla1_pllc2_c3_clkm_pllc4,
9192         .flags = MUX | DIV_U71 | DIV_U71_INT | PERIPH_NO_ENB | PERIPH_NO_RESET,
9193 };
9194
9195 static struct clk_mux_sel mux_isp[] = {
9196         { .input = &tegra_clk_isp, .value = 0},
9197         { NULL, 0},
9198 };
9199
9200 static struct clk tegra_clk_hda2codec_2x = {
9201         .name = "hda2codec_2x",
9202         .clk_id = TEGRA210_CLK_ID_HDA2CODEC_2X,
9203         .lookup = {
9204                 .dev_id = "tegra30-hda",
9205                 .con_id = "hda2codec",
9206         },
9207         .ops = &tegra_periph_clk_ops,
9208         .u.periph = {
9209                 .clk_num = 111,
9210         },
9211         .reg = 0x3e4,
9212         .max_rate = 48000000,
9213         .inputs = mux_pllp_pllc_plla_clkm,
9214         .flags = MUX | DIV_U71 | PERIPH_ON_APB,
9215 };
9216
9217 static struct clk_mux_sel mux_hda2codec_2x[] = {
9218         { .input = &tegra_clk_hda2codec_2x, .value = 0},
9219         { NULL, 0},
9220 };
9221
9222 static struct raw_notifier_head cbus_rate_change_nh;
9223
9224 static struct clk tegra_clk_cbus = {
9225         .name      = "cbus",
9226         .clk_id    = TEGRA210_CLK_ID_CBUS,
9227         .parent    = &tegra_pll_c,
9228         .ops       = &tegra_clk_cbus_ops,
9229         .max_rate  = 1000000000,
9230         .mul       = 1,
9231         .div       = 1,
9232         .flags     = PERIPH_ON_CBUS,
9233         .shared_bus_backup = {
9234                 .input = &tegra_pll_p,
9235         },
9236         .rate_change_nh = &cbus_rate_change_nh,
9237 };
9238
9239 #define PERIPH_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags, \
9240                         _clk_id)                                             \
9241         {                                               \
9242                 .name      = _name,                     \
9243                 .clk_id    = _clk_id,                   \
9244                 .lookup    = {                          \
9245                         .dev_id    = _dev,              \
9246                         .con_id    = _con,              \
9247                 },                                      \
9248                 .ops       = &tegra_periph_clk_ops,     \
9249                 .reg       = _reg,                      \
9250                 .inputs    = _inputs,                   \
9251                 .flags     = _flags,                    \
9252                 .max_rate  = _max,                      \
9253                 .u.periph = {                           \
9254                         .clk_num   = _clk_num,          \
9255                 },                                      \
9256         }
9257
9258 #define PERIPH_CLK_EX(_name, _dev, _con, _clk_num, _reg, _max, _inputs, \
9259                         _flags, _clk_id, _ops)                          \
9260         {                                               \
9261                 .name      = _name,                     \
9262                 .clk_id    = _clk_id,                   \
9263                 .lookup    = {                          \
9264                         .dev_id    = _dev,              \
9265                         .con_id    = _con,              \
9266                 },                                      \
9267                 .ops       = _ops,                      \
9268                 .reg       = _reg,                      \
9269                 .inputs    = _inputs,                   \
9270                 .flags     = _flags,                    \
9271                 .max_rate  = _max,                      \
9272                 .u.periph = {                           \
9273                         .clk_num   = _clk_num,          \
9274                 },                                      \
9275         }
9276
9277 #define SUPER_SKIP_CLK(_name, _dev, _con, _reg, _parent, _flags) \
9278         {                                               \
9279                 .name      = _name,                     \
9280                 .lookup    = {                          \
9281                         .dev_id    = _dev,              \
9282                         .con_id    = _con,              \
9283                 },                                      \
9284                 .ops       = &tegra_clk_super_skip_ops, \
9285                 .reg       = _reg,                      \
9286                 .parent    = _parent,                   \
9287                 .flags     = _flags,                    \
9288         }
9289
9290 #define PERIPH_CLK_SKIP(_name, _dev, _con, _clk_num, _reg, _reg_skip, _max, \
9291                         _inputs, _flags, _clk_id) \
9292         PERIPH_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags, \
9293                         _clk_id), \
9294         SUPER_SKIP_CLK(_name "_skip", _dev, "skip", _reg_skip, NULL, 0)
9295
9296 #define D_AUDIO_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags, _clk_id) \
9297         {                                               \
9298                 .name      = _name,                     \
9299                 .clk_id    = _clk_id,                   \
9300                 .lookup    = {                          \
9301                         .dev_id    = _dev,              \
9302                         .con_id    = _con,              \
9303                 },                                      \
9304                 .ops       = &tegra_periph_clk_ops,     \
9305                 .reg       = _reg,                      \
9306                 .inputs    = _inputs,                   \
9307                 .flags     = _flags,                    \
9308                 .max_rate  = _max,                      \
9309                 .u.periph = {                           \
9310                         .clk_num   = _clk_num,          \
9311                         .src_mask  = 0xE01F << 16,      \
9312                         .src_shift = 16,                \
9313                 },                                      \
9314         }
9315
9316 #define SHARED_CLK(_name, _dev, _con, _parent, _id, _div, _mode, _clk_id)\
9317         {                                               \
9318                 .name      = _name,                     \
9319                 .clk_id    = _clk_id,                   \
9320                 .lookup    = {                          \
9321                         .dev_id    = _dev,              \
9322                         .con_id    = _con,              \
9323                 },                                      \
9324                 .ops = &tegra_clk_shared_bus_user_ops,  \
9325                 .parent = _parent,                      \
9326                 .u.shared_bus_user = {                  \
9327                         .client_id = _id,               \
9328                         .client_div = _div,             \
9329                         .mode = _mode,                  \
9330                 },                                      \
9331         }
9332 #define SHARED_LIMIT(_name, _dev, _con, _parent, _id, _div, _mode, _clk_id)\
9333         {                                               \
9334                 .name      = _name,                     \
9335                 .clk_id    = _clk_id,                   \
9336                 .lookup    = {                          \
9337                         .dev_id    = _dev,              \
9338                         .con_id    = _con,              \
9339                 },                                      \
9340                 .ops = &tegra_clk_shared_bus_user_ops,  \
9341                 .parent = _parent,                      \
9342                 .flags     = BUS_RATE_LIMIT,            \
9343                 .u.shared_bus_user = {                  \
9344                         .client_id = _id,               \
9345                         .client_div = _div,             \
9346                         .mode = _mode,                  \
9347                 },                                      \
9348         }
9349 #define SHARED_CONNECT(_name, _dev, _con, _parent, _id, _div, _mode, _clk_id)\
9350         {                                               \
9351                 .name      = _name,                     \
9352                 .clk_id    = _clk_id,                   \
9353                 .lookup    = {                          \
9354                         .dev_id    = _dev,              \
9355                         .con_id    = _con,              \
9356                 },                                      \
9357                 .ops = &tegra_clk_shared_connector_ops, \
9358                 .parent = _parent,                      \
9359                 .u.shared_bus_user = {                  \
9360                         .client_id = _id,               \
9361                         .client_div = _div,             \
9362                         .mode = _mode,                  \
9363                 },                                      \
9364         }
9365 #define SHARED_EMC_CLK(_name, _dev, _con, _parent, _id, _div, _mode, _flag, _clk_id)\
9366         {                                               \
9367                 .name      = _name,                     \
9368                 .clk_id    = _clk_id,                   \
9369                 .lookup    = {                          \
9370                         .dev_id    = _dev,              \
9371                         .con_id    = _con,              \
9372                 },                                      \
9373                 .ops = &tegra_clk_shared_bus_user_ops,  \
9374                 .parent = _parent,                      \
9375                 .u.shared_bus_user = {                  \
9376                         .client_id = _id,               \
9377                         .client_div = _div,             \
9378                         .mode = _mode,                  \
9379                         .usage_flag = _flag,            \
9380                 },                                      \
9381         }
9382
9383 static DEFINE_MUTEX(sbus_cross_mutex);
9384 #define SHARED_SCLK(_name, _dev, _con, _parent, _id, _div, _mode, _clk_id)\
9385         {                                               \
9386                 .name = _name,                          \
9387                 .clk_id    = _clk_id,                   \
9388                 .lookup = {                             \
9389                         .dev_id = _dev,                 \
9390                         .con_id = _con,                 \
9391                 },                                      \
9392                 .ops = &tegra_clk_shared_bus_user_ops,  \
9393                 .parent = _parent,                      \
9394                 .u.shared_bus_user = {                  \
9395                         .client_id = _id,               \
9396                         .client_div = _div,             \
9397                         .mode = _mode,                  \
9398                 },                                      \
9399                 .cross_clk_mutex = &sbus_cross_mutex,   \
9400 }
9401
9402 static struct clk tegra_list_clks[] = {
9403         PERIPH_CLK("ahbdma",    "ahbdma",               NULL,   33,     0,      38400000,  mux_clk_m,                   0, TEGRA210_CLK_ID_AHBDMA),
9404         PERIPH_CLK("apbdma",    "tegra-apbdma",         NULL,   34,     0,      38400000,  mux_clk_m,                   0, TEGRA210_CLK_ID_APBDMA),
9405         PERIPH_CLK("rtc",       "rtc-tegra",            NULL,   4,      0,      32768,     mux_clk_32k,                 PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_RTC),
9406         PERIPH_CLK("timer",     "timer",                NULL,   5,      0,      38400000,  mux_clk_m,                   0, TEGRA210_CLK_ID_TIMER),
9407         PERIPH_CLK("spare1",    "spare1",               NULL,   192,    0,      38400000,  mux_clk_m,                   0, TEGRA210_CLK_ID_SPARE1),
9408         PERIPH_CLK("axiap",     "axiap",                NULL,   196,    0,      38400000,  mux_clk_m,                   0, TEGRA210_CLK_ID_AXIAP),
9409         PERIPH_CLK("iqc1",      "tegra210-iqc.0",       NULL,   221,    0,      38400000,  mux_clk_m,                   PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_IQC1),
9410         PERIPH_CLK("iqc2",      "tegra210-iqc.1",       NULL,   220,    0,      38400000,  mux_clk_m,                   PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_IQC2),
9411         PERIPH_CLK("kfuse",     "kfuse-tegra",          NULL,   40,     0,      38400000,  mux_clk_m,                   PERIPH_ON_APB, TEGRA210_CLK_ID_KFUSE),
9412         PERIPH_CLK("fuse",      "fuse-tegra",         "fuse",   39,     0,      38400000,  mux_clk_m,                   PERIPH_ON_APB, TEGRA210_CLK_ID_FUSE),
9413         PERIPH_CLK("fuse_burn", "fuse-tegra",    "fuse_burn",   39,     0,      38400000,  mux_clk_m,                   PERIPH_ON_APB, TEGRA210_CLK_ID_FUSE_BURN),
9414
9415         PERIPH_CLK("i2s0",      "tegra210-i2s.0",       NULL,   30,     0x1d8,   24576000, mux_pllaout0_audio0_pllp_clkm,       MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_I2S0),
9416         PERIPH_CLK("i2s1",      "tegra210-i2s.1",       NULL,   11,     0x100,   24576000, mux_pllaout0_audio1_pllp_clkm,       MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_I2S1),
9417         PERIPH_CLK("i2s2",      "tegra210-i2s.2",       NULL,   18,     0x104,   24576000, mux_pllaout0_audio2_pllp_clkm,       MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_I2S2),
9418         PERIPH_CLK("i2s3",      "tegra210-i2s.3",       NULL,   101,    0x3bc,   24576000, mux_pllaout0_audio3_pllp_clkm,       MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_I2S3),
9419         PERIPH_CLK("i2s4",      "tegra210-i2s.4",       NULL,   102,    0x3c0,   24576000, mux_pllaout0_audio4_pllp_clkm,       MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_I2S4),
9420         PERIPH_CLK("spdif_out", "tegra210-spdif", "spdif_out",  10,     0x108,   24727300, mux_pllaout0_audio_2x_pllp_clkm,     MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_SPDIF_OUT),
9421         PERIPH_CLK("spdif_in",  "tegra210-spdif",  "spdif_in",  10,     0x10c,  408000000, mux_pllp_pllc_clkm_1,                MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_SPDIF_IN),
9422         PERIPH_CLK("dmic1",     "tegra210-dmic.0",      NULL,   161,    0x64c,   12288000, mux_pllaout0_audio0_dmic_pllp_clkm,  MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_DMIC1),
9423         PERIPH_CLK("dmic2",     "tegra210-dmic.1",      NULL,   162,    0x650,   12288000, mux_pllaout0_audio1_dmic_pllp_clkm,  MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_DMIC2),
9424         PERIPH_CLK("dmic3",     "tegra210-dmic.2",      NULL,   197,    0x6bc,   12288000, mux_pllaout0_audio2_dmic_pllp_clkm,  MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_DMIC3),
9425         PERIPH_CLK("apb2ape",   NULL,               "apb2ape",  107,    0,      38400000,  mux_clk_m,                   PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_APB2APE),
9426         PERIPH_CLK("maud",      "maud",                 NULL,   202,    0x6d4,  102000000, mux_pllp_pllp_out3_clkm_clk32k_plla, MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_MAUD),
9427         PERIPH_CLK("pwm",       "7000a000.pwm",         NULL,   17,     0x110,   48000000, mux_pllp_pllc_clk32_clkm,            MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_PWM),
9428         D_AUDIO_CLK("d_audio",  "tegra210-axbar",       "ahub", 106,    0x3d0,   98304000, mux_d_audio_clk,                     MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_AHUB),
9429         PERIPH_CLK("hda",       "tegra30-hda",          "hda",  125,    0x428,  102000000, mux_pllp_pllc_clkm,                  MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_HDA),
9430         PERIPH_CLK("hda2hdmi",  "tegra30-hda",     "hda2hdmi",  128,    0,       48000000,  mux_hda2codec_2x,                           PERIPH_ON_APB, TEGRA210_CLK_ID_HDA2HDMI),
9431
9432         PERIPH_CLK_EX("qspi",   "qspi",                 NULL,   211,    0x6c4, 133000000, mux_pllp_pllc_pllc_out1_pllc4_out2_pllc4_out1_clkm_pllc4_out0, MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_QSPI, &tegra_qspi_clk_ops),
9433         PERIPH_CLK("sbc1",      "7000d400.spi",         NULL,   41,     0x134,  65000000, mux_pllp_pllc_clkm,   MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SBC1),
9434         PERIPH_CLK("sbc2",      "7000d600.spi",         NULL,   44,     0x118,  65000000, mux_pllp_pllc_clkm,   MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SBC2),
9435         PERIPH_CLK("sbc3",      "7000d800.spi",         NULL,   46,     0x11c,  65000000, mux_pllp_pllc_clkm,   MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SBC3),
9436         PERIPH_CLK("sbc4",      "7000da00.spi",         NULL,   68,     0x1b4,  65000000, mux_pllp_pllc_clkm,   MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SBC4),
9437         PERIPH_CLK("sata_oob",  "tegra_sata_oob",       NULL,   123,    0x420,  204000000, mux_pllp_pllc_clkm,  MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_SATA_OOB),
9438         PERIPH_CLK("sata_cold", "tegra_sata_cold",      NULL,   129,    0,      38400000,  mux_clk_m,           PERIPH_ON_APB, TEGRA210_CLK_ID_SATA_COLD),
9439         PERIPH_CLK("sdmmc1",    "sdhci-tegra.0",        NULL,   14,     0x150,  204000000, mux_pllp_pllc4_out2_pllc4_out1_clkm_pllc4_out0,      MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_SDMMC1),
9440         PERIPH_CLK("sdmmc3",    "sdhci-tegra.2",        NULL,   69,     0x1bc,  204000000, mux_pllp_pllc4_out2_pllc4_out1_clkm_pllc4_out0,      MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_SDMMC3),
9441         PERIPH_CLK("sdmmc1_ddr", "sdhci-tegra.0",       "ddr",  14,     0x150,   96000000, mux_pllp_pllc4_out2_pllc4_out1_clkm_pllc4_out0,      MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SDMMC1_DDR),
9442         PERIPH_CLK("sdmmc3_ddr", "sdhci-tegra.2",       "ddr",  69,     0x1bc,   96000000, mux_pllp_pllc4_out2_pllc4_out1_clkm_pllc4_out0,      MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SDMMC3_DDR),
9443         PERIPH_CLK_EX("sdmmc2", "sdhci-tegra.1",        NULL,   9,      0x154,  266000000, mux_pllp_clk_m_pllc4_out2_out1_out0_lj,      MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_SDMMC2, &tegra_sdmmc24_clk_ops),
9444         PERIPH_CLK_EX("sdmmc4", "sdhci-tegra.3",        NULL,   15,     0x164,  266000000, mux_pllp_clk_m_pllc4_out2_out1_out0_lj,      MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SDMMC4, &tegra_sdmmc24_clk_ops),
9445         PERIPH_CLK_EX("sdmmc2_ddr", "sdhci-tegra.1",    "ddr",  9,      0x154,  102000000, mux_pllp_clk_m_pllc4_out2_out1_out0_lj,      MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SDMMC2_DDR, &tegra_sdmmc24_clk_ops),
9446         PERIPH_CLK_EX("sdmmc4_ddr", "sdhci-tegra.3",    "ddr",  15,     0x164,  102000000, mux_pllp_clk_m_pllc4_out2_out1_out0_lj,      MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SDMMC4_DDR, &tegra_sdmmc24_clk_ops),
9447         PERIPH_CLK("sdmmc_legacy", "sdmmc_legacy",      NULL,   193,    0x694,  208000000, mux_pllp_out3_clkm_pllp_pllc4, MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_SDMMC_LEGACY),
9448
9449         PERIPH_CLK("vcp",       "nvavp",                "vcp",  29,     0,      250000000, mux_clk_m,                   0, TEGRA210_CLK_ID_VCP),
9450         PERIPH_CLK("bsev",      "tegra-aes",            "bsev", 63,     0,      250000000, mux_clk_m,                   0, TEGRA210_CLK_ID_BSEV),
9451         PERIPH_CLK("cec",       "tegra_cec",            NULL,   136,    0,      250000000, mux_clk_m,                   PERIPH_ON_APB, TEGRA210_CLK_ID_CEC),
9452         PERIPH_CLK("csite",     "csite",                NULL,   73,     0x1d4,  624000000, mux_pllp_pllre_clkm,         MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_CSITE),
9453         PERIPH_CLK("la",        "la",                   NULL,   76,     0x1f8,  408000000,  mux_pllp_pllc_clkm,         MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_LA),
9454
9455         PERIPH_CLK("owr",       "tegra_w1",             NULL,   71,     0x1cc,  38400000,  mux_pllp_pllc_clkm,          MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_OWR),
9456         PERIPH_CLK("i2c1",      "7000c000.i2c",   "div-clk",    12,     0x124,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB, TEGRA210_CLK_ID_I2C1),
9457         PERIPH_CLK("i2c2",      "7000c400.i2c",   "div-clk",    54,     0x198,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB, TEGRA210_CLK_ID_I2C2),
9458         PERIPH_CLK("i2c3",      "7000c500.i2c",   "div-clk",    67,     0x1b8,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB, TEGRA210_CLK_ID_I2C3),
9459         PERIPH_CLK("i2c4",      "7000c700.i2c",   "div-clk",    103,    0x3c4,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB, TEGRA210_CLK_ID_I2C4),
9460         PERIPH_CLK("i2c5",      "7000d000.i2c",   "div-clk",    47,     0x128,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB, TEGRA210_CLK_ID_I2C5),
9461         PERIPH_CLK("i2c6",      "7000d100.i2c",   "div-clk",    166,    0x65c,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB, TEGRA210_CLK_ID_I2C6),
9462         PERIPH_CLK("vii2c",     "546c0000.i2c",   "vii2c",      208,    0x6c8, 136000000, mux_pllp_pllc_clkm,   MUX | DIV_U16 | PERIPH_ON_APB, TEGRA210_CLK_ID_VII2C),
9463         PERIPH_CLK("i2cslow",   "546c0000.i2c",   "i2cslow",    81,     0x3fc,  50000000,  mux_pllp_pllc_clk32_clkm,    MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_I2CSLOW),
9464         PERIPH_CLK("mipibif",   "mipibif",              NULL,   173,    0x660,  408000000,  mux_pllp_clkm,      MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_MIPIIF),
9465         PERIPH_CLK("mipi-cal",  "mipi-cal",             NULL,   56,     0,      102000000,  mux_clk_m, PERIPH_ON_APB, TEGRA210_CLK_ID_MIPI_CAL),
9466         PERIPH_CLK("uarta",     "70006000.serial",      NULL,   6,      0x178, 1190400000, mux_pllp_pllc_pllc4_out0_pllc4_out1_clkm_pllc4_out2, MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB, TEGRA210_CLK_ID_UARTA),
9467         PERIPH_CLK("uartb",     "70006040.serial",      NULL,   7,      0x17c, 1190400000, mux_pllp_pllc_pllc4_out0_pllc4_out1_clkm_pllc4_out2, MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB, TEGRA210_CLK_ID_UARTB),
9468         PERIPH_CLK("uartc",     "70006200.serial",      NULL,   55,     0x1a0, 1190400000, mux_pllp_pllc_pllc4_out0_pllc4_out1_clkm_pllc4_out2, MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB, TEGRA210_CLK_ID_UARTC),
9469         PERIPH_CLK("uartd",     "70006300.serial",      NULL,   65,     0x1c0, 1190400000, mux_pllp_pllc_pllc4_out0_pllc4_out1_clkm_pllc4_out2, MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB, TEGRA210_CLK_ID_UARTD),
9470         PERIPH_CLK("uartape",   "uartape",              NULL,   212,    0x710,  204000000, mux_pllp_pllc_clkm, MUX | DIV_U151 | DIV_U151_UART | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_UARTAPE),
9471
9472         PERIPH_CLK("vi_sensor",  NULL,          "vi_sensor",    164,    0x1a8,  408000000, mux_pllc_pllp_plla,          MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_VI_SENSOR),
9473         PERIPH_CLK("vi_sensor2", NULL,          "vi_sensor2",   165,    0x658,  408000000, mux_pllc_pllp_plla,          MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_VI_SENSOR2),
9474         PERIPH_CLK_EX("dtv",    "dtv",                  NULL,   79,     0x1dc,   38400000, mux_clk_m,                   PERIPH_ON_APB,  TEGRA210_CLK_ID_DTV, &tegra_dtv_clk_ops),
9475         PERIPH_CLK("disp1",     "tegradc.0",            NULL,   27,     0x138, 1500000000, mux_pllp_plld_plld2_clkm,    MUX, TEGRA210_CLK_ID_DISP1),
9476         PERIPH_CLK("disp2",     "tegradc.1",            NULL,   26,     0x13c, 1500000000, mux_pllp_plld_plld2_clkm,    MUX, TEGRA210_CLK_ID_DISP2),
9477         PERIPH_CLK_EX("sor0",   NULL,                   "sor0", 182,    0x414,  600000000, mux_pllp_sor_sor0_brick,             MUX,    TEGRA210_CLK_ID_SOR0, &tegra_sor0_clk_ops),
9478         PERIPH_CLK_EX("sor1",   NULL,                   "sor1", 183,    0x410,  600000000, mux_pllp_sor_sor1_brick_sor1_src,    MUX,    TEGRA210_CLK_ID_SOR1, &tegra_sor1_clk_ops),
9479         PERIPH_CLK_EX("dpaux",  "dpaux",                NULL,   181,    0,       24000000, mux_pllp,                    0, TEGRA210_CLK_ID_DPAUX, &tegra_dpaux_clk_ops),
9480         PERIPH_CLK_EX("dpaux1", "dpaux1",               NULL,   207,    0,       24000000, mux_pllp,                    0, TEGRA210_CLK_ID_DPAUX1, &tegra_dpaux_clk_ops),
9481
9482         PERIPH_CLK("usbd",      "tegra-udc.0",          NULL,   22,     0,      480000000, mux_clk_m,                   0, TEGRA210_CLK_ID_USBD),
9483         PERIPH_CLK("usb2",      "tegra-ehci.1",         NULL,   58,     0,      480000000, mux_clk_m,                   0, TEGRA210_CLK_ID_USB2),
9484         PERIPH_CLK("hsic_trk",  NULL,             "hsic_trk",   209,    0,      38400000, mux_clk_usb2_hsic_trk,        PERIPH_NO_RESET, TEGRA210_CLK_ID_HSIC_TRK),
9485         PERIPH_CLK("usb2_trk",  NULL,             "usb2_trk",   210,    0,      38400000, mux_clk_usb2_hsic_trk,        PERIPH_NO_RESET, TEGRA210_CLK_ID_USB2_TRK),
9486
9487         PERIPH_CLK("dsia",      "tegradc.0",          "dsia",   48,     0xd0,   750000000, mux_plld_out0,               PLLD,   TEGRA210_CLK_ID_DSIA),
9488         PERIPH_CLK("dsib",      "tegradc.1",          "dsib",   82,     0x4b8,  750000000, mux_plld_out0,               PLLD,   TEGRA210_CLK_ID_DSIB),
9489         PERIPH_CLK("dsi1-fixed", "tegradc.0",    "dsi-fixed",   0,      0,      108000000, mux_pllp_out3,               PERIPH_NO_ENB | PERIPH_NO_RESET, TEGRA210_CLK_ID_DSI1_FIXED),
9490         PERIPH_CLK("dsi2-fixed", "tegradc.1",    "dsi-fixed",   0,      0,      108000000, mux_pllp_out3,               PERIPH_NO_ENB | PERIPH_NO_RESET, TEGRA210_CLK_ID_DSI2_FIXED),
9491         PERIPH_CLK("csi",       "vi",                  "csi",   52,     0,      750000000, mux_plld_out0,               PLLD, TEGRA210_CLK_ID_CSI),
9492         PERIPH_CLK("csus",      "vi",                 "csus",   92,     0,      150000000, mux_clk_m,                   PERIPH_NO_RESET, TEGRA210_CLK_ID_CSUS),
9493         PERIPH_CLK("vim2_clk",  "vi",             "vim2_clk",   171,    0,      150000000, mux_clk_m,                   PERIPH_NO_RESET, TEGRA210_CLK_ID_VIM2_CLK),
9494         PERIPH_CLK("cilab",     "vi",                "cilab",   144,    0x614,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_CILAB),
9495         PERIPH_CLK("cilcd",     "vi",                "cilcd",   145,    0x618,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_CILCD),
9496         PERIPH_CLK("cile",      "vi",                 "cile",   146,    0x61c,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_CILE),
9497         PERIPH_CLK("dsialp",    "tegradc.0",        "dsialp",   147,    0x620,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_DSIALP),
9498         PERIPH_CLK("dsiblp",    "tegradc.1",        "dsiblp",   148,    0x624,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_DSIBLP),
9499
9500         PERIPH_CLK("entropy",   "entropy",              NULL,   149,    0x628,  102000000, mux_pllp_clkm_clk32_plle,            MUX | DIV_U71, TEGRA210_CLK_ID_ENTROPY),
9501         PERIPH_CLK("uart_mipi_cal", "uart_mipi_cal",    NULL,   177,    0x66c,  102000000, mux_pllp_out3_pllp_pllc_clkm,        MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_UART_MIPI_CAL),
9502         PERIPH_CLK("dbgapb",    "dbgapb",               NULL,   185,    0x718,  136000000, mux_pllp_clkm_2,             MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_DBGAPB),
9503         PERIPH_CLK("tsensor",   "tegra-tsensor",        NULL,   100,    0x3b8,   19200000, mux_pllp_pllc_clkm_clk32,    MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_TSENSOR),
9504         PERIPH_CLK("actmon",    "actmon",               NULL,   119,    0x3e8,  204000000, mux_pllp_pllc_clk32_clkm,    MUX | DIV_U71, TEGRA210_CLK_ID_ACTMON),
9505         PERIPH_CLK("extern1",   "extern1",              NULL,   120,    0x3ec,  408000000, mux_plla_clk32_pllp_clkm_plle,       MUX | DIV_U71, TEGRA210_CLK_ID_EXTERN1),
9506         PERIPH_CLK("extern2",   "extern2",              NULL,   121,    0x3f0,  408000000, mux_plla_clk32_pllp_clkm_plle,       MUX | DIV_U71, TEGRA210_CLK_ID_EXTERN2),
9507         PERIPH_CLK("extern3",   "extern3",              NULL,   122,    0x3f4,  408000000, mux_plla_clk32_pllp_clkm_plle,       MUX | DIV_U71, TEGRA210_CLK_ID_EXTERN3),
9508         PERIPH_CLK("pcie",      "tegra-pcie",           "pcie", 70,     0,      250000000, mux_clk_m,                   0, TEGRA210_CLK_ID_PCIE),
9509         PERIPH_CLK("afi",       "tegra-pcie",           "afi",  72,     0,      250000000, mux_clk_m,                   0, TEGRA210_CLK_ID_AFI),
9510         PERIPH_CLK("cl_dvfs_ref", "tegra_cl_dvfs",      "ref",  155,    0x62c,  54000000,  mux_pllp_clkm,               MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_DFLL_REF),
9511         PERIPH_CLK("cl_dvfs_soc", "tegra_cl_dvfs",      "soc",  155,    0x630,  54000000,  mux_pllp_clkm,               MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_DFLL_SOC),
9512         PERIPH_CLK("soc_therm", "soc_therm",            NULL,   78,     0x644,  136000000, mux_clkm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_SOC_THERM),
9513
9514         PERIPH_CLK("dp2",       "dp2",                  NULL,   152,    0,      38400000, mux_clk_m,                    PERIPH_ON_APB, TEGRA210_CLK_ID_DP2),
9515         PERIPH_CLK("mc_bbc",    "mc_bbc",               NULL,   170,    0,      1066000000, mux_clk_mc,                 PERIPH_NO_RESET, TEGRA210_CLK_ID_MC_BBC),
9516         PERIPH_CLK("mc_capa",   "mc_capa",              NULL,   167,    0,      1066000000, mux_clk_mc,                 PERIPH_NO_RESET, TEGRA210_CLK_ID_MC_CAPA),
9517         PERIPH_CLK("mc_cbpa",   "mc_cbpa",              NULL,   168,    0,      1066000000, mux_clk_mc,                 PERIPH_NO_RESET, TEGRA210_CLK_ID_MC_CBPA),
9518         PERIPH_CLK("mc_cpu",    "mc_cpu",               NULL,   169,    0,      1066000000, mux_clk_mc,                 PERIPH_NO_RESET, TEGRA210_CLK_ID_MC_CPU),
9519         PERIPH_CLK("mc_ccpa",   "mc_ccpa",              NULL,   201,    0,      1066000000, mux_clk_mc,                 PERIPH_NO_RESET, TEGRA210_CLK_ID_MC_CCPA),
9520         PERIPH_CLK("mc_cdpa",   "mc_cdpa",              NULL,   200,    0,      1066000000, mux_clk_mc,                 PERIPH_NO_RESET, TEGRA210_CLK_ID_MC_CDPA),
9521
9522         PERIPH_CLK_SKIP("vic03", "vic03",       NULL,   178,    0x678,  0x6f0, 1000000000, mux_pllc_pllp_plla1_pllc2_c3_clkm,   MUX | DIV_U71 | DIV_U71_INT, TEGRA210_CLK_ID_VIC),
9523         PERIPH_CLK_SKIP("msenc", "msenc",       NULL,   219,    0x6a0,  0x6e8, 1000000000, mux_pllc2_c_c3_pllp_plla1_clkm,      MUX | DIV_U71 | DIV_U71_INT, TEGRA210_CLK_ID_NVENC),
9524         PERIPH_CLK_SKIP("nvdec", "nvdec",       NULL,   194,    0x698,  0x6f4, 1000000000, mux_pllc2_c_c3_pllp_plla1_clkm,      MUX | DIV_U71 | DIV_U71_INT, TEGRA210_CLK_ID_NVDEC),
9525         PERIPH_CLK_SKIP("nvjpg", "nvjpg",       NULL,   195,    0x69c,  0x700, 1000000000, mux_pllc2_c_c3_pllp_plla1_clkm,      MUX | DIV_U71 | DIV_U71_INT, TEGRA210_CLK_ID_NVJPG),
9526         PERIPH_CLK_SKIP("se",   "se",           NULL,   127,    0x42c,  0x704, 1000000000, mux_pllp_pllc2_c_c3_clkm,    MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB, TEGRA210_CLK_ID_SE),
9527         PERIPH_CLK_SKIP("tsec", "tegra_tsec",   "tsec", 83,     0x1f4,  0x708,  408000000, mux_pllp_pllc_clkm,          MUX | DIV_U71 | DIV_U71_INT, TEGRA210_CLK_ID_TSEC),
9528         PERIPH_CLK_SKIP("tsecb", "tsecb",       NULL,   206,    0x6d8,  0x70c, 1000000000, mux_pllp_pllc2_c_c3_clkm,    MUX | DIV_U71 | DIV_U71_INT, TEGRA210_CLK_ID_TSECB),
9529         PERIPH_CLK_SKIP("ispa", "isp",          "ispa", 23,     0,      0x6f8, 1000000000, mux_isp,                             PERIPH_ON_APB, TEGRA210_CLK_ID_ISPA),
9530         PERIPH_CLK_SKIP("ispb", "isp",          "ispb", 3,      0,      0x6fc, 1000000000, mux_isp,                             PERIPH_ON_APB, TEGRA210_CLK_ID_ISPB),
9531         PERIPH_CLK_EX("vi",     "vi",           "vi",   20,     0x148,         1000000000, mux_pllc2_c_c3_pllp_clkm_plla1_pllc4, MUX | DIV_U71 | DIV_U71_INT, TEGRA210_CLK_ID_VI, &tegra_vi_clk_ops),
9532         SUPER_SKIP_CLK("vi_skip", "vi",         "skip",         0x6ec,             NULL, 0),
9533
9534         SHARED_SCLK("avp.sclk", "nvavp",                "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0, TEGRA210_CLK_ID_SBUS_AVP_USER),
9535         SHARED_SCLK("usbd.sclk", "tegra-udc.0",         "sclk", &tegra_clk_ahb, NULL, 0, 0, TEGRA210_CLK_ID_SBUS_USBD_USER),
9536         SHARED_SCLK("usb1.sclk", "tegra-ehci.0",        "sclk", &tegra_clk_ahb, NULL, 0, 0, TEGRA210_CLK_ID_SBUS_USBD1_USER),
9537         SHARED_SCLK("usb2.sclk", "tegra-ehci.1",        "sclk", &tegra_clk_ahb, NULL, 0, 0, TEGRA210_CLK_ID_SBUS_USBD2_USER),
9538         SHARED_SCLK("sdmmc4.sclk",    "sdhci-tegra.3",  "sclk", &tegra_clk_ahb, NULL, 0, 0, TEGRA210_CLK_ID_SBUS_SDMMC4_USER),
9539         SHARED_SCLK("wake.sclk",   "wake_sclk",         "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0, TEGRA210_CLK_ID_SBUS_WAKE_USER),
9540         SHARED_SCLK("camera.sclk", "vi",                "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0, TEGRA210_CLK_ID_SBUS_CAMERA_USER),
9541         SHARED_SCLK("mon.avp",   "tegra_actmon",        "avp",  &tegra_clk_sbus_cmplx, NULL, 0, 0, TEGRA210_CLK_ID_SBUS_MON_AVP_USER),
9542         SHARED_SCLK("cap.sclk",  "cap_sclk",            NULL,   &tegra_clk_sbus_cmplx, NULL, 0, SHARED_CEILING, 0),
9543         SHARED_SCLK("cap.vcore.sclk", "cap.vcore.sclk", NULL,   &tegra_clk_sbus_cmplx, NULL, 0, SHARED_CEILING, 0),
9544         SHARED_SCLK("cap.throttle.sclk", "cap_throttle", NULL,  &tegra_clk_sbus_cmplx, NULL, 0, SHARED_CEILING, 0),
9545         SHARED_SCLK("floor.sclk", "floor_sclk",         NULL,   &tegra_clk_sbus_cmplx, NULL, 0, 0, 0),
9546         SHARED_SCLK("override.sclk", "override_sclk",   NULL,   &tegra_clk_sbus_cmplx, NULL, 0, SHARED_OVERRIDE, 0),
9547         SHARED_SCLK("sbc1.sclk", "7000d400.spi",        "sclk", &tegra_clk_apb,        NULL, 0, 0, TEGRA210_CLK_ID_SBUS_SBC1_USER),
9548         SHARED_SCLK("sbc2.sclk", "7000d600.spi",        "sclk", &tegra_clk_apb,        NULL, 0, 0, TEGRA210_CLK_ID_SBUS_SBC2_USER),
9549         SHARED_SCLK("sbc3.sclk", "7000d800.spi",        "sclk", &tegra_clk_apb,        NULL, 0, 0, TEGRA210_CLK_ID_SBUS_SBC3_USER),
9550         SHARED_SCLK("sbc4.sclk", "7000da00.spi",        "sclk", &tegra_clk_apb,        NULL, 0, 0, TEGRA210_CLK_ID_SBUS_SBC4_USER),
9551         SHARED_SCLK("qspi.sclk", "qspi",                "sclk", &tegra_clk_apb,        NULL, 0, 0, TEGRA210_CLK_ID_SBUS_QSPI_USER),
9552         SHARED_SCLK("boot.apb.sclk", "boot.apb.sclk",   NULL,   &tegra_clk_apb,        NULL, 0, 0, TEGRA210_CLK_ID_SBUS_BOOT_APB_USER),
9553
9554         SHARED_EMC_CLK("avp.emc",       "nvavp",        "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_AVP_USER),
9555         SHARED_EMC_CLK("cpu.emc",       "tegra-cpu", "cpu_emc", &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_CPU_USER),
9556         SHARED_EMC_CLK("disp1.emc",     "tegradc.0",    "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_DC1), TEGRA210_CLK_ID_EMC_DISP1_USER),
9557         SHARED_EMC_CLK("disp2.emc",     "tegradc.1",    "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_DC2), TEGRA210_CLK_ID_EMC_DISP2_USER),
9558         SHARED_EMC_CLK("disp1.la.emc",  "tegradc.0",    "emc.la", &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_DISP1_LA_USER),
9559         SHARED_EMC_CLK("disp2.la.emc",  "tegradc.1",    "emc.la", &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_DISP2_LA_USER),
9560         SHARED_EMC_CLK("usbd.emc",      "tegra-udc.0",  "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_USBD_USER),
9561         SHARED_EMC_CLK("usb1.emc",      "tegra-ehci.0", "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_USB1_USER),
9562         SHARED_EMC_CLK("usb2.emc",      "tegra-ehci.1", "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_USB2_USER),
9563         SHARED_EMC_CLK("sdmmc3.emc",    "sdhci-tegra.2", "emc", &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_SDMMC3_USER),
9564         SHARED_EMC_CLK("sdmmc4.emc",    "sdhci-tegra.3", "emc", &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_SDMMC4_USER),
9565         SHARED_EMC_CLK("mon.emc",       "tegra_actmon", "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_MON_USER),
9566         SHARED_EMC_CLK("cap.emc",       "cap.emc",      NULL,   &tegra_clk_emc, NULL, 0, SHARED_CEILING, 0, 0),
9567         SHARED_EMC_CLK("cap.vcore.emc", "cap.vcore.emc", NULL,  &tegra_clk_emc, NULL, 0, SHARED_CEILING, 0, 0),
9568         SHARED_EMC_CLK("cap.throttle.emc", "cap_throttle", NULL, &tegra_clk_emc, NULL, 0, SHARED_CEILING_BUT_ISO, 0, 0),
9569         SHARED_EMC_CLK("3d.emc",        "tegra_gpu.0",  "emc",  &tegra_clk_emc, NULL, 0, 0,             BIT(EMC_USER_3D), TEGRA210_CLK_ID_EMC_3D_USER),
9570         SHARED_EMC_CLK("msenc.emc",     "tegra_msenc",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_BW,     BIT(EMC_USER_MSENC), TEGRA210_CLK_ID_EMC_NVENC_USER),
9571         SHARED_EMC_CLK("nvjpg.emc",     "tegra_nvjpg",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_BW,     BIT(EMC_USER_NVJPG), TEGRA210_CLK_ID_EMC_NVJPG_USER),
9572         SHARED_EMC_CLK("nvdec.emc",     "tegra_nvdec",  "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_NVDEC_USER),
9573         SHARED_EMC_CLK("tsec.emc",      "tegra_tsec",   "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_TSEC_USER),
9574         SHARED_EMC_CLK("tsecb.emc",     "tegra_tsecb",  "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_TSECB_USER),
9575 #ifdef CONFIG_VI_ONE_DEVICE
9576         SHARED_EMC_CLK("vi.emc",        "tegra_vi",     "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_VI), TEGRA210_CLK_ID_EMC_VI_USER),
9577 #else
9578         SHARED_EMC_CLK("via.emc",       "tegra_vi.0",   "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_VI), TEGRA210_CLK_ID_EMC_VIA_USER),
9579         SHARED_EMC_CLK("vib.emc",       "tegra_vi.1",   "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_VI2), TEGRA210_CLK_ID_EMC_VIB_USER),
9580 #endif
9581         SHARED_EMC_CLK("ispa.emc",      "tegra_isp.0",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_ISP1), TEGRA210_CLK_ID_EMC_ISPA_USER),
9582         SHARED_EMC_CLK("ispb.emc",      "tegra_isp.1",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_ISP2), TEGRA210_CLK_ID_EMC_ISPB_USER),
9583         SHARED_EMC_CLK("camera.emc", "tegra_camera_ctrl",       "emc",  &tegra_clk_emc, NULL, 0, SHARED_BW, 0, TEGRA210_CLK_ID_EMC_CAMERA_USER),
9584         SHARED_EMC_CLK("iso.emc",       "iso",          "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_ISO_USER),
9585         SHARED_EMC_CLK("floor.emc",     "floor.emc",    NULL,   &tegra_clk_emc, NULL, 0, 0, 0, 0),
9586         SHARED_EMC_CLK("override.emc", "override.emc",  NULL,   &tegra_clk_emc, NULL, 0, SHARED_OVERRIDE, 0, 0),
9587         SHARED_EMC_CLK("vic.emc",       "tegra_vic03",  "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_VIC_USER),
9588         SHARED_EMC_CLK("vic_shared.emc",        "tegra_vic03",  "emc_shared",  &tegra_clk_emc, NULL, 0, SHARED_BW, 0, TEGRA210_CLK_ID_EMC_VIC_SHARED_USER),
9589         SHARED_EMC_CLK("battery.emc",   "battery_edp",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_CEILING, 0, 0),
9590         SHARED_EMC_CLK("ape.emc", "ape", "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_APE_USER),
9591         SHARED_EMC_CLK("pcie.emc", "tegra_pcie", "emc",  &tegra_clk_emc, NULL, 0, 0, 0, TEGRA210_CLK_ID_EMC_PCIE_USER),
9592
9593         DUAL_CBUS_CLK("vic03.cbus",     "tegra_vic03",          "vic03", &tegra_clk_c2bus, "vic03", 0, 0, TEGRA210_CLK_ID_CXBUS_VIC_USER),
9594         DUAL_CBUS_CLK("nvjpg.cbus",     "tegra_nvjpg",          "nvjpg", &tegra_clk_c2bus, "nvjpg", 0, 0, TEGRA210_CLK_ID_CXBUS_NVJPG_USER),
9595         DUAL_CBUS_CLK("se.cbus",        "tegra21-se",           NULL,    &tegra_clk_c2bus, "se",    0, 0, TEGRA210_CLK_ID_CXBUS_SE_USER),
9596         DUAL_CBUS_CLK("tsecb.cbus",     "tegra_tsecb",          "tsecb", &tegra_clk_c2bus, "tsecb", 0, 0, TEGRA210_CLK_ID_CXBUS_TSECB_USER),
9597         SHARED_LIMIT("cap.c2bus",       "cap.c2bus",            NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING, 0),
9598         SHARED_LIMIT("cap.vcore.c2bus", "cap.vcore.c2bus",      NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING, 0),
9599         SHARED_LIMIT("cap.throttle.c2bus", "cap_throttle",      NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING, 0),
9600         SHARED_LIMIT("floor.c2bus",     "floor.c2bus",          NULL,    &tegra_clk_c2bus, NULL,    0, 0, 0),
9601         SHARED_CLK("override.c2bus",    "override.c2bus",       NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_OVERRIDE, 0),
9602         SHARED_LIMIT("edp.c2bus",       "edp.c2bus",            NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING, 0),
9603         SHARED_LIMIT("vic_floor.cbus",  "tegra_vic03",          "vic_floor", &tegra_clk_c2bus, NULL, 0, 0, TEGRA210_CLK_ID_CXBUS_VIC_FLOOR_USER),
9604
9605         DUAL_CBUS_CLK("msenc.cbus",     "tegra_msenc",          "msenc", &tegra_clk_c3bus, "msenc", 0, 0, TEGRA210_CLK_ID_CXBUS_NVENC_USER),
9606         DUAL_CBUS_CLK("nvdec.cbus",     "tegra_nvdec",          "nvdec", &tegra_clk_c3bus, "nvdec", 0, 0, TEGRA210_CLK_ID_CXBUS_NVDEC_USER),
9607         SHARED_LIMIT("cap.c3bus",       "cap.c3bus",            NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_CEILING, 0),
9608         SHARED_LIMIT("cap.vcore.c3bus", "cap.vcore.c3bus",      NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_CEILING, 0),
9609         SHARED_LIMIT("cap.throttle.c3bus", "cap_throttle",      NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_CEILING, 0),
9610         SHARED_LIMIT("floor.c3bus",     "floor.c3bus",          NULL,    &tegra_clk_c3bus, NULL,    0, 0, 0),
9611         SHARED_CLK("override.c3bus",    "override.c3bus",       NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_OVERRIDE, 0),
9612
9613         SHARED_CLK("gm20b.gbus",        "tegra_gpu.0",          "gpu",  &tegra_clk_gbus, NULL,  0, 0, TEGRA210_CLK_ID_GBUS_GM20B_USER),
9614         SHARED_LIMIT("cap.gbus",        "cap.gbus",             NULL,   &tegra_clk_gbus, NULL,  0, SHARED_CEILING, 0),
9615         SHARED_LIMIT("edp.gbus",        "edp.gbus",             NULL,   &tegra_clk_gbus, NULL,  0, SHARED_CEILING, 0),
9616         SHARED_LIMIT("battery.gbus",    "battery_edp",          "gpu",  &tegra_clk_gbus, NULL,  0, SHARED_CEILING, 0),
9617         SHARED_LIMIT("cap.vgpu.gbus",   "cap.vgpu.gbus",        NULL,   &tegra_clk_gbus, NULL,  0, SHARED_CEILING, 0),
9618         SHARED_LIMIT("cap.throttle.gbus", "cap_throttle",       NULL,   &tegra_clk_gbus, NULL,  0, SHARED_CEILING, 0),
9619         SHARED_LIMIT("cap.profile.gbus", "profile.gbus",        "cap",  &tegra_clk_gbus, NULL,  0, SHARED_CEILING, 0),
9620         SHARED_CLK("override.gbus",     "override.gbus",        NULL,   &tegra_clk_gbus, NULL,  0, SHARED_OVERRIDE, 0),
9621         SHARED_LIMIT("floor.gbus",      "floor.gbus",           NULL,   &tegra_clk_gbus, NULL,  0, 0, 0),
9622         SHARED_LIMIT("floor.profile.gbus", "profile.gbus",      "floor", &tegra_clk_gbus, NULL, 0, 0, 0),
9623
9624         SHARED_CLK("nv.host1x", "tegra_host1x",         "host1x", &tegra_clk_host1x, NULL,  0, 0, TEGRA210_CLK_ID_HOST1X_NV_USER),
9625         SHARED_CLK("vi.host1x", "tegra_vi",             "host1x", &tegra_clk_host1x, NULL,  0, 0, TEGRA210_CLK_ID_HOST1X_VI_USER),
9626         SHARED_CLK("vii2c.host1x", "546c0000.i2c",      "host1x", &tegra_clk_host1x, NULL,  0, 0, TEGRA210_CLK_ID_HOST1X_VII2C_USER),
9627         SHARED_LIMIT("cap.host1x", "cap.host1x",        NULL,     &tegra_clk_host1x, NULL,  0, SHARED_CEILING, 0),
9628         SHARED_LIMIT("cap.vcore.host1x", "cap.vcore.host1x", NULL, &tegra_clk_host1x, NULL, 0, SHARED_CEILING, 0),
9629         SHARED_LIMIT("floor.host1x", "floor.host1x",    NULL,     &tegra_clk_host1x, NULL,  0, 0, 0),
9630         SHARED_CLK("override.host1x", "override.host1x", NULL,    &tegra_clk_host1x, NULL,  0, SHARED_OVERRIDE, 0),
9631
9632         SHARED_CLK("cpu.mselect",         "cpu",        "mselect",   &tegra_clk_mselect, NULL,  0, 0, TEGRA210_CLK_ID_MSELECT_CPU_USER),
9633         SHARED_CLK("pcie.mselect",        "tegra_pcie", "mselect",   &tegra_clk_mselect, NULL,  0, 0, TEGRA210_CLK_ID_MSELECT_PCIE_USER),
9634         SHARED_LIMIT("cap.vcore.mselect", "cap.vcore.mselect", NULL, &tegra_clk_mselect, NULL,  0, SHARED_CEILING, 0),
9635         SHARED_CLK("override.mselect",    "override.mselect",  NULL, &tegra_clk_mselect, NULL,  0, SHARED_OVERRIDE, 0),
9636
9637         SHARED_CLK("adma.ape",          NULL,           "adma.ape",  &tegra_clk_ape, NULL,  0, 0, TEGRA210_CLK_ID_APE_ADMA_USER),
9638         SHARED_CLK("adsp.ape",          NULL,           "adsp.ape",  &tegra_clk_ape, NULL,  0, 0, TEGRA210_CLK_ID_APE_ADSP_USER),
9639         SHARED_CLK("xbar.ape",          NULL,           "xbar.ape",  &tegra_clk_ape, NULL,  0, 0, TEGRA210_CLK_ID_APE_XBAR_USER),
9640         SHARED_LIMIT("cap.vcore.ape",   "cap.vcore.ape", NULL,       &tegra_clk_ape, NULL,  0, SHARED_CEILING, 0),
9641         SHARED_CLK("override.ape",      "override.ape",  NULL,       &tegra_clk_ape, NULL,  0, SHARED_OVERRIDE, 0),
9642
9643         SHARED_CLK("adsp_cpu.abus",     NULL,             "adsp_cpu",   &tegra_clk_virtual_adsp_bus, NULL,  0, 0, TEGRA210_CLK_ID_ADSP_CPU),
9644         SHARED_LIMIT("cap.vcore.abus",  "cap.vcore.abus", NULL,         &tegra_clk_virtual_adsp_bus, NULL,  0, SHARED_CEILING, 0),
9645         SHARED_CLK("override.abus",     "override.abus",  NULL,         &tegra_clk_virtual_adsp_bus, NULL,  0, SHARED_OVERRIDE, 0),
9646 };
9647
9648 /* VI, ISP buses */
9649 static struct clk tegra_visp_clks[] = {
9650         SHARED_CONNECT("vi.cbus",       "vi.cbus",      NULL,   &tegra_clk_cbus,   "vi",    0, 0, TEGRA210_CLK_ID_CXBUS_VI_USER),
9651         SHARED_CONNECT("isp.cbus",      "isp.cbus",     NULL,   &tegra_clk_cbus,   "isp",   0, 0, TEGRA210_CLK_ID_CXBUS_ISP_USER),
9652         SHARED_CLK("override.cbus",     "override.cbus", NULL,  &tegra_clk_cbus,    NULL,   0, SHARED_OVERRIDE, 0),
9653         SHARED_LIMIT("cap.vcore.cbus",  "cap.vcore.cbus", NULL, &tegra_clk_cbus,    NULL,   0, SHARED_CEILING, 0),
9654
9655 #ifndef CONFIG_VI_ONE_DEVICE
9656         SHARED_CLK("via.vi.cbus",       "via.vi",       NULL,   &tegra_visp_clks[0], NULL,   0, 0, TEGRA210_CLK_ID_CXBUS_VI_VIA_USER),
9657         SHARED_CLK("vib.vi.cbus",       "vib.vi",       NULL,   &tegra_visp_clks[0], NULL,   0, 0, TEGRA210_CLK_ID_CXBUS_VI_VIB_USER),
9658 #endif
9659
9660         SHARED_CLK("ispa.isp.cbus",     "ispa.isp",     NULL,   &tegra_visp_clks[1], "ispa", 0, 0, TEGRA210_CLK_ID_CXBUS_ISP_ISPA_USER),
9661         SHARED_CLK("ispb.isp.cbus",     "ispb.isp",     NULL,   &tegra_visp_clks[1], "ispb", 0, 0, TEGRA210_CLK_ID_CXBUS_ISP_ISPB_USER),
9662 };
9663
9664 /* XUSB clocks */
9665 #define XUSB_ID "tegra-xhci"
9666 #define XUDC_ID "tegra-xudc"
9667 /* xusb common clock gate - enabled on init and never disabled */
9668 static void tegra21_xusb_gate_clk_init(struct clk *c)
9669 {
9670         tegra21_periph_clk_enable(c);
9671 }
9672
9673 static struct clk_ops tegra_xusb_gate_clk_ops = {
9674         .init    = tegra21_xusb_gate_clk_init,
9675 };
9676
9677 static struct clk tegra_clk_xusb_gate = {
9678         .name      = "xusb_gate",
9679         .clk_id    = TEGRA210_CLK_ID_XUSB_GATE,
9680         .flags     = ENABLE_ON_INIT | PERIPH_NO_RESET,
9681         .ops       = &tegra_xusb_gate_clk_ops,
9682         .parent    = &tegra_clk_osc,
9683         .rate      = 12000000,
9684         .max_rate  = 48000000,
9685         .u.periph = {
9686                 .clk_num   = 143,
9687         },
9688 };
9689
9690 static struct clk tegra_xusb_source_clks[] = {
9691         PERIPH_CLK("xusb_host_src",     XUSB_ID, "host_src",    143,    0x600,  112000000, mux_clkm_pllp_pllre,         MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB, TEGRA210_CLK_ID_XUSB_HOST_SRC),
9692         PERIPH_CLK("xusb_falcon_src",   XUSB_ID, "falcon_src",  143,    0x604,  336000000, mux_clkm_pllp_pllre,         MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_XUSB_FALCON_SRC),
9693         PERIPH_CLK("xusb_fs_src",       NULL, "fs_src",         143,    0x608,   48000000, mux_clkm_48M_pllp_480M,      MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_XUSB_FS_SRC),
9694         PERIPH_CLK_EX("xusb_ss_src",    NULL, "ss_src",         143,    0x610,  120000000, mux_clkm_pllre_clk32_480M,   MUX | DIV_U71 | PERIPH_NO_RESET, TEGRA210_CLK_ID_XUSB_SS_SRC, &tegra_xusb_ss_ops),
9695         PERIPH_CLK("xusb_dev_src",      XUDC_ID, "dev_src",     95,     0x60c,  112000000, mux_clkm_pllp_pllre,         MUX | DIV_U71 | PERIPH_ON_APB, TEGRA210_CLK_ID_XUSB_DEV_SRC),
9696         SHARED_EMC_CLK("xusb.emc",      XUSB_ID, "emc", &tegra_clk_emc, NULL,   0,      SHARED_BW, 0, TEGRA210_CLK_ID_EMC_XUSB_USER),
9697 };
9698
9699 static struct clk_mux_sel mux_ss_clk_m[] = {
9700         { .input = &tegra_xusb_source_clks[3], .value = 0},
9701         { .input = &tegra_clk_m,               .value = 1},
9702         { NULL, 0},
9703 };
9704
9705 static struct clk tegra_xusb_ssp_src = {
9706         .name      = "xusb_ssp_src",
9707         .clk_id    = TEGRA210_CLK_ID_XUSB_SSP_SRC,
9708         .lookup    = {
9709                 .dev_id    = NULL,
9710                 .con_id    = "ssp_src",
9711         },
9712         .ops       = &tegra_xusb_ss_ops,
9713         .reg       = 0x610,
9714         .inputs    = mux_ss_clk_m,
9715         .flags     = MUX | PERIPH_NO_ENB | PERIPH_NO_RESET,
9716         .max_rate  = 120000000,
9717         .u.periph = {
9718                 .src_mask  = 0x1 << 24,
9719                 .src_shift = 24,
9720         },
9721 };
9722
9723 static struct clk tegra_xusb_ss_div2 = {
9724         .name      = "xusb_ss_div2",
9725         .clk_id    = TEGRA210_CLK_ID_XUSB_SS_DIV2,
9726         .ops       = &tegra_clk_m_div_ops,
9727         .parent    = &tegra_xusb_source_clks[3],
9728         .mul       = 1,
9729         .div       = 2,
9730         .state     = OFF,
9731         .max_rate  = 60000000,
9732 };
9733
9734 static struct clk_mux_sel mux_ss_div2_pllu_60M[] = {
9735         { .input = &tegra_xusb_ss_div2, .value = 0},
9736         { .input = &tegra_pll_u_60M,    .value = 1},
9737         { .input = &tegra_xusb_source_clks[3], .value = 2},
9738         { .input = &tegra_xusb_source_clks[3], .value = 3},
9739         { NULL, 0},
9740 };
9741
9742 static struct clk tegra_xusb_hs_src = {
9743         .name      = "xusb_hs_src",
9744         .clk_id    = TEGRA210_CLK_ID_XUSB_HS_SRC,
9745         .lookup    = {
9746                 .dev_id    = NULL,
9747                 .con_id    = "hs_src",
9748         },
9749         .ops       = &tegra_xusb_ss_ops,
9750         .reg       = 0x610,
9751         .inputs    = mux_ss_div2_pllu_60M,
9752         .flags     = MUX | PLLU | PERIPH_NO_ENB | PERIPH_NO_RESET,
9753         .max_rate  = 120000000,
9754         .u.periph = {
9755                 .src_mask  = 0x3 << 25,
9756                 .src_shift = 25,
9757         },
9758 };
9759
9760 static struct clk_mux_sel mux_xusb_host[] = {
9761         { .input = &tegra_xusb_source_clks[0], .value = 0},
9762         { .input = &tegra_xusb_source_clks[1], .value = 1},
9763         { .input = &tegra_xusb_source_clks[2], .value = 2},
9764         { .input = &tegra_xusb_hs_src,         .value = 5},
9765         { NULL, 0},
9766 };
9767
9768 static struct clk_mux_sel mux_xusb_ss[] = {
9769         { .input = &tegra_xusb_ssp_src,        .value = 3},
9770         { .input = &tegra_xusb_source_clks[0], .value = 0},
9771         { .input = &tegra_xusb_source_clks[1], .value = 1},
9772         { NULL, 0},
9773 };
9774
9775 static struct clk_mux_sel mux_xusb_dev[] = {
9776         { .input = &tegra_xusb_source_clks[4], .value = 4},
9777         { .input = &tegra_xusb_source_clks[2], .value = 2},
9778         { .input = &tegra_xusb_ssp_src,        .value = 3},
9779         { .input = &tegra_xusb_hs_src,         .value = 5},
9780         { NULL, 0},
9781 };
9782
9783 static struct clk tegra_xusb_coupled_clks[] = {
9784         PERIPH_CLK_EX("xusb_host", XUSB_ID, "host", 89, 0, 336000000, mux_xusb_host, 0, TEGRA210_CLK_ID_XUSB_HOST, &tegra_clk_coupled_gate_ops),
9785         PERIPH_CLK_EX("xusb_ss",   NULL,    "ss",  156, 0, 336000000, mux_xusb_ss,   0, TEGRA210_CLK_ID_XUSB_SS, &tegra_clk_coupled_gate_ops),
9786         PERIPH_CLK_EX("xusb_dev",  XUDC_ID, "dev",  95, 0, 120000000, mux_xusb_dev,  0, TEGRA210_CLK_ID_XUSB_DEV, &tegra_clk_coupled_gate_ops),
9787 };
9788
9789 #define SLCG_CLK(_root_name, _name_ext, _dev, _con, _reg, _bit)         \
9790         {                                                               \
9791                 .name      = _root_name "_slcg_ovr" _name_ext,          \
9792                 .lookup = {                                             \
9793                         .dev_id = _dev,                                 \
9794                         .con_id = _con,                                 \
9795                 },                                                      \
9796                 .ops       = &tegra_clk_slcg_ops,                       \
9797                 .reg       = _reg,                                      \
9798                 .max_rate  = 38400000, /* replaced by root max */       \
9799                 .u.periph = {                                           \
9800                         .clk_num   = _bit,                              \
9801                 },                                                      \
9802         }
9803
9804 static struct clk tegra_slcg_clks[] = {
9805         SLCG_CLK("disp2",       "",     "tegradc.1",    "slcg",         0xf8,   2),
9806         SLCG_CLK("disp1",       "",     "tegradc.0",    "slcg",         0xf8,   1),
9807
9808         SLCG_CLK("vi",          "",     "tegra_vi",     "slcg",         0xf8,   15),
9809         SLCG_CLK("ispa",        "",     "tegra_isp.0",  "slcg",         0x554,  3),
9810         SLCG_CLK("ispb",        "",     "tegra_isp.1",  "slcg",         0x3a4,  22),
9811
9812         SLCG_CLK("nvdec",       "",     "tegra_nvdec",  "slcg",         0x554,  31),
9813         SLCG_CLK("msenc",       "",     "tegra_msenc",  "slcg",         0x554,  29),
9814         SLCG_CLK("nvjpg",       "",     "tegra_nvjpg",  "slcg",         0x554,  9),
9815         SLCG_CLK("vic03",       "",     "tegra_vic03",  "slcg",         0x554,  5),
9816
9817         SLCG_CLK("xusb_dev",    "",     XUDC_ID,        "slcg",         0x3a0,  31),
9818         SLCG_CLK("xusb_host",   "",     XUSB_ID,        "slcg",         0x3a0,  30),
9819         SLCG_CLK("sata",        "_fpci", "tegra_sata",  "slcg_fpci",    0x3a0,  19),
9820         SLCG_CLK("sata",        "_ipfs", "tegra_sata",  "slcg_ipfs",    0x3a0,  17),
9821         SLCG_CLK("sata",        "",     "tegra_sata",   "slcg",         0x3a0,  0),
9822
9823         SLCG_CLK("d_audio",     "",   "tegra210-axbar", "slcg",         0x3a0,  1),
9824         SLCG_CLK("adsp",        "",     NULL,           "slcg_adsp",    0x554,  11),
9825         SLCG_CLK("ape",         "",     NULL,           "slcg_ape",     0x554,  10),
9826 };
9827
9828 #define CLK_DUPLICATE(_name, _dev, _con)                \
9829         {                                               \
9830                 .name   = _name,                        \
9831                 .lookup = {                             \
9832                         .dev_id = _dev,                 \
9833                         .con_id = _con,                 \
9834                 },                                      \
9835         }
9836
9837 /* Some clocks may be used by different drivers depending on the board
9838  * configuration.  List those here to register them twice in the clock lookup
9839  * table under two names.
9840  */
9841 static struct clk_duplicate tegra_clk_duplicates[] = {
9842         CLK_DUPLICATE("uarta",  "serial8250.0", NULL),
9843         CLK_DUPLICATE("uartb",  "serial8250.1", NULL),
9844         CLK_DUPLICATE("uartc",  "serial8250.2", NULL),
9845         CLK_DUPLICATE("uartd",  "serial8250.3", NULL),
9846         CLK_DUPLICATE("usbd", XUSB_ID, "utmip-pad"),
9847         CLK_DUPLICATE("usbd", "utmip-pad", NULL),
9848         CLK_DUPLICATE("usbd", "tegra-ehci.0", NULL),
9849         CLK_DUPLICATE("usbd", "tegra-otg", NULL),
9850         CLK_DUPLICATE("disp1", "tegra_dc_dsi_vs1.0", NULL),
9851         CLK_DUPLICATE("disp1.emc", "tegra_dc_dsi_vs1.0", "emc"),
9852         CLK_DUPLICATE("disp1", "tegra_dc_dsi_vs1.1", NULL),
9853         CLK_DUPLICATE("disp1.emc", "tegra_dc_dsi_vs1.1", "emc"),
9854         CLK_DUPLICATE("dsib", "tegradc.0", "dsib"),
9855         CLK_DUPLICATE("dsia", "tegradc.1", "dsia"),
9856         CLK_DUPLICATE("dsiblp", "tegradc.0", "dsiblp"),
9857         CLK_DUPLICATE("dsialp", "tegradc.1", "dsialp"),
9858         CLK_DUPLICATE("dsia", "tegra_dc_dsi_vs1.0", "dsia"),
9859         CLK_DUPLICATE("dsia", "tegra_dc_dsi_vs1.1", "dsia"),
9860         CLK_DUPLICATE("dsialp", "tegra_dc_dsi_vs1.0", "dsialp"),
9861         CLK_DUPLICATE("dsialp", "tegra_dc_dsi_vs1.1", "dsialp"),
9862         CLK_DUPLICATE("dsi1-fixed", "tegra_dc_dsi_vs1.0", "dsi-fixed"),
9863         CLK_DUPLICATE("dsi1-fixed", "tegra_dc_dsi_vs1.1", "dsi-fixed"),
9864         CLK_DUPLICATE("pwm", "tegra_pwm.0", NULL),
9865         CLK_DUPLICATE("pwm", "tegra_pwm.1", NULL),
9866         CLK_DUPLICATE("pwm", "tegra_pwm.2", NULL),
9867         CLK_DUPLICATE("pwm", "tegra_pwm.3", NULL),
9868         CLK_DUPLICATE("cop", "nvavp", "cop"),
9869         CLK_DUPLICATE("bsev", "nvavp", "bsev"),
9870         CLK_DUPLICATE("cml1", "tegra_sata_cml", NULL),
9871         CLK_DUPLICATE("cml0", "tegra_pcie", "cml"),
9872         CLK_DUPLICATE("pciex", "tegra_pcie", "pciex"),
9873         CLK_DUPLICATE("pex_uphy", "tegra_pcie", "pex_uphy"),
9874         CLK_DUPLICATE("clk_m", NULL, "apb_pclk"),
9875         CLK_DUPLICATE("i2c1", "tegra-i2c-slave.0", NULL),
9876         CLK_DUPLICATE("i2c2", "tegra-i2c-slave.1", NULL),
9877         CLK_DUPLICATE("i2c3", "tegra-i2c-slave.2", NULL),
9878         CLK_DUPLICATE("i2c4", "tegra-i2c-slave.3", NULL),
9879         CLK_DUPLICATE("i2c5", "tegra-i2c-slave.4", NULL),
9880         CLK_DUPLICATE("cl_dvfs_ref", "7000d000.i2c", NULL),
9881         CLK_DUPLICATE("cl_dvfs_soc", "7000d000.i2c", NULL),
9882         CLK_DUPLICATE("sbc1", "tegra11-spi-slave.0", NULL),
9883         CLK_DUPLICATE("sbc2", "tegra11-spi-slave.1", NULL),
9884         CLK_DUPLICATE("sbc3", "tegra11-spi-slave.2", NULL),
9885         CLK_DUPLICATE("sbc4", "tegra11-spi-slave.3", NULL),
9886         CLK_DUPLICATE("pll_p", "7000d400.spi", "pll_p"),
9887         CLK_DUPLICATE("pll_p", "7000d600.spi", "pll_p"),
9888         CLK_DUPLICATE("pll_p", "7000d800.spi", "pll_p"),
9889         CLK_DUPLICATE("pll_p", "7000da00.spi", "pll_p"),
9890         CLK_DUPLICATE("pll_p", "7000dc00.spi", "pll_p"),
9891         CLK_DUPLICATE("pll_p", "7000de00.spi", "pll_p"),
9892         CLK_DUPLICATE("clk_m", "7000d400.spi", "clk_m"),
9893         CLK_DUPLICATE("clk_m", "7000d600.spi", "clk_m"),
9894         CLK_DUPLICATE("clk_m", "7000d800.spi", "clk_m"),
9895         CLK_DUPLICATE("clk_m", "7000da00.spi", "clk_m"),
9896         CLK_DUPLICATE("clk_m", "7000dc00.spi", "clk_m"),
9897         CLK_DUPLICATE("clk_m", "7000de00.spi", "clk_m"),
9898         CLK_DUPLICATE("i2c5", "tegra_cl_dvfs", "i2c"),
9899         CLK_DUPLICATE("cpu_g", "tegra_cl_dvfs", "safe_dvfs"),
9900         CLK_DUPLICATE("actmon", "tegra_host1x", "actmon"),
9901         CLK_DUPLICATE("gpu_ref", "tegra_gpu.0", "PLLG_ref"),
9902         CLK_DUPLICATE("pll_p_out5", "tegra_gpu.0", "pwr"),
9903         CLK_DUPLICATE("ispa.isp.cbus", "tegra_isp.0", "isp"),
9904         CLK_DUPLICATE("ispb.isp.cbus", "tegra_isp.1", "isp"),
9905         CLK_DUPLICATE("vii2c", "tegra_vi", "vii2c"),
9906         CLK_DUPLICATE("i2cslow", "tegra_vi", "i2cslow"),
9907         CLK_DUPLICATE("mclk3", NULL, "cam_mclk1"),
9908         CLK_DUPLICATE("mclk", NULL, "cam_mclk2"),
9909         CLK_DUPLICATE("mclk2", NULL, "cam_mclk3"),
9910 #ifdef CONFIG_VI_ONE_DEVICE
9911         CLK_DUPLICATE("vi.cbus", "tegra_vi", "vi"),
9912         CLK_DUPLICATE("csi", "tegra_vi", "csi"),
9913         CLK_DUPLICATE("csus", "tegra_vi", "csus"),
9914         CLK_DUPLICATE("vim2_clk", "tegra_vi", "vim2_clk"),
9915         CLK_DUPLICATE("cilab", "tegra_vi", "cilab"),
9916         CLK_DUPLICATE("cilcd", "tegra_vi", "cilcd"),
9917         CLK_DUPLICATE("cile", "tegra_vi", "cile"),
9918 #else
9919         CLK_DUPLICATE("via.vi.cbus", "tegra_vi", "vi"),
9920         CLK_DUPLICATE("vib.vi.cbus", "tegra_vi.1", "vi"),
9921         CLK_DUPLICATE("csi", "tegra_vi", "csi"),
9922         CLK_DUPLICATE("csi", "tegra_vi.1", "csi"),
9923         CLK_DUPLICATE("csus", "tegra_vi", "csus"),
9924         CLK_DUPLICATE("vim2_clk", "tegra_vi.1", "vim2_clk"),
9925         CLK_DUPLICATE("cilab", "tegra_vi", "cilab"),
9926         CLK_DUPLICATE("cilcd", "tegra_vi.1", "cilcd"),
9927         CLK_DUPLICATE("cile", "tegra_vi.1", "cile"),
9928         CLK_DUPLICATE("via.emc", "tegra_vi", "emc"),
9929 #endif
9930         CLK_DUPLICATE("spdif_in", NULL, "spdif_in"),
9931         CLK_DUPLICATE("dmic1", "tegra-dmic.0", NULL),
9932         CLK_DUPLICATE("dmic2", "tegra-dmic.1", NULL),
9933         CLK_DUPLICATE("dmic3", "tegra-dmic.2", NULL),
9934         CLK_DUPLICATE("d_audio", "tegra210-adma", NULL),
9935         CLK_DUPLICATE("d_audio", "nvadsp", "ahub"),
9936         CLK_DUPLICATE("mclk", NULL, "default_mclk"),
9937         CLK_DUPLICATE("uart_mipi_cal", "clk72mhz", NULL),
9938 };
9939
9940
9941 static struct clk *tegra_main_clks[] = {
9942         &tegra_clk_32k,
9943         &tegra_clk_osc,
9944         &tegra_clk_m,
9945 };
9946
9947 static struct clk *tegra_ptr_clks[] = {
9948         &tegra_clk_m_div2,
9949         &tegra_clk_m_div4,
9950         &tegra_pll_ref,
9951         &tegra_pll_m,
9952         &tegra_pll_mb,
9953         &tegra_pll_c,
9954         &tegra_pll_c_out1,
9955         &tegra_pll_c2,
9956         &tegra_pll_c3,
9957         &tegra_pll_a1,
9958         &tegra_pll_p,
9959         &tegra_pll_p_out_adsp,
9960         &tegra_pll_p_out_cpu,
9961         &tegra_pll_p_out_hsio,
9962         &tegra_pll_p_out_xusb,
9963         &tegra_pll_p_out2,
9964         &tegra_pll_p_out3,
9965         &tegra_pll_p_out4,
9966         &tegra_pll_p_out5,
9967         &tegra_pll_p_out_sor,
9968         &tegra_pll_a,
9969         &tegra_pll_a_out_adsp,
9970         &tegra_pll_a_out0,
9971         &tegra_pll_a_out0_out_adsp,
9972         &tegra_pll_d,
9973         &tegra_pll_d_out0,
9974         &tegra_clk_xusb_gate,
9975         &tegra_pll_u_vco,
9976         &tegra_pll_u_out,
9977         &tegra_pll_u_out1,
9978         &tegra_pll_u_out2,
9979         &tegra_pll_u_480M,
9980         &tegra_pll_u_60M,
9981         &tegra_pll_u_48M,
9982         &tegra_pll_x,
9983         &tegra_dfll_cpu,
9984         &tegra_pll_d2,
9985         &tegra_pll_c4_vco,
9986         &tegra_pll_c4_out0,
9987         &tegra_pll_c4_out1,
9988         &tegra_pll_c4_out2,
9989         &tegra_pll_c4_out3,
9990         &tegra_pll_dp,
9991         &tegra_pll_re_vco,
9992         &tegra_pll_re_out,
9993         &tegra_pll_re_out1,
9994         &tegra_pll_e,
9995         &tegra_pll_e_gate,
9996         &tegra_cml0_clk,
9997         &tegra_cml1_clk,
9998         &tegra_pciex_clk,
9999         &tegra_pex_uphy_clk,
10000         &tegra_clk_cclk_g,
10001         &tegra_clk_cclk_lp,
10002         &tegra_clk_sclk_mux,
10003         &tegra_clk_sclk_div,
10004         &tegra_clk_sclk,
10005         &tegra_clk_hclk,
10006         &tegra_clk_pclk,
10007         &tegra_clk_aclk_adsp,
10008         &tegra_clk_virtual_adsp_bus,
10009         &tegra_clk_virtual_cpu_g,
10010         &tegra_clk_virtual_cpu_lp,
10011         &tegra_clk_cpu_cmplx,
10012         &tegra_clk_blink,
10013         &tegra_clk_cop,
10014         &tegra_clk_sbus_cmplx,
10015         &tegra_clk_ahb,
10016         &tegra_clk_apb,
10017         &tegra_clk_emc,
10018         &tegra_clk_mc,
10019         &tegra_clk_host1x,
10020         &tegra_clk_mselect,
10021         &tegra_clk_ape,
10022         &tegra_clk_c2bus,
10023         &tegra_clk_c3bus,
10024         &tegra_clk_gpu_gate,
10025         &tegra_clk_gpu_ref,
10026         &tegra_clk_gbus,
10027         &tegra_clk_isp,
10028         &tegra_clk_cbus,
10029         &tegra_clk_sor1_src,
10030         &tegra_clk_sor0_brick,
10031         &tegra_clk_sor1_brick,
10032         &tegra_clk_xusb_padctl,
10033         &tegra_clk_sata,
10034         &tegra_sata_aux_clk,
10035         &tegra_clk_sata_uphy,
10036         &tegra_clk_usb2_hsic_trk,
10037         &tegra_clk_hda2codec_2x,
10038 };
10039
10040 static struct clk *tegra_ptr_camera_mclks[] = {
10041         &tegra_camera_mclk,
10042         &tegra_camera_mclk2,
10043         &tegra_camera_mclk3,
10044         &tegra_camera_mipical,
10045 };
10046
10047 #ifdef CONFIG_DEBUG_FS
10048 static struct tegra_pto_table ptodefs[] = {
10049         { .name = "pll_c",  .divider = 2, .pto_id = 1,   .presel_reg = 0x088, .presel_value = BIT(3),  .presel_mask = BIT(3) },
10050         { .name = "pll_a1", .divider = 2, .pto_id = 85,  .presel_reg = 0x6a8, .presel_value = BIT(3),  .presel_mask = BIT(3) },
10051         { .name = "pll_c2", .divider = 2, .pto_id = 88,  .presel_reg = 0x4ec, .presel_value = BIT(3),  .presel_mask = BIT(3) },
10052         { .name = "pll_c3", .divider = 2, .pto_id = 89,  .presel_reg = 0x500, .presel_value = BIT(3),  .presel_mask = BIT(3) },
10053
10054         { .name = "pll_a",  .divider = 2, .pto_id = 4,   .presel_reg = 0x0bc, .presel_value = BIT(29), .presel_mask = BIT(29) },
10055         { .name = "pll_x",  .divider = 2, .pto_id = 5,   .presel_reg = 0x0e4, .presel_value = BIT(22), .presel_mask = BIT(22) },
10056
10057         { .name = "pll_d",  .divider = 2, .pto_id = 203, .presel_reg = 0x0dc, .presel_value = BIT(25), .presel_mask = BIT(25) },
10058         { .name = "pll_d2", .divider = 2, .pto_id = 205, .presel_reg = 0x4b8, .presel_value = BIT(16), .presel_mask = BIT(16) },
10059         { .name = "pll_dp", .divider = 2, .pto_id = 207, .presel_reg = 0x590, .presel_value = BIT(16), .presel_mask = BIT(16) },
10060         { .name = "pll_c4", .divider = 2, .pto_id = 81,  .presel_reg = 0x5a4, .presel_value = BIT(16), .presel_mask = BIT(16) },
10061
10062         { .name = "pll_m",  .divider = 2, .pto_id = 2,   .presel_reg = 0x9c,  .presel_value = BIT(8),  .presel_mask = BIT(8) },
10063         { .name = "pll_mb", .divider = 2, .pto_id = 37,  .presel_reg = 0x9c,  .presel_value = BIT(9),  .presel_mask = BIT(9) },
10064
10065         { .name = "pll_u", .divider = 2,  .pto_id = 269, .presel_reg = 0xcc,  .presel_value = BIT(27), .presel_mask = BIT(27) },
10066         { .name = "pll_re_vco", .divider = 2, .pto_id = 271, .presel_reg = 0x4c8, .presel_value = BIT(26), .presel_mask = BIT(26) },
10067
10068         { .name = "cpu",    .divider = 1,  .pto_id = 18, },
10069
10070         { .name = NULL, },
10071 };
10072 #endif
10073
10074 /*
10075  * Handle special clocks to check if they can be set to safe rate
10076  */
10077 static void check_isp_reset(void)
10078 {
10079         struct clk *isp = &tegra_clk_isp;
10080         struct clk *ispa = tegra_get_clock_by_name("ispa");
10081         struct clk *ispb = tegra_get_clock_by_name("ispb");
10082
10083         if (periph_clk_can_force_safe_rate(ispa) &&
10084             periph_clk_can_force_safe_rate(ispb))
10085                 tegra_periph_clk_safe_rate_init(isp);
10086 }
10087
10088 static void tegra21_periph_check_special_reset(void)
10089 {
10090         check_isp_reset();
10091 }
10092
10093 /* DFLL late init called with CPU clock lock taken */
10094 static void __init tegra21_dfll_cpu_late_init(struct clk *c)
10095 {
10096 #ifdef CONFIG_ARCH_TEGRA_HAS_CL_DVFS
10097         int ret;
10098         struct clk *cpu = &tegra_clk_virtual_cpu_g;
10099         unsigned long int dfll_boot_req_khz = tegra_dfll_boot_req_khz();
10100
10101         if (!cpu || !cpu->dvfs) {
10102                 pr_err("%s: CPU dvfs is not present\n", __func__);
10103                 return;
10104         }
10105         tegra_dvfs_set_dfll_tune_trimmers(cpu->dvfs, tune_cpu_trimmers);
10106
10107         /* release dfll clock source reset */
10108         tegra_periph_reset_deassert(c);
10109
10110         /*
10111          * Init DFLL control logic (cl_dvfs):
10112          * - if CPU is already set on DFLL by boot-loader, this call would sync
10113          *   cl_dvfs driver with cl_dvfs h/w state.
10114          * - if CPU booted on PLLX this call initializes cl_dvfs h/w, so DFLL
10115          *   can be used as CPU source.
10116          */
10117         ret = tegra_init_cl_dvfs();
10118
10119         /*
10120          * Boot on DFLL.
10121          * cl_dvfs device may not be present in DT for virtualized platform.
10122          */
10123         if (dfll_boot_req_khz) {
10124                 if (!ret || (ret == -ENODEV)) {
10125                         c->u.dfll.consumer = cpu;
10126                         c->state = ON;
10127                         use_dfll = DFLL_RANGE_ALL_RATES;
10128                         tegra_dvfs_set_dfll_range(cpu->dvfs, use_dfll);
10129
10130                         /* To read regulator (not cache), set volatile mode */
10131                         tegra_dvfs_rail_set_reg_volatile(tegra_cpu_rail, true);
10132
10133                         if (!ret)
10134                                 tegra_cl_dvfs_debug_init(c);
10135
10136                         pr_info("Tegra CPU DFLL booted with use_dfll = %d\n",
10137                                 use_dfll);
10138                         return;
10139                 }
10140                 pr_err("Tegra CPU DFLL failed to sync boot state\n");
10141                 return;
10142         }
10143
10144         /* Boot on PLLX */
10145         if (!ret) {
10146                 c->u.dfll.consumer = cpu;
10147                 c->state = OFF;
10148                 if (tegra_platform_is_silicon())
10149                         use_dfll = CONFIG_TEGRA_USE_DFLL_RANGE;
10150                 tegra_dvfs_set_dfll_range(cpu->dvfs, use_dfll);
10151                 tegra_cl_dvfs_debug_init(c);
10152                 pr_info("Tegra CPU DFLL is initialized with use_dfll = %d\n",
10153                         use_dfll);
10154         }
10155 #endif
10156 }
10157
10158 /*
10159  * Tegra21 clock policy: PLLC4 is a fixed frequency PLL with VCO output catered
10160  * to maximum eMMC rate. By default PLLC4 VCO is fixed at ~1GHz, that provides
10161  * target rates for DDR HS667 mode (PLLC4_OUT1 = VCO/3), DDR HS400 mode, and SDR
10162  * HS200 mode (PLLC4_OUT2 = VCO/5). For other possible eMMC rate targets VCO
10163  * rate is tabulated below.
10164  *
10165  * PLLC4 post-divider PLLC_OUT0 and secondary divider PLLC_OUT3 ratios specified
10166  * in the table are selected to provide max input frequency to downstream module
10167  * dividers (fort better granularity of possible rates).
10168  *
10169  * Commonly PLLC_OUT3 is also used as second clock source (along with PLLP) for
10170  * the system bus.
10171  */
10172 struct pllc4_sdmmc_map {
10173         unsigned long sdmmc_max_rate;
10174         unsigned long vco_rate;
10175         u32           out0_ratio;
10176         u32           out3_ratio;
10177         struct clk    *bus_source;
10178 };
10179
10180 static struct pllc4_sdmmc_map  tegra21_pllc4_sdmmc_map[] = {
10181         {         0, 1000000000, 1, 1, &tegra_pll_c4_out3 }, /* default cfg */
10182         { 266000000,  798000000, 4, 1, &tegra_pll_c4_out1 },
10183 };
10184
10185 static void pllc4_set_fixed_rates(unsigned long cf)
10186 {
10187         int i;
10188         u32 val;
10189         struct device_node *dn;
10190         unsigned long rate, sdmmc_max_rate = 0;
10191         struct pllc4_sdmmc_map *pllc4_cfg = &tegra21_pllc4_sdmmc_map[0];
10192
10193         for_each_compatible_node(dn, NULL, "nvidia,tegra210-sdhci") {
10194                 if (!of_device_is_available(dn))
10195                         continue;
10196
10197                 if (!of_property_read_u32(dn, "max-clk-limit", &val)
10198                     && (sdmmc_max_rate < val))
10199                         sdmmc_max_rate = val;
10200         }
10201
10202         if (sdmmc_max_rate) {
10203                 for (i = 1; i < ARRAY_SIZE(tegra21_pllc4_sdmmc_map); i++) {
10204                         struct pllc4_sdmmc_map *m = &tegra21_pllc4_sdmmc_map[i];
10205                         if (m->sdmmc_max_rate == sdmmc_max_rate) {
10206                                 pllc4_cfg = m;
10207                                 break;
10208                         }
10209                 }
10210         }
10211
10212         /* Align VCO rate on comparison frequency boundary */
10213         rate = (pllc4_cfg->vco_rate / cf) * cf;
10214         tegra_pll_c4_vco.u.pll.fixed_rate = rate;
10215         tegra_pll_c4_vco.flags |= PLL_FIXED;
10216
10217         rate = DIV_ROUND_UP(rate, pllc4_cfg->out0_ratio);
10218         tegra_pll_c4_out0.u.pll.fixed_rate = rate;
10219         tegra_pll_c4_out0.flags |= PLL_FIXED;
10220
10221         rate = DIV_ROUND_UP(rate, pllc4_cfg->out3_ratio);
10222         tegra_pll_c4_out3.u.pll_div.default_rate = rate;
10223
10224         tegra_clk_sbus_cmplx.u.system.sclk_high = pllc4_cfg->bus_source;
10225
10226         pr_info("pll_c4 rates match %lu max sdmmc: vco=%lu out0=%lu out3=%lu\n",
10227                 sdmmc_max_rate,
10228                 tegra_pll_c4_vco.u.pll.fixed_rate,
10229                 tegra_pll_c4_out0.u.pll.fixed_rate,
10230                 tegra_pll_c4_out3.u.pll_div.default_rate);
10231 }
10232
10233 /*
10234  * Backup pll is used as transitional CPU clock source while main pll is
10235  * relocking; in addition all CPU rates below backup level are sourced from
10236  * backup pll only. Target backup levels for each CPU mode are selected high
10237  * enough to avoid voltage droop when CPU clock is switched between backup and
10238  * main plls. Actual backup rates will be rounded to match backup source fixed
10239  * frequency. Backup rates are also used as stay-on-backup thresholds, and must
10240  * be kept the same in G and LP mode (will need to add a separate stay-on-backup
10241  * parameter to allow different backup rates if necessary).
10242  *
10243  * Sbus threshold must be exact factor of pll_p rate.
10244  */
10245 #define CPU_G_BACKUP_RATE_TARGET        200000000
10246 #define CPU_LP_BACKUP_RATE_TARGET       200000000
10247
10248 static void tegra21_pllp_init_dependencies(unsigned long pllp_rate)
10249 {
10250         u32 div;
10251         unsigned long backup_rate;
10252         unsigned long adsp_backup_rate;
10253
10254         switch (pllp_rate) {
10255         case 216000000:
10256                 tegra_pll_p_out3.u.pll_div.default_rate = 72000000;
10257                 tegra_pll_p_out4.u.pll_div.default_rate = 108000000;
10258                 tegra_pll_p_out5.u.pll_div.default_rate = 108000000;
10259                 tegra_clk_sbus_cmplx.u.system.threshold = 108000000;
10260                 tegra_clk_host1x.u.periph.threshold = 108000000;
10261                 adsp_backup_rate = 108000000;
10262                 break;
10263         case 408000000:
10264                 tegra_pll_p_out3.u.pll_div.default_rate = 102000000;
10265                 tegra_pll_p_out4.u.pll_div.default_rate = 204000000;
10266                 tegra_pll_p_out5.u.pll_div.default_rate = 204000000;
10267                 tegra_clk_sbus_cmplx.u.system.threshold = 204000000;
10268                 tegra_clk_host1x.u.periph.threshold = 204000000;
10269                 adsp_backup_rate = 136000000;
10270                 break;
10271         case 204000000:
10272                 tegra_pll_p_out3.u.pll_div.default_rate = 102000000;
10273                 tegra_pll_p_out4.u.pll_div.default_rate = 204000000;
10274                 tegra_pll_p_out5.u.pll_div.default_rate = 204000000;
10275                 tegra_clk_sbus_cmplx.u.system.threshold = 204000000;
10276                 tegra_clk_host1x.u.periph.threshold = 204000000;
10277                 adsp_backup_rate = 102000000;
10278                 break;
10279         default:
10280                 pr_err("tegra: PLLP rate: %lu is not supported\n", pllp_rate);
10281                 BUG();
10282         }
10283         pr_info("tegra: PLLP fixed rate: %lu\n", pllp_rate);
10284
10285         div = pllp_rate / CPU_G_BACKUP_RATE_TARGET;
10286         backup_rate = pllp_rate / div;
10287         tegra_clk_virtual_cpu_g.u.cpu.backup_rate = backup_rate;
10288
10289         div = pllp_rate / CPU_LP_BACKUP_RATE_TARGET;
10290         backup_rate = pllp_rate / div;
10291         tegra_clk_virtual_cpu_lp.u.cpu.backup_rate = backup_rate;
10292
10293         div = pllp_rate / adsp_backup_rate;
10294         tegra_clk_aclk_adsp.u.cclk.div71 = 2 * div - 2; /* reg settings */
10295         backup_rate = pllp_rate / div;
10296         tegra_clk_virtual_adsp_bus.u.cpu.backup_rate = adsp_backup_rate;
10297 }
10298
10299 static void tegra21_init_one_clock(struct clk *c)
10300 {
10301         clk_init(c);
10302         INIT_LIST_HEAD(&c->shared_bus_list);
10303         if (!c->lookup.dev_id && !c->lookup.con_id)
10304                 c->lookup.con_id = c->name;
10305         c->lookup.clk = c;
10306         clkdev_add(&c->lookup);
10307
10308         /* Sanity check for clock IDs within CLK_OUT_ENB_NUM */
10309         if (c->clk_id && (c->clk_id < CLK_OUT_ENB_NUM * 32) &&
10310             (c->clk_id != c->u.periph.clk_num))
10311                 WARN(1, "%s clock id %u does not match enable bit %u\n",
10312                      c->name, c->clk_id, c->u.periph.clk_num);
10313 }
10314
10315 void tegra_edp_throttle_cpu_now(u8 factor)
10316 {
10317         /* empty definition for tegra21 */
10318         return;
10319 }
10320
10321 bool tegra_clk_is_parent_allowed(struct clk *c, struct clk *p)
10322 {
10323         /*
10324          * Most of the Tegra21 multimedia and peripheral muxes include pll_c2
10325          * and pll_c3 as possible inputs. However, per clock policy these plls
10326          * are allowed to be used only by handful devices aggregated on cbus.
10327          * For all others, instead of enforcing policy at run-time in this
10328          * function, we simply stripped out pll_c2 and pll_c3 options from the
10329          * respective muxes statically. Similarly pll_a1 is removed from the
10330          * set of possible parents of non-cbus  modules.
10331          *
10332          * Traditionally ubiquitous on tegra chips pll_c is allowed to be used
10333          * as parent for cbus modules only as well, but it is not stripped out
10334          * from muxes statically, and the respective policy is enforced by this
10335          * function.
10336          */
10337         if ((p == &tegra_pll_c) && (c != &tegra_pll_c_out1))
10338                 return c->flags & PERIPH_ON_CBUS;
10339
10340         /*
10341          * On Tegra21 both PLLM and PLLMB are statically connected to EMC mux
10342          * only. However, PLLMB allowed to be used only if PLLM is in default
10343          * configuration (i.e., all overrides are disabled).
10344          */
10345         if (p == &tegra_pll_mb)
10346                 return tegra_pll_m.u.pll.defaults_set;
10347
10348         return true;
10349 }
10350
10351 /* Internal LA may request some clocks to be enabled on init via TRANSACTION
10352    SCRATCH register settings */
10353 static void __init tegra21x_clk_init_la(void)
10354 {
10355         struct clk *c;
10356         u32 reg = readl((void *)
10357                 ((uintptr_t)misc_gp_base + MISC_GP_TRANSACTOR_SCRATCH_0));
10358
10359         if (!(reg & MISC_GP_TRANSACTOR_SCRATCH_LA_ENABLE))
10360                 return;
10361
10362         c = tegra_get_clock_by_name("la");
10363         if (WARN(!c, "%s: could not find la clk\n", __func__))
10364                 return;
10365         tegra_clk_prepare_enable(c);
10366
10367         if (reg & MISC_GP_TRANSACTOR_SCRATCH_DP2_ENABLE) {
10368                 c = tegra_get_clock_by_name("dp2");
10369                 if (WARN(!c, "%s: could not find dp2 clk\n", __func__))
10370                         return;
10371                 tegra_clk_prepare_enable(c);
10372         }
10373 }
10374
10375 #ifdef CONFIG_PM_SLEEP
10376 static u32 clk_rst_suspend[RST_DEVICES_NUM + CLK_OUT_ENB_NUM +
10377                            PERIPH_CLK_SOURCE_NUM + 25];
10378
10379 static int tegra21_clk_suspend(void)
10380 {
10381         unsigned long off;
10382         u32 *ctx = clk_rst_suspend;
10383
10384         *ctx++ = clk_readl(OSC_CTRL) & OSC_CTRL_OSC_FREQ_MASK;
10385         *ctx++ = clk_readl(CPU_SOFTRST_CTRL);
10386         *ctx++ = clk_readl(CPU_SOFTRST_CTRL1);
10387         *ctx++ = clk_readl(CPU_SOFTRST_CTRL2);
10388
10389         *ctx++ = clk_readl(SPARE_REG);
10390         *ctx++ = clk_readl(MISC_CLK_ENB);
10391         *ctx++ = clk_readl(CLK_MASK_ARM);
10392
10393         *ctx++ = clk_readl(tegra_pll_p_out3.reg);
10394         *ctx++ = clk_readl(tegra_pll_p_out5.reg);
10395         *ctx++ = clk_readl(tegra_pll_p_out_hsio.reg);
10396
10397         *ctx++ = clk_readl(tegra_pll_u_out1.reg);
10398         *ctx++ = clk_readl(tegra_pll_u_vco.reg);
10399
10400         *ctx++ = clk_readl(tegra_pll_a_out0.reg);
10401         *ctx++ = clk_readl(tegra_pll_c_out1.reg);
10402         *ctx++ = clk_readl(tegra_pll_c4_out3.reg);
10403         *ctx++ = clk_readl(tegra_pll_re_out1.reg);
10404
10405         *ctx++ = clk_readl(tegra_clk_sclk.reg);
10406         *ctx++ = clk_readl(tegra_clk_sclk_div.reg);
10407         *ctx++ = clk_readl(tegra_clk_sclk_mux.reg);
10408         *ctx++ = clk_readl(tegra_clk_pclk.reg);
10409
10410         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_LA;
10411                         off += 4) {
10412                 if (off == PERIPH_CLK_SOURCE_EMC)
10413                         continue;
10414                 *ctx++ = clk_readl(off);
10415         }
10416         for (off = PERIPH_CLK_SOURCE_MSELECT; off <= PERIPH_CLK_SOURCE_SE;
10417                         off+=4) {
10418                 *ctx++ = clk_readl(off);
10419         }
10420         for (off = AUDIO_DLY_CLK; off <= AUDIO_SYNC_CLK_SPDIF; off+=4) {
10421                 *ctx++ = clk_readl(off);
10422         }
10423         for (off = AUDIO_SYNC_CLK_DMIC1;
10424               off <= AUDIO_SYNC_CLK_DMIC2; off += 4) {
10425                 *ctx++ = clk_readl(off);
10426         }
10427         for (off = PERIPH_CLK_SOURCE_XUSB_HOST;
10428                 off <= PERIPH_CLK_SOURCE_VIC; off += 4) {
10429                 if (off == PERIPH_CLK_SOURCE_EMC_DLL)
10430                         continue;
10431                 *ctx++ = clk_readl(off);
10432         }
10433         for (off = PERIPH_CLK_SOURCE_SDMMC_LEGACY;
10434               off <= PERIPH_CLK_SOURCE_NVENC; off += 4) {
10435                 *ctx++ = clk_readl(off);
10436         }
10437         for (off = AUDIO_SYNC_CLK_DMIC3;
10438               off <= PERIPH_CLK_SOURCE_DBGAPB; off += 4) {
10439                 *ctx++ = clk_readl(off);
10440         }
10441
10442         *ctx++ = clk_readl(RST_DEVICES_L);
10443         *ctx++ = clk_readl(RST_DEVICES_H);
10444         *ctx++ = clk_readl(RST_DEVICES_U);
10445         *ctx++ = clk_readl(RST_DEVICES_V);
10446         *ctx++ = clk_readl(RST_DEVICES_W);
10447         *ctx++ = clk_readl(RST_DEVICES_X);
10448         *ctx++ = clk_readl(RST_DEVICES_Y);
10449
10450         *ctx++ = clk_readl(CLK_OUT_ENB_L);
10451         *ctx++ = clk_readl(CLK_OUT_ENB_H);
10452         *ctx++ = clk_readl(CLK_OUT_ENB_U);
10453         *ctx++ = clk_readl(CLK_OUT_ENB_V);
10454         *ctx++ = clk_readl(CLK_OUT_ENB_W);
10455         *ctx++ = clk_readl(CLK_OUT_ENB_X);
10456         *ctx++ = clk_readl(CLK_OUT_ENB_Y);
10457
10458         *ctx++ = clk_readl(tegra_clk_cclk_g.reg);
10459         *ctx++ = clk_readl(tegra_clk_cclk_g.reg + SUPER_CLK_DIVIDER);
10460
10461         *ctx++ = clk_readl(tegra_clk_cclk_lp.reg);
10462         *ctx++ = clk_readl(tegra_clk_cclk_lp.reg + SUPER_CLK_DIVIDER);
10463
10464         *ctx = clk_get_rate_all_locked(&tegra_clk_emc);
10465         tegra21_emc_clk_suspend(&tegra_clk_emc, *ctx++);
10466
10467         pr_debug("%s: suspend entries: %d, suspend array: %u\n", __func__,
10468                 (s32)(ctx - clk_rst_suspend), (u32)ARRAY_SIZE(clk_rst_suspend));
10469         BUG_ON((ctx - clk_rst_suspend) > ARRAY_SIZE(clk_rst_suspend));
10470         return 0;
10471 }
10472
10473 static void tegra21_clk_resume(void)
10474 {
10475         unsigned long off, rate;
10476         const u32 *ctx = clk_rst_suspend;
10477         u32 val, clk_y, pll_u_mask, pll_u_base, pll_u_out12;
10478         u32 pll_p_out_hsio, pll_p_out_mask, pll_p_out34;
10479         u32 pll_a_out0, pll_c_out1, pll_c4_out3, pll_re_out1;
10480         struct clk *p;
10481
10482         pll_p_out_mask = PLLP_MISC1_HSIO_EN | PLLP_MISC1_XUSB_EN;
10483         pll_u_mask = (1 << tegra_pll_u_480M.reg_shift) |
10484                 (1 << tegra_pll_u_60M.reg_shift) |
10485                 (1 << tegra_pll_u_48M.reg_shift);
10486
10487         /*
10488          * OSC frequency selection should be already restored by warm boot
10489          * code, but just in case make sure it does not change across suspend.
10490          */
10491         val = clk_readl(OSC_CTRL) & ~OSC_CTRL_OSC_FREQ_MASK;
10492         val |= *ctx++;
10493         clk_writel(val, OSC_CTRL);
10494         clk_writel(*ctx++, CPU_SOFTRST_CTRL);
10495         clk_writel(*ctx++, CPU_SOFTRST_CTRL1);
10496         clk_writel(*ctx++, CPU_SOFTRST_CTRL2);
10497
10498         clk_writel(*ctx++, SPARE_REG);
10499         clk_writel(*ctx++, MISC_CLK_ENB);
10500         clk_writel(*ctx++, CLK_MASK_ARM);
10501
10502         /* Since we are going to reset devices and switch clock sources in this
10503          * function, plls and secondary dividers is required to be enabled. The
10504          * actual value will be restored back later. Note that boot plls: pllm,
10505          * and pllp, are already configured and enabled
10506          */
10507         tegra_pllp_out_resume_enable(&tegra_pll_p_out2); /* expected: NOP */
10508         val = PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
10509         val |= val << 16;
10510         pll_p_out34 = *ctx++;
10511         clk_writel(pll_p_out34 | val, tegra_pll_p_out3.reg);
10512
10513         /* Restore as is, GPU is rail-gated, anyway */
10514         clk_writel(*ctx++, tegra_pll_p_out5.reg);
10515
10516         pll_p_out_hsio = *ctx++;
10517         val = clk_readl(tegra_pll_p_out_hsio.reg) | pll_p_out_mask;
10518         clk_writel(val, tegra_pll_p_out_hsio.reg);
10519
10520         tegra_pllu_out_resume_enable(&tegra_pll_u_out);
10521         val = PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
10522         pll_u_out12 = *ctx++;
10523         clk_writel(pll_u_out12 | val, tegra_pll_u_out1.reg);
10524
10525         pll_u_base = *ctx++;
10526         val = clk_readl(tegra_pll_u_vco.reg) | pll_u_mask;
10527         clk_writel(val, tegra_pll_u_vco.reg);
10528
10529         tegra_pll_clk_resume_enable(&tegra_pll_c);
10530         tegra_pll_clk_resume_enable(&tegra_pll_c2);
10531         tegra_pll_clk_resume_enable(&tegra_pll_c3);
10532         tegra_pll_clk_resume_enable(&tegra_pll_a1);
10533         tegra_pll_clk_resume_enable(&tegra_pll_a);
10534         tegra_pll_clk_resume_enable(&tegra_pll_d);
10535         tegra_pll_clk_resume_enable(&tegra_pll_d2);
10536         tegra_pll_clk_resume_enable(&tegra_pll_dp);
10537         tegra_pll_clk_resume_enable(&tegra_pll_x);
10538         tegra_pll_out_resume_enable(&tegra_pll_c4_out0);
10539         tegra_pll_out_resume_enable(&tegra_pll_re_out);
10540
10541         udelay(1000);
10542
10543         val = PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
10544         pll_a_out0 = *ctx++;
10545         clk_writel(pll_a_out0 | val, tegra_pll_a_out0.reg);
10546         pll_c_out1 = *ctx++;
10547         clk_writel(pll_c_out1 | val, tegra_pll_c_out1.reg);
10548         pll_c4_out3 = *ctx++;
10549         clk_writel(pll_c4_out3 | val, tegra_pll_c4_out3.reg);
10550         pll_re_out1 = *ctx++;
10551         clk_writel(pll_re_out1 | val, tegra_pll_re_out1.reg);
10552
10553         /*
10554          * To make sure system bus is not over-clocked during restoration:
10555          * - use fixed safe 1:1:2 SCLK:HCLK:PCLK ratio until SCLK source is set
10556          * - defer SCLK divider restoration after SCLK source is restored if
10557          *   divider set by boot-rom is above saved value.
10558          */
10559         clk_writel(0x01, tegra_clk_pclk.reg);
10560         clk_writel_delay(*ctx++, tegra_clk_sclk.reg);
10561         val = *ctx++;
10562         if (val > clk_readl(tegra_clk_sclk_div.reg))
10563                 clk_writel_delay(val, tegra_clk_sclk_div.reg);
10564         clk_writel_delay(*ctx++, tegra_clk_sclk_mux.reg);
10565         if (val != clk_readl(tegra_clk_sclk_div.reg))
10566                 clk_writel_delay(val, tegra_clk_sclk_div.reg);
10567         clk_writel(*ctx++, tegra_clk_pclk.reg);
10568
10569         /* enable all clocks before configuring clock sources */
10570         clk_writel(CLK_OUT_ENB_L_RESET_MASK, CLK_OUT_ENB_L);
10571         clk_writel(CLK_OUT_ENB_H_RESET_MASK, CLK_OUT_ENB_H);
10572         clk_writel(CLK_OUT_ENB_U_RESET_MASK, CLK_OUT_ENB_U);
10573         clk_writel(CLK_OUT_ENB_V_RESET_MASK, CLK_OUT_ENB_V);
10574         clk_writel(CLK_OUT_ENB_W_RESET_MASK, CLK_OUT_ENB_W);
10575         clk_writel(CLK_OUT_ENB_X_RESET_MASK, CLK_OUT_ENB_X);
10576         clk_writel_delay(CLK_OUT_ENB_Y_RESET_MASK, CLK_OUT_ENB_Y);
10577
10578         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_LA;
10579                         off += 4) {
10580                 if (off == PERIPH_CLK_SOURCE_EMC)
10581                         continue;
10582                 clk_writel(*ctx++, off);
10583         }
10584         for (off = PERIPH_CLK_SOURCE_MSELECT; off <= PERIPH_CLK_SOURCE_SE;
10585                         off += 4) {
10586                 clk_writel(*ctx++, off);
10587         }
10588         for (off = AUDIO_DLY_CLK; off <= AUDIO_SYNC_CLK_SPDIF; off+=4) {
10589                 clk_writel(*ctx++, off);
10590         }
10591         for (off = AUDIO_SYNC_CLK_DMIC1;
10592               off <= AUDIO_SYNC_CLK_DMIC2; off += 4) {
10593                 clk_writel(*ctx++, off);
10594         }
10595         for (off = PERIPH_CLK_SOURCE_XUSB_HOST;
10596                 off <= PERIPH_CLK_SOURCE_VIC; off += 4) {
10597                 if (off == PERIPH_CLK_SOURCE_EMC_DLL)
10598                         continue;
10599                 clk_writel(*ctx++, off);
10600         }
10601         for (off = PERIPH_CLK_SOURCE_SDMMC_LEGACY;
10602               off <= PERIPH_CLK_SOURCE_NVENC; off += 4) {
10603                 clk_writel(*ctx++, off);
10604         }
10605         for (off = AUDIO_SYNC_CLK_DMIC3;
10606               off <= PERIPH_CLK_SOURCE_DBGAPB; off += 4) {
10607                 clk_writel(*ctx++, off);
10608         }
10609
10610         clk_readl(off);
10611         udelay(RESET_PROPAGATION_DELAY);
10612
10613         clk_writel(*ctx++, RST_DEVICES_L);
10614         clk_writel(*ctx++, RST_DEVICES_H);
10615         clk_writel(*ctx++, RST_DEVICES_U);
10616         clk_writel(*ctx++, RST_DEVICES_V);
10617         clk_writel(*ctx++, RST_DEVICES_W);
10618         clk_writel(*ctx++, RST_DEVICES_X);
10619         clk_writel_delay(*ctx++, RST_DEVICES_Y);
10620
10621         clk_writel(*ctx++, CLK_OUT_ENB_L);
10622         clk_writel(*ctx++, CLK_OUT_ENB_H);
10623         clk_writel(*ctx++, CLK_OUT_ENB_U);
10624         clk_writel(*ctx++, CLK_OUT_ENB_V);
10625         clk_writel(*ctx++, CLK_OUT_ENB_W);
10626         clk_writel(*ctx++, CLK_OUT_ENB_X);
10627
10628         /* Keep pllp_out_cpu enabled */
10629         clk_y = *ctx++;
10630         val = 1 << (tegra_pll_p_out_cpu.u.periph.clk_num % 32);
10631         clk_writel_delay(clk_y | val, CLK_OUT_ENB_Y);
10632
10633         /* DFLL resume after cl_dvfs and i2c5 clocks are resumed */
10634         tegra21_dfll_clk_resume(&tegra_dfll_cpu);
10635
10636         /* CPU G clock restored after DFLL and PLLs */
10637         clk_writel(*ctx++, tegra_clk_cclk_g.reg);
10638         clk_writel(*ctx++, tegra_clk_cclk_g.reg + SUPER_CLK_DIVIDER);
10639
10640         /* CPU LP clock restored after PLLs and pllp_out_cpu branch */
10641         clk_writel(*ctx++, tegra_clk_cclk_lp.reg);
10642         clk_writel_delay(*ctx++, tegra_clk_cclk_lp.reg + SUPER_CLK_DIVIDER);
10643
10644         /* Restore back the actual pll and secondary divider values */
10645         clk_writel(pll_p_out34, tegra_pll_p_out3.reg);
10646         val = clk_readl(tegra_pll_p_out_hsio.reg) & (~pll_p_out_mask);
10647         val |= pll_p_out_hsio & pll_p_out_mask;
10648         clk_writel(val, tegra_pll_p_out_hsio.reg);
10649
10650         clk_writel(pll_u_out12, tegra_pll_u_out1.reg);
10651         val = clk_readl(tegra_pll_u_vco.reg) & (~pll_u_mask);
10652         clk_writel(val | (pll_u_base & pll_u_mask), tegra_pll_u_vco.reg);
10653
10654         clk_writel_delay(clk_y, CLK_OUT_ENB_Y);
10655
10656         p = &tegra_pll_c;
10657         if (p->state == OFF)
10658                 p->ops->disable(p);
10659         p = &tegra_pll_c2;
10660         if (p->state == OFF)
10661                 p->ops->disable(p);
10662         p = &tegra_pll_c3;
10663         if (p->state == OFF)
10664                 p->ops->disable(p);
10665         p = &tegra_pll_a1;
10666         if (p->state == OFF)
10667                 p->ops->disable(p);
10668         p = &tegra_pll_a;
10669         if (p->state == OFF)
10670                 p->ops->disable(p);
10671         p = &tegra_pll_d;
10672         if (p->state == OFF)
10673                 p->ops->disable(p);
10674         p = &tegra_pll_d2;
10675         if (p->state == OFF)
10676                 p->ops->disable(p);
10677         p = &tegra_pll_dp;
10678         if (p->state == OFF)
10679                 p->ops->disable(p);
10680         p = &tegra_pll_x;
10681         if (p->state == OFF)
10682                 p->ops->disable(p);
10683         p = &tegra_pll_c4_vco;
10684         if (p->state == OFF)
10685                 p->ops->disable(p);
10686         p = &tegra_pll_re_vco;
10687         if (p->state == OFF)
10688                 p->ops->disable(p);
10689
10690         clk_writel(pll_a_out0, tegra_pll_a_out0.reg);
10691         clk_writel(pll_c_out1, tegra_pll_c_out1.reg);
10692         clk_writel(pll_c4_out3, tegra_pll_c4_out3.reg);
10693         clk_writel(pll_re_out1, tegra_pll_re_out1.reg);
10694
10695         /* Since EMC clock is not restored, and may not preserve parent across
10696            suspend, update current state, and mark EMC DFS as out of sync */
10697         p = tegra_clk_emc.parent;
10698         tegra21_periph_clk_init(&tegra_clk_emc);
10699
10700         /* Turn Off pll_m if it was OFF before suspend, and emc was not switched
10701            to pll_m across suspend; re-init pll_m to sync s/w and h/w states */
10702         if ((tegra_pll_m.state == OFF) &&
10703             (&tegra_pll_m != tegra_clk_emc.parent))
10704                 tegra21_pllm_clk_disable(&tegra_pll_m);
10705         tegra21_pllm_clk_init(&tegra_pll_m);
10706
10707         /* Resume pll_mb (FIXME: don't need to enable/disable) */
10708         tegra_pll_clk_resume_enable(&tegra_pll_mb);
10709         if (tegra_pll_mb.state == OFF)
10710                 tegra_pll_mb.ops->disable(&tegra_pll_mb);
10711
10712         if (p != tegra_clk_emc.parent) {
10713                 pr_debug("EMC parent(refcount) across suspend: %s(%d) : %s(%d)",
10714                         p->name, p->refcnt, tegra_clk_emc.parent->name,
10715                         tegra_clk_emc.parent->refcnt);
10716
10717                 /* emc switched to the new parent by low level code, but ref
10718                    count and s/w state need to be updated */
10719                 clk_disable_locked(p);
10720                 clk_enable_locked(tegra_clk_emc.parent);
10721         }
10722
10723         /* update DVFS voltage requirements */
10724         rate = clk_get_rate_all_locked(&tegra_clk_emc);
10725         if (*ctx != rate) {
10726                 tegra_dvfs_set_rate(&tegra_clk_emc, rate);
10727                 if (p == tegra_clk_emc.parent) {
10728                         rate = clk_get_rate_all_locked(p);
10729                         tegra_dvfs_set_rate(p, rate);
10730                 }
10731         }
10732         tegra_emc_timing_invalidate();
10733
10734         tegra21_pllu_hw_ctrl_set(&tegra_pll_u_vco); /* PLLU, UTMIP h/w ctrl */
10735 }
10736
10737 static struct syscore_ops tegra_clk_syscore_ops = {
10738         .suspend = tegra21_clk_suspend,
10739         .resume = tegra21_clk_resume,
10740         .save = tegra21_clk_suspend,
10741         .restore = tegra21_clk_resume,
10742 };
10743 #endif
10744
10745 static void tegra21_init_xusb_clocks(void)
10746 {
10747         int i;
10748
10749         for (i = 0; i < ARRAY_SIZE(tegra_xusb_source_clks); i++)
10750                 tegra21_init_one_clock(&tegra_xusb_source_clks[i]);
10751
10752         tegra21_init_one_clock(&tegra_xusb_ss_div2);
10753         tegra21_init_one_clock(&tegra_xusb_hs_src);
10754         tegra21_init_one_clock(&tegra_xusb_ssp_src);
10755
10756         for (i = 0; i < ARRAY_SIZE(tegra_xusb_coupled_clks); i++)
10757                 tegra21_init_one_clock(&tegra_xusb_coupled_clks[i]);
10758 }
10759
10760 /*
10761  * Check if arch timer frequency specified either in DT or in CNTFRQ register is
10762  * matching actual clk_m rate.
10763  */
10764 static void tegra21_check_timer_clock(u32 clk_m_rate)
10765 {
10766
10767         struct device_node *dn;
10768         u32 dt_rate = 0;
10769         u32 timer_rate = 0;
10770
10771         for_each_compatible_node(dn, NULL, "arm,armv8-timer") {
10772                 if (!of_device_is_available(dn))
10773                         continue;
10774
10775                 if (!of_property_read_u32(dn, "clock-frequency", &dt_rate))
10776                         timer_rate = dt_rate;
10777                 break;
10778         }
10779
10780         if (!dn) {
10781                 WARN(1, "No arch timer node in DT\n");
10782                 return;
10783         }
10784
10785         if (!timer_rate)
10786                 timer_rate = arch_timer_get_cntfrq();
10787
10788         if (timer_rate == clk_m_rate)
10789                 return;
10790
10791         WARN(1, "Arch timer %s rate %u doesn't match clk_m %u - kernel timing is broken\n",
10792                 dt_rate ? "DT" : "CNTFRQ", timer_rate, clk_m_rate);
10793 }
10794
10795 /*
10796  * The udelay() is implemented based on arch timers, using loops_per_jiffy as
10797  * scaling factor. To make it functional during early clock initialization -
10798  *  before timers are initialized - set loops_per_jiffy here.
10799  */
10800 static void tegra21_init_main_clock(void)
10801 {
10802         int i;
10803         unsigned long clk_m_rate;
10804
10805         for (i = 0; i < ARRAY_SIZE(tegra_main_clks); i++)
10806                 tegra21_init_one_clock(tegra_main_clks[i]);
10807
10808         clk_m_rate = clk_get_rate_all_locked(&tegra_clk_m);
10809
10810         loops_per_jiffy = clk_m_rate / HZ;
10811
10812         tegra21_check_timer_clock(clk_m_rate);
10813 }
10814
10815 void __init tegra21x_init_clocks(void)
10816 {
10817         int i;
10818         struct clk *c;
10819
10820 #ifndef CONFIG_TEGRA_DUAL_CBUS
10821         BUILD_BUG()
10822 #endif
10823         tegra21_init_main_clock();
10824
10825         for (i = 0; i < ARRAY_SIZE(tegra_ptr_clks); i++)
10826                 tegra21_init_one_clock(tegra_ptr_clks[i]);
10827
10828         for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++)
10829                 tegra21_init_one_clock(&tegra_list_clks[i]);
10830
10831         for (i = 0; i < ARRAY_SIZE(tegra_visp_clks); i++)
10832                 tegra21_init_one_clock(&tegra_visp_clks[i]);
10833
10834         for (i = 0; i < ARRAY_SIZE(tegra_ptr_camera_mclks); i++)
10835                 tegra21_init_one_clock(tegra_ptr_camera_mclks[i]);
10836
10837         for (i = 0; i < ARRAY_SIZE(tegra_sync_source_list); i++)
10838                 tegra21_init_one_clock(&tegra_sync_source_list[i]);
10839         for (i = 0; i < ARRAY_SIZE(tegra_clk_audio_list); i++)
10840                 tegra21_init_one_clock(&tegra_clk_audio_list[i]);
10841         for (i = 0; i < ARRAY_SIZE(tegra_clk_audio_2x_list); i++)
10842                 tegra21_init_one_clock(&tegra_clk_audio_2x_list[i]);
10843         for (i = 0; i < ARRAY_SIZE(tegra_clk_audio_dmic_list); i++)
10844                 tegra21_init_one_clock(&tegra_clk_audio_dmic_list[i]);
10845
10846         init_clk_out_mux();
10847         for (i = 0; i < ARRAY_SIZE(tegra_clk_out_list); i++)
10848                 tegra21_init_one_clock(&tegra_clk_out_list[i]);
10849
10850         tegra21_init_xusb_clocks();
10851
10852         for (i = 0; i < ARRAY_SIZE(tegra_slcg_clks); i++)
10853                 tegra21_init_one_clock(&tegra_slcg_clks[i]);
10854
10855         for (i = 0; i < ARRAY_SIZE(tegra_clk_duplicates); i++) {
10856                 c = tegra_get_clock_by_name(tegra_clk_duplicates[i].name);
10857                 if (!c) {
10858                         pr_err("%s: Unknown duplicate clock %s\n", __func__,
10859                                 tegra_clk_duplicates[i].name);
10860                         continue;
10861                 }
10862
10863                 tegra_clk_duplicates[i].lookup.clk = c;
10864                 clkdev_add(&tegra_clk_duplicates[i].lookup);
10865         }
10866
10867         /* Check/apply safe rate to clocks with reset dependency on others */
10868         tegra21_periph_check_special_reset();
10869
10870         /* Make sure at list one of dual PLLM/PLLMB is disabled */
10871         tegra21_emc_sync_plls(&tegra_clk_emc, &tegra_pll_m, &tegra_pll_mb);
10872
10873         /* Tegra21 allows to change dividers of disabled clocks */
10874         tegra_clk_set_disabled_div_all();
10875
10876 #ifdef CONFIG_PM_SLEEP
10877         register_syscore_ops(&tegra_clk_syscore_ops);
10878 #endif
10879
10880 #ifdef CONFIG_DEBUG_FS
10881         tegra_clk_add_pto_entries(ptodefs);
10882 #endif
10883 }
10884
10885 static int __init tegra21x_clk_late_init(void)
10886 {
10887         tegra_clk_disable_unprepare(&tegra_pll_re_vco);
10888         tegra_clk_disable_unprepare(tegra_get_clock_by_name("boot.apb.sclk"));
10889         return 0;
10890 }
10891 late_initcall(tegra21x_clk_late_init);