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,\ |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 43 | STRINGIFY(hz) " Hz cannot be hit with PLL "\ |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 44 | "divisors on line " STRINGIFY(__LINE__)) |
| 45 | |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 46 | static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 1, 4, 1); |
| 47 | static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 3, 1); |
| 48 | static const struct pll_div ppll_init_cfg = PLL_DIVISORS(PPLL_HZ, 3, 2, 1); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 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, |
Caesar Wang | e085a8a | 2017-05-04 09:24:23 +0800 | [diff] [blame] | 85 | PLL_FRAC_MODE = 0, |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 86 | PLL_INTEGER_MODE = 1, |
| 87 | |
Caesar Wang | e085a8a | 2017-05-04 09:24:23 +0800 | [diff] [blame] | 88 | /* PLL_CON4 */ |
| 89 | PLL_SSMOD_BP_MASK = 1, |
| 90 | PLL_SSMOD_BP_SHIFT = 0, |
| 91 | PLL_SSMOD_DIS_SSCG_MASK = 1, |
| 92 | PLL_SSMOD_DIS_SSCG_SHIFT = 1, |
| 93 | PLL_SSMOD_RESET_MASK = 1, |
| 94 | PLL_SSMOD_RESET_SHIFT = 2, |
| 95 | PLL_SSMOD_DOWNSPEAD_MASK = 1, |
| 96 | PLL_SSMOD_DOWNSPEAD_SHIFT = 3, |
| 97 | PLL_SSMOD_DIVVAL_MASK = 0Xf, |
| 98 | PLL_SSMOD_DIVVAL_SHIFT = 4, |
| 99 | PLL_SSMOD_SPREADAMP_MASK = 0x1f, |
| 100 | PLL_SSMOD_SPREADAMP_SHIFT = 8, |
| 101 | |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 102 | /* PMUCRU_CLKSEL_CON0 */ |
| 103 | PMU_PCLK_DIV_CON_MASK = 0x1f, |
| 104 | PMU_PCLK_DIV_CON_SHIFT = 0, |
| 105 | |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 106 | /* PMUCRU_CLKSEL_CON1 */ |
| 107 | SPI3_PLL_SEL_MASK = 1, |
| 108 | SPI3_PLL_SEL_SHIFT = 7, |
| 109 | SPI3_PLL_SEL_24M = 0, |
| 110 | SPI3_PLL_SEL_PPLL = 1, |
| 111 | SPI3_DIV_CON_MASK = 0x7f, |
| 112 | SPI3_DIV_CON_SHIFT = 0x0, |
| 113 | |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 114 | /* PMUCRU_CLKSEL_CON2 */ |
| 115 | I2C_DIV_CON_MASK = 0x7f, |
| 116 | I2C8_DIV_CON_SHIFT = 8, |
| 117 | I2C0_DIV_CON_SHIFT = 0, |
| 118 | |
| 119 | /* PMUCRU_CLKSEL_CON3 */ |
| 120 | I2C4_DIV_CON_SHIFT = 0, |
| 121 | |
Lin Huang | bdd06de | 2016-06-28 15:21:20 +0800 | [diff] [blame] | 122 | /* CLKSEL_CON0 / CLKSEL_CON2 */ |
| 123 | ACLKM_CORE_DIV_CON_MASK = 0x1f, |
| 124 | ACLKM_CORE_DIV_CON_SHIFT = 8, |
| 125 | CLK_CORE_PLL_SEL_MASK = 3, |
| 126 | CLK_CORE_PLL_SEL_SHIFT = 6, |
| 127 | CLK_CORE_PLL_SEL_ALPLL = 0x0, |
| 128 | CLK_CORE_PLL_SEL_ABPLL = 0x1, |
| 129 | CLK_CORE_PLL_SEL_DPLL = 0x10, |
| 130 | CLK_CORE_PLL_SEL_GPLL = 0x11, |
| 131 | CLK_CORE_DIV_MASK = 0x1f, |
| 132 | CLK_CORE_DIV_SHIFT = 0, |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 133 | |
Lin Huang | bdd06de | 2016-06-28 15:21:20 +0800 | [diff] [blame] | 134 | /* CLKSEL_CON1 / CLKSEL_CON3 */ |
| 135 | PCLK_DBG_DIV_MASK = 0x1f, |
| 136 | PCLK_DBG_DIV_SHIFT = 0x8, |
| 137 | ATCLK_CORE_DIV_MASK = 0x1f, |
| 138 | ATCLK_CORE_DIV_SHIFT = 0, |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 139 | |
| 140 | /* CLKSEL_CON14 */ |
| 141 | PCLK_PERIHP_DIV_CON_MASK = 0x7, |
| 142 | PCLK_PERIHP_DIV_CON_SHIFT = 12, |
| 143 | HCLK_PERIHP_DIV_CON_MASK = 3, |
| 144 | HCLK_PERIHP_DIV_CON_SHIFT = 8, |
| 145 | ACLK_PERIHP_PLL_SEL_MASK = 1, |
| 146 | ACLK_PERIHP_PLL_SEL_SHIFT = 7, |
| 147 | ACLK_PERIHP_PLL_SEL_CPLL = 0, |
| 148 | ACLK_PERIHP_PLL_SEL_GPLL = 1, |
| 149 | ACLK_PERIHP_DIV_CON_MASK = 0x1f, |
| 150 | ACLK_PERIHP_DIV_CON_SHIFT = 0, |
| 151 | |
Lin Huang | 2f7ed8d | 2016-04-08 18:56:20 +0800 | [diff] [blame] | 152 | /* CLKSEL_CON21 */ |
| 153 | ACLK_EMMC_PLL_SEL_MASK = 0x1, |
| 154 | ACLK_EMMC_PLL_SEL_SHIFT = 7, |
| 155 | ACLK_EMMC_PLL_SEL_GPLL = 0x1, |
| 156 | ACLK_EMMC_DIV_CON_MASK = 0x1f, |
| 157 | ACLK_EMMC_DIV_CON_SHIFT = 0, |
| 158 | |
| 159 | /* CLKSEL_CON22 */ |
| 160 | CLK_EMMC_PLL_MASK = 0x7, |
| 161 | CLK_EMMC_PLL_SHIFT = 8, |
| 162 | CLK_EMMC_PLL_SEL_GPLL = 0x1, |
| 163 | CLK_EMMC_DIV_CON_MASK = 0x7f, |
| 164 | CLK_EMMC_DIV_CON_SHIFT = 0, |
| 165 | |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 166 | /* CLKSEL_CON23 */ |
| 167 | PCLK_PERILP0_DIV_CON_MASK = 0x7, |
| 168 | PCLK_PERILP0_DIV_CON_SHIFT = 12, |
| 169 | HCLK_PERILP0_DIV_CON_MASK = 3, |
| 170 | HCLK_PERILP0_DIV_CON_SHIFT = 8, |
| 171 | ACLK_PERILP0_PLL_SEL_MASK = 1, |
| 172 | ACLK_PERILP0_PLL_SEL_SHIFT = 7, |
| 173 | ACLK_PERILP0_PLL_SEL_CPLL = 0, |
| 174 | ACLK_PERILP0_PLL_SEL_GPLL = 1, |
| 175 | ACLK_PERILP0_DIV_CON_MASK = 0x1f, |
| 176 | ACLK_PERILP0_DIV_CON_SHIFT = 0, |
| 177 | |
| 178 | /* CLKSEL_CON25 */ |
| 179 | PCLK_PERILP1_DIV_CON_MASK = 0x7, |
| 180 | PCLK_PERILP1_DIV_CON_SHIFT = 8, |
| 181 | HCLK_PERILP1_PLL_SEL_MASK = 1, |
| 182 | HCLK_PERILP1_PLL_SEL_SHIFT = 7, |
| 183 | HCLK_PERILP1_PLL_SEL_CPLL = 0, |
| 184 | HCLK_PERILP1_PLL_SEL_GPLL = 1, |
| 185 | HCLK_PERILP1_DIV_CON_MASK = 0x1f, |
| 186 | HCLK_PERILP1_DIV_CON_SHIFT = 0, |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 187 | |
Lin Huang | bf48fbb | 2016-03-23 19:24:53 +0800 | [diff] [blame] | 188 | /* CLKSEL_CON26 */ |
| 189 | CLK_SARADC_DIV_CON_MASK = 0xff, |
| 190 | CLK_SARADC_DIV_CON_SHIFT = 8, |
| 191 | |
Shunqian Zheng | f4181ce | 2016-05-06 16:50:48 +0800 | [diff] [blame] | 192 | /* CLKSEL_CON27 */ |
| 193 | CLK_TSADC_SEL_X24M = 0x0, |
| 194 | CLK_TSADC_SEL_MASK = 1, |
| 195 | CLK_TSADC_SEL_SHIFT = 15, |
| 196 | CLK_TSADC_DIV_CON_MASK = 0x3ff, |
| 197 | CLK_TSADC_DIV_CON_SHIFT = 0, |
| 198 | |
Lin Huang | 4ecccff | 2017-01-18 09:44:34 +0800 | [diff] [blame] | 199 | /* CLKSEL_CON44 */ |
| 200 | CLK_PCLK_EDP_PLL_SEL_MASK = 1, |
| 201 | CLK_PCLK_EDP_PLL_SEL_SHIFT = 15, |
| 202 | CLK_PCLK_EDP_PLL_SEL_CPLL = 0, |
| 203 | CLK_PCLK_EDP_DIV_CON_MASK = 0x3f, |
| 204 | CLK_PCLK_EDP_DIV_CON_SHIFT = 8, |
| 205 | |
Shunqian Zheng | c7f32a5 | 2016-05-04 15:54:37 +0800 | [diff] [blame] | 206 | /* CLKSEL_CON47 & CLKSEL_CON48 */ |
| 207 | ACLK_VOP_PLL_SEL_MASK = 0x3, |
| 208 | ACLK_VOP_PLL_SEL_SHIFT = 6, |
| 209 | ACLK_VOP_PLL_SEL_CPLL = 0x1, |
| 210 | ACLK_VOP_DIV_CON_MASK = 0x1f, |
| 211 | ACLK_VOP_DIV_CON_SHIFT = 0, |
| 212 | |
| 213 | /* CLKSEL_CON49 & CLKSEL_CON50 */ |
| 214 | DCLK_VOP_DCLK_SEL_MASK = 1, |
| 215 | DCLK_VOP_DCLK_SEL_SHIFT = 11, |
| 216 | DCLK_VOP_DCLK_SEL_DIVOUT = 0, |
| 217 | DCLK_VOP_PLL_SEL_MASK = 3, |
| 218 | DCLK_VOP_PLL_SEL_SHIFT = 8, |
| 219 | DCLK_VOP_PLL_SEL_VPLL = 0, |
| 220 | DCLK_VOP_DIV_CON_MASK = 0xff, |
| 221 | DCLK_VOP_DIV_CON_SHIFT = 0, |
| 222 | |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 223 | /* CLKSEL_CON58 */ |
| 224 | CLK_SPI_PLL_SEL_MASK = 1, |
| 225 | CLK_SPI_PLL_SEL_CPLL = 0, |
| 226 | CLK_SPI_PLL_SEL_GPLL = 1, |
| 227 | CLK_SPI_PLL_DIV_CON_MASK = 0x7f, |
| 228 | CLK_SPI5_PLL_DIV_CON_SHIFT = 8, |
| 229 | CLK_SPI5_PLL_SEL_SHIFT = 15, |
| 230 | |
| 231 | /* CLKSEL_CON59 */ |
| 232 | CLK_SPI1_PLL_SEL_SHIFT = 15, |
| 233 | CLK_SPI1_PLL_DIV_CON_SHIFT = 8, |
| 234 | CLK_SPI0_PLL_SEL_SHIFT = 7, |
| 235 | CLK_SPI0_PLL_DIV_CON_SHIFT = 0, |
| 236 | |
| 237 | /* CLKSEL_CON60 */ |
| 238 | CLK_SPI4_PLL_SEL_SHIFT = 15, |
| 239 | CLK_SPI4_PLL_DIV_CON_SHIFT = 8, |
| 240 | CLK_SPI2_PLL_SEL_SHIFT = 7, |
| 241 | CLK_SPI2_PLL_DIV_CON_SHIFT = 0, |
| 242 | |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 243 | /* CLKSEL_CON61 */ |
| 244 | CLK_I2C_PLL_SEL_MASK = 1, |
| 245 | CLK_I2C_PLL_SEL_CPLL = 0, |
| 246 | CLK_I2C_PLL_SEL_GPLL = 1, |
| 247 | CLK_I2C5_PLL_SEL_SHIFT = 15, |
| 248 | CLK_I2C5_DIV_CON_SHIFT = 8, |
| 249 | CLK_I2C1_PLL_SEL_SHIFT = 7, |
| 250 | CLK_I2C1_DIV_CON_SHIFT = 0, |
| 251 | |
| 252 | /* CLKSEL_CON62 */ |
| 253 | CLK_I2C6_PLL_SEL_SHIFT = 15, |
| 254 | CLK_I2C6_DIV_CON_SHIFT = 8, |
| 255 | CLK_I2C2_PLL_SEL_SHIFT = 7, |
| 256 | CLK_I2C2_DIV_CON_SHIFT = 0, |
| 257 | |
| 258 | /* CLKSEL_CON63 */ |
| 259 | CLK_I2C7_PLL_SEL_SHIFT = 15, |
| 260 | CLK_I2C7_DIV_CON_SHIFT = 8, |
| 261 | CLK_I2C3_PLL_SEL_SHIFT = 7, |
| 262 | CLK_I2C3_DIV_CON_SHIFT = 0, |
| 263 | |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 264 | /* CRU_SOFTRST_CON4 */ |
| 265 | RESETN_DDR0_REQ_MASK = 1, |
| 266 | RESETN_DDR0_REQ_SHIFT = 8, |
| 267 | RESETN_DDRPHY0_REQ_MASK = 1, |
| 268 | RESETN_DDRPHY0_REQ_SHIFT = 9, |
| 269 | RESETN_DDR1_REQ_MASK = 1, |
| 270 | RESETN_DDR1_REQ_SHIFT = 12, |
| 271 | RESETN_DDRPHY1_REQ_MASK = 1, |
| 272 | RESETN_DDRPHY1_REQ_SHIFT = 13, |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | #define VCO_MAX_KHZ (3200 * (MHz / KHz)) |
| 276 | #define VCO_MIN_KHZ (800 * (MHz / KHz)) |
| 277 | #define OUTPUT_MAX_KHZ (3200 * (MHz / KHz)) |
| 278 | #define OUTPUT_MIN_KHZ (16 * (MHz / KHz)) |
| 279 | |
| 280 | /* the div restrictions of pll in integer mode, |
| 281 | * these are defined in * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0 |
| 282 | */ |
| 283 | #define PLL_DIV_MIN 16 |
| 284 | #define PLL_DIV_MAX 3200 |
| 285 | |
| 286 | /* How to calculate the PLL(from TRM V0.3 Part 1 Page 63): |
| 287 | * Formulas also embedded within the Fractional PLL Verilog model: |
| 288 | * If DSMPD = 1 (DSM is disabled, "integer mode") |
| 289 | * FOUTVCO = FREF / REFDIV * FBDIV |
| 290 | * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2 |
| 291 | * Where: |
| 292 | * FOUTVCO = Fractional PLL non-divided output frequency |
| 293 | * FOUTPOSTDIV = Fractional PLL divided output frequency |
| 294 | * (output of second post divider) |
| 295 | * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input) |
| 296 | * REFDIV = Fractional PLL input reference clock divider |
| 297 | * FBDIV = Integer value programmed into feedback divide |
| 298 | * |
| 299 | */ |
| 300 | static void rkclk_set_pll(u32 *pll_con, const struct pll_div *div) |
| 301 | { |
| 302 | /* All 8 PLLs have same VCO and output frequency range restrictions. */ |
| 303 | u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv; |
| 304 | u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2; |
| 305 | |
| 306 | printk(BIOS_DEBUG, "PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, " |
| 307 | "postdiv2=%d, vco=%u khz, output=%u khz\n", |
| 308 | pll_con, div->fbdiv, div->refdiv, div->postdiv1, |
| 309 | div->postdiv2, vco_khz, output_khz); |
| 310 | assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ && |
| 311 | output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ && |
| 312 | div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX); |
| 313 | |
| 314 | /* When power on or changing PLL setting, |
| 315 | * we must force PLL into slow mode to ensure output stable clock. |
| 316 | */ |
| 317 | write32(&pll_con[3], RK_CLRSETBITS(PLL_MODE_MASK << PLL_MODE_SHIFT, |
| 318 | PLL_MODE_SLOW << PLL_MODE_SHIFT)); |
| 319 | |
| 320 | /* use integer mode */ |
| 321 | write32(&pll_con[3], |
| 322 | RK_CLRSETBITS(PLL_DSMPD_MASK << PLL_DSMPD_SHIFT, |
| 323 | PLL_INTEGER_MODE << PLL_DSMPD_SHIFT)); |
| 324 | |
| 325 | write32(&pll_con[0], RK_CLRSETBITS(PLL_FBDIV_MASK << PLL_FBDIV_SHIFT, |
| 326 | div->fbdiv << PLL_FBDIV_SHIFT)); |
| 327 | write32(&pll_con[1], |
| 328 | RK_CLRSETBITS(PLL_POSTDIV2_MASK << PLL_POSTDIV2_SHIFT | |
| 329 | PLL_POSTDIV1_MASK << PLL_POSTDIV1_SHIFT | |
| 330 | PLL_REFDIV_MASK | PLL_REFDIV_SHIFT, |
| 331 | (div->postdiv2 << PLL_POSTDIV2_SHIFT) | |
| 332 | (div->postdiv1 << PLL_POSTDIV1_SHIFT) | |
| 333 | (div->refdiv << PLL_REFDIV_SHIFT))); |
| 334 | |
| 335 | /* waiting for pll lock */ |
| 336 | while (!(read32(&pll_con[2]) & (1 << PLL_LOCK_STATUS_SHIFT))) |
| 337 | udelay(1); |
| 338 | |
| 339 | /* pll enter normal mode */ |
| 340 | write32(&pll_con[3], RK_CLRSETBITS(PLL_MODE_MASK << PLL_MODE_SHIFT, |
| 341 | PLL_MODE_NORM << PLL_MODE_SHIFT)); |
| 342 | } |
| 343 | |
Caesar Wang | e085a8a | 2017-05-04 09:24:23 +0800 | [diff] [blame] | 344 | /* |
| 345 | * Configure the DPLL spread spectrum feature on memory clock. |
| 346 | * Configure sequence: |
Martin Roth | 9641a92 | 2018-05-20 17:46:51 -0600 | [diff] [blame] | 347 | * 1. PLL been configured as frac mode, and DACPD should be set to 1'b0. |
Caesar Wang | e085a8a | 2017-05-04 09:24:23 +0800 | [diff] [blame] | 348 | * 2. Configure DOWNSPERAD, SPREAD, DIVVAL(option: configure xPLL_CON5 with |
| 349 | * extern wave table). |
Martin Roth | 9641a92 | 2018-05-20 17:46:51 -0600 | [diff] [blame] | 350 | * 3. set ssmod_disable_sscg = 1'b0, and set ssmod_bp = 1'b0. |
| 351 | * 4. Assert RESET = 1'b1 to SSMOD. |
| 352 | * 5. RESET = 1'b0 on SSMOD. |
Caesar Wang | e085a8a | 2017-05-04 09:24:23 +0800 | [diff] [blame] | 353 | * 6. Adjust SPREAD/DIVVAL/DOWNSPREAD. |
| 354 | */ |
| 355 | static void rkclk_set_dpllssc(struct pll_div *dpll_cfg) |
| 356 | { |
| 357 | u32 divval; |
| 358 | |
Caesar Wang | e085a8a | 2017-05-04 09:24:23 +0800 | [diff] [blame] | 359 | assert(dpll_cfg->refdiv && dpll_cfg->refdiv <= 6); |
| 360 | |
| 361 | /* |
| 362 | * Need to acquire ~30kHZ which is the target modulation frequency. |
| 363 | * The modulation frequency ~ 30kHz= OSC_HZ/revdiv/128/divval |
| 364 | * (the 128 is the number points in the query table). |
| 365 | */ |
| 366 | divval = OSC_HZ / 128 / (30 * KHz) / dpll_cfg->refdiv; |
| 367 | |
| 368 | /* |
| 369 | * Use frac mode. |
| 370 | * Make sure the output frequency isn't offset, set 0 for Fractional |
| 371 | * part of feedback divide. |
| 372 | */ |
| 373 | write32(&cru_ptr->dpll_con[3], |
| 374 | RK_CLRSETBITS(PLL_DSMPD_MASK << PLL_DSMPD_SHIFT, |
| 375 | PLL_FRAC_MODE << PLL_DSMPD_SHIFT)); |
| 376 | clrsetbits_le32(&cru_ptr->dpll_con[2], |
| 377 | PLL_FRACDIV_MASK << PLL_FRACDIV_SHIFT, |
| 378 | 0 << PLL_FRACDIV_SHIFT); |
| 379 | |
| 380 | /* |
| 381 | * Configure SSC divval. |
| 382 | * Spread amplitude range = 0.1 * SPREAD[4:0] (%). |
| 383 | * The below 8 means SPREAD[4:0] that appears to mitigate EMI on boards |
| 384 | * tested. Center and down spread modulation amplitudes based on the |
| 385 | * value of SPREAD. |
| 386 | * SPREAD[4:0] Center Spread Down Spread |
| 387 | * 0 0 0 |
Martin Roth | 9641a92 | 2018-05-20 17:46:51 -0600 | [diff] [blame] | 388 | * 1 +/-0.1% -0.10% |
| 389 | * 2 +/-0.2% -0.20% |
| 390 | * 3 +/-0.3% -0.30% |
| 391 | * 4 +/-0.4% -0.40% |
| 392 | * 5 +/-0.5% -0.50% |
Caesar Wang | e085a8a | 2017-05-04 09:24:23 +0800 | [diff] [blame] | 393 | * ... |
Martin Roth | 9641a92 | 2018-05-20 17:46:51 -0600 | [diff] [blame] | 394 | * 31 +/-3.1% -3.10% |
Caesar Wang | e085a8a | 2017-05-04 09:24:23 +0800 | [diff] [blame] | 395 | */ |
| 396 | write32(&cru_ptr->dpll_con[4], |
| 397 | RK_CLRSETBITS(PLL_SSMOD_DIVVAL_MASK << PLL_SSMOD_DIVVAL_SHIFT, |
| 398 | divval << PLL_SSMOD_DIVVAL_SHIFT)); |
| 399 | write32(&cru_ptr->dpll_con[4], |
| 400 | RK_CLRSETBITS(PLL_SSMOD_SPREADAMP_MASK << |
| 401 | PLL_SSMOD_SPREADAMP_SHIFT, |
| 402 | 8 << PLL_SSMOD_SPREADAMP_SHIFT)); |
| 403 | |
| 404 | /* Enable SSC for DPLL */ |
| 405 | write32(&cru_ptr->dpll_con[4], |
| 406 | RK_CLRBITS(PLL_SSMOD_BP_MASK << PLL_SSMOD_BP_SHIFT | |
| 407 | PLL_SSMOD_DIS_SSCG_MASK << PLL_SSMOD_DIS_SSCG_SHIFT)); |
| 408 | |
| 409 | /* Deassert reset SSMOD */ |
| 410 | write32(&cru_ptr->dpll_con[4], |
| 411 | RK_CLRBITS(PLL_SSMOD_RESET_MASK << PLL_SSMOD_RESET_SHIFT)); |
| 412 | |
| 413 | udelay(20); |
| 414 | } |
| 415 | |
Shunqian Zheng | c7f32a5 | 2016-05-04 15:54:37 +0800 | [diff] [blame] | 416 | static int pll_para_config(u32 freq_hz, struct pll_div *div) |
| 417 | { |
| 418 | u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0; |
| 419 | u32 postdiv1, postdiv2 = 1; |
| 420 | u32 fref_khz; |
| 421 | u32 diff_khz, best_diff_khz; |
| 422 | const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16; |
| 423 | const u32 max_postdiv1 = 7, max_postdiv2 = 7; |
| 424 | u32 vco_khz; |
| 425 | u32 freq_khz = freq_hz / KHz; |
| 426 | |
| 427 | if (!freq_hz) { |
| 428 | printk(BIOS_ERR, "%s: the frequency can't be 0 Hz\n", __func__); |
| 429 | return -1; |
| 430 | } |
| 431 | |
| 432 | postdiv1 = div_round_up(VCO_MIN_KHZ, freq_khz); |
| 433 | if (postdiv1 > max_postdiv1) { |
| 434 | postdiv2 = div_round_up(postdiv1, max_postdiv1); |
| 435 | postdiv1 = div_round_up(postdiv1, postdiv2); |
| 436 | } |
| 437 | |
| 438 | vco_khz = freq_khz * postdiv1 * postdiv2; |
| 439 | |
| 440 | if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || |
| 441 | postdiv2 > max_postdiv2) { |
| 442 | printk(BIOS_ERR, "%s: Cannot find out a supported VCO" |
| 443 | " for Frequency (%uHz).\n", __func__, freq_hz); |
| 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | div->postdiv1 = postdiv1; |
| 448 | div->postdiv2 = postdiv2; |
| 449 | |
| 450 | best_diff_khz = vco_khz; |
| 451 | for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) { |
| 452 | fref_khz = ref_khz / refdiv; |
| 453 | |
| 454 | fbdiv = vco_khz / fref_khz; |
| 455 | if ((fbdiv >= max_fbdiv) || (fbdiv <= min_fbdiv)) |
| 456 | continue; |
| 457 | diff_khz = vco_khz - fbdiv * fref_khz; |
| 458 | if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) { |
| 459 | fbdiv++; |
| 460 | diff_khz = fref_khz - diff_khz; |
| 461 | } |
| 462 | |
| 463 | if (diff_khz >= best_diff_khz) |
| 464 | continue; |
| 465 | |
| 466 | best_diff_khz = diff_khz; |
| 467 | div->refdiv = refdiv; |
| 468 | div->fbdiv = fbdiv; |
| 469 | } |
| 470 | |
| 471 | if (best_diff_khz > 4 * (MHz/KHz)) { |
| 472 | printk(BIOS_ERR, "%s: Failed to match output frequency %u, " |
| 473 | "difference is %u Hz,exceed 4MHZ\n", __func__, freq_hz, |
| 474 | best_diff_khz * KHz); |
| 475 | return -1; |
| 476 | } |
| 477 | return 0; |
| 478 | } |
| 479 | |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 480 | void rkclk_init(void) |
| 481 | { |
| 482 | u32 aclk_div; |
| 483 | u32 hclk_div; |
| 484 | u32 pclk_div; |
| 485 | |
| 486 | /* some cru registers changed by bootrom, we'd better reset them to |
| 487 | * reset/default values described in TRM to avoid confusion in kernel. |
| 488 | * Please consider these threee lines as a fix of bootrom bug. |
| 489 | */ |
| 490 | write32(&cru_ptr->clksel_con[12], 0xffff4101); |
| 491 | write32(&cru_ptr->clksel_con[19], 0xffff033f); |
| 492 | write32(&cru_ptr->clksel_con[56], 0x00030003); |
| 493 | |
| 494 | /* configure pmu pll(ppll) */ |
| 495 | rkclk_set_pll(&pmucru_ptr->ppll_con[0], &ppll_init_cfg); |
| 496 | |
| 497 | /* configure pmu pclk */ |
| 498 | pclk_div = PPLL_HZ / PMU_PCLK_HZ - 1; |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 499 | assert((unsigned int)(PPLL_HZ - (pclk_div + 1) * PMU_PCLK_HZ) <= pclk_div |
| 500 | && pclk_div <= 0x1f); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 501 | write32(&pmucru_ptr->pmucru_clksel[0], |
| 502 | RK_CLRSETBITS(PMU_PCLK_DIV_CON_MASK << PMU_PCLK_DIV_CON_SHIFT, |
| 503 | pclk_div << PMU_PCLK_DIV_CON_SHIFT)); |
| 504 | |
| 505 | /* configure gpll cpll */ |
| 506 | rkclk_set_pll(&cru_ptr->gpll_con[0], &gpll_init_cfg); |
| 507 | rkclk_set_pll(&cru_ptr->cpll_con[0], &cpll_init_cfg); |
| 508 | |
| 509 | /* configure perihp aclk, hclk, pclk */ |
| 510 | aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1; |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 511 | assert((aclk_div + 1) * PERIHP_ACLK_HZ == GPLL_HZ && aclk_div <= 0x1f); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 512 | |
| 513 | hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1; |
| 514 | assert((hclk_div + 1) * PERIHP_HCLK_HZ == |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 515 | PERIHP_ACLK_HZ && (hclk_div <= 0x3)); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 516 | |
| 517 | pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1; |
| 518 | assert((pclk_div + 1) * PERIHP_PCLK_HZ == |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 519 | PERIHP_ACLK_HZ && (pclk_div <= 0x7)); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 520 | |
| 521 | write32(&cru_ptr->clksel_con[14], |
| 522 | RK_CLRSETBITS(PCLK_PERIHP_DIV_CON_MASK << |
| 523 | PCLK_PERIHP_DIV_CON_SHIFT | |
| 524 | HCLK_PERIHP_DIV_CON_MASK << |
| 525 | HCLK_PERIHP_DIV_CON_SHIFT | |
| 526 | ACLK_PERIHP_PLL_SEL_MASK << |
| 527 | ACLK_PERIHP_PLL_SEL_SHIFT | |
| 528 | ACLK_PERIHP_DIV_CON_MASK << |
| 529 | ACLK_PERIHP_DIV_CON_SHIFT, |
| 530 | pclk_div << PCLK_PERIHP_DIV_CON_SHIFT | |
| 531 | hclk_div << HCLK_PERIHP_DIV_CON_SHIFT | |
| 532 | ACLK_PERIHP_PLL_SEL_GPLL << |
| 533 | ACLK_PERIHP_PLL_SEL_SHIFT | |
| 534 | aclk_div << ACLK_PERIHP_DIV_CON_SHIFT)); |
| 535 | |
| 536 | /* configure perilp0 aclk, hclk, pclk */ |
| 537 | aclk_div = GPLL_HZ / PERILP0_ACLK_HZ - 1; |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 538 | assert((aclk_div + 1) * PERILP0_ACLK_HZ == GPLL_HZ && aclk_div <= 0x1f); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 539 | |
| 540 | hclk_div = PERILP0_ACLK_HZ / PERILP0_HCLK_HZ - 1; |
| 541 | assert((hclk_div + 1) * PERILP0_HCLK_HZ == |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 542 | PERILP0_ACLK_HZ && (hclk_div <= 0x3)); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 543 | |
| 544 | pclk_div = PERILP0_ACLK_HZ / PERILP0_PCLK_HZ - 1; |
| 545 | assert((pclk_div + 1) * PERILP0_PCLK_HZ == |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 546 | PERILP0_ACLK_HZ && (pclk_div <= 0x7)); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 547 | |
| 548 | write32(&cru_ptr->clksel_con[23], |
| 549 | RK_CLRSETBITS(PCLK_PERILP0_DIV_CON_MASK << |
| 550 | PCLK_PERILP0_DIV_CON_SHIFT | |
| 551 | HCLK_PERILP0_DIV_CON_MASK << |
| 552 | HCLK_PERILP0_DIV_CON_SHIFT | |
| 553 | ACLK_PERILP0_PLL_SEL_MASK << |
| 554 | ACLK_PERILP0_PLL_SEL_SHIFT | |
| 555 | ACLK_PERILP0_DIV_CON_MASK << |
| 556 | ACLK_PERILP0_DIV_CON_SHIFT, |
| 557 | pclk_div << PCLK_PERILP0_DIV_CON_SHIFT | |
| 558 | hclk_div << HCLK_PERILP0_DIV_CON_SHIFT | |
| 559 | ACLK_PERILP0_PLL_SEL_GPLL << |
| 560 | ACLK_PERILP0_PLL_SEL_SHIFT | |
| 561 | aclk_div << ACLK_PERILP0_DIV_CON_SHIFT)); |
| 562 | |
| 563 | /* perilp1 hclk select gpll as source */ |
| 564 | hclk_div = GPLL_HZ / PERILP1_HCLK_HZ - 1; |
| 565 | assert((hclk_div + 1) * PERILP1_HCLK_HZ == |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 566 | GPLL_HZ && (hclk_div <= 0x1f)); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 567 | |
Julius Werner | f7d519c | 2016-09-02 23:48:10 -0700 | [diff] [blame] | 568 | pclk_div = PERILP1_HCLK_HZ / PERILP1_PCLK_HZ - 1; |
| 569 | assert((pclk_div + 1) * PERILP1_PCLK_HZ == |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 570 | PERILP1_HCLK_HZ && (pclk_div <= 0x7)); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 571 | |
| 572 | write32(&cru_ptr->clksel_con[25], |
| 573 | RK_CLRSETBITS(PCLK_PERILP1_DIV_CON_MASK << |
| 574 | PCLK_PERILP1_DIV_CON_SHIFT | |
| 575 | HCLK_PERILP1_DIV_CON_MASK << |
| 576 | HCLK_PERILP1_DIV_CON_SHIFT | |
| 577 | HCLK_PERILP1_PLL_SEL_MASK << |
| 578 | HCLK_PERILP1_PLL_SEL_SHIFT, |
| 579 | pclk_div << PCLK_PERILP1_DIV_CON_SHIFT | |
| 580 | hclk_div << HCLK_PERILP1_DIV_CON_SHIFT | |
| 581 | HCLK_PERILP1_PLL_SEL_GPLL << |
| 582 | HCLK_PERILP1_PLL_SEL_SHIFT)); |
| 583 | } |
| 584 | |
Julius Werner | 7f96589 | 2016-08-29 15:07:58 -0700 | [diff] [blame] | 585 | void rkclk_configure_cpu(enum apll_frequencies freq, enum cpu_cluster cluster) |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 586 | { |
Julius Werner | 7f96589 | 2016-08-29 15:07:58 -0700 | [diff] [blame] | 587 | u32 aclkm_div, atclk_div, pclk_dbg_div, apll_hz; |
| 588 | int con_base, parent; |
| 589 | u32 *pll_con; |
Lin Huang | e3d78b8 | 2016-06-28 11:10:54 +0800 | [diff] [blame] | 590 | |
Julius Werner | 7f96589 | 2016-08-29 15:07:58 -0700 | [diff] [blame] | 591 | switch (cluster) { |
| 592 | case CPU_CLUSTER_LITTLE: |
| 593 | con_base = 0; |
| 594 | parent = CLK_CORE_PLL_SEL_ALPLL; |
| 595 | pll_con = &cru_ptr->apll_l_con[0]; |
| 596 | break; |
| 597 | case CPU_CLUSTER_BIG: |
| 598 | default: |
| 599 | con_base = 2; |
| 600 | parent = CLK_CORE_PLL_SEL_ABPLL; |
| 601 | pll_con = &cru_ptr->apll_b_con[0]; |
| 602 | break; |
| 603 | } |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 604 | |
Julius Werner | 7f96589 | 2016-08-29 15:07:58 -0700 | [diff] [blame] | 605 | apll_hz = apll_cfgs[freq]->freq; |
| 606 | rkclk_set_pll(pll_con, apll_cfgs[freq]); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 607 | |
Julius Werner | 7f96589 | 2016-08-29 15:07:58 -0700 | [diff] [blame] | 608 | aclkm_div = div_round_up(apll_hz, ACLKM_CORE_HZ) - 1; |
| 609 | pclk_dbg_div = div_round_up(apll_hz, PCLK_DBG_HZ) - 1; |
| 610 | atclk_div = div_round_up(apll_hz, ATCLK_CORE_HZ) - 1; |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 611 | |
Lin Huang | bdd06de | 2016-06-28 15:21:20 +0800 | [diff] [blame] | 612 | write32(&cru_ptr->clksel_con[con_base], |
| 613 | RK_CLRSETBITS(ACLKM_CORE_DIV_CON_MASK << |
| 614 | ACLKM_CORE_DIV_CON_SHIFT | |
| 615 | CLK_CORE_PLL_SEL_MASK << CLK_CORE_PLL_SEL_SHIFT | |
| 616 | CLK_CORE_DIV_MASK << CLK_CORE_DIV_SHIFT, |
| 617 | aclkm_div << ACLKM_CORE_DIV_CON_SHIFT | |
| 618 | parent << CLK_CORE_PLL_SEL_SHIFT | |
| 619 | 0 << CLK_CORE_DIV_SHIFT)); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 620 | |
Lin Huang | bdd06de | 2016-06-28 15:21:20 +0800 | [diff] [blame] | 621 | write32(&cru_ptr->clksel_con[con_base + 1], |
| 622 | RK_CLRSETBITS(PCLK_DBG_DIV_MASK << PCLK_DBG_DIV_SHIFT | |
| 623 | ATCLK_CORE_DIV_MASK << ATCLK_CORE_DIV_SHIFT, |
| 624 | pclk_dbg_div << PCLK_DBG_DIV_SHIFT | |
| 625 | atclk_div << ATCLK_CORE_DIV_SHIFT)); |
Lin Huang | a1f82a3 | 2016-03-09 18:08:20 +0800 | [diff] [blame] | 626 | } |
Lin Huang | f5702e7 | 2016-03-19 22:45:19 +0800 | [diff] [blame] | 627 | |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 628 | void rkclk_configure_ddr(unsigned int hz) |
| 629 | { |
| 630 | struct pll_div dpll_cfg; |
| 631 | |
| 632 | /* IC ECO bug, need to set this register */ |
| 633 | write32(&rk3399_pmusgrf->ddr_rgn_con[16], 0xc000c000); |
| 634 | |
| 635 | /* clk_ddrc == DPLL = 24MHz / refdiv * fbdiv / postdiv1 / postdiv2 */ |
| 636 | switch (hz) { |
| 637 | case 200*MHz: |
| 638 | dpll_cfg = (struct pll_div) |
Caesar Wang | a0199d8 | 2017-06-22 16:14:58 +0800 | [diff] [blame] | 639 | {.refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 2}; |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 640 | break; |
| 641 | case 300*MHz: |
| 642 | dpll_cfg = (struct pll_div) |
| 643 | {.refdiv = 2, .fbdiv = 100, .postdiv1 = 4, .postdiv2 = 1}; |
| 644 | break; |
| 645 | case 666*MHz: |
| 646 | dpll_cfg = (struct pll_div) |
| 647 | {.refdiv = 2, .fbdiv = 111, .postdiv1 = 2, .postdiv2 = 1}; |
| 648 | break; |
| 649 | case 800*MHz: |
| 650 | dpll_cfg = (struct pll_div) |
| 651 | {.refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1}; |
| 652 | break; |
Lin Huang | ba2b63a | 2016-07-25 10:06:09 +0800 | [diff] [blame] | 653 | case 933*MHz: |
Shunqian Zheng | 0d9839b | 2016-05-11 15:18:17 +0800 | [diff] [blame] | 654 | dpll_cfg = (struct pll_div) |
Derek Basehore | 8e1a995 | 2016-10-27 13:51:49 -0700 | [diff] [blame] | 655 | {.refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1}; |
Shunqian Zheng | 0d9839b | 2016-05-11 15:18:17 +0800 | [diff] [blame] | 656 | break; |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 657 | default: |
| 658 | die("Unsupported SDRAM frequency, add to clock.c!"); |
| 659 | } |
| 660 | rkclk_set_pll(&cru_ptr->dpll_con[0], &dpll_cfg); |
Caesar Wang | e085a8a | 2017-05-04 09:24:23 +0800 | [diff] [blame] | 661 | |
| 662 | if (IS_ENABLED(CONFIG_RK3399_SPREAD_SPECTRUM_DDR)) |
| 663 | rkclk_set_dpllssc(&dpll_cfg); |
Shunqian Zheng | ce60d5a | 2016-04-21 23:53:08 +0800 | [diff] [blame] | 664 | } |
| 665 | |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 666 | #define SPI_CLK_REG_VALUE(bus, clk_div) \ |
| 667 | RK_CLRSETBITS(CLK_SPI_PLL_SEL_MASK << \ |
| 668 | CLK_SPI ##bus## _PLL_SEL_SHIFT | \ |
| 669 | CLK_SPI_PLL_DIV_CON_MASK << \ |
| 670 | CLK_SPI ##bus## _PLL_DIV_CON_SHIFT, \ |
| 671 | CLK_SPI_PLL_SEL_GPLL << \ |
| 672 | CLK_SPI ##bus## _PLL_SEL_SHIFT | \ |
| 673 | (clk_div - 1) << \ |
| 674 | CLK_SPI ##bus## _PLL_DIV_CON_SHIFT) |
| 675 | |
huang lin | c14b54d | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 676 | void rkclk_configure_spi(unsigned int bus, unsigned int hz) |
| 677 | { |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 678 | int src_clk_div; |
| 679 | int pll; |
| 680 | |
| 681 | /* spi3 src clock from ppll, while spi0,1,2,4,5 src clock from gpll */ |
| 682 | pll = (bus == 3) ? PPLL_HZ : GPLL_HZ; |
| 683 | src_clk_div = pll / hz; |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 684 | assert((src_clk_div - 1 <= 127) && (src_clk_div * hz == pll)); |
Shunqian Zheng | 347c83c | 2016-04-13 22:34:39 +0800 | [diff] [blame] | 685 | |
| 686 | switch (bus) { |
| 687 | case 0: |
| 688 | write32(&cru_ptr->clksel_con[59], |
| 689 | SPI_CLK_REG_VALUE(0, src_clk_div)); |
| 690 | break; |
| 691 | case 1: |
| 692 | write32(&cru_ptr->clksel_con[59], |
| 693 | SPI_CLK_REG_VALUE(1, src_clk_div)); |
| 694 | break; |
| 695 | case 2: |
| 696 | write32(&cru_ptr->clksel_con[60], |
| 697 | SPI_CLK_REG_VALUE(2, src_clk_div)); |
| 698 | break; |
| 699 | case 3: |
| 700 | write32(&pmucru_ptr->pmucru_clksel[1], |
| 701 | RK_CLRSETBITS(SPI3_PLL_SEL_MASK << SPI3_PLL_SEL_SHIFT | |
| 702 | SPI3_DIV_CON_MASK << SPI3_DIV_CON_SHIFT, |
| 703 | SPI3_PLL_SEL_PPLL << SPI3_PLL_SEL_SHIFT | |
| 704 | (src_clk_div - 1) << SPI3_DIV_CON_SHIFT)); |
| 705 | break; |
| 706 | case 4: |
| 707 | write32(&cru_ptr->clksel_con[60], |
| 708 | SPI_CLK_REG_VALUE(4, src_clk_div)); |
| 709 | break; |
| 710 | case 5: |
| 711 | write32(&cru_ptr->clksel_con[58], |
| 712 | SPI_CLK_REG_VALUE(5, src_clk_div)); |
| 713 | break; |
| 714 | default: |
| 715 | printk(BIOS_ERR, "do not support this spi bus\n"); |
| 716 | } |
huang lin | c14b54d | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 717 | } |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 718 | |
| 719 | #define I2C_CLK_REG_VALUE(bus, clk_div) \ |
| 720 | RK_CLRSETBITS(I2C_DIV_CON_MASK << \ |
| 721 | CLK_I2C ##bus## _DIV_CON_SHIFT | \ |
| 722 | CLK_I2C_PLL_SEL_MASK << \ |
| 723 | CLK_I2C ##bus## _PLL_SEL_SHIFT, \ |
| 724 | (clk_div - 1) << \ |
| 725 | CLK_I2C ##bus## _DIV_CON_SHIFT | \ |
| 726 | CLK_I2C_PLL_SEL_GPLL << \ |
| 727 | CLK_I2C ##bus## _PLL_SEL_SHIFT) |
| 728 | #define PMU_I2C_CLK_REG_VALUE(bus, clk_div) \ |
| 729 | RK_CLRSETBITS(I2C_DIV_CON_MASK << I2C ##bus## _DIV_CON_SHIFT, \ |
| 730 | (clk_div - 1) << I2C ##bus## _DIV_CON_SHIFT) |
| 731 | |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 732 | uint32_t rkclk_i2c_clock_for_bus(unsigned int bus) |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 733 | { |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 734 | int src_clk_div, pll, freq; |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 735 | |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 736 | /* i2c0,4,8 src clock from ppll, i2c1,2,3,5,6,7 src clock from gpll */ |
| 737 | if (bus == 0 || bus == 4 || bus == 8) { |
| 738 | pll = PPLL_HZ; |
| 739 | freq = 338*MHz; |
| 740 | } else { |
| 741 | pll = GPLL_HZ; |
| 742 | freq = 198*MHz; |
| 743 | } |
| 744 | src_clk_div = pll / freq; |
| 745 | assert((src_clk_div - 1 <= 127) && (src_clk_div * freq == pll)); |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 746 | |
| 747 | switch (bus) { |
| 748 | case 0: |
| 749 | write32(&pmucru_ptr->pmucru_clksel[2], |
| 750 | PMU_I2C_CLK_REG_VALUE(0, src_clk_div)); |
| 751 | break; |
| 752 | case 1: |
| 753 | write32(&cru_ptr->clksel_con[61], |
| 754 | I2C_CLK_REG_VALUE(1, src_clk_div)); |
| 755 | break; |
| 756 | case 2: |
| 757 | write32(&cru_ptr->clksel_con[62], |
| 758 | I2C_CLK_REG_VALUE(2, src_clk_div)); |
| 759 | break; |
| 760 | case 3: |
| 761 | write32(&cru_ptr->clksel_con[63], |
| 762 | I2C_CLK_REG_VALUE(3, src_clk_div)); |
| 763 | break; |
| 764 | case 4: |
| 765 | write32(&pmucru_ptr->pmucru_clksel[3], |
| 766 | PMU_I2C_CLK_REG_VALUE(4, src_clk_div)); |
| 767 | break; |
| 768 | case 5: |
| 769 | write32(&cru_ptr->clksel_con[61], |
| 770 | I2C_CLK_REG_VALUE(5, src_clk_div)); |
| 771 | break; |
| 772 | case 6: |
| 773 | write32(&cru_ptr->clksel_con[62], |
| 774 | I2C_CLK_REG_VALUE(6, src_clk_div)); |
| 775 | break; |
| 776 | case 7: |
| 777 | write32(&cru_ptr->clksel_con[63], |
| 778 | I2C_CLK_REG_VALUE(7, src_clk_div)); |
| 779 | break; |
| 780 | case 8: |
| 781 | write32(&pmucru_ptr->pmucru_clksel[2], |
| 782 | PMU_I2C_CLK_REG_VALUE(8, src_clk_div)); |
| 783 | break; |
| 784 | default: |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 785 | die("unknown i2c bus\n"); |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 786 | } |
huang lin | 4f17374 | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 787 | |
| 788 | return freq; |
| 789 | } |
Lin Huang | bf48fbb | 2016-03-23 19:24:53 +0800 | [diff] [blame] | 790 | |
Xing Zheng | 96fbc31 | 2016-05-19 11:39:20 +0800 | [diff] [blame] | 791 | static u32 clk_gcd(u32 a, u32 b) |
| 792 | { |
| 793 | while (b != 0) { |
| 794 | int r = b; |
| 795 | b = a % b; |
| 796 | a = r; |
| 797 | } |
| 798 | return a; |
| 799 | } |
| 800 | |
| 801 | void rkclk_configure_i2s(unsigned int hz) |
| 802 | { |
| 803 | int n, d; |
| 804 | int v; |
| 805 | |
| 806 | /** |
Elyes HAOUAS | 809aeee | 2018-08-07 12:14:33 +0200 | [diff] [blame] | 807 | * clk_i2s0_sel: divider output from fraction |
Xing Zheng | 96fbc31 | 2016-05-19 11:39:20 +0800 | [diff] [blame] | 808 | * clk_i2s0_pll_sel source clock: cpll |
| 809 | * clk_i2s0_div_con: 1 (div+1) |
| 810 | */ |
| 811 | write32(&cru_ptr->clksel_con[28], |
| 812 | RK_CLRSETBITS(3 << 8 | 1 << 7 | 0x7f << 0, |
| 813 | 1 << 8 | 0 << 7 | 0 << 0)); |
| 814 | |
| 815 | /* make sure and enable i2s0 path gates */ |
| 816 | write32(&cru_ptr->clkgate_con[8], |
| 817 | RK_CLRBITS(1 << 12 | 1 << 5 | 1 << 4 | 1 << 3)); |
| 818 | |
| 819 | /* set frac divider */ |
| 820 | v = clk_gcd(CPLL_HZ, hz); |
| 821 | n = (CPLL_HZ / v) & (0xffff); |
| 822 | d = (hz / v) & (0xffff); |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 823 | assert(hz == (u64)CPLL_HZ * d / n); |
Xing Zheng | 96fbc31 | 2016-05-19 11:39:20 +0800 | [diff] [blame] | 824 | write32(&cru_ptr->clksel_con[96], d << 16 | n); |
| 825 | |
| 826 | /** |
| 827 | * clk_i2sout_sel clk_i2s |
| 828 | * clk_i2s_ch_sel: clk_i2s0 |
| 829 | */ |
| 830 | write32(&cru_ptr->clksel_con[31], |
| 831 | RK_CLRSETBITS(1 << 2 | 3 << 0, |
| 832 | 0 << 2 | 0 << 0)); |
| 833 | } |
| 834 | |
Lin Huang | bf48fbb | 2016-03-23 19:24:53 +0800 | [diff] [blame] | 835 | void rkclk_configure_saradc(unsigned int hz) |
| 836 | { |
| 837 | int src_clk_div; |
| 838 | |
| 839 | /* saradc src clk from 24MHz */ |
| 840 | src_clk_div = 24 * MHz / hz; |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 841 | assert((src_clk_div - 1 <= 255) && (src_clk_div * hz == 24 * MHz)); |
Lin Huang | bf48fbb | 2016-03-23 19:24:53 +0800 | [diff] [blame] | 842 | |
| 843 | write32(&cru_ptr->clksel_con[26], |
| 844 | RK_CLRSETBITS(CLK_SARADC_DIV_CON_MASK << |
| 845 | CLK_SARADC_DIV_CON_SHIFT, |
| 846 | (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT)); |
| 847 | } |
Shunqian Zheng | c7f32a5 | 2016-05-04 15:54:37 +0800 | [diff] [blame] | 848 | |
| 849 | void rkclk_configure_vop_aclk(u32 vop_id, u32 aclk_hz) |
| 850 | { |
| 851 | u32 div; |
| 852 | void *reg_addr = vop_id ? &cru_ptr->clksel_con[48] : |
| 853 | &cru_ptr->clksel_con[47]; |
| 854 | |
| 855 | /* vop aclk source clk: cpll */ |
| 856 | div = CPLL_HZ / aclk_hz; |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 857 | assert((div - 1 <= 31) && (div * aclk_hz == CPLL_HZ)); |
Shunqian Zheng | c7f32a5 | 2016-05-04 15:54:37 +0800 | [diff] [blame] | 858 | |
| 859 | write32(reg_addr, RK_CLRSETBITS( |
| 860 | ACLK_VOP_PLL_SEL_MASK << ACLK_VOP_PLL_SEL_SHIFT | |
| 861 | ACLK_VOP_DIV_CON_MASK << ACLK_VOP_DIV_CON_SHIFT, |
| 862 | ACLK_VOP_PLL_SEL_CPLL << ACLK_VOP_PLL_SEL_SHIFT | |
| 863 | (div - 1) << ACLK_VOP_DIV_CON_SHIFT)); |
| 864 | } |
| 865 | |
| 866 | int rkclk_configure_vop_dclk(u32 vop_id, u32 dclk_hz) |
| 867 | { |
| 868 | struct pll_div vpll_config = {0}; |
| 869 | void *reg_addr = vop_id ? &cru_ptr->clksel_con[50] : |
| 870 | &cru_ptr->clksel_con[49]; |
| 871 | |
| 872 | /* vop dclk source from vpll, and equals to vpll(means div == 1) */ |
| 873 | if (pll_para_config(dclk_hz, &vpll_config)) |
| 874 | return -1; |
| 875 | |
| 876 | rkclk_set_pll(&cru_ptr->vpll_con[0], &vpll_config); |
| 877 | |
| 878 | write32(reg_addr, RK_CLRSETBITS( |
| 879 | DCLK_VOP_DCLK_SEL_MASK << DCLK_VOP_DCLK_SEL_SHIFT | |
| 880 | DCLK_VOP_PLL_SEL_MASK << DCLK_VOP_PLL_SEL_SHIFT | |
| 881 | DCLK_VOP_DIV_CON_MASK << DCLK_VOP_DIV_CON_SHIFT, |
| 882 | DCLK_VOP_DCLK_SEL_DIVOUT << DCLK_VOP_DCLK_SEL_SHIFT | |
| 883 | DCLK_VOP_PLL_SEL_VPLL << DCLK_VOP_PLL_SEL_SHIFT | |
| 884 | (1 - 1) << DCLK_VOP_DIV_CON_SHIFT)); |
| 885 | |
| 886 | return 0; |
| 887 | } |
Shunqian Zheng | f4181ce | 2016-05-06 16:50:48 +0800 | [diff] [blame] | 888 | |
| 889 | void rkclk_configure_tsadc(unsigned int hz) |
| 890 | { |
| 891 | int src_clk_div; |
| 892 | |
| 893 | /* use 24M as src clock */ |
| 894 | src_clk_div = OSC_HZ / hz; |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 895 | assert((src_clk_div - 1 <= 1023) && (src_clk_div * hz == OSC_HZ)); |
Shunqian Zheng | f4181ce | 2016-05-06 16:50:48 +0800 | [diff] [blame] | 896 | |
| 897 | write32(&cru_ptr->clksel_con[27], RK_CLRSETBITS( |
| 898 | CLK_TSADC_DIV_CON_MASK << CLK_TSADC_DIV_CON_SHIFT | |
| 899 | CLK_TSADC_SEL_MASK << CLK_TSADC_SEL_SHIFT, |
| 900 | src_clk_div << CLK_TSADC_DIV_CON_SHIFT | |
| 901 | CLK_TSADC_SEL_X24M << CLK_TSADC_SEL_SHIFT)); |
| 902 | } |
Lin Huang | 2f7ed8d | 2016-04-08 18:56:20 +0800 | [diff] [blame] | 903 | |
| 904 | void rkclk_configure_emmc(void) |
| 905 | { |
| 906 | int src_clk_div; |
Ziyuan Xu | c53cf64 | 2016-09-18 10:49:52 +0800 | [diff] [blame] | 907 | int aclk_emmc = 148500*KHz; |
| 908 | int clk_emmc = 148500*KHz; |
Lin Huang | 2f7ed8d | 2016-04-08 18:56:20 +0800 | [diff] [blame] | 909 | |
| 910 | /* Select aclk_emmc source from GPLL */ |
| 911 | src_clk_div = GPLL_HZ / aclk_emmc; |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 912 | assert((src_clk_div - 1 <= 31) && (src_clk_div * aclk_emmc == GPLL_HZ)); |
Lin Huang | 2f7ed8d | 2016-04-08 18:56:20 +0800 | [diff] [blame] | 913 | |
| 914 | write32(&cru_ptr->clksel_con[21], |
| 915 | RK_CLRSETBITS(ACLK_EMMC_PLL_SEL_MASK << |
| 916 | ACLK_EMMC_PLL_SEL_SHIFT | |
| 917 | ACLK_EMMC_DIV_CON_MASK << ACLK_EMMC_DIV_CON_SHIFT, |
| 918 | ACLK_EMMC_PLL_SEL_GPLL << |
| 919 | ACLK_EMMC_PLL_SEL_SHIFT | |
| 920 | (src_clk_div - 1) << ACLK_EMMC_DIV_CON_SHIFT)); |
| 921 | |
| 922 | /* Select clk_emmc source from GPLL too */ |
| 923 | src_clk_div = GPLL_HZ / clk_emmc; |
Julius Werner | b37c8c0 | 2016-09-06 14:09:16 -0700 | [diff] [blame] | 924 | assert((src_clk_div - 1 <= 127) && (src_clk_div * clk_emmc == GPLL_HZ)); |
Lin Huang | 2f7ed8d | 2016-04-08 18:56:20 +0800 | [diff] [blame] | 925 | |
| 926 | write32(&cru_ptr->clksel_con[22], |
| 927 | RK_CLRSETBITS(CLK_EMMC_PLL_MASK << CLK_EMMC_PLL_SHIFT | |
| 928 | CLK_EMMC_DIV_CON_MASK << CLK_EMMC_DIV_CON_SHIFT, |
| 929 | CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT | |
| 930 | (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT)); |
| 931 | } |
Julius Werner | b6bf1dd | 2016-08-24 19:38:05 -0700 | [diff] [blame] | 932 | |
| 933 | int rkclk_was_watchdog_reset(void) |
| 934 | { |
| 935 | /* Bits 5 and 4 are "second" and "first" global watchdog reset. */ |
| 936 | return read32(&cru_ptr->glb_rst_st) & 0x30; |
| 937 | } |
Lin Huang | 4ecccff | 2017-01-18 09:44:34 +0800 | [diff] [blame] | 938 | |
| 939 | void rkclk_configure_edp(unsigned int hz) |
| 940 | { |
| 941 | int src_clk_div; |
| 942 | |
| 943 | src_clk_div = CPLL_HZ / hz; |
| 944 | assert((src_clk_div - 1 <= 63) && (src_clk_div * hz == CPLL_HZ)); |
| 945 | |
| 946 | write32(&cru_ptr->clksel_con[44], |
| 947 | RK_CLRSETBITS(CLK_PCLK_EDP_PLL_SEL_MASK << |
| 948 | CLK_PCLK_EDP_PLL_SEL_SHIFT | |
| 949 | CLK_PCLK_EDP_DIV_CON_MASK << |
| 950 | CLK_PCLK_EDP_DIV_CON_SHIFT, |
| 951 | CLK_PCLK_EDP_PLL_SEL_CPLL << |
| 952 | CLK_PCLK_EDP_PLL_SEL_SHIFT | |
| 953 | (src_clk_div - 1) << |
| 954 | CLK_PCLK_EDP_DIV_CON_SHIFT)); |
| 955 | } |
Nickey Yang | fe122d4 | 2017-04-27 09:38:06 +0800 | [diff] [blame] | 956 | |
| 957 | void rkclk_configure_mipi(void) |
| 958 | { |
| 959 | /* Enable clk_mipidphy_ref and clk_mipidphy_cfg */ |
| 960 | write32(&cru_ptr->clkgate_con[11], |
| 961 | RK_CLRBITS(1 << 14 | 1 << 15)); |
| 962 | /* Enable pclk_mipi_dsi0 */ |
| 963 | write32(&cru_ptr->clkgate_con[29], |
| 964 | RK_CLRBITS(1 << 1)); |
| 965 | } |