1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2019 Samsung Electronics Co., Ltd. 4 * Author: Lukasz Luba <l.luba@partner.samsung.com> 5 */ 6 7 #include <linux/clk.h> 8 #include <linux/devfreq.h> 9 #include <linux/devfreq-event.h> 10 #include <linux/device.h> 11 #include <linux/io.h> 12 #include <linux/mfd/syscon.h> 13 #include <linux/module.h> 14 #include <linux/of_device.h> 15 #include <linux/pm_opp.h> 16 #include <linux/platform_device.h> 17 #include <linux/regmap.h> 18 #include <linux/regulator/consumer.h> 19 #include <linux/slab.h> 20 #include "../jedec_ddr.h" 21 #include "../of_memory.h" 22 23 #define EXYNOS5_DREXI_TIMINGAREF (0x0030) 24 #define EXYNOS5_DREXI_TIMINGROW0 (0x0034) 25 #define EXYNOS5_DREXI_TIMINGDATA0 (0x0038) 26 #define EXYNOS5_DREXI_TIMINGPOWER0 (0x003C) 27 #define EXYNOS5_DREXI_TIMINGROW1 (0x00E4) 28 #define EXYNOS5_DREXI_TIMINGDATA1 (0x00E8) 29 #define EXYNOS5_DREXI_TIMINGPOWER1 (0x00EC) 30 #define CDREX_PAUSE (0x2091c) 31 #define CDREX_LPDDR3PHY_CON3 (0x20a20) 32 #define CDREX_LPDDR3PHY_CLKM_SRC (0x20700) 33 #define EXYNOS5_TIMING_SET_SWI BIT(28) 34 #define USE_MX_MSPLL_TIMINGS (1) 35 #define USE_BPLL_TIMINGS (0) 36 #define EXYNOS5_AREF_NORMAL (0x2e) 37 38 /** 39 * struct dmc_opp_table - Operating level desciption 40 * 41 * Covers frequency and voltage settings of the DMC operating mode. 42 */ 43 struct dmc_opp_table { 44 u32 freq_hz; 45 u32 volt_uv; 46 }; 47 48 /** 49 * struct exynos5_dmc - main structure describing DMC device 50 * 51 * The main structure for the Dynamic Memory Controller which covers clocks, 52 * memory regions, HW information, parameters and current operating mode. 53 */ 54 struct exynos5_dmc { 55 struct device *dev; 56 struct devfreq *df; 57 struct devfreq_simple_ondemand_data gov_data; 58 void __iomem *base_drexi0; 59 void __iomem *base_drexi1; 60 struct regmap *clk_regmap; 61 struct mutex lock; 62 unsigned long curr_rate; 63 unsigned long curr_volt; 64 unsigned long bypass_rate; 65 struct dmc_opp_table *opp; 66 struct dmc_opp_table opp_bypass; 67 int opp_count; 68 u32 timings_arr_size; 69 u32 *timing_row; 70 u32 *timing_data; 71 u32 *timing_power; 72 const struct lpddr3_timings *timings; 73 const struct lpddr3_min_tck *min_tck; 74 u32 bypass_timing_row; 75 u32 bypass_timing_data; 76 u32 bypass_timing_power; 77 struct regulator *vdd_mif; 78 struct clk *fout_spll; 79 struct clk *fout_bpll; 80 struct clk *mout_spll; 81 struct clk *mout_bpll; 82 struct clk *mout_mclk_cdrex; 83 struct clk *mout_mx_mspll_ccore; 84 struct clk *mx_mspll_ccore_phy; 85 struct clk *mout_mx_mspll_ccore_phy; 86 struct devfreq_event_dev **counter; 87 int num_counters; 88 }; 89 90 #define TIMING_FIELD(t_name, t_bit_beg, t_bit_end) \ 91 { .name = t_name, .bit_beg = t_bit_beg, .bit_end = t_bit_end } 92 93 #define TIMING_VAL2REG(timing, t_val) \ 94 ({ \ 95 u32 __val; \ 96 __val = (t_val) << (timing)->bit_beg; \ 97 __val; \ 98 }) 99 100 struct timing_reg { 101 char *name; 102 int bit_beg; 103 int bit_end; 104 unsigned int val; 105 }; 106 107 static const struct timing_reg timing_row[] = { 108 TIMING_FIELD("tRFC", 24, 31), 109 TIMING_FIELD("tRRD", 20, 23), 110 TIMING_FIELD("tRP", 16, 19), 111 TIMING_FIELD("tRCD", 12, 15), 112 TIMING_FIELD("tRC", 6, 11), 113 TIMING_FIELD("tRAS", 0, 5), 114 }; 115 116 static const struct timing_reg timing_data[] = { 117 TIMING_FIELD("tWTR", 28, 31), 118 TIMING_FIELD("tWR", 24, 27), 119 TIMING_FIELD("tRTP", 20, 23), 120 TIMING_FIELD("tW2W-C2C", 14, 14), 121 TIMING_FIELD("tR2R-C2C", 12, 12), 122 TIMING_FIELD("WL", 8, 11), 123 TIMING_FIELD("tDQSCK", 4, 7), 124 TIMING_FIELD("RL", 0, 3), 125 }; 126 127 static const struct timing_reg timing_power[] = { 128 TIMING_FIELD("tFAW", 26, 31), 129 TIMING_FIELD("tXSR", 16, 25), 130 TIMING_FIELD("tXP", 8, 15), 131 TIMING_FIELD("tCKE", 4, 7), 132 TIMING_FIELD("tMRD", 0, 3), 133 }; 134 135 #define TIMING_COUNT (ARRAY_SIZE(timing_row) + ARRAY_SIZE(timing_data) + \ 136 ARRAY_SIZE(timing_power)) 137 138 static int exynos5_counters_set_event(struct exynos5_dmc *dmc) 139 { 140 int i, ret; 141 142 for (i = 0; i < dmc->num_counters; i++) { 143 if (!dmc->counter[i]) 144 continue; 145 ret = devfreq_event_set_event(dmc->counter[i]); 146 if (ret < 0) 147 return ret; 148 } 149 return 0; 150 } 151 152 static int exynos5_counters_enable_edev(struct exynos5_dmc *dmc) 153 { 154 int i, ret; 155 156 for (i = 0; i < dmc->num_counters; i++) { 157 if (!dmc->counter[i]) 158 continue; 159 ret = devfreq_event_enable_edev(dmc->counter[i]); 160 if (ret < 0) 161 return ret; 162 } 163 return 0; 164 } 165 166 static int exynos5_counters_disable_edev(struct exynos5_dmc *dmc) 167 { 168 int i, ret; 169 170 for (i = 0; i < dmc->num_counters; i++) { 171 if (!dmc->counter[i]) 172 continue; 173 ret = devfreq_event_disable_edev(dmc->counter[i]); 174 if (ret < 0) 175 return ret; 176 } 177 return 0; 178 } 179 180 /** 181 * find_target_freq_id() - Finds requested frequency in local DMC configuration 182 * @dmc: device for which the information is checked 183 * @target_rate: requested frequency in KHz 184 * 185 * Seeks in the local DMC driver structure for the requested frequency value 186 * and returns index or error value. 187 */ 188 static int find_target_freq_idx(struct exynos5_dmc *dmc, 189 unsigned long target_rate) 190 { 191 int i; 192 193 for (i = dmc->opp_count - 1; i >= 0; i--) 194 if (dmc->opp[i].freq_hz <= target_rate) 195 return i; 196 197 return -EINVAL; 198 } 199 200 /** 201 * exynos5_switch_timing_regs() - Changes bank register set for DRAM timings 202 * @dmc: device for which the new settings is going to be applied 203 * @set: boolean variable passing set value 204 * 205 * Changes the register set, which holds timing parameters. 206 * There is two register sets: 0 and 1. The register set 0 207 * is used in normal operation when the clock is provided from main PLL. 208 * The bank register set 1 is used when the main PLL frequency is going to be 209 * changed and the clock is taken from alternative, stable source. 210 * This function switches between these banks according to the 211 * currently used clock source. 212 */ 213 static void exynos5_switch_timing_regs(struct exynos5_dmc *dmc, bool set) 214 { 215 unsigned int reg; 216 int ret; 217 218 ret = regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, ®); 219 220 if (set) 221 reg |= EXYNOS5_TIMING_SET_SWI; 222 else 223 reg &= ~EXYNOS5_TIMING_SET_SWI; 224 225 regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, reg); 226 } 227 228 /** 229 * exynos5_init_freq_table() - Initialized PM OPP framework 230 * @dmc: DMC device for which the frequencies are used for OPP init 231 * @profile: devfreq device's profile 232 * 233 * Populate the devfreq device's OPP table based on current frequency, voltage. 234 */ 235 static int exynos5_init_freq_table(struct exynos5_dmc *dmc, 236 struct devfreq_dev_profile *profile) 237 { 238 int i, ret; 239 int idx; 240 unsigned long freq; 241 242 ret = dev_pm_opp_of_add_table(dmc->dev); 243 if (ret < 0) { 244 dev_err(dmc->dev, "Failed to get OPP table\n"); 245 return ret; 246 } 247 248 dmc->opp_count = dev_pm_opp_get_opp_count(dmc->dev); 249 250 dmc->opp = devm_kmalloc_array(dmc->dev, dmc->opp_count, 251 sizeof(struct dmc_opp_table), GFP_KERNEL); 252 if (!dmc->opp) 253 goto err_opp; 254 255 idx = dmc->opp_count - 1; 256 for (i = 0, freq = ULONG_MAX; i < dmc->opp_count; i++, freq--) { 257 struct dev_pm_opp *opp; 258 259 opp = dev_pm_opp_find_freq_floor(dmc->dev, &freq); 260 if (IS_ERR(opp)) 261 goto err_opp; 262 263 dmc->opp[idx - i].freq_hz = freq; 264 dmc->opp[idx - i].volt_uv = dev_pm_opp_get_voltage(opp); 265 266 dev_pm_opp_put(opp); 267 } 268 269 return 0; 270 271 err_opp: 272 dev_pm_opp_of_remove_table(dmc->dev); 273 274 return -EINVAL; 275 } 276 277 /** 278 * exynos5_set_bypass_dram_timings() - Low-level changes of the DRAM timings 279 * @dmc: device for which the new settings is going to be applied 280 * @param: DRAM parameters which passes timing data 281 * 282 * Low-level function for changing timings for DRAM memory clocking from 283 * 'bypass' clock source (fixed frequency @400MHz). 284 * It uses timing bank registers set 1. 285 */ 286 static void exynos5_set_bypass_dram_timings(struct exynos5_dmc *dmc) 287 { 288 writel(EXYNOS5_AREF_NORMAL, 289 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF); 290 291 writel(dmc->bypass_timing_row, 292 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW1); 293 writel(dmc->bypass_timing_row, 294 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW1); 295 writel(dmc->bypass_timing_data, 296 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA1); 297 writel(dmc->bypass_timing_data, 298 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA1); 299 writel(dmc->bypass_timing_power, 300 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER1); 301 writel(dmc->bypass_timing_power, 302 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER1); 303 } 304 305 /** 306 * exynos5_dram_change_timings() - Low-level changes of the DRAM final timings 307 * @dmc: device for which the new settings is going to be applied 308 * @target_rate: target frequency of the DMC 309 * 310 * Low-level function for changing timings for DRAM memory operating from main 311 * clock source (BPLL), which can have different frequencies. Thus, each 312 * frequency must have corresponding timings register values in order to keep 313 * the needed delays. 314 * It uses timing bank registers set 0. 315 */ 316 static int exynos5_dram_change_timings(struct exynos5_dmc *dmc, 317 unsigned long target_rate) 318 { 319 int idx; 320 321 for (idx = dmc->opp_count - 1; idx >= 0; idx--) 322 if (dmc->opp[idx].freq_hz <= target_rate) 323 break; 324 325 if (idx < 0) 326 return -EINVAL; 327 328 writel(EXYNOS5_AREF_NORMAL, 329 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF); 330 331 writel(dmc->timing_row[idx], 332 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW0); 333 writel(dmc->timing_row[idx], 334 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW0); 335 writel(dmc->timing_data[idx], 336 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA0); 337 writel(dmc->timing_data[idx], 338 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA0); 339 writel(dmc->timing_power[idx], 340 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER0); 341 writel(dmc->timing_power[idx], 342 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER0); 343 344 return 0; 345 } 346 347 /** 348 * exynos5_dmc_align_target_voltage() - Sets the final voltage for the DMC 349 * @dmc: device for which it is going to be set 350 * @target_volt: new voltage which is chosen to be final 351 * 352 * Function tries to align voltage to the safe level for 'normal' mode. 353 * It checks the need of higher voltage and changes the value. The target 354 * voltage might be lower that currently set and still the system will be 355 * stable. 356 */ 357 static int exynos5_dmc_align_target_voltage(struct exynos5_dmc *dmc, 358 unsigned long target_volt) 359 { 360 int ret = 0; 361 362 if (dmc->curr_volt <= target_volt) 363 return 0; 364 365 ret = regulator_set_voltage(dmc->vdd_mif, target_volt, 366 target_volt); 367 if (!ret) 368 dmc->curr_volt = target_volt; 369 370 return ret; 371 } 372 373 /** 374 * exynos5_dmc_align_bypass_voltage() - Sets the voltage for the DMC 375 * @dmc: device for which it is going to be set 376 * @target_volt: new voltage which is chosen to be final 377 * 378 * Function tries to align voltage to the safe level for the 'bypass' mode. 379 * It checks the need of higher voltage and changes the value. 380 * The target voltage must not be less than currently needed, because 381 * for current frequency the device might become unstable. 382 */ 383 static int exynos5_dmc_align_bypass_voltage(struct exynos5_dmc *dmc, 384 unsigned long target_volt) 385 { 386 int ret = 0; 387 unsigned long bypass_volt = dmc->opp_bypass.volt_uv; 388 389 target_volt = max(bypass_volt, target_volt); 390 391 if (dmc->curr_volt >= target_volt) 392 return 0; 393 394 ret = regulator_set_voltage(dmc->vdd_mif, target_volt, 395 target_volt); 396 if (!ret) 397 dmc->curr_volt = target_volt; 398 399 return ret; 400 } 401 402 /** 403 * exynos5_dmc_align_bypass_dram_timings() - Chooses and sets DRAM timings 404 * @dmc: device for which it is going to be set 405 * @target_rate: new frequency which is chosen to be final 406 * 407 * Function changes the DRAM timings for the temporary 'bypass' mode. 408 */ 409 static int exynos5_dmc_align_bypass_dram_timings(struct exynos5_dmc *dmc, 410 unsigned long target_rate) 411 { 412 int idx = find_target_freq_idx(dmc, target_rate); 413 414 if (idx < 0) 415 return -EINVAL; 416 417 exynos5_set_bypass_dram_timings(dmc); 418 419 return 0; 420 } 421 422 /** 423 * exynos5_dmc_switch_to_bypass_configuration() - Switching to temporary clock 424 * @dmc: DMC device for which the switching is going to happen 425 * @target_rate: new frequency which is going to be set as a final 426 * @target_volt: new voltage which is going to be set as a final 427 * 428 * Function configures DMC and clocks for operating in temporary 'bypass' mode. 429 * This mode is used only temporary but if required, changes voltage and timings 430 * for DRAM chips. It switches the main clock to stable clock source for the 431 * period of the main PLL reconfiguration. 432 */ 433 static int 434 exynos5_dmc_switch_to_bypass_configuration(struct exynos5_dmc *dmc, 435 unsigned long target_rate, 436 unsigned long target_volt) 437 { 438 int ret; 439 440 /* 441 * Having higher voltage for a particular frequency does not harm 442 * the chip. Use it for the temporary frequency change when one 443 * voltage manipulation might be avoided. 444 */ 445 ret = exynos5_dmc_align_bypass_voltage(dmc, target_volt); 446 if (ret) 447 return ret; 448 449 /* 450 * Longer delays for DRAM does not cause crash, the opposite does. 451 */ 452 ret = exynos5_dmc_align_bypass_dram_timings(dmc, target_rate); 453 if (ret) 454 return ret; 455 456 /* 457 * Delays are long enough, so use them for the new coming clock. 458 */ 459 exynos5_switch_timing_regs(dmc, USE_MX_MSPLL_TIMINGS); 460 461 return ret; 462 } 463 464 /** 465 * exynos5_dmc_change_freq_and_volt() - Changes voltage and frequency of the DMC 466 * using safe procedure 467 * @dmc: device for which the frequency is going to be changed 468 * @target_rate: requested new frequency 469 * @target_volt: requested voltage which corresponds to the new frequency 470 * 471 * The DMC frequency change procedure requires a few steps. 472 * The main requirement is to change the clock source in the clk mux 473 * for the time of main clock PLL locking. The assumption is that the 474 * alternative clock source set as parent is stable. 475 * The second parent's clock frequency is fixed to 400MHz, it is named 'bypass' 476 * clock. This requires alignment in DRAM timing parameters for the new 477 * T-period. There is two bank sets for keeping DRAM 478 * timings: set 0 and set 1. The set 0 is used when main clock source is 479 * chosen. The 2nd set of regs is used for 'bypass' clock. Switching between 480 * the two bank sets is part of the process. 481 * The voltage must also be aligned to the minimum required level. There is 482 * this intermediate step with switching to 'bypass' parent clock source. 483 * if the old voltage is lower, it requires an increase of the voltage level. 484 * The complexity of the voltage manipulation is hidden in low level function. 485 * In this function there is last alignment of the voltage level at the end. 486 */ 487 static int 488 exynos5_dmc_change_freq_and_volt(struct exynos5_dmc *dmc, 489 unsigned long target_rate, 490 unsigned long target_volt) 491 { 492 int ret; 493 494 ret = exynos5_dmc_switch_to_bypass_configuration(dmc, target_rate, 495 target_volt); 496 if (ret) 497 return ret; 498 499 /* 500 * Voltage is set at least to a level needed for this frequency, 501 * so switching clock source is safe now. 502 */ 503 clk_prepare_enable(dmc->fout_spll); 504 clk_prepare_enable(dmc->mout_spll); 505 clk_prepare_enable(dmc->mout_mx_mspll_ccore); 506 507 ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_mx_mspll_ccore); 508 if (ret) 509 goto disable_clocks; 510 511 /* 512 * We are safe to increase the timings for current bypass frequency. 513 * Thanks to this the settings will be ready for the upcoming clock 514 * source change. 515 */ 516 exynos5_dram_change_timings(dmc, target_rate); 517 518 clk_set_rate(dmc->fout_bpll, target_rate); 519 520 exynos5_switch_timing_regs(dmc, USE_BPLL_TIMINGS); 521 522 ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_bpll); 523 if (ret) 524 goto disable_clocks; 525 526 /* 527 * Make sure if the voltage is not from 'bypass' settings and align to 528 * the right level for power efficiency. 529 */ 530 ret = exynos5_dmc_align_target_voltage(dmc, target_volt); 531 532 disable_clocks: 533 clk_disable_unprepare(dmc->mout_mx_mspll_ccore); 534 clk_disable_unprepare(dmc->mout_spll); 535 clk_disable_unprepare(dmc->fout_spll); 536 537 return ret; 538 } 539 540 /** 541 * exynos5_dmc_get_volt_freq() - Gets the frequency and voltage from the OPP 542 * table. 543 * @dmc: device for which the frequency is going to be changed 544 * @freq: requested frequency in KHz 545 * @target_rate: returned frequency which is the same or lower than 546 * requested 547 * @target_volt: returned voltage which corresponds to the returned 548 * frequency 549 * 550 * Function gets requested frequency and checks OPP framework for needed 551 * frequency and voltage. It populates the values 'target_rate' and 552 * 'target_volt' or returns error value when OPP framework fails. 553 */ 554 static int exynos5_dmc_get_volt_freq(struct exynos5_dmc *dmc, 555 unsigned long *freq, 556 unsigned long *target_rate, 557 unsigned long *target_volt, u32 flags) 558 { 559 struct dev_pm_opp *opp; 560 561 opp = devfreq_recommended_opp(dmc->dev, freq, flags); 562 if (IS_ERR(opp)) 563 return PTR_ERR(opp); 564 565 *target_rate = dev_pm_opp_get_freq(opp); 566 *target_volt = dev_pm_opp_get_voltage(opp); 567 dev_pm_opp_put(opp); 568 569 return 0; 570 } 571 572 /** 573 * exynos5_dmc_target() - Function responsible for changing frequency of DMC 574 * @dev: device for which the frequency is going to be changed 575 * @freq: requested frequency in KHz 576 * @flags: flags provided for this frequency change request 577 * 578 * An entry function provided to the devfreq framework which provides frequency 579 * change of the DMC. The function gets the possible rate from OPP table based 580 * on requested frequency. It calls the next function responsible for the 581 * frequency and voltage change. In case of failure, does not set 'curr_rate' 582 * and returns error value to the framework. 583 */ 584 static int exynos5_dmc_target(struct device *dev, unsigned long *freq, 585 u32 flags) 586 { 587 struct exynos5_dmc *dmc = dev_get_drvdata(dev); 588 unsigned long target_rate = 0; 589 unsigned long target_volt = 0; 590 int ret; 591 592 ret = exynos5_dmc_get_volt_freq(dmc, freq, &target_rate, &target_volt, 593 flags); 594 595 if (ret) 596 return ret; 597 598 if (target_rate == dmc->curr_rate) 599 return 0; 600 601 mutex_lock(&dmc->lock); 602 603 ret = exynos5_dmc_change_freq_and_volt(dmc, target_rate, target_volt); 604 605 if (ret) { 606 mutex_unlock(&dmc->lock); 607 return ret; 608 } 609 610 dmc->curr_rate = target_rate; 611 612 mutex_unlock(&dmc->lock); 613 return 0; 614 } 615 616 /** 617 * exynos5_counters_get() - Gets the performance counters values. 618 * @dmc: device for which the counters are going to be checked 619 * @load_count: variable which is populated with counter value 620 * @total_count: variable which is used as 'wall clock' reference 621 * 622 * Function which provides performance counters values. It sums up counters for 623 * two DMC channels. The 'total_count' is used as a reference and max value. 624 * The ratio 'load_count/total_count' shows the busy percentage [0%, 100%]. 625 */ 626 static int exynos5_counters_get(struct exynos5_dmc *dmc, 627 unsigned long *load_count, 628 unsigned long *total_count) 629 { 630 unsigned long total = 0; 631 struct devfreq_event_data event; 632 int ret, i; 633 634 *load_count = 0; 635 636 /* Take into account only read+write counters, but stop all */ 637 for (i = 0; i < dmc->num_counters; i++) { 638 if (!dmc->counter[i]) 639 continue; 640 641 ret = devfreq_event_get_event(dmc->counter[i], &event); 642 if (ret < 0) 643 return ret; 644 645 *load_count += event.load_count; 646 647 if (total < event.total_count) 648 total = event.total_count; 649 } 650 651 *total_count = total; 652 653 return 0; 654 } 655 656 /** 657 * exynos5_dmc_get_status() - Read current DMC performance statistics. 658 * @dev: device for which the statistics are requested 659 * @stat: structure which has statistic fields 660 * 661 * Function reads the DMC performance counters and calculates 'busy_time' 662 * and 'total_time'. To protect from overflow, the values are shifted right 663 * by 10. After read out the counters are setup to count again. 664 */ 665 static int exynos5_dmc_get_status(struct device *dev, 666 struct devfreq_dev_status *stat) 667 { 668 struct exynos5_dmc *dmc = dev_get_drvdata(dev); 669 unsigned long load, total; 670 int ret; 671 672 ret = exynos5_counters_get(dmc, &load, &total); 673 if (ret < 0) 674 return -EINVAL; 675 676 /* To protect from overflow in calculation ratios, divide by 1024 */ 677 stat->busy_time = load >> 10; 678 stat->total_time = total >> 10; 679 680 ret = exynos5_counters_set_event(dmc); 681 if (ret < 0) { 682 dev_err(dev, "could not set event counter\n"); 683 return ret; 684 } 685 686 return 0; 687 } 688 689 /** 690 * exynos5_dmc_get_cur_freq() - Function returns current DMC frequency 691 * @dev: device for which the framework checks operating frequency 692 * @freq: returned frequency value 693 * 694 * It returns the currently used frequency of the DMC. The real operating 695 * frequency might be lower when the clock source value could not be divided 696 * to the requested value. 697 */ 698 static int exynos5_dmc_get_cur_freq(struct device *dev, unsigned long *freq) 699 { 700 struct exynos5_dmc *dmc = dev_get_drvdata(dev); 701 702 mutex_lock(&dmc->lock); 703 *freq = dmc->curr_rate; 704 mutex_unlock(&dmc->lock); 705 706 return 0; 707 } 708 709 /** 710 * exynos5_dmc_df_profile - Devfreq governor's profile structure 711 * 712 * It provides to the devfreq framework needed functions and polling period. 713 */ 714 static struct devfreq_dev_profile exynos5_dmc_df_profile = { 715 .polling_ms = 500, 716 .target = exynos5_dmc_target, 717 .get_dev_status = exynos5_dmc_get_status, 718 .get_cur_freq = exynos5_dmc_get_cur_freq, 719 }; 720 721 /** 722 * exynos5_dmc_align_initial_frequency() - Align initial frequency value 723 * @dmc: device for which the frequency is going to be set 724 * @bootloader_init_freq: initial frequency set by the bootloader in KHz 725 * 726 * The initial bootloader frequency, which is present during boot, might be 727 * different that supported frequency values in the driver. It is possible 728 * due to different PLL settings or used PLL as a source. 729 * This function provides the 'initial_freq' for the devfreq framework 730 * statistics engine which supports only registered values. Thus, some alignment 731 * must be made. 732 */ 733 static unsigned long 734 exynos5_dmc_align_init_freq(struct exynos5_dmc *dmc, 735 unsigned long bootloader_init_freq) 736 { 737 unsigned long aligned_freq; 738 int idx; 739 740 idx = find_target_freq_idx(dmc, bootloader_init_freq); 741 if (idx >= 0) 742 aligned_freq = dmc->opp[idx].freq_hz; 743 else 744 aligned_freq = dmc->opp[dmc->opp_count - 1].freq_hz; 745 746 return aligned_freq; 747 } 748 749 /** 750 * create_timings_aligned() - Create register values and align with standard 751 * @dmc: device for which the frequency is going to be set 752 * @idx: speed bin in the OPP table 753 * @clk_period_ps: the period of the clock, known as tCK 754 * 755 * The function calculates timings and creates a register value ready for 756 * a frequency transition. The register contains a few timings. They are 757 * shifted by a known offset. The timing value is calculated based on memory 758 * specyfication: minimal time required and minimal cycles required. 759 */ 760 static int create_timings_aligned(struct exynos5_dmc *dmc, u32 *reg_timing_row, 761 u32 *reg_timing_data, u32 *reg_timing_power, 762 u32 clk_period_ps) 763 { 764 u32 val; 765 const struct timing_reg *reg; 766 767 if (clk_period_ps == 0) 768 return -EINVAL; 769 770 *reg_timing_row = 0; 771 *reg_timing_data = 0; 772 *reg_timing_power = 0; 773 774 val = dmc->timings->tRFC / clk_period_ps; 775 val += dmc->timings->tRFC % clk_period_ps ? 1 : 0; 776 val = max(val, dmc->min_tck->tRFC); 777 reg = &timing_row[0]; 778 *reg_timing_row |= TIMING_VAL2REG(reg, val); 779 780 val = dmc->timings->tRRD / clk_period_ps; 781 val += dmc->timings->tRRD % clk_period_ps ? 1 : 0; 782 val = max(val, dmc->min_tck->tRRD); 783 reg = &timing_row[1]; 784 *reg_timing_row |= TIMING_VAL2REG(reg, val); 785 786 val = dmc->timings->tRPab / clk_period_ps; 787 val += dmc->timings->tRPab % clk_period_ps ? 1 : 0; 788 val = max(val, dmc->min_tck->tRPab); 789 reg = &timing_row[2]; 790 *reg_timing_row |= TIMING_VAL2REG(reg, val); 791 792 val = dmc->timings->tRCD / clk_period_ps; 793 val += dmc->timings->tRCD % clk_period_ps ? 1 : 0; 794 val = max(val, dmc->min_tck->tRCD); 795 reg = &timing_row[3]; 796 *reg_timing_row |= TIMING_VAL2REG(reg, val); 797 798 val = dmc->timings->tRC / clk_period_ps; 799 val += dmc->timings->tRC % clk_period_ps ? 1 : 0; 800 val = max(val, dmc->min_tck->tRC); 801 reg = &timing_row[4]; 802 *reg_timing_row |= TIMING_VAL2REG(reg, val); 803 804 val = dmc->timings->tRAS / clk_period_ps; 805 val += dmc->timings->tRAS % clk_period_ps ? 1 : 0; 806 val = max(val, dmc->min_tck->tRAS); 807 reg = &timing_row[5]; 808 *reg_timing_row |= TIMING_VAL2REG(reg, val); 809 810 /* data related timings */ 811 val = dmc->timings->tWTR / clk_period_ps; 812 val += dmc->timings->tWTR % clk_period_ps ? 1 : 0; 813 val = max(val, dmc->min_tck->tWTR); 814 reg = &timing_data[0]; 815 *reg_timing_data |= TIMING_VAL2REG(reg, val); 816 817 val = dmc->timings->tWR / clk_period_ps; 818 val += dmc->timings->tWR % clk_period_ps ? 1 : 0; 819 val = max(val, dmc->min_tck->tWR); 820 reg = &timing_data[1]; 821 *reg_timing_data |= TIMING_VAL2REG(reg, val); 822 823 val = dmc->timings->tRTP / clk_period_ps; 824 val += dmc->timings->tRTP % clk_period_ps ? 1 : 0; 825 val = max(val, dmc->min_tck->tRTP); 826 reg = &timing_data[2]; 827 *reg_timing_data |= TIMING_VAL2REG(reg, val); 828 829 val = dmc->timings->tW2W_C2C / clk_period_ps; 830 val += dmc->timings->tW2W_C2C % clk_period_ps ? 1 : 0; 831 val = max(val, dmc->min_tck->tW2W_C2C); 832 reg = &timing_data[3]; 833 *reg_timing_data |= TIMING_VAL2REG(reg, val); 834 835 val = dmc->timings->tR2R_C2C / clk_period_ps; 836 val += dmc->timings->tR2R_C2C % clk_period_ps ? 1 : 0; 837 val = max(val, dmc->min_tck->tR2R_C2C); 838 reg = &timing_data[4]; 839 *reg_timing_data |= TIMING_VAL2REG(reg, val); 840 841 val = dmc->timings->tWL / clk_period_ps; 842 val += dmc->timings->tWL % clk_period_ps ? 1 : 0; 843 val = max(val, dmc->min_tck->tWL); 844 reg = &timing_data[5]; 845 *reg_timing_data |= TIMING_VAL2REG(reg, val); 846 847 val = dmc->timings->tDQSCK / clk_period_ps; 848 val += dmc->timings->tDQSCK % clk_period_ps ? 1 : 0; 849 val = max(val, dmc->min_tck->tDQSCK); 850 reg = &timing_data[6]; 851 *reg_timing_data |= TIMING_VAL2REG(reg, val); 852 853 val = dmc->timings->tRL / clk_period_ps; 854 val += dmc->timings->tRL % clk_period_ps ? 1 : 0; 855 val = max(val, dmc->min_tck->tRL); 856 reg = &timing_data[7]; 857 *reg_timing_data |= TIMING_VAL2REG(reg, val); 858 859 /* power related timings */ 860 val = dmc->timings->tFAW / clk_period_ps; 861 val += dmc->timings->tFAW % clk_period_ps ? 1 : 0; 862 val = max(val, dmc->min_tck->tXP); 863 reg = &timing_power[0]; 864 *reg_timing_power |= TIMING_VAL2REG(reg, val); 865 866 val = dmc->timings->tXSR / clk_period_ps; 867 val += dmc->timings->tXSR % clk_period_ps ? 1 : 0; 868 val = max(val, dmc->min_tck->tXSR); 869 reg = &timing_power[1]; 870 *reg_timing_power |= TIMING_VAL2REG(reg, val); 871 872 val = dmc->timings->tXP / clk_period_ps; 873 val += dmc->timings->tXP % clk_period_ps ? 1 : 0; 874 val = max(val, dmc->min_tck->tXP); 875 reg = &timing_power[2]; 876 *reg_timing_power |= TIMING_VAL2REG(reg, val); 877 878 val = dmc->timings->tCKE / clk_period_ps; 879 val += dmc->timings->tCKE % clk_period_ps ? 1 : 0; 880 val = max(val, dmc->min_tck->tCKE); 881 reg = &timing_power[3]; 882 *reg_timing_power |= TIMING_VAL2REG(reg, val); 883 884 val = dmc->timings->tMRD / clk_period_ps; 885 val += dmc->timings->tMRD % clk_period_ps ? 1 : 0; 886 val = max(val, dmc->min_tck->tMRD); 887 reg = &timing_power[4]; 888 *reg_timing_power |= TIMING_VAL2REG(reg, val); 889 890 return 0; 891 } 892 893 /** 894 * of_get_dram_timings() - helper function for parsing DT settings for DRAM 895 * @dmc: device for which the frequency is going to be set 896 * 897 * The function parses DT entries with DRAM information. 898 */ 899 static int of_get_dram_timings(struct exynos5_dmc *dmc) 900 { 901 int ret = 0; 902 int idx; 903 struct device_node *np_ddr; 904 u32 freq_mhz, clk_period_ps; 905 906 np_ddr = of_parse_phandle(dmc->dev->of_node, "device-handle", 0); 907 if (!np_ddr) { 908 dev_warn(dmc->dev, "could not find 'device-handle' in DT\n"); 909 return -EINVAL; 910 } 911 912 dmc->timing_row = devm_kmalloc_array(dmc->dev, TIMING_COUNT, 913 sizeof(u32), GFP_KERNEL); 914 if (!dmc->timing_row) 915 return -ENOMEM; 916 917 dmc->timing_data = devm_kmalloc_array(dmc->dev, TIMING_COUNT, 918 sizeof(u32), GFP_KERNEL); 919 if (!dmc->timing_data) 920 return -ENOMEM; 921 922 dmc->timing_power = devm_kmalloc_array(dmc->dev, TIMING_COUNT, 923 sizeof(u32), GFP_KERNEL); 924 if (!dmc->timing_power) 925 return -ENOMEM; 926 927 dmc->timings = of_lpddr3_get_ddr_timings(np_ddr, dmc->dev, 928 DDR_TYPE_LPDDR3, 929 &dmc->timings_arr_size); 930 if (!dmc->timings) { 931 of_node_put(np_ddr); 932 dev_warn(dmc->dev, "could not get timings from DT\n"); 933 return -EINVAL; 934 } 935 936 dmc->min_tck = of_lpddr3_get_min_tck(np_ddr, dmc->dev); 937 if (!dmc->min_tck) { 938 of_node_put(np_ddr); 939 dev_warn(dmc->dev, "could not get tck from DT\n"); 940 return -EINVAL; 941 } 942 943 /* Sorted array of OPPs with frequency ascending */ 944 for (idx = 0; idx < dmc->opp_count; idx++) { 945 freq_mhz = dmc->opp[idx].freq_hz / 1000000; 946 clk_period_ps = 1000000 / freq_mhz; 947 948 ret = create_timings_aligned(dmc, &dmc->timing_row[idx], 949 &dmc->timing_data[idx], 950 &dmc->timing_power[idx], 951 clk_period_ps); 952 } 953 954 of_node_put(np_ddr); 955 956 /* Take the highest frequency's timings as 'bypass' */ 957 dmc->bypass_timing_row = dmc->timing_row[idx - 1]; 958 dmc->bypass_timing_data = dmc->timing_data[idx - 1]; 959 dmc->bypass_timing_power = dmc->timing_power[idx - 1]; 960 961 return ret; 962 } 963 964 /** 965 * exynos5_dmc_init_clks() - Initialize clocks needed for DMC operation. 966 * @dmc: DMC structure containing needed fields 967 * 968 * Get the needed clocks defined in DT device, enable and set the right parents. 969 * Read current frequency and initialize the initial rate for governor. 970 */ 971 static int exynos5_dmc_init_clks(struct exynos5_dmc *dmc) 972 { 973 int ret; 974 unsigned long target_volt = 0; 975 unsigned long target_rate = 0; 976 unsigned int tmp; 977 978 dmc->fout_spll = devm_clk_get(dmc->dev, "fout_spll"); 979 if (IS_ERR(dmc->fout_spll)) 980 return PTR_ERR(dmc->fout_spll); 981 982 dmc->fout_bpll = devm_clk_get(dmc->dev, "fout_bpll"); 983 if (IS_ERR(dmc->fout_bpll)) 984 return PTR_ERR(dmc->fout_bpll); 985 986 dmc->mout_mclk_cdrex = devm_clk_get(dmc->dev, "mout_mclk_cdrex"); 987 if (IS_ERR(dmc->mout_mclk_cdrex)) 988 return PTR_ERR(dmc->mout_mclk_cdrex); 989 990 dmc->mout_bpll = devm_clk_get(dmc->dev, "mout_bpll"); 991 if (IS_ERR(dmc->mout_bpll)) 992 return PTR_ERR(dmc->mout_bpll); 993 994 dmc->mout_mx_mspll_ccore = devm_clk_get(dmc->dev, 995 "mout_mx_mspll_ccore"); 996 if (IS_ERR(dmc->mout_mx_mspll_ccore)) 997 return PTR_ERR(dmc->mout_mx_mspll_ccore); 998 999 dmc->mout_spll = devm_clk_get(dmc->dev, "ff_dout_spll2"); 1000 if (IS_ERR(dmc->mout_spll)) { 1001 dmc->mout_spll = devm_clk_get(dmc->dev, "mout_sclk_spll"); 1002 if (IS_ERR(dmc->mout_spll)) 1003 return PTR_ERR(dmc->mout_spll); 1004 } 1005 1006 /* 1007 * Convert frequency to KHz values and set it for the governor. 1008 */ 1009 dmc->curr_rate = clk_get_rate(dmc->mout_mclk_cdrex); 1010 dmc->curr_rate = exynos5_dmc_align_init_freq(dmc, dmc->curr_rate); 1011 exynos5_dmc_df_profile.initial_freq = dmc->curr_rate; 1012 1013 ret = exynos5_dmc_get_volt_freq(dmc, &dmc->curr_rate, &target_rate, 1014 &target_volt, 0); 1015 if (ret) 1016 return ret; 1017 1018 dmc->curr_volt = target_volt; 1019 1020 clk_set_parent(dmc->mout_mx_mspll_ccore, dmc->mout_spll); 1021 1022 dmc->bypass_rate = clk_get_rate(dmc->mout_mx_mspll_ccore); 1023 1024 clk_prepare_enable(dmc->fout_bpll); 1025 clk_prepare_enable(dmc->mout_bpll); 1026 1027 /* 1028 * Some bootloaders do not set clock routes correctly. 1029 * Stop one path in clocks to PHY. 1030 */ 1031 regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, &tmp); 1032 tmp &= ~(BIT(1) | BIT(0)); 1033 regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, tmp); 1034 1035 return 0; 1036 } 1037 1038 /** 1039 * exynos5_performance_counters_init() - Initializes performance DMC's counters 1040 * @dmc: DMC for which it does the setup 1041 * 1042 * Initialization of performance counters in DMC for estimating usage. 1043 * The counter's values are used for calculation of a memory bandwidth and based 1044 * on that the governor changes the frequency. 1045 * The counters are not used when the governor is GOVERNOR_USERSPACE. 1046 */ 1047 static int exynos5_performance_counters_init(struct exynos5_dmc *dmc) 1048 { 1049 int counters_size; 1050 int ret, i; 1051 1052 dmc->num_counters = devfreq_event_get_edev_count(dmc->dev); 1053 if (dmc->num_counters < 0) { 1054 dev_err(dmc->dev, "could not get devfreq-event counters\n"); 1055 return dmc->num_counters; 1056 } 1057 1058 counters_size = sizeof(struct devfreq_event_dev) * dmc->num_counters; 1059 dmc->counter = devm_kzalloc(dmc->dev, counters_size, GFP_KERNEL); 1060 if (!dmc->counter) 1061 return -ENOMEM; 1062 1063 for (i = 0; i < dmc->num_counters; i++) { 1064 dmc->counter[i] = 1065 devfreq_event_get_edev_by_phandle(dmc->dev, i); 1066 if (IS_ERR_OR_NULL(dmc->counter[i])) 1067 return -EPROBE_DEFER; 1068 } 1069 1070 ret = exynos5_counters_enable_edev(dmc); 1071 if (ret < 0) { 1072 dev_err(dmc->dev, "could not enable event counter\n"); 1073 return ret; 1074 } 1075 1076 ret = exynos5_counters_set_event(dmc); 1077 if (ret < 0) { 1078 exynos5_counters_disable_edev(dmc); 1079 dev_err(dmc->dev, "could not set event counter\n"); 1080 return ret; 1081 } 1082 1083 return 0; 1084 } 1085 1086 /** 1087 * exynos5_dmc_set_pause_on_switching() - Controls a pause feature in DMC 1088 * @dmc: device which is used for changing this feature 1089 * @set: a boolean state passing enable/disable request 1090 * 1091 * There is a need of pausing DREX DMC when divider or MUX in clock tree 1092 * changes its configuration. In such situation access to the memory is blocked 1093 * in DMC automatically. This feature is used when clock frequency change 1094 * request appears and touches clock tree. 1095 */ 1096 static inline int exynos5_dmc_set_pause_on_switching(struct exynos5_dmc *dmc) 1097 { 1098 unsigned int val; 1099 int ret; 1100 1101 ret = regmap_read(dmc->clk_regmap, CDREX_PAUSE, &val); 1102 if (ret) 1103 return ret; 1104 1105 val |= 1UL; 1106 regmap_write(dmc->clk_regmap, CDREX_PAUSE, val); 1107 1108 return 0; 1109 } 1110 1111 /** 1112 * exynos5_dmc_probe() - Probe function for the DMC driver 1113 * @pdev: platform device for which the driver is going to be initialized 1114 * 1115 * Initialize basic components: clocks, regulators, performance counters, etc. 1116 * Read out product version and based on the information setup 1117 * internal structures for the controller (frequency and voltage) and for DRAM 1118 * memory parameters: timings for each operating frequency. 1119 * Register new devfreq device for controlling DVFS of the DMC. 1120 */ 1121 static int exynos5_dmc_probe(struct platform_device *pdev) 1122 { 1123 int ret = 0; 1124 struct device *dev = &pdev->dev; 1125 struct device_node *np = dev->of_node; 1126 struct exynos5_dmc *dmc; 1127 struct resource *res; 1128 1129 dmc = devm_kzalloc(dev, sizeof(*dmc), GFP_KERNEL); 1130 if (!dmc) 1131 return -ENOMEM; 1132 1133 mutex_init(&dmc->lock); 1134 1135 dmc->dev = dev; 1136 platform_set_drvdata(pdev, dmc); 1137 1138 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1139 dmc->base_drexi0 = devm_ioremap_resource(dev, res); 1140 if (IS_ERR(dmc->base_drexi0)) 1141 return PTR_ERR(dmc->base_drexi0); 1142 1143 res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1144 dmc->base_drexi1 = devm_ioremap_resource(dev, res); 1145 if (IS_ERR(dmc->base_drexi1)) 1146 return PTR_ERR(dmc->base_drexi1); 1147 1148 dmc->clk_regmap = syscon_regmap_lookup_by_phandle(np, 1149 "samsung,syscon-clk"); 1150 if (IS_ERR(dmc->clk_regmap)) 1151 return PTR_ERR(dmc->clk_regmap); 1152 1153 ret = exynos5_init_freq_table(dmc, &exynos5_dmc_df_profile); 1154 if (ret) { 1155 dev_warn(dev, "couldn't initialize frequency settings\n"); 1156 return ret; 1157 } 1158 1159 dmc->vdd_mif = devm_regulator_get(dev, "vdd"); 1160 if (IS_ERR(dmc->vdd_mif)) { 1161 ret = PTR_ERR(dmc->vdd_mif); 1162 return ret; 1163 } 1164 1165 ret = exynos5_dmc_init_clks(dmc); 1166 if (ret) 1167 return ret; 1168 1169 ret = of_get_dram_timings(dmc); 1170 if (ret) { 1171 dev_warn(dev, "couldn't initialize timings settings\n"); 1172 goto remove_clocks; 1173 } 1174 1175 ret = exynos5_performance_counters_init(dmc); 1176 if (ret) { 1177 dev_warn(dev, "couldn't probe performance counters\n"); 1178 goto remove_clocks; 1179 } 1180 1181 ret = exynos5_dmc_set_pause_on_switching(dmc); 1182 if (ret) { 1183 dev_warn(dev, "couldn't get access to PAUSE register\n"); 1184 goto err_devfreq_add; 1185 } 1186 1187 /* 1188 * Setup default thresholds for the devfreq governor. 1189 * The values are chosen based on experiments. 1190 */ 1191 dmc->gov_data.upthreshold = 30; 1192 dmc->gov_data.downdifferential = 5; 1193 1194 dmc->df = devm_devfreq_add_device(dev, &exynos5_dmc_df_profile, 1195 DEVFREQ_GOV_SIMPLE_ONDEMAND, 1196 &dmc->gov_data); 1197 1198 if (IS_ERR(dmc->df)) { 1199 ret = PTR_ERR(dmc->df); 1200 goto err_devfreq_add; 1201 } 1202 1203 dev_info(dev, "DMC initialized\n"); 1204 1205 return 0; 1206 1207 err_devfreq_add: 1208 exynos5_counters_disable_edev(dmc); 1209 remove_clocks: 1210 clk_disable_unprepare(dmc->mout_bpll); 1211 clk_disable_unprepare(dmc->fout_bpll); 1212 1213 return ret; 1214 } 1215 1216 /** 1217 * exynos5_dmc_remove() - Remove function for the platform device 1218 * @pdev: platform device which is going to be removed 1219 * 1220 * The function relies on 'devm' framework function which automatically 1221 * clean the device's resources. It just calls explicitly disable function for 1222 * the performance counters. 1223 */ 1224 static int exynos5_dmc_remove(struct platform_device *pdev) 1225 { 1226 struct exynos5_dmc *dmc = dev_get_drvdata(&pdev->dev); 1227 1228 exynos5_counters_disable_edev(dmc); 1229 1230 clk_disable_unprepare(dmc->mout_bpll); 1231 clk_disable_unprepare(dmc->fout_bpll); 1232 1233 dev_pm_opp_remove_table(dmc->dev); 1234 1235 return 0; 1236 } 1237 1238 static const struct of_device_id exynos5_dmc_of_match[] = { 1239 { .compatible = "samsung,exynos5422-dmc", }, 1240 { }, 1241 }; 1242 MODULE_DEVICE_TABLE(of, exynos5_dmc_of_match); 1243 1244 static struct platform_driver exynos5_dmc_platdrv = { 1245 .probe = exynos5_dmc_probe, 1246 .remove = exynos5_dmc_remove, 1247 .driver = { 1248 .name = "exynos5-dmc", 1249 .of_match_table = exynos5_dmc_of_match, 1250 }, 1251 }; 1252 module_platform_driver(exynos5_dmc_platdrv); 1253 MODULE_DESCRIPTION("Driver for Exynos5422 Dynamic Memory Controller dynamic frequency and voltage change"); 1254 MODULE_LICENSE("GPL v2"); 1255 MODULE_AUTHOR("Lukasz Luba"); 1256