blob: 1c06865e06db1fa3bd4043910f54841c2e1f03b5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warrene23bb6a2013-01-28 13:32:10 +00002/*
Tom Warren722e0002015-06-25 09:50:44 -07003 * (C) Copyright 2010-2015
4 * NVIDIA Corporation <www.nvidia.com>
Tom Warrene23bb6a2013-01-28 13:32:10 +00005 */
6
7/* Tegra114 Clock control functions */
8
9#include <common.h>
Simon Glass691d7192020-05-10 11:40:02 -060010#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Tom Warrene23bb6a2013-01-28 13:32:10 +000012#include <asm/io.h>
13#include <asm/arch/clock.h>
Tom Warrenb40f7342013-04-01 15:48:54 -070014#include <asm/arch/sysctr.h>
Tom Warrene23bb6a2013-01-28 13:32:10 +000015#include <asm/arch/tegra.h>
16#include <asm/arch-tegra/clk_rst.h>
17#include <asm/arch-tegra/timer.h>
18#include <div64.h>
19#include <fdtdec.h>
20
21/*
22 * Clock types that we can use as a source. The Tegra114 has muxes for the
23 * peripheral clocks, and in most cases there are four options for the clock
24 * source. This gives us a clock 'type' and exploits what commonality exists
25 * in the device.
26 *
27 * Letters are obvious, except for T which means CLK_M, and S which means the
28 * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
29 * datasheet) and PLL_M are different things. The former is the basic
30 * clock supplied to the SOC from an external oscillator. The latter is the
31 * memory clock PLL.
32 *
33 * See definitions in clock_id in the header file.
34 */
35enum clock_type_id {
36 CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
37 CLOCK_TYPE_MCPA, /* and so on */
38 CLOCK_TYPE_MCPT,
39 CLOCK_TYPE_PCM,
40 CLOCK_TYPE_PCMT,
41 CLOCK_TYPE_PCMT16,
42 CLOCK_TYPE_PDCT,
43 CLOCK_TYPE_ACPT,
44 CLOCK_TYPE_ASPTE,
45 CLOCK_TYPE_PMDACD2T,
46 CLOCK_TYPE_PCST,
47
48 CLOCK_TYPE_COUNT,
49 CLOCK_TYPE_NONE = -1, /* invalid clock type */
50};
51
52enum {
53 CLOCK_MAX_MUX = 8 /* number of source options for each clock */
54};
55
Tom Warrene23bb6a2013-01-28 13:32:10 +000056/*
57 * Clock source mux for each clock type. This just converts our enum into
58 * a list of mux sources for use by the code.
59 *
60 * Note:
61 * The extra column in each clock source array is used to store the mask
62 * bits in its register for the source.
63 */
64#define CLK(x) CLOCK_ID_ ## x
65static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX+1] = {
66 { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(OSC),
67 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
68 MASK_BITS_31_30},
69 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO),
70 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
71 MASK_BITS_31_30},
72 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
73 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
74 MASK_BITS_31_30},
75 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE),
76 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
77 MASK_BITS_31_30},
78 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC),
79 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
80 MASK_BITS_31_30},
81 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC),
82 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
83 MASK_BITS_31_30},
84 { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC),
85 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
86 MASK_BITS_31_30},
87 { CLK(AUDIO), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
88 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
89 MASK_BITS_31_30},
90 { CLK(AUDIO), CLK(SFROM32KHZ), CLK(PERIPH), CLK(OSC),
91 CLK(EPCI), CLK(NONE), CLK(NONE), CLK(NONE),
92 MASK_BITS_31_29},
93 { CLK(PERIPH), CLK(MEMORY), CLK(DISPLAY), CLK(AUDIO),
94 CLK(CGENERAL), CLK(DISPLAY2), CLK(OSC), CLK(NONE),
95 MASK_BITS_31_29},
96 { CLK(PERIPH), CLK(CGENERAL), CLK(SFROM32KHZ), CLK(OSC),
97 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Stephen Warren5916a362014-01-24 10:16:18 -070098 MASK_BITS_31_28}
Tom Warrene23bb6a2013-01-28 13:32:10 +000099};
100
101/*
102 * Clock type for each peripheral clock source. We put the name in each
103 * record just so it is easy to match things up
104 */
105#define TYPE(name, type) type
106static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
107 /* 0x00 */
108 TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
109 TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
110 TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
111 TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PCM),
112 TYPE(PERIPHC_PWM, CLOCK_TYPE_PCST), /* only PWM uses b29:28 */
113 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
114 TYPE(PERIPHC_SBC2, CLOCK_TYPE_PCMT),
115 TYPE(PERIPHC_SBC3, CLOCK_TYPE_PCMT),
116
117 /* 0x08 */
118 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
119 TYPE(PERIPHC_I2C1, CLOCK_TYPE_PCMT16),
120 TYPE(PERIPHC_I2C5, CLOCK_TYPE_PCMT16),
121 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
122 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
123 TYPE(PERIPHC_SBC1, CLOCK_TYPE_PCMT),
124 TYPE(PERIPHC_DISP1, CLOCK_TYPE_PMDACD2T),
125 TYPE(PERIPHC_DISP2, CLOCK_TYPE_PMDACD2T),
126
127 /* 0x10 */
128 TYPE(PERIPHC_CVE, CLOCK_TYPE_PDCT),
129 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
130 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
131 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
132 TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PCMT),
133 TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PCMT),
134 TYPE(PERIPHC_G3D, CLOCK_TYPE_MCPA),
135 TYPE(PERIPHC_G2D, CLOCK_TYPE_MCPA),
136
137 /* 0x18 */
138 TYPE(PERIPHC_NDFLASH, CLOCK_TYPE_PCMT),
139 TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PCMT),
140 TYPE(PERIPHC_VFIR, CLOCK_TYPE_PCMT),
141 TYPE(PERIPHC_EPP, CLOCK_TYPE_MCPA),
142 TYPE(PERIPHC_MPE, CLOCK_TYPE_MCPA),
143 TYPE(PERIPHC_MIPI, CLOCK_TYPE_PCMT), /* MIPI base-band HSI */
144 TYPE(PERIPHC_UART1, CLOCK_TYPE_PCMT),
145 TYPE(PERIPHC_UART2, CLOCK_TYPE_PCMT),
146
147 /* 0x20 */
148 TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MCPA),
149 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
150 TYPE(PERIPHC_TVO, CLOCK_TYPE_PDCT),
151 TYPE(PERIPHC_HDMI, CLOCK_TYPE_PMDACD2T),
152 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
153 TYPE(PERIPHC_TVDAC, CLOCK_TYPE_PDCT),
154 TYPE(PERIPHC_I2C2, CLOCK_TYPE_PCMT16),
155 TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPT),
156
157 /* 0x28 */
158 TYPE(PERIPHC_UART3, CLOCK_TYPE_PCMT),
159 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
160 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
161 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
162 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
163 TYPE(PERIPHC_SBC4, CLOCK_TYPE_PCMT),
164 TYPE(PERIPHC_I2C3, CLOCK_TYPE_PCMT16),
165 TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PCMT),
166
167 /* 0x30 */
168 TYPE(PERIPHC_UART4, CLOCK_TYPE_PCMT),
169 TYPE(PERIPHC_UART5, CLOCK_TYPE_PCMT),
170 TYPE(PERIPHC_VDE, CLOCK_TYPE_PCMT),
171 TYPE(PERIPHC_OWR, CLOCK_TYPE_PCMT),
172 TYPE(PERIPHC_NOR, CLOCK_TYPE_PCMT),
173 TYPE(PERIPHC_CSITE, CLOCK_TYPE_PCMT),
174 TYPE(PERIPHC_I2S0, CLOCK_TYPE_AXPT),
175 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
176
177 /* 0x38h */ /* Jumps to reg offset 0x3B0h */
178 TYPE(PERIPHC_G3D2, CLOCK_TYPE_MCPA),
179 TYPE(PERIPHC_MSELECT, CLOCK_TYPE_PCMT),
180 TYPE(PERIPHC_TSENSOR, CLOCK_TYPE_PCST), /* s/b PCTS */
181 TYPE(PERIPHC_I2S3, CLOCK_TYPE_AXPT),
182 TYPE(PERIPHC_I2S4, CLOCK_TYPE_AXPT),
183 TYPE(PERIPHC_I2C4, CLOCK_TYPE_PCMT16),
184 TYPE(PERIPHC_SBC5, CLOCK_TYPE_PCMT),
185 TYPE(PERIPHC_SBC6, CLOCK_TYPE_PCMT),
186
187 /* 0x40 */
188 TYPE(PERIPHC_AUDIO, CLOCK_TYPE_ACPT),
189 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
190 TYPE(PERIPHC_DAM0, CLOCK_TYPE_ACPT),
191 TYPE(PERIPHC_DAM1, CLOCK_TYPE_ACPT),
192 TYPE(PERIPHC_DAM2, CLOCK_TYPE_ACPT),
193 TYPE(PERIPHC_HDA2CODEC2X, CLOCK_TYPE_PCMT),
194 TYPE(PERIPHC_ACTMON, CLOCK_TYPE_PCST), /* MASK 31:30 */
195 TYPE(PERIPHC_EXTPERIPH1, CLOCK_TYPE_ASPTE),
196
197 /* 0x48 */
198 TYPE(PERIPHC_EXTPERIPH2, CLOCK_TYPE_ASPTE),
199 TYPE(PERIPHC_EXTPERIPH3, CLOCK_TYPE_ASPTE),
200 TYPE(PERIPHC_NANDSPEED, CLOCK_TYPE_PCMT),
201 TYPE(PERIPHC_I2CSLOW, CLOCK_TYPE_PCST), /* MASK 31:30 */
202 TYPE(PERIPHC_SYS, CLOCK_TYPE_NONE),
203 TYPE(PERIPHC_SPEEDO, CLOCK_TYPE_PCMT),
204 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
205 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
206
207 /* 0x50 */
208 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
209 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
210 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
211 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
212 TYPE(PERIPHC_SATAOOB, CLOCK_TYPE_PCMT), /* offset 0x420h */
213 TYPE(PERIPHC_SATA, CLOCK_TYPE_PCMT),
214 TYPE(PERIPHC_HDA, CLOCK_TYPE_PCMT),
215};
216
217/*
218 * This array translates a periph_id to a periphc_internal_id
219 *
220 * Not present/matched up:
221 * uint vi_sensor; _VI_SENSOR_0, 0x1A8
222 * SPDIF - which is both 0x08 and 0x0c
223 *
224 */
225#define NONE(name) (-1)
226#define OFFSET(name, value) PERIPHC_ ## name
227static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
228 /* Low word: 31:0 */
229 NONE(CPU),
230 NONE(COP),
231 NONE(TRIGSYS),
232 NONE(RESERVED3),
233 NONE(RTC),
234 NONE(TMR),
235 PERIPHC_UART1,
236 PERIPHC_UART2, /* and vfir 0x68 */
237
238 /* 8 */
239 NONE(GPIO),
240 PERIPHC_SDMMC2,
241 NONE(SPDIF), /* 0x08 and 0x0c, unclear which to use */
242 PERIPHC_I2S1,
243 PERIPHC_I2C1,
244 PERIPHC_NDFLASH,
245 PERIPHC_SDMMC1,
246 PERIPHC_SDMMC4,
247
248 /* 16 */
249 NONE(RESERVED16),
250 PERIPHC_PWM,
251 PERIPHC_I2S2,
252 PERIPHC_EPP,
253 PERIPHC_VI,
254 PERIPHC_G2D,
255 NONE(USBD),
256 NONE(ISP),
257
258 /* 24 */
259 PERIPHC_G3D,
260 NONE(RESERVED25),
261 PERIPHC_DISP2,
262 PERIPHC_DISP1,
263 PERIPHC_HOST1X,
264 NONE(VCP),
265 PERIPHC_I2S0,
266 NONE(CACHE2),
267
268 /* Middle word: 63:32 */
269 NONE(MEM),
270 NONE(AHBDMA),
271 NONE(APBDMA),
272 NONE(RESERVED35),
273 NONE(RESERVED36),
274 NONE(STAT_MON),
275 NONE(RESERVED38),
276 NONE(RESERVED39),
277
278 /* 40 */
279 NONE(KFUSE),
280 NONE(SBC1), /* SBC1, 0x34, is this SPI1? */
281 PERIPHC_NOR,
282 NONE(RESERVED43),
283 PERIPHC_SBC2,
284 NONE(RESERVED45),
285 PERIPHC_SBC3,
286 PERIPHC_I2C5,
287
288 /* 48 */
289 NONE(DSI),
290 PERIPHC_TVO, /* also CVE 0x40 */
291 PERIPHC_MIPI,
292 PERIPHC_HDMI,
293 NONE(CSI),
294 PERIPHC_TVDAC,
295 PERIPHC_I2C2,
296 PERIPHC_UART3,
297
298 /* 56 */
299 NONE(RESERVED56),
300 PERIPHC_EMC,
301 NONE(USB2),
302 NONE(USB3),
303 PERIPHC_MPE,
304 PERIPHC_VDE,
305 NONE(BSEA),
306 NONE(BSEV),
307
308 /* Upper word 95:64 */
309 PERIPHC_SPEEDO,
310 PERIPHC_UART4,
311 PERIPHC_UART5,
312 PERIPHC_I2C3,
313 PERIPHC_SBC4,
314 PERIPHC_SDMMC3,
315 NONE(PCIE),
316 PERIPHC_OWR,
317
318 /* 72 */
319 NONE(AFI),
320 PERIPHC_CSITE,
321 NONE(PCIEXCLK),
322 NONE(AVPUCQ),
323 NONE(RESERVED76),
324 NONE(RESERVED77),
325 NONE(RESERVED78),
326 NONE(DTV),
327
328 /* 80 */
329 PERIPHC_NANDSPEED,
330 PERIPHC_I2CSLOW,
331 NONE(DSIB),
332 NONE(RESERVED83),
333 NONE(IRAMA),
334 NONE(IRAMB),
335 NONE(IRAMC),
336 NONE(IRAMD),
337
338 /* 88 */
339 NONE(CRAM2),
340 NONE(RESERVED89),
341 NONE(MDOUBLER),
342 NONE(RESERVED91),
343 NONE(SUSOUT),
344 NONE(RESERVED93),
345 NONE(RESERVED94),
346 NONE(RESERVED95),
347
348 /* V word: 31:0 */
349 NONE(CPUG),
350 NONE(CPULP),
351 PERIPHC_G3D2,
352 PERIPHC_MSELECT,
353 PERIPHC_TSENSOR,
354 PERIPHC_I2S3,
355 PERIPHC_I2S4,
356 PERIPHC_I2C4,
357
358 /* 08 */
359 PERIPHC_SBC5,
360 PERIPHC_SBC6,
361 PERIPHC_AUDIO,
362 NONE(APBIF),
363 PERIPHC_DAM0,
364 PERIPHC_DAM1,
365 PERIPHC_DAM2,
366 PERIPHC_HDA2CODEC2X,
367
368 /* 16 */
369 NONE(ATOMICS),
370 NONE(RESERVED17),
371 NONE(RESERVED18),
372 NONE(RESERVED19),
373 NONE(RESERVED20),
374 NONE(RESERVED21),
375 NONE(RESERVED22),
376 PERIPHC_ACTMON,
377
378 /* 24 */
379 NONE(RESERVED24),
380 NONE(RESERVED25),
381 NONE(RESERVED26),
382 NONE(RESERVED27),
383 PERIPHC_SATA,
384 PERIPHC_HDA,
385 NONE(RESERVED30),
386 NONE(RESERVED31),
387
388 /* W word: 31:0 */
389 NONE(HDA2HDMICODEC),
390 NONE(RESERVED1_SATACOLD),
391 NONE(RESERVED2_PCIERX0),
392 NONE(RESERVED3_PCIERX1),
393 NONE(RESERVED4_PCIERX2),
394 NONE(RESERVED5_PCIERX3),
395 NONE(RESERVED6_PCIERX4),
396 NONE(RESERVED7_PCIERX5),
397
398 /* 40 */
399 NONE(CEC),
400 NONE(PCIE2_IOBIST),
401 NONE(EMC_IOBIST),
402 NONE(HDMI_IOBIST),
403 NONE(SATA_IOBIST),
404 NONE(MIPI_IOBIST),
405 NONE(EMC1_IOBIST),
406 NONE(XUSB),
407
408 /* 48 */
409 NONE(CILAB),
410 NONE(CILCD),
411 NONE(CILE),
412 NONE(DSIA_LP),
413 NONE(DSIB_LP),
414 NONE(RESERVED21_ENTROPY),
415 NONE(RESERVED22_W),
416 NONE(RESERVED23_W),
417
418 /* 56 */
419 NONE(RESERVED24_W),
420 NONE(AMX0),
421 NONE(ADX0),
422 NONE(DVFS),
423 NONE(XUSB_SS),
424 NONE(EMC_DLL),
425 NONE(MC1),
426 NONE(EMC1),
427};
428
429/*
Tom Warren722e0002015-06-25 09:50:44 -0700430 * PLL divider shift/mask tables for all PLL IDs.
431 */
432struct clk_pll_info tegra_pll_info_table[CLOCK_ID_PLL_COUNT] = {
433 /*
434 * T114: some deviations from T2x/T30.
435 * NOTE: If kcp_mask/kvco_mask == 0, they're not used in that PLL (PLLX, etc.)
436 * If lock_ena or lock_det are >31, they're not used in that PLL.
437 */
438
439 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x0F,
440 .lock_ena = 24, .lock_det = 27, .kcp_shift = 28, .kcp_mask = 3, .kvco_shift = 27, .kvco_mask = 1 }, /* PLLC */
441 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 0, .p_mask = 0,
442 .lock_ena = 0, .lock_det = 27, .kcp_shift = 1, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLM */
443 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
444 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLP */
445 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
446 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLA */
447 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x01,
448 .lock_ena = 22, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLU */
449 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
450 .lock_ena = 22, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLD */
451 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x0F,
452 .lock_ena = 18, .lock_det = 27, .kcp_shift = 0, .kcp_mask = 0, .kvco_shift = 0, .kvco_mask = 0 }, /* PLLX */
453 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 0, .p_mask = 0,
454 .lock_ena = 9, .lock_det = 11, .kcp_shift = 6, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLE */
455 { .m_shift = 0, .m_mask = 0x0F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
456 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLS (RESERVED) */
457};
458
459/*
Tom Warrene23bb6a2013-01-28 13:32:10 +0000460 * Get the oscillator frequency, from the corresponding hardware configuration
461 * field. Note that T30/T114 support 3 new higher freqs, but we map back
462 * to the old T20 freqs. Support for the higher oscillators is TBD.
463 */
464enum clock_osc_freq clock_get_osc_freq(void)
465{
466 struct clk_rst_ctlr *clkrst =
467 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
468 u32 reg;
469
470 reg = readl(&clkrst->crc_osc_ctrl);
471 reg = (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
472
473 if (reg & 1) /* one of the newer freqs */
474 printf("Warning: OSC_FREQ is unsupported! (%d)\n", reg);
475
476 return reg >> 2; /* Map to most common (T20) freqs */
477}
478
479/* Returns a pointer to the clock source register for a peripheral */
480u32 *get_periph_source_reg(enum periph_id periph_id)
481{
482 struct clk_rst_ctlr *clkrst =
483 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
484 enum periphc_internal_id internal_id;
485
486 /* Coresight is a special case */
487 if (periph_id == PERIPH_ID_CSI)
488 return &clkrst->crc_clk_src[PERIPH_ID_CSI+1];
489
490 assert(periph_id >= PERIPH_ID_FIRST && periph_id < PERIPH_ID_COUNT);
491 internal_id = periph_id_to_internal_id[periph_id];
492 assert(internal_id != -1);
493 if (internal_id >= PERIPHC_VW_FIRST) {
494 internal_id -= PERIPHC_VW_FIRST;
495 return &clkrst->crc_clk_src_vw[internal_id];
496 } else
497 return &clkrst->crc_clk_src[internal_id];
498}
499
Stephen Warrend0ad8a52016-09-13 10:45:56 -0600500int get_periph_clock_info(enum periph_id periph_id, int *mux_bits,
501 int *divider_bits, int *type)
502{
503 enum periphc_internal_id internal_id;
504
505 if (!clock_periph_id_isvalid(periph_id))
506 return -1;
507
508 internal_id = periph_id_to_internal_id[periph_id];
509 if (!periphc_internal_id_isvalid(internal_id))
510 return -1;
511
512 *type = clock_periph_type[internal_id];
513 if (!clock_type_id_isvalid(*type))
514 return -1;
515
516 *mux_bits = clock_source[*type][CLOCK_MAX_MUX];
517
518 if (*type == CLOCK_TYPE_PCMT16)
519 *divider_bits = 16;
520 else
521 *divider_bits = 8;
522
523 return 0;
524}
525
526enum clock_id get_periph_clock_id(enum periph_id periph_id, int source)
527{
528 enum periphc_internal_id internal_id;
529 int type;
530
531 if (!clock_periph_id_isvalid(periph_id))
532 return CLOCK_ID_NONE;
533
534 internal_id = periph_id_to_internal_id[periph_id];
535 if (!periphc_internal_id_isvalid(internal_id))
536 return CLOCK_ID_NONE;
537
538 type = clock_periph_type[internal_id];
539 if (!clock_type_id_isvalid(type))
540 return CLOCK_ID_NONE;
541
542 return clock_source[type][source];
543}
544
Tom Warrene23bb6a2013-01-28 13:32:10 +0000545/**
546 * Given a peripheral ID and the required source clock, this returns which
547 * value should be programmed into the source mux for that peripheral.
548 *
549 * There is special code here to handle the one source type with 5 sources.
550 *
551 * @param periph_id peripheral to start
552 * @param source PLL id of required parent clock
553 * @param mux_bits Set to number of bits in mux register: 2 or 4
554 * @param divider_bits Set to number of divider bits (8 or 16)
555 * @return mux value (0-4, or -1 if not found)
556 */
557int get_periph_clock_source(enum periph_id periph_id,
558 enum clock_id parent, int *mux_bits, int *divider_bits)
559{
560 enum clock_type_id type;
Stephen Warrend0ad8a52016-09-13 10:45:56 -0600561 int mux, err;
Tom Warrene23bb6a2013-01-28 13:32:10 +0000562
Stephen Warrend0ad8a52016-09-13 10:45:56 -0600563 err = get_periph_clock_info(periph_id, mux_bits, divider_bits, &type);
564 assert(!err);
Tom Warrene23bb6a2013-01-28 13:32:10 +0000565
566 for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
567 if (clock_source[type][mux] == parent)
568 return mux;
569
570 /* if we get here, either us or the caller has made a mistake */
571 printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
572 parent);
573 return -1;
574}
575
576void clock_set_enable(enum periph_id periph_id, int enable)
577{
578 struct clk_rst_ctlr *clkrst =
579 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
580 u32 *clk;
581 u32 reg;
582
583 /* Enable/disable the clock to this peripheral */
584 assert(clock_periph_id_isvalid(periph_id));
585 if ((int)periph_id < (int)PERIPH_ID_VW_FIRST)
586 clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
587 else
588 clk = &clkrst->crc_clk_out_enb_vw[PERIPH_REG(periph_id)];
589 reg = readl(clk);
590 if (enable)
591 reg |= PERIPH_MASK(periph_id);
592 else
593 reg &= ~PERIPH_MASK(periph_id);
594 writel(reg, clk);
595}
596
597void reset_set_enable(enum periph_id periph_id, int enable)
598{
599 struct clk_rst_ctlr *clkrst =
600 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
601 u32 *reset;
602 u32 reg;
603
604 /* Enable/disable reset to the peripheral */
605 assert(clock_periph_id_isvalid(periph_id));
606 if (periph_id < PERIPH_ID_VW_FIRST)
607 reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
608 else
609 reset = &clkrst->crc_rst_dev_vw[PERIPH_REG(periph_id)];
610 reg = readl(reset);
611 if (enable)
612 reg |= PERIPH_MASK(periph_id);
613 else
614 reg &= ~PERIPH_MASK(periph_id);
615 writel(reg, reset);
616}
617
Masahiro Yamada0f925822015-08-12 07:31:55 +0900618#if CONFIG_IS_ENABLED(OF_CONTROL)
Tom Warrene23bb6a2013-01-28 13:32:10 +0000619/*
620 * Convert a device tree clock ID to our peripheral ID. They are mostly
621 * the same but we are very cautious so we check that a valid clock ID is
622 * provided.
623 *
624 * @param clk_id Clock ID according to tegra114 device tree binding
625 * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
626 */
627enum periph_id clk_id_to_periph_id(int clk_id)
628{
629 if (clk_id > PERIPH_ID_COUNT)
630 return PERIPH_ID_NONE;
631
632 switch (clk_id) {
633 case PERIPH_ID_RESERVED3:
634 case PERIPH_ID_RESERVED16:
635 case PERIPH_ID_RESERVED24:
636 case PERIPH_ID_RESERVED35:
637 case PERIPH_ID_RESERVED43:
638 case PERIPH_ID_RESERVED45:
639 case PERIPH_ID_RESERVED56:
640 case PERIPH_ID_RESERVED76:
641 case PERIPH_ID_RESERVED77:
642 case PERIPH_ID_RESERVED78:
643 case PERIPH_ID_RESERVED83:
644 case PERIPH_ID_RESERVED89:
645 case PERIPH_ID_RESERVED91:
646 case PERIPH_ID_RESERVED93:
647 case PERIPH_ID_RESERVED94:
648 case PERIPH_ID_RESERVED95:
649 return PERIPH_ID_NONE;
650 default:
651 return clk_id;
652 }
653}
Masahiro Yamada0f925822015-08-12 07:31:55 +0900654#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Tom Warrene23bb6a2013-01-28 13:32:10 +0000655
656void clock_early_init(void)
657{
658 struct clk_rst_ctlr *clkrst =
659 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
Tom Warren722e0002015-06-25 09:50:44 -0700660 struct clk_pll_info *pllinfo;
661 u32 data;
Tom Warrene23bb6a2013-01-28 13:32:10 +0000662
Jimmy Zhangb9dd6212014-01-24 10:37:36 -0700663 tegra30_set_up_pllp();
664
Thierry Reding8e1601d2015-09-08 11:38:04 +0200665 /* clear IDDQ before accessing any other PLLC registers */
666 pllinfo = &tegra_pll_info_table[CLOCK_ID_CGENERAL];
667 clrbits_le32(&clkrst->crc_pll[CLOCK_ID_CGENERAL].pll_misc, PLLC_IDDQ);
668 udelay(2);
669
Tom Warrene23bb6a2013-01-28 13:32:10 +0000670 /*
Tom Warrene23bb6a2013-01-28 13:32:10 +0000671 * PLLC output frequency set to 600Mhz
672 * PLLD output frequency set to 925Mhz
673 */
674 switch (clock_get_osc_freq()) {
675 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
Tom Warrene23bb6a2013-01-28 13:32:10 +0000676 clock_set_rate(CLOCK_ID_CGENERAL, 600, 12, 0, 8);
677 clock_set_rate(CLOCK_ID_DISPLAY, 925, 12, 0, 12);
678 break;
679
680 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
Tom Warrene23bb6a2013-01-28 13:32:10 +0000681 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
682 clock_set_rate(CLOCK_ID_DISPLAY, 925, 26, 0, 12);
683 break;
684
685 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
Tom Warrene23bb6a2013-01-28 13:32:10 +0000686 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
687 clock_set_rate(CLOCK_ID_DISPLAY, 925, 13, 0, 12);
688 break;
689 case CLOCK_OSC_FREQ_19_2:
690 default:
691 /*
692 * These are not supported. It is too early to print a
693 * message and the UART likely won't work anyway due to the
694 * oscillator being wrong.
695 */
696 break;
697 }
698
699 /* PLLC_MISC2: Set dynramp_stepA/B. MISC2 maps to pll_out[1] */
700 writel(0x00561600, &clkrst->crc_pll[CLOCK_ID_CGENERAL].pll_out[1]);
701
702 /* PLLC_MISC: Set LOCK_ENABLE */
Tom Warren722e0002015-06-25 09:50:44 -0700703 pllinfo = &tegra_pll_info_table[CLOCK_ID_CGENERAL];
704 setbits_le32(&clkrst->crc_pll[CLOCK_ID_CGENERAL].pll_misc, (1 << pllinfo->lock_ena));
Tom Warrene23bb6a2013-01-28 13:32:10 +0000705 udelay(2);
706
Tom Warren722e0002015-06-25 09:50:44 -0700707 /* PLLD_MISC: Set CLKENABLE, CPCON 12, LFCON 1, and enable lock */
708 pllinfo = &tegra_pll_info_table[CLOCK_ID_DISPLAY];
709 data = (12 << pllinfo->kcp_shift) | (1 << pllinfo->kvco_shift);
710 data |= (1 << PLLD_CLKENABLE) | (1 << pllinfo->lock_ena);
711 writel(data, &clkrst->crc_pll[CLOCK_ID_DISPLAY].pll_misc);
Tom Warrene23bb6a2013-01-28 13:32:10 +0000712 udelay(2);
713}
Tom Warrenb40f7342013-04-01 15:48:54 -0700714
715void arch_timer_init(void)
716{
717 struct sysctr_ctlr *sysctr = (struct sysctr_ctlr *)NV_PA_TSC_BASE;
718 u32 freq, val;
719
Thierry Reding97c02d82015-08-20 11:42:20 +0200720 freq = clock_get_rate(CLOCK_ID_CLK_M);
721 debug("%s: clk_m freq is %dHz [0x%08X]\n", __func__, freq, freq);
Tom Warrenb40f7342013-04-01 15:48:54 -0700722
723 /* ARM CNTFRQ */
724 asm("mcr p15, 0, %0, c14, c0, 0\n" : : "r" (freq));
725
726 /* Only T114 has the System Counter regs */
727 debug("%s: setting CNTFID0 to 0x%08X\n", __func__, freq);
728 writel(freq, &sysctr->cntfid0);
729
730 val = readl(&sysctr->cntcr);
731 val |= TSC_CNTCR_ENABLE | TSC_CNTCR_HDBG;
732 writel(val, &sysctr->cntcr);
733 debug("%s: TSC CNTCR = 0x%08X\n", __func__, val);
734}
Stephen Warren6dbcc962016-09-13 10:45:55 -0600735
736struct periph_clk_init periph_clk_init_table[] = {
737 { PERIPH_ID_SBC1, CLOCK_ID_PERIPH },
738 { PERIPH_ID_SBC2, CLOCK_ID_PERIPH },
739 { PERIPH_ID_SBC3, CLOCK_ID_PERIPH },
740 { PERIPH_ID_SBC4, CLOCK_ID_PERIPH },
741 { PERIPH_ID_SBC5, CLOCK_ID_PERIPH },
742 { PERIPH_ID_SBC6, CLOCK_ID_PERIPH },
743 { PERIPH_ID_HOST1X, CLOCK_ID_PERIPH },
744 { PERIPH_ID_DISP1, CLOCK_ID_CGENERAL },
745 { PERIPH_ID_NDFLASH, CLOCK_ID_PERIPH },
746 { PERIPH_ID_SDMMC1, CLOCK_ID_PERIPH },
747 { PERIPH_ID_SDMMC2, CLOCK_ID_PERIPH },
748 { PERIPH_ID_SDMMC3, CLOCK_ID_PERIPH },
749 { PERIPH_ID_SDMMC4, CLOCK_ID_PERIPH },
750 { PERIPH_ID_PWM, CLOCK_ID_SFROM32KHZ },
751 { PERIPH_ID_I2C1, CLOCK_ID_PERIPH },
752 { PERIPH_ID_I2C2, CLOCK_ID_PERIPH },
753 { PERIPH_ID_I2C3, CLOCK_ID_PERIPH },
754 { PERIPH_ID_I2C4, CLOCK_ID_PERIPH },
755 { PERIPH_ID_I2C5, CLOCK_ID_PERIPH },
756 { -1, },
757};