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 2175605a4bSDylan Hung #define MAC_DEF_DELAY_1G 0x0041b410 2275605a4bSDylan Hung #define MAC_DEF_DELAY_100M 0x00417410 2375605a4bSDylan Hung #define MAC_DEF_DELAY_10M 0x00417410 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[] = { 15745e0908aSryan_chen 2, 2, 3, 4, 158d812df15Sryan_chen }; 159d812df15Sryan_chen 16045e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div0_table[] = { 16145e0908aSryan_chen 3, 2, 3, 4, 16245e0908aSryan_chen }; 16345e0908aSryan_chen 16445e0908aSryan_chen static u32 ast2600_a1_axi_ahb_div1_table[] = { 16545e0908aSryan_chen 3, 4, 6, 8, 16645e0908aSryan_chen }; 16745e0908aSryan_chen 16845e0908aSryan_chen static u32 ast2600_a1_axi_ahb_default_table[] = { 16945e0908aSryan_chen 3, 4, 3, 4, 2, 2, 2, 2, 170d812df15Sryan_chen }; 171d812df15Sryan_chen 172d812df15Sryan_chen static u32 ast2600_get_hclk(struct ast2600_scu *scu) 173d812df15Sryan_chen { 174d812df15Sryan_chen u32 hw_rev = readl(&scu->chip_id0); 17545e0908aSryan_chen u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap); 176d812df15Sryan_chen u32 axi_div = 1; 177d812df15Sryan_chen u32 ahb_div = 0; 178d812df15Sryan_chen u32 rate = 0; 179d812df15Sryan_chen 18045e0908aSryan_chen if (hw_rev & BIT(16)) { 18145e0908aSryan_chen if(hwstrap1 & BIT(16)) { 18245e0908aSryan_chen ast2600_a1_axi_ahb_div1_table[0] = ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 0x3]; 183d812df15Sryan_chen axi_div = 1; 18445e0908aSryan_chen ahb_div = ast2600_a1_axi_ahb_div1_table[(hwstrap1 >> 11) & 0x3]; 18545e0908aSryan_chen } else { 18645e0908aSryan_chen ast2600_a1_axi_ahb_div0_table[0] = ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 0x3]; 187d812df15Sryan_chen axi_div = 2; 18845e0908aSryan_chen ahb_div = ast2600_a1_axi_ahb_div0_table[(hwstrap1 >> 11) & 0x3]; 18945e0908aSryan_chen } 19045e0908aSryan_chen } else { 19145e0908aSryan_chen //a0 : fix axi = hpll / 2 19245e0908aSryan_chen axi_div = 2; 193d812df15Sryan_chen ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3]; 19445e0908aSryan_chen } 195d812df15Sryan_chen 196bbbfb0c5Sryan_chen rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 1972717883aSryan_chen return (rate / axi_div / ahb_div); 1982717883aSryan_chen } 1992717883aSryan_chen 2006fa1ef3dSryan_chen static u32 ast2600_hpll_pclk1_div_table[] = { 2012717883aSryan_chen 4, 8, 12, 16, 20, 24, 28, 32, 2022717883aSryan_chen }; 2032717883aSryan_chen 2046fa1ef3dSryan_chen static u32 ast2600_hpll_pclk2_div_table[] = { 2056fa1ef3dSryan_chen 2, 4, 6, 8, 10, 12, 14, 16, 2066fa1ef3dSryan_chen }; 2076fa1ef3dSryan_chen 2086fa1ef3dSryan_chen static u32 ast2600_get_pclk1(struct ast2600_scu *scu) 2092717883aSryan_chen { 2102717883aSryan_chen u32 clk_sel1 = readl(&scu->clk_sel1); 2116fa1ef3dSryan_chen u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)]; 212bbbfb0c5Sryan_chen u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 2132717883aSryan_chen 2142717883aSryan_chen return (rate / apb_div); 215d812df15Sryan_chen } 216d812df15Sryan_chen 2176fa1ef3dSryan_chen static u32 ast2600_get_pclk2(struct ast2600_scu *scu) 2186fa1ef3dSryan_chen { 2196fa1ef3dSryan_chen u32 clk_sel4 = readl(&scu->clk_sel4); 2206fa1ef3dSryan_chen u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)]; 2216fa1ef3dSryan_chen u32 rate = ast2600_get_hclk(scu); 2226fa1ef3dSryan_chen 2236fa1ef3dSryan_chen return (rate / apb_div); 2246fa1ef3dSryan_chen } 2256fa1ef3dSryan_chen 2262e195992Sryan_chen static u32 ast2600_get_uxclk_in_rate(struct ast2600_scu *scu) 227d6e349c7Sryan_chen { 22827881d20Sryan_chen u32 clk_in = 0; 2292e195992Sryan_chen u32 uxclk_sel = readl(&scu->clk_sel5); 230550e691bSryan_chen 23127881d20Sryan_chen uxclk_sel &= 0x3; 23227881d20Sryan_chen switch(uxclk_sel) { 23327881d20Sryan_chen case 0: 23427881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 23527881d20Sryan_chen break; 23627881d20Sryan_chen case 1: 23727881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 23827881d20Sryan_chen break; 23927881d20Sryan_chen case 2: 24027881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 24127881d20Sryan_chen break; 24227881d20Sryan_chen case 3: 24327881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 24427881d20Sryan_chen break; 24527881d20Sryan_chen } 246d6e349c7Sryan_chen 24727881d20Sryan_chen return clk_in; 24827881d20Sryan_chen } 24927881d20Sryan_chen 2502e195992Sryan_chen static u32 ast2600_get_huxclk_in_rate(struct ast2600_scu *scu) 25127881d20Sryan_chen { 25227881d20Sryan_chen u32 clk_in = 0; 2532e195992Sryan_chen u32 huclk_sel = readl(&scu->clk_sel5); 25427881d20Sryan_chen 25527881d20Sryan_chen huclk_sel = ((huclk_sel >> 3) & 0x3); 25627881d20Sryan_chen switch(huclk_sel) { 25727881d20Sryan_chen case 0: 25827881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 4; 25927881d20Sryan_chen break; 26027881d20Sryan_chen case 1: 26127881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu) / 2; 26227881d20Sryan_chen break; 26327881d20Sryan_chen case 2: 26427881d20Sryan_chen clk_in = ast2600_get_apll_rate(scu); 26527881d20Sryan_chen break; 26627881d20Sryan_chen case 3: 26727881d20Sryan_chen clk_in = ast2600_get_hclk(scu); 26827881d20Sryan_chen break; 26927881d20Sryan_chen } 27027881d20Sryan_chen 27127881d20Sryan_chen return clk_in; 27227881d20Sryan_chen } 27327881d20Sryan_chen 2742e195992Sryan_chen static u32 ast2600_get_uart_uxclk_rate(struct ast2600_scu *scu) 27527881d20Sryan_chen { 2762e195992Sryan_chen u32 clk_in = ast2600_get_uxclk_in_rate(scu); 27727881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_uxclk); 27827881d20Sryan_chen unsigned int mult, div; 27927881d20Sryan_chen 28027881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 28127881d20Sryan_chen u32 r = div_reg & 0xff; 28227881d20Sryan_chen 28327881d20Sryan_chen mult = r; 2842e195992Sryan_chen div = (n * 2); 28527881d20Sryan_chen return (clk_in * mult)/div; 28627881d20Sryan_chen } 28727881d20Sryan_chen 2882e195992Sryan_chen static u32 ast2600_get_uart_huxclk_rate(struct ast2600_scu *scu) 28927881d20Sryan_chen { 2902e195992Sryan_chen u32 clk_in = ast2600_get_huxclk_in_rate(scu); 29127881d20Sryan_chen u32 div_reg = readl(&scu->uart_24m_ref_huxclk); 29227881d20Sryan_chen 29327881d20Sryan_chen unsigned int mult, div; 29427881d20Sryan_chen 29527881d20Sryan_chen u32 n = (div_reg >> 8) & 0x3ff; 29627881d20Sryan_chen u32 r = div_reg & 0xff; 29727881d20Sryan_chen 29827881d20Sryan_chen mult = r; 2992e195992Sryan_chen div = (n * 2); 30027881d20Sryan_chen return (clk_in * mult)/div; 30127881d20Sryan_chen } 30227881d20Sryan_chen 303f51926eeSryan_chen static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu) 304f51926eeSryan_chen { 305f51926eeSryan_chen u32 clkin = 0; 306f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel4); 307f51926eeSryan_chen u32 div = (clk_sel >> 28) & 0x7; 308f51926eeSryan_chen 309f51926eeSryan_chen if(clk_sel & BIT(8)) { 310f51926eeSryan_chen clkin = ast2600_get_apll_rate(scu); 311f51926eeSryan_chen } else { 31210069884Sryan_chen clkin = ast2600_get_hclk(scu); 313f51926eeSryan_chen } 314f51926eeSryan_chen div = (div + 1) << 1; 315f51926eeSryan_chen 316f51926eeSryan_chen return (clkin / div); 317f51926eeSryan_chen } 318f51926eeSryan_chen 319f51926eeSryan_chen static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu) 320f51926eeSryan_chen { 321bbbfb0c5Sryan_chen u32 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 322f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel1); 323f51926eeSryan_chen u32 div = (clk_sel >> 12) & 0x7; 324f51926eeSryan_chen 325f51926eeSryan_chen div = (div + 1) << 2; 326f51926eeSryan_chen 327f51926eeSryan_chen return (clkin / div); 328f51926eeSryan_chen } 329f51926eeSryan_chen 330f51926eeSryan_chen static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx) 33127881d20Sryan_chen { 33227881d20Sryan_chen u32 uart_sel = readl(&scu->clk_sel4); 33327881d20Sryan_chen u32 uart_sel5 = readl(&scu->clk_sel5); 33427881d20Sryan_chen ulong uart_clk = 0; 33527881d20Sryan_chen 33627881d20Sryan_chen switch(uart_idx) { 33727881d20Sryan_chen case 1: 33827881d20Sryan_chen case 2: 33927881d20Sryan_chen case 3: 34027881d20Sryan_chen case 4: 34127881d20Sryan_chen case 6: 34227881d20Sryan_chen if(uart_sel & BIT(uart_idx - 1)) 3432e195992Sryan_chen uart_clk = ast2600_get_uart_huxclk_rate(scu) ; 344550e691bSryan_chen else 3452e195992Sryan_chen uart_clk = ast2600_get_uart_uxclk_rate(scu) ; 34627881d20Sryan_chen break; 34727881d20Sryan_chen case 5: //24mhz is come form usb phy 48Mhz 34827881d20Sryan_chen { 34927881d20Sryan_chen u8 uart5_clk_sel = 0; 35027881d20Sryan_chen //high bit 35127881d20Sryan_chen if (readl(&scu->misc_ctrl1) & BIT(12)) 35227881d20Sryan_chen uart5_clk_sel = 0x2; 35327881d20Sryan_chen else 35427881d20Sryan_chen uart5_clk_sel = 0x0; 355550e691bSryan_chen 35627881d20Sryan_chen if (readl(&scu->clk_sel2) & BIT(14)) 35727881d20Sryan_chen uart5_clk_sel |= 0x1; 358550e691bSryan_chen 35927881d20Sryan_chen switch(uart5_clk_sel) { 36027881d20Sryan_chen case 0: 36127881d20Sryan_chen uart_clk = 24000000; 36227881d20Sryan_chen break; 36327881d20Sryan_chen case 1: 364def99fcbSryan_chen uart_clk = 192000000; 36527881d20Sryan_chen break; 36627881d20Sryan_chen case 2: 36727881d20Sryan_chen uart_clk = 24000000/13; 36827881d20Sryan_chen break; 36927881d20Sryan_chen case 3: 37027881d20Sryan_chen uart_clk = 192000000/13; 37127881d20Sryan_chen break; 37227881d20Sryan_chen } 37327881d20Sryan_chen } 37427881d20Sryan_chen break; 37527881d20Sryan_chen case 7: 37627881d20Sryan_chen case 8: 37727881d20Sryan_chen case 9: 37827881d20Sryan_chen case 10: 37927881d20Sryan_chen case 11: 38027881d20Sryan_chen case 12: 38127881d20Sryan_chen case 13: 38227881d20Sryan_chen if(uart_sel5 & BIT(uart_idx - 1)) 3832e195992Sryan_chen uart_clk = ast2600_get_uart_huxclk_rate(scu); 38427881d20Sryan_chen else 3852e195992Sryan_chen uart_clk = ast2600_get_uart_uxclk_rate(scu); 38627881d20Sryan_chen break; 38727881d20Sryan_chen } 38827881d20Sryan_chen 38927881d20Sryan_chen return uart_clk; 390550e691bSryan_chen } 391550e691bSryan_chen 392feb42054Sryan_chen static ulong ast2600_clk_get_rate(struct clk *clk) 393feb42054Sryan_chen { 394feb42054Sryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 395feb42054Sryan_chen ulong rate = 0; 396feb42054Sryan_chen 397feb42054Sryan_chen switch (clk->id) { 398feb42054Sryan_chen case ASPEED_CLK_HPLL: 399bbbfb0c5Sryan_chen case ASPEED_CLK_EPLL: 400bbbfb0c5Sryan_chen case ASPEED_CLK_DPLL: 401d812df15Sryan_chen case ASPEED_CLK_MPLL: 402bbbfb0c5Sryan_chen rate = ast2600_get_pll_rate(priv->scu, clk->id); 403d812df15Sryan_chen break; 404feb42054Sryan_chen case ASPEED_CLK_AHB: 405feb42054Sryan_chen rate = ast2600_get_hclk(priv->scu); 406feb42054Sryan_chen break; 4076fa1ef3dSryan_chen case ASPEED_CLK_APB1: 4086fa1ef3dSryan_chen rate = ast2600_get_pclk1(priv->scu); 4096fa1ef3dSryan_chen break; 4106fa1ef3dSryan_chen case ASPEED_CLK_APB2: 4116fa1ef3dSryan_chen rate = ast2600_get_pclk2(priv->scu); 412feb42054Sryan_chen break; 413bbbfb0c5Sryan_chen case ASPEED_CLK_APLL: 414bbbfb0c5Sryan_chen rate = ast2600_get_apll_rate(priv->scu); 415bbbfb0c5Sryan_chen break; 416feb42054Sryan_chen case ASPEED_CLK_GATE_UART1CLK: 417feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 1); 418feb42054Sryan_chen break; 419feb42054Sryan_chen case ASPEED_CLK_GATE_UART2CLK: 420feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 2); 421feb42054Sryan_chen break; 422feb42054Sryan_chen case ASPEED_CLK_GATE_UART3CLK: 423feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 3); 424feb42054Sryan_chen break; 425feb42054Sryan_chen case ASPEED_CLK_GATE_UART4CLK: 426feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 4); 427feb42054Sryan_chen break; 428feb42054Sryan_chen case ASPEED_CLK_GATE_UART5CLK: 429feb42054Sryan_chen rate = ast2600_get_uart_clk_rate(priv->scu, 5); 430feb42054Sryan_chen break; 431f51926eeSryan_chen case ASPEED_CLK_SDIO: 432f51926eeSryan_chen rate = ast2600_get_sdio_clk_rate(priv->scu); 433f51926eeSryan_chen break; 434f51926eeSryan_chen case ASPEED_CLK_EMMC: 435f51926eeSryan_chen rate = ast2600_get_emmc_clk_rate(priv->scu); 436f51926eeSryan_chen break; 4372e195992Sryan_chen case ASPEED_CLK_UARTX: 4382e195992Sryan_chen rate = ast2600_get_uart_uxclk_rate(priv->scu); 4392e195992Sryan_chen break; 4400998ddefSryan_chen case ASPEED_CLK_HUARTX: 4412e195992Sryan_chen rate = ast2600_get_uart_huxclk_rate(priv->scu); 4422e195992Sryan_chen break; 443feb42054Sryan_chen default: 444d812df15Sryan_chen pr_debug("can't get clk rate \n"); 445feb42054Sryan_chen return -ENOENT; 446d812df15Sryan_chen break; 447feb42054Sryan_chen } 448feb42054Sryan_chen 449feb42054Sryan_chen return rate; 450feb42054Sryan_chen } 451feb42054Sryan_chen 452577fcdaeSDylan Hung /** 453577fcdaeSDylan Hung * @brief lookup PLL divider config by input/output rate 454577fcdaeSDylan Hung * @param[in] *pll - PLL descriptor 455577fcdaeSDylan Hung * @return true - if PLL divider config is found, false - else 456550e691bSryan_chen * 457577fcdaeSDylan Hung * The function caller shall fill "pll->in" and "pll->out", then this function 458577fcdaeSDylan Hung * will search the lookup table to find a valid PLL divider configuration. 459550e691bSryan_chen */ 460577fcdaeSDylan Hung static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll) 461550e691bSryan_chen { 462577fcdaeSDylan Hung u32 i; 463577fcdaeSDylan Hung bool is_found = false; 464550e691bSryan_chen 465577fcdaeSDylan Hung for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) { 466577fcdaeSDylan Hung const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i]; 467577fcdaeSDylan Hung if ((def_cfg->in == pll->in) && (def_cfg->out == pll->out)) { 468577fcdaeSDylan Hung is_found = true; 469577fcdaeSDylan Hung pll->cfg.reg.w = def_cfg->cfg.reg.w; 470577fcdaeSDylan Hung pll->cfg.ext_reg = def_cfg->cfg.ext_reg; 471577fcdaeSDylan Hung break; 472550e691bSryan_chen } 473550e691bSryan_chen } 474577fcdaeSDylan Hung return is_found; 475550e691bSryan_chen } 476fd52be0bSDylan Hung static u32 ast2600_configure_pll(struct ast2600_scu *scu, 477fd52be0bSDylan Hung struct ast2600_pll_cfg *p_cfg, int pll_idx) 478fd52be0bSDylan Hung { 479fd52be0bSDylan Hung u32 addr, addr_ext; 480fd52be0bSDylan Hung u32 reg; 481550e691bSryan_chen 482fd52be0bSDylan Hung switch (pll_idx) { 483fd52be0bSDylan Hung case ASPEED_CLK_HPLL: 484fd52be0bSDylan Hung addr = (u32)(&scu->h_pll_param); 485fd52be0bSDylan Hung addr_ext = (u32)(&scu->h_pll_ext_param); 486fd52be0bSDylan Hung break; 487fd52be0bSDylan Hung case ASPEED_CLK_MPLL: 488fd52be0bSDylan Hung addr = (u32)(&scu->m_pll_param); 489fd52be0bSDylan Hung addr_ext = (u32)(&scu->m_pll_ext_param); 490fd52be0bSDylan Hung break; 491fd52be0bSDylan Hung case ASPEED_CLK_DPLL: 492fd52be0bSDylan Hung addr = (u32)(&scu->d_pll_param); 493fd52be0bSDylan Hung addr_ext = (u32)(&scu->d_pll_ext_param); 494fd52be0bSDylan Hung break; 495fd52be0bSDylan Hung case ASPEED_CLK_EPLL: 496fd52be0bSDylan Hung addr = (u32)(&scu->e_pll_param); 497fd52be0bSDylan Hung addr_ext = (u32)(&scu->e_pll_ext_param); 498fd52be0bSDylan Hung break; 499fd52be0bSDylan Hung default: 500fd52be0bSDylan Hung debug("unknown PLL index\n"); 501fd52be0bSDylan Hung return 1; 502fd52be0bSDylan Hung } 503fd52be0bSDylan Hung 504fd52be0bSDylan Hung p_cfg->reg.b.bypass = 0; 505fd52be0bSDylan Hung p_cfg->reg.b.off = 1; 506fd52be0bSDylan Hung p_cfg->reg.b.reset = 1; 507fd52be0bSDylan Hung 508fd52be0bSDylan Hung reg = readl(addr); 509fd52be0bSDylan Hung reg &= ~GENMASK(25, 0); 510fd52be0bSDylan Hung reg |= p_cfg->reg.w; 511fd52be0bSDylan Hung writel(reg, addr); 512fd52be0bSDylan Hung 513fd52be0bSDylan Hung /* write extend parameter */ 514fd52be0bSDylan Hung writel(p_cfg->ext_reg, addr_ext); 515fd52be0bSDylan Hung udelay(100); 516fd52be0bSDylan Hung p_cfg->reg.b.off = 0; 517fd52be0bSDylan Hung p_cfg->reg.b.reset = 0; 518fd52be0bSDylan Hung reg &= ~GENMASK(25, 0); 519fd52be0bSDylan Hung reg |= p_cfg->reg.w; 520fd52be0bSDylan Hung writel(reg, addr); 521fd52be0bSDylan Hung 522fd52be0bSDylan Hung /* polling PLL lock status */ 523fd52be0bSDylan Hung while(0 == (readl(addr_ext) & BIT(31))); 524fd52be0bSDylan Hung 525fd52be0bSDylan Hung return 0; 526fd52be0bSDylan Hung } 527feb42054Sryan_chen static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate) 528550e691bSryan_chen { 529577fcdaeSDylan Hung struct ast2600_pll_desc mpll; 530550e691bSryan_chen 531577fcdaeSDylan Hung mpll.in = AST2600_CLK_IN; 532577fcdaeSDylan Hung mpll.out = rate; 533577fcdaeSDylan Hung if (false == ast2600_search_clock_config(&mpll)) { 534577fcdaeSDylan Hung printf("error!! unable to find valid DDR clock setting\n"); 535577fcdaeSDylan Hung return 0; 536577fcdaeSDylan Hung } 537fd52be0bSDylan Hung ast2600_configure_pll(scu, &(mpll.cfg), ASPEED_CLK_MPLL); 538577fcdaeSDylan Hung 539cc476ffcSDylan Hung return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL); 540d6e349c7Sryan_chen } 541d6e349c7Sryan_chen 542d6e349c7Sryan_chen static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate) 543550e691bSryan_chen { 544f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 545550e691bSryan_chen 546550e691bSryan_chen ulong new_rate; 547550e691bSryan_chen switch (clk->id) { 548f0d895afSryan_chen case ASPEED_CLK_MPLL: 549feb42054Sryan_chen new_rate = ast2600_configure_ddr(priv->scu, rate); 550550e691bSryan_chen break; 551550e691bSryan_chen default: 552550e691bSryan_chen return -ENOENT; 553550e691bSryan_chen } 554550e691bSryan_chen 555550e691bSryan_chen return new_rate; 556550e691bSryan_chen } 557feb42054Sryan_chen 558f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC1 (20) 559f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC2 (21) 560f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC3 (20) 561f9aa0ee1Sryan_chen #define SCU_CLKSTOP_MAC4 (21) 562f9aa0ee1Sryan_chen 563cc476ffcSDylan Hung static u32 ast2600_configure_mac12_clk(struct ast2600_scu *scu) 564cc476ffcSDylan Hung { 565eff28274SJohnny Huang #if 0 566577fcdaeSDylan Hung struct ast2600_pll_desc epll; 567cc476ffcSDylan Hung 568577fcdaeSDylan Hung epll.in = AST2600_CLK_IN; 569577fcdaeSDylan Hung epll.out = 1000000000; 570577fcdaeSDylan Hung if (false == ast2600_search_clock_config(&epll)) { 571577fcdaeSDylan Hung printf( 572577fcdaeSDylan Hung "error!! unable to find valid ETHNET MAC clock setting\n"); 573577fcdaeSDylan Hung debug("%s: epll cfg = 0x%08x 0x%08x\n", __func__, 574577fcdaeSDylan Hung epll.cfg.reg.w, epll.cfg.ext_reg); 575577fcdaeSDylan Hung debug("%s: epll cfg = %02x %02x %02x\n", __func__, 576577fcdaeSDylan Hung epll.cfg.reg.b.m, epll.cfg.reg.b.n, epll.cfg.reg.b.p); 577577fcdaeSDylan Hung return 0; 578577fcdaeSDylan Hung } 579fd52be0bSDylan Hung ast2600_configure_pll(scu, &(epll.cfg), ASPEED_CLK_EPLL); 580577fcdaeSDylan Hung 581cc476ffcSDylan Hung /* select MAC#1 and MAC#2 clock source = EPLL / 8 */ 582cc476ffcSDylan Hung clksel = readl(&scu->clk_sel2); 583cc476ffcSDylan Hung clksel &= ~BIT(23); 584cc476ffcSDylan Hung clksel |= 0x7 << 20; 585cc476ffcSDylan Hung writel(clksel, &scu->clk_sel2); 586eff28274SJohnny Huang #endif 587eff28274SJohnny Huang /* scu340[25:0]: 1G default delay */ 588eff28274SJohnny Huang clrsetbits_le32(&scu->mac12_clk_delay, GENMASK(25, 0), 589eff28274SJohnny Huang MAC_DEF_DELAY_1G); 5904760b3f8SDylan Hung 5914760b3f8SDylan Hung /* set 100M/10M default delay */ 5924760b3f8SDylan Hung writel(MAC_DEF_DELAY_100M, &scu->mac12_clk_delay_100M); 5934760b3f8SDylan Hung writel(MAC_DEF_DELAY_10M, &scu->mac12_clk_delay_10M); 594cc476ffcSDylan Hung 595ed30249cSDylan Hung /* MAC AHB = HPLL / 6 */ 596eff28274SJohnny Huang clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16)); 597894c19cfSDylan Hung 598cc476ffcSDylan Hung return 0; 599cc476ffcSDylan Hung } 600cc476ffcSDylan Hung 60154f9cba1SDylan Hung static u32 ast2600_configure_mac34_clk(struct ast2600_scu *scu) 60254f9cba1SDylan Hung { 60354f9cba1SDylan Hung 60454f9cba1SDylan Hung /* 605eff28274SJohnny Huang * scu350[31] RGMII 125M source: 0 = from IO pin 606eff28274SJohnny Huang * scu350[25:0] MAC 1G delay 60754f9cba1SDylan Hung */ 608eff28274SJohnny Huang clrsetbits_le32(&scu->mac34_clk_delay, (BIT(31) | GENMASK(25, 0)), 609eff28274SJohnny Huang MAC34_DEF_DELAY_1G); 61054f9cba1SDylan Hung writel(MAC34_DEF_DELAY_100M, &scu->mac34_clk_delay_100M); 61154f9cba1SDylan Hung writel(MAC34_DEF_DELAY_10M, &scu->mac34_clk_delay_10M); 61254f9cba1SDylan Hung 613eff28274SJohnny Huang /* 614eff28274SJohnny Huang * clock source seletion and divider 615eff28274SJohnny Huang * scu310[26:24] : MAC AHB bus clock = HCLK / 2 616eff28274SJohnny Huang * scu310[18:16] : RMII 50M = HCLK_200M / 4 617eff28274SJohnny Huang */ 618eff28274SJohnny Huang clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)), 619eff28274SJohnny Huang ((0x0 << 24) | (0x3 << 16))); 62054f9cba1SDylan Hung 621eff28274SJohnny Huang /* 622eff28274SJohnny Huang * set driving strength 623eff28274SJohnny Huang * scu458[3:2] : MAC4 driving strength 624eff28274SJohnny Huang * scu458[1:0] : MAC3 driving strength 625eff28274SJohnny Huang */ 626eff28274SJohnny Huang clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0), 627eff28274SJohnny Huang (0x2 << 2) | (0x2 << 0)); 62854f9cba1SDylan Hung 62954f9cba1SDylan Hung return 0; 63054f9cba1SDylan Hung } 631eff28274SJohnny Huang 63254f9cba1SDylan Hung /** 6335b5c3d44SDylan Hung * ast2600 RGMII clock source tree 63454f9cba1SDylan Hung * 63554f9cba1SDylan Hung * 125M from external PAD -------->|\ 63654f9cba1SDylan Hung * HPLL -->|\ | |---->RGMII 125M for MAC#1 & MAC#2 63754f9cba1SDylan Hung * | |---->| divider |---->|/ + 63854f9cba1SDylan Hung * EPLL -->|/ | 63954f9cba1SDylan Hung * | 640eff28274SJohnny Huang * +---------<-----------|RGMIICK PAD output enable|<-------------+ 64154f9cba1SDylan Hung * | 642eff28274SJohnny Huang * +--------------------------->|\ 64354f9cba1SDylan Hung * | |----> RGMII 125M for MAC#3 & MAC#4 644eff28274SJohnny Huang * HCLK 200M ---->|divider|---->|/ 6455b5c3d44SDylan Hung * 646eff28274SJohnny Huang * To simplify the control flow: 647eff28274SJohnny Huang * 1. RGMII 1/2 always use EPLL as the internal clock source 648eff28274SJohnny Huang * 2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source 6495b5c3d44SDylan Hung * 650eff28274SJohnny Huang * 125M from external PAD -------->|\ 651eff28274SJohnny Huang * | |---->RGMII 125M for MAC#1 & MAC#2 652eff28274SJohnny Huang * EPLL---->| divider |--->|/ + 653eff28274SJohnny Huang * | 654eff28274SJohnny Huang * +<--------------------|RGMIICK PAD output enable|<-------------+ 655eff28274SJohnny Huang * | 656eff28274SJohnny Huang * +--------------------------->RGMII 125M for MAC#3 & MAC#4 657eff28274SJohnny Huang */ 658eff28274SJohnny Huang #define RGMIICK_SRC_PAD 0 659eff28274SJohnny Huang #define RGMIICK_SRC_EPLL 1 /* recommended */ 660eff28274SJohnny Huang #define RGMIICK_SRC_HPLL 2 661eff28274SJohnny Huang 662eff28274SJohnny Huang #define RGMIICK_DIV2 1 663eff28274SJohnny Huang #define RGMIICK_DIV3 2 664eff28274SJohnny Huang #define RGMIICK_DIV4 3 665eff28274SJohnny Huang #define RGMIICK_DIV5 4 666eff28274SJohnny Huang #define RGMIICK_DIV6 5 667eff28274SJohnny Huang #define RGMIICK_DIV7 6 668eff28274SJohnny Huang #define RGMIICK_DIV8 7 /* recommended */ 669eff28274SJohnny Huang 670eff28274SJohnny Huang #define RMIICK_DIV4 0 671eff28274SJohnny Huang #define RMIICK_DIV8 1 672eff28274SJohnny Huang #define RMIICK_DIV12 2 673eff28274SJohnny Huang #define RMIICK_DIV16 3 674eff28274SJohnny Huang #define RMIICK_DIV20 4 /* recommended */ 675eff28274SJohnny Huang #define RMIICK_DIV24 5 676eff28274SJohnny Huang #define RMIICK_DIV28 6 677eff28274SJohnny Huang #define RMIICK_DIV32 7 678eff28274SJohnny Huang 679eff28274SJohnny Huang struct ast2600_mac_clk_div { 680eff28274SJohnny Huang u32 src; /* 0=external PAD, 1=internal PLL */ 681eff28274SJohnny Huang u32 fin; /* divider input speed */ 682eff28274SJohnny Huang u32 n; /* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */ 683eff28274SJohnny Huang u32 fout; /* fout = fin / n */ 684eff28274SJohnny Huang }; 685eff28274SJohnny Huang 686eff28274SJohnny Huang struct ast2600_mac_clk_div rgmii_clk_defconfig = { 687eff28274SJohnny Huang .src = ASPEED_CLK_EPLL, 688eff28274SJohnny Huang .fin = 1000000000, 689eff28274SJohnny Huang .n = RGMIICK_DIV8, 690eff28274SJohnny Huang .fout = 125000000, 691eff28274SJohnny Huang }; 692eff28274SJohnny Huang 693eff28274SJohnny Huang struct ast2600_mac_clk_div rmii_clk_defconfig = { 694eff28274SJohnny Huang .src = ASPEED_CLK_EPLL, 695eff28274SJohnny Huang .fin = 1000000000, 696eff28274SJohnny Huang .n = RMIICK_DIV20, 697eff28274SJohnny Huang .fout = 50000000, 698eff28274SJohnny Huang }; 699eff28274SJohnny Huang static void ast2600_init_mac_pll(struct ast2600_scu *p_scu, 700eff28274SJohnny Huang struct ast2600_mac_clk_div *p_cfg) 701eff28274SJohnny Huang { 702eff28274SJohnny Huang struct ast2600_pll_desc pll; 703eff28274SJohnny Huang 704eff28274SJohnny Huang pll.in = AST2600_CLK_IN; 705eff28274SJohnny Huang pll.out = p_cfg->fin; 706eff28274SJohnny Huang if (false == ast2600_search_clock_config(&pll)) { 707eff28274SJohnny Huang printf("error!! unable to find valid ETHNET MAC clock " 708eff28274SJohnny Huang "setting\n"); 709eff28274SJohnny Huang debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w, 710eff28274SJohnny Huang pll.cfg.ext_reg); 711eff28274SJohnny Huang debug("%s: pll cfg = %02x %02x %02x\n", __func__, 712eff28274SJohnny Huang pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p); 713eff28274SJohnny Huang return; 714eff28274SJohnny Huang } 715eff28274SJohnny Huang ast2600_configure_pll(p_scu, &(pll.cfg), p_cfg->src); 716eff28274SJohnny Huang } 717eff28274SJohnny Huang 718eff28274SJohnny Huang static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu, 719eff28274SJohnny Huang struct ast2600_mac_clk_div *p_cfg) 720eff28274SJohnny Huang { 721eff28274SJohnny Huang u32 reg_304 = readl(&p_scu->clk_sel2); 722eff28274SJohnny Huang u32 reg_340 = readl(&p_scu->mac12_clk_delay); 723eff28274SJohnny Huang u32 reg_350 = readl(&p_scu->mac34_clk_delay); 724eff28274SJohnny Huang 725eff28274SJohnny Huang reg_340 &= ~GENMASK(31, 29); 726eff28274SJohnny Huang /* scu340[28]: RGMIICK PAD output enable (to MAC 3/4) */ 727eff28274SJohnny Huang reg_340 |= BIT(28); 728eff28274SJohnny Huang if ((p_cfg->src == ASPEED_CLK_EPLL) || 729eff28274SJohnny Huang (p_cfg->src == ASPEED_CLK_HPLL)) { 730eff28274SJohnny Huang /* 731eff28274SJohnny Huang * re-init PLL if the current PLL output frequency doesn't match 732eff28274SJohnny Huang * the divider setting 733eff28274SJohnny Huang */ 734eff28274SJohnny Huang if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) { 735eff28274SJohnny Huang ast2600_init_mac_pll(p_scu, p_cfg); 736eff28274SJohnny Huang } 737eff28274SJohnny Huang /* scu340[31]: select RGMII 125M from internal source */ 738eff28274SJohnny Huang reg_340 |= BIT(31); 739eff28274SJohnny Huang } 740eff28274SJohnny Huang 741eff28274SJohnny Huang reg_304 &= ~GENMASK(23, 20); 742eff28274SJohnny Huang 743eff28274SJohnny Huang /* set clock divider */ 744eff28274SJohnny Huang reg_304 |= (p_cfg->n & 0x7) << 20; 745eff28274SJohnny Huang 746eff28274SJohnny Huang /* select internal clock source */ 747eff28274SJohnny Huang if (ASPEED_CLK_HPLL == p_cfg->src) { 748eff28274SJohnny Huang reg_304 |= BIT(23); 749eff28274SJohnny Huang } 750eff28274SJohnny Huang 751eff28274SJohnny Huang /* RGMII 3/4 clock source select */ 752eff28274SJohnny Huang reg_350 &= ~BIT(31); 753eff28274SJohnny Huang #if 0 754eff28274SJohnny Huang if (RGMII_3_4_CLK_SRC_HCLK == p_cfg->rgmii_3_4_clk_src) { 755eff28274SJohnny Huang reg_350 |= BIT(31); 756eff28274SJohnny Huang } 757eff28274SJohnny Huang 758eff28274SJohnny Huang /* set clock divider */ 759eff28274SJohnny Huang reg_310 &= ~GENMASK(22, 20); 760eff28274SJohnny Huang reg_310 |= (p_cfg->hclk_clk_div & 0x7) << 20; 761eff28274SJohnny Huang #endif 762eff28274SJohnny Huang 763eff28274SJohnny Huang writel(reg_304, &p_scu->clk_sel2); 764eff28274SJohnny Huang writel(reg_340, &p_scu->mac12_clk_delay); 765eff28274SJohnny Huang writel(reg_350, &p_scu->mac34_clk_delay); 766eff28274SJohnny Huang } 767eff28274SJohnny Huang 768eff28274SJohnny Huang /** 7695b5c3d44SDylan Hung * ast2600 RMII/NCSI clock source tree 7705b5c3d44SDylan Hung * 7715b5c3d44SDylan Hung * HPLL -->|\ 7725b5c3d44SDylan Hung * | |---->| divider |----> RMII 50M for MAC#1 & MAC#2 7735b5c3d44SDylan Hung * EPLL -->|/ 7745b5c3d44SDylan Hung * 7755b5c3d44SDylan Hung * HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4 77654f9cba1SDylan Hung */ 777eff28274SJohnny Huang static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu, 778eff28274SJohnny Huang struct ast2600_mac_clk_div *p_cfg) 77954f9cba1SDylan Hung { 780eff28274SJohnny Huang u32 reg_304; 781eff28274SJohnny Huang u32 reg_310; 782eff28274SJohnny Huang 783eff28274SJohnny Huang if ((p_cfg->src == ASPEED_CLK_EPLL) || 784eff28274SJohnny Huang (p_cfg->src == ASPEED_CLK_HPLL)) { 785eff28274SJohnny Huang /* 786eff28274SJohnny Huang * re-init PLL if the current PLL output frequency doesn't match 787eff28274SJohnny Huang * the divider setting 788eff28274SJohnny Huang */ 789eff28274SJohnny Huang if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) { 790eff28274SJohnny Huang ast2600_init_mac_pll(p_scu, p_cfg); 791eff28274SJohnny Huang } 79254f9cba1SDylan Hung } 79354f9cba1SDylan Hung 794eff28274SJohnny Huang reg_304 = readl(&p_scu->clk_sel2); 795eff28274SJohnny Huang reg_310 = readl(&p_scu->clk_sel4); 796eff28274SJohnny Huang 797eff28274SJohnny Huang reg_304 &= ~GENMASK(19, 16); 798eff28274SJohnny Huang 799eff28274SJohnny Huang /* set RMII 1/2 clock divider */ 800eff28274SJohnny Huang reg_304 |= (p_cfg->n & 0x7) << 16; 801eff28274SJohnny Huang 802eff28274SJohnny Huang /* RMII clock source selection */ 803eff28274SJohnny Huang if (ASPEED_CLK_HPLL == p_cfg->src) { 804eff28274SJohnny Huang reg_304 |= BIT(19); 80554f9cba1SDylan Hung } 806eff28274SJohnny Huang 807eff28274SJohnny Huang /* set RMII 3/4 clock divider */ 808eff28274SJohnny Huang reg_310 &= ~GENMASK(18, 16); 809eff28274SJohnny Huang reg_310 |= (0x3 << 16); 810eff28274SJohnny Huang 811eff28274SJohnny Huang writel(reg_304, &p_scu->clk_sel2); 812eff28274SJohnny Huang writel(reg_310, &p_scu->clk_sel4); 813eff28274SJohnny Huang } 814eff28274SJohnny Huang 815f9aa0ee1Sryan_chen static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index) 816f9aa0ee1Sryan_chen { 817f9aa0ee1Sryan_chen u32 reset_bit; 818f9aa0ee1Sryan_chen u32 clkstop_bit; 819f9aa0ee1Sryan_chen 820f9aa0ee1Sryan_chen switch (index) { 821f9aa0ee1Sryan_chen case 1: 822f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC1); 823f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC1); 824f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 825f9aa0ee1Sryan_chen udelay(100); 826f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 827f9aa0ee1Sryan_chen mdelay(10); 828f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 829f9aa0ee1Sryan_chen 830f9aa0ee1Sryan_chen break; 831f9aa0ee1Sryan_chen case 2: 832f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC2); 833f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC2); 834f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 835f9aa0ee1Sryan_chen udelay(100); 836f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 837f9aa0ee1Sryan_chen mdelay(10); 838f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 839f9aa0ee1Sryan_chen break; 840f9aa0ee1Sryan_chen case 3: 841f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC3 - 32); 842f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC3); 843f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 844f9aa0ee1Sryan_chen udelay(100); 845f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 846f9aa0ee1Sryan_chen mdelay(10); 847f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 848f9aa0ee1Sryan_chen break; 849f9aa0ee1Sryan_chen case 4: 850f9aa0ee1Sryan_chen reset_bit = BIT(ASPEED_RESET_MAC4 - 32); 851f9aa0ee1Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_MAC4); 852f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 853f9aa0ee1Sryan_chen udelay(100); 854f9aa0ee1Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 855f9aa0ee1Sryan_chen mdelay(10); 856f9aa0ee1Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 857f9aa0ee1Sryan_chen break; 858f9aa0ee1Sryan_chen default: 859f9aa0ee1Sryan_chen return -EINVAL; 860f9aa0ee1Sryan_chen } 861f9aa0ee1Sryan_chen 862f9aa0ee1Sryan_chen return 0; 863f9aa0ee1Sryan_chen } 864550e691bSryan_chen 865f51926eeSryan_chen #define SCU_CLKSTOP_SDIO 4 866f51926eeSryan_chen static ulong ast2600_enable_sdclk(struct ast2600_scu *scu) 867f51926eeSryan_chen { 868f51926eeSryan_chen u32 reset_bit; 869f51926eeSryan_chen u32 clkstop_bit; 870f51926eeSryan_chen 871f51926eeSryan_chen reset_bit = BIT(ASPEED_RESET_SD - 32); 872f51926eeSryan_chen clkstop_bit = BIT(SCU_CLKSTOP_SDIO); 873f51926eeSryan_chen 874fc9f12e6Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 875fc9f12e6Sryan_chen 876f51926eeSryan_chen udelay(100); 877f51926eeSryan_chen //enable clk 878f51926eeSryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 879f51926eeSryan_chen mdelay(10); 880fc9f12e6Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 881f51926eeSryan_chen 882f51926eeSryan_chen return 0; 883f51926eeSryan_chen } 884f51926eeSryan_chen 885f51926eeSryan_chen #define SCU_CLKSTOP_EXTSD 31 886f51926eeSryan_chen #define SCU_CLK_SD_MASK (0x7 << 28) 887f51926eeSryan_chen #define SCU_CLK_SD_DIV(x) (x << 28) 888f51926eeSryan_chen 889f51926eeSryan_chen static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu) 890f51926eeSryan_chen { 891f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel4); 892f51926eeSryan_chen u32 enableclk_bit; 893f51926eeSryan_chen 894f51926eeSryan_chen enableclk_bit = BIT(SCU_CLKSTOP_EXTSD); 895f51926eeSryan_chen 896fc9f12e6Sryan_chen //default use apll for clock source 800/4 = 200 : controller max is 200mhz 897f51926eeSryan_chen clk_sel &= ~SCU_CLK_SD_MASK; 898fc9f12e6Sryan_chen clk_sel |= SCU_CLK_SD_DIV(1) | BIT(8); 899f51926eeSryan_chen writel(clk_sel, &scu->clk_sel4); 900f51926eeSryan_chen 901f51926eeSryan_chen //enable clk 902f51926eeSryan_chen setbits_le32(&scu->clk_sel4, enableclk_bit); 903f51926eeSryan_chen 904f51926eeSryan_chen return 0; 905f51926eeSryan_chen } 906f51926eeSryan_chen 907f51926eeSryan_chen #define SCU_CLKSTOP_EMMC 27 908f51926eeSryan_chen static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu) 909f51926eeSryan_chen { 910f51926eeSryan_chen u32 reset_bit; 911f51926eeSryan_chen u32 clkstop_bit; 912f51926eeSryan_chen 913f51926eeSryan_chen reset_bit = BIT(ASPEED_RESET_EMMC); 914f51926eeSryan_chen clkstop_bit = BIT(SCU_CLKSTOP_EMMC); 915f51926eeSryan_chen 916fc9f12e6Sryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 917f51926eeSryan_chen udelay(100); 918f51926eeSryan_chen //enable clk 919f51926eeSryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 920f51926eeSryan_chen mdelay(10); 921fc9f12e6Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 922f51926eeSryan_chen 923f51926eeSryan_chen return 0; 924f51926eeSryan_chen } 925f51926eeSryan_chen 926f51926eeSryan_chen #define SCU_CLKSTOP_EXTEMMC 15 927f51926eeSryan_chen #define SCU_CLK_EMMC_MASK (0x7 << 12) 928f51926eeSryan_chen #define SCU_CLK_EMMC_DIV(x) (x << 12) 929f51926eeSryan_chen 930f51926eeSryan_chen static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu) 931f51926eeSryan_chen { 932*b0c30ea3Sryan_chen u32 revision_id = readl(&scu->chip_id0); 933f51926eeSryan_chen u32 clk_sel = readl(&scu->clk_sel1); 934f51926eeSryan_chen u32 enableclk_bit; 935f4c4ddb1Sryan_chen u32 rate = 0; 936f4c4ddb1Sryan_chen u32 div = 0; 937f4c4ddb1Sryan_chen int i = 0; 938f51926eeSryan_chen 939d0bdd5f3Sryan_chen enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC); 940f51926eeSryan_chen 941*b0c30ea3Sryan_chen if(((revision_id & GENMASK(23, 16)) >> 16) == 0x1) { 942*b0c30ea3Sryan_chen //use mpll to be clk source 943*b0c30ea3Sryan_chen rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL); 944*b0c30ea3Sryan_chen for(i = 0; i < 8; i++) { 945*b0c30ea3Sryan_chen div = (i + 1) * 2; 946*b0c30ea3Sryan_chen if ((rate / div) <= 200000000) 947*b0c30ea3Sryan_chen break; 948*b0c30ea3Sryan_chen } 949*b0c30ea3Sryan_chen 950*b0c30ea3Sryan_chen clk_sel &= ~SCU_CLK_EMMC_MASK; 951*b0c30ea3Sryan_chen clk_sel |= SCU_CLK_EMMC_DIV(i) | BIT(11); 952*b0c30ea3Sryan_chen writel(clk_sel, &scu->clk_sel1); 953*b0c30ea3Sryan_chen 954*b0c30ea3Sryan_chen } else { 955*b0c30ea3Sryan_chen //use hpll to be clk source 956*b0c30ea3Sryan_chen 957f4c4ddb1Sryan_chen //ast2600 eMMC controller max clk is 200Mhz 958f4c4ddb1Sryan_chen rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 959f4c4ddb1Sryan_chen 960f4c4ddb1Sryan_chen for(i = 0; i < 8; i++) { 961f4c4ddb1Sryan_chen div = (i + 1) * 4; 962f4c4ddb1Sryan_chen if ((rate / div) <= 200000000) 963f4c4ddb1Sryan_chen break; 964f4c4ddb1Sryan_chen } 965f4c4ddb1Sryan_chen 966f4c4ddb1Sryan_chen clk_sel &= ~SCU_CLK_EMMC_MASK; 967f4c4ddb1Sryan_chen clk_sel |= SCU_CLK_EMMC_DIV(i); 968f51926eeSryan_chen writel(clk_sel, &scu->clk_sel1); 969*b0c30ea3Sryan_chen } 970f51926eeSryan_chen 971f51926eeSryan_chen //enable clk 972f51926eeSryan_chen setbits_le32(&scu->clk_sel1, enableclk_bit); 973f51926eeSryan_chen 974f51926eeSryan_chen return 0; 975f51926eeSryan_chen } 976f51926eeSryan_chen 977baf00c26Sryan_chen #define SCU_CLKSTOP_FSICLK 30 978baf00c26Sryan_chen 979baf00c26Sryan_chen static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu) 980baf00c26Sryan_chen { 981baf00c26Sryan_chen u32 reset_bit; 982baf00c26Sryan_chen u32 clkstop_bit; 983baf00c26Sryan_chen 984baf00c26Sryan_chen reset_bit = BIT(ASPEED_RESET_FSI % 32); 985baf00c26Sryan_chen clkstop_bit = BIT(SCU_CLKSTOP_FSICLK); 986baf00c26Sryan_chen 987baf00c26Sryan_chen /* The FSI clock is shared between masters. If it's already on 988baf00c26Sryan_chen * don't touch it, as that will reset the existing master. */ 989baf00c26Sryan_chen if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) { 990baf00c26Sryan_chen debug("%s: already running, not touching it\n", __func__); 991baf00c26Sryan_chen return 0; 992baf00c26Sryan_chen } 993baf00c26Sryan_chen 994baf00c26Sryan_chen writel(reset_bit, &scu->sysreset_ctrl2); 995baf00c26Sryan_chen udelay(100); 996baf00c26Sryan_chen //enable clk 997baf00c26Sryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 998baf00c26Sryan_chen mdelay(10); 999baf00c26Sryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl2); 1000baf00c26Sryan_chen 1001baf00c26Sryan_chen return 0; 1002baf00c26Sryan_chen } 1003baf00c26Sryan_chen 1004b8ec5ceaSryan_chen static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu) 1005b8ec5ceaSryan_chen { 1006b8ec5ceaSryan_chen u32 reset_bit; 1007b8ec5ceaSryan_chen u32 clkstop_bit; 1008b8ec5ceaSryan_chen 1009b8ec5ceaSryan_chen reset_bit = BIT(ASPEED_RESET_EHCI_P1); 1010b8ec5ceaSryan_chen clkstop_bit = BIT(14); 1011b8ec5ceaSryan_chen 1012b8ec5ceaSryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 1013b8ec5ceaSryan_chen udelay(100); 1014b8ec5ceaSryan_chen //enable phy clk 1015b8ec5ceaSryan_chen writel(clkstop_bit, &scu->clk_stop_ctrl1); 1016b8ec5ceaSryan_chen mdelay(20); 1017b8ec5ceaSryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 1018b8ec5ceaSryan_chen 1019b8ec5ceaSryan_chen return 0; 1020b8ec5ceaSryan_chen } 1021b8ec5ceaSryan_chen 1022b8ec5ceaSryan_chen static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu) 1023b8ec5ceaSryan_chen { 1024b8ec5ceaSryan_chen u32 reset_bit; 1025b8ec5ceaSryan_chen u32 clkstop_bit; 1026b8ec5ceaSryan_chen 1027b8ec5ceaSryan_chen reset_bit = BIT(ASPEED_RESET_EHCI_P2); 1028b8ec5ceaSryan_chen clkstop_bit = BIT(7); 1029b8ec5ceaSryan_chen 1030b8ec5ceaSryan_chen writel(reset_bit, &scu->sysreset_ctrl1); 1031b8ec5ceaSryan_chen udelay(100); 1032b8ec5ceaSryan_chen //enable phy clk 1033b8ec5ceaSryan_chen writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1034b8ec5ceaSryan_chen mdelay(20); 1035b8ec5ceaSryan_chen 1036b8ec5ceaSryan_chen writel(reset_bit, &scu->sysreset_clr_ctrl1); 1037b8ec5ceaSryan_chen 1038b8ec5ceaSryan_chen return 0; 1039b8ec5ceaSryan_chen } 1040b8ec5ceaSryan_chen 1041d6e349c7Sryan_chen static int ast2600_clk_enable(struct clk *clk) 1042550e691bSryan_chen { 1043f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 1044550e691bSryan_chen 1045550e691bSryan_chen switch (clk->id) { 104686f91560Sryan_chen case ASPEED_CLK_GATE_MAC1CLK: 104786f91560Sryan_chen ast2600_configure_mac(priv->scu, 1); 1048550e691bSryan_chen break; 104986f91560Sryan_chen case ASPEED_CLK_GATE_MAC2CLK: 105086f91560Sryan_chen ast2600_configure_mac(priv->scu, 2); 1051550e691bSryan_chen break; 105277843939Sryan_chen case ASPEED_CLK_GATE_MAC3CLK: 105377843939Sryan_chen ast2600_configure_mac(priv->scu, 3); 105477843939Sryan_chen break; 105577843939Sryan_chen case ASPEED_CLK_GATE_MAC4CLK: 105677843939Sryan_chen ast2600_configure_mac(priv->scu, 4); 105777843939Sryan_chen break; 1058f51926eeSryan_chen case ASPEED_CLK_GATE_SDCLK: 1059f51926eeSryan_chen ast2600_enable_sdclk(priv->scu); 1060f51926eeSryan_chen break; 1061f51926eeSryan_chen case ASPEED_CLK_GATE_SDEXTCLK: 1062f51926eeSryan_chen ast2600_enable_extsdclk(priv->scu); 1063f51926eeSryan_chen break; 1064f51926eeSryan_chen case ASPEED_CLK_GATE_EMMCCLK: 1065f51926eeSryan_chen ast2600_enable_emmcclk(priv->scu); 1066f51926eeSryan_chen break; 1067f51926eeSryan_chen case ASPEED_CLK_GATE_EMMCEXTCLK: 1068f51926eeSryan_chen ast2600_enable_extemmcclk(priv->scu); 1069f51926eeSryan_chen break; 1070baf00c26Sryan_chen case ASPEED_CLK_GATE_FSICLK: 1071baf00c26Sryan_chen ast2600_enable_fsiclk(priv->scu); 1072baf00c26Sryan_chen break; 1073b8ec5ceaSryan_chen case ASPEED_CLK_GATE_USBPORT1CLK: 1074b8ec5ceaSryan_chen ast2600_enable_usbahclk(priv->scu); 1075b8ec5ceaSryan_chen break; 1076b8ec5ceaSryan_chen case ASPEED_CLK_GATE_USBPORT2CLK: 1077b8ec5ceaSryan_chen ast2600_enable_usbbhclk(priv->scu); 1078b8ec5ceaSryan_chen break; 1079550e691bSryan_chen default: 1080f9aa0ee1Sryan_chen pr_debug("can't enable clk \n"); 1081550e691bSryan_chen return -ENOENT; 108277843939Sryan_chen break; 1083550e691bSryan_chen } 1084550e691bSryan_chen 1085550e691bSryan_chen return 0; 1086550e691bSryan_chen } 1087550e691bSryan_chen 1088f9aa0ee1Sryan_chen struct clk_ops ast2600_clk_ops = { 1089d6e349c7Sryan_chen .get_rate = ast2600_clk_get_rate, 1090d6e349c7Sryan_chen .set_rate = ast2600_clk_set_rate, 1091d6e349c7Sryan_chen .enable = ast2600_clk_enable, 1092550e691bSryan_chen }; 1093550e691bSryan_chen 1094d6e349c7Sryan_chen static int ast2600_clk_probe(struct udevice *dev) 1095550e691bSryan_chen { 1096f0d895afSryan_chen struct ast2600_clk_priv *priv = dev_get_priv(dev); 109761ab9607Sryan_chen u32 uart_clk_source; 1098550e691bSryan_chen 1099f0d895afSryan_chen priv->scu = devfdt_get_addr_ptr(dev); 1100f0d895afSryan_chen if (IS_ERR(priv->scu)) 1101f0d895afSryan_chen return PTR_ERR(priv->scu); 1102550e691bSryan_chen 1103b55086a6SChia-Wei, Wang uart_clk_source = dev_read_u32_default(dev, "uart-clk-source", 110461ab9607Sryan_chen 0x0); 110561ab9607Sryan_chen 110661ab9607Sryan_chen if(uart_clk_source) { 110756dd3e85Sryan_chen if(uart_clk_source & GENMASK(5, 0)) 110856dd3e85Sryan_chen setbits_le32(&priv->scu->clk_sel4, uart_clk_source & GENMASK(5, 0)); 110956dd3e85Sryan_chen if(uart_clk_source & GENMASK(12, 6)) 111056dd3e85Sryan_chen setbits_le32(&priv->scu->clk_sel5, uart_clk_source & GENMASK(12, 6)); 111161ab9607Sryan_chen } 111261ab9607Sryan_chen 1113b89500a2SDylan Hung 1114b89500a2SDylan Hung ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig); 1115b89500a2SDylan Hung ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig); 1116b89500a2SDylan Hung ast2600_configure_mac12_clk(priv->scu); 1117b89500a2SDylan Hung ast2600_configure_mac34_clk(priv->scu); 1118b89500a2SDylan Hung 1119550e691bSryan_chen return 0; 1120550e691bSryan_chen } 1121550e691bSryan_chen 1122d6e349c7Sryan_chen static int ast2600_clk_bind(struct udevice *dev) 1123550e691bSryan_chen { 1124550e691bSryan_chen int ret; 1125550e691bSryan_chen 1126550e691bSryan_chen /* The reset driver does not have a device node, so bind it here */ 1127550e691bSryan_chen ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev); 1128550e691bSryan_chen if (ret) 1129550e691bSryan_chen debug("Warning: No reset driver: ret=%d\n", ret); 1130550e691bSryan_chen 1131550e691bSryan_chen return 0; 1132550e691bSryan_chen } 1133550e691bSryan_chen 1134d35ac78cSryan_chen #if CONFIG_IS_ENABLED(CMD_CLK) 1135d35ac78cSryan_chen struct aspeed_clks { 1136d35ac78cSryan_chen ulong id; 1137d35ac78cSryan_chen const char *name; 1138d35ac78cSryan_chen }; 1139d35ac78cSryan_chen 1140d35ac78cSryan_chen static struct aspeed_clks aspeed_clk_names[] = { 1141d35ac78cSryan_chen { ASPEED_CLK_HPLL, "hpll" }, 1142d35ac78cSryan_chen { ASPEED_CLK_MPLL, "mpll" }, 1143d35ac78cSryan_chen { ASPEED_CLK_APLL, "apll" }, 1144d35ac78cSryan_chen { ASPEED_CLK_EPLL, "epll" }, 1145d35ac78cSryan_chen { ASPEED_CLK_DPLL, "dpll" }, 1146d35ac78cSryan_chen { ASPEED_CLK_AHB, "hclk" }, 11476fa1ef3dSryan_chen { ASPEED_CLK_APB1, "pclk1" }, 11486fa1ef3dSryan_chen { ASPEED_CLK_APB2, "pclk2" }, 11492e195992Sryan_chen { ASPEED_CLK_UARTX, "uxclk" }, 1150def99fcbSryan_chen { ASPEED_CLK_HUARTX, "huxclk" }, 1151d35ac78cSryan_chen }; 1152d35ac78cSryan_chen 1153d35ac78cSryan_chen int soc_clk_dump(void) 1154d35ac78cSryan_chen { 1155d35ac78cSryan_chen struct udevice *dev; 1156d35ac78cSryan_chen struct clk clk; 1157d35ac78cSryan_chen unsigned long rate; 1158d35ac78cSryan_chen int i, ret; 1159d35ac78cSryan_chen 1160d35ac78cSryan_chen ret = uclass_get_device_by_driver(UCLASS_CLK, 1161d35ac78cSryan_chen DM_GET_DRIVER(aspeed_scu), &dev); 1162d35ac78cSryan_chen if (ret) 1163d35ac78cSryan_chen return ret; 1164d35ac78cSryan_chen 1165d35ac78cSryan_chen printf("Clk\t\tHz\n"); 1166d35ac78cSryan_chen 1167d35ac78cSryan_chen for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) { 1168d35ac78cSryan_chen clk.id = aspeed_clk_names[i].id; 1169d35ac78cSryan_chen ret = clk_request(dev, &clk); 1170d35ac78cSryan_chen if (ret < 0) { 1171d35ac78cSryan_chen debug("%s clk_request() failed: %d\n", __func__, ret); 1172d35ac78cSryan_chen continue; 1173d35ac78cSryan_chen } 1174d35ac78cSryan_chen 1175d35ac78cSryan_chen ret = clk_get_rate(&clk); 1176d35ac78cSryan_chen rate = ret; 1177d35ac78cSryan_chen 1178d35ac78cSryan_chen clk_free(&clk); 1179d35ac78cSryan_chen 1180d35ac78cSryan_chen if (ret == -ENOTSUPP) { 1181d35ac78cSryan_chen printf("clk ID %lu not supported yet\n", 1182d35ac78cSryan_chen aspeed_clk_names[i].id); 1183d35ac78cSryan_chen continue; 1184d35ac78cSryan_chen } 1185d35ac78cSryan_chen if (ret < 0) { 1186d35ac78cSryan_chen printf("%s %lu: get_rate err: %d\n", 1187d35ac78cSryan_chen __func__, aspeed_clk_names[i].id, ret); 1188d35ac78cSryan_chen continue; 1189d35ac78cSryan_chen } 1190d35ac78cSryan_chen 1191d35ac78cSryan_chen printf("%s(%3lu):\t%lu\n", 1192d35ac78cSryan_chen aspeed_clk_names[i].name, aspeed_clk_names[i].id, rate); 1193d35ac78cSryan_chen } 1194d35ac78cSryan_chen 1195d35ac78cSryan_chen return 0; 1196d35ac78cSryan_chen } 1197d35ac78cSryan_chen #endif 1198d35ac78cSryan_chen 1199d6e349c7Sryan_chen static const struct udevice_id ast2600_clk_ids[] = { 1200d6e349c7Sryan_chen { .compatible = "aspeed,ast2600-scu", }, 1201550e691bSryan_chen { } 1202550e691bSryan_chen }; 1203550e691bSryan_chen 1204aa36597fSDylan Hung U_BOOT_DRIVER(aspeed_scu) = { 1205aa36597fSDylan Hung .name = "aspeed_scu", 1206550e691bSryan_chen .id = UCLASS_CLK, 1207d6e349c7Sryan_chen .of_match = ast2600_clk_ids, 1208f0d895afSryan_chen .priv_auto_alloc_size = sizeof(struct ast2600_clk_priv), 1209f9aa0ee1Sryan_chen .ops = &ast2600_clk_ops, 1210d6e349c7Sryan_chen .bind = ast2600_clk_bind, 1211d6e349c7Sryan_chen .probe = ast2600_clk_probe, 1212550e691bSryan_chen }; 1213