xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision f27685eb109a22e0a9f08e8acbe7ba34b7cfdf1a)
1550e691bSryan_chen // SPDX-License-Identifier: GPL-2.0
2550e691bSryan_chen /*
3550e691bSryan_chen  * Copyright (C) ASPEED Technology Inc.
4550e691bSryan_chen  */
5550e691bSryan_chen 
6550e691bSryan_chen #include <common.h>
7550e691bSryan_chen #include <clk-uclass.h>
8550e691bSryan_chen #include <dm.h>
9550e691bSryan_chen #include <asm/io.h>
10550e691bSryan_chen #include <dm/lists.h>
1162a6bcbfSryan_chen #include <asm/arch/scu_ast2600.h>
12d6e349c7Sryan_chen #include <dt-bindings/clock/ast2600-clock.h>
1339283ea7Sryan_chen #include <dt-bindings/reset/ast2600-reset.h>
14550e691bSryan_chen 
15550e691bSryan_chen /*
16a8fc7648SRyan Chen  * MAC Clock Delay settings
17550e691bSryan_chen  */
18550e691bSryan_chen #define RGMII_TXCLK_ODLY 8
19550e691bSryan_chen #define RMII_RXCLK_IDLY 2
20550e691bSryan_chen 
213dc90377SDylan Hung #define MAC_DEF_DELAY_1G 0x0041b75d
2275605a4bSDylan Hung #define MAC_DEF_DELAY_100M 0x00417410
2375605a4bSDylan Hung #define MAC_DEF_DELAY_10M 0x00417410
2454f9cba1SDylan Hung 
253dc90377SDylan Hung #define MAC34_DEF_DELAY_1G 0x0010438a
2654f9cba1SDylan Hung #define MAC34_DEF_DELAY_100M 0x00104208
2754f9cba1SDylan Hung #define MAC34_DEF_DELAY_10M 0x00104208
284760b3f8SDylan Hung 
29550e691bSryan_chen /*
30550e691bSryan_chen  * TGMII Clock Duty constants, taken from Aspeed SDK
31550e691bSryan_chen  */
32550e691bSryan_chen #define RGMII2_TXCK_DUTY 0x66
33550e691bSryan_chen #define RGMII1_TXCK_DUTY 0x64
34550e691bSryan_chen #define D2PLL_DEFAULT_RATE (250 * 1000 * 1000)
3585d48d8cSryan_chen #define CHIP_REVISION_ID GENMASK(23, 16)
3685d48d8cSryan_chen 
37550e691bSryan_chen DECLARE_GLOBAL_DATA_PTR;
38550e691bSryan_chen 
39550e691bSryan_chen /*
40550e691bSryan_chen  * Clock divider/multiplier configuration struct.
41550e691bSryan_chen  * For H-PLL and M-PLL the formula is
42550e691bSryan_chen  * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
43550e691bSryan_chen  * M - Numerator
44550e691bSryan_chen  * N - Denumerator
45550e691bSryan_chen  * P - Post Divider
46550e691bSryan_chen  * They have the same layout in their control register.
47550e691bSryan_chen  *
48550e691bSryan_chen  * D-PLL and D2-PLL have extra divider (OD + 1), which is not
49550e691bSryan_chen  * yet needed and ignored by clock configurations.
50550e691bSryan_chen  */
51577fcdaeSDylan Hung union ast2600_pll_reg {
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[] = {
76a8fc7648SRyan Chen 	{
775d05f4fcSRyan Chen 		.in = AST2600_CLK_IN,
785d05f4fcSRyan Chen 		.out = 400000000,
795d05f4fcSRyan Chen 		.cfg.reg.b.m = 95,
805d05f4fcSRyan Chen 		.cfg.reg.b.n = 2,
815d05f4fcSRyan Chen 		.cfg.reg.b.p = 1,
82577fcdaeSDylan Hung 		.cfg.ext_reg = 0x31,
83577fcdaeSDylan Hung 	},
845d05f4fcSRyan Chen 	{ .in = AST2600_CLK_IN,
855d05f4fcSRyan Chen 	  .out = 200000000,
865d05f4fcSRyan Chen 	  .cfg.reg.b.m = 127,
875d05f4fcSRyan Chen 	  .cfg.reg.b.n = 0,
885d05f4fcSRyan Chen 	  .cfg.reg.b.p = 15,
895d05f4fcSRyan Chen 	  .cfg.ext_reg = 0x3f },
905d05f4fcSRyan Chen 	{ .in = AST2600_CLK_IN,
915d05f4fcSRyan Chen 	  .out = 334000000,
925d05f4fcSRyan Chen 	  .cfg.reg.b.m = 667,
935d05f4fcSRyan Chen 	  .cfg.reg.b.n = 4,
945d05f4fcSRyan Chen 	  .cfg.reg.b.p = 9,
955d05f4fcSRyan Chen 	  .cfg.ext_reg = 0x14d },
965d05f4fcSRyan Chen 	{ .in = AST2600_CLK_IN,
975d05f4fcSRyan Chen 	  .out = 1000000000,
985d05f4fcSRyan Chen 	  .cfg.reg.b.m = 119,
995d05f4fcSRyan Chen 	  .cfg.reg.b.n = 2,
1005d05f4fcSRyan Chen 	  .cfg.reg.b.p = 0,
1015d05f4fcSRyan Chen 	  .cfg.ext_reg = 0x3d },
1025d05f4fcSRyan Chen 	{ .in = AST2600_CLK_IN,
1035d05f4fcSRyan Chen 	  .out = 50000000,
1045d05f4fcSRyan Chen 	  .cfg.reg.b.m = 95,
1055d05f4fcSRyan Chen 	  .cfg.reg.b.n = 2,
1065d05f4fcSRyan Chen 	  .cfg.reg.b.p = 15,
1075d05f4fcSRyan Chen 	  .cfg.ext_reg = 0x31 },
108550e691bSryan_chen };
109550e691bSryan_chen 
110bbbfb0c5Sryan_chen extern u32 ast2600_get_pll_rate(struct ast2600_scu *scu, int pll_idx)
111550e691bSryan_chen {
112d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
113bbbfb0c5Sryan_chen 	u32 pll_reg = 0;
1149639db61Sryan_chen 	unsigned int mult, div = 1;
115550e691bSryan_chen 
116bbbfb0c5Sryan_chen 	switch (pll_idx) {
117bbbfb0c5Sryan_chen 	case ASPEED_CLK_HPLL:
118bbbfb0c5Sryan_chen 		pll_reg = readl(&scu->h_pll_param);
119bbbfb0c5Sryan_chen 		break;
120bbbfb0c5Sryan_chen 	case ASPEED_CLK_MPLL:
121bbbfb0c5Sryan_chen 		pll_reg = readl(&scu->m_pll_param);
122bbbfb0c5Sryan_chen 		break;
123bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
124bbbfb0c5Sryan_chen 		pll_reg = readl(&scu->d_pll_param);
125bbbfb0c5Sryan_chen 		break;
126bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
127bbbfb0c5Sryan_chen 		pll_reg = readl(&scu->e_pll_param);
128bbbfb0c5Sryan_chen 		break;
129bbbfb0c5Sryan_chen 	}
130bbbfb0c5Sryan_chen 	if (pll_reg & BIT(24)) {
1319639db61Sryan_chen 		/* Pass through mode */
1329639db61Sryan_chen 		mult = div = 1;
1339639db61Sryan_chen 	} else {
1349639db61Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */
13575ced45aSDylan Hung 		union ast2600_pll_reg reg;
13675ced45aSDylan Hung 		reg.w = pll_reg;
137e5c4f4dfSryan_chen 		/*
138e5c4f4dfSryan_chen 		HPLL Numerator (M) = fix 0x5F when SCU500[10]=1
139e5c4f4dfSryan_chen 							 fix 0xBF when SCU500[10]=0 and SCU500[8]=1
140e5c4f4dfSryan_chen 		SCU200[12:0] (default 0x8F) when SCU510[10]=0 and SCU510[8]=0
141e5c4f4dfSryan_chen 		HPLL Denumerator (N) =	SCU200[18:13] (default 0x2)
142e5c4f4dfSryan_chen 		HPLL Divider (P)	 =	SCU200[22:19] (default 0x0)
143e5c4f4dfSryan_chen 		HPLL Bandwidth Adj (NB) =  fix 0x2F when SCU500[10]=1
144e5c4f4dfSryan_chen 								   fix 0x5F when SCU500[10]=0 and SCU500[8]=1
145e5c4f4dfSryan_chen 		SCU204[11:0] (default 0x31) when SCU500[10]=0 and SCU500[8]=0
146e5c4f4dfSryan_chen 		*/
147*f27685ebSRyan Chen 		if (pll_idx == ASPEED_CLK_HPLL) {
148e5c4f4dfSryan_chen 			u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
149e5c4f4dfSryan_chen 			if (hwstrap1 & BIT(10))
150e5c4f4dfSryan_chen 				reg.b.m = 0x5F;
151e5c4f4dfSryan_chen 			else {
152e5c4f4dfSryan_chen 				if (hwstrap1 & BIT(8))
153e5c4f4dfSryan_chen 					reg.b.m = 0xBF;
154a8fc7648SRyan Chen 				/* Otherwise keep default 0x8F */
155e5c4f4dfSryan_chen 			}
156e5c4f4dfSryan_chen 		}
15775ced45aSDylan Hung 		mult = (reg.b.m + 1) / (reg.b.n + 1);
15875ced45aSDylan Hung 		div = (reg.b.p + 1);
1599639db61Sryan_chen 	}
160a8fc7648SRyan Chen 
1619639db61Sryan_chen 	return ((clkin * mult) / div);
162550e691bSryan_chen }
163550e691bSryan_chen 
1644f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
165550e691bSryan_chen {
16685d48d8cSryan_chen 	u32 hw_rev = readl(&scu->chip_id1);
167bbbfb0c5Sryan_chen 	u32 clkin = AST2600_CLK_IN;
16839283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
16939283ea7Sryan_chen 	unsigned int mult, div = 1;
170d6e349c7Sryan_chen 
171a8fc7648SRyan Chen 	if (((hw_rev & CHIP_REVISION_ID) >> 16) >= 3) {
172a8fc7648SRyan Chen 		//after A2 version
17385d48d8cSryan_chen 		if (apll_reg & BIT(24)) {
17485d48d8cSryan_chen 			/* Pass through mode */
17585d48d8cSryan_chen 			mult = div = 1;
17685d48d8cSryan_chen 		} else {
17785d48d8cSryan_chen 			/* F = 25Mhz * [(m + 1) / (n + 1)] / (p + 1) */
17885d48d8cSryan_chen 			u32 m = apll_reg & 0x1fff;
17985d48d8cSryan_chen 			u32 n = (apll_reg >> 13) & 0x3f;
18085d48d8cSryan_chen 			u32 p = (apll_reg >> 19) & 0xf;
18185d48d8cSryan_chen 
18285d48d8cSryan_chen 			mult = (m + 1);
18385d48d8cSryan_chen 			div = (n + 1) * (p + 1);
18485d48d8cSryan_chen 		}
18585d48d8cSryan_chen 
18685d48d8cSryan_chen 	} else {
18739283ea7Sryan_chen 		if (apll_reg & BIT(20)) {
188d6e349c7Sryan_chen 			/* Pass through mode */
189d6e349c7Sryan_chen 			mult = div = 1;
190d6e349c7Sryan_chen 		} else {
191bbbfb0c5Sryan_chen 			/* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
19239283ea7Sryan_chen 			u32 m = (apll_reg >> 5) & 0x3f;
19339283ea7Sryan_chen 			u32 od = (apll_reg >> 4) & 0x1;
19439283ea7Sryan_chen 			u32 n = apll_reg & 0xf;
195d6e349c7Sryan_chen 
196bbbfb0c5Sryan_chen 			mult = (2 - od) * (m + 2);
197bbbfb0c5Sryan_chen 			div = n + 1;
198d6e349c7Sryan_chen 		}
19985d48d8cSryan_chen 	}
200a8fc7648SRyan Chen 
201bbbfb0c5Sryan_chen 	return ((clkin * mult) / div);
20239283ea7Sryan_chen }
20339283ea7Sryan_chen 
204d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
2055d05f4fcSRyan Chen 	2,
2065d05f4fcSRyan Chen 	2,
2075d05f4fcSRyan Chen 	3,
2085d05f4fcSRyan Chen 	4,
209d812df15Sryan_chen };
210d812df15Sryan_chen 
21145e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div0_table[] = {
2125d05f4fcSRyan Chen 	3,
2135d05f4fcSRyan Chen 	2,
2145d05f4fcSRyan Chen 	3,
2155d05f4fcSRyan Chen 	4,
21645e0908aSryan_chen };
21745e0908aSryan_chen 
21845e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div1_table[] = {
2195d05f4fcSRyan Chen 	3,
2205d05f4fcSRyan Chen 	4,
2215d05f4fcSRyan Chen 	6,
2225d05f4fcSRyan Chen 	8,
223e29dc694Sryan_chen };
224e29dc694Sryan_chen 
225e29dc694Sryan_chen static u32 ast2600_a1_axi_ahb_default_table[] = {
226e29dc694Sryan_chen 	3, 4, 3, 4, 2, 2, 2, 2,
227d812df15Sryan_chen };
228d812df15Sryan_chen 
229d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
230d812df15Sryan_chen {
23185d48d8cSryan_chen 	u32 hw_rev = readl(&scu->chip_id1);
23245e0908aSryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap);
233d812df15Sryan_chen 	u32 axi_div = 1;
234d812df15Sryan_chen 	u32 ahb_div = 0;
235d812df15Sryan_chen 	u32 rate = 0;
236d812df15Sryan_chen 
23785d48d8cSryan_chen 	if ((hw_rev & CHIP_REVISION_ID) >> 16) {
238a8fc7648SRyan Chen 		//After A0
23945e0908aSryan_chen 		if (hwstrap1 & BIT(16)) {
240a8fc7648SRyan Chen 			ast2600_a1_axi_ahb_div1_table[0] =
2415d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) &
2425d05f4fcSRyan Chen 								 0x3];
243d812df15Sryan_chen 			axi_div = 1;
2445d05f4fcSRyan Chen 			ahb_div =
2455d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_div1_table[(hwstrap1 >> 11) &
2465d05f4fcSRyan Chen 							      0x3];
24745e0908aSryan_chen 		} else {
248a8fc7648SRyan Chen 			ast2600_a1_axi_ahb_div0_table[0] =
2495d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) &
2505d05f4fcSRyan Chen 								 0x3];
251d812df15Sryan_chen 			axi_div = 2;
2525d05f4fcSRyan Chen 			ahb_div =
2535d05f4fcSRyan Chen 				ast2600_a1_axi_ahb_div0_table[(hwstrap1 >> 11) &
2545d05f4fcSRyan Chen 							      0x3];
25545e0908aSryan_chen 		}
25645e0908aSryan_chen 	} else {
257a8fc7648SRyan Chen 		//A0 : fix axi = hpll / 2
25845e0908aSryan_chen 		axi_div = 2;
259d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
26045e0908aSryan_chen 	}
261bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
262a8fc7648SRyan Chen 
2632717883aSryan_chen 	return (rate / axi_div / ahb_div);
2642717883aSryan_chen }
2652717883aSryan_chen 
266c304f173Sryan_chen static u32 ast2600_get_bclk_rate(struct ast2600_scu *scu)
267c304f173Sryan_chen {
268c304f173Sryan_chen 	u32 rate;
269c304f173Sryan_chen 	u32 bclk_sel = (readl(&scu->clk_sel1) >> 20) & 0x7;
270c304f173Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
271c304f173Sryan_chen 
272c304f173Sryan_chen 	return (rate / ((bclk_sel + 1) * 4));
273c304f173Sryan_chen }
274c304f173Sryan_chen 
2756fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = {
2762717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
2772717883aSryan_chen };
2782717883aSryan_chen 
2796fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = {
2806fa1ef3dSryan_chen 	2, 4, 6, 8, 10, 12, 14, 16,
2816fa1ef3dSryan_chen };
2826fa1ef3dSryan_chen 
2836fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu)
2842717883aSryan_chen {
2852717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
2866fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)];
287bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
2882717883aSryan_chen 
2892717883aSryan_chen 	return (rate / apb_div);
290d812df15Sryan_chen }
291d812df15Sryan_chen 
2926fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu)
2936fa1ef3dSryan_chen {
2946fa1ef3dSryan_chen 	u32 clk_sel4 = readl(&scu->clk_sel4);
2956fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)];
2966fa1ef3dSryan_chen 	u32 rate = ast2600_get_hclk(scu);
2976fa1ef3dSryan_chen 
2986fa1ef3dSryan_chen 	return (rate / apb_div);
2996fa1ef3dSryan_chen }
3006fa1ef3dSryan_chen 
3012e195992Sryan_chen static u32 ast2600_get_uxclk_in_rate(struct ast2600_scu *scu)
302d6e349c7Sryan_chen {
30327881d20Sryan_chen 	u32 clk_in = 0;
3042e195992Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel5);
305550e691bSryan_chen 
30627881d20Sryan_chen 	uxclk_sel &= 0x3;
30727881d20Sryan_chen 	switch (uxclk_sel) {
30827881d20Sryan_chen 	case 0:
30927881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 4;
31027881d20Sryan_chen 		break;
31127881d20Sryan_chen 	case 1:
31227881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 2;
31327881d20Sryan_chen 		break;
31427881d20Sryan_chen 	case 2:
31527881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu);
31627881d20Sryan_chen 		break;
31727881d20Sryan_chen 	case 3:
31827881d20Sryan_chen 		clk_in = ast2600_get_hclk(scu);
31927881d20Sryan_chen 		break;
32027881d20Sryan_chen 	}
321d6e349c7Sryan_chen 
32227881d20Sryan_chen 	return clk_in;
32327881d20Sryan_chen }
32427881d20Sryan_chen 
3252e195992Sryan_chen static u32 ast2600_get_huxclk_in_rate(struct ast2600_scu *scu)
32627881d20Sryan_chen {
32727881d20Sryan_chen 	u32 clk_in = 0;
3282e195992Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel5);
32927881d20Sryan_chen 
33027881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
33127881d20Sryan_chen 	switch (huclk_sel) {
33227881d20Sryan_chen 	case 0:
33327881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 4;
33427881d20Sryan_chen 		break;
33527881d20Sryan_chen 	case 1:
33627881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu) / 2;
33727881d20Sryan_chen 		break;
33827881d20Sryan_chen 	case 2:
33927881d20Sryan_chen 		clk_in = ast2600_get_apll_rate(scu);
34027881d20Sryan_chen 		break;
34127881d20Sryan_chen 	case 3:
34227881d20Sryan_chen 		clk_in = ast2600_get_hclk(scu);
34327881d20Sryan_chen 		break;
34427881d20Sryan_chen 	}
34527881d20Sryan_chen 
34627881d20Sryan_chen 	return clk_in;
34727881d20Sryan_chen }
34827881d20Sryan_chen 
3492e195992Sryan_chen static u32 ast2600_get_uart_uxclk_rate(struct ast2600_scu *scu)
35027881d20Sryan_chen {
3512e195992Sryan_chen 	u32 clk_in = ast2600_get_uxclk_in_rate(scu);
35227881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
35327881d20Sryan_chen 	unsigned int mult, div;
35427881d20Sryan_chen 
35527881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
35627881d20Sryan_chen 	u32 r = div_reg & 0xff;
35727881d20Sryan_chen 
35827881d20Sryan_chen 	mult = r;
3592e195992Sryan_chen 	div = (n * 2);
36027881d20Sryan_chen 	return (clk_in * mult) / div;
36127881d20Sryan_chen }
36227881d20Sryan_chen 
3632e195992Sryan_chen static u32 ast2600_get_uart_huxclk_rate(struct ast2600_scu *scu)
36427881d20Sryan_chen {
3652e195992Sryan_chen 	u32 clk_in = ast2600_get_huxclk_in_rate(scu);
36627881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
36727881d20Sryan_chen 
36827881d20Sryan_chen 	unsigned int mult, div;
36927881d20Sryan_chen 
37027881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
37127881d20Sryan_chen 	u32 r = div_reg & 0xff;
37227881d20Sryan_chen 
37327881d20Sryan_chen 	mult = r;
3742e195992Sryan_chen 	div = (n * 2);
37527881d20Sryan_chen 	return (clk_in * mult) / div;
37627881d20Sryan_chen }
37727881d20Sryan_chen 
378f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
379f51926eeSryan_chen {
380f51926eeSryan_chen 	u32 clkin = 0;
381f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
382f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
383f51926eeSryan_chen 
384f51926eeSryan_chen 	if (clk_sel & BIT(8)) {
385f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
386f51926eeSryan_chen 	} else {
38710069884Sryan_chen 		clkin = ast2600_get_hclk(scu);
388f51926eeSryan_chen 	}
389f51926eeSryan_chen 	div = (div + 1) << 1;
390f51926eeSryan_chen 
391f51926eeSryan_chen 	return (clkin / div);
392f51926eeSryan_chen }
393f51926eeSryan_chen 
394f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
395f51926eeSryan_chen {
396bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
397f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
398f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
399f51926eeSryan_chen 
400f51926eeSryan_chen 	div = (div + 1) << 2;
401f51926eeSryan_chen 
402f51926eeSryan_chen 	return (clkin / div);
403f51926eeSryan_chen }
404f51926eeSryan_chen 
405f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
40627881d20Sryan_chen {
40727881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
40827881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
40927881d20Sryan_chen 	ulong uart_clk = 0;
41027881d20Sryan_chen 
41127881d20Sryan_chen 	switch (uart_idx) {
41227881d20Sryan_chen 	case 1:
41327881d20Sryan_chen 	case 2:
41427881d20Sryan_chen 	case 3:
41527881d20Sryan_chen 	case 4:
41627881d20Sryan_chen 	case 6:
41727881d20Sryan_chen 		if (uart_sel & BIT(uart_idx - 1))
4182e195992Sryan_chen 			uart_clk = ast2600_get_uart_huxclk_rate(scu);
419550e691bSryan_chen 		else
4202e195992Sryan_chen 			uart_clk = ast2600_get_uart_uxclk_rate(scu);
42127881d20Sryan_chen 		break;
42227881d20Sryan_chen 	case 5: //24mhz is come form usb phy 48Mhz
42327881d20Sryan_chen 	{
42427881d20Sryan_chen 		u8 uart5_clk_sel = 0;
42527881d20Sryan_chen 		//high bit
42627881d20Sryan_chen 		if (readl(&scu->misc_ctrl1) & BIT(12))
42727881d20Sryan_chen 			uart5_clk_sel = 0x2;
42827881d20Sryan_chen 		else
42927881d20Sryan_chen 			uart5_clk_sel = 0x0;
430550e691bSryan_chen 
43127881d20Sryan_chen 		if (readl(&scu->clk_sel2) & BIT(14))
43227881d20Sryan_chen 			uart5_clk_sel |= 0x1;
433550e691bSryan_chen 
43427881d20Sryan_chen 		switch (uart5_clk_sel) {
43527881d20Sryan_chen 		case 0:
43627881d20Sryan_chen 			uart_clk = 24000000;
43727881d20Sryan_chen 			break;
43827881d20Sryan_chen 		case 1:
439def99fcbSryan_chen 			uart_clk = 192000000;
44027881d20Sryan_chen 			break;
44127881d20Sryan_chen 		case 2:
44227881d20Sryan_chen 			uart_clk = 24000000 / 13;
44327881d20Sryan_chen 			break;
44427881d20Sryan_chen 		case 3:
44527881d20Sryan_chen 			uart_clk = 192000000 / 13;
44627881d20Sryan_chen 			break;
44727881d20Sryan_chen 		}
4485d05f4fcSRyan Chen 	} break;
44927881d20Sryan_chen 	case 7:
45027881d20Sryan_chen 	case 8:
45127881d20Sryan_chen 	case 9:
45227881d20Sryan_chen 	case 10:
45327881d20Sryan_chen 	case 11:
45427881d20Sryan_chen 	case 12:
45527881d20Sryan_chen 	case 13:
45627881d20Sryan_chen 		if (uart_sel5 & BIT(uart_idx - 1))
4572e195992Sryan_chen 			uart_clk = ast2600_get_uart_huxclk_rate(scu);
45827881d20Sryan_chen 		else
4592e195992Sryan_chen 			uart_clk = ast2600_get_uart_uxclk_rate(scu);
46027881d20Sryan_chen 		break;
46127881d20Sryan_chen 	}
46227881d20Sryan_chen 
46327881d20Sryan_chen 	return uart_clk;
464550e691bSryan_chen }
465550e691bSryan_chen 
466feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
467feb42054Sryan_chen {
468feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
469feb42054Sryan_chen 	ulong rate = 0;
470feb42054Sryan_chen 
471feb42054Sryan_chen 	switch (clk->id) {
472feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
473bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
474bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
475d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
476bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
477d812df15Sryan_chen 		break;
478feb42054Sryan_chen 	case ASPEED_CLK_AHB:
479feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
480feb42054Sryan_chen 		break;
4816fa1ef3dSryan_chen 	case ASPEED_CLK_APB1:
4826fa1ef3dSryan_chen 		rate = ast2600_get_pclk1(priv->scu);
4836fa1ef3dSryan_chen 		break;
4846fa1ef3dSryan_chen 	case ASPEED_CLK_APB2:
4856fa1ef3dSryan_chen 		rate = ast2600_get_pclk2(priv->scu);
486feb42054Sryan_chen 		break;
487bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
488bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
489bbbfb0c5Sryan_chen 		break;
490feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
491feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
492feb42054Sryan_chen 		break;
493feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
494feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
495feb42054Sryan_chen 		break;
496feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
497feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
498feb42054Sryan_chen 		break;
499feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
500feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
501feb42054Sryan_chen 		break;
502feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
503feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
504feb42054Sryan_chen 		break;
505c304f173Sryan_chen 	case ASPEED_CLK_BCLK:
506c304f173Sryan_chen 		rate = ast2600_get_bclk_rate(priv->scu);
507c304f173Sryan_chen 		break;
508f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
509f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
510f51926eeSryan_chen 		break;
511f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
512f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
513f51926eeSryan_chen 		break;
5142e195992Sryan_chen 	case ASPEED_CLK_UARTX:
5152e195992Sryan_chen 		rate = ast2600_get_uart_uxclk_rate(priv->scu);
5162e195992Sryan_chen 		break;
5170998ddefSryan_chen 	case ASPEED_CLK_HUARTX:
5182e195992Sryan_chen 		rate = ast2600_get_uart_huxclk_rate(priv->scu);
5192e195992Sryan_chen 		break;
520feb42054Sryan_chen 	default:
521d812df15Sryan_chen 		pr_debug("can't get clk rate \n");
522feb42054Sryan_chen 		return -ENOENT;
523d812df15Sryan_chen 		break;
524feb42054Sryan_chen 	}
525feb42054Sryan_chen 
526feb42054Sryan_chen 	return rate;
527feb42054Sryan_chen }
528feb42054Sryan_chen 
529577fcdaeSDylan Hung /**
530577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
531577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
532577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
533a8fc7648SRyan Chen  * The function caller shall fill "pll->in" and "pll->out",
534a8fc7648SRyan Chen  * then this function will search the lookup table
535a8fc7648SRyan Chen  * to find a valid PLL divider configuration.
536550e691bSryan_chen  */
537577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
538550e691bSryan_chen {
539577fcdaeSDylan Hung 	u32 i;
540577fcdaeSDylan Hung 	bool is_found = false;
541550e691bSryan_chen 
542577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
543577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
544577fcdaeSDylan Hung 		if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) {
545577fcdaeSDylan Hung 			is_found = true;
546577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
547577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
548577fcdaeSDylan Hung 			break;
549550e691bSryan_chen 		}
550550e691bSryan_chen 	}
551577fcdaeSDylan Hung 	return is_found;
552550e691bSryan_chen }
553fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu,
554fd52be0bSDylan Hung 				 struct ast2600_pll_cfg *p_cfg, int pll_idx)
555fd52be0bSDylan Hung {
556fd52be0bSDylan Hung 	u32 addr, addr_ext;
557fd52be0bSDylan Hung 	u32 reg;
558550e691bSryan_chen 
559fd52be0bSDylan Hung 	switch (pll_idx) {
560fd52be0bSDylan Hung 	case ASPEED_CLK_HPLL:
561fd52be0bSDylan Hung 		addr = (u32)(&scu->h_pll_param);
562fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->h_pll_ext_param);
563fd52be0bSDylan Hung 		break;
564fd52be0bSDylan Hung 	case ASPEED_CLK_MPLL:
565fd52be0bSDylan Hung 		addr = (u32)(&scu->m_pll_param);
566fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->m_pll_ext_param);
567fd52be0bSDylan Hung 		break;
568fd52be0bSDylan Hung 	case ASPEED_CLK_DPLL:
569fd52be0bSDylan Hung 		addr = (u32)(&scu->d_pll_param);
570fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->d_pll_ext_param);
571fd52be0bSDylan Hung 		break;
572fd52be0bSDylan Hung 	case ASPEED_CLK_EPLL:
573fd52be0bSDylan Hung 		addr = (u32)(&scu->e_pll_param);
574fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->e_pll_ext_param);
575fd52be0bSDylan Hung 		break;
576fd52be0bSDylan Hung 	default:
577fd52be0bSDylan Hung 		debug("unknown PLL index\n");
578fd52be0bSDylan Hung 		return 1;
579fd52be0bSDylan Hung 	}
580fd52be0bSDylan Hung 
581fd52be0bSDylan Hung 	p_cfg->reg.b.bypass = 0;
582fd52be0bSDylan Hung 	p_cfg->reg.b.off = 1;
583fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 1;
584fd52be0bSDylan Hung 
585fd52be0bSDylan Hung 	reg = readl(addr);
586fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
587fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
588fd52be0bSDylan Hung 	writel(reg, addr);
589fd52be0bSDylan Hung 
590fd52be0bSDylan Hung 	/* write extend parameter */
591fd52be0bSDylan Hung 	writel(p_cfg->ext_reg, addr_ext);
592fd52be0bSDylan Hung 	udelay(100);
593fd52be0bSDylan Hung 	p_cfg->reg.b.off = 0;
594fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 0;
595fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
596fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
597fd52be0bSDylan Hung 	writel(reg, addr);
598fd52be0bSDylan Hung 
599fd52be0bSDylan Hung 	/* polling PLL lock status */
600fd52be0bSDylan Hung 	while (0 == (readl(addr_ext) & BIT(31)));
601fd52be0bSDylan Hung 
602fd52be0bSDylan Hung 	return 0;
603fd52be0bSDylan Hung }
604feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
605550e691bSryan_chen {
606577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
607550e691bSryan_chen 
608577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
609577fcdaeSDylan Hung 	mpll.out = rate;
610*f27685ebSRyan Chen 	if (ast2600_search_clock_config(&mpll) == false) {
611577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
612577fcdaeSDylan Hung 		return 0;
613577fcdaeSDylan Hung 	}
614fd52be0bSDylan Hung 	ast2600_configure_pll(scu, &(mpll.cfg), ASPEED_CLK_MPLL);
615577fcdaeSDylan Hung 
616cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
617d6e349c7Sryan_chen }
618d6e349c7Sryan_chen 
619d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
620550e691bSryan_chen {
621f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
622550e691bSryan_chen 
623550e691bSryan_chen 	ulong new_rate;
624550e691bSryan_chen 	switch (clk->id) {
625f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
626feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
627550e691bSryan_chen 		break;
628550e691bSryan_chen 	default:
629550e691bSryan_chen 		return -ENOENT;
630550e691bSryan_chen 	}
631550e691bSryan_chen 
632550e691bSryan_chen 	return new_rate;
633550e691bSryan_chen }
634feb42054Sryan_chen 
635f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1 (20)
636f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2 (21)
637f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3 (20)
638f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4 (21)
639f9aa0ee1Sryan_chen 
640cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu)
641cc476ffcSDylan Hung {
642eff28274SJohnny Huang 	/* scu340[25:0]: 1G default delay */
643eff28274SJohnny Huang 	clrsetbits_le32(&scu->mac12_clk_delay, GENMASK(25, 0),
644eff28274SJohnny Huang 			MAC_DEF_DELAY_1G);
6454760b3f8SDylan Hung 
6464760b3f8SDylan Hung 	/* set 100M/10M default delay */
6474760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M);
6484760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M);
649cc476ffcSDylan Hung 
650ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
651eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16));
652894c19cfSDylan Hung 
653cc476ffcSDylan Hung 	return 0;
654cc476ffcSDylan Hung }
655cc476ffcSDylan Hung 
65654f9cba1SDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu)
65754f9cba1SDylan Hung {
65854f9cba1SDylan Hung 	/*
659eff28274SJohnny Huang 	 * scu350[31]   RGMII 125M source: 0 = from IO pin
660eff28274SJohnny Huang 	 * scu350[25:0] MAC 1G delay
66154f9cba1SDylan Hung 	 */
662eff28274SJohnny Huang 	clrsetbits_le32(&scu->mac34_clk_delay, (BIT(31) | GENMASK(25, 0)),
663eff28274SJohnny Huang 			MAC34_DEF_DELAY_1G);
66454f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M);
66554f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M);
66654f9cba1SDylan Hung 
667eff28274SJohnny Huang 	/*
668eff28274SJohnny Huang 	 * clock source seletion and divider
669eff28274SJohnny Huang 	 * scu310[26:24] : MAC AHB bus clock = HCLK / 2
670eff28274SJohnny Huang 	 * scu310[18:16] : RMII 50M = HCLK_200M / 4
671eff28274SJohnny Huang 	 */
672eff28274SJohnny Huang 	clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)),
673eff28274SJohnny Huang 			((0x0 << 24) | (0x3 << 16)));
67454f9cba1SDylan Hung 
675eff28274SJohnny Huang 	/*
676eff28274SJohnny Huang 	 * set driving strength
677eff28274SJohnny Huang 	 * scu458[3:2] : MAC4 driving strength
678eff28274SJohnny Huang 	 * scu458[1:0] : MAC3 driving strength
679eff28274SJohnny Huang 	 */
680eff28274SJohnny Huang 	clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0),
681a961159eSDylan Hung 			(0x3 << 2) | (0x3 << 0));
68254f9cba1SDylan Hung 
68354f9cba1SDylan Hung 	return 0;
68454f9cba1SDylan Hung }
685eff28274SJohnny Huang 
68654f9cba1SDylan Hung /**
6875b5c3d44SDylan Hung  * ast2600 RGMII clock source tree
68854f9cba1SDylan Hung  * 125M from external PAD -------->|\
68954f9cba1SDylan Hung  * HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
69054f9cba1SDylan Hung  *         | |---->| divider |---->|/                             +
69154f9cba1SDylan Hung  * EPLL -->|/                                                     |
69254f9cba1SDylan Hung  *                                                                |
693eff28274SJohnny Huang  * +---------<-----------|RGMIICK PAD output enable|<-------------+
69454f9cba1SDylan Hung  * |
695eff28274SJohnny Huang  * +--------------------------->|\
69654f9cba1SDylan Hung  *                              | |----> RGMII 125M for MAC#3 & MAC#4
697eff28274SJohnny Huang  * HCLK 200M ---->|divider|---->|/
698eff28274SJohnny Huang  * To simplify the control flow:
699eff28274SJohnny Huang  * 1. RGMII 1/2 always use EPLL as the internal clock source
700eff28274SJohnny Huang  * 2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source
701eff28274SJohnny Huang  * 125M from external PAD -------->|\
702eff28274SJohnny Huang  *                                 | |---->RGMII 125M for MAC#1 & MAC#2
703eff28274SJohnny Huang  *         EPLL---->| divider |--->|/                             +
704eff28274SJohnny Huang  *                                                                |
705eff28274SJohnny Huang  * +<--------------------|RGMIICK PAD output enable|<-------------+
706eff28274SJohnny Huang  * |
707eff28274SJohnny Huang  * +--------------------------->RGMII 125M for MAC#3 & MAC#4
708eff28274SJohnny Huang */
709eff28274SJohnny Huang #define RGMIICK_SRC_PAD 0
710eff28274SJohnny Huang #define RGMIICK_SRC_EPLL 1 /* recommended */
711eff28274SJohnny Huang #define RGMIICK_SRC_HPLL 2
712eff28274SJohnny Huang 
713eff28274SJohnny Huang #define RGMIICK_DIV2 1
714eff28274SJohnny Huang #define RGMIICK_DIV3 2
715eff28274SJohnny Huang #define RGMIICK_DIV4 3
716eff28274SJohnny Huang #define RGMIICK_DIV5 4
717eff28274SJohnny Huang #define RGMIICK_DIV6 5
718eff28274SJohnny Huang #define RGMIICK_DIV7 6
719eff28274SJohnny Huang #define RGMIICK_DIV8 7 /* recommended */
720eff28274SJohnny Huang 
721eff28274SJohnny Huang #define RMIICK_DIV4 0
722eff28274SJohnny Huang #define RMIICK_DIV8 1
723eff28274SJohnny Huang #define RMIICK_DIV12 2
724eff28274SJohnny Huang #define RMIICK_DIV16 3
725eff28274SJohnny Huang #define RMIICK_DIV20 4 /* recommended */
726eff28274SJohnny Huang #define RMIICK_DIV24 5
727eff28274SJohnny Huang #define RMIICK_DIV28 6
728eff28274SJohnny Huang #define RMIICK_DIV32 7
729eff28274SJohnny Huang 
730eff28274SJohnny Huang struct ast2600_mac_clk_div {
731eff28274SJohnny Huang 	u32 src; /* 0=external PAD, 1=internal PLL */
732eff28274SJohnny Huang 	u32 fin; /* divider input speed */
733eff28274SJohnny Huang 	u32 n; /* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */
734eff28274SJohnny Huang 	u32 fout; /* fout = fin / n */
735eff28274SJohnny Huang };
736eff28274SJohnny Huang 
737eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = {
738eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
739eff28274SJohnny Huang 	.fin = 1000000000,
740eff28274SJohnny Huang 	.n = RGMIICK_DIV8,
741eff28274SJohnny Huang 	.fout = 125000000,
742eff28274SJohnny Huang };
743eff28274SJohnny Huang 
744eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = {
745eff28274SJohnny Huang 	.src = ASPEED_CLK_EPLL,
746eff28274SJohnny Huang 	.fin = 1000000000,
747eff28274SJohnny Huang 	.n = RMIICK_DIV20,
748eff28274SJohnny Huang 	.fout = 50000000,
749eff28274SJohnny Huang };
750eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu,
751eff28274SJohnny Huang 				 struct ast2600_mac_clk_div *p_cfg)
752eff28274SJohnny Huang {
753eff28274SJohnny Huang 	struct ast2600_pll_desc pll;
754eff28274SJohnny Huang 
755eff28274SJohnny Huang 	pll.in = AST2600_CLK_IN;
756eff28274SJohnny Huang 	pll.out = p_cfg->fin;
757eff28274SJohnny Huang 	if (false == ast2600_search_clock_config(&pll)) {
758eff28274SJohnny Huang 		printf("error!! unable to find valid ETHNET MAC clock "
759eff28274SJohnny Huang 		       "setting\n");
760*f27685ebSRyan Chen 		debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__,
761*f27685ebSRyan Chen 			pll.cfg.reg.w, pll.cfg.ext_reg);
762eff28274SJohnny Huang 		debug("%s: pll cfg = %02x %02x %02x\n", __func__,
763eff28274SJohnny Huang 		      pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p);
764eff28274SJohnny Huang 		return;
765eff28274SJohnny Huang 	}
766eff28274SJohnny Huang 	ast2600_configure_pll(p_scu, &(pll.cfg), p_cfg->src);
767eff28274SJohnny Huang }
768eff28274SJohnny Huang 
769eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu,
770eff28274SJohnny Huang 				   struct ast2600_mac_clk_div *p_cfg)
771eff28274SJohnny Huang {
772eff28274SJohnny Huang 	u32 reg_304 = readl(&p_scu->clk_sel2);
773eff28274SJohnny Huang 	u32 reg_340 = readl(&p_scu->mac12_clk_delay);
774eff28274SJohnny Huang 	u32 reg_350 = readl(&p_scu->mac34_clk_delay);
775eff28274SJohnny Huang 
776eff28274SJohnny Huang 	reg_340 &= ~GENMASK(31, 29);
777eff28274SJohnny Huang 	/* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */
778eff28274SJohnny Huang 	reg_340 |= BIT(28);
779eff28274SJohnny Huang 	if ((p_cfg->src == ASPEED_CLK_EPLL) ||
780eff28274SJohnny Huang 	    (p_cfg->src == ASPEED_CLK_HPLL)) {
781eff28274SJohnny Huang 		/*
782eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
783eff28274SJohnny Huang 		 * the divider setting
784eff28274SJohnny Huang 		 */
785eff28274SJohnny Huang 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) {
786eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
787eff28274SJohnny Huang 		}
788eff28274SJohnny Huang 		/* scu340[31]: select RGMII 125M from internal source */
789eff28274SJohnny Huang 		reg_340 |= BIT(31);
790eff28274SJohnny Huang 	}
791eff28274SJohnny Huang 
792eff28274SJohnny Huang 	reg_304 &= ~GENMASK(23, 20);
793eff28274SJohnny Huang 
794eff28274SJohnny Huang 	/* set clock divider */
795eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 20;
796eff28274SJohnny Huang 
797eff28274SJohnny Huang 	/* select internal clock source */
798eff28274SJohnny Huang 	if (ASPEED_CLK_HPLL == p_cfg->src) {
799eff28274SJohnny Huang 		reg_304 |= BIT(23);
800eff28274SJohnny Huang 	}
801eff28274SJohnny Huang 
802eff28274SJohnny Huang 	/* RGMII 3/4 clock source select */
803eff28274SJohnny Huang 	reg_350 &= ~BIT(31);
804eff28274SJohnny Huang 
805eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
806eff28274SJohnny Huang 	writel(reg_340, &p_scu->mac12_clk_delay);
807eff28274SJohnny Huang 	writel(reg_350, &p_scu->mac34_clk_delay);
808eff28274SJohnny Huang }
809eff28274SJohnny Huang 
810eff28274SJohnny Huang /**
8115b5c3d44SDylan Hung  * ast2600 RMII/NCSI clock source tree
8125b5c3d44SDylan Hung  * HPLL -->|\
8135b5c3d44SDylan Hung  *         | |---->| divider |----> RMII 50M for MAC#1 & MAC#2
8145b5c3d44SDylan Hung  * EPLL -->|/
8155b5c3d44SDylan Hung  * HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4
81654f9cba1SDylan Hung */
817eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu,
818eff28274SJohnny Huang 				  struct ast2600_mac_clk_div *p_cfg)
81954f9cba1SDylan Hung {
820eff28274SJohnny Huang 	u32 reg_304;
821eff28274SJohnny Huang 	u32 reg_310;
822eff28274SJohnny Huang 
823eff28274SJohnny Huang 	if ((p_cfg->src == ASPEED_CLK_EPLL) ||
824eff28274SJohnny Huang 	    (p_cfg->src == ASPEED_CLK_HPLL)) {
825eff28274SJohnny Huang 		/*
826eff28274SJohnny Huang 		 * re-init PLL if the current PLL output frequency doesn't match
827eff28274SJohnny Huang 		 * the divider setting
828eff28274SJohnny Huang 		 */
829eff28274SJohnny Huang 		if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) {
830eff28274SJohnny Huang 			ast2600_init_mac_pll(p_scu, p_cfg);
831eff28274SJohnny Huang 		}
83254f9cba1SDylan Hung 	}
83354f9cba1SDylan Hung 
834eff28274SJohnny Huang 	reg_304 = readl(&p_scu->clk_sel2);
835eff28274SJohnny Huang 	reg_310 = readl(&p_scu->clk_sel4);
836eff28274SJohnny Huang 
837eff28274SJohnny Huang 	reg_304 &= ~GENMASK(19, 16);
838eff28274SJohnny Huang 
839eff28274SJohnny Huang 	/* set RMII 1/2 clock divider */
840eff28274SJohnny Huang 	reg_304 |= (p_cfg->n & 0x7) << 16;
841eff28274SJohnny Huang 
842eff28274SJohnny Huang 	/* RMII clock source selection */
843eff28274SJohnny Huang 	if (ASPEED_CLK_HPLL == p_cfg->src) {
844eff28274SJohnny Huang 		reg_304 |= BIT(19);
84554f9cba1SDylan Hung 	}
846eff28274SJohnny Huang 
847eff28274SJohnny Huang 	/* set RMII 3/4 clock divider */
848eff28274SJohnny Huang 	reg_310 &= ~GENMASK(18, 16);
849eff28274SJohnny Huang 	reg_310 |= (0x3 << 16);
850eff28274SJohnny Huang 
851eff28274SJohnny Huang 	writel(reg_304, &p_scu->clk_sel2);
852eff28274SJohnny Huang 	writel(reg_310, &p_scu->clk_sel4);
853eff28274SJohnny Huang }
854eff28274SJohnny Huang 
855f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
856f9aa0ee1Sryan_chen {
857f9aa0ee1Sryan_chen 	u32 reset_bit;
858f9aa0ee1Sryan_chen 	u32 clkstop_bit;
859f9aa0ee1Sryan_chen 
860f9aa0ee1Sryan_chen 	switch (index) {
861f9aa0ee1Sryan_chen 	case 1:
862f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
863f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
864f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
865f9aa0ee1Sryan_chen 		udelay(100);
866f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
867f9aa0ee1Sryan_chen 		mdelay(10);
868f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
869f9aa0ee1Sryan_chen 		break;
870f9aa0ee1Sryan_chen 	case 2:
871f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
872f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
873f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
874f9aa0ee1Sryan_chen 		udelay(100);
875f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
876f9aa0ee1Sryan_chen 		mdelay(10);
877f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
878f9aa0ee1Sryan_chen 		break;
879f9aa0ee1Sryan_chen 	case 3:
880f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
881f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
882f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
883f9aa0ee1Sryan_chen 		udelay(100);
884f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
885f9aa0ee1Sryan_chen 		mdelay(10);
886f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
887f9aa0ee1Sryan_chen 		break;
888f9aa0ee1Sryan_chen 	case 4:
889f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
890f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
891f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
892f9aa0ee1Sryan_chen 		udelay(100);
893f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
894f9aa0ee1Sryan_chen 		mdelay(10);
895f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
896f9aa0ee1Sryan_chen 		break;
897f9aa0ee1Sryan_chen 	default:
898f9aa0ee1Sryan_chen 		return -EINVAL;
899f9aa0ee1Sryan_chen 	}
900f9aa0ee1Sryan_chen 
901f9aa0ee1Sryan_chen 	return 0;
902f9aa0ee1Sryan_chen }
903550e691bSryan_chen 
904a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_FROM_HPLL_CLK BIT(19)
905a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_CLK_MASK GENMASK(27, 26)
906a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_CLK_DIV(x) (x << 26)
907a8fc7648SRyan Chen static void ast2600_configure_rsa_ecc_clk(struct ast2600_scu *scu)
908a8fc7648SRyan Chen {
909a8fc7648SRyan Chen 	u32 clk_sel = readl(&scu->clk_sel1);
910a8fc7648SRyan Chen 
911a8fc7648SRyan Chen 	/* Configure RSA clock = HPLL/3 */
912a8fc7648SRyan Chen 	clk_sel |= SCU_CLK_ECC_RSA_FROM_HPLL_CLK;
913a8fc7648SRyan Chen 	clk_sel &= ~SCU_CLK_ECC_RSA_CLK_MASK;
914a8fc7648SRyan Chen 	clk_sel |= SCU_CLK_ECC_RSA_CLK_DIV(2);
915a8fc7648SRyan Chen 
916a8fc7648SRyan Chen 	writel(clk_sel, &scu->clk_sel1);
917a8fc7648SRyan Chen }
918a8fc7648SRyan Chen 
919f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
920f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
921f51926eeSryan_chen {
922f51926eeSryan_chen 	u32 reset_bit;
923f51926eeSryan_chen 	u32 clkstop_bit;
924f51926eeSryan_chen 
925f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
926f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
927f51926eeSryan_chen 
928fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
929fc9f12e6Sryan_chen 
930f51926eeSryan_chen 	udelay(100);
931f51926eeSryan_chen 	//enable clk
932f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
933f51926eeSryan_chen 	mdelay(10);
934fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
935f51926eeSryan_chen 
936f51926eeSryan_chen 	return 0;
937f51926eeSryan_chen }
938f51926eeSryan_chen 
939f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31
940f51926eeSryan_chen #define SCU_CLK_SD_MASK (0x7 << 28)
941f51926eeSryan_chen #define SCU_CLK_SD_DIV(x) (x << 28)
9422cd7cba2Sryan_chen #define SCU_CLK_SD_FROM_APLL_CLK BIT(8)
943f51926eeSryan_chen 
944f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
945f51926eeSryan_chen {
946f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
947f51926eeSryan_chen 	u32 enableclk_bit;
9482cd7cba2Sryan_chen 	u32 rate = 0;
9492cd7cba2Sryan_chen 	u32 div = 0;
9502cd7cba2Sryan_chen 	int i = 0;
951f51926eeSryan_chen 
952f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
953f51926eeSryan_chen 
954a8fc7648SRyan Chen 	/* ast2600 sd controller max clk is 200Mhz :
955a8fc7648SRyan Chen 	 * use apll for clock source 800/4 = 200 : controller max is 200mhz
956a8fc7648SRyan Chen 	 */
9572cd7cba2Sryan_chen 	rate = ast2600_get_apll_rate(scu);
9582cd7cba2Sryan_chen 	for (i = 0; i < 8; i++) {
9592cd7cba2Sryan_chen 		div = (i + 1) * 2;
9602cd7cba2Sryan_chen 		if ((rate / div) <= 200000000)
9612cd7cba2Sryan_chen 			break;
9622cd7cba2Sryan_chen 	}
963f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
9642cd7cba2Sryan_chen 	clk_sel |= SCU_CLK_SD_DIV(i) | SCU_CLK_SD_FROM_APLL_CLK;
965f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
966f51926eeSryan_chen 
967f51926eeSryan_chen 	//enable clk
968f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
969f51926eeSryan_chen 
970f51926eeSryan_chen 	return 0;
971f51926eeSryan_chen }
972f51926eeSryan_chen 
973f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
974f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
975f51926eeSryan_chen {
976f51926eeSryan_chen 	u32 reset_bit;
977f51926eeSryan_chen 	u32 clkstop_bit;
978f51926eeSryan_chen 
979f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
980f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
981f51926eeSryan_chen 
982fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
983f51926eeSryan_chen 	udelay(100);
984f51926eeSryan_chen 	//enable clk
985f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
986f51926eeSryan_chen 	mdelay(10);
987fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
988f51926eeSryan_chen 
989f51926eeSryan_chen 	return 0;
990f51926eeSryan_chen }
991f51926eeSryan_chen 
992f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15
993f51926eeSryan_chen #define SCU_CLK_EMMC_MASK (0x7 << 12)
994f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x) (x << 12)
995a8fc7648SRyan Chen #define SCU_CLK_EMMC_FROM_MPLL_CLK BIT(11)
996f51926eeSryan_chen 
997f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
998f51926eeSryan_chen {
99985d48d8cSryan_chen 	u32 revision_id = readl(&scu->chip_id1);
1000f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
1001f51926eeSryan_chen 	u32 enableclk_bit;
1002f4c4ddb1Sryan_chen 	u32 rate = 0;
1003f4c4ddb1Sryan_chen 	u32 div = 0;
1004f4c4ddb1Sryan_chen 	int i = 0;
1005f51926eeSryan_chen 
1006d0bdd5f3Sryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC);
1007f51926eeSryan_chen 
10082cd7cba2Sryan_chen 	//ast2600 eMMC controller max clk is 200Mhz
1009*f27685ebSRyan Chen 
1010*f27685ebSRyan Chen 	/* HPll->1/2->
1011*f27685ebSRyan Chen     *             \
1012*f27685ebSRyan Chen 	* 	           ->SCU300[11]->SCU300[14:12][1/N]->EMMC12C[15:8][1/N]-> eMMC clk
1013*f27685ebSRyan Chen     *             /
1014*f27685ebSRyan Chen 	* MPLL------>
1015a8fc7648SRyan Chen 	*/
101685d48d8cSryan_chen 	if (((revision_id & CHIP_REVISION_ID) >> 16)) {
10178c32294fSryan_chen 		//AST2600A1 : use mpll to be clk source
1018b0c30ea3Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
1019b0c30ea3Sryan_chen 		for (i = 0; i < 8; i++) {
1020b0c30ea3Sryan_chen 			div = (i + 1) * 2;
1021b0c30ea3Sryan_chen 			if ((rate / div) <= 200000000)
1022b0c30ea3Sryan_chen 				break;
1023b0c30ea3Sryan_chen 		}
1024b0c30ea3Sryan_chen 
1025b0c30ea3Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
10262cd7cba2Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i) | SCU_CLK_EMMC_FROM_MPLL_CLK;
1027b0c30ea3Sryan_chen 		writel(clk_sel, &scu->clk_sel1);
1028b0c30ea3Sryan_chen 
1029b0c30ea3Sryan_chen 	} else {
10302cd7cba2Sryan_chen 		//AST2600A0 : use hpll to be clk source
1031f4c4ddb1Sryan_chen 		rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1032f4c4ddb1Sryan_chen 
1033f4c4ddb1Sryan_chen 		for (i = 0; i < 8; i++) {
1034f4c4ddb1Sryan_chen 			div = (i + 1) * 4;
1035f4c4ddb1Sryan_chen 			if ((rate / div) <= 200000000)
1036f4c4ddb1Sryan_chen 				break;
1037f4c4ddb1Sryan_chen 		}
1038f4c4ddb1Sryan_chen 
1039f4c4ddb1Sryan_chen 		clk_sel &= ~SCU_CLK_EMMC_MASK;
1040f4c4ddb1Sryan_chen 		clk_sel |= SCU_CLK_EMMC_DIV(i);
1041f51926eeSryan_chen 		writel(clk_sel, &scu->clk_sel1);
1042b0c30ea3Sryan_chen 	}
1043f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
1044f51926eeSryan_chen 
1045f51926eeSryan_chen 	return 0;
1046f51926eeSryan_chen }
1047f51926eeSryan_chen 
1048baf00c26Sryan_chen #define SCU_CLKSTOP_FSICLK 30
1049baf00c26Sryan_chen 
1050baf00c26Sryan_chen static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu)
1051baf00c26Sryan_chen {
1052baf00c26Sryan_chen 	u32 reset_bit;
1053baf00c26Sryan_chen 	u32 clkstop_bit;
1054baf00c26Sryan_chen 
1055baf00c26Sryan_chen 	reset_bit = BIT(ASPEED_RESET_FSI % 32);
1056baf00c26Sryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_FSICLK);
1057baf00c26Sryan_chen 
1058baf00c26Sryan_chen 	/* The FSI clock is shared between masters. If it's already on
1059baf00c26Sryan_chen 	 * don't touch it, as that will reset the existing master. */
1060baf00c26Sryan_chen 	if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) {
1061baf00c26Sryan_chen 		debug("%s: already running, not touching it\n", __func__);
1062baf00c26Sryan_chen 		return 0;
1063baf00c26Sryan_chen 	}
1064baf00c26Sryan_chen 
1065baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
1066baf00c26Sryan_chen 	udelay(100);
1067baf00c26Sryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
1068baf00c26Sryan_chen 	mdelay(10);
1069baf00c26Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
1070baf00c26Sryan_chen 
1071baf00c26Sryan_chen 	return 0;
1072baf00c26Sryan_chen }
1073baf00c26Sryan_chen 
1074b8ec5ceaSryan_chen static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu)
1075b8ec5ceaSryan_chen {
1076b8ec5ceaSryan_chen 	u32 reset_bit;
1077b8ec5ceaSryan_chen 	u32 clkstop_bit;
1078b8ec5ceaSryan_chen 
1079b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P1);
1080b8ec5ceaSryan_chen 	clkstop_bit = BIT(14);
1081b8ec5ceaSryan_chen 
1082b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1083b8ec5ceaSryan_chen 	udelay(100);
1084b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_ctrl1);
1085b8ec5ceaSryan_chen 	mdelay(20);
1086b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1087b8ec5ceaSryan_chen 
1088b8ec5ceaSryan_chen 	return 0;
1089b8ec5ceaSryan_chen }
1090b8ec5ceaSryan_chen 
1091b8ec5ceaSryan_chen static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu)
1092b8ec5ceaSryan_chen {
1093b8ec5ceaSryan_chen 	u32 reset_bit;
1094b8ec5ceaSryan_chen 	u32 clkstop_bit;
1095b8ec5ceaSryan_chen 
1096b8ec5ceaSryan_chen 	reset_bit = BIT(ASPEED_RESET_EHCI_P2);
1097b8ec5ceaSryan_chen 	clkstop_bit = BIT(7);
1098b8ec5ceaSryan_chen 
1099b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
1100b8ec5ceaSryan_chen 	udelay(100);
1101b8ec5ceaSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
1102b8ec5ceaSryan_chen 	mdelay(20);
1103b8ec5ceaSryan_chen 
1104b8ec5ceaSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
1105b8ec5ceaSryan_chen 
1106b8ec5ceaSryan_chen 	return 0;
1107b8ec5ceaSryan_chen }
1108b8ec5ceaSryan_chen 
1109d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
1110550e691bSryan_chen {
1111f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
1112550e691bSryan_chen 
1113550e691bSryan_chen 	switch (clk->id) {
111486f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC1CLK:
111586f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 1);
1116550e691bSryan_chen 		break;
111786f91560Sryan_chen 	case ASPEED_CLK_GATE_MAC2CLK:
111886f91560Sryan_chen 		ast2600_configure_mac(priv->scu, 2);
1119550e691bSryan_chen 		break;
112077843939Sryan_chen 	case ASPEED_CLK_GATE_MAC3CLK:
112177843939Sryan_chen 		ast2600_configure_mac(priv->scu, 3);
112277843939Sryan_chen 		break;
112377843939Sryan_chen 	case ASPEED_CLK_GATE_MAC4CLK:
112477843939Sryan_chen 		ast2600_configure_mac(priv->scu, 4);
112577843939Sryan_chen 		break;
1126f51926eeSryan_chen 	case ASPEED_CLK_GATE_SDCLK:
1127f51926eeSryan_chen 		ast2600_enable_sdclk(priv->scu);
1128f51926eeSryan_chen 		break;
1129f51926eeSryan_chen 	case ASPEED_CLK_GATE_SDEXTCLK:
1130f51926eeSryan_chen 		ast2600_enable_extsdclk(priv->scu);
1131f51926eeSryan_chen 		break;
1132f51926eeSryan_chen 	case ASPEED_CLK_GATE_EMMCCLK:
1133f51926eeSryan_chen 		ast2600_enable_emmcclk(priv->scu);
1134f51926eeSryan_chen 		break;
1135f51926eeSryan_chen 	case ASPEED_CLK_GATE_EMMCEXTCLK:
1136f51926eeSryan_chen 		ast2600_enable_extemmcclk(priv->scu);
1137f51926eeSryan_chen 		break;
1138baf00c26Sryan_chen 	case ASPEED_CLK_GATE_FSICLK:
1139baf00c26Sryan_chen 		ast2600_enable_fsiclk(priv->scu);
1140baf00c26Sryan_chen 		break;
1141b8ec5ceaSryan_chen 	case ASPEED_CLK_GATE_USBPORT1CLK:
1142b8ec5ceaSryan_chen 		ast2600_enable_usbahclk(priv->scu);
1143b8ec5ceaSryan_chen 		break;
1144b8ec5ceaSryan_chen 	case ASPEED_CLK_GATE_USBPORT2CLK:
1145b8ec5ceaSryan_chen 		ast2600_enable_usbbhclk(priv->scu);
1146b8ec5ceaSryan_chen 		break;
1147550e691bSryan_chen 	default:
1148f9aa0ee1Sryan_chen 		pr_debug("can't enable clk \n");
1149550e691bSryan_chen 		return -ENOENT;
115077843939Sryan_chen 		break;
1151550e691bSryan_chen 	}
1152550e691bSryan_chen 
1153550e691bSryan_chen 	return 0;
1154550e691bSryan_chen }
1155550e691bSryan_chen 
1156f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
1157d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
1158d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
1159d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
1160550e691bSryan_chen };
1161550e691bSryan_chen 
1162d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
1163550e691bSryan_chen {
1164f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
116561ab9607Sryan_chen 	u32 uart_clk_source;
1166550e691bSryan_chen 
1167f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
1168f0d895afSryan_chen 	if (IS_ERR(priv->scu))
1169f0d895afSryan_chen 		return PTR_ERR(priv->scu);
1170550e691bSryan_chen 
11715d05f4fcSRyan Chen 	uart_clk_source = dev_read_u32_default(dev, "uart-clk-source", 0x0);
117261ab9607Sryan_chen 
117361ab9607Sryan_chen 	if (uart_clk_source) {
117456dd3e85Sryan_chen 		if (uart_clk_source & GENMASK(5, 0))
11755d05f4fcSRyan Chen 			setbits_le32(&priv->scu->clk_sel4,
11765d05f4fcSRyan Chen 				     uart_clk_source & GENMASK(5, 0));
117756dd3e85Sryan_chen 		if (uart_clk_source & GENMASK(12, 6))
11785d05f4fcSRyan Chen 			setbits_le32(&priv->scu->clk_sel5,
11795d05f4fcSRyan Chen 				     uart_clk_source & GENMASK(12, 6));
118061ab9607Sryan_chen 	}
118161ab9607Sryan_chen 
1182b89500a2SDylan Hung 	ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig);
1183b89500a2SDylan Hung 	ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig);
1184b89500a2SDylan Hung 	ast2600_configure_mac12_clk(priv->scu);
1185b89500a2SDylan Hung 	ast2600_configure_mac34_clk(priv->scu);
1186a8fc7648SRyan Chen 	ast2600_configure_rsa_ecc_clk(priv->scu);
1187fd0306aaSJohnny Huang 
1188550e691bSryan_chen 	return 0;
1189550e691bSryan_chen }
1190550e691bSryan_chen 
1191d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
1192550e691bSryan_chen {
1193550e691bSryan_chen 	int ret;
1194550e691bSryan_chen 
1195550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
1196550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
1197550e691bSryan_chen 	if (ret)
1198550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
1199550e691bSryan_chen 
1200550e691bSryan_chen 	return 0;
1201550e691bSryan_chen }
1202550e691bSryan_chen 
1203d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK)
1204d35ac78cSryan_chen struct aspeed_clks {
1205d35ac78cSryan_chen 	ulong id;
1206d35ac78cSryan_chen 	const char *name;
1207d35ac78cSryan_chen };
1208d35ac78cSryan_chen 
1209d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
12105d05f4fcSRyan Chen 	{ ASPEED_CLK_HPLL, "hpll" },     { ASPEED_CLK_MPLL, "mpll" },
12115d05f4fcSRyan Chen 	{ ASPEED_CLK_APLL, "apll" },     { ASPEED_CLK_EPLL, "epll" },
12125d05f4fcSRyan Chen 	{ ASPEED_CLK_DPLL, "dpll" },     { ASPEED_CLK_AHB, "hclk" },
12135d05f4fcSRyan Chen 	{ ASPEED_CLK_APB1, "pclk1" },    { ASPEED_CLK_APB2, "pclk2" },
12145d05f4fcSRyan Chen 	{ ASPEED_CLK_BCLK, "bclk" },     { ASPEED_CLK_UARTX, "uxclk" },
1215def99fcbSryan_chen 	{ ASPEED_CLK_HUARTX, "huxclk" },
1216d35ac78cSryan_chen };
1217d35ac78cSryan_chen 
1218d35ac78cSryan_chen int soc_clk_dump(void)
1219d35ac78cSryan_chen {
1220d35ac78cSryan_chen 	struct udevice *dev;
1221d35ac78cSryan_chen 	struct clk clk;
1222d35ac78cSryan_chen 	unsigned long rate;
1223d35ac78cSryan_chen 	int i, ret;
1224d35ac78cSryan_chen 
12255d05f4fcSRyan Chen 	ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu),
12265d05f4fcSRyan Chen 					  &dev);
1227d35ac78cSryan_chen 	if (ret)
1228d35ac78cSryan_chen 		return ret;
1229d35ac78cSryan_chen 
1230d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
1231d35ac78cSryan_chen 
1232d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
1233d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
1234d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
1235d35ac78cSryan_chen 		if (ret < 0) {
1236d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
1237d35ac78cSryan_chen 			continue;
1238d35ac78cSryan_chen 		}
1239d35ac78cSryan_chen 
1240d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
1241d35ac78cSryan_chen 		rate = ret;
1242d35ac78cSryan_chen 
1243d35ac78cSryan_chen 		clk_free(&clk);
1244d35ac78cSryan_chen 
1245d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
1246d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
1247d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
1248d35ac78cSryan_chen 			continue;
1249d35ac78cSryan_chen 		}
1250d35ac78cSryan_chen 		if (ret < 0) {
12515d05f4fcSRyan Chen 			printf("%s %lu: get_rate err: %d\n", __func__,
12525d05f4fcSRyan Chen 			       aspeed_clk_names[i].id, ret);
1253d35ac78cSryan_chen 			continue;
1254d35ac78cSryan_chen 		}
1255d35ac78cSryan_chen 
12565d05f4fcSRyan Chen 		printf("%s(%3lu):\t%lu\n", aspeed_clk_names[i].name,
12575d05f4fcSRyan Chen 		       aspeed_clk_names[i].id, rate);
1258d35ac78cSryan_chen 	}
1259d35ac78cSryan_chen 
1260d35ac78cSryan_chen 	return 0;
1261d35ac78cSryan_chen }
1262d35ac78cSryan_chen #endif
1263d35ac78cSryan_chen 
1264d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
12655d05f4fcSRyan Chen 	{
12665d05f4fcSRyan Chen 		.compatible = "aspeed,ast2600-scu",
12675d05f4fcSRyan Chen 	},
1268550e691bSryan_chen 	{}
1269550e691bSryan_chen };
1270550e691bSryan_chen 
1271aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
1272aa36597fSDylan Hung 	.name = "aspeed_scu",
1273550e691bSryan_chen 	.id = UCLASS_CLK,
1274d6e349c7Sryan_chen 	.of_match = ast2600_clk_ids,
1275f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
1276f9aa0ee1Sryan_chen 	.ops = &ast2600_clk_ops,
1277d6e349c7Sryan_chen 	.bind = ast2600_clk_bind,
1278d6e349c7Sryan_chen 	.probe = ast2600_clk_probe,
1279550e691bSryan_chen };
1280