blob: 6af20e9c782bb7dfd0b7c42f4dbdc1214c1c13ce [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass16134fd2011-08-30 06:23:13 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Tom Warrena8480ef2015-06-25 09:50:44 -07004 * (C) Copyright 2010-2015
5 * NVIDIA Corporation <www.nvidia.com>
Simon Glass16134fd2011-08-30 06:23:13 +00006 */
7
Allen Martin55d98a12012-08-31 08:30:00 +00008/* Tegra20 Clock control functions */
Simon Glass16134fd2011-08-30 06:23:13 +00009
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>
Simon Glass16134fd2011-08-30 06:23:13 +000013#include <asm/io.h>
Simon Glass16134fd2011-08-30 06:23:13 +000014#include <asm/arch/clock.h>
Tom Warrenab371962012-09-19 15:50:56 -070015#include <asm/arch/tegra.h>
16#include <asm/arch-tegra/clk_rst.h>
17#include <asm/arch-tegra/timer.h>
Simon Glassc2ea5e42011-09-21 12:40:04 +000018#include <div64.h>
Simon Glass2966cd22012-03-06 17:10:27 +000019#include <fdtdec.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Simon Glass16134fd2011-08-30 06:23:13 +000022
Svyatoslav Ryhel19a5b032023-02-14 19:35:25 +020023#include <dt-bindings/clock/tegra20-car.h>
24
Simon Glass16134fd2011-08-30 06:23:13 +000025/*
Allen Martin55d98a12012-08-31 08:30:00 +000026 * Clock types that we can use as a source. The Tegra20 has muxes for the
Simon Glassc2ea5e42011-09-21 12:40:04 +000027 * peripheral clocks, and in most cases there are four options for the clock
28 * source. This gives us a clock 'type' and exploits what commonality exists
29 * in the device.
30 *
31 * Letters are obvious, except for T which means CLK_M, and S which means the
32 * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
33 * datasheet) and PLL_M are different things. The former is the basic
34 * clock supplied to the SOC from an external oscillator. The latter is the
35 * memory clock PLL.
36 *
37 * See definitions in clock_id in the header file.
38 */
39enum clock_type_id {
40 CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
41 CLOCK_TYPE_MCPA, /* and so on */
42 CLOCK_TYPE_MCPT,
43 CLOCK_TYPE_PCM,
44 CLOCK_TYPE_PCMT,
Simon Glassd2430222012-02-03 15:13:54 +000045 CLOCK_TYPE_PCMT16, /* CLOCK_TYPE_PCMT with 16-bit divider */
Simon Glassc2ea5e42011-09-21 12:40:04 +000046 CLOCK_TYPE_PCXTS,
47 CLOCK_TYPE_PDCT,
48
49 CLOCK_TYPE_COUNT,
50 CLOCK_TYPE_NONE = -1, /* invalid clock type */
51};
52
Simon Glassc2ea5e42011-09-21 12:40:04 +000053enum {
54 CLOCK_MAX_MUX = 4 /* number of source options for each clock */
55};
56
57/*
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. Note that CLOCK_TYPE_PCXTS
60 * is special as it has 5 sources. Since it also has a different number of
61 * bits in its register for the source, we just handle it with a special
62 * case in the code.
63 */
64#define CLK(x) CLOCK_ID_ ## x
65static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX] = {
66 { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(OSC) },
67 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO) },
68 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC) },
69 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE) },
70 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC) },
Simon Glassd2430222012-02-03 15:13:54 +000071 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC) },
Simon Glassc2ea5e42011-09-21 12:40:04 +000072 { CLK(PERIPH), CLK(CGENERAL), CLK(XCPU), CLK(OSC) },
73 { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC) },
74};
75
76/*
77 * Clock peripheral IDs which sadly don't match up with PERIPH_ID. This is
78 * not in the header file since it is for purely internal use - we want
79 * callers to use the PERIPH_ID for all access to peripheral clocks to avoid
80 * confusion bewteen PERIPH_ID_... and PERIPHC_...
81 *
82 * We don't call this CLOCK_PERIPH_ID or PERIPH_CLOCK_ID as it would just be
83 * confusing.
84 *
85 * Note to SOC vendors: perhaps define a unified numbering for peripherals and
86 * use it for reset, clock enable, clock source/divider and even pinmuxing
87 * if you can.
88 */
89enum periphc_internal_id {
90 /* 0x00 */
91 PERIPHC_I2S1,
92 PERIPHC_I2S2,
93 PERIPHC_SPDIF_OUT,
94 PERIPHC_SPDIF_IN,
95 PERIPHC_PWM,
96 PERIPHC_SPI1,
97 PERIPHC_SPI2,
98 PERIPHC_SPI3,
99
100 /* 0x08 */
101 PERIPHC_XIO,
102 PERIPHC_I2C1,
103 PERIPHC_DVC_I2C,
104 PERIPHC_TWC,
105 PERIPHC_0c,
106 PERIPHC_10, /* PERIPHC_SPI1, what is this really? */
107 PERIPHC_DISP1,
108 PERIPHC_DISP2,
109
110 /* 0x10 */
111 PERIPHC_CVE,
112 PERIPHC_IDE0,
113 PERIPHC_VI,
114 PERIPHC_1c,
115 PERIPHC_SDMMC1,
116 PERIPHC_SDMMC2,
117 PERIPHC_G3D,
118 PERIPHC_G2D,
119
120 /* 0x18 */
121 PERIPHC_NDFLASH,
122 PERIPHC_SDMMC4,
123 PERIPHC_VFIR,
124 PERIPHC_EPP,
125 PERIPHC_MPE,
126 PERIPHC_MIPI,
127 PERIPHC_UART1,
128 PERIPHC_UART2,
129
130 /* 0x20 */
131 PERIPHC_HOST1X,
132 PERIPHC_21,
133 PERIPHC_TVO,
134 PERIPHC_HDMI,
135 PERIPHC_24,
136 PERIPHC_TVDAC,
137 PERIPHC_I2C2,
138 PERIPHC_EMC,
139
140 /* 0x28 */
141 PERIPHC_UART3,
142 PERIPHC_29,
143 PERIPHC_VI_SENSOR,
144 PERIPHC_2b,
145 PERIPHC_2c,
146 PERIPHC_SPI4,
147 PERIPHC_I2C3,
148 PERIPHC_SDMMC3,
149
150 /* 0x30 */
151 PERIPHC_UART4,
152 PERIPHC_UART5,
153 PERIPHC_VDE,
154 PERIPHC_OWR,
155 PERIPHC_NOR,
156 PERIPHC_CSITE,
157
158 PERIPHC_COUNT,
159
160 PERIPHC_NONE = -1,
161};
162
Simon Glassc2ea5e42011-09-21 12:40:04 +0000163/*
164 * Clock type for each peripheral clock source. We put the name in each
165 * record just so it is easy to match things up
166 */
167#define TYPE(name, type) type
168static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
169 /* 0x00 */
170 TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
171 TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
172 TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
173 TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PCM),
174 TYPE(PERIPHC_PWM, CLOCK_TYPE_PCXTS),
175 TYPE(PERIPHC_SPI1, CLOCK_TYPE_PCMT),
176 TYPE(PERIPHC_SPI22, CLOCK_TYPE_PCMT),
177 TYPE(PERIPHC_SPI3, CLOCK_TYPE_PCMT),
178
179 /* 0x08 */
180 TYPE(PERIPHC_XIO, CLOCK_TYPE_PCMT),
Simon Glassd2430222012-02-03 15:13:54 +0000181 TYPE(PERIPHC_I2C1, CLOCK_TYPE_PCMT16),
182 TYPE(PERIPHC_DVC_I2C, CLOCK_TYPE_PCMT16),
Simon Glassc2ea5e42011-09-21 12:40:04 +0000183 TYPE(PERIPHC_TWC, CLOCK_TYPE_PCMT),
184 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
185 TYPE(PERIPHC_SPI1, CLOCK_TYPE_PCMT),
186 TYPE(PERIPHC_DISP1, CLOCK_TYPE_PDCT),
187 TYPE(PERIPHC_DISP2, CLOCK_TYPE_PDCT),
188
189 /* 0x10 */
190 TYPE(PERIPHC_CVE, CLOCK_TYPE_PDCT),
191 TYPE(PERIPHC_IDE0, CLOCK_TYPE_PCMT),
192 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
193 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
194 TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PCMT),
195 TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PCMT),
196 TYPE(PERIPHC_G3D, CLOCK_TYPE_MCPA),
197 TYPE(PERIPHC_G2D, CLOCK_TYPE_MCPA),
198
199 /* 0x18 */
200 TYPE(PERIPHC_NDFLASH, CLOCK_TYPE_PCMT),
201 TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PCMT),
202 TYPE(PERIPHC_VFIR, CLOCK_TYPE_PCMT),
203 TYPE(PERIPHC_EPP, CLOCK_TYPE_MCPA),
204 TYPE(PERIPHC_MPE, CLOCK_TYPE_MCPA),
205 TYPE(PERIPHC_MIPI, CLOCK_TYPE_PCMT),
206 TYPE(PERIPHC_UART1, CLOCK_TYPE_PCMT),
207 TYPE(PERIPHC_UART2, CLOCK_TYPE_PCMT),
208
209 /* 0x20 */
210 TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MCPA),
211 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
212 TYPE(PERIPHC_TVO, CLOCK_TYPE_PDCT),
213 TYPE(PERIPHC_HDMI, CLOCK_TYPE_PDCT),
214 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
215 TYPE(PERIPHC_TVDAC, CLOCK_TYPE_PDCT),
Simon Glassd2430222012-02-03 15:13:54 +0000216 TYPE(PERIPHC_I2C2, CLOCK_TYPE_PCMT16),
Simon Glassc2ea5e42011-09-21 12:40:04 +0000217 TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPT),
218
219 /* 0x28 */
220 TYPE(PERIPHC_UART3, CLOCK_TYPE_PCMT),
221 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
222 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
223 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
224 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
225 TYPE(PERIPHC_SPI4, CLOCK_TYPE_PCMT),
Simon Glassd2430222012-02-03 15:13:54 +0000226 TYPE(PERIPHC_I2C3, CLOCK_TYPE_PCMT16),
Simon Glassc2ea5e42011-09-21 12:40:04 +0000227 TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PCMT),
228
229 /* 0x30 */
230 TYPE(PERIPHC_UART4, CLOCK_TYPE_PCMT),
231 TYPE(PERIPHC_UART5, CLOCK_TYPE_PCMT),
232 TYPE(PERIPHC_VDE, CLOCK_TYPE_PCMT),
233 TYPE(PERIPHC_OWR, CLOCK_TYPE_PCMT),
234 TYPE(PERIPHC_NOR, CLOCK_TYPE_PCMT),
235 TYPE(PERIPHC_CSITE, CLOCK_TYPE_PCMT),
236};
237
238/*
239 * This array translates a periph_id to a periphc_internal_id
240 *
241 * Not present/matched up:
242 * uint vi_sensor; _VI_SENSOR_0, 0x1A8
243 * SPDIF - which is both 0x08 and 0x0c
244 *
245 */
246#define NONE(name) (-1)
247#define OFFSET(name, value) PERIPHC_ ## name
248static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
249 /* Low word: 31:0 */
250 NONE(CPU),
251 NONE(RESERVED1),
252 NONE(RESERVED2),
253 NONE(AC97),
254 NONE(RTC),
255 NONE(TMR),
256 PERIPHC_UART1,
257 PERIPHC_UART2, /* and vfir 0x68 */
258
259 /* 0x08 */
260 NONE(GPIO),
261 PERIPHC_SDMMC2,
262 NONE(SPDIF), /* 0x08 and 0x0c, unclear which to use */
263 PERIPHC_I2S1,
264 PERIPHC_I2C1,
265 PERIPHC_NDFLASH,
266 PERIPHC_SDMMC1,
267 PERIPHC_SDMMC4,
268
269 /* 0x10 */
270 PERIPHC_TWC,
271 PERIPHC_PWM,
272 PERIPHC_I2S2,
273 PERIPHC_EPP,
274 PERIPHC_VI,
275 PERIPHC_G2D,
276 NONE(USBD),
277 NONE(ISP),
278
279 /* 0x18 */
280 PERIPHC_G3D,
281 PERIPHC_IDE0,
282 PERIPHC_DISP2,
283 PERIPHC_DISP1,
284 PERIPHC_HOST1X,
285 NONE(VCP),
286 NONE(RESERVED30),
287 NONE(CACHE2),
288
289 /* Middle word: 63:32 */
290 NONE(MEM),
291 NONE(AHBDMA),
292 NONE(APBDMA),
293 NONE(RESERVED35),
294 NONE(KBC),
295 NONE(STAT_MON),
296 NONE(PMC),
297 NONE(FUSE),
298
299 /* 0x28 */
300 NONE(KFUSE),
301 NONE(SBC1), /* SBC1, 0x34, is this SPI1? */
302 PERIPHC_NOR,
303 PERIPHC_SPI1,
304 PERIPHC_SPI2,
305 PERIPHC_XIO,
306 PERIPHC_SPI3,
307 PERIPHC_DVC_I2C,
308
309 /* 0x30 */
310 NONE(DSI),
311 PERIPHC_TVO, /* also CVE 0x40 */
312 PERIPHC_MIPI,
313 PERIPHC_HDMI,
314 PERIPHC_CSITE,
315 PERIPHC_TVDAC,
316 PERIPHC_I2C2,
317 PERIPHC_UART3,
318
319 /* 0x38 */
320 NONE(RESERVED56),
321 PERIPHC_EMC,
322 NONE(USB2),
323 NONE(USB3),
324 PERIPHC_MPE,
325 PERIPHC_VDE,
326 NONE(BSEA),
327 NONE(BSEV),
328
329 /* Upper word 95:64 */
330 NONE(SPEEDO),
331 PERIPHC_UART4,
332 PERIPHC_UART5,
333 PERIPHC_I2C3,
334 PERIPHC_SPI4,
335 PERIPHC_SDMMC3,
336 NONE(PCIE),
337 PERIPHC_OWR,
338
339 /* 0x48 */
340 NONE(AFI),
341 NONE(CORESIGHT),
Thierry Reding289fc682014-12-09 22:25:07 -0700342 NONE(PCIEXCLK),
Simon Glassc2ea5e42011-09-21 12:40:04 +0000343 NONE(AVPUCQ),
344 NONE(RESERVED76),
345 NONE(RESERVED77),
346 NONE(RESERVED78),
347 NONE(RESERVED79),
348
349 /* 0x50 */
350 NONE(RESERVED80),
351 NONE(RESERVED81),
352 NONE(RESERVED82),
353 NONE(RESERVED83),
354 NONE(IRAMA),
355 NONE(IRAMB),
356 NONE(IRAMC),
357 NONE(IRAMD),
358
359 /* 0x58 */
360 NONE(CRAM2),
361};
362
363/*
Tom Warrena8480ef2015-06-25 09:50:44 -0700364 * PLL divider shift/mask tables for all PLL IDs.
365 */
366struct clk_pll_info tegra_pll_info_table[CLOCK_ID_PLL_COUNT] = {
367 /*
368 * T20 and T25
369 * NOTE: If kcp_mask/kvco_mask == 0, they're not used in that PLL (PLLX, etc.)
370 * If lock_ena or lock_det are >31, they're not used in that PLL.
371 */
372
373 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x0F,
374 .lock_ena = 24, .lock_det = 27, .kcp_shift = 28, .kcp_mask = 3, .kvco_shift = 27, .kvco_mask = 1 }, /* PLLC */
375 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 0, .p_mask = 0,
376 .lock_ena = 0, .lock_det = 27, .kcp_shift = 1, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLM */
377 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
378 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLP */
379 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
380 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLA */
381 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x01,
382 .lock_ena = 22, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLU */
383 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
384 .lock_ena = 22, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLD */
385 { .m_shift = 0, .m_mask = 0x1F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x0F,
386 .lock_ena = 18, .lock_det = 27, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 0, .kvco_mask = 0 }, /* PLLX */
387 { .m_shift = 0, .m_mask = 0xFF, .n_shift = 8, .n_mask = 0xFF, .p_shift = 0, .p_mask = 0,
388 .lock_ena = 9, .lock_det = 11, .kcp_shift = 6, .kcp_mask = 3, .kvco_shift = 0, .kvco_mask = 1 }, /* PLLE */
389 { .m_shift = 0, .m_mask = 0x0F, .n_shift = 8, .n_mask = 0x3FF, .p_shift = 20, .p_mask = 0x07,
390 .lock_ena = 18, .lock_det = 0, .kcp_shift = 8, .kcp_mask = 0xF, .kvco_shift = 4, .kvco_mask = 0xF }, /* PLLS */
391};
392
393/*
Simon Glass16134fd2011-08-30 06:23:13 +0000394 * Get the oscillator frequency, from the corresponding hardware configuration
Tom Warren795f9d72013-01-23 14:01:01 -0700395 * field. T20 has 4 frequencies that it supports.
Simon Glass16134fd2011-08-30 06:23:13 +0000396 */
397enum clock_osc_freq clock_get_osc_freq(void)
398{
399 struct clk_rst_ctlr *clkrst =
400 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
401 u32 reg;
402
403 reg = readl(&clkrst->crc_osc_ctrl);
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +0200404 reg = (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
405
406 return reg << 2;
Simon Glass16134fd2011-08-30 06:23:13 +0000407}
408
Simon Glassc2ea5e42011-09-21 12:40:04 +0000409/* Returns a pointer to the clock source register for a peripheral */
Tom Warren795f9d72013-01-23 14:01:01 -0700410u32 *get_periph_source_reg(enum periph_id periph_id)
Simon Glassc2ea5e42011-09-21 12:40:04 +0000411{
412 struct clk_rst_ctlr *clkrst =
413 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
414 enum periphc_internal_id internal_id;
415
416 assert(clock_periph_id_isvalid(periph_id));
417 internal_id = periph_id_to_internal_id[periph_id];
418 assert(internal_id != -1);
419 return &clkrst->crc_clk_src[internal_id];
420}
421
Stephen Warren532543c2016-09-13 10:45:56 -0600422int get_periph_clock_info(enum periph_id periph_id, int *mux_bits,
423 int *divider_bits, int *type)
Simon Glassc2ea5e42011-09-21 12:40:04 +0000424{
Simon Glassc2ea5e42011-09-21 12:40:04 +0000425 enum periphc_internal_id internal_id;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000426
Stephen Warren532543c2016-09-13 10:45:56 -0600427 if (!clock_periph_id_isvalid(periph_id))
428 return -1;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000429
430 internal_id = periph_id_to_internal_id[periph_id];
Stephen Warren532543c2016-09-13 10:45:56 -0600431 if (!periphc_internal_id_isvalid(internal_id))
432 return -1;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000433
Stephen Warren532543c2016-09-13 10:45:56 -0600434 *type = clock_periph_type[internal_id];
435 if (!clock_type_id_isvalid(*type))
436 return -1;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000437
Simon Glassd2430222012-02-03 15:13:54 +0000438 /*
439 * Special cases here for the clock with a 4-bit source mux and I2C
440 * with its 16-bit divisor
441 */
Stephen Warren532543c2016-09-13 10:45:56 -0600442 if (*type == CLOCK_TYPE_PCXTS)
Stephen Warrena9812c42014-01-24 10:16:20 -0700443 *mux_bits = MASK_BITS_31_28;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000444 else
Stephen Warrena9812c42014-01-24 10:16:20 -0700445 *mux_bits = MASK_BITS_31_30;
Stephen Warren532543c2016-09-13 10:45:56 -0600446 if (*type == CLOCK_TYPE_PCMT16)
Simon Glassd2430222012-02-03 15:13:54 +0000447 *divider_bits = 16;
448 else
449 *divider_bits = 8;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000450
Stephen Warren532543c2016-09-13 10:45:56 -0600451 return 0;
452}
453
454enum clock_id get_periph_clock_id(enum periph_id periph_id, int source)
455{
456 enum periphc_internal_id internal_id;
457 int type;
458
459 if (!clock_periph_id_isvalid(periph_id))
460 return CLOCK_ID_NONE;
461
462 internal_id = periph_id_to_internal_id[periph_id];
463 if (!periphc_internal_id_isvalid(internal_id))
464 return CLOCK_ID_NONE;
465
466 type = clock_periph_type[internal_id];
467 if (!clock_type_id_isvalid(type))
468 return CLOCK_ID_NONE;
469
470 return clock_source[type][source];
471}
472
473/**
474 * Given a peripheral ID and the required source clock, this returns which
475 * value should be programmed into the source mux for that peripheral.
476 *
477 * There is special code here to handle the one source type with 5 sources.
478 *
479 * @param periph_id peripheral to start
480 * @param source PLL id of required parent clock
481 * @param mux_bits Set to number of bits in mux register: 2 or 4
482 * @param divider_bits Set to number of divider bits (8 or 16)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100483 * Return: mux value (0-4, or -1 if not found)
Stephen Warren532543c2016-09-13 10:45:56 -0600484 */
485int get_periph_clock_source(enum periph_id periph_id,
486 enum clock_id parent, int *mux_bits, int *divider_bits)
487{
488 enum clock_type_id type;
489 int mux, err;
490
491 err = get_periph_clock_info(periph_id, mux_bits, divider_bits, &type);
492 assert(!err);
493
Simon Glassc2ea5e42011-09-21 12:40:04 +0000494 for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
495 if (clock_source[type][mux] == parent)
496 return mux;
497
498 /*
499 * Not found: it might be looking for the 'S' in CLOCK_TYPE_PCXTS
500 * which is not in our table. If not, then they are asking for a
501 * source which this peripheral can't access through its mux.
502 */
503 assert(type == CLOCK_TYPE_PCXTS);
504 assert(parent == CLOCK_ID_SFROM32KHZ);
505 if (type == CLOCK_TYPE_PCXTS && parent == CLOCK_ID_SFROM32KHZ)
506 return 4; /* mux value for this clock */
507
508 /* if we get here, either us or the caller has made a mistake */
509 printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
510 parent);
511 return -1;
512}
513
Simon Glass16134fd2011-08-30 06:23:13 +0000514void clock_set_enable(enum periph_id periph_id, int enable)
515{
516 struct clk_rst_ctlr *clkrst =
517 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
518 u32 *clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
519 u32 reg;
520
521 /* Enable/disable the clock to this peripheral */
522 assert(clock_periph_id_isvalid(periph_id));
523 reg = readl(clk);
524 if (enable)
525 reg |= PERIPH_MASK(periph_id);
526 else
527 reg &= ~PERIPH_MASK(periph_id);
528 writel(reg, clk);
529}
530
Simon Glass16134fd2011-08-30 06:23:13 +0000531void reset_set_enable(enum periph_id periph_id, int enable)
532{
533 struct clk_rst_ctlr *clkrst =
534 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
535 u32 *reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
536 u32 reg;
537
538 /* Enable/disable reset to the peripheral */
539 assert(clock_periph_id_isvalid(periph_id));
540 reg = readl(reset);
541 if (enable)
542 reg |= PERIPH_MASK(periph_id);
543 else
544 reg &= ~PERIPH_MASK(periph_id);
545 writel(reg, reset);
546}
547
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900548#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass2966cd22012-03-06 17:10:27 +0000549/*
550 * Convert a device tree clock ID to our peripheral ID. They are mostly
551 * the same but we are very cautious so we check that a valid clock ID is
552 * provided.
553 *
Allen Martin55d98a12012-08-31 08:30:00 +0000554 * @param clk_id Clock ID according to tegra20 device tree binding
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100555 * Return: peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
Simon Glass2966cd22012-03-06 17:10:27 +0000556 */
Tom Warren795f9d72013-01-23 14:01:01 -0700557enum periph_id clk_id_to_periph_id(int clk_id)
Simon Glass2966cd22012-03-06 17:10:27 +0000558{
Tom Warren795f9d72013-01-23 14:01:01 -0700559 if (clk_id > PERIPH_ID_COUNT)
Simon Glass2966cd22012-03-06 17:10:27 +0000560 return PERIPH_ID_NONE;
561
562 switch (clk_id) {
Tom Warren795f9d72013-01-23 14:01:01 -0700563 case PERIPH_ID_RESERVED1:
564 case PERIPH_ID_RESERVED2:
565 case PERIPH_ID_RESERVED30:
566 case PERIPH_ID_RESERVED35:
567 case PERIPH_ID_RESERVED56:
Thierry Reding289fc682014-12-09 22:25:07 -0700568 case PERIPH_ID_PCIEXCLK:
Tom Warren795f9d72013-01-23 14:01:01 -0700569 case PERIPH_ID_RESERVED76:
570 case PERIPH_ID_RESERVED77:
571 case PERIPH_ID_RESERVED78:
572 case PERIPH_ID_RESERVED79:
573 case PERIPH_ID_RESERVED80:
574 case PERIPH_ID_RESERVED81:
575 case PERIPH_ID_RESERVED82:
576 case PERIPH_ID_RESERVED83:
577 case PERIPH_ID_RESERVED91:
Simon Glass2966cd22012-03-06 17:10:27 +0000578 return PERIPH_ID_NONE;
579 default:
580 return clk_id;
581 }
582}
Svyatoslav Ryhel19a5b032023-02-14 19:35:25 +0200583
584/*
585 * Convert a device tree clock ID to our PLL ID.
586 *
587 * @param clk_id Clock ID according to tegra20 device tree binding
588 * Return: clock ID, or CLOCK_ID_NONE if the clock ID is invalid
589 */
590enum clock_id clk_id_to_pll_id(int clk_id)
591{
592 switch (clk_id) {
593 case TEGRA20_CLK_PLL_C:
594 return CLOCK_ID_CGENERAL;
595 case TEGRA20_CLK_PLL_M:
596 return CLOCK_ID_MEMORY;
597 case TEGRA20_CLK_PLL_P:
598 return CLOCK_ID_PERIPH;
599 case TEGRA20_CLK_PLL_A:
600 return CLOCK_ID_AUDIO;
601 case TEGRA20_CLK_PLL_U:
602 return CLOCK_ID_USB;
603 case TEGRA20_CLK_PLL_D:
604 case TEGRA20_CLK_PLL_D_OUT0:
605 return CLOCK_ID_DISPLAY;
606 case TEGRA20_CLK_PLL_X:
607 return CLOCK_ID_XCPU;
608 case TEGRA20_CLK_PLL_E:
609 return CLOCK_ID_EPCI;
610 case TEGRA20_CLK_CLK_32K:
611 return CLOCK_ID_32KHZ;
612 case TEGRA20_CLK_CLK_M:
613 return CLOCK_ID_CLK_M;
614 default:
615 return CLOCK_ID_NONE;
616 }
617}
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900618#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Simon Glass2966cd22012-03-06 17:10:27 +0000619
Simon Glassc2ea5e42011-09-21 12:40:04 +0000620void clock_early_init(void)
621{
622 /*
623 * PLLP output frequency set to 216MHz
624 * PLLC output frequency set to 600Mhz
625 *
626 * TODO: Can we calculate these values instead of hard-coding?
627 */
628 switch (clock_get_osc_freq()) {
629 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
630 clock_set_rate(CLOCK_ID_PERIPH, 432, 12, 1, 8);
631 clock_set_rate(CLOCK_ID_CGENERAL, 600, 12, 0, 8);
632 break;
633
634 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
635 clock_set_rate(CLOCK_ID_PERIPH, 432, 26, 1, 8);
636 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
637 break;
638
Lucas Stacha5851fc2012-05-01 12:50:05 +0000639 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
640 clock_set_rate(CLOCK_ID_PERIPH, 432, 13, 1, 8);
641 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
642 break;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000643 case CLOCK_OSC_FREQ_19_2:
644 default:
645 /*
646 * These are not supported. It is too early to print a
647 * message and the UART likely won't work anyway due to the
648 * oscillator being wrong.
649 */
650 break;
651 }
652}
Tom Warrenfbef3552013-04-01 15:48:54 -0700653
654void arch_timer_init(void)
655{
656}
Thierry Reding4bf98692014-12-09 22:25:06 -0700657
658#define PMC_SATA_PWRGT 0x1ac
659#define PMC_SATA_PWRGT_PLLE_IDDQ_OVERRIDE (1 << 5)
660#define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL (1 << 4)
661
662#define PLLE_SS_CNTL 0x68
663#define PLLE_SS_CNTL_SSCINCINTRV(x) (((x) & 0x3f) << 24)
664#define PLLE_SS_CNTL_SSCINC(x) (((x) & 0xff) << 16)
665#define PLLE_SS_CNTL_SSCBYP (1 << 12)
666#define PLLE_SS_CNTL_INTERP_RESET (1 << 11)
667#define PLLE_SS_CNTL_BYPASS_SS (1 << 10)
668#define PLLE_SS_CNTL_SSCMAX(x) (((x) & 0x1ff) << 0)
669
670#define PLLE_BASE 0x0e8
671#define PLLE_BASE_ENABLE_CML (1 << 31)
672#define PLLE_BASE_ENABLE (1 << 30)
673#define PLLE_BASE_PLDIV_CML(x) (((x) & 0xf) << 24)
674#define PLLE_BASE_PLDIV(x) (((x) & 0x3f) << 16)
675#define PLLE_BASE_NDIV(x) (((x) & 0xff) << 8)
676#define PLLE_BASE_MDIV(x) (((x) & 0xff) << 0)
677
678#define PLLE_MISC 0x0ec
679#define PLLE_MISC_SETUP_BASE(x) (((x) & 0xffff) << 16)
680#define PLLE_MISC_PLL_READY (1 << 15)
681#define PLLE_MISC_LOCK (1 << 11)
682#define PLLE_MISC_LOCK_ENABLE (1 << 9)
683#define PLLE_MISC_SETUP_EXT(x) (((x) & 0x3) << 2)
684
685static int tegra_plle_train(void)
686{
687 unsigned int timeout = 2000;
688 unsigned long value;
689
690 value = readl(NV_PA_PMC_BASE + PMC_SATA_PWRGT);
691 value |= PMC_SATA_PWRGT_PLLE_IDDQ_OVERRIDE;
692 writel(value, NV_PA_PMC_BASE + PMC_SATA_PWRGT);
693
694 value = readl(NV_PA_PMC_BASE + PMC_SATA_PWRGT);
695 value |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
696 writel(value, NV_PA_PMC_BASE + PMC_SATA_PWRGT);
697
698 value = readl(NV_PA_PMC_BASE + PMC_SATA_PWRGT);
699 value &= ~PMC_SATA_PWRGT_PLLE_IDDQ_OVERRIDE;
700 writel(value, NV_PA_PMC_BASE + PMC_SATA_PWRGT);
701
702 do {
703 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
704 if (value & PLLE_MISC_PLL_READY)
705 break;
706
707 udelay(100);
708 } while (--timeout);
709
710 if (timeout == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900711 pr_err("timeout waiting for PLLE to become ready");
Thierry Reding4bf98692014-12-09 22:25:06 -0700712 return -ETIMEDOUT;
713 }
714
715 return 0;
716}
717
718int tegra_plle_enable(void)
719{
720 unsigned int timeout = 1000;
721 u32 value;
722 int err;
723
724 /* disable PLLE clock */
725 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
726 value &= ~PLLE_BASE_ENABLE_CML;
727 value &= ~PLLE_BASE_ENABLE;
728 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
729
730 /* clear lock enable and setup field */
731 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
732 value &= ~PLLE_MISC_LOCK_ENABLE;
733 value &= ~PLLE_MISC_SETUP_BASE(0xffff);
734 value &= ~PLLE_MISC_SETUP_EXT(0x3);
735 writel(value, NV_PA_CLK_RST_BASE + PLLE_MISC);
736
737 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
738 if ((value & PLLE_MISC_PLL_READY) == 0) {
739 err = tegra_plle_train();
740 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900741 pr_err("failed to train PLLE: %d", err);
Thierry Reding4bf98692014-12-09 22:25:06 -0700742 return err;
743 }
744 }
745
746 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
747 value |= PLLE_MISC_SETUP_BASE(0x7);
748 value |= PLLE_MISC_LOCK_ENABLE;
749 value |= PLLE_MISC_SETUP_EXT(0);
750 writel(value, NV_PA_CLK_RST_BASE + PLLE_MISC);
751
752 value = readl(NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
753 value |= PLLE_SS_CNTL_SSCBYP | PLLE_SS_CNTL_INTERP_RESET |
754 PLLE_SS_CNTL_BYPASS_SS;
755 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
756
757 value = readl(NV_PA_CLK_RST_BASE + PLLE_BASE);
758 value |= PLLE_BASE_ENABLE_CML | PLLE_BASE_ENABLE;
759 writel(value, NV_PA_CLK_RST_BASE + PLLE_BASE);
760
761 do {
762 value = readl(NV_PA_CLK_RST_BASE + PLLE_MISC);
763 if (value & PLLE_MISC_LOCK)
764 break;
765
766 udelay(2);
767 } while (--timeout);
768
769 if (timeout == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900770 pr_err("timeout waiting for PLLE to lock");
Thierry Reding4bf98692014-12-09 22:25:06 -0700771 return -ETIMEDOUT;
772 }
773
774 udelay(50);
775
776 value = readl(NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
777 value &= ~PLLE_SS_CNTL_SSCINCINTRV(0x3f);
778 value |= PLLE_SS_CNTL_SSCINCINTRV(0x18);
779
780 value &= ~PLLE_SS_CNTL_SSCINC(0xff);
781 value |= PLLE_SS_CNTL_SSCINC(0x01);
782
783 value &= ~PLLE_SS_CNTL_SSCBYP;
784 value &= ~PLLE_SS_CNTL_INTERP_RESET;
785 value &= ~PLLE_SS_CNTL_BYPASS_SS;
786
787 value &= ~PLLE_SS_CNTL_SSCMAX(0x1ff);
788 value |= PLLE_SS_CNTL_SSCMAX(0x24);
789 writel(value, NV_PA_CLK_RST_BASE + PLLE_SS_CNTL);
790
791 return 0;
792}
Stephen Warren1453d102016-09-13 10:45:55 -0600793
Svyatoslav Ryhelc93b5182023-07-03 18:06:54 +0300794struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid)
795{
796 struct clk_rst_ctlr *clkrst =
797 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
798
799 switch (clkid) {
800 case CLOCK_ID_XCPU:
801 case CLOCK_ID_EPCI:
802 case CLOCK_ID_SFROM32KHZ:
803 return &clkrst->crc_pll_simple[clkid - CLOCK_ID_FIRST_SIMPLE];
804 default:
805 return NULL;
806 }
807}
808
Stephen Warren1453d102016-09-13 10:45:55 -0600809struct periph_clk_init periph_clk_init_table[] = {
810 { PERIPH_ID_SPI1, CLOCK_ID_PERIPH },
811 { PERIPH_ID_SBC1, CLOCK_ID_PERIPH },
812 { PERIPH_ID_SBC2, CLOCK_ID_PERIPH },
813 { PERIPH_ID_SBC3, CLOCK_ID_PERIPH },
814 { PERIPH_ID_SBC4, CLOCK_ID_PERIPH },
Svyatoslav Ryhel932ec722023-02-14 19:35:24 +0200815 { PERIPH_ID_HOST1X, CLOCK_ID_CGENERAL },
816 { PERIPH_ID_DISP1, CLOCK_ID_PERIPH },
Stephen Warren1453d102016-09-13 10:45:55 -0600817 { PERIPH_ID_NDFLASH, CLOCK_ID_PERIPH },
818 { PERIPH_ID_SDMMC1, CLOCK_ID_PERIPH },
819 { PERIPH_ID_SDMMC2, CLOCK_ID_PERIPH },
820 { PERIPH_ID_SDMMC3, CLOCK_ID_PERIPH },
821 { PERIPH_ID_SDMMC4, CLOCK_ID_PERIPH },
Svyatoslav Ryhelc226fc72023-02-14 19:35:28 +0200822 { PERIPH_ID_PWM, CLOCK_ID_PERIPH },
Stephen Warren1453d102016-09-13 10:45:55 -0600823 { PERIPH_ID_DVC_I2C, CLOCK_ID_PERIPH },
824 { PERIPH_ID_I2C1, CLOCK_ID_PERIPH },
825 { PERIPH_ID_I2C2, CLOCK_ID_PERIPH },
826 { PERIPH_ID_I2C3, CLOCK_ID_PERIPH },
827 { -1, },
828};