xref: /openbmc/linux/drivers/mmc/host/dw_mmc-exynos.c (revision c537a1c5ff63d3553617a9ff80ef5ed1493028e2)
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>
17*c537a1c5SSeungwon Jeon #include <linux/mmc/mmc.h>
18c3665006SThomas Abraham #include <linux/of.h>
19c3665006SThomas Abraham #include <linux/of_gpio.h>
20*c537a1c5SSeungwon 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 
59c3665006SThomas Abraham /* Variations in Exynos specific dw-mshc controller */
60c3665006SThomas Abraham enum dw_mci_exynos_type {
61c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4210,
62c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4412,
63c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS5250,
6400fd041bSYuvaraj Kumar C D 	DW_MCI_TYPE_EXYNOS5420,
656bce431cSYuvaraj Kumar C D 	DW_MCI_TYPE_EXYNOS5420_SMU,
66c3665006SThomas Abraham };
67c3665006SThomas Abraham 
68c3665006SThomas Abraham /* Exynos implementation specific driver private data */
69c3665006SThomas Abraham struct dw_mci_exynos_priv_data {
70c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
71c3665006SThomas Abraham 	u8				ciu_div;
72c3665006SThomas Abraham 	u32				sdr_timing;
73c3665006SThomas Abraham 	u32				ddr_timing;
74c3665006SThomas Abraham };
75c3665006SThomas Abraham 
76c3665006SThomas Abraham static struct dw_mci_exynos_compatible {
77c3665006SThomas Abraham 	char				*compatible;
78c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
79c3665006SThomas Abraham } exynos_compat[] = {
80c3665006SThomas Abraham 	{
81c3665006SThomas Abraham 		.compatible	= "samsung,exynos4210-dw-mshc",
82c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4210,
83c3665006SThomas Abraham 	}, {
84c3665006SThomas Abraham 		.compatible	= "samsung,exynos4412-dw-mshc",
85c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4412,
86c3665006SThomas Abraham 	}, {
87c3665006SThomas Abraham 		.compatible	= "samsung,exynos5250-dw-mshc",
88c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5250,
8900fd041bSYuvaraj Kumar C D 	}, {
9000fd041bSYuvaraj Kumar C D 		.compatible	= "samsung,exynos5420-dw-mshc",
9100fd041bSYuvaraj Kumar C D 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5420,
926bce431cSYuvaraj Kumar C D 	}, {
936bce431cSYuvaraj Kumar C D 		.compatible	= "samsung,exynos5420-dw-mshc-smu",
946bce431cSYuvaraj Kumar C D 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5420_SMU,
95c3665006SThomas Abraham 	},
96c3665006SThomas Abraham };
97c3665006SThomas Abraham 
98c3665006SThomas Abraham static int dw_mci_exynos_priv_init(struct dw_mci *host)
99c3665006SThomas Abraham {
100e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv = host->priv;
101c3665006SThomas Abraham 
1026bce431cSYuvaraj Kumar C D 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU) {
1036bce431cSYuvaraj Kumar C D 		mci_writel(host, MPSBEGIN0, 0);
1046bce431cSYuvaraj Kumar C D 		mci_writel(host, MPSEND0, DWMCI_BLOCK_NUM);
1056bce431cSYuvaraj Kumar C D 		mci_writel(host, MPSCTRL0, DWMCI_MPSCTRL_SECURE_WRITE_BIT |
1066bce431cSYuvaraj Kumar C D 			   DWMCI_MPSCTRL_NON_SECURE_READ_BIT |
1076bce431cSYuvaraj Kumar C D 			   DWMCI_MPSCTRL_VALID |
1086bce431cSYuvaraj Kumar C D 			   DWMCI_MPSCTRL_NON_SECURE_WRITE_BIT);
1096bce431cSYuvaraj Kumar C D 	}
1106bce431cSYuvaraj Kumar C D 
111c3665006SThomas Abraham 	return 0;
112c3665006SThomas Abraham }
113c3665006SThomas Abraham 
114c3665006SThomas Abraham static int dw_mci_exynos_setup_clock(struct dw_mci *host)
115c3665006SThomas Abraham {
116c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
117c3665006SThomas Abraham 
11800fd041bSYuvaraj Kumar C D 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5250 ||
1196bce431cSYuvaraj Kumar C D 	    priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420 ||
1206bce431cSYuvaraj Kumar C D 	    priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU)
121c3665006SThomas Abraham 		host->bus_hz /= (priv->ciu_div + 1);
122c3665006SThomas Abraham 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
123c3665006SThomas Abraham 		host->bus_hz /= EXYNOS4412_FIXED_CIU_CLK_DIV;
124c3665006SThomas Abraham 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
125c3665006SThomas Abraham 		host->bus_hz /= EXYNOS4210_FIXED_CIU_CLK_DIV;
126c3665006SThomas Abraham 
127c3665006SThomas Abraham 	return 0;
128c3665006SThomas Abraham }
129c3665006SThomas Abraham 
130e2c63599SDoug Anderson #ifdef CONFIG_PM_SLEEP
131e2c63599SDoug Anderson static int dw_mci_exynos_suspend(struct device *dev)
132e2c63599SDoug Anderson {
133e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
134e2c63599SDoug Anderson 
135e2c63599SDoug Anderson 	return dw_mci_suspend(host);
136e2c63599SDoug Anderson }
137e2c63599SDoug Anderson 
138e2c63599SDoug Anderson static int dw_mci_exynos_resume(struct device *dev)
139e2c63599SDoug Anderson {
140e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
141e2c63599SDoug Anderson 
1426bce431cSYuvaraj Kumar C D 	dw_mci_exynos_priv_init(host);
143e2c63599SDoug Anderson 	return dw_mci_resume(host);
144e2c63599SDoug Anderson }
145e2c63599SDoug Anderson 
146e2c63599SDoug Anderson /**
147e2c63599SDoug Anderson  * dw_mci_exynos_resume_noirq - Exynos-specific resume code
148e2c63599SDoug Anderson  *
149e2c63599SDoug Anderson  * On exynos5420 there is a silicon errata that will sometimes leave the
150e2c63599SDoug Anderson  * WAKEUP_INT bit in the CLKSEL register asserted.  This bit is 1 to indicate
151e2c63599SDoug Anderson  * that it fired and we can clear it by writing a 1 back.  Clear it to prevent
152e2c63599SDoug Anderson  * interrupts from going off constantly.
153e2c63599SDoug Anderson  *
154e2c63599SDoug Anderson  * We run this code on all exynos variants because it doesn't hurt.
155e2c63599SDoug Anderson  */
156e2c63599SDoug Anderson 
157e2c63599SDoug Anderson static int dw_mci_exynos_resume_noirq(struct device *dev)
158e2c63599SDoug Anderson {
159e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
160e2c63599SDoug Anderson 	u32 clksel;
161e2c63599SDoug Anderson 
162e2c63599SDoug Anderson 	clksel = mci_readl(host, CLKSEL);
163e2c63599SDoug Anderson 	if (clksel & SDMMC_CLKSEL_WAKEUP_INT)
164e2c63599SDoug Anderson 		mci_writel(host, CLKSEL, clksel);
165e2c63599SDoug Anderson 
166e2c63599SDoug Anderson 	return 0;
167e2c63599SDoug Anderson }
168e2c63599SDoug Anderson #else
169e2c63599SDoug Anderson #define dw_mci_exynos_suspend		NULL
170e2c63599SDoug Anderson #define dw_mci_exynos_resume		NULL
171e2c63599SDoug Anderson #define dw_mci_exynos_resume_noirq	NULL
172e2c63599SDoug Anderson #endif /* CONFIG_PM_SLEEP */
173e2c63599SDoug Anderson 
174c3665006SThomas Abraham static void dw_mci_exynos_prepare_command(struct dw_mci *host, u32 *cmdr)
175c3665006SThomas Abraham {
176c3665006SThomas Abraham 	/*
177c3665006SThomas Abraham 	 * Exynos4412 and Exynos5250 extends the use of CMD register with the
178c3665006SThomas Abraham 	 * use of bit 29 (which is reserved on standard MSHC controllers) for
179c3665006SThomas Abraham 	 * optionally bypassing the HOLD register for command and data. The
180c3665006SThomas Abraham 	 * HOLD register should be bypassed in case there is no phase shift
181c3665006SThomas Abraham 	 * applied on CMD/DATA that is sent to the card.
182c3665006SThomas Abraham 	 */
183c3665006SThomas Abraham 	if (SDMMC_CLKSEL_GET_DRV_WD3(mci_readl(host, CLKSEL)))
184c3665006SThomas Abraham 		*cmdr |= SDMMC_CMD_USE_HOLD_REG;
185c3665006SThomas Abraham }
186c3665006SThomas Abraham 
187c3665006SThomas Abraham static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
188c3665006SThomas Abraham {
189c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
190c3665006SThomas Abraham 
191c3665006SThomas Abraham 	if (ios->timing == MMC_TIMING_UHS_DDR50)
192c3665006SThomas Abraham 		mci_writel(host, CLKSEL, priv->ddr_timing);
193c3665006SThomas Abraham 	else
194c3665006SThomas Abraham 		mci_writel(host, CLKSEL, priv->sdr_timing);
195c3665006SThomas Abraham }
196c3665006SThomas Abraham 
197c3665006SThomas Abraham static int dw_mci_exynos_parse_dt(struct dw_mci *host)
198c3665006SThomas Abraham {
199e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv;
200c3665006SThomas Abraham 	struct device_node *np = host->dev->of_node;
201c3665006SThomas Abraham 	u32 timing[2];
202c3665006SThomas Abraham 	u32 div = 0;
203e6c784edSYuvaraj Kumar C D 	int idx;
204c3665006SThomas Abraham 	int ret;
205c3665006SThomas Abraham 
206e6c784edSYuvaraj Kumar C D 	priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
207e6c784edSYuvaraj Kumar C D 	if (!priv) {
208e6c784edSYuvaraj Kumar C D 		dev_err(host->dev, "mem alloc failed for private data\n");
209e6c784edSYuvaraj Kumar C D 		return -ENOMEM;
210e6c784edSYuvaraj Kumar C D 	}
211e6c784edSYuvaraj Kumar C D 
212e6c784edSYuvaraj Kumar C D 	for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
213e6c784edSYuvaraj Kumar C D 		if (of_device_is_compatible(np, exynos_compat[idx].compatible))
214e6c784edSYuvaraj Kumar C D 			priv->ctrl_type = exynos_compat[idx].ctrl_type;
215e6c784edSYuvaraj Kumar C D 	}
216e6c784edSYuvaraj Kumar C D 
217c3665006SThomas Abraham 	of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
218c3665006SThomas Abraham 	priv->ciu_div = div;
219c3665006SThomas Abraham 
220c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
221c3665006SThomas Abraham 			"samsung,dw-mshc-sdr-timing", timing, 2);
222c3665006SThomas Abraham 	if (ret)
223c3665006SThomas Abraham 		return ret;
224c3665006SThomas Abraham 
225c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
226c3665006SThomas Abraham 			"samsung,dw-mshc-ddr-timing", timing, 2);
227c3665006SThomas Abraham 	if (ret)
228c3665006SThomas Abraham 		return ret;
229c3665006SThomas Abraham 
230e6c784edSYuvaraj Kumar C D 	priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
231c3665006SThomas Abraham 	priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
232e6c784edSYuvaraj Kumar C D 	host->priv = priv;
233c3665006SThomas Abraham 	return 0;
234c3665006SThomas Abraham }
235c3665006SThomas Abraham 
236*c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_get_clksmpl(struct dw_mci *host)
237*c537a1c5SSeungwon Jeon {
238*c537a1c5SSeungwon Jeon 	return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL));
239*c537a1c5SSeungwon Jeon }
240*c537a1c5SSeungwon Jeon 
241*c537a1c5SSeungwon Jeon static inline void dw_mci_exynos_set_clksmpl(struct dw_mci *host, u8 sample)
242*c537a1c5SSeungwon Jeon {
243*c537a1c5SSeungwon Jeon 	u32 clksel;
244*c537a1c5SSeungwon Jeon 	clksel = mci_readl(host, CLKSEL);
245*c537a1c5SSeungwon Jeon 	clksel = (clksel & ~0x7) | SDMMC_CLKSEL_CCLK_SAMPLE(sample);
246*c537a1c5SSeungwon Jeon 	mci_writel(host, CLKSEL, clksel);
247*c537a1c5SSeungwon Jeon }
248*c537a1c5SSeungwon Jeon 
249*c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_move_next_clksmpl(struct dw_mci *host)
250*c537a1c5SSeungwon Jeon {
251*c537a1c5SSeungwon Jeon 	u32 clksel;
252*c537a1c5SSeungwon Jeon 	u8 sample;
253*c537a1c5SSeungwon Jeon 
254*c537a1c5SSeungwon Jeon 	clksel = mci_readl(host, CLKSEL);
255*c537a1c5SSeungwon Jeon 	sample = (clksel + 1) & 0x7;
256*c537a1c5SSeungwon Jeon 	clksel = (clksel & ~0x7) | sample;
257*c537a1c5SSeungwon Jeon 	mci_writel(host, CLKSEL, clksel);
258*c537a1c5SSeungwon Jeon 	return sample;
259*c537a1c5SSeungwon Jeon }
260*c537a1c5SSeungwon Jeon 
261*c537a1c5SSeungwon Jeon static s8 dw_mci_exynos_get_best_clksmpl(u8 candiates)
262*c537a1c5SSeungwon Jeon {
263*c537a1c5SSeungwon Jeon 	const u8 iter = 8;
264*c537a1c5SSeungwon Jeon 	u8 __c;
265*c537a1c5SSeungwon Jeon 	s8 i, loc = -1;
266*c537a1c5SSeungwon Jeon 
267*c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
268*c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
269*c537a1c5SSeungwon Jeon 		if ((__c & 0xc7) == 0xc7) {
270*c537a1c5SSeungwon Jeon 			loc = i;
271*c537a1c5SSeungwon Jeon 			goto out;
272*c537a1c5SSeungwon Jeon 		}
273*c537a1c5SSeungwon Jeon 	}
274*c537a1c5SSeungwon Jeon 
275*c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
276*c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
277*c537a1c5SSeungwon Jeon 		if ((__c & 0x83) == 0x83) {
278*c537a1c5SSeungwon Jeon 			loc = i;
279*c537a1c5SSeungwon Jeon 			goto out;
280*c537a1c5SSeungwon Jeon 		}
281*c537a1c5SSeungwon Jeon 	}
282*c537a1c5SSeungwon Jeon 
283*c537a1c5SSeungwon Jeon out:
284*c537a1c5SSeungwon Jeon 	return loc;
285*c537a1c5SSeungwon Jeon }
286*c537a1c5SSeungwon Jeon 
287*c537a1c5SSeungwon Jeon static int dw_mci_exynos_execute_tuning(struct dw_mci_slot *slot, u32 opcode,
288*c537a1c5SSeungwon Jeon 					struct dw_mci_tuning_data *tuning_data)
289*c537a1c5SSeungwon Jeon {
290*c537a1c5SSeungwon Jeon 	struct dw_mci *host = slot->host;
291*c537a1c5SSeungwon Jeon 	struct mmc_host *mmc = slot->mmc;
292*c537a1c5SSeungwon Jeon 	const u8 *blk_pattern = tuning_data->blk_pattern;
293*c537a1c5SSeungwon Jeon 	u8 *blk_test;
294*c537a1c5SSeungwon Jeon 	unsigned int blksz = tuning_data->blksz;
295*c537a1c5SSeungwon Jeon 	u8 start_smpl, smpl, candiates = 0;
296*c537a1c5SSeungwon Jeon 	s8 found = -1;
297*c537a1c5SSeungwon Jeon 	int ret = 0;
298*c537a1c5SSeungwon Jeon 
299*c537a1c5SSeungwon Jeon 	blk_test = kmalloc(blksz, GFP_KERNEL);
300*c537a1c5SSeungwon Jeon 	if (!blk_test)
301*c537a1c5SSeungwon Jeon 		return -ENOMEM;
302*c537a1c5SSeungwon Jeon 
303*c537a1c5SSeungwon Jeon 	start_smpl = dw_mci_exynos_get_clksmpl(host);
304*c537a1c5SSeungwon Jeon 
305*c537a1c5SSeungwon Jeon 	do {
306*c537a1c5SSeungwon Jeon 		struct mmc_request mrq = {NULL};
307*c537a1c5SSeungwon Jeon 		struct mmc_command cmd = {0};
308*c537a1c5SSeungwon Jeon 		struct mmc_command stop = {0};
309*c537a1c5SSeungwon Jeon 		struct mmc_data data = {0};
310*c537a1c5SSeungwon Jeon 		struct scatterlist sg;
311*c537a1c5SSeungwon Jeon 
312*c537a1c5SSeungwon Jeon 		cmd.opcode = opcode;
313*c537a1c5SSeungwon Jeon 		cmd.arg = 0;
314*c537a1c5SSeungwon Jeon 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
315*c537a1c5SSeungwon Jeon 
316*c537a1c5SSeungwon Jeon 		stop.opcode = MMC_STOP_TRANSMISSION;
317*c537a1c5SSeungwon Jeon 		stop.arg = 0;
318*c537a1c5SSeungwon Jeon 		stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
319*c537a1c5SSeungwon Jeon 
320*c537a1c5SSeungwon Jeon 		data.blksz = blksz;
321*c537a1c5SSeungwon Jeon 		data.blocks = 1;
322*c537a1c5SSeungwon Jeon 		data.flags = MMC_DATA_READ;
323*c537a1c5SSeungwon Jeon 		data.sg = &sg;
324*c537a1c5SSeungwon Jeon 		data.sg_len = 1;
325*c537a1c5SSeungwon Jeon 
326*c537a1c5SSeungwon Jeon 		sg_init_one(&sg, blk_test, blksz);
327*c537a1c5SSeungwon Jeon 		mrq.cmd = &cmd;
328*c537a1c5SSeungwon Jeon 		mrq.stop = &stop;
329*c537a1c5SSeungwon Jeon 		mrq.data = &data;
330*c537a1c5SSeungwon Jeon 		host->mrq = &mrq;
331*c537a1c5SSeungwon Jeon 
332*c537a1c5SSeungwon Jeon 		mci_writel(host, TMOUT, ~0);
333*c537a1c5SSeungwon Jeon 		smpl = dw_mci_exynos_move_next_clksmpl(host);
334*c537a1c5SSeungwon Jeon 
335*c537a1c5SSeungwon Jeon 		mmc_wait_for_req(mmc, &mrq);
336*c537a1c5SSeungwon Jeon 
337*c537a1c5SSeungwon Jeon 		if (!cmd.error && !data.error) {
338*c537a1c5SSeungwon Jeon 			if (!memcmp(blk_pattern, blk_test, blksz))
339*c537a1c5SSeungwon Jeon 				candiates |= (1 << smpl);
340*c537a1c5SSeungwon Jeon 		} else {
341*c537a1c5SSeungwon Jeon 			dev_dbg(host->dev,
342*c537a1c5SSeungwon Jeon 				"Tuning error: cmd.error:%d, data.error:%d\n",
343*c537a1c5SSeungwon Jeon 				cmd.error, data.error);
344*c537a1c5SSeungwon Jeon 		}
345*c537a1c5SSeungwon Jeon 	} while (start_smpl != smpl);
346*c537a1c5SSeungwon Jeon 
347*c537a1c5SSeungwon Jeon 	found = dw_mci_exynos_get_best_clksmpl(candiates);
348*c537a1c5SSeungwon Jeon 	if (found >= 0)
349*c537a1c5SSeungwon Jeon 		dw_mci_exynos_set_clksmpl(host, found);
350*c537a1c5SSeungwon Jeon 	else
351*c537a1c5SSeungwon Jeon 		ret = -EIO;
352*c537a1c5SSeungwon Jeon 
353*c537a1c5SSeungwon Jeon 	kfree(blk_test);
354*c537a1c5SSeungwon Jeon 	return ret;
355*c537a1c5SSeungwon Jeon }
356*c537a1c5SSeungwon Jeon 
3570f6e73d0SDongjin Kim /* Common capabilities of Exynos4/Exynos5 SoC */
3580f6e73d0SDongjin Kim static unsigned long exynos_dwmmc_caps[4] = {
359c3665006SThomas Abraham 	MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR |
360c3665006SThomas Abraham 		MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
361c3665006SThomas Abraham 	MMC_CAP_CMD23,
362c3665006SThomas Abraham 	MMC_CAP_CMD23,
363c3665006SThomas Abraham 	MMC_CAP_CMD23,
364c3665006SThomas Abraham };
365c3665006SThomas Abraham 
3660f6e73d0SDongjin Kim static const struct dw_mci_drv_data exynos_drv_data = {
3670f6e73d0SDongjin Kim 	.caps			= exynos_dwmmc_caps,
368c3665006SThomas Abraham 	.init			= dw_mci_exynos_priv_init,
369c3665006SThomas Abraham 	.setup_clock		= dw_mci_exynos_setup_clock,
370c3665006SThomas Abraham 	.prepare_command	= dw_mci_exynos_prepare_command,
371c3665006SThomas Abraham 	.set_ios		= dw_mci_exynos_set_ios,
372c3665006SThomas Abraham 	.parse_dt		= dw_mci_exynos_parse_dt,
373*c537a1c5SSeungwon Jeon 	.execute_tuning		= dw_mci_exynos_execute_tuning,
374c3665006SThomas Abraham };
375c3665006SThomas Abraham 
376c3665006SThomas Abraham static const struct of_device_id dw_mci_exynos_match[] = {
3770f6e73d0SDongjin Kim 	{ .compatible = "samsung,exynos4412-dw-mshc",
3780f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
379c3665006SThomas Abraham 	{ .compatible = "samsung,exynos5250-dw-mshc",
3800f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
38100fd041bSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc",
38200fd041bSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
3836bce431cSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc-smu",
3846bce431cSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
385c3665006SThomas Abraham 	{},
386c3665006SThomas Abraham };
387517cb9f1SArnd Bergmann MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
388c3665006SThomas Abraham 
3899665f7f2SSachin Kamat static int dw_mci_exynos_probe(struct platform_device *pdev)
390c3665006SThomas Abraham {
3918e2b36eaSArnd Bergmann 	const struct dw_mci_drv_data *drv_data;
392c3665006SThomas Abraham 	const struct of_device_id *match;
393c3665006SThomas Abraham 
394c3665006SThomas Abraham 	match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
395c3665006SThomas Abraham 	drv_data = match->data;
396c3665006SThomas Abraham 	return dw_mci_pltfm_register(pdev, drv_data);
397c3665006SThomas Abraham }
398c3665006SThomas Abraham 
399e2c63599SDoug Anderson const struct dev_pm_ops dw_mci_exynos_pmops = {
400e2c63599SDoug Anderson 	SET_SYSTEM_SLEEP_PM_OPS(dw_mci_exynos_suspend, dw_mci_exynos_resume)
401e2c63599SDoug Anderson 	.resume_noirq = dw_mci_exynos_resume_noirq,
402e2c63599SDoug Anderson 	.thaw_noirq = dw_mci_exynos_resume_noirq,
403e2c63599SDoug Anderson 	.restore_noirq = dw_mci_exynos_resume_noirq,
404e2c63599SDoug Anderson };
405e2c63599SDoug Anderson 
406c3665006SThomas Abraham static struct platform_driver dw_mci_exynos_pltfm_driver = {
407c3665006SThomas Abraham 	.probe		= dw_mci_exynos_probe,
408c3665006SThomas Abraham 	.remove		= __exit_p(dw_mci_pltfm_remove),
409c3665006SThomas Abraham 	.driver		= {
410c3665006SThomas Abraham 		.name		= "dwmmc_exynos",
41120183d50SSachin Kamat 		.of_match_table	= dw_mci_exynos_match,
412e2c63599SDoug Anderson 		.pm		= &dw_mci_exynos_pmops,
413c3665006SThomas Abraham 	},
414c3665006SThomas Abraham };
415c3665006SThomas Abraham 
416c3665006SThomas Abraham module_platform_driver(dw_mci_exynos_pltfm_driver);
417c3665006SThomas Abraham 
418c3665006SThomas Abraham MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
419c3665006SThomas Abraham MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
420c3665006SThomas Abraham MODULE_LICENSE("GPL v2");
421c3665006SThomas Abraham MODULE_ALIAS("platform:dwmmc-exynos");
422