xref: /linux/drivers/mmc/host/sdhci-of-at91.c (revision 21b2cec61c04bd175f0860d9411a472d5a0e7ba1)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2bb5f8ea4Sludovic.desroches@atmel.com /*
3bb5f8ea4Sludovic.desroches@atmel.com  * Atmel SDMMC controller driver.
4bb5f8ea4Sludovic.desroches@atmel.com  *
5bb5f8ea4Sludovic.desroches@atmel.com  * Copyright (C) 2015 Atmel,
6bb5f8ea4Sludovic.desroches@atmel.com  *		 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
7bb5f8ea4Sludovic.desroches@atmel.com  */
8bb5f8ea4Sludovic.desroches@atmel.com 
9a8e809ecSMasahiro Yamada #include <linux/bitfield.h>
10bb5f8ea4Sludovic.desroches@atmel.com #include <linux/clk.h>
114e289a7dSLudovic Desroches #include <linux/delay.h>
12bb5f8ea4Sludovic.desroches@atmel.com #include <linux/err.h>
13bb5f8ea4Sludovic.desroches@atmel.com #include <linux/io.h>
144406433dSLudovic Desroches #include <linux/kernel.h>
15bb5f8ea4Sludovic.desroches@atmel.com #include <linux/mmc/host.h>
1664e5cd72Sludovic.desroches@atmel.com #include <linux/mmc/slot-gpio.h>
17bb5f8ea4Sludovic.desroches@atmel.com #include <linux/module.h>
18bb5f8ea4Sludovic.desroches@atmel.com #include <linux/of.h>
19bb5f8ea4Sludovic.desroches@atmel.com #include <linux/of_device.h>
20f5f17813Sludovic.desroches@atmel.com #include <linux/pm.h>
21f5f17813Sludovic.desroches@atmel.com #include <linux/pm_runtime.h>
22bb5f8ea4Sludovic.desroches@atmel.com 
23bb5f8ea4Sludovic.desroches@atmel.com #include "sdhci-pltfm.h"
24bb5f8ea4Sludovic.desroches@atmel.com 
25d0918764SLudovic Desroches #define SDMMC_MC1R	0x204
26d0918764SLudovic Desroches #define		SDMMC_MC1R_DDR		BIT(3)
277a1e3f14SLudovic Desroches #define		SDMMC_MC1R_FCD		BIT(7)
28bb5f8ea4Sludovic.desroches@atmel.com #define SDMMC_CACR	0x230
29bb5f8ea4Sludovic.desroches@atmel.com #define		SDMMC_CACR_CAPWREN	BIT(0)
30bb5f8ea4Sludovic.desroches@atmel.com #define		SDMMC_CACR_KEY		(0x46 << 8)
31727d836aSNicolas Ferre #define SDMMC_CALCR	0x240
32727d836aSNicolas Ferre #define		SDMMC_CALCR_EN		BIT(0)
33727d836aSNicolas Ferre #define		SDMMC_CALCR_ALWYSON	BIT(4)
34bb5f8ea4Sludovic.desroches@atmel.com 
354406433dSLudovic Desroches #define SDHCI_AT91_PRESET_COMMON_CONF	0x400 /* drv type B, programmable clock mode */
364406433dSLudovic Desroches 
373976656dSLudovic Desroches struct sdhci_at91_soc_data {
383976656dSLudovic Desroches 	const struct sdhci_pltfm_data *pdata;
393976656dSLudovic Desroches 	bool baseclk_is_generated_internally;
403976656dSLudovic Desroches 	unsigned int divider_for_baseclk;
413976656dSLudovic Desroches };
423976656dSLudovic Desroches 
43bb5f8ea4Sludovic.desroches@atmel.com struct sdhci_at91_priv {
443976656dSLudovic Desroches 	const struct sdhci_at91_soc_data *soc_data;
45bb5f8ea4Sludovic.desroches@atmel.com 	struct clk *hclock;
46bb5f8ea4Sludovic.desroches@atmel.com 	struct clk *gck;
47bb5f8ea4Sludovic.desroches@atmel.com 	struct clk *mainck;
48e2b372ebSQuentin Schulz 	bool restore_needed;
49727d836aSNicolas Ferre 	bool cal_always_on;
50bb5f8ea4Sludovic.desroches@atmel.com };
51bb5f8ea4Sludovic.desroches@atmel.com 
527a1e3f14SLudovic Desroches static void sdhci_at91_set_force_card_detect(struct sdhci_host *host)
537a1e3f14SLudovic Desroches {
547a1e3f14SLudovic Desroches 	u8 mc1r;
557a1e3f14SLudovic Desroches 
567a1e3f14SLudovic Desroches 	mc1r = readb(host->ioaddr + SDMMC_MC1R);
577a1e3f14SLudovic Desroches 	mc1r |= SDMMC_MC1R_FCD;
587a1e3f14SLudovic Desroches 	writeb(mc1r, host->ioaddr + SDMMC_MC1R);
597a1e3f14SLudovic Desroches }
607a1e3f14SLudovic Desroches 
614e289a7dSLudovic Desroches static void sdhci_at91_set_clock(struct sdhci_host *host, unsigned int clock)
624e289a7dSLudovic Desroches {
634e289a7dSLudovic Desroches 	u16 clk;
644e289a7dSLudovic Desroches 	unsigned long timeout;
654e289a7dSLudovic Desroches 
664e289a7dSLudovic Desroches 	host->mmc->actual_clock = 0;
674e289a7dSLudovic Desroches 
684e289a7dSLudovic Desroches 	/*
694e289a7dSLudovic Desroches 	 * There is no requirement to disable the internal clock before
704e289a7dSLudovic Desroches 	 * changing the SD clock configuration. Moreover, disabling the
714e289a7dSLudovic Desroches 	 * internal clock, changing the configuration and re-enabling the
724e289a7dSLudovic Desroches 	 * internal clock causes some bugs. It can prevent to get the internal
734e289a7dSLudovic Desroches 	 * clock stable flag ready and an unexpected switch to the base clock
744e289a7dSLudovic Desroches 	 * when using presets.
754e289a7dSLudovic Desroches 	 */
764e289a7dSLudovic Desroches 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
774e289a7dSLudovic Desroches 	clk &= SDHCI_CLOCK_INT_EN;
784e289a7dSLudovic Desroches 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
794e289a7dSLudovic Desroches 
804e289a7dSLudovic Desroches 	if (clock == 0)
814e289a7dSLudovic Desroches 		return;
824e289a7dSLudovic Desroches 
834e289a7dSLudovic Desroches 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
844e289a7dSLudovic Desroches 
854e289a7dSLudovic Desroches 	clk |= SDHCI_CLOCK_INT_EN;
864e289a7dSLudovic Desroches 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
874e289a7dSLudovic Desroches 
884e289a7dSLudovic Desroches 	/* Wait max 20 ms */
894e289a7dSLudovic Desroches 	timeout = 20;
904e289a7dSLudovic Desroches 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
914e289a7dSLudovic Desroches 		& SDHCI_CLOCK_INT_STABLE)) {
924e289a7dSLudovic Desroches 		if (timeout == 0) {
934e289a7dSLudovic Desroches 			pr_err("%s: Internal clock never stabilised.\n",
944e289a7dSLudovic Desroches 			       mmc_hostname(host->mmc));
954e289a7dSLudovic Desroches 			return;
964e289a7dSLudovic Desroches 		}
974e289a7dSLudovic Desroches 		timeout--;
984e289a7dSLudovic Desroches 		mdelay(1);
994e289a7dSLudovic Desroches 	}
1004e289a7dSLudovic Desroches 
1014e289a7dSLudovic Desroches 	clk |= SDHCI_CLOCK_CARD_EN;
1024e289a7dSLudovic Desroches 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1034e289a7dSLudovic Desroches }
1044e289a7dSLudovic Desroches 
105519c51afSColin Ian King static void sdhci_at91_set_uhs_signaling(struct sdhci_host *host,
106519c51afSColin Ian King 					 unsigned int timing)
107d0918764SLudovic Desroches {
108d0918764SLudovic Desroches 	if (timing == MMC_TIMING_MMC_DDR52)
109d0918764SLudovic Desroches 		sdhci_writeb(host, SDMMC_MC1R_DDR, SDMMC_MC1R);
110d0918764SLudovic Desroches 	sdhci_set_uhs_signaling(host, timing);
111d0918764SLudovic Desroches }
112d0918764SLudovic Desroches 
1137a1e3f14SLudovic Desroches static void sdhci_at91_reset(struct sdhci_host *host, u8 mask)
1147a1e3f14SLudovic Desroches {
115727d836aSNicolas Ferre 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
116727d836aSNicolas Ferre 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
117727d836aSNicolas Ferre 
1187a1e3f14SLudovic Desroches 	sdhci_reset(host, mask);
1197a1e3f14SLudovic Desroches 
12053dd0a7cSMichał Mirosław 	if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
12153dd0a7cSMichał Mirosław 	    || mmc_gpio_get_cd(host->mmc) >= 0)
1227a1e3f14SLudovic Desroches 		sdhci_at91_set_force_card_detect(host);
123727d836aSNicolas Ferre 
124dbdea70fSEugen Hristev 	if (priv->cal_always_on && (mask & SDHCI_RESET_ALL)) {
125dbdea70fSEugen Hristev 		u32 calcr = sdhci_readl(host, SDMMC_CALCR);
126dbdea70fSEugen Hristev 
127dbdea70fSEugen Hristev 		sdhci_writel(host, calcr | SDMMC_CALCR_ALWYSON | SDMMC_CALCR_EN,
128727d836aSNicolas Ferre 			     SDMMC_CALCR);
1297a1e3f14SLudovic Desroches 	}
130dbdea70fSEugen Hristev }
1317a1e3f14SLudovic Desroches 
132bb5f8ea4Sludovic.desroches@atmel.com static const struct sdhci_ops sdhci_at91_sama5d2_ops = {
1334e289a7dSLudovic Desroches 	.set_clock		= sdhci_at91_set_clock,
134bb5f8ea4Sludovic.desroches@atmel.com 	.set_bus_width		= sdhci_set_bus_width,
1357a1e3f14SLudovic Desroches 	.reset			= sdhci_at91_reset,
136d0918764SLudovic Desroches 	.set_uhs_signaling	= sdhci_at91_set_uhs_signaling,
13798160562SNicolas Saenz Julienne 	.set_power		= sdhci_set_power_and_bus_voltage,
138bb5f8ea4Sludovic.desroches@atmel.com };
139bb5f8ea4Sludovic.desroches@atmel.com 
1403976656dSLudovic Desroches static const struct sdhci_pltfm_data sdhci_sama5d2_pdata = {
141bb5f8ea4Sludovic.desroches@atmel.com 	.ops = &sdhci_at91_sama5d2_ops,
142bb5f8ea4Sludovic.desroches@atmel.com };
143bb5f8ea4Sludovic.desroches@atmel.com 
1443976656dSLudovic Desroches static const struct sdhci_at91_soc_data soc_data_sama5d2 = {
1453976656dSLudovic Desroches 	.pdata = &sdhci_sama5d2_pdata,
1463976656dSLudovic Desroches 	.baseclk_is_generated_internally = false,
1473976656dSLudovic Desroches };
1483976656dSLudovic Desroches 
1493976656dSLudovic Desroches static const struct sdhci_at91_soc_data soc_data_sam9x60 = {
1503976656dSLudovic Desroches 	.pdata = &sdhci_sama5d2_pdata,
1513976656dSLudovic Desroches 	.baseclk_is_generated_internally = true,
1523976656dSLudovic Desroches 	.divider_for_baseclk = 2,
1533976656dSLudovic Desroches };
1543976656dSLudovic Desroches 
155bb5f8ea4Sludovic.desroches@atmel.com static const struct of_device_id sdhci_at91_dt_match[] = {
156bb5f8ea4Sludovic.desroches@atmel.com 	{ .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 },
1573976656dSLudovic Desroches 	{ .compatible = "microchip,sam9x60-sdhci", .data = &soc_data_sam9x60 },
158bb5f8ea4Sludovic.desroches@atmel.com 	{}
159bb5f8ea4Sludovic.desroches@atmel.com };
160d9943c68SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, sdhci_at91_dt_match);
161bb5f8ea4Sludovic.desroches@atmel.com 
162c8a019e7SQuentin Schulz static int sdhci_at91_set_clks_presets(struct device *dev)
163c8a019e7SQuentin Schulz {
164c8a019e7SQuentin Schulz 	struct sdhci_host *host = dev_get_drvdata(dev);
165c8a019e7SQuentin Schulz 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
166c8a019e7SQuentin Schulz 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
167c8a019e7SQuentin Schulz 	unsigned int			caps0, caps1;
168c8a019e7SQuentin Schulz 	unsigned int			clk_base, clk_mul;
1693976656dSLudovic Desroches 	unsigned int			gck_rate, clk_base_rate;
170c8a019e7SQuentin Schulz 	unsigned int			preset_div;
171c8a019e7SQuentin Schulz 
172c8a019e7SQuentin Schulz 	clk_prepare_enable(priv->hclock);
173c8a019e7SQuentin Schulz 	caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
174c8a019e7SQuentin Schulz 	caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
1753976656dSLudovic Desroches 
1763976656dSLudovic Desroches 	gck_rate = clk_get_rate(priv->gck);
1773976656dSLudovic Desroches 	if (priv->soc_data->baseclk_is_generated_internally)
1783976656dSLudovic Desroches 		clk_base_rate = gck_rate / priv->soc_data->divider_for_baseclk;
1793976656dSLudovic Desroches 	else
1803976656dSLudovic Desroches 		clk_base_rate = clk_get_rate(priv->mainck);
1813976656dSLudovic Desroches 
1823976656dSLudovic Desroches 	clk_base = clk_base_rate / 1000000;
1833976656dSLudovic Desroches 	clk_mul = gck_rate / clk_base_rate - 1;
1843976656dSLudovic Desroches 
1853976656dSLudovic Desroches 	caps0 &= ~SDHCI_CLOCK_V3_BASE_MASK;
186a8e809ecSMasahiro Yamada 	caps0 |= FIELD_PREP(SDHCI_CLOCK_V3_BASE_MASK, clk_base);
1873976656dSLudovic Desroches 	caps1 &= ~SDHCI_CLOCK_MUL_MASK;
188a8e809ecSMasahiro Yamada 	caps1 |= FIELD_PREP(SDHCI_CLOCK_MUL_MASK, clk_mul);
189c8a019e7SQuentin Schulz 	/* Set capabilities in r/w mode. */
1903976656dSLudovic Desroches 	writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR);
1913976656dSLudovic Desroches 	writel(caps0, host->ioaddr + SDHCI_CAPABILITIES);
192c8a019e7SQuentin Schulz 	writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1);
193c8a019e7SQuentin Schulz 	/* Set capabilities in ro mode. */
194c8a019e7SQuentin Schulz 	writel(0, host->ioaddr + SDMMC_CACR);
1953976656dSLudovic Desroches 
196fdd8eef4SCristian Birsan 	dev_dbg(dev, "update clk mul to %u as gck rate is %u Hz and clk base is %u Hz\n",
1973976656dSLudovic Desroches 		clk_mul, gck_rate, clk_base_rate);
198c8a019e7SQuentin Schulz 
199c8a019e7SQuentin Schulz 	/*
200c8a019e7SQuentin Schulz 	 * We have to set preset values because it depends on the clk_mul
201c8a019e7SQuentin Schulz 	 * value. Moreover, SDR104 is supported in a degraded mode since the
202c8a019e7SQuentin Schulz 	 * maximum sd clock value is 120 MHz instead of 208 MHz. For that
203c8a019e7SQuentin Schulz 	 * reason, we need to use presets to support SDR104.
204c8a019e7SQuentin Schulz 	 */
2053976656dSLudovic Desroches 	preset_div = DIV_ROUND_UP(gck_rate, 24000000) - 1;
206c8a019e7SQuentin Schulz 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
207c8a019e7SQuentin Schulz 	       host->ioaddr + SDHCI_PRESET_FOR_SDR12);
2083976656dSLudovic Desroches 	preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
209c8a019e7SQuentin Schulz 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
210c8a019e7SQuentin Schulz 	       host->ioaddr + SDHCI_PRESET_FOR_SDR25);
2113976656dSLudovic Desroches 	preset_div = DIV_ROUND_UP(gck_rate, 100000000) - 1;
212c8a019e7SQuentin Schulz 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
213c8a019e7SQuentin Schulz 	       host->ioaddr + SDHCI_PRESET_FOR_SDR50);
2143976656dSLudovic Desroches 	preset_div = DIV_ROUND_UP(gck_rate, 120000000) - 1;
215c8a019e7SQuentin Schulz 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
216c8a019e7SQuentin Schulz 	       host->ioaddr + SDHCI_PRESET_FOR_SDR104);
2173976656dSLudovic Desroches 	preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
218c8a019e7SQuentin Schulz 	writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
219c8a019e7SQuentin Schulz 	       host->ioaddr + SDHCI_PRESET_FOR_DDR50);
220c8a019e7SQuentin Schulz 
221c8a019e7SQuentin Schulz 	clk_prepare_enable(priv->mainck);
222c8a019e7SQuentin Schulz 	clk_prepare_enable(priv->gck);
223c8a019e7SQuentin Schulz 
224c8a019e7SQuentin Schulz 	return 0;
225c8a019e7SQuentin Schulz }
226c8a019e7SQuentin Schulz 
227e2b372ebSQuentin Schulz #ifdef CONFIG_PM_SLEEP
228e2b372ebSQuentin Schulz static int sdhci_at91_suspend(struct device *dev)
229e2b372ebSQuentin Schulz {
230e2b372ebSQuentin Schulz 	struct sdhci_host *host = dev_get_drvdata(dev);
231e2b372ebSQuentin Schulz 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
232e2b372ebSQuentin Schulz 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
233e2b372ebSQuentin Schulz 	int ret;
234e2b372ebSQuentin Schulz 
235e2b372ebSQuentin Schulz 	ret = pm_runtime_force_suspend(dev);
236e2b372ebSQuentin Schulz 
237e2b372ebSQuentin Schulz 	priv->restore_needed = true;
238e2b372ebSQuentin Schulz 
239e2b372ebSQuentin Schulz 	return ret;
240e2b372ebSQuentin Schulz }
241e2b372ebSQuentin Schulz #endif /* CONFIG_PM_SLEEP */
242e2b372ebSQuentin Schulz 
243f5f17813Sludovic.desroches@atmel.com #ifdef CONFIG_PM
244f5f17813Sludovic.desroches@atmel.com static int sdhci_at91_runtime_suspend(struct device *dev)
245f5f17813Sludovic.desroches@atmel.com {
246f5f17813Sludovic.desroches@atmel.com 	struct sdhci_host *host = dev_get_drvdata(dev);
247f5f17813Sludovic.desroches@atmel.com 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
24810f1c135SJisheng Zhang 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
249f5f17813Sludovic.desroches@atmel.com 	int ret;
250f5f17813Sludovic.desroches@atmel.com 
251f5f17813Sludovic.desroches@atmel.com 	ret = sdhci_runtime_suspend_host(host);
252f5f17813Sludovic.desroches@atmel.com 
253d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
254d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
255d38dcad4SAdrian Hunter 
256f5f17813Sludovic.desroches@atmel.com 	clk_disable_unprepare(priv->gck);
257f5f17813Sludovic.desroches@atmel.com 	clk_disable_unprepare(priv->hclock);
258f5f17813Sludovic.desroches@atmel.com 	clk_disable_unprepare(priv->mainck);
259f5f17813Sludovic.desroches@atmel.com 
260f5f17813Sludovic.desroches@atmel.com 	return ret;
261f5f17813Sludovic.desroches@atmel.com }
262f5f17813Sludovic.desroches@atmel.com 
263f5f17813Sludovic.desroches@atmel.com static int sdhci_at91_runtime_resume(struct device *dev)
264f5f17813Sludovic.desroches@atmel.com {
265f5f17813Sludovic.desroches@atmel.com 	struct sdhci_host *host = dev_get_drvdata(dev);
266f5f17813Sludovic.desroches@atmel.com 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
26710f1c135SJisheng Zhang 	struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
268f5f17813Sludovic.desroches@atmel.com 	int ret;
269f5f17813Sludovic.desroches@atmel.com 
270e2b372ebSQuentin Schulz 	if (priv->restore_needed) {
271e2b372ebSQuentin Schulz 		ret = sdhci_at91_set_clks_presets(dev);
272e2b372ebSQuentin Schulz 		if (ret)
273e2b372ebSQuentin Schulz 			return ret;
274e2b372ebSQuentin Schulz 
275e2b372ebSQuentin Schulz 		priv->restore_needed = false;
276e2b372ebSQuentin Schulz 		goto out;
277e2b372ebSQuentin Schulz 	}
278e2b372ebSQuentin Schulz 
279f5f17813Sludovic.desroches@atmel.com 	ret = clk_prepare_enable(priv->mainck);
280f5f17813Sludovic.desroches@atmel.com 	if (ret) {
281f5f17813Sludovic.desroches@atmel.com 		dev_err(dev, "can't enable mainck\n");
282f5f17813Sludovic.desroches@atmel.com 		return ret;
283f5f17813Sludovic.desroches@atmel.com 	}
284f5f17813Sludovic.desroches@atmel.com 
285f5f17813Sludovic.desroches@atmel.com 	ret = clk_prepare_enable(priv->hclock);
286f5f17813Sludovic.desroches@atmel.com 	if (ret) {
287f5f17813Sludovic.desroches@atmel.com 		dev_err(dev, "can't enable hclock\n");
288f5f17813Sludovic.desroches@atmel.com 		return ret;
289f5f17813Sludovic.desroches@atmel.com 	}
290f5f17813Sludovic.desroches@atmel.com 
291f5f17813Sludovic.desroches@atmel.com 	ret = clk_prepare_enable(priv->gck);
292f5f17813Sludovic.desroches@atmel.com 	if (ret) {
293f5f17813Sludovic.desroches@atmel.com 		dev_err(dev, "can't enable gck\n");
294f5f17813Sludovic.desroches@atmel.com 		return ret;
295f5f17813Sludovic.desroches@atmel.com 	}
296f5f17813Sludovic.desroches@atmel.com 
297e2b372ebSQuentin Schulz out:
298c6303c5dSBaolin Wang 	return sdhci_runtime_resume_host(host, 0);
299f5f17813Sludovic.desroches@atmel.com }
300f5f17813Sludovic.desroches@atmel.com #endif /* CONFIG_PM */
301f5f17813Sludovic.desroches@atmel.com 
302f5f17813Sludovic.desroches@atmel.com static const struct dev_pm_ops sdhci_at91_dev_pm_ops = {
303e2b372ebSQuentin Schulz 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_at91_suspend, pm_runtime_force_resume)
304f5f17813Sludovic.desroches@atmel.com 	SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend,
305f5f17813Sludovic.desroches@atmel.com 			   sdhci_at91_runtime_resume,
306f5f17813Sludovic.desroches@atmel.com 			   NULL)
307f5f17813Sludovic.desroches@atmel.com };
308f5f17813Sludovic.desroches@atmel.com 
309bb5f8ea4Sludovic.desroches@atmel.com static int sdhci_at91_probe(struct platform_device *pdev)
310bb5f8ea4Sludovic.desroches@atmel.com {
311bb5f8ea4Sludovic.desroches@atmel.com 	const struct of_device_id	*match;
3123976656dSLudovic Desroches 	const struct sdhci_at91_soc_data	*soc_data;
313bb5f8ea4Sludovic.desroches@atmel.com 	struct sdhci_host		*host;
314bb5f8ea4Sludovic.desroches@atmel.com 	struct sdhci_pltfm_host		*pltfm_host;
315bb5f8ea4Sludovic.desroches@atmel.com 	struct sdhci_at91_priv		*priv;
316bb5f8ea4Sludovic.desroches@atmel.com 	int				ret;
317bb5f8ea4Sludovic.desroches@atmel.com 
318bb5f8ea4Sludovic.desroches@atmel.com 	match = of_match_device(sdhci_at91_dt_match, &pdev->dev);
319bb5f8ea4Sludovic.desroches@atmel.com 	if (!match)
320bb5f8ea4Sludovic.desroches@atmel.com 		return -EINVAL;
321bb5f8ea4Sludovic.desroches@atmel.com 	soc_data = match->data;
322bb5f8ea4Sludovic.desroches@atmel.com 
3233976656dSLudovic Desroches 	host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*priv));
32410f1c135SJisheng Zhang 	if (IS_ERR(host))
32510f1c135SJisheng Zhang 		return PTR_ERR(host);
32610f1c135SJisheng Zhang 
32710f1c135SJisheng Zhang 	pltfm_host = sdhci_priv(host);
32810f1c135SJisheng Zhang 	priv = sdhci_pltfm_priv(pltfm_host);
3293976656dSLudovic Desroches 	priv->soc_data = soc_data;
330bb5f8ea4Sludovic.desroches@atmel.com 
331bb5f8ea4Sludovic.desroches@atmel.com 	priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
332bb5f8ea4Sludovic.desroches@atmel.com 	if (IS_ERR(priv->mainck)) {
3333976656dSLudovic Desroches 		if (soc_data->baseclk_is_generated_internally) {
3343976656dSLudovic Desroches 			priv->mainck = NULL;
3353976656dSLudovic Desroches 		} else {
336bb5f8ea4Sludovic.desroches@atmel.com 			dev_err(&pdev->dev, "failed to get baseclk\n");
337a04184ceSMichał Mirosław 			ret = PTR_ERR(priv->mainck);
338a04184ceSMichał Mirosław 			goto sdhci_pltfm_free;
339bb5f8ea4Sludovic.desroches@atmel.com 		}
3403976656dSLudovic Desroches 	}
341bb5f8ea4Sludovic.desroches@atmel.com 
342bb5f8ea4Sludovic.desroches@atmel.com 	priv->hclock = devm_clk_get(&pdev->dev, "hclock");
343bb5f8ea4Sludovic.desroches@atmel.com 	if (IS_ERR(priv->hclock)) {
344bb5f8ea4Sludovic.desroches@atmel.com 		dev_err(&pdev->dev, "failed to get hclock\n");
345a04184ceSMichał Mirosław 		ret = PTR_ERR(priv->hclock);
346a04184ceSMichał Mirosław 		goto sdhci_pltfm_free;
347bb5f8ea4Sludovic.desroches@atmel.com 	}
348bb5f8ea4Sludovic.desroches@atmel.com 
349bb5f8ea4Sludovic.desroches@atmel.com 	priv->gck = devm_clk_get(&pdev->dev, "multclk");
350bb5f8ea4Sludovic.desroches@atmel.com 	if (IS_ERR(priv->gck)) {
351bb5f8ea4Sludovic.desroches@atmel.com 		dev_err(&pdev->dev, "failed to get multclk\n");
352a04184ceSMichał Mirosław 		ret = PTR_ERR(priv->gck);
353a04184ceSMichał Mirosław 		goto sdhci_pltfm_free;
354bb5f8ea4Sludovic.desroches@atmel.com 	}
355bb5f8ea4Sludovic.desroches@atmel.com 
356c8a019e7SQuentin Schulz 	ret = sdhci_at91_set_clks_presets(&pdev->dev);
357c8a019e7SQuentin Schulz 	if (ret)
358c8a019e7SQuentin Schulz 		goto sdhci_pltfm_free;
359bb5f8ea4Sludovic.desroches@atmel.com 
360e2b372ebSQuentin Schulz 	priv->restore_needed = false;
361e2b372ebSQuentin Schulz 
362727d836aSNicolas Ferre 	/*
363727d836aSNicolas Ferre 	 * if SDCAL pin is wrongly connected, we must enable
364727d836aSNicolas Ferre 	 * the analog calibration cell permanently.
365727d836aSNicolas Ferre 	 */
366727d836aSNicolas Ferre 	priv->cal_always_on =
367727d836aSNicolas Ferre 		device_property_read_bool(&pdev->dev,
368727d836aSNicolas Ferre 					  "microchip,sdcal-inverted");
369727d836aSNicolas Ferre 
370bb5f8ea4Sludovic.desroches@atmel.com 	ret = mmc_of_parse(host->mmc);
371bb5f8ea4Sludovic.desroches@atmel.com 	if (ret)
372bb5f8ea4Sludovic.desroches@atmel.com 		goto clocks_disable_unprepare;
373bb5f8ea4Sludovic.desroches@atmel.com 
374bb5f8ea4Sludovic.desroches@atmel.com 	sdhci_get_of_property(pdev);
375bb5f8ea4Sludovic.desroches@atmel.com 
376f5f17813Sludovic.desroches@atmel.com 	pm_runtime_get_noresume(&pdev->dev);
377f5f17813Sludovic.desroches@atmel.com 	pm_runtime_set_active(&pdev->dev);
378f5f17813Sludovic.desroches@atmel.com 	pm_runtime_enable(&pdev->dev);
379f5f17813Sludovic.desroches@atmel.com 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
380f5f17813Sludovic.desroches@atmel.com 	pm_runtime_use_autosuspend(&pdev->dev);
381f5f17813Sludovic.desroches@atmel.com 
3827871aa60SEugen Hristev 	/* HS200 is broken at this moment */
383fed23c58SEugen Hristev 	host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
3847871aa60SEugen Hristev 
385bb5f8ea4Sludovic.desroches@atmel.com 	ret = sdhci_add_host(host);
386bb5f8ea4Sludovic.desroches@atmel.com 	if (ret)
387f5f17813Sludovic.desroches@atmel.com 		goto pm_runtime_disable;
388f5f17813Sludovic.desroches@atmel.com 
38964e5cd72Sludovic.desroches@atmel.com 	/*
39064e5cd72Sludovic.desroches@atmel.com 	 * When calling sdhci_runtime_suspend_host(), the sdhci layer makes
39164e5cd72Sludovic.desroches@atmel.com 	 * the assumption that all the clocks of the controller are disabled.
39264e5cd72Sludovic.desroches@atmel.com 	 * It means we can't get irq from it when it is runtime suspended.
39364e5cd72Sludovic.desroches@atmel.com 	 * For that reason, it is not planned to wake-up on a card detect irq
39464e5cd72Sludovic.desroches@atmel.com 	 * from the controller.
39564e5cd72Sludovic.desroches@atmel.com 	 * If we want to use runtime PM and to be able to wake-up on card
39664e5cd72Sludovic.desroches@atmel.com 	 * insertion, we have to use a GPIO for the card detection or we can
39764e5cd72Sludovic.desroches@atmel.com 	 * use polling. Be aware that using polling will resume/suspend the
39864e5cd72Sludovic.desroches@atmel.com 	 * controller between each attempt.
39964e5cd72Sludovic.desroches@atmel.com 	 * Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries
40064e5cd72Sludovic.desroches@atmel.com 	 * to enable polling via device tree with broken-cd property.
40164e5cd72Sludovic.desroches@atmel.com 	 */
402860951c5SJaehoon Chung 	if (mmc_card_is_removable(host->mmc) &&
403287980e4SArnd Bergmann 	    mmc_gpio_get_cd(host->mmc) < 0) {
40464e5cd72Sludovic.desroches@atmel.com 		host->mmc->caps |= MMC_CAP_NEEDS_POLL;
40564e5cd72Sludovic.desroches@atmel.com 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
40664e5cd72Sludovic.desroches@atmel.com 	}
40764e5cd72Sludovic.desroches@atmel.com 
4087a1e3f14SLudovic Desroches 	/*
4097a1e3f14SLudovic Desroches 	 * If the device attached to the MMC bus is not removable, it is safer
4107a1e3f14SLudovic Desroches 	 * to set the Force Card Detect bit. People often don't connect the
4117a1e3f14SLudovic Desroches 	 * card detect signal and use this pin for another purpose. If the card
4127a1e3f14SLudovic Desroches 	 * detect pin is not muxed to SDHCI controller, a default value is
4137a1e3f14SLudovic Desroches 	 * used. This value can be different from a SoC revision to another
4147a1e3f14SLudovic Desroches 	 * one. Problems come when this default value is not card present. To
4157a1e3f14SLudovic Desroches 	 * avoid this case, if the device is non removable then the card
4167a1e3f14SLudovic Desroches 	 * detection procedure using the SDMCC_CD signal is bypassed.
4177a1e3f14SLudovic Desroches 	 * This bit is reset when a software reset for all command is performed
4187a1e3f14SLudovic Desroches 	 * so we need to implement our own reset function to set back this bit.
41953dd0a7cSMichał Mirosław 	 *
42053dd0a7cSMichał Mirosław 	 * WA: SAMA5D2 doesn't drive CMD if using CD GPIO line.
4217a1e3f14SLudovic Desroches 	 */
42253dd0a7cSMichał Mirosław 	if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
42353dd0a7cSMichał Mirosław 	    || mmc_gpio_get_cd(host->mmc) >= 0)
4247a1e3f14SLudovic Desroches 		sdhci_at91_set_force_card_detect(host);
4257a1e3f14SLudovic Desroches 
426f5f17813Sludovic.desroches@atmel.com 	pm_runtime_put_autosuspend(&pdev->dev);
427bb5f8ea4Sludovic.desroches@atmel.com 
428bb5f8ea4Sludovic.desroches@atmel.com 	return 0;
429bb5f8ea4Sludovic.desroches@atmel.com 
430f5f17813Sludovic.desroches@atmel.com pm_runtime_disable:
431f5f17813Sludovic.desroches@atmel.com 	pm_runtime_disable(&pdev->dev);
432f5f17813Sludovic.desroches@atmel.com 	pm_runtime_set_suspended(&pdev->dev);
4332df9d58fSJisheng Zhang 	pm_runtime_put_noidle(&pdev->dev);
434bb5f8ea4Sludovic.desroches@atmel.com clocks_disable_unprepare:
435bb5f8ea4Sludovic.desroches@atmel.com 	clk_disable_unprepare(priv->gck);
436bb5f8ea4Sludovic.desroches@atmel.com 	clk_disable_unprepare(priv->mainck);
437bb5f8ea4Sludovic.desroches@atmel.com 	clk_disable_unprepare(priv->hclock);
438c8a019e7SQuentin Schulz sdhci_pltfm_free:
439bb5f8ea4Sludovic.desroches@atmel.com 	sdhci_pltfm_free(pdev);
440bb5f8ea4Sludovic.desroches@atmel.com 	return ret;
441bb5f8ea4Sludovic.desroches@atmel.com }
442bb5f8ea4Sludovic.desroches@atmel.com 
443bb5f8ea4Sludovic.desroches@atmel.com static int sdhci_at91_remove(struct platform_device *pdev)
444bb5f8ea4Sludovic.desroches@atmel.com {
445bb5f8ea4Sludovic.desroches@atmel.com 	struct sdhci_host	*host = platform_get_drvdata(pdev);
446bb5f8ea4Sludovic.desroches@atmel.com 	struct sdhci_pltfm_host	*pltfm_host = sdhci_priv(host);
44710f1c135SJisheng Zhang 	struct sdhci_at91_priv	*priv = sdhci_pltfm_priv(pltfm_host);
44810f1c135SJisheng Zhang 	struct clk *gck = priv->gck;
44910f1c135SJisheng Zhang 	struct clk *hclock = priv->hclock;
45010f1c135SJisheng Zhang 	struct clk *mainck = priv->mainck;
451bb5f8ea4Sludovic.desroches@atmel.com 
452f5f17813Sludovic.desroches@atmel.com 	pm_runtime_get_sync(&pdev->dev);
453f5f17813Sludovic.desroches@atmel.com 	pm_runtime_disable(&pdev->dev);
454f5f17813Sludovic.desroches@atmel.com 	pm_runtime_put_noidle(&pdev->dev);
455f5f17813Sludovic.desroches@atmel.com 
456bb5f8ea4Sludovic.desroches@atmel.com 	sdhci_pltfm_unregister(pdev);
457bb5f8ea4Sludovic.desroches@atmel.com 
45810f1c135SJisheng Zhang 	clk_disable_unprepare(gck);
45910f1c135SJisheng Zhang 	clk_disable_unprepare(hclock);
46010f1c135SJisheng Zhang 	clk_disable_unprepare(mainck);
461bb5f8ea4Sludovic.desroches@atmel.com 
462bb5f8ea4Sludovic.desroches@atmel.com 	return 0;
463bb5f8ea4Sludovic.desroches@atmel.com }
464bb5f8ea4Sludovic.desroches@atmel.com 
465bb5f8ea4Sludovic.desroches@atmel.com static struct platform_driver sdhci_at91_driver = {
466bb5f8ea4Sludovic.desroches@atmel.com 	.driver		= {
467bb5f8ea4Sludovic.desroches@atmel.com 		.name	= "sdhci-at91",
468*21b2cec6SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
469bb5f8ea4Sludovic.desroches@atmel.com 		.of_match_table = sdhci_at91_dt_match,
470f5f17813Sludovic.desroches@atmel.com 		.pm	= &sdhci_at91_dev_pm_ops,
471bb5f8ea4Sludovic.desroches@atmel.com 	},
472bb5f8ea4Sludovic.desroches@atmel.com 	.probe		= sdhci_at91_probe,
473bb5f8ea4Sludovic.desroches@atmel.com 	.remove		= sdhci_at91_remove,
474bb5f8ea4Sludovic.desroches@atmel.com };
475bb5f8ea4Sludovic.desroches@atmel.com 
476bb5f8ea4Sludovic.desroches@atmel.com module_platform_driver(sdhci_at91_driver);
477bb5f8ea4Sludovic.desroches@atmel.com 
478bb5f8ea4Sludovic.desroches@atmel.com MODULE_DESCRIPTION("SDHCI driver for at91");
479bb5f8ea4Sludovic.desroches@atmel.com MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
480bb5f8ea4Sludovic.desroches@atmel.com MODULE_LICENSE("GPL v2");
481