xref: /openbmc/u-boot/drivers/mmc/davinci_mmc.c (revision 07b0b9c0)
157418d21SSandeep Paulraj /*
257418d21SSandeep Paulraj  * Davinci MMC Controller Driver
357418d21SSandeep Paulraj  *
457418d21SSandeep Paulraj  * Copyright (C) 2010 Texas Instruments Incorporated
557418d21SSandeep Paulraj  *
61a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
757418d21SSandeep Paulraj  */
857418d21SSandeep Paulraj 
957418d21SSandeep Paulraj #include <config.h>
1057418d21SSandeep Paulraj #include <common.h>
1157418d21SSandeep Paulraj #include <command.h>
12915ffa52SJaehoon Chung #include <errno.h>
1357418d21SSandeep Paulraj #include <mmc.h>
1457418d21SSandeep Paulraj #include <part.h>
1557418d21SSandeep Paulraj #include <malloc.h>
1657418d21SSandeep Paulraj #include <asm/io.h>
1757418d21SSandeep Paulraj #include <asm/arch/sdmmc_defs.h>
1857418d21SSandeep Paulraj 
1957418d21SSandeep Paulraj #define DAVINCI_MAX_BLOCKS	(32)
2057418d21SSandeep Paulraj #define WATCHDOG_COUNT		(100000)
2157418d21SSandeep Paulraj 
2257418d21SSandeep Paulraj #define get_val(addr)		REG(addr)
2357418d21SSandeep Paulraj #define set_val(addr, val)	REG(addr) = (val)
2457418d21SSandeep Paulraj #define set_bit(addr, val)	set_val((addr), (get_val(addr) | (val)))
2557418d21SSandeep Paulraj #define clear_bit(addr, val)	set_val((addr), (get_val(addr) & ~(val)))
2657418d21SSandeep Paulraj 
2757418d21SSandeep Paulraj /* Set davinci clock prescalar value based on the required clock in HZ */
2857418d21SSandeep Paulraj static void dmmc_set_clock(struct mmc *mmc, uint clock)
2957418d21SSandeep Paulraj {
3057418d21SSandeep Paulraj 	struct davinci_mmc *host = mmc->priv;
3157418d21SSandeep Paulraj 	struct davinci_mmc_regs *regs = host->reg_base;
3257418d21SSandeep Paulraj 	uint clkrt, sysclk2, act_clock;
3357418d21SSandeep Paulraj 
3493bfd616SPantelis Antoniou 	if (clock < mmc->cfg->f_min)
3593bfd616SPantelis Antoniou 		clock = mmc->cfg->f_min;
3693bfd616SPantelis Antoniou 	if (clock > mmc->cfg->f_max)
3793bfd616SPantelis Antoniou 		clock = mmc->cfg->f_max;
3857418d21SSandeep Paulraj 
3957418d21SSandeep Paulraj 	set_val(&regs->mmcclk, 0);
4057418d21SSandeep Paulraj 	sysclk2 = host->input_clk;
4157418d21SSandeep Paulraj 	clkrt = (sysclk2 / (2 * clock)) - 1;
4257418d21SSandeep Paulraj 
4357418d21SSandeep Paulraj 	/* Calculate the actual clock for the divider used */
4457418d21SSandeep Paulraj 	act_clock = (sysclk2 / (2 * (clkrt + 1)));
4557418d21SSandeep Paulraj 
4657418d21SSandeep Paulraj 	/* Adjust divider if actual clock exceeds the required clock */
4757418d21SSandeep Paulraj 	if (act_clock > clock)
4857418d21SSandeep Paulraj 		clkrt++;
4957418d21SSandeep Paulraj 
5057418d21SSandeep Paulraj 	/* check clock divider boundary and correct it */
5157418d21SSandeep Paulraj 	if (clkrt > 0xFF)
5257418d21SSandeep Paulraj 		clkrt = 0xFF;
5357418d21SSandeep Paulraj 
5457418d21SSandeep Paulraj 	set_val(&regs->mmcclk, (clkrt | MMCCLK_CLKEN));
5557418d21SSandeep Paulraj }
5657418d21SSandeep Paulraj 
5757418d21SSandeep Paulraj /* Status bit wait loop for MMCST1 */
5857418d21SSandeep Paulraj static int
5957418d21SSandeep Paulraj dmmc_wait_fifo_status(volatile struct davinci_mmc_regs *regs, uint status)
6057418d21SSandeep Paulraj {
6179b05d59SHeiko Schocher 	uint wdog = WATCHDOG_COUNT;
6279b05d59SHeiko Schocher 
6357418d21SSandeep Paulraj 	while (--wdog && ((get_val(&regs->mmcst1) & status) != status))
6457418d21SSandeep Paulraj 		udelay(10);
6557418d21SSandeep Paulraj 
6657418d21SSandeep Paulraj 	if (!(get_val(&regs->mmcctl) & MMCCTL_WIDTH_4_BIT))
6757418d21SSandeep Paulraj 		udelay(100);
6857418d21SSandeep Paulraj 
6957418d21SSandeep Paulraj 	if (wdog == 0)
70915ffa52SJaehoon Chung 		return -ECOMM;
7157418d21SSandeep Paulraj 
7257418d21SSandeep Paulraj 	return 0;
7357418d21SSandeep Paulraj }
7457418d21SSandeep Paulraj 
7557418d21SSandeep Paulraj /* Busy bit wait loop for MMCST1 */
7657418d21SSandeep Paulraj static int dmmc_busy_wait(volatile struct davinci_mmc_regs *regs)
7757418d21SSandeep Paulraj {
7879b05d59SHeiko Schocher 	uint wdog = WATCHDOG_COUNT;
7957418d21SSandeep Paulraj 
8057418d21SSandeep Paulraj 	while (--wdog && (get_val(&regs->mmcst1) & MMCST1_BUSY))
8157418d21SSandeep Paulraj 		udelay(10);
8257418d21SSandeep Paulraj 
8357418d21SSandeep Paulraj 	if (wdog == 0)
84915ffa52SJaehoon Chung 		return -ECOMM;
8557418d21SSandeep Paulraj 
8657418d21SSandeep Paulraj 	return 0;
8757418d21SSandeep Paulraj }
8857418d21SSandeep Paulraj 
8957418d21SSandeep Paulraj /* Status bit wait loop for MMCST0 - Checks for error bits as well */
9057418d21SSandeep Paulraj static int dmmc_check_status(volatile struct davinci_mmc_regs *regs,
9157418d21SSandeep Paulraj 		uint *cur_st, uint st_ready, uint st_error)
9257418d21SSandeep Paulraj {
9357418d21SSandeep Paulraj 	uint wdog = WATCHDOG_COUNT;
9457418d21SSandeep Paulraj 	uint mmcstatus = *cur_st;
9557418d21SSandeep Paulraj 
9657418d21SSandeep Paulraj 	while (wdog--) {
9757418d21SSandeep Paulraj 		if (mmcstatus & st_ready) {
9857418d21SSandeep Paulraj 			*cur_st = mmcstatus;
9957418d21SSandeep Paulraj 			mmcstatus = get_val(&regs->mmcst1);
10057418d21SSandeep Paulraj 			return 0;
10157418d21SSandeep Paulraj 		} else if (mmcstatus & st_error) {
10257418d21SSandeep Paulraj 			if (mmcstatus & MMCST0_TOUTRS)
103915ffa52SJaehoon Chung 				return -ETIMEDOUT;
10457418d21SSandeep Paulraj 			printf("[ ST0 ERROR %x]\n", mmcstatus);
10557418d21SSandeep Paulraj 			/*
10657418d21SSandeep Paulraj 			 * Ignore CRC errors as some MMC cards fail to
10757418d21SSandeep Paulraj 			 * initialize on DM365-EVM on the SD1 slot
10857418d21SSandeep Paulraj 			 */
10957418d21SSandeep Paulraj 			if (mmcstatus & MMCST0_CRCRS)
11057418d21SSandeep Paulraj 				return 0;
111915ffa52SJaehoon Chung 			return -ECOMM;
11257418d21SSandeep Paulraj 		}
11357418d21SSandeep Paulraj 		udelay(10);
11457418d21SSandeep Paulraj 
11557418d21SSandeep Paulraj 		mmcstatus = get_val(&regs->mmcst0);
11657418d21SSandeep Paulraj 	}
11757418d21SSandeep Paulraj 
11857418d21SSandeep Paulraj 	printf("Status %x Timeout ST0:%x ST1:%x\n", st_ready, mmcstatus,
11957418d21SSandeep Paulraj 			get_val(&regs->mmcst1));
120915ffa52SJaehoon Chung 	return -ECOMM;
12157418d21SSandeep Paulraj }
12257418d21SSandeep Paulraj 
12357418d21SSandeep Paulraj /*
12457418d21SSandeep Paulraj  * Sends a command out on the bus.  Takes the mmc pointer,
12557418d21SSandeep Paulraj  * a command pointer, and an optional data pointer.
12657418d21SSandeep Paulraj  */
12757418d21SSandeep Paulraj static int
12857418d21SSandeep Paulraj dmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
12957418d21SSandeep Paulraj {
13057418d21SSandeep Paulraj 	struct davinci_mmc *host = mmc->priv;
13157418d21SSandeep Paulraj 	volatile struct davinci_mmc_regs *regs = host->reg_base;
13257418d21SSandeep Paulraj 	uint mmcstatus, status_rdy, status_err;
13357418d21SSandeep Paulraj 	uint i, cmddata, bytes_left = 0;
13457418d21SSandeep Paulraj 	int fifo_words, fifo_bytes, err;
13557418d21SSandeep Paulraj 	char *data_buf = NULL;
13657418d21SSandeep Paulraj 
13757418d21SSandeep Paulraj 	/* Clear status registers */
13857418d21SSandeep Paulraj 	mmcstatus = get_val(&regs->mmcst0);
13957418d21SSandeep Paulraj 	fifo_words = (host->version == MMC_CTLR_VERSION_2) ? 16 : 8;
14057418d21SSandeep Paulraj 	fifo_bytes = fifo_words << 2;
14157418d21SSandeep Paulraj 
14257418d21SSandeep Paulraj 	/* Wait for any previous busy signal to be cleared */
14357418d21SSandeep Paulraj 	dmmc_busy_wait(regs);
14457418d21SSandeep Paulraj 
14557418d21SSandeep Paulraj 	cmddata = cmd->cmdidx;
14657418d21SSandeep Paulraj 	cmddata |= MMCCMD_PPLEN;
14757418d21SSandeep Paulraj 
14857418d21SSandeep Paulraj 	/* Send init clock for CMD0 */
14957418d21SSandeep Paulraj 	if (cmd->cmdidx == MMC_CMD_GO_IDLE_STATE)
15057418d21SSandeep Paulraj 		cmddata |= MMCCMD_INITCK;
15157418d21SSandeep Paulraj 
15257418d21SSandeep Paulraj 	switch (cmd->resp_type) {
15357418d21SSandeep Paulraj 	case MMC_RSP_R1b:
15457418d21SSandeep Paulraj 		cmddata |= MMCCMD_BSYEXP;
15557418d21SSandeep Paulraj 		/* Fall-through */
15657418d21SSandeep Paulraj 	case MMC_RSP_R1:    /* R1, R1b, R5, R6, R7 */
15757418d21SSandeep Paulraj 		cmddata |= MMCCMD_RSPFMT_R1567;
15857418d21SSandeep Paulraj 		break;
15957418d21SSandeep Paulraj 	case MMC_RSP_R2:
16057418d21SSandeep Paulraj 		cmddata |= MMCCMD_RSPFMT_R2;
16157418d21SSandeep Paulraj 		break;
16257418d21SSandeep Paulraj 	case MMC_RSP_R3: /* R3, R4 */
16357418d21SSandeep Paulraj 		cmddata |= MMCCMD_RSPFMT_R3;
16457418d21SSandeep Paulraj 		break;
16557418d21SSandeep Paulraj 	}
16657418d21SSandeep Paulraj 
16757418d21SSandeep Paulraj 	set_val(&regs->mmcim, 0);
16857418d21SSandeep Paulraj 
16957418d21SSandeep Paulraj 	if (data) {
17057418d21SSandeep Paulraj 		/* clear previous data transfer if any and set new one */
17157418d21SSandeep Paulraj 		bytes_left = (data->blocksize * data->blocks);
17257418d21SSandeep Paulraj 
17357418d21SSandeep Paulraj 		/* Reset FIFO - Always use 32 byte fifo threshold */
17457418d21SSandeep Paulraj 		set_val(&regs->mmcfifoctl,
17557418d21SSandeep Paulraj 				(MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
17657418d21SSandeep Paulraj 
17757418d21SSandeep Paulraj 		if (host->version == MMC_CTLR_VERSION_2)
17857418d21SSandeep Paulraj 			cmddata |= MMCCMD_DMATRIG;
17957418d21SSandeep Paulraj 
18057418d21SSandeep Paulraj 		cmddata |= MMCCMD_WDATX;
18157418d21SSandeep Paulraj 		if (data->flags == MMC_DATA_READ) {
18257418d21SSandeep Paulraj 			set_val(&regs->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
18357418d21SSandeep Paulraj 		} else if (data->flags == MMC_DATA_WRITE) {
18457418d21SSandeep Paulraj 			set_val(&regs->mmcfifoctl,
18557418d21SSandeep Paulraj 					(MMCFIFOCTL_FIFOLEV |
18657418d21SSandeep Paulraj 					 MMCFIFOCTL_FIFODIR));
18757418d21SSandeep Paulraj 			cmddata |= MMCCMD_DTRW;
18857418d21SSandeep Paulraj 		}
18957418d21SSandeep Paulraj 
19057418d21SSandeep Paulraj 		set_val(&regs->mmctod, 0xFFFF);
19157418d21SSandeep Paulraj 		set_val(&regs->mmcnblk, (data->blocks & MMCNBLK_NBLK_MASK));
19257418d21SSandeep Paulraj 		set_val(&regs->mmcblen, (data->blocksize & MMCBLEN_BLEN_MASK));
19357418d21SSandeep Paulraj 
19457418d21SSandeep Paulraj 		if (data->flags == MMC_DATA_WRITE) {
19557418d21SSandeep Paulraj 			uint val;
19657418d21SSandeep Paulraj 			data_buf = (char *)data->src;
19757418d21SSandeep Paulraj 			/* For write, fill FIFO with data before issue of CMD */
19857418d21SSandeep Paulraj 			for (i = 0; (i < fifo_words) && bytes_left; i++) {
19957418d21SSandeep Paulraj 				memcpy((char *)&val, data_buf, 4);
20057418d21SSandeep Paulraj 				set_val(&regs->mmcdxr, val);
20157418d21SSandeep Paulraj 				data_buf += 4;
20257418d21SSandeep Paulraj 				bytes_left -= 4;
20357418d21SSandeep Paulraj 			}
20457418d21SSandeep Paulraj 		}
20557418d21SSandeep Paulraj 	} else {
20657418d21SSandeep Paulraj 		set_val(&regs->mmcblen, 0);
20757418d21SSandeep Paulraj 		set_val(&regs->mmcnblk, 0);
20857418d21SSandeep Paulraj 	}
20957418d21SSandeep Paulraj 
21057418d21SSandeep Paulraj 	set_val(&regs->mmctor, 0x1FFF);
21157418d21SSandeep Paulraj 
21257418d21SSandeep Paulraj 	/* Send the command */
21357418d21SSandeep Paulraj 	set_val(&regs->mmcarghl, cmd->cmdarg);
21457418d21SSandeep Paulraj 	set_val(&regs->mmccmd, cmddata);
21557418d21SSandeep Paulraj 
21657418d21SSandeep Paulraj 	status_rdy = MMCST0_RSPDNE;
21757418d21SSandeep Paulraj 	status_err = (MMCST0_TOUTRS | MMCST0_TOUTRD |
21857418d21SSandeep Paulraj 			MMCST0_CRCWR | MMCST0_CRCRD);
21957418d21SSandeep Paulraj 	if (cmd->resp_type & MMC_RSP_CRC)
22057418d21SSandeep Paulraj 		status_err |= MMCST0_CRCRS;
22157418d21SSandeep Paulraj 
22257418d21SSandeep Paulraj 	mmcstatus = get_val(&regs->mmcst0);
22357418d21SSandeep Paulraj 	err = dmmc_check_status(regs, &mmcstatus, status_rdy, status_err);
22457418d21SSandeep Paulraj 	if (err)
22557418d21SSandeep Paulraj 		return err;
22657418d21SSandeep Paulraj 
22757418d21SSandeep Paulraj 	/* For R1b wait for busy done */
22857418d21SSandeep Paulraj 	if (cmd->resp_type == MMC_RSP_R1b)
22957418d21SSandeep Paulraj 		dmmc_busy_wait(regs);
23057418d21SSandeep Paulraj 
23157418d21SSandeep Paulraj 	/* Collect response from controller for specific commands */
23257418d21SSandeep Paulraj 	if (mmcstatus & MMCST0_RSPDNE) {
23357418d21SSandeep Paulraj 		/* Copy the response to the response buffer */
23457418d21SSandeep Paulraj 		if (cmd->resp_type & MMC_RSP_136) {
23557418d21SSandeep Paulraj 			cmd->response[0] = get_val(&regs->mmcrsp67);
23657418d21SSandeep Paulraj 			cmd->response[1] = get_val(&regs->mmcrsp45);
23757418d21SSandeep Paulraj 			cmd->response[2] = get_val(&regs->mmcrsp23);
23857418d21SSandeep Paulraj 			cmd->response[3] = get_val(&regs->mmcrsp01);
23957418d21SSandeep Paulraj 		} else if (cmd->resp_type & MMC_RSP_PRESENT) {
24057418d21SSandeep Paulraj 			cmd->response[0] = get_val(&regs->mmcrsp67);
24157418d21SSandeep Paulraj 		}
24257418d21SSandeep Paulraj 	}
24357418d21SSandeep Paulraj 
24457418d21SSandeep Paulraj 	if (data == NULL)
24557418d21SSandeep Paulraj 		return 0;
24657418d21SSandeep Paulraj 
24757418d21SSandeep Paulraj 	if (data->flags == MMC_DATA_READ) {
24857418d21SSandeep Paulraj 		/* check for DATDNE along with DRRDY as the controller might
24957418d21SSandeep Paulraj 		 * set the DATDNE without DRRDY for smaller transfers with
25057418d21SSandeep Paulraj 		 * less than FIFO threshold bytes
25157418d21SSandeep Paulraj 		 */
25257418d21SSandeep Paulraj 		status_rdy = MMCST0_DRRDY | MMCST0_DATDNE;
25357418d21SSandeep Paulraj 		status_err = MMCST0_TOUTRD | MMCST0_CRCRD;
25457418d21SSandeep Paulraj 		data_buf = data->dest;
25557418d21SSandeep Paulraj 	} else {
25657418d21SSandeep Paulraj 		status_rdy = MMCST0_DXRDY | MMCST0_DATDNE;
25757418d21SSandeep Paulraj 		status_err = MMCST0_CRCWR;
25857418d21SSandeep Paulraj 	}
25957418d21SSandeep Paulraj 
26057418d21SSandeep Paulraj 	/* Wait until all of the blocks are transferred */
26157418d21SSandeep Paulraj 	while (bytes_left) {
26257418d21SSandeep Paulraj 		err = dmmc_check_status(regs, &mmcstatus, status_rdy,
26357418d21SSandeep Paulraj 				status_err);
26457418d21SSandeep Paulraj 		if (err)
26557418d21SSandeep Paulraj 			return err;
26657418d21SSandeep Paulraj 
26757418d21SSandeep Paulraj 		if (data->flags == MMC_DATA_READ) {
26857418d21SSandeep Paulraj 			/*
26957418d21SSandeep Paulraj 			 * MMC controller sets the Data receive ready bit
27057418d21SSandeep Paulraj 			 * (DRRDY) in MMCST0 even before the entire FIFO is
27157418d21SSandeep Paulraj 			 * full. This results in erratic behavior if we start
27257418d21SSandeep Paulraj 			 * reading the FIFO soon after DRRDY.  Wait for the
27357418d21SSandeep Paulraj 			 * FIFO full bit in MMCST1 for proper FIFO clearing.
27457418d21SSandeep Paulraj 			 */
27557418d21SSandeep Paulraj 			if (bytes_left > fifo_bytes)
27657418d21SSandeep Paulraj 				dmmc_wait_fifo_status(regs, 0x4a);
2773ba36d60SDavide Bonfanti 			else if (bytes_left == fifo_bytes) {
27857418d21SSandeep Paulraj 				dmmc_wait_fifo_status(regs, 0x40);
2793ba36d60SDavide Bonfanti 				if (cmd->cmdidx == MMC_CMD_SEND_EXT_CSD)
2803ba36d60SDavide Bonfanti 					udelay(600);
2813ba36d60SDavide Bonfanti 			}
28257418d21SSandeep Paulraj 
28357418d21SSandeep Paulraj 			for (i = 0; bytes_left && (i < fifo_words); i++) {
28457418d21SSandeep Paulraj 				cmddata = get_val(&regs->mmcdrr);
28557418d21SSandeep Paulraj 				memcpy(data_buf, (char *)&cmddata, 4);
28657418d21SSandeep Paulraj 				data_buf += 4;
28757418d21SSandeep Paulraj 				bytes_left -= 4;
28857418d21SSandeep Paulraj 			}
28957418d21SSandeep Paulraj 		} else {
29057418d21SSandeep Paulraj 			/*
29157418d21SSandeep Paulraj 			 * MMC controller sets the Data transmit ready bit
29257418d21SSandeep Paulraj 			 * (DXRDY) in MMCST0 even before the entire FIFO is
29357418d21SSandeep Paulraj 			 * empty. This results in erratic behavior if we start
29457418d21SSandeep Paulraj 			 * writing the FIFO soon after DXRDY.  Wait for the
29557418d21SSandeep Paulraj 			 * FIFO empty bit in MMCST1 for proper FIFO clearing.
29657418d21SSandeep Paulraj 			 */
29757418d21SSandeep Paulraj 			dmmc_wait_fifo_status(regs, MMCST1_FIFOEMP);
29857418d21SSandeep Paulraj 			for (i = 0; bytes_left && (i < fifo_words); i++) {
29957418d21SSandeep Paulraj 				memcpy((char *)&cmddata, data_buf, 4);
30057418d21SSandeep Paulraj 				set_val(&regs->mmcdxr, cmddata);
30157418d21SSandeep Paulraj 				data_buf += 4;
30257418d21SSandeep Paulraj 				bytes_left -= 4;
30357418d21SSandeep Paulraj 			}
30457418d21SSandeep Paulraj 			dmmc_busy_wait(regs);
30557418d21SSandeep Paulraj 		}
30657418d21SSandeep Paulraj 	}
30757418d21SSandeep Paulraj 
30857418d21SSandeep Paulraj 	err = dmmc_check_status(regs, &mmcstatus, MMCST0_DATDNE, status_err);
30957418d21SSandeep Paulraj 	if (err)
31057418d21SSandeep Paulraj 		return err;
31157418d21SSandeep Paulraj 
31257418d21SSandeep Paulraj 	return 0;
31357418d21SSandeep Paulraj }
31457418d21SSandeep Paulraj 
31557418d21SSandeep Paulraj /* Initialize Davinci MMC controller */
31657418d21SSandeep Paulraj static int dmmc_init(struct mmc *mmc)
31757418d21SSandeep Paulraj {
31857418d21SSandeep Paulraj 	struct davinci_mmc *host = mmc->priv;
31957418d21SSandeep Paulraj 	struct davinci_mmc_regs *regs = host->reg_base;
32057418d21SSandeep Paulraj 
32157418d21SSandeep Paulraj 	/* Clear status registers explicitly - soft reset doesn't clear it
32257418d21SSandeep Paulraj 	 * If Uboot is invoked from UBL with SDMMC Support, the status
32357418d21SSandeep Paulraj 	 * registers can have uncleared bits
32457418d21SSandeep Paulraj 	 */
32557418d21SSandeep Paulraj 	get_val(&regs->mmcst0);
32657418d21SSandeep Paulraj 	get_val(&regs->mmcst1);
32757418d21SSandeep Paulraj 
32857418d21SSandeep Paulraj 	/* Hold software reset */
32957418d21SSandeep Paulraj 	set_bit(&regs->mmcctl, MMCCTL_DATRST);
33057418d21SSandeep Paulraj 	set_bit(&regs->mmcctl, MMCCTL_CMDRST);
33157418d21SSandeep Paulraj 	udelay(10);
33257418d21SSandeep Paulraj 
33357418d21SSandeep Paulraj 	set_val(&regs->mmcclk, 0x0);
33457418d21SSandeep Paulraj 	set_val(&regs->mmctor, 0x1FFF);
33557418d21SSandeep Paulraj 	set_val(&regs->mmctod, 0xFFFF);
33657418d21SSandeep Paulraj 
33757418d21SSandeep Paulraj 	/* Clear software reset */
33857418d21SSandeep Paulraj 	clear_bit(&regs->mmcctl, MMCCTL_DATRST);
33957418d21SSandeep Paulraj 	clear_bit(&regs->mmcctl, MMCCTL_CMDRST);
34057418d21SSandeep Paulraj 
34157418d21SSandeep Paulraj 	udelay(10);
34257418d21SSandeep Paulraj 
34357418d21SSandeep Paulraj 	/* Reset FIFO - Always use the maximum fifo threshold */
34457418d21SSandeep Paulraj 	set_val(&regs->mmcfifoctl, (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
34557418d21SSandeep Paulraj 	set_val(&regs->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
34657418d21SSandeep Paulraj 
34757418d21SSandeep Paulraj 	return 0;
34857418d21SSandeep Paulraj }
34957418d21SSandeep Paulraj 
35057418d21SSandeep Paulraj /* Set buswidth or clock as indicated by the GENERIC_MMC framework */
351*07b0b9c0SJaehoon Chung static int dmmc_set_ios(struct mmc *mmc)
35257418d21SSandeep Paulraj {
35357418d21SSandeep Paulraj 	struct davinci_mmc *host = mmc->priv;
35457418d21SSandeep Paulraj 	struct davinci_mmc_regs *regs = host->reg_base;
35557418d21SSandeep Paulraj 
35657418d21SSandeep Paulraj 	/* Set the bus width */
35757418d21SSandeep Paulraj 	if (mmc->bus_width == 4)
35857418d21SSandeep Paulraj 		set_bit(&regs->mmcctl, MMCCTL_WIDTH_4_BIT);
35957418d21SSandeep Paulraj 	else
36057418d21SSandeep Paulraj 		clear_bit(&regs->mmcctl, MMCCTL_WIDTH_4_BIT);
36157418d21SSandeep Paulraj 
36257418d21SSandeep Paulraj 	/* Set clock speed */
36357418d21SSandeep Paulraj 	if (mmc->clock)
36457418d21SSandeep Paulraj 		dmmc_set_clock(mmc, mmc->clock);
365*07b0b9c0SJaehoon Chung 
366*07b0b9c0SJaehoon Chung 	return 0;
36757418d21SSandeep Paulraj }
36857418d21SSandeep Paulraj 
369ab769f22SPantelis Antoniou static const struct mmc_ops dmmc_ops = {
370ab769f22SPantelis Antoniou 	.send_cmd	= dmmc_send_cmd,
371ab769f22SPantelis Antoniou 	.set_ios	= dmmc_set_ios,
372ab769f22SPantelis Antoniou 	.init		= dmmc_init,
373ab769f22SPantelis Antoniou };
374ab769f22SPantelis Antoniou 
37557418d21SSandeep Paulraj /* Called from board_mmc_init during startup. Can be called multiple times
37657418d21SSandeep Paulraj  * depending on the number of slots available on board and controller
37757418d21SSandeep Paulraj  */
37857418d21SSandeep Paulraj int davinci_mmc_init(bd_t *bis, struct davinci_mmc *host)
37957418d21SSandeep Paulraj {
38093bfd616SPantelis Antoniou 	host->cfg.name = "davinci";
38193bfd616SPantelis Antoniou 	host->cfg.ops = &dmmc_ops;
38293bfd616SPantelis Antoniou 	host->cfg.f_min = 200000;
38393bfd616SPantelis Antoniou 	host->cfg.f_max = 25000000;
38493bfd616SPantelis Antoniou 	host->cfg.voltages = host->voltages;
38593bfd616SPantelis Antoniou 	host->cfg.host_caps = host->host_caps;
38657418d21SSandeep Paulraj 
38793bfd616SPantelis Antoniou 	host->cfg.b_max = DAVINCI_MAX_BLOCKS;
38857418d21SSandeep Paulraj 
38993bfd616SPantelis Antoniou 	mmc_create(&host->cfg, host);
39057418d21SSandeep Paulraj 
39157418d21SSandeep Paulraj 	return 0;
39257418d21SSandeep Paulraj }
393