xref: /openbmc/linux/drivers/mmc/host/dw_mmc-exynos.c (revision 15a2e2ab62f1f1d8b43ae43670de7bc2b21f7f86)
1c3665006SThomas Abraham /*
2c3665006SThomas Abraham  * Exynos Specific Extensions for Synopsys DW Multimedia Card Interface driver
3c3665006SThomas Abraham  *
4c3665006SThomas Abraham  * Copyright (C) 2012, Samsung Electronics Co., Ltd.
5c3665006SThomas Abraham  *
6c3665006SThomas Abraham  * This program is free software; you can redistribute it and/or modify
7c3665006SThomas Abraham  * it under the terms of the GNU General Public License as published by
8c3665006SThomas Abraham  * the Free Software Foundation; either version 2 of the License, or
9c3665006SThomas Abraham  * (at your option) any later version.
10c3665006SThomas Abraham  */
11c3665006SThomas Abraham 
12c3665006SThomas Abraham #include <linux/module.h>
13c3665006SThomas Abraham #include <linux/platform_device.h>
14c3665006SThomas Abraham #include <linux/clk.h>
15c3665006SThomas Abraham #include <linux/mmc/host.h>
16c3665006SThomas Abraham #include <linux/mmc/dw_mmc.h>
17c537a1c5SSeungwon Jeon #include <linux/mmc/mmc.h>
18c3665006SThomas Abraham #include <linux/of.h>
19c3665006SThomas Abraham #include <linux/of_gpio.h>
20c537a1c5SSeungwon Jeon #include <linux/slab.h>
21c3665006SThomas Abraham 
22c3665006SThomas Abraham #include "dw_mmc.h"
23c3665006SThomas Abraham #include "dw_mmc-pltfm.h"
24c3665006SThomas Abraham 
25c3665006SThomas Abraham #define NUM_PINS(x)			(x + 2)
26c3665006SThomas Abraham 
27c3665006SThomas Abraham #define SDMMC_CLKSEL			0x09C
28c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_SAMPLE(x)	(((x) & 7) << 0)
29c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_DRIVE(x)	(((x) & 7) << 16)
30c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_DIVIDER(x)	(((x) & 7) << 24)
31c3665006SThomas Abraham #define SDMMC_CLKSEL_GET_DRV_WD3(x)	(((x) >> 16) & 0x7)
32c3665006SThomas Abraham #define SDMMC_CLKSEL_TIMING(x, y, z)	(SDMMC_CLKSEL_CCLK_SAMPLE(x) |	\
33c3665006SThomas Abraham 					SDMMC_CLKSEL_CCLK_DRIVE(y) |	\
34c3665006SThomas Abraham 					SDMMC_CLKSEL_CCLK_DIVIDER(z))
35e2c63599SDoug Anderson #define SDMMC_CLKSEL_WAKEUP_INT		BIT(11)
36c3665006SThomas Abraham 
37c3665006SThomas Abraham #define EXYNOS4210_FIXED_CIU_CLK_DIV	2
38c3665006SThomas Abraham #define EXYNOS4412_FIXED_CIU_CLK_DIV	4
39c3665006SThomas Abraham 
406bce431cSYuvaraj Kumar C D /* Block number in eMMC */
416bce431cSYuvaraj Kumar C D #define DWMCI_BLOCK_NUM		0xFFFFFFFF
426bce431cSYuvaraj Kumar C D 
436bce431cSYuvaraj Kumar C D #define SDMMC_EMMCP_BASE	0x1000
446bce431cSYuvaraj Kumar C D #define SDMMC_MPSECURITY	(SDMMC_EMMCP_BASE + 0x0010)
456bce431cSYuvaraj Kumar C D #define SDMMC_MPSBEGIN0		(SDMMC_EMMCP_BASE + 0x0200)
466bce431cSYuvaraj Kumar C D #define SDMMC_MPSEND0		(SDMMC_EMMCP_BASE + 0x0204)
476bce431cSYuvaraj Kumar C D #define SDMMC_MPSCTRL0		(SDMMC_EMMCP_BASE + 0x020C)
486bce431cSYuvaraj Kumar C D 
496bce431cSYuvaraj Kumar C D /* SMU control bits */
506bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_SECURE_READ_BIT		BIT(7)
516bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_SECURE_WRITE_BIT		BIT(6)
526bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_NON_SECURE_READ_BIT	BIT(5)
536bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_NON_SECURE_WRITE_BIT	BIT(4)
546bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_USE_FUSE_KEY		BIT(3)
556bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_ECB_MODE			BIT(2)
566bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_ENCRYPTION		BIT(1)
576bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_VALID			BIT(0)
586bce431cSYuvaraj Kumar C D 
59c6d9dedaSSeungwon Jeon #define EXYNOS_CCLKIN_MIN	50000000	/* unit: HZ */
60c6d9dedaSSeungwon Jeon 
61c3665006SThomas Abraham /* Variations in Exynos specific dw-mshc controller */
62c3665006SThomas Abraham enum dw_mci_exynos_type {
63c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4210,
64c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4412,
65c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS5250,
6600fd041bSYuvaraj Kumar C D 	DW_MCI_TYPE_EXYNOS5420,
676bce431cSYuvaraj Kumar C D 	DW_MCI_TYPE_EXYNOS5420_SMU,
68c3665006SThomas Abraham };
69c3665006SThomas Abraham 
70c3665006SThomas Abraham /* Exynos implementation specific driver private data */
71c3665006SThomas Abraham struct dw_mci_exynos_priv_data {
72c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
73c3665006SThomas Abraham 	u8				ciu_div;
74c3665006SThomas Abraham 	u32				sdr_timing;
75c3665006SThomas Abraham 	u32				ddr_timing;
76c6d9dedaSSeungwon Jeon 	u32				cur_speed;
77c3665006SThomas Abraham };
78c3665006SThomas Abraham 
79c3665006SThomas Abraham static struct dw_mci_exynos_compatible {
80c3665006SThomas Abraham 	char				*compatible;
81c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
82c3665006SThomas Abraham } exynos_compat[] = {
83c3665006SThomas Abraham 	{
84c3665006SThomas Abraham 		.compatible	= "samsung,exynos4210-dw-mshc",
85c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4210,
86c3665006SThomas Abraham 	}, {
87c3665006SThomas Abraham 		.compatible	= "samsung,exynos4412-dw-mshc",
88c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4412,
89c3665006SThomas Abraham 	}, {
90c3665006SThomas Abraham 		.compatible	= "samsung,exynos5250-dw-mshc",
91c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5250,
9200fd041bSYuvaraj Kumar C D 	}, {
9300fd041bSYuvaraj Kumar C D 		.compatible	= "samsung,exynos5420-dw-mshc",
9400fd041bSYuvaraj Kumar C D 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5420,
956bce431cSYuvaraj Kumar C D 	}, {
966bce431cSYuvaraj Kumar C D 		.compatible	= "samsung,exynos5420-dw-mshc-smu",
976bce431cSYuvaraj Kumar C D 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5420_SMU,
98c3665006SThomas Abraham 	},
99c3665006SThomas Abraham };
100c3665006SThomas Abraham 
101c3665006SThomas Abraham static int dw_mci_exynos_priv_init(struct dw_mci *host)
102c3665006SThomas Abraham {
103e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv = host->priv;
104c3665006SThomas Abraham 
1056bce431cSYuvaraj Kumar C D 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU) {
1066bce431cSYuvaraj Kumar C D 		mci_writel(host, MPSBEGIN0, 0);
1076bce431cSYuvaraj Kumar C D 		mci_writel(host, MPSEND0, DWMCI_BLOCK_NUM);
1086bce431cSYuvaraj Kumar C D 		mci_writel(host, MPSCTRL0, DWMCI_MPSCTRL_SECURE_WRITE_BIT |
1096bce431cSYuvaraj Kumar C D 			   DWMCI_MPSCTRL_NON_SECURE_READ_BIT |
1106bce431cSYuvaraj Kumar C D 			   DWMCI_MPSCTRL_VALID |
1116bce431cSYuvaraj Kumar C D 			   DWMCI_MPSCTRL_NON_SECURE_WRITE_BIT);
1126bce431cSYuvaraj Kumar C D 	}
1136bce431cSYuvaraj Kumar C D 
114c3665006SThomas Abraham 	return 0;
115c3665006SThomas Abraham }
116c3665006SThomas Abraham 
117c3665006SThomas Abraham static int dw_mci_exynos_setup_clock(struct dw_mci *host)
118c3665006SThomas Abraham {
119c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
120c6d9dedaSSeungwon Jeon 	unsigned long rate = clk_get_rate(host->ciu_clk);
121c3665006SThomas Abraham 
122c6d9dedaSSeungwon Jeon 	host->bus_hz = rate / (priv->ciu_div + 1);
123c3665006SThomas Abraham 	return 0;
124c3665006SThomas Abraham }
125c3665006SThomas Abraham 
126e2c63599SDoug Anderson #ifdef CONFIG_PM_SLEEP
127e2c63599SDoug Anderson static int dw_mci_exynos_suspend(struct device *dev)
128e2c63599SDoug Anderson {
129e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
130e2c63599SDoug Anderson 
131e2c63599SDoug Anderson 	return dw_mci_suspend(host);
132e2c63599SDoug Anderson }
133e2c63599SDoug Anderson 
134e2c63599SDoug Anderson static int dw_mci_exynos_resume(struct device *dev)
135e2c63599SDoug Anderson {
136e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
137e2c63599SDoug Anderson 
1386bce431cSYuvaraj Kumar C D 	dw_mci_exynos_priv_init(host);
139e2c63599SDoug Anderson 	return dw_mci_resume(host);
140e2c63599SDoug Anderson }
141e2c63599SDoug Anderson 
142e2c63599SDoug Anderson /**
143e2c63599SDoug Anderson  * dw_mci_exynos_resume_noirq - Exynos-specific resume code
144e2c63599SDoug Anderson  *
145e2c63599SDoug Anderson  * On exynos5420 there is a silicon errata that will sometimes leave the
146e2c63599SDoug Anderson  * WAKEUP_INT bit in the CLKSEL register asserted.  This bit is 1 to indicate
147e2c63599SDoug Anderson  * that it fired and we can clear it by writing a 1 back.  Clear it to prevent
148e2c63599SDoug Anderson  * interrupts from going off constantly.
149e2c63599SDoug Anderson  *
150e2c63599SDoug Anderson  * We run this code on all exynos variants because it doesn't hurt.
151e2c63599SDoug Anderson  */
152e2c63599SDoug Anderson 
153e2c63599SDoug Anderson static int dw_mci_exynos_resume_noirq(struct device *dev)
154e2c63599SDoug Anderson {
155e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
156e2c63599SDoug Anderson 	u32 clksel;
157e2c63599SDoug Anderson 
158e2c63599SDoug Anderson 	clksel = mci_readl(host, CLKSEL);
159e2c63599SDoug Anderson 	if (clksel & SDMMC_CLKSEL_WAKEUP_INT)
160e2c63599SDoug Anderson 		mci_writel(host, CLKSEL, clksel);
161e2c63599SDoug Anderson 
162e2c63599SDoug Anderson 	return 0;
163e2c63599SDoug Anderson }
164e2c63599SDoug Anderson #else
165e2c63599SDoug Anderson #define dw_mci_exynos_suspend		NULL
166e2c63599SDoug Anderson #define dw_mci_exynos_resume		NULL
167e2c63599SDoug Anderson #define dw_mci_exynos_resume_noirq	NULL
168e2c63599SDoug Anderson #endif /* CONFIG_PM_SLEEP */
169e2c63599SDoug Anderson 
170c3665006SThomas Abraham static void dw_mci_exynos_prepare_command(struct dw_mci *host, u32 *cmdr)
171c3665006SThomas Abraham {
172c3665006SThomas Abraham 	/*
173c3665006SThomas Abraham 	 * Exynos4412 and Exynos5250 extends the use of CMD register with the
174c3665006SThomas Abraham 	 * use of bit 29 (which is reserved on standard MSHC controllers) for
175c3665006SThomas Abraham 	 * optionally bypassing the HOLD register for command and data. The
176c3665006SThomas Abraham 	 * HOLD register should be bypassed in case there is no phase shift
177c3665006SThomas Abraham 	 * applied on CMD/DATA that is sent to the card.
178c3665006SThomas Abraham 	 */
179c3665006SThomas Abraham 	if (SDMMC_CLKSEL_GET_DRV_WD3(mci_readl(host, CLKSEL)))
180c3665006SThomas Abraham 		*cmdr |= SDMMC_CMD_USE_HOLD_REG;
181c3665006SThomas Abraham }
182c3665006SThomas Abraham 
183c3665006SThomas Abraham static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
184c3665006SThomas Abraham {
185c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
186c6d9dedaSSeungwon Jeon 	unsigned int wanted = ios->clock;
187c6d9dedaSSeungwon Jeon 	unsigned long actual;
188c6d9dedaSSeungwon Jeon 	u8 div = priv->ciu_div + 1;
189c3665006SThomas Abraham 
190cab3a802SSeungwon Jeon 	if (ios->timing == MMC_TIMING_MMC_DDR52) {
191c3665006SThomas Abraham 		mci_writel(host, CLKSEL, priv->ddr_timing);
192c6d9dedaSSeungwon Jeon 		/* Should be double rate for DDR mode */
193c6d9dedaSSeungwon Jeon 		if (ios->bus_width == MMC_BUS_WIDTH_8)
194c6d9dedaSSeungwon Jeon 			wanted <<= 1;
195c6d9dedaSSeungwon Jeon 	} else {
196c3665006SThomas Abraham 		mci_writel(host, CLKSEL, priv->sdr_timing);
197c3665006SThomas Abraham 	}
198c3665006SThomas Abraham 
199c6d9dedaSSeungwon Jeon 	/* Don't care if wanted clock is zero */
200c6d9dedaSSeungwon Jeon 	if (!wanted)
201c6d9dedaSSeungwon Jeon 		return;
202c6d9dedaSSeungwon Jeon 
203c6d9dedaSSeungwon Jeon 	/* Guaranteed minimum frequency for cclkin */
204c6d9dedaSSeungwon Jeon 	if (wanted < EXYNOS_CCLKIN_MIN)
205c6d9dedaSSeungwon Jeon 		wanted = EXYNOS_CCLKIN_MIN;
206c6d9dedaSSeungwon Jeon 
207c6d9dedaSSeungwon Jeon 	if (wanted != priv->cur_speed) {
208c6d9dedaSSeungwon Jeon 		int ret = clk_set_rate(host->ciu_clk, wanted * div);
209c6d9dedaSSeungwon Jeon 		if (ret)
210c6d9dedaSSeungwon Jeon 			dev_warn(host->dev,
211c6d9dedaSSeungwon Jeon 				"failed to set clk-rate %u error: %d\n",
212c6d9dedaSSeungwon Jeon 				 wanted * div, ret);
213c6d9dedaSSeungwon Jeon 		actual = clk_get_rate(host->ciu_clk);
214c6d9dedaSSeungwon Jeon 		host->bus_hz = actual / div;
215c6d9dedaSSeungwon Jeon 		priv->cur_speed = wanted;
216c6d9dedaSSeungwon Jeon 		host->current_speed = 0;
217c6d9dedaSSeungwon Jeon 	}
218c6d9dedaSSeungwon Jeon }
219c6d9dedaSSeungwon Jeon 
220c3665006SThomas Abraham static int dw_mci_exynos_parse_dt(struct dw_mci *host)
221c3665006SThomas Abraham {
222e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv;
223c3665006SThomas Abraham 	struct device_node *np = host->dev->of_node;
224c3665006SThomas Abraham 	u32 timing[2];
225c3665006SThomas Abraham 	u32 div = 0;
226e6c784edSYuvaraj Kumar C D 	int idx;
227c3665006SThomas Abraham 	int ret;
228c3665006SThomas Abraham 
229e6c784edSYuvaraj Kumar C D 	priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
230e6c784edSYuvaraj Kumar C D 	if (!priv) {
231e6c784edSYuvaraj Kumar C D 		dev_err(host->dev, "mem alloc failed for private data\n");
232e6c784edSYuvaraj Kumar C D 		return -ENOMEM;
233e6c784edSYuvaraj Kumar C D 	}
234e6c784edSYuvaraj Kumar C D 
235e6c784edSYuvaraj Kumar C D 	for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
236e6c784edSYuvaraj Kumar C D 		if (of_device_is_compatible(np, exynos_compat[idx].compatible))
237e6c784edSYuvaraj Kumar C D 			priv->ctrl_type = exynos_compat[idx].ctrl_type;
238e6c784edSYuvaraj Kumar C D 	}
239e6c784edSYuvaraj Kumar C D 
240c6d9dedaSSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
241c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1;
242c6d9dedaSSeungwon Jeon 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
243c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4210_FIXED_CIU_CLK_DIV - 1;
244c6d9dedaSSeungwon Jeon 	else {
245c3665006SThomas Abraham 		of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
246c3665006SThomas Abraham 		priv->ciu_div = div;
247c6d9dedaSSeungwon Jeon 	}
248c3665006SThomas Abraham 
249c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
250c3665006SThomas Abraham 			"samsung,dw-mshc-sdr-timing", timing, 2);
251c3665006SThomas Abraham 	if (ret)
252c3665006SThomas Abraham 		return ret;
253c3665006SThomas Abraham 
2542d9f0bd1SYuvaraj Kumar C D 	priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
2552d9f0bd1SYuvaraj Kumar C D 
256c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
257c3665006SThomas Abraham 			"samsung,dw-mshc-ddr-timing", timing, 2);
258c3665006SThomas Abraham 	if (ret)
259c3665006SThomas Abraham 		return ret;
260c3665006SThomas Abraham 
261c3665006SThomas Abraham 	priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
262e6c784edSYuvaraj Kumar C D 	host->priv = priv;
263c3665006SThomas Abraham 	return 0;
264c3665006SThomas Abraham }
265c3665006SThomas Abraham 
266c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_get_clksmpl(struct dw_mci *host)
267c537a1c5SSeungwon Jeon {
268c537a1c5SSeungwon Jeon 	return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL));
269c537a1c5SSeungwon Jeon }
270c537a1c5SSeungwon Jeon 
271c537a1c5SSeungwon Jeon static inline void dw_mci_exynos_set_clksmpl(struct dw_mci *host, u8 sample)
272c537a1c5SSeungwon Jeon {
273c537a1c5SSeungwon Jeon 	u32 clksel;
274c537a1c5SSeungwon Jeon 	clksel = mci_readl(host, CLKSEL);
275c537a1c5SSeungwon Jeon 	clksel = (clksel & ~0x7) | SDMMC_CLKSEL_CCLK_SAMPLE(sample);
276c537a1c5SSeungwon Jeon 	mci_writel(host, CLKSEL, clksel);
277c537a1c5SSeungwon Jeon }
278c537a1c5SSeungwon Jeon 
279c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_move_next_clksmpl(struct dw_mci *host)
280c537a1c5SSeungwon Jeon {
281c537a1c5SSeungwon Jeon 	u32 clksel;
282c537a1c5SSeungwon Jeon 	u8 sample;
283c537a1c5SSeungwon Jeon 
284c537a1c5SSeungwon Jeon 	clksel = mci_readl(host, CLKSEL);
285c537a1c5SSeungwon Jeon 	sample = (clksel + 1) & 0x7;
286c537a1c5SSeungwon Jeon 	clksel = (clksel & ~0x7) | sample;
287c537a1c5SSeungwon Jeon 	mci_writel(host, CLKSEL, clksel);
288c537a1c5SSeungwon Jeon 	return sample;
289c537a1c5SSeungwon Jeon }
290c537a1c5SSeungwon Jeon 
291c537a1c5SSeungwon Jeon static s8 dw_mci_exynos_get_best_clksmpl(u8 candiates)
292c537a1c5SSeungwon Jeon {
293c537a1c5SSeungwon Jeon 	const u8 iter = 8;
294c537a1c5SSeungwon Jeon 	u8 __c;
295c537a1c5SSeungwon Jeon 	s8 i, loc = -1;
296c537a1c5SSeungwon Jeon 
297c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
298c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
299c537a1c5SSeungwon Jeon 		if ((__c & 0xc7) == 0xc7) {
300c537a1c5SSeungwon Jeon 			loc = i;
301c537a1c5SSeungwon Jeon 			goto out;
302c537a1c5SSeungwon Jeon 		}
303c537a1c5SSeungwon Jeon 	}
304c537a1c5SSeungwon Jeon 
305c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
306c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
307c537a1c5SSeungwon Jeon 		if ((__c & 0x83) == 0x83) {
308c537a1c5SSeungwon Jeon 			loc = i;
309c537a1c5SSeungwon Jeon 			goto out;
310c537a1c5SSeungwon Jeon 		}
311c537a1c5SSeungwon Jeon 	}
312c537a1c5SSeungwon Jeon 
313c537a1c5SSeungwon Jeon out:
314c537a1c5SSeungwon Jeon 	return loc;
315c537a1c5SSeungwon Jeon }
316c537a1c5SSeungwon Jeon 
317c537a1c5SSeungwon Jeon static int dw_mci_exynos_execute_tuning(struct dw_mci_slot *slot, u32 opcode,
318c537a1c5SSeungwon Jeon 					struct dw_mci_tuning_data *tuning_data)
319c537a1c5SSeungwon Jeon {
320c537a1c5SSeungwon Jeon 	struct dw_mci *host = slot->host;
321c537a1c5SSeungwon Jeon 	struct mmc_host *mmc = slot->mmc;
322c537a1c5SSeungwon Jeon 	const u8 *blk_pattern = tuning_data->blk_pattern;
323c537a1c5SSeungwon Jeon 	u8 *blk_test;
324c537a1c5SSeungwon Jeon 	unsigned int blksz = tuning_data->blksz;
325c537a1c5SSeungwon Jeon 	u8 start_smpl, smpl, candiates = 0;
326c537a1c5SSeungwon Jeon 	s8 found = -1;
327c537a1c5SSeungwon Jeon 	int ret = 0;
328c537a1c5SSeungwon Jeon 
329c537a1c5SSeungwon Jeon 	blk_test = kmalloc(blksz, GFP_KERNEL);
330c537a1c5SSeungwon Jeon 	if (!blk_test)
331c537a1c5SSeungwon Jeon 		return -ENOMEM;
332c537a1c5SSeungwon Jeon 
333c537a1c5SSeungwon Jeon 	start_smpl = dw_mci_exynos_get_clksmpl(host);
334c537a1c5SSeungwon Jeon 
335c537a1c5SSeungwon Jeon 	do {
336c537a1c5SSeungwon Jeon 		struct mmc_request mrq = {NULL};
337c537a1c5SSeungwon Jeon 		struct mmc_command cmd = {0};
338c537a1c5SSeungwon Jeon 		struct mmc_command stop = {0};
339c537a1c5SSeungwon Jeon 		struct mmc_data data = {0};
340c537a1c5SSeungwon Jeon 		struct scatterlist sg;
341c537a1c5SSeungwon Jeon 
342c537a1c5SSeungwon Jeon 		cmd.opcode = opcode;
343c537a1c5SSeungwon Jeon 		cmd.arg = 0;
344c537a1c5SSeungwon Jeon 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
345c537a1c5SSeungwon Jeon 
346c537a1c5SSeungwon Jeon 		stop.opcode = MMC_STOP_TRANSMISSION;
347c537a1c5SSeungwon Jeon 		stop.arg = 0;
348c537a1c5SSeungwon Jeon 		stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
349c537a1c5SSeungwon Jeon 
350c537a1c5SSeungwon Jeon 		data.blksz = blksz;
351c537a1c5SSeungwon Jeon 		data.blocks = 1;
352c537a1c5SSeungwon Jeon 		data.flags = MMC_DATA_READ;
353c537a1c5SSeungwon Jeon 		data.sg = &sg;
354c537a1c5SSeungwon Jeon 		data.sg_len = 1;
355c537a1c5SSeungwon Jeon 
356c537a1c5SSeungwon Jeon 		sg_init_one(&sg, blk_test, blksz);
357c537a1c5SSeungwon Jeon 		mrq.cmd = &cmd;
358c537a1c5SSeungwon Jeon 		mrq.stop = &stop;
359c537a1c5SSeungwon Jeon 		mrq.data = &data;
360c537a1c5SSeungwon Jeon 		host->mrq = &mrq;
361c537a1c5SSeungwon Jeon 
362c537a1c5SSeungwon Jeon 		mci_writel(host, TMOUT, ~0);
363c537a1c5SSeungwon Jeon 		smpl = dw_mci_exynos_move_next_clksmpl(host);
364c537a1c5SSeungwon Jeon 
365c537a1c5SSeungwon Jeon 		mmc_wait_for_req(mmc, &mrq);
366c537a1c5SSeungwon Jeon 
367c537a1c5SSeungwon Jeon 		if (!cmd.error && !data.error) {
368c537a1c5SSeungwon Jeon 			if (!memcmp(blk_pattern, blk_test, blksz))
369c537a1c5SSeungwon Jeon 				candiates |= (1 << smpl);
370c537a1c5SSeungwon Jeon 		} else {
371c537a1c5SSeungwon Jeon 			dev_dbg(host->dev,
372c537a1c5SSeungwon Jeon 				"Tuning error: cmd.error:%d, data.error:%d\n",
373c537a1c5SSeungwon Jeon 				cmd.error, data.error);
374c537a1c5SSeungwon Jeon 		}
375c537a1c5SSeungwon Jeon 	} while (start_smpl != smpl);
376c537a1c5SSeungwon Jeon 
377c537a1c5SSeungwon Jeon 	found = dw_mci_exynos_get_best_clksmpl(candiates);
378c537a1c5SSeungwon Jeon 	if (found >= 0)
379c537a1c5SSeungwon Jeon 		dw_mci_exynos_set_clksmpl(host, found);
380c537a1c5SSeungwon Jeon 	else
381c537a1c5SSeungwon Jeon 		ret = -EIO;
382c537a1c5SSeungwon Jeon 
383c537a1c5SSeungwon Jeon 	kfree(blk_test);
384c537a1c5SSeungwon Jeon 	return ret;
385c537a1c5SSeungwon Jeon }
386c537a1c5SSeungwon Jeon 
3870f6e73d0SDongjin Kim /* Common capabilities of Exynos4/Exynos5 SoC */
3880f6e73d0SDongjin Kim static unsigned long exynos_dwmmc_caps[4] = {
389cab3a802SSeungwon Jeon 	MMC_CAP_1_8V_DDR | MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
390c3665006SThomas Abraham 	MMC_CAP_CMD23,
391c3665006SThomas Abraham 	MMC_CAP_CMD23,
392c3665006SThomas Abraham 	MMC_CAP_CMD23,
393c3665006SThomas Abraham };
394c3665006SThomas Abraham 
3950f6e73d0SDongjin Kim static const struct dw_mci_drv_data exynos_drv_data = {
3960f6e73d0SDongjin Kim 	.caps			= exynos_dwmmc_caps,
397c3665006SThomas Abraham 	.init			= dw_mci_exynos_priv_init,
398c3665006SThomas Abraham 	.setup_clock		= dw_mci_exynos_setup_clock,
399c3665006SThomas Abraham 	.prepare_command	= dw_mci_exynos_prepare_command,
400c3665006SThomas Abraham 	.set_ios		= dw_mci_exynos_set_ios,
401c3665006SThomas Abraham 	.parse_dt		= dw_mci_exynos_parse_dt,
402c537a1c5SSeungwon Jeon 	.execute_tuning		= dw_mci_exynos_execute_tuning,
403c3665006SThomas Abraham };
404c3665006SThomas Abraham 
405c3665006SThomas Abraham static const struct of_device_id dw_mci_exynos_match[] = {
4060f6e73d0SDongjin Kim 	{ .compatible = "samsung,exynos4412-dw-mshc",
4070f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
408c3665006SThomas Abraham 	{ .compatible = "samsung,exynos5250-dw-mshc",
4090f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
41000fd041bSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc",
41100fd041bSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
4126bce431cSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc-smu",
4136bce431cSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
414c3665006SThomas Abraham 	{},
415c3665006SThomas Abraham };
416517cb9f1SArnd Bergmann MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
417c3665006SThomas Abraham 
4189665f7f2SSachin Kamat static int dw_mci_exynos_probe(struct platform_device *pdev)
419c3665006SThomas Abraham {
4208e2b36eaSArnd Bergmann 	const struct dw_mci_drv_data *drv_data;
421c3665006SThomas Abraham 	const struct of_device_id *match;
422c3665006SThomas Abraham 
423c3665006SThomas Abraham 	match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
424c3665006SThomas Abraham 	drv_data = match->data;
425c3665006SThomas Abraham 	return dw_mci_pltfm_register(pdev, drv_data);
426c3665006SThomas Abraham }
427c3665006SThomas Abraham 
428*15a2e2abSSachin Kamat static const struct dev_pm_ops dw_mci_exynos_pmops = {
429e2c63599SDoug Anderson 	SET_SYSTEM_SLEEP_PM_OPS(dw_mci_exynos_suspend, dw_mci_exynos_resume)
430e2c63599SDoug Anderson 	.resume_noirq = dw_mci_exynos_resume_noirq,
431e2c63599SDoug Anderson 	.thaw_noirq = dw_mci_exynos_resume_noirq,
432e2c63599SDoug Anderson 	.restore_noirq = dw_mci_exynos_resume_noirq,
433e2c63599SDoug Anderson };
434e2c63599SDoug Anderson 
435c3665006SThomas Abraham static struct platform_driver dw_mci_exynos_pltfm_driver = {
436c3665006SThomas Abraham 	.probe		= dw_mci_exynos_probe,
437c3665006SThomas Abraham 	.remove		= __exit_p(dw_mci_pltfm_remove),
438c3665006SThomas Abraham 	.driver		= {
439c3665006SThomas Abraham 		.name		= "dwmmc_exynos",
44020183d50SSachin Kamat 		.of_match_table	= dw_mci_exynos_match,
441e2c63599SDoug Anderson 		.pm		= &dw_mci_exynos_pmops,
442c3665006SThomas Abraham 	},
443c3665006SThomas Abraham };
444c3665006SThomas Abraham 
445c3665006SThomas Abraham module_platform_driver(dw_mci_exynos_pltfm_driver);
446c3665006SThomas Abraham 
447c3665006SThomas Abraham MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
448c3665006SThomas Abraham MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
449c3665006SThomas Abraham MODULE_LICENSE("GPL v2");
450c3665006SThomas Abraham MODULE_ALIAS("platform:dwmmc-exynos");
451