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> 116e7674c3SLukasz Luba #include <linux/io.h> 126e7674c3SLukasz Luba #include <linux/mfd/syscon.h> 136e7674c3SLukasz Luba #include <linux/module.h> 146e7674c3SLukasz Luba #include <linux/of_device.h> 156e7674c3SLukasz Luba #include <linux/pm_opp.h> 166e7674c3SLukasz Luba #include <linux/platform_device.h> 176e7674c3SLukasz Luba #include <linux/regmap.h> 186e7674c3SLukasz Luba #include <linux/regulator/consumer.h> 196e7674c3SLukasz Luba #include <linux/slab.h> 206e7674c3SLukasz Luba #include "../jedec_ddr.h" 216e7674c3SLukasz Luba #include "../of_memory.h" 226e7674c3SLukasz Luba 236e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGAREF (0x0030) 246e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGROW0 (0x0034) 256e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGDATA0 (0x0038) 266e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGPOWER0 (0x003C) 276e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGROW1 (0x00E4) 286e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGDATA1 (0x00E8) 296e7674c3SLukasz Luba #define EXYNOS5_DREXI_TIMINGPOWER1 (0x00EC) 306e7674c3SLukasz Luba #define CDREX_PAUSE (0x2091c) 316e7674c3SLukasz Luba #define CDREX_LPDDR3PHY_CON3 (0x20a20) 326e7674c3SLukasz Luba #define CDREX_LPDDR3PHY_CLKM_SRC (0x20700) 336e7674c3SLukasz Luba #define EXYNOS5_TIMING_SET_SWI BIT(28) 346e7674c3SLukasz Luba #define USE_MX_MSPLL_TIMINGS (1) 356e7674c3SLukasz Luba #define USE_BPLL_TIMINGS (0) 366e7674c3SLukasz Luba #define EXYNOS5_AREF_NORMAL (0x2e) 376e7674c3SLukasz Luba 386e7674c3SLukasz Luba /** 396e7674c3SLukasz Luba * struct dmc_opp_table - Operating level desciption 406e7674c3SLukasz Luba * 416e7674c3SLukasz Luba * Covers frequency and voltage settings of the DMC operating mode. 426e7674c3SLukasz Luba */ 436e7674c3SLukasz Luba struct dmc_opp_table { 446e7674c3SLukasz Luba u32 freq_hz; 456e7674c3SLukasz Luba u32 volt_uv; 466e7674c3SLukasz Luba }; 476e7674c3SLukasz Luba 486e7674c3SLukasz Luba /** 496e7674c3SLukasz Luba * struct exynos5_dmc - main structure describing DMC device 506e7674c3SLukasz Luba * 516e7674c3SLukasz Luba * The main structure for the Dynamic Memory Controller which covers clocks, 526e7674c3SLukasz Luba * memory regions, HW information, parameters and current operating mode. 536e7674c3SLukasz Luba */ 546e7674c3SLukasz Luba struct exynos5_dmc { 556e7674c3SLukasz Luba struct device *dev; 566e7674c3SLukasz Luba struct devfreq *df; 576e7674c3SLukasz Luba struct devfreq_simple_ondemand_data gov_data; 586e7674c3SLukasz Luba void __iomem *base_drexi0; 596e7674c3SLukasz Luba void __iomem *base_drexi1; 606e7674c3SLukasz Luba struct regmap *clk_regmap; 616e7674c3SLukasz Luba struct mutex lock; 626e7674c3SLukasz Luba unsigned long curr_rate; 636e7674c3SLukasz Luba unsigned long curr_volt; 646e7674c3SLukasz Luba unsigned long bypass_rate; 656e7674c3SLukasz Luba struct dmc_opp_table *opp; 666e7674c3SLukasz Luba struct dmc_opp_table opp_bypass; 676e7674c3SLukasz Luba int opp_count; 686e7674c3SLukasz Luba u32 timings_arr_size; 696e7674c3SLukasz Luba u32 *timing_row; 706e7674c3SLukasz Luba u32 *timing_data; 716e7674c3SLukasz Luba u32 *timing_power; 726e7674c3SLukasz Luba const struct lpddr3_timings *timings; 736e7674c3SLukasz Luba const struct lpddr3_min_tck *min_tck; 746e7674c3SLukasz Luba u32 bypass_timing_row; 756e7674c3SLukasz Luba u32 bypass_timing_data; 766e7674c3SLukasz Luba u32 bypass_timing_power; 776e7674c3SLukasz Luba struct regulator *vdd_mif; 786e7674c3SLukasz Luba struct clk *fout_spll; 796e7674c3SLukasz Luba struct clk *fout_bpll; 806e7674c3SLukasz Luba struct clk *mout_spll; 816e7674c3SLukasz Luba struct clk *mout_bpll; 826e7674c3SLukasz Luba struct clk *mout_mclk_cdrex; 836e7674c3SLukasz Luba struct clk *mout_mx_mspll_ccore; 846e7674c3SLukasz Luba struct clk *mx_mspll_ccore_phy; 856e7674c3SLukasz Luba struct clk *mout_mx_mspll_ccore_phy; 866e7674c3SLukasz Luba struct devfreq_event_dev **counter; 876e7674c3SLukasz Luba int num_counters; 886e7674c3SLukasz Luba }; 896e7674c3SLukasz Luba 906e7674c3SLukasz Luba #define TIMING_FIELD(t_name, t_bit_beg, t_bit_end) \ 916e7674c3SLukasz Luba { .name = t_name, .bit_beg = t_bit_beg, .bit_end = t_bit_end } 926e7674c3SLukasz Luba 936e7674c3SLukasz Luba #define TIMING_VAL2REG(timing, t_val) \ 946e7674c3SLukasz Luba ({ \ 956e7674c3SLukasz Luba u32 __val; \ 966e7674c3SLukasz Luba __val = (t_val) << (timing)->bit_beg; \ 976e7674c3SLukasz Luba __val; \ 986e7674c3SLukasz Luba }) 996e7674c3SLukasz Luba 1006e7674c3SLukasz Luba struct timing_reg { 1016e7674c3SLukasz Luba char *name; 1026e7674c3SLukasz Luba int bit_beg; 1036e7674c3SLukasz Luba int bit_end; 1046e7674c3SLukasz Luba unsigned int val; 1056e7674c3SLukasz Luba }; 1066e7674c3SLukasz Luba 1076e7674c3SLukasz Luba static const struct timing_reg timing_row[] = { 1086e7674c3SLukasz Luba TIMING_FIELD("tRFC", 24, 31), 1096e7674c3SLukasz Luba TIMING_FIELD("tRRD", 20, 23), 1106e7674c3SLukasz Luba TIMING_FIELD("tRP", 16, 19), 1116e7674c3SLukasz Luba TIMING_FIELD("tRCD", 12, 15), 1126e7674c3SLukasz Luba TIMING_FIELD("tRC", 6, 11), 1136e7674c3SLukasz Luba TIMING_FIELD("tRAS", 0, 5), 1146e7674c3SLukasz Luba }; 1156e7674c3SLukasz Luba 1166e7674c3SLukasz Luba static const struct timing_reg timing_data[] = { 1176e7674c3SLukasz Luba TIMING_FIELD("tWTR", 28, 31), 1186e7674c3SLukasz Luba TIMING_FIELD("tWR", 24, 27), 1196e7674c3SLukasz Luba TIMING_FIELD("tRTP", 20, 23), 1206e7674c3SLukasz Luba TIMING_FIELD("tW2W-C2C", 14, 14), 1216e7674c3SLukasz Luba TIMING_FIELD("tR2R-C2C", 12, 12), 1226e7674c3SLukasz Luba TIMING_FIELD("WL", 8, 11), 1236e7674c3SLukasz Luba TIMING_FIELD("tDQSCK", 4, 7), 1246e7674c3SLukasz Luba TIMING_FIELD("RL", 0, 3), 1256e7674c3SLukasz Luba }; 1266e7674c3SLukasz Luba 1276e7674c3SLukasz Luba static const struct timing_reg timing_power[] = { 1286e7674c3SLukasz Luba TIMING_FIELD("tFAW", 26, 31), 1296e7674c3SLukasz Luba TIMING_FIELD("tXSR", 16, 25), 1306e7674c3SLukasz Luba TIMING_FIELD("tXP", 8, 15), 1316e7674c3SLukasz Luba TIMING_FIELD("tCKE", 4, 7), 1326e7674c3SLukasz Luba TIMING_FIELD("tMRD", 0, 3), 1336e7674c3SLukasz Luba }; 1346e7674c3SLukasz Luba 1356e7674c3SLukasz Luba #define TIMING_COUNT (ARRAY_SIZE(timing_row) + ARRAY_SIZE(timing_data) + \ 1366e7674c3SLukasz Luba ARRAY_SIZE(timing_power)) 1376e7674c3SLukasz Luba 1386e7674c3SLukasz Luba static int exynos5_counters_set_event(struct exynos5_dmc *dmc) 1396e7674c3SLukasz Luba { 1406e7674c3SLukasz Luba int i, ret; 1416e7674c3SLukasz Luba 1426e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) { 1436e7674c3SLukasz Luba if (!dmc->counter[i]) 1446e7674c3SLukasz Luba continue; 1456e7674c3SLukasz Luba ret = devfreq_event_set_event(dmc->counter[i]); 1466e7674c3SLukasz Luba if (ret < 0) 1476e7674c3SLukasz Luba return ret; 1486e7674c3SLukasz Luba } 1496e7674c3SLukasz Luba return 0; 1506e7674c3SLukasz Luba } 1516e7674c3SLukasz Luba 1526e7674c3SLukasz Luba static int exynos5_counters_enable_edev(struct exynos5_dmc *dmc) 1536e7674c3SLukasz Luba { 1546e7674c3SLukasz Luba int i, ret; 1556e7674c3SLukasz Luba 1566e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) { 1576e7674c3SLukasz Luba if (!dmc->counter[i]) 1586e7674c3SLukasz Luba continue; 1596e7674c3SLukasz Luba ret = devfreq_event_enable_edev(dmc->counter[i]); 1606e7674c3SLukasz Luba if (ret < 0) 1616e7674c3SLukasz Luba return ret; 1626e7674c3SLukasz Luba } 1636e7674c3SLukasz Luba return 0; 1646e7674c3SLukasz Luba } 1656e7674c3SLukasz Luba 1666e7674c3SLukasz Luba static int exynos5_counters_disable_edev(struct exynos5_dmc *dmc) 1676e7674c3SLukasz Luba { 1686e7674c3SLukasz Luba int i, ret; 1696e7674c3SLukasz Luba 1706e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) { 1716e7674c3SLukasz Luba if (!dmc->counter[i]) 1726e7674c3SLukasz Luba continue; 1736e7674c3SLukasz Luba ret = devfreq_event_disable_edev(dmc->counter[i]); 1746e7674c3SLukasz Luba if (ret < 0) 1756e7674c3SLukasz Luba return ret; 1766e7674c3SLukasz Luba } 1776e7674c3SLukasz Luba return 0; 1786e7674c3SLukasz Luba } 1796e7674c3SLukasz Luba 1806e7674c3SLukasz Luba /** 1816e7674c3SLukasz Luba * find_target_freq_id() - Finds requested frequency in local DMC configuration 1826e7674c3SLukasz Luba * @dmc: device for which the information is checked 1836e7674c3SLukasz Luba * @target_rate: requested frequency in KHz 1846e7674c3SLukasz Luba * 1856e7674c3SLukasz Luba * Seeks in the local DMC driver structure for the requested frequency value 1866e7674c3SLukasz Luba * and returns index or error value. 1876e7674c3SLukasz Luba */ 1886e7674c3SLukasz Luba static int find_target_freq_idx(struct exynos5_dmc *dmc, 1896e7674c3SLukasz Luba unsigned long target_rate) 1906e7674c3SLukasz Luba { 1916e7674c3SLukasz Luba int i; 1926e7674c3SLukasz Luba 1936e7674c3SLukasz Luba for (i = dmc->opp_count - 1; i >= 0; i--) 1946e7674c3SLukasz Luba if (dmc->opp[i].freq_hz <= target_rate) 1956e7674c3SLukasz Luba return i; 1966e7674c3SLukasz Luba 1976e7674c3SLukasz Luba return -EINVAL; 1986e7674c3SLukasz Luba } 1996e7674c3SLukasz Luba 2006e7674c3SLukasz Luba /** 2016e7674c3SLukasz Luba * exynos5_switch_timing_regs() - Changes bank register set for DRAM timings 2026e7674c3SLukasz Luba * @dmc: device for which the new settings is going to be applied 2036e7674c3SLukasz Luba * @set: boolean variable passing set value 2046e7674c3SLukasz Luba * 2056e7674c3SLukasz Luba * Changes the register set, which holds timing parameters. 2066e7674c3SLukasz Luba * There is two register sets: 0 and 1. The register set 0 2076e7674c3SLukasz Luba * is used in normal operation when the clock is provided from main PLL. 2086e7674c3SLukasz Luba * The bank register set 1 is used when the main PLL frequency is going to be 2096e7674c3SLukasz Luba * changed and the clock is taken from alternative, stable source. 2106e7674c3SLukasz Luba * This function switches between these banks according to the 2116e7674c3SLukasz Luba * currently used clock source. 2126e7674c3SLukasz Luba */ 2136e7674c3SLukasz Luba static void exynos5_switch_timing_regs(struct exynos5_dmc *dmc, bool set) 2146e7674c3SLukasz Luba { 2156e7674c3SLukasz Luba unsigned int reg; 2166e7674c3SLukasz Luba int ret; 2176e7674c3SLukasz Luba 2186e7674c3SLukasz Luba ret = regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, ®); 2196e7674c3SLukasz Luba 2206e7674c3SLukasz Luba if (set) 2216e7674c3SLukasz Luba reg |= EXYNOS5_TIMING_SET_SWI; 2226e7674c3SLukasz Luba else 2236e7674c3SLukasz Luba reg &= ~EXYNOS5_TIMING_SET_SWI; 2246e7674c3SLukasz Luba 2256e7674c3SLukasz Luba regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CON3, reg); 2266e7674c3SLukasz Luba } 2276e7674c3SLukasz Luba 2286e7674c3SLukasz Luba /** 2296e7674c3SLukasz Luba * exynos5_init_freq_table() - Initialized PM OPP framework 2306e7674c3SLukasz Luba * @dmc: DMC device for which the frequencies are used for OPP init 2316e7674c3SLukasz Luba * @profile: devfreq device's profile 2326e7674c3SLukasz Luba * 2336e7674c3SLukasz Luba * Populate the devfreq device's OPP table based on current frequency, voltage. 2346e7674c3SLukasz Luba */ 2356e7674c3SLukasz Luba static int exynos5_init_freq_table(struct exynos5_dmc *dmc, 2366e7674c3SLukasz Luba struct devfreq_dev_profile *profile) 2376e7674c3SLukasz Luba { 2386e7674c3SLukasz Luba int i, ret; 2396e7674c3SLukasz Luba int idx; 2406e7674c3SLukasz Luba unsigned long freq; 2416e7674c3SLukasz Luba 2426e7674c3SLukasz Luba ret = dev_pm_opp_of_add_table(dmc->dev); 2436e7674c3SLukasz Luba if (ret < 0) { 2446e7674c3SLukasz Luba dev_err(dmc->dev, "Failed to get OPP table\n"); 2456e7674c3SLukasz Luba return ret; 2466e7674c3SLukasz Luba } 2476e7674c3SLukasz Luba 2486e7674c3SLukasz Luba dmc->opp_count = dev_pm_opp_get_opp_count(dmc->dev); 2496e7674c3SLukasz Luba 2506e7674c3SLukasz Luba dmc->opp = devm_kmalloc_array(dmc->dev, dmc->opp_count, 2516e7674c3SLukasz Luba sizeof(struct dmc_opp_table), GFP_KERNEL); 2526e7674c3SLukasz Luba if (!dmc->opp) 2536e7674c3SLukasz Luba goto err_opp; 2546e7674c3SLukasz Luba 2556e7674c3SLukasz Luba idx = dmc->opp_count - 1; 2566e7674c3SLukasz Luba for (i = 0, freq = ULONG_MAX; i < dmc->opp_count; i++, freq--) { 2576e7674c3SLukasz Luba struct dev_pm_opp *opp; 2586e7674c3SLukasz Luba 2596e7674c3SLukasz Luba opp = dev_pm_opp_find_freq_floor(dmc->dev, &freq); 2606e7674c3SLukasz Luba if (IS_ERR(opp)) 261*d51e6a69SLukasz Luba goto err_opp; 2626e7674c3SLukasz Luba 2636e7674c3SLukasz Luba dmc->opp[idx - i].freq_hz = freq; 2646e7674c3SLukasz Luba dmc->opp[idx - i].volt_uv = dev_pm_opp_get_voltage(opp); 2656e7674c3SLukasz Luba 2666e7674c3SLukasz Luba dev_pm_opp_put(opp); 2676e7674c3SLukasz Luba } 2686e7674c3SLukasz Luba 2696e7674c3SLukasz Luba return 0; 2706e7674c3SLukasz Luba 2716e7674c3SLukasz Luba err_opp: 2726e7674c3SLukasz Luba dev_pm_opp_of_remove_table(dmc->dev); 2736e7674c3SLukasz Luba 2746e7674c3SLukasz Luba return -EINVAL; 2756e7674c3SLukasz Luba } 2766e7674c3SLukasz Luba 2776e7674c3SLukasz Luba /** 2786e7674c3SLukasz Luba * exynos5_set_bypass_dram_timings() - Low-level changes of the DRAM timings 2796e7674c3SLukasz Luba * @dmc: device for which the new settings is going to be applied 2806e7674c3SLukasz Luba * @param: DRAM parameters which passes timing data 2816e7674c3SLukasz Luba * 2826e7674c3SLukasz Luba * Low-level function for changing timings for DRAM memory clocking from 2836e7674c3SLukasz Luba * 'bypass' clock source (fixed frequency @400MHz). 2846e7674c3SLukasz Luba * It uses timing bank registers set 1. 2856e7674c3SLukasz Luba */ 2866e7674c3SLukasz Luba static void exynos5_set_bypass_dram_timings(struct exynos5_dmc *dmc) 2876e7674c3SLukasz Luba { 2886e7674c3SLukasz Luba writel(EXYNOS5_AREF_NORMAL, 2896e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF); 2906e7674c3SLukasz Luba 2916e7674c3SLukasz Luba writel(dmc->bypass_timing_row, 2926e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW1); 2936e7674c3SLukasz Luba writel(dmc->bypass_timing_row, 2946e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW1); 2956e7674c3SLukasz Luba writel(dmc->bypass_timing_data, 2966e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA1); 2976e7674c3SLukasz Luba writel(dmc->bypass_timing_data, 2986e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA1); 2996e7674c3SLukasz Luba writel(dmc->bypass_timing_power, 3006e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER1); 3016e7674c3SLukasz Luba writel(dmc->bypass_timing_power, 3026e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER1); 3036e7674c3SLukasz Luba } 3046e7674c3SLukasz Luba 3056e7674c3SLukasz Luba /** 3066e7674c3SLukasz Luba * exynos5_dram_change_timings() - Low-level changes of the DRAM final timings 3076e7674c3SLukasz Luba * @dmc: device for which the new settings is going to be applied 3086e7674c3SLukasz Luba * @target_rate: target frequency of the DMC 3096e7674c3SLukasz Luba * 3106e7674c3SLukasz Luba * Low-level function for changing timings for DRAM memory operating from main 3116e7674c3SLukasz Luba * clock source (BPLL), which can have different frequencies. Thus, each 3126e7674c3SLukasz Luba * frequency must have corresponding timings register values in order to keep 3136e7674c3SLukasz Luba * the needed delays. 3146e7674c3SLukasz Luba * It uses timing bank registers set 0. 3156e7674c3SLukasz Luba */ 3166e7674c3SLukasz Luba static int exynos5_dram_change_timings(struct exynos5_dmc *dmc, 3176e7674c3SLukasz Luba unsigned long target_rate) 3186e7674c3SLukasz Luba { 3196e7674c3SLukasz Luba int idx; 3206e7674c3SLukasz Luba 3216e7674c3SLukasz Luba for (idx = dmc->opp_count - 1; idx >= 0; idx--) 3226e7674c3SLukasz Luba if (dmc->opp[idx].freq_hz <= target_rate) 3236e7674c3SLukasz Luba break; 3246e7674c3SLukasz Luba 3256e7674c3SLukasz Luba if (idx < 0) 3266e7674c3SLukasz Luba return -EINVAL; 3276e7674c3SLukasz Luba 3286e7674c3SLukasz Luba writel(EXYNOS5_AREF_NORMAL, 3296e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGAREF); 3306e7674c3SLukasz Luba 3316e7674c3SLukasz Luba writel(dmc->timing_row[idx], 3326e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGROW0); 3336e7674c3SLukasz Luba writel(dmc->timing_row[idx], 3346e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGROW0); 3356e7674c3SLukasz Luba writel(dmc->timing_data[idx], 3366e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGDATA0); 3376e7674c3SLukasz Luba writel(dmc->timing_data[idx], 3386e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGDATA0); 3396e7674c3SLukasz Luba writel(dmc->timing_power[idx], 3406e7674c3SLukasz Luba dmc->base_drexi0 + EXYNOS5_DREXI_TIMINGPOWER0); 3416e7674c3SLukasz Luba writel(dmc->timing_power[idx], 3426e7674c3SLukasz Luba dmc->base_drexi1 + EXYNOS5_DREXI_TIMINGPOWER0); 3436e7674c3SLukasz Luba 3446e7674c3SLukasz Luba return 0; 3456e7674c3SLukasz Luba } 3466e7674c3SLukasz Luba 3476e7674c3SLukasz Luba /** 3486e7674c3SLukasz Luba * exynos5_dmc_align_target_voltage() - Sets the final voltage for the DMC 3496e7674c3SLukasz Luba * @dmc: device for which it is going to be set 3506e7674c3SLukasz Luba * @target_volt: new voltage which is chosen to be final 3516e7674c3SLukasz Luba * 3526e7674c3SLukasz Luba * Function tries to align voltage to the safe level for 'normal' mode. 3536e7674c3SLukasz Luba * It checks the need of higher voltage and changes the value. The target 3546e7674c3SLukasz Luba * voltage might be lower that currently set and still the system will be 3556e7674c3SLukasz Luba * stable. 3566e7674c3SLukasz Luba */ 3576e7674c3SLukasz Luba static int exynos5_dmc_align_target_voltage(struct exynos5_dmc *dmc, 3586e7674c3SLukasz Luba unsigned long target_volt) 3596e7674c3SLukasz Luba { 3606e7674c3SLukasz Luba int ret = 0; 3616e7674c3SLukasz Luba 3626e7674c3SLukasz Luba if (dmc->curr_volt <= target_volt) 3636e7674c3SLukasz Luba return 0; 3646e7674c3SLukasz Luba 3656e7674c3SLukasz Luba ret = regulator_set_voltage(dmc->vdd_mif, target_volt, 3666e7674c3SLukasz Luba target_volt); 3676e7674c3SLukasz Luba if (!ret) 3686e7674c3SLukasz Luba dmc->curr_volt = target_volt; 3696e7674c3SLukasz Luba 3706e7674c3SLukasz Luba return ret; 3716e7674c3SLukasz Luba } 3726e7674c3SLukasz Luba 3736e7674c3SLukasz Luba /** 3746e7674c3SLukasz Luba * exynos5_dmc_align_bypass_voltage() - Sets the voltage for the DMC 3756e7674c3SLukasz Luba * @dmc: device for which it is going to be set 3766e7674c3SLukasz Luba * @target_volt: new voltage which is chosen to be final 3776e7674c3SLukasz Luba * 3786e7674c3SLukasz Luba * Function tries to align voltage to the safe level for the 'bypass' mode. 3796e7674c3SLukasz Luba * It checks the need of higher voltage and changes the value. 3806e7674c3SLukasz Luba * The target voltage must not be less than currently needed, because 3816e7674c3SLukasz Luba * for current frequency the device might become unstable. 3826e7674c3SLukasz Luba */ 3836e7674c3SLukasz Luba static int exynos5_dmc_align_bypass_voltage(struct exynos5_dmc *dmc, 3846e7674c3SLukasz Luba unsigned long target_volt) 3856e7674c3SLukasz Luba { 3866e7674c3SLukasz Luba int ret = 0; 3876e7674c3SLukasz Luba unsigned long bypass_volt = dmc->opp_bypass.volt_uv; 3886e7674c3SLukasz Luba 3896e7674c3SLukasz Luba target_volt = max(bypass_volt, target_volt); 3906e7674c3SLukasz Luba 3916e7674c3SLukasz Luba if (dmc->curr_volt >= target_volt) 3926e7674c3SLukasz Luba return 0; 3936e7674c3SLukasz Luba 3946e7674c3SLukasz Luba ret = regulator_set_voltage(dmc->vdd_mif, target_volt, 3956e7674c3SLukasz Luba target_volt); 3966e7674c3SLukasz Luba if (!ret) 3976e7674c3SLukasz Luba dmc->curr_volt = target_volt; 3986e7674c3SLukasz Luba 3996e7674c3SLukasz Luba return ret; 4006e7674c3SLukasz Luba } 4016e7674c3SLukasz Luba 4026e7674c3SLukasz Luba /** 4036e7674c3SLukasz Luba * exynos5_dmc_align_bypass_dram_timings() - Chooses and sets DRAM timings 4046e7674c3SLukasz Luba * @dmc: device for which it is going to be set 4056e7674c3SLukasz Luba * @target_rate: new frequency which is chosen to be final 4066e7674c3SLukasz Luba * 4076e7674c3SLukasz Luba * Function changes the DRAM timings for the temporary 'bypass' mode. 4086e7674c3SLukasz Luba */ 4096e7674c3SLukasz Luba static int exynos5_dmc_align_bypass_dram_timings(struct exynos5_dmc *dmc, 4106e7674c3SLukasz Luba unsigned long target_rate) 4116e7674c3SLukasz Luba { 4126e7674c3SLukasz Luba int idx = find_target_freq_idx(dmc, target_rate); 4136e7674c3SLukasz Luba 4146e7674c3SLukasz Luba if (idx < 0) 4156e7674c3SLukasz Luba return -EINVAL; 4166e7674c3SLukasz Luba 4176e7674c3SLukasz Luba exynos5_set_bypass_dram_timings(dmc); 4186e7674c3SLukasz Luba 4196e7674c3SLukasz Luba return 0; 4206e7674c3SLukasz Luba } 4216e7674c3SLukasz Luba 4226e7674c3SLukasz Luba /** 4236e7674c3SLukasz Luba * exynos5_dmc_switch_to_bypass_configuration() - Switching to temporary clock 4246e7674c3SLukasz Luba * @dmc: DMC device for which the switching is going to happen 4256e7674c3SLukasz Luba * @target_rate: new frequency which is going to be set as a final 4266e7674c3SLukasz Luba * @target_volt: new voltage which is going to be set as a final 4276e7674c3SLukasz Luba * 4286e7674c3SLukasz Luba * Function configures DMC and clocks for operating in temporary 'bypass' mode. 4296e7674c3SLukasz Luba * This mode is used only temporary but if required, changes voltage and timings 4306e7674c3SLukasz Luba * for DRAM chips. It switches the main clock to stable clock source for the 4316e7674c3SLukasz Luba * period of the main PLL reconfiguration. 4326e7674c3SLukasz Luba */ 4336e7674c3SLukasz Luba static int 4346e7674c3SLukasz Luba exynos5_dmc_switch_to_bypass_configuration(struct exynos5_dmc *dmc, 4356e7674c3SLukasz Luba unsigned long target_rate, 4366e7674c3SLukasz Luba unsigned long target_volt) 4376e7674c3SLukasz Luba { 4386e7674c3SLukasz Luba int ret; 4396e7674c3SLukasz Luba 4406e7674c3SLukasz Luba /* 4416e7674c3SLukasz Luba * Having higher voltage for a particular frequency does not harm 4426e7674c3SLukasz Luba * the chip. Use it for the temporary frequency change when one 4436e7674c3SLukasz Luba * voltage manipulation might be avoided. 4446e7674c3SLukasz Luba */ 4456e7674c3SLukasz Luba ret = exynos5_dmc_align_bypass_voltage(dmc, target_volt); 4466e7674c3SLukasz Luba if (ret) 4476e7674c3SLukasz Luba return ret; 4486e7674c3SLukasz Luba 4496e7674c3SLukasz Luba /* 4506e7674c3SLukasz Luba * Longer delays for DRAM does not cause crash, the opposite does. 4516e7674c3SLukasz Luba */ 4526e7674c3SLukasz Luba ret = exynos5_dmc_align_bypass_dram_timings(dmc, target_rate); 4536e7674c3SLukasz Luba if (ret) 4546e7674c3SLukasz Luba return ret; 4556e7674c3SLukasz Luba 4566e7674c3SLukasz Luba /* 4576e7674c3SLukasz Luba * Delays are long enough, so use them for the new coming clock. 4586e7674c3SLukasz Luba */ 4596e7674c3SLukasz Luba exynos5_switch_timing_regs(dmc, USE_MX_MSPLL_TIMINGS); 4606e7674c3SLukasz Luba 4616e7674c3SLukasz Luba return ret; 4626e7674c3SLukasz Luba } 4636e7674c3SLukasz Luba 4646e7674c3SLukasz Luba /** 4656e7674c3SLukasz Luba * exynos5_dmc_change_freq_and_volt() - Changes voltage and frequency of the DMC 4666e7674c3SLukasz Luba * using safe procedure 4676e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be changed 4686e7674c3SLukasz Luba * @target_rate: requested new frequency 4696e7674c3SLukasz Luba * @target_volt: requested voltage which corresponds to the new frequency 4706e7674c3SLukasz Luba * 4716e7674c3SLukasz Luba * The DMC frequency change procedure requires a few steps. 4726e7674c3SLukasz Luba * The main requirement is to change the clock source in the clk mux 4736e7674c3SLukasz Luba * for the time of main clock PLL locking. The assumption is that the 4746e7674c3SLukasz Luba * alternative clock source set as parent is stable. 4756e7674c3SLukasz Luba * The second parent's clock frequency is fixed to 400MHz, it is named 'bypass' 4766e7674c3SLukasz Luba * clock. This requires alignment in DRAM timing parameters for the new 4776e7674c3SLukasz Luba * T-period. There is two bank sets for keeping DRAM 4786e7674c3SLukasz Luba * timings: set 0 and set 1. The set 0 is used when main clock source is 4796e7674c3SLukasz Luba * chosen. The 2nd set of regs is used for 'bypass' clock. Switching between 4806e7674c3SLukasz Luba * the two bank sets is part of the process. 4816e7674c3SLukasz Luba * The voltage must also be aligned to the minimum required level. There is 4826e7674c3SLukasz Luba * this intermediate step with switching to 'bypass' parent clock source. 4836e7674c3SLukasz Luba * if the old voltage is lower, it requires an increase of the voltage level. 4846e7674c3SLukasz Luba * The complexity of the voltage manipulation is hidden in low level function. 4856e7674c3SLukasz Luba * In this function there is last alignment of the voltage level at the end. 4866e7674c3SLukasz Luba */ 4876e7674c3SLukasz Luba static int 4886e7674c3SLukasz Luba exynos5_dmc_change_freq_and_volt(struct exynos5_dmc *dmc, 4896e7674c3SLukasz Luba unsigned long target_rate, 4906e7674c3SLukasz Luba unsigned long target_volt) 4916e7674c3SLukasz Luba { 4926e7674c3SLukasz Luba int ret; 4936e7674c3SLukasz Luba 4946e7674c3SLukasz Luba ret = exynos5_dmc_switch_to_bypass_configuration(dmc, target_rate, 4956e7674c3SLukasz Luba target_volt); 4966e7674c3SLukasz Luba if (ret) 4976e7674c3SLukasz Luba return ret; 4986e7674c3SLukasz Luba 4996e7674c3SLukasz Luba /* 5006e7674c3SLukasz Luba * Voltage is set at least to a level needed for this frequency, 5016e7674c3SLukasz Luba * so switching clock source is safe now. 5026e7674c3SLukasz Luba */ 5036e7674c3SLukasz Luba clk_prepare_enable(dmc->fout_spll); 5046e7674c3SLukasz Luba clk_prepare_enable(dmc->mout_spll); 5056e7674c3SLukasz Luba clk_prepare_enable(dmc->mout_mx_mspll_ccore); 5066e7674c3SLukasz Luba 5076e7674c3SLukasz Luba ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_mx_mspll_ccore); 5086e7674c3SLukasz Luba if (ret) 5096e7674c3SLukasz Luba goto disable_clocks; 5106e7674c3SLukasz Luba 5116e7674c3SLukasz Luba /* 5126e7674c3SLukasz Luba * We are safe to increase the timings for current bypass frequency. 5136e7674c3SLukasz Luba * Thanks to this the settings will be ready for the upcoming clock 5146e7674c3SLukasz Luba * source change. 5156e7674c3SLukasz Luba */ 5166e7674c3SLukasz Luba exynos5_dram_change_timings(dmc, target_rate); 5176e7674c3SLukasz Luba 5186e7674c3SLukasz Luba clk_set_rate(dmc->fout_bpll, target_rate); 5196e7674c3SLukasz Luba 5206e7674c3SLukasz Luba exynos5_switch_timing_regs(dmc, USE_BPLL_TIMINGS); 5216e7674c3SLukasz Luba 5226e7674c3SLukasz Luba ret = clk_set_parent(dmc->mout_mclk_cdrex, dmc->mout_bpll); 5236e7674c3SLukasz Luba if (ret) 5246e7674c3SLukasz Luba goto disable_clocks; 5256e7674c3SLukasz Luba 5266e7674c3SLukasz Luba /* 5276e7674c3SLukasz Luba * Make sure if the voltage is not from 'bypass' settings and align to 5286e7674c3SLukasz Luba * the right level for power efficiency. 5296e7674c3SLukasz Luba */ 5306e7674c3SLukasz Luba ret = exynos5_dmc_align_target_voltage(dmc, target_volt); 5316e7674c3SLukasz Luba 5326e7674c3SLukasz Luba disable_clocks: 5336e7674c3SLukasz Luba clk_disable_unprepare(dmc->mout_mx_mspll_ccore); 5346e7674c3SLukasz Luba clk_disable_unprepare(dmc->mout_spll); 5356e7674c3SLukasz Luba clk_disable_unprepare(dmc->fout_spll); 5366e7674c3SLukasz Luba 5376e7674c3SLukasz Luba return ret; 5386e7674c3SLukasz Luba } 5396e7674c3SLukasz Luba 5406e7674c3SLukasz Luba /** 5416e7674c3SLukasz Luba * exynos5_dmc_get_volt_freq() - Gets the frequency and voltage from the OPP 5426e7674c3SLukasz Luba * table. 5436e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be changed 5446e7674c3SLukasz Luba * @freq: requested frequency in KHz 5456e7674c3SLukasz Luba * @target_rate: returned frequency which is the same or lower than 5466e7674c3SLukasz Luba * requested 5476e7674c3SLukasz Luba * @target_volt: returned voltage which corresponds to the returned 5486e7674c3SLukasz Luba * frequency 5496e7674c3SLukasz Luba * 5506e7674c3SLukasz Luba * Function gets requested frequency and checks OPP framework for needed 5516e7674c3SLukasz Luba * frequency and voltage. It populates the values 'target_rate' and 5526e7674c3SLukasz Luba * 'target_volt' or returns error value when OPP framework fails. 5536e7674c3SLukasz Luba */ 5546e7674c3SLukasz Luba static int exynos5_dmc_get_volt_freq(struct exynos5_dmc *dmc, 5556e7674c3SLukasz Luba unsigned long *freq, 5566e7674c3SLukasz Luba unsigned long *target_rate, 5576e7674c3SLukasz Luba unsigned long *target_volt, u32 flags) 5586e7674c3SLukasz Luba { 5596e7674c3SLukasz Luba struct dev_pm_opp *opp; 5606e7674c3SLukasz Luba 5616e7674c3SLukasz Luba opp = devfreq_recommended_opp(dmc->dev, freq, flags); 5626e7674c3SLukasz Luba if (IS_ERR(opp)) 5636e7674c3SLukasz Luba return PTR_ERR(opp); 5646e7674c3SLukasz Luba 5656e7674c3SLukasz Luba *target_rate = dev_pm_opp_get_freq(opp); 5666e7674c3SLukasz Luba *target_volt = dev_pm_opp_get_voltage(opp); 5676e7674c3SLukasz Luba dev_pm_opp_put(opp); 5686e7674c3SLukasz Luba 5696e7674c3SLukasz Luba return 0; 5706e7674c3SLukasz Luba } 5716e7674c3SLukasz Luba 5726e7674c3SLukasz Luba /** 5736e7674c3SLukasz Luba * exynos5_dmc_target() - Function responsible for changing frequency of DMC 5746e7674c3SLukasz Luba * @dev: device for which the frequency is going to be changed 5756e7674c3SLukasz Luba * @freq: requested frequency in KHz 5766e7674c3SLukasz Luba * @flags: flags provided for this frequency change request 5776e7674c3SLukasz Luba * 5786e7674c3SLukasz Luba * An entry function provided to the devfreq framework which provides frequency 5796e7674c3SLukasz Luba * change of the DMC. The function gets the possible rate from OPP table based 5806e7674c3SLukasz Luba * on requested frequency. It calls the next function responsible for the 5816e7674c3SLukasz Luba * frequency and voltage change. In case of failure, does not set 'curr_rate' 5826e7674c3SLukasz Luba * and returns error value to the framework. 5836e7674c3SLukasz Luba */ 5846e7674c3SLukasz Luba static int exynos5_dmc_target(struct device *dev, unsigned long *freq, 5856e7674c3SLukasz Luba u32 flags) 5866e7674c3SLukasz Luba { 5876e7674c3SLukasz Luba struct exynos5_dmc *dmc = dev_get_drvdata(dev); 5886e7674c3SLukasz Luba unsigned long target_rate = 0; 5896e7674c3SLukasz Luba unsigned long target_volt = 0; 5906e7674c3SLukasz Luba int ret; 5916e7674c3SLukasz Luba 5926e7674c3SLukasz Luba ret = exynos5_dmc_get_volt_freq(dmc, freq, &target_rate, &target_volt, 5936e7674c3SLukasz Luba flags); 5946e7674c3SLukasz Luba 5956e7674c3SLukasz Luba if (ret) 5966e7674c3SLukasz Luba return ret; 5976e7674c3SLukasz Luba 5986e7674c3SLukasz Luba if (target_rate == dmc->curr_rate) 5996e7674c3SLukasz Luba return 0; 6006e7674c3SLukasz Luba 6016e7674c3SLukasz Luba mutex_lock(&dmc->lock); 6026e7674c3SLukasz Luba 6036e7674c3SLukasz Luba ret = exynos5_dmc_change_freq_and_volt(dmc, target_rate, target_volt); 6046e7674c3SLukasz Luba 6056e7674c3SLukasz Luba if (ret) { 6066e7674c3SLukasz Luba mutex_unlock(&dmc->lock); 6076e7674c3SLukasz Luba return ret; 6086e7674c3SLukasz Luba } 6096e7674c3SLukasz Luba 6106e7674c3SLukasz Luba dmc->curr_rate = target_rate; 6116e7674c3SLukasz Luba 6126e7674c3SLukasz Luba mutex_unlock(&dmc->lock); 6136e7674c3SLukasz Luba return 0; 6146e7674c3SLukasz Luba } 6156e7674c3SLukasz Luba 6166e7674c3SLukasz Luba /** 6176e7674c3SLukasz Luba * exynos5_counters_get() - Gets the performance counters values. 6186e7674c3SLukasz Luba * @dmc: device for which the counters are going to be checked 6196e7674c3SLukasz Luba * @load_count: variable which is populated with counter value 6206e7674c3SLukasz Luba * @total_count: variable which is used as 'wall clock' reference 6216e7674c3SLukasz Luba * 6226e7674c3SLukasz Luba * Function which provides performance counters values. It sums up counters for 6236e7674c3SLukasz Luba * two DMC channels. The 'total_count' is used as a reference and max value. 6246e7674c3SLukasz Luba * The ratio 'load_count/total_count' shows the busy percentage [0%, 100%]. 6256e7674c3SLukasz Luba */ 6266e7674c3SLukasz Luba static int exynos5_counters_get(struct exynos5_dmc *dmc, 6276e7674c3SLukasz Luba unsigned long *load_count, 6286e7674c3SLukasz Luba unsigned long *total_count) 6296e7674c3SLukasz Luba { 6306e7674c3SLukasz Luba unsigned long total = 0; 6316e7674c3SLukasz Luba struct devfreq_event_data event; 6326e7674c3SLukasz Luba int ret, i; 6336e7674c3SLukasz Luba 6346e7674c3SLukasz Luba *load_count = 0; 6356e7674c3SLukasz Luba 6366e7674c3SLukasz Luba /* Take into account only read+write counters, but stop all */ 6376e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) { 6386e7674c3SLukasz Luba if (!dmc->counter[i]) 6396e7674c3SLukasz Luba continue; 6406e7674c3SLukasz Luba 6416e7674c3SLukasz Luba ret = devfreq_event_get_event(dmc->counter[i], &event); 6426e7674c3SLukasz Luba if (ret < 0) 6436e7674c3SLukasz Luba return ret; 6446e7674c3SLukasz Luba 6456e7674c3SLukasz Luba *load_count += event.load_count; 6466e7674c3SLukasz Luba 6476e7674c3SLukasz Luba if (total < event.total_count) 6486e7674c3SLukasz Luba total = event.total_count; 6496e7674c3SLukasz Luba } 6506e7674c3SLukasz Luba 6516e7674c3SLukasz Luba *total_count = total; 6526e7674c3SLukasz Luba 6536e7674c3SLukasz Luba return 0; 6546e7674c3SLukasz Luba } 6556e7674c3SLukasz Luba 6566e7674c3SLukasz Luba /** 6576e7674c3SLukasz Luba * exynos5_dmc_get_status() - Read current DMC performance statistics. 6586e7674c3SLukasz Luba * @dev: device for which the statistics are requested 6596e7674c3SLukasz Luba * @stat: structure which has statistic fields 6606e7674c3SLukasz Luba * 6616e7674c3SLukasz Luba * Function reads the DMC performance counters and calculates 'busy_time' 6626e7674c3SLukasz Luba * and 'total_time'. To protect from overflow, the values are shifted right 6636e7674c3SLukasz Luba * by 10. After read out the counters are setup to count again. 6646e7674c3SLukasz Luba */ 6656e7674c3SLukasz Luba static int exynos5_dmc_get_status(struct device *dev, 6666e7674c3SLukasz Luba struct devfreq_dev_status *stat) 6676e7674c3SLukasz Luba { 6686e7674c3SLukasz Luba struct exynos5_dmc *dmc = dev_get_drvdata(dev); 6696e7674c3SLukasz Luba unsigned long load, total; 6706e7674c3SLukasz Luba int ret; 6716e7674c3SLukasz Luba 6726e7674c3SLukasz Luba ret = exynos5_counters_get(dmc, &load, &total); 6736e7674c3SLukasz Luba if (ret < 0) 6746e7674c3SLukasz Luba return -EINVAL; 6756e7674c3SLukasz Luba 6766e7674c3SLukasz Luba /* To protect from overflow in calculation ratios, divide by 1024 */ 6776e7674c3SLukasz Luba stat->busy_time = load >> 10; 6786e7674c3SLukasz Luba stat->total_time = total >> 10; 6796e7674c3SLukasz Luba 6806e7674c3SLukasz Luba ret = exynos5_counters_set_event(dmc); 6816e7674c3SLukasz Luba if (ret < 0) { 6826e7674c3SLukasz Luba dev_err(dev, "could not set event counter\n"); 6836e7674c3SLukasz Luba return ret; 6846e7674c3SLukasz Luba } 6856e7674c3SLukasz Luba 6866e7674c3SLukasz Luba return 0; 6876e7674c3SLukasz Luba } 6886e7674c3SLukasz Luba 6896e7674c3SLukasz Luba /** 6906e7674c3SLukasz Luba * exynos5_dmc_get_cur_freq() - Function returns current DMC frequency 6916e7674c3SLukasz Luba * @dev: device for which the framework checks operating frequency 6926e7674c3SLukasz Luba * @freq: returned frequency value 6936e7674c3SLukasz Luba * 6946e7674c3SLukasz Luba * It returns the currently used frequency of the DMC. The real operating 6956e7674c3SLukasz Luba * frequency might be lower when the clock source value could not be divided 6966e7674c3SLukasz Luba * to the requested value. 6976e7674c3SLukasz Luba */ 6986e7674c3SLukasz Luba static int exynos5_dmc_get_cur_freq(struct device *dev, unsigned long *freq) 6996e7674c3SLukasz Luba { 7006e7674c3SLukasz Luba struct exynos5_dmc *dmc = dev_get_drvdata(dev); 7016e7674c3SLukasz Luba 7026e7674c3SLukasz Luba mutex_lock(&dmc->lock); 7036e7674c3SLukasz Luba *freq = dmc->curr_rate; 7046e7674c3SLukasz Luba mutex_unlock(&dmc->lock); 7056e7674c3SLukasz Luba 7066e7674c3SLukasz Luba return 0; 7076e7674c3SLukasz Luba } 7086e7674c3SLukasz Luba 7096e7674c3SLukasz Luba /** 7106e7674c3SLukasz Luba * exynos5_dmc_df_profile - Devfreq governor's profile structure 7116e7674c3SLukasz Luba * 7126e7674c3SLukasz Luba * It provides to the devfreq framework needed functions and polling period. 7136e7674c3SLukasz Luba */ 7146e7674c3SLukasz Luba static struct devfreq_dev_profile exynos5_dmc_df_profile = { 7156e7674c3SLukasz Luba .polling_ms = 500, 7166e7674c3SLukasz Luba .target = exynos5_dmc_target, 7176e7674c3SLukasz Luba .get_dev_status = exynos5_dmc_get_status, 7186e7674c3SLukasz Luba .get_cur_freq = exynos5_dmc_get_cur_freq, 7196e7674c3SLukasz Luba }; 7206e7674c3SLukasz Luba 7216e7674c3SLukasz Luba /** 7226e7674c3SLukasz Luba * exynos5_dmc_align_initial_frequency() - Align initial frequency value 7236e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be set 7246e7674c3SLukasz Luba * @bootloader_init_freq: initial frequency set by the bootloader in KHz 7256e7674c3SLukasz Luba * 7266e7674c3SLukasz Luba * The initial bootloader frequency, which is present during boot, might be 7276e7674c3SLukasz Luba * different that supported frequency values in the driver. It is possible 7286e7674c3SLukasz Luba * due to different PLL settings or used PLL as a source. 7296e7674c3SLukasz Luba * This function provides the 'initial_freq' for the devfreq framework 7306e7674c3SLukasz Luba * statistics engine which supports only registered values. Thus, some alignment 7316e7674c3SLukasz Luba * must be made. 7326e7674c3SLukasz Luba */ 733*d51e6a69SLukasz Luba static unsigned long 7346e7674c3SLukasz Luba exynos5_dmc_align_init_freq(struct exynos5_dmc *dmc, 7356e7674c3SLukasz Luba unsigned long bootloader_init_freq) 7366e7674c3SLukasz Luba { 7376e7674c3SLukasz Luba unsigned long aligned_freq; 7386e7674c3SLukasz Luba int idx; 7396e7674c3SLukasz Luba 7406e7674c3SLukasz Luba idx = find_target_freq_idx(dmc, bootloader_init_freq); 7416e7674c3SLukasz Luba if (idx >= 0) 7426e7674c3SLukasz Luba aligned_freq = dmc->opp[idx].freq_hz; 7436e7674c3SLukasz Luba else 7446e7674c3SLukasz Luba aligned_freq = dmc->opp[dmc->opp_count - 1].freq_hz; 7456e7674c3SLukasz Luba 7466e7674c3SLukasz Luba return aligned_freq; 7476e7674c3SLukasz Luba } 7486e7674c3SLukasz Luba 7496e7674c3SLukasz Luba /** 7506e7674c3SLukasz Luba * create_timings_aligned() - Create register values and align with standard 7516e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be set 7526e7674c3SLukasz Luba * @idx: speed bin in the OPP table 7536e7674c3SLukasz Luba * @clk_period_ps: the period of the clock, known as tCK 7546e7674c3SLukasz Luba * 7556e7674c3SLukasz Luba * The function calculates timings and creates a register value ready for 7566e7674c3SLukasz Luba * a frequency transition. The register contains a few timings. They are 7576e7674c3SLukasz Luba * shifted by a known offset. The timing value is calculated based on memory 7586e7674c3SLukasz Luba * specyfication: minimal time required and minimal cycles required. 7596e7674c3SLukasz Luba */ 7606e7674c3SLukasz Luba static int create_timings_aligned(struct exynos5_dmc *dmc, u32 *reg_timing_row, 7616e7674c3SLukasz Luba u32 *reg_timing_data, u32 *reg_timing_power, 7626e7674c3SLukasz Luba u32 clk_period_ps) 7636e7674c3SLukasz Luba { 7646e7674c3SLukasz Luba u32 val; 7656e7674c3SLukasz Luba const struct timing_reg *reg; 7666e7674c3SLukasz Luba 7676e7674c3SLukasz Luba if (clk_period_ps == 0) 7686e7674c3SLukasz Luba return -EINVAL; 7696e7674c3SLukasz Luba 7706e7674c3SLukasz Luba *reg_timing_row = 0; 7716e7674c3SLukasz Luba *reg_timing_data = 0; 7726e7674c3SLukasz Luba *reg_timing_power = 0; 7736e7674c3SLukasz Luba 7746e7674c3SLukasz Luba val = dmc->timings->tRFC / clk_period_ps; 7756e7674c3SLukasz Luba val += dmc->timings->tRFC % clk_period_ps ? 1 : 0; 7766e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRFC); 7776e7674c3SLukasz Luba reg = &timing_row[0]; 7786e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val); 7796e7674c3SLukasz Luba 7806e7674c3SLukasz Luba val = dmc->timings->tRRD / clk_period_ps; 7816e7674c3SLukasz Luba val += dmc->timings->tRRD % clk_period_ps ? 1 : 0; 7826e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRRD); 7836e7674c3SLukasz Luba reg = &timing_row[1]; 7846e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val); 7856e7674c3SLukasz Luba 7866e7674c3SLukasz Luba val = dmc->timings->tRPab / clk_period_ps; 7876e7674c3SLukasz Luba val += dmc->timings->tRPab % clk_period_ps ? 1 : 0; 7886e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRPab); 7896e7674c3SLukasz Luba reg = &timing_row[2]; 7906e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val); 7916e7674c3SLukasz Luba 7926e7674c3SLukasz Luba val = dmc->timings->tRCD / clk_period_ps; 7936e7674c3SLukasz Luba val += dmc->timings->tRCD % clk_period_ps ? 1 : 0; 7946e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRCD); 7956e7674c3SLukasz Luba reg = &timing_row[3]; 7966e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val); 7976e7674c3SLukasz Luba 7986e7674c3SLukasz Luba val = dmc->timings->tRC / clk_period_ps; 7996e7674c3SLukasz Luba val += dmc->timings->tRC % clk_period_ps ? 1 : 0; 8006e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRC); 8016e7674c3SLukasz Luba reg = &timing_row[4]; 8026e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val); 8036e7674c3SLukasz Luba 8046e7674c3SLukasz Luba val = dmc->timings->tRAS / clk_period_ps; 8056e7674c3SLukasz Luba val += dmc->timings->tRAS % clk_period_ps ? 1 : 0; 8066e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRAS); 8076e7674c3SLukasz Luba reg = &timing_row[5]; 8086e7674c3SLukasz Luba *reg_timing_row |= TIMING_VAL2REG(reg, val); 8096e7674c3SLukasz Luba 8106e7674c3SLukasz Luba /* data related timings */ 8116e7674c3SLukasz Luba val = dmc->timings->tWTR / clk_period_ps; 8126e7674c3SLukasz Luba val += dmc->timings->tWTR % clk_period_ps ? 1 : 0; 8136e7674c3SLukasz Luba val = max(val, dmc->min_tck->tWTR); 8146e7674c3SLukasz Luba reg = &timing_data[0]; 8156e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val); 8166e7674c3SLukasz Luba 8176e7674c3SLukasz Luba val = dmc->timings->tWR / clk_period_ps; 8186e7674c3SLukasz Luba val += dmc->timings->tWR % clk_period_ps ? 1 : 0; 8196e7674c3SLukasz Luba val = max(val, dmc->min_tck->tWR); 8206e7674c3SLukasz Luba reg = &timing_data[1]; 8216e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val); 8226e7674c3SLukasz Luba 8236e7674c3SLukasz Luba val = dmc->timings->tRTP / clk_period_ps; 8246e7674c3SLukasz Luba val += dmc->timings->tRTP % clk_period_ps ? 1 : 0; 8256e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRTP); 8266e7674c3SLukasz Luba reg = &timing_data[2]; 8276e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val); 8286e7674c3SLukasz Luba 8296e7674c3SLukasz Luba val = dmc->timings->tW2W_C2C / clk_period_ps; 8306e7674c3SLukasz Luba val += dmc->timings->tW2W_C2C % clk_period_ps ? 1 : 0; 8316e7674c3SLukasz Luba val = max(val, dmc->min_tck->tW2W_C2C); 8326e7674c3SLukasz Luba reg = &timing_data[3]; 8336e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val); 8346e7674c3SLukasz Luba 8356e7674c3SLukasz Luba val = dmc->timings->tR2R_C2C / clk_period_ps; 8366e7674c3SLukasz Luba val += dmc->timings->tR2R_C2C % clk_period_ps ? 1 : 0; 8376e7674c3SLukasz Luba val = max(val, dmc->min_tck->tR2R_C2C); 8386e7674c3SLukasz Luba reg = &timing_data[4]; 8396e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val); 8406e7674c3SLukasz Luba 8416e7674c3SLukasz Luba val = dmc->timings->tWL / clk_period_ps; 8426e7674c3SLukasz Luba val += dmc->timings->tWL % clk_period_ps ? 1 : 0; 8436e7674c3SLukasz Luba val = max(val, dmc->min_tck->tWL); 8446e7674c3SLukasz Luba reg = &timing_data[5]; 8456e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val); 8466e7674c3SLukasz Luba 8476e7674c3SLukasz Luba val = dmc->timings->tDQSCK / clk_period_ps; 8486e7674c3SLukasz Luba val += dmc->timings->tDQSCK % clk_period_ps ? 1 : 0; 8496e7674c3SLukasz Luba val = max(val, dmc->min_tck->tDQSCK); 8506e7674c3SLukasz Luba reg = &timing_data[6]; 8516e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val); 8526e7674c3SLukasz Luba 8536e7674c3SLukasz Luba val = dmc->timings->tRL / clk_period_ps; 8546e7674c3SLukasz Luba val += dmc->timings->tRL % clk_period_ps ? 1 : 0; 8556e7674c3SLukasz Luba val = max(val, dmc->min_tck->tRL); 8566e7674c3SLukasz Luba reg = &timing_data[7]; 8576e7674c3SLukasz Luba *reg_timing_data |= TIMING_VAL2REG(reg, val); 8586e7674c3SLukasz Luba 8596e7674c3SLukasz Luba /* power related timings */ 8606e7674c3SLukasz Luba val = dmc->timings->tFAW / clk_period_ps; 8616e7674c3SLukasz Luba val += dmc->timings->tFAW % clk_period_ps ? 1 : 0; 8626e7674c3SLukasz Luba val = max(val, dmc->min_tck->tXP); 8636e7674c3SLukasz Luba reg = &timing_power[0]; 8646e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val); 8656e7674c3SLukasz Luba 8666e7674c3SLukasz Luba val = dmc->timings->tXSR / clk_period_ps; 8676e7674c3SLukasz Luba val += dmc->timings->tXSR % clk_period_ps ? 1 : 0; 8686e7674c3SLukasz Luba val = max(val, dmc->min_tck->tXSR); 8696e7674c3SLukasz Luba reg = &timing_power[1]; 8706e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val); 8716e7674c3SLukasz Luba 8726e7674c3SLukasz Luba val = dmc->timings->tXP / clk_period_ps; 8736e7674c3SLukasz Luba val += dmc->timings->tXP % clk_period_ps ? 1 : 0; 8746e7674c3SLukasz Luba val = max(val, dmc->min_tck->tXP); 8756e7674c3SLukasz Luba reg = &timing_power[2]; 8766e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val); 8776e7674c3SLukasz Luba 8786e7674c3SLukasz Luba val = dmc->timings->tCKE / clk_period_ps; 8796e7674c3SLukasz Luba val += dmc->timings->tCKE % clk_period_ps ? 1 : 0; 8806e7674c3SLukasz Luba val = max(val, dmc->min_tck->tCKE); 8816e7674c3SLukasz Luba reg = &timing_power[3]; 8826e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val); 8836e7674c3SLukasz Luba 8846e7674c3SLukasz Luba val = dmc->timings->tMRD / clk_period_ps; 8856e7674c3SLukasz Luba val += dmc->timings->tMRD % clk_period_ps ? 1 : 0; 8866e7674c3SLukasz Luba val = max(val, dmc->min_tck->tMRD); 8876e7674c3SLukasz Luba reg = &timing_power[4]; 8886e7674c3SLukasz Luba *reg_timing_power |= TIMING_VAL2REG(reg, val); 8896e7674c3SLukasz Luba 8906e7674c3SLukasz Luba return 0; 8916e7674c3SLukasz Luba } 8926e7674c3SLukasz Luba 8936e7674c3SLukasz Luba /** 8946e7674c3SLukasz Luba * of_get_dram_timings() - helper function for parsing DT settings for DRAM 8956e7674c3SLukasz Luba * @dmc: device for which the frequency is going to be set 8966e7674c3SLukasz Luba * 8976e7674c3SLukasz Luba * The function parses DT entries with DRAM information. 8986e7674c3SLukasz Luba */ 8996e7674c3SLukasz Luba static int of_get_dram_timings(struct exynos5_dmc *dmc) 9006e7674c3SLukasz Luba { 9016e7674c3SLukasz Luba int ret = 0; 9026e7674c3SLukasz Luba int idx; 9036e7674c3SLukasz Luba struct device_node *np_ddr; 9046e7674c3SLukasz Luba u32 freq_mhz, clk_period_ps; 9056e7674c3SLukasz Luba 9066e7674c3SLukasz Luba np_ddr = of_parse_phandle(dmc->dev->of_node, "device-handle", 0); 9076e7674c3SLukasz Luba if (!np_ddr) { 9086e7674c3SLukasz Luba dev_warn(dmc->dev, "could not find 'device-handle' in DT\n"); 9096e7674c3SLukasz Luba return -EINVAL; 9106e7674c3SLukasz Luba } 9116e7674c3SLukasz Luba 9126e7674c3SLukasz Luba dmc->timing_row = devm_kmalloc_array(dmc->dev, TIMING_COUNT, 9136e7674c3SLukasz Luba sizeof(u32), GFP_KERNEL); 9146e7674c3SLukasz Luba if (!dmc->timing_row) 9156e7674c3SLukasz Luba return -ENOMEM; 9166e7674c3SLukasz Luba 9176e7674c3SLukasz Luba dmc->timing_data = devm_kmalloc_array(dmc->dev, TIMING_COUNT, 9186e7674c3SLukasz Luba sizeof(u32), GFP_KERNEL); 9196e7674c3SLukasz Luba if (!dmc->timing_data) 9206e7674c3SLukasz Luba return -ENOMEM; 9216e7674c3SLukasz Luba 9226e7674c3SLukasz Luba dmc->timing_power = devm_kmalloc_array(dmc->dev, TIMING_COUNT, 9236e7674c3SLukasz Luba sizeof(u32), GFP_KERNEL); 9246e7674c3SLukasz Luba if (!dmc->timing_power) 9256e7674c3SLukasz Luba return -ENOMEM; 9266e7674c3SLukasz Luba 9276e7674c3SLukasz Luba dmc->timings = of_lpddr3_get_ddr_timings(np_ddr, dmc->dev, 9286e7674c3SLukasz Luba DDR_TYPE_LPDDR3, 9296e7674c3SLukasz Luba &dmc->timings_arr_size); 9306e7674c3SLukasz Luba if (!dmc->timings) { 9316e7674c3SLukasz Luba of_node_put(np_ddr); 9326e7674c3SLukasz Luba dev_warn(dmc->dev, "could not get timings from DT\n"); 9336e7674c3SLukasz Luba return -EINVAL; 9346e7674c3SLukasz Luba } 9356e7674c3SLukasz Luba 9366e7674c3SLukasz Luba dmc->min_tck = of_lpddr3_get_min_tck(np_ddr, dmc->dev); 9376e7674c3SLukasz Luba if (!dmc->min_tck) { 9386e7674c3SLukasz Luba of_node_put(np_ddr); 9396e7674c3SLukasz Luba dev_warn(dmc->dev, "could not get tck from DT\n"); 9406e7674c3SLukasz Luba return -EINVAL; 9416e7674c3SLukasz Luba } 9426e7674c3SLukasz Luba 9436e7674c3SLukasz Luba /* Sorted array of OPPs with frequency ascending */ 9446e7674c3SLukasz Luba for (idx = 0; idx < dmc->opp_count; idx++) { 9456e7674c3SLukasz Luba freq_mhz = dmc->opp[idx].freq_hz / 1000000; 9466e7674c3SLukasz Luba clk_period_ps = 1000000 / freq_mhz; 9476e7674c3SLukasz Luba 9486e7674c3SLukasz Luba ret = create_timings_aligned(dmc, &dmc->timing_row[idx], 9496e7674c3SLukasz Luba &dmc->timing_data[idx], 9506e7674c3SLukasz Luba &dmc->timing_power[idx], 9516e7674c3SLukasz Luba clk_period_ps); 9526e7674c3SLukasz Luba } 9536e7674c3SLukasz Luba 9546e7674c3SLukasz Luba of_node_put(np_ddr); 9556e7674c3SLukasz Luba 9566e7674c3SLukasz Luba /* Take the highest frequency's timings as 'bypass' */ 9576e7674c3SLukasz Luba dmc->bypass_timing_row = dmc->timing_row[idx - 1]; 9586e7674c3SLukasz Luba dmc->bypass_timing_data = dmc->timing_data[idx - 1]; 9596e7674c3SLukasz Luba dmc->bypass_timing_power = dmc->timing_power[idx - 1]; 9606e7674c3SLukasz Luba 9616e7674c3SLukasz Luba return ret; 9626e7674c3SLukasz Luba } 9636e7674c3SLukasz Luba 9646e7674c3SLukasz Luba /** 9656e7674c3SLukasz Luba * exynos5_dmc_init_clks() - Initialize clocks needed for DMC operation. 9666e7674c3SLukasz Luba * @dmc: DMC structure containing needed fields 9676e7674c3SLukasz Luba * 9686e7674c3SLukasz Luba * Get the needed clocks defined in DT device, enable and set the right parents. 9696e7674c3SLukasz Luba * Read current frequency and initialize the initial rate for governor. 9706e7674c3SLukasz Luba */ 9716e7674c3SLukasz Luba static int exynos5_dmc_init_clks(struct exynos5_dmc *dmc) 9726e7674c3SLukasz Luba { 9736e7674c3SLukasz Luba int ret; 9746e7674c3SLukasz Luba unsigned long target_volt = 0; 9756e7674c3SLukasz Luba unsigned long target_rate = 0; 9766e7674c3SLukasz Luba unsigned int tmp; 9776e7674c3SLukasz Luba 9786e7674c3SLukasz Luba dmc->fout_spll = devm_clk_get(dmc->dev, "fout_spll"); 9796e7674c3SLukasz Luba if (IS_ERR(dmc->fout_spll)) 9806e7674c3SLukasz Luba return PTR_ERR(dmc->fout_spll); 9816e7674c3SLukasz Luba 9826e7674c3SLukasz Luba dmc->fout_bpll = devm_clk_get(dmc->dev, "fout_bpll"); 9836e7674c3SLukasz Luba if (IS_ERR(dmc->fout_bpll)) 9846e7674c3SLukasz Luba return PTR_ERR(dmc->fout_bpll); 9856e7674c3SLukasz Luba 9866e7674c3SLukasz Luba dmc->mout_mclk_cdrex = devm_clk_get(dmc->dev, "mout_mclk_cdrex"); 9876e7674c3SLukasz Luba if (IS_ERR(dmc->mout_mclk_cdrex)) 9886e7674c3SLukasz Luba return PTR_ERR(dmc->mout_mclk_cdrex); 9896e7674c3SLukasz Luba 9906e7674c3SLukasz Luba dmc->mout_bpll = devm_clk_get(dmc->dev, "mout_bpll"); 9916e7674c3SLukasz Luba if (IS_ERR(dmc->mout_bpll)) 9926e7674c3SLukasz Luba return PTR_ERR(dmc->mout_bpll); 9936e7674c3SLukasz Luba 9946e7674c3SLukasz Luba dmc->mout_mx_mspll_ccore = devm_clk_get(dmc->dev, 9956e7674c3SLukasz Luba "mout_mx_mspll_ccore"); 9966e7674c3SLukasz Luba if (IS_ERR(dmc->mout_mx_mspll_ccore)) 9976e7674c3SLukasz Luba return PTR_ERR(dmc->mout_mx_mspll_ccore); 9986e7674c3SLukasz Luba 9996e7674c3SLukasz Luba dmc->mout_spll = devm_clk_get(dmc->dev, "ff_dout_spll2"); 10006e7674c3SLukasz Luba if (IS_ERR(dmc->mout_spll)) { 10016e7674c3SLukasz Luba dmc->mout_spll = devm_clk_get(dmc->dev, "mout_sclk_spll"); 10026e7674c3SLukasz Luba if (IS_ERR(dmc->mout_spll)) 10036e7674c3SLukasz Luba return PTR_ERR(dmc->mout_spll); 10046e7674c3SLukasz Luba } 10056e7674c3SLukasz Luba 10066e7674c3SLukasz Luba /* 10076e7674c3SLukasz Luba * Convert frequency to KHz values and set it for the governor. 10086e7674c3SLukasz Luba */ 10096e7674c3SLukasz Luba dmc->curr_rate = clk_get_rate(dmc->mout_mclk_cdrex); 10106e7674c3SLukasz Luba dmc->curr_rate = exynos5_dmc_align_init_freq(dmc, dmc->curr_rate); 10116e7674c3SLukasz Luba exynos5_dmc_df_profile.initial_freq = dmc->curr_rate; 10126e7674c3SLukasz Luba 10136e7674c3SLukasz Luba ret = exynos5_dmc_get_volt_freq(dmc, &dmc->curr_rate, &target_rate, 10146e7674c3SLukasz Luba &target_volt, 0); 10156e7674c3SLukasz Luba if (ret) 10166e7674c3SLukasz Luba return ret; 10176e7674c3SLukasz Luba 10186e7674c3SLukasz Luba dmc->curr_volt = target_volt; 10196e7674c3SLukasz Luba 10206e7674c3SLukasz Luba clk_set_parent(dmc->mout_mx_mspll_ccore, dmc->mout_spll); 10216e7674c3SLukasz Luba 10226e7674c3SLukasz Luba dmc->bypass_rate = clk_get_rate(dmc->mout_mx_mspll_ccore); 10236e7674c3SLukasz Luba 10246e7674c3SLukasz Luba clk_prepare_enable(dmc->fout_bpll); 10256e7674c3SLukasz Luba clk_prepare_enable(dmc->mout_bpll); 10266e7674c3SLukasz Luba 10276e7674c3SLukasz Luba /* 10286e7674c3SLukasz Luba * Some bootloaders do not set clock routes correctly. 10296e7674c3SLukasz Luba * Stop one path in clocks to PHY. 10306e7674c3SLukasz Luba */ 10316e7674c3SLukasz Luba regmap_read(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, &tmp); 10326e7674c3SLukasz Luba tmp &= ~(BIT(1) | BIT(0)); 10336e7674c3SLukasz Luba regmap_write(dmc->clk_regmap, CDREX_LPDDR3PHY_CLKM_SRC, tmp); 10346e7674c3SLukasz Luba 10356e7674c3SLukasz Luba return 0; 10366e7674c3SLukasz Luba } 10376e7674c3SLukasz Luba 10386e7674c3SLukasz Luba /** 10396e7674c3SLukasz Luba * exynos5_performance_counters_init() - Initializes performance DMC's counters 10406e7674c3SLukasz Luba * @dmc: DMC for which it does the setup 10416e7674c3SLukasz Luba * 10426e7674c3SLukasz Luba * Initialization of performance counters in DMC for estimating usage. 10436e7674c3SLukasz Luba * The counter's values are used for calculation of a memory bandwidth and based 10446e7674c3SLukasz Luba * on that the governor changes the frequency. 10456e7674c3SLukasz Luba * The counters are not used when the governor is GOVERNOR_USERSPACE. 10466e7674c3SLukasz Luba */ 10476e7674c3SLukasz Luba static int exynos5_performance_counters_init(struct exynos5_dmc *dmc) 10486e7674c3SLukasz Luba { 10496e7674c3SLukasz Luba int counters_size; 10506e7674c3SLukasz Luba int ret, i; 10516e7674c3SLukasz Luba 10526e7674c3SLukasz Luba dmc->num_counters = devfreq_event_get_edev_count(dmc->dev); 10536e7674c3SLukasz Luba if (dmc->num_counters < 0) { 10546e7674c3SLukasz Luba dev_err(dmc->dev, "could not get devfreq-event counters\n"); 10556e7674c3SLukasz Luba return dmc->num_counters; 10566e7674c3SLukasz Luba } 10576e7674c3SLukasz Luba 10586e7674c3SLukasz Luba counters_size = sizeof(struct devfreq_event_dev) * dmc->num_counters; 10596e7674c3SLukasz Luba dmc->counter = devm_kzalloc(dmc->dev, counters_size, GFP_KERNEL); 10606e7674c3SLukasz Luba if (!dmc->counter) 10616e7674c3SLukasz Luba return -ENOMEM; 10626e7674c3SLukasz Luba 10636e7674c3SLukasz Luba for (i = 0; i < dmc->num_counters; i++) { 10646e7674c3SLukasz Luba dmc->counter[i] = 10656e7674c3SLukasz Luba devfreq_event_get_edev_by_phandle(dmc->dev, i); 10666e7674c3SLukasz Luba if (IS_ERR_OR_NULL(dmc->counter[i])) 10676e7674c3SLukasz Luba return -EPROBE_DEFER; 10686e7674c3SLukasz Luba } 10696e7674c3SLukasz Luba 10706e7674c3SLukasz Luba ret = exynos5_counters_enable_edev(dmc); 10716e7674c3SLukasz Luba if (ret < 0) { 10726e7674c3SLukasz Luba dev_err(dmc->dev, "could not enable event counter\n"); 10736e7674c3SLukasz Luba return ret; 10746e7674c3SLukasz Luba } 10756e7674c3SLukasz Luba 10766e7674c3SLukasz Luba ret = exynos5_counters_set_event(dmc); 10776e7674c3SLukasz Luba if (ret < 0) { 10786e7674c3SLukasz Luba exynos5_counters_disable_edev(dmc); 10797a5a687eSColin Ian King dev_err(dmc->dev, "could not set event counter\n"); 10806e7674c3SLukasz Luba return ret; 10816e7674c3SLukasz Luba } 10826e7674c3SLukasz Luba 10836e7674c3SLukasz Luba return 0; 10846e7674c3SLukasz Luba } 10856e7674c3SLukasz Luba 10866e7674c3SLukasz Luba /** 10876e7674c3SLukasz Luba * exynos5_dmc_set_pause_on_switching() - Controls a pause feature in DMC 10886e7674c3SLukasz Luba * @dmc: device which is used for changing this feature 10896e7674c3SLukasz Luba * @set: a boolean state passing enable/disable request 10906e7674c3SLukasz Luba * 10916e7674c3SLukasz Luba * There is a need of pausing DREX DMC when divider or MUX in clock tree 10926e7674c3SLukasz Luba * changes its configuration. In such situation access to the memory is blocked 10936e7674c3SLukasz Luba * in DMC automatically. This feature is used when clock frequency change 10946e7674c3SLukasz Luba * request appears and touches clock tree. 10956e7674c3SLukasz Luba */ 10966e7674c3SLukasz Luba static inline int exynos5_dmc_set_pause_on_switching(struct exynos5_dmc *dmc) 10976e7674c3SLukasz Luba { 10986e7674c3SLukasz Luba unsigned int val; 10996e7674c3SLukasz Luba int ret; 11006e7674c3SLukasz Luba 11016e7674c3SLukasz Luba ret = regmap_read(dmc->clk_regmap, CDREX_PAUSE, &val); 11026e7674c3SLukasz Luba if (ret) 11036e7674c3SLukasz Luba return ret; 11046e7674c3SLukasz Luba 11056e7674c3SLukasz Luba val |= 1UL; 11066e7674c3SLukasz Luba regmap_write(dmc->clk_regmap, CDREX_PAUSE, val); 11076e7674c3SLukasz Luba 11086e7674c3SLukasz Luba return 0; 11096e7674c3SLukasz Luba } 11106e7674c3SLukasz Luba 11116e7674c3SLukasz Luba /** 11126e7674c3SLukasz Luba * exynos5_dmc_probe() - Probe function for the DMC driver 11136e7674c3SLukasz Luba * @pdev: platform device for which the driver is going to be initialized 11146e7674c3SLukasz Luba * 11156e7674c3SLukasz Luba * Initialize basic components: clocks, regulators, performance counters, etc. 11166e7674c3SLukasz Luba * Read out product version and based on the information setup 11176e7674c3SLukasz Luba * internal structures for the controller (frequency and voltage) and for DRAM 11186e7674c3SLukasz Luba * memory parameters: timings for each operating frequency. 11196e7674c3SLukasz Luba * Register new devfreq device for controlling DVFS of the DMC. 11206e7674c3SLukasz Luba */ 11216e7674c3SLukasz Luba static int exynos5_dmc_probe(struct platform_device *pdev) 11226e7674c3SLukasz Luba { 11236e7674c3SLukasz Luba int ret = 0; 11246e7674c3SLukasz Luba struct device *dev = &pdev->dev; 11256e7674c3SLukasz Luba struct device_node *np = dev->of_node; 11266e7674c3SLukasz Luba struct exynos5_dmc *dmc; 11276e7674c3SLukasz Luba struct resource *res; 11286e7674c3SLukasz Luba 11296e7674c3SLukasz Luba dmc = devm_kzalloc(dev, sizeof(*dmc), GFP_KERNEL); 11306e7674c3SLukasz Luba if (!dmc) 11316e7674c3SLukasz Luba return -ENOMEM; 11326e7674c3SLukasz Luba 11336e7674c3SLukasz Luba mutex_init(&dmc->lock); 11346e7674c3SLukasz Luba 11356e7674c3SLukasz Luba dmc->dev = dev; 11366e7674c3SLukasz Luba platform_set_drvdata(pdev, dmc); 11376e7674c3SLukasz Luba 11386e7674c3SLukasz Luba res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 11396e7674c3SLukasz Luba dmc->base_drexi0 = devm_ioremap_resource(dev, res); 11406e7674c3SLukasz Luba if (IS_ERR(dmc->base_drexi0)) 11416e7674c3SLukasz Luba return PTR_ERR(dmc->base_drexi0); 11426e7674c3SLukasz Luba 11436e7674c3SLukasz Luba res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 11446e7674c3SLukasz Luba dmc->base_drexi1 = devm_ioremap_resource(dev, res); 11456e7674c3SLukasz Luba if (IS_ERR(dmc->base_drexi1)) 11466e7674c3SLukasz Luba return PTR_ERR(dmc->base_drexi1); 11476e7674c3SLukasz Luba 11486e7674c3SLukasz Luba dmc->clk_regmap = syscon_regmap_lookup_by_phandle(np, 11496e7674c3SLukasz Luba "samsung,syscon-clk"); 11506e7674c3SLukasz Luba if (IS_ERR(dmc->clk_regmap)) 11516e7674c3SLukasz Luba return PTR_ERR(dmc->clk_regmap); 11526e7674c3SLukasz Luba 11536e7674c3SLukasz Luba ret = exynos5_init_freq_table(dmc, &exynos5_dmc_df_profile); 11546e7674c3SLukasz Luba if (ret) { 11556e7674c3SLukasz Luba dev_warn(dev, "couldn't initialize frequency settings\n"); 11566e7674c3SLukasz Luba return ret; 11576e7674c3SLukasz Luba } 11586e7674c3SLukasz Luba 11596e7674c3SLukasz Luba dmc->vdd_mif = devm_regulator_get(dev, "vdd"); 11606e7674c3SLukasz Luba if (IS_ERR(dmc->vdd_mif)) { 11616e7674c3SLukasz Luba ret = PTR_ERR(dmc->vdd_mif); 11626e7674c3SLukasz Luba return ret; 11636e7674c3SLukasz Luba } 11646e7674c3SLukasz Luba 11656e7674c3SLukasz Luba ret = exynos5_dmc_init_clks(dmc); 11666e7674c3SLukasz Luba if (ret) 11676e7674c3SLukasz Luba return ret; 11686e7674c3SLukasz Luba 11696e7674c3SLukasz Luba ret = of_get_dram_timings(dmc); 11706e7674c3SLukasz Luba if (ret) { 11716e7674c3SLukasz Luba dev_warn(dev, "couldn't initialize timings settings\n"); 11726e7674c3SLukasz Luba goto remove_clocks; 11736e7674c3SLukasz Luba } 11746e7674c3SLukasz Luba 11756e7674c3SLukasz Luba ret = exynos5_performance_counters_init(dmc); 11766e7674c3SLukasz Luba if (ret) { 11776e7674c3SLukasz Luba dev_warn(dev, "couldn't probe performance counters\n"); 11786e7674c3SLukasz Luba goto remove_clocks; 11796e7674c3SLukasz Luba } 11806e7674c3SLukasz Luba 11816e7674c3SLukasz Luba ret = exynos5_dmc_set_pause_on_switching(dmc); 11826e7674c3SLukasz Luba if (ret) { 11836e7674c3SLukasz Luba dev_warn(dev, "couldn't get access to PAUSE register\n"); 11846e7674c3SLukasz Luba goto err_devfreq_add; 11856e7674c3SLukasz Luba } 11866e7674c3SLukasz Luba 11876e7674c3SLukasz Luba /* 11886e7674c3SLukasz Luba * Setup default thresholds for the devfreq governor. 11896e7674c3SLukasz Luba * The values are chosen based on experiments. 11906e7674c3SLukasz Luba */ 11916e7674c3SLukasz Luba dmc->gov_data.upthreshold = 30; 11926e7674c3SLukasz Luba dmc->gov_data.downdifferential = 5; 11936e7674c3SLukasz Luba 11946e7674c3SLukasz Luba dmc->df = devm_devfreq_add_device(dev, &exynos5_dmc_df_profile, 11956e7674c3SLukasz Luba DEVFREQ_GOV_SIMPLE_ONDEMAND, 11966e7674c3SLukasz Luba &dmc->gov_data); 11976e7674c3SLukasz Luba 11986e7674c3SLukasz Luba if (IS_ERR(dmc->df)) { 11996e7674c3SLukasz Luba ret = PTR_ERR(dmc->df); 12006e7674c3SLukasz Luba goto err_devfreq_add; 12016e7674c3SLukasz Luba } 12026e7674c3SLukasz Luba 12036e7674c3SLukasz Luba dev_info(dev, "DMC initialized\n"); 12046e7674c3SLukasz Luba 12056e7674c3SLukasz Luba return 0; 12066e7674c3SLukasz Luba 12076e7674c3SLukasz Luba err_devfreq_add: 12086e7674c3SLukasz Luba exynos5_counters_disable_edev(dmc); 12096e7674c3SLukasz Luba remove_clocks: 12106e7674c3SLukasz Luba clk_disable_unprepare(dmc->mout_bpll); 12116e7674c3SLukasz Luba clk_disable_unprepare(dmc->fout_bpll); 12126e7674c3SLukasz Luba 12136e7674c3SLukasz Luba return ret; 12146e7674c3SLukasz Luba } 12156e7674c3SLukasz Luba 12166e7674c3SLukasz Luba /** 12176e7674c3SLukasz Luba * exynos5_dmc_remove() - Remove function for the platform device 12186e7674c3SLukasz Luba * @pdev: platform device which is going to be removed 12196e7674c3SLukasz Luba * 12206e7674c3SLukasz Luba * The function relies on 'devm' framework function which automatically 12216e7674c3SLukasz Luba * clean the device's resources. It just calls explicitly disable function for 12226e7674c3SLukasz Luba * the performance counters. 12236e7674c3SLukasz Luba */ 12246e7674c3SLukasz Luba static int exynos5_dmc_remove(struct platform_device *pdev) 12256e7674c3SLukasz Luba { 12266e7674c3SLukasz Luba struct exynos5_dmc *dmc = dev_get_drvdata(&pdev->dev); 12276e7674c3SLukasz Luba 12286e7674c3SLukasz Luba exynos5_counters_disable_edev(dmc); 12296e7674c3SLukasz Luba 12306e7674c3SLukasz Luba clk_disable_unprepare(dmc->mout_bpll); 12316e7674c3SLukasz Luba clk_disable_unprepare(dmc->fout_bpll); 12326e7674c3SLukasz Luba 12336e7674c3SLukasz Luba dev_pm_opp_remove_table(dmc->dev); 12346e7674c3SLukasz Luba 12356e7674c3SLukasz Luba return 0; 12366e7674c3SLukasz Luba } 12376e7674c3SLukasz Luba 12386e7674c3SLukasz Luba static const struct of_device_id exynos5_dmc_of_match[] = { 12396e7674c3SLukasz Luba { .compatible = "samsung,exynos5422-dmc", }, 12406e7674c3SLukasz Luba { }, 12416e7674c3SLukasz Luba }; 12426e7674c3SLukasz Luba MODULE_DEVICE_TABLE(of, exynos5_dmc_of_match); 12436e7674c3SLukasz Luba 12446e7674c3SLukasz Luba static struct platform_driver exynos5_dmc_platdrv = { 12456e7674c3SLukasz Luba .probe = exynos5_dmc_probe, 12466e7674c3SLukasz Luba .remove = exynos5_dmc_remove, 12476e7674c3SLukasz Luba .driver = { 12486e7674c3SLukasz Luba .name = "exynos5-dmc", 12496e7674c3SLukasz Luba .of_match_table = exynos5_dmc_of_match, 12506e7674c3SLukasz Luba }, 12516e7674c3SLukasz Luba }; 12526e7674c3SLukasz Luba module_platform_driver(exynos5_dmc_platdrv); 12536e7674c3SLukasz Luba MODULE_DESCRIPTION("Driver for Exynos5422 Dynamic Memory Controller dynamic frequency and voltage change"); 12546e7674c3SLukasz Luba MODULE_LICENSE("GPL v2"); 12556e7674c3SLukasz Luba MODULE_AUTHOR("Lukasz Luba"); 1256