Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * (C) Copyright 2018-2019 Rockchip Electronics Co., Ltd |
| 4 | */ |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 5 | #include <bitfield.h> |
| 6 | #include <clk-uclass.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 10 | #include <asm/arch-rockchip/clock.h> |
| 11 | #include <asm/arch-rockchip/hardware.h> |
| 12 | #include <div64.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 14 | |
| 15 | static struct rockchip_pll_rate_table rockchip_auto_table; |
| 16 | |
| 17 | #define PLL_MODE_MASK 0x3 |
| 18 | #define PLL_RK3328_MODE_MASK 0x1 |
| 19 | |
| 20 | #define RK3036_PLLCON0_FBDIV_MASK 0xfff |
| 21 | #define RK3036_PLLCON0_FBDIV_SHIFT 0 |
| 22 | #define RK3036_PLLCON0_POSTDIV1_MASK 0x7 << 12 |
| 23 | #define RK3036_PLLCON0_POSTDIV1_SHIFT 12 |
| 24 | #define RK3036_PLLCON1_REFDIV_MASK 0x3f |
| 25 | #define RK3036_PLLCON1_REFDIV_SHIFT 0 |
| 26 | #define RK3036_PLLCON1_POSTDIV2_MASK 0x7 << 6 |
| 27 | #define RK3036_PLLCON1_POSTDIV2_SHIFT 6 |
| 28 | #define RK3036_PLLCON1_DSMPD_MASK 0x1 << 12 |
| 29 | #define RK3036_PLLCON1_DSMPD_SHIFT 12 |
| 30 | #define RK3036_PLLCON2_FRAC_MASK 0xffffff |
| 31 | #define RK3036_PLLCON2_FRAC_SHIFT 0 |
Michal Suchanek | 4578349 | 2022-09-28 12:41:29 +0200 | [diff] [blame] | 32 | #define RK3036_PLLCON1_PWRDOWN_SHIFT 13 |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 33 | |
| 34 | #define MHZ 1000000 |
| 35 | #define KHZ 1000 |
| 36 | enum { |
| 37 | OSC_HZ = 24 * 1000000, |
| 38 | VCO_MAX_HZ = 3200U * 1000000, |
| 39 | VCO_MIN_HZ = 800 * 1000000, |
| 40 | OUTPUT_MAX_HZ = 3200U * 1000000, |
| 41 | OUTPUT_MIN_HZ = 24 * 1000000, |
| 42 | }; |
| 43 | |
| 44 | #define MIN_FOUTVCO_FREQ (800 * MHZ) |
| 45 | #define MAX_FOUTVCO_FREQ (2000 * MHZ) |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 46 | #define RK3588_VCO_MIN_HZ (2250UL * MHZ) |
| 47 | #define RK3588_VCO_MAX_HZ (4500UL * MHZ) |
| 48 | #define RK3588_FOUT_MIN_HZ (37UL * MHZ) |
| 49 | #define RK3588_FOUT_MAX_HZ (4500UL * MHZ) |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 50 | |
| 51 | int gcd(int m, int n) |
| 52 | { |
| 53 | int t; |
| 54 | |
| 55 | while (m > 0) { |
| 56 | if (n > m) { |
| 57 | t = m; |
| 58 | m = n; |
| 59 | n = t; |
| 60 | } /* swap */ |
| 61 | m -= n; |
| 62 | } |
| 63 | return n; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * How to calculate the PLL(from TRM V0.3 Part 1 Page 63): |
| 68 | * Formulas also embedded within the Fractional PLL Verilog model: |
| 69 | * If DSMPD = 1 (DSM is disabled, "integer mode") |
| 70 | * FOUTVCO = FREF / REFDIV * FBDIV |
| 71 | * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2 |
| 72 | * Where: |
| 73 | * FOUTVCO = Fractional PLL non-divided output frequency |
| 74 | * FOUTPOSTDIV = Fractional PLL divided output frequency |
| 75 | * (output of second post divider) |
| 76 | * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input) |
| 77 | * REFDIV = Fractional PLL input reference clock divider |
| 78 | * FBDIV = Integer value programmed into feedback divide |
| 79 | * |
| 80 | */ |
| 81 | |
| 82 | static int rockchip_pll_clk_set_postdiv(ulong fout_hz, |
| 83 | u32 *postdiv1, |
| 84 | u32 *postdiv2, |
| 85 | u32 *foutvco) |
| 86 | { |
| 87 | ulong freq; |
| 88 | |
| 89 | if (fout_hz < MIN_FOUTVCO_FREQ) { |
| 90 | for (*postdiv1 = 1; *postdiv1 <= 7; (*postdiv1)++) { |
| 91 | for (*postdiv2 = 1; *postdiv2 <= 7; (*postdiv2)++) { |
| 92 | freq = fout_hz * (*postdiv1) * (*postdiv2); |
| 93 | if (freq >= MIN_FOUTVCO_FREQ && |
| 94 | freq <= MAX_FOUTVCO_FREQ) { |
| 95 | *foutvco = freq; |
| 96 | return 0; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | printf("Can't FIND postdiv1/2 to make fout=%lu in 800~2000M.\n", |
| 101 | fout_hz); |
| 102 | } else { |
| 103 | *postdiv1 = 1; |
| 104 | *postdiv2 = 1; |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static struct rockchip_pll_rate_table * |
| 110 | rockchip_pll_clk_set_by_auto(ulong fin_hz, |
| 111 | ulong fout_hz) |
| 112 | { |
| 113 | struct rockchip_pll_rate_table *rate_table = &rockchip_auto_table; |
| 114 | /* FIXME set postdiv1/2 always 1*/ |
| 115 | u32 foutvco = fout_hz; |
| 116 | ulong fin_64, frac_64; |
| 117 | u32 f_frac, postdiv1, postdiv2; |
| 118 | ulong clk_gcd = 0; |
| 119 | |
| 120 | if (fin_hz == 0 || fout_hz == 0 || fout_hz == fin_hz) |
| 121 | return NULL; |
| 122 | |
| 123 | rockchip_pll_clk_set_postdiv(fout_hz, &postdiv1, &postdiv2, &foutvco); |
| 124 | rate_table->postdiv1 = postdiv1; |
| 125 | rate_table->postdiv2 = postdiv2; |
| 126 | rate_table->dsmpd = 1; |
| 127 | |
| 128 | if (fin_hz / MHZ * MHZ == fin_hz && fout_hz / MHZ * MHZ == fout_hz) { |
| 129 | fin_hz /= MHZ; |
| 130 | foutvco /= MHZ; |
| 131 | clk_gcd = gcd(fin_hz, foutvco); |
| 132 | rate_table->refdiv = fin_hz / clk_gcd; |
| 133 | rate_table->fbdiv = foutvco / clk_gcd; |
| 134 | |
| 135 | rate_table->frac = 0; |
| 136 | |
| 137 | debug("fin = %ld, fout = %ld, clk_gcd = %ld,\n", |
| 138 | fin_hz, fout_hz, clk_gcd); |
| 139 | debug("refdiv= %d,fbdiv= %d,postdiv1= %d,postdiv2= %d\n", |
| 140 | rate_table->refdiv, |
| 141 | rate_table->fbdiv, rate_table->postdiv1, |
| 142 | rate_table->postdiv2); |
| 143 | } else { |
| 144 | debug("frac div,fin_hz = %ld,fout_hz = %ld\n", |
| 145 | fin_hz, fout_hz); |
| 146 | debug("frac get postdiv1 = %d, postdiv2 = %d, foutvco = %d\n", |
| 147 | rate_table->postdiv1, rate_table->postdiv2, foutvco); |
| 148 | clk_gcd = gcd(fin_hz / MHZ, foutvco / MHZ); |
| 149 | rate_table->refdiv = fin_hz / MHZ / clk_gcd; |
| 150 | rate_table->fbdiv = foutvco / MHZ / clk_gcd; |
| 151 | debug("frac get refdiv = %d, fbdiv = %d\n", |
| 152 | rate_table->refdiv, rate_table->fbdiv); |
| 153 | |
| 154 | rate_table->frac = 0; |
| 155 | |
| 156 | f_frac = (foutvco % MHZ); |
| 157 | fin_64 = fin_hz; |
| 158 | fin_64 = fin_64 / rate_table->refdiv; |
| 159 | frac_64 = f_frac << 24; |
| 160 | frac_64 = frac_64 / fin_64; |
| 161 | rate_table->frac = frac_64; |
| 162 | if (rate_table->frac > 0) |
| 163 | rate_table->dsmpd = 0; |
| 164 | debug("frac = %x\n", rate_table->frac); |
| 165 | } |
| 166 | return rate_table; |
| 167 | } |
| 168 | |
Elaine Zhang | bb988a3 | 2023-10-12 18:18:28 +0800 | [diff] [blame] | 169 | static u32 |
| 170 | rockchip_rk3588_pll_k_get(u32 m, u32 p, u32 s, u64 fin_hz, u64 fvco) |
| 171 | { |
| 172 | u64 fref, fout, ffrac; |
| 173 | u32 k = 0; |
| 174 | |
| 175 | fref = fin_hz / p; |
| 176 | ffrac = fvco - (m * fref); |
| 177 | fout = ffrac * 65536; |
| 178 | k = fout / fref; |
| 179 | if (k > 32767) { |
| 180 | fref = fin_hz / p; |
| 181 | ffrac = ((m + 1) * fref) - fvco; |
| 182 | fout = ffrac * 65536; |
| 183 | k = ((fout * 10 / fref) + 7) / 10; |
| 184 | if (k > 32767) |
| 185 | k = 0; |
| 186 | else |
| 187 | k = ~k + 1; |
| 188 | } |
| 189 | return k; |
| 190 | } |
| 191 | |
| 192 | static struct rockchip_pll_rate_table * |
| 193 | rockchip_rk3588_pll_frac_by_auto(unsigned long fin_hz, unsigned long fout_hz) |
| 194 | { |
| 195 | struct rockchip_pll_rate_table *rate_table = &rockchip_auto_table; |
| 196 | u32 p, m, s, k; |
| 197 | u64 fvco; |
| 198 | |
| 199 | for (s = 0; s <= 6; s++) { |
| 200 | fvco = (u64)fout_hz << s; |
| 201 | if (fvco < RK3588_VCO_MIN_HZ || fvco > RK3588_VCO_MAX_HZ) |
| 202 | continue; |
| 203 | for (p = 1; p <= 4; p++) { |
| 204 | for (m = 64; m <= 1023; m++) { |
| 205 | if ((fvco >= m * fin_hz / p) && |
| 206 | (fvco < (m + 1) * fin_hz / p)) { |
| 207 | k = rockchip_rk3588_pll_k_get(m, p, s, |
| 208 | fin_hz, |
| 209 | fvco); |
| 210 | if (!k) |
| 211 | continue; |
| 212 | rate_table->p = p; |
| 213 | rate_table->s = s; |
| 214 | rate_table->k = k; |
| 215 | if (k > 32767) |
| 216 | rate_table->m = m + 1; |
| 217 | else |
| 218 | rate_table->m = m; |
| 219 | return rate_table; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | return NULL; |
| 225 | } |
| 226 | |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 227 | static struct rockchip_pll_rate_table * |
| 228 | rk3588_pll_clk_set_by_auto(unsigned long fin_hz, |
| 229 | unsigned long fout_hz) |
| 230 | { |
| 231 | struct rockchip_pll_rate_table *rate_table = &rockchip_auto_table; |
| 232 | u32 p, m, s; |
Elaine Zhang | bb988a3 | 2023-10-12 18:18:28 +0800 | [diff] [blame] | 233 | ulong fvco; |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 234 | |
| 235 | if (fin_hz == 0 || fout_hz == 0 || fout_hz == fin_hz) |
| 236 | return NULL; |
| 237 | |
| 238 | if (fout_hz > RK3588_FOUT_MAX_HZ || fout_hz < RK3588_FOUT_MIN_HZ) |
| 239 | return NULL; |
| 240 | |
| 241 | if (fin_hz / MHZ * MHZ == fin_hz && fout_hz / MHZ * MHZ == fout_hz) { |
| 242 | for (s = 0; s <= 6; s++) { |
| 243 | fvco = fout_hz << s; |
| 244 | if (fvco < RK3588_VCO_MIN_HZ || |
| 245 | fvco > RK3588_VCO_MAX_HZ) |
| 246 | continue; |
| 247 | for (p = 2; p <= 4; p++) { |
| 248 | for (m = 64; m <= 1023; m++) { |
| 249 | if (fvco == m * fin_hz / p) { |
| 250 | rate_table->p = p; |
| 251 | rate_table->m = m; |
| 252 | rate_table->s = s; |
| 253 | rate_table->k = 0; |
| 254 | return rate_table; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | pr_err("CANNOT FIND Fout by auto,fout = %lu\n", fout_hz); |
| 260 | } else { |
Elaine Zhang | bb988a3 | 2023-10-12 18:18:28 +0800 | [diff] [blame] | 261 | rate_table = rockchip_rk3588_pll_frac_by_auto(fin_hz, fout_hz); |
| 262 | if (!rate_table) |
| 263 | pr_err("CANNOT FIND Fout by auto,fout = %lu\n", |
| 264 | fout_hz); |
| 265 | else |
| 266 | return rate_table; |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 267 | } |
| 268 | return NULL; |
| 269 | } |
| 270 | |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 271 | static const struct rockchip_pll_rate_table * |
| 272 | rockchip_get_pll_settings(struct rockchip_pll_clock *pll, ulong rate) |
| 273 | { |
| 274 | struct rockchip_pll_rate_table *rate_table = pll->rate_table; |
| 275 | |
| 276 | while (rate_table->rate) { |
| 277 | if (rate_table->rate == rate) |
| 278 | break; |
| 279 | rate_table++; |
| 280 | } |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 281 | if (rate_table->rate != rate) { |
| 282 | if (pll->type == pll_rk3588) |
| 283 | return rk3588_pll_clk_set_by_auto(24 * MHZ, rate); |
| 284 | else |
| 285 | return rockchip_pll_clk_set_by_auto(24 * MHZ, rate); |
| 286 | } else { |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 287 | return rate_table; |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 288 | } |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int rk3036_pll_set_rate(struct rockchip_pll_clock *pll, |
| 292 | void __iomem *base, ulong pll_id, |
| 293 | ulong drate) |
| 294 | { |
| 295 | const struct rockchip_pll_rate_table *rate; |
| 296 | |
| 297 | rate = rockchip_get_pll_settings(pll, drate); |
| 298 | if (!rate) { |
| 299 | printf("%s unsupport rate\n", __func__); |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | |
| 303 | debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d\n", |
| 304 | __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv); |
| 305 | debug("%s: rate settings for %lu postdiv2: %d, dsmpd: %d, frac: %d\n", |
| 306 | __func__, rate->rate, rate->postdiv2, rate->dsmpd, rate->frac); |
| 307 | |
| 308 | /* |
| 309 | * When power on or changing PLL setting, |
| 310 | * we must force PLL into slow mode to ensure output stable clock. |
| 311 | */ |
| 312 | rk_clrsetreg(base + pll->mode_offset, |
| 313 | pll->mode_mask << pll->mode_shift, |
| 314 | RKCLK_PLL_MODE_SLOW << pll->mode_shift); |
| 315 | |
| 316 | /* Power down */ |
| 317 | rk_setreg(base + pll->con_offset + 0x4, |
Michal Suchanek | 4578349 | 2022-09-28 12:41:29 +0200 | [diff] [blame] | 318 | 1 << RK3036_PLLCON1_PWRDOWN_SHIFT); |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 319 | |
| 320 | rk_clrsetreg(base + pll->con_offset, |
| 321 | (RK3036_PLLCON0_POSTDIV1_MASK | |
| 322 | RK3036_PLLCON0_FBDIV_MASK), |
| 323 | (rate->postdiv1 << RK3036_PLLCON0_POSTDIV1_SHIFT) | |
| 324 | rate->fbdiv); |
| 325 | rk_clrsetreg(base + pll->con_offset + 0x4, |
| 326 | (RK3036_PLLCON1_POSTDIV2_MASK | |
| 327 | RK3036_PLLCON1_REFDIV_MASK), |
| 328 | (rate->postdiv2 << RK3036_PLLCON1_POSTDIV2_SHIFT | |
| 329 | rate->refdiv << RK3036_PLLCON1_REFDIV_SHIFT)); |
| 330 | if (!rate->dsmpd) { |
| 331 | rk_clrsetreg(base + pll->con_offset + 0x4, |
| 332 | RK3036_PLLCON1_DSMPD_MASK, |
| 333 | rate->dsmpd << RK3036_PLLCON1_DSMPD_SHIFT); |
| 334 | writel((readl(base + pll->con_offset + 0x8) & |
| 335 | (~RK3036_PLLCON2_FRAC_MASK)) | |
| 336 | (rate->frac << RK3036_PLLCON2_FRAC_SHIFT), |
| 337 | base + pll->con_offset + 0x8); |
| 338 | } |
| 339 | |
| 340 | /* Power Up */ |
| 341 | rk_clrreg(base + pll->con_offset + 0x4, |
Michal Suchanek | 4578349 | 2022-09-28 12:41:29 +0200 | [diff] [blame] | 342 | 1 << RK3036_PLLCON1_PWRDOWN_SHIFT); |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 343 | |
| 344 | /* waiting for pll lock */ |
| 345 | while (!(readl(base + pll->con_offset + 0x4) & (1 << pll->lock_shift))) |
| 346 | udelay(1); |
| 347 | |
| 348 | rk_clrsetreg(base + pll->mode_offset, pll->mode_mask << pll->mode_shift, |
| 349 | RKCLK_PLL_MODE_NORMAL << pll->mode_shift); |
| 350 | debug("PLL at %p: con0=%x con1= %x con2= %x mode= %x\n", |
| 351 | pll, readl(base + pll->con_offset), |
| 352 | readl(base + pll->con_offset + 0x4), |
| 353 | readl(base + pll->con_offset + 0x8), |
| 354 | readl(base + pll->mode_offset)); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static ulong rk3036_pll_get_rate(struct rockchip_pll_clock *pll, |
| 360 | void __iomem *base, ulong pll_id) |
| 361 | { |
| 362 | u32 refdiv, fbdiv, postdiv1, postdiv2, dsmpd, frac; |
| 363 | u32 con = 0, shift, mask; |
| 364 | ulong rate; |
| 365 | |
| 366 | con = readl(base + pll->mode_offset); |
| 367 | shift = pll->mode_shift; |
| 368 | mask = pll->mode_mask << shift; |
| 369 | |
| 370 | switch ((con & mask) >> shift) { |
| 371 | case RKCLK_PLL_MODE_SLOW: |
| 372 | return OSC_HZ; |
| 373 | case RKCLK_PLL_MODE_NORMAL: |
| 374 | /* normal mode */ |
| 375 | con = readl(base + pll->con_offset); |
| 376 | postdiv1 = (con & RK3036_PLLCON0_POSTDIV1_MASK) >> |
| 377 | RK3036_PLLCON0_POSTDIV1_SHIFT; |
| 378 | fbdiv = (con & RK3036_PLLCON0_FBDIV_MASK) >> |
| 379 | RK3036_PLLCON0_FBDIV_SHIFT; |
| 380 | con = readl(base + pll->con_offset + 0x4); |
| 381 | postdiv2 = (con & RK3036_PLLCON1_POSTDIV2_MASK) >> |
| 382 | RK3036_PLLCON1_POSTDIV2_SHIFT; |
| 383 | refdiv = (con & RK3036_PLLCON1_REFDIV_MASK) >> |
| 384 | RK3036_PLLCON1_REFDIV_SHIFT; |
| 385 | dsmpd = (con & RK3036_PLLCON1_DSMPD_MASK) >> |
| 386 | RK3036_PLLCON1_DSMPD_SHIFT; |
| 387 | con = readl(base + pll->con_offset + 0x8); |
| 388 | frac = (con & RK3036_PLLCON2_FRAC_MASK) >> |
| 389 | RK3036_PLLCON2_FRAC_SHIFT; |
| 390 | rate = (24 * fbdiv / (refdiv * postdiv1 * postdiv2)) * 1000000; |
| 391 | if (dsmpd == 0) { |
| 392 | u64 frac_rate = OSC_HZ * (u64)frac; |
| 393 | |
| 394 | do_div(frac_rate, refdiv); |
| 395 | frac_rate >>= 24; |
| 396 | do_div(frac_rate, postdiv1); |
| 397 | do_div(frac_rate, postdiv1); |
| 398 | rate += frac_rate; |
| 399 | } |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 400 | return rate; |
| 401 | case RKCLK_PLL_MODE_DEEP: |
| 402 | default: |
| 403 | return 32768; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | #define RK3588_PLLCON(i) ((i) * 0x4) |
| 408 | #define RK3588_PLLCON0_M_MASK 0x3ff << 0 |
| 409 | #define RK3588_PLLCON0_M_SHIFT 0 |
| 410 | #define RK3588_PLLCON1_P_MASK 0x3f << 0 |
| 411 | #define RK3588_PLLCON1_P_SHIFT 0 |
| 412 | #define RK3588_PLLCON1_S_MASK 0x7 << 6 |
| 413 | #define RK3588_PLLCON1_S_SHIFT 6 |
| 414 | #define RK3588_PLLCON2_K_MASK 0xffff |
| 415 | #define RK3588_PLLCON2_K_SHIFT 0 |
| 416 | #define RK3588_PLLCON1_PWRDOWN BIT(13) |
| 417 | #define RK3588_PLLCON6_LOCK_STATUS BIT(15) |
| 418 | #define RK3588_B0PLL_CLKSEL_CON(i) ((i) * 0x4 + 0x50000 + 0x300) |
| 419 | #define RK3588_B1PLL_CLKSEL_CON(i) ((i) * 0x4 + 0x52000 + 0x300) |
| 420 | #define RK3588_LPLL_CLKSEL_CON(i) ((i) * 0x4 + 0x58000 + 0x300) |
| 421 | #define RK3588_CORE_DIV_MASK 0x1f |
| 422 | #define RK3588_CORE_L02_DIV_SHIFT 0 |
| 423 | #define RK3588_CORE_L13_DIV_SHIFT 7 |
| 424 | #define RK3588_CORE_B02_DIV_SHIFT 8 |
| 425 | #define RK3588_CORE_B13_DIV_SHIFT 0 |
| 426 | |
| 427 | static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll, |
| 428 | void __iomem *base, ulong pll_id, |
| 429 | ulong drate) |
| 430 | { |
| 431 | const struct rockchip_pll_rate_table *rate; |
| 432 | |
| 433 | rate = rockchip_get_pll_settings(pll, drate); |
| 434 | if (!rate) { |
| 435 | printf("%s unsupported rate\n", __func__); |
| 436 | return -EINVAL; |
| 437 | } |
| 438 | |
| 439 | debug("%s: rate settings for %lu p: %d, m: %d, s: %d, k: %d\n", |
| 440 | __func__, rate->rate, rate->p, rate->m, rate->s, rate->k); |
| 441 | |
| 442 | /* |
| 443 | * When power on or changing PLL setting, |
| 444 | * we must force PLL into slow mode to ensure output stable clock. |
| 445 | */ |
| 446 | if (pll_id == 3) |
| 447 | rk_clrsetreg(base + 0x84c, 0x1 << 1, 0x1 << 1); |
| 448 | |
| 449 | rk_clrsetreg(base + pll->mode_offset, |
| 450 | pll->mode_mask << pll->mode_shift, |
| 451 | RKCLK_PLL_MODE_SLOW << pll->mode_shift); |
| 452 | if (pll_id == 0) |
| 453 | rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(0), |
| 454 | pll->mode_mask << 6, |
| 455 | RKCLK_PLL_MODE_SLOW << 6); |
| 456 | else if (pll_id == 1) |
| 457 | rk_clrsetreg(base + RK3588_B1PLL_CLKSEL_CON(0), |
| 458 | pll->mode_mask << 6, |
| 459 | RKCLK_PLL_MODE_SLOW << 6); |
| 460 | else if (pll_id == 2) |
| 461 | rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(5), |
| 462 | pll->mode_mask << 14, |
| 463 | RKCLK_PLL_MODE_SLOW << 14); |
| 464 | |
| 465 | /* Power down */ |
| 466 | rk_setreg(base + pll->con_offset + RK3588_PLLCON(1), |
| 467 | RK3588_PLLCON1_PWRDOWN); |
| 468 | |
| 469 | rk_clrsetreg(base + pll->con_offset, |
| 470 | RK3588_PLLCON0_M_MASK, |
| 471 | (rate->m << RK3588_PLLCON0_M_SHIFT)); |
| 472 | rk_clrsetreg(base + pll->con_offset + RK3588_PLLCON(1), |
| 473 | (RK3588_PLLCON1_P_MASK | |
| 474 | RK3588_PLLCON1_S_MASK), |
| 475 | (rate->p << RK3588_PLLCON1_P_SHIFT | |
| 476 | rate->s << RK3588_PLLCON1_S_SHIFT)); |
| 477 | if (rate->k) { |
| 478 | rk_clrsetreg(base + pll->con_offset + RK3588_PLLCON(2), |
| 479 | RK3588_PLLCON2_K_MASK, |
| 480 | rate->k << RK3588_PLLCON2_K_SHIFT); |
| 481 | } |
| 482 | /* Power up */ |
| 483 | rk_clrreg(base + pll->con_offset + RK3588_PLLCON(1), |
| 484 | RK3588_PLLCON1_PWRDOWN); |
| 485 | |
| 486 | /* waiting for pll lock */ |
| 487 | while (!(readl(base + pll->con_offset + RK3588_PLLCON(6)) & |
| 488 | RK3588_PLLCON6_LOCK_STATUS)) { |
| 489 | udelay(1); |
| 490 | debug("%s: wait pll lock, pll_id=%ld\n", __func__, pll_id); |
| 491 | } |
| 492 | |
| 493 | rk_clrsetreg(base + pll->mode_offset, pll->mode_mask << pll->mode_shift, |
| 494 | RKCLK_PLL_MODE_NORMAL << pll->mode_shift); |
| 495 | if (pll_id == 0) { |
| 496 | rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(0), |
| 497 | pll->mode_mask << 6, |
| 498 | 2 << 6); |
| 499 | rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(0), |
| 500 | RK3588_CORE_DIV_MASK << RK3588_CORE_B02_DIV_SHIFT, |
| 501 | 0 << RK3588_CORE_B02_DIV_SHIFT); |
| 502 | rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(1), |
| 503 | RK3588_CORE_DIV_MASK << RK3588_CORE_B13_DIV_SHIFT, |
| 504 | 0 << RK3588_CORE_B13_DIV_SHIFT); |
| 505 | } else if (pll_id == 1) { |
| 506 | rk_clrsetreg(base + RK3588_B1PLL_CLKSEL_CON(0), |
| 507 | pll->mode_mask << 6, |
| 508 | 2 << 6); |
| 509 | rk_clrsetreg(base + RK3588_B1PLL_CLKSEL_CON(0), |
| 510 | RK3588_CORE_DIV_MASK << RK3588_CORE_B02_DIV_SHIFT, |
| 511 | 0 << RK3588_CORE_B02_DIV_SHIFT); |
| 512 | rk_clrsetreg(base + RK3588_B1PLL_CLKSEL_CON(1), |
| 513 | RK3588_CORE_DIV_MASK << RK3588_CORE_B13_DIV_SHIFT, |
| 514 | 0 << RK3588_CORE_B13_DIV_SHIFT); |
| 515 | } else if (pll_id == 2) { |
| 516 | rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(5), |
| 517 | pll->mode_mask << 14, |
| 518 | 2 << 14); |
| 519 | rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(6), |
| 520 | RK3588_CORE_DIV_MASK << RK3588_CORE_L13_DIV_SHIFT, |
| 521 | 0 << RK3588_CORE_L13_DIV_SHIFT); |
| 522 | rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(6), |
| 523 | RK3588_CORE_DIV_MASK << RK3588_CORE_L02_DIV_SHIFT, |
| 524 | 0 << RK3588_CORE_L02_DIV_SHIFT); |
| 525 | rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(7), |
| 526 | RK3588_CORE_DIV_MASK << RK3588_CORE_L13_DIV_SHIFT, |
| 527 | 0 << RK3588_CORE_L13_DIV_SHIFT); |
| 528 | rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(7), |
| 529 | RK3588_CORE_DIV_MASK << RK3588_CORE_L02_DIV_SHIFT, |
| 530 | 0 << RK3588_CORE_L02_DIV_SHIFT); |
| 531 | } |
| 532 | |
| 533 | if (pll_id == 3) |
| 534 | rk_clrsetreg(base + 0x84c, 0x1 << 1, 0); |
| 535 | |
| 536 | debug("PLL at %p: con0=%x con1= %x con2= %x mode= %x\n", |
| 537 | pll, readl(base + pll->con_offset), |
| 538 | readl(base + pll->con_offset + 0x4), |
| 539 | readl(base + pll->con_offset + 0x8), |
| 540 | readl(base + pll->mode_offset)); |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static ulong rk3588_pll_get_rate(struct rockchip_pll_clock *pll, |
| 546 | void __iomem *base, ulong pll_id) |
| 547 | { |
| 548 | u32 m, p, s, k; |
| 549 | u32 con = 0, shift, mode; |
| 550 | u64 rate, postdiv; |
| 551 | |
| 552 | con = readl(base + pll->mode_offset); |
| 553 | shift = pll->mode_shift; |
| 554 | if (pll_id == 8) |
| 555 | mode = RKCLK_PLL_MODE_NORMAL; |
| 556 | else |
| 557 | mode = (con & (pll->mode_mask << shift)) >> shift; |
| 558 | switch (mode) { |
| 559 | case RKCLK_PLL_MODE_SLOW: |
| 560 | return OSC_HZ; |
| 561 | case RKCLK_PLL_MODE_NORMAL: |
| 562 | /* normal mode */ |
| 563 | con = readl(base + pll->con_offset); |
| 564 | m = (con & RK3588_PLLCON0_M_MASK) >> |
| 565 | RK3588_PLLCON0_M_SHIFT; |
| 566 | con = readl(base + pll->con_offset + RK3588_PLLCON(1)); |
| 567 | p = (con & RK3588_PLLCON1_P_MASK) >> |
| 568 | RK3036_PLLCON0_FBDIV_SHIFT; |
| 569 | s = (con & RK3588_PLLCON1_S_MASK) >> |
| 570 | RK3588_PLLCON1_S_SHIFT; |
| 571 | con = readl(base + pll->con_offset + RK3588_PLLCON(2)); |
| 572 | k = (con & RK3588_PLLCON2_K_MASK) >> |
| 573 | RK3588_PLLCON2_K_SHIFT; |
| 574 | |
| 575 | rate = OSC_HZ / p; |
| 576 | rate *= m; |
Elaine Zhang | bb988a3 | 2023-10-12 18:18:28 +0800 | [diff] [blame] | 577 | if (k & BIT(15)) { |
| 578 | /* fractional mode */ |
| 579 | u64 frac_rate64; |
| 580 | |
| 581 | k = (~(k - 1)) & RK3588_PLLCON2_K_MASK; |
| 582 | frac_rate64 = OSC_HZ * k; |
| 583 | postdiv = p; |
| 584 | postdiv *= 65536; |
| 585 | do_div(frac_rate64, postdiv); |
| 586 | rate -= frac_rate64; |
| 587 | } else { |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 588 | /* fractional mode */ |
| 589 | u64 frac_rate64 = OSC_HZ * k; |
| 590 | |
Elaine Zhang | bb988a3 | 2023-10-12 18:18:28 +0800 | [diff] [blame] | 591 | postdiv = p; |
| 592 | postdiv *= 65536; |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 593 | do_div(frac_rate64, postdiv); |
| 594 | rate += frac_rate64; |
| 595 | } |
| 596 | rate = rate >> s; |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 597 | return rate; |
| 598 | case RKCLK_PLL_MODE_DEEP: |
| 599 | default: |
| 600 | return 32768; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | ulong rockchip_pll_get_rate(struct rockchip_pll_clock *pll, |
| 605 | void __iomem *base, |
| 606 | ulong pll_id) |
| 607 | { |
| 608 | ulong rate = 0; |
| 609 | |
| 610 | switch (pll->type) { |
| 611 | case pll_rk3036: |
| 612 | pll->mode_mask = PLL_MODE_MASK; |
| 613 | rate = rk3036_pll_get_rate(pll, base, pll_id); |
| 614 | break; |
| 615 | case pll_rk3328: |
| 616 | pll->mode_mask = PLL_RK3328_MODE_MASK; |
| 617 | rate = rk3036_pll_get_rate(pll, base, pll_id); |
| 618 | break; |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 619 | case pll_rk3588: |
| 620 | pll->mode_mask = PLL_MODE_MASK; |
| 621 | rate = rk3588_pll_get_rate(pll, base, pll_id); |
| 622 | break; |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 623 | default: |
| 624 | printf("%s: Unknown pll type for pll clk %ld\n", |
| 625 | __func__, pll_id); |
| 626 | } |
| 627 | return rate; |
| 628 | } |
| 629 | |
| 630 | int rockchip_pll_set_rate(struct rockchip_pll_clock *pll, |
| 631 | void __iomem *base, ulong pll_id, |
| 632 | ulong drate) |
| 633 | { |
| 634 | int ret = 0; |
| 635 | |
| 636 | if (rockchip_pll_get_rate(pll, base, pll_id) == drate) |
| 637 | return 0; |
| 638 | |
| 639 | switch (pll->type) { |
| 640 | case pll_rk3036: |
| 641 | pll->mode_mask = PLL_MODE_MASK; |
| 642 | ret = rk3036_pll_set_rate(pll, base, pll_id, drate); |
| 643 | break; |
| 644 | case pll_rk3328: |
| 645 | pll->mode_mask = PLL_RK3328_MODE_MASK; |
| 646 | ret = rk3036_pll_set_rate(pll, base, pll_id, drate); |
| 647 | break; |
Jagan Teki | 7d1bf8d | 2023-01-30 20:27:37 +0530 | [diff] [blame] | 648 | case pll_rk3588: |
| 649 | pll->mode_mask = PLL_MODE_MASK; |
| 650 | ret = rk3588_pll_set_rate(pll, base, pll_id, drate); |
| 651 | break; |
Elaine Zhang | a8a2ca8 | 2019-10-25 09:42:17 +0800 | [diff] [blame] | 652 | default: |
| 653 | printf("%s: Unknown pll type for pll clk %ld\n", |
| 654 | __func__, pll_id); |
| 655 | } |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | const struct rockchip_cpu_rate_table * |
| 660 | rockchip_get_cpu_settings(struct rockchip_cpu_rate_table *cpu_table, |
| 661 | ulong rate) |
| 662 | { |
| 663 | struct rockchip_cpu_rate_table *ps = cpu_table; |
| 664 | |
| 665 | while (ps->rate) { |
| 666 | if (ps->rate == rate) |
| 667 | break; |
| 668 | ps++; |
| 669 | } |
| 670 | if (ps->rate != rate) |
| 671 | return NULL; |
| 672 | else |
| 673 | return ps; |
| 674 | } |