1 // SPDX-License-Identifier: GPL-2.0+
3 * Keystone2: pll initialization
5 * (C) Copyright 2012-2014
6 * Texas Instruments Incorporated, <www.ti.com>
10 #include <asm/arch/clock.h>
11 #include <asm/arch/clock_defs.h>
13 /* DEV and ARM speed definitions as specified in DEVSPEED register */
14 int __weak speeds[DEVSPEED_NUMSPDS] = {
27 const struct keystone_pll_regs keystone_pll_regs[] = {
28 [CORE_PLL] = {KS2_MAINPLLCTL0, KS2_MAINPLLCTL1},
29 [PASS_PLL] = {KS2_PASSPLLCTL0, KS2_PASSPLLCTL1},
30 [TETRIS_PLL] = {KS2_ARMPLLCTL0, KS2_ARMPLLCTL1},
31 [DDR3A_PLL] = {KS2_DDR3APLLCTL0, KS2_DDR3APLLCTL1},
32 [DDR3B_PLL] = {KS2_DDR3BPLLCTL0, KS2_DDR3BPLLCTL1},
33 [UART_PLL] = {KS2_UARTPLLCTL0, KS2_UARTPLLCTL1},
36 inline void pll_pa_clk_sel(void)
38 setbits_le32(keystone_pll_regs[PASS_PLL].reg1, CFG_PLLCTL1_PAPLL_MASK);
41 static void wait_for_completion(const struct pll_init_data *data)
44 for (i = 0; i < 100; i++) {
46 if (!(pllctl_reg_read(data->pll, stat) & PLLSTAT_GOSTAT_MASK))
51 static inline void bypass_main_pll(const struct pll_init_data *data)
53 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLENSRC_MASK |
56 /* 4 cycles of reference clock CLKIN*/
60 static void configure_mult_div(const struct pll_init_data *data)
62 u32 pllm, plld, bwadj;
64 pllm = data->pll_m - 1;
65 plld = (data->pll_d - 1) & CFG_PLLCTL0_PLLD_MASK;
67 /* Program Multiplier */
68 if (data->pll == MAIN_PLL)
69 pllctl_reg_write(data->pll, mult, pllm & PLLM_MULT_LO_MASK);
71 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
72 CFG_PLLCTL0_PLLM_MASK,
73 pllm << CFG_PLLCTL0_PLLM_SHIFT);
76 bwadj = (data->pll_m - 1) >> 1; /* Divide pllm by 2 */
77 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
78 CFG_PLLCTL0_BWADJ_MASK,
79 (bwadj << CFG_PLLCTL0_BWADJ_SHIFT) &
80 CFG_PLLCTL0_BWADJ_MASK);
81 bwadj = bwadj >> CFG_PLLCTL0_BWADJ_BITS;
82 clrsetbits_le32(keystone_pll_regs[data->pll].reg1,
83 CFG_PLLCTL1_BWADJ_MASK, bwadj);
86 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
87 CFG_PLLCTL0_PLLD_MASK, plld);
90 void configure_main_pll(const struct pll_init_data *data)
92 u32 tmp, pllod, i, alnctl_val = 0;
95 pllod = data->pll_od - 1;
97 /* 100 micro sec for stabilization */
100 tmp = pllctl_reg_read(data->pll, secctl);
102 /* Check for Bypass */
103 if (tmp & SECCTL_BYPASS_MASK) {
104 setbits_le32(keystone_pll_regs[data->pll].reg1,
105 CFG_PLLCTL1_ENSAT_MASK);
107 bypass_main_pll(data);
109 /* Powerdown and powerup Main Pll */
110 pllctl_reg_setbits(data->pll, secctl, SECCTL_BYPASS_MASK);
111 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
115 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLPWRDN_MASK);
117 bypass_main_pll(data);
120 configure_mult_div(data);
122 /* Program Output Divider */
123 pllctl_reg_rmw(data->pll, secctl, SECCTL_OP_DIV_MASK,
124 ((pllod << SECCTL_OP_DIV_SHIFT) & SECCTL_OP_DIV_MASK));
126 /* Program PLLDIVn */
127 wait_for_completion(data);
128 for (i = 0; i < PLLDIV_MAX; i++) {
130 offset = pllctl_reg(data->pll, div1) + i;
132 offset = pllctl_reg(data->pll, div4) + (i - 3);
134 if (divn_val[i] != -1) {
135 __raw_writel(divn_val[i] | PLLDIV_ENABLE_MASK, offset);
136 alnctl_val |= BIT(i);
141 pllctl_reg_setbits(data->pll, alnctl, alnctl_val);
143 * Set GOSET bit in PLLCMD to initiate the GO operation
144 * to change the divide
146 pllctl_reg_setbits(data->pll, cmd, PLLSTAT_GOSTAT_MASK);
147 wait_for_completion(data);
151 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
152 sdelay(21000); /* Wait for a minimum of 7 us*/
153 pllctl_reg_clrbits(data->pll, ctl, PLLCTL_PLLRST_MASK);
154 sdelay(105000); /* Wait for PLL Lock time (min 50 us) */
157 pllctl_reg_clrbits(data->pll, secctl, SECCTL_BYPASS_MASK);
158 pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN_MASK);
161 void configure_secondary_pll(const struct pll_init_data *data)
163 int pllod = data->pll_od - 1;
165 /* Enable Glitch free bypass for ARM PLL */
166 if (cpu_is_k2hk() && data->pll == TETRIS_PLL)
167 clrbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
169 /* Enable Bypass mode */
170 setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_ENSAT_MASK);
171 setbits_le32(keystone_pll_regs[data->pll].reg0,
172 CFG_PLLCTL0_BYPASS_MASK);
174 configure_mult_div(data);
176 /* Program Output Divider */
177 clrsetbits_le32(keystone_pll_regs[data->pll].reg0,
178 CFG_PLLCTL0_CLKOD_MASK,
179 (pllod << CFG_PLLCTL0_CLKOD_SHIFT) &
180 CFG_PLLCTL0_CLKOD_MASK);
183 setbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
184 /* Wait for 5 micro seconds */
187 /* Select the Output of PASS PLL as input to PASS */
188 if (data->pll == PASS_PLL && cpu_is_k2hk())
191 clrbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK);
192 /* Wait for 500 * REFCLK cucles * (PLLD + 1) */
195 /* Switch to PLL mode */
196 clrbits_le32(keystone_pll_regs[data->pll].reg0,
197 CFG_PLLCTL0_BYPASS_MASK);
199 /* Select the Output of ARM PLL as input to ARM */
200 if (cpu_is_k2hk() && data->pll == TETRIS_PLL)
201 setbits_le32(KS2_MISC_CTRL, MISC_CTL1_ARM_PLL_EN);
204 void init_pll(const struct pll_init_data *data)
206 if (data->pll == MAIN_PLL)
207 configure_main_pll(data);
209 configure_secondary_pll(data);
212 * This is required to provide a delay between multiple
213 * consequent PPL configurations
220 struct pll_init_data *data;
223 for (pll = MAIN_PLL; pll < MAX_PLL_COUNT; pll++) {
224 data = get_pll_init_data(pll);
230 static int get_max_speed(u32 val, u32 speed_supported, int *spds)
234 /* Left most setbit gives the speed */
235 for (speed = DEVSPEED_NUMSPDS; speed >= 0; speed--) {
236 if ((val & BIT(speed)) & speed_supported)
240 /* If no bit is set, return minimum speed */
247 static inline u32 read_efuse_bootrom(void)
249 if (cpu_is_k2hk() && (cpu_revision() <= 1))
250 return __raw_readl(KS2_REV1_DEVSPEED);
252 return __raw_readl(KS2_EFUSE_BOOTROM);
255 int get_max_arm_speed(int *spds)
257 u32 armspeed = read_efuse_bootrom();
259 armspeed = (armspeed & DEVSPEED_ARMSPEED_MASK) >>
260 DEVSPEED_ARMSPEED_SHIFT;
262 return get_max_speed(armspeed, ARM_SUPPORTED_SPEEDS, spds);
265 int get_max_dev_speed(int *spds)
267 u32 devspeed = read_efuse_bootrom();
269 devspeed = (devspeed & DEVSPEED_DEVSPEED_MASK) >>
270 DEVSPEED_DEVSPEED_SHIFT;
272 return get_max_speed(devspeed, DEV_SUPPORTED_SPEEDS, spds);
276 * pll_freq_get - get pll frequency
277 * @pll: pll identifier
279 static unsigned long pll_freq_get(int pll)
281 unsigned long mult = 1, prediv = 1, output_div = 2;
285 if (pll == MAIN_PLL) {
286 ret = get_external_clk(sys_clk);
287 if (pllctl_reg_read(pll, ctl) & PLLCTL_PLLEN_MASK) {
289 tmp = __raw_readl(KS2_MAINPLLCTL0);
290 prediv = (tmp & CFG_PLLCTL0_PLLD_MASK) + 1;
291 mult = ((tmp & CFG_PLLCTL0_PLLM_HI_MASK) >>
292 CFG_PLLCTL0_PLLM_SHIFT |
293 (pllctl_reg_read(pll, mult) &
294 PLLM_MULT_LO_MASK)) + 1;
295 output_div = ((pllctl_reg_read(pll, secctl) &
296 SECCTL_OP_DIV_MASK) >>
297 SECCTL_OP_DIV_SHIFT) + 1;
299 ret = ret / prediv / output_div * mult;
304 ret = get_external_clk(pa_clk);
305 reg = KS2_PASSPLLCTL0;
308 ret = get_external_clk(tetris_clk);
309 reg = KS2_ARMPLLCTL0;
312 ret = get_external_clk(ddr3a_clk);
313 reg = KS2_DDR3APLLCTL0;
316 ret = get_external_clk(ddr3b_clk);
317 reg = KS2_DDR3BPLLCTL0;
320 ret = get_external_clk(uart_clk);
321 reg = KS2_UARTPLLCTL0;
327 tmp = __raw_readl(reg);
329 if (!(tmp & CFG_PLLCTL0_BYPASS_MASK)) {
330 /* Bypass disabled */
331 prediv = (tmp & CFG_PLLCTL0_PLLD_MASK) + 1;
332 mult = ((tmp & CFG_PLLCTL0_PLLM_MASK) >>
333 CFG_PLLCTL0_PLLM_SHIFT) + 1;
334 output_div = ((tmp & CFG_PLLCTL0_CLKOD_MASK) >>
335 CFG_PLLCTL0_CLKOD_SHIFT) + 1;
336 ret = ((ret / prediv) * mult) / output_div;
343 unsigned long ks_clk_get_rate(unsigned int clk)
345 unsigned long freq = 0;
349 freq = pll_freq_get(CORE_PLL);
352 freq = pll_freq_get(PASS_PLL);
356 freq = pll_freq_get(TETRIS_PLL);
359 freq = pll_freq_get(DDR3A_PLL);
363 freq = pll_freq_get(DDR3B_PLL);
367 freq = pll_freq_get(UART_PLL);
371 freq = pll_freq_get(CORE_PLL) / pll0div_read(1);
374 return pll_freq_get(CORE_PLL) / pll0div_read(2);
377 freq = pll_freq_get(CORE_PLL) / pll0div_read(3);
380 freq = pll_freq_get(CORE_PLL) / pll0div_read(4);
383 freq = ks_clk_get_rate(sys_clk0_clk) / 2;
386 freq = ks_clk_get_rate(sys_clk0_clk) / 3;
389 freq = ks_clk_get_rate(sys_clk0_clk) / 4;
392 freq = ks_clk_get_rate(sys_clk0_clk) / 6;
395 freq = ks_clk_get_rate(sys_clk0_clk) / 8;
397 case sys_clk0_12_clk:
398 freq = ks_clk_get_rate(sys_clk0_clk) / 12;
400 case sys_clk0_24_clk:
401 freq = ks_clk_get_rate(sys_clk0_clk) / 24;
404 freq = ks_clk_get_rate(sys_clk1_clk) / 3;
407 freq = ks_clk_get_rate(sys_clk1_clk) / 4;
410 freq = ks_clk_get_rate(sys_clk1_clk) / 6;
412 case sys_clk1_12_clk:
413 freq = ks_clk_get_rate(sys_clk1_clk) / 12;