xref: /OK3568_Linux_fs/kernel/drivers/mmc/host/sdhci-of-at91.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Atmel SDMMC controller driver.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2015 Atmel,
6*4882a593Smuzhiyun  *		 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/bitfield.h>
10*4882a593Smuzhiyun #include <linux/clk.h>
11*4882a593Smuzhiyun #include <linux/delay.h>
12*4882a593Smuzhiyun #include <linux/err.h>
13*4882a593Smuzhiyun #include <linux/io.h>
14*4882a593Smuzhiyun #include <linux/iopoll.h>
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/mmc/host.h>
17*4882a593Smuzhiyun #include <linux/mmc/slot-gpio.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/of.h>
20*4882a593Smuzhiyun #include <linux/of_device.h>
21*4882a593Smuzhiyun #include <linux/pm.h>
22*4882a593Smuzhiyun #include <linux/pm_runtime.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include "sdhci-pltfm.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define SDMMC_MC1R	0x204
27*4882a593Smuzhiyun #define		SDMMC_MC1R_DDR		BIT(3)
28*4882a593Smuzhiyun #define		SDMMC_MC1R_FCD		BIT(7)
29*4882a593Smuzhiyun #define SDMMC_CACR	0x230
30*4882a593Smuzhiyun #define		SDMMC_CACR_CAPWREN	BIT(0)
31*4882a593Smuzhiyun #define		SDMMC_CACR_KEY		(0x46 << 8)
32*4882a593Smuzhiyun #define SDMMC_CALCR	0x240
33*4882a593Smuzhiyun #define		SDMMC_CALCR_EN		BIT(0)
34*4882a593Smuzhiyun #define		SDMMC_CALCR_ALWYSON	BIT(4)
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define SDHCI_AT91_PRESET_COMMON_CONF	0x400 /* drv type B, programmable clock mode */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun struct sdhci_at91_soc_data {
39*4882a593Smuzhiyun 	const struct sdhci_pltfm_data *pdata;
40*4882a593Smuzhiyun 	bool baseclk_is_generated_internally;
41*4882a593Smuzhiyun 	unsigned int divider_for_baseclk;
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun struct sdhci_at91_priv {
45*4882a593Smuzhiyun 	const struct sdhci_at91_soc_data *soc_data;
46*4882a593Smuzhiyun 	struct clk *hclock;
47*4882a593Smuzhiyun 	struct clk *gck;
48*4882a593Smuzhiyun 	struct clk *mainck;
49*4882a593Smuzhiyun 	bool restore_needed;
50*4882a593Smuzhiyun 	bool cal_always_on;
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
sdhci_at91_set_force_card_detect(struct sdhci_host * host)53*4882a593Smuzhiyun static void sdhci_at91_set_force_card_detect(struct sdhci_host *host)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	u8 mc1r;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	mc1r = readb(host->ioaddr + SDMMC_MC1R);
58*4882a593Smuzhiyun 	mc1r |= SDMMC_MC1R_FCD;
59*4882a593Smuzhiyun 	writeb(mc1r, host->ioaddr + SDMMC_MC1R);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
sdhci_at91_set_clock(struct sdhci_host * host,unsigned int clock)62*4882a593Smuzhiyun static void sdhci_at91_set_clock(struct sdhci_host *host, unsigned int clock)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	u16 clk;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	host->mmc->actual_clock = 0;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	/*
69*4882a593Smuzhiyun 	 * There is no requirement to disable the internal clock before
70*4882a593Smuzhiyun 	 * changing the SD clock configuration. Moreover, disabling the
71*4882a593Smuzhiyun 	 * internal clock, changing the configuration and re-enabling the
72*4882a593Smuzhiyun 	 * internal clock causes some bugs. It can prevent to get the internal
73*4882a593Smuzhiyun 	 * clock stable flag ready and an unexpected switch to the base clock
74*4882a593Smuzhiyun 	 * when using presets.
75*4882a593Smuzhiyun 	 */
76*4882a593Smuzhiyun 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
77*4882a593Smuzhiyun 	clk &= SDHCI_CLOCK_INT_EN;
78*4882a593Smuzhiyun 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	if (clock == 0)
81*4882a593Smuzhiyun 		return;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	clk |= SDHCI_CLOCK_INT_EN;
86*4882a593Smuzhiyun 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	/* Wait max 20 ms */
89*4882a593Smuzhiyun 	if (read_poll_timeout(sdhci_readw, clk, (clk & SDHCI_CLOCK_INT_STABLE),
90*4882a593Smuzhiyun 			      1000, 20000, false, host, SDHCI_CLOCK_CONTROL)) {
91*4882a593Smuzhiyun 		pr_err("%s: Internal clock never stabilised.\n",
92*4882a593Smuzhiyun 		       mmc_hostname(host->mmc));
93*4882a593Smuzhiyun 		return;
94*4882a593Smuzhiyun 	}
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	clk |= SDHCI_CLOCK_CARD_EN;
97*4882a593Smuzhiyun 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
sdhci_at91_set_uhs_signaling(struct sdhci_host * host,unsigned int timing)100*4882a593Smuzhiyun static void sdhci_at91_set_uhs_signaling(struct sdhci_host *host,
101*4882a593Smuzhiyun 					 unsigned int timing)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	u8 mc1r;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	if (timing == MMC_TIMING_MMC_DDR52) {
106*4882a593Smuzhiyun 		mc1r = sdhci_readb(host, SDMMC_MC1R);
107*4882a593Smuzhiyun 		mc1r |= SDMMC_MC1R_DDR;
108*4882a593Smuzhiyun 		sdhci_writeb(host, mc1r, SDMMC_MC1R);
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 	sdhci_set_uhs_signaling(host, timing);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
sdhci_at91_reset(struct sdhci_host * host,u8 mask)113*4882a593Smuzhiyun static void sdhci_at91_reset(struct sdhci_host *host, u8 mask)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
116*4882a593Smuzhiyun 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
117*4882a593Smuzhiyun 	unsigned int tmp;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	sdhci_reset(host, mask);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
122*4882a593Smuzhiyun 	    || mmc_gpio_get_cd(host->mmc) >= 0)
123*4882a593Smuzhiyun 		sdhci_at91_set_force_card_detect(host);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (priv->cal_always_on && (mask & SDHCI_RESET_ALL)) {
126*4882a593Smuzhiyun 		u32 calcr = sdhci_readl(host, SDMMC_CALCR);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 		sdhci_writel(host, calcr | SDMMC_CALCR_ALWYSON | SDMMC_CALCR_EN,
129*4882a593Smuzhiyun 			     SDMMC_CALCR);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 		if (read_poll_timeout(sdhci_readl, tmp, !(tmp & SDMMC_CALCR_EN),
132*4882a593Smuzhiyun 				      10, 20000, false, host, SDMMC_CALCR))
133*4882a593Smuzhiyun 			dev_err(mmc_dev(host->mmc), "Failed to calibrate\n");
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun static const struct sdhci_ops sdhci_at91_sama5d2_ops = {
138*4882a593Smuzhiyun 	.set_clock		= sdhci_at91_set_clock,
139*4882a593Smuzhiyun 	.set_bus_width		= sdhci_set_bus_width,
140*4882a593Smuzhiyun 	.reset			= sdhci_at91_reset,
141*4882a593Smuzhiyun 	.set_uhs_signaling	= sdhci_at91_set_uhs_signaling,
142*4882a593Smuzhiyun 	.set_power		= sdhci_set_power_and_bus_voltage,
143*4882a593Smuzhiyun };
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun static const struct sdhci_pltfm_data sdhci_sama5d2_pdata = {
146*4882a593Smuzhiyun 	.ops = &sdhci_at91_sama5d2_ops,
147*4882a593Smuzhiyun };
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun static const struct sdhci_at91_soc_data soc_data_sama5d2 = {
150*4882a593Smuzhiyun 	.pdata = &sdhci_sama5d2_pdata,
151*4882a593Smuzhiyun 	.baseclk_is_generated_internally = false,
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun static const struct sdhci_at91_soc_data soc_data_sam9x60 = {
155*4882a593Smuzhiyun 	.pdata = &sdhci_sama5d2_pdata,
156*4882a593Smuzhiyun 	.baseclk_is_generated_internally = true,
157*4882a593Smuzhiyun 	.divider_for_baseclk = 2,
158*4882a593Smuzhiyun };
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun static const struct of_device_id sdhci_at91_dt_match[] = {
161*4882a593Smuzhiyun 	{ .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 },
162*4882a593Smuzhiyun 	{ .compatible = "microchip,sam9x60-sdhci", .data = &soc_data_sam9x60 },
163*4882a593Smuzhiyun 	{}
164*4882a593Smuzhiyun };
165*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, sdhci_at91_dt_match);
166*4882a593Smuzhiyun 
sdhci_at91_set_clks_presets(struct device * dev)167*4882a593Smuzhiyun static int sdhci_at91_set_clks_presets(struct device *dev)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	struct sdhci_host *host = dev_get_drvdata(dev);
170*4882a593Smuzhiyun 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
171*4882a593Smuzhiyun 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
172*4882a593Smuzhiyun 	unsigned int			caps0, caps1;
173*4882a593Smuzhiyun 	unsigned int			clk_base, clk_mul;
174*4882a593Smuzhiyun 	unsigned int			gck_rate, clk_base_rate;
175*4882a593Smuzhiyun 	unsigned int			preset_div;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	clk_prepare_enable(priv->hclock);
178*4882a593Smuzhiyun 	caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
179*4882a593Smuzhiyun 	caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	gck_rate = clk_get_rate(priv->gck);
182*4882a593Smuzhiyun 	if (priv->soc_data->baseclk_is_generated_internally)
183*4882a593Smuzhiyun 		clk_base_rate = gck_rate / priv->soc_data->divider_for_baseclk;
184*4882a593Smuzhiyun 	else
185*4882a593Smuzhiyun 		clk_base_rate = clk_get_rate(priv->mainck);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	clk_base = clk_base_rate / 1000000;
188*4882a593Smuzhiyun 	clk_mul = gck_rate / clk_base_rate - 1;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	caps0 &= ~SDHCI_CLOCK_V3_BASE_MASK;
191*4882a593Smuzhiyun 	caps0 |= FIELD_PREP(SDHCI_CLOCK_V3_BASE_MASK, clk_base);
192*4882a593Smuzhiyun 	caps1 &= ~SDHCI_CLOCK_MUL_MASK;
193*4882a593Smuzhiyun 	caps1 |= FIELD_PREP(SDHCI_CLOCK_MUL_MASK, clk_mul);
194*4882a593Smuzhiyun 	/* Set capabilities in r/w mode. */
195*4882a593Smuzhiyun 	writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR);
196*4882a593Smuzhiyun 	writel(caps0, host->ioaddr + SDHCI_CAPABILITIES);
197*4882a593Smuzhiyun 	writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1);
198*4882a593Smuzhiyun 	/* Set capabilities in ro mode. */
199*4882a593Smuzhiyun 	writel(0, host->ioaddr + SDMMC_CACR);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	dev_dbg(dev, "update clk mul to %u as gck rate is %u Hz and clk base is %u Hz\n",
202*4882a593Smuzhiyun 		clk_mul, gck_rate, clk_base_rate);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	/*
205*4882a593Smuzhiyun 	 * We have to set preset values because it depends on the clk_mul
206*4882a593Smuzhiyun 	 * value. Moreover, SDR104 is supported in a degraded mode since the
207*4882a593Smuzhiyun 	 * maximum sd clock value is 120 MHz instead of 208 MHz. For that
208*4882a593Smuzhiyun 	 * reason, we need to use presets to support SDR104.
209*4882a593Smuzhiyun 	 */
210*4882a593Smuzhiyun 	preset_div = DIV_ROUND_UP(gck_rate, 24000000) - 1;
211*4882a593Smuzhiyun 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
212*4882a593Smuzhiyun 	       host->ioaddr + SDHCI_PRESET_FOR_SDR12);
213*4882a593Smuzhiyun 	preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
214*4882a593Smuzhiyun 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
215*4882a593Smuzhiyun 	       host->ioaddr + SDHCI_PRESET_FOR_SDR25);
216*4882a593Smuzhiyun 	preset_div = DIV_ROUND_UP(gck_rate, 100000000) - 1;
217*4882a593Smuzhiyun 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
218*4882a593Smuzhiyun 	       host->ioaddr + SDHCI_PRESET_FOR_SDR50);
219*4882a593Smuzhiyun 	preset_div = DIV_ROUND_UP(gck_rate, 120000000) - 1;
220*4882a593Smuzhiyun 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
221*4882a593Smuzhiyun 	       host->ioaddr + SDHCI_PRESET_FOR_SDR104);
222*4882a593Smuzhiyun 	preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
223*4882a593Smuzhiyun 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
224*4882a593Smuzhiyun 	       host->ioaddr + SDHCI_PRESET_FOR_DDR50);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	clk_prepare_enable(priv->mainck);
227*4882a593Smuzhiyun 	clk_prepare_enable(priv->gck);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	return 0;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
sdhci_at91_suspend(struct device * dev)233*4882a593Smuzhiyun static int sdhci_at91_suspend(struct device *dev)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	struct sdhci_host *host = dev_get_drvdata(dev);
236*4882a593Smuzhiyun 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
237*4882a593Smuzhiyun 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
238*4882a593Smuzhiyun 	int ret;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	ret = pm_runtime_force_suspend(dev);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	priv->restore_needed = true;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	return ret;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun #ifdef CONFIG_PM
sdhci_at91_runtime_suspend(struct device * dev)249*4882a593Smuzhiyun static int sdhci_at91_runtime_suspend(struct device *dev)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	struct sdhci_host *host = dev_get_drvdata(dev);
252*4882a593Smuzhiyun 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
253*4882a593Smuzhiyun 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
254*4882a593Smuzhiyun 	int ret;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	ret = sdhci_runtime_suspend_host(host);
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
259*4882a593Smuzhiyun 		mmc_retune_needed(host->mmc);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	clk_disable_unprepare(priv->gck);
262*4882a593Smuzhiyun 	clk_disable_unprepare(priv->hclock);
263*4882a593Smuzhiyun 	clk_disable_unprepare(priv->mainck);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	return ret;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
sdhci_at91_runtime_resume(struct device * dev)268*4882a593Smuzhiyun static int sdhci_at91_runtime_resume(struct device *dev)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	struct sdhci_host *host = dev_get_drvdata(dev);
271*4882a593Smuzhiyun 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
272*4882a593Smuzhiyun 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
273*4882a593Smuzhiyun 	int ret;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (priv->restore_needed) {
276*4882a593Smuzhiyun 		ret = sdhci_at91_set_clks_presets(dev);
277*4882a593Smuzhiyun 		if (ret)
278*4882a593Smuzhiyun 			return ret;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		priv->restore_needed = false;
281*4882a593Smuzhiyun 		goto out;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	ret = clk_prepare_enable(priv->mainck);
285*4882a593Smuzhiyun 	if (ret) {
286*4882a593Smuzhiyun 		dev_err(dev, "can't enable mainck\n");
287*4882a593Smuzhiyun 		return ret;
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	ret = clk_prepare_enable(priv->hclock);
291*4882a593Smuzhiyun 	if (ret) {
292*4882a593Smuzhiyun 		dev_err(dev, "can't enable hclock\n");
293*4882a593Smuzhiyun 		return ret;
294*4882a593Smuzhiyun 	}
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	ret = clk_prepare_enable(priv->gck);
297*4882a593Smuzhiyun 	if (ret) {
298*4882a593Smuzhiyun 		dev_err(dev, "can't enable gck\n");
299*4882a593Smuzhiyun 		return ret;
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun out:
303*4882a593Smuzhiyun 	return sdhci_runtime_resume_host(host, 0);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun #endif /* CONFIG_PM */
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun static const struct dev_pm_ops sdhci_at91_dev_pm_ops = {
308*4882a593Smuzhiyun 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_at91_suspend, pm_runtime_force_resume)
309*4882a593Smuzhiyun 	SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend,
310*4882a593Smuzhiyun 			   sdhci_at91_runtime_resume,
311*4882a593Smuzhiyun 			   NULL)
312*4882a593Smuzhiyun };
313*4882a593Smuzhiyun 
sdhci_at91_probe(struct platform_device * pdev)314*4882a593Smuzhiyun static int sdhci_at91_probe(struct platform_device *pdev)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	const struct of_device_id	*match;
317*4882a593Smuzhiyun 	const struct sdhci_at91_soc_data	*soc_data;
318*4882a593Smuzhiyun 	struct sdhci_host		*host;
319*4882a593Smuzhiyun 	struct sdhci_pltfm_host		*pltfm_host;
320*4882a593Smuzhiyun 	struct sdhci_at91_priv		*priv;
321*4882a593Smuzhiyun 	int				ret;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	match = of_match_device(sdhci_at91_dt_match, &pdev->dev);
324*4882a593Smuzhiyun 	if (!match)
325*4882a593Smuzhiyun 		return -EINVAL;
326*4882a593Smuzhiyun 	soc_data = match->data;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*priv));
329*4882a593Smuzhiyun 	if (IS_ERR(host))
330*4882a593Smuzhiyun 		return PTR_ERR(host);
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	pltfm_host = sdhci_priv(host);
333*4882a593Smuzhiyun 	priv = sdhci_pltfm_priv(pltfm_host);
334*4882a593Smuzhiyun 	priv->soc_data = soc_data;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
337*4882a593Smuzhiyun 	if (IS_ERR(priv->mainck)) {
338*4882a593Smuzhiyun 		if (soc_data->baseclk_is_generated_internally) {
339*4882a593Smuzhiyun 			priv->mainck = NULL;
340*4882a593Smuzhiyun 		} else {
341*4882a593Smuzhiyun 			dev_err(&pdev->dev, "failed to get baseclk\n");
342*4882a593Smuzhiyun 			ret = PTR_ERR(priv->mainck);
343*4882a593Smuzhiyun 			goto sdhci_pltfm_free;
344*4882a593Smuzhiyun 		}
345*4882a593Smuzhiyun 	}
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	priv->hclock = devm_clk_get(&pdev->dev, "hclock");
348*4882a593Smuzhiyun 	if (IS_ERR(priv->hclock)) {
349*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to get hclock\n");
350*4882a593Smuzhiyun 		ret = PTR_ERR(priv->hclock);
351*4882a593Smuzhiyun 		goto sdhci_pltfm_free;
352*4882a593Smuzhiyun 	}
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	priv->gck = devm_clk_get(&pdev->dev, "multclk");
355*4882a593Smuzhiyun 	if (IS_ERR(priv->gck)) {
356*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to get multclk\n");
357*4882a593Smuzhiyun 		ret = PTR_ERR(priv->gck);
358*4882a593Smuzhiyun 		goto sdhci_pltfm_free;
359*4882a593Smuzhiyun 	}
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	ret = sdhci_at91_set_clks_presets(&pdev->dev);
362*4882a593Smuzhiyun 	if (ret)
363*4882a593Smuzhiyun 		goto sdhci_pltfm_free;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	priv->restore_needed = false;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/*
368*4882a593Smuzhiyun 	 * if SDCAL pin is wrongly connected, we must enable
369*4882a593Smuzhiyun 	 * the analog calibration cell permanently.
370*4882a593Smuzhiyun 	 */
371*4882a593Smuzhiyun 	priv->cal_always_on =
372*4882a593Smuzhiyun 		device_property_read_bool(&pdev->dev,
373*4882a593Smuzhiyun 					  "microchip,sdcal-inverted");
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	ret = mmc_of_parse(host->mmc);
376*4882a593Smuzhiyun 	if (ret)
377*4882a593Smuzhiyun 		goto clocks_disable_unprepare;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	sdhci_get_of_property(pdev);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	pm_runtime_get_noresume(&pdev->dev);
382*4882a593Smuzhiyun 	pm_runtime_set_active(&pdev->dev);
383*4882a593Smuzhiyun 	pm_runtime_enable(&pdev->dev);
384*4882a593Smuzhiyun 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
385*4882a593Smuzhiyun 	pm_runtime_use_autosuspend(&pdev->dev);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* HS200 is broken at this moment */
388*4882a593Smuzhiyun 	host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	ret = sdhci_add_host(host);
391*4882a593Smuzhiyun 	if (ret)
392*4882a593Smuzhiyun 		goto pm_runtime_disable;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/*
395*4882a593Smuzhiyun 	 * When calling sdhci_runtime_suspend_host(), the sdhci layer makes
396*4882a593Smuzhiyun 	 * the assumption that all the clocks of the controller are disabled.
397*4882a593Smuzhiyun 	 * It means we can't get irq from it when it is runtime suspended.
398*4882a593Smuzhiyun 	 * For that reason, it is not planned to wake-up on a card detect irq
399*4882a593Smuzhiyun 	 * from the controller.
400*4882a593Smuzhiyun 	 * If we want to use runtime PM and to be able to wake-up on card
401*4882a593Smuzhiyun 	 * insertion, we have to use a GPIO for the card detection or we can
402*4882a593Smuzhiyun 	 * use polling. Be aware that using polling will resume/suspend the
403*4882a593Smuzhiyun 	 * controller between each attempt.
404*4882a593Smuzhiyun 	 * Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries
405*4882a593Smuzhiyun 	 * to enable polling via device tree with broken-cd property.
406*4882a593Smuzhiyun 	 */
407*4882a593Smuzhiyun 	if (mmc_card_is_removable(host->mmc) &&
408*4882a593Smuzhiyun 	    mmc_gpio_get_cd(host->mmc) < 0) {
409*4882a593Smuzhiyun 		host->mmc->caps |= MMC_CAP_NEEDS_POLL;
410*4882a593Smuzhiyun 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
411*4882a593Smuzhiyun 	}
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	/*
414*4882a593Smuzhiyun 	 * If the device attached to the MMC bus is not removable, it is safer
415*4882a593Smuzhiyun 	 * to set the Force Card Detect bit. People often don't connect the
416*4882a593Smuzhiyun 	 * card detect signal and use this pin for another purpose. If the card
417*4882a593Smuzhiyun 	 * detect pin is not muxed to SDHCI controller, a default value is
418*4882a593Smuzhiyun 	 * used. This value can be different from a SoC revision to another
419*4882a593Smuzhiyun 	 * one. Problems come when this default value is not card present. To
420*4882a593Smuzhiyun 	 * avoid this case, if the device is non removable then the card
421*4882a593Smuzhiyun 	 * detection procedure using the SDMCC_CD signal is bypassed.
422*4882a593Smuzhiyun 	 * This bit is reset when a software reset for all command is performed
423*4882a593Smuzhiyun 	 * so we need to implement our own reset function to set back this bit.
424*4882a593Smuzhiyun 	 *
425*4882a593Smuzhiyun 	 * WA: SAMA5D2 doesn't drive CMD if using CD GPIO line.
426*4882a593Smuzhiyun 	 */
427*4882a593Smuzhiyun 	if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
428*4882a593Smuzhiyun 	    || mmc_gpio_get_cd(host->mmc) >= 0)
429*4882a593Smuzhiyun 		sdhci_at91_set_force_card_detect(host);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(&pdev->dev);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	return 0;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun pm_runtime_disable:
436*4882a593Smuzhiyun 	pm_runtime_disable(&pdev->dev);
437*4882a593Smuzhiyun 	pm_runtime_set_suspended(&pdev->dev);
438*4882a593Smuzhiyun 	pm_runtime_put_noidle(&pdev->dev);
439*4882a593Smuzhiyun clocks_disable_unprepare:
440*4882a593Smuzhiyun 	clk_disable_unprepare(priv->gck);
441*4882a593Smuzhiyun 	clk_disable_unprepare(priv->mainck);
442*4882a593Smuzhiyun 	clk_disable_unprepare(priv->hclock);
443*4882a593Smuzhiyun sdhci_pltfm_free:
444*4882a593Smuzhiyun 	sdhci_pltfm_free(pdev);
445*4882a593Smuzhiyun 	return ret;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun 
sdhci_at91_remove(struct platform_device * pdev)448*4882a593Smuzhiyun static int sdhci_at91_remove(struct platform_device *pdev)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun 	struct sdhci_host	*host = platform_get_drvdata(pdev);
451*4882a593Smuzhiyun 	struct sdhci_pltfm_host	*pltfm_host = sdhci_priv(host);
452*4882a593Smuzhiyun 	struct sdhci_at91_priv	*priv = sdhci_pltfm_priv(pltfm_host);
453*4882a593Smuzhiyun 	struct clk *gck = priv->gck;
454*4882a593Smuzhiyun 	struct clk *hclock = priv->hclock;
455*4882a593Smuzhiyun 	struct clk *mainck = priv->mainck;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	pm_runtime_get_sync(&pdev->dev);
458*4882a593Smuzhiyun 	pm_runtime_disable(&pdev->dev);
459*4882a593Smuzhiyun 	pm_runtime_put_noidle(&pdev->dev);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	sdhci_pltfm_unregister(pdev);
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	clk_disable_unprepare(gck);
464*4882a593Smuzhiyun 	clk_disable_unprepare(hclock);
465*4882a593Smuzhiyun 	clk_disable_unprepare(mainck);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	return 0;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun static struct platform_driver sdhci_at91_driver = {
471*4882a593Smuzhiyun 	.driver		= {
472*4882a593Smuzhiyun 		.name	= "sdhci-at91",
473*4882a593Smuzhiyun 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
474*4882a593Smuzhiyun 		.of_match_table = sdhci_at91_dt_match,
475*4882a593Smuzhiyun 		.pm	= &sdhci_at91_dev_pm_ops,
476*4882a593Smuzhiyun 	},
477*4882a593Smuzhiyun 	.probe		= sdhci_at91_probe,
478*4882a593Smuzhiyun 	.remove		= sdhci_at91_remove,
479*4882a593Smuzhiyun };
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun module_platform_driver(sdhci_at91_driver);
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun MODULE_DESCRIPTION("SDHCI driver for at91");
484*4882a593Smuzhiyun MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
485*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
486