xref: /OK3568_Linux_fs/kernel/drivers/memory/samsung/exynos5422-dmc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4*4882a593Smuzhiyun  * Author: Lukasz Luba <l.luba@partner.samsung.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/clk.h>
8*4882a593Smuzhiyun #include <linux/devfreq.h>
9*4882a593Smuzhiyun #include <linux/devfreq-event.h>
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/interrupt.h>
12*4882a593Smuzhiyun #include <linux/io.h>
13*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/moduleparam.h>
16*4882a593Smuzhiyun #include <linux/of_device.h>
17*4882a593Smuzhiyun #include <linux/pm_opp.h>
18*4882a593Smuzhiyun #include <linux/platform_device.h>
19*4882a593Smuzhiyun #include <linux/regmap.h>
20*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
21*4882a593Smuzhiyun #include <linux/slab.h>
22*4882a593Smuzhiyun #include "../jedec_ddr.h"
23*4882a593Smuzhiyun #include "../of_memory.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun static int irqmode;
26*4882a593Smuzhiyun module_param(irqmode, int, 0644);
27*4882a593Smuzhiyun MODULE_PARM_DESC(irqmode, "Enable IRQ mode (0=off [default], 1=on)");
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define EXYNOS5_DREXI_TIMINGAREF		(0x0030)
30*4882a593Smuzhiyun #define EXYNOS5_DREXI_TIMINGROW0		(0x0034)
31*4882a593Smuzhiyun #define EXYNOS5_DREXI_TIMINGDATA0		(0x0038)
32*4882a593Smuzhiyun #define EXYNOS5_DREXI_TIMINGPOWER0		(0x003C)
33*4882a593Smuzhiyun #define EXYNOS5_DREXI_TIMINGROW1		(0x00E4)
34*4882a593Smuzhiyun #define EXYNOS5_DREXI_TIMINGDATA1		(0x00E8)
35*4882a593Smuzhiyun #define EXYNOS5_DREXI_TIMINGPOWER1		(0x00EC)
36*4882a593Smuzhiyun #define CDREX_PAUSE				(0x2091c)
37*4882a593Smuzhiyun #define CDREX_LPDDR3PHY_CON3			(0x20a20)
38*4882a593Smuzhiyun #define CDREX_LPDDR3PHY_CLKM_SRC		(0x20700)
39*4882a593Smuzhiyun #define EXYNOS5_TIMING_SET_SWI			BIT(28)
40*4882a593Smuzhiyun #define USE_MX_MSPLL_TIMINGS			(1)
41*4882a593Smuzhiyun #define USE_BPLL_TIMINGS			(0)
42*4882a593Smuzhiyun #define EXYNOS5_AREF_NORMAL			(0x2e)
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #define DREX_PPCCLKCON		(0x0130)
45*4882a593Smuzhiyun #define DREX_PEREV2CONFIG	(0x013c)
46*4882a593Smuzhiyun #define DREX_PMNC_PPC		(0xE000)
47*4882a593Smuzhiyun #define DREX_CNTENS_PPC		(0xE010)
48*4882a593Smuzhiyun #define DREX_CNTENC_PPC		(0xE020)
49*4882a593Smuzhiyun #define DREX_INTENS_PPC		(0xE030)
50*4882a593Smuzhiyun #define DREX_INTENC_PPC		(0xE040)
51*4882a593Smuzhiyun #define DREX_FLAG_PPC		(0xE050)
52*4882a593Smuzhiyun #define DREX_PMCNT2_PPC		(0xE130)
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun  * A value for register DREX_PMNC_PPC which should be written to reset
56*4882a593Smuzhiyun  * the cycle counter CCNT (a reference wall clock). It sets zero to the
57*4882a593Smuzhiyun  * CCNT counter.
58*4882a593Smuzhiyun  */
59*4882a593Smuzhiyun #define CC_RESET		BIT(2)
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * A value for register DREX_PMNC_PPC which does the reset of all performance
63*4882a593Smuzhiyun  * counters to zero.
64*4882a593Smuzhiyun  */
65*4882a593Smuzhiyun #define PPC_COUNTER_RESET	BIT(1)
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun  * Enables all configured counters (including cycle counter). The value should
69*4882a593Smuzhiyun  * be written to the register DREX_PMNC_PPC.
70*4882a593Smuzhiyun  */
71*4882a593Smuzhiyun #define PPC_ENABLE		BIT(0)
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /* A value for register DREX_PPCCLKCON which enables performance events clock.
74*4882a593Smuzhiyun  * Must be written before first access to the performance counters register
75*4882a593Smuzhiyun  * set, otherwise it could crash.
76*4882a593Smuzhiyun  */
77*4882a593Smuzhiyun #define PEREV_CLK_EN		BIT(0)
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun  * Values which are used to enable counters, interrupts or configure flags of
81*4882a593Smuzhiyun  * the performance counters. They configure counter 2 and cycle counter.
82*4882a593Smuzhiyun  */
83*4882a593Smuzhiyun #define PERF_CNT2		BIT(2)
84*4882a593Smuzhiyun #define PERF_CCNT		BIT(31)
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun  * Performance event types which are used for setting the preferred event
88*4882a593Smuzhiyun  * to track in the counters.
89*4882a593Smuzhiyun  * There is a set of different types, the values are from range 0 to 0x6f.
90*4882a593Smuzhiyun  * These settings should be written to the configuration register which manages
91*4882a593Smuzhiyun  * the type of the event (register DREX_PEREV2CONFIG).
92*4882a593Smuzhiyun  */
93*4882a593Smuzhiyun #define READ_TRANSFER_CH0	(0x6d)
94*4882a593Smuzhiyun #define READ_TRANSFER_CH1	(0x6f)
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun #define PERF_COUNTER_START_VALUE 0xff000000
97*4882a593Smuzhiyun #define PERF_EVENT_UP_DOWN_THRESHOLD 900000000ULL
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun  * struct dmc_opp_table - Operating level desciption
101*4882a593Smuzhiyun  * @freq_hz:		target frequency in Hz
102*4882a593Smuzhiyun  * @volt_uv:		target voltage in uV
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * Covers frequency and voltage settings of the DMC operating mode.
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun struct dmc_opp_table {
107*4882a593Smuzhiyun 	u32 freq_hz;
108*4882a593Smuzhiyun 	u32 volt_uv;
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /**
112*4882a593Smuzhiyun  * struct exynos5_dmc - main structure describing DMC device
113*4882a593Smuzhiyun  * @dev:		DMC device
114*4882a593Smuzhiyun  * @df:			devfreq device structure returned by devfreq framework
115*4882a593Smuzhiyun  * @gov_data:		configuration of devfreq governor
116*4882a593Smuzhiyun  * @base_drexi0:	DREX0 registers mapping
117*4882a593Smuzhiyun  * @base_drexi1:	DREX1 registers mapping
118*4882a593Smuzhiyun  * @clk_regmap:		regmap for clock controller registers
119*4882a593Smuzhiyun  * @lock:		protects curr_rate and frequency/voltage setting section
120*4882a593Smuzhiyun  * @curr_rate:		current frequency
121*4882a593Smuzhiyun  * @curr_volt:		current voltage
122*4882a593Smuzhiyun  * @opp:		OPP table
123*4882a593Smuzhiyun  * @opp_count:		number of 'opp' elements
124*4882a593Smuzhiyun  * @timings_arr_size:	number of 'timings' elements
125*4882a593Smuzhiyun  * @timing_row:		values for timing row register, for each OPP
126*4882a593Smuzhiyun  * @timing_data:	values for timing data register, for each OPP
127*4882a593Smuzhiyun  * @timing_power:	balues for timing power register, for each OPP
128*4882a593Smuzhiyun  * @timings:		DDR memory timings, from device tree
129*4882a593Smuzhiyun  * @min_tck:		DDR memory minimum timing values, from device tree
130*4882a593Smuzhiyun  * @bypass_timing_row:	value for timing row register for bypass timings
131*4882a593Smuzhiyun  * @bypass_timing_data:	value for timing data register for bypass timings
132*4882a593Smuzhiyun  * @bypass_timing_power:	value for timing power register for bypass
133*4882a593Smuzhiyun  *				timings
134*4882a593Smuzhiyun  * @vdd_mif:		Memory interface regulator
135*4882a593Smuzhiyun  * @fout_spll:		clock: SPLL
136*4882a593Smuzhiyun  * @fout_bpll:		clock: BPLL
137*4882a593Smuzhiyun  * @mout_spll:		clock: mux SPLL
138*4882a593Smuzhiyun  * @mout_bpll:		clock: mux BPLL
139*4882a593Smuzhiyun  * @mout_mclk_cdrex:	clock: mux mclk_cdrex
140*4882a593Smuzhiyun  * @mout_mx_mspll_ccore:	clock: mux mx_mspll_ccore
141*4882a593Smuzhiyun  * @counter:		devfreq events
142*4882a593Smuzhiyun  * @num_counters:	number of 'counter' elements
143*4882a593Smuzhiyun  * @last_overflow_ts:	time (in ns) of last overflow of each DREX
144*4882a593Smuzhiyun  * @load:		utilization in percents
145*4882a593Smuzhiyun  * @total:		total time between devfreq events
146*4882a593Smuzhiyun  * @in_irq_mode:	whether running in interrupt mode (true)
147*4882a593Smuzhiyun  *			or polling (false)
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  * The main structure for the Dynamic Memory Controller which covers clocks,
150*4882a593Smuzhiyun  * memory regions, HW information, parameters and current operating mode.
151*4882a593Smuzhiyun  */
152*4882a593Smuzhiyun struct exynos5_dmc {
153*4882a593Smuzhiyun 	struct device *dev;
154*4882a593Smuzhiyun 	struct devfreq *df;
155*4882a593Smuzhiyun 	struct devfreq_simple_ondemand_data gov_data;
156*4882a593Smuzhiyun 	void __iomem *base_drexi0;
157*4882a593Smuzhiyun 	void __iomem *base_drexi1;
158*4882a593Smuzhiyun 	struct regmap *clk_regmap;
159*4882a593Smuzhiyun 	/* Protects curr_rate and frequency/voltage setting section */
160*4882a593Smuzhiyun 	struct mutex lock;
161*4882a593Smuzhiyun 	unsigned long curr_rate;
162*4882a593Smuzhiyun 	unsigned long curr_volt;
163*4882a593Smuzhiyun 	struct dmc_opp_table *opp;
164*4882a593Smuzhiyun 	int opp_count;
165*4882a593Smuzhiyun 	u32 timings_arr_size;
166*4882a593Smuzhiyun 	u32 *timing_row;
167*4882a593Smuzhiyun 	u32 *timing_data;
168*4882a593Smuzhiyun 	u32 *timing_power;
169*4882a593Smuzhiyun 	const struct lpddr3_timings *timings;
170*4882a593Smuzhiyun 	const struct lpddr3_min_tck *min_tck;
171*4882a593Smuzhiyun 	u32 bypass_timing_row;
172*4882a593Smuzhiyun 	u32 bypass_timing_data;
173*4882a593Smuzhiyun 	u32 bypass_timing_power;
174*4882a593Smuzhiyun 	struct regulator *vdd_mif;
175*4882a593Smuzhiyun 	struct clk *fout_spll;
176*4882a593Smuzhiyun 	struct clk *fout_bpll;
177*4882a593Smuzhiyun 	struct clk *mout_spll;
178*4882a593Smuzhiyun 	struct clk *mout_bpll;
179*4882a593Smuzhiyun 	struct clk *mout_mclk_cdrex;
180*4882a593Smuzhiyun 	struct clk *mout_mx_mspll_ccore;
181*4882a593Smuzhiyun 	struct devfreq_event_dev **counter;
182*4882a593Smuzhiyun 	int num_counters;
183*4882a593Smuzhiyun 	u64 last_overflow_ts[2];
184*4882a593Smuzhiyun 	unsigned long load;
185*4882a593Smuzhiyun 	unsigned long total;
186*4882a593Smuzhiyun 	bool in_irq_mode;
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun #define TIMING_FIELD(t_name, t_bit_beg, t_bit_end) \
190*4882a593Smuzhiyun 	{ .name = t_name, .bit_beg = t_bit_beg, .bit_end = t_bit_end }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun #define TIMING_VAL2REG(timing, t_val)			\
193*4882a593Smuzhiyun ({							\
194*4882a593Smuzhiyun 		u32 __val;				\
195*4882a593Smuzhiyun 		__val = (t_val) << (timing)->bit_beg;	\
196*4882a593Smuzhiyun 		__val;					\
197*4882a593Smuzhiyun })
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun struct timing_reg {
200*4882a593Smuzhiyun 	char *name;
201*4882a593Smuzhiyun 	int bit_beg;
202*4882a593Smuzhiyun 	int bit_end;
203*4882a593Smuzhiyun 	unsigned int val;
204*4882a593Smuzhiyun };
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun static const struct timing_reg timing_row_reg_fields[] = {
207*4882a593Smuzhiyun 	TIMING_FIELD("tRFC", 24, 31),
208*4882a593Smuzhiyun 	TIMING_FIELD("tRRD", 20, 23),
209*4882a593Smuzhiyun 	TIMING_FIELD("tRP", 16, 19),
210*4882a593Smuzhiyun 	TIMING_FIELD("tRCD", 12, 15),
211*4882a593Smuzhiyun 	TIMING_FIELD("tRC", 6, 11),
212*4882a593Smuzhiyun 	TIMING_FIELD("tRAS", 0, 5),
213*4882a593Smuzhiyun };
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun static const struct timing_reg timing_data_reg_fields[] = {
216*4882a593Smuzhiyun 	TIMING_FIELD("tWTR", 28, 31),
217*4882a593Smuzhiyun 	TIMING_FIELD("tWR", 24, 27),
218*4882a593Smuzhiyun 	TIMING_FIELD("tRTP", 20, 23),
219*4882a593Smuzhiyun 	TIMING_FIELD("tW2W-C2C", 14, 14),
220*4882a593Smuzhiyun 	TIMING_FIELD("tR2R-C2C", 12, 12),
221*4882a593Smuzhiyun 	TIMING_FIELD("WL", 8, 11),
222*4882a593Smuzhiyun 	TIMING_FIELD("tDQSCK", 4, 7),
223*4882a593Smuzhiyun 	TIMING_FIELD("RL", 0, 3),
224*4882a593Smuzhiyun };
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun static const struct timing_reg timing_power_reg_fields[] = {
227*4882a593Smuzhiyun 	TIMING_FIELD("tFAW", 26, 31),
228*4882a593Smuzhiyun 	TIMING_FIELD("tXSR", 16, 25),
229*4882a593Smuzhiyun 	TIMING_FIELD("tXP", 8, 15),
230*4882a593Smuzhiyun 	TIMING_FIELD("tCKE", 4, 7),
231*4882a593Smuzhiyun 	TIMING_FIELD("tMRD", 0, 3),
232*4882a593Smuzhiyun };
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun #define TIMING_COUNT (ARRAY_SIZE(timing_row_reg_fields) + \
235*4882a593Smuzhiyun 		      ARRAY_SIZE(timing_data_reg_fields) + \
236*4882a593Smuzhiyun 		      ARRAY_SIZE(timing_power_reg_fields))
237*4882a593Smuzhiyun 
exynos5_counters_set_event(struct exynos5_dmc * dmc)238*4882a593Smuzhiyun static int exynos5_counters_set_event(struct exynos5_dmc *dmc)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	int i, ret;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	for (i = 0; i < dmc->num_counters; i++) {
243*4882a593Smuzhiyun 		if (!dmc->counter[i])
244*4882a593Smuzhiyun 			continue;
245*4882a593Smuzhiyun 		ret = devfreq_event_set_event(dmc->counter[i]);
246*4882a593Smuzhiyun 		if (ret < 0)
247*4882a593Smuzhiyun 			return ret;
248*4882a593Smuzhiyun 	}
249*4882a593Smuzhiyun 	return 0;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
exynos5_counters_enable_edev(struct exynos5_dmc * dmc)252*4882a593Smuzhiyun static int exynos5_counters_enable_edev(struct exynos5_dmc *dmc)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	int i, ret;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	for (i = 0; i < dmc->num_counters; i++) {
257*4882a593Smuzhiyun 		if (!dmc->counter[i])
258*4882a593Smuzhiyun 			continue;
259*4882a593Smuzhiyun 		ret = devfreq_event_enable_edev(dmc->counter[i]);
260*4882a593Smuzhiyun 		if (ret < 0)
261*4882a593Smuzhiyun 			return ret;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 	return 0;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
exynos5_counters_disable_edev(struct exynos5_dmc * dmc)266*4882a593Smuzhiyun static int exynos5_counters_disable_edev(struct exynos5_dmc *dmc)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	int i, ret;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	for (i = 0; i < dmc->num_counters; i++) {
271*4882a593Smuzhiyun 		if (!dmc->counter[i])
272*4882a593Smuzhiyun 			continue;
273*4882a593Smuzhiyun 		ret = devfreq_event_disable_edev(dmc->counter[i]);
274*4882a593Smuzhiyun 		if (ret < 0)
275*4882a593Smuzhiyun 			return ret;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 	return 0;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /**
281*4882a593Smuzhiyun  * find_target_freq_id() - Finds requested frequency in local DMC configuration
282*4882a593Smuzhiyun  * @dmc:	device for which the information is checked
283*4882a593Smuzhiyun  * @target_rate:	requested frequency in KHz
284*4882a593Smuzhiyun  *
285*4882a593Smuzhiyun  * Seeks in the local DMC driver structure for the requested frequency value
286*4882a593Smuzhiyun  * and returns index or error value.
287*4882a593Smuzhiyun  */
find_target_freq_idx(struct exynos5_dmc * dmc,unsigned long target_rate)288*4882a593Smuzhiyun static int find_target_freq_idx(struct exynos5_dmc *dmc,
289*4882a593Smuzhiyun 				unsigned long target_rate)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	int i;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	for (i = dmc->opp_count - 1; i >= 0; i--)
294*4882a593Smuzhiyun 		if (dmc->opp[i].freq_hz <= target_rate)
295*4882a593Smuzhiyun 			return i;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	return -EINVAL;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun /**
301*4882a593Smuzhiyun  * exynos5_switch_timing_regs() - Changes bank register set for DRAM timings
302*4882a593Smuzhiyun  * @dmc:	device for which the new settings is going to be applied
303*4882a593Smuzhiyun  * @set:	boolean variable passing set value
304*4882a593Smuzhiyun  *
305*4882a593Smuzhiyun  * Changes the register set, which holds timing parameters.
306*4882a593Smuzhiyun  * There is two register sets: 0 and 1. The register set 0
307*4882a593Smuzhiyun  * is used in normal operation when the clock is provided from main PLL.
308*4882a593Smuzhiyun  * The bank register set 1 is used when the main PLL frequency is going to be
309*4882a593Smuzhiyun  * changed and the clock is taken from alternative, stable source.
310*4882a593Smuzhiyun  * This function switches between these banks according to the
311*4882a593Smuzhiyun  * currently used clock source.
312*4882a593Smuzhiyun  */
exynos5_switch_timing_regs(struct exynos5_dmc * dmc,bool set)313*4882a593Smuzhiyun static int exynos5_switch_timing_regs(struct exynos5_dmc *dmc, bool set)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	unsigned int reg;
316*4882a593Smuzhiyun 	int ret;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	ret = regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, &reg);
319*4882a593Smuzhiyun 	if (ret)
320*4882a593Smuzhiyun 		return ret;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (set)
323*4882a593Smuzhiyun 		reg |= EXYNOS5_TIMING_SET_SWI;
324*4882a593Smuzhiyun 	else
325*4882a593Smuzhiyun 		reg &= ~EXYNOS5_TIMING_SET_SWI;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, reg);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	return 0;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun /**
333*4882a593Smuzhiyun  * exynos5_init_freq_table() - Initialized PM OPP framework
334*4882a593Smuzhiyun  * @dmc:	DMC device for which the frequencies are used for OPP init
335*4882a593Smuzhiyun  * @profile:	devfreq device's profile
336*4882a593Smuzhiyun  *
337*4882a593Smuzhiyun  * Populate the devfreq device's OPP table based on current frequency, voltage.
338*4882a593Smuzhiyun  */
exynos5_init_freq_table(struct exynos5_dmc * dmc,struct devfreq_dev_profile * profile)339*4882a593Smuzhiyun static int exynos5_init_freq_table(struct exynos5_dmc *dmc,
340*4882a593Smuzhiyun 				   struct devfreq_dev_profile *profile)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	int i, ret;
343*4882a593Smuzhiyun 	int idx;
344*4882a593Smuzhiyun 	unsigned long freq;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	ret = dev_pm_opp_of_add_table(dmc->dev);
347*4882a593Smuzhiyun 	if (ret < 0) {
348*4882a593Smuzhiyun 		dev_err(dmc->dev, "Failed to get OPP table\n");
349*4882a593Smuzhiyun 		return ret;
350*4882a593Smuzhiyun 	}
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	dmc->opp_count = dev_pm_opp_get_opp_count(dmc->dev);
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	dmc->opp = devm_kmalloc_array(dmc->dev, dmc->opp_count,
355*4882a593Smuzhiyun 				      sizeof(struct dmc_opp_table), GFP_KERNEL);
356*4882a593Smuzhiyun 	if (!dmc->opp)
357*4882a593Smuzhiyun 		goto err_opp;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	idx = dmc->opp_count - 1;
360*4882a593Smuzhiyun 	for (i = 0, freq = ULONG_MAX; i < dmc->opp_count; i++, freq--) {
361*4882a593Smuzhiyun 		struct dev_pm_opp *opp;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 		opp = dev_pm_opp_find_freq_floor(dmc->dev, &freq);
364*4882a593Smuzhiyun 		if (IS_ERR(opp))
365*4882a593Smuzhiyun 			goto err_opp;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		dmc->opp[idx - i].freq_hz = freq;
368*4882a593Smuzhiyun 		dmc->opp[idx - i].volt_uv = dev_pm_opp_get_voltage(opp);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 		dev_pm_opp_put(opp);
371*4882a593Smuzhiyun 	}
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	return 0;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun err_opp:
376*4882a593Smuzhiyun 	dev_pm_opp_of_remove_table(dmc->dev);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	return -EINVAL;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun /**
382*4882a593Smuzhiyun  * exynos5_set_bypass_dram_timings() - Low-level changes of the DRAM timings
383*4882a593Smuzhiyun  * @dmc:	device for which the new settings is going to be applied
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * Low-level function for changing timings for DRAM memory clocking from
386*4882a593Smuzhiyun  * 'bypass' clock source (fixed frequency @400MHz).
387*4882a593Smuzhiyun  * It uses timing bank registers set 1.
388*4882a593Smuzhiyun  */
exynos5_set_bypass_dram_timings(struct exynos5_dmc * dmc)389*4882a593Smuzhiyun static void exynos5_set_bypass_dram_timings(struct exynos5_dmc *dmc)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	writel(EXYNOS5_AREF_NORMAL,
392*4882a593Smuzhiyun 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	writel(dmc->bypass_timing_row,
395*4882a593Smuzhiyun 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW1);
396*4882a593Smuzhiyun 	writel(dmc->bypass_timing_row,
397*4882a593Smuzhiyun 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW1);
398*4882a593Smuzhiyun 	writel(dmc->bypass_timing_data,
399*4882a593Smuzhiyun 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA1);
400*4882a593Smuzhiyun 	writel(dmc->bypass_timing_data,
401*4882a593Smuzhiyun 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA1);
402*4882a593Smuzhiyun 	writel(dmc->bypass_timing_power,
403*4882a593Smuzhiyun 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER1);
404*4882a593Smuzhiyun 	writel(dmc->bypass_timing_power,
405*4882a593Smuzhiyun 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER1);
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun /**
409*4882a593Smuzhiyun  * exynos5_dram_change_timings() - Low-level changes of the DRAM final timings
410*4882a593Smuzhiyun  * @dmc:	device for which the new settings is going to be applied
411*4882a593Smuzhiyun  * @target_rate:	target frequency of the DMC
412*4882a593Smuzhiyun  *
413*4882a593Smuzhiyun  * Low-level function for changing timings for DRAM memory operating from main
414*4882a593Smuzhiyun  * clock source (BPLL), which can have different frequencies. Thus, each
415*4882a593Smuzhiyun  * frequency must have corresponding timings register values in order to keep
416*4882a593Smuzhiyun  * the needed delays.
417*4882a593Smuzhiyun  * It uses timing bank registers set 0.
418*4882a593Smuzhiyun  */
exynos5_dram_change_timings(struct exynos5_dmc * dmc,unsigned long target_rate)419*4882a593Smuzhiyun static int exynos5_dram_change_timings(struct exynos5_dmc *dmc,
420*4882a593Smuzhiyun 				       unsigned long target_rate)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	int idx;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	for (idx = dmc->opp_count - 1; idx >= 0; idx--)
425*4882a593Smuzhiyun 		if (dmc->opp[idx].freq_hz <= target_rate)
426*4882a593Smuzhiyun 			break;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	if (idx < 0)
429*4882a593Smuzhiyun 		return -EINVAL;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	writel(EXYNOS5_AREF_NORMAL,
432*4882a593Smuzhiyun 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	writel(dmc->timing_row[idx],
435*4882a593Smuzhiyun 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW0);
436*4882a593Smuzhiyun 	writel(dmc->timing_row[idx],
437*4882a593Smuzhiyun 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW0);
438*4882a593Smuzhiyun 	writel(dmc->timing_data[idx],
439*4882a593Smuzhiyun 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA0);
440*4882a593Smuzhiyun 	writel(dmc->timing_data[idx],
441*4882a593Smuzhiyun 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA0);
442*4882a593Smuzhiyun 	writel(dmc->timing_power[idx],
443*4882a593Smuzhiyun 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER0);
444*4882a593Smuzhiyun 	writel(dmc->timing_power[idx],
445*4882a593Smuzhiyun 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER0);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	return 0;
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun /**
451*4882a593Smuzhiyun  * exynos5_dmc_align_target_voltage() - Sets the final voltage for the DMC
452*4882a593Smuzhiyun  * @dmc:	device for which it is going to be set
453*4882a593Smuzhiyun  * @target_volt:	new voltage which is chosen to be final
454*4882a593Smuzhiyun  *
455*4882a593Smuzhiyun  * Function tries to align voltage to the safe level for 'normal' mode.
456*4882a593Smuzhiyun  * It checks the need of higher voltage and changes the value. The target
457*4882a593Smuzhiyun  * voltage might be lower that currently set and still the system will be
458*4882a593Smuzhiyun  * stable.
459*4882a593Smuzhiyun  */
exynos5_dmc_align_target_voltage(struct exynos5_dmc * dmc,unsigned long target_volt)460*4882a593Smuzhiyun static int exynos5_dmc_align_target_voltage(struct exynos5_dmc *dmc,
461*4882a593Smuzhiyun 					    unsigned long target_volt)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun 	int ret = 0;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (dmc->curr_volt <= target_volt)
466*4882a593Smuzhiyun 		return 0;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	ret = regulator_set_voltage(dmc->vdd_mif, target_volt,
469*4882a593Smuzhiyun 				    target_volt);
470*4882a593Smuzhiyun 	if (!ret)
471*4882a593Smuzhiyun 		dmc->curr_volt = target_volt;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	return ret;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun /**
477*4882a593Smuzhiyun  * exynos5_dmc_align_bypass_voltage() - Sets the voltage for the DMC
478*4882a593Smuzhiyun  * @dmc:	device for which it is going to be set
479*4882a593Smuzhiyun  * @target_volt:	new voltage which is chosen to be final
480*4882a593Smuzhiyun  *
481*4882a593Smuzhiyun  * Function tries to align voltage to the safe level for the 'bypass' mode.
482*4882a593Smuzhiyun  * It checks the need of higher voltage and changes the value.
483*4882a593Smuzhiyun  * The target voltage must not be less than currently needed, because
484*4882a593Smuzhiyun  * for current frequency the device might become unstable.
485*4882a593Smuzhiyun  */
exynos5_dmc_align_bypass_voltage(struct exynos5_dmc * dmc,unsigned long target_volt)486*4882a593Smuzhiyun static int exynos5_dmc_align_bypass_voltage(struct exynos5_dmc *dmc,
487*4882a593Smuzhiyun 					    unsigned long target_volt)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	int ret = 0;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	if (dmc->curr_volt >= target_volt)
492*4882a593Smuzhiyun 		return 0;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	ret = regulator_set_voltage(dmc->vdd_mif, target_volt,
495*4882a593Smuzhiyun 				    target_volt);
496*4882a593Smuzhiyun 	if (!ret)
497*4882a593Smuzhiyun 		dmc->curr_volt = target_volt;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	return ret;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun /**
503*4882a593Smuzhiyun  * exynos5_dmc_align_bypass_dram_timings() - Chooses and sets DRAM timings
504*4882a593Smuzhiyun  * @dmc:	device for which it is going to be set
505*4882a593Smuzhiyun  * @target_rate:	new frequency which is chosen to be final
506*4882a593Smuzhiyun  *
507*4882a593Smuzhiyun  * Function changes the DRAM timings for the temporary 'bypass' mode.
508*4882a593Smuzhiyun  */
exynos5_dmc_align_bypass_dram_timings(struct exynos5_dmc * dmc,unsigned long target_rate)509*4882a593Smuzhiyun static int exynos5_dmc_align_bypass_dram_timings(struct exynos5_dmc *dmc,
510*4882a593Smuzhiyun 						 unsigned long target_rate)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun 	int idx = find_target_freq_idx(dmc, target_rate);
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	if (idx < 0)
515*4882a593Smuzhiyun 		return -EINVAL;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	exynos5_set_bypass_dram_timings(dmc);
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	return 0;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun /**
523*4882a593Smuzhiyun  * exynos5_dmc_switch_to_bypass_configuration() - Switching to temporary clock
524*4882a593Smuzhiyun  * @dmc:	DMC device for which the switching is going to happen
525*4882a593Smuzhiyun  * @target_rate:	new frequency which is going to be set as a final
526*4882a593Smuzhiyun  * @target_volt:	new voltage which is going to be set as a final
527*4882a593Smuzhiyun  *
528*4882a593Smuzhiyun  * Function configures DMC and clocks for operating in temporary 'bypass' mode.
529*4882a593Smuzhiyun  * This mode is used only temporary but if required, changes voltage and timings
530*4882a593Smuzhiyun  * for DRAM chips. It switches the main clock to stable clock source for the
531*4882a593Smuzhiyun  * period of the main PLL reconfiguration.
532*4882a593Smuzhiyun  */
533*4882a593Smuzhiyun static int
exynos5_dmc_switch_to_bypass_configuration(struct exynos5_dmc * dmc,unsigned long target_rate,unsigned long target_volt)534*4882a593Smuzhiyun exynos5_dmc_switch_to_bypass_configuration(struct exynos5_dmc *dmc,
535*4882a593Smuzhiyun 					   unsigned long target_rate,
536*4882a593Smuzhiyun 					   unsigned long target_volt)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun 	int ret;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	/*
541*4882a593Smuzhiyun 	 * Having higher voltage for a particular frequency does not harm
542*4882a593Smuzhiyun 	 * the chip. Use it for the temporary frequency change when one
543*4882a593Smuzhiyun 	 * voltage manipulation might be avoided.
544*4882a593Smuzhiyun 	 */
545*4882a593Smuzhiyun 	ret = exynos5_dmc_align_bypass_voltage(dmc, target_volt);
546*4882a593Smuzhiyun 	if (ret)
547*4882a593Smuzhiyun 		return ret;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	/*
550*4882a593Smuzhiyun 	 * Longer delays for DRAM does not cause crash, the opposite does.
551*4882a593Smuzhiyun 	 */
552*4882a593Smuzhiyun 	ret = exynos5_dmc_align_bypass_dram_timings(dmc, target_rate);
553*4882a593Smuzhiyun 	if (ret)
554*4882a593Smuzhiyun 		return ret;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	/*
557*4882a593Smuzhiyun 	 * Delays are long enough, so use them for the new coming clock.
558*4882a593Smuzhiyun 	 */
559*4882a593Smuzhiyun 	ret = exynos5_switch_timing_regs(dmc, USE_MX_MSPLL_TIMINGS);
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	return ret;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun /**
565*4882a593Smuzhiyun  * exynos5_dmc_change_freq_and_volt() - Changes voltage and frequency of the DMC
566*4882a593Smuzhiyun  * using safe procedure
567*4882a593Smuzhiyun  * @dmc:	device for which the frequency is going to be changed
568*4882a593Smuzhiyun  * @target_rate:	requested new frequency
569*4882a593Smuzhiyun  * @target_volt:	requested voltage which corresponds to the new frequency
570*4882a593Smuzhiyun  *
571*4882a593Smuzhiyun  * The DMC frequency change procedure requires a few steps.
572*4882a593Smuzhiyun  * The main requirement is to change the clock source in the clk mux
573*4882a593Smuzhiyun  * for the time of main clock PLL locking. The assumption is that the
574*4882a593Smuzhiyun  * alternative clock source set as parent is stable.
575*4882a593Smuzhiyun  * The second parent's clock frequency is fixed to 400MHz, it is named 'bypass'
576*4882a593Smuzhiyun  * clock. This requires alignment in DRAM timing parameters for the new
577*4882a593Smuzhiyun  * T-period. There is two bank sets for keeping DRAM
578*4882a593Smuzhiyun  * timings: set 0 and set 1. The set 0 is used when main clock source is
579*4882a593Smuzhiyun  * chosen. The 2nd set of regs is used for 'bypass' clock. Switching between
580*4882a593Smuzhiyun  * the two bank sets is part of the process.
581*4882a593Smuzhiyun  * The voltage must also be aligned to the minimum required level. There is
582*4882a593Smuzhiyun  * this intermediate step with switching to 'bypass' parent clock source.
583*4882a593Smuzhiyun  * if the old voltage is lower, it requires an increase of the voltage level.
584*4882a593Smuzhiyun  * The complexity of the voltage manipulation is hidden in low level function.
585*4882a593Smuzhiyun  * In this function there is last alignment of the voltage level at the end.
586*4882a593Smuzhiyun  */
587*4882a593Smuzhiyun static int
exynos5_dmc_change_freq_and_volt(struct exynos5_dmc * dmc,unsigned long target_rate,unsigned long target_volt)588*4882a593Smuzhiyun exynos5_dmc_change_freq_and_volt(struct exynos5_dmc *dmc,
589*4882a593Smuzhiyun 				 unsigned long target_rate,
590*4882a593Smuzhiyun 				 unsigned long target_volt)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun 	int ret;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	ret = exynos5_dmc_switch_to_bypass_configuration(dmc, target_rate,
595*4882a593Smuzhiyun 							 target_volt);
596*4882a593Smuzhiyun 	if (ret)
597*4882a593Smuzhiyun 		return ret;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	/*
600*4882a593Smuzhiyun 	 * Voltage is set at least to a level needed for this frequency,
601*4882a593Smuzhiyun 	 * so switching clock source is safe now.
602*4882a593Smuzhiyun 	 */
603*4882a593Smuzhiyun 	clk_prepare_enable(dmc->fout_spll);
604*4882a593Smuzhiyun 	clk_prepare_enable(dmc->mout_spll);
605*4882a593Smuzhiyun 	clk_prepare_enable(dmc->mout_mx_mspll_ccore);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_mx_mspll_ccore);
608*4882a593Smuzhiyun 	if (ret)
609*4882a593Smuzhiyun 		goto disable_clocks;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	/*
612*4882a593Smuzhiyun 	 * We are safe to increase the timings for current bypass frequency.
613*4882a593Smuzhiyun 	 * Thanks to this the settings will be ready for the upcoming clock
614*4882a593Smuzhiyun 	 * source change.
615*4882a593Smuzhiyun 	 */
616*4882a593Smuzhiyun 	exynos5_dram_change_timings(dmc, target_rate);
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	clk_set_rate(dmc->fout_bpll, target_rate);
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	ret = exynos5_switch_timing_regs(dmc, USE_BPLL_TIMINGS);
621*4882a593Smuzhiyun 	if (ret)
622*4882a593Smuzhiyun 		goto disable_clocks;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_bpll);
625*4882a593Smuzhiyun 	if (ret)
626*4882a593Smuzhiyun 		goto disable_clocks;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	/*
629*4882a593Smuzhiyun 	 * Make sure if the voltage is not from 'bypass' settings and align to
630*4882a593Smuzhiyun 	 * the right level for power efficiency.
631*4882a593Smuzhiyun 	 */
632*4882a593Smuzhiyun 	ret = exynos5_dmc_align_target_voltage(dmc, target_volt);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun disable_clocks:
635*4882a593Smuzhiyun 	clk_disable_unprepare(dmc->mout_mx_mspll_ccore);
636*4882a593Smuzhiyun 	clk_disable_unprepare(dmc->mout_spll);
637*4882a593Smuzhiyun 	clk_disable_unprepare(dmc->fout_spll);
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	return ret;
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun /**
643*4882a593Smuzhiyun  * exynos5_dmc_get_volt_freq() - Gets the frequency and voltage from the OPP
644*4882a593Smuzhiyun  * table.
645*4882a593Smuzhiyun  * @dmc:	device for which the frequency is going to be changed
646*4882a593Smuzhiyun  * @freq:       requested frequency in KHz
647*4882a593Smuzhiyun  * @target_rate:	returned frequency which is the same or lower than
648*4882a593Smuzhiyun  *			requested
649*4882a593Smuzhiyun  * @target_volt:	returned voltage which corresponds to the returned
650*4882a593Smuzhiyun  *			frequency
651*4882a593Smuzhiyun  * @flags:	devfreq flags provided for this frequency change request
652*4882a593Smuzhiyun  *
653*4882a593Smuzhiyun  * Function gets requested frequency and checks OPP framework for needed
654*4882a593Smuzhiyun  * frequency and voltage. It populates the values 'target_rate' and
655*4882a593Smuzhiyun  * 'target_volt' or returns error value when OPP framework fails.
656*4882a593Smuzhiyun  */
exynos5_dmc_get_volt_freq(struct exynos5_dmc * dmc,unsigned long * freq,unsigned long * target_rate,unsigned long * target_volt,u32 flags)657*4882a593Smuzhiyun static int exynos5_dmc_get_volt_freq(struct exynos5_dmc *dmc,
658*4882a593Smuzhiyun 				     unsigned long *freq,
659*4882a593Smuzhiyun 				     unsigned long *target_rate,
660*4882a593Smuzhiyun 				     unsigned long *target_volt, u32 flags)
661*4882a593Smuzhiyun {
662*4882a593Smuzhiyun 	struct dev_pm_opp *opp;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	opp = devfreq_recommended_opp(dmc->dev, freq, flags);
665*4882a593Smuzhiyun 	if (IS_ERR(opp))
666*4882a593Smuzhiyun 		return PTR_ERR(opp);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	*target_rate = dev_pm_opp_get_freq(opp);
669*4882a593Smuzhiyun 	*target_volt = dev_pm_opp_get_voltage(opp);
670*4882a593Smuzhiyun 	dev_pm_opp_put(opp);
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	return 0;
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun /**
676*4882a593Smuzhiyun  * exynos5_dmc_target() - Function responsible for changing frequency of DMC
677*4882a593Smuzhiyun  * @dev:	device for which the frequency is going to be changed
678*4882a593Smuzhiyun  * @freq:	requested frequency in KHz
679*4882a593Smuzhiyun  * @flags:	flags provided for this frequency change request
680*4882a593Smuzhiyun  *
681*4882a593Smuzhiyun  * An entry function provided to the devfreq framework which provides frequency
682*4882a593Smuzhiyun  * change of the DMC. The function gets the possible rate from OPP table based
683*4882a593Smuzhiyun  * on requested frequency. It calls the next function responsible for the
684*4882a593Smuzhiyun  * frequency and voltage change. In case of failure, does not set 'curr_rate'
685*4882a593Smuzhiyun  * and returns error value to the framework.
686*4882a593Smuzhiyun  */
exynos5_dmc_target(struct device * dev,unsigned long * freq,u32 flags)687*4882a593Smuzhiyun static int exynos5_dmc_target(struct device *dev, unsigned long *freq,
688*4882a593Smuzhiyun 			      u32 flags)
689*4882a593Smuzhiyun {
690*4882a593Smuzhiyun 	struct exynos5_dmc *dmc = dev_get_drvdata(dev);
691*4882a593Smuzhiyun 	unsigned long target_rate = 0;
692*4882a593Smuzhiyun 	unsigned long target_volt = 0;
693*4882a593Smuzhiyun 	int ret;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	ret = exynos5_dmc_get_volt_freq(dmc, freq, &target_rate, &target_volt,
696*4882a593Smuzhiyun 					flags);
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	if (ret)
699*4882a593Smuzhiyun 		return ret;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	if (target_rate == dmc->curr_rate)
702*4882a593Smuzhiyun 		return 0;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	mutex_lock(&dmc->lock);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	ret = exynos5_dmc_change_freq_and_volt(dmc, target_rate, target_volt);
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	if (ret) {
709*4882a593Smuzhiyun 		mutex_unlock(&dmc->lock);
710*4882a593Smuzhiyun 		return ret;
711*4882a593Smuzhiyun 	}
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	dmc->curr_rate = target_rate;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	mutex_unlock(&dmc->lock);
716*4882a593Smuzhiyun 	return 0;
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun /**
720*4882a593Smuzhiyun  * exynos5_counters_get() - Gets the performance counters values.
721*4882a593Smuzhiyun  * @dmc:	device for which the counters are going to be checked
722*4882a593Smuzhiyun  * @load_count:	variable which is populated with counter value
723*4882a593Smuzhiyun  * @total_count:	variable which is used as 'wall clock' reference
724*4882a593Smuzhiyun  *
725*4882a593Smuzhiyun  * Function which provides performance counters values. It sums up counters for
726*4882a593Smuzhiyun  * two DMC channels. The 'total_count' is used as a reference and max value.
727*4882a593Smuzhiyun  * The ratio 'load_count/total_count' shows the busy percentage [0%, 100%].
728*4882a593Smuzhiyun  */
exynos5_counters_get(struct exynos5_dmc * dmc,unsigned long * load_count,unsigned long * total_count)729*4882a593Smuzhiyun static int exynos5_counters_get(struct exynos5_dmc *dmc,
730*4882a593Smuzhiyun 				unsigned long *load_count,
731*4882a593Smuzhiyun 				unsigned long *total_count)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun 	unsigned long total = 0;
734*4882a593Smuzhiyun 	struct devfreq_event_data event;
735*4882a593Smuzhiyun 	int ret, i;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	*load_count = 0;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	/* Take into account only read+write counters, but stop all */
740*4882a593Smuzhiyun 	for (i = 0; i < dmc->num_counters; i++) {
741*4882a593Smuzhiyun 		if (!dmc->counter[i])
742*4882a593Smuzhiyun 			continue;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 		ret = devfreq_event_get_event(dmc->counter[i], &event);
745*4882a593Smuzhiyun 		if (ret < 0)
746*4882a593Smuzhiyun 			return ret;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 		*load_count += event.load_count;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 		if (total < event.total_count)
751*4882a593Smuzhiyun 			total = event.total_count;
752*4882a593Smuzhiyun 	}
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	*total_count = total;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	return 0;
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun /**
760*4882a593Smuzhiyun  * exynos5_dmc_start_perf_events() - Setup and start performance event counters
761*4882a593Smuzhiyun  * @dmc:	device for which the counters are going to be checked
762*4882a593Smuzhiyun  * @beg_value:	initial value for the counter
763*4882a593Smuzhiyun  *
764*4882a593Smuzhiyun  * Function which enables needed counters, interrupts and sets initial values
765*4882a593Smuzhiyun  * then starts the counters.
766*4882a593Smuzhiyun  */
exynos5_dmc_start_perf_events(struct exynos5_dmc * dmc,u32 beg_value)767*4882a593Smuzhiyun static void exynos5_dmc_start_perf_events(struct exynos5_dmc *dmc,
768*4882a593Smuzhiyun 					  u32 beg_value)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun 	/* Enable interrupts for counter 2 */
771*4882a593Smuzhiyun 	writel(PERF_CNT2, dmc->base_drexi0 + DREX_INTENS_PPC);
772*4882a593Smuzhiyun 	writel(PERF_CNT2, dmc->base_drexi1 + DREX_INTENS_PPC);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	/* Enable counter 2 and CCNT  */
775*4882a593Smuzhiyun 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_CNTENS_PPC);
776*4882a593Smuzhiyun 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_CNTENS_PPC);
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	/* Clear overflow flag for all counters */
779*4882a593Smuzhiyun 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_FLAG_PPC);
780*4882a593Smuzhiyun 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_FLAG_PPC);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	/* Reset all counters */
783*4882a593Smuzhiyun 	writel(CC_RESET | PPC_COUNTER_RESET, dmc->base_drexi0 + DREX_PMNC_PPC);
784*4882a593Smuzhiyun 	writel(CC_RESET | PPC_COUNTER_RESET, dmc->base_drexi1 + DREX_PMNC_PPC);
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	/*
787*4882a593Smuzhiyun 	 * Set start value for the counters, the number of samples that
788*4882a593Smuzhiyun 	 * will be gathered is calculated as: 0xffffffff - beg_value
789*4882a593Smuzhiyun 	 */
790*4882a593Smuzhiyun 	writel(beg_value, dmc->base_drexi0 + DREX_PMCNT2_PPC);
791*4882a593Smuzhiyun 	writel(beg_value, dmc->base_drexi1 + DREX_PMCNT2_PPC);
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	/* Start all counters */
794*4882a593Smuzhiyun 	writel(PPC_ENABLE, dmc->base_drexi0 + DREX_PMNC_PPC);
795*4882a593Smuzhiyun 	writel(PPC_ENABLE, dmc->base_drexi1 + DREX_PMNC_PPC);
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun /**
799*4882a593Smuzhiyun  * exynos5_dmc_perf_events_calc() - Calculate utilization
800*4882a593Smuzhiyun  * @dmc:	device for which the counters are going to be checked
801*4882a593Smuzhiyun  * @diff_ts:	time between last interrupt and current one
802*4882a593Smuzhiyun  *
803*4882a593Smuzhiyun  * Function which calculates needed utilization for the devfreq governor.
804*4882a593Smuzhiyun  * It prepares values for 'busy_time' and 'total_time' based on elapsed time
805*4882a593Smuzhiyun  * between interrupts, which approximates utilization.
806*4882a593Smuzhiyun  */
exynos5_dmc_perf_events_calc(struct exynos5_dmc * dmc,u64 diff_ts)807*4882a593Smuzhiyun static void exynos5_dmc_perf_events_calc(struct exynos5_dmc *dmc, u64 diff_ts)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun 	/*
810*4882a593Smuzhiyun 	 * This is a simple algorithm for managing traffic on DMC.
811*4882a593Smuzhiyun 	 * When there is almost no load the counters overflow every 4s,
812*4882a593Smuzhiyun 	 * no mater the DMC frequency.
813*4882a593Smuzhiyun 	 * The high load might be approximated using linear function.
814*4882a593Smuzhiyun 	 * Knowing that, simple calculation can provide 'busy_time' and
815*4882a593Smuzhiyun 	 * 'total_time' to the devfreq governor which picks up target
816*4882a593Smuzhiyun 	 * frequency.
817*4882a593Smuzhiyun 	 * We want a fast ramp up and slow decay in frequency change function.
818*4882a593Smuzhiyun 	 */
819*4882a593Smuzhiyun 	if (diff_ts < PERF_EVENT_UP_DOWN_THRESHOLD) {
820*4882a593Smuzhiyun 		/*
821*4882a593Smuzhiyun 		 * Set higher utilization for the simple_ondemand governor.
822*4882a593Smuzhiyun 		 * The governor should increase the frequency of the DMC.
823*4882a593Smuzhiyun 		 */
824*4882a593Smuzhiyun 		dmc->load = 70;
825*4882a593Smuzhiyun 		dmc->total = 100;
826*4882a593Smuzhiyun 	} else {
827*4882a593Smuzhiyun 		/*
828*4882a593Smuzhiyun 		 * Set low utilization for the simple_ondemand governor.
829*4882a593Smuzhiyun 		 * The governor should decrease the frequency of the DMC.
830*4882a593Smuzhiyun 		 */
831*4882a593Smuzhiyun 		dmc->load = 35;
832*4882a593Smuzhiyun 		dmc->total = 100;
833*4882a593Smuzhiyun 	}
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	dev_dbg(dmc->dev, "diff_ts=%llu\n", diff_ts);
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun /**
839*4882a593Smuzhiyun  * exynos5_dmc_perf_events_check() - Checks the status of the counters
840*4882a593Smuzhiyun  * @dmc:	device for which the counters are going to be checked
841*4882a593Smuzhiyun  *
842*4882a593Smuzhiyun  * Function which is called from threaded IRQ to check the counters state
843*4882a593Smuzhiyun  * and to call approximation for the needed utilization.
844*4882a593Smuzhiyun  */
exynos5_dmc_perf_events_check(struct exynos5_dmc * dmc)845*4882a593Smuzhiyun static void exynos5_dmc_perf_events_check(struct exynos5_dmc *dmc)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun 	u32 val;
848*4882a593Smuzhiyun 	u64 diff_ts, ts;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	ts = ktime_get_ns();
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	/* Stop all counters */
853*4882a593Smuzhiyun 	writel(0, dmc->base_drexi0 + DREX_PMNC_PPC);
854*4882a593Smuzhiyun 	writel(0, dmc->base_drexi1 + DREX_PMNC_PPC);
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	/* Check the source in interrupt flag registers (which channel) */
857*4882a593Smuzhiyun 	val = readl(dmc->base_drexi0 + DREX_FLAG_PPC);
858*4882a593Smuzhiyun 	if (val) {
859*4882a593Smuzhiyun 		diff_ts = ts - dmc->last_overflow_ts[0];
860*4882a593Smuzhiyun 		dmc->last_overflow_ts[0] = ts;
861*4882a593Smuzhiyun 		dev_dbg(dmc->dev, "drex0 0xE050 val= 0x%08x\n",  val);
862*4882a593Smuzhiyun 	} else {
863*4882a593Smuzhiyun 		val = readl(dmc->base_drexi1 + DREX_FLAG_PPC);
864*4882a593Smuzhiyun 		diff_ts = ts - dmc->last_overflow_ts[1];
865*4882a593Smuzhiyun 		dmc->last_overflow_ts[1] = ts;
866*4882a593Smuzhiyun 		dev_dbg(dmc->dev, "drex1 0xE050 val= 0x%08x\n",  val);
867*4882a593Smuzhiyun 	}
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	exynos5_dmc_perf_events_calc(dmc, diff_ts);
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	exynos5_dmc_start_perf_events(dmc, PERF_COUNTER_START_VALUE);
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun /**
875*4882a593Smuzhiyun  * exynos5_dmc_enable_perf_events() - Enable performance events
876*4882a593Smuzhiyun  * @dmc:	device for which the counters are going to be checked
877*4882a593Smuzhiyun  *
878*4882a593Smuzhiyun  * Function which is setup needed environment and enables counters.
879*4882a593Smuzhiyun  */
exynos5_dmc_enable_perf_events(struct exynos5_dmc * dmc)880*4882a593Smuzhiyun static void exynos5_dmc_enable_perf_events(struct exynos5_dmc *dmc)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun 	u64 ts;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	/* Enable Performance Event Clock */
885*4882a593Smuzhiyun 	writel(PEREV_CLK_EN, dmc->base_drexi0 + DREX_PPCCLKCON);
886*4882a593Smuzhiyun 	writel(PEREV_CLK_EN, dmc->base_drexi1 + DREX_PPCCLKCON);
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	/* Select read transfers as performance event2 */
889*4882a593Smuzhiyun 	writel(READ_TRANSFER_CH0, dmc->base_drexi0 + DREX_PEREV2CONFIG);
890*4882a593Smuzhiyun 	writel(READ_TRANSFER_CH1, dmc->base_drexi1 + DREX_PEREV2CONFIG);
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	ts = ktime_get_ns();
893*4882a593Smuzhiyun 	dmc->last_overflow_ts[0] = ts;
894*4882a593Smuzhiyun 	dmc->last_overflow_ts[1] = ts;
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	/* Devfreq shouldn't be faster than initialization, play safe though. */
897*4882a593Smuzhiyun 	dmc->load = 99;
898*4882a593Smuzhiyun 	dmc->total = 100;
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun /**
902*4882a593Smuzhiyun  * exynos5_dmc_disable_perf_events() - Disable performance events
903*4882a593Smuzhiyun  * @dmc:	device for which the counters are going to be checked
904*4882a593Smuzhiyun  *
905*4882a593Smuzhiyun  * Function which stops, disables performance event counters and interrupts.
906*4882a593Smuzhiyun  */
exynos5_dmc_disable_perf_events(struct exynos5_dmc * dmc)907*4882a593Smuzhiyun static void exynos5_dmc_disable_perf_events(struct exynos5_dmc *dmc)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun 	/* Stop all counters */
910*4882a593Smuzhiyun 	writel(0, dmc->base_drexi0 + DREX_PMNC_PPC);
911*4882a593Smuzhiyun 	writel(0, dmc->base_drexi1 + DREX_PMNC_PPC);
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	/* Disable interrupts for counter 2 */
914*4882a593Smuzhiyun 	writel(PERF_CNT2, dmc->base_drexi0 + DREX_INTENC_PPC);
915*4882a593Smuzhiyun 	writel(PERF_CNT2, dmc->base_drexi1 + DREX_INTENC_PPC);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	/* Disable counter 2 and CCNT  */
918*4882a593Smuzhiyun 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_CNTENC_PPC);
919*4882a593Smuzhiyun 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_CNTENC_PPC);
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	/* Clear overflow flag for all counters */
922*4882a593Smuzhiyun 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_FLAG_PPC);
923*4882a593Smuzhiyun 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_FLAG_PPC);
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun /**
927*4882a593Smuzhiyun  * exynos5_dmc_get_status() - Read current DMC performance statistics.
928*4882a593Smuzhiyun  * @dev:	device for which the statistics are requested
929*4882a593Smuzhiyun  * @stat:	structure which has statistic fields
930*4882a593Smuzhiyun  *
931*4882a593Smuzhiyun  * Function reads the DMC performance counters and calculates 'busy_time'
932*4882a593Smuzhiyun  * and 'total_time'. To protect from overflow, the values are shifted right
933*4882a593Smuzhiyun  * by 10. After read out the counters are setup to count again.
934*4882a593Smuzhiyun  */
exynos5_dmc_get_status(struct device * dev,struct devfreq_dev_status * stat)935*4882a593Smuzhiyun static int exynos5_dmc_get_status(struct device *dev,
936*4882a593Smuzhiyun 				  struct devfreq_dev_status *stat)
937*4882a593Smuzhiyun {
938*4882a593Smuzhiyun 	struct exynos5_dmc *dmc = dev_get_drvdata(dev);
939*4882a593Smuzhiyun 	unsigned long load, total;
940*4882a593Smuzhiyun 	int ret;
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	if (dmc->in_irq_mode) {
943*4882a593Smuzhiyun 		mutex_lock(&dmc->lock);
944*4882a593Smuzhiyun 		stat->current_frequency = dmc->curr_rate;
945*4882a593Smuzhiyun 		mutex_unlock(&dmc->lock);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 		stat->busy_time = dmc->load;
948*4882a593Smuzhiyun 		stat->total_time = dmc->total;
949*4882a593Smuzhiyun 	} else {
950*4882a593Smuzhiyun 		ret = exynos5_counters_get(dmc, &load, &total);
951*4882a593Smuzhiyun 		if (ret < 0)
952*4882a593Smuzhiyun 			return -EINVAL;
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 		/* To protect from overflow, divide by 1024 */
955*4882a593Smuzhiyun 		stat->busy_time = load >> 10;
956*4882a593Smuzhiyun 		stat->total_time = total >> 10;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 		ret = exynos5_counters_set_event(dmc);
959*4882a593Smuzhiyun 		if (ret < 0) {
960*4882a593Smuzhiyun 			dev_err(dev, "could not set event counter\n");
961*4882a593Smuzhiyun 			return ret;
962*4882a593Smuzhiyun 		}
963*4882a593Smuzhiyun 	}
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	return 0;
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun /**
969*4882a593Smuzhiyun  * exynos5_dmc_get_cur_freq() - Function returns current DMC frequency
970*4882a593Smuzhiyun  * @dev:	device for which the framework checks operating frequency
971*4882a593Smuzhiyun  * @freq:	returned frequency value
972*4882a593Smuzhiyun  *
973*4882a593Smuzhiyun  * It returns the currently used frequency of the DMC. The real operating
974*4882a593Smuzhiyun  * frequency might be lower when the clock source value could not be divided
975*4882a593Smuzhiyun  * to the requested value.
976*4882a593Smuzhiyun  */
exynos5_dmc_get_cur_freq(struct device * dev,unsigned long * freq)977*4882a593Smuzhiyun static int exynos5_dmc_get_cur_freq(struct device *dev, unsigned long *freq)
978*4882a593Smuzhiyun {
979*4882a593Smuzhiyun 	struct exynos5_dmc *dmc = dev_get_drvdata(dev);
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	mutex_lock(&dmc->lock);
982*4882a593Smuzhiyun 	*freq = dmc->curr_rate;
983*4882a593Smuzhiyun 	mutex_unlock(&dmc->lock);
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	return 0;
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun /*
989*4882a593Smuzhiyun  * exynos5_dmc_df_profile - Devfreq governor's profile structure
990*4882a593Smuzhiyun  *
991*4882a593Smuzhiyun  * It provides to the devfreq framework needed functions and polling period.
992*4882a593Smuzhiyun  */
993*4882a593Smuzhiyun static struct devfreq_dev_profile exynos5_dmc_df_profile = {
994*4882a593Smuzhiyun 	.timer = DEVFREQ_TIMER_DELAYED,
995*4882a593Smuzhiyun 	.target = exynos5_dmc_target,
996*4882a593Smuzhiyun 	.get_dev_status = exynos5_dmc_get_status,
997*4882a593Smuzhiyun 	.get_cur_freq = exynos5_dmc_get_cur_freq,
998*4882a593Smuzhiyun };
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun /**
1001*4882a593Smuzhiyun  * exynos5_dmc_align_initial_frequency() - Align initial frequency value
1002*4882a593Smuzhiyun  * @dmc:	device for which the frequency is going to be set
1003*4882a593Smuzhiyun  * @bootloader_init_freq:	initial frequency set by the bootloader in KHz
1004*4882a593Smuzhiyun  *
1005*4882a593Smuzhiyun  * The initial bootloader frequency, which is present during boot, might be
1006*4882a593Smuzhiyun  * different that supported frequency values in the driver. It is possible
1007*4882a593Smuzhiyun  * due to different PLL settings or used PLL as a source.
1008*4882a593Smuzhiyun  * This function provides the 'initial_freq' for the devfreq framework
1009*4882a593Smuzhiyun  * statistics engine which supports only registered values. Thus, some alignment
1010*4882a593Smuzhiyun  * must be made.
1011*4882a593Smuzhiyun  */
1012*4882a593Smuzhiyun static unsigned long
exynos5_dmc_align_init_freq(struct exynos5_dmc * dmc,unsigned long bootloader_init_freq)1013*4882a593Smuzhiyun exynos5_dmc_align_init_freq(struct exynos5_dmc *dmc,
1014*4882a593Smuzhiyun 			    unsigned long bootloader_init_freq)
1015*4882a593Smuzhiyun {
1016*4882a593Smuzhiyun 	unsigned long aligned_freq;
1017*4882a593Smuzhiyun 	int idx;
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	idx = find_target_freq_idx(dmc, bootloader_init_freq);
1020*4882a593Smuzhiyun 	if (idx >= 0)
1021*4882a593Smuzhiyun 		aligned_freq = dmc->opp[idx].freq_hz;
1022*4882a593Smuzhiyun 	else
1023*4882a593Smuzhiyun 		aligned_freq = dmc->opp[dmc->opp_count - 1].freq_hz;
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun 	return aligned_freq;
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun /**
1029*4882a593Smuzhiyun  * create_timings_aligned() - Create register values and align with standard
1030*4882a593Smuzhiyun  * @dmc:	device for which the frequency is going to be set
1031*4882a593Smuzhiyun  * @reg_timing_row:	array to fill with values for timing row register
1032*4882a593Smuzhiyun  * @reg_timing_data:	array to fill with values for timing data register
1033*4882a593Smuzhiyun  * @reg_timing_power:	array to fill with values for timing power register
1034*4882a593Smuzhiyun  * @clk_period_ps:	the period of the clock, known as tCK
1035*4882a593Smuzhiyun  *
1036*4882a593Smuzhiyun  * The function calculates timings and creates a register value ready for
1037*4882a593Smuzhiyun  * a frequency transition. The register contains a few timings. They are
1038*4882a593Smuzhiyun  * shifted by a known offset. The timing value is calculated based on memory
1039*4882a593Smuzhiyun  * specyfication: minimal time required and minimal cycles required.
1040*4882a593Smuzhiyun  */
create_timings_aligned(struct exynos5_dmc * dmc,u32 * reg_timing_row,u32 * reg_timing_data,u32 * reg_timing_power,u32 clk_period_ps)1041*4882a593Smuzhiyun static int create_timings_aligned(struct exynos5_dmc *dmc, u32 *reg_timing_row,
1042*4882a593Smuzhiyun 				  u32 *reg_timing_data, u32 *reg_timing_power,
1043*4882a593Smuzhiyun 				  u32 clk_period_ps)
1044*4882a593Smuzhiyun {
1045*4882a593Smuzhiyun 	u32 val;
1046*4882a593Smuzhiyun 	const struct timing_reg *reg;
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	if (clk_period_ps == 0)
1049*4882a593Smuzhiyun 		return -EINVAL;
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	*reg_timing_row = 0;
1052*4882a593Smuzhiyun 	*reg_timing_data = 0;
1053*4882a593Smuzhiyun 	*reg_timing_power = 0;
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	val = dmc->timings->tRFC / clk_period_ps;
1056*4882a593Smuzhiyun 	val += dmc->timings->tRFC % clk_period_ps ? 1 : 0;
1057*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tRFC);
1058*4882a593Smuzhiyun 	reg = &timing_row_reg_fields[0];
1059*4882a593Smuzhiyun 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	val = dmc->timings->tRRD / clk_period_ps;
1062*4882a593Smuzhiyun 	val += dmc->timings->tRRD % clk_period_ps ? 1 : 0;
1063*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tRRD);
1064*4882a593Smuzhiyun 	reg = &timing_row_reg_fields[1];
1065*4882a593Smuzhiyun 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	val = dmc->timings->tRPab / clk_period_ps;
1068*4882a593Smuzhiyun 	val += dmc->timings->tRPab % clk_period_ps ? 1 : 0;
1069*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tRPab);
1070*4882a593Smuzhiyun 	reg = &timing_row_reg_fields[2];
1071*4882a593Smuzhiyun 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	val = dmc->timings->tRCD / clk_period_ps;
1074*4882a593Smuzhiyun 	val += dmc->timings->tRCD % clk_period_ps ? 1 : 0;
1075*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tRCD);
1076*4882a593Smuzhiyun 	reg = &timing_row_reg_fields[3];
1077*4882a593Smuzhiyun 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	val = dmc->timings->tRC / clk_period_ps;
1080*4882a593Smuzhiyun 	val += dmc->timings->tRC % clk_period_ps ? 1 : 0;
1081*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tRC);
1082*4882a593Smuzhiyun 	reg = &timing_row_reg_fields[4];
1083*4882a593Smuzhiyun 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	val = dmc->timings->tRAS / clk_period_ps;
1086*4882a593Smuzhiyun 	val += dmc->timings->tRAS % clk_period_ps ? 1 : 0;
1087*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tRAS);
1088*4882a593Smuzhiyun 	reg = &timing_row_reg_fields[5];
1089*4882a593Smuzhiyun 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	/* data related timings */
1092*4882a593Smuzhiyun 	val = dmc->timings->tWTR / clk_period_ps;
1093*4882a593Smuzhiyun 	val += dmc->timings->tWTR % clk_period_ps ? 1 : 0;
1094*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tWTR);
1095*4882a593Smuzhiyun 	reg = &timing_data_reg_fields[0];
1096*4882a593Smuzhiyun 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	val = dmc->timings->tWR / clk_period_ps;
1099*4882a593Smuzhiyun 	val += dmc->timings->tWR % clk_period_ps ? 1 : 0;
1100*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tWR);
1101*4882a593Smuzhiyun 	reg = &timing_data_reg_fields[1];
1102*4882a593Smuzhiyun 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	val = dmc->timings->tRTP / clk_period_ps;
1105*4882a593Smuzhiyun 	val += dmc->timings->tRTP % clk_period_ps ? 1 : 0;
1106*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tRTP);
1107*4882a593Smuzhiyun 	reg = &timing_data_reg_fields[2];
1108*4882a593Smuzhiyun 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	val = dmc->timings->tW2W_C2C / clk_period_ps;
1111*4882a593Smuzhiyun 	val += dmc->timings->tW2W_C2C % clk_period_ps ? 1 : 0;
1112*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tW2W_C2C);
1113*4882a593Smuzhiyun 	reg = &timing_data_reg_fields[3];
1114*4882a593Smuzhiyun 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	val = dmc->timings->tR2R_C2C / clk_period_ps;
1117*4882a593Smuzhiyun 	val += dmc->timings->tR2R_C2C % clk_period_ps ? 1 : 0;
1118*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tR2R_C2C);
1119*4882a593Smuzhiyun 	reg = &timing_data_reg_fields[4];
1120*4882a593Smuzhiyun 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	val = dmc->timings->tWL / clk_period_ps;
1123*4882a593Smuzhiyun 	val += dmc->timings->tWL % clk_period_ps ? 1 : 0;
1124*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tWL);
1125*4882a593Smuzhiyun 	reg = &timing_data_reg_fields[5];
1126*4882a593Smuzhiyun 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	val = dmc->timings->tDQSCK / clk_period_ps;
1129*4882a593Smuzhiyun 	val += dmc->timings->tDQSCK % clk_period_ps ? 1 : 0;
1130*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tDQSCK);
1131*4882a593Smuzhiyun 	reg = &timing_data_reg_fields[6];
1132*4882a593Smuzhiyun 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	val = dmc->timings->tRL / clk_period_ps;
1135*4882a593Smuzhiyun 	val += dmc->timings->tRL % clk_period_ps ? 1 : 0;
1136*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tRL);
1137*4882a593Smuzhiyun 	reg = &timing_data_reg_fields[7];
1138*4882a593Smuzhiyun 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	/* power related timings */
1141*4882a593Smuzhiyun 	val = dmc->timings->tFAW / clk_period_ps;
1142*4882a593Smuzhiyun 	val += dmc->timings->tFAW % clk_period_ps ? 1 : 0;
1143*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tFAW);
1144*4882a593Smuzhiyun 	reg = &timing_power_reg_fields[0];
1145*4882a593Smuzhiyun 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 	val = dmc->timings->tXSR / clk_period_ps;
1148*4882a593Smuzhiyun 	val += dmc->timings->tXSR % clk_period_ps ? 1 : 0;
1149*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tXSR);
1150*4882a593Smuzhiyun 	reg = &timing_power_reg_fields[1];
1151*4882a593Smuzhiyun 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	val = dmc->timings->tXP / clk_period_ps;
1154*4882a593Smuzhiyun 	val += dmc->timings->tXP % clk_period_ps ? 1 : 0;
1155*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tXP);
1156*4882a593Smuzhiyun 	reg = &timing_power_reg_fields[2];
1157*4882a593Smuzhiyun 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	val = dmc->timings->tCKE / clk_period_ps;
1160*4882a593Smuzhiyun 	val += dmc->timings->tCKE % clk_period_ps ? 1 : 0;
1161*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tCKE);
1162*4882a593Smuzhiyun 	reg = &timing_power_reg_fields[3];
1163*4882a593Smuzhiyun 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	val = dmc->timings->tMRD / clk_period_ps;
1166*4882a593Smuzhiyun 	val += dmc->timings->tMRD % clk_period_ps ? 1 : 0;
1167*4882a593Smuzhiyun 	val = max(val, dmc->min_tck->tMRD);
1168*4882a593Smuzhiyun 	reg = &timing_power_reg_fields[4];
1169*4882a593Smuzhiyun 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	return 0;
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun /**
1175*4882a593Smuzhiyun  * of_get_dram_timings() - helper function for parsing DT settings for DRAM
1176*4882a593Smuzhiyun  * @dmc:        device for which the frequency is going to be set
1177*4882a593Smuzhiyun  *
1178*4882a593Smuzhiyun  * The function parses DT entries with DRAM information.
1179*4882a593Smuzhiyun  */
of_get_dram_timings(struct exynos5_dmc * dmc)1180*4882a593Smuzhiyun static int of_get_dram_timings(struct exynos5_dmc *dmc)
1181*4882a593Smuzhiyun {
1182*4882a593Smuzhiyun 	int ret = 0;
1183*4882a593Smuzhiyun 	int idx;
1184*4882a593Smuzhiyun 	struct device_node *np_ddr;
1185*4882a593Smuzhiyun 	u32 freq_mhz, clk_period_ps;
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun 	np_ddr = of_parse_phandle(dmc->dev->of_node, "device-handle", 0);
1188*4882a593Smuzhiyun 	if (!np_ddr) {
1189*4882a593Smuzhiyun 		dev_warn(dmc->dev, "could not find 'device-handle' in DT\n");
1190*4882a593Smuzhiyun 		return -EINVAL;
1191*4882a593Smuzhiyun 	}
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 	dmc->timing_row = devm_kmalloc_array(dmc->dev, TIMING_COUNT,
1194*4882a593Smuzhiyun 					     sizeof(u32), GFP_KERNEL);
1195*4882a593Smuzhiyun 	if (!dmc->timing_row) {
1196*4882a593Smuzhiyun 		ret = -ENOMEM;
1197*4882a593Smuzhiyun 		goto put_node;
1198*4882a593Smuzhiyun 	}
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 	dmc->timing_data = devm_kmalloc_array(dmc->dev, TIMING_COUNT,
1201*4882a593Smuzhiyun 					      sizeof(u32), GFP_KERNEL);
1202*4882a593Smuzhiyun 	if (!dmc->timing_data) {
1203*4882a593Smuzhiyun 		ret = -ENOMEM;
1204*4882a593Smuzhiyun 		goto put_node;
1205*4882a593Smuzhiyun 	}
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	dmc->timing_power = devm_kmalloc_array(dmc->dev, TIMING_COUNT,
1208*4882a593Smuzhiyun 					       sizeof(u32), GFP_KERNEL);
1209*4882a593Smuzhiyun 	if (!dmc->timing_power) {
1210*4882a593Smuzhiyun 		ret = -ENOMEM;
1211*4882a593Smuzhiyun 		goto put_node;
1212*4882a593Smuzhiyun 	}
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	dmc->timings = of_lpddr3_get_ddr_timings(np_ddr, dmc->dev,
1215*4882a593Smuzhiyun 						 DDR_TYPE_LPDDR3,
1216*4882a593Smuzhiyun 						 &dmc->timings_arr_size);
1217*4882a593Smuzhiyun 	if (!dmc->timings) {
1218*4882a593Smuzhiyun 		dev_warn(dmc->dev, "could not get timings from DT\n");
1219*4882a593Smuzhiyun 		ret = -EINVAL;
1220*4882a593Smuzhiyun 		goto put_node;
1221*4882a593Smuzhiyun 	}
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 	dmc->min_tck = of_lpddr3_get_min_tck(np_ddr, dmc->dev);
1224*4882a593Smuzhiyun 	if (!dmc->min_tck) {
1225*4882a593Smuzhiyun 		dev_warn(dmc->dev, "could not get tck from DT\n");
1226*4882a593Smuzhiyun 		ret = -EINVAL;
1227*4882a593Smuzhiyun 		goto put_node;
1228*4882a593Smuzhiyun 	}
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	/* Sorted array of OPPs with frequency ascending */
1231*4882a593Smuzhiyun 	for (idx = 0; idx < dmc->opp_count; idx++) {
1232*4882a593Smuzhiyun 		freq_mhz = dmc->opp[idx].freq_hz / 1000000;
1233*4882a593Smuzhiyun 		clk_period_ps = 1000000 / freq_mhz;
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun 		ret = create_timings_aligned(dmc, &dmc->timing_row[idx],
1236*4882a593Smuzhiyun 					     &dmc->timing_data[idx],
1237*4882a593Smuzhiyun 					     &dmc->timing_power[idx],
1238*4882a593Smuzhiyun 					     clk_period_ps);
1239*4882a593Smuzhiyun 	}
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 	/* Take the highest frequency's timings as 'bypass' */
1243*4882a593Smuzhiyun 	dmc->bypass_timing_row = dmc->timing_row[idx - 1];
1244*4882a593Smuzhiyun 	dmc->bypass_timing_data = dmc->timing_data[idx - 1];
1245*4882a593Smuzhiyun 	dmc->bypass_timing_power = dmc->timing_power[idx - 1];
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun put_node:
1248*4882a593Smuzhiyun 	of_node_put(np_ddr);
1249*4882a593Smuzhiyun 	return ret;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun /**
1253*4882a593Smuzhiyun  * exynos5_dmc_init_clks() - Initialize clocks needed for DMC operation.
1254*4882a593Smuzhiyun  * @dmc:	DMC structure containing needed fields
1255*4882a593Smuzhiyun  *
1256*4882a593Smuzhiyun  * Get the needed clocks defined in DT device, enable and set the right parents.
1257*4882a593Smuzhiyun  * Read current frequency and initialize the initial rate for governor.
1258*4882a593Smuzhiyun  */
exynos5_dmc_init_clks(struct exynos5_dmc * dmc)1259*4882a593Smuzhiyun static int exynos5_dmc_init_clks(struct exynos5_dmc *dmc)
1260*4882a593Smuzhiyun {
1261*4882a593Smuzhiyun 	int ret;
1262*4882a593Smuzhiyun 	unsigned long target_volt = 0;
1263*4882a593Smuzhiyun 	unsigned long target_rate = 0;
1264*4882a593Smuzhiyun 	unsigned int tmp;
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	dmc->fout_spll = devm_clk_get(dmc->dev, "fout_spll");
1267*4882a593Smuzhiyun 	if (IS_ERR(dmc->fout_spll))
1268*4882a593Smuzhiyun 		return PTR_ERR(dmc->fout_spll);
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	dmc->fout_bpll = devm_clk_get(dmc->dev, "fout_bpll");
1271*4882a593Smuzhiyun 	if (IS_ERR(dmc->fout_bpll))
1272*4882a593Smuzhiyun 		return PTR_ERR(dmc->fout_bpll);
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	dmc->mout_mclk_cdrex = devm_clk_get(dmc->dev, "mout_mclk_cdrex");
1275*4882a593Smuzhiyun 	if (IS_ERR(dmc->mout_mclk_cdrex))
1276*4882a593Smuzhiyun 		return PTR_ERR(dmc->mout_mclk_cdrex);
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	dmc->mout_bpll = devm_clk_get(dmc->dev, "mout_bpll");
1279*4882a593Smuzhiyun 	if (IS_ERR(dmc->mout_bpll))
1280*4882a593Smuzhiyun 		return PTR_ERR(dmc->mout_bpll);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	dmc->mout_mx_mspll_ccore = devm_clk_get(dmc->dev,
1283*4882a593Smuzhiyun 						"mout_mx_mspll_ccore");
1284*4882a593Smuzhiyun 	if (IS_ERR(dmc->mout_mx_mspll_ccore))
1285*4882a593Smuzhiyun 		return PTR_ERR(dmc->mout_mx_mspll_ccore);
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	dmc->mout_spll = devm_clk_get(dmc->dev, "ff_dout_spll2");
1288*4882a593Smuzhiyun 	if (IS_ERR(dmc->mout_spll)) {
1289*4882a593Smuzhiyun 		dmc->mout_spll = devm_clk_get(dmc->dev, "mout_sclk_spll");
1290*4882a593Smuzhiyun 		if (IS_ERR(dmc->mout_spll))
1291*4882a593Smuzhiyun 			return PTR_ERR(dmc->mout_spll);
1292*4882a593Smuzhiyun 	}
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	/*
1295*4882a593Smuzhiyun 	 * Convert frequency to KHz values and set it for the governor.
1296*4882a593Smuzhiyun 	 */
1297*4882a593Smuzhiyun 	dmc->curr_rate = clk_get_rate(dmc->mout_mclk_cdrex);
1298*4882a593Smuzhiyun 	dmc->curr_rate = exynos5_dmc_align_init_freq(dmc, dmc->curr_rate);
1299*4882a593Smuzhiyun 	exynos5_dmc_df_profile.initial_freq = dmc->curr_rate;
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 	ret = exynos5_dmc_get_volt_freq(dmc, &dmc->curr_rate, &target_rate,
1302*4882a593Smuzhiyun 					&target_volt, 0);
1303*4882a593Smuzhiyun 	if (ret)
1304*4882a593Smuzhiyun 		return ret;
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	dmc->curr_volt = target_volt;
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	ret = clk_set_parent(dmc->mout_mx_mspll_ccore, dmc->mout_spll);
1309*4882a593Smuzhiyun 	if (ret)
1310*4882a593Smuzhiyun 		return ret;
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	clk_prepare_enable(dmc->fout_bpll);
1313*4882a593Smuzhiyun 	clk_prepare_enable(dmc->mout_bpll);
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	/*
1316*4882a593Smuzhiyun 	 * Some bootloaders do not set clock routes correctly.
1317*4882a593Smuzhiyun 	 * Stop one path in clocks to PHY.
1318*4882a593Smuzhiyun 	 */
1319*4882a593Smuzhiyun 	regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, &tmp);
1320*4882a593Smuzhiyun 	tmp &= ~(BIT(1) | BIT(0));
1321*4882a593Smuzhiyun 	regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, tmp);
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	return 0;
1324*4882a593Smuzhiyun }
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun /**
1327*4882a593Smuzhiyun  * exynos5_performance_counters_init() - Initializes performance DMC's counters
1328*4882a593Smuzhiyun  * @dmc:	DMC for which it does the setup
1329*4882a593Smuzhiyun  *
1330*4882a593Smuzhiyun  * Initialization of performance counters in DMC for estimating usage.
1331*4882a593Smuzhiyun  * The counter's values are used for calculation of a memory bandwidth and based
1332*4882a593Smuzhiyun  * on that the governor changes the frequency.
1333*4882a593Smuzhiyun  * The counters are not used when the governor is GOVERNOR_USERSPACE.
1334*4882a593Smuzhiyun  */
exynos5_performance_counters_init(struct exynos5_dmc * dmc)1335*4882a593Smuzhiyun static int exynos5_performance_counters_init(struct exynos5_dmc *dmc)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun 	int ret, i;
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	dmc->num_counters = devfreq_event_get_edev_count(dmc->dev,
1340*4882a593Smuzhiyun 							"devfreq-events");
1341*4882a593Smuzhiyun 	if (dmc->num_counters < 0) {
1342*4882a593Smuzhiyun 		dev_err(dmc->dev, "could not get devfreq-event counters\n");
1343*4882a593Smuzhiyun 		return dmc->num_counters;
1344*4882a593Smuzhiyun 	}
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	dmc->counter = devm_kcalloc(dmc->dev, dmc->num_counters,
1347*4882a593Smuzhiyun 				    sizeof(*dmc->counter), GFP_KERNEL);
1348*4882a593Smuzhiyun 	if (!dmc->counter)
1349*4882a593Smuzhiyun 		return -ENOMEM;
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	for (i = 0; i < dmc->num_counters; i++) {
1352*4882a593Smuzhiyun 		dmc->counter[i] =
1353*4882a593Smuzhiyun 			devfreq_event_get_edev_by_phandle(dmc->dev,
1354*4882a593Smuzhiyun 						"devfreq-events", i);
1355*4882a593Smuzhiyun 		if (IS_ERR_OR_NULL(dmc->counter[i]))
1356*4882a593Smuzhiyun 			return -EPROBE_DEFER;
1357*4882a593Smuzhiyun 	}
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	ret = exynos5_counters_enable_edev(dmc);
1360*4882a593Smuzhiyun 	if (ret < 0) {
1361*4882a593Smuzhiyun 		dev_err(dmc->dev, "could not enable event counter\n");
1362*4882a593Smuzhiyun 		return ret;
1363*4882a593Smuzhiyun 	}
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	ret = exynos5_counters_set_event(dmc);
1366*4882a593Smuzhiyun 	if (ret < 0) {
1367*4882a593Smuzhiyun 		exynos5_counters_disable_edev(dmc);
1368*4882a593Smuzhiyun 		dev_err(dmc->dev, "could not set event counter\n");
1369*4882a593Smuzhiyun 		return ret;
1370*4882a593Smuzhiyun 	}
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	return 0;
1373*4882a593Smuzhiyun }
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun /**
1376*4882a593Smuzhiyun  * exynos5_dmc_set_pause_on_switching() - Controls a pause feature in DMC
1377*4882a593Smuzhiyun  * @dmc:	device which is used for changing this feature
1378*4882a593Smuzhiyun  *
1379*4882a593Smuzhiyun  * There is a need of pausing DREX DMC when divider or MUX in clock tree
1380*4882a593Smuzhiyun  * changes its configuration. In such situation access to the memory is blocked
1381*4882a593Smuzhiyun  * in DMC automatically. This feature is used when clock frequency change
1382*4882a593Smuzhiyun  * request appears and touches clock tree.
1383*4882a593Smuzhiyun  */
exynos5_dmc_set_pause_on_switching(struct exynos5_dmc * dmc)1384*4882a593Smuzhiyun static inline int exynos5_dmc_set_pause_on_switching(struct exynos5_dmc *dmc)
1385*4882a593Smuzhiyun {
1386*4882a593Smuzhiyun 	unsigned int val;
1387*4882a593Smuzhiyun 	int ret;
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	ret = regmap_read(dmc->clk_regmap, CDREX_PAUSE, &val);
1390*4882a593Smuzhiyun 	if (ret)
1391*4882a593Smuzhiyun 		return ret;
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 	val |= 1UL;
1394*4882a593Smuzhiyun 	regmap_write(dmc->clk_regmap, CDREX_PAUSE, val);
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	return 0;
1397*4882a593Smuzhiyun }
1398*4882a593Smuzhiyun 
dmc_irq_thread(int irq,void * priv)1399*4882a593Smuzhiyun static irqreturn_t dmc_irq_thread(int irq, void *priv)
1400*4882a593Smuzhiyun {
1401*4882a593Smuzhiyun 	int res;
1402*4882a593Smuzhiyun 	struct exynos5_dmc *dmc = priv;
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	mutex_lock(&dmc->df->lock);
1405*4882a593Smuzhiyun 	exynos5_dmc_perf_events_check(dmc);
1406*4882a593Smuzhiyun 	res = update_devfreq(dmc->df);
1407*4882a593Smuzhiyun 	mutex_unlock(&dmc->df->lock);
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	if (res)
1410*4882a593Smuzhiyun 		dev_warn(dmc->dev, "devfreq failed with %d\n", res);
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 	return IRQ_HANDLED;
1413*4882a593Smuzhiyun }
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun /**
1416*4882a593Smuzhiyun  * exynos5_dmc_probe() - Probe function for the DMC driver
1417*4882a593Smuzhiyun  * @pdev:	platform device for which the driver is going to be initialized
1418*4882a593Smuzhiyun  *
1419*4882a593Smuzhiyun  * Initialize basic components: clocks, regulators, performance counters, etc.
1420*4882a593Smuzhiyun  * Read out product version and based on the information setup
1421*4882a593Smuzhiyun  * internal structures for the controller (frequency and voltage) and for DRAM
1422*4882a593Smuzhiyun  * memory parameters: timings for each operating frequency.
1423*4882a593Smuzhiyun  * Register new devfreq device for controlling DVFS of the DMC.
1424*4882a593Smuzhiyun  */
exynos5_dmc_probe(struct platform_device * pdev)1425*4882a593Smuzhiyun static int exynos5_dmc_probe(struct platform_device *pdev)
1426*4882a593Smuzhiyun {
1427*4882a593Smuzhiyun 	int ret = 0;
1428*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
1429*4882a593Smuzhiyun 	struct device_node *np = dev->of_node;
1430*4882a593Smuzhiyun 	struct exynos5_dmc *dmc;
1431*4882a593Smuzhiyun 	int irq[2];
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	dmc = devm_kzalloc(dev, sizeof(*dmc), GFP_KERNEL);
1434*4882a593Smuzhiyun 	if (!dmc)
1435*4882a593Smuzhiyun 		return -ENOMEM;
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 	mutex_init(&dmc->lock);
1438*4882a593Smuzhiyun 
1439*4882a593Smuzhiyun 	dmc->dev = dev;
1440*4882a593Smuzhiyun 	platform_set_drvdata(pdev, dmc);
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun 	dmc->base_drexi0 = devm_platform_ioremap_resource(pdev, 0);
1443*4882a593Smuzhiyun 	if (IS_ERR(dmc->base_drexi0))
1444*4882a593Smuzhiyun 		return PTR_ERR(dmc->base_drexi0);
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun 	dmc->base_drexi1 = devm_platform_ioremap_resource(pdev, 1);
1447*4882a593Smuzhiyun 	if (IS_ERR(dmc->base_drexi1))
1448*4882a593Smuzhiyun 		return PTR_ERR(dmc->base_drexi1);
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	dmc->clk_regmap = syscon_regmap_lookup_by_phandle(np,
1451*4882a593Smuzhiyun 							  "samsung,syscon-clk");
1452*4882a593Smuzhiyun 	if (IS_ERR(dmc->clk_regmap))
1453*4882a593Smuzhiyun 		return PTR_ERR(dmc->clk_regmap);
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 	ret = exynos5_init_freq_table(dmc, &exynos5_dmc_df_profile);
1456*4882a593Smuzhiyun 	if (ret) {
1457*4882a593Smuzhiyun 		dev_warn(dev, "couldn't initialize frequency settings\n");
1458*4882a593Smuzhiyun 		return ret;
1459*4882a593Smuzhiyun 	}
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 	dmc->vdd_mif = devm_regulator_get(dev, "vdd");
1462*4882a593Smuzhiyun 	if (IS_ERR(dmc->vdd_mif)) {
1463*4882a593Smuzhiyun 		ret = PTR_ERR(dmc->vdd_mif);
1464*4882a593Smuzhiyun 		return ret;
1465*4882a593Smuzhiyun 	}
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 	ret = exynos5_dmc_init_clks(dmc);
1468*4882a593Smuzhiyun 	if (ret)
1469*4882a593Smuzhiyun 		return ret;
1470*4882a593Smuzhiyun 
1471*4882a593Smuzhiyun 	ret = of_get_dram_timings(dmc);
1472*4882a593Smuzhiyun 	if (ret) {
1473*4882a593Smuzhiyun 		dev_warn(dev, "couldn't initialize timings settings\n");
1474*4882a593Smuzhiyun 		goto remove_clocks;
1475*4882a593Smuzhiyun 	}
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 	ret = exynos5_dmc_set_pause_on_switching(dmc);
1478*4882a593Smuzhiyun 	if (ret) {
1479*4882a593Smuzhiyun 		dev_warn(dev, "couldn't get access to PAUSE register\n");
1480*4882a593Smuzhiyun 		goto remove_clocks;
1481*4882a593Smuzhiyun 	}
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun 	/* There is two modes in which the driver works: polling or IRQ */
1484*4882a593Smuzhiyun 	irq[0] = platform_get_irq_byname(pdev, "drex_0");
1485*4882a593Smuzhiyun 	irq[1] = platform_get_irq_byname(pdev, "drex_1");
1486*4882a593Smuzhiyun 	if (irq[0] > 0 && irq[1] > 0 && irqmode) {
1487*4882a593Smuzhiyun 		ret = devm_request_threaded_irq(dev, irq[0], NULL,
1488*4882a593Smuzhiyun 						dmc_irq_thread, IRQF_ONESHOT,
1489*4882a593Smuzhiyun 						dev_name(dev), dmc);
1490*4882a593Smuzhiyun 		if (ret) {
1491*4882a593Smuzhiyun 			dev_err(dev, "couldn't grab IRQ\n");
1492*4882a593Smuzhiyun 			goto remove_clocks;
1493*4882a593Smuzhiyun 		}
1494*4882a593Smuzhiyun 
1495*4882a593Smuzhiyun 		ret = devm_request_threaded_irq(dev, irq[1], NULL,
1496*4882a593Smuzhiyun 						dmc_irq_thread, IRQF_ONESHOT,
1497*4882a593Smuzhiyun 						dev_name(dev), dmc);
1498*4882a593Smuzhiyun 		if (ret) {
1499*4882a593Smuzhiyun 			dev_err(dev, "couldn't grab IRQ\n");
1500*4882a593Smuzhiyun 			goto remove_clocks;
1501*4882a593Smuzhiyun 		}
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 		/*
1504*4882a593Smuzhiyun 		 * Setup default thresholds for the devfreq governor.
1505*4882a593Smuzhiyun 		 * The values are chosen based on experiments.
1506*4882a593Smuzhiyun 		 */
1507*4882a593Smuzhiyun 		dmc->gov_data.upthreshold = 55;
1508*4882a593Smuzhiyun 		dmc->gov_data.downdifferential = 5;
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 		exynos5_dmc_enable_perf_events(dmc);
1511*4882a593Smuzhiyun 
1512*4882a593Smuzhiyun 		dmc->in_irq_mode = 1;
1513*4882a593Smuzhiyun 	} else {
1514*4882a593Smuzhiyun 		ret = exynos5_performance_counters_init(dmc);
1515*4882a593Smuzhiyun 		if (ret) {
1516*4882a593Smuzhiyun 			dev_warn(dev, "couldn't probe performance counters\n");
1517*4882a593Smuzhiyun 			goto remove_clocks;
1518*4882a593Smuzhiyun 		}
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 		/*
1521*4882a593Smuzhiyun 		 * Setup default thresholds for the devfreq governor.
1522*4882a593Smuzhiyun 		 * The values are chosen based on experiments.
1523*4882a593Smuzhiyun 		 */
1524*4882a593Smuzhiyun 		dmc->gov_data.upthreshold = 10;
1525*4882a593Smuzhiyun 		dmc->gov_data.downdifferential = 5;
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun 		exynos5_dmc_df_profile.polling_ms = 100;
1528*4882a593Smuzhiyun 	}
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 	dmc->df = devm_devfreq_add_device(dev, &exynos5_dmc_df_profile,
1531*4882a593Smuzhiyun 					  DEVFREQ_GOV_SIMPLE_ONDEMAND,
1532*4882a593Smuzhiyun 					  &dmc->gov_data);
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	if (IS_ERR(dmc->df)) {
1535*4882a593Smuzhiyun 		ret = PTR_ERR(dmc->df);
1536*4882a593Smuzhiyun 		goto err_devfreq_add;
1537*4882a593Smuzhiyun 	}
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	if (dmc->in_irq_mode)
1540*4882a593Smuzhiyun 		exynos5_dmc_start_perf_events(dmc, PERF_COUNTER_START_VALUE);
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 	dev_info(dev, "DMC initialized, in irq mode: %d\n", dmc->in_irq_mode);
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun 	return 0;
1545*4882a593Smuzhiyun 
1546*4882a593Smuzhiyun err_devfreq_add:
1547*4882a593Smuzhiyun 	if (dmc->in_irq_mode)
1548*4882a593Smuzhiyun 		exynos5_dmc_disable_perf_events(dmc);
1549*4882a593Smuzhiyun 	else
1550*4882a593Smuzhiyun 		exynos5_counters_disable_edev(dmc);
1551*4882a593Smuzhiyun remove_clocks:
1552*4882a593Smuzhiyun 	clk_disable_unprepare(dmc->mout_bpll);
1553*4882a593Smuzhiyun 	clk_disable_unprepare(dmc->fout_bpll);
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	return ret;
1556*4882a593Smuzhiyun }
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun /**
1559*4882a593Smuzhiyun  * exynos5_dmc_remove() - Remove function for the platform device
1560*4882a593Smuzhiyun  * @pdev:	platform device which is going to be removed
1561*4882a593Smuzhiyun  *
1562*4882a593Smuzhiyun  * The function relies on 'devm' framework function which automatically
1563*4882a593Smuzhiyun  * clean the device's resources. It just calls explicitly disable function for
1564*4882a593Smuzhiyun  * the performance counters.
1565*4882a593Smuzhiyun  */
exynos5_dmc_remove(struct platform_device * pdev)1566*4882a593Smuzhiyun static int exynos5_dmc_remove(struct platform_device *pdev)
1567*4882a593Smuzhiyun {
1568*4882a593Smuzhiyun 	struct exynos5_dmc *dmc = dev_get_drvdata(&pdev->dev);
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	if (dmc->in_irq_mode)
1571*4882a593Smuzhiyun 		exynos5_dmc_disable_perf_events(dmc);
1572*4882a593Smuzhiyun 	else
1573*4882a593Smuzhiyun 		exynos5_counters_disable_edev(dmc);
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	clk_disable_unprepare(dmc->mout_bpll);
1576*4882a593Smuzhiyun 	clk_disable_unprepare(dmc->fout_bpll);
1577*4882a593Smuzhiyun 
1578*4882a593Smuzhiyun 	dev_pm_opp_remove_table(dmc->dev);
1579*4882a593Smuzhiyun 
1580*4882a593Smuzhiyun 	return 0;
1581*4882a593Smuzhiyun }
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun static const struct of_device_id exynos5_dmc_of_match[] = {
1584*4882a593Smuzhiyun 	{ .compatible = "samsung,exynos5422-dmc", },
1585*4882a593Smuzhiyun 	{ },
1586*4882a593Smuzhiyun };
1587*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, exynos5_dmc_of_match);
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun static struct platform_driver exynos5_dmc_platdrv = {
1590*4882a593Smuzhiyun 	.probe	= exynos5_dmc_probe,
1591*4882a593Smuzhiyun 	.remove = exynos5_dmc_remove,
1592*4882a593Smuzhiyun 	.driver = {
1593*4882a593Smuzhiyun 		.name	= "exynos5-dmc",
1594*4882a593Smuzhiyun 		.of_match_table = exynos5_dmc_of_match,
1595*4882a593Smuzhiyun 	},
1596*4882a593Smuzhiyun };
1597*4882a593Smuzhiyun module_platform_driver(exynos5_dmc_platdrv);
1598*4882a593Smuzhiyun MODULE_DESCRIPTION("Driver for Exynos5422 Dynamic Memory Controller dynamic frequency and voltage change");
1599*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1600*4882a593Smuzhiyun MODULE_AUTHOR("Lukasz Luba");
1601