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