blob: 0c75810812a0a88efb77ea2c81c42a85529c9926 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Thomas Abrahamc3665002012-09-17 18:16:43 +00002/*
3 * Exynos Specific Extensions for Synopsys DW Multimedia Card Interface driver
4 *
5 * Copyright (C) 2012, Samsung Electronics Co., Ltd.
Thomas Abrahamc3665002012-09-17 18:16:43 +00006 */
7
8#include <linux/module.h>
9#include <linux/platform_device.h>
10#include <linux/clk.h>
11#include <linux/mmc/host.h>
Seungwon Jeonc537a1c2013-08-31 00:12:50 +090012#include <linux/mmc/mmc.h>
Thomas Abrahamc3665002012-09-17 18:16:43 +000013#include <linux/of.h>
14#include <linux/of_gpio.h>
Shawn Lincf5237e2016-10-12 10:55:55 +080015#include <linux/pm_runtime.h>
Seungwon Jeonc537a1c2013-08-31 00:12:50 +090016#include <linux/slab.h>
Thomas Abrahamc3665002012-09-17 18:16:43 +000017
18#include "dw_mmc.h"
19#include "dw_mmc-pltfm.h"
Seungwon Jeon0b5fce42014-12-22 17:42:04 +053020#include "dw_mmc-exynos.h"
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +090021
Thomas Abrahamc3665002012-09-17 18:16:43 +000022/* Variations in Exynos specific dw-mshc controller */
23enum dw_mci_exynos_type {
24 DW_MCI_TYPE_EXYNOS4210,
25 DW_MCI_TYPE_EXYNOS4412,
26 DW_MCI_TYPE_EXYNOS5250,
Yuvaraj Kumar C D00fd0412013-05-24 15:34:32 +053027 DW_MCI_TYPE_EXYNOS5420,
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +090028 DW_MCI_TYPE_EXYNOS5420_SMU,
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +053029 DW_MCI_TYPE_EXYNOS7,
30 DW_MCI_TYPE_EXYNOS7_SMU,
Thomas Abrahamc3665002012-09-17 18:16:43 +000031};
32
33/* Exynos implementation specific driver private data */
34struct dw_mci_exynos_priv_data {
35 enum dw_mci_exynos_type ctrl_type;
36 u8 ciu_div;
37 u32 sdr_timing;
38 u32 ddr_timing;
Seungwon Jeon80113132015-01-29 08:11:57 +053039 u32 hs400_timing;
40 u32 tuned_sample;
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +090041 u32 cur_speed;
Seungwon Jeon80113132015-01-29 08:11:57 +053042 u32 dqs_delay;
43 u32 saved_dqs_en;
44 u32 saved_strobe_ctrl;
Thomas Abrahamc3665002012-09-17 18:16:43 +000045};
46
47static struct dw_mci_exynos_compatible {
48 char *compatible;
49 enum dw_mci_exynos_type ctrl_type;
50} exynos_compat[] = {
51 {
52 .compatible = "samsung,exynos4210-dw-mshc",
53 .ctrl_type = DW_MCI_TYPE_EXYNOS4210,
54 }, {
55 .compatible = "samsung,exynos4412-dw-mshc",
56 .ctrl_type = DW_MCI_TYPE_EXYNOS4412,
57 }, {
58 .compatible = "samsung,exynos5250-dw-mshc",
59 .ctrl_type = DW_MCI_TYPE_EXYNOS5250,
Yuvaraj Kumar C D00fd0412013-05-24 15:34:32 +053060 }, {
61 .compatible = "samsung,exynos5420-dw-mshc",
62 .ctrl_type = DW_MCI_TYPE_EXYNOS5420,
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +090063 }, {
64 .compatible = "samsung,exynos5420-dw-mshc-smu",
65 .ctrl_type = DW_MCI_TYPE_EXYNOS5420_SMU,
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +053066 }, {
67 .compatible = "samsung,exynos7-dw-mshc",
68 .ctrl_type = DW_MCI_TYPE_EXYNOS7,
69 }, {
70 .compatible = "samsung,exynos7-dw-mshc-smu",
71 .ctrl_type = DW_MCI_TYPE_EXYNOS7_SMU,
Thomas Abrahamc3665002012-09-17 18:16:43 +000072 },
73};
74
Seungwon Jeon80113132015-01-29 08:11:57 +053075static inline u8 dw_mci_exynos_get_ciu_div(struct dw_mci *host)
76{
77 struct dw_mci_exynos_priv_data *priv = host->priv;
78
79 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
80 return EXYNOS4412_FIXED_CIU_CLK_DIV;
81 else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
82 return EXYNOS4210_FIXED_CIU_CLK_DIV;
83 else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
84 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
85 return SDMMC_CLKSEL_GET_DIV(mci_readl(host, CLKSEL64)) + 1;
86 else
87 return SDMMC_CLKSEL_GET_DIV(mci_readl(host, CLKSEL)) + 1;
88}
89
Jaehoon Chung5659eea2016-03-31 14:53:18 +090090static void dw_mci_exynos_config_smu(struct dw_mci *host)
Thomas Abrahamc3665002012-09-17 18:16:43 +000091{
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +090092 struct dw_mci_exynos_priv_data *priv = host->priv;
Thomas Abrahamc3665002012-09-17 18:16:43 +000093
Jaehoon Chung5659eea2016-03-31 14:53:18 +090094 /*
95 * If Exynos is provided the Security management,
96 * set for non-ecryption mode at this time.
97 */
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +053098 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU ||
99 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU) {
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +0900100 mci_writel(host, MPSBEGIN0, 0);
Seungwon Jeon0b5fce42014-12-22 17:42:04 +0530101 mci_writel(host, MPSEND0, SDMMC_ENDING_SEC_NR_MAX);
102 mci_writel(host, MPSCTRL0, SDMMC_MPSCTRL_SECURE_WRITE_BIT |
103 SDMMC_MPSCTRL_NON_SECURE_READ_BIT |
104 SDMMC_MPSCTRL_VALID |
105 SDMMC_MPSCTRL_NON_SECURE_WRITE_BIT);
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +0900106 }
Jaehoon Chung5659eea2016-03-31 14:53:18 +0900107}
108
109static int dw_mci_exynos_priv_init(struct dw_mci *host)
110{
111 struct dw_mci_exynos_priv_data *priv = host->priv;
112
113 dw_mci_exynos_config_smu(host);
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +0900114
Seungwon Jeon80113132015-01-29 08:11:57 +0530115 if (priv->ctrl_type >= DW_MCI_TYPE_EXYNOS5420) {
116 priv->saved_strobe_ctrl = mci_readl(host, HS400_DLINE_CTRL);
117 priv->saved_dqs_en = mci_readl(host, HS400_DQS_EN);
118 priv->saved_dqs_en |= AXI_NON_BLOCKING_WR;
119 mci_writel(host, HS400_DQS_EN, priv->saved_dqs_en);
120 if (!priv->dqs_delay)
121 priv->dqs_delay =
122 DQS_CTRL_GET_RD_DELAY(priv->saved_strobe_ctrl);
123 }
124
Seungwon Jeona2a1fed2014-12-22 17:42:03 +0530125 host->bus_hz /= (priv->ciu_div + 1);
126
Thomas Abrahamc3665002012-09-17 18:16:43 +0000127 return 0;
128}
129
Seungwon Jeon80113132015-01-29 08:11:57 +0530130static void dw_mci_exynos_set_clksel_timing(struct dw_mci *host, u32 timing)
131{
132 struct dw_mci_exynos_priv_data *priv = host->priv;
133 u32 clksel;
134
135 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
136 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
137 clksel = mci_readl(host, CLKSEL64);
138 else
139 clksel = mci_readl(host, CLKSEL);
140
141 clksel = (clksel & ~SDMMC_CLKSEL_TIMING_MASK) | timing;
142
143 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
144 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
145 mci_writel(host, CLKSEL64, clksel);
146 else
147 mci_writel(host, CLKSEL, clksel);
Jaehoon Chungaaaaeb72016-01-21 11:01:06 +0900148
149 /*
150 * Exynos4412 and Exynos5250 extends the use of CMD register with the
151 * use of bit 29 (which is reserved on standard MSHC controllers) for
152 * optionally bypassing the HOLD register for command and data. The
153 * HOLD register should be bypassed in case there is no phase shift
154 * applied on CMD/DATA that is sent to the card.
155 */
Jaehoon Chung42f989c2017-06-05 13:41:34 +0900156 if (!SDMMC_CLKSEL_GET_DRV_WD3(clksel) && host->slot)
157 set_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags);
Seungwon Jeon80113132015-01-29 08:11:57 +0530158}
159
Shawn Lincf5237e2016-10-12 10:55:55 +0800160#ifdef CONFIG_PM
161static int dw_mci_exynos_runtime_resume(struct device *dev)
Doug Andersone2c63592013-08-31 00:11:21 +0900162{
163 struct dw_mci *host = dev_get_drvdata(dev);
Jaehoon Chunge22842d2018-03-09 15:10:21 +0900164 int ret;
165
166 ret = dw_mci_runtime_resume(dev);
167 if (ret)
168 return ret;
Doug Andersone2c63592013-08-31 00:11:21 +0900169
Jaehoon Chung5659eea2016-03-31 14:53:18 +0900170 dw_mci_exynos_config_smu(host);
Jaehoon Chunge22842d2018-03-09 15:10:21 +0900171
172 return ret;
Doug Andersone2c63592013-08-31 00:11:21 +0900173}
Marek Szyprowskiecf7c7c2018-06-12 12:55:23 +0200174#endif /* CONFIG_PM */
175
176#ifdef CONFIG_PM_SLEEP
177/**
178 * dw_mci_exynos_suspend_noirq - Exynos-specific suspend code
Lee Jones306c59c2020-07-01 13:46:55 +0100179 * @dev: Device to suspend (this device)
Marek Szyprowskiecf7c7c2018-06-12 12:55:23 +0200180 *
181 * This ensures that device will be in runtime active state in
182 * dw_mci_exynos_resume_noirq after calling pm_runtime_force_resume()
183 */
184static int dw_mci_exynos_suspend_noirq(struct device *dev)
185{
186 pm_runtime_get_noresume(dev);
187 return pm_runtime_force_suspend(dev);
188}
Doug Andersone2c63592013-08-31 00:11:21 +0900189
190/**
191 * dw_mci_exynos_resume_noirq - Exynos-specific resume code
Lee Jones306c59c2020-07-01 13:46:55 +0100192 * @dev: Device to resume (this device)
Doug Andersone2c63592013-08-31 00:11:21 +0900193 *
194 * On exynos5420 there is a silicon errata that will sometimes leave the
195 * WAKEUP_INT bit in the CLKSEL register asserted. This bit is 1 to indicate
196 * that it fired and we can clear it by writing a 1 back. Clear it to prevent
197 * interrupts from going off constantly.
198 *
199 * We run this code on all exynos variants because it doesn't hurt.
200 */
Doug Andersone2c63592013-08-31 00:11:21 +0900201static int dw_mci_exynos_resume_noirq(struct device *dev)
202{
203 struct dw_mci *host = dev_get_drvdata(dev);
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530204 struct dw_mci_exynos_priv_data *priv = host->priv;
Doug Andersone2c63592013-08-31 00:11:21 +0900205 u32 clksel;
Marek Szyprowskiecf7c7c2018-06-12 12:55:23 +0200206 int ret;
207
208 ret = pm_runtime_force_resume(dev);
209 if (ret)
210 return ret;
Doug Andersone2c63592013-08-31 00:11:21 +0900211
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530212 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
213 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
214 clksel = mci_readl(host, CLKSEL64);
215 else
216 clksel = mci_readl(host, CLKSEL);
217
218 if (clksel & SDMMC_CLKSEL_WAKEUP_INT) {
219 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
220 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
221 mci_writel(host, CLKSEL64, clksel);
222 else
223 mci_writel(host, CLKSEL, clksel);
224 }
Doug Andersone2c63592013-08-31 00:11:21 +0900225
Marek Szyprowskiecf7c7c2018-06-12 12:55:23 +0200226 pm_runtime_put(dev);
227
Doug Andersone2c63592013-08-31 00:11:21 +0900228 return 0;
229}
Marek Szyprowskiecf7c7c2018-06-12 12:55:23 +0200230#endif /* CONFIG_PM_SLEEP */
Doug Andersone2c63592013-08-31 00:11:21 +0900231
Seungwon Jeon80113132015-01-29 08:11:57 +0530232static void dw_mci_exynos_config_hs400(struct dw_mci *host, u32 timing)
Thomas Abrahamc3665002012-09-17 18:16:43 +0000233{
234 struct dw_mci_exynos_priv_data *priv = host->priv;
Seungwon Jeon80113132015-01-29 08:11:57 +0530235 u32 dqs, strobe;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000236
Seungwon Jeon80113132015-01-29 08:11:57 +0530237 /*
238 * Not supported to configure register
239 * related to HS400
240 */
Krzysztof Kozlowski941a6592016-07-14 15:22:27 +0200241 if (priv->ctrl_type < DW_MCI_TYPE_EXYNOS5420) {
242 if (timing == MMC_TIMING_MMC_HS400)
243 dev_warn(host->dev,
244 "cannot configure HS400, unsupported chipset\n");
Seungwon Jeon80113132015-01-29 08:11:57 +0530245 return;
Krzysztof Kozlowski941a6592016-07-14 15:22:27 +0200246 }
Seungwon Jeon80113132015-01-29 08:11:57 +0530247
248 dqs = priv->saved_dqs_en;
249 strobe = priv->saved_strobe_ctrl;
250
251 if (timing == MMC_TIMING_MMC_HS400) {
252 dqs |= DATA_STROBE_EN;
253 strobe = DQS_CTRL_RD_DELAY(strobe, priv->dqs_delay);
Anand Moon32b64b02018-09-27 14:07:38 +0000254 } else if (timing == MMC_TIMING_UHS_SDR104) {
255 dqs &= 0xffffff00;
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900256 } else {
Seungwon Jeon80113132015-01-29 08:11:57 +0530257 dqs &= ~DATA_STROBE_EN;
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900258 }
259
Seungwon Jeon80113132015-01-29 08:11:57 +0530260 mci_writel(host, HS400_DQS_EN, dqs);
261 mci_writel(host, HS400_DLINE_CTRL, strobe);
262}
263
264static void dw_mci_exynos_adjust_clock(struct dw_mci *host, unsigned int wanted)
265{
266 struct dw_mci_exynos_priv_data *priv = host->priv;
267 unsigned long actual;
268 u8 div;
269 int ret;
Seungwon Jeona2a1fed2014-12-22 17:42:03 +0530270 /*
271 * Don't care if wanted clock is zero or
272 * ciu clock is unavailable
273 */
274 if (!wanted || IS_ERR(host->ciu_clk))
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900275 return;
276
277 /* Guaranteed minimum frequency for cclkin */
278 if (wanted < EXYNOS_CCLKIN_MIN)
279 wanted = EXYNOS_CCLKIN_MIN;
280
Seungwon Jeon80113132015-01-29 08:11:57 +0530281 if (wanted == priv->cur_speed)
282 return;
283
284 div = dw_mci_exynos_get_ciu_div(host);
285 ret = clk_set_rate(host->ciu_clk, wanted * div);
286 if (ret)
287 dev_warn(host->dev,
288 "failed to set clk-rate %u error: %d\n",
289 wanted * div, ret);
290 actual = clk_get_rate(host->ciu_clk);
291 host->bus_hz = actual / div;
292 priv->cur_speed = wanted;
293 host->current_speed = 0;
294}
295
296static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
297{
298 struct dw_mci_exynos_priv_data *priv = host->priv;
299 unsigned int wanted = ios->clock;
300 u32 timing = ios->timing, clksel;
301
302 switch (timing) {
303 case MMC_TIMING_MMC_HS400:
304 /* Update tuned sample timing */
305 clksel = SDMMC_CLKSEL_UP_SAMPLE(
306 priv->hs400_timing, priv->tuned_sample);
307 wanted <<= 1;
308 break;
309 case MMC_TIMING_MMC_DDR52:
310 clksel = priv->ddr_timing;
311 /* Should be double rate for DDR mode */
312 if (ios->bus_width == MMC_BUS_WIDTH_8)
313 wanted <<= 1;
314 break;
Anand Moon32b64b02018-09-27 14:07:38 +0000315 case MMC_TIMING_UHS_SDR104:
316 case MMC_TIMING_UHS_SDR50:
317 clksel = (priv->sdr_timing & 0xfff8ffff) |
318 (priv->ciu_div << 16);
319 break;
320 case MMC_TIMING_UHS_DDR50:
321 clksel = (priv->ddr_timing & 0xfff8ffff) |
322 (priv->ciu_div << 16);
323 break;
Seungwon Jeon80113132015-01-29 08:11:57 +0530324 default:
325 clksel = priv->sdr_timing;
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900326 }
Seungwon Jeon80113132015-01-29 08:11:57 +0530327
328 /* Set clock timing for the requested speed mode*/
329 dw_mci_exynos_set_clksel_timing(host, clksel);
330
331 /* Configure setting for HS400 */
332 dw_mci_exynos_config_hs400(host, timing);
333
334 /* Configure clock rate */
335 dw_mci_exynos_adjust_clock(host, wanted);
Thomas Abrahamc3665002012-09-17 18:16:43 +0000336}
337
338static int dw_mci_exynos_parse_dt(struct dw_mci *host)
339{
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900340 struct dw_mci_exynos_priv_data *priv;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000341 struct device_node *np = host->dev->of_node;
342 u32 timing[2];
343 u32 div = 0;
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900344 int idx;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000345 int ret;
346
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900347 priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
Beomho Seobf3707e2014-12-23 21:07:33 +0900348 if (!priv)
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900349 return -ENOMEM;
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900350
351 for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
352 if (of_device_is_compatible(np, exynos_compat[idx].compatible))
353 priv->ctrl_type = exynos_compat[idx].ctrl_type;
354 }
355
Seungwon Jeonc6d9ded2013-08-31 00:13:03 +0900356 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
357 priv->ciu_div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1;
358 else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
359 priv->ciu_div = EXYNOS4210_FIXED_CIU_CLK_DIV - 1;
360 else {
361 of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
362 priv->ciu_div = div;
363 }
Thomas Abrahamc3665002012-09-17 18:16:43 +0000364
365 ret = of_property_read_u32_array(np,
366 "samsung,dw-mshc-sdr-timing", timing, 2);
367 if (ret)
368 return ret;
369
Yuvaraj Kumar C D2d9f0bd2013-10-22 14:41:56 +0530370 priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
371
Thomas Abrahamc3665002012-09-17 18:16:43 +0000372 ret = of_property_read_u32_array(np,
373 "samsung,dw-mshc-ddr-timing", timing, 2);
374 if (ret)
375 return ret;
376
377 priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
Seungwon Jeon80113132015-01-29 08:11:57 +0530378
379 ret = of_property_read_u32_array(np,
380 "samsung,dw-mshc-hs400-timing", timing, 2);
381 if (!ret && of_property_read_u32(np,
382 "samsung,read-strobe-delay", &priv->dqs_delay))
383 dev_dbg(host->dev,
384 "read-strobe-delay is not found, assuming usage of default value\n");
385
386 priv->hs400_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1],
387 HS400_FIXED_CIU_CLK_DIV);
Yuvaraj Kumar C De6c784e2013-08-31 00:11:57 +0900388 host->priv = priv;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000389 return 0;
390}
391
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900392static inline u8 dw_mci_exynos_get_clksmpl(struct dw_mci *host)
393{
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530394 struct dw_mci_exynos_priv_data *priv = host->priv;
395
396 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
397 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
398 return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL64));
399 else
400 return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL));
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900401}
402
403static inline void dw_mci_exynos_set_clksmpl(struct dw_mci *host, u8 sample)
404{
405 u32 clksel;
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530406 struct dw_mci_exynos_priv_data *priv = host->priv;
407
408 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
409 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
410 clksel = mci_readl(host, CLKSEL64);
411 else
412 clksel = mci_readl(host, CLKSEL);
Seungwon Jeon80113132015-01-29 08:11:57 +0530413 clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530414 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
415 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
416 mci_writel(host, CLKSEL64, clksel);
417 else
418 mci_writel(host, CLKSEL, clksel);
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900419}
420
421static inline u8 dw_mci_exynos_move_next_clksmpl(struct dw_mci *host)
422{
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530423 struct dw_mci_exynos_priv_data *priv = host->priv;
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900424 u32 clksel;
425 u8 sample;
426
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530427 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
428 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
429 clksel = mci_readl(host, CLKSEL64);
430 else
431 clksel = mci_readl(host, CLKSEL);
Seungwon Jeon80113132015-01-29 08:11:57 +0530432
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900433 sample = (clksel + 1) & 0x7;
Seungwon Jeon80113132015-01-29 08:11:57 +0530434 clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
435
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530436 if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
437 priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
438 mci_writel(host, CLKSEL64, clksel);
439 else
440 mci_writel(host, CLKSEL, clksel);
Seungwon Jeon80113132015-01-29 08:11:57 +0530441
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900442 return sample;
443}
444
445static s8 dw_mci_exynos_get_best_clksmpl(u8 candiates)
446{
447 const u8 iter = 8;
448 u8 __c;
449 s8 i, loc = -1;
450
451 for (i = 0; i < iter; i++) {
452 __c = ror8(candiates, i);
453 if ((__c & 0xc7) == 0xc7) {
454 loc = i;
455 goto out;
456 }
457 }
458
459 for (i = 0; i < iter; i++) {
460 __c = ror8(candiates, i);
461 if ((__c & 0x83) == 0x83) {
462 loc = i;
463 goto out;
464 }
465 }
466
467out:
468 return loc;
469}
470
Chaotian Jing9979dbe2015-10-27 14:24:28 +0800471static int dw_mci_exynos_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900472{
473 struct dw_mci *host = slot->host;
Seungwon Jeon80113132015-01-29 08:11:57 +0530474 struct dw_mci_exynos_priv_data *priv = host->priv;
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900475 struct mmc_host *mmc = slot->mmc;
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900476 u8 start_smpl, smpl, candiates = 0;
Colin Ian King479cb7cf2020-07-06 17:30:31 +0100477 s8 found;
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900478 int ret = 0;
479
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900480 start_smpl = dw_mci_exynos_get_clksmpl(host);
481
482 do {
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900483 mci_writel(host, TMOUT, ~0);
484 smpl = dw_mci_exynos_move_next_clksmpl(host);
485
Chaotian Jing9979dbe2015-10-27 14:24:28 +0800486 if (!mmc_send_tuning(mmc, opcode, NULL))
Ulf Hansson6c2c6502014-12-01 16:13:39 +0100487 candiates |= (1 << smpl);
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900488
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900489 } while (start_smpl != smpl);
490
491 found = dw_mci_exynos_get_best_clksmpl(candiates);
Seungwon Jeon80113132015-01-29 08:11:57 +0530492 if (found >= 0) {
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900493 dw_mci_exynos_set_clksmpl(host, found);
Seungwon Jeon80113132015-01-29 08:11:57 +0530494 priv->tuned_sample = found;
495 } else {
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900496 ret = -EIO;
Seungwon Jeon80113132015-01-29 08:11:57 +0530497 }
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900498
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900499 return ret;
500}
501
Wu Fengguangc22f5e12015-03-05 18:02:54 +0800502static int dw_mci_exynos_prepare_hs400_tuning(struct dw_mci *host,
Seungwon Jeon80113132015-01-29 08:11:57 +0530503 struct mmc_ios *ios)
504{
505 struct dw_mci_exynos_priv_data *priv = host->priv;
506
507 dw_mci_exynos_set_clksel_timing(host, priv->hs400_timing);
508 dw_mci_exynos_adjust_clock(host, (ios->clock) << 1);
509
510 return 0;
511}
512
Dongjin Kim0f6e73d2013-02-23 00:17:45 +0900513/* Common capabilities of Exynos4/Exynos5 SoC */
514static unsigned long exynos_dwmmc_caps[4] = {
Seungwon Jeoncab3a802014-03-14 21:12:43 +0900515 MMC_CAP_1_8V_DDR | MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000516 MMC_CAP_CMD23,
517 MMC_CAP_CMD23,
518 MMC_CAP_CMD23,
519};
520
Dongjin Kim0f6e73d2013-02-23 00:17:45 +0900521static const struct dw_mci_drv_data exynos_drv_data = {
522 .caps = exynos_dwmmc_caps,
Shawn Lin0d84b9e2018-02-24 14:17:23 +0800523 .num_caps = ARRAY_SIZE(exynos_dwmmc_caps),
Thomas Abrahamc3665002012-09-17 18:16:43 +0000524 .init = dw_mci_exynos_priv_init,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000525 .set_ios = dw_mci_exynos_set_ios,
526 .parse_dt = dw_mci_exynos_parse_dt,
Seungwon Jeonc537a1c2013-08-31 00:12:50 +0900527 .execute_tuning = dw_mci_exynos_execute_tuning,
Seungwon Jeon80113132015-01-29 08:11:57 +0530528 .prepare_hs400_tuning = dw_mci_exynos_prepare_hs400_tuning,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000529};
530
531static const struct of_device_id dw_mci_exynos_match[] = {
Dongjin Kim0f6e73d2013-02-23 00:17:45 +0900532 { .compatible = "samsung,exynos4412-dw-mshc",
533 .data = &exynos_drv_data, },
Thomas Abrahamc3665002012-09-17 18:16:43 +0000534 { .compatible = "samsung,exynos5250-dw-mshc",
Dongjin Kim0f6e73d2013-02-23 00:17:45 +0900535 .data = &exynos_drv_data, },
Yuvaraj Kumar C D00fd0412013-05-24 15:34:32 +0530536 { .compatible = "samsung,exynos5420-dw-mshc",
537 .data = &exynos_drv_data, },
Yuvaraj Kumar C D6bce4312013-08-31 00:12:35 +0900538 { .compatible = "samsung,exynos5420-dw-mshc-smu",
539 .data = &exynos_drv_data, },
Abhilash Kesavan89ad2be2014-08-28 18:48:53 +0530540 { .compatible = "samsung,exynos7-dw-mshc",
541 .data = &exynos_drv_data, },
542 { .compatible = "samsung,exynos7-dw-mshc-smu",
543 .data = &exynos_drv_data, },
Thomas Abrahamc3665002012-09-17 18:16:43 +0000544 {},
545};
Arnd Bergmann517cb9f2012-11-06 22:55:30 +0100546MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
Thomas Abrahamc3665002012-09-17 18:16:43 +0000547
Sachin Kamat9665f7f2013-02-18 14:23:08 +0530548static int dw_mci_exynos_probe(struct platform_device *pdev)
Thomas Abrahamc3665002012-09-17 18:16:43 +0000549{
Arnd Bergmann8e2b36e2012-11-06 22:55:31 +0100550 const struct dw_mci_drv_data *drv_data;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000551 const struct of_device_id *match;
Joonyoung Shim9b93d3922016-11-23 18:36:02 +0900552 int ret;
Thomas Abrahamc3665002012-09-17 18:16:43 +0000553
554 match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
555 drv_data = match->data;
Joonyoung Shim9b93d3922016-11-23 18:36:02 +0900556
557 pm_runtime_get_noresume(&pdev->dev);
558 pm_runtime_set_active(&pdev->dev);
559 pm_runtime_enable(&pdev->dev);
560
561 ret = dw_mci_pltfm_register(pdev, drv_data);
562 if (ret) {
563 pm_runtime_disable(&pdev->dev);
564 pm_runtime_set_suspended(&pdev->dev);
565 pm_runtime_put_noidle(&pdev->dev);
566
567 return ret;
568 }
569
570 return 0;
571}
572
573static int dw_mci_exynos_remove(struct platform_device *pdev)
574{
575 pm_runtime_disable(&pdev->dev);
576 pm_runtime_set_suspended(&pdev->dev);
577 pm_runtime_put_noidle(&pdev->dev);
578
579 return dw_mci_pltfm_remove(pdev);
Thomas Abrahamc3665002012-09-17 18:16:43 +0000580}
581
Sachin Kamat15a2e2a2014-03-04 10:33:25 +0530582static const struct dev_pm_ops dw_mci_exynos_pmops = {
Marek Szyprowskiecf7c7c2018-06-12 12:55:23 +0200583 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dw_mci_exynos_suspend_noirq,
584 dw_mci_exynos_resume_noirq)
Shawn Lincf5237e2016-10-12 10:55:55 +0800585 SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
586 dw_mci_exynos_runtime_resume,
587 NULL)
Doug Andersone2c63592013-08-31 00:11:21 +0900588};
589
Thomas Abrahamc3665002012-09-17 18:16:43 +0000590static struct platform_driver dw_mci_exynos_pltfm_driver = {
591 .probe = dw_mci_exynos_probe,
Joonyoung Shim9b93d3922016-11-23 18:36:02 +0900592 .remove = dw_mci_exynos_remove,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000593 .driver = {
594 .name = "dwmmc_exynos",
Douglas Anderson21b2cec2020-09-03 16:24:36 -0700595 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
Sachin Kamat20183d52013-02-18 14:23:09 +0530596 .of_match_table = dw_mci_exynos_match,
Doug Andersone2c63592013-08-31 00:11:21 +0900597 .pm = &dw_mci_exynos_pmops,
Thomas Abrahamc3665002012-09-17 18:16:43 +0000598 },
599};
600
601module_platform_driver(dw_mci_exynos_pltfm_driver);
602
603MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
604MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
605MODULE_LICENSE("GPL v2");
Zhangfei Gao2fc546f2015-05-14 16:59:45 +0800606MODULE_ALIAS("platform:dwmmc_exynos");