Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * (C) Copyright 2017-2019 Rockchip Electronics Co., Ltd |
| 4 | */ |
| 5 | #include <common.h> |
| 6 | #include <bitfield.h> |
| 7 | #include <clk-uclass.h> |
| 8 | #include <dm.h> |
| 9 | #include <div64.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> |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 13 | #include <syscon.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <asm/arch/cru_rk3308.h> |
| 17 | #include <asm/arch-rockchip/clock.h> |
| 18 | #include <asm/arch-rockchip/hardware.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 19 | #include <dm/device-internal.h> |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 20 | #include <dm/lists.h> |
| 21 | #include <dt-bindings/clock/rk3308-cru.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 22 | #include <linux/bitops.h> |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [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 DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1)) |
| 34 | |
| 35 | #define RK3308_CPUCLK_RATE(_rate, _aclk_div, _pclk_div) \ |
| 36 | { \ |
| 37 | .rate = _rate##U, \ |
| 38 | .aclk_div = _aclk_div, \ |
| 39 | .pclk_div = _pclk_div, \ |
| 40 | } |
| 41 | |
| 42 | static struct rockchip_pll_rate_table rk3308_pll_rates[] = { |
| 43 | /* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2, _dsmpd, _frac */ |
| 44 | RK3036_PLL_RATE(1300000000, 6, 325, 1, 1, 1, 0), |
| 45 | RK3036_PLL_RATE(1200000000, 1, 50, 1, 1, 1, 0), |
| 46 | RK3036_PLL_RATE(816000000, 1, 68, 2, 1, 1, 0), |
| 47 | RK3036_PLL_RATE(748000000, 2, 187, 3, 1, 1, 0), |
| 48 | }; |
| 49 | |
| 50 | static struct rockchip_cpu_rate_table rk3308_cpu_rates[] = { |
| 51 | RK3308_CPUCLK_RATE(1200000000, 1, 5), |
| 52 | RK3308_CPUCLK_RATE(1008000000, 1, 5), |
| 53 | RK3308_CPUCLK_RATE(816000000, 1, 3), |
| 54 | RK3308_CPUCLK_RATE(600000000, 1, 3), |
| 55 | RK3308_CPUCLK_RATE(408000000, 1, 1), |
| 56 | }; |
| 57 | |
| 58 | static struct rockchip_pll_clock rk3308_pll_clks[] = { |
| 59 | [APLL] = PLL(pll_rk3328, PLL_APLL, RK3308_PLL_CON(0), |
| 60 | RK3308_MODE_CON, 0, 10, 0, rk3308_pll_rates), |
| 61 | [DPLL] = PLL(pll_rk3328, PLL_DPLL, RK3308_PLL_CON(8), |
| 62 | RK3308_MODE_CON, 2, 10, 0, NULL), |
| 63 | [VPLL0] = PLL(pll_rk3328, PLL_VPLL0, RK3308_PLL_CON(16), |
| 64 | RK3308_MODE_CON, 4, 10, 0, NULL), |
| 65 | [VPLL1] = PLL(pll_rk3328, PLL_VPLL1, RK3308_PLL_CON(24), |
| 66 | RK3308_MODE_CON, 6, 10, 0, NULL), |
| 67 | }; |
| 68 | |
| 69 | static ulong rk3308_armclk_set_clk(struct rk3308_clk_priv *priv, ulong hz) |
| 70 | { |
| 71 | struct rk3308_cru *cru = priv->cru; |
| 72 | const struct rockchip_cpu_rate_table *rate; |
| 73 | ulong old_rate; |
| 74 | |
| 75 | rate = rockchip_get_cpu_settings(rk3308_cpu_rates, hz); |
| 76 | if (!rate) { |
| 77 | printf("%s unsupport rate\n", __func__); |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * select apll as cpu/core clock pll source and |
| 83 | * set up dependent divisors for PERI and ACLK clocks. |
| 84 | * core hz : apll = 1:1 |
| 85 | */ |
| 86 | old_rate = rockchip_pll_get_rate(&rk3308_pll_clks[APLL], |
| 87 | priv->cru, APLL); |
| 88 | if (old_rate > hz) { |
| 89 | if (rockchip_pll_set_rate(&rk3308_pll_clks[APLL], |
| 90 | priv->cru, APLL, hz)) |
| 91 | return -EINVAL; |
| 92 | rk_clrsetreg(&cru->clksel_con[0], |
| 93 | CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK | |
| 94 | CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK, |
| 95 | rate->aclk_div << CORE_ACLK_DIV_SHIFT | |
| 96 | rate->pclk_div << CORE_DBG_DIV_SHIFT | |
| 97 | CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT | |
| 98 | 0 << CORE_DIV_CON_SHIFT); |
| 99 | } else if (old_rate < hz) { |
| 100 | rk_clrsetreg(&cru->clksel_con[0], |
| 101 | CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK | |
| 102 | CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK, |
| 103 | rate->aclk_div << CORE_ACLK_DIV_SHIFT | |
| 104 | rate->pclk_div << CORE_DBG_DIV_SHIFT | |
| 105 | CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT | |
| 106 | 0 << CORE_DIV_CON_SHIFT); |
| 107 | if (rockchip_pll_set_rate(&rk3308_pll_clks[APLL], |
| 108 | priv->cru, APLL, hz)) |
| 109 | return -EINVAL; |
| 110 | } |
| 111 | |
| 112 | return rockchip_pll_get_rate(&rk3308_pll_clks[APLL], priv->cru, APLL); |
| 113 | } |
| 114 | |
| 115 | static void rk3308_clk_get_pll_rate(struct rk3308_clk_priv *priv) |
| 116 | { |
| 117 | if (!priv->dpll_hz) |
| 118 | priv->dpll_hz = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL], |
| 119 | priv->cru, DPLL); |
| 120 | if (!priv->vpll0_hz) |
| 121 | priv->vpll0_hz = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL0], |
| 122 | priv->cru, VPLL0); |
| 123 | if (!priv->vpll1_hz) |
| 124 | priv->vpll1_hz = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL1], |
| 125 | priv->cru, VPLL1); |
| 126 | } |
| 127 | |
| 128 | static ulong rk3308_i2c_get_clk(struct clk *clk) |
| 129 | { |
| 130 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 131 | struct rk3308_cru *cru = priv->cru; |
| 132 | u32 div, con, con_id; |
| 133 | |
| 134 | switch (clk->id) { |
| 135 | case SCLK_I2C0: |
| 136 | con_id = 25; |
| 137 | break; |
| 138 | case SCLK_I2C1: |
| 139 | con_id = 26; |
| 140 | break; |
| 141 | case SCLK_I2C2: |
| 142 | con_id = 27; |
| 143 | break; |
| 144 | case SCLK_I2C3: |
| 145 | con_id = 28; |
| 146 | break; |
| 147 | default: |
| 148 | printf("do not support this i2c bus\n"); |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | con = readl(&cru->clksel_con[con_id]); |
Massimo Pegorer | 00a8fa3 | 2023-08-03 13:08:11 +0200 | [diff] [blame] | 153 | div = (con & CLK_I2C_DIV_CON_MASK) >> CLK_I2C_DIV_CON_SHIFT; |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 154 | |
| 155 | return DIV_TO_RATE(priv->dpll_hz, div); |
| 156 | } |
| 157 | |
| 158 | static ulong rk3308_i2c_set_clk(struct clk *clk, uint hz) |
| 159 | { |
| 160 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 161 | struct rk3308_cru *cru = priv->cru; |
| 162 | u32 src_clk_div, con_id; |
| 163 | |
| 164 | src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz); |
| 165 | assert(src_clk_div - 1 <= 127); |
| 166 | |
| 167 | switch (clk->id) { |
| 168 | case SCLK_I2C0: |
| 169 | con_id = 25; |
| 170 | break; |
| 171 | case SCLK_I2C1: |
| 172 | con_id = 26; |
| 173 | break; |
| 174 | case SCLK_I2C2: |
| 175 | con_id = 27; |
| 176 | break; |
| 177 | case SCLK_I2C3: |
| 178 | con_id = 28; |
| 179 | break; |
| 180 | default: |
| 181 | printf("do not support this i2c bus\n"); |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | rk_clrsetreg(&cru->clksel_con[con_id], |
| 185 | CLK_I2C_PLL_SEL_MASK | CLK_I2C_DIV_CON_MASK, |
| 186 | CLK_I2C_PLL_SEL_DPLL << CLK_I2C_PLL_SEL_SHIFT | |
| 187 | (src_clk_div - 1) << CLK_I2C_DIV_CON_SHIFT); |
| 188 | |
| 189 | return rk3308_i2c_get_clk(clk); |
| 190 | } |
| 191 | |
| 192 | static ulong rk3308_mac_set_clk(struct clk *clk, uint hz) |
| 193 | { |
| 194 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 195 | struct rk3308_cru *cru = priv->cru; |
| 196 | u32 con = readl(&cru->clksel_con[43]); |
| 197 | ulong pll_rate; |
| 198 | u8 div; |
| 199 | |
| 200 | if ((con >> MAC_PLL_SHIFT) & MAC_SEL_VPLL0) |
| 201 | pll_rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL0], |
| 202 | priv->cru, VPLL0); |
| 203 | else if ((con >> MAC_PLL_SHIFT) & MAC_SEL_VPLL1) |
| 204 | pll_rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL1], |
| 205 | priv->cru, VPLL1); |
| 206 | else |
| 207 | pll_rate = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL], |
| 208 | priv->cru, DPLL); |
| 209 | |
| 210 | /*default set 50MHZ for gmac*/ |
| 211 | if (!hz) |
| 212 | hz = 50000000; |
| 213 | |
| 214 | div = DIV_ROUND_UP(pll_rate, hz) - 1; |
| 215 | assert(div < 32); |
| 216 | rk_clrsetreg(&cru->clksel_con[43], MAC_DIV_MASK, |
| 217 | div << MAC_DIV_SHIFT); |
| 218 | |
| 219 | return DIV_TO_RATE(pll_rate, div); |
| 220 | } |
| 221 | |
| 222 | static int rk3308_mac_set_speed_clk(struct clk *clk, uint hz) |
| 223 | { |
| 224 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 225 | struct rk3308_cru *cru = priv->cru; |
| 226 | |
| 227 | if (hz != 2500000 && hz != 25000000) { |
| 228 | debug("Unsupported mac speed:%d\n", hz); |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | rk_clrsetreg(&cru->clksel_con[43], MAC_CLK_SPEED_SEL_MASK, |
| 233 | ((hz == 2500000) ? 0 : 1) << MAC_CLK_SPEED_SEL_SHIFT); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static ulong rk3308_mmc_get_clk(struct clk *clk) |
| 239 | { |
| 240 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 241 | struct rk3308_cru *cru = priv->cru; |
| 242 | u32 div, con, con_id; |
| 243 | |
| 244 | switch (clk->id) { |
| 245 | case HCLK_SDMMC: |
| 246 | case SCLK_SDMMC: |
| 247 | con_id = 39; |
| 248 | break; |
| 249 | case HCLK_EMMC: |
| 250 | case SCLK_EMMC: |
| 251 | case SCLK_EMMC_SAMPLE: |
| 252 | con_id = 41; |
| 253 | break; |
| 254 | default: |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | con = readl(&cru->clksel_con[con_id]); |
| 259 | div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT; |
| 260 | |
| 261 | if ((con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT |
| 262 | == EMMC_SEL_24M) |
| 263 | return DIV_TO_RATE(OSC_HZ, div) / 2; |
| 264 | else |
| 265 | return DIV_TO_RATE(priv->vpll0_hz, div) / 2; |
| 266 | } |
| 267 | |
| 268 | static ulong rk3308_mmc_set_clk(struct clk *clk, ulong set_rate) |
| 269 | { |
| 270 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 271 | struct rk3308_cru *cru = priv->cru; |
| 272 | int src_clk_div; |
| 273 | u32 con_id; |
| 274 | |
| 275 | switch (clk->id) { |
| 276 | case HCLK_SDMMC: |
| 277 | case SCLK_SDMMC: |
| 278 | con_id = 39; |
| 279 | break; |
| 280 | case HCLK_EMMC: |
| 281 | case SCLK_EMMC: |
| 282 | con_id = 41; |
| 283 | break; |
| 284 | default: |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | /* Select clk_sdmmc/emmc source from VPLL0 by default */ |
| 288 | /* mmc clock defaulg div 2 internal, need provide double in cru */ |
| 289 | src_clk_div = DIV_ROUND_UP(priv->vpll0_hz / 2, set_rate); |
| 290 | |
| 291 | if (src_clk_div > 127) { |
| 292 | /* use 24MHz source for 400KHz clock */ |
| 293 | src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate); |
| 294 | rk_clrsetreg(&cru->clksel_con[con_id], |
| 295 | EMMC_PLL_MASK | EMMC_DIV_MASK | EMMC_CLK_SEL_MASK, |
| 296 | EMMC_CLK_SEL_EMMC << EMMC_CLK_SEL_SHIFT | |
| 297 | EMMC_SEL_24M << EMMC_PLL_SHIFT | |
| 298 | (src_clk_div - 1) << EMMC_DIV_SHIFT); |
| 299 | } else { |
| 300 | rk_clrsetreg(&cru->clksel_con[con_id], |
| 301 | EMMC_PLL_MASK | EMMC_DIV_MASK | EMMC_CLK_SEL_MASK, |
| 302 | EMMC_CLK_SEL_EMMC << EMMC_CLK_SEL_SHIFT | |
| 303 | EMMC_SEL_VPLL0 << EMMC_PLL_SHIFT | |
| 304 | (src_clk_div - 1) << EMMC_DIV_SHIFT); |
| 305 | } |
| 306 | |
| 307 | return rk3308_mmc_get_clk(clk); |
| 308 | } |
| 309 | |
| 310 | static ulong rk3308_saradc_get_clk(struct clk *clk) |
| 311 | { |
| 312 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 313 | struct rk3308_cru *cru = priv->cru; |
| 314 | u32 div, con; |
| 315 | |
| 316 | con = readl(&cru->clksel_con[34]); |
Massimo Pegorer | 00a8fa3 | 2023-08-03 13:08:11 +0200 | [diff] [blame] | 317 | div = (con & CLK_SARADC_DIV_CON_MASK) >> CLK_SARADC_DIV_CON_SHIFT; |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 318 | |
| 319 | return DIV_TO_RATE(OSC_HZ, div); |
| 320 | } |
| 321 | |
| 322 | static ulong rk3308_saradc_set_clk(struct clk *clk, uint hz) |
| 323 | { |
| 324 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 325 | struct rk3308_cru *cru = priv->cru; |
| 326 | int src_clk_div; |
| 327 | |
| 328 | src_clk_div = DIV_ROUND_UP(OSC_HZ, hz); |
| 329 | assert(src_clk_div - 1 <= 2047); |
| 330 | |
| 331 | rk_clrsetreg(&cru->clksel_con[34], |
| 332 | CLK_SARADC_DIV_CON_MASK, |
| 333 | (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT); |
| 334 | |
| 335 | return rk3308_saradc_get_clk(clk); |
| 336 | } |
| 337 | |
| 338 | static ulong rk3308_tsadc_get_clk(struct clk *clk) |
| 339 | { |
| 340 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 341 | struct rk3308_cru *cru = priv->cru; |
| 342 | u32 div, con; |
| 343 | |
| 344 | con = readl(&cru->clksel_con[33]); |
Massimo Pegorer | 00a8fa3 | 2023-08-03 13:08:11 +0200 | [diff] [blame] | 345 | div = (con & CLK_SARADC_DIV_CON_MASK) >> CLK_SARADC_DIV_CON_SHIFT; |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 346 | |
| 347 | return DIV_TO_RATE(OSC_HZ, div); |
| 348 | } |
| 349 | |
| 350 | static ulong rk3308_tsadc_set_clk(struct clk *clk, uint hz) |
| 351 | { |
| 352 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 353 | struct rk3308_cru *cru = priv->cru; |
| 354 | int src_clk_div; |
| 355 | |
| 356 | src_clk_div = DIV_ROUND_UP(OSC_HZ, hz); |
| 357 | assert(src_clk_div - 1 <= 2047); |
| 358 | |
| 359 | rk_clrsetreg(&cru->clksel_con[33], |
| 360 | CLK_SARADC_DIV_CON_MASK, |
| 361 | (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT); |
| 362 | |
| 363 | return rk3308_tsadc_get_clk(clk); |
| 364 | } |
| 365 | |
| 366 | static ulong rk3308_spi_get_clk(struct clk *clk) |
| 367 | { |
| 368 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 369 | struct rk3308_cru *cru = priv->cru; |
| 370 | u32 div, con, con_id; |
| 371 | |
| 372 | switch (clk->id) { |
| 373 | case SCLK_SPI0: |
| 374 | con_id = 30; |
| 375 | break; |
| 376 | case SCLK_SPI1: |
| 377 | con_id = 31; |
| 378 | break; |
| 379 | case SCLK_SPI2: |
| 380 | con_id = 32; |
| 381 | break; |
| 382 | default: |
| 383 | printf("do not support this spi bus\n"); |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | |
| 387 | con = readl(&cru->clksel_con[con_id]); |
Massimo Pegorer | 00a8fa3 | 2023-08-03 13:08:11 +0200 | [diff] [blame] | 388 | div = (con & CLK_SPI_DIV_CON_MASK) >> CLK_SPI_DIV_CON_SHIFT; |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 389 | |
| 390 | return DIV_TO_RATE(priv->dpll_hz, div); |
| 391 | } |
| 392 | |
| 393 | static ulong rk3308_spi_set_clk(struct clk *clk, uint hz) |
| 394 | { |
| 395 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 396 | struct rk3308_cru *cru = priv->cru; |
| 397 | u32 src_clk_div, con_id; |
| 398 | |
| 399 | src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz); |
| 400 | assert(src_clk_div - 1 <= 127); |
| 401 | |
| 402 | switch (clk->id) { |
| 403 | case SCLK_SPI0: |
| 404 | con_id = 30; |
| 405 | break; |
| 406 | case SCLK_SPI1: |
| 407 | con_id = 31; |
| 408 | break; |
| 409 | case SCLK_SPI2: |
| 410 | con_id = 32; |
| 411 | break; |
| 412 | default: |
| 413 | printf("do not support this spi bus\n"); |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | |
| 417 | rk_clrsetreg(&cru->clksel_con[con_id], |
| 418 | CLK_SPI_PLL_SEL_MASK | CLK_SPI_DIV_CON_MASK, |
| 419 | CLK_SPI_PLL_SEL_DPLL << CLK_SPI_PLL_SEL_SHIFT | |
| 420 | (src_clk_div - 1) << CLK_SPI_DIV_CON_SHIFT); |
| 421 | |
| 422 | return rk3308_spi_get_clk(clk); |
| 423 | } |
| 424 | |
| 425 | static ulong rk3308_pwm_get_clk(struct clk *clk) |
| 426 | { |
| 427 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 428 | struct rk3308_cru *cru = priv->cru; |
| 429 | u32 div, con; |
| 430 | |
| 431 | con = readl(&cru->clksel_con[29]); |
Massimo Pegorer | 00a8fa3 | 2023-08-03 13:08:11 +0200 | [diff] [blame] | 432 | div = (con & CLK_PWM_DIV_CON_MASK) >> CLK_PWM_DIV_CON_SHIFT; |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 433 | |
| 434 | return DIV_TO_RATE(priv->dpll_hz, div); |
| 435 | } |
| 436 | |
| 437 | static ulong rk3308_pwm_set_clk(struct clk *clk, uint hz) |
| 438 | { |
| 439 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 440 | struct rk3308_cru *cru = priv->cru; |
| 441 | int src_clk_div; |
| 442 | |
| 443 | src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz); |
| 444 | assert(src_clk_div - 1 <= 127); |
| 445 | |
| 446 | rk_clrsetreg(&cru->clksel_con[29], |
| 447 | CLK_PWM_PLL_SEL_MASK | CLK_PWM_DIV_CON_MASK, |
| 448 | CLK_PWM_PLL_SEL_DPLL << CLK_PWM_PLL_SEL_SHIFT | |
| 449 | (src_clk_div - 1) << CLK_PWM_DIV_CON_SHIFT); |
| 450 | |
| 451 | return rk3308_pwm_get_clk(clk); |
| 452 | } |
| 453 | |
Massimo Pegorer | 2d7b0d0 | 2023-08-03 13:08:12 +0200 | [diff] [blame] | 454 | static ulong rk3308_uart_get_clk(struct clk *clk) |
| 455 | { |
| 456 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 457 | struct rk3308_cru *cru = priv->cru; |
| 458 | u32 div, pll_sel, con, con_id, parent; |
| 459 | |
| 460 | switch (clk->id) { |
| 461 | case SCLK_UART0: |
| 462 | con_id = 10; |
| 463 | break; |
| 464 | case SCLK_UART1: |
| 465 | con_id = 13; |
| 466 | break; |
| 467 | case SCLK_UART2: |
| 468 | con_id = 16; |
| 469 | break; |
| 470 | case SCLK_UART3: |
| 471 | con_id = 19; |
| 472 | break; |
| 473 | case SCLK_UART4: |
| 474 | con_id = 22; |
| 475 | break; |
| 476 | default: |
| 477 | printf("do not support this uart interface\n"); |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | |
| 481 | con = readl(&cru->clksel_con[con_id]); |
| 482 | pll_sel = (con & CLK_UART_PLL_SEL_MASK) >> CLK_UART_PLL_SEL_SHIFT; |
| 483 | div = (con & CLK_UART_DIV_CON_MASK) >> CLK_UART_DIV_CON_SHIFT; |
| 484 | |
| 485 | switch (pll_sel) { |
| 486 | case CLK_UART_PLL_SEL_DPLL: |
| 487 | parent = priv->dpll_hz; |
| 488 | break; |
| 489 | case CLK_UART_PLL_SEL_VPLL0: |
| 490 | parent = priv->vpll0_hz; |
| 491 | break; |
| 492 | case CLK_UART_PLL_SEL_VPLL1: |
| 493 | parent = priv->vpll0_hz; |
| 494 | break; |
| 495 | case CLK_UART_PLL_SEL_24M: |
| 496 | parent = OSC_HZ; |
| 497 | break; |
| 498 | default: |
| 499 | printf("do not support this uart pll sel\n"); |
| 500 | return -EINVAL; |
| 501 | } |
| 502 | |
| 503 | return DIV_TO_RATE(parent, div); |
| 504 | } |
| 505 | |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 506 | static ulong rk3308_vop_get_clk(struct clk *clk) |
| 507 | { |
| 508 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 509 | struct rk3308_cru *cru = priv->cru; |
| 510 | u32 div, pll_sel, vol_sel, con, parent; |
| 511 | |
| 512 | con = readl(&cru->clksel_con[8]); |
| 513 | vol_sel = (con & DCLK_VOP_SEL_MASK) >> DCLK_VOP_SEL_SHIFT; |
| 514 | pll_sel = (con & DCLK_VOP_PLL_SEL_MASK) >> DCLK_VOP_PLL_SEL_SHIFT; |
| 515 | div = con & DCLK_VOP_DIV_MASK; |
| 516 | |
| 517 | if (vol_sel == DCLK_VOP_SEL_24M) { |
| 518 | parent = OSC_HZ; |
| 519 | } else if (vol_sel == DCLK_VOP_SEL_DIVOUT) { |
| 520 | switch (pll_sel) { |
| 521 | case DCLK_VOP_PLL_SEL_DPLL: |
| 522 | parent = priv->dpll_hz; |
| 523 | break; |
| 524 | case DCLK_VOP_PLL_SEL_VPLL0: |
| 525 | parent = priv->vpll0_hz; |
| 526 | break; |
| 527 | case DCLK_VOP_PLL_SEL_VPLL1: |
| 528 | parent = priv->vpll0_hz; |
| 529 | break; |
| 530 | default: |
| 531 | printf("do not support this vop pll sel\n"); |
| 532 | return -EINVAL; |
| 533 | } |
| 534 | } else { |
| 535 | printf("do not support this vop sel\n"); |
| 536 | return -EINVAL; |
| 537 | } |
| 538 | |
| 539 | return DIV_TO_RATE(parent, div); |
| 540 | } |
| 541 | |
| 542 | static ulong rk3308_vop_set_clk(struct clk *clk, ulong hz) |
| 543 | { |
| 544 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 545 | struct rk3308_cru *cru = priv->cru; |
| 546 | ulong pll_rate, now, best_rate = 0; |
| 547 | u32 i, div, best_div = 0, best_sel = 0; |
| 548 | |
| 549 | for (i = 0; i <= DCLK_VOP_PLL_SEL_VPLL1; i++) { |
| 550 | switch (i) { |
| 551 | case DCLK_VOP_PLL_SEL_DPLL: |
| 552 | pll_rate = priv->dpll_hz; |
| 553 | break; |
| 554 | case DCLK_VOP_PLL_SEL_VPLL0: |
| 555 | pll_rate = priv->vpll0_hz; |
| 556 | break; |
| 557 | case DCLK_VOP_PLL_SEL_VPLL1: |
| 558 | pll_rate = priv->vpll1_hz; |
| 559 | break; |
| 560 | default: |
| 561 | printf("do not support this vop pll sel\n"); |
| 562 | return -EINVAL; |
| 563 | } |
| 564 | |
| 565 | div = DIV_ROUND_UP(pll_rate, hz); |
| 566 | if (div > 255) |
| 567 | continue; |
| 568 | now = pll_rate / div; |
| 569 | if (abs(hz - now) < abs(hz - best_rate)) { |
| 570 | best_rate = now; |
| 571 | best_div = div; |
| 572 | best_sel = i; |
| 573 | } |
| 574 | debug("pll_rate=%lu, best_rate=%lu, best_div=%u, best_sel=%u\n", |
| 575 | pll_rate, best_rate, best_div, best_sel); |
| 576 | } |
| 577 | |
| 578 | if (best_rate != hz && hz == OSC_HZ) { |
| 579 | rk_clrsetreg(&cru->clksel_con[8], |
| 580 | DCLK_VOP_SEL_MASK, |
| 581 | DCLK_VOP_SEL_24M << DCLK_VOP_SEL_SHIFT); |
| 582 | } else if (best_rate) { |
| 583 | rk_clrsetreg(&cru->clksel_con[8], |
| 584 | DCLK_VOP_SEL_MASK | DCLK_VOP_PLL_SEL_MASK | |
| 585 | DCLK_VOP_DIV_MASK, |
| 586 | DCLK_VOP_SEL_DIVOUT << DCLK_VOP_SEL_SHIFT | |
| 587 | best_sel << DCLK_VOP_PLL_SEL_SHIFT | |
| 588 | (best_div - 1) << DCLK_VOP_DIV_SHIFT); |
| 589 | } else { |
| 590 | printf("do not support this vop freq\n"); |
| 591 | return -EINVAL; |
| 592 | } |
| 593 | |
| 594 | return rk3308_vop_get_clk(clk); |
| 595 | } |
| 596 | |
| 597 | static ulong rk3308_bus_get_clk(struct rk3308_clk_priv *priv, ulong clk_id) |
| 598 | { |
| 599 | struct rk3308_cru *cru = priv->cru; |
| 600 | u32 div, con, parent = priv->dpll_hz; |
| 601 | |
| 602 | switch (clk_id) { |
| 603 | case ACLK_BUS: |
| 604 | con = readl(&cru->clksel_con[5]); |
| 605 | div = (con & BUS_ACLK_DIV_MASK) >> BUS_ACLK_DIV_SHIFT; |
| 606 | break; |
| 607 | case HCLK_BUS: |
| 608 | con = readl(&cru->clksel_con[6]); |
| 609 | div = (con & BUS_HCLK_DIV_MASK) >> BUS_HCLK_DIV_SHIFT; |
| 610 | break; |
| 611 | case PCLK_BUS: |
| 612 | case PCLK_WDT: |
| 613 | con = readl(&cru->clksel_con[6]); |
| 614 | div = (con & BUS_PCLK_DIV_MASK) >> BUS_PCLK_DIV_SHIFT; |
| 615 | break; |
| 616 | default: |
| 617 | return -ENOENT; |
| 618 | } |
| 619 | |
| 620 | return DIV_TO_RATE(parent, div); |
| 621 | } |
| 622 | |
| 623 | static ulong rk3308_bus_set_clk(struct rk3308_clk_priv *priv, ulong clk_id, |
| 624 | ulong hz) |
| 625 | { |
| 626 | struct rk3308_cru *cru = priv->cru; |
| 627 | int src_clk_div; |
| 628 | |
| 629 | src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz); |
| 630 | assert(src_clk_div - 1 <= 31); |
| 631 | |
| 632 | /* |
| 633 | * select dpll as pd_bus bus clock source and |
| 634 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 635 | */ |
| 636 | switch (clk_id) { |
| 637 | case ACLK_BUS: |
| 638 | rk_clrsetreg(&cru->clksel_con[5], |
| 639 | BUS_PLL_SEL_MASK | BUS_ACLK_DIV_MASK, |
| 640 | BUS_PLL_SEL_DPLL << BUS_PLL_SEL_SHIFT | |
| 641 | (src_clk_div - 1) << BUS_ACLK_DIV_SHIFT); |
| 642 | break; |
| 643 | case HCLK_BUS: |
| 644 | rk_clrsetreg(&cru->clksel_con[6], |
| 645 | BUS_HCLK_DIV_MASK, |
| 646 | (src_clk_div - 1) << BUS_HCLK_DIV_SHIFT); |
| 647 | break; |
| 648 | case PCLK_BUS: |
| 649 | rk_clrsetreg(&cru->clksel_con[6], |
| 650 | BUS_PCLK_DIV_MASK, |
| 651 | (src_clk_div - 1) << BUS_PCLK_DIV_SHIFT); |
| 652 | break; |
| 653 | default: |
| 654 | printf("do not support this bus freq\n"); |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | |
| 658 | return rk3308_bus_get_clk(priv, clk_id); |
| 659 | } |
| 660 | |
| 661 | static ulong rk3308_peri_get_clk(struct rk3308_clk_priv *priv, ulong clk_id) |
| 662 | { |
| 663 | struct rk3308_cru *cru = priv->cru; |
| 664 | u32 div, con, parent = priv->dpll_hz; |
| 665 | |
| 666 | switch (clk_id) { |
| 667 | case ACLK_PERI: |
| 668 | con = readl(&cru->clksel_con[36]); |
| 669 | div = (con & PERI_ACLK_DIV_MASK) >> PERI_ACLK_DIV_SHIFT; |
| 670 | break; |
| 671 | case HCLK_PERI: |
| 672 | con = readl(&cru->clksel_con[37]); |
| 673 | div = (con & PERI_HCLK_DIV_MASK) >> PERI_HCLK_DIV_SHIFT; |
| 674 | break; |
| 675 | case PCLK_PERI: |
| 676 | con = readl(&cru->clksel_con[37]); |
| 677 | div = (con & PERI_PCLK_DIV_MASK) >> PERI_PCLK_DIV_SHIFT; |
| 678 | break; |
| 679 | default: |
| 680 | return -ENOENT; |
| 681 | } |
| 682 | |
| 683 | return DIV_TO_RATE(parent, div); |
| 684 | } |
| 685 | |
| 686 | static ulong rk3308_peri_set_clk(struct rk3308_clk_priv *priv, ulong clk_id, |
| 687 | ulong hz) |
| 688 | { |
| 689 | struct rk3308_cru *cru = priv->cru; |
| 690 | int src_clk_div; |
| 691 | |
| 692 | src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz); |
| 693 | assert(src_clk_div - 1 <= 31); |
| 694 | |
| 695 | /* |
| 696 | * select dpll as pd_peri bus clock source and |
| 697 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 698 | */ |
| 699 | switch (clk_id) { |
| 700 | case ACLK_PERI: |
| 701 | rk_clrsetreg(&cru->clksel_con[36], |
| 702 | PERI_PLL_SEL_MASK | PERI_ACLK_DIV_MASK, |
| 703 | PERI_PLL_DPLL << PERI_PLL_SEL_SHIFT | |
| 704 | (src_clk_div - 1) << PERI_ACLK_DIV_SHIFT); |
| 705 | break; |
| 706 | case HCLK_PERI: |
| 707 | rk_clrsetreg(&cru->clksel_con[37], |
| 708 | PERI_HCLK_DIV_MASK, |
| 709 | (src_clk_div - 1) << PERI_HCLK_DIV_SHIFT); |
| 710 | break; |
| 711 | case PCLK_PERI: |
| 712 | rk_clrsetreg(&cru->clksel_con[37], |
| 713 | PERI_PCLK_DIV_MASK, |
| 714 | (src_clk_div - 1) << PERI_PCLK_DIV_SHIFT); |
| 715 | break; |
| 716 | default: |
| 717 | printf("do not support this peri freq\n"); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | |
| 721 | return rk3308_peri_get_clk(priv, clk_id); |
| 722 | } |
| 723 | |
| 724 | static ulong rk3308_audio_get_clk(struct rk3308_clk_priv *priv, ulong clk_id) |
| 725 | { |
| 726 | struct rk3308_cru *cru = priv->cru; |
| 727 | u32 div, con, parent = priv->vpll0_hz; |
| 728 | |
| 729 | switch (clk_id) { |
| 730 | case HCLK_AUDIO: |
| 731 | con = readl(&cru->clksel_con[45]); |
| 732 | div = (con & AUDIO_HCLK_DIV_MASK) >> AUDIO_HCLK_DIV_SHIFT; |
| 733 | break; |
| 734 | case PCLK_AUDIO: |
| 735 | con = readl(&cru->clksel_con[45]); |
| 736 | div = (con & AUDIO_PCLK_DIV_MASK) >> AUDIO_PCLK_DIV_SHIFT; |
| 737 | break; |
| 738 | default: |
| 739 | return -ENOENT; |
| 740 | } |
| 741 | |
| 742 | return DIV_TO_RATE(parent, div); |
| 743 | } |
| 744 | |
| 745 | static ulong rk3308_audio_set_clk(struct rk3308_clk_priv *priv, ulong clk_id, |
| 746 | ulong hz) |
| 747 | { |
| 748 | struct rk3308_cru *cru = priv->cru; |
| 749 | int src_clk_div; |
| 750 | |
| 751 | src_clk_div = DIV_ROUND_UP(priv->vpll0_hz, hz); |
| 752 | assert(src_clk_div - 1 <= 31); |
| 753 | |
| 754 | /* |
| 755 | * select vpll0 as audio bus clock source and |
| 756 | * set up dependent divisors for HCLK and PCLK clocks. |
| 757 | */ |
| 758 | switch (clk_id) { |
| 759 | case HCLK_AUDIO: |
| 760 | rk_clrsetreg(&cru->clksel_con[45], |
| 761 | AUDIO_PLL_SEL_MASK | AUDIO_HCLK_DIV_MASK, |
| 762 | AUDIO_PLL_VPLL0 << AUDIO_PLL_SEL_SHIFT | |
| 763 | (src_clk_div - 1) << AUDIO_HCLK_DIV_SHIFT); |
| 764 | break; |
| 765 | case PCLK_AUDIO: |
| 766 | rk_clrsetreg(&cru->clksel_con[45], |
| 767 | AUDIO_PLL_SEL_MASK | AUDIO_PCLK_DIV_MASK, |
| 768 | AUDIO_PLL_VPLL0 << AUDIO_PLL_SEL_SHIFT | |
| 769 | (src_clk_div - 1) << AUDIO_PCLK_DIV_SHIFT); |
| 770 | break; |
| 771 | default: |
| 772 | printf("do not support this audio freq\n"); |
| 773 | return -EINVAL; |
| 774 | } |
| 775 | |
| 776 | return rk3308_peri_get_clk(priv, clk_id); |
| 777 | } |
| 778 | |
| 779 | static ulong rk3308_crypto_get_clk(struct rk3308_clk_priv *priv, ulong clk_id) |
| 780 | { |
| 781 | struct rk3308_cru *cru = priv->cru; |
| 782 | u32 div, con, parent; |
| 783 | |
| 784 | switch (clk_id) { |
| 785 | case SCLK_CRYPTO: |
| 786 | con = readl(&cru->clksel_con[7]); |
| 787 | div = (con & CRYPTO_DIV_MASK) >> CRYPTO_DIV_SHIFT; |
| 788 | parent = priv->vpll0_hz; |
| 789 | break; |
| 790 | case SCLK_CRYPTO_APK: |
| 791 | con = readl(&cru->clksel_con[7]); |
| 792 | div = (con & CRYPTO_APK_DIV_MASK) >> CRYPTO_APK_DIV_SHIFT; |
| 793 | parent = priv->vpll0_hz; |
| 794 | break; |
| 795 | default: |
| 796 | return -ENOENT; |
| 797 | } |
| 798 | |
| 799 | return DIV_TO_RATE(parent, div); |
| 800 | } |
| 801 | |
| 802 | static ulong rk3308_crypto_set_clk(struct rk3308_clk_priv *priv, ulong clk_id, |
| 803 | ulong hz) |
| 804 | { |
| 805 | struct rk3308_cru *cru = priv->cru; |
| 806 | int src_clk_div; |
| 807 | |
| 808 | src_clk_div = DIV_ROUND_UP(priv->vpll0_hz, hz); |
| 809 | assert(src_clk_div - 1 <= 31); |
| 810 | |
| 811 | /* |
| 812 | * select gpll as crypto clock source and |
| 813 | * set up dependent divisors for crypto clocks. |
| 814 | */ |
| 815 | switch (clk_id) { |
| 816 | case SCLK_CRYPTO: |
| 817 | rk_clrsetreg(&cru->clksel_con[7], |
| 818 | CRYPTO_PLL_SEL_MASK | CRYPTO_DIV_MASK, |
| 819 | CRYPTO_PLL_SEL_VPLL0 << CRYPTO_PLL_SEL_SHIFT | |
| 820 | (src_clk_div - 1) << CRYPTO_DIV_SHIFT); |
| 821 | break; |
| 822 | case SCLK_CRYPTO_APK: |
| 823 | rk_clrsetreg(&cru->clksel_con[7], |
| 824 | CRYPTO_APK_PLL_SEL_MASK | CRYPTO_APK_DIV_MASK, |
| 825 | CRYPTO_PLL_SEL_VPLL0 << CRYPTO_APK_SEL_SHIFT | |
| 826 | (src_clk_div - 1) << CRYPTO_APK_DIV_SHIFT); |
| 827 | break; |
| 828 | default: |
| 829 | printf("do not support this peri freq\n"); |
| 830 | return -EINVAL; |
| 831 | } |
| 832 | |
| 833 | return rk3308_crypto_get_clk(priv, clk_id); |
| 834 | } |
| 835 | |
| 836 | static ulong rk3308_clk_get_rate(struct clk *clk) |
| 837 | { |
| 838 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 839 | ulong rate = 0; |
| 840 | |
| 841 | debug("%s id:%ld\n", __func__, clk->id); |
| 842 | |
| 843 | switch (clk->id) { |
| 844 | case PLL_APLL: |
| 845 | case ARMCLK: |
| 846 | rate = rockchip_pll_get_rate(&rk3308_pll_clks[APLL], |
| 847 | priv->cru, APLL); |
| 848 | break; |
| 849 | case PLL_DPLL: |
| 850 | rate = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL], |
| 851 | priv->cru, DPLL); |
| 852 | break; |
| 853 | case PLL_VPLL0: |
| 854 | rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL0], |
| 855 | priv->cru, VPLL0); |
| 856 | break; |
| 857 | case PLL_VPLL1: |
| 858 | rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL1], |
| 859 | priv->cru, VPLL1); |
| 860 | break; |
| 861 | case HCLK_SDMMC: |
| 862 | case HCLK_EMMC: |
| 863 | case SCLK_SDMMC: |
| 864 | case SCLK_EMMC: |
| 865 | case SCLK_EMMC_SAMPLE: |
| 866 | rate = rk3308_mmc_get_clk(clk); |
| 867 | break; |
Massimo Pegorer | 2d7b0d0 | 2023-08-03 13:08:12 +0200 | [diff] [blame] | 868 | case SCLK_UART0: |
| 869 | case SCLK_UART1: |
| 870 | case SCLK_UART2: |
| 871 | case SCLK_UART3: |
| 872 | case SCLK_UART4: |
| 873 | rate = rk3308_uart_get_clk(clk); |
| 874 | break; |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 875 | case SCLK_I2C0: |
| 876 | case SCLK_I2C1: |
| 877 | case SCLK_I2C2: |
| 878 | case SCLK_I2C3: |
| 879 | rate = rk3308_i2c_get_clk(clk); |
| 880 | break; |
| 881 | case SCLK_SARADC: |
| 882 | rate = rk3308_saradc_get_clk(clk); |
| 883 | break; |
| 884 | case SCLK_TSADC: |
| 885 | rate = rk3308_tsadc_get_clk(clk); |
| 886 | break; |
| 887 | case SCLK_SPI0: |
| 888 | case SCLK_SPI1: |
| 889 | rate = rk3308_spi_get_clk(clk); |
| 890 | break; |
| 891 | case SCLK_PWM0: |
| 892 | rate = rk3308_pwm_get_clk(clk); |
| 893 | break; |
| 894 | case DCLK_VOP: |
| 895 | rate = rk3308_vop_get_clk(clk); |
| 896 | break; |
| 897 | case ACLK_BUS: |
| 898 | case HCLK_BUS: |
| 899 | case PCLK_BUS: |
| 900 | case PCLK_WDT: |
| 901 | rate = rk3308_bus_get_clk(priv, clk->id); |
| 902 | break; |
| 903 | case ACLK_PERI: |
| 904 | case HCLK_PERI: |
| 905 | case PCLK_PERI: |
| 906 | rate = rk3308_peri_get_clk(priv, clk->id); |
| 907 | break; |
| 908 | case HCLK_AUDIO: |
| 909 | case PCLK_AUDIO: |
| 910 | rate = rk3308_audio_get_clk(priv, clk->id); |
| 911 | break; |
| 912 | case SCLK_CRYPTO: |
| 913 | case SCLK_CRYPTO_APK: |
| 914 | rate = rk3308_crypto_get_clk(priv, clk->id); |
| 915 | break; |
| 916 | default: |
| 917 | return -ENOENT; |
| 918 | } |
| 919 | |
| 920 | return rate; |
| 921 | } |
| 922 | |
| 923 | static ulong rk3308_clk_set_rate(struct clk *clk, ulong rate) |
| 924 | { |
| 925 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 926 | ulong ret = 0; |
| 927 | |
| 928 | debug("%s %ld %ld\n", __func__, clk->id, rate); |
| 929 | |
| 930 | switch (clk->id) { |
| 931 | case PLL_DPLL: |
| 932 | ret = rockchip_pll_set_rate(&rk3308_pll_clks[DPLL], priv->cru, |
| 933 | DPLL, rate); |
| 934 | priv->dpll_hz = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL], |
| 935 | priv->cru, DPLL); |
| 936 | break; |
| 937 | case ARMCLK: |
| 938 | if (priv->armclk_hz) |
| 939 | rk3308_armclk_set_clk(priv, rate); |
| 940 | priv->armclk_hz = rate; |
| 941 | break; |
| 942 | case HCLK_SDMMC: |
| 943 | case HCLK_EMMC: |
| 944 | case SCLK_SDMMC: |
| 945 | case SCLK_EMMC: |
| 946 | ret = rk3308_mmc_set_clk(clk, rate); |
| 947 | break; |
| 948 | case SCLK_I2C0: |
| 949 | case SCLK_I2C1: |
| 950 | case SCLK_I2C2: |
| 951 | case SCLK_I2C3: |
| 952 | ret = rk3308_i2c_set_clk(clk, rate); |
| 953 | break; |
| 954 | case SCLK_MAC: |
| 955 | ret = rk3308_mac_set_clk(clk, rate); |
| 956 | break; |
| 957 | case SCLK_MAC_RMII: |
| 958 | ret = rk3308_mac_set_speed_clk(clk, rate); |
| 959 | break; |
| 960 | case SCLK_SARADC: |
| 961 | ret = rk3308_saradc_set_clk(clk, rate); |
| 962 | break; |
| 963 | case SCLK_TSADC: |
| 964 | ret = rk3308_tsadc_set_clk(clk, rate); |
| 965 | break; |
| 966 | case SCLK_SPI0: |
| 967 | case SCLK_SPI1: |
| 968 | ret = rk3308_spi_set_clk(clk, rate); |
| 969 | break; |
| 970 | case SCLK_PWM0: |
| 971 | ret = rk3308_pwm_set_clk(clk, rate); |
| 972 | break; |
| 973 | case DCLK_VOP: |
| 974 | ret = rk3308_vop_set_clk(clk, rate); |
| 975 | break; |
| 976 | case ACLK_BUS: |
| 977 | case HCLK_BUS: |
| 978 | case PCLK_BUS: |
| 979 | rate = rk3308_bus_set_clk(priv, clk->id, rate); |
| 980 | break; |
| 981 | case ACLK_PERI: |
| 982 | case HCLK_PERI: |
| 983 | case PCLK_PERI: |
| 984 | rate = rk3308_peri_set_clk(priv, clk->id, rate); |
| 985 | break; |
| 986 | case HCLK_AUDIO: |
| 987 | case PCLK_AUDIO: |
| 988 | rate = rk3308_audio_set_clk(priv, clk->id, rate); |
| 989 | break; |
| 990 | case SCLK_CRYPTO: |
| 991 | case SCLK_CRYPTO_APK: |
| 992 | ret = rk3308_crypto_set_clk(priv, clk->id, rate); |
| 993 | break; |
| 994 | default: |
| 995 | return -ENOENT; |
| 996 | } |
| 997 | |
| 998 | return ret; |
| 999 | } |
| 1000 | |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 1001 | #if CONFIG_IS_ENABLED(OF_REAL) |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 1002 | static int __maybe_unused rk3308_mac_set_parent(struct clk *clk, struct clk *parent) |
| 1003 | { |
| 1004 | struct rk3308_clk_priv *priv = dev_get_priv(clk->dev); |
| 1005 | |
| 1006 | /* |
| 1007 | * If the requested parent is in the same clock-controller and |
| 1008 | * the id is SCLK_MAC_SRC, switch to the internal clock. |
| 1009 | */ |
| 1010 | if (parent->id == SCLK_MAC_SRC) { |
| 1011 | debug("%s: switching RMII to SCLK_MAC\n", __func__); |
| 1012 | rk_clrreg(&priv->cru->clksel_con[43], BIT(14)); |
| 1013 | } else { |
| 1014 | debug("%s: switching RMII to CLKIN\n", __func__); |
| 1015 | rk_setreg(&priv->cru->clksel_con[43], BIT(14)); |
| 1016 | } |
| 1017 | |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | static int __maybe_unused rk3308_clk_set_parent(struct clk *clk, struct clk *parent) |
| 1022 | { |
| 1023 | switch (clk->id) { |
| 1024 | case SCLK_MAC: |
| 1025 | return rk3308_mac_set_parent(clk, parent); |
| 1026 | default: |
| 1027 | break; |
| 1028 | } |
| 1029 | |
| 1030 | debug("%s: unsupported clk %ld\n", __func__, clk->id); |
| 1031 | return -ENOENT; |
| 1032 | } |
| 1033 | #endif |
| 1034 | |
| 1035 | static struct clk_ops rk3308_clk_ops = { |
| 1036 | .get_rate = rk3308_clk_get_rate, |
| 1037 | .set_rate = rk3308_clk_set_rate, |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 1038 | #if CONFIG_IS_ENABLED(OF_REAL) |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 1039 | .set_parent = rk3308_clk_set_parent, |
| 1040 | #endif |
| 1041 | }; |
| 1042 | |
| 1043 | static void rk3308_clk_init(struct udevice *dev) |
| 1044 | { |
| 1045 | struct rk3308_clk_priv *priv = dev_get_priv(dev); |
| 1046 | int ret; |
| 1047 | |
| 1048 | if (rockchip_pll_get_rate(&rk3308_pll_clks[APLL], |
| 1049 | priv->cru, APLL) != APLL_HZ) { |
| 1050 | ret = rk3308_armclk_set_clk(priv, APLL_HZ); |
| 1051 | if (ret < 0) |
| 1052 | printf("%s failed to set armclk rate\n", __func__); |
| 1053 | } |
| 1054 | |
| 1055 | rk3308_clk_get_pll_rate(priv); |
| 1056 | |
| 1057 | rk3308_bus_set_clk(priv, ACLK_BUS, BUS_ACLK_HZ); |
| 1058 | rk3308_bus_set_clk(priv, HCLK_BUS, BUS_HCLK_HZ); |
| 1059 | rk3308_bus_set_clk(priv, PCLK_BUS, BUS_PCLK_HZ); |
| 1060 | |
| 1061 | rk3308_peri_set_clk(priv, ACLK_PERI, PERI_ACLK_HZ); |
| 1062 | rk3308_peri_set_clk(priv, HCLK_PERI, PERI_HCLK_HZ); |
| 1063 | rk3308_peri_set_clk(priv, PCLK_PERI, PERI_PCLK_HZ); |
| 1064 | |
| 1065 | rk3308_audio_set_clk(priv, HCLK_AUDIO, AUDIO_HCLK_HZ); |
| 1066 | rk3308_audio_set_clk(priv, PCLK_AUDIO, AUDIO_PCLK_HZ); |
| 1067 | } |
| 1068 | |
| 1069 | static int rk3308_clk_probe(struct udevice *dev) |
| 1070 | { |
| 1071 | int ret; |
| 1072 | |
| 1073 | rk3308_clk_init(dev); |
| 1074 | |
| 1075 | /* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */ |
Sean Anderson | 08d531c | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 1076 | ret = clk_set_defaults(dev, CLK_DEFAULTS_POST); |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 1077 | if (ret) |
| 1078 | debug("%s clk_set_defaults failed %d\n", __func__, ret); |
| 1079 | |
| 1080 | return ret; |
| 1081 | } |
| 1082 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1083 | static int rk3308_clk_of_to_plat(struct udevice *dev) |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 1084 | { |
| 1085 | struct rk3308_clk_priv *priv = dev_get_priv(dev); |
| 1086 | |
| 1087 | priv->cru = dev_read_addr_ptr(dev); |
| 1088 | |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | static int rk3308_clk_bind(struct udevice *dev) |
| 1093 | { |
| 1094 | int ret; |
| 1095 | struct udevice *sys_child; |
| 1096 | struct sysreset_reg *priv; |
| 1097 | |
| 1098 | /* The reset driver does not have a device node, so bind it here */ |
| 1099 | ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset", |
| 1100 | &sys_child); |
| 1101 | if (ret) { |
| 1102 | debug("Warning: No sysreset driver: ret=%d\n", ret); |
| 1103 | } else { |
| 1104 | priv = malloc(sizeof(struct sysreset_reg)); |
| 1105 | priv->glb_srst_fst_value = offsetof(struct rk3308_cru, |
| 1106 | glb_srst_fst); |
| 1107 | priv->glb_srst_snd_value = offsetof(struct rk3308_cru, |
| 1108 | glb_srst_snd); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1109 | dev_set_priv(sys_child, priv); |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | #if CONFIG_IS_ENABLED(RESET_ROCKCHIP) |
| 1113 | ret = offsetof(struct rk3308_cru, softrst_con[0]); |
| 1114 | ret = rockchip_reset_bind(dev, ret, 12); |
| 1115 | if (ret) |
Eugen Hristev | f179826 | 2023-04-11 10:17:56 +0300 | [diff] [blame] | 1116 | debug("Warning: software reset driver bind failed\n"); |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 1117 | #endif |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | static const struct udevice_id rk3308_clk_ids[] = { |
| 1123 | { .compatible = "rockchip,rk3308-cru" }, |
| 1124 | { } |
| 1125 | }; |
| 1126 | |
| 1127 | U_BOOT_DRIVER(rockchip_rk3308_cru) = { |
| 1128 | .name = "rockchip_rk3308_cru", |
| 1129 | .id = UCLASS_CLK, |
| 1130 | .of_match = rk3308_clk_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1131 | .priv_auto = sizeof(struct rk3308_clk_priv), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1132 | .of_to_plat = rk3308_clk_of_to_plat, |
Finley Xiao | afa7160 | 2019-11-14 11:21:13 +0800 | [diff] [blame] | 1133 | .ops = &rk3308_clk_ops, |
| 1134 | .bind = rk3308_clk_bind, |
| 1135 | .probe = rk3308_clk_probe, |
| 1136 | }; |