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