xref: /openbmc/linux/drivers/mmc/host/dw_mmc-exynos.c (revision 941a659ffdefe31af8222451ca2a74ebab8d98dd)
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"
240b5fce48SSeungwon Jeon #include "dw_mmc-exynos.h"
25c6d9dedaSSeungwon Jeon 
26c3665006SThomas Abraham /* Variations in Exynos specific dw-mshc controller */
27c3665006SThomas Abraham enum dw_mci_exynos_type {
28c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4210,
29c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4412,
30c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS5250,
3100fd041bSYuvaraj Kumar C D 	DW_MCI_TYPE_EXYNOS5420,
326bce431cSYuvaraj Kumar C D 	DW_MCI_TYPE_EXYNOS5420_SMU,
3389ad2be7SAbhilash Kesavan 	DW_MCI_TYPE_EXYNOS7,
3489ad2be7SAbhilash Kesavan 	DW_MCI_TYPE_EXYNOS7_SMU,
35c3665006SThomas Abraham };
36c3665006SThomas Abraham 
37c3665006SThomas Abraham /* Exynos implementation specific driver private data */
38c3665006SThomas Abraham struct dw_mci_exynos_priv_data {
39c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
40c3665006SThomas Abraham 	u8				ciu_div;
41c3665006SThomas Abraham 	u32				sdr_timing;
42c3665006SThomas Abraham 	u32				ddr_timing;
4380113132SSeungwon Jeon 	u32				hs400_timing;
4480113132SSeungwon Jeon 	u32				tuned_sample;
45c6d9dedaSSeungwon Jeon 	u32				cur_speed;
4680113132SSeungwon Jeon 	u32				dqs_delay;
4780113132SSeungwon Jeon 	u32				saved_dqs_en;
4880113132SSeungwon Jeon 	u32				saved_strobe_ctrl;
49c3665006SThomas Abraham };
50c3665006SThomas Abraham 
51c3665006SThomas Abraham static struct dw_mci_exynos_compatible {
52c3665006SThomas Abraham 	char				*compatible;
53c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
54c3665006SThomas Abraham } exynos_compat[] = {
55c3665006SThomas Abraham 	{
56c3665006SThomas Abraham 		.compatible	= "samsung,exynos4210-dw-mshc",
57c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4210,
58c3665006SThomas Abraham 	}, {
59c3665006SThomas Abraham 		.compatible	= "samsung,exynos4412-dw-mshc",
60c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4412,
61c3665006SThomas Abraham 	}, {
62c3665006SThomas Abraham 		.compatible	= "samsung,exynos5250-dw-mshc",
63c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5250,
6400fd041bSYuvaraj Kumar C D 	}, {
6500fd041bSYuvaraj Kumar C D 		.compatible	= "samsung,exynos5420-dw-mshc",
6600fd041bSYuvaraj Kumar C D 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5420,
676bce431cSYuvaraj Kumar C D 	}, {
686bce431cSYuvaraj Kumar C D 		.compatible	= "samsung,exynos5420-dw-mshc-smu",
696bce431cSYuvaraj Kumar C D 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5420_SMU,
7089ad2be7SAbhilash Kesavan 	}, {
7189ad2be7SAbhilash Kesavan 		.compatible	= "samsung,exynos7-dw-mshc",
7289ad2be7SAbhilash Kesavan 		.ctrl_type	= DW_MCI_TYPE_EXYNOS7,
7389ad2be7SAbhilash Kesavan 	}, {
7489ad2be7SAbhilash Kesavan 		.compatible	= "samsung,exynos7-dw-mshc-smu",
7589ad2be7SAbhilash Kesavan 		.ctrl_type	= DW_MCI_TYPE_EXYNOS7_SMU,
76c3665006SThomas Abraham 	},
77c3665006SThomas Abraham };
78c3665006SThomas Abraham 
7980113132SSeungwon Jeon static inline u8 dw_mci_exynos_get_ciu_div(struct dw_mci *host)
8080113132SSeungwon Jeon {
8180113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
8280113132SSeungwon Jeon 
8380113132SSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
8480113132SSeungwon Jeon 		return EXYNOS4412_FIXED_CIU_CLK_DIV;
8580113132SSeungwon Jeon 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
8680113132SSeungwon Jeon 		return EXYNOS4210_FIXED_CIU_CLK_DIV;
8780113132SSeungwon Jeon 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
8880113132SSeungwon Jeon 			priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
8980113132SSeungwon Jeon 		return SDMMC_CLKSEL_GET_DIV(mci_readl(host, CLKSEL64)) + 1;
9080113132SSeungwon Jeon 	else
9180113132SSeungwon Jeon 		return SDMMC_CLKSEL_GET_DIV(mci_readl(host, CLKSEL)) + 1;
9280113132SSeungwon Jeon }
9380113132SSeungwon Jeon 
945659eeadSJaehoon Chung static void dw_mci_exynos_config_smu(struct dw_mci *host)
95c3665006SThomas Abraham {
96e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv = host->priv;
97c3665006SThomas Abraham 
985659eeadSJaehoon Chung 	/*
995659eeadSJaehoon Chung 	 * If Exynos is provided the Security management,
1005659eeadSJaehoon Chung 	 * set for non-ecryption mode at this time.
1015659eeadSJaehoon Chung 	 */
10289ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU ||
10389ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU) {
1046bce431cSYuvaraj Kumar C D 		mci_writel(host, MPSBEGIN0, 0);
1050b5fce48SSeungwon Jeon 		mci_writel(host, MPSEND0, SDMMC_ENDING_SEC_NR_MAX);
1060b5fce48SSeungwon Jeon 		mci_writel(host, MPSCTRL0, SDMMC_MPSCTRL_SECURE_WRITE_BIT |
1070b5fce48SSeungwon Jeon 			   SDMMC_MPSCTRL_NON_SECURE_READ_BIT |
1080b5fce48SSeungwon Jeon 			   SDMMC_MPSCTRL_VALID |
1090b5fce48SSeungwon Jeon 			   SDMMC_MPSCTRL_NON_SECURE_WRITE_BIT);
1106bce431cSYuvaraj Kumar C D 	}
1115659eeadSJaehoon Chung }
1125659eeadSJaehoon Chung 
1135659eeadSJaehoon Chung static int dw_mci_exynos_priv_init(struct dw_mci *host)
1145659eeadSJaehoon Chung {
1155659eeadSJaehoon Chung 	struct dw_mci_exynos_priv_data *priv = host->priv;
1165659eeadSJaehoon Chung 
1175659eeadSJaehoon Chung 	dw_mci_exynos_config_smu(host);
1186bce431cSYuvaraj Kumar C D 
11980113132SSeungwon Jeon 	if (priv->ctrl_type >= DW_MCI_TYPE_EXYNOS5420) {
12080113132SSeungwon Jeon 		priv->saved_strobe_ctrl = mci_readl(host, HS400_DLINE_CTRL);
12180113132SSeungwon Jeon 		priv->saved_dqs_en = mci_readl(host, HS400_DQS_EN);
12280113132SSeungwon Jeon 		priv->saved_dqs_en |= AXI_NON_BLOCKING_WR;
12380113132SSeungwon Jeon 		mci_writel(host, HS400_DQS_EN, priv->saved_dqs_en);
12480113132SSeungwon Jeon 		if (!priv->dqs_delay)
12580113132SSeungwon Jeon 			priv->dqs_delay =
12680113132SSeungwon Jeon 				DQS_CTRL_GET_RD_DELAY(priv->saved_strobe_ctrl);
12780113132SSeungwon Jeon 	}
12880113132SSeungwon Jeon 
129a2a1fed8SSeungwon Jeon 	host->bus_hz /= (priv->ciu_div + 1);
130a2a1fed8SSeungwon Jeon 
131c3665006SThomas Abraham 	return 0;
132c3665006SThomas Abraham }
133c3665006SThomas Abraham 
13480113132SSeungwon Jeon static void dw_mci_exynos_set_clksel_timing(struct dw_mci *host, u32 timing)
13580113132SSeungwon Jeon {
13680113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
13780113132SSeungwon Jeon 	u32 clksel;
13880113132SSeungwon Jeon 
13980113132SSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
14080113132SSeungwon Jeon 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
14180113132SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL64);
14280113132SSeungwon Jeon 	else
14380113132SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
14480113132SSeungwon Jeon 
14580113132SSeungwon Jeon 	clksel = (clksel & ~SDMMC_CLKSEL_TIMING_MASK) | timing;
14680113132SSeungwon Jeon 
14780113132SSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
14880113132SSeungwon Jeon 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
14980113132SSeungwon Jeon 		mci_writel(host, CLKSEL64, clksel);
15080113132SSeungwon Jeon 	else
15180113132SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
152aaaaeb7aSJaehoon Chung 
153aaaaeb7aSJaehoon Chung 	/*
154aaaaeb7aSJaehoon Chung 	 * Exynos4412 and Exynos5250 extends the use of CMD register with the
155aaaaeb7aSJaehoon Chung 	 * use of bit 29 (which is reserved on standard MSHC controllers) for
156aaaaeb7aSJaehoon Chung 	 * optionally bypassing the HOLD register for command and data. The
157aaaaeb7aSJaehoon Chung 	 * HOLD register should be bypassed in case there is no phase shift
158aaaaeb7aSJaehoon Chung 	 * applied on CMD/DATA that is sent to the card.
159aaaaeb7aSJaehoon Chung 	 */
160e5a61353SJaehoon Chung 	if (!SDMMC_CLKSEL_GET_DRV_WD3(clksel) && host->cur_slot)
161aaaaeb7aSJaehoon Chung 		set_bit(DW_MMC_CARD_NO_USE_HOLD, &host->cur_slot->flags);
16280113132SSeungwon Jeon }
16380113132SSeungwon Jeon 
164e2c63599SDoug Anderson #ifdef CONFIG_PM_SLEEP
165e2c63599SDoug Anderson static int dw_mci_exynos_suspend(struct device *dev)
166e2c63599SDoug Anderson {
167e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
168e2c63599SDoug Anderson 
169e2c63599SDoug Anderson 	return dw_mci_suspend(host);
170e2c63599SDoug Anderson }
171e2c63599SDoug Anderson 
172e2c63599SDoug Anderson static int dw_mci_exynos_resume(struct device *dev)
173e2c63599SDoug Anderson {
174e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
175e2c63599SDoug Anderson 
1765659eeadSJaehoon Chung 	dw_mci_exynos_config_smu(host);
177e2c63599SDoug Anderson 	return dw_mci_resume(host);
178e2c63599SDoug Anderson }
179e2c63599SDoug Anderson 
180e2c63599SDoug Anderson /**
181e2c63599SDoug Anderson  * dw_mci_exynos_resume_noirq - Exynos-specific resume code
182e2c63599SDoug Anderson  *
183e2c63599SDoug Anderson  * On exynos5420 there is a silicon errata that will sometimes leave the
184e2c63599SDoug Anderson  * WAKEUP_INT bit in the CLKSEL register asserted.  This bit is 1 to indicate
185e2c63599SDoug Anderson  * that it fired and we can clear it by writing a 1 back.  Clear it to prevent
186e2c63599SDoug Anderson  * interrupts from going off constantly.
187e2c63599SDoug Anderson  *
188e2c63599SDoug Anderson  * We run this code on all exynos variants because it doesn't hurt.
189e2c63599SDoug Anderson  */
190e2c63599SDoug Anderson 
191e2c63599SDoug Anderson static int dw_mci_exynos_resume_noirq(struct device *dev)
192e2c63599SDoug Anderson {
193e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
19489ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
195e2c63599SDoug Anderson 	u32 clksel;
196e2c63599SDoug Anderson 
19789ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
19889ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
19989ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
20089ad2be7SAbhilash Kesavan 	else
201e2c63599SDoug Anderson 		clksel = mci_readl(host, CLKSEL);
20289ad2be7SAbhilash Kesavan 
20389ad2be7SAbhilash Kesavan 	if (clksel & SDMMC_CLKSEL_WAKEUP_INT) {
20489ad2be7SAbhilash Kesavan 		if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
20589ad2be7SAbhilash Kesavan 			priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
20689ad2be7SAbhilash Kesavan 			mci_writel(host, CLKSEL64, clksel);
20789ad2be7SAbhilash Kesavan 		else
208e2c63599SDoug Anderson 			mci_writel(host, CLKSEL, clksel);
20989ad2be7SAbhilash Kesavan 	}
210e2c63599SDoug Anderson 
211e2c63599SDoug Anderson 	return 0;
212e2c63599SDoug Anderson }
213e2c63599SDoug Anderson #else
214e2c63599SDoug Anderson #define dw_mci_exynos_suspend		NULL
215e2c63599SDoug Anderson #define dw_mci_exynos_resume		NULL
216e2c63599SDoug Anderson #define dw_mci_exynos_resume_noirq	NULL
217e2c63599SDoug Anderson #endif /* CONFIG_PM_SLEEP */
218e2c63599SDoug Anderson 
21980113132SSeungwon Jeon static void dw_mci_exynos_config_hs400(struct dw_mci *host, u32 timing)
220c3665006SThomas Abraham {
221c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
22280113132SSeungwon Jeon 	u32 dqs, strobe;
223c3665006SThomas Abraham 
22480113132SSeungwon Jeon 	/*
22580113132SSeungwon Jeon 	 * Not supported to configure register
22680113132SSeungwon Jeon 	 * related to HS400
22780113132SSeungwon Jeon 	 */
228*941a659fSKrzysztof Kozlowski 	if (priv->ctrl_type < DW_MCI_TYPE_EXYNOS5420) {
229*941a659fSKrzysztof Kozlowski 		if (timing == MMC_TIMING_MMC_HS400)
230*941a659fSKrzysztof Kozlowski 			dev_warn(host->dev,
231*941a659fSKrzysztof Kozlowski 				 "cannot configure HS400, unsupported chipset\n");
23280113132SSeungwon Jeon 		return;
233*941a659fSKrzysztof Kozlowski 	}
23480113132SSeungwon Jeon 
23580113132SSeungwon Jeon 	dqs = priv->saved_dqs_en;
23680113132SSeungwon Jeon 	strobe = priv->saved_strobe_ctrl;
23780113132SSeungwon Jeon 
23880113132SSeungwon Jeon 	if (timing == MMC_TIMING_MMC_HS400) {
23980113132SSeungwon Jeon 		dqs |= DATA_STROBE_EN;
24080113132SSeungwon Jeon 		strobe = DQS_CTRL_RD_DELAY(strobe, priv->dqs_delay);
241c6d9dedaSSeungwon Jeon 	} else {
24280113132SSeungwon Jeon 		dqs &= ~DATA_STROBE_EN;
243c3665006SThomas Abraham 	}
244c3665006SThomas Abraham 
24580113132SSeungwon Jeon 	mci_writel(host, HS400_DQS_EN, dqs);
24680113132SSeungwon Jeon 	mci_writel(host, HS400_DLINE_CTRL, strobe);
24780113132SSeungwon Jeon }
24880113132SSeungwon Jeon 
24980113132SSeungwon Jeon static void dw_mci_exynos_adjust_clock(struct dw_mci *host, unsigned int wanted)
25080113132SSeungwon Jeon {
25180113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
25280113132SSeungwon Jeon 	unsigned long actual;
25380113132SSeungwon Jeon 	u8 div;
25480113132SSeungwon Jeon 	int ret;
255a2a1fed8SSeungwon Jeon 	/*
256a2a1fed8SSeungwon Jeon 	 * Don't care if wanted clock is zero or
257a2a1fed8SSeungwon Jeon 	 * ciu clock is unavailable
258a2a1fed8SSeungwon Jeon 	 */
259a2a1fed8SSeungwon Jeon 	if (!wanted || IS_ERR(host->ciu_clk))
260c6d9dedaSSeungwon Jeon 		return;
261c6d9dedaSSeungwon Jeon 
262c6d9dedaSSeungwon Jeon 	/* Guaranteed minimum frequency for cclkin */
263c6d9dedaSSeungwon Jeon 	if (wanted < EXYNOS_CCLKIN_MIN)
264c6d9dedaSSeungwon Jeon 		wanted = EXYNOS_CCLKIN_MIN;
265c6d9dedaSSeungwon Jeon 
26680113132SSeungwon Jeon 	if (wanted == priv->cur_speed)
26780113132SSeungwon Jeon 		return;
26880113132SSeungwon Jeon 
26980113132SSeungwon Jeon 	div = dw_mci_exynos_get_ciu_div(host);
27080113132SSeungwon Jeon 	ret = clk_set_rate(host->ciu_clk, wanted * div);
271c6d9dedaSSeungwon Jeon 	if (ret)
272c6d9dedaSSeungwon Jeon 		dev_warn(host->dev,
273c6d9dedaSSeungwon Jeon 			"failed to set clk-rate %u error: %d\n",
274c6d9dedaSSeungwon Jeon 			wanted * div, ret);
275c6d9dedaSSeungwon Jeon 	actual = clk_get_rate(host->ciu_clk);
276c6d9dedaSSeungwon Jeon 	host->bus_hz = actual / div;
277c6d9dedaSSeungwon Jeon 	priv->cur_speed = wanted;
278c6d9dedaSSeungwon Jeon 	host->current_speed = 0;
279c6d9dedaSSeungwon Jeon }
28080113132SSeungwon Jeon 
28180113132SSeungwon Jeon static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
28280113132SSeungwon Jeon {
28380113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
28480113132SSeungwon Jeon 	unsigned int wanted = ios->clock;
28580113132SSeungwon Jeon 	u32 timing = ios->timing, clksel;
28680113132SSeungwon Jeon 
28780113132SSeungwon Jeon 	switch (timing) {
28880113132SSeungwon Jeon 	case MMC_TIMING_MMC_HS400:
28980113132SSeungwon Jeon 		/* Update tuned sample timing */
29080113132SSeungwon Jeon 		clksel = SDMMC_CLKSEL_UP_SAMPLE(
29180113132SSeungwon Jeon 				priv->hs400_timing, priv->tuned_sample);
29280113132SSeungwon Jeon 		wanted <<= 1;
29380113132SSeungwon Jeon 		break;
29480113132SSeungwon Jeon 	case MMC_TIMING_MMC_DDR52:
29580113132SSeungwon Jeon 		clksel = priv->ddr_timing;
29680113132SSeungwon Jeon 		/* Should be double rate for DDR mode */
29780113132SSeungwon Jeon 		if (ios->bus_width == MMC_BUS_WIDTH_8)
29880113132SSeungwon Jeon 			wanted <<= 1;
29980113132SSeungwon Jeon 		break;
30080113132SSeungwon Jeon 	default:
30180113132SSeungwon Jeon 		clksel = priv->sdr_timing;
30280113132SSeungwon Jeon 	}
30380113132SSeungwon Jeon 
30480113132SSeungwon Jeon 	/* Set clock timing for the requested speed mode*/
30580113132SSeungwon Jeon 	dw_mci_exynos_set_clksel_timing(host, clksel);
30680113132SSeungwon Jeon 
30780113132SSeungwon Jeon 	/* Configure setting for HS400 */
30880113132SSeungwon Jeon 	dw_mci_exynos_config_hs400(host, timing);
30980113132SSeungwon Jeon 
31080113132SSeungwon Jeon 	/* Configure clock rate */
31180113132SSeungwon Jeon 	dw_mci_exynos_adjust_clock(host, wanted);
312c6d9dedaSSeungwon Jeon }
313c6d9dedaSSeungwon Jeon 
314c3665006SThomas Abraham static int dw_mci_exynos_parse_dt(struct dw_mci *host)
315c3665006SThomas Abraham {
316e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv;
317c3665006SThomas Abraham 	struct device_node *np = host->dev->of_node;
318c3665006SThomas Abraham 	u32 timing[2];
319c3665006SThomas Abraham 	u32 div = 0;
320e6c784edSYuvaraj Kumar C D 	int idx;
321c3665006SThomas Abraham 	int ret;
322c3665006SThomas Abraham 
323e6c784edSYuvaraj Kumar C D 	priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
324bf3707eaSBeomho Seo 	if (!priv)
325e6c784edSYuvaraj Kumar C D 		return -ENOMEM;
326e6c784edSYuvaraj Kumar C D 
327e6c784edSYuvaraj Kumar C D 	for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
328e6c784edSYuvaraj Kumar C D 		if (of_device_is_compatible(np, exynos_compat[idx].compatible))
329e6c784edSYuvaraj Kumar C D 			priv->ctrl_type = exynos_compat[idx].ctrl_type;
330e6c784edSYuvaraj Kumar C D 	}
331e6c784edSYuvaraj Kumar C D 
332c6d9dedaSSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
333c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1;
334c6d9dedaSSeungwon Jeon 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
335c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4210_FIXED_CIU_CLK_DIV - 1;
336c6d9dedaSSeungwon Jeon 	else {
337c3665006SThomas Abraham 		of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
338c3665006SThomas Abraham 		priv->ciu_div = div;
339c6d9dedaSSeungwon Jeon 	}
340c3665006SThomas Abraham 
341c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
342c3665006SThomas Abraham 			"samsung,dw-mshc-sdr-timing", timing, 2);
343c3665006SThomas Abraham 	if (ret)
344c3665006SThomas Abraham 		return ret;
345c3665006SThomas Abraham 
3462d9f0bd1SYuvaraj Kumar C D 	priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
3472d9f0bd1SYuvaraj Kumar C D 
348c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
349c3665006SThomas Abraham 			"samsung,dw-mshc-ddr-timing", timing, 2);
350c3665006SThomas Abraham 	if (ret)
351c3665006SThomas Abraham 		return ret;
352c3665006SThomas Abraham 
353c3665006SThomas Abraham 	priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
35480113132SSeungwon Jeon 
35580113132SSeungwon Jeon 	ret = of_property_read_u32_array(np,
35680113132SSeungwon Jeon 			"samsung,dw-mshc-hs400-timing", timing, 2);
35780113132SSeungwon Jeon 	if (!ret && of_property_read_u32(np,
35880113132SSeungwon Jeon 				"samsung,read-strobe-delay", &priv->dqs_delay))
35980113132SSeungwon Jeon 		dev_dbg(host->dev,
36080113132SSeungwon Jeon 			"read-strobe-delay is not found, assuming usage of default value\n");
36180113132SSeungwon Jeon 
36280113132SSeungwon Jeon 	priv->hs400_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1],
36380113132SSeungwon Jeon 						HS400_FIXED_CIU_CLK_DIV);
364e6c784edSYuvaraj Kumar C D 	host->priv = priv;
365c3665006SThomas Abraham 	return 0;
366c3665006SThomas Abraham }
367c3665006SThomas Abraham 
368c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_get_clksmpl(struct dw_mci *host)
369c537a1c5SSeungwon Jeon {
37089ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
37189ad2be7SAbhilash Kesavan 
37289ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
37389ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
37489ad2be7SAbhilash Kesavan 		return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL64));
37589ad2be7SAbhilash Kesavan 	else
376c537a1c5SSeungwon Jeon 		return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL));
377c537a1c5SSeungwon Jeon }
378c537a1c5SSeungwon Jeon 
379c537a1c5SSeungwon Jeon static inline void dw_mci_exynos_set_clksmpl(struct dw_mci *host, u8 sample)
380c537a1c5SSeungwon Jeon {
381c537a1c5SSeungwon Jeon 	u32 clksel;
38289ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
38389ad2be7SAbhilash Kesavan 
38489ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
38589ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
38689ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
38789ad2be7SAbhilash Kesavan 	else
388c537a1c5SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
38980113132SSeungwon Jeon 	clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
39089ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
39189ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
39289ad2be7SAbhilash Kesavan 		mci_writel(host, CLKSEL64, clksel);
39389ad2be7SAbhilash Kesavan 	else
394c537a1c5SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
395c537a1c5SSeungwon Jeon }
396c537a1c5SSeungwon Jeon 
397c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_move_next_clksmpl(struct dw_mci *host)
398c537a1c5SSeungwon Jeon {
39989ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
400c537a1c5SSeungwon Jeon 	u32 clksel;
401c537a1c5SSeungwon Jeon 	u8 sample;
402c537a1c5SSeungwon Jeon 
40389ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
40489ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
40589ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
40689ad2be7SAbhilash Kesavan 	else
407c537a1c5SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
40880113132SSeungwon Jeon 
409c537a1c5SSeungwon Jeon 	sample = (clksel + 1) & 0x7;
41080113132SSeungwon Jeon 	clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
41180113132SSeungwon Jeon 
41289ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
41389ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
41489ad2be7SAbhilash Kesavan 		mci_writel(host, CLKSEL64, clksel);
41589ad2be7SAbhilash Kesavan 	else
416c537a1c5SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
41780113132SSeungwon Jeon 
418c537a1c5SSeungwon Jeon 	return sample;
419c537a1c5SSeungwon Jeon }
420c537a1c5SSeungwon Jeon 
421c537a1c5SSeungwon Jeon static s8 dw_mci_exynos_get_best_clksmpl(u8 candiates)
422c537a1c5SSeungwon Jeon {
423c537a1c5SSeungwon Jeon 	const u8 iter = 8;
424c537a1c5SSeungwon Jeon 	u8 __c;
425c537a1c5SSeungwon Jeon 	s8 i, loc = -1;
426c537a1c5SSeungwon Jeon 
427c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
428c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
429c537a1c5SSeungwon Jeon 		if ((__c & 0xc7) == 0xc7) {
430c537a1c5SSeungwon Jeon 			loc = i;
431c537a1c5SSeungwon Jeon 			goto out;
432c537a1c5SSeungwon Jeon 		}
433c537a1c5SSeungwon Jeon 	}
434c537a1c5SSeungwon Jeon 
435c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
436c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
437c537a1c5SSeungwon Jeon 		if ((__c & 0x83) == 0x83) {
438c537a1c5SSeungwon Jeon 			loc = i;
439c537a1c5SSeungwon Jeon 			goto out;
440c537a1c5SSeungwon Jeon 		}
441c537a1c5SSeungwon Jeon 	}
442c537a1c5SSeungwon Jeon 
443c537a1c5SSeungwon Jeon out:
444c537a1c5SSeungwon Jeon 	return loc;
445c537a1c5SSeungwon Jeon }
446c537a1c5SSeungwon Jeon 
4479979dbe5SChaotian Jing static int dw_mci_exynos_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
448c537a1c5SSeungwon Jeon {
449c537a1c5SSeungwon Jeon 	struct dw_mci *host = slot->host;
45080113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
451c537a1c5SSeungwon Jeon 	struct mmc_host *mmc = slot->mmc;
452c537a1c5SSeungwon Jeon 	u8 start_smpl, smpl, candiates = 0;
453c537a1c5SSeungwon Jeon 	s8 found = -1;
454c537a1c5SSeungwon Jeon 	int ret = 0;
455c537a1c5SSeungwon Jeon 
456c537a1c5SSeungwon Jeon 	start_smpl = dw_mci_exynos_get_clksmpl(host);
457c537a1c5SSeungwon Jeon 
458c537a1c5SSeungwon Jeon 	do {
459c537a1c5SSeungwon Jeon 		mci_writel(host, TMOUT, ~0);
460c537a1c5SSeungwon Jeon 		smpl = dw_mci_exynos_move_next_clksmpl(host);
461c537a1c5SSeungwon Jeon 
4629979dbe5SChaotian Jing 		if (!mmc_send_tuning(mmc, opcode, NULL))
463c537a1c5SSeungwon Jeon 			candiates |= (1 << smpl);
4646c2c6506SUlf Hansson 
465c537a1c5SSeungwon Jeon 	} while (start_smpl != smpl);
466c537a1c5SSeungwon Jeon 
467c537a1c5SSeungwon Jeon 	found = dw_mci_exynos_get_best_clksmpl(candiates);
46880113132SSeungwon Jeon 	if (found >= 0) {
469c537a1c5SSeungwon Jeon 		dw_mci_exynos_set_clksmpl(host, found);
47080113132SSeungwon Jeon 		priv->tuned_sample = found;
47180113132SSeungwon Jeon 	} else {
472c537a1c5SSeungwon Jeon 		ret = -EIO;
47380113132SSeungwon Jeon 	}
474c537a1c5SSeungwon Jeon 
475c537a1c5SSeungwon Jeon 	return ret;
476c537a1c5SSeungwon Jeon }
477c537a1c5SSeungwon Jeon 
478c22f5e1bSWu Fengguang static int dw_mci_exynos_prepare_hs400_tuning(struct dw_mci *host,
47980113132SSeungwon Jeon 					struct mmc_ios *ios)
48080113132SSeungwon Jeon {
48180113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
48280113132SSeungwon Jeon 
48380113132SSeungwon Jeon 	dw_mci_exynos_set_clksel_timing(host, priv->hs400_timing);
48480113132SSeungwon Jeon 	dw_mci_exynos_adjust_clock(host, (ios->clock) << 1);
48580113132SSeungwon Jeon 
48680113132SSeungwon Jeon 	return 0;
48780113132SSeungwon Jeon }
48880113132SSeungwon Jeon 
4890f6e73d0SDongjin Kim /* Common capabilities of Exynos4/Exynos5 SoC */
4900f6e73d0SDongjin Kim static unsigned long exynos_dwmmc_caps[4] = {
491cab3a802SSeungwon Jeon 	MMC_CAP_1_8V_DDR | MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
492c3665006SThomas Abraham 	MMC_CAP_CMD23,
493c3665006SThomas Abraham 	MMC_CAP_CMD23,
494c3665006SThomas Abraham 	MMC_CAP_CMD23,
495c3665006SThomas Abraham };
496c3665006SThomas Abraham 
4970f6e73d0SDongjin Kim static const struct dw_mci_drv_data exynos_drv_data = {
4980f6e73d0SDongjin Kim 	.caps			= exynos_dwmmc_caps,
499c3665006SThomas Abraham 	.init			= dw_mci_exynos_priv_init,
500c3665006SThomas Abraham 	.set_ios		= dw_mci_exynos_set_ios,
501c3665006SThomas Abraham 	.parse_dt		= dw_mci_exynos_parse_dt,
502c537a1c5SSeungwon Jeon 	.execute_tuning		= dw_mci_exynos_execute_tuning,
50380113132SSeungwon Jeon 	.prepare_hs400_tuning	= dw_mci_exynos_prepare_hs400_tuning,
504c3665006SThomas Abraham };
505c3665006SThomas Abraham 
506c3665006SThomas Abraham static const struct of_device_id dw_mci_exynos_match[] = {
5070f6e73d0SDongjin Kim 	{ .compatible = "samsung,exynos4412-dw-mshc",
5080f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
509c3665006SThomas Abraham 	{ .compatible = "samsung,exynos5250-dw-mshc",
5100f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
51100fd041bSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc",
51200fd041bSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
5136bce431cSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc-smu",
5146bce431cSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
51589ad2be7SAbhilash Kesavan 	{ .compatible = "samsung,exynos7-dw-mshc",
51689ad2be7SAbhilash Kesavan 			.data = &exynos_drv_data, },
51789ad2be7SAbhilash Kesavan 	{ .compatible = "samsung,exynos7-dw-mshc-smu",
51889ad2be7SAbhilash Kesavan 			.data = &exynos_drv_data, },
519c3665006SThomas Abraham 	{},
520c3665006SThomas Abraham };
521517cb9f1SArnd Bergmann MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
522c3665006SThomas Abraham 
5239665f7f2SSachin Kamat static int dw_mci_exynos_probe(struct platform_device *pdev)
524c3665006SThomas Abraham {
5258e2b36eaSArnd Bergmann 	const struct dw_mci_drv_data *drv_data;
526c3665006SThomas Abraham 	const struct of_device_id *match;
527c3665006SThomas Abraham 
528c3665006SThomas Abraham 	match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
529c3665006SThomas Abraham 	drv_data = match->data;
530c3665006SThomas Abraham 	return dw_mci_pltfm_register(pdev, drv_data);
531c3665006SThomas Abraham }
532c3665006SThomas Abraham 
53315a2e2abSSachin Kamat static const struct dev_pm_ops dw_mci_exynos_pmops = {
534e2c63599SDoug Anderson 	SET_SYSTEM_SLEEP_PM_OPS(dw_mci_exynos_suspend, dw_mci_exynos_resume)
535e2c63599SDoug Anderson 	.resume_noirq = dw_mci_exynos_resume_noirq,
536e2c63599SDoug Anderson 	.thaw_noirq = dw_mci_exynos_resume_noirq,
537e2c63599SDoug Anderson 	.restore_noirq = dw_mci_exynos_resume_noirq,
538e2c63599SDoug Anderson };
539e2c63599SDoug Anderson 
540c3665006SThomas Abraham static struct platform_driver dw_mci_exynos_pltfm_driver = {
541c3665006SThomas Abraham 	.probe		= dw_mci_exynos_probe,
5427d589edcSDmitry Torokhov 	.remove		= dw_mci_pltfm_remove,
543c3665006SThomas Abraham 	.driver		= {
544c3665006SThomas Abraham 		.name		= "dwmmc_exynos",
54520183d50SSachin Kamat 		.of_match_table	= dw_mci_exynos_match,
546e2c63599SDoug Anderson 		.pm		= &dw_mci_exynos_pmops,
547c3665006SThomas Abraham 	},
548c3665006SThomas Abraham };
549c3665006SThomas Abraham 
550c3665006SThomas Abraham module_platform_driver(dw_mci_exynos_pltfm_driver);
551c3665006SThomas Abraham 
552c3665006SThomas Abraham MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
553c3665006SThomas Abraham MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
554c3665006SThomas Abraham MODULE_LICENSE("GPL v2");
5552fc546fdSZhangfei Gao MODULE_ALIAS("platform:dwmmc_exynos");
556