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