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