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