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