blob: 171c98daa2e23c0a9e235f7c34d24804f94c23c5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warren6c43f6c2015-02-02 13:22:29 -07002/*
Tom Warrend43c1dc2020-03-27 10:24:31 -07003 * (C) Copyright 2013-2020
Tom Warren6c43f6c2015-02-02 13:22:29 -07004 * NVIDIA Corporation <www.nvidia.com>
Tom Warren6c43f6c2015-02-02 13:22:29 -07005 */
6
7/* Tegra210 Clock control functions */
8
9#include <common.h>
Stephen Warrendfa551e2015-10-05 16:58:52 -060010#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060011#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060013#include <asm/cache.h>
Tom Warren6c43f6c2015-02-02 13:22:29 -070014#include <asm/io.h>
15#include <asm/arch/clock.h>
16#include <asm/arch/sysctr.h>
17#include <asm/arch/tegra.h>
18#include <asm/arch-tegra/clk_rst.h>
19#include <asm/arch-tegra/timer.h>
20#include <div64.h>
21#include <fdtdec.h>
22
23/*
24 * Clock types that we can use as a source. The Tegra210 has muxes for the
25 * peripheral clocks, and in most cases there are four options for the clock
26 * source. This gives us a clock 'type' and exploits what commonality exists
27 * in the device.
28 *
29 * Letters are obvious, except for T which means CLK_M, and S which means the
30 * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
31 * datasheet) and PLL_M are different things. The former is the basic
32 * clock supplied to the SOC from an external oscillator. The latter is the
33 * memory clock PLL.
34 *
35 * See definitions in clock_id in the header file.
36 */
37enum clock_type_id {
38 CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
39 CLOCK_TYPE_MCPA, /* and so on */
40 CLOCK_TYPE_MCPT,
41 CLOCK_TYPE_PCM,
42 CLOCK_TYPE_PCMT,
43 CLOCK_TYPE_PDCT,
44 CLOCK_TYPE_ACPT,
45 CLOCK_TYPE_ASPTE,
Thierry Redingbca79102019-04-15 11:32:15 +020046 CLOCK_TYPE_PDD2T,
Tom Warren6c43f6c2015-02-02 13:22:29 -070047 CLOCK_TYPE_PCST,
Simon Glass5a30cee2015-08-10 07:14:36 -060048 CLOCK_TYPE_DP,
Tom Warren6c43f6c2015-02-02 13:22:29 -070049
50 CLOCK_TYPE_PC2CC3M,
51 CLOCK_TYPE_PC2CC3S_T,
52 CLOCK_TYPE_PC2CC3M_T,
53 CLOCK_TYPE_PC2CC3M_T16, /* PC2CC3M_T, but w/16-bit divisor (I2C) */
54 CLOCK_TYPE_MC2CC3P_A,
55 CLOCK_TYPE_M,
56 CLOCK_TYPE_MCPTM2C2C3,
57 CLOCK_TYPE_PC2CC3T_S,
58 CLOCK_TYPE_AC2CC3P_TS2,
59 CLOCK_TYPE_PC01C00_C42C41TC40,
60
61 CLOCK_TYPE_COUNT,
62 CLOCK_TYPE_NONE = -1, /* invalid clock type */
63};
64
65enum {
66 CLOCK_MAX_MUX = 8 /* number of source options for each clock */
67};
68
69/*
70 * Clock source mux for each clock type. This just converts our enum into
71 * a list of mux sources for use by the code.
72 *
73 * Note:
74 * The extra column in each clock source array is used to store the mask
75 * bits in its register for the source.
76 */
77#define CLK(x) CLOCK_ID_ ## x
78static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX+1] = {
79 { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(OSC),
80 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
81 MASK_BITS_31_30},
82 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO),
83 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
84 MASK_BITS_31_30},
85 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
86 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
87 MASK_BITS_31_30},
88 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE),
89 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
90 MASK_BITS_31_30},
91 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC),
92 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
93 MASK_BITS_31_30},
94 { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC),
95 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
96 MASK_BITS_31_30},
97 { CLK(AUDIO), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
98 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
99 MASK_BITS_31_30},
100 { CLK(AUDIO), CLK(SFROM32KHZ), CLK(PERIPH), CLK(OSC),
101 CLK(EPCI), CLK(NONE), CLK(NONE), CLK(NONE),
102 MASK_BITS_31_29},
Thierry Redingbca79102019-04-15 11:32:15 +0200103 { CLK(PERIPH), CLK(NONE), CLK(DISPLAY), CLK(NONE),
104 CLK(NONE), CLK(DISPLAY2), CLK(OSC), CLK(NONE),
Tom Warren6c43f6c2015-02-02 13:22:29 -0700105 MASK_BITS_31_29},
106 { CLK(PERIPH), CLK(CGENERAL), CLK(SFROM32KHZ), CLK(OSC),
107 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
108 MASK_BITS_31_28},
Simon Glass5a30cee2015-08-10 07:14:36 -0600109 /* CLOCK_TYPE_DP */
110 { CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
111 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
112 MASK_BITS_31_28},
Tom Warren6c43f6c2015-02-02 13:22:29 -0700113
114 /* Additional clock types on Tegra114+ */
115 /* CLOCK_TYPE_PC2CC3M */
116 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
117 CLK(MEMORY), CLK(NONE), CLK(NONE), CLK(NONE),
118 MASK_BITS_31_29},
119 /* CLOCK_TYPE_PC2CC3S_T */
120 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
121 CLK(SFROM32KHZ), CLK(NONE), CLK(OSC), CLK(NONE),
122 MASK_BITS_31_29},
123 /* CLOCK_TYPE_PC2CC3M_T */
124 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
125 CLK(MEMORY), CLK(NONE), CLK(OSC), CLK(NONE),
126 MASK_BITS_31_29},
127 /* CLOCK_TYPE_PC2CC3M_T, w/16-bit divisor (I2C) */
128 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
129 CLK(MEMORY), CLK(NONE), CLK(OSC), CLK(NONE),
130 MASK_BITS_31_29},
131 /* CLOCK_TYPE_MC2CC3P_A */
132 { CLK(MEMORY), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
133 CLK(PERIPH), CLK(NONE), CLK(AUDIO), CLK(NONE),
134 MASK_BITS_31_29},
135 /* CLOCK_TYPE_M */
136 { CLK(MEMORY), CLK(NONE), CLK(NONE), CLK(NONE),
137 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
138 MASK_BITS_31_30},
139 /* CLOCK_TYPE_MCPTM2C2C3 */
140 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
141 CLK(MEMORY2), CLK(CGENERAL2), CLK(CGENERAL3), CLK(NONE),
142 MASK_BITS_31_29},
143 /* CLOCK_TYPE_PC2CC3T_S */
144 { CLK(PERIPH), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
145 CLK(OSC), CLK(NONE), CLK(SFROM32KHZ), CLK(NONE),
146 MASK_BITS_31_29},
147 /* CLOCK_TYPE_AC2CC3P_TS2 */
148 { CLK(AUDIO), CLK(CGENERAL2), CLK(CGENERAL), CLK(CGENERAL3),
149 CLK(PERIPH), CLK(NONE), CLK(OSC), CLK(SRC2),
150 MASK_BITS_31_29},
151 /* CLOCK_TYPE_PC01C00_C42C41TC40 */
152 { CLK(PERIPH), CLK(CGENERAL_1), CLK(CGENERAL_0), CLK(NONE),
153 CLK(CGENERAL4_2), CLK(CGENERAL4_1), CLK(OSC), CLK(CGENERAL4_0),
154 MASK_BITS_31_29},
155};
156
157/*
158 * Clock type for each peripheral clock source. We put the name in each
159 * record just so it is easy to match things up
160 */
161#define TYPE(name, type) type
162static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
163 /* 0x00 */
164 TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
165 TYPE(PERIPHC_I2S3, CLOCK_TYPE_AXPT),
166 TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
167 TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PC2CC3M),
168 TYPE(PERIPHC_PWM, CLOCK_TYPE_PC2CC3S_T),
169 TYPE(PERIPHC_05h, CLOCK_TYPE_NONE),
170 TYPE(PERIPHC_SBC2, CLOCK_TYPE_PC2CC3M_T),
171 TYPE(PERIPHC_SBC3, CLOCK_TYPE_PC2CC3M_T),
172
173 /* 0x08 */
174 TYPE(PERIPHC_08h, CLOCK_TYPE_NONE),
175 TYPE(PERIPHC_I2C1, CLOCK_TYPE_PC2CC3M_T16),
176 TYPE(PERIPHC_I2C5, CLOCK_TYPE_PC2CC3M_T16),
177 TYPE(PERIPHC_0bh, CLOCK_TYPE_NONE),
178 TYPE(PERIPHC_0ch, CLOCK_TYPE_NONE),
179 TYPE(PERIPHC_SBC1, CLOCK_TYPE_PC2CC3M_T),
Thierry Redingbca79102019-04-15 11:32:15 +0200180 TYPE(PERIPHC_DISP1, CLOCK_TYPE_PDD2T),
181 TYPE(PERIPHC_DISP2, CLOCK_TYPE_PDD2T),
Tom Warren6c43f6c2015-02-02 13:22:29 -0700182
183 /* 0x10 */
184 TYPE(PERIPHC_10h, CLOCK_TYPE_NONE),
185 TYPE(PERIPHC_11h, CLOCK_TYPE_NONE),
186 TYPE(PERIPHC_VI, CLOCK_TYPE_MC2CC3P_A),
187 TYPE(PERIPHC_13h, CLOCK_TYPE_NONE),
188 TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PC2CC3M_T),
189 TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PC2CC3M_T),
190 TYPE(PERIPHC_16h, CLOCK_TYPE_NONE),
191 TYPE(PERIPHC_17h, CLOCK_TYPE_NONE),
192
193 /* 0x18 */
194 TYPE(PERIPHC_18h, CLOCK_TYPE_NONE),
195 TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PC2CC3M_T),
196 TYPE(PERIPHC_VFIR, CLOCK_TYPE_PC2CC3M_T),
197 TYPE(PERIPHC_1Bh, CLOCK_TYPE_NONE),
198 TYPE(PERIPHC_1Ch, CLOCK_TYPE_NONE),
199 TYPE(PERIPHC_HSI, CLOCK_TYPE_PC2CC3M_T),
200 TYPE(PERIPHC_UART1, CLOCK_TYPE_PC2CC3M_T),
201 TYPE(PERIPHC_UART2, CLOCK_TYPE_PC2CC3M_T),
202
203 /* 0x20 */
204 TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MC2CC3P_A),
205 TYPE(PERIPHC_21h, CLOCK_TYPE_NONE),
206 TYPE(PERIPHC_22h, CLOCK_TYPE_NONE),
207 TYPE(PERIPHC_23h, CLOCK_TYPE_NONE),
208 TYPE(PERIPHC_24h, CLOCK_TYPE_NONE),
209 TYPE(PERIPHC_25h, CLOCK_TYPE_NONE),
210 TYPE(PERIPHC_I2C2, CLOCK_TYPE_PC2CC3M_T16),
211 TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPTM2C2C3),
212
213 /* 0x28 */
214 TYPE(PERIPHC_UART3, CLOCK_TYPE_PC2CC3M_T),
215 TYPE(PERIPHC_29h, CLOCK_TYPE_NONE),
216 TYPE(PERIPHC_VI_SENSOR, CLOCK_TYPE_MC2CC3P_A),
217 TYPE(PERIPHC_2bh, CLOCK_TYPE_NONE),
218 TYPE(PERIPHC_2ch, CLOCK_TYPE_NONE),
219 TYPE(PERIPHC_SBC4, CLOCK_TYPE_PC2CC3M_T),
220 TYPE(PERIPHC_I2C3, CLOCK_TYPE_PC2CC3M_T16),
221 TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PC2CC3M_T),
222
223 /* 0x30 */
224 TYPE(PERIPHC_UART4, CLOCK_TYPE_PC2CC3M_T),
225 TYPE(PERIPHC_UART5, CLOCK_TYPE_PC2CC3M_T),
226 TYPE(PERIPHC_VDE, CLOCK_TYPE_PC2CC3M_T),
227 TYPE(PERIPHC_OWR, CLOCK_TYPE_PC2CC3M_T),
228 TYPE(PERIPHC_NOR, CLOCK_TYPE_PC2CC3M_T),
229 TYPE(PERIPHC_CSITE, CLOCK_TYPE_PC2CC3M_T),
230 TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
231 TYPE(PERIPHC_DTV, CLOCK_TYPE_NONE),
232
233 /* 0x38 */
234 TYPE(PERIPHC_38h, CLOCK_TYPE_NONE),
235 TYPE(PERIPHC_39h, CLOCK_TYPE_NONE),
236 TYPE(PERIPHC_3ah, CLOCK_TYPE_NONE),
237 TYPE(PERIPHC_3bh, CLOCK_TYPE_NONE),
238 TYPE(PERIPHC_MSENC, CLOCK_TYPE_MC2CC3P_A),
239 TYPE(PERIPHC_TSEC, CLOCK_TYPE_PC2CC3M_T),
240 TYPE(PERIPHC_3eh, CLOCK_TYPE_NONE),
241 TYPE(PERIPHC_OSC, CLOCK_TYPE_NONE),
242
243 /* 0x40 */
244 TYPE(PERIPHC_40h, CLOCK_TYPE_NONE), /* start with 0x3b0 */
245 TYPE(PERIPHC_MSELECT, CLOCK_TYPE_PC2CC3M_T),
246 TYPE(PERIPHC_TSENSOR, CLOCK_TYPE_PC2CC3T_S),
247 TYPE(PERIPHC_I2S4, CLOCK_TYPE_AXPT),
248 TYPE(PERIPHC_I2S5, CLOCK_TYPE_AXPT),
249 TYPE(PERIPHC_I2C4, CLOCK_TYPE_PC2CC3M_T16),
250 TYPE(PERIPHC_SBC5, CLOCK_TYPE_PC2CC3M_T),
251 TYPE(PERIPHC_SBC6, CLOCK_TYPE_PC2CC3M_T),
252
253 /* 0x48 */
254 TYPE(PERIPHC_AUDIO, CLOCK_TYPE_AC2CC3P_TS2),
255 TYPE(PERIPHC_49h, CLOCK_TYPE_NONE),
256 TYPE(PERIPHC_4ah, CLOCK_TYPE_NONE),
257 TYPE(PERIPHC_4bh, CLOCK_TYPE_NONE),
258 TYPE(PERIPHC_4ch, CLOCK_TYPE_NONE),
259 TYPE(PERIPHC_HDA2CODEC2X, CLOCK_TYPE_PC2CC3M_T),
260 TYPE(PERIPHC_ACTMON, CLOCK_TYPE_PC2CC3S_T),
261 TYPE(PERIPHC_EXTPERIPH1, CLOCK_TYPE_ASPTE),
262
263 /* 0x50 */
264 TYPE(PERIPHC_EXTPERIPH2, CLOCK_TYPE_ASPTE),
265 TYPE(PERIPHC_EXTPERIPH3, CLOCK_TYPE_ASPTE),
266 TYPE(PERIPHC_52h, CLOCK_TYPE_NONE),
267 TYPE(PERIPHC_I2CSLOW, CLOCK_TYPE_PC2CC3S_T),
268 TYPE(PERIPHC_SYS, CLOCK_TYPE_NONE),
269 TYPE(PERIPHC_55h, CLOCK_TYPE_NONE),
270 TYPE(PERIPHC_56h, CLOCK_TYPE_NONE),
271 TYPE(PERIPHC_57h, CLOCK_TYPE_NONE),
272
273 /* 0x58 */
274 TYPE(PERIPHC_58h, CLOCK_TYPE_NONE),
275 TYPE(PERIPHC_59h, CLOCK_TYPE_NONE),
276 TYPE(PERIPHC_5ah, CLOCK_TYPE_NONE),
277 TYPE(PERIPHC_5bh, CLOCK_TYPE_NONE),
278 TYPE(PERIPHC_SATAOOB, CLOCK_TYPE_PCMT),
279 TYPE(PERIPHC_SATA, CLOCK_TYPE_PCMT),
280 TYPE(PERIPHC_HDA, CLOCK_TYPE_PC2CC3M_T),
281 TYPE(PERIPHC_5fh, CLOCK_TYPE_NONE),
282
283 /* 0x60 */
284 TYPE(PERIPHC_XUSB_CORE_HOST, CLOCK_TYPE_NONE),
285 TYPE(PERIPHC_XUSB_FALCON, CLOCK_TYPE_NONE),
286 TYPE(PERIPHC_XUSB_FS, CLOCK_TYPE_NONE),
287 TYPE(PERIPHC_XUSB_CORE_DEV, CLOCK_TYPE_NONE),
288 TYPE(PERIPHC_XUSB_SS, CLOCK_TYPE_NONE),
289 TYPE(PERIPHC_CILAB, CLOCK_TYPE_NONE),
290 TYPE(PERIPHC_CILCD, CLOCK_TYPE_NONE),
291 TYPE(PERIPHC_CILE, CLOCK_TYPE_NONE),
292
293 /* 0x68 */
294 TYPE(PERIPHC_DSIA_LP, CLOCK_TYPE_NONE),
295 TYPE(PERIPHC_DSIB_LP, CLOCK_TYPE_NONE),
296 TYPE(PERIPHC_ENTROPY, CLOCK_TYPE_NONE),
297 TYPE(PERIPHC_DVFS_REF, CLOCK_TYPE_NONE),
298 TYPE(PERIPHC_DVFS_SOC, CLOCK_TYPE_NONE),
299 TYPE(PERIPHC_TRACECLKIN, CLOCK_TYPE_NONE),
300 TYPE(PERIPHC_6eh, CLOCK_TYPE_NONE),
301 TYPE(PERIPHC_6fh, CLOCK_TYPE_NONE),
302
303 /* 0x70 */
304 TYPE(PERIPHC_EMC_LATENCY, CLOCK_TYPE_NONE),
305 TYPE(PERIPHC_SOC_THERM, CLOCK_TYPE_NONE),
306 TYPE(PERIPHC_72h, CLOCK_TYPE_NONE),
307 TYPE(PERIPHC_73h, CLOCK_TYPE_NONE),
308 TYPE(PERIPHC_74h, CLOCK_TYPE_NONE),
309 TYPE(PERIPHC_75h, CLOCK_TYPE_NONE),
310 TYPE(PERIPHC_VI_SENSOR2, CLOCK_TYPE_NONE),
311 TYPE(PERIPHC_I2C6, CLOCK_TYPE_PC2CC3M_T16),
312
313 /* 0x78 */
314 TYPE(PERIPHC_78h, CLOCK_TYPE_NONE),
315 TYPE(PERIPHC_EMC_DLL, CLOCK_TYPE_MCPTM2C2C3),
316 TYPE(PERIPHC_7ah, CLOCK_TYPE_NONE),
317 TYPE(PERIPHC_CLK72MHZ, CLOCK_TYPE_NONE),
318 TYPE(PERIPHC_7ch, CLOCK_TYPE_NONE),
319 TYPE(PERIPHC_7dh, CLOCK_TYPE_NONE),
320 TYPE(PERIPHC_VIC, CLOCK_TYPE_NONE),
321 TYPE(PERIPHC_7Fh, CLOCK_TYPE_NONE),
322
323 /* 0x80 */
324 TYPE(PERIPHC_SDMMC_LEGACY_TM, CLOCK_TYPE_NONE),
325 TYPE(PERIPHC_NVDEC, CLOCK_TYPE_NONE),
326 TYPE(PERIPHC_NVJPG, CLOCK_TYPE_NONE),
327 TYPE(PERIPHC_NVENC, CLOCK_TYPE_NONE),
328 TYPE(PERIPHC_84h, CLOCK_TYPE_NONE),
329 TYPE(PERIPHC_85h, CLOCK_TYPE_NONE),
330 TYPE(PERIPHC_86h, CLOCK_TYPE_NONE),
331 TYPE(PERIPHC_87h, CLOCK_TYPE_NONE),
332
333 /* 0x88 */
334 TYPE(PERIPHC_88h, CLOCK_TYPE_NONE),
335 TYPE(PERIPHC_89h, CLOCK_TYPE_NONE),
336 TYPE(PERIPHC_DMIC3, CLOCK_TYPE_NONE),
337 TYPE(PERIPHC_APE, CLOCK_TYPE_NONE),
338 TYPE(PERIPHC_QSPI, CLOCK_TYPE_PC01C00_C42C41TC40),
Tom Warrend43c1dc2020-03-27 10:24:31 -0700339 TYPE(PERIPHC_VI_I2C, CLOCK_TYPE_PC2CC3M_T16),
Tom Warren6c43f6c2015-02-02 13:22:29 -0700340 TYPE(PERIPHC_USB2_HSIC_TRK, CLOCK_TYPE_NONE),
341 TYPE(PERIPHC_PEX_SATA_USB_RX_BYP, CLOCK_TYPE_NONE),
342
343 /* 0x90 */
344 TYPE(PERIPHC_MAUD, CLOCK_TYPE_NONE),
345 TYPE(PERIPHC_TSECB, CLOCK_TYPE_NONE),
346};
347
348/*
349 * This array translates a periph_id to a periphc_internal_id
350 *
351 * Not present/matched up:
352 * uint vi_sensor; _VI_SENSOR_0, 0x1A8
353 * SPDIF - which is both 0x08 and 0x0c
354 *
355 */
356#define NONE(name) (-1)
357#define OFFSET(name, value) PERIPHC_ ## name
358#define INTERNAL_ID(id) (id & 0x000000ff)
359static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
360 /* Low word: 31:0 */
361 NONE(CPU),
362 NONE(COP),
363 NONE(TRIGSYS),
364 NONE(ISPB),
365 NONE(RESERVED4),
366 NONE(TMR),
367 PERIPHC_UART1,
368 PERIPHC_UART2, /* and vfir 0x68 */
369
370 /* 8 */
371 NONE(GPIO),
372 PERIPHC_SDMMC2,
373 PERIPHC_SPDIF_IN,
374 PERIPHC_I2S2,
375 PERIPHC_I2C1,
376 NONE(RESERVED13),
377 PERIPHC_SDMMC1,
378 PERIPHC_SDMMC4,
379
380 /* 16 */
381 NONE(TCW),
382 PERIPHC_PWM,
383 PERIPHC_I2S3,
384 NONE(RESERVED19),
385 PERIPHC_VI,
386 NONE(RESERVED21),
387 NONE(USBD),
388 NONE(ISP),
389
390 /* 24 */
391 NONE(RESERVED24),
392 NONE(RESERVED25),
393 PERIPHC_DISP2,
394 PERIPHC_DISP1,
395 PERIPHC_HOST1X,
396 NONE(VCP),
397 PERIPHC_I2S1,
398 NONE(CACHE2),
399
400 /* Middle word: 63:32 */
401 NONE(MEM),
402 NONE(AHBDMA),
403 NONE(APBDMA),
404 NONE(RESERVED35),
405 NONE(RESERVED36),
406 NONE(STAT_MON),
407 NONE(RESERVED38),
408 NONE(FUSE),
409
410 /* 40 */
411 NONE(KFUSE),
412 PERIPHC_SBC1, /* SBCx = SPIx */
413 PERIPHC_NOR,
414 NONE(RESERVED43),
415 PERIPHC_SBC2,
416 NONE(XIO),
417 PERIPHC_SBC3,
418 PERIPHC_I2C5,
419
420 /* 48 */
421 NONE(DSI),
422 NONE(RESERVED49),
423 PERIPHC_HSI,
424 NONE(RESERVED51),
425 NONE(CSI),
426 NONE(RESERVED53),
427 PERIPHC_I2C2,
428 PERIPHC_UART3,
429
430 /* 56 */
431 NONE(MIPI_CAL),
432 PERIPHC_EMC,
433 NONE(USB2),
434 NONE(USB3),
435 NONE(RESERVED60),
436 PERIPHC_VDE,
437 NONE(BSEA),
438 NONE(BSEV),
439
440 /* Upper word 95:64 */
441 NONE(RESERVED64),
442 PERIPHC_UART4,
443 PERIPHC_UART5,
444 PERIPHC_I2C3,
445 PERIPHC_SBC4,
446 PERIPHC_SDMMC3,
447 NONE(PCIE),
448 PERIPHC_OWR,
449
450 /* 72 */
451 NONE(AFI),
452 PERIPHC_CSITE,
453 NONE(PCIEXCLK),
454 NONE(AVPUCQ),
455 NONE(LA),
456 NONE(TRACECLKIN),
457 NONE(SOC_THERM),
458 NONE(DTV),
459
460 /* 80 */
461 NONE(RESERVED80),
462 PERIPHC_I2CSLOW,
463 NONE(DSIB),
464 PERIPHC_TSEC,
465 NONE(RESERVED84),
466 NONE(RESERVED85),
467 NONE(RESERVED86),
468 NONE(EMUCIF),
469
470 /* 88 */
471 NONE(RESERVED88),
472 NONE(XUSB_HOST),
473 NONE(RESERVED90),
474 PERIPHC_MSENC,
475 NONE(RESERVED92),
476 NONE(RESERVED93),
477 NONE(RESERVED94),
478 NONE(XUSB_DEV),
479
480 /* V word: 31:0 */
481 NONE(CPUG),
482 NONE(CPULP),
483 NONE(V_RESERVED2),
484 PERIPHC_MSELECT,
485 NONE(V_RESERVED4),
486 PERIPHC_I2S4,
487 PERIPHC_I2S5,
488 PERIPHC_I2C4,
489
490 /* 104 */
491 PERIPHC_SBC5,
492 PERIPHC_SBC6,
493 PERIPHC_AUDIO,
494 NONE(APBIF),
495 NONE(V_RESERVED12),
496 NONE(V_RESERVED13),
497 NONE(V_RESERVED14),
498 PERIPHC_HDA2CODEC2X,
499
500 /* 112 */
501 NONE(ATOMICS),
502 NONE(V_RESERVED17),
503 NONE(V_RESERVED18),
504 NONE(V_RESERVED19),
505 NONE(V_RESERVED20),
506 NONE(V_RESERVED21),
507 NONE(V_RESERVED22),
508 PERIPHC_ACTMON,
509
510 /* 120 */
511 NONE(EXTPERIPH1),
512 NONE(EXTPERIPH2),
513 NONE(EXTPERIPH3),
514 NONE(OOB),
515 PERIPHC_SATA,
516 PERIPHC_HDA,
517 NONE(TZRAM),
518 NONE(SE),
519
520 /* W word: 31:0 */
521 NONE(HDA2HDMICODEC),
522 NONE(SATACOLD),
523 NONE(W_RESERVED2),
524 NONE(W_RESERVED3),
525 NONE(W_RESERVED4),
526 NONE(W_RESERVED5),
527 NONE(W_RESERVED6),
528 NONE(W_RESERVED7),
529
530 /* 136 */
531 NONE(CEC),
532 NONE(W_RESERVED9),
533 NONE(W_RESERVED10),
534 NONE(W_RESERVED11),
535 NONE(W_RESERVED12),
536 NONE(W_RESERVED13),
537 NONE(XUSB_PADCTL),
538 NONE(W_RESERVED15),
539
540 /* 144 */
541 NONE(W_RESERVED16),
542 NONE(W_RESERVED17),
543 NONE(W_RESERVED18),
544 NONE(W_RESERVED19),
545 NONE(W_RESERVED20),
546 NONE(ENTROPY),
547 NONE(DDS),
548 NONE(W_RESERVED23),
549
550 /* 152 */
551 NONE(W_RESERVED24),
552 NONE(W_RESERVED25),
553 NONE(W_RESERVED26),
554 NONE(DVFS),
555 NONE(XUSB_SS),
556 NONE(W_RESERVED29),
557 NONE(W_RESERVED30),
558 NONE(W_RESERVED31),
559
560 /* X word: 31:0 */
561 NONE(SPARE),
562 NONE(X_RESERVED1),
563 NONE(X_RESERVED2),
564 NONE(X_RESERVED3),
565 NONE(CAM_MCLK),
566 NONE(CAM_MCLK2),
567 PERIPHC_I2C6,
568 NONE(X_RESERVED7),
569
570 /* 168 */
571 NONE(X_RESERVED8),
572 NONE(X_RESERVED9),
573 NONE(X_RESERVED10),
574 NONE(VIM2_CLK),
575 NONE(X_RESERVED12),
576 NONE(X_RESERVED13),
577 NONE(EMC_DLL),
578 NONE(X_RESERVED15),
579
580 /* 176 */
581 NONE(X_RESERVED16),
582 NONE(CLK72MHZ),
583 NONE(VIC),
584 NONE(X_RESERVED19),
585 NONE(X_RESERVED20),
586 NONE(DPAUX),
587 NONE(SOR0),
588 NONE(X_RESERVED23),
589
590 /* 184 */
591 NONE(GPU),
592 NONE(X_RESERVED25),
593 NONE(X_RESERVED26),
594 NONE(X_RESERVED27),
595 NONE(X_RESERVED28),
596 NONE(X_RESERVED29),
597 NONE(X_RESERVED30),
598 NONE(X_RESERVED31),
599
600 /* Y: 192 (192 - 223) */
601 NONE(Y_RESERVED0),
602 PERIPHC_SDMMC_LEGACY_TM,
603 PERIPHC_NVDEC,
604 PERIPHC_NVJPG,
605 NONE(Y_RESERVED4),
606 PERIPHC_DMIC3, /* 197 */
607 PERIPHC_APE, /* 198 */
608 NONE(Y_RESERVED7),
609
610 /* 200 */
611 NONE(Y_RESERVED8),
612 NONE(Y_RESERVED9),
613 NONE(Y_RESERVED10),
614 NONE(Y_RESERVED11),
615 NONE(Y_RESERVED12),
616 NONE(Y_RESERVED13),
617 NONE(Y_RESERVED14),
618 NONE(Y_RESERVED15),
619
620 /* 208 */
621 PERIPHC_VI_I2C, /* 208 */
622 NONE(Y_RESERVED17),
623 NONE(Y_RESERVED18),
624 PERIPHC_QSPI, /* 211 */
625 NONE(Y_RESERVED20),
626 NONE(Y_RESERVED21),
627 NONE(Y_RESERVED22),
628 NONE(Y_RESERVED23),
629
630 /* 216 */
631 NONE(Y_RESERVED24),
632 NONE(Y_RESERVED25),
633 NONE(Y_RESERVED26),
634 PERIPHC_NVENC, /* 219 */
635 NONE(Y_RESERVED28),
636 NONE(Y_RESERVED29),
637 NONE(Y_RESERVED30),
638 NONE(Y_RESERVED31),
639};
640
641/*
Tom Warren722e0002015-06-25 09:50:44 -0700642 * PLL divider shift/mask tables for all PLL IDs.
643 */
644struct clk_pll_info tegra_pll_info_table[CLOCK_ID_PLL_COUNT] = {
645 /*
646 * NOTE: If kcp_mask/kvco_mask == 0, they're not used in that PLL (PLLC, etc.)
647 * If lock_ena or lock_det are >31, they're not used in that PLL (PLLC, etc.)
648 */
649 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 10, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x1F,
650 .lock_ena = 32, .lock_det = 27, .kcp_shift = 0, .kcp_mask = 0, .kvco_shift = 0, .kvco_mask = 0 }, /* PLLC */
651 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x1F,
652 .lock_ena = 4, .lock_det = 27, .kcp_shift = 1, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLM */
653 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 10, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x1F,
654 .lock_ena = 18, .lock_det = 27, .kcp_shift = 0, .kcp_mask = 3, .kvco_shift = 2, .kvco_mask = 1 }, /* PLLP */
655 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x1F,
656 .lock_ena = 28, .lock_det = 27, .kcp_shift = 25, .kcp_mask = 3, .kvco_shift = 24, .kvco_mask = 1 }, /* PLLA */
657 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 16, .p_mask = 0x1F,
658 .lock_ena = 29, .lock_det = 27, .kcp_shift = 25, .kcp_mask = 3, .kvco_shift = 24, .kvco_mask = 1 }, /* PLLU */
659 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 11, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x07,
660 .lock_ena = 18, .lock_det = 27, .kcp_shift = 23, .kcp_mask = 3, .kvco_shift = 22, .kvco_mask = 1 }, /* PLLD */
661 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 20, .p_mask = 0x1F,
662 .lock_ena = 18, .lock_det = 27, .kcp_shift = 1, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLX */
663 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 0, .p_mask = 0,
664 .lock_ena = 9, .lock_det = 11, .kcp_shift = 6, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLE */
665 { .m_shift = 0, .m_mask = 0, .n_shift = 0, .n_mask = 0, .p_shift = 0, .p_mask = 0,
666 .lock_ena = 0, .lock_det = 0, .kcp_shift = 0, .kcp_mask = 0, .kvco_shift = 0, .kvco_mask = 0 }, /* PLLS (gone)*/
Simon Glass5a30cee2015-08-10 07:14:36 -0600667 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 19, .p_mask = 0x1F,
668 .lock_ena = 30, .lock_det = 27, .kcp_shift = 25, .kcp_mask = 3, .kvco_shift = 24, .kvco_mask = 1 }, /* PLLDP */
Tom Warren722e0002015-06-25 09:50:44 -0700669};
670
671/*
Tom Warren6c43f6c2015-02-02 13:22:29 -0700672 * Get the oscillator frequency, from the corresponding hardware configuration
673 * field. Note that Tegra30+ support 3 new higher freqs, but we map back
674 * to the old T20 freqs. Support for the higher oscillators is TBD.
675 */
676enum clock_osc_freq clock_get_osc_freq(void)
677{
678 struct clk_rst_ctlr *clkrst =
679 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
680 u32 reg;
681
682 reg = readl(&clkrst->crc_osc_ctrl);
683 reg = (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
684 /*
685 * 0 = 13MHz, 1 = 16.8MHz, 4 = 19.2MHz, 5 = 38.4MHz,
686 * 8 = 12MHz, 9 = 48MHz, 12 = 26MHz
687 */
688 if (reg == 5) {
689 debug("OSC_FREQ is 38.4MHz (%d) ...\n", reg);
Tom Warren3e8650c2015-06-22 13:03:44 -0700690 /* Map it to the 5th CLOCK_OSC_ enum, i.e. 4 */
691 return 4;
Tom Warren6c43f6c2015-02-02 13:22:29 -0700692 }
693
694 /*
695 * Map to most common (T20) freqs (except 38.4, handled above):
696 * 13/16.8 = 0, 19.2 = 1, 12/48 = 2, 26 = 3
697 */
698 return reg >> 2;
699}
700
701/* Returns a pointer to the clock source register for a peripheral */
702u32 *get_periph_source_reg(enum periph_id periph_id)
703{
704 struct clk_rst_ctlr *clkrst =
705 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
706 enum periphc_internal_id internal_id;
707
708 /* Coresight is a special case */
709 if (periph_id == PERIPH_ID_CSI)
710 return &clkrst->crc_clk_src[PERIPH_ID_CSI+1];
711
712 assert(periph_id >= PERIPH_ID_FIRST && periph_id < PERIPH_ID_COUNT);
713 internal_id = INTERNAL_ID(periph_id_to_internal_id[periph_id]);
714 assert(internal_id != -1);
715
716 if (internal_id < PERIPHC_VW_FIRST)
717 /* L, H, U */
718 return &clkrst->crc_clk_src[internal_id];
719
720 if (internal_id < PERIPHC_X_FIRST) {
721 /* VW */
722 internal_id -= PERIPHC_VW_FIRST;
723 return &clkrst->crc_clk_src_vw[internal_id];
724 }
725
726 if (internal_id < PERIPHC_Y_FIRST) {
727 /* X */
728 internal_id -= PERIPHC_X_FIRST;
729 return &clkrst->crc_clk_src_x[internal_id];
730 }
731
732 /* Y */
733 internal_id -= PERIPHC_Y_FIRST;
734 return &clkrst->crc_clk_src_y[internal_id];
735}
736
Stephen Warrend0ad8a52016-09-13 10:45:56 -0600737int get_periph_clock_info(enum periph_id periph_id, int *mux_bits,
738 int *divider_bits, int *type)
739{
740 enum periphc_internal_id internal_id;
741
742 if (!clock_periph_id_isvalid(periph_id))
743 return -1;
744
Tom Warrend43c1dc2020-03-27 10:24:31 -0700745 internal_id = INTERNAL_ID(periph_id_to_internal_id[periph_id]);
Stephen Warrend0ad8a52016-09-13 10:45:56 -0600746 if (!periphc_internal_id_isvalid(internal_id))
747 return -1;
748
749 *type = clock_periph_type[internal_id];
750 if (!clock_type_id_isvalid(*type))
751 return -1;
752
753 *mux_bits = clock_source[*type][CLOCK_MAX_MUX];
754
755 if (*type == CLOCK_TYPE_PC2CC3M_T16)
756 *divider_bits = 16;
757 else
758 *divider_bits = 8;
759
760 return 0;
761}
762
763enum clock_id get_periph_clock_id(enum periph_id periph_id, int source)
764{
765 enum periphc_internal_id internal_id;
766 int type;
767
768 if (!clock_periph_id_isvalid(periph_id))
769 return CLOCK_ID_NONE;
770
Tom Warrend43c1dc2020-03-27 10:24:31 -0700771 internal_id = INTERNAL_ID(periph_id_to_internal_id[periph_id]);
Stephen Warrend0ad8a52016-09-13 10:45:56 -0600772 if (!periphc_internal_id_isvalid(internal_id))
773 return CLOCK_ID_NONE;
774
775 type = clock_periph_type[internal_id];
776 if (!clock_type_id_isvalid(type))
777 return CLOCK_ID_NONE;
778
779 return clock_source[type][source];
780}
781
Tom Warren6c43f6c2015-02-02 13:22:29 -0700782/**
783 * Given a peripheral ID and the required source clock, this returns which
784 * value should be programmed into the source mux for that peripheral.
785 *
786 * There is special code here to handle the one source type with 5 sources.
787 *
788 * @param periph_id peripheral to start
789 * @param source PLL id of required parent clock
790 * @param mux_bits Set to number of bits in mux register: 2 or 4
791 * @param divider_bits Set to number of divider bits (8 or 16)
792 * @return mux value (0-4, or -1 if not found)
793 */
794int get_periph_clock_source(enum periph_id periph_id,
795 enum clock_id parent, int *mux_bits, int *divider_bits)
796{
797 enum clock_type_id type;
Stephen Warrend0ad8a52016-09-13 10:45:56 -0600798 int mux, err;
Tom Warren6c43f6c2015-02-02 13:22:29 -0700799
Stephen Warrend0ad8a52016-09-13 10:45:56 -0600800 err = get_periph_clock_info(periph_id, mux_bits, divider_bits, &type);
801 assert(!err);
Tom Warren6c43f6c2015-02-02 13:22:29 -0700802
803 for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
804 if (clock_source[type][mux] == parent)
805 return mux;
806
807 /* if we get here, either us or the caller has made a mistake */
808 printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
809 parent);
810 return -1;
811}
812
813void clock_set_enable(enum periph_id periph_id, int enable)
814{
815 struct clk_rst_ctlr *clkrst =
816 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
817 u32 *clk;
818 u32 reg;
819
820 /* Enable/disable the clock to this peripheral */
821 assert(clock_periph_id_isvalid(periph_id));
822 if ((int)periph_id < (int)PERIPH_ID_VW_FIRST)
823 clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
824 else if ((int)periph_id < (int)PERIPH_ID_X_FIRST)
825 clk = &clkrst->crc_clk_out_enb_vw[PERIPH_REG(periph_id)];
826 else if ((int)periph_id < (int)PERIPH_ID_Y_FIRST)
827 clk = &clkrst->crc_clk_out_enb_x;
828 else
829 clk = &clkrst->crc_clk_out_enb_y;
830
831 reg = readl(clk);
832 if (enable)
833 reg |= PERIPH_MASK(periph_id);
834 else
835 reg &= ~PERIPH_MASK(periph_id);
836 writel(reg, clk);
837}
838
839void reset_set_enable(enum periph_id periph_id, int enable)
840{
841 struct clk_rst_ctlr *clkrst =
842 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
843 u32 *reset;
844 u32 reg;
845
846 /* Enable/disable reset to the peripheral */
847 assert(clock_periph_id_isvalid(periph_id));
848 if (periph_id < PERIPH_ID_VW_FIRST)
849 reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
850 else if ((int)periph_id < (int)PERIPH_ID_X_FIRST)
851 reset = &clkrst->crc_rst_dev_vw[PERIPH_REG(periph_id)];
852 else if ((int)periph_id < (int)PERIPH_ID_Y_FIRST)
853 reset = &clkrst->crc_rst_devices_x;
854 else
855 reset = &clkrst->crc_rst_devices_y;
856
857 reg = readl(reset);
858 if (enable)
859 reg |= PERIPH_MASK(periph_id);
860 else
861 reg &= ~PERIPH_MASK(periph_id);
862 writel(reg, reset);
863}
864
865#ifdef CONFIG_OF_CONTROL
866/*
867 * Convert a device tree clock ID to our peripheral ID. They are mostly
868 * the same but we are very cautious so we check that a valid clock ID is
869 * provided.
870 *
871 * @param clk_id Clock ID according to tegra210 device tree binding
872 * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
873 */
874enum periph_id clk_id_to_periph_id(int clk_id)
875{
876 if (clk_id > PERIPH_ID_COUNT)
877 return PERIPH_ID_NONE;
878
879 switch (clk_id) {
880 case PERIPH_ID_RESERVED4:
881 case PERIPH_ID_RESERVED25:
882 case PERIPH_ID_RESERVED35:
883 case PERIPH_ID_RESERVED36:
884 case PERIPH_ID_RESERVED38:
885 case PERIPH_ID_RESERVED43:
886 case PERIPH_ID_RESERVED49:
887 case PERIPH_ID_RESERVED53:
888 case PERIPH_ID_RESERVED64:
889 case PERIPH_ID_RESERVED84:
890 case PERIPH_ID_RESERVED85:
891 case PERIPH_ID_RESERVED86:
892 case PERIPH_ID_RESERVED88:
893 case PERIPH_ID_RESERVED90:
894 case PERIPH_ID_RESERVED92:
895 case PERIPH_ID_RESERVED93:
896 case PERIPH_ID_RESERVED94:
897 case PERIPH_ID_V_RESERVED2:
898 case PERIPH_ID_V_RESERVED4:
899 case PERIPH_ID_V_RESERVED17:
900 case PERIPH_ID_V_RESERVED18:
901 case PERIPH_ID_V_RESERVED19:
902 case PERIPH_ID_V_RESERVED20:
903 case PERIPH_ID_V_RESERVED21:
904 case PERIPH_ID_V_RESERVED22:
905 case PERIPH_ID_W_RESERVED2:
906 case PERIPH_ID_W_RESERVED3:
907 case PERIPH_ID_W_RESERVED4:
908 case PERIPH_ID_W_RESERVED5:
909 case PERIPH_ID_W_RESERVED6:
910 case PERIPH_ID_W_RESERVED7:
911 case PERIPH_ID_W_RESERVED9:
912 case PERIPH_ID_W_RESERVED10:
913 case PERIPH_ID_W_RESERVED11:
914 case PERIPH_ID_W_RESERVED12:
915 case PERIPH_ID_W_RESERVED13:
916 case PERIPH_ID_W_RESERVED15:
917 case PERIPH_ID_W_RESERVED16:
918 case PERIPH_ID_W_RESERVED17:
919 case PERIPH_ID_W_RESERVED18:
920 case PERIPH_ID_W_RESERVED19:
921 case PERIPH_ID_W_RESERVED20:
922 case PERIPH_ID_W_RESERVED23:
923 case PERIPH_ID_W_RESERVED29:
924 case PERIPH_ID_W_RESERVED30:
925 case PERIPH_ID_W_RESERVED31:
926 return PERIPH_ID_NONE;
927 default:
928 return clk_id;
929 }
930}
931#endif /* CONFIG_OF_CONTROL */
932
933/*
934 * T210 redefines PLLP_OUT2 as PLLP_VCO/DIVP, so do different OUT1-4 setup here.
935 * PLLP_BASE/MISC/etc. is already set up for 408MHz in the BootROM.
936 */
937void tegra210_setup_pllp(void)
938{
939 struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
940 u32 reg;
941
942 /* Set PLLP_OUT1, 3 & 4 freqs to 9.6, 102 & 204MHz */
943
944 /* OUT1 */
945 /* Assert RSTN before enable */
946 reg = PLLP_OUT1_RSTN_EN;
947 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
948 /* Set divisor and reenable */
949 reg = (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO)
950 | PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS;
951 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
952
953 /* OUT3, 4 */
954 /* Assert RSTN before enable */
955 reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN;
956 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
957 /* Set divisor and reenable */
958 reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO)
959 | PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS
960 | (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO)
961 | PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS;
962 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
963
964 /*
965 * NOTE: If you want to change PLLP_OUT2 away from 204MHz,
966 * you can change PLLP_BASE DIVP here. Currently defaults
967 * to 1, which is 2^1, or 2, so PLLP_OUT2 is 204MHz.
968 * See Table 13 in section 5.1.4 in T210 TRM for more info.
969 */
970}
971
972void clock_early_init(void)
973{
974 struct clk_rst_ctlr *clkrst =
975 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
Tom Warren722e0002015-06-25 09:50:44 -0700976 struct clk_pll_info *pllinfo = &tegra_pll_info_table[CLOCK_ID_DISPLAY];
Tom Warren6c43f6c2015-02-02 13:22:29 -0700977 u32 data;
978
979 tegra210_setup_pllp();
980
981 /*
982 * PLLC output frequency set to 600Mhz
983 * PLLD output frequency set to 925Mhz
984 */
985 switch (clock_get_osc_freq()) {
986 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
987 clock_set_rate(CLOCK_ID_CGENERAL, 600, 12, 0, 8);
988 clock_set_rate(CLOCK_ID_DISPLAY, 925, 12, 0, 12);
989 break;
990
991 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
992 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
993 clock_set_rate(CLOCK_ID_DISPLAY, 925, 26, 0, 12);
994 break;
995
996 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
997 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
998 clock_set_rate(CLOCK_ID_DISPLAY, 925, 13, 0, 12);
999 break;
1000 case CLOCK_OSC_FREQ_19_2:
1001 clock_set_rate(CLOCK_ID_CGENERAL, 125, 4, 0, 0);
1002 clock_set_rate(CLOCK_ID_DISPLAY, 96, 2, 0, 12);
1003 break;
Tom Warren3e8650c2015-06-22 13:03:44 -07001004 case CLOCK_OSC_FREQ_38_4:
1005 clock_set_rate(CLOCK_ID_CGENERAL, 125, 8, 0, 0);
1006 clock_set_rate(CLOCK_ID_DISPLAY, 96, 4, 0, 0);
1007 break;
Tom Warren6c43f6c2015-02-02 13:22:29 -07001008 default:
1009 /*
1010 * These are not supported. It is too early to print a
1011 * message and the UART likely won't work anyway due to the
1012 * oscillator being wrong.
1013 */
1014 break;
1015 }
1016
1017 /* PLLC_MISC1: Turn IDDQ off. NOTE: T210 PLLC_MISC_1 maps to pll_misc */
1018 clrbits_le32(&clkrst->crc_pll[CLOCK_ID_CGENERAL].pll_misc,
1019 (1 << PLLC_IDDQ));
1020 udelay(2);
1021
1022 /*
1023 * PLLC_MISC: Take PLLC out of reset. NOTE: T210 PLLC_MISC maps
1024 * to pll_out[1]
1025 */
1026 clrbits_le32(&clkrst->crc_pll[CLOCK_ID_CGENERAL].pll_out[1],
1027 (1 << PLLC_RESET));
1028 udelay(2);
1029
1030 /* PLLD_MISC: Set CLKENABLE and LOCK_DETECT bits */
Tom Warren722e0002015-06-25 09:50:44 -07001031 data = (1 << PLLD_ENABLE_CLK) | (1 << pllinfo->lock_ena);
Tom Warren6c43f6c2015-02-02 13:22:29 -07001032 writel(data, &clkrst->crc_pll[CLOCK_ID_DISPLAY].pll_misc);
1033 udelay(2);
1034}
1035
Thierry Redingc043c022015-08-20 11:42:19 +02001036unsigned int clk_m_get_rate(unsigned parent_rate)
1037{
1038 struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
1039 u32 value, div;
1040
1041 value = readl(&clkrst->crc_spare_reg0);
1042 div = ((value >> 2) & 0x3) + 1;
1043
1044 return parent_rate / div;
1045}
1046
Tom Warren6c43f6c2015-02-02 13:22:29 -07001047void arch_timer_init(void)
1048{
1049 struct sysctr_ctlr *sysctr = (struct sysctr_ctlr *)NV_PA_TSC_BASE;
1050 u32 freq, val;
1051
Thierry Reding97c02d82015-08-20 11:42:20 +02001052 freq = clock_get_rate(CLOCK_ID_CLK_M);
1053 debug("%s: clk_m freq is %dHz [0x%08X]\n", __func__, freq, freq);
Tom Warren6c43f6c2015-02-02 13:22:29 -07001054
Thierry Reding97c02d82015-08-20 11:42:20 +02001055 if (current_el() == 3)
1056 asm("msr cntfrq_el0, %0\n" : : "r" (freq));
Tom Warren6c43f6c2015-02-02 13:22:29 -07001057
1058 /* Only Tegra114+ has the System Counter regs */
1059 debug("%s: setting CNTFID0 to 0x%08X\n", __func__, freq);
1060 writel(freq, &sysctr->cntfid0);
1061
1062 val = readl(&sysctr->cntcr);
1063 val |= TSC_CNTCR_ENABLE | TSC_CNTCR_HDBG;
1064 writel(val, &sysctr->cntcr);
1065 debug("%s: TSC CNTCR = 0x%08X\n", __func__, val);
1066}
1067
Stephen Warrendfa551e2015-10-05 16:58:52 -06001068#define PLLREFE_MISC 0x4c8
1069#define PLLREFE_MISC_LOCK BIT(27)
1070#define PLLREFE_MISC_IDDQ BIT(24)
1071
1072#define PLLREFE_BASE 0x4c4
1073#define PLLREFE_BASE_BYPASS BIT(31)
1074#define PLLREFE_BASE_ENABLE BIT(30)
1075#define PLLREFE_BASE_REF_DIS BIT(29)
1076#define PLLREFE_BASE_KCP(kcp) (((kcp) & 0x3) << 27)
1077#define PLLREFE_BASE_KVCO BIT(26)
1078#define PLLREFE_BASE_DIVP(p) (((p) & 0x1f) << 16)
1079#define PLLREFE_BASE_DIVN(n) (((n) & 0xff) << 8)
1080#define PLLREFE_BASE_DIVM(m) (((m) & 0xff) << 0)
1081
1082static int tegra_pllref_enable(void)
1083{
1084 u32 value;
1085 unsigned long start;
1086
1087 /*
1088 * This sequence comes from Tegra X1 TRM section "Cold Boot, with no
1089 * Recovery Mode or Boot from USB", sub-section "PLLREFE".
1090 */
1091
1092 value = readl(NV_PA_CLK_RST_BASE + PLLREFE_MISC);
1093 value &= ~PLLREFE_MISC_IDDQ;
1094 writel(value, NV_PA_CLK_RST_BASE + PLLREFE_MISC);
1095
1096 udelay(5);
1097
1098 value = PLLREFE_BASE_ENABLE |
1099 PLLREFE_BASE_KCP(0) |
1100 PLLREFE_BASE_DIVP(0) |
1101 PLLREFE_BASE_DIVN(0x41) |
1102 PLLREFE_BASE_DIVM(4);
1103 writel(value, NV_PA_CLK_RST_BASE + PLLREFE_BASE);
1104
1105 debug("waiting for pllrefe lock\n");
1106 start = get_timer(0);
1107 while (get_timer(start) < 250) {
1108 value = readl(NV_PA_CLK_RST_BASE + PLLREFE_MISC);
1109 if (value & PLLREFE_MISC_LOCK)
1110 break;
1111 }
1112 if (!(value & PLLREFE_MISC_LOCK)) {
1113 debug(" timeout\n");
1114 return -ETIMEDOUT;
1115 }
1116 debug(" done\n");
1117
1118 return 0;
1119}
1120
Tom Warren6c43f6c2015-02-02 13:22:29 -07001121#define PLLE_SS_CNTL 0x68
1122#define PLLE_SS_CNTL_SSCINCINTR(x) (((x) & 0x3f) << 24)
1123#define PLLE_SS_CNTL_SSCINC(x) (((x) & 0xff) << 16)
1124#define PLLE_SS_CNTL_SSCINVERT (1 << 15)
1125#define PLLE_SS_CNTL_SSCCENTER (1 << 14)
1126#define PLLE_SS_CNTL_SSCBYP (1 << 12)
1127#define PLLE_SS_CNTL_INTERP_RESET (1 << 11)
1128#define PLLE_SS_CNTL_BYPASS_SS (1 << 10)
1129#define PLLE_SS_CNTL_SSCMAX(x) (((x) & 0x1ff) << 0)
1130
1131#define PLLE_BASE 0x0e8
Stephen Warrendfa551e2015-10-05 16:58:52 -06001132#define PLLE_BASE_ENABLE (1 << 31)
1133#define PLLE_BASE_PLDIV_CML(x) (((x) & 0x1f) << 24)
Tom Warren6c43f6c2015-02-02 13:22:29 -07001134#define PLLE_BASE_NDIV(x) (((x) & 0xff) << 8)
1135#define PLLE_BASE_MDIV(x) (((x) & 0xff) << 0)
1136
1137#define PLLE_MISC 0x0ec
1138#define PLLE_MISC_IDDQ_SWCTL (1 << 14)
Stephen Warrendfa551e2015-10-05 16:58:52 -06001139#define PLLE_MISC_IDDQ_OVERRIDE_VALUE (1 << 13)
1140#define PLLE_MISC_LOCK (1 << 11)
Stephen Warren8f837592016-03-22 09:45:36 -06001141#define PLLE_PTS (1 << 8)
Stephen Warrendfa551e2015-10-05 16:58:52 -06001142#define PLLE_MISC_KCP(x) (((x) & 0x3) << 6)
Tom Warren6c43f6c2015-02-02 13:22:29 -07001143#define PLLE_MISC_VREG_CTRL(x) (((x) & 0x3) << 2)
Stephen Warrendfa551e2015-10-05 16:58:52 -06001144#define PLLE_MISC_KVCO (1 << 0)
Tom Warren6c43f6c2015-02-02 13:22:29 -07001145
1146#define PLLE_AUX 0x48c
Stephen Warrendfa551e2015-10-05 16:58:52 -06001147#define PLLE_AUX_SS_SEQ_INCLUDE (1 << 31)
1148#define PLLE_AUX_REF_SEL_PLLREFE (1 << 28)
Tom Warren6c43f6c2015-02-02 13:22:29 -07001149#define PLLE_AUX_SEQ_ENABLE (1 << 24)
Stephen Warrendfa551e2015-10-05 16:58:52 -06001150#define PLLE_AUX_SS_SWCTL (1 << 6)
Tom Warren6c43f6c2015-02-02 13:22:29 -07001151#define PLLE_AUX_ENABLE_SWCTL (1 << 4)
Stephen Warrendfa551e2015-10-05 16:58:52 -06001152#define PLLE_AUX_USE_LOCKDET (1 << 3)
Tom Warren6c43f6c2015-02-02 13:22:29 -07001153
1154int tegra_plle_enable(void)
1155{
Tom Warren6c43f6c2015-02-02 13:22:29 -07001156 u32 value;
Stephen Warrendfa551e2015-10-05 16:58:52 -06001157 unsigned long start;
Tom Warren6c43f6c2015-02-02 13:22:29 -07001158
Stephen Warrendfa551e2015-10-05 16:58:52 -06001159 /* PLLREF feeds PLLE */
1160 tegra_pllref_enable();
1161
1162 /*
1163 * This sequence comes from Tegra X1 TRM section "Cold Boot, with no
1164 * Recovery Mode or Boot from USB", sub-section "PLLEs".
1165 */
1166
1167 /* 1. Select XTAL as the source */
Tom Warren6c43f6c2015-02-02 13:22:29 -07001168
1169 value = readl(NV_PA_CLK_RST_BASE + PLLE_AUX);
Stephen Warrendfa551e2015-10-05 16:58:52 -06001170 value &= ~PLLE_AUX_REF_SEL_PLLREFE;
Tom Warren6c43f6c2015-02-02 13:22:29 -07001171 writel(value, NV_PA_CLK_RST_BASE + PLLE_AUX);
1172
Tom Warren6c43f6c2015-02-02 13:22:29 -07001173 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
Stephen Warrendfa551e2015-10-05 16:58:52 -06001174 value &= ~PLLE_MISC_IDDQ_OVERRIDE_VALUE;
Tom Warren6c43f6c2015-02-02 13:22:29 -07001175 writel(value, NV_PA_CLK_RST_BASE + PLLE_MISC);
1176
Stephen Warrendfa551e2015-10-05 16:58:52 -06001177 /* 2. Wait 5 us */
Tom Warren6c43f6c2015-02-02 13:22:29 -07001178 udelay(5);
1179
Stephen Warrendfa551e2015-10-05 16:58:52 -06001180 /*
1181 * 3. Program the following registers to generate a low jitter 100MHz
1182 * clock.
1183 */
Tom Warren6c43f6c2015-02-02 13:22:29 -07001184
1185 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
Stephen Warrendfa551e2015-10-05 16:58:52 -06001186 value &= ~PLLE_BASE_PLDIV_CML(0x1f);
Tom Warren6c43f6c2015-02-02 13:22:29 -07001187 value &= ~PLLE_BASE_NDIV(0xff);
1188 value &= ~PLLE_BASE_MDIV(0xff);
Stephen Warrendfa551e2015-10-05 16:58:52 -06001189 value |= PLLE_BASE_PLDIV_CML(0xe);
1190 value |= PLLE_BASE_NDIV(0x7d);
1191 value |= PLLE_BASE_MDIV(2);
Tom Warren6c43f6c2015-02-02 13:22:29 -07001192 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
1193
Stephen Warrendfa551e2015-10-05 16:58:52 -06001194 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
Stephen Warren8f837592016-03-22 09:45:36 -06001195 value |= PLLE_PTS;
Stephen Warrendfa551e2015-10-05 16:58:52 -06001196 value &= ~PLLE_MISC_KCP(3);
1197 value &= ~PLLE_MISC_VREG_CTRL(3);
1198 value &= ~PLLE_MISC_KVCO;
1199 writel(value, NV_PA_CLK_RST_BASE + PLLE_MISC);
Tom Warren6c43f6c2015-02-02 13:22:29 -07001200
1201 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
1202 value |= PLLE_BASE_ENABLE;
1203 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
1204
Stephen Warrendfa551e2015-10-05 16:58:52 -06001205 /* 4. Wait for LOCK */
1206
1207 debug("waiting for plle lock\n");
1208 start = get_timer(0);
1209 while (get_timer(start) < 250) {
1210 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
1211 if (value & PLLE_MISC_LOCK)
1212 break;
1213 }
1214 if (!(value & PLLE_MISC_LOCK)) {
1215 debug(" timeout\n");
1216 return -ETIMEDOUT;
1217 }
1218 debug(" done\n");
1219
1220 /* 5. Enable SSA */
Tom Warren6c43f6c2015-02-02 13:22:29 -07001221
1222 value = readl(NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
Stephen Warrendfa551e2015-10-05 16:58:52 -06001223 value &= ~PLLE_SS_CNTL_SSCINC(0xff);
1224 value |= PLLE_SS_CNTL_SSCINC(1);
1225 value &= ~PLLE_SS_CNTL_SSCINCINTR(0x3f);
1226 value |= PLLE_SS_CNTL_SSCINCINTR(0x23);
1227 value &= ~PLLE_SS_CNTL_SSCMAX(0x1fff);
1228 value |= PLLE_SS_CNTL_SSCMAX(0x21);
Tom Warren6c43f6c2015-02-02 13:22:29 -07001229 value &= ~PLLE_SS_CNTL_SSCINVERT;
1230 value &= ~PLLE_SS_CNTL_SSCCENTER;
Tom Warren6c43f6c2015-02-02 13:22:29 -07001231 value &= ~PLLE_SS_CNTL_BYPASS_SS;
Stephen Warrendfa551e2015-10-05 16:58:52 -06001232 value &= ~PLLE_SS_CNTL_SSCBYP;
Tom Warren6c43f6c2015-02-02 13:22:29 -07001233 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
1234
Stephen Warrendfa551e2015-10-05 16:58:52 -06001235 /* 6. Wait 300 ns */
1236
Tom Warren6c43f6c2015-02-02 13:22:29 -07001237 udelay(1);
Tom Warren6c43f6c2015-02-02 13:22:29 -07001238 value &= ~PLLE_SS_CNTL_INTERP_RESET;
1239 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
1240
Tom Warren6c43f6c2015-02-02 13:22:29 -07001241 return 0;
1242}
Stephen Warren6dbcc962016-09-13 10:45:55 -06001243
1244struct periph_clk_init periph_clk_init_table[] = {
1245 { PERIPH_ID_SBC1, CLOCK_ID_PERIPH },
1246 { PERIPH_ID_SBC2, CLOCK_ID_PERIPH },
1247 { PERIPH_ID_SBC3, CLOCK_ID_PERIPH },
1248 { PERIPH_ID_SBC4, CLOCK_ID_PERIPH },
1249 { PERIPH_ID_SBC5, CLOCK_ID_PERIPH },
1250 { PERIPH_ID_SBC6, CLOCK_ID_PERIPH },
1251 { PERIPH_ID_HOST1X, CLOCK_ID_PERIPH },
Stephen Warren6dbcc962016-09-13 10:45:55 -06001252 { PERIPH_ID_SDMMC1, CLOCK_ID_PERIPH },
1253 { PERIPH_ID_SDMMC2, CLOCK_ID_PERIPH },
1254 { PERIPH_ID_SDMMC3, CLOCK_ID_PERIPH },
1255 { PERIPH_ID_SDMMC4, CLOCK_ID_PERIPH },
1256 { PERIPH_ID_PWM, CLOCK_ID_SFROM32KHZ },
1257 { PERIPH_ID_I2C1, CLOCK_ID_PERIPH },
1258 { PERIPH_ID_I2C2, CLOCK_ID_PERIPH },
1259 { PERIPH_ID_I2C3, CLOCK_ID_PERIPH },
1260 { PERIPH_ID_I2C4, CLOCK_ID_PERIPH },
1261 { PERIPH_ID_I2C5, CLOCK_ID_PERIPH },
1262 { PERIPH_ID_I2C6, CLOCK_ID_PERIPH },
1263 { -1, },
1264};