blob: b5b66e4224dd9951ce04c82bdf180a815cec8a73 [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);
Lokesh Vutlac321a232015-07-28 14:16:43 +053021 if (!(pllctl_reg_read(data->pll, stat) & PLLSTAT_GOSTAT_MASK))
Vitaly Andrianovef509b92014-04-04 13:16:53 -040022 break;
23 }
24}
25
Lokesh Vutlac321a232015-07-28 14:16:43 +053026static inline void bypass_main_pll(const struct pll_init_data *data)
Vitaly Andrianovef509b92014-04-04 13:16:53 -040027{
Lokesh Vutlac321a232015-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 Andrianovef509b92014-04-04 13:16:53 -040038
39 pllm = data->pll_m - 1;
Lokesh Vutlac321a232015-07-28 14:16:43 +053040 plld = (data->pll_d - 1) & CFG_PLLCTL0_PLLD_MASK;
Vitaly Andrianovef509b92014-04-04 13:16:53 -040041
Lokesh Vutlac321a232015-07-28 14:16:43 +053042 /* Program Multiplier */
43 if (data->pll == MAIN_PLL)
Vitaly Andrianovef509b92014-04-04 13:16:53 -040044 pllctl_reg_write(data->pll, mult, pllm & PLLM_MULT_LO_MASK);
45
Lokesh Vutlac321a232015-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 Andrianovef509b92014-04-04 13:16:53 -040049
Lokesh Vutlac321a232015-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 Andrianovef509b92014-04-04 13:16:53 -040059
Lokesh Vutlac321a232015-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 Andrianovef509b92014-04-04 13:16:53 -040064
Lokesh Vutlac321a232015-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 Andrianovef509b92014-04-04 13:16:53 -040069
Lokesh Vutlac321a232015-07-28 14:16:43 +053070 pllod = data->pll_od - 1;
Vitaly Andrianovef509b92014-04-04 13:16:53 -040071
Lokesh Vutlac321a232015-07-28 14:16:43 +053072 /* 100 micro sec for stabilization */
73 sdelay(210000);
Vitaly Andrianovef509b92014-04-04 13:16:53 -040074
Lokesh Vutlac321a232015-07-28 14:16:43 +053075 tmp = pllctl_reg_read(data->pll, secctl);
76
77 /* Check for Bypass */
78 if (tmp & SECCTL_BYPASS_MASK) {
79 setbits_le32(keystone_pll_regs[data->pll].reg1,
80 CFG_PLLCTL1_ENSAT_MASK);
81
82 bypass_main_pll(data);
83
84 /* 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 }
94
95 configure_mult_div(data);
96
97 /* Program Output Divider */
98 pllctl_reg_rmw(data->pll, secctl, SECCTL_OP_DIV_MASK,
99 ((pllod << SECCTL_OP_DIV_SHIFT) & SECCTL_OP_DIV_MASK));
100
101 /* 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 Andrianovef509b92014-04-04 13:16:53 -0400117 /*
118 * Set GOSET bit in PLLCMD to initiate the GO operation
119 * to change the divide
120 */
Lokesh Vutlac321a232015-07-28 14:16:43 +0530121 pllctl_reg_setbits(data->pll, cmd, PLLSTAT_GOSTAT_MASK);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400122 wait_for_completion(data);
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400123 }
124
Lokesh Vutlac321a232015-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) */
130
131 /* Enable PLL */
132 pllctl_reg_clrbits(data->pll, secctl, SECCTL_BYPASS_MASK);
133 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN_MASK);
134}
135
136void configure_secondary_pll(const struct pll_init_data *data)
137{
138 int pllod = data->pll_od - 1;
139
140 /* 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);
144
145 /* 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);
148
149 configure_mult_div(data);
150
151 /* 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);
156
157 /* Reset PLL */
158 setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
159 /* Wait for 5 micro seconds */
160 sdelay(21000);
161
162 /* 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);
166
167 /* 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);
170
171 clrbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
172 /* Wait for 500 * REFCLK cucles * (PLLD + 1) */
173 sdelay(105000);
174
175 /* 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);
186
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400187 /*
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 Andrianov61f66fd2014-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 Andrianov61f66fd2014-07-25 22:23:19 +0300231#ifndef CONFIG_SOC_K2E
232inline int get_max_arm_speed(void)
233{
Vitaly Andrianov437a7292015-06-15 08:54:15 -0400234 return get_max_speed(read_efuse_bootrom() & 0xffff, arm_speeds);
Vitaly Andrianov61f66fd2014-07-25 22:23:19 +0300235}
236#endif
Khoronzhuk, Ivan69a3b812014-10-17 21:01:16 +0300237
Vitaly Andrianov437a7292015-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}