1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2016 Google, Inc
7 #include <clk-uclass.h>
11 #include <asm/arch/scu_ast2500.h>
13 #include <dt-bindings/clock/ast2500-scu.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
18 * MAC Clock Delay settings, taken from Aspeed SDK
20 #define RGMII_TXCLK_ODLY 8
21 #define RMII_RXCLK_IDLY 2
24 * TGMII Clock Duty constants, taken from Aspeed SDK
26 #define RGMII2_TXCK_DUTY 0x66
27 #define RGMII1_TXCK_DUTY 0x64
29 #define D2PLL_DEFAULT_RATE (250 * 1000 * 1000)
31 DECLARE_GLOBAL_DATA_PTR;
34 * Clock divider/multiplier configuration struct.
35 * For H-PLL and M-PLL the formula is
36 * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
40 * They have the same layout in their control register.
42 * D-PLL and D2-PLL have extra divider (OD + 1), which is not
43 * yet needed and ignored by clock configurations.
45 struct ast2500_div_config {
48 unsigned int post_div;
52 * Get the rate of the M-PLL clock from input clock frequency and
53 * the value of the M-PLL Parameter Register.
55 static ulong ast2500_get_mpll_rate(ulong clkin, u32 mpll_reg)
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;
63 return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1);
67 * Get the rate of the H-PLL clock from input clock frequency and
68 * the value of the H-PLL Parameter Register.
70 static ulong ast2500_get_hpll_rate(ulong clkin, u32 hpll_reg)
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;
78 return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1);
81 static ulong ast2500_get_clkin(struct ast2500_scu *scu)
83 return readl(&scu->hwstrap) & SCU_HWSTRAP_CLKIN_25MHZ
84 ? 25 * 1000 * 1000 : 24 * 1000 * 1000;
88 * Get current rate or uart clock
91 * @uart_index UART index, 1-5
93 * @return current setting for uart clock rate
95 static ulong ast2500_get_uart_clk_rate(struct ast2500_scu *scu, int uart_index)
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.
102 * This has only been tested with default settings and CLKIN = 24 MHz.
106 if (readl(&scu->misc_ctrl2) &
107 (1 << (uart_index - 1 + SCU_MISC2_UARTCLK_SHIFT)))
108 uart_clkin = 192 * 1000 * 1000;
110 uart_clkin = 24 * 1000 * 1000;
112 if (readl(&scu->misc_ctrl1) & SCU_MISC_UARTCLK_DIV13)
118 static ulong ast2500_clk_get_rate(struct clk *clk)
120 struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
121 ulong clkin = ast2500_get_clkin(priv->scu);
128 * This ignores dynamic/static slowdown of ARMCLK and may
131 rate = ast2500_get_hpll_rate(clkin,
132 readl(&priv->scu->h_pll_param));
135 rate = ast2500_get_mpll_rate(clkin,
136 readl(&priv->scu->m_pll_param));
140 ulong apb_div = 4 + 4 * ((readl(&priv->scu->clk_sel1)
142 >> SCU_PCLK_DIV_SHIFT);
143 rate = ast2500_get_hpll_rate(clkin,
146 rate = rate / apb_div;
151 ulong apb_div = 4 + 4 * ((readl(&priv->scu->clk_sel1)
152 & SCU_SDCLK_DIV_MASK)
153 >> SCU_SDCLK_DIV_SHIFT);
154 rate = ast2500_get_hpll_rate(clkin,
157 rate = rate / apb_div;
161 rate = ast2500_get_uart_clk_rate(priv->scu, 1);
164 rate = ast2500_get_uart_clk_rate(priv->scu, 2);
167 rate = ast2500_get_uart_clk_rate(priv->scu, 3);
170 rate = ast2500_get_uart_clk_rate(priv->scu, 4);
173 rate = ast2500_get_uart_clk_rate(priv->scu, 5);
182 struct ast2500_clock_config {
185 struct ast2500_div_config cfg;
188 static const struct ast2500_clock_config ast2500_clock_config_defaults[] = {
189 { 24000000, 250000000, { .num = 124, .denum = 1, .post_div = 5 } },
192 static bool ast2500_get_clock_config_default(ulong input_rate,
193 ulong requested_rate,
194 struct ast2500_div_config *cfg)
198 for (i = 0; i < ARRAY_SIZE(ast2500_clock_config_defaults); i++) {
199 const struct ast2500_clock_config *default_cfg =
200 &ast2500_clock_config_defaults[i];
201 if (default_cfg->input_rate == input_rate &&
202 default_cfg->rate == requested_rate) {
203 *cfg = default_cfg->cfg;
212 * @input_rate - the rate of input clock in Hz
213 * @requested_rate - desired output rate in Hz
214 * @div - this is an IN/OUT parameter, at input all fields of the config
215 * need to be set to their maximum allowed values.
216 * The result (the best config we could find), would also be returned
219 * @return The clock rate, when the resulting div_config is used.
221 static ulong ast2500_calc_clock_config(ulong input_rate, ulong requested_rate,
222 struct ast2500_div_config *cfg)
225 * The assumption is that kHz precision is good enough and
226 * also enough to avoid overflow when multiplying.
228 const ulong input_rate_khz = input_rate / 1000;
229 const ulong rate_khz = requested_rate / 1000;
230 const struct ast2500_div_config max_vals = *cfg;
231 struct ast2500_div_config it = { 0, 0, 0 };
232 ulong delta = rate_khz;
233 ulong new_rate_khz = 0;
236 * Look for a well known frequency first.
238 if (ast2500_get_clock_config_default(input_rate, requested_rate, cfg))
239 return requested_rate;
241 for (; it.denum <= max_vals.denum; ++it.denum) {
242 for (it.post_div = 0; it.post_div <= max_vals.post_div;
244 it.num = (rate_khz * (it.post_div + 1) / input_rate_khz)
246 if (it.num > max_vals.num)
249 new_rate_khz = (input_rate_khz
250 * ((it.num + 1) / (it.denum + 1)))
253 /* Keep the rate below requested one. */
254 if (new_rate_khz > rate_khz)
257 if (new_rate_khz - rate_khz < delta) {
258 delta = new_rate_khz - rate_khz;
261 return new_rate_khz * 1000;
266 return new_rate_khz * 1000;
269 static ulong ast2500_configure_ddr(struct ast2500_scu *scu, ulong rate)
271 ulong clkin = ast2500_get_clkin(scu);
273 struct ast2500_div_config div_cfg = {
274 .num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT),
275 .denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT),
276 .post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT),
279 ast2500_calc_clock_config(clkin, rate, &div_cfg);
281 mpll_reg = readl(&scu->m_pll_param);
282 mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK
283 | SCU_MPLL_DENUM_MASK);
284 mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT)
285 | (div_cfg.num << SCU_MPLL_NUM_SHIFT)
286 | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT);
289 writel(mpll_reg, &scu->m_pll_param);
292 return ast2500_get_mpll_rate(clkin, mpll_reg);
295 static ulong ast2500_configure_mac(struct ast2500_scu *scu, int index)
297 ulong clkin = ast2500_get_clkin(scu);
298 ulong hpll_rate = ast2500_get_hpll_rate(clkin,
299 readl(&scu->h_pll_param));
307 * According to data sheet, for 10/100 mode the MAC clock frequency
308 * should be at least 25MHz and for 1000 mode at least 100MHz
310 hwstrap = readl(&scu->hwstrap);
311 if (hwstrap & (SCU_HWSTRAP_MAC1_RGMII | SCU_HWSTRAP_MAC2_RGMII))
312 required_rate = 100 * 1000 * 1000;
314 required_rate = 25 * 1000 * 1000;
316 divisor = hpll_rate / required_rate;
319 /* Clock can't run fast enough, but let's try anyway */
320 debug("MAC clock too slow\n");
322 } else if (divisor > 16) {
323 /* Can't slow down the clock enough, but let's try anyway */
324 debug("MAC clock too fast\n");
330 reset_bit = SCU_SYSRESET_MAC1;
331 clkstop_bit = SCU_CLKSTOP_MAC1;
334 reset_bit = SCU_SYSRESET_MAC2;
335 clkstop_bit = SCU_CLKSTOP_MAC2;
342 clrsetbits_le32(&scu->clk_sel1, SCU_MACCLK_MASK,
343 ((divisor - 2) / 2) << SCU_MACCLK_SHIFT);
346 * Disable MAC, start its clock and re-enable it.
347 * The procedure and the delays (100us & 10ms) are
348 * specified in the datasheet.
350 setbits_le32(&scu->sysreset_ctrl1, reset_bit);
352 clrbits_le32(&scu->clk_stop_ctrl1, clkstop_bit);
354 clrbits_le32(&scu->sysreset_ctrl1, reset_bit);
356 writel((RGMII2_TXCK_DUTY << SCU_CLKDUTY_RGMII2TXCK_SHIFT)
357 | (RGMII1_TXCK_DUTY << SCU_CLKDUTY_RGMII1TXCK_SHIFT),
362 return required_rate;
365 static ulong ast2500_configure_d2pll(struct ast2500_scu *scu, ulong rate)
368 * The values and the meaning of the next three
369 * parameters are undocumented. Taken from Aspeed SDK.
374 const u32 d2_pll_ext_param = 0x2c;
375 const u32 d2_pll_sip = 0x11;
376 const u32 d2_pll_sic = 0x18;
377 u32 clk_delay_settings =
378 (RMII_RXCLK_IDLY << SCU_MICDS_MAC1RMII_RDLY_SHIFT)
379 | (RMII_RXCLK_IDLY << SCU_MICDS_MAC2RMII_RDLY_SHIFT)
380 | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC1RGMII_TXDLY_SHIFT)
381 | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC2RGMII_TXDLY_SHIFT);
382 struct ast2500_div_config div_cfg = {
383 .num = SCU_D2PLL_NUM_MASK >> SCU_D2PLL_NUM_SHIFT,
384 .denum = SCU_D2PLL_DENUM_MASK >> SCU_D2PLL_DENUM_SHIFT,
385 .post_div = SCU_D2PLL_POST_MASK >> SCU_D2PLL_POST_SHIFT,
387 ulong clkin = ast2500_get_clkin(scu);
391 writel((d2_pll_ext_param << SCU_D2PLL_EXT1_PARAM_SHIFT)
393 | SCU_D2PLL_EXT1_RESET, &scu->d2_pll_ext_param[0]);
396 * Select USB2.0 port1 PHY clock as a clock source for GCRT.
397 * This would disconnect it from D2-PLL.
399 clrsetbits_le32(&scu->misc_ctrl1, SCU_MISC_D2PLL_OFF,
400 SCU_MISC_GCRT_USB20CLK);
402 new_rate = ast2500_calc_clock_config(clkin, rate, &div_cfg);
403 writel((d2_pll_sip << SCU_D2PLL_SIP_SHIFT)
404 | (d2_pll_sic << SCU_D2PLL_SIC_SHIFT)
405 | (div_cfg.num << SCU_D2PLL_NUM_SHIFT)
406 | (div_cfg.denum << SCU_D2PLL_DENUM_SHIFT)
407 | (div_cfg.post_div << SCU_D2PLL_POST_SHIFT),
410 clrbits_le32(&scu->d2_pll_ext_param[0],
411 SCU_D2PLL_EXT1_OFF | SCU_D2PLL_EXT1_RESET);
413 clrsetbits_le32(&scu->misc_ctrl2,
414 SCU_MISC2_RGMII_HPLL | SCU_MISC2_RMII_MPLL
415 | SCU_MISC2_RGMII_CLKDIV_MASK |
416 SCU_MISC2_RMII_CLKDIV_MASK,
417 (4 << SCU_MISC2_RMII_CLKDIV_SHIFT));
419 writel(clk_delay_settings | SCU_MICDS_RGMIIPLL, &scu->mac_clk_delay);
420 writel(clk_delay_settings, &scu->mac_clk_delay_100M);
421 writel(clk_delay_settings, &scu->mac_clk_delay_10M);
428 static ulong ast2500_clk_set_rate(struct clk *clk, ulong rate)
430 struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
436 new_rate = ast2500_configure_ddr(priv->scu, rate);
439 new_rate = ast2500_configure_d2pll(priv->scu, rate);
448 static int ast2500_clk_enable(struct clk *clk)
450 struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
454 if (readl(&priv->scu->clk_stop_ctrl1) & SCU_CLKSTOP_SDCLK) {
455 ast_scu_unlock(priv->scu);
457 setbits_le32(&priv->scu->sysreset_ctrl1,
460 clrbits_le32(&priv->scu->clk_stop_ctrl1,
463 clrbits_le32(&priv->scu->sysreset_ctrl1,
466 ast_scu_lock(priv->scu);
470 * For MAC clocks the clock rate is
471 * configured based on whether RGMII or RMII mode has been selected
472 * through hardware strapping.
475 ast2500_configure_mac(priv->scu, 1);
478 ast2500_configure_mac(priv->scu, 2);
481 ast2500_configure_d2pll(priv->scu, D2PLL_DEFAULT_RATE);
490 struct clk_ops ast2500_clk_ops = {
491 .get_rate = ast2500_clk_get_rate,
492 .set_rate = ast2500_clk_set_rate,
493 .enable = ast2500_clk_enable,
496 static int ast2500_clk_ofdata_to_platdata(struct udevice *dev)
498 struct ast2500_clk_priv *priv = dev_get_priv(dev);
500 priv->scu = devfdt_get_addr_ptr(dev);
501 if (IS_ERR(priv->scu))
502 return PTR_ERR(priv->scu);
507 static int ast2500_clk_bind(struct udevice *dev)
511 /* The reset driver does not have a device node, so bind it here */
512 ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
514 debug("Warning: No reset driver: ret=%d\n", ret);
519 static const struct udevice_id ast2500_clk_ids[] = {
520 { .compatible = "aspeed,ast2500-scu" },
524 U_BOOT_DRIVER(aspeed_ast2500_scu) = {
525 .name = "aspeed_ast2500_scu",
527 .of_match = ast2500_clk_ids,
528 .priv_auto_alloc_size = sizeof(struct ast2500_clk_priv),
529 .ops = &ast2500_clk_ops,
530 .bind = ast2500_clk_bind,
531 .ofdata_to_platdata = ast2500_clk_ofdata_to_platdata,