xref: /openbmc/linux/drivers/mmc/host/sdhci-omap.c (revision e0b2dbcf)
17d326930SKishon Vijay Abraham I /**
27d326930SKishon Vijay Abraham I  * SDHCI Controller driver for TI's OMAP SoCs
37d326930SKishon Vijay Abraham I  *
47d326930SKishon Vijay Abraham I  * Copyright (C) 2017 Texas Instruments
57d326930SKishon Vijay Abraham I  * Author: Kishon Vijay Abraham I <kishon@ti.com>
67d326930SKishon Vijay Abraham I  *
77d326930SKishon Vijay Abraham I  * This program is free software: you can redistribute it and/or modify
87d326930SKishon Vijay Abraham I  * it under the terms of the GNU General Public License version 2 of
97d326930SKishon Vijay Abraham I  * the License as published by the Free Software Foundation.
107d326930SKishon Vijay Abraham I  *
117d326930SKishon Vijay Abraham I  * This program is distributed in the hope that it will be useful,
127d326930SKishon Vijay Abraham I  * but WITHOUT ANY WARRANTY; without even the implied warranty of
137d326930SKishon Vijay Abraham I  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
147d326930SKishon Vijay Abraham I  * GNU General Public License for more details.
157d326930SKishon Vijay Abraham I  *
167d326930SKishon Vijay Abraham I  * You should have received a copy of the GNU General Public License
177d326930SKishon Vijay Abraham I  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
187d326930SKishon Vijay Abraham I  */
197d326930SKishon Vijay Abraham I 
207d326930SKishon Vijay Abraham I #include <linux/delay.h>
217d326930SKishon Vijay Abraham I #include <linux/mmc/slot-gpio.h>
227d326930SKishon Vijay Abraham I #include <linux/module.h>
237d326930SKishon Vijay Abraham I #include <linux/of.h>
247d326930SKishon Vijay Abraham I #include <linux/of_device.h>
257d326930SKishon Vijay Abraham I #include <linux/platform_device.h>
267d326930SKishon Vijay Abraham I #include <linux/pm_runtime.h>
277d326930SKishon Vijay Abraham I #include <linux/regulator/consumer.h>
288d20b2eaSKishon Vijay Abraham I #include <linux/pinctrl/consumer.h>
297d326930SKishon Vijay Abraham I 
307d326930SKishon Vijay Abraham I #include "sdhci-pltfm.h"
317d326930SKishon Vijay Abraham I 
327d326930SKishon Vijay Abraham I #define SDHCI_OMAP_CON		0x12c
337d326930SKishon Vijay Abraham I #define CON_DW8			BIT(5)
347d326930SKishon Vijay Abraham I #define CON_DMA_MASTER		BIT(20)
3527ceb7e0SKishon Vijay Abraham I #define CON_DDR			BIT(19)
3620ea26a1SKishon Vijay Abraham I #define CON_CLKEXTFREE		BIT(16)
3720ea26a1SKishon Vijay Abraham I #define CON_PADEN		BIT(15)
387d326930SKishon Vijay Abraham I #define CON_INIT		BIT(1)
397d326930SKishon Vijay Abraham I #define CON_OD			BIT(0)
407d326930SKishon Vijay Abraham I 
419fc2cd76SKishon Vijay Abraham I #define SDHCI_OMAP_DLL		0x0134
429fc2cd76SKishon Vijay Abraham I #define DLL_SWT			BIT(20)
439fc2cd76SKishon Vijay Abraham I #define DLL_FORCE_SR_C_SHIFT	13
449fc2cd76SKishon Vijay Abraham I #define DLL_FORCE_SR_C_MASK	(0x7f << DLL_FORCE_SR_C_SHIFT)
459fc2cd76SKishon Vijay Abraham I #define DLL_FORCE_VALUE		BIT(12)
469fc2cd76SKishon Vijay Abraham I #define DLL_CALIB		BIT(1)
479fc2cd76SKishon Vijay Abraham I 
487d326930SKishon Vijay Abraham I #define SDHCI_OMAP_CMD		0x20c
497d326930SKishon Vijay Abraham I 
5020ea26a1SKishon Vijay Abraham I #define SDHCI_OMAP_PSTATE	0x0224
5120ea26a1SKishon Vijay Abraham I #define PSTATE_DLEV_DAT0	BIT(20)
5220ea26a1SKishon Vijay Abraham I #define PSTATE_DATI		BIT(1)
5320ea26a1SKishon Vijay Abraham I 
547d326930SKishon Vijay Abraham I #define SDHCI_OMAP_HCTL		0x228
557d326930SKishon Vijay Abraham I #define HCTL_SDBP		BIT(8)
567d326930SKishon Vijay Abraham I #define HCTL_SDVS_SHIFT		9
577d326930SKishon Vijay Abraham I #define HCTL_SDVS_MASK		(0x7 << HCTL_SDVS_SHIFT)
587d326930SKishon Vijay Abraham I #define HCTL_SDVS_33		(0x7 << HCTL_SDVS_SHIFT)
597d326930SKishon Vijay Abraham I #define HCTL_SDVS_30		(0x6 << HCTL_SDVS_SHIFT)
607d326930SKishon Vijay Abraham I #define HCTL_SDVS_18		(0x5 << HCTL_SDVS_SHIFT)
617d326930SKishon Vijay Abraham I 
627d326930SKishon Vijay Abraham I #define SDHCI_OMAP_SYSCTL	0x22c
637d326930SKishon Vijay Abraham I #define SYSCTL_CEN		BIT(2)
647d326930SKishon Vijay Abraham I #define SYSCTL_CLKD_SHIFT	6
657d326930SKishon Vijay Abraham I #define SYSCTL_CLKD_MASK	0x3ff
667d326930SKishon Vijay Abraham I 
677d326930SKishon Vijay Abraham I #define SDHCI_OMAP_STAT		0x230
687d326930SKishon Vijay Abraham I 
697d326930SKishon Vijay Abraham I #define SDHCI_OMAP_IE		0x234
707d326930SKishon Vijay Abraham I #define INT_CC_EN		BIT(0)
717d326930SKishon Vijay Abraham I 
727d326930SKishon Vijay Abraham I #define SDHCI_OMAP_AC12		0x23c
737d326930SKishon Vijay Abraham I #define AC12_V1V8_SIGEN		BIT(19)
749fc2cd76SKishon Vijay Abraham I #define AC12_SCLK_SEL		BIT(23)
757d326930SKishon Vijay Abraham I 
767d326930SKishon Vijay Abraham I #define SDHCI_OMAP_CAPA		0x240
777d326930SKishon Vijay Abraham I #define CAPA_VS33		BIT(24)
787d326930SKishon Vijay Abraham I #define CAPA_VS30		BIT(25)
797d326930SKishon Vijay Abraham I #define CAPA_VS18		BIT(26)
807d326930SKishon Vijay Abraham I 
819fc2cd76SKishon Vijay Abraham I #define SDHCI_OMAP_CAPA2	0x0244
829fc2cd76SKishon Vijay Abraham I #define CAPA2_TSDR50		BIT(13)
839fc2cd76SKishon Vijay Abraham I 
847d326930SKishon Vijay Abraham I #define SDHCI_OMAP_TIMEOUT	1		/* 1 msec */
857d326930SKishon Vijay Abraham I 
867d326930SKishon Vijay Abraham I #define SYSCTL_CLKD_MAX		0x3FF
877d326930SKishon Vijay Abraham I 
887d326930SKishon Vijay Abraham I #define IOV_1V8			1800000		/* 180000 uV */
897d326930SKishon Vijay Abraham I #define IOV_3V0			3000000		/* 300000 uV */
907d326930SKishon Vijay Abraham I #define IOV_3V3			3300000		/* 330000 uV */
917d326930SKishon Vijay Abraham I 
929fc2cd76SKishon Vijay Abraham I #define MAX_PHASE_DELAY		0x7C
939fc2cd76SKishon Vijay Abraham I 
948d20b2eaSKishon Vijay Abraham I /* sdhci-omap controller flags */
958d20b2eaSKishon Vijay Abraham I #define SDHCI_OMAP_REQUIRE_IODELAY	BIT(0)
968d20b2eaSKishon Vijay Abraham I 
977d326930SKishon Vijay Abraham I struct sdhci_omap_data {
987d326930SKishon Vijay Abraham I 	u32 offset;
998d20b2eaSKishon Vijay Abraham I 	u8 flags;
1007d326930SKishon Vijay Abraham I };
1017d326930SKishon Vijay Abraham I 
1027d326930SKishon Vijay Abraham I struct sdhci_omap_host {
1037d326930SKishon Vijay Abraham I 	void __iomem		*base;
1047d326930SKishon Vijay Abraham I 	struct device		*dev;
1057d326930SKishon Vijay Abraham I 	struct	regulator	*pbias;
1067d326930SKishon Vijay Abraham I 	bool			pbias_enabled;
1077d326930SKishon Vijay Abraham I 	struct sdhci_host	*host;
1087d326930SKishon Vijay Abraham I 	u8			bus_mode;
1097d326930SKishon Vijay Abraham I 	u8			power_mode;
1108d20b2eaSKishon Vijay Abraham I 	u8			timing;
1118d20b2eaSKishon Vijay Abraham I 	u8			flags;
1128d20b2eaSKishon Vijay Abraham I 
1138d20b2eaSKishon Vijay Abraham I 	struct pinctrl		*pinctrl;
1148d20b2eaSKishon Vijay Abraham I 	struct pinctrl_state	**pinctrl_state;
1157d326930SKishon Vijay Abraham I };
1167d326930SKishon Vijay Abraham I 
1178d20b2eaSKishon Vijay Abraham I static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host);
1188d20b2eaSKishon Vijay Abraham I static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host);
1198d20b2eaSKishon Vijay Abraham I 
1207d326930SKishon Vijay Abraham I static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host,
1217d326930SKishon Vijay Abraham I 				   unsigned int offset)
1227d326930SKishon Vijay Abraham I {
1237d326930SKishon Vijay Abraham I 	return readl(host->base + offset);
1247d326930SKishon Vijay Abraham I }
1257d326930SKishon Vijay Abraham I 
1267d326930SKishon Vijay Abraham I static inline void sdhci_omap_writel(struct sdhci_omap_host *host,
1277d326930SKishon Vijay Abraham I 				     unsigned int offset, u32 data)
1287d326930SKishon Vijay Abraham I {
1297d326930SKishon Vijay Abraham I 	writel(data, host->base + offset);
1307d326930SKishon Vijay Abraham I }
1317d326930SKishon Vijay Abraham I 
1327d326930SKishon Vijay Abraham I static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host,
1337d326930SKishon Vijay Abraham I 				bool power_on, unsigned int iov)
1347d326930SKishon Vijay Abraham I {
1357d326930SKishon Vijay Abraham I 	int ret;
1367d326930SKishon Vijay Abraham I 	struct device *dev = omap_host->dev;
1377d326930SKishon Vijay Abraham I 
1387d326930SKishon Vijay Abraham I 	if (IS_ERR(omap_host->pbias))
1397d326930SKishon Vijay Abraham I 		return 0;
1407d326930SKishon Vijay Abraham I 
1417d326930SKishon Vijay Abraham I 	if (power_on) {
1427d326930SKishon Vijay Abraham I 		ret = regulator_set_voltage(omap_host->pbias, iov, iov);
1437d326930SKishon Vijay Abraham I 		if (ret) {
1447d326930SKishon Vijay Abraham I 			dev_err(dev, "pbias set voltage failed\n");
1457d326930SKishon Vijay Abraham I 			return ret;
1467d326930SKishon Vijay Abraham I 		}
1477d326930SKishon Vijay Abraham I 
1487d326930SKishon Vijay Abraham I 		if (omap_host->pbias_enabled)
1497d326930SKishon Vijay Abraham I 			return 0;
1507d326930SKishon Vijay Abraham I 
1517d326930SKishon Vijay Abraham I 		ret = regulator_enable(omap_host->pbias);
1527d326930SKishon Vijay Abraham I 		if (ret) {
1537d326930SKishon Vijay Abraham I 			dev_err(dev, "pbias reg enable fail\n");
1547d326930SKishon Vijay Abraham I 			return ret;
1557d326930SKishon Vijay Abraham I 		}
1567d326930SKishon Vijay Abraham I 
1577d326930SKishon Vijay Abraham I 		omap_host->pbias_enabled = true;
1587d326930SKishon Vijay Abraham I 	} else {
1597d326930SKishon Vijay Abraham I 		if (!omap_host->pbias_enabled)
1607d326930SKishon Vijay Abraham I 			return 0;
1617d326930SKishon Vijay Abraham I 
1627d326930SKishon Vijay Abraham I 		ret = regulator_disable(omap_host->pbias);
1637d326930SKishon Vijay Abraham I 		if (ret) {
1647d326930SKishon Vijay Abraham I 			dev_err(dev, "pbias reg disable fail\n");
1657d326930SKishon Vijay Abraham I 			return ret;
1667d326930SKishon Vijay Abraham I 		}
1677d326930SKishon Vijay Abraham I 		omap_host->pbias_enabled = false;
1687d326930SKishon Vijay Abraham I 	}
1697d326930SKishon Vijay Abraham I 
1707d326930SKishon Vijay Abraham I 	return 0;
1717d326930SKishon Vijay Abraham I }
1727d326930SKishon Vijay Abraham I 
1737d326930SKishon Vijay Abraham I static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host,
1747d326930SKishon Vijay Abraham I 				 unsigned int iov)
1757d326930SKishon Vijay Abraham I {
1767d326930SKishon Vijay Abraham I 	int ret;
1777d326930SKishon Vijay Abraham I 	struct sdhci_host *host = omap_host->host;
1787d326930SKishon Vijay Abraham I 	struct mmc_host *mmc = host->mmc;
1797d326930SKishon Vijay Abraham I 
1807d326930SKishon Vijay Abraham I 	ret = sdhci_omap_set_pbias(omap_host, false, 0);
1817d326930SKishon Vijay Abraham I 	if (ret)
1827d326930SKishon Vijay Abraham I 		return ret;
1837d326930SKishon Vijay Abraham I 
1847d326930SKishon Vijay Abraham I 	if (!IS_ERR(mmc->supply.vqmmc)) {
1857d326930SKishon Vijay Abraham I 		ret = regulator_set_voltage(mmc->supply.vqmmc, iov, iov);
1867d326930SKishon Vijay Abraham I 		if (ret) {
1877d326930SKishon Vijay Abraham I 			dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n");
1887d326930SKishon Vijay Abraham I 			return ret;
1897d326930SKishon Vijay Abraham I 		}
1907d326930SKishon Vijay Abraham I 	}
1917d326930SKishon Vijay Abraham I 
1927d326930SKishon Vijay Abraham I 	ret = sdhci_omap_set_pbias(omap_host, true, iov);
1937d326930SKishon Vijay Abraham I 	if (ret)
1947d326930SKishon Vijay Abraham I 		return ret;
1957d326930SKishon Vijay Abraham I 
1967d326930SKishon Vijay Abraham I 	return 0;
1977d326930SKishon Vijay Abraham I }
1987d326930SKishon Vijay Abraham I 
1997d326930SKishon Vijay Abraham I static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host,
2007d326930SKishon Vijay Abraham I 				      unsigned char signal_voltage)
2017d326930SKishon Vijay Abraham I {
2027d326930SKishon Vijay Abraham I 	u32 reg;
2037d326930SKishon Vijay Abraham I 	ktime_t timeout;
2047d326930SKishon Vijay Abraham I 
2057d326930SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
2067d326930SKishon Vijay Abraham I 	reg &= ~HCTL_SDVS_MASK;
2077d326930SKishon Vijay Abraham I 
2087d326930SKishon Vijay Abraham I 	if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
2097d326930SKishon Vijay Abraham I 		reg |= HCTL_SDVS_33;
2107d326930SKishon Vijay Abraham I 	else
2117d326930SKishon Vijay Abraham I 		reg |= HCTL_SDVS_18;
2127d326930SKishon Vijay Abraham I 
2137d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
2147d326930SKishon Vijay Abraham I 
2157d326930SKishon Vijay Abraham I 	reg |= HCTL_SDBP;
2167d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
2177d326930SKishon Vijay Abraham I 
2187d326930SKishon Vijay Abraham I 	/* wait 1ms */
2197d326930SKishon Vijay Abraham I 	timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
2207d326930SKishon Vijay Abraham I 	while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)) {
2217d326930SKishon Vijay Abraham I 		if (WARN_ON(ktime_after(ktime_get(), timeout)))
2227d326930SKishon Vijay Abraham I 			return;
2237d326930SKishon Vijay Abraham I 		usleep_range(5, 10);
2247d326930SKishon Vijay Abraham I 	}
2257d326930SKishon Vijay Abraham I }
2267d326930SKishon Vijay Abraham I 
2279fc2cd76SKishon Vijay Abraham I static inline void sdhci_omap_set_dll(struct sdhci_omap_host *omap_host,
2289fc2cd76SKishon Vijay Abraham I 				      int count)
2299fc2cd76SKishon Vijay Abraham I {
2309fc2cd76SKishon Vijay Abraham I 	int i;
2319fc2cd76SKishon Vijay Abraham I 	u32 reg;
2329fc2cd76SKishon Vijay Abraham I 
2339fc2cd76SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
2349fc2cd76SKishon Vijay Abraham I 	reg |= DLL_FORCE_VALUE;
2359fc2cd76SKishon Vijay Abraham I 	reg &= ~DLL_FORCE_SR_C_MASK;
2369fc2cd76SKishon Vijay Abraham I 	reg |= (count << DLL_FORCE_SR_C_SHIFT);
2379fc2cd76SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
2389fc2cd76SKishon Vijay Abraham I 
2399fc2cd76SKishon Vijay Abraham I 	reg |= DLL_CALIB;
2409fc2cd76SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
2419fc2cd76SKishon Vijay Abraham I 	for (i = 0; i < 1000; i++) {
2429fc2cd76SKishon Vijay Abraham I 		reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
2439fc2cd76SKishon Vijay Abraham I 		if (reg & DLL_CALIB)
2449fc2cd76SKishon Vijay Abraham I 			break;
2459fc2cd76SKishon Vijay Abraham I 	}
2469fc2cd76SKishon Vijay Abraham I 	reg &= ~DLL_CALIB;
2479fc2cd76SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
2489fc2cd76SKishon Vijay Abraham I }
2499fc2cd76SKishon Vijay Abraham I 
2509fc2cd76SKishon Vijay Abraham I static void sdhci_omap_disable_tuning(struct sdhci_omap_host *omap_host)
2519fc2cd76SKishon Vijay Abraham I {
2529fc2cd76SKishon Vijay Abraham I 	u32 reg;
2539fc2cd76SKishon Vijay Abraham I 
2549fc2cd76SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
2559fc2cd76SKishon Vijay Abraham I 	reg &= ~AC12_SCLK_SEL;
2569fc2cd76SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
2579fc2cd76SKishon Vijay Abraham I 
2589fc2cd76SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
2599fc2cd76SKishon Vijay Abraham I 	reg &= ~(DLL_FORCE_VALUE | DLL_SWT);
2609fc2cd76SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
2619fc2cd76SKishon Vijay Abraham I }
2629fc2cd76SKishon Vijay Abraham I 
2639fc2cd76SKishon Vijay Abraham I static int sdhci_omap_execute_tuning(struct mmc_host *mmc, u32 opcode)
2649fc2cd76SKishon Vijay Abraham I {
2659fc2cd76SKishon Vijay Abraham I 	struct sdhci_host *host = mmc_priv(mmc);
2669fc2cd76SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2679fc2cd76SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
2689fc2cd76SKishon Vijay Abraham I 	struct device *dev = omap_host->dev;
2699fc2cd76SKishon Vijay Abraham I 	struct mmc_ios *ios = &mmc->ios;
2709fc2cd76SKishon Vijay Abraham I 	u32 start_window = 0, max_window = 0;
2719fc2cd76SKishon Vijay Abraham I 	u8 cur_match, prev_match = 0;
2729fc2cd76SKishon Vijay Abraham I 	u32 length = 0, max_len = 0;
2737d33c358SKishon Vijay Abraham I 	u32 ier = host->ier;
2749fc2cd76SKishon Vijay Abraham I 	u32 phase_delay = 0;
2759fc2cd76SKishon Vijay Abraham I 	int ret = 0;
2769fc2cd76SKishon Vijay Abraham I 	u32 reg;
2779fc2cd76SKishon Vijay Abraham I 
2789fc2cd76SKishon Vijay Abraham I 	pltfm_host = sdhci_priv(host);
2799fc2cd76SKishon Vijay Abraham I 	omap_host = sdhci_pltfm_priv(pltfm_host);
2809fc2cd76SKishon Vijay Abraham I 	dev = omap_host->dev;
2819fc2cd76SKishon Vijay Abraham I 
2829fc2cd76SKishon Vijay Abraham I 	/* clock tuning is not needed for upto 52MHz */
2839fc2cd76SKishon Vijay Abraham I 	if (ios->clock <= 52000000)
2849fc2cd76SKishon Vijay Abraham I 		return 0;
2859fc2cd76SKishon Vijay Abraham I 
2869fc2cd76SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA2);
2879fc2cd76SKishon Vijay Abraham I 	if (ios->timing == MMC_TIMING_UHS_SDR50 && !(reg & CAPA2_TSDR50))
2889fc2cd76SKishon Vijay Abraham I 		return 0;
2899fc2cd76SKishon Vijay Abraham I 
2909fc2cd76SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
2919fc2cd76SKishon Vijay Abraham I 	reg |= DLL_SWT;
2929fc2cd76SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
2939fc2cd76SKishon Vijay Abraham I 
2947d33c358SKishon Vijay Abraham I 	/*
2957d33c358SKishon Vijay Abraham I 	 * OMAP5/DRA74X/DRA72x Errata i802:
2967d33c358SKishon Vijay Abraham I 	 * DCRC error interrupts (MMCHS_STAT[21] DCRC=0x1) can occur
2977d33c358SKishon Vijay Abraham I 	 * during the tuning procedure. So disable it during the
2987d33c358SKishon Vijay Abraham I 	 * tuning procedure.
2997d33c358SKishon Vijay Abraham I 	 */
3007d33c358SKishon Vijay Abraham I 	ier &= ~SDHCI_INT_DATA_CRC;
3017d33c358SKishon Vijay Abraham I 	sdhci_writel(host, ier, SDHCI_INT_ENABLE);
3027d33c358SKishon Vijay Abraham I 	sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
3037d33c358SKishon Vijay Abraham I 
3049fc2cd76SKishon Vijay Abraham I 	while (phase_delay <= MAX_PHASE_DELAY) {
3059fc2cd76SKishon Vijay Abraham I 		sdhci_omap_set_dll(omap_host, phase_delay);
3069fc2cd76SKishon Vijay Abraham I 
3079fc2cd76SKishon Vijay Abraham I 		cur_match = !mmc_send_tuning(mmc, opcode, NULL);
3089fc2cd76SKishon Vijay Abraham I 		if (cur_match) {
3099fc2cd76SKishon Vijay Abraham I 			if (prev_match) {
3109fc2cd76SKishon Vijay Abraham I 				length++;
3119fc2cd76SKishon Vijay Abraham I 			} else {
3129fc2cd76SKishon Vijay Abraham I 				start_window = phase_delay;
3139fc2cd76SKishon Vijay Abraham I 				length = 1;
3149fc2cd76SKishon Vijay Abraham I 			}
3159fc2cd76SKishon Vijay Abraham I 		}
3169fc2cd76SKishon Vijay Abraham I 
3179fc2cd76SKishon Vijay Abraham I 		if (length > max_len) {
3189fc2cd76SKishon Vijay Abraham I 			max_window = start_window;
3199fc2cd76SKishon Vijay Abraham I 			max_len = length;
3209fc2cd76SKishon Vijay Abraham I 		}
3219fc2cd76SKishon Vijay Abraham I 
3229fc2cd76SKishon Vijay Abraham I 		prev_match = cur_match;
3239fc2cd76SKishon Vijay Abraham I 		phase_delay += 4;
3249fc2cd76SKishon Vijay Abraham I 	}
3259fc2cd76SKishon Vijay Abraham I 
3269fc2cd76SKishon Vijay Abraham I 	if (!max_len) {
3279fc2cd76SKishon Vijay Abraham I 		dev_err(dev, "Unable to find match\n");
3289fc2cd76SKishon Vijay Abraham I 		ret = -EIO;
3299fc2cd76SKishon Vijay Abraham I 		goto tuning_error;
3309fc2cd76SKishon Vijay Abraham I 	}
3319fc2cd76SKishon Vijay Abraham I 
3329fc2cd76SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
3339fc2cd76SKishon Vijay Abraham I 	if (!(reg & AC12_SCLK_SEL)) {
3349fc2cd76SKishon Vijay Abraham I 		ret = -EIO;
3359fc2cd76SKishon Vijay Abraham I 		goto tuning_error;
3369fc2cd76SKishon Vijay Abraham I 	}
3379fc2cd76SKishon Vijay Abraham I 
3389fc2cd76SKishon Vijay Abraham I 	phase_delay = max_window + 4 * (max_len >> 1);
3399fc2cd76SKishon Vijay Abraham I 	sdhci_omap_set_dll(omap_host, phase_delay);
3409fc2cd76SKishon Vijay Abraham I 
3419fc2cd76SKishon Vijay Abraham I 	goto ret;
3429fc2cd76SKishon Vijay Abraham I 
3439fc2cd76SKishon Vijay Abraham I tuning_error:
3449fc2cd76SKishon Vijay Abraham I 	dev_err(dev, "Tuning failed\n");
3459fc2cd76SKishon Vijay Abraham I 	sdhci_omap_disable_tuning(omap_host);
3469fc2cd76SKishon Vijay Abraham I 
3479fc2cd76SKishon Vijay Abraham I ret:
3489fc2cd76SKishon Vijay Abraham I 	sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
3497d33c358SKishon Vijay Abraham I 	sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3507d33c358SKishon Vijay Abraham I 	sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
3519fc2cd76SKishon Vijay Abraham I 	return ret;
3529fc2cd76SKishon Vijay Abraham I }
3539fc2cd76SKishon Vijay Abraham I 
35420ea26a1SKishon Vijay Abraham I static int sdhci_omap_card_busy(struct mmc_host *mmc)
35520ea26a1SKishon Vijay Abraham I {
35620ea26a1SKishon Vijay Abraham I 	u32 reg, ac12;
35720ea26a1SKishon Vijay Abraham I 	int ret = false;
35820ea26a1SKishon Vijay Abraham I 	struct sdhci_host *host = mmc_priv(mmc);
35920ea26a1SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host;
36020ea26a1SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host;
36120ea26a1SKishon Vijay Abraham I 	u32 ier = host->ier;
36220ea26a1SKishon Vijay Abraham I 
36320ea26a1SKishon Vijay Abraham I 	pltfm_host = sdhci_priv(host);
36420ea26a1SKishon Vijay Abraham I 	omap_host = sdhci_pltfm_priv(pltfm_host);
36520ea26a1SKishon Vijay Abraham I 
36620ea26a1SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
36720ea26a1SKishon Vijay Abraham I 	ac12 = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
36820ea26a1SKishon Vijay Abraham I 	reg &= ~CON_CLKEXTFREE;
36920ea26a1SKishon Vijay Abraham I 	if (ac12 & AC12_V1V8_SIGEN)
37020ea26a1SKishon Vijay Abraham I 		reg |= CON_CLKEXTFREE;
37120ea26a1SKishon Vijay Abraham I 	reg |= CON_PADEN;
37220ea26a1SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
37320ea26a1SKishon Vijay Abraham I 
37420ea26a1SKishon Vijay Abraham I 	disable_irq(host->irq);
37520ea26a1SKishon Vijay Abraham I 	ier |= SDHCI_INT_CARD_INT;
37620ea26a1SKishon Vijay Abraham I 	sdhci_writel(host, ier, SDHCI_INT_ENABLE);
37720ea26a1SKishon Vijay Abraham I 	sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
37820ea26a1SKishon Vijay Abraham I 
37920ea26a1SKishon Vijay Abraham I 	/*
38020ea26a1SKishon Vijay Abraham I 	 * Delay is required for PSTATE to correctly reflect
38120ea26a1SKishon Vijay Abraham I 	 * DLEV/CLEV values after PADEN is set.
38220ea26a1SKishon Vijay Abraham I 	 */
38320ea26a1SKishon Vijay Abraham I 	usleep_range(50, 100);
38420ea26a1SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_PSTATE);
38520ea26a1SKishon Vijay Abraham I 	if ((reg & PSTATE_DATI) || !(reg & PSTATE_DLEV_DAT0))
38620ea26a1SKishon Vijay Abraham I 		ret = true;
38720ea26a1SKishon Vijay Abraham I 
38820ea26a1SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
38920ea26a1SKishon Vijay Abraham I 	reg &= ~(CON_CLKEXTFREE | CON_PADEN);
39020ea26a1SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
39120ea26a1SKishon Vijay Abraham I 
39220ea26a1SKishon Vijay Abraham I 	sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
39320ea26a1SKishon Vijay Abraham I 	sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
39420ea26a1SKishon Vijay Abraham I 	enable_irq(host->irq);
39520ea26a1SKishon Vijay Abraham I 
39620ea26a1SKishon Vijay Abraham I 	return ret;
39720ea26a1SKishon Vijay Abraham I }
39820ea26a1SKishon Vijay Abraham I 
3997d326930SKishon Vijay Abraham I static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
4007d326930SKishon Vijay Abraham I 						  struct mmc_ios *ios)
4017d326930SKishon Vijay Abraham I {
4027d326930SKishon Vijay Abraham I 	u32 reg;
4037d326930SKishon Vijay Abraham I 	int ret;
4047d326930SKishon Vijay Abraham I 	unsigned int iov;
4057d326930SKishon Vijay Abraham I 	struct sdhci_host *host = mmc_priv(mmc);
4067d326930SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host;
4077d326930SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host;
4087d326930SKishon Vijay Abraham I 	struct device *dev;
4097d326930SKishon Vijay Abraham I 
4107d326930SKishon Vijay Abraham I 	pltfm_host = sdhci_priv(host);
4117d326930SKishon Vijay Abraham I 	omap_host = sdhci_pltfm_priv(pltfm_host);
4127d326930SKishon Vijay Abraham I 	dev = omap_host->dev;
4137d326930SKishon Vijay Abraham I 
4147d326930SKishon Vijay Abraham I 	if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
4157d326930SKishon Vijay Abraham I 		reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
4167d326930SKishon Vijay Abraham I 		if (!(reg & CAPA_VS33))
4177d326930SKishon Vijay Abraham I 			return -EOPNOTSUPP;
4187d326930SKishon Vijay Abraham I 
4197d326930SKishon Vijay Abraham I 		sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
4207d326930SKishon Vijay Abraham I 
4217d326930SKishon Vijay Abraham I 		reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
4227d326930SKishon Vijay Abraham I 		reg &= ~AC12_V1V8_SIGEN;
4237d326930SKishon Vijay Abraham I 		sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
4247d326930SKishon Vijay Abraham I 
4257d326930SKishon Vijay Abraham I 		iov = IOV_3V3;
4267d326930SKishon Vijay Abraham I 	} else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
4277d326930SKishon Vijay Abraham I 		reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
4287d326930SKishon Vijay Abraham I 		if (!(reg & CAPA_VS18))
4297d326930SKishon Vijay Abraham I 			return -EOPNOTSUPP;
4307d326930SKishon Vijay Abraham I 
4317d326930SKishon Vijay Abraham I 		sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
4327d326930SKishon Vijay Abraham I 
4337d326930SKishon Vijay Abraham I 		reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
4347d326930SKishon Vijay Abraham I 		reg |= AC12_V1V8_SIGEN;
4357d326930SKishon Vijay Abraham I 		sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
4367d326930SKishon Vijay Abraham I 
4377d326930SKishon Vijay Abraham I 		iov = IOV_1V8;
4387d326930SKishon Vijay Abraham I 	} else {
4397d326930SKishon Vijay Abraham I 		return -EOPNOTSUPP;
4407d326930SKishon Vijay Abraham I 	}
4417d326930SKishon Vijay Abraham I 
4427d326930SKishon Vijay Abraham I 	ret = sdhci_omap_enable_iov(omap_host, iov);
4437d326930SKishon Vijay Abraham I 	if (ret) {
4447d326930SKishon Vijay Abraham I 		dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
4457d326930SKishon Vijay Abraham I 		return ret;
4467d326930SKishon Vijay Abraham I 	}
4477d326930SKishon Vijay Abraham I 
4487d326930SKishon Vijay Abraham I 	dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
4497d326930SKishon Vijay Abraham I 	return 0;
4507d326930SKishon Vijay Abraham I }
4517d326930SKishon Vijay Abraham I 
4528d20b2eaSKishon Vijay Abraham I static void sdhci_omap_set_timing(struct sdhci_omap_host *omap_host, u8 timing)
4538d20b2eaSKishon Vijay Abraham I {
4548d20b2eaSKishon Vijay Abraham I 	int ret;
4558d20b2eaSKishon Vijay Abraham I 	struct pinctrl_state *pinctrl_state;
4568d20b2eaSKishon Vijay Abraham I 	struct device *dev = omap_host->dev;
4578d20b2eaSKishon Vijay Abraham I 
4588d20b2eaSKishon Vijay Abraham I 	if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
4598d20b2eaSKishon Vijay Abraham I 		return;
4608d20b2eaSKishon Vijay Abraham I 
4618d20b2eaSKishon Vijay Abraham I 	if (omap_host->timing == timing)
4628d20b2eaSKishon Vijay Abraham I 		return;
4638d20b2eaSKishon Vijay Abraham I 
4648d20b2eaSKishon Vijay Abraham I 	sdhci_omap_stop_clock(omap_host);
4658d20b2eaSKishon Vijay Abraham I 
4668d20b2eaSKishon Vijay Abraham I 	pinctrl_state = omap_host->pinctrl_state[timing];
4678d20b2eaSKishon Vijay Abraham I 	ret = pinctrl_select_state(omap_host->pinctrl, pinctrl_state);
4688d20b2eaSKishon Vijay Abraham I 	if (ret) {
4698d20b2eaSKishon Vijay Abraham I 		dev_err(dev, "failed to select pinctrl state\n");
4708d20b2eaSKishon Vijay Abraham I 		return;
4718d20b2eaSKishon Vijay Abraham I 	}
4728d20b2eaSKishon Vijay Abraham I 
4738d20b2eaSKishon Vijay Abraham I 	sdhci_omap_start_clock(omap_host);
4748d20b2eaSKishon Vijay Abraham I 	omap_host->timing = timing;
4758d20b2eaSKishon Vijay Abraham I }
4768d20b2eaSKishon Vijay Abraham I 
477300df508SKishon Vijay Abraham I static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
478300df508SKishon Vijay Abraham I 				      u8 power_mode)
479300df508SKishon Vijay Abraham I {
4809fc2cd76SKishon Vijay Abraham I 	if (omap_host->bus_mode == MMC_POWER_OFF)
4819fc2cd76SKishon Vijay Abraham I 		sdhci_omap_disable_tuning(omap_host);
482300df508SKishon Vijay Abraham I 	omap_host->power_mode = power_mode;
483300df508SKishon Vijay Abraham I }
484300df508SKishon Vijay Abraham I 
4857d326930SKishon Vijay Abraham I static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
4867d326930SKishon Vijay Abraham I 				    unsigned int mode)
4877d326930SKishon Vijay Abraham I {
4887d326930SKishon Vijay Abraham I 	u32 reg;
4897d326930SKishon Vijay Abraham I 
4907d326930SKishon Vijay Abraham I 	if (omap_host->bus_mode == mode)
4917d326930SKishon Vijay Abraham I 		return;
4927d326930SKishon Vijay Abraham I 
4937d326930SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
4947d326930SKishon Vijay Abraham I 	if (mode == MMC_BUSMODE_OPENDRAIN)
4957d326930SKishon Vijay Abraham I 		reg |= CON_OD;
4967d326930SKishon Vijay Abraham I 	else
4977d326930SKishon Vijay Abraham I 		reg &= ~CON_OD;
4987d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
4997d326930SKishon Vijay Abraham I 
5007d326930SKishon Vijay Abraham I 	omap_host->bus_mode = mode;
5017d326930SKishon Vijay Abraham I }
5027d326930SKishon Vijay Abraham I 
503ddde0e7dSColin Ian King static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
5047d326930SKishon Vijay Abraham I {
5057d326930SKishon Vijay Abraham I 	struct sdhci_host *host = mmc_priv(mmc);
5067d326930SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host;
5077d326930SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host;
5087d326930SKishon Vijay Abraham I 
5097d326930SKishon Vijay Abraham I 	pltfm_host = sdhci_priv(host);
5107d326930SKishon Vijay Abraham I 	omap_host = sdhci_pltfm_priv(pltfm_host);
5117d326930SKishon Vijay Abraham I 
5127d326930SKishon Vijay Abraham I 	sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
5138d20b2eaSKishon Vijay Abraham I 	sdhci_omap_set_timing(omap_host, ios->timing);
5147d326930SKishon Vijay Abraham I 	sdhci_set_ios(mmc, ios);
515300df508SKishon Vijay Abraham I 	sdhci_omap_set_power_mode(omap_host, ios->power_mode);
5167d326930SKishon Vijay Abraham I }
5177d326930SKishon Vijay Abraham I 
5187d326930SKishon Vijay Abraham I static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
5197d326930SKishon Vijay Abraham I 				   unsigned int clock)
5207d326930SKishon Vijay Abraham I {
5217d326930SKishon Vijay Abraham I 	u16 dsor;
5227d326930SKishon Vijay Abraham I 
5237d326930SKishon Vijay Abraham I 	dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
5247d326930SKishon Vijay Abraham I 	if (dsor > SYSCTL_CLKD_MAX)
5257d326930SKishon Vijay Abraham I 		dsor = SYSCTL_CLKD_MAX;
5267d326930SKishon Vijay Abraham I 
5277d326930SKishon Vijay Abraham I 	return dsor;
5287d326930SKishon Vijay Abraham I }
5297d326930SKishon Vijay Abraham I 
5307d326930SKishon Vijay Abraham I static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
5317d326930SKishon Vijay Abraham I {
5327d326930SKishon Vijay Abraham I 	u32 reg;
5337d326930SKishon Vijay Abraham I 
5347d326930SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
5357d326930SKishon Vijay Abraham I 	reg |= SYSCTL_CEN;
5367d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
5377d326930SKishon Vijay Abraham I }
5387d326930SKishon Vijay Abraham I 
5397d326930SKishon Vijay Abraham I static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
5407d326930SKishon Vijay Abraham I {
5417d326930SKishon Vijay Abraham I 	u32 reg;
5427d326930SKishon Vijay Abraham I 
5437d326930SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
5447d326930SKishon Vijay Abraham I 	reg &= ~SYSCTL_CEN;
5457d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
5467d326930SKishon Vijay Abraham I }
5477d326930SKishon Vijay Abraham I 
5487d326930SKishon Vijay Abraham I static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
5497d326930SKishon Vijay Abraham I {
5507d326930SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
5517d326930SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
5527d326930SKishon Vijay Abraham I 	unsigned long clkdiv;
5537d326930SKishon Vijay Abraham I 
5547d326930SKishon Vijay Abraham I 	sdhci_omap_stop_clock(omap_host);
5557d326930SKishon Vijay Abraham I 
5567d326930SKishon Vijay Abraham I 	if (!clock)
5577d326930SKishon Vijay Abraham I 		return;
5587d326930SKishon Vijay Abraham I 
5597d326930SKishon Vijay Abraham I 	clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
5607d326930SKishon Vijay Abraham I 	clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
5617d326930SKishon Vijay Abraham I 	sdhci_enable_clk(host, clkdiv);
5627d326930SKishon Vijay Abraham I 
5637d326930SKishon Vijay Abraham I 	sdhci_omap_start_clock(omap_host);
5647d326930SKishon Vijay Abraham I }
5657d326930SKishon Vijay Abraham I 
566ddde0e7dSColin Ian King static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
5677d326930SKishon Vijay Abraham I 			  unsigned short vdd)
5687d326930SKishon Vijay Abraham I {
5697d326930SKishon Vijay Abraham I 	struct mmc_host *mmc = host->mmc;
5707d326930SKishon Vijay Abraham I 
5717d326930SKishon Vijay Abraham I 	mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
5727d326930SKishon Vijay Abraham I }
5737d326930SKishon Vijay Abraham I 
5747d326930SKishon Vijay Abraham I static int sdhci_omap_enable_dma(struct sdhci_host *host)
5757d326930SKishon Vijay Abraham I {
5767d326930SKishon Vijay Abraham I 	u32 reg;
5777d326930SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
5787d326930SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
5797d326930SKishon Vijay Abraham I 
5807d326930SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
5817d326930SKishon Vijay Abraham I 	reg |= CON_DMA_MASTER;
5827d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
5837d326930SKishon Vijay Abraham I 
5847d326930SKishon Vijay Abraham I 	return 0;
5857d326930SKishon Vijay Abraham I }
5867d326930SKishon Vijay Abraham I 
587ddde0e7dSColin Ian King static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
5887d326930SKishon Vijay Abraham I {
5897d326930SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
5907d326930SKishon Vijay Abraham I 
5917d326930SKishon Vijay Abraham I 	return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
5927d326930SKishon Vijay Abraham I }
5937d326930SKishon Vijay Abraham I 
5947d326930SKishon Vijay Abraham I static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
5957d326930SKishon Vijay Abraham I {
5967d326930SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
5977d326930SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
5987d326930SKishon Vijay Abraham I 	u32 reg;
5997d326930SKishon Vijay Abraham I 
6007d326930SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
6017d326930SKishon Vijay Abraham I 	if (width == MMC_BUS_WIDTH_8)
6027d326930SKishon Vijay Abraham I 		reg |= CON_DW8;
6037d326930SKishon Vijay Abraham I 	else
6047d326930SKishon Vijay Abraham I 		reg &= ~CON_DW8;
6057d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
6067d326930SKishon Vijay Abraham I 
6077d326930SKishon Vijay Abraham I 	sdhci_set_bus_width(host, width);
6087d326930SKishon Vijay Abraham I }
6097d326930SKishon Vijay Abraham I 
6107d326930SKishon Vijay Abraham I static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
6117d326930SKishon Vijay Abraham I {
6127d326930SKishon Vijay Abraham I 	u32 reg;
6137d326930SKishon Vijay Abraham I 	ktime_t timeout;
6147d326930SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
6157d326930SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
6167d326930SKishon Vijay Abraham I 
6177d326930SKishon Vijay Abraham I 	if (omap_host->power_mode == power_mode)
6187d326930SKishon Vijay Abraham I 		return;
6197d326930SKishon Vijay Abraham I 
6207d326930SKishon Vijay Abraham I 	if (power_mode != MMC_POWER_ON)
6217d326930SKishon Vijay Abraham I 		return;
6227d326930SKishon Vijay Abraham I 
6237d326930SKishon Vijay Abraham I 	disable_irq(host->irq);
6247d326930SKishon Vijay Abraham I 
6257d326930SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
6267d326930SKishon Vijay Abraham I 	reg |= CON_INIT;
6277d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
6287d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
6297d326930SKishon Vijay Abraham I 
6307d326930SKishon Vijay Abraham I 	/* wait 1ms */
6317d326930SKishon Vijay Abraham I 	timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
6327d326930SKishon Vijay Abraham I 	while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)) {
6337d326930SKishon Vijay Abraham I 		if (WARN_ON(ktime_after(ktime_get(), timeout)))
6347d326930SKishon Vijay Abraham I 			return;
6357d326930SKishon Vijay Abraham I 		usleep_range(5, 10);
6367d326930SKishon Vijay Abraham I 	}
6377d326930SKishon Vijay Abraham I 
6387d326930SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
6397d326930SKishon Vijay Abraham I 	reg &= ~CON_INIT;
6407d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
6417d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
6427d326930SKishon Vijay Abraham I 
6437d326930SKishon Vijay Abraham I 	enable_irq(host->irq);
6447d326930SKishon Vijay Abraham I }
6457d326930SKishon Vijay Abraham I 
64627ceb7e0SKishon Vijay Abraham I static void sdhci_omap_set_uhs_signaling(struct sdhci_host *host,
64727ceb7e0SKishon Vijay Abraham I 					 unsigned int timing)
64827ceb7e0SKishon Vijay Abraham I {
64927ceb7e0SKishon Vijay Abraham I 	u32 reg;
65027ceb7e0SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
65127ceb7e0SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
65227ceb7e0SKishon Vijay Abraham I 
65327ceb7e0SKishon Vijay Abraham I 	sdhci_omap_stop_clock(omap_host);
65427ceb7e0SKishon Vijay Abraham I 
65527ceb7e0SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
65627ceb7e0SKishon Vijay Abraham I 	if (timing == MMC_TIMING_UHS_DDR50 || timing == MMC_TIMING_MMC_DDR52)
65727ceb7e0SKishon Vijay Abraham I 		reg |= CON_DDR;
65827ceb7e0SKishon Vijay Abraham I 	else
65927ceb7e0SKishon Vijay Abraham I 		reg &= ~CON_DDR;
66027ceb7e0SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
66127ceb7e0SKishon Vijay Abraham I 
66227ceb7e0SKishon Vijay Abraham I 	sdhci_set_uhs_signaling(host, timing);
66327ceb7e0SKishon Vijay Abraham I 	sdhci_omap_start_clock(omap_host);
66427ceb7e0SKishon Vijay Abraham I }
66527ceb7e0SKishon Vijay Abraham I 
6667d326930SKishon Vijay Abraham I static struct sdhci_ops sdhci_omap_ops = {
6677d326930SKishon Vijay Abraham I 	.set_clock = sdhci_omap_set_clock,
6687d326930SKishon Vijay Abraham I 	.set_power = sdhci_omap_set_power,
6697d326930SKishon Vijay Abraham I 	.enable_dma = sdhci_omap_enable_dma,
6707d326930SKishon Vijay Abraham I 	.get_max_clock = sdhci_pltfm_clk_get_max_clock,
6717d326930SKishon Vijay Abraham I 	.get_min_clock = sdhci_omap_get_min_clock,
6727d326930SKishon Vijay Abraham I 	.set_bus_width = sdhci_omap_set_bus_width,
6737d326930SKishon Vijay Abraham I 	.platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
6747d326930SKishon Vijay Abraham I 	.reset = sdhci_reset,
67527ceb7e0SKishon Vijay Abraham I 	.set_uhs_signaling = sdhci_omap_set_uhs_signaling,
6767d326930SKishon Vijay Abraham I };
6777d326930SKishon Vijay Abraham I 
6787d326930SKishon Vijay Abraham I static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host)
6797d326930SKishon Vijay Abraham I {
6807d326930SKishon Vijay Abraham I 	u32 reg;
6817d326930SKishon Vijay Abraham I 	int ret = 0;
6827d326930SKishon Vijay Abraham I 	struct device *dev = omap_host->dev;
6837d326930SKishon Vijay Abraham I 	struct regulator *vqmmc;
6847d326930SKishon Vijay Abraham I 
6857d326930SKishon Vijay Abraham I 	vqmmc = regulator_get(dev, "vqmmc");
6867d326930SKishon Vijay Abraham I 	if (IS_ERR(vqmmc)) {
6877d326930SKishon Vijay Abraham I 		ret = PTR_ERR(vqmmc);
6887d326930SKishon Vijay Abraham I 		goto reg_put;
6897d326930SKishon Vijay Abraham I 	}
6907d326930SKishon Vijay Abraham I 
6917d326930SKishon Vijay Abraham I 	/* voltage capabilities might be set by boot loader, clear it */
6927d326930SKishon Vijay Abraham I 	reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
6937d326930SKishon Vijay Abraham I 	reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
6947d326930SKishon Vijay Abraham I 
6957d326930SKishon Vijay Abraham I 	if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3))
6967d326930SKishon Vijay Abraham I 		reg |= CAPA_VS33;
6977d326930SKishon Vijay Abraham I 	if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8))
6987d326930SKishon Vijay Abraham I 		reg |= CAPA_VS18;
6997d326930SKishon Vijay Abraham I 
7007d326930SKishon Vijay Abraham I 	sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
7017d326930SKishon Vijay Abraham I 
7027d326930SKishon Vijay Abraham I reg_put:
7037d326930SKishon Vijay Abraham I 	regulator_put(vqmmc);
7047d326930SKishon Vijay Abraham I 
7057d326930SKishon Vijay Abraham I 	return ret;
7067d326930SKishon Vijay Abraham I }
7077d326930SKishon Vijay Abraham I 
7087d326930SKishon Vijay Abraham I static const struct sdhci_pltfm_data sdhci_omap_pdata = {
7097d326930SKishon Vijay Abraham I 	.quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
7107d326930SKishon Vijay Abraham I 		  SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
7117d326930SKishon Vijay Abraham I 		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
7127d326930SKishon Vijay Abraham I 		  SDHCI_QUIRK_NO_HISPD_BIT |
7137d326930SKishon Vijay Abraham I 		  SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
714e0b2dbcfSKishon Vijay Abraham I 	.quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN |
715e0b2dbcfSKishon Vijay Abraham I 		   SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
7167d326930SKishon Vijay Abraham I 		   SDHCI_QUIRK2_RSP_136_HAS_CRC,
7177d326930SKishon Vijay Abraham I 	.ops = &sdhci_omap_ops,
7187d326930SKishon Vijay Abraham I };
7197d326930SKishon Vijay Abraham I 
7207d326930SKishon Vijay Abraham I static const struct sdhci_omap_data dra7_data = {
7217d326930SKishon Vijay Abraham I 	.offset = 0x200,
7228d20b2eaSKishon Vijay Abraham I 	.flags	= SDHCI_OMAP_REQUIRE_IODELAY,
7237d326930SKishon Vijay Abraham I };
7247d326930SKishon Vijay Abraham I 
7257d326930SKishon Vijay Abraham I static const struct of_device_id omap_sdhci_match[] = {
7267d326930SKishon Vijay Abraham I 	{ .compatible = "ti,dra7-sdhci", .data = &dra7_data },
7277d326930SKishon Vijay Abraham I 	{},
7287d326930SKishon Vijay Abraham I };
7297d326930SKishon Vijay Abraham I MODULE_DEVICE_TABLE(of, omap_sdhci_match);
7307d326930SKishon Vijay Abraham I 
7318d20b2eaSKishon Vijay Abraham I static struct pinctrl_state
7328d20b2eaSKishon Vijay Abraham I *sdhci_omap_iodelay_pinctrl_state(struct sdhci_omap_host *omap_host, char *mode,
7338d20b2eaSKishon Vijay Abraham I 				  u32 *caps, u32 capmask)
7348d20b2eaSKishon Vijay Abraham I {
7358d20b2eaSKishon Vijay Abraham I 	struct device *dev = omap_host->dev;
7368d20b2eaSKishon Vijay Abraham I 	struct pinctrl_state *pinctrl_state = ERR_PTR(-ENODEV);
7378d20b2eaSKishon Vijay Abraham I 
7388d20b2eaSKishon Vijay Abraham I 	if (!(*caps & capmask))
7398d20b2eaSKishon Vijay Abraham I 		goto ret;
7408d20b2eaSKishon Vijay Abraham I 
7418d20b2eaSKishon Vijay Abraham I 	pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, mode);
7428d20b2eaSKishon Vijay Abraham I 	if (IS_ERR(pinctrl_state)) {
7438d20b2eaSKishon Vijay Abraham I 		dev_err(dev, "no pinctrl state for %s mode", mode);
7448d20b2eaSKishon Vijay Abraham I 		*caps &= ~capmask;
7458d20b2eaSKishon Vijay Abraham I 	}
7468d20b2eaSKishon Vijay Abraham I 
7478d20b2eaSKishon Vijay Abraham I ret:
7488d20b2eaSKishon Vijay Abraham I 	return pinctrl_state;
7498d20b2eaSKishon Vijay Abraham I }
7508d20b2eaSKishon Vijay Abraham I 
7518d20b2eaSKishon Vijay Abraham I static int sdhci_omap_config_iodelay_pinctrl_state(struct sdhci_omap_host
7528d20b2eaSKishon Vijay Abraham I 						   *omap_host)
7538d20b2eaSKishon Vijay Abraham I {
7548d20b2eaSKishon Vijay Abraham I 	struct device *dev = omap_host->dev;
7558d20b2eaSKishon Vijay Abraham I 	struct sdhci_host *host = omap_host->host;
7568d20b2eaSKishon Vijay Abraham I 	struct mmc_host *mmc = host->mmc;
7578d20b2eaSKishon Vijay Abraham I 	u32 *caps = &mmc->caps;
7588d20b2eaSKishon Vijay Abraham I 	u32 *caps2 = &mmc->caps2;
7598d20b2eaSKishon Vijay Abraham I 	struct pinctrl_state *state;
7608d20b2eaSKishon Vijay Abraham I 	struct pinctrl_state **pinctrl_state;
7618d20b2eaSKishon Vijay Abraham I 
7628d20b2eaSKishon Vijay Abraham I 	if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
7638d20b2eaSKishon Vijay Abraham I 		return 0;
7648d20b2eaSKishon Vijay Abraham I 
7658d20b2eaSKishon Vijay Abraham I 	pinctrl_state = devm_kzalloc(dev, sizeof(*pinctrl_state) *
7668d20b2eaSKishon Vijay Abraham I 				     (MMC_TIMING_MMC_HS200 + 1), GFP_KERNEL);
7678d20b2eaSKishon Vijay Abraham I 	if (!pinctrl_state)
7688d20b2eaSKishon Vijay Abraham I 		return -ENOMEM;
7698d20b2eaSKishon Vijay Abraham I 
7708d20b2eaSKishon Vijay Abraham I 	omap_host->pinctrl = devm_pinctrl_get(omap_host->dev);
7718d20b2eaSKishon Vijay Abraham I 	if (IS_ERR(omap_host->pinctrl)) {
7728d20b2eaSKishon Vijay Abraham I 		dev_err(dev, "Cannot get pinctrl\n");
7738d20b2eaSKishon Vijay Abraham I 		return PTR_ERR(omap_host->pinctrl);
7748d20b2eaSKishon Vijay Abraham I 	}
7758d20b2eaSKishon Vijay Abraham I 
7768d20b2eaSKishon Vijay Abraham I 	state = pinctrl_lookup_state(omap_host->pinctrl, "default");
7778d20b2eaSKishon Vijay Abraham I 	if (IS_ERR(state)) {
7788d20b2eaSKishon Vijay Abraham I 		dev_err(dev, "no pinctrl state for default mode\n");
7798d20b2eaSKishon Vijay Abraham I 		return PTR_ERR(state);
7808d20b2eaSKishon Vijay Abraham I 	}
7818d20b2eaSKishon Vijay Abraham I 	pinctrl_state[MMC_TIMING_LEGACY] = state;
7828d20b2eaSKishon Vijay Abraham I 
7838d20b2eaSKishon Vijay Abraham I 	state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr104", caps,
7848d20b2eaSKishon Vijay Abraham I 						 MMC_CAP_UHS_SDR104);
7858d20b2eaSKishon Vijay Abraham I 	if (!IS_ERR(state))
7868d20b2eaSKishon Vijay Abraham I 		pinctrl_state[MMC_TIMING_UHS_SDR104] = state;
7878d20b2eaSKishon Vijay Abraham I 
7888d20b2eaSKishon Vijay Abraham I 	state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr50", caps,
7898d20b2eaSKishon Vijay Abraham I 						 MMC_CAP_UHS_DDR50);
7908d20b2eaSKishon Vijay Abraham I 	if (!IS_ERR(state))
7918d20b2eaSKishon Vijay Abraham I 		pinctrl_state[MMC_TIMING_UHS_DDR50] = state;
7928d20b2eaSKishon Vijay Abraham I 
7938d20b2eaSKishon Vijay Abraham I 	state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr50", caps,
7948d20b2eaSKishon Vijay Abraham I 						 MMC_CAP_UHS_SDR50);
7958d20b2eaSKishon Vijay Abraham I 	if (!IS_ERR(state))
7968d20b2eaSKishon Vijay Abraham I 		pinctrl_state[MMC_TIMING_UHS_SDR50] = state;
7978d20b2eaSKishon Vijay Abraham I 
7988d20b2eaSKishon Vijay Abraham I 	state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr25", caps,
7998d20b2eaSKishon Vijay Abraham I 						 MMC_CAP_UHS_SDR25);
8008d20b2eaSKishon Vijay Abraham I 	if (!IS_ERR(state))
8018d20b2eaSKishon Vijay Abraham I 		pinctrl_state[MMC_TIMING_UHS_SDR25] = state;
8028d20b2eaSKishon Vijay Abraham I 
8038d20b2eaSKishon Vijay Abraham I 	state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr12", caps,
8048d20b2eaSKishon Vijay Abraham I 						 MMC_CAP_UHS_SDR12);
8058d20b2eaSKishon Vijay Abraham I 	if (!IS_ERR(state))
8068d20b2eaSKishon Vijay Abraham I 		pinctrl_state[MMC_TIMING_UHS_SDR12] = state;
8078d20b2eaSKishon Vijay Abraham I 
8088d20b2eaSKishon Vijay Abraham I 	state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_1_8v", caps,
8098d20b2eaSKishon Vijay Abraham I 						 MMC_CAP_1_8V_DDR);
8108d20b2eaSKishon Vijay Abraham I 	if (!IS_ERR(state))
8118d20b2eaSKishon Vijay Abraham I 		pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
8128d20b2eaSKishon Vijay Abraham I 
8138d20b2eaSKishon Vijay Abraham I 	state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
8148d20b2eaSKishon Vijay Abraham I 						 MMC_CAP_SD_HIGHSPEED);
8158d20b2eaSKishon Vijay Abraham I 	if (!IS_ERR(state))
8168d20b2eaSKishon Vijay Abraham I 		pinctrl_state[MMC_TIMING_SD_HS] = state;
8178d20b2eaSKishon Vijay Abraham I 
8188d20b2eaSKishon Vijay Abraham I 	state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
8198d20b2eaSKishon Vijay Abraham I 						 MMC_CAP_MMC_HIGHSPEED);
8208d20b2eaSKishon Vijay Abraham I 	if (!IS_ERR(state))
8218d20b2eaSKishon Vijay Abraham I 		pinctrl_state[MMC_TIMING_MMC_HS] = state;
8228d20b2eaSKishon Vijay Abraham I 
8238d20b2eaSKishon Vijay Abraham I 	state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs200_1_8v", caps2,
8248d20b2eaSKishon Vijay Abraham I 						 MMC_CAP2_HS200_1_8V_SDR);
8258d20b2eaSKishon Vijay Abraham I 	if (!IS_ERR(state))
8268d20b2eaSKishon Vijay Abraham I 		pinctrl_state[MMC_TIMING_MMC_HS200] = state;
8278d20b2eaSKishon Vijay Abraham I 
8288d20b2eaSKishon Vijay Abraham I 	omap_host->pinctrl_state = pinctrl_state;
8298d20b2eaSKishon Vijay Abraham I 
8308d20b2eaSKishon Vijay Abraham I 	return 0;
8318d20b2eaSKishon Vijay Abraham I }
8328d20b2eaSKishon Vijay Abraham I 
8337d326930SKishon Vijay Abraham I static int sdhci_omap_probe(struct platform_device *pdev)
8347d326930SKishon Vijay Abraham I {
8357d326930SKishon Vijay Abraham I 	int ret;
8367d326930SKishon Vijay Abraham I 	u32 offset;
8377d326930SKishon Vijay Abraham I 	struct device *dev = &pdev->dev;
8387d326930SKishon Vijay Abraham I 	struct sdhci_host *host;
8397d326930SKishon Vijay Abraham I 	struct sdhci_pltfm_host *pltfm_host;
8407d326930SKishon Vijay Abraham I 	struct sdhci_omap_host *omap_host;
8417d326930SKishon Vijay Abraham I 	struct mmc_host *mmc;
8427d326930SKishon Vijay Abraham I 	const struct of_device_id *match;
8437d326930SKishon Vijay Abraham I 	struct sdhci_omap_data *data;
8447d326930SKishon Vijay Abraham I 
8457d326930SKishon Vijay Abraham I 	match = of_match_device(omap_sdhci_match, dev);
8467d326930SKishon Vijay Abraham I 	if (!match)
8477d326930SKishon Vijay Abraham I 		return -EINVAL;
8487d326930SKishon Vijay Abraham I 
8497d326930SKishon Vijay Abraham I 	data = (struct sdhci_omap_data *)match->data;
8507d326930SKishon Vijay Abraham I 	if (!data) {
8517d326930SKishon Vijay Abraham I 		dev_err(dev, "no sdhci omap data\n");
8527d326930SKishon Vijay Abraham I 		return -EINVAL;
8537d326930SKishon Vijay Abraham I 	}
8547d326930SKishon Vijay Abraham I 	offset = data->offset;
8557d326930SKishon Vijay Abraham I 
8567d326930SKishon Vijay Abraham I 	host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
8577d326930SKishon Vijay Abraham I 				sizeof(*omap_host));
8587d326930SKishon Vijay Abraham I 	if (IS_ERR(host)) {
8597d326930SKishon Vijay Abraham I 		dev_err(dev, "Failed sdhci_pltfm_init\n");
8607d326930SKishon Vijay Abraham I 		return PTR_ERR(host);
8617d326930SKishon Vijay Abraham I 	}
8627d326930SKishon Vijay Abraham I 
8637d326930SKishon Vijay Abraham I 	pltfm_host = sdhci_priv(host);
8647d326930SKishon Vijay Abraham I 	omap_host = sdhci_pltfm_priv(pltfm_host);
8657d326930SKishon Vijay Abraham I 	omap_host->host = host;
8667d326930SKishon Vijay Abraham I 	omap_host->base = host->ioaddr;
8677d326930SKishon Vijay Abraham I 	omap_host->dev = dev;
868300df508SKishon Vijay Abraham I 	omap_host->power_mode = MMC_POWER_UNDEFINED;
8698d20b2eaSKishon Vijay Abraham I 	omap_host->timing = MMC_TIMING_LEGACY;
8708d20b2eaSKishon Vijay Abraham I 	omap_host->flags = data->flags;
8717d326930SKishon Vijay Abraham I 	host->ioaddr += offset;
8727d326930SKishon Vijay Abraham I 
8737d326930SKishon Vijay Abraham I 	mmc = host->mmc;
8747d326930SKishon Vijay Abraham I 	ret = mmc_of_parse(mmc);
8757d326930SKishon Vijay Abraham I 	if (ret)
8767d326930SKishon Vijay Abraham I 		goto err_pltfm_free;
8777d326930SKishon Vijay Abraham I 
8787d326930SKishon Vijay Abraham I 	pltfm_host->clk = devm_clk_get(dev, "fck");
8797d326930SKishon Vijay Abraham I 	if (IS_ERR(pltfm_host->clk)) {
8807d326930SKishon Vijay Abraham I 		ret = PTR_ERR(pltfm_host->clk);
8817d326930SKishon Vijay Abraham I 		goto err_pltfm_free;
8827d326930SKishon Vijay Abraham I 	}
8837d326930SKishon Vijay Abraham I 
8847d326930SKishon Vijay Abraham I 	ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
8857d326930SKishon Vijay Abraham I 	if (ret) {
8867d326930SKishon Vijay Abraham I 		dev_err(dev, "failed to set clock to %d\n", mmc->f_max);
8877d326930SKishon Vijay Abraham I 		goto err_pltfm_free;
8887d326930SKishon Vijay Abraham I 	}
8897d326930SKishon Vijay Abraham I 
8907d326930SKishon Vijay Abraham I 	omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
8917d326930SKishon Vijay Abraham I 	if (IS_ERR(omap_host->pbias)) {
8927d326930SKishon Vijay Abraham I 		ret = PTR_ERR(omap_host->pbias);
8937d326930SKishon Vijay Abraham I 		if (ret != -ENODEV)
8947d326930SKishon Vijay Abraham I 			goto err_pltfm_free;
8957d326930SKishon Vijay Abraham I 		dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
8967d326930SKishon Vijay Abraham I 	}
8977d326930SKishon Vijay Abraham I 	omap_host->pbias_enabled = false;
8987d326930SKishon Vijay Abraham I 
8997d326930SKishon Vijay Abraham I 	/*
9007d326930SKishon Vijay Abraham I 	 * omap_device_pm_domain has callbacks to enable the main
9017d326930SKishon Vijay Abraham I 	 * functional clock, interface clock and also configure the
9027d326930SKishon Vijay Abraham I 	 * SYSCONFIG register of omap devices. The callback will be invoked
9037d326930SKishon Vijay Abraham I 	 * as part of pm_runtime_get_sync.
9047d326930SKishon Vijay Abraham I 	 */
9057d326930SKishon Vijay Abraham I 	pm_runtime_enable(dev);
9067d326930SKishon Vijay Abraham I 	ret = pm_runtime_get_sync(dev);
9077d326930SKishon Vijay Abraham I 	if (ret < 0) {
9087d326930SKishon Vijay Abraham I 		dev_err(dev, "pm_runtime_get_sync failed\n");
9097d326930SKishon Vijay Abraham I 		pm_runtime_put_noidle(dev);
9107d326930SKishon Vijay Abraham I 		goto err_rpm_disable;
9117d326930SKishon Vijay Abraham I 	}
9127d326930SKishon Vijay Abraham I 
9137d326930SKishon Vijay Abraham I 	ret = sdhci_omap_set_capabilities(omap_host);
9147d326930SKishon Vijay Abraham I 	if (ret) {
9157d326930SKishon Vijay Abraham I 		dev_err(dev, "failed to set system capabilities\n");
9167d326930SKishon Vijay Abraham I 		goto err_put_sync;
9177d326930SKishon Vijay Abraham I 	}
9187d326930SKishon Vijay Abraham I 
9198d20b2eaSKishon Vijay Abraham I 	ret = sdhci_omap_config_iodelay_pinctrl_state(omap_host);
9208d20b2eaSKishon Vijay Abraham I 	if (ret)
9218d20b2eaSKishon Vijay Abraham I 		goto err_put_sync;
9228d20b2eaSKishon Vijay Abraham I 
9237d326930SKishon Vijay Abraham I 	host->mmc_host_ops.get_ro = mmc_gpio_get_ro;
9247d326930SKishon Vijay Abraham I 	host->mmc_host_ops.start_signal_voltage_switch =
9257d326930SKishon Vijay Abraham I 					sdhci_omap_start_signal_voltage_switch;
9267d326930SKishon Vijay Abraham I 	host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
92720ea26a1SKishon Vijay Abraham I 	host->mmc_host_ops.card_busy = sdhci_omap_card_busy;
9289fc2cd76SKishon Vijay Abraham I 	host->mmc_host_ops.execute_tuning = sdhci_omap_execute_tuning;
9297d326930SKishon Vijay Abraham I 
9307d326930SKishon Vijay Abraham I 	sdhci_read_caps(host);
9317d326930SKishon Vijay Abraham I 	host->caps |= SDHCI_CAN_DO_ADMA2;
9327d326930SKishon Vijay Abraham I 
9337d326930SKishon Vijay Abraham I 	ret = sdhci_add_host(host);
9347d326930SKishon Vijay Abraham I 	if (ret)
9357d326930SKishon Vijay Abraham I 		goto err_put_sync;
9367d326930SKishon Vijay Abraham I 
9377d326930SKishon Vijay Abraham I 	return 0;
9387d326930SKishon Vijay Abraham I 
9397d326930SKishon Vijay Abraham I err_put_sync:
9407d326930SKishon Vijay Abraham I 	pm_runtime_put_sync(dev);
9417d326930SKishon Vijay Abraham I 
9427d326930SKishon Vijay Abraham I err_rpm_disable:
9437d326930SKishon Vijay Abraham I 	pm_runtime_disable(dev);
9447d326930SKishon Vijay Abraham I 
9457d326930SKishon Vijay Abraham I err_pltfm_free:
9467d326930SKishon Vijay Abraham I 	sdhci_pltfm_free(pdev);
9477d326930SKishon Vijay Abraham I 	return ret;
9487d326930SKishon Vijay Abraham I }
9497d326930SKishon Vijay Abraham I 
9507d326930SKishon Vijay Abraham I static int sdhci_omap_remove(struct platform_device *pdev)
9517d326930SKishon Vijay Abraham I {
9527d326930SKishon Vijay Abraham I 	struct device *dev = &pdev->dev;
9537d326930SKishon Vijay Abraham I 	struct sdhci_host *host = platform_get_drvdata(pdev);
9547d326930SKishon Vijay Abraham I 
9557d326930SKishon Vijay Abraham I 	sdhci_remove_host(host, true);
9567d326930SKishon Vijay Abraham I 	pm_runtime_put_sync(dev);
9577d326930SKishon Vijay Abraham I 	pm_runtime_disable(dev);
9587d326930SKishon Vijay Abraham I 	sdhci_pltfm_free(pdev);
9597d326930SKishon Vijay Abraham I 
9607d326930SKishon Vijay Abraham I 	return 0;
9617d326930SKishon Vijay Abraham I }
9627d326930SKishon Vijay Abraham I 
9637d326930SKishon Vijay Abraham I static struct platform_driver sdhci_omap_driver = {
9647d326930SKishon Vijay Abraham I 	.probe = sdhci_omap_probe,
9657d326930SKishon Vijay Abraham I 	.remove = sdhci_omap_remove,
9667d326930SKishon Vijay Abraham I 	.driver = {
9677d326930SKishon Vijay Abraham I 		   .name = "sdhci-omap",
9687d326930SKishon Vijay Abraham I 		   .of_match_table = omap_sdhci_match,
9697d326930SKishon Vijay Abraham I 		  },
9707d326930SKishon Vijay Abraham I };
9717d326930SKishon Vijay Abraham I 
9727d326930SKishon Vijay Abraham I module_platform_driver(sdhci_omap_driver);
9737d326930SKishon Vijay Abraham I 
9747d326930SKishon Vijay Abraham I MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
9757d326930SKishon Vijay Abraham I MODULE_AUTHOR("Texas Instruments Inc.");
9767d326930SKishon Vijay Abraham I MODULE_LICENSE("GPL v2");
9777d326930SKishon Vijay Abraham I MODULE_ALIAS("platform:sdhci_omap");
978