blob: 35026795be2803c7387203c5719f4442c15b7c50 [file] [log] [blame]
Thomas Abrahamc3665002012-09-17 18:16:43 +00001/*
2 * Exynos Specific Extensions for Synopsys DW Multimedia Card Interface driver
3 *
4 * Copyright (C) 2012, Samsung Electronics Co., Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <linux/clk.h>
15#include <linux/mmc/host.h>
Seungwon Jeonc537a1c2013-08-31 00:12:50 +090016#include <linux/mmc/mmc.h>
Thomas Abrahamc3665002012-09-17 18:16:43 +000017#include <linux/of.h>
18#include <linux/of_gpio.h>
Shawn Lincf5237e2016-10-12 10:55:55 +080019#include <linux/pm_runtime.h>
Seungwon Jeonc537a1c2013-08-31 00:12:50 +090020#include <linux/slab.h>
Thomas Abrahamc3665002012-09-17 18:16:43 +000021
22#include "dw_mmc.h"
23#include "dw_mmc-pltfm.h"
Seungwon Jeon0b5fce42014-12-22 17:42:04 +053024#include "dw_mmc-exynos.h"
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +090025
Thomas Abrahamc3665002012-09-17 18:16:43 +000026/* Variations in Exynos specific dw-mshc controller */
27enum dw_mci_exynos_type {
28 DW_MCI_TYPE_EXYNOS4210,
29 DW_MCI_TYPE_EXYNOS4412,
30 DW_MCI_TYPE_EXYNOS5250,
Yuvaraj Kumar C D00fd0412013-05-24 15:34:32 +053031 DW_MCI_TYPE_EXYNOS5420,
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +090032 DW_MCI_TYPE_EXYNOS5420_SMU,
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +053033 DW_MCI_TYPE_EXYNOS7,
34 DW_MCI_TYPE_EXYNOS7_SMU,
Thomas Abrahamc3665002012-09-17 18:16:43 +000035};
36
37/* Exynos implementation specific driver private data */
38struct dw_mci_exynos_priv_data {
39 enum dw_mci_exynos_type ctrl_type;
40 u8 ciu_div;
41 u32 sdr_timing;
42 u32 ddr_timing;
Seungwon Jeon80113132015-01-29 08:11:57 +053043 u32 hs400_timing;
44 u32 tuned_sample;
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +090045 u32 cur_speed;
Seungwon Jeon80113132015-01-29 08:11:57 +053046 u32 dqs_delay;
47 u32 saved_dqs_en;
48 u32 saved_strobe_ctrl;
Thomas Abrahamc3665002012-09-17 18:16:43 +000049};
50
51static struct dw_mci_exynos_compatible {
52 char *compatible;
53 enum dw_mci_exynos_type ctrl_type;
54} exynos_compat[] = {
55 {
56 .compatible = "samsung,exynos4210-dw-mshc",
57 .ctrl_type = DW_MCI_TYPE_EXYNOS4210,
58 }, {
59 .compatible = "samsung,exynos4412-dw-mshc",
60 .ctrl_type = DW_MCI_TYPE_EXYNOS4412,
61 }, {
62 .compatible = "samsung,exynos5250-dw-mshc",
63 .ctrl_type = DW_MCI_TYPE_EXYNOS5250,
Yuvaraj Kumar C D00fd0412013-05-24 15:34:32 +053064 }, {
65 .compatible = "samsung,exynos5420-dw-mshc",
66 .ctrl_type = DW_MCI_TYPE_EXYNOS5420,
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +090067 }, {
68 .compatible = "samsung,exynos5420-dw-mshc-smu",
69 .ctrl_type = DW_MCI_TYPE_EXYNOS5420_SMU,
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +053070 }, {
71 .compatible = "samsung,exynos7-dw-mshc",
72 .ctrl_type = DW_MCI_TYPE_EXYNOS7,
73 }, {
74 .compatible = "samsung,exynos7-dw-mshc-smu",
75 .ctrl_type = DW_MCI_TYPE_EXYNOS7_SMU,
Thomas Abrahamc3665002012-09-17 18:16:43 +000076 },
77};
78
Seungwon Jeon80113132015-01-29 08:11:57 +053079static inline u8 dw_mci_exynos_get_ciu_div(struct dw_mci *host)
80{
81 struct dw_mci_exynos_priv_data *priv = host->priv;
82
83 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
84 return EXYNOS4412_FIXED_CIU_CLK_DIV;
85 else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
86 return EXYNOS4210_FIXED_CIU_CLK_DIV;
87 else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
88 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
89 return SDMMC_CLKSEL_GET_DIV(mci_readl(host, CLKSEL64)) + 1;
90 else
91 return SDMMC_CLKSEL_GET_DIV(mci_readl(host, CLKSEL)) + 1;
92}
93
Jaehoon Chung5659eea2016-03-31 14:53:18 +090094static void dw_mci_exynos_config_smu(struct dw_mci *host)
Thomas Abrahamc3665002012-09-17 18:16:43 +000095{
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +090096 struct dw_mci_exynos_priv_data *priv = host->priv;
Thomas Abrahamc3665002012-09-17 18:16:43 +000097
Jaehoon Chung5659eea2016-03-31 14:53:18 +090098 /*
99 * If Exynos is provided the Security management,
100 * set for non-ecryption mode at this time.
101 */
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530102 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU ||
103 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU) {
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +0900104 mci_writel(host, MPSBEGIN0, 0);
Seungwon Jeon0b5fce42014-12-22 17:42:04 +0530105 mci_writel(host, MPSEND0, SDMMC_ENDING_SEC_NR_MAX);
106 mci_writel(host, MPSCTRL0, SDMMC_MPSCTRL_SECURE_WRITE_BIT |
107 SDMMC_MPSCTRL_NON_SECURE_READ_BIT |
108 SDMMC_MPSCTRL_VALID |
109 SDMMC_MPSCTRL_NON_SECURE_WRITE_BIT);
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +0900110 }
Jaehoon Chung5659eea2016-03-31 14:53:18 +0900111}
112
113static int dw_mci_exynos_priv_init(struct dw_mci *host)
114{
115 struct dw_mci_exynos_priv_data *priv = host->priv;
116
117 dw_mci_exynos_config_smu(host);
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +0900118
Seungwon Jeon80113132015-01-29 08:11:57 +0530119 if (priv->ctrl_type >= DW_MCI_TYPE_EXYNOS5420) {
120 priv->saved_strobe_ctrl = mci_readl(host, HS400_DLINE_CTRL);
121 priv->saved_dqs_en = mci_readl(host, HS400_DQS_EN);
122 priv->saved_dqs_en |= AXI_NON_BLOCKING_WR;
123 mci_writel(host, HS400_DQS_EN, priv->saved_dqs_en);
124 if (!priv->dqs_delay)
125 priv->dqs_delay =
126 DQS_CTRL_GET_RD_DELAY(priv->saved_strobe_ctrl);
127 }
128
Seungwon Jeona2a1fed2014-12-22 17:42:03 +0530129 host->bus_hz /= (priv->ciu_div + 1);
130
Thomas Abrahamc3665002012-09-17 18:16:43 +0000131 return 0;
132}
133
Seungwon Jeon80113132015-01-29 08:11:57 +0530134static void dw_mci_exynos_set_clksel_timing(struct dw_mci *host, u32 timing)
135{
136 struct dw_mci_exynos_priv_data *priv = host->priv;
137 u32 clksel;
138
139 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
140 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
141 clksel = mci_readl(host, CLKSEL64);
142 else
143 clksel = mci_readl(host, CLKSEL);
144
145 clksel = (clksel & ~SDMMC_CLKSEL_TIMING_MASK) | timing;
146
147 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
148 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
149 mci_writel(host, CLKSEL64, clksel);
150 else
151 mci_writel(host, CLKSEL, clksel);
Jaehoon Chungaaaaeb72016-01-21 11:01:06 +0900152
153 /*
154 * Exynos4412 and Exynos5250 extends the use of CMD register with the
155 * use of bit 29 (which is reserved on standard MSHC controllers) for
156 * optionally bypassing the HOLD register for command and data. The
157 * HOLD register should be bypassed in case there is no phase shift
158 * applied on CMD/DATA that is sent to the card.
159 */
Jaehoon Chung42f989c2017-06-05 13:41:34 +0900160 if (!SDMMC_CLKSEL_GET_DRV_WD3(clksel) && host->slot)
161 set_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags);
Seungwon Jeon80113132015-01-29 08:11:57 +0530162}
163
Shawn Lincf5237e2016-10-12 10:55:55 +0800164#ifdef CONFIG_PM
165static int dw_mci_exynos_runtime_resume(struct device *dev)
Doug Andersone2c63592013-08-31 00:11:21 +0900166{
167 struct dw_mci *host = dev_get_drvdata(dev);
168
Jaehoon Chung5659eea2016-03-31 14:53:18 +0900169 dw_mci_exynos_config_smu(host);
Shawn Lincf5237e2016-10-12 10:55:55 +0800170 return dw_mci_runtime_resume(dev);
Doug Andersone2c63592013-08-31 00:11:21 +0900171}
172
173/**
174 * dw_mci_exynos_resume_noirq - Exynos-specific resume code
175 *
176 * On exynos5420 there is a silicon errata that will sometimes leave the
177 * WAKEUP_INT bit in the CLKSEL register asserted. This bit is 1 to indicate
178 * that it fired and we can clear it by writing a 1 back. Clear it to prevent
179 * interrupts from going off constantly.
180 *
181 * We run this code on all exynos variants because it doesn't hurt.
182 */
183
184static int dw_mci_exynos_resume_noirq(struct device *dev)
185{
186 struct dw_mci *host = dev_get_drvdata(dev);
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530187 struct dw_mci_exynos_priv_data *priv = host->priv;
Doug Andersone2c63592013-08-31 00:11:21 +0900188 u32 clksel;
189
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530190 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
191 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
192 clksel = mci_readl(host, CLKSEL64);
193 else
194 clksel = mci_readl(host, CLKSEL);
195
196 if (clksel & SDMMC_CLKSEL_WAKEUP_INT) {
197 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
198 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
199 mci_writel(host, CLKSEL64, clksel);
200 else
201 mci_writel(host, CLKSEL, clksel);
202 }
Doug Andersone2c63592013-08-31 00:11:21 +0900203
204 return 0;
205}
206#else
Doug Andersone2c63592013-08-31 00:11:21 +0900207#define dw_mci_exynos_resume_noirq NULL
Shawn Lincf5237e2016-10-12 10:55:55 +0800208#endif /* CONFIG_PM */
Doug Andersone2c63592013-08-31 00:11:21 +0900209
Seungwon Jeon80113132015-01-29 08:11:57 +0530210static void dw_mci_exynos_config_hs400(struct dw_mci *host, u32 timing)
Thomas Abrahamc3665002012-09-17 18:16:43 +0000211{
212 struct dw_mci_exynos_priv_data *priv = host->priv;
Seungwon Jeon80113132015-01-29 08:11:57 +0530213 u32 dqs, strobe;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000214
Seungwon Jeon80113132015-01-29 08:11:57 +0530215 /*
216 * Not supported to configure register
217 * related to HS400
218 */
Krzysztof Kozlowski941a6592016-07-14 15:22:27 +0200219 if (priv->ctrl_type < DW_MCI_TYPE_EXYNOS5420) {
220 if (timing == MMC_TIMING_MMC_HS400)
221 dev_warn(host->dev,
222 "cannot configure HS400, unsupported chipset\n");
Seungwon Jeon80113132015-01-29 08:11:57 +0530223 return;
Krzysztof Kozlowski941a6592016-07-14 15:22:27 +0200224 }
Seungwon Jeon80113132015-01-29 08:11:57 +0530225
226 dqs = priv->saved_dqs_en;
227 strobe = priv->saved_strobe_ctrl;
228
229 if (timing == MMC_TIMING_MMC_HS400) {
230 dqs |= DATA_STROBE_EN;
231 strobe = DQS_CTRL_RD_DELAY(strobe, priv->dqs_delay);
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900232 } else {
Seungwon Jeon80113132015-01-29 08:11:57 +0530233 dqs &= ~DATA_STROBE_EN;
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900234 }
235
Seungwon Jeon80113132015-01-29 08:11:57 +0530236 mci_writel(host, HS400_DQS_EN, dqs);
237 mci_writel(host, HS400_DLINE_CTRL, strobe);
238}
239
240static void dw_mci_exynos_adjust_clock(struct dw_mci *host, unsigned int wanted)
241{
242 struct dw_mci_exynos_priv_data *priv = host->priv;
243 unsigned long actual;
244 u8 div;
245 int ret;
Seungwon Jeona2a1fed2014-12-22 17:42:03 +0530246 /*
247 * Don't care if wanted clock is zero or
248 * ciu clock is unavailable
249 */
250 if (!wanted || IS_ERR(host->ciu_clk))
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900251 return;
252
253 /* Guaranteed minimum frequency for cclkin */
254 if (wanted < EXYNOS_CCLKIN_MIN)
255 wanted = EXYNOS_CCLKIN_MIN;
256
Seungwon Jeon80113132015-01-29 08:11:57 +0530257 if (wanted == priv->cur_speed)
258 return;
259
260 div = dw_mci_exynos_get_ciu_div(host);
261 ret = clk_set_rate(host->ciu_clk, wanted * div);
262 if (ret)
263 dev_warn(host->dev,
264 "failed to set clk-rate %u error: %d\n",
265 wanted * div, ret);
266 actual = clk_get_rate(host->ciu_clk);
267 host->bus_hz = actual / div;
268 priv->cur_speed = wanted;
269 host->current_speed = 0;
270}
271
272static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
273{
274 struct dw_mci_exynos_priv_data *priv = host->priv;
275 unsigned int wanted = ios->clock;
276 u32 timing = ios->timing, clksel;
277
278 switch (timing) {
279 case MMC_TIMING_MMC_HS400:
280 /* Update tuned sample timing */
281 clksel = SDMMC_CLKSEL_UP_SAMPLE(
282 priv->hs400_timing, priv->tuned_sample);
283 wanted <<= 1;
284 break;
285 case MMC_TIMING_MMC_DDR52:
286 clksel = priv->ddr_timing;
287 /* Should be double rate for DDR mode */
288 if (ios->bus_width == MMC_BUS_WIDTH_8)
289 wanted <<= 1;
290 break;
291 default:
292 clksel = priv->sdr_timing;
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900293 }
Seungwon Jeon80113132015-01-29 08:11:57 +0530294
295 /* Set clock timing for the requested speed mode*/
296 dw_mci_exynos_set_clksel_timing(host, clksel);
297
298 /* Configure setting for HS400 */
299 dw_mci_exynos_config_hs400(host, timing);
300
301 /* Configure clock rate */
302 dw_mci_exynos_adjust_clock(host, wanted);
Thomas Abrahamc3665002012-09-17 18:16:43 +0000303}
304
305static int dw_mci_exynos_parse_dt(struct dw_mci *host)
306{
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900307 struct dw_mci_exynos_priv_data *priv;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000308 struct device_node *np = host->dev->of_node;
309 u32 timing[2];
310 u32 div = 0;
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900311 int idx;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000312 int ret;
313
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900314 priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
Beomho Seobf3707e2014-12-23 21:07:33 +0900315 if (!priv)
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900316 return -ENOMEM;
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900317
318 for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
319 if (of_device_is_compatible(np, exynos_compat[idx].compatible))
320 priv->ctrl_type = exynos_compat[idx].ctrl_type;
321 }
322
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900323 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
324 priv->ciu_div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1;
325 else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
326 priv->ciu_div = EXYNOS4210_FIXED_CIU_CLK_DIV - 1;
327 else {
328 of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
329 priv->ciu_div = div;
330 }
Thomas Abrahamc3665002012-09-17 18:16:43 +0000331
332 ret = of_property_read_u32_array(np,
333 "samsung,dw-mshc-sdr-timing", timing, 2);
334 if (ret)
335 return ret;
336
Yuvaraj Kumar C D2d9f0bd2013-10-22 14:41:56 +0530337 priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
338
Thomas Abrahamc3665002012-09-17 18:16:43 +0000339 ret = of_property_read_u32_array(np,
340 "samsung,dw-mshc-ddr-timing", timing, 2);
341 if (ret)
342 return ret;
343
344 priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
Seungwon Jeon80113132015-01-29 08:11:57 +0530345
346 ret = of_property_read_u32_array(np,
347 "samsung,dw-mshc-hs400-timing", timing, 2);
348 if (!ret && of_property_read_u32(np,
349 "samsung,read-strobe-delay", &priv->dqs_delay))
350 dev_dbg(host->dev,
351 "read-strobe-delay is not found, assuming usage of default value\n");
352
353 priv->hs400_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1],
354 HS400_FIXED_CIU_CLK_DIV);
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900355 host->priv = priv;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000356 return 0;
357}
358
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900359static inline u8 dw_mci_exynos_get_clksmpl(struct dw_mci *host)
360{
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530361 struct dw_mci_exynos_priv_data *priv = host->priv;
362
363 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
364 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
365 return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL64));
366 else
367 return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL));
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900368}
369
370static inline void dw_mci_exynos_set_clksmpl(struct dw_mci *host, u8 sample)
371{
372 u32 clksel;
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530373 struct dw_mci_exynos_priv_data *priv = host->priv;
374
375 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
376 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
377 clksel = mci_readl(host, CLKSEL64);
378 else
379 clksel = mci_readl(host, CLKSEL);
Seungwon Jeon80113132015-01-29 08:11:57 +0530380 clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530381 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
382 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
383 mci_writel(host, CLKSEL64, clksel);
384 else
385 mci_writel(host, CLKSEL, clksel);
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900386}
387
388static inline u8 dw_mci_exynos_move_next_clksmpl(struct dw_mci *host)
389{
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530390 struct dw_mci_exynos_priv_data *priv = host->priv;
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900391 u32 clksel;
392 u8 sample;
393
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530394 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
395 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
396 clksel = mci_readl(host, CLKSEL64);
397 else
398 clksel = mci_readl(host, CLKSEL);
Seungwon Jeon80113132015-01-29 08:11:57 +0530399
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900400 sample = (clksel + 1) & 0x7;
Seungwon Jeon80113132015-01-29 08:11:57 +0530401 clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
402
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530403 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
404 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
405 mci_writel(host, CLKSEL64, clksel);
406 else
407 mci_writel(host, CLKSEL, clksel);
Seungwon Jeon80113132015-01-29 08:11:57 +0530408
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900409 return sample;
410}
411
412static s8 dw_mci_exynos_get_best_clksmpl(u8 candiates)
413{
414 const u8 iter = 8;
415 u8 __c;
416 s8 i, loc = -1;
417
418 for (i = 0; i < iter; i++) {
419 __c = ror8(candiates, i);
420 if ((__c & 0xc7) == 0xc7) {
421 loc = i;
422 goto out;
423 }
424 }
425
426 for (i = 0; i < iter; i++) {
427 __c = ror8(candiates, i);
428 if ((__c & 0x83) == 0x83) {
429 loc = i;
430 goto out;
431 }
432 }
433
434out:
435 return loc;
436}
437
Chaotian Jing9979dbe2015-10-27 14:24:28 +0800438static int dw_mci_exynos_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900439{
440 struct dw_mci *host = slot->host;
Seungwon Jeon80113132015-01-29 08:11:57 +0530441 struct dw_mci_exynos_priv_data *priv = host->priv;
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900442 struct mmc_host *mmc = slot->mmc;
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900443 u8 start_smpl, smpl, candiates = 0;
444 s8 found = -1;
445 int ret = 0;
446
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900447 start_smpl = dw_mci_exynos_get_clksmpl(host);
448
449 do {
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900450 mci_writel(host, TMOUT, ~0);
451 smpl = dw_mci_exynos_move_next_clksmpl(host);
452
Chaotian Jing9979dbe2015-10-27 14:24:28 +0800453 if (!mmc_send_tuning(mmc, opcode, NULL))
Ulf Hansson6c2c6502014-12-01 16:13:39 +0100454 candiates |= (1 << smpl);
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900455
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900456 } while (start_smpl != smpl);
457
458 found = dw_mci_exynos_get_best_clksmpl(candiates);
Seungwon Jeon80113132015-01-29 08:11:57 +0530459 if (found >= 0) {
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900460 dw_mci_exynos_set_clksmpl(host, found);
Seungwon Jeon80113132015-01-29 08:11:57 +0530461 priv->tuned_sample = found;
462 } else {
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900463 ret = -EIO;
Seungwon Jeon80113132015-01-29 08:11:57 +0530464 }
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900465
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900466 return ret;
467}
468
Wu Fengguangc22f5e12015-03-05 18:02:54 +0800469static int dw_mci_exynos_prepare_hs400_tuning(struct dw_mci *host,
Seungwon Jeon80113132015-01-29 08:11:57 +0530470 struct mmc_ios *ios)
471{
472 struct dw_mci_exynos_priv_data *priv = host->priv;
473
474 dw_mci_exynos_set_clksel_timing(host, priv->hs400_timing);
475 dw_mci_exynos_adjust_clock(host, (ios->clock) << 1);
476
477 return 0;
478}
479
Dongjin Kim0f6e73d2013-02-23 00:17:45 +0900480/* Common capabilities of Exynos4/Exynos5 SoC */
481static unsigned long exynos_dwmmc_caps[4] = {
Seungwon Jeoncab3a802014-03-14 21:12:43 +0900482 MMC_CAP_1_8V_DDR | MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000483 MMC_CAP_CMD23,
484 MMC_CAP_CMD23,
485 MMC_CAP_CMD23,
486};
487
Dongjin Kim0f6e73d2013-02-23 00:17:45 +0900488static const struct dw_mci_drv_data exynos_drv_data = {
489 .caps = exynos_dwmmc_caps,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000490 .init = dw_mci_exynos_priv_init,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000491 .set_ios = dw_mci_exynos_set_ios,
492 .parse_dt = dw_mci_exynos_parse_dt,
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900493 .execute_tuning = dw_mci_exynos_execute_tuning,
Seungwon Jeon80113132015-01-29 08:11:57 +0530494 .prepare_hs400_tuning = dw_mci_exynos_prepare_hs400_tuning,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000495};
496
497static const struct of_device_id dw_mci_exynos_match[] = {
Dongjin Kim0f6e73d2013-02-23 00:17:45 +0900498 { .compatible = "samsung,exynos4412-dw-mshc",
499 .data = &exynos_drv_data, },
Thomas Abrahamc3665002012-09-17 18:16:43 +0000500 { .compatible = "samsung,exynos5250-dw-mshc",
Dongjin Kim0f6e73d2013-02-23 00:17:45 +0900501 .data = &exynos_drv_data, },
Yuvaraj Kumar C D00fd0412013-05-24 15:34:32 +0530502 { .compatible = "samsung,exynos5420-dw-mshc",
503 .data = &exynos_drv_data, },
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +0900504 { .compatible = "samsung,exynos5420-dw-mshc-smu",
505 .data = &exynos_drv_data, },
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530506 { .compatible = "samsung,exynos7-dw-mshc",
507 .data = &exynos_drv_data, },
508 { .compatible = "samsung,exynos7-dw-mshc-smu",
509 .data = &exynos_drv_data, },
Thomas Abrahamc3665002012-09-17 18:16:43 +0000510 {},
511};
Arnd Bergmann517cb9f2012-11-06 22:55:30 +0100512MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
Thomas Abrahamc3665002012-09-17 18:16:43 +0000513
Sachin Kamat9665f7f2013-02-18 14:23:08 +0530514static int dw_mci_exynos_probe(struct platform_device *pdev)
Thomas Abrahamc3665002012-09-17 18:16:43 +0000515{
Arnd Bergmann8e2b36e2012-11-06 22:55:31 +0100516 const struct dw_mci_drv_data *drv_data;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000517 const struct of_device_id *match;
Joonyoung Shim9b93d3922016-11-23 18:36:02 +0900518 int ret;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000519
520 match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
521 drv_data = match->data;
Joonyoung Shim9b93d3922016-11-23 18:36:02 +0900522
523 pm_runtime_get_noresume(&pdev->dev);
524 pm_runtime_set_active(&pdev->dev);
525 pm_runtime_enable(&pdev->dev);
526
527 ret = dw_mci_pltfm_register(pdev, drv_data);
528 if (ret) {
529 pm_runtime_disable(&pdev->dev);
530 pm_runtime_set_suspended(&pdev->dev);
531 pm_runtime_put_noidle(&pdev->dev);
532
533 return ret;
534 }
535
536 return 0;
537}
538
539static int dw_mci_exynos_remove(struct platform_device *pdev)
540{
541 pm_runtime_disable(&pdev->dev);
542 pm_runtime_set_suspended(&pdev->dev);
543 pm_runtime_put_noidle(&pdev->dev);
544
545 return dw_mci_pltfm_remove(pdev);
Thomas Abrahamc3665002012-09-17 18:16:43 +0000546}
547
Sachin Kamat15a2e2a2014-03-04 10:33:25 +0530548static const struct dev_pm_ops dw_mci_exynos_pmops = {
Shawn Lincf5237e2016-10-12 10:55:55 +0800549 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
550 pm_runtime_force_resume)
551 SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
552 dw_mci_exynos_runtime_resume,
553 NULL)
Doug Andersone2c63592013-08-31 00:11:21 +0900554 .resume_noirq = dw_mci_exynos_resume_noirq,
555 .thaw_noirq = dw_mci_exynos_resume_noirq,
556 .restore_noirq = dw_mci_exynos_resume_noirq,
557};
558
Thomas Abrahamc3665002012-09-17 18:16:43 +0000559static struct platform_driver dw_mci_exynos_pltfm_driver = {
560 .probe = dw_mci_exynos_probe,
Joonyoung Shim9b93d3922016-11-23 18:36:02 +0900561 .remove = dw_mci_exynos_remove,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000562 .driver = {
563 .name = "dwmmc_exynos",
Sachin Kamat20183d52013-02-18 14:23:09 +0530564 .of_match_table = dw_mci_exynos_match,
Doug Andersone2c63592013-08-31 00:11:21 +0900565 .pm = &dw_mci_exynos_pmops,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000566 },
567};
568
569module_platform_driver(dw_mci_exynos_pltfm_driver);
570
571MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
572MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
573MODULE_LICENSE("GPL v2");
Zhangfei Gao2fc546f2015-05-14 16:59:45 +0800574MODULE_ALIAS("platform:dwmmc_exynos");