[U-Boot] [PATCH 1/2] drivers/mmc/atmel_mci.c: port to the new CONFIG_GENERIC_MMC API

Albin Tonnerre albin.tonnerre at free-electrons.com
Wed Aug 26 11:42:49 CEST 2009


This patch updates (well, pretty much rewrites, actually) the atmel_mci driver
so that it can be used with the new mmc API.
Currently, the driver only supports what the previous driver supported, that
is:
 - assumes that the MMC slot was configured in the cpu/board code, no
   slot-switching
 - doesn't support writing, only reading
Support for these features will come in a later patch.

Signed-off-by: Albin Tonnerre <albin.tonnerre at free-electrons.com>
---
 drivers/mmc/atmel_mci.c |  604 +++++++++++++++++++----------------------------
 1 files changed, 240 insertions(+), 364 deletions(-)

diff --git a/drivers/mmc/atmel_mci.c b/drivers/mmc/atmel_mci.c
index 3946ffe..b07deba 100644
--- a/drivers/mmc/atmel_mci.c
+++ b/drivers/mmc/atmel_mci.c
@@ -1,6 +1,9 @@
 /*
  * Copyright (C) 2004-2006 Atmel Corporation
  *
+ * Copyright (C) 2009
+ * Albin Tonnerre, Free-Electrons <albin.tonnerre at free-electrons.com>
+ *
  * See file CREDITS for list of people who contributed to this
  * project.
  *
@@ -38,40 +41,43 @@
 #define pr_debug(...) do { } while(0)
 #endif
 
-#ifndef CONFIG_SYS_MMC_CLK_OD
-#define CONFIG_SYS_MMC_CLK_OD		150000
-#endif
-
-#ifndef CONFIG_SYS_MMC_CLK_PP
-#define CONFIG_SYS_MMC_CLK_PP		5000000
-#endif
-
-#ifndef CONFIG_SYS_MMC_OP_COND
-#define CONFIG_SYS_MMC_OP_COND		0x00100000
-#endif
-
-#define MMC_DEFAULT_BLKLEN	512
-#define MMC_DEFAULT_RCA		1
+#define RESP_NO_CRC		1
+#define RESP_48BITS		MMCI_BF(RSPTYP, 1)
+#define RESP_136BITS		MMCI_BF(RSPTYP, 2)
+#define RESP_48BITS_NOCRC	(RESP_48BITS | RESP_NO_CRC)
+#define NID			MMCI_BF(MAXLAT, 0)
+#define NCR			MMCI_BF(MAXLAT, 1)
+#define TRCMD_START		MMCI_BF(TRCMD, 1)
+#define TRCMD_STOP		MMCI_BF(TRCMD, 2)
+#define TRDIR_READ		MMCI_BF(TRDIR, 1)
+#define TRTYP_BLOCK		MMCI_BF(TRTYP, 0)
+
+#define ERROR_FLAGS	(MMCI_BIT(DCRCE)		\
+			 | MMCI_BIT(RDIRE)		\
+			 | MMCI_BIT(RENDE)		\
+			 | MMCI_BIT(RINDE)		\
+			 | MMCI_BIT(RTOE))
 
-static unsigned int mmc_rca;
-static int mmc_card_is_sd;
-static block_dev_desc_t mmc_blkdev;
+struct atmel_mci_host {
+	struct mmc		*mmc;
+	struct mmc_cmd		*cmd;
+	struct mmc_data		*data;
+	u32 dtor;
+};
 
-block_dev_desc_t *mmc_get_dev(int dev)
-{
-	return &mmc_blkdev;
-}
+static struct atmel_mci_host atmel_mci_host;
+static struct atmel_mci_host *host = &atmel_mci_host;
 
-static void mci_set_mode(unsigned long hz, unsigned long blklen)
+static void atmel_mci_set_clk_rate(unsigned long hz)
 {
 	unsigned long bus_hz;
 	unsigned long clkdiv;
+	u32 mr;
 
 	bus_hz = get_mci_clk_rate();
 	clkdiv = (bus_hz / hz) / 2 - 1;
 
-	pr_debug("mmc: setting clock %lu Hz, block size %lu\n",
-		 hz, blklen);
+	pr_debug("mmc: setting clock %lu Hz\n", hz);
 
 	if (clkdiv & ~255UL) {
 		clkdiv = 255;
@@ -79,34 +85,22 @@ static void mci_set_mode(unsigned long hz, unsigned long blklen)
 			hz);
 	}
 
-	blklen &= 0xfffc;
-	mmci_writel(MR, (MMCI_BF(CLKDIV, clkdiv)
-			 | MMCI_BF(BLKLEN, blklen)
-			 | MMCI_BIT(RDPROOF)
-			 | MMCI_BIT(WRPROOF)));
+	mr = mmci_readl(MR);
+	mmci_writel(MR, MMCI_BFINS(CLKDIV, clkdiv, mr));
 }
 
-#define RESP_NO_CRC	1
-#define R1		MMCI_BF(RSPTYP, 1)
-#define R2		MMCI_BF(RSPTYP, 2)
-#define R3		(R1 | RESP_NO_CRC)
-#define R6		R1
-#define NID		MMCI_BF(MAXLAT, 0)
-#define NCR		MMCI_BF(MAXLAT, 1)
-#define TRCMD_START	MMCI_BF(TRCMD, 1)
-#define TRDIR_READ	MMCI_BF(TRDIR, 1)
-#define TRTYP_BLOCK	MMCI_BF(TRTYP, 0)
-#define INIT_CMD	MMCI_BF(SPCMD, 1)
-#define OPEN_DRAIN	MMCI_BF(OPDCMD, 1)
-
-#define ERROR_FLAGS	(MMCI_BIT(DTOE)			\
-			 | MMCI_BIT(RDIRE)		\
-			 | MMCI_BIT(RENDE)		\
-			 | MMCI_BIT(RINDE)		\
-			 | MMCI_BIT(RTOE))
+
+static void atmel_mci_set_blklen(unsigned long blklen)
+{
+	u32 mr;
+
+	blklen &= 0xfffc;
+	mr = mmci_readl(MR);
+	mmci_writel(MR, MMCI_BFINS(BLKLEN, blklen, mr));
+}
 
 static int
-mmc_cmd(unsigned long cmd, unsigned long arg,
+atmel_mci_mmc_cmd(unsigned long cmd, unsigned long arg,
 	void *resp, unsigned long flags)
 {
 	unsigned long *response = resp;
@@ -140,7 +134,7 @@ mmc_cmd(unsigned long cmd, unsigned long arg,
 	if (status & error_flags) {
 		printf("mmc: command %lu failed (status: 0x%08x)\n",
 		       cmd, status);
-		return -EIO;
+		return status;
 	}
 
 	if (response_words)
@@ -155,379 +149,261 @@ mmc_cmd(unsigned long cmd, unsigned long arg,
 	return 0;
 }
 
-static int mmc_acmd(unsigned long cmd, unsigned long arg,
-		    void *resp, unsigned long flags)
-{
-	unsigned long aresp[4];
-	int ret;
-
-	/*
-	 * Seems like the APP_CMD part of an ACMD has 64 cycles max
-	 * latency even though the ACMD part doesn't. This isn't
-	 * entirely clear in the SD Card spec, but some cards refuse
-	 * to work if we attempt to use 5 cycles max latency here...
-	 */
-	ret = mmc_cmd(MMC_CMD_APP_CMD, 0, aresp,
-		      R1 | NCR | (flags & OPEN_DRAIN));
-	if (ret)
-		return ret;
-	if ((aresp[0] & (R1_ILLEGAL_COMMAND | R1_APP_CMD)) != R1_APP_CMD)
-		return -ENODEV;
-
-	ret = mmc_cmd(cmd, arg, resp, flags);
-	return ret;
-}
-
-static unsigned long
-mmc_bread(int dev, unsigned long start, lbaint_t blkcnt,
-	  void *buffer)
+/*
+ * Trying to set the timeout in DTOR is useless here. On AT91, a hardware
+ * issue prevents the DTOE flag from being raised. On AT32P, DTOE won't
+ * raise for blocks smaller than 5 bytes, which might be an issue when
+ * doing writes. Just using a software timeout in all cases looks like
+ * the most reasonable solution.
+ */
+static u32 atmel_mci_calc_data_timeout(struct mmc *mmc)
 {
-	int ret, i = 0;
-	unsigned long resp[4];
-	unsigned long card_status, data;
-	unsigned long wordcount;
-	u32 *p = buffer;
-	u32 status;
-
-	if (blkcnt == 0)
-		return 0;
+	struct mmc_csd *csd = (struct mmc_csd *) mmc->csd;
 
-	pr_debug("mmc_bread: dev %d, start %lx, blkcnt %lx\n",
-		 dev, start, blkcnt);
-
-	/* Put the device into Transfer state */
-	ret = mmc_cmd(MMC_CMD_SELECT_CARD, mmc_rca << 16, resp, R1 | NCR);
-	if (ret) goto out;
-
-	/* Set block length */
-	ret = mmc_cmd(MMC_CMD_SET_BLOCKLEN, mmc_blkdev.blksz, resp, R1 | NCR);
-	if (ret) goto out;
+	static const unsigned int dtomul_to_shift[] = {
+		0, 4, 7, 8, 10, 12, 16, 20,
+	};
+	static const unsigned int taac_exp[] = {
+		1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
+	};
+	static const unsigned int taac_mant[] = {
+		0,  10, 12, 13, 15, 60, 25, 30,
+		35, 40, 45, 50, 55, 60, 70, 80,
+	};
+	unsigned int timeout_ns, timeout_clks;
+	unsigned int e, m;
+	unsigned int dtocyc, dtomul;
+	unsigned int shift;
 
-	pr_debug("MCI_DTOR = %08lx\n", mmci_readl(DTOR));
+	e = csd->taac & 0x07;
+	m = (csd->taac >> 3) & 0x0f;
 
-	for (i = 0; i < blkcnt; i++, start++) {
-		ret = mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK,
-			      start * mmc_blkdev.blksz, resp,
-			      (R1 | NCR | TRCMD_START | TRDIR_READ
-			       | TRTYP_BLOCK));
-		if (ret) goto out;
+	timeout_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
+	timeout_clks = csd->nsac * 100;
 
-		ret = -EIO;
-		wordcount = 0;
-		do {
-			do {
-				status = mmci_readl(SR);
-				if (status & (ERROR_FLAGS | MMCI_BIT(OVRE)))
-					goto read_error;
-			} while (!(status & MMCI_BIT(RXRDY)));
-
-			if (status & MMCI_BIT(RXRDY)) {
-				data = mmci_readl(RDR);
-				/* pr_debug("%x\n", data); */
-				*p++ = data;
-				wordcount++;
-			}
-		} while(wordcount < (mmc_blkdev.blksz / 4));
-
-		pr_debug("mmc: read %u words, waiting for BLKE\n", wordcount);
+	timeout_clks += (((timeout_ns + 9) / 10)
+			 * ((mmc->clock + 99999) / 100000) + 9999) / 10000;
+	if (IS_SD(mmc))
+		timeout_clks *= 10;
+	else
+		timeout_clks *= 100;
 
-		do {
-			status = mmci_readl(SR);
-		} while (!(status & MMCI_BIT(BLKE)));
+	dtocyc = timeout_clks;
+	dtomul = 0;
+	shift = 0;
+	while (dtocyc > 15 && dtomul < 8) {
+		dtomul++;
+		shift = dtomul_to_shift[dtomul];
+		dtocyc = (timeout_clks + (1 << shift) - 1) >> shift;
+	}
 
-		putc('.');
+	if (dtomul >= 8) {
+		dtomul = 7;
+		dtocyc = 15;
+		puts("Warning: Using maximum data timeout\n");
 	}
 
-out:
-	/* Put the device back into Standby state */
-	mmc_cmd(MMC_CMD_SELECT_CARD, 0, resp, NCR);
-	return i;
+	pr_debug("mmc: Using %u cycles data timeout\n", dtocyc);
 
-read_error:
-	mmc_cmd(MMC_CMD_SEND_STATUS, mmc_rca << 16, &card_status, R1 | NCR);
-	printf("mmc: bread failed, status = %08x, card status = %08lx\n",
-	       status, card_status);
-	goto out;
+	return dtomul * (dtocyc << shift);
 }
 
-static void mmc_parse_cid(struct mmc_cid *cid, unsigned long *resp)
+static void atmel_mci_finish_request(struct atmel_mci_host *host,
+		struct mmc_cmd *cmd, struct mmc_data *data)
 {
-	cid->mid = resp[0] >> 24;
-	cid->oid = (resp[0] >> 8) & 0xffff;
-	cid->pnm[0] = resp[0];
-	cid->pnm[1] = resp[1] >> 24;
-	cid->pnm[2] = resp[1] >> 16;
-	cid->pnm[3] = resp[1] >> 8;
-	cid->pnm[4] = resp[1];
-	cid->pnm[5] = resp[2] >> 24;
-	cid->pnm[6] = 0;
-	cid->prv = resp[2] >> 16;
-	cid->psn = (resp[2] << 16) | (resp[3] >> 16);
-	cid->mdt = resp[3] >> 8;
+	host->cmd = NULL;
+	host->data = NULL;
 }
 
-static void sd_parse_cid(struct mmc_cid *cid, unsigned long *resp)
+static int atmel_mci_finish_data(struct atmel_mci_host *host, unsigned int stat)
 {
-	cid->mid = resp[0] >> 24;
-	cid->oid = (resp[0] >> 8) & 0xffff;
-	cid->pnm[0] = resp[0];
-	cid->pnm[1] = resp[1] >> 24;
-	cid->pnm[2] = resp[1] >> 16;
-	cid->pnm[3] = resp[1] >> 8;
-	cid->pnm[4] = resp[1];
-	cid->pnm[5] = 0;
-	cid->pnm[6] = 0;
-	cid->prv = resp[2] >> 24;
-	cid->psn = (resp[2] << 8) | (resp[3] >> 24);
-	cid->mdt = (resp[3] >> 8) & 0x0fff;
-}
+	int data_error = 0;
+	if (stat & ERROR_FLAGS) {
+		printf("request failed. status: 0x%08x\n",
+				stat);
+		if (stat & MMCI_BIT(DCRCE)) {
+			data_error = -EILSEQ;
+		} else if (stat & MMCI_BIT(DTOE)) {
+			data_error = TIMEOUT;
+		} else {
+			data_error = -EIO;
+		}
+	}
 
-static void mmc_dump_cid(const struct mmc_cid *cid)
-{
-	printf("Manufacturer ID:       %02X\n", cid->mid);
-	printf("OEM/Application ID:    %04X\n", cid->oid);
-	printf("Product name:          %s\n", cid->pnm);
-	printf("Product Revision:      %u.%u\n",
-	       cid->prv >> 4, cid->prv & 0x0f);
-	printf("Product Serial Number: %lu\n", cid->psn);
-	printf("Manufacturing Date:    %02u/%02u\n",
-	       cid->mdt >> 4, cid->mdt & 0x0f);
+	host->data = NULL;
+	return data_error;
 }
 
-static void mmc_dump_csd(const struct mmc_csd *csd)
+static int atmel_mci_pull(struct atmel_mci_host *host, void *_buf, int bytes)
 {
-	unsigned long *csd_raw = (unsigned long *)csd;
-	printf("CSD data: %08lx %08lx %08lx %08lx\n",
-	       csd_raw[0], csd_raw[1], csd_raw[2], csd_raw[3]);
-	printf("CSD structure version:   1.%u\n", csd->csd_structure);
-	printf("MMC System Spec version: %u\n", csd->spec_vers);
-	printf("Card command classes:    %03x\n", csd->ccc);
-	printf("Read block length:       %u\n", 1 << csd->read_bl_len);
-	if (csd->read_bl_partial)
-		puts("Supports partial reads\n");
-	else
-		puts("Does not support partial reads\n");
-	printf("Write block length:      %u\n", 1 << csd->write_bl_len);
-	if (csd->write_bl_partial)
-		puts("Supports partial writes\n");
-	else
-		puts("Does not support partial writes\n");
-	if (csd->wp_grp_enable)
-		printf("Supports group WP:      %u\n", csd->wp_grp_size + 1);
-	else
-		puts("Does not support group WP\n");
-	printf("Card capacity:		%u bytes\n",
-	       (csd->c_size + 1) * (1 << (csd->c_size_mult + 2)) *
-	       (1 << csd->read_bl_len));
-	printf("File format:            %u/%u\n",
-	       csd->file_format_grp, csd->file_format);
-	puts("Write protection:        ");
-	if (csd->perm_write_protect)
-		puts(" permanent");
-	if (csd->tmp_write_protect)
-		puts(" temporary");
-	putc('\n');
+	unsigned int stat;
+	u32 timeout = 0;
+	u32 delay, status;
+	u32 *buf = _buf;
+
+	delay = host->dtor / (get_mci_clk_rate() / 1000000);
+	while (bytes > 3) {
+		do {
+			status = mmci_readl(SR);
+			if (status & MMCI_BIT(RXRDY))
+				break;
+			udelay(delay / 1000);
+			timeout += delay / 1000;
+		} while (timeout < delay);
+		if(timeout > delay)
+			return MMCI_BIT(DTOE);
+		*buf++ = mmci_readl(RDR);
+		bytes -= 4;
+	}
+
+	while(mmci_readl(SR) & MMCI_BIT(DTIP));
+	return 0;
 }
 
-static int mmc_idle_cards(void)
+static int atmel_mci_transfer_data(struct atmel_mci_host *host)
 {
-	int ret;
+	struct mmc_data *data = host->data;
+	int stat;
+	unsigned long length;
 
-	/* Reset and initialize all cards */
-	ret = mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, NULL, 0);
-	if (ret)
-		return ret;
+	length = data->blocks * data->blocksize;
 
-	/* Keep the bus idle for 74 clock cycles */
-	return mmc_cmd(0, 0, NULL, INIT_CMD);
+	if (data->flags & MMC_DATA_READ) {
+		stat = atmel_mci_pull(host, data->dest, length);
+		if (stat)
+			return stat;
+	}
+	else
+		return -EINVAL;
+	return 0;
 }
 
-static int sd_init_card(struct mmc_cid *cid, int verbose)
+static int atmel_mci_cmd_done(struct atmel_mci_host *host, unsigned int stat)
 {
-	unsigned long resp[4];
-	int i, ret = 0;
-
-	mmc_idle_cards();
-	for (i = 0; i < 1000; i++) {
-		ret = mmc_acmd(SD_CMD_APP_SEND_OP_COND, CONFIG_SYS_MMC_OP_COND,
-			       resp, R3 | NID);
-		if (ret || (resp[0] & 0x80000000))
-			break;
-		ret = -ETIMEDOUT;
-	}
+	int datastat;
+	int ret;
 
-	if (ret)
-		return ret;
+	if(stat & MMCI_BIT(RTOE))
+		ret = TIMEOUT;
+	else
+		ret = stat;
 
-	ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID);
-	if (ret)
+	if (ret) {
+		atmel_mci_finish_request(host, host->cmd, host->data);
 		return ret;
-	sd_parse_cid(cid, resp);
-	if (verbose)
-		mmc_dump_cid(cid);
+	}
 
-	/* Get RCA of the card that responded */
-	ret = mmc_cmd(SD_CMD_SEND_RELATIVE_ADDR, 0, resp, R6 | NCR);
-	if (ret)
-		return ret;
+	if (!host->data) {
+		atmel_mci_finish_request(host, host->cmd, host->data);
+		return 0;
+	}
 
-	mmc_rca = resp[0] >> 16;
-	if (verbose)
-		printf("SD Card detected (RCA %u)\n", mmc_rca);
-	mmc_card_is_sd = 1;
-	return 0;
+	datastat = atmel_mci_transfer_data(host);
+	ret = atmel_mci_finish_data(host, datastat);
+	atmel_mci_finish_request(host, host->cmd, host->data);
+	return ret;
 }
 
-static int mmc_init_card(struct mmc_cid *cid, int verbose)
+static void atmel_mci_setup_data(struct atmel_mci_host *host, struct mmc_data *data)
 {
-	unsigned long resp[4];
-	int i, ret = 0;
-
-	mmc_idle_cards();
-	for (i = 0; i < 1000; i++) {
-		ret = mmc_cmd(MMC_CMD_SEND_OP_COND, CONFIG_SYS_MMC_OP_COND, resp,
-			      R3 | NID | OPEN_DRAIN);
-		if (ret || (resp[0] & 0x80000000))
-			break;
-		ret = -ETIMEDOUT;
-	}
-
-	if (ret)
-		return ret;
+	host->data = data;
+	atmel_mci_set_blklen(data->blocksize);
+}
 
-	/* Get CID of all cards. FIXME: Support more than one card */
-	ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID | OPEN_DRAIN);
-	if (ret)
-		return ret;
-	mmc_parse_cid(cid, resp);
-	if (verbose)
-		mmc_dump_cid(cid);
+static int atmel_mci_request(struct mmc *mmc, struct mmc_cmd *cmd,
+		struct mmc_data *data)
+{
+	u32 flags = 0;
+	int ret;
+	struct atmel_mci_host *host = mmc->priv;
+
+	host->cmd = cmd;
+	host->mmc = mmc;
+
+	/* Only set the timeout once, when we know we have read the CSD */
+	if(!host->dtor && mmc->capacity)
+		host->dtor = atmel_mci_calc_data_timeout(mmc);
+
+	switch (cmd->resp_type) {
+	case MMC_RSP_R1: /* short CRC, OPCODE */
+	case MMC_RSP_R1b:/* short CRC, OPCODE, BUSY */
+		flags |= RESP_48BITS;
+		break;
+	case MMC_RSP_R2: /* long 136 bit + CRC */
+		flags |= RESP_136BITS;
+		break;
+	case MMC_RSP_R3: /* short */
+		flags |= RESP_48BITS_NOCRC;
+		break;
+	case MMC_RSP_NONE:
+		break;
+	default:
+		printf("atmel_mci: unhandled response type 0x%x\n",
+			cmd->resp_type);
+		return -EINVAL;
+	}
+	flags |= NCR;
 
-	/* Set Relative Address of the card that responded */
-	ret = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, mmc_rca << 16, resp,
-		      R1 | NCR | OPEN_DRAIN);
-	return ret;
+	if(data) {
+		atmel_mci_setup_data(host, data);
+		flags |= TRCMD_START | TRDIR_READ | TRTYP_BLOCK;
+	}
+	ret = atmel_mci_mmc_cmd(cmd->cmdidx, cmd->cmdarg, cmd->response, cmd->flags | flags);
+	return atmel_mci_cmd_done(host, ret);
 }
 
-static void mci_set_data_timeout(struct mmc_csd *csd)
+static void atmel_mci_set_ios(struct mmc *mmc)
 {
-	static const unsigned int dtomul_to_shift[] = {
-		0, 4, 7, 8, 10, 12, 16, 20,
-	};
-	static const unsigned int taac_exp[] = {
-		1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
-	};
-	static const unsigned int taac_mant[] = {
-		0,  10, 12, 13, 15, 60, 25, 30,
-		35, 40, 45, 50, 55, 60, 70, 80,
-	};
-	unsigned int timeout_ns, timeout_clks;
-	unsigned int e, m;
-	unsigned int dtocyc, dtomul;
-	unsigned int shift;
-	u32 dtor;
+	u32 sdcr;
+	struct atmel_mci_host *host = mmc->priv;
 
-	e = csd->taac & 0x07;
-	m = (csd->taac >> 3) & 0x0f;
+	if(mmc->clock)
+		atmel_mci_set_clk_rate(mmc->clock);
+	sdcr = mmci_readl(SDCR);
 
-	timeout_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
-	timeout_clks = csd->nsac * 100;
-
-	timeout_clks += (((timeout_ns + 9) / 10)
-			 * ((CONFIG_SYS_MMC_CLK_PP + 99999) / 100000) + 9999) / 10000;
-	if (!mmc_card_is_sd)
-		timeout_clks *= 10;
+	if(mmc->bus_width == 4)
+		sdcr |= MMCI_BIT(SCDBUS);
 	else
-		timeout_clks *= 100;
-
-	dtocyc = timeout_clks;
-	dtomul = 0;
-	shift = 0;
-	while (dtocyc > 15 && dtomul < 8) {
-		dtomul++;
-		shift = dtomul_to_shift[dtomul];
-		dtocyc = (timeout_clks + (1 << shift) - 1) >> shift;
-	}
+		sdcr &= ~MMCI_BIT(SCDBUS);
 
-	if (dtomul >= 8) {
-		dtomul = 7;
-		dtocyc = 15;
-		puts("Warning: Using maximum data timeout\n");
-	}
+	mmci_writel(SDCR, sdcr);
+}
 
-	dtor = (MMCI_BF(DTOMUL, dtomul)
-		| MMCI_BF(DTOCYC, dtocyc));
-	mmci_writel(DTOR, dtor);
+static int atmel_mci_mmc_init(struct mmc *mmc)
+{
+	mmci_writel(CR, MMCI_BIT(SWRST));
+	mmci_writel(MR, MMCI_BIT(RDPROOF) | MMCI_BIT(WRPROOF));
+	atmel_mci_set_clk_rate(mmc->f_min);
 
-	printf("mmc: Using %u cycles data timeout (DTOR=0x%x)\n",
-	       dtocyc << shift, dtor);
+	return 0;
 }
 
-int mmc_legacy_init(int verbose)
+int atmel_mci_init(bd_t *bis)
 {
-	struct mmc_cid cid;
-	struct mmc_csd csd;
-	unsigned int max_blksz;
-	int ret;
+	struct mmc *mmc = NULL;
+
+	mmc = calloc(1, sizeof(struct mmc));
+	if (!mmc)
+		return -ENOMEM;
 
-	/* Initialize controller */
-	mmci_writel(CR, MMCI_BIT(SWRST));
 	mmci_writel(CR, MMCI_BIT(MCIEN));
 	mmci_writel(DTOR, 0x5f);
 	mmci_writel(IDR, ~0UL);
-	mci_set_mode(CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
-
-	mmc_card_is_sd = 0;
 
-	ret = sd_init_card(&cid, verbose);
-	if (ret) {
-		mmc_rca = MMC_DEFAULT_RCA;
-		ret = mmc_init_card(&cid, verbose);
-	}
-	if (ret)
-		return ret;
+	sprintf(mmc->name, "ATMEL MCI");
+	mmc->send_cmd = atmel_mci_request;
+	mmc->set_ios = atmel_mci_set_ios;
+	mmc->init = atmel_mci_mmc_init;
+	mmc->host_caps = MMC_MODE_4BIT;
 
-	/* Get CSD from the card */
-	ret = mmc_cmd(MMC_CMD_SEND_CSD, mmc_rca << 16, &csd, R2 | NCR);
-	if (ret)
-		return ret;
-	if (verbose)
-		mmc_dump_csd(&csd);
-
-	mci_set_data_timeout(&csd);
-
-	/* Initialize the blockdev structure */
-	mmc_blkdev.if_type = IF_TYPE_MMC;
-	mmc_blkdev.part_type = PART_TYPE_DOS;
-	mmc_blkdev.block_read = mmc_bread;
-	sprintf((char *)mmc_blkdev.vendor,
-		"Man %02x%04x Snr %08lx",
-		cid.mid, cid.oid, cid.psn);
-	strncpy((char *)mmc_blkdev.product, cid.pnm,
-		sizeof(mmc_blkdev.product));
-	sprintf((char *)mmc_blkdev.revision, "%x %x",
-		cid.prv >> 4, cid.prv & 0x0f);
-
-	/*
-	 * If we can't use 512 byte blocks, refuse to deal with the
-	 * card. Tons of code elsewhere seems to depend on this.
-	 */
-	max_blksz = 1 << csd.read_bl_len;
-	if (max_blksz < 512 || (max_blksz > 512 && !csd.read_bl_partial)) {
-		printf("Card does not support 512 byte reads, aborting.\n");
-		return -ENODEV;
-	}
-	mmc_blkdev.blksz = 512;
-	mmc_blkdev.lba = (csd.c_size + 1) * (1 << (csd.c_size_mult + 2));
+	mmc->voltages = MMC_VDD_32_33;
 
-	mci_set_mode(CONFIG_SYS_MMC_CLK_PP, mmc_blkdev.blksz);
+	mmc->f_min = 150000;
+	mmc->f_max = 5000000;
 
-#if 0
-	if (fat_register_device(&mmc_blkdev, 1))
-		printf("Could not register MMC fat device\n");
-#else
-	init_part(&mmc_blkdev);
-#endif
+	mmc->priv = host;
+	host->mmc = mmc;
 
+	mmc_register(mmc);
 	return 0;
 }
-- 
1.6.0.4



More information about the U-Boot mailing list