xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision f6110ecd6dbc383d972dcf0f4dd49ca0665edfb8)
1550e691bSryan_chen // SPDX-License-Identifier: GPL-2.0
2550e691bSryan_chen /*
3550e691bSryan_chen  * Copyright (C) ASPEED Technology Inc.
4550e691bSryan_chen  */
5550e691bSryan_chen 
6550e691bSryan_chen #include <common.h>
7550e691bSryan_chen #include <clk-uclass.h>
8550e691bSryan_chen #include <dm.h>
9550e691bSryan_chen #include <asm/io.h>
10550e691bSryan_chen #include <dm/lists.h>
1162a6bcbfSryan_chen #include <asm/arch/scu_ast2600.h>
12d6e349c7Sryan_chen #include <dt-bindings/clock/ast2600-clock.h>
1339283ea7Sryan_chen #include <dt-bindings/reset/ast2600-reset.h>
14550e691bSryan_chen 
15550e691bSryan_chen /*
16a8fc7648SRyan Chen  * MAC Clock Delay settings
17550e691bSryan_chen  */
18550e691bSryan_chen #define RGMII_TXCLK_ODLY		8
19550e691bSryan_chen #define RMII_RXCLK_IDLY			2
20550e691bSryan_chen 
213dc90377SDylan Hung #define MAC_DEF_DELAY_1G		0x0041b75d
2275605a4bSDylan Hung #define MAC_DEF_DELAY_100M		0x00417410
2375605a4bSDylan Hung #define MAC_DEF_DELAY_10M		0x00417410
2454f9cba1SDylan Hung 
253dc90377SDylan Hung #define MAC34_DEF_DELAY_1G		0x0010438a
2654f9cba1SDylan Hung #define MAC34_DEF_DELAY_100M	0x00104208
2754f9cba1SDylan Hung #define MAC34_DEF_DELAY_10M		0x00104208
284760b3f8SDylan Hung 
29550e691bSryan_chen /*
30550e691bSryan_chen  * TGMII Clock Duty constants, taken from Aspeed SDK
31550e691bSryan_chen  */
32550e691bSryan_chen #define RGMII2_TXCK_DUTY		0x66
33550e691bSryan_chen #define RGMII1_TXCK_DUTY		0x64
34550e691bSryan_chen #define D2PLL_DEFAULT_RATE		(250 * 1000 * 1000)
3585d48d8cSryan_chen #define CHIP_REVISION_ID		GENMASK(23, 16)
3685d48d8cSryan_chen 
37550e691bSryan_chen DECLARE_GLOBAL_DATA_PTR;
38550e691bSryan_chen 
39550e691bSryan_chen /*
40550e691bSryan_chen  * Clock divider/multiplier configuration struct.
41550e691bSryan_chen  * For H-PLL and M-PLL the formula is
42550e691bSryan_chen  * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
43550e691bSryan_chen  * M - Numerator
44550e691bSryan_chen  * N - Denumerator
45550e691bSryan_chen  * P - Post Divider
46550e691bSryan_chen  * They have the same layout in their control register.
47550e691bSryan_chen  *
48550e691bSryan_chen  * D-PLL and D2-PLL have extra divider (OD + 1), which is not
49550e691bSryan_chen  * yet needed and ignored by clock configurations.
50550e691bSryan_chen  */
51577fcdaeSDylan Hung union ast2600_pll_reg {
52334bd202SDylan Hung 	u32 w;
53577fcdaeSDylan Hung 	struct {
54fd52be0bSDylan Hung 		unsigned int m : 13;		/* bit[12:0]	*/
55fd52be0bSDylan Hung 		unsigned int n : 6;		/* bit[18:13]	*/
56fd52be0bSDylan Hung 		unsigned int p : 4;		/* bit[22:19]	*/
57fd52be0bSDylan Hung 		unsigned int off : 1;		/* bit[23]	*/
58fd52be0bSDylan Hung 		unsigned int bypass : 1;	/* bit[24]	*/
59fd52be0bSDylan Hung 		unsigned int reset : 1;		/* bit[25]	*/
60fd52be0bSDylan Hung 		unsigned int reserved : 6;	/* bit[31:26]	*/
61577fcdaeSDylan Hung 	} b;
62577fcdaeSDylan Hung };
63577fcdaeSDylan Hung 
64577fcdaeSDylan Hung struct ast2600_pll_cfg {
65577fcdaeSDylan Hung 	union ast2600_pll_reg reg;
66334bd202SDylan Hung 	u32 ext_reg;
67577fcdaeSDylan Hung };
68577fcdaeSDylan Hung 
69577fcdaeSDylan Hung struct ast2600_pll_desc {
70577fcdaeSDylan Hung 	u32 in;
71577fcdaeSDylan Hung 	u32 out;
72577fcdaeSDylan Hung 	struct ast2600_pll_cfg cfg;
73577fcdaeSDylan Hung };
74577fcdaeSDylan Hung 
75577fcdaeSDylan Hung static const struct ast2600_pll_desc ast2600_pll_lookup[] = {
76a8fc7648SRyan Chen 	{
775d05f4fcSRyan Chen 		.in = AST2600_CLK_IN,
785d05f4fcSRyan Chen 		.out = 400000000,
795d05f4fcSRyan Chen 		.cfg.reg.b.m = 95,
805d05f4fcSRyan Chen 		.cfg.reg.b.n = 2,
815d05f4fcSRyan Chen 		.cfg.reg.b.p = 1,
82577fcdaeSDylan Hung 		.cfg.ext_reg = 0x31,
83fa59add1SRyan Chen 	}, {
84fa59add1SRyan Chen 		.in = AST2600_CLK_IN,
855d05f4fcSRyan Chen 		.out = 200000000,
865d05f4fcSRyan Chen 		.cfg.reg.b.m = 127,
875d05f4fcSRyan Chen 		.cfg.reg.b.n = 0,
885d05f4fcSRyan Chen 		.cfg.reg.b.p = 15,
89fa59add1SRyan Chen 		.cfg.ext_reg = 0x3f,
90fa59add1SRyan Chen 	}, {
91fa59add1SRyan Chen 		.in = AST2600_CLK_IN,
925d05f4fcSRyan Chen 		.out = 334000000,
935d05f4fcSRyan Chen 		.cfg.reg.b.m = 667,
945d05f4fcSRyan Chen 		.cfg.reg.b.n = 4,
955d05f4fcSRyan Chen 		.cfg.reg.b.p = 9,
96fa59add1SRyan Chen 		.cfg.ext_reg = 0x14d,
97fa59add1SRyan Chen 	}, {
98fa59add1SRyan Chen 		.in = AST2600_CLK_IN,
995d05f4fcSRyan Chen 		.out = 1000000000,
1005d05f4fcSRyan Chen 		.cfg.reg.b.m = 119,
1015d05f4fcSRyan Chen 		.cfg.reg.b.n = 2,
1025d05f4fcSRyan Chen 		.cfg.reg.b.p = 0,
103fa59add1SRyan Chen 		.cfg.ext_reg = 0x3d,
104fa59add1SRyan Chen 	}, {
105fa59add1SRyan Chen 		.in = AST2600_CLK_IN,
1065d05f4fcSRyan Chen 		.out = 50000000,
1075d05f4fcSRyan Chen 		.cfg.reg.b.m = 95,
1085d05f4fcSRyan Chen 		.cfg.reg.b.n = 2,
1095d05f4fcSRyan Chen 		.cfg.reg.b.p = 15,
110fa59add1SRyan Chen 		.cfg.ext_reg = 0x31,
111fa59add1SRyan Chen 	},
112550e691bSryan_chen };
113550e691bSryan_chen 
114a98c71fbSDylan Hung union mac_delay_1g {
115a98c71fbSDylan Hung 	u32 w;
116a98c71fbSDylan Hung 	struct {
117a98c71fbSDylan Hung 		unsigned int tx_delay_1		: 6;	/* bit[5:0] */
118a98c71fbSDylan Hung 		unsigned int tx_delay_2		: 6;	/* bit[11:6] */
119a98c71fbSDylan Hung 		unsigned int rx_delay_1		: 6;	/* bit[17:12] */
120a98c71fbSDylan Hung 		unsigned int rx_delay_2		: 6;	/* bit[23:18] */
121a98c71fbSDylan Hung 		unsigned int rx_clk_inv_1 	: 1;	/* bit[24] */
122a98c71fbSDylan Hung 		unsigned int rx_clk_inv_2 	: 1;	/* bit[25] */
123a98c71fbSDylan Hung 		unsigned int rmii_tx_data_at_falling_1 : 1; /* bit[26] */
124a98c71fbSDylan Hung 		unsigned int rmii_tx_data_at_falling_2 : 1; /* bit[27] */
125a98c71fbSDylan Hung 		unsigned int rgmiick_pad_dir	: 1;	/* bit[28] */
126a98c71fbSDylan Hung 		unsigned int rmii_50m_oe_1 	: 1;	/* bit[29] */
127a98c71fbSDylan Hung 		unsigned int rmii_50m_oe_2	: 1;	/* bit[30] */
128a98c71fbSDylan Hung 		unsigned int rgmii_125m_o_sel 	: 1;	/* bit[31] */
129a98c71fbSDylan Hung 	} b;
130a98c71fbSDylan Hung };
131a98c71fbSDylan Hung 
132a98c71fbSDylan Hung union mac_delay_100_10 {
133a98c71fbSDylan Hung 	u32 w;
134a98c71fbSDylan Hung 	struct {
135a98c71fbSDylan Hung 		unsigned int tx_delay_1		: 6;	/* bit[5:0] */
136a98c71fbSDylan Hung 		unsigned int tx_delay_2		: 6;	/* bit[11:6] */
137a98c71fbSDylan Hung 		unsigned int rx_delay_1		: 6;	/* bit[17:12] */
138a98c71fbSDylan Hung 		unsigned int rx_delay_2		: 6;	/* bit[23:18] */
139a98c71fbSDylan Hung 		unsigned int rx_clk_inv_1 	: 1;	/* bit[24] */
140a98c71fbSDylan Hung 		unsigned int rx_clk_inv_2 	: 1;	/* bit[25] */
141a98c71fbSDylan Hung 		unsigned int reserved_0 	: 6;	/* bit[31:26] */
142a98c71fbSDylan Hung 	} b;
143a98c71fbSDylan Hung };
144a98c71fbSDylan Hung 
145a98c71fbSDylan Hung struct mac_delay_config {
146a98c71fbSDylan Hung 	u32 tx_delay_1000;
147a98c71fbSDylan Hung 	u32 rx_delay_1000;
148a98c71fbSDylan Hung 	u32 tx_delay_100;
149a98c71fbSDylan Hung 	u32 rx_delay_100;
150a98c71fbSDylan Hung 	u32 tx_delay_10;
151a98c71fbSDylan Hung 	u32 rx_delay_10;
152a98c71fbSDylan Hung };
153a98c71fbSDylan Hung 
154bbbfb0c5Sryan_chen extern u32 ast2600_get_pll_rate(struct ast2600_scu *scu, int pll_idx)
155550e691bSryan_chen {
156d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
157bbbfb0c5Sryan_chen 	u32 pll_reg = 0;
1589639db61Sryan_chen 	unsigned int mult, div = 1;
159550e691bSryan_chen 
160bbbfb0c5Sryan_chen 	switch (pll_idx) {
161bbbfb0c5Sryan_chen 	case ASPEED_CLK_HPLL:
162bbbfb0c5Sryan_chen 		pll_reg = readl(&scu->h_pll_param);
163bbbfb0c5Sryan_chen 		break;
164bbbfb0c5Sryan_chen 	case ASPEED_CLK_MPLL:
165bbbfb0c5Sryan_chen 		pll_reg = readl(&scu->m_pll_param);
166bbbfb0c5Sryan_chen 		break;
167bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
168bbbfb0c5Sryan_chen 		pll_reg = readl(&scu->d_pll_param);
169bbbfb0c5Sryan_chen 		break;
170bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
171bbbfb0c5Sryan_chen 		pll_reg = readl(&scu->e_pll_param);
172bbbfb0c5Sryan_chen 		break;
173bbbfb0c5Sryan_chen 	}
174bbbfb0c5Sryan_chen 	if (pll_reg & BIT(24)) {
1759639db61Sryan_chen 		/* Pass through mode */
176ed3899c5SRyan Chen 		mult = 1;
177ed3899c5SRyan Chen 		div = 1;
1789639db61Sryan_chen 	} else {
17975ced45aSDylan Hung 		union ast2600_pll_reg reg;
180ed3899c5SRyan Chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1)
181ed3899c5SRyan Chen 		 * HPLL Numerator (M) = fix 0x5F when SCU500[10]=1
182ed3899c5SRyan Chen 		 * Fixed 0xBF when SCU500[10]=0 and SCU500[8]=1
183ed3899c5SRyan Chen 		 * SCU200[12:0] (default 0x8F) when SCU510[10]=0 and SCU510[8]=0
184ed3899c5SRyan Chen 		 * HPLL Denumerator (N) =	SCU200[18:13] (default 0x2)
185ed3899c5SRyan Chen 		 * HPLL Divider (P)	 =	SCU200[22:19] (default 0x0)
186ed3899c5SRyan Chen 		 * HPLL Bandwidth Adj (NB) =  fix 0x2F when SCU500[10]=1
187ed3899c5SRyan Chen 		 * Fixed 0x5F when SCU500[10]=0 and SCU500[8]=1
188ed3899c5SRyan Chen 		 * SCU204[11:0] (default 0x31) when SCU500[10]=0 and SCU500[8]=0
189e5c4f4dfSryan_chen 		 */
190ed3899c5SRyan Chen 		reg.w = pll_reg;
191f27685ebSRyan Chen 		if (pll_idx == ASPEED_CLK_HPLL) {
192e5c4f4dfSryan_chen 			u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
193ed3899c5SRyan Chen 
194ed3899c5SRyan Chen 			if (hwstrap1 & BIT(10)) {
195e5c4f4dfSryan_chen 				reg.b.m = 0x5F;
196ed3899c5SRyan Chen 			} else {
197e5c4f4dfSryan_chen 				if (hwstrap1 & BIT(8))
198e5c4f4dfSryan_chen 					reg.b.m = 0xBF;
199a8fc7648SRyan Chen 				/* Otherwise keep default 0x8F */
200e5c4f4dfSryan_chen 			}
201e5c4f4dfSryan_chen 		}
20275ced45aSDylan Hung 		mult = (reg.b.m + 1) / (reg.b.n + 1);
20375ced45aSDylan Hung 		div = (reg.b.p + 1);
2049639db61Sryan_chen 	}
205a8fc7648SRyan Chen 
2069639db61Sryan_chen 	return ((clkin * mult) / div);
207550e691bSryan_chen }
208550e691bSryan_chen 
2094f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
210550e691bSryan_chen {
21185d48d8cSryan_chen 	u32 hw_rev = readl(&scu->chip_id1);
212bbbfb0c5Sryan_chen 	u32 clkin = AST2600_CLK_IN;
21339283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
21439283ea7Sryan_chen 	unsigned int mult, div = 1;
215d6e349c7Sryan_chen 
2168d615c79SRyan Chen 	if (((hw_rev & CHIP_REVISION_ID) >> 16) >= 2) {
217a8fc7648SRyan Chen 		//after A2 version
21885d48d8cSryan_chen 		if (apll_reg & BIT(24)) {
21985d48d8cSryan_chen 			/* Pass through mode */
220ed3899c5SRyan Chen 			mult = 1;
221ed3899c5SRyan Chen 			div = 1;
22285d48d8cSryan_chen 		} else {
22385d48d8cSryan_chen 			/* F = 25Mhz * [(m + 1) / (n + 1)] / (p + 1) */
22485d48d8cSryan_chen 			u32 m = apll_reg & 0x1fff;
22585d48d8cSryan_chen 			u32 n = (apll_reg >> 13) & 0x3f;
22685d48d8cSryan_chen 			u32 p = (apll_reg >> 19) & 0xf;
22785d48d8cSryan_chen 
22885d48d8cSryan_chen 			mult = (m + 1);
22985d48d8cSryan_chen 			div = (n + 1) * (p + 1);
23085d48d8cSryan_chen 		}
23185d48d8cSryan_chen 	} else {
23239283ea7Sryan_chen 		if (apll_reg & BIT(20)) {
233d6e349c7Sryan_chen 			/* Pass through mode */
234ed3899c5SRyan Chen 			mult = 1;
235ed3899c5SRyan Chen 			div = 1;
236d6e349c7Sryan_chen 		} else {
237bbbfb0c5Sryan_chen 			/* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
23839283ea7Sryan_chen 			u32 m = (apll_reg >> 5) & 0x3f;
23939283ea7Sryan_chen 			u32 od = (apll_reg >> 4) & 0x1;
24039283ea7Sryan_chen 			u32 n = apll_reg & 0xf;
241d6e349c7Sryan_chen 
242bbbfb0c5Sryan_chen 			mult = (2 - od) * (m + 2);
243bbbfb0c5Sryan_chen 			div = n + 1;
244d6e349c7Sryan_chen 		}
24585d48d8cSryan_chen 	}
246a8fc7648SRyan Chen 
247bbbfb0c5Sryan_chen 	return ((clkin * mult) / div);
24839283ea7Sryan_chen }
24939283ea7Sryan_chen 
250d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
2515d05f4fcSRyan Chen 	2,
2525d05f4fcSRyan Chen 	2,
2535d05f4fcSRyan Chen 	3,
2545d05f4fcSRyan Chen 	4,
255d812df15Sryan_chen };
256d812df15Sryan_chen 
25745e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div0_table[] = {
2585d05f4fcSRyan Chen 	3,
2595d05f4fcSRyan Chen 	2,
2605d05f4fcSRyan Chen 	3,
2615d05f4fcSRyan Chen 	4,
26245e0908aSryan_chen };
26345e0908aSryan_chen 
26445e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div1_table[] = {
2655d05f4fcSRyan Chen 	3,
2665d05f4fcSRyan Chen 	4,
2675d05f4fcSRyan Chen 	6,
2685d05f4fcSRyan Chen 	8,
269e29dc694Sryan_chen };
270e29dc694Sryan_chen 
271e29dc694Sryan_chen static u32 ast2600_a1_axi_ahb_default_table[] = {
272e29dc694Sryan_chen 	3, 4, 3, 4, 2, 2, 2, 2,
273d812df15Sryan_chen };
274d812df15Sryan_chen 
275d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
276d812df15Sryan_chen {
27785d48d8cSryan_chen 	u32 hw_rev = readl(&scu->chip_id1);
27845e0908aSryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
279d812df15Sryan_chen 	u32 axi_div = 1;
280d812df15Sryan_chen 	u32 ahb_div = 0;
281d812df15Sryan_chen 	u32 rate = 0;
282d812df15Sryan_chen 
28385d48d8cSryan_chen 	if ((hw_rev & CHIP_REVISION_ID) >> 16) {
284a8fc7648SRyan Chen 		//After A0
28545e0908aSryan_chen 		if (hwstrap1 & BIT(16)) {
286a8fc7648SRyan Chen 			ast2600_a1_axi_ahb_div1_table[0] =
2875d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) &
28822545706SRyan Chen 								 0x7] * 2;
289d812df15Sryan_chen 			axi_div = 1;
2905d05f4fcSRyan Chen 			ahb_div =
2915d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_div1_table[(hwstrap1 >> 11) &
2925d05f4fcSRyan Chen 							      0x3];
29345e0908aSryan_chen 		} else {
294a8fc7648SRyan Chen 			ast2600_a1_axi_ahb_div0_table[0] =
2955d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) &
29622545706SRyan Chen 								 0x7];
297d812df15Sryan_chen 			axi_div = 2;
2985d05f4fcSRyan Chen 			ahb_div =
2995d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_div0_table[(hwstrap1 >> 11) &
3005d05f4fcSRyan Chen 							      0x3];
30145e0908aSryan_chen 		}
30245e0908aSryan_chen 	} else {
303a8fc7648SRyan Chen 		//A0 : fix axi = hpll / 2
30445e0908aSryan_chen 		axi_div = 2;
305d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
30645e0908aSryan_chen 	}
307bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
308a8fc7648SRyan Chen 
3092717883aSryan_chen 	return (rate / axi_div / ahb_div);
3102717883aSryan_chen }
3112717883aSryan_chen 
312c304f173Sryan_chen static u32 ast2600_get_bclk_rate(struct ast2600_scu *scu)
313c304f173Sryan_chen {
314c304f173Sryan_chen 	u32 rate;
315c304f173Sryan_chen 	u32 bclk_sel = (readl(&scu->clk_sel1) >> 20) & 0x7;
316ed3899c5SRyan Chen 
317c304f173Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
318c304f173Sryan_chen 
319c304f173Sryan_chen 	return (rate / ((bclk_sel + 1) * 4));
320c304f173Sryan_chen }
321c304f173Sryan_chen 
3226fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = {
3232717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
3242717883aSryan_chen };
3252717883aSryan_chen 
3266fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = {
3276fa1ef3dSryan_chen 	2, 4, 6, 8, 10, 12, 14, 16,
3286fa1ef3dSryan_chen };
3296fa1ef3dSryan_chen 
3306fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu)
3312717883aSryan_chen {
3322717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
3336fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)];
334bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
3352717883aSryan_chen 
3362717883aSryan_chen 	return (rate / apb_div);
337d812df15Sryan_chen }
338d812df15Sryan_chen 
3396fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu)
3406fa1ef3dSryan_chen {
3416fa1ef3dSryan_chen 	u32 clk_sel4 = readl(&scu->clk_sel4);
3426fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)];
3436fa1ef3dSryan_chen 	u32 rate = ast2600_get_hclk(scu);
3446fa1ef3dSryan_chen 
3456fa1ef3dSryan_chen 	return (rate / apb_div);
3466fa1ef3dSryan_chen }
3476fa1ef3dSryan_chen 
3482e195992Sryan_chen static u32 ast2600_get_uxclk_in_rate(struct ast2600_scu *scu)
349d6e349c7Sryan_chen {
35027881d20Sryan_chen 	u32 clk_in = 0;
3512e195992Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel5);
352550e691bSryan_chen 
35327881d20Sryan_chen 	uxclk_sel &= 0x3;
35427881d20Sryan_chen 	switch (uxclk_sel) {
35527881d20Sryan_chen 	case 0:
35627881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 4;
35727881d20Sryan_chen 		break;
35827881d20Sryan_chen 	case 1:
35927881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 2;
36027881d20Sryan_chen 		break;
36127881d20Sryan_chen 	case 2:
36227881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu);
36327881d20Sryan_chen 		break;
36427881d20Sryan_chen 	case 3:
36527881d20Sryan_chen 		clk_in = ast2600_get_hclk(scu);
36627881d20Sryan_chen 		break;
36727881d20Sryan_chen 	}
368d6e349c7Sryan_chen 
36927881d20Sryan_chen 	return clk_in;
37027881d20Sryan_chen }
37127881d20Sryan_chen 
3722e195992Sryan_chen static u32 ast2600_get_huxclk_in_rate(struct ast2600_scu *scu)
37327881d20Sryan_chen {
37427881d20Sryan_chen 	u32 clk_in = 0;
3752e195992Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel5);
37627881d20Sryan_chen 
37727881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
37827881d20Sryan_chen 	switch (huclk_sel) {
37927881d20Sryan_chen 	case 0:
38027881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 4;
38127881d20Sryan_chen 		break;
38227881d20Sryan_chen 	case 1:
38327881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 2;
38427881d20Sryan_chen 		break;
38527881d20Sryan_chen 	case 2:
38627881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu);
38727881d20Sryan_chen 		break;
38827881d20Sryan_chen 	case 3:
38927881d20Sryan_chen 		clk_in = ast2600_get_hclk(scu);
39027881d20Sryan_chen 		break;
39127881d20Sryan_chen 	}
39227881d20Sryan_chen 
39327881d20Sryan_chen 	return clk_in;
39427881d20Sryan_chen }
39527881d20Sryan_chen 
3962e195992Sryan_chen static u32 ast2600_get_uart_uxclk_rate(struct ast2600_scu *scu)
39727881d20Sryan_chen {
3982e195992Sryan_chen 	u32 clk_in = ast2600_get_uxclk_in_rate(scu);
39927881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
40027881d20Sryan_chen 	unsigned int mult, div;
40127881d20Sryan_chen 
40227881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
40327881d20Sryan_chen 	u32 r = div_reg & 0xff;
40427881d20Sryan_chen 
40527881d20Sryan_chen 	mult = r;
4062e195992Sryan_chen 	div = (n * 2);
40727881d20Sryan_chen 	return (clk_in * mult) / div;
40827881d20Sryan_chen }
40927881d20Sryan_chen 
4102e195992Sryan_chen static u32 ast2600_get_uart_huxclk_rate(struct ast2600_scu *scu)
41127881d20Sryan_chen {
4122e195992Sryan_chen 	u32 clk_in = ast2600_get_huxclk_in_rate(scu);
41327881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
41427881d20Sryan_chen 
41527881d20Sryan_chen 	unsigned int mult, div;
41627881d20Sryan_chen 
41727881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
41827881d20Sryan_chen 	u32 r = div_reg & 0xff;
41927881d20Sryan_chen 
42027881d20Sryan_chen 	mult = r;
4212e195992Sryan_chen 	div = (n * 2);
42227881d20Sryan_chen 	return (clk_in * mult) / div;
42327881d20Sryan_chen }
42427881d20Sryan_chen 
425f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
426f51926eeSryan_chen {
427f51926eeSryan_chen 	u32 clkin = 0;
428f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
429f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
430f51926eeSryan_chen 
431ed3899c5SRyan Chen 	if (clk_sel & BIT(8))
432f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
433ed3899c5SRyan Chen 	else
43410069884Sryan_chen 		clkin = ast2600_get_hclk(scu);
435ed3899c5SRyan Chen 
436f51926eeSryan_chen 	div = (div + 1) << 1;
437f51926eeSryan_chen 
438f51926eeSryan_chen 	return (clkin / div);
439f51926eeSryan_chen }
440f51926eeSryan_chen 
441f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
442f51926eeSryan_chen {
443125f2e11SChin-Ting Kuo 	u32 mmc_clk_src = readl(&scu->clk_sel1);
444125f2e11SChin-Ting Kuo 	u32 clkin;
445f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
446f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
447f51926eeSryan_chen 
448125f2e11SChin-Ting Kuo 	if (mmc_clk_src & BIT(11)) {
449125f2e11SChin-Ting Kuo 		/* emmc clock comes from MPLL */
450125f2e11SChin-Ting Kuo 		clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
451125f2e11SChin-Ting Kuo 		div = (div + 1) * 2;
452125f2e11SChin-Ting Kuo 	} else {
453125f2e11SChin-Ting Kuo 		clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
454f51926eeSryan_chen 		div = (div + 1) << 2;
455125f2e11SChin-Ting Kuo 	}
456f51926eeSryan_chen 
457f51926eeSryan_chen 	return (clkin / div);
458f51926eeSryan_chen }
459f51926eeSryan_chen 
460f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
46127881d20Sryan_chen {
46227881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
46327881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
46427881d20Sryan_chen 	ulong uart_clk = 0;
46527881d20Sryan_chen 
46627881d20Sryan_chen 	switch (uart_idx) {
46727881d20Sryan_chen 	case 1:
46827881d20Sryan_chen 	case 2:
46927881d20Sryan_chen 	case 3:
47027881d20Sryan_chen 	case 4:
47127881d20Sryan_chen 	case 6:
47227881d20Sryan_chen 		if (uart_sel & BIT(uart_idx - 1))
4732e195992Sryan_chen 			uart_clk = ast2600_get_uart_huxclk_rate(scu);
474550e691bSryan_chen 		else
4752e195992Sryan_chen 			uart_clk = ast2600_get_uart_uxclk_rate(scu);
47627881d20Sryan_chen 		break;
47727881d20Sryan_chen 	case 5: //24mhz is come form usb phy 48Mhz
47827881d20Sryan_chen 	{
47927881d20Sryan_chen 		u8 uart5_clk_sel = 0;
48027881d20Sryan_chen 		//high bit
48127881d20Sryan_chen 		if (readl(&scu->misc_ctrl1) & BIT(12))
48227881d20Sryan_chen 			uart5_clk_sel = 0x2;
48327881d20Sryan_chen 		else
48427881d20Sryan_chen 			uart5_clk_sel = 0x0;
485550e691bSryan_chen 
48627881d20Sryan_chen 		if (readl(&scu->clk_sel2) & BIT(14))
48727881d20Sryan_chen 			uart5_clk_sel |= 0x1;
488550e691bSryan_chen 
48927881d20Sryan_chen 		switch (uart5_clk_sel) {
49027881d20Sryan_chen 		case 0:
49127881d20Sryan_chen 			uart_clk = 24000000;
49227881d20Sryan_chen 			break;
49327881d20Sryan_chen 		case 1:
494def99fcbSryan_chen 			uart_clk = 192000000;
49527881d20Sryan_chen 			break;
49627881d20Sryan_chen 		case 2:
49727881d20Sryan_chen 			uart_clk = 24000000 / 13;
49827881d20Sryan_chen 			break;
49927881d20Sryan_chen 		case 3:
50027881d20Sryan_chen 			uart_clk = 192000000 / 13;
50127881d20Sryan_chen 			break;
50227881d20Sryan_chen 		}
5035d05f4fcSRyan Chen 	} break;
50427881d20Sryan_chen 	case 7:
50527881d20Sryan_chen 	case 8:
50627881d20Sryan_chen 	case 9:
50727881d20Sryan_chen 	case 10:
50827881d20Sryan_chen 	case 11:
50927881d20Sryan_chen 	case 12:
51027881d20Sryan_chen 	case 13:
51127881d20Sryan_chen 		if (uart_sel5 & BIT(uart_idx - 1))
5122e195992Sryan_chen 			uart_clk = ast2600_get_uart_huxclk_rate(scu);
51327881d20Sryan_chen 		else
5142e195992Sryan_chen 			uart_clk = ast2600_get_uart_uxclk_rate(scu);
51527881d20Sryan_chen 		break;
51627881d20Sryan_chen 	}
51727881d20Sryan_chen 
51827881d20Sryan_chen 	return uart_clk;
519550e691bSryan_chen }
520550e691bSryan_chen 
521feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
522feb42054Sryan_chen {
523feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
524feb42054Sryan_chen 	ulong rate = 0;
525feb42054Sryan_chen 
526feb42054Sryan_chen 	switch (clk->id) {
527feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
528bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
529bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
530d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
531bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
532d812df15Sryan_chen 		break;
533feb42054Sryan_chen 	case ASPEED_CLK_AHB:
534feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
535feb42054Sryan_chen 		break;
5366fa1ef3dSryan_chen 	case ASPEED_CLK_APB1:
5376fa1ef3dSryan_chen 		rate = ast2600_get_pclk1(priv->scu);
5386fa1ef3dSryan_chen 		break;
5396fa1ef3dSryan_chen 	case ASPEED_CLK_APB2:
5406fa1ef3dSryan_chen 		rate = ast2600_get_pclk2(priv->scu);
541feb42054Sryan_chen 		break;
542bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
543bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
544bbbfb0c5Sryan_chen 		break;
545feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
546feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
547feb42054Sryan_chen 		break;
548feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
549feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
550feb42054Sryan_chen 		break;
551feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
552feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
553feb42054Sryan_chen 		break;
554feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
555feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
556feb42054Sryan_chen 		break;
557feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
558feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
559feb42054Sryan_chen 		break;
560c304f173Sryan_chen 	case ASPEED_CLK_BCLK:
561c304f173Sryan_chen 		rate = ast2600_get_bclk_rate(priv->scu);
562c304f173Sryan_chen 		break;
563f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
564f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
565f51926eeSryan_chen 		break;
566f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
567f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
568f51926eeSryan_chen 		break;
5692e195992Sryan_chen 	case ASPEED_CLK_UARTX:
5702e195992Sryan_chen 		rate = ast2600_get_uart_uxclk_rate(priv->scu);
5712e195992Sryan_chen 		break;
5720998ddefSryan_chen 	case ASPEED_CLK_HUARTX:
5732e195992Sryan_chen 		rate = ast2600_get_uart_huxclk_rate(priv->scu);
5742e195992Sryan_chen 		break;
575feb42054Sryan_chen 	default:
576d812df15Sryan_chen 		pr_debug("can't get clk rate\n");
577feb42054Sryan_chen 		return -ENOENT;
578feb42054Sryan_chen 	}
579feb42054Sryan_chen 
580feb42054Sryan_chen 	return rate;
581feb42054Sryan_chen }
582feb42054Sryan_chen 
583577fcdaeSDylan Hung /**
584577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
585577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
586577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
587a8fc7648SRyan Chen  * The function caller shall fill "pll->in" and "pll->out",
588a8fc7648SRyan Chen  * then this function will search the lookup table
589a8fc7648SRyan Chen  * to find a valid PLL divider configuration.
590550e691bSryan_chen  */
591577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
592550e691bSryan_chen {
593577fcdaeSDylan Hung 	u32 i;
594577fcdaeSDylan Hung 	bool is_found = false;
595550e691bSryan_chen 
596577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
597577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
598ed3899c5SRyan Chen 
599ed3899c5SRyan Chen 		if (def_cfg->in == pll->in && def_cfg->out == pll->out) {
600577fcdaeSDylan Hung 			is_found = true;
601577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
602577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
603577fcdaeSDylan Hung 			break;
604550e691bSryan_chen 		}
605550e691bSryan_chen 	}
606577fcdaeSDylan Hung 	return is_found;
607550e691bSryan_chen }
608ed3899c5SRyan Chen 
609fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu,
610fd52be0bSDylan Hung 				 struct ast2600_pll_cfg *p_cfg, int pll_idx)
611fd52be0bSDylan Hung {
612fd52be0bSDylan Hung 	u32 addr, addr_ext;
613fd52be0bSDylan Hung 	u32 reg;
614550e691bSryan_chen 
615fd52be0bSDylan Hung 	switch (pll_idx) {
616fd52be0bSDylan Hung 	case ASPEED_CLK_HPLL:
617fd52be0bSDylan Hung 		addr = (u32)(&scu->h_pll_param);
618fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->h_pll_ext_param);
619fd52be0bSDylan Hung 		break;
620fd52be0bSDylan Hung 	case ASPEED_CLK_MPLL:
621fd52be0bSDylan Hung 		addr = (u32)(&scu->m_pll_param);
622fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->m_pll_ext_param);
623fd52be0bSDylan Hung 		break;
624fd52be0bSDylan Hung 	case ASPEED_CLK_DPLL:
625fd52be0bSDylan Hung 		addr = (u32)(&scu->d_pll_param);
626fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->d_pll_ext_param);
627fd52be0bSDylan Hung 		break;
628fd52be0bSDylan Hung 	case ASPEED_CLK_EPLL:
629fd52be0bSDylan Hung 		addr = (u32)(&scu->e_pll_param);
630fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->e_pll_ext_param);
631fd52be0bSDylan Hung 		break;
632fd52be0bSDylan Hung 	default:
633fd52be0bSDylan Hung 		debug("unknown PLL index\n");
634fd52be0bSDylan Hung 		return 1;
635fd52be0bSDylan Hung 	}
636fd52be0bSDylan Hung 
637fd52be0bSDylan Hung 	p_cfg->reg.b.bypass = 0;
638fd52be0bSDylan Hung 	p_cfg->reg.b.off = 1;
639fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 1;
640fd52be0bSDylan Hung 
641fd52be0bSDylan Hung 	reg = readl(addr);
642fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
643fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
644fd52be0bSDylan Hung 	writel(reg, addr);
645fd52be0bSDylan Hung 
646fd52be0bSDylan Hung 	/* write extend parameter */
647fd52be0bSDylan Hung 	writel(p_cfg->ext_reg, addr_ext);
648fd52be0bSDylan Hung 	udelay(100);
649fd52be0bSDylan Hung 	p_cfg->reg.b.off = 0;
650fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 0;
651fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
652fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
653fd52be0bSDylan Hung 	writel(reg, addr);
654ed3899c5SRyan Chen 	while (!(readl(addr_ext) & BIT(31)))
655ed3899c5SRyan Chen 		;
656fd52be0bSDylan Hung 
657fd52be0bSDylan Hung 	return 0;
658fd52be0bSDylan Hung }
659ed3899c5SRyan Chen 
660feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
661550e691bSryan_chen {
662577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
663550e691bSryan_chen 
664577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
665577fcdaeSDylan Hung 	mpll.out = rate;
666f27685ebSRyan Chen 	if (ast2600_search_clock_config(&mpll) == false) {
667577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
668577fcdaeSDylan Hung 		return 0;
669577fcdaeSDylan Hung 	}
670ed3899c5SRyan Chen 	ast2600_configure_pll(scu, &mpll.cfg, ASPEED_CLK_MPLL);
671577fcdaeSDylan Hung 
672cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
673d6e349c7Sryan_chen }
674d6e349c7Sryan_chen 
675d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
676550e691bSryan_chen {
677f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
678550e691bSryan_chen 	ulong new_rate;
679ed3899c5SRyan Chen 
680550e691bSryan_chen 	switch (clk->id) {
681f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
682feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
683550e691bSryan_chen 		break;
684550e691bSryan_chen 	default:
685550e691bSryan_chen 		return -ENOENT;
686550e691bSryan_chen 	}
687550e691bSryan_chen 
688550e691bSryan_chen 	return new_rate;
689550e691bSryan_chen }
690feb42054Sryan_chen 
691f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1	(20)
692f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2	(21)
693f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3	(20)
694f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4	(21)
695f9aa0ee1Sryan_chen 
696a98c71fbSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu, struct udevice *dev)
697cc476ffcSDylan Hung {
698a98c71fbSDylan Hung 	union mac_delay_1g reg_1g;
699a98c71fbSDylan Hung 	union mac_delay_100_10 reg_100, reg_10;
700a98c71fbSDylan Hung 	struct mac_delay_config mac1_cfg, mac2_cfg;
701a98c71fbSDylan Hung 	int ret;
7024760b3f8SDylan Hung 
703a98c71fbSDylan Hung 	reg_1g.w = (readl(&scu->mac12_clk_delay) & ~GENMASK(25, 0)) |
704a98c71fbSDylan Hung 		   MAC_DEF_DELAY_1G;
705a98c71fbSDylan Hung 	reg_100.w = MAC_DEF_DELAY_100M;
706a98c71fbSDylan Hung 	reg_10.w = MAC_DEF_DELAY_10M;
707a98c71fbSDylan Hung 
708a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac0-clk-delay", (u32 *)&mac1_cfg, sizeof(mac1_cfg) / sizeof(u32));
709a98c71fbSDylan Hung 	if (!ret) {
710a98c71fbSDylan Hung 		reg_1g.b.tx_delay_1 = mac1_cfg.tx_delay_1000;
711a98c71fbSDylan Hung 		reg_1g.b.rx_delay_1 = mac1_cfg.rx_delay_1000;
712a98c71fbSDylan Hung 		reg_100.b.tx_delay_1 = mac1_cfg.tx_delay_100;
713a98c71fbSDylan Hung 		reg_100.b.rx_delay_1 = mac1_cfg.rx_delay_100;
714a98c71fbSDylan Hung 		reg_10.b.tx_delay_1 = mac1_cfg.tx_delay_10;
715a98c71fbSDylan Hung 		reg_10.b.rx_delay_1 = mac1_cfg.rx_delay_10;
716a98c71fbSDylan Hung 	}
717a98c71fbSDylan Hung 
718a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac1-clk-delay", (u32 *)&mac2_cfg, sizeof(mac2_cfg) / sizeof(u32));
719a98c71fbSDylan Hung 	if (!ret) {
720a98c71fbSDylan Hung 		reg_1g.b.tx_delay_2 = mac2_cfg.tx_delay_1000;
721a98c71fbSDylan Hung 		reg_1g.b.rx_delay_2 = mac2_cfg.rx_delay_1000;
722a98c71fbSDylan Hung 		reg_100.b.tx_delay_2 = mac2_cfg.tx_delay_100;
723a98c71fbSDylan Hung 		reg_100.b.rx_delay_2 = mac2_cfg.rx_delay_100;
724a98c71fbSDylan Hung 		reg_10.b.tx_delay_2 = mac2_cfg.tx_delay_10;
725a98c71fbSDylan Hung 		reg_10.b.rx_delay_2 = mac2_cfg.rx_delay_10;
726a98c71fbSDylan Hung 	}
727a98c71fbSDylan Hung 
728a98c71fbSDylan Hung 	writel(reg_1g.w, &scu->mac12_clk_delay);
729a98c71fbSDylan Hung 	writel(reg_100.w, &scu->mac12_clk_delay_100M);
730a98c71fbSDylan Hung 	writel(reg_10.w, &scu->mac12_clk_delay_10M);
731cc476ffcSDylan Hung 
732ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
733eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16));
734894c19cfSDylan Hung 
735cc476ffcSDylan Hung 	return 0;
736cc476ffcSDylan Hung }
737cc476ffcSDylan Hung 
738a98c71fbSDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu, struct udevice *dev)
73954f9cba1SDylan Hung {
740a98c71fbSDylan Hung 	union mac_delay_1g reg_1g;
741a98c71fbSDylan Hung 	union mac_delay_100_10 reg_100, reg_10;
742a98c71fbSDylan Hung 	struct mac_delay_config mac3_cfg, mac4_cfg;
743a98c71fbSDylan Hung 	int ret;
744a98c71fbSDylan Hung 
74554f9cba1SDylan Hung 	/*
746eff28274SJohnny Huang 	 * scu350[31]   RGMII 125M source: 0 = from IO pin
747eff28274SJohnny Huang 	 * scu350[25:0] MAC 1G delay
74854f9cba1SDylan Hung 	 */
749a98c71fbSDylan Hung 	reg_1g.w = (readl(&scu->mac34_clk_delay) & ~GENMASK(25, 0)) |
750a98c71fbSDylan Hung 		   MAC34_DEF_DELAY_1G;
751a98c71fbSDylan Hung 	reg_1g.b.rgmii_125m_o_sel = 0;
752a98c71fbSDylan Hung 	reg_100.w = MAC34_DEF_DELAY_100M;
753a98c71fbSDylan Hung 	reg_10.w = MAC34_DEF_DELAY_10M;
754a98c71fbSDylan Hung 
755a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac2-clk-delay", (u32 *)&mac3_cfg, sizeof(mac3_cfg) / sizeof(u32));
756a98c71fbSDylan Hung 	if (!ret) {
757a98c71fbSDylan Hung 		reg_1g.b.tx_delay_1 = mac3_cfg.tx_delay_1000;
758a98c71fbSDylan Hung 		reg_1g.b.rx_delay_1 = mac3_cfg.rx_delay_1000;
759a98c71fbSDylan Hung 		reg_100.b.tx_delay_1 = mac3_cfg.tx_delay_100;
760a98c71fbSDylan Hung 		reg_100.b.rx_delay_1 = mac3_cfg.rx_delay_100;
761a98c71fbSDylan Hung 		reg_10.b.tx_delay_1 = mac3_cfg.tx_delay_10;
762a98c71fbSDylan Hung 		reg_10.b.rx_delay_1 = mac3_cfg.rx_delay_10;
763a98c71fbSDylan Hung 	}
764a98c71fbSDylan Hung 
765a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac3-clk-delay", (u32 *)&mac4_cfg, sizeof(mac4_cfg) / sizeof(u32));
766a98c71fbSDylan Hung 	if (!ret) {
767a98c71fbSDylan Hung 		reg_1g.b.tx_delay_2 = mac4_cfg.tx_delay_1000;
768a98c71fbSDylan Hung 		reg_1g.b.rx_delay_2 = mac4_cfg.rx_delay_1000;
769a98c71fbSDylan Hung 		reg_100.b.tx_delay_2 = mac4_cfg.tx_delay_100;
770a98c71fbSDylan Hung 		reg_100.b.rx_delay_2 = mac4_cfg.rx_delay_100;
771a98c71fbSDylan Hung 		reg_10.b.tx_delay_2 = mac4_cfg.tx_delay_10;
772a98c71fbSDylan Hung 		reg_10.b.rx_delay_2 = mac4_cfg.rx_delay_10;
773a98c71fbSDylan Hung 	}
774a98c71fbSDylan Hung 
775a98c71fbSDylan Hung 	writel(reg_1g.w, &scu->mac34_clk_delay);
776a98c71fbSDylan Hung 	writel(reg_100.w, &scu->mac34_clk_delay_100M);
777a98c71fbSDylan Hung 	writel(reg_10.w, &scu->mac34_clk_delay_10M);
77854f9cba1SDylan Hung 
779eff28274SJohnny Huang 	/*
780eff28274SJohnny Huang 	 * clock source seletion and divider
781eff28274SJohnny Huang 	 * scu310[26:24] : MAC AHB bus clock = HCLK / 2
782eff28274SJohnny Huang 	 * scu310[18:16] : RMII 50M = HCLK_200M / 4
783eff28274SJohnny Huang 	 */
784eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)),
785eff28274SJohnny Huang 			((0x0 << 24) | (0x3 << 16)));
78654f9cba1SDylan Hung 
787eff28274SJohnny Huang 	/*
788eff28274SJohnny Huang 	 * set driving strength
789eff28274SJohnny Huang 	 * scu458[3:2] : MAC4 driving strength
790eff28274SJohnny Huang 	 * scu458[1:0] : MAC3 driving strength
791eff28274SJohnny Huang 	 */
792eff28274SJohnny Huang 	clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0),
793a961159eSDylan Hung 			(0x3 << 2) | (0x3 << 0));
79454f9cba1SDylan Hung 
79554f9cba1SDylan Hung 	return 0;
79654f9cba1SDylan Hung }
797eff28274SJohnny Huang 
79854f9cba1SDylan Hung /**
7995b5c3d44SDylan Hung  * ast2600 RGMII clock source tree
80054f9cba1SDylan Hung  * 125M from external PAD -------->|\
80154f9cba1SDylan Hung  * HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
80254f9cba1SDylan Hung  *         | |---->| divider |---->|/                             +
80354f9cba1SDylan Hung  * EPLL -->|/                                                     |
80454f9cba1SDylan Hung  *                                                                |
805eff28274SJohnny Huang  * +---------<-----------|RGMIICK PAD output enable|<-------------+
80654f9cba1SDylan Hung  * |
807eff28274SJohnny Huang  * +--------------------------->|\
80854f9cba1SDylan Hung  *                              | |----> RGMII 125M for MAC#3 & MAC#4
809eff28274SJohnny Huang  * HCLK 200M ---->|divider|---->|/
810eff28274SJohnny Huang  * To simplify the control flow:
811eff28274SJohnny Huang  * 1. RGMII 1/2 always use EPLL as the internal clock source
812eff28274SJohnny Huang  * 2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source
813eff28274SJohnny Huang  * 125M from external PAD -------->|\
814eff28274SJohnny Huang  *                                 | |---->RGMII 125M for MAC#1 & MAC#2
815eff28274SJohnny Huang  *         EPLL---->| divider |--->|/                             +
816eff28274SJohnny Huang  *                                                                |
817eff28274SJohnny Huang  * +<--------------------|RGMIICK PAD output enable|<-------------+
818eff28274SJohnny Huang  * |
819eff28274SJohnny Huang  * +--------------------------->RGMII 125M for MAC#3 & MAC#4
820eff28274SJohnny Huang  */
821eff28274SJohnny Huang #define RGMIICK_SRC_PAD		0
822eff28274SJohnny Huang #define RGMIICK_SRC_EPLL	1 /* recommended */
823eff28274SJohnny Huang #define RGMIICK_SRC_HPLL	2
824eff28274SJohnny Huang 
825eff28274SJohnny Huang #define RGMIICK_DIV2	1
826eff28274SJohnny Huang #define RGMIICK_DIV3	2
827eff28274SJohnny Huang #define RGMIICK_DIV4	3
828eff28274SJohnny Huang #define RGMIICK_DIV5	4
829eff28274SJohnny Huang #define RGMIICK_DIV6	5
830eff28274SJohnny Huang #define RGMIICK_DIV7	6
831eff28274SJohnny Huang #define RGMIICK_DIV8	7 /* recommended */
832eff28274SJohnny Huang 
833eff28274SJohnny Huang #define RMIICK_DIV4		0
834eff28274SJohnny Huang #define RMIICK_DIV8		1
835eff28274SJohnny Huang #define RMIICK_DIV12	2
836eff28274SJohnny Huang #define RMIICK_DIV16	3
837eff28274SJohnny Huang #define RMIICK_DIV20	4 /* recommended */
838eff28274SJohnny Huang #define RMIICK_DIV24	5
839eff28274SJohnny Huang #define RMIICK_DIV28	6
840eff28274SJohnny Huang #define RMIICK_DIV32	7
841eff28274SJohnny Huang 
842eff28274SJohnny Huang struct ast2600_mac_clk_div {
843eff28274SJohnny Huang 	u32 src; /* 0=external PAD, 1=internal PLL */
844eff28274SJohnny Huang 	u32 fin; /* divider input speed */
845eff28274SJohnny Huang 	u32 n; /* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */
846eff28274SJohnny Huang 	u32 fout; /* fout = fin / n */
847eff28274SJohnny Huang };
848eff28274SJohnny Huang 
849eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = {
850eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
851eff28274SJohnny Huang 	.fin = 1000000000,
852eff28274SJohnny Huang 	.n = RGMIICK_DIV8,
853eff28274SJohnny Huang 	.fout = 125000000,
854eff28274SJohnny Huang };
855eff28274SJohnny Huang 
856eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = {
857eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
858eff28274SJohnny Huang 	.fin = 1000000000,
859eff28274SJohnny Huang 	.n = RMIICK_DIV20,
860eff28274SJohnny Huang 	.fout = 50000000,
861eff28274SJohnny Huang };
862ed3899c5SRyan Chen 
863eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu,
864eff28274SJohnny Huang 				 struct ast2600_mac_clk_div *p_cfg)
865eff28274SJohnny Huang {
866eff28274SJohnny Huang 	struct ast2600_pll_desc pll;
867eff28274SJohnny Huang 
868eff28274SJohnny Huang 	pll.in = AST2600_CLK_IN;
869eff28274SJohnny Huang 	pll.out = p_cfg->fin;
870ed3899c5SRyan Chen 	if (ast2600_search_clock_config(&pll) == false) {
871ed3899c5SRyan Chen 		pr_err("unable to find valid ETHNET MAC clock setting\n");
8723f295164SRyan Chen 		debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w,
8733f295164SRyan Chen 		      pll.cfg.ext_reg);
8743f295164SRyan Chen 		debug("%s: pll cfg = %02x %02x %02x\n", __func__,
8753f295164SRyan Chen 		      pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p);
876eff28274SJohnny Huang 		return;
877eff28274SJohnny Huang 	}
878ed3899c5SRyan Chen 	ast2600_configure_pll(p_scu, &pll.cfg, p_cfg->src);
879eff28274SJohnny Huang }
880eff28274SJohnny Huang 
881eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu,
882eff28274SJohnny Huang 				   struct ast2600_mac_clk_div *p_cfg)
883eff28274SJohnny Huang {
884eff28274SJohnny Huang 	u32 reg_304 = readl(&p_scu->clk_sel2);
885eff28274SJohnny Huang 	u32 reg_340 = readl(&p_scu->mac12_clk_delay);
886eff28274SJohnny Huang 	u32 reg_350 = readl(&p_scu->mac34_clk_delay);
887eff28274SJohnny Huang 
888eff28274SJohnny Huang 	reg_340 &= ~GENMASK(31, 29);
889eff28274SJohnny Huang 	/* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */
890eff28274SJohnny Huang 	reg_340 |= BIT(28);
8913f295164SRyan Chen 	if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) {
892eff28274SJohnny Huang 		/*
893eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
894eff28274SJohnny Huang 		 * the divider setting
895eff28274SJohnny Huang 		 */
896ed3899c5SRyan Chen 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src))
897eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
898eff28274SJohnny Huang 		/* scu340[31]: select RGMII 125M from internal source */
899eff28274SJohnny Huang 		reg_340 |= BIT(31);
900eff28274SJohnny Huang 	}
901eff28274SJohnny Huang 
902eff28274SJohnny Huang 	reg_304 &= ~GENMASK(23, 20);
903eff28274SJohnny Huang 
904eff28274SJohnny Huang 	/* set clock divider */
905eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 20;
906eff28274SJohnny Huang 
907eff28274SJohnny Huang 	/* select internal clock source */
908ed3899c5SRyan Chen 	if (p_cfg->src == ASPEED_CLK_HPLL)
909eff28274SJohnny Huang 		reg_304 |= BIT(23);
910eff28274SJohnny Huang 
911eff28274SJohnny Huang 	/* RGMII 3/4 clock source select */
912eff28274SJohnny Huang 	reg_350 &= ~BIT(31);
913eff28274SJohnny Huang 
914eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
915eff28274SJohnny Huang 	writel(reg_340, &p_scu->mac12_clk_delay);
916eff28274SJohnny Huang 	writel(reg_350, &p_scu->mac34_clk_delay);
917eff28274SJohnny Huang }
918eff28274SJohnny Huang 
919eff28274SJohnny Huang /**
9205b5c3d44SDylan Hung  * ast2600 RMII/NCSI clock source tree
9215b5c3d44SDylan Hung  * HPLL -->|\
9225b5c3d44SDylan Hung  *         | |---->| divider |----> RMII 50M for MAC#1 & MAC#2
9235b5c3d44SDylan Hung  * EPLL -->|/
9245b5c3d44SDylan Hung  * HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4
92554f9cba1SDylan Hung  */
926eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu,
927eff28274SJohnny Huang 				  struct ast2600_mac_clk_div *p_cfg)
92854f9cba1SDylan Hung {
929eff28274SJohnny Huang 	u32 reg_304;
930eff28274SJohnny Huang 	u32 reg_310;
931eff28274SJohnny Huang 
9323f295164SRyan Chen 	if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) {
933eff28274SJohnny Huang 		/*
934eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
935eff28274SJohnny Huang 		 * the divider setting
936eff28274SJohnny Huang 		 */
937ed3899c5SRyan Chen 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src))
938eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
939eff28274SJohnny Huang 	}
94054f9cba1SDylan Hung 
941eff28274SJohnny Huang 	reg_304 = readl(&p_scu->clk_sel2);
942eff28274SJohnny Huang 	reg_310 = readl(&p_scu->clk_sel4);
943eff28274SJohnny Huang 
944eff28274SJohnny Huang 	reg_304 &= ~GENMASK(19, 16);
945eff28274SJohnny Huang 
946eff28274SJohnny Huang 	/* set RMII 1/2 clock divider */
947eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 16;
948eff28274SJohnny Huang 
949eff28274SJohnny Huang 	/* RMII clock source selection */
950ed3899c5SRyan Chen 	if (p_cfg->src == ASPEED_CLK_HPLL)
951eff28274SJohnny Huang 		reg_304 |= BIT(19);
952eff28274SJohnny Huang 
953eff28274SJohnny Huang 	/* set RMII 3/4 clock divider */
954eff28274SJohnny Huang 	reg_310 &= ~GENMASK(18, 16);
955eff28274SJohnny Huang 	reg_310 |= (0x3 << 16);
956eff28274SJohnny Huang 
957eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
958eff28274SJohnny Huang 	writel(reg_310, &p_scu->clk_sel4);
959eff28274SJohnny Huang }
960eff28274SJohnny Huang 
961f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
962f9aa0ee1Sryan_chen {
963f9aa0ee1Sryan_chen 	u32 reset_bit;
964f9aa0ee1Sryan_chen 	u32 clkstop_bit;
965f9aa0ee1Sryan_chen 
966f9aa0ee1Sryan_chen 	switch (index) {
967f9aa0ee1Sryan_chen 	case 1:
968f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
969f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
970f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
971f9aa0ee1Sryan_chen 		udelay(100);
972f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
973f9aa0ee1Sryan_chen 		mdelay(10);
974f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
975f9aa0ee1Sryan_chen 		break;
976f9aa0ee1Sryan_chen 	case 2:
977f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
978f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
979f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
980f9aa0ee1Sryan_chen 		udelay(100);
981f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
982f9aa0ee1Sryan_chen 		mdelay(10);
983f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
984f9aa0ee1Sryan_chen 		break;
985f9aa0ee1Sryan_chen 	case 3:
986f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
987f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
988f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
989f9aa0ee1Sryan_chen 		udelay(100);
990f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
991f9aa0ee1Sryan_chen 		mdelay(10);
992f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
993f9aa0ee1Sryan_chen 		break;
994f9aa0ee1Sryan_chen 	case 4:
995f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
996f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
997f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
998f9aa0ee1Sryan_chen 		udelay(100);
999f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1000f9aa0ee1Sryan_chen 		mdelay(10);
1001f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
1002f9aa0ee1Sryan_chen 		break;
1003f9aa0ee1Sryan_chen 	default:
1004f9aa0ee1Sryan_chen 		return -EINVAL;
1005f9aa0ee1Sryan_chen 	}
1006f9aa0ee1Sryan_chen 
1007f9aa0ee1Sryan_chen 	return 0;
1008f9aa0ee1Sryan_chen }
1009550e691bSryan_chen 
1010a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_FROM_HPLL_CLK	BIT(19)
1011a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_CLK_MASK		GENMASK(27, 26)
1012ed3899c5SRyan Chen #define SCU_CLK_ECC_RSA_CLK_DIV(x)		((x) << 26)
1013a8fc7648SRyan Chen static void ast2600_configure_rsa_ecc_clk(struct ast2600_scu *scu)
1014a8fc7648SRyan Chen {
1015a8fc7648SRyan Chen 	u32 clk_sel = readl(&scu->clk_sel1);
1016a8fc7648SRyan Chen 
10177e3c964cSJohnny Huang 	/* Configure RSA clock = HPLL/4 */
1018a8fc7648SRyan Chen 	clk_sel |= SCU_CLK_ECC_RSA_FROM_HPLL_CLK;
1019a8fc7648SRyan Chen 	clk_sel &= ~SCU_CLK_ECC_RSA_CLK_MASK;
10207e3c964cSJohnny Huang 	clk_sel |= SCU_CLK_ECC_RSA_CLK_DIV(3);
1021a8fc7648SRyan Chen 
1022a8fc7648SRyan Chen 	writel(clk_sel, &scu->clk_sel1);
1023a8fc7648SRyan Chen }
1024a8fc7648SRyan Chen 
1025f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
1026f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
1027f51926eeSryan_chen {
1028f51926eeSryan_chen 	u32 reset_bit;
1029f51926eeSryan_chen 	u32 clkstop_bit;
1030f51926eeSryan_chen 
1031f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
1032f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
1033f51926eeSryan_chen 
1034fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
1035fc9f12e6Sryan_chen 
1036f51926eeSryan_chen 	udelay(100);
1037f51926eeSryan_chen 	//enable clk
1038f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1039f51926eeSryan_chen 	mdelay(10);
1040fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1041f51926eeSryan_chen 
1042f51926eeSryan_chen 	return 0;
1043f51926eeSryan_chen }
1044f51926eeSryan_chen 
1045f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD			31
1046f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
1047ed3899c5SRyan Chen #define SCU_CLK_SD_DIV(x)			((x) << 28)
10482cd7cba2Sryan_chen #define SCU_CLK_SD_FROM_APLL_CLK	BIT(8)
1049f51926eeSryan_chen 
1050f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
1051f51926eeSryan_chen {
1052f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
1053f51926eeSryan_chen 	u32 enableclk_bit;
10542cd7cba2Sryan_chen 	u32 rate = 0;
10552cd7cba2Sryan_chen 	u32 div = 0;
10562cd7cba2Sryan_chen 	int i = 0;
1057f51926eeSryan_chen 
1058f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
1059f51926eeSryan_chen 
1060a8fc7648SRyan Chen 	/* ast2600 sd controller max clk is 200Mhz :
1061a8fc7648SRyan Chen 	 * use apll for clock source 800/4 = 200 : controller max is 200mhz
1062a8fc7648SRyan Chen 	 */
10632cd7cba2Sryan_chen 	rate = ast2600_get_apll_rate(scu);
10642cd7cba2Sryan_chen 	for (i = 0; i < 8; i++) {
10652cd7cba2Sryan_chen 		div = (i + 1) * 2;
10662cd7cba2Sryan_chen 		if ((rate / div) <= 200000000)
10672cd7cba2Sryan_chen 			break;
10682cd7cba2Sryan_chen 	}
1069f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
10702cd7cba2Sryan_chen 	clk_sel |= SCU_CLK_SD_DIV(i) | SCU_CLK_SD_FROM_APLL_CLK;
1071f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
1072f51926eeSryan_chen 
1073f51926eeSryan_chen 	//enable clk
1074f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
1075f51926eeSryan_chen 
1076f51926eeSryan_chen 	return 0;
1077f51926eeSryan_chen }
1078f51926eeSryan_chen 
1079f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
1080f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
1081f51926eeSryan_chen {
1082f51926eeSryan_chen 	u32 reset_bit;
1083f51926eeSryan_chen 	u32 clkstop_bit;
1084f51926eeSryan_chen 
1085f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
1086f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
1087f51926eeSryan_chen 
1088fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1089f51926eeSryan_chen 	udelay(100);
1090f51926eeSryan_chen 	//enable clk
1091f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1092f51926eeSryan_chen 	mdelay(10);
1093fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1094f51926eeSryan_chen 
1095f51926eeSryan_chen 	return 0;
1096f51926eeSryan_chen }
1097f51926eeSryan_chen 
1098f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC			15
1099f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
1100ed3899c5SRyan Chen #define SCU_CLK_EMMC_DIV(x)			((x) << 12)
1101a8fc7648SRyan Chen #define SCU_CLK_EMMC_FROM_MPLL_CLK	BIT(11)
1102f51926eeSryan_chen 
1103f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
1104f51926eeSryan_chen {
110585d48d8cSryan_chen 	u32 revision_id = readl(&scu->chip_id1);
1106f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
1107ed3899c5SRyan Chen 	u32 enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC);
1108f4c4ddb1Sryan_chen 	u32 rate = 0;
1109f4c4ddb1Sryan_chen 	u32 div = 0;
1110f4c4ddb1Sryan_chen 	int i = 0;
1111f51926eeSryan_chen 
1112ed3899c5SRyan Chen 	/*
1113ed3899c5SRyan Chen 	 * ast2600 eMMC controller max clk is 200Mhz
1114ed3899c5SRyan Chen 	 * HPll->1/2->|\
1115ed3899c5SRyan Chen 	 *				|->SCU300[11]->SCU300[14:12][1/N] +
1116ed3899c5SRyan Chen 	 * MPLL------>|/								  |
1117ed3899c5SRyan Chen 	 * +----------------------------------------------+
1118ed3899c5SRyan Chen 	 * |
1119ed3899c5SRyan Chen 	 * +---------> EMMC12C[15:8][1/N]-> eMMC clk
1120a8fc7648SRyan Chen 	 */
112185d48d8cSryan_chen 	if (((revision_id & CHIP_REVISION_ID) >> 16)) {
11228c32294fSryan_chen 		//AST2600A1 : use mpll to be clk source
1123b0c30ea3Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
1124b0c30ea3Sryan_chen 		for (i = 0; i < 8; i++) {
1125b0c30ea3Sryan_chen 			div = (i + 1) * 2;
1126b0c30ea3Sryan_chen 			if ((rate / div) <= 200000000)
1127b0c30ea3Sryan_chen 				break;
1128b0c30ea3Sryan_chen 		}
1129b0c30ea3Sryan_chen 
1130b0c30ea3Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
11312cd7cba2Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i) | SCU_CLK_EMMC_FROM_MPLL_CLK;
1132b0c30ea3Sryan_chen 		writel(clk_sel, &scu->clk_sel1);
1133b0c30ea3Sryan_chen 
1134b0c30ea3Sryan_chen 	} else {
11352cd7cba2Sryan_chen 		//AST2600A0 : use hpll to be clk source
1136f4c4ddb1Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1137f4c4ddb1Sryan_chen 
1138f4c4ddb1Sryan_chen 		for (i = 0; i < 8; i++) {
1139f4c4ddb1Sryan_chen 			div = (i + 1) * 4;
1140f4c4ddb1Sryan_chen 			if ((rate / div) <= 200000000)
1141f4c4ddb1Sryan_chen 				break;
1142f4c4ddb1Sryan_chen 		}
1143f4c4ddb1Sryan_chen 
1144f4c4ddb1Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
1145f4c4ddb1Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i);
1146f51926eeSryan_chen 		writel(clk_sel, &scu->clk_sel1);
1147b0c30ea3Sryan_chen 	}
1148f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
1149f51926eeSryan_chen 
1150f51926eeSryan_chen 	return 0;
1151f51926eeSryan_chen }
1152f51926eeSryan_chen 
1153baf00c26Sryan_chen #define SCU_CLKSTOP_FSICLK 30
1154baf00c26Sryan_chen 
1155baf00c26Sryan_chen static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu)
1156baf00c26Sryan_chen {
1157baf00c26Sryan_chen 	u32 reset_bit;
1158baf00c26Sryan_chen 	u32 clkstop_bit;
1159baf00c26Sryan_chen 
1160baf00c26Sryan_chen 	reset_bit = BIT(ASPEED_RESET_FSI % 32);
1161baf00c26Sryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_FSICLK);
1162baf00c26Sryan_chen 
1163baf00c26Sryan_chen 	/* The FSI clock is shared between masters. If it's already on
1164ed3899c5SRyan Chen 	 * don't touch it, as that will reset the existing master.
1165ed3899c5SRyan Chen 	 */
1166baf00c26Sryan_chen 	if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) {
1167baf00c26Sryan_chen 		debug("%s: already running, not touching it\n", __func__);
1168baf00c26Sryan_chen 		return 0;
1169baf00c26Sryan_chen 	}
1170baf00c26Sryan_chen 
1171baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
1172baf00c26Sryan_chen 	udelay(100);
1173baf00c26Sryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1174baf00c26Sryan_chen 	mdelay(10);
1175baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1176baf00c26Sryan_chen 
1177baf00c26Sryan_chen 	return 0;
1178baf00c26Sryan_chen }
1179baf00c26Sryan_chen 
1180b8ec5ceaSryan_chen static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu)
1181b8ec5ceaSryan_chen {
1182b8ec5ceaSryan_chen 	u32 reset_bit;
1183b8ec5ceaSryan_chen 	u32 clkstop_bit;
1184b8ec5ceaSryan_chen 
1185b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P1);
1186b8ec5ceaSryan_chen 	clkstop_bit = BIT(14);
1187b8ec5ceaSryan_chen 
1188b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1189b8ec5ceaSryan_chen 	udelay(100);
1190b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_ctrl1);
1191b8ec5ceaSryan_chen 	mdelay(20);
1192b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1193b8ec5ceaSryan_chen 
1194b8ec5ceaSryan_chen 	return 0;
1195b8ec5ceaSryan_chen }
1196b8ec5ceaSryan_chen 
1197b8ec5ceaSryan_chen static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu)
1198b8ec5ceaSryan_chen {
1199b8ec5ceaSryan_chen 	u32 reset_bit;
1200b8ec5ceaSryan_chen 	u32 clkstop_bit;
1201b8ec5ceaSryan_chen 
1202b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P2);
1203b8ec5ceaSryan_chen 	clkstop_bit = BIT(7);
1204b8ec5ceaSryan_chen 
1205b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1206b8ec5ceaSryan_chen 	udelay(100);
1207b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1208b8ec5ceaSryan_chen 	mdelay(20);
1209b8ec5ceaSryan_chen 
1210b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1211b8ec5ceaSryan_chen 
1212b8ec5ceaSryan_chen 	return 0;
1213b8ec5ceaSryan_chen }
1214b8ec5ceaSryan_chen 
1215089713adSJoel Stanley /* also known as yclk */
1216089713adSJoel Stanley static ulong ast2600_enable_haceclk(struct ast2600_scu *scu)
1217089713adSJoel Stanley {
1218089713adSJoel Stanley 	u32 reset_bit;
1219089713adSJoel Stanley 	u32 clkstop_bit;
1220089713adSJoel Stanley 
1221089713adSJoel Stanley 	reset_bit = BIT(ASPEED_RESET_HACE);
1222089713adSJoel Stanley 	clkstop_bit = BIT(13);
1223089713adSJoel Stanley 
1224089713adSJoel Stanley 	writel(reset_bit, &scu->sysreset_ctrl1);
1225089713adSJoel Stanley 	udelay(100);
1226089713adSJoel Stanley 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1227089713adSJoel Stanley 	mdelay(20);
1228089713adSJoel Stanley 
1229089713adSJoel Stanley 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1230089713adSJoel Stanley 
1231089713adSJoel Stanley 	return 0;
1232089713adSJoel Stanley }
1233089713adSJoel Stanley 
1234*f6110ecdSChia-Wei Wang static ulong ast2600_enable_rsaeccclk(struct ast2600_scu *scu)
1235*f6110ecdSChia-Wei Wang {
1236*f6110ecdSChia-Wei Wang 	u32 clkstop_bit;
1237*f6110ecdSChia-Wei Wang 
1238*f6110ecdSChia-Wei Wang 	clkstop_bit = BIT(24);
1239*f6110ecdSChia-Wei Wang 
1240*f6110ecdSChia-Wei Wang 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1241*f6110ecdSChia-Wei Wang 	mdelay(20);
1242*f6110ecdSChia-Wei Wang 
1243*f6110ecdSChia-Wei Wang 	return 0;
1244*f6110ecdSChia-Wei Wang }
1245*f6110ecdSChia-Wei Wang 
1246d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
1247550e691bSryan_chen {
1248f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
1249550e691bSryan_chen 
1250550e691bSryan_chen 	switch (clk->id) {
125186f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC1CLK:
125286f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 1);
1253550e691bSryan_chen 		break;
125486f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC2CLK:
125586f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 2);
1256550e691bSryan_chen 		break;
125777843939Sryan_chen 	case ASPEED_CLK_GATE_MAC3CLK:
125877843939Sryan_chen 		ast2600_configure_mac(priv->scu, 3);
125977843939Sryan_chen 		break;
126077843939Sryan_chen 	case ASPEED_CLK_GATE_MAC4CLK:
126177843939Sryan_chen 		ast2600_configure_mac(priv->scu, 4);
126277843939Sryan_chen 		break;
1263f51926eeSryan_chen 	case ASPEED_CLK_GATE_SDCLK:
1264f51926eeSryan_chen 		ast2600_enable_sdclk(priv->scu);
1265f51926eeSryan_chen 		break;
1266f51926eeSryan_chen 	case ASPEED_CLK_GATE_SDEXTCLK:
1267f51926eeSryan_chen 		ast2600_enable_extsdclk(priv->scu);
1268f51926eeSryan_chen 		break;
1269f51926eeSryan_chen 	case ASPEED_CLK_GATE_EMMCCLK:
1270f51926eeSryan_chen 		ast2600_enable_emmcclk(priv->scu);
1271f51926eeSryan_chen 		break;
1272f51926eeSryan_chen 	case ASPEED_CLK_GATE_EMMCEXTCLK:
1273f51926eeSryan_chen 		ast2600_enable_extemmcclk(priv->scu);
1274f51926eeSryan_chen 		break;
1275baf00c26Sryan_chen 	case ASPEED_CLK_GATE_FSICLK:
1276baf00c26Sryan_chen 		ast2600_enable_fsiclk(priv->scu);
1277baf00c26Sryan_chen 		break;
1278b8ec5ceaSryan_chen 	case ASPEED_CLK_GATE_USBPORT1CLK:
1279b8ec5ceaSryan_chen 		ast2600_enable_usbahclk(priv->scu);
1280b8ec5ceaSryan_chen 		break;
1281b8ec5ceaSryan_chen 	case ASPEED_CLK_GATE_USBPORT2CLK:
1282b8ec5ceaSryan_chen 		ast2600_enable_usbbhclk(priv->scu);
1283b8ec5ceaSryan_chen 		break;
1284089713adSJoel Stanley 	case ASPEED_CLK_GATE_YCLK:
1285089713adSJoel Stanley 		ast2600_enable_haceclk(priv->scu);
1286089713adSJoel Stanley 		break;
1287*f6110ecdSChia-Wei Wang 	case ASPEED_CLK_GATE_RSAECCCLK:
1288*f6110ecdSChia-Wei Wang 		ast2600_enable_rsaeccclk(priv->scu);
1289*f6110ecdSChia-Wei Wang 		break;
1290550e691bSryan_chen 	default:
1291ed3899c5SRyan Chen 		pr_err("can't enable clk\n");
1292550e691bSryan_chen 		return -ENOENT;
1293550e691bSryan_chen 	}
1294550e691bSryan_chen 
1295550e691bSryan_chen 	return 0;
1296550e691bSryan_chen }
1297550e691bSryan_chen 
1298f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
1299d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
1300d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
1301d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
1302550e691bSryan_chen };
1303550e691bSryan_chen 
1304d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
1305550e691bSryan_chen {
1306f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
130761ab9607Sryan_chen 	u32 uart_clk_source;
1308550e691bSryan_chen 
1309f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
1310f0d895afSryan_chen 	if (IS_ERR(priv->scu))
1311f0d895afSryan_chen 		return PTR_ERR(priv->scu);
1312550e691bSryan_chen 
13135d05f4fcSRyan Chen 	uart_clk_source = dev_read_u32_default(dev, "uart-clk-source", 0x0);
131461ab9607Sryan_chen 
131561ab9607Sryan_chen 	if (uart_clk_source) {
131656dd3e85Sryan_chen 		if (uart_clk_source & GENMASK(5, 0))
13175d05f4fcSRyan Chen 			setbits_le32(&priv->scu->clk_sel4,
13185d05f4fcSRyan Chen 				     uart_clk_source & GENMASK(5, 0));
131956dd3e85Sryan_chen 		if (uart_clk_source & GENMASK(12, 6))
13205d05f4fcSRyan Chen 			setbits_le32(&priv->scu->clk_sel5,
13215d05f4fcSRyan Chen 				     uart_clk_source & GENMASK(12, 6));
132261ab9607Sryan_chen 	}
132361ab9607Sryan_chen 
1324b89500a2SDylan Hung 	ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig);
1325b89500a2SDylan Hung 	ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig);
1326a98c71fbSDylan Hung 	ast2600_configure_mac12_clk(priv->scu, dev);
1327a98c71fbSDylan Hung 	ast2600_configure_mac34_clk(priv->scu, dev);
1328a8fc7648SRyan Chen 	ast2600_configure_rsa_ecc_clk(priv->scu);
1329fd0306aaSJohnny Huang 
1330550e691bSryan_chen 	return 0;
1331550e691bSryan_chen }
1332550e691bSryan_chen 
1333d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
1334550e691bSryan_chen {
1335550e691bSryan_chen 	int ret;
1336550e691bSryan_chen 
1337550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
1338550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
1339550e691bSryan_chen 	if (ret)
1340550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
1341550e691bSryan_chen 
1342550e691bSryan_chen 	return 0;
1343550e691bSryan_chen }
1344550e691bSryan_chen 
1345d35ac78cSryan_chen struct aspeed_clks {
1346d35ac78cSryan_chen 	ulong id;
1347d35ac78cSryan_chen 	const char *name;
1348d35ac78cSryan_chen };
1349d35ac78cSryan_chen 
1350d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
13515d05f4fcSRyan Chen 	{ ASPEED_CLK_HPLL, "hpll" },     { ASPEED_CLK_MPLL, "mpll" },
13525d05f4fcSRyan Chen 	{ ASPEED_CLK_APLL, "apll" },     { ASPEED_CLK_EPLL, "epll" },
13535d05f4fcSRyan Chen 	{ ASPEED_CLK_DPLL, "dpll" },     { ASPEED_CLK_AHB, "hclk" },
13545d05f4fcSRyan Chen 	{ ASPEED_CLK_APB1, "pclk1" },    { ASPEED_CLK_APB2, "pclk2" },
13555d05f4fcSRyan Chen 	{ ASPEED_CLK_BCLK, "bclk" },     { ASPEED_CLK_UARTX, "uxclk" },
1356def99fcbSryan_chen 	{ ASPEED_CLK_HUARTX, "huxclk" },
1357d35ac78cSryan_chen };
1358d35ac78cSryan_chen 
1359d35ac78cSryan_chen int soc_clk_dump(void)
1360d35ac78cSryan_chen {
1361d35ac78cSryan_chen 	struct udevice *dev;
1362d35ac78cSryan_chen 	struct clk clk;
1363d35ac78cSryan_chen 	unsigned long rate;
1364d35ac78cSryan_chen 	int i, ret;
1365d35ac78cSryan_chen 
13665d05f4fcSRyan Chen 	ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu),
13675d05f4fcSRyan Chen 					  &dev);
1368d35ac78cSryan_chen 	if (ret)
1369d35ac78cSryan_chen 		return ret;
1370d35ac78cSryan_chen 
1371d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
1372d35ac78cSryan_chen 
1373d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
1374d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
1375d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
1376d35ac78cSryan_chen 		if (ret < 0) {
1377d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
1378d35ac78cSryan_chen 			continue;
1379d35ac78cSryan_chen 		}
1380d35ac78cSryan_chen 
1381d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
1382d35ac78cSryan_chen 		rate = ret;
1383d35ac78cSryan_chen 
1384d35ac78cSryan_chen 		clk_free(&clk);
1385d35ac78cSryan_chen 
1386d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
1387d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
1388d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
1389d35ac78cSryan_chen 			continue;
1390d35ac78cSryan_chen 		}
1391d35ac78cSryan_chen 		if (ret < 0) {
13925d05f4fcSRyan Chen 			printf("%s %lu: get_rate err: %d\n", __func__,
13935d05f4fcSRyan Chen 			       aspeed_clk_names[i].id, ret);
1394d35ac78cSryan_chen 			continue;
1395d35ac78cSryan_chen 		}
1396d35ac78cSryan_chen 
13975d05f4fcSRyan Chen 		printf("%s(%3lu):\t%lu\n", aspeed_clk_names[i].name,
13985d05f4fcSRyan Chen 		       aspeed_clk_names[i].id, rate);
1399d35ac78cSryan_chen 	}
1400d35ac78cSryan_chen 
1401d35ac78cSryan_chen 	return 0;
1402d35ac78cSryan_chen }
1403d35ac78cSryan_chen 
1404d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
14055d05f4fcSRyan Chen 	{
14065d05f4fcSRyan Chen 		.compatible = "aspeed,ast2600-scu",
14075d05f4fcSRyan Chen 	},
1408550e691bSryan_chen 	{}
1409550e691bSryan_chen };
1410550e691bSryan_chen 
1411aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
1412aa36597fSDylan Hung 	.name = "aspeed_scu",
1413550e691bSryan_chen 	.id = UCLASS_CLK,
1414d6e349c7Sryan_chen 	.of_match = ast2600_clk_ids,
1415f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
1416f9aa0ee1Sryan_chen 	.ops = &ast2600_clk_ops,
1417d6e349c7Sryan_chen 	.bind = ast2600_clk_bind,
1418d6e349c7Sryan_chen 	.probe = ast2600_clk_probe,
1419550e691bSryan_chen };
1420