xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision c304f1737da9a4e9db7876330047b3f9ba04533c)
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 	}
121bbbfb0c5Sryan_chen 	if (pll_reg & BIT(24)) {
1229639db61Sryan_chen 		/* Pass through mode */
1239639db61Sryan_chen 		mult = div = 1;
1249639db61Sryan_chen 	} else {
1259639db61Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */
12675ced45aSDylan Hung 		union ast2600_pll_reg reg;
12775ced45aSDylan Hung 		reg.w = pll_reg;
12875ced45aSDylan Hung 		mult = (reg.b.m + 1) / (reg.b.n + 1);
12975ced45aSDylan Hung 		div = (reg.b.p + 1);
1309639db61Sryan_chen 	}
1319639db61Sryan_chen 	return ((clkin * mult)/div);
132550e691bSryan_chen 
133550e691bSryan_chen }
134550e691bSryan_chen 
1354f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
136550e691bSryan_chen {
137bbbfb0c5Sryan_chen 	u32 clkin = AST2600_CLK_IN;
13839283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
13939283ea7Sryan_chen 	unsigned int mult, div = 1;
140d6e349c7Sryan_chen 
14139283ea7Sryan_chen 	if (apll_reg & BIT(20)) {
142d6e349c7Sryan_chen 		/* Pass through mode */
143d6e349c7Sryan_chen 		mult = div = 1;
144d6e349c7Sryan_chen 	} else {
145bbbfb0c5Sryan_chen 		/* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
14639283ea7Sryan_chen 		u32 m = (apll_reg >> 5) & 0x3f;
14739283ea7Sryan_chen 		u32 od = (apll_reg >> 4) & 0x1;
14839283ea7Sryan_chen 		u32 n = apll_reg & 0xf;
149d6e349c7Sryan_chen 
150bbbfb0c5Sryan_chen 		mult = (2 - od) * (m + 2);
151bbbfb0c5Sryan_chen 		div = n + 1;
152d6e349c7Sryan_chen 	}
153bbbfb0c5Sryan_chen 	return ((clkin * mult)/div);
15439283ea7Sryan_chen }
15539283ea7Sryan_chen 
156d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
15745e0908aSryan_chen 	2, 2, 3, 4,
158d812df15Sryan_chen };
159d812df15Sryan_chen 
16045e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div0_table[] = {
16145e0908aSryan_chen 	3, 2, 3, 4,
16245e0908aSryan_chen };
16345e0908aSryan_chen 
16445e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div1_table[] = {
16545e0908aSryan_chen 	3, 4, 6, 8,
16645e0908aSryan_chen };
16745e0908aSryan_chen 
16845e0908aSryan_chen static u32 ast2600_a1_axi_ahb_default_table[] = {
16945e0908aSryan_chen 	3, 4, 3, 4, 2, 2, 2, 2,
170d812df15Sryan_chen };
171d812df15Sryan_chen 
172d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
173d812df15Sryan_chen {
174d812df15Sryan_chen 	u32 hw_rev = readl(&scu->chip_id0);
17545e0908aSryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
176d812df15Sryan_chen 	u32 axi_div = 1;
177d812df15Sryan_chen 	u32 ahb_div = 0;
178d812df15Sryan_chen 	u32 rate = 0;
179d812df15Sryan_chen 
18045e0908aSryan_chen 	if (hw_rev & BIT(16)) {
18145e0908aSryan_chen 		if(hwstrap1 & BIT(16)) {
18245e0908aSryan_chen 			ast2600_a1_axi_ahb_div1_table[0] = ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 0x3];
183d812df15Sryan_chen 			axi_div = 1;
18445e0908aSryan_chen 			ahb_div = ast2600_a1_axi_ahb_div1_table[(hwstrap1 >> 11) & 0x3];
18545e0908aSryan_chen 		} else {
18645e0908aSryan_chen 			ast2600_a1_axi_ahb_div0_table[0] = ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 0x3];
187d812df15Sryan_chen 			axi_div = 2;
18845e0908aSryan_chen 			ahb_div = ast2600_a1_axi_ahb_div0_table[(hwstrap1 >> 11) & 0x3];
18945e0908aSryan_chen 		}
19045e0908aSryan_chen 	} else {
19145e0908aSryan_chen 		//a0 : fix axi = hpll / 2
19245e0908aSryan_chen 		axi_div = 2;
193d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
19445e0908aSryan_chen 	}
195d812df15Sryan_chen 
196bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1972717883aSryan_chen 	return (rate / axi_div / ahb_div);
1982717883aSryan_chen }
1992717883aSryan_chen 
200*c304f173Sryan_chen static u32 ast2600_get_bclk_rate(struct ast2600_scu *scu)
201*c304f173Sryan_chen {
202*c304f173Sryan_chen 	u32 rate;
203*c304f173Sryan_chen 	u32 bclk_sel = (readl(&scu->clk_sel1) >> 20) & 0x7;
204*c304f173Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
205*c304f173Sryan_chen 
206*c304f173Sryan_chen 	return (rate /((bclk_sel + 1) * 4));
207*c304f173Sryan_chen }
208*c304f173Sryan_chen 
2096fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = {
2102717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
2112717883aSryan_chen };
2122717883aSryan_chen 
2136fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = {
2146fa1ef3dSryan_chen 	2, 4, 6, 8, 10, 12, 14, 16,
2156fa1ef3dSryan_chen };
2166fa1ef3dSryan_chen 
2176fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu)
2182717883aSryan_chen {
2192717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
2206fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)];
221bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
2222717883aSryan_chen 
2232717883aSryan_chen 	return (rate / apb_div);
224d812df15Sryan_chen }
225d812df15Sryan_chen 
2266fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu)
2276fa1ef3dSryan_chen {
2286fa1ef3dSryan_chen 	u32 clk_sel4 = readl(&scu->clk_sel4);
2296fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)];
2306fa1ef3dSryan_chen 	u32 rate = ast2600_get_hclk(scu);
2316fa1ef3dSryan_chen 
2326fa1ef3dSryan_chen 	return (rate / apb_div);
2336fa1ef3dSryan_chen }
2346fa1ef3dSryan_chen 
2352e195992Sryan_chen static u32 ast2600_get_uxclk_in_rate(struct ast2600_scu *scu)
236d6e349c7Sryan_chen {
23727881d20Sryan_chen 	u32 clk_in = 0;
2382e195992Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel5);
239550e691bSryan_chen 
24027881d20Sryan_chen 	uxclk_sel &= 0x3;
24127881d20Sryan_chen 	switch(uxclk_sel) {
24227881d20Sryan_chen 		case 0:
24327881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
24427881d20Sryan_chen 			break;
24527881d20Sryan_chen 		case 1:
24627881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
24727881d20Sryan_chen 			break;
24827881d20Sryan_chen 		case 2:
24927881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
25027881d20Sryan_chen 			break;
25127881d20Sryan_chen 		case 3:
25227881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
25327881d20Sryan_chen 			break;
25427881d20Sryan_chen 	}
255d6e349c7Sryan_chen 
25627881d20Sryan_chen 	return clk_in;
25727881d20Sryan_chen }
25827881d20Sryan_chen 
2592e195992Sryan_chen static u32 ast2600_get_huxclk_in_rate(struct ast2600_scu *scu)
26027881d20Sryan_chen {
26127881d20Sryan_chen 	u32 clk_in = 0;
2622e195992Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel5);
26327881d20Sryan_chen 
26427881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
26527881d20Sryan_chen 	switch(huclk_sel) {
26627881d20Sryan_chen 		case 0:
26727881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
26827881d20Sryan_chen 			break;
26927881d20Sryan_chen 		case 1:
27027881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
27127881d20Sryan_chen 			break;
27227881d20Sryan_chen 		case 2:
27327881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
27427881d20Sryan_chen 			break;
27527881d20Sryan_chen 		case 3:
27627881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
27727881d20Sryan_chen 			break;
27827881d20Sryan_chen 	}
27927881d20Sryan_chen 
28027881d20Sryan_chen 	return clk_in;
28127881d20Sryan_chen }
28227881d20Sryan_chen 
2832e195992Sryan_chen static u32 ast2600_get_uart_uxclk_rate(struct ast2600_scu *scu)
28427881d20Sryan_chen {
2852e195992Sryan_chen 	u32 clk_in = ast2600_get_uxclk_in_rate(scu);
28627881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
28727881d20Sryan_chen 	unsigned int mult, div;
28827881d20Sryan_chen 
28927881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
29027881d20Sryan_chen 	u32 r = div_reg & 0xff;
29127881d20Sryan_chen 
29227881d20Sryan_chen 	mult = r;
2932e195992Sryan_chen 	div = (n * 2);
29427881d20Sryan_chen 	return (clk_in * mult)/div;
29527881d20Sryan_chen }
29627881d20Sryan_chen 
2972e195992Sryan_chen static u32 ast2600_get_uart_huxclk_rate(struct ast2600_scu *scu)
29827881d20Sryan_chen {
2992e195992Sryan_chen 	u32 clk_in = ast2600_get_huxclk_in_rate(scu);
30027881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
30127881d20Sryan_chen 
30227881d20Sryan_chen 	unsigned int mult, div;
30327881d20Sryan_chen 
30427881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
30527881d20Sryan_chen 	u32 r = div_reg & 0xff;
30627881d20Sryan_chen 
30727881d20Sryan_chen 	mult = r;
3082e195992Sryan_chen 	div = (n * 2);
30927881d20Sryan_chen 	return (clk_in * mult)/div;
31027881d20Sryan_chen }
31127881d20Sryan_chen 
312f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
313f51926eeSryan_chen {
314f51926eeSryan_chen 	u32 clkin = 0;
315f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
316f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
317f51926eeSryan_chen 
318f51926eeSryan_chen 	if(clk_sel & BIT(8)) {
319f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
320f51926eeSryan_chen 	} else {
32110069884Sryan_chen 		clkin = ast2600_get_hclk(scu);
322f51926eeSryan_chen 	}
323f51926eeSryan_chen 	div = (div + 1) << 1;
324f51926eeSryan_chen 
325f51926eeSryan_chen 	return (clkin / div);
326f51926eeSryan_chen }
327f51926eeSryan_chen 
328f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
329f51926eeSryan_chen {
330bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
331f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
332f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
333f51926eeSryan_chen 
334f51926eeSryan_chen 	div = (div + 1) << 2;
335f51926eeSryan_chen 
336f51926eeSryan_chen 	return (clkin / div);
337f51926eeSryan_chen }
338f51926eeSryan_chen 
339f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
34027881d20Sryan_chen {
34127881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
34227881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
34327881d20Sryan_chen 	ulong uart_clk = 0;
34427881d20Sryan_chen 
34527881d20Sryan_chen 	switch(uart_idx) {
34627881d20Sryan_chen 		case 1:
34727881d20Sryan_chen 		case 2:
34827881d20Sryan_chen 		case 3:
34927881d20Sryan_chen 		case 4:
35027881d20Sryan_chen 		case 6:
35127881d20Sryan_chen 			if(uart_sel & BIT(uart_idx - 1))
3522e195992Sryan_chen 				uart_clk = ast2600_get_uart_huxclk_rate(scu) ;
353550e691bSryan_chen 			else
3542e195992Sryan_chen 				uart_clk = ast2600_get_uart_uxclk_rate(scu) ;
35527881d20Sryan_chen 			break;
35627881d20Sryan_chen 		case 5: //24mhz is come form usb phy 48Mhz
35727881d20Sryan_chen 			{
35827881d20Sryan_chen 			u8 uart5_clk_sel = 0;
35927881d20Sryan_chen 			//high bit
36027881d20Sryan_chen 			if (readl(&scu->misc_ctrl1) & BIT(12))
36127881d20Sryan_chen 				uart5_clk_sel = 0x2;
36227881d20Sryan_chen 			else
36327881d20Sryan_chen 				uart5_clk_sel = 0x0;
364550e691bSryan_chen 
36527881d20Sryan_chen 			if (readl(&scu->clk_sel2) & BIT(14))
36627881d20Sryan_chen 				uart5_clk_sel |= 0x1;
367550e691bSryan_chen 
36827881d20Sryan_chen 			switch(uart5_clk_sel) {
36927881d20Sryan_chen 				case 0:
37027881d20Sryan_chen 					uart_clk = 24000000;
37127881d20Sryan_chen 					break;
37227881d20Sryan_chen 				case 1:
373def99fcbSryan_chen 					uart_clk = 192000000;
37427881d20Sryan_chen 					break;
37527881d20Sryan_chen 				case 2:
37627881d20Sryan_chen 					uart_clk = 24000000/13;
37727881d20Sryan_chen 					break;
37827881d20Sryan_chen 				case 3:
37927881d20Sryan_chen 					uart_clk = 192000000/13;
38027881d20Sryan_chen 					break;
38127881d20Sryan_chen 			}
38227881d20Sryan_chen 			}
38327881d20Sryan_chen 			break;
38427881d20Sryan_chen 		case 7:
38527881d20Sryan_chen 		case 8:
38627881d20Sryan_chen 		case 9:
38727881d20Sryan_chen 		case 10:
38827881d20Sryan_chen 		case 11:
38927881d20Sryan_chen 		case 12:
39027881d20Sryan_chen 		case 13:
39127881d20Sryan_chen 			if(uart_sel5 & BIT(uart_idx - 1))
3922e195992Sryan_chen 				uart_clk = ast2600_get_uart_huxclk_rate(scu);
39327881d20Sryan_chen 			else
3942e195992Sryan_chen 				uart_clk = ast2600_get_uart_uxclk_rate(scu);
39527881d20Sryan_chen 			break;
39627881d20Sryan_chen 	}
39727881d20Sryan_chen 
39827881d20Sryan_chen 	return uart_clk;
399550e691bSryan_chen }
400550e691bSryan_chen 
401feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
402feb42054Sryan_chen {
403feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
404feb42054Sryan_chen 	ulong rate = 0;
405feb42054Sryan_chen 
406feb42054Sryan_chen 	switch (clk->id) {
407feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
408bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
409bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
410d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
411bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
412d812df15Sryan_chen 		break;
413feb42054Sryan_chen 	case ASPEED_CLK_AHB:
414feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
415feb42054Sryan_chen 		break;
4166fa1ef3dSryan_chen 	case ASPEED_CLK_APB1:
4176fa1ef3dSryan_chen 		rate = ast2600_get_pclk1(priv->scu);
4186fa1ef3dSryan_chen 		break;
4196fa1ef3dSryan_chen 	case ASPEED_CLK_APB2:
4206fa1ef3dSryan_chen 		rate = ast2600_get_pclk2(priv->scu);
421feb42054Sryan_chen 		break;
422bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
423bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
424bbbfb0c5Sryan_chen 		break;
425feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
426feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
427feb42054Sryan_chen 		break;
428feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
429feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
430feb42054Sryan_chen 		break;
431feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
432feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
433feb42054Sryan_chen 		break;
434feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
435feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
436feb42054Sryan_chen 		break;
437feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
438feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
439feb42054Sryan_chen 		break;
440*c304f173Sryan_chen 	case ASPEED_CLK_BCLK:
441*c304f173Sryan_chen 		rate = ast2600_get_bclk_rate(priv->scu);
442*c304f173Sryan_chen 		break;
443f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
444f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
445f51926eeSryan_chen 		break;
446f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
447f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
448f51926eeSryan_chen 		break;
4492e195992Sryan_chen 	case ASPEED_CLK_UARTX:
4502e195992Sryan_chen 		rate = ast2600_get_uart_uxclk_rate(priv->scu);
4512e195992Sryan_chen 		break;
4520998ddefSryan_chen 	case ASPEED_CLK_HUARTX:
4532e195992Sryan_chen 		rate = ast2600_get_uart_huxclk_rate(priv->scu);
4542e195992Sryan_chen 		break;
455feb42054Sryan_chen 	default:
456d812df15Sryan_chen 		pr_debug("can't get clk rate \n");
457feb42054Sryan_chen 		return -ENOENT;
458d812df15Sryan_chen 		break;
459feb42054Sryan_chen 	}
460feb42054Sryan_chen 
461feb42054Sryan_chen 	return rate;
462feb42054Sryan_chen }
463feb42054Sryan_chen 
464577fcdaeSDylan Hung /**
465577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
466577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
467577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
468550e691bSryan_chen  *
469577fcdaeSDylan Hung  * The function caller shall fill "pll->in" and "pll->out", then this function
470577fcdaeSDylan Hung  * will search the lookup table to find a valid PLL divider configuration.
471550e691bSryan_chen  */
472577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
473550e691bSryan_chen {
474577fcdaeSDylan Hung 	u32 i;
475577fcdaeSDylan Hung 	bool is_found = false;
476550e691bSryan_chen 
477577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
478577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
479577fcdaeSDylan Hung 		if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) {
480577fcdaeSDylan Hung 			is_found = true;
481577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
482577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
483577fcdaeSDylan Hung 			break;
484550e691bSryan_chen 		}
485550e691bSryan_chen 	}
486577fcdaeSDylan Hung 	return is_found;
487550e691bSryan_chen }
488fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu,
489fd52be0bSDylan Hung 				 struct ast2600_pll_cfg *p_cfg, int pll_idx)
490fd52be0bSDylan Hung {
491fd52be0bSDylan Hung 	u32 addr, addr_ext;
492fd52be0bSDylan Hung 	u32 reg;
493550e691bSryan_chen 
494fd52be0bSDylan Hung 	switch (pll_idx) {
495fd52be0bSDylan Hung 	case ASPEED_CLK_HPLL:
496fd52be0bSDylan Hung 		addr = (u32)(&scu->h_pll_param);
497fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->h_pll_ext_param);
498fd52be0bSDylan Hung 		break;
499fd52be0bSDylan Hung 	case ASPEED_CLK_MPLL:
500fd52be0bSDylan Hung 		addr = (u32)(&scu->m_pll_param);
501fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->m_pll_ext_param);
502fd52be0bSDylan Hung 		break;
503fd52be0bSDylan Hung 	case ASPEED_CLK_DPLL:
504fd52be0bSDylan Hung 		addr = (u32)(&scu->d_pll_param);
505fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->d_pll_ext_param);
506fd52be0bSDylan Hung 		break;
507fd52be0bSDylan Hung 	case ASPEED_CLK_EPLL:
508fd52be0bSDylan Hung 		addr = (u32)(&scu->e_pll_param);
509fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->e_pll_ext_param);
510fd52be0bSDylan Hung 		break;
511fd52be0bSDylan Hung 	default:
512fd52be0bSDylan Hung 		debug("unknown PLL index\n");
513fd52be0bSDylan Hung 		return 1;
514fd52be0bSDylan Hung 	}
515fd52be0bSDylan Hung 
516fd52be0bSDylan Hung 	p_cfg->reg.b.bypass = 0;
517fd52be0bSDylan Hung 	p_cfg->reg.b.off = 1;
518fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 1;
519fd52be0bSDylan Hung 
520fd52be0bSDylan Hung 	reg = readl(addr);
521fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
522fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
523fd52be0bSDylan Hung 	writel(reg, addr);
524fd52be0bSDylan Hung 
525fd52be0bSDylan Hung 	/* write extend parameter */
526fd52be0bSDylan Hung 	writel(p_cfg->ext_reg, addr_ext);
527fd52be0bSDylan Hung 	udelay(100);
528fd52be0bSDylan Hung 	p_cfg->reg.b.off = 0;
529fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 0;
530fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
531fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
532fd52be0bSDylan Hung 	writel(reg, addr);
533fd52be0bSDylan Hung 
534fd52be0bSDylan Hung 	/* polling PLL lock status */
535fd52be0bSDylan Hung 	while(0 == (readl(addr_ext) & BIT(31)));
536fd52be0bSDylan Hung 
537fd52be0bSDylan Hung 	return 0;
538fd52be0bSDylan Hung }
539feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
540550e691bSryan_chen {
541577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
542550e691bSryan_chen 
543577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
544577fcdaeSDylan Hung 	mpll.out = rate;
545577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&mpll)) {
546577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
547577fcdaeSDylan Hung 		return 0;
548577fcdaeSDylan Hung 	}
549fd52be0bSDylan Hung 	ast2600_configure_pll(scu, &(mpll.cfg), ASPEED_CLK_MPLL);
550577fcdaeSDylan Hung 
551cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
552d6e349c7Sryan_chen }
553d6e349c7Sryan_chen 
554d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
555550e691bSryan_chen {
556f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
557550e691bSryan_chen 
558550e691bSryan_chen 	ulong new_rate;
559550e691bSryan_chen 	switch (clk->id) {
560f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
561feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
562550e691bSryan_chen 		break;
563550e691bSryan_chen 	default:
564550e691bSryan_chen 		return -ENOENT;
565550e691bSryan_chen 	}
566550e691bSryan_chen 
567550e691bSryan_chen 	return new_rate;
568550e691bSryan_chen }
569feb42054Sryan_chen 
570f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
571f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
572f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
573f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
574f9aa0ee1Sryan_chen 
575cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu)
576cc476ffcSDylan Hung {
577eff28274SJohnny Huang 	/* scu340[25:0]: 1G default delay */
578eff28274SJohnny Huang 	clrsetbits_le32(&scu->mac12_clk_delay, GENMASK(25, 0),
579eff28274SJohnny Huang 			MAC_DEF_DELAY_1G);
5804760b3f8SDylan Hung 
5814760b3f8SDylan Hung 	/* set 100M/10M default delay */
5824760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M);
5834760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M);
584cc476ffcSDylan Hung 
585ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
586eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16));
587894c19cfSDylan Hung 
588cc476ffcSDylan Hung 	return 0;
589cc476ffcSDylan Hung }
590cc476ffcSDylan Hung 
59154f9cba1SDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu)
59254f9cba1SDylan Hung {
59354f9cba1SDylan Hung 
59454f9cba1SDylan Hung 	/*
595eff28274SJohnny Huang 	 * scu350[31]   RGMII 125M source: 0 = from IO pin
596eff28274SJohnny Huang 	 * scu350[25:0] MAC 1G delay
59754f9cba1SDylan Hung 	 */
598eff28274SJohnny Huang 	clrsetbits_le32(&scu->mac34_clk_delay, (BIT(31) | GENMASK(25, 0)),
599eff28274SJohnny Huang 			MAC34_DEF_DELAY_1G);
60054f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M);
60154f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M);
60254f9cba1SDylan Hung 
603eff28274SJohnny Huang 	/*
604eff28274SJohnny Huang 	 * clock source seletion and divider
605eff28274SJohnny Huang 	 * scu310[26:24] : MAC AHB bus clock = HCLK / 2
606eff28274SJohnny Huang 	 * scu310[18:16] : RMII 50M = HCLK_200M / 4
607eff28274SJohnny Huang 	 */
608eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)),
609eff28274SJohnny Huang 			((0x0 << 24) | (0x3 << 16)));
61054f9cba1SDylan Hung 
611eff28274SJohnny Huang 	/*
612eff28274SJohnny Huang 	 * set driving strength
613eff28274SJohnny Huang 	 * scu458[3:2] : MAC4 driving strength
614eff28274SJohnny Huang 	 * scu458[1:0] : MAC3 driving strength
615eff28274SJohnny Huang 	 */
616eff28274SJohnny Huang 	clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0),
617a961159eSDylan Hung 			(0x3 << 2) | (0x3 << 0));
61854f9cba1SDylan Hung 
61954f9cba1SDylan Hung 	return 0;
62054f9cba1SDylan Hung }
621eff28274SJohnny Huang 
62254f9cba1SDylan Hung /**
6235b5c3d44SDylan Hung  * ast2600 RGMII clock source tree
62454f9cba1SDylan Hung  *
62554f9cba1SDylan Hung  *    125M from external PAD -------->|\
62654f9cba1SDylan Hung  *    HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
62754f9cba1SDylan Hung  *            | |---->| divider |---->|/                             +
62854f9cba1SDylan Hung  *    EPLL -->|/                                                     |
62954f9cba1SDylan Hung  *                                                                   |
630eff28274SJohnny Huang  *    +---------<-----------|RGMIICK PAD output enable|<-------------+
63154f9cba1SDylan Hung  *    |
632eff28274SJohnny Huang  *    +--------------------------->|\
63354f9cba1SDylan Hung  *                                 | |----> RGMII 125M for MAC#3 & MAC#4
634eff28274SJohnny Huang  *    HCLK 200M ---->|divider|---->|/
6355b5c3d44SDylan Hung  *
636eff28274SJohnny Huang  * To simplify the control flow:
637eff28274SJohnny Huang  * 	1. RGMII 1/2 always use EPLL as the internal clock source
638eff28274SJohnny Huang  * 	2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source
6395b5c3d44SDylan Hung  *
640eff28274SJohnny Huang  *    125M from external PAD -------->|\
641eff28274SJohnny Huang  *                                    | |---->RGMII 125M for MAC#1 & MAC#2
642eff28274SJohnny Huang  *            EPLL---->| divider |--->|/                             +
643eff28274SJohnny Huang  *                                                                   |
644eff28274SJohnny Huang  *    +<--------------------|RGMIICK PAD output enable|<-------------+
645eff28274SJohnny Huang  *    |
646eff28274SJohnny Huang  *    +--------------------------->RGMII 125M for MAC#3 & MAC#4
647eff28274SJohnny Huang */
648eff28274SJohnny Huang #define RGMIICK_SRC_PAD			0
649eff28274SJohnny Huang #define RGMIICK_SRC_EPLL		1	/* recommended */
650eff28274SJohnny Huang #define RGMIICK_SRC_HPLL		2
651eff28274SJohnny Huang 
652eff28274SJohnny Huang #define RGMIICK_DIV2			1
653eff28274SJohnny Huang #define RGMIICK_DIV3			2
654eff28274SJohnny Huang #define RGMIICK_DIV4			3
655eff28274SJohnny Huang #define RGMIICK_DIV5			4
656eff28274SJohnny Huang #define RGMIICK_DIV6			5
657eff28274SJohnny Huang #define RGMIICK_DIV7			6
658eff28274SJohnny Huang #define RGMIICK_DIV8			7	/* recommended */
659eff28274SJohnny Huang 
660eff28274SJohnny Huang #define RMIICK_DIV4			0
661eff28274SJohnny Huang #define RMIICK_DIV8			1
662eff28274SJohnny Huang #define RMIICK_DIV12			2
663eff28274SJohnny Huang #define RMIICK_DIV16			3
664eff28274SJohnny Huang #define RMIICK_DIV20			4	/* recommended */
665eff28274SJohnny Huang #define RMIICK_DIV24			5
666eff28274SJohnny Huang #define RMIICK_DIV28			6
667eff28274SJohnny Huang #define RMIICK_DIV32			7
668eff28274SJohnny Huang 
669eff28274SJohnny Huang struct ast2600_mac_clk_div {
670eff28274SJohnny Huang 	u32 src;	/* 0=external PAD, 1=internal PLL */
671eff28274SJohnny Huang 	u32 fin;	/* divider input speed */
672eff28274SJohnny Huang 	u32 n;		/* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */
673eff28274SJohnny Huang 	u32 fout;	/* fout = fin / n */
674eff28274SJohnny Huang };
675eff28274SJohnny Huang 
676eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = {
677eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
678eff28274SJohnny Huang 	.fin = 1000000000,
679eff28274SJohnny Huang 	.n = RGMIICK_DIV8,
680eff28274SJohnny Huang 	.fout = 125000000,
681eff28274SJohnny Huang };
682eff28274SJohnny Huang 
683eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = {
684eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
685eff28274SJohnny Huang 	.fin = 1000000000,
686eff28274SJohnny Huang 	.n = RMIICK_DIV20,
687eff28274SJohnny Huang 	.fout = 50000000,
688eff28274SJohnny Huang };
689eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu,
690eff28274SJohnny Huang 				 struct ast2600_mac_clk_div *p_cfg)
691eff28274SJohnny Huang {
692eff28274SJohnny Huang 	struct ast2600_pll_desc pll;
693eff28274SJohnny Huang 
694eff28274SJohnny Huang 	pll.in = AST2600_CLK_IN;
695eff28274SJohnny Huang 	pll.out = p_cfg->fin;
696eff28274SJohnny Huang 	if (false == ast2600_search_clock_config(&pll)) {
697eff28274SJohnny Huang 		printf("error!! unable to find valid ETHNET MAC clock "
698eff28274SJohnny Huang 		       "setting\n");
699eff28274SJohnny Huang 		debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w,
700eff28274SJohnny Huang 		      pll.cfg.ext_reg);
701eff28274SJohnny Huang 		debug("%s: pll cfg = %02x %02x %02x\n", __func__,
702eff28274SJohnny Huang 		      pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p);
703eff28274SJohnny Huang 		return;
704eff28274SJohnny Huang 	}
705eff28274SJohnny Huang 	ast2600_configure_pll(p_scu, &(pll.cfg), p_cfg->src);
706eff28274SJohnny Huang }
707eff28274SJohnny Huang 
708eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu,
709eff28274SJohnny Huang 				   struct ast2600_mac_clk_div *p_cfg)
710eff28274SJohnny Huang {
711eff28274SJohnny Huang 	u32 reg_304 = readl(&p_scu->clk_sel2);
712eff28274SJohnny Huang 	u32 reg_340 = readl(&p_scu->mac12_clk_delay);
713eff28274SJohnny Huang 	u32 reg_350 = readl(&p_scu->mac34_clk_delay);
714eff28274SJohnny Huang 
715eff28274SJohnny Huang 	reg_340 &= ~GENMASK(31, 29);
716eff28274SJohnny Huang 	/* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */
717eff28274SJohnny Huang 	reg_340 |= BIT(28);
718eff28274SJohnny Huang 	if ((p_cfg->src == ASPEED_CLK_EPLL) ||
719eff28274SJohnny Huang 	    (p_cfg->src == ASPEED_CLK_HPLL)) {
720eff28274SJohnny Huang 		/*
721eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
722eff28274SJohnny Huang 		 * the divider setting
723eff28274SJohnny Huang 		 */
724eff28274SJohnny Huang 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) {
725eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
726eff28274SJohnny Huang 		}
727eff28274SJohnny Huang 		/* scu340[31]: select RGMII 125M from internal source */
728eff28274SJohnny Huang 		reg_340 |= BIT(31);
729eff28274SJohnny Huang 	}
730eff28274SJohnny Huang 
731eff28274SJohnny Huang 	reg_304 &= ~GENMASK(23, 20);
732eff28274SJohnny Huang 
733eff28274SJohnny Huang 	/* set clock divider */
734eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 20;
735eff28274SJohnny Huang 
736eff28274SJohnny Huang 	/* select internal clock source */
737eff28274SJohnny Huang 	if (ASPEED_CLK_HPLL == p_cfg->src) {
738eff28274SJohnny Huang 		reg_304 |= BIT(23);
739eff28274SJohnny Huang 	}
740eff28274SJohnny Huang 
741eff28274SJohnny Huang 	/* RGMII 3/4 clock source select */
742eff28274SJohnny Huang 	reg_350 &= ~BIT(31);
743eff28274SJohnny Huang #if 0
744eff28274SJohnny Huang 	if (RGMII_3_4_CLK_SRC_HCLK == p_cfg->rgmii_3_4_clk_src) {
745eff28274SJohnny Huang 		reg_350 |= BIT(31);
746eff28274SJohnny Huang 	}
747eff28274SJohnny Huang 
748eff28274SJohnny Huang 	/* set clock divider */
749eff28274SJohnny Huang 	reg_310 &= ~GENMASK(22, 20);
750eff28274SJohnny Huang 	reg_310 |= (p_cfg->hclk_clk_div & 0x7) << 20;
751eff28274SJohnny Huang #endif
752eff28274SJohnny Huang 
753eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
754eff28274SJohnny Huang 	writel(reg_340, &p_scu->mac12_clk_delay);
755eff28274SJohnny Huang 	writel(reg_350, &p_scu->mac34_clk_delay);
756eff28274SJohnny Huang }
757eff28274SJohnny Huang 
758eff28274SJohnny Huang /**
7595b5c3d44SDylan Hung  * ast2600 RMII/NCSI clock source tree
7605b5c3d44SDylan Hung  *
7615b5c3d44SDylan Hung  *    HPLL -->|\
7625b5c3d44SDylan Hung  *            | |---->| divider |----> RMII 50M for MAC#1 & MAC#2
7635b5c3d44SDylan Hung  *    EPLL -->|/
7645b5c3d44SDylan Hung  *
7655b5c3d44SDylan Hung  *    HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4
76654f9cba1SDylan Hung */
767eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu,
768eff28274SJohnny Huang 				  struct ast2600_mac_clk_div *p_cfg)
76954f9cba1SDylan Hung {
770eff28274SJohnny Huang 	u32 reg_304;
771eff28274SJohnny Huang 	u32 reg_310;
772eff28274SJohnny Huang 
773eff28274SJohnny Huang 	if ((p_cfg->src == ASPEED_CLK_EPLL) ||
774eff28274SJohnny Huang 	    (p_cfg->src == ASPEED_CLK_HPLL)) {
775eff28274SJohnny Huang 		/*
776eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
777eff28274SJohnny Huang 		 * the divider setting
778eff28274SJohnny Huang 		 */
779eff28274SJohnny Huang 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) {
780eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
781eff28274SJohnny Huang 		}
78254f9cba1SDylan Hung 	}
78354f9cba1SDylan Hung 
784eff28274SJohnny Huang 	reg_304 = readl(&p_scu->clk_sel2);
785eff28274SJohnny Huang 	reg_310 = readl(&p_scu->clk_sel4);
786eff28274SJohnny Huang 
787eff28274SJohnny Huang 	reg_304 &= ~GENMASK(19, 16);
788eff28274SJohnny Huang 
789eff28274SJohnny Huang 	/* set RMII 1/2 clock divider */
790eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 16;
791eff28274SJohnny Huang 
792eff28274SJohnny Huang 	/* RMII clock source selection */
793eff28274SJohnny Huang 	if (ASPEED_CLK_HPLL == p_cfg->src) {
794eff28274SJohnny Huang 		reg_304 |= BIT(19);
79554f9cba1SDylan Hung 	}
796eff28274SJohnny Huang 
797eff28274SJohnny Huang 	/* set RMII 3/4 clock divider */
798eff28274SJohnny Huang 	reg_310 &= ~GENMASK(18, 16);
799eff28274SJohnny Huang 	reg_310 |= (0x3 << 16);
800eff28274SJohnny Huang 
801eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
802eff28274SJohnny Huang 	writel(reg_310, &p_scu->clk_sel4);
803eff28274SJohnny Huang }
804eff28274SJohnny Huang 
805f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
806f9aa0ee1Sryan_chen {
807f9aa0ee1Sryan_chen 	u32 reset_bit;
808f9aa0ee1Sryan_chen 	u32 clkstop_bit;
809f9aa0ee1Sryan_chen 
810f9aa0ee1Sryan_chen 	switch (index) {
811f9aa0ee1Sryan_chen 	case 1:
812f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
813f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
814f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
815f9aa0ee1Sryan_chen 		udelay(100);
816f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
817f9aa0ee1Sryan_chen 		mdelay(10);
818f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
819f9aa0ee1Sryan_chen 
820f9aa0ee1Sryan_chen 		break;
821f9aa0ee1Sryan_chen 	case 2:
822f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
823f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
824f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
825f9aa0ee1Sryan_chen 		udelay(100);
826f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
827f9aa0ee1Sryan_chen 		mdelay(10);
828f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
829f9aa0ee1Sryan_chen 		break;
830f9aa0ee1Sryan_chen 	case 3:
831f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
832f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
833f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
834f9aa0ee1Sryan_chen 		udelay(100);
835f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
836f9aa0ee1Sryan_chen 		mdelay(10);
837f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
838f9aa0ee1Sryan_chen 		break;
839f9aa0ee1Sryan_chen 	case 4:
840f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
841f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
842f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
843f9aa0ee1Sryan_chen 		udelay(100);
844f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
845f9aa0ee1Sryan_chen 		mdelay(10);
846f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
847f9aa0ee1Sryan_chen 		break;
848f9aa0ee1Sryan_chen 	default:
849f9aa0ee1Sryan_chen 		return -EINVAL;
850f9aa0ee1Sryan_chen 	}
851f9aa0ee1Sryan_chen 
852f9aa0ee1Sryan_chen 	return 0;
853f9aa0ee1Sryan_chen }
854550e691bSryan_chen 
855f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
856f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
857f51926eeSryan_chen {
858f51926eeSryan_chen 	u32 reset_bit;
859f51926eeSryan_chen 	u32 clkstop_bit;
860f51926eeSryan_chen 
861f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
862f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
863f51926eeSryan_chen 
864fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
865fc9f12e6Sryan_chen 
866f51926eeSryan_chen 	udelay(100);
867f51926eeSryan_chen 	//enable clk
868f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
869f51926eeSryan_chen 	mdelay(10);
870fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
871f51926eeSryan_chen 
872f51926eeSryan_chen 	return 0;
873f51926eeSryan_chen }
874f51926eeSryan_chen 
875f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31
876f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
877f51926eeSryan_chen #define SCU_CLK_SD_DIV(x)			(x << 28)
8782cd7cba2Sryan_chen #define SCU_CLK_SD_FROM_APLL_CLK	BIT(8)
879f51926eeSryan_chen 
880f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
881f51926eeSryan_chen {
882f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
883f51926eeSryan_chen 	u32 enableclk_bit;
8842cd7cba2Sryan_chen 	u32 rate = 0;
8852cd7cba2Sryan_chen 	u32 div = 0;
8862cd7cba2Sryan_chen 	int i = 0;
887f51926eeSryan_chen 
888f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
889f51926eeSryan_chen 
8902cd7cba2Sryan_chen 	//ast2600 sd controller max clk is 200Mhz : use apll for clock source 800/4 = 200 : controller max is 200mhz
8912cd7cba2Sryan_chen 	rate = ast2600_get_apll_rate(scu);
8922cd7cba2Sryan_chen 	for(i = 0; i < 8; i++) {
8932cd7cba2Sryan_chen 		div = (i + 1) * 2;
8942cd7cba2Sryan_chen 		if ((rate / div) <= 200000000)
8952cd7cba2Sryan_chen 			break;
8962cd7cba2Sryan_chen 	}
897f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
8982cd7cba2Sryan_chen 	clk_sel |= SCU_CLK_SD_DIV(i) | SCU_CLK_SD_FROM_APLL_CLK;
899f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
900f51926eeSryan_chen 
901f51926eeSryan_chen 	//enable clk
902f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
903f51926eeSryan_chen 
904f51926eeSryan_chen 	return 0;
905f51926eeSryan_chen }
906f51926eeSryan_chen 
907f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
908f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
909f51926eeSryan_chen {
910f51926eeSryan_chen 	u32 reset_bit;
911f51926eeSryan_chen 	u32 clkstop_bit;
912f51926eeSryan_chen 
913f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
914f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
915f51926eeSryan_chen 
916fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
917f51926eeSryan_chen 	udelay(100);
918f51926eeSryan_chen 	//enable clk
919f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
920f51926eeSryan_chen 	mdelay(10);
921fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
922f51926eeSryan_chen 
923f51926eeSryan_chen 	return 0;
924f51926eeSryan_chen }
925f51926eeSryan_chen 
926f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15
927f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
928f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x)			(x << 12)
9292cd7cba2Sryan_chen #define SCU_CLK_EMMC_FROM_MPLL_CLK	BIT(11)	//AST2600A1
930f51926eeSryan_chen 
931f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
932f51926eeSryan_chen {
933b0c30ea3Sryan_chen 	u32 revision_id = readl(&scu->chip_id0);
934f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
935f51926eeSryan_chen 	u32 enableclk_bit;
936f4c4ddb1Sryan_chen 	u32 rate = 0;
937f4c4ddb1Sryan_chen 	u32 div = 0;
938f4c4ddb1Sryan_chen 	int i = 0;
939f51926eeSryan_chen 
940d0bdd5f3Sryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC);
941f51926eeSryan_chen 
9422cd7cba2Sryan_chen 	//ast2600 eMMC controller max clk is 200Mhz
9432cd7cba2Sryan_chen 	if(((revision_id & GENMASK(23, 16)) >> 16)) {
9442cd7cba2Sryan_chen 		//AST2600A1 ~ : use mpll to be clk source
945b0c30ea3Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
946b0c30ea3Sryan_chen 		for(i = 0; i < 8; i++) {
947b0c30ea3Sryan_chen 			div = (i + 1) * 2;
948b0c30ea3Sryan_chen 			if ((rate / div) <= 200000000)
949b0c30ea3Sryan_chen 				break;
950b0c30ea3Sryan_chen 		}
951b0c30ea3Sryan_chen 
952b0c30ea3Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
9532cd7cba2Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i) | SCU_CLK_EMMC_FROM_MPLL_CLK;
954b0c30ea3Sryan_chen 		writel(clk_sel, &scu->clk_sel1);
955b0c30ea3Sryan_chen 
956b0c30ea3Sryan_chen 	} else {
9572cd7cba2Sryan_chen 		//AST2600A0 : use hpll to be clk source
958f4c4ddb1Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
959f4c4ddb1Sryan_chen 
960f4c4ddb1Sryan_chen 		for(i = 0; i < 8; i++) {
961f4c4ddb1Sryan_chen 			div = (i + 1) * 4;
962f4c4ddb1Sryan_chen 			if ((rate / div) <= 200000000)
963f4c4ddb1Sryan_chen 				break;
964f4c4ddb1Sryan_chen 		}
965f4c4ddb1Sryan_chen 
966f4c4ddb1Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
967f4c4ddb1Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i);
968f51926eeSryan_chen 		writel(clk_sel, &scu->clk_sel1);
969b0c30ea3Sryan_chen 	}
970f51926eeSryan_chen 
971f51926eeSryan_chen 	//enable clk
972f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
973f51926eeSryan_chen 
974f51926eeSryan_chen 	return 0;
975f51926eeSryan_chen }
976f51926eeSryan_chen 
977baf00c26Sryan_chen #define SCU_CLKSTOP_FSICLK 30
978baf00c26Sryan_chen 
979baf00c26Sryan_chen static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu)
980baf00c26Sryan_chen {
981baf00c26Sryan_chen 	u32 reset_bit;
982baf00c26Sryan_chen 	u32 clkstop_bit;
983baf00c26Sryan_chen 
984baf00c26Sryan_chen 	reset_bit = BIT(ASPEED_RESET_FSI % 32);
985baf00c26Sryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_FSICLK);
986baf00c26Sryan_chen 
987baf00c26Sryan_chen 	/* The FSI clock is shared between masters. If it's already on
988baf00c26Sryan_chen 	 * don't touch it, as that will reset the existing master. */
989baf00c26Sryan_chen 	if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) {
990baf00c26Sryan_chen 		debug("%s: already running, not touching it\n", __func__);
991baf00c26Sryan_chen 		return 0;
992baf00c26Sryan_chen 	}
993baf00c26Sryan_chen 
994baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
995baf00c26Sryan_chen 	udelay(100);
996baf00c26Sryan_chen 	//enable clk
997baf00c26Sryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
998baf00c26Sryan_chen 	mdelay(10);
999baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1000baf00c26Sryan_chen 
1001baf00c26Sryan_chen 	return 0;
1002baf00c26Sryan_chen }
1003baf00c26Sryan_chen 
1004b8ec5ceaSryan_chen static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu)
1005b8ec5ceaSryan_chen {
1006b8ec5ceaSryan_chen 	u32 reset_bit;
1007b8ec5ceaSryan_chen 	u32 clkstop_bit;
1008b8ec5ceaSryan_chen 
1009b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P1);
1010b8ec5ceaSryan_chen 	clkstop_bit = BIT(14);
1011b8ec5ceaSryan_chen 
1012b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1013b8ec5ceaSryan_chen 		udelay(100);
1014b8ec5ceaSryan_chen 	//enable phy clk
1015b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_ctrl1);
1016b8ec5ceaSryan_chen 	mdelay(20);
1017b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1018b8ec5ceaSryan_chen 
1019b8ec5ceaSryan_chen 	return 0;
1020b8ec5ceaSryan_chen }
1021b8ec5ceaSryan_chen 
1022b8ec5ceaSryan_chen static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu)
1023b8ec5ceaSryan_chen {
1024b8ec5ceaSryan_chen 	u32 reset_bit;
1025b8ec5ceaSryan_chen 	u32 clkstop_bit;
1026b8ec5ceaSryan_chen 
1027b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P2);
1028b8ec5ceaSryan_chen 	clkstop_bit = BIT(7);
1029b8ec5ceaSryan_chen 
1030b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1031b8ec5ceaSryan_chen 			udelay(100);
1032b8ec5ceaSryan_chen 	//enable phy clk
1033b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1034b8ec5ceaSryan_chen 	mdelay(20);
1035b8ec5ceaSryan_chen 
1036b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1037b8ec5ceaSryan_chen 
1038b8ec5ceaSryan_chen 	return 0;
1039b8ec5ceaSryan_chen }
1040b8ec5ceaSryan_chen 
1041d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
1042550e691bSryan_chen {
1043f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
1044550e691bSryan_chen 
1045550e691bSryan_chen 	switch (clk->id) {
104686f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC1CLK:
104786f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 1);
1048550e691bSryan_chen 			break;
104986f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC2CLK:
105086f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 2);
1051550e691bSryan_chen 			break;
105277843939Sryan_chen 		case ASPEED_CLK_GATE_MAC3CLK:
105377843939Sryan_chen 			ast2600_configure_mac(priv->scu, 3);
105477843939Sryan_chen 			break;
105577843939Sryan_chen 		case ASPEED_CLK_GATE_MAC4CLK:
105677843939Sryan_chen 			ast2600_configure_mac(priv->scu, 4);
105777843939Sryan_chen 			break;
1058f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDCLK:
1059f51926eeSryan_chen 			ast2600_enable_sdclk(priv->scu);
1060f51926eeSryan_chen 			break;
1061f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDEXTCLK:
1062f51926eeSryan_chen 			ast2600_enable_extsdclk(priv->scu);
1063f51926eeSryan_chen 			break;
1064f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCCLK:
1065f51926eeSryan_chen 			ast2600_enable_emmcclk(priv->scu);
1066f51926eeSryan_chen 			break;
1067f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCEXTCLK:
1068f51926eeSryan_chen 			ast2600_enable_extemmcclk(priv->scu);
1069f51926eeSryan_chen 			break;
1070baf00c26Sryan_chen 		case ASPEED_CLK_GATE_FSICLK:
1071baf00c26Sryan_chen 			ast2600_enable_fsiclk(priv->scu);
1072baf00c26Sryan_chen 			break;
1073b8ec5ceaSryan_chen 		case ASPEED_CLK_GATE_USBPORT1CLK:
1074b8ec5ceaSryan_chen 			ast2600_enable_usbahclk(priv->scu);
1075b8ec5ceaSryan_chen 			break;
1076b8ec5ceaSryan_chen 		case ASPEED_CLK_GATE_USBPORT2CLK:
1077b8ec5ceaSryan_chen 			ast2600_enable_usbbhclk(priv->scu);
1078b8ec5ceaSryan_chen 			break;
1079550e691bSryan_chen 		default:
1080f9aa0ee1Sryan_chen 			pr_debug("can't enable clk \n");
1081550e691bSryan_chen 			return -ENOENT;
108277843939Sryan_chen 			break;
1083550e691bSryan_chen 	}
1084550e691bSryan_chen 
1085550e691bSryan_chen 	return 0;
1086550e691bSryan_chen }
1087550e691bSryan_chen 
1088f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
1089d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
1090d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
1091d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
1092550e691bSryan_chen };
1093550e691bSryan_chen 
1094d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
1095550e691bSryan_chen {
1096f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
109761ab9607Sryan_chen 	u32 uart_clk_source;
1098550e691bSryan_chen 
1099f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
1100f0d895afSryan_chen 	if (IS_ERR(priv->scu))
1101f0d895afSryan_chen 		return PTR_ERR(priv->scu);
1102550e691bSryan_chen 
1103b55086a6SChia-Wei, Wang 	uart_clk_source = dev_read_u32_default(dev, "uart-clk-source",
110461ab9607Sryan_chen 					    0x0);
110561ab9607Sryan_chen 
110661ab9607Sryan_chen 	if(uart_clk_source) {
110756dd3e85Sryan_chen 		if(uart_clk_source & GENMASK(5, 0))
110856dd3e85Sryan_chen 			setbits_le32(&priv->scu->clk_sel4, uart_clk_source & GENMASK(5, 0));
110956dd3e85Sryan_chen 		if(uart_clk_source & GENMASK(12, 6))
111056dd3e85Sryan_chen 			setbits_le32(&priv->scu->clk_sel5, uart_clk_source & GENMASK(12, 6));
111161ab9607Sryan_chen 	}
111261ab9607Sryan_chen 
1113b89500a2SDylan Hung 
1114b89500a2SDylan Hung 	ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig);
1115b89500a2SDylan Hung 	ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig);
1116b89500a2SDylan Hung 	ast2600_configure_mac12_clk(priv->scu);
1117b89500a2SDylan Hung 	ast2600_configure_mac34_clk(priv->scu);
1118b89500a2SDylan Hung 
1119fd0306aaSJohnny Huang 	/* RSA clock = HPLL/3 */
1120fd0306aaSJohnny Huang 	setbits_le32(&priv->scu->clk_sel1, BIT(19));
1121fd0306aaSJohnny Huang 	setbits_le32(&priv->scu->clk_sel1, BIT(27));
1122fd0306aaSJohnny Huang 
1123550e691bSryan_chen 	return 0;
1124550e691bSryan_chen }
1125550e691bSryan_chen 
1126d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
1127550e691bSryan_chen {
1128550e691bSryan_chen 	int ret;
1129550e691bSryan_chen 
1130550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
1131550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
1132550e691bSryan_chen 	if (ret)
1133550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
1134550e691bSryan_chen 
1135550e691bSryan_chen 	return 0;
1136550e691bSryan_chen }
1137550e691bSryan_chen 
1138d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK)
1139d35ac78cSryan_chen struct aspeed_clks {
1140d35ac78cSryan_chen 	ulong id;
1141d35ac78cSryan_chen 	const char *name;
1142d35ac78cSryan_chen };
1143d35ac78cSryan_chen 
1144d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
1145d35ac78cSryan_chen 	{ ASPEED_CLK_HPLL, "hpll" },
1146d35ac78cSryan_chen 	{ ASPEED_CLK_MPLL, "mpll" },
1147d35ac78cSryan_chen 	{ ASPEED_CLK_APLL, "apll" },
1148d35ac78cSryan_chen 	{ ASPEED_CLK_EPLL, "epll" },
1149d35ac78cSryan_chen 	{ ASPEED_CLK_DPLL, "dpll" },
1150d35ac78cSryan_chen 	{ ASPEED_CLK_AHB, "hclk" },
11516fa1ef3dSryan_chen 	{ ASPEED_CLK_APB1, "pclk1" },
11526fa1ef3dSryan_chen 	{ ASPEED_CLK_APB2, "pclk2" },
1153*c304f173Sryan_chen 	{ ASPEED_CLK_BCLK, "bclk" },
11542e195992Sryan_chen 	{ ASPEED_CLK_UARTX, "uxclk" },
1155def99fcbSryan_chen 	{ ASPEED_CLK_HUARTX, "huxclk" },
1156d35ac78cSryan_chen };
1157d35ac78cSryan_chen 
1158d35ac78cSryan_chen int soc_clk_dump(void)
1159d35ac78cSryan_chen {
1160d35ac78cSryan_chen 	struct udevice *dev;
1161d35ac78cSryan_chen 	struct clk clk;
1162d35ac78cSryan_chen 	unsigned long rate;
1163d35ac78cSryan_chen 	int i, ret;
1164d35ac78cSryan_chen 
1165d35ac78cSryan_chen 	ret = uclass_get_device_by_driver(UCLASS_CLK,
1166d35ac78cSryan_chen 					  DM_GET_DRIVER(aspeed_scu), &dev);
1167d35ac78cSryan_chen 	if (ret)
1168d35ac78cSryan_chen 		return ret;
1169d35ac78cSryan_chen 
1170d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
1171d35ac78cSryan_chen 
1172d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
1173d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
1174d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
1175d35ac78cSryan_chen 		if (ret < 0) {
1176d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
1177d35ac78cSryan_chen 			continue;
1178d35ac78cSryan_chen 		}
1179d35ac78cSryan_chen 
1180d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
1181d35ac78cSryan_chen 		rate = ret;
1182d35ac78cSryan_chen 
1183d35ac78cSryan_chen 		clk_free(&clk);
1184d35ac78cSryan_chen 
1185d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
1186d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
1187d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
1188d35ac78cSryan_chen 			continue;
1189d35ac78cSryan_chen 		}
1190d35ac78cSryan_chen 		if (ret < 0) {
1191d35ac78cSryan_chen 			printf("%s %lu: get_rate err: %d\n",
1192d35ac78cSryan_chen 			       __func__, aspeed_clk_names[i].id, ret);
1193d35ac78cSryan_chen 			continue;
1194d35ac78cSryan_chen 		}
1195d35ac78cSryan_chen 
1196d35ac78cSryan_chen 		printf("%s(%3lu):\t%lu\n",
1197d35ac78cSryan_chen 		       aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate);
1198d35ac78cSryan_chen 	}
1199d35ac78cSryan_chen 
1200d35ac78cSryan_chen 	return 0;
1201d35ac78cSryan_chen }
1202d35ac78cSryan_chen #endif
1203d35ac78cSryan_chen 
1204d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
1205d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
1206550e691bSryan_chen 	{ }
1207550e691bSryan_chen };
1208550e691bSryan_chen 
1209aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
1210aa36597fSDylan Hung 	.name		= "aspeed_scu",
1211550e691bSryan_chen 	.id		= UCLASS_CLK,
1212d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
1213f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
1214f9aa0ee1Sryan_chen 	.ops		= &ast2600_clk_ops,
1215d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
1216d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
1217550e691bSryan_chen };
1218