xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision d0bdd5f36176ba7eb82494c521bc902697bbe822)
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 {
54577fcdaeSDylan Hung 		unsigned int m : 13;	/* 12:0  */
55577fcdaeSDylan Hung 		unsigned int n : 6;	/* 18:13  */
56577fcdaeSDylan Hung 		unsigned int p : 4;	/* 22:19  */
57577fcdaeSDylan Hung 		unsigned int reserved : 9; /* 31:20  */
58577fcdaeSDylan Hung 	} b;
59577fcdaeSDylan Hung };
60577fcdaeSDylan Hung 
61577fcdaeSDylan Hung struct ast2600_pll_cfg {
62577fcdaeSDylan Hung 	union ast2600_pll_reg reg;
63577fcdaeSDylan Hung 	unsigned int ext_reg;
64577fcdaeSDylan Hung };
65577fcdaeSDylan Hung 
66577fcdaeSDylan Hung struct ast2600_pll_desc {
67577fcdaeSDylan Hung 	u32 in;
68577fcdaeSDylan Hung 	u32 out;
69577fcdaeSDylan Hung 	struct ast2600_pll_cfg cfg;
70577fcdaeSDylan Hung };
71577fcdaeSDylan Hung 
72577fcdaeSDylan Hung static const struct ast2600_pll_desc ast2600_pll_lookup[] = {
73577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 400000000,
74577fcdaeSDylan Hung     .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 1,
75577fcdaeSDylan Hung     .cfg.ext_reg = 0x31,
76577fcdaeSDylan Hung     },
77577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 200000000,
78577fcdaeSDylan Hung     .cfg.reg.b.m = 127, .cfg.reg.b.n = 0, .cfg.reg.b.p = 15,
79577fcdaeSDylan Hung     .cfg.ext_reg = 0x3f
80577fcdaeSDylan Hung     },
81577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 334000000,
82577fcdaeSDylan Hung     .cfg.reg.b.m = 667, .cfg.reg.b.n = 4, .cfg.reg.b.p = 9,
83577fcdaeSDylan Hung     .cfg.ext_reg = 0x14d
84577fcdaeSDylan Hung     },
85577fcdaeSDylan Hung 
86577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 1000000000,
87577fcdaeSDylan Hung     .cfg.reg.b.m = 119, .cfg.reg.b.n = 2, .cfg.reg.b.p = 0,
88577fcdaeSDylan Hung     .cfg.ext_reg = 0x3d
89577fcdaeSDylan Hung     },
90577fcdaeSDylan Hung 
91577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 50000000,
92577fcdaeSDylan Hung     .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 15,
93577fcdaeSDylan Hung     .cfg.ext_reg = 0x31
94577fcdaeSDylan Hung     },
95550e691bSryan_chen };
96550e691bSryan_chen 
97bbbfb0c5Sryan_chen extern u32 ast2600_get_pll_rate(struct ast2600_scu *scu, int pll_idx)
98550e691bSryan_chen {
99d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
100bbbfb0c5Sryan_chen 	u32 pll_reg = 0;
1019639db61Sryan_chen 	unsigned int mult, div = 1;
102550e691bSryan_chen 
103bbbfb0c5Sryan_chen 	switch(pll_idx) {
104bbbfb0c5Sryan_chen 		case ASPEED_CLK_HPLL:
105bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->h_pll_param);
106bbbfb0c5Sryan_chen 			break;
107bbbfb0c5Sryan_chen 		case ASPEED_CLK_MPLL:
108bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->m_pll_param);
109bbbfb0c5Sryan_chen 			break;
110bbbfb0c5Sryan_chen 		case ASPEED_CLK_DPLL:
111bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->d_pll_param);
112bbbfb0c5Sryan_chen 			break;
113bbbfb0c5Sryan_chen 		case ASPEED_CLK_EPLL:
114bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->e_pll_param);
115bbbfb0c5Sryan_chen 			break;
116bbbfb0c5Sryan_chen 
117bbbfb0c5Sryan_chen 	}
118bbbfb0c5Sryan_chen 	if (pll_reg & BIT(24)) {
1199639db61Sryan_chen 		/* Pass through mode */
1209639db61Sryan_chen 		mult = div = 1;
1219639db61Sryan_chen 	} else {
1229639db61Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */
12375ced45aSDylan Hung 		union ast2600_pll_reg reg;
12475ced45aSDylan Hung 		reg.w = pll_reg;
12575ced45aSDylan Hung 		mult = (reg.b.m + 1) / (reg.b.n + 1);
12675ced45aSDylan Hung 		div = (reg.b.p + 1);
1279639db61Sryan_chen 	}
1289639db61Sryan_chen 	return ((clkin * mult)/div);
129550e691bSryan_chen 
130550e691bSryan_chen }
131550e691bSryan_chen 
1324f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
133550e691bSryan_chen {
134bbbfb0c5Sryan_chen 	u32 clkin = AST2600_CLK_IN;
13539283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
13639283ea7Sryan_chen 	unsigned int mult, div = 1;
137d6e349c7Sryan_chen 
13839283ea7Sryan_chen 	if (apll_reg & BIT(20)) {
139d6e349c7Sryan_chen 		/* Pass through mode */
140d6e349c7Sryan_chen 		mult = div = 1;
141d6e349c7Sryan_chen 	} else {
142bbbfb0c5Sryan_chen 		/* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
14339283ea7Sryan_chen 		u32 m = (apll_reg >> 5) & 0x3f;
14439283ea7Sryan_chen 		u32 od = (apll_reg >> 4) & 0x1;
14539283ea7Sryan_chen 		u32 n = apll_reg & 0xf;
146d6e349c7Sryan_chen 
147bbbfb0c5Sryan_chen 		mult = (2 - od) * (m + 2);
148bbbfb0c5Sryan_chen 		div = n + 1;
149d6e349c7Sryan_chen 	}
150bbbfb0c5Sryan_chen 	return ((clkin * mult)/div);
15139283ea7Sryan_chen }
15239283ea7Sryan_chen 
153d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
154d812df15Sryan_chen 	2, 2, 3, 5,
155d812df15Sryan_chen };
156d812df15Sryan_chen 
157d812df15Sryan_chen static u32 ast2600_a1_axi_ahb_div_table[] = {
158d812df15Sryan_chen 	4, 6, 2, 4,
159d812df15Sryan_chen };
160d812df15Sryan_chen 
161d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
162d812df15Sryan_chen {
163d812df15Sryan_chen 	u32 hw_rev = readl(&scu->chip_id0);
164d812df15Sryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1);
165d812df15Sryan_chen 	u32 axi_div = 1;
166d812df15Sryan_chen 	u32 ahb_div = 0;
167d812df15Sryan_chen 	u32 rate = 0;
168d812df15Sryan_chen 
169c29e1cc8Sryan_chen 	if(hwstrap1 & BIT(16))
170d812df15Sryan_chen 		axi_div = 1;
171d812df15Sryan_chen 	else
172d812df15Sryan_chen 		axi_div = 2;
173d812df15Sryan_chen 
174d812df15Sryan_chen 	if (hw_rev & BIT(16))
175d812df15Sryan_chen 		ahb_div = ast2600_a1_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
176d812df15Sryan_chen 	else
177d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
178d812df15Sryan_chen 
179bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
180d812df15Sryan_chen 
1812717883aSryan_chen 	return (rate / axi_div / ahb_div);
1822717883aSryan_chen }
1832717883aSryan_chen 
1846fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = {
1852717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
1862717883aSryan_chen };
1872717883aSryan_chen 
1886fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = {
1896fa1ef3dSryan_chen 	2, 4, 6, 8, 10, 12, 14, 16,
1906fa1ef3dSryan_chen };
1916fa1ef3dSryan_chen 
1926fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu)
1932717883aSryan_chen {
1942717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
1956fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)];
196bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1972717883aSryan_chen 
1982717883aSryan_chen 	return (rate / apb_div);
199d812df15Sryan_chen }
200d812df15Sryan_chen 
2016fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu)
2026fa1ef3dSryan_chen {
2036fa1ef3dSryan_chen 	u32 clk_sel4 = readl(&scu->clk_sel4);
2046fa1ef3dSryan_chen 	u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)];
2056fa1ef3dSryan_chen 	u32 rate = ast2600_get_hclk(scu);
2066fa1ef3dSryan_chen 
2076fa1ef3dSryan_chen 	return (rate / apb_div);
2086fa1ef3dSryan_chen }
2096fa1ef3dSryan_chen 
21027881d20Sryan_chen static u32 ast2600_get_uxclk_rate(struct ast2600_scu *scu)
211d6e349c7Sryan_chen {
21227881d20Sryan_chen 	u32 clk_in = 0;
21327881d20Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel4);
214550e691bSryan_chen 
21527881d20Sryan_chen 	uxclk_sel &= 0x3;
21627881d20Sryan_chen 	switch(uxclk_sel) {
21727881d20Sryan_chen 		case 0:
21827881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
21927881d20Sryan_chen 			break;
22027881d20Sryan_chen 		case 1:
22127881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
22227881d20Sryan_chen 			break;
22327881d20Sryan_chen 		case 2:
22427881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
22527881d20Sryan_chen 			break;
22627881d20Sryan_chen 		case 3:
22727881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
22827881d20Sryan_chen 			break;
22927881d20Sryan_chen 	}
230d6e349c7Sryan_chen 
23127881d20Sryan_chen 	return clk_in;
23227881d20Sryan_chen }
23327881d20Sryan_chen 
23427881d20Sryan_chen static u32 ast2600_get_huxclk_rate(struct ast2600_scu *scu)
23527881d20Sryan_chen {
23627881d20Sryan_chen 	u32 clk_in = 0;
23727881d20Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel4);
23827881d20Sryan_chen 
23927881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
24027881d20Sryan_chen 	switch(huclk_sel) {
24127881d20Sryan_chen 		case 0:
24227881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
24327881d20Sryan_chen 			break;
24427881d20Sryan_chen 		case 1:
24527881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
24627881d20Sryan_chen 			break;
24727881d20Sryan_chen 		case 2:
24827881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
24927881d20Sryan_chen 			break;
25027881d20Sryan_chen 		case 3:
25127881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
25227881d20Sryan_chen 			break;
25327881d20Sryan_chen 	}
25427881d20Sryan_chen 
25527881d20Sryan_chen 	return clk_in;
25627881d20Sryan_chen }
25727881d20Sryan_chen 
25827881d20Sryan_chen static u32 ast2600_get_uart_from_uxclk_rate(struct ast2600_scu *scu)
25927881d20Sryan_chen {
26027881d20Sryan_chen 	u32 clk_in = ast2600_get_uxclk_rate(scu);
26127881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
26227881d20Sryan_chen 	unsigned int mult, div;
26327881d20Sryan_chen 
26427881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
26527881d20Sryan_chen 	u32 r = div_reg & 0xff;
26627881d20Sryan_chen 
26727881d20Sryan_chen 	mult = r;
26827881d20Sryan_chen 	div = (n * 4);
26927881d20Sryan_chen 	return (clk_in * mult)/div;
27027881d20Sryan_chen }
27127881d20Sryan_chen 
27227881d20Sryan_chen static u32 ast2600_get_uart_from_huxclk_rate(struct ast2600_scu *scu)
27327881d20Sryan_chen {
27427881d20Sryan_chen 	u32 clk_in = ast2600_get_huxclk_rate(scu);
27527881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
27627881d20Sryan_chen 
27727881d20Sryan_chen 	unsigned int mult, div;
27827881d20Sryan_chen 
27927881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
28027881d20Sryan_chen 	u32 r = div_reg & 0xff;
28127881d20Sryan_chen 
28227881d20Sryan_chen 	mult = r;
28327881d20Sryan_chen 	div = (n * 4);
28427881d20Sryan_chen 	return (clk_in * mult)/div;
28527881d20Sryan_chen }
28627881d20Sryan_chen 
287f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
288f51926eeSryan_chen {
289f51926eeSryan_chen 	u32 clkin = 0;
290f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
291f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
292f51926eeSryan_chen 
293f51926eeSryan_chen 	if(clk_sel & BIT(8)) {
294f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
295f51926eeSryan_chen 	} else {
296f51926eeSryan_chen 		clkin = 200 * 1000 * 1000;
297f51926eeSryan_chen 	}
298f51926eeSryan_chen 	div = (div + 1) << 1;
299f51926eeSryan_chen 
300f51926eeSryan_chen 	return (clkin / div);
301f51926eeSryan_chen }
302f51926eeSryan_chen 
303f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
304f51926eeSryan_chen {
305bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
306f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
307f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
308f51926eeSryan_chen 
309f51926eeSryan_chen 	div = (div + 1) << 2;
310f51926eeSryan_chen 
311f51926eeSryan_chen 	return (clkin / div);
312f51926eeSryan_chen }
313f51926eeSryan_chen 
314f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
31527881d20Sryan_chen {
31627881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
31727881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
31827881d20Sryan_chen 	ulong uart_clk = 0;
31927881d20Sryan_chen 
32027881d20Sryan_chen 	switch(uart_idx) {
32127881d20Sryan_chen 		case 1:
32227881d20Sryan_chen 		case 2:
32327881d20Sryan_chen 		case 3:
32427881d20Sryan_chen 		case 4:
32527881d20Sryan_chen 		case 6:
32627881d20Sryan_chen 			if(uart_sel & BIT(uart_idx - 1))
32727881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
328550e691bSryan_chen 			else
32927881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
33027881d20Sryan_chen 			break;
33127881d20Sryan_chen 		case 5: //24mhz is come form usb phy 48Mhz
33227881d20Sryan_chen 			{
33327881d20Sryan_chen 			u8 uart5_clk_sel = 0;
33427881d20Sryan_chen 			//high bit
33527881d20Sryan_chen 			if (readl(&scu->misc_ctrl1) & BIT(12))
33627881d20Sryan_chen 				uart5_clk_sel = 0x2;
33727881d20Sryan_chen 			else
33827881d20Sryan_chen 				uart5_clk_sel = 0x0;
339550e691bSryan_chen 
34027881d20Sryan_chen 			if (readl(&scu->clk_sel2) & BIT(14))
34127881d20Sryan_chen 				uart5_clk_sel |= 0x1;
342550e691bSryan_chen 
34327881d20Sryan_chen 			switch(uart5_clk_sel) {
34427881d20Sryan_chen 				case 0:
34527881d20Sryan_chen 					uart_clk = 24000000;
34627881d20Sryan_chen 					break;
34727881d20Sryan_chen 				case 1:
34827881d20Sryan_chen 					uart_clk = 0;
34927881d20Sryan_chen 					break;
35027881d20Sryan_chen 				case 2:
35127881d20Sryan_chen 					uart_clk = 24000000/13;
35227881d20Sryan_chen 					break;
35327881d20Sryan_chen 				case 3:
35427881d20Sryan_chen 					uart_clk = 192000000/13;
35527881d20Sryan_chen 					break;
35627881d20Sryan_chen 			}
35727881d20Sryan_chen 			}
35827881d20Sryan_chen 			break;
35927881d20Sryan_chen 		case 7:
36027881d20Sryan_chen 		case 8:
36127881d20Sryan_chen 		case 9:
36227881d20Sryan_chen 		case 10:
36327881d20Sryan_chen 		case 11:
36427881d20Sryan_chen 		case 12:
36527881d20Sryan_chen 		case 13:
36627881d20Sryan_chen 			if(uart_sel5 & BIT(uart_idx - 1))
36727881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
36827881d20Sryan_chen 			else
36927881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
37027881d20Sryan_chen 			break;
37127881d20Sryan_chen 	}
37227881d20Sryan_chen 
37327881d20Sryan_chen 	return uart_clk;
374550e691bSryan_chen }
375550e691bSryan_chen 
376feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
377feb42054Sryan_chen {
378feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
379feb42054Sryan_chen 	ulong rate = 0;
380feb42054Sryan_chen 
381feb42054Sryan_chen 	switch (clk->id) {
382feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
383bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
384bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
385d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
386bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
387d812df15Sryan_chen 		break;
388feb42054Sryan_chen 	case ASPEED_CLK_AHB:
389feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
390feb42054Sryan_chen 		break;
3916fa1ef3dSryan_chen 	case ASPEED_CLK_APB1:
3926fa1ef3dSryan_chen 		rate = ast2600_get_pclk1(priv->scu);
3936fa1ef3dSryan_chen 		break;
3946fa1ef3dSryan_chen 	case ASPEED_CLK_APB2:
3956fa1ef3dSryan_chen 		rate = ast2600_get_pclk2(priv->scu);
396feb42054Sryan_chen 		break;
397bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
398bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
399bbbfb0c5Sryan_chen 		break;
400feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
401feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
402feb42054Sryan_chen 		break;
403feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
404feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
405feb42054Sryan_chen 		break;
406feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
407feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
408feb42054Sryan_chen 		break;
409feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
410feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
411feb42054Sryan_chen 		break;
412feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
413feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
414feb42054Sryan_chen 		break;
415f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
416f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
417f51926eeSryan_chen 		break;
418f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
419f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
420f51926eeSryan_chen 		break;
421feb42054Sryan_chen 	default:
422d812df15Sryan_chen 		pr_debug("can't get clk rate \n");
423feb42054Sryan_chen 		return -ENOENT;
424d812df15Sryan_chen 		break;
425feb42054Sryan_chen 	}
426feb42054Sryan_chen 
427feb42054Sryan_chen 	return rate;
428feb42054Sryan_chen }
429feb42054Sryan_chen 
430577fcdaeSDylan Hung /**
431577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
432577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
433577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
434550e691bSryan_chen  *
435577fcdaeSDylan Hung  * The function caller shall fill "pll->in" and "pll->out", then this function
436577fcdaeSDylan Hung  * will search the lookup table to find a valid PLL divider configuration.
437550e691bSryan_chen  */
438577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
439550e691bSryan_chen {
440577fcdaeSDylan Hung 	u32 i;
441577fcdaeSDylan Hung 	bool is_found = false;
442550e691bSryan_chen 
443577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
444577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
445577fcdaeSDylan Hung 		if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) {
446577fcdaeSDylan Hung 			is_found = true;
447577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
448577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
449577fcdaeSDylan Hung 			break;
450550e691bSryan_chen 		}
451550e691bSryan_chen 	}
452577fcdaeSDylan Hung 	return is_found;
453550e691bSryan_chen }
454550e691bSryan_chen 
455feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
456550e691bSryan_chen {
457550e691bSryan_chen 	u32 mpll_reg;
458577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
459550e691bSryan_chen 
460577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
461577fcdaeSDylan Hung 	mpll.out = rate;
462577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&mpll)) {
463577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
464577fcdaeSDylan Hung 		return 0;
465577fcdaeSDylan Hung 	}
466550e691bSryan_chen 
467feb42054Sryan_chen 	mpll_reg = readl(&scu->m_pll_param);
468577fcdaeSDylan Hung 	mpll_reg &= ~GENMASK(22, 0);
469577fcdaeSDylan Hung 	mpll_reg |= mpll.cfg.reg.w;
470feb42054Sryan_chen 	writel(mpll_reg, &scu->m_pll_param);
471550e691bSryan_chen 
472577fcdaeSDylan Hung 	/* write extend parameter */
473577fcdaeSDylan Hung 	writel(mpll.cfg.ext_reg, &scu->m_pll_ext_param);
474577fcdaeSDylan Hung 
475cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
476d6e349c7Sryan_chen }
477d6e349c7Sryan_chen 
478d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
479550e691bSryan_chen {
480f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
481550e691bSryan_chen 
482550e691bSryan_chen 	ulong new_rate;
483550e691bSryan_chen 	switch (clk->id) {
484f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
485feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
486550e691bSryan_chen 		break;
487550e691bSryan_chen 	default:
488550e691bSryan_chen 		return -ENOENT;
489550e691bSryan_chen 	}
490550e691bSryan_chen 
491550e691bSryan_chen 	return new_rate;
492550e691bSryan_chen }
493feb42054Sryan_chen 
494f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
495f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
496f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
497f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
498f9aa0ee1Sryan_chen 
499cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu)
500cc476ffcSDylan Hung {
501cc476ffcSDylan Hung 	u32 epll_reg;
502cc476ffcSDylan Hung 	u32 clksel;
5034760b3f8SDylan Hung 	u32 clkdelay;
504cc476ffcSDylan Hung 
505577fcdaeSDylan Hung 	struct ast2600_pll_desc epll;
506cc476ffcSDylan Hung 
507577fcdaeSDylan Hung 	epll.in = AST2600_CLK_IN;
508577fcdaeSDylan Hung 	epll.out = 1000000000;
509577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&epll)) {
510577fcdaeSDylan Hung 		printf(
511577fcdaeSDylan Hung 		    "error!! unable to find valid ETHNET MAC clock setting\n");
512577fcdaeSDylan Hung 		debug("%s: epll cfg = 0x%08x 0x%08x\n", __func__,
513577fcdaeSDylan Hung 		      epll.cfg.reg.w, epll.cfg.ext_reg);
514577fcdaeSDylan Hung 		debug("%s: epll cfg = %02x %02x %02x\n", __func__,
515577fcdaeSDylan Hung 		      epll.cfg.reg.b.m, epll.cfg.reg.b.n, epll.cfg.reg.b.p);
516577fcdaeSDylan Hung 		return 0;
517577fcdaeSDylan Hung 	}
518577fcdaeSDylan Hung 
519cc476ffcSDylan Hung 	epll_reg = readl(&scu->e_pll_param);
520cc476ffcSDylan Hung 	epll_reg &= ~GENMASK(22, 0);
521577fcdaeSDylan Hung 	epll_reg |= epll.cfg.reg.w;
522cc476ffcSDylan Hung 	writel(epll_reg, &scu->e_pll_param);
523cc476ffcSDylan Hung 
524577fcdaeSDylan Hung 	/* write extend parameter */
525577fcdaeSDylan Hung 	writel(epll.cfg.ext_reg, &scu->e_pll_ext_param);
526577fcdaeSDylan Hung 
527cc476ffcSDylan Hung 	/* select MAC#1 and MAC#2 clock source = EPLL / 8 */
528cc476ffcSDylan Hung 	clksel = readl(&scu->clk_sel2);
529cc476ffcSDylan Hung 	clksel &= ~BIT(23);
530cc476ffcSDylan Hung 	clksel |= 0x7 << 20;
531cc476ffcSDylan Hung 	writel(clksel, &scu->clk_sel2);
532cc476ffcSDylan Hung 
5334760b3f8SDylan Hung 	/*
5344760b3f8SDylan Hung 	BIT(31): select RGMII 125M from internal source
5354760b3f8SDylan Hung 	BIT(28): RGMII 125M output enable
5364760b3f8SDylan Hung 	BIT(25:0): 1G default delay
5374760b3f8SDylan Hung 	*/
5384760b3f8SDylan Hung 	clkdelay = MAC_DEF_DELAY_1G | BIT(31) | BIT(28);
5394760b3f8SDylan Hung 	writel(clkdelay, &scu->mac12_clk_delay);
5404760b3f8SDylan Hung 
5414760b3f8SDylan Hung 	/* set 100M/10M default delay */
5424760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M);
5434760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M);
544cc476ffcSDylan Hung 
545ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
546894c19cfSDylan Hung 	clksel = readl(&scu->clk_sel1);
547894c19cfSDylan Hung 	clksel &= ~GENMASK(18, 16);
548ed30249cSDylan Hung 	clksel |= 0x2 << 16;
549894c19cfSDylan Hung 	writel(clksel, &scu->clk_sel1);
550894c19cfSDylan Hung 
551cc476ffcSDylan Hung 	return 0;
552cc476ffcSDylan Hung }
553cc476ffcSDylan Hung 
55454f9cba1SDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu)
55554f9cba1SDylan Hung {
55654f9cba1SDylan Hung 	u32 reg;
55754f9cba1SDylan Hung 
55854f9cba1SDylan Hung 	ast2600_configure_mac12_clk(scu);
55954f9cba1SDylan Hung 
56054f9cba1SDylan Hung 	/*
56154f9cba1SDylan Hung 	BIT[31]   RGMII 125M source: 0 = from IO pin
56254f9cba1SDylan Hung 	BIT[25:0] MAC 1G delay
56354f9cba1SDylan Hung 	*/
56454f9cba1SDylan Hung 	reg = readl(&scu->mac34_clk_delay);
56554f9cba1SDylan Hung 	reg &= ~(BIT(31) | GENMASK(25, 0));
56654f9cba1SDylan Hung 	reg |= MAC34_DEF_DELAY_1G;
56754f9cba1SDylan Hung 	writel(reg, &scu->mac34_clk_delay);
56854f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M);
56954f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M);
57054f9cba1SDylan Hung 
57154f9cba1SDylan Hung 	/* clock source seletion and divider */
57254f9cba1SDylan Hung 	reg = readl(&scu->clk_sel4);
57354f9cba1SDylan Hung 	reg &= ~GENMASK(26, 24);	/* MAC AHB = HCLK / 2 */
57454f9cba1SDylan Hung 	reg &= ~GENMASK(18, 16);
57554f9cba1SDylan Hung 	reg |= 0x3 << 16;		/* RMII 50M = SLICLK_200M / 4 */
57654f9cba1SDylan Hung 	writel(reg, &scu->clk_sel4);
57754f9cba1SDylan Hung 
57854f9cba1SDylan Hung 	/* set driving strength */
57954f9cba1SDylan Hung 	reg = readl(&scu->pinmux_ctrl16);
58054f9cba1SDylan Hung 	reg &= GENMASK(3, 0);
58154f9cba1SDylan Hung 	reg |= (0x2 << 0) | (0x2 << 2);
58254f9cba1SDylan Hung 	writel(reg, &scu->pinmux_ctrl16);
58354f9cba1SDylan Hung 
58454f9cba1SDylan Hung 	return 0;
58554f9cba1SDylan Hung }
58654f9cba1SDylan Hung #if 0
58754f9cba1SDylan Hung /**
58854f9cba1SDylan Hung  * WIP: ast2600 RGMII clock source tree
58954f9cba1SDylan Hung  *
59054f9cba1SDylan Hung  *    125M from external PAD -------->|\
59154f9cba1SDylan Hung  *    HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
59254f9cba1SDylan Hung  *            | |---->| divider |---->|/                             +
59354f9cba1SDylan Hung  *    EPLL -->|/                                                     |
59454f9cba1SDylan Hung  *                                                                   |
59554f9cba1SDylan Hung  *    +---------<-----------|PAD output enable|<---------------------+
59654f9cba1SDylan Hung  *    |
59754f9cba1SDylan Hung  *    +--->|PAD input enable|----->|\
59854f9cba1SDylan Hung  *                                 | |----> RGMII 125M for MAC#3 & MAC#4
59954f9cba1SDylan Hung  *    SLICLK 200M -->|divider|---->|/
60054f9cba1SDylan Hung */
60154f9cba1SDylan Hung struct ast2600_rgmii_clk_config {
60254f9cba1SDylan Hung 	u32 mac_1_2_src;	/* 0=external PAD, 1=internal PLL */
60354f9cba1SDylan Hung 	u32 int_clk_src;	/* 0=EPLL, 1=HPLL */
60454f9cba1SDylan Hung 	u32 int_clk_div;
60554f9cba1SDylan Hung 
60654f9cba1SDylan Hung 	u32 mac_3_4_src;	/* 0=external PAD, 1=SLICLK */
60754f9cba1SDylan Hung 	u32 sli_clk_div;	/* reserved */
60854f9cba1SDylan Hung };
60954f9cba1SDylan Hung 
61054f9cba1SDylan Hung static void ast2600_init_rgmii_clk(struct ast2600_scu *scu, int index)
61154f9cba1SDylan Hung {
61254f9cba1SDylan Hung 	debug("%s not ready\n", __func__);
61354f9cba1SDylan Hung }
61454f9cba1SDylan Hung 
61554f9cba1SDylan Hung static void ast2600_init_rmii_clk(struct ast2600_scu *scu, int index)
61654f9cba1SDylan Hung {
61754f9cba1SDylan Hung 	debug("%s not ready\n", __func__);
61854f9cba1SDylan Hung }
61954f9cba1SDylan Hung #endif
620f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
621f9aa0ee1Sryan_chen {
622f9aa0ee1Sryan_chen 	u32 reset_bit;
623f9aa0ee1Sryan_chen 	u32 clkstop_bit;
624f9aa0ee1Sryan_chen 
625cc476ffcSDylan Hung 	if (index < 3)
626cc476ffcSDylan Hung 		ast2600_configure_mac12_clk(scu);
627cc476ffcSDylan Hung 	else
628cc476ffcSDylan Hung 		ast2600_configure_mac34_clk(scu);
629f9aa0ee1Sryan_chen 
630f9aa0ee1Sryan_chen 	switch (index) {
631f9aa0ee1Sryan_chen 	case 1:
632f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
633f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
634f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
635f9aa0ee1Sryan_chen 		udelay(100);
636f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
637f9aa0ee1Sryan_chen 		mdelay(10);
638f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
639f9aa0ee1Sryan_chen 
640f9aa0ee1Sryan_chen 		break;
641f9aa0ee1Sryan_chen 	case 2:
642f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
643f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
644f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
645f9aa0ee1Sryan_chen 		udelay(100);
646f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
647f9aa0ee1Sryan_chen 		mdelay(10);
648f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
649f9aa0ee1Sryan_chen 		break;
650f9aa0ee1Sryan_chen 	case 3:
651f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
652f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
653f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
654f9aa0ee1Sryan_chen 		udelay(100);
655f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
656f9aa0ee1Sryan_chen 		mdelay(10);
657f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
658f9aa0ee1Sryan_chen 		break;
659f9aa0ee1Sryan_chen 	case 4:
660f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
661f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
662f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
663f9aa0ee1Sryan_chen 		udelay(100);
664f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
665f9aa0ee1Sryan_chen 		mdelay(10);
666f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
667f9aa0ee1Sryan_chen 		break;
668f9aa0ee1Sryan_chen 	default:
669f9aa0ee1Sryan_chen 		return -EINVAL;
670f9aa0ee1Sryan_chen 	}
671f9aa0ee1Sryan_chen 
672f9aa0ee1Sryan_chen 	return 0;
673f9aa0ee1Sryan_chen }
674550e691bSryan_chen 
675f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
676f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
677f51926eeSryan_chen {
678f51926eeSryan_chen 	u32 reset_bit;
679f51926eeSryan_chen 	u32 clkstop_bit;
680f51926eeSryan_chen 
681f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
682f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
683f51926eeSryan_chen 
684f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
685f51926eeSryan_chen 	udelay(100);
686f51926eeSryan_chen 	//enable clk
687f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
688f51926eeSryan_chen 	mdelay(10);
689f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
690f51926eeSryan_chen 
691f51926eeSryan_chen 	return 0;
692f51926eeSryan_chen }
693f51926eeSryan_chen 
694f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31
695f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
696f51926eeSryan_chen #define SCU_CLK_SD_DIV(x)			(x << 28)
697f51926eeSryan_chen 
698f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
699f51926eeSryan_chen {
700f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
701f51926eeSryan_chen 	u32 enableclk_bit;
702f51926eeSryan_chen 
703f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
704f51926eeSryan_chen 
705f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
706f51926eeSryan_chen 	clk_sel |= SCU_CLK_SD_DIV(0);
707f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
708f51926eeSryan_chen 
709f51926eeSryan_chen 	//enable clk
710f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
711f51926eeSryan_chen 
712f51926eeSryan_chen 	return 0;
713f51926eeSryan_chen }
714f51926eeSryan_chen 
715f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
716f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
717f51926eeSryan_chen {
718f51926eeSryan_chen 	u32 reset_bit;
719f51926eeSryan_chen 	u32 clkstop_bit;
720f51926eeSryan_chen 
721f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
722f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
723f51926eeSryan_chen 
724f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
725f51926eeSryan_chen 	udelay(100);
726f51926eeSryan_chen 	//enable clk
727f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
728f51926eeSryan_chen 	mdelay(10);
729f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
730f51926eeSryan_chen 
731f51926eeSryan_chen 	return 0;
732f51926eeSryan_chen }
733f51926eeSryan_chen 
734f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15
735f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
736f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x)			(x << 12)
737f51926eeSryan_chen 
738f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
739f51926eeSryan_chen {
740f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
741f51926eeSryan_chen 	u32 enableclk_bit;
742f51926eeSryan_chen 
743*d0bdd5f3Sryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC);
744f51926eeSryan_chen 
745f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
746f51926eeSryan_chen 	clk_sel |= SCU_CLK_SD_DIV(1);
747f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel1);
748f51926eeSryan_chen 
749f51926eeSryan_chen 	//enable clk
750f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
751f51926eeSryan_chen 
752f51926eeSryan_chen 	return 0;
753f51926eeSryan_chen }
754f51926eeSryan_chen 
755d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
756550e691bSryan_chen {
757f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
758550e691bSryan_chen 
759550e691bSryan_chen 	switch (clk->id) {
76086f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC1CLK:
76186f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 1);
762550e691bSryan_chen 			break;
76386f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC2CLK:
76486f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 2);
765550e691bSryan_chen 			break;
76677843939Sryan_chen 		case ASPEED_CLK_GATE_MAC3CLK:
76777843939Sryan_chen 			ast2600_configure_mac(priv->scu, 3);
76877843939Sryan_chen 			break;
76977843939Sryan_chen 		case ASPEED_CLK_GATE_MAC4CLK:
77077843939Sryan_chen 			ast2600_configure_mac(priv->scu, 4);
77177843939Sryan_chen 			break;
772f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDCLK:
773f51926eeSryan_chen 			ast2600_enable_sdclk(priv->scu);
774f51926eeSryan_chen 			break;
775f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDEXTCLK:
776f51926eeSryan_chen 			ast2600_enable_extsdclk(priv->scu);
777f51926eeSryan_chen 			break;
778f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCCLK:
779f51926eeSryan_chen 			ast2600_enable_emmcclk(priv->scu);
780f51926eeSryan_chen 			break;
781f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCEXTCLK:
782f51926eeSryan_chen 			ast2600_enable_extemmcclk(priv->scu);
783f51926eeSryan_chen 			break;
784550e691bSryan_chen 		default:
785f9aa0ee1Sryan_chen 			pr_debug("can't enable clk \n");
786550e691bSryan_chen 			return -ENOENT;
78777843939Sryan_chen 			break;
788550e691bSryan_chen 	}
789550e691bSryan_chen 
790550e691bSryan_chen 	return 0;
791550e691bSryan_chen }
792550e691bSryan_chen 
793f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
794d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
795d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
796d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
797550e691bSryan_chen };
798550e691bSryan_chen 
799d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
800550e691bSryan_chen {
801f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
802550e691bSryan_chen 
803f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
804f0d895afSryan_chen 	if (IS_ERR(priv->scu))
805f0d895afSryan_chen 		return PTR_ERR(priv->scu);
806550e691bSryan_chen 
807550e691bSryan_chen 	return 0;
808550e691bSryan_chen }
809550e691bSryan_chen 
810d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
811550e691bSryan_chen {
812550e691bSryan_chen 	int ret;
813550e691bSryan_chen 
814550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
815550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
816550e691bSryan_chen 	if (ret)
817550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
818550e691bSryan_chen 
819550e691bSryan_chen 	return 0;
820550e691bSryan_chen }
821550e691bSryan_chen 
822d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK)
823d35ac78cSryan_chen struct aspeed_clks {
824d35ac78cSryan_chen 	ulong id;
825d35ac78cSryan_chen 	const char *name;
826d35ac78cSryan_chen };
827d35ac78cSryan_chen 
828d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
829d35ac78cSryan_chen 	{ ASPEED_CLK_HPLL, "hpll" },
830d35ac78cSryan_chen 	{ ASPEED_CLK_MPLL, "mpll" },
831d35ac78cSryan_chen 	{ ASPEED_CLK_APLL, "apll" },
832d35ac78cSryan_chen 	{ ASPEED_CLK_EPLL, "epll" },
833d35ac78cSryan_chen 	{ ASPEED_CLK_DPLL, "dpll" },
834d35ac78cSryan_chen 	{ ASPEED_CLK_AHB, "hclk" },
8356fa1ef3dSryan_chen 	{ ASPEED_CLK_APB1, "pclk1" },
8366fa1ef3dSryan_chen 	{ ASPEED_CLK_APB2, "pclk2" },
837d35ac78cSryan_chen };
838d35ac78cSryan_chen 
839d35ac78cSryan_chen int soc_clk_dump(void)
840d35ac78cSryan_chen {
841d35ac78cSryan_chen 	struct udevice *dev;
842d35ac78cSryan_chen 	struct clk clk;
843d35ac78cSryan_chen 	unsigned long rate;
844d35ac78cSryan_chen 	int i, ret;
845d35ac78cSryan_chen 
846d35ac78cSryan_chen 	ret = uclass_get_device_by_driver(UCLASS_CLK,
847d35ac78cSryan_chen 					  DM_GET_DRIVER(aspeed_scu), &dev);
848d35ac78cSryan_chen 	if (ret)
849d35ac78cSryan_chen 		return ret;
850d35ac78cSryan_chen 
851d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
852d35ac78cSryan_chen 
853d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
854d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
855d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
856d35ac78cSryan_chen 		if (ret < 0) {
857d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
858d35ac78cSryan_chen 			continue;
859d35ac78cSryan_chen 		}
860d35ac78cSryan_chen 
861d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
862d35ac78cSryan_chen 		rate = ret;
863d35ac78cSryan_chen 
864d35ac78cSryan_chen 		clk_free(&clk);
865d35ac78cSryan_chen 
866d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
867d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
868d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
869d35ac78cSryan_chen 			continue;
870d35ac78cSryan_chen 		}
871d35ac78cSryan_chen 		if (ret < 0) {
872d35ac78cSryan_chen 			printf("%s %lu: get_rate err: %d\n",
873d35ac78cSryan_chen 			       __func__, aspeed_clk_names[i].id, ret);
874d35ac78cSryan_chen 			continue;
875d35ac78cSryan_chen 		}
876d35ac78cSryan_chen 
877d35ac78cSryan_chen 		printf("%s(%3lu):\t%lu\n",
878d35ac78cSryan_chen 		       aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate);
879d35ac78cSryan_chen 	}
880d35ac78cSryan_chen 
881d35ac78cSryan_chen 	return 0;
882d35ac78cSryan_chen }
883d35ac78cSryan_chen #endif
884d35ac78cSryan_chen 
885d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
886d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
887550e691bSryan_chen 	{ }
888550e691bSryan_chen };
889550e691bSryan_chen 
890aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
891aa36597fSDylan Hung 	.name		= "aspeed_scu",
892550e691bSryan_chen 	.id		= UCLASS_CLK,
893d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
894f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
895f9aa0ee1Sryan_chen 	.ops		= &ast2600_clk_ops,
896d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
897d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
898550e691bSryan_chen };
899