blob: b5b66e4224dd9951ce04c82bdf180a815cec8a73 [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
Vitaly Andrianov047e7802014-07-25 22:23:19 +030014#define MAX_SPEEDS 13
15
Vitaly Andrianov7bcf4d62014-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);
Lokesh Vutla70438fc2015-07-28 14:16:43 +053021 if (!(pllctl_reg_read(data->pll, stat) & PLLSTAT_GOSTAT_MASK))
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040022 break;
23 }
24}
25
Lokesh Vutla70438fc2015-07-28 14:16:43 +053026static inline void bypass_main_pll(const struct pll_init_data *data)
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040027{
Lokesh Vutla70438fc2015-07-28 14:16:43 +053028 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLENSRC_MASK |
29 PLLCTL_PLLEN_MASK);
30
31 /* 4 cycles of reference clock CLKIN*/
32 sdelay(340);
33}
34
35static void configure_mult_div(const struct pll_init_data *data)
36{
37 u32 pllm, plld, bwadj;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040038
39 pllm = data->pll_m - 1;
Lokesh Vutla70438fc2015-07-28 14:16:43 +053040 plld = (data->pll_d - 1) & CFG_PLLCTL0_PLLD_MASK;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040041
Lokesh Vutla70438fc2015-07-28 14:16:43 +053042 /* Program Multiplier */
43 if (data->pll == MAIN_PLL)
44 pllctl_reg_write(data->pll, mult, pllm & PLLM_MULT_LO_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040045
Lokesh Vutla70438fc2015-07-28 14:16:43 +053046 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
47 CFG_PLLCTL0_PLLM_MASK,
48 pllm << CFG_PLLCTL0_PLLM_SHIFT);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040049
Lokesh Vutla70438fc2015-07-28 14:16:43 +053050 /* Program BWADJ */
51 bwadj = (data->pll_m - 1) >> 1; /* Divide pllm by 2 */
52 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
53 CFG_PLLCTL0_BWADJ_MASK,
54 (bwadj << CFG_PLLCTL0_BWADJ_SHIFT) &
55 CFG_PLLCTL0_BWADJ_MASK);
56 bwadj = bwadj >> CFG_PLLCTL0_BWADJ_BITS;
57 clrsetbits_le32(keystone_pll_regs[data->pll].reg1,
58 CFG_PLLCTL1_BWADJ_MASK, bwadj);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040059
Lokesh Vutla70438fc2015-07-28 14:16:43 +053060 /* Program Divider */
61 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
62 CFG_PLLCTL0_PLLD_MASK, plld);
63}
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040064
Lokesh Vutla70438fc2015-07-28 14:16:43 +053065void configure_main_pll(const struct pll_init_data *data)
66{
67 u32 tmp, pllod, i, alnctl_val = 0;
68 u32 *offset;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040069
Lokesh Vutla70438fc2015-07-28 14:16:43 +053070 pllod = data->pll_od - 1;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040071
Lokesh Vutla70438fc2015-07-28 14:16:43 +053072 /* 100 micro sec for stabilization */
73 sdelay(210000);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040074
Lokesh Vutla70438fc2015-07-28 14:16:43 +053075 tmp = pllctl_reg_read(data->pll, secctl);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040076
Lokesh Vutla70438fc2015-07-28 14:16:43 +053077 /* Check for Bypass */
78 if (tmp & SECCTL_BYPASS_MASK) {
79 setbits_le32(keystone_pll_regs[data->pll].reg1,
80 CFG_PLLCTL1_ENSAT_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040081
Lokesh Vutla70438fc2015-07-28 14:16:43 +053082 bypass_main_pll(data);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040083
Lokesh Vutla70438fc2015-07-28 14:16:43 +053084 /* Powerdown and powerup Main Pll */
85 pllctl_reg_setbits(data->pll, secctl, SECCTL_BYPASS_MASK);
86 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
87 /* 5 micro sec */
88 sdelay(21000);
89
90 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
91 } else {
92 bypass_main_pll(data);
93 }
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040094
Lokesh Vutla70438fc2015-07-28 14:16:43 +053095 configure_mult_div(data);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040096
Lokesh Vutla70438fc2015-07-28 14:16:43 +053097 /* Program Output Divider */
98 pllctl_reg_rmw(data->pll, secctl, SECCTL_OP_DIV_MASK,
99 ((pllod << SECCTL_OP_DIV_SHIFT) & SECCTL_OP_DIV_MASK));
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400100
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530101 /* Program PLLDIVn */
102 wait_for_completion(data);
103 for (i = 0; i < PLLDIV_MAX; i++) {
104 if (i < 3)
105 offset = pllctl_reg(data->pll, div1) + i;
106 else
107 offset = pllctl_reg(data->pll, div4) + (i - 3);
108
109 if (divn_val[i] != -1) {
110 __raw_writel(divn_val[i] | PLLDIV_ENABLE_MASK, offset);
111 alnctl_val |= BIT(i);
112 }
113 }
114
115 if (alnctl_val) {
116 pllctl_reg_setbits(data->pll, alnctl, alnctl_val);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400117 /*
118 * Set GOSET bit in PLLCMD to initiate the GO operation
119 * to change the divide
120 */
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530121 pllctl_reg_setbits(data->pll, cmd, PLLSTAT_GOSTAT_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400122 wait_for_completion(data);
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530123 }
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400124
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530125 /* Reset PLL */
126 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
127 sdelay(21000); /* Wait for a minimum of 7 us*/
128 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
129 sdelay(105000); /* Wait for PLL Lock time (min 50 us) */
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400130
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530131 /* Enable PLL */
132 pllctl_reg_clrbits(data->pll, secctl, SECCTL_BYPASS_MASK);
133 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN_MASK);
134}
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400135
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530136void configure_secondary_pll(const struct pll_init_data *data)
137{
138 int pllod = data->pll_od - 1;
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400139
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530140 /* Enable Bypass mode */
141 setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_ENSAT_MASK);
142 setbits_le32(keystone_pll_regs[data->pll].reg0,
143 CFG_PLLCTL0_BYPASS_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400144
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530145 /* Enable Glitch free bypass for ARM PLL */
146 if (cpu_is_k2hk() && data->pll == TETRIS_PLL)
147 clrbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400148
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530149 configure_mult_div(data);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400150
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530151 /* Program Output Divider */
152 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
153 CFG_PLLCTL0_CLKOD_MASK,
154 (pllod << CFG_PLLCTL0_CLKOD_SHIFT) &
155 CFG_PLLCTL0_CLKOD_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400156
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530157 /* Reset PLL */
158 setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
159 /* Wait for 5 micro seconds */
160 sdelay(21000);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400161
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530162 /* Select the Output of PASS PLL as input to PASS */
163 if (data->pll == PASS_PLL)
164 setbits_le32(keystone_pll_regs[data->pll].reg1,
165 CFG_PLLCTL1_PAPLL_MASK);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400166
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530167 /* Select the Output of ARM PLL as input to ARM */
168 if (data->pll == TETRIS_PLL)
169 setbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400170
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530171 clrbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
172 /* Wait for 500 * REFCLK cucles * (PLLD + 1) */
173 sdelay(105000);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400174
Lokesh Vutla70438fc2015-07-28 14:16:43 +0530175 /* Switch to PLL mode */
176 clrbits_le32(keystone_pll_regs[data->pll].reg0,
177 CFG_PLLCTL0_BYPASS_MASK);
178}
179
180void init_pll(const struct pll_init_data *data)
181{
182 if (data->pll == MAIN_PLL)
183 configure_main_pll(data);
184 else
185 configure_secondary_pll(data);
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400186
187 /*
188 * This is required to provide a delay between multiple
189 * consequent PPL configurations
190 */
191 sdelay(210000);
192}
193
194void init_plls(int num_pll, struct pll_init_data *config)
195{
196 int i;
197
198 for (i = 0; i < num_pll; i++)
199 init_pll(&config[i]);
200}
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300201
202static int get_max_speed(u32 val, int *speeds)
203{
204 int j;
205
206 if (!val)
207 return speeds[0];
208
209 for (j = 1; j < MAX_SPEEDS; j++) {
210 if (val == 1)
211 return speeds[j];
212 val >>= 1;
213 }
214
215 return SPD800;
216}
217
218#ifdef CONFIG_SOC_K2HK
219static u32 read_efuse_bootrom(void)
220{
221 return (cpu_revision() > 1) ? __raw_readl(KS2_EFUSE_BOOTROM) :
222 __raw_readl(KS2_REV1_DEVSPEED);
223}
224#else
225static inline u32 read_efuse_bootrom(void)
226{
227 return __raw_readl(KS2_EFUSE_BOOTROM);
228}
229#endif
230
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300231#ifndef CONFIG_SOC_K2E
232inline int get_max_arm_speed(void)
233{
Vitaly Andrianov6eb54a42015-06-15 08:54:15 -0400234 return get_max_speed(read_efuse_bootrom() & 0xffff, arm_speeds);
Vitaly Andrianov047e7802014-07-25 22:23:19 +0300235}
236#endif
Khoronzhuk, Ivan9f95c1b2014-10-17 21:01:16 +0300237
Vitaly Andrianov6eb54a42015-06-15 08:54:15 -0400238inline int get_max_dev_speed(void)
239{
240 return get_max_speed((read_efuse_bootrom() >> 16) & 0xffff, dev_speeds);
241}