xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision 8c32294f9f7437a12e45ce183e30b40a1fa34f27)
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 
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 {
52577fcdaeSDylan Hung 	unsigned int 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;
66577fcdaeSDylan Hung 	unsigned int 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[] = {
76577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 400000000,
77577fcdaeSDylan Hung     .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 1,
78577fcdaeSDylan Hung     .cfg.ext_reg = 0x31,
79577fcdaeSDylan Hung     },
80577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 200000000,
81577fcdaeSDylan Hung     .cfg.reg.b.m = 127, .cfg.reg.b.n = 0, .cfg.reg.b.p = 15,
82577fcdaeSDylan Hung     .cfg.ext_reg = 0x3f
83577fcdaeSDylan Hung     },
84577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 334000000,
85577fcdaeSDylan Hung     .cfg.reg.b.m = 667, .cfg.reg.b.n = 4, .cfg.reg.b.p = 9,
86577fcdaeSDylan Hung     .cfg.ext_reg = 0x14d
87577fcdaeSDylan Hung     },
88577fcdaeSDylan Hung 
89577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 1000000000,
90577fcdaeSDylan Hung     .cfg.reg.b.m = 119, .cfg.reg.b.n = 2, .cfg.reg.b.p = 0,
91577fcdaeSDylan Hung     .cfg.ext_reg = 0x3d
92577fcdaeSDylan Hung     },
93577fcdaeSDylan Hung 
94577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 50000000,
95577fcdaeSDylan Hung     .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 15,
96577fcdaeSDylan Hung     .cfg.ext_reg = 0x31
97577fcdaeSDylan Hung     },
98550e691bSryan_chen };
99550e691bSryan_chen 
100bbbfb0c5Sryan_chen extern u32 ast2600_get_pll_rate(struct ast2600_scu *scu, int pll_idx)
101550e691bSryan_chen {
102d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
103bbbfb0c5Sryan_chen 	u32 pll_reg = 0;
1049639db61Sryan_chen 	unsigned int mult, div = 1;
105550e691bSryan_chen 
106bbbfb0c5Sryan_chen 	switch(pll_idx) {
107bbbfb0c5Sryan_chen 		case ASPEED_CLK_HPLL:
108bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->h_pll_param);
109bbbfb0c5Sryan_chen 			break;
110bbbfb0c5Sryan_chen 		case ASPEED_CLK_MPLL:
111bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->m_pll_param);
112bbbfb0c5Sryan_chen 			break;
113bbbfb0c5Sryan_chen 		case ASPEED_CLK_DPLL:
114bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->d_pll_param);
115bbbfb0c5Sryan_chen 			break;
116bbbfb0c5Sryan_chen 		case ASPEED_CLK_EPLL:
117bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->e_pll_param);
118bbbfb0c5Sryan_chen 			break;
119bbbfb0c5Sryan_chen 	}
120bbbfb0c5Sryan_chen 	if (pll_reg & BIT(24)) {
1219639db61Sryan_chen 		/* Pass through mode */
1229639db61Sryan_chen 		mult = div = 1;
1239639db61Sryan_chen 	} else {
1249639db61Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */
12575ced45aSDylan Hung 		union ast2600_pll_reg reg;
12675ced45aSDylan Hung 		reg.w = pll_reg;
127e5c4f4dfSryan_chen 		if(pll_idx == ASPEED_CLK_HPLL) {
128e5c4f4dfSryan_chen 			/*
129e5c4f4dfSryan_chen 			HPLL Numerator (M) = fix 0x5F when SCU500[10]=1
130e5c4f4dfSryan_chen 								 fix 0xBF when SCU500[10]=0 and SCU500[8]=1
131e5c4f4dfSryan_chen 			SCU200[12:0] (default 0x8F) when SCU510[10]=0 and SCU510[8]=0
132e5c4f4dfSryan_chen 			HPLL Denumerator (N) =	SCU200[18:13] (default 0x2)
133e5c4f4dfSryan_chen 			HPLL Divider (P)	 =  SCU200[22:19] (default 0x0)
134e5c4f4dfSryan_chen 			HPLL Bandwidth Adj (NB) =  fix 0x2F when SCU500[10]=1
135e5c4f4dfSryan_chen 									   fix 0x5F	when SCU500[10]=0 and SCU500[8]=1
136e5c4f4dfSryan_chen 			SCU204[11:0] (default 0x31) when SCU500[10]=0 and SCU500[8]=0
137e5c4f4dfSryan_chen 			*/
138e5c4f4dfSryan_chen 			u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
139e5c4f4dfSryan_chen 			if(hwstrap1 & BIT(10))
140e5c4f4dfSryan_chen 				reg.b.m = 0x5F;
141e5c4f4dfSryan_chen 			else {
142e5c4f4dfSryan_chen 				if(hwstrap1 & BIT(8))
143e5c4f4dfSryan_chen 					reg.b.m = 0xBF;
144e5c4f4dfSryan_chen 				//otherwise keep default 0x8F
145e5c4f4dfSryan_chen 			}
146e5c4f4dfSryan_chen 		}
14775ced45aSDylan Hung 		mult = (reg.b.m + 1) / (reg.b.n + 1);
14875ced45aSDylan Hung 		div = (reg.b.p + 1);
1499639db61Sryan_chen 	}
1509639db61Sryan_chen 	return ((clkin * mult)/div);
151550e691bSryan_chen }
152550e691bSryan_chen 
1534f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
154550e691bSryan_chen {
155bbbfb0c5Sryan_chen 	u32 clkin = AST2600_CLK_IN;
15639283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
15739283ea7Sryan_chen 	unsigned int mult, div = 1;
158d6e349c7Sryan_chen 
15939283ea7Sryan_chen 	if (apll_reg & BIT(20)) {
160d6e349c7Sryan_chen 		/* Pass through mode */
161d6e349c7Sryan_chen 		mult = div = 1;
162d6e349c7Sryan_chen 	} else {
163bbbfb0c5Sryan_chen 		/* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
16439283ea7Sryan_chen 		u32 m = (apll_reg >> 5) & 0x3f;
16539283ea7Sryan_chen 		u32 od = (apll_reg >> 4) & 0x1;
16639283ea7Sryan_chen 		u32 n = apll_reg & 0xf;
167d6e349c7Sryan_chen 
168bbbfb0c5Sryan_chen 		mult = (2 - od) * (m + 2);
169bbbfb0c5Sryan_chen 		div = n + 1;
170d6e349c7Sryan_chen 	}
171bbbfb0c5Sryan_chen 	return ((clkin * mult)/div);
17239283ea7Sryan_chen }
17339283ea7Sryan_chen 
174d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
17545e0908aSryan_chen 	2, 2, 3, 4,
176d812df15Sryan_chen };
177d812df15Sryan_chen 
17845e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div0_table[] = {
179e29dc694Sryan_chen 	3, 2, 3, 4,
18045e0908aSryan_chen };
18145e0908aSryan_chen 
18245e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div1_table[] = {
183e29dc694Sryan_chen 	3, 4, 6, 8,
184e29dc694Sryan_chen };
185e29dc694Sryan_chen 
186e29dc694Sryan_chen static u32 ast2600_a1_axi_ahb_default_table[] = {
187e29dc694Sryan_chen 	3, 4, 3, 4, 2, 2, 2, 2,
188d812df15Sryan_chen };
189d812df15Sryan_chen 
190d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
191d812df15Sryan_chen {
192d812df15Sryan_chen 	u32 hw_rev = readl(&scu->chip_id0);
19345e0908aSryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
194d812df15Sryan_chen 	u32 axi_div = 1;
195d812df15Sryan_chen 	u32 ahb_div = 0;
196d812df15Sryan_chen 	u32 rate = 0;
197d812df15Sryan_chen 
19845e0908aSryan_chen 	if (hw_rev & BIT(16)) {
199e5c4f4dfSryan_chen 		//ast2600a1
20045e0908aSryan_chen 		if(hwstrap1 & BIT(16)) {
201e29dc694Sryan_chen 			ast2600_a1_axi_ahb_div1_table[0] = ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 0x3];
202d812df15Sryan_chen 			axi_div = 1;
20345e0908aSryan_chen 			ahb_div = ast2600_a1_axi_ahb_div1_table[(hwstrap1 >> 11) & 0x3];
20445e0908aSryan_chen 		} else {
205e29dc694Sryan_chen 			ast2600_a1_axi_ahb_div0_table[0] = ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 0x3];
206d812df15Sryan_chen 			axi_div = 2;
20745e0908aSryan_chen 			ahb_div = ast2600_a1_axi_ahb_div0_table[(hwstrap1 >> 11) & 0x3];
20845e0908aSryan_chen 		}
20945e0908aSryan_chen 	} else {
210e5c4f4dfSryan_chen 		//ast2600a0 : fix axi = hpll / 2
21145e0908aSryan_chen 		axi_div = 2;
212d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
21345e0908aSryan_chen 	}
214d812df15Sryan_chen 
215bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
2162717883aSryan_chen 	return (rate / axi_div / ahb_div);
2172717883aSryan_chen }
2182717883aSryan_chen 
219c304f173Sryan_chen static u32 ast2600_get_bclk_rate(struct ast2600_scu *scu)
220c304f173Sryan_chen {
221c304f173Sryan_chen 	u32 rate;
222c304f173Sryan_chen 	u32 bclk_sel = (readl(&scu->clk_sel1) >> 20) & 0x7;
223c304f173Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
224c304f173Sryan_chen 
225c304f173Sryan_chen 	return (rate /((bclk_sel + 1) * 4));
226c304f173Sryan_chen }
227c304f173Sryan_chen 
2286fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = {
2292717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
2302717883aSryan_chen };
2312717883aSryan_chen 
2326fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = {
2336fa1ef3dSryan_chen 	2, 4, 6, 8, 10, 12, 14, 16,
2346fa1ef3dSryan_chen };
2356fa1ef3dSryan_chen 
2366fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu)
2372717883aSryan_chen {
2382717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
2396fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)];
240bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
2412717883aSryan_chen 
2422717883aSryan_chen 	return (rate / apb_div);
243d812df15Sryan_chen }
244d812df15Sryan_chen 
2456fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu)
2466fa1ef3dSryan_chen {
2476fa1ef3dSryan_chen 	u32 clk_sel4 = readl(&scu->clk_sel4);
2486fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)];
2496fa1ef3dSryan_chen 	u32 rate = ast2600_get_hclk(scu);
2506fa1ef3dSryan_chen 
2516fa1ef3dSryan_chen 	return (rate / apb_div);
2526fa1ef3dSryan_chen }
2536fa1ef3dSryan_chen 
2542e195992Sryan_chen static u32 ast2600_get_uxclk_in_rate(struct ast2600_scu *scu)
255d6e349c7Sryan_chen {
25627881d20Sryan_chen 	u32 clk_in = 0;
2572e195992Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel5);
258550e691bSryan_chen 
25927881d20Sryan_chen 	uxclk_sel &= 0x3;
26027881d20Sryan_chen 	switch(uxclk_sel) {
26127881d20Sryan_chen 		case 0:
26227881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
26327881d20Sryan_chen 			break;
26427881d20Sryan_chen 		case 1:
26527881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
26627881d20Sryan_chen 			break;
26727881d20Sryan_chen 		case 2:
26827881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
26927881d20Sryan_chen 			break;
27027881d20Sryan_chen 		case 3:
27127881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
27227881d20Sryan_chen 			break;
27327881d20Sryan_chen 	}
274d6e349c7Sryan_chen 
27527881d20Sryan_chen 	return clk_in;
27627881d20Sryan_chen }
27727881d20Sryan_chen 
2782e195992Sryan_chen static u32 ast2600_get_huxclk_in_rate(struct ast2600_scu *scu)
27927881d20Sryan_chen {
28027881d20Sryan_chen 	u32 clk_in = 0;
2812e195992Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel5);
28227881d20Sryan_chen 
28327881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
28427881d20Sryan_chen 	switch(huclk_sel) {
28527881d20Sryan_chen 		case 0:
28627881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
28727881d20Sryan_chen 			break;
28827881d20Sryan_chen 		case 1:
28927881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
29027881d20Sryan_chen 			break;
29127881d20Sryan_chen 		case 2:
29227881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
29327881d20Sryan_chen 			break;
29427881d20Sryan_chen 		case 3:
29527881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
29627881d20Sryan_chen 			break;
29727881d20Sryan_chen 	}
29827881d20Sryan_chen 
29927881d20Sryan_chen 	return clk_in;
30027881d20Sryan_chen }
30127881d20Sryan_chen 
3022e195992Sryan_chen static u32 ast2600_get_uart_uxclk_rate(struct ast2600_scu *scu)
30327881d20Sryan_chen {
3042e195992Sryan_chen 	u32 clk_in = ast2600_get_uxclk_in_rate(scu);
30527881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
30627881d20Sryan_chen 	unsigned int mult, div;
30727881d20Sryan_chen 
30827881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
30927881d20Sryan_chen 	u32 r = div_reg & 0xff;
31027881d20Sryan_chen 
31127881d20Sryan_chen 	mult = r;
3122e195992Sryan_chen 	div = (n * 2);
31327881d20Sryan_chen 	return (clk_in * mult)/div;
31427881d20Sryan_chen }
31527881d20Sryan_chen 
3162e195992Sryan_chen static u32 ast2600_get_uart_huxclk_rate(struct ast2600_scu *scu)
31727881d20Sryan_chen {
3182e195992Sryan_chen 	u32 clk_in = ast2600_get_huxclk_in_rate(scu);
31927881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
32027881d20Sryan_chen 
32127881d20Sryan_chen 	unsigned int mult, div;
32227881d20Sryan_chen 
32327881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
32427881d20Sryan_chen 	u32 r = div_reg & 0xff;
32527881d20Sryan_chen 
32627881d20Sryan_chen 	mult = r;
3272e195992Sryan_chen 	div = (n * 2);
32827881d20Sryan_chen 	return (clk_in * mult)/div;
32927881d20Sryan_chen }
33027881d20Sryan_chen 
331f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
332f51926eeSryan_chen {
333f51926eeSryan_chen 	u32 clkin = 0;
334f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
335f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
336f51926eeSryan_chen 
337f51926eeSryan_chen 	if(clk_sel & BIT(8)) {
338f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
339f51926eeSryan_chen 	} else {
34010069884Sryan_chen 		clkin = ast2600_get_hclk(scu);
341f51926eeSryan_chen 	}
342f51926eeSryan_chen 	div = (div + 1) << 1;
343f51926eeSryan_chen 
344f51926eeSryan_chen 	return (clkin / div);
345f51926eeSryan_chen }
346f51926eeSryan_chen 
347f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
348f51926eeSryan_chen {
349bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
350f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
351f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
352f51926eeSryan_chen 
353f51926eeSryan_chen 	div = (div + 1) << 2;
354f51926eeSryan_chen 
355f51926eeSryan_chen 	return (clkin / div);
356f51926eeSryan_chen }
357f51926eeSryan_chen 
358f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
35927881d20Sryan_chen {
36027881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
36127881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
36227881d20Sryan_chen 	ulong uart_clk = 0;
36327881d20Sryan_chen 
36427881d20Sryan_chen 	switch(uart_idx) {
36527881d20Sryan_chen 		case 1:
36627881d20Sryan_chen 		case 2:
36727881d20Sryan_chen 		case 3:
36827881d20Sryan_chen 		case 4:
36927881d20Sryan_chen 		case 6:
37027881d20Sryan_chen 			if(uart_sel & BIT(uart_idx - 1))
3712e195992Sryan_chen 				uart_clk = ast2600_get_uart_huxclk_rate(scu) ;
372550e691bSryan_chen 			else
3732e195992Sryan_chen 				uart_clk = ast2600_get_uart_uxclk_rate(scu) ;
37427881d20Sryan_chen 			break;
37527881d20Sryan_chen 		case 5: //24mhz is come form usb phy 48Mhz
37627881d20Sryan_chen 			{
37727881d20Sryan_chen 			u8 uart5_clk_sel = 0;
37827881d20Sryan_chen 			//high bit
37927881d20Sryan_chen 			if (readl(&scu->misc_ctrl1) & BIT(12))
38027881d20Sryan_chen 				uart5_clk_sel = 0x2;
38127881d20Sryan_chen 			else
38227881d20Sryan_chen 				uart5_clk_sel = 0x0;
383550e691bSryan_chen 
38427881d20Sryan_chen 			if (readl(&scu->clk_sel2) & BIT(14))
38527881d20Sryan_chen 				uart5_clk_sel |= 0x1;
386550e691bSryan_chen 
38727881d20Sryan_chen 			switch(uart5_clk_sel) {
38827881d20Sryan_chen 				case 0:
38927881d20Sryan_chen 					uart_clk = 24000000;
39027881d20Sryan_chen 					break;
39127881d20Sryan_chen 				case 1:
392def99fcbSryan_chen 					uart_clk = 192000000;
39327881d20Sryan_chen 					break;
39427881d20Sryan_chen 				case 2:
39527881d20Sryan_chen 					uart_clk = 24000000/13;
39627881d20Sryan_chen 					break;
39727881d20Sryan_chen 				case 3:
39827881d20Sryan_chen 					uart_clk = 192000000/13;
39927881d20Sryan_chen 					break;
40027881d20Sryan_chen 			}
40127881d20Sryan_chen 			}
40227881d20Sryan_chen 			break;
40327881d20Sryan_chen 		case 7:
40427881d20Sryan_chen 		case 8:
40527881d20Sryan_chen 		case 9:
40627881d20Sryan_chen 		case 10:
40727881d20Sryan_chen 		case 11:
40827881d20Sryan_chen 		case 12:
40927881d20Sryan_chen 		case 13:
41027881d20Sryan_chen 			if(uart_sel5 & BIT(uart_idx - 1))
4112e195992Sryan_chen 				uart_clk = ast2600_get_uart_huxclk_rate(scu);
41227881d20Sryan_chen 			else
4132e195992Sryan_chen 				uart_clk = ast2600_get_uart_uxclk_rate(scu);
41427881d20Sryan_chen 			break;
41527881d20Sryan_chen 	}
41627881d20Sryan_chen 
41727881d20Sryan_chen 	return uart_clk;
418550e691bSryan_chen }
419550e691bSryan_chen 
420feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
421feb42054Sryan_chen {
422feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
423feb42054Sryan_chen 	ulong rate = 0;
424feb42054Sryan_chen 
425feb42054Sryan_chen 	switch (clk->id) {
426feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
427bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
428bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
429d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
430bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
431d812df15Sryan_chen 		break;
432feb42054Sryan_chen 	case ASPEED_CLK_AHB:
433feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
434feb42054Sryan_chen 		break;
4356fa1ef3dSryan_chen 	case ASPEED_CLK_APB1:
4366fa1ef3dSryan_chen 		rate = ast2600_get_pclk1(priv->scu);
4376fa1ef3dSryan_chen 		break;
4386fa1ef3dSryan_chen 	case ASPEED_CLK_APB2:
4396fa1ef3dSryan_chen 		rate = ast2600_get_pclk2(priv->scu);
440feb42054Sryan_chen 		break;
441bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
442bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
443bbbfb0c5Sryan_chen 		break;
444feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
445feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
446feb42054Sryan_chen 		break;
447feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
448feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
449feb42054Sryan_chen 		break;
450feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
451feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
452feb42054Sryan_chen 		break;
453feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
454feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
455feb42054Sryan_chen 		break;
456feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
457feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
458feb42054Sryan_chen 		break;
459c304f173Sryan_chen 	case ASPEED_CLK_BCLK:
460c304f173Sryan_chen 		rate = ast2600_get_bclk_rate(priv->scu);
461c304f173Sryan_chen 		break;
462f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
463f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
464f51926eeSryan_chen 		break;
465f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
466f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
467f51926eeSryan_chen 		break;
4682e195992Sryan_chen 	case ASPEED_CLK_UARTX:
4692e195992Sryan_chen 		rate = ast2600_get_uart_uxclk_rate(priv->scu);
4702e195992Sryan_chen 		break;
4710998ddefSryan_chen 	case ASPEED_CLK_HUARTX:
4722e195992Sryan_chen 		rate = ast2600_get_uart_huxclk_rate(priv->scu);
4732e195992Sryan_chen 		break;
474feb42054Sryan_chen 	default:
475d812df15Sryan_chen 		pr_debug("can't get clk rate \n");
476feb42054Sryan_chen 		return -ENOENT;
477d812df15Sryan_chen 		break;
478feb42054Sryan_chen 	}
479feb42054Sryan_chen 
480feb42054Sryan_chen 	return rate;
481feb42054Sryan_chen }
482feb42054Sryan_chen 
483577fcdaeSDylan Hung /**
484577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
485577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
486577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
487550e691bSryan_chen  *
488577fcdaeSDylan Hung  * The function caller shall fill "pll->in" and "pll->out", then this function
489577fcdaeSDylan Hung  * will search the lookup table to find a valid PLL divider configuration.
490550e691bSryan_chen  */
491577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
492550e691bSryan_chen {
493577fcdaeSDylan Hung 	u32 i;
494577fcdaeSDylan Hung 	bool is_found = false;
495550e691bSryan_chen 
496577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
497577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
498577fcdaeSDylan Hung 		if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) {
499577fcdaeSDylan Hung 			is_found = true;
500577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
501577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
502577fcdaeSDylan Hung 			break;
503550e691bSryan_chen 		}
504550e691bSryan_chen 	}
505577fcdaeSDylan Hung 	return is_found;
506550e691bSryan_chen }
507fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu,
508fd52be0bSDylan Hung 				 struct ast2600_pll_cfg *p_cfg, int pll_idx)
509fd52be0bSDylan Hung {
510fd52be0bSDylan Hung 	u32 addr, addr_ext;
511fd52be0bSDylan Hung 	u32 reg;
512550e691bSryan_chen 
513fd52be0bSDylan Hung 	switch (pll_idx) {
514fd52be0bSDylan Hung 	case ASPEED_CLK_HPLL:
515fd52be0bSDylan Hung 		addr = (u32)(&scu->h_pll_param);
516fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->h_pll_ext_param);
517fd52be0bSDylan Hung 		break;
518fd52be0bSDylan Hung 	case ASPEED_CLK_MPLL:
519fd52be0bSDylan Hung 		addr = (u32)(&scu->m_pll_param);
520fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->m_pll_ext_param);
521fd52be0bSDylan Hung 		break;
522fd52be0bSDylan Hung 	case ASPEED_CLK_DPLL:
523fd52be0bSDylan Hung 		addr = (u32)(&scu->d_pll_param);
524fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->d_pll_ext_param);
525fd52be0bSDylan Hung 		break;
526fd52be0bSDylan Hung 	case ASPEED_CLK_EPLL:
527fd52be0bSDylan Hung 		addr = (u32)(&scu->e_pll_param);
528fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->e_pll_ext_param);
529fd52be0bSDylan Hung 		break;
530fd52be0bSDylan Hung 	default:
531fd52be0bSDylan Hung 		debug("unknown PLL index\n");
532fd52be0bSDylan Hung 		return 1;
533fd52be0bSDylan Hung 	}
534fd52be0bSDylan Hung 
535fd52be0bSDylan Hung 	p_cfg->reg.b.bypass = 0;
536fd52be0bSDylan Hung 	p_cfg->reg.b.off = 1;
537fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 1;
538fd52be0bSDylan Hung 
539fd52be0bSDylan Hung 	reg = readl(addr);
540fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
541fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
542fd52be0bSDylan Hung 	writel(reg, addr);
543fd52be0bSDylan Hung 
544fd52be0bSDylan Hung 	/* write extend parameter */
545fd52be0bSDylan Hung 	writel(p_cfg->ext_reg, addr_ext);
546fd52be0bSDylan Hung 	udelay(100);
547fd52be0bSDylan Hung 	p_cfg->reg.b.off = 0;
548fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 0;
549fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
550fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
551fd52be0bSDylan Hung 	writel(reg, addr);
552fd52be0bSDylan Hung 
553fd52be0bSDylan Hung 	/* polling PLL lock status */
554fd52be0bSDylan Hung 	while(0 == (readl(addr_ext) & BIT(31)));
555fd52be0bSDylan Hung 
556fd52be0bSDylan Hung 	return 0;
557fd52be0bSDylan Hung }
558feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
559550e691bSryan_chen {
560577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
561550e691bSryan_chen 
562577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
563577fcdaeSDylan Hung 	mpll.out = rate;
564577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&mpll)) {
565577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
566577fcdaeSDylan Hung 		return 0;
567577fcdaeSDylan Hung 	}
568fd52be0bSDylan Hung 	ast2600_configure_pll(scu, &(mpll.cfg), ASPEED_CLK_MPLL);
569577fcdaeSDylan Hung 
570cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
571d6e349c7Sryan_chen }
572d6e349c7Sryan_chen 
573d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
574550e691bSryan_chen {
575f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
576550e691bSryan_chen 
577550e691bSryan_chen 	ulong new_rate;
578550e691bSryan_chen 	switch (clk->id) {
579f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
580feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
581550e691bSryan_chen 		break;
582550e691bSryan_chen 	default:
583550e691bSryan_chen 		return -ENOENT;
584550e691bSryan_chen 	}
585550e691bSryan_chen 
586550e691bSryan_chen 	return new_rate;
587550e691bSryan_chen }
588feb42054Sryan_chen 
589f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
590f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
591f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
592f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
593f9aa0ee1Sryan_chen 
594cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu)
595cc476ffcSDylan Hung {
596eff28274SJohnny Huang 	/* scu340[25:0]: 1G default delay */
597eff28274SJohnny Huang 	clrsetbits_le32(&scu->mac12_clk_delay, GENMASK(25, 0),
598eff28274SJohnny Huang 			MAC_DEF_DELAY_1G);
5994760b3f8SDylan Hung 
6004760b3f8SDylan Hung 	/* set 100M/10M default delay */
6014760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M);
6024760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M);
603cc476ffcSDylan Hung 
604ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
605eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16));
606894c19cfSDylan Hung 
607cc476ffcSDylan Hung 	return 0;
608cc476ffcSDylan Hung }
609cc476ffcSDylan Hung 
61054f9cba1SDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu)
61154f9cba1SDylan Hung {
61254f9cba1SDylan Hung 
61354f9cba1SDylan Hung 	/*
614eff28274SJohnny Huang 	 * scu350[31]   RGMII 125M source: 0 = from IO pin
615eff28274SJohnny Huang 	 * scu350[25:0] MAC 1G delay
61654f9cba1SDylan Hung 	 */
617eff28274SJohnny Huang 	clrsetbits_le32(&scu->mac34_clk_delay, (BIT(31) | GENMASK(25, 0)),
618eff28274SJohnny Huang 			MAC34_DEF_DELAY_1G);
61954f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M);
62054f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M);
62154f9cba1SDylan Hung 
622eff28274SJohnny Huang 	/*
623eff28274SJohnny Huang 	 * clock source seletion and divider
624eff28274SJohnny Huang 	 * scu310[26:24] : MAC AHB bus clock = HCLK / 2
625eff28274SJohnny Huang 	 * scu310[18:16] : RMII 50M = HCLK_200M / 4
626eff28274SJohnny Huang 	 */
627eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)),
628eff28274SJohnny Huang 			((0x0 << 24) | (0x3 << 16)));
62954f9cba1SDylan Hung 
630eff28274SJohnny Huang 	/*
631eff28274SJohnny Huang 	 * set driving strength
632eff28274SJohnny Huang 	 * scu458[3:2] : MAC4 driving strength
633eff28274SJohnny Huang 	 * scu458[1:0] : MAC3 driving strength
634eff28274SJohnny Huang 	 */
635eff28274SJohnny Huang 	clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0),
636a961159eSDylan Hung 			(0x3 << 2) | (0x3 << 0));
63754f9cba1SDylan Hung 
63854f9cba1SDylan Hung 	return 0;
63954f9cba1SDylan Hung }
640eff28274SJohnny Huang 
64154f9cba1SDylan Hung /**
6425b5c3d44SDylan Hung  * ast2600 RGMII clock source tree
64354f9cba1SDylan Hung  *
64454f9cba1SDylan Hung  *    125M from external PAD -------->|\
64554f9cba1SDylan Hung  *    HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
64654f9cba1SDylan Hung  *            | |---->| divider |---->|/                             +
64754f9cba1SDylan Hung  *    EPLL -->|/                                                     |
64854f9cba1SDylan Hung  *                                                                   |
649eff28274SJohnny Huang  *    +---------<-----------|RGMIICK PAD output enable|<-------------+
65054f9cba1SDylan Hung  *    |
651eff28274SJohnny Huang  *    +--------------------------->|\
65254f9cba1SDylan Hung  *                                 | |----> RGMII 125M for MAC#3 & MAC#4
653eff28274SJohnny Huang  *    HCLK 200M ---->|divider|---->|/
6545b5c3d44SDylan Hung  *
655eff28274SJohnny Huang  * To simplify the control flow:
656eff28274SJohnny Huang  * 	1. RGMII 1/2 always use EPLL as the internal clock source
657eff28274SJohnny Huang  * 	2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source
6585b5c3d44SDylan Hung  *
659eff28274SJohnny Huang  *    125M from external PAD -------->|\
660eff28274SJohnny Huang  *                                    | |---->RGMII 125M for MAC#1 & MAC#2
661eff28274SJohnny Huang  *            EPLL---->| divider |--->|/                             +
662eff28274SJohnny Huang  *                                                                   |
663eff28274SJohnny Huang  *    +<--------------------|RGMIICK PAD output enable|<-------------+
664eff28274SJohnny Huang  *    |
665eff28274SJohnny Huang  *    +--------------------------->RGMII 125M for MAC#3 & MAC#4
666eff28274SJohnny Huang */
667eff28274SJohnny Huang #define RGMIICK_SRC_PAD			0
668eff28274SJohnny Huang #define RGMIICK_SRC_EPLL		1	/* recommended */
669eff28274SJohnny Huang #define RGMIICK_SRC_HPLL		2
670eff28274SJohnny Huang 
671eff28274SJohnny Huang #define RGMIICK_DIV2			1
672eff28274SJohnny Huang #define RGMIICK_DIV3			2
673eff28274SJohnny Huang #define RGMIICK_DIV4			3
674eff28274SJohnny Huang #define RGMIICK_DIV5			4
675eff28274SJohnny Huang #define RGMIICK_DIV6			5
676eff28274SJohnny Huang #define RGMIICK_DIV7			6
677eff28274SJohnny Huang #define RGMIICK_DIV8			7	/* recommended */
678eff28274SJohnny Huang 
679eff28274SJohnny Huang #define RMIICK_DIV4			0
680eff28274SJohnny Huang #define RMIICK_DIV8			1
681eff28274SJohnny Huang #define RMIICK_DIV12			2
682eff28274SJohnny Huang #define RMIICK_DIV16			3
683eff28274SJohnny Huang #define RMIICK_DIV20			4	/* recommended */
684eff28274SJohnny Huang #define RMIICK_DIV24			5
685eff28274SJohnny Huang #define RMIICK_DIV28			6
686eff28274SJohnny Huang #define RMIICK_DIV32			7
687eff28274SJohnny Huang 
688eff28274SJohnny Huang struct ast2600_mac_clk_div {
689eff28274SJohnny Huang 	u32 src;	/* 0=external PAD, 1=internal PLL */
690eff28274SJohnny Huang 	u32 fin;	/* divider input speed */
691eff28274SJohnny Huang 	u32 n;		/* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */
692eff28274SJohnny Huang 	u32 fout;	/* fout = fin / n */
693eff28274SJohnny Huang };
694eff28274SJohnny Huang 
695eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = {
696eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
697eff28274SJohnny Huang 	.fin = 1000000000,
698eff28274SJohnny Huang 	.n = RGMIICK_DIV8,
699eff28274SJohnny Huang 	.fout = 125000000,
700eff28274SJohnny Huang };
701eff28274SJohnny Huang 
702eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = {
703eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
704eff28274SJohnny Huang 	.fin = 1000000000,
705eff28274SJohnny Huang 	.n = RMIICK_DIV20,
706eff28274SJohnny Huang 	.fout = 50000000,
707eff28274SJohnny Huang };
708eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu,
709eff28274SJohnny Huang 				 struct ast2600_mac_clk_div *p_cfg)
710eff28274SJohnny Huang {
711eff28274SJohnny Huang 	struct ast2600_pll_desc pll;
712eff28274SJohnny Huang 
713eff28274SJohnny Huang 	pll.in = AST2600_CLK_IN;
714eff28274SJohnny Huang 	pll.out = p_cfg->fin;
715eff28274SJohnny Huang 	if (false == ast2600_search_clock_config(&pll)) {
716eff28274SJohnny Huang 		printf("error!! unable to find valid ETHNET MAC clock "
717eff28274SJohnny Huang 		       "setting\n");
718eff28274SJohnny Huang 		debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w,
719eff28274SJohnny Huang 		      pll.cfg.ext_reg);
720eff28274SJohnny Huang 		debug("%s: pll cfg = %02x %02x %02x\n", __func__,
721eff28274SJohnny Huang 		      pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p);
722eff28274SJohnny Huang 		return;
723eff28274SJohnny Huang 	}
724eff28274SJohnny Huang 	ast2600_configure_pll(p_scu, &(pll.cfg), p_cfg->src);
725eff28274SJohnny Huang }
726eff28274SJohnny Huang 
727eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu,
728eff28274SJohnny Huang 				   struct ast2600_mac_clk_div *p_cfg)
729eff28274SJohnny Huang {
730eff28274SJohnny Huang 	u32 reg_304 = readl(&p_scu->clk_sel2);
731eff28274SJohnny Huang 	u32 reg_340 = readl(&p_scu->mac12_clk_delay);
732eff28274SJohnny Huang 	u32 reg_350 = readl(&p_scu->mac34_clk_delay);
733eff28274SJohnny Huang 
734eff28274SJohnny Huang 	reg_340 &= ~GENMASK(31, 29);
735eff28274SJohnny Huang 	/* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */
736eff28274SJohnny Huang 	reg_340 |= BIT(28);
737eff28274SJohnny Huang 	if ((p_cfg->src == ASPEED_CLK_EPLL) ||
738eff28274SJohnny Huang 	    (p_cfg->src == ASPEED_CLK_HPLL)) {
739eff28274SJohnny Huang 		/*
740eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
741eff28274SJohnny Huang 		 * the divider setting
742eff28274SJohnny Huang 		 */
743eff28274SJohnny Huang 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) {
744eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
745eff28274SJohnny Huang 		}
746eff28274SJohnny Huang 		/* scu340[31]: select RGMII 125M from internal source */
747eff28274SJohnny Huang 		reg_340 |= BIT(31);
748eff28274SJohnny Huang 	}
749eff28274SJohnny Huang 
750eff28274SJohnny Huang 	reg_304 &= ~GENMASK(23, 20);
751eff28274SJohnny Huang 
752eff28274SJohnny Huang 	/* set clock divider */
753eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 20;
754eff28274SJohnny Huang 
755eff28274SJohnny Huang 	/* select internal clock source */
756eff28274SJohnny Huang 	if (ASPEED_CLK_HPLL == p_cfg->src) {
757eff28274SJohnny Huang 		reg_304 |= BIT(23);
758eff28274SJohnny Huang 	}
759eff28274SJohnny Huang 
760eff28274SJohnny Huang 	/* RGMII 3/4 clock source select */
761eff28274SJohnny Huang 	reg_350 &= ~BIT(31);
762eff28274SJohnny Huang #if 0
763eff28274SJohnny Huang 	if (RGMII_3_4_CLK_SRC_HCLK == p_cfg->rgmii_3_4_clk_src) {
764eff28274SJohnny Huang 		reg_350 |= BIT(31);
765eff28274SJohnny Huang 	}
766eff28274SJohnny Huang 
767eff28274SJohnny Huang 	/* set clock divider */
768eff28274SJohnny Huang 	reg_310 &= ~GENMASK(22, 20);
769eff28274SJohnny Huang 	reg_310 |= (p_cfg->hclk_clk_div & 0x7) << 20;
770eff28274SJohnny Huang #endif
771eff28274SJohnny Huang 
772eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
773eff28274SJohnny Huang 	writel(reg_340, &p_scu->mac12_clk_delay);
774eff28274SJohnny Huang 	writel(reg_350, &p_scu->mac34_clk_delay);
775eff28274SJohnny Huang }
776eff28274SJohnny Huang 
777eff28274SJohnny Huang /**
7785b5c3d44SDylan Hung  * ast2600 RMII/NCSI clock source tree
7795b5c3d44SDylan Hung  *
7805b5c3d44SDylan Hung  *    HPLL -->|\
7815b5c3d44SDylan Hung  *            | |---->| divider |----> RMII 50M for MAC#1 & MAC#2
7825b5c3d44SDylan Hung  *    EPLL -->|/
7835b5c3d44SDylan Hung  *
7845b5c3d44SDylan Hung  *    HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4
78554f9cba1SDylan Hung */
786eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu,
787eff28274SJohnny Huang 				  struct ast2600_mac_clk_div *p_cfg)
78854f9cba1SDylan Hung {
789eff28274SJohnny Huang 	u32 reg_304;
790eff28274SJohnny Huang 	u32 reg_310;
791eff28274SJohnny Huang 
792eff28274SJohnny Huang 	if ((p_cfg->src == ASPEED_CLK_EPLL) ||
793eff28274SJohnny Huang 	    (p_cfg->src == ASPEED_CLK_HPLL)) {
794eff28274SJohnny Huang 		/*
795eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
796eff28274SJohnny Huang 		 * the divider setting
797eff28274SJohnny Huang 		 */
798eff28274SJohnny Huang 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) {
799eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
800eff28274SJohnny Huang 		}
80154f9cba1SDylan Hung 	}
80254f9cba1SDylan Hung 
803eff28274SJohnny Huang 	reg_304 = readl(&p_scu->clk_sel2);
804eff28274SJohnny Huang 	reg_310 = readl(&p_scu->clk_sel4);
805eff28274SJohnny Huang 
806eff28274SJohnny Huang 	reg_304 &= ~GENMASK(19, 16);
807eff28274SJohnny Huang 
808eff28274SJohnny Huang 	/* set RMII 1/2 clock divider */
809eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 16;
810eff28274SJohnny Huang 
811eff28274SJohnny Huang 	/* RMII clock source selection */
812eff28274SJohnny Huang 	if (ASPEED_CLK_HPLL == p_cfg->src) {
813eff28274SJohnny Huang 		reg_304 |= BIT(19);
81454f9cba1SDylan Hung 	}
815eff28274SJohnny Huang 
816eff28274SJohnny Huang 	/* set RMII 3/4 clock divider */
817eff28274SJohnny Huang 	reg_310 &= ~GENMASK(18, 16);
818eff28274SJohnny Huang 	reg_310 |= (0x3 << 16);
819eff28274SJohnny Huang 
820eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
821eff28274SJohnny Huang 	writel(reg_310, &p_scu->clk_sel4);
822eff28274SJohnny Huang }
823eff28274SJohnny Huang 
824f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
825f9aa0ee1Sryan_chen {
826f9aa0ee1Sryan_chen 	u32 reset_bit;
827f9aa0ee1Sryan_chen 	u32 clkstop_bit;
828f9aa0ee1Sryan_chen 
829f9aa0ee1Sryan_chen 	switch (index) {
830f9aa0ee1Sryan_chen 	case 1:
831f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
832f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
833f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
834f9aa0ee1Sryan_chen 		udelay(100);
835f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
836f9aa0ee1Sryan_chen 		mdelay(10);
837f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
838f9aa0ee1Sryan_chen 
839f9aa0ee1Sryan_chen 		break;
840f9aa0ee1Sryan_chen 	case 2:
841f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
842f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
843f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
844f9aa0ee1Sryan_chen 		udelay(100);
845f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
846f9aa0ee1Sryan_chen 		mdelay(10);
847f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
848f9aa0ee1Sryan_chen 		break;
849f9aa0ee1Sryan_chen 	case 3:
850f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
851f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
852f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
853f9aa0ee1Sryan_chen 		udelay(100);
854f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
855f9aa0ee1Sryan_chen 		mdelay(10);
856f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
857f9aa0ee1Sryan_chen 		break;
858f9aa0ee1Sryan_chen 	case 4:
859f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
860f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
861f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
862f9aa0ee1Sryan_chen 		udelay(100);
863f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
864f9aa0ee1Sryan_chen 		mdelay(10);
865f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
866f9aa0ee1Sryan_chen 		break;
867f9aa0ee1Sryan_chen 	default:
868f9aa0ee1Sryan_chen 		return -EINVAL;
869f9aa0ee1Sryan_chen 	}
870f9aa0ee1Sryan_chen 
871f9aa0ee1Sryan_chen 	return 0;
872f9aa0ee1Sryan_chen }
873550e691bSryan_chen 
874f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
875f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
876f51926eeSryan_chen {
877f51926eeSryan_chen 	u32 reset_bit;
878f51926eeSryan_chen 	u32 clkstop_bit;
879f51926eeSryan_chen 
880f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
881f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
882f51926eeSryan_chen 
883fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
884fc9f12e6Sryan_chen 
885f51926eeSryan_chen 	udelay(100);
886f51926eeSryan_chen 	//enable clk
887f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
888f51926eeSryan_chen 	mdelay(10);
889fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
890f51926eeSryan_chen 
891f51926eeSryan_chen 	return 0;
892f51926eeSryan_chen }
893f51926eeSryan_chen 
894f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31
895f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
896f51926eeSryan_chen #define SCU_CLK_SD_DIV(x)			(x << 28)
8972cd7cba2Sryan_chen #define SCU_CLK_SD_FROM_APLL_CLK	BIT(8)
898f51926eeSryan_chen 
899f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
900f51926eeSryan_chen {
901f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
902f51926eeSryan_chen 	u32 enableclk_bit;
9032cd7cba2Sryan_chen 	u32 rate = 0;
9042cd7cba2Sryan_chen 	u32 div = 0;
9052cd7cba2Sryan_chen 	int i = 0;
906f51926eeSryan_chen 
907f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
908f51926eeSryan_chen 
9092cd7cba2Sryan_chen 	//ast2600 sd controller max clk is 200Mhz : use apll for clock source 800/4 = 200 : controller max is 200mhz
9102cd7cba2Sryan_chen 	rate = ast2600_get_apll_rate(scu);
9112cd7cba2Sryan_chen 	for(i = 0; i < 8; i++) {
9122cd7cba2Sryan_chen 		div = (i + 1) * 2;
9132cd7cba2Sryan_chen 		if ((rate / div) <= 200000000)
9142cd7cba2Sryan_chen 			break;
9152cd7cba2Sryan_chen 	}
916f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
9172cd7cba2Sryan_chen 	clk_sel |= SCU_CLK_SD_DIV(i) | SCU_CLK_SD_FROM_APLL_CLK;
918f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
919f51926eeSryan_chen 
920f51926eeSryan_chen 	//enable clk
921f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
922f51926eeSryan_chen 
923f51926eeSryan_chen 	return 0;
924f51926eeSryan_chen }
925f51926eeSryan_chen 
926f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
927f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
928f51926eeSryan_chen {
929f51926eeSryan_chen 	u32 reset_bit;
930f51926eeSryan_chen 	u32 clkstop_bit;
931f51926eeSryan_chen 
932f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
933f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
934f51926eeSryan_chen 
935fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
936f51926eeSryan_chen 	udelay(100);
937f51926eeSryan_chen 	//enable clk
938f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
939f51926eeSryan_chen 	mdelay(10);
940fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
941f51926eeSryan_chen 
942f51926eeSryan_chen 	return 0;
943f51926eeSryan_chen }
944f51926eeSryan_chen 
945f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15
946f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
947f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x)			(x << 12)
9482cd7cba2Sryan_chen #define SCU_CLK_EMMC_FROM_MPLL_CLK	BIT(11)	//AST2600A1
949f51926eeSryan_chen 
950f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
951f51926eeSryan_chen {
952b0c30ea3Sryan_chen 	u32 revision_id = readl(&scu->chip_id0);
953f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
954f51926eeSryan_chen 	u32 enableclk_bit;
955f4c4ddb1Sryan_chen 	u32 rate = 0;
956f4c4ddb1Sryan_chen 	u32 div = 0;
957f4c4ddb1Sryan_chen 	int i = 0;
958f51926eeSryan_chen 
959d0bdd5f3Sryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC);
960f51926eeSryan_chen 
9612cd7cba2Sryan_chen 	//ast2600 eMMC controller max clk is 200Mhz
962*8c32294fSryan_chen 	/*********************************************************************************************
963*8c32294fSryan_chen 		HPll -> 1/2 --
964*8c32294fSryan_chen 						\
965*8c32294fSryan_chen 						--> SCU300[11] -> SCU300[14:12] [1/N] -> EMMC12C[15:8] [1/N] --> eMMC clk
966*8c32294fSryan_chen 						/
967*8c32294fSryan_chen 		MPLL - ----- ->
968*8c32294fSryan_chen 	*********************************************************************************************/
9692cd7cba2Sryan_chen 	if(((revision_id & GENMASK(23, 16)) >> 16)) {
970*8c32294fSryan_chen 		//AST2600A1 : use mpll to be clk source
971b0c30ea3Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
972b0c30ea3Sryan_chen 		for(i = 0; i < 8; i++) {
973b0c30ea3Sryan_chen 			div = (i + 1) * 2;
974b0c30ea3Sryan_chen 			if ((rate / div) <= 200000000)
975b0c30ea3Sryan_chen 				break;
976b0c30ea3Sryan_chen 		}
977b0c30ea3Sryan_chen 
978b0c30ea3Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
9792cd7cba2Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i) | SCU_CLK_EMMC_FROM_MPLL_CLK;
980b0c30ea3Sryan_chen 		writel(clk_sel, &scu->clk_sel1);
981b0c30ea3Sryan_chen 
982b0c30ea3Sryan_chen 	} else {
9832cd7cba2Sryan_chen 		//AST2600A0 : use hpll to be clk source
984f4c4ddb1Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
985f4c4ddb1Sryan_chen 
986f4c4ddb1Sryan_chen 		for(i = 0; i < 8; i++) {
987f4c4ddb1Sryan_chen 			div = (i + 1) * 4;
988f4c4ddb1Sryan_chen 			if ((rate / div) <= 200000000)
989f4c4ddb1Sryan_chen 				break;
990f4c4ddb1Sryan_chen 		}
991f4c4ddb1Sryan_chen 
992f4c4ddb1Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
993f4c4ddb1Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i);
994f51926eeSryan_chen 		writel(clk_sel, &scu->clk_sel1);
995b0c30ea3Sryan_chen 	}
996f51926eeSryan_chen 
997f51926eeSryan_chen 	//enable clk
998f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
999f51926eeSryan_chen 
1000f51926eeSryan_chen 	return 0;
1001f51926eeSryan_chen }
1002f51926eeSryan_chen 
1003baf00c26Sryan_chen #define SCU_CLKSTOP_FSICLK 30
1004baf00c26Sryan_chen 
1005baf00c26Sryan_chen static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu)
1006baf00c26Sryan_chen {
1007baf00c26Sryan_chen 	u32 reset_bit;
1008baf00c26Sryan_chen 	u32 clkstop_bit;
1009baf00c26Sryan_chen 
1010baf00c26Sryan_chen 	reset_bit = BIT(ASPEED_RESET_FSI % 32);
1011baf00c26Sryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_FSICLK);
1012baf00c26Sryan_chen 
1013baf00c26Sryan_chen 	/* The FSI clock is shared between masters. If it's already on
1014baf00c26Sryan_chen 	 * don't touch it, as that will reset the existing master. */
1015baf00c26Sryan_chen 	if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) {
1016baf00c26Sryan_chen 		debug("%s: already running, not touching it\n", __func__);
1017baf00c26Sryan_chen 		return 0;
1018baf00c26Sryan_chen 	}
1019baf00c26Sryan_chen 
1020baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
1021baf00c26Sryan_chen 	udelay(100);
1022baf00c26Sryan_chen 	//enable clk
1023baf00c26Sryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1024baf00c26Sryan_chen 	mdelay(10);
1025baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1026baf00c26Sryan_chen 
1027baf00c26Sryan_chen 	return 0;
1028baf00c26Sryan_chen }
1029baf00c26Sryan_chen 
1030b8ec5ceaSryan_chen static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu)
1031b8ec5ceaSryan_chen {
1032b8ec5ceaSryan_chen 	u32 reset_bit;
1033b8ec5ceaSryan_chen 	u32 clkstop_bit;
1034b8ec5ceaSryan_chen 
1035b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P1);
1036b8ec5ceaSryan_chen 	clkstop_bit = BIT(14);
1037b8ec5ceaSryan_chen 
1038b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1039b8ec5ceaSryan_chen 		udelay(100);
1040b8ec5ceaSryan_chen 	//enable phy clk
1041b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_ctrl1);
1042b8ec5ceaSryan_chen 	mdelay(20);
1043b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1044b8ec5ceaSryan_chen 
1045b8ec5ceaSryan_chen 	return 0;
1046b8ec5ceaSryan_chen }
1047b8ec5ceaSryan_chen 
1048b8ec5ceaSryan_chen static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu)
1049b8ec5ceaSryan_chen {
1050b8ec5ceaSryan_chen 	u32 reset_bit;
1051b8ec5ceaSryan_chen 	u32 clkstop_bit;
1052b8ec5ceaSryan_chen 
1053b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P2);
1054b8ec5ceaSryan_chen 	clkstop_bit = BIT(7);
1055b8ec5ceaSryan_chen 
1056b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1057b8ec5ceaSryan_chen 			udelay(100);
1058b8ec5ceaSryan_chen 	//enable phy clk
1059b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1060b8ec5ceaSryan_chen 	mdelay(20);
1061b8ec5ceaSryan_chen 
1062b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1063b8ec5ceaSryan_chen 
1064b8ec5ceaSryan_chen 	return 0;
1065b8ec5ceaSryan_chen }
1066b8ec5ceaSryan_chen 
1067d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
1068550e691bSryan_chen {
1069f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
1070550e691bSryan_chen 
1071550e691bSryan_chen 	switch (clk->id) {
107286f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC1CLK:
107386f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 1);
1074550e691bSryan_chen 			break;
107586f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC2CLK:
107686f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 2);
1077550e691bSryan_chen 			break;
107877843939Sryan_chen 		case ASPEED_CLK_GATE_MAC3CLK:
107977843939Sryan_chen 			ast2600_configure_mac(priv->scu, 3);
108077843939Sryan_chen 			break;
108177843939Sryan_chen 		case ASPEED_CLK_GATE_MAC4CLK:
108277843939Sryan_chen 			ast2600_configure_mac(priv->scu, 4);
108377843939Sryan_chen 			break;
1084f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDCLK:
1085f51926eeSryan_chen 			ast2600_enable_sdclk(priv->scu);
1086f51926eeSryan_chen 			break;
1087f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDEXTCLK:
1088f51926eeSryan_chen 			ast2600_enable_extsdclk(priv->scu);
1089f51926eeSryan_chen 			break;
1090f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCCLK:
1091f51926eeSryan_chen 			ast2600_enable_emmcclk(priv->scu);
1092f51926eeSryan_chen 			break;
1093f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCEXTCLK:
1094f51926eeSryan_chen 			ast2600_enable_extemmcclk(priv->scu);
1095f51926eeSryan_chen 			break;
1096baf00c26Sryan_chen 		case ASPEED_CLK_GATE_FSICLK:
1097baf00c26Sryan_chen 			ast2600_enable_fsiclk(priv->scu);
1098baf00c26Sryan_chen 			break;
1099b8ec5ceaSryan_chen 		case ASPEED_CLK_GATE_USBPORT1CLK:
1100b8ec5ceaSryan_chen 			ast2600_enable_usbahclk(priv->scu);
1101b8ec5ceaSryan_chen 			break;
1102b8ec5ceaSryan_chen 		case ASPEED_CLK_GATE_USBPORT2CLK:
1103b8ec5ceaSryan_chen 			ast2600_enable_usbbhclk(priv->scu);
1104b8ec5ceaSryan_chen 			break;
1105550e691bSryan_chen 		default:
1106f9aa0ee1Sryan_chen 			pr_debug("can't enable clk \n");
1107550e691bSryan_chen 			return -ENOENT;
110877843939Sryan_chen 			break;
1109550e691bSryan_chen 	}
1110550e691bSryan_chen 
1111550e691bSryan_chen 	return 0;
1112550e691bSryan_chen }
1113550e691bSryan_chen 
1114f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
1115d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
1116d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
1117d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
1118550e691bSryan_chen };
1119550e691bSryan_chen 
1120d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
1121550e691bSryan_chen {
1122f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
112361ab9607Sryan_chen 	u32 uart_clk_source;
1124550e691bSryan_chen 
1125f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
1126f0d895afSryan_chen 	if (IS_ERR(priv->scu))
1127f0d895afSryan_chen 		return PTR_ERR(priv->scu);
1128550e691bSryan_chen 
1129b55086a6SChia-Wei, Wang 	uart_clk_source = dev_read_u32_default(dev, "uart-clk-source",
113061ab9607Sryan_chen 					    0x0);
113161ab9607Sryan_chen 
113261ab9607Sryan_chen 	if(uart_clk_source) {
113356dd3e85Sryan_chen 		if(uart_clk_source & GENMASK(5, 0))
113456dd3e85Sryan_chen 			setbits_le32(&priv->scu->clk_sel4, uart_clk_source & GENMASK(5, 0));
113556dd3e85Sryan_chen 		if(uart_clk_source & GENMASK(12, 6))
113656dd3e85Sryan_chen 			setbits_le32(&priv->scu->clk_sel5, uart_clk_source & GENMASK(12, 6));
113761ab9607Sryan_chen 	}
113861ab9607Sryan_chen 
1139b89500a2SDylan Hung 
1140b89500a2SDylan Hung 	ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig);
1141b89500a2SDylan Hung 	ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig);
1142b89500a2SDylan Hung 	ast2600_configure_mac12_clk(priv->scu);
1143b89500a2SDylan Hung 	ast2600_configure_mac34_clk(priv->scu);
1144b89500a2SDylan Hung 
1145fd0306aaSJohnny Huang 	/* RSA clock = HPLL/3 */
1146fd0306aaSJohnny Huang 	setbits_le32(&priv->scu->clk_sel1, BIT(19));
1147fd0306aaSJohnny Huang 	setbits_le32(&priv->scu->clk_sel1, BIT(27));
1148fd0306aaSJohnny Huang 
1149550e691bSryan_chen 	return 0;
1150550e691bSryan_chen }
1151550e691bSryan_chen 
1152d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
1153550e691bSryan_chen {
1154550e691bSryan_chen 	int ret;
1155550e691bSryan_chen 
1156550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
1157550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
1158550e691bSryan_chen 	if (ret)
1159550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
1160550e691bSryan_chen 
1161550e691bSryan_chen 	return 0;
1162550e691bSryan_chen }
1163550e691bSryan_chen 
1164d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK)
1165d35ac78cSryan_chen struct aspeed_clks {
1166d35ac78cSryan_chen 	ulong id;
1167d35ac78cSryan_chen 	const char *name;
1168d35ac78cSryan_chen };
1169d35ac78cSryan_chen 
1170d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
1171d35ac78cSryan_chen 	{ ASPEED_CLK_HPLL, "hpll" },
1172d35ac78cSryan_chen 	{ ASPEED_CLK_MPLL, "mpll" },
1173d35ac78cSryan_chen 	{ ASPEED_CLK_APLL, "apll" },
1174d35ac78cSryan_chen 	{ ASPEED_CLK_EPLL, "epll" },
1175d35ac78cSryan_chen 	{ ASPEED_CLK_DPLL, "dpll" },
1176d35ac78cSryan_chen 	{ ASPEED_CLK_AHB, "hclk" },
11776fa1ef3dSryan_chen 	{ ASPEED_CLK_APB1, "pclk1" },
11786fa1ef3dSryan_chen 	{ ASPEED_CLK_APB2, "pclk2" },
1179c304f173Sryan_chen 	{ ASPEED_CLK_BCLK, "bclk" },
11802e195992Sryan_chen 	{ ASPEED_CLK_UARTX, "uxclk" },
1181def99fcbSryan_chen 	{ ASPEED_CLK_HUARTX, "huxclk" },
1182d35ac78cSryan_chen };
1183d35ac78cSryan_chen 
1184d35ac78cSryan_chen int soc_clk_dump(void)
1185d35ac78cSryan_chen {
1186d35ac78cSryan_chen 	struct udevice *dev;
1187d35ac78cSryan_chen 	struct clk clk;
1188d35ac78cSryan_chen 	unsigned long rate;
1189d35ac78cSryan_chen 	int i, ret;
1190d35ac78cSryan_chen 
1191d35ac78cSryan_chen 	ret = uclass_get_device_by_driver(UCLASS_CLK,
1192d35ac78cSryan_chen 					  DM_GET_DRIVER(aspeed_scu), &dev);
1193d35ac78cSryan_chen 	if (ret)
1194d35ac78cSryan_chen 		return ret;
1195d35ac78cSryan_chen 
1196d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
1197d35ac78cSryan_chen 
1198d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
1199d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
1200d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
1201d35ac78cSryan_chen 		if (ret < 0) {
1202d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
1203d35ac78cSryan_chen 			continue;
1204d35ac78cSryan_chen 		}
1205d35ac78cSryan_chen 
1206d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
1207d35ac78cSryan_chen 		rate = ret;
1208d35ac78cSryan_chen 
1209d35ac78cSryan_chen 		clk_free(&clk);
1210d35ac78cSryan_chen 
1211d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
1212d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
1213d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
1214d35ac78cSryan_chen 			continue;
1215d35ac78cSryan_chen 		}
1216d35ac78cSryan_chen 		if (ret < 0) {
1217d35ac78cSryan_chen 			printf("%s %lu: get_rate err: %d\n",
1218d35ac78cSryan_chen 			       __func__, aspeed_clk_names[i].id, ret);
1219d35ac78cSryan_chen 			continue;
1220d35ac78cSryan_chen 		}
1221d35ac78cSryan_chen 
1222d35ac78cSryan_chen 		printf("%s(%3lu):\t%lu\n",
1223d35ac78cSryan_chen 		       aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate);
1224d35ac78cSryan_chen 	}
1225d35ac78cSryan_chen 
1226d35ac78cSryan_chen 	return 0;
1227d35ac78cSryan_chen }
1228d35ac78cSryan_chen #endif
1229d35ac78cSryan_chen 
1230d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
1231d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
1232550e691bSryan_chen 	{ }
1233550e691bSryan_chen };
1234550e691bSryan_chen 
1235aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
1236aa36597fSDylan Hung 	.name		= "aspeed_scu",
1237550e691bSryan_chen 	.id		= UCLASS_CLK,
1238d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
1239f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
1240f9aa0ee1Sryan_chen 	.ops		= &ast2600_clk_ops,
1241d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
1242d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
1243550e691bSryan_chen };
1244