xref: /openbmc/linux/drivers/clk/tegra/clk-tegra20.c (revision b58c6630)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
4  */
5 
6 #include <linux/io.h>
7 #include <linux/clk-provider.h>
8 #include <linux/clkdev.h>
9 #include <linux/of.h>
10 #include <linux/of_address.h>
11 #include <linux/clk/tegra.h>
12 #include <linux/delay.h>
13 #include <dt-bindings/clock/tegra20-car.h>
14 
15 #include "clk.h"
16 #include "clk-id.h"
17 
18 #define MISC_CLK_ENB 0x48
19 
20 #define OSC_CTRL 0x50
21 #define OSC_CTRL_OSC_FREQ_MASK (3<<30)
22 #define OSC_CTRL_OSC_FREQ_13MHZ (0<<30)
23 #define OSC_CTRL_OSC_FREQ_19_2MHZ (1<<30)
24 #define OSC_CTRL_OSC_FREQ_12MHZ (2<<30)
25 #define OSC_CTRL_OSC_FREQ_26MHZ (3<<30)
26 #define OSC_CTRL_MASK (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
27 
28 #define OSC_CTRL_PLL_REF_DIV_MASK (3<<28)
29 #define OSC_CTRL_PLL_REF_DIV_1		(0<<28)
30 #define OSC_CTRL_PLL_REF_DIV_2		(1<<28)
31 #define OSC_CTRL_PLL_REF_DIV_4		(2<<28)
32 
33 #define OSC_FREQ_DET 0x58
34 #define OSC_FREQ_DET_TRIG (1<<31)
35 
36 #define OSC_FREQ_DET_STATUS 0x5c
37 #define OSC_FREQ_DET_BUSY (1<<31)
38 #define OSC_FREQ_DET_CNT_MASK 0xFFFF
39 
40 #define TEGRA20_CLK_PERIPH_BANKS	3
41 
42 #define PLLS_BASE 0xf0
43 #define PLLS_MISC 0xf4
44 #define PLLC_BASE 0x80
45 #define PLLC_MISC 0x8c
46 #define PLLM_BASE 0x90
47 #define PLLM_MISC 0x9c
48 #define PLLP_BASE 0xa0
49 #define PLLP_MISC 0xac
50 #define PLLA_BASE 0xb0
51 #define PLLA_MISC 0xbc
52 #define PLLU_BASE 0xc0
53 #define PLLU_MISC 0xcc
54 #define PLLD_BASE 0xd0
55 #define PLLD_MISC 0xdc
56 #define PLLX_BASE 0xe0
57 #define PLLX_MISC 0xe4
58 #define PLLE_BASE 0xe8
59 #define PLLE_MISC 0xec
60 
61 #define PLL_BASE_LOCK BIT(27)
62 #define PLLE_MISC_LOCK BIT(11)
63 
64 #define PLL_MISC_LOCK_ENABLE 18
65 #define PLLDU_MISC_LOCK_ENABLE 22
66 #define PLLE_MISC_LOCK_ENABLE 9
67 
68 #define PLLC_OUT 0x84
69 #define PLLM_OUT 0x94
70 #define PLLP_OUTA 0xa4
71 #define PLLP_OUTB 0xa8
72 #define PLLA_OUT 0xb4
73 
74 #define CCLK_BURST_POLICY 0x20
75 #define SUPER_CCLK_DIVIDER 0x24
76 #define SCLK_BURST_POLICY 0x28
77 #define SUPER_SCLK_DIVIDER 0x2c
78 #define CLK_SYSTEM_RATE 0x30
79 
80 #define CCLK_BURST_POLICY_SHIFT	28
81 #define CCLK_RUN_POLICY_SHIFT	4
82 #define CCLK_IDLE_POLICY_SHIFT	0
83 #define CCLK_IDLE_POLICY	1
84 #define CCLK_RUN_POLICY		2
85 #define CCLK_BURST_POLICY_PLLX	8
86 
87 #define CLK_SOURCE_I2S1 0x100
88 #define CLK_SOURCE_I2S2 0x104
89 #define CLK_SOURCE_PWM 0x110
90 #define CLK_SOURCE_SPI 0x114
91 #define CLK_SOURCE_XIO 0x120
92 #define CLK_SOURCE_TWC 0x12c
93 #define CLK_SOURCE_IDE 0x144
94 #define CLK_SOURCE_HDMI 0x18c
95 #define CLK_SOURCE_DISP1 0x138
96 #define CLK_SOURCE_DISP2 0x13c
97 #define CLK_SOURCE_CSITE 0x1d4
98 #define CLK_SOURCE_I2C1 0x124
99 #define CLK_SOURCE_I2C2 0x198
100 #define CLK_SOURCE_I2C3 0x1b8
101 #define CLK_SOURCE_DVC 0x128
102 #define CLK_SOURCE_UARTA 0x178
103 #define CLK_SOURCE_UARTB 0x17c
104 #define CLK_SOURCE_UARTC 0x1a0
105 #define CLK_SOURCE_UARTD 0x1c0
106 #define CLK_SOURCE_UARTE 0x1c4
107 #define CLK_SOURCE_EMC 0x19c
108 
109 #define AUDIO_SYNC_CLK 0x38
110 
111 /* Tegra CPU clock and reset control regs */
112 #define TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX		0x4c
113 #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET	0x340
114 #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR	0x344
115 
116 #define CPU_CLOCK(cpu)	(0x1 << (8 + cpu))
117 #define CPU_RESET(cpu)	(0x1111ul << (cpu))
118 
119 #ifdef CONFIG_PM_SLEEP
120 static struct cpu_clk_suspend_context {
121 	u32 pllx_misc;
122 	u32 pllx_base;
123 
124 	u32 cpu_burst;
125 	u32 clk_csite_src;
126 	u32 cclk_divider;
127 } tegra20_cpu_clk_sctx;
128 #endif
129 
130 static void __iomem *clk_base;
131 static void __iomem *pmc_base;
132 
133 #define TEGRA_INIT_DATA_MUX(_name, _parents, _offset,	\
134 			    _clk_num, _gate_flags, _clk_id)	\
135 	TEGRA_INIT_DATA(_name, NULL, NULL, _parents, _offset,	\
136 			30, 2, 0, 0, 8, 1, TEGRA_DIVIDER_ROUND_UP,	\
137 			_clk_num, \
138 			_gate_flags, _clk_id)
139 
140 #define TEGRA_INIT_DATA_DIV16(_name, _parents, _offset, \
141 			      _clk_num, _gate_flags, _clk_id)	\
142 	TEGRA_INIT_DATA(_name, NULL, NULL, _parents, _offset,	\
143 			30, 2, 0, 0, 16, 0, TEGRA_DIVIDER_ROUND_UP, \
144 			_clk_num, _gate_flags,	\
145 			_clk_id)
146 
147 #define TEGRA_INIT_DATA_NODIV(_name, _parents, _offset, \
148 			      _mux_shift, _mux_width, _clk_num, \
149 			      _gate_flags, _clk_id)			\
150 	TEGRA_INIT_DATA(_name, NULL, NULL, _parents, _offset,	\
151 			_mux_shift, _mux_width, 0, 0, 0, 0, 0, \
152 			_clk_num, _gate_flags,	\
153 			_clk_id)
154 
155 static struct clk **clks;
156 
157 static struct tegra_clk_pll_freq_table pll_c_freq_table[] = {
158 	{ 12000000, 600000000, 600, 12, 1, 8 },
159 	{ 13000000, 600000000, 600, 13, 1, 8 },
160 	{ 19200000, 600000000, 500, 16, 1, 6 },
161 	{ 26000000, 600000000, 600, 26, 1, 8 },
162 	{        0,         0,   0,  0, 0, 0 },
163 };
164 
165 static struct tegra_clk_pll_freq_table pll_m_freq_table[] = {
166 	{ 12000000, 666000000, 666, 12, 1, 8 },
167 	{ 13000000, 666000000, 666, 13, 1, 8 },
168 	{ 19200000, 666000000, 555, 16, 1, 8 },
169 	{ 26000000, 666000000, 666, 26, 1, 8 },
170 	{ 12000000, 600000000, 600, 12, 1, 8 },
171 	{ 13000000, 600000000, 600, 13, 1, 8 },
172 	{ 19200000, 600000000, 375, 12, 1, 6 },
173 	{ 26000000, 600000000, 600, 26, 1, 8 },
174 	{        0,         0,   0,  0, 0, 0 },
175 };
176 
177 static struct tegra_clk_pll_freq_table pll_p_freq_table[] = {
178 	{ 12000000, 216000000, 432, 12, 2, 8 },
179 	{ 13000000, 216000000, 432, 13, 2, 8 },
180 	{ 19200000, 216000000,  90,  4, 2, 1 },
181 	{ 26000000, 216000000, 432, 26, 2, 8 },
182 	{ 12000000, 432000000, 432, 12, 1, 8 },
183 	{ 13000000, 432000000, 432, 13, 1, 8 },
184 	{ 19200000, 432000000,  90,  4, 1, 1 },
185 	{ 26000000, 432000000, 432, 26, 1, 8 },
186 	{        0,         0,   0,  0, 0, 0 },
187 };
188 
189 static struct tegra_clk_pll_freq_table pll_a_freq_table[] = {
190 	{ 28800000, 56448000, 49, 25, 1, 1 },
191 	{ 28800000, 73728000, 64, 25, 1, 1 },
192 	{ 28800000, 24000000,  5,  6, 1, 1 },
193 	{        0,        0,  0,  0, 0, 0 },
194 };
195 
196 static struct tegra_clk_pll_freq_table pll_d_freq_table[] = {
197 	{ 12000000,  216000000,  216, 12, 1,  4 },
198 	{ 13000000,  216000000,  216, 13, 1,  4 },
199 	{ 19200000,  216000000,  135, 12, 1,  3 },
200 	{ 26000000,  216000000,  216, 26, 1,  4 },
201 	{ 12000000,  594000000,  594, 12, 1,  8 },
202 	{ 13000000,  594000000,  594, 13, 1,  8 },
203 	{ 19200000,  594000000,  495, 16, 1,  8 },
204 	{ 26000000,  594000000,  594, 26, 1,  8 },
205 	{ 12000000, 1000000000, 1000, 12, 1, 12 },
206 	{ 13000000, 1000000000, 1000, 13, 1, 12 },
207 	{ 19200000, 1000000000,  625, 12, 1,  8 },
208 	{ 26000000, 1000000000, 1000, 26, 1, 12 },
209 	{        0,          0,    0,  0, 0,  0 },
210 };
211 
212 static struct tegra_clk_pll_freq_table pll_u_freq_table[] = {
213 	{ 12000000, 480000000, 960, 12, 1, 0 },
214 	{ 13000000, 480000000, 960, 13, 1, 0 },
215 	{ 19200000, 480000000, 200,  4, 1, 0 },
216 	{ 26000000, 480000000, 960, 26, 1, 0 },
217 	{        0,         0,   0,  0, 0, 0 },
218 };
219 
220 static struct tegra_clk_pll_freq_table pll_x_freq_table[] = {
221 	/* 1 GHz */
222 	{ 12000000, 1000000000, 1000, 12, 1, 12 },
223 	{ 13000000, 1000000000, 1000, 13, 1, 12 },
224 	{ 19200000, 1000000000,  625, 12, 1,  8 },
225 	{ 26000000, 1000000000, 1000, 26, 1, 12 },
226 	/* 912 MHz */
227 	{ 12000000,  912000000,  912, 12, 1, 12 },
228 	{ 13000000,  912000000,  912, 13, 1, 12 },
229 	{ 19200000,  912000000,  760, 16, 1,  8 },
230 	{ 26000000,  912000000,  912, 26, 1, 12 },
231 	/* 816 MHz */
232 	{ 12000000,  816000000,  816, 12, 1, 12 },
233 	{ 13000000,  816000000,  816, 13, 1, 12 },
234 	{ 19200000,  816000000,  680, 16, 1,  8 },
235 	{ 26000000,  816000000,  816, 26, 1, 12 },
236 	/* 760 MHz */
237 	{ 12000000,  760000000,  760, 12, 1, 12 },
238 	{ 13000000,  760000000,  760, 13, 1, 12 },
239 	{ 19200000,  760000000,  950, 24, 1,  8 },
240 	{ 26000000,  760000000,  760, 26, 1, 12 },
241 	/* 750 MHz */
242 	{ 12000000,  750000000,  750, 12, 1, 12 },
243 	{ 13000000,  750000000,  750, 13, 1, 12 },
244 	{ 19200000,  750000000,  625, 16, 1,  8 },
245 	{ 26000000,  750000000,  750, 26, 1, 12 },
246 	/* 608 MHz */
247 	{ 12000000,  608000000,  608, 12, 1, 12 },
248 	{ 13000000,  608000000,  608, 13, 1, 12 },
249 	{ 19200000,  608000000,  380, 12, 1,  8 },
250 	{ 26000000,  608000000,  608, 26, 1, 12 },
251 	/* 456 MHz */
252 	{ 12000000,  456000000,  456, 12, 1, 12 },
253 	{ 13000000,  456000000,  456, 13, 1, 12 },
254 	{ 19200000,  456000000,  380, 16, 1,  8 },
255 	{ 26000000,  456000000,  456, 26, 1, 12 },
256 	/* 312 MHz */
257 	{ 12000000,  312000000,  312, 12, 1, 12 },
258 	{ 13000000,  312000000,  312, 13, 1, 12 },
259 	{ 19200000,  312000000,  260, 16, 1,  8 },
260 	{ 26000000,  312000000,  312, 26, 1, 12 },
261 	{        0,          0,    0,  0, 0,  0 },
262 };
263 
264 static const struct pdiv_map plle_p[] = {
265 	{ .pdiv = 1, .hw_val = 1 },
266 	{ .pdiv = 0, .hw_val = 0 },
267 };
268 
269 static struct tegra_clk_pll_freq_table pll_e_freq_table[] = {
270 	{ 12000000, 100000000, 200, 24, 1, 0 },
271 	{        0,         0,   0,  0, 0, 0 },
272 };
273 
274 /* PLL parameters */
275 static struct tegra_clk_pll_params pll_c_params = {
276 	.input_min = 2000000,
277 	.input_max = 31000000,
278 	.cf_min = 1000000,
279 	.cf_max = 6000000,
280 	.vco_min = 20000000,
281 	.vco_max = 1400000000,
282 	.base_reg = PLLC_BASE,
283 	.misc_reg = PLLC_MISC,
284 	.lock_mask = PLL_BASE_LOCK,
285 	.lock_enable_bit_idx = PLL_MISC_LOCK_ENABLE,
286 	.lock_delay = 300,
287 	.freq_table = pll_c_freq_table,
288 	.flags = TEGRA_PLL_HAS_CPCON | TEGRA_PLL_HAS_LOCK_ENABLE,
289 };
290 
291 static struct tegra_clk_pll_params pll_m_params = {
292 	.input_min = 2000000,
293 	.input_max = 31000000,
294 	.cf_min = 1000000,
295 	.cf_max = 6000000,
296 	.vco_min = 20000000,
297 	.vco_max = 1200000000,
298 	.base_reg = PLLM_BASE,
299 	.misc_reg = PLLM_MISC,
300 	.lock_mask = PLL_BASE_LOCK,
301 	.lock_enable_bit_idx = PLL_MISC_LOCK_ENABLE,
302 	.lock_delay = 300,
303 	.freq_table = pll_m_freq_table,
304 	.flags = TEGRA_PLL_HAS_CPCON | TEGRA_PLL_HAS_LOCK_ENABLE,
305 };
306 
307 static struct tegra_clk_pll_params pll_p_params = {
308 	.input_min = 2000000,
309 	.input_max = 31000000,
310 	.cf_min = 1000000,
311 	.cf_max = 6000000,
312 	.vco_min = 20000000,
313 	.vco_max = 1400000000,
314 	.base_reg = PLLP_BASE,
315 	.misc_reg = PLLP_MISC,
316 	.lock_mask = PLL_BASE_LOCK,
317 	.lock_enable_bit_idx = PLL_MISC_LOCK_ENABLE,
318 	.lock_delay = 300,
319 	.freq_table = pll_p_freq_table,
320 	.flags = TEGRA_PLL_FIXED | TEGRA_PLL_HAS_CPCON |
321 		 TEGRA_PLL_HAS_LOCK_ENABLE,
322 	.fixed_rate =  216000000,
323 };
324 
325 static struct tegra_clk_pll_params pll_a_params = {
326 	.input_min = 2000000,
327 	.input_max = 31000000,
328 	.cf_min = 1000000,
329 	.cf_max = 6000000,
330 	.vco_min = 20000000,
331 	.vco_max = 1400000000,
332 	.base_reg = PLLA_BASE,
333 	.misc_reg = PLLA_MISC,
334 	.lock_mask = PLL_BASE_LOCK,
335 	.lock_enable_bit_idx = PLL_MISC_LOCK_ENABLE,
336 	.lock_delay = 300,
337 	.freq_table = pll_a_freq_table,
338 	.flags = TEGRA_PLL_HAS_CPCON | TEGRA_PLL_HAS_LOCK_ENABLE,
339 };
340 
341 static struct tegra_clk_pll_params pll_d_params = {
342 	.input_min = 2000000,
343 	.input_max = 40000000,
344 	.cf_min = 1000000,
345 	.cf_max = 6000000,
346 	.vco_min = 40000000,
347 	.vco_max = 1000000000,
348 	.base_reg = PLLD_BASE,
349 	.misc_reg = PLLD_MISC,
350 	.lock_mask = PLL_BASE_LOCK,
351 	.lock_enable_bit_idx = PLLDU_MISC_LOCK_ENABLE,
352 	.lock_delay = 1000,
353 	.freq_table = pll_d_freq_table,
354 	.flags = TEGRA_PLL_HAS_CPCON | TEGRA_PLL_HAS_LOCK_ENABLE,
355 };
356 
357 static const struct pdiv_map pllu_p[] = {
358 	{ .pdiv = 1, .hw_val = 1 },
359 	{ .pdiv = 2, .hw_val = 0 },
360 	{ .pdiv = 0, .hw_val = 0 },
361 };
362 
363 static struct tegra_clk_pll_params pll_u_params = {
364 	.input_min = 2000000,
365 	.input_max = 40000000,
366 	.cf_min = 1000000,
367 	.cf_max = 6000000,
368 	.vco_min = 48000000,
369 	.vco_max = 960000000,
370 	.base_reg = PLLU_BASE,
371 	.misc_reg = PLLU_MISC,
372 	.lock_mask = PLL_BASE_LOCK,
373 	.lock_enable_bit_idx = PLLDU_MISC_LOCK_ENABLE,
374 	.lock_delay = 1000,
375 	.pdiv_tohw = pllu_p,
376 	.freq_table = pll_u_freq_table,
377 	.flags = TEGRA_PLLU | TEGRA_PLL_HAS_CPCON | TEGRA_PLL_HAS_LOCK_ENABLE,
378 };
379 
380 static struct tegra_clk_pll_params pll_x_params = {
381 	.input_min = 2000000,
382 	.input_max = 31000000,
383 	.cf_min = 1000000,
384 	.cf_max = 6000000,
385 	.vco_min = 20000000,
386 	.vco_max = 1200000000,
387 	.base_reg = PLLX_BASE,
388 	.misc_reg = PLLX_MISC,
389 	.lock_mask = PLL_BASE_LOCK,
390 	.lock_enable_bit_idx = PLL_MISC_LOCK_ENABLE,
391 	.lock_delay = 300,
392 	.freq_table = pll_x_freq_table,
393 	.flags = TEGRA_PLL_HAS_CPCON | TEGRA_PLL_HAS_LOCK_ENABLE,
394 };
395 
396 static struct tegra_clk_pll_params pll_e_params = {
397 	.input_min = 12000000,
398 	.input_max = 12000000,
399 	.cf_min = 0,
400 	.cf_max = 0,
401 	.vco_min = 0,
402 	.vco_max = 0,
403 	.base_reg = PLLE_BASE,
404 	.misc_reg = PLLE_MISC,
405 	.lock_mask = PLLE_MISC_LOCK,
406 	.lock_enable_bit_idx = PLLE_MISC_LOCK_ENABLE,
407 	.lock_delay = 0,
408 	.pdiv_tohw = plle_p,
409 	.freq_table = pll_e_freq_table,
410 	.flags = TEGRA_PLL_FIXED | TEGRA_PLL_LOCK_MISC |
411 		 TEGRA_PLL_HAS_LOCK_ENABLE,
412 	.fixed_rate = 100000000,
413 };
414 
415 static struct tegra_devclk devclks[] __initdata = {
416 	{ .con_id = "pll_c", .dt_id = TEGRA20_CLK_PLL_C },
417 	{ .con_id = "pll_c_out1", .dt_id = TEGRA20_CLK_PLL_C_OUT1 },
418 	{ .con_id = "pll_p", .dt_id = TEGRA20_CLK_PLL_P },
419 	{ .con_id = "pll_p_out1", .dt_id = TEGRA20_CLK_PLL_P_OUT1 },
420 	{ .con_id = "pll_p_out2", .dt_id = TEGRA20_CLK_PLL_P_OUT2 },
421 	{ .con_id = "pll_p_out3", .dt_id = TEGRA20_CLK_PLL_P_OUT3 },
422 	{ .con_id = "pll_p_out4", .dt_id = TEGRA20_CLK_PLL_P_OUT4 },
423 	{ .con_id = "pll_m", .dt_id = TEGRA20_CLK_PLL_M },
424 	{ .con_id = "pll_m_out1", .dt_id = TEGRA20_CLK_PLL_M_OUT1 },
425 	{ .con_id = "pll_x", .dt_id = TEGRA20_CLK_PLL_X },
426 	{ .con_id = "pll_u", .dt_id = TEGRA20_CLK_PLL_U },
427 	{ .con_id = "pll_d", .dt_id = TEGRA20_CLK_PLL_D },
428 	{ .con_id = "pll_d_out0", .dt_id = TEGRA20_CLK_PLL_D_OUT0 },
429 	{ .con_id = "pll_a", .dt_id = TEGRA20_CLK_PLL_A },
430 	{ .con_id = "pll_a_out0", .dt_id = TEGRA20_CLK_PLL_A_OUT0 },
431 	{ .con_id = "pll_e", .dt_id = TEGRA20_CLK_PLL_E },
432 	{ .con_id = "cclk", .dt_id = TEGRA20_CLK_CCLK },
433 	{ .con_id = "sclk", .dt_id = TEGRA20_CLK_SCLK },
434 	{ .con_id = "hclk", .dt_id = TEGRA20_CLK_HCLK },
435 	{ .con_id = "pclk", .dt_id = TEGRA20_CLK_PCLK },
436 	{ .con_id = "fuse", .dt_id = TEGRA20_CLK_FUSE },
437 	{ .con_id = "twd", .dt_id = TEGRA20_CLK_TWD },
438 	{ .con_id = "audio", .dt_id = TEGRA20_CLK_AUDIO },
439 	{ .con_id = "audio_2x", .dt_id = TEGRA20_CLK_AUDIO_2X },
440 	{ .dev_id = "tegra20-ac97", .dt_id = TEGRA20_CLK_AC97 },
441 	{ .dev_id = "tegra-apbdma", .dt_id = TEGRA20_CLK_APBDMA },
442 	{ .dev_id = "rtc-tegra", .dt_id = TEGRA20_CLK_RTC },
443 	{ .dev_id = "timer", .dt_id = TEGRA20_CLK_TIMER },
444 	{ .dev_id = "tegra-kbc", .dt_id = TEGRA20_CLK_KBC },
445 	{ .con_id = "csus", .dev_id =  "tegra_camera", .dt_id = TEGRA20_CLK_CSUS },
446 	{ .con_id = "vcp", .dev_id = "tegra-avp", .dt_id = TEGRA20_CLK_VCP },
447 	{ .con_id = "bsea", .dev_id = "tegra-avp", .dt_id = TEGRA20_CLK_BSEA },
448 	{ .con_id = "bsev", .dev_id = "tegra-aes", .dt_id = TEGRA20_CLK_BSEV },
449 	{ .con_id = "emc", .dt_id = TEGRA20_CLK_EMC },
450 	{ .dev_id = "fsl-tegra-udc", .dt_id = TEGRA20_CLK_USBD },
451 	{ .dev_id = "tegra-ehci.1", .dt_id = TEGRA20_CLK_USB2 },
452 	{ .dev_id = "tegra-ehci.2", .dt_id = TEGRA20_CLK_USB3 },
453 	{ .dev_id = "dsi", .dt_id = TEGRA20_CLK_DSI },
454 	{ .con_id = "csi", .dev_id = "tegra_camera", .dt_id = TEGRA20_CLK_CSI },
455 	{ .con_id = "isp", .dev_id = "tegra_camera", .dt_id = TEGRA20_CLK_ISP },
456 	{ .con_id = "pex", .dt_id = TEGRA20_CLK_PEX },
457 	{ .con_id = "afi", .dt_id = TEGRA20_CLK_AFI },
458 	{ .con_id = "cdev1", .dt_id = TEGRA20_CLK_CDEV1 },
459 	{ .con_id = "cdev2", .dt_id = TEGRA20_CLK_CDEV2 },
460 	{ .con_id = "clk_32k", .dt_id = TEGRA20_CLK_CLK_32K },
461 	{ .con_id = "clk_m", .dt_id = TEGRA20_CLK_CLK_M },
462 	{ .con_id = "pll_ref", .dt_id = TEGRA20_CLK_PLL_REF },
463 	{ .dev_id = "tegra20-i2s.0", .dt_id = TEGRA20_CLK_I2S1 },
464 	{ .dev_id = "tegra20-i2s.1", .dt_id = TEGRA20_CLK_I2S2 },
465 	{ .con_id = "spdif_out", .dev_id = "tegra20-spdif", .dt_id = TEGRA20_CLK_SPDIF_OUT },
466 	{ .con_id = "spdif_in", .dev_id = "tegra20-spdif", .dt_id = TEGRA20_CLK_SPDIF_IN },
467 	{ .dev_id = "spi_tegra.0", .dt_id = TEGRA20_CLK_SBC1 },
468 	{ .dev_id = "spi_tegra.1", .dt_id = TEGRA20_CLK_SBC2 },
469 	{ .dev_id = "spi_tegra.2", .dt_id = TEGRA20_CLK_SBC3 },
470 	{ .dev_id = "spi_tegra.3", .dt_id = TEGRA20_CLK_SBC4 },
471 	{ .dev_id = "spi", .dt_id = TEGRA20_CLK_SPI },
472 	{ .dev_id = "xio", .dt_id = TEGRA20_CLK_XIO },
473 	{ .dev_id = "twc", .dt_id = TEGRA20_CLK_TWC },
474 	{ .dev_id = "ide", .dt_id = TEGRA20_CLK_IDE },
475 	{ .dev_id = "tegra_nand", .dt_id = TEGRA20_CLK_NDFLASH },
476 	{ .dev_id = "vfir", .dt_id = TEGRA20_CLK_VFIR },
477 	{ .dev_id = "csite", .dt_id = TEGRA20_CLK_CSITE },
478 	{ .dev_id = "la", .dt_id = TEGRA20_CLK_LA },
479 	{ .dev_id = "tegra_w1", .dt_id = TEGRA20_CLK_OWR },
480 	{ .dev_id = "mipi", .dt_id = TEGRA20_CLK_MIPI },
481 	{ .dev_id = "vde", .dt_id = TEGRA20_CLK_VDE },
482 	{ .con_id = "vi", .dev_id =  "tegra_camera", .dt_id = TEGRA20_CLK_VI },
483 	{ .dev_id = "epp", .dt_id = TEGRA20_CLK_EPP },
484 	{ .dev_id = "mpe", .dt_id = TEGRA20_CLK_MPE },
485 	{ .dev_id = "host1x", .dt_id = TEGRA20_CLK_HOST1X },
486 	{ .dev_id = "3d", .dt_id = TEGRA20_CLK_GR3D },
487 	{ .dev_id = "2d", .dt_id = TEGRA20_CLK_GR2D },
488 	{ .dev_id = "tegra-nor", .dt_id = TEGRA20_CLK_NOR },
489 	{ .dev_id = "sdhci-tegra.0", .dt_id = TEGRA20_CLK_SDMMC1 },
490 	{ .dev_id = "sdhci-tegra.1", .dt_id = TEGRA20_CLK_SDMMC2 },
491 	{ .dev_id = "sdhci-tegra.2", .dt_id = TEGRA20_CLK_SDMMC3 },
492 	{ .dev_id = "sdhci-tegra.3", .dt_id = TEGRA20_CLK_SDMMC4 },
493 	{ .dev_id = "cve", .dt_id = TEGRA20_CLK_CVE },
494 	{ .dev_id = "tvo", .dt_id = TEGRA20_CLK_TVO },
495 	{ .dev_id = "tvdac", .dt_id = TEGRA20_CLK_TVDAC },
496 	{ .con_id = "vi_sensor", .dev_id = "tegra_camera", .dt_id = TEGRA20_CLK_VI_SENSOR },
497 	{ .dev_id = "hdmi", .dt_id = TEGRA20_CLK_HDMI },
498 	{ .con_id = "div-clk", .dev_id = "tegra-i2c.0", .dt_id = TEGRA20_CLK_I2C1 },
499 	{ .con_id = "div-clk", .dev_id = "tegra-i2c.1", .dt_id = TEGRA20_CLK_I2C2 },
500 	{ .con_id = "div-clk", .dev_id = "tegra-i2c.2", .dt_id = TEGRA20_CLK_I2C3 },
501 	{ .con_id = "div-clk", .dev_id = "tegra-i2c.3", .dt_id = TEGRA20_CLK_DVC },
502 	{ .dev_id = "tegra-pwm", .dt_id = TEGRA20_CLK_PWM },
503 	{ .dev_id = "tegra_uart.0", .dt_id = TEGRA20_CLK_UARTA },
504 	{ .dev_id = "tegra_uart.1", .dt_id = TEGRA20_CLK_UARTB },
505 	{ .dev_id = "tegra_uart.2", .dt_id = TEGRA20_CLK_UARTC },
506 	{ .dev_id = "tegra_uart.3", .dt_id = TEGRA20_CLK_UARTD },
507 	{ .dev_id = "tegra_uart.4", .dt_id = TEGRA20_CLK_UARTE },
508 	{ .dev_id = "tegradc.0", .dt_id = TEGRA20_CLK_DISP1 },
509 	{ .dev_id = "tegradc.1", .dt_id = TEGRA20_CLK_DISP2 },
510 };
511 
512 static struct tegra_clk tegra20_clks[tegra_clk_max] __initdata = {
513 	[tegra_clk_ahbdma] = { .dt_id = TEGRA20_CLK_AHBDMA, .present = true },
514 	[tegra_clk_apbdma] = { .dt_id = TEGRA20_CLK_APBDMA, .present = true },
515 	[tegra_clk_spdif_out] = { .dt_id = TEGRA20_CLK_SPDIF_OUT, .present = true },
516 	[tegra_clk_spdif_in] = { .dt_id = TEGRA20_CLK_SPDIF_IN, .present = true },
517 	[tegra_clk_sdmmc1] = { .dt_id = TEGRA20_CLK_SDMMC1, .present = true },
518 	[tegra_clk_sdmmc2] = { .dt_id = TEGRA20_CLK_SDMMC2, .present = true },
519 	[tegra_clk_sdmmc3] = { .dt_id = TEGRA20_CLK_SDMMC3, .present = true },
520 	[tegra_clk_sdmmc4] = { .dt_id = TEGRA20_CLK_SDMMC4, .present = true },
521 	[tegra_clk_la] = { .dt_id = TEGRA20_CLK_LA, .present = true },
522 	[tegra_clk_csite] = { .dt_id = TEGRA20_CLK_CSITE, .present = true },
523 	[tegra_clk_vfir] = { .dt_id = TEGRA20_CLK_VFIR, .present = true },
524 	[tegra_clk_mipi] = { .dt_id = TEGRA20_CLK_MIPI, .present = true },
525 	[tegra_clk_nor] = { .dt_id = TEGRA20_CLK_NOR, .present = true },
526 	[tegra_clk_rtc] = { .dt_id = TEGRA20_CLK_RTC, .present = true },
527 	[tegra_clk_timer] = { .dt_id = TEGRA20_CLK_TIMER, .present = true },
528 	[tegra_clk_kbc] = { .dt_id = TEGRA20_CLK_KBC, .present = true },
529 	[tegra_clk_csus] = { .dt_id = TEGRA20_CLK_CSUS, .present = true },
530 	[tegra_clk_vcp] = { .dt_id = TEGRA20_CLK_VCP, .present = true },
531 	[tegra_clk_bsea] = { .dt_id = TEGRA20_CLK_BSEA, .present = true },
532 	[tegra_clk_bsev] = { .dt_id = TEGRA20_CLK_BSEV, .present = true },
533 	[tegra_clk_usbd] = { .dt_id = TEGRA20_CLK_USBD, .present = true },
534 	[tegra_clk_usb2] = { .dt_id = TEGRA20_CLK_USB2, .present = true },
535 	[tegra_clk_usb3] = { .dt_id = TEGRA20_CLK_USB3, .present = true },
536 	[tegra_clk_csi] = { .dt_id = TEGRA20_CLK_CSI, .present = true },
537 	[tegra_clk_isp] = { .dt_id = TEGRA20_CLK_ISP, .present = true },
538 	[tegra_clk_clk_32k] = { .dt_id = TEGRA20_CLK_CLK_32K, .present = true },
539 	[tegra_clk_hclk] = { .dt_id = TEGRA20_CLK_HCLK, .present = true },
540 	[tegra_clk_pclk] = { .dt_id = TEGRA20_CLK_PCLK, .present = true },
541 	[tegra_clk_pll_p_out1] = { .dt_id = TEGRA20_CLK_PLL_P_OUT1, .present = true },
542 	[tegra_clk_pll_p_out2] = { .dt_id = TEGRA20_CLK_PLL_P_OUT2, .present = true },
543 	[tegra_clk_pll_p_out3] = { .dt_id = TEGRA20_CLK_PLL_P_OUT3, .present = true },
544 	[tegra_clk_pll_p_out4] = { .dt_id = TEGRA20_CLK_PLL_P_OUT4, .present = true },
545 	[tegra_clk_pll_p] = { .dt_id = TEGRA20_CLK_PLL_P, .present = true },
546 	[tegra_clk_owr] = { .dt_id = TEGRA20_CLK_OWR, .present = true },
547 	[tegra_clk_sbc1] = { .dt_id = TEGRA20_CLK_SBC1, .present = true },
548 	[tegra_clk_sbc2] = { .dt_id = TEGRA20_CLK_SBC2, .present = true },
549 	[tegra_clk_sbc3] = { .dt_id = TEGRA20_CLK_SBC3, .present = true },
550 	[tegra_clk_sbc4] = { .dt_id = TEGRA20_CLK_SBC4, .present = true },
551 	[tegra_clk_vde] = { .dt_id = TEGRA20_CLK_VDE, .present = true },
552 	[tegra_clk_vi] = { .dt_id = TEGRA20_CLK_VI, .present = true },
553 	[tegra_clk_epp] = { .dt_id = TEGRA20_CLK_EPP, .present = true },
554 	[tegra_clk_mpe] = { .dt_id = TEGRA20_CLK_MPE, .present = true },
555 	[tegra_clk_host1x] = { .dt_id = TEGRA20_CLK_HOST1X, .present = true },
556 	[tegra_clk_gr2d] = { .dt_id = TEGRA20_CLK_GR2D, .present = true },
557 	[tegra_clk_gr3d] = { .dt_id = TEGRA20_CLK_GR3D, .present = true },
558 	[tegra_clk_ndflash] = { .dt_id = TEGRA20_CLK_NDFLASH, .present = true },
559 	[tegra_clk_cve] = { .dt_id = TEGRA20_CLK_CVE, .present = true },
560 	[tegra_clk_tvo] = { .dt_id = TEGRA20_CLK_TVO, .present = true },
561 	[tegra_clk_tvdac] = { .dt_id = TEGRA20_CLK_TVDAC, .present = true },
562 	[tegra_clk_vi_sensor] = { .dt_id = TEGRA20_CLK_VI_SENSOR, .present = true },
563 	[tegra_clk_afi] = { .dt_id = TEGRA20_CLK_AFI, .present = true },
564 	[tegra_clk_fuse] = { .dt_id = TEGRA20_CLK_FUSE, .present = true },
565 	[tegra_clk_kfuse] = { .dt_id = TEGRA20_CLK_KFUSE, .present = true },
566 };
567 
568 static unsigned long tegra20_clk_measure_input_freq(void)
569 {
570 	u32 osc_ctrl = readl_relaxed(clk_base + OSC_CTRL);
571 	u32 auto_clk_control = osc_ctrl & OSC_CTRL_OSC_FREQ_MASK;
572 	u32 pll_ref_div = osc_ctrl & OSC_CTRL_PLL_REF_DIV_MASK;
573 	unsigned long input_freq;
574 
575 	switch (auto_clk_control) {
576 	case OSC_CTRL_OSC_FREQ_12MHZ:
577 		BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
578 		input_freq = 12000000;
579 		break;
580 	case OSC_CTRL_OSC_FREQ_13MHZ:
581 		BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
582 		input_freq = 13000000;
583 		break;
584 	case OSC_CTRL_OSC_FREQ_19_2MHZ:
585 		BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
586 		input_freq = 19200000;
587 		break;
588 	case OSC_CTRL_OSC_FREQ_26MHZ:
589 		BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
590 		input_freq = 26000000;
591 		break;
592 	default:
593 		pr_err("Unexpected clock autodetect value %d",
594 		       auto_clk_control);
595 		BUG();
596 		return 0;
597 	}
598 
599 	return input_freq;
600 }
601 
602 static unsigned int tegra20_get_pll_ref_div(void)
603 {
604 	u32 pll_ref_div = readl_relaxed(clk_base + OSC_CTRL) &
605 		OSC_CTRL_PLL_REF_DIV_MASK;
606 
607 	switch (pll_ref_div) {
608 	case OSC_CTRL_PLL_REF_DIV_1:
609 		return 1;
610 	case OSC_CTRL_PLL_REF_DIV_2:
611 		return 2;
612 	case OSC_CTRL_PLL_REF_DIV_4:
613 		return 4;
614 	default:
615 		pr_err("Invalid pll ref divider %d\n", pll_ref_div);
616 		BUG();
617 	}
618 	return 0;
619 }
620 
621 static void tegra20_pll_init(void)
622 {
623 	struct clk *clk;
624 
625 	/* PLLC */
626 	clk = tegra_clk_register_pll("pll_c", "pll_ref", clk_base, NULL, 0,
627 			    &pll_c_params, NULL);
628 	clks[TEGRA20_CLK_PLL_C] = clk;
629 
630 	/* PLLC_OUT1 */
631 	clk = tegra_clk_register_divider("pll_c_out1_div", "pll_c",
632 				clk_base + PLLC_OUT, 0, TEGRA_DIVIDER_ROUND_UP,
633 				8, 8, 1, NULL);
634 	clk = tegra_clk_register_pll_out("pll_c_out1", "pll_c_out1_div",
635 				clk_base + PLLC_OUT, 1, 0, CLK_SET_RATE_PARENT,
636 				0, NULL);
637 	clks[TEGRA20_CLK_PLL_C_OUT1] = clk;
638 
639 	/* PLLM */
640 	clk = tegra_clk_register_pll("pll_m", "pll_ref", clk_base, NULL,
641 			    CLK_SET_RATE_GATE, &pll_m_params, NULL);
642 	clks[TEGRA20_CLK_PLL_M] = clk;
643 
644 	/* PLLM_OUT1 */
645 	clk = tegra_clk_register_divider("pll_m_out1_div", "pll_m",
646 				clk_base + PLLM_OUT, 0, TEGRA_DIVIDER_ROUND_UP,
647 				8, 8, 1, NULL);
648 	clk = tegra_clk_register_pll_out("pll_m_out1", "pll_m_out1_div",
649 				clk_base + PLLM_OUT, 1, 0,
650 				CLK_SET_RATE_PARENT, 0, NULL);
651 	clks[TEGRA20_CLK_PLL_M_OUT1] = clk;
652 
653 	/* PLLX */
654 	clk = tegra_clk_register_pll("pll_x", "pll_ref", clk_base, NULL, 0,
655 			    &pll_x_params, NULL);
656 	clks[TEGRA20_CLK_PLL_X] = clk;
657 
658 	/* PLLU */
659 	clk = tegra_clk_register_pll("pll_u", "pll_ref", clk_base, NULL, 0,
660 			    &pll_u_params, NULL);
661 	clks[TEGRA20_CLK_PLL_U] = clk;
662 
663 	/* PLLD */
664 	clk = tegra_clk_register_pll("pll_d", "pll_ref", clk_base, NULL, 0,
665 			    &pll_d_params, NULL);
666 	clks[TEGRA20_CLK_PLL_D] = clk;
667 
668 	/* PLLD_OUT0 */
669 	clk = clk_register_fixed_factor(NULL, "pll_d_out0", "pll_d",
670 					CLK_SET_RATE_PARENT, 1, 2);
671 	clks[TEGRA20_CLK_PLL_D_OUT0] = clk;
672 
673 	/* PLLA */
674 	clk = tegra_clk_register_pll("pll_a", "pll_p_out1", clk_base, NULL, 0,
675 			    &pll_a_params, NULL);
676 	clks[TEGRA20_CLK_PLL_A] = clk;
677 
678 	/* PLLA_OUT0 */
679 	clk = tegra_clk_register_divider("pll_a_out0_div", "pll_a",
680 				clk_base + PLLA_OUT, 0, TEGRA_DIVIDER_ROUND_UP,
681 				8, 8, 1, NULL);
682 	clk = tegra_clk_register_pll_out("pll_a_out0", "pll_a_out0_div",
683 				clk_base + PLLA_OUT, 1, 0, CLK_IGNORE_UNUSED |
684 				CLK_SET_RATE_PARENT, 0, NULL);
685 	clks[TEGRA20_CLK_PLL_A_OUT0] = clk;
686 
687 	/* PLLE */
688 	clk = tegra_clk_register_plle("pll_e", "pll_ref", clk_base, pmc_base,
689 			     0, &pll_e_params, NULL);
690 	clks[TEGRA20_CLK_PLL_E] = clk;
691 }
692 
693 static const char *cclk_parents[] = { "clk_m", "pll_c", "clk_32k", "pll_m",
694 				      "pll_p", "pll_p_out4",
695 				      "pll_p_out3", "clk_d", "pll_x" };
696 static const char *sclk_parents[] = { "clk_m", "pll_c_out1", "pll_p_out4",
697 				      "pll_p_out3", "pll_p_out2", "clk_d",
698 				      "clk_32k", "pll_m_out1" };
699 
700 static void tegra20_super_clk_init(void)
701 {
702 	struct clk *clk;
703 
704 	/* CCLK */
705 	clk = tegra_clk_register_super_mux("cclk", cclk_parents,
706 			      ARRAY_SIZE(cclk_parents), CLK_SET_RATE_PARENT,
707 			      clk_base + CCLK_BURST_POLICY, 0, 4, 0, 0, NULL);
708 	clks[TEGRA20_CLK_CCLK] = clk;
709 
710 	/* SCLK */
711 	clk = tegra_clk_register_super_mux("sclk", sclk_parents,
712 			      ARRAY_SIZE(sclk_parents),
713 			      CLK_SET_RATE_PARENT | CLK_IS_CRITICAL,
714 			      clk_base + SCLK_BURST_POLICY, 0, 4, 0, 0, NULL);
715 	clks[TEGRA20_CLK_SCLK] = clk;
716 
717 	/* twd */
718 	clk = clk_register_fixed_factor(NULL, "twd", "cclk", 0, 1, 4);
719 	clks[TEGRA20_CLK_TWD] = clk;
720 }
721 
722 static const char *audio_parents[] = { "spdif_in", "i2s1", "i2s2", "unused",
723 				       "pll_a_out0", "unused", "unused",
724 				       "unused" };
725 
726 static void __init tegra20_audio_clk_init(void)
727 {
728 	struct clk *clk;
729 
730 	/* audio */
731 	clk = clk_register_mux(NULL, "audio_mux", audio_parents,
732 				ARRAY_SIZE(audio_parents),
733 				CLK_SET_RATE_NO_REPARENT,
734 				clk_base + AUDIO_SYNC_CLK, 0, 3, 0, NULL);
735 	clk = clk_register_gate(NULL, "audio", "audio_mux", 0,
736 				clk_base + AUDIO_SYNC_CLK, 4,
737 				CLK_GATE_SET_TO_DISABLE, NULL);
738 	clks[TEGRA20_CLK_AUDIO] = clk;
739 
740 	/* audio_2x */
741 	clk = clk_register_fixed_factor(NULL, "audio_doubler", "audio",
742 					CLK_SET_RATE_PARENT, 2, 1);
743 	clk = tegra_clk_register_periph_gate("audio_2x", "audio_doubler",
744 				    TEGRA_PERIPH_NO_RESET, clk_base,
745 				    CLK_SET_RATE_PARENT, 89,
746 				    periph_clk_enb_refcnt);
747 	clks[TEGRA20_CLK_AUDIO_2X] = clk;
748 }
749 
750 static const char *i2s1_parents[] = { "pll_a_out0", "audio_2x", "pll_p",
751 				      "clk_m" };
752 static const char *i2s2_parents[] = { "pll_a_out0", "audio_2x", "pll_p",
753 				      "clk_m" };
754 static const char *pwm_parents[] = { "pll_p", "pll_c", "audio", "clk_m",
755 				     "clk_32k" };
756 static const char *mux_pllpcm_clkm[] = { "pll_p", "pll_c", "pll_m", "clk_m" };
757 static const char *mux_pllpdc_clkm[] = { "pll_p", "pll_d_out0", "pll_c",
758 					 "clk_m" };
759 
760 static struct tegra_periph_init_data tegra_periph_clk_list[] = {
761 	TEGRA_INIT_DATA_MUX("i2s1", i2s1_parents,     CLK_SOURCE_I2S1,   11, TEGRA_PERIPH_ON_APB, TEGRA20_CLK_I2S1),
762 	TEGRA_INIT_DATA_MUX("i2s2", i2s2_parents,     CLK_SOURCE_I2S2,   18, TEGRA_PERIPH_ON_APB, TEGRA20_CLK_I2S2),
763 	TEGRA_INIT_DATA_MUX("spi",   mux_pllpcm_clkm,   CLK_SOURCE_SPI,   43, TEGRA_PERIPH_ON_APB, TEGRA20_CLK_SPI),
764 	TEGRA_INIT_DATA_MUX("xio",   mux_pllpcm_clkm,   CLK_SOURCE_XIO,   45, 0, TEGRA20_CLK_XIO),
765 	TEGRA_INIT_DATA_MUX("twc",   mux_pllpcm_clkm,   CLK_SOURCE_TWC,   16, TEGRA_PERIPH_ON_APB, TEGRA20_CLK_TWC),
766 	TEGRA_INIT_DATA_MUX("ide",   mux_pllpcm_clkm,   CLK_SOURCE_XIO,   25, 0, TEGRA20_CLK_IDE),
767 	TEGRA_INIT_DATA_DIV16("dvc", mux_pllpcm_clkm,   CLK_SOURCE_DVC,   47, TEGRA_PERIPH_ON_APB, TEGRA20_CLK_DVC),
768 	TEGRA_INIT_DATA_DIV16("i2c1", mux_pllpcm_clkm,   CLK_SOURCE_I2C1,   12, TEGRA_PERIPH_ON_APB, TEGRA20_CLK_I2C1),
769 	TEGRA_INIT_DATA_DIV16("i2c2", mux_pllpcm_clkm,   CLK_SOURCE_I2C2,   54, TEGRA_PERIPH_ON_APB, TEGRA20_CLK_I2C2),
770 	TEGRA_INIT_DATA_DIV16("i2c3", mux_pllpcm_clkm,   CLK_SOURCE_I2C3,   67, TEGRA_PERIPH_ON_APB, TEGRA20_CLK_I2C3),
771 	TEGRA_INIT_DATA_MUX("hdmi", mux_pllpdc_clkm,   CLK_SOURCE_HDMI,   51, 0, TEGRA20_CLK_HDMI),
772 	TEGRA_INIT_DATA("pwm", NULL, NULL, pwm_parents,     CLK_SOURCE_PWM,   28, 3, 0, 0, 8, 1, 0, 17, TEGRA_PERIPH_ON_APB, TEGRA20_CLK_PWM),
773 };
774 
775 static struct tegra_periph_init_data tegra_periph_nodiv_clk_list[] = {
776 	TEGRA_INIT_DATA_NODIV("uarta",	mux_pllpcm_clkm, CLK_SOURCE_UARTA, 30, 2, 6,   TEGRA_PERIPH_ON_APB, TEGRA20_CLK_UARTA),
777 	TEGRA_INIT_DATA_NODIV("uartb",	mux_pllpcm_clkm, CLK_SOURCE_UARTB, 30, 2, 7,   TEGRA_PERIPH_ON_APB, TEGRA20_CLK_UARTB),
778 	TEGRA_INIT_DATA_NODIV("uartc",	mux_pllpcm_clkm, CLK_SOURCE_UARTC, 30, 2, 55,  TEGRA_PERIPH_ON_APB, TEGRA20_CLK_UARTC),
779 	TEGRA_INIT_DATA_NODIV("uartd",	mux_pllpcm_clkm, CLK_SOURCE_UARTD, 30, 2, 65,  TEGRA_PERIPH_ON_APB, TEGRA20_CLK_UARTD),
780 	TEGRA_INIT_DATA_NODIV("uarte",	mux_pllpcm_clkm, CLK_SOURCE_UARTE, 30, 2, 66,  TEGRA_PERIPH_ON_APB, TEGRA20_CLK_UARTE),
781 	TEGRA_INIT_DATA_NODIV("disp1",	mux_pllpdc_clkm, CLK_SOURCE_DISP1, 30, 2, 27,  0, TEGRA20_CLK_DISP1),
782 	TEGRA_INIT_DATA_NODIV("disp2",	mux_pllpdc_clkm, CLK_SOURCE_DISP2, 30, 2, 26,  0, TEGRA20_CLK_DISP2),
783 };
784 
785 static void __init tegra20_periph_clk_init(void)
786 {
787 	struct tegra_periph_init_data *data;
788 	struct clk *clk;
789 	unsigned int i;
790 
791 	/* ac97 */
792 	clk = tegra_clk_register_periph_gate("ac97", "pll_a_out0",
793 				    TEGRA_PERIPH_ON_APB,
794 				    clk_base, 0, 3, periph_clk_enb_refcnt);
795 	clks[TEGRA20_CLK_AC97] = clk;
796 
797 	/* emc */
798 	clk = tegra20_clk_register_emc(clk_base + CLK_SOURCE_EMC, false);
799 
800 	clks[TEGRA20_CLK_EMC] = clk;
801 
802 	clk = tegra_clk_register_mc("mc", "emc", clk_base + CLK_SOURCE_EMC,
803 				    NULL);
804 	clks[TEGRA20_CLK_MC] = clk;
805 
806 	/* dsi */
807 	clk = tegra_clk_register_periph_gate("dsi", "pll_d", 0, clk_base, 0,
808 				    48, periph_clk_enb_refcnt);
809 	clk_register_clkdev(clk, NULL, "dsi");
810 	clks[TEGRA20_CLK_DSI] = clk;
811 
812 	/* pex */
813 	clk = tegra_clk_register_periph_gate("pex", "clk_m", 0, clk_base, 0, 70,
814 				    periph_clk_enb_refcnt);
815 	clks[TEGRA20_CLK_PEX] = clk;
816 
817 	/* dev1 OSC divider */
818 	clk_register_divider(NULL, "dev1_osc_div", "clk_m",
819 			     0, clk_base + MISC_CLK_ENB, 22, 2,
820 			     CLK_DIVIDER_POWER_OF_TWO | CLK_DIVIDER_READ_ONLY,
821 			     NULL);
822 
823 	/* dev2 OSC divider */
824 	clk_register_divider(NULL, "dev2_osc_div", "clk_m",
825 			     0, clk_base + MISC_CLK_ENB, 20, 2,
826 			     CLK_DIVIDER_POWER_OF_TWO | CLK_DIVIDER_READ_ONLY,
827 			     NULL);
828 
829 	/* cdev1 */
830 	clk = tegra_clk_register_periph_gate("cdev1", "cdev1_mux", 0,
831 				    clk_base, 0, 94, periph_clk_enb_refcnt);
832 	clks[TEGRA20_CLK_CDEV1] = clk;
833 
834 	/* cdev2 */
835 	clk = tegra_clk_register_periph_gate("cdev2", "cdev2_mux", 0,
836 				    clk_base, 0, 93, periph_clk_enb_refcnt);
837 	clks[TEGRA20_CLK_CDEV2] = clk;
838 
839 	for (i = 0; i < ARRAY_SIZE(tegra_periph_clk_list); i++) {
840 		data = &tegra_periph_clk_list[i];
841 		clk = tegra_clk_register_periph_data(clk_base, data);
842 		clks[data->clk_id] = clk;
843 	}
844 
845 	for (i = 0; i < ARRAY_SIZE(tegra_periph_nodiv_clk_list); i++) {
846 		data = &tegra_periph_nodiv_clk_list[i];
847 		clk = tegra_clk_register_periph_nodiv(data->name,
848 					data->p.parent_names,
849 					data->num_parents, &data->periph,
850 					clk_base, data->offset);
851 		clks[data->clk_id] = clk;
852 	}
853 
854 	tegra_periph_clk_init(clk_base, pmc_base, tegra20_clks, &pll_p_params);
855 }
856 
857 static void __init tegra20_osc_clk_init(void)
858 {
859 	struct clk *clk;
860 	unsigned long input_freq;
861 	unsigned int pll_ref_div;
862 
863 	input_freq = tegra20_clk_measure_input_freq();
864 
865 	/* clk_m */
866 	clk = clk_register_fixed_rate(NULL, "clk_m", NULL, CLK_IGNORE_UNUSED,
867 				      input_freq);
868 	clks[TEGRA20_CLK_CLK_M] = clk;
869 
870 	/* pll_ref */
871 	pll_ref_div = tegra20_get_pll_ref_div();
872 	clk = clk_register_fixed_factor(NULL, "pll_ref", "clk_m",
873 					CLK_SET_RATE_PARENT, 1, pll_ref_div);
874 	clks[TEGRA20_CLK_PLL_REF] = clk;
875 }
876 
877 /* Tegra20 CPU clock and reset control functions */
878 static void tegra20_wait_cpu_in_reset(u32 cpu)
879 {
880 	unsigned int reg;
881 
882 	do {
883 		reg = readl(clk_base +
884 			    TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
885 		cpu_relax();
886 	} while (!(reg & (1 << cpu)));	/* check CPU been reset or not */
887 
888 	return;
889 }
890 
891 static void tegra20_put_cpu_in_reset(u32 cpu)
892 {
893 	writel(CPU_RESET(cpu),
894 	       clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
895 	dmb();
896 }
897 
898 static void tegra20_cpu_out_of_reset(u32 cpu)
899 {
900 	writel(CPU_RESET(cpu),
901 	       clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR);
902 	wmb();
903 }
904 
905 static void tegra20_enable_cpu_clock(u32 cpu)
906 {
907 	unsigned int reg;
908 
909 	reg = readl(clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
910 	writel(reg & ~CPU_CLOCK(cpu),
911 	       clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
912 	barrier();
913 	reg = readl(clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
914 }
915 
916 static void tegra20_disable_cpu_clock(u32 cpu)
917 {
918 	unsigned int reg;
919 
920 	reg = readl(clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
921 	writel(reg | CPU_CLOCK(cpu),
922 	       clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
923 }
924 
925 #ifdef CONFIG_PM_SLEEP
926 static bool tegra20_cpu_rail_off_ready(void)
927 {
928 	unsigned int cpu_rst_status;
929 
930 	cpu_rst_status = readl(clk_base +
931 			       TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
932 
933 	return !!(cpu_rst_status & 0x2);
934 }
935 
936 static void tegra20_cpu_clock_suspend(void)
937 {
938 	/* switch coresite to clk_m, save off original source */
939 	tegra20_cpu_clk_sctx.clk_csite_src =
940 				readl(clk_base + CLK_SOURCE_CSITE);
941 	writel(3<<30, clk_base + CLK_SOURCE_CSITE);
942 
943 	tegra20_cpu_clk_sctx.cpu_burst =
944 				readl(clk_base + CCLK_BURST_POLICY);
945 	tegra20_cpu_clk_sctx.pllx_base =
946 				readl(clk_base + PLLX_BASE);
947 	tegra20_cpu_clk_sctx.pllx_misc =
948 				readl(clk_base + PLLX_MISC);
949 	tegra20_cpu_clk_sctx.cclk_divider =
950 				readl(clk_base + SUPER_CCLK_DIVIDER);
951 }
952 
953 static void tegra20_cpu_clock_resume(void)
954 {
955 	unsigned int reg, policy;
956 	u32 misc, base;
957 
958 	/* Is CPU complex already running on PLLX? */
959 	reg = readl(clk_base + CCLK_BURST_POLICY);
960 	policy = (reg >> CCLK_BURST_POLICY_SHIFT) & 0xF;
961 
962 	if (policy == CCLK_IDLE_POLICY)
963 		reg = (reg >> CCLK_IDLE_POLICY_SHIFT) & 0xF;
964 	else if (policy == CCLK_RUN_POLICY)
965 		reg = (reg >> CCLK_RUN_POLICY_SHIFT) & 0xF;
966 	else
967 		BUG();
968 
969 	if (reg != CCLK_BURST_POLICY_PLLX) {
970 		misc = readl_relaxed(clk_base + PLLX_MISC);
971 		base = readl_relaxed(clk_base + PLLX_BASE);
972 
973 		if (misc != tegra20_cpu_clk_sctx.pllx_misc ||
974 		    base != tegra20_cpu_clk_sctx.pllx_base) {
975 			/* restore PLLX settings if CPU is on different PLL */
976 			writel(tegra20_cpu_clk_sctx.pllx_misc,
977 						clk_base + PLLX_MISC);
978 			writel(tegra20_cpu_clk_sctx.pllx_base,
979 						clk_base + PLLX_BASE);
980 
981 			/* wait for PLL stabilization if PLLX was enabled */
982 			if (tegra20_cpu_clk_sctx.pllx_base & (1 << 30))
983 				udelay(300);
984 		}
985 	}
986 
987 	/*
988 	 * Restore original burst policy setting for calls resulting from CPU
989 	 * LP2 in idle or system suspend.
990 	 */
991 	writel(tegra20_cpu_clk_sctx.cclk_divider,
992 					clk_base + SUPER_CCLK_DIVIDER);
993 	writel(tegra20_cpu_clk_sctx.cpu_burst,
994 					clk_base + CCLK_BURST_POLICY);
995 
996 	writel(tegra20_cpu_clk_sctx.clk_csite_src,
997 					clk_base + CLK_SOURCE_CSITE);
998 }
999 #endif
1000 
1001 static struct tegra_cpu_car_ops tegra20_cpu_car_ops = {
1002 	.wait_for_reset	= tegra20_wait_cpu_in_reset,
1003 	.put_in_reset	= tegra20_put_cpu_in_reset,
1004 	.out_of_reset	= tegra20_cpu_out_of_reset,
1005 	.enable_clock	= tegra20_enable_cpu_clock,
1006 	.disable_clock	= tegra20_disable_cpu_clock,
1007 #ifdef CONFIG_PM_SLEEP
1008 	.rail_off_ready = tegra20_cpu_rail_off_ready,
1009 	.suspend	= tegra20_cpu_clock_suspend,
1010 	.resume		= tegra20_cpu_clock_resume,
1011 #endif
1012 };
1013 
1014 static struct tegra_clk_init_table init_table[] __initdata = {
1015 	{ TEGRA20_CLK_PLL_P, TEGRA20_CLK_CLK_MAX, 216000000, 1 },
1016 	{ TEGRA20_CLK_PLL_P_OUT1, TEGRA20_CLK_CLK_MAX, 28800000, 1 },
1017 	{ TEGRA20_CLK_PLL_P_OUT2, TEGRA20_CLK_CLK_MAX, 48000000, 1 },
1018 	{ TEGRA20_CLK_PLL_P_OUT3, TEGRA20_CLK_CLK_MAX, 72000000, 1 },
1019 	{ TEGRA20_CLK_PLL_P_OUT4, TEGRA20_CLK_CLK_MAX, 24000000, 1 },
1020 	{ TEGRA20_CLK_PLL_C, TEGRA20_CLK_CLK_MAX, 600000000, 0 },
1021 	{ TEGRA20_CLK_PLL_C_OUT1, TEGRA20_CLK_CLK_MAX, 240000000, 0 },
1022 	{ TEGRA20_CLK_SCLK, TEGRA20_CLK_PLL_C_OUT1, 240000000, 0 },
1023 	{ TEGRA20_CLK_HCLK, TEGRA20_CLK_CLK_MAX, 240000000, 0 },
1024 	{ TEGRA20_CLK_PCLK, TEGRA20_CLK_CLK_MAX, 60000000, 0 },
1025 	{ TEGRA20_CLK_CSITE, TEGRA20_CLK_CLK_MAX, 0, 1 },
1026 	{ TEGRA20_CLK_CCLK, TEGRA20_CLK_CLK_MAX, 0, 1 },
1027 	{ TEGRA20_CLK_UARTA, TEGRA20_CLK_PLL_P, 0, 0 },
1028 	{ TEGRA20_CLK_UARTB, TEGRA20_CLK_PLL_P, 0, 0 },
1029 	{ TEGRA20_CLK_UARTC, TEGRA20_CLK_PLL_P, 0, 0 },
1030 	{ TEGRA20_CLK_UARTD, TEGRA20_CLK_PLL_P, 0, 0 },
1031 	{ TEGRA20_CLK_UARTE, TEGRA20_CLK_PLL_P, 0, 0 },
1032 	{ TEGRA20_CLK_PLL_A, TEGRA20_CLK_CLK_MAX, 56448000, 0 },
1033 	{ TEGRA20_CLK_PLL_A_OUT0, TEGRA20_CLK_CLK_MAX, 11289600, 0 },
1034 	{ TEGRA20_CLK_I2S1, TEGRA20_CLK_PLL_A_OUT0, 11289600, 0 },
1035 	{ TEGRA20_CLK_I2S2, TEGRA20_CLK_PLL_A_OUT0, 11289600, 0 },
1036 	{ TEGRA20_CLK_SDMMC1, TEGRA20_CLK_PLL_P, 48000000, 0 },
1037 	{ TEGRA20_CLK_SDMMC3, TEGRA20_CLK_PLL_P, 48000000, 0 },
1038 	{ TEGRA20_CLK_SDMMC4, TEGRA20_CLK_PLL_P, 48000000, 0 },
1039 	{ TEGRA20_CLK_SPI, TEGRA20_CLK_PLL_P, 20000000, 0 },
1040 	{ TEGRA20_CLK_SBC1, TEGRA20_CLK_PLL_P, 100000000, 0 },
1041 	{ TEGRA20_CLK_SBC2, TEGRA20_CLK_PLL_P, 100000000, 0 },
1042 	{ TEGRA20_CLK_SBC3, TEGRA20_CLK_PLL_P, 100000000, 0 },
1043 	{ TEGRA20_CLK_SBC4, TEGRA20_CLK_PLL_P, 100000000, 0 },
1044 	{ TEGRA20_CLK_HOST1X, TEGRA20_CLK_PLL_C, 150000000, 0 },
1045 	{ TEGRA20_CLK_GR2D, TEGRA20_CLK_PLL_C, 300000000, 0 },
1046 	{ TEGRA20_CLK_GR3D, TEGRA20_CLK_PLL_C, 300000000, 0 },
1047 	{ TEGRA20_CLK_VDE, TEGRA20_CLK_PLL_C, 300000000, 0 },
1048 	/* must be the last entry */
1049 	{ TEGRA20_CLK_CLK_MAX, TEGRA20_CLK_CLK_MAX, 0, 0 },
1050 };
1051 
1052 static void __init tegra20_clock_apply_init_table(void)
1053 {
1054 	tegra_init_from_table(init_table, clks, TEGRA20_CLK_CLK_MAX);
1055 }
1056 
1057 /*
1058  * Some clocks may be used by different drivers depending on the board
1059  * configuration.  List those here to register them twice in the clock lookup
1060  * table under two names.
1061  */
1062 static struct tegra_clk_duplicate tegra_clk_duplicates[] = {
1063 	TEGRA_CLK_DUPLICATE(TEGRA20_CLK_USBD,    "utmip-pad",     NULL),
1064 	TEGRA_CLK_DUPLICATE(TEGRA20_CLK_USBD,    "tegra-ehci.0",  NULL),
1065 	TEGRA_CLK_DUPLICATE(TEGRA20_CLK_USBD,    "tegra-otg",     NULL),
1066 	TEGRA_CLK_DUPLICATE(TEGRA20_CLK_CCLK,    NULL,           "cpu"),
1067 	/* must be the last entry */
1068 	TEGRA_CLK_DUPLICATE(TEGRA20_CLK_CLK_MAX, NULL,            NULL),
1069 };
1070 
1071 static const struct of_device_id pmc_match[] __initconst = {
1072 	{ .compatible = "nvidia,tegra20-pmc" },
1073 	{ },
1074 };
1075 
1076 static struct clk *tegra20_clk_src_onecell_get(struct of_phandle_args *clkspec,
1077 					       void *data)
1078 {
1079 	struct clk_hw *parent_hw;
1080 	struct clk_hw *hw;
1081 	struct clk *clk;
1082 
1083 	clk = of_clk_src_onecell_get(clkspec, data);
1084 	if (IS_ERR(clk))
1085 		return clk;
1086 
1087 	hw = __clk_get_hw(clk);
1088 
1089 	/*
1090 	 * Tegra20 CDEV1 and CDEV2 clocks are a bit special case, their parent
1091 	 * clock is created by the pinctrl driver. It is possible for clk user
1092 	 * to request these clocks before pinctrl driver got probed and hence
1093 	 * user will get an orphaned clock. That might be undesirable because
1094 	 * user may expect parent clock to be enabled by the child.
1095 	 */
1096 	if (clkspec->args[0] == TEGRA20_CLK_CDEV1 ||
1097 	    clkspec->args[0] == TEGRA20_CLK_CDEV2) {
1098 		parent_hw = clk_hw_get_parent(hw);
1099 		if (!parent_hw)
1100 			return ERR_PTR(-EPROBE_DEFER);
1101 	}
1102 
1103 	if (clkspec->args[0] == TEGRA20_CLK_EMC) {
1104 		if (!tegra20_clk_emc_driver_available(hw))
1105 			return ERR_PTR(-EPROBE_DEFER);
1106 	}
1107 
1108 	return clk;
1109 }
1110 
1111 static void __init tegra20_clock_init(struct device_node *np)
1112 {
1113 	struct device_node *node;
1114 
1115 	clk_base = of_iomap(np, 0);
1116 	if (!clk_base) {
1117 		pr_err("Can't map CAR registers\n");
1118 		BUG();
1119 	}
1120 
1121 	node = of_find_matching_node(NULL, pmc_match);
1122 	if (!node) {
1123 		pr_err("Failed to find pmc node\n");
1124 		BUG();
1125 	}
1126 
1127 	pmc_base = of_iomap(node, 0);
1128 	if (!pmc_base) {
1129 		pr_err("Can't map pmc registers\n");
1130 		BUG();
1131 	}
1132 
1133 	clks = tegra_clk_init(clk_base, TEGRA20_CLK_CLK_MAX,
1134 				TEGRA20_CLK_PERIPH_BANKS);
1135 	if (!clks)
1136 		return;
1137 
1138 	tegra20_osc_clk_init();
1139 	tegra_fixed_clk_init(tegra20_clks);
1140 	tegra20_pll_init();
1141 	tegra20_super_clk_init();
1142 	tegra_super_clk_gen4_init(clk_base, pmc_base, tegra20_clks, NULL);
1143 	tegra20_periph_clk_init();
1144 	tegra20_audio_clk_init();
1145 
1146 	tegra_init_dup_clks(tegra_clk_duplicates, clks, TEGRA20_CLK_CLK_MAX);
1147 
1148 	tegra_add_of_provider(np, tegra20_clk_src_onecell_get);
1149 	tegra_register_devclks(devclks, ARRAY_SIZE(devclks));
1150 
1151 	tegra_clk_apply_init_table = tegra20_clock_apply_init_table;
1152 
1153 	tegra_cpu_car_ops = &tegra20_cpu_car_ops;
1154 }
1155 CLK_OF_DECLARE(tegra20, "nvidia,tegra20-car", tegra20_clock_init);
1156