xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision 75ced45aaa3aba8f2e80ee8070aec012f3461605)
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) */
123*75ced45aSDylan Hung 		union ast2600_pll_reg reg;
124*75ced45aSDylan Hung 		reg.w = pll_reg;
125*75ced45aSDylan Hung 		mult = (reg.b.m + 1) / (reg.b.n + 1);
126*75ced45aSDylan 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 
1842717883aSryan_chen static u32 ast2600_hpll_pclk_div_table[] = {
1852717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
1862717883aSryan_chen };
1872717883aSryan_chen 
1882717883aSryan_chen static u32 ast2600_get_pclk(struct ast2600_scu *scu)
1892717883aSryan_chen {
1902717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
1912717883aSryan_chen 	u32 apb_div = ast2600_hpll_pclk_div_table[((clk_sel1 >> 23) & 0x7)];
192bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1932717883aSryan_chen 
1942717883aSryan_chen 	return (rate / apb_div);
195d812df15Sryan_chen }
196d812df15Sryan_chen 
19727881d20Sryan_chen static u32 ast2600_get_uxclk_rate(struct ast2600_scu *scu)
198d6e349c7Sryan_chen {
19927881d20Sryan_chen 	u32 clk_in = 0;
20027881d20Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel4);
201550e691bSryan_chen 
20227881d20Sryan_chen 	uxclk_sel &= 0x3;
20327881d20Sryan_chen 	switch(uxclk_sel) {
20427881d20Sryan_chen 		case 0:
20527881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
20627881d20Sryan_chen 			break;
20727881d20Sryan_chen 		case 1:
20827881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
20927881d20Sryan_chen 			break;
21027881d20Sryan_chen 		case 2:
21127881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
21227881d20Sryan_chen 			break;
21327881d20Sryan_chen 		case 3:
21427881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
21527881d20Sryan_chen 			break;
21627881d20Sryan_chen 	}
217d6e349c7Sryan_chen 
21827881d20Sryan_chen 	return clk_in;
21927881d20Sryan_chen }
22027881d20Sryan_chen 
22127881d20Sryan_chen static u32 ast2600_get_huxclk_rate(struct ast2600_scu *scu)
22227881d20Sryan_chen {
22327881d20Sryan_chen 	u32 clk_in = 0;
22427881d20Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel4);
22527881d20Sryan_chen 
22627881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
22727881d20Sryan_chen 	switch(huclk_sel) {
22827881d20Sryan_chen 		case 0:
22927881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
23027881d20Sryan_chen 			break;
23127881d20Sryan_chen 		case 1:
23227881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
23327881d20Sryan_chen 			break;
23427881d20Sryan_chen 		case 2:
23527881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
23627881d20Sryan_chen 			break;
23727881d20Sryan_chen 		case 3:
23827881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
23927881d20Sryan_chen 			break;
24027881d20Sryan_chen 	}
24127881d20Sryan_chen 
24227881d20Sryan_chen 	return clk_in;
24327881d20Sryan_chen }
24427881d20Sryan_chen 
24527881d20Sryan_chen static u32 ast2600_get_uart_from_uxclk_rate(struct ast2600_scu *scu)
24627881d20Sryan_chen {
24727881d20Sryan_chen 	u32 clk_in = ast2600_get_uxclk_rate(scu);
24827881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
24927881d20Sryan_chen 	unsigned int mult, div;
25027881d20Sryan_chen 
25127881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
25227881d20Sryan_chen 	u32 r = div_reg & 0xff;
25327881d20Sryan_chen 
25427881d20Sryan_chen 	mult = r;
25527881d20Sryan_chen 	div = (n * 4);
25627881d20Sryan_chen 	return (clk_in * mult)/div;
25727881d20Sryan_chen }
25827881d20Sryan_chen 
25927881d20Sryan_chen static u32 ast2600_get_uart_from_huxclk_rate(struct ast2600_scu *scu)
26027881d20Sryan_chen {
26127881d20Sryan_chen 	u32 clk_in = ast2600_get_huxclk_rate(scu);
26227881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
26327881d20Sryan_chen 
26427881d20Sryan_chen 	unsigned int mult, div;
26527881d20Sryan_chen 
26627881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
26727881d20Sryan_chen 	u32 r = div_reg & 0xff;
26827881d20Sryan_chen 
26927881d20Sryan_chen 	mult = r;
27027881d20Sryan_chen 	div = (n * 4);
27127881d20Sryan_chen 	return (clk_in * mult)/div;
27227881d20Sryan_chen }
27327881d20Sryan_chen 
274f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
275f51926eeSryan_chen {
276f51926eeSryan_chen 	u32 clkin = 0;
277f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
278f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
279f51926eeSryan_chen 
280f51926eeSryan_chen 	if(clk_sel & BIT(8)) {
281f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
282f51926eeSryan_chen 	} else {
283f51926eeSryan_chen 		clkin = 200 * 1000 * 1000;
284f51926eeSryan_chen 	}
285f51926eeSryan_chen 	div = (div + 1) << 1;
286f51926eeSryan_chen 
287f51926eeSryan_chen 	return (clkin / div);
288f51926eeSryan_chen }
289f51926eeSryan_chen 
290f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
291f51926eeSryan_chen {
292bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
293f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
294f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
295f51926eeSryan_chen 
296f51926eeSryan_chen 	div = (div + 1) << 2;
297f51926eeSryan_chen 
298f51926eeSryan_chen 	return (clkin / div);
299f51926eeSryan_chen }
300f51926eeSryan_chen 
301f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
30227881d20Sryan_chen {
30327881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
30427881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
30527881d20Sryan_chen 	ulong uart_clk = 0;
30627881d20Sryan_chen 
30727881d20Sryan_chen 	switch(uart_idx) {
30827881d20Sryan_chen 		case 1:
30927881d20Sryan_chen 		case 2:
31027881d20Sryan_chen 		case 3:
31127881d20Sryan_chen 		case 4:
31227881d20Sryan_chen 		case 6:
31327881d20Sryan_chen 			if(uart_sel & BIT(uart_idx - 1))
31427881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
315550e691bSryan_chen 			else
31627881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
31727881d20Sryan_chen 			break;
31827881d20Sryan_chen 		case 5: //24mhz is come form usb phy 48Mhz
31927881d20Sryan_chen 			{
32027881d20Sryan_chen 			u8 uart5_clk_sel = 0;
32127881d20Sryan_chen 			//high bit
32227881d20Sryan_chen 			if (readl(&scu->misc_ctrl1) & BIT(12))
32327881d20Sryan_chen 				uart5_clk_sel = 0x2;
32427881d20Sryan_chen 			else
32527881d20Sryan_chen 				uart5_clk_sel = 0x0;
326550e691bSryan_chen 
32727881d20Sryan_chen 			if (readl(&scu->clk_sel2) & BIT(14))
32827881d20Sryan_chen 				uart5_clk_sel |= 0x1;
329550e691bSryan_chen 
33027881d20Sryan_chen 			switch(uart5_clk_sel) {
33127881d20Sryan_chen 				case 0:
33227881d20Sryan_chen 					uart_clk = 24000000;
33327881d20Sryan_chen 					break;
33427881d20Sryan_chen 				case 1:
33527881d20Sryan_chen 					uart_clk = 0;
33627881d20Sryan_chen 					break;
33727881d20Sryan_chen 				case 2:
33827881d20Sryan_chen 					uart_clk = 24000000/13;
33927881d20Sryan_chen 					break;
34027881d20Sryan_chen 				case 3:
34127881d20Sryan_chen 					uart_clk = 192000000/13;
34227881d20Sryan_chen 					break;
34327881d20Sryan_chen 			}
34427881d20Sryan_chen 			}
34527881d20Sryan_chen 			break;
34627881d20Sryan_chen 		case 7:
34727881d20Sryan_chen 		case 8:
34827881d20Sryan_chen 		case 9:
34927881d20Sryan_chen 		case 10:
35027881d20Sryan_chen 		case 11:
35127881d20Sryan_chen 		case 12:
35227881d20Sryan_chen 		case 13:
35327881d20Sryan_chen 			if(uart_sel5 & BIT(uart_idx - 1))
35427881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
35527881d20Sryan_chen 			else
35627881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
35727881d20Sryan_chen 			break;
35827881d20Sryan_chen 	}
35927881d20Sryan_chen 
36027881d20Sryan_chen 	return uart_clk;
361550e691bSryan_chen }
362550e691bSryan_chen 
363feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
364feb42054Sryan_chen {
365feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
366feb42054Sryan_chen 	ulong rate = 0;
367feb42054Sryan_chen 
368feb42054Sryan_chen 	switch (clk->id) {
369feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
370bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
371bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
372d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
373bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
374d812df15Sryan_chen 		break;
375feb42054Sryan_chen 	case ASPEED_CLK_AHB:
376feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
377feb42054Sryan_chen 		break;
378feb42054Sryan_chen 	case ASPEED_CLK_APB:
3792717883aSryan_chen 		rate = ast2600_get_pclk(priv->scu);
380feb42054Sryan_chen 		break;
381bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
382bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
383bbbfb0c5Sryan_chen 		break;
384feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
385feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
386feb42054Sryan_chen 		break;
387feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
388feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
389feb42054Sryan_chen 		break;
390feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
391feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
392feb42054Sryan_chen 		break;
393feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
394feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
395feb42054Sryan_chen 		break;
396feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
397feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
398feb42054Sryan_chen 		break;
399f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
400f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
401f51926eeSryan_chen 		break;
402f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
403f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
404f51926eeSryan_chen 		break;
405feb42054Sryan_chen 	default:
406d812df15Sryan_chen 		pr_debug("can't get clk rate \n");
407feb42054Sryan_chen 		return -ENOENT;
408d812df15Sryan_chen 		break;
409feb42054Sryan_chen 	}
410feb42054Sryan_chen 
411feb42054Sryan_chen 	return rate;
412feb42054Sryan_chen }
413feb42054Sryan_chen 
414577fcdaeSDylan Hung /**
415577fcdaeSDylan Hung  * @brief	lookup PLL divider config by input/output rate
416577fcdaeSDylan Hung  * @param[in]	*pll - PLL descriptor
417577fcdaeSDylan Hung  * @return	true - if PLL divider config is found, false - else
418550e691bSryan_chen  *
419577fcdaeSDylan Hung  * The function caller shall fill "pll->in" and "pll->out", then this function
420577fcdaeSDylan Hung  * will search the lookup table to find a valid PLL divider configuration.
421550e691bSryan_chen  */
422577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll)
423550e691bSryan_chen {
424577fcdaeSDylan Hung 	u32 i;
425577fcdaeSDylan Hung 	bool is_found = false;
426550e691bSryan_chen 
427577fcdaeSDylan Hung 	for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) {
428577fcdaeSDylan Hung 		const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i];
429577fcdaeSDylan Hung 		if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) {
430577fcdaeSDylan Hung 			is_found = true;
431577fcdaeSDylan Hung 			pll->cfg.reg.w = def_cfg->cfg.reg.w;
432577fcdaeSDylan Hung 			pll->cfg.ext_reg = def_cfg->cfg.ext_reg;
433577fcdaeSDylan Hung 			break;
434550e691bSryan_chen 		}
435550e691bSryan_chen 	}
436577fcdaeSDylan Hung 	return is_found;
437550e691bSryan_chen }
438550e691bSryan_chen 
439feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
440550e691bSryan_chen {
441550e691bSryan_chen 	u32 mpll_reg;
442577fcdaeSDylan Hung 	struct ast2600_pll_desc mpll;
443550e691bSryan_chen 
444577fcdaeSDylan Hung 	mpll.in = AST2600_CLK_IN;
445577fcdaeSDylan Hung 	mpll.out = rate;
446577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&mpll)) {
447577fcdaeSDylan Hung 		printf("error!! unable to find valid DDR clock setting\n");
448577fcdaeSDylan Hung 		return 0;
449577fcdaeSDylan Hung 	}
450550e691bSryan_chen 
451feb42054Sryan_chen 	mpll_reg = readl(&scu->m_pll_param);
452577fcdaeSDylan Hung 	mpll_reg &= ~GENMASK(22, 0);
453577fcdaeSDylan Hung 	mpll_reg |= mpll.cfg.reg.w;
454feb42054Sryan_chen 	writel(mpll_reg, &scu->m_pll_param);
455550e691bSryan_chen 
456577fcdaeSDylan Hung 	/* write extend parameter */
457577fcdaeSDylan Hung 	writel(mpll.cfg.ext_reg, &scu->m_pll_ext_param);
458577fcdaeSDylan Hung 
459cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
460d6e349c7Sryan_chen }
461d6e349c7Sryan_chen 
462d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
463550e691bSryan_chen {
464f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
465550e691bSryan_chen 
466550e691bSryan_chen 	ulong new_rate;
467550e691bSryan_chen 	switch (clk->id) {
468f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
469feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
470550e691bSryan_chen 		break;
471550e691bSryan_chen 	default:
472550e691bSryan_chen 		return -ENOENT;
473550e691bSryan_chen 	}
474550e691bSryan_chen 
475550e691bSryan_chen 	return new_rate;
476550e691bSryan_chen }
477feb42054Sryan_chen 
478f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
479f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
480f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
481f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
482f9aa0ee1Sryan_chen 
483cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu)
484cc476ffcSDylan Hung {
485cc476ffcSDylan Hung 	u32 epll_reg;
486cc476ffcSDylan Hung 	u32 clksel;
4874760b3f8SDylan Hung 	u32 clkdelay;
488cc476ffcSDylan Hung 
489577fcdaeSDylan Hung 	struct ast2600_pll_desc epll;
490cc476ffcSDylan Hung 
491577fcdaeSDylan Hung 	epll.in = AST2600_CLK_IN;
492577fcdaeSDylan Hung 	epll.out = 1000000000;
493577fcdaeSDylan Hung 	if (false == ast2600_search_clock_config(&epll)) {
494577fcdaeSDylan Hung 		printf(
495577fcdaeSDylan Hung 		    "error!! unable to find valid ETHNET MAC clock setting\n");
496577fcdaeSDylan Hung 		debug("%s: epll cfg = 0x%08x 0x%08x\n", __func__,
497577fcdaeSDylan Hung 		      epll.cfg.reg.w, epll.cfg.ext_reg);
498577fcdaeSDylan Hung 		debug("%s: epll cfg = %02x %02x %02x\n", __func__,
499577fcdaeSDylan Hung 		      epll.cfg.reg.b.m, epll.cfg.reg.b.n, epll.cfg.reg.b.p);
500577fcdaeSDylan Hung 		return 0;
501577fcdaeSDylan Hung 	}
502577fcdaeSDylan Hung 
503cc476ffcSDylan Hung 	epll_reg = readl(&scu->e_pll_param);
504cc476ffcSDylan Hung 	epll_reg &= ~GENMASK(22, 0);
505577fcdaeSDylan Hung 	epll_reg |= epll.cfg.reg.w;
506cc476ffcSDylan Hung 	writel(epll_reg, &scu->e_pll_param);
507cc476ffcSDylan Hung 
508577fcdaeSDylan Hung 	/* write extend parameter */
509577fcdaeSDylan Hung 	writel(epll.cfg.ext_reg, &scu->e_pll_ext_param);
510577fcdaeSDylan Hung 
511cc476ffcSDylan Hung 	/* select MAC#1 and MAC#2 clock source = EPLL / 8 */
512cc476ffcSDylan Hung 	clksel = readl(&scu->clk_sel2);
513cc476ffcSDylan Hung 	clksel &= ~BIT(23);
514cc476ffcSDylan Hung 	clksel |= 0x7 << 20;
515cc476ffcSDylan Hung 	writel(clksel, &scu->clk_sel2);
516cc476ffcSDylan Hung 
5174760b3f8SDylan Hung 	/*
5184760b3f8SDylan Hung 	BIT(31): select RGMII 125M from internal source
5194760b3f8SDylan Hung 	BIT(28): RGMII 125M output enable
5204760b3f8SDylan Hung 	BIT(25:0): 1G default delay
5214760b3f8SDylan Hung 	*/
5224760b3f8SDylan Hung 	clkdelay = MAC_DEF_DELAY_1G | BIT(31) | BIT(28);
5234760b3f8SDylan Hung 	writel(clkdelay, &scu->mac12_clk_delay);
5244760b3f8SDylan Hung 
5254760b3f8SDylan Hung 	/* set 100M/10M default delay */
5264760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M);
5274760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M);
528cc476ffcSDylan Hung 
529ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
530894c19cfSDylan Hung 	clksel = readl(&scu->clk_sel1);
531894c19cfSDylan Hung 	clksel &= ~GENMASK(18, 16);
532ed30249cSDylan Hung 	clksel |= 0x2 << 16;
533894c19cfSDylan Hung 	writel(clksel, &scu->clk_sel1);
534894c19cfSDylan Hung 
535cc476ffcSDylan Hung 	return 0;
536cc476ffcSDylan Hung }
537cc476ffcSDylan Hung 
53854f9cba1SDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu)
53954f9cba1SDylan Hung {
54054f9cba1SDylan Hung 	u32 reg;
54154f9cba1SDylan Hung 
54254f9cba1SDylan Hung 	ast2600_configure_mac12_clk(scu);
54354f9cba1SDylan Hung 
54454f9cba1SDylan Hung 	/*
54554f9cba1SDylan Hung 	BIT[31]   RGMII 125M source: 0 = from IO pin
54654f9cba1SDylan Hung 	BIT[25:0] MAC 1G delay
54754f9cba1SDylan Hung 	*/
54854f9cba1SDylan Hung 	reg = readl(&scu->mac34_clk_delay);
54954f9cba1SDylan Hung 	reg &= ~(BIT(31) | GENMASK(25, 0));
55054f9cba1SDylan Hung 	reg |= MAC34_DEF_DELAY_1G;
55154f9cba1SDylan Hung 	writel(reg, &scu->mac34_clk_delay);
55254f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M);
55354f9cba1SDylan Hung 	writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M);
55454f9cba1SDylan Hung 
55554f9cba1SDylan Hung 	/* clock source seletion and divider */
55654f9cba1SDylan Hung 	reg = readl(&scu->clk_sel4);
55754f9cba1SDylan Hung 	reg &= ~GENMASK(26, 24);	/* MAC AHB = HCLK / 2 */
55854f9cba1SDylan Hung 	reg &= ~GENMASK(18, 16);
55954f9cba1SDylan Hung 	reg |= 0x3 << 16;		/* RMII 50M = SLICLK_200M / 4 */
56054f9cba1SDylan Hung 	writel(reg, &scu->clk_sel4);
56154f9cba1SDylan Hung 
56254f9cba1SDylan Hung 	/* set driving strength */
56354f9cba1SDylan Hung 	reg = readl(&scu->pinmux_ctrl16);
56454f9cba1SDylan Hung 	reg &= GENMASK(3, 0);
56554f9cba1SDylan Hung 	reg |= (0x2 << 0) | (0x2 << 2);
56654f9cba1SDylan Hung 	writel(reg, &scu->pinmux_ctrl16);
56754f9cba1SDylan Hung 
56854f9cba1SDylan Hung 	return 0;
56954f9cba1SDylan Hung }
57054f9cba1SDylan Hung #if 0
57154f9cba1SDylan Hung /**
57254f9cba1SDylan Hung  * WIP: ast2600 RGMII clock source tree
57354f9cba1SDylan Hung  *
57454f9cba1SDylan Hung  *    125M from external PAD -------->|\
57554f9cba1SDylan Hung  *    HPLL -->|\                      | |---->RGMII 125M for MAC#1 & MAC#2
57654f9cba1SDylan Hung  *            | |---->| divider |---->|/                             +
57754f9cba1SDylan Hung  *    EPLL -->|/                                                     |
57854f9cba1SDylan Hung  *                                                                   |
57954f9cba1SDylan Hung  *    +---------<-----------|PAD output enable|<---------------------+
58054f9cba1SDylan Hung  *    |
58154f9cba1SDylan Hung  *    +--->|PAD input enable|----->|\
58254f9cba1SDylan Hung  *                                 | |----> RGMII 125M for MAC#3 & MAC#4
58354f9cba1SDylan Hung  *    SLICLK 200M -->|divider|---->|/
58454f9cba1SDylan Hung */
58554f9cba1SDylan Hung struct ast2600_rgmii_clk_config {
58654f9cba1SDylan Hung 	u32 mac_1_2_src;	/* 0=external PAD, 1=internal PLL */
58754f9cba1SDylan Hung 	u32 int_clk_src;	/* 0=EPLL, 1=HPLL */
58854f9cba1SDylan Hung 	u32 int_clk_div;
58954f9cba1SDylan Hung 
59054f9cba1SDylan Hung 	u32 mac_3_4_src;	/* 0=external PAD, 1=SLICLK */
59154f9cba1SDylan Hung 	u32 sli_clk_div;	/* reserved */
59254f9cba1SDylan Hung };
59354f9cba1SDylan Hung 
59454f9cba1SDylan Hung static void ast2600_init_rgmii_clk(struct ast2600_scu *scu, int index)
59554f9cba1SDylan Hung {
59654f9cba1SDylan Hung 	debug("%s not ready\n", __func__);
59754f9cba1SDylan Hung }
59854f9cba1SDylan Hung 
59954f9cba1SDylan Hung static void ast2600_init_rmii_clk(struct ast2600_scu *scu, int index)
60054f9cba1SDylan Hung {
60154f9cba1SDylan Hung 	debug("%s not ready\n", __func__);
60254f9cba1SDylan Hung }
60354f9cba1SDylan Hung #endif
604f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
605f9aa0ee1Sryan_chen {
606f9aa0ee1Sryan_chen 	u32 reset_bit;
607f9aa0ee1Sryan_chen 	u32 clkstop_bit;
608f9aa0ee1Sryan_chen 
609cc476ffcSDylan Hung 	if (index < 3)
610cc476ffcSDylan Hung 		ast2600_configure_mac12_clk(scu);
611cc476ffcSDylan Hung 	else
612cc476ffcSDylan Hung 		ast2600_configure_mac34_clk(scu);
613f9aa0ee1Sryan_chen 
614f9aa0ee1Sryan_chen 	switch (index) {
615f9aa0ee1Sryan_chen 	case 1:
616f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
617f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
618f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
619f9aa0ee1Sryan_chen 		udelay(100);
620f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
621f9aa0ee1Sryan_chen 		mdelay(10);
622f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
623f9aa0ee1Sryan_chen 
624f9aa0ee1Sryan_chen 		break;
625f9aa0ee1Sryan_chen 	case 2:
626f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
627f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
628f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
629f9aa0ee1Sryan_chen 		udelay(100);
630f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
631f9aa0ee1Sryan_chen 		mdelay(10);
632f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
633f9aa0ee1Sryan_chen 		break;
634f9aa0ee1Sryan_chen 	case 3:
635f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
636f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
637f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
638f9aa0ee1Sryan_chen 		udelay(100);
639f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
640f9aa0ee1Sryan_chen 		mdelay(10);
641f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
642f9aa0ee1Sryan_chen 		break;
643f9aa0ee1Sryan_chen 	case 4:
644f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
645f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
646f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
647f9aa0ee1Sryan_chen 		udelay(100);
648f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
649f9aa0ee1Sryan_chen 		mdelay(10);
650f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
651f9aa0ee1Sryan_chen 		break;
652f9aa0ee1Sryan_chen 	default:
653f9aa0ee1Sryan_chen 		return -EINVAL;
654f9aa0ee1Sryan_chen 	}
655f9aa0ee1Sryan_chen 
656f9aa0ee1Sryan_chen 	return 0;
657f9aa0ee1Sryan_chen }
658550e691bSryan_chen 
659f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
660f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
661f51926eeSryan_chen {
662f51926eeSryan_chen 	u32 reset_bit;
663f51926eeSryan_chen 	u32 clkstop_bit;
664f51926eeSryan_chen 
665f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
666f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
667f51926eeSryan_chen 
668f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
669f51926eeSryan_chen 	udelay(100);
670f51926eeSryan_chen 	//enable clk
671f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
672f51926eeSryan_chen 	mdelay(10);
673f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
674f51926eeSryan_chen 
675f51926eeSryan_chen 	return 0;
676f51926eeSryan_chen }
677f51926eeSryan_chen 
678f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31
679f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
680f51926eeSryan_chen #define SCU_CLK_SD_DIV(x)			(x << 28)
681f51926eeSryan_chen 
682f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
683f51926eeSryan_chen {
684f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
685f51926eeSryan_chen 	u32 enableclk_bit;
686f51926eeSryan_chen 
687f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
688f51926eeSryan_chen 
689f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
690f51926eeSryan_chen 	clk_sel |= SCU_CLK_SD_DIV(0);
691f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
692f51926eeSryan_chen 
693f51926eeSryan_chen 	//enable clk
694f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
695f51926eeSryan_chen 
696f51926eeSryan_chen 	return 0;
697f51926eeSryan_chen }
698f51926eeSryan_chen 
699f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
700f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
701f51926eeSryan_chen {
702f51926eeSryan_chen 	u32 reset_bit;
703f51926eeSryan_chen 	u32 clkstop_bit;
704f51926eeSryan_chen 
705f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
706f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
707f51926eeSryan_chen 
708f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
709f51926eeSryan_chen 	udelay(100);
710f51926eeSryan_chen 	//enable clk
711f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
712f51926eeSryan_chen 	mdelay(10);
713f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
714f51926eeSryan_chen 
715f51926eeSryan_chen 	return 0;
716f51926eeSryan_chen }
717f51926eeSryan_chen 
718f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15
719f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
720f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x)			(x << 12)
721f51926eeSryan_chen 
722f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
723f51926eeSryan_chen {
724f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
725f51926eeSryan_chen 	u32 enableclk_bit;
726f51926eeSryan_chen 
727f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
728f51926eeSryan_chen 
729f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
730f51926eeSryan_chen 	clk_sel |= SCU_CLK_SD_DIV(1);
731f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel1);
732f51926eeSryan_chen 
733f51926eeSryan_chen 	//enable clk
734f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
735f51926eeSryan_chen 
736f51926eeSryan_chen 	return 0;
737f51926eeSryan_chen }
738f51926eeSryan_chen 
739d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
740550e691bSryan_chen {
741f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
742550e691bSryan_chen 
743550e691bSryan_chen 	switch (clk->id) {
74486f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC1CLK:
74586f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 1);
746550e691bSryan_chen 			break;
74786f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC2CLK:
74886f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 2);
749550e691bSryan_chen 			break;
75077843939Sryan_chen 		case ASPEED_CLK_GATE_MAC3CLK:
75177843939Sryan_chen 			ast2600_configure_mac(priv->scu, 3);
75277843939Sryan_chen 			break;
75377843939Sryan_chen 		case ASPEED_CLK_GATE_MAC4CLK:
75477843939Sryan_chen 			ast2600_configure_mac(priv->scu, 4);
75577843939Sryan_chen 			break;
756f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDCLK:
757f51926eeSryan_chen 			ast2600_enable_sdclk(priv->scu);
758f51926eeSryan_chen 			break;
759f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDEXTCLK:
760f51926eeSryan_chen 			ast2600_enable_extsdclk(priv->scu);
761f51926eeSryan_chen 			break;
762f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCCLK:
763f51926eeSryan_chen 			ast2600_enable_emmcclk(priv->scu);
764f51926eeSryan_chen 			break;
765f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCEXTCLK:
766f51926eeSryan_chen 			ast2600_enable_extemmcclk(priv->scu);
767f51926eeSryan_chen 			break;
768550e691bSryan_chen 		default:
769f9aa0ee1Sryan_chen 			pr_debug("can't enable clk \n");
770550e691bSryan_chen 			return -ENOENT;
77177843939Sryan_chen 			break;
772550e691bSryan_chen 	}
773550e691bSryan_chen 
774550e691bSryan_chen 	return 0;
775550e691bSryan_chen }
776550e691bSryan_chen 
777f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
778d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
779d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
780d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
781550e691bSryan_chen };
782550e691bSryan_chen 
783d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
784550e691bSryan_chen {
785f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
786550e691bSryan_chen 
787f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
788f0d895afSryan_chen 	if (IS_ERR(priv->scu))
789f0d895afSryan_chen 		return PTR_ERR(priv->scu);
790550e691bSryan_chen 
791550e691bSryan_chen 	return 0;
792550e691bSryan_chen }
793550e691bSryan_chen 
794d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
795550e691bSryan_chen {
796550e691bSryan_chen 	int ret;
797550e691bSryan_chen 
798550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
799550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
800550e691bSryan_chen 	if (ret)
801550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
802550e691bSryan_chen 
803550e691bSryan_chen 	return 0;
804550e691bSryan_chen }
805550e691bSryan_chen 
806d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK)
807d35ac78cSryan_chen struct aspeed_clks {
808d35ac78cSryan_chen 	ulong id;
809d35ac78cSryan_chen 	const char *name;
810d35ac78cSryan_chen };
811d35ac78cSryan_chen 
812d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
813d35ac78cSryan_chen 	{ ASPEED_CLK_HPLL, "hpll" },
814d35ac78cSryan_chen 	{ ASPEED_CLK_MPLL, "mpll" },
815d35ac78cSryan_chen 	{ ASPEED_CLK_APLL, "apll" },
816d35ac78cSryan_chen 	{ ASPEED_CLK_EPLL, "epll" },
817d35ac78cSryan_chen 	{ ASPEED_CLK_DPLL, "dpll" },
818d35ac78cSryan_chen 	{ ASPEED_CLK_AHB, "hclk" },
819d35ac78cSryan_chen 	{ ASPEED_CLK_APB, "pclk" },
820d35ac78cSryan_chen };
821d35ac78cSryan_chen 
822d35ac78cSryan_chen int soc_clk_dump(void)
823d35ac78cSryan_chen {
824d35ac78cSryan_chen 	struct udevice *dev;
825d35ac78cSryan_chen 	struct clk clk;
826d35ac78cSryan_chen 	unsigned long rate;
827d35ac78cSryan_chen 	int i, ret;
828d35ac78cSryan_chen 
829d35ac78cSryan_chen 	ret = uclass_get_device_by_driver(UCLASS_CLK,
830d35ac78cSryan_chen 					  DM_GET_DRIVER(aspeed_scu), &dev);
831d35ac78cSryan_chen 	if (ret)
832d35ac78cSryan_chen 		return ret;
833d35ac78cSryan_chen 
834d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
835d35ac78cSryan_chen 
836d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
837d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
838d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
839d35ac78cSryan_chen 		if (ret < 0) {
840d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
841d35ac78cSryan_chen 			continue;
842d35ac78cSryan_chen 		}
843d35ac78cSryan_chen 
844d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
845d35ac78cSryan_chen 		rate = ret;
846d35ac78cSryan_chen 
847d35ac78cSryan_chen 		clk_free(&clk);
848d35ac78cSryan_chen 
849d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
850d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
851d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
852d35ac78cSryan_chen 			continue;
853d35ac78cSryan_chen 		}
854d35ac78cSryan_chen 		if (ret < 0) {
855d35ac78cSryan_chen 			printf("%s %lu: get_rate err: %d\n",
856d35ac78cSryan_chen 			       __func__, aspeed_clk_names[i].id, ret);
857d35ac78cSryan_chen 			continue;
858d35ac78cSryan_chen 		}
859d35ac78cSryan_chen 
860d35ac78cSryan_chen 		printf("%s(%3lu):\t%lu\n",
861d35ac78cSryan_chen 		       aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate);
862d35ac78cSryan_chen 	}
863d35ac78cSryan_chen 
864d35ac78cSryan_chen 	return 0;
865d35ac78cSryan_chen }
866d35ac78cSryan_chen #endif
867d35ac78cSryan_chen 
868d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
869d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
870550e691bSryan_chen 	{ }
871550e691bSryan_chen };
872550e691bSryan_chen 
873aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
874aa36597fSDylan Hung 	.name		= "aspeed_scu",
875550e691bSryan_chen 	.id		= UCLASS_CLK,
876d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
877f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
878f9aa0ee1Sryan_chen 	.ops		= &ast2600_clk_ops,
879d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
880d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
881550e691bSryan_chen };
882