xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision 4f22e838bb6942458d16c0fad8ed0d29810830e1)
1550e691bSryan_chen // SPDX-License-Identifier: GPL-2.0
2550e691bSryan_chen /*
3550e691bSryan_chen  * Copyright (C) ASPEED Technology Inc.
4550e691bSryan_chen  * Ryan Chen <ryan_chen@aspeedtech.com>
5550e691bSryan_chen  */
6550e691bSryan_chen 
7550e691bSryan_chen #include <common.h>
8550e691bSryan_chen #include <clk-uclass.h>
9550e691bSryan_chen #include <dm.h>
10550e691bSryan_chen #include <asm/io.h>
11550e691bSryan_chen #include <dm/lists.h>
1262a6bcbfSryan_chen #include <asm/arch/scu_ast2600.h>
13d6e349c7Sryan_chen #include <dt-bindings/clock/ast2600-clock.h>
1439283ea7Sryan_chen #include <dt-bindings/reset/ast2600-reset.h>
15550e691bSryan_chen 
16550e691bSryan_chen /*
17550e691bSryan_chen  * MAC Clock Delay settings, taken from Aspeed SDK
18550e691bSryan_chen  */
19550e691bSryan_chen #define RGMII_TXCLK_ODLY		8
20550e691bSryan_chen #define RMII_RXCLK_IDLY		2
21550e691bSryan_chen 
22550e691bSryan_chen /*
23550e691bSryan_chen  * TGMII Clock Duty constants, taken from Aspeed SDK
24550e691bSryan_chen  */
25550e691bSryan_chen #define RGMII2_TXCK_DUTY	0x66
26550e691bSryan_chen #define RGMII1_TXCK_DUTY	0x64
27550e691bSryan_chen 
28550e691bSryan_chen #define D2PLL_DEFAULT_RATE	(250 * 1000 * 1000)
29550e691bSryan_chen 
30550e691bSryan_chen DECLARE_GLOBAL_DATA_PTR;
31550e691bSryan_chen 
32550e691bSryan_chen /*
33550e691bSryan_chen  * Clock divider/multiplier configuration struct.
34550e691bSryan_chen  * For H-PLL and M-PLL the formula is
35550e691bSryan_chen  * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
36550e691bSryan_chen  * M - Numerator
37550e691bSryan_chen  * N - Denumerator
38550e691bSryan_chen  * P - Post Divider
39550e691bSryan_chen  * They have the same layout in their control register.
40550e691bSryan_chen  *
41550e691bSryan_chen  * D-PLL and D2-PLL have extra divider (OD + 1), which is not
42550e691bSryan_chen  * yet needed and ignored by clock configurations.
43550e691bSryan_chen  */
4439283ea7Sryan_chen struct ast2600_div_config {
45550e691bSryan_chen 	unsigned int num;
46550e691bSryan_chen 	unsigned int denum;
47550e691bSryan_chen 	unsigned int post_div;
48550e691bSryan_chen };
49550e691bSryan_chen 
50550e691bSryan_chen /*
51550e691bSryan_chen  * Get the rate of the M-PLL clock from input clock frequency and
52550e691bSryan_chen  * the value of the M-PLL Parameter Register.
53550e691bSryan_chen  */
54*4f22e838Sryan_chen extern u32 ast2600_get_mpll_rate(struct ast2600_scu *scu)
55550e691bSryan_chen {
56d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
5739283ea7Sryan_chen 	u32 mpll_reg = readl(&scu->m_pll_param);
589639db61Sryan_chen 	unsigned int mult, div = 1;
59550e691bSryan_chen 
609639db61Sryan_chen 	if (mpll_reg & BIT(24)) {
619639db61Sryan_chen 		/* Pass through mode */
629639db61Sryan_chen 		mult = div = 1;
639639db61Sryan_chen 	} else {
649639db61Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */
659639db61Sryan_chen 		u32 m = mpll_reg  & 0x1fff;
669639db61Sryan_chen 		u32 n = (mpll_reg >> 13) & 0x3f;
679639db61Sryan_chen 		u32 p = (mpll_reg >> 19) & 0xf;
689639db61Sryan_chen 		mult = (m + 1) / (n + 1);
699639db61Sryan_chen 		div = (p + 1);
709639db61Sryan_chen 	}
719639db61Sryan_chen 	return ((clkin * mult)/div);
72550e691bSryan_chen 
73550e691bSryan_chen }
74550e691bSryan_chen 
75550e691bSryan_chen /*
76550e691bSryan_chen  * Get the rate of the H-PLL clock from input clock frequency and
77550e691bSryan_chen  * the value of the H-PLL Parameter Register.
78550e691bSryan_chen  */
79*4f22e838Sryan_chen extern u32 ast2600_get_hpll_rate(struct ast2600_scu *scu)
80550e691bSryan_chen {
81*4f22e838Sryan_chen 	u32 clkin = AST2600_CLK_IN;
8239283ea7Sryan_chen 	u32 hpll_reg = readl(&scu->h_pll_param);
83*4f22e838Sryan_chen 	unsigned int mult, div = 1;
84f0d895afSryan_chen 
85*4f22e838Sryan_chen 	if (hpll_reg & BIT(24)) {
86*4f22e838Sryan_chen 		/* Pass through mode */
87*4f22e838Sryan_chen 		mult = div = 1;
88*4f22e838Sryan_chen 	} else {
89*4f22e838Sryan_chen 		/* F = 25Mhz * [(M + 1) / (n + 1)] / (p + 1) */
90*4f22e838Sryan_chen 		u32 m = (hpll_reg & 0x1fff);
91*4f22e838Sryan_chen 		u32 n = (hpll_reg >> 13) & 0x3f;
92*4f22e838Sryan_chen 		u32 p = (hpll_reg >> 19) & 0xf;
93*4f22e838Sryan_chen 		mult = (m + 1) / (n + 1);
94*4f22e838Sryan_chen 		div = (p + 1);
95*4f22e838Sryan_chen 	}
96*4f22e838Sryan_chen 	return ((clkin * mult)/div);
97550e691bSryan_chen }
98550e691bSryan_chen 
99*4f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
100550e691bSryan_chen {
10139283ea7Sryan_chen 	u32 clk_in = AST2600_CLK_IN;
10239283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
10339283ea7Sryan_chen 	unsigned int mult, div = 1;
104d6e349c7Sryan_chen 
10539283ea7Sryan_chen 	if (apll_reg & BIT(20)) {
106d6e349c7Sryan_chen 		/* Pass through mode */
107d6e349c7Sryan_chen 		mult = div = 1;
108d6e349c7Sryan_chen 	} else {
109d6e349c7Sryan_chen 		/* F = 25Mhz * (2-OD) * [(M + 2) / (n + 1)] */
11039283ea7Sryan_chen 		u32 m = (apll_reg >> 5) & 0x3f;
11139283ea7Sryan_chen 		u32 od = (apll_reg >> 4) & 0x1;
11239283ea7Sryan_chen 		u32 n = apll_reg & 0xf;
113d6e349c7Sryan_chen 
11439283ea7Sryan_chen 		mult = (2 - od) * ((m + 2) / (n + 1));
115d6e349c7Sryan_chen 	}
116d6e349c7Sryan_chen 	return (clk_in * mult)/div;
117d6e349c7Sryan_chen }
118d6e349c7Sryan_chen 
119*4f22e838Sryan_chen extern u32 ast2600_get_epll_rate(struct ast2600_scu *scu)
12039283ea7Sryan_chen {
12139283ea7Sryan_chen 	u32 clk_in = AST2600_CLK_IN;
12239283ea7Sryan_chen 	u32 epll_reg = readl(&scu->e_pll_param);
12339283ea7Sryan_chen 	unsigned int mult, div = 1;
12439283ea7Sryan_chen 
12539283ea7Sryan_chen 	if (epll_reg & BIT(24)) {
12639283ea7Sryan_chen 		/* Pass through mode */
12739283ea7Sryan_chen 		mult = div = 1;
12839283ea7Sryan_chen 	} else {
12939283ea7Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1)*/
13039283ea7Sryan_chen 		u32 m = epll_reg  & 0x1fff;
13139283ea7Sryan_chen 		u32 n = (epll_reg >> 13) & 0x3f;
13239283ea7Sryan_chen 		u32 p = (epll_reg >> 19) & 0x7;
13339283ea7Sryan_chen 
13439283ea7Sryan_chen 		mult = ((m + 1) / (n + 1));
13539283ea7Sryan_chen 		div = (p + 1);
13639283ea7Sryan_chen 	}
13739283ea7Sryan_chen 	return (clk_in * mult)/div;
13839283ea7Sryan_chen }
13939283ea7Sryan_chen 
140*4f22e838Sryan_chen extern u32 ast2600_get_dpll_rate(struct ast2600_scu *scu)
14139283ea7Sryan_chen {
14239283ea7Sryan_chen 	u32 clk_in = AST2600_CLK_IN;
14339283ea7Sryan_chen 	u32 dpll_reg = readl(&scu->d_pll_param);
14439283ea7Sryan_chen 	unsigned int mult, div = 1;
14539283ea7Sryan_chen 
14639283ea7Sryan_chen 	if (dpll_reg & BIT(24)) {
14739283ea7Sryan_chen 		/* Pass through mode */
14839283ea7Sryan_chen 		mult = div = 1;
14939283ea7Sryan_chen 	} else {
15039283ea7Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1)*/
15139283ea7Sryan_chen 		u32 m = dpll_reg  & 0x1fff;
15239283ea7Sryan_chen 		u32 n = (dpll_reg >> 13) & 0x3f;
15339283ea7Sryan_chen 		u32 p = (dpll_reg >> 19) & 0x7;
15439283ea7Sryan_chen 		mult = ((m + 1) / (n + 1));
15539283ea7Sryan_chen 		div = (p + 1);
15639283ea7Sryan_chen 	}
15739283ea7Sryan_chen 	return (clk_in * mult)/div;
15839283ea7Sryan_chen }
159d6e349c7Sryan_chen 
160f0d895afSryan_chen static ulong ast2600_get_uart_clk_rate(struct ast2600_clk_priv *priv, int uart_index)
161d6e349c7Sryan_chen {
162550e691bSryan_chen 	ulong uart_clkin;
163550e691bSryan_chen 
164*4f22e838Sryan_chen 	printf("ast2600_get_uart_clk_rate source %d \n\n", ast2600_get_apll_rate(priv->scu));
165d6e349c7Sryan_chen 	return (24000000/13);
166d6e349c7Sryan_chen 
167f0d895afSryan_chen 	if (readl(&priv->scu->misc_ctrl2) &
168550e691bSryan_chen 	    (1 << (uart_index - 1 + SCU_MISC2_UARTCLK_SHIFT)))
169550e691bSryan_chen 		uart_clkin = 192 * 1000 * 1000;
170550e691bSryan_chen 	else
171550e691bSryan_chen 		uart_clkin = 24 * 1000 * 1000;
172550e691bSryan_chen 
173f0d895afSryan_chen 	if (readl(&priv->scu->misc_ctrl2) & SCU_MISC_UARTCLK_DIV13)
174550e691bSryan_chen 		uart_clkin /= 13;
175550e691bSryan_chen 
176550e691bSryan_chen 	return uart_clkin;
177550e691bSryan_chen }
178550e691bSryan_chen 
179550e691bSryan_chen struct aspeed_clock_config {
180550e691bSryan_chen 	ulong input_rate;
181550e691bSryan_chen 	ulong rate;
18239283ea7Sryan_chen 	struct ast2600_div_config cfg;
183550e691bSryan_chen };
184550e691bSryan_chen 
185550e691bSryan_chen static const struct aspeed_clock_config aspeed_clock_config_defaults[] = {
1861cd71a14SDylan Hung 	{ 25000000, 400000000, { .num = 95, .denum = 2, .post_div = 1 } },
187550e691bSryan_chen };
188550e691bSryan_chen 
189550e691bSryan_chen static bool aspeed_get_clock_config_default(ulong input_rate,
190550e691bSryan_chen 					     ulong requested_rate,
19139283ea7Sryan_chen 					     struct ast2600_div_config *cfg)
192550e691bSryan_chen {
193550e691bSryan_chen 	int i;
194550e691bSryan_chen 
195550e691bSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clock_config_defaults); i++) {
196550e691bSryan_chen 		const struct aspeed_clock_config *default_cfg =
197550e691bSryan_chen 			&aspeed_clock_config_defaults[i];
198550e691bSryan_chen 		if (default_cfg->input_rate == input_rate &&
199550e691bSryan_chen 		    default_cfg->rate == requested_rate) {
200550e691bSryan_chen 			*cfg = default_cfg->cfg;
201550e691bSryan_chen 			return true;
202550e691bSryan_chen 		}
203550e691bSryan_chen 	}
204550e691bSryan_chen 
205550e691bSryan_chen 	return false;
206550e691bSryan_chen }
207550e691bSryan_chen 
208550e691bSryan_chen /*
209550e691bSryan_chen  * @input_rate - the rate of input clock in Hz
210550e691bSryan_chen  * @requested_rate - desired output rate in Hz
211550e691bSryan_chen  * @div - this is an IN/OUT parameter, at input all fields of the config
212550e691bSryan_chen  * need to be set to their maximum allowed values.
213550e691bSryan_chen  * The result (the best config we could find), would also be returned
214550e691bSryan_chen  * in this structure.
215550e691bSryan_chen  *
216550e691bSryan_chen  * @return The clock rate, when the resulting div_config is used.
217550e691bSryan_chen  */
218550e691bSryan_chen static ulong aspeed_calc_clock_config(ulong input_rate, ulong requested_rate,
21939283ea7Sryan_chen 				       struct ast2600_div_config *cfg)
220550e691bSryan_chen {
221550e691bSryan_chen 	/*
222550e691bSryan_chen 	 * The assumption is that kHz precision is good enough and
223550e691bSryan_chen 	 * also enough to avoid overflow when multiplying.
224550e691bSryan_chen 	 */
225550e691bSryan_chen 	const ulong input_rate_khz = input_rate / 1000;
226550e691bSryan_chen 	const ulong rate_khz = requested_rate / 1000;
22739283ea7Sryan_chen 	const struct ast2600_div_config max_vals = *cfg;
22839283ea7Sryan_chen 	struct ast2600_div_config it = { 0, 0, 0 };
229550e691bSryan_chen 	ulong delta = rate_khz;
230550e691bSryan_chen 	ulong new_rate_khz = 0;
231550e691bSryan_chen 
232550e691bSryan_chen 	/*
233550e691bSryan_chen 	 * Look for a well known frequency first.
234550e691bSryan_chen 	 */
235550e691bSryan_chen 	if (aspeed_get_clock_config_default(input_rate, requested_rate, cfg))
236550e691bSryan_chen 		return requested_rate;
237550e691bSryan_chen 
238550e691bSryan_chen 	for (; it.denum <= max_vals.denum; ++it.denum) {
239550e691bSryan_chen 		for (it.post_div = 0; it.post_div <= max_vals.post_div;
240550e691bSryan_chen 		     ++it.post_div) {
241550e691bSryan_chen 			it.num = (rate_khz * (it.post_div + 1) / input_rate_khz)
242550e691bSryan_chen 			    * (it.denum + 1);
243550e691bSryan_chen 			if (it.num > max_vals.num)
244550e691bSryan_chen 				continue;
245550e691bSryan_chen 
246550e691bSryan_chen 			new_rate_khz = (input_rate_khz
247550e691bSryan_chen 					* ((it.num + 1) / (it.denum + 1)))
248550e691bSryan_chen 			    / (it.post_div + 1);
249550e691bSryan_chen 
250550e691bSryan_chen 			/* Keep the rate below requested one. */
251550e691bSryan_chen 			if (new_rate_khz > rate_khz)
252550e691bSryan_chen 				continue;
253550e691bSryan_chen 
254550e691bSryan_chen 			if (new_rate_khz - rate_khz < delta) {
255550e691bSryan_chen 				delta = new_rate_khz - rate_khz;
256550e691bSryan_chen 				*cfg = it;
257550e691bSryan_chen 				if (delta == 0)
258550e691bSryan_chen 					return new_rate_khz * 1000;
259550e691bSryan_chen 			}
260550e691bSryan_chen 		}
261550e691bSryan_chen 	}
262550e691bSryan_chen 
263550e691bSryan_chen 	return new_rate_khz * 1000;
264550e691bSryan_chen }
265550e691bSryan_chen 
26639283ea7Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_clk_priv *priv, ulong rate)
267550e691bSryan_chen {
268d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
269550e691bSryan_chen 	u32 mpll_reg;
27039283ea7Sryan_chen 	struct ast2600_div_config div_cfg = {
271550e691bSryan_chen 		.num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT),
272550e691bSryan_chen 		.denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT),
273550e691bSryan_chen 		.post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT),
274550e691bSryan_chen 	};
275550e691bSryan_chen 
276550e691bSryan_chen 	aspeed_calc_clock_config(clkin, rate, &div_cfg);
277550e691bSryan_chen 
278f0d895afSryan_chen 	mpll_reg = readl(&priv->scu->m_pll_param);
279550e691bSryan_chen 	mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK
280550e691bSryan_chen 		      | SCU_MPLL_DENUM_MASK);
281550e691bSryan_chen 	mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT)
282550e691bSryan_chen 	    | (div_cfg.num << SCU_MPLL_NUM_SHIFT)
283550e691bSryan_chen 	    | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT);
284550e691bSryan_chen 
285f0d895afSryan_chen 	writel(mpll_reg, &priv->scu->m_pll_param);
286550e691bSryan_chen 
28739283ea7Sryan_chen 	return ast2600_get_mpll_rate(priv->scu);
288550e691bSryan_chen }
289550e691bSryan_chen 
29086f91560Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
29186f91560Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
29286f91560Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
29386f91560Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
29486f91560Sryan_chen 
29500d2d4a5Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
296550e691bSryan_chen {
29700d2d4a5Sryan_chen 	u32 reset_bit;
29800d2d4a5Sryan_chen 	u32 clkstop_bit;
29900d2d4a5Sryan_chen 
30000d2d4a5Sryan_chen 
30100d2d4a5Sryan_chen 	switch (index) {
30200d2d4a5Sryan_chen 	case 1:
30300d2d4a5Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
30486f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
305*4f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
30686f91560Sryan_chen 		udelay(100);
307*4f22e838Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
30886f91560Sryan_chen 		mdelay(10);
309*4f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
31086f91560Sryan_chen 
31186f91560Sryan_chen 
31200d2d4a5Sryan_chen 		break;
31300d2d4a5Sryan_chen 	case 2:
31400d2d4a5Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
31586f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
316*4f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
31786f91560Sryan_chen 		udelay(100);
318*4f22e838Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
31986f91560Sryan_chen 		mdelay(10);
320*4f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
32100d2d4a5Sryan_chen 		break;
32200d2d4a5Sryan_chen 	case 3:
32386f91560Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
32486f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
32586f91560Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
32686f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
327*4f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
32886f91560Sryan_chen 		udelay(100);
329*4f22e838Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
33086f91560Sryan_chen 		mdelay(10);
331*4f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
33286f91560Sryan_chen 
33300d2d4a5Sryan_chen 		break;
33400d2d4a5Sryan_chen 	case 4:
33586f91560Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
33686f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
33786f91560Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
33886f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
339*4f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
34086f91560Sryan_chen 		udelay(100);
341*4f22e838Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
34286f91560Sryan_chen 		mdelay(10);
343*4f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
34486f91560Sryan_chen 
34500d2d4a5Sryan_chen 		break;
34600d2d4a5Sryan_chen 	default:
34700d2d4a5Sryan_chen 		return -EINVAL;
34800d2d4a5Sryan_chen 	}
34900d2d4a5Sryan_chen 
35039283ea7Sryan_chen 	return 0;
351550e691bSryan_chen }
352550e691bSryan_chen 
353f0d895afSryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
354f0d895afSryan_chen 	2, 2, 3, 5,
355f0d895afSryan_chen };
356f0d895afSryan_chen 
357f0d895afSryan_chen static u32 ast2600_a1_axi_ahb_div_table[] = {
358f0d895afSryan_chen 	4, 6, 2, 4,
359f0d895afSryan_chen };
360f0d895afSryan_chen 
361f0d895afSryan_chen static u32 ast2600_hpll_pclk_div_table[] = {
362f0d895afSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
363f0d895afSryan_chen };
364d6e349c7Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
365d6e349c7Sryan_chen {
366f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
367d6e349c7Sryan_chen 	ulong rate;
368d6e349c7Sryan_chen 
369d6e349c7Sryan_chen 	switch (clk->id) {
370d1e64dd1Sryan_chen 	//HPLL
371d1e64dd1Sryan_chen 	case ASPEED_CLK_HPLL:
37239283ea7Sryan_chen 		rate = ast2600_get_hpll_rate(priv->scu);
37339283ea7Sryan_chen 		printf("hpll %ld \n", rate);
374d6e349c7Sryan_chen 		break;
375d1e64dd1Sryan_chen 	//HCLK
376d1e64dd1Sryan_chen 	case ASPEED_CLK_AHB:
37762a6bcbfSryan_chen 		{
378f0d895afSryan_chen 			u32 hw_rev = readl(&priv->scu->chip_id0);
379f0d895afSryan_chen 			u32 hwstrap1 = readl(&priv->scu->hwstrap1);
380f0d895afSryan_chen 			u32 axi_div = 1;
381f0d895afSryan_chen 			u32 ahb_div = 0;
382f0d895afSryan_chen 			if((hwstrap1 >> 16) & 0x1)
383f0d895afSryan_chen 				axi_div = 1;
384f0d895afSryan_chen 			else
385f0d895afSryan_chen 				axi_div = 2;
38662a6bcbfSryan_chen 
387f0d895afSryan_chen 			if (hw_rev & BIT(16))
388f0d895afSryan_chen 				ahb_div = ast2600_a1_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
389f0d895afSryan_chen 			else
390f0d895afSryan_chen 				ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
391f0d895afSryan_chen 
39239283ea7Sryan_chen 			rate = ast2600_get_hpll_rate(priv->scu);
39362a6bcbfSryan_chen 			rate = rate / axi_div / ahb_div;
39400d2d4a5Sryan_chen 			printf("hclk %ld \n", rate);
39562a6bcbfSryan_chen 		}
396d1e64dd1Sryan_chen 		break;
397f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
39839283ea7Sryan_chen 		rate = ast2600_get_mpll_rate(priv->scu);
399d6e349c7Sryan_chen 		break;
40039283ea7Sryan_chen 	//pclk
40139283ea7Sryan_chen 	case ASPEED_CLK_APB:
402d6e349c7Sryan_chen 		{
403f0d895afSryan_chen 			u32 clk_sel1 = readl(&priv->scu->clk_sel1);
404f0d895afSryan_chen 			u32 apb_div = ast2600_hpll_pclk_div_table[((clk_sel1 >> 23) & 0x7)];
40539283ea7Sryan_chen 			rate = ast2600_get_hpll_rate(priv->scu);
406d6e349c7Sryan_chen 			rate = rate / apb_div;
407d6e349c7Sryan_chen 		}
408d6e349c7Sryan_chen 		break;
409d6e349c7Sryan_chen 	case PCLK_UART1:
410d6e349c7Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv, 1);
411d6e349c7Sryan_chen 		break;
412d6e349c7Sryan_chen 	case PCLK_UART2:
413d6e349c7Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv, 2);
414d6e349c7Sryan_chen 		break;
415d6e349c7Sryan_chen 	case PCLK_UART3:
416d6e349c7Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv, 3);
417d6e349c7Sryan_chen 		break;
418d6e349c7Sryan_chen 	case PCLK_UART4:
419d6e349c7Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv, 4);
420d6e349c7Sryan_chen 		break;
421d1e64dd1Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
422d6e349c7Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv, 5);
423d6e349c7Sryan_chen 		break;
424d6e349c7Sryan_chen 	default:
425d6e349c7Sryan_chen 		return -ENOENT;
426d6e349c7Sryan_chen 	}
427d6e349c7Sryan_chen 
428d6e349c7Sryan_chen 	return rate;
429d6e349c7Sryan_chen }
430d6e349c7Sryan_chen 
431d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
432550e691bSryan_chen {
433f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
434550e691bSryan_chen 
435550e691bSryan_chen 	ulong new_rate;
436550e691bSryan_chen 	switch (clk->id) {
437f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
43839283ea7Sryan_chen 		new_rate = ast2600_configure_ddr(priv, rate);
439550e691bSryan_chen 		break;
440550e691bSryan_chen 	default:
441550e691bSryan_chen 		return -ENOENT;
442550e691bSryan_chen 	}
443550e691bSryan_chen 
444550e691bSryan_chen 	return new_rate;
445550e691bSryan_chen }
446550e691bSryan_chen 
447d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
448550e691bSryan_chen {
449f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
450550e691bSryan_chen 
451550e691bSryan_chen 	switch (clk->id) {
452550e691bSryan_chen 	/*
453550e691bSryan_chen 	 * For MAC clocks the clock rate is
454550e691bSryan_chen 	 * configured based on whether RGMII or RMII mode has been selected
455550e691bSryan_chen 	 * through hardware strapping.
456550e691bSryan_chen 	 */
45786f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC1CLK:
45800d2d4a5Sryan_chen 		printf("ast2600_clk_enable mac 1 ~~~\n");
45986f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 1);
460550e691bSryan_chen 		break;
46186f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC2CLK:
46200d2d4a5Sryan_chen 		printf("ast2600_clk_enable mac 2 ~~~\n");
46386f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 2);
464550e691bSryan_chen 		break;
465550e691bSryan_chen 	default:
466550e691bSryan_chen 		return -ENOENT;
467550e691bSryan_chen 	}
468550e691bSryan_chen 
469550e691bSryan_chen 	return 0;
470550e691bSryan_chen }
471550e691bSryan_chen 
472550e691bSryan_chen struct clk_ops aspeed_clk_ops = {
473d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
474d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
475d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
476550e691bSryan_chen };
477550e691bSryan_chen 
478d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
479550e691bSryan_chen {
480f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
481550e691bSryan_chen 
482f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
483f0d895afSryan_chen 	if (IS_ERR(priv->scu))
484f0d895afSryan_chen 		return PTR_ERR(priv->scu);
485550e691bSryan_chen 
486550e691bSryan_chen 	return 0;
487550e691bSryan_chen }
488550e691bSryan_chen 
489d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
490550e691bSryan_chen {
491550e691bSryan_chen 	int ret;
492550e691bSryan_chen 
493550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
494550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
495550e691bSryan_chen 	if (ret)
496550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
497550e691bSryan_chen 
498550e691bSryan_chen 	return 0;
499550e691bSryan_chen }
500550e691bSryan_chen 
501d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
502d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
503550e691bSryan_chen 	{ }
504550e691bSryan_chen };
505550e691bSryan_chen 
506aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
507aa36597fSDylan Hung 	.name		= "aspeed_scu",
508550e691bSryan_chen 	.id		= UCLASS_CLK,
509d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
510f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
511550e691bSryan_chen 	.ops		= &aspeed_clk_ops,
512d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
513d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
514550e691bSryan_chen };
515