blob: 5c6051e76db88807734b910010c659414060a23b [file] [log] [blame]
Vitaly Andrianovef509b92014-04-04 13:16:53 -04001/*
2 * Keystone2: pll initialization
3 *
4 * (C) Copyright 2012-2014
5 * Texas Instruments Incorporated, <www.ti.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
Vitaly Andrianovef509b92014-04-04 13:16:53 -040011#include <asm/arch/clock.h>
12#include <asm/arch/clock_defs.h>
13
Lokesh Vutla7b50e152015-07-28 14:16:44 +053014/* DEV and ARM speed definitions as specified in DEVSPEED register */
15int __weak speeds[DEVSPEED_NUMSPDS] = {
16 SPD1000,
17 SPD1200,
18 SPD1350,
19 SPD1400,
20 SPD1500,
21 SPD1400,
22 SPD1350,
23 SPD1200,
24 SPD1000,
25 SPD800,
26};
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +030027
Lokesh Vutla74af5832015-07-28 14:16:45 +053028const struct keystone_pll_regs keystone_pll_regs[] = {
29 [CORE_PLL] = {KS2_MAINPLLCTL0, KS2_MAINPLLCTL1},
30 [PASS_PLL] = {KS2_PASSPLLCTL0, KS2_PASSPLLCTL1},
31 [TETRIS_PLL] = {KS2_ARMPLLCTL0, KS2_ARMPLLCTL1},
32 [DDR3A_PLL] = {KS2_DDR3APLLCTL0, KS2_DDR3APLLCTL1},
33 [DDR3B_PLL] = {KS2_DDR3BPLLCTL0, KS2_DDR3BPLLCTL1},
Vitaly Andrianovbda920c2015-09-19 16:26:40 +053034 [UART_PLL] = {KS2_UARTPLLCTL0, KS2_UARTPLLCTL1},
Lokesh Vutla74af5832015-07-28 14:16:45 +053035};
36
Lokesh Vutla8626cb82015-10-08 11:31:47 +053037inline void pll_pa_clk_sel(void)
38{
39 setbits_le32(keystone_pll_regs[PASS_PLL].reg1, CFG_PLLCTL1_PAPLL_MASK);
40}
41
Vitaly Andrianovef509b92014-04-04 13:16:53 -040042static void wait_for_completion(const struct pll_init_data *data)
43{
44 int i;
45 for (i = 0; i < 100; i++) {
46 sdelay(450);
Lokesh Vutlac321a232015-07-28 14:16:43 +053047 if (!(pllctl_reg_read(data->pll, stat) & PLLSTAT_GOSTAT_MASK))
Vitaly Andrianovef509b92014-04-04 13:16:53 -040048 break;
49 }
50}
51
Lokesh Vutlac321a232015-07-28 14:16:43 +053052static inline void bypass_main_pll(const struct pll_init_data *data)
Vitaly Andrianovef509b92014-04-04 13:16:53 -040053{
Lokesh Vutlac321a232015-07-28 14:16:43 +053054 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLENSRC_MASK |
55 PLLCTL_PLLEN_MASK);
56
57 /* 4 cycles of reference clock CLKIN*/
58 sdelay(340);
59}
60
61static void configure_mult_div(const struct pll_init_data *data)
62{
63 u32 pllm, plld, bwadj;
Vitaly Andrianovef509b92014-04-04 13:16:53 -040064
65 pllm = data->pll_m - 1;
Lokesh Vutlac321a232015-07-28 14:16:43 +053066 plld = (data->pll_d - 1) & CFG_PLLCTL0_PLLD_MASK;
Vitaly Andrianovef509b92014-04-04 13:16:53 -040067
Lokesh Vutlac321a232015-07-28 14:16:43 +053068 /* Program Multiplier */
69 if (data->pll == MAIN_PLL)
Vitaly Andrianovef509b92014-04-04 13:16:53 -040070 pllctl_reg_write(data->pll, mult, pllm & PLLM_MULT_LO_MASK);
71
Lokesh Vutlac321a232015-07-28 14:16:43 +053072 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
73 CFG_PLLCTL0_PLLM_MASK,
74 pllm << CFG_PLLCTL0_PLLM_SHIFT);
Vitaly Andrianovef509b92014-04-04 13:16:53 -040075
Lokesh Vutlac321a232015-07-28 14:16:43 +053076 /* Program BWADJ */
77 bwadj = (data->pll_m - 1) >> 1; /* Divide pllm by 2 */
78 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
79 CFG_PLLCTL0_BWADJ_MASK,
80 (bwadj << CFG_PLLCTL0_BWADJ_SHIFT) &
81 CFG_PLLCTL0_BWADJ_MASK);
82 bwadj = bwadj >> CFG_PLLCTL0_BWADJ_BITS;
83 clrsetbits_le32(keystone_pll_regs[data->pll].reg1,
84 CFG_PLLCTL1_BWADJ_MASK, bwadj);
Vitaly Andrianovef509b92014-04-04 13:16:53 -040085
Lokesh Vutlac321a232015-07-28 14:16:43 +053086 /* Program Divider */
87 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
88 CFG_PLLCTL0_PLLD_MASK, plld);
89}
Vitaly Andrianovef509b92014-04-04 13:16:53 -040090
Lokesh Vutlac321a232015-07-28 14:16:43 +053091void configure_main_pll(const struct pll_init_data *data)
92{
93 u32 tmp, pllod, i, alnctl_val = 0;
94 u32 *offset;
Vitaly Andrianovef509b92014-04-04 13:16:53 -040095
Lokesh Vutlac321a232015-07-28 14:16:43 +053096 pllod = data->pll_od - 1;
Vitaly Andrianovef509b92014-04-04 13:16:53 -040097
Lokesh Vutlac321a232015-07-28 14:16:43 +053098 /* 100 micro sec for stabilization */
99 sdelay(210000);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400100
Lokesh Vutlac321a232015-07-28 14:16:43 +0530101 tmp = pllctl_reg_read(data->pll, secctl);
102
103 /* Check for Bypass */
104 if (tmp & SECCTL_BYPASS_MASK) {
105 setbits_le32(keystone_pll_regs[data->pll].reg1,
106 CFG_PLLCTL1_ENSAT_MASK);
107
108 bypass_main_pll(data);
109
110 /* Powerdown and powerup Main Pll */
111 pllctl_reg_setbits(data->pll, secctl, SECCTL_BYPASS_MASK);
112 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
113 /* 5 micro sec */
114 sdelay(21000);
115
116 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
117 } else {
118 bypass_main_pll(data);
119 }
120
121 configure_mult_div(data);
122
123 /* Program Output Divider */
124 pllctl_reg_rmw(data->pll, secctl, SECCTL_OP_DIV_MASK,
125 ((pllod << SECCTL_OP_DIV_SHIFT) & SECCTL_OP_DIV_MASK));
126
127 /* Program PLLDIVn */
128 wait_for_completion(data);
129 for (i = 0; i < PLLDIV_MAX; i++) {
130 if (i < 3)
131 offset = pllctl_reg(data->pll, div1) + i;
132 else
133 offset = pllctl_reg(data->pll, div4) + (i - 3);
134
135 if (divn_val[i] != -1) {
136 __raw_writel(divn_val[i] | PLLDIV_ENABLE_MASK, offset);
137 alnctl_val |= BIT(i);
138 }
139 }
140
141 if (alnctl_val) {
142 pllctl_reg_setbits(data->pll, alnctl, alnctl_val);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400143 /*
144 * Set GOSET bit in PLLCMD to initiate the GO operation
145 * to change the divide
146 */
Lokesh Vutlac321a232015-07-28 14:16:43 +0530147 pllctl_reg_setbits(data->pll, cmd, PLLSTAT_GOSTAT_MASK);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400148 wait_for_completion(data);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400149 }
150
Lokesh Vutlac321a232015-07-28 14:16:43 +0530151 /* Reset PLL */
152 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
153 sdelay(21000); /* Wait for a minimum of 7 us*/
154 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
155 sdelay(105000); /* Wait for PLL Lock time (min 50 us) */
156
157 /* Enable PLL */
158 pllctl_reg_clrbits(data->pll, secctl, SECCTL_BYPASS_MASK);
159 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN_MASK);
160}
161
162void configure_secondary_pll(const struct pll_init_data *data)
163{
164 int pllod = data->pll_od - 1;
165
166 /* Enable Bypass mode */
167 setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_ENSAT_MASK);
168 setbits_le32(keystone_pll_regs[data->pll].reg0,
169 CFG_PLLCTL0_BYPASS_MASK);
170
171 /* Enable Glitch free bypass for ARM PLL */
172 if (cpu_is_k2hk() && data->pll == TETRIS_PLL)
173 clrbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
174
175 configure_mult_div(data);
176
177 /* Program Output Divider */
178 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
179 CFG_PLLCTL0_CLKOD_MASK,
180 (pllod << CFG_PLLCTL0_CLKOD_SHIFT) &
181 CFG_PLLCTL0_CLKOD_MASK);
182
183 /* Reset PLL */
184 setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
185 /* Wait for 5 micro seconds */
186 sdelay(21000);
187
188 /* Select the Output of PASS PLL as input to PASS */
Lokesh Vutla8626cb82015-10-08 11:31:47 +0530189 if (data->pll == PASS_PLL && cpu_is_k2hk())
190 pll_pa_clk_sel();
Lokesh Vutlac321a232015-07-28 14:16:43 +0530191
192 /* Select the Output of ARM PLL as input to ARM */
193 if (data->pll == TETRIS_PLL)
194 setbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
195
196 clrbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
197 /* Wait for 500 * REFCLK cucles * (PLLD + 1) */
198 sdelay(105000);
199
200 /* Switch to PLL mode */
201 clrbits_le32(keystone_pll_regs[data->pll].reg0,
202 CFG_PLLCTL0_BYPASS_MASK);
203}
204
205void init_pll(const struct pll_init_data *data)
206{
207 if (data->pll == MAIN_PLL)
208 configure_main_pll(data);
209 else
210 configure_secondary_pll(data);
211
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400212 /*
213 * This is required to provide a delay between multiple
214 * consequent PPL configurations
215 */
216 sdelay(210000);
217}
218
Lokesh Vutla94069302015-07-28 14:16:46 +0530219void init_plls(void)
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400220{
Lokesh Vutla94069302015-07-28 14:16:46 +0530221 struct pll_init_data *data;
222 int pll;
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400223
Lokesh Vutla94069302015-07-28 14:16:46 +0530224 for (pll = MAIN_PLL; pll < MAX_PLL_COUNT; pll++) {
225 data = get_pll_init_data(pll);
226 if (data)
227 init_pll(data);
228 }
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400229}
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300230
Lokesh Vutla7b50e152015-07-28 14:16:44 +0530231static int get_max_speed(u32 val, u32 speed_supported)
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300232{
Lokesh Vutla7b50e152015-07-28 14:16:44 +0530233 int speed;
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300234
Lokesh Vutla7b50e152015-07-28 14:16:44 +0530235 /* Left most setbit gives the speed */
236 for (speed = DEVSPEED_NUMSPDS; speed >= 0; speed--) {
237 if ((val & BIT(speed)) & speed_supported)
238 return speeds[speed];
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300239 }
240
Lokesh Vutla7b50e152015-07-28 14:16:44 +0530241 /* If no bit is set, use SPD800 */
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300242 return SPD800;
243}
244
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300245static inline u32 read_efuse_bootrom(void)
246{
Lokesh Vutla7b50e152015-07-28 14:16:44 +0530247 if (cpu_is_k2hk() && (cpu_revision() <= 1))
248 return __raw_readl(KS2_REV1_DEVSPEED);
249 else
250 return __raw_readl(KS2_EFUSE_BOOTROM);
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300251}
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300252
Lokesh Vutla7b50e152015-07-28 14:16:44 +0530253int get_max_arm_speed(void)
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300254{
Lokesh Vutla7b50e152015-07-28 14:16:44 +0530255 u32 armspeed = read_efuse_bootrom();
256
257 armspeed = (armspeed & DEVSPEED_ARMSPEED_MASK) >>
258 DEVSPEED_ARMSPEED_SHIFT;
259
260 return get_max_speed(armspeed, ARM_SUPPORTED_SPEEDS);
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300261}
Khoronzhuk, Ivan69a3b812014-10-17 21:01:16 +0300262
Lokesh Vutla7b50e152015-07-28 14:16:44 +0530263int get_max_dev_speed(void)
Vitaly Andrianov437a7292015-06-15 08:54:15 -0400264{
Lokesh Vutla7b50e152015-07-28 14:16:44 +0530265 u32 devspeed = read_efuse_bootrom();
266
267 devspeed = (devspeed & DEVSPEED_DEVSPEED_MASK) >>
268 DEVSPEED_DEVSPEED_SHIFT;
269
270 return get_max_speed(devspeed, DEV_SUPPORTED_SPEEDS);
Vitaly Andrianov437a7292015-06-15 08:54:15 -0400271}
Lokesh Vutlafe772eb2015-07-28 14:16:48 +0530272
273/**
274 * pll_freq_get - get pll frequency
275 * @pll: pll identifier
276 */
277static unsigned long pll_freq_get(int pll)
278{
279 unsigned long mult = 1, prediv = 1, output_div = 2;
280 unsigned long ret;
281 u32 tmp, reg;
282
283 if (pll == MAIN_PLL) {
284 ret = external_clk[sys_clk];
285 if (pllctl_reg_read(pll, ctl) & PLLCTL_PLLEN_MASK) {
286 /* PLL mode */
287 tmp = __raw_readl(KS2_MAINPLLCTL0);
288 prediv = (tmp & CFG_PLLCTL0_PLLD_MASK) + 1;
289 mult = ((tmp & CFG_PLLCTL0_PLLM_HI_MASK) >>
290 CFG_PLLCTL0_PLLM_SHIFT |
291 (pllctl_reg_read(pll, mult) &
292 PLLM_MULT_LO_MASK)) + 1;
293 output_div = ((pllctl_reg_read(pll, secctl) &
294 SECCTL_OP_DIV_MASK) >>
295 SECCTL_OP_DIV_SHIFT) + 1;
296
297 ret = ret / prediv / output_div * mult;
298 }
299 } else {
300 switch (pll) {
301 case PASS_PLL:
302 ret = external_clk[pa_clk];
303 reg = KS2_PASSPLLCTL0;
304 break;
305 case TETRIS_PLL:
306 ret = external_clk[tetris_clk];
307 reg = KS2_ARMPLLCTL0;
308 break;
309 case DDR3A_PLL:
310 ret = external_clk[ddr3a_clk];
311 reg = KS2_DDR3APLLCTL0;
312 break;
313 case DDR3B_PLL:
314 ret = external_clk[ddr3b_clk];
315 reg = KS2_DDR3BPLLCTL0;
316 break;
Vitaly Andrianovbda920c2015-09-19 16:26:40 +0530317 case UART_PLL:
318 ret = external_clk[uart_clk];
319 reg = KS2_UARTPLLCTL0;
320 break;
Lokesh Vutlafe772eb2015-07-28 14:16:48 +0530321 default:
322 return 0;
323 }
324
325 tmp = __raw_readl(reg);
326
327 if (!(tmp & CFG_PLLCTL0_BYPASS_MASK)) {
328 /* Bypass disabled */
329 prediv = (tmp & CFG_PLLCTL0_PLLD_MASK) + 1;
330 mult = ((tmp & CFG_PLLCTL0_PLLM_MASK) >>
331 CFG_PLLCTL0_PLLM_SHIFT) + 1;
332 output_div = ((tmp & CFG_PLLCTL0_CLKOD_MASK) >>
333 CFG_PLLCTL0_CLKOD_SHIFT) + 1;
334 ret = ((ret / prediv) * mult) / output_div;
335 }
336 }
337
338 return ret;
339}
340
341unsigned long clk_get_rate(unsigned int clk)
342{
343 unsigned long freq = 0;
344
345 switch (clk) {
346 case core_pll_clk:
347 freq = pll_freq_get(CORE_PLL);
348 break;
349 case pass_pll_clk:
350 freq = pll_freq_get(PASS_PLL);
351 break;
352 case tetris_pll_clk:
353 if (!cpu_is_k2e())
354 freq = pll_freq_get(TETRIS_PLL);
355 break;
356 case ddr3a_pll_clk:
357 freq = pll_freq_get(DDR3A_PLL);
358 break;
359 case ddr3b_pll_clk:
360 if (cpu_is_k2hk())
361 freq = pll_freq_get(DDR3B_PLL);
362 break;
Vitaly Andrianove6d71e12015-09-19 16:26:41 +0530363 case uart_pll_clk:
364 if (cpu_is_k2g())
365 freq = pll_freq_get(UART_PLL);
366 break;
Lokesh Vutlafe772eb2015-07-28 14:16:48 +0530367 case sys_clk0_1_clk:
368 case sys_clk0_clk:
369 freq = pll_freq_get(CORE_PLL) / pll0div_read(1);
370 break;
371 case sys_clk1_clk:
372 return pll_freq_get(CORE_PLL) / pll0div_read(2);
373 break;
374 case sys_clk2_clk:
375 freq = pll_freq_get(CORE_PLL) / pll0div_read(3);
376 break;
377 case sys_clk3_clk:
378 freq = pll_freq_get(CORE_PLL) / pll0div_read(4);
379 break;
380 case sys_clk0_2_clk:
381 freq = clk_get_rate(sys_clk0_clk) / 2;
382 break;
383 case sys_clk0_3_clk:
384 freq = clk_get_rate(sys_clk0_clk) / 3;
385 break;
386 case sys_clk0_4_clk:
387 freq = clk_get_rate(sys_clk0_clk) / 4;
388 break;
389 case sys_clk0_6_clk:
390 freq = clk_get_rate(sys_clk0_clk) / 6;
391 break;
392 case sys_clk0_8_clk:
393 freq = clk_get_rate(sys_clk0_clk) / 8;
394 break;
395 case sys_clk0_12_clk:
396 freq = clk_get_rate(sys_clk0_clk) / 12;
397 break;
398 case sys_clk0_24_clk:
399 freq = clk_get_rate(sys_clk0_clk) / 24;
400 break;
401 case sys_clk1_3_clk:
402 freq = clk_get_rate(sys_clk1_clk) / 3;
403 break;
404 case sys_clk1_4_clk:
405 freq = clk_get_rate(sys_clk1_clk) / 4;
406 break;
407 case sys_clk1_6_clk:
408 freq = clk_get_rate(sys_clk1_clk) / 6;
409 break;
410 case sys_clk1_12_clk:
411 freq = clk_get_rate(sys_clk1_clk) / 12;
412 break;
413 default:
414 break;
415 }
416
417 return freq;
418}