xref: /openbmc/linux/drivers/mmc/host/dw_mmc-exynos.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c3665006SThomas Abraham /*
3c3665006SThomas Abraham  * Exynos Specific Extensions for Synopsys DW Multimedia Card Interface driver
4c3665006SThomas Abraham  *
5c3665006SThomas Abraham  * Copyright (C) 2012, Samsung Electronics Co., Ltd.
6c3665006SThomas Abraham  */
7c3665006SThomas Abraham 
8c3665006SThomas Abraham #include <linux/module.h>
9c3665006SThomas Abraham #include <linux/platform_device.h>
10c3665006SThomas Abraham #include <linux/clk.h>
11c3665006SThomas Abraham #include <linux/mmc/host.h>
12c537a1c5SSeungwon Jeon #include <linux/mmc/mmc.h>
13c3665006SThomas Abraham #include <linux/of.h>
14c3665006SThomas Abraham #include <linux/of_gpio.h>
15cf5237efSShawn Lin #include <linux/pm_runtime.h>
16c537a1c5SSeungwon Jeon #include <linux/slab.h>
17c3665006SThomas Abraham 
18c3665006SThomas Abraham #include "dw_mmc.h"
19c3665006SThomas Abraham #include "dw_mmc-pltfm.h"
200b5fce48SSeungwon Jeon #include "dw_mmc-exynos.h"
21c6d9dedaSSeungwon Jeon 
22c3665006SThomas Abraham /* Variations in Exynos specific dw-mshc controller */
23c3665006SThomas Abraham enum dw_mci_exynos_type {
24c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4210,
25c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4412,
26c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS5250,
2700fd041bSYuvaraj Kumar C D 	DW_MCI_TYPE_EXYNOS5420,
286bce431cSYuvaraj Kumar C D 	DW_MCI_TYPE_EXYNOS5420_SMU,
2989ad2be7SAbhilash Kesavan 	DW_MCI_TYPE_EXYNOS7,
3089ad2be7SAbhilash Kesavan 	DW_MCI_TYPE_EXYNOS7_SMU,
31c3665006SThomas Abraham };
32c3665006SThomas Abraham 
33c3665006SThomas Abraham /* Exynos implementation specific driver private data */
34c3665006SThomas Abraham struct dw_mci_exynos_priv_data {
35c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
36c3665006SThomas Abraham 	u8				ciu_div;
37c3665006SThomas Abraham 	u32				sdr_timing;
38c3665006SThomas Abraham 	u32				ddr_timing;
3980113132SSeungwon Jeon 	u32				hs400_timing;
4080113132SSeungwon Jeon 	u32				tuned_sample;
41c6d9dedaSSeungwon Jeon 	u32				cur_speed;
4280113132SSeungwon Jeon 	u32				dqs_delay;
4380113132SSeungwon Jeon 	u32				saved_dqs_en;
4480113132SSeungwon Jeon 	u32				saved_strobe_ctrl;
45c3665006SThomas Abraham };
46c3665006SThomas Abraham 
47c3665006SThomas Abraham static struct dw_mci_exynos_compatible {
48c3665006SThomas Abraham 	char				*compatible;
49c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
50c3665006SThomas Abraham } exynos_compat[] = {
51c3665006SThomas Abraham 	{
52c3665006SThomas Abraham 		.compatible	= "samsung,exynos4210-dw-mshc",
53c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4210,
54c3665006SThomas Abraham 	}, {
55c3665006SThomas Abraham 		.compatible	= "samsung,exynos4412-dw-mshc",
56c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4412,
57c3665006SThomas Abraham 	}, {
58c3665006SThomas Abraham 		.compatible	= "samsung,exynos5250-dw-mshc",
59c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5250,
6000fd041bSYuvaraj Kumar C D 	}, {
6100fd041bSYuvaraj Kumar C D 		.compatible	= "samsung,exynos5420-dw-mshc",
6200fd041bSYuvaraj Kumar C D 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5420,
636bce431cSYuvaraj Kumar C D 	}, {
646bce431cSYuvaraj Kumar C D 		.compatible	= "samsung,exynos5420-dw-mshc-smu",
656bce431cSYuvaraj Kumar C D 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5420_SMU,
6689ad2be7SAbhilash Kesavan 	}, {
6789ad2be7SAbhilash Kesavan 		.compatible	= "samsung,exynos7-dw-mshc",
6889ad2be7SAbhilash Kesavan 		.ctrl_type	= DW_MCI_TYPE_EXYNOS7,
6989ad2be7SAbhilash Kesavan 	}, {
7089ad2be7SAbhilash Kesavan 		.compatible	= "samsung,exynos7-dw-mshc-smu",
7189ad2be7SAbhilash Kesavan 		.ctrl_type	= DW_MCI_TYPE_EXYNOS7_SMU,
72c3665006SThomas Abraham 	},
73c3665006SThomas Abraham };
74c3665006SThomas Abraham 
7580113132SSeungwon Jeon static inline u8 dw_mci_exynos_get_ciu_div(struct dw_mci *host)
7680113132SSeungwon Jeon {
7780113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
7880113132SSeungwon Jeon 
7980113132SSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
8080113132SSeungwon Jeon 		return EXYNOS4412_FIXED_CIU_CLK_DIV;
8180113132SSeungwon Jeon 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
8280113132SSeungwon Jeon 		return EXYNOS4210_FIXED_CIU_CLK_DIV;
8380113132SSeungwon Jeon 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
8480113132SSeungwon Jeon 			priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
8580113132SSeungwon Jeon 		return SDMMC_CLKSEL_GET_DIV(mci_readl(host, CLKSEL64)) + 1;
8680113132SSeungwon Jeon 	else
8780113132SSeungwon Jeon 		return SDMMC_CLKSEL_GET_DIV(mci_readl(host, CLKSEL)) + 1;
8880113132SSeungwon Jeon }
8980113132SSeungwon Jeon 
905659eeadSJaehoon Chung static void dw_mci_exynos_config_smu(struct dw_mci *host)
91c3665006SThomas Abraham {
92e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv = host->priv;
93c3665006SThomas Abraham 
945659eeadSJaehoon Chung 	/*
955659eeadSJaehoon Chung 	 * If Exynos is provided the Security management,
965659eeadSJaehoon Chung 	 * set for non-ecryption mode at this time.
975659eeadSJaehoon Chung 	 */
9889ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU ||
9989ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU) {
1006bce431cSYuvaraj Kumar C D 		mci_writel(host, MPSBEGIN0, 0);
1010b5fce48SSeungwon Jeon 		mci_writel(host, MPSEND0, SDMMC_ENDING_SEC_NR_MAX);
1020b5fce48SSeungwon Jeon 		mci_writel(host, MPSCTRL0, SDMMC_MPSCTRL_SECURE_WRITE_BIT |
1030b5fce48SSeungwon Jeon 			   SDMMC_MPSCTRL_NON_SECURE_READ_BIT |
1040b5fce48SSeungwon Jeon 			   SDMMC_MPSCTRL_VALID |
1050b5fce48SSeungwon Jeon 			   SDMMC_MPSCTRL_NON_SECURE_WRITE_BIT);
1066bce431cSYuvaraj Kumar C D 	}
1075659eeadSJaehoon Chung }
1085659eeadSJaehoon Chung 
1095659eeadSJaehoon Chung static int dw_mci_exynos_priv_init(struct dw_mci *host)
1105659eeadSJaehoon Chung {
1115659eeadSJaehoon Chung 	struct dw_mci_exynos_priv_data *priv = host->priv;
1125659eeadSJaehoon Chung 
1135659eeadSJaehoon Chung 	dw_mci_exynos_config_smu(host);
1146bce431cSYuvaraj Kumar C D 
11580113132SSeungwon Jeon 	if (priv->ctrl_type >= DW_MCI_TYPE_EXYNOS5420) {
11680113132SSeungwon Jeon 		priv->saved_strobe_ctrl = mci_readl(host, HS400_DLINE_CTRL);
11780113132SSeungwon Jeon 		priv->saved_dqs_en = mci_readl(host, HS400_DQS_EN);
11880113132SSeungwon Jeon 		priv->saved_dqs_en |= AXI_NON_BLOCKING_WR;
11980113132SSeungwon Jeon 		mci_writel(host, HS400_DQS_EN, priv->saved_dqs_en);
12080113132SSeungwon Jeon 		if (!priv->dqs_delay)
12180113132SSeungwon Jeon 			priv->dqs_delay =
12280113132SSeungwon Jeon 				DQS_CTRL_GET_RD_DELAY(priv->saved_strobe_ctrl);
12380113132SSeungwon Jeon 	}
12480113132SSeungwon Jeon 
125a2a1fed8SSeungwon Jeon 	host->bus_hz /= (priv->ciu_div + 1);
126a2a1fed8SSeungwon Jeon 
127c3665006SThomas Abraham 	return 0;
128c3665006SThomas Abraham }
129c3665006SThomas Abraham 
13080113132SSeungwon Jeon static void dw_mci_exynos_set_clksel_timing(struct dw_mci *host, u32 timing)
13180113132SSeungwon Jeon {
13280113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
13380113132SSeungwon Jeon 	u32 clksel;
13480113132SSeungwon Jeon 
13580113132SSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
13680113132SSeungwon Jeon 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
13780113132SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL64);
13880113132SSeungwon Jeon 	else
13980113132SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
14080113132SSeungwon Jeon 
14180113132SSeungwon Jeon 	clksel = (clksel & ~SDMMC_CLKSEL_TIMING_MASK) | timing;
14280113132SSeungwon Jeon 
14380113132SSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
14480113132SSeungwon Jeon 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
14580113132SSeungwon Jeon 		mci_writel(host, CLKSEL64, clksel);
14680113132SSeungwon Jeon 	else
14780113132SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
148aaaaeb7aSJaehoon Chung 
149aaaaeb7aSJaehoon Chung 	/*
150aaaaeb7aSJaehoon Chung 	 * Exynos4412 and Exynos5250 extends the use of CMD register with the
151aaaaeb7aSJaehoon Chung 	 * use of bit 29 (which is reserved on standard MSHC controllers) for
152aaaaeb7aSJaehoon Chung 	 * optionally bypassing the HOLD register for command and data. The
153aaaaeb7aSJaehoon Chung 	 * HOLD register should be bypassed in case there is no phase shift
154aaaaeb7aSJaehoon Chung 	 * applied on CMD/DATA that is sent to the card.
155aaaaeb7aSJaehoon Chung 	 */
15642f989c0SJaehoon Chung 	if (!SDMMC_CLKSEL_GET_DRV_WD3(clksel) && host->slot)
15742f989c0SJaehoon Chung 		set_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags);
15880113132SSeungwon Jeon }
15980113132SSeungwon Jeon 
160cf5237efSShawn Lin #ifdef CONFIG_PM
161cf5237efSShawn Lin static int dw_mci_exynos_runtime_resume(struct device *dev)
162e2c63599SDoug Anderson {
163e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
164e22842ddSJaehoon Chung 	int ret;
165e22842ddSJaehoon Chung 
166e22842ddSJaehoon Chung 	ret = dw_mci_runtime_resume(dev);
167e22842ddSJaehoon Chung 	if (ret)
168e22842ddSJaehoon Chung 		return ret;
169e2c63599SDoug Anderson 
1705659eeadSJaehoon Chung 	dw_mci_exynos_config_smu(host);
171e22842ddSJaehoon Chung 
172e22842ddSJaehoon Chung 	return ret;
173e2c63599SDoug Anderson }
174ecf7c7c5SMarek Szyprowski #endif /* CONFIG_PM */
175ecf7c7c5SMarek Szyprowski 
176ecf7c7c5SMarek Szyprowski #ifdef CONFIG_PM_SLEEP
177ecf7c7c5SMarek Szyprowski /**
178ecf7c7c5SMarek Szyprowski  * dw_mci_exynos_suspend_noirq - Exynos-specific suspend code
179ecf7c7c5SMarek Szyprowski  *
180ecf7c7c5SMarek Szyprowski  * This ensures that device will be in runtime active state in
181ecf7c7c5SMarek Szyprowski  * dw_mci_exynos_resume_noirq after calling pm_runtime_force_resume()
182ecf7c7c5SMarek Szyprowski  */
183ecf7c7c5SMarek Szyprowski static int dw_mci_exynos_suspend_noirq(struct device *dev)
184ecf7c7c5SMarek Szyprowski {
185ecf7c7c5SMarek Szyprowski 	pm_runtime_get_noresume(dev);
186ecf7c7c5SMarek Szyprowski 	return pm_runtime_force_suspend(dev);
187ecf7c7c5SMarek Szyprowski }
188e2c63599SDoug Anderson 
189e2c63599SDoug Anderson /**
190e2c63599SDoug Anderson  * dw_mci_exynos_resume_noirq - Exynos-specific resume code
191e2c63599SDoug Anderson  *
192e2c63599SDoug Anderson  * On exynos5420 there is a silicon errata that will sometimes leave the
193e2c63599SDoug Anderson  * WAKEUP_INT bit in the CLKSEL register asserted.  This bit is 1 to indicate
194e2c63599SDoug Anderson  * that it fired and we can clear it by writing a 1 back.  Clear it to prevent
195e2c63599SDoug Anderson  * interrupts from going off constantly.
196e2c63599SDoug Anderson  *
197e2c63599SDoug Anderson  * We run this code on all exynos variants because it doesn't hurt.
198e2c63599SDoug Anderson  */
199e2c63599SDoug Anderson static int dw_mci_exynos_resume_noirq(struct device *dev)
200e2c63599SDoug Anderson {
201e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
20289ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
203e2c63599SDoug Anderson 	u32 clksel;
204ecf7c7c5SMarek Szyprowski 	int ret;
205ecf7c7c5SMarek Szyprowski 
206ecf7c7c5SMarek Szyprowski 	ret = pm_runtime_force_resume(dev);
207ecf7c7c5SMarek Szyprowski 	if (ret)
208ecf7c7c5SMarek Szyprowski 		return ret;
209e2c63599SDoug Anderson 
21089ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
21189ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
21289ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
21389ad2be7SAbhilash Kesavan 	else
214e2c63599SDoug Anderson 		clksel = mci_readl(host, CLKSEL);
21589ad2be7SAbhilash Kesavan 
21689ad2be7SAbhilash Kesavan 	if (clksel & SDMMC_CLKSEL_WAKEUP_INT) {
21789ad2be7SAbhilash Kesavan 		if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
21889ad2be7SAbhilash Kesavan 			priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
21989ad2be7SAbhilash Kesavan 			mci_writel(host, CLKSEL64, clksel);
22089ad2be7SAbhilash Kesavan 		else
221e2c63599SDoug Anderson 			mci_writel(host, CLKSEL, clksel);
22289ad2be7SAbhilash Kesavan 	}
223e2c63599SDoug Anderson 
224ecf7c7c5SMarek Szyprowski 	pm_runtime_put(dev);
225ecf7c7c5SMarek Szyprowski 
226e2c63599SDoug Anderson 	return 0;
227e2c63599SDoug Anderson }
228ecf7c7c5SMarek Szyprowski #endif /* CONFIG_PM_SLEEP */
229e2c63599SDoug Anderson 
23080113132SSeungwon Jeon static void dw_mci_exynos_config_hs400(struct dw_mci *host, u32 timing)
231c3665006SThomas Abraham {
232c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
23380113132SSeungwon Jeon 	u32 dqs, strobe;
234c3665006SThomas Abraham 
23580113132SSeungwon Jeon 	/*
23680113132SSeungwon Jeon 	 * Not supported to configure register
23780113132SSeungwon Jeon 	 * related to HS400
23880113132SSeungwon Jeon 	 */
239941a659fSKrzysztof Kozlowski 	if (priv->ctrl_type < DW_MCI_TYPE_EXYNOS5420) {
240941a659fSKrzysztof Kozlowski 		if (timing == MMC_TIMING_MMC_HS400)
241941a659fSKrzysztof Kozlowski 			dev_warn(host->dev,
242941a659fSKrzysztof Kozlowski 				 "cannot configure HS400, unsupported chipset\n");
24380113132SSeungwon Jeon 		return;
244941a659fSKrzysztof Kozlowski 	}
24580113132SSeungwon Jeon 
24680113132SSeungwon Jeon 	dqs = priv->saved_dqs_en;
24780113132SSeungwon Jeon 	strobe = priv->saved_strobe_ctrl;
24880113132SSeungwon Jeon 
24980113132SSeungwon Jeon 	if (timing == MMC_TIMING_MMC_HS400) {
25080113132SSeungwon Jeon 		dqs |= DATA_STROBE_EN;
25180113132SSeungwon Jeon 		strobe = DQS_CTRL_RD_DELAY(strobe, priv->dqs_delay);
25232b64b03SAnand Moon 	} else if (timing == MMC_TIMING_UHS_SDR104) {
25332b64b03SAnand Moon 		dqs &= 0xffffff00;
254c6d9dedaSSeungwon Jeon 	} else {
25580113132SSeungwon Jeon 		dqs &= ~DATA_STROBE_EN;
256c3665006SThomas Abraham 	}
257c3665006SThomas Abraham 
25880113132SSeungwon Jeon 	mci_writel(host, HS400_DQS_EN, dqs);
25980113132SSeungwon Jeon 	mci_writel(host, HS400_DLINE_CTRL, strobe);
26080113132SSeungwon Jeon }
26180113132SSeungwon Jeon 
26280113132SSeungwon Jeon static void dw_mci_exynos_adjust_clock(struct dw_mci *host, unsigned int wanted)
26380113132SSeungwon Jeon {
26480113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
26580113132SSeungwon Jeon 	unsigned long actual;
26680113132SSeungwon Jeon 	u8 div;
26780113132SSeungwon Jeon 	int ret;
268a2a1fed8SSeungwon Jeon 	/*
269a2a1fed8SSeungwon Jeon 	 * Don't care if wanted clock is zero or
270a2a1fed8SSeungwon Jeon 	 * ciu clock is unavailable
271a2a1fed8SSeungwon Jeon 	 */
272a2a1fed8SSeungwon Jeon 	if (!wanted || IS_ERR(host->ciu_clk))
273c6d9dedaSSeungwon Jeon 		return;
274c6d9dedaSSeungwon Jeon 
275c6d9dedaSSeungwon Jeon 	/* Guaranteed minimum frequency for cclkin */
276c6d9dedaSSeungwon Jeon 	if (wanted < EXYNOS_CCLKIN_MIN)
277c6d9dedaSSeungwon Jeon 		wanted = EXYNOS_CCLKIN_MIN;
278c6d9dedaSSeungwon Jeon 
27980113132SSeungwon Jeon 	if (wanted == priv->cur_speed)
28080113132SSeungwon Jeon 		return;
28180113132SSeungwon Jeon 
28280113132SSeungwon Jeon 	div = dw_mci_exynos_get_ciu_div(host);
28380113132SSeungwon Jeon 	ret = clk_set_rate(host->ciu_clk, wanted * div);
284c6d9dedaSSeungwon Jeon 	if (ret)
285c6d9dedaSSeungwon Jeon 		dev_warn(host->dev,
286c6d9dedaSSeungwon Jeon 			"failed to set clk-rate %u error: %d\n",
287c6d9dedaSSeungwon Jeon 			wanted * div, ret);
288c6d9dedaSSeungwon Jeon 	actual = clk_get_rate(host->ciu_clk);
289c6d9dedaSSeungwon Jeon 	host->bus_hz = actual / div;
290c6d9dedaSSeungwon Jeon 	priv->cur_speed = wanted;
291c6d9dedaSSeungwon Jeon 	host->current_speed = 0;
292c6d9dedaSSeungwon Jeon }
29380113132SSeungwon Jeon 
29480113132SSeungwon Jeon static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
29580113132SSeungwon Jeon {
29680113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
29780113132SSeungwon Jeon 	unsigned int wanted = ios->clock;
29880113132SSeungwon Jeon 	u32 timing = ios->timing, clksel;
29980113132SSeungwon Jeon 
30080113132SSeungwon Jeon 	switch (timing) {
30180113132SSeungwon Jeon 	case MMC_TIMING_MMC_HS400:
30280113132SSeungwon Jeon 		/* Update tuned sample timing */
30380113132SSeungwon Jeon 		clksel = SDMMC_CLKSEL_UP_SAMPLE(
30480113132SSeungwon Jeon 				priv->hs400_timing, priv->tuned_sample);
30580113132SSeungwon Jeon 		wanted <<= 1;
30680113132SSeungwon Jeon 		break;
30780113132SSeungwon Jeon 	case MMC_TIMING_MMC_DDR52:
30880113132SSeungwon Jeon 		clksel = priv->ddr_timing;
30980113132SSeungwon Jeon 		/* Should be double rate for DDR mode */
31080113132SSeungwon Jeon 		if (ios->bus_width == MMC_BUS_WIDTH_8)
31180113132SSeungwon Jeon 			wanted <<= 1;
31280113132SSeungwon Jeon 		break;
31332b64b03SAnand Moon 	case MMC_TIMING_UHS_SDR104:
31432b64b03SAnand Moon 	case MMC_TIMING_UHS_SDR50:
31532b64b03SAnand Moon 		clksel = (priv->sdr_timing & 0xfff8ffff) |
31632b64b03SAnand Moon 			(priv->ciu_div << 16);
31732b64b03SAnand Moon 		break;
31832b64b03SAnand Moon 	case MMC_TIMING_UHS_DDR50:
31932b64b03SAnand Moon 		clksel = (priv->ddr_timing & 0xfff8ffff) |
32032b64b03SAnand Moon 			(priv->ciu_div << 16);
32132b64b03SAnand Moon 		break;
32280113132SSeungwon Jeon 	default:
32380113132SSeungwon Jeon 		clksel = priv->sdr_timing;
32480113132SSeungwon Jeon 	}
32580113132SSeungwon Jeon 
32680113132SSeungwon Jeon 	/* Set clock timing for the requested speed mode*/
32780113132SSeungwon Jeon 	dw_mci_exynos_set_clksel_timing(host, clksel);
32880113132SSeungwon Jeon 
32980113132SSeungwon Jeon 	/* Configure setting for HS400 */
33080113132SSeungwon Jeon 	dw_mci_exynos_config_hs400(host, timing);
33180113132SSeungwon Jeon 
33280113132SSeungwon Jeon 	/* Configure clock rate */
33380113132SSeungwon Jeon 	dw_mci_exynos_adjust_clock(host, wanted);
334c6d9dedaSSeungwon Jeon }
335c6d9dedaSSeungwon Jeon 
336c3665006SThomas Abraham static int dw_mci_exynos_parse_dt(struct dw_mci *host)
337c3665006SThomas Abraham {
338e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv;
339c3665006SThomas Abraham 	struct device_node *np = host->dev->of_node;
340c3665006SThomas Abraham 	u32 timing[2];
341c3665006SThomas Abraham 	u32 div = 0;
342e6c784edSYuvaraj Kumar C D 	int idx;
343c3665006SThomas Abraham 	int ret;
344c3665006SThomas Abraham 
345e6c784edSYuvaraj Kumar C D 	priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
346bf3707eaSBeomho Seo 	if (!priv)
347e6c784edSYuvaraj Kumar C D 		return -ENOMEM;
348e6c784edSYuvaraj Kumar C D 
349e6c784edSYuvaraj Kumar C D 	for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
350e6c784edSYuvaraj Kumar C D 		if (of_device_is_compatible(np, exynos_compat[idx].compatible))
351e6c784edSYuvaraj Kumar C D 			priv->ctrl_type = exynos_compat[idx].ctrl_type;
352e6c784edSYuvaraj Kumar C D 	}
353e6c784edSYuvaraj Kumar C D 
354c6d9dedaSSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
355c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1;
356c6d9dedaSSeungwon Jeon 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
357c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4210_FIXED_CIU_CLK_DIV - 1;
358c6d9dedaSSeungwon Jeon 	else {
359c3665006SThomas Abraham 		of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
360c3665006SThomas Abraham 		priv->ciu_div = div;
361c6d9dedaSSeungwon Jeon 	}
362c3665006SThomas Abraham 
363c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
364c3665006SThomas Abraham 			"samsung,dw-mshc-sdr-timing", timing, 2);
365c3665006SThomas Abraham 	if (ret)
366c3665006SThomas Abraham 		return ret;
367c3665006SThomas Abraham 
3682d9f0bd1SYuvaraj Kumar C D 	priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
3692d9f0bd1SYuvaraj Kumar C D 
370c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
371c3665006SThomas Abraham 			"samsung,dw-mshc-ddr-timing", timing, 2);
372c3665006SThomas Abraham 	if (ret)
373c3665006SThomas Abraham 		return ret;
374c3665006SThomas Abraham 
375c3665006SThomas Abraham 	priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
37680113132SSeungwon Jeon 
37780113132SSeungwon Jeon 	ret = of_property_read_u32_array(np,
37880113132SSeungwon Jeon 			"samsung,dw-mshc-hs400-timing", timing, 2);
37980113132SSeungwon Jeon 	if (!ret && of_property_read_u32(np,
38080113132SSeungwon Jeon 				"samsung,read-strobe-delay", &priv->dqs_delay))
38180113132SSeungwon Jeon 		dev_dbg(host->dev,
38280113132SSeungwon Jeon 			"read-strobe-delay is not found, assuming usage of default value\n");
38380113132SSeungwon Jeon 
38480113132SSeungwon Jeon 	priv->hs400_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1],
38580113132SSeungwon Jeon 						HS400_FIXED_CIU_CLK_DIV);
386e6c784edSYuvaraj Kumar C D 	host->priv = priv;
387c3665006SThomas Abraham 	return 0;
388c3665006SThomas Abraham }
389c3665006SThomas Abraham 
390c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_get_clksmpl(struct dw_mci *host)
391c537a1c5SSeungwon Jeon {
39289ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
39389ad2be7SAbhilash Kesavan 
39489ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
39589ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
39689ad2be7SAbhilash Kesavan 		return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL64));
39789ad2be7SAbhilash Kesavan 	else
398c537a1c5SSeungwon Jeon 		return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL));
399c537a1c5SSeungwon Jeon }
400c537a1c5SSeungwon Jeon 
401c537a1c5SSeungwon Jeon static inline void dw_mci_exynos_set_clksmpl(struct dw_mci *host, u8 sample)
402c537a1c5SSeungwon Jeon {
403c537a1c5SSeungwon Jeon 	u32 clksel;
40489ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
40589ad2be7SAbhilash Kesavan 
40689ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
40789ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
40889ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
40989ad2be7SAbhilash Kesavan 	else
410c537a1c5SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
41180113132SSeungwon Jeon 	clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
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);
417c537a1c5SSeungwon Jeon }
418c537a1c5SSeungwon Jeon 
419c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_move_next_clksmpl(struct dw_mci *host)
420c537a1c5SSeungwon Jeon {
42189ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
422c537a1c5SSeungwon Jeon 	u32 clksel;
423c537a1c5SSeungwon Jeon 	u8 sample;
424c537a1c5SSeungwon Jeon 
42589ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
42689ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
42789ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
42889ad2be7SAbhilash Kesavan 	else
429c537a1c5SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
43080113132SSeungwon Jeon 
431c537a1c5SSeungwon Jeon 	sample = (clksel + 1) & 0x7;
43280113132SSeungwon Jeon 	clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
43380113132SSeungwon Jeon 
43489ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
43589ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
43689ad2be7SAbhilash Kesavan 		mci_writel(host, CLKSEL64, clksel);
43789ad2be7SAbhilash Kesavan 	else
438c537a1c5SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
43980113132SSeungwon Jeon 
440c537a1c5SSeungwon Jeon 	return sample;
441c537a1c5SSeungwon Jeon }
442c537a1c5SSeungwon Jeon 
443c537a1c5SSeungwon Jeon static s8 dw_mci_exynos_get_best_clksmpl(u8 candiates)
444c537a1c5SSeungwon Jeon {
445c537a1c5SSeungwon Jeon 	const u8 iter = 8;
446c537a1c5SSeungwon Jeon 	u8 __c;
447c537a1c5SSeungwon Jeon 	s8 i, loc = -1;
448c537a1c5SSeungwon Jeon 
449c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
450c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
451c537a1c5SSeungwon Jeon 		if ((__c & 0xc7) == 0xc7) {
452c537a1c5SSeungwon Jeon 			loc = i;
453c537a1c5SSeungwon Jeon 			goto out;
454c537a1c5SSeungwon Jeon 		}
455c537a1c5SSeungwon Jeon 	}
456c537a1c5SSeungwon Jeon 
457c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
458c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
459c537a1c5SSeungwon Jeon 		if ((__c & 0x83) == 0x83) {
460c537a1c5SSeungwon Jeon 			loc = i;
461c537a1c5SSeungwon Jeon 			goto out;
462c537a1c5SSeungwon Jeon 		}
463c537a1c5SSeungwon Jeon 	}
464c537a1c5SSeungwon Jeon 
465c537a1c5SSeungwon Jeon out:
466c537a1c5SSeungwon Jeon 	return loc;
467c537a1c5SSeungwon Jeon }
468c537a1c5SSeungwon Jeon 
4699979dbe5SChaotian Jing static int dw_mci_exynos_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
470c537a1c5SSeungwon Jeon {
471c537a1c5SSeungwon Jeon 	struct dw_mci *host = slot->host;
47280113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
473c537a1c5SSeungwon Jeon 	struct mmc_host *mmc = slot->mmc;
474c537a1c5SSeungwon Jeon 	u8 start_smpl, smpl, candiates = 0;
475c537a1c5SSeungwon Jeon 	s8 found = -1;
476c537a1c5SSeungwon Jeon 	int ret = 0;
477c537a1c5SSeungwon Jeon 
478c537a1c5SSeungwon Jeon 	start_smpl = dw_mci_exynos_get_clksmpl(host);
479c537a1c5SSeungwon Jeon 
480c537a1c5SSeungwon Jeon 	do {
481c537a1c5SSeungwon Jeon 		mci_writel(host, TMOUT, ~0);
482c537a1c5SSeungwon Jeon 		smpl = dw_mci_exynos_move_next_clksmpl(host);
483c537a1c5SSeungwon Jeon 
4849979dbe5SChaotian Jing 		if (!mmc_send_tuning(mmc, opcode, NULL))
485c537a1c5SSeungwon Jeon 			candiates |= (1 << smpl);
4866c2c6506SUlf Hansson 
487c537a1c5SSeungwon Jeon 	} while (start_smpl != smpl);
488c537a1c5SSeungwon Jeon 
489c537a1c5SSeungwon Jeon 	found = dw_mci_exynos_get_best_clksmpl(candiates);
49080113132SSeungwon Jeon 	if (found >= 0) {
491c537a1c5SSeungwon Jeon 		dw_mci_exynos_set_clksmpl(host, found);
49280113132SSeungwon Jeon 		priv->tuned_sample = found;
49380113132SSeungwon Jeon 	} else {
494c537a1c5SSeungwon Jeon 		ret = -EIO;
49580113132SSeungwon Jeon 	}
496c537a1c5SSeungwon Jeon 
497c537a1c5SSeungwon Jeon 	return ret;
498c537a1c5SSeungwon Jeon }
499c537a1c5SSeungwon Jeon 
500c22f5e1bSWu Fengguang static int dw_mci_exynos_prepare_hs400_tuning(struct dw_mci *host,
50180113132SSeungwon Jeon 					struct mmc_ios *ios)
50280113132SSeungwon Jeon {
50380113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
50480113132SSeungwon Jeon 
50580113132SSeungwon Jeon 	dw_mci_exynos_set_clksel_timing(host, priv->hs400_timing);
50680113132SSeungwon Jeon 	dw_mci_exynos_adjust_clock(host, (ios->clock) << 1);
50780113132SSeungwon Jeon 
50880113132SSeungwon Jeon 	return 0;
50980113132SSeungwon Jeon }
51080113132SSeungwon Jeon 
5110f6e73d0SDongjin Kim /* Common capabilities of Exynos4/Exynos5 SoC */
5120f6e73d0SDongjin Kim static unsigned long exynos_dwmmc_caps[4] = {
513cab3a802SSeungwon Jeon 	MMC_CAP_1_8V_DDR | MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
514c3665006SThomas Abraham 	MMC_CAP_CMD23,
515c3665006SThomas Abraham 	MMC_CAP_CMD23,
516c3665006SThomas Abraham 	MMC_CAP_CMD23,
517c3665006SThomas Abraham };
518c3665006SThomas Abraham 
5190f6e73d0SDongjin Kim static const struct dw_mci_drv_data exynos_drv_data = {
5200f6e73d0SDongjin Kim 	.caps			= exynos_dwmmc_caps,
5210d84b9e5SShawn Lin 	.num_caps		= ARRAY_SIZE(exynos_dwmmc_caps),
522c3665006SThomas Abraham 	.init			= dw_mci_exynos_priv_init,
523c3665006SThomas Abraham 	.set_ios		= dw_mci_exynos_set_ios,
524c3665006SThomas Abraham 	.parse_dt		= dw_mci_exynos_parse_dt,
525c537a1c5SSeungwon Jeon 	.execute_tuning		= dw_mci_exynos_execute_tuning,
52680113132SSeungwon Jeon 	.prepare_hs400_tuning	= dw_mci_exynos_prepare_hs400_tuning,
527c3665006SThomas Abraham };
528c3665006SThomas Abraham 
529c3665006SThomas Abraham static const struct of_device_id dw_mci_exynos_match[] = {
5300f6e73d0SDongjin Kim 	{ .compatible = "samsung,exynos4412-dw-mshc",
5310f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
532c3665006SThomas Abraham 	{ .compatible = "samsung,exynos5250-dw-mshc",
5330f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
53400fd041bSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc",
53500fd041bSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
5366bce431cSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc-smu",
5376bce431cSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
53889ad2be7SAbhilash Kesavan 	{ .compatible = "samsung,exynos7-dw-mshc",
53989ad2be7SAbhilash Kesavan 			.data = &exynos_drv_data, },
54089ad2be7SAbhilash Kesavan 	{ .compatible = "samsung,exynos7-dw-mshc-smu",
54189ad2be7SAbhilash Kesavan 			.data = &exynos_drv_data, },
542c3665006SThomas Abraham 	{},
543c3665006SThomas Abraham };
544517cb9f1SArnd Bergmann MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
545c3665006SThomas Abraham 
5469665f7f2SSachin Kamat static int dw_mci_exynos_probe(struct platform_device *pdev)
547c3665006SThomas Abraham {
5488e2b36eaSArnd Bergmann 	const struct dw_mci_drv_data *drv_data;
549c3665006SThomas Abraham 	const struct of_device_id *match;
5509b93d392SJoonyoung Shim 	int ret;
551c3665006SThomas Abraham 
552c3665006SThomas Abraham 	match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
553c3665006SThomas Abraham 	drv_data = match->data;
5549b93d392SJoonyoung Shim 
5559b93d392SJoonyoung Shim 	pm_runtime_get_noresume(&pdev->dev);
5569b93d392SJoonyoung Shim 	pm_runtime_set_active(&pdev->dev);
5579b93d392SJoonyoung Shim 	pm_runtime_enable(&pdev->dev);
5589b93d392SJoonyoung Shim 
5599b93d392SJoonyoung Shim 	ret = dw_mci_pltfm_register(pdev, drv_data);
5609b93d392SJoonyoung Shim 	if (ret) {
5619b93d392SJoonyoung Shim 		pm_runtime_disable(&pdev->dev);
5629b93d392SJoonyoung Shim 		pm_runtime_set_suspended(&pdev->dev);
5639b93d392SJoonyoung Shim 		pm_runtime_put_noidle(&pdev->dev);
5649b93d392SJoonyoung Shim 
5659b93d392SJoonyoung Shim 		return ret;
5669b93d392SJoonyoung Shim 	}
5679b93d392SJoonyoung Shim 
5689b93d392SJoonyoung Shim 	return 0;
5699b93d392SJoonyoung Shim }
5709b93d392SJoonyoung Shim 
5719b93d392SJoonyoung Shim static int dw_mci_exynos_remove(struct platform_device *pdev)
5729b93d392SJoonyoung Shim {
5739b93d392SJoonyoung Shim 	pm_runtime_disable(&pdev->dev);
5749b93d392SJoonyoung Shim 	pm_runtime_set_suspended(&pdev->dev);
5759b93d392SJoonyoung Shim 	pm_runtime_put_noidle(&pdev->dev);
5769b93d392SJoonyoung Shim 
5779b93d392SJoonyoung Shim 	return dw_mci_pltfm_remove(pdev);
578c3665006SThomas Abraham }
579c3665006SThomas Abraham 
58015a2e2abSSachin Kamat static const struct dev_pm_ops dw_mci_exynos_pmops = {
581ecf7c7c5SMarek Szyprowski 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dw_mci_exynos_suspend_noirq,
582ecf7c7c5SMarek Szyprowski 				      dw_mci_exynos_resume_noirq)
583cf5237efSShawn Lin 	SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
584cf5237efSShawn Lin 			   dw_mci_exynos_runtime_resume,
585cf5237efSShawn Lin 			   NULL)
586e2c63599SDoug Anderson };
587e2c63599SDoug Anderson 
588c3665006SThomas Abraham static struct platform_driver dw_mci_exynos_pltfm_driver = {
589c3665006SThomas Abraham 	.probe		= dw_mci_exynos_probe,
5909b93d392SJoonyoung Shim 	.remove		= dw_mci_exynos_remove,
591c3665006SThomas Abraham 	.driver		= {
592c3665006SThomas Abraham 		.name		= "dwmmc_exynos",
59320183d50SSachin Kamat 		.of_match_table	= dw_mci_exynos_match,
594e2c63599SDoug Anderson 		.pm		= &dw_mci_exynos_pmops,
595c3665006SThomas Abraham 	},
596c3665006SThomas Abraham };
597c3665006SThomas Abraham 
598c3665006SThomas Abraham module_platform_driver(dw_mci_exynos_pltfm_driver);
599c3665006SThomas Abraham 
600c3665006SThomas Abraham MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
601c3665006SThomas Abraham MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
602c3665006SThomas Abraham MODULE_LICENSE("GPL v2");
6032fc546fdSZhangfei Gao MODULE_ALIAS("platform:dwmmc_exynos");
604