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>
11 #include <linux/interrupt.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/pm_opp.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include "../jedec_ddr.h"
22 #include "../of_memory.h"
24 #define EXYNOS5_DREXI_TIMINGAREF (0x0030)
25 #define EXYNOS5_DREXI_TIMINGROW0 (0x0034)
26 #define EXYNOS5_DREXI_TIMINGDATA0 (0x0038)
27 #define EXYNOS5_DREXI_TIMINGPOWER0 (0x003C)
28 #define EXYNOS5_DREXI_TIMINGROW1 (0x00E4)
29 #define EXYNOS5_DREXI_TIMINGDATA1 (0x00E8)
30 #define EXYNOS5_DREXI_TIMINGPOWER1 (0x00EC)
31 #define CDREX_PAUSE (0x2091c)
32 #define CDREX_LPDDR3PHY_CON3 (0x20a20)
33 #define CDREX_LPDDR3PHY_CLKM_SRC (0x20700)
34 #define EXYNOS5_TIMING_SET_SWI BIT(28)
35 #define USE_MX_MSPLL_TIMINGS (1)
36 #define USE_BPLL_TIMINGS (0)
37 #define EXYNOS5_AREF_NORMAL (0x2e)
39 #define DREX_PPCCLKCON (0x0130)
40 #define DREX_PEREV2CONFIG (0x013c)
41 #define DREX_PMNC_PPC (0xE000)
42 #define DREX_CNTENS_PPC (0xE010)
43 #define DREX_CNTENC_PPC (0xE020)
44 #define DREX_INTENS_PPC (0xE030)
45 #define DREX_INTENC_PPC (0xE040)
46 #define DREX_FLAG_PPC (0xE050)
47 #define DREX_PMCNT2_PPC (0xE130)
50 * A value for register DREX_PMNC_PPC which should be written to reset
51 * the cycle counter CCNT (a reference wall clock). It sets zero to the
54 #define CC_RESET BIT(2)
57 * A value for register DREX_PMNC_PPC which does the reset of all performance
60 #define PPC_COUNTER_RESET BIT(1)
63 * Enables all configured counters (including cycle counter). The value should
64 * be written to the register DREX_PMNC_PPC.
66 #define PPC_ENABLE BIT(0)
68 /* A value for register DREX_PPCCLKCON which enables performance events clock.
69 * Must be written before first access to the performance counters register
70 * set, otherwise it could crash.
72 #define PEREV_CLK_EN BIT(0)
75 * Values which are used to enable counters, interrupts or configure flags of
76 * the performance counters. They configure counter 2 and cycle counter.
78 #define PERF_CNT2 BIT(2)
79 #define PERF_CCNT BIT(31)
82 * Performance event types which are used for setting the preferred event
83 * to track in the counters.
84 * There is a set of different types, the values are from range 0 to 0x6f.
85 * These settings should be written to the configuration register which manages
86 * the type of the event (register DREX_PEREV2CONFIG).
88 #define READ_TRANSFER_CH0 (0x6d)
89 #define READ_TRANSFER_CH1 (0x6f)
91 #define PERF_COUNTER_START_VALUE 0xff000000
92 #define PERF_EVENT_UP_DOWN_THRESHOLD 900000000ULL
95 * struct dmc_opp_table - Operating level desciption
97 * Covers frequency and voltage settings of the DMC operating mode.
99 struct dmc_opp_table
{
105 * struct exynos5_dmc - main structure describing DMC device
107 * The main structure for the Dynamic Memory Controller which covers clocks,
108 * memory regions, HW information, parameters and current operating mode.
113 struct devfreq_simple_ondemand_data gov_data
;
114 void __iomem
*base_drexi0
;
115 void __iomem
*base_drexi1
;
116 struct regmap
*clk_regmap
;
118 unsigned long curr_rate
;
119 unsigned long curr_volt
;
120 unsigned long bypass_rate
;
121 struct dmc_opp_table
*opp
;
122 struct dmc_opp_table opp_bypass
;
124 u32 timings_arr_size
;
128 const struct lpddr3_timings
*timings
;
129 const struct lpddr3_min_tck
*min_tck
;
130 u32 bypass_timing_row
;
131 u32 bypass_timing_data
;
132 u32 bypass_timing_power
;
133 struct regulator
*vdd_mif
;
134 struct clk
*fout_spll
;
135 struct clk
*fout_bpll
;
136 struct clk
*mout_spll
;
137 struct clk
*mout_bpll
;
138 struct clk
*mout_mclk_cdrex
;
139 struct clk
*mout_mx_mspll_ccore
;
140 struct clk
*mx_mspll_ccore_phy
;
141 struct clk
*mout_mx_mspll_ccore_phy
;
142 struct devfreq_event_dev
**counter
;
144 u64 last_overflow_ts
[2];
150 #define TIMING_FIELD(t_name, t_bit_beg, t_bit_end) \
151 { .name = t_name, .bit_beg = t_bit_beg, .bit_end = t_bit_end }
153 #define TIMING_VAL2REG(timing, t_val) \
156 __val = (t_val) << (timing)->bit_beg; \
167 static const struct timing_reg timing_row
[] = {
168 TIMING_FIELD("tRFC", 24, 31),
169 TIMING_FIELD("tRRD", 20, 23),
170 TIMING_FIELD("tRP", 16, 19),
171 TIMING_FIELD("tRCD", 12, 15),
172 TIMING_FIELD("tRC", 6, 11),
173 TIMING_FIELD("tRAS", 0, 5),
176 static const struct timing_reg timing_data
[] = {
177 TIMING_FIELD("tWTR", 28, 31),
178 TIMING_FIELD("tWR", 24, 27),
179 TIMING_FIELD("tRTP", 20, 23),
180 TIMING_FIELD("tW2W-C2C", 14, 14),
181 TIMING_FIELD("tR2R-C2C", 12, 12),
182 TIMING_FIELD("WL", 8, 11),
183 TIMING_FIELD("tDQSCK", 4, 7),
184 TIMING_FIELD("RL", 0, 3),
187 static const struct timing_reg timing_power
[] = {
188 TIMING_FIELD("tFAW", 26, 31),
189 TIMING_FIELD("tXSR", 16, 25),
190 TIMING_FIELD("tXP", 8, 15),
191 TIMING_FIELD("tCKE", 4, 7),
192 TIMING_FIELD("tMRD", 0, 3),
195 #define TIMING_COUNT (ARRAY_SIZE(timing_row) + ARRAY_SIZE(timing_data) + \
196 ARRAY_SIZE(timing_power))
198 static int exynos5_counters_set_event(struct exynos5_dmc
*dmc
)
202 for (i
= 0; i
< dmc
->num_counters
; i
++) {
203 if (!dmc
->counter
[i
])
205 ret
= devfreq_event_set_event(dmc
->counter
[i
]);
212 static int exynos5_counters_enable_edev(struct exynos5_dmc
*dmc
)
216 for (i
= 0; i
< dmc
->num_counters
; i
++) {
217 if (!dmc
->counter
[i
])
219 ret
= devfreq_event_enable_edev(dmc
->counter
[i
]);
226 static int exynos5_counters_disable_edev(struct exynos5_dmc
*dmc
)
230 for (i
= 0; i
< dmc
->num_counters
; i
++) {
231 if (!dmc
->counter
[i
])
233 ret
= devfreq_event_disable_edev(dmc
->counter
[i
]);
241 * find_target_freq_id() - Finds requested frequency in local DMC configuration
242 * @dmc: device for which the information is checked
243 * @target_rate: requested frequency in KHz
245 * Seeks in the local DMC driver structure for the requested frequency value
246 * and returns index or error value.
248 static int find_target_freq_idx(struct exynos5_dmc
*dmc
,
249 unsigned long target_rate
)
253 for (i
= dmc
->opp_count
- 1; i
>= 0; i
--)
254 if (dmc
->opp
[i
].freq_hz
<= target_rate
)
261 * exynos5_switch_timing_regs() - Changes bank register set for DRAM timings
262 * @dmc: device for which the new settings is going to be applied
263 * @set: boolean variable passing set value
265 * Changes the register set, which holds timing parameters.
266 * There is two register sets: 0 and 1. The register set 0
267 * is used in normal operation when the clock is provided from main PLL.
268 * The bank register set 1 is used when the main PLL frequency is going to be
269 * changed and the clock is taken from alternative, stable source.
270 * This function switches between these banks according to the
271 * currently used clock source.
273 static void exynos5_switch_timing_regs(struct exynos5_dmc
*dmc
, bool set
)
278 ret
= regmap_read(dmc
->clk_regmap
, CDREX_LPDDR3PHY_CON3
, ®
);
281 reg
|= EXYNOS5_TIMING_SET_SWI
;
283 reg
&= ~EXYNOS5_TIMING_SET_SWI
;
285 regmap_write(dmc
->clk_regmap
, CDREX_LPDDR3PHY_CON3
, reg
);
289 * exynos5_init_freq_table() - Initialized PM OPP framework
290 * @dmc: DMC device for which the frequencies are used for OPP init
291 * @profile: devfreq device's profile
293 * Populate the devfreq device's OPP table based on current frequency, voltage.
295 static int exynos5_init_freq_table(struct exynos5_dmc
*dmc
,
296 struct devfreq_dev_profile
*profile
)
302 ret
= dev_pm_opp_of_add_table(dmc
->dev
);
304 dev_err(dmc
->dev
, "Failed to get OPP table\n");
308 dmc
->opp_count
= dev_pm_opp_get_opp_count(dmc
->dev
);
310 dmc
->opp
= devm_kmalloc_array(dmc
->dev
, dmc
->opp_count
,
311 sizeof(struct dmc_opp_table
), GFP_KERNEL
);
315 idx
= dmc
->opp_count
- 1;
316 for (i
= 0, freq
= ULONG_MAX
; i
< dmc
->opp_count
; i
++, freq
--) {
317 struct dev_pm_opp
*opp
;
319 opp
= dev_pm_opp_find_freq_floor(dmc
->dev
, &freq
);
323 dmc
->opp
[idx
- i
].freq_hz
= freq
;
324 dmc
->opp
[idx
- i
].volt_uv
= dev_pm_opp_get_voltage(opp
);
332 dev_pm_opp_of_remove_table(dmc
->dev
);
338 * exynos5_set_bypass_dram_timings() - Low-level changes of the DRAM timings
339 * @dmc: device for which the new settings is going to be applied
340 * @param: DRAM parameters which passes timing data
342 * Low-level function for changing timings for DRAM memory clocking from
343 * 'bypass' clock source (fixed frequency @400MHz).
344 * It uses timing bank registers set 1.
346 static void exynos5_set_bypass_dram_timings(struct exynos5_dmc
*dmc
)
348 writel(EXYNOS5_AREF_NORMAL
,
349 dmc
->base_drexi0
+ EXYNOS5_DREXI_TIMINGAREF
);
351 writel(dmc
->bypass_timing_row
,
352 dmc
->base_drexi0
+ EXYNOS5_DREXI_TIMINGROW1
);
353 writel(dmc
->bypass_timing_row
,
354 dmc
->base_drexi1
+ EXYNOS5_DREXI_TIMINGROW1
);
355 writel(dmc
->bypass_timing_data
,
356 dmc
->base_drexi0
+ EXYNOS5_DREXI_TIMINGDATA1
);
357 writel(dmc
->bypass_timing_data
,
358 dmc
->base_drexi1
+ EXYNOS5_DREXI_TIMINGDATA1
);
359 writel(dmc
->bypass_timing_power
,
360 dmc
->base_drexi0
+ EXYNOS5_DREXI_TIMINGPOWER1
);
361 writel(dmc
->bypass_timing_power
,
362 dmc
->base_drexi1
+ EXYNOS5_DREXI_TIMINGPOWER1
);
366 * exynos5_dram_change_timings() - Low-level changes of the DRAM final timings
367 * @dmc: device for which the new settings is going to be applied
368 * @target_rate: target frequency of the DMC
370 * Low-level function for changing timings for DRAM memory operating from main
371 * clock source (BPLL), which can have different frequencies. Thus, each
372 * frequency must have corresponding timings register values in order to keep
374 * It uses timing bank registers set 0.
376 static int exynos5_dram_change_timings(struct exynos5_dmc
*dmc
,
377 unsigned long target_rate
)
381 for (idx
= dmc
->opp_count
- 1; idx
>= 0; idx
--)
382 if (dmc
->opp
[idx
].freq_hz
<= target_rate
)
388 writel(EXYNOS5_AREF_NORMAL
,
389 dmc
->base_drexi0
+ EXYNOS5_DREXI_TIMINGAREF
);
391 writel(dmc
->timing_row
[idx
],
392 dmc
->base_drexi0
+ EXYNOS5_DREXI_TIMINGROW0
);
393 writel(dmc
->timing_row
[idx
],
394 dmc
->base_drexi1
+ EXYNOS5_DREXI_TIMINGROW0
);
395 writel(dmc
->timing_data
[idx
],
396 dmc
->base_drexi0
+ EXYNOS5_DREXI_TIMINGDATA0
);
397 writel(dmc
->timing_data
[idx
],
398 dmc
->base_drexi1
+ EXYNOS5_DREXI_TIMINGDATA0
);
399 writel(dmc
->timing_power
[idx
],
400 dmc
->base_drexi0
+ EXYNOS5_DREXI_TIMINGPOWER0
);
401 writel(dmc
->timing_power
[idx
],
402 dmc
->base_drexi1
+ EXYNOS5_DREXI_TIMINGPOWER0
);
408 * exynos5_dmc_align_target_voltage() - Sets the final voltage for the DMC
409 * @dmc: device for which it is going to be set
410 * @target_volt: new voltage which is chosen to be final
412 * Function tries to align voltage to the safe level for 'normal' mode.
413 * It checks the need of higher voltage and changes the value. The target
414 * voltage might be lower that currently set and still the system will be
417 static int exynos5_dmc_align_target_voltage(struct exynos5_dmc
*dmc
,
418 unsigned long target_volt
)
422 if (dmc
->curr_volt
<= target_volt
)
425 ret
= regulator_set_voltage(dmc
->vdd_mif
, target_volt
,
428 dmc
->curr_volt
= target_volt
;
434 * exynos5_dmc_align_bypass_voltage() - Sets the voltage for the DMC
435 * @dmc: device for which it is going to be set
436 * @target_volt: new voltage which is chosen to be final
438 * Function tries to align voltage to the safe level for the 'bypass' mode.
439 * It checks the need of higher voltage and changes the value.
440 * The target voltage must not be less than currently needed, because
441 * for current frequency the device might become unstable.
443 static int exynos5_dmc_align_bypass_voltage(struct exynos5_dmc
*dmc
,
444 unsigned long target_volt
)
447 unsigned long bypass_volt
= dmc
->opp_bypass
.volt_uv
;
449 target_volt
= max(bypass_volt
, target_volt
);
451 if (dmc
->curr_volt
>= target_volt
)
454 ret
= regulator_set_voltage(dmc
->vdd_mif
, target_volt
,
457 dmc
->curr_volt
= target_volt
;
463 * exynos5_dmc_align_bypass_dram_timings() - Chooses and sets DRAM timings
464 * @dmc: device for which it is going to be set
465 * @target_rate: new frequency which is chosen to be final
467 * Function changes the DRAM timings for the temporary 'bypass' mode.
469 static int exynos5_dmc_align_bypass_dram_timings(struct exynos5_dmc
*dmc
,
470 unsigned long target_rate
)
472 int idx
= find_target_freq_idx(dmc
, target_rate
);
477 exynos5_set_bypass_dram_timings(dmc
);
483 * exynos5_dmc_switch_to_bypass_configuration() - Switching to temporary clock
484 * @dmc: DMC device for which the switching is going to happen
485 * @target_rate: new frequency which is going to be set as a final
486 * @target_volt: new voltage which is going to be set as a final
488 * Function configures DMC and clocks for operating in temporary 'bypass' mode.
489 * This mode is used only temporary but if required, changes voltage and timings
490 * for DRAM chips. It switches the main clock to stable clock source for the
491 * period of the main PLL reconfiguration.
494 exynos5_dmc_switch_to_bypass_configuration(struct exynos5_dmc
*dmc
,
495 unsigned long target_rate
,
496 unsigned long target_volt
)
501 * Having higher voltage for a particular frequency does not harm
502 * the chip. Use it for the temporary frequency change when one
503 * voltage manipulation might be avoided.
505 ret
= exynos5_dmc_align_bypass_voltage(dmc
, target_volt
);
510 * Longer delays for DRAM does not cause crash, the opposite does.
512 ret
= exynos5_dmc_align_bypass_dram_timings(dmc
, target_rate
);
517 * Delays are long enough, so use them for the new coming clock.
519 exynos5_switch_timing_regs(dmc
, USE_MX_MSPLL_TIMINGS
);
525 * exynos5_dmc_change_freq_and_volt() - Changes voltage and frequency of the DMC
526 * using safe procedure
527 * @dmc: device for which the frequency is going to be changed
528 * @target_rate: requested new frequency
529 * @target_volt: requested voltage which corresponds to the new frequency
531 * The DMC frequency change procedure requires a few steps.
532 * The main requirement is to change the clock source in the clk mux
533 * for the time of main clock PLL locking. The assumption is that the
534 * alternative clock source set as parent is stable.
535 * The second parent's clock frequency is fixed to 400MHz, it is named 'bypass'
536 * clock. This requires alignment in DRAM timing parameters for the new
537 * T-period. There is two bank sets for keeping DRAM
538 * timings: set 0 and set 1. The set 0 is used when main clock source is
539 * chosen. The 2nd set of regs is used for 'bypass' clock. Switching between
540 * the two bank sets is part of the process.
541 * The voltage must also be aligned to the minimum required level. There is
542 * this intermediate step with switching to 'bypass' parent clock source.
543 * if the old voltage is lower, it requires an increase of the voltage level.
544 * The complexity of the voltage manipulation is hidden in low level function.
545 * In this function there is last alignment of the voltage level at the end.
548 exynos5_dmc_change_freq_and_volt(struct exynos5_dmc
*dmc
,
549 unsigned long target_rate
,
550 unsigned long target_volt
)
554 ret
= exynos5_dmc_switch_to_bypass_configuration(dmc
, target_rate
,
560 * Voltage is set at least to a level needed for this frequency,
561 * so switching clock source is safe now.
563 clk_prepare_enable(dmc
->fout_spll
);
564 clk_prepare_enable(dmc
->mout_spll
);
565 clk_prepare_enable(dmc
->mout_mx_mspll_ccore
);
567 ret
= clk_set_parent(dmc
->mout_mclk_cdrex
, dmc
->mout_mx_mspll_ccore
);
572 * We are safe to increase the timings for current bypass frequency.
573 * Thanks to this the settings will be ready for the upcoming clock
576 exynos5_dram_change_timings(dmc
, target_rate
);
578 clk_set_rate(dmc
->fout_bpll
, target_rate
);
580 exynos5_switch_timing_regs(dmc
, USE_BPLL_TIMINGS
);
582 ret
= clk_set_parent(dmc
->mout_mclk_cdrex
, dmc
->mout_bpll
);
587 * Make sure if the voltage is not from 'bypass' settings and align to
588 * the right level for power efficiency.
590 ret
= exynos5_dmc_align_target_voltage(dmc
, target_volt
);
593 clk_disable_unprepare(dmc
->mout_mx_mspll_ccore
);
594 clk_disable_unprepare(dmc
->mout_spll
);
595 clk_disable_unprepare(dmc
->fout_spll
);
601 * exynos5_dmc_get_volt_freq() - Gets the frequency and voltage from the OPP
603 * @dmc: device for which the frequency is going to be changed
604 * @freq: requested frequency in KHz
605 * @target_rate: returned frequency which is the same or lower than
607 * @target_volt: returned voltage which corresponds to the returned
610 * Function gets requested frequency and checks OPP framework for needed
611 * frequency and voltage. It populates the values 'target_rate' and
612 * 'target_volt' or returns error value when OPP framework fails.
614 static int exynos5_dmc_get_volt_freq(struct exynos5_dmc
*dmc
,
616 unsigned long *target_rate
,
617 unsigned long *target_volt
, u32 flags
)
619 struct dev_pm_opp
*opp
;
621 opp
= devfreq_recommended_opp(dmc
->dev
, freq
, flags
);
625 *target_rate
= dev_pm_opp_get_freq(opp
);
626 *target_volt
= dev_pm_opp_get_voltage(opp
);
633 * exynos5_dmc_target() - Function responsible for changing frequency of DMC
634 * @dev: device for which the frequency is going to be changed
635 * @freq: requested frequency in KHz
636 * @flags: flags provided for this frequency change request
638 * An entry function provided to the devfreq framework which provides frequency
639 * change of the DMC. The function gets the possible rate from OPP table based
640 * on requested frequency. It calls the next function responsible for the
641 * frequency and voltage change. In case of failure, does not set 'curr_rate'
642 * and returns error value to the framework.
644 static int exynos5_dmc_target(struct device
*dev
, unsigned long *freq
,
647 struct exynos5_dmc
*dmc
= dev_get_drvdata(dev
);
648 unsigned long target_rate
= 0;
649 unsigned long target_volt
= 0;
652 ret
= exynos5_dmc_get_volt_freq(dmc
, freq
, &target_rate
, &target_volt
,
658 if (target_rate
== dmc
->curr_rate
)
661 mutex_lock(&dmc
->lock
);
663 ret
= exynos5_dmc_change_freq_and_volt(dmc
, target_rate
, target_volt
);
666 mutex_unlock(&dmc
->lock
);
670 dmc
->curr_rate
= target_rate
;
672 mutex_unlock(&dmc
->lock
);
677 * exynos5_counters_get() - Gets the performance counters values.
678 * @dmc: device for which the counters are going to be checked
679 * @load_count: variable which is populated with counter value
680 * @total_count: variable which is used as 'wall clock' reference
682 * Function which provides performance counters values. It sums up counters for
683 * two DMC channels. The 'total_count' is used as a reference and max value.
684 * The ratio 'load_count/total_count' shows the busy percentage [0%, 100%].
686 static int exynos5_counters_get(struct exynos5_dmc
*dmc
,
687 unsigned long *load_count
,
688 unsigned long *total_count
)
690 unsigned long total
= 0;
691 struct devfreq_event_data event
;
696 /* Take into account only read+write counters, but stop all */
697 for (i
= 0; i
< dmc
->num_counters
; i
++) {
698 if (!dmc
->counter
[i
])
701 ret
= devfreq_event_get_event(dmc
->counter
[i
], &event
);
705 *load_count
+= event
.load_count
;
707 if (total
< event
.total_count
)
708 total
= event
.total_count
;
711 *total_count
= total
;
717 * exynos5_dmc_start_perf_events() - Setup and start performance event counters
718 * @dmc: device for which the counters are going to be checked
719 * @beg_value: initial value for the counter
721 * Function which enables needed counters, interrupts and sets initial values
722 * then starts the counters.
724 static void exynos5_dmc_start_perf_events(struct exynos5_dmc
*dmc
,
727 /* Enable interrupts for counter 2 */
728 writel(PERF_CNT2
, dmc
->base_drexi0
+ DREX_INTENS_PPC
);
729 writel(PERF_CNT2
, dmc
->base_drexi1
+ DREX_INTENS_PPC
);
731 /* Enable counter 2 and CCNT */
732 writel(PERF_CNT2
| PERF_CCNT
, dmc
->base_drexi0
+ DREX_CNTENS_PPC
);
733 writel(PERF_CNT2
| PERF_CCNT
, dmc
->base_drexi1
+ DREX_CNTENS_PPC
);
735 /* Clear overflow flag for all counters */
736 writel(PERF_CNT2
| PERF_CCNT
, dmc
->base_drexi0
+ DREX_FLAG_PPC
);
737 writel(PERF_CNT2
| PERF_CCNT
, dmc
->base_drexi1
+ DREX_FLAG_PPC
);
739 /* Reset all counters */
740 writel(CC_RESET
| PPC_COUNTER_RESET
, dmc
->base_drexi0
+ DREX_PMNC_PPC
);
741 writel(CC_RESET
| PPC_COUNTER_RESET
, dmc
->base_drexi1
+ DREX_PMNC_PPC
);
744 * Set start value for the counters, the number of samples that
745 * will be gathered is calculated as: 0xffffffff - beg_value
747 writel(beg_value
, dmc
->base_drexi0
+ DREX_PMCNT2_PPC
);
748 writel(beg_value
, dmc
->base_drexi1
+ DREX_PMCNT2_PPC
);
750 /* Start all counters */
751 writel(PPC_ENABLE
, dmc
->base_drexi0
+ DREX_PMNC_PPC
);
752 writel(PPC_ENABLE
, dmc
->base_drexi1
+ DREX_PMNC_PPC
);
756 * exynos5_dmc_perf_events_calc() - Calculate utilization
757 * @dmc: device for which the counters are going to be checked
758 * @diff_ts: time between last interrupt and current one
760 * Function which calculates needed utilization for the devfreq governor.
761 * It prepares values for 'busy_time' and 'total_time' based on elapsed time
762 * between interrupts, which approximates utilization.
764 static void exynos5_dmc_perf_events_calc(struct exynos5_dmc
*dmc
, u64 diff_ts
)
767 * This is a simple algorithm for managing traffic on DMC.
768 * When there is almost no load the counters overflow every 4s,
769 * no mater the DMC frequency.
770 * The high load might be approximated using linear function.
771 * Knowing that, simple calculation can provide 'busy_time' and
772 * 'total_time' to the devfreq governor which picks up target
774 * We want a fast ramp up and slow decay in frequency change function.
776 if (diff_ts
< PERF_EVENT_UP_DOWN_THRESHOLD
) {
778 * Set higher utilization for the simple_ondemand governor.
779 * The governor should increase the frequency of the DMC.
785 * Set low utilization for the simple_ondemand governor.
786 * The governor should decrease the frequency of the DMC.
792 dev_dbg(dmc
->dev
, "diff_ts=%llu\n", diff_ts
);
796 * exynos5_dmc_perf_events_check() - Checks the status of the counters
797 * @dmc: device for which the counters are going to be checked
799 * Function which is called from threaded IRQ to check the counters state
800 * and to call approximation for the needed utilization.
802 static void exynos5_dmc_perf_events_check(struct exynos5_dmc
*dmc
)
809 /* Stop all counters */
810 writel(0, dmc
->base_drexi0
+ DREX_PMNC_PPC
);
811 writel(0, dmc
->base_drexi1
+ DREX_PMNC_PPC
);
813 /* Check the source in interrupt flag registers (which channel) */
814 val
= readl(dmc
->base_drexi0
+ DREX_FLAG_PPC
);
816 diff_ts
= ts
- dmc
->last_overflow_ts
[0];
817 dmc
->last_overflow_ts
[0] = ts
;
818 dev_dbg(dmc
->dev
, "drex0 0xE050 val= 0x%08x\n", val
);
820 val
= readl(dmc
->base_drexi1
+ DREX_FLAG_PPC
);
821 diff_ts
= ts
- dmc
->last_overflow_ts
[1];
822 dmc
->last_overflow_ts
[1] = ts
;
823 dev_dbg(dmc
->dev
, "drex1 0xE050 val= 0x%08x\n", val
);
826 exynos5_dmc_perf_events_calc(dmc
, diff_ts
);
828 exynos5_dmc_start_perf_events(dmc
, PERF_COUNTER_START_VALUE
);
832 * exynos5_dmc_enable_perf_events() - Enable performance events
833 * @dmc: device for which the counters are going to be checked
835 * Function which is setup needed environment and enables counters.
837 static void exynos5_dmc_enable_perf_events(struct exynos5_dmc
*dmc
)
841 /* Enable Performance Event Clock */
842 writel(PEREV_CLK_EN
, dmc
->base_drexi0
+ DREX_PPCCLKCON
);
843 writel(PEREV_CLK_EN
, dmc
->base_drexi1
+ DREX_PPCCLKCON
);
845 /* Select read transfers as performance event2 */
846 writel(READ_TRANSFER_CH0
, dmc
->base_drexi0
+ DREX_PEREV2CONFIG
);
847 writel(READ_TRANSFER_CH1
, dmc
->base_drexi1
+ DREX_PEREV2CONFIG
);
850 dmc
->last_overflow_ts
[0] = ts
;
851 dmc
->last_overflow_ts
[1] = ts
;
853 /* Devfreq shouldn't be faster than initialization, play safe though. */
859 * exynos5_dmc_disable_perf_events() - Disable performance events
860 * @dmc: device for which the counters are going to be checked
862 * Function which stops, disables performance event counters and interrupts.
864 static void exynos5_dmc_disable_perf_events(struct exynos5_dmc
*dmc
)
866 /* Stop all counters */
867 writel(0, dmc
->base_drexi0
+ DREX_PMNC_PPC
);
868 writel(0, dmc
->base_drexi1
+ DREX_PMNC_PPC
);
870 /* Disable interrupts for counter 2 */
871 writel(PERF_CNT2
, dmc
->base_drexi0
+ DREX_INTENC_PPC
);
872 writel(PERF_CNT2
, dmc
->base_drexi1
+ DREX_INTENC_PPC
);
874 /* Disable counter 2 and CCNT */
875 writel(PERF_CNT2
| PERF_CCNT
, dmc
->base_drexi0
+ DREX_CNTENC_PPC
);
876 writel(PERF_CNT2
| PERF_CCNT
, dmc
->base_drexi1
+ DREX_CNTENC_PPC
);
878 /* Clear overflow flag for all counters */
879 writel(PERF_CNT2
| PERF_CCNT
, dmc
->base_drexi0
+ DREX_FLAG_PPC
);
880 writel(PERF_CNT2
| PERF_CCNT
, dmc
->base_drexi1
+ DREX_FLAG_PPC
);
884 * exynos5_dmc_get_status() - Read current DMC performance statistics.
885 * @dev: device for which the statistics are requested
886 * @stat: structure which has statistic fields
888 * Function reads the DMC performance counters and calculates 'busy_time'
889 * and 'total_time'. To protect from overflow, the values are shifted right
890 * by 10. After read out the counters are setup to count again.
892 static int exynos5_dmc_get_status(struct device
*dev
,
893 struct devfreq_dev_status
*stat
)
895 struct exynos5_dmc
*dmc
= dev_get_drvdata(dev
);
896 unsigned long load
, total
;
899 if (dmc
->in_irq_mode
) {
900 stat
->current_frequency
= dmc
->curr_rate
;
901 stat
->busy_time
= dmc
->load
;
902 stat
->total_time
= dmc
->total
;
904 ret
= exynos5_counters_get(dmc
, &load
, &total
);
908 /* To protect from overflow, divide by 1024 */
909 stat
->busy_time
= load
>> 10;
910 stat
->total_time
= total
>> 10;
912 ret
= exynos5_counters_set_event(dmc
);
914 dev_err(dev
, "could not set event counter\n");
923 * exynos5_dmc_get_cur_freq() - Function returns current DMC frequency
924 * @dev: device for which the framework checks operating frequency
925 * @freq: returned frequency value
927 * It returns the currently used frequency of the DMC. The real operating
928 * frequency might be lower when the clock source value could not be divided
929 * to the requested value.
931 static int exynos5_dmc_get_cur_freq(struct device
*dev
, unsigned long *freq
)
933 struct exynos5_dmc
*dmc
= dev_get_drvdata(dev
);
935 mutex_lock(&dmc
->lock
);
936 *freq
= dmc
->curr_rate
;
937 mutex_unlock(&dmc
->lock
);
943 * exynos5_dmc_df_profile - Devfreq governor's profile structure
945 * It provides to the devfreq framework needed functions and polling period.
947 static struct devfreq_dev_profile exynos5_dmc_df_profile
= {
948 .target
= exynos5_dmc_target
,
949 .get_dev_status
= exynos5_dmc_get_status
,
950 .get_cur_freq
= exynos5_dmc_get_cur_freq
,
954 * exynos5_dmc_align_initial_frequency() - Align initial frequency value
955 * @dmc: device for which the frequency is going to be set
956 * @bootloader_init_freq: initial frequency set by the bootloader in KHz
958 * The initial bootloader frequency, which is present during boot, might be
959 * different that supported frequency values in the driver. It is possible
960 * due to different PLL settings or used PLL as a source.
961 * This function provides the 'initial_freq' for the devfreq framework
962 * statistics engine which supports only registered values. Thus, some alignment
966 exynos5_dmc_align_init_freq(struct exynos5_dmc
*dmc
,
967 unsigned long bootloader_init_freq
)
969 unsigned long aligned_freq
;
972 idx
= find_target_freq_idx(dmc
, bootloader_init_freq
);
974 aligned_freq
= dmc
->opp
[idx
].freq_hz
;
976 aligned_freq
= dmc
->opp
[dmc
->opp_count
- 1].freq_hz
;
982 * create_timings_aligned() - Create register values and align with standard
983 * @dmc: device for which the frequency is going to be set
984 * @idx: speed bin in the OPP table
985 * @clk_period_ps: the period of the clock, known as tCK
987 * The function calculates timings and creates a register value ready for
988 * a frequency transition. The register contains a few timings. They are
989 * shifted by a known offset. The timing value is calculated based on memory
990 * specyfication: minimal time required and minimal cycles required.
992 static int create_timings_aligned(struct exynos5_dmc
*dmc
, u32
*reg_timing_row
,
993 u32
*reg_timing_data
, u32
*reg_timing_power
,
997 const struct timing_reg
*reg
;
999 if (clk_period_ps
== 0)
1002 *reg_timing_row
= 0;
1003 *reg_timing_data
= 0;
1004 *reg_timing_power
= 0;
1006 val
= dmc
->timings
->tRFC
/ clk_period_ps
;
1007 val
+= dmc
->timings
->tRFC
% clk_period_ps
? 1 : 0;
1008 val
= max(val
, dmc
->min_tck
->tRFC
);
1009 reg
= &timing_row
[0];
1010 *reg_timing_row
|= TIMING_VAL2REG(reg
, val
);
1012 val
= dmc
->timings
->tRRD
/ clk_period_ps
;
1013 val
+= dmc
->timings
->tRRD
% clk_period_ps
? 1 : 0;
1014 val
= max(val
, dmc
->min_tck
->tRRD
);
1015 reg
= &timing_row
[1];
1016 *reg_timing_row
|= TIMING_VAL2REG(reg
, val
);
1018 val
= dmc
->timings
->tRPab
/ clk_period_ps
;
1019 val
+= dmc
->timings
->tRPab
% clk_period_ps
? 1 : 0;
1020 val
= max(val
, dmc
->min_tck
->tRPab
);
1021 reg
= &timing_row
[2];
1022 *reg_timing_row
|= TIMING_VAL2REG(reg
, val
);
1024 val
= dmc
->timings
->tRCD
/ clk_period_ps
;
1025 val
+= dmc
->timings
->tRCD
% clk_period_ps
? 1 : 0;
1026 val
= max(val
, dmc
->min_tck
->tRCD
);
1027 reg
= &timing_row
[3];
1028 *reg_timing_row
|= TIMING_VAL2REG(reg
, val
);
1030 val
= dmc
->timings
->tRC
/ clk_period_ps
;
1031 val
+= dmc
->timings
->tRC
% clk_period_ps
? 1 : 0;
1032 val
= max(val
, dmc
->min_tck
->tRC
);
1033 reg
= &timing_row
[4];
1034 *reg_timing_row
|= TIMING_VAL2REG(reg
, val
);
1036 val
= dmc
->timings
->tRAS
/ clk_period_ps
;
1037 val
+= dmc
->timings
->tRAS
% clk_period_ps
? 1 : 0;
1038 val
= max(val
, dmc
->min_tck
->tRAS
);
1039 reg
= &timing_row
[5];
1040 *reg_timing_row
|= TIMING_VAL2REG(reg
, val
);
1042 /* data related timings */
1043 val
= dmc
->timings
->tWTR
/ clk_period_ps
;
1044 val
+= dmc
->timings
->tWTR
% clk_period_ps
? 1 : 0;
1045 val
= max(val
, dmc
->min_tck
->tWTR
);
1046 reg
= &timing_data
[0];
1047 *reg_timing_data
|= TIMING_VAL2REG(reg
, val
);
1049 val
= dmc
->timings
->tWR
/ clk_period_ps
;
1050 val
+= dmc
->timings
->tWR
% clk_period_ps
? 1 : 0;
1051 val
= max(val
, dmc
->min_tck
->tWR
);
1052 reg
= &timing_data
[1];
1053 *reg_timing_data
|= TIMING_VAL2REG(reg
, val
);
1055 val
= dmc
->timings
->tRTP
/ clk_period_ps
;
1056 val
+= dmc
->timings
->tRTP
% clk_period_ps
? 1 : 0;
1057 val
= max(val
, dmc
->min_tck
->tRTP
);
1058 reg
= &timing_data
[2];
1059 *reg_timing_data
|= TIMING_VAL2REG(reg
, val
);
1061 val
= dmc
->timings
->tW2W_C2C
/ clk_period_ps
;
1062 val
+= dmc
->timings
->tW2W_C2C
% clk_period_ps
? 1 : 0;
1063 val
= max(val
, dmc
->min_tck
->tW2W_C2C
);
1064 reg
= &timing_data
[3];
1065 *reg_timing_data
|= TIMING_VAL2REG(reg
, val
);
1067 val
= dmc
->timings
->tR2R_C2C
/ clk_period_ps
;
1068 val
+= dmc
->timings
->tR2R_C2C
% clk_period_ps
? 1 : 0;
1069 val
= max(val
, dmc
->min_tck
->tR2R_C2C
);
1070 reg
= &timing_data
[4];
1071 *reg_timing_data
|= TIMING_VAL2REG(reg
, val
);
1073 val
= dmc
->timings
->tWL
/ clk_period_ps
;
1074 val
+= dmc
->timings
->tWL
% clk_period_ps
? 1 : 0;
1075 val
= max(val
, dmc
->min_tck
->tWL
);
1076 reg
= &timing_data
[5];
1077 *reg_timing_data
|= TIMING_VAL2REG(reg
, val
);
1079 val
= dmc
->timings
->tDQSCK
/ clk_period_ps
;
1080 val
+= dmc
->timings
->tDQSCK
% clk_period_ps
? 1 : 0;
1081 val
= max(val
, dmc
->min_tck
->tDQSCK
);
1082 reg
= &timing_data
[6];
1083 *reg_timing_data
|= TIMING_VAL2REG(reg
, val
);
1085 val
= dmc
->timings
->tRL
/ clk_period_ps
;
1086 val
+= dmc
->timings
->tRL
% clk_period_ps
? 1 : 0;
1087 val
= max(val
, dmc
->min_tck
->tRL
);
1088 reg
= &timing_data
[7];
1089 *reg_timing_data
|= TIMING_VAL2REG(reg
, val
);
1091 /* power related timings */
1092 val
= dmc
->timings
->tFAW
/ clk_period_ps
;
1093 val
+= dmc
->timings
->tFAW
% clk_period_ps
? 1 : 0;
1094 val
= max(val
, dmc
->min_tck
->tXP
);
1095 reg
= &timing_power
[0];
1096 *reg_timing_power
|= TIMING_VAL2REG(reg
, val
);
1098 val
= dmc
->timings
->tXSR
/ clk_period_ps
;
1099 val
+= dmc
->timings
->tXSR
% clk_period_ps
? 1 : 0;
1100 val
= max(val
, dmc
->min_tck
->tXSR
);
1101 reg
= &timing_power
[1];
1102 *reg_timing_power
|= TIMING_VAL2REG(reg
, val
);
1104 val
= dmc
->timings
->tXP
/ clk_period_ps
;
1105 val
+= dmc
->timings
->tXP
% clk_period_ps
? 1 : 0;
1106 val
= max(val
, dmc
->min_tck
->tXP
);
1107 reg
= &timing_power
[2];
1108 *reg_timing_power
|= TIMING_VAL2REG(reg
, val
);
1110 val
= dmc
->timings
->tCKE
/ clk_period_ps
;
1111 val
+= dmc
->timings
->tCKE
% clk_period_ps
? 1 : 0;
1112 val
= max(val
, dmc
->min_tck
->tCKE
);
1113 reg
= &timing_power
[3];
1114 *reg_timing_power
|= TIMING_VAL2REG(reg
, val
);
1116 val
= dmc
->timings
->tMRD
/ clk_period_ps
;
1117 val
+= dmc
->timings
->tMRD
% clk_period_ps
? 1 : 0;
1118 val
= max(val
, dmc
->min_tck
->tMRD
);
1119 reg
= &timing_power
[4];
1120 *reg_timing_power
|= TIMING_VAL2REG(reg
, val
);
1126 * of_get_dram_timings() - helper function for parsing DT settings for DRAM
1127 * @dmc: device for which the frequency is going to be set
1129 * The function parses DT entries with DRAM information.
1131 static int of_get_dram_timings(struct exynos5_dmc
*dmc
)
1135 struct device_node
*np_ddr
;
1136 u32 freq_mhz
, clk_period_ps
;
1138 np_ddr
= of_parse_phandle(dmc
->dev
->of_node
, "device-handle", 0);
1140 dev_warn(dmc
->dev
, "could not find 'device-handle' in DT\n");
1144 dmc
->timing_row
= devm_kmalloc_array(dmc
->dev
, TIMING_COUNT
,
1145 sizeof(u32
), GFP_KERNEL
);
1146 if (!dmc
->timing_row
)
1149 dmc
->timing_data
= devm_kmalloc_array(dmc
->dev
, TIMING_COUNT
,
1150 sizeof(u32
), GFP_KERNEL
);
1151 if (!dmc
->timing_data
)
1154 dmc
->timing_power
= devm_kmalloc_array(dmc
->dev
, TIMING_COUNT
,
1155 sizeof(u32
), GFP_KERNEL
);
1156 if (!dmc
->timing_power
)
1159 dmc
->timings
= of_lpddr3_get_ddr_timings(np_ddr
, dmc
->dev
,
1161 &dmc
->timings_arr_size
);
1162 if (!dmc
->timings
) {
1163 of_node_put(np_ddr
);
1164 dev_warn(dmc
->dev
, "could not get timings from DT\n");
1168 dmc
->min_tck
= of_lpddr3_get_min_tck(np_ddr
, dmc
->dev
);
1169 if (!dmc
->min_tck
) {
1170 of_node_put(np_ddr
);
1171 dev_warn(dmc
->dev
, "could not get tck from DT\n");
1175 /* Sorted array of OPPs with frequency ascending */
1176 for (idx
= 0; idx
< dmc
->opp_count
; idx
++) {
1177 freq_mhz
= dmc
->opp
[idx
].freq_hz
/ 1000000;
1178 clk_period_ps
= 1000000 / freq_mhz
;
1180 ret
= create_timings_aligned(dmc
, &dmc
->timing_row
[idx
],
1181 &dmc
->timing_data
[idx
],
1182 &dmc
->timing_power
[idx
],
1186 of_node_put(np_ddr
);
1188 /* Take the highest frequency's timings as 'bypass' */
1189 dmc
->bypass_timing_row
= dmc
->timing_row
[idx
- 1];
1190 dmc
->bypass_timing_data
= dmc
->timing_data
[idx
- 1];
1191 dmc
->bypass_timing_power
= dmc
->timing_power
[idx
- 1];
1197 * exynos5_dmc_init_clks() - Initialize clocks needed for DMC operation.
1198 * @dmc: DMC structure containing needed fields
1200 * Get the needed clocks defined in DT device, enable and set the right parents.
1201 * Read current frequency and initialize the initial rate for governor.
1203 static int exynos5_dmc_init_clks(struct exynos5_dmc
*dmc
)
1206 unsigned long target_volt
= 0;
1207 unsigned long target_rate
= 0;
1210 dmc
->fout_spll
= devm_clk_get(dmc
->dev
, "fout_spll");
1211 if (IS_ERR(dmc
->fout_spll
))
1212 return PTR_ERR(dmc
->fout_spll
);
1214 dmc
->fout_bpll
= devm_clk_get(dmc
->dev
, "fout_bpll");
1215 if (IS_ERR(dmc
->fout_bpll
))
1216 return PTR_ERR(dmc
->fout_bpll
);
1218 dmc
->mout_mclk_cdrex
= devm_clk_get(dmc
->dev
, "mout_mclk_cdrex");
1219 if (IS_ERR(dmc
->mout_mclk_cdrex
))
1220 return PTR_ERR(dmc
->mout_mclk_cdrex
);
1222 dmc
->mout_bpll
= devm_clk_get(dmc
->dev
, "mout_bpll");
1223 if (IS_ERR(dmc
->mout_bpll
))
1224 return PTR_ERR(dmc
->mout_bpll
);
1226 dmc
->mout_mx_mspll_ccore
= devm_clk_get(dmc
->dev
,
1227 "mout_mx_mspll_ccore");
1228 if (IS_ERR(dmc
->mout_mx_mspll_ccore
))
1229 return PTR_ERR(dmc
->mout_mx_mspll_ccore
);
1231 dmc
->mout_spll
= devm_clk_get(dmc
->dev
, "ff_dout_spll2");
1232 if (IS_ERR(dmc
->mout_spll
)) {
1233 dmc
->mout_spll
= devm_clk_get(dmc
->dev
, "mout_sclk_spll");
1234 if (IS_ERR(dmc
->mout_spll
))
1235 return PTR_ERR(dmc
->mout_spll
);
1239 * Convert frequency to KHz values and set it for the governor.
1241 dmc
->curr_rate
= clk_get_rate(dmc
->mout_mclk_cdrex
);
1242 dmc
->curr_rate
= exynos5_dmc_align_init_freq(dmc
, dmc
->curr_rate
);
1243 exynos5_dmc_df_profile
.initial_freq
= dmc
->curr_rate
;
1245 ret
= exynos5_dmc_get_volt_freq(dmc
, &dmc
->curr_rate
, &target_rate
,
1250 dmc
->curr_volt
= target_volt
;
1252 clk_set_parent(dmc
->mout_mx_mspll_ccore
, dmc
->mout_spll
);
1254 dmc
->bypass_rate
= clk_get_rate(dmc
->mout_mx_mspll_ccore
);
1256 clk_prepare_enable(dmc
->fout_bpll
);
1257 clk_prepare_enable(dmc
->mout_bpll
);
1260 * Some bootloaders do not set clock routes correctly.
1261 * Stop one path in clocks to PHY.
1263 regmap_read(dmc
->clk_regmap
, CDREX_LPDDR3PHY_CLKM_SRC
, &tmp
);
1264 tmp
&= ~(BIT(1) | BIT(0));
1265 regmap_write(dmc
->clk_regmap
, CDREX_LPDDR3PHY_CLKM_SRC
, tmp
);
1271 * exynos5_performance_counters_init() - Initializes performance DMC's counters
1272 * @dmc: DMC for which it does the setup
1274 * Initialization of performance counters in DMC for estimating usage.
1275 * The counter's values are used for calculation of a memory bandwidth and based
1276 * on that the governor changes the frequency.
1277 * The counters are not used when the governor is GOVERNOR_USERSPACE.
1279 static int exynos5_performance_counters_init(struct exynos5_dmc
*dmc
)
1284 dmc
->num_counters
= devfreq_event_get_edev_count(dmc
->dev
);
1285 if (dmc
->num_counters
< 0) {
1286 dev_err(dmc
->dev
, "could not get devfreq-event counters\n");
1287 return dmc
->num_counters
;
1290 counters_size
= sizeof(struct devfreq_event_dev
) * dmc
->num_counters
;
1291 dmc
->counter
= devm_kzalloc(dmc
->dev
, counters_size
, GFP_KERNEL
);
1295 for (i
= 0; i
< dmc
->num_counters
; i
++) {
1297 devfreq_event_get_edev_by_phandle(dmc
->dev
, i
);
1298 if (IS_ERR_OR_NULL(dmc
->counter
[i
]))
1299 return -EPROBE_DEFER
;
1302 ret
= exynos5_counters_enable_edev(dmc
);
1304 dev_err(dmc
->dev
, "could not enable event counter\n");
1308 ret
= exynos5_counters_set_event(dmc
);
1310 exynos5_counters_disable_edev(dmc
);
1311 dev_err(dmc
->dev
, "could not set event counter\n");
1319 * exynos5_dmc_set_pause_on_switching() - Controls a pause feature in DMC
1320 * @dmc: device which is used for changing this feature
1321 * @set: a boolean state passing enable/disable request
1323 * There is a need of pausing DREX DMC when divider or MUX in clock tree
1324 * changes its configuration. In such situation access to the memory is blocked
1325 * in DMC automatically. This feature is used when clock frequency change
1326 * request appears and touches clock tree.
1328 static inline int exynos5_dmc_set_pause_on_switching(struct exynos5_dmc
*dmc
)
1333 ret
= regmap_read(dmc
->clk_regmap
, CDREX_PAUSE
, &val
);
1338 regmap_write(dmc
->clk_regmap
, CDREX_PAUSE
, val
);
1343 static irqreturn_t
dmc_irq_thread(int irq
, void *priv
)
1346 struct exynos5_dmc
*dmc
= priv
;
1348 mutex_lock(&dmc
->df
->lock
);
1350 exynos5_dmc_perf_events_check(dmc
);
1352 res
= update_devfreq(dmc
->df
);
1354 dev_warn(dmc
->dev
, "devfreq failed with %d\n", res
);
1356 mutex_unlock(&dmc
->df
->lock
);
1362 * exynos5_dmc_probe() - Probe function for the DMC driver
1363 * @pdev: platform device for which the driver is going to be initialized
1365 * Initialize basic components: clocks, regulators, performance counters, etc.
1366 * Read out product version and based on the information setup
1367 * internal structures for the controller (frequency and voltage) and for DRAM
1368 * memory parameters: timings for each operating frequency.
1369 * Register new devfreq device for controlling DVFS of the DMC.
1371 static int exynos5_dmc_probe(struct platform_device
*pdev
)
1374 struct device
*dev
= &pdev
->dev
;
1375 struct device_node
*np
= dev
->of_node
;
1376 struct exynos5_dmc
*dmc
;
1377 struct resource
*res
;
1380 dmc
= devm_kzalloc(dev
, sizeof(*dmc
), GFP_KERNEL
);
1384 mutex_init(&dmc
->lock
);
1387 platform_set_drvdata(pdev
, dmc
);
1389 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1390 dmc
->base_drexi0
= devm_ioremap_resource(dev
, res
);
1391 if (IS_ERR(dmc
->base_drexi0
))
1392 return PTR_ERR(dmc
->base_drexi0
);
1394 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1395 dmc
->base_drexi1
= devm_ioremap_resource(dev
, res
);
1396 if (IS_ERR(dmc
->base_drexi1
))
1397 return PTR_ERR(dmc
->base_drexi1
);
1399 dmc
->clk_regmap
= syscon_regmap_lookup_by_phandle(np
,
1400 "samsung,syscon-clk");
1401 if (IS_ERR(dmc
->clk_regmap
))
1402 return PTR_ERR(dmc
->clk_regmap
);
1404 ret
= exynos5_init_freq_table(dmc
, &exynos5_dmc_df_profile
);
1406 dev_warn(dev
, "couldn't initialize frequency settings\n");
1410 dmc
->vdd_mif
= devm_regulator_get(dev
, "vdd");
1411 if (IS_ERR(dmc
->vdd_mif
)) {
1412 ret
= PTR_ERR(dmc
->vdd_mif
);
1416 ret
= exynos5_dmc_init_clks(dmc
);
1420 ret
= of_get_dram_timings(dmc
);
1422 dev_warn(dev
, "couldn't initialize timings settings\n");
1426 ret
= exynos5_dmc_set_pause_on_switching(dmc
);
1428 dev_warn(dev
, "couldn't get access to PAUSE register\n");
1432 /* There is two modes in which the driver works: polling or IRQ */
1433 irq
[0] = platform_get_irq_byname(pdev
, "drex_0");
1434 irq
[1] = platform_get_irq_byname(pdev
, "drex_1");
1435 if (irq
[0] > 0 && irq
[1] > 0) {
1436 ret
= devm_request_threaded_irq(dev
, irq
[0], NULL
,
1437 dmc_irq_thread
, IRQF_ONESHOT
,
1438 dev_name(dev
), dmc
);
1440 dev_err(dev
, "couldn't grab IRQ\n");
1444 ret
= devm_request_threaded_irq(dev
, irq
[1], NULL
,
1445 dmc_irq_thread
, IRQF_ONESHOT
,
1446 dev_name(dev
), dmc
);
1448 dev_err(dev
, "couldn't grab IRQ\n");
1453 * Setup default thresholds for the devfreq governor.
1454 * The values are chosen based on experiments.
1456 dmc
->gov_data
.upthreshold
= 55;
1457 dmc
->gov_data
.downdifferential
= 5;
1459 exynos5_dmc_enable_perf_events(dmc
);
1461 dmc
->in_irq_mode
= 1;
1463 ret
= exynos5_performance_counters_init(dmc
);
1465 dev_warn(dev
, "couldn't probe performance counters\n");
1470 * Setup default thresholds for the devfreq governor.
1471 * The values are chosen based on experiments.
1473 dmc
->gov_data
.upthreshold
= 30;
1474 dmc
->gov_data
.downdifferential
= 5;
1476 exynos5_dmc_df_profile
.polling_ms
= 500;
1480 dmc
->df
= devm_devfreq_add_device(dev
, &exynos5_dmc_df_profile
,
1481 DEVFREQ_GOV_SIMPLE_ONDEMAND
,
1484 if (IS_ERR(dmc
->df
)) {
1485 ret
= PTR_ERR(dmc
->df
);
1486 goto err_devfreq_add
;
1489 if (dmc
->in_irq_mode
)
1490 exynos5_dmc_start_perf_events(dmc
, PERF_COUNTER_START_VALUE
);
1492 dev_info(dev
, "DMC initialized\n");
1497 if (dmc
->in_irq_mode
)
1498 exynos5_dmc_disable_perf_events(dmc
);
1500 exynos5_counters_disable_edev(dmc
);
1502 clk_disable_unprepare(dmc
->mout_bpll
);
1503 clk_disable_unprepare(dmc
->fout_bpll
);
1509 * exynos5_dmc_remove() - Remove function for the platform device
1510 * @pdev: platform device which is going to be removed
1512 * The function relies on 'devm' framework function which automatically
1513 * clean the device's resources. It just calls explicitly disable function for
1514 * the performance counters.
1516 static int exynos5_dmc_remove(struct platform_device
*pdev
)
1518 struct exynos5_dmc
*dmc
= dev_get_drvdata(&pdev
->dev
);
1520 if (dmc
->in_irq_mode
)
1521 exynos5_dmc_disable_perf_events(dmc
);
1523 exynos5_counters_disable_edev(dmc
);
1525 clk_disable_unprepare(dmc
->mout_bpll
);
1526 clk_disable_unprepare(dmc
->fout_bpll
);
1528 dev_pm_opp_remove_table(dmc
->dev
);
1533 static const struct of_device_id exynos5_dmc_of_match
[] = {
1534 { .compatible
= "samsung,exynos5422-dmc", },
1537 MODULE_DEVICE_TABLE(of
, exynos5_dmc_of_match
);
1539 static struct platform_driver exynos5_dmc_platdrv
= {
1540 .probe
= exynos5_dmc_probe
,
1541 .remove
= exynos5_dmc_remove
,
1543 .name
= "exynos5-dmc",
1544 .of_match_table
= exynos5_dmc_of_match
,
1547 module_platform_driver(exynos5_dmc_platdrv
);
1548 MODULE_DESCRIPTION("Driver for Exynos5422 Dynamic Memory Controller dynamic frequency and voltage change");
1549 MODULE_LICENSE("GPL v2");
1550 MODULE_AUTHOR("Lukasz Luba");