1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * Author: Lukasz Luba <l.luba@partner.samsung.com>
8 #include <linux/devfreq.h>
9 #include <linux/devfreq-event.h>
10 #include <linux/device.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"
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)
39 * struct dmc_opp_table - Operating level desciption
41 * Covers frequency and voltage settings of the DMC operating mode.
43 struct dmc_opp_table {
49 * struct exynos5_dmc - main structure describing DMC device
51 * The main structure for the Dynamic Memory Controller which covers clocks,
52 * memory regions, HW information, parameters and current operating mode.
57 struct devfreq_simple_ondemand_data gov_data;
58 void __iomem *base_drexi0;
59 void __iomem *base_drexi1;
60 struct regmap *clk_regmap;
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;
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;
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 }
93 #define TIMING_VAL2REG(timing, t_val) \
96 __val = (t_val) << (timing)->bit_beg; \
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),
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),
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),
135 #define TIMING_COUNT (ARRAY_SIZE(timing_row) + ARRAY_SIZE(timing_data) + \
136 ARRAY_SIZE(timing_power))
138 static int exynos5_counters_set_event(struct exynos5_dmc *dmc)
142 for (i = 0; i < dmc->num_counters; i++) {
143 if (!dmc->counter[i])
145 ret = devfreq_event_set_event(dmc->counter[i]);
152 static int exynos5_counters_enable_edev(struct exynos5_dmc *dmc)
156 for (i = 0; i < dmc->num_counters; i++) {
157 if (!dmc->counter[i])
159 ret = devfreq_event_enable_edev(dmc->counter[i]);
166 static int exynos5_counters_disable_edev(struct exynos5_dmc *dmc)
170 for (i = 0; i < dmc->num_counters; i++) {
171 if (!dmc->counter[i])
173 ret = devfreq_event_disable_edev(dmc->counter[i]);
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
185 * Seeks in the local DMC driver structure for the requested frequency value
186 * and returns index or error value.
188 static int find_target_freq_idx(struct exynos5_dmc *dmc,
189 unsigned long target_rate)
193 for (i = dmc->opp_count - 1; i >= 0; i--)
194 if (dmc->opp[i].freq_hz <= target_rate)
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
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.
213 static void exynos5_switch_timing_regs(struct exynos5_dmc *dmc, bool set)
218 ret = regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, ®);
221 reg |= EXYNOS5_TIMING_SET_SWI;
223 reg &= ~EXYNOS5_TIMING_SET_SWI;
225 regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, reg);
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
233 * Populate the devfreq device's OPP table based on current frequency, voltage.
235 static int exynos5_init_freq_table(struct exynos5_dmc *dmc,
236 struct devfreq_dev_profile *profile)
242 ret = dev_pm_opp_of_add_table(dmc->dev);
244 dev_err(dmc->dev, "Failed to get OPP table\n");
248 dmc->opp_count = dev_pm_opp_get_opp_count(dmc->dev);
250 dmc->opp = devm_kmalloc_array(dmc->dev, dmc->opp_count,
251 sizeof(struct dmc_opp_table), GFP_KERNEL);
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;
259 opp = dev_pm_opp_find_freq_floor(dmc->dev, &freq);
261 goto err_free_tables;
263 dmc->opp[idx - i].freq_hz = freq;
264 dmc->opp[idx - i].volt_uv = dev_pm_opp_get_voltage(opp);
274 dev_pm_opp_of_remove_table(dmc->dev);
280 * exynos5_set_bypass_dram_timings() - Low-level changes of the DRAM timings
281 * @dmc: device for which the new settings is going to be applied
282 * @param: DRAM parameters which passes timing data
284 * Low-level function for changing timings for DRAM memory clocking from
285 * 'bypass' clock source (fixed frequency @400MHz).
286 * It uses timing bank registers set 1.
288 static void exynos5_set_bypass_dram_timings(struct exynos5_dmc *dmc)
290 writel(EXYNOS5_AREF_NORMAL,
291 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF);
293 writel(dmc->bypass_timing_row,
294 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW1);
295 writel(dmc->bypass_timing_row,
296 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW1);
297 writel(dmc->bypass_timing_data,
298 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA1);
299 writel(dmc->bypass_timing_data,
300 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA1);
301 writel(dmc->bypass_timing_power,
302 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER1);
303 writel(dmc->bypass_timing_power,
304 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER1);
308 * exynos5_dram_change_timings() - Low-level changes of the DRAM final timings
309 * @dmc: device for which the new settings is going to be applied
310 * @target_rate: target frequency of the DMC
312 * Low-level function for changing timings for DRAM memory operating from main
313 * clock source (BPLL), which can have different frequencies. Thus, each
314 * frequency must have corresponding timings register values in order to keep
316 * It uses timing bank registers set 0.
318 static int exynos5_dram_change_timings(struct exynos5_dmc *dmc,
319 unsigned long target_rate)
323 for (idx = dmc->opp_count - 1; idx >= 0; idx--)
324 if (dmc->opp[idx].freq_hz <= target_rate)
330 writel(EXYNOS5_AREF_NORMAL,
331 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF);
333 writel(dmc->timing_row[idx],
334 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW0);
335 writel(dmc->timing_row[idx],
336 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW0);
337 writel(dmc->timing_data[idx],
338 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA0);
339 writel(dmc->timing_data[idx],
340 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA0);
341 writel(dmc->timing_power[idx],
342 dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER0);
343 writel(dmc->timing_power[idx],
344 dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER0);
350 * exynos5_dmc_align_target_voltage() - Sets the final voltage for the DMC
351 * @dmc: device for which it is going to be set
352 * @target_volt: new voltage which is chosen to be final
354 * Function tries to align voltage to the safe level for 'normal' mode.
355 * It checks the need of higher voltage and changes the value. The target
356 * voltage might be lower that currently set and still the system will be
359 static int exynos5_dmc_align_target_voltage(struct exynos5_dmc *dmc,
360 unsigned long target_volt)
364 if (dmc->curr_volt <= target_volt)
367 ret = regulator_set_voltage(dmc->vdd_mif, target_volt,
370 dmc->curr_volt = target_volt;
376 * exynos5_dmc_align_bypass_voltage() - Sets the voltage for the DMC
377 * @dmc: device for which it is going to be set
378 * @target_volt: new voltage which is chosen to be final
380 * Function tries to align voltage to the safe level for the 'bypass' mode.
381 * It checks the need of higher voltage and changes the value.
382 * The target voltage must not be less than currently needed, because
383 * for current frequency the device might become unstable.
385 static int exynos5_dmc_align_bypass_voltage(struct exynos5_dmc *dmc,
386 unsigned long target_volt)
389 unsigned long bypass_volt = dmc->opp_bypass.volt_uv;
391 target_volt = max(bypass_volt, target_volt);
393 if (dmc->curr_volt >= target_volt)
396 ret = regulator_set_voltage(dmc->vdd_mif, target_volt,
399 dmc->curr_volt = target_volt;
405 * exynos5_dmc_align_bypass_dram_timings() - Chooses and sets DRAM timings
406 * @dmc: device for which it is going to be set
407 * @target_rate: new frequency which is chosen to be final
409 * Function changes the DRAM timings for the temporary 'bypass' mode.
411 static int exynos5_dmc_align_bypass_dram_timings(struct exynos5_dmc *dmc,
412 unsigned long target_rate)
414 int idx = find_target_freq_idx(dmc, target_rate);
419 exynos5_set_bypass_dram_timings(dmc);
425 * exynos5_dmc_switch_to_bypass_configuration() - Switching to temporary clock
426 * @dmc: DMC device for which the switching is going to happen
427 * @target_rate: new frequency which is going to be set as a final
428 * @target_volt: new voltage which is going to be set as a final
430 * Function configures DMC and clocks for operating in temporary 'bypass' mode.
431 * This mode is used only temporary but if required, changes voltage and timings
432 * for DRAM chips. It switches the main clock to stable clock source for the
433 * period of the main PLL reconfiguration.
436 exynos5_dmc_switch_to_bypass_configuration(struct exynos5_dmc *dmc,
437 unsigned long target_rate,
438 unsigned long target_volt)
443 * Having higher voltage for a particular frequency does not harm
444 * the chip. Use it for the temporary frequency change when one
445 * voltage manipulation might be avoided.
447 ret = exynos5_dmc_align_bypass_voltage(dmc, target_volt);
452 * Longer delays for DRAM does not cause crash, the opposite does.
454 ret = exynos5_dmc_align_bypass_dram_timings(dmc, target_rate);
459 * Delays are long enough, so use them for the new coming clock.
461 exynos5_switch_timing_regs(dmc, USE_MX_MSPLL_TIMINGS);
467 * exynos5_dmc_change_freq_and_volt() - Changes voltage and frequency of the DMC
468 * using safe procedure
469 * @dmc: device for which the frequency is going to be changed
470 * @target_rate: requested new frequency
471 * @target_volt: requested voltage which corresponds to the new frequency
473 * The DMC frequency change procedure requires a few steps.
474 * The main requirement is to change the clock source in the clk mux
475 * for the time of main clock PLL locking. The assumption is that the
476 * alternative clock source set as parent is stable.
477 * The second parent's clock frequency is fixed to 400MHz, it is named 'bypass'
478 * clock. This requires alignment in DRAM timing parameters for the new
479 * T-period. There is two bank sets for keeping DRAM
480 * timings: set 0 and set 1. The set 0 is used when main clock source is
481 * chosen. The 2nd set of regs is used for 'bypass' clock. Switching between
482 * the two bank sets is part of the process.
483 * The voltage must also be aligned to the minimum required level. There is
484 * this intermediate step with switching to 'bypass' parent clock source.
485 * if the old voltage is lower, it requires an increase of the voltage level.
486 * The complexity of the voltage manipulation is hidden in low level function.
487 * In this function there is last alignment of the voltage level at the end.
490 exynos5_dmc_change_freq_and_volt(struct exynos5_dmc *dmc,
491 unsigned long target_rate,
492 unsigned long target_volt)
496 ret = exynos5_dmc_switch_to_bypass_configuration(dmc, target_rate,
502 * Voltage is set at least to a level needed for this frequency,
503 * so switching clock source is safe now.
505 clk_prepare_enable(dmc->fout_spll);
506 clk_prepare_enable(dmc->mout_spll);
507 clk_prepare_enable(dmc->mout_mx_mspll_ccore);
509 ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_mx_mspll_ccore);
514 * We are safe to increase the timings for current bypass frequency.
515 * Thanks to this the settings will be ready for the upcoming clock
518 exynos5_dram_change_timings(dmc, target_rate);
520 clk_set_rate(dmc->fout_bpll, target_rate);
522 exynos5_switch_timing_regs(dmc, USE_BPLL_TIMINGS);
524 ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_bpll);
529 * Make sure if the voltage is not from 'bypass' settings and align to
530 * the right level for power efficiency.
532 ret = exynos5_dmc_align_target_voltage(dmc, target_volt);
535 clk_disable_unprepare(dmc->mout_mx_mspll_ccore);
536 clk_disable_unprepare(dmc->mout_spll);
537 clk_disable_unprepare(dmc->fout_spll);
543 * exynos5_dmc_get_volt_freq() - Gets the frequency and voltage from the OPP
545 * @dmc: device for which the frequency is going to be changed
546 * @freq: requested frequency in KHz
547 * @target_rate: returned frequency which is the same or lower than
549 * @target_volt: returned voltage which corresponds to the returned
552 * Function gets requested frequency and checks OPP framework for needed
553 * frequency and voltage. It populates the values 'target_rate' and
554 * 'target_volt' or returns error value when OPP framework fails.
556 static int exynos5_dmc_get_volt_freq(struct exynos5_dmc *dmc,
558 unsigned long *target_rate,
559 unsigned long *target_volt, u32 flags)
561 struct dev_pm_opp *opp;
563 opp = devfreq_recommended_opp(dmc->dev, freq, flags);
567 *target_rate = dev_pm_opp_get_freq(opp);
568 *target_volt = dev_pm_opp_get_voltage(opp);
575 * exynos5_dmc_target() - Function responsible for changing frequency of DMC
576 * @dev: device for which the frequency is going to be changed
577 * @freq: requested frequency in KHz
578 * @flags: flags provided for this frequency change request
580 * An entry function provided to the devfreq framework which provides frequency
581 * change of the DMC. The function gets the possible rate from OPP table based
582 * on requested frequency. It calls the next function responsible for the
583 * frequency and voltage change. In case of failure, does not set 'curr_rate'
584 * and returns error value to the framework.
586 static int exynos5_dmc_target(struct device *dev, unsigned long *freq,
589 struct exynos5_dmc *dmc = dev_get_drvdata(dev);
590 unsigned long target_rate = 0;
591 unsigned long target_volt = 0;
594 ret = exynos5_dmc_get_volt_freq(dmc, freq, &target_rate, &target_volt,
600 if (target_rate == dmc->curr_rate)
603 mutex_lock(&dmc->lock);
605 ret = exynos5_dmc_change_freq_and_volt(dmc, target_rate, target_volt);
608 mutex_unlock(&dmc->lock);
612 dmc->curr_rate = target_rate;
614 mutex_unlock(&dmc->lock);
619 * exynos5_counters_get() - Gets the performance counters values.
620 * @dmc: device for which the counters are going to be checked
621 * @load_count: variable which is populated with counter value
622 * @total_count: variable which is used as 'wall clock' reference
624 * Function which provides performance counters values. It sums up counters for
625 * two DMC channels. The 'total_count' is used as a reference and max value.
626 * The ratio 'load_count/total_count' shows the busy percentage [0%, 100%].
628 static int exynos5_counters_get(struct exynos5_dmc *dmc,
629 unsigned long *load_count,
630 unsigned long *total_count)
632 unsigned long total = 0;
633 struct devfreq_event_data event;
638 /* Take into account only read+write counters, but stop all */
639 for (i = 0; i < dmc->num_counters; i++) {
640 if (!dmc->counter[i])
643 ret = devfreq_event_get_event(dmc->counter[i], &event);
647 *load_count += event.load_count;
649 if (total < event.total_count)
650 total = event.total_count;
653 *total_count = total;
659 * exynos5_dmc_get_status() - Read current DMC performance statistics.
660 * @dev: device for which the statistics are requested
661 * @stat: structure which has statistic fields
663 * Function reads the DMC performance counters and calculates 'busy_time'
664 * and 'total_time'. To protect from overflow, the values are shifted right
665 * by 10. After read out the counters are setup to count again.
667 static int exynos5_dmc_get_status(struct device *dev,
668 struct devfreq_dev_status *stat)
670 struct exynos5_dmc *dmc = dev_get_drvdata(dev);
671 unsigned long load, total;
674 ret = exynos5_counters_get(dmc, &load, &total);
678 /* To protect from overflow in calculation ratios, divide by 1024 */
679 stat->busy_time = load >> 10;
680 stat->total_time = total >> 10;
682 ret = exynos5_counters_set_event(dmc);
684 dev_err(dev, "could not set event counter\n");
692 * exynos5_dmc_get_cur_freq() - Function returns current DMC frequency
693 * @dev: device for which the framework checks operating frequency
694 * @freq: returned frequency value
696 * It returns the currently used frequency of the DMC. The real operating
697 * frequency might be lower when the clock source value could not be divided
698 * to the requested value.
700 static int exynos5_dmc_get_cur_freq(struct device *dev, unsigned long *freq)
702 struct exynos5_dmc *dmc = dev_get_drvdata(dev);
704 mutex_lock(&dmc->lock);
705 *freq = dmc->curr_rate;
706 mutex_unlock(&dmc->lock);
712 * exynos5_dmc_df_profile - Devfreq governor's profile structure
714 * It provides to the devfreq framework needed functions and polling period.
716 static struct devfreq_dev_profile exynos5_dmc_df_profile = {
718 .target = exynos5_dmc_target,
719 .get_dev_status = exynos5_dmc_get_status,
720 .get_cur_freq = exynos5_dmc_get_cur_freq,
724 * exynos5_dmc_align_initial_frequency() - Align initial frequency value
725 * @dmc: device for which the frequency is going to be set
726 * @bootloader_init_freq: initial frequency set by the bootloader in KHz
728 * The initial bootloader frequency, which is present during boot, might be
729 * different that supported frequency values in the driver. It is possible
730 * due to different PLL settings or used PLL as a source.
731 * This function provides the 'initial_freq' for the devfreq framework
732 * statistics engine which supports only registered values. Thus, some alignment
736 exynos5_dmc_align_init_freq(struct exynos5_dmc *dmc,
737 unsigned long bootloader_init_freq)
739 unsigned long aligned_freq;
742 idx = find_target_freq_idx(dmc, bootloader_init_freq);
744 aligned_freq = dmc->opp[idx].freq_hz;
746 aligned_freq = dmc->opp[dmc->opp_count - 1].freq_hz;
752 * create_timings_aligned() - Create register values and align with standard
753 * @dmc: device for which the frequency is going to be set
754 * @idx: speed bin in the OPP table
755 * @clk_period_ps: the period of the clock, known as tCK
757 * The function calculates timings and creates a register value ready for
758 * a frequency transition. The register contains a few timings. They are
759 * shifted by a known offset. The timing value is calculated based on memory
760 * specyfication: minimal time required and minimal cycles required.
762 static int create_timings_aligned(struct exynos5_dmc *dmc, u32 *reg_timing_row,
763 u32 *reg_timing_data, u32 *reg_timing_power,
767 const struct timing_reg *reg;
769 if (clk_period_ps == 0)
773 *reg_timing_data = 0;
774 *reg_timing_power = 0;
776 val = dmc->timings->tRFC / clk_period_ps;
777 val += dmc->timings->tRFC % clk_period_ps ? 1 : 0;
778 val = max(val, dmc->min_tck->tRFC);
779 reg = &timing_row[0];
780 *reg_timing_row |= TIMING_VAL2REG(reg, val);
782 val = dmc->timings->tRRD / clk_period_ps;
783 val += dmc->timings->tRRD % clk_period_ps ? 1 : 0;
784 val = max(val, dmc->min_tck->tRRD);
785 reg = &timing_row[1];
786 *reg_timing_row |= TIMING_VAL2REG(reg, val);
788 val = dmc->timings->tRPab / clk_period_ps;
789 val += dmc->timings->tRPab % clk_period_ps ? 1 : 0;
790 val = max(val, dmc->min_tck->tRPab);
791 reg = &timing_row[2];
792 *reg_timing_row |= TIMING_VAL2REG(reg, val);
794 val = dmc->timings->tRCD / clk_period_ps;
795 val += dmc->timings->tRCD % clk_period_ps ? 1 : 0;
796 val = max(val, dmc->min_tck->tRCD);
797 reg = &timing_row[3];
798 *reg_timing_row |= TIMING_VAL2REG(reg, val);
800 val = dmc->timings->tRC / clk_period_ps;
801 val += dmc->timings->tRC % clk_period_ps ? 1 : 0;
802 val = max(val, dmc->min_tck->tRC);
803 reg = &timing_row[4];
804 *reg_timing_row |= TIMING_VAL2REG(reg, val);
806 val = dmc->timings->tRAS / clk_period_ps;
807 val += dmc->timings->tRAS % clk_period_ps ? 1 : 0;
808 val = max(val, dmc->min_tck->tRAS);
809 reg = &timing_row[5];
810 *reg_timing_row |= TIMING_VAL2REG(reg, val);
812 /* data related timings */
813 val = dmc->timings->tWTR / clk_period_ps;
814 val += dmc->timings->tWTR % clk_period_ps ? 1 : 0;
815 val = max(val, dmc->min_tck->tWTR);
816 reg = &timing_data[0];
817 *reg_timing_data |= TIMING_VAL2REG(reg, val);
819 val = dmc->timings->tWR / clk_period_ps;
820 val += dmc->timings->tWR % clk_period_ps ? 1 : 0;
821 val = max(val, dmc->min_tck->tWR);
822 reg = &timing_data[1];
823 *reg_timing_data |= TIMING_VAL2REG(reg, val);
825 val = dmc->timings->tRTP / clk_period_ps;
826 val += dmc->timings->tRTP % clk_period_ps ? 1 : 0;
827 val = max(val, dmc->min_tck->tRTP);
828 reg = &timing_data[2];
829 *reg_timing_data |= TIMING_VAL2REG(reg, val);
831 val = dmc->timings->tW2W_C2C / clk_period_ps;
832 val += dmc->timings->tW2W_C2C % clk_period_ps ? 1 : 0;
833 val = max(val, dmc->min_tck->tW2W_C2C);
834 reg = &timing_data[3];
835 *reg_timing_data |= TIMING_VAL2REG(reg, val);
837 val = dmc->timings->tR2R_C2C / clk_period_ps;
838 val += dmc->timings->tR2R_C2C % clk_period_ps ? 1 : 0;
839 val = max(val, dmc->min_tck->tR2R_C2C);
840 reg = &timing_data[4];
841 *reg_timing_data |= TIMING_VAL2REG(reg, val);
843 val = dmc->timings->tWL / clk_period_ps;
844 val += dmc->timings->tWL % clk_period_ps ? 1 : 0;
845 val = max(val, dmc->min_tck->tWL);
846 reg = &timing_data[5];
847 *reg_timing_data |= TIMING_VAL2REG(reg, val);
849 val = dmc->timings->tDQSCK / clk_period_ps;
850 val += dmc->timings->tDQSCK % clk_period_ps ? 1 : 0;
851 val = max(val, dmc->min_tck->tDQSCK);
852 reg = &timing_data[6];
853 *reg_timing_data |= TIMING_VAL2REG(reg, val);
855 val = dmc->timings->tRL / clk_period_ps;
856 val += dmc->timings->tRL % clk_period_ps ? 1 : 0;
857 val = max(val, dmc->min_tck->tRL);
858 reg = &timing_data[7];
859 *reg_timing_data |= TIMING_VAL2REG(reg, val);
861 /* power related timings */
862 val = dmc->timings->tFAW / clk_period_ps;
863 val += dmc->timings->tFAW % clk_period_ps ? 1 : 0;
864 val = max(val, dmc->min_tck->tXP);
865 reg = &timing_power[0];
866 *reg_timing_power |= TIMING_VAL2REG(reg, val);
868 val = dmc->timings->tXSR / clk_period_ps;
869 val += dmc->timings->tXSR % clk_period_ps ? 1 : 0;
870 val = max(val, dmc->min_tck->tXSR);
871 reg = &timing_power[1];
872 *reg_timing_power |= TIMING_VAL2REG(reg, val);
874 val = dmc->timings->tXP / clk_period_ps;
875 val += dmc->timings->tXP % clk_period_ps ? 1 : 0;
876 val = max(val, dmc->min_tck->tXP);
877 reg = &timing_power[2];
878 *reg_timing_power |= TIMING_VAL2REG(reg, val);
880 val = dmc->timings->tCKE / clk_period_ps;
881 val += dmc->timings->tCKE % clk_period_ps ? 1 : 0;
882 val = max(val, dmc->min_tck->tCKE);
883 reg = &timing_power[3];
884 *reg_timing_power |= TIMING_VAL2REG(reg, val);
886 val = dmc->timings->tMRD / clk_period_ps;
887 val += dmc->timings->tMRD % clk_period_ps ? 1 : 0;
888 val = max(val, dmc->min_tck->tMRD);
889 reg = &timing_power[4];
890 *reg_timing_power |= TIMING_VAL2REG(reg, val);
896 * of_get_dram_timings() - helper function for parsing DT settings for DRAM
897 * @dmc: device for which the frequency is going to be set
899 * The function parses DT entries with DRAM information.
901 static int of_get_dram_timings(struct exynos5_dmc *dmc)
905 struct device_node *np_ddr;
906 u32 freq_mhz, clk_period_ps;
908 np_ddr = of_parse_phandle(dmc->dev->of_node, "device-handle", 0);
910 dev_warn(dmc->dev, "could not find 'device-handle' in DT\n");
914 dmc->timing_row = devm_kmalloc_array(dmc->dev, TIMING_COUNT,
915 sizeof(u32), GFP_KERNEL);
916 if (!dmc->timing_row)
919 dmc->timing_data = devm_kmalloc_array(dmc->dev, TIMING_COUNT,
920 sizeof(u32), GFP_KERNEL);
921 if (!dmc->timing_data)
924 dmc->timing_power = devm_kmalloc_array(dmc->dev, TIMING_COUNT,
925 sizeof(u32), GFP_KERNEL);
926 if (!dmc->timing_power)
929 dmc->timings = of_lpddr3_get_ddr_timings(np_ddr, dmc->dev,
931 &dmc->timings_arr_size);
934 dev_warn(dmc->dev, "could not get timings from DT\n");
938 dmc->min_tck = of_lpddr3_get_min_tck(np_ddr, dmc->dev);
941 dev_warn(dmc->dev, "could not get tck from DT\n");
945 /* Sorted array of OPPs with frequency ascending */
946 for (idx = 0; idx < dmc->opp_count; idx++) {
947 freq_mhz = dmc->opp[idx].freq_hz / 1000000;
948 clk_period_ps = 1000000 / freq_mhz;
950 ret = create_timings_aligned(dmc, &dmc->timing_row[idx],
951 &dmc->timing_data[idx],
952 &dmc->timing_power[idx],
958 /* Take the highest frequency's timings as 'bypass' */
959 dmc->bypass_timing_row = dmc->timing_row[idx - 1];
960 dmc->bypass_timing_data = dmc->timing_data[idx - 1];
961 dmc->bypass_timing_power = dmc->timing_power[idx - 1];
967 * exynos5_dmc_init_clks() - Initialize clocks needed for DMC operation.
968 * @dmc: DMC structure containing needed fields
970 * Get the needed clocks defined in DT device, enable and set the right parents.
971 * Read current frequency and initialize the initial rate for governor.
973 static int exynos5_dmc_init_clks(struct exynos5_dmc *dmc)
976 unsigned long target_volt = 0;
977 unsigned long target_rate = 0;
980 dmc->fout_spll = devm_clk_get(dmc->dev, "fout_spll");
981 if (IS_ERR(dmc->fout_spll))
982 return PTR_ERR(dmc->fout_spll);
984 dmc->fout_bpll = devm_clk_get(dmc->dev, "fout_bpll");
985 if (IS_ERR(dmc->fout_bpll))
986 return PTR_ERR(dmc->fout_bpll);
988 dmc->mout_mclk_cdrex = devm_clk_get(dmc->dev, "mout_mclk_cdrex");
989 if (IS_ERR(dmc->mout_mclk_cdrex))
990 return PTR_ERR(dmc->mout_mclk_cdrex);
992 dmc->mout_bpll = devm_clk_get(dmc->dev, "mout_bpll");
993 if (IS_ERR(dmc->mout_bpll))
994 return PTR_ERR(dmc->mout_bpll);
996 dmc->mout_mx_mspll_ccore = devm_clk_get(dmc->dev,
997 "mout_mx_mspll_ccore");
998 if (IS_ERR(dmc->mout_mx_mspll_ccore))
999 return PTR_ERR(dmc->mout_mx_mspll_ccore);
1001 dmc->mout_spll = devm_clk_get(dmc->dev, "ff_dout_spll2");
1002 if (IS_ERR(dmc->mout_spll)) {
1003 dmc->mout_spll = devm_clk_get(dmc->dev, "mout_sclk_spll");
1004 if (IS_ERR(dmc->mout_spll))
1005 return PTR_ERR(dmc->mout_spll);
1009 * Convert frequency to KHz values and set it for the governor.
1011 dmc->curr_rate = clk_get_rate(dmc->mout_mclk_cdrex);
1012 dmc->curr_rate = exynos5_dmc_align_init_freq(dmc, dmc->curr_rate);
1013 exynos5_dmc_df_profile.initial_freq = dmc->curr_rate;
1015 ret = exynos5_dmc_get_volt_freq(dmc, &dmc->curr_rate, &target_rate,
1020 dmc->curr_volt = target_volt;
1022 clk_set_parent(dmc->mout_mx_mspll_ccore, dmc->mout_spll);
1024 dmc->bypass_rate = clk_get_rate(dmc->mout_mx_mspll_ccore);
1026 clk_prepare_enable(dmc->fout_bpll);
1027 clk_prepare_enable(dmc->mout_bpll);
1030 * Some bootloaders do not set clock routes correctly.
1031 * Stop one path in clocks to PHY.
1033 regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, &tmp);
1034 tmp &= ~(BIT(1) | BIT(0));
1035 regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, tmp);
1041 * exynos5_performance_counters_init() - Initializes performance DMC's counters
1042 * @dmc: DMC for which it does the setup
1044 * Initialization of performance counters in DMC for estimating usage.
1045 * The counter's values are used for calculation of a memory bandwidth and based
1046 * on that the governor changes the frequency.
1047 * The counters are not used when the governor is GOVERNOR_USERSPACE.
1049 static int exynos5_performance_counters_init(struct exynos5_dmc *dmc)
1054 dmc->num_counters = devfreq_event_get_edev_count(dmc->dev);
1055 if (dmc->num_counters < 0) {
1056 dev_err(dmc->dev, "could not get devfreq-event counters\n");
1057 return dmc->num_counters;
1060 counters_size = sizeof(struct devfreq_event_dev) * dmc->num_counters;
1061 dmc->counter = devm_kzalloc(dmc->dev, counters_size, GFP_KERNEL);
1065 for (i = 0; i < dmc->num_counters; i++) {
1067 devfreq_event_get_edev_by_phandle(dmc->dev, i);
1068 if (IS_ERR_OR_NULL(dmc->counter[i]))
1069 return -EPROBE_DEFER;
1072 ret = exynos5_counters_enable_edev(dmc);
1074 dev_err(dmc->dev, "could not enable event counter\n");
1078 ret = exynos5_counters_set_event(dmc);
1080 exynos5_counters_disable_edev(dmc);
1081 dev_err(dmc->dev, "counld not set event counter\n");
1089 * exynos5_dmc_set_pause_on_switching() - Controls a pause feature in DMC
1090 * @dmc: device which is used for changing this feature
1091 * @set: a boolean state passing enable/disable request
1093 * There is a need of pausing DREX DMC when divider or MUX in clock tree
1094 * changes its configuration. In such situation access to the memory is blocked
1095 * in DMC automatically. This feature is used when clock frequency change
1096 * request appears and touches clock tree.
1098 static inline int exynos5_dmc_set_pause_on_switching(struct exynos5_dmc *dmc)
1103 ret = regmap_read(dmc->clk_regmap, CDREX_PAUSE, &val);
1108 regmap_write(dmc->clk_regmap, CDREX_PAUSE, val);
1114 * exynos5_dmc_probe() - Probe function for the DMC driver
1115 * @pdev: platform device for which the driver is going to be initialized
1117 * Initialize basic components: clocks, regulators, performance counters, etc.
1118 * Read out product version and based on the information setup
1119 * internal structures for the controller (frequency and voltage) and for DRAM
1120 * memory parameters: timings for each operating frequency.
1121 * Register new devfreq device for controlling DVFS of the DMC.
1123 static int exynos5_dmc_probe(struct platform_device *pdev)
1126 struct device *dev = &pdev->dev;
1127 struct device_node *np = dev->of_node;
1128 struct exynos5_dmc *dmc;
1129 struct resource *res;
1131 dmc = devm_kzalloc(dev, sizeof(*dmc), GFP_KERNEL);
1135 mutex_init(&dmc->lock);
1138 platform_set_drvdata(pdev, dmc);
1140 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1141 dmc->base_drexi0 = devm_ioremap_resource(dev, res);
1142 if (IS_ERR(dmc->base_drexi0))
1143 return PTR_ERR(dmc->base_drexi0);
1145 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1146 dmc->base_drexi1 = devm_ioremap_resource(dev, res);
1147 if (IS_ERR(dmc->base_drexi1))
1148 return PTR_ERR(dmc->base_drexi1);
1150 dmc->clk_regmap = syscon_regmap_lookup_by_phandle(np,
1151 "samsung,syscon-clk");
1152 if (IS_ERR(dmc->clk_regmap))
1153 return PTR_ERR(dmc->clk_regmap);
1155 ret = exynos5_init_freq_table(dmc, &exynos5_dmc_df_profile);
1157 dev_warn(dev, "couldn't initialize frequency settings\n");
1161 dmc->vdd_mif = devm_regulator_get(dev, "vdd");
1162 if (IS_ERR(dmc->vdd_mif)) {
1163 ret = PTR_ERR(dmc->vdd_mif);
1167 ret = exynos5_dmc_init_clks(dmc);
1171 ret = of_get_dram_timings(dmc);
1173 dev_warn(dev, "couldn't initialize timings settings\n");
1177 ret = exynos5_performance_counters_init(dmc);
1179 dev_warn(dev, "couldn't probe performance counters\n");
1183 ret = exynos5_dmc_set_pause_on_switching(dmc);
1185 dev_warn(dev, "couldn't get access to PAUSE register\n");
1186 goto err_devfreq_add;
1190 * Setup default thresholds for the devfreq governor.
1191 * The values are chosen based on experiments.
1193 dmc->gov_data.upthreshold = 30;
1194 dmc->gov_data.downdifferential = 5;
1196 dmc->df = devm_devfreq_add_device(dev, &exynos5_dmc_df_profile,
1197 DEVFREQ_GOV_SIMPLE_ONDEMAND,
1200 if (IS_ERR(dmc->df)) {
1201 ret = PTR_ERR(dmc->df);
1202 goto err_devfreq_add;
1205 dev_info(dev, "DMC initialized\n");
1210 exynos5_counters_disable_edev(dmc);
1212 clk_disable_unprepare(dmc->mout_bpll);
1213 clk_disable_unprepare(dmc->fout_bpll);
1219 * exynos5_dmc_remove() - Remove function for the platform device
1220 * @pdev: platform device which is going to be removed
1222 * The function relies on 'devm' framework function which automatically
1223 * clean the device's resources. It just calls explicitly disable function for
1224 * the performance counters.
1226 static int exynos5_dmc_remove(struct platform_device *pdev)
1228 struct exynos5_dmc *dmc = dev_get_drvdata(&pdev->dev);
1230 exynos5_counters_disable_edev(dmc);
1232 clk_disable_unprepare(dmc->mout_bpll);
1233 clk_disable_unprepare(dmc->fout_bpll);
1235 dev_pm_opp_remove_table(dmc->dev);
1240 static const struct of_device_id exynos5_dmc_of_match[] = {
1241 { .compatible = "samsung,exynos5422-dmc", },
1244 MODULE_DEVICE_TABLE(of, exynos5_dmc_of_match);
1246 static struct platform_driver exynos5_dmc_platdrv = {
1247 .probe = exynos5_dmc_probe,
1248 .remove = exynos5_dmc_remove,
1250 .name = "exynos5-dmc",
1251 .of_match_table = exynos5_dmc_of_match,
1254 module_platform_driver(exynos5_dmc_platdrv);
1255 MODULE_DESCRIPTION("Driver for Exynos5422 Dynamic Memory Controller dynamic frequency and voltage change");
1256 MODULE_LICENSE("GPL v2");
1257 MODULE_AUTHOR("Lukasz Luba");