Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * (C) Copyright 2017 Rockchip Electronics Co., Ltd |
| 4 | */ |
| 5 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 6 | #include <common.h> |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 7 | #include <bitfield.h> |
| 8 | #include <clk-uclass.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 13 | #include <syscon.h> |
| 14 | #include <asm/arch-rockchip/clock.h> |
| 15 | #include <asm/arch-rockchip/cru_px30.h> |
| 16 | #include <asm/arch-rockchip/hardware.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 19 | #include <dm/lists.h> |
| 20 | #include <dt-bindings/clock/px30-cru.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
| 26 | enum { |
| 27 | VCO_MAX_HZ = 3200U * 1000000, |
| 28 | VCO_MIN_HZ = 800 * 1000000, |
| 29 | OUTPUT_MAX_HZ = 3200U * 1000000, |
| 30 | OUTPUT_MIN_HZ = 24 * 1000000, |
| 31 | }; |
| 32 | |
| 33 | #define PX30_VOP_PLL_LIMIT 600000000 |
| 34 | |
| 35 | #define PX30_PLL_RATE(_rate, _refdiv, _fbdiv, _postdiv1, \ |
| 36 | _postdiv2, _dsmpd, _frac) \ |
| 37 | { \ |
| 38 | .rate = _rate##U, \ |
| 39 | .fbdiv = _fbdiv, \ |
| 40 | .postdiv1 = _postdiv1, \ |
| 41 | .refdiv = _refdiv, \ |
| 42 | .postdiv2 = _postdiv2, \ |
| 43 | .dsmpd = _dsmpd, \ |
| 44 | .frac = _frac, \ |
| 45 | } |
| 46 | |
| 47 | #define PX30_CPUCLK_RATE(_rate, _aclk_div, _pclk_div) \ |
| 48 | { \ |
| 49 | .rate = _rate##U, \ |
| 50 | .aclk_div = _aclk_div, \ |
| 51 | .pclk_div = _pclk_div, \ |
| 52 | } |
| 53 | |
| 54 | #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1)) |
| 55 | |
| 56 | #define PX30_CLK_DUMP(_id, _name, _iscru) \ |
| 57 | { \ |
| 58 | .id = _id, \ |
| 59 | .name = _name, \ |
| 60 | .is_cru = _iscru, \ |
| 61 | } |
| 62 | |
| 63 | static struct pll_rate_table px30_pll_rates[] = { |
| 64 | /* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2, _dsmpd, _frac */ |
| 65 | PX30_PLL_RATE(1200000000, 1, 50, 1, 1, 1, 0), |
| 66 | PX30_PLL_RATE(1188000000, 2, 99, 1, 1, 1, 0), |
| 67 | PX30_PLL_RATE(1100000000, 12, 550, 1, 1, 1, 0), |
| 68 | PX30_PLL_RATE(1008000000, 1, 84, 2, 1, 1, 0), |
| 69 | PX30_PLL_RATE(1000000000, 6, 500, 2, 1, 1, 0), |
| 70 | PX30_PLL_RATE(816000000, 1, 68, 2, 1, 1, 0), |
| 71 | PX30_PLL_RATE(600000000, 1, 75, 3, 1, 1, 0), |
| 72 | }; |
| 73 | |
| 74 | static struct cpu_rate_table px30_cpu_rates[] = { |
| 75 | PX30_CPUCLK_RATE(1200000000, 1, 5), |
| 76 | PX30_CPUCLK_RATE(1008000000, 1, 5), |
| 77 | PX30_CPUCLK_RATE(816000000, 1, 3), |
| 78 | PX30_CPUCLK_RATE(600000000, 1, 3), |
| 79 | PX30_CPUCLK_RATE(408000000, 1, 1), |
| 80 | }; |
| 81 | |
| 82 | static u8 pll_mode_shift[PLL_COUNT] = { |
| 83 | APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT, |
| 84 | NPLL_MODE_SHIFT, GPLL_MODE_SHIFT |
| 85 | }; |
| 86 | |
| 87 | static u32 pll_mode_mask[PLL_COUNT] = { |
| 88 | APLL_MODE_MASK, DPLL_MODE_MASK, CPLL_MODE_MASK, |
| 89 | NPLL_MODE_MASK, GPLL_MODE_MASK |
| 90 | }; |
| 91 | |
| 92 | static struct pll_rate_table auto_table; |
| 93 | |
| 94 | static ulong px30_clk_get_pll_rate(struct px30_clk_priv *priv, |
| 95 | enum px30_pll_id pll_id); |
| 96 | |
| 97 | static struct pll_rate_table *pll_clk_set_by_auto(u32 drate) |
| 98 | { |
| 99 | struct pll_rate_table *rate = &auto_table; |
| 100 | u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0; |
| 101 | u32 postdiv1, postdiv2 = 1; |
| 102 | u32 fref_khz; |
| 103 | u32 diff_khz, best_diff_khz; |
| 104 | const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16; |
| 105 | const u32 max_postdiv1 = 7, max_postdiv2 = 7; |
| 106 | u32 vco_khz; |
| 107 | u32 rate_khz = drate / KHz; |
| 108 | |
| 109 | if (!drate) { |
| 110 | printf("%s: the frequency can't be 0 Hz\n", __func__); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | postdiv1 = DIV_ROUND_UP(VCO_MIN_HZ / 1000, rate_khz); |
| 115 | if (postdiv1 > max_postdiv1) { |
| 116 | postdiv2 = DIV_ROUND_UP(postdiv1, max_postdiv1); |
| 117 | postdiv1 = DIV_ROUND_UP(postdiv1, postdiv2); |
| 118 | } |
| 119 | |
| 120 | vco_khz = rate_khz * postdiv1 * postdiv2; |
| 121 | |
| 122 | if (vco_khz < (VCO_MIN_HZ / KHz) || vco_khz > (VCO_MAX_HZ / KHz) || |
| 123 | postdiv2 > max_postdiv2) { |
| 124 | printf("%s: Cannot find out a supported VCO for Freq (%uHz)\n", |
| 125 | __func__, rate_khz); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | rate->postdiv1 = postdiv1; |
| 130 | rate->postdiv2 = postdiv2; |
| 131 | |
| 132 | best_diff_khz = vco_khz; |
| 133 | for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) { |
| 134 | fref_khz = ref_khz / refdiv; |
| 135 | |
| 136 | fbdiv = vco_khz / fref_khz; |
| 137 | if (fbdiv >= max_fbdiv || fbdiv <= min_fbdiv) |
| 138 | continue; |
| 139 | |
| 140 | diff_khz = vco_khz - fbdiv * fref_khz; |
| 141 | if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) { |
| 142 | fbdiv++; |
| 143 | diff_khz = fref_khz - diff_khz; |
| 144 | } |
| 145 | |
| 146 | if (diff_khz >= best_diff_khz) |
| 147 | continue; |
| 148 | |
| 149 | best_diff_khz = diff_khz; |
| 150 | rate->refdiv = refdiv; |
| 151 | rate->fbdiv = fbdiv; |
| 152 | } |
| 153 | |
| 154 | if (best_diff_khz > 4 * (MHz / KHz)) { |
| 155 | printf("%s: Failed to match output frequency %u bestis %u Hz\n", |
| 156 | __func__, rate_khz, |
| 157 | best_diff_khz * KHz); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | return rate; |
| 162 | } |
| 163 | |
| 164 | static const struct pll_rate_table *get_pll_settings(unsigned long rate) |
| 165 | { |
| 166 | unsigned int rate_count = ARRAY_SIZE(px30_pll_rates); |
| 167 | int i; |
| 168 | |
| 169 | for (i = 0; i < rate_count; i++) { |
| 170 | if (rate == px30_pll_rates[i].rate) |
| 171 | return &px30_pll_rates[i]; |
| 172 | } |
| 173 | |
| 174 | return pll_clk_set_by_auto(rate); |
| 175 | } |
| 176 | |
| 177 | static const struct cpu_rate_table *get_cpu_settings(unsigned long rate) |
| 178 | { |
| 179 | unsigned int rate_count = ARRAY_SIZE(px30_cpu_rates); |
| 180 | int i; |
| 181 | |
| 182 | for (i = 0; i < rate_count; i++) { |
| 183 | if (rate == px30_cpu_rates[i].rate) |
| 184 | return &px30_cpu_rates[i]; |
| 185 | } |
| 186 | |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * How to calculate the PLL(from TRM V0.3 Part 1 Page 63): |
| 192 | * Formulas also embedded within the Fractional PLL Verilog model: |
| 193 | * If DSMPD = 1 (DSM is disabled, "integer mode") |
| 194 | * FOUTVCO = FREF / REFDIV * FBDIV |
| 195 | * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2 |
| 196 | * Where: |
| 197 | * FOUTVCO = Fractional PLL non-divided output frequency |
| 198 | * FOUTPOSTDIV = Fractional PLL divided output frequency |
| 199 | * (output of second post divider) |
| 200 | * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input) |
| 201 | * REFDIV = Fractional PLL input reference clock divider |
| 202 | * FBDIV = Integer value programmed into feedback divide |
| 203 | * |
| 204 | */ |
| 205 | static int rkclk_set_pll(struct px30_pll *pll, unsigned int *mode, |
| 206 | enum px30_pll_id pll_id, |
| 207 | unsigned long drate) |
| 208 | { |
| 209 | const struct pll_rate_table *rate; |
| 210 | uint vco_hz, output_hz; |
| 211 | |
| 212 | rate = get_pll_settings(drate); |
| 213 | if (!rate) { |
| 214 | printf("%s unsupport rate\n", __func__); |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | /* All PLLs have same VCO and output frequency range restrictions. */ |
| 219 | vco_hz = OSC_HZ / 1000 * rate->fbdiv / rate->refdiv * 1000; |
| 220 | output_hz = vco_hz / rate->postdiv1 / rate->postdiv2; |
| 221 | |
| 222 | debug("PLL at %p: fb=%d, ref=%d, pst1=%d, pst2=%d, vco=%u Hz, output=%u Hz\n", |
| 223 | pll, rate->fbdiv, rate->refdiv, rate->postdiv1, |
| 224 | rate->postdiv2, vco_hz, output_hz); |
| 225 | assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ && |
| 226 | output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ); |
| 227 | |
| 228 | /* |
| 229 | * When power on or changing PLL setting, |
| 230 | * we must force PLL into slow mode to ensure output stable clock. |
| 231 | */ |
| 232 | rk_clrsetreg(mode, pll_mode_mask[pll_id], |
| 233 | PLLMUX_FROM_XIN24M << pll_mode_shift[pll_id]); |
| 234 | |
| 235 | /* use integer mode */ |
| 236 | rk_setreg(&pll->con1, 1 << PLL_DSMPD_SHIFT); |
| 237 | /* Power down */ |
| 238 | rk_setreg(&pll->con1, 1 << PLL_PD_SHIFT); |
| 239 | |
| 240 | rk_clrsetreg(&pll->con0, |
| 241 | PLL_POSTDIV1_MASK | PLL_FBDIV_MASK, |
| 242 | (rate->postdiv1 << PLL_POSTDIV1_SHIFT) | rate->fbdiv); |
| 243 | rk_clrsetreg(&pll->con1, PLL_POSTDIV2_MASK | PLL_REFDIV_MASK, |
| 244 | (rate->postdiv2 << PLL_POSTDIV2_SHIFT | |
| 245 | rate->refdiv << PLL_REFDIV_SHIFT)); |
| 246 | |
| 247 | /* Power Up */ |
| 248 | rk_clrreg(&pll->con1, 1 << PLL_PD_SHIFT); |
| 249 | |
| 250 | /* waiting for pll lock */ |
| 251 | while (!(readl(&pll->con1) & (1 << PLL_LOCK_STATUS_SHIFT))) |
| 252 | udelay(1); |
| 253 | |
| 254 | rk_clrsetreg(mode, pll_mode_mask[pll_id], |
| 255 | PLLMUX_FROM_PLL << pll_mode_shift[pll_id]); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static uint32_t rkclk_pll_get_rate(struct px30_pll *pll, unsigned int *mode, |
| 261 | enum px30_pll_id pll_id) |
| 262 | { |
| 263 | u32 refdiv, fbdiv, postdiv1, postdiv2; |
| 264 | u32 con, shift, mask; |
| 265 | |
| 266 | con = readl(mode); |
| 267 | shift = pll_mode_shift[pll_id]; |
| 268 | mask = pll_mode_mask[pll_id]; |
| 269 | |
| 270 | switch ((con & mask) >> shift) { |
| 271 | case PLLMUX_FROM_XIN24M: |
| 272 | return OSC_HZ; |
| 273 | case PLLMUX_FROM_PLL: |
| 274 | /* normal mode */ |
| 275 | con = readl(&pll->con0); |
| 276 | postdiv1 = (con & PLL_POSTDIV1_MASK) >> PLL_POSTDIV1_SHIFT; |
| 277 | fbdiv = (con & PLL_FBDIV_MASK) >> PLL_FBDIV_SHIFT; |
| 278 | con = readl(&pll->con1); |
| 279 | postdiv2 = (con & PLL_POSTDIV2_MASK) >> PLL_POSTDIV2_SHIFT; |
| 280 | refdiv = (con & PLL_REFDIV_MASK) >> PLL_REFDIV_SHIFT; |
| 281 | return (24 * fbdiv / (refdiv * postdiv1 * postdiv2)) * 1000000; |
| 282 | case PLLMUX_FROM_RTC32K: |
| 283 | default: |
| 284 | return 32768; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static ulong px30_i2c_get_clk(struct px30_clk_priv *priv, ulong clk_id) |
| 289 | { |
| 290 | struct px30_cru *cru = priv->cru; |
| 291 | u32 div, con; |
| 292 | |
| 293 | switch (clk_id) { |
| 294 | case SCLK_I2C0: |
| 295 | con = readl(&cru->clksel_con[49]); |
| 296 | div = con >> CLK_I2C0_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK; |
| 297 | break; |
| 298 | case SCLK_I2C1: |
| 299 | con = readl(&cru->clksel_con[49]); |
| 300 | div = con >> CLK_I2C1_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK; |
| 301 | break; |
| 302 | case SCLK_I2C2: |
| 303 | con = readl(&cru->clksel_con[50]); |
| 304 | div = con >> CLK_I2C2_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK; |
| 305 | break; |
| 306 | case SCLK_I2C3: |
| 307 | con = readl(&cru->clksel_con[50]); |
| 308 | div = con >> CLK_I2C3_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK; |
| 309 | break; |
| 310 | default: |
| 311 | printf("do not support this i2c bus\n"); |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | |
| 315 | return DIV_TO_RATE(priv->gpll_hz, div); |
| 316 | } |
| 317 | |
| 318 | static ulong px30_i2c_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz) |
| 319 | { |
| 320 | struct px30_cru *cru = priv->cru; |
| 321 | int src_clk_div; |
| 322 | |
| 323 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz); |
| 324 | assert(src_clk_div - 1 <= 127); |
| 325 | |
| 326 | switch (clk_id) { |
| 327 | case SCLK_I2C0: |
| 328 | rk_clrsetreg(&cru->clksel_con[49], |
| 329 | CLK_I2C_DIV_CON_MASK << CLK_I2C0_DIV_CON_SHIFT | |
| 330 | CLK_I2C_PLL_SEL_MASK << CLK_I2C0_PLL_SEL_SHIFT, |
| 331 | (src_clk_div - 1) << CLK_I2C0_DIV_CON_SHIFT | |
| 332 | CLK_I2C_PLL_SEL_GPLL << CLK_I2C0_PLL_SEL_SHIFT); |
| 333 | break; |
| 334 | case SCLK_I2C1: |
| 335 | rk_clrsetreg(&cru->clksel_con[49], |
| 336 | CLK_I2C_DIV_CON_MASK << CLK_I2C1_DIV_CON_SHIFT | |
| 337 | CLK_I2C_PLL_SEL_MASK << CLK_I2C1_PLL_SEL_SHIFT, |
| 338 | (src_clk_div - 1) << CLK_I2C1_DIV_CON_SHIFT | |
| 339 | CLK_I2C_PLL_SEL_GPLL << CLK_I2C1_PLL_SEL_SHIFT); |
| 340 | break; |
| 341 | case SCLK_I2C2: |
| 342 | rk_clrsetreg(&cru->clksel_con[50], |
| 343 | CLK_I2C_DIV_CON_MASK << CLK_I2C2_DIV_CON_SHIFT | |
| 344 | CLK_I2C_PLL_SEL_MASK << CLK_I2C2_PLL_SEL_SHIFT, |
| 345 | (src_clk_div - 1) << CLK_I2C2_DIV_CON_SHIFT | |
| 346 | CLK_I2C_PLL_SEL_GPLL << CLK_I2C2_PLL_SEL_SHIFT); |
| 347 | break; |
| 348 | case SCLK_I2C3: |
| 349 | rk_clrsetreg(&cru->clksel_con[50], |
| 350 | CLK_I2C_DIV_CON_MASK << CLK_I2C3_DIV_CON_SHIFT | |
| 351 | CLK_I2C_PLL_SEL_MASK << CLK_I2C3_PLL_SEL_SHIFT, |
| 352 | (src_clk_div - 1) << CLK_I2C3_DIV_CON_SHIFT | |
| 353 | CLK_I2C_PLL_SEL_GPLL << CLK_I2C3_PLL_SEL_SHIFT); |
| 354 | break; |
| 355 | default: |
| 356 | printf("do not support this i2c bus\n"); |
| 357 | return -EINVAL; |
| 358 | } |
| 359 | |
| 360 | return px30_i2c_get_clk(priv, clk_id); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * calculate best rational approximation for a given fraction |
| 365 | * taking into account restricted register size, e.g. to find |
| 366 | * appropriate values for a pll with 5 bit denominator and |
| 367 | * 8 bit numerator register fields, trying to set up with a |
| 368 | * frequency ratio of 3.1415, one would say: |
| 369 | * |
| 370 | * rational_best_approximation(31415, 10000, |
| 371 | * (1 << 8) - 1, (1 << 5) - 1, &n, &d); |
| 372 | * |
| 373 | * you may look at given_numerator as a fixed point number, |
| 374 | * with the fractional part size described in given_denominator. |
| 375 | * |
| 376 | * for theoretical background, see: |
| 377 | * http://en.wikipedia.org/wiki/Continued_fraction |
| 378 | */ |
| 379 | static void rational_best_approximation(unsigned long given_numerator, |
| 380 | unsigned long given_denominator, |
| 381 | unsigned long max_numerator, |
| 382 | unsigned long max_denominator, |
| 383 | unsigned long *best_numerator, |
| 384 | unsigned long *best_denominator) |
| 385 | { |
| 386 | unsigned long n, d, n0, d0, n1, d1; |
| 387 | |
| 388 | n = given_numerator; |
| 389 | d = given_denominator; |
| 390 | n0 = 0; |
| 391 | d1 = 0; |
| 392 | n1 = 1; |
| 393 | d0 = 1; |
| 394 | for (;;) { |
| 395 | unsigned long t, a; |
| 396 | |
| 397 | if (n1 > max_numerator || d1 > max_denominator) { |
| 398 | n1 = n0; |
| 399 | d1 = d0; |
| 400 | break; |
| 401 | } |
| 402 | if (d == 0) |
| 403 | break; |
| 404 | t = d; |
| 405 | a = n / d; |
| 406 | d = n % d; |
| 407 | n = t; |
| 408 | t = n0 + a * n1; |
| 409 | n0 = n1; |
| 410 | n1 = t; |
| 411 | t = d0 + a * d1; |
| 412 | d0 = d1; |
| 413 | d1 = t; |
| 414 | } |
| 415 | *best_numerator = n1; |
| 416 | *best_denominator = d1; |
| 417 | } |
| 418 | |
| 419 | static ulong px30_i2s_get_clk(struct px30_clk_priv *priv, ulong clk_id) |
| 420 | { |
| 421 | u32 con, fracdiv, gate; |
| 422 | u32 clk_src = priv->gpll_hz / 2; |
| 423 | unsigned long m, n; |
| 424 | struct px30_cru *cru = priv->cru; |
| 425 | |
| 426 | switch (clk_id) { |
| 427 | case SCLK_I2S1: |
| 428 | con = readl(&cru->clksel_con[30]); |
| 429 | fracdiv = readl(&cru->clksel_con[31]); |
| 430 | gate = readl(&cru->clkgate_con[10]); |
| 431 | m = fracdiv & CLK_I2S1_FRAC_NUMERATOR_MASK; |
| 432 | m >>= CLK_I2S1_FRAC_NUMERATOR_SHIFT; |
| 433 | n = fracdiv & CLK_I2S1_FRAC_DENOMINATOR_MASK; |
| 434 | n >>= CLK_I2S1_FRAC_DENOMINATOR_SHIFT; |
| 435 | debug("con30: 0x%x, gate: 0x%x, frac: 0x%x\n", |
| 436 | con, gate, fracdiv); |
| 437 | break; |
| 438 | default: |
| 439 | printf("do not support this i2s bus\n"); |
| 440 | return -EINVAL; |
| 441 | } |
| 442 | |
| 443 | return clk_src * n / m; |
| 444 | } |
| 445 | |
| 446 | static ulong px30_i2s_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz) |
| 447 | { |
| 448 | u32 clk_src; |
| 449 | unsigned long m, n, val; |
| 450 | struct px30_cru *cru = priv->cru; |
| 451 | |
| 452 | clk_src = priv->gpll_hz / 2; |
| 453 | rational_best_approximation(hz, clk_src, |
| 454 | GENMASK(16 - 1, 0), |
| 455 | GENMASK(16 - 1, 0), |
| 456 | &m, &n); |
| 457 | switch (clk_id) { |
| 458 | case SCLK_I2S1: |
| 459 | rk_clrsetreg(&cru->clksel_con[30], |
| 460 | CLK_I2S1_PLL_SEL_MASK, CLK_I2S1_PLL_SEL_GPLL); |
| 461 | rk_clrsetreg(&cru->clksel_con[30], |
| 462 | CLK_I2S1_DIV_CON_MASK, 0x1); |
| 463 | rk_clrsetreg(&cru->clksel_con[30], |
| 464 | CLK_I2S1_SEL_MASK, CLK_I2S1_SEL_FRAC); |
| 465 | val = m << CLK_I2S1_FRAC_NUMERATOR_SHIFT | n; |
| 466 | writel(val, &cru->clksel_con[31]); |
| 467 | rk_clrsetreg(&cru->clkgate_con[10], |
| 468 | CLK_I2S1_OUT_MCLK_PAD_MASK, |
| 469 | CLK_I2S1_OUT_MCLK_PAD_ENABLE); |
| 470 | break; |
| 471 | default: |
| 472 | printf("do not support this i2s bus\n"); |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
| 476 | return px30_i2s_get_clk(priv, clk_id); |
| 477 | } |
| 478 | |
| 479 | static ulong px30_nandc_get_clk(struct px30_clk_priv *priv) |
| 480 | { |
| 481 | struct px30_cru *cru = priv->cru; |
| 482 | u32 div, con; |
| 483 | |
| 484 | con = readl(&cru->clksel_con[15]); |
| 485 | div = (con & NANDC_DIV_MASK) >> NANDC_DIV_SHIFT; |
| 486 | |
| 487 | return DIV_TO_RATE(priv->gpll_hz, div); |
| 488 | } |
| 489 | |
| 490 | static ulong px30_nandc_set_clk(struct px30_clk_priv *priv, |
| 491 | ulong set_rate) |
| 492 | { |
| 493 | struct px30_cru *cru = priv->cru; |
| 494 | int src_clk_div; |
| 495 | |
| 496 | /* Select nandc source from GPLL by default */ |
| 497 | /* nandc clock defaulg div 2 internal, need provide double in cru */ |
| 498 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, set_rate); |
| 499 | assert(src_clk_div - 1 <= 31); |
| 500 | |
| 501 | rk_clrsetreg(&cru->clksel_con[15], |
| 502 | NANDC_CLK_SEL_MASK | NANDC_PLL_MASK | |
| 503 | NANDC_DIV_MASK, |
| 504 | NANDC_CLK_SEL_NANDC << NANDC_CLK_SEL_SHIFT | |
| 505 | NANDC_SEL_GPLL << NANDC_PLL_SHIFT | |
| 506 | (src_clk_div - 1) << NANDC_DIV_SHIFT); |
| 507 | |
| 508 | return px30_nandc_get_clk(priv); |
| 509 | } |
| 510 | |
| 511 | static ulong px30_mmc_get_clk(struct px30_clk_priv *priv, uint clk_id) |
| 512 | { |
| 513 | struct px30_cru *cru = priv->cru; |
| 514 | u32 div, con, con_id; |
| 515 | |
| 516 | switch (clk_id) { |
| 517 | case HCLK_SDMMC: |
| 518 | case SCLK_SDMMC: |
| 519 | con_id = 16; |
| 520 | break; |
| 521 | case HCLK_EMMC: |
| 522 | case SCLK_EMMC: |
| 523 | case SCLK_EMMC_SAMPLE: |
| 524 | con_id = 20; |
| 525 | break; |
| 526 | default: |
| 527 | return -EINVAL; |
| 528 | } |
| 529 | |
| 530 | con = readl(&cru->clksel_con[con_id]); |
| 531 | div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT; |
| 532 | |
| 533 | if ((con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT |
| 534 | == EMMC_SEL_24M) |
| 535 | return DIV_TO_RATE(OSC_HZ, div) / 2; |
| 536 | else |
| 537 | return DIV_TO_RATE(priv->gpll_hz, div) / 2; |
| 538 | } |
| 539 | |
| 540 | static ulong px30_mmc_set_clk(struct px30_clk_priv *priv, |
| 541 | ulong clk_id, ulong set_rate) |
| 542 | { |
| 543 | struct px30_cru *cru = priv->cru; |
| 544 | int src_clk_div; |
| 545 | u32 con_id; |
| 546 | |
| 547 | switch (clk_id) { |
| 548 | case HCLK_SDMMC: |
| 549 | case SCLK_SDMMC: |
| 550 | con_id = 16; |
| 551 | break; |
| 552 | case HCLK_EMMC: |
| 553 | case SCLK_EMMC: |
| 554 | con_id = 20; |
| 555 | break; |
| 556 | default: |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | |
| 560 | /* Select clk_sdmmc/emmc source from GPLL by default */ |
| 561 | /* mmc clock defaulg div 2 internal, need provide double in cru */ |
| 562 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz / 2, set_rate); |
| 563 | |
| 564 | if (src_clk_div > 127) { |
| 565 | /* use 24MHz source for 400KHz clock */ |
| 566 | src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate); |
| 567 | rk_clrsetreg(&cru->clksel_con[con_id], |
| 568 | EMMC_PLL_MASK | EMMC_DIV_MASK, |
| 569 | EMMC_SEL_24M << EMMC_PLL_SHIFT | |
| 570 | (src_clk_div - 1) << EMMC_DIV_SHIFT); |
| 571 | } else { |
| 572 | rk_clrsetreg(&cru->clksel_con[con_id], |
| 573 | EMMC_PLL_MASK | EMMC_DIV_MASK, |
| 574 | EMMC_SEL_GPLL << EMMC_PLL_SHIFT | |
| 575 | (src_clk_div - 1) << EMMC_DIV_SHIFT); |
| 576 | } |
| 577 | rk_clrsetreg(&cru->clksel_con[con_id + 1], EMMC_CLK_SEL_MASK, |
| 578 | EMMC_CLK_SEL_EMMC); |
| 579 | |
| 580 | return px30_mmc_get_clk(priv, clk_id); |
| 581 | } |
| 582 | |
Jon Lin | 8f20e73 | 2021-08-05 16:27:53 +0800 | [diff] [blame] | 583 | static ulong px30_sfc_get_clk(struct px30_clk_priv *priv, uint clk_id) |
| 584 | { |
| 585 | struct px30_cru *cru = priv->cru; |
| 586 | u32 div, con; |
| 587 | |
| 588 | con = readl(&cru->clksel_con[22]); |
| 589 | div = (con & SFC_DIV_CON_MASK) >> SFC_DIV_CON_SHIFT; |
| 590 | |
| 591 | return DIV_TO_RATE(priv->gpll_hz, div); |
| 592 | } |
| 593 | |
| 594 | static ulong px30_sfc_set_clk(struct px30_clk_priv *priv, |
| 595 | ulong clk_id, ulong set_rate) |
| 596 | { |
| 597 | struct px30_cru *cru = priv->cru; |
| 598 | int src_clk_div; |
| 599 | |
| 600 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, set_rate); |
| 601 | rk_clrsetreg(&cru->clksel_con[22], |
| 602 | SFC_PLL_SEL_MASK | SFC_DIV_CON_MASK, |
| 603 | 0 << SFC_PLL_SEL_SHIFT | |
| 604 | (src_clk_div - 1) << SFC_DIV_CON_SHIFT); |
| 605 | |
| 606 | return px30_sfc_get_clk(priv, clk_id); |
| 607 | } |
| 608 | |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 609 | static ulong px30_pwm_get_clk(struct px30_clk_priv *priv, ulong clk_id) |
| 610 | { |
| 611 | struct px30_cru *cru = priv->cru; |
| 612 | u32 div, con; |
| 613 | |
| 614 | switch (clk_id) { |
| 615 | case SCLK_PWM0: |
| 616 | con = readl(&cru->clksel_con[52]); |
| 617 | div = con >> CLK_PWM0_DIV_CON_SHIFT & CLK_PWM_DIV_CON_MASK; |
| 618 | break; |
| 619 | case SCLK_PWM1: |
| 620 | con = readl(&cru->clksel_con[52]); |
| 621 | div = con >> CLK_PWM1_DIV_CON_SHIFT & CLK_PWM_DIV_CON_MASK; |
| 622 | break; |
| 623 | default: |
| 624 | printf("do not support this pwm bus\n"); |
| 625 | return -EINVAL; |
| 626 | } |
| 627 | |
| 628 | return DIV_TO_RATE(priv->gpll_hz, div); |
| 629 | } |
| 630 | |
| 631 | static ulong px30_pwm_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz) |
| 632 | { |
| 633 | struct px30_cru *cru = priv->cru; |
| 634 | int src_clk_div; |
| 635 | |
| 636 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz); |
| 637 | assert(src_clk_div - 1 <= 127); |
| 638 | |
| 639 | switch (clk_id) { |
| 640 | case SCLK_PWM0: |
| 641 | rk_clrsetreg(&cru->clksel_con[52], |
| 642 | CLK_PWM_DIV_CON_MASK << CLK_PWM0_DIV_CON_SHIFT | |
| 643 | CLK_PWM_PLL_SEL_MASK << CLK_PWM0_PLL_SEL_SHIFT, |
| 644 | (src_clk_div - 1) << CLK_PWM0_DIV_CON_SHIFT | |
| 645 | CLK_PWM_PLL_SEL_GPLL << CLK_PWM0_PLL_SEL_SHIFT); |
| 646 | break; |
| 647 | case SCLK_PWM1: |
| 648 | rk_clrsetreg(&cru->clksel_con[52], |
| 649 | CLK_PWM_DIV_CON_MASK << CLK_PWM1_DIV_CON_SHIFT | |
| 650 | CLK_PWM_PLL_SEL_MASK << CLK_PWM1_PLL_SEL_SHIFT, |
| 651 | (src_clk_div - 1) << CLK_PWM1_DIV_CON_SHIFT | |
| 652 | CLK_PWM_PLL_SEL_GPLL << CLK_PWM1_PLL_SEL_SHIFT); |
| 653 | break; |
| 654 | default: |
| 655 | printf("do not support this pwm bus\n"); |
| 656 | return -EINVAL; |
| 657 | } |
| 658 | |
| 659 | return px30_pwm_get_clk(priv, clk_id); |
| 660 | } |
| 661 | |
| 662 | static ulong px30_saradc_get_clk(struct px30_clk_priv *priv) |
| 663 | { |
| 664 | struct px30_cru *cru = priv->cru; |
| 665 | u32 div, con; |
| 666 | |
| 667 | con = readl(&cru->clksel_con[55]); |
| 668 | div = con >> CLK_SARADC_DIV_CON_SHIFT & CLK_SARADC_DIV_CON_MASK; |
| 669 | |
| 670 | return DIV_TO_RATE(OSC_HZ, div); |
| 671 | } |
| 672 | |
| 673 | static ulong px30_saradc_set_clk(struct px30_clk_priv *priv, uint hz) |
| 674 | { |
| 675 | struct px30_cru *cru = priv->cru; |
| 676 | int src_clk_div; |
| 677 | |
| 678 | src_clk_div = DIV_ROUND_UP(OSC_HZ, hz); |
| 679 | assert(src_clk_div - 1 <= 2047); |
| 680 | |
| 681 | rk_clrsetreg(&cru->clksel_con[55], |
| 682 | CLK_SARADC_DIV_CON_MASK, |
| 683 | (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT); |
| 684 | |
| 685 | return px30_saradc_get_clk(priv); |
| 686 | } |
| 687 | |
| 688 | static ulong px30_tsadc_get_clk(struct px30_clk_priv *priv) |
| 689 | { |
| 690 | struct px30_cru *cru = priv->cru; |
| 691 | u32 div, con; |
| 692 | |
| 693 | con = readl(&cru->clksel_con[54]); |
| 694 | div = con >> CLK_SARADC_DIV_CON_SHIFT & CLK_SARADC_DIV_CON_MASK; |
| 695 | |
| 696 | return DIV_TO_RATE(OSC_HZ, div); |
| 697 | } |
| 698 | |
| 699 | static ulong px30_tsadc_set_clk(struct px30_clk_priv *priv, uint hz) |
| 700 | { |
| 701 | struct px30_cru *cru = priv->cru; |
| 702 | int src_clk_div; |
| 703 | |
| 704 | src_clk_div = DIV_ROUND_UP(OSC_HZ, hz); |
| 705 | assert(src_clk_div - 1 <= 2047); |
| 706 | |
| 707 | rk_clrsetreg(&cru->clksel_con[54], |
| 708 | CLK_SARADC_DIV_CON_MASK, |
| 709 | (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT); |
| 710 | |
| 711 | return px30_tsadc_get_clk(priv); |
| 712 | } |
| 713 | |
| 714 | static ulong px30_spi_get_clk(struct px30_clk_priv *priv, ulong clk_id) |
| 715 | { |
| 716 | struct px30_cru *cru = priv->cru; |
| 717 | u32 div, con; |
| 718 | |
| 719 | switch (clk_id) { |
| 720 | case SCLK_SPI0: |
| 721 | con = readl(&cru->clksel_con[53]); |
| 722 | div = con >> CLK_SPI0_DIV_CON_SHIFT & CLK_SPI_DIV_CON_MASK; |
| 723 | break; |
| 724 | case SCLK_SPI1: |
| 725 | con = readl(&cru->clksel_con[53]); |
| 726 | div = con >> CLK_SPI1_DIV_CON_SHIFT & CLK_SPI_DIV_CON_MASK; |
| 727 | break; |
| 728 | default: |
| 729 | printf("do not support this pwm bus\n"); |
| 730 | return -EINVAL; |
| 731 | } |
| 732 | |
| 733 | return DIV_TO_RATE(priv->gpll_hz, div); |
| 734 | } |
| 735 | |
| 736 | static ulong px30_spi_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz) |
| 737 | { |
| 738 | struct px30_cru *cru = priv->cru; |
| 739 | int src_clk_div; |
| 740 | |
| 741 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz); |
| 742 | assert(src_clk_div - 1 <= 127); |
| 743 | |
| 744 | switch (clk_id) { |
| 745 | case SCLK_SPI0: |
| 746 | rk_clrsetreg(&cru->clksel_con[53], |
| 747 | CLK_SPI_DIV_CON_MASK << CLK_SPI0_DIV_CON_SHIFT | |
| 748 | CLK_SPI_PLL_SEL_MASK << CLK_SPI0_PLL_SEL_SHIFT, |
| 749 | (src_clk_div - 1) << CLK_SPI0_DIV_CON_SHIFT | |
| 750 | CLK_SPI_PLL_SEL_GPLL << CLK_SPI0_PLL_SEL_SHIFT); |
| 751 | break; |
| 752 | case SCLK_SPI1: |
| 753 | rk_clrsetreg(&cru->clksel_con[53], |
| 754 | CLK_SPI_DIV_CON_MASK << CLK_SPI1_DIV_CON_SHIFT | |
| 755 | CLK_SPI_PLL_SEL_MASK << CLK_SPI1_PLL_SEL_SHIFT, |
| 756 | (src_clk_div - 1) << CLK_SPI1_DIV_CON_SHIFT | |
| 757 | CLK_SPI_PLL_SEL_GPLL << CLK_SPI1_PLL_SEL_SHIFT); |
| 758 | break; |
| 759 | default: |
| 760 | printf("do not support this pwm bus\n"); |
| 761 | return -EINVAL; |
| 762 | } |
| 763 | |
| 764 | return px30_spi_get_clk(priv, clk_id); |
| 765 | } |
| 766 | |
| 767 | static ulong px30_vop_get_clk(struct px30_clk_priv *priv, ulong clk_id) |
| 768 | { |
| 769 | struct px30_cru *cru = priv->cru; |
| 770 | u32 div, con, parent; |
| 771 | |
| 772 | switch (clk_id) { |
| 773 | case ACLK_VOPB: |
| 774 | case ACLK_VOPL: |
| 775 | con = readl(&cru->clksel_con[3]); |
| 776 | div = con & ACLK_VO_DIV_MASK; |
| 777 | parent = priv->gpll_hz; |
| 778 | break; |
| 779 | case DCLK_VOPB: |
| 780 | con = readl(&cru->clksel_con[5]); |
| 781 | div = con & DCLK_VOPB_DIV_MASK; |
| 782 | parent = rkclk_pll_get_rate(&cru->pll[CPLL], &cru->mode, CPLL); |
| 783 | break; |
| 784 | case DCLK_VOPL: |
| 785 | con = readl(&cru->clksel_con[8]); |
| 786 | div = con & DCLK_VOPL_DIV_MASK; |
| 787 | parent = rkclk_pll_get_rate(&cru->pll[NPLL], &cru->mode, NPLL); |
| 788 | break; |
| 789 | default: |
| 790 | return -ENOENT; |
| 791 | } |
| 792 | |
| 793 | return DIV_TO_RATE(parent, div); |
| 794 | } |
| 795 | |
| 796 | static ulong px30_vop_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz) |
| 797 | { |
| 798 | struct px30_cru *cru = priv->cru; |
| 799 | ulong npll_hz; |
| 800 | int src_clk_div; |
| 801 | |
| 802 | switch (clk_id) { |
| 803 | case ACLK_VOPB: |
| 804 | case ACLK_VOPL: |
| 805 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz); |
| 806 | assert(src_clk_div - 1 <= 31); |
| 807 | rk_clrsetreg(&cru->clksel_con[3], |
| 808 | ACLK_VO_PLL_MASK | ACLK_VO_DIV_MASK, |
| 809 | ACLK_VO_SEL_GPLL << ACLK_VO_PLL_SHIFT | |
| 810 | (src_clk_div - 1) << ACLK_VO_DIV_SHIFT); |
| 811 | break; |
| 812 | case DCLK_VOPB: |
| 813 | if (hz < PX30_VOP_PLL_LIMIT) { |
| 814 | src_clk_div = DIV_ROUND_UP(PX30_VOP_PLL_LIMIT, hz); |
| 815 | if (src_clk_div % 2) |
| 816 | src_clk_div = src_clk_div - 1; |
| 817 | } else { |
| 818 | src_clk_div = 1; |
| 819 | } |
| 820 | assert(src_clk_div - 1 <= 255); |
| 821 | rkclk_set_pll(&cru->pll[CPLL], &cru->mode, |
| 822 | CPLL, hz * src_clk_div); |
| 823 | rk_clrsetreg(&cru->clksel_con[5], |
| 824 | DCLK_VOPB_SEL_MASK | DCLK_VOPB_PLL_SEL_MASK | |
| 825 | DCLK_VOPB_DIV_MASK, |
| 826 | DCLK_VOPB_SEL_DIVOUT << DCLK_VOPB_SEL_SHIFT | |
| 827 | DCLK_VOPB_PLL_SEL_CPLL << DCLK_VOPB_PLL_SEL_SHIFT | |
| 828 | (src_clk_div - 1) << DCLK_VOPB_DIV_SHIFT); |
| 829 | break; |
| 830 | case DCLK_VOPL: |
| 831 | npll_hz = px30_clk_get_pll_rate(priv, NPLL); |
| 832 | if (npll_hz >= PX30_VOP_PLL_LIMIT && npll_hz >= hz && |
| 833 | npll_hz % hz == 0) { |
| 834 | src_clk_div = npll_hz / hz; |
| 835 | assert(src_clk_div - 1 <= 255); |
| 836 | } else { |
| 837 | if (hz < PX30_VOP_PLL_LIMIT) { |
| 838 | src_clk_div = DIV_ROUND_UP(PX30_VOP_PLL_LIMIT, |
| 839 | hz); |
| 840 | if (src_clk_div % 2) |
| 841 | src_clk_div = src_clk_div - 1; |
| 842 | } else { |
| 843 | src_clk_div = 1; |
| 844 | } |
| 845 | assert(src_clk_div - 1 <= 255); |
| 846 | rkclk_set_pll(&cru->pll[NPLL], &cru->mode, NPLL, |
| 847 | hz * src_clk_div); |
| 848 | } |
| 849 | rk_clrsetreg(&cru->clksel_con[8], |
| 850 | DCLK_VOPL_SEL_MASK | DCLK_VOPL_PLL_SEL_MASK | |
| 851 | DCLK_VOPL_DIV_MASK, |
| 852 | DCLK_VOPL_SEL_DIVOUT << DCLK_VOPL_SEL_SHIFT | |
| 853 | DCLK_VOPL_PLL_SEL_NPLL << DCLK_VOPL_PLL_SEL_SHIFT | |
| 854 | (src_clk_div - 1) << DCLK_VOPL_DIV_SHIFT); |
| 855 | break; |
| 856 | default: |
| 857 | printf("do not support this vop freq\n"); |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | |
| 861 | return px30_vop_get_clk(priv, clk_id); |
| 862 | } |
| 863 | |
| 864 | static ulong px30_bus_get_clk(struct px30_clk_priv *priv, ulong clk_id) |
| 865 | { |
| 866 | struct px30_cru *cru = priv->cru; |
| 867 | u32 div, con, parent; |
| 868 | |
| 869 | switch (clk_id) { |
| 870 | case ACLK_BUS_PRE: |
| 871 | con = readl(&cru->clksel_con[23]); |
| 872 | div = (con & BUS_ACLK_DIV_MASK) >> BUS_ACLK_DIV_SHIFT; |
| 873 | parent = priv->gpll_hz; |
| 874 | break; |
| 875 | case HCLK_BUS_PRE: |
| 876 | con = readl(&cru->clksel_con[24]); |
| 877 | div = (con & BUS_HCLK_DIV_MASK) >> BUS_HCLK_DIV_SHIFT; |
| 878 | parent = priv->gpll_hz; |
| 879 | break; |
| 880 | case PCLK_BUS_PRE: |
| 881 | case PCLK_WDT_NS: |
| 882 | parent = px30_bus_get_clk(priv, ACLK_BUS_PRE); |
| 883 | con = readl(&cru->clksel_con[24]); |
| 884 | div = (con & BUS_PCLK_DIV_MASK) >> BUS_PCLK_DIV_SHIFT; |
| 885 | break; |
| 886 | default: |
| 887 | return -ENOENT; |
| 888 | } |
| 889 | |
| 890 | return DIV_TO_RATE(parent, div); |
| 891 | } |
| 892 | |
| 893 | static ulong px30_bus_set_clk(struct px30_clk_priv *priv, ulong clk_id, |
| 894 | ulong hz) |
| 895 | { |
| 896 | struct px30_cru *cru = priv->cru; |
| 897 | int src_clk_div; |
| 898 | |
| 899 | /* |
| 900 | * select gpll as pd_bus bus clock source and |
| 901 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 902 | */ |
| 903 | switch (clk_id) { |
| 904 | case ACLK_BUS_PRE: |
| 905 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz); |
| 906 | assert(src_clk_div - 1 <= 31); |
| 907 | rk_clrsetreg(&cru->clksel_con[23], |
| 908 | BUS_PLL_SEL_MASK | BUS_ACLK_DIV_MASK, |
| 909 | BUS_PLL_SEL_GPLL << BUS_PLL_SEL_SHIFT | |
| 910 | (src_clk_div - 1) << BUS_ACLK_DIV_SHIFT); |
| 911 | break; |
| 912 | case HCLK_BUS_PRE: |
| 913 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz); |
| 914 | assert(src_clk_div - 1 <= 31); |
| 915 | rk_clrsetreg(&cru->clksel_con[24], |
| 916 | BUS_PLL_SEL_MASK | BUS_HCLK_DIV_MASK, |
| 917 | BUS_PLL_SEL_GPLL << BUS_PLL_SEL_SHIFT | |
| 918 | (src_clk_div - 1) << BUS_HCLK_DIV_SHIFT); |
| 919 | break; |
| 920 | case PCLK_BUS_PRE: |
| 921 | src_clk_div = |
| 922 | DIV_ROUND_UP(px30_bus_get_clk(priv, ACLK_BUS_PRE), hz); |
| 923 | assert(src_clk_div - 1 <= 3); |
| 924 | rk_clrsetreg(&cru->clksel_con[24], |
| 925 | BUS_PCLK_DIV_MASK, |
| 926 | (src_clk_div - 1) << BUS_PCLK_DIV_SHIFT); |
| 927 | break; |
| 928 | default: |
| 929 | printf("do not support this bus freq\n"); |
| 930 | return -EINVAL; |
| 931 | } |
| 932 | |
| 933 | return px30_bus_get_clk(priv, clk_id); |
| 934 | } |
| 935 | |
| 936 | static ulong px30_peri_get_clk(struct px30_clk_priv *priv, ulong clk_id) |
| 937 | { |
| 938 | struct px30_cru *cru = priv->cru; |
| 939 | u32 div, con, parent; |
| 940 | |
| 941 | switch (clk_id) { |
| 942 | case ACLK_PERI_PRE: |
| 943 | con = readl(&cru->clksel_con[14]); |
| 944 | div = (con & PERI_ACLK_DIV_MASK) >> PERI_ACLK_DIV_SHIFT; |
| 945 | parent = priv->gpll_hz; |
| 946 | break; |
| 947 | case HCLK_PERI_PRE: |
| 948 | con = readl(&cru->clksel_con[14]); |
| 949 | div = (con & PERI_HCLK_DIV_MASK) >> PERI_HCLK_DIV_SHIFT; |
| 950 | parent = priv->gpll_hz; |
| 951 | break; |
| 952 | default: |
| 953 | return -ENOENT; |
| 954 | } |
| 955 | |
| 956 | return DIV_TO_RATE(parent, div); |
| 957 | } |
| 958 | |
| 959 | static ulong px30_peri_set_clk(struct px30_clk_priv *priv, ulong clk_id, |
| 960 | ulong hz) |
| 961 | { |
| 962 | struct px30_cru *cru = priv->cru; |
| 963 | int src_clk_div; |
| 964 | |
| 965 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz); |
| 966 | assert(src_clk_div - 1 <= 31); |
| 967 | |
| 968 | /* |
| 969 | * select gpll as pd_peri bus clock source and |
| 970 | * set up dependent divisors for HCLK and ACLK clocks. |
| 971 | */ |
| 972 | switch (clk_id) { |
| 973 | case ACLK_PERI_PRE: |
| 974 | rk_clrsetreg(&cru->clksel_con[14], |
| 975 | PERI_PLL_SEL_MASK | PERI_ACLK_DIV_MASK, |
| 976 | PERI_PLL_GPLL << PERI_PLL_SEL_SHIFT | |
| 977 | (src_clk_div - 1) << PERI_ACLK_DIV_SHIFT); |
| 978 | break; |
| 979 | case HCLK_PERI_PRE: |
| 980 | rk_clrsetreg(&cru->clksel_con[14], |
| 981 | PERI_PLL_SEL_MASK | PERI_HCLK_DIV_MASK, |
| 982 | PERI_PLL_GPLL << PERI_PLL_SEL_SHIFT | |
| 983 | (src_clk_div - 1) << PERI_HCLK_DIV_SHIFT); |
| 984 | break; |
| 985 | default: |
| 986 | printf("do not support this peri freq\n"); |
| 987 | return -EINVAL; |
| 988 | } |
| 989 | |
| 990 | return px30_peri_get_clk(priv, clk_id); |
| 991 | } |
| 992 | |
| 993 | #ifndef CONFIG_SPL_BUILD |
| 994 | static ulong px30_crypto_get_clk(struct px30_clk_priv *priv, ulong clk_id) |
| 995 | { |
| 996 | struct px30_cru *cru = priv->cru; |
| 997 | u32 div, con, parent; |
| 998 | |
| 999 | switch (clk_id) { |
| 1000 | case SCLK_CRYPTO: |
| 1001 | con = readl(&cru->clksel_con[25]); |
| 1002 | div = (con & CRYPTO_DIV_MASK) >> CRYPTO_DIV_SHIFT; |
| 1003 | parent = priv->gpll_hz; |
| 1004 | break; |
| 1005 | case SCLK_CRYPTO_APK: |
| 1006 | con = readl(&cru->clksel_con[25]); |
| 1007 | div = (con & CRYPTO_APK_DIV_MASK) >> CRYPTO_APK_DIV_SHIFT; |
| 1008 | parent = priv->gpll_hz; |
| 1009 | break; |
| 1010 | default: |
| 1011 | return -ENOENT; |
| 1012 | } |
| 1013 | |
| 1014 | return DIV_TO_RATE(parent, div); |
| 1015 | } |
| 1016 | |
| 1017 | static ulong px30_crypto_set_clk(struct px30_clk_priv *priv, ulong clk_id, |
| 1018 | ulong hz) |
| 1019 | { |
| 1020 | struct px30_cru *cru = priv->cru; |
| 1021 | int src_clk_div; |
| 1022 | |
| 1023 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz); |
| 1024 | assert(src_clk_div - 1 <= 31); |
| 1025 | |
| 1026 | /* |
| 1027 | * select gpll as crypto clock source and |
| 1028 | * set up dependent divisors for crypto clocks. |
| 1029 | */ |
| 1030 | switch (clk_id) { |
| 1031 | case SCLK_CRYPTO: |
| 1032 | rk_clrsetreg(&cru->clksel_con[25], |
| 1033 | CRYPTO_PLL_SEL_MASK | CRYPTO_DIV_MASK, |
| 1034 | CRYPTO_PLL_SEL_GPLL << CRYPTO_PLL_SEL_SHIFT | |
| 1035 | (src_clk_div - 1) << CRYPTO_DIV_SHIFT); |
| 1036 | break; |
| 1037 | case SCLK_CRYPTO_APK: |
| 1038 | rk_clrsetreg(&cru->clksel_con[25], |
| 1039 | CRYPTO_APK_PLL_SEL_MASK | CRYPTO_APK_DIV_MASK, |
| 1040 | CRYPTO_PLL_SEL_GPLL << CRYPTO_APK_SEL_SHIFT | |
| 1041 | (src_clk_div - 1) << CRYPTO_APK_DIV_SHIFT); |
| 1042 | break; |
| 1043 | default: |
| 1044 | printf("do not support this peri freq\n"); |
| 1045 | return -EINVAL; |
| 1046 | } |
| 1047 | |
| 1048 | return px30_crypto_get_clk(priv, clk_id); |
| 1049 | } |
| 1050 | |
| 1051 | static ulong px30_i2s1_mclk_get_clk(struct px30_clk_priv *priv, ulong clk_id) |
| 1052 | { |
| 1053 | struct px30_cru *cru = priv->cru; |
| 1054 | u32 con; |
| 1055 | |
| 1056 | con = readl(&cru->clksel_con[30]); |
| 1057 | |
| 1058 | if (!(con & CLK_I2S1_OUT_SEL_MASK)) |
| 1059 | return -ENOENT; |
| 1060 | |
| 1061 | return 12000000; |
| 1062 | } |
| 1063 | |
| 1064 | static ulong px30_i2s1_mclk_set_clk(struct px30_clk_priv *priv, ulong clk_id, |
| 1065 | ulong hz) |
| 1066 | { |
| 1067 | struct px30_cru *cru = priv->cru; |
| 1068 | |
| 1069 | if (hz != 12000000) { |
| 1070 | printf("do not support this i2s1_mclk freq\n"); |
| 1071 | return -EINVAL; |
| 1072 | } |
| 1073 | |
| 1074 | rk_clrsetreg(&cru->clksel_con[30], CLK_I2S1_OUT_SEL_MASK, |
| 1075 | CLK_I2S1_OUT_SEL_OSC); |
| 1076 | rk_clrsetreg(&cru->clkgate_con[10], CLK_I2S1_OUT_MCLK_PAD_MASK, |
| 1077 | CLK_I2S1_OUT_MCLK_PAD_ENABLE); |
| 1078 | |
| 1079 | return px30_i2s1_mclk_get_clk(priv, clk_id); |
| 1080 | } |
| 1081 | |
| 1082 | static ulong px30_mac_set_clk(struct px30_clk_priv *priv, uint hz) |
| 1083 | { |
| 1084 | struct px30_cru *cru = priv->cru; |
| 1085 | u32 con = readl(&cru->clksel_con[22]); |
| 1086 | ulong pll_rate; |
| 1087 | u8 div; |
| 1088 | |
| 1089 | if ((con >> GMAC_PLL_SEL_SHIFT) & GMAC_PLL_SEL_CPLL) |
| 1090 | pll_rate = px30_clk_get_pll_rate(priv, CPLL); |
| 1091 | else if ((con >> GMAC_PLL_SEL_SHIFT) & GMAC_PLL_SEL_NPLL) |
| 1092 | pll_rate = px30_clk_get_pll_rate(priv, NPLL); |
| 1093 | else |
| 1094 | pll_rate = priv->gpll_hz; |
| 1095 | |
| 1096 | /*default set 50MHZ for gmac*/ |
| 1097 | if (!hz) |
| 1098 | hz = 50000000; |
| 1099 | |
| 1100 | div = DIV_ROUND_UP(pll_rate, hz) - 1; |
| 1101 | assert(div < 32); |
| 1102 | rk_clrsetreg(&cru->clksel_con[22], CLK_GMAC_DIV_MASK, |
| 1103 | div << CLK_GMAC_DIV_SHIFT); |
| 1104 | |
| 1105 | return DIV_TO_RATE(pll_rate, div); |
| 1106 | } |
| 1107 | |
| 1108 | static int px30_mac_set_speed_clk(struct px30_clk_priv *priv, uint hz) |
| 1109 | { |
| 1110 | struct px30_cru *cru = priv->cru; |
| 1111 | |
| 1112 | if (hz != 2500000 && hz != 25000000) { |
| 1113 | debug("Unsupported mac speed:%d\n", hz); |
| 1114 | return -EINVAL; |
| 1115 | } |
| 1116 | |
| 1117 | rk_clrsetreg(&cru->clksel_con[23], RMII_CLK_SEL_MASK, |
| 1118 | ((hz == 2500000) ? 0 : 1) << RMII_CLK_SEL_SHIFT); |
| 1119 | |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | #endif |
| 1124 | |
| 1125 | static ulong px30_clk_get_pll_rate(struct px30_clk_priv *priv, |
| 1126 | enum px30_pll_id pll_id) |
| 1127 | { |
| 1128 | struct px30_cru *cru = priv->cru; |
| 1129 | |
| 1130 | return rkclk_pll_get_rate(&cru->pll[pll_id], &cru->mode, pll_id); |
| 1131 | } |
| 1132 | |
| 1133 | static ulong px30_clk_set_pll_rate(struct px30_clk_priv *priv, |
| 1134 | enum px30_pll_id pll_id, ulong hz) |
| 1135 | { |
| 1136 | struct px30_cru *cru = priv->cru; |
| 1137 | |
| 1138 | if (rkclk_set_pll(&cru->pll[pll_id], &cru->mode, pll_id, hz)) |
| 1139 | return -EINVAL; |
| 1140 | return rkclk_pll_get_rate(&cru->pll[pll_id], &cru->mode, pll_id); |
| 1141 | } |
| 1142 | |
| 1143 | static ulong px30_armclk_set_clk(struct px30_clk_priv *priv, ulong hz) |
| 1144 | { |
| 1145 | struct px30_cru *cru = priv->cru; |
| 1146 | const struct cpu_rate_table *rate; |
| 1147 | ulong old_rate; |
| 1148 | |
| 1149 | rate = get_cpu_settings(hz); |
| 1150 | if (!rate) { |
| 1151 | printf("%s unsupport rate\n", __func__); |
| 1152 | return -EINVAL; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
| 1156 | * select apll as cpu/core clock pll source and |
| 1157 | * set up dependent divisors for PERI and ACLK clocks. |
| 1158 | * core hz : apll = 1:1 |
| 1159 | */ |
| 1160 | old_rate = px30_clk_get_pll_rate(priv, APLL); |
| 1161 | if (old_rate > hz) { |
| 1162 | if (rkclk_set_pll(&cru->pll[APLL], &cru->mode, APLL, hz)) |
| 1163 | return -EINVAL; |
| 1164 | rk_clrsetreg(&cru->clksel_con[0], |
| 1165 | CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK | |
| 1166 | CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK, |
| 1167 | rate->aclk_div << CORE_ACLK_DIV_SHIFT | |
| 1168 | rate->pclk_div << CORE_DBG_DIV_SHIFT | |
| 1169 | CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT | |
| 1170 | 0 << CORE_DIV_CON_SHIFT); |
| 1171 | } else if (old_rate < hz) { |
| 1172 | rk_clrsetreg(&cru->clksel_con[0], |
| 1173 | CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK | |
| 1174 | CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK, |
| 1175 | rate->aclk_div << CORE_ACLK_DIV_SHIFT | |
| 1176 | rate->pclk_div << CORE_DBG_DIV_SHIFT | |
| 1177 | CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT | |
| 1178 | 0 << CORE_DIV_CON_SHIFT); |
| 1179 | if (rkclk_set_pll(&cru->pll[APLL], &cru->mode, APLL, hz)) |
| 1180 | return -EINVAL; |
| 1181 | } |
| 1182 | |
| 1183 | return px30_clk_get_pll_rate(priv, APLL); |
| 1184 | } |
| 1185 | |
| 1186 | static ulong px30_clk_get_rate(struct clk *clk) |
| 1187 | { |
| 1188 | struct px30_clk_priv *priv = dev_get_priv(clk->dev); |
| 1189 | ulong rate = 0; |
| 1190 | |
| 1191 | if (!priv->gpll_hz && clk->id > ARMCLK) { |
| 1192 | printf("%s gpll=%lu\n", __func__, priv->gpll_hz); |
| 1193 | return -ENOENT; |
| 1194 | } |
| 1195 | |
| 1196 | debug("%s %ld\n", __func__, clk->id); |
| 1197 | switch (clk->id) { |
| 1198 | case PLL_APLL: |
| 1199 | rate = px30_clk_get_pll_rate(priv, APLL); |
| 1200 | break; |
| 1201 | case PLL_DPLL: |
| 1202 | rate = px30_clk_get_pll_rate(priv, DPLL); |
| 1203 | break; |
| 1204 | case PLL_CPLL: |
| 1205 | rate = px30_clk_get_pll_rate(priv, CPLL); |
| 1206 | break; |
| 1207 | case PLL_NPLL: |
| 1208 | rate = px30_clk_get_pll_rate(priv, NPLL); |
| 1209 | break; |
| 1210 | case ARMCLK: |
| 1211 | rate = px30_clk_get_pll_rate(priv, APLL); |
| 1212 | break; |
| 1213 | case HCLK_SDMMC: |
| 1214 | case HCLK_EMMC: |
| 1215 | case SCLK_SDMMC: |
| 1216 | case SCLK_EMMC: |
| 1217 | case SCLK_EMMC_SAMPLE: |
| 1218 | rate = px30_mmc_get_clk(priv, clk->id); |
| 1219 | break; |
Jon Lin | 8f20e73 | 2021-08-05 16:27:53 +0800 | [diff] [blame] | 1220 | case SCLK_SFC: |
| 1221 | rate = px30_sfc_get_clk(priv, clk->id); |
| 1222 | break; |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1223 | case SCLK_I2C0: |
| 1224 | case SCLK_I2C1: |
| 1225 | case SCLK_I2C2: |
| 1226 | case SCLK_I2C3: |
| 1227 | rate = px30_i2c_get_clk(priv, clk->id); |
| 1228 | break; |
| 1229 | case SCLK_I2S1: |
| 1230 | rate = px30_i2s_get_clk(priv, clk->id); |
| 1231 | break; |
| 1232 | case SCLK_NANDC: |
| 1233 | rate = px30_nandc_get_clk(priv); |
| 1234 | break; |
| 1235 | case SCLK_PWM0: |
| 1236 | case SCLK_PWM1: |
| 1237 | rate = px30_pwm_get_clk(priv, clk->id); |
| 1238 | break; |
| 1239 | case SCLK_SARADC: |
| 1240 | rate = px30_saradc_get_clk(priv); |
| 1241 | break; |
| 1242 | case SCLK_TSADC: |
| 1243 | rate = px30_tsadc_get_clk(priv); |
| 1244 | break; |
| 1245 | case SCLK_SPI0: |
| 1246 | case SCLK_SPI1: |
| 1247 | rate = px30_spi_get_clk(priv, clk->id); |
| 1248 | break; |
| 1249 | case ACLK_VOPB: |
| 1250 | case ACLK_VOPL: |
| 1251 | case DCLK_VOPB: |
| 1252 | case DCLK_VOPL: |
| 1253 | rate = px30_vop_get_clk(priv, clk->id); |
| 1254 | break; |
| 1255 | case ACLK_BUS_PRE: |
| 1256 | case HCLK_BUS_PRE: |
| 1257 | case PCLK_BUS_PRE: |
| 1258 | case PCLK_WDT_NS: |
| 1259 | rate = px30_bus_get_clk(priv, clk->id); |
| 1260 | break; |
| 1261 | case ACLK_PERI_PRE: |
| 1262 | case HCLK_PERI_PRE: |
| 1263 | rate = px30_peri_get_clk(priv, clk->id); |
| 1264 | break; |
| 1265 | #ifndef CONFIG_SPL_BUILD |
| 1266 | case SCLK_CRYPTO: |
| 1267 | case SCLK_CRYPTO_APK: |
| 1268 | rate = px30_crypto_get_clk(priv, clk->id); |
| 1269 | break; |
| 1270 | #endif |
| 1271 | default: |
| 1272 | return -ENOENT; |
| 1273 | } |
| 1274 | |
| 1275 | return rate; |
| 1276 | } |
| 1277 | |
| 1278 | static ulong px30_clk_set_rate(struct clk *clk, ulong rate) |
| 1279 | { |
| 1280 | struct px30_clk_priv *priv = dev_get_priv(clk->dev); |
| 1281 | ulong ret = 0; |
| 1282 | |
| 1283 | if (!priv->gpll_hz && clk->id > ARMCLK) { |
| 1284 | printf("%s gpll=%lu\n", __func__, priv->gpll_hz); |
| 1285 | return -ENOENT; |
| 1286 | } |
| 1287 | |
| 1288 | debug("%s %ld %ld\n", __func__, clk->id, rate); |
| 1289 | switch (clk->id) { |
| 1290 | case PLL_NPLL: |
| 1291 | ret = px30_clk_set_pll_rate(priv, NPLL, rate); |
| 1292 | break; |
Chris Morgan | b959910 | 2021-08-05 11:48:47 -0500 | [diff] [blame] | 1293 | case PLL_CPLL: |
| 1294 | ret = px30_clk_set_pll_rate(priv, CPLL, rate); |
| 1295 | break; |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1296 | case ARMCLK: |
| 1297 | ret = px30_armclk_set_clk(priv, rate); |
| 1298 | break; |
| 1299 | case HCLK_SDMMC: |
| 1300 | case HCLK_EMMC: |
| 1301 | case SCLK_SDMMC: |
| 1302 | case SCLK_EMMC: |
| 1303 | ret = px30_mmc_set_clk(priv, clk->id, rate); |
| 1304 | break; |
Jon Lin | 8f20e73 | 2021-08-05 16:27:53 +0800 | [diff] [blame] | 1305 | case SCLK_SFC: |
| 1306 | ret = px30_sfc_set_clk(priv, clk->id, rate); |
| 1307 | break; |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1308 | case SCLK_I2C0: |
| 1309 | case SCLK_I2C1: |
| 1310 | case SCLK_I2C2: |
| 1311 | case SCLK_I2C3: |
| 1312 | ret = px30_i2c_set_clk(priv, clk->id, rate); |
| 1313 | break; |
| 1314 | case SCLK_I2S1: |
| 1315 | ret = px30_i2s_set_clk(priv, clk->id, rate); |
| 1316 | break; |
| 1317 | case SCLK_NANDC: |
| 1318 | ret = px30_nandc_set_clk(priv, rate); |
| 1319 | break; |
| 1320 | case SCLK_PWM0: |
| 1321 | case SCLK_PWM1: |
| 1322 | ret = px30_pwm_set_clk(priv, clk->id, rate); |
| 1323 | break; |
| 1324 | case SCLK_SARADC: |
| 1325 | ret = px30_saradc_set_clk(priv, rate); |
| 1326 | break; |
| 1327 | case SCLK_TSADC: |
| 1328 | ret = px30_tsadc_set_clk(priv, rate); |
| 1329 | break; |
| 1330 | case SCLK_SPI0: |
| 1331 | case SCLK_SPI1: |
| 1332 | ret = px30_spi_set_clk(priv, clk->id, rate); |
| 1333 | break; |
| 1334 | case ACLK_VOPB: |
| 1335 | case ACLK_VOPL: |
| 1336 | case DCLK_VOPB: |
| 1337 | case DCLK_VOPL: |
| 1338 | ret = px30_vop_set_clk(priv, clk->id, rate); |
| 1339 | break; |
| 1340 | case ACLK_BUS_PRE: |
| 1341 | case HCLK_BUS_PRE: |
| 1342 | case PCLK_BUS_PRE: |
| 1343 | ret = px30_bus_set_clk(priv, clk->id, rate); |
| 1344 | break; |
| 1345 | case ACLK_PERI_PRE: |
| 1346 | case HCLK_PERI_PRE: |
| 1347 | ret = px30_peri_set_clk(priv, clk->id, rate); |
| 1348 | break; |
| 1349 | #ifndef CONFIG_SPL_BUILD |
| 1350 | case SCLK_CRYPTO: |
| 1351 | case SCLK_CRYPTO_APK: |
| 1352 | ret = px30_crypto_set_clk(priv, clk->id, rate); |
| 1353 | break; |
| 1354 | case SCLK_I2S1_OUT: |
| 1355 | ret = px30_i2s1_mclk_set_clk(priv, clk->id, rate); |
| 1356 | break; |
| 1357 | case SCLK_GMAC: |
| 1358 | case SCLK_GMAC_SRC: |
| 1359 | ret = px30_mac_set_clk(priv, rate); |
| 1360 | break; |
| 1361 | case SCLK_GMAC_RMII: |
| 1362 | ret = px30_mac_set_speed_clk(priv, rate); |
| 1363 | break; |
| 1364 | #endif |
| 1365 | default: |
| 1366 | return -ENOENT; |
| 1367 | } |
| 1368 | |
| 1369 | return ret; |
| 1370 | } |
| 1371 | |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 1372 | #if CONFIG_IS_ENABLED(OF_REAL) |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1373 | static int px30_gmac_set_parent(struct clk *clk, struct clk *parent) |
| 1374 | { |
| 1375 | struct px30_clk_priv *priv = dev_get_priv(clk->dev); |
| 1376 | struct px30_cru *cru = priv->cru; |
| 1377 | |
| 1378 | if (parent->id == SCLK_GMAC_SRC) { |
| 1379 | debug("%s: switching GAMC to SCLK_GMAC_SRC\n", __func__); |
| 1380 | rk_clrsetreg(&cru->clksel_con[23], RMII_EXTCLK_SEL_MASK, |
| 1381 | RMII_EXTCLK_SEL_INT << RMII_EXTCLK_SEL_SHIFT); |
| 1382 | } else { |
| 1383 | debug("%s: switching GMAC to external clock\n", __func__); |
| 1384 | rk_clrsetreg(&cru->clksel_con[23], RMII_EXTCLK_SEL_MASK, |
| 1385 | RMII_EXTCLK_SEL_EXT << RMII_EXTCLK_SEL_SHIFT); |
| 1386 | } |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | static int px30_clk_set_parent(struct clk *clk, struct clk *parent) |
| 1391 | { |
| 1392 | switch (clk->id) { |
| 1393 | case SCLK_GMAC: |
| 1394 | return px30_gmac_set_parent(clk, parent); |
| 1395 | default: |
| 1396 | return -ENOENT; |
| 1397 | } |
| 1398 | } |
| 1399 | #endif |
| 1400 | |
| 1401 | static int px30_clk_enable(struct clk *clk) |
| 1402 | { |
| 1403 | switch (clk->id) { |
| 1404 | case HCLK_HOST: |
Chris Morgan | a579868 | 2022-03-25 12:09:22 -0500 | [diff] [blame] | 1405 | case HCLK_OTG: |
| 1406 | case HCLK_SFC: |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1407 | case SCLK_GMAC: |
| 1408 | case SCLK_GMAC_RX_TX: |
| 1409 | case SCLK_MAC_REF: |
| 1410 | case SCLK_MAC_REFOUT: |
Chris Morgan | a579868 | 2022-03-25 12:09:22 -0500 | [diff] [blame] | 1411 | case SCLK_SFC: |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1412 | case ACLK_GMAC: |
| 1413 | case PCLK_GMAC: |
| 1414 | case SCLK_GMAC_RMII: |
| 1415 | /* Required to successfully probe the Designware GMAC driver */ |
| 1416 | return 0; |
Quentin Schulz | 9b2f9d1 | 2022-11-14 11:33:46 +0100 | [diff] [blame] | 1417 | case PCLK_WDT_NS: |
| 1418 | /* Required to successfully probe the Designware watchdog driver */ |
| 1419 | return 0; |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | debug("%s: unsupported clk %ld\n", __func__, clk->id); |
| 1423 | return -ENOENT; |
| 1424 | } |
| 1425 | |
| 1426 | static struct clk_ops px30_clk_ops = { |
| 1427 | .get_rate = px30_clk_get_rate, |
| 1428 | .set_rate = px30_clk_set_rate, |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 1429 | #if CONFIG_IS_ENABLED(OF_REAL) |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1430 | .set_parent = px30_clk_set_parent, |
| 1431 | #endif |
| 1432 | .enable = px30_clk_enable, |
| 1433 | }; |
| 1434 | |
| 1435 | static void px30_clk_init(struct px30_clk_priv *priv) |
| 1436 | { |
| 1437 | ulong npll_hz; |
| 1438 | int ret; |
| 1439 | |
| 1440 | npll_hz = px30_clk_get_pll_rate(priv, NPLL); |
| 1441 | if (npll_hz != NPLL_HZ) { |
| 1442 | ret = px30_clk_set_pll_rate(priv, NPLL, NPLL_HZ); |
| 1443 | if (ret < 0) |
| 1444 | printf("%s failed to set npll rate\n", __func__); |
| 1445 | } |
| 1446 | |
| 1447 | px30_bus_set_clk(priv, ACLK_BUS_PRE, ACLK_BUS_HZ); |
| 1448 | px30_bus_set_clk(priv, HCLK_BUS_PRE, HCLK_BUS_HZ); |
| 1449 | px30_bus_set_clk(priv, PCLK_BUS_PRE, PCLK_BUS_HZ); |
| 1450 | px30_peri_set_clk(priv, ACLK_PERI_PRE, ACLK_PERI_HZ); |
| 1451 | px30_peri_set_clk(priv, HCLK_PERI_PRE, HCLK_PERI_HZ); |
| 1452 | } |
| 1453 | |
| 1454 | static int px30_clk_probe(struct udevice *dev) |
| 1455 | { |
| 1456 | struct px30_clk_priv *priv = dev_get_priv(dev); |
| 1457 | struct clk clk_gpll; |
| 1458 | int ret; |
| 1459 | |
| 1460 | if (px30_clk_get_pll_rate(priv, APLL) != APLL_HZ) |
| 1461 | px30_armclk_set_clk(priv, APLL_HZ); |
| 1462 | |
| 1463 | /* get the GPLL rate from the pmucru */ |
| 1464 | ret = clk_get_by_name(dev, "gpll", &clk_gpll); |
| 1465 | if (ret) { |
| 1466 | printf("%s: failed to get gpll clk from pmucru\n", __func__); |
| 1467 | return ret; |
| 1468 | } |
| 1469 | |
| 1470 | priv->gpll_hz = clk_get_rate(&clk_gpll); |
| 1471 | |
| 1472 | px30_clk_init(priv); |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1477 | static int px30_clk_of_to_plat(struct udevice *dev) |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1478 | { |
| 1479 | struct px30_clk_priv *priv = dev_get_priv(dev); |
| 1480 | |
| 1481 | priv->cru = dev_read_addr_ptr(dev); |
| 1482 | |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | static int px30_clk_bind(struct udevice *dev) |
| 1487 | { |
| 1488 | int ret; |
| 1489 | struct udevice *sys_child; |
| 1490 | struct sysreset_reg *priv; |
| 1491 | |
| 1492 | /* The reset driver does not have a device node, so bind it here */ |
| 1493 | ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset", |
| 1494 | &sys_child); |
| 1495 | if (ret) { |
| 1496 | debug("Warning: No sysreset driver: ret=%d\n", ret); |
| 1497 | } else { |
| 1498 | priv = malloc(sizeof(struct sysreset_reg)); |
| 1499 | priv->glb_srst_fst_value = offsetof(struct px30_cru, |
| 1500 | glb_srst_fst); |
| 1501 | priv->glb_srst_snd_value = offsetof(struct px30_cru, |
| 1502 | glb_srst_snd); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1503 | dev_set_priv(sys_child, priv); |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | #if CONFIG_IS_ENABLED(RESET_ROCKCHIP) |
| 1507 | ret = offsetof(struct px30_cru, softrst_con[0]); |
| 1508 | ret = rockchip_reset_bind(dev, ret, 12); |
| 1509 | if (ret) |
Eugen Hristev | f179826 | 2023-04-11 10:17:56 +0300 | [diff] [blame] | 1510 | debug("Warning: software reset driver bind failed\n"); |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1511 | #endif |
| 1512 | |
| 1513 | return 0; |
| 1514 | } |
| 1515 | |
| 1516 | static const struct udevice_id px30_clk_ids[] = { |
| 1517 | { .compatible = "rockchip,px30-cru" }, |
| 1518 | { } |
| 1519 | }; |
| 1520 | |
| 1521 | U_BOOT_DRIVER(rockchip_px30_cru) = { |
| 1522 | .name = "rockchip_px30_cru", |
| 1523 | .id = UCLASS_CLK, |
| 1524 | .of_match = px30_clk_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1525 | .priv_auto = sizeof(struct px30_clk_priv), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1526 | .of_to_plat = px30_clk_of_to_plat, |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1527 | .ops = &px30_clk_ops, |
| 1528 | .bind = px30_clk_bind, |
| 1529 | .probe = px30_clk_probe, |
| 1530 | }; |
| 1531 | |
| 1532 | static ulong px30_pclk_pmu_get_pmuclk(struct px30_pmuclk_priv *priv) |
| 1533 | { |
| 1534 | struct px30_pmucru *pmucru = priv->pmucru; |
| 1535 | u32 div, con; |
| 1536 | |
| 1537 | con = readl(&pmucru->pmu_clksel_con[0]); |
| 1538 | div = (con & CLK_PMU_PCLK_DIV_MASK) >> CLK_PMU_PCLK_DIV_SHIFT; |
| 1539 | |
| 1540 | return DIV_TO_RATE(priv->gpll_hz, div); |
| 1541 | } |
| 1542 | |
| 1543 | static ulong px30_pclk_pmu_set_pmuclk(struct px30_pmuclk_priv *priv, ulong hz) |
| 1544 | { |
| 1545 | struct px30_pmucru *pmucru = priv->pmucru; |
| 1546 | int src_clk_div; |
| 1547 | |
| 1548 | src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz); |
| 1549 | assert(src_clk_div - 1 <= 31); |
| 1550 | |
| 1551 | rk_clrsetreg(&pmucru->pmu_clksel_con[0], |
| 1552 | CLK_PMU_PCLK_DIV_MASK, |
| 1553 | (src_clk_div - 1) << CLK_PMU_PCLK_DIV_SHIFT); |
| 1554 | |
| 1555 | return px30_pclk_pmu_get_pmuclk(priv); |
| 1556 | } |
| 1557 | |
| 1558 | static ulong px30_pmuclk_get_gpll_rate(struct px30_pmuclk_priv *priv) |
| 1559 | { |
| 1560 | struct px30_pmucru *pmucru = priv->pmucru; |
| 1561 | |
| 1562 | return rkclk_pll_get_rate(&pmucru->pll, &pmucru->pmu_mode, GPLL); |
| 1563 | } |
| 1564 | |
| 1565 | static ulong px30_pmuclk_set_gpll_rate(struct px30_pmuclk_priv *priv, ulong hz) |
| 1566 | { |
| 1567 | struct px30_pmucru *pmucru = priv->pmucru; |
| 1568 | ulong pclk_pmu_rate; |
| 1569 | u32 div; |
| 1570 | |
| 1571 | if (priv->gpll_hz == hz) |
| 1572 | return priv->gpll_hz; |
| 1573 | |
| 1574 | div = DIV_ROUND_UP(hz, priv->gpll_hz); |
| 1575 | |
| 1576 | /* save clock rate */ |
| 1577 | pclk_pmu_rate = px30_pclk_pmu_get_pmuclk(priv); |
| 1578 | |
| 1579 | /* avoid rate too large, reduce rate first */ |
| 1580 | px30_pclk_pmu_set_pmuclk(priv, pclk_pmu_rate / div); |
| 1581 | |
| 1582 | /* change gpll rate */ |
| 1583 | rkclk_set_pll(&pmucru->pll, &pmucru->pmu_mode, GPLL, hz); |
| 1584 | priv->gpll_hz = px30_pmuclk_get_gpll_rate(priv); |
| 1585 | |
| 1586 | /* restore clock rate */ |
| 1587 | px30_pclk_pmu_set_pmuclk(priv, pclk_pmu_rate); |
| 1588 | |
| 1589 | return priv->gpll_hz; |
| 1590 | } |
| 1591 | |
| 1592 | static ulong px30_pmuclk_get_rate(struct clk *clk) |
| 1593 | { |
| 1594 | struct px30_pmuclk_priv *priv = dev_get_priv(clk->dev); |
| 1595 | ulong rate = 0; |
| 1596 | |
| 1597 | debug("%s %ld\n", __func__, clk->id); |
| 1598 | switch (clk->id) { |
| 1599 | case PLL_GPLL: |
| 1600 | rate = px30_pmuclk_get_gpll_rate(priv); |
| 1601 | break; |
| 1602 | case PCLK_PMU_PRE: |
| 1603 | rate = px30_pclk_pmu_get_pmuclk(priv); |
| 1604 | break; |
| 1605 | default: |
| 1606 | return -ENOENT; |
| 1607 | } |
| 1608 | |
| 1609 | return rate; |
| 1610 | } |
| 1611 | |
| 1612 | static ulong px30_pmuclk_set_rate(struct clk *clk, ulong rate) |
| 1613 | { |
| 1614 | struct px30_pmuclk_priv *priv = dev_get_priv(clk->dev); |
| 1615 | ulong ret = 0; |
| 1616 | |
| 1617 | debug("%s %ld %ld\n", __func__, clk->id, rate); |
| 1618 | switch (clk->id) { |
| 1619 | case PLL_GPLL: |
| 1620 | ret = px30_pmuclk_set_gpll_rate(priv, rate); |
| 1621 | break; |
| 1622 | case PCLK_PMU_PRE: |
| 1623 | ret = px30_pclk_pmu_set_pmuclk(priv, rate); |
| 1624 | break; |
| 1625 | default: |
| 1626 | return -ENOENT; |
| 1627 | } |
| 1628 | |
| 1629 | return ret; |
| 1630 | } |
| 1631 | |
| 1632 | static struct clk_ops px30_pmuclk_ops = { |
| 1633 | .get_rate = px30_pmuclk_get_rate, |
| 1634 | .set_rate = px30_pmuclk_set_rate, |
| 1635 | }; |
| 1636 | |
| 1637 | static void px30_pmuclk_init(struct px30_pmuclk_priv *priv) |
| 1638 | { |
| 1639 | priv->gpll_hz = px30_pmuclk_get_gpll_rate(priv); |
| 1640 | px30_pmuclk_set_gpll_rate(priv, GPLL_HZ); |
| 1641 | |
| 1642 | px30_pclk_pmu_set_pmuclk(priv, PCLK_PMU_HZ); |
| 1643 | } |
| 1644 | |
| 1645 | static int px30_pmuclk_probe(struct udevice *dev) |
| 1646 | { |
| 1647 | struct px30_pmuclk_priv *priv = dev_get_priv(dev); |
| 1648 | |
| 1649 | px30_pmuclk_init(priv); |
| 1650 | |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1654 | static int px30_pmuclk_of_to_plat(struct udevice *dev) |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1655 | { |
| 1656 | struct px30_pmuclk_priv *priv = dev_get_priv(dev); |
| 1657 | |
| 1658 | priv->pmucru = dev_read_addr_ptr(dev); |
| 1659 | |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
| 1663 | static const struct udevice_id px30_pmuclk_ids[] = { |
| 1664 | { .compatible = "rockchip,px30-pmucru" }, |
| 1665 | { } |
| 1666 | }; |
| 1667 | |
| 1668 | U_BOOT_DRIVER(rockchip_px30_pmucru) = { |
| 1669 | .name = "rockchip_px30_pmucru", |
| 1670 | .id = UCLASS_CLK, |
| 1671 | .of_match = px30_pmuclk_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1672 | .priv_auto = sizeof(struct px30_pmuclk_priv), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1673 | .of_to_plat = px30_pmuclk_of_to_plat, |
Kever Yang | ba1033d | 2019-07-11 10:42:16 +0200 | [diff] [blame] | 1674 | .ops = &px30_pmuclk_ops, |
| 1675 | .probe = px30_pmuclk_probe, |
| 1676 | }; |