Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 Rockchip Electronics Co., Ltd |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
David Wu | a942223 | 2017-09-20 14:35:44 +0800 | [diff] [blame] | 7 | #include <bitfield.h> |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 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 | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 13 | #include <syscon.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 14 | #include <asm/arch-rockchip/clock.h> |
| 15 | #include <asm/arch-rockchip/cru_rk3328.h> |
| 16 | #include <asm/arch-rockchip/hardware.h> |
| 17 | #include <asm/arch-rockchip/grf_rk3328.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 19 | #include <dm/lists.h> |
| 20 | #include <dt-bindings/clock/rk3328-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 | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 23 | |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 24 | struct pll_div { |
| 25 | u32 refdiv; |
| 26 | u32 fbdiv; |
| 27 | u32 postdiv1; |
| 28 | u32 postdiv2; |
| 29 | u32 frac; |
| 30 | }; |
| 31 | |
| 32 | #define RATE_TO_DIV(input_rate, output_rate) \ |
| 33 | ((input_rate) / (output_rate) - 1); |
| 34 | #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1)) |
| 35 | |
| 36 | #define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\ |
| 37 | .refdiv = _refdiv,\ |
| 38 | .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\ |
| 39 | .postdiv1 = _postdiv1, .postdiv2 = _postdiv2}; |
| 40 | |
| 41 | static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 1, 4, 1); |
| 42 | static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 2, 2, 1); |
| 43 | |
| 44 | static const struct pll_div apll_816_cfg = PLL_DIVISORS(816 * MHz, 1, 2, 1); |
| 45 | static const struct pll_div apll_600_cfg = PLL_DIVISORS(600 * MHz, 1, 3, 1); |
| 46 | |
| 47 | static const struct pll_div *apll_cfgs[] = { |
| 48 | [APLL_816_MHZ] = &apll_816_cfg, |
| 49 | [APLL_600_MHZ] = &apll_600_cfg, |
| 50 | }; |
| 51 | |
| 52 | enum { |
| 53 | /* PLL_CON0 */ |
| 54 | PLL_POSTDIV1_SHIFT = 12, |
| 55 | PLL_POSTDIV1_MASK = 0x7 << PLL_POSTDIV1_SHIFT, |
| 56 | PLL_FBDIV_SHIFT = 0, |
| 57 | PLL_FBDIV_MASK = 0xfff, |
| 58 | |
| 59 | /* PLL_CON1 */ |
| 60 | PLL_DSMPD_SHIFT = 12, |
| 61 | PLL_DSMPD_MASK = 1 << PLL_DSMPD_SHIFT, |
| 62 | PLL_INTEGER_MODE = 1, |
| 63 | PLL_LOCK_STATUS_SHIFT = 10, |
| 64 | PLL_LOCK_STATUS_MASK = 1 << PLL_LOCK_STATUS_SHIFT, |
| 65 | PLL_POSTDIV2_SHIFT = 6, |
| 66 | PLL_POSTDIV2_MASK = 0x7 << PLL_POSTDIV2_SHIFT, |
| 67 | PLL_REFDIV_SHIFT = 0, |
| 68 | PLL_REFDIV_MASK = 0x3f, |
| 69 | |
| 70 | /* PLL_CON2 */ |
| 71 | PLL_FRACDIV_SHIFT = 0, |
| 72 | PLL_FRACDIV_MASK = 0xffffff, |
| 73 | |
| 74 | /* MODE_CON */ |
| 75 | APLL_MODE_SHIFT = 0, |
| 76 | NPLL_MODE_SHIFT = 1, |
| 77 | DPLL_MODE_SHIFT = 4, |
| 78 | CPLL_MODE_SHIFT = 8, |
| 79 | GPLL_MODE_SHIFT = 12, |
| 80 | PLL_MODE_SLOW = 0, |
| 81 | PLL_MODE_NORM, |
| 82 | |
| 83 | /* CLKSEL_CON0 */ |
| 84 | CLK_CORE_PLL_SEL_APLL = 0, |
| 85 | CLK_CORE_PLL_SEL_GPLL, |
| 86 | CLK_CORE_PLL_SEL_DPLL, |
| 87 | CLK_CORE_PLL_SEL_NPLL, |
| 88 | CLK_CORE_PLL_SEL_SHIFT = 6, |
| 89 | CLK_CORE_PLL_SEL_MASK = 3 << CLK_CORE_PLL_SEL_SHIFT, |
| 90 | CLK_CORE_DIV_SHIFT = 0, |
| 91 | CLK_CORE_DIV_MASK = 0x1f, |
| 92 | |
| 93 | /* CLKSEL_CON1 */ |
| 94 | ACLKM_CORE_DIV_SHIFT = 4, |
| 95 | ACLKM_CORE_DIV_MASK = 0x7 << ACLKM_CORE_DIV_SHIFT, |
| 96 | PCLK_DBG_DIV_SHIFT = 0, |
| 97 | PCLK_DBG_DIV_MASK = 0xF << PCLK_DBG_DIV_SHIFT, |
| 98 | |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 99 | /* CLKSEL_CON27 */ |
| 100 | GMAC2IO_PLL_SEL_SHIFT = 7, |
| 101 | GMAC2IO_PLL_SEL_MASK = 1 << GMAC2IO_PLL_SEL_SHIFT, |
| 102 | GMAC2IO_PLL_SEL_CPLL = 0, |
| 103 | GMAC2IO_PLL_SEL_GPLL = 1, |
| 104 | GMAC2IO_CLK_DIV_MASK = 0x1f, |
| 105 | GMAC2IO_CLK_DIV_SHIFT = 0, |
| 106 | |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 107 | /* CLKSEL_CON28 */ |
| 108 | ACLK_PERIHP_PLL_SEL_CPLL = 0, |
| 109 | ACLK_PERIHP_PLL_SEL_GPLL, |
| 110 | ACLK_PERIHP_PLL_SEL_HDMIPHY, |
| 111 | ACLK_PERIHP_PLL_SEL_SHIFT = 6, |
| 112 | ACLK_PERIHP_PLL_SEL_MASK = 3 << ACLK_PERIHP_PLL_SEL_SHIFT, |
| 113 | ACLK_PERIHP_DIV_CON_SHIFT = 0, |
| 114 | ACLK_PERIHP_DIV_CON_MASK = 0x1f, |
| 115 | |
| 116 | /* CLKSEL_CON29 */ |
| 117 | PCLK_PERIHP_DIV_CON_SHIFT = 4, |
| 118 | PCLK_PERIHP_DIV_CON_MASK = 0x7 << PCLK_PERIHP_DIV_CON_SHIFT, |
| 119 | HCLK_PERIHP_DIV_CON_SHIFT = 0, |
| 120 | HCLK_PERIHP_DIV_CON_MASK = 3 << HCLK_PERIHP_DIV_CON_SHIFT, |
| 121 | |
| 122 | /* CLKSEL_CON22 */ |
| 123 | CLK_TSADC_DIV_CON_SHIFT = 0, |
| 124 | CLK_TSADC_DIV_CON_MASK = 0x3ff, |
| 125 | |
| 126 | /* CLKSEL_CON23 */ |
| 127 | CLK_SARADC_DIV_CON_SHIFT = 0, |
David Wu | a942223 | 2017-09-20 14:35:44 +0800 | [diff] [blame] | 128 | CLK_SARADC_DIV_CON_MASK = GENMASK(9, 0), |
| 129 | CLK_SARADC_DIV_CON_WIDTH = 10, |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 130 | |
| 131 | /* CLKSEL_CON24 */ |
| 132 | CLK_PWM_PLL_SEL_CPLL = 0, |
| 133 | CLK_PWM_PLL_SEL_GPLL, |
| 134 | CLK_PWM_PLL_SEL_SHIFT = 15, |
| 135 | CLK_PWM_PLL_SEL_MASK = 1 << CLK_PWM_PLL_SEL_SHIFT, |
| 136 | CLK_PWM_DIV_CON_SHIFT = 8, |
| 137 | CLK_PWM_DIV_CON_MASK = 0x7f << CLK_PWM_DIV_CON_SHIFT, |
| 138 | |
| 139 | CLK_SPI_PLL_SEL_CPLL = 0, |
| 140 | CLK_SPI_PLL_SEL_GPLL, |
| 141 | CLK_SPI_PLL_SEL_SHIFT = 7, |
| 142 | CLK_SPI_PLL_SEL_MASK = 1 << CLK_SPI_PLL_SEL_SHIFT, |
| 143 | CLK_SPI_DIV_CON_SHIFT = 0, |
| 144 | CLK_SPI_DIV_CON_MASK = 0x7f << CLK_SPI_DIV_CON_SHIFT, |
| 145 | |
| 146 | /* CLKSEL_CON30 */ |
| 147 | CLK_SDMMC_PLL_SEL_CPLL = 0, |
| 148 | CLK_SDMMC_PLL_SEL_GPLL, |
| 149 | CLK_SDMMC_PLL_SEL_24M, |
| 150 | CLK_SDMMC_PLL_SEL_USBPHY, |
| 151 | CLK_SDMMC_PLL_SHIFT = 8, |
| 152 | CLK_SDMMC_PLL_MASK = 0x3 << CLK_SDMMC_PLL_SHIFT, |
| 153 | CLK_SDMMC_DIV_CON_SHIFT = 0, |
| 154 | CLK_SDMMC_DIV_CON_MASK = 0xff << CLK_SDMMC_DIV_CON_SHIFT, |
| 155 | |
| 156 | /* CLKSEL_CON32 */ |
| 157 | CLK_EMMC_PLL_SEL_CPLL = 0, |
| 158 | CLK_EMMC_PLL_SEL_GPLL, |
| 159 | CLK_EMMC_PLL_SEL_24M, |
| 160 | CLK_EMMC_PLL_SEL_USBPHY, |
| 161 | CLK_EMMC_PLL_SHIFT = 8, |
| 162 | CLK_EMMC_PLL_MASK = 0x3 << CLK_EMMC_PLL_SHIFT, |
| 163 | CLK_EMMC_DIV_CON_SHIFT = 0, |
| 164 | CLK_EMMC_DIV_CON_MASK = 0xff << CLK_EMMC_DIV_CON_SHIFT, |
| 165 | |
| 166 | /* CLKSEL_CON34 */ |
| 167 | CLK_I2C_PLL_SEL_CPLL = 0, |
| 168 | CLK_I2C_PLL_SEL_GPLL, |
| 169 | CLK_I2C_DIV_CON_MASK = 0x7f, |
| 170 | CLK_I2C_PLL_SEL_MASK = 1, |
| 171 | CLK_I2C1_PLL_SEL_SHIFT = 15, |
| 172 | CLK_I2C1_DIV_CON_SHIFT = 8, |
| 173 | CLK_I2C0_PLL_SEL_SHIFT = 7, |
| 174 | CLK_I2C0_DIV_CON_SHIFT = 0, |
| 175 | |
| 176 | /* CLKSEL_CON35 */ |
| 177 | CLK_I2C3_PLL_SEL_SHIFT = 15, |
| 178 | CLK_I2C3_DIV_CON_SHIFT = 8, |
| 179 | CLK_I2C2_PLL_SEL_SHIFT = 7, |
| 180 | CLK_I2C2_DIV_CON_SHIFT = 0, |
Jagan Teki | 350ab5d | 2024-01-17 13:21:47 +0530 | [diff] [blame] | 181 | |
| 182 | /* CLKSEL_CON40 */ |
| 183 | CLK_HDMIPHY_DIV_CON_SHIFT = 3, |
| 184 | CLK_HDMIPHY_DIV_CON_MASK = 0x7 << CLK_HDMIPHY_DIV_CON_SHIFT, |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | #define VCO_MAX_KHZ (3200 * (MHz / KHz)) |
| 188 | #define VCO_MIN_KHZ (800 * (MHz / KHz)) |
| 189 | #define OUTPUT_MAX_KHZ (3200 * (MHz / KHz)) |
| 190 | #define OUTPUT_MIN_KHZ (16 * (MHz / KHz)) |
| 191 | |
| 192 | /* |
| 193 | * the div restructions of pll in integer mode, these are defined in |
| 194 | * * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0 |
| 195 | */ |
| 196 | #define PLL_DIV_MIN 16 |
| 197 | #define PLL_DIV_MAX 3200 |
| 198 | |
| 199 | /* |
| 200 | * How to calculate the PLL(from TRM V0.3 Part 1 Page 63): |
| 201 | * Formulas also embedded within the Fractional PLL Verilog model: |
| 202 | * If DSMPD = 1 (DSM is disabled, "integer mode") |
| 203 | * FOUTVCO = FREF / REFDIV * FBDIV |
| 204 | * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2 |
| 205 | * Where: |
| 206 | * FOUTVCO = Fractional PLL non-divided output frequency |
| 207 | * FOUTPOSTDIV = Fractional PLL divided output frequency |
| 208 | * (output of second post divider) |
| 209 | * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input) |
| 210 | * REFDIV = Fractional PLL input reference clock divider |
| 211 | * FBDIV = Integer value programmed into feedback divide |
| 212 | * |
| 213 | */ |
| 214 | static void rkclk_set_pll(struct rk3328_cru *cru, enum rk_clk_id clk_id, |
| 215 | const struct pll_div *div) |
| 216 | { |
| 217 | u32 *pll_con; |
| 218 | u32 mode_shift, mode_mask; |
| 219 | |
| 220 | pll_con = NULL; |
| 221 | mode_shift = 0; |
| 222 | switch (clk_id) { |
| 223 | case CLK_ARM: |
| 224 | pll_con = cru->apll_con; |
| 225 | mode_shift = APLL_MODE_SHIFT; |
| 226 | break; |
| 227 | case CLK_DDR: |
| 228 | pll_con = cru->dpll_con; |
| 229 | mode_shift = DPLL_MODE_SHIFT; |
| 230 | break; |
| 231 | case CLK_CODEC: |
| 232 | pll_con = cru->cpll_con; |
| 233 | mode_shift = CPLL_MODE_SHIFT; |
| 234 | break; |
| 235 | case CLK_GENERAL: |
| 236 | pll_con = cru->gpll_con; |
| 237 | mode_shift = GPLL_MODE_SHIFT; |
| 238 | break; |
| 239 | case CLK_NEW: |
| 240 | pll_con = cru->npll_con; |
| 241 | mode_shift = NPLL_MODE_SHIFT; |
| 242 | break; |
| 243 | default: |
| 244 | break; |
| 245 | } |
| 246 | mode_mask = 1 << mode_shift; |
| 247 | |
| 248 | /* All 8 PLLs have same VCO and output frequency range restrictions. */ |
| 249 | u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv; |
| 250 | u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2; |
| 251 | |
| 252 | debug("PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, \ |
| 253 | postdiv2=%d, vco=%u khz, output=%u khz\n", |
| 254 | pll_con, div->fbdiv, div->refdiv, div->postdiv1, |
| 255 | div->postdiv2, vco_khz, output_khz); |
| 256 | assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ && |
| 257 | output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ && |
| 258 | div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX); |
| 259 | |
| 260 | /* |
| 261 | * When power on or changing PLL setting, |
| 262 | * we must force PLL into slow mode to ensure output stable clock. |
| 263 | */ |
| 264 | rk_clrsetreg(&cru->mode_con, mode_mask, PLL_MODE_SLOW << mode_shift); |
| 265 | |
| 266 | /* use integer mode */ |
| 267 | rk_clrsetreg(&pll_con[1], PLL_DSMPD_MASK, |
| 268 | PLL_INTEGER_MODE << PLL_DSMPD_SHIFT); |
| 269 | |
| 270 | rk_clrsetreg(&pll_con[0], |
| 271 | PLL_FBDIV_MASK | PLL_POSTDIV1_MASK, |
| 272 | (div->fbdiv << PLL_FBDIV_SHIFT) | |
| 273 | (div->postdiv1 << PLL_POSTDIV1_SHIFT)); |
| 274 | rk_clrsetreg(&pll_con[1], |
| 275 | PLL_POSTDIV2_MASK | PLL_REFDIV_MASK, |
| 276 | (div->postdiv2 << PLL_POSTDIV2_SHIFT) | |
| 277 | (div->refdiv << PLL_REFDIV_SHIFT)); |
| 278 | |
| 279 | /* waiting for pll lock */ |
| 280 | while (!(readl(&pll_con[1]) & (1 << PLL_LOCK_STATUS_SHIFT))) |
| 281 | udelay(1); |
| 282 | |
| 283 | /* pll enter normal mode */ |
| 284 | rk_clrsetreg(&cru->mode_con, mode_mask, PLL_MODE_NORM << mode_shift); |
| 285 | } |
| 286 | |
| 287 | static void rkclk_init(struct rk3328_cru *cru) |
| 288 | { |
| 289 | u32 aclk_div; |
| 290 | u32 hclk_div; |
| 291 | u32 pclk_div; |
| 292 | |
Simon South | 93c4485 | 2019-10-10 15:28:36 -0400 | [diff] [blame] | 293 | rk3328_configure_cpu(cru, APLL_600_MHZ); |
| 294 | |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 295 | /* configure gpll cpll */ |
| 296 | rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg); |
| 297 | rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg); |
| 298 | |
| 299 | /* configure perihp aclk, hclk, pclk */ |
| 300 | aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1; |
| 301 | hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1; |
| 302 | pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1; |
| 303 | |
| 304 | rk_clrsetreg(&cru->clksel_con[28], |
| 305 | ACLK_PERIHP_PLL_SEL_MASK | ACLK_PERIHP_DIV_CON_MASK, |
| 306 | ACLK_PERIHP_PLL_SEL_GPLL << ACLK_PERIHP_PLL_SEL_SHIFT | |
| 307 | aclk_div << ACLK_PERIHP_DIV_CON_SHIFT); |
| 308 | rk_clrsetreg(&cru->clksel_con[29], |
| 309 | PCLK_PERIHP_DIV_CON_MASK | HCLK_PERIHP_DIV_CON_MASK, |
| 310 | pclk_div << PCLK_PERIHP_DIV_CON_SHIFT | |
| 311 | hclk_div << HCLK_PERIHP_DIV_CON_SHIFT); |
| 312 | } |
| 313 | |
| 314 | void rk3328_configure_cpu(struct rk3328_cru *cru, |
| 315 | enum apll_frequencies apll_freq) |
| 316 | { |
| 317 | u32 clk_core_div; |
| 318 | u32 aclkm_div; |
| 319 | u32 pclk_dbg_div; |
| 320 | |
| 321 | rkclk_set_pll(cru, CLK_ARM, apll_cfgs[apll_freq]); |
| 322 | |
| 323 | clk_core_div = APLL_HZ / CLK_CORE_HZ - 1; |
| 324 | aclkm_div = APLL_HZ / ACLKM_CORE_HZ / (clk_core_div + 1) - 1; |
| 325 | pclk_dbg_div = APLL_HZ / PCLK_DBG_HZ / (clk_core_div + 1) - 1; |
| 326 | |
| 327 | rk_clrsetreg(&cru->clksel_con[0], |
| 328 | CLK_CORE_PLL_SEL_MASK | CLK_CORE_DIV_MASK, |
| 329 | CLK_CORE_PLL_SEL_APLL << CLK_CORE_PLL_SEL_SHIFT | |
| 330 | clk_core_div << CLK_CORE_DIV_SHIFT); |
| 331 | |
| 332 | rk_clrsetreg(&cru->clksel_con[1], |
| 333 | PCLK_DBG_DIV_MASK | ACLKM_CORE_DIV_MASK, |
| 334 | pclk_dbg_div << PCLK_DBG_DIV_SHIFT | |
| 335 | aclkm_div << ACLKM_CORE_DIV_SHIFT); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | static ulong rk3328_i2c_get_clk(struct rk3328_cru *cru, ulong clk_id) |
| 340 | { |
| 341 | u32 div, con; |
| 342 | |
| 343 | switch (clk_id) { |
| 344 | case SCLK_I2C0: |
| 345 | con = readl(&cru->clksel_con[34]); |
| 346 | div = con >> CLK_I2C0_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK; |
| 347 | break; |
| 348 | case SCLK_I2C1: |
| 349 | con = readl(&cru->clksel_con[34]); |
| 350 | div = con >> CLK_I2C1_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK; |
| 351 | break; |
| 352 | case SCLK_I2C2: |
| 353 | con = readl(&cru->clksel_con[35]); |
| 354 | div = con >> CLK_I2C2_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK; |
| 355 | break; |
| 356 | case SCLK_I2C3: |
| 357 | con = readl(&cru->clksel_con[35]); |
| 358 | div = con >> CLK_I2C3_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK; |
| 359 | break; |
| 360 | default: |
| 361 | printf("do not support this i2c bus\n"); |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | |
| 365 | return DIV_TO_RATE(GPLL_HZ, div); |
| 366 | } |
| 367 | |
| 368 | static ulong rk3328_i2c_set_clk(struct rk3328_cru *cru, ulong clk_id, uint hz) |
| 369 | { |
| 370 | int src_clk_div; |
| 371 | |
| 372 | src_clk_div = GPLL_HZ / hz; |
| 373 | assert(src_clk_div - 1 < 127); |
| 374 | |
| 375 | switch (clk_id) { |
| 376 | case SCLK_I2C0: |
| 377 | rk_clrsetreg(&cru->clksel_con[34], |
| 378 | CLK_I2C_DIV_CON_MASK << CLK_I2C0_DIV_CON_SHIFT | |
| 379 | CLK_I2C_PLL_SEL_MASK << CLK_I2C0_PLL_SEL_SHIFT, |
| 380 | (src_clk_div - 1) << CLK_I2C0_DIV_CON_SHIFT | |
| 381 | CLK_I2C_PLL_SEL_GPLL << CLK_I2C0_PLL_SEL_SHIFT); |
| 382 | break; |
| 383 | case SCLK_I2C1: |
| 384 | rk_clrsetreg(&cru->clksel_con[34], |
| 385 | CLK_I2C_DIV_CON_MASK << CLK_I2C1_DIV_CON_SHIFT | |
| 386 | CLK_I2C_PLL_SEL_MASK << CLK_I2C1_PLL_SEL_SHIFT, |
| 387 | (src_clk_div - 1) << CLK_I2C1_DIV_CON_SHIFT | |
| 388 | CLK_I2C_PLL_SEL_GPLL << CLK_I2C1_PLL_SEL_SHIFT); |
| 389 | break; |
| 390 | case SCLK_I2C2: |
| 391 | rk_clrsetreg(&cru->clksel_con[35], |
| 392 | CLK_I2C_DIV_CON_MASK << CLK_I2C2_DIV_CON_SHIFT | |
| 393 | CLK_I2C_PLL_SEL_MASK << CLK_I2C2_PLL_SEL_SHIFT, |
| 394 | (src_clk_div - 1) << CLK_I2C2_DIV_CON_SHIFT | |
| 395 | CLK_I2C_PLL_SEL_GPLL << CLK_I2C2_PLL_SEL_SHIFT); |
| 396 | break; |
| 397 | case SCLK_I2C3: |
| 398 | rk_clrsetreg(&cru->clksel_con[35], |
| 399 | CLK_I2C_DIV_CON_MASK << CLK_I2C3_DIV_CON_SHIFT | |
| 400 | CLK_I2C_PLL_SEL_MASK << CLK_I2C3_PLL_SEL_SHIFT, |
| 401 | (src_clk_div - 1) << CLK_I2C3_DIV_CON_SHIFT | |
| 402 | CLK_I2C_PLL_SEL_GPLL << CLK_I2C3_PLL_SEL_SHIFT); |
| 403 | break; |
| 404 | default: |
| 405 | printf("do not support this i2c bus\n"); |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | return DIV_TO_RATE(GPLL_HZ, src_clk_div); |
| 410 | } |
| 411 | |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 412 | static ulong rk3328_gmac2io_set_clk(struct rk3328_cru *cru, ulong rate) |
| 413 | { |
| 414 | struct rk3328_grf_regs *grf; |
| 415 | ulong ret; |
| 416 | |
| 417 | grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 418 | |
| 419 | /* |
| 420 | * The RGMII CLK can be derived either from an external "clkin" |
| 421 | * or can be generated from internally by a divider from SCLK_MAC. |
| 422 | */ |
| 423 | if (readl(&grf->mac_con[1]) & BIT(10) && |
| 424 | readl(&grf->soc_con[4]) & BIT(14)) { |
| 425 | /* An external clock will always generate the right rate... */ |
| 426 | ret = rate; |
| 427 | } else { |
| 428 | u32 con = readl(&cru->clksel_con[27]); |
| 429 | ulong pll_rate; |
| 430 | u8 div; |
| 431 | |
| 432 | if ((con >> GMAC2IO_PLL_SEL_SHIFT) & GMAC2IO_PLL_SEL_GPLL) |
| 433 | pll_rate = GPLL_HZ; |
| 434 | else |
| 435 | pll_rate = CPLL_HZ; |
| 436 | |
| 437 | div = DIV_ROUND_UP(pll_rate, rate) - 1; |
| 438 | if (div <= 0x1f) |
| 439 | rk_clrsetreg(&cru->clksel_con[27], GMAC2IO_CLK_DIV_MASK, |
| 440 | div << GMAC2IO_CLK_DIV_SHIFT); |
| 441 | else |
| 442 | debug("Unsupported div for gmac:%d\n", div); |
| 443 | |
| 444 | return DIV_TO_RATE(pll_rate, div); |
| 445 | } |
| 446 | |
| 447 | return ret; |
| 448 | } |
| 449 | |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 450 | static ulong rk3328_mmc_get_clk(struct rk3328_cru *cru, uint clk_id) |
| 451 | { |
| 452 | u32 div, con, con_id; |
| 453 | |
| 454 | switch (clk_id) { |
| 455 | case HCLK_SDMMC: |
Xu Ziyuan | 5a02763 | 2017-04-16 17:44:46 +0800 | [diff] [blame] | 456 | case SCLK_SDMMC: |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 457 | con_id = 30; |
| 458 | break; |
| 459 | case HCLK_EMMC: |
Xu Ziyuan | 5a02763 | 2017-04-16 17:44:46 +0800 | [diff] [blame] | 460 | case SCLK_EMMC: |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 461 | con_id = 32; |
| 462 | break; |
| 463 | default: |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | con = readl(&cru->clksel_con[con_id]); |
| 467 | div = (con & CLK_EMMC_DIV_CON_MASK) >> CLK_EMMC_DIV_CON_SHIFT; |
| 468 | |
| 469 | if ((con & CLK_EMMC_PLL_MASK) >> CLK_EMMC_PLL_SHIFT |
| 470 | == CLK_EMMC_PLL_SEL_24M) |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 471 | return DIV_TO_RATE(OSC_HZ, div) / 2; |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 472 | else |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 473 | return DIV_TO_RATE(GPLL_HZ, div) / 2; |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static ulong rk3328_mmc_set_clk(struct rk3328_cru *cru, |
| 477 | ulong clk_id, ulong set_rate) |
| 478 | { |
| 479 | int src_clk_div; |
| 480 | u32 con_id; |
| 481 | |
| 482 | switch (clk_id) { |
| 483 | case HCLK_SDMMC: |
Xu Ziyuan | 5a02763 | 2017-04-16 17:44:46 +0800 | [diff] [blame] | 484 | case SCLK_SDMMC: |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 485 | con_id = 30; |
| 486 | break; |
| 487 | case HCLK_EMMC: |
Xu Ziyuan | 5a02763 | 2017-04-16 17:44:46 +0800 | [diff] [blame] | 488 | case SCLK_EMMC: |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 489 | con_id = 32; |
| 490 | break; |
| 491 | default: |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | /* Select clk_sdmmc/emmc source from GPLL by default */ |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 495 | /* mmc clock defaulg div 2 internal, need provide double in cru */ |
| 496 | src_clk_div = DIV_ROUND_UP(GPLL_HZ / 2, set_rate); |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 497 | |
| 498 | if (src_clk_div > 127) { |
| 499 | /* use 24MHz source for 400KHz clock */ |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 500 | src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate); |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 501 | rk_clrsetreg(&cru->clksel_con[con_id], |
| 502 | CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK, |
| 503 | CLK_EMMC_PLL_SEL_24M << CLK_EMMC_PLL_SHIFT | |
| 504 | (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT); |
| 505 | } else { |
| 506 | rk_clrsetreg(&cru->clksel_con[con_id], |
| 507 | CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK, |
| 508 | CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT | |
| 509 | (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT); |
| 510 | } |
| 511 | |
| 512 | return rk3328_mmc_get_clk(cru, clk_id); |
| 513 | } |
| 514 | |
| 515 | static ulong rk3328_pwm_get_clk(struct rk3328_cru *cru) |
| 516 | { |
| 517 | u32 div, con; |
| 518 | |
| 519 | con = readl(&cru->clksel_con[24]); |
| 520 | div = (con & CLK_PWM_DIV_CON_MASK) >> CLK_PWM_DIV_CON_SHIFT; |
| 521 | |
| 522 | return DIV_TO_RATE(GPLL_HZ, div); |
| 523 | } |
| 524 | |
| 525 | static ulong rk3328_pwm_set_clk(struct rk3328_cru *cru, uint hz) |
| 526 | { |
| 527 | u32 div = GPLL_HZ / hz; |
| 528 | |
| 529 | rk_clrsetreg(&cru->clksel_con[24], |
| 530 | CLK_PWM_PLL_SEL_MASK | CLK_PWM_DIV_CON_MASK, |
| 531 | CLK_PWM_PLL_SEL_GPLL << CLK_PWM_PLL_SEL_SHIFT | |
| 532 | (div - 1) << CLK_PWM_DIV_CON_SHIFT); |
| 533 | |
| 534 | return DIV_TO_RATE(GPLL_HZ, div); |
| 535 | } |
| 536 | |
David Wu | a942223 | 2017-09-20 14:35:44 +0800 | [diff] [blame] | 537 | static ulong rk3328_saradc_get_clk(struct rk3328_cru *cru) |
| 538 | { |
| 539 | u32 div, val; |
| 540 | |
| 541 | val = readl(&cru->clksel_con[23]); |
| 542 | div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT, |
| 543 | CLK_SARADC_DIV_CON_WIDTH); |
| 544 | |
| 545 | return DIV_TO_RATE(OSC_HZ, div); |
| 546 | } |
| 547 | |
| 548 | static ulong rk3328_saradc_set_clk(struct rk3328_cru *cru, uint hz) |
| 549 | { |
| 550 | int src_clk_div; |
| 551 | |
| 552 | src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1; |
| 553 | assert(src_clk_div < 128); |
| 554 | |
| 555 | rk_clrsetreg(&cru->clksel_con[23], |
| 556 | CLK_SARADC_DIV_CON_MASK, |
| 557 | src_clk_div << CLK_SARADC_DIV_CON_SHIFT); |
| 558 | |
| 559 | return rk3328_saradc_get_clk(cru); |
| 560 | } |
| 561 | |
Johannes Krottmayer | 0f0237d | 2020-07-08 23:57:38 +0200 | [diff] [blame] | 562 | static ulong rk3328_spi_get_clk(struct rk3328_cru *cru) |
| 563 | { |
| 564 | u32 div, val; |
| 565 | |
| 566 | val = readl(&cru->clksel_con[24]); |
| 567 | div = (val & CLK_SPI_DIV_CON_MASK) >> CLK_SPI_DIV_CON_SHIFT; |
| 568 | |
| 569 | return DIV_TO_RATE(OSC_HZ, div); |
| 570 | } |
| 571 | |
| 572 | static ulong rk3328_spi_set_clk(struct rk3328_cru *cru, uint hz) |
| 573 | { |
| 574 | u32 src_clk_div; |
| 575 | |
| 576 | src_clk_div = GPLL_HZ / hz; |
| 577 | assert(src_clk_div < 128); |
| 578 | |
| 579 | rk_clrsetreg(&cru->clksel_con[24], |
| 580 | CLK_PWM_PLL_SEL_MASK | CLK_PWM_DIV_CON_MASK, |
| 581 | CLK_PWM_PLL_SEL_GPLL << CLK_PWM_PLL_SEL_SHIFT | |
| 582 | (src_clk_div - 1) << CLK_PWM_DIV_CON_SHIFT); |
| 583 | |
| 584 | return rk3328_spi_get_clk(cru); |
| 585 | } |
| 586 | |
Jagan Teki | 175ee82 | 2024-01-17 13:21:46 +0530 | [diff] [blame] | 587 | #ifndef CONFIG_SPL_BUILD |
| 588 | static ulong rk3328_vop_get_clk(struct rk3328_clk_priv *priv, ulong clk_id) |
| 589 | { |
| 590 | struct rk3328_cru *cru = priv->cru; |
| 591 | u32 div, con, parent; |
| 592 | |
| 593 | switch (clk_id) { |
| 594 | case ACLK_VOP_PRE: |
| 595 | con = readl(&cru->clksel_con[39]); |
| 596 | div = (con & ACLK_VOP_DIV_CON_MASK) >> ACLK_VOP_DIV_CON_SHIFT; |
| 597 | parent = GPLL_HZ; |
| 598 | break; |
| 599 | case ACLK_VIO_PRE: |
| 600 | con = readl(&cru->clksel_con[37]); |
| 601 | div = (con & ACLK_VIO_DIV_CON_MASK) >> ACLK_VIO_DIV_CON_SHIFT; |
| 602 | parent = GPLL_HZ; |
| 603 | break; |
| 604 | case DCLK_LCDC: |
| 605 | con = readl(&cru->clksel_con[40]); |
| 606 | div = (con & DCLK_LCDC_DIV_CON_MASK) >> DCLK_LCDC_DIV_CON_SHIFT; |
| 607 | parent = GPLL_HZ; |
| 608 | break; |
| 609 | default: |
| 610 | printf("%s: Unsupported vop get clk#%ld\n", __func__, clk_id); |
| 611 | return -ENOENT; |
| 612 | } |
| 613 | |
| 614 | return DIV_TO_RATE(parent, div); |
| 615 | } |
| 616 | |
| 617 | static ulong rk3328_vop_set_clk(struct rk3328_clk_priv *priv, |
| 618 | ulong clk_id, uint hz) |
| 619 | { |
| 620 | struct rk3328_cru *cru = priv->cru; |
| 621 | int src_clk_div; |
| 622 | u32 con, parent; |
| 623 | |
| 624 | src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz); |
| 625 | assert(src_clk_div - 1 < 31); |
| 626 | |
| 627 | switch (clk_id) { |
| 628 | case ACLK_VOP_PRE: |
| 629 | rk_clrsetreg(&cru->clksel_con[39], |
| 630 | ACLK_VOP_PLL_SEL_MASK | ACLK_VOP_DIV_CON_MASK, |
| 631 | ACLK_VOP_PLL_SEL_CPLL << ACLK_VOP_PLL_SEL_SHIFT | |
| 632 | (src_clk_div - 1) << ACLK_VOP_DIV_CON_SHIFT); |
| 633 | break; |
| 634 | case ACLK_VIO_PRE: |
| 635 | rk_clrsetreg(&cru->clksel_con[37], |
| 636 | ACLK_VIO_PLL_SEL_MASK | ACLK_VIO_DIV_CON_MASK, |
| 637 | ACLK_VIO_PLL_SEL_CPLL << ACLK_VIO_PLL_SEL_SHIFT | |
| 638 | (src_clk_div - 1) << ACLK_VIO_DIV_CON_SHIFT); |
| 639 | break; |
| 640 | case DCLK_LCDC: |
| 641 | con = readl(&cru->clksel_con[40]); |
| 642 | con = (con & DCLK_LCDC_SEL_MASK) >> DCLK_LCDC_SEL_SHIFT; |
| 643 | if (con) { |
| 644 | parent = readl(&cru->clksel_con[40]); |
| 645 | parent = (parent & DCLK_LCDC_PLL_SEL_MASK) >> |
| 646 | DCLK_LCDC_PLL_SEL_SHIFT; |
| 647 | if (parent) |
| 648 | src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz); |
| 649 | else |
| 650 | src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz); |
| 651 | |
| 652 | rk_clrsetreg(&cru->clksel_con[40], |
| 653 | DCLK_LCDC_DIV_CON_MASK, |
| 654 | (src_clk_div - 1) << |
| 655 | DCLK_LCDC_DIV_CON_SHIFT); |
| 656 | } |
| 657 | break; |
| 658 | default: |
| 659 | printf("%s: Unable to set vop clk#%ld\n", __func__, clk_id); |
| 660 | return -EINVAL; |
| 661 | } |
| 662 | |
| 663 | return rk3328_vop_get_clk(priv, clk_id); |
| 664 | } |
| 665 | #endif |
| 666 | |
Jagan Teki | 350ab5d | 2024-01-17 13:21:47 +0530 | [diff] [blame] | 667 | static ulong rk3328_hdmiphy_get_clk(struct rk3328_cru *cru) |
| 668 | { |
| 669 | u32 div, con; |
| 670 | |
| 671 | con = readl(&cru->clksel_con[40]); |
| 672 | div = (con & CLK_HDMIPHY_DIV_CON_MASK) >> CLK_HDMIPHY_DIV_CON_SHIFT; |
| 673 | |
| 674 | return DIV_TO_RATE(GPLL_HZ, div); |
| 675 | } |
| 676 | |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 677 | static ulong rk3328_clk_get_rate(struct clk *clk) |
| 678 | { |
| 679 | struct rk3328_clk_priv *priv = dev_get_priv(clk->dev); |
| 680 | ulong rate = 0; |
| 681 | |
| 682 | switch (clk->id) { |
| 683 | case 0 ... 29: |
| 684 | return 0; |
| 685 | case HCLK_SDMMC: |
| 686 | case HCLK_EMMC: |
Xu Ziyuan | 5a02763 | 2017-04-16 17:44:46 +0800 | [diff] [blame] | 687 | case SCLK_SDMMC: |
| 688 | case SCLK_EMMC: |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 689 | rate = rk3328_mmc_get_clk(priv->cru, clk->id); |
| 690 | break; |
| 691 | case SCLK_I2C0: |
| 692 | case SCLK_I2C1: |
| 693 | case SCLK_I2C2: |
| 694 | case SCLK_I2C3: |
| 695 | rate = rk3328_i2c_get_clk(priv->cru, clk->id); |
| 696 | break; |
| 697 | case SCLK_PWM: |
| 698 | rate = rk3328_pwm_get_clk(priv->cru); |
| 699 | break; |
David Wu | a942223 | 2017-09-20 14:35:44 +0800 | [diff] [blame] | 700 | case SCLK_SARADC: |
| 701 | rate = rk3328_saradc_get_clk(priv->cru); |
| 702 | break; |
Johannes Krottmayer | 0f0237d | 2020-07-08 23:57:38 +0200 | [diff] [blame] | 703 | case SCLK_SPI: |
| 704 | rate = rk3328_spi_get_clk(priv->cru); |
| 705 | break; |
Jagan Teki | 350ab5d | 2024-01-17 13:21:47 +0530 | [diff] [blame] | 706 | case PCLK_HDMIPHY: |
| 707 | rate = rk3328_hdmiphy_get_clk(priv->cru); |
| 708 | break; |
Jonas Karlman | be20132 | 2024-05-01 19:23:50 +0000 | [diff] [blame] | 709 | case SCLK_USB3OTG_REF: |
| 710 | rate = OSC_HZ; |
| 711 | break; |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 712 | default: |
| 713 | return -ENOENT; |
| 714 | } |
| 715 | |
| 716 | return rate; |
| 717 | } |
| 718 | |
| 719 | static ulong rk3328_clk_set_rate(struct clk *clk, ulong rate) |
| 720 | { |
| 721 | struct rk3328_clk_priv *priv = dev_get_priv(clk->dev); |
| 722 | ulong ret = 0; |
| 723 | |
| 724 | switch (clk->id) { |
| 725 | case 0 ... 29: |
| 726 | return 0; |
| 727 | case HCLK_SDMMC: |
| 728 | case HCLK_EMMC: |
Xu Ziyuan | 5a02763 | 2017-04-16 17:44:46 +0800 | [diff] [blame] | 729 | case SCLK_SDMMC: |
| 730 | case SCLK_EMMC: |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 731 | ret = rk3328_mmc_set_clk(priv->cru, clk->id, rate); |
| 732 | break; |
| 733 | case SCLK_I2C0: |
| 734 | case SCLK_I2C1: |
| 735 | case SCLK_I2C2: |
| 736 | case SCLK_I2C3: |
| 737 | ret = rk3328_i2c_set_clk(priv->cru, clk->id, rate); |
| 738 | break; |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 739 | case SCLK_MAC2IO: |
| 740 | ret = rk3328_gmac2io_set_clk(priv->cru, rate); |
| 741 | break; |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 742 | case SCLK_PWM: |
| 743 | ret = rk3328_pwm_set_clk(priv->cru, rate); |
| 744 | break; |
David Wu | a942223 | 2017-09-20 14:35:44 +0800 | [diff] [blame] | 745 | case SCLK_SARADC: |
| 746 | ret = rk3328_saradc_set_clk(priv->cru, rate); |
| 747 | break; |
Johannes Krottmayer | 0f0237d | 2020-07-08 23:57:38 +0200 | [diff] [blame] | 748 | case SCLK_SPI: |
| 749 | ret = rk3328_spi_set_clk(priv->cru, rate); |
| 750 | break; |
Jagan Teki | 175ee82 | 2024-01-17 13:21:46 +0530 | [diff] [blame] | 751 | #ifndef CONFIG_SPL_BUILD |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 752 | case DCLK_LCDC: |
Jagan Teki | 175ee82 | 2024-01-17 13:21:46 +0530 | [diff] [blame] | 753 | case ACLK_VOP_PRE: |
| 754 | case ACLK_VIO_PRE: |
| 755 | rate = rk3328_vop_set_clk(priv, clk->id, rate); |
| 756 | break; |
| 757 | #endif |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 758 | case SCLK_PDM: |
| 759 | case SCLK_RTC32K: |
| 760 | case SCLK_UART0: |
| 761 | case SCLK_UART1: |
| 762 | case SCLK_UART2: |
| 763 | case SCLK_SDIO: |
| 764 | case SCLK_TSP: |
| 765 | case SCLK_WIFI: |
| 766 | case ACLK_BUS_PRE: |
| 767 | case HCLK_BUS_PRE: |
| 768 | case PCLK_BUS_PRE: |
| 769 | case ACLK_PERI_PRE: |
| 770 | case HCLK_PERI: |
| 771 | case PCLK_PERI: |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 772 | case HCLK_VIO_PRE: |
| 773 | case ACLK_RGA_PRE: |
| 774 | case SCLK_RGA: |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 775 | case ACLK_RKVDEC_PRE: |
| 776 | case ACLK_RKVENC: |
| 777 | case ACLK_VPU_PRE: |
| 778 | case SCLK_VDEC_CABAC: |
| 779 | case SCLK_VDEC_CORE: |
| 780 | case SCLK_VENC_CORE: |
| 781 | case SCLK_VENC_DSP: |
| 782 | case SCLK_EFUSE: |
| 783 | case PCLK_DDR: |
| 784 | case ACLK_GMAC: |
| 785 | case PCLK_GMAC: |
Jonas Karlman | be20132 | 2024-05-01 19:23:50 +0000 | [diff] [blame] | 786 | case SCLK_USB3OTG_REF: |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 787 | case SCLK_USB3OTG_SUSPEND: |
Jagan Teki | c46620f | 2023-06-06 22:39:17 +0530 | [diff] [blame] | 788 | case USB480M: |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 789 | return 0; |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 790 | default: |
| 791 | return -ENOENT; |
| 792 | } |
| 793 | |
| 794 | return ret; |
| 795 | } |
| 796 | |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 797 | static int rk3328_gmac2io_set_parent(struct clk *clk, struct clk *parent) |
| 798 | { |
| 799 | struct rk3328_grf_regs *grf; |
| 800 | const char *clock_output_name; |
| 801 | int ret; |
| 802 | |
| 803 | grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 804 | |
| 805 | /* |
| 806 | * If the requested parent is in the same clock-controller and the id |
| 807 | * is SCLK_MAC2IO_SRC ("clk_mac2io_src"), switch to the internal clock. |
| 808 | */ |
| 809 | if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC2IO_SRC)) { |
| 810 | debug("%s: switching RGMII to SCLK_MAC2IO_SRC\n", __func__); |
| 811 | rk_clrreg(&grf->mac_con[1], BIT(10)); |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Otherwise, we need to check the clock-output-names of the |
| 817 | * requested parent to see if the requested id is "gmac_clkin". |
| 818 | */ |
| 819 | ret = dev_read_string_index(parent->dev, "clock-output-names", |
| 820 | parent->id, &clock_output_name); |
| 821 | if (ret < 0) |
| 822 | return -ENODATA; |
| 823 | |
| 824 | /* If this is "gmac_clkin", switch to the external clock input */ |
| 825 | if (!strcmp(clock_output_name, "gmac_clkin")) { |
| 826 | debug("%s: switching RGMII to CLKIN\n", __func__); |
| 827 | rk_setreg(&grf->mac_con[1], BIT(10)); |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | |
| 834 | static int rk3328_gmac2io_ext_set_parent(struct clk *clk, struct clk *parent) |
| 835 | { |
| 836 | struct rk3328_grf_regs *grf; |
| 837 | const char *clock_output_name; |
| 838 | int ret; |
| 839 | |
| 840 | grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 841 | |
| 842 | /* |
| 843 | * If the requested parent is in the same clock-controller and the id |
| 844 | * is SCLK_MAC2IO ("clk_mac2io"), switch to the internal clock. |
| 845 | */ |
| 846 | if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC2IO)) { |
| 847 | debug("%s: switching RGMII to SCLK_MAC2IO\n", __func__); |
| 848 | rk_clrreg(&grf->soc_con[4], BIT(14)); |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Otherwise, we need to check the clock-output-names of the |
| 854 | * requested parent to see if the requested id is "gmac_clkin". |
| 855 | */ |
| 856 | ret = dev_read_string_index(parent->dev, "clock-output-names", |
| 857 | parent->id, &clock_output_name); |
| 858 | if (ret < 0) |
| 859 | return -ENODATA; |
| 860 | |
| 861 | /* If this is "gmac_clkin", switch to the external clock input */ |
| 862 | if (!strcmp(clock_output_name, "gmac_clkin")) { |
| 863 | debug("%s: switching RGMII to CLKIN\n", __func__); |
| 864 | rk_setreg(&grf->soc_con[4], BIT(14)); |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | return -EINVAL; |
| 869 | } |
| 870 | |
| 871 | static int rk3328_clk_set_parent(struct clk *clk, struct clk *parent) |
| 872 | { |
| 873 | switch (clk->id) { |
| 874 | case SCLK_MAC2IO: |
| 875 | return rk3328_gmac2io_set_parent(clk, parent); |
| 876 | case SCLK_MAC2IO_EXT: |
| 877 | return rk3328_gmac2io_ext_set_parent(clk, parent); |
| 878 | case DCLK_LCDC: |
Jagan Teki | c46620f | 2023-06-06 22:39:17 +0530 | [diff] [blame] | 879 | case USB480M: |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 880 | case SCLK_PDM: |
| 881 | case SCLK_RTC32K: |
| 882 | case SCLK_UART0: |
| 883 | case SCLK_UART1: |
| 884 | case SCLK_UART2: |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | debug("%s: unsupported clk %ld\n", __func__, clk->id); |
| 889 | return -ENOENT; |
| 890 | } |
| 891 | |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 892 | static struct clk_ops rk3328_clk_ops = { |
| 893 | .get_rate = rk3328_clk_get_rate, |
| 894 | .set_rate = rk3328_clk_set_rate, |
David Wu | f01c581 | 2018-01-13 14:02:36 +0800 | [diff] [blame] | 895 | .set_parent = rk3328_clk_set_parent, |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 896 | }; |
| 897 | |
| 898 | static int rk3328_clk_probe(struct udevice *dev) |
| 899 | { |
| 900 | struct rk3328_clk_priv *priv = dev_get_priv(dev); |
| 901 | |
| 902 | rkclk_init(priv->cru); |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 907 | static int rk3328_clk_of_to_plat(struct udevice *dev) |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 908 | { |
| 909 | struct rk3328_clk_priv *priv = dev_get_priv(dev); |
| 910 | |
Kever Yang | bb870a5 | 2018-02-11 11:53:09 +0800 | [diff] [blame] | 911 | priv->cru = dev_read_addr_ptr(dev); |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 912 | |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | static int rk3328_clk_bind(struct udevice *dev) |
| 917 | { |
| 918 | int ret; |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 919 | struct udevice *sys_child; |
| 920 | struct sysreset_reg *priv; |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 921 | |
| 922 | /* The reset driver does not have a device node, so bind it here */ |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 923 | ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset", |
| 924 | &sys_child); |
| 925 | if (ret) { |
| 926 | debug("Warning: No sysreset driver: ret=%d\n", ret); |
| 927 | } else { |
| 928 | priv = malloc(sizeof(struct sysreset_reg)); |
| 929 | priv->glb_srst_fst_value = offsetof(struct rk3328_cru, |
| 930 | glb_srst_fst_value); |
| 931 | priv->glb_srst_snd_value = offsetof(struct rk3328_cru, |
| 932 | glb_srst_snd_value); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 933 | dev_set_priv(sys_child, priv); |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 934 | } |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 935 | |
Heiko Stuebner | 416f8d3 | 2019-11-09 00:06:30 +0100 | [diff] [blame] | 936 | #if CONFIG_IS_ENABLED(RESET_ROCKCHIP) |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 937 | ret = offsetof(struct rk3328_cru, softrst_con[0]); |
| 938 | ret = rockchip_reset_bind(dev, ret, 12); |
| 939 | if (ret) |
Eugen Hristev | f179826 | 2023-04-11 10:17:56 +0300 | [diff] [blame] | 940 | debug("Warning: software reset driver bind failed\n"); |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 941 | #endif |
| 942 | |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | static const struct udevice_id rk3328_clk_ids[] = { |
| 947 | { .compatible = "rockchip,rk3328-cru" }, |
| 948 | { } |
| 949 | }; |
| 950 | |
| 951 | U_BOOT_DRIVER(rockchip_rk3328_cru) = { |
| 952 | .name = "rockchip_rk3328_cru", |
| 953 | .id = UCLASS_CLK, |
| 954 | .of_match = rk3328_clk_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 955 | .priv_auto = sizeof(struct rk3328_clk_priv), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 956 | .of_to_plat = rk3328_clk_of_to_plat, |
Kever Yang | 1cfd550 | 2017-02-23 15:37:52 +0800 | [diff] [blame] | 957 | .ops = &rk3328_clk_ops, |
| 958 | .bind = rk3328_clk_bind, |
| 959 | .probe = rk3328_clk_probe, |
| 960 | }; |