blob: aaeef79bd3969ff6fe634acf8db9bb36556da18b [file] [log] [blame]
Vitaly Andrianov7bcf4d62014-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 Andrianov7bcf4d62014-04-04 13:16:53 -040011#include <asm/arch/clock.h>
12#include <asm/arch/clock_defs.h>
13
Lokesh Vutla9da9afa2015-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 Andrianov047e7802014-07-25 22:23:19 +030027
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040028static void wait_for_completion(const struct pll_init_data *data)
29{
30 int i;
31 for (i = 0; i < 100; i++) {
32 sdelay(450);
Lokesh Vutla70438fc2015-07-28 14:16:43 +053033 if (!(pllctl_reg_read(data->pll, stat) & PLLSTAT_GOSTAT_MASK))
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040034 break;
35 }
36}
37
Lokesh Vutla70438fc2015-07-28 14:16:43 +053038static inline void bypass_main_pll(const struct pll_init_data *data)
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040039{
Lokesh Vutla70438fc2015-07-28 14:16:43 +053040 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLENSRC_MASK |
41 PLLCTL_PLLEN_MASK);
42
43 /* 4 cycles of reference clock CLKIN*/
44 sdelay(340);
45}
46
47static void configure_mult_div(const struct pll_init_data *data)
48{
49 u32 pllm, plld, bwadj;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040050
51 pllm = data->pll_m - 1;
Lokesh Vutla70438fc2015-07-28 14:16:43 +053052 plld = (data->pll_d - 1) & CFG_PLLCTL0_PLLD_MASK;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040053
Lokesh Vutla70438fc2015-07-28 14:16:43 +053054 /* Program Multiplier */
55 if (data->pll == MAIN_PLL)
56 pllctl_reg_write(data->pll, mult, pllm & PLLM_MULT_LO_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040057
Lokesh Vutla70438fc2015-07-28 14:16:43 +053058 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
59 CFG_PLLCTL0_PLLM_MASK,
60 pllm << CFG_PLLCTL0_PLLM_SHIFT);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040061
Lokesh Vutla70438fc2015-07-28 14:16:43 +053062 /* Program BWADJ */
63 bwadj = (data->pll_m - 1) >> 1; /* Divide pllm by 2 */
64 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
65 CFG_PLLCTL0_BWADJ_MASK,
66 (bwadj << CFG_PLLCTL0_BWADJ_SHIFT) &
67 CFG_PLLCTL0_BWADJ_MASK);
68 bwadj = bwadj >> CFG_PLLCTL0_BWADJ_BITS;
69 clrsetbits_le32(keystone_pll_regs[data->pll].reg1,
70 CFG_PLLCTL1_BWADJ_MASK, bwadj);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040071
Lokesh Vutla70438fc2015-07-28 14:16:43 +053072 /* Program Divider */
73 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
74 CFG_PLLCTL0_PLLD_MASK, plld);
75}
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040076
Lokesh Vutla70438fc2015-07-28 14:16:43 +053077void configure_main_pll(const struct pll_init_data *data)
78{
79 u32 tmp, pllod, i, alnctl_val = 0;
80 u32 *offset;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040081
Lokesh Vutla70438fc2015-07-28 14:16:43 +053082 pllod = data->pll_od - 1;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040083
Lokesh Vutla70438fc2015-07-28 14:16:43 +053084 /* 100 micro sec for stabilization */
85 sdelay(210000);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040086
Lokesh Vutla70438fc2015-07-28 14:16:43 +053087 tmp = pllctl_reg_read(data->pll, secctl);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040088
Lokesh Vutla70438fc2015-07-28 14:16:43 +053089 /* Check for Bypass */
90 if (tmp & SECCTL_BYPASS_MASK) {
91 setbits_le32(keystone_pll_regs[data->pll].reg1,
92 CFG_PLLCTL1_ENSAT_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040093
Lokesh Vutla70438fc2015-07-28 14:16:43 +053094 bypass_main_pll(data);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040095
Lokesh Vutla70438fc2015-07-28 14:16:43 +053096 /* Powerdown and powerup Main Pll */
97 pllctl_reg_setbits(data->pll, secctl, SECCTL_BYPASS_MASK);
98 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
99 /* 5 micro sec */
100 sdelay(21000);
101
102 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
103 } else {
104 bypass_main_pll(data);
105 }
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400106
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530107 configure_mult_div(data);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400108
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530109 /* Program Output Divider */
110 pllctl_reg_rmw(data->pll, secctl, SECCTL_OP_DIV_MASK,
111 ((pllod << SECCTL_OP_DIV_SHIFT) & SECCTL_OP_DIV_MASK));
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400112
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530113 /* Program PLLDIVn */
114 wait_for_completion(data);
115 for (i = 0; i < PLLDIV_MAX; i++) {
116 if (i < 3)
117 offset = pllctl_reg(data->pll, div1) + i;
118 else
119 offset = pllctl_reg(data->pll, div4) + (i - 3);
120
121 if (divn_val[i] != -1) {
122 __raw_writel(divn_val[i] | PLLDIV_ENABLE_MASK, offset);
123 alnctl_val |= BIT(i);
124 }
125 }
126
127 if (alnctl_val) {
128 pllctl_reg_setbits(data->pll, alnctl, alnctl_val);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400129 /*
130 * Set GOSET bit in PLLCMD to initiate the GO operation
131 * to change the divide
132 */
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530133 pllctl_reg_setbits(data->pll, cmd, PLLSTAT_GOSTAT_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400134 wait_for_completion(data);
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530135 }
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400136
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530137 /* Reset PLL */
138 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
139 sdelay(21000); /* Wait for a minimum of 7 us*/
140 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
141 sdelay(105000); /* Wait for PLL Lock time (min 50 us) */
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400142
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530143 /* Enable PLL */
144 pllctl_reg_clrbits(data->pll, secctl, SECCTL_BYPASS_MASK);
145 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN_MASK);
146}
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400147
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530148void configure_secondary_pll(const struct pll_init_data *data)
149{
150 int pllod = data->pll_od - 1;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400151
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530152 /* Enable Bypass mode */
153 setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_ENSAT_MASK);
154 setbits_le32(keystone_pll_regs[data->pll].reg0,
155 CFG_PLLCTL0_BYPASS_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400156
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530157 /* Enable Glitch free bypass for ARM PLL */
158 if (cpu_is_k2hk() && data->pll == TETRIS_PLL)
159 clrbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400160
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530161 configure_mult_div(data);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400162
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530163 /* Program Output Divider */
164 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
165 CFG_PLLCTL0_CLKOD_MASK,
166 (pllod << CFG_PLLCTL0_CLKOD_SHIFT) &
167 CFG_PLLCTL0_CLKOD_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400168
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530169 /* Reset PLL */
170 setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
171 /* Wait for 5 micro seconds */
172 sdelay(21000);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400173
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530174 /* Select the Output of PASS PLL as input to PASS */
175 if (data->pll == PASS_PLL)
176 setbits_le32(keystone_pll_regs[data->pll].reg1,
177 CFG_PLLCTL1_PAPLL_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400178
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530179 /* Select the Output of ARM PLL as input to ARM */
180 if (data->pll == TETRIS_PLL)
181 setbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400182
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530183 clrbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
184 /* Wait for 500 * REFCLK cucles * (PLLD + 1) */
185 sdelay(105000);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400186
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530187 /* Switch to PLL mode */
188 clrbits_le32(keystone_pll_regs[data->pll].reg0,
189 CFG_PLLCTL0_BYPASS_MASK);
190}
191
192void init_pll(const struct pll_init_data *data)
193{
194 if (data->pll == MAIN_PLL)
195 configure_main_pll(data);
196 else
197 configure_secondary_pll(data);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400198
199 /*
200 * This is required to provide a delay between multiple
201 * consequent PPL configurations
202 */
203 sdelay(210000);
204}
205
206void init_plls(int num_pll, struct pll_init_data *config)
207{
208 int i;
209
210 for (i = 0; i < num_pll; i++)
211 init_pll(&config[i]);
212}
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300213
Lokesh Vutla9da9afa2015-07-28 14:16:44 +0530214static int get_max_speed(u32 val, u32 speed_supported)
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300215{
Lokesh Vutla9da9afa2015-07-28 14:16:44 +0530216 int speed;
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300217
Lokesh Vutla9da9afa2015-07-28 14:16:44 +0530218 /* Left most setbit gives the speed */
219 for (speed = DEVSPEED_NUMSPDS; speed >= 0; speed--) {
220 if ((val & BIT(speed)) & speed_supported)
221 return speeds[speed];
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300222 }
223
Lokesh Vutla9da9afa2015-07-28 14:16:44 +0530224 /* If no bit is set, use SPD800 */
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300225 return SPD800;
226}
227
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300228static inline u32 read_efuse_bootrom(void)
229{
Lokesh Vutla9da9afa2015-07-28 14:16:44 +0530230 if (cpu_is_k2hk() && (cpu_revision() <= 1))
231 return __raw_readl(KS2_REV1_DEVSPEED);
232 else
233 return __raw_readl(KS2_EFUSE_BOOTROM);
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300234}
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300235
Lokesh Vutla9da9afa2015-07-28 14:16:44 +0530236int get_max_arm_speed(void)
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300237{
Lokesh Vutla9da9afa2015-07-28 14:16:44 +0530238 u32 armspeed = read_efuse_bootrom();
239
240 armspeed = (armspeed & DEVSPEED_ARMSPEED_MASK) >>
241 DEVSPEED_ARMSPEED_SHIFT;
242
243 return get_max_speed(armspeed, ARM_SUPPORTED_SPEEDS);
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300244}
Khoronzhuk, Ivan9f95c1b2014-10-17 21:01:16 +0300245
Lokesh Vutla9da9afa2015-07-28 14:16:44 +0530246int get_max_dev_speed(void)
Vitaly Andrianov6eb54a42015-06-15 08:54:15 -0400247{
Lokesh Vutla9da9afa2015-07-28 14:16:44 +0530248 u32 devspeed = read_efuse_bootrom();
249
250 devspeed = (devspeed & DEVSPEED_DEVSPEED_MASK) >>
251 DEVSPEED_DEVSPEED_SHIFT;
252
253 return get_max_speed(devspeed, DEV_SUPPORTED_SPEEDS);
Vitaly Andrianov6eb54a42015-06-15 08:54:15 -0400254}