Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Google, Inc |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk-uclass.h> |
| 8 | #include <dm.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <asm/arch/scu_ast2500.h> |
| 11 | #include <dm/lists.h> |
| 12 | #include <dt-bindings/clock/ast2500-scu.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 13 | #include <linux/err.h> |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 14 | |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 15 | /* |
| 16 | * MAC Clock Delay settings, taken from Aspeed SDK |
| 17 | */ |
| 18 | #define RGMII_TXCLK_ODLY 8 |
| 19 | #define RMII_RXCLK_IDLY 2 |
| 20 | |
| 21 | /* |
| 22 | * TGMII Clock Duty constants, taken from Aspeed SDK |
| 23 | */ |
| 24 | #define RGMII2_TXCK_DUTY 0x66 |
| 25 | #define RGMII1_TXCK_DUTY 0x64 |
| 26 | |
| 27 | #define D2PLL_DEFAULT_RATE (250 * 1000 * 1000) |
| 28 | |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
| 31 | /* |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 32 | * Clock divider/multiplier configuration struct. |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 33 | * For H-PLL and M-PLL the formula is |
| 34 | * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1) |
| 35 | * M - Numerator |
| 36 | * N - Denumerator |
| 37 | * P - Post Divider |
| 38 | * They have the same layout in their control register. |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 39 | * |
| 40 | * D-PLL and D2-PLL have extra divider (OD + 1), which is not |
| 41 | * yet needed and ignored by clock configurations. |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 42 | */ |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 43 | struct ast2500_div_config { |
| 44 | unsigned int num; |
| 45 | unsigned int denum; |
| 46 | unsigned int post_div; |
| 47 | }; |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Get the rate of the M-PLL clock from input clock frequency and |
| 51 | * the value of the M-PLL Parameter Register. |
| 52 | */ |
| 53 | static ulong ast2500_get_mpll_rate(ulong clkin, u32 mpll_reg) |
| 54 | { |
maxims@google.com | a91f1d2 | 2017-04-17 12:00:33 -0700 | [diff] [blame] | 55 | const ulong num = (mpll_reg & SCU_MPLL_NUM_MASK) >> SCU_MPLL_NUM_SHIFT; |
| 56 | const ulong denum = (mpll_reg & SCU_MPLL_DENUM_MASK) |
| 57 | >> SCU_MPLL_DENUM_SHIFT; |
| 58 | const ulong post_div = (mpll_reg & SCU_MPLL_POST_MASK) |
| 59 | >> SCU_MPLL_POST_SHIFT; |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 60 | |
maxims@google.com | d067217 | 2017-01-30 11:35:04 -0800 | [diff] [blame] | 61 | return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1); |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Get the rate of the H-PLL clock from input clock frequency and |
| 66 | * the value of the H-PLL Parameter Register. |
| 67 | */ |
| 68 | static ulong ast2500_get_hpll_rate(ulong clkin, u32 hpll_reg) |
| 69 | { |
maxims@google.com | a91f1d2 | 2017-04-17 12:00:33 -0700 | [diff] [blame] | 70 | const ulong num = (hpll_reg & SCU_HPLL_NUM_MASK) >> SCU_HPLL_NUM_SHIFT; |
| 71 | const ulong denum = (hpll_reg & SCU_HPLL_DENUM_MASK) |
| 72 | >> SCU_HPLL_DENUM_SHIFT; |
| 73 | const ulong post_div = (hpll_reg & SCU_HPLL_POST_MASK) |
| 74 | >> SCU_HPLL_POST_SHIFT; |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 75 | |
maxims@google.com | d067217 | 2017-01-30 11:35:04 -0800 | [diff] [blame] | 76 | return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1); |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static ulong ast2500_get_clkin(struct ast2500_scu *scu) |
| 80 | { |
| 81 | return readl(&scu->hwstrap) & SCU_HWSTRAP_CLKIN_25MHZ |
| 82 | ? 25 * 1000 * 1000 : 24 * 1000 * 1000; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get current rate or uart clock |
| 87 | * |
| 88 | * @scu SCU registers |
| 89 | * @uart_index UART index, 1-5 |
| 90 | * |
| 91 | * @return current setting for uart clock rate |
| 92 | */ |
| 93 | static ulong ast2500_get_uart_clk_rate(struct ast2500_scu *scu, int uart_index) |
| 94 | { |
| 95 | /* |
| 96 | * ast2500 datasheet is very confusing when it comes to UART clocks, |
| 97 | * especially when CLKIN = 25 MHz. The settings are in |
| 98 | * different registers and it is unclear how they interact. |
| 99 | * |
| 100 | * This has only been tested with default settings and CLKIN = 24 MHz. |
| 101 | */ |
| 102 | ulong uart_clkin; |
| 103 | |
| 104 | if (readl(&scu->misc_ctrl2) & |
| 105 | (1 << (uart_index - 1 + SCU_MISC2_UARTCLK_SHIFT))) |
| 106 | uart_clkin = 192 * 1000 * 1000; |
| 107 | else |
| 108 | uart_clkin = 24 * 1000 * 1000; |
| 109 | |
| 110 | if (readl(&scu->misc_ctrl1) & SCU_MISC_UARTCLK_DIV13) |
| 111 | uart_clkin /= 13; |
| 112 | |
| 113 | return uart_clkin; |
| 114 | } |
| 115 | |
| 116 | static ulong ast2500_clk_get_rate(struct clk *clk) |
| 117 | { |
| 118 | struct ast2500_clk_priv *priv = dev_get_priv(clk->dev); |
| 119 | ulong clkin = ast2500_get_clkin(priv->scu); |
| 120 | ulong rate; |
| 121 | |
| 122 | switch (clk->id) { |
| 123 | case PLL_HPLL: |
| 124 | case ARMCLK: |
| 125 | /* |
| 126 | * This ignores dynamic/static slowdown of ARMCLK and may |
| 127 | * be inaccurate. |
| 128 | */ |
| 129 | rate = ast2500_get_hpll_rate(clkin, |
| 130 | readl(&priv->scu->h_pll_param)); |
| 131 | break; |
| 132 | case MCLK_DDR: |
| 133 | rate = ast2500_get_mpll_rate(clkin, |
| 134 | readl(&priv->scu->m_pll_param)); |
| 135 | break; |
maxims@google.com | 995167b | 2017-04-17 12:00:29 -0700 | [diff] [blame] | 136 | case BCLK_PCLK: |
| 137 | { |
| 138 | ulong apb_div = 4 + 4 * ((readl(&priv->scu->clk_sel1) |
maxims@google.com | a91f1d2 | 2017-04-17 12:00:33 -0700 | [diff] [blame] | 139 | & SCU_PCLK_DIV_MASK) |
| 140 | >> SCU_PCLK_DIV_SHIFT); |
maxims@google.com | 995167b | 2017-04-17 12:00:29 -0700 | [diff] [blame] | 141 | rate = ast2500_get_hpll_rate(clkin, |
maxims@google.com | a91f1d2 | 2017-04-17 12:00:33 -0700 | [diff] [blame] | 142 | readl(&priv-> |
| 143 | scu->h_pll_param)); |
maxims@google.com | 995167b | 2017-04-17 12:00:29 -0700 | [diff] [blame] | 144 | rate = rate / apb_div; |
| 145 | } |
| 146 | break; |
Eddie James | b7d76ac | 2019-08-15 14:29:37 -0500 | [diff] [blame] | 147 | case BCLK_SDCLK: |
| 148 | { |
| 149 | ulong apb_div = 4 + 4 * ((readl(&priv->scu->clk_sel1) |
| 150 | & SCU_SDCLK_DIV_MASK) |
| 151 | >> SCU_SDCLK_DIV_SHIFT); |
| 152 | rate = ast2500_get_hpll_rate(clkin, |
| 153 | readl(&priv-> |
| 154 | scu->h_pll_param)); |
| 155 | rate = rate / apb_div; |
| 156 | } |
| 157 | break; |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 158 | case PCLK_UART1: |
| 159 | rate = ast2500_get_uart_clk_rate(priv->scu, 1); |
| 160 | break; |
| 161 | case PCLK_UART2: |
| 162 | rate = ast2500_get_uart_clk_rate(priv->scu, 2); |
| 163 | break; |
| 164 | case PCLK_UART3: |
| 165 | rate = ast2500_get_uart_clk_rate(priv->scu, 3); |
| 166 | break; |
| 167 | case PCLK_UART4: |
| 168 | rate = ast2500_get_uart_clk_rate(priv->scu, 4); |
| 169 | break; |
| 170 | case PCLK_UART5: |
| 171 | rate = ast2500_get_uart_clk_rate(priv->scu, 5); |
| 172 | break; |
| 173 | default: |
| 174 | return -ENOENT; |
| 175 | } |
| 176 | |
| 177 | return rate; |
| 178 | } |
| 179 | |
Cédric Le Goater | d7f3789 | 2018-10-29 07:06:41 +0100 | [diff] [blame] | 180 | struct ast2500_clock_config { |
| 181 | ulong input_rate; |
| 182 | ulong rate; |
| 183 | struct ast2500_div_config cfg; |
| 184 | }; |
| 185 | |
| 186 | static const struct ast2500_clock_config ast2500_clock_config_defaults[] = { |
| 187 | { 24000000, 250000000, { .num = 124, .denum = 1, .post_div = 5 } }, |
| 188 | }; |
| 189 | |
| 190 | static bool ast2500_get_clock_config_default(ulong input_rate, |
| 191 | ulong requested_rate, |
| 192 | struct ast2500_div_config *cfg) |
| 193 | { |
| 194 | int i; |
| 195 | |
| 196 | for (i = 0; i < ARRAY_SIZE(ast2500_clock_config_defaults); i++) { |
| 197 | const struct ast2500_clock_config *default_cfg = |
| 198 | &ast2500_clock_config_defaults[i]; |
| 199 | if (default_cfg->input_rate == input_rate && |
| 200 | default_cfg->rate == requested_rate) { |
| 201 | *cfg = default_cfg->cfg; |
| 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return false; |
| 207 | } |
| 208 | |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 209 | /* |
| 210 | * @input_rate - the rate of input clock in Hz |
| 211 | * @requested_rate - desired output rate in Hz |
| 212 | * @div - this is an IN/OUT parameter, at input all fields of the config |
| 213 | * need to be set to their maximum allowed values. |
| 214 | * The result (the best config we could find), would also be returned |
| 215 | * in this structure. |
| 216 | * |
| 217 | * @return The clock rate, when the resulting div_config is used. |
| 218 | */ |
| 219 | static ulong ast2500_calc_clock_config(ulong input_rate, ulong requested_rate, |
| 220 | struct ast2500_div_config *cfg) |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 221 | { |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 222 | /* |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 223 | * The assumption is that kHz precision is good enough and |
| 224 | * also enough to avoid overflow when multiplying. |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 225 | */ |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 226 | const ulong input_rate_khz = input_rate / 1000; |
| 227 | const ulong rate_khz = requested_rate / 1000; |
| 228 | const struct ast2500_div_config max_vals = *cfg; |
| 229 | struct ast2500_div_config it = { 0, 0, 0 }; |
| 230 | ulong delta = rate_khz; |
| 231 | ulong new_rate_khz = 0; |
| 232 | |
Cédric Le Goater | d7f3789 | 2018-10-29 07:06:41 +0100 | [diff] [blame] | 233 | /* |
| 234 | * Look for a well known frequency first. |
| 235 | */ |
| 236 | if (ast2500_get_clock_config_default(input_rate, requested_rate, cfg)) |
| 237 | return requested_rate; |
| 238 | |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 239 | for (; it.denum <= max_vals.denum; ++it.denum) { |
| 240 | for (it.post_div = 0; it.post_div <= max_vals.post_div; |
| 241 | ++it.post_div) { |
| 242 | it.num = (rate_khz * (it.post_div + 1) / input_rate_khz) |
| 243 | * (it.denum + 1); |
| 244 | if (it.num > max_vals.num) |
| 245 | continue; |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 246 | |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 247 | new_rate_khz = (input_rate_khz |
| 248 | * ((it.num + 1) / (it.denum + 1))) |
| 249 | / (it.post_div + 1); |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 250 | |
| 251 | /* Keep the rate below requested one. */ |
| 252 | if (new_rate_khz > rate_khz) |
| 253 | continue; |
| 254 | |
| 255 | if (new_rate_khz - rate_khz < delta) { |
| 256 | delta = new_rate_khz - rate_khz; |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 257 | *cfg = it; |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 258 | if (delta == 0) |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 259 | return new_rate_khz * 1000; |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 264 | return new_rate_khz * 1000; |
| 265 | } |
| 266 | |
| 267 | static ulong ast2500_configure_ddr(struct ast2500_scu *scu, ulong rate) |
| 268 | { |
| 269 | ulong clkin = ast2500_get_clkin(scu); |
| 270 | u32 mpll_reg; |
| 271 | struct ast2500_div_config div_cfg = { |
maxims@google.com | a91f1d2 | 2017-04-17 12:00:33 -0700 | [diff] [blame] | 272 | .num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT), |
| 273 | .denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT), |
| 274 | .post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT), |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | ast2500_calc_clock_config(clkin, rate, &div_cfg); |
| 278 | |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 279 | mpll_reg = readl(&scu->m_pll_param); |
maxims@google.com | a91f1d2 | 2017-04-17 12:00:33 -0700 | [diff] [blame] | 280 | mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK |
| 281 | | SCU_MPLL_DENUM_MASK); |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 282 | mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT) |
| 283 | | (div_cfg.num << SCU_MPLL_NUM_SHIFT) |
| 284 | | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT); |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 285 | |
maxims@google.com | adea66c | 2017-04-17 12:00:23 -0700 | [diff] [blame] | 286 | ast_scu_unlock(scu); |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 287 | writel(mpll_reg, &scu->m_pll_param); |
maxims@google.com | adea66c | 2017-04-17 12:00:23 -0700 | [diff] [blame] | 288 | ast_scu_lock(scu); |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 289 | |
| 290 | return ast2500_get_mpll_rate(clkin, mpll_reg); |
| 291 | } |
| 292 | |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 293 | static ulong ast2500_configure_mac(struct ast2500_scu *scu, int index) |
| 294 | { |
| 295 | ulong clkin = ast2500_get_clkin(scu); |
| 296 | ulong hpll_rate = ast2500_get_hpll_rate(clkin, |
| 297 | readl(&scu->h_pll_param)); |
| 298 | ulong required_rate; |
| 299 | u32 hwstrap; |
| 300 | u32 divisor; |
| 301 | u32 reset_bit; |
| 302 | u32 clkstop_bit; |
| 303 | |
| 304 | /* |
| 305 | * According to data sheet, for 10/100 mode the MAC clock frequency |
| 306 | * should be at least 25MHz and for 1000 mode at least 100MHz |
| 307 | */ |
| 308 | hwstrap = readl(&scu->hwstrap); |
| 309 | if (hwstrap & (SCU_HWSTRAP_MAC1_RGMII | SCU_HWSTRAP_MAC2_RGMII)) |
| 310 | required_rate = 100 * 1000 * 1000; |
| 311 | else |
| 312 | required_rate = 25 * 1000 * 1000; |
| 313 | |
| 314 | divisor = hpll_rate / required_rate; |
| 315 | |
| 316 | if (divisor < 4) { |
| 317 | /* Clock can't run fast enough, but let's try anyway */ |
| 318 | debug("MAC clock too slow\n"); |
| 319 | divisor = 4; |
| 320 | } else if (divisor > 16) { |
| 321 | /* Can't slow down the clock enough, but let's try anyway */ |
| 322 | debug("MAC clock too fast\n"); |
| 323 | divisor = 16; |
| 324 | } |
| 325 | |
| 326 | switch (index) { |
| 327 | case 1: |
| 328 | reset_bit = SCU_SYSRESET_MAC1; |
| 329 | clkstop_bit = SCU_CLKSTOP_MAC1; |
| 330 | break; |
| 331 | case 2: |
| 332 | reset_bit = SCU_SYSRESET_MAC2; |
| 333 | clkstop_bit = SCU_CLKSTOP_MAC2; |
| 334 | break; |
| 335 | default: |
| 336 | return -EINVAL; |
| 337 | } |
| 338 | |
| 339 | ast_scu_unlock(scu); |
| 340 | clrsetbits_le32(&scu->clk_sel1, SCU_MACCLK_MASK, |
| 341 | ((divisor - 2) / 2) << SCU_MACCLK_SHIFT); |
| 342 | |
| 343 | /* |
| 344 | * Disable MAC, start its clock and re-enable it. |
| 345 | * The procedure and the delays (100us & 10ms) are |
| 346 | * specified in the datasheet. |
| 347 | */ |
| 348 | setbits_le32(&scu->sysreset_ctrl1, reset_bit); |
| 349 | udelay(100); |
| 350 | clrbits_le32(&scu->clk_stop_ctrl1, clkstop_bit); |
| 351 | mdelay(10); |
| 352 | clrbits_le32(&scu->sysreset_ctrl1, reset_bit); |
| 353 | |
| 354 | writel((RGMII2_TXCK_DUTY << SCU_CLKDUTY_RGMII2TXCK_SHIFT) |
| 355 | | (RGMII1_TXCK_DUTY << SCU_CLKDUTY_RGMII1TXCK_SHIFT), |
| 356 | &scu->clk_duty_sel); |
| 357 | |
| 358 | ast_scu_lock(scu); |
| 359 | |
| 360 | return required_rate; |
| 361 | } |
| 362 | |
| 363 | static ulong ast2500_configure_d2pll(struct ast2500_scu *scu, ulong rate) |
| 364 | { |
| 365 | /* |
| 366 | * The values and the meaning of the next three |
| 367 | * parameters are undocumented. Taken from Aspeed SDK. |
Cédric Le Goater | d7f3789 | 2018-10-29 07:06:41 +0100 | [diff] [blame] | 368 | * |
| 369 | * TODO(clg@kaod.org): the SIP and SIC values depend on the |
| 370 | * Numerator value |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 371 | */ |
| 372 | const u32 d2_pll_ext_param = 0x2c; |
| 373 | const u32 d2_pll_sip = 0x11; |
| 374 | const u32 d2_pll_sic = 0x18; |
| 375 | u32 clk_delay_settings = |
| 376 | (RMII_RXCLK_IDLY << SCU_MICDS_MAC1RMII_RDLY_SHIFT) |
| 377 | | (RMII_RXCLK_IDLY << SCU_MICDS_MAC2RMII_RDLY_SHIFT) |
| 378 | | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC1RGMII_TXDLY_SHIFT) |
| 379 | | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC2RGMII_TXDLY_SHIFT); |
| 380 | struct ast2500_div_config div_cfg = { |
| 381 | .num = SCU_D2PLL_NUM_MASK >> SCU_D2PLL_NUM_SHIFT, |
| 382 | .denum = SCU_D2PLL_DENUM_MASK >> SCU_D2PLL_DENUM_SHIFT, |
| 383 | .post_div = SCU_D2PLL_POST_MASK >> SCU_D2PLL_POST_SHIFT, |
| 384 | }; |
| 385 | ulong clkin = ast2500_get_clkin(scu); |
| 386 | ulong new_rate; |
| 387 | |
| 388 | ast_scu_unlock(scu); |
| 389 | writel((d2_pll_ext_param << SCU_D2PLL_EXT1_PARAM_SHIFT) |
| 390 | | SCU_D2PLL_EXT1_OFF |
| 391 | | SCU_D2PLL_EXT1_RESET, &scu->d2_pll_ext_param[0]); |
| 392 | |
| 393 | /* |
| 394 | * Select USB2.0 port1 PHY clock as a clock source for GCRT. |
| 395 | * This would disconnect it from D2-PLL. |
| 396 | */ |
| 397 | clrsetbits_le32(&scu->misc_ctrl1, SCU_MISC_D2PLL_OFF, |
| 398 | SCU_MISC_GCRT_USB20CLK); |
| 399 | |
| 400 | new_rate = ast2500_calc_clock_config(clkin, rate, &div_cfg); |
| 401 | writel((d2_pll_sip << SCU_D2PLL_SIP_SHIFT) |
| 402 | | (d2_pll_sic << SCU_D2PLL_SIC_SHIFT) |
| 403 | | (div_cfg.num << SCU_D2PLL_NUM_SHIFT) |
| 404 | | (div_cfg.denum << SCU_D2PLL_DENUM_SHIFT) |
| 405 | | (div_cfg.post_div << SCU_D2PLL_POST_SHIFT), |
| 406 | &scu->d2_pll_param); |
| 407 | |
| 408 | clrbits_le32(&scu->d2_pll_ext_param[0], |
| 409 | SCU_D2PLL_EXT1_OFF | SCU_D2PLL_EXT1_RESET); |
| 410 | |
| 411 | clrsetbits_le32(&scu->misc_ctrl2, |
| 412 | SCU_MISC2_RGMII_HPLL | SCU_MISC2_RMII_MPLL |
| 413 | | SCU_MISC2_RGMII_CLKDIV_MASK | |
| 414 | SCU_MISC2_RMII_CLKDIV_MASK, |
| 415 | (4 << SCU_MISC2_RMII_CLKDIV_SHIFT)); |
| 416 | |
| 417 | writel(clk_delay_settings | SCU_MICDS_RGMIIPLL, &scu->mac_clk_delay); |
| 418 | writel(clk_delay_settings, &scu->mac_clk_delay_100M); |
| 419 | writel(clk_delay_settings, &scu->mac_clk_delay_10M); |
| 420 | |
| 421 | ast_scu_lock(scu); |
| 422 | |
| 423 | return new_rate; |
| 424 | } |
| 425 | |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 426 | static ulong ast2500_clk_set_rate(struct clk *clk, ulong rate) |
| 427 | { |
| 428 | struct ast2500_clk_priv *priv = dev_get_priv(clk->dev); |
| 429 | |
| 430 | ulong new_rate; |
| 431 | switch (clk->id) { |
| 432 | case PLL_MPLL: |
| 433 | case MCLK_DDR: |
| 434 | new_rate = ast2500_configure_ddr(priv->scu, rate); |
| 435 | break; |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 436 | case PLL_D2PLL: |
| 437 | new_rate = ast2500_configure_d2pll(priv->scu, rate); |
| 438 | break; |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 439 | default: |
| 440 | return -ENOENT; |
| 441 | } |
| 442 | |
| 443 | return new_rate; |
| 444 | } |
| 445 | |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 446 | static int ast2500_clk_enable(struct clk *clk) |
| 447 | { |
| 448 | struct ast2500_clk_priv *priv = dev_get_priv(clk->dev); |
| 449 | |
| 450 | switch (clk->id) { |
Eddie James | b7d76ac | 2019-08-15 14:29:37 -0500 | [diff] [blame] | 451 | case BCLK_SDCLK: |
| 452 | if (readl(&priv->scu->clk_stop_ctrl1) & SCU_CLKSTOP_SDCLK) { |
| 453 | ast_scu_unlock(priv->scu); |
| 454 | |
| 455 | setbits_le32(&priv->scu->sysreset_ctrl1, |
| 456 | SCU_SYSRESET_SDIO); |
| 457 | udelay(100); |
| 458 | clrbits_le32(&priv->scu->clk_stop_ctrl1, |
| 459 | SCU_CLKSTOP_SDCLK); |
| 460 | mdelay(10); |
| 461 | clrbits_le32(&priv->scu->sysreset_ctrl1, |
| 462 | SCU_SYSRESET_SDIO); |
| 463 | |
| 464 | ast_scu_lock(priv->scu); |
| 465 | } |
| 466 | break; |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 467 | /* |
| 468 | * For MAC clocks the clock rate is |
| 469 | * configured based on whether RGMII or RMII mode has been selected |
| 470 | * through hardware strapping. |
| 471 | */ |
| 472 | case PCLK_MAC1: |
| 473 | ast2500_configure_mac(priv->scu, 1); |
| 474 | break; |
| 475 | case PCLK_MAC2: |
| 476 | ast2500_configure_mac(priv->scu, 2); |
| 477 | break; |
| 478 | case PLL_D2PLL: |
| 479 | ast2500_configure_d2pll(priv->scu, D2PLL_DEFAULT_RATE); |
Cédric Le Goater | 62b4bfd | 2018-10-29 07:06:37 +0100 | [diff] [blame] | 480 | break; |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 481 | default: |
| 482 | return -ENOENT; |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 488 | struct clk_ops ast2500_clk_ops = { |
| 489 | .get_rate = ast2500_clk_get_rate, |
| 490 | .set_rate = ast2500_clk_set_rate, |
maxims@google.com | 15016af | 2017-04-17 12:00:32 -0700 | [diff] [blame] | 491 | .enable = ast2500_clk_enable, |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 492 | }; |
| 493 | |
Simon Glass | 7d7c704 | 2019-12-29 21:19:15 -0700 | [diff] [blame] | 494 | static int ast2500_clk_ofdata_to_platdata(struct udevice *dev) |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 495 | { |
| 496 | struct ast2500_clk_priv *priv = dev_get_priv(dev); |
| 497 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 498 | priv->scu = devfdt_get_addr_ptr(dev); |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 499 | if (IS_ERR(priv->scu)) |
| 500 | return PTR_ERR(priv->scu); |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int ast2500_clk_bind(struct udevice *dev) |
| 506 | { |
| 507 | int ret; |
| 508 | |
| 509 | /* The reset driver does not have a device node, so bind it here */ |
| 510 | ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev); |
| 511 | if (ret) |
| 512 | debug("Warning: No reset driver: ret=%d\n", ret); |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static const struct udevice_id ast2500_clk_ids[] = { |
| 518 | { .compatible = "aspeed,ast2500-scu" }, |
| 519 | { } |
| 520 | }; |
| 521 | |
| 522 | U_BOOT_DRIVER(aspeed_ast2500_scu) = { |
| 523 | .name = "aspeed_ast2500_scu", |
| 524 | .id = UCLASS_CLK, |
| 525 | .of_match = ast2500_clk_ids, |
| 526 | .priv_auto_alloc_size = sizeof(struct ast2500_clk_priv), |
| 527 | .ops = &ast2500_clk_ops, |
| 528 | .bind = ast2500_clk_bind, |
Simon Glass | 7d7c704 | 2019-12-29 21:19:15 -0700 | [diff] [blame] | 529 | .ofdata_to_platdata = ast2500_clk_ofdata_to_platdata, |
maxims@google.com | 2d5a2ad | 2017-01-18 13:44:56 -0800 | [diff] [blame] | 530 | }; |