Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 4 | * (C) 2017 Theobroma Systems Design und Consulting GmbH |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 7 | #include <clk-uclass.h> |
| 8 | #include <dm.h> |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 9 | #include <dt-structs.h> |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 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 | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 13 | #include <mapmem.h> |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 14 | #include <syscon.h> |
David Wu | f91b9b4 | 2017-09-20 14:38:58 +0800 | [diff] [blame] | 15 | #include <bitfield.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 16 | #include <asm/arch-rockchip/clock.h> |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 17 | #include <asm/arch-rockchip/cru.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 18 | #include <asm/arch-rockchip/hardware.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 20 | #include <dm/device-internal.h> |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 21 | #include <dm/lists.h> |
| 22 | #include <dt-bindings/clock/rk3399-cru.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 24 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 25 | #include <linux/printk.h> |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 26 | |
Alper Nebi Yasak | 66327ce | 2020-10-28 00:15:10 +0300 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 29 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 30 | struct rk3399_clk_plat { |
| 31 | struct dtd_rockchip_rk3399_cru dtd; |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 34 | struct rk3399_pmuclk_plat { |
| 35 | struct dtd_rockchip_rk3399_pmucru dtd; |
| 36 | }; |
| 37 | #endif |
| 38 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 39 | struct pll_div { |
| 40 | u32 refdiv; |
| 41 | u32 fbdiv; |
| 42 | u32 postdiv1; |
| 43 | u32 postdiv2; |
| 44 | u32 frac; |
| 45 | }; |
| 46 | |
| 47 | #define RATE_TO_DIV(input_rate, output_rate) \ |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 48 | ((input_rate) / (output_rate) - 1) |
| 49 | #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1)) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 50 | |
| 51 | #define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\ |
| 52 | .refdiv = _refdiv,\ |
| 53 | .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\ |
| 54 | .postdiv1 = _postdiv1, .postdiv2 = _postdiv2}; |
| 55 | |
| 56 | static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2, 1); |
| 57 | static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2, 2); |
Alper Nebi Yasak | 66327ce | 2020-10-28 00:15:10 +0300 | [diff] [blame] | 58 | #if !defined(CONFIG_SPL_BUILD) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 59 | static const struct pll_div ppll_init_cfg = PLL_DIVISORS(PPLL_HZ, 2, 2, 1); |
Philipp Tomsich | cf0a4ba | 2017-03-24 19:24:24 +0100 | [diff] [blame] | 60 | #endif |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 61 | |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 62 | static const struct pll_div apll_l_1600_cfg = PLL_DIVISORS(1600 * MHz, 3, 1, 1); |
| 63 | static const struct pll_div apll_l_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 64 | |
| 65 | static const struct pll_div *apll_l_cfgs[] = { |
| 66 | [APLL_L_1600_MHZ] = &apll_l_1600_cfg, |
| 67 | [APLL_L_600_MHZ] = &apll_l_600_cfg, |
| 68 | }; |
| 69 | |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 70 | static const struct pll_div apll_b_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1); |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 71 | static const struct pll_div *apll_b_cfgs[] = { |
| 72 | [APLL_B_600_MHZ] = &apll_b_600_cfg, |
| 73 | }; |
| 74 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 75 | enum { |
| 76 | /* PLL_CON0 */ |
| 77 | PLL_FBDIV_MASK = 0xfff, |
| 78 | PLL_FBDIV_SHIFT = 0, |
| 79 | |
| 80 | /* PLL_CON1 */ |
| 81 | PLL_POSTDIV2_SHIFT = 12, |
| 82 | PLL_POSTDIV2_MASK = 0x7 << PLL_POSTDIV2_SHIFT, |
| 83 | PLL_POSTDIV1_SHIFT = 8, |
| 84 | PLL_POSTDIV1_MASK = 0x7 << PLL_POSTDIV1_SHIFT, |
| 85 | PLL_REFDIV_MASK = 0x3f, |
| 86 | PLL_REFDIV_SHIFT = 0, |
| 87 | |
| 88 | /* PLL_CON2 */ |
| 89 | PLL_LOCK_STATUS_SHIFT = 31, |
| 90 | PLL_LOCK_STATUS_MASK = 1 << PLL_LOCK_STATUS_SHIFT, |
| 91 | PLL_FRACDIV_MASK = 0xffffff, |
| 92 | PLL_FRACDIV_SHIFT = 0, |
| 93 | |
| 94 | /* PLL_CON3 */ |
| 95 | PLL_MODE_SHIFT = 8, |
| 96 | PLL_MODE_MASK = 3 << PLL_MODE_SHIFT, |
| 97 | PLL_MODE_SLOW = 0, |
| 98 | PLL_MODE_NORM, |
| 99 | PLL_MODE_DEEP, |
| 100 | PLL_DSMPD_SHIFT = 3, |
| 101 | PLL_DSMPD_MASK = 1 << PLL_DSMPD_SHIFT, |
| 102 | PLL_INTEGER_MODE = 1, |
| 103 | |
| 104 | /* PMUCRU_CLKSEL_CON0 */ |
| 105 | PMU_PCLK_DIV_CON_MASK = 0x1f, |
| 106 | PMU_PCLK_DIV_CON_SHIFT = 0, |
| 107 | |
| 108 | /* PMUCRU_CLKSEL_CON1 */ |
| 109 | SPI3_PLL_SEL_SHIFT = 7, |
| 110 | SPI3_PLL_SEL_MASK = 1 << SPI3_PLL_SEL_SHIFT, |
| 111 | SPI3_PLL_SEL_24M = 0, |
| 112 | SPI3_PLL_SEL_PPLL = 1, |
| 113 | SPI3_DIV_CON_SHIFT = 0x0, |
| 114 | SPI3_DIV_CON_MASK = 0x7f, |
| 115 | |
| 116 | /* PMUCRU_CLKSEL_CON2 */ |
| 117 | I2C_DIV_CON_MASK = 0x7f, |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 118 | CLK_I2C8_DIV_CON_SHIFT = 8, |
| 119 | CLK_I2C0_DIV_CON_SHIFT = 0, |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 120 | |
| 121 | /* PMUCRU_CLKSEL_CON3 */ |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 122 | CLK_I2C4_DIV_CON_SHIFT = 0, |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 123 | |
| 124 | /* CLKSEL_CON0 */ |
| 125 | ACLKM_CORE_L_DIV_CON_SHIFT = 8, |
| 126 | ACLKM_CORE_L_DIV_CON_MASK = 0x1f << ACLKM_CORE_L_DIV_CON_SHIFT, |
| 127 | CLK_CORE_L_PLL_SEL_SHIFT = 6, |
| 128 | CLK_CORE_L_PLL_SEL_MASK = 3 << CLK_CORE_L_PLL_SEL_SHIFT, |
| 129 | CLK_CORE_L_PLL_SEL_ALPLL = 0x0, |
| 130 | CLK_CORE_L_PLL_SEL_ABPLL = 0x1, |
| 131 | CLK_CORE_L_PLL_SEL_DPLL = 0x10, |
| 132 | CLK_CORE_L_PLL_SEL_GPLL = 0x11, |
| 133 | CLK_CORE_L_DIV_MASK = 0x1f, |
| 134 | CLK_CORE_L_DIV_SHIFT = 0, |
| 135 | |
| 136 | /* CLKSEL_CON1 */ |
| 137 | PCLK_DBG_L_DIV_SHIFT = 0x8, |
| 138 | PCLK_DBG_L_DIV_MASK = 0x1f << PCLK_DBG_L_DIV_SHIFT, |
| 139 | ATCLK_CORE_L_DIV_SHIFT = 0, |
| 140 | ATCLK_CORE_L_DIV_MASK = 0x1f << ATCLK_CORE_L_DIV_SHIFT, |
| 141 | |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 142 | /* CLKSEL_CON2 */ |
| 143 | ACLKM_CORE_B_DIV_CON_SHIFT = 8, |
| 144 | ACLKM_CORE_B_DIV_CON_MASK = 0x1f << ACLKM_CORE_B_DIV_CON_SHIFT, |
| 145 | CLK_CORE_B_PLL_SEL_SHIFT = 6, |
| 146 | CLK_CORE_B_PLL_SEL_MASK = 3 << CLK_CORE_B_PLL_SEL_SHIFT, |
| 147 | CLK_CORE_B_PLL_SEL_ALPLL = 0x0, |
| 148 | CLK_CORE_B_PLL_SEL_ABPLL = 0x1, |
| 149 | CLK_CORE_B_PLL_SEL_DPLL = 0x10, |
| 150 | CLK_CORE_B_PLL_SEL_GPLL = 0x11, |
| 151 | CLK_CORE_B_DIV_MASK = 0x1f, |
| 152 | CLK_CORE_B_DIV_SHIFT = 0, |
| 153 | |
| 154 | /* CLKSEL_CON3 */ |
| 155 | PCLK_DBG_B_DIV_SHIFT = 0x8, |
| 156 | PCLK_DBG_B_DIV_MASK = 0x1f << PCLK_DBG_B_DIV_SHIFT, |
| 157 | ATCLK_CORE_B_DIV_SHIFT = 0, |
| 158 | ATCLK_CORE_B_DIV_MASK = 0x1f << ATCLK_CORE_B_DIV_SHIFT, |
| 159 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 160 | /* CLKSEL_CON14 */ |
| 161 | PCLK_PERIHP_DIV_CON_SHIFT = 12, |
| 162 | PCLK_PERIHP_DIV_CON_MASK = 0x7 << PCLK_PERIHP_DIV_CON_SHIFT, |
| 163 | HCLK_PERIHP_DIV_CON_SHIFT = 8, |
| 164 | HCLK_PERIHP_DIV_CON_MASK = 3 << HCLK_PERIHP_DIV_CON_SHIFT, |
| 165 | ACLK_PERIHP_PLL_SEL_SHIFT = 7, |
| 166 | ACLK_PERIHP_PLL_SEL_MASK = 1 << ACLK_PERIHP_PLL_SEL_SHIFT, |
| 167 | ACLK_PERIHP_PLL_SEL_CPLL = 0, |
| 168 | ACLK_PERIHP_PLL_SEL_GPLL = 1, |
| 169 | ACLK_PERIHP_DIV_CON_SHIFT = 0, |
| 170 | ACLK_PERIHP_DIV_CON_MASK = 0x1f, |
| 171 | |
| 172 | /* CLKSEL_CON21 */ |
| 173 | ACLK_EMMC_PLL_SEL_SHIFT = 7, |
| 174 | ACLK_EMMC_PLL_SEL_MASK = 0x1 << ACLK_EMMC_PLL_SEL_SHIFT, |
| 175 | ACLK_EMMC_PLL_SEL_GPLL = 0x1, |
| 176 | ACLK_EMMC_DIV_CON_SHIFT = 0, |
| 177 | ACLK_EMMC_DIV_CON_MASK = 0x1f, |
| 178 | |
| 179 | /* CLKSEL_CON22 */ |
| 180 | CLK_EMMC_PLL_SHIFT = 8, |
| 181 | CLK_EMMC_PLL_MASK = 0x7 << CLK_EMMC_PLL_SHIFT, |
| 182 | CLK_EMMC_PLL_SEL_GPLL = 0x1, |
Kever Yang | dc850de | 2016-08-04 11:44:58 +0800 | [diff] [blame] | 183 | CLK_EMMC_PLL_SEL_24M = 0x5, |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 184 | CLK_EMMC_DIV_CON_SHIFT = 0, |
| 185 | CLK_EMMC_DIV_CON_MASK = 0x7f << CLK_EMMC_DIV_CON_SHIFT, |
| 186 | |
| 187 | /* CLKSEL_CON23 */ |
| 188 | PCLK_PERILP0_DIV_CON_SHIFT = 12, |
| 189 | PCLK_PERILP0_DIV_CON_MASK = 0x7 << PCLK_PERILP0_DIV_CON_SHIFT, |
| 190 | HCLK_PERILP0_DIV_CON_SHIFT = 8, |
| 191 | HCLK_PERILP0_DIV_CON_MASK = 3 << HCLK_PERILP0_DIV_CON_SHIFT, |
| 192 | ACLK_PERILP0_PLL_SEL_SHIFT = 7, |
| 193 | ACLK_PERILP0_PLL_SEL_MASK = 1 << ACLK_PERILP0_PLL_SEL_SHIFT, |
| 194 | ACLK_PERILP0_PLL_SEL_CPLL = 0, |
| 195 | ACLK_PERILP0_PLL_SEL_GPLL = 1, |
| 196 | ACLK_PERILP0_DIV_CON_SHIFT = 0, |
| 197 | ACLK_PERILP0_DIV_CON_MASK = 0x1f, |
| 198 | |
| 199 | /* CLKSEL_CON25 */ |
| 200 | PCLK_PERILP1_DIV_CON_SHIFT = 8, |
| 201 | PCLK_PERILP1_DIV_CON_MASK = 0x7 << PCLK_PERILP1_DIV_CON_SHIFT, |
| 202 | HCLK_PERILP1_PLL_SEL_SHIFT = 7, |
| 203 | HCLK_PERILP1_PLL_SEL_MASK = 1 << HCLK_PERILP1_PLL_SEL_SHIFT, |
| 204 | HCLK_PERILP1_PLL_SEL_CPLL = 0, |
| 205 | HCLK_PERILP1_PLL_SEL_GPLL = 1, |
| 206 | HCLK_PERILP1_DIV_CON_SHIFT = 0, |
| 207 | HCLK_PERILP1_DIV_CON_MASK = 0x1f, |
| 208 | |
| 209 | /* CLKSEL_CON26 */ |
| 210 | CLK_SARADC_DIV_CON_SHIFT = 8, |
David Wu | f91b9b4 | 2017-09-20 14:38:58 +0800 | [diff] [blame] | 211 | CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8), |
| 212 | CLK_SARADC_DIV_CON_WIDTH = 8, |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 213 | |
| 214 | /* CLKSEL_CON27 */ |
| 215 | CLK_TSADC_SEL_X24M = 0x0, |
| 216 | CLK_TSADC_SEL_SHIFT = 15, |
| 217 | CLK_TSADC_SEL_MASK = 1 << CLK_TSADC_SEL_SHIFT, |
| 218 | CLK_TSADC_DIV_CON_SHIFT = 0, |
| 219 | CLK_TSADC_DIV_CON_MASK = 0x3ff, |
| 220 | |
| 221 | /* CLKSEL_CON47 & CLKSEL_CON48 */ |
| 222 | ACLK_VOP_PLL_SEL_SHIFT = 6, |
| 223 | ACLK_VOP_PLL_SEL_MASK = 0x3 << ACLK_VOP_PLL_SEL_SHIFT, |
| 224 | ACLK_VOP_PLL_SEL_CPLL = 0x1, |
| 225 | ACLK_VOP_DIV_CON_SHIFT = 0, |
| 226 | ACLK_VOP_DIV_CON_MASK = 0x1f << ACLK_VOP_DIV_CON_SHIFT, |
| 227 | |
| 228 | /* CLKSEL_CON49 & CLKSEL_CON50 */ |
| 229 | DCLK_VOP_DCLK_SEL_SHIFT = 11, |
| 230 | DCLK_VOP_DCLK_SEL_MASK = 1 << DCLK_VOP_DCLK_SEL_SHIFT, |
| 231 | DCLK_VOP_DCLK_SEL_DIVOUT = 0, |
| 232 | DCLK_VOP_PLL_SEL_SHIFT = 8, |
| 233 | DCLK_VOP_PLL_SEL_MASK = 3 << DCLK_VOP_PLL_SEL_SHIFT, |
| 234 | DCLK_VOP_PLL_SEL_VPLL = 0, |
| 235 | DCLK_VOP_DIV_CON_MASK = 0xff, |
| 236 | DCLK_VOP_DIV_CON_SHIFT = 0, |
| 237 | |
Jack Mitchell | 4ef38ce | 2020-09-17 10:42:06 +0100 | [diff] [blame] | 238 | /* CLKSEL_CON57 */ |
| 239 | PCLK_ALIVE_DIV_CON_SHIFT = 0, |
| 240 | PCLK_ALIVE_DIV_CON_MASK = 0x1f << PCLK_ALIVE_DIV_CON_SHIFT, |
| 241 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 242 | /* CLKSEL_CON58 */ |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 243 | CLK_SPI_PLL_SEL_WIDTH = 1, |
| 244 | CLK_SPI_PLL_SEL_MASK = ((1 < CLK_SPI_PLL_SEL_WIDTH) - 1), |
| 245 | CLK_SPI_PLL_SEL_CPLL = 0, |
| 246 | CLK_SPI_PLL_SEL_GPLL = 1, |
| 247 | CLK_SPI_PLL_DIV_CON_WIDTH = 7, |
| 248 | CLK_SPI_PLL_DIV_CON_MASK = ((1 << CLK_SPI_PLL_DIV_CON_WIDTH) - 1), |
| 249 | |
| 250 | CLK_SPI5_PLL_DIV_CON_SHIFT = 8, |
| 251 | CLK_SPI5_PLL_SEL_SHIFT = 15, |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 252 | |
| 253 | /* CLKSEL_CON59 */ |
| 254 | CLK_SPI1_PLL_SEL_SHIFT = 15, |
| 255 | CLK_SPI1_PLL_DIV_CON_SHIFT = 8, |
| 256 | CLK_SPI0_PLL_SEL_SHIFT = 7, |
| 257 | CLK_SPI0_PLL_DIV_CON_SHIFT = 0, |
| 258 | |
| 259 | /* CLKSEL_CON60 */ |
| 260 | CLK_SPI4_PLL_SEL_SHIFT = 15, |
| 261 | CLK_SPI4_PLL_DIV_CON_SHIFT = 8, |
| 262 | CLK_SPI2_PLL_SEL_SHIFT = 7, |
| 263 | CLK_SPI2_PLL_DIV_CON_SHIFT = 0, |
| 264 | |
| 265 | /* CLKSEL_CON61 */ |
| 266 | CLK_I2C_PLL_SEL_MASK = 1, |
| 267 | CLK_I2C_PLL_SEL_CPLL = 0, |
| 268 | CLK_I2C_PLL_SEL_GPLL = 1, |
| 269 | CLK_I2C5_PLL_SEL_SHIFT = 15, |
| 270 | CLK_I2C5_DIV_CON_SHIFT = 8, |
| 271 | CLK_I2C1_PLL_SEL_SHIFT = 7, |
| 272 | CLK_I2C1_DIV_CON_SHIFT = 0, |
| 273 | |
| 274 | /* CLKSEL_CON62 */ |
| 275 | CLK_I2C6_PLL_SEL_SHIFT = 15, |
| 276 | CLK_I2C6_DIV_CON_SHIFT = 8, |
| 277 | CLK_I2C2_PLL_SEL_SHIFT = 7, |
| 278 | CLK_I2C2_DIV_CON_SHIFT = 0, |
| 279 | |
| 280 | /* CLKSEL_CON63 */ |
| 281 | CLK_I2C7_PLL_SEL_SHIFT = 15, |
| 282 | CLK_I2C7_DIV_CON_SHIFT = 8, |
| 283 | CLK_I2C3_PLL_SEL_SHIFT = 7, |
| 284 | CLK_I2C3_DIV_CON_SHIFT = 0, |
| 285 | |
| 286 | /* CRU_SOFTRST_CON4 */ |
| 287 | RESETN_DDR0_REQ_SHIFT = 8, |
| 288 | RESETN_DDR0_REQ_MASK = 1 << RESETN_DDR0_REQ_SHIFT, |
| 289 | RESETN_DDRPHY0_REQ_SHIFT = 9, |
| 290 | RESETN_DDRPHY0_REQ_MASK = 1 << RESETN_DDRPHY0_REQ_SHIFT, |
| 291 | RESETN_DDR1_REQ_SHIFT = 12, |
| 292 | RESETN_DDR1_REQ_MASK = 1 << RESETN_DDR1_REQ_SHIFT, |
| 293 | RESETN_DDRPHY1_REQ_SHIFT = 13, |
| 294 | RESETN_DDRPHY1_REQ_MASK = 1 << RESETN_DDRPHY1_REQ_SHIFT, |
| 295 | }; |
| 296 | |
| 297 | #define VCO_MAX_KHZ (3200 * (MHz / KHz)) |
| 298 | #define VCO_MIN_KHZ (800 * (MHz / KHz)) |
| 299 | #define OUTPUT_MAX_KHZ (3200 * (MHz / KHz)) |
| 300 | #define OUTPUT_MIN_KHZ (16 * (MHz / KHz)) |
| 301 | |
| 302 | /* |
| 303 | * the div restructions of pll in integer mode, these are defined in |
| 304 | * * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0 |
| 305 | */ |
| 306 | #define PLL_DIV_MIN 16 |
| 307 | #define PLL_DIV_MAX 3200 |
| 308 | |
| 309 | /* |
| 310 | * How to calculate the PLL(from TRM V0.3 Part 1 Page 63): |
| 311 | * Formulas also embedded within the Fractional PLL Verilog model: |
| 312 | * If DSMPD = 1 (DSM is disabled, "integer mode") |
| 313 | * FOUTVCO = FREF / REFDIV * FBDIV |
| 314 | * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2 |
| 315 | * Where: |
| 316 | * FOUTVCO = Fractional PLL non-divided output frequency |
| 317 | * FOUTPOSTDIV = Fractional PLL divided output frequency |
| 318 | * (output of second post divider) |
| 319 | * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input) |
| 320 | * REFDIV = Fractional PLL input reference clock divider |
| 321 | * FBDIV = Integer value programmed into feedback divide |
| 322 | * |
| 323 | */ |
| 324 | static void rkclk_set_pll(u32 *pll_con, const struct pll_div *div) |
| 325 | { |
| 326 | /* All 8 PLLs have same VCO and output frequency range restrictions. */ |
| 327 | u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv; |
| 328 | u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2; |
| 329 | |
| 330 | debug("PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, " |
| 331 | "postdiv2=%d, vco=%u khz, output=%u khz\n", |
| 332 | pll_con, div->fbdiv, div->refdiv, div->postdiv1, |
| 333 | div->postdiv2, vco_khz, output_khz); |
| 334 | assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ && |
| 335 | output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ && |
| 336 | div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX); |
| 337 | |
| 338 | /* |
| 339 | * When power on or changing PLL setting, |
| 340 | * we must force PLL into slow mode to ensure output stable clock. |
| 341 | */ |
| 342 | rk_clrsetreg(&pll_con[3], PLL_MODE_MASK, |
| 343 | PLL_MODE_SLOW << PLL_MODE_SHIFT); |
| 344 | |
| 345 | /* use integer mode */ |
| 346 | rk_clrsetreg(&pll_con[3], PLL_DSMPD_MASK, |
| 347 | PLL_INTEGER_MODE << PLL_DSMPD_SHIFT); |
| 348 | |
| 349 | rk_clrsetreg(&pll_con[0], PLL_FBDIV_MASK, |
| 350 | div->fbdiv << PLL_FBDIV_SHIFT); |
| 351 | rk_clrsetreg(&pll_con[1], |
| 352 | PLL_POSTDIV2_MASK | PLL_POSTDIV1_MASK | |
| 353 | PLL_REFDIV_MASK | PLL_REFDIV_SHIFT, |
| 354 | (div->postdiv2 << PLL_POSTDIV2_SHIFT) | |
| 355 | (div->postdiv1 << PLL_POSTDIV1_SHIFT) | |
| 356 | (div->refdiv << PLL_REFDIV_SHIFT)); |
| 357 | |
| 358 | /* waiting for pll lock */ |
| 359 | while (!(readl(&pll_con[2]) & (1 << PLL_LOCK_STATUS_SHIFT))) |
| 360 | udelay(1); |
| 361 | |
| 362 | /* pll enter normal mode */ |
| 363 | rk_clrsetreg(&pll_con[3], PLL_MODE_MASK, |
| 364 | PLL_MODE_NORM << PLL_MODE_SHIFT); |
| 365 | } |
| 366 | |
| 367 | static int pll_para_config(u32 freq_hz, struct pll_div *div) |
| 368 | { |
| 369 | u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0; |
| 370 | u32 postdiv1, postdiv2 = 1; |
| 371 | u32 fref_khz; |
| 372 | u32 diff_khz, best_diff_khz; |
| 373 | const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16; |
| 374 | const u32 max_postdiv1 = 7, max_postdiv2 = 7; |
| 375 | u32 vco_khz; |
| 376 | u32 freq_khz = freq_hz / KHz; |
| 377 | |
| 378 | if (!freq_hz) { |
| 379 | printf("%s: the frequency can't be 0 Hz\n", __func__); |
| 380 | return -1; |
| 381 | } |
| 382 | |
| 383 | postdiv1 = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz); |
| 384 | if (postdiv1 > max_postdiv1) { |
| 385 | postdiv2 = DIV_ROUND_UP(postdiv1, max_postdiv1); |
| 386 | postdiv1 = DIV_ROUND_UP(postdiv1, postdiv2); |
| 387 | } |
| 388 | |
| 389 | vco_khz = freq_khz * postdiv1 * postdiv2; |
| 390 | |
| 391 | if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || |
| 392 | postdiv2 > max_postdiv2) { |
| 393 | printf("%s: Cannot find out a supported VCO" |
| 394 | " for Frequency (%uHz).\n", __func__, freq_hz); |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | div->postdiv1 = postdiv1; |
| 399 | div->postdiv2 = postdiv2; |
| 400 | |
| 401 | best_diff_khz = vco_khz; |
| 402 | for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) { |
| 403 | fref_khz = ref_khz / refdiv; |
| 404 | |
| 405 | fbdiv = vco_khz / fref_khz; |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 406 | if (fbdiv >= max_fbdiv || fbdiv <= min_fbdiv) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 407 | continue; |
| 408 | diff_khz = vco_khz - fbdiv * fref_khz; |
| 409 | if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) { |
| 410 | fbdiv++; |
| 411 | diff_khz = fref_khz - diff_khz; |
| 412 | } |
| 413 | |
| 414 | if (diff_khz >= best_diff_khz) |
| 415 | continue; |
| 416 | |
| 417 | best_diff_khz = diff_khz; |
| 418 | div->refdiv = refdiv; |
| 419 | div->fbdiv = fbdiv; |
| 420 | } |
| 421 | |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 422 | if (best_diff_khz > 4 * (MHz / KHz)) { |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 423 | printf("%s: Failed to match output frequency %u, " |
| 424 | "difference is %u Hz,exceed 4MHZ\n", __func__, freq_hz, |
| 425 | best_diff_khz * KHz); |
| 426 | return -1; |
| 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 431 | void rk3399_configure_cpu_l(struct rockchip_cru *cru, |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 432 | enum apll_l_frequencies apll_l_freq) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 433 | { |
| 434 | u32 aclkm_div; |
| 435 | u32 pclk_dbg_div; |
| 436 | u32 atclk_div; |
| 437 | |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 438 | /* Setup cluster L */ |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 439 | rkclk_set_pll(&cru->apll_l_con[0], apll_l_cfgs[apll_l_freq]); |
| 440 | |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 441 | aclkm_div = LPLL_HZ / ACLKM_CORE_L_HZ - 1; |
| 442 | assert((aclkm_div + 1) * ACLKM_CORE_L_HZ == LPLL_HZ && |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 443 | aclkm_div < 0x1f); |
| 444 | |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 445 | pclk_dbg_div = LPLL_HZ / PCLK_DBG_L_HZ - 1; |
| 446 | assert((pclk_dbg_div + 1) * PCLK_DBG_L_HZ == LPLL_HZ && |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 447 | pclk_dbg_div < 0x1f); |
| 448 | |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 449 | atclk_div = LPLL_HZ / ATCLK_CORE_L_HZ - 1; |
| 450 | assert((atclk_div + 1) * ATCLK_CORE_L_HZ == LPLL_HZ && |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 451 | atclk_div < 0x1f); |
| 452 | |
| 453 | rk_clrsetreg(&cru->clksel_con[0], |
| 454 | ACLKM_CORE_L_DIV_CON_MASK | CLK_CORE_L_PLL_SEL_MASK | |
| 455 | CLK_CORE_L_DIV_MASK, |
| 456 | aclkm_div << ACLKM_CORE_L_DIV_CON_SHIFT | |
| 457 | CLK_CORE_L_PLL_SEL_ALPLL << CLK_CORE_L_PLL_SEL_SHIFT | |
| 458 | 0 << CLK_CORE_L_DIV_SHIFT); |
| 459 | |
| 460 | rk_clrsetreg(&cru->clksel_con[1], |
| 461 | PCLK_DBG_L_DIV_MASK | ATCLK_CORE_L_DIV_MASK, |
| 462 | pclk_dbg_div << PCLK_DBG_L_DIV_SHIFT | |
| 463 | atclk_div << ATCLK_CORE_L_DIV_SHIFT); |
| 464 | } |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 465 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 466 | void rk3399_configure_cpu_b(struct rockchip_cru *cru, |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 467 | enum apll_b_frequencies apll_b_freq) |
| 468 | { |
| 469 | u32 aclkm_div; |
| 470 | u32 pclk_dbg_div; |
| 471 | u32 atclk_div; |
| 472 | |
| 473 | /* Setup cluster B */ |
| 474 | rkclk_set_pll(&cru->apll_b_con[0], apll_b_cfgs[apll_b_freq]); |
| 475 | |
| 476 | aclkm_div = BPLL_HZ / ACLKM_CORE_B_HZ - 1; |
| 477 | assert((aclkm_div + 1) * ACLKM_CORE_B_HZ == BPLL_HZ && |
| 478 | aclkm_div < 0x1f); |
| 479 | |
| 480 | pclk_dbg_div = BPLL_HZ / PCLK_DBG_B_HZ - 1; |
| 481 | assert((pclk_dbg_div + 1) * PCLK_DBG_B_HZ == BPLL_HZ && |
| 482 | pclk_dbg_div < 0x1f); |
| 483 | |
| 484 | atclk_div = BPLL_HZ / ATCLK_CORE_B_HZ - 1; |
| 485 | assert((atclk_div + 1) * ATCLK_CORE_B_HZ == BPLL_HZ && |
| 486 | atclk_div < 0x1f); |
| 487 | |
| 488 | rk_clrsetreg(&cru->clksel_con[2], |
| 489 | ACLKM_CORE_B_DIV_CON_MASK | CLK_CORE_B_PLL_SEL_MASK | |
| 490 | CLK_CORE_B_DIV_MASK, |
| 491 | aclkm_div << ACLKM_CORE_B_DIV_CON_SHIFT | |
| 492 | CLK_CORE_B_PLL_SEL_ABPLL << CLK_CORE_B_PLL_SEL_SHIFT | |
| 493 | 0 << CLK_CORE_B_DIV_SHIFT); |
| 494 | |
| 495 | rk_clrsetreg(&cru->clksel_con[3], |
| 496 | PCLK_DBG_B_DIV_MASK | ATCLK_CORE_B_DIV_MASK, |
| 497 | pclk_dbg_div << PCLK_DBG_B_DIV_SHIFT | |
| 498 | atclk_div << ATCLK_CORE_B_DIV_SHIFT); |
| 499 | } |
| 500 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 501 | #define I2C_CLK_REG_MASK(bus) \ |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 502 | (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT | \ |
| 503 | CLK_I2C_PLL_SEL_MASK << CLK_I2C ##bus## _PLL_SEL_SHIFT) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 504 | |
| 505 | #define I2C_CLK_REG_VALUE(bus, clk_div) \ |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 506 | ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT | \ |
| 507 | CLK_I2C_PLL_SEL_GPLL << CLK_I2C ##bus## _PLL_SEL_SHIFT) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 508 | |
| 509 | #define I2C_CLK_DIV_VALUE(con, bus) \ |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 510 | ((con >> CLK_I2C ##bus## _DIV_CON_SHIFT) & I2C_DIV_CON_MASK) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 511 | |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 512 | #define I2C_PMUCLK_REG_MASK(bus) \ |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 513 | (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT) |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 514 | |
| 515 | #define I2C_PMUCLK_REG_VALUE(bus, clk_div) \ |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 516 | ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT) |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 517 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 518 | static ulong rk3399_i2c_get_clk(struct rockchip_cru *cru, ulong clk_id) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 519 | { |
| 520 | u32 div, con; |
| 521 | |
| 522 | switch (clk_id) { |
| 523 | case SCLK_I2C1: |
| 524 | con = readl(&cru->clksel_con[61]); |
| 525 | div = I2C_CLK_DIV_VALUE(con, 1); |
| 526 | break; |
| 527 | case SCLK_I2C2: |
| 528 | con = readl(&cru->clksel_con[62]); |
| 529 | div = I2C_CLK_DIV_VALUE(con, 2); |
| 530 | break; |
| 531 | case SCLK_I2C3: |
| 532 | con = readl(&cru->clksel_con[63]); |
| 533 | div = I2C_CLK_DIV_VALUE(con, 3); |
| 534 | break; |
| 535 | case SCLK_I2C5: |
| 536 | con = readl(&cru->clksel_con[61]); |
| 537 | div = I2C_CLK_DIV_VALUE(con, 5); |
| 538 | break; |
| 539 | case SCLK_I2C6: |
| 540 | con = readl(&cru->clksel_con[62]); |
| 541 | div = I2C_CLK_DIV_VALUE(con, 6); |
| 542 | break; |
| 543 | case SCLK_I2C7: |
| 544 | con = readl(&cru->clksel_con[63]); |
| 545 | div = I2C_CLK_DIV_VALUE(con, 7); |
| 546 | break; |
| 547 | default: |
| 548 | printf("do not support this i2c bus\n"); |
| 549 | return -EINVAL; |
| 550 | } |
| 551 | |
| 552 | return DIV_TO_RATE(GPLL_HZ, div); |
| 553 | } |
| 554 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 555 | static ulong rk3399_i2c_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 556 | { |
| 557 | int src_clk_div; |
| 558 | |
| 559 | /* i2c0,4,8 src clock from ppll, i2c1,2,3,5,6,7 src clock from gpll*/ |
| 560 | src_clk_div = GPLL_HZ / hz; |
| 561 | assert(src_clk_div - 1 < 127); |
| 562 | |
| 563 | switch (clk_id) { |
| 564 | case SCLK_I2C1: |
| 565 | rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(1), |
| 566 | I2C_CLK_REG_VALUE(1, src_clk_div)); |
| 567 | break; |
| 568 | case SCLK_I2C2: |
| 569 | rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(2), |
| 570 | I2C_CLK_REG_VALUE(2, src_clk_div)); |
| 571 | break; |
| 572 | case SCLK_I2C3: |
| 573 | rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(3), |
| 574 | I2C_CLK_REG_VALUE(3, src_clk_div)); |
| 575 | break; |
| 576 | case SCLK_I2C5: |
| 577 | rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(5), |
| 578 | I2C_CLK_REG_VALUE(5, src_clk_div)); |
| 579 | break; |
| 580 | case SCLK_I2C6: |
| 581 | rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(6), |
| 582 | I2C_CLK_REG_VALUE(6, src_clk_div)); |
| 583 | break; |
| 584 | case SCLK_I2C7: |
| 585 | rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(7), |
| 586 | I2C_CLK_REG_VALUE(7, src_clk_div)); |
| 587 | break; |
| 588 | default: |
| 589 | printf("do not support this i2c bus\n"); |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | |
Philipp Tomsich | 30d7c15 | 2017-04-20 22:05:50 +0200 | [diff] [blame] | 593 | return rk3399_i2c_get_clk(cru, clk_id); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 594 | } |
| 595 | |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 596 | /* |
| 597 | * RK3399 SPI clocks have a common divider-width (7 bits) and a single bit |
| 598 | * to select either CPLL or GPLL as the clock-parent. The location within |
| 599 | * the enclosing CLKSEL_CON (i.e. div_shift and sel_shift) are variable. |
| 600 | */ |
| 601 | |
| 602 | struct spi_clkreg { |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 603 | u8 reg; /* CLKSEL_CON[reg] register in CRU */ |
| 604 | u8 div_shift; |
| 605 | u8 sel_shift; |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | /* |
| 609 | * The entries are numbered relative to their offset from SCLK_SPI0. |
| 610 | * |
| 611 | * Note that SCLK_SPI3 (which is configured via PMUCRU and requires different |
| 612 | * logic is not supported). |
| 613 | */ |
| 614 | static const struct spi_clkreg spi_clkregs[] = { |
| 615 | [0] = { .reg = 59, |
| 616 | .div_shift = CLK_SPI0_PLL_DIV_CON_SHIFT, |
| 617 | .sel_shift = CLK_SPI0_PLL_SEL_SHIFT, }, |
| 618 | [1] = { .reg = 59, |
| 619 | .div_shift = CLK_SPI1_PLL_DIV_CON_SHIFT, |
| 620 | .sel_shift = CLK_SPI1_PLL_SEL_SHIFT, }, |
| 621 | [2] = { .reg = 60, |
| 622 | .div_shift = CLK_SPI2_PLL_DIV_CON_SHIFT, |
| 623 | .sel_shift = CLK_SPI2_PLL_SEL_SHIFT, }, |
| 624 | [3] = { .reg = 60, |
| 625 | .div_shift = CLK_SPI4_PLL_DIV_CON_SHIFT, |
| 626 | .sel_shift = CLK_SPI4_PLL_SEL_SHIFT, }, |
| 627 | [4] = { .reg = 58, |
| 628 | .div_shift = CLK_SPI5_PLL_DIV_CON_SHIFT, |
| 629 | .sel_shift = CLK_SPI5_PLL_SEL_SHIFT, }, |
| 630 | }; |
| 631 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 632 | static ulong rk3399_spi_get_clk(struct rockchip_cru *cru, ulong clk_id) |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 633 | { |
| 634 | const struct spi_clkreg *spiclk = NULL; |
| 635 | u32 div, val; |
| 636 | |
| 637 | switch (clk_id) { |
| 638 | case SCLK_SPI0 ... SCLK_SPI5: |
| 639 | spiclk = &spi_clkregs[clk_id - SCLK_SPI0]; |
| 640 | break; |
| 641 | |
| 642 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 643 | pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id); |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 644 | return -EINVAL; |
| 645 | } |
| 646 | |
| 647 | val = readl(&cru->clksel_con[spiclk->reg]); |
Philipp Tomsich | 8a4868f | 2017-11-22 19:45:04 +0100 | [diff] [blame] | 648 | div = bitfield_extract(val, spiclk->div_shift, |
| 649 | CLK_SPI_PLL_DIV_CON_WIDTH); |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 650 | |
| 651 | return DIV_TO_RATE(GPLL_HZ, div); |
| 652 | } |
| 653 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 654 | static ulong rk3399_spi_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz) |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 655 | { |
| 656 | const struct spi_clkreg *spiclk = NULL; |
| 657 | int src_clk_div; |
| 658 | |
Kever Yang | f20995b | 2017-07-27 12:54:02 +0800 | [diff] [blame] | 659 | src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz) - 1; |
| 660 | assert(src_clk_div < 128); |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 661 | |
| 662 | switch (clk_id) { |
| 663 | case SCLK_SPI1 ... SCLK_SPI5: |
| 664 | spiclk = &spi_clkregs[clk_id - SCLK_SPI0]; |
| 665 | break; |
| 666 | |
| 667 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 668 | pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id); |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 669 | return -EINVAL; |
| 670 | } |
| 671 | |
| 672 | rk_clrsetreg(&cru->clksel_con[spiclk->reg], |
| 673 | ((CLK_SPI_PLL_DIV_CON_MASK << spiclk->div_shift) | |
| 674 | (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift)), |
| 675 | ((src_clk_div << spiclk->div_shift) | |
| 676 | (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift))); |
| 677 | |
Philipp Tomsich | 30d7c15 | 2017-04-20 22:05:50 +0200 | [diff] [blame] | 678 | return rk3399_spi_get_clk(cru, clk_id); |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 681 | static ulong rk3399_vop_set_clk(struct rockchip_cru *cru, ulong clk_id, u32 hz) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 682 | { |
| 683 | struct pll_div vpll_config = {0}; |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 684 | int aclk_vop = 198 * MHz; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 685 | void *aclkreg_addr, *dclkreg_addr; |
| 686 | u32 div; |
| 687 | |
| 688 | switch (clk_id) { |
| 689 | case DCLK_VOP0: |
| 690 | aclkreg_addr = &cru->clksel_con[47]; |
| 691 | dclkreg_addr = &cru->clksel_con[49]; |
| 692 | break; |
| 693 | case DCLK_VOP1: |
| 694 | aclkreg_addr = &cru->clksel_con[48]; |
| 695 | dclkreg_addr = &cru->clksel_con[50]; |
| 696 | break; |
| 697 | default: |
| 698 | return -EINVAL; |
| 699 | } |
| 700 | /* vop aclk source clk: cpll */ |
| 701 | div = CPLL_HZ / aclk_vop; |
| 702 | assert(div - 1 < 32); |
| 703 | |
| 704 | rk_clrsetreg(aclkreg_addr, |
| 705 | ACLK_VOP_PLL_SEL_MASK | ACLK_VOP_DIV_CON_MASK, |
| 706 | ACLK_VOP_PLL_SEL_CPLL << ACLK_VOP_PLL_SEL_SHIFT | |
| 707 | (div - 1) << ACLK_VOP_DIV_CON_SHIFT); |
| 708 | |
| 709 | /* vop dclk source from vpll, and equals to vpll(means div == 1) */ |
| 710 | if (pll_para_config(hz, &vpll_config)) |
| 711 | return -1; |
| 712 | |
| 713 | rkclk_set_pll(&cru->vpll_con[0], &vpll_config); |
| 714 | |
| 715 | rk_clrsetreg(dclkreg_addr, |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 716 | DCLK_VOP_DCLK_SEL_MASK | DCLK_VOP_PLL_SEL_MASK | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 717 | DCLK_VOP_DIV_CON_MASK, |
| 718 | DCLK_VOP_DCLK_SEL_DIVOUT << DCLK_VOP_DCLK_SEL_SHIFT | |
| 719 | DCLK_VOP_PLL_SEL_VPLL << DCLK_VOP_PLL_SEL_SHIFT | |
| 720 | (1 - 1) << DCLK_VOP_DIV_CON_SHIFT); |
| 721 | |
| 722 | return hz; |
| 723 | } |
| 724 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 725 | static ulong rk3399_mmc_get_clk(struct rockchip_cru *cru, uint clk_id) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 726 | { |
| 727 | u32 div, con; |
| 728 | |
| 729 | switch (clk_id) { |
Michal Suchanek | 8478944 | 2022-08-21 09:17:24 +0200 | [diff] [blame] | 730 | case HCLK_SDIO: |
| 731 | case SCLK_SDIO: |
| 732 | con = readl(&cru->clksel_con[15]); |
| 733 | /* dwmmc controller have internal div 2 */ |
| 734 | div = 2; |
| 735 | break; |
Philipp Tomsich | 78a7314 | 2017-04-25 09:52:06 +0200 | [diff] [blame] | 736 | case HCLK_SDMMC: |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 737 | case SCLK_SDMMC: |
| 738 | con = readl(&cru->clksel_con[16]); |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 739 | /* dwmmc controller have internal div 2 */ |
| 740 | div = 2; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 741 | break; |
| 742 | case SCLK_EMMC: |
Jagan Teki | ad38600 | 2020-05-24 22:13:15 +0530 | [diff] [blame] | 743 | con = readl(&cru->clksel_con[22]); |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 744 | div = 1; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 745 | break; |
| 746 | default: |
| 747 | return -EINVAL; |
| 748 | } |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 749 | |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 750 | div *= (con & CLK_EMMC_DIV_CON_MASK) >> CLK_EMMC_DIV_CON_SHIFT; |
Kever Yang | dc850de | 2016-08-04 11:44:58 +0800 | [diff] [blame] | 751 | if ((con & CLK_EMMC_PLL_MASK) >> CLK_EMMC_PLL_SHIFT |
| 752 | == CLK_EMMC_PLL_SEL_24M) |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 753 | return DIV_TO_RATE(OSC_HZ, div); |
Kever Yang | dc850de | 2016-08-04 11:44:58 +0800 | [diff] [blame] | 754 | else |
| 755 | return DIV_TO_RATE(GPLL_HZ, div); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 756 | } |
| 757 | |
Michal Suchanek | 8478944 | 2022-08-21 09:17:24 +0200 | [diff] [blame] | 758 | static void rk3399_dwmmc_set_clk(struct rockchip_cru *cru, |
| 759 | unsigned int con, ulong set_rate) |
| 760 | { |
| 761 | /* Select clk_sdmmc source from GPLL by default */ |
| 762 | /* mmc clock defaulg div 2 internal, provide double in cru */ |
| 763 | int src_clk_div = DIV_ROUND_UP(GPLL_HZ / 2, set_rate); |
| 764 | |
| 765 | if (src_clk_div > 128) { |
| 766 | /* use 24MHz source for 400KHz clock */ |
| 767 | src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate); |
| 768 | assert(src_clk_div - 1 < 128); |
| 769 | rk_clrsetreg(&cru->clksel_con[con], |
| 770 | CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK, |
| 771 | CLK_EMMC_PLL_SEL_24M << CLK_EMMC_PLL_SHIFT | |
| 772 | (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT); |
| 773 | } else { |
| 774 | rk_clrsetreg(&cru->clksel_con[con], |
| 775 | CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK, |
| 776 | CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT | |
| 777 | (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT); |
| 778 | } |
| 779 | } |
| 780 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 781 | static ulong rk3399_mmc_set_clk(struct rockchip_cru *cru, |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 782 | ulong clk_id, ulong set_rate) |
| 783 | { |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 784 | switch (clk_id) { |
Michal Suchanek | 8478944 | 2022-08-21 09:17:24 +0200 | [diff] [blame] | 785 | case HCLK_SDIO: |
| 786 | case SCLK_SDIO: |
| 787 | rk3399_dwmmc_set_clk(cru, 15, set_rate); |
| 788 | break; |
Philipp Tomsich | 78a7314 | 2017-04-25 09:52:06 +0200 | [diff] [blame] | 789 | case HCLK_SDMMC: |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 790 | case SCLK_SDMMC: |
Michal Suchanek | 8478944 | 2022-08-21 09:17:24 +0200 | [diff] [blame] | 791 | rk3399_dwmmc_set_clk(cru, 16, set_rate); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 792 | break; |
Michal Suchanek | 8478944 | 2022-08-21 09:17:24 +0200 | [diff] [blame] | 793 | case SCLK_EMMC: { |
| 794 | int aclk_emmc = 198 * MHz; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 795 | /* Select aclk_emmc source from GPLL */ |
Michal Suchanek | 8478944 | 2022-08-21 09:17:24 +0200 | [diff] [blame] | 796 | int src_clk_div = DIV_ROUND_UP(GPLL_HZ, aclk_emmc); |
| 797 | |
Kever Yang | f20995b | 2017-07-27 12:54:02 +0800 | [diff] [blame] | 798 | assert(src_clk_div - 1 < 32); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 799 | |
| 800 | rk_clrsetreg(&cru->clksel_con[21], |
| 801 | ACLK_EMMC_PLL_SEL_MASK | ACLK_EMMC_DIV_CON_MASK, |
| 802 | ACLK_EMMC_PLL_SEL_GPLL << ACLK_EMMC_PLL_SEL_SHIFT | |
| 803 | (src_clk_div - 1) << ACLK_EMMC_DIV_CON_SHIFT); |
| 804 | |
| 805 | /* Select clk_emmc source from GPLL too */ |
Kever Yang | f20995b | 2017-07-27 12:54:02 +0800 | [diff] [blame] | 806 | src_clk_div = DIV_ROUND_UP(GPLL_HZ, set_rate); |
| 807 | assert(src_clk_div - 1 < 128); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 808 | |
| 809 | rk_clrsetreg(&cru->clksel_con[22], |
| 810 | CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK, |
| 811 | CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT | |
| 812 | (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT); |
| 813 | break; |
Michal Suchanek | 8478944 | 2022-08-21 09:17:24 +0200 | [diff] [blame] | 814 | } |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 815 | default: |
| 816 | return -EINVAL; |
| 817 | } |
| 818 | return rk3399_mmc_get_clk(cru, clk_id); |
| 819 | } |
| 820 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 821 | static ulong rk3399_gmac_set_clk(struct rockchip_cru *cru, ulong rate) |
Philipp Tomsich | f4ba6ed | 2018-01-08 13:11:01 +0100 | [diff] [blame] | 822 | { |
| 823 | ulong ret; |
| 824 | |
| 825 | /* |
| 826 | * The RGMII CLK can be derived either from an external "clkin" |
| 827 | * or can be generated from internally by a divider from SCLK_MAC. |
| 828 | */ |
| 829 | if (readl(&cru->clksel_con[19]) & BIT(4)) { |
| 830 | /* An external clock will always generate the right rate... */ |
| 831 | ret = rate; |
| 832 | } else { |
| 833 | /* |
| 834 | * No platform uses an internal clock to date. |
| 835 | * Implement this once it becomes necessary and print an error |
| 836 | * if someone tries to use it (while it remains unimplemented). |
| 837 | */ |
| 838 | pr_err("%s: internal clock is UNIMPLEMENTED\n", __func__); |
| 839 | ret = 0; |
| 840 | } |
| 841 | |
| 842 | return ret; |
| 843 | } |
| 844 | |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 845 | #define PMUSGRF_DDR_RGN_CON16 0xff330040 |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 846 | static ulong rk3399_ddr_set_clk(struct rockchip_cru *cru, |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 847 | ulong set_rate) |
| 848 | { |
| 849 | struct pll_div dpll_cfg; |
| 850 | |
| 851 | /* IC ECO bug, need to set this register */ |
| 852 | writel(0xc000c000, PMUSGRF_DDR_RGN_CON16); |
| 853 | |
| 854 | /* clk_ddrc == DPLL = 24MHz / refdiv * fbdiv / postdiv1 / postdiv2 */ |
| 855 | switch (set_rate) { |
Jagan Teki | 4833f32 | 2019-07-16 17:27:35 +0530 | [diff] [blame] | 856 | case 50 * MHz: |
| 857 | dpll_cfg = (struct pll_div) |
Xavier Drudis Ferran | 7d20cf9 | 2022-07-16 12:31:45 +0200 | [diff] [blame] | 858 | {.refdiv = 2, .fbdiv = 75, .postdiv1 = 3, .postdiv2 = 6}; |
Jagan Teki | 4833f32 | 2019-07-16 17:27:35 +0530 | [diff] [blame] | 859 | break; |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 860 | case 200 * MHz: |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 861 | dpll_cfg = (struct pll_div) |
| 862 | {.refdiv = 1, .fbdiv = 50, .postdiv1 = 6, .postdiv2 = 1}; |
| 863 | break; |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 864 | case 300 * MHz: |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 865 | dpll_cfg = (struct pll_div) |
| 866 | {.refdiv = 2, .fbdiv = 100, .postdiv1 = 4, .postdiv2 = 1}; |
| 867 | break; |
Jagan Teki | f0b0631 | 2019-07-16 17:27:36 +0530 | [diff] [blame] | 868 | case 400 * MHz: |
| 869 | dpll_cfg = (struct pll_div) |
| 870 | {.refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 1}; |
| 871 | break; |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 872 | case 666 * MHz: |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 873 | dpll_cfg = (struct pll_div) |
| 874 | {.refdiv = 2, .fbdiv = 111, .postdiv1 = 2, .postdiv2 = 1}; |
| 875 | break; |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 876 | case 800 * MHz: |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 877 | dpll_cfg = (struct pll_div) |
| 878 | {.refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1}; |
| 879 | break; |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 880 | case 933 * MHz: |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 881 | dpll_cfg = (struct pll_div) |
| 882 | {.refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1}; |
| 883 | break; |
| 884 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 885 | pr_err("Unsupported SDRAM frequency!,%ld\n", set_rate); |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 886 | } |
| 887 | rkclk_set_pll(&cru->dpll_con[0], &dpll_cfg); |
| 888 | |
| 889 | return set_rate; |
| 890 | } |
David Wu | f91b9b4 | 2017-09-20 14:38:58 +0800 | [diff] [blame] | 891 | |
Jack Mitchell | 4ef38ce | 2020-09-17 10:42:06 +0100 | [diff] [blame] | 892 | static ulong rk3399_alive_get_clk(struct rockchip_cru *cru) |
| 893 | { |
| 894 | u32 div, val; |
| 895 | |
| 896 | val = readl(&cru->clksel_con[57]); |
| 897 | div = (val & PCLK_ALIVE_DIV_CON_MASK) >> |
| 898 | PCLK_ALIVE_DIV_CON_SHIFT; |
| 899 | |
| 900 | return DIV_TO_RATE(GPLL_HZ, div); |
| 901 | } |
| 902 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 903 | static ulong rk3399_saradc_get_clk(struct rockchip_cru *cru) |
David Wu | f91b9b4 | 2017-09-20 14:38:58 +0800 | [diff] [blame] | 904 | { |
| 905 | u32 div, val; |
| 906 | |
| 907 | val = readl(&cru->clksel_con[26]); |
| 908 | div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT, |
| 909 | CLK_SARADC_DIV_CON_WIDTH); |
| 910 | |
| 911 | return DIV_TO_RATE(OSC_HZ, div); |
| 912 | } |
| 913 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 914 | static ulong rk3399_saradc_set_clk(struct rockchip_cru *cru, uint hz) |
David Wu | f91b9b4 | 2017-09-20 14:38:58 +0800 | [diff] [blame] | 915 | { |
| 916 | int src_clk_div; |
| 917 | |
| 918 | src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1; |
| 919 | assert(src_clk_div < 128); |
| 920 | |
| 921 | rk_clrsetreg(&cru->clksel_con[26], |
| 922 | CLK_SARADC_DIV_CON_MASK, |
| 923 | src_clk_div << CLK_SARADC_DIV_CON_SHIFT); |
| 924 | |
| 925 | return rk3399_saradc_get_clk(cru); |
| 926 | } |
| 927 | |
Jonas Karlman | 1f66283 | 2024-05-01 16:22:20 +0000 | [diff] [blame] | 928 | static ulong rk3399_pciephy_get_clk(struct rockchip_cru *cru) |
| 929 | { |
| 930 | if (readl(&cru->clksel_con[18]) & BIT(10)) |
| 931 | return 100 * MHz; |
| 932 | else |
| 933 | return OSC_HZ; |
| 934 | } |
| 935 | |
| 936 | static ulong rk3399_pciephy_set_clk(struct rockchip_cru *cru, uint hz) |
| 937 | { |
| 938 | if (hz == 100 * MHz) |
| 939 | rk_setreg(&cru->clksel_con[18], BIT(10)); |
| 940 | else if (hz == OSC_HZ) |
| 941 | rk_clrreg(&cru->clksel_con[18], BIT(10)); |
| 942 | else |
| 943 | return -EINVAL; |
| 944 | |
| 945 | return rk3399_pciephy_get_clk(cru); |
| 946 | } |
| 947 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 948 | static ulong rk3399_clk_get_rate(struct clk *clk) |
| 949 | { |
| 950 | struct rk3399_clk_priv *priv = dev_get_priv(clk->dev); |
| 951 | ulong rate = 0; |
| 952 | |
| 953 | switch (clk->id) { |
| 954 | case 0 ... 63: |
| 955 | return 0; |
Michal Suchanek | 8478944 | 2022-08-21 09:17:24 +0200 | [diff] [blame] | 956 | case HCLK_SDIO: |
| 957 | case SCLK_SDIO: |
Philipp Tomsich | 78a7314 | 2017-04-25 09:52:06 +0200 | [diff] [blame] | 958 | case HCLK_SDMMC: |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 959 | case SCLK_SDMMC: |
| 960 | case SCLK_EMMC: |
| 961 | rate = rk3399_mmc_get_clk(priv->cru, clk->id); |
| 962 | break; |
| 963 | case SCLK_I2C1: |
| 964 | case SCLK_I2C2: |
| 965 | case SCLK_I2C3: |
| 966 | case SCLK_I2C5: |
| 967 | case SCLK_I2C6: |
| 968 | case SCLK_I2C7: |
| 969 | rate = rk3399_i2c_get_clk(priv->cru, clk->id); |
| 970 | break; |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 971 | case SCLK_SPI0...SCLK_SPI5: |
| 972 | rate = rk3399_spi_get_clk(priv->cru, clk->id); |
| 973 | break; |
| 974 | case SCLK_UART0: |
Christoph Muellner | e5607a0 | 2019-05-07 10:58:44 +0200 | [diff] [blame] | 975 | case SCLK_UART1: |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 976 | case SCLK_UART2: |
Christoph Muellner | e5607a0 | 2019-05-07 10:58:44 +0200 | [diff] [blame] | 977 | case SCLK_UART3: |
Jonas Karlman | 696ab87 | 2024-05-01 16:22:21 +0000 | [diff] [blame] | 978 | case SCLK_USB3OTG0_REF: |
| 979 | case SCLK_USB3OTG1_REF: |
| 980 | return OSC_HZ; |
Philipp Tomsich | 10b594b | 2017-04-28 18:33:57 +0200 | [diff] [blame] | 981 | case PCLK_HDMI_CTRL: |
| 982 | break; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 983 | case DCLK_VOP0: |
| 984 | case DCLK_VOP1: |
| 985 | break; |
Philipp Tomsich | d10b45e | 2017-04-28 17:11:55 +0200 | [diff] [blame] | 986 | case PCLK_EFUSE1024NS: |
| 987 | break; |
David Wu | f91b9b4 | 2017-09-20 14:38:58 +0800 | [diff] [blame] | 988 | case SCLK_SARADC: |
| 989 | rate = rk3399_saradc_get_clk(priv->cru); |
| 990 | break; |
Jonas Karlman | 1f66283 | 2024-05-01 16:22:20 +0000 | [diff] [blame] | 991 | case SCLK_PCIEPHY_REF: |
| 992 | rate = rk3399_pciephy_get_clk(priv->cru); |
| 993 | break; |
Simon Glass | d27b317 | 2019-01-21 14:53:30 -0700 | [diff] [blame] | 994 | case ACLK_VIO: |
| 995 | case ACLK_HDCP: |
| 996 | case ACLK_GIC_PRE: |
| 997 | case PCLK_DDR: |
Jonas Karlman | 4d10698 | 2024-05-01 16:22:19 +0000 | [diff] [blame] | 998 | case ACLK_VDU: |
Simon Glass | d27b317 | 2019-01-21 14:53:30 -0700 | [diff] [blame] | 999 | break; |
Jack Mitchell | 4ef38ce | 2020-09-17 10:42:06 +0100 | [diff] [blame] | 1000 | case PCLK_ALIVE: |
| 1001 | case PCLK_WDT: |
| 1002 | rate = rk3399_alive_get_clk(priv->cru); |
| 1003 | break; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1004 | default: |
Simon Glass | d27b317 | 2019-01-21 14:53:30 -0700 | [diff] [blame] | 1005 | log_debug("Unknown clock %lu\n", clk->id); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1006 | return -ENOENT; |
| 1007 | } |
| 1008 | |
| 1009 | return rate; |
| 1010 | } |
| 1011 | |
| 1012 | static ulong rk3399_clk_set_rate(struct clk *clk, ulong rate) |
| 1013 | { |
| 1014 | struct rk3399_clk_priv *priv = dev_get_priv(clk->dev); |
| 1015 | ulong ret = 0; |
| 1016 | |
| 1017 | switch (clk->id) { |
| 1018 | case 0 ... 63: |
| 1019 | return 0; |
Philipp Tomsich | 2d20a63 | 2018-01-08 14:00:27 +0100 | [diff] [blame] | 1020 | |
| 1021 | case ACLK_PERIHP: |
| 1022 | case HCLK_PERIHP: |
| 1023 | case PCLK_PERIHP: |
| 1024 | return 0; |
| 1025 | |
| 1026 | case ACLK_PERILP0: |
| 1027 | case HCLK_PERILP0: |
| 1028 | case PCLK_PERILP0: |
| 1029 | return 0; |
| 1030 | |
| 1031 | case ACLK_CCI: |
| 1032 | return 0; |
| 1033 | |
| 1034 | case HCLK_PERILP1: |
| 1035 | case PCLK_PERILP1: |
| 1036 | return 0; |
| 1037 | |
Michal Suchanek | 8478944 | 2022-08-21 09:17:24 +0200 | [diff] [blame] | 1038 | case HCLK_SDIO: |
| 1039 | case SCLK_SDIO: |
Philipp Tomsich | 78a7314 | 2017-04-25 09:52:06 +0200 | [diff] [blame] | 1040 | case HCLK_SDMMC: |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1041 | case SCLK_SDMMC: |
| 1042 | case SCLK_EMMC: |
| 1043 | ret = rk3399_mmc_set_clk(priv->cru, clk->id, rate); |
| 1044 | break; |
Philipp Tomsich | bfa896c | 2017-03-24 19:24:25 +0100 | [diff] [blame] | 1045 | case SCLK_MAC: |
Philipp Tomsich | f4ba6ed | 2018-01-08 13:11:01 +0100 | [diff] [blame] | 1046 | ret = rk3399_gmac_set_clk(priv->cru, rate); |
Philipp Tomsich | bfa896c | 2017-03-24 19:24:25 +0100 | [diff] [blame] | 1047 | break; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1048 | case SCLK_I2C1: |
| 1049 | case SCLK_I2C2: |
| 1050 | case SCLK_I2C3: |
| 1051 | case SCLK_I2C5: |
| 1052 | case SCLK_I2C6: |
| 1053 | case SCLK_I2C7: |
| 1054 | ret = rk3399_i2c_set_clk(priv->cru, clk->id, rate); |
| 1055 | break; |
Philipp Tomsich | c31ee92 | 2017-04-20 22:05:49 +0200 | [diff] [blame] | 1056 | case SCLK_SPI0...SCLK_SPI5: |
| 1057 | ret = rk3399_spi_set_clk(priv->cru, clk->id, rate); |
| 1058 | break; |
Philipp Tomsich | 10b594b | 2017-04-28 18:33:57 +0200 | [diff] [blame] | 1059 | case PCLK_HDMI_CTRL: |
| 1060 | case PCLK_VIO_GRF: |
| 1061 | /* the PCLK gates for video are enabled by default */ |
| 1062 | break; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1063 | case DCLK_VOP0: |
| 1064 | case DCLK_VOP1: |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1065 | ret = rk3399_vop_set_clk(priv->cru, clk->id, rate); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1066 | break; |
Jagan Teki | 99f0f82 | 2020-04-02 17:11:21 +0530 | [diff] [blame] | 1067 | case ACLK_VOP1: |
| 1068 | case HCLK_VOP1: |
Jagan Teki | 3f26bce | 2020-04-28 15:30:16 +0530 | [diff] [blame] | 1069 | case HCLK_SD: |
Jagan Teki | 4fb2c6d | 2020-05-26 11:32:06 +0800 | [diff] [blame] | 1070 | case SCLK_UPHY0_TCPDCORE: |
| 1071 | case SCLK_UPHY1_TCPDCORE: |
Jagan Teki | 99f0f82 | 2020-04-02 17:11:21 +0530 | [diff] [blame] | 1072 | /** |
| 1073 | * assigned-clocks handling won't require for vopl, so |
| 1074 | * return 0 to satisfy clk_set_defaults during device probe. |
| 1075 | */ |
| 1076 | return 0; |
Jonas Karlman | 91e4c15 | 2024-05-01 16:22:18 +0000 | [diff] [blame] | 1077 | case SCLK_DDRC: |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1078 | ret = rk3399_ddr_set_clk(priv->cru, rate); |
| 1079 | break; |
Philipp Tomsich | d10b45e | 2017-04-28 17:11:55 +0200 | [diff] [blame] | 1080 | case PCLK_EFUSE1024NS: |
| 1081 | break; |
David Wu | f91b9b4 | 2017-09-20 14:38:58 +0800 | [diff] [blame] | 1082 | case SCLK_SARADC: |
| 1083 | ret = rk3399_saradc_set_clk(priv->cru, rate); |
| 1084 | break; |
Jonas Karlman | 1f66283 | 2024-05-01 16:22:20 +0000 | [diff] [blame] | 1085 | case SCLK_PCIEPHY_REF: |
| 1086 | ret = rk3399_pciephy_set_clk(priv->cru, rate); |
| 1087 | break; |
Simon Glass | d27b317 | 2019-01-21 14:53:30 -0700 | [diff] [blame] | 1088 | case ACLK_VIO: |
| 1089 | case ACLK_HDCP: |
| 1090 | case ACLK_GIC_PRE: |
| 1091 | case PCLK_DDR: |
Jonas Karlman | 4d10698 | 2024-05-01 16:22:19 +0000 | [diff] [blame] | 1092 | case ACLK_VDU: |
Simon Glass | d27b317 | 2019-01-21 14:53:30 -0700 | [diff] [blame] | 1093 | return 0; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1094 | default: |
Simon Glass | d27b317 | 2019-01-21 14:53:30 -0700 | [diff] [blame] | 1095 | log_debug("Unknown clock %lu\n", clk->id); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1096 | return -ENOENT; |
| 1097 | } |
| 1098 | |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 1102 | static int __maybe_unused rk3399_gmac_set_parent(struct clk *clk, |
| 1103 | struct clk *parent) |
Philipp Tomsich | f4ba6ed | 2018-01-08 13:11:01 +0100 | [diff] [blame] | 1104 | { |
| 1105 | struct rk3399_clk_priv *priv = dev_get_priv(clk->dev); |
| 1106 | const char *clock_output_name; |
| 1107 | int ret; |
| 1108 | |
| 1109 | /* |
| 1110 | * If the requested parent is in the same clock-controller and |
| 1111 | * the id is SCLK_MAC ("clk_gmac"), switch to the internal clock. |
| 1112 | */ |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 1113 | if (parent->dev == clk->dev && parent->id == SCLK_MAC) { |
Philipp Tomsich | f4ba6ed | 2018-01-08 13:11:01 +0100 | [diff] [blame] | 1114 | debug("%s: switching RGMII to SCLK_MAC\n", __func__); |
| 1115 | rk_clrreg(&priv->cru->clksel_con[19], BIT(4)); |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * Otherwise, we need to check the clock-output-names of the |
| 1121 | * requested parent to see if the requested id is "clkin_gmac". |
| 1122 | */ |
| 1123 | ret = dev_read_string_index(parent->dev, "clock-output-names", |
| 1124 | parent->id, &clock_output_name); |
| 1125 | if (ret < 0) |
| 1126 | return -ENODATA; |
| 1127 | |
| 1128 | /* If this is "clkin_gmac", switch to the external clock input */ |
| 1129 | if (!strcmp(clock_output_name, "clkin_gmac")) { |
| 1130 | debug("%s: switching RGMII to CLKIN\n", __func__); |
| 1131 | rk_setreg(&priv->cru->clksel_con[19], BIT(4)); |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
Jonas Karlman | 1f66283 | 2024-05-01 16:22:20 +0000 | [diff] [blame] | 1135 | return -EINVAL; |
| 1136 | } |
| 1137 | |
| 1138 | static int __maybe_unused rk3399_pciephy_set_parent(struct clk *clk, |
| 1139 | struct clk *parent) |
| 1140 | { |
| 1141 | struct rk3399_clk_priv *priv = dev_get_priv(clk->dev); |
| 1142 | const char *clock_output_name; |
| 1143 | int ret; |
| 1144 | |
| 1145 | if (parent->dev == clk->dev && parent->id == SCLK_PCIEPHY_REF100M) { |
| 1146 | rk_setreg(&priv->cru->clksel_con[18], BIT(10)); |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | ret = dev_read_string_index(parent->dev, "clock-output-names", |
| 1151 | parent->id, &clock_output_name); |
| 1152 | if (ret < 0) |
| 1153 | return -ENODATA; |
| 1154 | |
| 1155 | if (!strcmp(clock_output_name, "xin24m")) { |
| 1156 | rk_clrreg(&priv->cru->clksel_con[18], BIT(10)); |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
Philipp Tomsich | f4ba6ed | 2018-01-08 13:11:01 +0100 | [diff] [blame] | 1160 | return -EINVAL; |
| 1161 | } |
| 1162 | |
Jagan Teki | bef02a3 | 2019-07-15 23:51:10 +0530 | [diff] [blame] | 1163 | static int __maybe_unused rk3399_clk_set_parent(struct clk *clk, |
| 1164 | struct clk *parent) |
Philipp Tomsich | f4ba6ed | 2018-01-08 13:11:01 +0100 | [diff] [blame] | 1165 | { |
| 1166 | switch (clk->id) { |
| 1167 | case SCLK_RMII_SRC: |
| 1168 | return rk3399_gmac_set_parent(clk, parent); |
Jonas Karlman | 1f66283 | 2024-05-01 16:22:20 +0000 | [diff] [blame] | 1169 | case SCLK_PCIEPHY_REF: |
| 1170 | return rk3399_pciephy_set_parent(clk, parent); |
Philipp Tomsich | f4ba6ed | 2018-01-08 13:11:01 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | debug("%s: unsupported clk %ld\n", __func__, clk->id); |
| 1174 | return -ENOENT; |
| 1175 | } |
| 1176 | |
Jagan Teki | 6c7f14a | 2020-05-09 22:26:19 +0530 | [diff] [blame] | 1177 | static int rk3399_clk_enable(struct clk *clk) |
| 1178 | { |
| 1179 | struct rk3399_clk_priv *priv = dev_get_priv(clk->dev); |
| 1180 | |
| 1181 | switch (clk->id) { |
| 1182 | case SCLK_MAC: |
| 1183 | rk_clrreg(&priv->cru->clkgate_con[5], BIT(5)); |
| 1184 | break; |
| 1185 | case SCLK_MAC_RX: |
| 1186 | rk_clrreg(&priv->cru->clkgate_con[5], BIT(8)); |
| 1187 | break; |
| 1188 | case SCLK_MAC_TX: |
| 1189 | rk_clrreg(&priv->cru->clkgate_con[5], BIT(9)); |
| 1190 | break; |
| 1191 | case SCLK_MACREF: |
| 1192 | rk_clrreg(&priv->cru->clkgate_con[5], BIT(7)); |
| 1193 | break; |
| 1194 | case SCLK_MACREF_OUT: |
| 1195 | rk_clrreg(&priv->cru->clkgate_con[5], BIT(6)); |
| 1196 | break; |
Jagan Teki | a591537 | 2020-05-26 11:32:05 +0800 | [diff] [blame] | 1197 | case SCLK_USB2PHY0_REF: |
| 1198 | rk_clrreg(&priv->cru->clkgate_con[6], BIT(5)); |
| 1199 | break; |
| 1200 | case SCLK_USB2PHY1_REF: |
| 1201 | rk_clrreg(&priv->cru->clkgate_con[6], BIT(6)); |
| 1202 | break; |
Jagan Teki | 6c7f14a | 2020-05-09 22:26:19 +0530 | [diff] [blame] | 1203 | case ACLK_GMAC: |
| 1204 | rk_clrreg(&priv->cru->clkgate_con[32], BIT(0)); |
| 1205 | break; |
| 1206 | case PCLK_GMAC: |
| 1207 | rk_clrreg(&priv->cru->clkgate_con[32], BIT(2)); |
| 1208 | break; |
| 1209 | case SCLK_USB3OTG0_REF: |
| 1210 | rk_clrreg(&priv->cru->clkgate_con[12], BIT(1)); |
| 1211 | break; |
| 1212 | case SCLK_USB3OTG1_REF: |
| 1213 | rk_clrreg(&priv->cru->clkgate_con[12], BIT(2)); |
| 1214 | break; |
| 1215 | case SCLK_USB3OTG0_SUSPEND: |
| 1216 | rk_clrreg(&priv->cru->clkgate_con[12], BIT(3)); |
| 1217 | break; |
| 1218 | case SCLK_USB3OTG1_SUSPEND: |
| 1219 | rk_clrreg(&priv->cru->clkgate_con[12], BIT(4)); |
| 1220 | break; |
| 1221 | case ACLK_USB3OTG0: |
| 1222 | rk_clrreg(&priv->cru->clkgate_con[30], BIT(1)); |
| 1223 | break; |
| 1224 | case ACLK_USB3OTG1: |
| 1225 | rk_clrreg(&priv->cru->clkgate_con[30], BIT(2)); |
| 1226 | break; |
| 1227 | case ACLK_USB3_RKSOC_AXI_PERF: |
| 1228 | rk_clrreg(&priv->cru->clkgate_con[30], BIT(3)); |
| 1229 | break; |
| 1230 | case ACLK_USB3: |
| 1231 | rk_clrreg(&priv->cru->clkgate_con[12], BIT(0)); |
| 1232 | break; |
| 1233 | case ACLK_USB3_GRF: |
| 1234 | rk_clrreg(&priv->cru->clkgate_con[30], BIT(4)); |
| 1235 | break; |
| 1236 | case HCLK_HOST0: |
| 1237 | rk_clrreg(&priv->cru->clksel_con[20], BIT(5)); |
| 1238 | break; |
| 1239 | case HCLK_HOST0_ARB: |
| 1240 | rk_clrreg(&priv->cru->clksel_con[20], BIT(6)); |
| 1241 | break; |
| 1242 | case HCLK_HOST1: |
| 1243 | rk_clrreg(&priv->cru->clksel_con[20], BIT(7)); |
| 1244 | break; |
| 1245 | case HCLK_HOST1_ARB: |
| 1246 | rk_clrreg(&priv->cru->clksel_con[20], BIT(8)); |
| 1247 | break; |
Jagan Teki | 055c218 | 2020-05-26 11:32:07 +0800 | [diff] [blame] | 1248 | case SCLK_UPHY0_TCPDPHY_REF: |
| 1249 | rk_clrreg(&priv->cru->clkgate_con[13], BIT(4)); |
| 1250 | break; |
| 1251 | case SCLK_UPHY0_TCPDCORE: |
| 1252 | rk_clrreg(&priv->cru->clkgate_con[13], BIT(5)); |
| 1253 | break; |
| 1254 | case SCLK_UPHY1_TCPDPHY_REF: |
| 1255 | rk_clrreg(&priv->cru->clkgate_con[13], BIT(6)); |
| 1256 | break; |
| 1257 | case SCLK_UPHY1_TCPDCORE: |
| 1258 | rk_clrreg(&priv->cru->clkgate_con[13], BIT(7)); |
| 1259 | break; |
Jagan Teki | 70c54ee | 2020-05-09 22:26:20 +0530 | [diff] [blame] | 1260 | case SCLK_PCIEPHY_REF: |
Jonas Karlman | 1f66283 | 2024-05-01 16:22:20 +0000 | [diff] [blame] | 1261 | if (readl(&priv->cru->clksel_con[18]) & BIT(10)) |
| 1262 | rk_clrreg(&priv->cru->clkgate_con[12], BIT(6)); |
Jagan Teki | 70c54ee | 2020-05-09 22:26:20 +0530 | [diff] [blame] | 1263 | break; |
Jagan Teki | 6c7f14a | 2020-05-09 22:26:19 +0530 | [diff] [blame] | 1264 | default: |
| 1265 | debug("%s: unsupported clk %ld\n", __func__, clk->id); |
| 1266 | return -ENOENT; |
| 1267 | } |
| 1268 | |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | static int rk3399_clk_disable(struct clk *clk) |
| 1273 | { |
| 1274 | struct rk3399_clk_priv *priv = dev_get_priv(clk->dev); |
| 1275 | |
| 1276 | switch (clk->id) { |
| 1277 | case SCLK_MAC: |
| 1278 | rk_setreg(&priv->cru->clkgate_con[5], BIT(5)); |
| 1279 | break; |
| 1280 | case SCLK_MAC_RX: |
| 1281 | rk_setreg(&priv->cru->clkgate_con[5], BIT(8)); |
| 1282 | break; |
| 1283 | case SCLK_MAC_TX: |
| 1284 | rk_setreg(&priv->cru->clkgate_con[5], BIT(9)); |
| 1285 | break; |
| 1286 | case SCLK_MACREF: |
| 1287 | rk_setreg(&priv->cru->clkgate_con[5], BIT(7)); |
| 1288 | break; |
| 1289 | case SCLK_MACREF_OUT: |
| 1290 | rk_setreg(&priv->cru->clkgate_con[5], BIT(6)); |
| 1291 | break; |
Jagan Teki | a591537 | 2020-05-26 11:32:05 +0800 | [diff] [blame] | 1292 | case SCLK_USB2PHY0_REF: |
| 1293 | rk_setreg(&priv->cru->clkgate_con[6], BIT(5)); |
| 1294 | break; |
| 1295 | case SCLK_USB2PHY1_REF: |
| 1296 | rk_setreg(&priv->cru->clkgate_con[6], BIT(6)); |
| 1297 | break; |
Jagan Teki | 6c7f14a | 2020-05-09 22:26:19 +0530 | [diff] [blame] | 1298 | case ACLK_GMAC: |
| 1299 | rk_setreg(&priv->cru->clkgate_con[32], BIT(0)); |
| 1300 | break; |
| 1301 | case PCLK_GMAC: |
| 1302 | rk_setreg(&priv->cru->clkgate_con[32], BIT(2)); |
| 1303 | break; |
| 1304 | case SCLK_USB3OTG0_REF: |
| 1305 | rk_setreg(&priv->cru->clkgate_con[12], BIT(1)); |
| 1306 | break; |
| 1307 | case SCLK_USB3OTG1_REF: |
| 1308 | rk_setreg(&priv->cru->clkgate_con[12], BIT(2)); |
| 1309 | break; |
| 1310 | case SCLK_USB3OTG0_SUSPEND: |
| 1311 | rk_setreg(&priv->cru->clkgate_con[12], BIT(3)); |
| 1312 | break; |
| 1313 | case SCLK_USB3OTG1_SUSPEND: |
| 1314 | rk_setreg(&priv->cru->clkgate_con[12], BIT(4)); |
| 1315 | break; |
| 1316 | case ACLK_USB3OTG0: |
| 1317 | rk_setreg(&priv->cru->clkgate_con[30], BIT(1)); |
| 1318 | break; |
| 1319 | case ACLK_USB3OTG1: |
| 1320 | rk_setreg(&priv->cru->clkgate_con[30], BIT(2)); |
| 1321 | break; |
| 1322 | case ACLK_USB3_RKSOC_AXI_PERF: |
| 1323 | rk_setreg(&priv->cru->clkgate_con[30], BIT(3)); |
| 1324 | break; |
| 1325 | case ACLK_USB3: |
| 1326 | rk_setreg(&priv->cru->clkgate_con[12], BIT(0)); |
| 1327 | break; |
| 1328 | case ACLK_USB3_GRF: |
| 1329 | rk_setreg(&priv->cru->clkgate_con[30], BIT(4)); |
| 1330 | break; |
| 1331 | case HCLK_HOST0: |
| 1332 | rk_setreg(&priv->cru->clksel_con[20], BIT(5)); |
| 1333 | break; |
| 1334 | case HCLK_HOST0_ARB: |
| 1335 | rk_setreg(&priv->cru->clksel_con[20], BIT(6)); |
| 1336 | break; |
| 1337 | case HCLK_HOST1: |
| 1338 | rk_setreg(&priv->cru->clksel_con[20], BIT(7)); |
| 1339 | break; |
| 1340 | case HCLK_HOST1_ARB: |
| 1341 | rk_setreg(&priv->cru->clksel_con[20], BIT(8)); |
| 1342 | break; |
Jagan Teki | 055c218 | 2020-05-26 11:32:07 +0800 | [diff] [blame] | 1343 | case SCLK_UPHY0_TCPDPHY_REF: |
| 1344 | rk_setreg(&priv->cru->clkgate_con[13], BIT(4)); |
| 1345 | break; |
| 1346 | case SCLK_UPHY0_TCPDCORE: |
| 1347 | rk_setreg(&priv->cru->clkgate_con[13], BIT(5)); |
| 1348 | break; |
| 1349 | case SCLK_UPHY1_TCPDPHY_REF: |
| 1350 | rk_setreg(&priv->cru->clkgate_con[13], BIT(6)); |
| 1351 | break; |
| 1352 | case SCLK_UPHY1_TCPDCORE: |
| 1353 | rk_setreg(&priv->cru->clkgate_con[13], BIT(7)); |
| 1354 | break; |
Jagan Teki | 70c54ee | 2020-05-09 22:26:20 +0530 | [diff] [blame] | 1355 | case SCLK_PCIEPHY_REF: |
Jonas Karlman | 1f66283 | 2024-05-01 16:22:20 +0000 | [diff] [blame] | 1356 | if (readl(&priv->cru->clksel_con[18]) & BIT(10)) |
| 1357 | rk_setreg(&priv->cru->clkgate_con[12], BIT(6)); |
Jagan Teki | 70c54ee | 2020-05-09 22:26:20 +0530 | [diff] [blame] | 1358 | break; |
Jagan Teki | 6c7f14a | 2020-05-09 22:26:19 +0530 | [diff] [blame] | 1359 | default: |
| 1360 | debug("%s: unsupported clk %ld\n", __func__, clk->id); |
| 1361 | return -ENOENT; |
| 1362 | } |
| 1363 | |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1367 | static struct clk_ops rk3399_clk_ops = { |
| 1368 | .get_rate = rk3399_clk_get_rate, |
| 1369 | .set_rate = rk3399_clk_set_rate, |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 1370 | #if CONFIG_IS_ENABLED(OF_REAL) |
Philipp Tomsich | f4ba6ed | 2018-01-08 13:11:01 +0100 | [diff] [blame] | 1371 | .set_parent = rk3399_clk_set_parent, |
Philipp Tomsich | 6dd2fb4 | 2018-01-25 15:27:10 +0100 | [diff] [blame] | 1372 | #endif |
Jagan Teki | 6c7f14a | 2020-05-09 22:26:19 +0530 | [diff] [blame] | 1373 | .enable = rk3399_clk_enable, |
| 1374 | .disable = rk3399_clk_disable, |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1375 | }; |
| 1376 | |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 1377 | static void rkclk_init(struct rockchip_cru *cru) |
Kever Yang | 05a14b0 | 2017-10-12 15:27:29 +0800 | [diff] [blame] | 1378 | { |
| 1379 | u32 aclk_div; |
| 1380 | u32 hclk_div; |
| 1381 | u32 pclk_div; |
| 1382 | |
Christoph Muellner | 25c7ba9 | 2018-11-30 20:32:48 +0100 | [diff] [blame] | 1383 | rk3399_configure_cpu_l(cru, APLL_L_600_MHZ); |
| 1384 | rk3399_configure_cpu_b(cru, APLL_B_600_MHZ); |
Kever Yang | 05a14b0 | 2017-10-12 15:27:29 +0800 | [diff] [blame] | 1385 | /* |
| 1386 | * some cru registers changed by bootrom, we'd better reset them to |
| 1387 | * reset/default values described in TRM to avoid confusion in kernel. |
| 1388 | * Please consider these three lines as a fix of bootrom bug. |
| 1389 | */ |
| 1390 | rk_clrsetreg(&cru->clksel_con[12], 0xffff, 0x4101); |
| 1391 | rk_clrsetreg(&cru->clksel_con[19], 0xffff, 0x033f); |
| 1392 | rk_clrsetreg(&cru->clksel_con[56], 0x0003, 0x0003); |
| 1393 | |
| 1394 | /* configure gpll cpll */ |
| 1395 | rkclk_set_pll(&cru->gpll_con[0], &gpll_init_cfg); |
| 1396 | rkclk_set_pll(&cru->cpll_con[0], &cpll_init_cfg); |
| 1397 | |
| 1398 | /* configure perihp aclk, hclk, pclk */ |
| 1399 | aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1; |
| 1400 | assert((aclk_div + 1) * PERIHP_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 1401 | |
| 1402 | hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1; |
| 1403 | assert((hclk_div + 1) * PERIHP_HCLK_HZ == |
| 1404 | PERIHP_ACLK_HZ && (hclk_div < 0x4)); |
| 1405 | |
| 1406 | pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1; |
| 1407 | assert((pclk_div + 1) * PERIHP_PCLK_HZ == |
| 1408 | PERIHP_ACLK_HZ && (pclk_div < 0x7)); |
| 1409 | |
| 1410 | rk_clrsetreg(&cru->clksel_con[14], |
| 1411 | PCLK_PERIHP_DIV_CON_MASK | HCLK_PERIHP_DIV_CON_MASK | |
| 1412 | ACLK_PERIHP_PLL_SEL_MASK | ACLK_PERIHP_DIV_CON_MASK, |
| 1413 | pclk_div << PCLK_PERIHP_DIV_CON_SHIFT | |
| 1414 | hclk_div << HCLK_PERIHP_DIV_CON_SHIFT | |
| 1415 | ACLK_PERIHP_PLL_SEL_GPLL << ACLK_PERIHP_PLL_SEL_SHIFT | |
| 1416 | aclk_div << ACLK_PERIHP_DIV_CON_SHIFT); |
| 1417 | |
| 1418 | /* configure perilp0 aclk, hclk, pclk */ |
| 1419 | aclk_div = GPLL_HZ / PERILP0_ACLK_HZ - 1; |
| 1420 | assert((aclk_div + 1) * PERILP0_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 1421 | |
| 1422 | hclk_div = PERILP0_ACLK_HZ / PERILP0_HCLK_HZ - 1; |
| 1423 | assert((hclk_div + 1) * PERILP0_HCLK_HZ == |
| 1424 | PERILP0_ACLK_HZ && (hclk_div < 0x4)); |
| 1425 | |
| 1426 | pclk_div = PERILP0_ACLK_HZ / PERILP0_PCLK_HZ - 1; |
| 1427 | assert((pclk_div + 1) * PERILP0_PCLK_HZ == |
| 1428 | PERILP0_ACLK_HZ && (pclk_div < 0x7)); |
| 1429 | |
| 1430 | rk_clrsetreg(&cru->clksel_con[23], |
| 1431 | PCLK_PERILP0_DIV_CON_MASK | HCLK_PERILP0_DIV_CON_MASK | |
| 1432 | ACLK_PERILP0_PLL_SEL_MASK | ACLK_PERILP0_DIV_CON_MASK, |
| 1433 | pclk_div << PCLK_PERILP0_DIV_CON_SHIFT | |
| 1434 | hclk_div << HCLK_PERILP0_DIV_CON_SHIFT | |
| 1435 | ACLK_PERILP0_PLL_SEL_GPLL << ACLK_PERILP0_PLL_SEL_SHIFT | |
| 1436 | aclk_div << ACLK_PERILP0_DIV_CON_SHIFT); |
| 1437 | |
| 1438 | /* perilp1 hclk select gpll as source */ |
| 1439 | hclk_div = GPLL_HZ / PERILP1_HCLK_HZ - 1; |
| 1440 | assert((hclk_div + 1) * PERILP1_HCLK_HZ == |
| 1441 | GPLL_HZ && (hclk_div < 0x1f)); |
| 1442 | |
| 1443 | pclk_div = PERILP1_HCLK_HZ / PERILP1_HCLK_HZ - 1; |
| 1444 | assert((pclk_div + 1) * PERILP1_HCLK_HZ == |
| 1445 | PERILP1_HCLK_HZ && (hclk_div < 0x7)); |
| 1446 | |
| 1447 | rk_clrsetreg(&cru->clksel_con[25], |
| 1448 | PCLK_PERILP1_DIV_CON_MASK | HCLK_PERILP1_DIV_CON_MASK | |
| 1449 | HCLK_PERILP1_PLL_SEL_MASK, |
| 1450 | pclk_div << PCLK_PERILP1_DIV_CON_SHIFT | |
| 1451 | hclk_div << HCLK_PERILP1_DIV_CON_SHIFT | |
| 1452 | HCLK_PERILP1_PLL_SEL_GPLL << HCLK_PERILP1_PLL_SEL_SHIFT); |
| 1453 | } |
Kever Yang | 05a14b0 | 2017-10-12 15:27:29 +0800 | [diff] [blame] | 1454 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1455 | static int rk3399_clk_probe(struct udevice *dev) |
| 1456 | { |
| 1457 | struct rk3399_clk_priv *priv = dev_get_priv(dev); |
Alper Nebi Yasak | 66327ce | 2020-10-28 00:15:10 +0300 | [diff] [blame] | 1458 | bool init_clocks = false; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1459 | |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1460 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1461 | struct rk3399_clk_plat *plat = dev_get_plat(dev); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1462 | |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 1463 | priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]); |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1464 | #endif |
Alper Nebi Yasak | 66327ce | 2020-10-28 00:15:10 +0300 | [diff] [blame] | 1465 | |
| 1466 | #if defined(CONFIG_SPL_BUILD) |
| 1467 | init_clocks = true; |
| 1468 | #elif CONFIG_IS_ENABLED(HANDOFF) |
| 1469 | if (!(gd->flags & GD_FLG_RELOC)) { |
| 1470 | if (!(gd->spl_handoff)) |
| 1471 | init_clocks = true; |
| 1472 | } |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1473 | #endif |
Alper Nebi Yasak | 66327ce | 2020-10-28 00:15:10 +0300 | [diff] [blame] | 1474 | |
| 1475 | if (init_clocks) |
| 1476 | rkclk_init(priv->cru); |
| 1477 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1478 | return 0; |
| 1479 | } |
| 1480 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1481 | static int rk3399_clk_of_to_plat(struct udevice *dev) |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1482 | { |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 1483 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
| 1484 | struct rk3399_clk_priv *priv = dev_get_priv(dev); |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1485 | |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 1486 | priv->cru = dev_read_addr_ptr(dev); |
| 1487 | } |
| 1488 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | static int rk3399_clk_bind(struct udevice *dev) |
| 1493 | { |
| 1494 | int ret; |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 1495 | struct udevice *sys_child; |
| 1496 | struct sysreset_reg *priv; |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1497 | |
| 1498 | /* 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] | 1499 | ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset", |
| 1500 | &sys_child); |
| 1501 | if (ret) { |
| 1502 | debug("Warning: No sysreset driver: ret=%d\n", ret); |
| 1503 | } else { |
| 1504 | priv = malloc(sizeof(struct sysreset_reg)); |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 1505 | priv->glb_srst_fst_value = offsetof(struct rockchip_cru, |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 1506 | glb_srst_fst_value); |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 1507 | priv->glb_srst_snd_value = offsetof(struct rockchip_cru, |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 1508 | glb_srst_snd_value); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1509 | dev_set_priv(sys_child, priv); |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 1510 | } |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1511 | |
Heiko Stuebner | 416f8d3 | 2019-11-09 00:06:30 +0100 | [diff] [blame] | 1512 | #if CONFIG_IS_ENABLED(RESET_ROCKCHIP) |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 1513 | ret = offsetof(struct rockchip_cru, softrst_con[0]); |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 1514 | ret = rockchip_reset_bind(dev, ret, 21); |
| 1515 | if (ret) |
Eugen Hristev | f179826 | 2023-04-11 10:17:56 +0300 | [diff] [blame] | 1516 | debug("Warning: software reset driver bind failed\n"); |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 1517 | #endif |
| 1518 | |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1519 | return 0; |
| 1520 | } |
| 1521 | |
| 1522 | static const struct udevice_id rk3399_clk_ids[] = { |
| 1523 | { .compatible = "rockchip,rk3399-cru" }, |
| 1524 | { } |
| 1525 | }; |
| 1526 | |
| 1527 | U_BOOT_DRIVER(clk_rk3399) = { |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1528 | .name = "rockchip_rk3399_cru", |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1529 | .id = UCLASS_CLK, |
| 1530 | .of_match = rk3399_clk_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1531 | .priv_auto = sizeof(struct rk3399_clk_priv), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1532 | .of_to_plat = rk3399_clk_of_to_plat, |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1533 | .ops = &rk3399_clk_ops, |
| 1534 | .bind = rk3399_clk_bind, |
| 1535 | .probe = rk3399_clk_probe, |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1536 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1537 | .plat_auto = sizeof(struct rk3399_clk_plat), |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1538 | #endif |
Kever Yang | ca19eac | 2016-07-29 10:35:25 +0800 | [diff] [blame] | 1539 | }; |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1540 | |
| 1541 | static ulong rk3399_i2c_get_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id) |
| 1542 | { |
| 1543 | u32 div, con; |
| 1544 | |
| 1545 | switch (clk_id) { |
| 1546 | case SCLK_I2C0_PMU: |
| 1547 | con = readl(&pmucru->pmucru_clksel[2]); |
| 1548 | div = I2C_CLK_DIV_VALUE(con, 0); |
| 1549 | break; |
| 1550 | case SCLK_I2C4_PMU: |
| 1551 | con = readl(&pmucru->pmucru_clksel[3]); |
| 1552 | div = I2C_CLK_DIV_VALUE(con, 4); |
| 1553 | break; |
| 1554 | case SCLK_I2C8_PMU: |
| 1555 | con = readl(&pmucru->pmucru_clksel[2]); |
| 1556 | div = I2C_CLK_DIV_VALUE(con, 8); |
| 1557 | break; |
| 1558 | default: |
| 1559 | printf("do not support this i2c bus\n"); |
| 1560 | return -EINVAL; |
| 1561 | } |
| 1562 | |
| 1563 | return DIV_TO_RATE(PPLL_HZ, div); |
| 1564 | } |
| 1565 | |
| 1566 | static ulong rk3399_i2c_set_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id, |
| 1567 | uint hz) |
| 1568 | { |
| 1569 | int src_clk_div; |
| 1570 | |
| 1571 | src_clk_div = PPLL_HZ / hz; |
| 1572 | assert(src_clk_div - 1 < 127); |
| 1573 | |
| 1574 | switch (clk_id) { |
| 1575 | case SCLK_I2C0_PMU: |
| 1576 | rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(0), |
| 1577 | I2C_PMUCLK_REG_VALUE(0, src_clk_div)); |
| 1578 | break; |
| 1579 | case SCLK_I2C4_PMU: |
| 1580 | rk_clrsetreg(&pmucru->pmucru_clksel[3], I2C_PMUCLK_REG_MASK(4), |
| 1581 | I2C_PMUCLK_REG_VALUE(4, src_clk_div)); |
| 1582 | break; |
| 1583 | case SCLK_I2C8_PMU: |
| 1584 | rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(8), |
| 1585 | I2C_PMUCLK_REG_VALUE(8, src_clk_div)); |
| 1586 | break; |
| 1587 | default: |
| 1588 | printf("do not support this i2c bus\n"); |
| 1589 | return -EINVAL; |
| 1590 | } |
| 1591 | |
| 1592 | return DIV_TO_RATE(PPLL_HZ, src_clk_div); |
| 1593 | } |
| 1594 | |
| 1595 | static ulong rk3399_pwm_get_clk(struct rk3399_pmucru *pmucru) |
| 1596 | { |
| 1597 | u32 div, con; |
| 1598 | |
| 1599 | /* PWM closk rate is same as pclk_pmu */ |
| 1600 | con = readl(&pmucru->pmucru_clksel[0]); |
| 1601 | div = con & PMU_PCLK_DIV_CON_MASK; |
| 1602 | |
| 1603 | return DIV_TO_RATE(PPLL_HZ, div); |
| 1604 | } |
| 1605 | |
| 1606 | static ulong rk3399_pmuclk_get_rate(struct clk *clk) |
| 1607 | { |
| 1608 | struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev); |
| 1609 | ulong rate = 0; |
| 1610 | |
| 1611 | switch (clk->id) { |
Philipp Tomsich | 932908c | 2018-02-23 17:36:41 +0100 | [diff] [blame] | 1612 | case PLL_PPLL: |
| 1613 | return PPLL_HZ; |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1614 | case PCLK_RKPWM_PMU: |
Jack Mitchell | 4ef38ce | 2020-09-17 10:42:06 +0100 | [diff] [blame] | 1615 | case PCLK_WDT_M0_PMU: |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1616 | rate = rk3399_pwm_get_clk(priv->pmucru); |
| 1617 | break; |
| 1618 | case SCLK_I2C0_PMU: |
| 1619 | case SCLK_I2C4_PMU: |
| 1620 | case SCLK_I2C8_PMU: |
| 1621 | rate = rk3399_i2c_get_pmuclk(priv->pmucru, clk->id); |
| 1622 | break; |
| 1623 | default: |
| 1624 | return -ENOENT; |
| 1625 | } |
| 1626 | |
| 1627 | return rate; |
| 1628 | } |
| 1629 | |
| 1630 | static ulong rk3399_pmuclk_set_rate(struct clk *clk, ulong rate) |
| 1631 | { |
| 1632 | struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev); |
| 1633 | ulong ret = 0; |
| 1634 | |
| 1635 | switch (clk->id) { |
Philipp Tomsich | 932908c | 2018-02-23 17:36:41 +0100 | [diff] [blame] | 1636 | case PLL_PPLL: |
| 1637 | /* |
| 1638 | * This has already been set up and we don't want/need |
| 1639 | * to change it here. Accept the request though, as the |
| 1640 | * device-tree has this in an 'assigned-clocks' list. |
| 1641 | */ |
| 1642 | return PPLL_HZ; |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1643 | case SCLK_I2C0_PMU: |
| 1644 | case SCLK_I2C4_PMU: |
| 1645 | case SCLK_I2C8_PMU: |
| 1646 | ret = rk3399_i2c_set_pmuclk(priv->pmucru, clk->id, rate); |
| 1647 | break; |
| 1648 | default: |
| 1649 | return -ENOENT; |
| 1650 | } |
| 1651 | |
| 1652 | return ret; |
| 1653 | } |
| 1654 | |
| 1655 | static struct clk_ops rk3399_pmuclk_ops = { |
| 1656 | .get_rate = rk3399_pmuclk_get_rate, |
| 1657 | .set_rate = rk3399_pmuclk_set_rate, |
| 1658 | }; |
| 1659 | |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1660 | #ifndef CONFIG_SPL_BUILD |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1661 | static void pmuclk_init(struct rk3399_pmucru *pmucru) |
| 1662 | { |
| 1663 | u32 pclk_div; |
| 1664 | |
| 1665 | /* configure pmu pll(ppll) */ |
| 1666 | rkclk_set_pll(&pmucru->ppll_con[0], &ppll_init_cfg); |
| 1667 | |
| 1668 | /* configure pmu pclk */ |
| 1669 | pclk_div = PPLL_HZ / PMU_PCLK_HZ - 1; |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1670 | rk_clrsetreg(&pmucru->pmucru_clksel[0], |
| 1671 | PMU_PCLK_DIV_CON_MASK, |
| 1672 | pclk_div << PMU_PCLK_DIV_CON_SHIFT); |
| 1673 | } |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1674 | #endif |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1675 | |
| 1676 | static int rk3399_pmuclk_probe(struct udevice *dev) |
| 1677 | { |
Philipp Tomsich | cf0a4ba | 2017-03-24 19:24:24 +0100 | [diff] [blame] | 1678 | #if CONFIG_IS_ENABLED(OF_PLATDATA) || !defined(CONFIG_SPL_BUILD) |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1679 | struct rk3399_pmuclk_priv *priv = dev_get_priv(dev); |
Philipp Tomsich | cf0a4ba | 2017-03-24 19:24:24 +0100 | [diff] [blame] | 1680 | #endif |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1681 | |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1682 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1683 | struct rk3399_pmuclk_plat *plat = dev_get_plat(dev); |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1684 | |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 1685 | priv->pmucru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]); |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1686 | #endif |
| 1687 | |
| 1688 | #ifndef CONFIG_SPL_BUILD |
| 1689 | pmuclk_init(priv->pmucru); |
| 1690 | #endif |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1691 | return 0; |
| 1692 | } |
| 1693 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1694 | static int rk3399_pmuclk_of_to_plat(struct udevice *dev) |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1695 | { |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 1696 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
| 1697 | struct rk3399_pmuclk_priv *priv = dev_get_priv(dev); |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1698 | |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 1699 | priv->pmucru = dev_read_addr_ptr(dev); |
| 1700 | } |
| 1701 | |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1702 | return 0; |
| 1703 | } |
| 1704 | |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 1705 | static int rk3399_pmuclk_bind(struct udevice *dev) |
| 1706 | { |
Alper Nebi Yasak | 5de2f33 | 2020-10-05 09:57:29 +0300 | [diff] [blame] | 1707 | #if CONFIG_IS_ENABLED(RESET_ROCKCHIP) |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 1708 | int ret; |
| 1709 | |
| 1710 | ret = offsetof(struct rk3399_pmucru, pmucru_softrst_con[0]); |
| 1711 | ret = rockchip_reset_bind(dev, ret, 2); |
| 1712 | if (ret) |
Eugen Hristev | f179826 | 2023-04-11 10:17:56 +0300 | [diff] [blame] | 1713 | debug("Warning: software reset driver bind failed\n"); |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 1714 | #endif |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1718 | static const struct udevice_id rk3399_pmuclk_ids[] = { |
| 1719 | { .compatible = "rockchip,rk3399-pmucru" }, |
| 1720 | { } |
| 1721 | }; |
| 1722 | |
Simon Glass | d1dfea7 | 2016-10-01 20:04:51 -0600 | [diff] [blame] | 1723 | U_BOOT_DRIVER(rockchip_rk3399_pmuclk) = { |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1724 | .name = "rockchip_rk3399_pmucru", |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1725 | .id = UCLASS_CLK, |
| 1726 | .of_match = rk3399_pmuclk_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1727 | .priv_auto = sizeof(struct rk3399_pmuclk_priv), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1728 | .of_to_plat = rk3399_pmuclk_of_to_plat, |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1729 | .ops = &rk3399_pmuclk_ops, |
| 1730 | .probe = rk3399_pmuclk_probe, |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 1731 | .bind = rk3399_pmuclk_bind, |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1732 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1733 | .plat_auto = sizeof(struct rk3399_pmuclk_plat), |
Kever Yang | e198053 | 2017-02-13 17:38:56 +0800 | [diff] [blame] | 1734 | #endif |
Kever Yang | e54d26a | 2016-08-12 17:47:15 +0800 | [diff] [blame] | 1735 | }; |