xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision ed30249cd754066490b5232d44f86b1a230e42d4)
1550e691bSryan_chen // SPDX-License-Identifier: GPL-2.0
2550e691bSryan_chen /*
3550e691bSryan_chen  * Copyright (C) ASPEED Technology Inc.
4550e691bSryan_chen  * Ryan Chen <ryan_chen@aspeedtech.com>
5550e691bSryan_chen  */
6550e691bSryan_chen 
7550e691bSryan_chen #include <common.h>
8550e691bSryan_chen #include <clk-uclass.h>
9550e691bSryan_chen #include <dm.h>
10550e691bSryan_chen #include <asm/io.h>
11550e691bSryan_chen #include <dm/lists.h>
1262a6bcbfSryan_chen #include <asm/arch/scu_ast2600.h>
13d6e349c7Sryan_chen #include <dt-bindings/clock/ast2600-clock.h>
1439283ea7Sryan_chen #include <dt-bindings/reset/ast2600-reset.h>
15550e691bSryan_chen 
16550e691bSryan_chen /*
17550e691bSryan_chen  * MAC Clock Delay settings, taken from Aspeed SDK
18550e691bSryan_chen  */
19550e691bSryan_chen #define RGMII_TXCLK_ODLY		8
20550e691bSryan_chen #define RMII_RXCLK_IDLY		2
21550e691bSryan_chen 
22*ed30249cSDylan Hung #define MAC_DEF_DELAY_1G	0x00410410
234760b3f8SDylan Hung #define MAC_DEF_DELAY_100M	0x00810810
244760b3f8SDylan Hung #define MAC_DEF_DELAY_10M	0x00810810
254760b3f8SDylan Hung 
26550e691bSryan_chen /*
27550e691bSryan_chen  * TGMII Clock Duty constants, taken from Aspeed SDK
28550e691bSryan_chen  */
29550e691bSryan_chen #define RGMII2_TXCK_DUTY	0x66
30550e691bSryan_chen #define RGMII1_TXCK_DUTY	0x64
31550e691bSryan_chen 
32550e691bSryan_chen #define D2PLL_DEFAULT_RATE	(250 * 1000 * 1000)
33550e691bSryan_chen 
34550e691bSryan_chen DECLARE_GLOBAL_DATA_PTR;
35550e691bSryan_chen 
36550e691bSryan_chen /*
37550e691bSryan_chen  * Clock divider/multiplier configuration struct.
38550e691bSryan_chen  * For H-PLL and M-PLL the formula is
39550e691bSryan_chen  * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
40550e691bSryan_chen  * M - Numerator
41550e691bSryan_chen  * N - Denumerator
42550e691bSryan_chen  * P - Post Divider
43550e691bSryan_chen  * They have the same layout in their control register.
44550e691bSryan_chen  *
45550e691bSryan_chen  * D-PLL and D2-PLL have extra divider (OD + 1), which is not
46550e691bSryan_chen  * yet needed and ignored by clock configurations.
47550e691bSryan_chen  */
4839283ea7Sryan_chen struct ast2600_div_config {
49550e691bSryan_chen 	unsigned int num;
50550e691bSryan_chen 	unsigned int denum;
51550e691bSryan_chen 	unsigned int post_div;
52550e691bSryan_chen };
53550e691bSryan_chen 
54bbbfb0c5Sryan_chen extern u32 ast2600_get_pll_rate(struct ast2600_scu *scu, int pll_idx)
55550e691bSryan_chen {
56d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
57bbbfb0c5Sryan_chen 	u32 pll_reg = 0;
589639db61Sryan_chen 	unsigned int mult, div = 1;
59550e691bSryan_chen 
60bbbfb0c5Sryan_chen 	switch(pll_idx) {
61bbbfb0c5Sryan_chen 		case ASPEED_CLK_HPLL:
62bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->h_pll_param);
63bbbfb0c5Sryan_chen 			break;
64bbbfb0c5Sryan_chen 		case ASPEED_CLK_MPLL:
65bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->m_pll_param);
66bbbfb0c5Sryan_chen 			break;
67bbbfb0c5Sryan_chen 		case ASPEED_CLK_DPLL:
68bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->d_pll_param);
69bbbfb0c5Sryan_chen 			break;
70bbbfb0c5Sryan_chen 		case ASPEED_CLK_EPLL:
71bbbfb0c5Sryan_chen 			pll_reg = readl(&scu->e_pll_param);
72bbbfb0c5Sryan_chen 			break;
73bbbfb0c5Sryan_chen 
74bbbfb0c5Sryan_chen 	}
75bbbfb0c5Sryan_chen 	if (pll_reg & BIT(24)) {
769639db61Sryan_chen 		/* Pass through mode */
779639db61Sryan_chen 		mult = div = 1;
789639db61Sryan_chen 	} else {
799639db61Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */
80bbbfb0c5Sryan_chen 		u32 m = pll_reg  & 0x1fff;
81bbbfb0c5Sryan_chen 		u32 n = (pll_reg >> 13) & 0x3f;
82bbbfb0c5Sryan_chen 		u32 p = (pll_reg >> 19) & 0xf;
839639db61Sryan_chen 		mult = (m + 1) / (n + 1);
849639db61Sryan_chen 		div = (p + 1);
859639db61Sryan_chen 	}
869639db61Sryan_chen 	return ((clkin * mult)/div);
87550e691bSryan_chen 
88550e691bSryan_chen }
89550e691bSryan_chen 
904f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
91550e691bSryan_chen {
92bbbfb0c5Sryan_chen 	u32 clkin = AST2600_CLK_IN;
9339283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
9439283ea7Sryan_chen 	unsigned int mult, div = 1;
95d6e349c7Sryan_chen 
9639283ea7Sryan_chen 	if (apll_reg & BIT(20)) {
97d6e349c7Sryan_chen 		/* Pass through mode */
98d6e349c7Sryan_chen 		mult = div = 1;
99d6e349c7Sryan_chen 	} else {
100bbbfb0c5Sryan_chen 		/* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
10139283ea7Sryan_chen 		u32 m = (apll_reg >> 5) & 0x3f;
10239283ea7Sryan_chen 		u32 od = (apll_reg >> 4) & 0x1;
10339283ea7Sryan_chen 		u32 n = apll_reg & 0xf;
104d6e349c7Sryan_chen 
105bbbfb0c5Sryan_chen 		mult = (2 - od) * (m + 2);
106bbbfb0c5Sryan_chen 		div = n + 1;
107d6e349c7Sryan_chen 	}
108bbbfb0c5Sryan_chen 	return ((clkin * mult)/div);
10939283ea7Sryan_chen }
11039283ea7Sryan_chen 
111d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
112d812df15Sryan_chen 	2, 2, 3, 5,
113d812df15Sryan_chen };
114d812df15Sryan_chen 
115d812df15Sryan_chen static u32 ast2600_a1_axi_ahb_div_table[] = {
116d812df15Sryan_chen 	4, 6, 2, 4,
117d812df15Sryan_chen };
118d812df15Sryan_chen 
119d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
120d812df15Sryan_chen {
121d812df15Sryan_chen 	u32 hw_rev = readl(&scu->chip_id0);
122d812df15Sryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1);
123d812df15Sryan_chen 	u32 axi_div = 1;
124d812df15Sryan_chen 	u32 ahb_div = 0;
125d812df15Sryan_chen 	u32 rate = 0;
126d812df15Sryan_chen 
127d812df15Sryan_chen 	if((hwstrap1 >> 16) & 0x1)
128d812df15Sryan_chen 		axi_div = 1;
129d812df15Sryan_chen 	else
130d812df15Sryan_chen 		axi_div = 2;
131d812df15Sryan_chen 
132d812df15Sryan_chen 	if (hw_rev & BIT(16))
133d812df15Sryan_chen 		ahb_div = ast2600_a1_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
134d812df15Sryan_chen 	else
135d812df15Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
136d812df15Sryan_chen 
137bbbfb0c5Sryan_chen 	rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
138d812df15Sryan_chen 
1392717883aSryan_chen 	return (rate / axi_div / ahb_div);
1402717883aSryan_chen }
1412717883aSryan_chen 
1422717883aSryan_chen static u32 ast2600_hpll_pclk_div_table[] = {
1432717883aSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
1442717883aSryan_chen };
1452717883aSryan_chen 
1462717883aSryan_chen static u32 ast2600_get_pclk(struct ast2600_scu *scu)
1472717883aSryan_chen {
1482717883aSryan_chen 	u32 clk_sel1 = readl(&scu->clk_sel1);
1492717883aSryan_chen 	u32 apb_div = ast2600_hpll_pclk_div_table[((clk_sel1 >> 23) & 0x7)];
150bbbfb0c5Sryan_chen 	u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
1512717883aSryan_chen 
1522717883aSryan_chen 	return (rate / apb_div);
153d812df15Sryan_chen }
154d812df15Sryan_chen 
155d6e349c7Sryan_chen 
15627881d20Sryan_chen static u32 ast2600_get_uxclk_rate(struct ast2600_scu *scu)
157d6e349c7Sryan_chen {
15827881d20Sryan_chen 	u32 clk_in = 0;
15927881d20Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel4);
160550e691bSryan_chen 
16127881d20Sryan_chen 	uxclk_sel &= 0x3;
16227881d20Sryan_chen 	switch(uxclk_sel) {
16327881d20Sryan_chen 		case 0:
16427881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
16527881d20Sryan_chen 			break;
16627881d20Sryan_chen 		case 1:
16727881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
16827881d20Sryan_chen 			break;
16927881d20Sryan_chen 		case 2:
17027881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
17127881d20Sryan_chen 			break;
17227881d20Sryan_chen 		case 3:
17327881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
17427881d20Sryan_chen 			break;
17527881d20Sryan_chen 	}
176d6e349c7Sryan_chen 
17727881d20Sryan_chen 	return clk_in;
17827881d20Sryan_chen }
17927881d20Sryan_chen 
18027881d20Sryan_chen static u32 ast2600_get_huxclk_rate(struct ast2600_scu *scu)
18127881d20Sryan_chen {
18227881d20Sryan_chen 	u32 clk_in = 0;
18327881d20Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel4);
18427881d20Sryan_chen 
18527881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
18627881d20Sryan_chen 	switch(huclk_sel) {
18727881d20Sryan_chen 		case 0:
18827881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
18927881d20Sryan_chen 			break;
19027881d20Sryan_chen 		case 1:
19127881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
19227881d20Sryan_chen 			break;
19327881d20Sryan_chen 		case 2:
19427881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
19527881d20Sryan_chen 			break;
19627881d20Sryan_chen 		case 3:
19727881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
19827881d20Sryan_chen 			break;
19927881d20Sryan_chen 	}
20027881d20Sryan_chen 
20127881d20Sryan_chen 	return clk_in;
20227881d20Sryan_chen }
20327881d20Sryan_chen 
20427881d20Sryan_chen static u32 ast2600_get_uart_from_uxclk_rate(struct ast2600_scu *scu)
20527881d20Sryan_chen {
20627881d20Sryan_chen 	u32 clk_in = ast2600_get_uxclk_rate(scu);
20727881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
20827881d20Sryan_chen 	unsigned int mult, div;
20927881d20Sryan_chen 
21027881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
21127881d20Sryan_chen 	u32 r = div_reg & 0xff;
21227881d20Sryan_chen 
21327881d20Sryan_chen 	mult = r;
21427881d20Sryan_chen 	div = (n * 4);
21527881d20Sryan_chen 	return (clk_in * mult)/div;
21627881d20Sryan_chen }
21727881d20Sryan_chen 
21827881d20Sryan_chen static u32 ast2600_get_uart_from_huxclk_rate(struct ast2600_scu *scu)
21927881d20Sryan_chen {
22027881d20Sryan_chen 	u32 clk_in = ast2600_get_huxclk_rate(scu);
22127881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
22227881d20Sryan_chen 
22327881d20Sryan_chen 	unsigned int mult, div;
22427881d20Sryan_chen 
22527881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
22627881d20Sryan_chen 	u32 r = div_reg & 0xff;
22727881d20Sryan_chen 
22827881d20Sryan_chen 	mult = r;
22927881d20Sryan_chen 	div = (n * 4);
23027881d20Sryan_chen 	return (clk_in * mult)/div;
23127881d20Sryan_chen }
23227881d20Sryan_chen 
233f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu)
234f51926eeSryan_chen {
235f51926eeSryan_chen 	u32 clkin = 0;
236f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
237f51926eeSryan_chen 	u32 div = (clk_sel >> 28) & 0x7;
238f51926eeSryan_chen 
239f51926eeSryan_chen 	if(clk_sel & BIT(8)) {
240f51926eeSryan_chen 		clkin = ast2600_get_apll_rate(scu);
241f51926eeSryan_chen 	} else {
242f51926eeSryan_chen 		clkin = 200 * 1000 * 1000;
243f51926eeSryan_chen 	}
244f51926eeSryan_chen 	div = (div + 1) << 1;
245f51926eeSryan_chen 
246f51926eeSryan_chen 	return (clkin / div);
247f51926eeSryan_chen }
248f51926eeSryan_chen 
249f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu)
250f51926eeSryan_chen {
251bbbfb0c5Sryan_chen 	u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL);
252f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
253f51926eeSryan_chen 	u32 div = (clk_sel >> 12) & 0x7;
254f51926eeSryan_chen 
255f51926eeSryan_chen 	div = (div + 1) << 2;
256f51926eeSryan_chen 
257f51926eeSryan_chen 	return (clkin / div);
258f51926eeSryan_chen }
259f51926eeSryan_chen 
260f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
26127881d20Sryan_chen {
26227881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
26327881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
26427881d20Sryan_chen 	ulong uart_clk = 0;
26527881d20Sryan_chen 
26627881d20Sryan_chen 	switch(uart_idx) {
26727881d20Sryan_chen 		case 1:
26827881d20Sryan_chen 		case 2:
26927881d20Sryan_chen 		case 3:
27027881d20Sryan_chen 		case 4:
27127881d20Sryan_chen 		case 6:
27227881d20Sryan_chen 			if(uart_sel & BIT(uart_idx - 1))
27327881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
274550e691bSryan_chen 			else
27527881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
27627881d20Sryan_chen 			break;
27727881d20Sryan_chen 		case 5: //24mhz is come form usb phy 48Mhz
27827881d20Sryan_chen 			{
27927881d20Sryan_chen 			u8 uart5_clk_sel = 0;
28027881d20Sryan_chen 			//high bit
28127881d20Sryan_chen 			if (readl(&scu->misc_ctrl1) & BIT(12))
28227881d20Sryan_chen 				uart5_clk_sel = 0x2;
28327881d20Sryan_chen 			else
28427881d20Sryan_chen 				uart5_clk_sel = 0x0;
285550e691bSryan_chen 
28627881d20Sryan_chen 			if (readl(&scu->clk_sel2) & BIT(14))
28727881d20Sryan_chen 				uart5_clk_sel |= 0x1;
288550e691bSryan_chen 
28927881d20Sryan_chen 			switch(uart5_clk_sel) {
29027881d20Sryan_chen 				case 0:
29127881d20Sryan_chen 					uart_clk = 24000000;
29227881d20Sryan_chen 					break;
29327881d20Sryan_chen 				case 1:
29427881d20Sryan_chen 					uart_clk = 0;
29527881d20Sryan_chen 					break;
29627881d20Sryan_chen 				case 2:
29727881d20Sryan_chen 					uart_clk = 24000000/13;
29827881d20Sryan_chen 					break;
29927881d20Sryan_chen 				case 3:
30027881d20Sryan_chen 					uart_clk = 192000000/13;
30127881d20Sryan_chen 					break;
30227881d20Sryan_chen 			}
30327881d20Sryan_chen 			}
30427881d20Sryan_chen 			break;
30527881d20Sryan_chen 		case 7:
30627881d20Sryan_chen 		case 8:
30727881d20Sryan_chen 		case 9:
30827881d20Sryan_chen 		case 10:
30927881d20Sryan_chen 		case 11:
31027881d20Sryan_chen 		case 12:
31127881d20Sryan_chen 		case 13:
31227881d20Sryan_chen 			if(uart_sel5 & BIT(uart_idx - 1))
31327881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
31427881d20Sryan_chen 			else
31527881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
31627881d20Sryan_chen 			break;
31727881d20Sryan_chen 	}
31827881d20Sryan_chen 
31927881d20Sryan_chen 	return uart_clk;
320550e691bSryan_chen }
321550e691bSryan_chen 
322feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
323feb42054Sryan_chen {
324feb42054Sryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
325feb42054Sryan_chen 	ulong rate = 0;
326feb42054Sryan_chen 
327feb42054Sryan_chen 	switch (clk->id) {
328feb42054Sryan_chen 	case ASPEED_CLK_HPLL:
329bbbfb0c5Sryan_chen 	case ASPEED_CLK_EPLL:
330bbbfb0c5Sryan_chen 	case ASPEED_CLK_DPLL:
331d812df15Sryan_chen 	case ASPEED_CLK_MPLL:
332bbbfb0c5Sryan_chen 		rate = ast2600_get_pll_rate(priv->scu, clk->id);
333d812df15Sryan_chen 		break;
334feb42054Sryan_chen 	case ASPEED_CLK_AHB:
335feb42054Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
336feb42054Sryan_chen 		break;
337feb42054Sryan_chen 	case ASPEED_CLK_APB:
3382717883aSryan_chen 		rate = ast2600_get_pclk(priv->scu);
339feb42054Sryan_chen 		break;
340bbbfb0c5Sryan_chen 	case ASPEED_CLK_APLL:
341bbbfb0c5Sryan_chen 		rate = ast2600_get_apll_rate(priv->scu);
342bbbfb0c5Sryan_chen 		break;
343feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
344feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
345feb42054Sryan_chen 		break;
346feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
347feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
348feb42054Sryan_chen 		break;
349feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
350feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
351feb42054Sryan_chen 		break;
352feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
353feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
354feb42054Sryan_chen 		break;
355feb42054Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
356feb42054Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
357feb42054Sryan_chen 		break;
358f51926eeSryan_chen 	case ASPEED_CLK_SDIO:
359f51926eeSryan_chen 		rate = ast2600_get_sdio_clk_rate(priv->scu);
360f51926eeSryan_chen 		break;
361f51926eeSryan_chen 	case ASPEED_CLK_EMMC:
362f51926eeSryan_chen 		rate = ast2600_get_emmc_clk_rate(priv->scu);
363f51926eeSryan_chen 		break;
364feb42054Sryan_chen 	default:
365d812df15Sryan_chen 		pr_debug("can't get clk rate \n");
366feb42054Sryan_chen 		return -ENOENT;
367d812df15Sryan_chen 		break;
368feb42054Sryan_chen 	}
369feb42054Sryan_chen 
370feb42054Sryan_chen 	return rate;
371feb42054Sryan_chen }
372feb42054Sryan_chen 
373550e691bSryan_chen struct aspeed_clock_config {
374550e691bSryan_chen 	ulong input_rate;
375550e691bSryan_chen 	ulong rate;
37639283ea7Sryan_chen 	struct ast2600_div_config cfg;
377550e691bSryan_chen };
378550e691bSryan_chen 
379550e691bSryan_chen static const struct aspeed_clock_config aspeed_clock_config_defaults[] = {
3801cd71a14SDylan Hung 	{ 25000000, 400000000, { .num = 95, .denum = 2, .post_div = 1   } },
38131a90994SDylan Hung 	{ 25000000, 200000000, { .num = 127, .denum = 0, .post_div = 15 } },
38231a90994SDylan Hung 	{ 25000000, 334000000, { .num = 667, .denum = 4, .post_div = 9  } },
383cc476ffcSDylan Hung 	{ 25000000, 1000000000, { .num = 119, .denum = 2, .post_div = 0 } },
384550e691bSryan_chen };
385550e691bSryan_chen 
386550e691bSryan_chen static bool aspeed_get_clock_config_default(ulong input_rate,
387550e691bSryan_chen 					     ulong requested_rate,
38839283ea7Sryan_chen 					     struct ast2600_div_config *cfg)
389550e691bSryan_chen {
390550e691bSryan_chen 	int i;
391550e691bSryan_chen 
392550e691bSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clock_config_defaults); i++) {
393550e691bSryan_chen 		const struct aspeed_clock_config *default_cfg =
394550e691bSryan_chen 			&aspeed_clock_config_defaults[i];
395550e691bSryan_chen 		if (default_cfg->input_rate == input_rate &&
396550e691bSryan_chen 		    default_cfg->rate == requested_rate) {
397550e691bSryan_chen 			*cfg = default_cfg->cfg;
398550e691bSryan_chen 			return true;
399550e691bSryan_chen 		}
400550e691bSryan_chen 	}
401550e691bSryan_chen 
402550e691bSryan_chen 	return false;
403550e691bSryan_chen }
404550e691bSryan_chen 
405550e691bSryan_chen /*
406550e691bSryan_chen  * @input_rate - the rate of input clock in Hz
407550e691bSryan_chen  * @requested_rate - desired output rate in Hz
408550e691bSryan_chen  * @div - this is an IN/OUT parameter, at input all fields of the config
409550e691bSryan_chen  * need to be set to their maximum allowed values.
410550e691bSryan_chen  * The result (the best config we could find), would also be returned
411550e691bSryan_chen  * in this structure.
412550e691bSryan_chen  *
413550e691bSryan_chen  * @return The clock rate, when the resulting div_config is used.
414550e691bSryan_chen  */
415550e691bSryan_chen static ulong aspeed_calc_clock_config(ulong input_rate, ulong requested_rate,
41639283ea7Sryan_chen 				       struct ast2600_div_config *cfg)
417550e691bSryan_chen {
418550e691bSryan_chen 	/*
419550e691bSryan_chen 	 * The assumption is that kHz precision is good enough and
420550e691bSryan_chen 	 * also enough to avoid overflow when multiplying.
421550e691bSryan_chen 	 */
422550e691bSryan_chen 	const ulong input_rate_khz = input_rate / 1000;
423550e691bSryan_chen 	const ulong rate_khz = requested_rate / 1000;
42439283ea7Sryan_chen 	const struct ast2600_div_config max_vals = *cfg;
42539283ea7Sryan_chen 	struct ast2600_div_config it = { 0, 0, 0 };
426550e691bSryan_chen 	ulong delta = rate_khz;
427550e691bSryan_chen 	ulong new_rate_khz = 0;
428550e691bSryan_chen 
429550e691bSryan_chen 	/*
430550e691bSryan_chen 	 * Look for a well known frequency first.
431550e691bSryan_chen 	 */
432550e691bSryan_chen 	if (aspeed_get_clock_config_default(input_rate, requested_rate, cfg))
433550e691bSryan_chen 		return requested_rate;
434550e691bSryan_chen 
435550e691bSryan_chen 	for (; it.denum <= max_vals.denum; ++it.denum) {
436550e691bSryan_chen 		for (it.post_div = 0; it.post_div <= max_vals.post_div;
437550e691bSryan_chen 		     ++it.post_div) {
438550e691bSryan_chen 			it.num = (rate_khz * (it.post_div + 1) / input_rate_khz)
439550e691bSryan_chen 			    * (it.denum + 1);
440550e691bSryan_chen 			if (it.num > max_vals.num)
441550e691bSryan_chen 				continue;
442550e691bSryan_chen 
443550e691bSryan_chen 			new_rate_khz = (input_rate_khz
444550e691bSryan_chen 					* ((it.num + 1) / (it.denum + 1)))
445550e691bSryan_chen 			    / (it.post_div + 1);
446550e691bSryan_chen 
447550e691bSryan_chen 			/* Keep the rate below requested one. */
448550e691bSryan_chen 			if (new_rate_khz > rate_khz)
449550e691bSryan_chen 				continue;
450550e691bSryan_chen 
451550e691bSryan_chen 			if (new_rate_khz - rate_khz < delta) {
452550e691bSryan_chen 				delta = new_rate_khz - rate_khz;
453550e691bSryan_chen 				*cfg = it;
454550e691bSryan_chen 				if (delta == 0)
455550e691bSryan_chen 					return new_rate_khz * 1000;
456550e691bSryan_chen 			}
457550e691bSryan_chen 		}
458550e691bSryan_chen 	}
459550e691bSryan_chen 
460550e691bSryan_chen 	return new_rate_khz * 1000;
461550e691bSryan_chen }
462550e691bSryan_chen 
463feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate)
464550e691bSryan_chen {
465d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
466550e691bSryan_chen 	u32 mpll_reg;
46739283ea7Sryan_chen 	struct ast2600_div_config div_cfg = {
468e9526877SDylan Hung 		.num = 0x1fff,			/* SCU220 bit[12:0] */
469e9526877SDylan Hung 		.denum = 0x3f,			/* SCU220 bit[18:13] */
470e9526877SDylan Hung 		.post_div = 0xf,		/* SCU220 bit[22:19] */
471550e691bSryan_chen 	};
472550e691bSryan_chen 
473550e691bSryan_chen 	aspeed_calc_clock_config(clkin, rate, &div_cfg);
474550e691bSryan_chen 
475feb42054Sryan_chen 	mpll_reg = readl(&scu->m_pll_param);
476e9526877SDylan Hung 	mpll_reg &= ~0x7fffff;
477e9526877SDylan Hung 	mpll_reg |= (div_cfg.post_div << 19)
478e9526877SDylan Hung 	    | (div_cfg.denum << 13)
479e9526877SDylan Hung 	    | (div_cfg.num << 0);
480550e691bSryan_chen 
481feb42054Sryan_chen 	writel(mpll_reg, &scu->m_pll_param);
482550e691bSryan_chen 
483cc476ffcSDylan Hung 	return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL);
484d6e349c7Sryan_chen }
485d6e349c7Sryan_chen 
486d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
487550e691bSryan_chen {
488f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
489550e691bSryan_chen 
490550e691bSryan_chen 	ulong new_rate;
491550e691bSryan_chen 	switch (clk->id) {
492f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
493feb42054Sryan_chen 		new_rate = ast2600_configure_ddr(priv->scu, rate);
494550e691bSryan_chen 		break;
495550e691bSryan_chen 	default:
496550e691bSryan_chen 		return -ENOENT;
497550e691bSryan_chen 	}
498550e691bSryan_chen 
499550e691bSryan_chen 	return new_rate;
500550e691bSryan_chen }
501feb42054Sryan_chen 
502f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
503f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
504f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
505f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
506f9aa0ee1Sryan_chen 
507cc476ffcSDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu)
508cc476ffcSDylan Hung {
509894c19cfSDylan Hung 	u32 clksel;
5104760b3f8SDylan Hung 
511cc476ffcSDylan Hung 	writel(readl(&scu->mac34_clk_delay) & ~BIT(31), &scu->mac34_clk_delay);
512894c19cfSDylan Hung 
513894c19cfSDylan Hung 	/* MAC AHB = HCLK / 2 */
514894c19cfSDylan Hung 	clksel = readl(&scu->clk_sel4);
515894c19cfSDylan Hung 	clksel &= ~GENMASK(26, 24);
516894c19cfSDylan Hung 	writel(clksel, &scu->clk_sel4);
517cc476ffcSDylan Hung 	return 0;
518cc476ffcSDylan Hung }
519cc476ffcSDylan Hung 
520cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu)
521cc476ffcSDylan Hung {
522cc476ffcSDylan Hung 	u32 epll_reg;
523cc476ffcSDylan Hung 	u32 clksel;
5244760b3f8SDylan Hung 	u32 clkdelay;
525cc476ffcSDylan Hung 
526cc476ffcSDylan Hung 	struct ast2600_div_config div_cfg = {
527cc476ffcSDylan Hung 		.num = 0x1fff,			/* SCU240 bit[12:0] */
528cc476ffcSDylan Hung 		.denum = 0x3f,			/* SCU240 bit[18:13] */
529cc476ffcSDylan Hung 		.post_div = 0xf,		/* SCU240 bit[22:19] */
530cc476ffcSDylan Hung 	};
531cc476ffcSDylan Hung 
532cc476ffcSDylan Hung 	/* configure E-PLL 1000M */
533cc476ffcSDylan Hung 	aspeed_calc_clock_config(AST2600_CLK_IN, 1000000000, &div_cfg);
534cc476ffcSDylan Hung 	epll_reg = readl(&scu->e_pll_param);
535cc476ffcSDylan Hung 	epll_reg &= ~GENMASK(22, 0);
536cc476ffcSDylan Hung 	epll_reg |= (div_cfg.post_div << 19)
537cc476ffcSDylan Hung 	    | (div_cfg.denum << 13)
538cc476ffcSDylan Hung 	    | (div_cfg.num << 0);
539cc476ffcSDylan Hung 
540cc476ffcSDylan Hung 	writel(epll_reg, &scu->e_pll_param);
541cc476ffcSDylan Hung 
542cc476ffcSDylan Hung 	/* select MAC#1 and MAC#2 clock source = EPLL / 8 */
543cc476ffcSDylan Hung 	clksel = readl(&scu->clk_sel2);
544cc476ffcSDylan Hung 	clksel &= ~BIT(23);
545cc476ffcSDylan Hung 	clksel |= 0x7 << 20;
546cc476ffcSDylan Hung 	writel(clksel, &scu->clk_sel2);
547cc476ffcSDylan Hung 
5484760b3f8SDylan Hung 	/*
5494760b3f8SDylan Hung 	BIT(31): select RGMII 125M from internal source
5504760b3f8SDylan Hung 	BIT(28): RGMII 125M output enable
5514760b3f8SDylan Hung 	BIT(25:0): 1G default delay
5524760b3f8SDylan Hung 	*/
5534760b3f8SDylan Hung 	clkdelay = MAC_DEF_DELAY_1G | BIT(31) | BIT(28);
5544760b3f8SDylan Hung 	writel(clkdelay, &scu->mac12_clk_delay);
5554760b3f8SDylan Hung 
5564760b3f8SDylan Hung 	/* set 100M/10M default delay */
5574760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M);
5584760b3f8SDylan Hung 	writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M);
559cc476ffcSDylan Hung 
560*ed30249cSDylan Hung 	/* MAC AHB = HPLL / 6 */
561894c19cfSDylan Hung 	clksel = readl(&scu->clk_sel1);
562894c19cfSDylan Hung 	clksel &= ~GENMASK(18, 16);
563*ed30249cSDylan Hung 	clksel |= 0x2 << 16;
564894c19cfSDylan Hung 	writel(clksel, &scu->clk_sel1);
565894c19cfSDylan Hung 
566cc476ffcSDylan Hung 	return 0;
567cc476ffcSDylan Hung }
568cc476ffcSDylan Hung 
569f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
570f9aa0ee1Sryan_chen {
571f9aa0ee1Sryan_chen 	u32 reset_bit;
572f9aa0ee1Sryan_chen 	u32 clkstop_bit;
573f9aa0ee1Sryan_chen 
574cc476ffcSDylan Hung 	if (index < 3)
575cc476ffcSDylan Hung 		ast2600_configure_mac12_clk(scu);
576cc476ffcSDylan Hung 	else
577cc476ffcSDylan Hung 		ast2600_configure_mac34_clk(scu);
578f9aa0ee1Sryan_chen 
579f9aa0ee1Sryan_chen 	switch (index) {
580f9aa0ee1Sryan_chen 	case 1:
581f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
582f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
583f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
584f9aa0ee1Sryan_chen 		udelay(100);
585f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
586f9aa0ee1Sryan_chen 		mdelay(10);
587f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
588f9aa0ee1Sryan_chen 
589f9aa0ee1Sryan_chen 		break;
590f9aa0ee1Sryan_chen 	case 2:
591f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
592f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
593f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
594f9aa0ee1Sryan_chen 		udelay(100);
595f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
596f9aa0ee1Sryan_chen 		mdelay(10);
597f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
598f9aa0ee1Sryan_chen 		break;
599f9aa0ee1Sryan_chen 	case 3:
600f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
601f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
602f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
603f9aa0ee1Sryan_chen 		udelay(100);
604f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
605f9aa0ee1Sryan_chen 		mdelay(10);
606f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
607f9aa0ee1Sryan_chen 		break;
608f9aa0ee1Sryan_chen 	case 4:
609f9aa0ee1Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
610f9aa0ee1Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
611f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
612f9aa0ee1Sryan_chen 		udelay(100);
613f9aa0ee1Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
614f9aa0ee1Sryan_chen 		mdelay(10);
615f9aa0ee1Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
616f9aa0ee1Sryan_chen 		break;
617f9aa0ee1Sryan_chen 	default:
618f9aa0ee1Sryan_chen 		return -EINVAL;
619f9aa0ee1Sryan_chen 	}
620f9aa0ee1Sryan_chen 
621f9aa0ee1Sryan_chen 	return 0;
622f9aa0ee1Sryan_chen }
623550e691bSryan_chen 
624f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4
625f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu)
626f51926eeSryan_chen {
627f51926eeSryan_chen 	u32 reset_bit;
628f51926eeSryan_chen 	u32 clkstop_bit;
629f51926eeSryan_chen 
630f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_SD - 32);
631f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_SDIO);
632f51926eeSryan_chen 
633f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl2);
634f51926eeSryan_chen 	udelay(100);
635f51926eeSryan_chen 	//enable clk
636f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
637f51926eeSryan_chen 	mdelay(10);
638f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
639f51926eeSryan_chen 
640f51926eeSryan_chen 	return 0;
641f51926eeSryan_chen }
642f51926eeSryan_chen 
643f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31
644f51926eeSryan_chen #define SCU_CLK_SD_MASK				(0x7 << 28)
645f51926eeSryan_chen #define SCU_CLK_SD_DIV(x)			(x << 28)
646f51926eeSryan_chen 
647f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu)
648f51926eeSryan_chen {
649f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel4);
650f51926eeSryan_chen 	u32 enableclk_bit;
651f51926eeSryan_chen 
652f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
653f51926eeSryan_chen 
654f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
655f51926eeSryan_chen 	clk_sel |= SCU_CLK_SD_DIV(0);
656f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel4);
657f51926eeSryan_chen 
658f51926eeSryan_chen 	//enable clk
659f51926eeSryan_chen 	setbits_le32(&scu->clk_sel4, enableclk_bit);
660f51926eeSryan_chen 
661f51926eeSryan_chen 	return 0;
662f51926eeSryan_chen }
663f51926eeSryan_chen 
664f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27
665f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu)
666f51926eeSryan_chen {
667f51926eeSryan_chen 	u32 reset_bit;
668f51926eeSryan_chen 	u32 clkstop_bit;
669f51926eeSryan_chen 
670f51926eeSryan_chen 	reset_bit = BIT(ASPEED_RESET_EMMC);
671f51926eeSryan_chen 	clkstop_bit = BIT(SCU_CLKSTOP_EMMC);
672f51926eeSryan_chen 
673f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_clr_ctrl1);
674f51926eeSryan_chen 	udelay(100);
675f51926eeSryan_chen 	//enable clk
676f51926eeSryan_chen 	writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
677f51926eeSryan_chen 	mdelay(10);
678f51926eeSryan_chen 	writel(reset_bit, &scu->sysreset_ctrl2);
679f51926eeSryan_chen 
680f51926eeSryan_chen 	return 0;
681f51926eeSryan_chen }
682f51926eeSryan_chen 
683f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15
684f51926eeSryan_chen #define SCU_CLK_EMMC_MASK			(0x7 << 12)
685f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x)			(x << 12)
686f51926eeSryan_chen 
687f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu)
688f51926eeSryan_chen {
689f51926eeSryan_chen 	u32 clk_sel = readl(&scu->clk_sel1);
690f51926eeSryan_chen 	u32 enableclk_bit;
691f51926eeSryan_chen 
692f51926eeSryan_chen 	enableclk_bit = BIT(SCU_CLKSTOP_EXTSD);
693f51926eeSryan_chen 
694f51926eeSryan_chen 	clk_sel &= ~SCU_CLK_SD_MASK;
695f51926eeSryan_chen 	clk_sel |= SCU_CLK_SD_DIV(1);
696f51926eeSryan_chen 	writel(clk_sel, &scu->clk_sel1);
697f51926eeSryan_chen 
698f51926eeSryan_chen 	//enable clk
699f51926eeSryan_chen 	setbits_le32(&scu->clk_sel1, enableclk_bit);
700f51926eeSryan_chen 
701f51926eeSryan_chen 	return 0;
702f51926eeSryan_chen }
703f51926eeSryan_chen 
704d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
705550e691bSryan_chen {
706f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
707550e691bSryan_chen 
708550e691bSryan_chen 	switch (clk->id) {
70986f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC1CLK:
71086f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 1);
711550e691bSryan_chen 			break;
71286f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC2CLK:
71386f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 2);
714550e691bSryan_chen 			break;
71577843939Sryan_chen 		case ASPEED_CLK_GATE_MAC3CLK:
71677843939Sryan_chen 			ast2600_configure_mac(priv->scu, 3);
71777843939Sryan_chen 			break;
71877843939Sryan_chen 		case ASPEED_CLK_GATE_MAC4CLK:
71977843939Sryan_chen 			ast2600_configure_mac(priv->scu, 4);
72077843939Sryan_chen 			break;
721f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDCLK:
722f51926eeSryan_chen 			ast2600_enable_sdclk(priv->scu);
723f51926eeSryan_chen 			break;
724f51926eeSryan_chen 		case ASPEED_CLK_GATE_SDEXTCLK:
725f51926eeSryan_chen 			ast2600_enable_extsdclk(priv->scu);
726f51926eeSryan_chen 			break;
727f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCCLK:
728f51926eeSryan_chen 			ast2600_enable_emmcclk(priv->scu);
729f51926eeSryan_chen 			break;
730f51926eeSryan_chen 		case ASPEED_CLK_GATE_EMMCEXTCLK:
731f51926eeSryan_chen 			ast2600_enable_extemmcclk(priv->scu);
732f51926eeSryan_chen 			break;
733550e691bSryan_chen 		default:
734f9aa0ee1Sryan_chen 			pr_debug("can't enable clk \n");
735550e691bSryan_chen 			return -ENOENT;
73677843939Sryan_chen 			break;
737550e691bSryan_chen 	}
738550e691bSryan_chen 
739550e691bSryan_chen 	return 0;
740550e691bSryan_chen }
741550e691bSryan_chen 
742f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = {
743d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
744d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
745d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
746550e691bSryan_chen };
747550e691bSryan_chen 
748d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
749550e691bSryan_chen {
750f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
751550e691bSryan_chen 
752f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
753f0d895afSryan_chen 	if (IS_ERR(priv->scu))
754f0d895afSryan_chen 		return PTR_ERR(priv->scu);
755550e691bSryan_chen 
756550e691bSryan_chen 	return 0;
757550e691bSryan_chen }
758550e691bSryan_chen 
759d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
760550e691bSryan_chen {
761550e691bSryan_chen 	int ret;
762550e691bSryan_chen 
763550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
764550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
765550e691bSryan_chen 	if (ret)
766550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
767550e691bSryan_chen 
768550e691bSryan_chen 	return 0;
769550e691bSryan_chen }
770550e691bSryan_chen 
771d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK)
772d35ac78cSryan_chen struct aspeed_clks {
773d35ac78cSryan_chen 	ulong id;
774d35ac78cSryan_chen 	const char *name;
775d35ac78cSryan_chen };
776d35ac78cSryan_chen 
777d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = {
778d35ac78cSryan_chen 	{ ASPEED_CLK_HPLL, "hpll" },
779d35ac78cSryan_chen 	{ ASPEED_CLK_MPLL, "mpll" },
780d35ac78cSryan_chen 	{ ASPEED_CLK_APLL, "apll" },
781d35ac78cSryan_chen 	{ ASPEED_CLK_EPLL, "epll" },
782d35ac78cSryan_chen 	{ ASPEED_CLK_DPLL, "dpll" },
783d35ac78cSryan_chen 	{ ASPEED_CLK_AHB, "hclk" },
784d35ac78cSryan_chen 	{ ASPEED_CLK_APB, "pclk" },
785d35ac78cSryan_chen };
786d35ac78cSryan_chen 
787d35ac78cSryan_chen int soc_clk_dump(void)
788d35ac78cSryan_chen {
789d35ac78cSryan_chen 	struct udevice *dev;
790d35ac78cSryan_chen 	struct clk clk;
791d35ac78cSryan_chen 	unsigned long rate;
792d35ac78cSryan_chen 	int i, ret;
793d35ac78cSryan_chen 
794d35ac78cSryan_chen 	ret = uclass_get_device_by_driver(UCLASS_CLK,
795d35ac78cSryan_chen 					  DM_GET_DRIVER(aspeed_scu), &dev);
796d35ac78cSryan_chen 	if (ret)
797d35ac78cSryan_chen 		return ret;
798d35ac78cSryan_chen 
799d35ac78cSryan_chen 	printf("Clk\t\tHz\n");
800d35ac78cSryan_chen 
801d35ac78cSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) {
802d35ac78cSryan_chen 		clk.id = aspeed_clk_names[i].id;
803d35ac78cSryan_chen 		ret = clk_request(dev, &clk);
804d35ac78cSryan_chen 		if (ret < 0) {
805d35ac78cSryan_chen 			debug("%s clk_request() failed: %d\n", __func__, ret);
806d35ac78cSryan_chen 			continue;
807d35ac78cSryan_chen 		}
808d35ac78cSryan_chen 
809d35ac78cSryan_chen 		ret = clk_get_rate(&clk);
810d35ac78cSryan_chen 		rate = ret;
811d35ac78cSryan_chen 
812d35ac78cSryan_chen 		clk_free(&clk);
813d35ac78cSryan_chen 
814d35ac78cSryan_chen 		if (ret == -ENOTSUPP) {
815d35ac78cSryan_chen 			printf("clk ID %lu not supported yet\n",
816d35ac78cSryan_chen 			       aspeed_clk_names[i].id);
817d35ac78cSryan_chen 			continue;
818d35ac78cSryan_chen 		}
819d35ac78cSryan_chen 		if (ret < 0) {
820d35ac78cSryan_chen 			printf("%s %lu: get_rate err: %d\n",
821d35ac78cSryan_chen 			       __func__, aspeed_clk_names[i].id, ret);
822d35ac78cSryan_chen 			continue;
823d35ac78cSryan_chen 		}
824d35ac78cSryan_chen 
825d35ac78cSryan_chen 		printf("%s(%3lu):\t%lu\n",
826d35ac78cSryan_chen 		       aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate);
827d35ac78cSryan_chen 	}
828d35ac78cSryan_chen 
829d35ac78cSryan_chen 	return 0;
830d35ac78cSryan_chen }
831d35ac78cSryan_chen #endif
832d35ac78cSryan_chen 
833d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
834d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
835550e691bSryan_chen 	{ }
836550e691bSryan_chen };
837550e691bSryan_chen 
838aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
839aa36597fSDylan Hung 	.name		= "aspeed_scu",
840550e691bSryan_chen 	.id		= UCLASS_CLK,
841d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
842f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
843f9aa0ee1Sryan_chen 	.ops		= &ast2600_clk_ops,
844d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
845d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
846550e691bSryan_chen };
847