blob: e5c2fd542ceda67599b495ea371a14d55f8150f0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warren61c6d0e2012-12-11 13:34:15 +00002/*
Tom Warrena8480ef2015-06-25 09:50:44 -07003 * (C) Copyright 2010-2015
4 * NVIDIA Corporation <www.nvidia.com>
Tom Warren61c6d0e2012-12-11 13:34:15 +00005 */
6
7/* Tegra30 Clock control functions */
8
9#include <common.h>
Thierry Reding4bf98692014-12-09 22:25:06 -070010#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Tom Warren61c6d0e2012-12-11 13:34:15 +000013#include <asm/io.h>
14#include <asm/arch/clock.h>
15#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 Warren61c6d0e2012-12-11 13:34:15 +000021
22/*
Tom Warren795f9d72013-01-23 14:01:01 -070023 * Clock types that we can use as a source. The Tegra30 has muxes for the
Tom Warren61c6d0e2012-12-11 13:34:15 +000024 * peripheral clocks, and in most cases there are four options for the clock
25 * source. This gives us a clock 'type' and exploits what commonality exists
26 * in the device.
27 *
28 * Letters are obvious, except for T which means CLK_M, and S which means the
29 * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
30 * datasheet) and PLL_M are different things. The former is the basic
31 * clock supplied to the SOC from an external oscillator. The latter is the
32 * memory clock PLL.
33 *
34 * See definitions in clock_id in the header file.
35 */
36enum clock_type_id {
37 CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
38 CLOCK_TYPE_MCPA, /* and so on */
39 CLOCK_TYPE_MCPT,
40 CLOCK_TYPE_PCM,
41 CLOCK_TYPE_PCMT,
Tom Warrenea226042012-12-21 15:02:45 -070042 CLOCK_TYPE_PCMT16,
Tom Warren61c6d0e2012-12-11 13:34:15 +000043 CLOCK_TYPE_PDCT,
44 CLOCK_TYPE_ACPT,
45 CLOCK_TYPE_ASPTE,
46 CLOCK_TYPE_PMDACD2T,
47 CLOCK_TYPE_PCST,
48
49 CLOCK_TYPE_COUNT,
Tom Warren795f9d72013-01-23 14:01:01 -070050 CLOCK_TYPE_NONE = -1, /* invalid clock type */
Tom Warren61c6d0e2012-12-11 13:34:15 +000051};
52
Tom Warren61c6d0e2012-12-11 13:34:15 +000053enum {
Tom Warren795f9d72013-01-23 14:01:01 -070054 CLOCK_MAX_MUX = 8 /* number of source options for each clock */
Tom Warren61c6d0e2012-12-11 13:34:15 +000055};
56
Tom Warren61c6d0e2012-12-11 13:34:15 +000057/*
58 * Clock source mux for each clock type. This just converts our enum into
59 * a list of mux sources for use by the code.
60 *
61 * Note:
62 * The extra column in each clock source array is used to store the mask
63 * bits in its register for the source.
64 */
65#define CLK(x) CLOCK_ID_ ## x
66static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX+1] = {
Tom Warren795f9d72013-01-23 14:01:01 -070067 { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(OSC),
68 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +000069 MASK_BITS_31_30},
Tom Warren795f9d72013-01-23 14:01:01 -070070 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO),
71 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +000072 MASK_BITS_31_30},
Tom Warren795f9d72013-01-23 14:01:01 -070073 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
74 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +000075 MASK_BITS_31_30},
Tom Warren795f9d72013-01-23 14:01:01 -070076 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE),
77 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +000078 MASK_BITS_31_30},
Tom Warren795f9d72013-01-23 14:01:01 -070079 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC),
80 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +000081 MASK_BITS_31_30},
Tom Warren795f9d72013-01-23 14:01:01 -070082 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC),
83 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenea226042012-12-21 15:02:45 -070084 MASK_BITS_31_30},
Tom Warren795f9d72013-01-23 14:01:01 -070085 { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC),
86 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +000087 MASK_BITS_31_30},
Tom Warren795f9d72013-01-23 14:01:01 -070088 { CLK(AUDIO), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
89 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +000090 MASK_BITS_31_30},
Tom Warren795f9d72013-01-23 14:01:01 -070091 { CLK(AUDIO), CLK(SFROM32KHZ), CLK(PERIPH), CLK(OSC),
92 CLK(EPCI), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +000093 MASK_BITS_31_29},
Tom Warren795f9d72013-01-23 14:01:01 -070094 { CLK(PERIPH), CLK(MEMORY), CLK(DISPLAY), CLK(AUDIO),
95 CLK(CGENERAL), CLK(DISPLAY2), CLK(OSC), CLK(NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +000096 MASK_BITS_31_29},
Tom Warren795f9d72013-01-23 14:01:01 -070097 { CLK(PERIPH), CLK(CGENERAL), CLK(SFROM32KHZ), CLK(OSC),
98 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Stephen Warren510c0ae2014-01-24 10:16:18 -070099 MASK_BITS_31_28}
Tom Warren61c6d0e2012-12-11 13:34:15 +0000100};
101
Tom Warren61c6d0e2012-12-11 13:34:15 +0000102/*
103 * Clock type for each peripheral clock source. We put the name in each
104 * record just so it is easy to match things up
105 */
106#define TYPE(name, type) type
107static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
108 /* 0x00 */
109 TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
Tom Warren795f9d72013-01-23 14:01:01 -0700110 TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
111 TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
112 TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PCM),
113 TYPE(PERIPHC_PWM, CLOCK_TYPE_PCST), /* only PWM uses b29:28 */
114 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
115 TYPE(PERIPHC_SBC2, CLOCK_TYPE_PCMT),
116 TYPE(PERIPHC_SBC3, CLOCK_TYPE_PCMT),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000117
118 /* 0x08 */
Tom Warren795f9d72013-01-23 14:01:01 -0700119 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
120 TYPE(PERIPHC_I2C1, CLOCK_TYPE_PCMT16),
121 TYPE(PERIPHC_DVC_I2C, CLOCK_TYPE_PCMT16),
122 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
123 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
124 TYPE(PERIPHC_SBC1, CLOCK_TYPE_PCMT),
125 TYPE(PERIPHC_DISP1, CLOCK_TYPE_PMDACD2T),
126 TYPE(PERIPHC_DISP2, CLOCK_TYPE_PMDACD2T),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000127
128 /* 0x10 */
Tom Warren795f9d72013-01-23 14:01:01 -0700129 TYPE(PERIPHC_CVE, CLOCK_TYPE_PDCT),
130 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000131 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
Tom Warren795f9d72013-01-23 14:01:01 -0700132 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
133 TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PCMT),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000134 TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PCMT),
135 TYPE(PERIPHC_G3D, CLOCK_TYPE_MCPA),
136 TYPE(PERIPHC_G2D, CLOCK_TYPE_MCPA),
137
138 /* 0x18 */
139 TYPE(PERIPHC_NDFLASH, CLOCK_TYPE_PCMT),
140 TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PCMT),
Tom Warren795f9d72013-01-23 14:01:01 -0700141 TYPE(PERIPHC_VFIR, CLOCK_TYPE_PCMT),
142 TYPE(PERIPHC_EPP, CLOCK_TYPE_MCPA),
143 TYPE(PERIPHC_MPE, CLOCK_TYPE_MCPA),
144 TYPE(PERIPHC_MIPI, CLOCK_TYPE_PCMT), /* MIPI base-band HSI */
145 TYPE(PERIPHC_UART1, CLOCK_TYPE_PCMT),
146 TYPE(PERIPHC_UART2, CLOCK_TYPE_PCMT),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000147
148 /* 0x20 */
Tom Warren795f9d72013-01-23 14:01:01 -0700149 TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MCPA),
150 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
151 TYPE(PERIPHC_TVO, CLOCK_TYPE_PDCT),
152 TYPE(PERIPHC_HDMI, CLOCK_TYPE_PMDACD2T),
153 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
154 TYPE(PERIPHC_TVDAC, CLOCK_TYPE_PDCT),
155 TYPE(PERIPHC_I2C2, CLOCK_TYPE_PCMT16),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000156 TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPT),
157
158 /* 0x28 */
159 TYPE(PERIPHC_UART3, CLOCK_TYPE_PCMT),
160 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
161 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
Tom Warren795f9d72013-01-23 14:01:01 -0700162 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
163 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
164 TYPE(PERIPHC_SBC4, CLOCK_TYPE_PCMT),
165 TYPE(PERIPHC_I2C3, CLOCK_TYPE_PCMT16),
166 TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PCMT),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000167
168 /* 0x30 */
169 TYPE(PERIPHC_UART4, CLOCK_TYPE_PCMT),
170 TYPE(PERIPHC_UART5, CLOCK_TYPE_PCMT),
171 TYPE(PERIPHC_VDE, CLOCK_TYPE_PCMT),
Tom Warren795f9d72013-01-23 14:01:01 -0700172 TYPE(PERIPHC_OWR, CLOCK_TYPE_PCMT),
173 TYPE(PERIPHC_NOR, CLOCK_TYPE_PCMT),
174 TYPE(PERIPHC_CSITE, CLOCK_TYPE_PCMT),
175 TYPE(PERIPHC_I2S0, CLOCK_TYPE_AXPT),
176 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000177
Tom Warren795f9d72013-01-23 14:01:01 -0700178 /* 0x38h */ /* Jumps to reg offset 0x3B0h - new for T30 */
179 TYPE(PERIPHC_G3D2, CLOCK_TYPE_MCPA),
180 TYPE(PERIPHC_MSELECT, CLOCK_TYPE_PCMT),
181 TYPE(PERIPHC_TSENSOR, CLOCK_TYPE_PCST), /* s/b PCTS */
182 TYPE(PERIPHC_I2S3, CLOCK_TYPE_AXPT),
183 TYPE(PERIPHC_I2S4, CLOCK_TYPE_AXPT),
184 TYPE(PERIPHC_I2C4, CLOCK_TYPE_PCMT16),
185 TYPE(PERIPHC_SBC5, CLOCK_TYPE_PCMT),
186 TYPE(PERIPHC_SBC6, CLOCK_TYPE_PCMT),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000187
188 /* 0x40 */
Tom Warren795f9d72013-01-23 14:01:01 -0700189 TYPE(PERIPHC_AUDIO, CLOCK_TYPE_ACPT),
190 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
191 TYPE(PERIPHC_DAM0, CLOCK_TYPE_ACPT),
192 TYPE(PERIPHC_DAM1, CLOCK_TYPE_ACPT),
193 TYPE(PERIPHC_DAM2, CLOCK_TYPE_ACPT),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000194 TYPE(PERIPHC_HDA2CODEC2X, CLOCK_TYPE_PCMT),
Tom Warren795f9d72013-01-23 14:01:01 -0700195 TYPE(PERIPHC_ACTMON, CLOCK_TYPE_PCST), /* MASK 31:30 */
Tom Warren61c6d0e2012-12-11 13:34:15 +0000196 TYPE(PERIPHC_EXTPERIPH1, CLOCK_TYPE_ASPTE),
197
198 /* 0x48 */
199 TYPE(PERIPHC_EXTPERIPH2, CLOCK_TYPE_ASPTE),
200 TYPE(PERIPHC_EXTPERIPH3, CLOCK_TYPE_ASPTE),
Tom Warren795f9d72013-01-23 14:01:01 -0700201 TYPE(PERIPHC_NANDSPEED, CLOCK_TYPE_PCMT),
202 TYPE(PERIPHC_I2CSLOW, CLOCK_TYPE_PCST), /* MASK 31:30 */
203 TYPE(PERIPHC_SYS, CLOCK_TYPE_NONE),
204 TYPE(PERIPHC_SPEEDO, CLOCK_TYPE_PCMT),
205 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
206 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000207
208 /* 0x50 */
Tom Warren795f9d72013-01-23 14:01:01 -0700209 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
210 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
211 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
212 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
213 TYPE(PERIPHC_SATAOOB, CLOCK_TYPE_PCMT), /* offset 0x420h */
214 TYPE(PERIPHC_SATA, CLOCK_TYPE_PCMT),
215 TYPE(PERIPHC_HDA, CLOCK_TYPE_PCMT),
Tom Warren61c6d0e2012-12-11 13:34:15 +0000216};
217
218/*
219 * This array translates a periph_id to a periphc_internal_id
220 *
221 * Not present/matched up:
222 * uint vi_sensor; _VI_SENSOR_0, 0x1A8
223 * SPDIF - which is both 0x08 and 0x0c
224 *
225 */
226#define NONE(name) (-1)
227#define OFFSET(name, value) PERIPHC_ ## name
228static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
229 /* Low word: 31:0 */
230 NONE(CPU),
231 NONE(COP),
232 NONE(TRIGSYS),
233 NONE(RESERVED3),
234 NONE(RESERVED4),
235 NONE(TMR),
236 PERIPHC_UART1,
Tom Warren795f9d72013-01-23 14:01:01 -0700237 PERIPHC_UART2, /* and vfir 0x68 */
Tom Warren61c6d0e2012-12-11 13:34:15 +0000238
239 /* 8 */
240 NONE(GPIO),
241 PERIPHC_SDMMC2,
Tom Warren795f9d72013-01-23 14:01:01 -0700242 NONE(SPDIF), /* 0x08 and 0x0c, unclear which to use */
Tom Warren61c6d0e2012-12-11 13:34:15 +0000243 PERIPHC_I2S1,
244 PERIPHC_I2C1,
245 PERIPHC_NDFLASH,
246 PERIPHC_SDMMC1,
247 PERIPHC_SDMMC4,
248
249 /* 16 */
250 NONE(RESERVED16),
251 PERIPHC_PWM,
252 PERIPHC_I2S2,
253 PERIPHC_EPP,
254 PERIPHC_VI,
255 PERIPHC_G2D,
256 NONE(USBD),
257 NONE(ISP),
258
259 /* 24 */
260 PERIPHC_G3D,
261 NONE(RESERVED25),
262 PERIPHC_DISP2,
263 PERIPHC_DISP1,
264 PERIPHC_HOST1X,
265 NONE(VCP),
266 PERIPHC_I2S0,
267 NONE(CACHE2),
268
269 /* Middle word: 63:32 */
270 NONE(MEM),
271 NONE(AHBDMA),
272 NONE(APBDMA),
273 NONE(RESERVED35),
274 NONE(RESERVED36),
275 NONE(STAT_MON),
276 NONE(RESERVED38),
277 NONE(RESERVED39),
278
279 /* 40 */
280 NONE(KFUSE),
Allen Martin3f419f82013-01-29 13:51:25 +0000281 PERIPHC_SBC1,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000282 PERIPHC_NOR,
283 NONE(RESERVED43),
284 PERIPHC_SBC2,
285 NONE(RESERVED45),
286 PERIPHC_SBC3,
287 PERIPHC_DVC_I2C,
288
289 /* 48 */
290 NONE(DSI),
Tom Warren795f9d72013-01-23 14:01:01 -0700291 PERIPHC_TVO, /* also CVE 0x40 */
Tom Warren61c6d0e2012-12-11 13:34:15 +0000292 PERIPHC_MIPI,
293 PERIPHC_HDMI,
294 NONE(CSI),
295 PERIPHC_TVDAC,
296 PERIPHC_I2C2,
297 PERIPHC_UART3,
298
299 /* 56 */
300 NONE(RESERVED56),
301 PERIPHC_EMC,
302 NONE(USB2),
303 NONE(USB3),
304 PERIPHC_MPE,
305 PERIPHC_VDE,
306 NONE(BSEA),
307 NONE(BSEV),
308
309 /* Upper word 95:64 */
310 PERIPHC_SPEEDO,
311 PERIPHC_UART4,
312 PERIPHC_UART5,
313 PERIPHC_I2C3,
314 PERIPHC_SBC4,
315 PERIPHC_SDMMC3,
316 NONE(PCIE),
317 PERIPHC_OWR,
318
319 /* 72 */
320 NONE(AFI),
321 PERIPHC_CSITE,
322 NONE(PCIEXCLK),
323 NONE(AVPUCQ),
324 NONE(RESERVED76),
325 NONE(RESERVED77),
326 NONE(RESERVED78),
327 NONE(DTV),
328
329 /* 80 */
330 PERIPHC_NANDSPEED,
331 PERIPHC_I2CSLOW,
332 NONE(DSIB),
333 NONE(RESERVED83),
334 NONE(IRAMA),
335 NONE(IRAMB),
336 NONE(IRAMC),
337 NONE(IRAMD),
338
339 /* 88 */
340 NONE(CRAM2),
341 NONE(RESERVED89),
342 NONE(MDOUBLER),
343 NONE(RESERVED91),
344 NONE(SUSOUT),
345 NONE(RESERVED93),
346 NONE(RESERVED94),
347 NONE(RESERVED95),
348
349 /* V word: 31:0 */
350 NONE(CPUG),
351 NONE(CPULP),
352 PERIPHC_G3D2,
353 PERIPHC_MSELECT,
354 PERIPHC_TSENSOR,
355 PERIPHC_I2S3,
356 PERIPHC_I2S4,
357 PERIPHC_I2C4,
358
359 /* 08 */
360 PERIPHC_SBC5,
361 PERIPHC_SBC6,
362 PERIPHC_AUDIO,
363 NONE(APBIF),
364 PERIPHC_DAM0,
365 PERIPHC_DAM1,
366 PERIPHC_DAM2,
367 PERIPHC_HDA2CODEC2X,
368
369 /* 16 */
370 NONE(ATOMICS),
371 NONE(RESERVED17),
372 NONE(RESERVED18),
373 NONE(RESERVED19),
374 NONE(RESERVED20),
375 NONE(RESERVED21),
376 NONE(RESERVED22),
377 PERIPHC_ACTMON,
378
379 /* 24 */
Svyatoslav Ryheld956f352023-02-14 19:35:23 +0200380 PERIPHC_EXTPERIPH1,
381 PERIPHC_EXTPERIPH2,
382 PERIPHC_EXTPERIPH3,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000383 NONE(RESERVED27),
384 PERIPHC_SATA,
385 PERIPHC_HDA,
386 NONE(RESERVED30),
387 NONE(RESERVED31),
388
389 /* W word: 31:0 */
390 NONE(HDA2HDMICODEC),
391 NONE(SATACOLD),
392 NONE(RESERVED0_PCIERX0),
393 NONE(RESERVED1_PCIERX1),
394 NONE(RESERVED2_PCIERX2),
395 NONE(RESERVED3_PCIERX3),
396 NONE(RESERVED4_PCIERX4),
397 NONE(RESERVED5_PCIERX5),
398
399 /* 40 */
400 NONE(CEC),
401 NONE(RESERVED6_PCIE2),
402 NONE(RESERVED7_EMC),
403 NONE(RESERVED8_HDMI),
404 NONE(RESERVED9_SATA),
405 NONE(RESERVED10_MIPI),
406 NONE(EX_RESERVED46),
407 NONE(EX_RESERVED47),
408};
409
410/*
Tom Warrena8480ef2015-06-25 09:50:44 -0700411 * PLL divider shift/mask tables for all PLL IDs.
412 */
413struct clk_pll_info tegra_pll_info_table[CLOCK_ID_PLL_COUNT] = {
414 /*
415 * T30: some deviations from T2x.
416 * NOTE: If kcp_mask/kvco_mask == 0, they're not used in that PLL (PLLX, etc.)
417 * If lock_ena or lock_det are >31, they're not used in that PLL.
418 */
419
420 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x0F,
421 .lock_ena = 24, .lock_det = 27, .kcp_shift = 28, .kcp_mask = 3, .kvco_shift = 27, .kvco_mask = 1 }, /* PLLC */
422 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 0, .p_mask = 0,
423 .lock_ena = 0, .lock_det = 27, .kcp_shift = 1, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLM */
424 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
425 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLP */
426 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
427 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLA */
428 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x01,
429 .lock_ena = 22, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLU */
430 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
431 .lock_ena = 22, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLD */
432 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x0F,
433 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 0, .kvco_mask = 0 }, /* PLLX */
434 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 0, .p_mask = 0,
435 .lock_ena = 9, .lock_det = 11, .kcp_shift = 6, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLE */
436 { .m_shift = 0, .m_mask = 0x0F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
437 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLS (RESERVED) */
438};
439
440/*
Tom Warren61c6d0e2012-12-11 13:34:15 +0000441 * Get the oscillator frequency, from the corresponding hardware configuration
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200442 * field. Note that T30+ supports 3 new higher freqs.
Tom Warren61c6d0e2012-12-11 13:34:15 +0000443 */
444enum clock_osc_freq clock_get_osc_freq(void)
445{
446 struct clk_rst_ctlr *clkrst =
447 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
448 u32 reg;
449
450 reg = readl(&clkrst->crc_osc_ctrl);
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200451 return (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
Tom Warren61c6d0e2012-12-11 13:34:15 +0000452}
453
454/* Returns a pointer to the clock source register for a peripheral */
Tom Warren795f9d72013-01-23 14:01:01 -0700455u32 *get_periph_source_reg(enum periph_id periph_id)
Tom Warren61c6d0e2012-12-11 13:34:15 +0000456{
457 struct clk_rst_ctlr *clkrst =
Tom Warren795f9d72013-01-23 14:01:01 -0700458 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
Tom Warren61c6d0e2012-12-11 13:34:15 +0000459 enum periphc_internal_id internal_id;
460
461 /* Coresight is a special case */
462 if (periph_id == PERIPH_ID_CSI)
463 return &clkrst->crc_clk_src[PERIPH_ID_CSI+1];
464
465 assert(periph_id >= PERIPH_ID_FIRST && periph_id < PERIPH_ID_COUNT);
466 internal_id = periph_id_to_internal_id[periph_id];
467 assert(internal_id != -1);
468 if (internal_id >= PERIPHC_VW_FIRST) {
469 internal_id -= PERIPHC_VW_FIRST;
470 return &clkrst->crc_clk_src_vw[internal_id];
471 } else
472 return &clkrst->crc_clk_src[internal_id];
473}
474
Stephen Warren532543c2016-09-13 10:45:56 -0600475int get_periph_clock_info(enum periph_id periph_id, int *mux_bits,
476 int *divider_bits, int *type)
477{
478 enum periphc_internal_id internal_id;
479
480 if (!clock_periph_id_isvalid(periph_id))
481 return -1;
482
483 internal_id = periph_id_to_internal_id[periph_id];
484 if (!periphc_internal_id_isvalid(internal_id))
485 return -1;
486
487 *type = clock_periph_type[internal_id];
488 if (!clock_type_id_isvalid(*type))
489 return -1;
490
491 *mux_bits = clock_source[*type][CLOCK_MAX_MUX];
492
493 if (*type == CLOCK_TYPE_PCMT16)
494 *divider_bits = 16;
495 else
496 *divider_bits = 8;
497
498 return 0;
499}
500
501enum clock_id get_periph_clock_id(enum periph_id periph_id, int source)
502{
503 enum periphc_internal_id internal_id;
504 int type;
505
506 if (!clock_periph_id_isvalid(periph_id))
507 return CLOCK_ID_NONE;
508
509 internal_id = periph_id_to_internal_id[periph_id];
510 if (!periphc_internal_id_isvalid(internal_id))
511 return CLOCK_ID_NONE;
512
513 type = clock_periph_type[internal_id];
514 if (!clock_type_id_isvalid(type))
515 return CLOCK_ID_NONE;
516
517 return clock_source[type][source];
518}
519
Tom Warren61c6d0e2012-12-11 13:34:15 +0000520/**
521 * Given a peripheral ID and the required source clock, this returns which
522 * value should be programmed into the source mux for that peripheral.
523 *
524 * There is special code here to handle the one source type with 5 sources.
525 *
526 * @param periph_id peripheral to start
527 * @param source PLL id of required parent clock
528 * @param mux_bits Set to number of bits in mux register: 2 or 4
Tom Warren795f9d72013-01-23 14:01:01 -0700529 * @param divider_bits Set to number of divider bits (8 or 16)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100530 * Return: mux value (0-4, or -1 if not found)
Tom Warren61c6d0e2012-12-11 13:34:15 +0000531 */
Tom Warren795f9d72013-01-23 14:01:01 -0700532int get_periph_clock_source(enum periph_id periph_id,
533 enum clock_id parent, int *mux_bits, int *divider_bits)
Tom Warren61c6d0e2012-12-11 13:34:15 +0000534{
535 enum clock_type_id type;
Stephen Warren532543c2016-09-13 10:45:56 -0600536 int mux, err;
Tom Warren61c6d0e2012-12-11 13:34:15 +0000537
Stephen Warren532543c2016-09-13 10:45:56 -0600538 err = get_periph_clock_info(periph_id, mux_bits, divider_bits, &type);
539 assert(!err);
Tom Warrenea226042012-12-21 15:02:45 -0700540
Tom Warren61c6d0e2012-12-11 13:34:15 +0000541 for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
542 if (clock_source[type][mux] == parent)
543 return mux;
544
545 /* if we get here, either us or the caller has made a mistake */
546 printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
547 parent);
548 return -1;
549}
550
Tom Warren61c6d0e2012-12-11 13:34:15 +0000551void clock_set_enable(enum periph_id periph_id, int enable)
552{
553 struct clk_rst_ctlr *clkrst =
554 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
555 u32 *clk;
556 u32 reg;
557
558 /* Enable/disable the clock to this peripheral */
559 assert(clock_periph_id_isvalid(periph_id));
560 if ((int)periph_id < (int)PERIPH_ID_VW_FIRST)
561 clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
562 else
563 clk = &clkrst->crc_clk_out_enb_vw[PERIPH_REG(periph_id)];
564 reg = readl(clk);
565 if (enable)
566 reg |= PERIPH_MASK(periph_id);
567 else
568 reg &= ~PERIPH_MASK(periph_id);
569 writel(reg, clk);
570}
571
Tom Warren61c6d0e2012-12-11 13:34:15 +0000572void reset_set_enable(enum periph_id periph_id, int enable)
573{
574 struct clk_rst_ctlr *clkrst =
575 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
576 u32 *reset;
577 u32 reg;
578
579 /* Enable/disable reset to the peripheral */
580 assert(clock_periph_id_isvalid(periph_id));
581 if (periph_id < PERIPH_ID_VW_FIRST)
582 reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
583 else
584 reset = &clkrst->crc_rst_dev_vw[PERIPH_REG(periph_id)];
585 reg = readl(reset);
586 if (enable)
587 reg |= PERIPH_MASK(periph_id);
588 else
589 reg &= ~PERIPH_MASK(periph_id);
590 writel(reg, reset);
591}
592
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900593#if CONFIG_IS_ENABLED(OF_CONTROL)
Tom Warren61c6d0e2012-12-11 13:34:15 +0000594/*
595 * Convert a device tree clock ID to our peripheral ID. They are mostly
596 * the same but we are very cautious so we check that a valid clock ID is
597 * provided.
598 *
Tom Warrenea226042012-12-21 15:02:45 -0700599 * @param clk_id Clock ID according to tegra30 device tree binding
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100600 * Return: peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
Tom Warren61c6d0e2012-12-11 13:34:15 +0000601 */
Tom Warren795f9d72013-01-23 14:01:01 -0700602enum periph_id clk_id_to_periph_id(int clk_id)
Tom Warren61c6d0e2012-12-11 13:34:15 +0000603{
Tom Warrenea226042012-12-21 15:02:45 -0700604 if (clk_id > PERIPH_ID_COUNT)
Tom Warren61c6d0e2012-12-11 13:34:15 +0000605 return PERIPH_ID_NONE;
606
607 switch (clk_id) {
Tom Warrenea226042012-12-21 15:02:45 -0700608 case PERIPH_ID_RESERVED3:
609 case PERIPH_ID_RESERVED4:
610 case PERIPH_ID_RESERVED16:
611 case PERIPH_ID_RESERVED24:
612 case PERIPH_ID_RESERVED35:
613 case PERIPH_ID_RESERVED43:
614 case PERIPH_ID_RESERVED45:
615 case PERIPH_ID_RESERVED56:
Thierry Reding289fc682014-12-09 22:25:07 -0700616 case PERIPH_ID_PCIEXCLK:
Tom Warrenea226042012-12-21 15:02:45 -0700617 case PERIPH_ID_RESERVED76:
618 case PERIPH_ID_RESERVED77:
619 case PERIPH_ID_RESERVED78:
620 case PERIPH_ID_RESERVED83:
621 case PERIPH_ID_RESERVED89:
622 case PERIPH_ID_RESERVED91:
623 case PERIPH_ID_RESERVED93:
624 case PERIPH_ID_RESERVED94:
625 case PERIPH_ID_RESERVED95:
Tom Warren61c6d0e2012-12-11 13:34:15 +0000626 return PERIPH_ID_NONE;
627 default:
628 return clk_id;
629 }
630}
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900631#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Tom Warren61c6d0e2012-12-11 13:34:15 +0000632
Tom Warren61c6d0e2012-12-11 13:34:15 +0000633void clock_early_init(void)
634{
Jimmy Zhang2a544db2014-01-24 10:37:36 -0700635 tegra30_set_up_pllp();
Tom Warren61c6d0e2012-12-11 13:34:15 +0000636}
Tom Warrenfbef3552013-04-01 15:48:54 -0700637
638void arch_timer_init(void)
639{
640}
Thierry Reding4bf98692014-12-09 22:25:06 -0700641
642#define PMC_SATA_PWRGT 0x1ac
643#define PMC_SATA_PWRGT_PLLE_IDDQ_OVERRIDE (1 << 5)
644#define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL (1 << 4)
645
646#define PLLE_SS_CNTL 0x68
647#define PLLE_SS_CNTL_SSCINCINTRV(x) (((x) & 0x3f) << 24)
648#define PLLE_SS_CNTL_SSCINC(x) (((x) & 0xff) << 16)
649#define PLLE_SS_CNTL_SSCBYP (1 << 12)
650#define PLLE_SS_CNTL_INTERP_RESET (1 << 11)
651#define PLLE_SS_CNTL_BYPASS_SS (1 << 10)
652#define PLLE_SS_CNTL_SSCMAX(x) (((x) & 0x1ff) << 0)
653
654#define PLLE_BASE 0x0e8
655#define PLLE_BASE_ENABLE_CML (1 << 31)
656#define PLLE_BASE_ENABLE (1 << 30)
657#define PLLE_BASE_PLDIV_CML(x) (((x) & 0xf) << 24)
658#define PLLE_BASE_PLDIV(x) (((x) & 0x3f) << 16)
659#define PLLE_BASE_NDIV(x) (((x) & 0xff) << 8)
660#define PLLE_BASE_MDIV(x) (((x) & 0xff) << 0)
661
662#define PLLE_MISC 0x0ec
663#define PLLE_MISC_SETUP_BASE(x) (((x) & 0xffff) << 16)
664#define PLLE_MISC_PLL_READY (1 << 15)
665#define PLLE_MISC_LOCK (1 << 11)
666#define PLLE_MISC_LOCK_ENABLE (1 << 9)
667#define PLLE_MISC_SETUP_EXT(x) (((x) & 0x3) << 2)
668
669static int tegra_plle_train(void)
670{
671 unsigned int timeout = 2000;
672 unsigned long value;
673
674 value = readl(NV_PA_PMC_BASE + PMC_SATA_PWRGT);
675 value |= PMC_SATA_PWRGT_PLLE_IDDQ_OVERRIDE;
676 writel(value, NV_PA_PMC_BASE + PMC_SATA_PWRGT);
677
678 value = readl(NV_PA_PMC_BASE + PMC_SATA_PWRGT);
679 value |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
680 writel(value, NV_PA_PMC_BASE + PMC_SATA_PWRGT);
681
682 value = readl(NV_PA_PMC_BASE + PMC_SATA_PWRGT);
683 value &= ~PMC_SATA_PWRGT_PLLE_IDDQ_OVERRIDE;
684 writel(value, NV_PA_PMC_BASE + PMC_SATA_PWRGT);
685
686 do {
687 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
688 if (value & PLLE_MISC_PLL_READY)
689 break;
690
691 udelay(100);
692 } while (--timeout);
693
694 if (timeout == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900695 pr_err("timeout waiting for PLLE to become ready");
Thierry Reding4bf98692014-12-09 22:25:06 -0700696 return -ETIMEDOUT;
697 }
698
699 return 0;
700}
701
702int tegra_plle_enable(void)
703{
704 unsigned int cpcon = 11, p = 18, n = 150, m = 1, timeout = 1000;
705 u32 value;
706 int err;
707
708 /* disable PLLE clock */
709 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
710 value &= ~PLLE_BASE_ENABLE_CML;
711 value &= ~PLLE_BASE_ENABLE;
712 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
713
714 /* clear lock enable and setup field */
715 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
716 value &= ~PLLE_MISC_LOCK_ENABLE;
717 value &= ~PLLE_MISC_SETUP_BASE(0xffff);
718 value &= ~PLLE_MISC_SETUP_EXT(0x3);
719 writel(value, NV_PA_CLK_RST_BASE + PLLE_MISC);
720
721 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
722 if ((value & PLLE_MISC_PLL_READY) == 0) {
723 err = tegra_plle_train();
724 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900725 pr_err("failed to train PLLE: %d", err);
Thierry Reding4bf98692014-12-09 22:25:06 -0700726 return err;
727 }
728 }
729
730 /* configure PLLE */
731 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
732
733 value &= ~PLLE_BASE_PLDIV_CML(0x0f);
734 value |= PLLE_BASE_PLDIV_CML(cpcon);
735
736 value &= ~PLLE_BASE_PLDIV(0x3f);
737 value |= PLLE_BASE_PLDIV(p);
738
739 value &= ~PLLE_BASE_NDIV(0xff);
740 value |= PLLE_BASE_NDIV(n);
741
742 value &= ~PLLE_BASE_MDIV(0xff);
743 value |= PLLE_BASE_MDIV(m);
744
745 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
746
747 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
748 value |= PLLE_MISC_SETUP_BASE(0x7);
749 value |= PLLE_MISC_LOCK_ENABLE;
750 value |= PLLE_MISC_SETUP_EXT(0);
751 writel(value, NV_PA_CLK_RST_BASE + PLLE_MISC);
752
753 value = readl(NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
754 value |= PLLE_SS_CNTL_SSCBYP | PLLE_SS_CNTL_INTERP_RESET |
755 PLLE_SS_CNTL_BYPASS_SS;
756 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
757
758 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
759 value |= PLLE_BASE_ENABLE_CML | PLLE_BASE_ENABLE;
760 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
761
762 do {
763 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
764 if (value & PLLE_MISC_LOCK)
765 break;
766
767 udelay(2);
768 } while (--timeout);
769
770 if (timeout == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900771 pr_err("timeout waiting for PLLE to lock");
Thierry Reding4bf98692014-12-09 22:25:06 -0700772 return -ETIMEDOUT;
773 }
774
775 udelay(50);
776
777 value = readl(NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
778 value &= ~PLLE_SS_CNTL_SSCINCINTRV(0x3f);
779 value |= PLLE_SS_CNTL_SSCINCINTRV(0x18);
780
781 value &= ~PLLE_SS_CNTL_SSCINC(0xff);
782 value |= PLLE_SS_CNTL_SSCINC(0x01);
783
784 value &= ~PLLE_SS_CNTL_SSCBYP;
785 value &= ~PLLE_SS_CNTL_INTERP_RESET;
786 value &= ~PLLE_SS_CNTL_BYPASS_SS;
787
788 value &= ~PLLE_SS_CNTL_SSCMAX(0x1ff);
789 value |= PLLE_SS_CNTL_SSCMAX(0x24);
790 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
791
792 return 0;
793}
Stephen Warren1453d102016-09-13 10:45:55 -0600794
795struct periph_clk_init periph_clk_init_table[] = {
796 { PERIPH_ID_SBC1, CLOCK_ID_PERIPH },
797 { PERIPH_ID_SBC2, CLOCK_ID_PERIPH },
798 { PERIPH_ID_SBC3, CLOCK_ID_PERIPH },
799 { PERIPH_ID_SBC4, CLOCK_ID_PERIPH },
800 { PERIPH_ID_SBC5, CLOCK_ID_PERIPH },
801 { PERIPH_ID_SBC6, CLOCK_ID_PERIPH },
Svyatoslav Ryhel932ec722023-02-14 19:35:24 +0200802 { PERIPH_ID_HOST1X, CLOCK_ID_CGENERAL },
803 { PERIPH_ID_DISP1, CLOCK_ID_PERIPH },
Stephen Warren1453d102016-09-13 10:45:55 -0600804 { PERIPH_ID_NDFLASH, CLOCK_ID_PERIPH },
805 { PERIPH_ID_SDMMC1, CLOCK_ID_PERIPH },
806 { PERIPH_ID_SDMMC2, CLOCK_ID_PERIPH },
807 { PERIPH_ID_SDMMC3, CLOCK_ID_PERIPH },
808 { PERIPH_ID_SDMMC4, CLOCK_ID_PERIPH },
809 { PERIPH_ID_PWM, CLOCK_ID_SFROM32KHZ },
810 { PERIPH_ID_DVC_I2C, CLOCK_ID_PERIPH },
811 { PERIPH_ID_I2C1, CLOCK_ID_PERIPH },
812 { PERIPH_ID_I2C2, CLOCK_ID_PERIPH },
813 { PERIPH_ID_I2C3, CLOCK_ID_PERIPH },
814 { PERIPH_ID_I2C4, CLOCK_ID_PERIPH },
815 { -1, },
816};