Paweł Jarosz | bca89a5 | 2022-04-16 17:09:39 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * (C) Copyright 2016 Heiko Stuebner <heiko@sntech.de> |
| 5 | */ |
| 6 | |
| 7 | #include <bitfield.h> |
| 8 | #include <common.h> |
| 9 | #include <clk-uclass.h> |
| 10 | #include <dm.h> |
| 11 | #include <dt-structs.h> |
| 12 | #include <errno.h> |
| 13 | #include <log.h> |
| 14 | #include <malloc.h> |
| 15 | #include <mapmem.h> |
| 16 | #include <syscon.h> |
| 17 | #include <asm/io.h> |
| 18 | #include <asm/arch-rockchip/clock.h> |
| 19 | #include <asm/arch-rockchip/cru_rk3066.h> |
| 20 | #include <asm/arch-rockchip/grf_rk3066.h> |
| 21 | #include <asm/arch-rockchip/hardware.h> |
| 22 | #include <dt-bindings/clock/rk3066a-cru.h> |
| 23 | #include <dm/device_compat.h> |
| 24 | #include <dm/device-internal.h> |
| 25 | #include <dm/lists.h> |
| 26 | #include <dm/uclass-internal.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/log2.h> |
| 30 | #include <linux/stringify.h> |
| 31 | |
| 32 | struct rk3066_clk_plat { |
| 33 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 34 | struct dtd_rockchip_rk3066a_cru dtd; |
| 35 | #endif |
| 36 | }; |
| 37 | |
| 38 | struct pll_div { |
| 39 | u32 nr; |
| 40 | u32 nf; |
| 41 | u32 no; |
| 42 | }; |
| 43 | |
| 44 | enum { |
| 45 | VCO_MAX_HZ = 1416U * 1000000, |
| 46 | VCO_MIN_HZ = 300 * 1000000, |
| 47 | OUTPUT_MAX_HZ = 1416U * 1000000, |
| 48 | OUTPUT_MIN_HZ = 30 * 1000000, |
| 49 | FREF_MAX_HZ = 1416U * 1000000, |
| 50 | FREF_MIN_HZ = 30 * 1000, |
| 51 | }; |
| 52 | |
| 53 | enum { |
| 54 | /* PLL CON0 */ |
| 55 | PLL_OD_MASK = GENMASK(3, 0), |
| 56 | |
| 57 | /* PLL CON1 */ |
| 58 | PLL_NF_MASK = GENMASK(12, 0), |
| 59 | |
| 60 | /* PLL CON2 */ |
| 61 | PLL_BWADJ_MASK = GENMASK(11, 0), |
| 62 | |
| 63 | /* PLL CON3 */ |
| 64 | PLL_RESET_SHIFT = 5, |
| 65 | |
| 66 | /* GRF_SOC_STATUS0 */ |
| 67 | SOCSTS_DPLL_LOCK = BIT(4), |
| 68 | SOCSTS_APLL_LOCK = BIT(5), |
| 69 | SOCSTS_CPLL_LOCK = BIT(6), |
| 70 | SOCSTS_GPLL_LOCK = BIT(7), |
| 71 | }; |
| 72 | |
| 73 | #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1)) |
| 74 | |
| 75 | #define PLL_DIVISORS(hz, _nr, _no) {\ |
| 76 | .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\ |
| 77 | _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\ |
| 78 | (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\ |
| 79 | "divisors on line " __stringify(__LINE__)) |
| 80 | |
| 81 | /* Keep divisors as low as possible to reduce jitter and power usage. */ |
| 82 | static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2); |
| 83 | static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2); |
| 84 | |
| 85 | static int rk3066_clk_set_pll(struct rk3066_cru *cru, enum rk_clk_id clk_id, |
| 86 | const struct pll_div *div) |
| 87 | { |
| 88 | int pll_id = rk_pll_id(clk_id); |
| 89 | struct rk3066_pll *pll = &cru->pll[pll_id]; |
| 90 | /* All PLLs have the same VCO and output frequency range restrictions. */ |
| 91 | uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000; |
| 92 | uint output_hz = vco_hz / div->no; |
| 93 | |
| 94 | debug("%s: PLL at %x: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n", __func__, |
| 95 | (uint)pll, div->nf, div->nr, div->no, vco_hz, output_hz); |
| 96 | assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ && |
| 97 | output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ && |
| 98 | (div->no == 1 || !(div->no % 2))); |
| 99 | |
| 100 | /* Enter reset. */ |
| 101 | rk_setreg(&pll->con3, BIT(PLL_RESET_SHIFT)); |
| 102 | |
| 103 | rk_clrsetreg(&pll->con0, |
| 104 | CLKR_MASK | PLL_OD_MASK, |
| 105 | ((div->nr - 1) << CLKR_SHIFT) | (div->no - 1)); |
| 106 | rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1); |
| 107 | |
| 108 | rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1); |
| 109 | |
| 110 | /* Exit reset. */ |
| 111 | rk_clrreg(&pll->con3, BIT(PLL_RESET_SHIFT)); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int rk3066_clk_configure_ddr(struct rk3066_cru *cru, struct rk3066_grf *grf, |
| 117 | unsigned int hz) |
| 118 | { |
| 119 | static const struct pll_div dpll_cfg[] = { |
| 120 | {.nf = 25, .nr = 2, .no = 1}, |
| 121 | {.nf = 400, .nr = 9, .no = 2}, |
| 122 | {.nf = 500, .nr = 9, .no = 2}, |
| 123 | {.nf = 100, .nr = 3, .no = 1}, |
| 124 | }; |
| 125 | int cfg; |
| 126 | |
| 127 | switch (hz) { |
| 128 | case 300000000: |
| 129 | cfg = 0; |
| 130 | break; |
| 131 | case 533000000: /* actually 533.3P MHz */ |
| 132 | cfg = 1; |
| 133 | break; |
| 134 | case 666000000: /* actually 666.6P MHz */ |
| 135 | cfg = 2; |
| 136 | break; |
| 137 | case 800000000: |
| 138 | cfg = 3; |
| 139 | break; |
| 140 | default: |
| 141 | debug("%s: unsupported SDRAM frequency", __func__); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | |
| 145 | /* Enter PLL slow mode. */ |
| 146 | rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK, |
| 147 | PLL_MODE_SLOW << DPLL_MODE_SHIFT); |
| 148 | |
| 149 | rk3066_clk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]); |
| 150 | |
| 151 | /* Wait for PLL lock. */ |
| 152 | while (!(readl(&grf->soc_status0) & SOCSTS_DPLL_LOCK)) |
| 153 | udelay(1); |
| 154 | |
| 155 | /* Enter PLL normal mode. */ |
| 156 | rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK, |
| 157 | PLL_MODE_NORMAL << DPLL_MODE_SHIFT); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int rk3066_clk_configure_cpu(struct rk3066_cru *cru, struct rk3066_grf *grf, |
| 163 | unsigned int hz) |
| 164 | { |
| 165 | static const struct pll_div apll_cfg[] = { |
| 166 | {.nf = 50, .nr = 1, .no = 2}, |
| 167 | {.nf = 59, .nr = 1, .no = 1}, |
| 168 | }; |
| 169 | int div_core_peri, div_cpu_aclk, cfg; |
| 170 | |
| 171 | /* |
| 172 | * We support two possible frequencies, the safe 600MHz |
| 173 | * which will work with default pmic settings and will |
| 174 | * be set to get away from the 24MHz default and |
| 175 | * the maximum of 1.416Ghz, which boards can set if they |
| 176 | * were able to get pmic support for it. |
| 177 | */ |
| 178 | switch (hz) { |
| 179 | case APLL_SAFE_HZ: |
| 180 | cfg = 0; |
| 181 | div_core_peri = 1; |
| 182 | div_cpu_aclk = 3; |
| 183 | break; |
| 184 | case APLL_HZ: |
| 185 | cfg = 1; |
| 186 | div_core_peri = 2; |
| 187 | div_cpu_aclk = 3; |
| 188 | break; |
| 189 | default: |
| 190 | debug("unsupported ARMCLK frequency"); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | /* Enter PLL slow mode. */ |
| 195 | rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK, |
| 196 | PLL_MODE_SLOW << APLL_MODE_SHIFT); |
| 197 | |
| 198 | rk3066_clk_set_pll(cru, CLK_ARM, &apll_cfg[cfg]); |
| 199 | |
| 200 | /* Wait for PLL lock. */ |
| 201 | while (!(readl(&grf->soc_status0) & SOCSTS_APLL_LOCK)) |
| 202 | udelay(1); |
| 203 | |
| 204 | /* Set divider for peripherals attached to the CPU core. */ |
| 205 | rk_clrsetreg(&cru->cru_clksel_con[0], |
| 206 | CORE_PERI_DIV_MASK, |
| 207 | div_core_peri << CORE_PERI_DIV_SHIFT); |
| 208 | |
| 209 | /* Set up dependent divisor for cpu_aclk. */ |
| 210 | rk_clrsetreg(&cru->cru_clksel_con[1], |
| 211 | CPU_ACLK_DIV_MASK, |
| 212 | div_cpu_aclk << CPU_ACLK_DIV_SHIFT); |
| 213 | |
| 214 | /* Enter PLL normal mode. */ |
| 215 | rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK, |
| 216 | PLL_MODE_NORMAL << APLL_MODE_SHIFT); |
| 217 | |
| 218 | return hz; |
| 219 | } |
| 220 | |
| 221 | static uint32_t rk3066_clk_pll_get_rate(struct rk3066_cru *cru, |
| 222 | enum rk_clk_id clk_id) |
| 223 | { |
| 224 | u32 nr, no, nf; |
| 225 | u32 con; |
| 226 | int pll_id = rk_pll_id(clk_id); |
| 227 | struct rk3066_pll *pll = &cru->pll[pll_id]; |
| 228 | static u8 clk_shift[CLK_COUNT] = { |
| 229 | 0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT, |
| 230 | GPLL_MODE_SHIFT |
| 231 | }; |
| 232 | uint shift; |
| 233 | |
| 234 | con = readl(&cru->cru_mode_con); |
| 235 | shift = clk_shift[clk_id]; |
| 236 | switch (FIELD_GET(APLL_MODE_MASK, con >> shift)) { |
| 237 | case PLL_MODE_SLOW: |
| 238 | return OSC_HZ; |
| 239 | case PLL_MODE_NORMAL: |
| 240 | /* normal mode */ |
| 241 | con = readl(&pll->con0); |
| 242 | no = bitfield_extract_by_mask(con, CLKOD_MASK) + 1; |
| 243 | nr = bitfield_extract_by_mask(con, CLKR_MASK) + 1; |
| 244 | con = readl(&pll->con1); |
| 245 | nf = bitfield_extract_by_mask(con, CLKF_MASK) + 1; |
| 246 | |
| 247 | return (OSC_HZ * nf) / (nr * no); |
| 248 | case PLL_MODE_DEEP: |
| 249 | default: |
| 250 | return 32768; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static ulong rk3066_clk_mmc_get_clk(struct rk3066_cru *cru, uint gclk_rate, |
| 255 | int periph) |
| 256 | { |
| 257 | uint div; |
| 258 | u32 con; |
| 259 | |
| 260 | switch (periph) { |
| 261 | case HCLK_EMMC: |
| 262 | case SCLK_EMMC: |
| 263 | con = readl(&cru->cru_clksel_con[12]); |
| 264 | div = bitfield_extract_by_mask(con, EMMC_DIV_MASK); |
| 265 | break; |
| 266 | case HCLK_SDMMC: |
| 267 | case SCLK_SDMMC: |
| 268 | con = readl(&cru->cru_clksel_con[11]); |
| 269 | div = bitfield_extract_by_mask(con, MMC0_DIV_MASK); |
| 270 | break; |
| 271 | case HCLK_SDIO: |
| 272 | case SCLK_SDIO: |
| 273 | con = readl(&cru->cru_clksel_con[12]); |
| 274 | div = bitfield_extract_by_mask(con, SDIO_DIV_MASK); |
| 275 | break; |
| 276 | default: |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | return DIV_TO_RATE(gclk_rate, div) / 2; |
| 281 | } |
| 282 | |
| 283 | static ulong rk3066_clk_mmc_set_clk(struct rk3066_cru *cru, uint gclk_rate, |
| 284 | int periph, uint freq) |
| 285 | { |
| 286 | int src_clk_div; |
| 287 | |
| 288 | debug("%s: gclk_rate=%u\n", __func__, gclk_rate); |
| 289 | /* MMC clock by default divides by 2 internally, so need to provide double in CRU. */ |
| 290 | src_clk_div = DIV_ROUND_UP(gclk_rate / 2, freq) - 1; |
| 291 | assert(src_clk_div <= 0x3f); |
| 292 | |
| 293 | switch (periph) { |
| 294 | case HCLK_EMMC: |
| 295 | case SCLK_EMMC: |
| 296 | rk_clrsetreg(&cru->cru_clksel_con[12], |
| 297 | EMMC_DIV_MASK, |
| 298 | src_clk_div << EMMC_DIV_SHIFT); |
| 299 | break; |
| 300 | case HCLK_SDMMC: |
| 301 | case SCLK_SDMMC: |
| 302 | rk_clrsetreg(&cru->cru_clksel_con[11], |
| 303 | MMC0_DIV_MASK, |
| 304 | src_clk_div << MMC0_DIV_SHIFT); |
| 305 | break; |
| 306 | case HCLK_SDIO: |
| 307 | case SCLK_SDIO: |
| 308 | rk_clrsetreg(&cru->cru_clksel_con[12], |
| 309 | SDIO_DIV_MASK, |
| 310 | src_clk_div << SDIO_DIV_SHIFT); |
| 311 | break; |
| 312 | default: |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | |
| 316 | return rk3066_clk_mmc_get_clk(cru, gclk_rate, periph); |
| 317 | } |
| 318 | |
| 319 | static ulong rk3066_clk_spi_get_clk(struct rk3066_cru *cru, uint gclk_rate, |
| 320 | int periph) |
| 321 | { |
| 322 | uint div; |
| 323 | u32 con; |
| 324 | |
| 325 | switch (periph) { |
| 326 | case SCLK_SPI0: |
| 327 | con = readl(&cru->cru_clksel_con[25]); |
| 328 | div = bitfield_extract_by_mask(con, SPI0_DIV_MASK); |
| 329 | break; |
| 330 | case SCLK_SPI1: |
| 331 | con = readl(&cru->cru_clksel_con[25]); |
| 332 | div = bitfield_extract_by_mask(con, SPI1_DIV_MASK); |
| 333 | break; |
| 334 | default: |
| 335 | return -EINVAL; |
| 336 | } |
| 337 | |
| 338 | return DIV_TO_RATE(gclk_rate, div); |
| 339 | } |
| 340 | |
| 341 | static ulong rk3066_clk_spi_set_clk(struct rk3066_cru *cru, uint gclk_rate, |
| 342 | int periph, uint freq) |
| 343 | { |
| 344 | int src_clk_div = DIV_ROUND_UP(gclk_rate, freq) - 1; |
| 345 | |
| 346 | assert(src_clk_div < 128); |
| 347 | switch (periph) { |
| 348 | case SCLK_SPI0: |
| 349 | assert(src_clk_div <= SPI0_DIV_MASK >> SPI0_DIV_SHIFT); |
| 350 | rk_clrsetreg(&cru->cru_clksel_con[25], |
| 351 | SPI0_DIV_MASK, |
| 352 | src_clk_div << SPI0_DIV_SHIFT); |
| 353 | break; |
| 354 | case SCLK_SPI1: |
| 355 | assert(src_clk_div <= SPI1_DIV_MASK >> SPI1_DIV_SHIFT); |
| 356 | rk_clrsetreg(&cru->cru_clksel_con[25], |
| 357 | SPI1_DIV_MASK, |
| 358 | src_clk_div << SPI1_DIV_SHIFT); |
| 359 | break; |
| 360 | default: |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
| 364 | return rk3066_clk_spi_get_clk(cru, gclk_rate, periph); |
| 365 | } |
| 366 | |
| 367 | static ulong rk3066_clk_saradc_get_clk(struct rk3066_cru *cru, int periph) |
| 368 | { |
| 369 | u32 div, con; |
| 370 | |
| 371 | switch (periph) { |
| 372 | case SCLK_SARADC: |
| 373 | con = readl(&cru->cru_clksel_con[24]); |
| 374 | div = bitfield_extract_by_mask(con, SARADC_DIV_MASK); |
| 375 | break; |
| 376 | case SCLK_TSADC: |
| 377 | con = readl(&cru->cru_clksel_con[34]); |
| 378 | div = bitfield_extract_by_mask(con, TSADC_DIV_MASK); |
| 379 | break; |
| 380 | default: |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | return DIV_TO_RATE(PERI_PCLK_HZ, div); |
| 384 | } |
| 385 | |
| 386 | static ulong rk3066_clk_saradc_set_clk(struct rk3066_cru *cru, uint hz, |
| 387 | int periph) |
| 388 | { |
| 389 | int src_clk_div; |
| 390 | |
| 391 | src_clk_div = DIV_ROUND_UP(PERI_PCLK_HZ, hz) - 1; |
| 392 | assert(src_clk_div < 128); |
| 393 | |
| 394 | switch (periph) { |
| 395 | case SCLK_SARADC: |
| 396 | rk_clrsetreg(&cru->cru_clksel_con[24], |
| 397 | SARADC_DIV_MASK, |
| 398 | src_clk_div << SARADC_DIV_SHIFT); |
| 399 | break; |
| 400 | case SCLK_TSADC: |
| 401 | rk_clrsetreg(&cru->cru_clksel_con[34], |
| 402 | SARADC_DIV_MASK, |
| 403 | src_clk_div << SARADC_DIV_SHIFT); |
| 404 | break; |
| 405 | default: |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | return rk3066_clk_saradc_get_clk(cru, periph); |
| 410 | } |
| 411 | |
| 412 | static void rk3066_clk_init(struct rk3066_cru *cru, struct rk3066_grf *grf) |
| 413 | { |
| 414 | u32 aclk_div, hclk_div, pclk_div, h2p_div; |
| 415 | |
| 416 | /* Enter PLL slow mode. */ |
| 417 | rk_clrsetreg(&cru->cru_mode_con, |
| 418 | GPLL_MODE_MASK | |
| 419 | CPLL_MODE_MASK, |
| 420 | PLL_MODE_SLOW << GPLL_MODE_SHIFT | |
| 421 | PLL_MODE_SLOW << CPLL_MODE_SHIFT); |
| 422 | |
| 423 | /* Init PLL. */ |
| 424 | rk3066_clk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg); |
| 425 | rk3066_clk_set_pll(cru, CLK_CODEC, &cpll_init_cfg); |
| 426 | |
| 427 | /* Wait for PLL lock. */ |
| 428 | while ((readl(&grf->soc_status0) & |
| 429 | (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) != |
| 430 | (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) |
| 431 | udelay(1); |
| 432 | |
| 433 | /* |
| 434 | * Select CPU clock PLL source and |
| 435 | * reparent aclk_cpu_pre from APPL to GPLL. |
| 436 | * Set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 437 | */ |
| 438 | aclk_div = DIV_ROUND_UP(GPLL_HZ, CPU_ACLK_HZ) - 1; |
| 439 | assert((aclk_div + 1) * CPU_ACLK_HZ == GPLL_HZ && aclk_div <= 0x1f); |
| 440 | |
| 441 | rk_clrsetreg(&cru->cru_clksel_con[0], |
| 442 | CPU_ACLK_PLL_MASK | |
| 443 | A9_CORE_DIV_MASK, |
| 444 | CPU_ACLK_PLL_SELECT_GPLL << CPU_ACLK_PLL_SHIFT | |
| 445 | aclk_div << A9_CORE_DIV_SHIFT); |
| 446 | |
| 447 | hclk_div = ilog2(CPU_ACLK_HZ / CPU_HCLK_HZ); |
| 448 | assert((1 << hclk_div) * CPU_HCLK_HZ == CPU_ACLK_HZ && hclk_div < 0x3); |
| 449 | pclk_div = ilog2(CPU_ACLK_HZ / CPU_PCLK_HZ); |
| 450 | assert((1 << pclk_div) * CPU_PCLK_HZ == CPU_ACLK_HZ && pclk_div < 0x4); |
| 451 | h2p_div = ilog2(CPU_HCLK_HZ / CPU_H2P_HZ); |
| 452 | assert((1 << h2p_div) * CPU_H2P_HZ == CPU_HCLK_HZ && pclk_div < 0x3); |
| 453 | |
| 454 | rk_clrsetreg(&cru->cru_clksel_con[1], |
| 455 | AHB2APB_DIV_MASK | |
| 456 | CPU_PCLK_DIV_MASK | |
| 457 | CPU_HCLK_DIV_MASK, |
| 458 | h2p_div << AHB2APB_DIV_SHIFT | |
| 459 | pclk_div << CPU_PCLK_DIV_SHIFT | |
| 460 | hclk_div << CPU_HCLK_DIV_SHIFT); |
| 461 | |
| 462 | /* |
| 463 | * Select PERI clock PLL source and |
| 464 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 465 | */ |
| 466 | aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1; |
| 467 | assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 468 | |
| 469 | hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ); |
| 470 | assert((1 << hclk_div) * PERI_HCLK_HZ == |
| 471 | PERI_ACLK_HZ && (hclk_div < 0x4)); |
| 472 | |
| 473 | pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ); |
| 474 | assert((1 << pclk_div) * PERI_PCLK_HZ == |
| 475 | PERI_ACLK_HZ && (pclk_div < 0x4)); |
| 476 | |
| 477 | rk_clrsetreg(&cru->cru_clksel_con[10], |
| 478 | PERI_PCLK_DIV_MASK | |
| 479 | PERI_HCLK_DIV_MASK | |
| 480 | PERI_ACLK_DIV_MASK, |
| 481 | PERI_SEL_GPLL << PERI_SEL_PLL_SHIFT | |
| 482 | pclk_div << PERI_PCLK_DIV_SHIFT | |
| 483 | hclk_div << PERI_HCLK_DIV_SHIFT | |
| 484 | aclk_div << PERI_ACLK_DIV_SHIFT); |
| 485 | |
| 486 | /* Enter PLL normal mode. */ |
| 487 | rk_clrsetreg(&cru->cru_mode_con, |
| 488 | GPLL_MODE_MASK | |
| 489 | CPLL_MODE_MASK, |
| 490 | PLL_MODE_NORMAL << GPLL_MODE_SHIFT | |
| 491 | PLL_MODE_NORMAL << CPLL_MODE_SHIFT); |
| 492 | |
| 493 | rk3066_clk_mmc_set_clk(cru, PERI_HCLK_HZ, HCLK_SDMMC, 16000000); |
| 494 | } |
| 495 | |
| 496 | static ulong rk3066_clk_get_rate(struct clk *clk) |
| 497 | { |
| 498 | struct rk3066_clk_priv *priv = dev_get_priv(clk->dev); |
| 499 | ulong new_rate, gclk_rate; |
| 500 | |
| 501 | gclk_rate = rk3066_clk_pll_get_rate(priv->cru, CLK_GENERAL); |
| 502 | switch (clk->id) { |
| 503 | case 1 ... 4: |
| 504 | new_rate = rk3066_clk_pll_get_rate(priv->cru, clk->id); |
| 505 | break; |
| 506 | case HCLK_EMMC: |
| 507 | case HCLK_SDMMC: |
| 508 | case HCLK_SDIO: |
| 509 | case SCLK_EMMC: |
| 510 | case SCLK_SDMMC: |
| 511 | case SCLK_SDIO: |
| 512 | new_rate = rk3066_clk_mmc_get_clk(priv->cru, PERI_HCLK_HZ, |
| 513 | clk->id); |
| 514 | break; |
| 515 | case SCLK_SPI0: |
| 516 | case SCLK_SPI1: |
| 517 | new_rate = rk3066_clk_spi_get_clk(priv->cru, PERI_PCLK_HZ, |
| 518 | clk->id); |
| 519 | break; |
| 520 | case PCLK_I2C0: |
| 521 | case PCLK_I2C1: |
| 522 | case PCLK_I2C2: |
| 523 | case PCLK_I2C3: |
| 524 | case PCLK_I2C4: |
| 525 | return gclk_rate; |
| 526 | case SCLK_SARADC: |
| 527 | case SCLK_TSADC: |
| 528 | new_rate = rk3066_clk_saradc_get_clk(priv->cru, clk->id); |
| 529 | break; |
| 530 | case SCLK_TIMER0: |
| 531 | case SCLK_TIMER1: |
| 532 | case SCLK_TIMER2: |
| 533 | case SCLK_UART0: |
| 534 | case SCLK_UART1: |
| 535 | case SCLK_UART2: |
| 536 | case SCLK_UART3: |
| 537 | return OSC_HZ; |
| 538 | default: |
| 539 | return -ENOENT; |
| 540 | } |
| 541 | |
| 542 | return new_rate; |
| 543 | } |
| 544 | |
| 545 | static ulong rk3066_clk_set_rate(struct clk *clk, ulong rate) |
| 546 | { |
| 547 | struct rk3066_clk_priv *priv = dev_get_priv(clk->dev); |
| 548 | struct rk3066_cru *cru = priv->cru; |
| 549 | ulong new_rate; |
| 550 | |
| 551 | switch (clk->id) { |
| 552 | case PLL_APLL: |
| 553 | new_rate = rk3066_clk_configure_cpu(priv->cru, priv->grf, rate); |
| 554 | break; |
| 555 | case CLK_DDR: |
| 556 | new_rate = rk3066_clk_configure_ddr(priv->cru, priv->grf, rate); |
| 557 | break; |
| 558 | case HCLK_EMMC: |
| 559 | case HCLK_SDMMC: |
| 560 | case HCLK_SDIO: |
| 561 | case SCLK_EMMC: |
| 562 | case SCLK_SDMMC: |
| 563 | case SCLK_SDIO: |
| 564 | new_rate = rk3066_clk_mmc_set_clk(cru, PERI_HCLK_HZ, |
| 565 | clk->id, rate); |
| 566 | break; |
| 567 | case SCLK_SPI0: |
| 568 | case SCLK_SPI1: |
| 569 | new_rate = rk3066_clk_spi_set_clk(cru, PERI_PCLK_HZ, |
| 570 | clk->id, rate); |
| 571 | break; |
| 572 | case SCLK_SARADC: |
| 573 | case SCLK_TSADC: |
| 574 | new_rate = rk3066_clk_saradc_set_clk(cru, rate, clk->id); |
| 575 | break; |
| 576 | case PLL_CPLL: |
| 577 | case PLL_GPLL: |
| 578 | case ACLK_CPU: |
| 579 | case HCLK_CPU: |
| 580 | case PCLK_CPU: |
| 581 | case ACLK_PERI: |
| 582 | case HCLK_PERI: |
| 583 | case PCLK_PERI: |
| 584 | return 0; |
| 585 | default: |
| 586 | return -ENOENT; |
| 587 | } |
| 588 | |
| 589 | return new_rate; |
| 590 | } |
| 591 | |
| 592 | static int rk3066_clk_enable(struct clk *clk) |
| 593 | { |
| 594 | struct rk3066_clk_priv *priv = dev_get_priv(clk->dev); |
| 595 | |
| 596 | switch (clk->id) { |
| 597 | case HCLK_NANDC0: |
| 598 | rk_clrreg(&priv->cru->cru_clkgate_con[5], BIT(9)); |
| 599 | break; |
| 600 | case HCLK_SDMMC: |
| 601 | rk_clrreg(&priv->cru->cru_clkgate_con[5], BIT(10)); |
| 602 | break; |
| 603 | case HCLK_SDIO: |
| 604 | rk_clrreg(&priv->cru->cru_clkgate_con[5], BIT(11)); |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static int rk3066_clk_disable(struct clk *clk) |
| 612 | { |
| 613 | struct rk3066_clk_priv *priv = dev_get_priv(clk->dev); |
| 614 | |
| 615 | switch (clk->id) { |
| 616 | case HCLK_NANDC0: |
| 617 | rk_setreg(&priv->cru->cru_clkgate_con[5], BIT(9)); |
| 618 | break; |
| 619 | case HCLK_SDMMC: |
| 620 | rk_setreg(&priv->cru->cru_clkgate_con[5], BIT(10)); |
| 621 | break; |
| 622 | case HCLK_SDIO: |
| 623 | rk_setreg(&priv->cru->cru_clkgate_con[5], BIT(11)); |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static struct clk_ops rk3066_clk_ops = { |
| 631 | .disable = rk3066_clk_disable, |
| 632 | .enable = rk3066_clk_enable, |
| 633 | .get_rate = rk3066_clk_get_rate, |
| 634 | .set_rate = rk3066_clk_set_rate, |
| 635 | }; |
| 636 | |
| 637 | static int rk3066_clk_of_to_plat(struct udevice *dev) |
| 638 | { |
| 639 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
| 640 | struct rk3066_clk_priv *priv = dev_get_priv(dev); |
| 641 | |
| 642 | priv->cru = dev_read_addr_ptr(dev); |
| 643 | } |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static int rk3066_clk_probe(struct udevice *dev) |
| 649 | { |
| 650 | struct rk3066_clk_priv *priv = dev_get_priv(dev); |
| 651 | |
| 652 | priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 653 | if (IS_ERR(priv->grf)) |
| 654 | return PTR_ERR(priv->grf); |
| 655 | |
| 656 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 657 | struct rk3066_clk_plat *plat = dev_get_plat(dev); |
| 658 | |
| 659 | priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]); |
| 660 | #endif |
| 661 | |
| 662 | if (IS_ENABLED(CONFIG_TPL_BUILD)) { |
| 663 | rk3066_clk_init(priv->cru, priv->grf); |
| 664 | |
| 665 | /* Init CPU frequency. */ |
| 666 | rk3066_clk_configure_cpu(priv->cru, priv->grf, APLL_SAFE_HZ); |
| 667 | } |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static int rk3066_clk_bind(struct udevice *dev) |
| 673 | { |
| 674 | struct udevice *sys_child; |
| 675 | struct sysreset_reg *priv; |
| 676 | int reg_offset, ret; |
| 677 | |
| 678 | /* The reset driver does not have a device node, so bind it here. */ |
| 679 | ret = device_bind(dev, DM_DRIVER_GET(sysreset_rockchip), "sysreset", |
| 680 | NULL, ofnode_null(), &sys_child); |
| 681 | if (ret) { |
| 682 | dev_dbg(dev, "Warning: No sysreset driver: ret=%d\n", ret); |
| 683 | } else { |
| 684 | priv = malloc(sizeof(struct sysreset_reg)); |
| 685 | priv->glb_srst_fst_value = offsetof(struct rk3066_cru, |
| 686 | cru_glb_srst_fst_value); |
| 687 | priv->glb_srst_snd_value = offsetof(struct rk3066_cru, |
| 688 | cru_glb_srst_snd_value); |
| 689 | dev_set_priv(sys_child, priv); |
| 690 | } |
| 691 | |
| 692 | if (CONFIG_IS_ENABLED(RESET_ROCKCHIP)) { |
| 693 | reg_offset = offsetof(struct rk3066_cru, cru_softrst_con[0]); |
| 694 | ret = rockchip_reset_bind(dev, reg_offset, 9); |
| 695 | if (ret) |
| 696 | dev_dbg(dev, "Warning: software reset driver bind failed\n"); |
| 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static const struct udevice_id rk3066_clk_ids[] = { |
| 703 | { .compatible = "rockchip,rk3066a-cru" }, |
| 704 | { } |
| 705 | }; |
| 706 | |
| 707 | U_BOOT_DRIVER(rockchip_rk3066a_cru) = { |
| 708 | .name = "rockchip_rk3066a_cru", |
| 709 | .id = UCLASS_CLK, |
| 710 | .ops = &rk3066_clk_ops, |
| 711 | .probe = rk3066_clk_probe, |
| 712 | .bind = rk3066_clk_bind, |
| 713 | .of_match = rk3066_clk_ids, |
| 714 | .of_to_plat = rk3066_clk_of_to_plat, |
| 715 | .priv_auto = sizeof(struct rk3066_clk_priv), |
| 716 | .plat_auto = sizeof(struct rk3066_clk_plat), |
| 717 | }; |