xref: /openbmc/linux/drivers/mmc/host/dw_mmc-exynos.c (revision 42f989c002f235557e3a03feac3b2f16b17d53f6)
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>
16c537a1c5SSeungwon Jeon #include <linux/mmc/mmc.h>
17c3665006SThomas Abraham #include <linux/of.h>
18c3665006SThomas Abraham #include <linux/of_gpio.h>
19cf5237efSShawn Lin #include <linux/pm_runtime.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 	 */
160*42f989c0SJaehoon Chung 	if (!SDMMC_CLKSEL_GET_DRV_WD3(clksel) && host->slot)
161*42f989c0SJaehoon Chung 		set_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags);
16280113132SSeungwon Jeon }
16380113132SSeungwon Jeon 
164cf5237efSShawn Lin #ifdef CONFIG_PM
165cf5237efSShawn Lin static int dw_mci_exynos_runtime_resume(struct device *dev)
166e2c63599SDoug Anderson {
167e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
168e2c63599SDoug Anderson 
1695659eeadSJaehoon Chung 	dw_mci_exynos_config_smu(host);
170cf5237efSShawn Lin 	return dw_mci_runtime_resume(dev);
171e2c63599SDoug Anderson }
172e2c63599SDoug Anderson 
173e2c63599SDoug Anderson /**
174e2c63599SDoug Anderson  * dw_mci_exynos_resume_noirq - Exynos-specific resume code
175e2c63599SDoug Anderson  *
176e2c63599SDoug Anderson  * On exynos5420 there is a silicon errata that will sometimes leave the
177e2c63599SDoug Anderson  * WAKEUP_INT bit in the CLKSEL register asserted.  This bit is 1 to indicate
178e2c63599SDoug Anderson  * that it fired and we can clear it by writing a 1 back.  Clear it to prevent
179e2c63599SDoug Anderson  * interrupts from going off constantly.
180e2c63599SDoug Anderson  *
181e2c63599SDoug Anderson  * We run this code on all exynos variants because it doesn't hurt.
182e2c63599SDoug Anderson  */
183e2c63599SDoug Anderson 
184e2c63599SDoug Anderson static int dw_mci_exynos_resume_noirq(struct device *dev)
185e2c63599SDoug Anderson {
186e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
18789ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
188e2c63599SDoug Anderson 	u32 clksel;
189e2c63599SDoug Anderson 
19089ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
19189ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
19289ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
19389ad2be7SAbhilash Kesavan 	else
194e2c63599SDoug Anderson 		clksel = mci_readl(host, CLKSEL);
19589ad2be7SAbhilash Kesavan 
19689ad2be7SAbhilash Kesavan 	if (clksel & SDMMC_CLKSEL_WAKEUP_INT) {
19789ad2be7SAbhilash Kesavan 		if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
19889ad2be7SAbhilash Kesavan 			priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
19989ad2be7SAbhilash Kesavan 			mci_writel(host, CLKSEL64, clksel);
20089ad2be7SAbhilash Kesavan 		else
201e2c63599SDoug Anderson 			mci_writel(host, CLKSEL, clksel);
20289ad2be7SAbhilash Kesavan 	}
203e2c63599SDoug Anderson 
204e2c63599SDoug Anderson 	return 0;
205e2c63599SDoug Anderson }
206e2c63599SDoug Anderson #else
207e2c63599SDoug Anderson #define dw_mci_exynos_resume_noirq	NULL
208cf5237efSShawn Lin #endif /* CONFIG_PM */
209e2c63599SDoug Anderson 
21080113132SSeungwon Jeon static void dw_mci_exynos_config_hs400(struct dw_mci *host, u32 timing)
211c3665006SThomas Abraham {
212c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
21380113132SSeungwon Jeon 	u32 dqs, strobe;
214c3665006SThomas Abraham 
21580113132SSeungwon Jeon 	/*
21680113132SSeungwon Jeon 	 * Not supported to configure register
21780113132SSeungwon Jeon 	 * related to HS400
21880113132SSeungwon Jeon 	 */
219941a659fSKrzysztof Kozlowski 	if (priv->ctrl_type < DW_MCI_TYPE_EXYNOS5420) {
220941a659fSKrzysztof Kozlowski 		if (timing == MMC_TIMING_MMC_HS400)
221941a659fSKrzysztof Kozlowski 			dev_warn(host->dev,
222941a659fSKrzysztof Kozlowski 				 "cannot configure HS400, unsupported chipset\n");
22380113132SSeungwon Jeon 		return;
224941a659fSKrzysztof Kozlowski 	}
22580113132SSeungwon Jeon 
22680113132SSeungwon Jeon 	dqs = priv->saved_dqs_en;
22780113132SSeungwon Jeon 	strobe = priv->saved_strobe_ctrl;
22880113132SSeungwon Jeon 
22980113132SSeungwon Jeon 	if (timing == MMC_TIMING_MMC_HS400) {
23080113132SSeungwon Jeon 		dqs |= DATA_STROBE_EN;
23180113132SSeungwon Jeon 		strobe = DQS_CTRL_RD_DELAY(strobe, priv->dqs_delay);
232c6d9dedaSSeungwon Jeon 	} else {
23380113132SSeungwon Jeon 		dqs &= ~DATA_STROBE_EN;
234c3665006SThomas Abraham 	}
235c3665006SThomas Abraham 
23680113132SSeungwon Jeon 	mci_writel(host, HS400_DQS_EN, dqs);
23780113132SSeungwon Jeon 	mci_writel(host, HS400_DLINE_CTRL, strobe);
23880113132SSeungwon Jeon }
23980113132SSeungwon Jeon 
24080113132SSeungwon Jeon static void dw_mci_exynos_adjust_clock(struct dw_mci *host, unsigned int wanted)
24180113132SSeungwon Jeon {
24280113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
24380113132SSeungwon Jeon 	unsigned long actual;
24480113132SSeungwon Jeon 	u8 div;
24580113132SSeungwon Jeon 	int ret;
246a2a1fed8SSeungwon Jeon 	/*
247a2a1fed8SSeungwon Jeon 	 * Don't care if wanted clock is zero or
248a2a1fed8SSeungwon Jeon 	 * ciu clock is unavailable
249a2a1fed8SSeungwon Jeon 	 */
250a2a1fed8SSeungwon Jeon 	if (!wanted || IS_ERR(host->ciu_clk))
251c6d9dedaSSeungwon Jeon 		return;
252c6d9dedaSSeungwon Jeon 
253c6d9dedaSSeungwon Jeon 	/* Guaranteed minimum frequency for cclkin */
254c6d9dedaSSeungwon Jeon 	if (wanted < EXYNOS_CCLKIN_MIN)
255c6d9dedaSSeungwon Jeon 		wanted = EXYNOS_CCLKIN_MIN;
256c6d9dedaSSeungwon Jeon 
25780113132SSeungwon Jeon 	if (wanted == priv->cur_speed)
25880113132SSeungwon Jeon 		return;
25980113132SSeungwon Jeon 
26080113132SSeungwon Jeon 	div = dw_mci_exynos_get_ciu_div(host);
26180113132SSeungwon Jeon 	ret = clk_set_rate(host->ciu_clk, wanted * div);
262c6d9dedaSSeungwon Jeon 	if (ret)
263c6d9dedaSSeungwon Jeon 		dev_warn(host->dev,
264c6d9dedaSSeungwon Jeon 			"failed to set clk-rate %u error: %d\n",
265c6d9dedaSSeungwon Jeon 			wanted * div, ret);
266c6d9dedaSSeungwon Jeon 	actual = clk_get_rate(host->ciu_clk);
267c6d9dedaSSeungwon Jeon 	host->bus_hz = actual / div;
268c6d9dedaSSeungwon Jeon 	priv->cur_speed = wanted;
269c6d9dedaSSeungwon Jeon 	host->current_speed = 0;
270c6d9dedaSSeungwon Jeon }
27180113132SSeungwon Jeon 
27280113132SSeungwon Jeon static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
27380113132SSeungwon Jeon {
27480113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
27580113132SSeungwon Jeon 	unsigned int wanted = ios->clock;
27680113132SSeungwon Jeon 	u32 timing = ios->timing, clksel;
27780113132SSeungwon Jeon 
27880113132SSeungwon Jeon 	switch (timing) {
27980113132SSeungwon Jeon 	case MMC_TIMING_MMC_HS400:
28080113132SSeungwon Jeon 		/* Update tuned sample timing */
28180113132SSeungwon Jeon 		clksel = SDMMC_CLKSEL_UP_SAMPLE(
28280113132SSeungwon Jeon 				priv->hs400_timing, priv->tuned_sample);
28380113132SSeungwon Jeon 		wanted <<= 1;
28480113132SSeungwon Jeon 		break;
28580113132SSeungwon Jeon 	case MMC_TIMING_MMC_DDR52:
28680113132SSeungwon Jeon 		clksel = priv->ddr_timing;
28780113132SSeungwon Jeon 		/* Should be double rate for DDR mode */
28880113132SSeungwon Jeon 		if (ios->bus_width == MMC_BUS_WIDTH_8)
28980113132SSeungwon Jeon 			wanted <<= 1;
29080113132SSeungwon Jeon 		break;
29180113132SSeungwon Jeon 	default:
29280113132SSeungwon Jeon 		clksel = priv->sdr_timing;
29380113132SSeungwon Jeon 	}
29480113132SSeungwon Jeon 
29580113132SSeungwon Jeon 	/* Set clock timing for the requested speed mode*/
29680113132SSeungwon Jeon 	dw_mci_exynos_set_clksel_timing(host, clksel);
29780113132SSeungwon Jeon 
29880113132SSeungwon Jeon 	/* Configure setting for HS400 */
29980113132SSeungwon Jeon 	dw_mci_exynos_config_hs400(host, timing);
30080113132SSeungwon Jeon 
30180113132SSeungwon Jeon 	/* Configure clock rate */
30280113132SSeungwon Jeon 	dw_mci_exynos_adjust_clock(host, wanted);
303c6d9dedaSSeungwon Jeon }
304c6d9dedaSSeungwon Jeon 
305c3665006SThomas Abraham static int dw_mci_exynos_parse_dt(struct dw_mci *host)
306c3665006SThomas Abraham {
307e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv;
308c3665006SThomas Abraham 	struct device_node *np = host->dev->of_node;
309c3665006SThomas Abraham 	u32 timing[2];
310c3665006SThomas Abraham 	u32 div = 0;
311e6c784edSYuvaraj Kumar C D 	int idx;
312c3665006SThomas Abraham 	int ret;
313c3665006SThomas Abraham 
314e6c784edSYuvaraj Kumar C D 	priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
315bf3707eaSBeomho Seo 	if (!priv)
316e6c784edSYuvaraj Kumar C D 		return -ENOMEM;
317e6c784edSYuvaraj Kumar C D 
318e6c784edSYuvaraj Kumar C D 	for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
319e6c784edSYuvaraj Kumar C D 		if (of_device_is_compatible(np, exynos_compat[idx].compatible))
320e6c784edSYuvaraj Kumar C D 			priv->ctrl_type = exynos_compat[idx].ctrl_type;
321e6c784edSYuvaraj Kumar C D 	}
322e6c784edSYuvaraj Kumar C D 
323c6d9dedaSSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
324c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1;
325c6d9dedaSSeungwon Jeon 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
326c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4210_FIXED_CIU_CLK_DIV - 1;
327c6d9dedaSSeungwon Jeon 	else {
328c3665006SThomas Abraham 		of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
329c3665006SThomas Abraham 		priv->ciu_div = div;
330c6d9dedaSSeungwon Jeon 	}
331c3665006SThomas Abraham 
332c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
333c3665006SThomas Abraham 			"samsung,dw-mshc-sdr-timing", timing, 2);
334c3665006SThomas Abraham 	if (ret)
335c3665006SThomas Abraham 		return ret;
336c3665006SThomas Abraham 
3372d9f0bd1SYuvaraj Kumar C D 	priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
3382d9f0bd1SYuvaraj Kumar C D 
339c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
340c3665006SThomas Abraham 			"samsung,dw-mshc-ddr-timing", timing, 2);
341c3665006SThomas Abraham 	if (ret)
342c3665006SThomas Abraham 		return ret;
343c3665006SThomas Abraham 
344c3665006SThomas Abraham 	priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
34580113132SSeungwon Jeon 
34680113132SSeungwon Jeon 	ret = of_property_read_u32_array(np,
34780113132SSeungwon Jeon 			"samsung,dw-mshc-hs400-timing", timing, 2);
34880113132SSeungwon Jeon 	if (!ret && of_property_read_u32(np,
34980113132SSeungwon Jeon 				"samsung,read-strobe-delay", &priv->dqs_delay))
35080113132SSeungwon Jeon 		dev_dbg(host->dev,
35180113132SSeungwon Jeon 			"read-strobe-delay is not found, assuming usage of default value\n");
35280113132SSeungwon Jeon 
35380113132SSeungwon Jeon 	priv->hs400_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1],
35480113132SSeungwon Jeon 						HS400_FIXED_CIU_CLK_DIV);
355e6c784edSYuvaraj Kumar C D 	host->priv = priv;
356c3665006SThomas Abraham 	return 0;
357c3665006SThomas Abraham }
358c3665006SThomas Abraham 
359c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_get_clksmpl(struct dw_mci *host)
360c537a1c5SSeungwon Jeon {
36189ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
36289ad2be7SAbhilash Kesavan 
36389ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
36489ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
36589ad2be7SAbhilash Kesavan 		return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL64));
36689ad2be7SAbhilash Kesavan 	else
367c537a1c5SSeungwon Jeon 		return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL));
368c537a1c5SSeungwon Jeon }
369c537a1c5SSeungwon Jeon 
370c537a1c5SSeungwon Jeon static inline void dw_mci_exynos_set_clksmpl(struct dw_mci *host, u8 sample)
371c537a1c5SSeungwon Jeon {
372c537a1c5SSeungwon Jeon 	u32 clksel;
37389ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
37489ad2be7SAbhilash Kesavan 
37589ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
37689ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
37789ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
37889ad2be7SAbhilash Kesavan 	else
379c537a1c5SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
38080113132SSeungwon Jeon 	clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
38189ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
38289ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
38389ad2be7SAbhilash Kesavan 		mci_writel(host, CLKSEL64, clksel);
38489ad2be7SAbhilash Kesavan 	else
385c537a1c5SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
386c537a1c5SSeungwon Jeon }
387c537a1c5SSeungwon Jeon 
388c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_move_next_clksmpl(struct dw_mci *host)
389c537a1c5SSeungwon Jeon {
39089ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
391c537a1c5SSeungwon Jeon 	u32 clksel;
392c537a1c5SSeungwon Jeon 	u8 sample;
393c537a1c5SSeungwon Jeon 
39489ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
39589ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
39689ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
39789ad2be7SAbhilash Kesavan 	else
398c537a1c5SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
39980113132SSeungwon Jeon 
400c537a1c5SSeungwon Jeon 	sample = (clksel + 1) & 0x7;
40180113132SSeungwon Jeon 	clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
40280113132SSeungwon Jeon 
40389ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
40489ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
40589ad2be7SAbhilash Kesavan 		mci_writel(host, CLKSEL64, clksel);
40689ad2be7SAbhilash Kesavan 	else
407c537a1c5SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
40880113132SSeungwon Jeon 
409c537a1c5SSeungwon Jeon 	return sample;
410c537a1c5SSeungwon Jeon }
411c537a1c5SSeungwon Jeon 
412c537a1c5SSeungwon Jeon static s8 dw_mci_exynos_get_best_clksmpl(u8 candiates)
413c537a1c5SSeungwon Jeon {
414c537a1c5SSeungwon Jeon 	const u8 iter = 8;
415c537a1c5SSeungwon Jeon 	u8 __c;
416c537a1c5SSeungwon Jeon 	s8 i, loc = -1;
417c537a1c5SSeungwon Jeon 
418c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
419c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
420c537a1c5SSeungwon Jeon 		if ((__c & 0xc7) == 0xc7) {
421c537a1c5SSeungwon Jeon 			loc = i;
422c537a1c5SSeungwon Jeon 			goto out;
423c537a1c5SSeungwon Jeon 		}
424c537a1c5SSeungwon Jeon 	}
425c537a1c5SSeungwon Jeon 
426c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
427c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
428c537a1c5SSeungwon Jeon 		if ((__c & 0x83) == 0x83) {
429c537a1c5SSeungwon Jeon 			loc = i;
430c537a1c5SSeungwon Jeon 			goto out;
431c537a1c5SSeungwon Jeon 		}
432c537a1c5SSeungwon Jeon 	}
433c537a1c5SSeungwon Jeon 
434c537a1c5SSeungwon Jeon out:
435c537a1c5SSeungwon Jeon 	return loc;
436c537a1c5SSeungwon Jeon }
437c537a1c5SSeungwon Jeon 
4389979dbe5SChaotian Jing static int dw_mci_exynos_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
439c537a1c5SSeungwon Jeon {
440c537a1c5SSeungwon Jeon 	struct dw_mci *host = slot->host;
44180113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
442c537a1c5SSeungwon Jeon 	struct mmc_host *mmc = slot->mmc;
443c537a1c5SSeungwon Jeon 	u8 start_smpl, smpl, candiates = 0;
444c537a1c5SSeungwon Jeon 	s8 found = -1;
445c537a1c5SSeungwon Jeon 	int ret = 0;
446c537a1c5SSeungwon Jeon 
447c537a1c5SSeungwon Jeon 	start_smpl = dw_mci_exynos_get_clksmpl(host);
448c537a1c5SSeungwon Jeon 
449c537a1c5SSeungwon Jeon 	do {
450c537a1c5SSeungwon Jeon 		mci_writel(host, TMOUT, ~0);
451c537a1c5SSeungwon Jeon 		smpl = dw_mci_exynos_move_next_clksmpl(host);
452c537a1c5SSeungwon Jeon 
4539979dbe5SChaotian Jing 		if (!mmc_send_tuning(mmc, opcode, NULL))
454c537a1c5SSeungwon Jeon 			candiates |= (1 << smpl);
4556c2c6506SUlf Hansson 
456c537a1c5SSeungwon Jeon 	} while (start_smpl != smpl);
457c537a1c5SSeungwon Jeon 
458c537a1c5SSeungwon Jeon 	found = dw_mci_exynos_get_best_clksmpl(candiates);
45980113132SSeungwon Jeon 	if (found >= 0) {
460c537a1c5SSeungwon Jeon 		dw_mci_exynos_set_clksmpl(host, found);
46180113132SSeungwon Jeon 		priv->tuned_sample = found;
46280113132SSeungwon Jeon 	} else {
463c537a1c5SSeungwon Jeon 		ret = -EIO;
46480113132SSeungwon Jeon 	}
465c537a1c5SSeungwon Jeon 
466c537a1c5SSeungwon Jeon 	return ret;
467c537a1c5SSeungwon Jeon }
468c537a1c5SSeungwon Jeon 
469c22f5e1bSWu Fengguang static int dw_mci_exynos_prepare_hs400_tuning(struct dw_mci *host,
47080113132SSeungwon Jeon 					struct mmc_ios *ios)
47180113132SSeungwon Jeon {
47280113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
47380113132SSeungwon Jeon 
47480113132SSeungwon Jeon 	dw_mci_exynos_set_clksel_timing(host, priv->hs400_timing);
47580113132SSeungwon Jeon 	dw_mci_exynos_adjust_clock(host, (ios->clock) << 1);
47680113132SSeungwon Jeon 
47780113132SSeungwon Jeon 	return 0;
47880113132SSeungwon Jeon }
47980113132SSeungwon Jeon 
4800f6e73d0SDongjin Kim /* Common capabilities of Exynos4/Exynos5 SoC */
4810f6e73d0SDongjin Kim static unsigned long exynos_dwmmc_caps[4] = {
482cab3a802SSeungwon Jeon 	MMC_CAP_1_8V_DDR | MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
483c3665006SThomas Abraham 	MMC_CAP_CMD23,
484c3665006SThomas Abraham 	MMC_CAP_CMD23,
485c3665006SThomas Abraham 	MMC_CAP_CMD23,
486c3665006SThomas Abraham };
487c3665006SThomas Abraham 
4880f6e73d0SDongjin Kim static const struct dw_mci_drv_data exynos_drv_data = {
4890f6e73d0SDongjin Kim 	.caps			= exynos_dwmmc_caps,
490c3665006SThomas Abraham 	.init			= dw_mci_exynos_priv_init,
491c3665006SThomas Abraham 	.set_ios		= dw_mci_exynos_set_ios,
492c3665006SThomas Abraham 	.parse_dt		= dw_mci_exynos_parse_dt,
493c537a1c5SSeungwon Jeon 	.execute_tuning		= dw_mci_exynos_execute_tuning,
49480113132SSeungwon Jeon 	.prepare_hs400_tuning	= dw_mci_exynos_prepare_hs400_tuning,
495c3665006SThomas Abraham };
496c3665006SThomas Abraham 
497c3665006SThomas Abraham static const struct of_device_id dw_mci_exynos_match[] = {
4980f6e73d0SDongjin Kim 	{ .compatible = "samsung,exynos4412-dw-mshc",
4990f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
500c3665006SThomas Abraham 	{ .compatible = "samsung,exynos5250-dw-mshc",
5010f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
50200fd041bSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc",
50300fd041bSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
5046bce431cSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc-smu",
5056bce431cSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
50689ad2be7SAbhilash Kesavan 	{ .compatible = "samsung,exynos7-dw-mshc",
50789ad2be7SAbhilash Kesavan 			.data = &exynos_drv_data, },
50889ad2be7SAbhilash Kesavan 	{ .compatible = "samsung,exynos7-dw-mshc-smu",
50989ad2be7SAbhilash Kesavan 			.data = &exynos_drv_data, },
510c3665006SThomas Abraham 	{},
511c3665006SThomas Abraham };
512517cb9f1SArnd Bergmann MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
513c3665006SThomas Abraham 
5149665f7f2SSachin Kamat static int dw_mci_exynos_probe(struct platform_device *pdev)
515c3665006SThomas Abraham {
5168e2b36eaSArnd Bergmann 	const struct dw_mci_drv_data *drv_data;
517c3665006SThomas Abraham 	const struct of_device_id *match;
5189b93d392SJoonyoung Shim 	int ret;
519c3665006SThomas Abraham 
520c3665006SThomas Abraham 	match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
521c3665006SThomas Abraham 	drv_data = match->data;
5229b93d392SJoonyoung Shim 
5239b93d392SJoonyoung Shim 	pm_runtime_get_noresume(&pdev->dev);
5249b93d392SJoonyoung Shim 	pm_runtime_set_active(&pdev->dev);
5259b93d392SJoonyoung Shim 	pm_runtime_enable(&pdev->dev);
5269b93d392SJoonyoung Shim 
5279b93d392SJoonyoung Shim 	ret = dw_mci_pltfm_register(pdev, drv_data);
5289b93d392SJoonyoung Shim 	if (ret) {
5299b93d392SJoonyoung Shim 		pm_runtime_disable(&pdev->dev);
5309b93d392SJoonyoung Shim 		pm_runtime_set_suspended(&pdev->dev);
5319b93d392SJoonyoung Shim 		pm_runtime_put_noidle(&pdev->dev);
5329b93d392SJoonyoung Shim 
5339b93d392SJoonyoung Shim 		return ret;
5349b93d392SJoonyoung Shim 	}
5359b93d392SJoonyoung Shim 
5369b93d392SJoonyoung Shim 	return 0;
5379b93d392SJoonyoung Shim }
5389b93d392SJoonyoung Shim 
5399b93d392SJoonyoung Shim static int dw_mci_exynos_remove(struct platform_device *pdev)
5409b93d392SJoonyoung Shim {
5419b93d392SJoonyoung Shim 	pm_runtime_disable(&pdev->dev);
5429b93d392SJoonyoung Shim 	pm_runtime_set_suspended(&pdev->dev);
5439b93d392SJoonyoung Shim 	pm_runtime_put_noidle(&pdev->dev);
5449b93d392SJoonyoung Shim 
5459b93d392SJoonyoung Shim 	return dw_mci_pltfm_remove(pdev);
546c3665006SThomas Abraham }
547c3665006SThomas Abraham 
54815a2e2abSSachin Kamat static const struct dev_pm_ops dw_mci_exynos_pmops = {
549cf5237efSShawn Lin 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
550cf5237efSShawn Lin 				pm_runtime_force_resume)
551cf5237efSShawn Lin 	SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
552cf5237efSShawn Lin 			   dw_mci_exynos_runtime_resume,
553cf5237efSShawn Lin 			   NULL)
554e2c63599SDoug Anderson 	.resume_noirq = dw_mci_exynos_resume_noirq,
555e2c63599SDoug Anderson 	.thaw_noirq = dw_mci_exynos_resume_noirq,
556e2c63599SDoug Anderson 	.restore_noirq = dw_mci_exynos_resume_noirq,
557e2c63599SDoug Anderson };
558e2c63599SDoug Anderson 
559c3665006SThomas Abraham static struct platform_driver dw_mci_exynos_pltfm_driver = {
560c3665006SThomas Abraham 	.probe		= dw_mci_exynos_probe,
5619b93d392SJoonyoung Shim 	.remove		= dw_mci_exynos_remove,
562c3665006SThomas Abraham 	.driver		= {
563c3665006SThomas Abraham 		.name		= "dwmmc_exynos",
56420183d50SSachin Kamat 		.of_match_table	= dw_mci_exynos_match,
565e2c63599SDoug Anderson 		.pm		= &dw_mci_exynos_pmops,
566c3665006SThomas Abraham 	},
567c3665006SThomas Abraham };
568c3665006SThomas Abraham 
569c3665006SThomas Abraham module_platform_driver(dw_mci_exynos_pltfm_driver);
570c3665006SThomas Abraham 
571c3665006SThomas Abraham MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
572c3665006SThomas Abraham MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
573c3665006SThomas Abraham MODULE_LICENSE("GPL v2");
5742fc546fdSZhangfei Gao MODULE_ALIAS("platform:dwmmc_exynos");
575