xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision fc9f12e6e609f943146e9cd6cdab6b6765bbf3b2)
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 
21ed30249cSDylan Hung #define MAC_DEF_DELAY_1G	0x00410410
2254f9cba1SDylan Hung #define MAC_DEF_DELAY_100M	0x00410410
2354f9cba1SDylan Hung #define MAC_DEF_DELAY_10M	0x00410410
2454f9cba1SDylan Hung 
2554f9cba1SDylan Hung #define MAC34_DEF_DELAY_1G	0x00104208
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[] = {
157d812df15Sryan_chen 	2, 2, 3, 5,
158d812df15Sryan_chen };
159d812df15Sryan_chen 
160d812df15Sryan_chen static u32 ast2600_a1_axi_ahb_div_table[] = {
161d812df15Sryan_chen 	4, 6, 2, 4,
162d812df15Sryan_chen };
163d812df15Sryan_chen 
164d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
165d812df15Sryan_chen {
166d812df15Sryan_chen 	u32 hw_rev = readl(&scu->chip_id0);
167d812df15Sryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1);
168d812df15Sryan_chen 	u32 axi_div = 1;
169d812df15Sryan_chen 	u32 ahb_div = 0;
170d812df15Sryan_chen 	u32 rate = 0;
171d812df15Sryan_chen 
172c29e1cc8Sryan_chen 	if(hwstrap1 & BIT(16))
173d812df15Sryan_chen 		axi_div = 1;
174d812df15Sryan_chen 	else
175d812df15Sryan_chen 		axi_div = 2;
176d812df15Sryan_chen 
177d812df15Sryan_chen 	if (hw_rev & BIT(16))
178d812df15Sryan_chen 		ahb_div = ast2600_a1_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
179d812df15Sryan_chen 	else
180d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
181d812df15Sryan_chen 
182bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
183d812df15Sryan_chen 
1842717883aSryan_chen 	return (rate / axi_div / ahb_div);
1852717883aSryan_chen }
1862717883aSryan_chen 
1876fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = {
1882717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
1892717883aSryan_chen };
1902717883aSryan_chen 
1916fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = {
1926fa1ef3dSryan_chen 	2, 4, 6, 8, 10, 12, 14, 16,
1936fa1ef3dSryan_chen };
1946fa1ef3dSryan_chen 
1956fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu)
1962717883aSryan_chen {
1972717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
1986fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)];
199bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
2002717883aSryan_chen 
2012717883aSryan_chen 	return (rate / apb_div);
202d812df15Sryan_chen }
203d812df15Sryan_chen 
2046fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu)
2056fa1ef3dSryan_chen {
2066fa1ef3dSryan_chen 	u32 clk_sel4 = readl(&scu->clk_sel4);
2076fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)];
2086fa1ef3dSryan_chen 	u32 rate = ast2600_get_hclk(scu);
2096fa1ef3dSryan_chen 
2106fa1ef3dSryan_chen 	return (rate / apb_div);
2116fa1ef3dSryan_chen }
2126fa1ef3dSryan_chen 
21327881d20Sryan_chen static u32 ast2600_get_uxclk_rate(struct ast2600_scu *scu)
214d6e349c7Sryan_chen {
21527881d20Sryan_chen 	u32 clk_in = 0;
21627881d20Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel4);
217550e691bSryan_chen 
21827881d20Sryan_chen 	uxclk_sel &= 0x3;
21927881d20Sryan_chen 	switch(uxclk_sel) {
22027881d20Sryan_chen 		case 0:
22127881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
22227881d20Sryan_chen 			break;
22327881d20Sryan_chen 		case 1:
22427881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
22527881d20Sryan_chen 			break;
22627881d20Sryan_chen 		case 2:
22727881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
22827881d20Sryan_chen 			break;
22927881d20Sryan_chen 		case 3:
23027881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
23127881d20Sryan_chen 			break;
23227881d20Sryan_chen 	}
233d6e349c7Sryan_chen 
23427881d20Sryan_chen 	return clk_in;
23527881d20Sryan_chen }
23627881d20Sryan_chen 
23727881d20Sryan_chen static u32 ast2600_get_huxclk_rate(struct ast2600_scu *scu)
23827881d20Sryan_chen {
23927881d20Sryan_chen 	u32 clk_in = 0;
24027881d20Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel4);
24127881d20Sryan_chen 
24227881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
24327881d20Sryan_chen 	switch(huclk_sel) {
24427881d20Sryan_chen 		case 0:
24527881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
24627881d20Sryan_chen 			break;
24727881d20Sryan_chen 		case 1:
24827881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
24927881d20Sryan_chen 			break;
25027881d20Sryan_chen 		case 2:
25127881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
25227881d20Sryan_chen 			break;
25327881d20Sryan_chen 		case 3:
25427881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
25527881d20Sryan_chen 			break;
25627881d20Sryan_chen 	}
25727881d20Sryan_chen 
25827881d20Sryan_chen 	return clk_in;
25927881d20Sryan_chen }
26027881d20Sryan_chen 
26127881d20Sryan_chen static u32 ast2600_get_uart_from_uxclk_rate(struct ast2600_scu *scu)
26227881d20Sryan_chen {
26327881d20Sryan_chen 	u32 clk_in = ast2600_get_uxclk_rate(scu);
26427881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
26527881d20Sryan_chen 	unsigned int mult, div;
26627881d20Sryan_chen 
26727881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
26827881d20Sryan_chen 	u32 r = div_reg & 0xff;
26927881d20Sryan_chen 
27027881d20Sryan_chen 	mult = r;
27127881d20Sryan_chen 	div = (n * 4);
27227881d20Sryan_chen 	return (clk_in * mult)/div;
27327881d20Sryan_chen }
27427881d20Sryan_chen 
27527881d20Sryan_chen static u32 ast2600_get_uart_from_huxclk_rate(struct ast2600_scu *scu)
27627881d20Sryan_chen {
27727881d20Sryan_chen 	u32 clk_in = ast2600_get_huxclk_rate(scu);
27827881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
27927881d20Sryan_chen 
28027881d20Sryan_chen 	unsigned int mult, div;
28127881d20Sryan_chen 
28227881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
28327881d20Sryan_chen 	u32 r = div_reg & 0xff;
28427881d20Sryan_chen 
28527881d20Sryan_chen 	mult = r;
28627881d20Sryan_chen 	div = (n * 4);
28727881d20Sryan_chen 	return (clk_in * mult)/div;
28827881d20Sryan_chen }
28927881d20Sryan_chen 
290f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
291f51926eeSryan_chen {
292f51926eeSryan_chen 	u32 clkin = 0;
293f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
294f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
295f51926eeSryan_chen 
296f51926eeSryan_chen 	if(clk_sel & BIT(8)) {
297f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
298f51926eeSryan_chen 	} else {
29910069884Sryan_chen 		clkin = ast2600_get_hclk(scu);
300f51926eeSryan_chen 	}
301f51926eeSryan_chen 	div = (div + 1) << 1;
302f51926eeSryan_chen 
303f51926eeSryan_chen 	return (clkin / div);
304f51926eeSryan_chen }
305f51926eeSryan_chen 
306f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
307f51926eeSryan_chen {
308bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
309f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
310f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
311f51926eeSryan_chen 
312f51926eeSryan_chen 	div = (div + 1) << 2;
313f51926eeSryan_chen 
314f51926eeSryan_chen 	return (clkin / div);
315f51926eeSryan_chen }
316f51926eeSryan_chen 
317f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
31827881d20Sryan_chen {
31927881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
32027881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
32127881d20Sryan_chen 	ulong uart_clk = 0;
32227881d20Sryan_chen 
32327881d20Sryan_chen 	switch(uart_idx) {
32427881d20Sryan_chen 		case 1:
32527881d20Sryan_chen 		case 2:
32627881d20Sryan_chen 		case 3:
32727881d20Sryan_chen 		case 4:
32827881d20Sryan_chen 		case 6:
32927881d20Sryan_chen 			if(uart_sel & BIT(uart_idx - 1))
33027881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
331550e691bSryan_chen 			else
33227881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
33327881d20Sryan_chen 			break;
33427881d20Sryan_chen 		case 5: //24mhz is come form usb phy 48Mhz
33527881d20Sryan_chen 			{
33627881d20Sryan_chen 			u8 uart5_clk_sel = 0;
33727881d20Sryan_chen 			//high bit
33827881d20Sryan_chen 			if (readl(&scu->misc_ctrl1) & BIT(12))
33927881d20Sryan_chen 				uart5_clk_sel = 0x2;
34027881d20Sryan_chen 			else
34127881d20Sryan_chen 				uart5_clk_sel = 0x0;
342550e691bSryan_chen 
34327881d20Sryan_chen 			if (readl(&scu->clk_sel2) & BIT(14))
34427881d20Sryan_chen 				uart5_clk_sel |= 0x1;
345550e691bSryan_chen 
34627881d20Sryan_chen 			switch(uart5_clk_sel) {
34727881d20Sryan_chen 				case 0:
34827881d20Sryan_chen 					uart_clk = 24000000;
34927881d20Sryan_chen 					break;
35027881d20Sryan_chen 				case 1:
35127881d20Sryan_chen 					uart_clk = 0;
35227881d20Sryan_chen 					break;
35327881d20Sryan_chen 				case 2:
35427881d20Sryan_chen 					uart_clk = 24000000/13;
35527881d20Sryan_chen 					break;
35627881d20Sryan_chen 				case 3:
35727881d20Sryan_chen 					uart_clk = 192000000/13;
35827881d20Sryan_chen 					break;
35927881d20Sryan_chen 			}
36027881d20Sryan_chen 			}
36127881d20Sryan_chen 			break;
36227881d20Sryan_chen 		case 7:
36327881d20Sryan_chen 		case 8:
36427881d20Sryan_chen 		case 9:
36527881d20Sryan_chen 		case 10:
36627881d20Sryan_chen 		case 11:
36727881d20Sryan_chen 		case 12:
36827881d20Sryan_chen 		case 13:
36927881d20Sryan_chen 			if(uart_sel5 & BIT(uart_idx - 1))
37027881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
37127881d20Sryan_chen 			else
37227881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
37327881d20Sryan_chen 			break;
37427881d20Sryan_chen 	}
37527881d20Sryan_chen 
37627881d20Sryan_chen 	return uart_clk;
377550e691bSryan_chen }
378550e691bSryan_chen 
379feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
380feb42054Sryan_chen {
381feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
382feb42054Sryan_chen 	ulong rate = 0;
383feb42054Sryan_chen 
384feb42054Sryan_chen 	switch (clk->id) {
385feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
386bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
387bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
388d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
389bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
390d812df15Sryan_chen 		break;
391feb42054Sryan_chen 	case ASPEED_CLK_AHB:
392feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
393feb42054Sryan_chen 		break;
3946fa1ef3dSryan_chen 	case ASPEED_CLK_APB1:
3956fa1ef3dSryan_chen 		rate = ast2600_get_pclk1(priv->scu);
3966fa1ef3dSryan_chen 		break;
3976fa1ef3dSryan_chen 	case ASPEED_CLK_APB2:
3986fa1ef3dSryan_chen 		rate = ast2600_get_pclk2(priv->scu);
399feb42054Sryan_chen 		break;
400bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
401bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
402bbbfb0c5Sryan_chen 		break;
403feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
404feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
405feb42054Sryan_chen 		break;
406feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
407feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
408feb42054Sryan_chen 		break;
409feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
410feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
411feb42054Sryan_chen 		break;
412feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
413feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
414feb42054Sryan_chen 		break;
415feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
416feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
417feb42054Sryan_chen 		break;
418f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
419f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
420f51926eeSryan_chen 		break;
421f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
422f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
423f51926eeSryan_chen 		break;
424feb42054Sryan_chen 	default:
425d812df15Sryan_chen 		pr_debug("can't get clk rate \n");
426feb42054Sryan_chen 		return -ENOENT;
427d812df15Sryan_chen 		break;
428feb42054Sryan_chen 	}
429feb42054Sryan_chen 
430feb42054Sryan_chen 	return rate;
431feb42054Sryan_chen }
432feb42054Sryan_chen 
433577fcdaeSDylan Hung /**
434577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
435577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
436577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
437550e691bSryan_chen  *
438577fcdaeSDylan Hung  * The function caller shall fill "pll->in" and "pll->out", then this function
439577fcdaeSDylan Hung  * will search the lookup table to find a valid PLL divider configuration.
440550e691bSryan_chen  */
441577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
442550e691bSryan_chen {
443577fcdaeSDylan Hung 	u32 i;
444577fcdaeSDylan Hung 	bool is_found = false;
445550e691bSryan_chen 
446577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
447577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
448577fcdaeSDylan Hung 		if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) {
449577fcdaeSDylan Hung 			is_found = true;
450577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
451577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
452577fcdaeSDylan Hung 			break;
453550e691bSryan_chen 		}
454550e691bSryan_chen 	}
455577fcdaeSDylan Hung 	return is_found;
456550e691bSryan_chen }
457fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu,
458fd52be0bSDylan Hung 				 struct ast2600_pll_cfg *p_cfg, int pll_idx)
459fd52be0bSDylan Hung {
460fd52be0bSDylan Hung 	u32 addr, addr_ext;
461fd52be0bSDylan Hung 	u32 reg;
462550e691bSryan_chen 
463fd52be0bSDylan Hung 	switch (pll_idx) {
464fd52be0bSDylan Hung 	case ASPEED_CLK_HPLL:
465fd52be0bSDylan Hung 		addr = (u32)(&scu->h_pll_param);
466fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->h_pll_ext_param);
467fd52be0bSDylan Hung 		break;
468fd52be0bSDylan Hung 	case ASPEED_CLK_MPLL:
469fd52be0bSDylan Hung 		addr = (u32)(&scu->m_pll_param);
470fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->m_pll_ext_param);
471fd52be0bSDylan Hung 		break;
472fd52be0bSDylan Hung 	case ASPEED_CLK_DPLL:
473fd52be0bSDylan Hung 		addr = (u32)(&scu->d_pll_param);
474fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->d_pll_ext_param);
475fd52be0bSDylan Hung 		break;
476fd52be0bSDylan Hung 	case ASPEED_CLK_EPLL:
477fd52be0bSDylan Hung 		addr = (u32)(&scu->e_pll_param);
478fd52be0bSDylan Hung 		addr_ext = (u32)(&scu->e_pll_ext_param);
479fd52be0bSDylan Hung 		break;
480fd52be0bSDylan Hung 	default:
481fd52be0bSDylan Hung 		debug("unknown PLL index\n");
482fd52be0bSDylan Hung 		return 1;
483fd52be0bSDylan Hung 	}
484fd52be0bSDylan Hung 
485fd52be0bSDylan Hung 	p_cfg->reg.b.bypass = 0;
486fd52be0bSDylan Hung 	p_cfg->reg.b.off = 1;
487fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 1;
488fd52be0bSDylan Hung 
489fd52be0bSDylan Hung 	reg = readl(addr);
490fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
491fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
492fd52be0bSDylan Hung 	writel(reg, addr);
493fd52be0bSDylan Hung 
494fd52be0bSDylan Hung 	/* write extend parameter */
495fd52be0bSDylan Hung 	writel(p_cfg->ext_reg, addr_ext);
496fd52be0bSDylan Hung 	udelay(100);
497fd52be0bSDylan Hung 	p_cfg->reg.b.off = 0;
498fd52be0bSDylan Hung 	p_cfg->reg.b.reset = 0;
499fd52be0bSDylan Hung 	reg &= ~GENMASK(25, 0);
500fd52be0bSDylan Hung 	reg |= p_cfg->reg.w;
501fd52be0bSDylan Hung 	writel(reg, addr);
502fd52be0bSDylan Hung 
503fd52be0bSDylan Hung 	/* polling PLL lock status */
504fd52be0bSDylan Hung 	while(0 == (readl(addr_ext) & BIT(31)));
505fd52be0bSDylan Hung 
506fd52be0bSDylan Hung 	return 0;
507fd52be0bSDylan Hung }
508feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
509550e691bSryan_chen {
510577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
511550e691bSryan_chen 
512577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
513577fcdaeSDylan Hung 	mpll.out = rate;
514577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&mpll)) {
515577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
516577fcdaeSDylan Hung 		return 0;
517577fcdaeSDylan Hung 	}
518fd52be0bSDylan Hung 	ast2600_configure_pll(scu, &(mpll.cfg), ASPEED_CLK_MPLL);
519577fcdaeSDylan Hung 
520cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
521d6e349c7Sryan_chen }
522d6e349c7Sryan_chen 
523d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
524550e691bSryan_chen {
525f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
526550e691bSryan_chen 
527550e691bSryan_chen 	ulong new_rate;
528550e691bSryan_chen 	switch (clk->id) {
529f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
530feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
531550e691bSryan_chen 		break;
532550e691bSryan_chen 	default:
533550e691bSryan_chen 		return -ENOENT;
534550e691bSryan_chen 	}
535550e691bSryan_chen 
536550e691bSryan_chen 	return new_rate;
537550e691bSryan_chen }
538feb42054Sryan_chen 
539f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
540f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
541f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
542f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
543f9aa0ee1Sryan_chen 
544cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu)
545cc476ffcSDylan Hung {
546cc476ffcSDylan Hung 	u32 clksel;
5474760b3f8SDylan Hung 	u32 clkdelay;
548cc476ffcSDylan Hung 
549577fcdaeSDylan Hung 	struct ast2600_pll_desc epll;
550cc476ffcSDylan Hung 
551577fcdaeSDylan Hung 	epll.in = AST2600_CLK_IN;
552577fcdaeSDylan Hung 	epll.out = 1000000000;
553577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&epll)) {
554577fcdaeSDylan Hung 		printf(
555577fcdaeSDylan Hung 		    "error!! unable to find valid ETHNET MAC clock setting\n");
556577fcdaeSDylan Hung 		debug("%s: epll cfg = 0x%08x 0x%08x\n", __func__,
557577fcdaeSDylan Hung 		      epll.cfg.reg.w, epll.cfg.ext_reg);
558577fcdaeSDylan Hung 		debug("%s: epll cfg = %02x %02x %02x\n", __func__,
559577fcdaeSDylan Hung 		      epll.cfg.reg.b.m, epll.cfg.reg.b.n, epll.cfg.reg.b.p);
560577fcdaeSDylan Hung 		return 0;
561577fcdaeSDylan Hung 	}
562fd52be0bSDylan Hung 	ast2600_configure_pll(scu, &(epll.cfg), ASPEED_CLK_EPLL);
563577fcdaeSDylan Hung 
564cc476ffcSDylan Hung 	/* select MAC#1 and MAC#2 clock source = EPLL / 8 */
565cc476ffcSDylan Hung 	clksel = readl(&scu->clk_sel2);
566cc476ffcSDylan Hung 	clksel &= ~BIT(23);
567cc476ffcSDylan Hung 	clksel |= 0x7 << 20;
568cc476ffcSDylan Hung 	writel(clksel, &scu->clk_sel2);
569cc476ffcSDylan Hung 
5704760b3f8SDylan Hung 	/*
5714760b3f8SDylan Hung 	BIT(31): select RGMII 125M from internal source
5724760b3f8SDylan Hung 	BIT(28): RGMII 125M output enable
5734760b3f8SDylan Hung 	BIT(25:0): 1G default delay
5744760b3f8SDylan Hung 	*/
5754760b3f8SDylan Hung 	clkdelay = MAC_DEF_DELAY_1G | BIT(31) | BIT(28);
5764760b3f8SDylan Hung 	writel(clkdelay, &scu->mac12_clk_delay);
5774760b3f8SDylan Hung 
5784760b3f8SDylan Hung 	/* set 100M/10M default delay */
5794760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M);
5804760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M);
581cc476ffcSDylan Hung 
582ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
583894c19cfSDylan Hung 	clksel = readl(&scu->clk_sel1);
584894c19cfSDylan Hung 	clksel &= ~GENMASK(18, 16);
585ed30249cSDylan Hung 	clksel |= 0x2 << 16;
586894c19cfSDylan Hung 	writel(clksel, &scu->clk_sel1);
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 	u32 reg;
59454f9cba1SDylan Hung 
59554f9cba1SDylan Hung 	ast2600_configure_mac12_clk(scu);
59654f9cba1SDylan Hung 
59754f9cba1SDylan Hung 	/*
59854f9cba1SDylan Hung 	BIT[31]   RGMII 125M source: 0 = from IO pin
59954f9cba1SDylan Hung 	BIT[25:0] MAC 1G delay
60054f9cba1SDylan Hung 	*/
60154f9cba1SDylan Hung 	reg = readl(&scu->mac34_clk_delay);
60254f9cba1SDylan Hung 	reg &= ~(BIT(31) | GENMASK(25, 0));
60354f9cba1SDylan Hung 	reg |= MAC34_DEF_DELAY_1G;
60454f9cba1SDylan Hung 	writel(reg, &scu->mac34_clk_delay);
60554f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M);
60654f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M);
60754f9cba1SDylan Hung 
60854f9cba1SDylan Hung 	/* clock source seletion and divider */
60954f9cba1SDylan Hung 	reg = readl(&scu->clk_sel4);
61054f9cba1SDylan Hung 	reg &= ~GENMASK(26, 24);	/* MAC AHB = HCLK / 2 */
61154f9cba1SDylan Hung 	reg &= ~GENMASK(18, 16);
61254f9cba1SDylan Hung 	reg |= 0x3 << 16;		/* RMII 50M = SLICLK_200M / 4 */
61354f9cba1SDylan Hung 	writel(reg, &scu->clk_sel4);
61454f9cba1SDylan Hung 
61554f9cba1SDylan Hung 	/* set driving strength */
61654f9cba1SDylan Hung 	reg = readl(&scu->pinmux_ctrl16);
61754f9cba1SDylan Hung 	reg &= GENMASK(3, 0);
61854f9cba1SDylan Hung 	reg |= (0x2 << 0) | (0x2 << 2);
61954f9cba1SDylan Hung 	writel(reg, &scu->pinmux_ctrl16);
62054f9cba1SDylan Hung 
62154f9cba1SDylan Hung 	return 0;
62254f9cba1SDylan Hung }
62354f9cba1SDylan Hung #if 0
62454f9cba1SDylan Hung /**
6255b5c3d44SDylan Hung  * ast2600 RGMII clock source tree
62654f9cba1SDylan Hung  *
62754f9cba1SDylan Hung  *    125M from external PAD -------->|\
62854f9cba1SDylan Hung  *    HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
62954f9cba1SDylan Hung  *            | |---->| divider |---->|/                             +
63054f9cba1SDylan Hung  *    EPLL -->|/                                                     |
63154f9cba1SDylan Hung  *                                                                   |
63254f9cba1SDylan Hung  *    +---------<-----------|PAD output enable|<---------------------+
63354f9cba1SDylan Hung  *    |
63454f9cba1SDylan Hung  *    +--->|PAD input enable|----->|\
63554f9cba1SDylan Hung  *                                 | |----> RGMII 125M for MAC#3 & MAC#4
63654f9cba1SDylan Hung  *    SLICLK 200M -->|divider|---->|/
6375b5c3d44SDylan Hung  *
6385b5c3d44SDylan Hung  *
6395b5c3d44SDylan Hung  * ast2600 RMII/NCSI clock source tree
6405b5c3d44SDylan Hung  *
6415b5c3d44SDylan Hung  *    HPLL -->|\
6425b5c3d44SDylan Hung  *            | |---->| divider |----> RMII 50M for MAC#1 & MAC#2
6435b5c3d44SDylan Hung  *    EPLL -->|/
6445b5c3d44SDylan Hung  *
6455b5c3d44SDylan Hung  *    HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4
64654f9cba1SDylan Hung */
64754f9cba1SDylan Hung struct ast2600_rgmii_clk_config {
64854f9cba1SDylan Hung 	u32 mac_1_2_src;	/* 0=external PAD, 1=internal PLL */
64954f9cba1SDylan Hung 	u32 int_clk_src;	/* 0=EPLL, 1=HPLL */
65054f9cba1SDylan Hung 	u32 int_clk_div;
65154f9cba1SDylan Hung 
65254f9cba1SDylan Hung 	u32 mac_3_4_src;	/* 0=external PAD, 1=SLICLK */
65354f9cba1SDylan Hung 	u32 sli_clk_div;	/* reserved */
65454f9cba1SDylan Hung };
65554f9cba1SDylan Hung 
65654f9cba1SDylan Hung static void ast2600_init_rgmii_clk(struct ast2600_scu *scu, int index)
65754f9cba1SDylan Hung {
65854f9cba1SDylan Hung 	debug("%s not ready\n", __func__);
65954f9cba1SDylan Hung }
66054f9cba1SDylan Hung 
66154f9cba1SDylan Hung static void ast2600_init_rmii_clk(struct ast2600_scu *scu, int index)
66254f9cba1SDylan Hung {
66354f9cba1SDylan Hung 	debug("%s not ready\n", __func__);
66454f9cba1SDylan Hung }
66554f9cba1SDylan Hung #endif
666f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
667f9aa0ee1Sryan_chen {
668f9aa0ee1Sryan_chen 	u32 reset_bit;
669f9aa0ee1Sryan_chen 	u32 clkstop_bit;
670f9aa0ee1Sryan_chen 
671cc476ffcSDylan Hung 	if (index < 3)
672cc476ffcSDylan Hung 		ast2600_configure_mac12_clk(scu);
673cc476ffcSDylan Hung 	else
674cc476ffcSDylan Hung 		ast2600_configure_mac34_clk(scu);
675f9aa0ee1Sryan_chen 
676f9aa0ee1Sryan_chen 	switch (index) {
677f9aa0ee1Sryan_chen 	case 1:
678f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
679f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
680f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
681f9aa0ee1Sryan_chen 		udelay(100);
682f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
683f9aa0ee1Sryan_chen 		mdelay(10);
684f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
685f9aa0ee1Sryan_chen 
686f9aa0ee1Sryan_chen 		break;
687f9aa0ee1Sryan_chen 	case 2:
688f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
689f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
690f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
691f9aa0ee1Sryan_chen 		udelay(100);
692f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
693f9aa0ee1Sryan_chen 		mdelay(10);
694f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
695f9aa0ee1Sryan_chen 		break;
696f9aa0ee1Sryan_chen 	case 3:
697f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
698f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
699f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
700f9aa0ee1Sryan_chen 		udelay(100);
701f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
702f9aa0ee1Sryan_chen 		mdelay(10);
703f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
704f9aa0ee1Sryan_chen 		break;
705f9aa0ee1Sryan_chen 	case 4:
706f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
707f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
708f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
709f9aa0ee1Sryan_chen 		udelay(100);
710f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
711f9aa0ee1Sryan_chen 		mdelay(10);
712f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
713f9aa0ee1Sryan_chen 		break;
714f9aa0ee1Sryan_chen 	default:
715f9aa0ee1Sryan_chen 		return -EINVAL;
716f9aa0ee1Sryan_chen 	}
717f9aa0ee1Sryan_chen 
718f9aa0ee1Sryan_chen 	return 0;
719f9aa0ee1Sryan_chen }
720550e691bSryan_chen 
721f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
722f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
723f51926eeSryan_chen {
724f51926eeSryan_chen 	u32 reset_bit;
725f51926eeSryan_chen 	u32 clkstop_bit;
726f51926eeSryan_chen 
727f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
728f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
729f51926eeSryan_chen 
730*fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
731*fc9f12e6Sryan_chen 
732f51926eeSryan_chen 	udelay(100);
733f51926eeSryan_chen 	//enable clk
734f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
735f51926eeSryan_chen 	mdelay(10);
736*fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
737f51926eeSryan_chen 
738f51926eeSryan_chen 	return 0;
739f51926eeSryan_chen }
740f51926eeSryan_chen 
741f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31
742f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
743f51926eeSryan_chen #define SCU_CLK_SD_DIV(x)			(x << 28)
744f51926eeSryan_chen 
745f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
746f51926eeSryan_chen {
747f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
748f51926eeSryan_chen 	u32 enableclk_bit;
749f51926eeSryan_chen 
750f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
751f51926eeSryan_chen 
752*fc9f12e6Sryan_chen 	//default use apll for clock source 800/4 = 200 : controller max is 200mhz
753f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
754*fc9f12e6Sryan_chen 	clk_sel |= SCU_CLK_SD_DIV(1) | BIT(8);
755f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
756f51926eeSryan_chen 
757f51926eeSryan_chen 	//enable clk
758f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
759f51926eeSryan_chen 
760f51926eeSryan_chen 	return 0;
761f51926eeSryan_chen }
762f51926eeSryan_chen 
763f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
764f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
765f51926eeSryan_chen {
766f51926eeSryan_chen 	u32 reset_bit;
767f51926eeSryan_chen 	u32 clkstop_bit;
768f51926eeSryan_chen 
769f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
770f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
771f51926eeSryan_chen 
772*fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_ctrl1);
773f51926eeSryan_chen 	udelay(100);
774f51926eeSryan_chen 	//enable clk
775f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
776f51926eeSryan_chen 	mdelay(10);
777*fc9f12e6Sryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
778f51926eeSryan_chen 
779f51926eeSryan_chen 	return 0;
780f51926eeSryan_chen }
781f51926eeSryan_chen 
782f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15
783f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
784f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x)			(x << 12)
785f51926eeSryan_chen 
786f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
787f51926eeSryan_chen {
788f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
789f51926eeSryan_chen 	u32 enableclk_bit;
790f51926eeSryan_chen 
791d0bdd5f3Sryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC);
792f51926eeSryan_chen 
793f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
794f51926eeSryan_chen 	clk_sel |= SCU_CLK_SD_DIV(1);
795f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel1);
796f51926eeSryan_chen 
797f51926eeSryan_chen 	//enable clk
798f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
799f51926eeSryan_chen 
800f51926eeSryan_chen 	return 0;
801f51926eeSryan_chen }
802f51926eeSryan_chen 
803d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
804550e691bSryan_chen {
805f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
806550e691bSryan_chen 
807550e691bSryan_chen 	switch (clk->id) {
80886f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC1CLK:
80986f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 1);
810550e691bSryan_chen 			break;
81186f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC2CLK:
81286f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 2);
813550e691bSryan_chen 			break;
81477843939Sryan_chen 		case ASPEED_CLK_GATE_MAC3CLK:
81577843939Sryan_chen 			ast2600_configure_mac(priv->scu, 3);
81677843939Sryan_chen 			break;
81777843939Sryan_chen 		case ASPEED_CLK_GATE_MAC4CLK:
81877843939Sryan_chen 			ast2600_configure_mac(priv->scu, 4);
81977843939Sryan_chen 			break;
820f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDCLK:
821f51926eeSryan_chen 			ast2600_enable_sdclk(priv->scu);
822f51926eeSryan_chen 			break;
823f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDEXTCLK:
824f51926eeSryan_chen 			ast2600_enable_extsdclk(priv->scu);
825f51926eeSryan_chen 			break;
826f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCCLK:
827f51926eeSryan_chen 			ast2600_enable_emmcclk(priv->scu);
828f51926eeSryan_chen 			break;
829f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCEXTCLK:
830f51926eeSryan_chen 			ast2600_enable_extemmcclk(priv->scu);
831f51926eeSryan_chen 			break;
832550e691bSryan_chen 		default:
833f9aa0ee1Sryan_chen 			pr_debug("can't enable clk \n");
834550e691bSryan_chen 			return -ENOENT;
83577843939Sryan_chen 			break;
836550e691bSryan_chen 	}
837550e691bSryan_chen 
838550e691bSryan_chen 	return 0;
839550e691bSryan_chen }
840550e691bSryan_chen 
841f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
842d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
843d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
844d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
845550e691bSryan_chen };
846550e691bSryan_chen 
847d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
848550e691bSryan_chen {
849f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
850550e691bSryan_chen 
851f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
852f0d895afSryan_chen 	if (IS_ERR(priv->scu))
853f0d895afSryan_chen 		return PTR_ERR(priv->scu);
854550e691bSryan_chen 
855550e691bSryan_chen 	return 0;
856550e691bSryan_chen }
857550e691bSryan_chen 
858d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
859550e691bSryan_chen {
860550e691bSryan_chen 	int ret;
861550e691bSryan_chen 
862550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
863550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
864550e691bSryan_chen 	if (ret)
865550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
866550e691bSryan_chen 
867550e691bSryan_chen 	return 0;
868550e691bSryan_chen }
869550e691bSryan_chen 
870d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK)
871d35ac78cSryan_chen struct aspeed_clks {
872d35ac78cSryan_chen 	ulong id;
873d35ac78cSryan_chen 	const char *name;
874d35ac78cSryan_chen };
875d35ac78cSryan_chen 
876d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
877d35ac78cSryan_chen 	{ ASPEED_CLK_HPLL, "hpll" },
878d35ac78cSryan_chen 	{ ASPEED_CLK_MPLL, "mpll" },
879d35ac78cSryan_chen 	{ ASPEED_CLK_APLL, "apll" },
880d35ac78cSryan_chen 	{ ASPEED_CLK_EPLL, "epll" },
881d35ac78cSryan_chen 	{ ASPEED_CLK_DPLL, "dpll" },
882d35ac78cSryan_chen 	{ ASPEED_CLK_AHB, "hclk" },
8836fa1ef3dSryan_chen 	{ ASPEED_CLK_APB1, "pclk1" },
8846fa1ef3dSryan_chen 	{ ASPEED_CLK_APB2, "pclk2" },
885d35ac78cSryan_chen };
886d35ac78cSryan_chen 
887d35ac78cSryan_chen int soc_clk_dump(void)
888d35ac78cSryan_chen {
889d35ac78cSryan_chen 	struct udevice *dev;
890d35ac78cSryan_chen 	struct clk clk;
891d35ac78cSryan_chen 	unsigned long rate;
892d35ac78cSryan_chen 	int i, ret;
893d35ac78cSryan_chen 
894d35ac78cSryan_chen 	ret = uclass_get_device_by_driver(UCLASS_CLK,
895d35ac78cSryan_chen 					  DM_GET_DRIVER(aspeed_scu), &dev);
896d35ac78cSryan_chen 	if (ret)
897d35ac78cSryan_chen 		return ret;
898d35ac78cSryan_chen 
899d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
900d35ac78cSryan_chen 
901d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
902d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
903d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
904d35ac78cSryan_chen 		if (ret < 0) {
905d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
906d35ac78cSryan_chen 			continue;
907d35ac78cSryan_chen 		}
908d35ac78cSryan_chen 
909d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
910d35ac78cSryan_chen 		rate = ret;
911d35ac78cSryan_chen 
912d35ac78cSryan_chen 		clk_free(&clk);
913d35ac78cSryan_chen 
914d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
915d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
916d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
917d35ac78cSryan_chen 			continue;
918d35ac78cSryan_chen 		}
919d35ac78cSryan_chen 		if (ret < 0) {
920d35ac78cSryan_chen 			printf("%s %lu: get_rate err: %d\n",
921d35ac78cSryan_chen 			       __func__, aspeed_clk_names[i].id, ret);
922d35ac78cSryan_chen 			continue;
923d35ac78cSryan_chen 		}
924d35ac78cSryan_chen 
925d35ac78cSryan_chen 		printf("%s(%3lu):\t%lu\n",
926d35ac78cSryan_chen 		       aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate);
927d35ac78cSryan_chen 	}
928d35ac78cSryan_chen 
929d35ac78cSryan_chen 	return 0;
930d35ac78cSryan_chen }
931d35ac78cSryan_chen #endif
932d35ac78cSryan_chen 
933d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
934d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
935550e691bSryan_chen 	{ }
936550e691bSryan_chen };
937550e691bSryan_chen 
938aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
939aa36597fSDylan Hung 	.name		= "aspeed_scu",
940550e691bSryan_chen 	.id		= UCLASS_CLK,
941d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
942f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
943f9aa0ee1Sryan_chen 	.ops		= &ast2600_clk_ops,
944d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
945d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
946550e691bSryan_chen };
947