xref: /openbmc/linux/drivers/mmc/host/dw_mmc-exynos.c (revision 5659eeaddd47982d03a19c651384456cc3164acc)
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 
94*5659eeadSJaehoon 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 
98*5659eeadSJaehoon Chung 	/*
99*5659eeadSJaehoon Chung 	 * If Exynos is provided the Security management,
100*5659eeadSJaehoon Chung 	 * set for non-ecryption mode at this time.
101*5659eeadSJaehoon 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 	}
111*5659eeadSJaehoon Chung }
112*5659eeadSJaehoon Chung 
113*5659eeadSJaehoon Chung static int dw_mci_exynos_priv_init(struct dw_mci *host)
114*5659eeadSJaehoon Chung {
115*5659eeadSJaehoon Chung 	struct dw_mci_exynos_priv_data *priv = host->priv;
116*5659eeadSJaehoon Chung 
117*5659eeadSJaehoon 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 
129c3665006SThomas Abraham 	return 0;
130c3665006SThomas Abraham }
131c3665006SThomas Abraham 
132c3665006SThomas Abraham static int dw_mci_exynos_setup_clock(struct dw_mci *host)
133c3665006SThomas Abraham {
134c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
135c3665006SThomas Abraham 
136a2a1fed8SSeungwon Jeon 	host->bus_hz /= (priv->ciu_div + 1);
137a2a1fed8SSeungwon Jeon 
138c3665006SThomas Abraham 	return 0;
139c3665006SThomas Abraham }
140c3665006SThomas Abraham 
14180113132SSeungwon Jeon static void dw_mci_exynos_set_clksel_timing(struct dw_mci *host, u32 timing)
14280113132SSeungwon Jeon {
14380113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
14480113132SSeungwon Jeon 	u32 clksel;
14580113132SSeungwon Jeon 
14680113132SSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
14780113132SSeungwon Jeon 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
14880113132SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL64);
14980113132SSeungwon Jeon 	else
15080113132SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
15180113132SSeungwon Jeon 
15280113132SSeungwon Jeon 	clksel = (clksel & ~SDMMC_CLKSEL_TIMING_MASK) | timing;
15380113132SSeungwon Jeon 
15480113132SSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
15580113132SSeungwon Jeon 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
15680113132SSeungwon Jeon 		mci_writel(host, CLKSEL64, clksel);
15780113132SSeungwon Jeon 	else
15880113132SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
159aaaaeb7aSJaehoon Chung 
160aaaaeb7aSJaehoon Chung 	/*
161aaaaeb7aSJaehoon Chung 	 * Exynos4412 and Exynos5250 extends the use of CMD register with the
162aaaaeb7aSJaehoon Chung 	 * use of bit 29 (which is reserved on standard MSHC controllers) for
163aaaaeb7aSJaehoon Chung 	 * optionally bypassing the HOLD register for command and data. The
164aaaaeb7aSJaehoon Chung 	 * HOLD register should be bypassed in case there is no phase shift
165aaaaeb7aSJaehoon Chung 	 * applied on CMD/DATA that is sent to the card.
166aaaaeb7aSJaehoon Chung 	 */
167aaaaeb7aSJaehoon Chung 	if (!SDMMC_CLKSEL_GET_DRV_WD3(clksel))
168aaaaeb7aSJaehoon Chung 		set_bit(DW_MMC_CARD_NO_USE_HOLD, &host->cur_slot->flags);
16980113132SSeungwon Jeon }
17080113132SSeungwon Jeon 
171e2c63599SDoug Anderson #ifdef CONFIG_PM_SLEEP
172e2c63599SDoug Anderson static int dw_mci_exynos_suspend(struct device *dev)
173e2c63599SDoug Anderson {
174e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
175e2c63599SDoug Anderson 
176e2c63599SDoug Anderson 	return dw_mci_suspend(host);
177e2c63599SDoug Anderson }
178e2c63599SDoug Anderson 
179e2c63599SDoug Anderson static int dw_mci_exynos_resume(struct device *dev)
180e2c63599SDoug Anderson {
181e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
182e2c63599SDoug Anderson 
183*5659eeadSJaehoon Chung 	dw_mci_exynos_config_smu(host);
184e2c63599SDoug Anderson 	return dw_mci_resume(host);
185e2c63599SDoug Anderson }
186e2c63599SDoug Anderson 
187e2c63599SDoug Anderson /**
188e2c63599SDoug Anderson  * dw_mci_exynos_resume_noirq - Exynos-specific resume code
189e2c63599SDoug Anderson  *
190e2c63599SDoug Anderson  * On exynos5420 there is a silicon errata that will sometimes leave the
191e2c63599SDoug Anderson  * WAKEUP_INT bit in the CLKSEL register asserted.  This bit is 1 to indicate
192e2c63599SDoug Anderson  * that it fired and we can clear it by writing a 1 back.  Clear it to prevent
193e2c63599SDoug Anderson  * interrupts from going off constantly.
194e2c63599SDoug Anderson  *
195e2c63599SDoug Anderson  * We run this code on all exynos variants because it doesn't hurt.
196e2c63599SDoug Anderson  */
197e2c63599SDoug Anderson 
198e2c63599SDoug Anderson static int dw_mci_exynos_resume_noirq(struct device *dev)
199e2c63599SDoug Anderson {
200e2c63599SDoug Anderson 	struct dw_mci *host = dev_get_drvdata(dev);
20189ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
202e2c63599SDoug Anderson 	u32 clksel;
203e2c63599SDoug Anderson 
20489ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
20589ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
20689ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
20789ad2be7SAbhilash Kesavan 	else
208e2c63599SDoug Anderson 		clksel = mci_readl(host, CLKSEL);
20989ad2be7SAbhilash Kesavan 
21089ad2be7SAbhilash Kesavan 	if (clksel & SDMMC_CLKSEL_WAKEUP_INT) {
21189ad2be7SAbhilash Kesavan 		if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
21289ad2be7SAbhilash Kesavan 			priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
21389ad2be7SAbhilash Kesavan 			mci_writel(host, CLKSEL64, clksel);
21489ad2be7SAbhilash Kesavan 		else
215e2c63599SDoug Anderson 			mci_writel(host, CLKSEL, clksel);
21689ad2be7SAbhilash Kesavan 	}
217e2c63599SDoug Anderson 
218e2c63599SDoug Anderson 	return 0;
219e2c63599SDoug Anderson }
220e2c63599SDoug Anderson #else
221e2c63599SDoug Anderson #define dw_mci_exynos_suspend		NULL
222e2c63599SDoug Anderson #define dw_mci_exynos_resume		NULL
223e2c63599SDoug Anderson #define dw_mci_exynos_resume_noirq	NULL
224e2c63599SDoug Anderson #endif /* CONFIG_PM_SLEEP */
225e2c63599SDoug Anderson 
22680113132SSeungwon Jeon static void dw_mci_exynos_config_hs400(struct dw_mci *host, u32 timing)
227c3665006SThomas Abraham {
228c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
22980113132SSeungwon Jeon 	u32 dqs, strobe;
230c3665006SThomas Abraham 
23180113132SSeungwon Jeon 	/*
23280113132SSeungwon Jeon 	 * Not supported to configure register
23380113132SSeungwon Jeon 	 * related to HS400
23480113132SSeungwon Jeon 	 */
23580113132SSeungwon Jeon 	if (priv->ctrl_type < DW_MCI_TYPE_EXYNOS5420)
23680113132SSeungwon Jeon 		return;
23780113132SSeungwon Jeon 
23880113132SSeungwon Jeon 	dqs = priv->saved_dqs_en;
23980113132SSeungwon Jeon 	strobe = priv->saved_strobe_ctrl;
24080113132SSeungwon Jeon 
24180113132SSeungwon Jeon 	if (timing == MMC_TIMING_MMC_HS400) {
24280113132SSeungwon Jeon 		dqs |= DATA_STROBE_EN;
24380113132SSeungwon Jeon 		strobe = DQS_CTRL_RD_DELAY(strobe, priv->dqs_delay);
244c6d9dedaSSeungwon Jeon 	} else {
24580113132SSeungwon Jeon 		dqs &= ~DATA_STROBE_EN;
246c3665006SThomas Abraham 	}
247c3665006SThomas Abraham 
24880113132SSeungwon Jeon 	mci_writel(host, HS400_DQS_EN, dqs);
24980113132SSeungwon Jeon 	mci_writel(host, HS400_DLINE_CTRL, strobe);
25080113132SSeungwon Jeon }
25180113132SSeungwon Jeon 
25280113132SSeungwon Jeon static void dw_mci_exynos_adjust_clock(struct dw_mci *host, unsigned int wanted)
25380113132SSeungwon Jeon {
25480113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
25580113132SSeungwon Jeon 	unsigned long actual;
25680113132SSeungwon Jeon 	u8 div;
25780113132SSeungwon Jeon 	int ret;
258a2a1fed8SSeungwon Jeon 	/*
259a2a1fed8SSeungwon Jeon 	 * Don't care if wanted clock is zero or
260a2a1fed8SSeungwon Jeon 	 * ciu clock is unavailable
261a2a1fed8SSeungwon Jeon 	 */
262a2a1fed8SSeungwon Jeon 	if (!wanted || IS_ERR(host->ciu_clk))
263c6d9dedaSSeungwon Jeon 		return;
264c6d9dedaSSeungwon Jeon 
265c6d9dedaSSeungwon Jeon 	/* Guaranteed minimum frequency for cclkin */
266c6d9dedaSSeungwon Jeon 	if (wanted < EXYNOS_CCLKIN_MIN)
267c6d9dedaSSeungwon Jeon 		wanted = EXYNOS_CCLKIN_MIN;
268c6d9dedaSSeungwon Jeon 
26980113132SSeungwon Jeon 	if (wanted == priv->cur_speed)
27080113132SSeungwon Jeon 		return;
27180113132SSeungwon Jeon 
27280113132SSeungwon Jeon 	div = dw_mci_exynos_get_ciu_div(host);
27380113132SSeungwon Jeon 	ret = clk_set_rate(host->ciu_clk, wanted * div);
274c6d9dedaSSeungwon Jeon 	if (ret)
275c6d9dedaSSeungwon Jeon 		dev_warn(host->dev,
276c6d9dedaSSeungwon Jeon 			"failed to set clk-rate %u error: %d\n",
277c6d9dedaSSeungwon Jeon 			wanted * div, ret);
278c6d9dedaSSeungwon Jeon 	actual = clk_get_rate(host->ciu_clk);
279c6d9dedaSSeungwon Jeon 	host->bus_hz = actual / div;
280c6d9dedaSSeungwon Jeon 	priv->cur_speed = wanted;
281c6d9dedaSSeungwon Jeon 	host->current_speed = 0;
282c6d9dedaSSeungwon Jeon }
28380113132SSeungwon Jeon 
28480113132SSeungwon Jeon static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
28580113132SSeungwon Jeon {
28680113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
28780113132SSeungwon Jeon 	unsigned int wanted = ios->clock;
28880113132SSeungwon Jeon 	u32 timing = ios->timing, clksel;
28980113132SSeungwon Jeon 
29080113132SSeungwon Jeon 	switch (timing) {
29180113132SSeungwon Jeon 	case MMC_TIMING_MMC_HS400:
29280113132SSeungwon Jeon 		/* Update tuned sample timing */
29380113132SSeungwon Jeon 		clksel = SDMMC_CLKSEL_UP_SAMPLE(
29480113132SSeungwon Jeon 				priv->hs400_timing, priv->tuned_sample);
29580113132SSeungwon Jeon 		wanted <<= 1;
29680113132SSeungwon Jeon 		break;
29780113132SSeungwon Jeon 	case MMC_TIMING_MMC_DDR52:
29880113132SSeungwon Jeon 		clksel = priv->ddr_timing;
29980113132SSeungwon Jeon 		/* Should be double rate for DDR mode */
30080113132SSeungwon Jeon 		if (ios->bus_width == MMC_BUS_WIDTH_8)
30180113132SSeungwon Jeon 			wanted <<= 1;
30280113132SSeungwon Jeon 		break;
30380113132SSeungwon Jeon 	default:
30480113132SSeungwon Jeon 		clksel = priv->sdr_timing;
30580113132SSeungwon Jeon 	}
30680113132SSeungwon Jeon 
30780113132SSeungwon Jeon 	/* Set clock timing for the requested speed mode*/
30880113132SSeungwon Jeon 	dw_mci_exynos_set_clksel_timing(host, clksel);
30980113132SSeungwon Jeon 
31080113132SSeungwon Jeon 	/* Configure setting for HS400 */
31180113132SSeungwon Jeon 	dw_mci_exynos_config_hs400(host, timing);
31280113132SSeungwon Jeon 
31380113132SSeungwon Jeon 	/* Configure clock rate */
31480113132SSeungwon Jeon 	dw_mci_exynos_adjust_clock(host, wanted);
315c6d9dedaSSeungwon Jeon }
316c6d9dedaSSeungwon Jeon 
317c3665006SThomas Abraham static int dw_mci_exynos_parse_dt(struct dw_mci *host)
318c3665006SThomas Abraham {
319e6c784edSYuvaraj Kumar C D 	struct dw_mci_exynos_priv_data *priv;
320c3665006SThomas Abraham 	struct device_node *np = host->dev->of_node;
321c3665006SThomas Abraham 	u32 timing[2];
322c3665006SThomas Abraham 	u32 div = 0;
323e6c784edSYuvaraj Kumar C D 	int idx;
324c3665006SThomas Abraham 	int ret;
325c3665006SThomas Abraham 
326e6c784edSYuvaraj Kumar C D 	priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
327bf3707eaSBeomho Seo 	if (!priv)
328e6c784edSYuvaraj Kumar C D 		return -ENOMEM;
329e6c784edSYuvaraj Kumar C D 
330e6c784edSYuvaraj Kumar C D 	for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
331e6c784edSYuvaraj Kumar C D 		if (of_device_is_compatible(np, exynos_compat[idx].compatible))
332e6c784edSYuvaraj Kumar C D 			priv->ctrl_type = exynos_compat[idx].ctrl_type;
333e6c784edSYuvaraj Kumar C D 	}
334e6c784edSYuvaraj Kumar C D 
335c6d9dedaSSeungwon Jeon 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
336c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1;
337c6d9dedaSSeungwon Jeon 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
338c6d9dedaSSeungwon Jeon 		priv->ciu_div = EXYNOS4210_FIXED_CIU_CLK_DIV - 1;
339c6d9dedaSSeungwon Jeon 	else {
340c3665006SThomas Abraham 		of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
341c3665006SThomas Abraham 		priv->ciu_div = div;
342c6d9dedaSSeungwon Jeon 	}
343c3665006SThomas Abraham 
344c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
345c3665006SThomas Abraham 			"samsung,dw-mshc-sdr-timing", timing, 2);
346c3665006SThomas Abraham 	if (ret)
347c3665006SThomas Abraham 		return ret;
348c3665006SThomas Abraham 
3492d9f0bd1SYuvaraj Kumar C D 	priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
3502d9f0bd1SYuvaraj Kumar C D 
351c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
352c3665006SThomas Abraham 			"samsung,dw-mshc-ddr-timing", timing, 2);
353c3665006SThomas Abraham 	if (ret)
354c3665006SThomas Abraham 		return ret;
355c3665006SThomas Abraham 
356c3665006SThomas Abraham 	priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
35780113132SSeungwon Jeon 
35880113132SSeungwon Jeon 	ret = of_property_read_u32_array(np,
35980113132SSeungwon Jeon 			"samsung,dw-mshc-hs400-timing", timing, 2);
36080113132SSeungwon Jeon 	if (!ret && of_property_read_u32(np,
36180113132SSeungwon Jeon 				"samsung,read-strobe-delay", &priv->dqs_delay))
36280113132SSeungwon Jeon 		dev_dbg(host->dev,
36380113132SSeungwon Jeon 			"read-strobe-delay is not found, assuming usage of default value\n");
36480113132SSeungwon Jeon 
36580113132SSeungwon Jeon 	priv->hs400_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1],
36680113132SSeungwon Jeon 						HS400_FIXED_CIU_CLK_DIV);
367e6c784edSYuvaraj Kumar C D 	host->priv = priv;
368c3665006SThomas Abraham 	return 0;
369c3665006SThomas Abraham }
370c3665006SThomas Abraham 
371c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_get_clksmpl(struct dw_mci *host)
372c537a1c5SSeungwon Jeon {
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 		return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL64));
37889ad2be7SAbhilash Kesavan 	else
379c537a1c5SSeungwon Jeon 		return SDMMC_CLKSEL_CCLK_SAMPLE(mci_readl(host, CLKSEL));
380c537a1c5SSeungwon Jeon }
381c537a1c5SSeungwon Jeon 
382c537a1c5SSeungwon Jeon static inline void dw_mci_exynos_set_clksmpl(struct dw_mci *host, u8 sample)
383c537a1c5SSeungwon Jeon {
384c537a1c5SSeungwon Jeon 	u32 clksel;
38589ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
38689ad2be7SAbhilash Kesavan 
38789ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
38889ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
38989ad2be7SAbhilash Kesavan 		clksel = mci_readl(host, CLKSEL64);
39089ad2be7SAbhilash Kesavan 	else
391c537a1c5SSeungwon Jeon 		clksel = mci_readl(host, CLKSEL);
39280113132SSeungwon Jeon 	clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
39389ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
39489ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
39589ad2be7SAbhilash Kesavan 		mci_writel(host, CLKSEL64, clksel);
39689ad2be7SAbhilash Kesavan 	else
397c537a1c5SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
398c537a1c5SSeungwon Jeon }
399c537a1c5SSeungwon Jeon 
400c537a1c5SSeungwon Jeon static inline u8 dw_mci_exynos_move_next_clksmpl(struct dw_mci *host)
401c537a1c5SSeungwon Jeon {
40289ad2be7SAbhilash Kesavan 	struct dw_mci_exynos_priv_data *priv = host->priv;
403c537a1c5SSeungwon Jeon 	u32 clksel;
404c537a1c5SSeungwon Jeon 	u8 sample;
405c537a1c5SSeungwon Jeon 
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 
412c537a1c5SSeungwon Jeon 	sample = (clksel + 1) & 0x7;
41380113132SSeungwon Jeon 	clksel = SDMMC_CLKSEL_UP_SAMPLE(clksel, sample);
41480113132SSeungwon Jeon 
41589ad2be7SAbhilash Kesavan 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS7 ||
41689ad2be7SAbhilash Kesavan 		priv->ctrl_type == DW_MCI_TYPE_EXYNOS7_SMU)
41789ad2be7SAbhilash Kesavan 		mci_writel(host, CLKSEL64, clksel);
41889ad2be7SAbhilash Kesavan 	else
419c537a1c5SSeungwon Jeon 		mci_writel(host, CLKSEL, clksel);
42080113132SSeungwon Jeon 
421c537a1c5SSeungwon Jeon 	return sample;
422c537a1c5SSeungwon Jeon }
423c537a1c5SSeungwon Jeon 
424c537a1c5SSeungwon Jeon static s8 dw_mci_exynos_get_best_clksmpl(u8 candiates)
425c537a1c5SSeungwon Jeon {
426c537a1c5SSeungwon Jeon 	const u8 iter = 8;
427c537a1c5SSeungwon Jeon 	u8 __c;
428c537a1c5SSeungwon Jeon 	s8 i, loc = -1;
429c537a1c5SSeungwon Jeon 
430c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
431c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
432c537a1c5SSeungwon Jeon 		if ((__c & 0xc7) == 0xc7) {
433c537a1c5SSeungwon Jeon 			loc = i;
434c537a1c5SSeungwon Jeon 			goto out;
435c537a1c5SSeungwon Jeon 		}
436c537a1c5SSeungwon Jeon 	}
437c537a1c5SSeungwon Jeon 
438c537a1c5SSeungwon Jeon 	for (i = 0; i < iter; i++) {
439c537a1c5SSeungwon Jeon 		__c = ror8(candiates, i);
440c537a1c5SSeungwon Jeon 		if ((__c & 0x83) == 0x83) {
441c537a1c5SSeungwon Jeon 			loc = i;
442c537a1c5SSeungwon Jeon 			goto out;
443c537a1c5SSeungwon Jeon 		}
444c537a1c5SSeungwon Jeon 	}
445c537a1c5SSeungwon Jeon 
446c537a1c5SSeungwon Jeon out:
447c537a1c5SSeungwon Jeon 	return loc;
448c537a1c5SSeungwon Jeon }
449c537a1c5SSeungwon Jeon 
4509979dbe5SChaotian Jing static int dw_mci_exynos_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
451c537a1c5SSeungwon Jeon {
452c537a1c5SSeungwon Jeon 	struct dw_mci *host = slot->host;
45380113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
454c537a1c5SSeungwon Jeon 	struct mmc_host *mmc = slot->mmc;
455c537a1c5SSeungwon Jeon 	u8 start_smpl, smpl, candiates = 0;
456c537a1c5SSeungwon Jeon 	s8 found = -1;
457c537a1c5SSeungwon Jeon 	int ret = 0;
458c537a1c5SSeungwon Jeon 
459c537a1c5SSeungwon Jeon 	start_smpl = dw_mci_exynos_get_clksmpl(host);
460c537a1c5SSeungwon Jeon 
461c537a1c5SSeungwon Jeon 	do {
462c537a1c5SSeungwon Jeon 		mci_writel(host, TMOUT, ~0);
463c537a1c5SSeungwon Jeon 		smpl = dw_mci_exynos_move_next_clksmpl(host);
464c537a1c5SSeungwon Jeon 
4659979dbe5SChaotian Jing 		if (!mmc_send_tuning(mmc, opcode, NULL))
466c537a1c5SSeungwon Jeon 			candiates |= (1 << smpl);
4676c2c6506SUlf Hansson 
468c537a1c5SSeungwon Jeon 	} while (start_smpl != smpl);
469c537a1c5SSeungwon Jeon 
470c537a1c5SSeungwon Jeon 	found = dw_mci_exynos_get_best_clksmpl(candiates);
47180113132SSeungwon Jeon 	if (found >= 0) {
472c537a1c5SSeungwon Jeon 		dw_mci_exynos_set_clksmpl(host, found);
47380113132SSeungwon Jeon 		priv->tuned_sample = found;
47480113132SSeungwon Jeon 	} else {
475c537a1c5SSeungwon Jeon 		ret = -EIO;
47680113132SSeungwon Jeon 	}
477c537a1c5SSeungwon Jeon 
478c537a1c5SSeungwon Jeon 	return ret;
479c537a1c5SSeungwon Jeon }
480c537a1c5SSeungwon Jeon 
481c22f5e1bSWu Fengguang static int dw_mci_exynos_prepare_hs400_tuning(struct dw_mci *host,
48280113132SSeungwon Jeon 					struct mmc_ios *ios)
48380113132SSeungwon Jeon {
48480113132SSeungwon Jeon 	struct dw_mci_exynos_priv_data *priv = host->priv;
48580113132SSeungwon Jeon 
48680113132SSeungwon Jeon 	dw_mci_exynos_set_clksel_timing(host, priv->hs400_timing);
48780113132SSeungwon Jeon 	dw_mci_exynos_adjust_clock(host, (ios->clock) << 1);
48880113132SSeungwon Jeon 
48980113132SSeungwon Jeon 	return 0;
49080113132SSeungwon Jeon }
49180113132SSeungwon Jeon 
4920f6e73d0SDongjin Kim /* Common capabilities of Exynos4/Exynos5 SoC */
4930f6e73d0SDongjin Kim static unsigned long exynos_dwmmc_caps[4] = {
494cab3a802SSeungwon Jeon 	MMC_CAP_1_8V_DDR | MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
495c3665006SThomas Abraham 	MMC_CAP_CMD23,
496c3665006SThomas Abraham 	MMC_CAP_CMD23,
497c3665006SThomas Abraham 	MMC_CAP_CMD23,
498c3665006SThomas Abraham };
499c3665006SThomas Abraham 
5000f6e73d0SDongjin Kim static const struct dw_mci_drv_data exynos_drv_data = {
5010f6e73d0SDongjin Kim 	.caps			= exynos_dwmmc_caps,
502c3665006SThomas Abraham 	.init			= dw_mci_exynos_priv_init,
503c3665006SThomas Abraham 	.setup_clock		= dw_mci_exynos_setup_clock,
504c3665006SThomas Abraham 	.set_ios		= dw_mci_exynos_set_ios,
505c3665006SThomas Abraham 	.parse_dt		= dw_mci_exynos_parse_dt,
506c537a1c5SSeungwon Jeon 	.execute_tuning		= dw_mci_exynos_execute_tuning,
50780113132SSeungwon Jeon 	.prepare_hs400_tuning	= dw_mci_exynos_prepare_hs400_tuning,
508c3665006SThomas Abraham };
509c3665006SThomas Abraham 
510c3665006SThomas Abraham static const struct of_device_id dw_mci_exynos_match[] = {
5110f6e73d0SDongjin Kim 	{ .compatible = "samsung,exynos4412-dw-mshc",
5120f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
513c3665006SThomas Abraham 	{ .compatible = "samsung,exynos5250-dw-mshc",
5140f6e73d0SDongjin Kim 			.data = &exynos_drv_data, },
51500fd041bSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc",
51600fd041bSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
5176bce431cSYuvaraj Kumar C D 	{ .compatible = "samsung,exynos5420-dw-mshc-smu",
5186bce431cSYuvaraj Kumar C D 			.data = &exynos_drv_data, },
51989ad2be7SAbhilash Kesavan 	{ .compatible = "samsung,exynos7-dw-mshc",
52089ad2be7SAbhilash Kesavan 			.data = &exynos_drv_data, },
52189ad2be7SAbhilash Kesavan 	{ .compatible = "samsung,exynos7-dw-mshc-smu",
52289ad2be7SAbhilash Kesavan 			.data = &exynos_drv_data, },
523c3665006SThomas Abraham 	{},
524c3665006SThomas Abraham };
525517cb9f1SArnd Bergmann MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
526c3665006SThomas Abraham 
5279665f7f2SSachin Kamat static int dw_mci_exynos_probe(struct platform_device *pdev)
528c3665006SThomas Abraham {
5298e2b36eaSArnd Bergmann 	const struct dw_mci_drv_data *drv_data;
530c3665006SThomas Abraham 	const struct of_device_id *match;
531c3665006SThomas Abraham 
532c3665006SThomas Abraham 	match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
533c3665006SThomas Abraham 	drv_data = match->data;
534c3665006SThomas Abraham 	return dw_mci_pltfm_register(pdev, drv_data);
535c3665006SThomas Abraham }
536c3665006SThomas Abraham 
53715a2e2abSSachin Kamat static const struct dev_pm_ops dw_mci_exynos_pmops = {
538e2c63599SDoug Anderson 	SET_SYSTEM_SLEEP_PM_OPS(dw_mci_exynos_suspend, dw_mci_exynos_resume)
539e2c63599SDoug Anderson 	.resume_noirq = dw_mci_exynos_resume_noirq,
540e2c63599SDoug Anderson 	.thaw_noirq = dw_mci_exynos_resume_noirq,
541e2c63599SDoug Anderson 	.restore_noirq = dw_mci_exynos_resume_noirq,
542e2c63599SDoug Anderson };
543e2c63599SDoug Anderson 
544c3665006SThomas Abraham static struct platform_driver dw_mci_exynos_pltfm_driver = {
545c3665006SThomas Abraham 	.probe		= dw_mci_exynos_probe,
5467d589edcSDmitry Torokhov 	.remove		= dw_mci_pltfm_remove,
547c3665006SThomas Abraham 	.driver		= {
548c3665006SThomas Abraham 		.name		= "dwmmc_exynos",
54920183d50SSachin Kamat 		.of_match_table	= dw_mci_exynos_match,
550e2c63599SDoug Anderson 		.pm		= &dw_mci_exynos_pmops,
551c3665006SThomas Abraham 	},
552c3665006SThomas Abraham };
553c3665006SThomas Abraham 
554c3665006SThomas Abraham module_platform_driver(dw_mci_exynos_pltfm_driver);
555c3665006SThomas Abraham 
556c3665006SThomas Abraham MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
557c3665006SThomas Abraham MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
558c3665006SThomas Abraham MODULE_LICENSE("GPL v2");
5592fc546fdSZhangfei Gao MODULE_ALIAS("platform:dwmmc_exynos");
560