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
7*e2cc3ddaSKrzysztof Kozlowski #include <linux/cleanup.h>
86e7674c3SLukasz Luba #include <linux/clk.h>
96e7674c3SLukasz Luba #include <linux/devfreq.h>
106e7674c3SLukasz Luba #include <linux/devfreq-event.h>
116e7674c3SLukasz Luba #include <linux/device.h>
12bbf91886SLukasz Luba #include <linux/interrupt.h>
136e7674c3SLukasz Luba #include <linux/io.h>
146e7674c3SLukasz Luba #include <linux/mfd/syscon.h>
156e7674c3SLukasz Luba #include <linux/module.h>
164fc9a047SLukasz Luba #include <linux/moduleparam.h>
170b483871SRob Herring #include <linux/of.h>
186e7674c3SLukasz Luba #include <linux/pm_opp.h>
196e7674c3SLukasz Luba #include <linux/platform_device.h>
206e7674c3SLukasz Luba #include <linux/regmap.h>
216e7674c3SLukasz Luba #include <linux/regulator/consumer.h>
226e7674c3SLukasz Luba #include <linux/slab.h>
236e7674c3SLukasz Luba #include "../jedec_ddr.h"
246e7674c3SLukasz Luba #include "../of_memory.h"
256e7674c3SLukasz Luba
264fc9a047SLukasz Luba static int irqmode;
274fc9a047SLukasz Luba module_param(irqmode, int, 0644);
284fc9a047SLukasz Luba MODULE_PARM_DESC(irqmode, "Enable IRQ mode (0=off [default], 1=on)");
294fc9a047SLukasz Luba
306e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGAREF (0x0030)
316e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGROW0 (0x0034)
326e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGDATA0 (0x0038)
336e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGPOWER0 (0x003C)
346e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGROW1 (0x00E4)
356e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGDATA1 (0x00E8)
366e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGPOWER1 (0x00EC)
376e7674c3SLukasz Luba #define CDREX_PAUSE (0x2091c)
386e7674c3SLukasz Luba #define CDREX_LPDDR3PHY_CON3 (0x20a20)
396e7674c3SLukasz Luba #define CDREX_LPDDR3PHY_CLKM_SRC (0x20700)
406e7674c3SLukasz Luba #define EXYNOS5_TIMING_SET_SWI BIT(28)
416e7674c3SLukasz Luba #define USE_MX_MSPLL_TIMINGS (1)
426e7674c3SLukasz Luba #define USE_BPLL_TIMINGS (0)
436e7674c3SLukasz Luba #define EXYNOS5_AREF_NORMAL (0x2e)
446e7674c3SLukasz Luba
45bbf91886SLukasz Luba #define DREX_PPCCLKCON (0x0130)
46bbf91886SLukasz Luba #define DREX_PEREV2CONFIG (0x013c)
47bbf91886SLukasz Luba #define DREX_PMNC_PPC (0xE000)
48bbf91886SLukasz Luba #define DREX_CNTENS_PPC (0xE010)
49bbf91886SLukasz Luba #define DREX_CNTENC_PPC (0xE020)
50bbf91886SLukasz Luba #define DREX_INTENS_PPC (0xE030)
51bbf91886SLukasz Luba #define DREX_INTENC_PPC (0xE040)
52bbf91886SLukasz Luba #define DREX_FLAG_PPC (0xE050)
53bbf91886SLukasz Luba #define DREX_PMCNT2_PPC (0xE130)
54bbf91886SLukasz Luba
55bbf91886SLukasz Luba /*
56bbf91886SLukasz Luba * A value for register DREX_PMNC_PPC which should be written to reset
57bbf91886SLukasz Luba * the cycle counter CCNT (a reference wall clock). It sets zero to the
58bbf91886SLukasz Luba * CCNT counter.
59bbf91886SLukasz Luba */
60bbf91886SLukasz Luba #define CC_RESET BIT(2)
61bbf91886SLukasz Luba
62bbf91886SLukasz Luba /*
63bbf91886SLukasz Luba * A value for register DREX_PMNC_PPC which does the reset of all performance
64bbf91886SLukasz Luba * counters to zero.
65bbf91886SLukasz Luba */
66bbf91886SLukasz Luba #define PPC_COUNTER_RESET BIT(1)
67bbf91886SLukasz Luba
68bbf91886SLukasz Luba /*
69bbf91886SLukasz Luba * Enables all configured counters (including cycle counter). The value should
70bbf91886SLukasz Luba * be written to the register DREX_PMNC_PPC.
71bbf91886SLukasz Luba */
72bbf91886SLukasz Luba #define PPC_ENABLE BIT(0)
73bbf91886SLukasz Luba
74bbf91886SLukasz Luba /* A value for register DREX_PPCCLKCON which enables performance events clock.
75bbf91886SLukasz Luba * Must be written before first access to the performance counters register
76bbf91886SLukasz Luba * set, otherwise it could crash.
77bbf91886SLukasz Luba */
78bbf91886SLukasz Luba #define PEREV_CLK_EN BIT(0)
79bbf91886SLukasz Luba
80bbf91886SLukasz Luba /*
81bbf91886SLukasz Luba * Values which are used to enable counters, interrupts or configure flags of
82bbf91886SLukasz Luba * the performance counters. They configure counter 2 and cycle counter.
83bbf91886SLukasz Luba */
84bbf91886SLukasz Luba #define PERF_CNT2 BIT(2)
85bbf91886SLukasz Luba #define PERF_CCNT BIT(31)
86bbf91886SLukasz Luba
87bbf91886SLukasz Luba /*
88bbf91886SLukasz Luba * Performance event types which are used for setting the preferred event
89bbf91886SLukasz Luba * to track in the counters.
90bbf91886SLukasz Luba * There is a set of different types, the values are from range 0 to 0x6f.
91bbf91886SLukasz Luba * These settings should be written to the configuration register which manages
92bbf91886SLukasz Luba * the type of the event (register DREX_PEREV2CONFIG).
93bbf91886SLukasz Luba */
94bbf91886SLukasz Luba #define READ_TRANSFER_CH0 (0x6d)
95bbf91886SLukasz Luba #define READ_TRANSFER_CH1 (0x6f)
96bbf91886SLukasz Luba
97bbf91886SLukasz Luba #define PERF_COUNTER_START_VALUE 0xff000000
98bbf91886SLukasz Luba #define PERF_EVENT_UP_DOWN_THRESHOLD 900000000ULL
99bbf91886SLukasz Luba
1006e7674c3SLukasz Luba /**
1016e7674c3SLukasz Luba * struct dmc_opp_table - Operating level desciption
1024c2af5ddSKrzysztof Kozlowski * @freq_hz: target frequency in Hz
1034c2af5ddSKrzysztof Kozlowski * @volt_uv: target voltage in uV
1046e7674c3SLukasz Luba *
1056e7674c3SLukasz Luba * Covers frequency and voltage settings of the DMC operating mode.
1066e7674c3SLukasz Luba */
1076e7674c3SLukasz Luba struct dmc_opp_table {
1086e7674c3SLukasz Luba u32 freq_hz;
1096e7674c3SLukasz Luba u32 volt_uv;
1106e7674c3SLukasz Luba };
1116e7674c3SLukasz Luba
1126e7674c3SLukasz Luba /**
1136e7674c3SLukasz Luba * struct exynos5_dmc - main structure describing DMC device
1144c2af5ddSKrzysztof Kozlowski * @dev: DMC device
1154c2af5ddSKrzysztof Kozlowski * @df: devfreq device structure returned by devfreq framework
1164c2af5ddSKrzysztof Kozlowski * @gov_data: configuration of devfreq governor
1174c2af5ddSKrzysztof Kozlowski * @base_drexi0: DREX0 registers mapping
1184c2af5ddSKrzysztof Kozlowski * @base_drexi1: DREX1 registers mapping
1194c2af5ddSKrzysztof Kozlowski * @clk_regmap: regmap for clock controller registers
1204c2af5ddSKrzysztof Kozlowski * @lock: protects curr_rate and frequency/voltage setting section
1214c2af5ddSKrzysztof Kozlowski * @curr_rate: current frequency
1224c2af5ddSKrzysztof Kozlowski * @curr_volt: current voltage
1234c2af5ddSKrzysztof Kozlowski * @opp: OPP table
1244c2af5ddSKrzysztof Kozlowski * @opp_count: number of 'opp' elements
1254c2af5ddSKrzysztof Kozlowski * @timings_arr_size: number of 'timings' elements
1264c2af5ddSKrzysztof Kozlowski * @timing_row: values for timing row register, for each OPP
1274c2af5ddSKrzysztof Kozlowski * @timing_data: values for timing data register, for each OPP
1284c2af5ddSKrzysztof Kozlowski * @timing_power: balues for timing power register, for each OPP
1294c2af5ddSKrzysztof Kozlowski * @timings: DDR memory timings, from device tree
1304c2af5ddSKrzysztof Kozlowski * @min_tck: DDR memory minimum timing values, from device tree
1314c2af5ddSKrzysztof Kozlowski * @bypass_timing_row: value for timing row register for bypass timings
1324c2af5ddSKrzysztof Kozlowski * @bypass_timing_data: value for timing data register for bypass timings
1334c2af5ddSKrzysztof Kozlowski * @bypass_timing_power: value for timing power register for bypass
1344c2af5ddSKrzysztof Kozlowski * timings
1354c2af5ddSKrzysztof Kozlowski * @vdd_mif: Memory interface regulator
1364c2af5ddSKrzysztof Kozlowski * @fout_spll: clock: SPLL
1374c2af5ddSKrzysztof Kozlowski * @fout_bpll: clock: BPLL
1384c2af5ddSKrzysztof Kozlowski * @mout_spll: clock: mux SPLL
1394c2af5ddSKrzysztof Kozlowski * @mout_bpll: clock: mux BPLL
1404c2af5ddSKrzysztof Kozlowski * @mout_mclk_cdrex: clock: mux mclk_cdrex
1414c2af5ddSKrzysztof Kozlowski * @mout_mx_mspll_ccore: clock: mux mx_mspll_ccore
1424c2af5ddSKrzysztof Kozlowski * @counter: devfreq events
1434c2af5ddSKrzysztof Kozlowski * @num_counters: number of 'counter' elements
1444c2af5ddSKrzysztof Kozlowski * @last_overflow_ts: time (in ns) of last overflow of each DREX
1454c2af5ddSKrzysztof Kozlowski * @load: utilization in percents
1464c2af5ddSKrzysztof Kozlowski * @total: total time between devfreq events
1474c2af5ddSKrzysztof Kozlowski * @in_irq_mode: whether running in interrupt mode (true)
1484c2af5ddSKrzysztof Kozlowski * or polling (false)
1496e7674c3SLukasz Luba *
1506e7674c3SLukasz Luba * The main structure for the Dynamic Memory Controller which covers clocks,
1516e7674c3SLukasz Luba * memory regions, HW information, parameters and current operating mode.
1526e7674c3SLukasz Luba */
1536e7674c3SLukasz Luba struct exynos5_dmc {
1546e7674c3SLukasz Luba struct device *dev;
1556e7674c3SLukasz Luba struct devfreq *df;
1566e7674c3SLukasz Luba struct devfreq_simple_ondemand_data gov_data;
1576e7674c3SLukasz Luba void __iomem *base_drexi0;
1586e7674c3SLukasz Luba void __iomem *base_drexi1;
1596e7674c3SLukasz Luba struct regmap *clk_regmap;
160911c94daSKrzysztof Kozlowski /* Protects curr_rate and frequency/voltage setting section */
1616e7674c3SLukasz Luba struct mutex lock;
1626e7674c3SLukasz Luba unsigned long curr_rate;
1636e7674c3SLukasz Luba unsigned long curr_volt;
1646e7674c3SLukasz Luba struct dmc_opp_table *opp;
1656e7674c3SLukasz Luba int opp_count;
1666e7674c3SLukasz Luba u32 timings_arr_size;
1676e7674c3SLukasz Luba u32 *timing_row;
1686e7674c3SLukasz Luba u32 *timing_data;
1696e7674c3SLukasz Luba u32 *timing_power;
1706e7674c3SLukasz Luba const struct lpddr3_timings *timings;
1716e7674c3SLukasz Luba const struct lpddr3_min_tck *min_tck;
1726e7674c3SLukasz Luba u32 bypass_timing_row;
1736e7674c3SLukasz Luba u32 bypass_timing_data;
1746e7674c3SLukasz Luba u32 bypass_timing_power;
1756e7674c3SLukasz Luba struct regulator *vdd_mif;
1766e7674c3SLukasz Luba struct clk *fout_spll;
1776e7674c3SLukasz Luba struct clk *fout_bpll;
1786e7674c3SLukasz Luba struct clk *mout_spll;
1796e7674c3SLukasz Luba struct clk *mout_bpll;
1806e7674c3SLukasz Luba struct clk *mout_mclk_cdrex;
1816e7674c3SLukasz Luba struct clk *mout_mx_mspll_ccore;
1826e7674c3SLukasz Luba struct devfreq_event_dev **counter;
1836e7674c3SLukasz Luba int num_counters;
184bbf91886SLukasz Luba u64 last_overflow_ts[2];
185bbf91886SLukasz Luba unsigned long load;
186bbf91886SLukasz Luba unsigned long total;
187bbf91886SLukasz Luba bool in_irq_mode;
1886e7674c3SLukasz Luba };
1896e7674c3SLukasz Luba
1906e7674c3SLukasz Luba #define TIMING_FIELD(t_name, t_bit_beg, t_bit_end) \
1916e7674c3SLukasz Luba { .name = t_name, .bit_beg = t_bit_beg, .bit_end = t_bit_end }
1926e7674c3SLukasz Luba
1936e7674c3SLukasz Luba #define TIMING_VAL2REG(timing, t_val) \
1946e7674c3SLukasz Luba ({ \
1956e7674c3SLukasz Luba u32 __val; \
1966e7674c3SLukasz Luba __val = (t_val) << (timing)->bit_beg; \
1976e7674c3SLukasz Luba __val; \
1986e7674c3SLukasz Luba })
1996e7674c3SLukasz Luba
2006e7674c3SLukasz Luba struct timing_reg {
2016e7674c3SLukasz Luba char *name;
2026e7674c3SLukasz Luba int bit_beg;
2036e7674c3SLukasz Luba int bit_end;
2046e7674c3SLukasz Luba unsigned int val;
2056e7674c3SLukasz Luba };
2066e7674c3SLukasz Luba
207bb0ebc7dSKrzysztof Kozlowski static const struct timing_reg timing_row_reg_fields[] = {
2086e7674c3SLukasz Luba TIMING_FIELD("tRFC", 24, 31),
2096e7674c3SLukasz Luba TIMING_FIELD("tRRD", 20, 23),
2106e7674c3SLukasz Luba TIMING_FIELD("tRP", 16, 19),
2116e7674c3SLukasz Luba TIMING_FIELD("tRCD", 12, 15),
2126e7674c3SLukasz Luba TIMING_FIELD("tRC", 6, 11),
2136e7674c3SLukasz Luba TIMING_FIELD("tRAS", 0, 5),
2146e7674c3SLukasz Luba };
2156e7674c3SLukasz Luba
216bb0ebc7dSKrzysztof Kozlowski static const struct timing_reg timing_data_reg_fields[] = {
2176e7674c3SLukasz Luba TIMING_FIELD("tWTR", 28, 31),
2186e7674c3SLukasz Luba TIMING_FIELD("tWR", 24, 27),
2196e7674c3SLukasz Luba TIMING_FIELD("tRTP", 20, 23),
2206e7674c3SLukasz Luba TIMING_FIELD("tW2W-C2C", 14, 14),
2216e7674c3SLukasz Luba TIMING_FIELD("tR2R-C2C", 12, 12),
2226e7674c3SLukasz Luba TIMING_FIELD("WL", 8, 11),
2236e7674c3SLukasz Luba TIMING_FIELD("tDQSCK", 4, 7),
2246e7674c3SLukasz Luba TIMING_FIELD("RL", 0, 3),
2256e7674c3SLukasz Luba };
2266e7674c3SLukasz Luba
227bb0ebc7dSKrzysztof Kozlowski static const struct timing_reg timing_power_reg_fields[] = {
2286e7674c3SLukasz Luba TIMING_FIELD("tFAW", 26, 31),
2296e7674c3SLukasz Luba TIMING_FIELD("tXSR", 16, 25),
2306e7674c3SLukasz Luba TIMING_FIELD("tXP", 8, 15),
2316e7674c3SLukasz Luba TIMING_FIELD("tCKE", 4, 7),
2326e7674c3SLukasz Luba TIMING_FIELD("tMRD", 0, 3),
2336e7674c3SLukasz Luba };
2346e7674c3SLukasz Luba
235bb0ebc7dSKrzysztof Kozlowski #define TIMING_COUNT (ARRAY_SIZE(timing_row_reg_fields) + \
236bb0ebc7dSKrzysztof Kozlowski ARRAY_SIZE(timing_data_reg_fields) + \
237bb0ebc7dSKrzysztof Kozlowski ARRAY_SIZE(timing_power_reg_fields))
2386e7674c3SLukasz Luba
exynos5_counters_set_event(struct exynos5_dmc * dmc)2396e7674c3SLukasz Luba static int exynos5_counters_set_event(struct exynos5_dmc *dmc)
2406e7674c3SLukasz Luba {
2416e7674c3SLukasz Luba int i, ret;
2426e7674c3SLukasz Luba
2436e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) {
2446e7674c3SLukasz Luba if (!dmc->counter[i])
2456e7674c3SLukasz Luba continue;
2466e7674c3SLukasz Luba ret = devfreq_event_set_event(dmc->counter[i]);
2476e7674c3SLukasz Luba if (ret < 0)
2486e7674c3SLukasz Luba return ret;
2496e7674c3SLukasz Luba }
2506e7674c3SLukasz Luba return 0;
2516e7674c3SLukasz Luba }
2526e7674c3SLukasz Luba
exynos5_counters_enable_edev(struct exynos5_dmc * dmc)2536e7674c3SLukasz Luba static int exynos5_counters_enable_edev(struct exynos5_dmc *dmc)
2546e7674c3SLukasz Luba {
2556e7674c3SLukasz Luba int i, ret;
2566e7674c3SLukasz Luba
2576e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) {
2586e7674c3SLukasz Luba if (!dmc->counter[i])
2596e7674c3SLukasz Luba continue;
2606e7674c3SLukasz Luba ret = devfreq_event_enable_edev(dmc->counter[i]);
2616e7674c3SLukasz Luba if (ret < 0)
2626e7674c3SLukasz Luba return ret;
2636e7674c3SLukasz Luba }
2646e7674c3SLukasz Luba return 0;
2656e7674c3SLukasz Luba }
2666e7674c3SLukasz Luba
exynos5_counters_disable_edev(struct exynos5_dmc * dmc)2676e7674c3SLukasz Luba static int exynos5_counters_disable_edev(struct exynos5_dmc *dmc)
2686e7674c3SLukasz Luba {
2696e7674c3SLukasz Luba int i, ret;
2706e7674c3SLukasz Luba
2716e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) {
2726e7674c3SLukasz Luba if (!dmc->counter[i])
2736e7674c3SLukasz Luba continue;
2746e7674c3SLukasz Luba ret = devfreq_event_disable_edev(dmc->counter[i]);
2756e7674c3SLukasz Luba if (ret < 0)
2766e7674c3SLukasz Luba return ret;
2776e7674c3SLukasz Luba }
2786e7674c3SLukasz Luba return 0;
2796e7674c3SLukasz Luba }
2806e7674c3SLukasz Luba
2816e7674c3SLukasz Luba /**
2820e9bc420SKrzysztof Kozlowski * find_target_freq_idx() - Finds requested frequency in local DMC configuration
2836e7674c3SLukasz Luba * @dmc: device for which the information is checked
2846e7674c3SLukasz Luba * @target_rate: requested frequency in KHz
2856e7674c3SLukasz Luba *
2866e7674c3SLukasz Luba * Seeks in the local DMC driver structure for the requested frequency value
2876e7674c3SLukasz Luba * and returns index or error value.
2886e7674c3SLukasz Luba */
find_target_freq_idx(struct exynos5_dmc * dmc,unsigned long target_rate)2896e7674c3SLukasz Luba static int find_target_freq_idx(struct exynos5_dmc *dmc,
2906e7674c3SLukasz Luba unsigned long target_rate)
2916e7674c3SLukasz Luba {
2926e7674c3SLukasz Luba int i;
2936e7674c3SLukasz Luba
2946e7674c3SLukasz Luba for (i = dmc->opp_count - 1; i >= 0; i--)
2956e7674c3SLukasz Luba if (dmc->opp[i].freq_hz <= target_rate)
2966e7674c3SLukasz Luba return i;
2976e7674c3SLukasz Luba
2986e7674c3SLukasz Luba return -EINVAL;
2996e7674c3SLukasz Luba }
3006e7674c3SLukasz Luba
3016e7674c3SLukasz Luba /**
3026e7674c3SLukasz Luba * exynos5_switch_timing_regs() - Changes bank register set for DRAM timings
3036e7674c3SLukasz Luba * @dmc: device for which the new settings is going to be applied
3046e7674c3SLukasz Luba * @set: boolean variable passing set value
3056e7674c3SLukasz Luba *
3066e7674c3SLukasz Luba * Changes the register set, which holds timing parameters.
3076e7674c3SLukasz Luba * There is two register sets: 0 and 1. The register set 0
3086e7674c3SLukasz Luba * is used in normal operation when the clock is provided from main PLL.
3096e7674c3SLukasz Luba * The bank register set 1 is used when the main PLL frequency is going to be
3106e7674c3SLukasz Luba * changed and the clock is taken from alternative, stable source.
3116e7674c3SLukasz Luba * This function switches between these banks according to the
3126e7674c3SLukasz Luba * currently used clock source.
3136e7674c3SLukasz Luba */
exynos5_switch_timing_regs(struct exynos5_dmc * dmc,bool set)314c4f16e96SKrzysztof Kozlowski static int exynos5_switch_timing_regs(struct exynos5_dmc *dmc, bool set)
3156e7674c3SLukasz Luba {
3166e7674c3SLukasz Luba unsigned int reg;
3176e7674c3SLukasz Luba int ret;
3186e7674c3SLukasz Luba
3196e7674c3SLukasz Luba ret = regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, ®);
320c4f16e96SKrzysztof Kozlowski if (ret)
321c4f16e96SKrzysztof Kozlowski return ret;
3226e7674c3SLukasz Luba
3236e7674c3SLukasz Luba if (set)
3246e7674c3SLukasz Luba reg |= EXYNOS5_TIMING_SET_SWI;
3256e7674c3SLukasz Luba else
3266e7674c3SLukasz Luba reg &= ~EXYNOS5_TIMING_SET_SWI;
3276e7674c3SLukasz Luba
3286e7674c3SLukasz Luba regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, reg);
329c4f16e96SKrzysztof Kozlowski
330c4f16e96SKrzysztof Kozlowski return 0;
3316e7674c3SLukasz Luba }
3326e7674c3SLukasz Luba
3336e7674c3SLukasz Luba /**
3346e7674c3SLukasz Luba * exynos5_init_freq_table() - Initialized PM OPP framework
3356e7674c3SLukasz Luba * @dmc: DMC device for which the frequencies are used for OPP init
3366e7674c3SLukasz Luba * @profile: devfreq device's profile
3376e7674c3SLukasz Luba *
3386e7674c3SLukasz Luba * Populate the devfreq device's OPP table based on current frequency, voltage.
3396e7674c3SLukasz Luba */
exynos5_init_freq_table(struct exynos5_dmc * dmc,struct devfreq_dev_profile * profile)3406e7674c3SLukasz Luba static int exynos5_init_freq_table(struct exynos5_dmc *dmc,
3416e7674c3SLukasz Luba struct devfreq_dev_profile *profile)
3426e7674c3SLukasz Luba {
3432af13f97SKrzysztof Kozlowski struct device *dev = dmc->dev;
3446e7674c3SLukasz Luba int i, ret;
3456e7674c3SLukasz Luba int idx;
3466e7674c3SLukasz Luba unsigned long freq;
3476e7674c3SLukasz Luba
3482af13f97SKrzysztof Kozlowski ret = devm_pm_opp_of_add_table(dev);
3496e7674c3SLukasz Luba if (ret < 0) {
3502af13f97SKrzysztof Kozlowski dev_err(dev, "Failed to get OPP table\n");
3516e7674c3SLukasz Luba return ret;
3526e7674c3SLukasz Luba }
3536e7674c3SLukasz Luba
3542af13f97SKrzysztof Kozlowski dmc->opp_count = dev_pm_opp_get_opp_count(dev);
3556e7674c3SLukasz Luba
3562af13f97SKrzysztof Kozlowski dmc->opp = devm_kmalloc_array(dev, dmc->opp_count,
3576e7674c3SLukasz Luba sizeof(struct dmc_opp_table), GFP_KERNEL);
3586e7674c3SLukasz Luba if (!dmc->opp)
359a8bb0e87SYangtao Li return -ENOMEM;
3606e7674c3SLukasz Luba
3616e7674c3SLukasz Luba idx = dmc->opp_count - 1;
3626e7674c3SLukasz Luba for (i = 0, freq = ULONG_MAX; i < dmc->opp_count; i++, freq--) {
3636e7674c3SLukasz Luba struct dev_pm_opp *opp;
3646e7674c3SLukasz Luba
3652af13f97SKrzysztof Kozlowski opp = dev_pm_opp_find_freq_floor(dev, &freq);
3666e7674c3SLukasz Luba if (IS_ERR(opp))
367a8bb0e87SYangtao Li return PTR_ERR(opp);
3686e7674c3SLukasz Luba
3696e7674c3SLukasz Luba dmc->opp[idx - i].freq_hz = freq;
3706e7674c3SLukasz Luba dmc->opp[idx - i].volt_uv = dev_pm_opp_get_voltage(opp);
3716e7674c3SLukasz Luba
3726e7674c3SLukasz Luba dev_pm_opp_put(opp);
3736e7674c3SLukasz Luba }
3746e7674c3SLukasz Luba
3756e7674c3SLukasz Luba return 0;
3766e7674c3SLukasz Luba }
3776e7674c3SLukasz Luba
3786e7674c3SLukasz Luba /**
3796e7674c3SLukasz Luba * exynos5_set_bypass_dram_timings() - Low-level changes of the DRAM timings
3806e7674c3SLukasz Luba * @dmc: device for which the new settings is going to be applied
3816e7674c3SLukasz Luba *
3826e7674c3SLukasz Luba * Low-level function for changing timings for DRAM memory clocking from
3836e7674c3SLukasz Luba * 'bypass' clock source (fixed frequency @400MHz).
3846e7674c3SLukasz Luba * It uses timing bank registers set 1.
3856e7674c3SLukasz Luba */
exynos5_set_bypass_dram_timings(struct exynos5_dmc * dmc)3866e7674c3SLukasz Luba static void exynos5_set_bypass_dram_timings(struct exynos5_dmc *dmc)
3876e7674c3SLukasz Luba {
3886e7674c3SLukasz Luba writel(EXYNOS5_AREF_NORMAL,
3896e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF);
3906e7674c3SLukasz Luba
3916e7674c3SLukasz Luba writel(dmc->bypass_timing_row,
3926e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW1);
3936e7674c3SLukasz Luba writel(dmc->bypass_timing_row,
3946e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW1);
3956e7674c3SLukasz Luba writel(dmc->bypass_timing_data,
3966e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA1);
3976e7674c3SLukasz Luba writel(dmc->bypass_timing_data,
3986e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA1);
3996e7674c3SLukasz Luba writel(dmc->bypass_timing_power,
4006e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER1);
4016e7674c3SLukasz Luba writel(dmc->bypass_timing_power,
4026e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER1);
4036e7674c3SLukasz Luba }
4046e7674c3SLukasz Luba
4056e7674c3SLukasz Luba /**
4066e7674c3SLukasz Luba * exynos5_dram_change_timings() - Low-level changes of the DRAM final timings
4076e7674c3SLukasz Luba * @dmc: device for which the new settings is going to be applied
4086e7674c3SLukasz Luba * @target_rate: target frequency of the DMC
4096e7674c3SLukasz Luba *
4106e7674c3SLukasz Luba * Low-level function for changing timings for DRAM memory operating from main
4116e7674c3SLukasz Luba * clock source (BPLL), which can have different frequencies. Thus, each
4126e7674c3SLukasz Luba * frequency must have corresponding timings register values in order to keep
4136e7674c3SLukasz Luba * the needed delays.
4146e7674c3SLukasz Luba * It uses timing bank registers set 0.
4156e7674c3SLukasz Luba */
exynos5_dram_change_timings(struct exynos5_dmc * dmc,unsigned long target_rate)4166e7674c3SLukasz Luba static int exynos5_dram_change_timings(struct exynos5_dmc *dmc,
4176e7674c3SLukasz Luba unsigned long target_rate)
4186e7674c3SLukasz Luba {
4196e7674c3SLukasz Luba int idx;
4206e7674c3SLukasz Luba
4216e7674c3SLukasz Luba for (idx = dmc->opp_count - 1; idx >= 0; idx--)
4226e7674c3SLukasz Luba if (dmc->opp[idx].freq_hz <= target_rate)
4236e7674c3SLukasz Luba break;
4246e7674c3SLukasz Luba
4256e7674c3SLukasz Luba if (idx < 0)
4266e7674c3SLukasz Luba return -EINVAL;
4276e7674c3SLukasz Luba
4286e7674c3SLukasz Luba writel(EXYNOS5_AREF_NORMAL,
4296e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF);
4306e7674c3SLukasz Luba
4316e7674c3SLukasz Luba writel(dmc->timing_row[idx],
4326e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW0);
4336e7674c3SLukasz Luba writel(dmc->timing_row[idx],
4346e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW0);
4356e7674c3SLukasz Luba writel(dmc->timing_data[idx],
4366e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA0);
4376e7674c3SLukasz Luba writel(dmc->timing_data[idx],
4386e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA0);
4396e7674c3SLukasz Luba writel(dmc->timing_power[idx],
4406e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER0);
4416e7674c3SLukasz Luba writel(dmc->timing_power[idx],
4426e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER0);
4436e7674c3SLukasz Luba
4446e7674c3SLukasz Luba return 0;
4456e7674c3SLukasz Luba }
4466e7674c3SLukasz Luba
4476e7674c3SLukasz Luba /**
4486e7674c3SLukasz Luba * exynos5_dmc_align_target_voltage() - Sets the final voltage for the DMC
4496e7674c3SLukasz Luba * @dmc: device for which it is going to be set
4506e7674c3SLukasz Luba * @target_volt: new voltage which is chosen to be final
4516e7674c3SLukasz Luba *
4526e7674c3SLukasz Luba * Function tries to align voltage to the safe level for 'normal' mode.
4536e7674c3SLukasz Luba * It checks the need of higher voltage and changes the value. The target
4546e7674c3SLukasz Luba * voltage might be lower that currently set and still the system will be
4556e7674c3SLukasz Luba * stable.
4566e7674c3SLukasz Luba */
exynos5_dmc_align_target_voltage(struct exynos5_dmc * dmc,unsigned long target_volt)4576e7674c3SLukasz Luba static int exynos5_dmc_align_target_voltage(struct exynos5_dmc *dmc,
4586e7674c3SLukasz Luba unsigned long target_volt)
4596e7674c3SLukasz Luba {
4606e7674c3SLukasz Luba int ret = 0;
4616e7674c3SLukasz Luba
4626e7674c3SLukasz Luba if (dmc->curr_volt <= target_volt)
4636e7674c3SLukasz Luba return 0;
4646e7674c3SLukasz Luba
4656e7674c3SLukasz Luba ret = regulator_set_voltage(dmc->vdd_mif, target_volt,
4666e7674c3SLukasz Luba target_volt);
4676e7674c3SLukasz Luba if (!ret)
4686e7674c3SLukasz Luba dmc->curr_volt = target_volt;
4696e7674c3SLukasz Luba
4706e7674c3SLukasz Luba return ret;
4716e7674c3SLukasz Luba }
4726e7674c3SLukasz Luba
4736e7674c3SLukasz Luba /**
4746e7674c3SLukasz Luba * exynos5_dmc_align_bypass_voltage() - Sets the voltage for the DMC
4756e7674c3SLukasz Luba * @dmc: device for which it is going to be set
4766e7674c3SLukasz Luba * @target_volt: new voltage which is chosen to be final
4776e7674c3SLukasz Luba *
4786e7674c3SLukasz Luba * Function tries to align voltage to the safe level for the 'bypass' mode.
4796e7674c3SLukasz Luba * It checks the need of higher voltage and changes the value.
4806e7674c3SLukasz Luba * The target voltage must not be less than currently needed, because
4816e7674c3SLukasz Luba * for current frequency the device might become unstable.
4826e7674c3SLukasz Luba */
exynos5_dmc_align_bypass_voltage(struct exynos5_dmc * dmc,unsigned long target_volt)4836e7674c3SLukasz Luba static int exynos5_dmc_align_bypass_voltage(struct exynos5_dmc *dmc,
4846e7674c3SLukasz Luba unsigned long target_volt)
4856e7674c3SLukasz Luba {
4866e7674c3SLukasz Luba int ret = 0;
4876e7674c3SLukasz Luba
4886e7674c3SLukasz Luba if (dmc->curr_volt >= target_volt)
4896e7674c3SLukasz Luba return 0;
4906e7674c3SLukasz Luba
4916e7674c3SLukasz Luba ret = regulator_set_voltage(dmc->vdd_mif, target_volt,
4926e7674c3SLukasz Luba target_volt);
4936e7674c3SLukasz Luba if (!ret)
4946e7674c3SLukasz Luba dmc->curr_volt = target_volt;
4956e7674c3SLukasz Luba
4966e7674c3SLukasz Luba return ret;
4976e7674c3SLukasz Luba }
4986e7674c3SLukasz Luba
4996e7674c3SLukasz Luba /**
5006e7674c3SLukasz Luba * exynos5_dmc_align_bypass_dram_timings() - Chooses and sets DRAM timings
5016e7674c3SLukasz Luba * @dmc: device for which it is going to be set
5026e7674c3SLukasz Luba * @target_rate: new frequency which is chosen to be final
5036e7674c3SLukasz Luba *
5046e7674c3SLukasz Luba * Function changes the DRAM timings for the temporary 'bypass' mode.
5056e7674c3SLukasz Luba */
exynos5_dmc_align_bypass_dram_timings(struct exynos5_dmc * dmc,unsigned long target_rate)5066e7674c3SLukasz Luba static int exynos5_dmc_align_bypass_dram_timings(struct exynos5_dmc *dmc,
5076e7674c3SLukasz Luba unsigned long target_rate)
5086e7674c3SLukasz Luba {
5096e7674c3SLukasz Luba int idx = find_target_freq_idx(dmc, target_rate);
5106e7674c3SLukasz Luba
5116e7674c3SLukasz Luba if (idx < 0)
5126e7674c3SLukasz Luba return -EINVAL;
5136e7674c3SLukasz Luba
5146e7674c3SLukasz Luba exynos5_set_bypass_dram_timings(dmc);
5156e7674c3SLukasz Luba
5166e7674c3SLukasz Luba return 0;
5176e7674c3SLukasz Luba }
5186e7674c3SLukasz Luba
5196e7674c3SLukasz Luba /**
5206e7674c3SLukasz Luba * exynos5_dmc_switch_to_bypass_configuration() - Switching to temporary clock
5216e7674c3SLukasz Luba * @dmc: DMC device for which the switching is going to happen
5226e7674c3SLukasz Luba * @target_rate: new frequency which is going to be set as a final
5236e7674c3SLukasz Luba * @target_volt: new voltage which is going to be set as a final
5246e7674c3SLukasz Luba *
5256e7674c3SLukasz Luba * Function configures DMC and clocks for operating in temporary 'bypass' mode.
5266e7674c3SLukasz Luba * This mode is used only temporary but if required, changes voltage and timings
5276e7674c3SLukasz Luba * for DRAM chips. It switches the main clock to stable clock source for the
5286e7674c3SLukasz Luba * period of the main PLL reconfiguration.
5296e7674c3SLukasz Luba */
5306e7674c3SLukasz Luba static int
exynos5_dmc_switch_to_bypass_configuration(struct exynos5_dmc * dmc,unsigned long target_rate,unsigned long target_volt)5316e7674c3SLukasz Luba exynos5_dmc_switch_to_bypass_configuration(struct exynos5_dmc *dmc,
5326e7674c3SLukasz Luba unsigned long target_rate,
5336e7674c3SLukasz Luba unsigned long target_volt)
5346e7674c3SLukasz Luba {
5356e7674c3SLukasz Luba int ret;
5366e7674c3SLukasz Luba
5376e7674c3SLukasz Luba /*
5386e7674c3SLukasz Luba * Having higher voltage for a particular frequency does not harm
5396e7674c3SLukasz Luba * the chip. Use it for the temporary frequency change when one
5406e7674c3SLukasz Luba * voltage manipulation might be avoided.
5416e7674c3SLukasz Luba */
5426e7674c3SLukasz Luba ret = exynos5_dmc_align_bypass_voltage(dmc, target_volt);
5436e7674c3SLukasz Luba if (ret)
5446e7674c3SLukasz Luba return ret;
5456e7674c3SLukasz Luba
5466e7674c3SLukasz Luba /*
5476e7674c3SLukasz Luba * Longer delays for DRAM does not cause crash, the opposite does.
5486e7674c3SLukasz Luba */
5496e7674c3SLukasz Luba ret = exynos5_dmc_align_bypass_dram_timings(dmc, target_rate);
5506e7674c3SLukasz Luba if (ret)
5516e7674c3SLukasz Luba return ret;
5526e7674c3SLukasz Luba
5536e7674c3SLukasz Luba /*
5546e7674c3SLukasz Luba * Delays are long enough, so use them for the new coming clock.
5556e7674c3SLukasz Luba */
556c4f16e96SKrzysztof Kozlowski ret = exynos5_switch_timing_regs(dmc, USE_MX_MSPLL_TIMINGS);
5576e7674c3SLukasz Luba
5586e7674c3SLukasz Luba return ret;
5596e7674c3SLukasz Luba }
5606e7674c3SLukasz Luba
5616e7674c3SLukasz Luba /**
5626e7674c3SLukasz Luba * exynos5_dmc_change_freq_and_volt() - Changes voltage and frequency of the DMC
5636e7674c3SLukasz Luba * using safe procedure
5646e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be changed
5656e7674c3SLukasz Luba * @target_rate: requested new frequency
5666e7674c3SLukasz Luba * @target_volt: requested voltage which corresponds to the new frequency
5676e7674c3SLukasz Luba *
5686e7674c3SLukasz Luba * The DMC frequency change procedure requires a few steps.
5696e7674c3SLukasz Luba * The main requirement is to change the clock source in the clk mux
5706e7674c3SLukasz Luba * for the time of main clock PLL locking. The assumption is that the
5716e7674c3SLukasz Luba * alternative clock source set as parent is stable.
5726e7674c3SLukasz Luba * The second parent's clock frequency is fixed to 400MHz, it is named 'bypass'
5736e7674c3SLukasz Luba * clock. This requires alignment in DRAM timing parameters for the new
5746e7674c3SLukasz Luba * T-period. There is two bank sets for keeping DRAM
5756e7674c3SLukasz Luba * timings: set 0 and set 1. The set 0 is used when main clock source is
5766e7674c3SLukasz Luba * chosen. The 2nd set of regs is used for 'bypass' clock. Switching between
5776e7674c3SLukasz Luba * the two bank sets is part of the process.
5786e7674c3SLukasz Luba * The voltage must also be aligned to the minimum required level. There is
5796e7674c3SLukasz Luba * this intermediate step with switching to 'bypass' parent clock source.
5806e7674c3SLukasz Luba * if the old voltage is lower, it requires an increase of the voltage level.
5816e7674c3SLukasz Luba * The complexity of the voltage manipulation is hidden in low level function.
5826e7674c3SLukasz Luba * In this function there is last alignment of the voltage level at the end.
5836e7674c3SLukasz Luba */
5846e7674c3SLukasz Luba static int
exynos5_dmc_change_freq_and_volt(struct exynos5_dmc * dmc,unsigned long target_rate,unsigned long target_volt)5856e7674c3SLukasz Luba exynos5_dmc_change_freq_and_volt(struct exynos5_dmc *dmc,
5866e7674c3SLukasz Luba unsigned long target_rate,
5876e7674c3SLukasz Luba unsigned long target_volt)
5886e7674c3SLukasz Luba {
5896e7674c3SLukasz Luba int ret;
5906e7674c3SLukasz Luba
5916e7674c3SLukasz Luba ret = exynos5_dmc_switch_to_bypass_configuration(dmc, target_rate,
5926e7674c3SLukasz Luba target_volt);
5936e7674c3SLukasz Luba if (ret)
5946e7674c3SLukasz Luba return ret;
5956e7674c3SLukasz Luba
5966e7674c3SLukasz Luba /*
5976e7674c3SLukasz Luba * Voltage is set at least to a level needed for this frequency,
5986e7674c3SLukasz Luba * so switching clock source is safe now.
5996e7674c3SLukasz Luba */
6006e7674c3SLukasz Luba clk_prepare_enable(dmc->fout_spll);
6016e7674c3SLukasz Luba clk_prepare_enable(dmc->mout_spll);
6026e7674c3SLukasz Luba clk_prepare_enable(dmc->mout_mx_mspll_ccore);
6036e7674c3SLukasz Luba
6046e7674c3SLukasz Luba ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_mx_mspll_ccore);
6056e7674c3SLukasz Luba if (ret)
6066e7674c3SLukasz Luba goto disable_clocks;
6076e7674c3SLukasz Luba
6086e7674c3SLukasz Luba /*
6096e7674c3SLukasz Luba * We are safe to increase the timings for current bypass frequency.
6106e7674c3SLukasz Luba * Thanks to this the settings will be ready for the upcoming clock
6116e7674c3SLukasz Luba * source change.
6126e7674c3SLukasz Luba */
6136e7674c3SLukasz Luba exynos5_dram_change_timings(dmc, target_rate);
6146e7674c3SLukasz Luba
6156e7674c3SLukasz Luba clk_set_rate(dmc->fout_bpll, target_rate);
6166e7674c3SLukasz Luba
617c4f16e96SKrzysztof Kozlowski ret = exynos5_switch_timing_regs(dmc, USE_BPLL_TIMINGS);
618c4f16e96SKrzysztof Kozlowski if (ret)
619c4f16e96SKrzysztof Kozlowski goto disable_clocks;
6206e7674c3SLukasz Luba
6216e7674c3SLukasz Luba ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_bpll);
6226e7674c3SLukasz Luba if (ret)
6236e7674c3SLukasz Luba goto disable_clocks;
6246e7674c3SLukasz Luba
6256e7674c3SLukasz Luba /*
6266e7674c3SLukasz Luba * Make sure if the voltage is not from 'bypass' settings and align to
6276e7674c3SLukasz Luba * the right level for power efficiency.
6286e7674c3SLukasz Luba */
6296e7674c3SLukasz Luba ret = exynos5_dmc_align_target_voltage(dmc, target_volt);
6306e7674c3SLukasz Luba
6316e7674c3SLukasz Luba disable_clocks:
6326e7674c3SLukasz Luba clk_disable_unprepare(dmc->mout_mx_mspll_ccore);
6336e7674c3SLukasz Luba clk_disable_unprepare(dmc->mout_spll);
6346e7674c3SLukasz Luba clk_disable_unprepare(dmc->fout_spll);
6356e7674c3SLukasz Luba
6366e7674c3SLukasz Luba return ret;
6376e7674c3SLukasz Luba }
6386e7674c3SLukasz Luba
6396e7674c3SLukasz Luba /**
6406e7674c3SLukasz Luba * exynos5_dmc_get_volt_freq() - Gets the frequency and voltage from the OPP
6416e7674c3SLukasz Luba * table.
6426e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be changed
6436e7674c3SLukasz Luba * @freq: requested frequency in KHz
6446e7674c3SLukasz Luba * @target_rate: returned frequency which is the same or lower than
6456e7674c3SLukasz Luba * requested
6466e7674c3SLukasz Luba * @target_volt: returned voltage which corresponds to the returned
6476e7674c3SLukasz Luba * frequency
6484c2af5ddSKrzysztof Kozlowski * @flags: devfreq flags provided for this frequency change request
6496e7674c3SLukasz Luba *
6506e7674c3SLukasz Luba * Function gets requested frequency and checks OPP framework for needed
6516e7674c3SLukasz Luba * frequency and voltage. It populates the values 'target_rate' and
6526e7674c3SLukasz Luba * 'target_volt' or returns error value when OPP framework fails.
6536e7674c3SLukasz Luba */
exynos5_dmc_get_volt_freq(struct exynos5_dmc * dmc,unsigned long * freq,unsigned long * target_rate,unsigned long * target_volt,u32 flags)6546e7674c3SLukasz Luba static int exynos5_dmc_get_volt_freq(struct exynos5_dmc *dmc,
6556e7674c3SLukasz Luba unsigned long *freq,
6566e7674c3SLukasz Luba unsigned long *target_rate,
6576e7674c3SLukasz Luba unsigned long *target_volt, u32 flags)
6586e7674c3SLukasz Luba {
6596e7674c3SLukasz Luba struct dev_pm_opp *opp;
6606e7674c3SLukasz Luba
6616e7674c3SLukasz Luba opp = devfreq_recommended_opp(dmc->dev, freq, flags);
6626e7674c3SLukasz Luba if (IS_ERR(opp))
6636e7674c3SLukasz Luba return PTR_ERR(opp);
6646e7674c3SLukasz Luba
6656e7674c3SLukasz Luba *target_rate = dev_pm_opp_get_freq(opp);
6666e7674c3SLukasz Luba *target_volt = dev_pm_opp_get_voltage(opp);
6676e7674c3SLukasz Luba dev_pm_opp_put(opp);
6686e7674c3SLukasz Luba
6696e7674c3SLukasz Luba return 0;
6706e7674c3SLukasz Luba }
6716e7674c3SLukasz Luba
6726e7674c3SLukasz Luba /**
6736e7674c3SLukasz Luba * exynos5_dmc_target() - Function responsible for changing frequency of DMC
6746e7674c3SLukasz Luba * @dev: device for which the frequency is going to be changed
6756e7674c3SLukasz Luba * @freq: requested frequency in KHz
6766e7674c3SLukasz Luba * @flags: flags provided for this frequency change request
6776e7674c3SLukasz Luba *
6786e7674c3SLukasz Luba * An entry function provided to the devfreq framework which provides frequency
6796e7674c3SLukasz Luba * change of the DMC. The function gets the possible rate from OPP table based
6806e7674c3SLukasz Luba * on requested frequency. It calls the next function responsible for the
6816e7674c3SLukasz Luba * frequency and voltage change. In case of failure, does not set 'curr_rate'
6826e7674c3SLukasz Luba * and returns error value to the framework.
6836e7674c3SLukasz Luba */
exynos5_dmc_target(struct device * dev,unsigned long * freq,u32 flags)6846e7674c3SLukasz Luba static int exynos5_dmc_target(struct device *dev, unsigned long *freq,
6856e7674c3SLukasz Luba u32 flags)
6866e7674c3SLukasz Luba {
6876e7674c3SLukasz Luba struct exynos5_dmc *dmc = dev_get_drvdata(dev);
6886e7674c3SLukasz Luba unsigned long target_rate = 0;
6896e7674c3SLukasz Luba unsigned long target_volt = 0;
6906e7674c3SLukasz Luba int ret;
6916e7674c3SLukasz Luba
6926e7674c3SLukasz Luba ret = exynos5_dmc_get_volt_freq(dmc, freq, &target_rate, &target_volt,
6936e7674c3SLukasz Luba flags);
6946e7674c3SLukasz Luba
6956e7674c3SLukasz Luba if (ret)
6966e7674c3SLukasz Luba return ret;
6976e7674c3SLukasz Luba
6986e7674c3SLukasz Luba if (target_rate == dmc->curr_rate)
6996e7674c3SLukasz Luba return 0;
7006e7674c3SLukasz Luba
7016e7674c3SLukasz Luba mutex_lock(&dmc->lock);
7026e7674c3SLukasz Luba
7036e7674c3SLukasz Luba ret = exynos5_dmc_change_freq_and_volt(dmc, target_rate, target_volt);
7046e7674c3SLukasz Luba
7056e7674c3SLukasz Luba if (ret) {
7066e7674c3SLukasz Luba mutex_unlock(&dmc->lock);
7076e7674c3SLukasz Luba return ret;
7086e7674c3SLukasz Luba }
7096e7674c3SLukasz Luba
7106e7674c3SLukasz Luba dmc->curr_rate = target_rate;
7116e7674c3SLukasz Luba
7126e7674c3SLukasz Luba mutex_unlock(&dmc->lock);
7136e7674c3SLukasz Luba return 0;
7146e7674c3SLukasz Luba }
7156e7674c3SLukasz Luba
7166e7674c3SLukasz Luba /**
7176e7674c3SLukasz Luba * exynos5_counters_get() - Gets the performance counters values.
7186e7674c3SLukasz Luba * @dmc: device for which the counters are going to be checked
7196e7674c3SLukasz Luba * @load_count: variable which is populated with counter value
7206e7674c3SLukasz Luba * @total_count: variable which is used as 'wall clock' reference
7216e7674c3SLukasz Luba *
7226e7674c3SLukasz Luba * Function which provides performance counters values. It sums up counters for
7236e7674c3SLukasz Luba * two DMC channels. The 'total_count' is used as a reference and max value.
7246e7674c3SLukasz Luba * The ratio 'load_count/total_count' shows the busy percentage [0%, 100%].
7256e7674c3SLukasz Luba */
exynos5_counters_get(struct exynos5_dmc * dmc,unsigned long * load_count,unsigned long * total_count)7266e7674c3SLukasz Luba static int exynos5_counters_get(struct exynos5_dmc *dmc,
7276e7674c3SLukasz Luba unsigned long *load_count,
7286e7674c3SLukasz Luba unsigned long *total_count)
7296e7674c3SLukasz Luba {
7306e7674c3SLukasz Luba unsigned long total = 0;
7316e7674c3SLukasz Luba struct devfreq_event_data event;
7326e7674c3SLukasz Luba int ret, i;
7336e7674c3SLukasz Luba
7346e7674c3SLukasz Luba *load_count = 0;
7356e7674c3SLukasz Luba
7366e7674c3SLukasz Luba /* Take into account only read+write counters, but stop all */
7376e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) {
7386e7674c3SLukasz Luba if (!dmc->counter[i])
7396e7674c3SLukasz Luba continue;
7406e7674c3SLukasz Luba
7416e7674c3SLukasz Luba ret = devfreq_event_get_event(dmc->counter[i], &event);
7426e7674c3SLukasz Luba if (ret < 0)
7436e7674c3SLukasz Luba return ret;
7446e7674c3SLukasz Luba
7456e7674c3SLukasz Luba *load_count += event.load_count;
7466e7674c3SLukasz Luba
7476e7674c3SLukasz Luba if (total < event.total_count)
7486e7674c3SLukasz Luba total = event.total_count;
7496e7674c3SLukasz Luba }
7506e7674c3SLukasz Luba
7516e7674c3SLukasz Luba *total_count = total;
7526e7674c3SLukasz Luba
7536e7674c3SLukasz Luba return 0;
7546e7674c3SLukasz Luba }
7556e7674c3SLukasz Luba
7566e7674c3SLukasz Luba /**
757bbf91886SLukasz Luba * exynos5_dmc_start_perf_events() - Setup and start performance event counters
758bbf91886SLukasz Luba * @dmc: device for which the counters are going to be checked
759bbf91886SLukasz Luba * @beg_value: initial value for the counter
760bbf91886SLukasz Luba *
761bbf91886SLukasz Luba * Function which enables needed counters, interrupts and sets initial values
762bbf91886SLukasz Luba * then starts the counters.
763bbf91886SLukasz Luba */
exynos5_dmc_start_perf_events(struct exynos5_dmc * dmc,u32 beg_value)764bbf91886SLukasz Luba static void exynos5_dmc_start_perf_events(struct exynos5_dmc *dmc,
765bbf91886SLukasz Luba u32 beg_value)
766bbf91886SLukasz Luba {
767bbf91886SLukasz Luba /* Enable interrupts for counter 2 */
768bbf91886SLukasz Luba writel(PERF_CNT2, dmc->base_drexi0 + DREX_INTENS_PPC);
769bbf91886SLukasz Luba writel(PERF_CNT2, dmc->base_drexi1 + DREX_INTENS_PPC);
770bbf91886SLukasz Luba
771bbf91886SLukasz Luba /* Enable counter 2 and CCNT */
772bbf91886SLukasz Luba writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_CNTENS_PPC);
773bbf91886SLukasz Luba writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_CNTENS_PPC);
774bbf91886SLukasz Luba
775bbf91886SLukasz Luba /* Clear overflow flag for all counters */
776bbf91886SLukasz Luba writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_FLAG_PPC);
777bbf91886SLukasz Luba writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_FLAG_PPC);
778bbf91886SLukasz Luba
779bbf91886SLukasz Luba /* Reset all counters */
780bbf91886SLukasz Luba writel(CC_RESET | PPC_COUNTER_RESET, dmc->base_drexi0 + DREX_PMNC_PPC);
781bbf91886SLukasz Luba writel(CC_RESET | PPC_COUNTER_RESET, dmc->base_drexi1 + DREX_PMNC_PPC);
782bbf91886SLukasz Luba
783bbf91886SLukasz Luba /*
784bbf91886SLukasz Luba * Set start value for the counters, the number of samples that
785bbf91886SLukasz Luba * will be gathered is calculated as: 0xffffffff - beg_value
786bbf91886SLukasz Luba */
787bbf91886SLukasz Luba writel(beg_value, dmc->base_drexi0 + DREX_PMCNT2_PPC);
788bbf91886SLukasz Luba writel(beg_value, dmc->base_drexi1 + DREX_PMCNT2_PPC);
789bbf91886SLukasz Luba
790bbf91886SLukasz Luba /* Start all counters */
791bbf91886SLukasz Luba writel(PPC_ENABLE, dmc->base_drexi0 + DREX_PMNC_PPC);
792bbf91886SLukasz Luba writel(PPC_ENABLE, dmc->base_drexi1 + DREX_PMNC_PPC);
793bbf91886SLukasz Luba }
794bbf91886SLukasz Luba
795bbf91886SLukasz Luba /**
796bbf91886SLukasz Luba * exynos5_dmc_perf_events_calc() - Calculate utilization
797bbf91886SLukasz Luba * @dmc: device for which the counters are going to be checked
798bbf91886SLukasz Luba * @diff_ts: time between last interrupt and current one
799bbf91886SLukasz Luba *
800bbf91886SLukasz Luba * Function which calculates needed utilization for the devfreq governor.
801bbf91886SLukasz Luba * It prepares values for 'busy_time' and 'total_time' based on elapsed time
802bbf91886SLukasz Luba * between interrupts, which approximates utilization.
803bbf91886SLukasz Luba */
exynos5_dmc_perf_events_calc(struct exynos5_dmc * dmc,u64 diff_ts)804bbf91886SLukasz Luba static void exynos5_dmc_perf_events_calc(struct exynos5_dmc *dmc, u64 diff_ts)
805bbf91886SLukasz Luba {
806bbf91886SLukasz Luba /*
807bbf91886SLukasz Luba * This is a simple algorithm for managing traffic on DMC.
808bbf91886SLukasz Luba * When there is almost no load the counters overflow every 4s,
809bbf91886SLukasz Luba * no mater the DMC frequency.
810bbf91886SLukasz Luba * The high load might be approximated using linear function.
811bbf91886SLukasz Luba * Knowing that, simple calculation can provide 'busy_time' and
812bbf91886SLukasz Luba * 'total_time' to the devfreq governor which picks up target
813bbf91886SLukasz Luba * frequency.
814bbf91886SLukasz Luba * We want a fast ramp up and slow decay in frequency change function.
815bbf91886SLukasz Luba */
816bbf91886SLukasz Luba if (diff_ts < PERF_EVENT_UP_DOWN_THRESHOLD) {
817bbf91886SLukasz Luba /*
818bbf91886SLukasz Luba * Set higher utilization for the simple_ondemand governor.
819bbf91886SLukasz Luba * The governor should increase the frequency of the DMC.
820bbf91886SLukasz Luba */
821bbf91886SLukasz Luba dmc->load = 70;
822bbf91886SLukasz Luba dmc->total = 100;
823bbf91886SLukasz Luba } else {
824bbf91886SLukasz Luba /*
825bbf91886SLukasz Luba * Set low utilization for the simple_ondemand governor.
826bbf91886SLukasz Luba * The governor should decrease the frequency of the DMC.
827bbf91886SLukasz Luba */
828bbf91886SLukasz Luba dmc->load = 35;
829bbf91886SLukasz Luba dmc->total = 100;
830bbf91886SLukasz Luba }
831bbf91886SLukasz Luba
832bbf91886SLukasz Luba dev_dbg(dmc->dev, "diff_ts=%llu\n", diff_ts);
833bbf91886SLukasz Luba }
834bbf91886SLukasz Luba
835bbf91886SLukasz Luba /**
836bbf91886SLukasz Luba * exynos5_dmc_perf_events_check() - Checks the status of the counters
837bbf91886SLukasz Luba * @dmc: device for which the counters are going to be checked
838bbf91886SLukasz Luba *
839bbf91886SLukasz Luba * Function which is called from threaded IRQ to check the counters state
840bbf91886SLukasz Luba * and to call approximation for the needed utilization.
841bbf91886SLukasz Luba */
exynos5_dmc_perf_events_check(struct exynos5_dmc * dmc)842bbf91886SLukasz Luba static void exynos5_dmc_perf_events_check(struct exynos5_dmc *dmc)
843bbf91886SLukasz Luba {
844bbf91886SLukasz Luba u32 val;
845bbf91886SLukasz Luba u64 diff_ts, ts;
846bbf91886SLukasz Luba
847bbf91886SLukasz Luba ts = ktime_get_ns();
848bbf91886SLukasz Luba
849bbf91886SLukasz Luba /* Stop all counters */
850bbf91886SLukasz Luba writel(0, dmc->base_drexi0 + DREX_PMNC_PPC);
851bbf91886SLukasz Luba writel(0, dmc->base_drexi1 + DREX_PMNC_PPC);
852bbf91886SLukasz Luba
853bbf91886SLukasz Luba /* Check the source in interrupt flag registers (which channel) */
854bbf91886SLukasz Luba val = readl(dmc->base_drexi0 + DREX_FLAG_PPC);
855bbf91886SLukasz Luba if (val) {
856bbf91886SLukasz Luba diff_ts = ts - dmc->last_overflow_ts[0];
857bbf91886SLukasz Luba dmc->last_overflow_ts[0] = ts;
858bbf91886SLukasz Luba dev_dbg(dmc->dev, "drex0 0xE050 val= 0x%08x\n", val);
859bbf91886SLukasz Luba } else {
860bbf91886SLukasz Luba val = readl(dmc->base_drexi1 + DREX_FLAG_PPC);
861bbf91886SLukasz Luba diff_ts = ts - dmc->last_overflow_ts[1];
862bbf91886SLukasz Luba dmc->last_overflow_ts[1] = ts;
863bbf91886SLukasz Luba dev_dbg(dmc->dev, "drex1 0xE050 val= 0x%08x\n", val);
864bbf91886SLukasz Luba }
865bbf91886SLukasz Luba
866bbf91886SLukasz Luba exynos5_dmc_perf_events_calc(dmc, diff_ts);
867bbf91886SLukasz Luba
868bbf91886SLukasz Luba exynos5_dmc_start_perf_events(dmc, PERF_COUNTER_START_VALUE);
869bbf91886SLukasz Luba }
870bbf91886SLukasz Luba
871bbf91886SLukasz Luba /**
872bbf91886SLukasz Luba * exynos5_dmc_enable_perf_events() - Enable performance events
873bbf91886SLukasz Luba * @dmc: device for which the counters are going to be checked
874bbf91886SLukasz Luba *
875bbf91886SLukasz Luba * Function which is setup needed environment and enables counters.
876bbf91886SLukasz Luba */
exynos5_dmc_enable_perf_events(struct exynos5_dmc * dmc)877bbf91886SLukasz Luba static void exynos5_dmc_enable_perf_events(struct exynos5_dmc *dmc)
878bbf91886SLukasz Luba {
879bbf91886SLukasz Luba u64 ts;
880bbf91886SLukasz Luba
881bbf91886SLukasz Luba /* Enable Performance Event Clock */
882bbf91886SLukasz Luba writel(PEREV_CLK_EN, dmc->base_drexi0 + DREX_PPCCLKCON);
883bbf91886SLukasz Luba writel(PEREV_CLK_EN, dmc->base_drexi1 + DREX_PPCCLKCON);
884bbf91886SLukasz Luba
885bbf91886SLukasz Luba /* Select read transfers as performance event2 */
886bbf91886SLukasz Luba writel(READ_TRANSFER_CH0, dmc->base_drexi0 + DREX_PEREV2CONFIG);
887bbf91886SLukasz Luba writel(READ_TRANSFER_CH1, dmc->base_drexi1 + DREX_PEREV2CONFIG);
888bbf91886SLukasz Luba
889bbf91886SLukasz Luba ts = ktime_get_ns();
890bbf91886SLukasz Luba dmc->last_overflow_ts[0] = ts;
891bbf91886SLukasz Luba dmc->last_overflow_ts[1] = ts;
892bbf91886SLukasz Luba
893bbf91886SLukasz Luba /* Devfreq shouldn't be faster than initialization, play safe though. */
894bbf91886SLukasz Luba dmc->load = 99;
895bbf91886SLukasz Luba dmc->total = 100;
896bbf91886SLukasz Luba }
897bbf91886SLukasz Luba
898bbf91886SLukasz Luba /**
899bbf91886SLukasz Luba * exynos5_dmc_disable_perf_events() - Disable performance events
900bbf91886SLukasz Luba * @dmc: device for which the counters are going to be checked
901bbf91886SLukasz Luba *
902bbf91886SLukasz Luba * Function which stops, disables performance event counters and interrupts.
903bbf91886SLukasz Luba */
exynos5_dmc_disable_perf_events(struct exynos5_dmc * dmc)904bbf91886SLukasz Luba static void exynos5_dmc_disable_perf_events(struct exynos5_dmc *dmc)
905bbf91886SLukasz Luba {
906bbf91886SLukasz Luba /* Stop all counters */
907bbf91886SLukasz Luba writel(0, dmc->base_drexi0 + DREX_PMNC_PPC);
908bbf91886SLukasz Luba writel(0, dmc->base_drexi1 + DREX_PMNC_PPC);
909bbf91886SLukasz Luba
910bbf91886SLukasz Luba /* Disable interrupts for counter 2 */
911bbf91886SLukasz Luba writel(PERF_CNT2, dmc->base_drexi0 + DREX_INTENC_PPC);
912bbf91886SLukasz Luba writel(PERF_CNT2, dmc->base_drexi1 + DREX_INTENC_PPC);
913bbf91886SLukasz Luba
914bbf91886SLukasz Luba /* Disable counter 2 and CCNT */
915bbf91886SLukasz Luba writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_CNTENC_PPC);
916bbf91886SLukasz Luba writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_CNTENC_PPC);
917bbf91886SLukasz Luba
918bbf91886SLukasz Luba /* Clear overflow flag for all counters */
919bbf91886SLukasz Luba writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi0 + DREX_FLAG_PPC);
920bbf91886SLukasz Luba writel(PERF_CNT2 | PERF_CCNT, dmc->base_drexi1 + DREX_FLAG_PPC);
921bbf91886SLukasz Luba }
922bbf91886SLukasz Luba
923bbf91886SLukasz Luba /**
9246e7674c3SLukasz Luba * exynos5_dmc_get_status() - Read current DMC performance statistics.
9256e7674c3SLukasz Luba * @dev: device for which the statistics are requested
9266e7674c3SLukasz Luba * @stat: structure which has statistic fields
9276e7674c3SLukasz Luba *
9286e7674c3SLukasz Luba * Function reads the DMC performance counters and calculates 'busy_time'
9296e7674c3SLukasz Luba * and 'total_time'. To protect from overflow, the values are shifted right
9306e7674c3SLukasz Luba * by 10. After read out the counters are setup to count again.
9316e7674c3SLukasz Luba */
exynos5_dmc_get_status(struct device * dev,struct devfreq_dev_status * stat)9326e7674c3SLukasz Luba static int exynos5_dmc_get_status(struct device *dev,
9336e7674c3SLukasz Luba struct devfreq_dev_status *stat)
9346e7674c3SLukasz Luba {
9356e7674c3SLukasz Luba struct exynos5_dmc *dmc = dev_get_drvdata(dev);
9366e7674c3SLukasz Luba unsigned long load, total;
9376e7674c3SLukasz Luba int ret;
9386e7674c3SLukasz Luba
939bbf91886SLukasz Luba if (dmc->in_irq_mode) {
9407f7d9e1eSLukasz Luba mutex_lock(&dmc->lock);
941bbf91886SLukasz Luba stat->current_frequency = dmc->curr_rate;
9427f7d9e1eSLukasz Luba mutex_unlock(&dmc->lock);
9437f7d9e1eSLukasz Luba
944bbf91886SLukasz Luba stat->busy_time = dmc->load;
945bbf91886SLukasz Luba stat->total_time = dmc->total;
946bbf91886SLukasz Luba } else {
9476e7674c3SLukasz Luba ret = exynos5_counters_get(dmc, &load, &total);
9486e7674c3SLukasz Luba if (ret < 0)
9496e7674c3SLukasz Luba return -EINVAL;
9506e7674c3SLukasz Luba
951bbf91886SLukasz Luba /* To protect from overflow, divide by 1024 */
9526e7674c3SLukasz Luba stat->busy_time = load >> 10;
9536e7674c3SLukasz Luba stat->total_time = total >> 10;
9546e7674c3SLukasz Luba
9556e7674c3SLukasz Luba ret = exynos5_counters_set_event(dmc);
9566e7674c3SLukasz Luba if (ret < 0) {
9576e7674c3SLukasz Luba dev_err(dev, "could not set event counter\n");
9586e7674c3SLukasz Luba return ret;
9596e7674c3SLukasz Luba }
960bbf91886SLukasz Luba }
9616e7674c3SLukasz Luba
9626e7674c3SLukasz Luba return 0;
9636e7674c3SLukasz Luba }
9646e7674c3SLukasz Luba
9656e7674c3SLukasz Luba /**
9666e7674c3SLukasz Luba * exynos5_dmc_get_cur_freq() - Function returns current DMC frequency
9676e7674c3SLukasz Luba * @dev: device for which the framework checks operating frequency
9686e7674c3SLukasz Luba * @freq: returned frequency value
9696e7674c3SLukasz Luba *
9706e7674c3SLukasz Luba * It returns the currently used frequency of the DMC. The real operating
9716e7674c3SLukasz Luba * frequency might be lower when the clock source value could not be divided
9726e7674c3SLukasz Luba * to the requested value.
9736e7674c3SLukasz Luba */
exynos5_dmc_get_cur_freq(struct device * dev,unsigned long * freq)9746e7674c3SLukasz Luba static int exynos5_dmc_get_cur_freq(struct device *dev, unsigned long *freq)
9756e7674c3SLukasz Luba {
9766e7674c3SLukasz Luba struct exynos5_dmc *dmc = dev_get_drvdata(dev);
9776e7674c3SLukasz Luba
9786e7674c3SLukasz Luba mutex_lock(&dmc->lock);
9796e7674c3SLukasz Luba *freq = dmc->curr_rate;
9806e7674c3SLukasz Luba mutex_unlock(&dmc->lock);
9816e7674c3SLukasz Luba
9826e7674c3SLukasz Luba return 0;
9836e7674c3SLukasz Luba }
9846e7674c3SLukasz Luba
9854c2af5ddSKrzysztof Kozlowski /*
9866e7674c3SLukasz Luba * exynos5_dmc_df_profile - Devfreq governor's profile structure
9876e7674c3SLukasz Luba *
9886e7674c3SLukasz Luba * It provides to the devfreq framework needed functions and polling period.
9896e7674c3SLukasz Luba */
9906e7674c3SLukasz Luba static struct devfreq_dev_profile exynos5_dmc_df_profile = {
991ae8eb8baSChanwoo Choi .timer = DEVFREQ_TIMER_DELAYED,
9926e7674c3SLukasz Luba .target = exynos5_dmc_target,
9936e7674c3SLukasz Luba .get_dev_status = exynos5_dmc_get_status,
9946e7674c3SLukasz Luba .get_cur_freq = exynos5_dmc_get_cur_freq,
9956e7674c3SLukasz Luba };
9966e7674c3SLukasz Luba
9976e7674c3SLukasz Luba /**
9980e9bc420SKrzysztof Kozlowski * exynos5_dmc_align_init_freq() - Align initial frequency value
9996e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be set
10006e7674c3SLukasz Luba * @bootloader_init_freq: initial frequency set by the bootloader in KHz
10016e7674c3SLukasz Luba *
10026e7674c3SLukasz Luba * The initial bootloader frequency, which is present during boot, might be
10036e7674c3SLukasz Luba * different that supported frequency values in the driver. It is possible
10046e7674c3SLukasz Luba * due to different PLL settings or used PLL as a source.
10056e7674c3SLukasz Luba * This function provides the 'initial_freq' for the devfreq framework
10066e7674c3SLukasz Luba * statistics engine which supports only registered values. Thus, some alignment
10076e7674c3SLukasz Luba * must be made.
10086e7674c3SLukasz Luba */
1009d51e6a69SLukasz Luba static unsigned long
exynos5_dmc_align_init_freq(struct exynos5_dmc * dmc,unsigned long bootloader_init_freq)10106e7674c3SLukasz Luba exynos5_dmc_align_init_freq(struct exynos5_dmc *dmc,
10116e7674c3SLukasz Luba unsigned long bootloader_init_freq)
10126e7674c3SLukasz Luba {
10136e7674c3SLukasz Luba unsigned long aligned_freq;
10146e7674c3SLukasz Luba int idx;
10156e7674c3SLukasz Luba
10166e7674c3SLukasz Luba idx = find_target_freq_idx(dmc, bootloader_init_freq);
10176e7674c3SLukasz Luba if (idx >= 0)
10186e7674c3SLukasz Luba aligned_freq = dmc->opp[idx].freq_hz;
10196e7674c3SLukasz Luba else
10206e7674c3SLukasz Luba aligned_freq = dmc->opp[dmc->opp_count - 1].freq_hz;
10216e7674c3SLukasz Luba
10226e7674c3SLukasz Luba return aligned_freq;
10236e7674c3SLukasz Luba }
10246e7674c3SLukasz Luba
10256e7674c3SLukasz Luba /**
10266e7674c3SLukasz Luba * create_timings_aligned() - Create register values and align with standard
10276e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be set
10284c2af5ddSKrzysztof Kozlowski * @reg_timing_row: array to fill with values for timing row register
10294c2af5ddSKrzysztof Kozlowski * @reg_timing_data: array to fill with values for timing data register
10304c2af5ddSKrzysztof Kozlowski * @reg_timing_power: array to fill with values for timing power register
10316e7674c3SLukasz Luba * @clk_period_ps: the period of the clock, known as tCK
10326e7674c3SLukasz Luba *
10336e7674c3SLukasz Luba * The function calculates timings and creates a register value ready for
10346e7674c3SLukasz Luba * a frequency transition. The register contains a few timings. They are
10356e7674c3SLukasz Luba * shifted by a known offset. The timing value is calculated based on memory
10366e7674c3SLukasz Luba * specyfication: minimal time required and minimal cycles required.
10376e7674c3SLukasz Luba */
create_timings_aligned(struct exynos5_dmc * dmc,u32 * reg_timing_row,u32 * reg_timing_data,u32 * reg_timing_power,u32 clk_period_ps)10386e7674c3SLukasz Luba static int create_timings_aligned(struct exynos5_dmc *dmc, u32 *reg_timing_row,
10396e7674c3SLukasz Luba u32 *reg_timing_data, u32 *reg_timing_power,
10406e7674c3SLukasz Luba u32 clk_period_ps)
10416e7674c3SLukasz Luba {
10426e7674c3SLukasz Luba u32 val;
10436e7674c3SLukasz Luba const struct timing_reg *reg;
10446e7674c3SLukasz Luba
10456e7674c3SLukasz Luba if (clk_period_ps == 0)
10466e7674c3SLukasz Luba return -EINVAL;
10476e7674c3SLukasz Luba
10486e7674c3SLukasz Luba *reg_timing_row = 0;
10496e7674c3SLukasz Luba *reg_timing_data = 0;
10506e7674c3SLukasz Luba *reg_timing_power = 0;
10516e7674c3SLukasz Luba
10526e7674c3SLukasz Luba val = dmc->timings->tRFC / clk_period_ps;
10536e7674c3SLukasz Luba val += dmc->timings->tRFC % clk_period_ps ? 1 : 0;
10546e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRFC);
1055bb0ebc7dSKrzysztof Kozlowski reg = &timing_row_reg_fields[0];
10566e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val);
10576e7674c3SLukasz Luba
10586e7674c3SLukasz Luba val = dmc->timings->tRRD / clk_period_ps;
10596e7674c3SLukasz Luba val += dmc->timings->tRRD % clk_period_ps ? 1 : 0;
10606e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRRD);
1061bb0ebc7dSKrzysztof Kozlowski reg = &timing_row_reg_fields[1];
10626e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val);
10636e7674c3SLukasz Luba
10646e7674c3SLukasz Luba val = dmc->timings->tRPab / clk_period_ps;
10656e7674c3SLukasz Luba val += dmc->timings->tRPab % clk_period_ps ? 1 : 0;
10666e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRPab);
1067bb0ebc7dSKrzysztof Kozlowski reg = &timing_row_reg_fields[2];
10686e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val);
10696e7674c3SLukasz Luba
10706e7674c3SLukasz Luba val = dmc->timings->tRCD / clk_period_ps;
10716e7674c3SLukasz Luba val += dmc->timings->tRCD % clk_period_ps ? 1 : 0;
10726e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRCD);
1073bb0ebc7dSKrzysztof Kozlowski reg = &timing_row_reg_fields[3];
10746e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val);
10756e7674c3SLukasz Luba
10766e7674c3SLukasz Luba val = dmc->timings->tRC / clk_period_ps;
10776e7674c3SLukasz Luba val += dmc->timings->tRC % clk_period_ps ? 1 : 0;
10786e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRC);
1079bb0ebc7dSKrzysztof Kozlowski reg = &timing_row_reg_fields[4];
10806e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val);
10816e7674c3SLukasz Luba
10826e7674c3SLukasz Luba val = dmc->timings->tRAS / clk_period_ps;
10836e7674c3SLukasz Luba val += dmc->timings->tRAS % clk_period_ps ? 1 : 0;
10846e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRAS);
1085bb0ebc7dSKrzysztof Kozlowski reg = &timing_row_reg_fields[5];
10866e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val);
10876e7674c3SLukasz Luba
10886e7674c3SLukasz Luba /* data related timings */
10896e7674c3SLukasz Luba val = dmc->timings->tWTR / clk_period_ps;
10906e7674c3SLukasz Luba val += dmc->timings->tWTR % clk_period_ps ? 1 : 0;
10916e7674c3SLukasz Luba val = max(val, dmc->min_tck->tWTR);
1092bb0ebc7dSKrzysztof Kozlowski reg = &timing_data_reg_fields[0];
10936e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val);
10946e7674c3SLukasz Luba
10956e7674c3SLukasz Luba val = dmc->timings->tWR / clk_period_ps;
10966e7674c3SLukasz Luba val += dmc->timings->tWR % clk_period_ps ? 1 : 0;
10976e7674c3SLukasz Luba val = max(val, dmc->min_tck->tWR);
1098bb0ebc7dSKrzysztof Kozlowski reg = &timing_data_reg_fields[1];
10996e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val);
11006e7674c3SLukasz Luba
11016e7674c3SLukasz Luba val = dmc->timings->tRTP / clk_period_ps;
11026e7674c3SLukasz Luba val += dmc->timings->tRTP % clk_period_ps ? 1 : 0;
11036e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRTP);
1104bb0ebc7dSKrzysztof Kozlowski reg = &timing_data_reg_fields[2];
11056e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val);
11066e7674c3SLukasz Luba
11076e7674c3SLukasz Luba val = dmc->timings->tW2W_C2C / clk_period_ps;
11086e7674c3SLukasz Luba val += dmc->timings->tW2W_C2C % clk_period_ps ? 1 : 0;
11096e7674c3SLukasz Luba val = max(val, dmc->min_tck->tW2W_C2C);
1110bb0ebc7dSKrzysztof Kozlowski reg = &timing_data_reg_fields[3];
11116e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val);
11126e7674c3SLukasz Luba
11136e7674c3SLukasz Luba val = dmc->timings->tR2R_C2C / clk_period_ps;
11146e7674c3SLukasz Luba val += dmc->timings->tR2R_C2C % clk_period_ps ? 1 : 0;
11156e7674c3SLukasz Luba val = max(val, dmc->min_tck->tR2R_C2C);
1116bb0ebc7dSKrzysztof Kozlowski reg = &timing_data_reg_fields[4];
11176e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val);
11186e7674c3SLukasz Luba
11196e7674c3SLukasz Luba val = dmc->timings->tWL / clk_period_ps;
11206e7674c3SLukasz Luba val += dmc->timings->tWL % clk_period_ps ? 1 : 0;
11216e7674c3SLukasz Luba val = max(val, dmc->min_tck->tWL);
1122bb0ebc7dSKrzysztof Kozlowski reg = &timing_data_reg_fields[5];
11236e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val);
11246e7674c3SLukasz Luba
11256e7674c3SLukasz Luba val = dmc->timings->tDQSCK / clk_period_ps;
11266e7674c3SLukasz Luba val += dmc->timings->tDQSCK % clk_period_ps ? 1 : 0;
11276e7674c3SLukasz Luba val = max(val, dmc->min_tck->tDQSCK);
1128bb0ebc7dSKrzysztof Kozlowski reg = &timing_data_reg_fields[6];
11296e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val);
11306e7674c3SLukasz Luba
11316e7674c3SLukasz Luba val = dmc->timings->tRL / clk_period_ps;
11326e7674c3SLukasz Luba val += dmc->timings->tRL % clk_period_ps ? 1 : 0;
11336e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRL);
1134bb0ebc7dSKrzysztof Kozlowski reg = &timing_data_reg_fields[7];
11356e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val);
11366e7674c3SLukasz Luba
11376e7674c3SLukasz Luba /* power related timings */
11386e7674c3SLukasz Luba val = dmc->timings->tFAW / clk_period_ps;
11396e7674c3SLukasz Luba val += dmc->timings->tFAW % clk_period_ps ? 1 : 0;
11404bff7214SBernard Zhao val = max(val, dmc->min_tck->tFAW);
1141bb0ebc7dSKrzysztof Kozlowski reg = &timing_power_reg_fields[0];
11426e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val);
11436e7674c3SLukasz Luba
11446e7674c3SLukasz Luba val = dmc->timings->tXSR / clk_period_ps;
11456e7674c3SLukasz Luba val += dmc->timings->tXSR % clk_period_ps ? 1 : 0;
11466e7674c3SLukasz Luba val = max(val, dmc->min_tck->tXSR);
1147bb0ebc7dSKrzysztof Kozlowski reg = &timing_power_reg_fields[1];
11486e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val);
11496e7674c3SLukasz Luba
11506e7674c3SLukasz Luba val = dmc->timings->tXP / clk_period_ps;
11516e7674c3SLukasz Luba val += dmc->timings->tXP % clk_period_ps ? 1 : 0;
11526e7674c3SLukasz Luba val = max(val, dmc->min_tck->tXP);
1153bb0ebc7dSKrzysztof Kozlowski reg = &timing_power_reg_fields[2];
11546e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val);
11556e7674c3SLukasz Luba
11566e7674c3SLukasz Luba val = dmc->timings->tCKE / clk_period_ps;
11576e7674c3SLukasz Luba val += dmc->timings->tCKE % clk_period_ps ? 1 : 0;
11586e7674c3SLukasz Luba val = max(val, dmc->min_tck->tCKE);
1159bb0ebc7dSKrzysztof Kozlowski reg = &timing_power_reg_fields[3];
11606e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val);
11616e7674c3SLukasz Luba
11626e7674c3SLukasz Luba val = dmc->timings->tMRD / clk_period_ps;
11636e7674c3SLukasz Luba val += dmc->timings->tMRD % clk_period_ps ? 1 : 0;
11646e7674c3SLukasz Luba val = max(val, dmc->min_tck->tMRD);
1165bb0ebc7dSKrzysztof Kozlowski reg = &timing_power_reg_fields[4];
11666e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val);
11676e7674c3SLukasz Luba
11686e7674c3SLukasz Luba return 0;
11696e7674c3SLukasz Luba }
11706e7674c3SLukasz Luba
11716e7674c3SLukasz Luba /**
11726e7674c3SLukasz Luba * of_get_dram_timings() - helper function for parsing DT settings for DRAM
11736e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be set
11746e7674c3SLukasz Luba *
11756e7674c3SLukasz Luba * The function parses DT entries with DRAM information.
11766e7674c3SLukasz Luba */
of_get_dram_timings(struct exynos5_dmc * dmc)11776e7674c3SLukasz Luba static int of_get_dram_timings(struct exynos5_dmc *dmc)
11786e7674c3SLukasz Luba {
11796e7674c3SLukasz Luba int ret = 0;
11802af13f97SKrzysztof Kozlowski struct device *dev = dmc->dev;
11816e7674c3SLukasz Luba int idx;
11826e7674c3SLukasz Luba u32 freq_mhz, clk_period_ps;
11836e7674c3SLukasz Luba
1184*e2cc3ddaSKrzysztof Kozlowski struct device_node *np_ddr __free(device_node) =
1185*e2cc3ddaSKrzysztof Kozlowski of_parse_phandle(dev->of_node, "device-handle", 0);
11866e7674c3SLukasz Luba if (!np_ddr) {
11872af13f97SKrzysztof Kozlowski dev_warn(dev, "could not find 'device-handle' in DT\n");
11886e7674c3SLukasz Luba return -EINVAL;
11896e7674c3SLukasz Luba }
11906e7674c3SLukasz Luba
11912af13f97SKrzysztof Kozlowski dmc->timing_row = devm_kmalloc_array(dev, TIMING_COUNT,
11926e7674c3SLukasz Luba sizeof(u32), GFP_KERNEL);
1193*e2cc3ddaSKrzysztof Kozlowski if (!dmc->timing_row)
1194*e2cc3ddaSKrzysztof Kozlowski return -ENOMEM;
11956e7674c3SLukasz Luba
11962af13f97SKrzysztof Kozlowski dmc->timing_data = devm_kmalloc_array(dev, TIMING_COUNT,
11976e7674c3SLukasz Luba sizeof(u32), GFP_KERNEL);
1198*e2cc3ddaSKrzysztof Kozlowski if (!dmc->timing_data)
1199*e2cc3ddaSKrzysztof Kozlowski return -ENOMEM;
12006e7674c3SLukasz Luba
12012af13f97SKrzysztof Kozlowski dmc->timing_power = devm_kmalloc_array(dev, TIMING_COUNT,
12026e7674c3SLukasz Luba sizeof(u32), GFP_KERNEL);
1203*e2cc3ddaSKrzysztof Kozlowski if (!dmc->timing_power)
1204*e2cc3ddaSKrzysztof Kozlowski return -ENOMEM;
12056e7674c3SLukasz Luba
12062af13f97SKrzysztof Kozlowski dmc->timings = of_lpddr3_get_ddr_timings(np_ddr, dev,
12076e7674c3SLukasz Luba DDR_TYPE_LPDDR3,
12086e7674c3SLukasz Luba &dmc->timings_arr_size);
12096e7674c3SLukasz Luba if (!dmc->timings) {
12102af13f97SKrzysztof Kozlowski dev_warn(dev, "could not get timings from DT\n");
1211*e2cc3ddaSKrzysztof Kozlowski return -EINVAL;
12126e7674c3SLukasz Luba }
12136e7674c3SLukasz Luba
12142af13f97SKrzysztof Kozlowski dmc->min_tck = of_lpddr3_get_min_tck(np_ddr, dev);
12156e7674c3SLukasz Luba if (!dmc->min_tck) {
12162af13f97SKrzysztof Kozlowski dev_warn(dev, "could not get tck from DT\n");
1217*e2cc3ddaSKrzysztof Kozlowski return -EINVAL;
12186e7674c3SLukasz Luba }
12196e7674c3SLukasz Luba
12206e7674c3SLukasz Luba /* Sorted array of OPPs with frequency ascending */
12216e7674c3SLukasz Luba for (idx = 0; idx < dmc->opp_count; idx++) {
12226e7674c3SLukasz Luba freq_mhz = dmc->opp[idx].freq_hz / 1000000;
12236e7674c3SLukasz Luba clk_period_ps = 1000000 / freq_mhz;
12246e7674c3SLukasz Luba
12256e7674c3SLukasz Luba ret = create_timings_aligned(dmc, &dmc->timing_row[idx],
12266e7674c3SLukasz Luba &dmc->timing_data[idx],
12276e7674c3SLukasz Luba &dmc->timing_power[idx],
12286e7674c3SLukasz Luba clk_period_ps);
12296e7674c3SLukasz Luba }
12306e7674c3SLukasz Luba
12316e7674c3SLukasz Luba
12326e7674c3SLukasz Luba /* Take the highest frequency's timings as 'bypass' */
12336e7674c3SLukasz Luba dmc->bypass_timing_row = dmc->timing_row[idx - 1];
12346e7674c3SLukasz Luba dmc->bypass_timing_data = dmc->timing_data[idx - 1];
12356e7674c3SLukasz Luba dmc->bypass_timing_power = dmc->timing_power[idx - 1];
12366e7674c3SLukasz Luba
12376e7674c3SLukasz Luba return ret;
12386e7674c3SLukasz Luba }
12396e7674c3SLukasz Luba
12406e7674c3SLukasz Luba /**
12416e7674c3SLukasz Luba * exynos5_dmc_init_clks() - Initialize clocks needed for DMC operation.
12426e7674c3SLukasz Luba * @dmc: DMC structure containing needed fields
12436e7674c3SLukasz Luba *
12446e7674c3SLukasz Luba * Get the needed clocks defined in DT device, enable and set the right parents.
12456e7674c3SLukasz Luba * Read current frequency and initialize the initial rate for governor.
12466e7674c3SLukasz Luba */
exynos5_dmc_init_clks(struct exynos5_dmc * dmc)12476e7674c3SLukasz Luba static int exynos5_dmc_init_clks(struct exynos5_dmc *dmc)
12486e7674c3SLukasz Luba {
12496e7674c3SLukasz Luba int ret;
12502af13f97SKrzysztof Kozlowski struct device *dev = dmc->dev;
12516e7674c3SLukasz Luba unsigned long target_volt = 0;
12526e7674c3SLukasz Luba unsigned long target_rate = 0;
12536e7674c3SLukasz Luba unsigned int tmp;
12546e7674c3SLukasz Luba
12552af13f97SKrzysztof Kozlowski dmc->fout_spll = devm_clk_get(dev, "fout_spll");
12566e7674c3SLukasz Luba if (IS_ERR(dmc->fout_spll))
12576e7674c3SLukasz Luba return PTR_ERR(dmc->fout_spll);
12586e7674c3SLukasz Luba
12592af13f97SKrzysztof Kozlowski dmc->fout_bpll = devm_clk_get(dev, "fout_bpll");
12606e7674c3SLukasz Luba if (IS_ERR(dmc->fout_bpll))
12616e7674c3SLukasz Luba return PTR_ERR(dmc->fout_bpll);
12626e7674c3SLukasz Luba
12632af13f97SKrzysztof Kozlowski dmc->mout_mclk_cdrex = devm_clk_get(dev, "mout_mclk_cdrex");
12646e7674c3SLukasz Luba if (IS_ERR(dmc->mout_mclk_cdrex))
12656e7674c3SLukasz Luba return PTR_ERR(dmc->mout_mclk_cdrex);
12666e7674c3SLukasz Luba
12672af13f97SKrzysztof Kozlowski dmc->mout_bpll = devm_clk_get(dev, "mout_bpll");
12686e7674c3SLukasz Luba if (IS_ERR(dmc->mout_bpll))
12696e7674c3SLukasz Luba return PTR_ERR(dmc->mout_bpll);
12706e7674c3SLukasz Luba
12712af13f97SKrzysztof Kozlowski dmc->mout_mx_mspll_ccore = devm_clk_get(dev, "mout_mx_mspll_ccore");
12726e7674c3SLukasz Luba if (IS_ERR(dmc->mout_mx_mspll_ccore))
12736e7674c3SLukasz Luba return PTR_ERR(dmc->mout_mx_mspll_ccore);
12746e7674c3SLukasz Luba
12752af13f97SKrzysztof Kozlowski dmc->mout_spll = devm_clk_get(dev, "ff_dout_spll2");
12766e7674c3SLukasz Luba if (IS_ERR(dmc->mout_spll)) {
12772af13f97SKrzysztof Kozlowski dmc->mout_spll = devm_clk_get(dev, "mout_sclk_spll");
12786e7674c3SLukasz Luba if (IS_ERR(dmc->mout_spll))
12796e7674c3SLukasz Luba return PTR_ERR(dmc->mout_spll);
12806e7674c3SLukasz Luba }
12816e7674c3SLukasz Luba
12826e7674c3SLukasz Luba /*
12836e7674c3SLukasz Luba * Convert frequency to KHz values and set it for the governor.
12846e7674c3SLukasz Luba */
12856e7674c3SLukasz Luba dmc->curr_rate = clk_get_rate(dmc->mout_mclk_cdrex);
12866e7674c3SLukasz Luba dmc->curr_rate = exynos5_dmc_align_init_freq(dmc, dmc->curr_rate);
12876e7674c3SLukasz Luba exynos5_dmc_df_profile.initial_freq = dmc->curr_rate;
12886e7674c3SLukasz Luba
12896e7674c3SLukasz Luba ret = exynos5_dmc_get_volt_freq(dmc, &dmc->curr_rate, &target_rate,
12906e7674c3SLukasz Luba &target_volt, 0);
12916e7674c3SLukasz Luba if (ret)
12926e7674c3SLukasz Luba return ret;
12936e7674c3SLukasz Luba
12946e7674c3SLukasz Luba dmc->curr_volt = target_volt;
12956e7674c3SLukasz Luba
1296132c17c3SKrzysztof Kozlowski ret = clk_set_parent(dmc->mout_mx_mspll_ccore, dmc->mout_spll);
1297132c17c3SKrzysztof Kozlowski if (ret)
1298132c17c3SKrzysztof Kozlowski return ret;
12996e7674c3SLukasz Luba
13006e7674c3SLukasz Luba clk_prepare_enable(dmc->fout_bpll);
13016e7674c3SLukasz Luba clk_prepare_enable(dmc->mout_bpll);
13026e7674c3SLukasz Luba
13036e7674c3SLukasz Luba /*
13046e7674c3SLukasz Luba * Some bootloaders do not set clock routes correctly.
13056e7674c3SLukasz Luba * Stop one path in clocks to PHY.
13066e7674c3SLukasz Luba */
13076e7674c3SLukasz Luba regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, &tmp);
13086e7674c3SLukasz Luba tmp &= ~(BIT(1) | BIT(0));
13096e7674c3SLukasz Luba regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, tmp);
13106e7674c3SLukasz Luba
13116e7674c3SLukasz Luba return 0;
13126e7674c3SLukasz Luba }
13136e7674c3SLukasz Luba
13146e7674c3SLukasz Luba /**
13156e7674c3SLukasz Luba * exynos5_performance_counters_init() - Initializes performance DMC's counters
13166e7674c3SLukasz Luba * @dmc: DMC for which it does the setup
13176e7674c3SLukasz Luba *
13186e7674c3SLukasz Luba * Initialization of performance counters in DMC for estimating usage.
13196e7674c3SLukasz Luba * The counter's values are used for calculation of a memory bandwidth and based
13206e7674c3SLukasz Luba * on that the governor changes the frequency.
13216e7674c3SLukasz Luba * The counters are not used when the governor is GOVERNOR_USERSPACE.
13226e7674c3SLukasz Luba */
exynos5_performance_counters_init(struct exynos5_dmc * dmc)13236e7674c3SLukasz Luba static int exynos5_performance_counters_init(struct exynos5_dmc *dmc)
13246e7674c3SLukasz Luba {
13252af13f97SKrzysztof Kozlowski struct device *dev = dmc->dev;
13266e7674c3SLukasz Luba int ret, i;
13276e7674c3SLukasz Luba
13282af13f97SKrzysztof Kozlowski dmc->num_counters = devfreq_event_get_edev_count(dev, "devfreq-events");
13296e7674c3SLukasz Luba if (dmc->num_counters < 0) {
13302af13f97SKrzysztof Kozlowski dev_err(dev, "could not get devfreq-event counters\n");
13316e7674c3SLukasz Luba return dmc->num_counters;
13326e7674c3SLukasz Luba }
13336e7674c3SLukasz Luba
13342af13f97SKrzysztof Kozlowski dmc->counter = devm_kcalloc(dev, dmc->num_counters,
133556653827SChristophe JAILLET sizeof(*dmc->counter), GFP_KERNEL);
13366e7674c3SLukasz Luba if (!dmc->counter)
13376e7674c3SLukasz Luba return -ENOMEM;
13386e7674c3SLukasz Luba
13396e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) {
13406e7674c3SLukasz Luba dmc->counter[i] =
13412af13f97SKrzysztof Kozlowski devfreq_event_get_edev_by_phandle(dev, "devfreq-events", i);
13426e7674c3SLukasz Luba if (IS_ERR_OR_NULL(dmc->counter[i]))
13436e7674c3SLukasz Luba return -EPROBE_DEFER;
13446e7674c3SLukasz Luba }
13456e7674c3SLukasz Luba
13466e7674c3SLukasz Luba ret = exynos5_counters_enable_edev(dmc);
13476e7674c3SLukasz Luba if (ret < 0) {
13482af13f97SKrzysztof Kozlowski dev_err(dev, "could not enable event counter\n");
13496e7674c3SLukasz Luba return ret;
13506e7674c3SLukasz Luba }
13516e7674c3SLukasz Luba
13526e7674c3SLukasz Luba ret = exynos5_counters_set_event(dmc);
13536e7674c3SLukasz Luba if (ret < 0) {
13546e7674c3SLukasz Luba exynos5_counters_disable_edev(dmc);
13552af13f97SKrzysztof Kozlowski dev_err(dev, "could not set event counter\n");
13566e7674c3SLukasz Luba return ret;
13576e7674c3SLukasz Luba }
13586e7674c3SLukasz Luba
13596e7674c3SLukasz Luba return 0;
13606e7674c3SLukasz Luba }
13616e7674c3SLukasz Luba
13626e7674c3SLukasz Luba /**
13636e7674c3SLukasz Luba * exynos5_dmc_set_pause_on_switching() - Controls a pause feature in DMC
13646e7674c3SLukasz Luba * @dmc: device which is used for changing this feature
13656e7674c3SLukasz Luba *
13666e7674c3SLukasz Luba * There is a need of pausing DREX DMC when divider or MUX in clock tree
13676e7674c3SLukasz Luba * changes its configuration. In such situation access to the memory is blocked
13686e7674c3SLukasz Luba * in DMC automatically. This feature is used when clock frequency change
13696e7674c3SLukasz Luba * request appears and touches clock tree.
13706e7674c3SLukasz Luba */
exynos5_dmc_set_pause_on_switching(struct exynos5_dmc * dmc)13716e7674c3SLukasz Luba static inline int exynos5_dmc_set_pause_on_switching(struct exynos5_dmc *dmc)
13726e7674c3SLukasz Luba {
13736e7674c3SLukasz Luba unsigned int val;
13746e7674c3SLukasz Luba int ret;
13756e7674c3SLukasz Luba
13766e7674c3SLukasz Luba ret = regmap_read(dmc->clk_regmap, CDREX_PAUSE, &val);
13776e7674c3SLukasz Luba if (ret)
13786e7674c3SLukasz Luba return ret;
13796e7674c3SLukasz Luba
13806e7674c3SLukasz Luba val |= 1UL;
13816e7674c3SLukasz Luba regmap_write(dmc->clk_regmap, CDREX_PAUSE, val);
13826e7674c3SLukasz Luba
13836e7674c3SLukasz Luba return 0;
13846e7674c3SLukasz Luba }
13856e7674c3SLukasz Luba
dmc_irq_thread(int irq,void * priv)1386bbf91886SLukasz Luba static irqreturn_t dmc_irq_thread(int irq, void *priv)
1387bbf91886SLukasz Luba {
1388bbf91886SLukasz Luba int res;
1389bbf91886SLukasz Luba struct exynos5_dmc *dmc = priv;
1390bbf91886SLukasz Luba
1391bbf91886SLukasz Luba mutex_lock(&dmc->df->lock);
1392bbf91886SLukasz Luba exynos5_dmc_perf_events_check(dmc);
1393bbf91886SLukasz Luba res = update_devfreq(dmc->df);
1394108c31e7SBernard Zhao mutex_unlock(&dmc->df->lock);
1395108c31e7SBernard Zhao
1396bbf91886SLukasz Luba if (res)
1397bbf91886SLukasz Luba dev_warn(dmc->dev, "devfreq failed with %d\n", res);
1398bbf91886SLukasz Luba
1399bbf91886SLukasz Luba return IRQ_HANDLED;
1400bbf91886SLukasz Luba }
1401bbf91886SLukasz Luba
14026e7674c3SLukasz Luba /**
14036e7674c3SLukasz Luba * exynos5_dmc_probe() - Probe function for the DMC driver
14046e7674c3SLukasz Luba * @pdev: platform device for which the driver is going to be initialized
14056e7674c3SLukasz Luba *
14066e7674c3SLukasz Luba * Initialize basic components: clocks, regulators, performance counters, etc.
14076e7674c3SLukasz Luba * Read out product version and based on the information setup
14086e7674c3SLukasz Luba * internal structures for the controller (frequency and voltage) and for DRAM
14096e7674c3SLukasz Luba * memory parameters: timings for each operating frequency.
14106e7674c3SLukasz Luba * Register new devfreq device for controlling DVFS of the DMC.
14116e7674c3SLukasz Luba */
exynos5_dmc_probe(struct platform_device * pdev)14126e7674c3SLukasz Luba static int exynos5_dmc_probe(struct platform_device *pdev)
14136e7674c3SLukasz Luba {
14146e7674c3SLukasz Luba int ret = 0;
14156e7674c3SLukasz Luba struct device *dev = &pdev->dev;
14166e7674c3SLukasz Luba struct device_node *np = dev->of_node;
14176e7674c3SLukasz Luba struct exynos5_dmc *dmc;
1418bbf91886SLukasz Luba int irq[2];
14196e7674c3SLukasz Luba
14206e7674c3SLukasz Luba dmc = devm_kzalloc(dev, sizeof(*dmc), GFP_KERNEL);
14216e7674c3SLukasz Luba if (!dmc)
14226e7674c3SLukasz Luba return -ENOMEM;
14236e7674c3SLukasz Luba
14246e7674c3SLukasz Luba mutex_init(&dmc->lock);
14256e7674c3SLukasz Luba
14266e7674c3SLukasz Luba dmc->dev = dev;
14276e7674c3SLukasz Luba platform_set_drvdata(pdev, dmc);
14286e7674c3SLukasz Luba
14295383953fSYangtao Li dmc->base_drexi0 = devm_platform_ioremap_resource(pdev, 0);
14306e7674c3SLukasz Luba if (IS_ERR(dmc->base_drexi0))
14316e7674c3SLukasz Luba return PTR_ERR(dmc->base_drexi0);
14326e7674c3SLukasz Luba
14335383953fSYangtao Li dmc->base_drexi1 = devm_platform_ioremap_resource(pdev, 1);
14346e7674c3SLukasz Luba if (IS_ERR(dmc->base_drexi1))
14356e7674c3SLukasz Luba return PTR_ERR(dmc->base_drexi1);
14366e7674c3SLukasz Luba
14376e7674c3SLukasz Luba dmc->clk_regmap = syscon_regmap_lookup_by_phandle(np,
14386e7674c3SLukasz Luba "samsung,syscon-clk");
14396e7674c3SLukasz Luba if (IS_ERR(dmc->clk_regmap))
14406e7674c3SLukasz Luba return PTR_ERR(dmc->clk_regmap);
14416e7674c3SLukasz Luba
14426e7674c3SLukasz Luba ret = exynos5_init_freq_table(dmc, &exynos5_dmc_df_profile);
14436e7674c3SLukasz Luba if (ret) {
14446e7674c3SLukasz Luba dev_warn(dev, "couldn't initialize frequency settings\n");
14456e7674c3SLukasz Luba return ret;
14466e7674c3SLukasz Luba }
14476e7674c3SLukasz Luba
14486e7674c3SLukasz Luba dmc->vdd_mif = devm_regulator_get(dev, "vdd");
14496e7674c3SLukasz Luba if (IS_ERR(dmc->vdd_mif)) {
14506e7674c3SLukasz Luba ret = PTR_ERR(dmc->vdd_mif);
14516e7674c3SLukasz Luba return ret;
14526e7674c3SLukasz Luba }
14536e7674c3SLukasz Luba
14546e7674c3SLukasz Luba ret = exynos5_dmc_init_clks(dmc);
14556e7674c3SLukasz Luba if (ret)
14566e7674c3SLukasz Luba return ret;
14576e7674c3SLukasz Luba
14586e7674c3SLukasz Luba ret = of_get_dram_timings(dmc);
14596e7674c3SLukasz Luba if (ret) {
14606e7674c3SLukasz Luba dev_warn(dev, "couldn't initialize timings settings\n");
14616e7674c3SLukasz Luba goto remove_clocks;
14626e7674c3SLukasz Luba }
14636e7674c3SLukasz Luba
1464bbf91886SLukasz Luba ret = exynos5_dmc_set_pause_on_switching(dmc);
1465bbf91886SLukasz Luba if (ret) {
1466bbf91886SLukasz Luba dev_warn(dev, "couldn't get access to PAUSE register\n");
1467bbf91886SLukasz Luba goto remove_clocks;
1468bbf91886SLukasz Luba }
1469bbf91886SLukasz Luba
1470bbf91886SLukasz Luba /* There is two modes in which the driver works: polling or IRQ */
1471bbf91886SLukasz Luba irq[0] = platform_get_irq_byname(pdev, "drex_0");
1472bbf91886SLukasz Luba irq[1] = platform_get_irq_byname(pdev, "drex_1");
14734fc9a047SLukasz Luba if (irq[0] > 0 && irq[1] > 0 && irqmode) {
1474bbf91886SLukasz Luba ret = devm_request_threaded_irq(dev, irq[0], NULL,
1475bbf91886SLukasz Luba dmc_irq_thread, IRQF_ONESHOT,
1476bbf91886SLukasz Luba dev_name(dev), dmc);
1477bbf91886SLukasz Luba if (ret) {
1478bbf91886SLukasz Luba dev_err(dev, "couldn't grab IRQ\n");
1479bbf91886SLukasz Luba goto remove_clocks;
1480bbf91886SLukasz Luba }
1481bbf91886SLukasz Luba
1482bbf91886SLukasz Luba ret = devm_request_threaded_irq(dev, irq[1], NULL,
1483bbf91886SLukasz Luba dmc_irq_thread, IRQF_ONESHOT,
1484bbf91886SLukasz Luba dev_name(dev), dmc);
1485bbf91886SLukasz Luba if (ret) {
1486bbf91886SLukasz Luba dev_err(dev, "couldn't grab IRQ\n");
1487bbf91886SLukasz Luba goto remove_clocks;
1488bbf91886SLukasz Luba }
1489bbf91886SLukasz Luba
1490bbf91886SLukasz Luba /*
1491bbf91886SLukasz Luba * Setup default thresholds for the devfreq governor.
1492bbf91886SLukasz Luba * The values are chosen based on experiments.
1493bbf91886SLukasz Luba */
1494bbf91886SLukasz Luba dmc->gov_data.upthreshold = 55;
1495bbf91886SLukasz Luba dmc->gov_data.downdifferential = 5;
1496bbf91886SLukasz Luba
1497bbf91886SLukasz Luba exynos5_dmc_enable_perf_events(dmc);
1498bbf91886SLukasz Luba
1499bbf91886SLukasz Luba dmc->in_irq_mode = 1;
1500bbf91886SLukasz Luba } else {
15016e7674c3SLukasz Luba ret = exynos5_performance_counters_init(dmc);
15026e7674c3SLukasz Luba if (ret) {
15036e7674c3SLukasz Luba dev_warn(dev, "couldn't probe performance counters\n");
15046e7674c3SLukasz Luba goto remove_clocks;
15056e7674c3SLukasz Luba }
15066e7674c3SLukasz Luba
15076e7674c3SLukasz Luba /*
15086e7674c3SLukasz Luba * Setup default thresholds for the devfreq governor.
15096e7674c3SLukasz Luba * The values are chosen based on experiments.
15106e7674c3SLukasz Luba */
151174ca9e46SLukasz Luba dmc->gov_data.upthreshold = 10;
15126e7674c3SLukasz Luba dmc->gov_data.downdifferential = 5;
15136e7674c3SLukasz Luba
151474ca9e46SLukasz Luba exynos5_dmc_df_profile.polling_ms = 100;
1515bbf91886SLukasz Luba }
1516bbf91886SLukasz Luba
15176e7674c3SLukasz Luba dmc->df = devm_devfreq_add_device(dev, &exynos5_dmc_df_profile,
15186e7674c3SLukasz Luba DEVFREQ_GOV_SIMPLE_ONDEMAND,
15196e7674c3SLukasz Luba &dmc->gov_data);
15206e7674c3SLukasz Luba
15216e7674c3SLukasz Luba if (IS_ERR(dmc->df)) {
15226e7674c3SLukasz Luba ret = PTR_ERR(dmc->df);
15236e7674c3SLukasz Luba goto err_devfreq_add;
15246e7674c3SLukasz Luba }
15256e7674c3SLukasz Luba
1526bbf91886SLukasz Luba if (dmc->in_irq_mode)
1527bbf91886SLukasz Luba exynos5_dmc_start_perf_events(dmc, PERF_COUNTER_START_VALUE);
1528bbf91886SLukasz Luba
15294fc9a047SLukasz Luba dev_info(dev, "DMC initialized, in irq mode: %d\n", dmc->in_irq_mode);
15306e7674c3SLukasz Luba
15316e7674c3SLukasz Luba return 0;
15326e7674c3SLukasz Luba
15336e7674c3SLukasz Luba err_devfreq_add:
1534bbf91886SLukasz Luba if (dmc->in_irq_mode)
1535bbf91886SLukasz Luba exynos5_dmc_disable_perf_events(dmc);
1536bbf91886SLukasz Luba else
15376e7674c3SLukasz Luba exynos5_counters_disable_edev(dmc);
15386e7674c3SLukasz Luba remove_clocks:
15396e7674c3SLukasz Luba clk_disable_unprepare(dmc->mout_bpll);
15406e7674c3SLukasz Luba clk_disable_unprepare(dmc->fout_bpll);
15416e7674c3SLukasz Luba
15426e7674c3SLukasz Luba return ret;
15436e7674c3SLukasz Luba }
15446e7674c3SLukasz Luba
15456e7674c3SLukasz Luba /**
15466e7674c3SLukasz Luba * exynos5_dmc_remove() - Remove function for the platform device
15476e7674c3SLukasz Luba * @pdev: platform device which is going to be removed
15486e7674c3SLukasz Luba *
15496e7674c3SLukasz Luba * The function relies on 'devm' framework function which automatically
15506e7674c3SLukasz Luba * clean the device's resources. It just calls explicitly disable function for
15516e7674c3SLukasz Luba * the performance counters.
15526e7674c3SLukasz Luba */
exynos5_dmc_remove(struct platform_device * pdev)15538013408eSUwe Kleine-König static void exynos5_dmc_remove(struct platform_device *pdev)
15546e7674c3SLukasz Luba {
15556e7674c3SLukasz Luba struct exynos5_dmc *dmc = dev_get_drvdata(&pdev->dev);
15566e7674c3SLukasz Luba
1557bbf91886SLukasz Luba if (dmc->in_irq_mode)
1558bbf91886SLukasz Luba exynos5_dmc_disable_perf_events(dmc);
1559bbf91886SLukasz Luba else
15606e7674c3SLukasz Luba exynos5_counters_disable_edev(dmc);
15616e7674c3SLukasz Luba
15626e7674c3SLukasz Luba clk_disable_unprepare(dmc->mout_bpll);
15636e7674c3SLukasz Luba clk_disable_unprepare(dmc->fout_bpll);
15646e7674c3SLukasz Luba }
15656e7674c3SLukasz Luba
15666e7674c3SLukasz Luba static const struct of_device_id exynos5_dmc_of_match[] = {
15676e7674c3SLukasz Luba { .compatible = "samsung,exynos5422-dmc", },
15686e7674c3SLukasz Luba { },
15696e7674c3SLukasz Luba };
15706e7674c3SLukasz Luba MODULE_DEVICE_TABLE(of, exynos5_dmc_of_match);
15716e7674c3SLukasz Luba
15726e7674c3SLukasz Luba static struct platform_driver exynos5_dmc_platdrv = {
15736e7674c3SLukasz Luba .probe = exynos5_dmc_probe,
15748013408eSUwe Kleine-König .remove_new = exynos5_dmc_remove,
15756e7674c3SLukasz Luba .driver = {
15766e7674c3SLukasz Luba .name = "exynos5-dmc",
15776e7674c3SLukasz Luba .of_match_table = exynos5_dmc_of_match,
15786e7674c3SLukasz Luba },
15796e7674c3SLukasz Luba };
15806e7674c3SLukasz Luba module_platform_driver(exynos5_dmc_platdrv);
15816e7674c3SLukasz Luba MODULE_DESCRIPTION("Driver for Exynos5422 Dynamic Memory Controller dynamic frequency and voltage change");
15826e7674c3SLukasz Luba MODULE_LICENSE("GPL v2");
15836e7674c3SLukasz Luba MODULE_AUTHOR("Lukasz Luba");
1584