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