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 160d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = { 161d812df15Sryan_chen 2, 2, 3, 5, 162d812df15Sryan_chen }; 163d812df15Sryan_chen 164d812df15Sryan_chen static u32 ast2600_a1_axi_ahb_div_table[] = { 165d812df15Sryan_chen 4, 6, 2, 4, 166d812df15Sryan_chen }; 167d812df15Sryan_chen 168d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu) 169d812df15Sryan_chen { 170d812df15Sryan_chen u32 hw_rev = readl(&scu->chip_id0); 171d812df15Sryan_chen u32 hwstrap1 = readl(&scu->hwstrap1); 172d812df15Sryan_chen u32 axi_div = 1; 173d812df15Sryan_chen u32 ahb_div = 0; 174d812df15Sryan_chen u32 rate = 0; 175d812df15Sryan_chen 176d812df15Sryan_chen if((hwstrap1 >> 16) & 0x1) 177d812df15Sryan_chen axi_div = 1; 178d812df15Sryan_chen else 179d812df15Sryan_chen axi_div = 2; 180d812df15Sryan_chen 181d812df15Sryan_chen if (hw_rev & BIT(16)) 182d812df15Sryan_chen ahb_div = ast2600_a1_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3]; 183d812df15Sryan_chen else 184d812df15Sryan_chen ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3]; 185d812df15Sryan_chen 186d812df15Sryan_chen rate = ast2600_get_hpll_rate(scu); 187d812df15Sryan_chen 188*2717883aSryan_chen return (rate / axi_div / ahb_div); 189*2717883aSryan_chen } 190*2717883aSryan_chen 191*2717883aSryan_chen static u32 ast2600_hpll_pclk_div_table[] = { 192*2717883aSryan_chen 4, 8, 12, 16, 20, 24, 28, 32, 193*2717883aSryan_chen }; 194*2717883aSryan_chen 195*2717883aSryan_chen static u32 ast2600_get_pclk(struct ast2600_scu *scu) 196*2717883aSryan_chen { 197*2717883aSryan_chen u32 clk_sel1 = readl(&scu->clk_sel1); 198*2717883aSryan_chen u32 apb_div = ast2600_hpll_pclk_div_table[((clk_sel1 >> 23) & 0x7)]; 199*2717883aSryan_chen u32 rate = ast2600_get_hpll_rate(scu); 200*2717883aSryan_chen 201*2717883aSryan_chen return (rate / apb_div); 202d812df15Sryan_chen } 203d812df15Sryan_chen 204d6e349c7Sryan_chen 20527881d20Sryan_chen static u32 ast2600_get_uxclk_rate(struct ast2600_scu *scu) 206d6e349c7Sryan_chen { 20727881d20Sryan_chen u32 clk_in = 0; 20827881d20Sryan_chen u32 uxclk_sel = readl(&scu->clk_sel4); 209550e691bSryan_chen 21027881d20Sryan_chen uxclk_sel &= 0x3; 21127881d20Sryan_chen switch(uxclk_sel) { 21227881d20Sryan_chen case 0: 21327881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 21427881d20Sryan_chen break; 21527881d20Sryan_chen case 1: 21627881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 21727881d20Sryan_chen break; 21827881d20Sryan_chen case 2: 21927881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 22027881d20Sryan_chen break; 22127881d20Sryan_chen case 3: 22227881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 22327881d20Sryan_chen break; 22427881d20Sryan_chen } 225d6e349c7Sryan_chen 22627881d20Sryan_chen return clk_in; 22727881d20Sryan_chen } 22827881d20Sryan_chen 22927881d20Sryan_chen static u32 ast2600_get_huxclk_rate(struct ast2600_scu *scu) 23027881d20Sryan_chen { 23127881d20Sryan_chen u32 clk_in = 0; 23227881d20Sryan_chen u32 huclk_sel = readl(&scu->clk_sel4); 23327881d20Sryan_chen 23427881d20Sryan_chen huclk_sel = ((huclk_sel >> 3) & 0x3); 23527881d20Sryan_chen switch(huclk_sel) { 23627881d20Sryan_chen case 0: 23727881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 23827881d20Sryan_chen break; 23927881d20Sryan_chen case 1: 24027881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 24127881d20Sryan_chen break; 24227881d20Sryan_chen case 2: 24327881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 24427881d20Sryan_chen break; 24527881d20Sryan_chen case 3: 24627881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 24727881d20Sryan_chen break; 24827881d20Sryan_chen } 24927881d20Sryan_chen 25027881d20Sryan_chen return clk_in; 25127881d20Sryan_chen } 25227881d20Sryan_chen 25327881d20Sryan_chen static u32 ast2600_get_uart_from_uxclk_rate(struct ast2600_scu *scu) 25427881d20Sryan_chen { 25527881d20Sryan_chen u32 clk_in = ast2600_get_uxclk_rate(scu); 25627881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_uxclk); 25727881d20Sryan_chen unsigned int mult, div; 25827881d20Sryan_chen 25927881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 26027881d20Sryan_chen u32 r = div_reg & 0xff; 26127881d20Sryan_chen 26227881d20Sryan_chen mult = r; 26327881d20Sryan_chen div = (n * 4); 26427881d20Sryan_chen return (clk_in * mult)/div; 26527881d20Sryan_chen } 26627881d20Sryan_chen 26727881d20Sryan_chen static u32 ast2600_get_uart_from_huxclk_rate(struct ast2600_scu *scu) 26827881d20Sryan_chen { 26927881d20Sryan_chen u32 clk_in = ast2600_get_huxclk_rate(scu); 27027881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_huxclk); 27127881d20Sryan_chen 27227881d20Sryan_chen unsigned int mult, div; 27327881d20Sryan_chen 27427881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 27527881d20Sryan_chen u32 r = div_reg & 0xff; 27627881d20Sryan_chen 27727881d20Sryan_chen mult = r; 27827881d20Sryan_chen div = (n * 4); 27927881d20Sryan_chen return (clk_in * mult)/div; 28027881d20Sryan_chen } 28127881d20Sryan_chen 28227881d20Sryan_chen static ulong ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx) 28327881d20Sryan_chen { 28427881d20Sryan_chen u32 uart_sel = readl(&scu->clk_sel4); 28527881d20Sryan_chen u32 uart_sel5 = readl(&scu->clk_sel5); 28627881d20Sryan_chen ulong uart_clk = 0; 28727881d20Sryan_chen 28827881d20Sryan_chen switch(uart_idx) { 28927881d20Sryan_chen case 1: 29027881d20Sryan_chen case 2: 29127881d20Sryan_chen case 3: 29227881d20Sryan_chen case 4: 29327881d20Sryan_chen case 6: 29427881d20Sryan_chen if(uart_sel & BIT(uart_idx - 1)) 29527881d20Sryan_chen uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ; 296550e691bSryan_chen else 29727881d20Sryan_chen uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ; 29827881d20Sryan_chen break; 29927881d20Sryan_chen case 5: //24mhz is come form usb phy 48Mhz 30027881d20Sryan_chen { 30127881d20Sryan_chen u8 uart5_clk_sel = 0; 30227881d20Sryan_chen //high bit 30327881d20Sryan_chen if (readl(&scu->misc_ctrl1) & BIT(12)) 30427881d20Sryan_chen uart5_clk_sel = 0x2; 30527881d20Sryan_chen else 30627881d20Sryan_chen uart5_clk_sel = 0x0; 307550e691bSryan_chen 30827881d20Sryan_chen if (readl(&scu->clk_sel2) & BIT(14)) 30927881d20Sryan_chen uart5_clk_sel |= 0x1; 310550e691bSryan_chen 31127881d20Sryan_chen switch(uart5_clk_sel) { 31227881d20Sryan_chen case 0: 31327881d20Sryan_chen uart_clk = 24000000; 31427881d20Sryan_chen break; 31527881d20Sryan_chen case 1: 31627881d20Sryan_chen uart_clk = 0; 31727881d20Sryan_chen break; 31827881d20Sryan_chen case 2: 31927881d20Sryan_chen uart_clk = 24000000/13; 32027881d20Sryan_chen break; 32127881d20Sryan_chen case 3: 32227881d20Sryan_chen uart_clk = 192000000/13; 32327881d20Sryan_chen break; 32427881d20Sryan_chen } 32527881d20Sryan_chen } 32627881d20Sryan_chen break; 32727881d20Sryan_chen case 7: 32827881d20Sryan_chen case 8: 32927881d20Sryan_chen case 9: 33027881d20Sryan_chen case 10: 33127881d20Sryan_chen case 11: 33227881d20Sryan_chen case 12: 33327881d20Sryan_chen case 13: 33427881d20Sryan_chen if(uart_sel5 & BIT(uart_idx - 1)) 33527881d20Sryan_chen uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ; 33627881d20Sryan_chen else 33727881d20Sryan_chen uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ; 33827881d20Sryan_chen break; 33927881d20Sryan_chen } 34027881d20Sryan_chen 34127881d20Sryan_chen return uart_clk; 342550e691bSryan_chen } 343550e691bSryan_chen 344feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk) 345feb42054Sryan_chen { 346feb42054Sryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 347feb42054Sryan_chen ulong rate = 0; 348feb42054Sryan_chen 349feb42054Sryan_chen switch (clk->id) { 350feb42054Sryan_chen case ASPEED_CLK_HPLL: 351feb42054Sryan_chen rate = ast2600_get_hpll_rate(priv->scu); 352feb42054Sryan_chen break; 353d812df15Sryan_chen case ASPEED_CLK_MPLL: 354d812df15Sryan_chen rate = ast2600_get_mpll_rate(priv->scu); 355d812df15Sryan_chen break; 356feb42054Sryan_chen case ASPEED_CLK_AHB: 357feb42054Sryan_chen rate = ast2600_get_hclk(priv->scu); 358feb42054Sryan_chen break; 359feb42054Sryan_chen case ASPEED_CLK_APB: 360*2717883aSryan_chen rate = ast2600_get_pclk(priv->scu); 361feb42054Sryan_chen break; 362feb42054Sryan_chen case ASPEED_CLK_GATE_UART1CLK: 363feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 1); 364feb42054Sryan_chen break; 365feb42054Sryan_chen case ASPEED_CLK_GATE_UART2CLK: 366feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 2); 367feb42054Sryan_chen break; 368feb42054Sryan_chen case ASPEED_CLK_GATE_UART3CLK: 369feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 3); 370feb42054Sryan_chen break; 371feb42054Sryan_chen case ASPEED_CLK_GATE_UART4CLK: 372feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 4); 373feb42054Sryan_chen break; 374feb42054Sryan_chen case ASPEED_CLK_GATE_UART5CLK: 375feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 5); 376feb42054Sryan_chen break; 377feb42054Sryan_chen default: 378d812df15Sryan_chen pr_debug("can't get clk rate \n"); 379feb42054Sryan_chen return -ENOENT; 380d812df15Sryan_chen break; 381feb42054Sryan_chen } 382feb42054Sryan_chen 383feb42054Sryan_chen return rate; 384feb42054Sryan_chen } 385feb42054Sryan_chen 386550e691bSryan_chen struct aspeed_clock_config { 387550e691bSryan_chen ulong input_rate; 388550e691bSryan_chen ulong rate; 38939283ea7Sryan_chen struct ast2600_div_config cfg; 390550e691bSryan_chen }; 391550e691bSryan_chen 392550e691bSryan_chen static const struct aspeed_clock_config aspeed_clock_config_defaults[] = { 3931cd71a14SDylan Hung { 25000000, 400000000, { .num = 95, .denum = 2, .post_div = 1 } }, 394550e691bSryan_chen }; 395550e691bSryan_chen 396550e691bSryan_chen static bool aspeed_get_clock_config_default(ulong input_rate, 397550e691bSryan_chen ulong requested_rate, 39839283ea7Sryan_chen struct ast2600_div_config *cfg) 399550e691bSryan_chen { 400550e691bSryan_chen int i; 401550e691bSryan_chen 402550e691bSryan_chen for (i = 0; i < ARRAY_SIZE(aspeed_clock_config_defaults); i++) { 403550e691bSryan_chen const struct aspeed_clock_config *default_cfg = 404550e691bSryan_chen &aspeed_clock_config_defaults[i]; 405550e691bSryan_chen if (default_cfg->input_rate == input_rate && 406550e691bSryan_chen default_cfg->rate == requested_rate) { 407550e691bSryan_chen *cfg = default_cfg->cfg; 408550e691bSryan_chen return true; 409550e691bSryan_chen } 410550e691bSryan_chen } 411550e691bSryan_chen 412550e691bSryan_chen return false; 413550e691bSryan_chen } 414550e691bSryan_chen 415550e691bSryan_chen /* 416550e691bSryan_chen * @input_rate - the rate of input clock in Hz 417550e691bSryan_chen * @requested_rate - desired output rate in Hz 418550e691bSryan_chen * @div - this is an IN/OUT parameter, at input all fields of the config 419550e691bSryan_chen * need to be set to their maximum allowed values. 420550e691bSryan_chen * The result (the best config we could find), would also be returned 421550e691bSryan_chen * in this structure. 422550e691bSryan_chen * 423550e691bSryan_chen * @return The clock rate, when the resulting div_config is used. 424550e691bSryan_chen */ 425550e691bSryan_chen static ulong aspeed_calc_clock_config(ulong input_rate, ulong requested_rate, 42639283ea7Sryan_chen struct ast2600_div_config *cfg) 427550e691bSryan_chen { 428550e691bSryan_chen /* 429550e691bSryan_chen * The assumption is that kHz precision is good enough and 430550e691bSryan_chen * also enough to avoid overflow when multiplying. 431550e691bSryan_chen */ 432550e691bSryan_chen const ulong input_rate_khz = input_rate / 1000; 433550e691bSryan_chen const ulong rate_khz = requested_rate / 1000; 43439283ea7Sryan_chen const struct ast2600_div_config max_vals = *cfg; 43539283ea7Sryan_chen struct ast2600_div_config it = { 0, 0, 0 }; 436550e691bSryan_chen ulong delta = rate_khz; 437550e691bSryan_chen ulong new_rate_khz = 0; 438550e691bSryan_chen 439550e691bSryan_chen /* 440550e691bSryan_chen * Look for a well known frequency first. 441550e691bSryan_chen */ 442550e691bSryan_chen if (aspeed_get_clock_config_default(input_rate, requested_rate, cfg)) 443550e691bSryan_chen return requested_rate; 444550e691bSryan_chen 445550e691bSryan_chen for (; it.denum <= max_vals.denum; ++it.denum) { 446550e691bSryan_chen for (it.post_div = 0; it.post_div <= max_vals.post_div; 447550e691bSryan_chen ++it.post_div) { 448550e691bSryan_chen it.num = (rate_khz * (it.post_div + 1) / input_rate_khz) 449550e691bSryan_chen * (it.denum + 1); 450550e691bSryan_chen if (it.num > max_vals.num) 451550e691bSryan_chen continue; 452550e691bSryan_chen 453550e691bSryan_chen new_rate_khz = (input_rate_khz 454550e691bSryan_chen * ((it.num + 1) / (it.denum + 1))) 455550e691bSryan_chen / (it.post_div + 1); 456550e691bSryan_chen 457550e691bSryan_chen /* Keep the rate below requested one. */ 458550e691bSryan_chen if (new_rate_khz > rate_khz) 459550e691bSryan_chen continue; 460550e691bSryan_chen 461550e691bSryan_chen if (new_rate_khz - rate_khz < delta) { 462550e691bSryan_chen delta = new_rate_khz - rate_khz; 463550e691bSryan_chen *cfg = it; 464550e691bSryan_chen if (delta == 0) 465550e691bSryan_chen return new_rate_khz * 1000; 466550e691bSryan_chen } 467550e691bSryan_chen } 468550e691bSryan_chen } 469550e691bSryan_chen 470550e691bSryan_chen return new_rate_khz * 1000; 471550e691bSryan_chen } 472550e691bSryan_chen 473feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate) 474550e691bSryan_chen { 475d6e349c7Sryan_chen u32 clkin = AST2600_CLK_IN; 476550e691bSryan_chen u32 mpll_reg; 47739283ea7Sryan_chen struct ast2600_div_config div_cfg = { 478550e691bSryan_chen .num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT), 479550e691bSryan_chen .denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT), 480550e691bSryan_chen .post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT), 481550e691bSryan_chen }; 482550e691bSryan_chen 483550e691bSryan_chen aspeed_calc_clock_config(clkin, rate, &div_cfg); 484550e691bSryan_chen 485feb42054Sryan_chen mpll_reg = readl(&scu->m_pll_param); 486550e691bSryan_chen mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK 487550e691bSryan_chen | SCU_MPLL_DENUM_MASK); 488550e691bSryan_chen mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT) 489550e691bSryan_chen | (div_cfg.num << SCU_MPLL_NUM_SHIFT) 490550e691bSryan_chen | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT); 491550e691bSryan_chen 492feb42054Sryan_chen writel(mpll_reg, &scu->m_pll_param); 493550e691bSryan_chen 494feb42054Sryan_chen return ast2600_get_mpll_rate(scu); 495d6e349c7Sryan_chen } 496d6e349c7Sryan_chen 497d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate) 498550e691bSryan_chen { 499f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 500550e691bSryan_chen 501550e691bSryan_chen ulong new_rate; 502550e691bSryan_chen switch (clk->id) { 503f0d895afSryan_chen case ASPEED_CLK_MPLL: 504feb42054Sryan_chen new_rate = ast2600_configure_ddr(priv->scu, rate); 505550e691bSryan_chen break; 506550e691bSryan_chen default: 507550e691bSryan_chen return -ENOENT; 508550e691bSryan_chen } 509550e691bSryan_chen 510550e691bSryan_chen return new_rate; 511550e691bSryan_chen } 512feb42054Sryan_chen 513f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1 (20) 514f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2 (21) 515f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3 (20) 516f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4 (21) 517f9aa0ee1Sryan_chen 518f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index) 519f9aa0ee1Sryan_chen { 520f9aa0ee1Sryan_chen u32 reset_bit; 521f9aa0ee1Sryan_chen u32 clkstop_bit; 522f9aa0ee1Sryan_chen 523f9aa0ee1Sryan_chen 524f9aa0ee1Sryan_chen switch (index) { 525f9aa0ee1Sryan_chen case 1: 526f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC1); 527f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC1); 528f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 529f9aa0ee1Sryan_chen udelay(100); 530f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 531f9aa0ee1Sryan_chen mdelay(10); 532f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 533f9aa0ee1Sryan_chen 534f9aa0ee1Sryan_chen break; 535f9aa0ee1Sryan_chen case 2: 536f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC2); 537f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC2); 538f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 539f9aa0ee1Sryan_chen udelay(100); 540f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 541f9aa0ee1Sryan_chen mdelay(10); 542f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 543f9aa0ee1Sryan_chen break; 544f9aa0ee1Sryan_chen case 3: 545f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC3 - 32); 546f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC3); 547f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 548f9aa0ee1Sryan_chen udelay(100); 549f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 550f9aa0ee1Sryan_chen mdelay(10); 551f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 552f9aa0ee1Sryan_chen break; 553f9aa0ee1Sryan_chen case 4: 554f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC4 - 32); 555f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC4); 556f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 557f9aa0ee1Sryan_chen udelay(100); 558f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 559f9aa0ee1Sryan_chen mdelay(10); 560f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 561f9aa0ee1Sryan_chen break; 562f9aa0ee1Sryan_chen default: 563f9aa0ee1Sryan_chen return -EINVAL; 564f9aa0ee1Sryan_chen } 565f9aa0ee1Sryan_chen 566f9aa0ee1Sryan_chen return 0; 567f9aa0ee1Sryan_chen } 568550e691bSryan_chen 569d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk) 570550e691bSryan_chen { 571f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 572550e691bSryan_chen 573550e691bSryan_chen switch (clk->id) { 57486f91560Sryan_chen case ASPEED_CLK_GATE_MAC1CLK: 57586f91560Sryan_chen ast2600_configure_mac(priv->scu, 1); 576550e691bSryan_chen break; 57786f91560Sryan_chen case ASPEED_CLK_GATE_MAC2CLK: 57886f91560Sryan_chen ast2600_configure_mac(priv->scu, 2); 579550e691bSryan_chen break; 58077843939Sryan_chen case ASPEED_CLK_GATE_MAC3CLK: 58177843939Sryan_chen ast2600_configure_mac(priv->scu, 3); 58277843939Sryan_chen break; 58377843939Sryan_chen case ASPEED_CLK_GATE_MAC4CLK: 58477843939Sryan_chen ast2600_configure_mac(priv->scu, 4); 58577843939Sryan_chen break; 586550e691bSryan_chen default: 587f9aa0ee1Sryan_chen pr_debug("can't enable clk \n"); 588550e691bSryan_chen return -ENOENT; 58977843939Sryan_chen break; 590550e691bSryan_chen } 591550e691bSryan_chen 592550e691bSryan_chen return 0; 593550e691bSryan_chen } 594550e691bSryan_chen 595f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = { 596d6e349c7Sryan_chen .get_rate = ast2600_clk_get_rate, 597d6e349c7Sryan_chen .set_rate = ast2600_clk_set_rate, 598d6e349c7Sryan_chen .enable = ast2600_clk_enable, 599550e691bSryan_chen }; 600550e691bSryan_chen 601d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev) 602550e691bSryan_chen { 603f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(dev); 604550e691bSryan_chen 605f0d895afSryan_chen priv->scu = devfdt_get_addr_ptr(dev); 606f0d895afSryan_chen if (IS_ERR(priv->scu)) 607f0d895afSryan_chen return PTR_ERR(priv->scu); 608550e691bSryan_chen 609550e691bSryan_chen return 0; 610550e691bSryan_chen } 611550e691bSryan_chen 612d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev) 613550e691bSryan_chen { 614550e691bSryan_chen int ret; 615550e691bSryan_chen 616550e691bSryan_chen /* The reset driver does not have a device node, so bind it here */ 617550e691bSryan_chen ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev); 618550e691bSryan_chen if (ret) 619550e691bSryan_chen debug("Warning: No reset driver: ret=%d\n", ret); 620550e691bSryan_chen 621550e691bSryan_chen return 0; 622550e691bSryan_chen } 623550e691bSryan_chen 624d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = { 625d6e349c7Sryan_chen { .compatible = "aspeed,ast2600-scu", }, 626550e691bSryan_chen { } 627550e691bSryan_chen }; 628550e691bSryan_chen 629aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = { 630aa36597fSDylan Hung .name = "aspeed_scu", 631550e691bSryan_chen .id = UCLASS_CLK, 632d6e349c7Sryan_chen .of_match = ast2600_clk_ids, 633f0d895afSryan_chen .priv_auto_alloc_size = sizeof(struct ast2600_clk_priv), 634f9aa0ee1Sryan_chen .ops = &ast2600_clk_ops, 635d6e349c7Sryan_chen .bind = ast2600_clk_bind, 636d6e349c7Sryan_chen .probe = ast2600_clk_probe, 637550e691bSryan_chen }; 638