Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
David Wu | ef4cf5a | 2017-09-20 14:28:19 +0800 | [diff] [blame] | 7 | #include <bitfield.h> |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 8 | #include <clk-uclass.h> |
Simon Glass | 3dbfe5a | 2018-12-27 20:15:20 -0700 | [diff] [blame] | 9 | #include <div64.h> |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 2d143bd | 2016-07-04 11:58:29 -0600 | [diff] [blame] | 11 | #include <dt-structs.h> |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 2d143bd | 2016-07-04 11:58:29 -0600 | [diff] [blame] | 14 | #include <mapmem.h> |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 15 | #include <syscon.h> |
| 16 | #include <asm/io.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 17 | #include <asm/arch-rockchip/clock.h> |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 18 | #include <asm/arch-rockchip/cru.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 19 | #include <asm/arch-rockchip/grf_rk3288.h> |
| 20 | #include <asm/arch-rockchip/hardware.h> |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 21 | #include <dt-bindings/clock/rk3288-cru.h> |
Simon Glass | 64b7faa | 2016-01-21 19:43:41 -0700 | [diff] [blame] | 22 | #include <dm/device-internal.h> |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 23 | #include <dm/lists.h> |
Simon Glass | 64b7faa | 2016-01-21 19:43:41 -0700 | [diff] [blame] | 24 | #include <dm/uclass-internal.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 25 | #include <linux/err.h> |
Heiko Stübner | abd0128 | 2016-07-22 23:51:06 +0200 | [diff] [blame] | 26 | #include <linux/log2.h> |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 27 | |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Simon Glass | 2d143bd | 2016-07-04 11:58:29 -0600 | [diff] [blame] | 30 | struct rk3288_clk_plat { |
| 31 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 32 | struct dtd_rockchip_rk3288_cru dtd; |
| 33 | #endif |
| 34 | }; |
| 35 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 36 | struct pll_div { |
| 37 | u32 nr; |
| 38 | u32 nf; |
| 39 | u32 no; |
| 40 | }; |
| 41 | |
| 42 | enum { |
| 43 | VCO_MAX_HZ = 2200U * 1000000, |
| 44 | VCO_MIN_HZ = 440 * 1000000, |
| 45 | OUTPUT_MAX_HZ = 2200U * 1000000, |
| 46 | OUTPUT_MIN_HZ = 27500000, |
| 47 | FREF_MAX_HZ = 2200U * 1000000, |
Heiko Stübner | c3f03ff | 2016-07-16 00:17:17 +0200 | [diff] [blame] | 48 | FREF_MIN_HZ = 269 * 1000, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | enum { |
| 52 | /* PLL CON0 */ |
| 53 | PLL_OD_MASK = 0x0f, |
| 54 | |
| 55 | /* PLL CON1 */ |
| 56 | PLL_NF_MASK = 0x1fff, |
| 57 | |
| 58 | /* PLL CON2 */ |
| 59 | PLL_BWADJ_MASK = 0x0fff, |
| 60 | |
| 61 | /* PLL CON3 */ |
| 62 | PLL_RESET_SHIFT = 5, |
| 63 | |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 64 | /* CLKSEL0 */ |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 65 | CORE_SEL_PLL_SHIFT = 15, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 66 | CORE_SEL_PLL_MASK = 1 << CORE_SEL_PLL_SHIFT, |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 67 | A17_DIV_SHIFT = 8, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 68 | A17_DIV_MASK = 0x1f << A17_DIV_SHIFT, |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 69 | MP_DIV_SHIFT = 4, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 70 | MP_DIV_MASK = 0xf << MP_DIV_SHIFT, |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 71 | M0_DIV_SHIFT = 0, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 72 | M0_DIV_MASK = 0xf << M0_DIV_SHIFT, |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 73 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 74 | /* CLKSEL1: pd bus clk pll sel: codec or general */ |
| 75 | PD_BUS_SEL_PLL_MASK = 15, |
| 76 | PD_BUS_SEL_CPLL = 0, |
| 77 | PD_BUS_SEL_GPLL, |
| 78 | |
| 79 | /* pd bus pclk div: pclk = pd_bus_aclk /(div + 1) */ |
| 80 | PD_BUS_PCLK_DIV_SHIFT = 12, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 81 | PD_BUS_PCLK_DIV_MASK = 7 << PD_BUS_PCLK_DIV_SHIFT, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 82 | |
| 83 | /* pd bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */ |
| 84 | PD_BUS_HCLK_DIV_SHIFT = 8, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 85 | PD_BUS_HCLK_DIV_MASK = 3 << PD_BUS_HCLK_DIV_SHIFT, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 86 | |
| 87 | /* pd bus aclk div: pd_bus_aclk = pd_bus_src_clk /(div0 * div1) */ |
| 88 | PD_BUS_ACLK_DIV0_SHIFT = 3, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 89 | PD_BUS_ACLK_DIV0_MASK = 0x1f << PD_BUS_ACLK_DIV0_SHIFT, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 90 | PD_BUS_ACLK_DIV1_SHIFT = 0, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 91 | PD_BUS_ACLK_DIV1_MASK = 0x7 << PD_BUS_ACLK_DIV1_SHIFT, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * CLKSEL10 |
| 95 | * peripheral bus pclk div: |
| 96 | * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1 |
| 97 | */ |
Simon Glass | c87c129 | 2016-01-21 19:45:15 -0700 | [diff] [blame] | 98 | PERI_SEL_PLL_SHIFT = 15, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 99 | PERI_SEL_PLL_MASK = 1 << PERI_SEL_PLL_SHIFT, |
Simon Glass | c87c129 | 2016-01-21 19:45:15 -0700 | [diff] [blame] | 100 | PERI_SEL_CPLL = 0, |
| 101 | PERI_SEL_GPLL, |
| 102 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 103 | PERI_PCLK_DIV_SHIFT = 12, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 104 | PERI_PCLK_DIV_MASK = 3 << PERI_PCLK_DIV_SHIFT, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 105 | |
| 106 | /* peripheral bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */ |
| 107 | PERI_HCLK_DIV_SHIFT = 8, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 108 | PERI_HCLK_DIV_MASK = 3 << PERI_HCLK_DIV_SHIFT, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * peripheral bus aclk div: |
| 112 | * aclk_periph = periph_clk_src / (peri_aclk_div_con + 1) |
| 113 | */ |
| 114 | PERI_ACLK_DIV_SHIFT = 0, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 115 | PERI_ACLK_DIV_MASK = 0x1f << PERI_ACLK_DIV_SHIFT, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 116 | |
David Wu | ef4cf5a | 2017-09-20 14:28:19 +0800 | [diff] [blame] | 117 | /* |
| 118 | * CLKSEL24 |
| 119 | * saradc_div_con: |
| 120 | * clk_saradc=24MHz/(saradc_div_con+1) |
| 121 | */ |
| 122 | CLK_SARADC_DIV_CON_SHIFT = 8, |
| 123 | CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8), |
| 124 | CLK_SARADC_DIV_CON_WIDTH = 8, |
| 125 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 126 | SOCSTS_DPLL_LOCK = 1 << 5, |
| 127 | SOCSTS_APLL_LOCK = 1 << 6, |
| 128 | SOCSTS_CPLL_LOCK = 1 << 7, |
| 129 | SOCSTS_GPLL_LOCK = 1 << 8, |
| 130 | SOCSTS_NPLL_LOCK = 1 << 9, |
| 131 | }; |
| 132 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 133 | #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1)) |
| 134 | |
| 135 | #define PLL_DIVISORS(hz, _nr, _no) {\ |
| 136 | .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\ |
| 137 | _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\ |
| 138 | (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\ |
| 139 | "divisors on line " __stringify(__LINE__)); |
| 140 | |
| 141 | /* Keep divisors as low as possible to reduce jitter and power usage */ |
| 142 | static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1); |
| 143 | static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2); |
| 144 | static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2); |
| 145 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 146 | static int rkclk_set_pll(struct rockchip_cru *cru, enum rk_clk_id clk_id, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 147 | const struct pll_div *div) |
| 148 | { |
| 149 | int pll_id = rk_pll_id(clk_id); |
| 150 | struct rk3288_pll *pll = &cru->pll[pll_id]; |
| 151 | /* All PLLs have same VCO and output frequency range restrictions. */ |
| 152 | uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000; |
| 153 | uint output_hz = vco_hz / div->no; |
| 154 | |
Simon Glass | c87c129 | 2016-01-21 19:45:15 -0700 | [diff] [blame] | 155 | debug("PLL at %x: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n", |
| 156 | (uint)pll, div->nf, div->nr, div->no, vco_hz, output_hz); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 157 | assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ && |
| 158 | output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ && |
| 159 | (div->no == 1 || !(div->no % 2))); |
| 160 | |
Simon Glass | c87c129 | 2016-01-21 19:45:15 -0700 | [diff] [blame] | 161 | /* enter reset */ |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 162 | rk_setreg(&pll->con3, 1 << PLL_RESET_SHIFT); |
| 163 | |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 164 | rk_clrsetreg(&pll->con0, CLKR_MASK | PLL_OD_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 165 | ((div->nr - 1) << CLKR_SHIFT) | (div->no - 1)); |
| 166 | rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1); |
| 167 | rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1); |
| 168 | |
| 169 | udelay(10); |
| 170 | |
Simon Glass | c87c129 | 2016-01-21 19:45:15 -0700 | [diff] [blame] | 171 | /* return from reset */ |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 172 | rk_clrreg(&pll->con3, 1 << PLL_RESET_SHIFT); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 177 | static int rkclk_configure_ddr(struct rockchip_cru *cru, struct rk3288_grf *grf, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 178 | unsigned int hz) |
| 179 | { |
| 180 | static const struct pll_div dpll_cfg[] = { |
| 181 | {.nf = 25, .nr = 2, .no = 1}, |
| 182 | {.nf = 400, .nr = 9, .no = 2}, |
| 183 | {.nf = 500, .nr = 9, .no = 2}, |
| 184 | {.nf = 100, .nr = 3, .no = 1}, |
| 185 | }; |
| 186 | int cfg; |
| 187 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 188 | switch (hz) { |
| 189 | case 300000000: |
| 190 | cfg = 0; |
| 191 | break; |
| 192 | case 533000000: /* actually 533.3P MHz */ |
| 193 | cfg = 1; |
| 194 | break; |
| 195 | case 666000000: /* actually 666.6P MHz */ |
| 196 | cfg = 2; |
| 197 | break; |
| 198 | case 800000000: |
| 199 | cfg = 3; |
| 200 | break; |
| 201 | default: |
Simon Glass | c87c129 | 2016-01-21 19:45:15 -0700 | [diff] [blame] | 202 | debug("Unsupported SDRAM frequency"); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 203 | return -EINVAL; |
| 204 | } |
| 205 | |
| 206 | /* pll enter slow-mode */ |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 207 | rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 208 | DPLL_MODE_SLOW << DPLL_MODE_SHIFT); |
| 209 | |
| 210 | rkclk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]); |
| 211 | |
| 212 | /* wait for pll lock */ |
| 213 | while (!(readl(&grf->soc_status[1]) & SOCSTS_DPLL_LOCK)) |
| 214 | udelay(1); |
| 215 | |
| 216 | /* PLL enter normal-mode */ |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 217 | rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK, |
Simon Glass | 009741f | 2016-01-21 19:45:01 -0700 | [diff] [blame] | 218 | DPLL_MODE_NORMAL << DPLL_MODE_SHIFT); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Simon Glass | 830a608 | 2016-01-21 19:45:02 -0700 | [diff] [blame] | 223 | #ifndef CONFIG_SPL_BUILD |
| 224 | #define VCO_MAX_KHZ 2200000 |
| 225 | #define VCO_MIN_KHZ 440000 |
| 226 | #define FREF_MAX_KHZ 2200000 |
| 227 | #define FREF_MIN_KHZ 269 |
| 228 | |
| 229 | static int pll_para_config(ulong freq_hz, struct pll_div *div, uint *ext_div) |
| 230 | { |
| 231 | uint ref_khz = OSC_HZ / 1000, nr, nf = 0; |
| 232 | uint fref_khz; |
| 233 | uint diff_khz, best_diff_khz; |
| 234 | const uint max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4; |
| 235 | uint vco_khz; |
| 236 | uint no = 1; |
| 237 | uint freq_khz = freq_hz / 1000; |
| 238 | |
| 239 | if (!freq_hz) { |
| 240 | printf("%s: the frequency can not be 0 Hz\n", __func__); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
| 244 | no = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz); |
| 245 | if (ext_div) { |
| 246 | *ext_div = DIV_ROUND_UP(no, max_no); |
| 247 | no = DIV_ROUND_UP(no, *ext_div); |
| 248 | } |
| 249 | |
| 250 | /* only even divisors (and 1) are supported */ |
| 251 | if (no > 1) |
| 252 | no = DIV_ROUND_UP(no, 2) * 2; |
| 253 | |
| 254 | vco_khz = freq_khz * no; |
| 255 | if (ext_div) |
| 256 | vco_khz *= *ext_div; |
| 257 | |
| 258 | if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) { |
| 259 | printf("%s: Cannot find out a supported VCO for Frequency (%luHz).\n", |
| 260 | __func__, freq_hz); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | div->no = no; |
| 265 | |
| 266 | best_diff_khz = vco_khz; |
| 267 | for (nr = 1; nr < max_nr && best_diff_khz; nr++) { |
| 268 | fref_khz = ref_khz / nr; |
| 269 | if (fref_khz < FREF_MIN_KHZ) |
| 270 | break; |
| 271 | if (fref_khz > FREF_MAX_KHZ) |
| 272 | continue; |
| 273 | |
| 274 | nf = vco_khz / fref_khz; |
| 275 | if (nf >= max_nf) |
| 276 | continue; |
| 277 | diff_khz = vco_khz - nf * fref_khz; |
| 278 | if (nf + 1 < max_nf && diff_khz > fref_khz / 2) { |
| 279 | nf++; |
| 280 | diff_khz = fref_khz - diff_khz; |
| 281 | } |
| 282 | |
| 283 | if (diff_khz >= best_diff_khz) |
| 284 | continue; |
| 285 | |
| 286 | best_diff_khz = diff_khz; |
| 287 | div->nr = nr; |
| 288 | div->nf = nf; |
| 289 | } |
| 290 | |
| 291 | if (best_diff_khz > 4 * 1000) { |
| 292 | printf("%s: Failed to match output frequency %lu, difference is %u Hz, exceed 4MHZ\n", |
| 293 | __func__, freq_hz, best_diff_khz * 1000); |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 300 | static int rockchip_mac_set_clk(struct rockchip_cru *cru, uint freq) |
Sjoerd Simons | 0aefc0b | 2016-02-28 22:24:59 +0100 | [diff] [blame] | 301 | { |
David Wu | 01c60ea | 2018-01-13 14:06:33 +0800 | [diff] [blame] | 302 | ulong ret; |
Sjoerd Simons | 0aefc0b | 2016-02-28 22:24:59 +0100 | [diff] [blame] | 303 | |
David Wu | 01c60ea | 2018-01-13 14:06:33 +0800 | [diff] [blame] | 304 | /* |
| 305 | * The gmac clock can be derived either from an external clock |
| 306 | * or can be generated from internally by a divider from SCLK_MAC. |
| 307 | */ |
| 308 | if (readl(&cru->cru_clksel_con[21]) & RMII_EXTCLK_MASK) { |
| 309 | /* An external clock will always generate the right rate... */ |
| 310 | ret = freq; |
| 311 | } else { |
| 312 | u32 con = readl(&cru->cru_clksel_con[21]); |
| 313 | ulong pll_rate; |
| 314 | u8 div; |
| 315 | |
| 316 | if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) == |
| 317 | EMAC_PLL_SELECT_GENERAL) |
| 318 | pll_rate = GPLL_HZ; |
| 319 | else if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) == |
| 320 | EMAC_PLL_SELECT_CODEC) |
| 321 | pll_rate = CPLL_HZ; |
| 322 | else |
| 323 | pll_rate = NPLL_HZ; |
| 324 | |
| 325 | div = DIV_ROUND_UP(pll_rate, freq) - 1; |
| 326 | if (div <= 0x1f) |
| 327 | rk_clrsetreg(&cru->cru_clksel_con[21], MAC_DIV_CON_MASK, |
| 328 | div << MAC_DIV_CON_SHIFT); |
| 329 | else |
| 330 | debug("Unsupported div for gmac:%d\n", div); |
| 331 | |
| 332 | return DIV_TO_RATE(pll_rate, div); |
| 333 | } |
| 334 | |
| 335 | return ret; |
Sjoerd Simons | 0aefc0b | 2016-02-28 22:24:59 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 338 | static int rockchip_vop_set_clk(struct rockchip_cru *cru, struct rk3288_grf *grf, |
Simon Glass | 830a608 | 2016-01-21 19:45:02 -0700 | [diff] [blame] | 339 | int periph, unsigned int rate_hz) |
| 340 | { |
| 341 | struct pll_div npll_config = {0}; |
| 342 | u32 lcdc_div; |
| 343 | int ret; |
| 344 | |
| 345 | ret = pll_para_config(rate_hz, &npll_config, &lcdc_div); |
| 346 | if (ret) |
| 347 | return ret; |
| 348 | |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 349 | rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK, |
Simon Glass | 830a608 | 2016-01-21 19:45:02 -0700 | [diff] [blame] | 350 | NPLL_MODE_SLOW << NPLL_MODE_SHIFT); |
| 351 | rkclk_set_pll(cru, CLK_NEW, &npll_config); |
| 352 | |
| 353 | /* waiting for pll lock */ |
| 354 | while (1) { |
| 355 | if (readl(&grf->soc_status[1]) & SOCSTS_NPLL_LOCK) |
| 356 | break; |
| 357 | udelay(1); |
| 358 | } |
| 359 | |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 360 | rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK, |
Simon Glass | 830a608 | 2016-01-21 19:45:02 -0700 | [diff] [blame] | 361 | NPLL_MODE_NORMAL << NPLL_MODE_SHIFT); |
| 362 | |
| 363 | /* vop dclk source clk: npll,dclk_div: 1 */ |
| 364 | switch (periph) { |
| 365 | case DCLK_VOP0: |
| 366 | rk_clrsetreg(&cru->cru_clksel_con[27], 0xff << 8 | 3 << 0, |
| 367 | (lcdc_div - 1) << 8 | 2 << 0); |
| 368 | break; |
| 369 | case DCLK_VOP1: |
| 370 | rk_clrsetreg(&cru->cru_clksel_con[29], 0xff << 8 | 3 << 6, |
| 371 | (lcdc_div - 1) << 8 | 2 << 6); |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | return 0; |
| 376 | } |
Simon Glass | 3dbfe5a | 2018-12-27 20:15:20 -0700 | [diff] [blame] | 377 | |
| 378 | static u32 rockchip_clk_gcd(u32 a, u32 b) |
| 379 | { |
| 380 | while (b != 0) { |
| 381 | int r = b; |
| 382 | |
| 383 | b = a % b; |
| 384 | a = r; |
| 385 | } |
| 386 | return a; |
| 387 | } |
| 388 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 389 | static ulong rockchip_i2s_get_clk(struct rockchip_cru *cru, uint gclk_rate) |
Simon Glass | 3dbfe5a | 2018-12-27 20:15:20 -0700 | [diff] [blame] | 390 | { |
| 391 | unsigned long long rate; |
| 392 | uint val; |
| 393 | int n, d; |
| 394 | |
| 395 | val = readl(&cru->cru_clksel_con[8]); |
| 396 | n = (val & I2S0_FRAC_NUMER_MASK) >> I2S0_FRAC_NUMER_SHIFT; |
| 397 | d = (val & I2S0_FRAC_DENOM_MASK) >> I2S0_FRAC_DENOM_SHIFT; |
| 398 | |
| 399 | rate = (unsigned long long)gclk_rate * n; |
| 400 | do_div(rate, d); |
| 401 | |
| 402 | return (ulong)rate; |
| 403 | } |
| 404 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 405 | static ulong rockchip_i2s_set_clk(struct rockchip_cru *cru, uint gclk_rate, |
Simon Glass | 3dbfe5a | 2018-12-27 20:15:20 -0700 | [diff] [blame] | 406 | uint freq) |
| 407 | { |
| 408 | int n, d; |
| 409 | int v; |
| 410 | |
| 411 | /* set frac divider */ |
| 412 | v = rockchip_clk_gcd(gclk_rate, freq); |
| 413 | n = gclk_rate / v; |
| 414 | d = freq / v; |
| 415 | assert(freq == gclk_rate / n * d); |
| 416 | writel(d << I2S0_FRAC_NUMER_SHIFT | n << I2S0_FRAC_DENOM_SHIFT, |
| 417 | &cru->cru_clksel_con[8]); |
| 418 | |
| 419 | return rockchip_i2s_get_clk(cru, gclk_rate); |
| 420 | } |
Simon Glass | d3cb46a | 2017-05-31 17:57:32 -0600 | [diff] [blame] | 421 | #endif /* CONFIG_SPL_BUILD */ |
Simon Glass | 830a608 | 2016-01-21 19:45:02 -0700 | [diff] [blame] | 422 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 423 | static void rkclk_init(struct rockchip_cru *cru, struct rk3288_grf *grf) |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 424 | { |
| 425 | u32 aclk_div; |
| 426 | u32 hclk_div; |
| 427 | u32 pclk_div; |
| 428 | |
| 429 | /* pll enter slow-mode */ |
| 430 | rk_clrsetreg(&cru->cru_mode_con, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 431 | GPLL_MODE_MASK | CPLL_MODE_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 432 | GPLL_MODE_SLOW << GPLL_MODE_SHIFT | |
| 433 | CPLL_MODE_SLOW << CPLL_MODE_SHIFT); |
| 434 | |
| 435 | /* init pll */ |
| 436 | rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg); |
| 437 | rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg); |
| 438 | |
| 439 | /* waiting for pll lock */ |
| 440 | while ((readl(&grf->soc_status[1]) & |
| 441 | (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) != |
| 442 | (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) |
| 443 | udelay(1); |
| 444 | |
| 445 | /* |
| 446 | * pd_bus clock pll source selection and |
| 447 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 448 | */ |
| 449 | aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1; |
| 450 | assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 451 | hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1; |
| 452 | assert((hclk_div + 1) * PD_BUS_HCLK_HZ == |
| 453 | PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2)); |
| 454 | |
| 455 | pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1; |
| 456 | assert((pclk_div + 1) * PD_BUS_PCLK_HZ == |
| 457 | PD_BUS_ACLK_HZ && pclk_div < 0x7); |
| 458 | |
| 459 | rk_clrsetreg(&cru->cru_clksel_con[1], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 460 | PD_BUS_PCLK_DIV_MASK | PD_BUS_HCLK_DIV_MASK | |
| 461 | PD_BUS_ACLK_DIV0_MASK | PD_BUS_ACLK_DIV1_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 462 | pclk_div << PD_BUS_PCLK_DIV_SHIFT | |
| 463 | hclk_div << PD_BUS_HCLK_DIV_SHIFT | |
| 464 | aclk_div << PD_BUS_ACLK_DIV0_SHIFT | |
| 465 | 0 << 0); |
| 466 | |
| 467 | /* |
| 468 | * peri clock pll source selection and |
| 469 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 470 | */ |
| 471 | aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1; |
| 472 | assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 473 | |
Heiko Stübner | abd0128 | 2016-07-22 23:51:06 +0200 | [diff] [blame] | 474 | hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 475 | assert((1 << hclk_div) * PERI_HCLK_HZ == |
| 476 | PERI_ACLK_HZ && (hclk_div < 0x4)); |
| 477 | |
Heiko Stübner | abd0128 | 2016-07-22 23:51:06 +0200 | [diff] [blame] | 478 | pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 479 | assert((1 << pclk_div) * PERI_PCLK_HZ == |
| 480 | PERI_ACLK_HZ && (pclk_div < 0x4)); |
| 481 | |
| 482 | rk_clrsetreg(&cru->cru_clksel_con[10], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 483 | PERI_PCLK_DIV_MASK | PERI_HCLK_DIV_MASK | |
| 484 | PERI_ACLK_DIV_MASK, |
Simon Glass | c87c129 | 2016-01-21 19:45:15 -0700 | [diff] [blame] | 485 | PERI_SEL_GPLL << PERI_SEL_PLL_SHIFT | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 486 | pclk_div << PERI_PCLK_DIV_SHIFT | |
| 487 | hclk_div << PERI_HCLK_DIV_SHIFT | |
| 488 | aclk_div << PERI_ACLK_DIV_SHIFT); |
| 489 | |
| 490 | /* PLL enter normal-mode */ |
| 491 | rk_clrsetreg(&cru->cru_mode_con, |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 492 | GPLL_MODE_MASK | CPLL_MODE_MASK, |
Simon Glass | 009741f | 2016-01-21 19:45:01 -0700 | [diff] [blame] | 493 | GPLL_MODE_NORMAL << GPLL_MODE_SHIFT | |
| 494 | CPLL_MODE_NORMAL << CPLL_MODE_SHIFT); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 495 | } |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 496 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 497 | void rk3288_clk_configure_cpu(struct rockchip_cru *cru, struct rk3288_grf *grf) |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 498 | { |
| 499 | /* pll enter slow-mode */ |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 500 | rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK, |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 501 | APLL_MODE_SLOW << APLL_MODE_SHIFT); |
| 502 | |
| 503 | rkclk_set_pll(cru, CLK_ARM, &apll_init_cfg); |
| 504 | |
| 505 | /* waiting for pll lock */ |
| 506 | while (!(readl(&grf->soc_status[1]) & SOCSTS_APLL_LOCK)) |
| 507 | udelay(1); |
| 508 | |
| 509 | /* |
| 510 | * core clock pll source selection and |
| 511 | * set up dependent divisors for MPAXI/M0AXI and ARM clocks. |
| 512 | * core clock select apll, apll clk = 1800MHz |
| 513 | * arm clk = 1800MHz, mpclk = 450MHz, m0clk = 900MHz |
| 514 | */ |
| 515 | rk_clrsetreg(&cru->cru_clksel_con[0], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 516 | CORE_SEL_PLL_MASK | A17_DIV_MASK | MP_DIV_MASK | |
| 517 | M0_DIV_MASK, |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 518 | 0 << A17_DIV_SHIFT | |
| 519 | 3 << MP_DIV_SHIFT | |
| 520 | 1 << M0_DIV_SHIFT); |
| 521 | |
| 522 | /* |
| 523 | * set up dependent divisors for L2RAM/ATCLK and PCLK clocks. |
| 524 | * l2ramclk = 900MHz, atclk = 450MHz, pclk_dbg = 450MHz |
| 525 | */ |
| 526 | rk_clrsetreg(&cru->cru_clksel_con[37], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 527 | CLK_L2RAM_DIV_MASK | ATCLK_CORE_DIV_CON_MASK | |
| 528 | PCLK_CORE_DBG_DIV_MASK, |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 529 | 1 << CLK_L2RAM_DIV_SHIFT | |
| 530 | 3 << ATCLK_CORE_DIV_CON_SHIFT | |
| 531 | 3 << PCLK_CORE_DBG_DIV_SHIFT); |
| 532 | |
| 533 | /* PLL enter normal-mode */ |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 534 | rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK, |
Simon Glass | dae594f | 2016-01-21 19:45:17 -0700 | [diff] [blame] | 535 | APLL_MODE_NORMAL << APLL_MODE_SHIFT); |
| 536 | } |
| 537 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 538 | /* Get pll rate by id */ |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 539 | static uint32_t rkclk_pll_get_rate(struct rockchip_cru *cru, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 540 | enum rk_clk_id clk_id) |
| 541 | { |
| 542 | uint32_t nr, no, nf; |
| 543 | uint32_t con; |
| 544 | int pll_id = rk_pll_id(clk_id); |
| 545 | struct rk3288_pll *pll = &cru->pll[pll_id]; |
| 546 | static u8 clk_shift[CLK_COUNT] = { |
Simon Glass | 009741f | 2016-01-21 19:45:01 -0700 | [diff] [blame] | 547 | 0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT, |
| 548 | GPLL_MODE_SHIFT, NPLL_MODE_SHIFT |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 549 | }; |
| 550 | uint shift; |
| 551 | |
| 552 | con = readl(&cru->cru_mode_con); |
| 553 | shift = clk_shift[clk_id]; |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 554 | switch ((con >> shift) & CRU_MODE_MASK) { |
Simon Glass | 009741f | 2016-01-21 19:45:01 -0700 | [diff] [blame] | 555 | case APLL_MODE_SLOW: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 556 | return OSC_HZ; |
Simon Glass | 009741f | 2016-01-21 19:45:01 -0700 | [diff] [blame] | 557 | case APLL_MODE_NORMAL: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 558 | /* normal mode */ |
| 559 | con = readl(&pll->con0); |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 560 | no = ((con & CLKOD_MASK) >> CLKOD_SHIFT) + 1; |
| 561 | nr = ((con & CLKR_MASK) >> CLKR_SHIFT) + 1; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 562 | con = readl(&pll->con1); |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 563 | nf = ((con & CLKF_MASK) >> CLKF_SHIFT) + 1; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 564 | |
| 565 | return (24 * nf / (nr * no)) * 1000000; |
Simon Glass | 009741f | 2016-01-21 19:45:01 -0700 | [diff] [blame] | 566 | case APLL_MODE_DEEP: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 567 | default: |
| 568 | return 32768; |
| 569 | } |
| 570 | } |
| 571 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 572 | static ulong rockchip_mmc_get_clk(struct rockchip_cru *cru, uint gclk_rate, |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 573 | int periph) |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 574 | { |
| 575 | uint src_rate; |
| 576 | uint div, mux; |
| 577 | u32 con; |
| 578 | |
| 579 | switch (periph) { |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 580 | case HCLK_EMMC: |
Xu Ziyuan | 4511227 | 2017-04-16 17:44:45 +0800 | [diff] [blame] | 581 | case SCLK_EMMC: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 582 | con = readl(&cru->cru_clksel_con[12]); |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 583 | mux = (con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT; |
| 584 | div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 585 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 586 | case HCLK_SDMMC: |
Xu Ziyuan | 4511227 | 2017-04-16 17:44:45 +0800 | [diff] [blame] | 587 | case SCLK_SDMMC: |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 588 | con = readl(&cru->cru_clksel_con[11]); |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 589 | mux = (con & MMC0_PLL_MASK) >> MMC0_PLL_SHIFT; |
| 590 | div = (con & MMC0_DIV_MASK) >> MMC0_DIV_SHIFT; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 591 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 592 | case HCLK_SDIO0: |
Xu Ziyuan | 4511227 | 2017-04-16 17:44:45 +0800 | [diff] [blame] | 593 | case SCLK_SDIO0: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 594 | con = readl(&cru->cru_clksel_con[12]); |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 595 | mux = (con & SDIO0_PLL_MASK) >> SDIO0_PLL_SHIFT; |
| 596 | div = (con & SDIO0_DIV_MASK) >> SDIO0_DIV_SHIFT; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 597 | break; |
| 598 | default: |
| 599 | return -EINVAL; |
| 600 | } |
| 601 | |
Simon Glass | 542635a | 2016-01-21 19:43:39 -0700 | [diff] [blame] | 602 | src_rate = mux == EMMC_PLL_SELECT_24MHZ ? OSC_HZ : gclk_rate; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 603 | return DIV_TO_RATE(src_rate, div); |
| 604 | } |
| 605 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 606 | static ulong rockchip_mmc_set_clk(struct rockchip_cru *cru, uint gclk_rate, |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 607 | int periph, uint freq) |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 608 | { |
| 609 | int src_clk_div; |
| 610 | int mux; |
| 611 | |
Simon Glass | 542635a | 2016-01-21 19:43:39 -0700 | [diff] [blame] | 612 | debug("%s: gclk_rate=%u\n", __func__, gclk_rate); |
Kever Yang | 3a94d75 | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 613 | /* mmc clock default div 2 internal, need provide double in cru */ |
| 614 | src_clk_div = DIV_ROUND_UP(gclk_rate / 2, freq); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 615 | |
| 616 | if (src_clk_div > 0x3f) { |
Kever Yang | 3a94d75 | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 617 | src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, freq); |
Kever Yang | 217273c | 2017-07-27 12:54:02 +0800 | [diff] [blame] | 618 | assert(src_clk_div < 0x40); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 619 | mux = EMMC_PLL_SELECT_24MHZ; |
| 620 | assert((int)EMMC_PLL_SELECT_24MHZ == |
| 621 | (int)MMC0_PLL_SELECT_24MHZ); |
| 622 | } else { |
| 623 | mux = EMMC_PLL_SELECT_GENERAL; |
| 624 | assert((int)EMMC_PLL_SELECT_GENERAL == |
| 625 | (int)MMC0_PLL_SELECT_GENERAL); |
| 626 | } |
| 627 | switch (periph) { |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 628 | case HCLK_EMMC: |
Xu Ziyuan | 4511227 | 2017-04-16 17:44:45 +0800 | [diff] [blame] | 629 | case SCLK_EMMC: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 630 | rk_clrsetreg(&cru->cru_clksel_con[12], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 631 | EMMC_PLL_MASK | EMMC_DIV_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 632 | mux << EMMC_PLL_SHIFT | |
| 633 | (src_clk_div - 1) << EMMC_DIV_SHIFT); |
| 634 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 635 | case HCLK_SDMMC: |
Xu Ziyuan | 4511227 | 2017-04-16 17:44:45 +0800 | [diff] [blame] | 636 | case SCLK_SDMMC: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 637 | rk_clrsetreg(&cru->cru_clksel_con[11], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 638 | MMC0_PLL_MASK | MMC0_DIV_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 639 | mux << MMC0_PLL_SHIFT | |
| 640 | (src_clk_div - 1) << MMC0_DIV_SHIFT); |
| 641 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 642 | case HCLK_SDIO0: |
Xu Ziyuan | 4511227 | 2017-04-16 17:44:45 +0800 | [diff] [blame] | 643 | case SCLK_SDIO0: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 644 | rk_clrsetreg(&cru->cru_clksel_con[12], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 645 | SDIO0_PLL_MASK | SDIO0_DIV_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 646 | mux << SDIO0_PLL_SHIFT | |
| 647 | (src_clk_div - 1) << SDIO0_DIV_SHIFT); |
| 648 | break; |
| 649 | default: |
| 650 | return -EINVAL; |
| 651 | } |
| 652 | |
Simon Glass | 542635a | 2016-01-21 19:43:39 -0700 | [diff] [blame] | 653 | return rockchip_mmc_get_clk(cru, gclk_rate, periph); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 654 | } |
| 655 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 656 | static ulong rockchip_spi_get_clk(struct rockchip_cru *cru, uint gclk_rate, |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 657 | int periph) |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 658 | { |
| 659 | uint div, mux; |
| 660 | u32 con; |
| 661 | |
| 662 | switch (periph) { |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 663 | case SCLK_SPI0: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 664 | con = readl(&cru->cru_clksel_con[25]); |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 665 | mux = (con & SPI0_PLL_MASK) >> SPI0_PLL_SHIFT; |
| 666 | div = (con & SPI0_DIV_MASK) >> SPI0_DIV_SHIFT; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 667 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 668 | case SCLK_SPI1: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 669 | con = readl(&cru->cru_clksel_con[25]); |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 670 | mux = (con & SPI1_PLL_MASK) >> SPI1_PLL_SHIFT; |
| 671 | div = (con & SPI1_DIV_MASK) >> SPI1_DIV_SHIFT; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 672 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 673 | case SCLK_SPI2: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 674 | con = readl(&cru->cru_clksel_con[39]); |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 675 | mux = (con & SPI2_PLL_MASK) >> SPI2_PLL_SHIFT; |
| 676 | div = (con & SPI2_DIV_MASK) >> SPI2_DIV_SHIFT; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 677 | break; |
| 678 | default: |
| 679 | return -EINVAL; |
| 680 | } |
| 681 | assert(mux == SPI0_PLL_SELECT_GENERAL); |
| 682 | |
Simon Glass | 542635a | 2016-01-21 19:43:39 -0700 | [diff] [blame] | 683 | return DIV_TO_RATE(gclk_rate, div); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 684 | } |
| 685 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 686 | static ulong rockchip_spi_set_clk(struct rockchip_cru *cru, uint gclk_rate, |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 687 | int periph, uint freq) |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 688 | { |
| 689 | int src_clk_div; |
| 690 | |
Simon Glass | 542635a | 2016-01-21 19:43:39 -0700 | [diff] [blame] | 691 | debug("%s: clk_general_rate=%u\n", __func__, gclk_rate); |
Kever Yang | 217273c | 2017-07-27 12:54:02 +0800 | [diff] [blame] | 692 | src_clk_div = DIV_ROUND_UP(gclk_rate, freq) - 1; |
| 693 | assert(src_clk_div < 128); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 694 | switch (periph) { |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 695 | case SCLK_SPI0: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 696 | rk_clrsetreg(&cru->cru_clksel_con[25], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 697 | SPI0_PLL_MASK | SPI0_DIV_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 698 | SPI0_PLL_SELECT_GENERAL << SPI0_PLL_SHIFT | |
| 699 | src_clk_div << SPI0_DIV_SHIFT); |
| 700 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 701 | case SCLK_SPI1: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 702 | rk_clrsetreg(&cru->cru_clksel_con[25], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 703 | SPI1_PLL_MASK | SPI1_DIV_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 704 | SPI1_PLL_SELECT_GENERAL << SPI1_PLL_SHIFT | |
| 705 | src_clk_div << SPI1_DIV_SHIFT); |
| 706 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 707 | case SCLK_SPI2: |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 708 | rk_clrsetreg(&cru->cru_clksel_con[39], |
Simon Glass | b223c1a | 2017-05-31 17:57:31 -0600 | [diff] [blame] | 709 | SPI2_PLL_MASK | SPI2_DIV_MASK, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 710 | SPI2_PLL_SELECT_GENERAL << SPI2_PLL_SHIFT | |
| 711 | src_clk_div << SPI2_DIV_SHIFT); |
| 712 | break; |
| 713 | default: |
| 714 | return -EINVAL; |
| 715 | } |
| 716 | |
Simon Glass | 542635a | 2016-01-21 19:43:39 -0700 | [diff] [blame] | 717 | return rockchip_spi_get_clk(cru, gclk_rate, periph); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 718 | } |
| 719 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 720 | static ulong rockchip_saradc_get_clk(struct rockchip_cru *cru) |
David Wu | ef4cf5a | 2017-09-20 14:28:19 +0800 | [diff] [blame] | 721 | { |
| 722 | u32 div, val; |
| 723 | |
| 724 | val = readl(&cru->cru_clksel_con[24]); |
| 725 | div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT, |
| 726 | CLK_SARADC_DIV_CON_WIDTH); |
| 727 | |
| 728 | return DIV_TO_RATE(OSC_HZ, div); |
| 729 | } |
| 730 | |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 731 | static ulong rockchip_saradc_set_clk(struct rockchip_cru *cru, uint hz) |
David Wu | ef4cf5a | 2017-09-20 14:28:19 +0800 | [diff] [blame] | 732 | { |
| 733 | int src_clk_div; |
| 734 | |
| 735 | src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1; |
| 736 | assert(src_clk_div < 128); |
| 737 | |
| 738 | rk_clrsetreg(&cru->cru_clksel_con[24], |
| 739 | CLK_SARADC_DIV_CON_MASK, |
| 740 | src_clk_div << CLK_SARADC_DIV_CON_SHIFT); |
| 741 | |
| 742 | return rockchip_saradc_get_clk(cru); |
| 743 | } |
| 744 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 745 | static ulong rk3288_clk_get_rate(struct clk *clk) |
Simon Glass | 4f43673 | 2016-01-21 19:43:40 -0700 | [diff] [blame] | 746 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 747 | struct rk3288_clk_priv *priv = dev_get_priv(clk->dev); |
Simon Glass | 4f43673 | 2016-01-21 19:43:40 -0700 | [diff] [blame] | 748 | ulong new_rate, gclk_rate; |
Simon Glass | 4f43673 | 2016-01-21 19:43:40 -0700 | [diff] [blame] | 749 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 750 | gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL); |
| 751 | switch (clk->id) { |
| 752 | case 0 ... 63: |
| 753 | new_rate = rkclk_pll_get_rate(priv->cru, clk->id); |
| 754 | break; |
Simon Glass | 4f43673 | 2016-01-21 19:43:40 -0700 | [diff] [blame] | 755 | case HCLK_EMMC: |
Simon Glass | 342999f | 2016-01-21 19:43:45 -0700 | [diff] [blame] | 756 | case HCLK_SDMMC: |
Simon Glass | 4f43673 | 2016-01-21 19:43:40 -0700 | [diff] [blame] | 757 | case HCLK_SDIO0: |
Xu Ziyuan | 4511227 | 2017-04-16 17:44:45 +0800 | [diff] [blame] | 758 | case SCLK_EMMC: |
| 759 | case SCLK_SDMMC: |
| 760 | case SCLK_SDIO0: |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 761 | new_rate = rockchip_mmc_get_clk(priv->cru, gclk_rate, clk->id); |
Simon Glass | 4f43673 | 2016-01-21 19:43:40 -0700 | [diff] [blame] | 762 | break; |
| 763 | case SCLK_SPI0: |
| 764 | case SCLK_SPI1: |
| 765 | case SCLK_SPI2: |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 766 | new_rate = rockchip_spi_get_clk(priv->cru, gclk_rate, clk->id); |
Simon Glass | 4f43673 | 2016-01-21 19:43:40 -0700 | [diff] [blame] | 767 | break; |
| 768 | case PCLK_I2C0: |
| 769 | case PCLK_I2C1: |
| 770 | case PCLK_I2C2: |
| 771 | case PCLK_I2C3: |
| 772 | case PCLK_I2C4: |
| 773 | case PCLK_I2C5: |
| 774 | return gclk_rate; |
Kever Yang | 4f0b8ef | 2016-08-12 17:57:05 +0800 | [diff] [blame] | 775 | case PCLK_PWM: |
| 776 | return PD_BUS_PCLK_HZ; |
David Wu | ef4cf5a | 2017-09-20 14:28:19 +0800 | [diff] [blame] | 777 | case SCLK_SARADC: |
| 778 | new_rate = rockchip_saradc_get_clk(priv->cru); |
| 779 | break; |
Simon Glass | 4f43673 | 2016-01-21 19:43:40 -0700 | [diff] [blame] | 780 | default: |
| 781 | return -ENOENT; |
| 782 | } |
| 783 | |
| 784 | return new_rate; |
| 785 | } |
| 786 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 787 | static ulong rk3288_clk_set_rate(struct clk *clk, ulong rate) |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 788 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 789 | struct rk3288_clk_priv *priv = dev_get_priv(clk->dev); |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 790 | struct rockchip_cru *cru = priv->cru; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 791 | ulong new_rate, gclk_rate; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 792 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 793 | gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL); |
| 794 | switch (clk->id) { |
Simon Glass | 3a8a42d | 2016-11-13 14:22:13 -0700 | [diff] [blame] | 795 | case PLL_APLL: |
| 796 | /* We only support a fixed rate here */ |
| 797 | if (rate != 1800000000) |
| 798 | return -EINVAL; |
| 799 | rk3288_clk_configure_cpu(priv->cru, priv->grf); |
| 800 | new_rate = rate; |
| 801 | break; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 802 | case CLK_DDR: |
| 803 | new_rate = rkclk_configure_ddr(priv->cru, priv->grf, rate); |
| 804 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 805 | case HCLK_EMMC: |
| 806 | case HCLK_SDMMC: |
| 807 | case HCLK_SDIO0: |
Xu Ziyuan | 4511227 | 2017-04-16 17:44:45 +0800 | [diff] [blame] | 808 | case SCLK_EMMC: |
| 809 | case SCLK_SDMMC: |
| 810 | case SCLK_SDIO0: |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 811 | new_rate = rockchip_mmc_set_clk(cru, gclk_rate, clk->id, rate); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 812 | break; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 813 | case SCLK_SPI0: |
| 814 | case SCLK_SPI1: |
| 815 | case SCLK_SPI2: |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 816 | new_rate = rockchip_spi_set_clk(cru, gclk_rate, clk->id, rate); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 817 | break; |
Simon Glass | 830a608 | 2016-01-21 19:45:02 -0700 | [diff] [blame] | 818 | #ifndef CONFIG_SPL_BUILD |
Simon Glass | 3dbfe5a | 2018-12-27 20:15:20 -0700 | [diff] [blame] | 819 | case SCLK_I2S0: |
| 820 | new_rate = rockchip_i2s_set_clk(cru, gclk_rate, rate); |
| 821 | break; |
Sjoerd Simons | 0aefc0b | 2016-02-28 22:24:59 +0100 | [diff] [blame] | 822 | case SCLK_MAC: |
David Wu | 01c60ea | 2018-01-13 14:06:33 +0800 | [diff] [blame] | 823 | new_rate = rockchip_mac_set_clk(priv->cru, rate); |
Sjoerd Simons | 0aefc0b | 2016-02-28 22:24:59 +0100 | [diff] [blame] | 824 | break; |
Simon Glass | 830a608 | 2016-01-21 19:45:02 -0700 | [diff] [blame] | 825 | case DCLK_VOP0: |
| 826 | case DCLK_VOP1: |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 827 | new_rate = rockchip_vop_set_clk(cru, priv->grf, clk->id, rate); |
Simon Glass | 830a608 | 2016-01-21 19:45:02 -0700 | [diff] [blame] | 828 | break; |
| 829 | case SCLK_EDP_24M: |
| 830 | /* clk_edp_24M source: 24M */ |
| 831 | rk_setreg(&cru->cru_clksel_con[28], 1 << 15); |
| 832 | |
| 833 | /* rst edp */ |
| 834 | rk_setreg(&cru->cru_clksel_con[6], 1 << 15); |
| 835 | udelay(1); |
| 836 | rk_clrreg(&cru->cru_clksel_con[6], 1 << 15); |
| 837 | new_rate = rate; |
| 838 | break; |
| 839 | case ACLK_VOP0: |
| 840 | case ACLK_VOP1: { |
| 841 | u32 div; |
| 842 | |
| 843 | /* vop aclk source clk: cpll */ |
| 844 | div = CPLL_HZ / rate; |
| 845 | assert((div - 1 < 64) && (div * rate == CPLL_HZ)); |
| 846 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 847 | switch (clk->id) { |
Simon Glass | 830a608 | 2016-01-21 19:45:02 -0700 | [diff] [blame] | 848 | case ACLK_VOP0: |
| 849 | rk_clrsetreg(&cru->cru_clksel_con[31], |
| 850 | 3 << 6 | 0x1f << 0, |
| 851 | 0 << 6 | (div - 1) << 0); |
| 852 | break; |
| 853 | case ACLK_VOP1: |
| 854 | rk_clrsetreg(&cru->cru_clksel_con[31], |
| 855 | 3 << 14 | 0x1f << 8, |
| 856 | 0 << 14 | (div - 1) << 8); |
| 857 | break; |
| 858 | } |
| 859 | new_rate = rate; |
| 860 | break; |
| 861 | } |
| 862 | case PCLK_HDMI_CTRL: |
| 863 | /* enable pclk hdmi ctrl */ |
| 864 | rk_clrreg(&cru->cru_clkgate_con[16], 1 << 9); |
| 865 | |
| 866 | /* software reset hdmi */ |
| 867 | rk_setreg(&cru->cru_clkgate_con[7], 1 << 9); |
| 868 | udelay(1); |
| 869 | rk_clrreg(&cru->cru_clkgate_con[7], 1 << 9); |
| 870 | new_rate = rate; |
| 871 | break; |
| 872 | #endif |
David Wu | ef4cf5a | 2017-09-20 14:28:19 +0800 | [diff] [blame] | 873 | case SCLK_SARADC: |
| 874 | new_rate = rockchip_saradc_set_clk(priv->cru, rate); |
| 875 | break; |
David Wu | 01c60ea | 2018-01-13 14:06:33 +0800 | [diff] [blame] | 876 | case PLL_GPLL: |
| 877 | case PLL_CPLL: |
| 878 | case PLL_NPLL: |
| 879 | case ACLK_CPU: |
| 880 | case HCLK_CPU: |
| 881 | case PCLK_CPU: |
| 882 | case ACLK_PERI: |
| 883 | case HCLK_PERI: |
| 884 | case PCLK_PERI: |
| 885 | case SCLK_UART0: |
| 886 | return 0; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 887 | default: |
| 888 | return -ENOENT; |
| 889 | } |
| 890 | |
| 891 | return new_rate; |
| 892 | } |
| 893 | |
Philipp Tomsich | 75b381a | 2018-01-25 15:27:10 +0100 | [diff] [blame] | 894 | static int __maybe_unused rk3288_gmac_set_parent(struct clk *clk, struct clk *parent) |
David Wu | 01c60ea | 2018-01-13 14:06:33 +0800 | [diff] [blame] | 895 | { |
| 896 | struct rk3288_clk_priv *priv = dev_get_priv(clk->dev); |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 897 | struct rockchip_cru *cru = priv->cru; |
David Wu | 01c60ea | 2018-01-13 14:06:33 +0800 | [diff] [blame] | 898 | const char *clock_output_name; |
| 899 | int ret; |
| 900 | |
| 901 | /* |
| 902 | * If the requested parent is in the same clock-controller and |
| 903 | * the id is SCLK_MAC_PLL ("mac_pll_src"), switch to the internal |
| 904 | * clock. |
| 905 | */ |
| 906 | if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC_PLL)) { |
| 907 | debug("%s: switching GAMC to SCLK_MAC_PLL\n", __func__); |
| 908 | rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK, 0); |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * Otherwise, we need to check the clock-output-names of the |
| 914 | * requested parent to see if the requested id is "ext_gmac". |
| 915 | */ |
| 916 | ret = dev_read_string_index(parent->dev, "clock-output-names", |
| 917 | parent->id, &clock_output_name); |
| 918 | if (ret < 0) |
| 919 | return -ENODATA; |
| 920 | |
| 921 | /* If this is "ext_gmac", switch to the external clock input */ |
| 922 | if (!strcmp(clock_output_name, "ext_gmac")) { |
| 923 | debug("%s: switching GMAC to external clock\n", __func__); |
| 924 | rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK, |
| 925 | RMII_EXTCLK_SELECT_EXT_CLK << RMII_EXTCLK_SHIFT); |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | return -EINVAL; |
| 930 | } |
| 931 | |
Philipp Tomsich | 75b381a | 2018-01-25 15:27:10 +0100 | [diff] [blame] | 932 | static int __maybe_unused rk3288_clk_set_parent(struct clk *clk, struct clk *parent) |
David Wu | 01c60ea | 2018-01-13 14:06:33 +0800 | [diff] [blame] | 933 | { |
| 934 | switch (clk->id) { |
| 935 | case SCLK_MAC: |
| 936 | return rk3288_gmac_set_parent(clk, parent); |
| 937 | case SCLK_USBPHY480M_SRC: |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | debug("%s: unsupported clk %ld\n", __func__, clk->id); |
| 942 | return -ENOENT; |
| 943 | } |
| 944 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 945 | static struct clk_ops rk3288_clk_ops = { |
| 946 | .get_rate = rk3288_clk_get_rate, |
| 947 | .set_rate = rk3288_clk_set_rate, |
Philipp Tomsich | 75b381a | 2018-01-25 15:27:10 +0100 | [diff] [blame] | 948 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
David Wu | 01c60ea | 2018-01-13 14:06:33 +0800 | [diff] [blame] | 949 | .set_parent = rk3288_clk_set_parent, |
Philipp Tomsich | 75b381a | 2018-01-25 15:27:10 +0100 | [diff] [blame] | 950 | #endif |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 951 | }; |
| 952 | |
Simon Glass | 08fd82c | 2016-07-04 11:58:28 -0600 | [diff] [blame] | 953 | static int rk3288_clk_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 954 | { |
Simon Glass | 2d143bd | 2016-07-04 11:58:29 -0600 | [diff] [blame] | 955 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 956 | struct rk3288_clk_priv *priv = dev_get_priv(dev); |
| 957 | |
Kever Yang | 995cde1 | 2018-02-11 11:53:08 +0800 | [diff] [blame] | 958 | priv->cru = dev_read_addr_ptr(dev); |
Simon Glass | 2d143bd | 2016-07-04 11:58:29 -0600 | [diff] [blame] | 959 | #endif |
Simon Glass | 08fd82c | 2016-07-04 11:58:28 -0600 | [diff] [blame] | 960 | |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | static int rk3288_clk_probe(struct udevice *dev) |
| 965 | { |
| 966 | struct rk3288_clk_priv *priv = dev_get_priv(dev); |
Simon Glass | d3cb46a | 2017-05-31 17:57:32 -0600 | [diff] [blame] | 967 | bool init_clocks = false; |
Simon Glass | 08fd82c | 2016-07-04 11:58:28 -0600 | [diff] [blame] | 968 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 969 | priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
Simon Glass | 08fd82c | 2016-07-04 11:58:28 -0600 | [diff] [blame] | 970 | if (IS_ERR(priv->grf)) |
| 971 | return PTR_ERR(priv->grf); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 972 | #ifdef CONFIG_SPL_BUILD |
Simon Glass | 2d143bd | 2016-07-04 11:58:29 -0600 | [diff] [blame] | 973 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 974 | struct rk3288_clk_plat *plat = dev_get_platdata(dev); |
| 975 | |
| 976 | priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]); |
| 977 | #endif |
Simon Glass | d3cb46a | 2017-05-31 17:57:32 -0600 | [diff] [blame] | 978 | init_clocks = true; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 979 | #endif |
Simon Glass | d3cb46a | 2017-05-31 17:57:32 -0600 | [diff] [blame] | 980 | if (!(gd->flags & GD_FLG_RELOC)) { |
| 981 | u32 reg; |
| 982 | |
| 983 | /* |
| 984 | * Init clocks in U-Boot proper if the NPLL is runnning. This |
| 985 | * indicates that a previous boot loader set up the clocks, so |
| 986 | * we need to redo it. U-Boot's SPL does not set this clock. |
| 987 | */ |
| 988 | reg = readl(&priv->cru->cru_mode_con); |
| 989 | if (((reg & NPLL_MODE_MASK) >> NPLL_MODE_SHIFT) == |
| 990 | NPLL_MODE_NORMAL) |
| 991 | init_clocks = true; |
| 992 | } |
| 993 | |
| 994 | if (init_clocks) |
| 995 | rkclk_init(priv->cru, priv->grf); |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 1000 | static int rk3288_clk_bind(struct udevice *dev) |
| 1001 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 1002 | int ret; |
Kever Yang | f24e36d | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 1003 | struct udevice *sys_child; |
| 1004 | struct sysreset_reg *priv; |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 1005 | |
| 1006 | /* The reset driver does not have a device node, so bind it here */ |
Kever Yang | f24e36d | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 1007 | ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset", |
| 1008 | &sys_child); |
| 1009 | if (ret) { |
| 1010 | debug("Warning: No sysreset driver: ret=%d\n", ret); |
| 1011 | } else { |
| 1012 | priv = malloc(sizeof(struct sysreset_reg)); |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 1013 | priv->glb_srst_fst_value = offsetof(struct rockchip_cru, |
Kever Yang | f24e36d | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 1014 | cru_glb_srst_fst_value); |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 1015 | priv->glb_srst_snd_value = offsetof(struct rockchip_cru, |
Kever Yang | f24e36d | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 1016 | cru_glb_srst_snd_value); |
| 1017 | sys_child->priv = priv; |
| 1018 | } |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 1019 | |
Heiko Stuebner | a5ada25 | 2019-11-09 00:06:30 +0100 | [diff] [blame] | 1020 | #if CONFIG_IS_ENABLED(RESET_ROCKCHIP) |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 1021 | ret = offsetof(struct rockchip_cru, cru_softrst_con[0]); |
Elaine Zhang | 538f67c | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 1022 | ret = rockchip_reset_bind(dev, ret, 12); |
| 1023 | if (ret) |
| 1024 | debug("Warning: software reset driver bind faile\n"); |
| 1025 | #endif |
| 1026 | |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | static const struct udevice_id rk3288_clk_ids[] = { |
| 1031 | { .compatible = "rockchip,rk3288-cru" }, |
| 1032 | { } |
| 1033 | }; |
| 1034 | |
Simon Glass | 2d143bd | 2016-07-04 11:58:29 -0600 | [diff] [blame] | 1035 | U_BOOT_DRIVER(rockchip_rk3288_cru) = { |
| 1036 | .name = "rockchip_rk3288_cru", |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 1037 | .id = UCLASS_CLK, |
| 1038 | .of_match = rk3288_clk_ids, |
| 1039 | .priv_auto_alloc_size = sizeof(struct rk3288_clk_priv), |
Simon Glass | 2d143bd | 2016-07-04 11:58:29 -0600 | [diff] [blame] | 1040 | .platdata_auto_alloc_size = sizeof(struct rk3288_clk_plat), |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 1041 | .ops = &rk3288_clk_ops, |
| 1042 | .bind = rk3288_clk_bind, |
Simon Glass | 08fd82c | 2016-07-04 11:58:28 -0600 | [diff] [blame] | 1043 | .ofdata_to_platdata = rk3288_clk_ofdata_to_platdata, |
Simon Glass | 99c1565 | 2015-08-30 16:55:31 -0600 | [diff] [blame] | 1044 | .probe = rk3288_clk_probe, |
| 1045 | }; |