1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) ASPEED Technology Inc. 4 */ 5 6 #include <common.h> 7 #include <linux/bitfield.h> 8 #include <linux/bitops.h> 9 #include <linux/iopoll.h> 10 #include <clk-uclass.h> 11 #include <dm.h> 12 #include <asm/io.h> 13 #include <dm/lists.h> 14 #include <asm/arch/scu_ast2600.h> 15 #include <dt-bindings/clock/ast2600-clock.h> 16 #include <dt-bindings/reset/ast2600-reset.h> 17 18 /* 19 * SCU 80 & 90 clock stop control for MAC controllers 20 */ 21 #define SCU_CLKSTOP_MAC1 (20) 22 #define SCU_CLKSTOP_MAC2 (21) 23 #define SCU_CLKSTOP_MAC3 (20) 24 #define SCU_CLKSTOP_MAC4 (21) 25 26 /* 27 * MAC Clock Delay settings 28 */ 29 #define MAC_CLK_RGMII_125M_SRC_SEL BIT(31) 30 #define MAC_CLK_RGMII_125M_SRC_PAD_RGMIICK 0 31 #define MAC_CLK_RGMII_125M_SRC_PLL 1 32 #define MAC_CLK_RMII2_50M_RCLK_O_CTRL BIT(30) 33 #define MAC_CLK_RMII2_50M_RCLK_O_DIS 0 34 #define MAC_CLK_RMII2_50M_RCLK_O_EN 1 35 #define MAC_CLK_RMII1_50M_RCLK_O_CTRL BIT(29) 36 #define MAC_CLK_RMII1_5M_RCLK_O_DIS 0 37 #define MAC_CLK_RMII1_5M_RCLK_O_EN 1 38 #define MAC_CLK_RGMIICK_PAD_DIR BIT(28) 39 #define MAC_CLK_RGMIICK_PAD_DIR_INPUT 0 40 #define MAC_CLK_RGMIICK_PAD_DIR_OUTPUT 1 41 #define MAC_CLK_RMII_TXD_FALLING_2 BIT(27) 42 #define MAC_CLK_RMII_TXD_FALLING_1 BIT(26) 43 #define MAC_CLK_RXCLK_INV_2 BIT(25) 44 #define MAC_CLK_RXCLK_INV_1 BIT(24) 45 #define MAC_CLK_1G_INPUT_DELAY_2 GENMASK(23, 18) 46 #define MAC_CLK_1G_INPUT_DELAY_1 GENMASK(17, 12) 47 #define MAC_CLK_1G_OUTPUT_DELAY_2 GENMASK(11, 6) 48 #define MAC_CLK_1G_OUTPUT_DELAY_1 GENMASK(5, 0) 49 50 #define MAC_CLK_100M_10M_RESERVED GENMASK(31, 26) 51 #define MAC_CLK_100M_10M_RXCLK_INV_2 BIT(25) 52 #define MAC_CLK_100M_10M_RXCLK_INV_1 BIT(24) 53 #define MAC_CLK_100M_10M_INPUT_DELAY_2 GENMASK(23, 18) 54 #define MAC_CLK_100M_10M_INPUT_DELAY_1 GENMASK(17, 12) 55 #define MAC_CLK_100M_10M_OUTPUT_DELAY_2 GENMASK(11, 6) 56 #define MAC_CLK_100M_10M_OUTPUT_DELAY_1 GENMASK(5, 0) 57 58 #define RGMII12_CLK_OUTPUT_DELAY_PS 1000 59 #define RGMII34_CLK_OUTPUT_DELAY_PS 1600 60 61 #define MAC_DEF_DELAY_1G FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_1, 16) | \ 62 FIELD_PREP(MAC_CLK_1G_INPUT_DELAY_1, 10) | \ 63 FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_2, 16) | \ 64 FIELD_PREP(MAC_CLK_1G_INPUT_DELAY_2, 10) 65 #define MAC_DEF_DELAY_100M FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, 16) | \ 66 FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_1, 16) | \ 67 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, 16) | \ 68 FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_2, 16) 69 #define MAC_DEF_DELAY_10M FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, 16) | \ 70 FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_1, 16) | \ 71 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, 16) | \ 72 FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_2, 16) 73 #define MAC34_DEF_DELAY_1G FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_1, 8) | \ 74 FIELD_PREP(MAC_CLK_1G_INPUT_DELAY_1, 4) | \ 75 FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_2, 8) | \ 76 FIELD_PREP(MAC_CLK_1G_INPUT_DELAY_2, 4) 77 #define MAC34_DEF_DELAY_100M FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, 8) | \ 78 FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_1, 4) | \ 79 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, 8) | \ 80 FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_2, 4) 81 #define MAC34_DEF_DELAY_10M FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, 8) | \ 82 FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_1, 4) | \ 83 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, 8) | \ 84 FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_2, 4) 85 86 /* 87 * SCU 320 & 330 Frequency counters 88 */ 89 #define FREQC_CTRL_RESERVED GENMASK(31, 30) 90 #define FREQC_CTRL_RESULT GENMASK(29, 16) 91 #define FREQC_CTRL_RING_STAGE GENMASK(15, 9) 92 #define FREQC_CTRL_PIN_O_CTRL BIT(8) 93 #define FREQC_CTRL_PIN_O_DIS 0 94 #define FREQC_CTRL_PIN_O_EN 1 95 #define FREQC_CTRL_CMP_RESULT BIT(7) 96 #define FREQC_CTRL_CMP_RESULT_FAIL 0 97 #define FREQC_CTRL_CMP_RESULT_PASS 1 98 #define FREQC_CTRL_STATUS BIT(6) 99 #define FREQC_CTRL_STATUS_NOT_FINISHED 0 100 #define FREQC_CTRL_STATUS_FINISHED 1 101 #define FREQC_CTRL_SRC_SEL GENMASK(5, 2) 102 #define FREQC_CTRL_SRC_SEL_HCLK_DIE0 9 103 #define FREQC_CTRL_SRC_SEL_DLY32_DIE0 3 104 #define FREQC_CTRL_SRC_SEL_HCLK_DIE1 1 105 #define FREQC_CTRL_SRC_SEL_DLY32_DIE1 7 106 #define FREQC_CTRL_OSC_CTRL BIT(1) 107 #define FREQC_CTRL_OSC_DIS 0 108 #define FREQC_CTRL_OSC_EN 1 109 #define FREQC_CTRL_RING_CTRL BIT(0) 110 #define FREQC_CTRL_RING_DIS 0 111 #define FREQC_CTRL_RING_EN 1 112 113 #define FREQC_RANGE_RESERVED0 GENMASK(31, 30) 114 #define FREQC_RANGE_LOWER GENMASK(29, 16) 115 #define FREQC_RANGE_RESERVED1 GENMASK(15, 14) 116 #define FREQC_RANGE_UPPER GENMASK(13, 0) 117 118 #define DLY32_NUM_OF_TAPS 32 119 #define DLY32_AVERAGE_COUNT_LOG2 4 120 #define DLY32_AVERAGE_COUNT BIT(DLY32_AVERAGE_COUNT_LOG2) 121 122 /* 123 * TGMII Clock Duty constants, taken from Aspeed SDK 124 */ 125 #define RGMII2_TXCK_DUTY 0x66 126 #define RGMII1_TXCK_DUTY 0x64 127 #define D2PLL_DEFAULT_RATE (250 * 1000 * 1000) 128 #define CHIP_REVISION_ID GENMASK(23, 16) 129 130 DECLARE_GLOBAL_DATA_PTR; 131 132 /* 133 * Clock divider/multiplier configuration struct. 134 * For H-PLL and M-PLL the formula is 135 * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1) 136 * M - Numerator 137 * N - Denumerator 138 * P - Post Divider 139 * They have the same layout in their control register. 140 * 141 * D-PLL and D2-PLL have extra divider (OD + 1), which is not 142 * yet needed and ignored by clock configurations. 143 */ 144 union ast2600_pll_reg { 145 u32 w; 146 struct { 147 unsigned int m : 13; /* bit[12:0] */ 148 unsigned int n : 6; /* bit[18:13] */ 149 unsigned int p : 4; /* bit[22:19] */ 150 unsigned int off : 1; /* bit[23] */ 151 unsigned int bypass : 1; /* bit[24] */ 152 unsigned int reset : 1; /* bit[25] */ 153 unsigned int reserved : 6; /* bit[31:26] */ 154 } b; 155 }; 156 157 struct ast2600_pll_cfg { 158 union ast2600_pll_reg reg; 159 u32 ext_reg; 160 }; 161 162 struct ast2600_pll_desc { 163 u32 in; 164 u32 out; 165 struct ast2600_pll_cfg cfg; 166 }; 167 168 static const struct ast2600_pll_desc ast2600_pll_lookup[] = { 169 { 170 .in = AST2600_CLK_IN, 171 .out = 400000000, 172 .cfg.reg.b.m = 95, 173 .cfg.reg.b.n = 2, 174 .cfg.reg.b.p = 1, 175 .cfg.ext_reg = 0x31, 176 }, { 177 .in = AST2600_CLK_IN, 178 .out = 200000000, 179 .cfg.reg.b.m = 127, 180 .cfg.reg.b.n = 0, 181 .cfg.reg.b.p = 15, 182 .cfg.ext_reg = 0x3f, 183 }, { 184 .in = AST2600_CLK_IN, 185 .out = 334000000, 186 .cfg.reg.b.m = 667, 187 .cfg.reg.b.n = 4, 188 .cfg.reg.b.p = 9, 189 .cfg.ext_reg = 0x14d, 190 }, { 191 .in = AST2600_CLK_IN, 192 .out = 1000000000, 193 .cfg.reg.b.m = 119, 194 .cfg.reg.b.n = 2, 195 .cfg.reg.b.p = 0, 196 .cfg.ext_reg = 0x3d, 197 }, { 198 .in = AST2600_CLK_IN, 199 .out = 50000000, 200 .cfg.reg.b.m = 95, 201 .cfg.reg.b.n = 2, 202 .cfg.reg.b.p = 15, 203 .cfg.ext_reg = 0x31, 204 }, 205 }; 206 207 struct mac_delay_config { 208 u32 tx_delay_1000; 209 u32 rx_delay_1000; 210 u32 tx_delay_100; 211 u32 rx_delay_100; 212 u32 tx_delay_10; 213 u32 rx_delay_10; 214 }; 215 216 extern u32 ast2600_get_pll_rate(struct ast2600_scu *scu, int pll_idx) 217 { 218 u32 clkin = AST2600_CLK_IN; 219 u32 pll_reg = 0; 220 unsigned int mult, div = 1; 221 222 switch (pll_idx) { 223 case ASPEED_CLK_HPLL: 224 pll_reg = readl(&scu->h_pll_param); 225 break; 226 case ASPEED_CLK_MPLL: 227 pll_reg = readl(&scu->m_pll_param); 228 break; 229 case ASPEED_CLK_DPLL: 230 pll_reg = readl(&scu->d_pll_param); 231 break; 232 case ASPEED_CLK_EPLL: 233 pll_reg = readl(&scu->e_pll_param); 234 break; 235 } 236 if (pll_reg & BIT(24)) { 237 /* Pass through mode */ 238 mult = 1; 239 div = 1; 240 } else { 241 union ast2600_pll_reg reg; 242 /* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) 243 * HPLL Numerator (M) = fix 0x5F when SCU500[10]=1 244 * Fixed 0xBF when SCU500[10]=0 and SCU500[8]=1 245 * SCU200[12:0] (default 0x8F) when SCU510[10]=0 and SCU510[8]=0 246 * HPLL Denumerator (N) = SCU200[18:13] (default 0x2) 247 * HPLL Divider (P) = SCU200[22:19] (default 0x0) 248 * HPLL Bandwidth Adj (NB) = fix 0x2F when SCU500[10]=1 249 * Fixed 0x5F when SCU500[10]=0 and SCU500[8]=1 250 * SCU204[11:0] (default 0x31) when SCU500[10]=0 and SCU500[8]=0 251 */ 252 reg.w = pll_reg; 253 if (pll_idx == ASPEED_CLK_HPLL) { 254 u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap); 255 256 if (hwstrap1 & BIT(10)) { 257 reg.b.m = 0x5F; 258 } else { 259 if (hwstrap1 & BIT(8)) 260 reg.b.m = 0xBF; 261 /* Otherwise keep default 0x8F */ 262 } 263 } 264 mult = (reg.b.m + 1) / (reg.b.n + 1); 265 div = (reg.b.p + 1); 266 } 267 268 return ((clkin * mult) / div); 269 } 270 271 extern u32 ast2600_get_apll_rate(struct ast2600_scu *scu) 272 { 273 u32 hw_rev = readl(&scu->chip_id1); 274 u32 clkin = AST2600_CLK_IN; 275 u32 apll_reg = readl(&scu->a_pll_param); 276 unsigned int mult, div = 1; 277 278 if (((hw_rev & CHIP_REVISION_ID) >> 16) >= 2) { 279 //after A2 version 280 if (apll_reg & BIT(24)) { 281 /* Pass through mode */ 282 mult = 1; 283 div = 1; 284 } else { 285 /* F = 25Mhz * [(m + 1) / (n + 1)] / (p + 1) */ 286 u32 m = apll_reg & 0x1fff; 287 u32 n = (apll_reg >> 13) & 0x3f; 288 u32 p = (apll_reg >> 19) & 0xf; 289 290 mult = (m + 1); 291 div = (n + 1) * (p + 1); 292 } 293 } else { 294 if (apll_reg & BIT(20)) { 295 /* Pass through mode */ 296 mult = 1; 297 div = 1; 298 } else { 299 /* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */ 300 u32 m = (apll_reg >> 5) & 0x3f; 301 u32 od = (apll_reg >> 4) & 0x1; 302 u32 n = apll_reg & 0xf; 303 304 mult = (2 - od) * (m + 2); 305 div = n + 1; 306 } 307 } 308 309 return ((clkin * mult) / div); 310 } 311 312 static u32 ast2600_a0_axi_ahb_div_table[] = { 313 2, 314 2, 315 3, 316 4, 317 }; 318 319 static u32 ast2600_a1_axi_ahb_div0_table[] = { 320 3, 321 2, 322 3, 323 4, 324 }; 325 326 static u32 ast2600_a1_axi_ahb_div1_table[] = { 327 3, 328 4, 329 6, 330 8, 331 }; 332 333 static u32 ast2600_a1_axi_ahb_default_table[] = { 334 3, 4, 3, 4, 2, 2, 2, 2, 335 }; 336 337 static u32 ast2600_get_hclk(struct ast2600_scu *scu) 338 { 339 u32 hw_rev = readl(&scu->chip_id1); 340 u32 hwstrap1 = readl(&scu->hwstrap1.hwstrap); 341 u32 axi_div = 1; 342 u32 ahb_div = 0; 343 u32 rate = 0; 344 345 if ((hw_rev & CHIP_REVISION_ID) >> 16) { 346 //After A0 347 if (hwstrap1 & BIT(16)) { 348 ast2600_a1_axi_ahb_div1_table[0] = 349 ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 350 0x7] * 2; 351 axi_div = 1; 352 ahb_div = 353 ast2600_a1_axi_ahb_div1_table[(hwstrap1 >> 11) & 354 0x3]; 355 } else { 356 ast2600_a1_axi_ahb_div0_table[0] = 357 ast2600_a1_axi_ahb_default_table[(hwstrap1 >> 8) & 358 0x7]; 359 axi_div = 2; 360 ahb_div = 361 ast2600_a1_axi_ahb_div0_table[(hwstrap1 >> 11) & 362 0x3]; 363 } 364 } else { 365 //A0 : fix axi = hpll / 2 366 axi_div = 2; 367 ahb_div = ast2600_a0_axi_ahb_div_table[(hwstrap1 >> 11) & 0x3]; 368 } 369 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 370 371 return (rate / axi_div / ahb_div); 372 } 373 374 static u32 ast2600_get_bclk_rate(struct ast2600_scu *scu) 375 { 376 u32 rate; 377 u32 bclk_sel = (readl(&scu->clk_sel1) >> 20) & 0x7; 378 379 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 380 381 return (rate / ((bclk_sel + 1) * 4)); 382 } 383 384 static u32 ast2600_hpll_pclk1_div_table[] = { 385 4, 8, 12, 16, 20, 24, 28, 32, 386 }; 387 388 static u32 ast2600_hpll_pclk2_div_table[] = { 389 2, 4, 6, 8, 10, 12, 14, 16, 390 }; 391 392 static u32 ast2600_get_pclk1(struct ast2600_scu *scu) 393 { 394 u32 clk_sel1 = readl(&scu->clk_sel1); 395 u32 apb_div = ast2600_hpll_pclk1_div_table[((clk_sel1 >> 23) & 0x7)]; 396 u32 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 397 398 return (rate / apb_div); 399 } 400 401 static u32 ast2600_get_pclk2(struct ast2600_scu *scu) 402 { 403 u32 clk_sel4 = readl(&scu->clk_sel4); 404 u32 apb_div = ast2600_hpll_pclk2_div_table[((clk_sel4 >> 9) & 0x7)]; 405 u32 rate = ast2600_get_hclk(scu); 406 407 return (rate / apb_div); 408 } 409 410 static u32 ast2600_get_uxclk_in_rate(struct ast2600_scu *scu) 411 { 412 u32 clk_in = 0; 413 u32 uxclk_sel = readl(&scu->clk_sel5); 414 415 uxclk_sel &= 0x3; 416 switch (uxclk_sel) { 417 case 0: 418 clk_in = ast2600_get_apll_rate(scu) / 4; 419 break; 420 case 1: 421 clk_in = ast2600_get_apll_rate(scu) / 2; 422 break; 423 case 2: 424 clk_in = ast2600_get_apll_rate(scu); 425 break; 426 case 3: 427 clk_in = ast2600_get_hclk(scu); 428 break; 429 } 430 431 return clk_in; 432 } 433 434 static u32 ast2600_get_huxclk_in_rate(struct ast2600_scu *scu) 435 { 436 u32 clk_in = 0; 437 u32 huclk_sel = readl(&scu->clk_sel5); 438 439 huclk_sel = ((huclk_sel >> 3) & 0x3); 440 switch (huclk_sel) { 441 case 0: 442 clk_in = ast2600_get_apll_rate(scu) / 4; 443 break; 444 case 1: 445 clk_in = ast2600_get_apll_rate(scu) / 2; 446 break; 447 case 2: 448 clk_in = ast2600_get_apll_rate(scu); 449 break; 450 case 3: 451 clk_in = ast2600_get_hclk(scu); 452 break; 453 } 454 455 return clk_in; 456 } 457 458 static u32 ast2600_get_uart_uxclk_rate(struct ast2600_scu *scu) 459 { 460 u32 clk_in = ast2600_get_uxclk_in_rate(scu); 461 u32 div_reg = readl(&scu->uart_24m_ref_uxclk); 462 unsigned int mult, div; 463 464 u32 n = (div_reg >> 8) & 0x3ff; 465 u32 r = div_reg & 0xff; 466 467 mult = r; 468 div = (n * 2); 469 return (clk_in * mult) / div; 470 } 471 472 static u32 ast2600_get_uart_huxclk_rate(struct ast2600_scu *scu) 473 { 474 u32 clk_in = ast2600_get_huxclk_in_rate(scu); 475 u32 div_reg = readl(&scu->uart_24m_ref_huxclk); 476 477 unsigned int mult, div; 478 479 u32 n = (div_reg >> 8) & 0x3ff; 480 u32 r = div_reg & 0xff; 481 482 mult = r; 483 div = (n * 2); 484 return (clk_in * mult) / div; 485 } 486 487 static u32 ast2600_get_sdio_clk_rate(struct ast2600_scu *scu) 488 { 489 u32 clkin = 0; 490 u32 clk_sel = readl(&scu->clk_sel4); 491 u32 div = (clk_sel >> 28) & 0x7; 492 u32 hw_rev = readl(&scu->chip_id1); 493 494 if (clk_sel & BIT(8)) 495 clkin = ast2600_get_apll_rate(scu); 496 else 497 clkin = ast2600_get_hclk(scu); 498 499 div = (1 + div) * 2; 500 if (((hw_rev & GENMASK(23, 16)) >> 16) >= 2) 501 div = (div & 0xf) ? div : 1; 502 503 return (clkin / div); 504 } 505 506 static u32 ast2600_get_emmc_clk_rate(struct ast2600_scu *scu) 507 { 508 u32 mmc_clk_src = readl(&scu->clk_sel1); 509 u32 clkin; 510 u32 clk_sel = readl(&scu->clk_sel1); 511 u32 div = (clk_sel >> 12) & 0x7; 512 513 if (mmc_clk_src & BIT(11)) { 514 /* emmc clock comes from MPLL */ 515 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL); 516 div = (div + 1) * 2; 517 } else { 518 clkin = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 519 div = (div + 1) << 2; 520 } 521 522 return (clkin / div); 523 } 524 525 static u32 ast2600_get_uart_clk_rate(struct ast2600_scu *scu, int uart_idx) 526 { 527 u32 hicr9 = readl(0x1e789098); 528 u32 uart_sel = readl(&scu->clk_sel4); 529 u32 uart_sel5 = readl(&scu->clk_sel5); 530 ulong uart_clk = 0; 531 532 switch (uart_idx) { 533 case 1: 534 case 2: 535 case 3: 536 case 4: 537 hicr9 &= ~(BIT(uart_idx + 3)); 538 writel(hicr9, 0x1e789098); 539 case 6: 540 if (uart_sel & BIT(uart_idx - 1)) 541 uart_clk = ast2600_get_uart_huxclk_rate(scu); 542 else 543 uart_clk = ast2600_get_uart_uxclk_rate(scu); 544 break; 545 case 5: //24mhz is come form usb phy 48Mhz 546 { 547 u8 uart5_clk_sel = 0; 548 //high bit 549 if (readl(&scu->misc_ctrl1) & BIT(12)) 550 uart5_clk_sel = 0x2; 551 else 552 uart5_clk_sel = 0x0; 553 554 if (readl(&scu->clk_sel2) & BIT(14)) 555 uart5_clk_sel |= 0x1; 556 557 switch (uart5_clk_sel) { 558 case 0: 559 uart_clk = 24000000; 560 break; 561 case 1: 562 uart_clk = 192000000; 563 break; 564 case 2: 565 uart_clk = 24000000 / 13; 566 break; 567 case 3: 568 uart_clk = 192000000 / 13; 569 break; 570 } 571 } break; 572 case 7: 573 case 8: 574 case 9: 575 case 10: 576 case 11: 577 case 12: 578 case 13: 579 if (uart_sel5 & BIT(uart_idx - 1)) 580 uart_clk = ast2600_get_uart_huxclk_rate(scu); 581 else 582 uart_clk = ast2600_get_uart_uxclk_rate(scu); 583 break; 584 } 585 586 return uart_clk; 587 } 588 589 static ulong ast2600_clk_get_rate(struct clk *clk) 590 { 591 struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 592 ulong rate = 0; 593 594 switch (clk->id) { 595 case ASPEED_CLK_HPLL: 596 case ASPEED_CLK_EPLL: 597 case ASPEED_CLK_DPLL: 598 case ASPEED_CLK_MPLL: 599 rate = ast2600_get_pll_rate(priv->scu, clk->id); 600 break; 601 case ASPEED_CLK_AHB: 602 rate = ast2600_get_hclk(priv->scu); 603 break; 604 case ASPEED_CLK_APB1: 605 rate = ast2600_get_pclk1(priv->scu); 606 break; 607 case ASPEED_CLK_APB2: 608 rate = ast2600_get_pclk2(priv->scu); 609 break; 610 case ASPEED_CLK_APLL: 611 rate = ast2600_get_apll_rate(priv->scu); 612 break; 613 case ASPEED_CLK_GATE_UART1CLK: 614 rate = ast2600_get_uart_clk_rate(priv->scu, 1); 615 break; 616 case ASPEED_CLK_GATE_UART2CLK: 617 rate = ast2600_get_uart_clk_rate(priv->scu, 2); 618 break; 619 case ASPEED_CLK_GATE_UART3CLK: 620 rate = ast2600_get_uart_clk_rate(priv->scu, 3); 621 break; 622 case ASPEED_CLK_GATE_UART4CLK: 623 rate = ast2600_get_uart_clk_rate(priv->scu, 4); 624 break; 625 case ASPEED_CLK_GATE_UART5CLK: 626 rate = ast2600_get_uart_clk_rate(priv->scu, 5); 627 break; 628 case ASPEED_CLK_BCLK: 629 rate = ast2600_get_bclk_rate(priv->scu); 630 break; 631 case ASPEED_CLK_SDIO: 632 rate = ast2600_get_sdio_clk_rate(priv->scu); 633 break; 634 case ASPEED_CLK_EMMC: 635 rate = ast2600_get_emmc_clk_rate(priv->scu); 636 break; 637 case ASPEED_CLK_UARTX: 638 rate = ast2600_get_uart_uxclk_rate(priv->scu); 639 break; 640 case ASPEED_CLK_HUARTX: 641 rate = ast2600_get_uart_huxclk_rate(priv->scu); 642 break; 643 default: 644 pr_debug("can't get clk rate\n"); 645 return -ENOENT; 646 } 647 648 return rate; 649 } 650 651 /** 652 * @brief lookup PLL divider config by input/output rate 653 * @param[in] *pll - PLL descriptor 654 * @return true - if PLL divider config is found, false - else 655 * The function caller shall fill "pll->in" and "pll->out", 656 * then this function will search the lookup table 657 * to find a valid PLL divider configuration. 658 */ 659 static bool ast2600_search_clock_config(struct ast2600_pll_desc *pll) 660 { 661 u32 i; 662 bool is_found = false; 663 664 for (i = 0; i < ARRAY_SIZE(ast2600_pll_lookup); i++) { 665 const struct ast2600_pll_desc *def_cfg = &ast2600_pll_lookup[i]; 666 667 if (def_cfg->in == pll->in && def_cfg->out == pll->out) { 668 is_found = true; 669 pll->cfg.reg.w = def_cfg->cfg.reg.w; 670 pll->cfg.ext_reg = def_cfg->cfg.ext_reg; 671 break; 672 } 673 } 674 return is_found; 675 } 676 677 static u32 ast2600_configure_pll(struct ast2600_scu *scu, 678 struct ast2600_pll_cfg *p_cfg, int pll_idx) 679 { 680 u32 addr, addr_ext; 681 u32 reg; 682 683 switch (pll_idx) { 684 case ASPEED_CLK_HPLL: 685 addr = (u32)(&scu->h_pll_param); 686 addr_ext = (u32)(&scu->h_pll_ext_param); 687 break; 688 case ASPEED_CLK_MPLL: 689 addr = (u32)(&scu->m_pll_param); 690 addr_ext = (u32)(&scu->m_pll_ext_param); 691 break; 692 case ASPEED_CLK_DPLL: 693 addr = (u32)(&scu->d_pll_param); 694 addr_ext = (u32)(&scu->d_pll_ext_param); 695 break; 696 case ASPEED_CLK_EPLL: 697 addr = (u32)(&scu->e_pll_param); 698 addr_ext = (u32)(&scu->e_pll_ext_param); 699 break; 700 default: 701 debug("unknown PLL index\n"); 702 return 1; 703 } 704 705 p_cfg->reg.b.bypass = 0; 706 p_cfg->reg.b.off = 1; 707 p_cfg->reg.b.reset = 1; 708 709 reg = readl(addr); 710 reg &= ~GENMASK(25, 0); 711 reg |= p_cfg->reg.w; 712 writel(reg, addr); 713 714 /* write extend parameter */ 715 writel(p_cfg->ext_reg, addr_ext); 716 udelay(100); 717 p_cfg->reg.b.off = 0; 718 p_cfg->reg.b.reset = 0; 719 reg &= ~GENMASK(25, 0); 720 reg |= p_cfg->reg.w; 721 writel(reg, addr); 722 while (!(readl(addr_ext) & BIT(31))) 723 ; 724 725 return 0; 726 } 727 728 static u32 ast2600_configure_ddr(struct ast2600_scu *scu, ulong rate) 729 { 730 struct ast2600_pll_desc mpll; 731 732 mpll.in = AST2600_CLK_IN; 733 mpll.out = rate; 734 if (ast2600_search_clock_config(&mpll) == false) { 735 printf("error!! unable to find valid DDR clock setting\n"); 736 return 0; 737 } 738 ast2600_configure_pll(scu, &mpll.cfg, ASPEED_CLK_MPLL); 739 740 return ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL); 741 } 742 743 static ulong ast2600_clk_set_rate(struct clk *clk, ulong rate) 744 { 745 struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 746 ulong new_rate; 747 748 switch (clk->id) { 749 case ASPEED_CLK_MPLL: 750 new_rate = ast2600_configure_ddr(priv->scu, rate); 751 break; 752 default: 753 return -ENOENT; 754 } 755 756 return new_rate; 757 } 758 759 static int ast2600_calc_dly32_time(struct ast2600_scu *scu, int die_id, int stage) 760 { 761 int ret, i; 762 u64 sum = 0; 763 u32 base, value, reset_sel, dly32_sel; 764 765 if (die_id) { 766 base = (u32)&scu->freq_counter_ctrl2; 767 reset_sel = FREQC_CTRL_SRC_SEL_HCLK_DIE1; 768 dly32_sel = FREQC_CTRL_SRC_SEL_DLY32_DIE1; 769 } else { 770 base = (u32)&scu->freq_counter_ctrl1; 771 reset_sel = FREQC_CTRL_SRC_SEL_HCLK_DIE0; 772 dly32_sel = FREQC_CTRL_SRC_SEL_DLY32_DIE0; 773 } 774 775 for (i = 0; i < DLY32_AVERAGE_COUNT; i++) { 776 /* reset frequency-counter */ 777 writel(FIELD_PREP(FREQC_CTRL_SRC_SEL, reset_sel), base); 778 ret = readl_poll_timeout(base, value, !(value & FREQC_CTRL_RESULT), 1000); 779 if (ret) 780 return -1; 781 782 /* start frequency counter */ 783 value = FIELD_PREP(FREQC_CTRL_RING_STAGE, stage) 784 | FIELD_PREP(FREQC_CTRL_SRC_SEL, dly32_sel) 785 | FIELD_PREP(FREQC_CTRL_RING_CTRL, FREQC_CTRL_RING_EN); 786 writel(value, base); 787 788 /* delay for a while for settling down */ 789 udelay(100); 790 791 /* enable osc for measurement */ 792 value |= FIELD_PREP(FREQC_CTRL_OSC_CTRL, FREQC_CTRL_OSC_EN); 793 writel(value, base); 794 ret = readl_poll_timeout(base, value, value & FREQC_CTRL_STATUS, 1000); 795 if (ret) 796 return -1; 797 798 /* the result is represented in T count, will translate to pico-second later */ 799 sum += FIELD_GET(FREQC_CTRL_RESULT, value); 800 } 801 802 /* return the DLY32 value in pico-second */ 803 return (2560000 / (int)(sum >> DLY32_AVERAGE_COUNT_LOG2)); 804 } 805 806 static void ast2600_init_dly32_lookup(struct ast2600_clk_priv *priv) 807 { 808 struct ast2600_scu *scu = priv->scu; 809 int i; 810 811 for (i = 0; i < DLY32_NUM_OF_TAPS; i++) { 812 priv->dly32_lookup[0][i] = ast2600_calc_dly32_time(scu, 0, i); 813 priv->dly32_lookup[1][i] = ast2600_calc_dly32_time(scu, 1, i); 814 } 815 816 #ifdef DEBUG 817 for (i = 0; i < DLY32_NUM_OF_TAPS; i++) 818 printf("28nm DLY32[%d] = %d ps\n", i, priv->dly32_lookup[0][i]); 819 820 for (i = 0; i < DLY32_NUM_OF_TAPS; i++) 821 printf("55nm DLY32[%d] = %d ps\n", i, priv->dly32_lookup[1][i]); 822 #endif 823 } 824 825 /** 826 * @brief find the DLY32 tap number fitting the target delay time 827 * 828 * @param target_pico_sec target delay time in pico-second 829 * @param lookup DLY32 lookup table 830 * @return int DLY32 tap number 831 */ 832 static int ast2600_find_dly32_tap(int target_pico_sec, int *lookup) 833 { 834 int i; 835 bool found = false; 836 837 for (i = 0; i < DLY32_NUM_OF_TAPS; i++) { 838 if (lookup[i] >= target_pico_sec) { 839 found = true; 840 break; 841 } 842 } 843 844 if (!found) 845 return -1; 846 847 return lookup[i]; 848 } 849 850 static u32 ast2600_configure_mac12_clk(struct ast2600_clk_priv *priv, struct udevice *dev) 851 { 852 struct ast2600_scu *scu = priv->scu; 853 struct mac_delay_config mac1_cfg, mac2_cfg; 854 u32 reg[3]; 855 int ret; 856 857 reg[0] = MAC_DEF_DELAY_1G; 858 reg[1] = MAC_DEF_DELAY_100M; 859 reg[2] = MAC_DEF_DELAY_10M; 860 861 ret = ast2600_find_dly32_tap(RGMII12_CLK_OUTPUT_DELAY_PS, priv->dly32_lookup[0]); 862 if (ret > 0) { 863 debug("suggested tx delay for mac1/2: %d\n", ret); 864 865 reg[0] &= ~(MAC_CLK_1G_OUTPUT_DELAY_1 | MAC_CLK_1G_OUTPUT_DELAY_2); 866 reg[0] |= FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_1, ret) | 867 FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_2, ret); 868 reg[1] &= ~(MAC_CLK_100M_10M_OUTPUT_DELAY_1 | MAC_CLK_100M_10M_OUTPUT_DELAY_2); 869 reg[1] |= FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, ret) | 870 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, ret); 871 reg[2] &= ~(MAC_CLK_100M_10M_OUTPUT_DELAY_1 | MAC_CLK_100M_10M_OUTPUT_DELAY_2); 872 reg[2] |= FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, ret) | 873 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, ret); 874 } 875 ret = dev_read_u32_array(dev, "mac0-clk-delay", (u32 *)&mac1_cfg, 876 sizeof(mac1_cfg) / sizeof(u32)); 877 if (!ret) { 878 reg[0] &= ~(MAC_CLK_1G_INPUT_DELAY_1 | MAC_CLK_1G_OUTPUT_DELAY_1); 879 reg[0] |= FIELD_PREP(MAC_CLK_1G_INPUT_DELAY_1, mac1_cfg.rx_delay_1000) | 880 FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_1, mac1_cfg.tx_delay_1000); 881 882 reg[1] &= ~(MAC_CLK_100M_10M_INPUT_DELAY_1 | MAC_CLK_100M_10M_OUTPUT_DELAY_1); 883 reg[1] |= FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_1, mac1_cfg.rx_delay_100) | 884 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, mac1_cfg.tx_delay_100); 885 886 reg[2] &= ~(MAC_CLK_100M_10M_INPUT_DELAY_1 | MAC_CLK_100M_10M_OUTPUT_DELAY_1); 887 reg[2] |= FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_1, mac1_cfg.rx_delay_10) | 888 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, mac1_cfg.tx_delay_10); 889 } 890 891 ret = dev_read_u32_array(dev, "mac1-clk-delay", (u32 *)&mac2_cfg, 892 sizeof(mac2_cfg) / sizeof(u32)); 893 if (!ret) { 894 reg[0] &= ~(MAC_CLK_1G_INPUT_DELAY_2 | MAC_CLK_1G_OUTPUT_DELAY_2); 895 reg[0] |= FIELD_PREP(MAC_CLK_1G_INPUT_DELAY_2, mac2_cfg.rx_delay_1000) | 896 FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_2, mac2_cfg.tx_delay_1000); 897 898 reg[1] &= ~(MAC_CLK_100M_10M_INPUT_DELAY_2 | MAC_CLK_100M_10M_OUTPUT_DELAY_2); 899 reg[1] |= FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_2, mac2_cfg.rx_delay_100) | 900 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, mac2_cfg.tx_delay_100); 901 902 reg[2] &= ~(MAC_CLK_100M_10M_INPUT_DELAY_2 | MAC_CLK_100M_10M_OUTPUT_DELAY_2); 903 reg[2] |= FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_2, mac2_cfg.rx_delay_10) | 904 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, mac2_cfg.tx_delay_10); 905 } 906 907 reg[0] |= (readl(&scu->mac12_clk_delay) & ~GENMASK(25, 0)); 908 writel(reg[0], &scu->mac12_clk_delay); 909 writel(reg[1], &scu->mac12_clk_delay_100M); 910 writel(reg[2], &scu->mac12_clk_delay_10M); 911 912 /* MAC AHB = HPLL / 6 */ 913 clrsetbits_le32(&scu->clk_sel1, GENMASK(18, 16), (0x2 << 16)); 914 915 return 0; 916 } 917 918 static u32 ast2600_configure_mac34_clk(struct ast2600_clk_priv *priv, struct udevice *dev) 919 { 920 struct ast2600_scu *scu = priv->scu; 921 struct mac_delay_config mac3_cfg, mac4_cfg; 922 u32 reg[3]; 923 int ret; 924 925 reg[0] = MAC34_DEF_DELAY_1G; 926 reg[1] = MAC34_DEF_DELAY_100M; 927 reg[2] = MAC34_DEF_DELAY_10M; 928 929 ret = ast2600_find_dly32_tap(RGMII34_CLK_OUTPUT_DELAY_PS, priv->dly32_lookup[1]); 930 if (ret > 0) { 931 debug("suggested tx delay for mac3/4: %d\n", ret); 932 933 reg[0] &= ~(MAC_CLK_1G_OUTPUT_DELAY_1 | MAC_CLK_1G_OUTPUT_DELAY_2); 934 reg[0] |= FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_1, ret) | 935 FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_2, ret); 936 reg[1] &= ~(MAC_CLK_100M_10M_OUTPUT_DELAY_1 | MAC_CLK_100M_10M_OUTPUT_DELAY_2); 937 reg[1] |= FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, ret) | 938 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, ret); 939 reg[2] &= ~(MAC_CLK_100M_10M_OUTPUT_DELAY_1 | MAC_CLK_100M_10M_OUTPUT_DELAY_2); 940 reg[2] |= FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, ret) | 941 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, ret); 942 } 943 944 ret = dev_read_u32_array(dev, "mac2-clk-delay", (u32 *)&mac3_cfg, sizeof(mac3_cfg) / sizeof(u32)); 945 if (!ret) { 946 reg[0] &= ~(MAC_CLK_1G_INPUT_DELAY_1 | MAC_CLK_1G_OUTPUT_DELAY_1); 947 reg[0] |= FIELD_PREP(MAC_CLK_1G_INPUT_DELAY_1, mac3_cfg.rx_delay_1000) | 948 FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_1, mac3_cfg.tx_delay_1000); 949 950 reg[1] &= ~(MAC_CLK_100M_10M_INPUT_DELAY_1 | MAC_CLK_100M_10M_OUTPUT_DELAY_1); 951 reg[1] |= FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_1, mac3_cfg.rx_delay_100) | 952 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, mac3_cfg.tx_delay_100); 953 954 reg[2] &= ~(MAC_CLK_100M_10M_INPUT_DELAY_1 | MAC_CLK_100M_10M_OUTPUT_DELAY_1); 955 reg[2] |= FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_1, mac3_cfg.rx_delay_10) | 956 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_1, mac3_cfg.tx_delay_10); 957 } 958 959 ret = dev_read_u32_array(dev, "mac3-clk-delay", (u32 *)&mac4_cfg, sizeof(mac4_cfg) / sizeof(u32)); 960 if (!ret) { 961 reg[0] &= ~(MAC_CLK_1G_INPUT_DELAY_2 | MAC_CLK_1G_OUTPUT_DELAY_2); 962 reg[0] |= FIELD_PREP(MAC_CLK_1G_INPUT_DELAY_2, mac4_cfg.rx_delay_1000) | 963 FIELD_PREP(MAC_CLK_1G_OUTPUT_DELAY_2, mac4_cfg.tx_delay_1000); 964 965 reg[1] &= ~(MAC_CLK_100M_10M_INPUT_DELAY_2 | MAC_CLK_100M_10M_OUTPUT_DELAY_2); 966 reg[1] |= FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_2, mac4_cfg.rx_delay_100) | 967 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, mac4_cfg.tx_delay_100); 968 969 reg[2] &= ~(MAC_CLK_100M_10M_INPUT_DELAY_2 | MAC_CLK_100M_10M_OUTPUT_DELAY_2); 970 reg[2] |= FIELD_PREP(MAC_CLK_100M_10M_INPUT_DELAY_2, mac4_cfg.rx_delay_10) | 971 FIELD_PREP(MAC_CLK_100M_10M_OUTPUT_DELAY_2, mac4_cfg.tx_delay_10); 972 } 973 974 reg[0] |= (readl(&scu->mac34_clk_delay) & ~GENMASK(25, 0)); 975 reg[0] &= ~MAC_CLK_RGMII_125M_SRC_SEL; 976 reg[0] |= FIELD_PREP(MAC_CLK_RGMII_125M_SRC_SEL, MAC_CLK_RGMII_125M_SRC_PAD_RGMIICK); 977 writel(reg[0], &scu->mac34_clk_delay); 978 writel(reg[1], &scu->mac34_clk_delay_100M); 979 writel(reg[2], &scu->mac34_clk_delay_10M); 980 981 /* 982 * clock source seletion and divider 983 * scu310[26:24] : MAC AHB bus clock = HCLK / 2 984 * scu310[18:16] : RMII 50M = HCLK_200M / 4 985 */ 986 clrsetbits_le32(&scu->clk_sel4, (GENMASK(26, 24) | GENMASK(18, 16)), 987 ((0x0 << 24) | (0x3 << 16))); 988 989 /* 990 * set driving strength 991 * scu458[3:2] : MAC4 driving strength 992 * scu458[1:0] : MAC3 driving strength 993 */ 994 clrsetbits_le32(&scu->pinmux_ctrl16, GENMASK(3, 0), 995 (0x3 << 2) | (0x3 << 0)); 996 997 return 0; 998 } 999 1000 /** 1001 * ast2600 RGMII clock source tree 1002 * 125M from external PAD -------->|\ 1003 * HPLL -->|\ | |---->RGMII 125M for MAC#1 & MAC#2 1004 * | |---->| divider |---->|/ + 1005 * EPLL -->|/ | 1006 * | 1007 * +---------<-----------|RGMIICK PAD output enable|<-------------+ 1008 * | 1009 * +--------------------------->|\ 1010 * | |----> RGMII 125M for MAC#3 & MAC#4 1011 * HCLK 200M ---->|divider|---->|/ 1012 * To simplify the control flow: 1013 * 1. RGMII 1/2 always use EPLL as the internal clock source 1014 * 2. RGMII 3/4 always use RGMIICK pad as the RGMII 125M source 1015 * 125M from external PAD -------->|\ 1016 * | |---->RGMII 125M for MAC#1 & MAC#2 1017 * EPLL---->| divider |--->|/ + 1018 * | 1019 * +<--------------------|RGMIICK PAD output enable|<-------------+ 1020 * | 1021 * +--------------------------->RGMII 125M for MAC#3 & MAC#4 1022 */ 1023 #define RGMIICK_SRC_PAD 0 1024 #define RGMIICK_SRC_EPLL 1 /* recommended */ 1025 #define RGMIICK_SRC_HPLL 2 1026 1027 #define RGMIICK_DIV2 1 1028 #define RGMIICK_DIV3 2 1029 #define RGMIICK_DIV4 3 1030 #define RGMIICK_DIV5 4 1031 #define RGMIICK_DIV6 5 1032 #define RGMIICK_DIV7 6 1033 #define RGMIICK_DIV8 7 /* recommended */ 1034 1035 #define RMIICK_DIV4 0 1036 #define RMIICK_DIV8 1 1037 #define RMIICK_DIV12 2 1038 #define RMIICK_DIV16 3 1039 #define RMIICK_DIV20 4 /* recommended */ 1040 #define RMIICK_DIV24 5 1041 #define RMIICK_DIV28 6 1042 #define RMIICK_DIV32 7 1043 1044 struct ast2600_mac_clk_div { 1045 u32 src; /* 0=external PAD, 1=internal PLL */ 1046 u32 fin; /* divider input speed */ 1047 u32 n; /* 0=div2, 1=div2, 2=div3, 3=div4,...,7=div8 */ 1048 u32 fout; /* fout = fin / n */ 1049 }; 1050 1051 struct ast2600_mac_clk_div rgmii_clk_defconfig = { 1052 .src = ASPEED_CLK_EPLL, 1053 .fin = 1000000000, 1054 .n = RGMIICK_DIV8, 1055 .fout = 125000000, 1056 }; 1057 1058 struct ast2600_mac_clk_div rmii_clk_defconfig = { 1059 .src = ASPEED_CLK_EPLL, 1060 .fin = 1000000000, 1061 .n = RMIICK_DIV20, 1062 .fout = 50000000, 1063 }; 1064 1065 static void ast2600_init_mac_pll(struct ast2600_scu *p_scu, 1066 struct ast2600_mac_clk_div *p_cfg) 1067 { 1068 struct ast2600_pll_desc pll; 1069 1070 pll.in = AST2600_CLK_IN; 1071 pll.out = p_cfg->fin; 1072 if (ast2600_search_clock_config(&pll) == false) { 1073 pr_err("unable to find valid ETHNET MAC clock setting\n"); 1074 debug("%s: pll cfg = 0x%08x 0x%08x\n", __func__, pll.cfg.reg.w, 1075 pll.cfg.ext_reg); 1076 debug("%s: pll cfg = %02x %02x %02x\n", __func__, 1077 pll.cfg.reg.b.m, pll.cfg.reg.b.n, pll.cfg.reg.b.p); 1078 return; 1079 } 1080 ast2600_configure_pll(p_scu, &pll.cfg, p_cfg->src); 1081 } 1082 1083 static void ast2600_init_rgmii_clk(struct ast2600_scu *p_scu, 1084 struct ast2600_mac_clk_div *p_cfg) 1085 { 1086 u32 reg_304 = readl(&p_scu->clk_sel2); 1087 u32 reg_340 = readl(&p_scu->mac12_clk_delay); 1088 u32 reg_350 = readl(&p_scu->mac34_clk_delay); 1089 1090 reg_340 &= ~(MAC_CLK_RGMII_125M_SRC_SEL | MAC_CLK_RMII2_50M_RCLK_O_CTRL | 1091 MAC_CLK_RMII1_50M_RCLK_O_CTRL | MAC_CLK_RGMIICK_PAD_DIR); 1092 /* RGMIICK PAD output enable (to MAC 3/4) */ 1093 reg_340 |= FIELD_PREP(MAC_CLK_RGMIICK_PAD_DIR, MAC_CLK_RGMIICK_PAD_DIR_OUTPUT); 1094 if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) { 1095 /* 1096 * re-init PLL if the current PLL output frequency doesn't match 1097 * the divider setting 1098 */ 1099 if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) 1100 ast2600_init_mac_pll(p_scu, p_cfg); 1101 /* select RGMII 125M from internal source */ 1102 reg_340 |= FIELD_PREP(MAC_CLK_RGMII_125M_SRC_SEL, MAC_CLK_RGMII_125M_SRC_PLL); 1103 } 1104 1105 reg_304 &= ~GENMASK(23, 20); 1106 1107 /* set clock divider */ 1108 reg_304 |= (p_cfg->n & 0x7) << 20; 1109 1110 /* select internal clock source */ 1111 if (p_cfg->src == ASPEED_CLK_HPLL) 1112 reg_304 |= BIT(23); 1113 1114 /* RGMII 3/4 clock source select */ 1115 reg_350 &= ~BIT(31); 1116 1117 writel(reg_304, &p_scu->clk_sel2); 1118 writel(reg_340, &p_scu->mac12_clk_delay); 1119 writel(reg_350, &p_scu->mac34_clk_delay); 1120 } 1121 1122 /** 1123 * ast2600 RMII/NCSI clock source tree 1124 * HPLL -->|\ 1125 * | |---->| divider |----> RMII 50M for MAC#1 & MAC#2 1126 * EPLL -->|/ 1127 * HCLK(SCLICLK)---->| divider |----> RMII 50M for MAC#3 & MAC#4 1128 */ 1129 static void ast2600_init_rmii_clk(struct ast2600_scu *p_scu, 1130 struct ast2600_mac_clk_div *p_cfg) 1131 { 1132 u32 reg_304; 1133 u32 reg_310; 1134 1135 if (p_cfg->src == ASPEED_CLK_EPLL || p_cfg->src == ASPEED_CLK_HPLL) { 1136 /* 1137 * re-init PLL if the current PLL output frequency doesn't match 1138 * the divider setting 1139 */ 1140 if (p_cfg->fin != ast2600_get_pll_rate(p_scu, p_cfg->src)) 1141 ast2600_init_mac_pll(p_scu, p_cfg); 1142 } 1143 1144 reg_304 = readl(&p_scu->clk_sel2); 1145 reg_310 = readl(&p_scu->clk_sel4); 1146 1147 reg_304 &= ~GENMASK(19, 16); 1148 1149 /* set RMII 1/2 clock divider */ 1150 reg_304 |= (p_cfg->n & 0x7) << 16; 1151 1152 /* RMII clock source selection */ 1153 if (p_cfg->src == ASPEED_CLK_HPLL) 1154 reg_304 |= BIT(19); 1155 1156 /* set RMII 3/4 clock divider */ 1157 reg_310 &= ~GENMASK(18, 16); 1158 reg_310 |= (0x3 << 16); 1159 1160 writel(reg_304, &p_scu->clk_sel2); 1161 writel(reg_310, &p_scu->clk_sel4); 1162 } 1163 1164 static u32 ast2600_configure_mac(struct ast2600_scu *scu, int index) 1165 { 1166 u32 reset_bit; 1167 u32 clkstop_bit; 1168 1169 switch (index) { 1170 case 1: 1171 reset_bit = BIT(ASPEED_RESET_MAC1); 1172 clkstop_bit = BIT(SCU_CLKSTOP_MAC1); 1173 writel(reset_bit, &scu->sysreset_ctrl1); 1174 udelay(100); 1175 writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1176 mdelay(10); 1177 writel(reset_bit, &scu->sysreset_clr_ctrl1); 1178 break; 1179 case 2: 1180 reset_bit = BIT(ASPEED_RESET_MAC2); 1181 clkstop_bit = BIT(SCU_CLKSTOP_MAC2); 1182 writel(reset_bit, &scu->sysreset_ctrl1); 1183 udelay(100); 1184 writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1185 mdelay(10); 1186 writel(reset_bit, &scu->sysreset_clr_ctrl1); 1187 break; 1188 case 3: 1189 reset_bit = BIT(ASPEED_RESET_MAC3 - 32); 1190 clkstop_bit = BIT(SCU_CLKSTOP_MAC3); 1191 writel(reset_bit, &scu->sysreset_ctrl2); 1192 udelay(100); 1193 writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 1194 mdelay(10); 1195 writel(reset_bit, &scu->sysreset_clr_ctrl2); 1196 break; 1197 case 4: 1198 reset_bit = BIT(ASPEED_RESET_MAC4 - 32); 1199 clkstop_bit = BIT(SCU_CLKSTOP_MAC4); 1200 writel(reset_bit, &scu->sysreset_ctrl2); 1201 udelay(100); 1202 writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 1203 mdelay(10); 1204 writel(reset_bit, &scu->sysreset_clr_ctrl2); 1205 break; 1206 default: 1207 return -EINVAL; 1208 } 1209 1210 return 0; 1211 } 1212 1213 #define SCU_CLK_ECC_RSA_FROM_HPLL_CLK BIT(19) 1214 #define SCU_CLK_ECC_RSA_CLK_MASK GENMASK(27, 26) 1215 #define SCU_CLK_ECC_RSA_CLK_DIV(x) ((x) << 26) 1216 static void ast2600_configure_rsa_ecc_clk(struct ast2600_scu *scu) 1217 { 1218 u32 clk_sel = readl(&scu->clk_sel1); 1219 1220 /* Configure RSA clock = HPLL/4 */ 1221 clk_sel |= SCU_CLK_ECC_RSA_FROM_HPLL_CLK; 1222 clk_sel &= ~SCU_CLK_ECC_RSA_CLK_MASK; 1223 clk_sel |= SCU_CLK_ECC_RSA_CLK_DIV(3); 1224 1225 writel(clk_sel, &scu->clk_sel1); 1226 } 1227 1228 #define SCU_CLKSTOP_SDIO 4 1229 static ulong ast2600_enable_sdclk(struct ast2600_scu *scu) 1230 { 1231 u32 reset_bit; 1232 u32 clkstop_bit; 1233 1234 reset_bit = BIT(ASPEED_RESET_SD - 32); 1235 clkstop_bit = BIT(SCU_CLKSTOP_SDIO); 1236 1237 writel(reset_bit, &scu->sysreset_ctrl2); 1238 1239 udelay(100); 1240 //enable clk 1241 writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 1242 mdelay(10); 1243 writel(reset_bit, &scu->sysreset_clr_ctrl2); 1244 1245 return 0; 1246 } 1247 1248 #define SCU_CLKSTOP_EXTSD 31 1249 #define SCU_CLK_SD_MASK (0x7 << 28) 1250 #define SCU_CLK_SD_DIV(x) ((x) << 28) 1251 #define SCU_CLK_SD_FROM_APLL_CLK BIT(8) 1252 1253 static ulong ast2600_enable_extsdclk(struct ast2600_scu *scu) 1254 { 1255 u32 clk_sel = readl(&scu->clk_sel4); 1256 u32 enableclk_bit; 1257 u32 rate = 0; 1258 u32 div = 0; 1259 int i = 0; 1260 1261 enableclk_bit = BIT(SCU_CLKSTOP_EXTSD); 1262 1263 /* ast2600 sd controller max clk is 200Mhz : 1264 * use apll for clock source 800/4 = 200 : controller max is 200mhz 1265 */ 1266 rate = ast2600_get_apll_rate(scu); 1267 for (i = 0; i < 8; i++) { 1268 div = (i + 1) * 2; 1269 if ((rate / div) <= 200000000) 1270 break; 1271 } 1272 clk_sel &= ~SCU_CLK_SD_MASK; 1273 clk_sel |= SCU_CLK_SD_DIV(i) | SCU_CLK_SD_FROM_APLL_CLK; 1274 writel(clk_sel, &scu->clk_sel4); 1275 1276 //enable clk 1277 setbits_le32(&scu->clk_sel4, enableclk_bit); 1278 1279 return 0; 1280 } 1281 1282 #define SCU_CLKSTOP_EMMC 27 1283 static ulong ast2600_enable_emmcclk(struct ast2600_scu *scu) 1284 { 1285 u32 reset_bit; 1286 u32 clkstop_bit; 1287 1288 reset_bit = BIT(ASPEED_RESET_EMMC); 1289 clkstop_bit = BIT(SCU_CLKSTOP_EMMC); 1290 1291 writel(reset_bit, &scu->sysreset_ctrl1); 1292 udelay(100); 1293 //enable clk 1294 writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1295 mdelay(10); 1296 writel(reset_bit, &scu->sysreset_clr_ctrl1); 1297 1298 return 0; 1299 } 1300 1301 #define SCU_CLKSTOP_EXTEMMC 15 1302 #define SCU_CLK_EMMC_MASK (0x7 << 12) 1303 #define SCU_CLK_EMMC_DIV(x) ((x) << 12) 1304 #define SCU_CLK_EMMC_FROM_MPLL_CLK BIT(11) 1305 1306 static ulong ast2600_enable_extemmcclk(struct ast2600_scu *scu) 1307 { 1308 u32 revision_id = readl(&scu->chip_id1); 1309 u32 clk_sel = readl(&scu->clk_sel1); 1310 u32 enableclk_bit = BIT(SCU_CLKSTOP_EXTEMMC); 1311 u32 rate = 0; 1312 u32 div = 0; 1313 int i = 0; 1314 1315 /* 1316 * ast2600 eMMC controller max clk is 200Mhz 1317 * HPll->1/2->|\ 1318 * |->SCU300[11]->SCU300[14:12][1/N] + 1319 * MPLL------>|/ | 1320 * +----------------------------------------------+ 1321 * | 1322 * +---------> EMMC12C[15:8][1/N]-> eMMC clk 1323 */ 1324 if (((revision_id & CHIP_REVISION_ID) >> 16)) { 1325 //AST2600A1 : use mpll to be clk source 1326 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_MPLL); 1327 for (i = 0; i < 8; i++) { 1328 div = (i + 1) * 2; 1329 if ((rate / div) <= 200000000) 1330 break; 1331 } 1332 1333 clk_sel &= ~SCU_CLK_EMMC_MASK; 1334 clk_sel |= SCU_CLK_EMMC_DIV(i) | SCU_CLK_EMMC_FROM_MPLL_CLK; 1335 writel(clk_sel, &scu->clk_sel1); 1336 1337 } else { 1338 //AST2600A0 : use hpll to be clk source 1339 rate = ast2600_get_pll_rate(scu, ASPEED_CLK_HPLL); 1340 1341 for (i = 0; i < 8; i++) { 1342 div = (i + 1) * 4; 1343 if ((rate / div) <= 200000000) 1344 break; 1345 } 1346 1347 clk_sel &= ~SCU_CLK_EMMC_MASK; 1348 clk_sel |= SCU_CLK_EMMC_DIV(i); 1349 writel(clk_sel, &scu->clk_sel1); 1350 } 1351 setbits_le32(&scu->clk_sel1, enableclk_bit); 1352 1353 return 0; 1354 } 1355 1356 #define SCU_CLKSTOP_FSICLK 30 1357 1358 static ulong ast2600_enable_fsiclk(struct ast2600_scu *scu) 1359 { 1360 u32 reset_bit; 1361 u32 clkstop_bit; 1362 1363 reset_bit = BIT(ASPEED_RESET_FSI % 32); 1364 clkstop_bit = BIT(SCU_CLKSTOP_FSICLK); 1365 1366 /* The FSI clock is shared between masters. If it's already on 1367 * don't touch it, as that will reset the existing master. 1368 */ 1369 if (!(readl(&scu->clk_stop_ctrl2) & clkstop_bit)) { 1370 debug("%s: already running, not touching it\n", __func__); 1371 return 0; 1372 } 1373 1374 writel(reset_bit, &scu->sysreset_ctrl2); 1375 udelay(100); 1376 writel(clkstop_bit, &scu->clk_stop_clr_ctrl2); 1377 mdelay(10); 1378 writel(reset_bit, &scu->sysreset_clr_ctrl2); 1379 1380 return 0; 1381 } 1382 1383 static ulong ast2600_enable_usbahclk(struct ast2600_scu *scu) 1384 { 1385 u32 reset_bit; 1386 u32 clkstop_bit; 1387 1388 reset_bit = BIT(ASPEED_RESET_EHCI_P1); 1389 clkstop_bit = BIT(14); 1390 1391 writel(reset_bit, &scu->sysreset_ctrl1); 1392 udelay(100); 1393 writel(clkstop_bit, &scu->clk_stop_ctrl1); 1394 mdelay(20); 1395 writel(reset_bit, &scu->sysreset_clr_ctrl1); 1396 1397 return 0; 1398 } 1399 1400 static ulong ast2600_enable_usbbhclk(struct ast2600_scu *scu) 1401 { 1402 u32 reset_bit; 1403 u32 clkstop_bit; 1404 1405 reset_bit = BIT(ASPEED_RESET_EHCI_P2); 1406 clkstop_bit = BIT(7); 1407 1408 writel(reset_bit, &scu->sysreset_ctrl1); 1409 udelay(100); 1410 writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1411 mdelay(20); 1412 1413 writel(reset_bit, &scu->sysreset_clr_ctrl1); 1414 1415 return 0; 1416 } 1417 1418 /* also known as yclk */ 1419 static ulong ast2600_enable_haceclk(struct ast2600_scu *scu) 1420 { 1421 u32 reset_bit; 1422 u32 clkstop_bit; 1423 1424 reset_bit = BIT(ASPEED_RESET_HACE); 1425 clkstop_bit = BIT(13); 1426 1427 writel(reset_bit, &scu->sysreset_ctrl1); 1428 udelay(100); 1429 writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1430 mdelay(20); 1431 1432 writel(reset_bit, &scu->sysreset_clr_ctrl1); 1433 1434 return 0; 1435 } 1436 1437 static ulong ast2600_enable_rsaeccclk(struct ast2600_scu *scu) 1438 { 1439 u32 clkstop_bit; 1440 1441 clkstop_bit = BIT(24); 1442 1443 writel(clkstop_bit, &scu->clk_stop_clr_ctrl1); 1444 mdelay(20); 1445 1446 return 0; 1447 } 1448 1449 static int ast2600_clk_enable(struct clk *clk) 1450 { 1451 struct ast2600_clk_priv *priv = dev_get_priv(clk->dev); 1452 1453 switch (clk->id) { 1454 case ASPEED_CLK_GATE_MAC1CLK: 1455 ast2600_configure_mac(priv->scu, 1); 1456 break; 1457 case ASPEED_CLK_GATE_MAC2CLK: 1458 ast2600_configure_mac(priv->scu, 2); 1459 break; 1460 case ASPEED_CLK_GATE_MAC3CLK: 1461 ast2600_configure_mac(priv->scu, 3); 1462 break; 1463 case ASPEED_CLK_GATE_MAC4CLK: 1464 ast2600_configure_mac(priv->scu, 4); 1465 break; 1466 case ASPEED_CLK_GATE_SDCLK: 1467 ast2600_enable_sdclk(priv->scu); 1468 break; 1469 case ASPEED_CLK_GATE_SDEXTCLK: 1470 ast2600_enable_extsdclk(priv->scu); 1471 break; 1472 case ASPEED_CLK_GATE_EMMCCLK: 1473 ast2600_enable_emmcclk(priv->scu); 1474 break; 1475 case ASPEED_CLK_GATE_EMMCEXTCLK: 1476 ast2600_enable_extemmcclk(priv->scu); 1477 break; 1478 case ASPEED_CLK_GATE_FSICLK: 1479 ast2600_enable_fsiclk(priv->scu); 1480 break; 1481 case ASPEED_CLK_GATE_USBPORT1CLK: 1482 ast2600_enable_usbahclk(priv->scu); 1483 break; 1484 case ASPEED_CLK_GATE_USBPORT2CLK: 1485 ast2600_enable_usbbhclk(priv->scu); 1486 break; 1487 case ASPEED_CLK_GATE_YCLK: 1488 ast2600_enable_haceclk(priv->scu); 1489 break; 1490 case ASPEED_CLK_GATE_RSAECCCLK: 1491 ast2600_enable_rsaeccclk(priv->scu); 1492 break; 1493 default: 1494 pr_err("can't enable clk\n"); 1495 return -ENOENT; 1496 } 1497 1498 return 0; 1499 } 1500 1501 struct clk_ops ast2600_clk_ops = { 1502 .get_rate = ast2600_clk_get_rate, 1503 .set_rate = ast2600_clk_set_rate, 1504 .enable = ast2600_clk_enable, 1505 }; 1506 1507 static int ast2600_clk_probe(struct udevice *dev) 1508 { 1509 struct ast2600_clk_priv *priv = dev_get_priv(dev); 1510 u32 uart_clk_source; 1511 1512 priv->scu = devfdt_get_addr_ptr(dev); 1513 if (IS_ERR(priv->scu)) 1514 return PTR_ERR(priv->scu); 1515 1516 uart_clk_source = dev_read_u32_default(dev, "uart-clk-source", 0x0); 1517 1518 if (uart_clk_source) { 1519 if (uart_clk_source & GENMASK(5, 0)) 1520 setbits_le32(&priv->scu->clk_sel4, 1521 uart_clk_source & GENMASK(5, 0)); 1522 if (uart_clk_source & GENMASK(12, 6)) 1523 setbits_le32(&priv->scu->clk_sel5, 1524 uart_clk_source & GENMASK(12, 6)); 1525 } 1526 1527 ast2600_init_rgmii_clk(priv->scu, &rgmii_clk_defconfig); 1528 ast2600_init_rmii_clk(priv->scu, &rmii_clk_defconfig); 1529 ast2600_init_dly32_lookup(priv); 1530 ast2600_configure_mac12_clk(priv, dev); 1531 ast2600_configure_mac34_clk(priv, dev); 1532 ast2600_configure_rsa_ecc_clk(priv->scu); 1533 1534 return 0; 1535 } 1536 1537 static int ast2600_clk_bind(struct udevice *dev) 1538 { 1539 int ret; 1540 1541 /* The reset driver does not have a device node, so bind it here */ 1542 ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev); 1543 if (ret) 1544 debug("Warning: No reset driver: ret=%d\n", ret); 1545 1546 return 0; 1547 } 1548 1549 struct aspeed_clks { 1550 ulong id; 1551 const char *name; 1552 }; 1553 1554 static struct aspeed_clks aspeed_clk_names[] = { 1555 { ASPEED_CLK_HPLL, "hpll" }, { ASPEED_CLK_MPLL, "mpll" }, 1556 { ASPEED_CLK_APLL, "apll" }, { ASPEED_CLK_EPLL, "epll" }, 1557 { ASPEED_CLK_DPLL, "dpll" }, { ASPEED_CLK_AHB, "hclk" }, 1558 { ASPEED_CLK_APB1, "pclk1" }, { ASPEED_CLK_APB2, "pclk2" }, 1559 { ASPEED_CLK_BCLK, "bclk" }, { ASPEED_CLK_UARTX, "uxclk" }, 1560 { ASPEED_CLK_HUARTX, "huxclk" }, 1561 }; 1562 1563 int soc_clk_dump(void) 1564 { 1565 struct udevice *dev; 1566 struct clk clk; 1567 unsigned long rate; 1568 int i, ret; 1569 1570 ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu), 1571 &dev); 1572 if (ret) 1573 return ret; 1574 1575 printf("Clk\t\tHz\n"); 1576 1577 for (i = 0; i < ARRAY_SIZE(aspeed_clk_names); i++) { 1578 clk.id = aspeed_clk_names[i].id; 1579 ret = clk_request(dev, &clk); 1580 if (ret < 0) { 1581 debug("%s clk_request() failed: %d\n", __func__, ret); 1582 continue; 1583 } 1584 1585 ret = clk_get_rate(&clk); 1586 rate = ret; 1587 1588 clk_free(&clk); 1589 1590 if (ret == -ENOTSUPP) { 1591 printf("clk ID %lu not supported yet\n", 1592 aspeed_clk_names[i].id); 1593 continue; 1594 } 1595 if (ret < 0) { 1596 printf("%s %lu: get_rate err: %d\n", __func__, 1597 aspeed_clk_names[i].id, ret); 1598 continue; 1599 } 1600 1601 printf("%s(%3lu):\t%lu\n", aspeed_clk_names[i].name, 1602 aspeed_clk_names[i].id, rate); 1603 } 1604 1605 return 0; 1606 } 1607 1608 static const struct udevice_id ast2600_clk_ids[] = { 1609 { 1610 .compatible = "aspeed,ast2600-scu", 1611 }, 1612 {} 1613 }; 1614 1615 U_BOOT_DRIVER(aspeed_scu) = { 1616 .name = "aspeed_scu", 1617 .id = UCLASS_CLK, 1618 .of_match = ast2600_clk_ids, 1619 .priv_auto_alloc_size = sizeof(struct ast2600_clk_priv), 1620 .ops = &ast2600_clk_ops, 1621 .bind = ast2600_clk_bind, 1622 .probe = ast2600_clk_probe, 1623 }; 1624