xref: /rk3399_rockchip-uboot/drivers/mmc/omap_hsmmc.c (revision 25c719e2c3247ef0bab28ee562f978e2ce9211e3)
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>
27de941241SSukumar Ghorai #include <mmc.h>
28de941241SSukumar Ghorai #include <part.h>
29de941241SSukumar Ghorai #include <i2c.h>
30de941241SSukumar Ghorai #include <twl4030.h>
3114fa2dd0SBalaji T K #include <twl6030.h>
32de941241SSukumar Ghorai #include <asm/io.h>
33de941241SSukumar Ghorai #include <asm/arch/mmc_host_def.h>
3496e0e7b3SDirk Behme #include <asm/arch/sys_proto.h>
35de941241SSukumar Ghorai 
36*25c719e2SGrazvydas Ignotas /* common definitions for all OMAPs */
37*25c719e2SGrazvydas Ignotas #define SYSCTL_SRC	(1 << 25)
38*25c719e2SGrazvydas Ignotas #define SYSCTL_SRD	(1 << 26)
39*25c719e2SGrazvydas Ignotas 
40eb9a28f6SNishanth Menon /* If we fail after 1 second wait, something is really bad */
41eb9a28f6SNishanth Menon #define MAX_RETRY_MS	1000
42eb9a28f6SNishanth Menon 
43933efe64SSricharan static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
44933efe64SSricharan static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
45933efe64SSricharan 			unsigned int siz);
46de941241SSukumar Ghorai static struct mmc hsmmc_dev[2];
4714fa2dd0SBalaji T K 
4814fa2dd0SBalaji T K #if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
4914fa2dd0SBalaji T K static void omap4_vmmc_pbias_config(struct mmc *mmc)
5014fa2dd0SBalaji T K {
5114fa2dd0SBalaji T K 	u32 value = 0;
5214fa2dd0SBalaji T K 	struct omap4_sys_ctrl_regs *const ctrl =
5314fa2dd0SBalaji T K 		(struct omap4_sys_ctrl_regs *)SYSCTRL_GENERAL_CORE_BASE;
5414fa2dd0SBalaji T K 
5514fa2dd0SBalaji T K 
5614fa2dd0SBalaji T K 	value = readl(&ctrl->control_pbiaslite);
5714fa2dd0SBalaji T K 	value &= ~(MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ);
5814fa2dd0SBalaji T K 	writel(value, &ctrl->control_pbiaslite);
5914fa2dd0SBalaji T K 	/* set VMMC to 3V */
6014fa2dd0SBalaji T K 	twl6030_power_mmc_init();
6114fa2dd0SBalaji T K 	value = readl(&ctrl->control_pbiaslite);
6214fa2dd0SBalaji T K 	value |= MMC1_PBIASLITE_VMODE | MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ;
6314fa2dd0SBalaji T K 	writel(value, &ctrl->control_pbiaslite);
6414fa2dd0SBalaji T K }
6514fa2dd0SBalaji T K #endif
6614fa2dd0SBalaji T K 
6714fa2dd0SBalaji T K unsigned char mmc_board_init(struct mmc *mmc)
68de941241SSukumar Ghorai {
69de941241SSukumar Ghorai #if defined(CONFIG_OMAP34XX)
70de941241SSukumar Ghorai 	t2_t *t2_base = (t2_t *)T2_BASE;
71de941241SSukumar Ghorai 	struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
72b1e725f2SGrazvydas Ignotas 	u32 pbias_lite;
73de941241SSukumar Ghorai 
74b1e725f2SGrazvydas Ignotas 	pbias_lite = readl(&t2_base->pbias_lite);
75b1e725f2SGrazvydas Ignotas 	pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
76b1e725f2SGrazvydas Ignotas 	writel(pbias_lite, &t2_base->pbias_lite);
77b1e725f2SGrazvydas Ignotas #endif
78b1e725f2SGrazvydas Ignotas #if defined(CONFIG_TWL4030_POWER)
79b1e725f2SGrazvydas Ignotas 	twl4030_power_mmc_init();
80b1e725f2SGrazvydas Ignotas 	mdelay(100);	/* ramp-up delay from Linux code */
81b1e725f2SGrazvydas Ignotas #endif
82b1e725f2SGrazvydas Ignotas #if defined(CONFIG_OMAP34XX)
83b1e725f2SGrazvydas Ignotas 	writel(pbias_lite | PBIASLITEPWRDNZ1 |
84de941241SSukumar Ghorai 		PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
85de941241SSukumar Ghorai 		&t2_base->pbias_lite);
86de941241SSukumar Ghorai 
87de941241SSukumar Ghorai 	writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
88de941241SSukumar Ghorai 		&t2_base->devconf0);
89de941241SSukumar Ghorai 
90de941241SSukumar Ghorai 	writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
91de941241SSukumar Ghorai 		&t2_base->devconf1);
92de941241SSukumar Ghorai 
93de941241SSukumar Ghorai 	writel(readl(&prcm_base->fclken1_core) |
94de941241SSukumar Ghorai 		EN_MMC1 | EN_MMC2 | EN_MMC3,
95de941241SSukumar Ghorai 		&prcm_base->fclken1_core);
96de941241SSukumar Ghorai 
97de941241SSukumar Ghorai 	writel(readl(&prcm_base->iclken1_core) |
98de941241SSukumar Ghorai 		EN_MMC1 | EN_MMC2 | EN_MMC3,
99de941241SSukumar Ghorai 		&prcm_base->iclken1_core);
100de941241SSukumar Ghorai #endif
101de941241SSukumar Ghorai 
10214fa2dd0SBalaji T K #if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
10314fa2dd0SBalaji T K 	/* PBIAS config needed for MMC1 only */
10414fa2dd0SBalaji T K 	if (mmc->block_dev.dev == 0)
10514fa2dd0SBalaji T K 		omap4_vmmc_pbias_config(mmc);
10614fa2dd0SBalaji T K #endif
107de941241SSukumar Ghorai 
108de941241SSukumar Ghorai 	return 0;
109de941241SSukumar Ghorai }
110de941241SSukumar Ghorai 
111933efe64SSricharan void mmc_init_stream(struct hsmmc *mmc_base)
112de941241SSukumar Ghorai {
113eb9a28f6SNishanth Menon 	ulong start;
114de941241SSukumar Ghorai 
115de941241SSukumar Ghorai 	writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
116de941241SSukumar Ghorai 
117de941241SSukumar Ghorai 	writel(MMC_CMD0, &mmc_base->cmd);
118eb9a28f6SNishanth Menon 	start = get_timer(0);
119eb9a28f6SNishanth Menon 	while (!(readl(&mmc_base->stat) & CC_MASK)) {
120eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
121eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for cc!\n", __func__);
122eb9a28f6SNishanth Menon 			return;
123eb9a28f6SNishanth Menon 		}
124eb9a28f6SNishanth Menon 	}
125de941241SSukumar Ghorai 	writel(CC_MASK, &mmc_base->stat)
126de941241SSukumar Ghorai 		;
127de941241SSukumar Ghorai 	writel(MMC_CMD0, &mmc_base->cmd)
128de941241SSukumar Ghorai 		;
129eb9a28f6SNishanth Menon 	start = get_timer(0);
130eb9a28f6SNishanth Menon 	while (!(readl(&mmc_base->stat) & CC_MASK)) {
131eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
132eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for cc2!\n", __func__);
133eb9a28f6SNishanth Menon 			return;
134eb9a28f6SNishanth Menon 		}
135eb9a28f6SNishanth Menon 	}
136de941241SSukumar Ghorai 	writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
137de941241SSukumar Ghorai }
138de941241SSukumar Ghorai 
139de941241SSukumar Ghorai 
140de941241SSukumar Ghorai static int mmc_init_setup(struct mmc *mmc)
141de941241SSukumar Ghorai {
142933efe64SSricharan 	struct hsmmc *mmc_base = (struct hsmmc *)mmc->priv;
143de941241SSukumar Ghorai 	unsigned int reg_val;
144de941241SSukumar Ghorai 	unsigned int dsor;
145eb9a28f6SNishanth Menon 	ulong start;
146de941241SSukumar Ghorai 
14714fa2dd0SBalaji T K 	mmc_board_init(mmc);
148de941241SSukumar Ghorai 
149de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
150de941241SSukumar Ghorai 		&mmc_base->sysconfig);
151eb9a28f6SNishanth Menon 	start = get_timer(0);
152eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
153eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
154eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for cc2!\n", __func__);
155eb9a28f6SNishanth Menon 			return TIMEOUT;
156eb9a28f6SNishanth Menon 		}
157eb9a28f6SNishanth Menon 	}
158de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
159eb9a28f6SNishanth Menon 	start = get_timer(0);
160eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
161eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
162eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for softresetall!\n",
163eb9a28f6SNishanth Menon 				__func__);
164eb9a28f6SNishanth Menon 			return TIMEOUT;
165eb9a28f6SNishanth Menon 		}
166eb9a28f6SNishanth Menon 	}
167de941241SSukumar Ghorai 	writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
168de941241SSukumar Ghorai 	writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
169de941241SSukumar Ghorai 		&mmc_base->capa);
170de941241SSukumar Ghorai 
171de941241SSukumar Ghorai 	reg_val = readl(&mmc_base->con) & RESERVED_MASK;
172de941241SSukumar Ghorai 
173de941241SSukumar Ghorai 	writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
174de941241SSukumar Ghorai 		MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
175de941241SSukumar Ghorai 		HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
176de941241SSukumar Ghorai 
177de941241SSukumar Ghorai 	dsor = 240;
178de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
179de941241SSukumar Ghorai 		(ICE_STOP | DTO_15THDTO | CEN_DISABLE));
180de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
181de941241SSukumar Ghorai 		(dsor << CLKD_OFFSET) | ICE_OSCILLATE);
182eb9a28f6SNishanth Menon 	start = get_timer(0);
183eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
184eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
185eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for ics!\n", __func__);
186eb9a28f6SNishanth Menon 			return TIMEOUT;
187eb9a28f6SNishanth Menon 		}
188eb9a28f6SNishanth Menon 	}
189de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
190de941241SSukumar Ghorai 
191de941241SSukumar Ghorai 	writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
192de941241SSukumar Ghorai 
193de941241SSukumar Ghorai 	writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
194de941241SSukumar Ghorai 		IE_CEB | IE_CCRC | IE_CTO | IE_BRR | IE_BWR | IE_TC | IE_CC,
195de941241SSukumar Ghorai 		&mmc_base->ie);
196de941241SSukumar Ghorai 
197de941241SSukumar Ghorai 	mmc_init_stream(mmc_base);
198de941241SSukumar Ghorai 
199de941241SSukumar Ghorai 	return 0;
200de941241SSukumar Ghorai }
201de941241SSukumar Ghorai 
202*25c719e2SGrazvydas Ignotas /*
203*25c719e2SGrazvydas Ignotas  * MMC controller internal finite state machine reset
204*25c719e2SGrazvydas Ignotas  *
205*25c719e2SGrazvydas Ignotas  * Used to reset command or data internal state machines, using respectively
206*25c719e2SGrazvydas Ignotas  * SRC or SRD bit of SYSCTL register
207*25c719e2SGrazvydas Ignotas  */
208*25c719e2SGrazvydas Ignotas static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
209*25c719e2SGrazvydas Ignotas {
210*25c719e2SGrazvydas Ignotas 	ulong start;
211*25c719e2SGrazvydas Ignotas 
212*25c719e2SGrazvydas Ignotas 	mmc_reg_out(&mmc_base->sysctl, bit, bit);
213*25c719e2SGrazvydas Ignotas 
214*25c719e2SGrazvydas Ignotas 	start = get_timer(0);
215*25c719e2SGrazvydas Ignotas 	while ((readl(&mmc_base->sysctl) & bit) != 0) {
216*25c719e2SGrazvydas Ignotas 		if (get_timer(0) - start > MAX_RETRY_MS) {
217*25c719e2SGrazvydas Ignotas 			printf("%s: timedout waiting for sysctl %x to clear\n",
218*25c719e2SGrazvydas Ignotas 				__func__, bit);
219*25c719e2SGrazvydas Ignotas 			return;
220*25c719e2SGrazvydas Ignotas 		}
221*25c719e2SGrazvydas Ignotas 	}
222*25c719e2SGrazvydas Ignotas }
223de941241SSukumar Ghorai 
224de941241SSukumar Ghorai static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
225de941241SSukumar Ghorai 			struct mmc_data *data)
226de941241SSukumar Ghorai {
227933efe64SSricharan 	struct hsmmc *mmc_base = (struct hsmmc *)mmc->priv;
228de941241SSukumar Ghorai 	unsigned int flags, mmc_stat;
229eb9a28f6SNishanth Menon 	ulong start;
230de941241SSukumar Ghorai 
231eb9a28f6SNishanth Menon 	start = get_timer(0);
232a7778f8fSTom Rini 	while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
233eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
234a7778f8fSTom Rini 			printf("%s: timedout waiting on cmd inhibit to clear\n",
235a7778f8fSTom Rini 					__func__);
236eb9a28f6SNishanth Menon 			return TIMEOUT;
237eb9a28f6SNishanth Menon 		}
238eb9a28f6SNishanth Menon 	}
239de941241SSukumar Ghorai 	writel(0xFFFFFFFF, &mmc_base->stat);
240eb9a28f6SNishanth Menon 	start = get_timer(0);
241eb9a28f6SNishanth Menon 	while (readl(&mmc_base->stat)) {
242eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
24315ceb1deSGrazvydas Ignotas 			printf("%s: timedout waiting for STAT (%x) to clear\n",
24415ceb1deSGrazvydas Ignotas 				__func__, readl(&mmc_base->stat));
245eb9a28f6SNishanth Menon 			return TIMEOUT;
246eb9a28f6SNishanth Menon 		}
247eb9a28f6SNishanth Menon 	}
248de941241SSukumar Ghorai 	/*
249de941241SSukumar Ghorai 	 * CMDREG
250de941241SSukumar Ghorai 	 * CMDIDX[13:8]	: Command index
251de941241SSukumar Ghorai 	 * DATAPRNT[5]	: Data Present Select
252de941241SSukumar Ghorai 	 * ENCMDIDX[4]	: Command Index Check Enable
253de941241SSukumar Ghorai 	 * ENCMDCRC[3]	: Command CRC Check Enable
254de941241SSukumar Ghorai 	 * RSPTYP[1:0]
255de941241SSukumar Ghorai 	 *	00 = No Response
256de941241SSukumar Ghorai 	 *	01 = Length 136
257de941241SSukumar Ghorai 	 *	10 = Length 48
258de941241SSukumar Ghorai 	 *	11 = Length 48 Check busy after response
259de941241SSukumar Ghorai 	 */
260de941241SSukumar Ghorai 	/* Delay added before checking the status of frq change
261de941241SSukumar Ghorai 	 * retry not supported by mmc.c(core file)
262de941241SSukumar Ghorai 	 */
263de941241SSukumar Ghorai 	if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
264de941241SSukumar Ghorai 		udelay(50000); /* wait 50 ms */
265de941241SSukumar Ghorai 
266de941241SSukumar Ghorai 	if (!(cmd->resp_type & MMC_RSP_PRESENT))
267de941241SSukumar Ghorai 		flags = 0;
268de941241SSukumar Ghorai 	else if (cmd->resp_type & MMC_RSP_136)
269de941241SSukumar Ghorai 		flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
270de941241SSukumar Ghorai 	else if (cmd->resp_type & MMC_RSP_BUSY)
271de941241SSukumar Ghorai 		flags = RSP_TYPE_LGHT48B;
272de941241SSukumar Ghorai 	else
273de941241SSukumar Ghorai 		flags = RSP_TYPE_LGHT48;
274de941241SSukumar Ghorai 
275de941241SSukumar Ghorai 	/* enable default flags */
276de941241SSukumar Ghorai 	flags =	flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
277de941241SSukumar Ghorai 			MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE | DE_DISABLE);
278de941241SSukumar Ghorai 
279de941241SSukumar Ghorai 	if (cmd->resp_type & MMC_RSP_CRC)
280de941241SSukumar Ghorai 		flags |= CCCE_CHECK;
281de941241SSukumar Ghorai 	if (cmd->resp_type & MMC_RSP_OPCODE)
282de941241SSukumar Ghorai 		flags |= CICE_CHECK;
283de941241SSukumar Ghorai 
284de941241SSukumar Ghorai 	if (data) {
285de941241SSukumar Ghorai 		if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
286de941241SSukumar Ghorai 			 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
287de941241SSukumar Ghorai 			flags |= (MSBS_MULTIBLK | BCE_ENABLE);
288de941241SSukumar Ghorai 			data->blocksize = 512;
289de941241SSukumar Ghorai 			writel(data->blocksize | (data->blocks << 16),
290de941241SSukumar Ghorai 							&mmc_base->blk);
291de941241SSukumar Ghorai 		} else
292de941241SSukumar Ghorai 			writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
293de941241SSukumar Ghorai 
294de941241SSukumar Ghorai 		if (data->flags & MMC_DATA_READ)
295de941241SSukumar Ghorai 			flags |= (DP_DATA | DDIR_READ);
296de941241SSukumar Ghorai 		else
297de941241SSukumar Ghorai 			flags |= (DP_DATA | DDIR_WRITE);
298de941241SSukumar Ghorai 	}
299de941241SSukumar Ghorai 
300de941241SSukumar Ghorai 	writel(cmd->cmdarg, &mmc_base->arg);
301de941241SSukumar Ghorai 	writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
302de941241SSukumar Ghorai 
303eb9a28f6SNishanth Menon 	start = get_timer(0);
304de941241SSukumar Ghorai 	do {
305de941241SSukumar Ghorai 		mmc_stat = readl(&mmc_base->stat);
306eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
307de941241SSukumar Ghorai 			printf("%s : timeout: No status update\n", __func__);
308de941241SSukumar Ghorai 			return TIMEOUT;
309de941241SSukumar Ghorai 		}
310eb9a28f6SNishanth Menon 	} while (!mmc_stat);
311de941241SSukumar Ghorai 
312*25c719e2SGrazvydas Ignotas 	if ((mmc_stat & IE_CTO) != 0) {
313*25c719e2SGrazvydas Ignotas 		mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
314de941241SSukumar Ghorai 		return TIMEOUT;
315*25c719e2SGrazvydas Ignotas 	} else if ((mmc_stat & ERRI_MASK) != 0)
316de941241SSukumar Ghorai 		return -1;
317de941241SSukumar Ghorai 
318de941241SSukumar Ghorai 	if (mmc_stat & CC_MASK) {
319de941241SSukumar Ghorai 		writel(CC_MASK, &mmc_base->stat);
320de941241SSukumar Ghorai 		if (cmd->resp_type & MMC_RSP_PRESENT) {
321de941241SSukumar Ghorai 			if (cmd->resp_type & MMC_RSP_136) {
322de941241SSukumar Ghorai 				/* response type 2 */
323de941241SSukumar Ghorai 				cmd->response[3] = readl(&mmc_base->rsp10);
324de941241SSukumar Ghorai 				cmd->response[2] = readl(&mmc_base->rsp32);
325de941241SSukumar Ghorai 				cmd->response[1] = readl(&mmc_base->rsp54);
326de941241SSukumar Ghorai 				cmd->response[0] = readl(&mmc_base->rsp76);
327de941241SSukumar Ghorai 			} else
328de941241SSukumar Ghorai 				/* response types 1, 1b, 3, 4, 5, 6 */
329de941241SSukumar Ghorai 				cmd->response[0] = readl(&mmc_base->rsp10);
330de941241SSukumar Ghorai 		}
331de941241SSukumar Ghorai 	}
332de941241SSukumar Ghorai 
333de941241SSukumar Ghorai 	if (data && (data->flags & MMC_DATA_READ)) {
334de941241SSukumar Ghorai 		mmc_read_data(mmc_base,	data->dest,
335de941241SSukumar Ghorai 				data->blocksize * data->blocks);
336de941241SSukumar Ghorai 	} else if (data && (data->flags & MMC_DATA_WRITE)) {
337de941241SSukumar Ghorai 		mmc_write_data(mmc_base, data->src,
338de941241SSukumar Ghorai 				data->blocksize * data->blocks);
339de941241SSukumar Ghorai 	}
340de941241SSukumar Ghorai 	return 0;
341de941241SSukumar Ghorai }
342de941241SSukumar Ghorai 
343933efe64SSricharan static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
344de941241SSukumar Ghorai {
345de941241SSukumar Ghorai 	unsigned int *output_buf = (unsigned int *)buf;
346de941241SSukumar Ghorai 	unsigned int mmc_stat;
347de941241SSukumar Ghorai 	unsigned int count;
348de941241SSukumar Ghorai 
349de941241SSukumar Ghorai 	/*
350de941241SSukumar Ghorai 	 * Start Polled Read
351de941241SSukumar Ghorai 	 */
352de941241SSukumar Ghorai 	count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
353de941241SSukumar Ghorai 	count /= 4;
354de941241SSukumar Ghorai 
355de941241SSukumar Ghorai 	while (size) {
356eb9a28f6SNishanth Menon 		ulong start = get_timer(0);
357de941241SSukumar Ghorai 		do {
358de941241SSukumar Ghorai 			mmc_stat = readl(&mmc_base->stat);
359eb9a28f6SNishanth Menon 			if (get_timer(0) - start > MAX_RETRY_MS) {
360eb9a28f6SNishanth Menon 				printf("%s: timedout waiting for status!\n",
361eb9a28f6SNishanth Menon 						__func__);
362eb9a28f6SNishanth Menon 				return TIMEOUT;
363eb9a28f6SNishanth Menon 			}
364de941241SSukumar Ghorai 		} while (mmc_stat == 0);
365de941241SSukumar Ghorai 
366*25c719e2SGrazvydas Ignotas 		if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
367*25c719e2SGrazvydas Ignotas 			mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
368*25c719e2SGrazvydas Ignotas 
369de941241SSukumar Ghorai 		if ((mmc_stat & ERRI_MASK) != 0)
370de941241SSukumar Ghorai 			return 1;
371de941241SSukumar Ghorai 
372de941241SSukumar Ghorai 		if (mmc_stat & BRR_MASK) {
373de941241SSukumar Ghorai 			unsigned int k;
374de941241SSukumar Ghorai 
375de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BRR_MASK,
376de941241SSukumar Ghorai 				&mmc_base->stat);
377de941241SSukumar Ghorai 			for (k = 0; k < count; k++) {
378de941241SSukumar Ghorai 				*output_buf = readl(&mmc_base->data);
379de941241SSukumar Ghorai 				output_buf++;
380de941241SSukumar Ghorai 			}
381de941241SSukumar Ghorai 			size -= (count*4);
382de941241SSukumar Ghorai 		}
383de941241SSukumar Ghorai 
384de941241SSukumar Ghorai 		if (mmc_stat & BWR_MASK)
385de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BWR_MASK,
386de941241SSukumar Ghorai 				&mmc_base->stat);
387de941241SSukumar Ghorai 
388de941241SSukumar Ghorai 		if (mmc_stat & TC_MASK) {
389de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | TC_MASK,
390de941241SSukumar Ghorai 				&mmc_base->stat);
391de941241SSukumar Ghorai 			break;
392de941241SSukumar Ghorai 		}
393de941241SSukumar Ghorai 	}
394de941241SSukumar Ghorai 	return 0;
395de941241SSukumar Ghorai }
396de941241SSukumar Ghorai 
397933efe64SSricharan static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
398933efe64SSricharan 				unsigned int size)
399de941241SSukumar Ghorai {
400de941241SSukumar Ghorai 	unsigned int *input_buf = (unsigned int *)buf;
401de941241SSukumar Ghorai 	unsigned int mmc_stat;
402de941241SSukumar Ghorai 	unsigned int count;
403de941241SSukumar Ghorai 
404de941241SSukumar Ghorai 	/*
405de941241SSukumar Ghorai 	 * Start Polled Read
406de941241SSukumar Ghorai 	 */
407de941241SSukumar Ghorai 	count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
408de941241SSukumar Ghorai 	count /= 4;
409de941241SSukumar Ghorai 
410de941241SSukumar Ghorai 	while (size) {
411eb9a28f6SNishanth Menon 		ulong start = get_timer(0);
412de941241SSukumar Ghorai 		do {
413de941241SSukumar Ghorai 			mmc_stat = readl(&mmc_base->stat);
414eb9a28f6SNishanth Menon 			if (get_timer(0) - start > MAX_RETRY_MS) {
415eb9a28f6SNishanth Menon 				printf("%s: timedout waiting for status!\n",
416eb9a28f6SNishanth Menon 						__func__);
417eb9a28f6SNishanth Menon 				return TIMEOUT;
418eb9a28f6SNishanth Menon 			}
419de941241SSukumar Ghorai 		} while (mmc_stat == 0);
420de941241SSukumar Ghorai 
421*25c719e2SGrazvydas Ignotas 		if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
422*25c719e2SGrazvydas Ignotas 			mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
423*25c719e2SGrazvydas Ignotas 
424de941241SSukumar Ghorai 		if ((mmc_stat & ERRI_MASK) != 0)
425de941241SSukumar Ghorai 			return 1;
426de941241SSukumar Ghorai 
427de941241SSukumar Ghorai 		if (mmc_stat & BWR_MASK) {
428de941241SSukumar Ghorai 			unsigned int k;
429de941241SSukumar Ghorai 
430de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BWR_MASK,
431de941241SSukumar Ghorai 					&mmc_base->stat);
432de941241SSukumar Ghorai 			for (k = 0; k < count; k++) {
433de941241SSukumar Ghorai 				writel(*input_buf, &mmc_base->data);
434de941241SSukumar Ghorai 				input_buf++;
435de941241SSukumar Ghorai 			}
436de941241SSukumar Ghorai 			size -= (count*4);
437de941241SSukumar Ghorai 		}
438de941241SSukumar Ghorai 
439de941241SSukumar Ghorai 		if (mmc_stat & BRR_MASK)
440de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | BRR_MASK,
441de941241SSukumar Ghorai 				&mmc_base->stat);
442de941241SSukumar Ghorai 
443de941241SSukumar Ghorai 		if (mmc_stat & TC_MASK) {
444de941241SSukumar Ghorai 			writel(readl(&mmc_base->stat) | TC_MASK,
445de941241SSukumar Ghorai 				&mmc_base->stat);
446de941241SSukumar Ghorai 			break;
447de941241SSukumar Ghorai 		}
448de941241SSukumar Ghorai 	}
449de941241SSukumar Ghorai 	return 0;
450de941241SSukumar Ghorai }
451de941241SSukumar Ghorai 
452de941241SSukumar Ghorai static void mmc_set_ios(struct mmc *mmc)
453de941241SSukumar Ghorai {
454933efe64SSricharan 	struct hsmmc *mmc_base = (struct hsmmc *)mmc->priv;
455de941241SSukumar Ghorai 	unsigned int dsor = 0;
456eb9a28f6SNishanth Menon 	ulong start;
457de941241SSukumar Ghorai 
458de941241SSukumar Ghorai 	/* configue bus width */
459de941241SSukumar Ghorai 	switch (mmc->bus_width) {
460de941241SSukumar Ghorai 	case 8:
461de941241SSukumar Ghorai 		writel(readl(&mmc_base->con) | DTW_8_BITMODE,
462de941241SSukumar Ghorai 			&mmc_base->con);
463de941241SSukumar Ghorai 		break;
464de941241SSukumar Ghorai 
465de941241SSukumar Ghorai 	case 4:
466de941241SSukumar Ghorai 		writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
467de941241SSukumar Ghorai 			&mmc_base->con);
468de941241SSukumar Ghorai 		writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
469de941241SSukumar Ghorai 			&mmc_base->hctl);
470de941241SSukumar Ghorai 		break;
471de941241SSukumar Ghorai 
472de941241SSukumar Ghorai 	case 1:
473de941241SSukumar Ghorai 	default:
474de941241SSukumar Ghorai 		writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
475de941241SSukumar Ghorai 			&mmc_base->con);
476de941241SSukumar Ghorai 		writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
477de941241SSukumar Ghorai 			&mmc_base->hctl);
478de941241SSukumar Ghorai 		break;
479de941241SSukumar Ghorai 	}
480de941241SSukumar Ghorai 
481de941241SSukumar Ghorai 	/* configure clock with 96Mhz system clock.
482de941241SSukumar Ghorai 	 */
483de941241SSukumar Ghorai 	if (mmc->clock != 0) {
484de941241SSukumar Ghorai 		dsor = (MMC_CLOCK_REFERENCE * 1000000 / mmc->clock);
485de941241SSukumar Ghorai 		if ((MMC_CLOCK_REFERENCE * 1000000) / dsor > mmc->clock)
486de941241SSukumar Ghorai 			dsor++;
487de941241SSukumar Ghorai 	}
488de941241SSukumar Ghorai 
489de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
490de941241SSukumar Ghorai 				(ICE_STOP | DTO_15THDTO | CEN_DISABLE));
491de941241SSukumar Ghorai 
492de941241SSukumar Ghorai 	mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
493de941241SSukumar Ghorai 				(dsor << CLKD_OFFSET) | ICE_OSCILLATE);
494de941241SSukumar Ghorai 
495eb9a28f6SNishanth Menon 	start = get_timer(0);
496eb9a28f6SNishanth Menon 	while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
497eb9a28f6SNishanth Menon 		if (get_timer(0) - start > MAX_RETRY_MS) {
498eb9a28f6SNishanth Menon 			printf("%s: timedout waiting for ics!\n", __func__);
499eb9a28f6SNishanth Menon 			return;
500eb9a28f6SNishanth Menon 		}
501eb9a28f6SNishanth Menon 	}
502de941241SSukumar Ghorai 	writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
503de941241SSukumar Ghorai }
504de941241SSukumar Ghorai 
505de941241SSukumar Ghorai int omap_mmc_init(int dev_index)
506de941241SSukumar Ghorai {
507de941241SSukumar Ghorai 	struct mmc *mmc;
508de941241SSukumar Ghorai 
509de941241SSukumar Ghorai 	mmc = &hsmmc_dev[dev_index];
510de941241SSukumar Ghorai 
511de941241SSukumar Ghorai 	sprintf(mmc->name, "OMAP SD/MMC");
512de941241SSukumar Ghorai 	mmc->send_cmd = mmc_send_cmd;
513de941241SSukumar Ghorai 	mmc->set_ios = mmc_set_ios;
514de941241SSukumar Ghorai 	mmc->init = mmc_init_setup;
51548972d90SThierry Reding 	mmc->getcd = NULL;
516de941241SSukumar Ghorai 
517de941241SSukumar Ghorai 	switch (dev_index) {
518de941241SSukumar Ghorai 	case 0:
519933efe64SSricharan 		mmc->priv = (struct hsmmc *)OMAP_HSMMC1_BASE;
520de941241SSukumar Ghorai 		break;
5211037d585STom Rini #ifdef OMAP_HSMMC2_BASE
522de941241SSukumar Ghorai 	case 1:
523933efe64SSricharan 		mmc->priv = (struct hsmmc *)OMAP_HSMMC2_BASE;
524de941241SSukumar Ghorai 		break;
5251037d585STom Rini #endif
5261037d585STom Rini #ifdef OMAP_HSMMC3_BASE
527de941241SSukumar Ghorai 	case 2:
528933efe64SSricharan 		mmc->priv = (struct hsmmc *)OMAP_HSMMC3_BASE;
529de941241SSukumar Ghorai 		break;
5301037d585STom Rini #endif
531de941241SSukumar Ghorai 	default:
532933efe64SSricharan 		mmc->priv = (struct hsmmc *)OMAP_HSMMC1_BASE;
533de941241SSukumar Ghorai 		return 1;
534de941241SSukumar Ghorai 	}
535de941241SSukumar Ghorai 	mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
536ecd9af88SBalaji T K 	mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS |
537ecd9af88SBalaji T K 				MMC_MODE_HC;
538de941241SSukumar Ghorai 
539de941241SSukumar Ghorai 	mmc->f_min = 400000;
540de941241SSukumar Ghorai 	mmc->f_max = 52000000;
541de941241SSukumar Ghorai 
5428feafcc4SJohn Rigby 	mmc->b_max = 0;
5438feafcc4SJohn Rigby 
5444ca9244dSJohn Rigby #if defined(CONFIG_OMAP34XX)
5454ca9244dSJohn Rigby 	/*
5464ca9244dSJohn Rigby 	 * Silicon revs 2.1 and older do not support multiblock transfers.
5474ca9244dSJohn Rigby 	 */
5484ca9244dSJohn Rigby 	if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
5494ca9244dSJohn Rigby 		mmc->b_max = 1;
5504ca9244dSJohn Rigby #endif
5514ca9244dSJohn Rigby 
552de941241SSukumar Ghorai 	mmc_register(mmc);
553de941241SSukumar Ghorai 
554de941241SSukumar Ghorai 	return 0;
555de941241SSukumar Ghorai }
556