xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision 85d48d8c79819b807fc78bfb59e17089b6a726a0)
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 /*
16550e691bSryan_chen  * MAC Clock Delay settings, taken from Aspeed SDK
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 
35550e691bSryan_chen #define D2PLL_DEFAULT_RATE	(250 * 1000 * 1000)
36550e691bSryan_chen 
37*85d48d8cSryan_chen 
38*85d48d8cSryan_chen #define CHIP_REVISION_ID GENMASK(23, 16)
39*85d48d8cSryan_chen 
40550e691bSryan_chen DECLARE_GLOBAL_DATA_PTR;
41550e691bSryan_chen 
42550e691bSryan_chen /*
43550e691bSryan_chen  * Clock divider/multiplier configuration struct.
44550e691bSryan_chen  * For H-PLL and M-PLL the formula is
45550e691bSryan_chen  * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
46550e691bSryan_chen  * M - Numerator
47550e691bSryan_chen  * N - Denumerator
48550e691bSryan_chen  * P - Post Divider
49550e691bSryan_chen  * They have the same layout in their control register.
50550e691bSryan_chen  *
51550e691bSryan_chen  * D-PLL and D2-PLL have extra divider (OD + 1), which is not
52550e691bSryan_chen  * yet needed and ignored by clock configurations.
53550e691bSryan_chen  */
54577fcdaeSDylan Hung union ast2600_pll_reg {
55577fcdaeSDylan Hung 	unsigned int w;
56577fcdaeSDylan Hung 	struct {
57fd52be0bSDylan Hung 		unsigned int m : 13;		/* bit[12:0]	*/
58fd52be0bSDylan Hung 		unsigned int n : 6;		/* bit[18:13]	*/
59fd52be0bSDylan Hung 		unsigned int p : 4;		/* bit[22:19]	*/
60fd52be0bSDylan Hung 		unsigned int off : 1;		/* bit[23]	*/
61fd52be0bSDylan Hung 		unsigned int bypass : 1;	/* bit[24]	*/
62fd52be0bSDylan Hung 		unsigned int reset : 1;		/* bit[25]	*/
63fd52be0bSDylan Hung 		unsigned int reserved : 6;	/* bit[31:26]	*/
64577fcdaeSDylan Hung 	} b;
65577fcdaeSDylan Hung };
66577fcdaeSDylan Hung 
67577fcdaeSDylan Hung struct ast2600_pll_cfg {
68577fcdaeSDylan Hung 	union ast2600_pll_reg reg;
69577fcdaeSDylan Hung 	unsigned int ext_reg;
70577fcdaeSDylan Hung };
71577fcdaeSDylan Hung 
72577fcdaeSDylan Hung struct ast2600_pll_desc {
73577fcdaeSDylan Hung 	u32 in;
74577fcdaeSDylan Hung 	u32 out;
75577fcdaeSDylan Hung 	struct ast2600_pll_cfg cfg;
76577fcdaeSDylan Hung };
77577fcdaeSDylan Hung 
78577fcdaeSDylan Hung static const struct ast2600_pll_desc ast2600_pll_lookup[] = {
79577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 400000000,
80577fcdaeSDylan Hung     .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 1,
81577fcdaeSDylan Hung     .cfg.ext_reg = 0x31,
82577fcdaeSDylan Hung     },
83577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 200000000,
84577fcdaeSDylan Hung     .cfg.reg.b.m = 127, .cfg.reg.b.n = 0, .cfg.reg.b.p = 15,
85577fcdaeSDylan Hung     .cfg.ext_reg = 0x3f
86577fcdaeSDylan Hung     },
87577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 334000000,
88577fcdaeSDylan Hung     .cfg.reg.b.m = 667, .cfg.reg.b.n = 4, .cfg.reg.b.p = 9,
89577fcdaeSDylan Hung     .cfg.ext_reg = 0x14d
90577fcdaeSDylan Hung     },
91577fcdaeSDylan Hung 
92577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 1000000000,
93577fcdaeSDylan Hung     .cfg.reg.b.m = 119, .cfg.reg.b.n = 2, .cfg.reg.b.p = 0,
94577fcdaeSDylan Hung     .cfg.ext_reg = 0x3d
95577fcdaeSDylan Hung     },
96577fcdaeSDylan Hung 
97577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 50000000,
98577fcdaeSDylan Hung     .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 15,
99577fcdaeSDylan Hung     .cfg.ext_reg = 0x31
100577fcdaeSDylan Hung     },
101550e691bSryan_chen };
102550e691bSryan_chen 
103bbbfb0c5Sryan_chen extern u32 ast2600_get_pll_rate(struct ast2600_scu *scu, int pll_idx)
104550e691bSryan_chen {
105d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
106bbbfb0c5Sryan_chen 	u32 pll_reg = 0;
1079639db61Sryan_chen 	unsigned int mult, div = 1;
108550e691bSryan_chen 
109bbbfb0c5Sryan_chen 	switch(pll_idx) {
110bbbfb0c5Sryan_chen 		case ASPEED_CLK_HPLL:
111bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->h_pll_param);
112bbbfb0c5Sryan_chen 			break;
113bbbfb0c5Sryan_chen 		case ASPEED_CLK_MPLL:
114bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->m_pll_param);
115bbbfb0c5Sryan_chen 			break;
116bbbfb0c5Sryan_chen 		case ASPEED_CLK_DPLL:
117bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->d_pll_param);
118bbbfb0c5Sryan_chen 			break;
119bbbfb0c5Sryan_chen 		case ASPEED_CLK_EPLL:
120bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->e_pll_param);
121bbbfb0c5Sryan_chen 			break;
122bbbfb0c5Sryan_chen 	}
123bbbfb0c5Sryan_chen 	if (pll_reg & BIT(24)) {
1249639db61Sryan_chen 		/* Pass through mode */
1259639db61Sryan_chen 		mult = div = 1;
1269639db61Sryan_chen 	} else {
1279639db61Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */
12875ced45aSDylan Hung 		union ast2600_pll_reg reg;
12975ced45aSDylan Hung 		reg.w = pll_reg;
130e5c4f4dfSryan_chen 		if(pll_idx == ASPEED_CLK_HPLL) {
131e5c4f4dfSryan_chen 			/*
132e5c4f4dfSryan_chen 			HPLL Numerator (M) = fix 0x5F when SCU500[10]=1
133e5c4f4dfSryan_chen 								 fix 0xBF when SCU500[10]=0 and SCU500[8]=1
134e5c4f4dfSryan_chen 			SCU200[12:0] (default 0x8F) when SCU510[10]=0 and SCU510[8]=0
135e5c4f4dfSryan_chen 			HPLL Denumerator (N) =	SCU200[18:13] (default 0x2)
136e5c4f4dfSryan_chen 			HPLL Divider (P)	 =  SCU200[22:19] (default 0x0)
137e5c4f4dfSryan_chen 			HPLL Bandwidth Adj (NB) =  fix 0x2F when SCU500[10]=1
138e5c4f4dfSryan_chen 									   fix 0x5F	when SCU500[10]=0 and SCU500[8]=1
139e5c4f4dfSryan_chen 			SCU204[11:0] (default 0x31) when SCU500[10]=0 and SCU500[8]=0
140e5c4f4dfSryan_chen 			*/
141e5c4f4dfSryan_chen 			u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
142e5c4f4dfSryan_chen 			if(hwstrap1 & BIT(10))
143e5c4f4dfSryan_chen 				reg.b.m = 0x5F;
144e5c4f4dfSryan_chen 			else {
145e5c4f4dfSryan_chen 				if(hwstrap1 & BIT(8))
146e5c4f4dfSryan_chen 					reg.b.m = 0xBF;
147e5c4f4dfSryan_chen 				//otherwise keep default 0x8F
148e5c4f4dfSryan_chen 			}
149e5c4f4dfSryan_chen 		}
15075ced45aSDylan Hung 		mult = (reg.b.m + 1) / (reg.b.n + 1);
15175ced45aSDylan Hung 		div = (reg.b.p + 1);
1529639db61Sryan_chen 	}
1539639db61Sryan_chen 	return ((clkin * mult)/div);
154550e691bSryan_chen }
155550e691bSryan_chen 
1564f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
157550e691bSryan_chen {
158*85d48d8cSryan_chen 	u32 hw_rev = readl(&scu->chip_id1);
159bbbfb0c5Sryan_chen 	u32 clkin = AST2600_CLK_IN;
16039283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
16139283ea7Sryan_chen 	unsigned int mult, div = 1;
162d6e349c7Sryan_chen 
163*85d48d8cSryan_chen 	if (((hw_rev & CHIP_REVISION_ID) >> 16) == 3) {
164*85d48d8cSryan_chen 		//ast2600a2
165*85d48d8cSryan_chen 		if (apll_reg & BIT(24)) {
166*85d48d8cSryan_chen 			/* Pass through mode */
167*85d48d8cSryan_chen 			mult = div = 1;
168*85d48d8cSryan_chen 		} else {
169*85d48d8cSryan_chen 			/* F = 25Mhz * [(m + 1) / (n + 1)] / (p + 1) */
170*85d48d8cSryan_chen 			u32 m = apll_reg & 0x1fff;
171*85d48d8cSryan_chen 			u32 n = (apll_reg >> 13) & 0x3f;
172*85d48d8cSryan_chen 			u32 p = (apll_reg >> 19) & 0xf;
173*85d48d8cSryan_chen 
174*85d48d8cSryan_chen 			mult = (m + 1);
175*85d48d8cSryan_chen 			div = (n + 1) * (p + 1);
176*85d48d8cSryan_chen 		}
177*85d48d8cSryan_chen 
178*85d48d8cSryan_chen 	} else {
17939283ea7Sryan_chen 		if (apll_reg & BIT(20)) {
180d6e349c7Sryan_chen 			/* Pass through mode */
181d6e349c7Sryan_chen 			mult = div = 1;
182d6e349c7Sryan_chen 		} else {
183bbbfb0c5Sryan_chen 			/* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
18439283ea7Sryan_chen 			u32 m = (apll_reg >> 5) & 0x3f;
18539283ea7Sryan_chen 			u32 od = (apll_reg >> 4) & 0x1;
18639283ea7Sryan_chen 			u32 n = apll_reg & 0xf;
187d6e349c7Sryan_chen 
188bbbfb0c5Sryan_chen 			mult = (2 - od) * (m + 2);
189bbbfb0c5Sryan_chen 			div = n + 1;
190d6e349c7Sryan_chen 		}
191*85d48d8cSryan_chen 	}
192bbbfb0c5Sryan_chen 	return ((clkin * mult)/div);
19339283ea7Sryan_chen }
19439283ea7Sryan_chen 
195d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
19645e0908aSryan_chen 	2, 2, 3, 4,
197d812df15Sryan_chen };
198d812df15Sryan_chen 
19945e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div0_table[] = {
200e29dc694Sryan_chen 	3, 2, 3, 4,
20145e0908aSryan_chen };
20245e0908aSryan_chen 
20345e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div1_table[] = {
204e29dc694Sryan_chen 	3, 4, 6, 8,
205e29dc694Sryan_chen };
206e29dc694Sryan_chen 
207e29dc694Sryan_chen static u32 ast2600_a1_axi_ahb_default_table[] = {
208e29dc694Sryan_chen 	3, 4, 3, 4, 2, 2, 2, 2,
209d812df15Sryan_chen };
210d812df15Sryan_chen 
211d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
212d812df15Sryan_chen {
213*85d48d8cSryan_chen 	u32 hw_rev = readl(&scu->chip_id1);
21445e0908aSryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
215d812df15Sryan_chen 	u32 axi_div = 1;
216d812df15Sryan_chen 	u32 ahb_div = 0;
217d812df15Sryan_chen 	u32 rate = 0;
218d812df15Sryan_chen 
219*85d48d8cSryan_chen 	if ((hw_rev & CHIP_REVISION_ID) >> 16) {
220e5c4f4dfSryan_chen 		//ast2600a1
22145e0908aSryan_chen 		if(hwstrap1 & BIT(16)) {
222e29dc694Sryan_chen 			ast2600_a1_axi_ahb_div1_table[0] = ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 0x3];
223d812df15Sryan_chen 			axi_div = 1;
22445e0908aSryan_chen 			ahb_div = ast2600_a1_axi_ahb_div1_table[(hwstrap1 >> 11) & 0x3];
22545e0908aSryan_chen 		} else {
226e29dc694Sryan_chen 			ast2600_a1_axi_ahb_div0_table[0] = ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 0x3];
227d812df15Sryan_chen 			axi_div = 2;
22845e0908aSryan_chen 			ahb_div = ast2600_a1_axi_ahb_div0_table[(hwstrap1 >> 11) & 0x3];
22945e0908aSryan_chen 		}
23045e0908aSryan_chen 	} else {
231e5c4f4dfSryan_chen 		//ast2600a0 : fix axi = hpll / 2
23245e0908aSryan_chen 		axi_div = 2;
233d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
23445e0908aSryan_chen 	}
235d812df15Sryan_chen 
236bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
2372717883aSryan_chen 	return (rate / axi_div / ahb_div);
2382717883aSryan_chen }
2392717883aSryan_chen 
240c304f173Sryan_chen static u32 ast2600_get_bclk_rate(struct ast2600_scu *scu)
241c304f173Sryan_chen {
242c304f173Sryan_chen 	u32 rate;
243c304f173Sryan_chen 	u32 bclk_sel = (readl(&scu->clk_sel1) >> 20) & 0x7;
244c304f173Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
245c304f173Sryan_chen 
246c304f173Sryan_chen 	return (rate /((bclk_sel + 1) * 4));
247c304f173Sryan_chen }
248c304f173Sryan_chen 
2496fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = {
2502717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
2512717883aSryan_chen };
2522717883aSryan_chen 
2536fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = {
2546fa1ef3dSryan_chen 	2, 4, 6, 8, 10, 12, 14, 16,
2556fa1ef3dSryan_chen };
2566fa1ef3dSryan_chen 
2576fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu)
2582717883aSryan_chen {
2592717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
2606fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)];
261bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
2622717883aSryan_chen 
2632717883aSryan_chen 	return (rate / apb_div);
264d812df15Sryan_chen }
265d812df15Sryan_chen 
2666fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu)
2676fa1ef3dSryan_chen {
2686fa1ef3dSryan_chen 	u32 clk_sel4 = readl(&scu->clk_sel4);
2696fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)];
2706fa1ef3dSryan_chen 	u32 rate = ast2600_get_hclk(scu);
2716fa1ef3dSryan_chen 
2726fa1ef3dSryan_chen 	return (rate / apb_div);
2736fa1ef3dSryan_chen }
2746fa1ef3dSryan_chen 
2752e195992Sryan_chen static u32 ast2600_get_uxclk_in_rate(struct ast2600_scu *scu)
276d6e349c7Sryan_chen {
27727881d20Sryan_chen 	u32 clk_in = 0;
2782e195992Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel5);
279550e691bSryan_chen 
28027881d20Sryan_chen 	uxclk_sel &= 0x3;
28127881d20Sryan_chen 	switch(uxclk_sel) {
28227881d20Sryan_chen 		case 0:
28327881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
28427881d20Sryan_chen 			break;
28527881d20Sryan_chen 		case 1:
28627881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
28727881d20Sryan_chen 			break;
28827881d20Sryan_chen 		case 2:
28927881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
29027881d20Sryan_chen 			break;
29127881d20Sryan_chen 		case 3:
29227881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
29327881d20Sryan_chen 			break;
29427881d20Sryan_chen 	}
295d6e349c7Sryan_chen 
29627881d20Sryan_chen 	return clk_in;
29727881d20Sryan_chen }
29827881d20Sryan_chen 
2992e195992Sryan_chen static u32 ast2600_get_huxclk_in_rate(struct ast2600_scu *scu)
30027881d20Sryan_chen {
30127881d20Sryan_chen 	u32 clk_in = 0;
3022e195992Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel5);
30327881d20Sryan_chen 
30427881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
30527881d20Sryan_chen 	switch(huclk_sel) {
30627881d20Sryan_chen 		case 0:
30727881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
30827881d20Sryan_chen 			break;
30927881d20Sryan_chen 		case 1:
31027881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
31127881d20Sryan_chen 			break;
31227881d20Sryan_chen 		case 2:
31327881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
31427881d20Sryan_chen 			break;
31527881d20Sryan_chen 		case 3:
31627881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
31727881d20Sryan_chen 			break;
31827881d20Sryan_chen 	}
31927881d20Sryan_chen 
32027881d20Sryan_chen 	return clk_in;
32127881d20Sryan_chen }
32227881d20Sryan_chen 
3232e195992Sryan_chen static u32 ast2600_get_uart_uxclk_rate(struct ast2600_scu *scu)
32427881d20Sryan_chen {
3252e195992Sryan_chen 	u32 clk_in = ast2600_get_uxclk_in_rate(scu);
32627881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
32727881d20Sryan_chen 	unsigned int mult, div;
32827881d20Sryan_chen 
32927881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
33027881d20Sryan_chen 	u32 r = div_reg & 0xff;
33127881d20Sryan_chen 
33227881d20Sryan_chen 	mult = r;
3332e195992Sryan_chen 	div = (n * 2);
33427881d20Sryan_chen 	return (clk_in * mult)/div;
33527881d20Sryan_chen }
33627881d20Sryan_chen 
3372e195992Sryan_chen static u32 ast2600_get_uart_huxclk_rate(struct ast2600_scu *scu)
33827881d20Sryan_chen {
3392e195992Sryan_chen 	u32 clk_in = ast2600_get_huxclk_in_rate(scu);
34027881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
34127881d20Sryan_chen 
34227881d20Sryan_chen 	unsigned int mult, div;
34327881d20Sryan_chen 
34427881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
34527881d20Sryan_chen 	u32 r = div_reg & 0xff;
34627881d20Sryan_chen 
34727881d20Sryan_chen 	mult = r;
3482e195992Sryan_chen 	div = (n * 2);
34927881d20Sryan_chen 	return (clk_in * mult)/div;
35027881d20Sryan_chen }
35127881d20Sryan_chen 
352f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
353f51926eeSryan_chen {
354f51926eeSryan_chen 	u32 clkin = 0;
355f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
356f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
357f51926eeSryan_chen 
358f51926eeSryan_chen 	if(clk_sel & BIT(8)) {
359f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
360f51926eeSryan_chen 	} else {
36110069884Sryan_chen 		clkin = ast2600_get_hclk(scu);
362f51926eeSryan_chen 	}
363f51926eeSryan_chen 	div = (div + 1) << 1;
364f51926eeSryan_chen 
365f51926eeSryan_chen 	return (clkin / div);
366f51926eeSryan_chen }
367f51926eeSryan_chen 
368f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
369f51926eeSryan_chen {
370bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
371f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
372f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
373f51926eeSryan_chen 
374f51926eeSryan_chen 	div = (div + 1) << 2;
375f51926eeSryan_chen 
376f51926eeSryan_chen 	return (clkin / div);
377f51926eeSryan_chen }
378f51926eeSryan_chen 
379f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
38027881d20Sryan_chen {
38127881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
38227881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
38327881d20Sryan_chen 	ulong uart_clk = 0;
38427881d20Sryan_chen 
38527881d20Sryan_chen 	switch(uart_idx) {
38627881d20Sryan_chen 		case 1:
38727881d20Sryan_chen 		case 2:
38827881d20Sryan_chen 		case 3:
38927881d20Sryan_chen 		case 4:
39027881d20Sryan_chen 		case 6:
39127881d20Sryan_chen 			if(uart_sel & BIT(uart_idx - 1))
3922e195992Sryan_chen 				uart_clk = ast2600_get_uart_huxclk_rate(scu) ;
393550e691bSryan_chen 			else
3942e195992Sryan_chen 				uart_clk = ast2600_get_uart_uxclk_rate(scu) ;
39527881d20Sryan_chen 			break;
39627881d20Sryan_chen 		case 5: //24mhz is come form usb phy 48Mhz
39727881d20Sryan_chen 			{
39827881d20Sryan_chen 			u8 uart5_clk_sel = 0;
39927881d20Sryan_chen 			//high bit
40027881d20Sryan_chen 			if (readl(&scu->misc_ctrl1) & BIT(12))
40127881d20Sryan_chen 				uart5_clk_sel = 0x2;
40227881d20Sryan_chen 			else
40327881d20Sryan_chen 				uart5_clk_sel = 0x0;
404550e691bSryan_chen 
40527881d20Sryan_chen 			if (readl(&scu->clk_sel2) & BIT(14))
40627881d20Sryan_chen 				uart5_clk_sel |= 0x1;
407550e691bSryan_chen 
40827881d20Sryan_chen 			switch(uart5_clk_sel) {
40927881d20Sryan_chen 				case 0:
41027881d20Sryan_chen 					uart_clk = 24000000;
41127881d20Sryan_chen 					break;
41227881d20Sryan_chen 				case 1:
413def99fcbSryan_chen 					uart_clk = 192000000;
41427881d20Sryan_chen 					break;
41527881d20Sryan_chen 				case 2:
41627881d20Sryan_chen 					uart_clk = 24000000/13;
41727881d20Sryan_chen 					break;
41827881d20Sryan_chen 				case 3:
41927881d20Sryan_chen 					uart_clk = 192000000/13;
42027881d20Sryan_chen 					break;
42127881d20Sryan_chen 			}
42227881d20Sryan_chen 			}
42327881d20Sryan_chen 			break;
42427881d20Sryan_chen 		case 7:
42527881d20Sryan_chen 		case 8:
42627881d20Sryan_chen 		case 9:
42727881d20Sryan_chen 		case 10:
42827881d20Sryan_chen 		case 11:
42927881d20Sryan_chen 		case 12:
43027881d20Sryan_chen 		case 13:
43127881d20Sryan_chen 			if(uart_sel5 & BIT(uart_idx - 1))
4322e195992Sryan_chen 				uart_clk = ast2600_get_uart_huxclk_rate(scu);
43327881d20Sryan_chen 			else
4342e195992Sryan_chen 				uart_clk = ast2600_get_uart_uxclk_rate(scu);
43527881d20Sryan_chen 			break;
43627881d20Sryan_chen 	}
43727881d20Sryan_chen 
43827881d20Sryan_chen 	return uart_clk;
439550e691bSryan_chen }
440550e691bSryan_chen 
441feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
442feb42054Sryan_chen {
443feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
444feb42054Sryan_chen 	ulong rate = 0;
445feb42054Sryan_chen 
446feb42054Sryan_chen 	switch (clk->id) {
447feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
448bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
449bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
450d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
451bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
452d812df15Sryan_chen 		break;
453feb42054Sryan_chen 	case ASPEED_CLK_AHB:
454feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
455feb42054Sryan_chen 		break;
4566fa1ef3dSryan_chen 	case ASPEED_CLK_APB1:
4576fa1ef3dSryan_chen 		rate = ast2600_get_pclk1(priv->scu);
4586fa1ef3dSryan_chen 		break;
4596fa1ef3dSryan_chen 	case ASPEED_CLK_APB2:
4606fa1ef3dSryan_chen 		rate = ast2600_get_pclk2(priv->scu);
461feb42054Sryan_chen 		break;
462bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
463bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
464bbbfb0c5Sryan_chen 		break;
465feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
466feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
467feb42054Sryan_chen 		break;
468feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
469feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
470feb42054Sryan_chen 		break;
471feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
472feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
473feb42054Sryan_chen 		break;
474feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
475feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
476feb42054Sryan_chen 		break;
477feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
478feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
479feb42054Sryan_chen 		break;
480c304f173Sryan_chen 	case ASPEED_CLK_BCLK:
481c304f173Sryan_chen 		rate = ast2600_get_bclk_rate(priv->scu);
482c304f173Sryan_chen 		break;
483f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
484f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
485f51926eeSryan_chen 		break;
486f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
487f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
488f51926eeSryan_chen 		break;
4892e195992Sryan_chen 	case ASPEED_CLK_UARTX:
4902e195992Sryan_chen 		rate = ast2600_get_uart_uxclk_rate(priv->scu);
4912e195992Sryan_chen 		break;
4920998ddefSryan_chen 	case ASPEED_CLK_HUARTX:
4932e195992Sryan_chen 		rate = ast2600_get_uart_huxclk_rate(priv->scu);
4942e195992Sryan_chen 		break;
495feb42054Sryan_chen 	default:
496d812df15Sryan_chen 		pr_debug("can't get clk rate \n");
497feb42054Sryan_chen 		return -ENOENT;
498d812df15Sryan_chen 		break;
499feb42054Sryan_chen 	}
500feb42054Sryan_chen 
501feb42054Sryan_chen 	return rate;
502feb42054Sryan_chen }
503feb42054Sryan_chen 
504577fcdaeSDylan Hung /**
505577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
506577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
507577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
508550e691bSryan_chen  *
509577fcdaeSDylan Hung  * The function caller shall fill "pll->in" and "pll->out", then this function
510577fcdaeSDylan Hung  * will search the lookup table to find a valid PLL divider configuration.
511550e691bSryan_chen  */
512577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
513550e691bSryan_chen {
514577fcdaeSDylan Hung 	u32 i;
515577fcdaeSDylan Hung 	bool is_found = false;
516550e691bSryan_chen 
517577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
518577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
519577fcdaeSDylan Hung 		if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) {
520577fcdaeSDylan Hung 			is_found = true;
521577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
522577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
523577fcdaeSDylan Hung 			break;
524550e691bSryan_chen 		}
525550e691bSryan_chen 	}
526577fcdaeSDylan Hung 	return is_found;
527550e691bSryan_chen }
528fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu,
529fd52be0bSDylan Hung 				 struct ast2600_pll_cfg *p_cfg, int pll_idx)
530fd52be0bSDylan Hung {
531fd52be0bSDylan Hung 	u32 addr, addr_ext;
532fd52be0bSDylan Hung 	u32 reg;
533550e691bSryan_chen 
534fd52be0bSDylan Hung 	switch (pll_idx) {
535fd52be0bSDylan Hung 	case ASPEED_CLK_HPLL:
536fd52be0bSDylan Hung 		addr = (u32)(&scu->h_pll_param);
537fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->h_pll_ext_param);
538fd52be0bSDylan Hung 		break;
539fd52be0bSDylan Hung 	case ASPEED_CLK_MPLL:
540fd52be0bSDylan Hung 		addr = (u32)(&scu->m_pll_param);
541fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->m_pll_ext_param);
542fd52be0bSDylan Hung 		break;
543fd52be0bSDylan Hung 	case ASPEED_CLK_DPLL:
544fd52be0bSDylan Hung 		addr = (u32)(&scu->d_pll_param);
545fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->d_pll_ext_param);
546fd52be0bSDylan Hung 		break;
547fd52be0bSDylan Hung 	case ASPEED_CLK_EPLL:
548fd52be0bSDylan Hung 		addr = (u32)(&scu->e_pll_param);
549fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->e_pll_ext_param);
550fd52be0bSDylan Hung 		break;
551fd52be0bSDylan Hung 	default:
552fd52be0bSDylan Hung 		debug("unknown PLL index\n");
553fd52be0bSDylan Hung 		return 1;
554fd52be0bSDylan Hung 	}
555fd52be0bSDylan Hung 
556fd52be0bSDylan Hung 	p_cfg->reg.b.bypass = 0;
557fd52be0bSDylan Hung 	p_cfg->reg.b.off = 1;
558fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 1;
559fd52be0bSDylan Hung 
560fd52be0bSDylan Hung 	reg = readl(addr);
561fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
562fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
563fd52be0bSDylan Hung 	writel(reg, addr);
564fd52be0bSDylan Hung 
565fd52be0bSDylan Hung 	/* write extend parameter */
566fd52be0bSDylan Hung 	writel(p_cfg->ext_reg, addr_ext);
567fd52be0bSDylan Hung 	udelay(100);
568fd52be0bSDylan Hung 	p_cfg->reg.b.off = 0;
569fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 0;
570fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
571fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
572fd52be0bSDylan Hung 	writel(reg, addr);
573fd52be0bSDylan Hung 
574fd52be0bSDylan Hung 	/* polling PLL lock status */
575fd52be0bSDylan Hung 	while(0 == (readl(addr_ext) & BIT(31)));
576fd52be0bSDylan Hung 
577fd52be0bSDylan Hung 	return 0;
578fd52be0bSDylan Hung }
579feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
580550e691bSryan_chen {
581577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
582550e691bSryan_chen 
583577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
584577fcdaeSDylan Hung 	mpll.out = rate;
585577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&mpll)) {
586577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
587577fcdaeSDylan Hung 		return 0;
588577fcdaeSDylan Hung 	}
589fd52be0bSDylan Hung 	ast2600_configure_pll(scu, &(mpll.cfg), ASPEED_CLK_MPLL);
590577fcdaeSDylan Hung 
591cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
592d6e349c7Sryan_chen }
593d6e349c7Sryan_chen 
594d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
595550e691bSryan_chen {
596f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
597550e691bSryan_chen 
598550e691bSryan_chen 	ulong new_rate;
599550e691bSryan_chen 	switch (clk->id) {
600f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
601feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
602550e691bSryan_chen 		break;
603550e691bSryan_chen 	default:
604550e691bSryan_chen 		return -ENOENT;
605550e691bSryan_chen 	}
606550e691bSryan_chen 
607550e691bSryan_chen 	return new_rate;
608550e691bSryan_chen }
609feb42054Sryan_chen 
610f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
611f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
612f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
613f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
614f9aa0ee1Sryan_chen 
615cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu)
616cc476ffcSDylan Hung {
617eff28274SJohnny Huang 	/* scu340[25:0]: 1G default delay */
618eff28274SJohnny Huang 	clrsetbits_le32(&scu->mac12_clk_delay, GENMASK(25, 0),
619eff28274SJohnny Huang 			MAC_DEF_DELAY_1G);
6204760b3f8SDylan Hung 
6214760b3f8SDylan Hung 	/* set 100M/10M default delay */
6224760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M);
6234760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M);
624cc476ffcSDylan Hung 
625ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
626eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16));
627894c19cfSDylan Hung 
628cc476ffcSDylan Hung 	return 0;
629cc476ffcSDylan Hung }
630cc476ffcSDylan Hung 
63154f9cba1SDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu)
63254f9cba1SDylan Hung {
63354f9cba1SDylan Hung 
63454f9cba1SDylan Hung 	/*
635eff28274SJohnny Huang 	 * scu350[31]   RGMII 125M source: 0 = from IO pin
636eff28274SJohnny Huang 	 * scu350[25:0] MAC 1G delay
63754f9cba1SDylan Hung 	 */
638eff28274SJohnny Huang 	clrsetbits_le32(&scu->mac34_clk_delay, (BIT(31) | GENMASK(25, 0)),
639eff28274SJohnny Huang 			MAC34_DEF_DELAY_1G);
64054f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M);
64154f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M);
64254f9cba1SDylan Hung 
643eff28274SJohnny Huang 	/*
644eff28274SJohnny Huang 	 * clock source seletion and divider
645eff28274SJohnny Huang 	 * scu310[26:24] : MAC AHB bus clock = HCLK / 2
646eff28274SJohnny Huang 	 * scu310[18:16] : RMII 50M = HCLK_200M / 4
647eff28274SJohnny Huang 	 */
648eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)),
649eff28274SJohnny Huang 			((0x0 << 24) | (0x3 << 16)));
65054f9cba1SDylan Hung 
651eff28274SJohnny Huang 	/*
652eff28274SJohnny Huang 	 * set driving strength
653eff28274SJohnny Huang 	 * scu458[3:2] : MAC4 driving strength
654eff28274SJohnny Huang 	 * scu458[1:0] : MAC3 driving strength
655eff28274SJohnny Huang 	 */
656eff28274SJohnny Huang 	clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0),
657a961159eSDylan Hung 			(0x3 << 2) | (0x3 << 0));
65854f9cba1SDylan Hung 
65954f9cba1SDylan Hung 	return 0;
66054f9cba1SDylan Hung }
661eff28274SJohnny Huang 
66254f9cba1SDylan Hung /**
6635b5c3d44SDylan Hung  * ast2600 RGMII clock source tree
66454f9cba1SDylan Hung  *
66554f9cba1SDylan Hung  *    125M from external PAD -------->|\
66654f9cba1SDylan Hung  *    HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
66754f9cba1SDylan Hung  *            | |---->| divider |---->|/                             +
66854f9cba1SDylan Hung  *    EPLL -->|/                                                     |
66954f9cba1SDylan Hung  *                                                                   |
670eff28274SJohnny Huang  *    +---------<-----------|RGMIICK PAD output enable|<-------------+
67154f9cba1SDylan Hung  *    |
672eff28274SJohnny Huang  *    +--------------------------->|\
67354f9cba1SDylan Hung  *                                 | |----> RGMII 125M for MAC#3 & MAC#4
674eff28274SJohnny Huang  *    HCLK 200M ---->|divider|---->|/
6755b5c3d44SDylan Hung  *
676eff28274SJohnny Huang  * To simplify the control flow:
677eff28274SJohnny Huang  * 	1. RGMII 1/2 always use EPLL as the internal clock source
678eff28274SJohnny Huang  * 	2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source
6795b5c3d44SDylan Hung  *
680eff28274SJohnny Huang  *    125M from external PAD -------->|\
681eff28274SJohnny Huang  *                                    | |---->RGMII 125M for MAC#1 & MAC#2
682eff28274SJohnny Huang  *            EPLL---->| divider |--->|/                             +
683eff28274SJohnny Huang  *                                                                   |
684eff28274SJohnny Huang  *    +<--------------------|RGMIICK PAD output enable|<-------------+
685eff28274SJohnny Huang  *    |
686eff28274SJohnny Huang  *    +--------------------------->RGMII 125M for MAC#3 & MAC#4
687eff28274SJohnny Huang */
688eff28274SJohnny Huang #define RGMIICK_SRC_PAD			0
689eff28274SJohnny Huang #define RGMIICK_SRC_EPLL		1	/* recommended */
690eff28274SJohnny Huang #define RGMIICK_SRC_HPLL		2
691eff28274SJohnny Huang 
692eff28274SJohnny Huang #define RGMIICK_DIV2			1
693eff28274SJohnny Huang #define RGMIICK_DIV3			2
694eff28274SJohnny Huang #define RGMIICK_DIV4			3
695eff28274SJohnny Huang #define RGMIICK_DIV5			4
696eff28274SJohnny Huang #define RGMIICK_DIV6			5
697eff28274SJohnny Huang #define RGMIICK_DIV7			6
698eff28274SJohnny Huang #define RGMIICK_DIV8			7	/* recommended */
699eff28274SJohnny Huang 
700eff28274SJohnny Huang #define RMIICK_DIV4			0
701eff28274SJohnny Huang #define RMIICK_DIV8			1
702eff28274SJohnny Huang #define RMIICK_DIV12			2
703eff28274SJohnny Huang #define RMIICK_DIV16			3
704eff28274SJohnny Huang #define RMIICK_DIV20			4	/* recommended */
705eff28274SJohnny Huang #define RMIICK_DIV24			5
706eff28274SJohnny Huang #define RMIICK_DIV28			6
707eff28274SJohnny Huang #define RMIICK_DIV32			7
708eff28274SJohnny Huang 
709eff28274SJohnny Huang struct ast2600_mac_clk_div {
710eff28274SJohnny Huang 	u32 src;	/* 0=external PAD, 1=internal PLL */
711eff28274SJohnny Huang 	u32 fin;	/* divider input speed */
712eff28274SJohnny Huang 	u32 n;		/* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */
713eff28274SJohnny Huang 	u32 fout;	/* fout = fin / n */
714eff28274SJohnny Huang };
715eff28274SJohnny Huang 
716eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = {
717eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
718eff28274SJohnny Huang 	.fin = 1000000000,
719eff28274SJohnny Huang 	.n = RGMIICK_DIV8,
720eff28274SJohnny Huang 	.fout = 125000000,
721eff28274SJohnny Huang };
722eff28274SJohnny Huang 
723eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = {
724eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
725eff28274SJohnny Huang 	.fin = 1000000000,
726eff28274SJohnny Huang 	.n = RMIICK_DIV20,
727eff28274SJohnny Huang 	.fout = 50000000,
728eff28274SJohnny Huang };
729eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu,
730eff28274SJohnny Huang 				 struct ast2600_mac_clk_div *p_cfg)
731eff28274SJohnny Huang {
732eff28274SJohnny Huang 	struct ast2600_pll_desc pll;
733eff28274SJohnny Huang 
734eff28274SJohnny Huang 	pll.in = AST2600_CLK_IN;
735eff28274SJohnny Huang 	pll.out = p_cfg->fin;
736eff28274SJohnny Huang 	if (false == ast2600_search_clock_config(&pll)) {
737eff28274SJohnny Huang 		printf("error!! unable to find valid ETHNET MAC clock "
738eff28274SJohnny Huang 		       "setting\n");
739eff28274SJohnny Huang 		debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w,
740eff28274SJohnny Huang 		      pll.cfg.ext_reg);
741eff28274SJohnny Huang 		debug("%s: pll cfg = %02x %02x %02x\n", __func__,
742eff28274SJohnny Huang 		      pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p);
743eff28274SJohnny Huang 		return;
744eff28274SJohnny Huang 	}
745eff28274SJohnny Huang 	ast2600_configure_pll(p_scu, &(pll.cfg), p_cfg->src);
746eff28274SJohnny Huang }
747eff28274SJohnny Huang 
748eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu,
749eff28274SJohnny Huang 				   struct ast2600_mac_clk_div *p_cfg)
750eff28274SJohnny Huang {
751eff28274SJohnny Huang 	u32 reg_304 = readl(&p_scu->clk_sel2);
752eff28274SJohnny Huang 	u32 reg_340 = readl(&p_scu->mac12_clk_delay);
753eff28274SJohnny Huang 	u32 reg_350 = readl(&p_scu->mac34_clk_delay);
754eff28274SJohnny Huang 
755eff28274SJohnny Huang 	reg_340 &= ~GENMASK(31, 29);
756eff28274SJohnny Huang 	/* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */
757eff28274SJohnny Huang 	reg_340 |= BIT(28);
758eff28274SJohnny Huang 	if ((p_cfg->src == ASPEED_CLK_EPLL) ||
759eff28274SJohnny Huang 	    (p_cfg->src == ASPEED_CLK_HPLL)) {
760eff28274SJohnny Huang 		/*
761eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
762eff28274SJohnny Huang 		 * the divider setting
763eff28274SJohnny Huang 		 */
764eff28274SJohnny Huang 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) {
765eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
766eff28274SJohnny Huang 		}
767eff28274SJohnny Huang 		/* scu340[31]: select RGMII 125M from internal source */
768eff28274SJohnny Huang 		reg_340 |= BIT(31);
769eff28274SJohnny Huang 	}
770eff28274SJohnny Huang 
771eff28274SJohnny Huang 	reg_304 &= ~GENMASK(23, 20);
772eff28274SJohnny Huang 
773eff28274SJohnny Huang 	/* set clock divider */
774eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 20;
775eff28274SJohnny Huang 
776eff28274SJohnny Huang 	/* select internal clock source */
777eff28274SJohnny Huang 	if (ASPEED_CLK_HPLL == p_cfg->src) {
778eff28274SJohnny Huang 		reg_304 |= BIT(23);
779eff28274SJohnny Huang 	}
780eff28274SJohnny Huang 
781eff28274SJohnny Huang 	/* RGMII 3/4 clock source select */
782eff28274SJohnny Huang 	reg_350 &= ~BIT(31);
783eff28274SJohnny Huang #if 0
784eff28274SJohnny Huang 	if (RGMII_3_4_CLK_SRC_HCLK == p_cfg->rgmii_3_4_clk_src) {
785eff28274SJohnny Huang 		reg_350 |= BIT(31);
786eff28274SJohnny Huang 	}
787eff28274SJohnny Huang 
788eff28274SJohnny Huang 	/* set clock divider */
789eff28274SJohnny Huang 	reg_310 &= ~GENMASK(22, 20);
790eff28274SJohnny Huang 	reg_310 |= (p_cfg->hclk_clk_div & 0x7) << 20;
791eff28274SJohnny Huang #endif
792eff28274SJohnny Huang 
793eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
794eff28274SJohnny Huang 	writel(reg_340, &p_scu->mac12_clk_delay);
795eff28274SJohnny Huang 	writel(reg_350, &p_scu->mac34_clk_delay);
796eff28274SJohnny Huang }
797eff28274SJohnny Huang 
798eff28274SJohnny Huang /**
7995b5c3d44SDylan Hung  * ast2600 RMII/NCSI clock source tree
8005b5c3d44SDylan Hung  *
8015b5c3d44SDylan Hung  *    HPLL -->|\
8025b5c3d44SDylan Hung  *            | |---->| divider |----> RMII 50M for MAC#1 & MAC#2
8035b5c3d44SDylan Hung  *    EPLL -->|/
8045b5c3d44SDylan Hung  *
8055b5c3d44SDylan Hung  *    HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4
80654f9cba1SDylan Hung */
807eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu,
808eff28274SJohnny Huang 				  struct ast2600_mac_clk_div *p_cfg)
80954f9cba1SDylan Hung {
810eff28274SJohnny Huang 	u32 reg_304;
811eff28274SJohnny Huang 	u32 reg_310;
812eff28274SJohnny Huang 
813eff28274SJohnny Huang 	if ((p_cfg->src == ASPEED_CLK_EPLL) ||
814eff28274SJohnny Huang 	    (p_cfg->src == ASPEED_CLK_HPLL)) {
815eff28274SJohnny Huang 		/*
816eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
817eff28274SJohnny Huang 		 * the divider setting
818eff28274SJohnny Huang 		 */
819eff28274SJohnny Huang 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) {
820eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
821eff28274SJohnny Huang 		}
82254f9cba1SDylan Hung 	}
82354f9cba1SDylan Hung 
824eff28274SJohnny Huang 	reg_304 = readl(&p_scu->clk_sel2);
825eff28274SJohnny Huang 	reg_310 = readl(&p_scu->clk_sel4);
826eff28274SJohnny Huang 
827eff28274SJohnny Huang 	reg_304 &= ~GENMASK(19, 16);
828eff28274SJohnny Huang 
829eff28274SJohnny Huang 	/* set RMII 1/2 clock divider */
830eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 16;
831eff28274SJohnny Huang 
832eff28274SJohnny Huang 	/* RMII clock source selection */
833eff28274SJohnny Huang 	if (ASPEED_CLK_HPLL == p_cfg->src) {
834eff28274SJohnny Huang 		reg_304 |= BIT(19);
83554f9cba1SDylan Hung 	}
836eff28274SJohnny Huang 
837eff28274SJohnny Huang 	/* set RMII 3/4 clock divider */
838eff28274SJohnny Huang 	reg_310 &= ~GENMASK(18, 16);
839eff28274SJohnny Huang 	reg_310 |= (0x3 << 16);
840eff28274SJohnny Huang 
841eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
842eff28274SJohnny Huang 	writel(reg_310, &p_scu->clk_sel4);
843eff28274SJohnny Huang }
844eff28274SJohnny Huang 
845f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
846f9aa0ee1Sryan_chen {
847f9aa0ee1Sryan_chen 	u32 reset_bit;
848f9aa0ee1Sryan_chen 	u32 clkstop_bit;
849f9aa0ee1Sryan_chen 
850f9aa0ee1Sryan_chen 	switch (index) {
851f9aa0ee1Sryan_chen 	case 1:
852f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
853f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
854f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
855f9aa0ee1Sryan_chen 		udelay(100);
856f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
857f9aa0ee1Sryan_chen 		mdelay(10);
858f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
859f9aa0ee1Sryan_chen 
860f9aa0ee1Sryan_chen 		break;
861f9aa0ee1Sryan_chen 	case 2:
862f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
863f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
864f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
865f9aa0ee1Sryan_chen 		udelay(100);
866f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
867f9aa0ee1Sryan_chen 		mdelay(10);
868f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
869f9aa0ee1Sryan_chen 		break;
870f9aa0ee1Sryan_chen 	case 3:
871f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
872f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
873f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
874f9aa0ee1Sryan_chen 		udelay(100);
875f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
876f9aa0ee1Sryan_chen 		mdelay(10);
877f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
878f9aa0ee1Sryan_chen 		break;
879f9aa0ee1Sryan_chen 	case 4:
880f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
881f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
882f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
883f9aa0ee1Sryan_chen 		udelay(100);
884f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
885f9aa0ee1Sryan_chen 		mdelay(10);
886f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
887f9aa0ee1Sryan_chen 		break;
888f9aa0ee1Sryan_chen 	default:
889f9aa0ee1Sryan_chen 		return -EINVAL;
890f9aa0ee1Sryan_chen 	}
891f9aa0ee1Sryan_chen 
892f9aa0ee1Sryan_chen 	return 0;
893f9aa0ee1Sryan_chen }
894550e691bSryan_chen 
895f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
896f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
897f51926eeSryan_chen {
898f51926eeSryan_chen 	u32 reset_bit;
899f51926eeSryan_chen 	u32 clkstop_bit;
900f51926eeSryan_chen 
901f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
902f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
903f51926eeSryan_chen 
904fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
905fc9f12e6Sryan_chen 
906f51926eeSryan_chen 	udelay(100);
907f51926eeSryan_chen 	//enable clk
908f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
909f51926eeSryan_chen 	mdelay(10);
910fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
911f51926eeSryan_chen 
912f51926eeSryan_chen 	return 0;
913f51926eeSryan_chen }
914f51926eeSryan_chen 
915f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31
916f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
917f51926eeSryan_chen #define SCU_CLK_SD_DIV(x)			(x << 28)
9182cd7cba2Sryan_chen #define SCU_CLK_SD_FROM_APLL_CLK	BIT(8)
919f51926eeSryan_chen 
920f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
921f51926eeSryan_chen {
922f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
923f51926eeSryan_chen 	u32 enableclk_bit;
9242cd7cba2Sryan_chen 	u32 rate = 0;
9252cd7cba2Sryan_chen 	u32 div = 0;
9262cd7cba2Sryan_chen 	int i = 0;
927f51926eeSryan_chen 
928f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
929f51926eeSryan_chen 
9302cd7cba2Sryan_chen 	//ast2600 sd controller max clk is 200Mhz : use apll for clock source 800/4 = 200 : controller max is 200mhz
9312cd7cba2Sryan_chen 	rate = ast2600_get_apll_rate(scu);
9322cd7cba2Sryan_chen 	for(i = 0; i < 8; i++) {
9332cd7cba2Sryan_chen 		div = (i + 1) * 2;
9342cd7cba2Sryan_chen 		if ((rate / div) <= 200000000)
9352cd7cba2Sryan_chen 			break;
9362cd7cba2Sryan_chen 	}
937f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
9382cd7cba2Sryan_chen 	clk_sel |= SCU_CLK_SD_DIV(i) | SCU_CLK_SD_FROM_APLL_CLK;
939f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
940f51926eeSryan_chen 
941f51926eeSryan_chen 	//enable clk
942f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
943f51926eeSryan_chen 
944f51926eeSryan_chen 	return 0;
945f51926eeSryan_chen }
946f51926eeSryan_chen 
947f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
948f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
949f51926eeSryan_chen {
950f51926eeSryan_chen 	u32 reset_bit;
951f51926eeSryan_chen 	u32 clkstop_bit;
952f51926eeSryan_chen 
953f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
954f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
955f51926eeSryan_chen 
956fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
957f51926eeSryan_chen 	udelay(100);
958f51926eeSryan_chen 	//enable clk
959f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
960f51926eeSryan_chen 	mdelay(10);
961fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
962f51926eeSryan_chen 
963f51926eeSryan_chen 	return 0;
964f51926eeSryan_chen }
965f51926eeSryan_chen 
966f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15
967f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
968f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x)			(x << 12)
9692cd7cba2Sryan_chen #define SCU_CLK_EMMC_FROM_MPLL_CLK	BIT(11)	//AST2600A1
970f51926eeSryan_chen 
971f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
972f51926eeSryan_chen {
973*85d48d8cSryan_chen 	u32 revision_id = readl(&scu->chip_id1);
974f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
975f51926eeSryan_chen 	u32 enableclk_bit;
976f4c4ddb1Sryan_chen 	u32 rate = 0;
977f4c4ddb1Sryan_chen 	u32 div = 0;
978f4c4ddb1Sryan_chen 	int i = 0;
979f51926eeSryan_chen 
980d0bdd5f3Sryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC);
981f51926eeSryan_chen 
9822cd7cba2Sryan_chen 	//ast2600 eMMC controller max clk is 200Mhz
9838c32294fSryan_chen 	/*********************************************************************************************
9848c32294fSryan_chen 		HPll -> 1/2 --
9858c32294fSryan_chen 						\
9868c32294fSryan_chen 						--> SCU300[11] -> SCU300[14:12] [1/N] -> EMMC12C[15:8] [1/N] --> eMMC clk
9878c32294fSryan_chen 						/
9888c32294fSryan_chen 		MPLL - ----- ->
9898c32294fSryan_chen 	*********************************************************************************************/
990*85d48d8cSryan_chen 	if(((revision_id & CHIP_REVISION_ID) >> 16)) {
9918c32294fSryan_chen 		//AST2600A1 : use mpll to be clk source
992b0c30ea3Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
993b0c30ea3Sryan_chen 		for(i = 0; i < 8; i++) {
994b0c30ea3Sryan_chen 			div = (i + 1) * 2;
995b0c30ea3Sryan_chen 			if ((rate / div) <= 200000000)
996b0c30ea3Sryan_chen 				break;
997b0c30ea3Sryan_chen 		}
998b0c30ea3Sryan_chen 
999b0c30ea3Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
10002cd7cba2Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i) | SCU_CLK_EMMC_FROM_MPLL_CLK;
1001b0c30ea3Sryan_chen 		writel(clk_sel, &scu->clk_sel1);
1002b0c30ea3Sryan_chen 
1003b0c30ea3Sryan_chen 	} else {
10042cd7cba2Sryan_chen 		//AST2600A0 : use hpll to be clk source
1005f4c4ddb1Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1006f4c4ddb1Sryan_chen 
1007f4c4ddb1Sryan_chen 		for(i = 0; i < 8; i++) {
1008f4c4ddb1Sryan_chen 			div = (i + 1) * 4;
1009f4c4ddb1Sryan_chen 			if ((rate / div) <= 200000000)
1010f4c4ddb1Sryan_chen 				break;
1011f4c4ddb1Sryan_chen 		}
1012f4c4ddb1Sryan_chen 
1013f4c4ddb1Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
1014f4c4ddb1Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i);
1015f51926eeSryan_chen 		writel(clk_sel, &scu->clk_sel1);
1016b0c30ea3Sryan_chen 	}
1017f51926eeSryan_chen 
1018f51926eeSryan_chen 	//enable clk
1019f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
1020f51926eeSryan_chen 
1021f51926eeSryan_chen 	return 0;
1022f51926eeSryan_chen }
1023f51926eeSryan_chen 
1024baf00c26Sryan_chen #define SCU_CLKSTOP_FSICLK 30
1025baf00c26Sryan_chen 
1026baf00c26Sryan_chen static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu)
1027baf00c26Sryan_chen {
1028baf00c26Sryan_chen 	u32 reset_bit;
1029baf00c26Sryan_chen 	u32 clkstop_bit;
1030baf00c26Sryan_chen 
1031baf00c26Sryan_chen 	reset_bit = BIT(ASPEED_RESET_FSI % 32);
1032baf00c26Sryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_FSICLK);
1033baf00c26Sryan_chen 
1034baf00c26Sryan_chen 	/* The FSI clock is shared between masters. If it's already on
1035baf00c26Sryan_chen 	 * don't touch it, as that will reset the existing master. */
1036baf00c26Sryan_chen 	if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) {
1037baf00c26Sryan_chen 		debug("%s: already running, not touching it\n", __func__);
1038baf00c26Sryan_chen 		return 0;
1039baf00c26Sryan_chen 	}
1040baf00c26Sryan_chen 
1041baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
1042baf00c26Sryan_chen 	udelay(100);
1043baf00c26Sryan_chen 	//enable clk
1044baf00c26Sryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1045baf00c26Sryan_chen 	mdelay(10);
1046baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1047baf00c26Sryan_chen 
1048baf00c26Sryan_chen 	return 0;
1049baf00c26Sryan_chen }
1050baf00c26Sryan_chen 
1051b8ec5ceaSryan_chen static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu)
1052b8ec5ceaSryan_chen {
1053b8ec5ceaSryan_chen 	u32 reset_bit;
1054b8ec5ceaSryan_chen 	u32 clkstop_bit;
1055b8ec5ceaSryan_chen 
1056b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P1);
1057b8ec5ceaSryan_chen 	clkstop_bit = BIT(14);
1058b8ec5ceaSryan_chen 
1059b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1060b8ec5ceaSryan_chen 		udelay(100);
1061b8ec5ceaSryan_chen 	//enable phy clk
1062b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_ctrl1);
1063b8ec5ceaSryan_chen 	mdelay(20);
1064b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1065b8ec5ceaSryan_chen 
1066b8ec5ceaSryan_chen 	return 0;
1067b8ec5ceaSryan_chen }
1068b8ec5ceaSryan_chen 
1069b8ec5ceaSryan_chen static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu)
1070b8ec5ceaSryan_chen {
1071b8ec5ceaSryan_chen 	u32 reset_bit;
1072b8ec5ceaSryan_chen 	u32 clkstop_bit;
1073b8ec5ceaSryan_chen 
1074b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P2);
1075b8ec5ceaSryan_chen 	clkstop_bit = BIT(7);
1076b8ec5ceaSryan_chen 
1077b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1078b8ec5ceaSryan_chen 			udelay(100);
1079b8ec5ceaSryan_chen 	//enable phy clk
1080b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1081b8ec5ceaSryan_chen 	mdelay(20);
1082b8ec5ceaSryan_chen 
1083b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1084b8ec5ceaSryan_chen 
1085b8ec5ceaSryan_chen 	return 0;
1086b8ec5ceaSryan_chen }
1087b8ec5ceaSryan_chen 
1088d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
1089550e691bSryan_chen {
1090f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
1091550e691bSryan_chen 
1092550e691bSryan_chen 	switch (clk->id) {
109386f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC1CLK:
109486f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 1);
1095550e691bSryan_chen 			break;
109686f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC2CLK:
109786f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 2);
1098550e691bSryan_chen 			break;
109977843939Sryan_chen 		case ASPEED_CLK_GATE_MAC3CLK:
110077843939Sryan_chen 			ast2600_configure_mac(priv->scu, 3);
110177843939Sryan_chen 			break;
110277843939Sryan_chen 		case ASPEED_CLK_GATE_MAC4CLK:
110377843939Sryan_chen 			ast2600_configure_mac(priv->scu, 4);
110477843939Sryan_chen 			break;
1105f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDCLK:
1106f51926eeSryan_chen 			ast2600_enable_sdclk(priv->scu);
1107f51926eeSryan_chen 			break;
1108f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDEXTCLK:
1109f51926eeSryan_chen 			ast2600_enable_extsdclk(priv->scu);
1110f51926eeSryan_chen 			break;
1111f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCCLK:
1112f51926eeSryan_chen 			ast2600_enable_emmcclk(priv->scu);
1113f51926eeSryan_chen 			break;
1114f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCEXTCLK:
1115f51926eeSryan_chen 			ast2600_enable_extemmcclk(priv->scu);
1116f51926eeSryan_chen 			break;
1117baf00c26Sryan_chen 		case ASPEED_CLK_GATE_FSICLK:
1118baf00c26Sryan_chen 			ast2600_enable_fsiclk(priv->scu);
1119baf00c26Sryan_chen 			break;
1120b8ec5ceaSryan_chen 		case ASPEED_CLK_GATE_USBPORT1CLK:
1121b8ec5ceaSryan_chen 			ast2600_enable_usbahclk(priv->scu);
1122b8ec5ceaSryan_chen 			break;
1123b8ec5ceaSryan_chen 		case ASPEED_CLK_GATE_USBPORT2CLK:
1124b8ec5ceaSryan_chen 			ast2600_enable_usbbhclk(priv->scu);
1125b8ec5ceaSryan_chen 			break;
1126550e691bSryan_chen 		default:
1127f9aa0ee1Sryan_chen 			pr_debug("can't enable clk \n");
1128550e691bSryan_chen 			return -ENOENT;
112977843939Sryan_chen 			break;
1130550e691bSryan_chen 	}
1131550e691bSryan_chen 
1132550e691bSryan_chen 	return 0;
1133550e691bSryan_chen }
1134550e691bSryan_chen 
1135f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
1136d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
1137d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
1138d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
1139550e691bSryan_chen };
1140550e691bSryan_chen 
1141d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
1142550e691bSryan_chen {
1143f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
114461ab9607Sryan_chen 	u32 uart_clk_source;
1145550e691bSryan_chen 
1146f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
1147f0d895afSryan_chen 	if (IS_ERR(priv->scu))
1148f0d895afSryan_chen 		return PTR_ERR(priv->scu);
1149550e691bSryan_chen 
1150b55086a6SChia-Wei, Wang 	uart_clk_source = dev_read_u32_default(dev, "uart-clk-source",
115161ab9607Sryan_chen 					    0x0);
115261ab9607Sryan_chen 
115361ab9607Sryan_chen 	if(uart_clk_source) {
115456dd3e85Sryan_chen 		if(uart_clk_source & GENMASK(5, 0))
115556dd3e85Sryan_chen 			setbits_le32(&priv->scu->clk_sel4, uart_clk_source & GENMASK(5, 0));
115656dd3e85Sryan_chen 		if(uart_clk_source & GENMASK(12, 6))
115756dd3e85Sryan_chen 			setbits_le32(&priv->scu->clk_sel5, uart_clk_source & GENMASK(12, 6));
115861ab9607Sryan_chen 	}
115961ab9607Sryan_chen 
1160b89500a2SDylan Hung 
1161b89500a2SDylan Hung 	ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig);
1162b89500a2SDylan Hung 	ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig);
1163b89500a2SDylan Hung 	ast2600_configure_mac12_clk(priv->scu);
1164b89500a2SDylan Hung 	ast2600_configure_mac34_clk(priv->scu);
1165b89500a2SDylan Hung 
1166fd0306aaSJohnny Huang 	/* RSA clock = HPLL/3 */
1167fd0306aaSJohnny Huang 	setbits_le32(&priv->scu->clk_sel1, BIT(19));
1168fd0306aaSJohnny Huang 	setbits_le32(&priv->scu->clk_sel1, BIT(27));
1169fd0306aaSJohnny Huang 
1170550e691bSryan_chen 	return 0;
1171550e691bSryan_chen }
1172550e691bSryan_chen 
1173d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
1174550e691bSryan_chen {
1175550e691bSryan_chen 	int ret;
1176550e691bSryan_chen 
1177550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
1178550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
1179550e691bSryan_chen 	if (ret)
1180550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
1181550e691bSryan_chen 
1182550e691bSryan_chen 	return 0;
1183550e691bSryan_chen }
1184550e691bSryan_chen 
1185d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK)
1186d35ac78cSryan_chen struct aspeed_clks {
1187d35ac78cSryan_chen 	ulong id;
1188d35ac78cSryan_chen 	const char *name;
1189d35ac78cSryan_chen };
1190d35ac78cSryan_chen 
1191d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
1192d35ac78cSryan_chen 	{ ASPEED_CLK_HPLL, "hpll" },
1193d35ac78cSryan_chen 	{ ASPEED_CLK_MPLL, "mpll" },
1194d35ac78cSryan_chen 	{ ASPEED_CLK_APLL, "apll" },
1195d35ac78cSryan_chen 	{ ASPEED_CLK_EPLL, "epll" },
1196d35ac78cSryan_chen 	{ ASPEED_CLK_DPLL, "dpll" },
1197d35ac78cSryan_chen 	{ ASPEED_CLK_AHB, "hclk" },
11986fa1ef3dSryan_chen 	{ ASPEED_CLK_APB1, "pclk1" },
11996fa1ef3dSryan_chen 	{ ASPEED_CLK_APB2, "pclk2" },
1200c304f173Sryan_chen 	{ ASPEED_CLK_BCLK, "bclk" },
12012e195992Sryan_chen 	{ ASPEED_CLK_UARTX, "uxclk" },
1202def99fcbSryan_chen 	{ ASPEED_CLK_HUARTX, "huxclk" },
1203d35ac78cSryan_chen };
1204d35ac78cSryan_chen 
1205d35ac78cSryan_chen int soc_clk_dump(void)
1206d35ac78cSryan_chen {
1207d35ac78cSryan_chen 	struct udevice *dev;
1208d35ac78cSryan_chen 	struct clk clk;
1209d35ac78cSryan_chen 	unsigned long rate;
1210d35ac78cSryan_chen 	int i, ret;
1211d35ac78cSryan_chen 
1212d35ac78cSryan_chen 	ret = uclass_get_device_by_driver(UCLASS_CLK,
1213d35ac78cSryan_chen 					  DM_GET_DRIVER(aspeed_scu), &dev);
1214d35ac78cSryan_chen 	if (ret)
1215d35ac78cSryan_chen 		return ret;
1216d35ac78cSryan_chen 
1217d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
1218d35ac78cSryan_chen 
1219d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
1220d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
1221d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
1222d35ac78cSryan_chen 		if (ret < 0) {
1223d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
1224d35ac78cSryan_chen 			continue;
1225d35ac78cSryan_chen 		}
1226d35ac78cSryan_chen 
1227d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
1228d35ac78cSryan_chen 		rate = ret;
1229d35ac78cSryan_chen 
1230d35ac78cSryan_chen 		clk_free(&clk);
1231d35ac78cSryan_chen 
1232d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
1233d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
1234d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
1235d35ac78cSryan_chen 			continue;
1236d35ac78cSryan_chen 		}
1237d35ac78cSryan_chen 		if (ret < 0) {
1238d35ac78cSryan_chen 			printf("%s %lu: get_rate err: %d\n",
1239d35ac78cSryan_chen 			       __func__, aspeed_clk_names[i].id, ret);
1240d35ac78cSryan_chen 			continue;
1241d35ac78cSryan_chen 		}
1242d35ac78cSryan_chen 
1243d35ac78cSryan_chen 		printf("%s(%3lu):\t%lu\n",
1244d35ac78cSryan_chen 		       aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate);
1245d35ac78cSryan_chen 	}
1246d35ac78cSryan_chen 
1247d35ac78cSryan_chen 	return 0;
1248d35ac78cSryan_chen }
1249d35ac78cSryan_chen #endif
1250d35ac78cSryan_chen 
1251d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
1252d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
1253550e691bSryan_chen 	{ }
1254550e691bSryan_chen };
1255550e691bSryan_chen 
1256aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
1257aa36597fSDylan Hung 	.name		= "aspeed_scu",
1258550e691bSryan_chen 	.id		= UCLASS_CLK,
1259d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
1260f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
1261f9aa0ee1Sryan_chen 	.ops		= &ast2600_clk_ops,
1262d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
1263d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
1264550e691bSryan_chen };
1265