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 | |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 158 | /* CLKSEL_CON58 */ |
| 159 | CLK_SPI_PLL_SEL_MASK = 1, |
| 160 | CLK_SPI_PLL_SEL_CPLL = 0, |
| 161 | CLK_SPI_PLL_SEL_GPLL = 1, |
| 162 | CLK_SPI_PLL_DIV_CON_MASK = 0x7f, |
| 163 | CLK_SPI5_PLL_DIV_CON_SHIFT = 8, |
| 164 | CLK_SPI5_PLL_SEL_SHIFT = 15, |
| 165 | |
| 166 | /* CLKSEL_CON59 */ |
| 167 | CLK_SPI1_PLL_SEL_SHIFT = 15, |
| 168 | CLK_SPI1_PLL_DIV_CON_SHIFT = 8, |
| 169 | CLK_SPI0_PLL_SEL_SHIFT = 7, |
| 170 | CLK_SPI0_PLL_DIV_CON_SHIFT = 0, |
| 171 | |
| 172 | /* CLKSEL_CON60 */ |
| 173 | CLK_SPI4_PLL_SEL_SHIFT = 15, |
| 174 | CLK_SPI4_PLL_DIV_CON_SHIFT = 8, |
| 175 | CLK_SPI2_PLL_SEL_SHIFT = 7, |
| 176 | CLK_SPI2_PLL_DIV_CON_SHIFT = 0, |
| 177 | |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 178 | /* CLKSEL_CON61 */ |
| 179 | CLK_I2C_PLL_SEL_MASK = 1, |
| 180 | CLK_I2C_PLL_SEL_CPLL = 0, |
| 181 | CLK_I2C_PLL_SEL_GPLL = 1, |
| 182 | CLK_I2C5_PLL_SEL_SHIFT = 15, |
| 183 | CLK_I2C5_DIV_CON_SHIFT = 8, |
| 184 | CLK_I2C1_PLL_SEL_SHIFT = 7, |
| 185 | CLK_I2C1_DIV_CON_SHIFT = 0, |
| 186 | |
| 187 | /* CLKSEL_CON62 */ |
| 188 | CLK_I2C6_PLL_SEL_SHIFT = 15, |
| 189 | CLK_I2C6_DIV_CON_SHIFT = 8, |
| 190 | CLK_I2C2_PLL_SEL_SHIFT = 7, |
| 191 | CLK_I2C2_DIV_CON_SHIFT = 0, |
| 192 | |
| 193 | /* CLKSEL_CON63 */ |
| 194 | CLK_I2C7_PLL_SEL_SHIFT = 15, |
| 195 | CLK_I2C7_DIV_CON_SHIFT = 8, |
| 196 | CLK_I2C3_PLL_SEL_SHIFT = 7, |
| 197 | CLK_I2C3_DIV_CON_SHIFT = 0, |
| 198 | |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 199 | /* CRU_SOFTRST_CON4 */ |
| 200 | RESETN_DDR0_REQ_MASK = 1, |
| 201 | RESETN_DDR0_REQ_SHIFT = 8, |
| 202 | RESETN_DDRPHY0_REQ_MASK = 1, |
| 203 | RESETN_DDRPHY0_REQ_SHIFT = 9, |
| 204 | RESETN_DDR1_REQ_MASK = 1, |
| 205 | RESETN_DDR1_REQ_SHIFT = 12, |
| 206 | RESETN_DDRPHY1_REQ_MASK = 1, |
| 207 | RESETN_DDRPHY1_REQ_SHIFT = 13, |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | #define VCO_MAX_KHZ (3200 * (MHz / KHz)) |
| 211 | #define VCO_MIN_KHZ (800 * (MHz / KHz)) |
| 212 | #define OUTPUT_MAX_KHZ (3200 * (MHz / KHz)) |
| 213 | #define OUTPUT_MIN_KHZ (16 * (MHz / KHz)) |
| 214 | |
| 215 | /* the div restrictions of pll in integer mode, |
| 216 | * these are defined in * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0 |
| 217 | */ |
| 218 | #define PLL_DIV_MIN 16 |
| 219 | #define PLL_DIV_MAX 3200 |
| 220 | |
| 221 | /* How to calculate the PLL(from TRM V0.3 Part 1 Page 63): |
| 222 | * Formulas also embedded within the Fractional PLL Verilog model: |
| 223 | * If DSMPD = 1 (DSM is disabled, "integer mode") |
| 224 | * FOUTVCO = FREF / REFDIV * FBDIV |
| 225 | * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2 |
| 226 | * Where: |
| 227 | * FOUTVCO = Fractional PLL non-divided output frequency |
| 228 | * FOUTPOSTDIV = Fractional PLL divided output frequency |
| 229 | * (output of second post divider) |
| 230 | * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input) |
| 231 | * REFDIV = Fractional PLL input reference clock divider |
| 232 | * FBDIV = Integer value programmed into feedback divide |
| 233 | * |
| 234 | */ |
| 235 | static void rkclk_set_pll(u32 *pll_con, const struct pll_div *div) |
| 236 | { |
| 237 | /* All 8 PLLs have same VCO and output frequency range restrictions. */ |
| 238 | u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv; |
| 239 | u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2; |
| 240 | |
| 241 | printk(BIOS_DEBUG, "PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, " |
| 242 | "postdiv2=%d, vco=%u khz, output=%u khz\n", |
| 243 | pll_con, div->fbdiv, div->refdiv, div->postdiv1, |
| 244 | div->postdiv2, vco_khz, output_khz); |
| 245 | assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ && |
| 246 | output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ && |
| 247 | div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX); |
| 248 | |
| 249 | /* When power on or changing PLL setting, |
| 250 | * we must force PLL into slow mode to ensure output stable clock. |
| 251 | */ |
| 252 | write32(&pll_con[3], RK_CLRSETBITS(PLL_MODE_MASK << PLL_MODE_SHIFT, |
| 253 | PLL_MODE_SLOW << PLL_MODE_SHIFT)); |
| 254 | |
| 255 | /* use integer mode */ |
| 256 | write32(&pll_con[3], |
| 257 | RK_CLRSETBITS(PLL_DSMPD_MASK << PLL_DSMPD_SHIFT, |
| 258 | PLL_INTEGER_MODE << PLL_DSMPD_SHIFT)); |
| 259 | |
| 260 | write32(&pll_con[0], RK_CLRSETBITS(PLL_FBDIV_MASK << PLL_FBDIV_SHIFT, |
| 261 | div->fbdiv << PLL_FBDIV_SHIFT)); |
| 262 | write32(&pll_con[1], |
| 263 | RK_CLRSETBITS(PLL_POSTDIV2_MASK << PLL_POSTDIV2_SHIFT | |
| 264 | PLL_POSTDIV1_MASK << PLL_POSTDIV1_SHIFT | |
| 265 | PLL_REFDIV_MASK | PLL_REFDIV_SHIFT, |
| 266 | (div->postdiv2 << PLL_POSTDIV2_SHIFT) | |
| 267 | (div->postdiv1 << PLL_POSTDIV1_SHIFT) | |
| 268 | (div->refdiv << PLL_REFDIV_SHIFT))); |
| 269 | |
| 270 | /* waiting for pll lock */ |
| 271 | while (!(read32(&pll_con[2]) & (1 << PLL_LOCK_STATUS_SHIFT))) |
| 272 | udelay(1); |
| 273 | |
| 274 | /* pll enter normal mode */ |
| 275 | write32(&pll_con[3], RK_CLRSETBITS(PLL_MODE_MASK << PLL_MODE_SHIFT, |
| 276 | PLL_MODE_NORM << PLL_MODE_SHIFT)); |
| 277 | } |
| 278 | |
| 279 | void rkclk_init(void) |
| 280 | { |
| 281 | u32 aclk_div; |
| 282 | u32 hclk_div; |
| 283 | u32 pclk_div; |
| 284 | |
| 285 | /* some cru registers changed by bootrom, we'd better reset them to |
| 286 | * reset/default values described in TRM to avoid confusion in kernel. |
| 287 | * Please consider these threee lines as a fix of bootrom bug. |
| 288 | */ |
| 289 | write32(&cru_ptr->clksel_con[12], 0xffff4101); |
| 290 | write32(&cru_ptr->clksel_con[19], 0xffff033f); |
| 291 | write32(&cru_ptr->clksel_con[56], 0x00030003); |
| 292 | |
| 293 | /* configure pmu pll(ppll) */ |
| 294 | rkclk_set_pll(&pmucru_ptr->ppll_con[0], &ppll_init_cfg); |
| 295 | |
| 296 | /* configure pmu pclk */ |
| 297 | pclk_div = PPLL_HZ / PMU_PCLK_HZ - 1; |
| 298 | assert((pclk_div + 1) * PMU_PCLK_HZ == PPLL_HZ && pclk_div < 0x1f); |
| 299 | write32(&pmucru_ptr->pmucru_clksel[0], |
| 300 | RK_CLRSETBITS(PMU_PCLK_DIV_CON_MASK << PMU_PCLK_DIV_CON_SHIFT, |
| 301 | pclk_div << PMU_PCLK_DIV_CON_SHIFT)); |
| 302 | |
| 303 | /* configure gpll cpll */ |
| 304 | rkclk_set_pll(&cru_ptr->gpll_con[0], &gpll_init_cfg); |
| 305 | rkclk_set_pll(&cru_ptr->cpll_con[0], &cpll_init_cfg); |
| 306 | |
| 307 | /* configure perihp aclk, hclk, pclk */ |
| 308 | aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1; |
| 309 | assert((aclk_div + 1) * PERIHP_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 310 | |
| 311 | hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1; |
| 312 | assert((hclk_div + 1) * PERIHP_HCLK_HZ == |
| 313 | PERIHP_ACLK_HZ && (hclk_div < 0x4)); |
| 314 | |
| 315 | pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1; |
| 316 | assert((pclk_div + 1) * PERIHP_PCLK_HZ == |
| 317 | PERIHP_ACLK_HZ && (pclk_div < 0x7)); |
| 318 | |
| 319 | write32(&cru_ptr->clksel_con[14], |
| 320 | RK_CLRSETBITS(PCLK_PERIHP_DIV_CON_MASK << |
| 321 | PCLK_PERIHP_DIV_CON_SHIFT | |
| 322 | HCLK_PERIHP_DIV_CON_MASK << |
| 323 | HCLK_PERIHP_DIV_CON_SHIFT | |
| 324 | ACLK_PERIHP_PLL_SEL_MASK << |
| 325 | ACLK_PERIHP_PLL_SEL_SHIFT | |
| 326 | ACLK_PERIHP_DIV_CON_MASK << |
| 327 | ACLK_PERIHP_DIV_CON_SHIFT, |
| 328 | pclk_div << PCLK_PERIHP_DIV_CON_SHIFT | |
| 329 | hclk_div << HCLK_PERIHP_DIV_CON_SHIFT | |
| 330 | ACLK_PERIHP_PLL_SEL_GPLL << |
| 331 | ACLK_PERIHP_PLL_SEL_SHIFT | |
| 332 | aclk_div << ACLK_PERIHP_DIV_CON_SHIFT)); |
| 333 | |
| 334 | /* configure perilp0 aclk, hclk, pclk */ |
| 335 | aclk_div = GPLL_HZ / PERILP0_ACLK_HZ - 1; |
| 336 | assert((aclk_div + 1) * PERILP0_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 337 | |
| 338 | hclk_div = PERILP0_ACLK_HZ / PERILP0_HCLK_HZ - 1; |
| 339 | assert((hclk_div + 1) * PERILP0_HCLK_HZ == |
| 340 | PERILP0_ACLK_HZ && (hclk_div < 0x4)); |
| 341 | |
| 342 | pclk_div = PERILP0_ACLK_HZ / PERILP0_PCLK_HZ - 1; |
| 343 | assert((pclk_div + 1) * PERILP0_PCLK_HZ == |
| 344 | PERILP0_ACLK_HZ && (pclk_div < 0x7)); |
| 345 | |
| 346 | write32(&cru_ptr->clksel_con[23], |
| 347 | RK_CLRSETBITS(PCLK_PERILP0_DIV_CON_MASK << |
| 348 | PCLK_PERILP0_DIV_CON_SHIFT | |
| 349 | HCLK_PERILP0_DIV_CON_MASK << |
| 350 | HCLK_PERILP0_DIV_CON_SHIFT | |
| 351 | ACLK_PERILP0_PLL_SEL_MASK << |
| 352 | ACLK_PERILP0_PLL_SEL_SHIFT | |
| 353 | ACLK_PERILP0_DIV_CON_MASK << |
| 354 | ACLK_PERILP0_DIV_CON_SHIFT, |
| 355 | pclk_div << PCLK_PERILP0_DIV_CON_SHIFT | |
| 356 | hclk_div << HCLK_PERILP0_DIV_CON_SHIFT | |
| 357 | ACLK_PERILP0_PLL_SEL_GPLL << |
| 358 | ACLK_PERILP0_PLL_SEL_SHIFT | |
| 359 | aclk_div << ACLK_PERILP0_DIV_CON_SHIFT)); |
| 360 | |
| 361 | /* perilp1 hclk select gpll as source */ |
| 362 | hclk_div = GPLL_HZ / PERILP1_HCLK_HZ - 1; |
| 363 | assert((hclk_div + 1) * PERILP1_HCLK_HZ == |
| 364 | GPLL_HZ && (hclk_div < 0x1f)); |
| 365 | |
| 366 | pclk_div = PERILP1_HCLK_HZ / PERILP1_HCLK_HZ - 1; |
| 367 | assert((pclk_div + 1) * PERILP1_HCLK_HZ == |
| 368 | PERILP1_HCLK_HZ && (hclk_div < 0x7)); |
| 369 | |
| 370 | write32(&cru_ptr->clksel_con[25], |
| 371 | RK_CLRSETBITS(PCLK_PERILP1_DIV_CON_MASK << |
| 372 | PCLK_PERILP1_DIV_CON_SHIFT | |
| 373 | HCLK_PERILP1_DIV_CON_MASK << |
| 374 | HCLK_PERILP1_DIV_CON_SHIFT | |
| 375 | HCLK_PERILP1_PLL_SEL_MASK << |
| 376 | HCLK_PERILP1_PLL_SEL_SHIFT, |
| 377 | pclk_div << PCLK_PERILP1_DIV_CON_SHIFT | |
| 378 | hclk_div << HCLK_PERILP1_DIV_CON_SHIFT | |
| 379 | HCLK_PERILP1_PLL_SEL_GPLL << |
| 380 | HCLK_PERILP1_PLL_SEL_SHIFT)); |
| 381 | } |
| 382 | |
| 383 | void rkclk_configure_cpu(enum apll_l_frequencies apll_l_freq) |
| 384 | { |
| 385 | u32 aclkm_div; |
| 386 | u32 pclk_dbg_div; |
| 387 | u32 atclk_div; |
| 388 | |
| 389 | rkclk_set_pll(&cru_ptr->apll_l_con[0], apll_l_cfgs[apll_l_freq]); |
| 390 | |
| 391 | aclkm_div = APLL_HZ / ACLKM_CORE_HZ - 1; |
| 392 | assert((aclkm_div + 1) * ACLKM_CORE_HZ == APLL_HZ && |
| 393 | aclkm_div < 0x1f); |
| 394 | |
| 395 | pclk_dbg_div = APLL_HZ / PCLK_DBG_HZ - 1; |
| 396 | assert((pclk_dbg_div + 1) * PCLK_DBG_HZ == APLL_HZ && |
| 397 | pclk_dbg_div < 0x1f); |
| 398 | |
| 399 | atclk_div = APLL_HZ / ATCLK_CORE_HZ - 1; |
| 400 | assert((atclk_div + 1) * ATCLK_CORE_HZ == APLL_HZ && |
| 401 | atclk_div < 0x1f); |
| 402 | |
| 403 | write32(&cru_ptr->clksel_con[0], |
| 404 | RK_CLRSETBITS(ACLKM_CORE_L_DIV_CON_MASK << |
| 405 | ACLKM_CORE_L_DIV_CON_SHIFT | |
| 406 | CLK_CORE_L_PLL_SEL_MASK << |
| 407 | CLK_CORE_L_PLL_SEL_SHIFT | |
| 408 | CLK_CORE_L_DIV_MASK << CLK_CORE_L_DIV_SHIFT, |
| 409 | aclkm_div << ACLKM_CORE_L_DIV_CON_SHIFT | |
| 410 | CLK_CORE_L_PLL_SEL_ALPLL << |
| 411 | CLK_CORE_L_PLL_SEL_SHIFT | |
| 412 | 0 << CLK_CORE_L_DIV_SHIFT)); |
| 413 | |
| 414 | write32(&cru_ptr->clksel_con[1], |
| 415 | RK_CLRSETBITS(PCLK_DBG_L_DIV_MASK << PCLK_DBG_L_DIV_SHIFT | |
| 416 | ATCLK_CORE_L_DIV_MASK << ATCLK_CORE_L_DIV_SHIFT, |
| 417 | pclk_dbg_div << PCLK_DBG_L_DIV_SHIFT | |
| 418 | atclk_div << ATCLK_CORE_L_DIV_SHIFT)); |
| 419 | } |
Lin Huang | f5702e7 | 2016-03-19 22:45:19 +0800 | [diff] [blame] | 420 | |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 421 | void rkclk_configure_ddr(unsigned int hz) |
| 422 | { |
| 423 | struct pll_div dpll_cfg; |
| 424 | |
| 425 | /* IC ECO bug, need to set this register */ |
| 426 | write32(&rk3399_pmusgrf->ddr_rgn_con[16], 0xc000c000); |
| 427 | |
| 428 | /* clk_ddrc == DPLL = 24MHz / refdiv * fbdiv / postdiv1 / postdiv2 */ |
| 429 | switch (hz) { |
| 430 | case 200*MHz: |
| 431 | dpll_cfg = (struct pll_div) |
| 432 | {.refdiv = 1, .fbdiv = 50, .postdiv1 = 6, .postdiv2 = 1}; |
| 433 | break; |
| 434 | case 300*MHz: |
| 435 | dpll_cfg = (struct pll_div) |
| 436 | {.refdiv = 2, .fbdiv = 100, .postdiv1 = 4, .postdiv2 = 1}; |
| 437 | break; |
| 438 | case 666*MHz: |
| 439 | dpll_cfg = (struct pll_div) |
| 440 | {.refdiv = 2, .fbdiv = 111, .postdiv1 = 2, .postdiv2 = 1}; |
| 441 | break; |
| 442 | case 800*MHz: |
| 443 | dpll_cfg = (struct pll_div) |
| 444 | {.refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1}; |
| 445 | break; |
| 446 | default: |
| 447 | die("Unsupported SDRAM frequency, add to clock.c!"); |
| 448 | } |
| 449 | rkclk_set_pll(&cru_ptr->dpll_con[0], &dpll_cfg); |
| 450 | } |
| 451 | |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 452 | #define SPI_CLK_REG_VALUE(bus, clk_div) \ |
| 453 | RK_CLRSETBITS(CLK_SPI_PLL_SEL_MASK << \ |
| 454 | CLK_SPI ##bus## _PLL_SEL_SHIFT | \ |
| 455 | CLK_SPI_PLL_DIV_CON_MASK << \ |
| 456 | CLK_SPI ##bus## _PLL_DIV_CON_SHIFT, \ |
| 457 | CLK_SPI_PLL_SEL_GPLL << \ |
| 458 | CLK_SPI ##bus## _PLL_SEL_SHIFT | \ |
| 459 | (clk_div - 1) << \ |
| 460 | CLK_SPI ##bus## _PLL_DIV_CON_SHIFT) |
| 461 | |
huang lin | c14b54d | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 462 | void rkclk_configure_spi(unsigned int bus, unsigned int hz) |
| 463 | { |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 464 | int src_clk_div; |
| 465 | int pll; |
| 466 | |
| 467 | /* spi3 src clock from ppll, while spi0,1,2,4,5 src clock from gpll */ |
| 468 | pll = (bus == 3) ? PPLL_HZ : GPLL_HZ; |
| 469 | src_clk_div = pll / hz; |
| 470 | assert((src_clk_div - 1 < 127) && (src_clk_div * hz == pll)); |
| 471 | |
| 472 | switch (bus) { |
| 473 | case 0: |
| 474 | write32(&cru_ptr->clksel_con[59], |
| 475 | SPI_CLK_REG_VALUE(0, src_clk_div)); |
| 476 | break; |
| 477 | case 1: |
| 478 | write32(&cru_ptr->clksel_con[59], |
| 479 | SPI_CLK_REG_VALUE(1, src_clk_div)); |
| 480 | break; |
| 481 | case 2: |
| 482 | write32(&cru_ptr->clksel_con[60], |
| 483 | SPI_CLK_REG_VALUE(2, src_clk_div)); |
| 484 | break; |
| 485 | case 3: |
| 486 | write32(&pmucru_ptr->pmucru_clksel[1], |
| 487 | RK_CLRSETBITS(SPI3_PLL_SEL_MASK << SPI3_PLL_SEL_SHIFT | |
| 488 | SPI3_DIV_CON_MASK << SPI3_DIV_CON_SHIFT, |
| 489 | SPI3_PLL_SEL_PPLL << SPI3_PLL_SEL_SHIFT | |
| 490 | (src_clk_div - 1) << SPI3_DIV_CON_SHIFT)); |
| 491 | break; |
| 492 | case 4: |
| 493 | write32(&cru_ptr->clksel_con[60], |
| 494 | SPI_CLK_REG_VALUE(4, src_clk_div)); |
| 495 | break; |
| 496 | case 5: |
| 497 | write32(&cru_ptr->clksel_con[58], |
| 498 | SPI_CLK_REG_VALUE(5, src_clk_div)); |
| 499 | break; |
| 500 | default: |
| 501 | printk(BIOS_ERR, "do not support this spi bus\n"); |
| 502 | } |
huang lin | c14b54d | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 503 | } |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 504 | |
| 505 | #define I2C_CLK_REG_VALUE(bus, clk_div) \ |
| 506 | RK_CLRSETBITS(I2C_DIV_CON_MASK << \ |
| 507 | CLK_I2C ##bus## _DIV_CON_SHIFT | \ |
| 508 | CLK_I2C_PLL_SEL_MASK << \ |
| 509 | CLK_I2C ##bus## _PLL_SEL_SHIFT, \ |
| 510 | (clk_div - 1) << \ |
| 511 | CLK_I2C ##bus## _DIV_CON_SHIFT | \ |
| 512 | CLK_I2C_PLL_SEL_GPLL << \ |
| 513 | CLK_I2C ##bus## _PLL_SEL_SHIFT) |
| 514 | #define PMU_I2C_CLK_REG_VALUE(bus, clk_div) \ |
| 515 | RK_CLRSETBITS(I2C_DIV_CON_MASK << I2C ##bus## _DIV_CON_SHIFT, \ |
| 516 | (clk_div - 1) << I2C ##bus## _DIV_CON_SHIFT) |
| 517 | |
| 518 | static void rkclk_configure_i2c(unsigned int bus, unsigned int hz) |
| 519 | { |
| 520 | int src_clk_div; |
| 521 | int pll; |
| 522 | |
| 523 | /* i2c0,4,8 src clock from ppll, i2c1,2,3,5,6,7 src clock from gpll*/ |
| 524 | pll = (bus == 0 || bus == 4 || bus == 8) ? PPLL_HZ : GPLL_HZ; |
| 525 | src_clk_div = pll / hz; |
| 526 | assert((src_clk_div - 1 < 127) && (src_clk_div * hz == pll)); |
| 527 | |
| 528 | switch (bus) { |
| 529 | case 0: |
| 530 | write32(&pmucru_ptr->pmucru_clksel[2], |
| 531 | PMU_I2C_CLK_REG_VALUE(0, src_clk_div)); |
| 532 | break; |
| 533 | case 1: |
| 534 | write32(&cru_ptr->clksel_con[61], |
| 535 | I2C_CLK_REG_VALUE(1, src_clk_div)); |
| 536 | break; |
| 537 | case 2: |
| 538 | write32(&cru_ptr->clksel_con[62], |
| 539 | I2C_CLK_REG_VALUE(2, src_clk_div)); |
| 540 | break; |
| 541 | case 3: |
| 542 | write32(&cru_ptr->clksel_con[63], |
| 543 | I2C_CLK_REG_VALUE(3, src_clk_div)); |
| 544 | break; |
| 545 | case 4: |
| 546 | write32(&pmucru_ptr->pmucru_clksel[3], |
| 547 | PMU_I2C_CLK_REG_VALUE(4, src_clk_div)); |
| 548 | break; |
| 549 | case 5: |
| 550 | write32(&cru_ptr->clksel_con[61], |
| 551 | I2C_CLK_REG_VALUE(5, src_clk_div)); |
| 552 | break; |
| 553 | case 6: |
| 554 | write32(&cru_ptr->clksel_con[62], |
| 555 | I2C_CLK_REG_VALUE(6, src_clk_div)); |
| 556 | break; |
| 557 | case 7: |
| 558 | write32(&cru_ptr->clksel_con[63], |
| 559 | I2C_CLK_REG_VALUE(7, src_clk_div)); |
| 560 | break; |
| 561 | case 8: |
| 562 | write32(&pmucru_ptr->pmucru_clksel[2], |
| 563 | PMU_I2C_CLK_REG_VALUE(8, src_clk_div)); |
| 564 | break; |
| 565 | default: |
| 566 | printk(BIOS_ERR, "do not support this i2c bus\n"); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | uint32_t rkclk_i2c_clock_for_bus(unsigned bus) |
| 571 | { |
| 572 | uint32_t freq = 198 * 1000 * 1000; |
| 573 | |
| 574 | rkclk_configure_i2c(bus, freq); |
| 575 | |
| 576 | return freq; |
| 577 | } |