xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision a98c71fb5f0bb7c5af14251a768a5f763759b2d1)
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 
114*a98c71fbSDylan Hung union mac_delay_1g {
115*a98c71fbSDylan Hung 	u32 w;
116*a98c71fbSDylan Hung 	struct {
117*a98c71fbSDylan Hung 		unsigned int tx_delay_1		: 6;	/* bit[5:0] */
118*a98c71fbSDylan Hung 		unsigned int tx_delay_2		: 6;	/* bit[11:6] */
119*a98c71fbSDylan Hung 		unsigned int rx_delay_1		: 6;	/* bit[17:12] */
120*a98c71fbSDylan Hung 		unsigned int rx_delay_2		: 6;	/* bit[23:18] */
121*a98c71fbSDylan Hung 		unsigned int rx_clk_inv_1 	: 1;	/* bit[24] */
122*a98c71fbSDylan Hung 		unsigned int rx_clk_inv_2 	: 1;	/* bit[25] */
123*a98c71fbSDylan Hung 		unsigned int rmii_tx_data_at_falling_1 : 1; /* bit[26] */
124*a98c71fbSDylan Hung 		unsigned int rmii_tx_data_at_falling_2 : 1; /* bit[27] */
125*a98c71fbSDylan Hung 		unsigned int rgmiick_pad_dir	: 1;	/* bit[28] */
126*a98c71fbSDylan Hung 		unsigned int rmii_50m_oe_1 	: 1;	/* bit[29] */
127*a98c71fbSDylan Hung 		unsigned int rmii_50m_oe_2	: 1;	/* bit[30] */
128*a98c71fbSDylan Hung 		unsigned int rgmii_125m_o_sel 	: 1;	/* bit[31] */
129*a98c71fbSDylan Hung 	} b;
130*a98c71fbSDylan Hung };
131*a98c71fbSDylan Hung 
132*a98c71fbSDylan Hung union mac_delay_100_10 {
133*a98c71fbSDylan Hung 	u32 w;
134*a98c71fbSDylan Hung 	struct {
135*a98c71fbSDylan Hung 		unsigned int tx_delay_1		: 6;	/* bit[5:0] */
136*a98c71fbSDylan Hung 		unsigned int tx_delay_2		: 6;	/* bit[11:6] */
137*a98c71fbSDylan Hung 		unsigned int rx_delay_1		: 6;	/* bit[17:12] */
138*a98c71fbSDylan Hung 		unsigned int rx_delay_2		: 6;	/* bit[23:18] */
139*a98c71fbSDylan Hung 		unsigned int rx_clk_inv_1 	: 1;	/* bit[24] */
140*a98c71fbSDylan Hung 		unsigned int rx_clk_inv_2 	: 1;	/* bit[25] */
141*a98c71fbSDylan Hung 		unsigned int reserved_0 	: 6;	/* bit[31:26] */
142*a98c71fbSDylan Hung 	} b;
143*a98c71fbSDylan Hung };
144*a98c71fbSDylan Hung 
145*a98c71fbSDylan Hung struct mac_delay_config {
146*a98c71fbSDylan Hung 	u32 tx_delay_1000;
147*a98c71fbSDylan Hung 	u32 rx_delay_1000;
148*a98c71fbSDylan Hung 	u32 tx_delay_100;
149*a98c71fbSDylan Hung 	u32 rx_delay_100;
150*a98c71fbSDylan Hung 	u32 tx_delay_10;
151*a98c71fbSDylan Hung 	u32 rx_delay_10;
152*a98c71fbSDylan Hung };
153*a98c71fbSDylan 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 
216a8fc7648SRyan Chen 	if (((hw_rev & CHIP_REVISION_ID) >> 16) >= 3) {
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 
23285d48d8cSryan_chen 	} else {
23339283ea7Sryan_chen 		if (apll_reg & BIT(20)) {
234d6e349c7Sryan_chen 			/* Pass through mode */
235ed3899c5SRyan Chen 			mult = 1;
236ed3899c5SRyan Chen 			div = 1;
237d6e349c7Sryan_chen 		} else {
238bbbfb0c5Sryan_chen 			/* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
23939283ea7Sryan_chen 			u32 m = (apll_reg >> 5) & 0x3f;
24039283ea7Sryan_chen 			u32 od = (apll_reg >> 4) & 0x1;
24139283ea7Sryan_chen 			u32 n = apll_reg & 0xf;
242d6e349c7Sryan_chen 
243bbbfb0c5Sryan_chen 			mult = (2 - od) * (m + 2);
244bbbfb0c5Sryan_chen 			div = n + 1;
245d6e349c7Sryan_chen 		}
24685d48d8cSryan_chen 	}
247a8fc7648SRyan Chen 
248bbbfb0c5Sryan_chen 	return ((clkin * mult) / div);
24939283ea7Sryan_chen }
25039283ea7Sryan_chen 
251d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
2525d05f4fcSRyan Chen 	2,
2535d05f4fcSRyan Chen 	2,
2545d05f4fcSRyan Chen 	3,
2555d05f4fcSRyan Chen 	4,
256d812df15Sryan_chen };
257d812df15Sryan_chen 
25845e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div0_table[] = {
2595d05f4fcSRyan Chen 	3,
2605d05f4fcSRyan Chen 	2,
2615d05f4fcSRyan Chen 	3,
2625d05f4fcSRyan Chen 	4,
26345e0908aSryan_chen };
26445e0908aSryan_chen 
26545e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div1_table[] = {
2665d05f4fcSRyan Chen 	3,
2675d05f4fcSRyan Chen 	4,
2685d05f4fcSRyan Chen 	6,
2695d05f4fcSRyan Chen 	8,
270e29dc694Sryan_chen };
271e29dc694Sryan_chen 
272e29dc694Sryan_chen static u32 ast2600_a1_axi_ahb_default_table[] = {
273e29dc694Sryan_chen 	3, 4, 3, 4, 2, 2, 2, 2,
274d812df15Sryan_chen };
275d812df15Sryan_chen 
276d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
277d812df15Sryan_chen {
27885d48d8cSryan_chen 	u32 hw_rev = readl(&scu->chip_id1);
27945e0908aSryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
280d812df15Sryan_chen 	u32 axi_div = 1;
281d812df15Sryan_chen 	u32 ahb_div = 0;
282d812df15Sryan_chen 	u32 rate = 0;
283d812df15Sryan_chen 
28485d48d8cSryan_chen 	if ((hw_rev & CHIP_REVISION_ID) >> 16) {
285a8fc7648SRyan Chen 		//After A0
28645e0908aSryan_chen 		if (hwstrap1 & BIT(16)) {
287a8fc7648SRyan Chen 			ast2600_a1_axi_ahb_div1_table[0] =
2885d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) &
2895d05f4fcSRyan Chen 								 0x3];
290d812df15Sryan_chen 			axi_div = 1;
2915d05f4fcSRyan Chen 			ahb_div =
2925d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_div1_table[(hwstrap1 >> 11) &
2935d05f4fcSRyan Chen 							      0x3];
29445e0908aSryan_chen 		} else {
295a8fc7648SRyan Chen 			ast2600_a1_axi_ahb_div0_table[0] =
2965d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) &
2975d05f4fcSRyan Chen 								 0x3];
298d812df15Sryan_chen 			axi_div = 2;
2995d05f4fcSRyan Chen 			ahb_div =
3005d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_div0_table[(hwstrap1 >> 11) &
3015d05f4fcSRyan Chen 							      0x3];
30245e0908aSryan_chen 		}
30345e0908aSryan_chen 	} else {
304a8fc7648SRyan Chen 		//A0 : fix axi = hpll / 2
30545e0908aSryan_chen 		axi_div = 2;
306d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
30745e0908aSryan_chen 	}
308bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
309a8fc7648SRyan Chen 
3102717883aSryan_chen 	return (rate / axi_div / ahb_div);
3112717883aSryan_chen }
3122717883aSryan_chen 
313c304f173Sryan_chen static u32 ast2600_get_bclk_rate(struct ast2600_scu *scu)
314c304f173Sryan_chen {
315c304f173Sryan_chen 	u32 rate;
316c304f173Sryan_chen 	u32 bclk_sel = (readl(&scu->clk_sel1) >> 20) & 0x7;
317ed3899c5SRyan Chen 
318c304f173Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
319c304f173Sryan_chen 
320c304f173Sryan_chen 	return (rate / ((bclk_sel + 1) * 4));
321c304f173Sryan_chen }
322c304f173Sryan_chen 
3236fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = {
3242717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
3252717883aSryan_chen };
3262717883aSryan_chen 
3276fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = {
3286fa1ef3dSryan_chen 	2, 4, 6, 8, 10, 12, 14, 16,
3296fa1ef3dSryan_chen };
3306fa1ef3dSryan_chen 
3316fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu)
3322717883aSryan_chen {
3332717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
3346fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)];
335bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
3362717883aSryan_chen 
3372717883aSryan_chen 	return (rate / apb_div);
338d812df15Sryan_chen }
339d812df15Sryan_chen 
3406fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu)
3416fa1ef3dSryan_chen {
3426fa1ef3dSryan_chen 	u32 clk_sel4 = readl(&scu->clk_sel4);
3436fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)];
3446fa1ef3dSryan_chen 	u32 rate = ast2600_get_hclk(scu);
3456fa1ef3dSryan_chen 
3466fa1ef3dSryan_chen 	return (rate / apb_div);
3476fa1ef3dSryan_chen }
3486fa1ef3dSryan_chen 
3492e195992Sryan_chen static u32 ast2600_get_uxclk_in_rate(struct ast2600_scu *scu)
350d6e349c7Sryan_chen {
35127881d20Sryan_chen 	u32 clk_in = 0;
3522e195992Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel5);
353550e691bSryan_chen 
35427881d20Sryan_chen 	uxclk_sel &= 0x3;
35527881d20Sryan_chen 	switch (uxclk_sel) {
35627881d20Sryan_chen 	case 0:
35727881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 4;
35827881d20Sryan_chen 		break;
35927881d20Sryan_chen 	case 1:
36027881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 2;
36127881d20Sryan_chen 		break;
36227881d20Sryan_chen 	case 2:
36327881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu);
36427881d20Sryan_chen 		break;
36527881d20Sryan_chen 	case 3:
36627881d20Sryan_chen 		clk_in = ast2600_get_hclk(scu);
36727881d20Sryan_chen 		break;
36827881d20Sryan_chen 	}
369d6e349c7Sryan_chen 
37027881d20Sryan_chen 	return clk_in;
37127881d20Sryan_chen }
37227881d20Sryan_chen 
3732e195992Sryan_chen static u32 ast2600_get_huxclk_in_rate(struct ast2600_scu *scu)
37427881d20Sryan_chen {
37527881d20Sryan_chen 	u32 clk_in = 0;
3762e195992Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel5);
37727881d20Sryan_chen 
37827881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
37927881d20Sryan_chen 	switch (huclk_sel) {
38027881d20Sryan_chen 	case 0:
38127881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 4;
38227881d20Sryan_chen 		break;
38327881d20Sryan_chen 	case 1:
38427881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 2;
38527881d20Sryan_chen 		break;
38627881d20Sryan_chen 	case 2:
38727881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu);
38827881d20Sryan_chen 		break;
38927881d20Sryan_chen 	case 3:
39027881d20Sryan_chen 		clk_in = ast2600_get_hclk(scu);
39127881d20Sryan_chen 		break;
39227881d20Sryan_chen 	}
39327881d20Sryan_chen 
39427881d20Sryan_chen 	return clk_in;
39527881d20Sryan_chen }
39627881d20Sryan_chen 
3972e195992Sryan_chen static u32 ast2600_get_uart_uxclk_rate(struct ast2600_scu *scu)
39827881d20Sryan_chen {
3992e195992Sryan_chen 	u32 clk_in = ast2600_get_uxclk_in_rate(scu);
40027881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
40127881d20Sryan_chen 	unsigned int mult, div;
40227881d20Sryan_chen 
40327881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
40427881d20Sryan_chen 	u32 r = div_reg & 0xff;
40527881d20Sryan_chen 
40627881d20Sryan_chen 	mult = r;
4072e195992Sryan_chen 	div = (n * 2);
40827881d20Sryan_chen 	return (clk_in * mult) / div;
40927881d20Sryan_chen }
41027881d20Sryan_chen 
4112e195992Sryan_chen static u32 ast2600_get_uart_huxclk_rate(struct ast2600_scu *scu)
41227881d20Sryan_chen {
4132e195992Sryan_chen 	u32 clk_in = ast2600_get_huxclk_in_rate(scu);
41427881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
41527881d20Sryan_chen 
41627881d20Sryan_chen 	unsigned int mult, div;
41727881d20Sryan_chen 
41827881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
41927881d20Sryan_chen 	u32 r = div_reg & 0xff;
42027881d20Sryan_chen 
42127881d20Sryan_chen 	mult = r;
4222e195992Sryan_chen 	div = (n * 2);
42327881d20Sryan_chen 	return (clk_in * mult) / div;
42427881d20Sryan_chen }
42527881d20Sryan_chen 
426f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
427f51926eeSryan_chen {
428f51926eeSryan_chen 	u32 clkin = 0;
429f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
430f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
431f51926eeSryan_chen 
432ed3899c5SRyan Chen 	if (clk_sel & BIT(8))
433f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
434ed3899c5SRyan Chen 	else
43510069884Sryan_chen 		clkin = ast2600_get_hclk(scu);
436ed3899c5SRyan Chen 
437f51926eeSryan_chen 	div = (div + 1) << 1;
438f51926eeSryan_chen 
439f51926eeSryan_chen 	return (clkin / div);
440f51926eeSryan_chen }
441f51926eeSryan_chen 
442f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
443f51926eeSryan_chen {
444bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
445f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
446f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
447f51926eeSryan_chen 
448f51926eeSryan_chen 	div = (div + 1) << 2;
449f51926eeSryan_chen 
450f51926eeSryan_chen 	return (clkin / div);
451f51926eeSryan_chen }
452f51926eeSryan_chen 
453f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
45427881d20Sryan_chen {
45527881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
45627881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
45727881d20Sryan_chen 	ulong uart_clk = 0;
45827881d20Sryan_chen 
45927881d20Sryan_chen 	switch (uart_idx) {
46027881d20Sryan_chen 	case 1:
46127881d20Sryan_chen 	case 2:
46227881d20Sryan_chen 	case 3:
46327881d20Sryan_chen 	case 4:
46427881d20Sryan_chen 	case 6:
46527881d20Sryan_chen 		if (uart_sel & BIT(uart_idx - 1))
4662e195992Sryan_chen 			uart_clk = ast2600_get_uart_huxclk_rate(scu);
467550e691bSryan_chen 		else
4682e195992Sryan_chen 			uart_clk = ast2600_get_uart_uxclk_rate(scu);
46927881d20Sryan_chen 		break;
47027881d20Sryan_chen 	case 5: //24mhz is come form usb phy 48Mhz
47127881d20Sryan_chen 	{
47227881d20Sryan_chen 		u8 uart5_clk_sel = 0;
47327881d20Sryan_chen 		//high bit
47427881d20Sryan_chen 		if (readl(&scu->misc_ctrl1) & BIT(12))
47527881d20Sryan_chen 			uart5_clk_sel = 0x2;
47627881d20Sryan_chen 		else
47727881d20Sryan_chen 			uart5_clk_sel = 0x0;
478550e691bSryan_chen 
47927881d20Sryan_chen 		if (readl(&scu->clk_sel2) & BIT(14))
48027881d20Sryan_chen 			uart5_clk_sel |= 0x1;
481550e691bSryan_chen 
48227881d20Sryan_chen 		switch (uart5_clk_sel) {
48327881d20Sryan_chen 		case 0:
48427881d20Sryan_chen 			uart_clk = 24000000;
48527881d20Sryan_chen 			break;
48627881d20Sryan_chen 		case 1:
487def99fcbSryan_chen 			uart_clk = 192000000;
48827881d20Sryan_chen 			break;
48927881d20Sryan_chen 		case 2:
49027881d20Sryan_chen 			uart_clk = 24000000 / 13;
49127881d20Sryan_chen 			break;
49227881d20Sryan_chen 		case 3:
49327881d20Sryan_chen 			uart_clk = 192000000 / 13;
49427881d20Sryan_chen 			break;
49527881d20Sryan_chen 		}
4965d05f4fcSRyan Chen 	} break;
49727881d20Sryan_chen 	case 7:
49827881d20Sryan_chen 	case 8:
49927881d20Sryan_chen 	case 9:
50027881d20Sryan_chen 	case 10:
50127881d20Sryan_chen 	case 11:
50227881d20Sryan_chen 	case 12:
50327881d20Sryan_chen 	case 13:
50427881d20Sryan_chen 		if (uart_sel5 & BIT(uart_idx - 1))
5052e195992Sryan_chen 			uart_clk = ast2600_get_uart_huxclk_rate(scu);
50627881d20Sryan_chen 		else
5072e195992Sryan_chen 			uart_clk = ast2600_get_uart_uxclk_rate(scu);
50827881d20Sryan_chen 		break;
50927881d20Sryan_chen 	}
51027881d20Sryan_chen 
51127881d20Sryan_chen 	return uart_clk;
512550e691bSryan_chen }
513550e691bSryan_chen 
514feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
515feb42054Sryan_chen {
516feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
517feb42054Sryan_chen 	ulong rate = 0;
518feb42054Sryan_chen 
519feb42054Sryan_chen 	switch (clk->id) {
520feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
521bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
522bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
523d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
524bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
525d812df15Sryan_chen 		break;
526feb42054Sryan_chen 	case ASPEED_CLK_AHB:
527feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
528feb42054Sryan_chen 		break;
5296fa1ef3dSryan_chen 	case ASPEED_CLK_APB1:
5306fa1ef3dSryan_chen 		rate = ast2600_get_pclk1(priv->scu);
5316fa1ef3dSryan_chen 		break;
5326fa1ef3dSryan_chen 	case ASPEED_CLK_APB2:
5336fa1ef3dSryan_chen 		rate = ast2600_get_pclk2(priv->scu);
534feb42054Sryan_chen 		break;
535bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
536bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
537bbbfb0c5Sryan_chen 		break;
538feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
539feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
540feb42054Sryan_chen 		break;
541feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
542feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
543feb42054Sryan_chen 		break;
544feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
545feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
546feb42054Sryan_chen 		break;
547feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
548feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
549feb42054Sryan_chen 		break;
550feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
551feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
552feb42054Sryan_chen 		break;
553c304f173Sryan_chen 	case ASPEED_CLK_BCLK:
554c304f173Sryan_chen 		rate = ast2600_get_bclk_rate(priv->scu);
555c304f173Sryan_chen 		break;
556f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
557f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
558f51926eeSryan_chen 		break;
559f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
560f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
561f51926eeSryan_chen 		break;
5622e195992Sryan_chen 	case ASPEED_CLK_UARTX:
5632e195992Sryan_chen 		rate = ast2600_get_uart_uxclk_rate(priv->scu);
5642e195992Sryan_chen 		break;
5650998ddefSryan_chen 	case ASPEED_CLK_HUARTX:
5662e195992Sryan_chen 		rate = ast2600_get_uart_huxclk_rate(priv->scu);
5672e195992Sryan_chen 		break;
568feb42054Sryan_chen 	default:
569d812df15Sryan_chen 		pr_debug("can't get clk rate\n");
570feb42054Sryan_chen 		return -ENOENT;
571feb42054Sryan_chen 	}
572feb42054Sryan_chen 
573feb42054Sryan_chen 	return rate;
574feb42054Sryan_chen }
575feb42054Sryan_chen 
576577fcdaeSDylan Hung /**
577577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
578577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
579577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
580a8fc7648SRyan Chen  * The function caller shall fill "pll->in" and "pll->out",
581a8fc7648SRyan Chen  * then this function will search the lookup table
582a8fc7648SRyan Chen  * to find a valid PLL divider configuration.
583550e691bSryan_chen  */
584577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
585550e691bSryan_chen {
586577fcdaeSDylan Hung 	u32 i;
587577fcdaeSDylan Hung 	bool is_found = false;
588550e691bSryan_chen 
589577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
590577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
591ed3899c5SRyan Chen 
592ed3899c5SRyan Chen 		if (def_cfg->in == pll->in && def_cfg->out == pll->out) {
593577fcdaeSDylan Hung 			is_found = true;
594577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
595577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
596577fcdaeSDylan Hung 			break;
597550e691bSryan_chen 		}
598550e691bSryan_chen 	}
599577fcdaeSDylan Hung 	return is_found;
600550e691bSryan_chen }
601ed3899c5SRyan Chen 
602fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu,
603fd52be0bSDylan Hung 				 struct ast2600_pll_cfg *p_cfg, int pll_idx)
604fd52be0bSDylan Hung {
605fd52be0bSDylan Hung 	u32 addr, addr_ext;
606fd52be0bSDylan Hung 	u32 reg;
607550e691bSryan_chen 
608fd52be0bSDylan Hung 	switch (pll_idx) {
609fd52be0bSDylan Hung 	case ASPEED_CLK_HPLL:
610fd52be0bSDylan Hung 		addr = (u32)(&scu->h_pll_param);
611fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->h_pll_ext_param);
612fd52be0bSDylan Hung 		break;
613fd52be0bSDylan Hung 	case ASPEED_CLK_MPLL:
614fd52be0bSDylan Hung 		addr = (u32)(&scu->m_pll_param);
615fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->m_pll_ext_param);
616fd52be0bSDylan Hung 		break;
617fd52be0bSDylan Hung 	case ASPEED_CLK_DPLL:
618fd52be0bSDylan Hung 		addr = (u32)(&scu->d_pll_param);
619fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->d_pll_ext_param);
620fd52be0bSDylan Hung 		break;
621fd52be0bSDylan Hung 	case ASPEED_CLK_EPLL:
622fd52be0bSDylan Hung 		addr = (u32)(&scu->e_pll_param);
623fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->e_pll_ext_param);
624fd52be0bSDylan Hung 		break;
625fd52be0bSDylan Hung 	default:
626fd52be0bSDylan Hung 		debug("unknown PLL index\n");
627fd52be0bSDylan Hung 		return 1;
628fd52be0bSDylan Hung 	}
629fd52be0bSDylan Hung 
630fd52be0bSDylan Hung 	p_cfg->reg.b.bypass = 0;
631fd52be0bSDylan Hung 	p_cfg->reg.b.off = 1;
632fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 1;
633fd52be0bSDylan Hung 
634fd52be0bSDylan Hung 	reg = readl(addr);
635fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
636fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
637fd52be0bSDylan Hung 	writel(reg, addr);
638fd52be0bSDylan Hung 
639fd52be0bSDylan Hung 	/* write extend parameter */
640fd52be0bSDylan Hung 	writel(p_cfg->ext_reg, addr_ext);
641fd52be0bSDylan Hung 	udelay(100);
642fd52be0bSDylan Hung 	p_cfg->reg.b.off = 0;
643fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 0;
644fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
645fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
646fd52be0bSDylan Hung 	writel(reg, addr);
647ed3899c5SRyan Chen 	while (!(readl(addr_ext) & BIT(31)))
648ed3899c5SRyan Chen 		;
649fd52be0bSDylan Hung 
650fd52be0bSDylan Hung 	return 0;
651fd52be0bSDylan Hung }
652ed3899c5SRyan Chen 
653feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
654550e691bSryan_chen {
655577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
656550e691bSryan_chen 
657577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
658577fcdaeSDylan Hung 	mpll.out = rate;
659f27685ebSRyan Chen 	if (ast2600_search_clock_config(&mpll) == false) {
660577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
661577fcdaeSDylan Hung 		return 0;
662577fcdaeSDylan Hung 	}
663ed3899c5SRyan Chen 	ast2600_configure_pll(scu, &mpll.cfg, ASPEED_CLK_MPLL);
664577fcdaeSDylan Hung 
665cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
666d6e349c7Sryan_chen }
667d6e349c7Sryan_chen 
668d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
669550e691bSryan_chen {
670f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
671550e691bSryan_chen 	ulong new_rate;
672ed3899c5SRyan Chen 
673550e691bSryan_chen 	switch (clk->id) {
674f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
675feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
676550e691bSryan_chen 		break;
677550e691bSryan_chen 	default:
678550e691bSryan_chen 		return -ENOENT;
679550e691bSryan_chen 	}
680550e691bSryan_chen 
681550e691bSryan_chen 	return new_rate;
682550e691bSryan_chen }
683feb42054Sryan_chen 
684f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1	(20)
685f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2	(21)
686f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3	(20)
687f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4	(21)
688f9aa0ee1Sryan_chen 
689*a98c71fbSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu, struct udevice *dev)
690cc476ffcSDylan Hung {
691*a98c71fbSDylan Hung 	union mac_delay_1g reg_1g;
692*a98c71fbSDylan Hung 	union mac_delay_100_10 reg_100, reg_10;
693*a98c71fbSDylan Hung 	struct mac_delay_config mac1_cfg, mac2_cfg;
694*a98c71fbSDylan Hung 	int ret;
6954760b3f8SDylan Hung 
696*a98c71fbSDylan Hung 	reg_1g.w = (readl(&scu->mac12_clk_delay) & ~GENMASK(25, 0)) |
697*a98c71fbSDylan Hung 		   MAC_DEF_DELAY_1G;
698*a98c71fbSDylan Hung 	reg_100.w = MAC_DEF_DELAY_100M;
699*a98c71fbSDylan Hung 	reg_10.w = MAC_DEF_DELAY_10M;
700*a98c71fbSDylan Hung 
701*a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac0-clk-delay", (u32 *)&mac1_cfg, sizeof(mac1_cfg) / sizeof(u32));
702*a98c71fbSDylan Hung 	if (!ret) {
703*a98c71fbSDylan Hung 		reg_1g.b.tx_delay_1 = mac1_cfg.tx_delay_1000;
704*a98c71fbSDylan Hung 		reg_1g.b.rx_delay_1 = mac1_cfg.rx_delay_1000;
705*a98c71fbSDylan Hung 		reg_100.b.tx_delay_1 = mac1_cfg.tx_delay_100;
706*a98c71fbSDylan Hung 		reg_100.b.rx_delay_1 = mac1_cfg.rx_delay_100;
707*a98c71fbSDylan Hung 		reg_10.b.tx_delay_1 = mac1_cfg.tx_delay_10;
708*a98c71fbSDylan Hung 		reg_10.b.rx_delay_1 = mac1_cfg.rx_delay_10;
709*a98c71fbSDylan Hung 	}
710*a98c71fbSDylan Hung 
711*a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac1-clk-delay", (u32 *)&mac2_cfg, sizeof(mac2_cfg) / sizeof(u32));
712*a98c71fbSDylan Hung 	if (!ret) {
713*a98c71fbSDylan Hung 		reg_1g.b.tx_delay_2 = mac2_cfg.tx_delay_1000;
714*a98c71fbSDylan Hung 		reg_1g.b.rx_delay_2 = mac2_cfg.rx_delay_1000;
715*a98c71fbSDylan Hung 		reg_100.b.tx_delay_2 = mac2_cfg.tx_delay_100;
716*a98c71fbSDylan Hung 		reg_100.b.rx_delay_2 = mac2_cfg.rx_delay_100;
717*a98c71fbSDylan Hung 		reg_10.b.tx_delay_2 = mac2_cfg.tx_delay_10;
718*a98c71fbSDylan Hung 		reg_10.b.rx_delay_2 = mac2_cfg.rx_delay_10;
719*a98c71fbSDylan Hung 	}
720*a98c71fbSDylan Hung 
721*a98c71fbSDylan Hung 	writel(reg_1g.w, &scu->mac12_clk_delay);
722*a98c71fbSDylan Hung 	writel(reg_100.w, &scu->mac12_clk_delay_100M);
723*a98c71fbSDylan Hung 	writel(reg_10.w, &scu->mac12_clk_delay_10M);
724cc476ffcSDylan Hung 
725ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
726eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16));
727894c19cfSDylan Hung 
728cc476ffcSDylan Hung 	return 0;
729cc476ffcSDylan Hung }
730cc476ffcSDylan Hung 
731*a98c71fbSDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu, struct udevice *dev)
73254f9cba1SDylan Hung {
733*a98c71fbSDylan Hung 	union mac_delay_1g reg_1g;
734*a98c71fbSDylan Hung 	union mac_delay_100_10 reg_100, reg_10;
735*a98c71fbSDylan Hung 	struct mac_delay_config mac3_cfg, mac4_cfg;
736*a98c71fbSDylan Hung 	int ret;
737*a98c71fbSDylan Hung 
73854f9cba1SDylan Hung 	/*
739eff28274SJohnny Huang 	 * scu350[31]   RGMII 125M source: 0 = from IO pin
740eff28274SJohnny Huang 	 * scu350[25:0] MAC 1G delay
74154f9cba1SDylan Hung 	 */
742*a98c71fbSDylan Hung 	reg_1g.w = (readl(&scu->mac34_clk_delay) & ~GENMASK(25, 0)) |
743*a98c71fbSDylan Hung 		   MAC34_DEF_DELAY_1G;
744*a98c71fbSDylan Hung 	reg_1g.b.rgmii_125m_o_sel = 0;
745*a98c71fbSDylan Hung 	reg_100.w = MAC34_DEF_DELAY_100M;
746*a98c71fbSDylan Hung 	reg_10.w = MAC34_DEF_DELAY_10M;
747*a98c71fbSDylan Hung 
748*a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac2-clk-delay", (u32 *)&mac3_cfg, sizeof(mac3_cfg) / sizeof(u32));
749*a98c71fbSDylan Hung 	if (!ret) {
750*a98c71fbSDylan Hung 		reg_1g.b.tx_delay_1 = mac3_cfg.tx_delay_1000;
751*a98c71fbSDylan Hung 		reg_1g.b.rx_delay_1 = mac3_cfg.rx_delay_1000;
752*a98c71fbSDylan Hung 		reg_100.b.tx_delay_1 = mac3_cfg.tx_delay_100;
753*a98c71fbSDylan Hung 		reg_100.b.rx_delay_1 = mac3_cfg.rx_delay_100;
754*a98c71fbSDylan Hung 		reg_10.b.tx_delay_1 = mac3_cfg.tx_delay_10;
755*a98c71fbSDylan Hung 		reg_10.b.rx_delay_1 = mac3_cfg.rx_delay_10;
756*a98c71fbSDylan Hung 	}
757*a98c71fbSDylan Hung 
758*a98c71fbSDylan Hung 	ret = dev_read_u32_array(dev, "mac3-clk-delay", (u32 *)&mac4_cfg, sizeof(mac4_cfg) / sizeof(u32));
759*a98c71fbSDylan Hung 	if (!ret) {
760*a98c71fbSDylan Hung 		reg_1g.b.tx_delay_2 = mac4_cfg.tx_delay_1000;
761*a98c71fbSDylan Hung 		reg_1g.b.rx_delay_2 = mac4_cfg.rx_delay_1000;
762*a98c71fbSDylan Hung 		reg_100.b.tx_delay_2 = mac4_cfg.tx_delay_100;
763*a98c71fbSDylan Hung 		reg_100.b.rx_delay_2 = mac4_cfg.rx_delay_100;
764*a98c71fbSDylan Hung 		reg_10.b.tx_delay_2 = mac4_cfg.tx_delay_10;
765*a98c71fbSDylan Hung 		reg_10.b.rx_delay_2 = mac4_cfg.rx_delay_10;
766*a98c71fbSDylan Hung 	}
767*a98c71fbSDylan Hung 
768*a98c71fbSDylan Hung 	writel(reg_1g.w, &scu->mac34_clk_delay);
769*a98c71fbSDylan Hung 	writel(reg_100.w, &scu->mac34_clk_delay_100M);
770*a98c71fbSDylan Hung 	writel(reg_10.w, &scu->mac34_clk_delay_10M);
77154f9cba1SDylan Hung 
772eff28274SJohnny Huang 	/*
773eff28274SJohnny Huang 	 * clock source seletion and divider
774eff28274SJohnny Huang 	 * scu310[26:24] : MAC AHB bus clock = HCLK / 2
775eff28274SJohnny Huang 	 * scu310[18:16] : RMII 50M = HCLK_200M / 4
776eff28274SJohnny Huang 	 */
777eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)),
778eff28274SJohnny Huang 			((0x0 << 24) | (0x3 << 16)));
77954f9cba1SDylan Hung 
780eff28274SJohnny Huang 	/*
781eff28274SJohnny Huang 	 * set driving strength
782eff28274SJohnny Huang 	 * scu458[3:2] : MAC4 driving strength
783eff28274SJohnny Huang 	 * scu458[1:0] : MAC3 driving strength
784eff28274SJohnny Huang 	 */
785eff28274SJohnny Huang 	clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0),
786a961159eSDylan Hung 			(0x3 << 2) | (0x3 << 0));
78754f9cba1SDylan Hung 
78854f9cba1SDylan Hung 	return 0;
78954f9cba1SDylan Hung }
790eff28274SJohnny Huang 
79154f9cba1SDylan Hung /**
7925b5c3d44SDylan Hung  * ast2600 RGMII clock source tree
79354f9cba1SDylan Hung  * 125M from external PAD -------->|\
79454f9cba1SDylan Hung  * HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
79554f9cba1SDylan Hung  *         | |---->| divider |---->|/                             +
79654f9cba1SDylan Hung  * EPLL -->|/                                                     |
79754f9cba1SDylan Hung  *                                                                |
798eff28274SJohnny Huang  * +---------<-----------|RGMIICK PAD output enable|<-------------+
79954f9cba1SDylan Hung  * |
800eff28274SJohnny Huang  * +--------------------------->|\
80154f9cba1SDylan Hung  *                              | |----> RGMII 125M for MAC#3 & MAC#4
802eff28274SJohnny Huang  * HCLK 200M ---->|divider|---->|/
803eff28274SJohnny Huang  * To simplify the control flow:
804eff28274SJohnny Huang  * 1. RGMII 1/2 always use EPLL as the internal clock source
805eff28274SJohnny Huang  * 2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source
806eff28274SJohnny Huang  * 125M from external PAD -------->|\
807eff28274SJohnny Huang  *                                 | |---->RGMII 125M for MAC#1 & MAC#2
808eff28274SJohnny Huang  *         EPLL---->| divider |--->|/                             +
809eff28274SJohnny Huang  *                                                                |
810eff28274SJohnny Huang  * +<--------------------|RGMIICK PAD output enable|<-------------+
811eff28274SJohnny Huang  * |
812eff28274SJohnny Huang  * +--------------------------->RGMII 125M for MAC#3 & MAC#4
813eff28274SJohnny Huang  */
814eff28274SJohnny Huang #define RGMIICK_SRC_PAD		0
815eff28274SJohnny Huang #define RGMIICK_SRC_EPLL	1 /* recommended */
816eff28274SJohnny Huang #define RGMIICK_SRC_HPLL	2
817eff28274SJohnny Huang 
818eff28274SJohnny Huang #define RGMIICK_DIV2	1
819eff28274SJohnny Huang #define RGMIICK_DIV3	2
820eff28274SJohnny Huang #define RGMIICK_DIV4	3
821eff28274SJohnny Huang #define RGMIICK_DIV5	4
822eff28274SJohnny Huang #define RGMIICK_DIV6	5
823eff28274SJohnny Huang #define RGMIICK_DIV7	6
824eff28274SJohnny Huang #define RGMIICK_DIV8	7 /* recommended */
825eff28274SJohnny Huang 
826eff28274SJohnny Huang #define RMIICK_DIV4		0
827eff28274SJohnny Huang #define RMIICK_DIV8		1
828eff28274SJohnny Huang #define RMIICK_DIV12	2
829eff28274SJohnny Huang #define RMIICK_DIV16	3
830eff28274SJohnny Huang #define RMIICK_DIV20	4 /* recommended */
831eff28274SJohnny Huang #define RMIICK_DIV24	5
832eff28274SJohnny Huang #define RMIICK_DIV28	6
833eff28274SJohnny Huang #define RMIICK_DIV32	7
834eff28274SJohnny Huang 
835eff28274SJohnny Huang struct ast2600_mac_clk_div {
836eff28274SJohnny Huang 	u32 src; /* 0=external PAD, 1=internal PLL */
837eff28274SJohnny Huang 	u32 fin; /* divider input speed */
838eff28274SJohnny Huang 	u32 n; /* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */
839eff28274SJohnny Huang 	u32 fout; /* fout = fin / n */
840eff28274SJohnny Huang };
841eff28274SJohnny Huang 
842eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = {
843eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
844eff28274SJohnny Huang 	.fin = 1000000000,
845eff28274SJohnny Huang 	.n = RGMIICK_DIV8,
846eff28274SJohnny Huang 	.fout = 125000000,
847eff28274SJohnny Huang };
848eff28274SJohnny Huang 
849eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = {
850eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
851eff28274SJohnny Huang 	.fin = 1000000000,
852eff28274SJohnny Huang 	.n = RMIICK_DIV20,
853eff28274SJohnny Huang 	.fout = 50000000,
854eff28274SJohnny Huang };
855ed3899c5SRyan Chen 
856eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu,
857eff28274SJohnny Huang 				 struct ast2600_mac_clk_div *p_cfg)
858eff28274SJohnny Huang {
859eff28274SJohnny Huang 	struct ast2600_pll_desc pll;
860eff28274SJohnny Huang 
861eff28274SJohnny Huang 	pll.in = AST2600_CLK_IN;
862eff28274SJohnny Huang 	pll.out = p_cfg->fin;
863ed3899c5SRyan Chen 	if (ast2600_search_clock_config(&pll) == false) {
864ed3899c5SRyan Chen 		pr_err("unable to find valid ETHNET MAC clock setting\n");
8653f295164SRyan Chen 		debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w,
8663f295164SRyan Chen 		      pll.cfg.ext_reg);
8673f295164SRyan Chen 		debug("%s: pll cfg = %02x %02x %02x\n", __func__,
8683f295164SRyan Chen 		      pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p);
869eff28274SJohnny Huang 		return;
870eff28274SJohnny Huang 	}
871ed3899c5SRyan Chen 	ast2600_configure_pll(p_scu, &pll.cfg, p_cfg->src);
872eff28274SJohnny Huang }
873eff28274SJohnny Huang 
874eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu,
875eff28274SJohnny Huang 				   struct ast2600_mac_clk_div *p_cfg)
876eff28274SJohnny Huang {
877eff28274SJohnny Huang 	u32 reg_304 = readl(&p_scu->clk_sel2);
878eff28274SJohnny Huang 	u32 reg_340 = readl(&p_scu->mac12_clk_delay);
879eff28274SJohnny Huang 	u32 reg_350 = readl(&p_scu->mac34_clk_delay);
880eff28274SJohnny Huang 
881eff28274SJohnny Huang 	reg_340 &= ~GENMASK(31, 29);
882eff28274SJohnny Huang 	/* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */
883eff28274SJohnny Huang 	reg_340 |= BIT(28);
8843f295164SRyan Chen 	if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) {
885eff28274SJohnny Huang 		/*
886eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
887eff28274SJohnny Huang 		 * the divider setting
888eff28274SJohnny Huang 		 */
889ed3899c5SRyan Chen 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src))
890eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
891eff28274SJohnny Huang 		/* scu340[31]: select RGMII 125M from internal source */
892eff28274SJohnny Huang 		reg_340 |= BIT(31);
893eff28274SJohnny Huang 	}
894eff28274SJohnny Huang 
895eff28274SJohnny Huang 	reg_304 &= ~GENMASK(23, 20);
896eff28274SJohnny Huang 
897eff28274SJohnny Huang 	/* set clock divider */
898eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 20;
899eff28274SJohnny Huang 
900eff28274SJohnny Huang 	/* select internal clock source */
901ed3899c5SRyan Chen 	if (p_cfg->src == ASPEED_CLK_HPLL)
902eff28274SJohnny Huang 		reg_304 |= BIT(23);
903eff28274SJohnny Huang 
904eff28274SJohnny Huang 	/* RGMII 3/4 clock source select */
905eff28274SJohnny Huang 	reg_350 &= ~BIT(31);
906eff28274SJohnny Huang 
907eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
908eff28274SJohnny Huang 	writel(reg_340, &p_scu->mac12_clk_delay);
909eff28274SJohnny Huang 	writel(reg_350, &p_scu->mac34_clk_delay);
910eff28274SJohnny Huang }
911eff28274SJohnny Huang 
912eff28274SJohnny Huang /**
9135b5c3d44SDylan Hung  * ast2600 RMII/NCSI clock source tree
9145b5c3d44SDylan Hung  * HPLL -->|\
9155b5c3d44SDylan Hung  *         | |---->| divider |----> RMII 50M for MAC#1 & MAC#2
9165b5c3d44SDylan Hung  * EPLL -->|/
9175b5c3d44SDylan Hung  * HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4
91854f9cba1SDylan Hung  */
919eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu,
920eff28274SJohnny Huang 				  struct ast2600_mac_clk_div *p_cfg)
92154f9cba1SDylan Hung {
922eff28274SJohnny Huang 	u32 reg_304;
923eff28274SJohnny Huang 	u32 reg_310;
924eff28274SJohnny Huang 
9253f295164SRyan Chen 	if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) {
926eff28274SJohnny Huang 		/*
927eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
928eff28274SJohnny Huang 		 * the divider setting
929eff28274SJohnny Huang 		 */
930ed3899c5SRyan Chen 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src))
931eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
932eff28274SJohnny Huang 	}
93354f9cba1SDylan Hung 
934eff28274SJohnny Huang 	reg_304 = readl(&p_scu->clk_sel2);
935eff28274SJohnny Huang 	reg_310 = readl(&p_scu->clk_sel4);
936eff28274SJohnny Huang 
937eff28274SJohnny Huang 	reg_304 &= ~GENMASK(19, 16);
938eff28274SJohnny Huang 
939eff28274SJohnny Huang 	/* set RMII 1/2 clock divider */
940eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 16;
941eff28274SJohnny Huang 
942eff28274SJohnny Huang 	/* RMII clock source selection */
943ed3899c5SRyan Chen 	if (p_cfg->src == ASPEED_CLK_HPLL)
944eff28274SJohnny Huang 		reg_304 |= BIT(19);
945eff28274SJohnny Huang 
946eff28274SJohnny Huang 	/* set RMII 3/4 clock divider */
947eff28274SJohnny Huang 	reg_310 &= ~GENMASK(18, 16);
948eff28274SJohnny Huang 	reg_310 |= (0x3 << 16);
949eff28274SJohnny Huang 
950eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
951eff28274SJohnny Huang 	writel(reg_310, &p_scu->clk_sel4);
952eff28274SJohnny Huang }
953eff28274SJohnny Huang 
954f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
955f9aa0ee1Sryan_chen {
956f9aa0ee1Sryan_chen 	u32 reset_bit;
957f9aa0ee1Sryan_chen 	u32 clkstop_bit;
958f9aa0ee1Sryan_chen 
959f9aa0ee1Sryan_chen 	switch (index) {
960f9aa0ee1Sryan_chen 	case 1:
961f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
962f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
963f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
964f9aa0ee1Sryan_chen 		udelay(100);
965f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
966f9aa0ee1Sryan_chen 		mdelay(10);
967f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
968f9aa0ee1Sryan_chen 		break;
969f9aa0ee1Sryan_chen 	case 2:
970f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
971f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
972f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
973f9aa0ee1Sryan_chen 		udelay(100);
974f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
975f9aa0ee1Sryan_chen 		mdelay(10);
976f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
977f9aa0ee1Sryan_chen 		break;
978f9aa0ee1Sryan_chen 	case 3:
979f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
980f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
981f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
982f9aa0ee1Sryan_chen 		udelay(100);
983f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
984f9aa0ee1Sryan_chen 		mdelay(10);
985f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
986f9aa0ee1Sryan_chen 		break;
987f9aa0ee1Sryan_chen 	case 4:
988f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
989f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
990f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
991f9aa0ee1Sryan_chen 		udelay(100);
992f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
993f9aa0ee1Sryan_chen 		mdelay(10);
994f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
995f9aa0ee1Sryan_chen 		break;
996f9aa0ee1Sryan_chen 	default:
997f9aa0ee1Sryan_chen 		return -EINVAL;
998f9aa0ee1Sryan_chen 	}
999f9aa0ee1Sryan_chen 
1000f9aa0ee1Sryan_chen 	return 0;
1001f9aa0ee1Sryan_chen }
1002550e691bSryan_chen 
1003a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_FROM_HPLL_CLK	BIT(19)
1004a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_CLK_MASK		GENMASK(27, 26)
1005ed3899c5SRyan Chen #define SCU_CLK_ECC_RSA_CLK_DIV(x)		((x) << 26)
1006a8fc7648SRyan Chen static void ast2600_configure_rsa_ecc_clk(struct ast2600_scu *scu)
1007a8fc7648SRyan Chen {
1008a8fc7648SRyan Chen 	u32 clk_sel = readl(&scu->clk_sel1);
1009a8fc7648SRyan Chen 
1010a8fc7648SRyan Chen 	/* Configure RSA clock = HPLL/3 */
1011a8fc7648SRyan Chen 	clk_sel |= SCU_CLK_ECC_RSA_FROM_HPLL_CLK;
1012a8fc7648SRyan Chen 	clk_sel &= ~SCU_CLK_ECC_RSA_CLK_MASK;
1013a8fc7648SRyan Chen 	clk_sel |= SCU_CLK_ECC_RSA_CLK_DIV(2);
1014a8fc7648SRyan Chen 
1015a8fc7648SRyan Chen 	writel(clk_sel, &scu->clk_sel1);
1016a8fc7648SRyan Chen }
1017a8fc7648SRyan Chen 
1018f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
1019f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
1020f51926eeSryan_chen {
1021f51926eeSryan_chen 	u32 reset_bit;
1022f51926eeSryan_chen 	u32 clkstop_bit;
1023f51926eeSryan_chen 
1024f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
1025f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
1026f51926eeSryan_chen 
1027fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
1028fc9f12e6Sryan_chen 
1029f51926eeSryan_chen 	udelay(100);
1030f51926eeSryan_chen 	//enable clk
1031f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1032f51926eeSryan_chen 	mdelay(10);
1033fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1034f51926eeSryan_chen 
1035f51926eeSryan_chen 	return 0;
1036f51926eeSryan_chen }
1037f51926eeSryan_chen 
1038f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD			31
1039f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
1040ed3899c5SRyan Chen #define SCU_CLK_SD_DIV(x)			((x) << 28)
10412cd7cba2Sryan_chen #define SCU_CLK_SD_FROM_APLL_CLK	BIT(8)
1042f51926eeSryan_chen 
1043f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
1044f51926eeSryan_chen {
1045f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
1046f51926eeSryan_chen 	u32 enableclk_bit;
10472cd7cba2Sryan_chen 	u32 rate = 0;
10482cd7cba2Sryan_chen 	u32 div = 0;
10492cd7cba2Sryan_chen 	int i = 0;
1050f51926eeSryan_chen 
1051f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
1052f51926eeSryan_chen 
1053a8fc7648SRyan Chen 	/* ast2600 sd controller max clk is 200Mhz :
1054a8fc7648SRyan Chen 	 * use apll for clock source 800/4 = 200 : controller max is 200mhz
1055a8fc7648SRyan Chen 	 */
10562cd7cba2Sryan_chen 	rate = ast2600_get_apll_rate(scu);
10572cd7cba2Sryan_chen 	for (i = 0; i < 8; i++) {
10582cd7cba2Sryan_chen 		div = (i + 1) * 2;
10592cd7cba2Sryan_chen 		if ((rate / div) <= 200000000)
10602cd7cba2Sryan_chen 			break;
10612cd7cba2Sryan_chen 	}
1062f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
10632cd7cba2Sryan_chen 	clk_sel |= SCU_CLK_SD_DIV(i) | SCU_CLK_SD_FROM_APLL_CLK;
1064f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
1065f51926eeSryan_chen 
1066f51926eeSryan_chen 	//enable clk
1067f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
1068f51926eeSryan_chen 
1069f51926eeSryan_chen 	return 0;
1070f51926eeSryan_chen }
1071f51926eeSryan_chen 
1072f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
1073f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
1074f51926eeSryan_chen {
1075f51926eeSryan_chen 	u32 reset_bit;
1076f51926eeSryan_chen 	u32 clkstop_bit;
1077f51926eeSryan_chen 
1078f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
1079f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
1080f51926eeSryan_chen 
1081fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1082f51926eeSryan_chen 	udelay(100);
1083f51926eeSryan_chen 	//enable clk
1084f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1085f51926eeSryan_chen 	mdelay(10);
1086fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1087f51926eeSryan_chen 
1088f51926eeSryan_chen 	return 0;
1089f51926eeSryan_chen }
1090f51926eeSryan_chen 
1091f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC			15
1092f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
1093ed3899c5SRyan Chen #define SCU_CLK_EMMC_DIV(x)			((x) << 12)
1094a8fc7648SRyan Chen #define SCU_CLK_EMMC_FROM_MPLL_CLK	BIT(11)
1095f51926eeSryan_chen 
1096f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
1097f51926eeSryan_chen {
109885d48d8cSryan_chen 	u32 revision_id = readl(&scu->chip_id1);
1099f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
1100ed3899c5SRyan Chen 	u32 enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC);
1101f4c4ddb1Sryan_chen 	u32 rate = 0;
1102f4c4ddb1Sryan_chen 	u32 div = 0;
1103f4c4ddb1Sryan_chen 	int i = 0;
1104f51926eeSryan_chen 
1105ed3899c5SRyan Chen 	/*
1106ed3899c5SRyan Chen 	 * ast2600 eMMC controller max clk is 200Mhz
1107ed3899c5SRyan Chen 	 * HPll->1/2->|\
1108ed3899c5SRyan Chen 	 *				|->SCU300[11]->SCU300[14:12][1/N] +
1109ed3899c5SRyan Chen 	 * MPLL------>|/								  |
1110ed3899c5SRyan Chen 	 * +----------------------------------------------+
1111ed3899c5SRyan Chen 	 * |
1112ed3899c5SRyan Chen 	 * +---------> EMMC12C[15:8][1/N]-> eMMC clk
1113a8fc7648SRyan Chen 	 */
111485d48d8cSryan_chen 	if (((revision_id & CHIP_REVISION_ID) >> 16)) {
11158c32294fSryan_chen 		//AST2600A1 : use mpll to be clk source
1116b0c30ea3Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
1117b0c30ea3Sryan_chen 		for (i = 0; i < 8; i++) {
1118b0c30ea3Sryan_chen 			div = (i + 1) * 2;
1119b0c30ea3Sryan_chen 			if ((rate / div) <= 200000000)
1120b0c30ea3Sryan_chen 				break;
1121b0c30ea3Sryan_chen 		}
1122b0c30ea3Sryan_chen 
1123b0c30ea3Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
11242cd7cba2Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i) | SCU_CLK_EMMC_FROM_MPLL_CLK;
1125b0c30ea3Sryan_chen 		writel(clk_sel, &scu->clk_sel1);
1126b0c30ea3Sryan_chen 
1127b0c30ea3Sryan_chen 	} else {
11282cd7cba2Sryan_chen 		//AST2600A0 : use hpll to be clk source
1129f4c4ddb1Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1130f4c4ddb1Sryan_chen 
1131f4c4ddb1Sryan_chen 		for (i = 0; i < 8; i++) {
1132f4c4ddb1Sryan_chen 			div = (i + 1) * 4;
1133f4c4ddb1Sryan_chen 			if ((rate / div) <= 200000000)
1134f4c4ddb1Sryan_chen 				break;
1135f4c4ddb1Sryan_chen 		}
1136f4c4ddb1Sryan_chen 
1137f4c4ddb1Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
1138f4c4ddb1Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i);
1139f51926eeSryan_chen 		writel(clk_sel, &scu->clk_sel1);
1140b0c30ea3Sryan_chen 	}
1141f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
1142f51926eeSryan_chen 
1143f51926eeSryan_chen 	return 0;
1144f51926eeSryan_chen }
1145f51926eeSryan_chen 
1146baf00c26Sryan_chen #define SCU_CLKSTOP_FSICLK 30
1147baf00c26Sryan_chen 
1148baf00c26Sryan_chen static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu)
1149baf00c26Sryan_chen {
1150baf00c26Sryan_chen 	u32 reset_bit;
1151baf00c26Sryan_chen 	u32 clkstop_bit;
1152baf00c26Sryan_chen 
1153baf00c26Sryan_chen 	reset_bit = BIT(ASPEED_RESET_FSI % 32);
1154baf00c26Sryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_FSICLK);
1155baf00c26Sryan_chen 
1156baf00c26Sryan_chen 	/* The FSI clock is shared between masters. If it's already on
1157ed3899c5SRyan Chen 	 * don't touch it, as that will reset the existing master.
1158ed3899c5SRyan Chen 	 */
1159baf00c26Sryan_chen 	if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) {
1160baf00c26Sryan_chen 		debug("%s: already running, not touching it\n", __func__);
1161baf00c26Sryan_chen 		return 0;
1162baf00c26Sryan_chen 	}
1163baf00c26Sryan_chen 
1164baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
1165baf00c26Sryan_chen 	udelay(100);
1166baf00c26Sryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1167baf00c26Sryan_chen 	mdelay(10);
1168baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1169baf00c26Sryan_chen 
1170baf00c26Sryan_chen 	return 0;
1171baf00c26Sryan_chen }
1172baf00c26Sryan_chen 
1173b8ec5ceaSryan_chen static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu)
1174b8ec5ceaSryan_chen {
1175b8ec5ceaSryan_chen 	u32 reset_bit;
1176b8ec5ceaSryan_chen 	u32 clkstop_bit;
1177b8ec5ceaSryan_chen 
1178b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P1);
1179b8ec5ceaSryan_chen 	clkstop_bit = BIT(14);
1180b8ec5ceaSryan_chen 
1181b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1182b8ec5ceaSryan_chen 	udelay(100);
1183b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_ctrl1);
1184b8ec5ceaSryan_chen 	mdelay(20);
1185b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1186b8ec5ceaSryan_chen 
1187b8ec5ceaSryan_chen 	return 0;
1188b8ec5ceaSryan_chen }
1189b8ec5ceaSryan_chen 
1190b8ec5ceaSryan_chen static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu)
1191b8ec5ceaSryan_chen {
1192b8ec5ceaSryan_chen 	u32 reset_bit;
1193b8ec5ceaSryan_chen 	u32 clkstop_bit;
1194b8ec5ceaSryan_chen 
1195b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P2);
1196b8ec5ceaSryan_chen 	clkstop_bit = BIT(7);
1197b8ec5ceaSryan_chen 
1198b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1199b8ec5ceaSryan_chen 	udelay(100);
1200b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1201b8ec5ceaSryan_chen 	mdelay(20);
1202b8ec5ceaSryan_chen 
1203b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1204b8ec5ceaSryan_chen 
1205b8ec5ceaSryan_chen 	return 0;
1206b8ec5ceaSryan_chen }
1207b8ec5ceaSryan_chen 
1208d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
1209550e691bSryan_chen {
1210f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
1211550e691bSryan_chen 
1212550e691bSryan_chen 	switch (clk->id) {
121386f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC1CLK:
121486f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 1);
1215550e691bSryan_chen 		break;
121686f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC2CLK:
121786f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 2);
1218550e691bSryan_chen 		break;
121977843939Sryan_chen 	case ASPEED_CLK_GATE_MAC3CLK:
122077843939Sryan_chen 		ast2600_configure_mac(priv->scu, 3);
122177843939Sryan_chen 		break;
122277843939Sryan_chen 	case ASPEED_CLK_GATE_MAC4CLK:
122377843939Sryan_chen 		ast2600_configure_mac(priv->scu, 4);
122477843939Sryan_chen 		break;
1225f51926eeSryan_chen 	case ASPEED_CLK_GATE_SDCLK:
1226f51926eeSryan_chen 		ast2600_enable_sdclk(priv->scu);
1227f51926eeSryan_chen 		break;
1228f51926eeSryan_chen 	case ASPEED_CLK_GATE_SDEXTCLK:
1229f51926eeSryan_chen 		ast2600_enable_extsdclk(priv->scu);
1230f51926eeSryan_chen 		break;
1231f51926eeSryan_chen 	case ASPEED_CLK_GATE_EMMCCLK:
1232f51926eeSryan_chen 		ast2600_enable_emmcclk(priv->scu);
1233f51926eeSryan_chen 		break;
1234f51926eeSryan_chen 	case ASPEED_CLK_GATE_EMMCEXTCLK:
1235f51926eeSryan_chen 		ast2600_enable_extemmcclk(priv->scu);
1236f51926eeSryan_chen 		break;
1237baf00c26Sryan_chen 	case ASPEED_CLK_GATE_FSICLK:
1238baf00c26Sryan_chen 		ast2600_enable_fsiclk(priv->scu);
1239baf00c26Sryan_chen 		break;
1240b8ec5ceaSryan_chen 	case ASPEED_CLK_GATE_USBPORT1CLK:
1241b8ec5ceaSryan_chen 		ast2600_enable_usbahclk(priv->scu);
1242b8ec5ceaSryan_chen 		break;
1243b8ec5ceaSryan_chen 	case ASPEED_CLK_GATE_USBPORT2CLK:
1244b8ec5ceaSryan_chen 		ast2600_enable_usbbhclk(priv->scu);
1245b8ec5ceaSryan_chen 		break;
1246550e691bSryan_chen 	default:
1247ed3899c5SRyan Chen 		pr_err("can't enable clk\n");
1248550e691bSryan_chen 		return -ENOENT;
1249550e691bSryan_chen 	}
1250550e691bSryan_chen 
1251550e691bSryan_chen 	return 0;
1252550e691bSryan_chen }
1253550e691bSryan_chen 
1254f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
1255d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
1256d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
1257d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
1258550e691bSryan_chen };
1259550e691bSryan_chen 
1260d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
1261550e691bSryan_chen {
1262f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
126361ab9607Sryan_chen 	u32 uart_clk_source;
1264550e691bSryan_chen 
1265f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
1266f0d895afSryan_chen 	if (IS_ERR(priv->scu))
1267f0d895afSryan_chen 		return PTR_ERR(priv->scu);
1268550e691bSryan_chen 
12695d05f4fcSRyan Chen 	uart_clk_source = dev_read_u32_default(dev, "uart-clk-source", 0x0);
127061ab9607Sryan_chen 
127161ab9607Sryan_chen 	if (uart_clk_source) {
127256dd3e85Sryan_chen 		if (uart_clk_source & GENMASK(5, 0))
12735d05f4fcSRyan Chen 			setbits_le32(&priv->scu->clk_sel4,
12745d05f4fcSRyan Chen 				     uart_clk_source & GENMASK(5, 0));
127556dd3e85Sryan_chen 		if (uart_clk_source & GENMASK(12, 6))
12765d05f4fcSRyan Chen 			setbits_le32(&priv->scu->clk_sel5,
12775d05f4fcSRyan Chen 				     uart_clk_source & GENMASK(12, 6));
127861ab9607Sryan_chen 	}
127961ab9607Sryan_chen 
1280b89500a2SDylan Hung 	ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig);
1281b89500a2SDylan Hung 	ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig);
1282*a98c71fbSDylan Hung 	ast2600_configure_mac12_clk(priv->scu, dev);
1283*a98c71fbSDylan Hung 	ast2600_configure_mac34_clk(priv->scu, dev);
1284a8fc7648SRyan Chen 	ast2600_configure_rsa_ecc_clk(priv->scu);
1285fd0306aaSJohnny Huang 
1286550e691bSryan_chen 	return 0;
1287550e691bSryan_chen }
1288550e691bSryan_chen 
1289d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
1290550e691bSryan_chen {
1291550e691bSryan_chen 	int ret;
1292550e691bSryan_chen 
1293550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
1294550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
1295550e691bSryan_chen 	if (ret)
1296550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
1297550e691bSryan_chen 
1298550e691bSryan_chen 	return 0;
1299550e691bSryan_chen }
1300550e691bSryan_chen 
1301d35ac78cSryan_chen struct aspeed_clks {
1302d35ac78cSryan_chen 	ulong id;
1303d35ac78cSryan_chen 	const char *name;
1304d35ac78cSryan_chen };
1305d35ac78cSryan_chen 
1306d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
13075d05f4fcSRyan Chen 	{ ASPEED_CLK_HPLL, "hpll" },     { ASPEED_CLK_MPLL, "mpll" },
13085d05f4fcSRyan Chen 	{ ASPEED_CLK_APLL, "apll" },     { ASPEED_CLK_EPLL, "epll" },
13095d05f4fcSRyan Chen 	{ ASPEED_CLK_DPLL, "dpll" },     { ASPEED_CLK_AHB, "hclk" },
13105d05f4fcSRyan Chen 	{ ASPEED_CLK_APB1, "pclk1" },    { ASPEED_CLK_APB2, "pclk2" },
13115d05f4fcSRyan Chen 	{ ASPEED_CLK_BCLK, "bclk" },     { ASPEED_CLK_UARTX, "uxclk" },
1312def99fcbSryan_chen 	{ ASPEED_CLK_HUARTX, "huxclk" },
1313d35ac78cSryan_chen };
1314d35ac78cSryan_chen 
1315d35ac78cSryan_chen int soc_clk_dump(void)
1316d35ac78cSryan_chen {
1317d35ac78cSryan_chen 	struct udevice *dev;
1318d35ac78cSryan_chen 	struct clk clk;
1319d35ac78cSryan_chen 	unsigned long rate;
1320d35ac78cSryan_chen 	int i, ret;
1321d35ac78cSryan_chen 
13225d05f4fcSRyan Chen 	ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu),
13235d05f4fcSRyan Chen 					  &dev);
1324d35ac78cSryan_chen 	if (ret)
1325d35ac78cSryan_chen 		return ret;
1326d35ac78cSryan_chen 
1327d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
1328d35ac78cSryan_chen 
1329d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
1330d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
1331d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
1332d35ac78cSryan_chen 		if (ret < 0) {
1333d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
1334d35ac78cSryan_chen 			continue;
1335d35ac78cSryan_chen 		}
1336d35ac78cSryan_chen 
1337d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
1338d35ac78cSryan_chen 		rate = ret;
1339d35ac78cSryan_chen 
1340d35ac78cSryan_chen 		clk_free(&clk);
1341d35ac78cSryan_chen 
1342d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
1343d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
1344d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
1345d35ac78cSryan_chen 			continue;
1346d35ac78cSryan_chen 		}
1347d35ac78cSryan_chen 		if (ret < 0) {
13485d05f4fcSRyan Chen 			printf("%s %lu: get_rate err: %d\n", __func__,
13495d05f4fcSRyan Chen 			       aspeed_clk_names[i].id, ret);
1350d35ac78cSryan_chen 			continue;
1351d35ac78cSryan_chen 		}
1352d35ac78cSryan_chen 
13535d05f4fcSRyan Chen 		printf("%s(%3lu):\t%lu\n", aspeed_clk_names[i].name,
13545d05f4fcSRyan Chen 		       aspeed_clk_names[i].id, rate);
1355d35ac78cSryan_chen 	}
1356d35ac78cSryan_chen 
1357d35ac78cSryan_chen 	return 0;
1358d35ac78cSryan_chen }
1359d35ac78cSryan_chen 
1360d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
13615d05f4fcSRyan Chen 	{
13625d05f4fcSRyan Chen 		.compatible = "aspeed,ast2600-scu",
13635d05f4fcSRyan Chen 	},
1364550e691bSryan_chen 	{}
1365550e691bSryan_chen };
1366550e691bSryan_chen 
1367aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
1368aa36597fSDylan Hung 	.name = "aspeed_scu",
1369550e691bSryan_chen 	.id = UCLASS_CLK,
1370d6e349c7Sryan_chen 	.of_match = ast2600_clk_ids,
1371f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
1372f9aa0ee1Sryan_chen 	.ops = &ast2600_clk_ops,
1373d6e349c7Sryan_chen 	.bind = ast2600_clk_bind,
1374d6e349c7Sryan_chen 	.probe = ast2600_clk_probe,
1375550e691bSryan_chen };
1376