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 /* 16550e691bSryan_chen * MAC Clock Delay settings, taken from Aspeed SDK 17550e691bSryan_chen */ 18550e691bSryan_chen #define RGMII_TXCLK_ODLY 8 19550e691bSryan_chen #define RMII_RXCLK_IDLY 2 20550e691bSryan_chen 21ed30249cSDylan Hung #define MAC_DEF_DELAY_1G 0x00410410 2254f9cba1SDylan Hung #define MAC_DEF_DELAY_100M 0x00410410 2354f9cba1SDylan Hung #define MAC_DEF_DELAY_10M 0x00410410 2454f9cba1SDylan Hung 2554f9cba1SDylan Hung #define MAC34_DEF_DELAY_1G 0x00104208 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 35550e691bSryan_chen #define D2PLL_DEFAULT_RATE (250 * 1000 * 1000) 36550e691bSryan_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 { 52577fcdaeSDylan Hung unsigned int 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; 66577fcdaeSDylan Hung unsigned int 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[] = { 76577fcdaeSDylan Hung {.in = AST2600_CLK_IN, .out = 400000000, 77577fcdaeSDylan Hung .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 1, 78577fcdaeSDylan Hung .cfg.ext_reg = 0x31, 79577fcdaeSDylan Hung }, 80577fcdaeSDylan Hung {.in = AST2600_CLK_IN, .out = 200000000, 81577fcdaeSDylan Hung .cfg.reg.b.m = 127, .cfg.reg.b.n = 0, .cfg.reg.b.p = 15, 82577fcdaeSDylan Hung .cfg.ext_reg = 0x3f 83577fcdaeSDylan Hung }, 84577fcdaeSDylan Hung {.in = AST2600_CLK_IN, .out = 334000000, 85577fcdaeSDylan Hung .cfg.reg.b.m = 667, .cfg.reg.b.n = 4, .cfg.reg.b.p = 9, 86577fcdaeSDylan Hung .cfg.ext_reg = 0x14d 87577fcdaeSDylan Hung }, 88577fcdaeSDylan Hung 89577fcdaeSDylan Hung {.in = AST2600_CLK_IN, .out = 1000000000, 90577fcdaeSDylan Hung .cfg.reg.b.m = 119, .cfg.reg.b.n = 2, .cfg.reg.b.p = 0, 91577fcdaeSDylan Hung .cfg.ext_reg = 0x3d 92577fcdaeSDylan Hung }, 93577fcdaeSDylan Hung 94577fcdaeSDylan Hung {.in = AST2600_CLK_IN, .out = 50000000, 95577fcdaeSDylan Hung .cfg.reg.b.m = 95, .cfg.reg.b.n = 2, .cfg.reg.b.p = 15, 96577fcdaeSDylan Hung .cfg.ext_reg = 0x31 97577fcdaeSDylan Hung }, 98550e691bSryan_chen }; 99550e691bSryan_chen 100bbbfb0c5Sryan_chen extern u32 ast2600_get_pll_rate(struct ast2600_scu *scu, int pll_idx) 101550e691bSryan_chen { 102d6e349c7Sryan_chen u32 clkin = AST2600_CLK_IN; 103bbbfb0c5Sryan_chen u32 pll_reg = 0; 1049639db61Sryan_chen unsigned int mult, div = 1; 105550e691bSryan_chen 106bbbfb0c5Sryan_chen switch(pll_idx) { 107bbbfb0c5Sryan_chen case ASPEED_CLK_HPLL: 108bbbfb0c5Sryan_chen pll_reg = readl(&scu->h_pll_param); 109bbbfb0c5Sryan_chen break; 110bbbfb0c5Sryan_chen case ASPEED_CLK_MPLL: 111bbbfb0c5Sryan_chen pll_reg = readl(&scu->m_pll_param); 112bbbfb0c5Sryan_chen break; 113bbbfb0c5Sryan_chen case ASPEED_CLK_DPLL: 114bbbfb0c5Sryan_chen pll_reg = readl(&scu->d_pll_param); 115bbbfb0c5Sryan_chen break; 116bbbfb0c5Sryan_chen case ASPEED_CLK_EPLL: 117bbbfb0c5Sryan_chen pll_reg = readl(&scu->e_pll_param); 118bbbfb0c5Sryan_chen break; 119bbbfb0c5Sryan_chen 120bbbfb0c5Sryan_chen } 121bbbfb0c5Sryan_chen if (pll_reg & BIT(24)) { 1229639db61Sryan_chen /* Pass through mode */ 1239639db61Sryan_chen mult = div = 1; 1249639db61Sryan_chen } else { 1259639db61Sryan_chen /* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */ 12675ced45aSDylan Hung union ast2600_pll_reg reg; 12775ced45aSDylan Hung reg.w = pll_reg; 12875ced45aSDylan Hung mult = (reg.b.m + 1) / (reg.b.n + 1); 12975ced45aSDylan Hung div = (reg.b.p + 1); 1309639db61Sryan_chen } 1319639db61Sryan_chen return ((clkin * mult)/div); 132550e691bSryan_chen 133550e691bSryan_chen } 134550e691bSryan_chen 1354f22e838Sryan_chen extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu) 136550e691bSryan_chen { 137bbbfb0c5Sryan_chen u32 clkin = AST2600_CLK_IN; 13839283ea7Sryan_chen u32 apll_reg = readl(&scu->a_pll_param); 13939283ea7Sryan_chen unsigned int mult, div = 1; 140d6e349c7Sryan_chen 14139283ea7Sryan_chen if (apll_reg & BIT(20)) { 142d6e349c7Sryan_chen /* Pass through mode */ 143d6e349c7Sryan_chen mult = div = 1; 144d6e349c7Sryan_chen } else { 145bbbfb0c5Sryan_chen /* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */ 14639283ea7Sryan_chen u32 m = (apll_reg >> 5) & 0x3f; 14739283ea7Sryan_chen u32 od = (apll_reg >> 4) & 0x1; 14839283ea7Sryan_chen u32 n = apll_reg & 0xf; 149d6e349c7Sryan_chen 150bbbfb0c5Sryan_chen mult = (2 - od) * (m + 2); 151bbbfb0c5Sryan_chen div = n + 1; 152d6e349c7Sryan_chen } 153bbbfb0c5Sryan_chen return ((clkin * mult)/div); 15439283ea7Sryan_chen } 15539283ea7Sryan_chen 156d812df15Sryan_chen static u32 ast2600_a0_axi_ahb_div_table[] = { 157d812df15Sryan_chen 2, 2, 3, 5, 158d812df15Sryan_chen }; 159d812df15Sryan_chen 160d812df15Sryan_chen static u32 ast2600_a1_axi_ahb_div_table[] = { 161d812df15Sryan_chen 4, 6, 2, 4, 162d812df15Sryan_chen }; 163d812df15Sryan_chen 164d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu) 165d812df15Sryan_chen { 166d812df15Sryan_chen u32 hw_rev = readl(&scu->chip_id0); 167d812df15Sryan_chen u32 hwstrap1 = readl(&scu->hwstrap1); 168d812df15Sryan_chen u32 axi_div = 1; 169d812df15Sryan_chen u32 ahb_div = 0; 170d812df15Sryan_chen u32 rate = 0; 171d812df15Sryan_chen 172c29e1cc8Sryan_chen if(hwstrap1 & BIT(16)) 173d812df15Sryan_chen axi_div = 1; 174d812df15Sryan_chen else 175d812df15Sryan_chen axi_div = 2; 176d812df15Sryan_chen 177d812df15Sryan_chen if (hw_rev & BIT(16)) 178d812df15Sryan_chen ahb_div = ast2600_a1_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3]; 179d812df15Sryan_chen else 180d812df15Sryan_chen ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3]; 181d812df15Sryan_chen 182bbbfb0c5Sryan_chen rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 183d812df15Sryan_chen 1842717883aSryan_chen return (rate / axi_div / ahb_div); 1852717883aSryan_chen } 1862717883aSryan_chen 1876fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = { 1882717883aSryan_chen 4, 8, 12, 16, 20, 24, 28, 32, 1892717883aSryan_chen }; 1902717883aSryan_chen 1916fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = { 1926fa1ef3dSryan_chen 2, 4, 6, 8, 10, 12, 14, 16, 1936fa1ef3dSryan_chen }; 1946fa1ef3dSryan_chen 1956fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu) 1962717883aSryan_chen { 1972717883aSryan_chen u32 clk_sel1 = readl(&scu->clk_sel1); 1986fa1ef3dSryan_chen u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)]; 199bbbfb0c5Sryan_chen u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 2002717883aSryan_chen 2012717883aSryan_chen return (rate / apb_div); 202d812df15Sryan_chen } 203d812df15Sryan_chen 2046fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu) 2056fa1ef3dSryan_chen { 2066fa1ef3dSryan_chen u32 clk_sel4 = readl(&scu->clk_sel4); 2076fa1ef3dSryan_chen u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)]; 2086fa1ef3dSryan_chen u32 rate = ast2600_get_hclk(scu); 2096fa1ef3dSryan_chen 2106fa1ef3dSryan_chen return (rate / apb_div); 2116fa1ef3dSryan_chen } 2126fa1ef3dSryan_chen 21327881d20Sryan_chen static u32 ast2600_get_uxclk_rate(struct ast2600_scu *scu) 214d6e349c7Sryan_chen { 21527881d20Sryan_chen u32 clk_in = 0; 21627881d20Sryan_chen u32 uxclk_sel = readl(&scu->clk_sel4); 217550e691bSryan_chen 21827881d20Sryan_chen uxclk_sel &= 0x3; 21927881d20Sryan_chen switch(uxclk_sel) { 22027881d20Sryan_chen case 0: 22127881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 22227881d20Sryan_chen break; 22327881d20Sryan_chen case 1: 22427881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 22527881d20Sryan_chen break; 22627881d20Sryan_chen case 2: 22727881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 22827881d20Sryan_chen break; 22927881d20Sryan_chen case 3: 23027881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 23127881d20Sryan_chen break; 23227881d20Sryan_chen } 233d6e349c7Sryan_chen 23427881d20Sryan_chen return clk_in; 23527881d20Sryan_chen } 23627881d20Sryan_chen 23727881d20Sryan_chen static u32 ast2600_get_huxclk_rate(struct ast2600_scu *scu) 23827881d20Sryan_chen { 23927881d20Sryan_chen u32 clk_in = 0; 24027881d20Sryan_chen u32 huclk_sel = readl(&scu->clk_sel4); 24127881d20Sryan_chen 24227881d20Sryan_chen huclk_sel = ((huclk_sel >> 3) & 0x3); 24327881d20Sryan_chen switch(huclk_sel) { 24427881d20Sryan_chen case 0: 24527881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 24627881d20Sryan_chen break; 24727881d20Sryan_chen case 1: 24827881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 24927881d20Sryan_chen break; 25027881d20Sryan_chen case 2: 25127881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 25227881d20Sryan_chen break; 25327881d20Sryan_chen case 3: 25427881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 25527881d20Sryan_chen break; 25627881d20Sryan_chen } 25727881d20Sryan_chen 25827881d20Sryan_chen return clk_in; 25927881d20Sryan_chen } 26027881d20Sryan_chen 26127881d20Sryan_chen static u32 ast2600_get_uart_from_uxclk_rate(struct ast2600_scu *scu) 26227881d20Sryan_chen { 26327881d20Sryan_chen u32 clk_in = ast2600_get_uxclk_rate(scu); 26427881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_uxclk); 26527881d20Sryan_chen unsigned int mult, div; 26627881d20Sryan_chen 26727881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 26827881d20Sryan_chen u32 r = div_reg & 0xff; 26927881d20Sryan_chen 27027881d20Sryan_chen mult = r; 27127881d20Sryan_chen div = (n * 4); 27227881d20Sryan_chen return (clk_in * mult)/div; 27327881d20Sryan_chen } 27427881d20Sryan_chen 27527881d20Sryan_chen static u32 ast2600_get_uart_from_huxclk_rate(struct ast2600_scu *scu) 27627881d20Sryan_chen { 27727881d20Sryan_chen u32 clk_in = ast2600_get_huxclk_rate(scu); 27827881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_huxclk); 27927881d20Sryan_chen 28027881d20Sryan_chen unsigned int mult, div; 28127881d20Sryan_chen 28227881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 28327881d20Sryan_chen u32 r = div_reg & 0xff; 28427881d20Sryan_chen 28527881d20Sryan_chen mult = r; 28627881d20Sryan_chen div = (n * 4); 28727881d20Sryan_chen return (clk_in * mult)/div; 28827881d20Sryan_chen } 28927881d20Sryan_chen 290f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu) 291f51926eeSryan_chen { 292f51926eeSryan_chen u32 clkin = 0; 293f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel4); 294f51926eeSryan_chen u32 div = (clk_sel >> 28) & 0x7; 295f51926eeSryan_chen 296f51926eeSryan_chen if(clk_sel & BIT(8)) { 297f51926eeSryan_chen clkin = ast2600_get_apll_rate(scu); 298f51926eeSryan_chen } else { 29910069884Sryan_chen clkin = ast2600_get_hclk(scu); 300f51926eeSryan_chen } 301f51926eeSryan_chen div = (div + 1) << 1; 302f51926eeSryan_chen 303f51926eeSryan_chen return (clkin / div); 304f51926eeSryan_chen } 305f51926eeSryan_chen 306f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu) 307f51926eeSryan_chen { 308bbbfb0c5Sryan_chen u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 309f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel1); 310f51926eeSryan_chen u32 div = (clk_sel >> 12) & 0x7; 311f51926eeSryan_chen 312f51926eeSryan_chen div = (div + 1) << 2; 313f51926eeSryan_chen 314f51926eeSryan_chen return (clkin / div); 315f51926eeSryan_chen } 316f51926eeSryan_chen 317f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx) 31827881d20Sryan_chen { 31927881d20Sryan_chen u32 uart_sel = readl(&scu->clk_sel4); 32027881d20Sryan_chen u32 uart_sel5 = readl(&scu->clk_sel5); 32127881d20Sryan_chen ulong uart_clk = 0; 32227881d20Sryan_chen 32327881d20Sryan_chen switch(uart_idx) { 32427881d20Sryan_chen case 1: 32527881d20Sryan_chen case 2: 32627881d20Sryan_chen case 3: 32727881d20Sryan_chen case 4: 32827881d20Sryan_chen case 6: 32927881d20Sryan_chen if(uart_sel & BIT(uart_idx - 1)) 33027881d20Sryan_chen uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ; 331550e691bSryan_chen else 33227881d20Sryan_chen uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ; 33327881d20Sryan_chen break; 33427881d20Sryan_chen case 5: //24mhz is come form usb phy 48Mhz 33527881d20Sryan_chen { 33627881d20Sryan_chen u8 uart5_clk_sel = 0; 33727881d20Sryan_chen //high bit 33827881d20Sryan_chen if (readl(&scu->misc_ctrl1) & BIT(12)) 33927881d20Sryan_chen uart5_clk_sel = 0x2; 34027881d20Sryan_chen else 34127881d20Sryan_chen uart5_clk_sel = 0x0; 342550e691bSryan_chen 34327881d20Sryan_chen if (readl(&scu->clk_sel2) & BIT(14)) 34427881d20Sryan_chen uart5_clk_sel |= 0x1; 345550e691bSryan_chen 34627881d20Sryan_chen switch(uart5_clk_sel) { 34727881d20Sryan_chen case 0: 34827881d20Sryan_chen uart_clk = 24000000; 34927881d20Sryan_chen break; 35027881d20Sryan_chen case 1: 35127881d20Sryan_chen uart_clk = 0; 35227881d20Sryan_chen break; 35327881d20Sryan_chen case 2: 35427881d20Sryan_chen uart_clk = 24000000/13; 35527881d20Sryan_chen break; 35627881d20Sryan_chen case 3: 35727881d20Sryan_chen uart_clk = 192000000/13; 35827881d20Sryan_chen break; 35927881d20Sryan_chen } 36027881d20Sryan_chen } 36127881d20Sryan_chen break; 36227881d20Sryan_chen case 7: 36327881d20Sryan_chen case 8: 36427881d20Sryan_chen case 9: 36527881d20Sryan_chen case 10: 36627881d20Sryan_chen case 11: 36727881d20Sryan_chen case 12: 36827881d20Sryan_chen case 13: 36927881d20Sryan_chen if(uart_sel5 & BIT(uart_idx - 1)) 37027881d20Sryan_chen uart_clk = ast2600_get_uart_from_uxclk_rate(scu)/13 ; 37127881d20Sryan_chen else 37227881d20Sryan_chen uart_clk = ast2600_get_uart_from_huxclk_rate(scu)/13 ; 37327881d20Sryan_chen break; 37427881d20Sryan_chen } 37527881d20Sryan_chen 37627881d20Sryan_chen return uart_clk; 377550e691bSryan_chen } 378550e691bSryan_chen 379feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk) 380feb42054Sryan_chen { 381feb42054Sryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 382feb42054Sryan_chen ulong rate = 0; 383feb42054Sryan_chen 384feb42054Sryan_chen switch (clk->id) { 385feb42054Sryan_chen case ASPEED_CLK_HPLL: 386bbbfb0c5Sryan_chen case ASPEED_CLK_EPLL: 387bbbfb0c5Sryan_chen case ASPEED_CLK_DPLL: 388d812df15Sryan_chen case ASPEED_CLK_MPLL: 389bbbfb0c5Sryan_chen rate = ast2600_get_pll_rate(priv->scu, clk->id); 390d812df15Sryan_chen break; 391feb42054Sryan_chen case ASPEED_CLK_AHB: 392feb42054Sryan_chen rate = ast2600_get_hclk(priv->scu); 393feb42054Sryan_chen break; 3946fa1ef3dSryan_chen case ASPEED_CLK_APB1: 3956fa1ef3dSryan_chen rate = ast2600_get_pclk1(priv->scu); 3966fa1ef3dSryan_chen break; 3976fa1ef3dSryan_chen case ASPEED_CLK_APB2: 3986fa1ef3dSryan_chen rate = ast2600_get_pclk2(priv->scu); 399feb42054Sryan_chen break; 400bbbfb0c5Sryan_chen case ASPEED_CLK_APLL: 401bbbfb0c5Sryan_chen rate = ast2600_get_apll_rate(priv->scu); 402bbbfb0c5Sryan_chen break; 403feb42054Sryan_chen case ASPEED_CLK_GATE_UART1CLK: 404feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 1); 405feb42054Sryan_chen break; 406feb42054Sryan_chen case ASPEED_CLK_GATE_UART2CLK: 407feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 2); 408feb42054Sryan_chen break; 409feb42054Sryan_chen case ASPEED_CLK_GATE_UART3CLK: 410feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 3); 411feb42054Sryan_chen break; 412feb42054Sryan_chen case ASPEED_CLK_GATE_UART4CLK: 413feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 4); 414feb42054Sryan_chen break; 415feb42054Sryan_chen case ASPEED_CLK_GATE_UART5CLK: 416feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 5); 417feb42054Sryan_chen break; 418f51926eeSryan_chen case ASPEED_CLK_SDIO: 419f51926eeSryan_chen rate = ast2600_get_sdio_clk_rate(priv->scu); 420f51926eeSryan_chen break; 421f51926eeSryan_chen case ASPEED_CLK_EMMC: 422f51926eeSryan_chen rate = ast2600_get_emmc_clk_rate(priv->scu); 423f51926eeSryan_chen break; 424feb42054Sryan_chen default: 425d812df15Sryan_chen pr_debug("can't get clk rate \n"); 426feb42054Sryan_chen return -ENOENT; 427d812df15Sryan_chen break; 428feb42054Sryan_chen } 429feb42054Sryan_chen 430feb42054Sryan_chen return rate; 431feb42054Sryan_chen } 432feb42054Sryan_chen 433577fcdaeSDylan Hung /** 434577fcdaeSDylan Hung * @brief lookup PLL divider config by input/output rate 435577fcdaeSDylan Hung * @param[in] *pll - PLL descriptor 436577fcdaeSDylan Hung * @return true - if PLL divider config is found, false - else 437550e691bSryan_chen * 438577fcdaeSDylan Hung * The function caller shall fill "pll->in" and "pll->out", then this function 439577fcdaeSDylan Hung * will search the lookup table to find a valid PLL divider configuration. 440550e691bSryan_chen */ 441577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll) 442550e691bSryan_chen { 443577fcdaeSDylan Hung u32 i; 444577fcdaeSDylan Hung bool is_found = false; 445550e691bSryan_chen 446577fcdaeSDylan Hung for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) { 447577fcdaeSDylan Hung const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i]; 448577fcdaeSDylan Hung if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) { 449577fcdaeSDylan Hung is_found = true; 450577fcdaeSDylan Hung pll->cfg.reg.w = def_cfg->cfg.reg.w; 451577fcdaeSDylan Hung pll->cfg.ext_reg = def_cfg->cfg.ext_reg; 452577fcdaeSDylan Hung break; 453550e691bSryan_chen } 454550e691bSryan_chen } 455577fcdaeSDylan Hung return is_found; 456550e691bSryan_chen } 457fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu, 458fd52be0bSDylan Hung struct ast2600_pll_cfg *p_cfg, int pll_idx) 459fd52be0bSDylan Hung { 460fd52be0bSDylan Hung u32 addr, addr_ext; 461fd52be0bSDylan Hung u32 reg; 462550e691bSryan_chen 463fd52be0bSDylan Hung switch (pll_idx) { 464fd52be0bSDylan Hung case ASPEED_CLK_HPLL: 465fd52be0bSDylan Hung addr = (u32)(&scu->h_pll_param); 466fd52be0bSDylan Hung addr_ext = (u32)(&scu->h_pll_ext_param); 467fd52be0bSDylan Hung break; 468fd52be0bSDylan Hung case ASPEED_CLK_MPLL: 469fd52be0bSDylan Hung addr = (u32)(&scu->m_pll_param); 470fd52be0bSDylan Hung addr_ext = (u32)(&scu->m_pll_ext_param); 471fd52be0bSDylan Hung break; 472fd52be0bSDylan Hung case ASPEED_CLK_DPLL: 473fd52be0bSDylan Hung addr = (u32)(&scu->d_pll_param); 474fd52be0bSDylan Hung addr_ext = (u32)(&scu->d_pll_ext_param); 475fd52be0bSDylan Hung break; 476fd52be0bSDylan Hung case ASPEED_CLK_EPLL: 477fd52be0bSDylan Hung addr = (u32)(&scu->e_pll_param); 478fd52be0bSDylan Hung addr_ext = (u32)(&scu->e_pll_ext_param); 479fd52be0bSDylan Hung break; 480fd52be0bSDylan Hung default: 481fd52be0bSDylan Hung debug("unknown PLL index\n"); 482fd52be0bSDylan Hung return 1; 483fd52be0bSDylan Hung } 484fd52be0bSDylan Hung 485fd52be0bSDylan Hung p_cfg->reg.b.bypass = 0; 486fd52be0bSDylan Hung p_cfg->reg.b.off = 1; 487fd52be0bSDylan Hung p_cfg->reg.b.reset = 1; 488fd52be0bSDylan Hung 489fd52be0bSDylan Hung reg = readl(addr); 490fd52be0bSDylan Hung reg &= ~GENMASK(25, 0); 491fd52be0bSDylan Hung reg |= p_cfg->reg.w; 492fd52be0bSDylan Hung writel(reg, addr); 493fd52be0bSDylan Hung 494fd52be0bSDylan Hung /* write extend parameter */ 495fd52be0bSDylan Hung writel(p_cfg->ext_reg, addr_ext); 496fd52be0bSDylan Hung udelay(100); 497fd52be0bSDylan Hung p_cfg->reg.b.off = 0; 498fd52be0bSDylan Hung p_cfg->reg.b.reset = 0; 499fd52be0bSDylan Hung reg &= ~GENMASK(25, 0); 500fd52be0bSDylan Hung reg |= p_cfg->reg.w; 501fd52be0bSDylan Hung writel(reg, addr); 502fd52be0bSDylan Hung 503fd52be0bSDylan Hung /* polling PLL lock status */ 504fd52be0bSDylan Hung while(0 == (readl(addr_ext) & BIT(31))); 505fd52be0bSDylan Hung 506fd52be0bSDylan Hung return 0; 507fd52be0bSDylan Hung } 508feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate) 509550e691bSryan_chen { 510577fcdaeSDylan Hung struct ast2600_pll_desc mpll; 511550e691bSryan_chen 512577fcdaeSDylan Hung mpll.in = AST2600_CLK_IN; 513577fcdaeSDylan Hung mpll.out = rate; 514577fcdaeSDylan Hung if (false == ast2600_search_clock_config(&mpll)) { 515577fcdaeSDylan Hung printf("error!! unable to find valid DDR clock setting\n"); 516577fcdaeSDylan Hung return 0; 517577fcdaeSDylan Hung } 518fd52be0bSDylan Hung ast2600_configure_pll(scu, &(mpll.cfg), ASPEED_CLK_MPLL); 519577fcdaeSDylan Hung 520cc476ffcSDylan Hung return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL); 521d6e349c7Sryan_chen } 522d6e349c7Sryan_chen 523d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate) 524550e691bSryan_chen { 525f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 526550e691bSryan_chen 527550e691bSryan_chen ulong new_rate; 528550e691bSryan_chen switch (clk->id) { 529f0d895afSryan_chen case ASPEED_CLK_MPLL: 530feb42054Sryan_chen new_rate = ast2600_configure_ddr(priv->scu, rate); 531550e691bSryan_chen break; 532550e691bSryan_chen default: 533550e691bSryan_chen return -ENOENT; 534550e691bSryan_chen } 535550e691bSryan_chen 536550e691bSryan_chen return new_rate; 537550e691bSryan_chen } 538feb42054Sryan_chen 539f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1 (20) 540f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2 (21) 541f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3 (20) 542f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4 (21) 543f9aa0ee1Sryan_chen 544cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu) 545cc476ffcSDylan Hung { 546*eff28274SJohnny Huang #if 0 547577fcdaeSDylan Hung struct ast2600_pll_desc epll; 548cc476ffcSDylan Hung 549577fcdaeSDylan Hung epll.in = AST2600_CLK_IN; 550577fcdaeSDylan Hung epll.out = 1000000000; 551577fcdaeSDylan Hung if (false == ast2600_search_clock_config(&epll)) { 552577fcdaeSDylan Hung printf( 553577fcdaeSDylan Hung "error!! unable to find valid ETHNET MAC clock setting\n"); 554577fcdaeSDylan Hung debug("%s: epll cfg = 0x%08x 0x%08x\n", __func__, 555577fcdaeSDylan Hung epll.cfg.reg.w, epll.cfg.ext_reg); 556577fcdaeSDylan Hung debug("%s: epll cfg = %02x %02x %02x\n", __func__, 557577fcdaeSDylan Hung epll.cfg.reg.b.m, epll.cfg.reg.b.n, epll.cfg.reg.b.p); 558577fcdaeSDylan Hung return 0; 559577fcdaeSDylan Hung } 560fd52be0bSDylan Hung ast2600_configure_pll(scu, &(epll.cfg), ASPEED_CLK_EPLL); 561577fcdaeSDylan Hung 562cc476ffcSDylan Hung /* select MAC#1 and MAC#2 clock source = EPLL / 8 */ 563cc476ffcSDylan Hung clksel = readl(&scu->clk_sel2); 564cc476ffcSDylan Hung clksel &= ~BIT(23); 565cc476ffcSDylan Hung clksel |= 0x7 << 20; 566cc476ffcSDylan Hung writel(clksel, &scu->clk_sel2); 567*eff28274SJohnny Huang #endif 568*eff28274SJohnny Huang /* scu340[25:0]: 1G default delay */ 569*eff28274SJohnny Huang clrsetbits_le32(&scu->mac12_clk_delay, GENMASK(25, 0), 570*eff28274SJohnny Huang MAC_DEF_DELAY_1G); 5714760b3f8SDylan Hung 5724760b3f8SDylan Hung /* set 100M/10M default delay */ 5734760b3f8SDylan Hung writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M); 5744760b3f8SDylan Hung writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M); 575cc476ffcSDylan Hung 576ed30249cSDylan Hung /* MAC AHB = HPLL / 6 */ 577*eff28274SJohnny Huang clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16)); 578894c19cfSDylan Hung 579cc476ffcSDylan Hung return 0; 580cc476ffcSDylan Hung } 581cc476ffcSDylan Hung 58254f9cba1SDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu) 58354f9cba1SDylan Hung { 58454f9cba1SDylan Hung ast2600_configure_mac12_clk(scu); 58554f9cba1SDylan Hung 58654f9cba1SDylan Hung /* 587*eff28274SJohnny Huang * scu350[31] RGMII 125M source: 0 = from IO pin 588*eff28274SJohnny Huang * scu350[25:0] MAC 1G delay 58954f9cba1SDylan Hung */ 590*eff28274SJohnny Huang clrsetbits_le32(&scu->mac34_clk_delay, (BIT(31) | GENMASK(25, 0)), 591*eff28274SJohnny Huang MAC34_DEF_DELAY_1G); 59254f9cba1SDylan Hung writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M); 59354f9cba1SDylan Hung writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M); 59454f9cba1SDylan Hung 595*eff28274SJohnny Huang /* 596*eff28274SJohnny Huang * clock source seletion and divider 597*eff28274SJohnny Huang * scu310[26:24] : MAC AHB bus clock = HCLK / 2 598*eff28274SJohnny Huang * scu310[18:16] : RMII 50M = HCLK_200M / 4 599*eff28274SJohnny Huang */ 600*eff28274SJohnny Huang clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)), 601*eff28274SJohnny Huang ((0x0 << 24) | (0x3 << 16))); 60254f9cba1SDylan Hung 603*eff28274SJohnny Huang /* 604*eff28274SJohnny Huang * set driving strength 605*eff28274SJohnny Huang * scu458[3:2] : MAC4 driving strength 606*eff28274SJohnny Huang * scu458[1:0] : MAC3 driving strength 607*eff28274SJohnny Huang */ 608*eff28274SJohnny Huang clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0), 609*eff28274SJohnny Huang (0x2 << 2) | (0x2 << 0)); 61054f9cba1SDylan Hung 61154f9cba1SDylan Hung return 0; 61254f9cba1SDylan Hung } 613*eff28274SJohnny Huang 61454f9cba1SDylan Hung /** 6155b5c3d44SDylan Hung * ast2600 RGMII clock source tree 61654f9cba1SDylan Hung * 61754f9cba1SDylan Hung * 125M from external PAD -------->|\ 61854f9cba1SDylan Hung * HPLL -->|\ | |---->RGMII 125M for MAC#1 & MAC#2 61954f9cba1SDylan Hung * | |---->| divider |---->|/ + 62054f9cba1SDylan Hung * EPLL -->|/ | 62154f9cba1SDylan Hung * | 622*eff28274SJohnny Huang * +---------<-----------|RGMIICK PAD output enable|<-------------+ 62354f9cba1SDylan Hung * | 624*eff28274SJohnny Huang * +--------------------------->|\ 62554f9cba1SDylan Hung * | |----> RGMII 125M for MAC#3 & MAC#4 626*eff28274SJohnny Huang * HCLK 200M ---->|divider|---->|/ 6275b5c3d44SDylan Hung * 628*eff28274SJohnny Huang * To simplify the control flow: 629*eff28274SJohnny Huang * 1. RGMII 1/2 always use EPLL as the internal clock source 630*eff28274SJohnny Huang * 2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source 6315b5c3d44SDylan Hung * 632*eff28274SJohnny Huang * 125M from external PAD -------->|\ 633*eff28274SJohnny Huang * | |---->RGMII 125M for MAC#1 & MAC#2 634*eff28274SJohnny Huang * EPLL---->| divider |--->|/ + 635*eff28274SJohnny Huang * | 636*eff28274SJohnny Huang * +<--------------------|RGMIICK PAD output enable|<-------------+ 637*eff28274SJohnny Huang * | 638*eff28274SJohnny Huang * +--------------------------->RGMII 125M for MAC#3 & MAC#4 639*eff28274SJohnny Huang */ 640*eff28274SJohnny Huang #define RGMIICK_SRC_PAD 0 641*eff28274SJohnny Huang #define RGMIICK_SRC_EPLL 1 /* recommended */ 642*eff28274SJohnny Huang #define RGMIICK_SRC_HPLL 2 643*eff28274SJohnny Huang 644*eff28274SJohnny Huang #define RGMIICK_DIV2 1 645*eff28274SJohnny Huang #define RGMIICK_DIV3 2 646*eff28274SJohnny Huang #define RGMIICK_DIV4 3 647*eff28274SJohnny Huang #define RGMIICK_DIV5 4 648*eff28274SJohnny Huang #define RGMIICK_DIV6 5 649*eff28274SJohnny Huang #define RGMIICK_DIV7 6 650*eff28274SJohnny Huang #define RGMIICK_DIV8 7 /* recommended */ 651*eff28274SJohnny Huang 652*eff28274SJohnny Huang #define RMIICK_DIV4 0 653*eff28274SJohnny Huang #define RMIICK_DIV8 1 654*eff28274SJohnny Huang #define RMIICK_DIV12 2 655*eff28274SJohnny Huang #define RMIICK_DIV16 3 656*eff28274SJohnny Huang #define RMIICK_DIV20 4 /* recommended */ 657*eff28274SJohnny Huang #define RMIICK_DIV24 5 658*eff28274SJohnny Huang #define RMIICK_DIV28 6 659*eff28274SJohnny Huang #define RMIICK_DIV32 7 660*eff28274SJohnny Huang 661*eff28274SJohnny Huang struct ast2600_mac_clk_div { 662*eff28274SJohnny Huang u32 src; /* 0=external PAD, 1=internal PLL */ 663*eff28274SJohnny Huang u32 fin; /* divider input speed */ 664*eff28274SJohnny Huang u32 n; /* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */ 665*eff28274SJohnny Huang u32 fout; /* fout = fin / n */ 666*eff28274SJohnny Huang }; 667*eff28274SJohnny Huang 668*eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = { 669*eff28274SJohnny Huang .src = ASPEED_CLK_EPLL, 670*eff28274SJohnny Huang .fin = 1000000000, 671*eff28274SJohnny Huang .n = RGMIICK_DIV8, 672*eff28274SJohnny Huang .fout = 125000000, 673*eff28274SJohnny Huang }; 674*eff28274SJohnny Huang 675*eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = { 676*eff28274SJohnny Huang .src = ASPEED_CLK_EPLL, 677*eff28274SJohnny Huang .fin = 1000000000, 678*eff28274SJohnny Huang .n = RMIICK_DIV20, 679*eff28274SJohnny Huang .fout = 50000000, 680*eff28274SJohnny Huang }; 681*eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu, 682*eff28274SJohnny Huang struct ast2600_mac_clk_div *p_cfg) 683*eff28274SJohnny Huang { 684*eff28274SJohnny Huang struct ast2600_pll_desc pll; 685*eff28274SJohnny Huang 686*eff28274SJohnny Huang pll.in = AST2600_CLK_IN; 687*eff28274SJohnny Huang pll.out = p_cfg->fin; 688*eff28274SJohnny Huang if (false == ast2600_search_clock_config(&pll)) { 689*eff28274SJohnny Huang printf("error!! unable to find valid ETHNET MAC clock " 690*eff28274SJohnny Huang "setting\n"); 691*eff28274SJohnny Huang debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w, 692*eff28274SJohnny Huang pll.cfg.ext_reg); 693*eff28274SJohnny Huang debug("%s: pll cfg = %02x %02x %02x\n", __func__, 694*eff28274SJohnny Huang pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p); 695*eff28274SJohnny Huang return; 696*eff28274SJohnny Huang } 697*eff28274SJohnny Huang ast2600_configure_pll(p_scu, &(pll.cfg), p_cfg->src); 698*eff28274SJohnny Huang } 699*eff28274SJohnny Huang 700*eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu, 701*eff28274SJohnny Huang struct ast2600_mac_clk_div *p_cfg) 702*eff28274SJohnny Huang { 703*eff28274SJohnny Huang u32 reg_304 = readl(&p_scu->clk_sel2); 704*eff28274SJohnny Huang u32 reg_340 = readl(&p_scu->mac12_clk_delay); 705*eff28274SJohnny Huang u32 reg_350 = readl(&p_scu->mac34_clk_delay); 706*eff28274SJohnny Huang 707*eff28274SJohnny Huang reg_340 &= ~GENMASK(31, 29); 708*eff28274SJohnny Huang /* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */ 709*eff28274SJohnny Huang reg_340 |= BIT(28); 710*eff28274SJohnny Huang if ((p_cfg->src == ASPEED_CLK_EPLL) || 711*eff28274SJohnny Huang (p_cfg->src == ASPEED_CLK_HPLL)) { 712*eff28274SJohnny Huang /* 713*eff28274SJohnny Huang * re-init PLL if the current PLL output frequency doesn't match 714*eff28274SJohnny Huang * the divider setting 715*eff28274SJohnny Huang */ 716*eff28274SJohnny Huang if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) { 717*eff28274SJohnny Huang ast2600_init_mac_pll(p_scu, p_cfg); 718*eff28274SJohnny Huang } 719*eff28274SJohnny Huang /* scu340[31]: select RGMII 125M from internal source */ 720*eff28274SJohnny Huang reg_340 |= BIT(31); 721*eff28274SJohnny Huang } 722*eff28274SJohnny Huang 723*eff28274SJohnny Huang reg_304 &= ~GENMASK(23, 20); 724*eff28274SJohnny Huang 725*eff28274SJohnny Huang /* set clock divider */ 726*eff28274SJohnny Huang reg_304 |= (p_cfg->n & 0x7) << 20; 727*eff28274SJohnny Huang 728*eff28274SJohnny Huang /* select internal clock source */ 729*eff28274SJohnny Huang if (ASPEED_CLK_HPLL == p_cfg->src) { 730*eff28274SJohnny Huang reg_304 |= BIT(23); 731*eff28274SJohnny Huang } 732*eff28274SJohnny Huang 733*eff28274SJohnny Huang /* RGMII 3/4 clock source select */ 734*eff28274SJohnny Huang reg_350 &= ~BIT(31); 735*eff28274SJohnny Huang #if 0 736*eff28274SJohnny Huang if (RGMII_3_4_CLK_SRC_HCLK == p_cfg->rgmii_3_4_clk_src) { 737*eff28274SJohnny Huang reg_350 |= BIT(31); 738*eff28274SJohnny Huang } 739*eff28274SJohnny Huang 740*eff28274SJohnny Huang /* set clock divider */ 741*eff28274SJohnny Huang reg_310 &= ~GENMASK(22, 20); 742*eff28274SJohnny Huang reg_310 |= (p_cfg->hclk_clk_div & 0x7) << 20; 743*eff28274SJohnny Huang #endif 744*eff28274SJohnny Huang 745*eff28274SJohnny Huang writel(reg_304, &p_scu->clk_sel2); 746*eff28274SJohnny Huang writel(reg_340, &p_scu->mac12_clk_delay); 747*eff28274SJohnny Huang writel(reg_350, &p_scu->mac34_clk_delay); 748*eff28274SJohnny Huang } 749*eff28274SJohnny Huang 750*eff28274SJohnny Huang /** 7515b5c3d44SDylan Hung * ast2600 RMII/NCSI clock source tree 7525b5c3d44SDylan Hung * 7535b5c3d44SDylan Hung * HPLL -->|\ 7545b5c3d44SDylan Hung * | |---->| divider |----> RMII 50M for MAC#1 & MAC#2 7555b5c3d44SDylan Hung * EPLL -->|/ 7565b5c3d44SDylan Hung * 7575b5c3d44SDylan Hung * HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4 75854f9cba1SDylan Hung */ 759*eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu, 760*eff28274SJohnny Huang struct ast2600_mac_clk_div *p_cfg) 76154f9cba1SDylan Hung { 762*eff28274SJohnny Huang u32 reg_304; 763*eff28274SJohnny Huang u32 reg_310; 764*eff28274SJohnny Huang 765*eff28274SJohnny Huang if ((p_cfg->src == ASPEED_CLK_EPLL) || 766*eff28274SJohnny Huang (p_cfg->src == ASPEED_CLK_HPLL)) { 767*eff28274SJohnny Huang /* 768*eff28274SJohnny Huang * re-init PLL if the current PLL output frequency doesn't match 769*eff28274SJohnny Huang * the divider setting 770*eff28274SJohnny Huang */ 771*eff28274SJohnny Huang if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) { 772*eff28274SJohnny Huang ast2600_init_mac_pll(p_scu, p_cfg); 773*eff28274SJohnny Huang } 77454f9cba1SDylan Hung } 77554f9cba1SDylan Hung 776*eff28274SJohnny Huang reg_304 = readl(&p_scu->clk_sel2); 777*eff28274SJohnny Huang reg_310 = readl(&p_scu->clk_sel4); 778*eff28274SJohnny Huang 779*eff28274SJohnny Huang reg_304 &= ~GENMASK(19, 16); 780*eff28274SJohnny Huang 781*eff28274SJohnny Huang /* set RMII 1/2 clock divider */ 782*eff28274SJohnny Huang reg_304 |= (p_cfg->n & 0x7) << 16; 783*eff28274SJohnny Huang 784*eff28274SJohnny Huang /* RMII clock source selection */ 785*eff28274SJohnny Huang if (ASPEED_CLK_HPLL == p_cfg->src) { 786*eff28274SJohnny Huang reg_304 |= BIT(19); 78754f9cba1SDylan Hung } 788*eff28274SJohnny Huang 789*eff28274SJohnny Huang /* set RMII 3/4 clock divider */ 790*eff28274SJohnny Huang reg_310 &= ~GENMASK(18, 16); 791*eff28274SJohnny Huang reg_310 |= (0x3 << 16); 792*eff28274SJohnny Huang 793*eff28274SJohnny Huang writel(reg_304, &p_scu->clk_sel2); 794*eff28274SJohnny Huang writel(reg_310, &p_scu->clk_sel4); 795*eff28274SJohnny Huang } 796*eff28274SJohnny Huang 797f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index) 798f9aa0ee1Sryan_chen { 799f9aa0ee1Sryan_chen u32 reset_bit; 800f9aa0ee1Sryan_chen u32 clkstop_bit; 801f9aa0ee1Sryan_chen 802*eff28274SJohnny Huang /* check board level setup */ 803*eff28274SJohnny Huang u32 mac_1_2_cfg = readl(&scu->hwstrap1) & GENMASK(7, 6); 804*eff28274SJohnny Huang u32 mac_3_4_cfg = readl(&scu->hwstrap2) & GENMASK(1, 0); 805*eff28274SJohnny Huang 806*eff28274SJohnny Huang if ((mac_1_2_cfg == 0) && (mac_3_4_cfg != 0)) { 807*eff28274SJohnny Huang /** 808*eff28274SJohnny Huang * HW limitation: 809*eff28274SJohnny Huang * impossible to set MAC 3/4 = RGMII when MAC 1/2 = RMII 810*eff28274SJohnny Huang */ 811*eff28274SJohnny Huang printf("%s: unsupported configuration\n", __func__); 812*eff28274SJohnny Huang return -EINVAL; 813*eff28274SJohnny Huang } else if (mac_1_2_cfg | mac_3_4_cfg) { 814*eff28274SJohnny Huang /* setup RGMII clock */ 815*eff28274SJohnny Huang ast2600_init_rgmii_clk(scu, &rgmii_clk_defconfig); 816*eff28274SJohnny Huang } else { 817*eff28274SJohnny Huang /* setup RMII clock */ 818*eff28274SJohnny Huang ast2600_init_rmii_clk(scu, &rmii_clk_defconfig); 819*eff28274SJohnny Huang } 820*eff28274SJohnny Huang 821cc476ffcSDylan Hung if (index < 3) 822cc476ffcSDylan Hung ast2600_configure_mac12_clk(scu); 823cc476ffcSDylan Hung else 824cc476ffcSDylan Hung ast2600_configure_mac34_clk(scu); 825f9aa0ee1Sryan_chen 826f9aa0ee1Sryan_chen switch (index) { 827f9aa0ee1Sryan_chen case 1: 828f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC1); 829f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC1); 830f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 831f9aa0ee1Sryan_chen udelay(100); 832f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 833f9aa0ee1Sryan_chen mdelay(10); 834f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 835f9aa0ee1Sryan_chen 836f9aa0ee1Sryan_chen break; 837f9aa0ee1Sryan_chen case 2: 838f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC2); 839f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC2); 840f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 841f9aa0ee1Sryan_chen udelay(100); 842f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 843f9aa0ee1Sryan_chen mdelay(10); 844f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 845f9aa0ee1Sryan_chen break; 846f9aa0ee1Sryan_chen case 3: 847f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC3 - 32); 848f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC3); 849f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 850f9aa0ee1Sryan_chen udelay(100); 851f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 852f9aa0ee1Sryan_chen mdelay(10); 853f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 854f9aa0ee1Sryan_chen break; 855f9aa0ee1Sryan_chen case 4: 856f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC4 - 32); 857f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC4); 858f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 859f9aa0ee1Sryan_chen udelay(100); 860f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 861f9aa0ee1Sryan_chen mdelay(10); 862f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 863f9aa0ee1Sryan_chen break; 864f9aa0ee1Sryan_chen default: 865f9aa0ee1Sryan_chen return -EINVAL; 866f9aa0ee1Sryan_chen } 867f9aa0ee1Sryan_chen 868f9aa0ee1Sryan_chen return 0; 869f9aa0ee1Sryan_chen } 870550e691bSryan_chen 871f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4 872f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu) 873f51926eeSryan_chen { 874f51926eeSryan_chen u32 reset_bit; 875f51926eeSryan_chen u32 clkstop_bit; 876f51926eeSryan_chen 877f51926eeSryan_chen reset_bit = BIT(ASPEED_RESET_SD - 32); 878f51926eeSryan_chen clkstop_bit = BIT(SCU_CLKSTOP_SDIO); 879f51926eeSryan_chen 880f51926eeSryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 881f51926eeSryan_chen udelay(100); 882f51926eeSryan_chen //enable clk 883f51926eeSryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 884f51926eeSryan_chen mdelay(10); 885f51926eeSryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 886f51926eeSryan_chen 887f51926eeSryan_chen return 0; 888f51926eeSryan_chen } 889f51926eeSryan_chen 890f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31 891f51926eeSryan_chen #define SCU_CLK_SD_MASK (0x7 << 28) 892f51926eeSryan_chen #define SCU_CLK_SD_DIV(x) (x << 28) 893f51926eeSryan_chen 894f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu) 895f51926eeSryan_chen { 896f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel4); 897f51926eeSryan_chen u32 enableclk_bit; 898f51926eeSryan_chen 899f51926eeSryan_chen enableclk_bit = BIT(SCU_CLKSTOP_EXTSD); 900f51926eeSryan_chen 90110069884Sryan_chen //default use apll for clock source 800/2 = 400 902f51926eeSryan_chen clk_sel &= ~SCU_CLK_SD_MASK; 90310069884Sryan_chen clk_sel |= SCU_CLK_SD_DIV(0) | BIT(8); 904f51926eeSryan_chen writel(clk_sel, &scu->clk_sel4); 905f51926eeSryan_chen 906f51926eeSryan_chen //enable clk 907f51926eeSryan_chen setbits_le32(&scu->clk_sel4, enableclk_bit); 908f51926eeSryan_chen 909f51926eeSryan_chen return 0; 910f51926eeSryan_chen } 911f51926eeSryan_chen 912f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27 913f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu) 914f51926eeSryan_chen { 915f51926eeSryan_chen u32 reset_bit; 916f51926eeSryan_chen u32 clkstop_bit; 917f51926eeSryan_chen 918f51926eeSryan_chen reset_bit = BIT(ASPEED_RESET_EMMC); 919f51926eeSryan_chen clkstop_bit = BIT(SCU_CLKSTOP_EMMC); 920f51926eeSryan_chen 921f51926eeSryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 922f51926eeSryan_chen udelay(100); 923f51926eeSryan_chen //enable clk 924f51926eeSryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 925f51926eeSryan_chen mdelay(10); 926f51926eeSryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 927f51926eeSryan_chen 928f51926eeSryan_chen return 0; 929f51926eeSryan_chen } 930f51926eeSryan_chen 931f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15 932f51926eeSryan_chen #define SCU_CLK_EMMC_MASK (0x7 << 12) 933f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x) (x << 12) 934f51926eeSryan_chen 935f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu) 936f51926eeSryan_chen { 937f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel1); 938f51926eeSryan_chen u32 enableclk_bit; 939f51926eeSryan_chen 940d0bdd5f3Sryan_chen enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC); 941f51926eeSryan_chen 942f51926eeSryan_chen clk_sel &= ~SCU_CLK_SD_MASK; 943f51926eeSryan_chen clk_sel |= SCU_CLK_SD_DIV(1); 944f51926eeSryan_chen writel(clk_sel, &scu->clk_sel1); 945f51926eeSryan_chen 946f51926eeSryan_chen //enable clk 947f51926eeSryan_chen setbits_le32(&scu->clk_sel1, enableclk_bit); 948f51926eeSryan_chen 949f51926eeSryan_chen return 0; 950f51926eeSryan_chen } 951f51926eeSryan_chen 952d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk) 953550e691bSryan_chen { 954f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 955550e691bSryan_chen 956550e691bSryan_chen switch (clk->id) { 95786f91560Sryan_chen case ASPEED_CLK_GATE_MAC1CLK: 95886f91560Sryan_chen ast2600_configure_mac(priv->scu, 1); 959550e691bSryan_chen break; 96086f91560Sryan_chen case ASPEED_CLK_GATE_MAC2CLK: 96186f91560Sryan_chen ast2600_configure_mac(priv->scu, 2); 962550e691bSryan_chen break; 96377843939Sryan_chen case ASPEED_CLK_GATE_MAC3CLK: 96477843939Sryan_chen ast2600_configure_mac(priv->scu, 3); 96577843939Sryan_chen break; 96677843939Sryan_chen case ASPEED_CLK_GATE_MAC4CLK: 96777843939Sryan_chen ast2600_configure_mac(priv->scu, 4); 96877843939Sryan_chen break; 969f51926eeSryan_chen case ASPEED_CLK_GATE_SDCLK: 970f51926eeSryan_chen ast2600_enable_sdclk(priv->scu); 971f51926eeSryan_chen break; 972f51926eeSryan_chen case ASPEED_CLK_GATE_SDEXTCLK: 973f51926eeSryan_chen ast2600_enable_extsdclk(priv->scu); 974f51926eeSryan_chen break; 975f51926eeSryan_chen case ASPEED_CLK_GATE_EMMCCLK: 976f51926eeSryan_chen ast2600_enable_emmcclk(priv->scu); 977f51926eeSryan_chen break; 978f51926eeSryan_chen case ASPEED_CLK_GATE_EMMCEXTCLK: 979f51926eeSryan_chen ast2600_enable_extemmcclk(priv->scu); 980f51926eeSryan_chen break; 981550e691bSryan_chen default: 982f9aa0ee1Sryan_chen pr_debug("can't enable clk \n"); 983550e691bSryan_chen return -ENOENT; 98477843939Sryan_chen break; 985550e691bSryan_chen } 986550e691bSryan_chen 987550e691bSryan_chen return 0; 988550e691bSryan_chen } 989550e691bSryan_chen 990f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = { 991d6e349c7Sryan_chen .get_rate = ast2600_clk_get_rate, 992d6e349c7Sryan_chen .set_rate = ast2600_clk_set_rate, 993d6e349c7Sryan_chen .enable = ast2600_clk_enable, 994550e691bSryan_chen }; 995550e691bSryan_chen 996d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev) 997550e691bSryan_chen { 998f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(dev); 999550e691bSryan_chen 1000f0d895afSryan_chen priv->scu = devfdt_get_addr_ptr(dev); 1001f0d895afSryan_chen if (IS_ERR(priv->scu)) 1002f0d895afSryan_chen return PTR_ERR(priv->scu); 1003550e691bSryan_chen 1004550e691bSryan_chen return 0; 1005550e691bSryan_chen } 1006550e691bSryan_chen 1007d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev) 1008550e691bSryan_chen { 1009550e691bSryan_chen int ret; 1010550e691bSryan_chen 1011550e691bSryan_chen /* The reset driver does not have a device node, so bind it here */ 1012550e691bSryan_chen ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev); 1013550e691bSryan_chen if (ret) 1014550e691bSryan_chen debug("Warning: No reset driver: ret=%d\n", ret); 1015550e691bSryan_chen 1016550e691bSryan_chen return 0; 1017550e691bSryan_chen } 1018550e691bSryan_chen 1019d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK) 1020d35ac78cSryan_chen struct aspeed_clks { 1021d35ac78cSryan_chen ulong id; 1022d35ac78cSryan_chen const char *name; 1023d35ac78cSryan_chen }; 1024d35ac78cSryan_chen 1025d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = { 1026d35ac78cSryan_chen { ASPEED_CLK_HPLL, "hpll" }, 1027d35ac78cSryan_chen { ASPEED_CLK_MPLL, "mpll" }, 1028d35ac78cSryan_chen { ASPEED_CLK_APLL, "apll" }, 1029d35ac78cSryan_chen { ASPEED_CLK_EPLL, "epll" }, 1030d35ac78cSryan_chen { ASPEED_CLK_DPLL, "dpll" }, 1031d35ac78cSryan_chen { ASPEED_CLK_AHB, "hclk" }, 10326fa1ef3dSryan_chen { ASPEED_CLK_APB1, "pclk1" }, 10336fa1ef3dSryan_chen { ASPEED_CLK_APB2, "pclk2" }, 1034d35ac78cSryan_chen }; 1035d35ac78cSryan_chen 1036d35ac78cSryan_chen int soc_clk_dump(void) 1037d35ac78cSryan_chen { 1038d35ac78cSryan_chen struct udevice *dev; 1039d35ac78cSryan_chen struct clk clk; 1040d35ac78cSryan_chen unsigned long rate; 1041d35ac78cSryan_chen int i, ret; 1042d35ac78cSryan_chen 1043d35ac78cSryan_chen ret = uclass_get_device_by_driver(UCLASS_CLK, 1044d35ac78cSryan_chen DM_GET_DRIVER(aspeed_scu), &dev); 1045d35ac78cSryan_chen if (ret) 1046d35ac78cSryan_chen return ret; 1047d35ac78cSryan_chen 1048d35ac78cSryan_chen printf("Clk\t\tHz\n"); 1049d35ac78cSryan_chen 1050d35ac78cSryan_chen for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) { 1051d35ac78cSryan_chen clk.id = aspeed_clk_names[i].id; 1052d35ac78cSryan_chen ret = clk_request(dev, &clk); 1053d35ac78cSryan_chen if (ret < 0) { 1054d35ac78cSryan_chen debug("%s clk_request() failed: %d\n", __func__, ret); 1055d35ac78cSryan_chen continue; 1056d35ac78cSryan_chen } 1057d35ac78cSryan_chen 1058d35ac78cSryan_chen ret = clk_get_rate(&clk); 1059d35ac78cSryan_chen rate = ret; 1060d35ac78cSryan_chen 1061d35ac78cSryan_chen clk_free(&clk); 1062d35ac78cSryan_chen 1063d35ac78cSryan_chen if (ret == -ENOTSUPP) { 1064d35ac78cSryan_chen printf("clk ID %lu not supported yet\n", 1065d35ac78cSryan_chen aspeed_clk_names[i].id); 1066d35ac78cSryan_chen continue; 1067d35ac78cSryan_chen } 1068d35ac78cSryan_chen if (ret < 0) { 1069d35ac78cSryan_chen printf("%s %lu: get_rate err: %d\n", 1070d35ac78cSryan_chen __func__, aspeed_clk_names[i].id, ret); 1071d35ac78cSryan_chen continue; 1072d35ac78cSryan_chen } 1073d35ac78cSryan_chen 1074d35ac78cSryan_chen printf("%s(%3lu):\t%lu\n", 1075d35ac78cSryan_chen aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate); 1076d35ac78cSryan_chen } 1077d35ac78cSryan_chen 1078d35ac78cSryan_chen return 0; 1079d35ac78cSryan_chen } 1080d35ac78cSryan_chen #endif 1081d35ac78cSryan_chen 1082d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = { 1083d6e349c7Sryan_chen { .compatible = "aspeed,ast2600-scu", }, 1084550e691bSryan_chen { } 1085550e691bSryan_chen }; 1086550e691bSryan_chen 1087aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = { 1088aa36597fSDylan Hung .name = "aspeed_scu", 1089550e691bSryan_chen .id = UCLASS_CLK, 1090d6e349c7Sryan_chen .of_match = ast2600_clk_ids, 1091f0d895afSryan_chen .priv_auto_alloc_size = sizeof(struct ast2600_clk_priv), 1092f9aa0ee1Sryan_chen .ops = &ast2600_clk_ops, 1093d6e349c7Sryan_chen .bind = ast2600_clk_bind, 1094d6e349c7Sryan_chen .probe = ast2600_clk_probe, 1095550e691bSryan_chen }; 1096