xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision 577fcdaed390c1ce596465e2fb8291ae9bb310ac)
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  */
51*577fcdaeSDylan Hung union ast2600_pll_reg {
52*577fcdaeSDylan Hung 	unsigned int w;
53*577fcdaeSDylan Hung 	struct {
54*577fcdaeSDylan Hung 		unsigned int m : 13;	/* 12:0  */
55*577fcdaeSDylan Hung 		unsigned int n : 6;	/* 18:13  */
56*577fcdaeSDylan Hung 		unsigned int p : 4;	/* 22:19  */
57*577fcdaeSDylan Hung 		unsigned int reserved : 9; /* 31:20  */
58*577fcdaeSDylan Hung 	} b;
59*577fcdaeSDylan Hung };
60*577fcdaeSDylan Hung 
61*577fcdaeSDylan Hung struct ast2600_pll_cfg {
62*577fcdaeSDylan Hung 	union ast2600_pll_reg reg;
63*577fcdaeSDylan Hung 	unsigned int ext_reg;
64*577fcdaeSDylan Hung };
65*577fcdaeSDylan Hung 
66*577fcdaeSDylan Hung struct ast2600_pll_desc {
67*577fcdaeSDylan Hung 	u32 in;
68*577fcdaeSDylan Hung 	u32 out;
69*577fcdaeSDylan Hung 	struct ast2600_pll_cfg cfg;
70*577fcdaeSDylan Hung };
71*577fcdaeSDylan Hung 
72*577fcdaeSDylan Hung static const struct ast2600_pll_desc ast2600_pll_lookup[] = {
73*577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 400000000,
74*577fcdaeSDylan Hung     .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 1,
75*577fcdaeSDylan Hung     .cfg.ext_reg = 0x31,
76*577fcdaeSDylan Hung     },
77*577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 200000000,
78*577fcdaeSDylan Hung     .cfg.reg.b.m = 127, .cfg.reg.b.n = 0, .cfg.reg.b.p = 15,
79*577fcdaeSDylan Hung     .cfg.ext_reg = 0x3f
80*577fcdaeSDylan Hung     },
81*577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 334000000,
82*577fcdaeSDylan Hung     .cfg.reg.b.m = 667, .cfg.reg.b.n = 4, .cfg.reg.b.p = 9,
83*577fcdaeSDylan Hung     .cfg.ext_reg = 0x14d
84*577fcdaeSDylan Hung     },
85*577fcdaeSDylan Hung 
86*577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 1000000000,
87*577fcdaeSDylan Hung     .cfg.reg.b.m = 119, .cfg.reg.b.n = 2, .cfg.reg.b.p = 0,
88*577fcdaeSDylan Hung     .cfg.ext_reg = 0x3d
89*577fcdaeSDylan Hung     },
90*577fcdaeSDylan Hung 
91*577fcdaeSDylan Hung     {.in = AST2600_CLK_IN, .out = 50000000,
92*577fcdaeSDylan Hung     .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 15,
93*577fcdaeSDylan Hung     .cfg.ext_reg = 0x31
94*577fcdaeSDylan 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) */
123bbbfb0c5Sryan_chen 		u32 m = pll_reg  & 0x1fff;
124bbbfb0c5Sryan_chen 		u32 n = (pll_reg >> 13) & 0x3f;
125bbbfb0c5Sryan_chen 		u32 p = (pll_reg >> 19) & 0xf;
1269639db61Sryan_chen 		mult = (m + 1) / (n + 1);
1279639db61Sryan_chen 		div = (p + 1);
1289639db61Sryan_chen 	}
1299639db61Sryan_chen 	return ((clkin * mult)/div);
130550e691bSryan_chen 
131550e691bSryan_chen }
132550e691bSryan_chen 
1334f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
134550e691bSryan_chen {
135bbbfb0c5Sryan_chen 	u32 clkin = AST2600_CLK_IN;
13639283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
13739283ea7Sryan_chen 	unsigned int mult, div = 1;
138d6e349c7Sryan_chen 
13939283ea7Sryan_chen 	if (apll_reg & BIT(20)) {
140d6e349c7Sryan_chen 		/* Pass through mode */
141d6e349c7Sryan_chen 		mult = div = 1;
142d6e349c7Sryan_chen 	} else {
143bbbfb0c5Sryan_chen 		/* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
14439283ea7Sryan_chen 		u32 m = (apll_reg >> 5) & 0x3f;
14539283ea7Sryan_chen 		u32 od = (apll_reg >> 4) & 0x1;
14639283ea7Sryan_chen 		u32 n = apll_reg & 0xf;
147d6e349c7Sryan_chen 
148bbbfb0c5Sryan_chen 		mult = (2 - od) * (m + 2);
149bbbfb0c5Sryan_chen 		div = n + 1;
150d6e349c7Sryan_chen 	}
151bbbfb0c5Sryan_chen 	return ((clkin * mult)/div);
15239283ea7Sryan_chen }
15339283ea7Sryan_chen 
154d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
155d812df15Sryan_chen 	2, 2, 3, 5,
156d812df15Sryan_chen };
157d812df15Sryan_chen 
158d812df15Sryan_chen static u32 ast2600_a1_axi_ahb_div_table[] = {
159d812df15Sryan_chen 	4, 6, 2, 4,
160d812df15Sryan_chen };
161d812df15Sryan_chen 
162d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
163d812df15Sryan_chen {
164d812df15Sryan_chen 	u32 hw_rev = readl(&scu->chip_id0);
165d812df15Sryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1);
166d812df15Sryan_chen 	u32 axi_div = 1;
167d812df15Sryan_chen 	u32 ahb_div = 0;
168d812df15Sryan_chen 	u32 rate = 0;
169d812df15Sryan_chen 
170c29e1cc8Sryan_chen 	if(hwstrap1 & BIT(16))
171d812df15Sryan_chen 		axi_div = 1;
172d812df15Sryan_chen 	else
173d812df15Sryan_chen 		axi_div = 2;
174d812df15Sryan_chen 
175d812df15Sryan_chen 	if (hw_rev & BIT(16))
176d812df15Sryan_chen 		ahb_div = ast2600_a1_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
177d812df15Sryan_chen 	else
178d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
179d812df15Sryan_chen 
180bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
181d812df15Sryan_chen 
1822717883aSryan_chen 	return (rate / axi_div / ahb_div);
1832717883aSryan_chen }
1842717883aSryan_chen 
1852717883aSryan_chen static u32 ast2600_hpll_pclk_div_table[] = {
1862717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
1872717883aSryan_chen };
1882717883aSryan_chen 
1892717883aSryan_chen static u32 ast2600_get_pclk(struct ast2600_scu *scu)
1902717883aSryan_chen {
1912717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
1922717883aSryan_chen 	u32 apb_div = ast2600_hpll_pclk_div_table[((clk_sel1 >> 23) & 0x7)];
193bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1942717883aSryan_chen 
1952717883aSryan_chen 	return (rate / apb_div);
196d812df15Sryan_chen }
197d812df15Sryan_chen 
19827881d20Sryan_chen static u32 ast2600_get_uxclk_rate(struct ast2600_scu *scu)
199d6e349c7Sryan_chen {
20027881d20Sryan_chen 	u32 clk_in = 0;
20127881d20Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel4);
202550e691bSryan_chen 
20327881d20Sryan_chen 	uxclk_sel &= 0x3;
20427881d20Sryan_chen 	switch(uxclk_sel) {
20527881d20Sryan_chen 		case 0:
20627881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
20727881d20Sryan_chen 			break;
20827881d20Sryan_chen 		case 1:
20927881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
21027881d20Sryan_chen 			break;
21127881d20Sryan_chen 		case 2:
21227881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
21327881d20Sryan_chen 			break;
21427881d20Sryan_chen 		case 3:
21527881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
21627881d20Sryan_chen 			break;
21727881d20Sryan_chen 	}
218d6e349c7Sryan_chen 
21927881d20Sryan_chen 	return clk_in;
22027881d20Sryan_chen }
22127881d20Sryan_chen 
22227881d20Sryan_chen static u32 ast2600_get_huxclk_rate(struct ast2600_scu *scu)
22327881d20Sryan_chen {
22427881d20Sryan_chen 	u32 clk_in = 0;
22527881d20Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel4);
22627881d20Sryan_chen 
22727881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
22827881d20Sryan_chen 	switch(huclk_sel) {
22927881d20Sryan_chen 		case 0:
23027881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
23127881d20Sryan_chen 			break;
23227881d20Sryan_chen 		case 1:
23327881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
23427881d20Sryan_chen 			break;
23527881d20Sryan_chen 		case 2:
23627881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
23727881d20Sryan_chen 			break;
23827881d20Sryan_chen 		case 3:
23927881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
24027881d20Sryan_chen 			break;
24127881d20Sryan_chen 	}
24227881d20Sryan_chen 
24327881d20Sryan_chen 	return clk_in;
24427881d20Sryan_chen }
24527881d20Sryan_chen 
24627881d20Sryan_chen static u32 ast2600_get_uart_from_uxclk_rate(struct ast2600_scu *scu)
24727881d20Sryan_chen {
24827881d20Sryan_chen 	u32 clk_in = ast2600_get_uxclk_rate(scu);
24927881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
25027881d20Sryan_chen 	unsigned int mult, div;
25127881d20Sryan_chen 
25227881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
25327881d20Sryan_chen 	u32 r = div_reg & 0xff;
25427881d20Sryan_chen 
25527881d20Sryan_chen 	mult = r;
25627881d20Sryan_chen 	div = (n * 4);
25727881d20Sryan_chen 	return (clk_in * mult)/div;
25827881d20Sryan_chen }
25927881d20Sryan_chen 
26027881d20Sryan_chen static u32 ast2600_get_uart_from_huxclk_rate(struct ast2600_scu *scu)
26127881d20Sryan_chen {
26227881d20Sryan_chen 	u32 clk_in = ast2600_get_huxclk_rate(scu);
26327881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
26427881d20Sryan_chen 
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 
275f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
276f51926eeSryan_chen {
277f51926eeSryan_chen 	u32 clkin = 0;
278f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
279f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
280f51926eeSryan_chen 
281f51926eeSryan_chen 	if(clk_sel & BIT(8)) {
282f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
283f51926eeSryan_chen 	} else {
284f51926eeSryan_chen 		clkin = 200 * 1000 * 1000;
285f51926eeSryan_chen 	}
286f51926eeSryan_chen 	div = (div + 1) << 1;
287f51926eeSryan_chen 
288f51926eeSryan_chen 	return (clkin / div);
289f51926eeSryan_chen }
290f51926eeSryan_chen 
291f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
292f51926eeSryan_chen {
293bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
294f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
295f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
296f51926eeSryan_chen 
297f51926eeSryan_chen 	div = (div + 1) << 2;
298f51926eeSryan_chen 
299f51926eeSryan_chen 	return (clkin / div);
300f51926eeSryan_chen }
301f51926eeSryan_chen 
302f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
30327881d20Sryan_chen {
30427881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
30527881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
30627881d20Sryan_chen 	ulong uart_clk = 0;
30727881d20Sryan_chen 
30827881d20Sryan_chen 	switch(uart_idx) {
30927881d20Sryan_chen 		case 1:
31027881d20Sryan_chen 		case 2:
31127881d20Sryan_chen 		case 3:
31227881d20Sryan_chen 		case 4:
31327881d20Sryan_chen 		case 6:
31427881d20Sryan_chen 			if(uart_sel & BIT(uart_idx - 1))
31527881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
316550e691bSryan_chen 			else
31727881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
31827881d20Sryan_chen 			break;
31927881d20Sryan_chen 		case 5: //24mhz is come form usb phy 48Mhz
32027881d20Sryan_chen 			{
32127881d20Sryan_chen 			u8 uart5_clk_sel = 0;
32227881d20Sryan_chen 			//high bit
32327881d20Sryan_chen 			if (readl(&scu->misc_ctrl1) & BIT(12))
32427881d20Sryan_chen 				uart5_clk_sel = 0x2;
32527881d20Sryan_chen 			else
32627881d20Sryan_chen 				uart5_clk_sel = 0x0;
327550e691bSryan_chen 
32827881d20Sryan_chen 			if (readl(&scu->clk_sel2) & BIT(14))
32927881d20Sryan_chen 				uart5_clk_sel |= 0x1;
330550e691bSryan_chen 
33127881d20Sryan_chen 			switch(uart5_clk_sel) {
33227881d20Sryan_chen 				case 0:
33327881d20Sryan_chen 					uart_clk = 24000000;
33427881d20Sryan_chen 					break;
33527881d20Sryan_chen 				case 1:
33627881d20Sryan_chen 					uart_clk = 0;
33727881d20Sryan_chen 					break;
33827881d20Sryan_chen 				case 2:
33927881d20Sryan_chen 					uart_clk = 24000000/13;
34027881d20Sryan_chen 					break;
34127881d20Sryan_chen 				case 3:
34227881d20Sryan_chen 					uart_clk = 192000000/13;
34327881d20Sryan_chen 					break;
34427881d20Sryan_chen 			}
34527881d20Sryan_chen 			}
34627881d20Sryan_chen 			break;
34727881d20Sryan_chen 		case 7:
34827881d20Sryan_chen 		case 8:
34927881d20Sryan_chen 		case 9:
35027881d20Sryan_chen 		case 10:
35127881d20Sryan_chen 		case 11:
35227881d20Sryan_chen 		case 12:
35327881d20Sryan_chen 		case 13:
35427881d20Sryan_chen 			if(uart_sel5 & BIT(uart_idx - 1))
35527881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
35627881d20Sryan_chen 			else
35727881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
35827881d20Sryan_chen 			break;
35927881d20Sryan_chen 	}
36027881d20Sryan_chen 
36127881d20Sryan_chen 	return uart_clk;
362550e691bSryan_chen }
363550e691bSryan_chen 
364feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
365feb42054Sryan_chen {
366feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
367feb42054Sryan_chen 	ulong rate = 0;
368feb42054Sryan_chen 
369feb42054Sryan_chen 	switch (clk->id) {
370feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
371bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
372bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
373d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
374bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
375d812df15Sryan_chen 		break;
376feb42054Sryan_chen 	case ASPEED_CLK_AHB:
377feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
378feb42054Sryan_chen 		break;
379feb42054Sryan_chen 	case ASPEED_CLK_APB:
3802717883aSryan_chen 		rate = ast2600_get_pclk(priv->scu);
381feb42054Sryan_chen 		break;
382bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
383bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
384bbbfb0c5Sryan_chen 		break;
385feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
386feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
387feb42054Sryan_chen 		break;
388feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
389feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
390feb42054Sryan_chen 		break;
391feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
392feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
393feb42054Sryan_chen 		break;
394feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
395feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
396feb42054Sryan_chen 		break;
397feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
398feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
399feb42054Sryan_chen 		break;
400f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
401f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
402f51926eeSryan_chen 		break;
403f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
404f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
405f51926eeSryan_chen 		break;
406feb42054Sryan_chen 	default:
407d812df15Sryan_chen 		pr_debug("can't get clk rate \n");
408feb42054Sryan_chen 		return -ENOENT;
409d812df15Sryan_chen 		break;
410feb42054Sryan_chen 	}
411feb42054Sryan_chen 
412feb42054Sryan_chen 	return rate;
413feb42054Sryan_chen }
414feb42054Sryan_chen 
415*577fcdaeSDylan Hung /**
416*577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
417*577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
418*577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
419550e691bSryan_chen  *
420*577fcdaeSDylan Hung  * The function caller shall fill "pll->in" and "pll->out", then this function
421*577fcdaeSDylan Hung  * will search the lookup table to find a valid PLL divider configuration.
422550e691bSryan_chen  */
423*577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
424550e691bSryan_chen {
425*577fcdaeSDylan Hung 	u32 i;
426*577fcdaeSDylan Hung 	bool is_found = false;
427550e691bSryan_chen 
428*577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
429*577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
430*577fcdaeSDylan Hung 		if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) {
431*577fcdaeSDylan Hung 			is_found = true;
432*577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
433*577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
434*577fcdaeSDylan Hung 			break;
435550e691bSryan_chen 		}
436550e691bSryan_chen 	}
437*577fcdaeSDylan Hung 	return is_found;
438550e691bSryan_chen }
439550e691bSryan_chen 
440feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
441550e691bSryan_chen {
442550e691bSryan_chen 	u32 mpll_reg;
443*577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
444550e691bSryan_chen 
445*577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
446*577fcdaeSDylan Hung 	mpll.out = rate;
447*577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&mpll)) {
448*577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
449*577fcdaeSDylan Hung 		return 0;
450*577fcdaeSDylan Hung 	}
451550e691bSryan_chen 
452feb42054Sryan_chen 	mpll_reg = readl(&scu->m_pll_param);
453*577fcdaeSDylan Hung 	mpll_reg &= ~GENMASK(22, 0);
454*577fcdaeSDylan Hung 	mpll_reg |= mpll.cfg.reg.w;
455feb42054Sryan_chen 	writel(mpll_reg, &scu->m_pll_param);
456550e691bSryan_chen 
457*577fcdaeSDylan Hung 	/* write extend parameter */
458*577fcdaeSDylan Hung 	writel(mpll.cfg.ext_reg, &scu->m_pll_ext_param);
459*577fcdaeSDylan Hung 
460cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
461d6e349c7Sryan_chen }
462d6e349c7Sryan_chen 
463d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
464550e691bSryan_chen {
465f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
466550e691bSryan_chen 
467550e691bSryan_chen 	ulong new_rate;
468550e691bSryan_chen 	switch (clk->id) {
469f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
470feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
471550e691bSryan_chen 		break;
472550e691bSryan_chen 	default:
473550e691bSryan_chen 		return -ENOENT;
474550e691bSryan_chen 	}
475550e691bSryan_chen 
476550e691bSryan_chen 	return new_rate;
477550e691bSryan_chen }
478feb42054Sryan_chen 
479f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
480f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
481f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
482f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
483f9aa0ee1Sryan_chen 
484cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu)
485cc476ffcSDylan Hung {
486cc476ffcSDylan Hung 	u32 epll_reg;
487cc476ffcSDylan Hung 	u32 clksel;
4884760b3f8SDylan Hung 	u32 clkdelay;
489cc476ffcSDylan Hung 
490*577fcdaeSDylan Hung 	struct ast2600_pll_desc epll;
491cc476ffcSDylan Hung 
492*577fcdaeSDylan Hung 	epll.in = AST2600_CLK_IN;
493*577fcdaeSDylan Hung 	epll.out = 1000000000;
494*577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&epll)) {
495*577fcdaeSDylan Hung 		printf(
496*577fcdaeSDylan Hung 		    "error!! unable to find valid ETHNET MAC clock setting\n");
497*577fcdaeSDylan Hung 		debug("%s: epll cfg = 0x%08x 0x%08x\n", __func__,
498*577fcdaeSDylan Hung 		      epll.cfg.reg.w, epll.cfg.ext_reg);
499*577fcdaeSDylan Hung 		debug("%s: epll cfg = %02x %02x %02x\n", __func__,
500*577fcdaeSDylan Hung 		      epll.cfg.reg.b.m, epll.cfg.reg.b.n, epll.cfg.reg.b.p);
501*577fcdaeSDylan Hung 		return 0;
502*577fcdaeSDylan Hung 	}
503*577fcdaeSDylan Hung 
504cc476ffcSDylan Hung 	epll_reg = readl(&scu->e_pll_param);
505cc476ffcSDylan Hung 	epll_reg &= ~GENMASK(22, 0);
506*577fcdaeSDylan Hung 	epll_reg |= epll.cfg.reg.w;
507cc476ffcSDylan Hung 	writel(epll_reg, &scu->e_pll_param);
508cc476ffcSDylan Hung 
509*577fcdaeSDylan Hung 	/* write extend parameter */
510*577fcdaeSDylan Hung 	writel(epll.cfg.ext_reg, &scu->e_pll_ext_param);
511*577fcdaeSDylan Hung 
512cc476ffcSDylan Hung 	/* select MAC#1 and MAC#2 clock source = EPLL / 8 */
513cc476ffcSDylan Hung 	clksel = readl(&scu->clk_sel2);
514cc476ffcSDylan Hung 	clksel &= ~BIT(23);
515cc476ffcSDylan Hung 	clksel |= 0x7 << 20;
516cc476ffcSDylan Hung 	writel(clksel, &scu->clk_sel2);
517cc476ffcSDylan Hung 
5184760b3f8SDylan Hung 	/*
5194760b3f8SDylan Hung 	BIT(31): select RGMII 125M from internal source
5204760b3f8SDylan Hung 	BIT(28): RGMII 125M output enable
5214760b3f8SDylan Hung 	BIT(25:0): 1G default delay
5224760b3f8SDylan Hung 	*/
5234760b3f8SDylan Hung 	clkdelay = MAC_DEF_DELAY_1G | BIT(31) | BIT(28);
5244760b3f8SDylan Hung 	writel(clkdelay, &scu->mac12_clk_delay);
5254760b3f8SDylan Hung 
5264760b3f8SDylan Hung 	/* set 100M/10M default delay */
5274760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M);
5284760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M);
529cc476ffcSDylan Hung 
530ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
531894c19cfSDylan Hung 	clksel = readl(&scu->clk_sel1);
532894c19cfSDylan Hung 	clksel &= ~GENMASK(18, 16);
533ed30249cSDylan Hung 	clksel |= 0x2 << 16;
534894c19cfSDylan Hung 	writel(clksel, &scu->clk_sel1);
535894c19cfSDylan Hung 
536cc476ffcSDylan Hung 	return 0;
537cc476ffcSDylan Hung }
538cc476ffcSDylan Hung 
53954f9cba1SDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu)
54054f9cba1SDylan Hung {
54154f9cba1SDylan Hung 	u32 reg;
54254f9cba1SDylan Hung 
54354f9cba1SDylan Hung 	ast2600_configure_mac12_clk(scu);
54454f9cba1SDylan Hung 
54554f9cba1SDylan Hung 	/*
54654f9cba1SDylan Hung 	BIT[31]   RGMII 125M source: 0 = from IO pin
54754f9cba1SDylan Hung 	BIT[25:0] MAC 1G delay
54854f9cba1SDylan Hung 	*/
54954f9cba1SDylan Hung 	reg = readl(&scu->mac34_clk_delay);
55054f9cba1SDylan Hung 	reg &= ~(BIT(31) | GENMASK(25, 0));
55154f9cba1SDylan Hung 	reg |= MAC34_DEF_DELAY_1G;
55254f9cba1SDylan Hung 	writel(reg, &scu->mac34_clk_delay);
55354f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M);
55454f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M);
55554f9cba1SDylan Hung 
55654f9cba1SDylan Hung 	/* clock source seletion and divider */
55754f9cba1SDylan Hung 	reg = readl(&scu->clk_sel4);
55854f9cba1SDylan Hung 	reg &= ~GENMASK(26, 24);	/* MAC AHB = HCLK / 2 */
55954f9cba1SDylan Hung 	reg &= ~GENMASK(18, 16);
56054f9cba1SDylan Hung 	reg |= 0x3 << 16;		/* RMII 50M = SLICLK_200M / 4 */
56154f9cba1SDylan Hung 	writel(reg, &scu->clk_sel4);
56254f9cba1SDylan Hung 
56354f9cba1SDylan Hung 	/* set driving strength */
56454f9cba1SDylan Hung 	reg = readl(&scu->pinmux_ctrl16);
56554f9cba1SDylan Hung 	reg &= GENMASK(3, 0);
56654f9cba1SDylan Hung 	reg |= (0x2 << 0) | (0x2 << 2);
56754f9cba1SDylan Hung 	writel(reg, &scu->pinmux_ctrl16);
56854f9cba1SDylan Hung 
56954f9cba1SDylan Hung 	return 0;
57054f9cba1SDylan Hung }
57154f9cba1SDylan Hung #if 0
57254f9cba1SDylan Hung /**
57354f9cba1SDylan Hung  * WIP: ast2600 RGMII clock source tree
57454f9cba1SDylan Hung  *
57554f9cba1SDylan Hung  *    125M from external PAD -------->|\
57654f9cba1SDylan Hung  *    HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
57754f9cba1SDylan Hung  *            | |---->| divider |---->|/                             +
57854f9cba1SDylan Hung  *    EPLL -->|/                                                     |
57954f9cba1SDylan Hung  *                                                                   |
58054f9cba1SDylan Hung  *    +---------<-----------|PAD output enable|<---------------------+
58154f9cba1SDylan Hung  *    |
58254f9cba1SDylan Hung  *    +--->|PAD input enable|----->|\
58354f9cba1SDylan Hung  *                                 | |----> RGMII 125M for MAC#3 & MAC#4
58454f9cba1SDylan Hung  *    SLICLK 200M -->|divider|---->|/
58554f9cba1SDylan Hung */
58654f9cba1SDylan Hung struct ast2600_rgmii_clk_config {
58754f9cba1SDylan Hung 	u32 mac_1_2_src;	/* 0=external PAD, 1=internal PLL */
58854f9cba1SDylan Hung 	u32 int_clk_src;	/* 0=EPLL, 1=HPLL */
58954f9cba1SDylan Hung 	u32 int_clk_div;
59054f9cba1SDylan Hung 
59154f9cba1SDylan Hung 	u32 mac_3_4_src;	/* 0=external PAD, 1=SLICLK */
59254f9cba1SDylan Hung 	u32 sli_clk_div;	/* reserved */
59354f9cba1SDylan Hung };
59454f9cba1SDylan Hung 
59554f9cba1SDylan Hung static void ast2600_init_rgmii_clk(struct ast2600_scu *scu, int index)
59654f9cba1SDylan Hung {
59754f9cba1SDylan Hung 	debug("%s not ready\n", __func__);
59854f9cba1SDylan Hung }
59954f9cba1SDylan Hung 
60054f9cba1SDylan Hung static void ast2600_init_rmii_clk(struct ast2600_scu *scu, int index)
60154f9cba1SDylan Hung {
60254f9cba1SDylan Hung 	debug("%s not ready\n", __func__);
60354f9cba1SDylan Hung }
60454f9cba1SDylan Hung #endif
605f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
606f9aa0ee1Sryan_chen {
607f9aa0ee1Sryan_chen 	u32 reset_bit;
608f9aa0ee1Sryan_chen 	u32 clkstop_bit;
609f9aa0ee1Sryan_chen 
610cc476ffcSDylan Hung 	if (index < 3)
611cc476ffcSDylan Hung 		ast2600_configure_mac12_clk(scu);
612cc476ffcSDylan Hung 	else
613cc476ffcSDylan Hung 		ast2600_configure_mac34_clk(scu);
614f9aa0ee1Sryan_chen 
615f9aa0ee1Sryan_chen 	switch (index) {
616f9aa0ee1Sryan_chen 	case 1:
617f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
618f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
619f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
620f9aa0ee1Sryan_chen 		udelay(100);
621f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
622f9aa0ee1Sryan_chen 		mdelay(10);
623f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
624f9aa0ee1Sryan_chen 
625f9aa0ee1Sryan_chen 		break;
626f9aa0ee1Sryan_chen 	case 2:
627f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
628f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
629f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
630f9aa0ee1Sryan_chen 		udelay(100);
631f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
632f9aa0ee1Sryan_chen 		mdelay(10);
633f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
634f9aa0ee1Sryan_chen 		break;
635f9aa0ee1Sryan_chen 	case 3:
636f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
637f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
638f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
639f9aa0ee1Sryan_chen 		udelay(100);
640f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
641f9aa0ee1Sryan_chen 		mdelay(10);
642f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
643f9aa0ee1Sryan_chen 		break;
644f9aa0ee1Sryan_chen 	case 4:
645f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
646f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
647f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
648f9aa0ee1Sryan_chen 		udelay(100);
649f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
650f9aa0ee1Sryan_chen 		mdelay(10);
651f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
652f9aa0ee1Sryan_chen 		break;
653f9aa0ee1Sryan_chen 	default:
654f9aa0ee1Sryan_chen 		return -EINVAL;
655f9aa0ee1Sryan_chen 	}
656f9aa0ee1Sryan_chen 
657f9aa0ee1Sryan_chen 	return 0;
658f9aa0ee1Sryan_chen }
659550e691bSryan_chen 
660f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
661f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
662f51926eeSryan_chen {
663f51926eeSryan_chen 	u32 reset_bit;
664f51926eeSryan_chen 	u32 clkstop_bit;
665f51926eeSryan_chen 
666f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
667f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
668f51926eeSryan_chen 
669f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
670f51926eeSryan_chen 	udelay(100);
671f51926eeSryan_chen 	//enable clk
672f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
673f51926eeSryan_chen 	mdelay(10);
674f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
675f51926eeSryan_chen 
676f51926eeSryan_chen 	return 0;
677f51926eeSryan_chen }
678f51926eeSryan_chen 
679f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31
680f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
681f51926eeSryan_chen #define SCU_CLK_SD_DIV(x)			(x << 28)
682f51926eeSryan_chen 
683f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
684f51926eeSryan_chen {
685f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
686f51926eeSryan_chen 	u32 enableclk_bit;
687f51926eeSryan_chen 
688f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
689f51926eeSryan_chen 
690f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
691f51926eeSryan_chen 	clk_sel |= SCU_CLK_SD_DIV(0);
692f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
693f51926eeSryan_chen 
694f51926eeSryan_chen 	//enable clk
695f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
696f51926eeSryan_chen 
697f51926eeSryan_chen 	return 0;
698f51926eeSryan_chen }
699f51926eeSryan_chen 
700f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
701f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
702f51926eeSryan_chen {
703f51926eeSryan_chen 	u32 reset_bit;
704f51926eeSryan_chen 	u32 clkstop_bit;
705f51926eeSryan_chen 
706f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
707f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
708f51926eeSryan_chen 
709f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
710f51926eeSryan_chen 	udelay(100);
711f51926eeSryan_chen 	//enable clk
712f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
713f51926eeSryan_chen 	mdelay(10);
714f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
715f51926eeSryan_chen 
716f51926eeSryan_chen 	return 0;
717f51926eeSryan_chen }
718f51926eeSryan_chen 
719f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15
720f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
721f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x)			(x << 12)
722f51926eeSryan_chen 
723f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
724f51926eeSryan_chen {
725f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
726f51926eeSryan_chen 	u32 enableclk_bit;
727f51926eeSryan_chen 
728f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
729f51926eeSryan_chen 
730f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
731f51926eeSryan_chen 	clk_sel |= SCU_CLK_SD_DIV(1);
732f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel1);
733f51926eeSryan_chen 
734f51926eeSryan_chen 	//enable clk
735f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
736f51926eeSryan_chen 
737f51926eeSryan_chen 	return 0;
738f51926eeSryan_chen }
739f51926eeSryan_chen 
740d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
741550e691bSryan_chen {
742f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
743550e691bSryan_chen 
744550e691bSryan_chen 	switch (clk->id) {
74586f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC1CLK:
74686f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 1);
747550e691bSryan_chen 			break;
74886f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC2CLK:
74986f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 2);
750550e691bSryan_chen 			break;
75177843939Sryan_chen 		case ASPEED_CLK_GATE_MAC3CLK:
75277843939Sryan_chen 			ast2600_configure_mac(priv->scu, 3);
75377843939Sryan_chen 			break;
75477843939Sryan_chen 		case ASPEED_CLK_GATE_MAC4CLK:
75577843939Sryan_chen 			ast2600_configure_mac(priv->scu, 4);
75677843939Sryan_chen 			break;
757f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDCLK:
758f51926eeSryan_chen 			ast2600_enable_sdclk(priv->scu);
759f51926eeSryan_chen 			break;
760f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDEXTCLK:
761f51926eeSryan_chen 			ast2600_enable_extsdclk(priv->scu);
762f51926eeSryan_chen 			break;
763f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCCLK:
764f51926eeSryan_chen 			ast2600_enable_emmcclk(priv->scu);
765f51926eeSryan_chen 			break;
766f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCEXTCLK:
767f51926eeSryan_chen 			ast2600_enable_extemmcclk(priv->scu);
768f51926eeSryan_chen 			break;
769550e691bSryan_chen 		default:
770f9aa0ee1Sryan_chen 			pr_debug("can't enable clk \n");
771550e691bSryan_chen 			return -ENOENT;
77277843939Sryan_chen 			break;
773550e691bSryan_chen 	}
774550e691bSryan_chen 
775550e691bSryan_chen 	return 0;
776550e691bSryan_chen }
777550e691bSryan_chen 
778f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
779d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
780d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
781d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
782550e691bSryan_chen };
783550e691bSryan_chen 
784d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
785550e691bSryan_chen {
786f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
787550e691bSryan_chen 
788f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
789f0d895afSryan_chen 	if (IS_ERR(priv->scu))
790f0d895afSryan_chen 		return PTR_ERR(priv->scu);
791550e691bSryan_chen 
792550e691bSryan_chen 	return 0;
793550e691bSryan_chen }
794550e691bSryan_chen 
795d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
796550e691bSryan_chen {
797550e691bSryan_chen 	int ret;
798550e691bSryan_chen 
799550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
800550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
801550e691bSryan_chen 	if (ret)
802550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
803550e691bSryan_chen 
804550e691bSryan_chen 	return 0;
805550e691bSryan_chen }
806550e691bSryan_chen 
807d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK)
808d35ac78cSryan_chen struct aspeed_clks {
809d35ac78cSryan_chen 	ulong id;
810d35ac78cSryan_chen 	const char *name;
811d35ac78cSryan_chen };
812d35ac78cSryan_chen 
813d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
814d35ac78cSryan_chen 	{ ASPEED_CLK_HPLL, "hpll" },
815d35ac78cSryan_chen 	{ ASPEED_CLK_MPLL, "mpll" },
816d35ac78cSryan_chen 	{ ASPEED_CLK_APLL, "apll" },
817d35ac78cSryan_chen 	{ ASPEED_CLK_EPLL, "epll" },
818d35ac78cSryan_chen 	{ ASPEED_CLK_DPLL, "dpll" },
819d35ac78cSryan_chen 	{ ASPEED_CLK_AHB, "hclk" },
820d35ac78cSryan_chen 	{ ASPEED_CLK_APB, "pclk" },
821d35ac78cSryan_chen };
822d35ac78cSryan_chen 
823d35ac78cSryan_chen int soc_clk_dump(void)
824d35ac78cSryan_chen {
825d35ac78cSryan_chen 	struct udevice *dev;
826d35ac78cSryan_chen 	struct clk clk;
827d35ac78cSryan_chen 	unsigned long rate;
828d35ac78cSryan_chen 	int i, ret;
829d35ac78cSryan_chen 
830d35ac78cSryan_chen 	ret = uclass_get_device_by_driver(UCLASS_CLK,
831d35ac78cSryan_chen 					  DM_GET_DRIVER(aspeed_scu), &dev);
832d35ac78cSryan_chen 	if (ret)
833d35ac78cSryan_chen 		return ret;
834d35ac78cSryan_chen 
835d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
836d35ac78cSryan_chen 
837d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
838d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
839d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
840d35ac78cSryan_chen 		if (ret < 0) {
841d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
842d35ac78cSryan_chen 			continue;
843d35ac78cSryan_chen 		}
844d35ac78cSryan_chen 
845d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
846d35ac78cSryan_chen 		rate = ret;
847d35ac78cSryan_chen 
848d35ac78cSryan_chen 		clk_free(&clk);
849d35ac78cSryan_chen 
850d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
851d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
852d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
853d35ac78cSryan_chen 			continue;
854d35ac78cSryan_chen 		}
855d35ac78cSryan_chen 		if (ret < 0) {
856d35ac78cSryan_chen 			printf("%s %lu: get_rate err: %d\n",
857d35ac78cSryan_chen 			       __func__, aspeed_clk_names[i].id, ret);
858d35ac78cSryan_chen 			continue;
859d35ac78cSryan_chen 		}
860d35ac78cSryan_chen 
861d35ac78cSryan_chen 		printf("%s(%3lu):\t%lu\n",
862d35ac78cSryan_chen 		       aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate);
863d35ac78cSryan_chen 	}
864d35ac78cSryan_chen 
865d35ac78cSryan_chen 	return 0;
866d35ac78cSryan_chen }
867d35ac78cSryan_chen #endif
868d35ac78cSryan_chen 
869d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
870d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
871550e691bSryan_chen 	{ }
872550e691bSryan_chen };
873550e691bSryan_chen 
874aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
875aa36597fSDylan Hung 	.name		= "aspeed_scu",
876550e691bSryan_chen 	.id		= UCLASS_CLK,
877d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
878f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
879f9aa0ee1Sryan_chen 	.ops		= &ast2600_clk_ops,
880d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
881d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
882550e691bSryan_chen };
883