1550e691bSryan_chen // SPDX-License-Identifier: GPL-2.0 2550e691bSryan_chen /* 3550e691bSryan_chen * Copyright (C) ASPEED Technology Inc. 4550e691bSryan_chen */ 5550e691bSryan_chen 6550e691bSryan_chen #include <common.h> 7550e691bSryan_chen #include <clk-uclass.h> 8550e691bSryan_chen #include <dm.h> 9550e691bSryan_chen #include <asm/io.h> 10550e691bSryan_chen #include <dm/lists.h> 1162a6bcbfSryan_chen #include <asm/arch/scu_ast2600.h> 12d6e349c7Sryan_chen #include <dt-bindings/clock/ast2600-clock.h> 1339283ea7Sryan_chen #include <dt-bindings/reset/ast2600-reset.h> 14550e691bSryan_chen 15550e691bSryan_chen /* 16a8fc7648SRyan Chen * MAC Clock Delay settings 17550e691bSryan_chen */ 18550e691bSryan_chen #define RGMII_TXCLK_ODLY 8 19550e691bSryan_chen #define RMII_RXCLK_IDLY 2 20550e691bSryan_chen 213dc90377SDylan Hung #define MAC_DEF_DELAY_1G 0x0041b75d 2275605a4bSDylan Hung #define MAC_DEF_DELAY_100M 0x00417410 2375605a4bSDylan Hung #define MAC_DEF_DELAY_10M 0x00417410 2454f9cba1SDylan Hung 253dc90377SDylan Hung #define MAC34_DEF_DELAY_1G 0x0010438a 2654f9cba1SDylan Hung #define MAC34_DEF_DELAY_100M 0x00104208 2754f9cba1SDylan Hung #define MAC34_DEF_DELAY_10M 0x00104208 284760b3f8SDylan Hung 29550e691bSryan_chen /* 30550e691bSryan_chen * TGMII Clock Duty constants, taken from Aspeed SDK 31550e691bSryan_chen */ 32550e691bSryan_chen #define RGMII2_TXCK_DUTY 0x66 33550e691bSryan_chen #define RGMII1_TXCK_DUTY 0x64 34550e691bSryan_chen #define D2PLL_DEFAULT_RATE (250 * 1000 * 1000) 3585d48d8cSryan_chen #define CHIP_REVISION_ID GENMASK(23, 16) 3685d48d8cSryan_chen 37550e691bSryan_chen DECLARE_GLOBAL_DATA_PTR; 38550e691bSryan_chen 39550e691bSryan_chen /* 40550e691bSryan_chen * Clock divider/multiplier configuration struct. 41550e691bSryan_chen * For H-PLL and M-PLL the formula is 42550e691bSryan_chen * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1) 43550e691bSryan_chen * M - Numerator 44550e691bSryan_chen * N - Denumerator 45550e691bSryan_chen * P - Post Divider 46550e691bSryan_chen * They have the same layout in their control register. 47550e691bSryan_chen * 48550e691bSryan_chen * D-PLL and D2-PLL have extra divider (OD + 1), which is not 49550e691bSryan_chen * yet needed and ignored by clock configurations. 50550e691bSryan_chen */ 51577fcdaeSDylan Hung union ast2600_pll_reg { 52334bd202SDylan Hung u32 w; 53577fcdaeSDylan Hung struct { 54fd52be0bSDylan Hung unsigned int m : 13; /* bit[12:0] */ 55fd52be0bSDylan Hung unsigned int n : 6; /* bit[18:13] */ 56fd52be0bSDylan Hung unsigned int p : 4; /* bit[22:19] */ 57fd52be0bSDylan Hung unsigned int off : 1; /* bit[23] */ 58fd52be0bSDylan Hung unsigned int bypass : 1; /* bit[24] */ 59fd52be0bSDylan Hung unsigned int reset : 1; /* bit[25] */ 60fd52be0bSDylan Hung unsigned int reserved : 6; /* bit[31:26] */ 61577fcdaeSDylan Hung } b; 62577fcdaeSDylan Hung }; 63577fcdaeSDylan Hung 64577fcdaeSDylan Hung struct ast2600_pll_cfg { 65577fcdaeSDylan Hung union ast2600_pll_reg reg; 66334bd202SDylan Hung u32 ext_reg; 67577fcdaeSDylan Hung }; 68577fcdaeSDylan Hung 69577fcdaeSDylan Hung struct ast2600_pll_desc { 70577fcdaeSDylan Hung u32 in; 71577fcdaeSDylan Hung u32 out; 72577fcdaeSDylan Hung struct ast2600_pll_cfg cfg; 73577fcdaeSDylan Hung }; 74577fcdaeSDylan Hung 75577fcdaeSDylan Hung static const struct ast2600_pll_desc ast2600_pll_lookup[] = { 76a8fc7648SRyan Chen { 775d05f4fcSRyan Chen .in = AST2600_CLK_IN, 785d05f4fcSRyan Chen .out = 400000000, 795d05f4fcSRyan Chen .cfg.reg.b.m = 95, 805d05f4fcSRyan Chen .cfg.reg.b.n = 2, 815d05f4fcSRyan Chen .cfg.reg.b.p = 1, 82577fcdaeSDylan Hung .cfg.ext_reg = 0x31, 83fa59add1SRyan Chen }, { 84fa59add1SRyan Chen .in = AST2600_CLK_IN, 855d05f4fcSRyan Chen .out = 200000000, 865d05f4fcSRyan Chen .cfg.reg.b.m = 127, 875d05f4fcSRyan Chen .cfg.reg.b.n = 0, 885d05f4fcSRyan Chen .cfg.reg.b.p = 15, 89fa59add1SRyan Chen .cfg.ext_reg = 0x3f, 90fa59add1SRyan Chen }, { 91fa59add1SRyan Chen .in = AST2600_CLK_IN, 925d05f4fcSRyan Chen .out = 334000000, 935d05f4fcSRyan Chen .cfg.reg.b.m = 667, 945d05f4fcSRyan Chen .cfg.reg.b.n = 4, 955d05f4fcSRyan Chen .cfg.reg.b.p = 9, 96fa59add1SRyan Chen .cfg.ext_reg = 0x14d, 97fa59add1SRyan Chen }, { 98fa59add1SRyan Chen .in = AST2600_CLK_IN, 995d05f4fcSRyan Chen .out = 1000000000, 1005d05f4fcSRyan Chen .cfg.reg.b.m = 119, 1015d05f4fcSRyan Chen .cfg.reg.b.n = 2, 1025d05f4fcSRyan Chen .cfg.reg.b.p = 0, 103fa59add1SRyan Chen .cfg.ext_reg = 0x3d, 104fa59add1SRyan Chen }, { 105fa59add1SRyan Chen .in = AST2600_CLK_IN, 1065d05f4fcSRyan Chen .out = 50000000, 1075d05f4fcSRyan Chen .cfg.reg.b.m = 95, 1085d05f4fcSRyan Chen .cfg.reg.b.n = 2, 1095d05f4fcSRyan Chen .cfg.reg.b.p = 15, 110fa59add1SRyan Chen .cfg.ext_reg = 0x31, 111fa59add1SRyan Chen }, 112550e691bSryan_chen }; 113550e691bSryan_chen 114a98c71fbSDylan Hung union mac_delay_1g { 115a98c71fbSDylan Hung u32 w; 116a98c71fbSDylan Hung struct { 117a98c71fbSDylan Hung unsigned int tx_delay_1 : 6; /* bit[5:0] */ 118a98c71fbSDylan Hung unsigned int tx_delay_2 : 6; /* bit[11:6] */ 119a98c71fbSDylan Hung unsigned int rx_delay_1 : 6; /* bit[17:12] */ 120a98c71fbSDylan Hung unsigned int rx_delay_2 : 6; /* bit[23:18] */ 121a98c71fbSDylan Hung unsigned int rx_clk_inv_1 : 1; /* bit[24] */ 122a98c71fbSDylan Hung unsigned int rx_clk_inv_2 : 1; /* bit[25] */ 123a98c71fbSDylan Hung unsigned int rmii_tx_data_at_falling_1 : 1; /* bit[26] */ 124a98c71fbSDylan Hung unsigned int rmii_tx_data_at_falling_2 : 1; /* bit[27] */ 125a98c71fbSDylan Hung unsigned int rgmiick_pad_dir : 1; /* bit[28] */ 126a98c71fbSDylan Hung unsigned int rmii_50m_oe_1 : 1; /* bit[29] */ 127a98c71fbSDylan Hung unsigned int rmii_50m_oe_2 : 1; /* bit[30] */ 128a98c71fbSDylan Hung unsigned int rgmii_125m_o_sel : 1; /* bit[31] */ 129a98c71fbSDylan Hung } b; 130a98c71fbSDylan Hung }; 131a98c71fbSDylan Hung 132a98c71fbSDylan Hung union mac_delay_100_10 { 133a98c71fbSDylan Hung u32 w; 134a98c71fbSDylan Hung struct { 135a98c71fbSDylan Hung unsigned int tx_delay_1 : 6; /* bit[5:0] */ 136a98c71fbSDylan Hung unsigned int tx_delay_2 : 6; /* bit[11:6] */ 137a98c71fbSDylan Hung unsigned int rx_delay_1 : 6; /* bit[17:12] */ 138a98c71fbSDylan Hung unsigned int rx_delay_2 : 6; /* bit[23:18] */ 139a98c71fbSDylan Hung unsigned int rx_clk_inv_1 : 1; /* bit[24] */ 140a98c71fbSDylan Hung unsigned int rx_clk_inv_2 : 1; /* bit[25] */ 141a98c71fbSDylan Hung unsigned int reserved_0 : 6; /* bit[31:26] */ 142a98c71fbSDylan Hung } b; 143a98c71fbSDylan Hung }; 144a98c71fbSDylan Hung 145a98c71fbSDylan Hung struct mac_delay_config { 146a98c71fbSDylan Hung u32 tx_delay_1000; 147a98c71fbSDylan Hung u32 rx_delay_1000; 148a98c71fbSDylan Hung u32 tx_delay_100; 149a98c71fbSDylan Hung u32 rx_delay_100; 150a98c71fbSDylan Hung u32 tx_delay_10; 151a98c71fbSDylan Hung u32 rx_delay_10; 152a98c71fbSDylan Hung }; 153a98c71fbSDylan Hung 154bbbfb0c5Sryan_chen extern u32 ast2600_get_pll_rate(struct ast2600_scu *scu, int pll_idx) 155550e691bSryan_chen { 156d6e349c7Sryan_chen u32 clkin = AST2600_CLK_IN; 157bbbfb0c5Sryan_chen u32 pll_reg = 0; 1589639db61Sryan_chen unsigned int mult, div = 1; 159550e691bSryan_chen 160bbbfb0c5Sryan_chen switch (pll_idx) { 161bbbfb0c5Sryan_chen case ASPEED_CLK_HPLL: 162bbbfb0c5Sryan_chen pll_reg = readl(&scu->h_pll_param); 163bbbfb0c5Sryan_chen break; 164bbbfb0c5Sryan_chen case ASPEED_CLK_MPLL: 165bbbfb0c5Sryan_chen pll_reg = readl(&scu->m_pll_param); 166bbbfb0c5Sryan_chen break; 167bbbfb0c5Sryan_chen case ASPEED_CLK_DPLL: 168bbbfb0c5Sryan_chen pll_reg = readl(&scu->d_pll_param); 169bbbfb0c5Sryan_chen break; 170bbbfb0c5Sryan_chen case ASPEED_CLK_EPLL: 171bbbfb0c5Sryan_chen pll_reg = readl(&scu->e_pll_param); 172bbbfb0c5Sryan_chen break; 173bbbfb0c5Sryan_chen } 174bbbfb0c5Sryan_chen if (pll_reg & BIT(24)) { 1759639db61Sryan_chen /* Pass through mode */ 176ed3899c5SRyan Chen mult = 1; 177ed3899c5SRyan Chen div = 1; 1789639db61Sryan_chen } else { 17975ced45aSDylan Hung union ast2600_pll_reg reg; 180ed3899c5SRyan Chen /* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) 181ed3899c5SRyan Chen * HPLL Numerator (M) = fix 0x5F when SCU500[10]=1 182ed3899c5SRyan Chen * Fixed 0xBF when SCU500[10]=0 and SCU500[8]=1 183ed3899c5SRyan Chen * SCU200[12:0] (default 0x8F) when SCU510[10]=0 and SCU510[8]=0 184ed3899c5SRyan Chen * HPLL Denumerator (N) = SCU200[18:13] (default 0x2) 185ed3899c5SRyan Chen * HPLL Divider (P) = SCU200[22:19] (default 0x0) 186ed3899c5SRyan Chen * HPLL Bandwidth Adj (NB) = fix 0x2F when SCU500[10]=1 187ed3899c5SRyan Chen * Fixed 0x5F when SCU500[10]=0 and SCU500[8]=1 188ed3899c5SRyan Chen * SCU204[11:0] (default 0x31) when SCU500[10]=0 and SCU500[8]=0 189e5c4f4dfSryan_chen */ 190ed3899c5SRyan Chen reg.w = pll_reg; 191f27685ebSRyan Chen if (pll_idx == ASPEED_CLK_HPLL) { 192e5c4f4dfSryan_chen u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap); 193ed3899c5SRyan Chen 194ed3899c5SRyan Chen if (hwstrap1 & BIT(10)) { 195e5c4f4dfSryan_chen reg.b.m = 0x5F; 196ed3899c5SRyan Chen } else { 197e5c4f4dfSryan_chen if (hwstrap1 & BIT(8)) 198e5c4f4dfSryan_chen reg.b.m = 0xBF; 199a8fc7648SRyan Chen /* Otherwise keep default 0x8F */ 200e5c4f4dfSryan_chen } 201e5c4f4dfSryan_chen } 20275ced45aSDylan Hung mult = (reg.b.m + 1) / (reg.b.n + 1); 20375ced45aSDylan Hung div = (reg.b.p + 1); 2049639db61Sryan_chen } 205a8fc7648SRyan Chen 2069639db61Sryan_chen return ((clkin * mult) / div); 207550e691bSryan_chen } 208550e691bSryan_chen 2094f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu) 210550e691bSryan_chen { 21185d48d8cSryan_chen u32 hw_rev = readl(&scu->chip_id1); 212bbbfb0c5Sryan_chen u32 clkin = AST2600_CLK_IN; 21339283ea7Sryan_chen u32 apll_reg = readl(&scu->a_pll_param); 21439283ea7Sryan_chen unsigned int mult, div = 1; 215d6e349c7Sryan_chen 2168d615c79SRyan Chen if (((hw_rev & CHIP_REVISION_ID) >> 16) >= 2) { 217a8fc7648SRyan Chen //after A2 version 21885d48d8cSryan_chen if (apll_reg & BIT(24)) { 21985d48d8cSryan_chen /* Pass through mode */ 220ed3899c5SRyan Chen mult = 1; 221ed3899c5SRyan Chen div = 1; 22285d48d8cSryan_chen } else { 22385d48d8cSryan_chen /* F = 25Mhz * [(m + 1) / (n + 1)] / (p + 1) */ 22485d48d8cSryan_chen u32 m = apll_reg & 0x1fff; 22585d48d8cSryan_chen u32 n = (apll_reg >> 13) & 0x3f; 22685d48d8cSryan_chen u32 p = (apll_reg >> 19) & 0xf; 22785d48d8cSryan_chen 22885d48d8cSryan_chen mult = (m + 1); 22985d48d8cSryan_chen div = (n + 1) * (p + 1); 23085d48d8cSryan_chen } 23185d48d8cSryan_chen } else { 23239283ea7Sryan_chen if (apll_reg & BIT(20)) { 233d6e349c7Sryan_chen /* Pass through mode */ 234ed3899c5SRyan Chen mult = 1; 235ed3899c5SRyan Chen div = 1; 236d6e349c7Sryan_chen } else { 237bbbfb0c5Sryan_chen /* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */ 23839283ea7Sryan_chen u32 m = (apll_reg >> 5) & 0x3f; 23939283ea7Sryan_chen u32 od = (apll_reg >> 4) & 0x1; 24039283ea7Sryan_chen u32 n = apll_reg & 0xf; 241d6e349c7Sryan_chen 242bbbfb0c5Sryan_chen mult = (2 - od) * (m + 2); 243bbbfb0c5Sryan_chen div = n + 1; 244d6e349c7Sryan_chen } 24585d48d8cSryan_chen } 246a8fc7648SRyan Chen 247bbbfb0c5Sryan_chen return ((clkin * mult) / div); 24839283ea7Sryan_chen } 24939283ea7Sryan_chen 250d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = { 2515d05f4fcSRyan Chen 2, 2525d05f4fcSRyan Chen 2, 2535d05f4fcSRyan Chen 3, 2545d05f4fcSRyan Chen 4, 255d812df15Sryan_chen }; 256d812df15Sryan_chen 25745e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div0_table[] = { 2585d05f4fcSRyan Chen 3, 2595d05f4fcSRyan Chen 2, 2605d05f4fcSRyan Chen 3, 2615d05f4fcSRyan Chen 4, 26245e0908aSryan_chen }; 26345e0908aSryan_chen 26445e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div1_table[] = { 2655d05f4fcSRyan Chen 3, 2665d05f4fcSRyan Chen 4, 2675d05f4fcSRyan Chen 6, 2685d05f4fcSRyan Chen 8, 269e29dc694Sryan_chen }; 270e29dc694Sryan_chen 271e29dc694Sryan_chen static u32 ast2600_a1_axi_ahb_default_table[] = { 272e29dc694Sryan_chen 3, 4, 3, 4, 2, 2, 2, 2, 273d812df15Sryan_chen }; 274d812df15Sryan_chen 275d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu) 276d812df15Sryan_chen { 27785d48d8cSryan_chen u32 hw_rev = readl(&scu->chip_id1); 27845e0908aSryan_chen u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap); 279d812df15Sryan_chen u32 axi_div = 1; 280d812df15Sryan_chen u32 ahb_div = 0; 281d812df15Sryan_chen u32 rate = 0; 282d812df15Sryan_chen 28385d48d8cSryan_chen if ((hw_rev & CHIP_REVISION_ID) >> 16) { 284a8fc7648SRyan Chen //After A0 28545e0908aSryan_chen if (hwstrap1 & BIT(16)) { 286a8fc7648SRyan Chen ast2600_a1_axi_ahb_div1_table[0] = 2875d05f4fcSRyan Chen ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 28822545706SRyan Chen 0x7] * 2; 289d812df15Sryan_chen axi_div = 1; 2905d05f4fcSRyan Chen ahb_div = 2915d05f4fcSRyan Chen ast2600_a1_axi_ahb_div1_table[(hwstrap1 >> 11) & 2925d05f4fcSRyan Chen 0x3]; 29345e0908aSryan_chen } else { 294a8fc7648SRyan Chen ast2600_a1_axi_ahb_div0_table[0] = 2955d05f4fcSRyan Chen ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 29622545706SRyan Chen 0x7]; 297d812df15Sryan_chen axi_div = 2; 2985d05f4fcSRyan Chen ahb_div = 2995d05f4fcSRyan Chen ast2600_a1_axi_ahb_div0_table[(hwstrap1 >> 11) & 3005d05f4fcSRyan Chen 0x3]; 30145e0908aSryan_chen } 30245e0908aSryan_chen } else { 303a8fc7648SRyan Chen //A0 : fix axi = hpll / 2 30445e0908aSryan_chen axi_div = 2; 305d812df15Sryan_chen ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3]; 30645e0908aSryan_chen } 307bbbfb0c5Sryan_chen rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 308a8fc7648SRyan Chen 3092717883aSryan_chen return (rate / axi_div / ahb_div); 3102717883aSryan_chen } 3112717883aSryan_chen 312c304f173Sryan_chen static u32 ast2600_get_bclk_rate(struct ast2600_scu *scu) 313c304f173Sryan_chen { 314c304f173Sryan_chen u32 rate; 315c304f173Sryan_chen u32 bclk_sel = (readl(&scu->clk_sel1) >> 20) & 0x7; 316ed3899c5SRyan Chen 317c304f173Sryan_chen rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 318c304f173Sryan_chen 319c304f173Sryan_chen return (rate / ((bclk_sel + 1) * 4)); 320c304f173Sryan_chen } 321c304f173Sryan_chen 3226fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = { 3232717883aSryan_chen 4, 8, 12, 16, 20, 24, 28, 32, 3242717883aSryan_chen }; 3252717883aSryan_chen 3266fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = { 3276fa1ef3dSryan_chen 2, 4, 6, 8, 10, 12, 14, 16, 3286fa1ef3dSryan_chen }; 3296fa1ef3dSryan_chen 3306fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu) 3312717883aSryan_chen { 3322717883aSryan_chen u32 clk_sel1 = readl(&scu->clk_sel1); 3336fa1ef3dSryan_chen u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)]; 334bbbfb0c5Sryan_chen u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 3352717883aSryan_chen 3362717883aSryan_chen return (rate / apb_div); 337d812df15Sryan_chen } 338d812df15Sryan_chen 3396fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu) 3406fa1ef3dSryan_chen { 3416fa1ef3dSryan_chen u32 clk_sel4 = readl(&scu->clk_sel4); 3426fa1ef3dSryan_chen u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)]; 3436fa1ef3dSryan_chen u32 rate = ast2600_get_hclk(scu); 3446fa1ef3dSryan_chen 3456fa1ef3dSryan_chen return (rate / apb_div); 3466fa1ef3dSryan_chen } 3476fa1ef3dSryan_chen 3482e195992Sryan_chen static u32 ast2600_get_uxclk_in_rate(struct ast2600_scu *scu) 349d6e349c7Sryan_chen { 35027881d20Sryan_chen u32 clk_in = 0; 3512e195992Sryan_chen u32 uxclk_sel = readl(&scu->clk_sel5); 352550e691bSryan_chen 35327881d20Sryan_chen uxclk_sel &= 0x3; 35427881d20Sryan_chen switch (uxclk_sel) { 35527881d20Sryan_chen case 0: 35627881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 35727881d20Sryan_chen break; 35827881d20Sryan_chen case 1: 35927881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 36027881d20Sryan_chen break; 36127881d20Sryan_chen case 2: 36227881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 36327881d20Sryan_chen break; 36427881d20Sryan_chen case 3: 36527881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 36627881d20Sryan_chen break; 36727881d20Sryan_chen } 368d6e349c7Sryan_chen 36927881d20Sryan_chen return clk_in; 37027881d20Sryan_chen } 37127881d20Sryan_chen 3722e195992Sryan_chen static u32 ast2600_get_huxclk_in_rate(struct ast2600_scu *scu) 37327881d20Sryan_chen { 37427881d20Sryan_chen u32 clk_in = 0; 3752e195992Sryan_chen u32 huclk_sel = readl(&scu->clk_sel5); 37627881d20Sryan_chen 37727881d20Sryan_chen huclk_sel = ((huclk_sel >> 3) & 0x3); 37827881d20Sryan_chen switch (huclk_sel) { 37927881d20Sryan_chen case 0: 38027881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 38127881d20Sryan_chen break; 38227881d20Sryan_chen case 1: 38327881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 38427881d20Sryan_chen break; 38527881d20Sryan_chen case 2: 38627881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 38727881d20Sryan_chen break; 38827881d20Sryan_chen case 3: 38927881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 39027881d20Sryan_chen break; 39127881d20Sryan_chen } 39227881d20Sryan_chen 39327881d20Sryan_chen return clk_in; 39427881d20Sryan_chen } 39527881d20Sryan_chen 3962e195992Sryan_chen static u32 ast2600_get_uart_uxclk_rate(struct ast2600_scu *scu) 39727881d20Sryan_chen { 3982e195992Sryan_chen u32 clk_in = ast2600_get_uxclk_in_rate(scu); 39927881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_uxclk); 40027881d20Sryan_chen unsigned int mult, div; 40127881d20Sryan_chen 40227881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 40327881d20Sryan_chen u32 r = div_reg & 0xff; 40427881d20Sryan_chen 40527881d20Sryan_chen mult = r; 4062e195992Sryan_chen div = (n * 2); 40727881d20Sryan_chen return (clk_in * mult) / div; 40827881d20Sryan_chen } 40927881d20Sryan_chen 4102e195992Sryan_chen static u32 ast2600_get_uart_huxclk_rate(struct ast2600_scu *scu) 41127881d20Sryan_chen { 4122e195992Sryan_chen u32 clk_in = ast2600_get_huxclk_in_rate(scu); 41327881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_huxclk); 41427881d20Sryan_chen 41527881d20Sryan_chen unsigned int mult, div; 41627881d20Sryan_chen 41727881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 41827881d20Sryan_chen u32 r = div_reg & 0xff; 41927881d20Sryan_chen 42027881d20Sryan_chen mult = r; 4212e195992Sryan_chen div = (n * 2); 42227881d20Sryan_chen return (clk_in * mult) / div; 42327881d20Sryan_chen } 42427881d20Sryan_chen 425f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu) 426f51926eeSryan_chen { 427f51926eeSryan_chen u32 clkin = 0; 428f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel4); 429f51926eeSryan_chen u32 div = (clk_sel >> 28) & 0x7; 430*8004dfdeSChin-Ting Kuo u32 hw_rev = readl(&scu->chip_id1); 431f51926eeSryan_chen 432ed3899c5SRyan Chen if (clk_sel & BIT(8)) 433f51926eeSryan_chen clkin = ast2600_get_apll_rate(scu); 434ed3899c5SRyan Chen else 43510069884Sryan_chen clkin = ast2600_get_hclk(scu); 436ed3899c5SRyan Chen 437*8004dfdeSChin-Ting Kuo div = (1 + div) * 2; 438*8004dfdeSChin-Ting Kuo if (((hw_rev & GENMASK(23, 16)) >> 16) >= 2) 439*8004dfdeSChin-Ting Kuo div = (div & 0xf) ? div : 1; 440f51926eeSryan_chen 441f51926eeSryan_chen return (clkin / div); 442f51926eeSryan_chen } 443f51926eeSryan_chen 444f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu) 445f51926eeSryan_chen { 446125f2e11SChin-Ting Kuo u32 mmc_clk_src = readl(&scu->clk_sel1); 447125f2e11SChin-Ting Kuo u32 clkin; 448f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel1); 449f51926eeSryan_chen u32 div = (clk_sel >> 12) & 0x7; 450f51926eeSryan_chen 451125f2e11SChin-Ting Kuo if (mmc_clk_src & BIT(11)) { 452125f2e11SChin-Ting Kuo /* emmc clock comes from MPLL */ 453125f2e11SChin-Ting Kuo clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL); 454125f2e11SChin-Ting Kuo div = (div + 1) * 2; 455125f2e11SChin-Ting Kuo } else { 456125f2e11SChin-Ting Kuo clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 457f51926eeSryan_chen div = (div + 1) << 2; 458125f2e11SChin-Ting Kuo } 459f51926eeSryan_chen 460f51926eeSryan_chen return (clkin / div); 461f51926eeSryan_chen } 462f51926eeSryan_chen 463f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx) 46427881d20Sryan_chen { 46527881d20Sryan_chen u32 uart_sel = readl(&scu->clk_sel4); 46627881d20Sryan_chen u32 uart_sel5 = readl(&scu->clk_sel5); 46727881d20Sryan_chen ulong uart_clk = 0; 46827881d20Sryan_chen 46927881d20Sryan_chen switch (uart_idx) { 47027881d20Sryan_chen case 1: 47127881d20Sryan_chen case 2: 47227881d20Sryan_chen case 3: 47327881d20Sryan_chen case 4: 47427881d20Sryan_chen case 6: 47527881d20Sryan_chen if (uart_sel & BIT(uart_idx - 1)) 4762e195992Sryan_chen uart_clk = ast2600_get_uart_huxclk_rate(scu); 477550e691bSryan_chen else 4782e195992Sryan_chen uart_clk = ast2600_get_uart_uxclk_rate(scu); 47927881d20Sryan_chen break; 48027881d20Sryan_chen case 5: //24mhz is come form usb phy 48Mhz 48127881d20Sryan_chen { 48227881d20Sryan_chen u8 uart5_clk_sel = 0; 48327881d20Sryan_chen //high bit 48427881d20Sryan_chen if (readl(&scu->misc_ctrl1) & BIT(12)) 48527881d20Sryan_chen uart5_clk_sel = 0x2; 48627881d20Sryan_chen else 48727881d20Sryan_chen uart5_clk_sel = 0x0; 488550e691bSryan_chen 48927881d20Sryan_chen if (readl(&scu->clk_sel2) & BIT(14)) 49027881d20Sryan_chen uart5_clk_sel |= 0x1; 491550e691bSryan_chen 49227881d20Sryan_chen switch (uart5_clk_sel) { 49327881d20Sryan_chen case 0: 49427881d20Sryan_chen uart_clk = 24000000; 49527881d20Sryan_chen break; 49627881d20Sryan_chen case 1: 497def99fcbSryan_chen uart_clk = 192000000; 49827881d20Sryan_chen break; 49927881d20Sryan_chen case 2: 50027881d20Sryan_chen uart_clk = 24000000 / 13; 50127881d20Sryan_chen break; 50227881d20Sryan_chen case 3: 50327881d20Sryan_chen uart_clk = 192000000 / 13; 50427881d20Sryan_chen break; 50527881d20Sryan_chen } 5065d05f4fcSRyan Chen } break; 50727881d20Sryan_chen case 7: 50827881d20Sryan_chen case 8: 50927881d20Sryan_chen case 9: 51027881d20Sryan_chen case 10: 51127881d20Sryan_chen case 11: 51227881d20Sryan_chen case 12: 51327881d20Sryan_chen case 13: 51427881d20Sryan_chen if (uart_sel5 & BIT(uart_idx - 1)) 5152e195992Sryan_chen uart_clk = ast2600_get_uart_huxclk_rate(scu); 51627881d20Sryan_chen else 5172e195992Sryan_chen uart_clk = ast2600_get_uart_uxclk_rate(scu); 51827881d20Sryan_chen break; 51927881d20Sryan_chen } 52027881d20Sryan_chen 52127881d20Sryan_chen return uart_clk; 522550e691bSryan_chen } 523550e691bSryan_chen 524feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk) 525feb42054Sryan_chen { 526feb42054Sryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 527feb42054Sryan_chen ulong rate = 0; 528feb42054Sryan_chen 529feb42054Sryan_chen switch (clk->id) { 530feb42054Sryan_chen case ASPEED_CLK_HPLL: 531bbbfb0c5Sryan_chen case ASPEED_CLK_EPLL: 532bbbfb0c5Sryan_chen case ASPEED_CLK_DPLL: 533d812df15Sryan_chen case ASPEED_CLK_MPLL: 534bbbfb0c5Sryan_chen rate = ast2600_get_pll_rate(priv->scu, clk->id); 535d812df15Sryan_chen break; 536feb42054Sryan_chen case ASPEED_CLK_AHB: 537feb42054Sryan_chen rate = ast2600_get_hclk(priv->scu); 538feb42054Sryan_chen break; 5396fa1ef3dSryan_chen case ASPEED_CLK_APB1: 5406fa1ef3dSryan_chen rate = ast2600_get_pclk1(priv->scu); 5416fa1ef3dSryan_chen break; 5426fa1ef3dSryan_chen case ASPEED_CLK_APB2: 5436fa1ef3dSryan_chen rate = ast2600_get_pclk2(priv->scu); 544feb42054Sryan_chen break; 545bbbfb0c5Sryan_chen case ASPEED_CLK_APLL: 546bbbfb0c5Sryan_chen rate = ast2600_get_apll_rate(priv->scu); 547bbbfb0c5Sryan_chen break; 548feb42054Sryan_chen case ASPEED_CLK_GATE_UART1CLK: 549feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 1); 550feb42054Sryan_chen break; 551feb42054Sryan_chen case ASPEED_CLK_GATE_UART2CLK: 552feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 2); 553feb42054Sryan_chen break; 554feb42054Sryan_chen case ASPEED_CLK_GATE_UART3CLK: 555feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 3); 556feb42054Sryan_chen break; 557feb42054Sryan_chen case ASPEED_CLK_GATE_UART4CLK: 558feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 4); 559feb42054Sryan_chen break; 560feb42054Sryan_chen case ASPEED_CLK_GATE_UART5CLK: 561feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 5); 562feb42054Sryan_chen break; 563c304f173Sryan_chen case ASPEED_CLK_BCLK: 564c304f173Sryan_chen rate = ast2600_get_bclk_rate(priv->scu); 565c304f173Sryan_chen break; 566f51926eeSryan_chen case ASPEED_CLK_SDIO: 567f51926eeSryan_chen rate = ast2600_get_sdio_clk_rate(priv->scu); 568f51926eeSryan_chen break; 569f51926eeSryan_chen case ASPEED_CLK_EMMC: 570f51926eeSryan_chen rate = ast2600_get_emmc_clk_rate(priv->scu); 571f51926eeSryan_chen break; 5722e195992Sryan_chen case ASPEED_CLK_UARTX: 5732e195992Sryan_chen rate = ast2600_get_uart_uxclk_rate(priv->scu); 5742e195992Sryan_chen break; 5750998ddefSryan_chen case ASPEED_CLK_HUARTX: 5762e195992Sryan_chen rate = ast2600_get_uart_huxclk_rate(priv->scu); 5772e195992Sryan_chen break; 578feb42054Sryan_chen default: 579d812df15Sryan_chen pr_debug("can't get clk rate\n"); 580feb42054Sryan_chen return -ENOENT; 581feb42054Sryan_chen } 582feb42054Sryan_chen 583feb42054Sryan_chen return rate; 584feb42054Sryan_chen } 585feb42054Sryan_chen 586577fcdaeSDylan Hung /** 587577fcdaeSDylan Hung * @brief lookup PLL divider config by input/output rate 588577fcdaeSDylan Hung * @param[in] *pll - PLL descriptor 589577fcdaeSDylan Hung * @return true - if PLL divider config is found, false - else 590a8fc7648SRyan Chen * The function caller shall fill "pll->in" and "pll->out", 591a8fc7648SRyan Chen * then this function will search the lookup table 592a8fc7648SRyan Chen * to find a valid PLL divider configuration. 593550e691bSryan_chen */ 594577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll) 595550e691bSryan_chen { 596577fcdaeSDylan Hung u32 i; 597577fcdaeSDylan Hung bool is_found = false; 598550e691bSryan_chen 599577fcdaeSDylan Hung for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) { 600577fcdaeSDylan Hung const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i]; 601ed3899c5SRyan Chen 602ed3899c5SRyan Chen if (def_cfg->in == pll->in && def_cfg->out == pll->out) { 603577fcdaeSDylan Hung is_found = true; 604577fcdaeSDylan Hung pll->cfg.reg.w = def_cfg->cfg.reg.w; 605577fcdaeSDylan Hung pll->cfg.ext_reg = def_cfg->cfg.ext_reg; 606577fcdaeSDylan Hung break; 607550e691bSryan_chen } 608550e691bSryan_chen } 609577fcdaeSDylan Hung return is_found; 610550e691bSryan_chen } 611ed3899c5SRyan Chen 612fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu, 613fd52be0bSDylan Hung struct ast2600_pll_cfg *p_cfg, int pll_idx) 614fd52be0bSDylan Hung { 615fd52be0bSDylan Hung u32 addr, addr_ext; 616fd52be0bSDylan Hung u32 reg; 617550e691bSryan_chen 618fd52be0bSDylan Hung switch (pll_idx) { 619fd52be0bSDylan Hung case ASPEED_CLK_HPLL: 620fd52be0bSDylan Hung addr = (u32)(&scu->h_pll_param); 621fd52be0bSDylan Hung addr_ext = (u32)(&scu->h_pll_ext_param); 622fd52be0bSDylan Hung break; 623fd52be0bSDylan Hung case ASPEED_CLK_MPLL: 624fd52be0bSDylan Hung addr = (u32)(&scu->m_pll_param); 625fd52be0bSDylan Hung addr_ext = (u32)(&scu->m_pll_ext_param); 626fd52be0bSDylan Hung break; 627fd52be0bSDylan Hung case ASPEED_CLK_DPLL: 628fd52be0bSDylan Hung addr = (u32)(&scu->d_pll_param); 629fd52be0bSDylan Hung addr_ext = (u32)(&scu->d_pll_ext_param); 630fd52be0bSDylan Hung break; 631fd52be0bSDylan Hung case ASPEED_CLK_EPLL: 632fd52be0bSDylan Hung addr = (u32)(&scu->e_pll_param); 633fd52be0bSDylan Hung addr_ext = (u32)(&scu->e_pll_ext_param); 634fd52be0bSDylan Hung break; 635fd52be0bSDylan Hung default: 636fd52be0bSDylan Hung debug("unknown PLL index\n"); 637fd52be0bSDylan Hung return 1; 638fd52be0bSDylan Hung } 639fd52be0bSDylan Hung 640fd52be0bSDylan Hung p_cfg->reg.b.bypass = 0; 641fd52be0bSDylan Hung p_cfg->reg.b.off = 1; 642fd52be0bSDylan Hung p_cfg->reg.b.reset = 1; 643fd52be0bSDylan Hung 644fd52be0bSDylan Hung reg = readl(addr); 645fd52be0bSDylan Hung reg &= ~GENMASK(25, 0); 646fd52be0bSDylan Hung reg |= p_cfg->reg.w; 647fd52be0bSDylan Hung writel(reg, addr); 648fd52be0bSDylan Hung 649fd52be0bSDylan Hung /* write extend parameter */ 650fd52be0bSDylan Hung writel(p_cfg->ext_reg, addr_ext); 651fd52be0bSDylan Hung udelay(100); 652fd52be0bSDylan Hung p_cfg->reg.b.off = 0; 653fd52be0bSDylan Hung p_cfg->reg.b.reset = 0; 654fd52be0bSDylan Hung reg &= ~GENMASK(25, 0); 655fd52be0bSDylan Hung reg |= p_cfg->reg.w; 656fd52be0bSDylan Hung writel(reg, addr); 657ed3899c5SRyan Chen while (!(readl(addr_ext) & BIT(31))) 658ed3899c5SRyan Chen ; 659fd52be0bSDylan Hung 660fd52be0bSDylan Hung return 0; 661fd52be0bSDylan Hung } 662ed3899c5SRyan Chen 663feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate) 664550e691bSryan_chen { 665577fcdaeSDylan Hung struct ast2600_pll_desc mpll; 666550e691bSryan_chen 667577fcdaeSDylan Hung mpll.in = AST2600_CLK_IN; 668577fcdaeSDylan Hung mpll.out = rate; 669f27685ebSRyan Chen if (ast2600_search_clock_config(&mpll) == false) { 670577fcdaeSDylan Hung printf("error!! unable to find valid DDR clock setting\n"); 671577fcdaeSDylan Hung return 0; 672577fcdaeSDylan Hung } 673ed3899c5SRyan Chen ast2600_configure_pll(scu, &mpll.cfg, ASPEED_CLK_MPLL); 674577fcdaeSDylan Hung 675cc476ffcSDylan Hung return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL); 676d6e349c7Sryan_chen } 677d6e349c7Sryan_chen 678d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate) 679550e691bSryan_chen { 680f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 681550e691bSryan_chen ulong new_rate; 682ed3899c5SRyan Chen 683550e691bSryan_chen switch (clk->id) { 684f0d895afSryan_chen case ASPEED_CLK_MPLL: 685feb42054Sryan_chen new_rate = ast2600_configure_ddr(priv->scu, rate); 686550e691bSryan_chen break; 687550e691bSryan_chen default: 688550e691bSryan_chen return -ENOENT; 689550e691bSryan_chen } 690550e691bSryan_chen 691550e691bSryan_chen return new_rate; 692550e691bSryan_chen } 693feb42054Sryan_chen 694f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1 (20) 695f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2 (21) 696f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3 (20) 697f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4 (21) 698f9aa0ee1Sryan_chen 699a98c71fbSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu, struct udevice *dev) 700cc476ffcSDylan Hung { 701a98c71fbSDylan Hung union mac_delay_1g reg_1g; 702a98c71fbSDylan Hung union mac_delay_100_10 reg_100, reg_10; 703a98c71fbSDylan Hung struct mac_delay_config mac1_cfg, mac2_cfg; 704a98c71fbSDylan Hung int ret; 7054760b3f8SDylan Hung 706a98c71fbSDylan Hung reg_1g.w = (readl(&scu->mac12_clk_delay) & ~GENMASK(25, 0)) | 707a98c71fbSDylan Hung MAC_DEF_DELAY_1G; 708a98c71fbSDylan Hung reg_100.w = MAC_DEF_DELAY_100M; 709a98c71fbSDylan Hung reg_10.w = MAC_DEF_DELAY_10M; 710a98c71fbSDylan Hung 711a98c71fbSDylan Hung ret = dev_read_u32_array(dev, "mac0-clk-delay", (u32 *)&mac1_cfg, sizeof(mac1_cfg) / sizeof(u32)); 712a98c71fbSDylan Hung if (!ret) { 713a98c71fbSDylan Hung reg_1g.b.tx_delay_1 = mac1_cfg.tx_delay_1000; 714a98c71fbSDylan Hung reg_1g.b.rx_delay_1 = mac1_cfg.rx_delay_1000; 715a98c71fbSDylan Hung reg_100.b.tx_delay_1 = mac1_cfg.tx_delay_100; 716a98c71fbSDylan Hung reg_100.b.rx_delay_1 = mac1_cfg.rx_delay_100; 717a98c71fbSDylan Hung reg_10.b.tx_delay_1 = mac1_cfg.tx_delay_10; 718a98c71fbSDylan Hung reg_10.b.rx_delay_1 = mac1_cfg.rx_delay_10; 719a98c71fbSDylan Hung } 720a98c71fbSDylan Hung 721a98c71fbSDylan Hung ret = dev_read_u32_array(dev, "mac1-clk-delay", (u32 *)&mac2_cfg, sizeof(mac2_cfg) / sizeof(u32)); 722a98c71fbSDylan Hung if (!ret) { 723a98c71fbSDylan Hung reg_1g.b.tx_delay_2 = mac2_cfg.tx_delay_1000; 724a98c71fbSDylan Hung reg_1g.b.rx_delay_2 = mac2_cfg.rx_delay_1000; 725a98c71fbSDylan Hung reg_100.b.tx_delay_2 = mac2_cfg.tx_delay_100; 726a98c71fbSDylan Hung reg_100.b.rx_delay_2 = mac2_cfg.rx_delay_100; 727a98c71fbSDylan Hung reg_10.b.tx_delay_2 = mac2_cfg.tx_delay_10; 728a98c71fbSDylan Hung reg_10.b.rx_delay_2 = mac2_cfg.rx_delay_10; 729a98c71fbSDylan Hung } 730a98c71fbSDylan Hung 731a98c71fbSDylan Hung writel(reg_1g.w, &scu->mac12_clk_delay); 732a98c71fbSDylan Hung writel(reg_100.w, &scu->mac12_clk_delay_100M); 733a98c71fbSDylan Hung writel(reg_10.w, &scu->mac12_clk_delay_10M); 734cc476ffcSDylan Hung 735ed30249cSDylan Hung /* MAC AHB = HPLL / 6 */ 736eff28274SJohnny Huang clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16)); 737894c19cfSDylan Hung 738cc476ffcSDylan Hung return 0; 739cc476ffcSDylan Hung } 740cc476ffcSDylan Hung 741a98c71fbSDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu, struct udevice *dev) 74254f9cba1SDylan Hung { 743a98c71fbSDylan Hung union mac_delay_1g reg_1g; 744a98c71fbSDylan Hung union mac_delay_100_10 reg_100, reg_10; 745a98c71fbSDylan Hung struct mac_delay_config mac3_cfg, mac4_cfg; 746a98c71fbSDylan Hung int ret; 747a98c71fbSDylan Hung 74854f9cba1SDylan Hung /* 749eff28274SJohnny Huang * scu350[31] RGMII 125M source: 0 = from IO pin 750eff28274SJohnny Huang * scu350[25:0] MAC 1G delay 75154f9cba1SDylan Hung */ 752a98c71fbSDylan Hung reg_1g.w = (readl(&scu->mac34_clk_delay) & ~GENMASK(25, 0)) | 753a98c71fbSDylan Hung MAC34_DEF_DELAY_1G; 754a98c71fbSDylan Hung reg_1g.b.rgmii_125m_o_sel = 0; 755a98c71fbSDylan Hung reg_100.w = MAC34_DEF_DELAY_100M; 756a98c71fbSDylan Hung reg_10.w = MAC34_DEF_DELAY_10M; 757a98c71fbSDylan Hung 758a98c71fbSDylan Hung ret = dev_read_u32_array(dev, "mac2-clk-delay", (u32 *)&mac3_cfg, sizeof(mac3_cfg) / sizeof(u32)); 759a98c71fbSDylan Hung if (!ret) { 760a98c71fbSDylan Hung reg_1g.b.tx_delay_1 = mac3_cfg.tx_delay_1000; 761a98c71fbSDylan Hung reg_1g.b.rx_delay_1 = mac3_cfg.rx_delay_1000; 762a98c71fbSDylan Hung reg_100.b.tx_delay_1 = mac3_cfg.tx_delay_100; 763a98c71fbSDylan Hung reg_100.b.rx_delay_1 = mac3_cfg.rx_delay_100; 764a98c71fbSDylan Hung reg_10.b.tx_delay_1 = mac3_cfg.tx_delay_10; 765a98c71fbSDylan Hung reg_10.b.rx_delay_1 = mac3_cfg.rx_delay_10; 766a98c71fbSDylan Hung } 767a98c71fbSDylan Hung 768a98c71fbSDylan Hung ret = dev_read_u32_array(dev, "mac3-clk-delay", (u32 *)&mac4_cfg, sizeof(mac4_cfg) / sizeof(u32)); 769a98c71fbSDylan Hung if (!ret) { 770a98c71fbSDylan Hung reg_1g.b.tx_delay_2 = mac4_cfg.tx_delay_1000; 771a98c71fbSDylan Hung reg_1g.b.rx_delay_2 = mac4_cfg.rx_delay_1000; 772a98c71fbSDylan Hung reg_100.b.tx_delay_2 = mac4_cfg.tx_delay_100; 773a98c71fbSDylan Hung reg_100.b.rx_delay_2 = mac4_cfg.rx_delay_100; 774a98c71fbSDylan Hung reg_10.b.tx_delay_2 = mac4_cfg.tx_delay_10; 775a98c71fbSDylan Hung reg_10.b.rx_delay_2 = mac4_cfg.rx_delay_10; 776a98c71fbSDylan Hung } 777a98c71fbSDylan Hung 778a98c71fbSDylan Hung writel(reg_1g.w, &scu->mac34_clk_delay); 779a98c71fbSDylan Hung writel(reg_100.w, &scu->mac34_clk_delay_100M); 780a98c71fbSDylan Hung writel(reg_10.w, &scu->mac34_clk_delay_10M); 78154f9cba1SDylan Hung 782eff28274SJohnny Huang /* 783eff28274SJohnny Huang * clock source seletion and divider 784eff28274SJohnny Huang * scu310[26:24] : MAC AHB bus clock = HCLK / 2 785eff28274SJohnny Huang * scu310[18:16] : RMII 50M = HCLK_200M / 4 786eff28274SJohnny Huang */ 787eff28274SJohnny Huang clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)), 788eff28274SJohnny Huang ((0x0 << 24) | (0x3 << 16))); 78954f9cba1SDylan Hung 790eff28274SJohnny Huang /* 791eff28274SJohnny Huang * set driving strength 792eff28274SJohnny Huang * scu458[3:2] : MAC4 driving strength 793eff28274SJohnny Huang * scu458[1:0] : MAC3 driving strength 794eff28274SJohnny Huang */ 795eff28274SJohnny Huang clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0), 796a961159eSDylan Hung (0x3 << 2) | (0x3 << 0)); 79754f9cba1SDylan Hung 79854f9cba1SDylan Hung return 0; 79954f9cba1SDylan Hung } 800eff28274SJohnny Huang 80154f9cba1SDylan Hung /** 8025b5c3d44SDylan Hung * ast2600 RGMII clock source tree 80354f9cba1SDylan Hung * 125M from external PAD -------->|\ 80454f9cba1SDylan Hung * HPLL -->|\ | |---->RGMII 125M for MAC#1 & MAC#2 80554f9cba1SDylan Hung * | |---->| divider |---->|/ + 80654f9cba1SDylan Hung * EPLL -->|/ | 80754f9cba1SDylan Hung * | 808eff28274SJohnny Huang * +---------<-----------|RGMIICK PAD output enable|<-------------+ 80954f9cba1SDylan Hung * | 810eff28274SJohnny Huang * +--------------------------->|\ 81154f9cba1SDylan Hung * | |----> RGMII 125M for MAC#3 & MAC#4 812eff28274SJohnny Huang * HCLK 200M ---->|divider|---->|/ 813eff28274SJohnny Huang * To simplify the control flow: 814eff28274SJohnny Huang * 1. RGMII 1/2 always use EPLL as the internal clock source 815eff28274SJohnny Huang * 2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source 816eff28274SJohnny Huang * 125M from external PAD -------->|\ 817eff28274SJohnny Huang * | |---->RGMII 125M for MAC#1 & MAC#2 818eff28274SJohnny Huang * EPLL---->| divider |--->|/ + 819eff28274SJohnny Huang * | 820eff28274SJohnny Huang * +<--------------------|RGMIICK PAD output enable|<-------------+ 821eff28274SJohnny Huang * | 822eff28274SJohnny Huang * +--------------------------->RGMII 125M for MAC#3 & MAC#4 823eff28274SJohnny Huang */ 824eff28274SJohnny Huang #define RGMIICK_SRC_PAD 0 825eff28274SJohnny Huang #define RGMIICK_SRC_EPLL 1 /* recommended */ 826eff28274SJohnny Huang #define RGMIICK_SRC_HPLL 2 827eff28274SJohnny Huang 828eff28274SJohnny Huang #define RGMIICK_DIV2 1 829eff28274SJohnny Huang #define RGMIICK_DIV3 2 830eff28274SJohnny Huang #define RGMIICK_DIV4 3 831eff28274SJohnny Huang #define RGMIICK_DIV5 4 832eff28274SJohnny Huang #define RGMIICK_DIV6 5 833eff28274SJohnny Huang #define RGMIICK_DIV7 6 834eff28274SJohnny Huang #define RGMIICK_DIV8 7 /* recommended */ 835eff28274SJohnny Huang 836eff28274SJohnny Huang #define RMIICK_DIV4 0 837eff28274SJohnny Huang #define RMIICK_DIV8 1 838eff28274SJohnny Huang #define RMIICK_DIV12 2 839eff28274SJohnny Huang #define RMIICK_DIV16 3 840eff28274SJohnny Huang #define RMIICK_DIV20 4 /* recommended */ 841eff28274SJohnny Huang #define RMIICK_DIV24 5 842eff28274SJohnny Huang #define RMIICK_DIV28 6 843eff28274SJohnny Huang #define RMIICK_DIV32 7 844eff28274SJohnny Huang 845eff28274SJohnny Huang struct ast2600_mac_clk_div { 846eff28274SJohnny Huang u32 src; /* 0=external PAD, 1=internal PLL */ 847eff28274SJohnny Huang u32 fin; /* divider input speed */ 848eff28274SJohnny Huang u32 n; /* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */ 849eff28274SJohnny Huang u32 fout; /* fout = fin / n */ 850eff28274SJohnny Huang }; 851eff28274SJohnny Huang 852eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = { 853eff28274SJohnny Huang .src = ASPEED_CLK_EPLL, 854eff28274SJohnny Huang .fin = 1000000000, 855eff28274SJohnny Huang .n = RGMIICK_DIV8, 856eff28274SJohnny Huang .fout = 125000000, 857eff28274SJohnny Huang }; 858eff28274SJohnny Huang 859eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = { 860eff28274SJohnny Huang .src = ASPEED_CLK_EPLL, 861eff28274SJohnny Huang .fin = 1000000000, 862eff28274SJohnny Huang .n = RMIICK_DIV20, 863eff28274SJohnny Huang .fout = 50000000, 864eff28274SJohnny Huang }; 865ed3899c5SRyan Chen 866eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu, 867eff28274SJohnny Huang struct ast2600_mac_clk_div *p_cfg) 868eff28274SJohnny Huang { 869eff28274SJohnny Huang struct ast2600_pll_desc pll; 870eff28274SJohnny Huang 871eff28274SJohnny Huang pll.in = AST2600_CLK_IN; 872eff28274SJohnny Huang pll.out = p_cfg->fin; 873ed3899c5SRyan Chen if (ast2600_search_clock_config(&pll) == false) { 874ed3899c5SRyan Chen pr_err("unable to find valid ETHNET MAC clock setting\n"); 8753f295164SRyan Chen debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w, 8763f295164SRyan Chen pll.cfg.ext_reg); 8773f295164SRyan Chen debug("%s: pll cfg = %02x %02x %02x\n", __func__, 8783f295164SRyan Chen pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p); 879eff28274SJohnny Huang return; 880eff28274SJohnny Huang } 881ed3899c5SRyan Chen ast2600_configure_pll(p_scu, &pll.cfg, p_cfg->src); 882eff28274SJohnny Huang } 883eff28274SJohnny Huang 884eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu, 885eff28274SJohnny Huang struct ast2600_mac_clk_div *p_cfg) 886eff28274SJohnny Huang { 887eff28274SJohnny Huang u32 reg_304 = readl(&p_scu->clk_sel2); 888eff28274SJohnny Huang u32 reg_340 = readl(&p_scu->mac12_clk_delay); 889eff28274SJohnny Huang u32 reg_350 = readl(&p_scu->mac34_clk_delay); 890eff28274SJohnny Huang 891eff28274SJohnny Huang reg_340 &= ~GENMASK(31, 29); 892eff28274SJohnny Huang /* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */ 893eff28274SJohnny Huang reg_340 |= BIT(28); 8943f295164SRyan Chen if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) { 895eff28274SJohnny Huang /* 896eff28274SJohnny Huang * re-init PLL if the current PLL output frequency doesn't match 897eff28274SJohnny Huang * the divider setting 898eff28274SJohnny Huang */ 899ed3899c5SRyan Chen if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) 900eff28274SJohnny Huang ast2600_init_mac_pll(p_scu, p_cfg); 901eff28274SJohnny Huang /* scu340[31]: select RGMII 125M from internal source */ 902eff28274SJohnny Huang reg_340 |= BIT(31); 903eff28274SJohnny Huang } 904eff28274SJohnny Huang 905eff28274SJohnny Huang reg_304 &= ~GENMASK(23, 20); 906eff28274SJohnny Huang 907eff28274SJohnny Huang /* set clock divider */ 908eff28274SJohnny Huang reg_304 |= (p_cfg->n & 0x7) << 20; 909eff28274SJohnny Huang 910eff28274SJohnny Huang /* select internal clock source */ 911ed3899c5SRyan Chen if (p_cfg->src == ASPEED_CLK_HPLL) 912eff28274SJohnny Huang reg_304 |= BIT(23); 913eff28274SJohnny Huang 914eff28274SJohnny Huang /* RGMII 3/4 clock source select */ 915eff28274SJohnny Huang reg_350 &= ~BIT(31); 916eff28274SJohnny Huang 917eff28274SJohnny Huang writel(reg_304, &p_scu->clk_sel2); 918eff28274SJohnny Huang writel(reg_340, &p_scu->mac12_clk_delay); 919eff28274SJohnny Huang writel(reg_350, &p_scu->mac34_clk_delay); 920eff28274SJohnny Huang } 921eff28274SJohnny Huang 922eff28274SJohnny Huang /** 9235b5c3d44SDylan Hung * ast2600 RMII/NCSI clock source tree 9245b5c3d44SDylan Hung * HPLL -->|\ 9255b5c3d44SDylan Hung * | |---->| divider |----> RMII 50M for MAC#1 & MAC#2 9265b5c3d44SDylan Hung * EPLL -->|/ 9275b5c3d44SDylan Hung * HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4 92854f9cba1SDylan Hung */ 929eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu, 930eff28274SJohnny Huang struct ast2600_mac_clk_div *p_cfg) 93154f9cba1SDylan Hung { 932eff28274SJohnny Huang u32 reg_304; 933eff28274SJohnny Huang u32 reg_310; 934eff28274SJohnny Huang 9353f295164SRyan Chen if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) { 936eff28274SJohnny Huang /* 937eff28274SJohnny Huang * re-init PLL if the current PLL output frequency doesn't match 938eff28274SJohnny Huang * the divider setting 939eff28274SJohnny Huang */ 940ed3899c5SRyan Chen if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) 941eff28274SJohnny Huang ast2600_init_mac_pll(p_scu, p_cfg); 942eff28274SJohnny Huang } 94354f9cba1SDylan Hung 944eff28274SJohnny Huang reg_304 = readl(&p_scu->clk_sel2); 945eff28274SJohnny Huang reg_310 = readl(&p_scu->clk_sel4); 946eff28274SJohnny Huang 947eff28274SJohnny Huang reg_304 &= ~GENMASK(19, 16); 948eff28274SJohnny Huang 949eff28274SJohnny Huang /* set RMII 1/2 clock divider */ 950eff28274SJohnny Huang reg_304 |= (p_cfg->n & 0x7) << 16; 951eff28274SJohnny Huang 952eff28274SJohnny Huang /* RMII clock source selection */ 953ed3899c5SRyan Chen if (p_cfg->src == ASPEED_CLK_HPLL) 954eff28274SJohnny Huang reg_304 |= BIT(19); 955eff28274SJohnny Huang 956eff28274SJohnny Huang /* set RMII 3/4 clock divider */ 957eff28274SJohnny Huang reg_310 &= ~GENMASK(18, 16); 958eff28274SJohnny Huang reg_310 |= (0x3 << 16); 959eff28274SJohnny Huang 960eff28274SJohnny Huang writel(reg_304, &p_scu->clk_sel2); 961eff28274SJohnny Huang writel(reg_310, &p_scu->clk_sel4); 962eff28274SJohnny Huang } 963eff28274SJohnny Huang 964f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index) 965f9aa0ee1Sryan_chen { 966f9aa0ee1Sryan_chen u32 reset_bit; 967f9aa0ee1Sryan_chen u32 clkstop_bit; 968f9aa0ee1Sryan_chen 969f9aa0ee1Sryan_chen switch (index) { 970f9aa0ee1Sryan_chen case 1: 971f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC1); 972f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC1); 973f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 974f9aa0ee1Sryan_chen udelay(100); 975f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 976f9aa0ee1Sryan_chen mdelay(10); 977f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 978f9aa0ee1Sryan_chen break; 979f9aa0ee1Sryan_chen case 2: 980f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC2); 981f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC2); 982f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 983f9aa0ee1Sryan_chen udelay(100); 984f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 985f9aa0ee1Sryan_chen mdelay(10); 986f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 987f9aa0ee1Sryan_chen break; 988f9aa0ee1Sryan_chen case 3: 989f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC3 - 32); 990f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC3); 991f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 992f9aa0ee1Sryan_chen udelay(100); 993f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 994f9aa0ee1Sryan_chen mdelay(10); 995f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 996f9aa0ee1Sryan_chen break; 997f9aa0ee1Sryan_chen case 4: 998f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC4 - 32); 999f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC4); 1000f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 1001f9aa0ee1Sryan_chen udelay(100); 1002f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 1003f9aa0ee1Sryan_chen mdelay(10); 1004f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 1005f9aa0ee1Sryan_chen break; 1006f9aa0ee1Sryan_chen default: 1007f9aa0ee1Sryan_chen return -EINVAL; 1008f9aa0ee1Sryan_chen } 1009f9aa0ee1Sryan_chen 1010f9aa0ee1Sryan_chen return 0; 1011f9aa0ee1Sryan_chen } 1012550e691bSryan_chen 1013a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_FROM_HPLL_CLK BIT(19) 1014a8fc7648SRyan Chen #define SCU_CLK_ECC_RSA_CLK_MASK GENMASK(27, 26) 1015ed3899c5SRyan Chen #define SCU_CLK_ECC_RSA_CLK_DIV(x) ((x) << 26) 1016a8fc7648SRyan Chen static void ast2600_configure_rsa_ecc_clk(struct ast2600_scu *scu) 1017a8fc7648SRyan Chen { 1018a8fc7648SRyan Chen u32 clk_sel = readl(&scu->clk_sel1); 1019a8fc7648SRyan Chen 10207e3c964cSJohnny Huang /* Configure RSA clock = HPLL/4 */ 1021a8fc7648SRyan Chen clk_sel |= SCU_CLK_ECC_RSA_FROM_HPLL_CLK; 1022a8fc7648SRyan Chen clk_sel &= ~SCU_CLK_ECC_RSA_CLK_MASK; 10237e3c964cSJohnny Huang clk_sel |= SCU_CLK_ECC_RSA_CLK_DIV(3); 1024a8fc7648SRyan Chen 1025a8fc7648SRyan Chen writel(clk_sel, &scu->clk_sel1); 1026a8fc7648SRyan Chen } 1027a8fc7648SRyan Chen 1028f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4 1029f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu) 1030f51926eeSryan_chen { 1031f51926eeSryan_chen u32 reset_bit; 1032f51926eeSryan_chen u32 clkstop_bit; 1033f51926eeSryan_chen 1034f51926eeSryan_chen reset_bit = BIT(ASPEED_RESET_SD - 32); 1035f51926eeSryan_chen clkstop_bit = BIT(SCU_CLKSTOP_SDIO); 1036f51926eeSryan_chen 1037fc9f12e6Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 1038fc9f12e6Sryan_chen 1039f51926eeSryan_chen udelay(100); 1040f51926eeSryan_chen //enable clk 1041f51926eeSryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 1042f51926eeSryan_chen mdelay(10); 1043fc9f12e6Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 1044f51926eeSryan_chen 1045f51926eeSryan_chen return 0; 1046f51926eeSryan_chen } 1047f51926eeSryan_chen 1048f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31 1049f51926eeSryan_chen #define SCU_CLK_SD_MASK (0x7 << 28) 1050ed3899c5SRyan Chen #define SCU_CLK_SD_DIV(x) ((x) << 28) 10512cd7cba2Sryan_chen #define SCU_CLK_SD_FROM_APLL_CLK BIT(8) 1052f51926eeSryan_chen 1053f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu) 1054f51926eeSryan_chen { 1055f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel4); 1056f51926eeSryan_chen u32 enableclk_bit; 10572cd7cba2Sryan_chen u32 rate = 0; 10582cd7cba2Sryan_chen u32 div = 0; 10592cd7cba2Sryan_chen int i = 0; 1060f51926eeSryan_chen 1061f51926eeSryan_chen enableclk_bit = BIT(SCU_CLKSTOP_EXTSD); 1062f51926eeSryan_chen 1063a8fc7648SRyan Chen /* ast2600 sd controller max clk is 200Mhz : 1064a8fc7648SRyan Chen * use apll for clock source 800/4 = 200 : controller max is 200mhz 1065a8fc7648SRyan Chen */ 10662cd7cba2Sryan_chen rate = ast2600_get_apll_rate(scu); 10672cd7cba2Sryan_chen for (i = 0; i < 8; i++) { 10682cd7cba2Sryan_chen div = (i + 1) * 2; 10692cd7cba2Sryan_chen if ((rate / div) <= 200000000) 10702cd7cba2Sryan_chen break; 10712cd7cba2Sryan_chen } 1072f51926eeSryan_chen clk_sel &= ~SCU_CLK_SD_MASK; 10732cd7cba2Sryan_chen clk_sel |= SCU_CLK_SD_DIV(i) | SCU_CLK_SD_FROM_APLL_CLK; 1074f51926eeSryan_chen writel(clk_sel, &scu->clk_sel4); 1075f51926eeSryan_chen 1076f51926eeSryan_chen //enable clk 1077f51926eeSryan_chen setbits_le32(&scu->clk_sel4, enableclk_bit); 1078f51926eeSryan_chen 1079f51926eeSryan_chen return 0; 1080f51926eeSryan_chen } 1081f51926eeSryan_chen 1082f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27 1083f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu) 1084f51926eeSryan_chen { 1085f51926eeSryan_chen u32 reset_bit; 1086f51926eeSryan_chen u32 clkstop_bit; 1087f51926eeSryan_chen 1088f51926eeSryan_chen reset_bit = BIT(ASPEED_RESET_EMMC); 1089f51926eeSryan_chen clkstop_bit = BIT(SCU_CLKSTOP_EMMC); 1090f51926eeSryan_chen 1091fc9f12e6Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 1092f51926eeSryan_chen udelay(100); 1093f51926eeSryan_chen //enable clk 1094f51926eeSryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1095f51926eeSryan_chen mdelay(10); 1096fc9f12e6Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 1097f51926eeSryan_chen 1098f51926eeSryan_chen return 0; 1099f51926eeSryan_chen } 1100f51926eeSryan_chen 1101f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15 1102f51926eeSryan_chen #define SCU_CLK_EMMC_MASK (0x7 << 12) 1103ed3899c5SRyan Chen #define SCU_CLK_EMMC_DIV(x) ((x) << 12) 1104a8fc7648SRyan Chen #define SCU_CLK_EMMC_FROM_MPLL_CLK BIT(11) 1105f51926eeSryan_chen 1106f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu) 1107f51926eeSryan_chen { 110885d48d8cSryan_chen u32 revision_id = readl(&scu->chip_id1); 1109f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel1); 1110ed3899c5SRyan Chen u32 enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC); 1111f4c4ddb1Sryan_chen u32 rate = 0; 1112f4c4ddb1Sryan_chen u32 div = 0; 1113f4c4ddb1Sryan_chen int i = 0; 1114f51926eeSryan_chen 1115ed3899c5SRyan Chen /* 1116ed3899c5SRyan Chen * ast2600 eMMC controller max clk is 200Mhz 1117ed3899c5SRyan Chen * HPll->1/2->|\ 1118ed3899c5SRyan Chen * |->SCU300[11]->SCU300[14:12][1/N] + 1119ed3899c5SRyan Chen * MPLL------>|/ | 1120ed3899c5SRyan Chen * +----------------------------------------------+ 1121ed3899c5SRyan Chen * | 1122ed3899c5SRyan Chen * +---------> EMMC12C[15:8][1/N]-> eMMC clk 1123a8fc7648SRyan Chen */ 112485d48d8cSryan_chen if (((revision_id & CHIP_REVISION_ID) >> 16)) { 11258c32294fSryan_chen //AST2600A1 : use mpll to be clk source 1126b0c30ea3Sryan_chen rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL); 1127b0c30ea3Sryan_chen for (i = 0; i < 8; i++) { 1128b0c30ea3Sryan_chen div = (i + 1) * 2; 1129b0c30ea3Sryan_chen if ((rate / div) <= 200000000) 1130b0c30ea3Sryan_chen break; 1131b0c30ea3Sryan_chen } 1132b0c30ea3Sryan_chen 1133b0c30ea3Sryan_chen clk_sel &= ~SCU_CLK_EMMC_MASK; 11342cd7cba2Sryan_chen clk_sel |= SCU_CLK_EMMC_DIV(i) | SCU_CLK_EMMC_FROM_MPLL_CLK; 1135b0c30ea3Sryan_chen writel(clk_sel, &scu->clk_sel1); 1136b0c30ea3Sryan_chen 1137b0c30ea3Sryan_chen } else { 11382cd7cba2Sryan_chen //AST2600A0 : use hpll to be clk source 1139f4c4ddb1Sryan_chen rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 1140f4c4ddb1Sryan_chen 1141f4c4ddb1Sryan_chen for (i = 0; i < 8; i++) { 1142f4c4ddb1Sryan_chen div = (i + 1) * 4; 1143f4c4ddb1Sryan_chen if ((rate / div) <= 200000000) 1144f4c4ddb1Sryan_chen break; 1145f4c4ddb1Sryan_chen } 1146f4c4ddb1Sryan_chen 1147f4c4ddb1Sryan_chen clk_sel &= ~SCU_CLK_EMMC_MASK; 1148f4c4ddb1Sryan_chen clk_sel |= SCU_CLK_EMMC_DIV(i); 1149f51926eeSryan_chen writel(clk_sel, &scu->clk_sel1); 1150b0c30ea3Sryan_chen } 1151f51926eeSryan_chen setbits_le32(&scu->clk_sel1, enableclk_bit); 1152f51926eeSryan_chen 1153f51926eeSryan_chen return 0; 1154f51926eeSryan_chen } 1155f51926eeSryan_chen 1156baf00c26Sryan_chen #define SCU_CLKSTOP_FSICLK 30 1157baf00c26Sryan_chen 1158baf00c26Sryan_chen static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu) 1159baf00c26Sryan_chen { 1160baf00c26Sryan_chen u32 reset_bit; 1161baf00c26Sryan_chen u32 clkstop_bit; 1162baf00c26Sryan_chen 1163baf00c26Sryan_chen reset_bit = BIT(ASPEED_RESET_FSI % 32); 1164baf00c26Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_FSICLK); 1165baf00c26Sryan_chen 1166baf00c26Sryan_chen /* The FSI clock is shared between masters. If it's already on 1167ed3899c5SRyan Chen * don't touch it, as that will reset the existing master. 1168ed3899c5SRyan Chen */ 1169baf00c26Sryan_chen if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) { 1170baf00c26Sryan_chen debug("%s: already running, not touching it\n", __func__); 1171baf00c26Sryan_chen return 0; 1172baf00c26Sryan_chen } 1173baf00c26Sryan_chen 1174baf00c26Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 1175baf00c26Sryan_chen udelay(100); 1176baf00c26Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 1177baf00c26Sryan_chen mdelay(10); 1178baf00c26Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 1179baf00c26Sryan_chen 1180baf00c26Sryan_chen return 0; 1181baf00c26Sryan_chen } 1182baf00c26Sryan_chen 1183b8ec5ceaSryan_chen static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu) 1184b8ec5ceaSryan_chen { 1185b8ec5ceaSryan_chen u32 reset_bit; 1186b8ec5ceaSryan_chen u32 clkstop_bit; 1187b8ec5ceaSryan_chen 1188b8ec5ceaSryan_chen reset_bit = BIT(ASPEED_RESET_EHCI_P1); 1189b8ec5ceaSryan_chen clkstop_bit = BIT(14); 1190b8ec5ceaSryan_chen 1191b8ec5ceaSryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 1192b8ec5ceaSryan_chen udelay(100); 1193b8ec5ceaSryan_chen writel(clkstop_bit, &scu->clk_stop_ctrl1); 1194b8ec5ceaSryan_chen mdelay(20); 1195b8ec5ceaSryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 1196b8ec5ceaSryan_chen 1197b8ec5ceaSryan_chen return 0; 1198b8ec5ceaSryan_chen } 1199b8ec5ceaSryan_chen 1200b8ec5ceaSryan_chen static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu) 1201b8ec5ceaSryan_chen { 1202b8ec5ceaSryan_chen u32 reset_bit; 1203b8ec5ceaSryan_chen u32 clkstop_bit; 1204b8ec5ceaSryan_chen 1205b8ec5ceaSryan_chen reset_bit = BIT(ASPEED_RESET_EHCI_P2); 1206b8ec5ceaSryan_chen clkstop_bit = BIT(7); 1207b8ec5ceaSryan_chen 1208b8ec5ceaSryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 1209b8ec5ceaSryan_chen udelay(100); 1210b8ec5ceaSryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1211b8ec5ceaSryan_chen mdelay(20); 1212b8ec5ceaSryan_chen 1213b8ec5ceaSryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 1214b8ec5ceaSryan_chen 1215b8ec5ceaSryan_chen return 0; 1216b8ec5ceaSryan_chen } 1217b8ec5ceaSryan_chen 1218089713adSJoel Stanley /* also known as yclk */ 1219089713adSJoel Stanley static ulong ast2600_enable_haceclk(struct ast2600_scu *scu) 1220089713adSJoel Stanley { 1221089713adSJoel Stanley u32 reset_bit; 1222089713adSJoel Stanley u32 clkstop_bit; 1223089713adSJoel Stanley 1224089713adSJoel Stanley reset_bit = BIT(ASPEED_RESET_HACE); 1225089713adSJoel Stanley clkstop_bit = BIT(13); 1226089713adSJoel Stanley 1227089713adSJoel Stanley writel(reset_bit, &scu->sysreset_ctrl1); 1228089713adSJoel Stanley udelay(100); 1229089713adSJoel Stanley writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1230089713adSJoel Stanley mdelay(20); 1231089713adSJoel Stanley 1232089713adSJoel Stanley writel(reset_bit, &scu->sysreset_clr_ctrl1); 1233089713adSJoel Stanley 1234089713adSJoel Stanley return 0; 1235089713adSJoel Stanley } 1236089713adSJoel Stanley 1237f6110ecdSChia-Wei Wang static ulong ast2600_enable_rsaeccclk(struct ast2600_scu *scu) 1238f6110ecdSChia-Wei Wang { 1239f6110ecdSChia-Wei Wang u32 clkstop_bit; 1240f6110ecdSChia-Wei Wang 1241f6110ecdSChia-Wei Wang clkstop_bit = BIT(24); 1242f6110ecdSChia-Wei Wang 1243f6110ecdSChia-Wei Wang writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1244f6110ecdSChia-Wei Wang mdelay(20); 1245f6110ecdSChia-Wei Wang 1246f6110ecdSChia-Wei Wang return 0; 1247f6110ecdSChia-Wei Wang } 1248f6110ecdSChia-Wei Wang 1249d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk) 1250550e691bSryan_chen { 1251f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 1252550e691bSryan_chen 1253550e691bSryan_chen switch (clk->id) { 125486f91560Sryan_chen case ASPEED_CLK_GATE_MAC1CLK: 125586f91560Sryan_chen ast2600_configure_mac(priv->scu, 1); 1256550e691bSryan_chen break; 125786f91560Sryan_chen case ASPEED_CLK_GATE_MAC2CLK: 125886f91560Sryan_chen ast2600_configure_mac(priv->scu, 2); 1259550e691bSryan_chen break; 126077843939Sryan_chen case ASPEED_CLK_GATE_MAC3CLK: 126177843939Sryan_chen ast2600_configure_mac(priv->scu, 3); 126277843939Sryan_chen break; 126377843939Sryan_chen case ASPEED_CLK_GATE_MAC4CLK: 126477843939Sryan_chen ast2600_configure_mac(priv->scu, 4); 126577843939Sryan_chen break; 1266f51926eeSryan_chen case ASPEED_CLK_GATE_SDCLK: 1267f51926eeSryan_chen ast2600_enable_sdclk(priv->scu); 1268f51926eeSryan_chen break; 1269f51926eeSryan_chen case ASPEED_CLK_GATE_SDEXTCLK: 1270f51926eeSryan_chen ast2600_enable_extsdclk(priv->scu); 1271f51926eeSryan_chen break; 1272f51926eeSryan_chen case ASPEED_CLK_GATE_EMMCCLK: 1273f51926eeSryan_chen ast2600_enable_emmcclk(priv->scu); 1274f51926eeSryan_chen break; 1275f51926eeSryan_chen case ASPEED_CLK_GATE_EMMCEXTCLK: 1276f51926eeSryan_chen ast2600_enable_extemmcclk(priv->scu); 1277f51926eeSryan_chen break; 1278baf00c26Sryan_chen case ASPEED_CLK_GATE_FSICLK: 1279baf00c26Sryan_chen ast2600_enable_fsiclk(priv->scu); 1280baf00c26Sryan_chen break; 1281b8ec5ceaSryan_chen case ASPEED_CLK_GATE_USBPORT1CLK: 1282b8ec5ceaSryan_chen ast2600_enable_usbahclk(priv->scu); 1283b8ec5ceaSryan_chen break; 1284b8ec5ceaSryan_chen case ASPEED_CLK_GATE_USBPORT2CLK: 1285b8ec5ceaSryan_chen ast2600_enable_usbbhclk(priv->scu); 1286b8ec5ceaSryan_chen break; 1287089713adSJoel Stanley case ASPEED_CLK_GATE_YCLK: 1288089713adSJoel Stanley ast2600_enable_haceclk(priv->scu); 1289089713adSJoel Stanley break; 1290f6110ecdSChia-Wei Wang case ASPEED_CLK_GATE_RSAECCCLK: 1291f6110ecdSChia-Wei Wang ast2600_enable_rsaeccclk(priv->scu); 1292f6110ecdSChia-Wei Wang break; 1293550e691bSryan_chen default: 1294ed3899c5SRyan Chen pr_err("can't enable clk\n"); 1295550e691bSryan_chen return -ENOENT; 1296550e691bSryan_chen } 1297550e691bSryan_chen 1298550e691bSryan_chen return 0; 1299550e691bSryan_chen } 1300550e691bSryan_chen 1301f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = { 1302d6e349c7Sryan_chen .get_rate = ast2600_clk_get_rate, 1303d6e349c7Sryan_chen .set_rate = ast2600_clk_set_rate, 1304d6e349c7Sryan_chen .enable = ast2600_clk_enable, 1305550e691bSryan_chen }; 1306550e691bSryan_chen 1307d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev) 1308550e691bSryan_chen { 1309f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(dev); 131061ab9607Sryan_chen u32 uart_clk_source; 1311550e691bSryan_chen 1312f0d895afSryan_chen priv->scu = devfdt_get_addr_ptr(dev); 1313f0d895afSryan_chen if (IS_ERR(priv->scu)) 1314f0d895afSryan_chen return PTR_ERR(priv->scu); 1315550e691bSryan_chen 13165d05f4fcSRyan Chen uart_clk_source = dev_read_u32_default(dev, "uart-clk-source", 0x0); 131761ab9607Sryan_chen 131861ab9607Sryan_chen if (uart_clk_source) { 131956dd3e85Sryan_chen if (uart_clk_source & GENMASK(5, 0)) 13205d05f4fcSRyan Chen setbits_le32(&priv->scu->clk_sel4, 13215d05f4fcSRyan Chen uart_clk_source & GENMASK(5, 0)); 132256dd3e85Sryan_chen if (uart_clk_source & GENMASK(12, 6)) 13235d05f4fcSRyan Chen setbits_le32(&priv->scu->clk_sel5, 13245d05f4fcSRyan Chen uart_clk_source & GENMASK(12, 6)); 132561ab9607Sryan_chen } 132661ab9607Sryan_chen 1327b89500a2SDylan Hung ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig); 1328b89500a2SDylan Hung ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig); 1329a98c71fbSDylan Hung ast2600_configure_mac12_clk(priv->scu, dev); 1330a98c71fbSDylan Hung ast2600_configure_mac34_clk(priv->scu, dev); 1331a8fc7648SRyan Chen ast2600_configure_rsa_ecc_clk(priv->scu); 1332fd0306aaSJohnny Huang 1333550e691bSryan_chen return 0; 1334550e691bSryan_chen } 1335550e691bSryan_chen 1336d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev) 1337550e691bSryan_chen { 1338550e691bSryan_chen int ret; 1339550e691bSryan_chen 1340550e691bSryan_chen /* The reset driver does not have a device node, so bind it here */ 1341550e691bSryan_chen ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev); 1342550e691bSryan_chen if (ret) 1343550e691bSryan_chen debug("Warning: No reset driver: ret=%d\n", ret); 1344550e691bSryan_chen 1345550e691bSryan_chen return 0; 1346550e691bSryan_chen } 1347550e691bSryan_chen 1348d35ac78cSryan_chen struct aspeed_clks { 1349d35ac78cSryan_chen ulong id; 1350d35ac78cSryan_chen const char *name; 1351d35ac78cSryan_chen }; 1352d35ac78cSryan_chen 1353d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = { 13545d05f4fcSRyan Chen { ASPEED_CLK_HPLL, "hpll" }, { ASPEED_CLK_MPLL, "mpll" }, 13555d05f4fcSRyan Chen { ASPEED_CLK_APLL, "apll" }, { ASPEED_CLK_EPLL, "epll" }, 13565d05f4fcSRyan Chen { ASPEED_CLK_DPLL, "dpll" }, { ASPEED_CLK_AHB, "hclk" }, 13575d05f4fcSRyan Chen { ASPEED_CLK_APB1, "pclk1" }, { ASPEED_CLK_APB2, "pclk2" }, 13585d05f4fcSRyan Chen { ASPEED_CLK_BCLK, "bclk" }, { ASPEED_CLK_UARTX, "uxclk" }, 1359def99fcbSryan_chen { ASPEED_CLK_HUARTX, "huxclk" }, 1360d35ac78cSryan_chen }; 1361d35ac78cSryan_chen 1362d35ac78cSryan_chen int soc_clk_dump(void) 1363d35ac78cSryan_chen { 1364d35ac78cSryan_chen struct udevice *dev; 1365d35ac78cSryan_chen struct clk clk; 1366d35ac78cSryan_chen unsigned long rate; 1367d35ac78cSryan_chen int i, ret; 1368d35ac78cSryan_chen 13695d05f4fcSRyan Chen ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu), 13705d05f4fcSRyan Chen &dev); 1371d35ac78cSryan_chen if (ret) 1372d35ac78cSryan_chen return ret; 1373d35ac78cSryan_chen 1374d35ac78cSryan_chen printf("Clk\t\tHz\n"); 1375d35ac78cSryan_chen 1376d35ac78cSryan_chen for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) { 1377d35ac78cSryan_chen clk.id = aspeed_clk_names[i].id; 1378d35ac78cSryan_chen ret = clk_request(dev, &clk); 1379d35ac78cSryan_chen if (ret < 0) { 1380d35ac78cSryan_chen debug("%s clk_request() failed: %d\n", __func__, ret); 1381d35ac78cSryan_chen continue; 1382d35ac78cSryan_chen } 1383d35ac78cSryan_chen 1384d35ac78cSryan_chen ret = clk_get_rate(&clk); 1385d35ac78cSryan_chen rate = ret; 1386d35ac78cSryan_chen 1387d35ac78cSryan_chen clk_free(&clk); 1388d35ac78cSryan_chen 1389d35ac78cSryan_chen if (ret == -ENOTSUPP) { 1390d35ac78cSryan_chen printf("clk ID %lu not supported yet\n", 1391d35ac78cSryan_chen aspeed_clk_names[i].id); 1392d35ac78cSryan_chen continue; 1393d35ac78cSryan_chen } 1394d35ac78cSryan_chen if (ret < 0) { 13955d05f4fcSRyan Chen printf("%s %lu: get_rate err: %d\n", __func__, 13965d05f4fcSRyan Chen aspeed_clk_names[i].id, ret); 1397d35ac78cSryan_chen continue; 1398d35ac78cSryan_chen } 1399d35ac78cSryan_chen 14005d05f4fcSRyan Chen printf("%s(%3lu):\t%lu\n", aspeed_clk_names[i].name, 14015d05f4fcSRyan Chen aspeed_clk_names[i].id, rate); 1402d35ac78cSryan_chen } 1403d35ac78cSryan_chen 1404d35ac78cSryan_chen return 0; 1405d35ac78cSryan_chen } 1406d35ac78cSryan_chen 1407d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = { 14085d05f4fcSRyan Chen { 14095d05f4fcSRyan Chen .compatible = "aspeed,ast2600-scu", 14105d05f4fcSRyan Chen }, 1411550e691bSryan_chen {} 1412550e691bSryan_chen }; 1413550e691bSryan_chen 1414aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = { 1415aa36597fSDylan Hung .name = "aspeed_scu", 1416550e691bSryan_chen .id = UCLASS_CLK, 1417d6e349c7Sryan_chen .of_match = ast2600_clk_ids, 1418f0d895afSryan_chen .priv_auto_alloc_size = sizeof(struct ast2600_clk_priv), 1419f9aa0ee1Sryan_chen .ops = &ast2600_clk_ops, 1420d6e349c7Sryan_chen .bind = ast2600_clk_bind, 1421d6e349c7Sryan_chen .probe = ast2600_clk_probe, 1422550e691bSryan_chen }; 1423