jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 20 | #include <arch/io.h> |
Julius Werner | 7a453eb | 2014-10-20 13:14:55 -0700 | [diff] [blame] | 21 | #include <assert.h> |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 22 | #include <console/console.h> |
| 23 | #include <delay.h> |
Julius Werner | 7a453eb | 2014-10-20 13:14:55 -0700 | [diff] [blame] | 24 | #include <soc/addressmap.h> |
| 25 | #include <soc/clock.h> |
| 26 | #include <soc/grf.h> |
| 27 | #include <soc/soc.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 31 | |
| 32 | struct pll_div { |
| 33 | u32 nr; |
| 34 | u32 nf; |
| 35 | u32 no; |
| 36 | }; |
| 37 | |
| 38 | struct rk3288_cru_reg { |
| 39 | u32 cru_apll_con[4]; |
| 40 | u32 cru_dpll_con[4]; |
| 41 | u32 cru_cpll_con[4]; |
| 42 | u32 cru_gpll_con[4]; |
| 43 | u32 cru_npll_con[4]; |
| 44 | u32 cru_mode_con; |
| 45 | u32 reserved0[3]; |
| 46 | u32 cru_clksel_con[43]; |
| 47 | u32 reserved1[21]; |
| 48 | u32 cru_clkgate_con[19]; |
| 49 | u32 reserved2; |
| 50 | u32 cru_glb_srst_fst_value; |
| 51 | u32 cru_glb_srst_snd_value; |
| 52 | u32 cru_softrst_con[12]; |
| 53 | u32 cru_misc_con; |
| 54 | u32 cru_glb_cnt_th; |
| 55 | u32 cru_glb_rst_con; |
| 56 | u32 reserved3; |
| 57 | u32 cru_glb_rst_st; |
| 58 | u32 reserved4; |
| 59 | u32 cru_sdmmc_con[2]; |
| 60 | u32 cru_sdio0_con[2]; |
| 61 | u32 cru_sdio1_con[2]; |
| 62 | u32 cru_emmc_con[2]; |
| 63 | }; |
| 64 | check_member(rk3288_cru_reg, cru_emmc_con[1], 0x021c); |
| 65 | |
| 66 | static struct rk3288_cru_reg * const cru_ptr = (void *)CRU_BASE; |
| 67 | |
huang lin | 630c86d | 2014-08-26 17:28:46 +0800 | [diff] [blame] | 68 | #define PLL_DIVISORS(hz, _nr, _no) {\ |
Julius Werner | f8dcdea | 2014-10-06 15:02:12 -0700 | [diff] [blame] | 69 | .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\ |
| 70 | _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\ |
| 71 | (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\ |
| 72 | "divisors on line " STRINGIFY(__LINE__)); |
huang lin | 630c86d | 2014-08-26 17:28:46 +0800 | [diff] [blame] | 73 | |
Julius Werner | f8dcdea | 2014-10-06 15:02:12 -0700 | [diff] [blame] | 74 | /* Keep divisors as low as possible to reduce jitter and power usage. */ |
| 75 | static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1); |
| 76 | static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2); |
| 77 | static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2); |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 78 | |
| 79 | /*******************PLL CON0 BITS***************************/ |
| 80 | #define PLL_OD_MSK (0x0F) |
| 81 | |
| 82 | #define PLL_NR_MSK (0x3F << 8) |
| 83 | #define PLL_NR_SHIFT (8) |
| 84 | |
| 85 | /*******************PLL CON1 BITS***************************/ |
| 86 | #define PLL_NF_MSK (0x1FFF) |
| 87 | |
| 88 | /*******************PLL CON2 BITS***************************/ |
| 89 | #define PLL_BWADJ_MSK (0x0FFF) |
| 90 | |
| 91 | /*******************PLL CON3 BITS***************************/ |
| 92 | #define PLL_RESET_MSK (1 << 5) |
| 93 | #define PLL_RESET (1 << 5) |
| 94 | #define PLL_RESET_RESUME (0 << 5) |
| 95 | |
| 96 | /*******************CLKSEL0 BITS***************************/ |
| 97 | /* core clk pll sel: amr or general */ |
| 98 | #define CORE_SEL_PLL_MSK (1 << 15) |
| 99 | #define CORE_SEL_APLL (0 << 15) |
| 100 | #define CORE_SEL_GPLL (1 << 15) |
| 101 | |
| 102 | /* a12 core clock div: clk_core = clk_src / (div_con + 1) */ |
| 103 | #define A12_DIV_SHIFT (8) |
| 104 | #define A12_DIV_MSK (0x1F << 8) |
| 105 | |
| 106 | /* mp core axi clock div: clk = clk_src / (div_con + 1) */ |
| 107 | #define MP_DIV_SHIFT (4) |
| 108 | #define MP_DIV_MSK (0xF << 4) |
| 109 | |
| 110 | /* m0 core axi clock div: clk = clk_src / (div_con + 1) */ |
| 111 | #define M0_DIV_MSK (0xF) |
| 112 | |
huang lin | bfdd732 | 2014-09-25 16:33:38 +0800 | [diff] [blame] | 113 | /*******************CLKSEL1 BITS***************************/ |
| 114 | /* pd bus clk pll sel: codec or general */ |
| 115 | #define PD_BUS_SEL_PLL_MSK (1 << 15) |
| 116 | #define PD_BUS_SEL_CPLL (0 << 15) |
| 117 | #define PD_BUS_SEL_GPLL (1 << 15) |
| 118 | |
| 119 | /* pd bus pclk div: |
| 120 | * pclk = pd_bus_aclk /(div + 1) |
| 121 | */ |
| 122 | #define PD_BUS_PCLK_DIV_SHIFT (12) |
| 123 | #define PD_BUS_PCLK_DIV_MSK (0x7 << 12) |
| 124 | |
| 125 | /* pd bus hclk div: |
| 126 | * aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 |
| 127 | */ |
| 128 | #define PD_BUS_HCLK_DIV_SHIFT (8) |
| 129 | #define PD_BUS_HCLK_DIV_MSK (0x3 << 8) |
| 130 | |
| 131 | /* pd bus aclk div: |
| 132 | * pd_bus_aclk = pd_bus_src_clk /(div0 * div1) |
| 133 | */ |
| 134 | #define PD_BUS_ACLK_DIV0_SHIFT (3) |
| 135 | #define PD_BUS_ACLK_DIV0_MASK (0x1f << 3) |
| 136 | #define PD_BUS_ACLK_DIV1_SHIFT (0) |
| 137 | #define PD_BUS_ACLK_DIV1_MASK (0x7 << 0) |
| 138 | |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 139 | /*******************CLKSEL10 BITS***************************/ |
| 140 | /* peripheral bus clk pll sel: codec or general */ |
| 141 | #define PERI_SEL_PLL_MSK (1 << 15) |
| 142 | #define PERI_SEL_CPLL (0 << 15) |
| 143 | #define PERI_SEL_GPLL (1 << 15) |
| 144 | |
| 145 | /* peripheral bus pclk div: |
| 146 | * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1 |
| 147 | */ |
| 148 | #define PERI_PCLK_DIV_SHIFT (12) |
| 149 | #define PERI_PCLK_DIV_MSK (0x7 << 12) |
| 150 | |
| 151 | /* peripheral bus hclk div: |
| 152 | * aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 |
| 153 | */ |
| 154 | #define PERI_HCLK_DIV_SHIFT (8) |
| 155 | #define PERI_HCLK_DIV_MSK (0x3 << 8) |
| 156 | |
| 157 | /* peripheral bus aclk div: |
| 158 | * aclk_periph = |
| 159 | * periph_clk_src / (peri_aclk_div_con + 1) |
| 160 | */ |
huang lin | bbcffd9 | 2014-09-27 12:02:27 +0800 | [diff] [blame] | 161 | #define PERI_ACLK_DIV_SHIFT (0x0) |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 162 | #define PERI_ACLK_DIV_MSK (0x1F) |
| 163 | |
| 164 | /*******************CLKSEL37 BITS***************************/ |
| 165 | #define L2_DIV_MSK (0x7) |
| 166 | |
| 167 | #define ATCLK_DIV_MSK (0x1F << 4) |
| 168 | #define ATCLK_DIV_SHIFT (4) |
| 169 | |
| 170 | #define PCLK_DBG_DIV_MSK (0x1F << 9) |
| 171 | #define PCLK_DBG_DIV_SHIFT (9) |
| 172 | |
| 173 | #define APLL_MODE_MSK (0x3) |
| 174 | #define APLL_MODE_SLOW (0) |
| 175 | #define APLL_MODE_NORM (1) |
| 176 | |
| 177 | #define DPLL_MODE_MSK (0x3 << 4) |
| 178 | #define DPLL_MODE_SLOW (0 << 4) |
| 179 | #define DPLL_MODE_NORM (1 << 4) |
| 180 | |
| 181 | #define CPLL_MODE_MSK (0x3 << 8) |
| 182 | #define CPLL_MODE_SLOW (0 << 8) |
| 183 | #define CPLL_MODE_NORM (1 << 8) |
| 184 | |
| 185 | #define GPLL_MODE_MSK (0x3 << 12) |
| 186 | #define GPLL_MODE_SLOW (0 << 12) |
| 187 | #define GPLL_MODE_NORM (1 << 12) |
| 188 | |
huang lin | 40f558e | 2014-09-19 14:51:52 +0800 | [diff] [blame] | 189 | #define NPLL_MODE_MSK (0x3 << 14) |
| 190 | #define NPLL_MODE_SLOW (0 << 14) |
| 191 | #define NPLL_MODE_NORM (1 << 14) |
| 192 | |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 193 | #define SOCSTS_DPLL_LOCK (1 << 5) |
| 194 | #define SOCSTS_APLL_LOCK (1 << 6) |
| 195 | #define SOCSTS_CPLL_LOCK (1 << 7) |
| 196 | #define SOCSTS_GPLL_LOCK (1 << 8) |
huang lin | 40f558e | 2014-09-19 14:51:52 +0800 | [diff] [blame] | 197 | #define SOCSTS_NPLL_LOCK (1 << 9) |
| 198 | |
| 199 | #define VCO_MAX_KHZ (2200 * (MHz/KHz)) |
| 200 | #define VCO_MIN_KHZ (440 * (MHz/KHz)) |
| 201 | #define OUTPUT_MAX_KHZ (2200 * (MHz/KHz)) |
| 202 | #define OUTPUT_MIN_KHZ 27500 |
| 203 | #define FREF_MAX_KHZ (2200 * (MHz/KHz)) |
| 204 | #define FREF_MIN_KHZ 269 |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 205 | |
Julius Werner | f8dcdea | 2014-10-06 15:02:12 -0700 | [diff] [blame] | 206 | static int rkclk_set_pll(u32 *pll_con, const struct pll_div *div) |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 207 | { |
Julius Werner | f8dcdea | 2014-10-06 15:02:12 -0700 | [diff] [blame] | 208 | /* All PLLs have same VCO and output frequency range restrictions. */ |
| 209 | u32 vco_khz = OSC_HZ/KHz * div->nf / div->nr; |
| 210 | u32 output_khz = vco_khz / div->no; |
| 211 | |
| 212 | printk(BIOS_DEBUG, "Configuring PLL at %p with NF = %d, NR = %d and " |
| 213 | "NO = %d (VCO = %uKHz, output = %uKHz)\n", |
| 214 | pll_con, div->nf, div->nr, div->no, vco_khz, output_khz); |
huang lin | 40f558e | 2014-09-19 14:51:52 +0800 | [diff] [blame] | 215 | assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ && |
| 216 | output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ && |
Julius Werner | f8dcdea | 2014-10-06 15:02:12 -0700 | [diff] [blame] | 217 | (div->no == 1 || !(div->no % 2))); |
| 218 | |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 219 | /* enter rest */ |
huang lin | 630c86d | 2014-08-26 17:28:46 +0800 | [diff] [blame] | 220 | writel(RK_SETBITS(PLL_RESET_MSK), &pll_con[3]); |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 221 | |
Julius Werner | f8dcdea | 2014-10-06 15:02:12 -0700 | [diff] [blame] | 222 | writel(RK_CLRSETBITS(PLL_NR_MSK, (div->nr - 1) << PLL_NR_SHIFT) |
| 223 | | RK_CLRSETBITS(PLL_OD_MSK, (div->no - 1)), &pll_con[0]); |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 224 | |
Julius Werner | f8dcdea | 2014-10-06 15:02:12 -0700 | [diff] [blame] | 225 | writel(RK_CLRSETBITS(PLL_NF_MSK, (div->nf - 1)), &pll_con[1]); |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 226 | |
Julius Werner | f8dcdea | 2014-10-06 15:02:12 -0700 | [diff] [blame] | 227 | writel(RK_CLRSETBITS(PLL_BWADJ_MSK, ((div->nf >> 1) - 1)), &pll_con[2]); |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 228 | |
| 229 | udelay(10); |
| 230 | |
| 231 | /* return form rest */ |
huang lin | 630c86d | 2014-08-26 17:28:46 +0800 | [diff] [blame] | 232 | writel(RK_CLRBITS(PLL_RESET_MSK), &pll_con[3]); |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
huang lin | bfdd732 | 2014-09-25 16:33:38 +0800 | [diff] [blame] | 237 | /* |
| 238 | TODO: |
| 239 | it should be replaced by lib.h function |
| 240 | 'unsigned long log2(unsigned long x)' |
| 241 | */ |
| 242 | static unsigned int log2(unsigned int value) |
| 243 | { |
| 244 | unsigned int div = 0; |
| 245 | |
| 246 | while (value != 1) { |
| 247 | div++; |
| 248 | value = ALIGN_UP(value, 2) / 2; |
| 249 | } |
| 250 | return div; |
| 251 | } |
| 252 | |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 253 | void rkclk_init(void) |
| 254 | { |
huang lin | bfdd732 | 2014-09-25 16:33:38 +0800 | [diff] [blame] | 255 | u32 aclk_div; |
| 256 | u32 hclk_div; |
| 257 | u32 pclk_div; |
| 258 | |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 259 | /* pll enter slow-mode */ |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 260 | writel(RK_CLRSETBITS(GPLL_MODE_MSK, GPLL_MODE_SLOW) |
Jinkun Hong | c33ce35 | 2014-08-28 09:37:22 -0700 | [diff] [blame] | 261 | | RK_CLRSETBITS(CPLL_MODE_MSK, CPLL_MODE_SLOW), |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 262 | &cru_ptr->cru_mode_con); |
| 263 | |
| 264 | /* init pll */ |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 265 | rkclk_set_pll(&cru_ptr->cru_gpll_con[0], &gpll_init_cfg); |
| 266 | rkclk_set_pll(&cru_ptr->cru_cpll_con[0], &cpll_init_cfg); |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 267 | |
| 268 | /* waiting for pll lock */ |
| 269 | while (1) { |
| 270 | if ((readl(&rk3288_grf->soc_status[1]) |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 271 | & (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) |
| 272 | == (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 273 | break; |
| 274 | udelay(1); |
| 275 | } |
| 276 | |
| 277 | /* |
huang lin | bfdd732 | 2014-09-25 16:33:38 +0800 | [diff] [blame] | 278 | * pd_bus clock pll source selection and |
| 279 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 280 | */ |
| 281 | aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1; |
| 282 | assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 283 | hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1; |
| 284 | assert((hclk_div + 1) * PD_BUS_HCLK_HZ == |
| 285 | PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2)); |
| 286 | |
| 287 | pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1; |
| 288 | assert((pclk_div + 1) * PD_BUS_PCLK_HZ == |
| 289 | PD_BUS_ACLK_HZ && pclk_div < 0x7); |
| 290 | |
| 291 | writel(RK_SETBITS(PD_BUS_SEL_GPLL) |
| 292 | | RK_CLRSETBITS(PD_BUS_PCLK_DIV_MSK, |
| 293 | pclk_div << PD_BUS_PCLK_DIV_SHIFT) |
| 294 | | RK_CLRSETBITS(PD_BUS_HCLK_DIV_MSK, |
| 295 | hclk_div << PD_BUS_HCLK_DIV_SHIFT) |
| 296 | | RK_CLRSETBITS(PD_BUS_ACLK_DIV0_MASK, |
| 297 | aclk_div << PD_BUS_ACLK_DIV0_SHIFT) |
| 298 | | RK_CLRSETBITS(PD_BUS_ACLK_DIV1_MASK, 0 << 0), |
| 299 | &cru_ptr->cru_clksel_con[1]); |
| 300 | |
| 301 | /* |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 302 | * peri clock pll source selection and |
| 303 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 304 | */ |
huang lin | bfdd732 | 2014-09-25 16:33:38 +0800 | [diff] [blame] | 305 | aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1; |
| 306 | assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 307 | |
| 308 | hclk_div = log2(PERI_ACLK_HZ / PERI_HCLK_HZ); |
| 309 | assert((1 << hclk_div) * PERI_HCLK_HZ == |
| 310 | PERI_ACLK_HZ && (hclk_div < 0x4)); |
| 311 | |
| 312 | pclk_div = log2(PERI_ACLK_HZ / PERI_PCLK_HZ); |
| 313 | assert((1 << pclk_div) * PERI_PCLK_HZ == |
| 314 | PERI_ACLK_HZ && (pclk_div < 0x4)); |
| 315 | |
| 316 | writel(RK_SETBITS(PERI_SEL_GPLL) |
| 317 | | RK_CLRSETBITS(PERI_PCLK_DIV_MSK, |
| 318 | pclk_div << PERI_PCLK_DIV_SHIFT) |
| 319 | | RK_CLRSETBITS(PERI_HCLK_DIV_MSK, |
| 320 | hclk_div << PERI_HCLK_DIV_SHIFT) |
| 321 | | RK_CLRSETBITS(PERI_ACLK_DIV_MSK, |
| 322 | aclk_div << PERI_ACLK_DIV_SHIFT), |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 323 | &cru_ptr->cru_clksel_con[10]); |
| 324 | |
| 325 | /* PLL enter normal-mode */ |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 326 | writel(RK_CLRSETBITS(GPLL_MODE_MSK, GPLL_MODE_NORM) |
Jinkun Hong | c33ce35 | 2014-08-28 09:37:22 -0700 | [diff] [blame] | 327 | | RK_CLRSETBITS(CPLL_MODE_MSK, CPLL_MODE_NORM), |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 328 | &cru_ptr->cru_mode_con); |
| 329 | |
| 330 | } |
| 331 | |
huang lin | 40f558e | 2014-09-19 14:51:52 +0800 | [diff] [blame] | 332 | void rkclk_configure_cpu(void) |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 333 | { |
| 334 | /* pll enter slow-mode */ |
| 335 | writel(RK_CLRSETBITS(APLL_MODE_MSK, APLL_MODE_SLOW), |
| 336 | &cru_ptr->cru_mode_con); |
| 337 | |
| 338 | rkclk_set_pll(&cru_ptr->cru_apll_con[0], &apll_init_cfg); |
| 339 | |
| 340 | /* waiting for pll lock */ |
| 341 | while (1) { |
| 342 | if (readl(&rk3288_grf->soc_status[1]) & SOCSTS_APLL_LOCK) |
| 343 | break; |
| 344 | udelay(1); |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * core clock pll source selection and |
| 349 | * set up dependent divisors for MPAXI/M0AXI and ARM clocks. |
| 350 | * core clock select apll, apll clk = 1800MHz |
| 351 | * arm clk = 1800MHz, mpclk = 450MHz, m0clk = 900MHz |
| 352 | */ |
| 353 | writel(RK_CLRBITS(CORE_SEL_PLL_MSK) |
| 354 | | RK_CLRSETBITS(A12_DIV_MSK, 0 << A12_DIV_SHIFT) |
| 355 | | RK_CLRSETBITS(MP_DIV_MSK, 3 << MP_DIV_SHIFT) |
| 356 | | RK_CLRSETBITS(M0_DIV_MSK, 1 << 0), |
| 357 | &cru_ptr->cru_clksel_con[0]); |
| 358 | |
| 359 | /* |
| 360 | * set up dependent divisors for L2RAM/ATCLK and PCLK clocks. |
| 361 | * l2ramclk = 900MHz, atclk = 450MHz, pclk_dbg = 450MHz |
| 362 | */ |
| 363 | writel(RK_CLRSETBITS(L2_DIV_MSK, 1 << 0) |
| 364 | | RK_CLRSETBITS(ATCLK_DIV_MSK, (3 << ATCLK_DIV_SHIFT)) |
| 365 | | RK_CLRSETBITS(PCLK_DBG_DIV_MSK, (3 << PCLK_DBG_DIV_SHIFT)), |
| 366 | &cru_ptr->cru_clksel_con[37]); |
| 367 | |
| 368 | /* PLL enter normal-mode */ |
| 369 | writel(RK_CLRSETBITS(APLL_MODE_MSK, APLL_MODE_NORM), |
| 370 | &cru_ptr->cru_mode_con); |
| 371 | } |
| 372 | |
Jinkun Hong | c33ce35 | 2014-08-28 09:37:22 -0700 | [diff] [blame] | 373 | void rkclk_configure_ddr(unsigned int hz) |
| 374 | { |
| 375 | struct pll_div dpll_cfg; |
| 376 | |
Julius Werner | f8dcdea | 2014-10-06 15:02:12 -0700 | [diff] [blame] | 377 | switch (hz) { |
| 378 | case 300*MHz: |
| 379 | dpll_cfg = (struct pll_div){.nf = 25, .nr = 2, .no = 1}; |
| 380 | break; |
| 381 | case 533*MHz: /* actually 533.3P MHz */ |
| 382 | dpll_cfg = (struct pll_div){.nf = 400, .nr = 9, .no = 2}; |
| 383 | break; |
| 384 | case 666*MHz: /* actually 666.6P MHz */ |
| 385 | dpll_cfg = (struct pll_div){.nf = 500, .nr = 9, .no = 2}; |
| 386 | break; |
| 387 | case 800*MHz: |
| 388 | dpll_cfg = (struct pll_div){.nf = 100, .nr = 3, .no = 1}; |
| 389 | break; |
| 390 | default: |
| 391 | die("Unsupported SDRAM frequency, add to clock.c!"); |
Jinkun Hong | c33ce35 | 2014-08-28 09:37:22 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Jinkun Hong | c33ce35 | 2014-08-28 09:37:22 -0700 | [diff] [blame] | 394 | /* pll enter slow-mode */ |
| 395 | writel(RK_CLRSETBITS(DPLL_MODE_MSK, DPLL_MODE_SLOW), |
| 396 | &cru_ptr->cru_mode_con); |
| 397 | |
| 398 | rkclk_set_pll(&cru_ptr->cru_dpll_con[0], &dpll_cfg); |
| 399 | |
| 400 | /* waiting for pll lock */ |
| 401 | while (1) { |
| 402 | if (readl(&rk3288_grf->soc_status[1]) & SOCSTS_DPLL_LOCK) |
| 403 | break; |
| 404 | udelay(1); |
| 405 | } |
| 406 | |
| 407 | /* PLL enter normal-mode */ |
| 408 | writel(RK_CLRSETBITS(DPLL_MODE_MSK, DPLL_MODE_NORM), |
| 409 | &cru_ptr->cru_mode_con); |
| 410 | } |
| 411 | |
| 412 | void rkclk_ddr_reset(u32 ch, u32 ctl, u32 phy) |
| 413 | { |
| 414 | u32 phy_ctl_srstn_shift = 4 + 5 * ch; |
| 415 | u32 ctl_psrstn_shift = 3 + 5 * ch; |
| 416 | u32 ctl_srstn_shift = 2 + 5 * ch; |
| 417 | u32 phy_psrstn_shift = 1 + 5 * ch; |
| 418 | u32 phy_srstn_shift = 5 * ch; |
| 419 | |
| 420 | writel(RK_CLRSETBITS(1 << phy_ctl_srstn_shift, |
| 421 | phy << phy_ctl_srstn_shift) |
| 422 | | RK_CLRSETBITS(1 << ctl_psrstn_shift, ctl << ctl_psrstn_shift) |
| 423 | | RK_CLRSETBITS(1 << ctl_srstn_shift, ctl << ctl_srstn_shift) |
| 424 | | RK_CLRSETBITS(1 << phy_psrstn_shift, phy << phy_psrstn_shift) |
| 425 | | RK_CLRSETBITS(1 << phy_srstn_shift, phy << phy_srstn_shift), |
| 426 | &cru_ptr->cru_softrst_con[10]); |
| 427 | } |
| 428 | |
| 429 | void rkclk_ddr_phy_ctl_reset(u32 ch, u32 n) |
| 430 | { |
| 431 | u32 phy_ctl_srstn_shift = 4 + 5 * ch; |
| 432 | |
| 433 | writel(RK_CLRSETBITS(1 << phy_ctl_srstn_shift, |
| 434 | n << phy_ctl_srstn_shift), |
| 435 | &cru_ptr->cru_softrst_con[10]); |
| 436 | } |
| 437 | |
huang lin | 630c86d | 2014-08-26 17:28:46 +0800 | [diff] [blame] | 438 | void rkclk_configure_spi(unsigned int bus, unsigned int hz) |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 439 | { |
huang lin | 630c86d | 2014-08-26 17:28:46 +0800 | [diff] [blame] | 440 | int src_clk_div = GPLL_HZ / hz; |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 441 | |
huang lin | 630c86d | 2014-08-26 17:28:46 +0800 | [diff] [blame] | 442 | assert((src_clk_div - 1 < 127) && (src_clk_div * hz == GPLL_HZ)); |
| 443 | |
| 444 | switch (bus) { /*select gpll as spi src clk, and set div*/ |
| 445 | case 0: |
| 446 | writel(RK_CLRSETBITS(1 << 7 | 0x1f << 0, 1 << 7 |
| 447 | | (src_clk_div - 1) << 0), |
| 448 | &cru_ptr->cru_clksel_con[25]); |
| 449 | break; |
| 450 | case 1: |
| 451 | writel(RK_CLRSETBITS(1 << 15 | 0x1f << 8, 1 << 15 |
| 452 | | (src_clk_div - 1) << 8), |
| 453 | &cru_ptr->cru_clksel_con[25]); |
| 454 | break; |
| 455 | case 2: |
| 456 | writel(RK_CLRSETBITS(1 << 7 | 0x1f << 0, 1 << 7 |
| 457 | | (src_clk_div - 1) << 0), |
| 458 | &cru_ptr->cru_clksel_con[39]); |
| 459 | break; |
| 460 | default: |
| 461 | printk(BIOS_ERR, "do not support this spi bus\n"); |
| 462 | } |
jinkun.hong | 503d121 | 2014-07-31 14:50:49 +0800 | [diff] [blame] | 463 | } |
huang lin | 739df1b | 2014-08-27 17:07:42 +0800 | [diff] [blame] | 464 | |
| 465 | static u32 clk_gcd(u32 a, u32 b) |
| 466 | { |
| 467 | while (b != 0) { |
| 468 | int r = b; |
| 469 | b = a % b; |
| 470 | a = r; |
| 471 | } |
| 472 | return a; |
| 473 | } |
| 474 | |
| 475 | void rkclk_configure_i2s(unsigned int hz) |
| 476 | { |
| 477 | int n, d; |
| 478 | int v; |
| 479 | |
| 480 | /* i2s source clock: gpll |
| 481 | i2s0_outclk_sel: clk_i2s |
| 482 | i2s0_clk_sel: divider ouput from fraction |
| 483 | i2s0_pll_div_con: 0*/ |
| 484 | writel(RK_CLRSETBITS(1 << 15 | 1 << 12 | 3 << 8 | 0x7f << 0 , |
| 485 | 1 << 15 | 0 << 12 | 1 << 8 | 0 << 0), |
| 486 | &cru_ptr->cru_clksel_con[4]); |
| 487 | |
| 488 | /* set frac divider */ |
| 489 | v = clk_gcd(GPLL_HZ, hz); |
| 490 | n = (GPLL_HZ / v) & (0xffff); |
| 491 | d = (hz / v) & (0xffff); |
| 492 | assert(hz == GPLL_HZ / n * d); |
| 493 | writel(d << 16 | n, &cru_ptr->cru_clksel_con[8]); |
| 494 | } |
huang lin | a97bd5a | 2014-10-14 10:04:16 -0700 | [diff] [blame] | 495 | |
Julius Werner | 33df495 | 2014-12-16 22:48:26 -0800 | [diff] [blame] | 496 | void rkclk_configure_crypto(unsigned int hz) |
| 497 | { |
| 498 | u32 div = PD_BUS_ACLK_HZ / hz; |
| 499 | |
| 500 | assert((div - 1 < 4) && (div * hz == PD_BUS_ACLK_HZ)); |
| 501 | assert(hz <= 150*MHz); /* Suggested max in TRM. */ |
| 502 | writel(RK_CLRSETBITS(0x3 << 6, (div - 1) << 6), |
| 503 | &cru_ptr->cru_clksel_con[26]); |
| 504 | } |
| 505 | |
huang lin | a97bd5a | 2014-10-14 10:04:16 -0700 | [diff] [blame] | 506 | void rkclk_configure_tsadc(unsigned int hz) |
| 507 | { |
| 508 | u32 div; |
| 509 | u32 src_clk = 32 * KHz; /* tsadc source clock is 32KHz*/ |
| 510 | |
| 511 | div = src_clk / hz; |
| 512 | assert((div - 1 < 64) && (div * hz == 32 * KHz)); |
| 513 | writel(RK_CLRSETBITS(0x3f << 0, (div - 1) << 0), |
| 514 | &cru_ptr->cru_clksel_con[2]); |
| 515 | } |
huang lin | 40f558e | 2014-09-19 14:51:52 +0800 | [diff] [blame] | 516 | |
| 517 | static int pll_para_config(u32 freq_hz, struct pll_div *div) |
| 518 | { |
| 519 | u32 ref_khz = OSC_HZ / KHz, nr, nf = 0; |
| 520 | u32 fref_khz; |
| 521 | u32 diff_khz, best_diff_khz; |
| 522 | const u32 max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4; |
| 523 | u32 vco_khz; |
| 524 | u32 no = 1; |
| 525 | u32 freq_khz = freq_hz / KHz; |
| 526 | |
| 527 | if (!freq_hz) { |
| 528 | printk(BIOS_ERR, "%s: the frequency can not be 0 Hz\n", __func__); |
| 529 | return -1; |
| 530 | } |
| 531 | no = div_round_up(VCO_MIN_KHZ, freq_khz); |
| 532 | |
| 533 | /* only even divisors (and 1) are supported */ |
| 534 | if (no > 1) |
| 535 | no = div_round_up(no, 2) * 2; |
| 536 | vco_khz = freq_khz * no; |
| 537 | if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) { |
| 538 | printk(BIOS_ERR, "%s: Cannot find out a supported VCO" |
| 539 | " for Frequency (%uHz).\n", __func__, freq_hz); |
| 540 | return -1; |
| 541 | } |
| 542 | div->no = no; |
| 543 | |
| 544 | best_diff_khz = vco_khz; |
| 545 | for (nr = 1; nr < max_nr && best_diff_khz; nr++) { |
| 546 | fref_khz = ref_khz / nr; |
| 547 | if (fref_khz < FREF_MIN_KHZ) |
| 548 | break; |
| 549 | if (fref_khz > FREF_MAX_KHZ) |
| 550 | continue; |
| 551 | |
| 552 | nf = vco_khz / fref_khz; |
| 553 | if (nf >= max_nf) |
| 554 | continue; |
| 555 | diff_khz = vco_khz - nf * fref_khz; |
| 556 | if (nf + 1 < max_nf && diff_khz > fref_khz / 2) { |
| 557 | nf++; |
| 558 | diff_khz = fref_khz - diff_khz; |
| 559 | } |
| 560 | |
| 561 | if (diff_khz >= best_diff_khz) |
| 562 | continue; |
| 563 | |
| 564 | best_diff_khz = diff_khz; |
| 565 | div->nr = nr; |
| 566 | div->nf = nf; |
| 567 | } |
| 568 | |
| 569 | if (best_diff_khz > 4 * (MHz/KHz)) { |
| 570 | printk(BIOS_ERR, "%s: Failed to match output frequency %u, " |
| 571 | "difference is %u Hz,exceed 4MHZ\n", __func__, freq_hz, |
| 572 | best_diff_khz * KHz); |
| 573 | return -1; |
| 574 | } |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | void rkclk_configure_edp(void) |
| 580 | { |
huang lin | 2e2288d | 2014-11-25 09:27:13 +0800 | [diff] [blame] | 581 | /* clk_edp_24M source: 24M */ |
| 582 | writel(RK_SETBITS(1 << 15), &cru_ptr->cru_clksel_con[28]); |
| 583 | |
huang lin | 40f558e | 2014-09-19 14:51:52 +0800 | [diff] [blame] | 584 | /* rst edp */ |
| 585 | writel(RK_SETBITS(1 << 15), &cru_ptr->cru_softrst_con[6]); |
| 586 | udelay(1); |
| 587 | writel(RK_CLRBITS(1 << 15), &cru_ptr->cru_softrst_con[6]); |
huang lin | 40f558e | 2014-09-19 14:51:52 +0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void rkclk_configure_vop_aclk(u32 vop_id, u32 aclk_hz) |
| 591 | { |
| 592 | u32 div; |
| 593 | |
| 594 | /* vop aclk source clk: cpll */ |
| 595 | div = CPLL_HZ / aclk_hz; |
| 596 | assert((div - 1 < 64) && (div * aclk_hz == CPLL_HZ)); |
| 597 | |
| 598 | switch (vop_id) { |
| 599 | case 0: |
| 600 | writel(RK_CLRSETBITS(3 << 6 | 0x1f << 0, |
| 601 | 0 << 6 | (div - 1) << 0), |
| 602 | &cru_ptr->cru_clksel_con[31]); |
| 603 | break; |
| 604 | |
| 605 | case 1: |
| 606 | writel(RK_CLRSETBITS(3 << 14 | 0x1f << 8, |
| 607 | 0 << 14 | (div - 1) << 8), |
| 608 | &cru_ptr->cru_clksel_con[31]); |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | |
huang lin | 40f558e | 2014-09-19 14:51:52 +0800 | [diff] [blame] | 613 | int rkclk_configure_vop_dclk(u32 vop_id, u32 dclk_hz) |
| 614 | { |
| 615 | struct pll_div npll_config = {0}; |
| 616 | |
| 617 | if (pll_para_config(dclk_hz, &npll_config)) |
| 618 | return -1; |
| 619 | |
| 620 | /* npll enter slow-mode */ |
| 621 | writel(RK_CLRSETBITS(NPLL_MODE_MSK, NPLL_MODE_SLOW), |
| 622 | &cru_ptr->cru_mode_con); |
| 623 | |
| 624 | rkclk_set_pll(&cru_ptr->cru_npll_con[0], &npll_config); |
| 625 | |
| 626 | /* waiting for pll lock */ |
| 627 | while (1) { |
| 628 | if (readl(&rk3288_grf->soc_status[1]) & SOCSTS_NPLL_LOCK) |
| 629 | break; |
| 630 | udelay(1); |
| 631 | } |
| 632 | |
| 633 | /* npll enter normal-mode */ |
| 634 | writel(RK_CLRSETBITS(NPLL_MODE_MSK, NPLL_MODE_NORM), |
| 635 | &cru_ptr->cru_mode_con); |
| 636 | |
| 637 | /* vop dclk source clk: npll,dclk_div: 1 */ |
| 638 | switch (vop_id) { |
| 639 | case 0: |
| 640 | writel(RK_CLRSETBITS(0xff << 8 | 3 << 0, |
| 641 | 0 << 8 | 2 << 0), |
| 642 | &cru_ptr->cru_clksel_con[27]); |
| 643 | break; |
| 644 | |
| 645 | case 1: |
| 646 | writel(RK_CLRSETBITS(0xff << 8 | 3 << 6, |
| 647 | 0 << 8 | 2 << 6), |
| 648 | &cru_ptr->cru_clksel_con[29]); |
| 649 | break; |
| 650 | } |
| 651 | return 0; |
| 652 | } |
Julius Werner | 2460a55 | 2014-11-24 13:50:46 -0800 | [diff] [blame] | 653 | |
| 654 | int rkclk_was_watchdog_reset(void) |
| 655 | { |
| 656 | /* Bits 5 and 4 are "second" and "first" global watchdog reset. */ |
| 657 | return readl(&cru_ptr->cru_glb_rst_st) & 0x30; |
| 658 | } |