aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mmc
diff options
context:
space:
mode:
authorAlexandru M Stan <amstan@chromium.org>2015-10-12 08:48:28 -0400
committerUlf Hansson <ulf.hansson@linaro.org>2015-10-26 11:00:12 -0400
commitcbb79e43c82635840cdcbf71b1d1c374e2c3a025 (patch)
tree76a30a5348f966fecccdd338c672e3b0a5640f03 /drivers/mmc
parentf8085bbada3b124b5b39b61bb1a7a0a78c24604b (diff)
mmc: dw_mmc-rockchip: MMC tuning with the clock phase framework
This algorithm will try 1 degree increments, since there's no way to tell what resolution the underlying phase code uses. As an added bonus, doing many tunings yields better results since some tests are run more than once (ex: if the underlying driver uses 45 degree increments, the tuning code will try the same angle more than once). It will then construct a list of good phase ranges (even ranges that cross 360/0), will pick the biggest range then it will set the sample_clk to the middle of that range. We do not touch ciu_drive (and by extension define default-drive-phase). Drive phase is mostly used to define minimum hold times, while one could write some code to determine what phase meets the minimum hold time (ex 10 degrees) this will not work with the current clock phase framework (which floors angles, so we'll get 0 deg, and there's no way to know what resolution the floors happen at). We assume that the default drive angles set by the hardware are good enough. If a device has device specific code (like exynos) then that will still take precedence, otherwise this new code will execute. If the device wants to tune, but has no sample_clk defined we'll return EIO with an error message. Signed-off-by: Alexandru M Stan <amstan@chromium.org> Signed-off-by: Heiko Stuebner <heiko@sntech.de> Acked-by: Jaehoon Chung <jh80.chung@samsung.com> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
Diffstat (limited to 'drivers/mmc')
-rw-r--r--drivers/mmc/host/dw_mmc-rockchip.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/drivers/mmc/host/dw_mmc-rockchip.c b/drivers/mmc/host/dw_mmc-rockchip.c
index bc76aa22473e..4b3650f7d43f 100644
--- a/drivers/mmc/host/dw_mmc-rockchip.c
+++ b/drivers/mmc/host/dw_mmc-rockchip.c
@@ -13,12 +13,19 @@
13#include <linux/mmc/host.h> 13#include <linux/mmc/host.h>
14#include <linux/mmc/dw_mmc.h> 14#include <linux/mmc/dw_mmc.h>
15#include <linux/of_address.h> 15#include <linux/of_address.h>
16#include <linux/slab.h>
16 17
17#include "dw_mmc.h" 18#include "dw_mmc.h"
18#include "dw_mmc-pltfm.h" 19#include "dw_mmc-pltfm.h"
19 20
20#define RK3288_CLKGEN_DIV 2 21#define RK3288_CLKGEN_DIV 2
21 22
23struct dw_mci_rockchip_priv_data {
24 struct clk *drv_clk;
25 struct clk *sample_clk;
26 int default_sample_phase;
27};
28
22static void dw_mci_rockchip_prepare_command(struct dw_mci *host, u32 *cmdr) 29static void dw_mci_rockchip_prepare_command(struct dw_mci *host, u32 *cmdr)
23{ 30{
24 *cmdr |= SDMMC_CMD_USE_HOLD_REG; 31 *cmdr |= SDMMC_CMD_USE_HOLD_REG;
@@ -33,6 +40,7 @@ static int dw_mci_rk3288_setup_clock(struct dw_mci *host)
33 40
34static void dw_mci_rk3288_set_ios(struct dw_mci *host, struct mmc_ios *ios) 41static void dw_mci_rk3288_set_ios(struct dw_mci *host, struct mmc_ios *ios)
35{ 42{
43 struct dw_mci_rockchip_priv_data *priv = host->priv;
36 int ret; 44 int ret;
37 unsigned int cclkin; 45 unsigned int cclkin;
38 u32 bus_hz; 46 u32 bus_hz;
@@ -66,6 +74,158 @@ static void dw_mci_rk3288_set_ios(struct dw_mci *host, struct mmc_ios *ios)
66 /* force dw_mci_setup_bus() */ 74 /* force dw_mci_setup_bus() */
67 host->current_speed = 0; 75 host->current_speed = 0;
68 } 76 }
77
78 /* Make sure we use phases which we can enumerate with */
79 if (!IS_ERR(priv->sample_clk))
80 clk_set_phase(priv->sample_clk, priv->default_sample_phase);
81}
82
83#define NUM_PHASES 360
84#define TUNING_ITERATION_TO_PHASE(i) (DIV_ROUND_UP((i) * 360, NUM_PHASES))
85
86static int dw_mci_rk3288_execute_tuning(struct dw_mci_slot *slot)
87{
88 struct dw_mci *host = slot->host;
89 struct dw_mci_rockchip_priv_data *priv = host->priv;
90 struct mmc_host *mmc = slot->mmc;
91 int ret = 0;
92 int i;
93 bool v, prev_v = 0, first_v;
94 struct range_t {
95 int start;
96 int end; /* inclusive */
97 };
98 struct range_t *ranges;
99 unsigned int range_count = 0;
100 int longest_range_len = -1;
101 int longest_range = -1;
102 int middle_phase;
103
104 if (IS_ERR(priv->sample_clk)) {
105 dev_err(host->dev, "Tuning clock (sample_clk) not defined.\n");
106 return -EIO;
107 }
108
109 ranges = kmalloc_array(NUM_PHASES / 2 + 1, sizeof(*ranges), GFP_KERNEL);
110 if (!ranges)
111 return -ENOMEM;
112
113 /* Try each phase and extract good ranges */
114 for (i = 0; i < NUM_PHASES; ) {
115 clk_set_phase(priv->sample_clk, TUNING_ITERATION_TO_PHASE(i));
116
117 v = !mmc_send_tuning(mmc);
118
119 if (i == 0)
120 first_v = v;
121
122 if ((!prev_v) && v) {
123 range_count++;
124 ranges[range_count-1].start = i;
125 }
126 if (v) {
127 ranges[range_count-1].end = i;
128 i++;
129 } else if (i == NUM_PHASES - 1) {
130 /* No extra skipping rules if we're at the end */
131 i++;
132 } else {
133 /*
134 * No need to check too close to an invalid
135 * one since testing bad phases is slow. Skip
136 * 20 degrees.
137 */
138 i += DIV_ROUND_UP(20 * NUM_PHASES, 360);
139
140 /* Always test the last one */
141 if (i >= NUM_PHASES)
142 i = NUM_PHASES - 1;
143 }
144
145 prev_v = v;
146 }
147
148 if (range_count == 0) {
149 dev_warn(host->dev, "All phases bad!");
150 ret = -EIO;
151 goto free;
152 }
153
154 /* wrap around case, merge the end points */
155 if ((range_count > 1) && first_v && v) {
156 ranges[0].start = ranges[range_count-1].start;
157 range_count--;
158 }
159
160 if (ranges[0].start == 0 && ranges[0].end == NUM_PHASES - 1) {
161 clk_set_phase(priv->sample_clk, priv->default_sample_phase);
162 dev_info(host->dev, "All phases work, using default phase %d.",
163 priv->default_sample_phase);
164 goto free;
165 }
166
167 /* Find the longest range */
168 for (i = 0; i < range_count; i++) {
169 int len = (ranges[i].end - ranges[i].start + 1);
170
171 if (len < 0)
172 len += NUM_PHASES;
173
174 if (longest_range_len < len) {
175 longest_range_len = len;
176 longest_range = i;
177 }
178
179 dev_dbg(host->dev, "Good phase range %d-%d (%d len)\n",
180 TUNING_ITERATION_TO_PHASE(ranges[i].start),
181 TUNING_ITERATION_TO_PHASE(ranges[i].end),
182 len
183 );
184 }
185
186 dev_dbg(host->dev, "Best phase range %d-%d (%d len)\n",
187 TUNING_ITERATION_TO_PHASE(ranges[longest_range].start),
188 TUNING_ITERATION_TO_PHASE(ranges[longest_range].end),
189 longest_range_len
190 );
191
192 middle_phase = ranges[longest_range].start + longest_range_len / 2;
193 middle_phase %= NUM_PHASES;
194 dev_info(host->dev, "Successfully tuned phase to %d\n",
195 TUNING_ITERATION_TO_PHASE(middle_phase));
196
197 clk_set_phase(priv->sample_clk,
198 TUNING_ITERATION_TO_PHASE(middle_phase));
199
200free:
201 kfree(ranges);
202 return ret;
203}
204
205static int dw_mci_rk3288_parse_dt(struct dw_mci *host)
206{
207 struct device_node *np = host->dev->of_node;
208 struct dw_mci_rockchip_priv_data *priv;
209
210 priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
211 if (!priv)
212 return -ENOMEM;
213
214 if (of_property_read_u32(np, "rockchip,default-sample-phase",
215 &priv->default_sample_phase))
216 priv->default_sample_phase = 0;
217
218 priv->drv_clk = devm_clk_get(host->dev, "ciu-drive");
219 if (IS_ERR(priv->drv_clk))
220 dev_dbg(host->dev, "ciu_drv not available\n");
221
222 priv->sample_clk = devm_clk_get(host->dev, "ciu-sample");
223 if (IS_ERR(priv->sample_clk))
224 dev_dbg(host->dev, "ciu_sample not available\n");
225
226 host->priv = priv;
227
228 return 0;
69} 229}
70 230
71static int dw_mci_rockchip_init(struct dw_mci *host) 231static int dw_mci_rockchip_init(struct dw_mci *host)
@@ -95,6 +255,8 @@ static const struct dw_mci_drv_data rk3288_drv_data = {
95 .caps = dw_mci_rk3288_dwmmc_caps, 255 .caps = dw_mci_rk3288_dwmmc_caps,
96 .prepare_command = dw_mci_rockchip_prepare_command, 256 .prepare_command = dw_mci_rockchip_prepare_command,
97 .set_ios = dw_mci_rk3288_set_ios, 257 .set_ios = dw_mci_rk3288_set_ios,
258 .execute_tuning = dw_mci_rk3288_execute_tuning,
259 .parse_dt = dw_mci_rk3288_parse_dt,
98 .setup_clock = dw_mci_rk3288_setup_clock, 260 .setup_clock = dw_mci_rk3288_setup_clock,
99 .init = dw_mci_rockchip_init, 261 .init = dw_mci_rockchip_init,
100}; 262};