xref: /openbmc/u-boot/drivers/clk/aspeed/clk_ast2600.c (revision 77843939f5ad6e6de7ddb4e05826aae462654666)
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 
22550e691bSryan_chen /*
23550e691bSryan_chen  * TGMII Clock Duty constants, taken from Aspeed SDK
24550e691bSryan_chen  */
25550e691bSryan_chen #define RGMII2_TXCK_DUTY	0x66
26550e691bSryan_chen #define RGMII1_TXCK_DUTY	0x64
27550e691bSryan_chen 
28550e691bSryan_chen #define D2PLL_DEFAULT_RATE	(250 * 1000 * 1000)
29550e691bSryan_chen 
30550e691bSryan_chen DECLARE_GLOBAL_DATA_PTR;
31550e691bSryan_chen 
32550e691bSryan_chen /*
33550e691bSryan_chen  * Clock divider/multiplier configuration struct.
34550e691bSryan_chen  * For H-PLL and M-PLL the formula is
35550e691bSryan_chen  * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
36550e691bSryan_chen  * M - Numerator
37550e691bSryan_chen  * N - Denumerator
38550e691bSryan_chen  * P - Post Divider
39550e691bSryan_chen  * They have the same layout in their control register.
40550e691bSryan_chen  *
41550e691bSryan_chen  * D-PLL and D2-PLL have extra divider (OD + 1), which is not
42550e691bSryan_chen  * yet needed and ignored by clock configurations.
43550e691bSryan_chen  */
4439283ea7Sryan_chen struct ast2600_div_config {
45550e691bSryan_chen 	unsigned int num;
46550e691bSryan_chen 	unsigned int denum;
47550e691bSryan_chen 	unsigned int post_div;
48550e691bSryan_chen };
49550e691bSryan_chen 
50550e691bSryan_chen /*
51550e691bSryan_chen  * Get the rate of the M-PLL clock from input clock frequency and
52550e691bSryan_chen  * the value of the M-PLL Parameter Register.
53550e691bSryan_chen  */
544f22e838Sryan_chen extern u32 ast2600_get_mpll_rate(struct ast2600_scu *scu)
55550e691bSryan_chen {
56d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
5739283ea7Sryan_chen 	u32 mpll_reg = readl(&scu->m_pll_param);
589639db61Sryan_chen 	unsigned int mult, div = 1;
59550e691bSryan_chen 
609639db61Sryan_chen 	if (mpll_reg & BIT(24)) {
619639db61Sryan_chen 		/* Pass through mode */
629639db61Sryan_chen 		mult = div = 1;
639639db61Sryan_chen 	} else {
649639db61Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */
659639db61Sryan_chen 		u32 m = mpll_reg  & 0x1fff;
669639db61Sryan_chen 		u32 n = (mpll_reg >> 13) & 0x3f;
679639db61Sryan_chen 		u32 p = (mpll_reg >> 19) & 0xf;
689639db61Sryan_chen 		mult = (m + 1) / (n + 1);
699639db61Sryan_chen 		div = (p + 1);
709639db61Sryan_chen 	}
719639db61Sryan_chen 	return ((clkin * mult)/div);
72550e691bSryan_chen 
73550e691bSryan_chen }
74550e691bSryan_chen 
75550e691bSryan_chen /*
76550e691bSryan_chen  * Get the rate of the H-PLL clock from input clock frequency and
77550e691bSryan_chen  * the value of the H-PLL Parameter Register.
78550e691bSryan_chen  */
794f22e838Sryan_chen extern u32 ast2600_get_hpll_rate(struct ast2600_scu *scu)
80550e691bSryan_chen {
814f22e838Sryan_chen 	u32 clkin = AST2600_CLK_IN;
8239283ea7Sryan_chen 	u32 hpll_reg = readl(&scu->h_pll_param);
834f22e838Sryan_chen 	unsigned int mult, div = 1;
84f0d895afSryan_chen 
854f22e838Sryan_chen 	if (hpll_reg & BIT(24)) {
864f22e838Sryan_chen 		/* Pass through mode */
874f22e838Sryan_chen 		mult = div = 1;
884f22e838Sryan_chen 	} else {
894f22e838Sryan_chen 		/* F = 25Mhz * [(M + 1) / (n + 1)] / (p + 1) */
904f22e838Sryan_chen 		u32 m = (hpll_reg & 0x1fff);
914f22e838Sryan_chen 		u32 n = (hpll_reg >> 13) & 0x3f;
924f22e838Sryan_chen 		u32 p = (hpll_reg >> 19) & 0xf;
934f22e838Sryan_chen 		mult = (m + 1) / (n + 1);
944f22e838Sryan_chen 		div = (p + 1);
954f22e838Sryan_chen 	}
964f22e838Sryan_chen 	return ((clkin * mult)/div);
97550e691bSryan_chen }
98550e691bSryan_chen 
9927881d20Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = {
10027881d20Sryan_chen 	2, 2, 3, 5,
10127881d20Sryan_chen };
10227881d20Sryan_chen 
10327881d20Sryan_chen static u32 ast2600_a1_axi_ahb_div_table[] = {
10427881d20Sryan_chen 	4, 6, 2, 4,
10527881d20Sryan_chen };
10627881d20Sryan_chen 
10727881d20Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu)
10827881d20Sryan_chen {
10927881d20Sryan_chen 	u32 hw_rev = readl(&scu->chip_id0);
11027881d20Sryan_chen 	u32 hwstrap1 = readl(&scu->hwstrap1);
11127881d20Sryan_chen 	u32 axi_div = 1;
11227881d20Sryan_chen 	u32 ahb_div = 0;
11327881d20Sryan_chen 	u32 rate = 0;
11427881d20Sryan_chen 
11527881d20Sryan_chen 	if((hwstrap1 >> 16) & 0x1)
11627881d20Sryan_chen 		axi_div = 1;
11727881d20Sryan_chen 	else
11827881d20Sryan_chen 		axi_div = 2;
11927881d20Sryan_chen 
12027881d20Sryan_chen 	if (hw_rev & BIT(16))
12127881d20Sryan_chen 		ahb_div = ast2600_a1_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
12227881d20Sryan_chen 	else
12327881d20Sryan_chen 		ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3];
12427881d20Sryan_chen 
12527881d20Sryan_chen 	rate = ast2600_get_hpll_rate(scu);
12627881d20Sryan_chen 	rate = rate / axi_div / ahb_div;
12727881d20Sryan_chen 
12827881d20Sryan_chen 	return rate;
12927881d20Sryan_chen }
13027881d20Sryan_chen 
1314f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu)
132550e691bSryan_chen {
13339283ea7Sryan_chen 	u32 clk_in = AST2600_CLK_IN;
13439283ea7Sryan_chen 	u32 apll_reg = readl(&scu->a_pll_param);
13539283ea7Sryan_chen 	unsigned int mult, div = 1;
136d6e349c7Sryan_chen 
13739283ea7Sryan_chen 	if (apll_reg & BIT(20)) {
138d6e349c7Sryan_chen 		/* Pass through mode */
139d6e349c7Sryan_chen 		mult = div = 1;
140d6e349c7Sryan_chen 	} else {
141d6e349c7Sryan_chen 		/* F = 25Mhz * (2-OD) * [(M + 2) / (n + 1)] */
14239283ea7Sryan_chen 		u32 m = (apll_reg >> 5) & 0x3f;
14339283ea7Sryan_chen 		u32 od = (apll_reg >> 4) & 0x1;
14439283ea7Sryan_chen 		u32 n = apll_reg & 0xf;
145d6e349c7Sryan_chen 
14639283ea7Sryan_chen 		mult = (2 - od) * ((m + 2) / (n + 1));
147d6e349c7Sryan_chen 	}
148d6e349c7Sryan_chen 	return (clk_in * mult)/div;
149d6e349c7Sryan_chen }
150d6e349c7Sryan_chen 
1514f22e838Sryan_chen extern u32 ast2600_get_epll_rate(struct ast2600_scu *scu)
15239283ea7Sryan_chen {
15339283ea7Sryan_chen 	u32 clk_in = AST2600_CLK_IN;
15439283ea7Sryan_chen 	u32 epll_reg = readl(&scu->e_pll_param);
15539283ea7Sryan_chen 	unsigned int mult, div = 1;
15639283ea7Sryan_chen 
15739283ea7Sryan_chen 	if (epll_reg & BIT(24)) {
15839283ea7Sryan_chen 		/* Pass through mode */
15939283ea7Sryan_chen 		mult = div = 1;
16039283ea7Sryan_chen 	} else {
16139283ea7Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1)*/
16239283ea7Sryan_chen 		u32 m = epll_reg  & 0x1fff;
16339283ea7Sryan_chen 		u32 n = (epll_reg >> 13) & 0x3f;
16439283ea7Sryan_chen 		u32 p = (epll_reg >> 19) & 0x7;
16539283ea7Sryan_chen 
16639283ea7Sryan_chen 		mult = ((m + 1) / (n + 1));
16739283ea7Sryan_chen 		div = (p + 1);
16839283ea7Sryan_chen 	}
16939283ea7Sryan_chen 	return (clk_in * mult)/div;
17039283ea7Sryan_chen }
17139283ea7Sryan_chen 
1724f22e838Sryan_chen extern u32 ast2600_get_dpll_rate(struct ast2600_scu *scu)
17339283ea7Sryan_chen {
17439283ea7Sryan_chen 	u32 clk_in = AST2600_CLK_IN;
17539283ea7Sryan_chen 	u32 dpll_reg = readl(&scu->d_pll_param);
17639283ea7Sryan_chen 	unsigned int mult, div = 1;
17739283ea7Sryan_chen 
17839283ea7Sryan_chen 	if (dpll_reg & BIT(24)) {
17939283ea7Sryan_chen 		/* Pass through mode */
18039283ea7Sryan_chen 		mult = div = 1;
18139283ea7Sryan_chen 	} else {
18239283ea7Sryan_chen 		/* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1)*/
18339283ea7Sryan_chen 		u32 m = dpll_reg  & 0x1fff;
18439283ea7Sryan_chen 		u32 n = (dpll_reg >> 13) & 0x3f;
18539283ea7Sryan_chen 		u32 p = (dpll_reg >> 19) & 0x7;
18639283ea7Sryan_chen 		mult = ((m + 1) / (n + 1));
18739283ea7Sryan_chen 		div = (p + 1);
18839283ea7Sryan_chen 	}
18939283ea7Sryan_chen 	return (clk_in * mult)/div;
19039283ea7Sryan_chen }
191d6e349c7Sryan_chen 
19227881d20Sryan_chen static u32 ast2600_get_uxclk_rate(struct ast2600_scu *scu)
193d6e349c7Sryan_chen {
19427881d20Sryan_chen 	u32 clk_in = 0;
19527881d20Sryan_chen 	u32 uxclk_sel = readl(&scu->clk_sel4);
196550e691bSryan_chen 
19727881d20Sryan_chen 	uxclk_sel &= 0x3;
19827881d20Sryan_chen 	switch(uxclk_sel) {
19927881d20Sryan_chen 		case 0:
20027881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
20127881d20Sryan_chen 			break;
20227881d20Sryan_chen 		case 1:
20327881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
20427881d20Sryan_chen 			break;
20527881d20Sryan_chen 		case 2:
20627881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
20727881d20Sryan_chen 			break;
20827881d20Sryan_chen 		case 3:
20927881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
21027881d20Sryan_chen 			break;
21127881d20Sryan_chen 	}
212d6e349c7Sryan_chen 
21327881d20Sryan_chen 	return clk_in;
21427881d20Sryan_chen }
21527881d20Sryan_chen 
21627881d20Sryan_chen static u32 ast2600_get_huxclk_rate(struct ast2600_scu *scu)
21727881d20Sryan_chen {
21827881d20Sryan_chen 	u32 clk_in = 0;
21927881d20Sryan_chen 	u32 huclk_sel = readl(&scu->clk_sel4);
22027881d20Sryan_chen 
22127881d20Sryan_chen 	huclk_sel = ((huclk_sel >> 3) & 0x3);
22227881d20Sryan_chen 	switch(huclk_sel) {
22327881d20Sryan_chen 		case 0:
22427881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 4;
22527881d20Sryan_chen 			break;
22627881d20Sryan_chen 		case 1:
22727881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu) / 2;
22827881d20Sryan_chen 			break;
22927881d20Sryan_chen 		case 2:
23027881d20Sryan_chen 			clk_in = ast2600_get_apll_rate(scu);
23127881d20Sryan_chen 			break;
23227881d20Sryan_chen 		case 3:
23327881d20Sryan_chen 			clk_in = ast2600_get_hclk(scu);
23427881d20Sryan_chen 			break;
23527881d20Sryan_chen 	}
23627881d20Sryan_chen 
23727881d20Sryan_chen 	return clk_in;
23827881d20Sryan_chen }
23927881d20Sryan_chen 
24027881d20Sryan_chen static u32 ast2600_get_uart_from_uxclk_rate(struct ast2600_scu *scu)
24127881d20Sryan_chen {
24227881d20Sryan_chen 	u32 clk_in = ast2600_get_uxclk_rate(scu);
24327881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_uxclk);
24427881d20Sryan_chen 	unsigned int mult, div;
24527881d20Sryan_chen 
24627881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
24727881d20Sryan_chen 	u32 r = div_reg & 0xff;
24827881d20Sryan_chen 
24927881d20Sryan_chen 	mult = r;
25027881d20Sryan_chen 	div = (n * 4);
25127881d20Sryan_chen 	return (clk_in * mult)/div;
25227881d20Sryan_chen }
25327881d20Sryan_chen 
25427881d20Sryan_chen static u32 ast2600_get_uart_from_huxclk_rate(struct ast2600_scu *scu)
25527881d20Sryan_chen {
25627881d20Sryan_chen 	u32 clk_in = ast2600_get_huxclk_rate(scu);
25727881d20Sryan_chen 	u32 div_reg = readl(&scu->uart_24m_ref_huxclk);
25827881d20Sryan_chen 
25927881d20Sryan_chen 	unsigned int mult, div;
26027881d20Sryan_chen 
26127881d20Sryan_chen 	u32 n = (div_reg >> 8) & 0x3ff;
26227881d20Sryan_chen 	u32 r = div_reg & 0xff;
26327881d20Sryan_chen 
26427881d20Sryan_chen 	mult = r;
26527881d20Sryan_chen 	div = (n * 4);
26627881d20Sryan_chen 	return (clk_in * mult)/div;
26727881d20Sryan_chen }
26827881d20Sryan_chen 
26927881d20Sryan_chen static ulong ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx)
27027881d20Sryan_chen {
27127881d20Sryan_chen 	u32 uart_sel = readl(&scu->clk_sel4);
27227881d20Sryan_chen 	u32 uart_sel5 = readl(&scu->clk_sel5);
27327881d20Sryan_chen 	ulong uart_clk = 0;
27427881d20Sryan_chen 
27527881d20Sryan_chen 	switch(uart_idx) {
27627881d20Sryan_chen 		case 1:
27727881d20Sryan_chen 		case 2:
27827881d20Sryan_chen 		case 3:
27927881d20Sryan_chen 		case 4:
28027881d20Sryan_chen 		case 6:
28127881d20Sryan_chen 			if(uart_sel & BIT(uart_idx - 1))
28227881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
283550e691bSryan_chen 			else
28427881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
28527881d20Sryan_chen 			break;
28627881d20Sryan_chen 		case 5: //24mhz is come form usb phy 48Mhz
28727881d20Sryan_chen 			{
28827881d20Sryan_chen 			u8 uart5_clk_sel = 0;
28927881d20Sryan_chen 			//high bit
29027881d20Sryan_chen 			if (readl(&scu->misc_ctrl1) & BIT(12))
29127881d20Sryan_chen 				uart5_clk_sel = 0x2;
29227881d20Sryan_chen 			else
29327881d20Sryan_chen 				uart5_clk_sel = 0x0;
294550e691bSryan_chen 
29527881d20Sryan_chen 			if (readl(&scu->clk_sel2) & BIT(14))
29627881d20Sryan_chen 				uart5_clk_sel |= 0x1;
297550e691bSryan_chen 
29827881d20Sryan_chen 			switch(uart5_clk_sel) {
29927881d20Sryan_chen 				case 0:
30027881d20Sryan_chen 					uart_clk = 24000000;
30127881d20Sryan_chen 					break;
30227881d20Sryan_chen 				case 1:
30327881d20Sryan_chen 					uart_clk = 0;
30427881d20Sryan_chen 					break;
30527881d20Sryan_chen 				case 2:
30627881d20Sryan_chen 					uart_clk = 24000000/13;
30727881d20Sryan_chen 					break;
30827881d20Sryan_chen 				case 3:
30927881d20Sryan_chen 					uart_clk = 192000000/13;
31027881d20Sryan_chen 					break;
31127881d20Sryan_chen 			}
31227881d20Sryan_chen 			}
31327881d20Sryan_chen 			break;
31427881d20Sryan_chen 		case 7:
31527881d20Sryan_chen 		case 8:
31627881d20Sryan_chen 		case 9:
31727881d20Sryan_chen 		case 10:
31827881d20Sryan_chen 		case 11:
31927881d20Sryan_chen 		case 12:
32027881d20Sryan_chen 		case 13:
32127881d20Sryan_chen 			if(uart_sel5 & BIT(uart_idx - 1))
32227881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ;
32327881d20Sryan_chen 			else
32427881d20Sryan_chen 				uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ;
32527881d20Sryan_chen 			break;
32627881d20Sryan_chen 	}
32727881d20Sryan_chen 
32827881d20Sryan_chen 	return uart_clk;
329550e691bSryan_chen }
330550e691bSryan_chen 
331550e691bSryan_chen struct aspeed_clock_config {
332550e691bSryan_chen 	ulong input_rate;
333550e691bSryan_chen 	ulong rate;
33439283ea7Sryan_chen 	struct ast2600_div_config cfg;
335550e691bSryan_chen };
336550e691bSryan_chen 
337550e691bSryan_chen static const struct aspeed_clock_config aspeed_clock_config_defaults[] = {
3381cd71a14SDylan Hung 	{ 25000000, 400000000, { .num = 95, .denum = 2, .post_div = 1 } },
339550e691bSryan_chen };
340550e691bSryan_chen 
341550e691bSryan_chen static bool aspeed_get_clock_config_default(ulong input_rate,
342550e691bSryan_chen 					     ulong requested_rate,
34339283ea7Sryan_chen 					     struct ast2600_div_config *cfg)
344550e691bSryan_chen {
345550e691bSryan_chen 	int i;
346550e691bSryan_chen 
347550e691bSryan_chen 	for (i = 0; i < ARRAY_SIZE(aspeed_clock_config_defaults); i++) {
348550e691bSryan_chen 		const struct aspeed_clock_config *default_cfg =
349550e691bSryan_chen 			&aspeed_clock_config_defaults[i];
350550e691bSryan_chen 		if (default_cfg->input_rate == input_rate &&
351550e691bSryan_chen 		    default_cfg->rate == requested_rate) {
352550e691bSryan_chen 			*cfg = default_cfg->cfg;
353550e691bSryan_chen 			return true;
354550e691bSryan_chen 		}
355550e691bSryan_chen 	}
356550e691bSryan_chen 
357550e691bSryan_chen 	return false;
358550e691bSryan_chen }
359550e691bSryan_chen 
360550e691bSryan_chen /*
361550e691bSryan_chen  * @input_rate - the rate of input clock in Hz
362550e691bSryan_chen  * @requested_rate - desired output rate in Hz
363550e691bSryan_chen  * @div - this is an IN/OUT parameter, at input all fields of the config
364550e691bSryan_chen  * need to be set to their maximum allowed values.
365550e691bSryan_chen  * The result (the best config we could find), would also be returned
366550e691bSryan_chen  * in this structure.
367550e691bSryan_chen  *
368550e691bSryan_chen  * @return The clock rate, when the resulting div_config is used.
369550e691bSryan_chen  */
370550e691bSryan_chen static ulong aspeed_calc_clock_config(ulong input_rate, ulong requested_rate,
37139283ea7Sryan_chen 				       struct ast2600_div_config *cfg)
372550e691bSryan_chen {
373550e691bSryan_chen 	/*
374550e691bSryan_chen 	 * The assumption is that kHz precision is good enough and
375550e691bSryan_chen 	 * also enough to avoid overflow when multiplying.
376550e691bSryan_chen 	 */
377550e691bSryan_chen 	const ulong input_rate_khz = input_rate / 1000;
378550e691bSryan_chen 	const ulong rate_khz = requested_rate / 1000;
37939283ea7Sryan_chen 	const struct ast2600_div_config max_vals = *cfg;
38039283ea7Sryan_chen 	struct ast2600_div_config it = { 0, 0, 0 };
381550e691bSryan_chen 	ulong delta = rate_khz;
382550e691bSryan_chen 	ulong new_rate_khz = 0;
383550e691bSryan_chen 
384550e691bSryan_chen 	/*
385550e691bSryan_chen 	 * Look for a well known frequency first.
386550e691bSryan_chen 	 */
387550e691bSryan_chen 	if (aspeed_get_clock_config_default(input_rate, requested_rate, cfg))
388550e691bSryan_chen 		return requested_rate;
389550e691bSryan_chen 
390550e691bSryan_chen 	for (; it.denum <= max_vals.denum; ++it.denum) {
391550e691bSryan_chen 		for (it.post_div = 0; it.post_div <= max_vals.post_div;
392550e691bSryan_chen 		     ++it.post_div) {
393550e691bSryan_chen 			it.num = (rate_khz * (it.post_div + 1) / input_rate_khz)
394550e691bSryan_chen 			    * (it.denum + 1);
395550e691bSryan_chen 			if (it.num > max_vals.num)
396550e691bSryan_chen 				continue;
397550e691bSryan_chen 
398550e691bSryan_chen 			new_rate_khz = (input_rate_khz
399550e691bSryan_chen 					* ((it.num + 1) / (it.denum + 1)))
400550e691bSryan_chen 			    / (it.post_div + 1);
401550e691bSryan_chen 
402550e691bSryan_chen 			/* Keep the rate below requested one. */
403550e691bSryan_chen 			if (new_rate_khz > rate_khz)
404550e691bSryan_chen 				continue;
405550e691bSryan_chen 
406550e691bSryan_chen 			if (new_rate_khz - rate_khz < delta) {
407550e691bSryan_chen 				delta = new_rate_khz - rate_khz;
408550e691bSryan_chen 				*cfg = it;
409550e691bSryan_chen 				if (delta == 0)
410550e691bSryan_chen 					return new_rate_khz * 1000;
411550e691bSryan_chen 			}
412550e691bSryan_chen 		}
413550e691bSryan_chen 	}
414550e691bSryan_chen 
415550e691bSryan_chen 	return new_rate_khz * 1000;
416550e691bSryan_chen }
417550e691bSryan_chen 
41839283ea7Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_clk_priv *priv, ulong rate)
419550e691bSryan_chen {
420d6e349c7Sryan_chen 	u32 clkin = AST2600_CLK_IN;
421550e691bSryan_chen 	u32 mpll_reg;
42239283ea7Sryan_chen 	struct ast2600_div_config div_cfg = {
423550e691bSryan_chen 		.num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT),
424550e691bSryan_chen 		.denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT),
425550e691bSryan_chen 		.post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT),
426550e691bSryan_chen 	};
427550e691bSryan_chen 
428550e691bSryan_chen 	aspeed_calc_clock_config(clkin, rate, &div_cfg);
429550e691bSryan_chen 
430f0d895afSryan_chen 	mpll_reg = readl(&priv->scu->m_pll_param);
431550e691bSryan_chen 	mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK
432550e691bSryan_chen 		      | SCU_MPLL_DENUM_MASK);
433550e691bSryan_chen 	mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT)
434550e691bSryan_chen 	    | (div_cfg.num << SCU_MPLL_NUM_SHIFT)
435550e691bSryan_chen 	    | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT);
436550e691bSryan_chen 
437f0d895afSryan_chen 	writel(mpll_reg, &priv->scu->m_pll_param);
438550e691bSryan_chen 
43939283ea7Sryan_chen 	return ast2600_get_mpll_rate(priv->scu);
440550e691bSryan_chen }
441550e691bSryan_chen 
44286f91560Sryan_chen #define SCU_CLKSTOP_MAC1		(20)
44386f91560Sryan_chen #define SCU_CLKSTOP_MAC2		(21)
44486f91560Sryan_chen #define SCU_CLKSTOP_MAC3		(20)
44586f91560Sryan_chen #define SCU_CLKSTOP_MAC4		(21)
44686f91560Sryan_chen 
44700d2d4a5Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index)
448550e691bSryan_chen {
44900d2d4a5Sryan_chen 	u32 reset_bit;
45000d2d4a5Sryan_chen 	u32 clkstop_bit;
45100d2d4a5Sryan_chen 
45200d2d4a5Sryan_chen 
45300d2d4a5Sryan_chen 	switch (index) {
45400d2d4a5Sryan_chen 	case 1:
45500d2d4a5Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC1);
45686f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC1);
4574f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
45886f91560Sryan_chen 		udelay(100);
4594f22e838Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
46086f91560Sryan_chen 		mdelay(10);
4614f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
46286f91560Sryan_chen 
46300d2d4a5Sryan_chen 		break;
46400d2d4a5Sryan_chen 	case 2:
46500d2d4a5Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC2);
46686f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC2);
4674f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl1);
46886f91560Sryan_chen 		udelay(100);
4694f22e838Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl1);
47086f91560Sryan_chen 		mdelay(10);
4714f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl1);
47200d2d4a5Sryan_chen 		break;
47300d2d4a5Sryan_chen 	case 3:
47486f91560Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC3 - 32);
47586f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC3);
4764f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
47786f91560Sryan_chen 		udelay(100);
4784f22e838Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
47986f91560Sryan_chen 		mdelay(10);
4804f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
48100d2d4a5Sryan_chen 		break;
48200d2d4a5Sryan_chen 	case 4:
48386f91560Sryan_chen 		reset_bit = BIT(ASPEED_RESET_MAC4 - 32);
48486f91560Sryan_chen 		clkstop_bit = BIT(SCU_CLKSTOP_MAC4);
4854f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_ctrl2);
48686f91560Sryan_chen 		udelay(100);
4874f22e838Sryan_chen 		writel(clkstop_bit, &scu->clk_stop_clr_ctrl2);
48886f91560Sryan_chen 		mdelay(10);
4894f22e838Sryan_chen 		writel(reset_bit, &scu->sysreset_clr_ctrl2);
49000d2d4a5Sryan_chen 		break;
49100d2d4a5Sryan_chen 	default:
49200d2d4a5Sryan_chen 		return -EINVAL;
49300d2d4a5Sryan_chen 	}
49400d2d4a5Sryan_chen 
49539283ea7Sryan_chen 	return 0;
496550e691bSryan_chen }
497550e691bSryan_chen 
498f0d895afSryan_chen static u32 ast2600_hpll_pclk_div_table[] = {
499f0d895afSryan_chen 	4, 8, 12, 16, 20, 24, 28, 32,
500f0d895afSryan_chen };
501d6e349c7Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk)
502d6e349c7Sryan_chen {
503f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
50427881d20Sryan_chen 	ulong rate = 0;
505d6e349c7Sryan_chen 
506d6e349c7Sryan_chen 	switch (clk->id) {
507d1e64dd1Sryan_chen 	//HPLL
508d1e64dd1Sryan_chen 	case ASPEED_CLK_HPLL:
50939283ea7Sryan_chen 		rate = ast2600_get_hpll_rate(priv->scu);
51039283ea7Sryan_chen 		printf("hpll %ld \n", rate);
511d6e349c7Sryan_chen 		break;
512d1e64dd1Sryan_chen 	//HCLK
513d1e64dd1Sryan_chen 	case ASPEED_CLK_AHB:
51427881d20Sryan_chen 		rate = ast2600_get_hclk(priv->scu);
51500d2d4a5Sryan_chen 		printf("hclk %ld \n", rate);
516d1e64dd1Sryan_chen 		break;
517f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
51839283ea7Sryan_chen 		rate = ast2600_get_mpll_rate(priv->scu);
519d6e349c7Sryan_chen 		break;
52039283ea7Sryan_chen 	//pclk
52139283ea7Sryan_chen 	case ASPEED_CLK_APB:
522d6e349c7Sryan_chen 		{
523f0d895afSryan_chen 			u32 clk_sel1 = readl(&priv->scu->clk_sel1);
524f0d895afSryan_chen 			u32 apb_div = ast2600_hpll_pclk_div_table[((clk_sel1 >> 23) & 0x7)];
52539283ea7Sryan_chen 			rate = ast2600_get_hpll_rate(priv->scu);
526d6e349c7Sryan_chen 			rate = rate / apb_div;
527d6e349c7Sryan_chen 		}
528d6e349c7Sryan_chen 		break;
52927881d20Sryan_chen 	case ASPEED_CLK_GATE_UART1CLK:
53027881d20Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 1);
531d6e349c7Sryan_chen 		break;
53227881d20Sryan_chen 	case ASPEED_CLK_GATE_UART2CLK:
53327881d20Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 2);
534d6e349c7Sryan_chen 		break;
53527881d20Sryan_chen 	case ASPEED_CLK_GATE_UART3CLK:
53627881d20Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 3);
537d6e349c7Sryan_chen 		break;
53827881d20Sryan_chen 	case ASPEED_CLK_GATE_UART4CLK:
53927881d20Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 4);
540d6e349c7Sryan_chen 		break;
541d1e64dd1Sryan_chen 	case ASPEED_CLK_GATE_UART5CLK:
54227881d20Sryan_chen 		rate = ast2600_get_uart_clk_rate(priv->scu, 5);
543d6e349c7Sryan_chen 		break;
544d6e349c7Sryan_chen 	default:
545d6e349c7Sryan_chen 		return -ENOENT;
546d6e349c7Sryan_chen 	}
547d6e349c7Sryan_chen 
548d6e349c7Sryan_chen 	return rate;
549d6e349c7Sryan_chen }
550d6e349c7Sryan_chen 
551d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate)
552550e691bSryan_chen {
553f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
554550e691bSryan_chen 
555550e691bSryan_chen 	ulong new_rate;
556550e691bSryan_chen 	switch (clk->id) {
557f0d895afSryan_chen 	case ASPEED_CLK_MPLL:
55839283ea7Sryan_chen 		new_rate = ast2600_configure_ddr(priv, rate);
559550e691bSryan_chen 		break;
560550e691bSryan_chen 	default:
561550e691bSryan_chen 		return -ENOENT;
562550e691bSryan_chen 	}
563550e691bSryan_chen 
564550e691bSryan_chen 	return new_rate;
565550e691bSryan_chen }
566550e691bSryan_chen 
567d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk)
568550e691bSryan_chen {
569f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(clk->dev);
570550e691bSryan_chen 
571550e691bSryan_chen 	switch (clk->id) {
57286f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC1CLK:
57386f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 1);
574550e691bSryan_chen 			break;
57586f91560Sryan_chen 		case ASPEED_CLK_GATE_MAC2CLK:
57686f91560Sryan_chen 			ast2600_configure_mac(priv->scu, 2);
577550e691bSryan_chen 			break;
578*77843939Sryan_chen 		case ASPEED_CLK_GATE_MAC3CLK:
579*77843939Sryan_chen 			ast2600_configure_mac(priv->scu, 3);
580*77843939Sryan_chen 			break;
581*77843939Sryan_chen 		case ASPEED_CLK_GATE_MAC4CLK:
582*77843939Sryan_chen 			ast2600_configure_mac(priv->scu, 4);
583*77843939Sryan_chen 			break;
584550e691bSryan_chen 		default:
585550e691bSryan_chen 			return -ENOENT;
586*77843939Sryan_chen 			break;
587550e691bSryan_chen 	}
588550e691bSryan_chen 
589550e691bSryan_chen 	return 0;
590550e691bSryan_chen }
591550e691bSryan_chen 
592550e691bSryan_chen struct clk_ops aspeed_clk_ops = {
593d6e349c7Sryan_chen 	.get_rate = ast2600_clk_get_rate,
594d6e349c7Sryan_chen 	.set_rate = ast2600_clk_set_rate,
595d6e349c7Sryan_chen 	.enable = ast2600_clk_enable,
596550e691bSryan_chen };
597550e691bSryan_chen 
598d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev)
599550e691bSryan_chen {
600f0d895afSryan_chen 	struct ast2600_clk_priv *priv = dev_get_priv(dev);
601550e691bSryan_chen 
602f0d895afSryan_chen 	priv->scu = devfdt_get_addr_ptr(dev);
603f0d895afSryan_chen 	if (IS_ERR(priv->scu))
604f0d895afSryan_chen 		return PTR_ERR(priv->scu);
605550e691bSryan_chen 
606550e691bSryan_chen 	return 0;
607550e691bSryan_chen }
608550e691bSryan_chen 
609d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev)
610550e691bSryan_chen {
611550e691bSryan_chen 	int ret;
612550e691bSryan_chen 
613550e691bSryan_chen 	/* The reset driver does not have a device node, so bind it here */
614550e691bSryan_chen 	ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
615550e691bSryan_chen 	if (ret)
616550e691bSryan_chen 		debug("Warning: No reset driver: ret=%d\n", ret);
617550e691bSryan_chen 
618550e691bSryan_chen 	return 0;
619550e691bSryan_chen }
620550e691bSryan_chen 
621d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = {
622d6e349c7Sryan_chen 	{ .compatible = "aspeed,ast2600-scu", },
623550e691bSryan_chen 	{ }
624550e691bSryan_chen };
625550e691bSryan_chen 
626aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = {
627aa36597fSDylan Hung 	.name		= "aspeed_scu",
628550e691bSryan_chen 	.id		= UCLASS_CLK,
629d6e349c7Sryan_chen 	.of_match	= ast2600_clk_ids,
630f0d895afSryan_chen 	.priv_auto_alloc_size = sizeof(struct ast2600_clk_priv),
631550e691bSryan_chen 	.ops		= &aspeed_clk_ops,
632d6e349c7Sryan_chen 	.bind		= ast2600_clk_bind,
633d6e349c7Sryan_chen 	.probe		= ast2600_clk_probe,
634550e691bSryan_chen };
635