xref: /rk3399_rockchip-uboot/drivers/mmc/omap_hsmmc.c (revision 5c6631beb27491f3f78b6a0ad888d38810e3d96b)
1de941241SSukumar Ghorai /*
2de941241SSukumar Ghorai  * (C) Copyright 2008
3de941241SSukumar Ghorai  * Texas Instruments, <www.ti.com>
4de941241SSukumar Ghorai  * Sukumar Ghorai <s-ghorai@ti.com>
5de941241SSukumar Ghorai  *
6de941241SSukumar Ghorai  * See file CREDITS for list of people who contributed to this
7de941241SSukumar Ghorai  * project.
8de941241SSukumar Ghorai  *
9de941241SSukumar Ghorai  * This program is free software; you can redistribute it and/or
10de941241SSukumar Ghorai  * modify it under the terms of the GNU General Public License as
11de941241SSukumar Ghorai  * published by the Free Software Foundation's version 2 of
12de941241SSukumar Ghorai  * the License.
13de941241SSukumar Ghorai  *
14de941241SSukumar Ghorai  * This program is distributed in the hope that it will be useful,
15de941241SSukumar Ghorai  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16de941241SSukumar Ghorai  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17de941241SSukumar Ghorai  * GNU General Public License for more details.
18de941241SSukumar Ghorai  *
19de941241SSukumar Ghorai  * You should have received a copy of the GNU General Public License
20de941241SSukumar Ghorai  * along with this program; if not, write to the Free Software
21de941241SSukumar Ghorai  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22de941241SSukumar Ghorai  * MA 02111-1307 USA
23de941241SSukumar Ghorai  */
24de941241SSukumar Ghorai 
25de941241SSukumar Ghorai #include <config.h>
26de941241SSukumar Ghorai #include <common.h>
2793bfd616SPantelis Antoniou #include <malloc.h>
28de941241SSukumar Ghorai #include <mmc.h>
29de941241SSukumar Ghorai #include <part.h>
30de941241SSukumar Ghorai #include <i2c.h>
31de941241SSukumar Ghorai #include <twl4030.h>
3214fa2dd0SBalaji T K #include <twl6030.h>
33cb199102SNishanth Menon #include <palmas.h>
34de941241SSukumar Ghorai #include <asm/io.h>
35de941241SSukumar Ghorai #include <asm/arch/mmc_host_def.h>
363b68939fSRoger Quadros #if !defined(CONFIG_SOC_KEYSTONE)
373b68939fSRoger Quadros #include <asm/gpio.h>
3896e0e7b3SDirk Behme #include <asm/arch/sys_proto.h>
393b68939fSRoger Quadros #endif
402a48b3a2STom Rini #ifdef CONFIG_MMC_OMAP36XX_PINS
412a48b3a2STom Rini #include <asm/arch/mux.h>
422a48b3a2STom Rini #endif
43a9d6a7e2SMugunthan V N #include <dm.h>
44a9d6a7e2SMugunthan V N 
45a9d6a7e2SMugunthan V N DECLARE_GLOBAL_DATA_PTR;
46de941241SSukumar Ghorai 
47ab769f22SPantelis Antoniou /* simplify defines to OMAP_HSMMC_USE_GPIO */
48ab769f22SPantelis Antoniou #if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
49ab769f22SPantelis Antoniou 	(defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO_SUPPORT))
50ab769f22SPantelis Antoniou #define OMAP_HSMMC_USE_GPIO
51ab769f22SPantelis Antoniou #else
52ab769f22SPantelis Antoniou #undef OMAP_HSMMC_USE_GPIO
53ab769f22SPantelis Antoniou #endif
54ab769f22SPantelis Antoniou 
5525c719e2SGrazvydas Ignotas /* common definitions for all OMAPs */
5625c719e2SGrazvydas Ignotas #define SYSCTL_SRC	(1 << 25)
5725c719e2SGrazvydas Ignotas #define SYSCTL_SRD	(1 << 26)
5825c719e2SGrazvydas Ignotas 
5946831c1aSAdam Ford struct omap2_mmc_platform_config {
6046831c1aSAdam Ford 	u32 reg_offset;
6146831c1aSAdam Ford };
6246831c1aSAdam Ford 
63cc22b0c0SNikita Kiryanov struct omap_hsmmc_data {
64cc22b0c0SNikita Kiryanov 	struct hsmmc *base_addr;
65*c4d660d4SSimon Glass #if !CONFIG_IS_ENABLED(DM_MMC)
6693bfd616SPantelis Antoniou 	struct mmc_config cfg;
673d673ffcSJean-Jacques Hiblot #endif
68ab769f22SPantelis Antoniou #ifdef OMAP_HSMMC_USE_GPIO
69*c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(DM_MMC)
70a9d6a7e2SMugunthan V N 	struct gpio_desc cd_gpio;	/* Change Detect GPIO */
71a9d6a7e2SMugunthan V N 	struct gpio_desc wp_gpio;	/* Write Protect GPIO */
72a9d6a7e2SMugunthan V N 	bool cd_inverted;
73a9d6a7e2SMugunthan V N #else
74e874d5b0SNikita Kiryanov 	int cd_gpio;
75e3913f56SNikita Kiryanov 	int wp_gpio;
76ab769f22SPantelis Antoniou #endif
77a9d6a7e2SMugunthan V N #endif
78cc22b0c0SNikita Kiryanov };
79cc22b0c0SNikita Kiryanov 
80eb9a28f6SNishanth Menon /* If we fail after 1 second wait, something is really bad */
81eb9a28f6SNishanth Menon #define MAX_RETRY_MS	1000
82eb9a28f6SNishanth Menon 
83933efe64SSricharan static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
84933efe64SSricharan static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
85933efe64SSricharan 			unsigned int siz);
8614fa2dd0SBalaji T K 
omap_hsmmc_get_data(struct mmc * mmc)87ae000e23SJean-Jacques Hiblot static inline struct omap_hsmmc_data *omap_hsmmc_get_data(struct mmc *mmc)
88ae000e23SJean-Jacques Hiblot {
89*c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(DM_MMC)
90ae000e23SJean-Jacques Hiblot 	return dev_get_priv(mmc->dev);
91ae000e23SJean-Jacques Hiblot #else
92ae000e23SJean-Jacques Hiblot 	return (struct omap_hsmmc_data *)mmc->priv;
93ae000e23SJean-Jacques Hiblot #endif
94ae000e23SJean-Jacques Hiblot }
omap_hsmmc_get_cfg(struct mmc * mmc)953d673ffcSJean-Jacques Hiblot static inline struct mmc_config *omap_hsmmc_get_cfg(struct mmc *mmc)
963d673ffcSJean-Jacques Hiblot {
97*c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(DM_MMC)
983d673ffcSJean-Jacques Hiblot 	struct omap_hsmmc_plat *plat = dev_get_platdata(mmc->dev);
993d673ffcSJean-Jacques Hiblot 	return &plat->cfg;
1003d673ffcSJean-Jacques Hiblot #else
1013d673ffcSJean-Jacques Hiblot 	return &((struct omap_hsmmc_data *)mmc->priv)->cfg;
1023d673ffcSJean-Jacques Hiblot #endif
1033d673ffcSJean-Jacques Hiblot }
104ae000e23SJean-Jacques Hiblot 
105*c4d660d4SSimon Glass #if defined(OMAP_HSMMC_USE_GPIO) && !CONFIG_IS_ENABLED(DM_MMC)
omap_mmc_setup_gpio_in(int gpio,const char * label)106e874d5b0SNikita Kiryanov static int omap_mmc_setup_gpio_in(int gpio, const char *label)
107e874d5b0SNikita Kiryanov {
1085915a2adSSimon Glass 	int ret;
1095915a2adSSimon Glass 
1105915a2adSSimon Glass #ifndef CONFIG_DM_GPIO
111e874d5b0SNikita Kiryanov 	if (!gpio_is_valid(gpio))
112e874d5b0SNikita Kiryanov 		return -1;
1135915a2adSSimon Glass #endif
1145915a2adSSimon Glass 	ret = gpio_request(gpio, label);
1155915a2adSSimon Glass 	if (ret)
1165915a2adSSimon Glass 		return ret;
117e874d5b0SNikita Kiryanov 
1185915a2adSSimon Glass 	ret = gpio_direction_input(gpio);
1195915a2adSSimon Glass 	if (ret)
1205915a2adSSimon Glass 		return ret;
121e874d5b0SNikita Kiryanov 
122e874d5b0SNikita Kiryanov 	return gpio;
123e874d5b0SNikita Kiryanov }
124e874d5b0SNikita Kiryanov #endif
125e874d5b0SNikita Kiryanov 
mmc_board_init(struct mmc * mmc)126750121c3SJeroen Hofstee static unsigned char mmc_board_init(struct mmc *mmc)
127de941241SSukumar Ghorai {
128de941241SSukumar Ghorai #if defined(CONFIG_OMAP34XX)
1293d673ffcSJean-Jacques Hiblot 	struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
130de941241SSukumar Ghorai 	t2_t *t2_base = (t2_t *)T2_BASE;
131de941241SSukumar Ghorai 	struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
132b1e725f2SGrazvydas Ignotas 	u32 pbias_lite;
1336aca17c9SAdam Ford #ifdef CONFIG_MMC_OMAP36XX_PINS
1346aca17c9SAdam Ford 	u32 wkup_ctrl = readl(OMAP34XX_CTRL_WKUP_CTRL);
1356aca17c9SAdam Ford #endif
136de941241SSukumar Ghorai 
137b1e725f2SGrazvydas Ignotas 	pbias_lite = readl(&t2_base->pbias_lite);
138b1e725f2SGrazvydas Ignotas 	pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
1395bfdd1fcSAlbert ARIBAUD \(3ADEV\) #ifdef CONFIG_TARGET_OMAP3_CAIRO
1405bfdd1fcSAlbert ARIBAUD \(3ADEV\) 	/* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
1415bfdd1fcSAlbert ARIBAUD \(3ADEV\) 	pbias_lite &= ~PBIASLITEVMODE0;
1425bfdd1fcSAlbert ARIBAUD \(3ADEV\) #endif
1436aca17c9SAdam Ford #ifdef CONFIG_MMC_OMAP36XX_PINS
1446aca17c9SAdam Ford 	if (get_cpu_family() == CPU_OMAP36XX) {
1456aca17c9SAdam Ford 		/* Disable extended drain IO before changing PBIAS */
1466aca17c9SAdam Ford 		wkup_ctrl &= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ;
1476aca17c9SAdam Ford 		writel(wkup_ctrl, OMAP34XX_CTRL_WKUP_CTRL);
1486aca17c9SAdam Ford 	}
1496aca17c9SAdam Ford #endif
150b1e725f2SGrazvydas Ignotas 	writel(pbias_lite, &t2_base->pbias_lite);
151aac5450eSPaul Kocialkowski 
152b1e725f2SGrazvydas Ignotas 	writel(pbias_lite | PBIASLITEPWRDNZ1 |
153de941241SSukumar Ghorai 		PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
154de941241SSukumar Ghorai 		&t2_base->pbias_lite);
155de941241SSukumar Ghorai 
1566aca17c9SAdam Ford #ifdef CONFIG_MMC_OMAP36XX_PINS
1576aca17c9SAdam Ford 	if (get_cpu_family() == CPU_OMAP36XX)
1586aca17c9SAdam Ford 		/* Enable extended drain IO after changing PBIAS */
1596aca17c9SAdam Ford 		writel(wkup_ctrl |
1606aca17c9SAdam Ford 				OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ,
1616aca17c9SAdam Ford 				OMAP34XX_CTRL_WKUP_CTRL);
1626aca17c9SAdam Ford #endif
163de941241SSukumar Ghorai 	writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
164de941241SSukumar Ghorai 		&t2_base->devconf0);
165de941241SSukumar Ghorai 
166de941241SSukumar Ghorai 	writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
167de941241SSukumar Ghorai 		&t2_base->devconf1);
168de941241SSukumar Ghorai 
169bbbc1ae9SJonathan Solnit 	/* Change from default of 52MHz to 26MHz if necessary */
1703d673ffcSJean-Jacques Hiblot 	if (!(cfg->host_caps & MMC_MODE_HS_52MHz))
171bbbc1ae9SJonathan Solnit 		writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
172bbbc1ae9SJonathan Solnit 			&t2_base->ctl_prog_io1);
173bbbc1ae9SJonathan Solnit 
174de941241SSukumar Ghorai 	writel(readl(&prcm_base->fclken1_core) |
175de941241SSukumar Ghorai 		EN_MMC1 | EN_MMC2 | EN_MMC3,
176de941241SSukumar Ghorai 		&prcm_base->fclken1_core);
177de941241SSukumar Ghorai 
178de941241SSukumar Ghorai 	writel(readl(&prcm_base->iclken1_core) |
179de941241SSukumar Ghorai 		EN_MMC1 | EN_MMC2 | EN_MMC3,
180de941241SSukumar Ghorai 		&prcm_base->iclken1_core);
181de941241SSukumar Ghorai #endif
182de941241SSukumar Ghorai 
183b4b06006SLokesh Vutla #if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
18414fa2dd0SBalaji T K 	/* PBIAS config needed for MMC1 only */
185dc09127aSJean-Jacques Hiblot 	if (mmc_get_blk_desc(mmc)->devnum == 0)
186b4b06006SLokesh Vutla 		vmmc_pbias_config(LDO_VOLT_3V0);
187dd23e59dSBalaji T K #endif
188de941241SSukumar Ghorai 
189de941241SSukumar Ghorai 	return 0;
190de941241SSukumar Ghorai }
191de941241SSukumar Ghorai 
mmc_init_stream(struct hsmmc * mmc_base)192933efe64SSricharan void mmc_init_stream(struct hsmmc *mmc_base)
193de941241SSukumar Ghorai {
194eb9a28f6SNishanth Menon 	ulong start;
195de941241SSukumar Ghorai 
196de941241SSukumar Ghorai 	writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
197de941241SSukumar Ghorai 
198de941241SSukumar Ghorai 	writel(MMC_CMD0, &mmc_base->cmd);
199eb9a28f6SNishanth Menon 	start = get_timer(0);
200eb9a28f6SNishanth Menon 	while (!(readl(&mmc_base->stat) & CC_MASK)) {
201eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
202eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for cc!\n", __func__);
203eb9a28f6SNishanth Menon 			return;
204eb9a28f6SNishanth Menon 		}
205eb9a28f6SNishanth Menon 	}
206de941241SSukumar Ghorai 	writel(CC_MASK, &mmc_base->stat)
207de941241SSukumar Ghorai 		;
208de941241SSukumar Ghorai 	writel(MMC_CMD0, &mmc_base->cmd)
209de941241SSukumar Ghorai 		;
210eb9a28f6SNishanth Menon 	start = get_timer(0);
211eb9a28f6SNishanth Menon 	while (!(readl(&mmc_base->stat) & CC_MASK)) {
212eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
213eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for cc2!\n", __func__);
214eb9a28f6SNishanth Menon 			return;
215eb9a28f6SNishanth Menon 		}
216eb9a28f6SNishanth Menon 	}
217de941241SSukumar Ghorai 	writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
218de941241SSukumar Ghorai }
219de941241SSukumar Ghorai 
omap_hsmmc_init_setup(struct mmc * mmc)220ab769f22SPantelis Antoniou static int omap_hsmmc_init_setup(struct mmc *mmc)
221de941241SSukumar Ghorai {
222ae000e23SJean-Jacques Hiblot 	struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
223cc22b0c0SNikita Kiryanov 	struct hsmmc *mmc_base;
224de941241SSukumar Ghorai 	unsigned int reg_val;
225de941241SSukumar Ghorai 	unsigned int dsor;
226eb9a28f6SNishanth Menon 	ulong start;
227de941241SSukumar Ghorai 
228ae000e23SJean-Jacques Hiblot 	mmc_base = priv->base_addr;
22914fa2dd0SBalaji T K 	mmc_board_init(mmc);
230de941241SSukumar Ghorai 
231de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
232de941241SSukumar Ghorai 		&mmc_base->sysconfig);
233eb9a28f6SNishanth Menon 	start = get_timer(0);
234eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
235eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
236eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for cc2!\n", __func__);
237915ffa52SJaehoon Chung 			return -ETIMEDOUT;
238eb9a28f6SNishanth Menon 		}
239eb9a28f6SNishanth Menon 	}
240de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
241eb9a28f6SNishanth Menon 	start = get_timer(0);
242eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
243eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
244eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for softresetall!\n",
245eb9a28f6SNishanth Menon 				__func__);
246915ffa52SJaehoon Chung 			return -ETIMEDOUT;
247eb9a28f6SNishanth Menon 		}
248eb9a28f6SNishanth Menon 	}
249de941241SSukumar Ghorai 	writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
250de941241SSukumar Ghorai 	writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
251de941241SSukumar Ghorai 		&mmc_base->capa);
252de941241SSukumar Ghorai 
253de941241SSukumar Ghorai 	reg_val = readl(&mmc_base->con) & RESERVED_MASK;
254de941241SSukumar Ghorai 
255de941241SSukumar Ghorai 	writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
256de941241SSukumar Ghorai 		MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
257de941241SSukumar Ghorai 		HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
258de941241SSukumar Ghorai 
259de941241SSukumar Ghorai 	dsor = 240;
260de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
261de941241SSukumar Ghorai 		(ICE_STOP | DTO_15THDTO | CEN_DISABLE));
262de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
263de941241SSukumar Ghorai 		(dsor << CLKD_OFFSET) | ICE_OSCILLATE);
264eb9a28f6SNishanth Menon 	start = get_timer(0);
265eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
266eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
267eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for ics!\n", __func__);
268915ffa52SJaehoon Chung 			return -ETIMEDOUT;
269eb9a28f6SNishanth Menon 		}
270eb9a28f6SNishanth Menon 	}
271de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
272de941241SSukumar Ghorai 
273de941241SSukumar Ghorai 	writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
274de941241SSukumar Ghorai 
275de941241SSukumar Ghorai 	writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
276de941241SSukumar Ghorai 		IE_CEB | IE_CCRC | IE_CTO | IE_BRR | IE_BWR | IE_TC | IE_CC,
277de941241SSukumar Ghorai 		&mmc_base->ie);
278de941241SSukumar Ghorai 
279de941241SSukumar Ghorai 	mmc_init_stream(mmc_base);
280de941241SSukumar Ghorai 
281de941241SSukumar Ghorai 	return 0;
282de941241SSukumar Ghorai }
283de941241SSukumar Ghorai 
28425c719e2SGrazvydas Ignotas /*
28525c719e2SGrazvydas Ignotas  * MMC controller internal finite state machine reset
28625c719e2SGrazvydas Ignotas  *
28725c719e2SGrazvydas Ignotas  * Used to reset command or data internal state machines, using respectively
28825c719e2SGrazvydas Ignotas  * SRC or SRD bit of SYSCTL register
28925c719e2SGrazvydas Ignotas  */
mmc_reset_controller_fsm(struct hsmmc * mmc_base,u32 bit)29025c719e2SGrazvydas Ignotas static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
29125c719e2SGrazvydas Ignotas {
29225c719e2SGrazvydas Ignotas 	ulong start;
29325c719e2SGrazvydas Ignotas 
29425c719e2SGrazvydas Ignotas 	mmc_reg_out(&mmc_base->sysctl, bit, bit);
29525c719e2SGrazvydas Ignotas 
29661a6cc27SOleksandr Tyshchenko 	/*
29761a6cc27SOleksandr Tyshchenko 	 * CMD(DAT) lines reset procedures are slightly different
29861a6cc27SOleksandr Tyshchenko 	 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
29961a6cc27SOleksandr Tyshchenko 	 * According to OMAP3 TRM:
30061a6cc27SOleksandr Tyshchenko 	 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
30161a6cc27SOleksandr Tyshchenko 	 * returns to 0x0.
30261a6cc27SOleksandr Tyshchenko 	 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
30361a6cc27SOleksandr Tyshchenko 	 * procedure steps must be as follows:
30461a6cc27SOleksandr Tyshchenko 	 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
30561a6cc27SOleksandr Tyshchenko 	 *    MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
30661a6cc27SOleksandr Tyshchenko 	 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
30761a6cc27SOleksandr Tyshchenko 	 * 3. Wait until the SRC (SRD) bit returns to 0x0
30861a6cc27SOleksandr Tyshchenko 	 *    (reset procedure is completed).
30961a6cc27SOleksandr Tyshchenko 	 */
31061a6cc27SOleksandr Tyshchenko #if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
311dce55b93SNikita Kiryanov 	defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
31261a6cc27SOleksandr Tyshchenko 	if (!(readl(&mmc_base->sysctl) & bit)) {
31361a6cc27SOleksandr Tyshchenko 		start = get_timer(0);
31461a6cc27SOleksandr Tyshchenko 		while (!(readl(&mmc_base->sysctl) & bit)) {
31561a6cc27SOleksandr Tyshchenko 			if (get_timer(0) - start > MAX_RETRY_MS)
31661a6cc27SOleksandr Tyshchenko 				return;
31761a6cc27SOleksandr Tyshchenko 		}
31861a6cc27SOleksandr Tyshchenko 	}
31961a6cc27SOleksandr Tyshchenko #endif
32025c719e2SGrazvydas Ignotas 	start = get_timer(0);
32125c719e2SGrazvydas Ignotas 	while ((readl(&mmc_base->sysctl) & bit) != 0) {
32225c719e2SGrazvydas Ignotas 		if (get_timer(0) - start > MAX_RETRY_MS) {
32325c719e2SGrazvydas Ignotas 			printf("%s: timedout waiting for sysctl %x to clear\n",
32425c719e2SGrazvydas Ignotas 				__func__, bit);
32525c719e2SGrazvydas Ignotas 			return;
32625c719e2SGrazvydas Ignotas 		}
32725c719e2SGrazvydas Ignotas 	}
32825c719e2SGrazvydas Ignotas }
329*c4d660d4SSimon Glass #if !CONFIG_IS_ENABLED(DM_MMC)
omap_hsmmc_send_cmd(struct mmc * mmc,struct mmc_cmd * cmd,struct mmc_data * data)330ab769f22SPantelis Antoniou static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
331de941241SSukumar Ghorai 			struct mmc_data *data)
332de941241SSukumar Ghorai {
333ae000e23SJean-Jacques Hiblot 	struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
334b5511d6cSJean-Jacques Hiblot #else
335b5511d6cSJean-Jacques Hiblot static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
336b5511d6cSJean-Jacques Hiblot 			struct mmc_data *data)
337b5511d6cSJean-Jacques Hiblot {
338b5511d6cSJean-Jacques Hiblot 	struct omap_hsmmc_data *priv = dev_get_priv(dev);
339b5511d6cSJean-Jacques Hiblot #endif
340cc22b0c0SNikita Kiryanov 	struct hsmmc *mmc_base;
341de941241SSukumar Ghorai 	unsigned int flags, mmc_stat;
342eb9a28f6SNishanth Menon 	ulong start;
343de941241SSukumar Ghorai 
344ae000e23SJean-Jacques Hiblot 	mmc_base = priv->base_addr;
345eb9a28f6SNishanth Menon 	start = get_timer(0);
346a7778f8fSTom Rini 	while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
347eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
348a7778f8fSTom Rini 			printf("%s: timedout waiting on cmd inhibit to clear\n",
349a7778f8fSTom Rini 					__func__);
350915ffa52SJaehoon Chung 			return -ETIMEDOUT;
351eb9a28f6SNishanth Menon 		}
352eb9a28f6SNishanth Menon 	}
353de941241SSukumar Ghorai 	writel(0xFFFFFFFF, &mmc_base->stat);
354eb9a28f6SNishanth Menon 	start = get_timer(0);
355eb9a28f6SNishanth Menon 	while (readl(&mmc_base->stat)) {
356eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
35715ceb1deSGrazvydas Ignotas 			printf("%s: timedout waiting for STAT (%x) to clear\n",
35815ceb1deSGrazvydas Ignotas 				__func__, readl(&mmc_base->stat));
359915ffa52SJaehoon Chung 			return -ETIMEDOUT;
360eb9a28f6SNishanth Menon 		}
361eb9a28f6SNishanth Menon 	}
362de941241SSukumar Ghorai 	/*
363de941241SSukumar Ghorai 	 * CMDREG
364de941241SSukumar Ghorai 	 * CMDIDX[13:8]	: Command index
365de941241SSukumar Ghorai 	 * DATAPRNT[5]	: Data Present Select
366de941241SSukumar Ghorai 	 * ENCMDIDX[4]	: Command Index Check Enable
367de941241SSukumar Ghorai 	 * ENCMDCRC[3]	: Command CRC Check Enable
368de941241SSukumar Ghorai 	 * RSPTYP[1:0]
369de941241SSukumar Ghorai 	 *	00 = No Response
370de941241SSukumar Ghorai 	 *	01 = Length 136
371de941241SSukumar Ghorai 	 *	10 = Length 48
372de941241SSukumar Ghorai 	 *	11 = Length 48 Check busy after response
373de941241SSukumar Ghorai 	 */
374de941241SSukumar Ghorai 	/* Delay added before checking the status of frq change
375de941241SSukumar Ghorai 	 * retry not supported by mmc.c(core file)
376de941241SSukumar Ghorai 	 */
377de941241SSukumar Ghorai 	if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
378de941241SSukumar Ghorai 		udelay(50000); /* wait 50 ms */
379de941241SSukumar Ghorai 
380de941241SSukumar Ghorai 	if (!(cmd->resp_type & MMC_RSP_PRESENT))
381de941241SSukumar Ghorai 		flags = 0;
382de941241SSukumar Ghorai 	else if (cmd->resp_type & MMC_RSP_136)
383de941241SSukumar Ghorai 		flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
384de941241SSukumar Ghorai 	else if (cmd->resp_type & MMC_RSP_BUSY)
385de941241SSukumar Ghorai 		flags = RSP_TYPE_LGHT48B;
386de941241SSukumar Ghorai 	else
387de941241SSukumar Ghorai 		flags = RSP_TYPE_LGHT48;
388de941241SSukumar Ghorai 
389de941241SSukumar Ghorai 	/* enable default flags */
390de941241SSukumar Ghorai 	flags =	flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
391de941241SSukumar Ghorai 			MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE | DE_DISABLE);
392de941241SSukumar Ghorai 
393de941241SSukumar Ghorai 	if (cmd->resp_type & MMC_RSP_CRC)
394de941241SSukumar Ghorai 		flags |= CCCE_CHECK;
395de941241SSukumar Ghorai 	if (cmd->resp_type & MMC_RSP_OPCODE)
396de941241SSukumar Ghorai 		flags |= CICE_CHECK;
397de941241SSukumar Ghorai 
398de941241SSukumar Ghorai 	if (data) {
399de941241SSukumar Ghorai 		if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
400de941241SSukumar Ghorai 			 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
401de941241SSukumar Ghorai 			flags |= (MSBS_MULTIBLK | BCE_ENABLE);
402de941241SSukumar Ghorai 			data->blocksize = 512;
403de941241SSukumar Ghorai 			writel(data->blocksize | (data->blocks << 16),
404de941241SSukumar Ghorai 							&mmc_base->blk);
405de941241SSukumar Ghorai 		} else
406de941241SSukumar Ghorai 			writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
407de941241SSukumar Ghorai 
408de941241SSukumar Ghorai 		if (data->flags & MMC_DATA_READ)
409de941241SSukumar Ghorai 			flags |= (DP_DATA | DDIR_READ);
410de941241SSukumar Ghorai 		else
411de941241SSukumar Ghorai 			flags |= (DP_DATA | DDIR_WRITE);
412de941241SSukumar Ghorai 	}
413de941241SSukumar Ghorai 
414de941241SSukumar Ghorai 	writel(cmd->cmdarg, &mmc_base->arg);
415152ba363SLubomir Popov 	udelay(20);		/* To fix "No status update" error on eMMC */
416de941241SSukumar Ghorai 	writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
417de941241SSukumar Ghorai 
418eb9a28f6SNishanth Menon 	start = get_timer(0);
419de941241SSukumar Ghorai 	do {
420de941241SSukumar Ghorai 		mmc_stat = readl(&mmc_base->stat);
421eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
422de941241SSukumar Ghorai 			printf("%s : timeout: No status update\n", __func__);
423915ffa52SJaehoon Chung 			return -ETIMEDOUT;
424de941241SSukumar Ghorai 		}
425eb9a28f6SNishanth Menon 	} while (!mmc_stat);
426de941241SSukumar Ghorai 
42725c719e2SGrazvydas Ignotas 	if ((mmc_stat & IE_CTO) != 0) {
42825c719e2SGrazvydas Ignotas 		mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
429915ffa52SJaehoon Chung 		return -ETIMEDOUT;
43025c719e2SGrazvydas Ignotas 	} else if ((mmc_stat & ERRI_MASK) != 0)
431de941241SSukumar Ghorai 		return -1;
432de941241SSukumar Ghorai 
433de941241SSukumar Ghorai 	if (mmc_stat & CC_MASK) {
434de941241SSukumar Ghorai 		writel(CC_MASK, &mmc_base->stat);
435de941241SSukumar Ghorai 		if (cmd->resp_type & MMC_RSP_PRESENT) {
436de941241SSukumar Ghorai 			if (cmd->resp_type & MMC_RSP_136) {
437de941241SSukumar Ghorai 				/* response type 2 */
438de941241SSukumar Ghorai 				cmd->response[3] = readl(&mmc_base->rsp10);
439de941241SSukumar Ghorai 				cmd->response[2] = readl(&mmc_base->rsp32);
440de941241SSukumar Ghorai 				cmd->response[1] = readl(&mmc_base->rsp54);
441de941241SSukumar Ghorai 				cmd->response[0] = readl(&mmc_base->rsp76);
442de941241SSukumar Ghorai 			} else
443de941241SSukumar Ghorai 				/* response types 1, 1b, 3, 4, 5, 6 */
444de941241SSukumar Ghorai 				cmd->response[0] = readl(&mmc_base->rsp10);
445de941241SSukumar Ghorai 		}
446de941241SSukumar Ghorai 	}
447de941241SSukumar Ghorai 
448de941241SSukumar Ghorai 	if (data && (data->flags & MMC_DATA_READ)) {
449de941241SSukumar Ghorai 		mmc_read_data(mmc_base,	data->dest,
450de941241SSukumar Ghorai 				data->blocksize * data->blocks);
451de941241SSukumar Ghorai 	} else if (data && (data->flags & MMC_DATA_WRITE)) {
452de941241SSukumar Ghorai 		mmc_write_data(mmc_base, data->src,
453de941241SSukumar Ghorai 				data->blocksize * data->blocks);
454de941241SSukumar Ghorai 	}
455de941241SSukumar Ghorai 	return 0;
456de941241SSukumar Ghorai }
457de941241SSukumar Ghorai 
458933efe64SSricharan static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
459de941241SSukumar Ghorai {
460de941241SSukumar Ghorai 	unsigned int *output_buf = (unsigned int *)buf;
461de941241SSukumar Ghorai 	unsigned int mmc_stat;
462de941241SSukumar Ghorai 	unsigned int count;
463de941241SSukumar Ghorai 
464de941241SSukumar Ghorai 	/*
465de941241SSukumar Ghorai 	 * Start Polled Read
466de941241SSukumar Ghorai 	 */
467de941241SSukumar Ghorai 	count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
468de941241SSukumar Ghorai 	count /= 4;
469de941241SSukumar Ghorai 
470de941241SSukumar Ghorai 	while (size) {
471eb9a28f6SNishanth Menon 		ulong start = get_timer(0);
472de941241SSukumar Ghorai 		do {
473de941241SSukumar Ghorai 			mmc_stat = readl(&mmc_base->stat);
474eb9a28f6SNishanth Menon 			if (get_timer(0) - start > MAX_RETRY_MS) {
475eb9a28f6SNishanth Menon 				printf("%s: timedout waiting for status!\n",
476eb9a28f6SNishanth Menon 						__func__);
477915ffa52SJaehoon Chung 				return -ETIMEDOUT;
478eb9a28f6SNishanth Menon 			}
479de941241SSukumar Ghorai 		} while (mmc_stat == 0);
480de941241SSukumar Ghorai 
48125c719e2SGrazvydas Ignotas 		if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
48225c719e2SGrazvydas Ignotas 			mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
48325c719e2SGrazvydas Ignotas 
484de941241SSukumar Ghorai 		if ((mmc_stat & ERRI_MASK) != 0)
485de941241SSukumar Ghorai 			return 1;
486de941241SSukumar Ghorai 
487de941241SSukumar Ghorai 		if (mmc_stat & BRR_MASK) {
488de941241SSukumar Ghorai 			unsigned int k;
489de941241SSukumar Ghorai 
490de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BRR_MASK,
491de941241SSukumar Ghorai 				&mmc_base->stat);
492de941241SSukumar Ghorai 			for (k = 0; k < count; k++) {
493de941241SSukumar Ghorai 				*output_buf = readl(&mmc_base->data);
494de941241SSukumar Ghorai 				output_buf++;
495de941241SSukumar Ghorai 			}
496de941241SSukumar Ghorai 			size -= (count*4);
497de941241SSukumar Ghorai 		}
498de941241SSukumar Ghorai 
499de941241SSukumar Ghorai 		if (mmc_stat & BWR_MASK)
500de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BWR_MASK,
501de941241SSukumar Ghorai 				&mmc_base->stat);
502de941241SSukumar Ghorai 
503de941241SSukumar Ghorai 		if (mmc_stat & TC_MASK) {
504de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | TC_MASK,
505de941241SSukumar Ghorai 				&mmc_base->stat);
506de941241SSukumar Ghorai 			break;
507de941241SSukumar Ghorai 		}
508de941241SSukumar Ghorai 	}
509de941241SSukumar Ghorai 	return 0;
510de941241SSukumar Ghorai }
511de941241SSukumar Ghorai 
512933efe64SSricharan static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
513933efe64SSricharan 				unsigned int size)
514de941241SSukumar Ghorai {
515de941241SSukumar Ghorai 	unsigned int *input_buf = (unsigned int *)buf;
516de941241SSukumar Ghorai 	unsigned int mmc_stat;
517de941241SSukumar Ghorai 	unsigned int count;
518de941241SSukumar Ghorai 
519de941241SSukumar Ghorai 	/*
520152ba363SLubomir Popov 	 * Start Polled Write
521de941241SSukumar Ghorai 	 */
522de941241SSukumar Ghorai 	count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
523de941241SSukumar Ghorai 	count /= 4;
524de941241SSukumar Ghorai 
525de941241SSukumar Ghorai 	while (size) {
526eb9a28f6SNishanth Menon 		ulong start = get_timer(0);
527de941241SSukumar Ghorai 		do {
528de941241SSukumar Ghorai 			mmc_stat = readl(&mmc_base->stat);
529eb9a28f6SNishanth Menon 			if (get_timer(0) - start > MAX_RETRY_MS) {
530eb9a28f6SNishanth Menon 				printf("%s: timedout waiting for status!\n",
531eb9a28f6SNishanth Menon 						__func__);
532915ffa52SJaehoon Chung 				return -ETIMEDOUT;
533eb9a28f6SNishanth Menon 			}
534de941241SSukumar Ghorai 		} while (mmc_stat == 0);
535de941241SSukumar Ghorai 
53625c719e2SGrazvydas Ignotas 		if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
53725c719e2SGrazvydas Ignotas 			mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
53825c719e2SGrazvydas Ignotas 
539de941241SSukumar Ghorai 		if ((mmc_stat & ERRI_MASK) != 0)
540de941241SSukumar Ghorai 			return 1;
541de941241SSukumar Ghorai 
542de941241SSukumar Ghorai 		if (mmc_stat & BWR_MASK) {
543de941241SSukumar Ghorai 			unsigned int k;
544de941241SSukumar Ghorai 
545de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BWR_MASK,
546de941241SSukumar Ghorai 					&mmc_base->stat);
547de941241SSukumar Ghorai 			for (k = 0; k < count; k++) {
548de941241SSukumar Ghorai 				writel(*input_buf, &mmc_base->data);
549de941241SSukumar Ghorai 				input_buf++;
550de941241SSukumar Ghorai 			}
551de941241SSukumar Ghorai 			size -= (count*4);
552de941241SSukumar Ghorai 		}
553de941241SSukumar Ghorai 
554de941241SSukumar Ghorai 		if (mmc_stat & BRR_MASK)
555de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BRR_MASK,
556de941241SSukumar Ghorai 				&mmc_base->stat);
557de941241SSukumar Ghorai 
558de941241SSukumar Ghorai 		if (mmc_stat & TC_MASK) {
559de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | TC_MASK,
560de941241SSukumar Ghorai 				&mmc_base->stat);
561de941241SSukumar Ghorai 			break;
562de941241SSukumar Ghorai 		}
563de941241SSukumar Ghorai 	}
564de941241SSukumar Ghorai 	return 0;
565de941241SSukumar Ghorai }
566de941241SSukumar Ghorai 
567*c4d660d4SSimon Glass #if !CONFIG_IS_ENABLED(DM_MMC)
56807b0b9c0SJaehoon Chung static int omap_hsmmc_set_ios(struct mmc *mmc)
569de941241SSukumar Ghorai {
570ae000e23SJean-Jacques Hiblot 	struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
571b5511d6cSJean-Jacques Hiblot #else
572b5511d6cSJean-Jacques Hiblot static int omap_hsmmc_set_ios(struct udevice *dev)
573b5511d6cSJean-Jacques Hiblot {
574b5511d6cSJean-Jacques Hiblot 	struct omap_hsmmc_data *priv = dev_get_priv(dev);
575b5511d6cSJean-Jacques Hiblot 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
576b5511d6cSJean-Jacques Hiblot 	struct mmc *mmc = upriv->mmc;
577b5511d6cSJean-Jacques Hiblot #endif
578cc22b0c0SNikita Kiryanov 	struct hsmmc *mmc_base;
579de941241SSukumar Ghorai 	unsigned int dsor = 0;
580eb9a28f6SNishanth Menon 	ulong start;
581de941241SSukumar Ghorai 
582ae000e23SJean-Jacques Hiblot 	mmc_base = priv->base_addr;
583de941241SSukumar Ghorai 	/* configue bus width */
584de941241SSukumar Ghorai 	switch (mmc->bus_width) {
585de941241SSukumar Ghorai 	case 8:
586de941241SSukumar Ghorai 		writel(readl(&mmc_base->con) | DTW_8_BITMODE,
587de941241SSukumar Ghorai 			&mmc_base->con);
588de941241SSukumar Ghorai 		break;
589de941241SSukumar Ghorai 
590de941241SSukumar Ghorai 	case 4:
591de941241SSukumar Ghorai 		writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
592de941241SSukumar Ghorai 			&mmc_base->con);
593de941241SSukumar Ghorai 		writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
594de941241SSukumar Ghorai 			&mmc_base->hctl);
595de941241SSukumar Ghorai 		break;
596de941241SSukumar Ghorai 
597de941241SSukumar Ghorai 	case 1:
598de941241SSukumar Ghorai 	default:
599de941241SSukumar Ghorai 		writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
600de941241SSukumar Ghorai 			&mmc_base->con);
601de941241SSukumar Ghorai 		writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
602de941241SSukumar Ghorai 			&mmc_base->hctl);
603de941241SSukumar Ghorai 		break;
604de941241SSukumar Ghorai 	}
605de941241SSukumar Ghorai 
606de941241SSukumar Ghorai 	/* configure clock with 96Mhz system clock.
607de941241SSukumar Ghorai 	 */
608de941241SSukumar Ghorai 	if (mmc->clock != 0) {
609de941241SSukumar Ghorai 		dsor = (MMC_CLOCK_REFERENCE * 1000000 / mmc->clock);
610de941241SSukumar Ghorai 		if ((MMC_CLOCK_REFERENCE * 1000000) / dsor > mmc->clock)
611de941241SSukumar Ghorai 			dsor++;
612de941241SSukumar Ghorai 	}
613de941241SSukumar Ghorai 
614de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
615de941241SSukumar Ghorai 				(ICE_STOP | DTO_15THDTO | CEN_DISABLE));
616de941241SSukumar Ghorai 
617de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
618de941241SSukumar Ghorai 				(dsor << CLKD_OFFSET) | ICE_OSCILLATE);
619de941241SSukumar Ghorai 
620eb9a28f6SNishanth Menon 	start = get_timer(0);
621eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
622eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
623eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for ics!\n", __func__);
62407b0b9c0SJaehoon Chung 			return -ETIMEDOUT;
625eb9a28f6SNishanth Menon 		}
626eb9a28f6SNishanth Menon 	}
627de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
62807b0b9c0SJaehoon Chung 
62907b0b9c0SJaehoon Chung 	return 0;
630de941241SSukumar Ghorai }
631de941241SSukumar Ghorai 
632ab769f22SPantelis Antoniou #ifdef OMAP_HSMMC_USE_GPIO
633*c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(DM_MMC)
634b5511d6cSJean-Jacques Hiblot static int omap_hsmmc_getcd(struct udevice *dev)
635a9d6a7e2SMugunthan V N {
636b5511d6cSJean-Jacques Hiblot 	struct omap_hsmmc_data *priv = dev_get_priv(dev);
637a9d6a7e2SMugunthan V N 	int value;
638a9d6a7e2SMugunthan V N 
639a9d6a7e2SMugunthan V N 	value = dm_gpio_get_value(&priv->cd_gpio);
640a9d6a7e2SMugunthan V N 	/* if no CD return as 1 */
641a9d6a7e2SMugunthan V N 	if (value < 0)
642a9d6a7e2SMugunthan V N 		return 1;
643a9d6a7e2SMugunthan V N 
644a9d6a7e2SMugunthan V N 	if (priv->cd_inverted)
645a9d6a7e2SMugunthan V N 		return !value;
646a9d6a7e2SMugunthan V N 	return value;
647a9d6a7e2SMugunthan V N }
648a9d6a7e2SMugunthan V N 
649b5511d6cSJean-Jacques Hiblot static int omap_hsmmc_getwp(struct udevice *dev)
650a9d6a7e2SMugunthan V N {
651b5511d6cSJean-Jacques Hiblot 	struct omap_hsmmc_data *priv = dev_get_priv(dev);
652a9d6a7e2SMugunthan V N 	int value;
653a9d6a7e2SMugunthan V N 
654a9d6a7e2SMugunthan V N 	value = dm_gpio_get_value(&priv->wp_gpio);
655a9d6a7e2SMugunthan V N 	/* if no WP return as 0 */
656a9d6a7e2SMugunthan V N 	if (value < 0)
657a9d6a7e2SMugunthan V N 		return 0;
658a9d6a7e2SMugunthan V N 	return value;
659a9d6a7e2SMugunthan V N }
660a9d6a7e2SMugunthan V N #else
661ab769f22SPantelis Antoniou static int omap_hsmmc_getcd(struct mmc *mmc)
662ab769f22SPantelis Antoniou {
663ae000e23SJean-Jacques Hiblot 	struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
664ab769f22SPantelis Antoniou 	int cd_gpio;
665ab769f22SPantelis Antoniou 
666ab769f22SPantelis Antoniou 	/* if no CD return as 1 */
667ae000e23SJean-Jacques Hiblot 	cd_gpio = priv->cd_gpio;
668ab769f22SPantelis Antoniou 	if (cd_gpio < 0)
669ab769f22SPantelis Antoniou 		return 1;
670ab769f22SPantelis Antoniou 
6710b03a931SIgor Grinberg 	/* NOTE: assumes card detect signal is active-low */
6720b03a931SIgor Grinberg 	return !gpio_get_value(cd_gpio);
673ab769f22SPantelis Antoniou }
674ab769f22SPantelis Antoniou 
675ab769f22SPantelis Antoniou static int omap_hsmmc_getwp(struct mmc *mmc)
676ab769f22SPantelis Antoniou {
677ae000e23SJean-Jacques Hiblot 	struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
678ab769f22SPantelis Antoniou 	int wp_gpio;
679ab769f22SPantelis Antoniou 
680ab769f22SPantelis Antoniou 	/* if no WP return as 0 */
681ae000e23SJean-Jacques Hiblot 	wp_gpio = priv->wp_gpio;
682ab769f22SPantelis Antoniou 	if (wp_gpio < 0)
683ab769f22SPantelis Antoniou 		return 0;
684ab769f22SPantelis Antoniou 
6850b03a931SIgor Grinberg 	/* NOTE: assumes write protect signal is active-high */
686ab769f22SPantelis Antoniou 	return gpio_get_value(wp_gpio);
687ab769f22SPantelis Antoniou }
688ab769f22SPantelis Antoniou #endif
689a9d6a7e2SMugunthan V N #endif
690ab769f22SPantelis Antoniou 
691*c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(DM_MMC)
692b5511d6cSJean-Jacques Hiblot static const struct dm_mmc_ops omap_hsmmc_ops = {
693b5511d6cSJean-Jacques Hiblot 	.send_cmd	= omap_hsmmc_send_cmd,
694b5511d6cSJean-Jacques Hiblot 	.set_ios	= omap_hsmmc_set_ios,
695b5511d6cSJean-Jacques Hiblot #ifdef OMAP_HSMMC_USE_GPIO
696b5511d6cSJean-Jacques Hiblot 	.get_cd		= omap_hsmmc_getcd,
697b5511d6cSJean-Jacques Hiblot 	.get_wp		= omap_hsmmc_getwp,
698b5511d6cSJean-Jacques Hiblot #endif
699b5511d6cSJean-Jacques Hiblot };
700b5511d6cSJean-Jacques Hiblot #else
701ab769f22SPantelis Antoniou static const struct mmc_ops omap_hsmmc_ops = {
702ab769f22SPantelis Antoniou 	.send_cmd	= omap_hsmmc_send_cmd,
703ab769f22SPantelis Antoniou 	.set_ios	= omap_hsmmc_set_ios,
704ab769f22SPantelis Antoniou 	.init		= omap_hsmmc_init_setup,
705ab769f22SPantelis Antoniou #ifdef OMAP_HSMMC_USE_GPIO
706ab769f22SPantelis Antoniou 	.getcd		= omap_hsmmc_getcd,
707ab769f22SPantelis Antoniou 	.getwp		= omap_hsmmc_getwp,
708ab769f22SPantelis Antoniou #endif
709ab769f22SPantelis Antoniou };
710b5511d6cSJean-Jacques Hiblot #endif
711ab769f22SPantelis Antoniou 
712*c4d660d4SSimon Glass #if !CONFIG_IS_ENABLED(DM_MMC)
713e3913f56SNikita Kiryanov int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
714e3913f56SNikita Kiryanov 		int wp_gpio)
715de941241SSukumar Ghorai {
71693bfd616SPantelis Antoniou 	struct mmc *mmc;
717ae000e23SJean-Jacques Hiblot 	struct omap_hsmmc_data *priv;
71893bfd616SPantelis Antoniou 	struct mmc_config *cfg;
71993bfd616SPantelis Antoniou 	uint host_caps_val;
720de941241SSukumar Ghorai 
721ae000e23SJean-Jacques Hiblot 	priv = malloc(sizeof(*priv));
722ae000e23SJean-Jacques Hiblot 	if (priv == NULL)
72393bfd616SPantelis Antoniou 		return -1;
72493bfd616SPantelis Antoniou 
7255a20397bSRob Herring 	host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
726de941241SSukumar Ghorai 
727de941241SSukumar Ghorai 	switch (dev_index) {
728de941241SSukumar Ghorai 	case 0:
729ae000e23SJean-Jacques Hiblot 		priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
730de941241SSukumar Ghorai 		break;
7311037d585STom Rini #ifdef OMAP_HSMMC2_BASE
732de941241SSukumar Ghorai 	case 1:
733ae000e23SJean-Jacques Hiblot 		priv->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
734152ba363SLubomir Popov #if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
7353891a54fSNishanth Menon 	defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
7363b68939fSRoger Quadros 	defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
7373b68939fSRoger Quadros 		defined(CONFIG_HSMMC2_8BIT)
738152ba363SLubomir Popov 		/* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
739152ba363SLubomir Popov 		host_caps_val |= MMC_MODE_8BIT;
740152ba363SLubomir Popov #endif
741de941241SSukumar Ghorai 		break;
7421037d585STom Rini #endif
7431037d585STom Rini #ifdef OMAP_HSMMC3_BASE
744de941241SSukumar Ghorai 	case 2:
745ae000e23SJean-Jacques Hiblot 		priv->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
7463891a54fSNishanth Menon #if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
747152ba363SLubomir Popov 		/* Enable 8-bit interface for eMMC on DRA7XX */
748152ba363SLubomir Popov 		host_caps_val |= MMC_MODE_8BIT;
749152ba363SLubomir Popov #endif
750de941241SSukumar Ghorai 		break;
7511037d585STom Rini #endif
752de941241SSukumar Ghorai 	default:
753ae000e23SJean-Jacques Hiblot 		priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
754de941241SSukumar Ghorai 		return 1;
755de941241SSukumar Ghorai 	}
756ab769f22SPantelis Antoniou #ifdef OMAP_HSMMC_USE_GPIO
757ab769f22SPantelis Antoniou 	/* on error gpio values are set to -1, which is what we want */
758ae000e23SJean-Jacques Hiblot 	priv->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
759ae000e23SJean-Jacques Hiblot 	priv->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
760ab769f22SPantelis Antoniou #endif
761173ddc5bSPeter Korsgaard 
762ae000e23SJean-Jacques Hiblot 	cfg = &priv->cfg;
763de941241SSukumar Ghorai 
76493bfd616SPantelis Antoniou 	cfg->name = "OMAP SD/MMC";
76593bfd616SPantelis Antoniou 	cfg->ops = &omap_hsmmc_ops;
76693bfd616SPantelis Antoniou 
76793bfd616SPantelis Antoniou 	cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
76893bfd616SPantelis Antoniou 	cfg->host_caps = host_caps_val & ~host_caps_mask;
76993bfd616SPantelis Antoniou 
77093bfd616SPantelis Antoniou 	cfg->f_min = 400000;
771bbbc1ae9SJonathan Solnit 
772bbbc1ae9SJonathan Solnit 	if (f_max != 0)
77393bfd616SPantelis Antoniou 		cfg->f_max = f_max;
774bbbc1ae9SJonathan Solnit 	else {
77593bfd616SPantelis Antoniou 		if (cfg->host_caps & MMC_MODE_HS) {
77693bfd616SPantelis Antoniou 			if (cfg->host_caps & MMC_MODE_HS_52MHz)
77793bfd616SPantelis Antoniou 				cfg->f_max = 52000000;
778bbbc1ae9SJonathan Solnit 			else
77993bfd616SPantelis Antoniou 				cfg->f_max = 26000000;
780bbbc1ae9SJonathan Solnit 		} else
78193bfd616SPantelis Antoniou 			cfg->f_max = 20000000;
782bbbc1ae9SJonathan Solnit 	}
783de941241SSukumar Ghorai 
78493bfd616SPantelis Antoniou 	cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
7858feafcc4SJohn Rigby 
7864ca9244dSJohn Rigby #if defined(CONFIG_OMAP34XX)
7874ca9244dSJohn Rigby 	/*
7884ca9244dSJohn Rigby 	 * Silicon revs 2.1 and older do not support multiblock transfers.
7894ca9244dSJohn Rigby 	 */
7904ca9244dSJohn Rigby 	if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
79193bfd616SPantelis Antoniou 		cfg->b_max = 1;
7924ca9244dSJohn Rigby #endif
793ae000e23SJean-Jacques Hiblot 	mmc = mmc_create(cfg, priv);
79493bfd616SPantelis Antoniou 	if (mmc == NULL)
79593bfd616SPantelis Antoniou 		return -1;
796de941241SSukumar Ghorai 
797de941241SSukumar Ghorai 	return 0;
798de941241SSukumar Ghorai }
799a9d6a7e2SMugunthan V N #else
8002558c049SLokesh Vutla #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
801a9d6a7e2SMugunthan V N static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
802a9d6a7e2SMugunthan V N {
8033d673ffcSJean-Jacques Hiblot 	struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
8043d673ffcSJean-Jacques Hiblot 	struct mmc_config *cfg = &plat->cfg;
80546831c1aSAdam Ford 	struct omap2_mmc_platform_config *data =
80646831c1aSAdam Ford 		(struct omap2_mmc_platform_config *)dev_get_driver_data(dev);
807a9d6a7e2SMugunthan V N 	const void *fdt = gd->fdt_blob;
808e160f7d4SSimon Glass 	int node = dev_of_offset(dev);
809a9d6a7e2SMugunthan V N 	int val;
810a9d6a7e2SMugunthan V N 
811a821c4afSSimon Glass 	plat->base_addr = map_physmem(devfdt_get_addr(dev),
812a821c4afSSimon Glass 				      sizeof(struct hsmmc *),
81346831c1aSAdam Ford 				      MAP_NOCACHE) + data->reg_offset;
814a9d6a7e2SMugunthan V N 
815a9d6a7e2SMugunthan V N 	cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
816a9d6a7e2SMugunthan V N 	val = fdtdec_get_int(fdt, node, "bus-width", -1);
817a9d6a7e2SMugunthan V N 	if (val < 0) {
818a9d6a7e2SMugunthan V N 		printf("error: bus-width property missing\n");
819a9d6a7e2SMugunthan V N 		return -ENOENT;
820a9d6a7e2SMugunthan V N 	}
821a9d6a7e2SMugunthan V N 
822a9d6a7e2SMugunthan V N 	switch (val) {
823a9d6a7e2SMugunthan V N 	case 0x8:
824a9d6a7e2SMugunthan V N 		cfg->host_caps |= MMC_MODE_8BIT;
825a9d6a7e2SMugunthan V N 	case 0x4:
826a9d6a7e2SMugunthan V N 		cfg->host_caps |= MMC_MODE_4BIT;
827a9d6a7e2SMugunthan V N 		break;
828a9d6a7e2SMugunthan V N 	default:
829a9d6a7e2SMugunthan V N 		printf("error: invalid bus-width property\n");
830a9d6a7e2SMugunthan V N 		return -ENOENT;
831a9d6a7e2SMugunthan V N 	}
832a9d6a7e2SMugunthan V N 
833a9d6a7e2SMugunthan V N 	cfg->f_min = 400000;
834a9d6a7e2SMugunthan V N 	cfg->f_max = fdtdec_get_int(fdt, node, "max-frequency", 52000000);
835a9d6a7e2SMugunthan V N 	cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
836a9d6a7e2SMugunthan V N 	cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
837a9d6a7e2SMugunthan V N 
8384de2de51SSekhar Nori #ifdef OMAP_HSMMC_USE_GPIO
8392558c049SLokesh Vutla 	plat->cd_inverted = fdtdec_get_bool(fdt, node, "cd-inverted");
8404de2de51SSekhar Nori #endif
841a9d6a7e2SMugunthan V N 
842a9d6a7e2SMugunthan V N 	return 0;
843a9d6a7e2SMugunthan V N }
8442558c049SLokesh Vutla #endif
845a9d6a7e2SMugunthan V N 
84617c9a1c1SJean-Jacques Hiblot #ifdef CONFIG_BLK
84717c9a1c1SJean-Jacques Hiblot 
84817c9a1c1SJean-Jacques Hiblot static int omap_hsmmc_bind(struct udevice *dev)
84917c9a1c1SJean-Jacques Hiblot {
85017c9a1c1SJean-Jacques Hiblot 	struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
85117c9a1c1SJean-Jacques Hiblot 
85217c9a1c1SJean-Jacques Hiblot 	return mmc_bind(dev, &plat->mmc, &plat->cfg);
85317c9a1c1SJean-Jacques Hiblot }
85417c9a1c1SJean-Jacques Hiblot #endif
855a9d6a7e2SMugunthan V N static int omap_hsmmc_probe(struct udevice *dev)
856a9d6a7e2SMugunthan V N {
8573d673ffcSJean-Jacques Hiblot 	struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
858a9d6a7e2SMugunthan V N 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
859a9d6a7e2SMugunthan V N 	struct omap_hsmmc_data *priv = dev_get_priv(dev);
8603d673ffcSJean-Jacques Hiblot 	struct mmc_config *cfg = &plat->cfg;
861a9d6a7e2SMugunthan V N 	struct mmc *mmc;
862a9d6a7e2SMugunthan V N 
863a9d6a7e2SMugunthan V N 	cfg->name = "OMAP SD/MMC";
8642558c049SLokesh Vutla 	priv->base_addr = plat->base_addr;
8652558c049SLokesh Vutla #ifdef OMAP_HSMMC_USE_GPIO
8662558c049SLokesh Vutla 	priv->cd_inverted = plat->cd_inverted;
8672558c049SLokesh Vutla #endif
868a9d6a7e2SMugunthan V N 
86917c9a1c1SJean-Jacques Hiblot #ifdef CONFIG_BLK
87017c9a1c1SJean-Jacques Hiblot 	mmc = &plat->mmc;
87117c9a1c1SJean-Jacques Hiblot #else
872a9d6a7e2SMugunthan V N 	mmc = mmc_create(cfg, priv);
873a9d6a7e2SMugunthan V N 	if (mmc == NULL)
874a9d6a7e2SMugunthan V N 		return -1;
87517c9a1c1SJean-Jacques Hiblot #endif
876a9d6a7e2SMugunthan V N 
8772558c049SLokesh Vutla #if defined(OMAP_HSMMC_USE_GPIO) && CONFIG_IS_ENABLED(OF_CONTROL)
8785cc6a245SMugunthan V N 	gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
8795cc6a245SMugunthan V N 	gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
8805cc6a245SMugunthan V N #endif
8815cc6a245SMugunthan V N 
882cffe5d86SSimon Glass 	mmc->dev = dev;
883a9d6a7e2SMugunthan V N 	upriv->mmc = mmc;
884a9d6a7e2SMugunthan V N 
885b5511d6cSJean-Jacques Hiblot 	return omap_hsmmc_init_setup(mmc);
886a9d6a7e2SMugunthan V N }
887a9d6a7e2SMugunthan V N 
8882558c049SLokesh Vutla #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
88946831c1aSAdam Ford static const struct omap2_mmc_platform_config omap3_mmc_pdata = {
89046831c1aSAdam Ford 	.reg_offset = 0,
89146831c1aSAdam Ford };
89246831c1aSAdam Ford 
89346831c1aSAdam Ford static const struct omap2_mmc_platform_config am33xx_mmc_pdata = {
89446831c1aSAdam Ford 	.reg_offset = 0x100,
89546831c1aSAdam Ford };
89646831c1aSAdam Ford 
89746831c1aSAdam Ford static const struct omap2_mmc_platform_config omap4_mmc_pdata = {
89846831c1aSAdam Ford 	.reg_offset = 0x100,
89946831c1aSAdam Ford };
90046831c1aSAdam Ford 
901a9d6a7e2SMugunthan V N static const struct udevice_id omap_hsmmc_ids[] = {
90246831c1aSAdam Ford 	{
90346831c1aSAdam Ford 			.compatible = "ti,omap3-hsmmc",
90446831c1aSAdam Ford 			.data = (ulong)&omap3_mmc_pdata
90546831c1aSAdam Ford 	},
90646831c1aSAdam Ford 	{
90746831c1aSAdam Ford 			.compatible = "ti,omap4-hsmmc",
90846831c1aSAdam Ford 			.data = (ulong)&omap4_mmc_pdata
90946831c1aSAdam Ford 	},
91046831c1aSAdam Ford 	{
91146831c1aSAdam Ford 			.compatible = "ti,am33xx-hsmmc",
91246831c1aSAdam Ford 			.data = (ulong)&am33xx_mmc_pdata
91346831c1aSAdam Ford 	},
914a9d6a7e2SMugunthan V N 	{ }
915a9d6a7e2SMugunthan V N };
9162558c049SLokesh Vutla #endif
917a9d6a7e2SMugunthan V N 
918a9d6a7e2SMugunthan V N U_BOOT_DRIVER(omap_hsmmc) = {
919a9d6a7e2SMugunthan V N 	.name	= "omap_hsmmc",
920a9d6a7e2SMugunthan V N 	.id	= UCLASS_MMC,
9212558c049SLokesh Vutla #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
922a9d6a7e2SMugunthan V N 	.of_match = omap_hsmmc_ids,
923a9d6a7e2SMugunthan V N 	.ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
9242558c049SLokesh Vutla 	.platdata_auto_alloc_size = sizeof(struct omap_hsmmc_plat),
9252558c049SLokesh Vutla #endif
92617c9a1c1SJean-Jacques Hiblot #ifdef CONFIG_BLK
92717c9a1c1SJean-Jacques Hiblot 	.bind = omap_hsmmc_bind,
92817c9a1c1SJean-Jacques Hiblot #endif
929b5511d6cSJean-Jacques Hiblot 	.ops = &omap_hsmmc_ops,
930a9d6a7e2SMugunthan V N 	.probe	= omap_hsmmc_probe,
931a9d6a7e2SMugunthan V N 	.priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
932cbcb1701SLokesh Vutla 	.flags	= DM_FLAG_PRE_RELOC,
933a9d6a7e2SMugunthan V N };
934a9d6a7e2SMugunthan V N #endif
935