xref: /linux/drivers/memory/samsung/exynos5422-dmc.c (revision bbf918863e183d66adf00ca1b24fb641149a0d3d)
16e7674c3SLukasz Luba // SPDX-License-Identifier: GPL-2.0
26e7674c3SLukasz Luba /*
36e7674c3SLukasz Luba  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
46e7674c3SLukasz Luba  * Author: Lukasz Luba <l.luba@partner.samsung.com>
56e7674c3SLukasz Luba  */
66e7674c3SLukasz Luba 
76e7674c3SLukasz Luba #include <linux/clk.h>
86e7674c3SLukasz Luba #include <linux/devfreq.h>
96e7674c3SLukasz Luba #include <linux/devfreq-event.h>
106e7674c3SLukasz Luba #include <linux/device.h>
11*bbf91886SLukasz Luba #include <linux/interrupt.h>
126e7674c3SLukasz Luba #include <linux/io.h>
136e7674c3SLukasz Luba #include <linux/mfd/syscon.h>
146e7674c3SLukasz Luba #include <linux/module.h>
156e7674c3SLukasz Luba #include <linux/of_device.h>
166e7674c3SLukasz Luba #include <linux/pm_opp.h>
176e7674c3SLukasz Luba #include <linux/platform_device.h>
186e7674c3SLukasz Luba #include <linux/regmap.h>
196e7674c3SLukasz Luba #include <linux/regulator/consumer.h>
206e7674c3SLukasz Luba #include <linux/slab.h>
216e7674c3SLukasz Luba #include "../jedec_ddr.h"
226e7674c3SLukasz Luba #include "../of_memory.h"
236e7674c3SLukasz Luba 
246e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGAREF		(0x0030)
256e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGROW0		(0x0034)
266e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGDATA0		(0x0038)
276e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGPOWER0		(0x003C)
286e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGROW1		(0x00E4)
296e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGDATA1		(0x00E8)
306e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGPOWER1		(0x00EC)
316e7674c3SLukasz Luba #define CDREX_PAUSE				(0x2091c)
326e7674c3SLukasz Luba #define CDREX_LPDDR3PHY_CON3			(0x20a20)
336e7674c3SLukasz Luba #define CDREX_LPDDR3PHY_CLKM_SRC		(0x20700)
346e7674c3SLukasz Luba #define EXYNOS5_TIMING_SET_SWI			BIT(28)
356e7674c3SLukasz Luba #define USE_MX_MSPLL_TIMINGS			(1)
366e7674c3SLukasz Luba #define USE_BPLL_TIMINGS			(0)
376e7674c3SLukasz Luba #define EXYNOS5_AREF_NORMAL			(0x2e)
386e7674c3SLukasz Luba 
39*bbf91886SLukasz Luba #define DREX_PPCCLKCON		(0x0130)
40*bbf91886SLukasz Luba #define DREX_PEREV2CONFIG	(0x013c)
41*bbf91886SLukasz Luba #define DREX_PMNC_PPC		(0xE000)
42*bbf91886SLukasz Luba #define DREX_CNTENS_PPC		(0xE010)
43*bbf91886SLukasz Luba #define DREX_CNTENC_PPC		(0xE020)
44*bbf91886SLukasz Luba #define DREX_INTENS_PPC		(0xE030)
45*bbf91886SLukasz Luba #define DREX_INTENC_PPC		(0xE040)
46*bbf91886SLukasz Luba #define DREX_FLAG_PPC		(0xE050)
47*bbf91886SLukasz Luba #define DREX_PMCNT2_PPC		(0xE130)
48*bbf91886SLukasz Luba 
49*bbf91886SLukasz Luba /*
50*bbf91886SLukasz Luba  * A value for register DREX_PMNC_PPC which should be written to reset
51*bbf91886SLukasz Luba  * the cycle counter CCNT (a reference wall clock). It sets zero to the
52*bbf91886SLukasz Luba  * CCNT counter.
53*bbf91886SLukasz Luba  */
54*bbf91886SLukasz Luba #define CC_RESET		BIT(2)
55*bbf91886SLukasz Luba 
56*bbf91886SLukasz Luba /*
57*bbf91886SLukasz Luba  * A value for register DREX_PMNC_PPC which does the reset of all performance
58*bbf91886SLukasz Luba  * counters to zero.
59*bbf91886SLukasz Luba  */
60*bbf91886SLukasz Luba #define PPC_COUNTER_RESET	BIT(1)
61*bbf91886SLukasz Luba 
62*bbf91886SLukasz Luba /*
63*bbf91886SLukasz Luba  * Enables all configured counters (including cycle counter). The value should
64*bbf91886SLukasz Luba  * be written to the register DREX_PMNC_PPC.
65*bbf91886SLukasz Luba  */
66*bbf91886SLukasz Luba #define PPC_ENABLE		BIT(0)
67*bbf91886SLukasz Luba 
68*bbf91886SLukasz Luba /* A value for register DREX_PPCCLKCON which enables performance events clock.
69*bbf91886SLukasz Luba  * Must be written before first access to the performance counters register
70*bbf91886SLukasz Luba  * set, otherwise it could crash.
71*bbf91886SLukasz Luba  */
72*bbf91886SLukasz Luba #define PEREV_CLK_EN		BIT(0)
73*bbf91886SLukasz Luba 
74*bbf91886SLukasz Luba /*
75*bbf91886SLukasz Luba  * Values which are used to enable counters, interrupts or configure flags of
76*bbf91886SLukasz Luba  * the performance counters. They configure counter 2 and cycle counter.
77*bbf91886SLukasz Luba  */
78*bbf91886SLukasz Luba #define PERF_CNT2		BIT(2)
79*bbf91886SLukasz Luba #define PERF_CCNT		BIT(31)
80*bbf91886SLukasz Luba 
81*bbf91886SLukasz Luba /*
82*bbf91886SLukasz Luba  * Performance event types which are used for setting the preferred event
83*bbf91886SLukasz Luba  * to track in the counters.
84*bbf91886SLukasz Luba  * There is a set of different types, the values are from range 0 to 0x6f.
85*bbf91886SLukasz Luba  * These settings should be written to the configuration register which manages
86*bbf91886SLukasz Luba  * the type of the event (register DREX_PEREV2CONFIG).
87*bbf91886SLukasz Luba  */
88*bbf91886SLukasz Luba #define READ_TRANSFER_CH0	(0x6d)
89*bbf91886SLukasz Luba #define READ_TRANSFER_CH1	(0x6f)
90*bbf91886SLukasz Luba 
91*bbf91886SLukasz Luba #define PERF_COUNTER_START_VALUE 0xff000000
92*bbf91886SLukasz Luba #define PERF_EVENT_UP_DOWN_THRESHOLD 900000000ULL
93*bbf91886SLukasz Luba 
946e7674c3SLukasz Luba /**
956e7674c3SLukasz Luba  * struct dmc_opp_table - Operating level desciption
966e7674c3SLukasz Luba  *
976e7674c3SLukasz Luba  * Covers frequency and voltage settings of the DMC operating mode.
986e7674c3SLukasz Luba  */
996e7674c3SLukasz Luba struct dmc_opp_table {
1006e7674c3SLukasz Luba 	u32 freq_hz;
1016e7674c3SLukasz Luba 	u32 volt_uv;
1026e7674c3SLukasz Luba };
1036e7674c3SLukasz Luba 
1046e7674c3SLukasz Luba /**
1056e7674c3SLukasz Luba  * struct exynos5_dmc - main structure describing DMC device
1066e7674c3SLukasz Luba  *
1076e7674c3SLukasz Luba  * The main structure for the Dynamic Memory Controller which covers clocks,
1086e7674c3SLukasz Luba  * memory regions, HW information, parameters and current operating mode.
1096e7674c3SLukasz Luba  */
1106e7674c3SLukasz Luba struct exynos5_dmc {
1116e7674c3SLukasz Luba 	struct device *dev;
1126e7674c3SLukasz Luba 	struct devfreq *df;
1136e7674c3SLukasz Luba 	struct devfreq_simple_ondemand_data gov_data;
1146e7674c3SLukasz Luba 	void __iomem *base_drexi0;
1156e7674c3SLukasz Luba 	void __iomem *base_drexi1;
1166e7674c3SLukasz Luba 	struct regmap *clk_regmap;
1176e7674c3SLukasz Luba 	struct mutex lock;
1186e7674c3SLukasz Luba 	unsigned long curr_rate;
1196e7674c3SLukasz Luba 	unsigned long curr_volt;
1206e7674c3SLukasz Luba 	unsigned long bypass_rate;
1216e7674c3SLukasz Luba 	struct dmc_opp_table *opp;
1226e7674c3SLukasz Luba 	struct dmc_opp_table opp_bypass;
1236e7674c3SLukasz Luba 	int opp_count;
1246e7674c3SLukasz Luba 	u32 timings_arr_size;
1256e7674c3SLukasz Luba 	u32 *timing_row;
1266e7674c3SLukasz Luba 	u32 *timing_data;
1276e7674c3SLukasz Luba 	u32 *timing_power;
1286e7674c3SLukasz Luba 	const struct lpddr3_timings *timings;
1296e7674c3SLukasz Luba 	const struct lpddr3_min_tck *min_tck;
1306e7674c3SLukasz Luba 	u32 bypass_timing_row;
1316e7674c3SLukasz Luba 	u32 bypass_timing_data;
1326e7674c3SLukasz Luba 	u32 bypass_timing_power;
1336e7674c3SLukasz Luba 	struct regulator *vdd_mif;
1346e7674c3SLukasz Luba 	struct clk *fout_spll;
1356e7674c3SLukasz Luba 	struct clk *fout_bpll;
1366e7674c3SLukasz Luba 	struct clk *mout_spll;
1376e7674c3SLukasz Luba 	struct clk *mout_bpll;
1386e7674c3SLukasz Luba 	struct clk *mout_mclk_cdrex;
1396e7674c3SLukasz Luba 	struct clk *mout_mx_mspll_ccore;
1406e7674c3SLukasz Luba 	struct clk *mx_mspll_ccore_phy;
1416e7674c3SLukasz Luba 	struct clk *mout_mx_mspll_ccore_phy;
1426e7674c3SLukasz Luba 	struct devfreq_event_dev **counter;
1436e7674c3SLukasz Luba 	int num_counters;
144*bbf91886SLukasz Luba 	u64 last_overflow_ts[2];
145*bbf91886SLukasz Luba 	unsigned long load;
146*bbf91886SLukasz Luba 	unsigned long total;
147*bbf91886SLukasz Luba 	bool in_irq_mode;
1486e7674c3SLukasz Luba };
1496e7674c3SLukasz Luba 
1506e7674c3SLukasz Luba #define TIMING_FIELD(t_name, t_bit_beg, t_bit_end) \
1516e7674c3SLukasz Luba 	{ .name = t_name, .bit_beg = t_bit_beg, .bit_end = t_bit_end }
1526e7674c3SLukasz Luba 
1536e7674c3SLukasz Luba #define TIMING_VAL2REG(timing, t_val)			\
1546e7674c3SLukasz Luba ({							\
1556e7674c3SLukasz Luba 		u32 __val;				\
1566e7674c3SLukasz Luba 		__val = (t_val) << (timing)->bit_beg;	\
1576e7674c3SLukasz Luba 		__val;					\
1586e7674c3SLukasz Luba })
1596e7674c3SLukasz Luba 
1606e7674c3SLukasz Luba struct timing_reg {
1616e7674c3SLukasz Luba 	char *name;
1626e7674c3SLukasz Luba 	int bit_beg;
1636e7674c3SLukasz Luba 	int bit_end;
1646e7674c3SLukasz Luba 	unsigned int val;
1656e7674c3SLukasz Luba };
1666e7674c3SLukasz Luba 
1676e7674c3SLukasz Luba static const struct timing_reg timing_row[] = {
1686e7674c3SLukasz Luba 	TIMING_FIELD("tRFC", 24, 31),
1696e7674c3SLukasz Luba 	TIMING_FIELD("tRRD", 20, 23),
1706e7674c3SLukasz Luba 	TIMING_FIELD("tRP", 16, 19),
1716e7674c3SLukasz Luba 	TIMING_FIELD("tRCD", 12, 15),
1726e7674c3SLukasz Luba 	TIMING_FIELD("tRC", 6, 11),
1736e7674c3SLukasz Luba 	TIMING_FIELD("tRAS", 0, 5),
1746e7674c3SLukasz Luba };
1756e7674c3SLukasz Luba 
1766e7674c3SLukasz Luba static const struct timing_reg timing_data[] = {
1776e7674c3SLukasz Luba 	TIMING_FIELD("tWTR", 28, 31),
1786e7674c3SLukasz Luba 	TIMING_FIELD("tWR", 24, 27),
1796e7674c3SLukasz Luba 	TIMING_FIELD("tRTP", 20, 23),
1806e7674c3SLukasz Luba 	TIMING_FIELD("tW2W-C2C", 14, 14),
1816e7674c3SLukasz Luba 	TIMING_FIELD("tR2R-C2C", 12, 12),
1826e7674c3SLukasz Luba 	TIMING_FIELD("WL", 8, 11),
1836e7674c3SLukasz Luba 	TIMING_FIELD("tDQSCK", 4, 7),
1846e7674c3SLukasz Luba 	TIMING_FIELD("RL", 0, 3),
1856e7674c3SLukasz Luba };
1866e7674c3SLukasz Luba 
1876e7674c3SLukasz Luba static const struct timing_reg timing_power[] = {
1886e7674c3SLukasz Luba 	TIMING_FIELD("tFAW", 26, 31),
1896e7674c3SLukasz Luba 	TIMING_FIELD("tXSR", 16, 25),
1906e7674c3SLukasz Luba 	TIMING_FIELD("tXP", 8, 15),
1916e7674c3SLukasz Luba 	TIMING_FIELD("tCKE", 4, 7),
1926e7674c3SLukasz Luba 	TIMING_FIELD("tMRD", 0, 3),
1936e7674c3SLukasz Luba };
1946e7674c3SLukasz Luba 
1956e7674c3SLukasz Luba #define TIMING_COUNT (ARRAY_SIZE(timing_row) + ARRAY_SIZE(timing_data) + \
1966e7674c3SLukasz Luba 		      ARRAY_SIZE(timing_power))
1976e7674c3SLukasz Luba 
1986e7674c3SLukasz Luba static int exynos5_counters_set_event(struct exynos5_dmc *dmc)
1996e7674c3SLukasz Luba {
2006e7674c3SLukasz Luba 	int i, ret;
2016e7674c3SLukasz Luba 
2026e7674c3SLukasz Luba 	for (i = 0; i < dmc->num_counters; i++) {
2036e7674c3SLukasz Luba 		if (!dmc->counter[i])
2046e7674c3SLukasz Luba 			continue;
2056e7674c3SLukasz Luba 		ret = devfreq_event_set_event(dmc->counter[i]);
2066e7674c3SLukasz Luba 		if (ret < 0)
2076e7674c3SLukasz Luba 			return ret;
2086e7674c3SLukasz Luba 	}
2096e7674c3SLukasz Luba 	return 0;
2106e7674c3SLukasz Luba }
2116e7674c3SLukasz Luba 
2126e7674c3SLukasz Luba static int exynos5_counters_enable_edev(struct exynos5_dmc *dmc)
2136e7674c3SLukasz Luba {
2146e7674c3SLukasz Luba 	int i, ret;
2156e7674c3SLukasz Luba 
2166e7674c3SLukasz Luba 	for (i = 0; i < dmc->num_counters; i++) {
2176e7674c3SLukasz Luba 		if (!dmc->counter[i])
2186e7674c3SLukasz Luba 			continue;
2196e7674c3SLukasz Luba 		ret = devfreq_event_enable_edev(dmc->counter[i]);
2206e7674c3SLukasz Luba 		if (ret < 0)
2216e7674c3SLukasz Luba 			return ret;
2226e7674c3SLukasz Luba 	}
2236e7674c3SLukasz Luba 	return 0;
2246e7674c3SLukasz Luba }
2256e7674c3SLukasz Luba 
2266e7674c3SLukasz Luba static int exynos5_counters_disable_edev(struct exynos5_dmc *dmc)
2276e7674c3SLukasz Luba {
2286e7674c3SLukasz Luba 	int i, ret;
2296e7674c3SLukasz Luba 
2306e7674c3SLukasz Luba 	for (i = 0; i < dmc->num_counters; i++) {
2316e7674c3SLukasz Luba 		if (!dmc->counter[i])
2326e7674c3SLukasz Luba 			continue;
2336e7674c3SLukasz Luba 		ret = devfreq_event_disable_edev(dmc->counter[i]);
2346e7674c3SLukasz Luba 		if (ret < 0)
2356e7674c3SLukasz Luba 			return ret;
2366e7674c3SLukasz Luba 	}
2376e7674c3SLukasz Luba 	return 0;
2386e7674c3SLukasz Luba }
2396e7674c3SLukasz Luba 
2406e7674c3SLukasz Luba /**
2416e7674c3SLukasz Luba  * find_target_freq_id() - Finds requested frequency in local DMC configuration
2426e7674c3SLukasz Luba  * @dmc:	device for which the information is checked
2436e7674c3SLukasz Luba  * @target_rate:	requested frequency in KHz
2446e7674c3SLukasz Luba  *
2456e7674c3SLukasz Luba  * Seeks in the local DMC driver structure for the requested frequency value
2466e7674c3SLukasz Luba  * and returns index or error value.
2476e7674c3SLukasz Luba  */
2486e7674c3SLukasz Luba static int find_target_freq_idx(struct exynos5_dmc *dmc,
2496e7674c3SLukasz Luba 				unsigned long target_rate)
2506e7674c3SLukasz Luba {
2516e7674c3SLukasz Luba 	int i;
2526e7674c3SLukasz Luba 
2536e7674c3SLukasz Luba 	for (i = dmc->opp_count - 1; i >= 0; i--)
2546e7674c3SLukasz Luba 		if (dmc->opp[i].freq_hz <= target_rate)
2556e7674c3SLukasz Luba 			return i;
2566e7674c3SLukasz Luba 
2576e7674c3SLukasz Luba 	return -EINVAL;
2586e7674c3SLukasz Luba }
2596e7674c3SLukasz Luba 
2606e7674c3SLukasz Luba /**
2616e7674c3SLukasz Luba  * exynos5_switch_timing_regs() - Changes bank register set for DRAM timings
2626e7674c3SLukasz Luba  * @dmc:	device for which the new settings is going to be applied
2636e7674c3SLukasz Luba  * @set:	boolean variable passing set value
2646e7674c3SLukasz Luba  *
2656e7674c3SLukasz Luba  * Changes the register set, which holds timing parameters.
2666e7674c3SLukasz Luba  * There is two register sets: 0 and 1. The register set 0
2676e7674c3SLukasz Luba  * is used in normal operation when the clock is provided from main PLL.
2686e7674c3SLukasz Luba  * The bank register set 1 is used when the main PLL frequency is going to be
2696e7674c3SLukasz Luba  * changed and the clock is taken from alternative, stable source.
2706e7674c3SLukasz Luba  * This function switches between these banks according to the
2716e7674c3SLukasz Luba  * currently used clock source.
2726e7674c3SLukasz Luba  */
2736e7674c3SLukasz Luba static void exynos5_switch_timing_regs(struct exynos5_dmc *dmc, bool set)
2746e7674c3SLukasz Luba {
2756e7674c3SLukasz Luba 	unsigned int reg;
2766e7674c3SLukasz Luba 	int ret;
2776e7674c3SLukasz Luba 
2786e7674c3SLukasz Luba 	ret = regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, &reg);
2796e7674c3SLukasz Luba 
2806e7674c3SLukasz Luba 	if (set)
2816e7674c3SLukasz Luba 		reg |= EXYNOS5_TIMING_SET_SWI;
2826e7674c3SLukasz Luba 	else
2836e7674c3SLukasz Luba 		reg &= ~EXYNOS5_TIMING_SET_SWI;
2846e7674c3SLukasz Luba 
2856e7674c3SLukasz Luba 	regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, reg);
2866e7674c3SLukasz Luba }
2876e7674c3SLukasz Luba 
2886e7674c3SLukasz Luba /**
2896e7674c3SLukasz Luba  * exynos5_init_freq_table() - Initialized PM OPP framework
2906e7674c3SLukasz Luba  * @dmc:	DMC device for which the frequencies are used for OPP init
2916e7674c3SLukasz Luba  * @profile:	devfreq device's profile
2926e7674c3SLukasz Luba  *
2936e7674c3SLukasz Luba  * Populate the devfreq device's OPP table based on current frequency, voltage.
2946e7674c3SLukasz Luba  */
2956e7674c3SLukasz Luba static int exynos5_init_freq_table(struct exynos5_dmc *dmc,
2966e7674c3SLukasz Luba 				   struct devfreq_dev_profile *profile)
2976e7674c3SLukasz Luba {
2986e7674c3SLukasz Luba 	int i, ret;
2996e7674c3SLukasz Luba 	int idx;
3006e7674c3SLukasz Luba 	unsigned long freq;
3016e7674c3SLukasz Luba 
3026e7674c3SLukasz Luba 	ret = dev_pm_opp_of_add_table(dmc->dev);
3036e7674c3SLukasz Luba 	if (ret < 0) {
3046e7674c3SLukasz Luba 		dev_err(dmc->dev, "Failed to get OPP table\n");
3056e7674c3SLukasz Luba 		return ret;
3066e7674c3SLukasz Luba 	}
3076e7674c3SLukasz Luba 
3086e7674c3SLukasz Luba 	dmc->opp_count = dev_pm_opp_get_opp_count(dmc->dev);
3096e7674c3SLukasz Luba 
3106e7674c3SLukasz Luba 	dmc->opp = devm_kmalloc_array(dmc->dev, dmc->opp_count,
3116e7674c3SLukasz Luba 				      sizeof(struct dmc_opp_table), GFP_KERNEL);
3126e7674c3SLukasz Luba 	if (!dmc->opp)
3136e7674c3SLukasz Luba 		goto err_opp;
3146e7674c3SLukasz Luba 
3156e7674c3SLukasz Luba 	idx = dmc->opp_count - 1;
3166e7674c3SLukasz Luba 	for (i = 0, freq = ULONG_MAX; i < dmc->opp_count; i++, freq--) {
3176e7674c3SLukasz Luba 		struct dev_pm_opp *opp;
3186e7674c3SLukasz Luba 
3196e7674c3SLukasz Luba 		opp = dev_pm_opp_find_freq_floor(dmc->dev, &freq);
3206e7674c3SLukasz Luba 		if (IS_ERR(opp))
321d51e6a69SLukasz Luba 			goto err_opp;
3226e7674c3SLukasz Luba 
3236e7674c3SLukasz Luba 		dmc->opp[idx - i].freq_hz = freq;
3246e7674c3SLukasz Luba 		dmc->opp[idx - i].volt_uv = dev_pm_opp_get_voltage(opp);
3256e7674c3SLukasz Luba 
3266e7674c3SLukasz Luba 		dev_pm_opp_put(opp);
3276e7674c3SLukasz Luba 	}
3286e7674c3SLukasz Luba 
3296e7674c3SLukasz Luba 	return 0;
3306e7674c3SLukasz Luba 
3316e7674c3SLukasz Luba err_opp:
3326e7674c3SLukasz Luba 	dev_pm_opp_of_remove_table(dmc->dev);
3336e7674c3SLukasz Luba 
3346e7674c3SLukasz Luba 	return -EINVAL;
3356e7674c3SLukasz Luba }
3366e7674c3SLukasz Luba 
3376e7674c3SLukasz Luba /**
3386e7674c3SLukasz Luba  * exynos5_set_bypass_dram_timings() - Low-level changes of the DRAM timings
3396e7674c3SLukasz Luba  * @dmc:	device for which the new settings is going to be applied
3406e7674c3SLukasz Luba  * @param:	DRAM parameters which passes timing data
3416e7674c3SLukasz Luba  *
3426e7674c3SLukasz Luba  * Low-level function for changing timings for DRAM memory clocking from
3436e7674c3SLukasz Luba  * 'bypass' clock source (fixed frequency @400MHz).
3446e7674c3SLukasz Luba  * It uses timing bank registers set 1.
3456e7674c3SLukasz Luba  */
3466e7674c3SLukasz Luba static void exynos5_set_bypass_dram_timings(struct exynos5_dmc *dmc)
3476e7674c3SLukasz Luba {
3486e7674c3SLukasz Luba 	writel(EXYNOS5_AREF_NORMAL,
3496e7674c3SLukasz Luba 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF);
3506e7674c3SLukasz Luba 
3516e7674c3SLukasz Luba 	writel(dmc->bypass_timing_row,
3526e7674c3SLukasz Luba 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW1);
3536e7674c3SLukasz Luba 	writel(dmc->bypass_timing_row,
3546e7674c3SLukasz Luba 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW1);
3556e7674c3SLukasz Luba 	writel(dmc->bypass_timing_data,
3566e7674c3SLukasz Luba 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA1);
3576e7674c3SLukasz Luba 	writel(dmc->bypass_timing_data,
3586e7674c3SLukasz Luba 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA1);
3596e7674c3SLukasz Luba 	writel(dmc->bypass_timing_power,
3606e7674c3SLukasz Luba 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER1);
3616e7674c3SLukasz Luba 	writel(dmc->bypass_timing_power,
3626e7674c3SLukasz Luba 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER1);
3636e7674c3SLukasz Luba }
3646e7674c3SLukasz Luba 
3656e7674c3SLukasz Luba /**
3666e7674c3SLukasz Luba  * exynos5_dram_change_timings() - Low-level changes of the DRAM final timings
3676e7674c3SLukasz Luba  * @dmc:	device for which the new settings is going to be applied
3686e7674c3SLukasz Luba  * @target_rate:	target frequency of the DMC
3696e7674c3SLukasz Luba  *
3706e7674c3SLukasz Luba  * Low-level function for changing timings for DRAM memory operating from main
3716e7674c3SLukasz Luba  * clock source (BPLL), which can have different frequencies. Thus, each
3726e7674c3SLukasz Luba  * frequency must have corresponding timings register values in order to keep
3736e7674c3SLukasz Luba  * the needed delays.
3746e7674c3SLukasz Luba  * It uses timing bank registers set 0.
3756e7674c3SLukasz Luba  */
3766e7674c3SLukasz Luba static int exynos5_dram_change_timings(struct exynos5_dmc *dmc,
3776e7674c3SLukasz Luba 				       unsigned long target_rate)
3786e7674c3SLukasz Luba {
3796e7674c3SLukasz Luba 	int idx;
3806e7674c3SLukasz Luba 
3816e7674c3SLukasz Luba 	for (idx = dmc->opp_count - 1; idx >= 0; idx--)
3826e7674c3SLukasz Luba 		if (dmc->opp[idx].freq_hz <= target_rate)
3836e7674c3SLukasz Luba 			break;
3846e7674c3SLukasz Luba 
3856e7674c3SLukasz Luba 	if (idx < 0)
3866e7674c3SLukasz Luba 		return -EINVAL;
3876e7674c3SLukasz Luba 
3886e7674c3SLukasz Luba 	writel(EXYNOS5_AREF_NORMAL,
3896e7674c3SLukasz Luba 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF);
3906e7674c3SLukasz Luba 
3916e7674c3SLukasz Luba 	writel(dmc->timing_row[idx],
3926e7674c3SLukasz Luba 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW0);
3936e7674c3SLukasz Luba 	writel(dmc->timing_row[idx],
3946e7674c3SLukasz Luba 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW0);
3956e7674c3SLukasz Luba 	writel(dmc->timing_data[idx],
3966e7674c3SLukasz Luba 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA0);
3976e7674c3SLukasz Luba 	writel(dmc->timing_data[idx],
3986e7674c3SLukasz Luba 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA0);
3996e7674c3SLukasz Luba 	writel(dmc->timing_power[idx],
4006e7674c3SLukasz Luba 	       dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER0);
4016e7674c3SLukasz Luba 	writel(dmc->timing_power[idx],
4026e7674c3SLukasz Luba 	       dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER0);
4036e7674c3SLukasz Luba 
4046e7674c3SLukasz Luba 	return 0;
4056e7674c3SLukasz Luba }
4066e7674c3SLukasz Luba 
4076e7674c3SLukasz Luba /**
4086e7674c3SLukasz Luba  * exynos5_dmc_align_target_voltage() - Sets the final voltage for the DMC
4096e7674c3SLukasz Luba  * @dmc:	device for which it is going to be set
4106e7674c3SLukasz Luba  * @target_volt:	new voltage which is chosen to be final
4116e7674c3SLukasz Luba  *
4126e7674c3SLukasz Luba  * Function tries to align voltage to the safe level for 'normal' mode.
4136e7674c3SLukasz Luba  * It checks the need of higher voltage and changes the value. The target
4146e7674c3SLukasz Luba  * voltage might be lower that currently set and still the system will be
4156e7674c3SLukasz Luba  * stable.
4166e7674c3SLukasz Luba  */
4176e7674c3SLukasz Luba static int exynos5_dmc_align_target_voltage(struct exynos5_dmc *dmc,
4186e7674c3SLukasz Luba 					    unsigned long target_volt)
4196e7674c3SLukasz Luba {
4206e7674c3SLukasz Luba 	int ret = 0;
4216e7674c3SLukasz Luba 
4226e7674c3SLukasz Luba 	if (dmc->curr_volt <= target_volt)
4236e7674c3SLukasz Luba 		return 0;
4246e7674c3SLukasz Luba 
4256e7674c3SLukasz Luba 	ret = regulator_set_voltage(dmc->vdd_mif, target_volt,
4266e7674c3SLukasz Luba 				    target_volt);
4276e7674c3SLukasz Luba 	if (!ret)
4286e7674c3SLukasz Luba 		dmc->curr_volt = target_volt;
4296e7674c3SLukasz Luba 
4306e7674c3SLukasz Luba 	return ret;
4316e7674c3SLukasz Luba }
4326e7674c3SLukasz Luba 
4336e7674c3SLukasz Luba /**
4346e7674c3SLukasz Luba  * exynos5_dmc_align_bypass_voltage() - Sets the voltage for the DMC
4356e7674c3SLukasz Luba  * @dmc:	device for which it is going to be set
4366e7674c3SLukasz Luba  * @target_volt:	new voltage which is chosen to be final
4376e7674c3SLukasz Luba  *
4386e7674c3SLukasz Luba  * Function tries to align voltage to the safe level for the 'bypass' mode.
4396e7674c3SLukasz Luba  * It checks the need of higher voltage and changes the value.
4406e7674c3SLukasz Luba  * The target voltage must not be less than currently needed, because
4416e7674c3SLukasz Luba  * for current frequency the device might become unstable.
4426e7674c3SLukasz Luba  */
4436e7674c3SLukasz Luba static int exynos5_dmc_align_bypass_voltage(struct exynos5_dmc *dmc,
4446e7674c3SLukasz Luba 					    unsigned long target_volt)
4456e7674c3SLukasz Luba {
4466e7674c3SLukasz Luba 	int ret = 0;
4476e7674c3SLukasz Luba 	unsigned long bypass_volt = dmc->opp_bypass.volt_uv;
4486e7674c3SLukasz Luba 
4496e7674c3SLukasz Luba 	target_volt = max(bypass_volt, target_volt);
4506e7674c3SLukasz Luba 
4516e7674c3SLukasz Luba 	if (dmc->curr_volt >= target_volt)
4526e7674c3SLukasz Luba 		return 0;
4536e7674c3SLukasz Luba 
4546e7674c3SLukasz Luba 	ret = regulator_set_voltage(dmc->vdd_mif, target_volt,
4556e7674c3SLukasz Luba 				    target_volt);
4566e7674c3SLukasz Luba 	if (!ret)
4576e7674c3SLukasz Luba 		dmc->curr_volt = target_volt;
4586e7674c3SLukasz Luba 
4596e7674c3SLukasz Luba 	return ret;
4606e7674c3SLukasz Luba }
4616e7674c3SLukasz Luba 
4626e7674c3SLukasz Luba /**
4636e7674c3SLukasz Luba  * exynos5_dmc_align_bypass_dram_timings() - Chooses and sets DRAM timings
4646e7674c3SLukasz Luba  * @dmc:	device for which it is going to be set
4656e7674c3SLukasz Luba  * @target_rate:	new frequency which is chosen to be final
4666e7674c3SLukasz Luba  *
4676e7674c3SLukasz Luba  * Function changes the DRAM timings for the temporary 'bypass' mode.
4686e7674c3SLukasz Luba  */
4696e7674c3SLukasz Luba static int exynos5_dmc_align_bypass_dram_timings(struct exynos5_dmc *dmc,
4706e7674c3SLukasz Luba 						 unsigned long target_rate)
4716e7674c3SLukasz Luba {
4726e7674c3SLukasz Luba 	int idx = find_target_freq_idx(dmc, target_rate);
4736e7674c3SLukasz Luba 
4746e7674c3SLukasz Luba 	if (idx < 0)
4756e7674c3SLukasz Luba 		return -EINVAL;
4766e7674c3SLukasz Luba 
4776e7674c3SLukasz Luba 	exynos5_set_bypass_dram_timings(dmc);
4786e7674c3SLukasz Luba 
4796e7674c3SLukasz Luba 	return 0;
4806e7674c3SLukasz Luba }
4816e7674c3SLukasz Luba 
4826e7674c3SLukasz Luba /**
4836e7674c3SLukasz Luba  * exynos5_dmc_switch_to_bypass_configuration() - Switching to temporary clock
4846e7674c3SLukasz Luba  * @dmc:	DMC device for which the switching is going to happen
4856e7674c3SLukasz Luba  * @target_rate:	new frequency which is going to be set as a final
4866e7674c3SLukasz Luba  * @target_volt:	new voltage which is going to be set as a final
4876e7674c3SLukasz Luba  *
4886e7674c3SLukasz Luba  * Function configures DMC and clocks for operating in temporary 'bypass' mode.
4896e7674c3SLukasz Luba  * This mode is used only temporary but if required, changes voltage and timings
4906e7674c3SLukasz Luba  * for DRAM chips. It switches the main clock to stable clock source for the
4916e7674c3SLukasz Luba  * period of the main PLL reconfiguration.
4926e7674c3SLukasz Luba  */
4936e7674c3SLukasz Luba static int
4946e7674c3SLukasz Luba exynos5_dmc_switch_to_bypass_configuration(struct exynos5_dmc *dmc,
4956e7674c3SLukasz Luba 					   unsigned long target_rate,
4966e7674c3SLukasz Luba 					   unsigned long target_volt)
4976e7674c3SLukasz Luba {
4986e7674c3SLukasz Luba 	int ret;
4996e7674c3SLukasz Luba 
5006e7674c3SLukasz Luba 	/*
5016e7674c3SLukasz Luba 	 * Having higher voltage for a particular frequency does not harm
5026e7674c3SLukasz Luba 	 * the chip. Use it for the temporary frequency change when one
5036e7674c3SLukasz Luba 	 * voltage manipulation might be avoided.
5046e7674c3SLukasz Luba 	 */
5056e7674c3SLukasz Luba 	ret = exynos5_dmc_align_bypass_voltage(dmc, target_volt);
5066e7674c3SLukasz Luba 	if (ret)
5076e7674c3SLukasz Luba 		return ret;
5086e7674c3SLukasz Luba 
5096e7674c3SLukasz Luba 	/*
5106e7674c3SLukasz Luba 	 * Longer delays for DRAM does not cause crash, the opposite does.
5116e7674c3SLukasz Luba 	 */
5126e7674c3SLukasz Luba 	ret = exynos5_dmc_align_bypass_dram_timings(dmc, target_rate);
5136e7674c3SLukasz Luba 	if (ret)
5146e7674c3SLukasz Luba 		return ret;
5156e7674c3SLukasz Luba 
5166e7674c3SLukasz Luba 	/*
5176e7674c3SLukasz Luba 	 * Delays are long enough, so use them for the new coming clock.
5186e7674c3SLukasz Luba 	 */
5196e7674c3SLukasz Luba 	exynos5_switch_timing_regs(dmc, USE_MX_MSPLL_TIMINGS);
5206e7674c3SLukasz Luba 
5216e7674c3SLukasz Luba 	return ret;
5226e7674c3SLukasz Luba }
5236e7674c3SLukasz Luba 
5246e7674c3SLukasz Luba /**
5256e7674c3SLukasz Luba  * exynos5_dmc_change_freq_and_volt() - Changes voltage and frequency of the DMC
5266e7674c3SLukasz Luba  * using safe procedure
5276e7674c3SLukasz Luba  * @dmc:	device for which the frequency is going to be changed
5286e7674c3SLukasz Luba  * @target_rate:	requested new frequency
5296e7674c3SLukasz Luba  * @target_volt:	requested voltage which corresponds to the new frequency
5306e7674c3SLukasz Luba  *
5316e7674c3SLukasz Luba  * The DMC frequency change procedure requires a few steps.
5326e7674c3SLukasz Luba  * The main requirement is to change the clock source in the clk mux
5336e7674c3SLukasz Luba  * for the time of main clock PLL locking. The assumption is that the
5346e7674c3SLukasz Luba  * alternative clock source set as parent is stable.
5356e7674c3SLukasz Luba  * The second parent's clock frequency is fixed to 400MHz, it is named 'bypass'
5366e7674c3SLukasz Luba  * clock. This requires alignment in DRAM timing parameters for the new
5376e7674c3SLukasz Luba  * T-period. There is two bank sets for keeping DRAM
5386e7674c3SLukasz Luba  * timings: set 0 and set 1. The set 0 is used when main clock source is
5396e7674c3SLukasz Luba  * chosen. The 2nd set of regs is used for 'bypass' clock. Switching between
5406e7674c3SLukasz Luba  * the two bank sets is part of the process.
5416e7674c3SLukasz Luba  * The voltage must also be aligned to the minimum required level. There is
5426e7674c3SLukasz Luba  * this intermediate step with switching to 'bypass' parent clock source.
5436e7674c3SLukasz Luba  * if the old voltage is lower, it requires an increase of the voltage level.
5446e7674c3SLukasz Luba  * The complexity of the voltage manipulation is hidden in low level function.
5456e7674c3SLukasz Luba  * In this function there is last alignment of the voltage level at the end.
5466e7674c3SLukasz Luba  */
5476e7674c3SLukasz Luba static int
5486e7674c3SLukasz Luba exynos5_dmc_change_freq_and_volt(struct exynos5_dmc *dmc,
5496e7674c3SLukasz Luba 				 unsigned long target_rate,
5506e7674c3SLukasz Luba 				 unsigned long target_volt)
5516e7674c3SLukasz Luba {
5526e7674c3SLukasz Luba 	int ret;
5536e7674c3SLukasz Luba 
5546e7674c3SLukasz Luba 	ret = exynos5_dmc_switch_to_bypass_configuration(dmc, target_rate,
5556e7674c3SLukasz Luba 							 target_volt);
5566e7674c3SLukasz Luba 	if (ret)
5576e7674c3SLukasz Luba 		return ret;
5586e7674c3SLukasz Luba 
5596e7674c3SLukasz Luba 	/*
5606e7674c3SLukasz Luba 	 * Voltage is set at least to a level needed for this frequency,
5616e7674c3SLukasz Luba 	 * so switching clock source is safe now.
5626e7674c3SLukasz Luba 	 */
5636e7674c3SLukasz Luba 	clk_prepare_enable(dmc->fout_spll);
5646e7674c3SLukasz Luba 	clk_prepare_enable(dmc->mout_spll);
5656e7674c3SLukasz Luba 	clk_prepare_enable(dmc->mout_mx_mspll_ccore);
5666e7674c3SLukasz Luba 
5676e7674c3SLukasz Luba 	ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_mx_mspll_ccore);
5686e7674c3SLukasz Luba 	if (ret)
5696e7674c3SLukasz Luba 		goto disable_clocks;
5706e7674c3SLukasz Luba 
5716e7674c3SLukasz Luba 	/*
5726e7674c3SLukasz Luba 	 * We are safe to increase the timings for current bypass frequency.
5736e7674c3SLukasz Luba 	 * Thanks to this the settings will be ready for the upcoming clock
5746e7674c3SLukasz Luba 	 * source change.
5756e7674c3SLukasz Luba 	 */
5766e7674c3SLukasz Luba 	exynos5_dram_change_timings(dmc, target_rate);
5776e7674c3SLukasz Luba 
5786e7674c3SLukasz Luba 	clk_set_rate(dmc->fout_bpll, target_rate);
5796e7674c3SLukasz Luba 
5806e7674c3SLukasz Luba 	exynos5_switch_timing_regs(dmc, USE_BPLL_TIMINGS);
5816e7674c3SLukasz Luba 
5826e7674c3SLukasz Luba 	ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_bpll);
5836e7674c3SLukasz Luba 	if (ret)
5846e7674c3SLukasz Luba 		goto disable_clocks;
5856e7674c3SLukasz Luba 
5866e7674c3SLukasz Luba 	/*
5876e7674c3SLukasz Luba 	 * Make sure if the voltage is not from 'bypass' settings and align to
5886e7674c3SLukasz Luba 	 * the right level for power efficiency.
5896e7674c3SLukasz Luba 	 */
5906e7674c3SLukasz Luba 	ret = exynos5_dmc_align_target_voltage(dmc, target_volt);
5916e7674c3SLukasz Luba 
5926e7674c3SLukasz Luba disable_clocks:
5936e7674c3SLukasz Luba 	clk_disable_unprepare(dmc->mout_mx_mspll_ccore);
5946e7674c3SLukasz Luba 	clk_disable_unprepare(dmc->mout_spll);
5956e7674c3SLukasz Luba 	clk_disable_unprepare(dmc->fout_spll);
5966e7674c3SLukasz Luba 
5976e7674c3SLukasz Luba 	return ret;
5986e7674c3SLukasz Luba }
5996e7674c3SLukasz Luba 
6006e7674c3SLukasz Luba /**
6016e7674c3SLukasz Luba  * exynos5_dmc_get_volt_freq() - Gets the frequency and voltage from the OPP
6026e7674c3SLukasz Luba  * table.
6036e7674c3SLukasz Luba  * @dmc:	device for which the frequency is going to be changed
6046e7674c3SLukasz Luba  * @freq:       requested frequency in KHz
6056e7674c3SLukasz Luba  * @target_rate:	returned frequency which is the same or lower than
6066e7674c3SLukasz Luba  *			requested
6076e7674c3SLukasz Luba  * @target_volt:	returned voltage which corresponds to the returned
6086e7674c3SLukasz Luba  *			frequency
6096e7674c3SLukasz Luba  *
6106e7674c3SLukasz Luba  * Function gets requested frequency and checks OPP framework for needed
6116e7674c3SLukasz Luba  * frequency and voltage. It populates the values 'target_rate' and
6126e7674c3SLukasz Luba  * 'target_volt' or returns error value when OPP framework fails.
6136e7674c3SLukasz Luba  */
6146e7674c3SLukasz Luba static int exynos5_dmc_get_volt_freq(struct exynos5_dmc *dmc,
6156e7674c3SLukasz Luba 				     unsigned long *freq,
6166e7674c3SLukasz Luba 				     unsigned long *target_rate,
6176e7674c3SLukasz Luba 				     unsigned long *target_volt, u32 flags)
6186e7674c3SLukasz Luba {
6196e7674c3SLukasz Luba 	struct dev_pm_opp *opp;
6206e7674c3SLukasz Luba 
6216e7674c3SLukasz Luba 	opp = devfreq_recommended_opp(dmc->dev, freq, flags);
6226e7674c3SLukasz Luba 	if (IS_ERR(opp))
6236e7674c3SLukasz Luba 		return PTR_ERR(opp);
6246e7674c3SLukasz Luba 
6256e7674c3SLukasz Luba 	*target_rate = dev_pm_opp_get_freq(opp);
6266e7674c3SLukasz Luba 	*target_volt = dev_pm_opp_get_voltage(opp);
6276e7674c3SLukasz Luba 	dev_pm_opp_put(opp);
6286e7674c3SLukasz Luba 
6296e7674c3SLukasz Luba 	return 0;
6306e7674c3SLukasz Luba }
6316e7674c3SLukasz Luba 
6326e7674c3SLukasz Luba /**
6336e7674c3SLukasz Luba  * exynos5_dmc_target() - Function responsible for changing frequency of DMC
6346e7674c3SLukasz Luba  * @dev:	device for which the frequency is going to be changed
6356e7674c3SLukasz Luba  * @freq:	requested frequency in KHz
6366e7674c3SLukasz Luba  * @flags:	flags provided for this frequency change request
6376e7674c3SLukasz Luba  *
6386e7674c3SLukasz Luba  * An entry function provided to the devfreq framework which provides frequency
6396e7674c3SLukasz Luba  * change of the DMC. The function gets the possible rate from OPP table based
6406e7674c3SLukasz Luba  * on requested frequency. It calls the next function responsible for the
6416e7674c3SLukasz Luba  * frequency and voltage change. In case of failure, does not set 'curr_rate'
6426e7674c3SLukasz Luba  * and returns error value to the framework.
6436e7674c3SLukasz Luba  */
6446e7674c3SLukasz Luba static int exynos5_dmc_target(struct device *dev, unsigned long *freq,
6456e7674c3SLukasz Luba 			      u32 flags)
6466e7674c3SLukasz Luba {
6476e7674c3SLukasz Luba 	struct exynos5_dmc *dmc = dev_get_drvdata(dev);
6486e7674c3SLukasz Luba 	unsigned long target_rate = 0;
6496e7674c3SLukasz Luba 	unsigned long target_volt = 0;
6506e7674c3SLukasz Luba 	int ret;
6516e7674c3SLukasz Luba 
6526e7674c3SLukasz Luba 	ret = exynos5_dmc_get_volt_freq(dmc, freq, &target_rate, &target_volt,
6536e7674c3SLukasz Luba 					flags);
6546e7674c3SLukasz Luba 
6556e7674c3SLukasz Luba 	if (ret)
6566e7674c3SLukasz Luba 		return ret;
6576e7674c3SLukasz Luba 
6586e7674c3SLukasz Luba 	if (target_rate == dmc->curr_rate)
6596e7674c3SLukasz Luba 		return 0;
6606e7674c3SLukasz Luba 
6616e7674c3SLukasz Luba 	mutex_lock(&dmc->lock);
6626e7674c3SLukasz Luba 
6636e7674c3SLukasz Luba 	ret = exynos5_dmc_change_freq_and_volt(dmc, target_rate, target_volt);
6646e7674c3SLukasz Luba 
6656e7674c3SLukasz Luba 	if (ret) {
6666e7674c3SLukasz Luba 		mutex_unlock(&dmc->lock);
6676e7674c3SLukasz Luba 		return ret;
6686e7674c3SLukasz Luba 	}
6696e7674c3SLukasz Luba 
6706e7674c3SLukasz Luba 	dmc->curr_rate = target_rate;
6716e7674c3SLukasz Luba 
6726e7674c3SLukasz Luba 	mutex_unlock(&dmc->lock);
6736e7674c3SLukasz Luba 	return 0;
6746e7674c3SLukasz Luba }
6756e7674c3SLukasz Luba 
6766e7674c3SLukasz Luba /**
6776e7674c3SLukasz Luba  * exynos5_counters_get() - Gets the performance counters values.
6786e7674c3SLukasz Luba  * @dmc:	device for which the counters are going to be checked
6796e7674c3SLukasz Luba  * @load_count:	variable which is populated with counter value
6806e7674c3SLukasz Luba  * @total_count:	variable which is used as 'wall clock' reference
6816e7674c3SLukasz Luba  *
6826e7674c3SLukasz Luba  * Function which provides performance counters values. It sums up counters for
6836e7674c3SLukasz Luba  * two DMC channels. The 'total_count' is used as a reference and max value.
6846e7674c3SLukasz Luba  * The ratio 'load_count/total_count' shows the busy percentage [0%, 100%].
6856e7674c3SLukasz Luba  */
6866e7674c3SLukasz Luba static int exynos5_counters_get(struct exynos5_dmc *dmc,
6876e7674c3SLukasz Luba 				unsigned long *load_count,
6886e7674c3SLukasz Luba 				unsigned long *total_count)
6896e7674c3SLukasz Luba {
6906e7674c3SLukasz Luba 	unsigned long total = 0;
6916e7674c3SLukasz Luba 	struct devfreq_event_data event;
6926e7674c3SLukasz Luba 	int ret, i;
6936e7674c3SLukasz Luba 
6946e7674c3SLukasz Luba 	*load_count = 0;
6956e7674c3SLukasz Luba 
6966e7674c3SLukasz Luba 	/* Take into account only read+write counters, but stop all */
6976e7674c3SLukasz Luba 	for (i = 0; i < dmc->num_counters; i++) {
6986e7674c3SLukasz Luba 		if (!dmc->counter[i])
6996e7674c3SLukasz Luba 			continue;
7006e7674c3SLukasz Luba 
7016e7674c3SLukasz Luba 		ret = devfreq_event_get_event(dmc->counter[i], &event);
7026e7674c3SLukasz Luba 		if (ret < 0)
7036e7674c3SLukasz Luba 			return ret;
7046e7674c3SLukasz Luba 
7056e7674c3SLukasz Luba 		*load_count += event.load_count;
7066e7674c3SLukasz Luba 
7076e7674c3SLukasz Luba 		if (total < event.total_count)
7086e7674c3SLukasz Luba 			total = event.total_count;
7096e7674c3SLukasz Luba 	}
7106e7674c3SLukasz Luba 
7116e7674c3SLukasz Luba 	*total_count = total;
7126e7674c3SLukasz Luba 
7136e7674c3SLukasz Luba 	return 0;
7146e7674c3SLukasz Luba }
7156e7674c3SLukasz Luba 
7166e7674c3SLukasz Luba /**
717*bbf91886SLukasz Luba  * exynos5_dmc_start_perf_events() - Setup and start performance event counters
718*bbf91886SLukasz Luba  * @dmc:	device for which the counters are going to be checked
719*bbf91886SLukasz Luba  * @beg_value:	initial value for the counter
720*bbf91886SLukasz Luba  *
721*bbf91886SLukasz Luba  * Function which enables needed counters, interrupts and sets initial values
722*bbf91886SLukasz Luba  * then starts the counters.
723*bbf91886SLukasz Luba  */
724*bbf91886SLukasz Luba static void exynos5_dmc_start_perf_events(struct exynos5_dmc *dmc,
725*bbf91886SLukasz Luba 					  u32 beg_value)
726*bbf91886SLukasz Luba {
727*bbf91886SLukasz Luba 	/* Enable interrupts for counter 2 */
728*bbf91886SLukasz Luba 	writel(PERF_CNT2, dmc->base_drexi0 + DREX_INTENS_PPC);
729*bbf91886SLukasz Luba 	writel(PERF_CNT2, dmc->base_drexi1 + DREX_INTENS_PPC);
730*bbf91886SLukasz Luba 
731*bbf91886SLukasz Luba 	/* Enable counter 2 and CCNT  */
732*bbf91886SLukasz Luba 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_CNTENS_PPC);
733*bbf91886SLukasz Luba 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_CNTENS_PPC);
734*bbf91886SLukasz Luba 
735*bbf91886SLukasz Luba 	/* Clear overflow flag for all counters */
736*bbf91886SLukasz Luba 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_FLAG_PPC);
737*bbf91886SLukasz Luba 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_FLAG_PPC);
738*bbf91886SLukasz Luba 
739*bbf91886SLukasz Luba 	/* Reset all counters */
740*bbf91886SLukasz Luba 	writel(CC_RESET | PPC_COUNTER_RESET, dmc->base_drexi0 + DREX_PMNC_PPC);
741*bbf91886SLukasz Luba 	writel(CC_RESET | PPC_COUNTER_RESET, dmc->base_drexi1 + DREX_PMNC_PPC);
742*bbf91886SLukasz Luba 
743*bbf91886SLukasz Luba 	/*
744*bbf91886SLukasz Luba 	 * Set start value for the counters, the number of samples that
745*bbf91886SLukasz Luba 	 * will be gathered is calculated as: 0xffffffff - beg_value
746*bbf91886SLukasz Luba 	 */
747*bbf91886SLukasz Luba 	writel(beg_value, dmc->base_drexi0 + DREX_PMCNT2_PPC);
748*bbf91886SLukasz Luba 	writel(beg_value, dmc->base_drexi1 + DREX_PMCNT2_PPC);
749*bbf91886SLukasz Luba 
750*bbf91886SLukasz Luba 	/* Start all counters */
751*bbf91886SLukasz Luba 	writel(PPC_ENABLE, dmc->base_drexi0 + DREX_PMNC_PPC);
752*bbf91886SLukasz Luba 	writel(PPC_ENABLE, dmc->base_drexi1 + DREX_PMNC_PPC);
753*bbf91886SLukasz Luba }
754*bbf91886SLukasz Luba 
755*bbf91886SLukasz Luba /**
756*bbf91886SLukasz Luba  * exynos5_dmc_perf_events_calc() - Calculate utilization
757*bbf91886SLukasz Luba  * @dmc:	device for which the counters are going to be checked
758*bbf91886SLukasz Luba  * @diff_ts:	time between last interrupt and current one
759*bbf91886SLukasz Luba  *
760*bbf91886SLukasz Luba  * Function which calculates needed utilization for the devfreq governor.
761*bbf91886SLukasz Luba  * It prepares values for 'busy_time' and 'total_time' based on elapsed time
762*bbf91886SLukasz Luba  * between interrupts, which approximates utilization.
763*bbf91886SLukasz Luba  */
764*bbf91886SLukasz Luba static void exynos5_dmc_perf_events_calc(struct exynos5_dmc *dmc, u64 diff_ts)
765*bbf91886SLukasz Luba {
766*bbf91886SLukasz Luba 	/*
767*bbf91886SLukasz Luba 	 * This is a simple algorithm for managing traffic on DMC.
768*bbf91886SLukasz Luba 	 * When there is almost no load the counters overflow every 4s,
769*bbf91886SLukasz Luba 	 * no mater the DMC frequency.
770*bbf91886SLukasz Luba 	 * The high load might be approximated using linear function.
771*bbf91886SLukasz Luba 	 * Knowing that, simple calculation can provide 'busy_time' and
772*bbf91886SLukasz Luba 	 * 'total_time' to the devfreq governor which picks up target
773*bbf91886SLukasz Luba 	 * frequency.
774*bbf91886SLukasz Luba 	 * We want a fast ramp up and slow decay in frequency change function.
775*bbf91886SLukasz Luba 	 */
776*bbf91886SLukasz Luba 	if (diff_ts < PERF_EVENT_UP_DOWN_THRESHOLD) {
777*bbf91886SLukasz Luba 		/*
778*bbf91886SLukasz Luba 		 * Set higher utilization for the simple_ondemand governor.
779*bbf91886SLukasz Luba 		 * The governor should increase the frequency of the DMC.
780*bbf91886SLukasz Luba 		 */
781*bbf91886SLukasz Luba 		dmc->load = 70;
782*bbf91886SLukasz Luba 		dmc->total = 100;
783*bbf91886SLukasz Luba 	} else {
784*bbf91886SLukasz Luba 		/*
785*bbf91886SLukasz Luba 		 * Set low utilization for the simple_ondemand governor.
786*bbf91886SLukasz Luba 		 * The governor should decrease the frequency of the DMC.
787*bbf91886SLukasz Luba 		 */
788*bbf91886SLukasz Luba 		dmc->load = 35;
789*bbf91886SLukasz Luba 		dmc->total = 100;
790*bbf91886SLukasz Luba 	}
791*bbf91886SLukasz Luba 
792*bbf91886SLukasz Luba 	dev_dbg(dmc->dev, "diff_ts=%llu\n", diff_ts);
793*bbf91886SLukasz Luba }
794*bbf91886SLukasz Luba 
795*bbf91886SLukasz Luba /**
796*bbf91886SLukasz Luba  * exynos5_dmc_perf_events_check() - Checks the status of the counters
797*bbf91886SLukasz Luba  * @dmc:	device for which the counters are going to be checked
798*bbf91886SLukasz Luba  *
799*bbf91886SLukasz Luba  * Function which is called from threaded IRQ to check the counters state
800*bbf91886SLukasz Luba  * and to call approximation for the needed utilization.
801*bbf91886SLukasz Luba  */
802*bbf91886SLukasz Luba static void exynos5_dmc_perf_events_check(struct exynos5_dmc *dmc)
803*bbf91886SLukasz Luba {
804*bbf91886SLukasz Luba 	u32 val;
805*bbf91886SLukasz Luba 	u64 diff_ts, ts;
806*bbf91886SLukasz Luba 
807*bbf91886SLukasz Luba 	ts = ktime_get_ns();
808*bbf91886SLukasz Luba 
809*bbf91886SLukasz Luba 	/* Stop all counters */
810*bbf91886SLukasz Luba 	writel(0, dmc->base_drexi0 + DREX_PMNC_PPC);
811*bbf91886SLukasz Luba 	writel(0, dmc->base_drexi1 + DREX_PMNC_PPC);
812*bbf91886SLukasz Luba 
813*bbf91886SLukasz Luba 	/* Check the source in interrupt flag registers (which channel) */
814*bbf91886SLukasz Luba 	val = readl(dmc->base_drexi0 + DREX_FLAG_PPC);
815*bbf91886SLukasz Luba 	if (val) {
816*bbf91886SLukasz Luba 		diff_ts = ts - dmc->last_overflow_ts[0];
817*bbf91886SLukasz Luba 		dmc->last_overflow_ts[0] = ts;
818*bbf91886SLukasz Luba 		dev_dbg(dmc->dev, "drex0 0xE050 val= 0x%08x\n",  val);
819*bbf91886SLukasz Luba 	} else {
820*bbf91886SLukasz Luba 		val = readl(dmc->base_drexi1 + DREX_FLAG_PPC);
821*bbf91886SLukasz Luba 		diff_ts = ts - dmc->last_overflow_ts[1];
822*bbf91886SLukasz Luba 		dmc->last_overflow_ts[1] = ts;
823*bbf91886SLukasz Luba 		dev_dbg(dmc->dev, "drex1 0xE050 val= 0x%08x\n",  val);
824*bbf91886SLukasz Luba 	}
825*bbf91886SLukasz Luba 
826*bbf91886SLukasz Luba 	exynos5_dmc_perf_events_calc(dmc, diff_ts);
827*bbf91886SLukasz Luba 
828*bbf91886SLukasz Luba 	exynos5_dmc_start_perf_events(dmc, PERF_COUNTER_START_VALUE);
829*bbf91886SLukasz Luba }
830*bbf91886SLukasz Luba 
831*bbf91886SLukasz Luba /**
832*bbf91886SLukasz Luba  * exynos5_dmc_enable_perf_events() - Enable performance events
833*bbf91886SLukasz Luba  * @dmc:	device for which the counters are going to be checked
834*bbf91886SLukasz Luba  *
835*bbf91886SLukasz Luba  * Function which is setup needed environment and enables counters.
836*bbf91886SLukasz Luba  */
837*bbf91886SLukasz Luba static void exynos5_dmc_enable_perf_events(struct exynos5_dmc *dmc)
838*bbf91886SLukasz Luba {
839*bbf91886SLukasz Luba 	u64 ts;
840*bbf91886SLukasz Luba 
841*bbf91886SLukasz Luba 	/* Enable Performance Event Clock */
842*bbf91886SLukasz Luba 	writel(PEREV_CLK_EN, dmc->base_drexi0 + DREX_PPCCLKCON);
843*bbf91886SLukasz Luba 	writel(PEREV_CLK_EN, dmc->base_drexi1 + DREX_PPCCLKCON);
844*bbf91886SLukasz Luba 
845*bbf91886SLukasz Luba 	/* Select read transfers as performance event2 */
846*bbf91886SLukasz Luba 	writel(READ_TRANSFER_CH0, dmc->base_drexi0 + DREX_PEREV2CONFIG);
847*bbf91886SLukasz Luba 	writel(READ_TRANSFER_CH1, dmc->base_drexi1 + DREX_PEREV2CONFIG);
848*bbf91886SLukasz Luba 
849*bbf91886SLukasz Luba 	ts = ktime_get_ns();
850*bbf91886SLukasz Luba 	dmc->last_overflow_ts[0] = ts;
851*bbf91886SLukasz Luba 	dmc->last_overflow_ts[1] = ts;
852*bbf91886SLukasz Luba 
853*bbf91886SLukasz Luba 	/* Devfreq shouldn't be faster than initialization, play safe though. */
854*bbf91886SLukasz Luba 	dmc->load = 99;
855*bbf91886SLukasz Luba 	dmc->total = 100;
856*bbf91886SLukasz Luba }
857*bbf91886SLukasz Luba 
858*bbf91886SLukasz Luba /**
859*bbf91886SLukasz Luba  * exynos5_dmc_disable_perf_events() - Disable performance events
860*bbf91886SLukasz Luba  * @dmc:	device for which the counters are going to be checked
861*bbf91886SLukasz Luba  *
862*bbf91886SLukasz Luba  * Function which stops, disables performance event counters and interrupts.
863*bbf91886SLukasz Luba  */
864*bbf91886SLukasz Luba static void exynos5_dmc_disable_perf_events(struct exynos5_dmc *dmc)
865*bbf91886SLukasz Luba {
866*bbf91886SLukasz Luba 	/* Stop all counters */
867*bbf91886SLukasz Luba 	writel(0, dmc->base_drexi0 + DREX_PMNC_PPC);
868*bbf91886SLukasz Luba 	writel(0, dmc->base_drexi1 + DREX_PMNC_PPC);
869*bbf91886SLukasz Luba 
870*bbf91886SLukasz Luba 	/* Disable interrupts for counter 2 */
871*bbf91886SLukasz Luba 	writel(PERF_CNT2, dmc->base_drexi0 + DREX_INTENC_PPC);
872*bbf91886SLukasz Luba 	writel(PERF_CNT2, dmc->base_drexi1 + DREX_INTENC_PPC);
873*bbf91886SLukasz Luba 
874*bbf91886SLukasz Luba 	/* Disable counter 2 and CCNT  */
875*bbf91886SLukasz Luba 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_CNTENC_PPC);
876*bbf91886SLukasz Luba 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_CNTENC_PPC);
877*bbf91886SLukasz Luba 
878*bbf91886SLukasz Luba 	/* Clear overflow flag for all counters */
879*bbf91886SLukasz Luba 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_FLAG_PPC);
880*bbf91886SLukasz Luba 	writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_FLAG_PPC);
881*bbf91886SLukasz Luba }
882*bbf91886SLukasz Luba 
883*bbf91886SLukasz Luba /**
8846e7674c3SLukasz Luba  * exynos5_dmc_get_status() - Read current DMC performance statistics.
8856e7674c3SLukasz Luba  * @dev:	device for which the statistics are requested
8866e7674c3SLukasz Luba  * @stat:	structure which has statistic fields
8876e7674c3SLukasz Luba  *
8886e7674c3SLukasz Luba  * Function reads the DMC performance counters and calculates 'busy_time'
8896e7674c3SLukasz Luba  * and 'total_time'. To protect from overflow, the values are shifted right
8906e7674c3SLukasz Luba  * by 10. After read out the counters are setup to count again.
8916e7674c3SLukasz Luba  */
8926e7674c3SLukasz Luba static int exynos5_dmc_get_status(struct device *dev,
8936e7674c3SLukasz Luba 				  struct devfreq_dev_status *stat)
8946e7674c3SLukasz Luba {
8956e7674c3SLukasz Luba 	struct exynos5_dmc *dmc = dev_get_drvdata(dev);
8966e7674c3SLukasz Luba 	unsigned long load, total;
8976e7674c3SLukasz Luba 	int ret;
8986e7674c3SLukasz Luba 
899*bbf91886SLukasz Luba 	if (dmc->in_irq_mode) {
900*bbf91886SLukasz Luba 		stat->current_frequency = dmc->curr_rate;
901*bbf91886SLukasz Luba 		stat->busy_time = dmc->load;
902*bbf91886SLukasz Luba 		stat->total_time = dmc->total;
903*bbf91886SLukasz Luba 	} else {
9046e7674c3SLukasz Luba 		ret = exynos5_counters_get(dmc, &load, &total);
9056e7674c3SLukasz Luba 		if (ret < 0)
9066e7674c3SLukasz Luba 			return -EINVAL;
9076e7674c3SLukasz Luba 
908*bbf91886SLukasz Luba 		/* To protect from overflow, divide by 1024 */
9096e7674c3SLukasz Luba 		stat->busy_time = load >> 10;
9106e7674c3SLukasz Luba 		stat->total_time = total >> 10;
9116e7674c3SLukasz Luba 
9126e7674c3SLukasz Luba 		ret = exynos5_counters_set_event(dmc);
9136e7674c3SLukasz Luba 		if (ret < 0) {
9146e7674c3SLukasz Luba 			dev_err(dev, "could not set event counter\n");
9156e7674c3SLukasz Luba 			return ret;
9166e7674c3SLukasz Luba 		}
917*bbf91886SLukasz Luba 	}
9186e7674c3SLukasz Luba 
9196e7674c3SLukasz Luba 	return 0;
9206e7674c3SLukasz Luba }
9216e7674c3SLukasz Luba 
9226e7674c3SLukasz Luba /**
9236e7674c3SLukasz Luba  * exynos5_dmc_get_cur_freq() - Function returns current DMC frequency
9246e7674c3SLukasz Luba  * @dev:	device for which the framework checks operating frequency
9256e7674c3SLukasz Luba  * @freq:	returned frequency value
9266e7674c3SLukasz Luba  *
9276e7674c3SLukasz Luba  * It returns the currently used frequency of the DMC. The real operating
9286e7674c3SLukasz Luba  * frequency might be lower when the clock source value could not be divided
9296e7674c3SLukasz Luba  * to the requested value.
9306e7674c3SLukasz Luba  */
9316e7674c3SLukasz Luba static int exynos5_dmc_get_cur_freq(struct device *dev, unsigned long *freq)
9326e7674c3SLukasz Luba {
9336e7674c3SLukasz Luba 	struct exynos5_dmc *dmc = dev_get_drvdata(dev);
9346e7674c3SLukasz Luba 
9356e7674c3SLukasz Luba 	mutex_lock(&dmc->lock);
9366e7674c3SLukasz Luba 	*freq = dmc->curr_rate;
9376e7674c3SLukasz Luba 	mutex_unlock(&dmc->lock);
9386e7674c3SLukasz Luba 
9396e7674c3SLukasz Luba 	return 0;
9406e7674c3SLukasz Luba }
9416e7674c3SLukasz Luba 
9426e7674c3SLukasz Luba /**
9436e7674c3SLukasz Luba  * exynos5_dmc_df_profile - Devfreq governor's profile structure
9446e7674c3SLukasz Luba  *
9456e7674c3SLukasz Luba  * It provides to the devfreq framework needed functions and polling period.
9466e7674c3SLukasz Luba  */
9476e7674c3SLukasz Luba static struct devfreq_dev_profile exynos5_dmc_df_profile = {
9486e7674c3SLukasz Luba 	.target = exynos5_dmc_target,
9496e7674c3SLukasz Luba 	.get_dev_status = exynos5_dmc_get_status,
9506e7674c3SLukasz Luba 	.get_cur_freq = exynos5_dmc_get_cur_freq,
9516e7674c3SLukasz Luba };
9526e7674c3SLukasz Luba 
9536e7674c3SLukasz Luba /**
9546e7674c3SLukasz Luba  * exynos5_dmc_align_initial_frequency() - Align initial frequency value
9556e7674c3SLukasz Luba  * @dmc:	device for which the frequency is going to be set
9566e7674c3SLukasz Luba  * @bootloader_init_freq:	initial frequency set by the bootloader in KHz
9576e7674c3SLukasz Luba  *
9586e7674c3SLukasz Luba  * The initial bootloader frequency, which is present during boot, might be
9596e7674c3SLukasz Luba  * different that supported frequency values in the driver. It is possible
9606e7674c3SLukasz Luba  * due to different PLL settings or used PLL as a source.
9616e7674c3SLukasz Luba  * This function provides the 'initial_freq' for the devfreq framework
9626e7674c3SLukasz Luba  * statistics engine which supports only registered values. Thus, some alignment
9636e7674c3SLukasz Luba  * must be made.
9646e7674c3SLukasz Luba  */
965d51e6a69SLukasz Luba static unsigned long
9666e7674c3SLukasz Luba exynos5_dmc_align_init_freq(struct exynos5_dmc *dmc,
9676e7674c3SLukasz Luba 			    unsigned long bootloader_init_freq)
9686e7674c3SLukasz Luba {
9696e7674c3SLukasz Luba 	unsigned long aligned_freq;
9706e7674c3SLukasz Luba 	int idx;
9716e7674c3SLukasz Luba 
9726e7674c3SLukasz Luba 	idx = find_target_freq_idx(dmc, bootloader_init_freq);
9736e7674c3SLukasz Luba 	if (idx >= 0)
9746e7674c3SLukasz Luba 		aligned_freq = dmc->opp[idx].freq_hz;
9756e7674c3SLukasz Luba 	else
9766e7674c3SLukasz Luba 		aligned_freq = dmc->opp[dmc->opp_count - 1].freq_hz;
9776e7674c3SLukasz Luba 
9786e7674c3SLukasz Luba 	return aligned_freq;
9796e7674c3SLukasz Luba }
9806e7674c3SLukasz Luba 
9816e7674c3SLukasz Luba /**
9826e7674c3SLukasz Luba  * create_timings_aligned() - Create register values and align with standard
9836e7674c3SLukasz Luba  * @dmc:	device for which the frequency is going to be set
9846e7674c3SLukasz Luba  * @idx:	speed bin in the OPP table
9856e7674c3SLukasz Luba  * @clk_period_ps:	the period of the clock, known as tCK
9866e7674c3SLukasz Luba  *
9876e7674c3SLukasz Luba  * The function calculates timings and creates a register value ready for
9886e7674c3SLukasz Luba  * a frequency transition. The register contains a few timings. They are
9896e7674c3SLukasz Luba  * shifted by a known offset. The timing value is calculated based on memory
9906e7674c3SLukasz Luba  * specyfication: minimal time required and minimal cycles required.
9916e7674c3SLukasz Luba  */
9926e7674c3SLukasz Luba static int create_timings_aligned(struct exynos5_dmc *dmc, u32 *reg_timing_row,
9936e7674c3SLukasz Luba 				  u32 *reg_timing_data, u32 *reg_timing_power,
9946e7674c3SLukasz Luba 				  u32 clk_period_ps)
9956e7674c3SLukasz Luba {
9966e7674c3SLukasz Luba 	u32 val;
9976e7674c3SLukasz Luba 	const struct timing_reg *reg;
9986e7674c3SLukasz Luba 
9996e7674c3SLukasz Luba 	if (clk_period_ps == 0)
10006e7674c3SLukasz Luba 		return -EINVAL;
10016e7674c3SLukasz Luba 
10026e7674c3SLukasz Luba 	*reg_timing_row = 0;
10036e7674c3SLukasz Luba 	*reg_timing_data = 0;
10046e7674c3SLukasz Luba 	*reg_timing_power = 0;
10056e7674c3SLukasz Luba 
10066e7674c3SLukasz Luba 	val = dmc->timings->tRFC / clk_period_ps;
10076e7674c3SLukasz Luba 	val += dmc->timings->tRFC % clk_period_ps ? 1 : 0;
10086e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tRFC);
10096e7674c3SLukasz Luba 	reg = &timing_row[0];
10106e7674c3SLukasz Luba 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
10116e7674c3SLukasz Luba 
10126e7674c3SLukasz Luba 	val = dmc->timings->tRRD / clk_period_ps;
10136e7674c3SLukasz Luba 	val += dmc->timings->tRRD % clk_period_ps ? 1 : 0;
10146e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tRRD);
10156e7674c3SLukasz Luba 	reg = &timing_row[1];
10166e7674c3SLukasz Luba 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
10176e7674c3SLukasz Luba 
10186e7674c3SLukasz Luba 	val = dmc->timings->tRPab / clk_period_ps;
10196e7674c3SLukasz Luba 	val += dmc->timings->tRPab % clk_period_ps ? 1 : 0;
10206e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tRPab);
10216e7674c3SLukasz Luba 	reg = &timing_row[2];
10226e7674c3SLukasz Luba 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
10236e7674c3SLukasz Luba 
10246e7674c3SLukasz Luba 	val = dmc->timings->tRCD / clk_period_ps;
10256e7674c3SLukasz Luba 	val += dmc->timings->tRCD % clk_period_ps ? 1 : 0;
10266e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tRCD);
10276e7674c3SLukasz Luba 	reg = &timing_row[3];
10286e7674c3SLukasz Luba 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
10296e7674c3SLukasz Luba 
10306e7674c3SLukasz Luba 	val = dmc->timings->tRC / clk_period_ps;
10316e7674c3SLukasz Luba 	val += dmc->timings->tRC % clk_period_ps ? 1 : 0;
10326e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tRC);
10336e7674c3SLukasz Luba 	reg = &timing_row[4];
10346e7674c3SLukasz Luba 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
10356e7674c3SLukasz Luba 
10366e7674c3SLukasz Luba 	val = dmc->timings->tRAS / clk_period_ps;
10376e7674c3SLukasz Luba 	val += dmc->timings->tRAS % clk_period_ps ? 1 : 0;
10386e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tRAS);
10396e7674c3SLukasz Luba 	reg = &timing_row[5];
10406e7674c3SLukasz Luba 	*reg_timing_row |= TIMING_VAL2REG(reg, val);
10416e7674c3SLukasz Luba 
10426e7674c3SLukasz Luba 	/* data related timings */
10436e7674c3SLukasz Luba 	val = dmc->timings->tWTR / clk_period_ps;
10446e7674c3SLukasz Luba 	val += dmc->timings->tWTR % clk_period_ps ? 1 : 0;
10456e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tWTR);
10466e7674c3SLukasz Luba 	reg = &timing_data[0];
10476e7674c3SLukasz Luba 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
10486e7674c3SLukasz Luba 
10496e7674c3SLukasz Luba 	val = dmc->timings->tWR / clk_period_ps;
10506e7674c3SLukasz Luba 	val += dmc->timings->tWR % clk_period_ps ? 1 : 0;
10516e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tWR);
10526e7674c3SLukasz Luba 	reg = &timing_data[1];
10536e7674c3SLukasz Luba 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
10546e7674c3SLukasz Luba 
10556e7674c3SLukasz Luba 	val = dmc->timings->tRTP / clk_period_ps;
10566e7674c3SLukasz Luba 	val += dmc->timings->tRTP % clk_period_ps ? 1 : 0;
10576e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tRTP);
10586e7674c3SLukasz Luba 	reg = &timing_data[2];
10596e7674c3SLukasz Luba 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
10606e7674c3SLukasz Luba 
10616e7674c3SLukasz Luba 	val = dmc->timings->tW2W_C2C / clk_period_ps;
10626e7674c3SLukasz Luba 	val += dmc->timings->tW2W_C2C % clk_period_ps ? 1 : 0;
10636e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tW2W_C2C);
10646e7674c3SLukasz Luba 	reg = &timing_data[3];
10656e7674c3SLukasz Luba 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
10666e7674c3SLukasz Luba 
10676e7674c3SLukasz Luba 	val = dmc->timings->tR2R_C2C / clk_period_ps;
10686e7674c3SLukasz Luba 	val += dmc->timings->tR2R_C2C % clk_period_ps ? 1 : 0;
10696e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tR2R_C2C);
10706e7674c3SLukasz Luba 	reg = &timing_data[4];
10716e7674c3SLukasz Luba 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
10726e7674c3SLukasz Luba 
10736e7674c3SLukasz Luba 	val = dmc->timings->tWL / clk_period_ps;
10746e7674c3SLukasz Luba 	val += dmc->timings->tWL % clk_period_ps ? 1 : 0;
10756e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tWL);
10766e7674c3SLukasz Luba 	reg = &timing_data[5];
10776e7674c3SLukasz Luba 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
10786e7674c3SLukasz Luba 
10796e7674c3SLukasz Luba 	val = dmc->timings->tDQSCK / clk_period_ps;
10806e7674c3SLukasz Luba 	val += dmc->timings->tDQSCK % clk_period_ps ? 1 : 0;
10816e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tDQSCK);
10826e7674c3SLukasz Luba 	reg = &timing_data[6];
10836e7674c3SLukasz Luba 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
10846e7674c3SLukasz Luba 
10856e7674c3SLukasz Luba 	val = dmc->timings->tRL / clk_period_ps;
10866e7674c3SLukasz Luba 	val += dmc->timings->tRL % clk_period_ps ? 1 : 0;
10876e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tRL);
10886e7674c3SLukasz Luba 	reg = &timing_data[7];
10896e7674c3SLukasz Luba 	*reg_timing_data |= TIMING_VAL2REG(reg, val);
10906e7674c3SLukasz Luba 
10916e7674c3SLukasz Luba 	/* power related timings */
10926e7674c3SLukasz Luba 	val = dmc->timings->tFAW / clk_period_ps;
10936e7674c3SLukasz Luba 	val += dmc->timings->tFAW % clk_period_ps ? 1 : 0;
10946e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tXP);
10956e7674c3SLukasz Luba 	reg = &timing_power[0];
10966e7674c3SLukasz Luba 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
10976e7674c3SLukasz Luba 
10986e7674c3SLukasz Luba 	val = dmc->timings->tXSR / clk_period_ps;
10996e7674c3SLukasz Luba 	val += dmc->timings->tXSR % clk_period_ps ? 1 : 0;
11006e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tXSR);
11016e7674c3SLukasz Luba 	reg = &timing_power[1];
11026e7674c3SLukasz Luba 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
11036e7674c3SLukasz Luba 
11046e7674c3SLukasz Luba 	val = dmc->timings->tXP / clk_period_ps;
11056e7674c3SLukasz Luba 	val += dmc->timings->tXP % clk_period_ps ? 1 : 0;
11066e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tXP);
11076e7674c3SLukasz Luba 	reg = &timing_power[2];
11086e7674c3SLukasz Luba 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
11096e7674c3SLukasz Luba 
11106e7674c3SLukasz Luba 	val = dmc->timings->tCKE / clk_period_ps;
11116e7674c3SLukasz Luba 	val += dmc->timings->tCKE % clk_period_ps ? 1 : 0;
11126e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tCKE);
11136e7674c3SLukasz Luba 	reg = &timing_power[3];
11146e7674c3SLukasz Luba 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
11156e7674c3SLukasz Luba 
11166e7674c3SLukasz Luba 	val = dmc->timings->tMRD / clk_period_ps;
11176e7674c3SLukasz Luba 	val += dmc->timings->tMRD % clk_period_ps ? 1 : 0;
11186e7674c3SLukasz Luba 	val = max(val, dmc->min_tck->tMRD);
11196e7674c3SLukasz Luba 	reg = &timing_power[4];
11206e7674c3SLukasz Luba 	*reg_timing_power |= TIMING_VAL2REG(reg, val);
11216e7674c3SLukasz Luba 
11226e7674c3SLukasz Luba 	return 0;
11236e7674c3SLukasz Luba }
11246e7674c3SLukasz Luba 
11256e7674c3SLukasz Luba /**
11266e7674c3SLukasz Luba  * of_get_dram_timings() - helper function for parsing DT settings for DRAM
11276e7674c3SLukasz Luba  * @dmc:        device for which the frequency is going to be set
11286e7674c3SLukasz Luba  *
11296e7674c3SLukasz Luba  * The function parses DT entries with DRAM information.
11306e7674c3SLukasz Luba  */
11316e7674c3SLukasz Luba static int of_get_dram_timings(struct exynos5_dmc *dmc)
11326e7674c3SLukasz Luba {
11336e7674c3SLukasz Luba 	int ret = 0;
11346e7674c3SLukasz Luba 	int idx;
11356e7674c3SLukasz Luba 	struct device_node *np_ddr;
11366e7674c3SLukasz Luba 	u32 freq_mhz, clk_period_ps;
11376e7674c3SLukasz Luba 
11386e7674c3SLukasz Luba 	np_ddr = of_parse_phandle(dmc->dev->of_node, "device-handle", 0);
11396e7674c3SLukasz Luba 	if (!np_ddr) {
11406e7674c3SLukasz Luba 		dev_warn(dmc->dev, "could not find 'device-handle' in DT\n");
11416e7674c3SLukasz Luba 		return -EINVAL;
11426e7674c3SLukasz Luba 	}
11436e7674c3SLukasz Luba 
11446e7674c3SLukasz Luba 	dmc->timing_row = devm_kmalloc_array(dmc->dev, TIMING_COUNT,
11456e7674c3SLukasz Luba 					     sizeof(u32), GFP_KERNEL);
11466e7674c3SLukasz Luba 	if (!dmc->timing_row)
11476e7674c3SLukasz Luba 		return -ENOMEM;
11486e7674c3SLukasz Luba 
11496e7674c3SLukasz Luba 	dmc->timing_data = devm_kmalloc_array(dmc->dev, TIMING_COUNT,
11506e7674c3SLukasz Luba 					      sizeof(u32), GFP_KERNEL);
11516e7674c3SLukasz Luba 	if (!dmc->timing_data)
11526e7674c3SLukasz Luba 		return -ENOMEM;
11536e7674c3SLukasz Luba 
11546e7674c3SLukasz Luba 	dmc->timing_power = devm_kmalloc_array(dmc->dev, TIMING_COUNT,
11556e7674c3SLukasz Luba 					       sizeof(u32), GFP_KERNEL);
11566e7674c3SLukasz Luba 	if (!dmc->timing_power)
11576e7674c3SLukasz Luba 		return -ENOMEM;
11586e7674c3SLukasz Luba 
11596e7674c3SLukasz Luba 	dmc->timings = of_lpddr3_get_ddr_timings(np_ddr, dmc->dev,
11606e7674c3SLukasz Luba 						 DDR_TYPE_LPDDR3,
11616e7674c3SLukasz Luba 						 &dmc->timings_arr_size);
11626e7674c3SLukasz Luba 	if (!dmc->timings) {
11636e7674c3SLukasz Luba 		of_node_put(np_ddr);
11646e7674c3SLukasz Luba 		dev_warn(dmc->dev, "could not get timings from DT\n");
11656e7674c3SLukasz Luba 		return -EINVAL;
11666e7674c3SLukasz Luba 	}
11676e7674c3SLukasz Luba 
11686e7674c3SLukasz Luba 	dmc->min_tck = of_lpddr3_get_min_tck(np_ddr, dmc->dev);
11696e7674c3SLukasz Luba 	if (!dmc->min_tck) {
11706e7674c3SLukasz Luba 		of_node_put(np_ddr);
11716e7674c3SLukasz Luba 		dev_warn(dmc->dev, "could not get tck from DT\n");
11726e7674c3SLukasz Luba 		return -EINVAL;
11736e7674c3SLukasz Luba 	}
11746e7674c3SLukasz Luba 
11756e7674c3SLukasz Luba 	/* Sorted array of OPPs with frequency ascending */
11766e7674c3SLukasz Luba 	for (idx = 0; idx < dmc->opp_count; idx++) {
11776e7674c3SLukasz Luba 		freq_mhz = dmc->opp[idx].freq_hz / 1000000;
11786e7674c3SLukasz Luba 		clk_period_ps = 1000000 / freq_mhz;
11796e7674c3SLukasz Luba 
11806e7674c3SLukasz Luba 		ret = create_timings_aligned(dmc, &dmc->timing_row[idx],
11816e7674c3SLukasz Luba 					     &dmc->timing_data[idx],
11826e7674c3SLukasz Luba 					     &dmc->timing_power[idx],
11836e7674c3SLukasz Luba 					     clk_period_ps);
11846e7674c3SLukasz Luba 	}
11856e7674c3SLukasz Luba 
11866e7674c3SLukasz Luba 	of_node_put(np_ddr);
11876e7674c3SLukasz Luba 
11886e7674c3SLukasz Luba 	/* Take the highest frequency's timings as 'bypass' */
11896e7674c3SLukasz Luba 	dmc->bypass_timing_row = dmc->timing_row[idx - 1];
11906e7674c3SLukasz Luba 	dmc->bypass_timing_data = dmc->timing_data[idx - 1];
11916e7674c3SLukasz Luba 	dmc->bypass_timing_power = dmc->timing_power[idx - 1];
11926e7674c3SLukasz Luba 
11936e7674c3SLukasz Luba 	return ret;
11946e7674c3SLukasz Luba }
11956e7674c3SLukasz Luba 
11966e7674c3SLukasz Luba /**
11976e7674c3SLukasz Luba  * exynos5_dmc_init_clks() - Initialize clocks needed for DMC operation.
11986e7674c3SLukasz Luba  * @dmc:	DMC structure containing needed fields
11996e7674c3SLukasz Luba  *
12006e7674c3SLukasz Luba  * Get the needed clocks defined in DT device, enable and set the right parents.
12016e7674c3SLukasz Luba  * Read current frequency and initialize the initial rate for governor.
12026e7674c3SLukasz Luba  */
12036e7674c3SLukasz Luba static int exynos5_dmc_init_clks(struct exynos5_dmc *dmc)
12046e7674c3SLukasz Luba {
12056e7674c3SLukasz Luba 	int ret;
12066e7674c3SLukasz Luba 	unsigned long target_volt = 0;
12076e7674c3SLukasz Luba 	unsigned long target_rate = 0;
12086e7674c3SLukasz Luba 	unsigned int tmp;
12096e7674c3SLukasz Luba 
12106e7674c3SLukasz Luba 	dmc->fout_spll = devm_clk_get(dmc->dev, "fout_spll");
12116e7674c3SLukasz Luba 	if (IS_ERR(dmc->fout_spll))
12126e7674c3SLukasz Luba 		return PTR_ERR(dmc->fout_spll);
12136e7674c3SLukasz Luba 
12146e7674c3SLukasz Luba 	dmc->fout_bpll = devm_clk_get(dmc->dev, "fout_bpll");
12156e7674c3SLukasz Luba 	if (IS_ERR(dmc->fout_bpll))
12166e7674c3SLukasz Luba 		return PTR_ERR(dmc->fout_bpll);
12176e7674c3SLukasz Luba 
12186e7674c3SLukasz Luba 	dmc->mout_mclk_cdrex = devm_clk_get(dmc->dev, "mout_mclk_cdrex");
12196e7674c3SLukasz Luba 	if (IS_ERR(dmc->mout_mclk_cdrex))
12206e7674c3SLukasz Luba 		return PTR_ERR(dmc->mout_mclk_cdrex);
12216e7674c3SLukasz Luba 
12226e7674c3SLukasz Luba 	dmc->mout_bpll = devm_clk_get(dmc->dev, "mout_bpll");
12236e7674c3SLukasz Luba 	if (IS_ERR(dmc->mout_bpll))
12246e7674c3SLukasz Luba 		return PTR_ERR(dmc->mout_bpll);
12256e7674c3SLukasz Luba 
12266e7674c3SLukasz Luba 	dmc->mout_mx_mspll_ccore = devm_clk_get(dmc->dev,
12276e7674c3SLukasz Luba 						"mout_mx_mspll_ccore");
12286e7674c3SLukasz Luba 	if (IS_ERR(dmc->mout_mx_mspll_ccore))
12296e7674c3SLukasz Luba 		return PTR_ERR(dmc->mout_mx_mspll_ccore);
12306e7674c3SLukasz Luba 
12316e7674c3SLukasz Luba 	dmc->mout_spll = devm_clk_get(dmc->dev, "ff_dout_spll2");
12326e7674c3SLukasz Luba 	if (IS_ERR(dmc->mout_spll)) {
12336e7674c3SLukasz Luba 		dmc->mout_spll = devm_clk_get(dmc->dev, "mout_sclk_spll");
12346e7674c3SLukasz Luba 		if (IS_ERR(dmc->mout_spll))
12356e7674c3SLukasz Luba 			return PTR_ERR(dmc->mout_spll);
12366e7674c3SLukasz Luba 	}
12376e7674c3SLukasz Luba 
12386e7674c3SLukasz Luba 	/*
12396e7674c3SLukasz Luba 	 * Convert frequency to KHz values and set it for the governor.
12406e7674c3SLukasz Luba 	 */
12416e7674c3SLukasz Luba 	dmc->curr_rate = clk_get_rate(dmc->mout_mclk_cdrex);
12426e7674c3SLukasz Luba 	dmc->curr_rate = exynos5_dmc_align_init_freq(dmc, dmc->curr_rate);
12436e7674c3SLukasz Luba 	exynos5_dmc_df_profile.initial_freq = dmc->curr_rate;
12446e7674c3SLukasz Luba 
12456e7674c3SLukasz Luba 	ret = exynos5_dmc_get_volt_freq(dmc, &dmc->curr_rate, &target_rate,
12466e7674c3SLukasz Luba 					&target_volt, 0);
12476e7674c3SLukasz Luba 	if (ret)
12486e7674c3SLukasz Luba 		return ret;
12496e7674c3SLukasz Luba 
12506e7674c3SLukasz Luba 	dmc->curr_volt = target_volt;
12516e7674c3SLukasz Luba 
12526e7674c3SLukasz Luba 	clk_set_parent(dmc->mout_mx_mspll_ccore, dmc->mout_spll);
12536e7674c3SLukasz Luba 
12546e7674c3SLukasz Luba 	dmc->bypass_rate = clk_get_rate(dmc->mout_mx_mspll_ccore);
12556e7674c3SLukasz Luba 
12566e7674c3SLukasz Luba 	clk_prepare_enable(dmc->fout_bpll);
12576e7674c3SLukasz Luba 	clk_prepare_enable(dmc->mout_bpll);
12586e7674c3SLukasz Luba 
12596e7674c3SLukasz Luba 	/*
12606e7674c3SLukasz Luba 	 * Some bootloaders do not set clock routes correctly.
12616e7674c3SLukasz Luba 	 * Stop one path in clocks to PHY.
12626e7674c3SLukasz Luba 	 */
12636e7674c3SLukasz Luba 	regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, &tmp);
12646e7674c3SLukasz Luba 	tmp &= ~(BIT(1) | BIT(0));
12656e7674c3SLukasz Luba 	regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, tmp);
12666e7674c3SLukasz Luba 
12676e7674c3SLukasz Luba 	return 0;
12686e7674c3SLukasz Luba }
12696e7674c3SLukasz Luba 
12706e7674c3SLukasz Luba /**
12716e7674c3SLukasz Luba  * exynos5_performance_counters_init() - Initializes performance DMC's counters
12726e7674c3SLukasz Luba  * @dmc:	DMC for which it does the setup
12736e7674c3SLukasz Luba  *
12746e7674c3SLukasz Luba  * Initialization of performance counters in DMC for estimating usage.
12756e7674c3SLukasz Luba  * The counter's values are used for calculation of a memory bandwidth and based
12766e7674c3SLukasz Luba  * on that the governor changes the frequency.
12776e7674c3SLukasz Luba  * The counters are not used when the governor is GOVERNOR_USERSPACE.
12786e7674c3SLukasz Luba  */
12796e7674c3SLukasz Luba static int exynos5_performance_counters_init(struct exynos5_dmc *dmc)
12806e7674c3SLukasz Luba {
12816e7674c3SLukasz Luba 	int counters_size;
12826e7674c3SLukasz Luba 	int ret, i;
12836e7674c3SLukasz Luba 
12846e7674c3SLukasz Luba 	dmc->num_counters = devfreq_event_get_edev_count(dmc->dev);
12856e7674c3SLukasz Luba 	if (dmc->num_counters < 0) {
12866e7674c3SLukasz Luba 		dev_err(dmc->dev, "could not get devfreq-event counters\n");
12876e7674c3SLukasz Luba 		return dmc->num_counters;
12886e7674c3SLukasz Luba 	}
12896e7674c3SLukasz Luba 
12906e7674c3SLukasz Luba 	counters_size = sizeof(struct devfreq_event_dev) * dmc->num_counters;
12916e7674c3SLukasz Luba 	dmc->counter = devm_kzalloc(dmc->dev, counters_size, GFP_KERNEL);
12926e7674c3SLukasz Luba 	if (!dmc->counter)
12936e7674c3SLukasz Luba 		return -ENOMEM;
12946e7674c3SLukasz Luba 
12956e7674c3SLukasz Luba 	for (i = 0; i < dmc->num_counters; i++) {
12966e7674c3SLukasz Luba 		dmc->counter[i] =
12976e7674c3SLukasz Luba 			devfreq_event_get_edev_by_phandle(dmc->dev, i);
12986e7674c3SLukasz Luba 		if (IS_ERR_OR_NULL(dmc->counter[i]))
12996e7674c3SLukasz Luba 			return -EPROBE_DEFER;
13006e7674c3SLukasz Luba 	}
13016e7674c3SLukasz Luba 
13026e7674c3SLukasz Luba 	ret = exynos5_counters_enable_edev(dmc);
13036e7674c3SLukasz Luba 	if (ret < 0) {
13046e7674c3SLukasz Luba 		dev_err(dmc->dev, "could not enable event counter\n");
13056e7674c3SLukasz Luba 		return ret;
13066e7674c3SLukasz Luba 	}
13076e7674c3SLukasz Luba 
13086e7674c3SLukasz Luba 	ret = exynos5_counters_set_event(dmc);
13096e7674c3SLukasz Luba 	if (ret < 0) {
13106e7674c3SLukasz Luba 		exynos5_counters_disable_edev(dmc);
13117a5a687eSColin Ian King 		dev_err(dmc->dev, "could not set event counter\n");
13126e7674c3SLukasz Luba 		return ret;
13136e7674c3SLukasz Luba 	}
13146e7674c3SLukasz Luba 
13156e7674c3SLukasz Luba 	return 0;
13166e7674c3SLukasz Luba }
13176e7674c3SLukasz Luba 
13186e7674c3SLukasz Luba /**
13196e7674c3SLukasz Luba  * exynos5_dmc_set_pause_on_switching() - Controls a pause feature in DMC
13206e7674c3SLukasz Luba  * @dmc:	device which is used for changing this feature
13216e7674c3SLukasz Luba  * @set:	a boolean state passing enable/disable request
13226e7674c3SLukasz Luba  *
13236e7674c3SLukasz Luba  * There is a need of pausing DREX DMC when divider or MUX in clock tree
13246e7674c3SLukasz Luba  * changes its configuration. In such situation access to the memory is blocked
13256e7674c3SLukasz Luba  * in DMC automatically. This feature is used when clock frequency change
13266e7674c3SLukasz Luba  * request appears and touches clock tree.
13276e7674c3SLukasz Luba  */
13286e7674c3SLukasz Luba static inline int exynos5_dmc_set_pause_on_switching(struct exynos5_dmc *dmc)
13296e7674c3SLukasz Luba {
13306e7674c3SLukasz Luba 	unsigned int val;
13316e7674c3SLukasz Luba 	int ret;
13326e7674c3SLukasz Luba 
13336e7674c3SLukasz Luba 	ret = regmap_read(dmc->clk_regmap, CDREX_PAUSE, &val);
13346e7674c3SLukasz Luba 	if (ret)
13356e7674c3SLukasz Luba 		return ret;
13366e7674c3SLukasz Luba 
13376e7674c3SLukasz Luba 	val |= 1UL;
13386e7674c3SLukasz Luba 	regmap_write(dmc->clk_regmap, CDREX_PAUSE, val);
13396e7674c3SLukasz Luba 
13406e7674c3SLukasz Luba 	return 0;
13416e7674c3SLukasz Luba }
13426e7674c3SLukasz Luba 
1343*bbf91886SLukasz Luba static irqreturn_t dmc_irq_thread(int irq, void *priv)
1344*bbf91886SLukasz Luba {
1345*bbf91886SLukasz Luba 	int res;
1346*bbf91886SLukasz Luba 	struct exynos5_dmc *dmc = priv;
1347*bbf91886SLukasz Luba 
1348*bbf91886SLukasz Luba 	mutex_lock(&dmc->df->lock);
1349*bbf91886SLukasz Luba 
1350*bbf91886SLukasz Luba 	exynos5_dmc_perf_events_check(dmc);
1351*bbf91886SLukasz Luba 
1352*bbf91886SLukasz Luba 	res = update_devfreq(dmc->df);
1353*bbf91886SLukasz Luba 	if (res)
1354*bbf91886SLukasz Luba 		dev_warn(dmc->dev, "devfreq failed with %d\n", res);
1355*bbf91886SLukasz Luba 
1356*bbf91886SLukasz Luba 	mutex_unlock(&dmc->df->lock);
1357*bbf91886SLukasz Luba 
1358*bbf91886SLukasz Luba 	return IRQ_HANDLED;
1359*bbf91886SLukasz Luba }
1360*bbf91886SLukasz Luba 
13616e7674c3SLukasz Luba /**
13626e7674c3SLukasz Luba  * exynos5_dmc_probe() - Probe function for the DMC driver
13636e7674c3SLukasz Luba  * @pdev:	platform device for which the driver is going to be initialized
13646e7674c3SLukasz Luba  *
13656e7674c3SLukasz Luba  * Initialize basic components: clocks, regulators, performance counters, etc.
13666e7674c3SLukasz Luba  * Read out product version and based on the information setup
13676e7674c3SLukasz Luba  * internal structures for the controller (frequency and voltage) and for DRAM
13686e7674c3SLukasz Luba  * memory parameters: timings for each operating frequency.
13696e7674c3SLukasz Luba  * Register new devfreq device for controlling DVFS of the DMC.
13706e7674c3SLukasz Luba  */
13716e7674c3SLukasz Luba static int exynos5_dmc_probe(struct platform_device *pdev)
13726e7674c3SLukasz Luba {
13736e7674c3SLukasz Luba 	int ret = 0;
13746e7674c3SLukasz Luba 	struct device *dev = &pdev->dev;
13756e7674c3SLukasz Luba 	struct device_node *np = dev->of_node;
13766e7674c3SLukasz Luba 	struct exynos5_dmc *dmc;
13776e7674c3SLukasz Luba 	struct resource *res;
1378*bbf91886SLukasz Luba 	int irq[2];
13796e7674c3SLukasz Luba 
13806e7674c3SLukasz Luba 	dmc = devm_kzalloc(dev, sizeof(*dmc), GFP_KERNEL);
13816e7674c3SLukasz Luba 	if (!dmc)
13826e7674c3SLukasz Luba 		return -ENOMEM;
13836e7674c3SLukasz Luba 
13846e7674c3SLukasz Luba 	mutex_init(&dmc->lock);
13856e7674c3SLukasz Luba 
13866e7674c3SLukasz Luba 	dmc->dev = dev;
13876e7674c3SLukasz Luba 	platform_set_drvdata(pdev, dmc);
13886e7674c3SLukasz Luba 
13896e7674c3SLukasz Luba 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
13906e7674c3SLukasz Luba 	dmc->base_drexi0 = devm_ioremap_resource(dev, res);
13916e7674c3SLukasz Luba 	if (IS_ERR(dmc->base_drexi0))
13926e7674c3SLukasz Luba 		return PTR_ERR(dmc->base_drexi0);
13936e7674c3SLukasz Luba 
13946e7674c3SLukasz Luba 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
13956e7674c3SLukasz Luba 	dmc->base_drexi1 = devm_ioremap_resource(dev, res);
13966e7674c3SLukasz Luba 	if (IS_ERR(dmc->base_drexi1))
13976e7674c3SLukasz Luba 		return PTR_ERR(dmc->base_drexi1);
13986e7674c3SLukasz Luba 
13996e7674c3SLukasz Luba 	dmc->clk_regmap = syscon_regmap_lookup_by_phandle(np,
14006e7674c3SLukasz Luba 				"samsung,syscon-clk");
14016e7674c3SLukasz Luba 	if (IS_ERR(dmc->clk_regmap))
14026e7674c3SLukasz Luba 		return PTR_ERR(dmc->clk_regmap);
14036e7674c3SLukasz Luba 
14046e7674c3SLukasz Luba 	ret = exynos5_init_freq_table(dmc, &exynos5_dmc_df_profile);
14056e7674c3SLukasz Luba 	if (ret) {
14066e7674c3SLukasz Luba 		dev_warn(dev, "couldn't initialize frequency settings\n");
14076e7674c3SLukasz Luba 		return ret;
14086e7674c3SLukasz Luba 	}
14096e7674c3SLukasz Luba 
14106e7674c3SLukasz Luba 	dmc->vdd_mif = devm_regulator_get(dev, "vdd");
14116e7674c3SLukasz Luba 	if (IS_ERR(dmc->vdd_mif)) {
14126e7674c3SLukasz Luba 		ret = PTR_ERR(dmc->vdd_mif);
14136e7674c3SLukasz Luba 		return ret;
14146e7674c3SLukasz Luba 	}
14156e7674c3SLukasz Luba 
14166e7674c3SLukasz Luba 	ret = exynos5_dmc_init_clks(dmc);
14176e7674c3SLukasz Luba 	if (ret)
14186e7674c3SLukasz Luba 		return ret;
14196e7674c3SLukasz Luba 
14206e7674c3SLukasz Luba 	ret = of_get_dram_timings(dmc);
14216e7674c3SLukasz Luba 	if (ret) {
14226e7674c3SLukasz Luba 		dev_warn(dev, "couldn't initialize timings settings\n");
14236e7674c3SLukasz Luba 		goto remove_clocks;
14246e7674c3SLukasz Luba 	}
14256e7674c3SLukasz Luba 
1426*bbf91886SLukasz Luba 	ret = exynos5_dmc_set_pause_on_switching(dmc);
1427*bbf91886SLukasz Luba 	if (ret) {
1428*bbf91886SLukasz Luba 		dev_warn(dev, "couldn't get access to PAUSE register\n");
1429*bbf91886SLukasz Luba 		goto remove_clocks;
1430*bbf91886SLukasz Luba 	}
1431*bbf91886SLukasz Luba 
1432*bbf91886SLukasz Luba 	/* There is two modes in which the driver works: polling or IRQ */
1433*bbf91886SLukasz Luba 	irq[0] = platform_get_irq_byname(pdev, "drex_0");
1434*bbf91886SLukasz Luba 	irq[1] = platform_get_irq_byname(pdev, "drex_1");
1435*bbf91886SLukasz Luba 	if (irq[0] > 0 && irq[1] > 0) {
1436*bbf91886SLukasz Luba 		ret = devm_request_threaded_irq(dev, irq[0], NULL,
1437*bbf91886SLukasz Luba 						dmc_irq_thread, IRQF_ONESHOT,
1438*bbf91886SLukasz Luba 						dev_name(dev), dmc);
1439*bbf91886SLukasz Luba 		if (ret) {
1440*bbf91886SLukasz Luba 			dev_err(dev, "couldn't grab IRQ\n");
1441*bbf91886SLukasz Luba 			goto remove_clocks;
1442*bbf91886SLukasz Luba 		}
1443*bbf91886SLukasz Luba 
1444*bbf91886SLukasz Luba 		ret = devm_request_threaded_irq(dev, irq[1], NULL,
1445*bbf91886SLukasz Luba 						dmc_irq_thread, IRQF_ONESHOT,
1446*bbf91886SLukasz Luba 						dev_name(dev), dmc);
1447*bbf91886SLukasz Luba 		if (ret) {
1448*bbf91886SLukasz Luba 			dev_err(dev, "couldn't grab IRQ\n");
1449*bbf91886SLukasz Luba 			goto remove_clocks;
1450*bbf91886SLukasz Luba 		}
1451*bbf91886SLukasz Luba 
1452*bbf91886SLukasz Luba 		/*
1453*bbf91886SLukasz Luba 		 * Setup default thresholds for the devfreq governor.
1454*bbf91886SLukasz Luba 		 * The values are chosen based on experiments.
1455*bbf91886SLukasz Luba 		 */
1456*bbf91886SLukasz Luba 		dmc->gov_data.upthreshold = 55;
1457*bbf91886SLukasz Luba 		dmc->gov_data.downdifferential = 5;
1458*bbf91886SLukasz Luba 
1459*bbf91886SLukasz Luba 		exynos5_dmc_enable_perf_events(dmc);
1460*bbf91886SLukasz Luba 
1461*bbf91886SLukasz Luba 		dmc->in_irq_mode = 1;
1462*bbf91886SLukasz Luba 	} else {
14636e7674c3SLukasz Luba 		ret = exynos5_performance_counters_init(dmc);
14646e7674c3SLukasz Luba 		if (ret) {
14656e7674c3SLukasz Luba 			dev_warn(dev, "couldn't probe performance counters\n");
14666e7674c3SLukasz Luba 			goto remove_clocks;
14676e7674c3SLukasz Luba 		}
14686e7674c3SLukasz Luba 
14696e7674c3SLukasz Luba 		/*
14706e7674c3SLukasz Luba 		 * Setup default thresholds for the devfreq governor.
14716e7674c3SLukasz Luba 		 * The values are chosen based on experiments.
14726e7674c3SLukasz Luba 		 */
14736e7674c3SLukasz Luba 		dmc->gov_data.upthreshold = 30;
14746e7674c3SLukasz Luba 		dmc->gov_data.downdifferential = 5;
14756e7674c3SLukasz Luba 
1476*bbf91886SLukasz Luba 		exynos5_dmc_df_profile.polling_ms = 500;
1477*bbf91886SLukasz Luba 	}
1478*bbf91886SLukasz Luba 
1479*bbf91886SLukasz Luba 
14806e7674c3SLukasz Luba 	dmc->df = devm_devfreq_add_device(dev, &exynos5_dmc_df_profile,
14816e7674c3SLukasz Luba 					  DEVFREQ_GOV_SIMPLE_ONDEMAND,
14826e7674c3SLukasz Luba 					  &dmc->gov_data);
14836e7674c3SLukasz Luba 
14846e7674c3SLukasz Luba 	if (IS_ERR(dmc->df)) {
14856e7674c3SLukasz Luba 		ret = PTR_ERR(dmc->df);
14866e7674c3SLukasz Luba 		goto err_devfreq_add;
14876e7674c3SLukasz Luba 	}
14886e7674c3SLukasz Luba 
1489*bbf91886SLukasz Luba 	if (dmc->in_irq_mode)
1490*bbf91886SLukasz Luba 		exynos5_dmc_start_perf_events(dmc, PERF_COUNTER_START_VALUE);
1491*bbf91886SLukasz Luba 
14926e7674c3SLukasz Luba 	dev_info(dev, "DMC initialized\n");
14936e7674c3SLukasz Luba 
14946e7674c3SLukasz Luba 	return 0;
14956e7674c3SLukasz Luba 
14966e7674c3SLukasz Luba err_devfreq_add:
1497*bbf91886SLukasz Luba 	if (dmc->in_irq_mode)
1498*bbf91886SLukasz Luba 		exynos5_dmc_disable_perf_events(dmc);
1499*bbf91886SLukasz Luba 	else
15006e7674c3SLukasz Luba 		exynos5_counters_disable_edev(dmc);
15016e7674c3SLukasz Luba remove_clocks:
15026e7674c3SLukasz Luba 	clk_disable_unprepare(dmc->mout_bpll);
15036e7674c3SLukasz Luba 	clk_disable_unprepare(dmc->fout_bpll);
15046e7674c3SLukasz Luba 
15056e7674c3SLukasz Luba 	return ret;
15066e7674c3SLukasz Luba }
15076e7674c3SLukasz Luba 
15086e7674c3SLukasz Luba /**
15096e7674c3SLukasz Luba  * exynos5_dmc_remove() - Remove function for the platform device
15106e7674c3SLukasz Luba  * @pdev:	platform device which is going to be removed
15116e7674c3SLukasz Luba  *
15126e7674c3SLukasz Luba  * The function relies on 'devm' framework function which automatically
15136e7674c3SLukasz Luba  * clean the device's resources. It just calls explicitly disable function for
15146e7674c3SLukasz Luba  * the performance counters.
15156e7674c3SLukasz Luba  */
15166e7674c3SLukasz Luba static int exynos5_dmc_remove(struct platform_device *pdev)
15176e7674c3SLukasz Luba {
15186e7674c3SLukasz Luba 	struct exynos5_dmc *dmc = dev_get_drvdata(&pdev->dev);
15196e7674c3SLukasz Luba 
1520*bbf91886SLukasz Luba 	if (dmc->in_irq_mode)
1521*bbf91886SLukasz Luba 		exynos5_dmc_disable_perf_events(dmc);
1522*bbf91886SLukasz Luba 	else
15236e7674c3SLukasz Luba 		exynos5_counters_disable_edev(dmc);
15246e7674c3SLukasz Luba 
15256e7674c3SLukasz Luba 	clk_disable_unprepare(dmc->mout_bpll);
15266e7674c3SLukasz Luba 	clk_disable_unprepare(dmc->fout_bpll);
15276e7674c3SLukasz Luba 
15286e7674c3SLukasz Luba 	dev_pm_opp_remove_table(dmc->dev);
15296e7674c3SLukasz Luba 
15306e7674c3SLukasz Luba 	return 0;
15316e7674c3SLukasz Luba }
15326e7674c3SLukasz Luba 
15336e7674c3SLukasz Luba static const struct of_device_id exynos5_dmc_of_match[] = {
15346e7674c3SLukasz Luba 	{ .compatible = "samsung,exynos5422-dmc", },
15356e7674c3SLukasz Luba 	{ },
15366e7674c3SLukasz Luba };
15376e7674c3SLukasz Luba MODULE_DEVICE_TABLE(of, exynos5_dmc_of_match);
15386e7674c3SLukasz Luba 
15396e7674c3SLukasz Luba static struct platform_driver exynos5_dmc_platdrv = {
15406e7674c3SLukasz Luba 	.probe	= exynos5_dmc_probe,
15416e7674c3SLukasz Luba 	.remove = exynos5_dmc_remove,
15426e7674c3SLukasz Luba 	.driver = {
15436e7674c3SLukasz Luba 		.name	= "exynos5-dmc",
15446e7674c3SLukasz Luba 		.of_match_table = exynos5_dmc_of_match,
15456e7674c3SLukasz Luba 	},
15466e7674c3SLukasz Luba };
15476e7674c3SLukasz Luba module_platform_driver(exynos5_dmc_platdrv);
15486e7674c3SLukasz Luba MODULE_DESCRIPTION("Driver for Exynos5422 Dynamic Memory Controller dynamic frequency and voltage change");
15496e7674c3SLukasz Luba MODULE_LICENSE("GPL v2");
15506e7674c3SLukasz Luba MODULE_AUTHOR("Lukasz Luba");
1551