xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision 225457067f19c94573cf04ca9735c3ceec08284e)
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) &
288*22545706SRyan 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) &
296*22545706SRyan 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 {
443bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
444f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
445f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
446f51926eeSryan_chen 
447f51926eeSryan_chen 	div = (div + 1) << 2;
448f51926eeSryan_chen 
449f51926eeSryan_chen 	return (clkin / div);
450f51926eeSryan_chen }
451f51926eeSryan_chen 
452f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
45327881d20Sryan_chen {
45427881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
45527881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
45627881d20Sryan_chen 	ulong uart_clk = 0;
45727881d20Sryan_chen 
45827881d20Sryan_chen 	switch (uart_idx) {
45927881d20Sryan_chen 	case 1:
46027881d20Sryan_chen 	case 2:
46127881d20Sryan_chen 	case 3:
46227881d20Sryan_chen 	case 4:
46327881d20Sryan_chen 	case 6:
46427881d20Sryan_chen 		if (uart_sel & BIT(uart_idx - 1))
4652e195992Sryan_chen 			uart_clk = ast2600_get_uart_huxclk_rate(scu);
466550e691bSryan_chen 		else
4672e195992Sryan_chen 			uart_clk = ast2600_get_uart_uxclk_rate(scu);
46827881d20Sryan_chen 		break;
46927881d20Sryan_chen 	case 5: //24mhz is come form usb phy 48Mhz
47027881d20Sryan_chen 	{
47127881d20Sryan_chen 		u8 uart5_clk_sel = 0;
47227881d20Sryan_chen 		//high bit
47327881d20Sryan_chen 		if (readl(&scu->misc_ctrl1) & BIT(12))
47427881d20Sryan_chen 			uart5_clk_sel = 0x2;
47527881d20Sryan_chen 		else
47627881d20Sryan_chen 			uart5_clk_sel = 0x0;
477550e691bSryan_chen 
47827881d20Sryan_chen 		if (readl(&scu->clk_sel2) & BIT(14))
47927881d20Sryan_chen 			uart5_clk_sel |= 0x1;
480550e691bSryan_chen 
48127881d20Sryan_chen 		switch (uart5_clk_sel) {
48227881d20Sryan_chen 		case 0:
48327881d20Sryan_chen 			uart_clk = 24000000;
48427881d20Sryan_chen 			break;
48527881d20Sryan_chen 		case 1:
486def99fcbSryan_chen 			uart_clk = 192000000;
48727881d20Sryan_chen 			break;
48827881d20Sryan_chen 		case 2:
48927881d20Sryan_chen 			uart_clk = 24000000 / 13;
49027881d20Sryan_chen 			break;
49127881d20Sryan_chen 		case 3:
49227881d20Sryan_chen 			uart_clk = 192000000 / 13;
49327881d20Sryan_chen 			break;
49427881d20Sryan_chen 		}
4955d05f4fcSRyan Chen 	} break;
49627881d20Sryan_chen 	case 7:
49727881d20Sryan_chen 	case 8:
49827881d20Sryan_chen 	case 9:
49927881d20Sryan_chen 	case 10:
50027881d20Sryan_chen 	case 11:
50127881d20Sryan_chen 	case 12:
50227881d20Sryan_chen 	case 13:
50327881d20Sryan_chen 		if (uart_sel5 & BIT(uart_idx - 1))
5042e195992Sryan_chen 			uart_clk = ast2600_get_uart_huxclk_rate(scu);
50527881d20Sryan_chen 		else
5062e195992Sryan_chen 			uart_clk = ast2600_get_uart_uxclk_rate(scu);
50727881d20Sryan_chen 		break;
50827881d20Sryan_chen 	}
50927881d20Sryan_chen 
51027881d20Sryan_chen 	return uart_clk;
511550e691bSryan_chen }
512550e691bSryan_chen 
513feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
514feb42054Sryan_chen {
515feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
516feb42054Sryan_chen 	ulong rate = 0;
517feb42054Sryan_chen 
518feb42054Sryan_chen 	switch (clk->id) {
519feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
520bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
521bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
522d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
523bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
524d812df15Sryan_chen 		break;
525feb42054Sryan_chen 	case ASPEED_CLK_AHB:
526feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
527feb42054Sryan_chen 		break;
5286fa1ef3dSryan_chen 	case ASPEED_CLK_APB1:
5296fa1ef3dSryan_chen 		rate = ast2600_get_pclk1(priv->scu);
5306fa1ef3dSryan_chen 		break;
5316fa1ef3dSryan_chen 	case ASPEED_CLK_APB2:
5326fa1ef3dSryan_chen 		rate = ast2600_get_pclk2(priv->scu);
533feb42054Sryan_chen 		break;
534bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
535bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
536bbbfb0c5Sryan_chen 		break;
537feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
538feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
539feb42054Sryan_chen 		break;
540feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
541feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
542feb42054Sryan_chen 		break;
543feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
544feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
545feb42054Sryan_chen 		break;
546feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
547feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
548feb42054Sryan_chen 		break;
549feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
550feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
551feb42054Sryan_chen 		break;
552c304f173Sryan_chen 	case ASPEED_CLK_BCLK:
553c304f173Sryan_chen 		rate = ast2600_get_bclk_rate(priv->scu);
554c304f173Sryan_chen 		break;
555f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
556f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
557f51926eeSryan_chen 		break;
558f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
559f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
560f51926eeSryan_chen 		break;
5612e195992Sryan_chen 	case ASPEED_CLK_UARTX:
5622e195992Sryan_chen 		rate = ast2600_get_uart_uxclk_rate(priv->scu);
5632e195992Sryan_chen 		break;
5640998ddefSryan_chen 	case ASPEED_CLK_HUARTX:
5652e195992Sryan_chen 		rate = ast2600_get_uart_huxclk_rate(priv->scu);
5662e195992Sryan_chen 		break;
567feb42054Sryan_chen 	default:
568d812df15Sryan_chen 		pr_debug("can't get clk rate\n");
569feb42054Sryan_chen 		return -ENOENT;
570feb42054Sryan_chen 	}
571feb42054Sryan_chen 
572feb42054Sryan_chen 	return rate;
573feb42054Sryan_chen }
574feb42054Sryan_chen 
575577fcdaeSDylan Hung /**
576577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
577577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
578577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
579a8fc7648SRyan Chen  * The function caller shall fill "pll->in" and "pll->out",
580a8fc7648SRyan Chen  * then this function will search the lookup table
581a8fc7648SRyan Chen  * to find a valid PLL divider configuration.
582550e691bSryan_chen  */
583577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
584550e691bSryan_chen {
585577fcdaeSDylan Hung 	u32 i;
586577fcdaeSDylan Hung 	bool is_found = false;
587550e691bSryan_chen 
588577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
589577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
590ed3899c5SRyan Chen 
591ed3899c5SRyan Chen 		if (def_cfg->in == pll->in && def_cfg->out == pll->out) {
592577fcdaeSDylan Hung 			is_found = true;
593577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
594577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
595577fcdaeSDylan Hung 			break;
596550e691bSryan_chen 		}
597550e691bSryan_chen 	}
598577fcdaeSDylan Hung 	return is_found;
599550e691bSryan_chen }
600ed3899c5SRyan Chen 
601fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu,
602fd52be0bSDylan Hung 				 struct ast2600_pll_cfg *p_cfg, int pll_idx)
603fd52be0bSDylan Hung {
604fd52be0bSDylan Hung 	u32 addr, addr_ext;
605fd52be0bSDylan Hung 	u32 reg;
606550e691bSryan_chen 
607fd52be0bSDylan Hung 	switch (pll_idx) {
608fd52be0bSDylan Hung 	case ASPEED_CLK_HPLL:
609fd52be0bSDylan Hung 		addr = (u32)(&scu->h_pll_param);
610fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->h_pll_ext_param);
611fd52be0bSDylan Hung 		break;
612fd52be0bSDylan Hung 	case ASPEED_CLK_MPLL:
613fd52be0bSDylan Hung 		addr = (u32)(&scu->m_pll_param);
614fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->m_pll_ext_param);
615fd52be0bSDylan Hung 		break;
616fd52be0bSDylan Hung 	case ASPEED_CLK_DPLL:
617fd52be0bSDylan Hung 		addr = (u32)(&scu->d_pll_param);
618fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->d_pll_ext_param);
619fd52be0bSDylan Hung 		break;
620fd52be0bSDylan Hung 	case ASPEED_CLK_EPLL:
621fd52be0bSDylan Hung 		addr = (u32)(&scu->e_pll_param);
622fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->e_pll_ext_param);
623fd52be0bSDylan Hung 		break;
624fd52be0bSDylan Hung 	default:
625fd52be0bSDylan Hung 		debug("unknown PLL index\n");
626fd52be0bSDylan Hung 		return 1;
627fd52be0bSDylan Hung 	}
628fd52be0bSDylan Hung 
629fd52be0bSDylan Hung 	p_cfg->reg.b.bypass = 0;
630fd52be0bSDylan Hung 	p_cfg->reg.b.off = 1;
631fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 1;
632fd52be0bSDylan Hung 
633fd52be0bSDylan Hung 	reg = readl(addr);
634fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
635fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
636fd52be0bSDylan Hung 	writel(reg, addr);
637fd52be0bSDylan Hung 
638fd52be0bSDylan Hung 	/* write extend parameter */
639fd52be0bSDylan Hung 	writel(p_cfg->ext_reg, addr_ext);
640fd52be0bSDylan Hung 	udelay(100);
641fd52be0bSDylan Hung 	p_cfg->reg.b.off = 0;
642fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 0;
643fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
644fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
645fd52be0bSDylan Hung 	writel(reg, addr);
646ed3899c5SRyan Chen 	while (!(readl(addr_ext) & BIT(31)))
647ed3899c5SRyan Chen 		;
648fd52be0bSDylan Hung 
649fd52be0bSDylan Hung 	return 0;
650fd52be0bSDylan Hung }
651ed3899c5SRyan Chen 
652feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
653550e691bSryan_chen {
654577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
655550e691bSryan_chen 
656577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
657577fcdaeSDylan Hung 	mpll.out = rate;
658f27685ebSRyan Chen 	if (ast2600_search_clock_config(&mpll) == false) {
659577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
660577fcdaeSDylan Hung 		return 0;
661577fcdaeSDylan Hung 	}
662ed3899c5SRyan Chen 	ast2600_configure_pll(scu, &mpll.cfg, ASPEED_CLK_MPLL);
663577fcdaeSDylan Hung 
664cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
665d6e349c7Sryan_chen }
666d6e349c7Sryan_chen 
667d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
668550e691bSryan_chen {
669f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
670550e691bSryan_chen 	ulong new_rate;
671ed3899c5SRyan Chen 
672550e691bSryan_chen 	switch (clk->id) {
673f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
674feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
675550e691bSryan_chen 		break;
676550e691bSryan_chen 	default:
677550e691bSryan_chen 		return -ENOENT;
678550e691bSryan_chen 	}
679550e691bSryan_chen 
680550e691bSryan_chen 	return new_rate;
681550e691bSryan_chen }
682feb42054Sryan_chen 
683f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1	(20)
684f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2	(21)
685f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3	(20)
686f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4	(21)
687f9aa0ee1Sryan_chen 
688a98c71fbSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu, struct udevice *dev)
689cc476ffcSDylan Hung {
690a98c71fbSDylan Hung 	union mac_delay_1g reg_1g;
691a98c71fbSDylan Hung 	union mac_delay_100_10 reg_100, reg_10;
692a98c71fbSDylan Hung 	struct mac_delay_config mac1_cfg, mac2_cfg;
693a98c71fbSDylan Hung 	int ret;
6944760b3f8SDylan Hung 
695a98c71fbSDylan Hung 	reg_1g.w = (readl(&scu->mac12_clk_delay) & ~GENMASK(25, 0)) |
696a98c71fbSDylan Hung 		   MAC_DEF_DELAY_1G;
697a98c71fbSDylan Hung 	reg_100.w = MAC_DEF_DELAY_100M;
698a98c71fbSDylan Hung 	reg_10.w = MAC_DEF_DELAY_10M;
699a98c71fbSDylan Hung 
700a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac0-clk-delay", (u32 *)&mac1_cfg, sizeof(mac1_cfg) / sizeof(u32));
701a98c71fbSDylan Hung 	if (!ret) {
702a98c71fbSDylan Hung 		reg_1g.b.tx_delay_1 = mac1_cfg.tx_delay_1000;
703a98c71fbSDylan Hung 		reg_1g.b.rx_delay_1 = mac1_cfg.rx_delay_1000;
704a98c71fbSDylan Hung 		reg_100.b.tx_delay_1 = mac1_cfg.tx_delay_100;
705a98c71fbSDylan Hung 		reg_100.b.rx_delay_1 = mac1_cfg.rx_delay_100;
706a98c71fbSDylan Hung 		reg_10.b.tx_delay_1 = mac1_cfg.tx_delay_10;
707a98c71fbSDylan Hung 		reg_10.b.rx_delay_1 = mac1_cfg.rx_delay_10;
708a98c71fbSDylan Hung 	}
709a98c71fbSDylan Hung 
710a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac1-clk-delay", (u32 *)&mac2_cfg, sizeof(mac2_cfg) / sizeof(u32));
711a98c71fbSDylan Hung 	if (!ret) {
712a98c71fbSDylan Hung 		reg_1g.b.tx_delay_2 = mac2_cfg.tx_delay_1000;
713a98c71fbSDylan Hung 		reg_1g.b.rx_delay_2 = mac2_cfg.rx_delay_1000;
714a98c71fbSDylan Hung 		reg_100.b.tx_delay_2 = mac2_cfg.tx_delay_100;
715a98c71fbSDylan Hung 		reg_100.b.rx_delay_2 = mac2_cfg.rx_delay_100;
716a98c71fbSDylan Hung 		reg_10.b.tx_delay_2 = mac2_cfg.tx_delay_10;
717a98c71fbSDylan Hung 		reg_10.b.rx_delay_2 = mac2_cfg.rx_delay_10;
718a98c71fbSDylan Hung 	}
719a98c71fbSDylan Hung 
720a98c71fbSDylan Hung 	writel(reg_1g.w, &scu->mac12_clk_delay);
721a98c71fbSDylan Hung 	writel(reg_100.w, &scu->mac12_clk_delay_100M);
722a98c71fbSDylan Hung 	writel(reg_10.w, &scu->mac12_clk_delay_10M);
723cc476ffcSDylan Hung 
724ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
725eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16));
726894c19cfSDylan Hung 
727cc476ffcSDylan Hung 	return 0;
728cc476ffcSDylan Hung }
729cc476ffcSDylan Hung 
730a98c71fbSDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu, struct udevice *dev)
73154f9cba1SDylan Hung {
732a98c71fbSDylan Hung 	union mac_delay_1g reg_1g;
733a98c71fbSDylan Hung 	union mac_delay_100_10 reg_100, reg_10;
734a98c71fbSDylan Hung 	struct mac_delay_config mac3_cfg, mac4_cfg;
735a98c71fbSDylan Hung 	int ret;
736a98c71fbSDylan Hung 
73754f9cba1SDylan Hung 	/*
738eff28274SJohnny Huang 	 * scu350[31]   RGMII 125M source: 0 = from IO pin
739eff28274SJohnny Huang 	 * scu350[25:0] MAC 1G delay
74054f9cba1SDylan Hung 	 */
741a98c71fbSDylan Hung 	reg_1g.w = (readl(&scu->mac34_clk_delay) & ~GENMASK(25, 0)) |
742a98c71fbSDylan Hung 		   MAC34_DEF_DELAY_1G;
743a98c71fbSDylan Hung 	reg_1g.b.rgmii_125m_o_sel = 0;
744a98c71fbSDylan Hung 	reg_100.w = MAC34_DEF_DELAY_100M;
745a98c71fbSDylan Hung 	reg_10.w = MAC34_DEF_DELAY_10M;
746a98c71fbSDylan Hung 
747a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac2-clk-delay", (u32 *)&mac3_cfg, sizeof(mac3_cfg) / sizeof(u32));
748a98c71fbSDylan Hung 	if (!ret) {
749a98c71fbSDylan Hung 		reg_1g.b.tx_delay_1 = mac3_cfg.tx_delay_1000;
750a98c71fbSDylan Hung 		reg_1g.b.rx_delay_1 = mac3_cfg.rx_delay_1000;
751a98c71fbSDylan Hung 		reg_100.b.tx_delay_1 = mac3_cfg.tx_delay_100;
752a98c71fbSDylan Hung 		reg_100.b.rx_delay_1 = mac3_cfg.rx_delay_100;
753a98c71fbSDylan Hung 		reg_10.b.tx_delay_1 = mac3_cfg.tx_delay_10;
754a98c71fbSDylan Hung 		reg_10.b.rx_delay_1 = mac3_cfg.rx_delay_10;
755a98c71fbSDylan Hung 	}
756a98c71fbSDylan Hung 
757a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac3-clk-delay", (u32 *)&mac4_cfg, sizeof(mac4_cfg) / sizeof(u32));
758a98c71fbSDylan Hung 	if (!ret) {
759a98c71fbSDylan Hung 		reg_1g.b.tx_delay_2 = mac4_cfg.tx_delay_1000;
760a98c71fbSDylan Hung 		reg_1g.b.rx_delay_2 = mac4_cfg.rx_delay_1000;
761a98c71fbSDylan Hung 		reg_100.b.tx_delay_2 = mac4_cfg.tx_delay_100;
762a98c71fbSDylan Hung 		reg_100.b.rx_delay_2 = mac4_cfg.rx_delay_100;
763a98c71fbSDylan Hung 		reg_10.b.tx_delay_2 = mac4_cfg.tx_delay_10;
764a98c71fbSDylan Hung 		reg_10.b.rx_delay_2 = mac4_cfg.rx_delay_10;
765a98c71fbSDylan Hung 	}
766a98c71fbSDylan Hung 
767a98c71fbSDylan Hung 	writel(reg_1g.w, &scu->mac34_clk_delay);
768a98c71fbSDylan Hung 	writel(reg_100.w, &scu->mac34_clk_delay_100M);
769a98c71fbSDylan Hung 	writel(reg_10.w, &scu->mac34_clk_delay_10M);
77054f9cba1SDylan Hung 
771eff28274SJohnny Huang 	/*
772eff28274SJohnny Huang 	 * clock source seletion and divider
773eff28274SJohnny Huang 	 * scu310[26:24] : MAC AHB bus clock = HCLK / 2
774eff28274SJohnny Huang 	 * scu310[18:16] : RMII 50M = HCLK_200M / 4
775eff28274SJohnny Huang 	 */
776eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)),
777eff28274SJohnny Huang 			((0x0 << 24) | (0x3 << 16)));
77854f9cba1SDylan Hung 
779eff28274SJohnny Huang 	/*
780eff28274SJohnny Huang 	 * set driving strength
781eff28274SJohnny Huang 	 * scu458[3:2] : MAC4 driving strength
782eff28274SJohnny Huang 	 * scu458[1:0] : MAC3 driving strength
783eff28274SJohnny Huang 	 */
784eff28274SJohnny Huang 	clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0),
785a961159eSDylan Hung 			(0x3 << 2) | (0x3 << 0));
78654f9cba1SDylan Hung 
78754f9cba1SDylan Hung 	return 0;
78854f9cba1SDylan Hung }
789eff28274SJohnny Huang 
79054f9cba1SDylan Hung /**
7915b5c3d44SDylan Hung  * ast2600 RGMII clock source tree
79254f9cba1SDylan Hung  * 125M from external PAD -------->|\
79354f9cba1SDylan Hung  * HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
79454f9cba1SDylan Hung  *         | |---->| divider |---->|/                             +
79554f9cba1SDylan Hung  * EPLL -->|/                                                     |
79654f9cba1SDylan Hung  *                                                                |
797eff28274SJohnny Huang  * +---------<-----------|RGMIICK PAD output enable|<-------------+
79854f9cba1SDylan Hung  * |
799eff28274SJohnny Huang  * +--------------------------->|\
80054f9cba1SDylan Hung  *                              | |----> RGMII 125M for MAC#3 & MAC#4
801eff28274SJohnny Huang  * HCLK 200M ---->|divider|---->|/
802eff28274SJohnny Huang  * To simplify the control flow:
803eff28274SJohnny Huang  * 1. RGMII 1/2 always use EPLL as the internal clock source
804eff28274SJohnny Huang  * 2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source
805eff28274SJohnny Huang  * 125M from external PAD -------->|\
806eff28274SJohnny Huang  *                                 | |---->RGMII 125M for MAC#1 & MAC#2
807eff28274SJohnny Huang  *         EPLL---->| divider |--->|/                             +
808eff28274SJohnny Huang  *                                                                |
809eff28274SJohnny Huang  * +<--------------------|RGMIICK PAD output enable|<-------------+
810eff28274SJohnny Huang  * |
811eff28274SJohnny Huang  * +--------------------------->RGMII 125M for MAC#3 & MAC#4
812eff28274SJohnny Huang  */
813eff28274SJohnny Huang #define RGMIICK_SRC_PAD		0
814eff28274SJohnny Huang #define RGMIICK_SRC_EPLL	1 /* recommended */
815eff28274SJohnny Huang #define RGMIICK_SRC_HPLL	2
816eff28274SJohnny Huang 
817eff28274SJohnny Huang #define RGMIICK_DIV2	1
818eff28274SJohnny Huang #define RGMIICK_DIV3	2
819eff28274SJohnny Huang #define RGMIICK_DIV4	3
820eff28274SJohnny Huang #define RGMIICK_DIV5	4
821eff28274SJohnny Huang #define RGMIICK_DIV6	5
822eff28274SJohnny Huang #define RGMIICK_DIV7	6
823eff28274SJohnny Huang #define RGMIICK_DIV8	7 /* recommended */
824eff28274SJohnny Huang 
825eff28274SJohnny Huang #define RMIICK_DIV4		0
826eff28274SJohnny Huang #define RMIICK_DIV8		1
827eff28274SJohnny Huang #define RMIICK_DIV12	2
828eff28274SJohnny Huang #define RMIICK_DIV16	3
829eff28274SJohnny Huang #define RMIICK_DIV20	4 /* recommended */
830eff28274SJohnny Huang #define RMIICK_DIV24	5
831eff28274SJohnny Huang #define RMIICK_DIV28	6
832eff28274SJohnny Huang #define RMIICK_DIV32	7
833eff28274SJohnny Huang 
834eff28274SJohnny Huang struct ast2600_mac_clk_div {
835eff28274SJohnny Huang 	u32 src; /* 0=external PAD, 1=internal PLL */
836eff28274SJohnny Huang 	u32 fin; /* divider input speed */
837eff28274SJohnny Huang 	u32 n; /* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */
838eff28274SJohnny Huang 	u32 fout; /* fout = fin / n */
839eff28274SJohnny Huang };
840eff28274SJohnny Huang 
841eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = {
842eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
843eff28274SJohnny Huang 	.fin = 1000000000,
844eff28274SJohnny Huang 	.n = RGMIICK_DIV8,
845eff28274SJohnny Huang 	.fout = 125000000,
846eff28274SJohnny Huang };
847eff28274SJohnny Huang 
848eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = {
849eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
850eff28274SJohnny Huang 	.fin = 1000000000,
851eff28274SJohnny Huang 	.n = RMIICK_DIV20,
852eff28274SJohnny Huang 	.fout = 50000000,
853eff28274SJohnny Huang };
854ed3899c5SRyan Chen 
855eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu,
856eff28274SJohnny Huang 				 struct ast2600_mac_clk_div *p_cfg)
857eff28274SJohnny Huang {
858eff28274SJohnny Huang 	struct ast2600_pll_desc pll;
859eff28274SJohnny Huang 
860eff28274SJohnny Huang 	pll.in = AST2600_CLK_IN;
861eff28274SJohnny Huang 	pll.out = p_cfg->fin;
862ed3899c5SRyan Chen 	if (ast2600_search_clock_config(&pll) == false) {
863ed3899c5SRyan Chen 		pr_err("unable to find valid ETHNET MAC clock setting\n");
8643f295164SRyan Chen 		debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w,
8653f295164SRyan Chen 		      pll.cfg.ext_reg);
8663f295164SRyan Chen 		debug("%s: pll cfg = %02x %02x %02x\n", __func__,
8673f295164SRyan Chen 		      pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p);
868eff28274SJohnny Huang 		return;
869eff28274SJohnny Huang 	}
870ed3899c5SRyan Chen 	ast2600_configure_pll(p_scu, &pll.cfg, p_cfg->src);
871eff28274SJohnny Huang }
872eff28274SJohnny Huang 
873eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu,
874eff28274SJohnny Huang 				   struct ast2600_mac_clk_div *p_cfg)
875eff28274SJohnny Huang {
876eff28274SJohnny Huang 	u32 reg_304 = readl(&p_scu->clk_sel2);
877eff28274SJohnny Huang 	u32 reg_340 = readl(&p_scu->mac12_clk_delay);
878eff28274SJohnny Huang 	u32 reg_350 = readl(&p_scu->mac34_clk_delay);
879eff28274SJohnny Huang 
880eff28274SJohnny Huang 	reg_340 &= ~GENMASK(31, 29);
881eff28274SJohnny Huang 	/* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */
882eff28274SJohnny Huang 	reg_340 |= BIT(28);
8833f295164SRyan Chen 	if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) {
884eff28274SJohnny Huang 		/*
885eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
886eff28274SJohnny Huang 		 * the divider setting
887eff28274SJohnny Huang 		 */
888ed3899c5SRyan Chen 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src))
889eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
890eff28274SJohnny Huang 		/* scu340[31]: select RGMII 125M from internal source */
891eff28274SJohnny Huang 		reg_340 |= BIT(31);
892eff28274SJohnny Huang 	}
893eff28274SJohnny Huang 
894eff28274SJohnny Huang 	reg_304 &= ~GENMASK(23, 20);
895eff28274SJohnny Huang 
896eff28274SJohnny Huang 	/* set clock divider */
897eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 20;
898eff28274SJohnny Huang 
899eff28274SJohnny Huang 	/* select internal clock source */
900ed3899c5SRyan Chen 	if (p_cfg->src == ASPEED_CLK_HPLL)
901eff28274SJohnny Huang 		reg_304 |= BIT(23);
902eff28274SJohnny Huang 
903eff28274SJohnny Huang 	/* RGMII 3/4 clock source select */
904eff28274SJohnny Huang 	reg_350 &= ~BIT(31);
905eff28274SJohnny Huang 
906eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
907eff28274SJohnny Huang 	writel(reg_340, &p_scu->mac12_clk_delay);
908eff28274SJohnny Huang 	writel(reg_350, &p_scu->mac34_clk_delay);
909eff28274SJohnny Huang }
910eff28274SJohnny Huang 
911eff28274SJohnny Huang /**
9125b5c3d44SDylan Hung  * ast2600 RMII/NCSI clock source tree
9135b5c3d44SDylan Hung  * HPLL -->|\
9145b5c3d44SDylan Hung  *         | |---->| divider |----> RMII 50M for MAC#1 & MAC#2
9155b5c3d44SDylan Hung  * EPLL -->|/
9165b5c3d44SDylan Hung  * HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4
91754f9cba1SDylan Hung  */
918eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu,
919eff28274SJohnny Huang 				  struct ast2600_mac_clk_div *p_cfg)
92054f9cba1SDylan Hung {
921eff28274SJohnny Huang 	u32 reg_304;
922eff28274SJohnny Huang 	u32 reg_310;
923eff28274SJohnny Huang 
9243f295164SRyan Chen 	if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) {
925eff28274SJohnny Huang 		/*
926eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
927eff28274SJohnny Huang 		 * the divider setting
928eff28274SJohnny Huang 		 */
929ed3899c5SRyan Chen 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src))
930eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
931eff28274SJohnny Huang 	}
93254f9cba1SDylan Hung 
933eff28274SJohnny Huang 	reg_304 = readl(&p_scu->clk_sel2);
934eff28274SJohnny Huang 	reg_310 = readl(&p_scu->clk_sel4);
935eff28274SJohnny Huang 
936eff28274SJohnny Huang 	reg_304 &= ~GENMASK(19, 16);
937eff28274SJohnny Huang 
938eff28274SJohnny Huang 	/* set RMII 1/2 clock divider */
939eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 16;
940eff28274SJohnny Huang 
941eff28274SJohnny Huang 	/* RMII clock source selection */
942ed3899c5SRyan Chen 	if (p_cfg->src == ASPEED_CLK_HPLL)
943eff28274SJohnny Huang 		reg_304 |= BIT(19);
944eff28274SJohnny Huang 
945eff28274SJohnny Huang 	/* set RMII 3/4 clock divider */
946eff28274SJohnny Huang 	reg_310 &= ~GENMASK(18, 16);
947eff28274SJohnny Huang 	reg_310 |= (0x3 << 16);
948eff28274SJohnny Huang 
949eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
950eff28274SJohnny Huang 	writel(reg_310, &p_scu->clk_sel4);
951eff28274SJohnny Huang }
952eff28274SJohnny Huang 
953f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
954f9aa0ee1Sryan_chen {
955f9aa0ee1Sryan_chen 	u32 reset_bit;
956f9aa0ee1Sryan_chen 	u32 clkstop_bit;
957f9aa0ee1Sryan_chen 
958f9aa0ee1Sryan_chen 	switch (index) {
959f9aa0ee1Sryan_chen 	case 1:
960f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
961f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
962f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
963f9aa0ee1Sryan_chen 		udelay(100);
964f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
965f9aa0ee1Sryan_chen 		mdelay(10);
966f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
967f9aa0ee1Sryan_chen 		break;
968f9aa0ee1Sryan_chen 	case 2:
969f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
970f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
971f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
972f9aa0ee1Sryan_chen 		udelay(100);
973f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
974f9aa0ee1Sryan_chen 		mdelay(10);
975f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
976f9aa0ee1Sryan_chen 		break;
977f9aa0ee1Sryan_chen 	case 3:
978f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
979f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
980f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
981f9aa0ee1Sryan_chen 		udelay(100);
982f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
983f9aa0ee1Sryan_chen 		mdelay(10);
984f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
985f9aa0ee1Sryan_chen 		break;
986f9aa0ee1Sryan_chen 	case 4:
987f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
988f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
989f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
990f9aa0ee1Sryan_chen 		udelay(100);
991f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
992f9aa0ee1Sryan_chen 		mdelay(10);
993f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
994f9aa0ee1Sryan_chen 		break;
995f9aa0ee1Sryan_chen 	default:
996f9aa0ee1Sryan_chen 		return -EINVAL;
997f9aa0ee1Sryan_chen 	}
998f9aa0ee1Sryan_chen 
999f9aa0ee1Sryan_chen 	return 0;
1000f9aa0ee1Sryan_chen }
1001550e691bSryan_chen 
1002a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_FROM_HPLL_CLK	BIT(19)
1003a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_CLK_MASK		GENMASK(27, 26)
1004ed3899c5SRyan Chen #define SCU_CLK_ECC_RSA_CLK_DIV(x)		((x) << 26)
1005a8fc7648SRyan Chen static void ast2600_configure_rsa_ecc_clk(struct ast2600_scu *scu)
1006a8fc7648SRyan Chen {
1007a8fc7648SRyan Chen 	u32 clk_sel = readl(&scu->clk_sel1);
1008a8fc7648SRyan Chen 
1009a8fc7648SRyan Chen 	/* Configure RSA clock = HPLL/3 */
1010a8fc7648SRyan Chen 	clk_sel |= SCU_CLK_ECC_RSA_FROM_HPLL_CLK;
1011a8fc7648SRyan Chen 	clk_sel &= ~SCU_CLK_ECC_RSA_CLK_MASK;
1012a8fc7648SRyan Chen 	clk_sel |= SCU_CLK_ECC_RSA_CLK_DIV(2);
1013a8fc7648SRyan Chen 
1014a8fc7648SRyan Chen 	writel(clk_sel, &scu->clk_sel1);
1015a8fc7648SRyan Chen }
1016a8fc7648SRyan Chen 
1017f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
1018f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
1019f51926eeSryan_chen {
1020f51926eeSryan_chen 	u32 reset_bit;
1021f51926eeSryan_chen 	u32 clkstop_bit;
1022f51926eeSryan_chen 
1023f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
1024f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
1025f51926eeSryan_chen 
1026fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
1027fc9f12e6Sryan_chen 
1028f51926eeSryan_chen 	udelay(100);
1029f51926eeSryan_chen 	//enable clk
1030f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1031f51926eeSryan_chen 	mdelay(10);
1032fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1033f51926eeSryan_chen 
1034f51926eeSryan_chen 	return 0;
1035f51926eeSryan_chen }
1036f51926eeSryan_chen 
1037f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD			31
1038f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
1039ed3899c5SRyan Chen #define SCU_CLK_SD_DIV(x)			((x) << 28)
10402cd7cba2Sryan_chen #define SCU_CLK_SD_FROM_APLL_CLK	BIT(8)
1041f51926eeSryan_chen 
1042f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
1043f51926eeSryan_chen {
1044f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
1045f51926eeSryan_chen 	u32 enableclk_bit;
10462cd7cba2Sryan_chen 	u32 rate = 0;
10472cd7cba2Sryan_chen 	u32 div = 0;
10482cd7cba2Sryan_chen 	int i = 0;
1049f51926eeSryan_chen 
1050f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
1051f51926eeSryan_chen 
1052a8fc7648SRyan Chen 	/* ast2600 sd controller max clk is 200Mhz :
1053a8fc7648SRyan Chen 	 * use apll for clock source 800/4 = 200 : controller max is 200mhz
1054a8fc7648SRyan Chen 	 */
10552cd7cba2Sryan_chen 	rate = ast2600_get_apll_rate(scu);
10562cd7cba2Sryan_chen 	for (i = 0; i < 8; i++) {
10572cd7cba2Sryan_chen 		div = (i + 1) * 2;
10582cd7cba2Sryan_chen 		if ((rate / div) <= 200000000)
10592cd7cba2Sryan_chen 			break;
10602cd7cba2Sryan_chen 	}
1061f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
10622cd7cba2Sryan_chen 	clk_sel |= SCU_CLK_SD_DIV(i) | SCU_CLK_SD_FROM_APLL_CLK;
1063f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
1064f51926eeSryan_chen 
1065f51926eeSryan_chen 	//enable clk
1066f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
1067f51926eeSryan_chen 
1068f51926eeSryan_chen 	return 0;
1069f51926eeSryan_chen }
1070f51926eeSryan_chen 
1071f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
1072f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
1073f51926eeSryan_chen {
1074f51926eeSryan_chen 	u32 reset_bit;
1075f51926eeSryan_chen 	u32 clkstop_bit;
1076f51926eeSryan_chen 
1077f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
1078f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
1079f51926eeSryan_chen 
1080fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1081f51926eeSryan_chen 	udelay(100);
1082f51926eeSryan_chen 	//enable clk
1083f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1084f51926eeSryan_chen 	mdelay(10);
1085fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1086f51926eeSryan_chen 
1087f51926eeSryan_chen 	return 0;
1088f51926eeSryan_chen }
1089f51926eeSryan_chen 
1090f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC			15
1091f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
1092ed3899c5SRyan Chen #define SCU_CLK_EMMC_DIV(x)			((x) << 12)
1093a8fc7648SRyan Chen #define SCU_CLK_EMMC_FROM_MPLL_CLK	BIT(11)
1094f51926eeSryan_chen 
1095f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
1096f51926eeSryan_chen {
109785d48d8cSryan_chen 	u32 revision_id = readl(&scu->chip_id1);
1098f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
1099ed3899c5SRyan Chen 	u32 enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC);
1100f4c4ddb1Sryan_chen 	u32 rate = 0;
1101f4c4ddb1Sryan_chen 	u32 div = 0;
1102f4c4ddb1Sryan_chen 	int i = 0;
1103f51926eeSryan_chen 
1104ed3899c5SRyan Chen 	/*
1105ed3899c5SRyan Chen 	 * ast2600 eMMC controller max clk is 200Mhz
1106ed3899c5SRyan Chen 	 * HPll->1/2->|\
1107ed3899c5SRyan Chen 	 *				|->SCU300[11]->SCU300[14:12][1/N] +
1108ed3899c5SRyan Chen 	 * MPLL------>|/								  |
1109ed3899c5SRyan Chen 	 * +----------------------------------------------+
1110ed3899c5SRyan Chen 	 * |
1111ed3899c5SRyan Chen 	 * +---------> EMMC12C[15:8][1/N]-> eMMC clk
1112a8fc7648SRyan Chen 	 */
111385d48d8cSryan_chen 	if (((revision_id & CHIP_REVISION_ID) >> 16)) {
11148c32294fSryan_chen 		//AST2600A1 : use mpll to be clk source
1115b0c30ea3Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
1116b0c30ea3Sryan_chen 		for (i = 0; i < 8; i++) {
1117b0c30ea3Sryan_chen 			div = (i + 1) * 2;
1118b0c30ea3Sryan_chen 			if ((rate / div) <= 200000000)
1119b0c30ea3Sryan_chen 				break;
1120b0c30ea3Sryan_chen 		}
1121b0c30ea3Sryan_chen 
1122b0c30ea3Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
11232cd7cba2Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i) | SCU_CLK_EMMC_FROM_MPLL_CLK;
1124b0c30ea3Sryan_chen 		writel(clk_sel, &scu->clk_sel1);
1125b0c30ea3Sryan_chen 
1126b0c30ea3Sryan_chen 	} else {
11272cd7cba2Sryan_chen 		//AST2600A0 : use hpll to be clk source
1128f4c4ddb1Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1129f4c4ddb1Sryan_chen 
1130f4c4ddb1Sryan_chen 		for (i = 0; i < 8; i++) {
1131f4c4ddb1Sryan_chen 			div = (i + 1) * 4;
1132f4c4ddb1Sryan_chen 			if ((rate / div) <= 200000000)
1133f4c4ddb1Sryan_chen 				break;
1134f4c4ddb1Sryan_chen 		}
1135f4c4ddb1Sryan_chen 
1136f4c4ddb1Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
1137f4c4ddb1Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i);
1138f51926eeSryan_chen 		writel(clk_sel, &scu->clk_sel1);
1139b0c30ea3Sryan_chen 	}
1140f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
1141f51926eeSryan_chen 
1142f51926eeSryan_chen 	return 0;
1143f51926eeSryan_chen }
1144f51926eeSryan_chen 
1145baf00c26Sryan_chen #define SCU_CLKSTOP_FSICLK 30
1146baf00c26Sryan_chen 
1147baf00c26Sryan_chen static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu)
1148baf00c26Sryan_chen {
1149baf00c26Sryan_chen 	u32 reset_bit;
1150baf00c26Sryan_chen 	u32 clkstop_bit;
1151baf00c26Sryan_chen 
1152baf00c26Sryan_chen 	reset_bit = BIT(ASPEED_RESET_FSI % 32);
1153baf00c26Sryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_FSICLK);
1154baf00c26Sryan_chen 
1155baf00c26Sryan_chen 	/* The FSI clock is shared between masters. If it's already on
1156ed3899c5SRyan Chen 	 * don't touch it, as that will reset the existing master.
1157ed3899c5SRyan Chen 	 */
1158baf00c26Sryan_chen 	if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) {
1159baf00c26Sryan_chen 		debug("%s: already running, not touching it\n", __func__);
1160baf00c26Sryan_chen 		return 0;
1161baf00c26Sryan_chen 	}
1162baf00c26Sryan_chen 
1163baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
1164baf00c26Sryan_chen 	udelay(100);
1165baf00c26Sryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1166baf00c26Sryan_chen 	mdelay(10);
1167baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1168baf00c26Sryan_chen 
1169baf00c26Sryan_chen 	return 0;
1170baf00c26Sryan_chen }
1171baf00c26Sryan_chen 
1172b8ec5ceaSryan_chen static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu)
1173b8ec5ceaSryan_chen {
1174b8ec5ceaSryan_chen 	u32 reset_bit;
1175b8ec5ceaSryan_chen 	u32 clkstop_bit;
1176b8ec5ceaSryan_chen 
1177b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P1);
1178b8ec5ceaSryan_chen 	clkstop_bit = BIT(14);
1179b8ec5ceaSryan_chen 
1180b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1181b8ec5ceaSryan_chen 	udelay(100);
1182b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_ctrl1);
1183b8ec5ceaSryan_chen 	mdelay(20);
1184b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1185b8ec5ceaSryan_chen 
1186b8ec5ceaSryan_chen 	return 0;
1187b8ec5ceaSryan_chen }
1188b8ec5ceaSryan_chen 
1189b8ec5ceaSryan_chen static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu)
1190b8ec5ceaSryan_chen {
1191b8ec5ceaSryan_chen 	u32 reset_bit;
1192b8ec5ceaSryan_chen 	u32 clkstop_bit;
1193b8ec5ceaSryan_chen 
1194b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P2);
1195b8ec5ceaSryan_chen 	clkstop_bit = BIT(7);
1196b8ec5ceaSryan_chen 
1197b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1198b8ec5ceaSryan_chen 	udelay(100);
1199b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1200b8ec5ceaSryan_chen 	mdelay(20);
1201b8ec5ceaSryan_chen 
1202b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1203b8ec5ceaSryan_chen 
1204b8ec5ceaSryan_chen 	return 0;
1205b8ec5ceaSryan_chen }
1206b8ec5ceaSryan_chen 
1207d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
1208550e691bSryan_chen {
1209f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
1210550e691bSryan_chen 
1211550e691bSryan_chen 	switch (clk->id) {
121286f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC1CLK:
121386f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 1);
1214550e691bSryan_chen 		break;
121586f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC2CLK:
121686f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 2);
1217550e691bSryan_chen 		break;
121877843939Sryan_chen 	case ASPEED_CLK_GATE_MAC3CLK:
121977843939Sryan_chen 		ast2600_configure_mac(priv->scu, 3);
122077843939Sryan_chen 		break;
122177843939Sryan_chen 	case ASPEED_CLK_GATE_MAC4CLK:
122277843939Sryan_chen 		ast2600_configure_mac(priv->scu, 4);
122377843939Sryan_chen 		break;
1224f51926eeSryan_chen 	case ASPEED_CLK_GATE_SDCLK:
1225f51926eeSryan_chen 		ast2600_enable_sdclk(priv->scu);
1226f51926eeSryan_chen 		break;
1227f51926eeSryan_chen 	case ASPEED_CLK_GATE_SDEXTCLK:
1228f51926eeSryan_chen 		ast2600_enable_extsdclk(priv->scu);
1229f51926eeSryan_chen 		break;
1230f51926eeSryan_chen 	case ASPEED_CLK_GATE_EMMCCLK:
1231f51926eeSryan_chen 		ast2600_enable_emmcclk(priv->scu);
1232f51926eeSryan_chen 		break;
1233f51926eeSryan_chen 	case ASPEED_CLK_GATE_EMMCEXTCLK:
1234f51926eeSryan_chen 		ast2600_enable_extemmcclk(priv->scu);
1235f51926eeSryan_chen 		break;
1236baf00c26Sryan_chen 	case ASPEED_CLK_GATE_FSICLK:
1237baf00c26Sryan_chen 		ast2600_enable_fsiclk(priv->scu);
1238baf00c26Sryan_chen 		break;
1239b8ec5ceaSryan_chen 	case ASPEED_CLK_GATE_USBPORT1CLK:
1240b8ec5ceaSryan_chen 		ast2600_enable_usbahclk(priv->scu);
1241b8ec5ceaSryan_chen 		break;
1242b8ec5ceaSryan_chen 	case ASPEED_CLK_GATE_USBPORT2CLK:
1243b8ec5ceaSryan_chen 		ast2600_enable_usbbhclk(priv->scu);
1244b8ec5ceaSryan_chen 		break;
1245550e691bSryan_chen 	default:
1246ed3899c5SRyan Chen 		pr_err("can't enable clk\n");
1247550e691bSryan_chen 		return -ENOENT;
1248550e691bSryan_chen 	}
1249550e691bSryan_chen 
1250550e691bSryan_chen 	return 0;
1251550e691bSryan_chen }
1252550e691bSryan_chen 
1253f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
1254d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
1255d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
1256d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
1257550e691bSryan_chen };
1258550e691bSryan_chen 
1259d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
1260550e691bSryan_chen {
1261f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
126261ab9607Sryan_chen 	u32 uart_clk_source;
1263550e691bSryan_chen 
1264f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
1265f0d895afSryan_chen 	if (IS_ERR(priv->scu))
1266f0d895afSryan_chen 		return PTR_ERR(priv->scu);
1267550e691bSryan_chen 
12685d05f4fcSRyan Chen 	uart_clk_source = dev_read_u32_default(dev, "uart-clk-source", 0x0);
126961ab9607Sryan_chen 
127061ab9607Sryan_chen 	if (uart_clk_source) {
127156dd3e85Sryan_chen 		if (uart_clk_source & GENMASK(5, 0))
12725d05f4fcSRyan Chen 			setbits_le32(&priv->scu->clk_sel4,
12735d05f4fcSRyan Chen 				     uart_clk_source & GENMASK(5, 0));
127456dd3e85Sryan_chen 		if (uart_clk_source & GENMASK(12, 6))
12755d05f4fcSRyan Chen 			setbits_le32(&priv->scu->clk_sel5,
12765d05f4fcSRyan Chen 				     uart_clk_source & GENMASK(12, 6));
127761ab9607Sryan_chen 	}
127861ab9607Sryan_chen 
1279b89500a2SDylan Hung 	ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig);
1280b89500a2SDylan Hung 	ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig);
1281a98c71fbSDylan Hung 	ast2600_configure_mac12_clk(priv->scu, dev);
1282a98c71fbSDylan Hung 	ast2600_configure_mac34_clk(priv->scu, dev);
1283a8fc7648SRyan Chen 	ast2600_configure_rsa_ecc_clk(priv->scu);
1284fd0306aaSJohnny Huang 
1285550e691bSryan_chen 	return 0;
1286550e691bSryan_chen }
1287550e691bSryan_chen 
1288d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
1289550e691bSryan_chen {
1290550e691bSryan_chen 	int ret;
1291550e691bSryan_chen 
1292550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
1293550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
1294550e691bSryan_chen 	if (ret)
1295550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
1296550e691bSryan_chen 
1297550e691bSryan_chen 	return 0;
1298550e691bSryan_chen }
1299550e691bSryan_chen 
1300d35ac78cSryan_chen struct aspeed_clks {
1301d35ac78cSryan_chen 	ulong id;
1302d35ac78cSryan_chen 	const char *name;
1303d35ac78cSryan_chen };
1304d35ac78cSryan_chen 
1305d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
13065d05f4fcSRyan Chen 	{ ASPEED_CLK_HPLL, "hpll" },     { ASPEED_CLK_MPLL, "mpll" },
13075d05f4fcSRyan Chen 	{ ASPEED_CLK_APLL, "apll" },     { ASPEED_CLK_EPLL, "epll" },
13085d05f4fcSRyan Chen 	{ ASPEED_CLK_DPLL, "dpll" },     { ASPEED_CLK_AHB, "hclk" },
13095d05f4fcSRyan Chen 	{ ASPEED_CLK_APB1, "pclk1" },    { ASPEED_CLK_APB2, "pclk2" },
13105d05f4fcSRyan Chen 	{ ASPEED_CLK_BCLK, "bclk" },     { ASPEED_CLK_UARTX, "uxclk" },
1311def99fcbSryan_chen 	{ ASPEED_CLK_HUARTX, "huxclk" },
1312d35ac78cSryan_chen };
1313d35ac78cSryan_chen 
1314d35ac78cSryan_chen int soc_clk_dump(void)
1315d35ac78cSryan_chen {
1316d35ac78cSryan_chen 	struct udevice *dev;
1317d35ac78cSryan_chen 	struct clk clk;
1318d35ac78cSryan_chen 	unsigned long rate;
1319d35ac78cSryan_chen 	int i, ret;
1320d35ac78cSryan_chen 
13215d05f4fcSRyan Chen 	ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu),
13225d05f4fcSRyan Chen 					  &dev);
1323d35ac78cSryan_chen 	if (ret)
1324d35ac78cSryan_chen 		return ret;
1325d35ac78cSryan_chen 
1326d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
1327d35ac78cSryan_chen 
1328d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
1329d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
1330d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
1331d35ac78cSryan_chen 		if (ret < 0) {
1332d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
1333d35ac78cSryan_chen 			continue;
1334d35ac78cSryan_chen 		}
1335d35ac78cSryan_chen 
1336d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
1337d35ac78cSryan_chen 		rate = ret;
1338d35ac78cSryan_chen 
1339d35ac78cSryan_chen 		clk_free(&clk);
1340d35ac78cSryan_chen 
1341d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
1342d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
1343d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
1344d35ac78cSryan_chen 			continue;
1345d35ac78cSryan_chen 		}
1346d35ac78cSryan_chen 		if (ret < 0) {
13475d05f4fcSRyan Chen 			printf("%s %lu: get_rate err: %d\n", __func__,
13485d05f4fcSRyan Chen 			       aspeed_clk_names[i].id, ret);
1349d35ac78cSryan_chen 			continue;
1350d35ac78cSryan_chen 		}
1351d35ac78cSryan_chen 
13525d05f4fcSRyan Chen 		printf("%s(%3lu):\t%lu\n", aspeed_clk_names[i].name,
13535d05f4fcSRyan Chen 		       aspeed_clk_names[i].id, rate);
1354d35ac78cSryan_chen 	}
1355d35ac78cSryan_chen 
1356d35ac78cSryan_chen 	return 0;
1357d35ac78cSryan_chen }
1358d35ac78cSryan_chen 
1359d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
13605d05f4fcSRyan Chen 	{
13615d05f4fcSRyan Chen 		.compatible = "aspeed,ast2600-scu",
13625d05f4fcSRyan Chen 	},
1363550e691bSryan_chen 	{}
1364550e691bSryan_chen };
1365550e691bSryan_chen 
1366aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
1367aa36597fSDylan Hung 	.name = "aspeed_scu",
1368550e691bSryan_chen 	.id = UCLASS_CLK,
1369d6e349c7Sryan_chen 	.of_match = ast2600_clk_ids,
1370f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
1371f9aa0ee1Sryan_chen 	.ops = &ast2600_clk_ops,
1372d6e349c7Sryan_chen 	.bind = ast2600_clk_bind,
1373d6e349c7Sryan_chen 	.probe = ast2600_clk_probe,
1374550e691bSryan_chen };
1375