2 * (C) Copyright 2016 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0
8 #include <clk-uclass.h>
11 #include <asm/arch/scu_ast2500.h>
13 #include <dt-bindings/clock/ast2500-scu.h>
16 * MAC Clock Delay settings, taken from Aspeed SDK
18 #define RGMII_TXCLK_ODLY 8
19 #define RMII_RXCLK_IDLY 2
22 * TGMII Clock Duty constants, taken from Aspeed SDK
24 #define RGMII2_TXCK_DUTY 0x66
25 #define RGMII1_TXCK_DUTY 0x64
27 #define D2PLL_DEFAULT_RATE (250 * 1000 * 1000)
29 DECLARE_GLOBAL_DATA_PTR;
32 * Clock divider/multiplier configuration struct.
33 * For H-PLL and M-PLL the formula is
34 * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
38 * They have the same layout in their control register.
40 * D-PLL and D2-PLL have extra divider (OD + 1), which is not
41 * yet needed and ignored by clock configurations.
43 struct ast2500_div_config {
46 unsigned int post_div;
50 * Get the rate of the M-PLL clock from input clock frequency and
51 * the value of the M-PLL Parameter Register.
53 static ulong ast2500_get_mpll_rate(ulong clkin, u32 mpll_reg)
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;
61 return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1);
65 * Get the rate of the H-PLL clock from input clock frequency and
66 * the value of the H-PLL Parameter Register.
68 static ulong ast2500_get_hpll_rate(ulong clkin, u32 hpll_reg)
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;
76 return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1);
79 static ulong ast2500_get_clkin(struct ast2500_scu *scu)
81 return readl(&scu->hwstrap) & SCU_HWSTRAP_CLKIN_25MHZ
82 ? 25 * 1000 * 1000 : 24 * 1000 * 1000;
86 * Get current rate or uart clock
89 * @uart_index UART index, 1-5
91 * @return current setting for uart clock rate
93 static ulong ast2500_get_uart_clk_rate(struct ast2500_scu *scu, int uart_index)
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.
100 * This has only been tested with default settings and CLKIN = 24 MHz.
104 if (readl(&scu->misc_ctrl2) &
105 (1 << (uart_index - 1 + SCU_MISC2_UARTCLK_SHIFT)))
106 uart_clkin = 192 * 1000 * 1000;
108 uart_clkin = 24 * 1000 * 1000;
110 if (readl(&scu->misc_ctrl1) & SCU_MISC_UARTCLK_DIV13)
116 static ulong ast2500_clk_get_rate(struct clk *clk)
118 struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
119 ulong clkin = ast2500_get_clkin(priv->scu);
126 * This ignores dynamic/static slowdown of ARMCLK and may
129 rate = ast2500_get_hpll_rate(clkin,
130 readl(&priv->scu->h_pll_param));
133 rate = ast2500_get_mpll_rate(clkin,
134 readl(&priv->scu->m_pll_param));
138 ulong apb_div = 4 + 4 * ((readl(&priv->scu->clk_sel1)
140 >> SCU_PCLK_DIV_SHIFT);
141 rate = ast2500_get_hpll_rate(clkin,
144 rate = rate / apb_div;
148 rate = ast2500_get_uart_clk_rate(priv->scu, 1);
151 rate = ast2500_get_uart_clk_rate(priv->scu, 2);
154 rate = ast2500_get_uart_clk_rate(priv->scu, 3);
157 rate = ast2500_get_uart_clk_rate(priv->scu, 4);
160 rate = ast2500_get_uart_clk_rate(priv->scu, 5);
170 * @input_rate - the rate of input clock in Hz
171 * @requested_rate - desired output rate in Hz
172 * @div - this is an IN/OUT parameter, at input all fields of the config
173 * need to be set to their maximum allowed values.
174 * The result (the best config we could find), would also be returned
177 * @return The clock rate, when the resulting div_config is used.
179 static ulong ast2500_calc_clock_config(ulong input_rate, ulong requested_rate,
180 struct ast2500_div_config *cfg)
183 * The assumption is that kHz precision is good enough and
184 * also enough to avoid overflow when multiplying.
186 const ulong input_rate_khz = input_rate / 1000;
187 const ulong rate_khz = requested_rate / 1000;
188 const struct ast2500_div_config max_vals = *cfg;
189 struct ast2500_div_config it = { 0, 0, 0 };
190 ulong delta = rate_khz;
191 ulong new_rate_khz = 0;
193 for (; it.denum <= max_vals.denum; ++it.denum) {
194 for (it.post_div = 0; it.post_div <= max_vals.post_div;
196 it.num = (rate_khz * (it.post_div + 1) / input_rate_khz)
198 if (it.num > max_vals.num)
201 new_rate_khz = (input_rate_khz
202 * ((it.num + 1) / (it.denum + 1)))
205 /* Keep the rate below requested one. */
206 if (new_rate_khz > rate_khz)
209 if (new_rate_khz - rate_khz < delta) {
210 delta = new_rate_khz - rate_khz;
213 return new_rate_khz * 1000;
218 return new_rate_khz * 1000;
221 static ulong ast2500_configure_ddr(struct ast2500_scu *scu, ulong rate)
223 ulong clkin = ast2500_get_clkin(scu);
225 struct ast2500_div_config div_cfg = {
226 .num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT),
227 .denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT),
228 .post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT),
231 ast2500_calc_clock_config(clkin, rate, &div_cfg);
233 mpll_reg = readl(&scu->m_pll_param);
234 mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK
235 | SCU_MPLL_DENUM_MASK);
236 mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT)
237 | (div_cfg.num << SCU_MPLL_NUM_SHIFT)
238 | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT);
241 writel(mpll_reg, &scu->m_pll_param);
244 return ast2500_get_mpll_rate(clkin, mpll_reg);
247 static ulong ast2500_configure_mac(struct ast2500_scu *scu, int index)
249 ulong clkin = ast2500_get_clkin(scu);
250 ulong hpll_rate = ast2500_get_hpll_rate(clkin,
251 readl(&scu->h_pll_param));
259 * According to data sheet, for 10/100 mode the MAC clock frequency
260 * should be at least 25MHz and for 1000 mode at least 100MHz
262 hwstrap = readl(&scu->hwstrap);
263 if (hwstrap & (SCU_HWSTRAP_MAC1_RGMII | SCU_HWSTRAP_MAC2_RGMII))
264 required_rate = 100 * 1000 * 1000;
266 required_rate = 25 * 1000 * 1000;
268 divisor = hpll_rate / required_rate;
271 /* Clock can't run fast enough, but let's try anyway */
272 debug("MAC clock too slow\n");
274 } else if (divisor > 16) {
275 /* Can't slow down the clock enough, but let's try anyway */
276 debug("MAC clock too fast\n");
282 reset_bit = SCU_SYSRESET_MAC1;
283 clkstop_bit = SCU_CLKSTOP_MAC1;
286 reset_bit = SCU_SYSRESET_MAC2;
287 clkstop_bit = SCU_CLKSTOP_MAC2;
294 clrsetbits_le32(&scu->clk_sel1, SCU_MACCLK_MASK,
295 ((divisor - 2) / 2) << SCU_MACCLK_SHIFT);
298 * Disable MAC, start its clock and re-enable it.
299 * The procedure and the delays (100us & 10ms) are
300 * specified in the datasheet.
302 setbits_le32(&scu->sysreset_ctrl1, reset_bit);
304 clrbits_le32(&scu->clk_stop_ctrl1, clkstop_bit);
306 clrbits_le32(&scu->sysreset_ctrl1, reset_bit);
308 writel((RGMII2_TXCK_DUTY << SCU_CLKDUTY_RGMII2TXCK_SHIFT)
309 | (RGMII1_TXCK_DUTY << SCU_CLKDUTY_RGMII1TXCK_SHIFT),
314 return required_rate;
317 static ulong ast2500_configure_d2pll(struct ast2500_scu *scu, ulong rate)
320 * The values and the meaning of the next three
321 * parameters are undocumented. Taken from Aspeed SDK.
323 const u32 d2_pll_ext_param = 0x2c;
324 const u32 d2_pll_sip = 0x11;
325 const u32 d2_pll_sic = 0x18;
326 u32 clk_delay_settings =
327 (RMII_RXCLK_IDLY << SCU_MICDS_MAC1RMII_RDLY_SHIFT)
328 | (RMII_RXCLK_IDLY << SCU_MICDS_MAC2RMII_RDLY_SHIFT)
329 | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC1RGMII_TXDLY_SHIFT)
330 | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC2RGMII_TXDLY_SHIFT);
331 struct ast2500_div_config div_cfg = {
332 .num = SCU_D2PLL_NUM_MASK >> SCU_D2PLL_NUM_SHIFT,
333 .denum = SCU_D2PLL_DENUM_MASK >> SCU_D2PLL_DENUM_SHIFT,
334 .post_div = SCU_D2PLL_POST_MASK >> SCU_D2PLL_POST_SHIFT,
336 ulong clkin = ast2500_get_clkin(scu);
340 writel((d2_pll_ext_param << SCU_D2PLL_EXT1_PARAM_SHIFT)
342 | SCU_D2PLL_EXT1_RESET, &scu->d2_pll_ext_param[0]);
345 * Select USB2.0 port1 PHY clock as a clock source for GCRT.
346 * This would disconnect it from D2-PLL.
348 clrsetbits_le32(&scu->misc_ctrl1, SCU_MISC_D2PLL_OFF,
349 SCU_MISC_GCRT_USB20CLK);
351 new_rate = ast2500_calc_clock_config(clkin, rate, &div_cfg);
352 writel((d2_pll_sip << SCU_D2PLL_SIP_SHIFT)
353 | (d2_pll_sic << SCU_D2PLL_SIC_SHIFT)
354 | (div_cfg.num << SCU_D2PLL_NUM_SHIFT)
355 | (div_cfg.denum << SCU_D2PLL_DENUM_SHIFT)
356 | (div_cfg.post_div << SCU_D2PLL_POST_SHIFT),
359 clrbits_le32(&scu->d2_pll_ext_param[0],
360 SCU_D2PLL_EXT1_OFF | SCU_D2PLL_EXT1_RESET);
362 clrsetbits_le32(&scu->misc_ctrl2,
363 SCU_MISC2_RGMII_HPLL | SCU_MISC2_RMII_MPLL
364 | SCU_MISC2_RGMII_CLKDIV_MASK |
365 SCU_MISC2_RMII_CLKDIV_MASK,
366 (4 << SCU_MISC2_RMII_CLKDIV_SHIFT));
368 writel(clk_delay_settings | SCU_MICDS_RGMIIPLL, &scu->mac_clk_delay);
369 writel(clk_delay_settings, &scu->mac_clk_delay_100M);
370 writel(clk_delay_settings, &scu->mac_clk_delay_10M);
377 static ulong ast2500_clk_set_rate(struct clk *clk, ulong rate)
379 struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
385 new_rate = ast2500_configure_ddr(priv->scu, rate);
388 new_rate = ast2500_configure_d2pll(priv->scu, rate);
397 static int ast2500_clk_enable(struct clk *clk)
399 struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
403 * For MAC clocks the clock rate is
404 * configured based on whether RGMII or RMII mode has been selected
405 * through hardware strapping.
408 ast2500_configure_mac(priv->scu, 1);
411 ast2500_configure_mac(priv->scu, 2);
414 ast2500_configure_d2pll(priv->scu, D2PLL_DEFAULT_RATE);
422 struct clk_ops ast2500_clk_ops = {
423 .get_rate = ast2500_clk_get_rate,
424 .set_rate = ast2500_clk_set_rate,
425 .enable = ast2500_clk_enable,
428 static int ast2500_clk_probe(struct udevice *dev)
430 struct ast2500_clk_priv *priv = dev_get_priv(dev);
432 priv->scu = dev_get_addr_ptr(dev);
433 if (IS_ERR(priv->scu))
434 return PTR_ERR(priv->scu);
439 static int ast2500_clk_bind(struct udevice *dev)
443 /* The reset driver does not have a device node, so bind it here */
444 ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
446 debug("Warning: No reset driver: ret=%d\n", ret);
451 static const struct udevice_id ast2500_clk_ids[] = {
452 { .compatible = "aspeed,ast2500-scu" },
456 U_BOOT_DRIVER(aspeed_ast2500_scu) = {
457 .name = "aspeed_ast2500_scu",
459 .of_match = ast2500_clk_ids,
460 .priv_auto_alloc_size = sizeof(struct ast2500_clk_priv),
461 .ops = &ast2500_clk_ops,
462 .bind = ast2500_clk_bind,
463 .probe = ast2500_clk_probe,