Vitaly Andrianov | 7bcf4d6 | 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 | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 11 | #include <asm/arch/clock.h> |
| 12 | #include <asm/arch/clock_defs.h> |
| 13 | |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 14 | /* DEV and ARM speed definitions as specified in DEVSPEED register */ |
| 15 | int __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 Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 27 | |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 28 | static 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 Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 33 | if (!(pllctl_reg_read(data->pll, stat) & PLLSTAT_GOSTAT_MASK)) |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 34 | break; |
| 35 | } |
| 36 | } |
| 37 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 38 | static inline void bypass_main_pll(const struct pll_init_data *data) |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 39 | { |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 40 | 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 | |
| 47 | static void configure_mult_div(const struct pll_init_data *data) |
| 48 | { |
| 49 | u32 pllm, plld, bwadj; |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 50 | |
| 51 | pllm = data->pll_m - 1; |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 52 | plld = (data->pll_d - 1) & CFG_PLLCTL0_PLLD_MASK; |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 53 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 54 | /* Program Multiplier */ |
| 55 | if (data->pll == MAIN_PLL) |
| 56 | pllctl_reg_write(data->pll, mult, pllm & PLLM_MULT_LO_MASK); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 57 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 58 | clrsetbits_le32(keystone_pll_regs[data->pll].reg0, |
| 59 | CFG_PLLCTL0_PLLM_MASK, |
| 60 | pllm << CFG_PLLCTL0_PLLM_SHIFT); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 61 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 62 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 71 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 72 | /* Program Divider */ |
| 73 | clrsetbits_le32(keystone_pll_regs[data->pll].reg0, |
| 74 | CFG_PLLCTL0_PLLD_MASK, plld); |
| 75 | } |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 76 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 77 | void configure_main_pll(const struct pll_init_data *data) |
| 78 | { |
| 79 | u32 tmp, pllod, i, alnctl_val = 0; |
| 80 | u32 *offset; |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 81 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 82 | pllod = data->pll_od - 1; |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 83 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 84 | /* 100 micro sec for stabilization */ |
| 85 | sdelay(210000); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 86 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 87 | tmp = pllctl_reg_read(data->pll, secctl); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 88 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 89 | /* Check for Bypass */ |
| 90 | if (tmp & SECCTL_BYPASS_MASK) { |
| 91 | setbits_le32(keystone_pll_regs[data->pll].reg1, |
| 92 | CFG_PLLCTL1_ENSAT_MASK); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 93 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 94 | bypass_main_pll(data); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 95 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 96 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 106 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 107 | configure_mult_div(data); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 108 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 109 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 112 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 113 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 129 | /* |
| 130 | * Set GOSET bit in PLLCMD to initiate the GO operation |
| 131 | * to change the divide |
| 132 | */ |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 133 | pllctl_reg_setbits(data->pll, cmd, PLLSTAT_GOSTAT_MASK); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 134 | wait_for_completion(data); |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 135 | } |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 136 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 137 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 142 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 143 | /* Enable PLL */ |
| 144 | pllctl_reg_clrbits(data->pll, secctl, SECCTL_BYPASS_MASK); |
| 145 | pllctl_reg_setbits(data->pll, ctl, PLLCTL_PLLEN_MASK); |
| 146 | } |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 147 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 148 | void configure_secondary_pll(const struct pll_init_data *data) |
| 149 | { |
| 150 | int pllod = data->pll_od - 1; |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 151 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 152 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 156 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 157 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 160 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 161 | configure_mult_div(data); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 162 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 163 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 168 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 169 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 173 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 174 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 178 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 179 | /* 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 182 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 183 | clrbits_le32(keystone_pll_regs[data->pll].reg1, CFG_PLLCTL1_RST_MASK); |
| 184 | /* Wait for 500 * REFCLK cucles * (PLLD + 1) */ |
| 185 | sdelay(105000); |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 186 | |
Lokesh Vutla | 70438fc | 2015-07-28 14:16:43 +0530 | [diff] [blame] | 187 | /* Switch to PLL mode */ |
| 188 | clrbits_le32(keystone_pll_regs[data->pll].reg0, |
| 189 | CFG_PLLCTL0_BYPASS_MASK); |
| 190 | } |
| 191 | |
| 192 | void 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 Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * This is required to provide a delay between multiple |
| 201 | * consequent PPL configurations |
| 202 | */ |
| 203 | sdelay(210000); |
| 204 | } |
| 205 | |
| 206 | void 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 Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 213 | |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 214 | static int get_max_speed(u32 val, u32 speed_supported) |
Vitaly Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 215 | { |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 216 | int speed; |
Vitaly Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 217 | |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 218 | /* 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 Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 222 | } |
| 223 | |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 224 | /* If no bit is set, use SPD800 */ |
Vitaly Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 225 | return SPD800; |
| 226 | } |
| 227 | |
Vitaly Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 228 | static inline u32 read_efuse_bootrom(void) |
| 229 | { |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 230 | if (cpu_is_k2hk() && (cpu_revision() <= 1)) |
| 231 | return __raw_readl(KS2_REV1_DEVSPEED); |
| 232 | else |
| 233 | return __raw_readl(KS2_EFUSE_BOOTROM); |
Vitaly Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 234 | } |
Vitaly Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 235 | |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 236 | int get_max_arm_speed(void) |
Vitaly Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 237 | { |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 238 | 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 Andrianov | 047e780 | 2014-07-25 22:23:19 +0300 | [diff] [blame] | 244 | } |
Khoronzhuk, Ivan | 9f95c1b | 2014-10-17 21:01:16 +0300 | [diff] [blame] | 245 | |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 246 | int get_max_dev_speed(void) |
Vitaly Andrianov | 6eb54a4 | 2015-06-15 08:54:15 -0400 | [diff] [blame] | 247 | { |
Lokesh Vutla | 9da9afa | 2015-07-28 14:16:44 +0530 | [diff] [blame^] | 248 | 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 Andrianov | 6eb54a4 | 2015-06-15 08:54:15 -0400 | [diff] [blame] | 254 | } |