Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 1 | /* |
| 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 Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 11 | #include <asm/arch/clock.h> |
| 12 | #include <asm/arch/clock_defs.h> |
| 13 | |
Vitaly Andrianov | 61f66fd | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 14 | #define MAX_SPEEDS 13 |
| 15 | |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 16 | static 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 Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 26 | void 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, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 41 | setbits_le32(keystone_pll_regs[data->pll].reg1, |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 42 | 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, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 61 | clrsetbits_le32(keystone_pll_regs[data->pll].reg0, |
| 62 | PLLM_MULT_HI_SMASK, (pllm << 6)); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 63 | |
| 64 | /* Set the BWADJ (12 bit field) */ |
| 65 | tmp_ctl = pllm >> 1; /* Divide the pllm by 2 */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 66 | clrsetbits_le32(keystone_pll_regs[data->pll].reg0, |
| 67 | PLL_BWADJ_LO_SMASK, |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 68 | (tmp_ctl << PLL_BWADJ_LO_SHIFT)); |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 69 | clrsetbits_le32(keystone_pll_regs[data->pll].reg1, |
| 70 | PLL_BWADJ_HI_MASK, |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 71 | (tmp_ctl >> 8)); |
| 72 | |
| 73 | /* |
| 74 | * Set the pll divider (6 bit field) * |
| 75 | * PLLD[5:0] is located in MAINPLLCTL0 |
| 76 | */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 77 | clrsetbits_le32(keystone_pll_regs[data->pll].reg0, |
| 78 | PLL_DIV_MASK, plld); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 79 | |
| 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 Zhang | 4dca7f0 | 2014-07-16 00:59:23 +0300 | [diff] [blame] | 111 | #ifndef CONFIG_SOC_K2E |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 112 | } else if (data->pll == TETRIS_PLL) { |
| 113 | bwadj = pllm >> 1; |
| 114 | /* 1.5 Set PLLCTL0[BYPASS] =1 (enable bypass), */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 115 | setbits_le32(keystone_pll_regs[data->pll].reg0, PLLCTL_BYPASS); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 116 | /* |
| 117 | * Set CHIPMISCCTL1[13] = 0 (enable glitchfree bypass) |
| 118 | * only applicable for Kepler |
| 119 | */ |
Khoronzhuk, Ivan | 3d31538 | 2014-07-09 23:44:44 +0300 | [diff] [blame] | 120 | clrbits_le32(KS2_MISC_CTRL, KS2_ARM_PLL_EN); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 121 | /* 2 In PLLCTL1, write PLLRST = 1 (PLL is reset) */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 122 | setbits_le32(keystone_pll_regs[data->pll].reg1 , |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 123 | 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, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 135 | __raw_writel(tmp, keystone_pll_regs[data->pll].reg0); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 136 | |
| 137 | /* Set BWADJ[11:8] bits */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 138 | tmp = __raw_readl(keystone_pll_regs[data->pll].reg1); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 139 | tmp &= ~(PLL_BWADJ_HI_MASK); |
| 140 | tmp |= ((bwadj>>8) & PLL_BWADJ_HI_MASK); |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 141 | __raw_writel(tmp, keystone_pll_regs[data->pll].reg1); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 142 | /* |
| 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, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 149 | clrbits_le32(keystone_pll_regs[data->pll].reg1, PLL_PLLRST); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 150 | /* |
| 151 | * 7 Wait for at least 500 * REFCLK cycles * (PLLD + 1) |
| 152 | * (PLL lock time) |
| 153 | */ |
| 154 | sdelay(105000); |
| 155 | /* 8 disable bypass */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 156 | clrbits_le32(keystone_pll_regs[data->pll].reg0, PLLCTL_BYPASS); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 157 | /* |
| 158 | * 9 Set CHIPMISCCTL1[13] = 1 (disable glitchfree bypass) |
| 159 | * only applicable for Kepler |
| 160 | */ |
Khoronzhuk, Ivan | 3d31538 | 2014-07-09 23:44:44 +0300 | [diff] [blame] | 161 | setbits_le32(KS2_MISC_CTRL, KS2_ARM_PLL_EN); |
Hao Zhang | 4dca7f0 | 2014-07-16 00:59:23 +0300 | [diff] [blame] | 162 | #endif |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 163 | } else { |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 164 | setbits_le32(keystone_pll_regs[data->pll].reg1, PLLCTL_ENSAT); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 165 | /* |
| 166 | * process keeps state of Bypass bit while programming |
| 167 | * all other DDR PLL settings |
| 168 | */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 169 | tmp = __raw_readl(keystone_pll_regs[data->pll].reg0); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 170 | 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, Ivan | 1c4044a | 2014-08-11 11:59:42 +0300 | [diff] [blame] | 177 | tmp |= ((bwadj & PLL_BWADJ_LO_MASK) << PLL_BWADJ_LO_SHIFT) | |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 178 | (pllm << PLL_MULT_SHIFT) | |
| 179 | (plld & PLL_DIV_MASK) | |
| 180 | (pllod << PLL_CLKOD_SHIFT); |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 181 | __raw_writel(tmp, keystone_pll_regs[data->pll].reg0); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 182 | |
| 183 | /* Set BWADJ[11:8] bits */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 184 | tmp = __raw_readl(keystone_pll_regs[data->pll].reg1); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 185 | tmp &= ~(PLL_BWADJ_HI_MASK); |
| 186 | tmp |= ((bwadj >> 8) & PLL_BWADJ_HI_MASK); |
| 187 | |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 188 | __raw_writel(tmp, keystone_pll_regs[data->pll].reg1); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 189 | |
| 190 | /* Reset bit: bit 14 for both DDR3 & PASS PLL */ |
| 191 | tmp = PLL_PLLRST; |
| 192 | /* Set RESET bit = 1 */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 193 | setbits_le32(keystone_pll_regs[data->pll].reg1, tmp); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 194 | /* Wait for a minimum of 7 us*/ |
| 195 | sdelay(21000); |
| 196 | /* Clear RESET bit */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 197 | clrbits_le32(keystone_pll_regs[data->pll].reg1, tmp); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 198 | sdelay(105000); |
| 199 | |
| 200 | /* clear BYPASS (Enable PLL Mode) */ |
Khoronzhuk, Ivan | 8dfc15f | 2014-07-09 23:44:47 +0300 | [diff] [blame] | 201 | clrbits_le32(keystone_pll_regs[data->pll].reg0, PLLCTL_BYPASS); |
Vitaly Andrianov | ef509b9 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 202 | 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 | |
| 212 | void 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 Andrianov | 61f66fd | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 219 | |
| 220 | static 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 |
| 237 | static 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 |
| 243 | static inline u32 read_efuse_bootrom(void) |
| 244 | { |
| 245 | return __raw_readl(KS2_EFUSE_BOOTROM); |
| 246 | } |
| 247 | #endif |
| 248 | |
Vitaly Andrianov | 61f66fd | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 249 | #ifndef CONFIG_SOC_K2E |
| 250 | inline int get_max_arm_speed(void) |
| 251 | { |
Vitaly Andrianov | 437a729 | 2015-06-15 08:54:15 -0400 | [diff] [blame] | 252 | return get_max_speed(read_efuse_bootrom() & 0xffff, arm_speeds); |
Vitaly Andrianov | 61f66fd | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 253 | } |
| 254 | #endif |
Khoronzhuk, Ivan | 69a3b81 | 2014-10-17 21:01:16 +0300 | [diff] [blame] | 255 | |
Vitaly Andrianov | 437a729 | 2015-06-15 08:54:15 -0400 | [diff] [blame] | 256 | inline int get_max_dev_speed(void) |
| 257 | { |
| 258 | return get_max_speed((read_efuse_bootrom() >> 16) & 0xffff, dev_speeds); |
| 259 | } |
| 260 | |
Khoronzhuk, Ivan | 69a3b81 | 2014-10-17 21:01:16 +0300 | [diff] [blame] | 261 | void 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 | } |