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 99f9aa0ee1Sryan_chen extern u32 ast2600_get_dpll_rate(struct ast2600_scu *scu) 100f9aa0ee1Sryan_chen { 101f9aa0ee1Sryan_chen u32 clk_in = AST2600_CLK_IN; 102f9aa0ee1Sryan_chen u32 dpll_reg = readl(&scu->d_pll_param); 103f9aa0ee1Sryan_chen unsigned int mult, div = 1; 104f9aa0ee1Sryan_chen 105f9aa0ee1Sryan_chen if (dpll_reg & BIT(24)) { 106f9aa0ee1Sryan_chen /* Pass through mode */ 107f9aa0ee1Sryan_chen mult = div = 1; 108f9aa0ee1Sryan_chen } else { 109f9aa0ee1Sryan_chen /* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1)*/ 110f9aa0ee1Sryan_chen u32 m = dpll_reg & 0x1fff; 111f9aa0ee1Sryan_chen u32 n = (dpll_reg >> 13) & 0x3f; 112f9aa0ee1Sryan_chen u32 p = (dpll_reg >> 19) & 0x7; 113f9aa0ee1Sryan_chen mult = ((m + 1) / (n + 1)); 114f9aa0ee1Sryan_chen div = (p + 1); 115f9aa0ee1Sryan_chen } 116f9aa0ee1Sryan_chen return (clk_in * mult)/div; 117f9aa0ee1Sryan_chen } 118f9aa0ee1Sryan_chen 1194f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu) 120550e691bSryan_chen { 12139283ea7Sryan_chen u32 clk_in = AST2600_CLK_IN; 12239283ea7Sryan_chen u32 apll_reg = readl(&scu->a_pll_param); 12339283ea7Sryan_chen unsigned int mult, div = 1; 124d6e349c7Sryan_chen 12539283ea7Sryan_chen if (apll_reg & BIT(20)) { 126d6e349c7Sryan_chen /* Pass through mode */ 127d6e349c7Sryan_chen mult = div = 1; 128d6e349c7Sryan_chen } else { 129d6e349c7Sryan_chen /* F = 25Mhz * (2-OD) * [(M + 2) / (n + 1)] */ 13039283ea7Sryan_chen u32 m = (apll_reg >> 5) & 0x3f; 13139283ea7Sryan_chen u32 od = (apll_reg >> 4) & 0x1; 13239283ea7Sryan_chen u32 n = apll_reg & 0xf; 133d6e349c7Sryan_chen 13439283ea7Sryan_chen mult = (2 - od) * ((m + 2) / (n + 1)); 135d6e349c7Sryan_chen } 136d6e349c7Sryan_chen return (clk_in * mult)/div; 137d6e349c7Sryan_chen } 138d6e349c7Sryan_chen 1394f22e838Sryan_chen extern u32 ast2600_get_epll_rate(struct ast2600_scu *scu) 14039283ea7Sryan_chen { 14139283ea7Sryan_chen u32 clk_in = AST2600_CLK_IN; 14239283ea7Sryan_chen u32 epll_reg = readl(&scu->e_pll_param); 14339283ea7Sryan_chen unsigned int mult, div = 1; 14439283ea7Sryan_chen 14539283ea7Sryan_chen if (epll_reg & BIT(24)) { 14639283ea7Sryan_chen /* Pass through mode */ 14739283ea7Sryan_chen mult = div = 1; 14839283ea7Sryan_chen } else { 14939283ea7Sryan_chen /* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1)*/ 15039283ea7Sryan_chen u32 m = epll_reg & 0x1fff; 15139283ea7Sryan_chen u32 n = (epll_reg >> 13) & 0x3f; 15239283ea7Sryan_chen u32 p = (epll_reg >> 19) & 0x7; 15339283ea7Sryan_chen 15439283ea7Sryan_chen mult = ((m + 1) / (n + 1)); 15539283ea7Sryan_chen div = (p + 1); 15639283ea7Sryan_chen } 15739283ea7Sryan_chen return (clk_in * mult)/div; 15839283ea7Sryan_chen } 15939283ea7Sryan_chen 160*d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = { 161*d812df15Sryan_chen 2, 2, 3, 5, 162*d812df15Sryan_chen }; 163*d812df15Sryan_chen 164*d812df15Sryan_chen static u32 ast2600_a1_axi_ahb_div_table[] = { 165*d812df15Sryan_chen 4, 6, 2, 4, 166*d812df15Sryan_chen }; 167*d812df15Sryan_chen 168*d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu) 169*d812df15Sryan_chen { 170*d812df15Sryan_chen u32 hw_rev = readl(&scu->chip_id0); 171*d812df15Sryan_chen u32 hwstrap1 = readl(&scu->hwstrap1); 172*d812df15Sryan_chen u32 axi_div = 1; 173*d812df15Sryan_chen u32 ahb_div = 0; 174*d812df15Sryan_chen u32 rate = 0; 175*d812df15Sryan_chen 176*d812df15Sryan_chen if((hwstrap1 >> 16) & 0x1) 177*d812df15Sryan_chen axi_div = 1; 178*d812df15Sryan_chen else 179*d812df15Sryan_chen axi_div = 2; 180*d812df15Sryan_chen 181*d812df15Sryan_chen if (hw_rev & BIT(16)) 182*d812df15Sryan_chen ahb_div = ast2600_a1_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3]; 183*d812df15Sryan_chen else 184*d812df15Sryan_chen ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3]; 185*d812df15Sryan_chen 186*d812df15Sryan_chen rate = ast2600_get_hpll_rate(scu); 187*d812df15Sryan_chen rate = rate / axi_div / ahb_div; 188*d812df15Sryan_chen 189*d812df15Sryan_chen return rate; 190*d812df15Sryan_chen } 191*d812df15Sryan_chen 192d6e349c7Sryan_chen 19327881d20Sryan_chen static u32 ast2600_get_uxclk_rate(struct ast2600_scu *scu) 194d6e349c7Sryan_chen { 19527881d20Sryan_chen u32 clk_in = 0; 19627881d20Sryan_chen u32 uxclk_sel = readl(&scu->clk_sel4); 197550e691bSryan_chen 19827881d20Sryan_chen uxclk_sel &= 0x3; 19927881d20Sryan_chen switch(uxclk_sel) { 20027881d20Sryan_chen case 0: 20127881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 20227881d20Sryan_chen break; 20327881d20Sryan_chen case 1: 20427881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 20527881d20Sryan_chen break; 20627881d20Sryan_chen case 2: 20727881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 20827881d20Sryan_chen break; 20927881d20Sryan_chen case 3: 21027881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 21127881d20Sryan_chen break; 21227881d20Sryan_chen } 213d6e349c7Sryan_chen 21427881d20Sryan_chen return clk_in; 21527881d20Sryan_chen } 21627881d20Sryan_chen 21727881d20Sryan_chen static u32 ast2600_get_huxclk_rate(struct ast2600_scu *scu) 21827881d20Sryan_chen { 21927881d20Sryan_chen u32 clk_in = 0; 22027881d20Sryan_chen u32 huclk_sel = readl(&scu->clk_sel4); 22127881d20Sryan_chen 22227881d20Sryan_chen huclk_sel = ((huclk_sel >> 3) & 0x3); 22327881d20Sryan_chen switch(huclk_sel) { 22427881d20Sryan_chen case 0: 22527881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 22627881d20Sryan_chen break; 22727881d20Sryan_chen case 1: 22827881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 22927881d20Sryan_chen break; 23027881d20Sryan_chen case 2: 23127881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 23227881d20Sryan_chen break; 23327881d20Sryan_chen case 3: 23427881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 23527881d20Sryan_chen break; 23627881d20Sryan_chen } 23727881d20Sryan_chen 23827881d20Sryan_chen return clk_in; 23927881d20Sryan_chen } 24027881d20Sryan_chen 24127881d20Sryan_chen static u32 ast2600_get_uart_from_uxclk_rate(struct ast2600_scu *scu) 24227881d20Sryan_chen { 24327881d20Sryan_chen u32 clk_in = ast2600_get_uxclk_rate(scu); 24427881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_uxclk); 24527881d20Sryan_chen unsigned int mult, div; 24627881d20Sryan_chen 24727881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 24827881d20Sryan_chen u32 r = div_reg & 0xff; 24927881d20Sryan_chen 25027881d20Sryan_chen mult = r; 25127881d20Sryan_chen div = (n * 4); 25227881d20Sryan_chen return (clk_in * mult)/div; 25327881d20Sryan_chen } 25427881d20Sryan_chen 25527881d20Sryan_chen static u32 ast2600_get_uart_from_huxclk_rate(struct ast2600_scu *scu) 25627881d20Sryan_chen { 25727881d20Sryan_chen u32 clk_in = ast2600_get_huxclk_rate(scu); 25827881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_huxclk); 25927881d20Sryan_chen 26027881d20Sryan_chen unsigned int mult, div; 26127881d20Sryan_chen 26227881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 26327881d20Sryan_chen u32 r = div_reg & 0xff; 26427881d20Sryan_chen 26527881d20Sryan_chen mult = r; 26627881d20Sryan_chen div = (n * 4); 26727881d20Sryan_chen return (clk_in * mult)/div; 26827881d20Sryan_chen } 26927881d20Sryan_chen 27027881d20Sryan_chen static ulong ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx) 27127881d20Sryan_chen { 27227881d20Sryan_chen u32 uart_sel = readl(&scu->clk_sel4); 27327881d20Sryan_chen u32 uart_sel5 = readl(&scu->clk_sel5); 27427881d20Sryan_chen ulong uart_clk = 0; 27527881d20Sryan_chen 27627881d20Sryan_chen switch(uart_idx) { 27727881d20Sryan_chen case 1: 27827881d20Sryan_chen case 2: 27927881d20Sryan_chen case 3: 28027881d20Sryan_chen case 4: 28127881d20Sryan_chen case 6: 28227881d20Sryan_chen if(uart_sel & BIT(uart_idx - 1)) 28327881d20Sryan_chen uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ; 284550e691bSryan_chen else 28527881d20Sryan_chen uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ; 28627881d20Sryan_chen break; 28727881d20Sryan_chen case 5: //24mhz is come form usb phy 48Mhz 28827881d20Sryan_chen { 28927881d20Sryan_chen u8 uart5_clk_sel = 0; 29027881d20Sryan_chen //high bit 29127881d20Sryan_chen if (readl(&scu->misc_ctrl1) & BIT(12)) 29227881d20Sryan_chen uart5_clk_sel = 0x2; 29327881d20Sryan_chen else 29427881d20Sryan_chen uart5_clk_sel = 0x0; 295550e691bSryan_chen 29627881d20Sryan_chen if (readl(&scu->clk_sel2) & BIT(14)) 29727881d20Sryan_chen uart5_clk_sel |= 0x1; 298550e691bSryan_chen 29927881d20Sryan_chen switch(uart5_clk_sel) { 30027881d20Sryan_chen case 0: 30127881d20Sryan_chen uart_clk = 24000000; 30227881d20Sryan_chen break; 30327881d20Sryan_chen case 1: 30427881d20Sryan_chen uart_clk = 0; 30527881d20Sryan_chen break; 30627881d20Sryan_chen case 2: 30727881d20Sryan_chen uart_clk = 24000000/13; 30827881d20Sryan_chen break; 30927881d20Sryan_chen case 3: 31027881d20Sryan_chen uart_clk = 192000000/13; 31127881d20Sryan_chen break; 31227881d20Sryan_chen } 31327881d20Sryan_chen } 31427881d20Sryan_chen break; 31527881d20Sryan_chen case 7: 31627881d20Sryan_chen case 8: 31727881d20Sryan_chen case 9: 31827881d20Sryan_chen case 10: 31927881d20Sryan_chen case 11: 32027881d20Sryan_chen case 12: 32127881d20Sryan_chen case 13: 32227881d20Sryan_chen if(uart_sel5 & BIT(uart_idx - 1)) 32327881d20Sryan_chen uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ; 32427881d20Sryan_chen else 32527881d20Sryan_chen uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ; 32627881d20Sryan_chen break; 32727881d20Sryan_chen } 32827881d20Sryan_chen 32927881d20Sryan_chen return uart_clk; 330550e691bSryan_chen } 331550e691bSryan_chen 332feb42054Sryan_chen static u32 ast2600_hpll_pclk_div_table[] = { 333feb42054Sryan_chen 4, 8, 12, 16, 20, 24, 28, 32, 334feb42054Sryan_chen }; 335feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk) 336feb42054Sryan_chen { 337feb42054Sryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 338feb42054Sryan_chen ulong rate = 0; 339feb42054Sryan_chen 340feb42054Sryan_chen switch (clk->id) { 341feb42054Sryan_chen case ASPEED_CLK_HPLL: 342feb42054Sryan_chen rate = ast2600_get_hpll_rate(priv->scu); 343feb42054Sryan_chen break; 344*d812df15Sryan_chen case ASPEED_CLK_MPLL: 345*d812df15Sryan_chen rate = ast2600_get_mpll_rate(priv->scu); 346*d812df15Sryan_chen break; 347feb42054Sryan_chen //HCLK 348feb42054Sryan_chen case ASPEED_CLK_AHB: 349feb42054Sryan_chen rate = ast2600_get_hclk(priv->scu); 350feb42054Sryan_chen break; 351feb42054Sryan_chen //pclk 352feb42054Sryan_chen case ASPEED_CLK_APB: 353feb42054Sryan_chen { 354feb42054Sryan_chen u32 clk_sel1 = readl(&priv->scu->clk_sel1); 355feb42054Sryan_chen u32 apb_div = ast2600_hpll_pclk_div_table[((clk_sel1 >> 23) & 0x7)]; 356feb42054Sryan_chen rate = ast2600_get_hpll_rate(priv->scu); 357feb42054Sryan_chen rate = rate / apb_div; 358feb42054Sryan_chen } 359feb42054Sryan_chen break; 360feb42054Sryan_chen case ASPEED_CLK_GATE_UART1CLK: 361feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 1); 362feb42054Sryan_chen break; 363feb42054Sryan_chen case ASPEED_CLK_GATE_UART2CLK: 364feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 2); 365feb42054Sryan_chen break; 366feb42054Sryan_chen case ASPEED_CLK_GATE_UART3CLK: 367feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 3); 368feb42054Sryan_chen break; 369feb42054Sryan_chen case ASPEED_CLK_GATE_UART4CLK: 370feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 4); 371feb42054Sryan_chen break; 372feb42054Sryan_chen case ASPEED_CLK_GATE_UART5CLK: 373feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 5); 374feb42054Sryan_chen break; 375feb42054Sryan_chen default: 376*d812df15Sryan_chen pr_debug("can't get clk rate \n"); 377feb42054Sryan_chen return -ENOENT; 378*d812df15Sryan_chen break; 379feb42054Sryan_chen } 380feb42054Sryan_chen 381feb42054Sryan_chen return rate; 382feb42054Sryan_chen } 383feb42054Sryan_chen 384550e691bSryan_chen struct aspeed_clock_config { 385550e691bSryan_chen ulong input_rate; 386550e691bSryan_chen ulong rate; 38739283ea7Sryan_chen struct ast2600_div_config cfg; 388550e691bSryan_chen }; 389550e691bSryan_chen 390550e691bSryan_chen static const struct aspeed_clock_config aspeed_clock_config_defaults[] = { 3911cd71a14SDylan Hung { 25000000, 400000000, { .num = 95, .denum = 2, .post_div = 1 } }, 392550e691bSryan_chen }; 393550e691bSryan_chen 394550e691bSryan_chen static bool aspeed_get_clock_config_default(ulong input_rate, 395550e691bSryan_chen ulong requested_rate, 39639283ea7Sryan_chen struct ast2600_div_config *cfg) 397550e691bSryan_chen { 398550e691bSryan_chen int i; 399550e691bSryan_chen 400550e691bSryan_chen for (i = 0; i < ARRAY_SIZE(aspeed_clock_config_defaults); i++) { 401550e691bSryan_chen const struct aspeed_clock_config *default_cfg = 402550e691bSryan_chen &aspeed_clock_config_defaults[i]; 403550e691bSryan_chen if (default_cfg->input_rate == input_rate && 404550e691bSryan_chen default_cfg->rate == requested_rate) { 405550e691bSryan_chen *cfg = default_cfg->cfg; 406550e691bSryan_chen return true; 407550e691bSryan_chen } 408550e691bSryan_chen } 409550e691bSryan_chen 410550e691bSryan_chen return false; 411550e691bSryan_chen } 412550e691bSryan_chen 413550e691bSryan_chen /* 414550e691bSryan_chen * @input_rate - the rate of input clock in Hz 415550e691bSryan_chen * @requested_rate - desired output rate in Hz 416550e691bSryan_chen * @div - this is an IN/OUT parameter, at input all fields of the config 417550e691bSryan_chen * need to be set to their maximum allowed values. 418550e691bSryan_chen * The result (the best config we could find), would also be returned 419550e691bSryan_chen * in this structure. 420550e691bSryan_chen * 421550e691bSryan_chen * @return The clock rate, when the resulting div_config is used. 422550e691bSryan_chen */ 423550e691bSryan_chen static ulong aspeed_calc_clock_config(ulong input_rate, ulong requested_rate, 42439283ea7Sryan_chen struct ast2600_div_config *cfg) 425550e691bSryan_chen { 426550e691bSryan_chen /* 427550e691bSryan_chen * The assumption is that kHz precision is good enough and 428550e691bSryan_chen * also enough to avoid overflow when multiplying. 429550e691bSryan_chen */ 430550e691bSryan_chen const ulong input_rate_khz = input_rate / 1000; 431550e691bSryan_chen const ulong rate_khz = requested_rate / 1000; 43239283ea7Sryan_chen const struct ast2600_div_config max_vals = *cfg; 43339283ea7Sryan_chen struct ast2600_div_config it = { 0, 0, 0 }; 434550e691bSryan_chen ulong delta = rate_khz; 435550e691bSryan_chen ulong new_rate_khz = 0; 436550e691bSryan_chen 437550e691bSryan_chen /* 438550e691bSryan_chen * Look for a well known frequency first. 439550e691bSryan_chen */ 440550e691bSryan_chen if (aspeed_get_clock_config_default(input_rate, requested_rate, cfg)) 441550e691bSryan_chen return requested_rate; 442550e691bSryan_chen 443550e691bSryan_chen for (; it.denum <= max_vals.denum; ++it.denum) { 444550e691bSryan_chen for (it.post_div = 0; it.post_div <= max_vals.post_div; 445550e691bSryan_chen ++it.post_div) { 446550e691bSryan_chen it.num = (rate_khz * (it.post_div + 1) / input_rate_khz) 447550e691bSryan_chen * (it.denum + 1); 448550e691bSryan_chen if (it.num > max_vals.num) 449550e691bSryan_chen continue; 450550e691bSryan_chen 451550e691bSryan_chen new_rate_khz = (input_rate_khz 452550e691bSryan_chen * ((it.num + 1) / (it.denum + 1))) 453550e691bSryan_chen / (it.post_div + 1); 454550e691bSryan_chen 455550e691bSryan_chen /* Keep the rate below requested one. */ 456550e691bSryan_chen if (new_rate_khz > rate_khz) 457550e691bSryan_chen continue; 458550e691bSryan_chen 459550e691bSryan_chen if (new_rate_khz - rate_khz < delta) { 460550e691bSryan_chen delta = new_rate_khz - rate_khz; 461550e691bSryan_chen *cfg = it; 462550e691bSryan_chen if (delta == 0) 463550e691bSryan_chen return new_rate_khz * 1000; 464550e691bSryan_chen } 465550e691bSryan_chen } 466550e691bSryan_chen } 467550e691bSryan_chen 468550e691bSryan_chen return new_rate_khz * 1000; 469550e691bSryan_chen } 470550e691bSryan_chen 471feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate) 472550e691bSryan_chen { 473d6e349c7Sryan_chen u32 clkin = AST2600_CLK_IN; 474550e691bSryan_chen u32 mpll_reg; 47539283ea7Sryan_chen struct ast2600_div_config div_cfg = { 476550e691bSryan_chen .num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT), 477550e691bSryan_chen .denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT), 478550e691bSryan_chen .post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT), 479550e691bSryan_chen }; 480550e691bSryan_chen 481550e691bSryan_chen aspeed_calc_clock_config(clkin, rate, &div_cfg); 482550e691bSryan_chen 483feb42054Sryan_chen mpll_reg = readl(&scu->m_pll_param); 484550e691bSryan_chen mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK 485550e691bSryan_chen | SCU_MPLL_DENUM_MASK); 486550e691bSryan_chen mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT) 487550e691bSryan_chen | (div_cfg.num << SCU_MPLL_NUM_SHIFT) 488550e691bSryan_chen | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT); 489550e691bSryan_chen 490feb42054Sryan_chen writel(mpll_reg, &scu->m_pll_param); 491550e691bSryan_chen 492feb42054Sryan_chen return ast2600_get_mpll_rate(scu); 493d6e349c7Sryan_chen } 494d6e349c7Sryan_chen 495d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate) 496550e691bSryan_chen { 497f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 498550e691bSryan_chen 499550e691bSryan_chen ulong new_rate; 500550e691bSryan_chen switch (clk->id) { 501f0d895afSryan_chen case ASPEED_CLK_MPLL: 502feb42054Sryan_chen new_rate = ast2600_configure_ddr(priv->scu, rate); 503550e691bSryan_chen break; 504550e691bSryan_chen default: 505550e691bSryan_chen return -ENOENT; 506550e691bSryan_chen } 507550e691bSryan_chen 508550e691bSryan_chen return new_rate; 509550e691bSryan_chen } 510feb42054Sryan_chen 511f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1 (20) 512f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2 (21) 513f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3 (20) 514f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4 (21) 515f9aa0ee1Sryan_chen 516f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index) 517f9aa0ee1Sryan_chen { 518f9aa0ee1Sryan_chen u32 reset_bit; 519f9aa0ee1Sryan_chen u32 clkstop_bit; 520f9aa0ee1Sryan_chen 521f9aa0ee1Sryan_chen 522f9aa0ee1Sryan_chen switch (index) { 523f9aa0ee1Sryan_chen case 1: 524f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC1); 525f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC1); 526f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 527f9aa0ee1Sryan_chen udelay(100); 528f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 529f9aa0ee1Sryan_chen mdelay(10); 530f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 531f9aa0ee1Sryan_chen 532f9aa0ee1Sryan_chen break; 533f9aa0ee1Sryan_chen case 2: 534f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC2); 535f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC2); 536f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 537f9aa0ee1Sryan_chen udelay(100); 538f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 539f9aa0ee1Sryan_chen mdelay(10); 540f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 541f9aa0ee1Sryan_chen break; 542f9aa0ee1Sryan_chen case 3: 543f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC3 - 32); 544f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC3); 545f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 546f9aa0ee1Sryan_chen udelay(100); 547f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 548f9aa0ee1Sryan_chen mdelay(10); 549f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 550f9aa0ee1Sryan_chen break; 551f9aa0ee1Sryan_chen case 4: 552f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC4 - 32); 553f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC4); 554f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 555f9aa0ee1Sryan_chen udelay(100); 556f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 557f9aa0ee1Sryan_chen mdelay(10); 558f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 559f9aa0ee1Sryan_chen break; 560f9aa0ee1Sryan_chen default: 561f9aa0ee1Sryan_chen return -EINVAL; 562f9aa0ee1Sryan_chen } 563f9aa0ee1Sryan_chen 564f9aa0ee1Sryan_chen return 0; 565f9aa0ee1Sryan_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; 57877843939Sryan_chen case ASPEED_CLK_GATE_MAC3CLK: 57977843939Sryan_chen ast2600_configure_mac(priv->scu, 3); 58077843939Sryan_chen break; 58177843939Sryan_chen case ASPEED_CLK_GATE_MAC4CLK: 58277843939Sryan_chen ast2600_configure_mac(priv->scu, 4); 58377843939Sryan_chen break; 584550e691bSryan_chen default: 585f9aa0ee1Sryan_chen pr_debug("can't enable clk \n"); 586550e691bSryan_chen return -ENOENT; 58777843939Sryan_chen break; 588550e691bSryan_chen } 589550e691bSryan_chen 590550e691bSryan_chen return 0; 591550e691bSryan_chen } 592550e691bSryan_chen 593f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = { 594d6e349c7Sryan_chen .get_rate = ast2600_clk_get_rate, 595d6e349c7Sryan_chen .set_rate = ast2600_clk_set_rate, 596d6e349c7Sryan_chen .enable = ast2600_clk_enable, 597550e691bSryan_chen }; 598550e691bSryan_chen 599d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev) 600550e691bSryan_chen { 601f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(dev); 602550e691bSryan_chen 603f0d895afSryan_chen priv->scu = devfdt_get_addr_ptr(dev); 604f0d895afSryan_chen if (IS_ERR(priv->scu)) 605f0d895afSryan_chen return PTR_ERR(priv->scu); 606550e691bSryan_chen 607550e691bSryan_chen return 0; 608550e691bSryan_chen } 609550e691bSryan_chen 610d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev) 611550e691bSryan_chen { 612550e691bSryan_chen int ret; 613550e691bSryan_chen 614550e691bSryan_chen /* The reset driver does not have a device node, so bind it here */ 615550e691bSryan_chen ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev); 616550e691bSryan_chen if (ret) 617550e691bSryan_chen debug("Warning: No reset driver: ret=%d\n", ret); 618550e691bSryan_chen 619550e691bSryan_chen return 0; 620550e691bSryan_chen } 621550e691bSryan_chen 622d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = { 623d6e349c7Sryan_chen { .compatible = "aspeed,ast2600-scu", }, 624550e691bSryan_chen { } 625550e691bSryan_chen }; 626550e691bSryan_chen 627aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = { 628aa36597fSDylan Hung .name = "aspeed_scu", 629550e691bSryan_chen .id = UCLASS_CLK, 630d6e349c7Sryan_chen .of_match = ast2600_clk_ids, 631f0d895afSryan_chen .priv_auto_alloc_size = sizeof(struct ast2600_clk_priv), 632f9aa0ee1Sryan_chen .ops = &ast2600_clk_ops, 633d6e349c7Sryan_chen .bind = ast2600_clk_bind, 634d6e349c7Sryan_chen .probe = ast2600_clk_probe, 635550e691bSryan_chen }; 636