xref: /rk3399_rockchip-uboot/drivers/mmc/omap_hsmmc.c (revision 2a48b3a2c4ec0f1775deeac5cfe7e61072b6b894)
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
40*2a48b3a2STom Rini #ifdef CONFIG_MMC_OMAP36XX_PINS
41*2a48b3a2STom Rini #include <asm/arch/mux.h>
42*2a48b3a2STom 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 
59cc22b0c0SNikita Kiryanov struct omap_hsmmc_data {
60cc22b0c0SNikita Kiryanov 	struct hsmmc *base_addr;
6193bfd616SPantelis Antoniou 	struct mmc_config cfg;
62ab769f22SPantelis Antoniou #ifdef OMAP_HSMMC_USE_GPIO
63a9d6a7e2SMugunthan V N #ifdef CONFIG_DM_MMC
64a9d6a7e2SMugunthan V N 	struct gpio_desc cd_gpio;	/* Change Detect GPIO */
65a9d6a7e2SMugunthan V N 	struct gpio_desc wp_gpio;	/* Write Protect GPIO */
66a9d6a7e2SMugunthan V N 	bool cd_inverted;
67a9d6a7e2SMugunthan V N #else
68e874d5b0SNikita Kiryanov 	int cd_gpio;
69e3913f56SNikita Kiryanov 	int wp_gpio;
70ab769f22SPantelis Antoniou #endif
71a9d6a7e2SMugunthan V N #endif
72cc22b0c0SNikita Kiryanov };
73cc22b0c0SNikita Kiryanov 
74eb9a28f6SNishanth Menon /* If we fail after 1 second wait, something is really bad */
75eb9a28f6SNishanth Menon #define MAX_RETRY_MS	1000
76eb9a28f6SNishanth Menon 
77933efe64SSricharan static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
78933efe64SSricharan static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
79933efe64SSricharan 			unsigned int siz);
8014fa2dd0SBalaji T K 
81a9d6a7e2SMugunthan V N #if defined(OMAP_HSMMC_USE_GPIO) && !defined(CONFIG_DM_MMC)
82e874d5b0SNikita Kiryanov static int omap_mmc_setup_gpio_in(int gpio, const char *label)
83e874d5b0SNikita Kiryanov {
845915a2adSSimon Glass 	int ret;
855915a2adSSimon Glass 
865915a2adSSimon Glass #ifndef CONFIG_DM_GPIO
87e874d5b0SNikita Kiryanov 	if (!gpio_is_valid(gpio))
88e874d5b0SNikita Kiryanov 		return -1;
895915a2adSSimon Glass #endif
905915a2adSSimon Glass 	ret = gpio_request(gpio, label);
915915a2adSSimon Glass 	if (ret)
925915a2adSSimon Glass 		return ret;
93e874d5b0SNikita Kiryanov 
945915a2adSSimon Glass 	ret = gpio_direction_input(gpio);
955915a2adSSimon Glass 	if (ret)
965915a2adSSimon Glass 		return ret;
97e874d5b0SNikita Kiryanov 
98e874d5b0SNikita Kiryanov 	return gpio;
99e874d5b0SNikita Kiryanov }
100e874d5b0SNikita Kiryanov #endif
101e874d5b0SNikita Kiryanov 
102750121c3SJeroen Hofstee static unsigned char mmc_board_init(struct mmc *mmc)
103de941241SSukumar Ghorai {
104de941241SSukumar Ghorai #if defined(CONFIG_OMAP34XX)
105de941241SSukumar Ghorai 	t2_t *t2_base = (t2_t *)T2_BASE;
106de941241SSukumar Ghorai 	struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
107b1e725f2SGrazvydas Ignotas 	u32 pbias_lite;
1086aca17c9SAdam Ford #ifdef CONFIG_MMC_OMAP36XX_PINS
1096aca17c9SAdam Ford 	u32 wkup_ctrl = readl(OMAP34XX_CTRL_WKUP_CTRL);
1106aca17c9SAdam Ford #endif
111de941241SSukumar Ghorai 
112b1e725f2SGrazvydas Ignotas 	pbias_lite = readl(&t2_base->pbias_lite);
113b1e725f2SGrazvydas Ignotas 	pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
1145bfdd1fcSAlbert ARIBAUD \(3ADEV\) #ifdef CONFIG_TARGET_OMAP3_CAIRO
1155bfdd1fcSAlbert ARIBAUD \(3ADEV\) 	/* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
1165bfdd1fcSAlbert ARIBAUD \(3ADEV\) 	pbias_lite &= ~PBIASLITEVMODE0;
1175bfdd1fcSAlbert ARIBAUD \(3ADEV\) #endif
1186aca17c9SAdam Ford #ifdef CONFIG_MMC_OMAP36XX_PINS
1196aca17c9SAdam Ford 	if (get_cpu_family() == CPU_OMAP36XX) {
1206aca17c9SAdam Ford 		/* Disable extended drain IO before changing PBIAS */
1216aca17c9SAdam Ford 		wkup_ctrl &= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ;
1226aca17c9SAdam Ford 		writel(wkup_ctrl, OMAP34XX_CTRL_WKUP_CTRL);
1236aca17c9SAdam Ford 	}
1246aca17c9SAdam Ford #endif
125b1e725f2SGrazvydas Ignotas 	writel(pbias_lite, &t2_base->pbias_lite);
126aac5450eSPaul Kocialkowski 
127b1e725f2SGrazvydas Ignotas 	writel(pbias_lite | PBIASLITEPWRDNZ1 |
128de941241SSukumar Ghorai 		PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
129de941241SSukumar Ghorai 		&t2_base->pbias_lite);
130de941241SSukumar Ghorai 
1316aca17c9SAdam Ford #ifdef CONFIG_MMC_OMAP36XX_PINS
1326aca17c9SAdam Ford 	if (get_cpu_family() == CPU_OMAP36XX)
1336aca17c9SAdam Ford 		/* Enable extended drain IO after changing PBIAS */
1346aca17c9SAdam Ford 		writel(wkup_ctrl |
1356aca17c9SAdam Ford 				OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ,
1366aca17c9SAdam Ford 				OMAP34XX_CTRL_WKUP_CTRL);
1376aca17c9SAdam Ford #endif
138de941241SSukumar Ghorai 	writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
139de941241SSukumar Ghorai 		&t2_base->devconf0);
140de941241SSukumar Ghorai 
141de941241SSukumar Ghorai 	writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
142de941241SSukumar Ghorai 		&t2_base->devconf1);
143de941241SSukumar Ghorai 
144bbbc1ae9SJonathan Solnit 	/* Change from default of 52MHz to 26MHz if necessary */
14593bfd616SPantelis Antoniou 	if (!(mmc->cfg->host_caps & MMC_MODE_HS_52MHz))
146bbbc1ae9SJonathan Solnit 		writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
147bbbc1ae9SJonathan Solnit 			&t2_base->ctl_prog_io1);
148bbbc1ae9SJonathan Solnit 
149de941241SSukumar Ghorai 	writel(readl(&prcm_base->fclken1_core) |
150de941241SSukumar Ghorai 		EN_MMC1 | EN_MMC2 | EN_MMC3,
151de941241SSukumar Ghorai 		&prcm_base->fclken1_core);
152de941241SSukumar Ghorai 
153de941241SSukumar Ghorai 	writel(readl(&prcm_base->iclken1_core) |
154de941241SSukumar Ghorai 		EN_MMC1 | EN_MMC2 | EN_MMC3,
155de941241SSukumar Ghorai 		&prcm_base->iclken1_core);
156de941241SSukumar Ghorai #endif
157de941241SSukumar Ghorai 
158b4b06006SLokesh Vutla #if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
15914fa2dd0SBalaji T K 	/* PBIAS config needed for MMC1 only */
160bcce53d0SSimon Glass 	if (mmc->block_dev.devnum == 0)
161b4b06006SLokesh Vutla 		vmmc_pbias_config(LDO_VOLT_3V0);
162dd23e59dSBalaji T K #endif
163de941241SSukumar Ghorai 
164de941241SSukumar Ghorai 	return 0;
165de941241SSukumar Ghorai }
166de941241SSukumar Ghorai 
167933efe64SSricharan void mmc_init_stream(struct hsmmc *mmc_base)
168de941241SSukumar Ghorai {
169eb9a28f6SNishanth Menon 	ulong start;
170de941241SSukumar Ghorai 
171de941241SSukumar Ghorai 	writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
172de941241SSukumar Ghorai 
173de941241SSukumar Ghorai 	writel(MMC_CMD0, &mmc_base->cmd);
174eb9a28f6SNishanth Menon 	start = get_timer(0);
175eb9a28f6SNishanth Menon 	while (!(readl(&mmc_base->stat) & CC_MASK)) {
176eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
177eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for cc!\n", __func__);
178eb9a28f6SNishanth Menon 			return;
179eb9a28f6SNishanth Menon 		}
180eb9a28f6SNishanth Menon 	}
181de941241SSukumar Ghorai 	writel(CC_MASK, &mmc_base->stat)
182de941241SSukumar Ghorai 		;
183de941241SSukumar Ghorai 	writel(MMC_CMD0, &mmc_base->cmd)
184de941241SSukumar Ghorai 		;
185eb9a28f6SNishanth Menon 	start = get_timer(0);
186eb9a28f6SNishanth Menon 	while (!(readl(&mmc_base->stat) & CC_MASK)) {
187eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
188eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for cc2!\n", __func__);
189eb9a28f6SNishanth Menon 			return;
190eb9a28f6SNishanth Menon 		}
191eb9a28f6SNishanth Menon 	}
192de941241SSukumar Ghorai 	writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
193de941241SSukumar Ghorai }
194de941241SSukumar Ghorai 
195ab769f22SPantelis Antoniou static int omap_hsmmc_init_setup(struct mmc *mmc)
196de941241SSukumar Ghorai {
197cc22b0c0SNikita Kiryanov 	struct hsmmc *mmc_base;
198de941241SSukumar Ghorai 	unsigned int reg_val;
199de941241SSukumar Ghorai 	unsigned int dsor;
200eb9a28f6SNishanth Menon 	ulong start;
201de941241SSukumar Ghorai 
202cc22b0c0SNikita Kiryanov 	mmc_base = ((struct omap_hsmmc_data *)mmc->priv)->base_addr;
20314fa2dd0SBalaji T K 	mmc_board_init(mmc);
204de941241SSukumar Ghorai 
205de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
206de941241SSukumar Ghorai 		&mmc_base->sysconfig);
207eb9a28f6SNishanth Menon 	start = get_timer(0);
208eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
209eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
210eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for cc2!\n", __func__);
211915ffa52SJaehoon Chung 			return -ETIMEDOUT;
212eb9a28f6SNishanth Menon 		}
213eb9a28f6SNishanth Menon 	}
214de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
215eb9a28f6SNishanth Menon 	start = get_timer(0);
216eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
217eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
218eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for softresetall!\n",
219eb9a28f6SNishanth Menon 				__func__);
220915ffa52SJaehoon Chung 			return -ETIMEDOUT;
221eb9a28f6SNishanth Menon 		}
222eb9a28f6SNishanth Menon 	}
223de941241SSukumar Ghorai 	writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
224de941241SSukumar Ghorai 	writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
225de941241SSukumar Ghorai 		&mmc_base->capa);
226de941241SSukumar Ghorai 
227de941241SSukumar Ghorai 	reg_val = readl(&mmc_base->con) & RESERVED_MASK;
228de941241SSukumar Ghorai 
229de941241SSukumar Ghorai 	writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
230de941241SSukumar Ghorai 		MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
231de941241SSukumar Ghorai 		HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
232de941241SSukumar Ghorai 
233de941241SSukumar Ghorai 	dsor = 240;
234de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
235de941241SSukumar Ghorai 		(ICE_STOP | DTO_15THDTO | CEN_DISABLE));
236de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
237de941241SSukumar Ghorai 		(dsor << CLKD_OFFSET) | ICE_OSCILLATE);
238eb9a28f6SNishanth Menon 	start = get_timer(0);
239eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
240eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
241eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for ics!\n", __func__);
242915ffa52SJaehoon Chung 			return -ETIMEDOUT;
243eb9a28f6SNishanth Menon 		}
244eb9a28f6SNishanth Menon 	}
245de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
246de941241SSukumar Ghorai 
247de941241SSukumar Ghorai 	writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
248de941241SSukumar Ghorai 
249de941241SSukumar Ghorai 	writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
250de941241SSukumar Ghorai 		IE_CEB | IE_CCRC | IE_CTO | IE_BRR | IE_BWR | IE_TC | IE_CC,
251de941241SSukumar Ghorai 		&mmc_base->ie);
252de941241SSukumar Ghorai 
253de941241SSukumar Ghorai 	mmc_init_stream(mmc_base);
254de941241SSukumar Ghorai 
255de941241SSukumar Ghorai 	return 0;
256de941241SSukumar Ghorai }
257de941241SSukumar Ghorai 
25825c719e2SGrazvydas Ignotas /*
25925c719e2SGrazvydas Ignotas  * MMC controller internal finite state machine reset
26025c719e2SGrazvydas Ignotas  *
26125c719e2SGrazvydas Ignotas  * Used to reset command or data internal state machines, using respectively
26225c719e2SGrazvydas Ignotas  * SRC or SRD bit of SYSCTL register
26325c719e2SGrazvydas Ignotas  */
26425c719e2SGrazvydas Ignotas static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
26525c719e2SGrazvydas Ignotas {
26625c719e2SGrazvydas Ignotas 	ulong start;
26725c719e2SGrazvydas Ignotas 
26825c719e2SGrazvydas Ignotas 	mmc_reg_out(&mmc_base->sysctl, bit, bit);
26925c719e2SGrazvydas Ignotas 
27061a6cc27SOleksandr Tyshchenko 	/*
27161a6cc27SOleksandr Tyshchenko 	 * CMD(DAT) lines reset procedures are slightly different
27261a6cc27SOleksandr Tyshchenko 	 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
27361a6cc27SOleksandr Tyshchenko 	 * According to OMAP3 TRM:
27461a6cc27SOleksandr Tyshchenko 	 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
27561a6cc27SOleksandr Tyshchenko 	 * returns to 0x0.
27661a6cc27SOleksandr Tyshchenko 	 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
27761a6cc27SOleksandr Tyshchenko 	 * procedure steps must be as follows:
27861a6cc27SOleksandr Tyshchenko 	 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
27961a6cc27SOleksandr Tyshchenko 	 *    MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
28061a6cc27SOleksandr Tyshchenko 	 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
28161a6cc27SOleksandr Tyshchenko 	 * 3. Wait until the SRC (SRD) bit returns to 0x0
28261a6cc27SOleksandr Tyshchenko 	 *    (reset procedure is completed).
28361a6cc27SOleksandr Tyshchenko 	 */
28461a6cc27SOleksandr Tyshchenko #if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
285dce55b93SNikita Kiryanov 	defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
28661a6cc27SOleksandr Tyshchenko 	if (!(readl(&mmc_base->sysctl) & bit)) {
28761a6cc27SOleksandr Tyshchenko 		start = get_timer(0);
28861a6cc27SOleksandr Tyshchenko 		while (!(readl(&mmc_base->sysctl) & bit)) {
28961a6cc27SOleksandr Tyshchenko 			if (get_timer(0) - start > MAX_RETRY_MS)
29061a6cc27SOleksandr Tyshchenko 				return;
29161a6cc27SOleksandr Tyshchenko 		}
29261a6cc27SOleksandr Tyshchenko 	}
29361a6cc27SOleksandr Tyshchenko #endif
29425c719e2SGrazvydas Ignotas 	start = get_timer(0);
29525c719e2SGrazvydas Ignotas 	while ((readl(&mmc_base->sysctl) & bit) != 0) {
29625c719e2SGrazvydas Ignotas 		if (get_timer(0) - start > MAX_RETRY_MS) {
29725c719e2SGrazvydas Ignotas 			printf("%s: timedout waiting for sysctl %x to clear\n",
29825c719e2SGrazvydas Ignotas 				__func__, bit);
29925c719e2SGrazvydas Ignotas 			return;
30025c719e2SGrazvydas Ignotas 		}
30125c719e2SGrazvydas Ignotas 	}
30225c719e2SGrazvydas Ignotas }
303de941241SSukumar Ghorai 
304ab769f22SPantelis Antoniou static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
305de941241SSukumar Ghorai 			struct mmc_data *data)
306de941241SSukumar Ghorai {
307cc22b0c0SNikita Kiryanov 	struct hsmmc *mmc_base;
308de941241SSukumar Ghorai 	unsigned int flags, mmc_stat;
309eb9a28f6SNishanth Menon 	ulong start;
310de941241SSukumar Ghorai 
311cc22b0c0SNikita Kiryanov 	mmc_base = ((struct omap_hsmmc_data *)mmc->priv)->base_addr;
312eb9a28f6SNishanth Menon 	start = get_timer(0);
313a7778f8fSTom Rini 	while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
314eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
315a7778f8fSTom Rini 			printf("%s: timedout waiting on cmd inhibit to clear\n",
316a7778f8fSTom Rini 					__func__);
317915ffa52SJaehoon Chung 			return -ETIMEDOUT;
318eb9a28f6SNishanth Menon 		}
319eb9a28f6SNishanth Menon 	}
320de941241SSukumar Ghorai 	writel(0xFFFFFFFF, &mmc_base->stat);
321eb9a28f6SNishanth Menon 	start = get_timer(0);
322eb9a28f6SNishanth Menon 	while (readl(&mmc_base->stat)) {
323eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
32415ceb1deSGrazvydas Ignotas 			printf("%s: timedout waiting for STAT (%x) to clear\n",
32515ceb1deSGrazvydas Ignotas 				__func__, readl(&mmc_base->stat));
326915ffa52SJaehoon Chung 			return -ETIMEDOUT;
327eb9a28f6SNishanth Menon 		}
328eb9a28f6SNishanth Menon 	}
329de941241SSukumar Ghorai 	/*
330de941241SSukumar Ghorai 	 * CMDREG
331de941241SSukumar Ghorai 	 * CMDIDX[13:8]	: Command index
332de941241SSukumar Ghorai 	 * DATAPRNT[5]	: Data Present Select
333de941241SSukumar Ghorai 	 * ENCMDIDX[4]	: Command Index Check Enable
334de941241SSukumar Ghorai 	 * ENCMDCRC[3]	: Command CRC Check Enable
335de941241SSukumar Ghorai 	 * RSPTYP[1:0]
336de941241SSukumar Ghorai 	 *	00 = No Response
337de941241SSukumar Ghorai 	 *	01 = Length 136
338de941241SSukumar Ghorai 	 *	10 = Length 48
339de941241SSukumar Ghorai 	 *	11 = Length 48 Check busy after response
340de941241SSukumar Ghorai 	 */
341de941241SSukumar Ghorai 	/* Delay added before checking the status of frq change
342de941241SSukumar Ghorai 	 * retry not supported by mmc.c(core file)
343de941241SSukumar Ghorai 	 */
344de941241SSukumar Ghorai 	if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
345de941241SSukumar Ghorai 		udelay(50000); /* wait 50 ms */
346de941241SSukumar Ghorai 
347de941241SSukumar Ghorai 	if (!(cmd->resp_type & MMC_RSP_PRESENT))
348de941241SSukumar Ghorai 		flags = 0;
349de941241SSukumar Ghorai 	else if (cmd->resp_type & MMC_RSP_136)
350de941241SSukumar Ghorai 		flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
351de941241SSukumar Ghorai 	else if (cmd->resp_type & MMC_RSP_BUSY)
352de941241SSukumar Ghorai 		flags = RSP_TYPE_LGHT48B;
353de941241SSukumar Ghorai 	else
354de941241SSukumar Ghorai 		flags = RSP_TYPE_LGHT48;
355de941241SSukumar Ghorai 
356de941241SSukumar Ghorai 	/* enable default flags */
357de941241SSukumar Ghorai 	flags =	flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
358de941241SSukumar Ghorai 			MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE | DE_DISABLE);
359de941241SSukumar Ghorai 
360de941241SSukumar Ghorai 	if (cmd->resp_type & MMC_RSP_CRC)
361de941241SSukumar Ghorai 		flags |= CCCE_CHECK;
362de941241SSukumar Ghorai 	if (cmd->resp_type & MMC_RSP_OPCODE)
363de941241SSukumar Ghorai 		flags |= CICE_CHECK;
364de941241SSukumar Ghorai 
365de941241SSukumar Ghorai 	if (data) {
366de941241SSukumar Ghorai 		if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
367de941241SSukumar Ghorai 			 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
368de941241SSukumar Ghorai 			flags |= (MSBS_MULTIBLK | BCE_ENABLE);
369de941241SSukumar Ghorai 			data->blocksize = 512;
370de941241SSukumar Ghorai 			writel(data->blocksize | (data->blocks << 16),
371de941241SSukumar Ghorai 							&mmc_base->blk);
372de941241SSukumar Ghorai 		} else
373de941241SSukumar Ghorai 			writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
374de941241SSukumar Ghorai 
375de941241SSukumar Ghorai 		if (data->flags & MMC_DATA_READ)
376de941241SSukumar Ghorai 			flags |= (DP_DATA | DDIR_READ);
377de941241SSukumar Ghorai 		else
378de941241SSukumar Ghorai 			flags |= (DP_DATA | DDIR_WRITE);
379de941241SSukumar Ghorai 	}
380de941241SSukumar Ghorai 
381de941241SSukumar Ghorai 	writel(cmd->cmdarg, &mmc_base->arg);
382152ba363SLubomir Popov 	udelay(20);		/* To fix "No status update" error on eMMC */
383de941241SSukumar Ghorai 	writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
384de941241SSukumar Ghorai 
385eb9a28f6SNishanth Menon 	start = get_timer(0);
386de941241SSukumar Ghorai 	do {
387de941241SSukumar Ghorai 		mmc_stat = readl(&mmc_base->stat);
388eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
389de941241SSukumar Ghorai 			printf("%s : timeout: No status update\n", __func__);
390915ffa52SJaehoon Chung 			return -ETIMEDOUT;
391de941241SSukumar Ghorai 		}
392eb9a28f6SNishanth Menon 	} while (!mmc_stat);
393de941241SSukumar Ghorai 
39425c719e2SGrazvydas Ignotas 	if ((mmc_stat & IE_CTO) != 0) {
39525c719e2SGrazvydas Ignotas 		mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
396915ffa52SJaehoon Chung 		return -ETIMEDOUT;
39725c719e2SGrazvydas Ignotas 	} else if ((mmc_stat & ERRI_MASK) != 0)
398de941241SSukumar Ghorai 		return -1;
399de941241SSukumar Ghorai 
400de941241SSukumar Ghorai 	if (mmc_stat & CC_MASK) {
401de941241SSukumar Ghorai 		writel(CC_MASK, &mmc_base->stat);
402de941241SSukumar Ghorai 		if (cmd->resp_type & MMC_RSP_PRESENT) {
403de941241SSukumar Ghorai 			if (cmd->resp_type & MMC_RSP_136) {
404de941241SSukumar Ghorai 				/* response type 2 */
405de941241SSukumar Ghorai 				cmd->response[3] = readl(&mmc_base->rsp10);
406de941241SSukumar Ghorai 				cmd->response[2] = readl(&mmc_base->rsp32);
407de941241SSukumar Ghorai 				cmd->response[1] = readl(&mmc_base->rsp54);
408de941241SSukumar Ghorai 				cmd->response[0] = readl(&mmc_base->rsp76);
409de941241SSukumar Ghorai 			} else
410de941241SSukumar Ghorai 				/* response types 1, 1b, 3, 4, 5, 6 */
411de941241SSukumar Ghorai 				cmd->response[0] = readl(&mmc_base->rsp10);
412de941241SSukumar Ghorai 		}
413de941241SSukumar Ghorai 	}
414de941241SSukumar Ghorai 
415de941241SSukumar Ghorai 	if (data && (data->flags & MMC_DATA_READ)) {
416de941241SSukumar Ghorai 		mmc_read_data(mmc_base,	data->dest,
417de941241SSukumar Ghorai 				data->blocksize * data->blocks);
418de941241SSukumar Ghorai 	} else if (data && (data->flags & MMC_DATA_WRITE)) {
419de941241SSukumar Ghorai 		mmc_write_data(mmc_base, data->src,
420de941241SSukumar Ghorai 				data->blocksize * data->blocks);
421de941241SSukumar Ghorai 	}
422de941241SSukumar Ghorai 	return 0;
423de941241SSukumar Ghorai }
424de941241SSukumar Ghorai 
425933efe64SSricharan static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
426de941241SSukumar Ghorai {
427de941241SSukumar Ghorai 	unsigned int *output_buf = (unsigned int *)buf;
428de941241SSukumar Ghorai 	unsigned int mmc_stat;
429de941241SSukumar Ghorai 	unsigned int count;
430de941241SSukumar Ghorai 
431de941241SSukumar Ghorai 	/*
432de941241SSukumar Ghorai 	 * Start Polled Read
433de941241SSukumar Ghorai 	 */
434de941241SSukumar Ghorai 	count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
435de941241SSukumar Ghorai 	count /= 4;
436de941241SSukumar Ghorai 
437de941241SSukumar Ghorai 	while (size) {
438eb9a28f6SNishanth Menon 		ulong start = get_timer(0);
439de941241SSukumar Ghorai 		do {
440de941241SSukumar Ghorai 			mmc_stat = readl(&mmc_base->stat);
441eb9a28f6SNishanth Menon 			if (get_timer(0) - start > MAX_RETRY_MS) {
442eb9a28f6SNishanth Menon 				printf("%s: timedout waiting for status!\n",
443eb9a28f6SNishanth Menon 						__func__);
444915ffa52SJaehoon Chung 				return -ETIMEDOUT;
445eb9a28f6SNishanth Menon 			}
446de941241SSukumar Ghorai 		} while (mmc_stat == 0);
447de941241SSukumar Ghorai 
44825c719e2SGrazvydas Ignotas 		if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
44925c719e2SGrazvydas Ignotas 			mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
45025c719e2SGrazvydas Ignotas 
451de941241SSukumar Ghorai 		if ((mmc_stat & ERRI_MASK) != 0)
452de941241SSukumar Ghorai 			return 1;
453de941241SSukumar Ghorai 
454de941241SSukumar Ghorai 		if (mmc_stat & BRR_MASK) {
455de941241SSukumar Ghorai 			unsigned int k;
456de941241SSukumar Ghorai 
457de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BRR_MASK,
458de941241SSukumar Ghorai 				&mmc_base->stat);
459de941241SSukumar Ghorai 			for (k = 0; k < count; k++) {
460de941241SSukumar Ghorai 				*output_buf = readl(&mmc_base->data);
461de941241SSukumar Ghorai 				output_buf++;
462de941241SSukumar Ghorai 			}
463de941241SSukumar Ghorai 			size -= (count*4);
464de941241SSukumar Ghorai 		}
465de941241SSukumar Ghorai 
466de941241SSukumar Ghorai 		if (mmc_stat & BWR_MASK)
467de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BWR_MASK,
468de941241SSukumar Ghorai 				&mmc_base->stat);
469de941241SSukumar Ghorai 
470de941241SSukumar Ghorai 		if (mmc_stat & TC_MASK) {
471de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | TC_MASK,
472de941241SSukumar Ghorai 				&mmc_base->stat);
473de941241SSukumar Ghorai 			break;
474de941241SSukumar Ghorai 		}
475de941241SSukumar Ghorai 	}
476de941241SSukumar Ghorai 	return 0;
477de941241SSukumar Ghorai }
478de941241SSukumar Ghorai 
479933efe64SSricharan static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
480933efe64SSricharan 				unsigned int size)
481de941241SSukumar Ghorai {
482de941241SSukumar Ghorai 	unsigned int *input_buf = (unsigned int *)buf;
483de941241SSukumar Ghorai 	unsigned int mmc_stat;
484de941241SSukumar Ghorai 	unsigned int count;
485de941241SSukumar Ghorai 
486de941241SSukumar Ghorai 	/*
487152ba363SLubomir Popov 	 * Start Polled Write
488de941241SSukumar Ghorai 	 */
489de941241SSukumar Ghorai 	count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
490de941241SSukumar Ghorai 	count /= 4;
491de941241SSukumar Ghorai 
492de941241SSukumar Ghorai 	while (size) {
493eb9a28f6SNishanth Menon 		ulong start = get_timer(0);
494de941241SSukumar Ghorai 		do {
495de941241SSukumar Ghorai 			mmc_stat = readl(&mmc_base->stat);
496eb9a28f6SNishanth Menon 			if (get_timer(0) - start > MAX_RETRY_MS) {
497eb9a28f6SNishanth Menon 				printf("%s: timedout waiting for status!\n",
498eb9a28f6SNishanth Menon 						__func__);
499915ffa52SJaehoon Chung 				return -ETIMEDOUT;
500eb9a28f6SNishanth Menon 			}
501de941241SSukumar Ghorai 		} while (mmc_stat == 0);
502de941241SSukumar Ghorai 
50325c719e2SGrazvydas Ignotas 		if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
50425c719e2SGrazvydas Ignotas 			mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
50525c719e2SGrazvydas Ignotas 
506de941241SSukumar Ghorai 		if ((mmc_stat & ERRI_MASK) != 0)
507de941241SSukumar Ghorai 			return 1;
508de941241SSukumar Ghorai 
509de941241SSukumar Ghorai 		if (mmc_stat & BWR_MASK) {
510de941241SSukumar Ghorai 			unsigned int k;
511de941241SSukumar Ghorai 
512de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BWR_MASK,
513de941241SSukumar Ghorai 					&mmc_base->stat);
514de941241SSukumar Ghorai 			for (k = 0; k < count; k++) {
515de941241SSukumar Ghorai 				writel(*input_buf, &mmc_base->data);
516de941241SSukumar Ghorai 				input_buf++;
517de941241SSukumar Ghorai 			}
518de941241SSukumar Ghorai 			size -= (count*4);
519de941241SSukumar Ghorai 		}
520de941241SSukumar Ghorai 
521de941241SSukumar Ghorai 		if (mmc_stat & BRR_MASK)
522de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BRR_MASK,
523de941241SSukumar Ghorai 				&mmc_base->stat);
524de941241SSukumar Ghorai 
525de941241SSukumar Ghorai 		if (mmc_stat & TC_MASK) {
526de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | TC_MASK,
527de941241SSukumar Ghorai 				&mmc_base->stat);
528de941241SSukumar Ghorai 			break;
529de941241SSukumar Ghorai 		}
530de941241SSukumar Ghorai 	}
531de941241SSukumar Ghorai 	return 0;
532de941241SSukumar Ghorai }
533de941241SSukumar Ghorai 
53407b0b9c0SJaehoon Chung static int omap_hsmmc_set_ios(struct mmc *mmc)
535de941241SSukumar Ghorai {
536cc22b0c0SNikita Kiryanov 	struct hsmmc *mmc_base;
537de941241SSukumar Ghorai 	unsigned int dsor = 0;
538eb9a28f6SNishanth Menon 	ulong start;
539de941241SSukumar Ghorai 
540cc22b0c0SNikita Kiryanov 	mmc_base = ((struct omap_hsmmc_data *)mmc->priv)->base_addr;
541de941241SSukumar Ghorai 	/* configue bus width */
542de941241SSukumar Ghorai 	switch (mmc->bus_width) {
543de941241SSukumar Ghorai 	case 8:
544de941241SSukumar Ghorai 		writel(readl(&mmc_base->con) | DTW_8_BITMODE,
545de941241SSukumar Ghorai 			&mmc_base->con);
546de941241SSukumar Ghorai 		break;
547de941241SSukumar Ghorai 
548de941241SSukumar Ghorai 	case 4:
549de941241SSukumar Ghorai 		writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
550de941241SSukumar Ghorai 			&mmc_base->con);
551de941241SSukumar Ghorai 		writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
552de941241SSukumar Ghorai 			&mmc_base->hctl);
553de941241SSukumar Ghorai 		break;
554de941241SSukumar Ghorai 
555de941241SSukumar Ghorai 	case 1:
556de941241SSukumar Ghorai 	default:
557de941241SSukumar Ghorai 		writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
558de941241SSukumar Ghorai 			&mmc_base->con);
559de941241SSukumar Ghorai 		writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
560de941241SSukumar Ghorai 			&mmc_base->hctl);
561de941241SSukumar Ghorai 		break;
562de941241SSukumar Ghorai 	}
563de941241SSukumar Ghorai 
564de941241SSukumar Ghorai 	/* configure clock with 96Mhz system clock.
565de941241SSukumar Ghorai 	 */
566de941241SSukumar Ghorai 	if (mmc->clock != 0) {
567de941241SSukumar Ghorai 		dsor = (MMC_CLOCK_REFERENCE * 1000000 / mmc->clock);
568de941241SSukumar Ghorai 		if ((MMC_CLOCK_REFERENCE * 1000000) / dsor > mmc->clock)
569de941241SSukumar Ghorai 			dsor++;
570de941241SSukumar Ghorai 	}
571de941241SSukumar Ghorai 
572de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
573de941241SSukumar Ghorai 				(ICE_STOP | DTO_15THDTO | CEN_DISABLE));
574de941241SSukumar Ghorai 
575de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
576de941241SSukumar Ghorai 				(dsor << CLKD_OFFSET) | ICE_OSCILLATE);
577de941241SSukumar Ghorai 
578eb9a28f6SNishanth Menon 	start = get_timer(0);
579eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
580eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
581eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for ics!\n", __func__);
58207b0b9c0SJaehoon Chung 			return -ETIMEDOUT;
583eb9a28f6SNishanth Menon 		}
584eb9a28f6SNishanth Menon 	}
585de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
58607b0b9c0SJaehoon Chung 
58707b0b9c0SJaehoon Chung 	return 0;
588de941241SSukumar Ghorai }
589de941241SSukumar Ghorai 
590ab769f22SPantelis Antoniou #ifdef OMAP_HSMMC_USE_GPIO
591a9d6a7e2SMugunthan V N #ifdef CONFIG_DM_MMC
592a9d6a7e2SMugunthan V N static int omap_hsmmc_getcd(struct mmc *mmc)
593a9d6a7e2SMugunthan V N {
594a9d6a7e2SMugunthan V N 	struct omap_hsmmc_data *priv = mmc->priv;
595a9d6a7e2SMugunthan V N 	int value;
596a9d6a7e2SMugunthan V N 
597a9d6a7e2SMugunthan V N 	value = dm_gpio_get_value(&priv->cd_gpio);
598a9d6a7e2SMugunthan V N 	/* if no CD return as 1 */
599a9d6a7e2SMugunthan V N 	if (value < 0)
600a9d6a7e2SMugunthan V N 		return 1;
601a9d6a7e2SMugunthan V N 
602a9d6a7e2SMugunthan V N 	if (priv->cd_inverted)
603a9d6a7e2SMugunthan V N 		return !value;
604a9d6a7e2SMugunthan V N 	return value;
605a9d6a7e2SMugunthan V N }
606a9d6a7e2SMugunthan V N 
607a9d6a7e2SMugunthan V N static int omap_hsmmc_getwp(struct mmc *mmc)
608a9d6a7e2SMugunthan V N {
609a9d6a7e2SMugunthan V N 	struct omap_hsmmc_data *priv = mmc->priv;
610a9d6a7e2SMugunthan V N 	int value;
611a9d6a7e2SMugunthan V N 
612a9d6a7e2SMugunthan V N 	value = dm_gpio_get_value(&priv->wp_gpio);
613a9d6a7e2SMugunthan V N 	/* if no WP return as 0 */
614a9d6a7e2SMugunthan V N 	if (value < 0)
615a9d6a7e2SMugunthan V N 		return 0;
616a9d6a7e2SMugunthan V N 	return value;
617a9d6a7e2SMugunthan V N }
618a9d6a7e2SMugunthan V N #else
619ab769f22SPantelis Antoniou static int omap_hsmmc_getcd(struct mmc *mmc)
620ab769f22SPantelis Antoniou {
621ab769f22SPantelis Antoniou 	struct omap_hsmmc_data *priv_data = mmc->priv;
622ab769f22SPantelis Antoniou 	int cd_gpio;
623ab769f22SPantelis Antoniou 
624ab769f22SPantelis Antoniou 	/* if no CD return as 1 */
625ab769f22SPantelis Antoniou 	cd_gpio = priv_data->cd_gpio;
626ab769f22SPantelis Antoniou 	if (cd_gpio < 0)
627ab769f22SPantelis Antoniou 		return 1;
628ab769f22SPantelis Antoniou 
6290b03a931SIgor Grinberg 	/* NOTE: assumes card detect signal is active-low */
6300b03a931SIgor Grinberg 	return !gpio_get_value(cd_gpio);
631ab769f22SPantelis Antoniou }
632ab769f22SPantelis Antoniou 
633ab769f22SPantelis Antoniou static int omap_hsmmc_getwp(struct mmc *mmc)
634ab769f22SPantelis Antoniou {
635ab769f22SPantelis Antoniou 	struct omap_hsmmc_data *priv_data = mmc->priv;
636ab769f22SPantelis Antoniou 	int wp_gpio;
637ab769f22SPantelis Antoniou 
638ab769f22SPantelis Antoniou 	/* if no WP return as 0 */
639ab769f22SPantelis Antoniou 	wp_gpio = priv_data->wp_gpio;
640ab769f22SPantelis Antoniou 	if (wp_gpio < 0)
641ab769f22SPantelis Antoniou 		return 0;
642ab769f22SPantelis Antoniou 
6430b03a931SIgor Grinberg 	/* NOTE: assumes write protect signal is active-high */
644ab769f22SPantelis Antoniou 	return gpio_get_value(wp_gpio);
645ab769f22SPantelis Antoniou }
646ab769f22SPantelis Antoniou #endif
647a9d6a7e2SMugunthan V N #endif
648ab769f22SPantelis Antoniou 
649ab769f22SPantelis Antoniou static const struct mmc_ops omap_hsmmc_ops = {
650ab769f22SPantelis Antoniou 	.send_cmd	= omap_hsmmc_send_cmd,
651ab769f22SPantelis Antoniou 	.set_ios	= omap_hsmmc_set_ios,
652ab769f22SPantelis Antoniou 	.init		= omap_hsmmc_init_setup,
653ab769f22SPantelis Antoniou #ifdef OMAP_HSMMC_USE_GPIO
654ab769f22SPantelis Antoniou 	.getcd		= omap_hsmmc_getcd,
655ab769f22SPantelis Antoniou 	.getwp		= omap_hsmmc_getwp,
656ab769f22SPantelis Antoniou #endif
657ab769f22SPantelis Antoniou };
658ab769f22SPantelis Antoniou 
659a9d6a7e2SMugunthan V N #ifndef CONFIG_DM_MMC
660e3913f56SNikita Kiryanov int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
661e3913f56SNikita Kiryanov 		int wp_gpio)
662de941241SSukumar Ghorai {
66393bfd616SPantelis Antoniou 	struct mmc *mmc;
66493bfd616SPantelis Antoniou 	struct omap_hsmmc_data *priv_data;
66593bfd616SPantelis Antoniou 	struct mmc_config *cfg;
66693bfd616SPantelis Antoniou 	uint host_caps_val;
667de941241SSukumar Ghorai 
66893bfd616SPantelis Antoniou 	priv_data = malloc(sizeof(*priv_data));
66993bfd616SPantelis Antoniou 	if (priv_data == NULL)
67093bfd616SPantelis Antoniou 		return -1;
67193bfd616SPantelis Antoniou 
6725a20397bSRob Herring 	host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
673de941241SSukumar Ghorai 
674de941241SSukumar Ghorai 	switch (dev_index) {
675de941241SSukumar Ghorai 	case 0:
676cc22b0c0SNikita Kiryanov 		priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
677de941241SSukumar Ghorai 		break;
6781037d585STom Rini #ifdef OMAP_HSMMC2_BASE
679de941241SSukumar Ghorai 	case 1:
680cc22b0c0SNikita Kiryanov 		priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
681152ba363SLubomir Popov #if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
6823891a54fSNishanth Menon 	defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
6833b68939fSRoger Quadros 	defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
6843b68939fSRoger Quadros 		defined(CONFIG_HSMMC2_8BIT)
685152ba363SLubomir Popov 		/* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
686152ba363SLubomir Popov 		host_caps_val |= MMC_MODE_8BIT;
687152ba363SLubomir Popov #endif
688de941241SSukumar Ghorai 		break;
6891037d585STom Rini #endif
6901037d585STom Rini #ifdef OMAP_HSMMC3_BASE
691de941241SSukumar Ghorai 	case 2:
692cc22b0c0SNikita Kiryanov 		priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
6933891a54fSNishanth Menon #if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
694152ba363SLubomir Popov 		/* Enable 8-bit interface for eMMC on DRA7XX */
695152ba363SLubomir Popov 		host_caps_val |= MMC_MODE_8BIT;
696152ba363SLubomir Popov #endif
697de941241SSukumar Ghorai 		break;
6981037d585STom Rini #endif
699de941241SSukumar Ghorai 	default:
700cc22b0c0SNikita Kiryanov 		priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
701de941241SSukumar Ghorai 		return 1;
702de941241SSukumar Ghorai 	}
703ab769f22SPantelis Antoniou #ifdef OMAP_HSMMC_USE_GPIO
704ab769f22SPantelis Antoniou 	/* on error gpio values are set to -1, which is what we want */
705e874d5b0SNikita Kiryanov 	priv_data->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
706e3913f56SNikita Kiryanov 	priv_data->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
707ab769f22SPantelis Antoniou #endif
708173ddc5bSPeter Korsgaard 
70993bfd616SPantelis Antoniou 	cfg = &priv_data->cfg;
710de941241SSukumar Ghorai 
71193bfd616SPantelis Antoniou 	cfg->name = "OMAP SD/MMC";
71293bfd616SPantelis Antoniou 	cfg->ops = &omap_hsmmc_ops;
71393bfd616SPantelis Antoniou 
71493bfd616SPantelis Antoniou 	cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
71593bfd616SPantelis Antoniou 	cfg->host_caps = host_caps_val & ~host_caps_mask;
71693bfd616SPantelis Antoniou 
71793bfd616SPantelis Antoniou 	cfg->f_min = 400000;
718bbbc1ae9SJonathan Solnit 
719bbbc1ae9SJonathan Solnit 	if (f_max != 0)
72093bfd616SPantelis Antoniou 		cfg->f_max = f_max;
721bbbc1ae9SJonathan Solnit 	else {
72293bfd616SPantelis Antoniou 		if (cfg->host_caps & MMC_MODE_HS) {
72393bfd616SPantelis Antoniou 			if (cfg->host_caps & MMC_MODE_HS_52MHz)
72493bfd616SPantelis Antoniou 				cfg->f_max = 52000000;
725bbbc1ae9SJonathan Solnit 			else
72693bfd616SPantelis Antoniou 				cfg->f_max = 26000000;
727bbbc1ae9SJonathan Solnit 		} else
72893bfd616SPantelis Antoniou 			cfg->f_max = 20000000;
729bbbc1ae9SJonathan Solnit 	}
730de941241SSukumar Ghorai 
73193bfd616SPantelis Antoniou 	cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
7328feafcc4SJohn Rigby 
7334ca9244dSJohn Rigby #if defined(CONFIG_OMAP34XX)
7344ca9244dSJohn Rigby 	/*
7354ca9244dSJohn Rigby 	 * Silicon revs 2.1 and older do not support multiblock transfers.
7364ca9244dSJohn Rigby 	 */
7374ca9244dSJohn Rigby 	if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
73893bfd616SPantelis Antoniou 		cfg->b_max = 1;
7394ca9244dSJohn Rigby #endif
74093bfd616SPantelis Antoniou 	mmc = mmc_create(cfg, priv_data);
74193bfd616SPantelis Antoniou 	if (mmc == NULL)
74293bfd616SPantelis Antoniou 		return -1;
743de941241SSukumar Ghorai 
744de941241SSukumar Ghorai 	return 0;
745de941241SSukumar Ghorai }
746a9d6a7e2SMugunthan V N #else
747a9d6a7e2SMugunthan V N static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
748a9d6a7e2SMugunthan V N {
749a9d6a7e2SMugunthan V N 	struct omap_hsmmc_data *priv = dev_get_priv(dev);
750a9d6a7e2SMugunthan V N 	const void *fdt = gd->fdt_blob;
751e160f7d4SSimon Glass 	int node = dev_of_offset(dev);
752a9d6a7e2SMugunthan V N 	struct mmc_config *cfg;
753a9d6a7e2SMugunthan V N 	int val;
754a9d6a7e2SMugunthan V N 
7554bc5e19eSMugunthan V N 	priv->base_addr = map_physmem(dev_get_addr(dev), sizeof(struct hsmmc *),
7564bc5e19eSMugunthan V N 				      MAP_NOCACHE);
757a9d6a7e2SMugunthan V N 	cfg = &priv->cfg;
758a9d6a7e2SMugunthan V N 
759a9d6a7e2SMugunthan V N 	cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
760a9d6a7e2SMugunthan V N 	val = fdtdec_get_int(fdt, node, "bus-width", -1);
761a9d6a7e2SMugunthan V N 	if (val < 0) {
762a9d6a7e2SMugunthan V N 		printf("error: bus-width property missing\n");
763a9d6a7e2SMugunthan V N 		return -ENOENT;
764a9d6a7e2SMugunthan V N 	}
765a9d6a7e2SMugunthan V N 
766a9d6a7e2SMugunthan V N 	switch (val) {
767a9d6a7e2SMugunthan V N 	case 0x8:
768a9d6a7e2SMugunthan V N 		cfg->host_caps |= MMC_MODE_8BIT;
769a9d6a7e2SMugunthan V N 	case 0x4:
770a9d6a7e2SMugunthan V N 		cfg->host_caps |= MMC_MODE_4BIT;
771a9d6a7e2SMugunthan V N 		break;
772a9d6a7e2SMugunthan V N 	default:
773a9d6a7e2SMugunthan V N 		printf("error: invalid bus-width property\n");
774a9d6a7e2SMugunthan V N 		return -ENOENT;
775a9d6a7e2SMugunthan V N 	}
776a9d6a7e2SMugunthan V N 
777a9d6a7e2SMugunthan V N 	cfg->f_min = 400000;
778a9d6a7e2SMugunthan V N 	cfg->f_max = fdtdec_get_int(fdt, node, "max-frequency", 52000000);
779a9d6a7e2SMugunthan V N 	cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
780a9d6a7e2SMugunthan V N 	cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
781a9d6a7e2SMugunthan V N 
7824de2de51SSekhar Nori #ifdef OMAP_HSMMC_USE_GPIO
783a9d6a7e2SMugunthan V N 	priv->cd_inverted = fdtdec_get_bool(fdt, node, "cd-inverted");
7844de2de51SSekhar Nori #endif
785a9d6a7e2SMugunthan V N 
786a9d6a7e2SMugunthan V N 	return 0;
787a9d6a7e2SMugunthan V N }
788a9d6a7e2SMugunthan V N 
789a9d6a7e2SMugunthan V N static int omap_hsmmc_probe(struct udevice *dev)
790a9d6a7e2SMugunthan V N {
791a9d6a7e2SMugunthan V N 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
792a9d6a7e2SMugunthan V N 	struct omap_hsmmc_data *priv = dev_get_priv(dev);
793a9d6a7e2SMugunthan V N 	struct mmc_config *cfg;
794a9d6a7e2SMugunthan V N 	struct mmc *mmc;
795a9d6a7e2SMugunthan V N 
796a9d6a7e2SMugunthan V N 	cfg = &priv->cfg;
797a9d6a7e2SMugunthan V N 	cfg->name = "OMAP SD/MMC";
798a9d6a7e2SMugunthan V N 	cfg->ops = &omap_hsmmc_ops;
799a9d6a7e2SMugunthan V N 
800a9d6a7e2SMugunthan V N 	mmc = mmc_create(cfg, priv);
801a9d6a7e2SMugunthan V N 	if (mmc == NULL)
802a9d6a7e2SMugunthan V N 		return -1;
803a9d6a7e2SMugunthan V N 
8045cc6a245SMugunthan V N #ifdef OMAP_HSMMC_USE_GPIO
8055cc6a245SMugunthan V N 	gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
8065cc6a245SMugunthan V N 	gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
8075cc6a245SMugunthan V N #endif
8085cc6a245SMugunthan V N 
809cffe5d86SSimon Glass 	mmc->dev = dev;
810a9d6a7e2SMugunthan V N 	upriv->mmc = mmc;
811a9d6a7e2SMugunthan V N 
812a9d6a7e2SMugunthan V N 	return 0;
813a9d6a7e2SMugunthan V N }
814a9d6a7e2SMugunthan V N 
815a9d6a7e2SMugunthan V N static const struct udevice_id omap_hsmmc_ids[] = {
816a9d6a7e2SMugunthan V N 	{ .compatible = "ti,omap3-hsmmc" },
817a9d6a7e2SMugunthan V N 	{ .compatible = "ti,omap4-hsmmc" },
818a9d6a7e2SMugunthan V N 	{ .compatible = "ti,am33xx-hsmmc" },
819a9d6a7e2SMugunthan V N 	{ }
820a9d6a7e2SMugunthan V N };
821a9d6a7e2SMugunthan V N 
822a9d6a7e2SMugunthan V N U_BOOT_DRIVER(omap_hsmmc) = {
823a9d6a7e2SMugunthan V N 	.name	= "omap_hsmmc",
824a9d6a7e2SMugunthan V N 	.id	= UCLASS_MMC,
825a9d6a7e2SMugunthan V N 	.of_match = omap_hsmmc_ids,
826a9d6a7e2SMugunthan V N 	.ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
827a9d6a7e2SMugunthan V N 	.probe	= omap_hsmmc_probe,
828a9d6a7e2SMugunthan V N 	.priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
829a9d6a7e2SMugunthan V N };
830a9d6a7e2SMugunthan V N #endif
831