huang lin | c14b54d | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2016 Rockchip Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
huang lin | c14b54d | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 14 | */ |
| 15 | |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 16 | #include <assert.h> |
| 17 | #include <console/console.h> |
| 18 | #include <delay.h> |
| 19 | #include <soc/addressmap.h> |
huang lin | c14b54d | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 20 | #include <soc/clock.h> |
Lin Huang | f5702e7 | 2016-03-19 22:45:19 +0800 | [diff] [blame] | 21 | #include <soc/grf.h> |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 22 | #include <soc/i2c.h> |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 23 | #include <soc/soc.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | struct pll_div { |
| 29 | u32 refdiv; |
| 30 | u32 fbdiv; |
| 31 | u32 postdiv1; |
| 32 | u32 postdiv2; |
| 33 | u32 frac; |
| 34 | }; |
| 35 | |
| 36 | #define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\ |
| 37 | .refdiv = _refdiv,\ |
| 38 | .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\ |
| 39 | .postdiv1 = _postdiv1, .postdiv2 = _postdiv2};\ |
| 40 | _Static_assert(((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ) *\ |
| 41 | OSC_HZ / (_refdiv * _postdiv1 * _postdiv2) == hz,\ |
| 42 | #hz "Hz cannot be hit with PLL "\ |
| 43 | "divisors on line " STRINGIFY(__LINE__)) |
| 44 | |
| 45 | static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2, 1); |
| 46 | static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2, 2); |
| 47 | static const struct pll_div ppll_init_cfg = PLL_DIVISORS(PPLL_HZ, 2, 2, 1); |
| 48 | |
| 49 | static const struct pll_div apll_l_1600_cfg = PLL_DIVISORS(1600*MHz, 3, 1, 1); |
| 50 | static const struct pll_div apll_l_600_cfg = PLL_DIVISORS(600*MHz, 1, 2, 1); |
| 51 | |
| 52 | static const struct pll_div *apll_l_cfgs[] = { |
| 53 | [APLL_L_1600_MHZ] = &apll_l_1600_cfg, |
| 54 | [APLL_L_600_MHZ] = &apll_l_600_cfg, |
| 55 | }; |
| 56 | |
| 57 | enum { |
| 58 | /* PLL_CON0 */ |
| 59 | PLL_FBDIV_MASK = 0xfff, |
| 60 | PLL_FBDIV_SHIFT = 0, |
| 61 | |
| 62 | /* PLL_CON1 */ |
| 63 | PLL_POSTDIV2_MASK = 0x7, |
| 64 | PLL_POSTDIV2_SHIFT = 12, |
| 65 | PLL_POSTDIV1_MASK = 0x7, |
| 66 | PLL_POSTDIV1_SHIFT = 8, |
| 67 | PLL_REFDIV_MASK = 0x3f, |
| 68 | PLL_REFDIV_SHIFT = 0, |
| 69 | |
| 70 | /* PLL_CON2 */ |
| 71 | PLL_LOCK_STATUS_MASK = 1, |
| 72 | PLL_LOCK_STATUS_SHIFT = 31, |
| 73 | PLL_FRACDIV_MASK = 0xffffff, |
| 74 | PLL_FRACDIV_SHIFT = 0, |
| 75 | |
| 76 | /* PLL_CON3 */ |
| 77 | PLL_MODE_MASK = 3, |
| 78 | PLL_MODE_SHIFT = 8, |
| 79 | PLL_MODE_SLOW = 0, |
| 80 | PLL_MODE_NORM, |
| 81 | PLL_MODE_DEEP, |
| 82 | PLL_DSMPD_MASK = 1, |
| 83 | PLL_DSMPD_SHIFT = 3, |
| 84 | PLL_INTEGER_MODE = 1, |
| 85 | |
| 86 | /* PMUCRU_CLKSEL_CON0 */ |
| 87 | PMU_PCLK_DIV_CON_MASK = 0x1f, |
| 88 | PMU_PCLK_DIV_CON_SHIFT = 0, |
| 89 | |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 90 | /* PMUCRU_CLKSEL_CON1 */ |
| 91 | SPI3_PLL_SEL_MASK = 1, |
| 92 | SPI3_PLL_SEL_SHIFT = 7, |
| 93 | SPI3_PLL_SEL_24M = 0, |
| 94 | SPI3_PLL_SEL_PPLL = 1, |
| 95 | SPI3_DIV_CON_MASK = 0x7f, |
| 96 | SPI3_DIV_CON_SHIFT = 0x0, |
| 97 | |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 98 | /* PMUCRU_CLKSEL_CON2 */ |
| 99 | I2C_DIV_CON_MASK = 0x7f, |
| 100 | I2C8_DIV_CON_SHIFT = 8, |
| 101 | I2C0_DIV_CON_SHIFT = 0, |
| 102 | |
| 103 | /* PMUCRU_CLKSEL_CON3 */ |
| 104 | I2C4_DIV_CON_SHIFT = 0, |
| 105 | |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 106 | /* CLKSEL_CON0 */ |
| 107 | ACLKM_CORE_L_DIV_CON_MASK = 0x1f, |
| 108 | ACLKM_CORE_L_DIV_CON_SHIFT = 8, |
| 109 | CLK_CORE_L_PLL_SEL_MASK = 3, |
| 110 | CLK_CORE_L_PLL_SEL_SHIFT = 6, |
| 111 | CLK_CORE_L_PLL_SEL_ALPLL = 0x0, |
| 112 | CLK_CORE_L_PLL_SEL_ABPLL = 0x1, |
| 113 | CLK_CORE_L_PLL_SEL_DPLL = 0x10, |
| 114 | CLK_CORE_L_PLL_SEL_GPLL = 0x11, |
| 115 | CLK_CORE_L_DIV_MASK = 0x1f, |
| 116 | CLK_CORE_L_DIV_SHIFT = 0, |
| 117 | |
| 118 | /* CLKSEL_CON1 */ |
| 119 | PCLK_DBG_L_DIV_MASK = 0x1f, |
| 120 | PCLK_DBG_L_DIV_SHIFT = 0x8, |
| 121 | ATCLK_CORE_L_DIV_MASK = 0x1f, |
| 122 | ATCLK_CORE_L_DIV_SHIFT = 0, |
| 123 | |
| 124 | /* CLKSEL_CON14 */ |
| 125 | PCLK_PERIHP_DIV_CON_MASK = 0x7, |
| 126 | PCLK_PERIHP_DIV_CON_SHIFT = 12, |
| 127 | HCLK_PERIHP_DIV_CON_MASK = 3, |
| 128 | HCLK_PERIHP_DIV_CON_SHIFT = 8, |
| 129 | ACLK_PERIHP_PLL_SEL_MASK = 1, |
| 130 | ACLK_PERIHP_PLL_SEL_SHIFT = 7, |
| 131 | ACLK_PERIHP_PLL_SEL_CPLL = 0, |
| 132 | ACLK_PERIHP_PLL_SEL_GPLL = 1, |
| 133 | ACLK_PERIHP_DIV_CON_MASK = 0x1f, |
| 134 | ACLK_PERIHP_DIV_CON_SHIFT = 0, |
| 135 | |
| 136 | /* CLKSEL_CON23 */ |
| 137 | PCLK_PERILP0_DIV_CON_MASK = 0x7, |
| 138 | PCLK_PERILP0_DIV_CON_SHIFT = 12, |
| 139 | HCLK_PERILP0_DIV_CON_MASK = 3, |
| 140 | HCLK_PERILP0_DIV_CON_SHIFT = 8, |
| 141 | ACLK_PERILP0_PLL_SEL_MASK = 1, |
| 142 | ACLK_PERILP0_PLL_SEL_SHIFT = 7, |
| 143 | ACLK_PERILP0_PLL_SEL_CPLL = 0, |
| 144 | ACLK_PERILP0_PLL_SEL_GPLL = 1, |
| 145 | ACLK_PERILP0_DIV_CON_MASK = 0x1f, |
| 146 | ACLK_PERILP0_DIV_CON_SHIFT = 0, |
| 147 | |
| 148 | /* CLKSEL_CON25 */ |
| 149 | PCLK_PERILP1_DIV_CON_MASK = 0x7, |
| 150 | PCLK_PERILP1_DIV_CON_SHIFT = 8, |
| 151 | HCLK_PERILP1_PLL_SEL_MASK = 1, |
| 152 | HCLK_PERILP1_PLL_SEL_SHIFT = 7, |
| 153 | HCLK_PERILP1_PLL_SEL_CPLL = 0, |
| 154 | HCLK_PERILP1_PLL_SEL_GPLL = 1, |
| 155 | HCLK_PERILP1_DIV_CON_MASK = 0x1f, |
| 156 | HCLK_PERILP1_DIV_CON_SHIFT = 0, |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 157 | |
Lin Huang | bf48fbb | 2016-03-23 19:24:53 +0800 | [diff] [blame] | 158 | /* CLKSEL_CON26 */ |
| 159 | CLK_SARADC_DIV_CON_MASK = 0xff, |
| 160 | CLK_SARADC_DIV_CON_SHIFT = 8, |
| 161 | |
Shunqian Zheng | f4181ce | 2016-05-06 16:50:48 +0800 | [diff] [blame] | 162 | /* CLKSEL_CON27 */ |
| 163 | CLK_TSADC_SEL_X24M = 0x0, |
| 164 | CLK_TSADC_SEL_MASK = 1, |
| 165 | CLK_TSADC_SEL_SHIFT = 15, |
| 166 | CLK_TSADC_DIV_CON_MASK = 0x3ff, |
| 167 | CLK_TSADC_DIV_CON_SHIFT = 0, |
| 168 | |
Shunqian Zheng | c7f32a5 | 2016-05-04 15:54:37 +0800 | [diff] [blame] | 169 | /* CLKSEL_CON47 & CLKSEL_CON48 */ |
| 170 | ACLK_VOP_PLL_SEL_MASK = 0x3, |
| 171 | ACLK_VOP_PLL_SEL_SHIFT = 6, |
| 172 | ACLK_VOP_PLL_SEL_CPLL = 0x1, |
| 173 | ACLK_VOP_DIV_CON_MASK = 0x1f, |
| 174 | ACLK_VOP_DIV_CON_SHIFT = 0, |
| 175 | |
| 176 | /* CLKSEL_CON49 & CLKSEL_CON50 */ |
| 177 | DCLK_VOP_DCLK_SEL_MASK = 1, |
| 178 | DCLK_VOP_DCLK_SEL_SHIFT = 11, |
| 179 | DCLK_VOP_DCLK_SEL_DIVOUT = 0, |
| 180 | DCLK_VOP_PLL_SEL_MASK = 3, |
| 181 | DCLK_VOP_PLL_SEL_SHIFT = 8, |
| 182 | DCLK_VOP_PLL_SEL_VPLL = 0, |
| 183 | DCLK_VOP_DIV_CON_MASK = 0xff, |
| 184 | DCLK_VOP_DIV_CON_SHIFT = 0, |
| 185 | |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 186 | /* CLKSEL_CON58 */ |
| 187 | CLK_SPI_PLL_SEL_MASK = 1, |
| 188 | CLK_SPI_PLL_SEL_CPLL = 0, |
| 189 | CLK_SPI_PLL_SEL_GPLL = 1, |
| 190 | CLK_SPI_PLL_DIV_CON_MASK = 0x7f, |
| 191 | CLK_SPI5_PLL_DIV_CON_SHIFT = 8, |
| 192 | CLK_SPI5_PLL_SEL_SHIFT = 15, |
| 193 | |
| 194 | /* CLKSEL_CON59 */ |
| 195 | CLK_SPI1_PLL_SEL_SHIFT = 15, |
| 196 | CLK_SPI1_PLL_DIV_CON_SHIFT = 8, |
| 197 | CLK_SPI0_PLL_SEL_SHIFT = 7, |
| 198 | CLK_SPI0_PLL_DIV_CON_SHIFT = 0, |
| 199 | |
| 200 | /* CLKSEL_CON60 */ |
| 201 | CLK_SPI4_PLL_SEL_SHIFT = 15, |
| 202 | CLK_SPI4_PLL_DIV_CON_SHIFT = 8, |
| 203 | CLK_SPI2_PLL_SEL_SHIFT = 7, |
| 204 | CLK_SPI2_PLL_DIV_CON_SHIFT = 0, |
| 205 | |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 206 | /* CLKSEL_CON61 */ |
| 207 | CLK_I2C_PLL_SEL_MASK = 1, |
| 208 | CLK_I2C_PLL_SEL_CPLL = 0, |
| 209 | CLK_I2C_PLL_SEL_GPLL = 1, |
| 210 | CLK_I2C5_PLL_SEL_SHIFT = 15, |
| 211 | CLK_I2C5_DIV_CON_SHIFT = 8, |
| 212 | CLK_I2C1_PLL_SEL_SHIFT = 7, |
| 213 | CLK_I2C1_DIV_CON_SHIFT = 0, |
| 214 | |
| 215 | /* CLKSEL_CON62 */ |
| 216 | CLK_I2C6_PLL_SEL_SHIFT = 15, |
| 217 | CLK_I2C6_DIV_CON_SHIFT = 8, |
| 218 | CLK_I2C2_PLL_SEL_SHIFT = 7, |
| 219 | CLK_I2C2_DIV_CON_SHIFT = 0, |
| 220 | |
| 221 | /* CLKSEL_CON63 */ |
| 222 | CLK_I2C7_PLL_SEL_SHIFT = 15, |
| 223 | CLK_I2C7_DIV_CON_SHIFT = 8, |
| 224 | CLK_I2C3_PLL_SEL_SHIFT = 7, |
| 225 | CLK_I2C3_DIV_CON_SHIFT = 0, |
| 226 | |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 227 | /* CRU_SOFTRST_CON4 */ |
| 228 | RESETN_DDR0_REQ_MASK = 1, |
| 229 | RESETN_DDR0_REQ_SHIFT = 8, |
| 230 | RESETN_DDRPHY0_REQ_MASK = 1, |
| 231 | RESETN_DDRPHY0_REQ_SHIFT = 9, |
| 232 | RESETN_DDR1_REQ_MASK = 1, |
| 233 | RESETN_DDR1_REQ_SHIFT = 12, |
| 234 | RESETN_DDRPHY1_REQ_MASK = 1, |
| 235 | RESETN_DDRPHY1_REQ_SHIFT = 13, |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | #define VCO_MAX_KHZ (3200 * (MHz / KHz)) |
| 239 | #define VCO_MIN_KHZ (800 * (MHz / KHz)) |
| 240 | #define OUTPUT_MAX_KHZ (3200 * (MHz / KHz)) |
| 241 | #define OUTPUT_MIN_KHZ (16 * (MHz / KHz)) |
| 242 | |
| 243 | /* the div restrictions of pll in integer mode, |
| 244 | * these are defined in * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0 |
| 245 | */ |
| 246 | #define PLL_DIV_MIN 16 |
| 247 | #define PLL_DIV_MAX 3200 |
| 248 | |
| 249 | /* How to calculate the PLL(from TRM V0.3 Part 1 Page 63): |
| 250 | * Formulas also embedded within the Fractional PLL Verilog model: |
| 251 | * If DSMPD = 1 (DSM is disabled, "integer mode") |
| 252 | * FOUTVCO = FREF / REFDIV * FBDIV |
| 253 | * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2 |
| 254 | * Where: |
| 255 | * FOUTVCO = Fractional PLL non-divided output frequency |
| 256 | * FOUTPOSTDIV = Fractional PLL divided output frequency |
| 257 | * (output of second post divider) |
| 258 | * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input) |
| 259 | * REFDIV = Fractional PLL input reference clock divider |
| 260 | * FBDIV = Integer value programmed into feedback divide |
| 261 | * |
| 262 | */ |
| 263 | static void rkclk_set_pll(u32 *pll_con, const struct pll_div *div) |
| 264 | { |
| 265 | /* All 8 PLLs have same VCO and output frequency range restrictions. */ |
| 266 | u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv; |
| 267 | u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2; |
| 268 | |
| 269 | printk(BIOS_DEBUG, "PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, " |
| 270 | "postdiv2=%d, vco=%u khz, output=%u khz\n", |
| 271 | pll_con, div->fbdiv, div->refdiv, div->postdiv1, |
| 272 | div->postdiv2, vco_khz, output_khz); |
| 273 | assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ && |
| 274 | output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ && |
| 275 | div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX); |
| 276 | |
| 277 | /* When power on or changing PLL setting, |
| 278 | * we must force PLL into slow mode to ensure output stable clock. |
| 279 | */ |
| 280 | write32(&pll_con[3], RK_CLRSETBITS(PLL_MODE_MASK << PLL_MODE_SHIFT, |
| 281 | PLL_MODE_SLOW << PLL_MODE_SHIFT)); |
| 282 | |
| 283 | /* use integer mode */ |
| 284 | write32(&pll_con[3], |
| 285 | RK_CLRSETBITS(PLL_DSMPD_MASK << PLL_DSMPD_SHIFT, |
| 286 | PLL_INTEGER_MODE << PLL_DSMPD_SHIFT)); |
| 287 | |
| 288 | write32(&pll_con[0], RK_CLRSETBITS(PLL_FBDIV_MASK << PLL_FBDIV_SHIFT, |
| 289 | div->fbdiv << PLL_FBDIV_SHIFT)); |
| 290 | write32(&pll_con[1], |
| 291 | RK_CLRSETBITS(PLL_POSTDIV2_MASK << PLL_POSTDIV2_SHIFT | |
| 292 | PLL_POSTDIV1_MASK << PLL_POSTDIV1_SHIFT | |
| 293 | PLL_REFDIV_MASK | PLL_REFDIV_SHIFT, |
| 294 | (div->postdiv2 << PLL_POSTDIV2_SHIFT) | |
| 295 | (div->postdiv1 << PLL_POSTDIV1_SHIFT) | |
| 296 | (div->refdiv << PLL_REFDIV_SHIFT))); |
| 297 | |
| 298 | /* waiting for pll lock */ |
| 299 | while (!(read32(&pll_con[2]) & (1 << PLL_LOCK_STATUS_SHIFT))) |
| 300 | udelay(1); |
| 301 | |
| 302 | /* pll enter normal mode */ |
| 303 | write32(&pll_con[3], RK_CLRSETBITS(PLL_MODE_MASK << PLL_MODE_SHIFT, |
| 304 | PLL_MODE_NORM << PLL_MODE_SHIFT)); |
| 305 | } |
| 306 | |
Shunqian Zheng | c7f32a5 | 2016-05-04 15:54:37 +0800 | [diff] [blame] | 307 | static int pll_para_config(u32 freq_hz, struct pll_div *div) |
| 308 | { |
| 309 | u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0; |
| 310 | u32 postdiv1, postdiv2 = 1; |
| 311 | u32 fref_khz; |
| 312 | u32 diff_khz, best_diff_khz; |
| 313 | const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16; |
| 314 | const u32 max_postdiv1 = 7, max_postdiv2 = 7; |
| 315 | u32 vco_khz; |
| 316 | u32 freq_khz = freq_hz / KHz; |
| 317 | |
| 318 | if (!freq_hz) { |
| 319 | printk(BIOS_ERR, "%s: the frequency can't be 0 Hz\n", __func__); |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | postdiv1 = div_round_up(VCO_MIN_KHZ, freq_khz); |
| 324 | if (postdiv1 > max_postdiv1) { |
| 325 | postdiv2 = div_round_up(postdiv1, max_postdiv1); |
| 326 | postdiv1 = div_round_up(postdiv1, postdiv2); |
| 327 | } |
| 328 | |
| 329 | vco_khz = freq_khz * postdiv1 * postdiv2; |
| 330 | |
| 331 | if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || |
| 332 | postdiv2 > max_postdiv2) { |
| 333 | printk(BIOS_ERR, "%s: Cannot find out a supported VCO" |
| 334 | " for Frequency (%uHz).\n", __func__, freq_hz); |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | div->postdiv1 = postdiv1; |
| 339 | div->postdiv2 = postdiv2; |
| 340 | |
| 341 | best_diff_khz = vco_khz; |
| 342 | for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) { |
| 343 | fref_khz = ref_khz / refdiv; |
| 344 | |
| 345 | fbdiv = vco_khz / fref_khz; |
| 346 | if ((fbdiv >= max_fbdiv) || (fbdiv <= min_fbdiv)) |
| 347 | continue; |
| 348 | diff_khz = vco_khz - fbdiv * fref_khz; |
| 349 | if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) { |
| 350 | fbdiv++; |
| 351 | diff_khz = fref_khz - diff_khz; |
| 352 | } |
| 353 | |
| 354 | if (diff_khz >= best_diff_khz) |
| 355 | continue; |
| 356 | |
| 357 | best_diff_khz = diff_khz; |
| 358 | div->refdiv = refdiv; |
| 359 | div->fbdiv = fbdiv; |
| 360 | } |
| 361 | |
| 362 | if (best_diff_khz > 4 * (MHz/KHz)) { |
| 363 | printk(BIOS_ERR, "%s: Failed to match output frequency %u, " |
| 364 | "difference is %u Hz,exceed 4MHZ\n", __func__, freq_hz, |
| 365 | best_diff_khz * KHz); |
| 366 | return -1; |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 371 | void rkclk_init(void) |
| 372 | { |
| 373 | u32 aclk_div; |
| 374 | u32 hclk_div; |
| 375 | u32 pclk_div; |
| 376 | |
| 377 | /* some cru registers changed by bootrom, we'd better reset them to |
| 378 | * reset/default values described in TRM to avoid confusion in kernel. |
| 379 | * Please consider these threee lines as a fix of bootrom bug. |
| 380 | */ |
| 381 | write32(&cru_ptr->clksel_con[12], 0xffff4101); |
| 382 | write32(&cru_ptr->clksel_con[19], 0xffff033f); |
| 383 | write32(&cru_ptr->clksel_con[56], 0x00030003); |
| 384 | |
| 385 | /* configure pmu pll(ppll) */ |
| 386 | rkclk_set_pll(&pmucru_ptr->ppll_con[0], &ppll_init_cfg); |
| 387 | |
| 388 | /* configure pmu pclk */ |
| 389 | pclk_div = PPLL_HZ / PMU_PCLK_HZ - 1; |
| 390 | assert((pclk_div + 1) * PMU_PCLK_HZ == PPLL_HZ && pclk_div < 0x1f); |
| 391 | write32(&pmucru_ptr->pmucru_clksel[0], |
| 392 | RK_CLRSETBITS(PMU_PCLK_DIV_CON_MASK << PMU_PCLK_DIV_CON_SHIFT, |
| 393 | pclk_div << PMU_PCLK_DIV_CON_SHIFT)); |
| 394 | |
| 395 | /* configure gpll cpll */ |
| 396 | rkclk_set_pll(&cru_ptr->gpll_con[0], &gpll_init_cfg); |
| 397 | rkclk_set_pll(&cru_ptr->cpll_con[0], &cpll_init_cfg); |
| 398 | |
| 399 | /* configure perihp aclk, hclk, pclk */ |
| 400 | aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1; |
| 401 | assert((aclk_div + 1) * PERIHP_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 402 | |
| 403 | hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1; |
| 404 | assert((hclk_div + 1) * PERIHP_HCLK_HZ == |
| 405 | PERIHP_ACLK_HZ && (hclk_div < 0x4)); |
| 406 | |
| 407 | pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1; |
| 408 | assert((pclk_div + 1) * PERIHP_PCLK_HZ == |
| 409 | PERIHP_ACLK_HZ && (pclk_div < 0x7)); |
| 410 | |
| 411 | write32(&cru_ptr->clksel_con[14], |
| 412 | RK_CLRSETBITS(PCLK_PERIHP_DIV_CON_MASK << |
| 413 | PCLK_PERIHP_DIV_CON_SHIFT | |
| 414 | HCLK_PERIHP_DIV_CON_MASK << |
| 415 | HCLK_PERIHP_DIV_CON_SHIFT | |
| 416 | ACLK_PERIHP_PLL_SEL_MASK << |
| 417 | ACLK_PERIHP_PLL_SEL_SHIFT | |
| 418 | ACLK_PERIHP_DIV_CON_MASK << |
| 419 | ACLK_PERIHP_DIV_CON_SHIFT, |
| 420 | pclk_div << PCLK_PERIHP_DIV_CON_SHIFT | |
| 421 | hclk_div << HCLK_PERIHP_DIV_CON_SHIFT | |
| 422 | ACLK_PERIHP_PLL_SEL_GPLL << |
| 423 | ACLK_PERIHP_PLL_SEL_SHIFT | |
| 424 | aclk_div << ACLK_PERIHP_DIV_CON_SHIFT)); |
| 425 | |
| 426 | /* configure perilp0 aclk, hclk, pclk */ |
| 427 | aclk_div = GPLL_HZ / PERILP0_ACLK_HZ - 1; |
| 428 | assert((aclk_div + 1) * PERILP0_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 429 | |
| 430 | hclk_div = PERILP0_ACLK_HZ / PERILP0_HCLK_HZ - 1; |
| 431 | assert((hclk_div + 1) * PERILP0_HCLK_HZ == |
| 432 | PERILP0_ACLK_HZ && (hclk_div < 0x4)); |
| 433 | |
| 434 | pclk_div = PERILP0_ACLK_HZ / PERILP0_PCLK_HZ - 1; |
| 435 | assert((pclk_div + 1) * PERILP0_PCLK_HZ == |
| 436 | PERILP0_ACLK_HZ && (pclk_div < 0x7)); |
| 437 | |
| 438 | write32(&cru_ptr->clksel_con[23], |
| 439 | RK_CLRSETBITS(PCLK_PERILP0_DIV_CON_MASK << |
| 440 | PCLK_PERILP0_DIV_CON_SHIFT | |
| 441 | HCLK_PERILP0_DIV_CON_MASK << |
| 442 | HCLK_PERILP0_DIV_CON_SHIFT | |
| 443 | ACLK_PERILP0_PLL_SEL_MASK << |
| 444 | ACLK_PERILP0_PLL_SEL_SHIFT | |
| 445 | ACLK_PERILP0_DIV_CON_MASK << |
| 446 | ACLK_PERILP0_DIV_CON_SHIFT, |
| 447 | pclk_div << PCLK_PERILP0_DIV_CON_SHIFT | |
| 448 | hclk_div << HCLK_PERILP0_DIV_CON_SHIFT | |
| 449 | ACLK_PERILP0_PLL_SEL_GPLL << |
| 450 | ACLK_PERILP0_PLL_SEL_SHIFT | |
| 451 | aclk_div << ACLK_PERILP0_DIV_CON_SHIFT)); |
| 452 | |
| 453 | /* perilp1 hclk select gpll as source */ |
| 454 | hclk_div = GPLL_HZ / PERILP1_HCLK_HZ - 1; |
| 455 | assert((hclk_div + 1) * PERILP1_HCLK_HZ == |
| 456 | GPLL_HZ && (hclk_div < 0x1f)); |
| 457 | |
| 458 | pclk_div = PERILP1_HCLK_HZ / PERILP1_HCLK_HZ - 1; |
| 459 | assert((pclk_div + 1) * PERILP1_HCLK_HZ == |
| 460 | PERILP1_HCLK_HZ && (hclk_div < 0x7)); |
| 461 | |
| 462 | write32(&cru_ptr->clksel_con[25], |
| 463 | RK_CLRSETBITS(PCLK_PERILP1_DIV_CON_MASK << |
| 464 | PCLK_PERILP1_DIV_CON_SHIFT | |
| 465 | HCLK_PERILP1_DIV_CON_MASK << |
| 466 | HCLK_PERILP1_DIV_CON_SHIFT | |
| 467 | HCLK_PERILP1_PLL_SEL_MASK << |
| 468 | HCLK_PERILP1_PLL_SEL_SHIFT, |
| 469 | pclk_div << PCLK_PERILP1_DIV_CON_SHIFT | |
| 470 | hclk_div << HCLK_PERILP1_DIV_CON_SHIFT | |
| 471 | HCLK_PERILP1_PLL_SEL_GPLL << |
| 472 | HCLK_PERILP1_PLL_SEL_SHIFT)); |
| 473 | } |
| 474 | |
| 475 | void rkclk_configure_cpu(enum apll_l_frequencies apll_l_freq) |
| 476 | { |
| 477 | u32 aclkm_div; |
| 478 | u32 pclk_dbg_div; |
| 479 | u32 atclk_div; |
| 480 | |
| 481 | rkclk_set_pll(&cru_ptr->apll_l_con[0], apll_l_cfgs[apll_l_freq]); |
| 482 | |
| 483 | aclkm_div = APLL_HZ / ACLKM_CORE_HZ - 1; |
| 484 | assert((aclkm_div + 1) * ACLKM_CORE_HZ == APLL_HZ && |
| 485 | aclkm_div < 0x1f); |
| 486 | |
| 487 | pclk_dbg_div = APLL_HZ / PCLK_DBG_HZ - 1; |
| 488 | assert((pclk_dbg_div + 1) * PCLK_DBG_HZ == APLL_HZ && |
| 489 | pclk_dbg_div < 0x1f); |
| 490 | |
| 491 | atclk_div = APLL_HZ / ATCLK_CORE_HZ - 1; |
| 492 | assert((atclk_div + 1) * ATCLK_CORE_HZ == APLL_HZ && |
| 493 | atclk_div < 0x1f); |
| 494 | |
| 495 | write32(&cru_ptr->clksel_con[0], |
| 496 | RK_CLRSETBITS(ACLKM_CORE_L_DIV_CON_MASK << |
| 497 | ACLKM_CORE_L_DIV_CON_SHIFT | |
| 498 | CLK_CORE_L_PLL_SEL_MASK << |
| 499 | CLK_CORE_L_PLL_SEL_SHIFT | |
| 500 | CLK_CORE_L_DIV_MASK << CLK_CORE_L_DIV_SHIFT, |
| 501 | aclkm_div << ACLKM_CORE_L_DIV_CON_SHIFT | |
| 502 | CLK_CORE_L_PLL_SEL_ALPLL << |
| 503 | CLK_CORE_L_PLL_SEL_SHIFT | |
| 504 | 0 << CLK_CORE_L_DIV_SHIFT)); |
| 505 | |
| 506 | write32(&cru_ptr->clksel_con[1], |
| 507 | RK_CLRSETBITS(PCLK_DBG_L_DIV_MASK << PCLK_DBG_L_DIV_SHIFT | |
| 508 | ATCLK_CORE_L_DIV_MASK << ATCLK_CORE_L_DIV_SHIFT, |
| 509 | pclk_dbg_div << PCLK_DBG_L_DIV_SHIFT | |
| 510 | atclk_div << ATCLK_CORE_L_DIV_SHIFT)); |
| 511 | } |
Lin Huang | f5702e7 | 2016-03-19 22:45:19 +0800 | [diff] [blame] | 512 | |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 513 | void rkclk_configure_ddr(unsigned int hz) |
| 514 | { |
| 515 | struct pll_div dpll_cfg; |
| 516 | |
| 517 | /* IC ECO bug, need to set this register */ |
| 518 | write32(&rk3399_pmusgrf->ddr_rgn_con[16], 0xc000c000); |
| 519 | |
| 520 | /* clk_ddrc == DPLL = 24MHz / refdiv * fbdiv / postdiv1 / postdiv2 */ |
| 521 | switch (hz) { |
| 522 | case 200*MHz: |
| 523 | dpll_cfg = (struct pll_div) |
| 524 | {.refdiv = 1, .fbdiv = 50, .postdiv1 = 6, .postdiv2 = 1}; |
| 525 | break; |
| 526 | case 300*MHz: |
| 527 | dpll_cfg = (struct pll_div) |
| 528 | {.refdiv = 2, .fbdiv = 100, .postdiv1 = 4, .postdiv2 = 1}; |
| 529 | break; |
| 530 | case 666*MHz: |
| 531 | dpll_cfg = (struct pll_div) |
| 532 | {.refdiv = 2, .fbdiv = 111, .postdiv1 = 2, .postdiv2 = 1}; |
| 533 | break; |
| 534 | case 800*MHz: |
| 535 | dpll_cfg = (struct pll_div) |
| 536 | {.refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1}; |
| 537 | break; |
| 538 | default: |
| 539 | die("Unsupported SDRAM frequency, add to clock.c!"); |
| 540 | } |
| 541 | rkclk_set_pll(&cru_ptr->dpll_con[0], &dpll_cfg); |
| 542 | } |
| 543 | |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 544 | #define SPI_CLK_REG_VALUE(bus, clk_div) \ |
| 545 | RK_CLRSETBITS(CLK_SPI_PLL_SEL_MASK << \ |
| 546 | CLK_SPI ##bus## _PLL_SEL_SHIFT | \ |
| 547 | CLK_SPI_PLL_DIV_CON_MASK << \ |
| 548 | CLK_SPI ##bus## _PLL_DIV_CON_SHIFT, \ |
| 549 | CLK_SPI_PLL_SEL_GPLL << \ |
| 550 | CLK_SPI ##bus## _PLL_SEL_SHIFT | \ |
| 551 | (clk_div - 1) << \ |
| 552 | CLK_SPI ##bus## _PLL_DIV_CON_SHIFT) |
| 553 | |
huang lin | c14b54d | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 554 | void rkclk_configure_spi(unsigned int bus, unsigned int hz) |
| 555 | { |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 556 | int src_clk_div; |
| 557 | int pll; |
| 558 | |
| 559 | /* spi3 src clock from ppll, while spi0,1,2,4,5 src clock from gpll */ |
| 560 | pll = (bus == 3) ? PPLL_HZ : GPLL_HZ; |
| 561 | src_clk_div = pll / hz; |
| 562 | assert((src_clk_div - 1 < 127) && (src_clk_div * hz == pll)); |
| 563 | |
| 564 | switch (bus) { |
| 565 | case 0: |
| 566 | write32(&cru_ptr->clksel_con[59], |
| 567 | SPI_CLK_REG_VALUE(0, src_clk_div)); |
| 568 | break; |
| 569 | case 1: |
| 570 | write32(&cru_ptr->clksel_con[59], |
| 571 | SPI_CLK_REG_VALUE(1, src_clk_div)); |
| 572 | break; |
| 573 | case 2: |
| 574 | write32(&cru_ptr->clksel_con[60], |
| 575 | SPI_CLK_REG_VALUE(2, src_clk_div)); |
| 576 | break; |
| 577 | case 3: |
| 578 | write32(&pmucru_ptr->pmucru_clksel[1], |
| 579 | RK_CLRSETBITS(SPI3_PLL_SEL_MASK << SPI3_PLL_SEL_SHIFT | |
| 580 | SPI3_DIV_CON_MASK << SPI3_DIV_CON_SHIFT, |
| 581 | SPI3_PLL_SEL_PPLL << SPI3_PLL_SEL_SHIFT | |
| 582 | (src_clk_div - 1) << SPI3_DIV_CON_SHIFT)); |
| 583 | break; |
| 584 | case 4: |
| 585 | write32(&cru_ptr->clksel_con[60], |
| 586 | SPI_CLK_REG_VALUE(4, src_clk_div)); |
| 587 | break; |
| 588 | case 5: |
| 589 | write32(&cru_ptr->clksel_con[58], |
| 590 | SPI_CLK_REG_VALUE(5, src_clk_div)); |
| 591 | break; |
| 592 | default: |
| 593 | printk(BIOS_ERR, "do not support this spi bus\n"); |
| 594 | } |
huang lin | c14b54d | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 595 | } |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 596 | |
| 597 | #define I2C_CLK_REG_VALUE(bus, clk_div) \ |
| 598 | RK_CLRSETBITS(I2C_DIV_CON_MASK << \ |
| 599 | CLK_I2C ##bus## _DIV_CON_SHIFT | \ |
| 600 | CLK_I2C_PLL_SEL_MASK << \ |
| 601 | CLK_I2C ##bus## _PLL_SEL_SHIFT, \ |
| 602 | (clk_div - 1) << \ |
| 603 | CLK_I2C ##bus## _DIV_CON_SHIFT | \ |
| 604 | CLK_I2C_PLL_SEL_GPLL << \ |
| 605 | CLK_I2C ##bus## _PLL_SEL_SHIFT) |
| 606 | #define PMU_I2C_CLK_REG_VALUE(bus, clk_div) \ |
| 607 | RK_CLRSETBITS(I2C_DIV_CON_MASK << I2C ##bus## _DIV_CON_SHIFT, \ |
| 608 | (clk_div - 1) << I2C ##bus## _DIV_CON_SHIFT) |
| 609 | |
| 610 | static void rkclk_configure_i2c(unsigned int bus, unsigned int hz) |
| 611 | { |
| 612 | int src_clk_div; |
| 613 | int pll; |
| 614 | |
| 615 | /* i2c0,4,8 src clock from ppll, i2c1,2,3,5,6,7 src clock from gpll*/ |
| 616 | pll = (bus == 0 || bus == 4 || bus == 8) ? PPLL_HZ : GPLL_HZ; |
| 617 | src_clk_div = pll / hz; |
| 618 | assert((src_clk_div - 1 < 127) && (src_clk_div * hz == pll)); |
| 619 | |
| 620 | switch (bus) { |
| 621 | case 0: |
| 622 | write32(&pmucru_ptr->pmucru_clksel[2], |
| 623 | PMU_I2C_CLK_REG_VALUE(0, src_clk_div)); |
| 624 | break; |
| 625 | case 1: |
| 626 | write32(&cru_ptr->clksel_con[61], |
| 627 | I2C_CLK_REG_VALUE(1, src_clk_div)); |
| 628 | break; |
| 629 | case 2: |
| 630 | write32(&cru_ptr->clksel_con[62], |
| 631 | I2C_CLK_REG_VALUE(2, src_clk_div)); |
| 632 | break; |
| 633 | case 3: |
| 634 | write32(&cru_ptr->clksel_con[63], |
| 635 | I2C_CLK_REG_VALUE(3, src_clk_div)); |
| 636 | break; |
| 637 | case 4: |
| 638 | write32(&pmucru_ptr->pmucru_clksel[3], |
| 639 | PMU_I2C_CLK_REG_VALUE(4, src_clk_div)); |
| 640 | break; |
| 641 | case 5: |
| 642 | write32(&cru_ptr->clksel_con[61], |
| 643 | I2C_CLK_REG_VALUE(5, src_clk_div)); |
| 644 | break; |
| 645 | case 6: |
| 646 | write32(&cru_ptr->clksel_con[62], |
| 647 | I2C_CLK_REG_VALUE(6, src_clk_div)); |
| 648 | break; |
| 649 | case 7: |
| 650 | write32(&cru_ptr->clksel_con[63], |
| 651 | I2C_CLK_REG_VALUE(7, src_clk_div)); |
| 652 | break; |
| 653 | case 8: |
| 654 | write32(&pmucru_ptr->pmucru_clksel[2], |
| 655 | PMU_I2C_CLK_REG_VALUE(8, src_clk_div)); |
| 656 | break; |
| 657 | default: |
| 658 | printk(BIOS_ERR, "do not support this i2c bus\n"); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | uint32_t rkclk_i2c_clock_for_bus(unsigned bus) |
| 663 | { |
| 664 | uint32_t freq = 198 * 1000 * 1000; |
| 665 | |
| 666 | rkclk_configure_i2c(bus, freq); |
| 667 | |
| 668 | return freq; |
| 669 | } |
Lin Huang | bf48fbb | 2016-03-23 19:24:53 +0800 | [diff] [blame] | 670 | |
| 671 | void rkclk_configure_saradc(unsigned int hz) |
| 672 | { |
| 673 | int src_clk_div; |
| 674 | |
| 675 | /* saradc src clk from 24MHz */ |
| 676 | src_clk_div = 24 * MHz / hz; |
| 677 | assert((src_clk_div - 1 < 255) && (src_clk_div * hz == 24 * MHz)); |
| 678 | |
| 679 | write32(&cru_ptr->clksel_con[26], |
| 680 | RK_CLRSETBITS(CLK_SARADC_DIV_CON_MASK << |
| 681 | CLK_SARADC_DIV_CON_SHIFT, |
| 682 | (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT)); |
| 683 | } |
Shunqian Zheng | c7f32a5 | 2016-05-04 15:54:37 +0800 | [diff] [blame] | 684 | |
| 685 | void rkclk_configure_vop_aclk(u32 vop_id, u32 aclk_hz) |
| 686 | { |
| 687 | u32 div; |
| 688 | void *reg_addr = vop_id ? &cru_ptr->clksel_con[48] : |
| 689 | &cru_ptr->clksel_con[47]; |
| 690 | |
| 691 | /* vop aclk source clk: cpll */ |
| 692 | div = CPLL_HZ / aclk_hz; |
| 693 | assert((div - 1 < 32) && (div * aclk_hz == CPLL_HZ)); |
| 694 | |
| 695 | write32(reg_addr, RK_CLRSETBITS( |
| 696 | ACLK_VOP_PLL_SEL_MASK << ACLK_VOP_PLL_SEL_SHIFT | |
| 697 | ACLK_VOP_DIV_CON_MASK << ACLK_VOP_DIV_CON_SHIFT, |
| 698 | ACLK_VOP_PLL_SEL_CPLL << ACLK_VOP_PLL_SEL_SHIFT | |
| 699 | (div - 1) << ACLK_VOP_DIV_CON_SHIFT)); |
| 700 | } |
| 701 | |
| 702 | int rkclk_configure_vop_dclk(u32 vop_id, u32 dclk_hz) |
| 703 | { |
| 704 | struct pll_div vpll_config = {0}; |
| 705 | void *reg_addr = vop_id ? &cru_ptr->clksel_con[50] : |
| 706 | &cru_ptr->clksel_con[49]; |
| 707 | |
| 708 | /* vop dclk source from vpll, and equals to vpll(means div == 1) */ |
| 709 | if (pll_para_config(dclk_hz, &vpll_config)) |
| 710 | return -1; |
| 711 | |
| 712 | rkclk_set_pll(&cru_ptr->vpll_con[0], &vpll_config); |
| 713 | |
| 714 | write32(reg_addr, RK_CLRSETBITS( |
| 715 | DCLK_VOP_DCLK_SEL_MASK << DCLK_VOP_DCLK_SEL_SHIFT | |
| 716 | DCLK_VOP_PLL_SEL_MASK << DCLK_VOP_PLL_SEL_SHIFT | |
| 717 | DCLK_VOP_DIV_CON_MASK << DCLK_VOP_DIV_CON_SHIFT, |
| 718 | DCLK_VOP_DCLK_SEL_DIVOUT << DCLK_VOP_DCLK_SEL_SHIFT | |
| 719 | DCLK_VOP_PLL_SEL_VPLL << DCLK_VOP_PLL_SEL_SHIFT | |
| 720 | (1 - 1) << DCLK_VOP_DIV_CON_SHIFT)); |
| 721 | |
| 722 | return 0; |
| 723 | } |
Shunqian Zheng | f4181ce | 2016-05-06 16:50:48 +0800 | [diff] [blame] | 724 | |
| 725 | void rkclk_configure_tsadc(unsigned int hz) |
| 726 | { |
| 727 | int src_clk_div; |
| 728 | |
| 729 | /* use 24M as src clock */ |
| 730 | src_clk_div = OSC_HZ / hz; |
| 731 | assert((src_clk_div - 1 < 1024) && (src_clk_div * hz == OSC_HZ)); |
| 732 | |
| 733 | write32(&cru_ptr->clksel_con[27], RK_CLRSETBITS( |
| 734 | CLK_TSADC_DIV_CON_MASK << CLK_TSADC_DIV_CON_SHIFT | |
| 735 | CLK_TSADC_SEL_MASK << CLK_TSADC_SEL_SHIFT, |
| 736 | src_clk_div << CLK_TSADC_DIV_CON_SHIFT | |
| 737 | CLK_TSADC_SEL_X24M << CLK_TSADC_SEL_SHIFT)); |
| 738 | } |