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