blob: 625907fcda31d6a8aacdad9766edf5d1b4641fc8 [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
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +030014#define MAX_SPEEDS 13
15
Vitaly Andrianovef509b92014-04-04 13:16:53 -040016static void wait_for_completion(const struct pll_init_data *data)
17{
18 int i;
19 for (i = 0; i < 100; i++) {
20 sdelay(450);
21 if ((pllctl_reg_read(data->pll, stat) & PLLSTAT_GO) == 0)
22 break;
23 }
24}
25
Vitaly Andrianovef509b92014-04-04 13:16:53 -040026void init_pll(const struct pll_init_data *data)
27{
28 u32 tmp, tmp_ctl, pllm, plld, pllod, bwadj;
29
30 pllm = data->pll_m - 1;
31 plld = (data->pll_d - 1) & PLL_DIV_MASK;
32 pllod = (data->pll_od - 1) & PLL_CLKOD_MASK;
33
34 if (data->pll == MAIN_PLL) {
35 /* The requered delay before main PLL configuration */
36 sdelay(210000);
37
38 tmp = pllctl_reg_read(data->pll, secctl);
39
40 if (tmp & (PLLCTL_BYPASS)) {
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +030041 setbits_le32(keystone_pll_regs[data->pll].reg1,
Vitaly Andrianovef509b92014-04-04 13:16:53 -040042 BIT(MAIN_ENSAT_OFFSET));
43
44 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLEN |
45 PLLCTL_PLLENSRC);
46 sdelay(340);
47
48 pllctl_reg_setbits(data->pll, secctl, PLLCTL_BYPASS);
49 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLPWRDN);
50 sdelay(21000);
51
52 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLPWRDN);
53 } else {
54 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLEN |
55 PLLCTL_PLLENSRC);
56 sdelay(340);
57 }
58
59 pllctl_reg_write(data->pll, mult, pllm & PLLM_MULT_LO_MASK);
60
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +030061 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
62 PLLM_MULT_HI_SMASK, (pllm << 6));
Vitaly Andrianovef509b92014-04-04 13:16:53 -040063
64 /* Set the BWADJ (12 bit field) */
65 tmp_ctl = pllm >> 1; /* Divide the pllm by 2 */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +030066 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
67 PLL_BWADJ_LO_SMASK,
Vitaly Andrianovef509b92014-04-04 13:16:53 -040068 (tmp_ctl << PLL_BWADJ_LO_SHIFT));
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +030069 clrsetbits_le32(keystone_pll_regs[data->pll].reg1,
70 PLL_BWADJ_HI_MASK,
Vitaly Andrianovef509b92014-04-04 13:16:53 -040071 (tmp_ctl >> 8));
72
73 /*
74 * Set the pll divider (6 bit field) *
75 * PLLD[5:0] is located in MAINPLLCTL0
76 */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +030077 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
78 PLL_DIV_MASK, plld);
Vitaly Andrianovef509b92014-04-04 13:16:53 -040079
80 /* Set the OUTPUT DIVIDE (4 bit field) in SECCTL */
81 pllctl_reg_rmw(data->pll, secctl, PLL_CLKOD_SMASK,
82 (pllod << PLL_CLKOD_SHIFT));
83 wait_for_completion(data);
84
85 pllctl_reg_write(data->pll, div1, PLLM_RATIO_DIV1);
86 pllctl_reg_write(data->pll, div2, PLLM_RATIO_DIV2);
87 pllctl_reg_write(data->pll, div3, PLLM_RATIO_DIV3);
88 pllctl_reg_write(data->pll, div4, PLLM_RATIO_DIV4);
89 pllctl_reg_write(data->pll, div5, PLLM_RATIO_DIV5);
90
91 pllctl_reg_setbits(data->pll, alnctl, 0x1f);
92
93 /*
94 * Set GOSET bit in PLLCMD to initiate the GO operation
95 * to change the divide
96 */
97 pllctl_reg_setbits(data->pll, cmd, PLLSTAT_GO);
98 sdelay(1500); /* wait for the phase adj */
99 wait_for_completion(data);
100
101 /* Reset PLL */
102 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLRST);
103 sdelay(21000); /* Wait for a minimum of 7 us*/
104 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLRST);
105 sdelay(105000); /* Wait for PLL Lock time (min 50 us) */
106
107 pllctl_reg_clrbits(data->pll, secctl, PLLCTL_BYPASS);
108
109 tmp = pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN);
110
Hao Zhang4dca7f02014-07-16 00:59:23 +0300111#ifndef CONFIG_SOC_K2E
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400112 } else if (data->pll == TETRIS_PLL) {
113 bwadj = pllm >> 1;
114 /* 1.5 Set PLLCTL0[BYPASS] =1 (enable bypass), */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300115 setbits_le32(keystone_pll_regs[data->pll].reg0, PLLCTL_BYPASS);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400116 /*
117 * Set CHIPMISCCTL1[13] = 0 (enable glitchfree bypass)
118 * only applicable for Kepler
119 */
Khoronzhuk, Ivan3d315382014-07-09 23:44:44 +0300120 clrbits_le32(KS2_MISC_CTRL, KS2_ARM_PLL_EN);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400121 /* 2 In PLLCTL1, write PLLRST = 1 (PLL is reset) */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300122 setbits_le32(keystone_pll_regs[data->pll].reg1 ,
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400123 PLL_PLLRST | PLLCTL_ENSAT);
124
125 /*
126 * 3 Program PLLM and PLLD in PLLCTL0 register
127 * 4 Program BWADJ[7:0] in PLLCTL0 and BWADJ[11:8] in
128 * PLLCTL1 register. BWADJ value must be set
129 * to ((PLLM + 1) >> 1) – 1)
130 */
131 tmp = ((bwadj & PLL_BWADJ_LO_MASK) << PLL_BWADJ_LO_SHIFT) |
132 (pllm << 6) |
133 (plld & PLL_DIV_MASK) |
134 (pllod << PLL_CLKOD_SHIFT) | PLLCTL_BYPASS;
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300135 __raw_writel(tmp, keystone_pll_regs[data->pll].reg0);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400136
137 /* Set BWADJ[11:8] bits */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300138 tmp = __raw_readl(keystone_pll_regs[data->pll].reg1);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400139 tmp &= ~(PLL_BWADJ_HI_MASK);
140 tmp |= ((bwadj>>8) & PLL_BWADJ_HI_MASK);
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300141 __raw_writel(tmp, keystone_pll_regs[data->pll].reg1);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400142 /*
143 * 5 Wait for at least 5 us based on the reference
144 * clock (PLL reset time)
145 */
146 sdelay(21000); /* Wait for a minimum of 7 us*/
147
148 /* 6 In PLLCTL1, write PLLRST = 0 (PLL reset is released) */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300149 clrbits_le32(keystone_pll_regs[data->pll].reg1, PLL_PLLRST);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400150 /*
151 * 7 Wait for at least 500 * REFCLK cycles * (PLLD + 1)
152 * (PLL lock time)
153 */
154 sdelay(105000);
155 /* 8 disable bypass */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300156 clrbits_le32(keystone_pll_regs[data->pll].reg0, PLLCTL_BYPASS);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400157 /*
158 * 9 Set CHIPMISCCTL1[13] = 1 (disable glitchfree bypass)
159 * only applicable for Kepler
160 */
Khoronzhuk, Ivan3d315382014-07-09 23:44:44 +0300161 setbits_le32(KS2_MISC_CTRL, KS2_ARM_PLL_EN);
Hao Zhang4dca7f02014-07-16 00:59:23 +0300162#endif
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400163 } else {
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300164 setbits_le32(keystone_pll_regs[data->pll].reg1, PLLCTL_ENSAT);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400165 /*
166 * process keeps state of Bypass bit while programming
167 * all other DDR PLL settings
168 */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300169 tmp = __raw_readl(keystone_pll_regs[data->pll].reg0);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400170 tmp &= PLLCTL_BYPASS; /* clear everything except Bypass */
171
172 /*
173 * Set the BWADJ[7:0], PLLD[5:0] and PLLM to PLLCTL0,
174 * bypass disabled
175 */
176 bwadj = pllm >> 1;
Khoronzhuk, Ivan1c4044a2014-08-11 11:59:42 +0300177 tmp |= ((bwadj & PLL_BWADJ_LO_MASK) << PLL_BWADJ_LO_SHIFT) |
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400178 (pllm << PLL_MULT_SHIFT) |
179 (plld & PLL_DIV_MASK) |
180 (pllod << PLL_CLKOD_SHIFT);
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300181 __raw_writel(tmp, keystone_pll_regs[data->pll].reg0);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400182
183 /* Set BWADJ[11:8] bits */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300184 tmp = __raw_readl(keystone_pll_regs[data->pll].reg1);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400185 tmp &= ~(PLL_BWADJ_HI_MASK);
186 tmp |= ((bwadj >> 8) & PLL_BWADJ_HI_MASK);
187
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300188 __raw_writel(tmp, keystone_pll_regs[data->pll].reg1);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400189
190 /* Reset bit: bit 14 for both DDR3 & PASS PLL */
191 tmp = PLL_PLLRST;
192 /* Set RESET bit = 1 */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300193 setbits_le32(keystone_pll_regs[data->pll].reg1, tmp);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400194 /* Wait for a minimum of 7 us*/
195 sdelay(21000);
196 /* Clear RESET bit */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300197 clrbits_le32(keystone_pll_regs[data->pll].reg1, tmp);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400198 sdelay(105000);
199
200 /* clear BYPASS (Enable PLL Mode) */
Khoronzhuk, Ivan8dfc15f2014-07-09 23:44:47 +0300201 clrbits_le32(keystone_pll_regs[data->pll].reg0, PLLCTL_BYPASS);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400202 sdelay(21000); /* Wait for a minimum of 7 us*/
203 }
204
205 /*
206 * This is required to provide a delay between multiple
207 * consequent PPL configurations
208 */
209 sdelay(210000);
210}
211
212void init_plls(int num_pll, struct pll_init_data *config)
213{
214 int i;
215
216 for (i = 0; i < num_pll; i++)
217 init_pll(&config[i]);
218}
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300219
220static int get_max_speed(u32 val, int *speeds)
221{
222 int j;
223
224 if (!val)
225 return speeds[0];
226
227 for (j = 1; j < MAX_SPEEDS; j++) {
228 if (val == 1)
229 return speeds[j];
230 val >>= 1;
231 }
232
233 return SPD800;
234}
235
236#ifdef CONFIG_SOC_K2HK
237static u32 read_efuse_bootrom(void)
238{
239 return (cpu_revision() > 1) ? __raw_readl(KS2_EFUSE_BOOTROM) :
240 __raw_readl(KS2_REV1_DEVSPEED);
241}
242#else
243static inline u32 read_efuse_bootrom(void)
244{
245 return __raw_readl(KS2_EFUSE_BOOTROM);
246}
247#endif
248
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300249#ifndef CONFIG_SOC_K2E
250inline int get_max_arm_speed(void)
251{
Vitaly Andrianov437a7292015-06-15 08:54:15 -0400252 return get_max_speed(read_efuse_bootrom() & 0xffff, arm_speeds);
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300253}
254#endif
Khoronzhuk, Ivan69a3b812014-10-17 21:01:16 +0300255
Vitaly Andrianov437a7292015-06-15 08:54:15 -0400256inline int get_max_dev_speed(void)
257{
258 return get_max_speed((read_efuse_bootrom() >> 16) & 0xffff, dev_speeds);
259}
260
Khoronzhuk, Ivan69a3b812014-10-17 21:01:16 +0300261void pass_pll_pa_clk_enable(void)
262{
263 u32 reg;
264
265 reg = readl(keystone_pll_regs[PASS_PLL].reg1);
266
267 reg |= PLLCTL_PAPLL;
268 writel(reg, keystone_pll_regs[PASS_PLL].reg1);
269
270 /* wait till clock is enabled */
271 sdelay(15000);
272}